1 .\" $Id: 1.t,v 1.2 2004/04/23 02:58:27 simonb Exp $
3 .\".ds RH 4.4BSD incompatibility with IPv6/IPsec packet processing
5 4.4BSD incompatibility with IPv6/IPsec packet processing
7 The 4.4BSD network code holds a packet in a chain of ``mbuf'' structures.
8 Each mbuf structure has three flavors:
10 non-cluster header mbuf, which holds MHLEN
11 (100 bytes in a 32bit architecture installation of 4.4BSD),
13 non-cluster data mbuf, which holds MLEN (104 bytes), and
15 cluster mbuf which holds MCLBYTES (2048 bytes).
17 We can make a chain of mbuf structures as a linked list.
18 Mbuf chains will efficiently hold variable-length packet data.
19 Such chains also enable us to insert or remove
20 some of the packet data from the chain
23 When processing inbound packets, 4.4BSD uses a function called
25 to ease the manipulation of data content in the mbufs.
26 It also uses a deep function call tree for inbound packet processing.
27 While these two items work just fine for traditional IPv4 processing,
28 they do not work as well with IPv6 and IPsec processing.
30 Restrictions in 4.4BSD m_pullup
32 For input packet processing,
33 the 4.4BSD network stack uses the
35 function to ease parsing efforts
36 by adjusting the data content in mbufs for placement onto the continuous memory
39 is defined as follows:
49 will ensure that the first
52 are placed in the continuous memory region.
55 the caller can safely access the first
57 bytes of the packet, assuming that they are continuous.
58 The caller can, for example, safely use pointer variables into
59 the continuous region, as long as they point inside the
66 box wid boxwid*1.2 "IPv6 header" "next = routing"
67 box same "routing header" "next = auth"
68 box same "auth header" "next = TCP"
70 box same "TCP payload"
74 Figure \n[figure]: IPv6 extension header chain
78 makes certain assumptions regarding protocol headers.
83 If the total packet header length is longer than MHLEN,
85 will fail, and the result will be a loss of the packet.
90 the length assumption worked fine in most cases,
91 since for almost every protocol, the total length of the protocol header part
93 Each packet has only two protocol headers, including the IPv4 header.
94 For example, the total length of the protocol header part of a TCP packet
95 (up to TCP data payload) is a maximum of 120 bytes.
96 Typically, this length is 40 to 48 bytes.
97 When an IPv4 option is present, it is stripped off before TCP
98 header processing, and the maximum length passed to
102 The IPv4 header occupies 20 bytes.
104 The IPv4 option occupies 40 bytes maximum.
105 It will be stripped off before we parse the TCP header.
106 Also note that the use of IPv4 options is very rare.
108 The TCP header length is 20 bytes.
110 The TCP option is 40 bytes maximum.
111 In most cases it is 0 to 8 bytes.
118 and IPsec specification
122 allow more flexible use of protocol headers
123 by introducing chained extension headers.
124 With chained extension headers, each header has a ``next header field'' in it.
125 A chain of headers can be made as shown
127 in Figure \n[figure].
129 The type of protocol header is determined by
130 inspecting the previous protocol header.
131 There is no restriction in the number of extension headers in the spec.
133 Because of extension header chains, there is now no upper limit in
134 protocol packet header length.
137 function would impose unnecessary restriction
138 to the extension header processing.
140 with the introduction of IPsec, it is now impossible to strip off extension headers
141 during inbound packet processing.
142 All of the data on the packet must be retained if it is to be authenticated
143 using Authentication Header.
147 Continuing the use of
150 number of extension headers allowed on the packet,
151 and could jeopadize the possible usefulness of IPv6 extension headers. \**
153 In IPv4 days, the IPv4 options turned out to be unusable
154 due to a lack of implementation.
155 This was because most commercial products simply did not support IPv4 options.
158 Another problem related to
160 is that it tends to copy the protocol header even
161 when it is unnecessary to do so.
162 For example, consider the mbuf chain shown
164 in Figure \n[figure]:
168 define pointer { box ht boxht*1/4 }
169 define payload { box }
172 IPd: payload with .n at bottom of IPp "IPv4"
177 TCPd: payload with .n at bottom of TCPp "TCP" "TCP payload"
179 arrow from IP.IPp.center to TCP.TCPp.center
183 .nr beforepullup \n[figure]
184 Figure \n[figure]: mbuf chain before \fIm_pullup\fP
186 Here, the first mbuf contains an IPv4 header in the continuous region,
187 and the second mbuf contains a TCP header in the continuous region.
188 When we look at the content of the TCP header,
189 under 4.4BSD the code will look like the following:
194 ip = mtod(m, struct ip *);
195 /* extra copy with m_pullup */
196 m = m_pullup(m, iphdrlen + tcphdrlen);
198 ip = mtod(m, struct ip *);
199 th = mtod(m, caddr_t) + iphdrlen;\fP
202 As a result, we will get a mbuf chain shown in
208 define pointer { box ht boxht*1/4 }
209 define payload { box }
212 IPd: payload with .n at bottom of IPp "IPv4" "TCP"
217 TCPd: payload with .n at bottom of TCPp "TCP payload"
219 arrow from IP.IPp.center to TCP.TCPp.center
223 Figure \n[figure]: mbuf chain in figure \n[beforepullup] after \fIm_pullup\fP
227 is only able to make a continuous
228 region starting from the top of the mbuf chain,
229 it copies the TCP portion in second mbuf
231 The copy could be avoided if
235 Also, the caller side is required to reinitialize all of
236 the pointers that point to the content of mbuf,
239 the first mbuf on the chain
243 ellipse "\fIip6_input\fP"
245 ellipse "\fIrthdr6_input\fP"
247 ellipse "\fIah_input\fP"
248 arrow "stack" "overflow"
249 ellipse "\fIesp_input\fP"
251 ellipse "\fItcp_input\fP"
254 Figure 5: an excessively deep call chain can cause kernel stack overflow
258 can be reallocated and lives at
259 a different address than before.
262 design has provided simplicity in packet parsing,
263 it is disadvantageous for protocols like IPv6.
265 The problems can be summarized as follows:
268 imposes too strong restriction
269 on the total length of the packet header (MHLEN);
272 makes an extra copy even when this can be avoided; and
275 requires the caller to reinitialize all of the pointers into the mbuf chain.
277 Protocol header processing with a deep function call chain
279 Under 4.4BSD, protocol header processing will make a chain of function calls.
280 For example, if we have an IPv4 TCP packet, the following function call chain will be made
282 (see Figure \n[figure]):
286 will be called from the network software interrupt logic,
289 processes the IPv4 header, then calls
294 .\"from its functionality.
297 will process the TCP header and pass the data payload
298 to the socket queues.
302 ellipse "\fIipintr\fP"
304 ellipse "\fItcp_input\fP"
308 Figure \n[figure]: function call chain in IPv4 inbound packet processing
311 If chained extension headers are handled as described above,
312 the kernel stack can overflow by a deep function call chain, as shown in
317 IPv6/IPsec specifications do not define any upper limit
318 to the number of extension headers on a packet,
319 so a malicious party can transmit a ``legal'' packet with a large number of chained
320 headers in order to attack IPv6/IPsec implementations.
321 We have experienced kernel stack overflow in IPsec code,
322 tunnelled packet processing code, and in several other cases.
323 The IPsec processing routines tend to use a large chunk of memory
324 on the kernel stack, in order to hold intermediate data and the secret keys
325 used for encryption. \**
327 For example, blowfish encryption processing code typically uses
328 an intermediate data region of 4K or more.
329 With typical 4.4BSD installation on i386 architecture,
330 the kernel stack region occupies less than 8K bytes and does not grow on demand.
332 We cannot put the intermediate data region into a static data region outside of
334 because it would become a source of performance drawback on multiprocessors
337 Even though the IPv6 specifications do not define any restrictions
338 on the number of extension headers, it may be possible
339 to impose additional restriction in an IPv6 implementation for safety.
340 In any case, it is not possible to estimate the amount of the
341 kernel stack, which will be used by protocol handlers.
342 We need a better calling convention for IPv6/IPsec header processing,
343 regardless of the limits in the number of extension headers we may impose.