Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 12: Networks


transport_header
MAC

IP

TCP

network_header
mac_header
tail
end
head
data

Figure 12-6: Link between socket buffer and
network packet data.

❑ mac_headerpoints to the start of the MAC header, whilenetwork_headerand
transport_headerpoint to the header data of the network and transport layer, respec-
tively. On systems with 32-bit word length, the data typesk_buff_data_tthat is used for the
various data components is a simple pointer:
<skbuff.h>
typedef unsigned char *sk_buff_data_t;
This enables the kernel to use socket buffers for all protocol types. Simple type conversions are
necessary to interpret the data correctly, and several auxiliary functions are provided for this
purpose. A socket buffer can, for example, contain a TCP or UDP packet. The corresponding
information from the transport header can be extracted withtcp_hdr, respectively,udp_hdr.
Both functions convert the raw pointer into an appropriate data type. Other transport layer pro-
tocols also provide helper functions of the typeXXX_hdrthat require a pointer tostruct sk_buff
and return the reinterpreted transport headerdata. Observe, for example, how a TCP header can
be obtained from a socket buffer:
<tcp.h>
static inline struct tcphdr *tcp_hdr(const struct sk_buff *skb)
{
return (struct tcphdr *)skb_transport_header(skb);
}
struct tcphdris a structure that collects all fields contained in a TCP header; the exact layout is
discussed in Section 12.9.2.
Similar conversion functions are also available for the network layer. For our purposes,ip_hdr
is most important: It is used to interpret the contents of an IP packet.

dataandtailenable data to be passed between protocol levels without requiring explicit copy opera-
tions, as shown in Figure 12-7, which demonstrates how packets are synthesized.


When a new packet is generated, the TCP layer first allocates memory in userspace to hold the packet
data (header and payroll). The space reserved is larger than needed for the data so that lower-level layers
can add further headers.


A socket buffer is allocated so thatheadandendpoint to the start and end of the space reserved in
memory, while the TCP data are located betweendataandtail.


A new layer must be added when the socket buffer is passed to the IP layer. This can simply be written
into the reserved space, and all pointers remain unchanged with the exception ofdata, which now points

Free download pdf