Journey of a packet throughny the linux 2.4 network stack --------------------------------------- ---------------------------------------- This Document Describes the journey of a network packet Inside the Linux kernel 2.4.x. This Has Changed Dravestical Since 2.2 Because The GlobalLiRized Bottom Half Was Abandoned In Favor of The New Softirq System. -------------------------------------------------------------------------------------------------------- -------------------------------------------------- ------------
1. Prefacei Have to Excuse for My Ignorance, But this Document Has A Strong Focus on The "default case": x86 Architecture and ip packets which get forwarded.
I am Definitely no kener guru and the information provided by this document may be wrong. So don't expect to much, i'll always appreciate your comments and bugfixes.
2. Receiving the packet2.1 The receive interruptIf the network card receives an ethernet frame which matches the local MAC address or is a linklayer broadcast, it issues an interrupt. The network driver for this particular card handles the interrupt, fetches the packet data via DMA / PIO / WHATO RAM. IT THEN Allocates A SKB AND Calls A Function of The Protocol Independent Device Support Routines: Net / Core / Dev.c: Netif_Rx (SKB).
If the driver did not already timestamp the skb, it is timestamped now. Afterwards the skb gets enqueued in the apropriate queue for the processor handling this packet. If the queue backlog is full the packet is dropped at this place. After enqueuing the skb The Receive SoftInterrupt IS Marked for Execution Via include / Linux / Interrupt.h: __ cpu_raise_softirq ().
The Interrupt Handler EXITS AND All Interrupts Are Reenabled.
2.2 The network RX softirqNow we encounter one of the big changes between 2.2 and 2.4: The whole network stack is no longer a bottom half, but a softirq Softirqs have the major advantage, that they may run on more than one CPU simultaneously bh's.. were guaranteed to run only on one CPU at a time.Our network receive softirq is registered in net / core / dev.c: net_init () using the function kernel / softirq.c: open_softirq () provided by the softirq subsystem.
Further handling of our packet is done in the network receive softirq (NET_RX_SOFTIRQ) which is called from kernel / softirq.c:. Do_softirq () do_softirq () itself is called from three places within the kernel:
from arch / i386 / kernel / irq.c: do_IRQ (), which is the generic IRQ handlerfrom arch / i386 / kernel / entry.S in case the kernel just returned from a syscallinside the main process scheduler in kernel / sched.c: schedule () So if execution passes one of these points, do_softirq () is called, it detects the NET_RX_SOFTIRQ marked an calls net / core / dev.c:. net_rx_action () Here the sbk is dequeued from this cpu's receive queue and afterwards handled To the apriate packet handler. in case of ipv4 this is the ipv4 packet handler.
2.3 The IPv4 Packet Handlerthe IP Packet Handler Is Registered Via Net / Core / Dev.c: dev_add_pack () Called from Net / IPv4 / IP_Output.c: ip_init ().
The IPv4 packet handling function is net / ipv4 / ip_input.c:.. Ip_rcv () After some initial checks (if the packet is for this host, ...) the ip checksum is calculated Additional checks are done on the length and IP Protocol Version 4.
Every Packet Failing One of the Sanity Checks Is Dropped at this point.
IF The Packet Passs The Tests, We Determine The Size of The IP Packet and Trim The Skb in Case The Transport Medium Has Appended Some Padding.Now It Is The First Time One of the Netfilter Hooks IS Called.
Netfilter provides an generict and abstract interface to the standard routing code. This is currently used for packet filtering, mangling, NAT and queuing packets to userspace. For further reference see my conference paper 'The netfilter subsystem in Linux 2.4' or one of Rustys unreliable Guides, IE The Netfilter-Hacking-Guide.
After Successful Traversal The Netfilter Hook, Net / IPv4 / IPV_INPUT.C: IP_RCV_FINISH () is Called.
Inside ip_rcv_finish (), the packet's destination is determined by calling the routing function net / ipv4 / route.c:.. Ip_route_input () Furthermore, if our IP packet has IP options, they are processed now Depending on the routing decision made by net /ipv4/route.c:ip_route_input_slow (), The Journey of Our Packet Continues in One of the Following Functions:
Net / IPv4 / ip_input.c: ip_local_deliver () The Packet's Destination IS Local, We Have to Process The Layer 4 Protocol and pass it to an useerspace process.
Net / IPv4 / ip_forward.c: ip_forward () The Packet's Destination IS Not Local, We Have To Forward It To Another NetWork
Net / IPv4 / route.c: IP_ERROR () An Error Occurred, WE Are Unable to find an aprice routing Table Entry for this packet.
Net / IPv4 / IPMR.c: IP_MR_INPUT () IT IS A MULTICAST PACKET AND We Have to Do Some Multicast Routing.
3. Packet Forwarding to another Deviceif The Routing Decided That Packet Has To Be Forwarded To Another Device, The Function Net / IPv4 / IP_FORWARD.C: IP_FORWARD () IS CALLED.
The first task of this function is to check the ip header's TTL. If it is <= 1 we drop the packet and return an ICMP time exceeded message to the sender.We check the header's tailroom if we have enough tailroom for the destination device's link Layer Header and Expand The SKB IF NECcessary.
Next the ttl is decrement by one.
IF Our New Packet Is Bigger THEN THE MTU OF THE DESTINATION DEW Header IS Set, We Drop The Packet, We Drop The Packet And Send A ICMP FRAG NEDED MESSAGE TO The sender.
Finally it is time to call another one of the netfilter hooks - this time it is the nf_ip_forward hook.
Assuming That the netfilter hooks is return a nf_accept verdict, the function net / ipv4 / ip_forward.c: ip_forward_finish () is The next step in Our packet's journey.
IP_forward_finish () Itself checks if we need to set any additional options in the ip header, and has ip_optfixme doing this. AfterWards it calls include / net / ip.h: ip_send ().
IF WE NEED SOME FRAGMENTATION, FIXME: IP_FRAGMENT GETS CALED, OTHERWISE We Continue IN NET / IPv4 / IP_forward: IP_FINISH_OUTPUT ().
IP_FINISH_OUTPUT () AGAIN DOES NOTING ELSE THAN CALLING The Netfilter PostRouting Hook NF_IP_POST_ROUTING AND CALLING IP_FINISH_OUTPUT2 () ON Successful Traversal of this hook.
IP_FINISH_OUTPUT2 () Calls Prepends The Hardware (Link Layer) Header To Our SKB and Calls Net / IPv4 / IP_output.c: ip_output ().