Protocol Handler Organization
Figure
13-7 shows how the different protocol handlers are organized in the kernel. Each
protocol is described by a packet_type data structure.

Figure 13-7. Data structure used to store the registered protocol handlers
To make access faster, a very simple hash function is used for most of the protocols.
Sixteen lists are organized into an array to which the global variable ptype_base points. When a protocol is registered, using the
dev_add_pack function, described in the next section,
this function runs a hash function over the protocol type and assigns the packet_type structure to one of the 16 lists. Later on, to
find a packet_type structure, the kernel can simply
rerun the hash and go through the matching list.
The ETH_P_ALL protocols (see Table 13-1) are organized in their own
list to which the global variable ptype_all
points.[*] The number of protocols in this list is stored in netdev_nit. The latter is used by dev_queue_xmit and qdisc_restart to check
whether a PF_PACKET socket is open (i.e., a listening
sniffer) to which it can deliver a copy of ingress frames (see Chapter 10).
[*] The figure shows that the ETH_P_ALL protocol
types use the packet_rcv routine. func is initialized by packet_create in net/packet/af_packet.c based on the kernel configuration.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access