原创 2004年09月29日 10:53:00

Receiving IP Packets

At boot time, the network drivers are built into the kernel and initialized. This results in a series of device data structures linked together in the dev_base list.Each device data structure describes its device and provides a set of callback routines that the network protocol layers call when they need the network driver to perform work.These functions are mostly concerned with transmitting data and with the network device's addresses.When a network device receives packets from its network it must convert the received data into sk_buff data structures. These received sk_buff's are added onto the backlog queue by the network drivers as they are received.If the backlog queue grows too large, then the received sk_buff's are discarded. The network bottom half is flagged as ready to run as there is work to do.

When the network bottom half handler is run by the scheduler it processes any network packets waiting to be transmitted before processing the backlog queue of sk_buff's determining which protocol layer to pass the received packets to.As the Linux networking layers were initialized, each protocol registered itself by adding a packet_type (include/linux/netdevice.h) data structure onto either the ptype_all list or into the ptype_base hash table.The packet_type data structure contains the protocol type, a pointer to a network device, a pointer to the protocol's receive data processing routine and, finally, a pointer to the next packet_type data structure in the list or hash chain. The ptype_all chain is used to snoop all packets being received from any network device and is not normally used.The ptype_base hash table is hashed by protocol identifier and is used to decide which protocol should receive the incoming network packet. The network bottom half matches the protocol types of incoming sk_buff's against one or more of the packet type entries in either table. The protocol may match more than one entry, for example when snooping all network traffic, and in this case the sk_buff will be cloned.The sk_buff is passed to the matching protocol's handling routine. For example, IP packets are passed onto ip_rcv.

Sending IP Packets

Packets are transmitted by applications exchanging data or else they are generated by the network protocols as they support established connections or connections being established. Whichever way the data is generated, an sk_buff is built to contain the data and various headers are added by the protocol layers as it passes through them.

The sk_buff needs to be passed to a network device to be transmitted.First though the protocol, for example IP, needs to decide which network device to use.This depends on the best route for the packet.For computers connected by modem to a single network, say via the PPP protocol, the routing choice is easy.The packet should either be sent to the local host via the loopback device or to the gateway at the end of the PPP modem connection. For computers connected to an Ethernet the choices are harder as there are many computers connected to the network. For every IP packet transmitted. IP uses the routing tables to resolve the route for the destination IP address. Each IP destination successfully looked up in the routing tables returns a rtable data structure describing the route to use.This includes the source IP address to use, the address of the network device data structure and, sometimes, a prebuilt hardware header. This hardware header is network device specific and contains the source and destination physical addresses and other media specific information. If the network device is an ethernet device, the hardware header would be as shown in Figure [*] and the source and destination addresses would be physical ethernet addresses. The hardware header is cached with the route because it must be appended to each IP packet transmitted on this route and constructing it takes time. The hardware header may contain physical addresses that have to be resolved using the ARP protocol. In this case the outgoing packet is stalled until the address has been resolved.Once it has been resolved and the hardware header built. the hardware header is cached so that future IP packets sent using this interface do not have to ARP.

Forwarding IP Packets

The packets that are from a source and to a destination other than this machine are forwarded to the next hop machine provided that this machine is configured to be an ip_forwarder and there is a valid routing table entry for the destination machine. This machine will act as a router. The ip_forwarding can be controlled via a configuration parameter or a sysctl variable using the proc system interface. The function ip_rcv checks if a particular packet is for the machine itself. If yes, it will provide the packet to the appropriate upper level protocol's receive handler. If this is not the case, and the ip_forwarding is enabled, it will call the ip_forward routine. The ip_forward routine will consult the routing table to find the next hop for this destination and transmits the packet to that machine. But, before this, it might have to masquerade the packet and it is explained shortly.


  • wolflion
  • wolflion
  • 2009年11月19日 17:52
  • 4482


主机A与主机B通信,A Ping B,图:A-交换机-B,AB同一子网 (1)ping数据包用的是ICMP协议,IP协议的一个子协议,位于三层,包含A的IP,B的IP,三层进行IP封装成包,进入二层...
  • u013485792
  • u013485792
  • 2016年03月18日 17:16
  • 2429


  • xiliang_pan
  • xiliang_pan
  • 2012年08月30日 14:22
  • 13991


A-----(B1-B2)-----(C1-C2)-------E 就假设拓扑图是这个样子吧,B1和B2是路由器B上的两个接口,C1和C2是路由器C上的两个接口,A和E是PC,由主机A向主机...
  • xbgprogrammer
  • xbgprogrammer
  • 2016年01月12日 23:12
  • 1985


TCP/IP–IP层转发分组的流程标签(空格分隔): 网络协议 路由选择一、前言接着上一篇TCP/IP–网络协议,这篇继续网际协议IP的内容,在了解了IP协议的基础上,继续学习IP路由选择。先看张图,...
  • w1992wishes
  • w1992wishes
  • 2017年09月26日 16:16
  • 295


一.编辑过,可以运行的原始数据包传送代码 自定义IP包头,IP_OPTIONS,TCP包头,具体如下: //---cat rawtcp.c--- // Run as root or SUID 0...
  • lieye_leaves
  • lieye_leaves
  • 2014年04月08日 10:39
  • 5700

数据包接收系列 — IP协议处理流程(一)

本文主要内容:在接收数据包时,IP协议的处理流程。 内核版本:2.6.37 Author:zhangskd @ csdn blog    IP报头   IP报头: struct iphd...
  • zhangskd
  • zhangskd
  • 2014年04月02日 16:32
  • 4606


最近学习交换与路由知识,做点笔记,作为以后的复习使用,也分享给网友学习使用。     二层交换与三层转发有什么区别呢? 最重要的是他们寻找目的地址的关键字不一样。二层交换是链路层的任务,因此二层...
  • muaxi8
  • muaxi8
  • 2016年07月19日 09:56
  • 1325


一.包头结构体 //包头 struct PackageHeader { //包头大小(sizeof(PackageHeader)) unsigned int uTransPack...
  • caoshangpa
  • caoshangpa
  • 2016年09月27日 16:35
  • 3263


int   raw_sockfd_l2 = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));   struct sockaddr_ll sock_ip_pac...
  • wang_xya
  • wang_xya
  • 2014年03月31日 16:34
  • 782