Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * INET An implementation of the TCP/IP protocol suite for the LINUX | |
3 | * operating system. INET is implemented using the BSD Socket | |
4 | * interface as the means of communication with the user level. | |
5 | * | |
6 | * The IP fragmentation functionality. | |
e905a9ed | 7 | * |
1da177e4 | 8 | * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG> |
113aa838 | 9 | * Alan Cox <alan@lxorguk.ukuu.org.uk> |
1da177e4 LT |
10 | * |
11 | * Fixes: | |
12 | * Alan Cox : Split from ip.c , see ip_input.c for history. | |
13 | * David S. Miller : Begin massive cleanup... | |
14 | * Andi Kleen : Add sysctls. | |
15 | * xxxx : Overlapfrag bug. | |
16 | * Ultima : ip_expire() kernel panic. | |
17 | * Bill Hawes : Frag accounting and evictor fixes. | |
18 | * John McDonald : 0 length frag bug. | |
19 | * Alexey Kuznetsov: SMP races, threading, cleanup. | |
20 | * Patrick McHardy : LRU queue of frag heads for evictor. | |
21 | */ | |
22 | ||
89cee8b1 | 23 | #include <linux/compiler.h> |
1da177e4 LT |
24 | #include <linux/module.h> |
25 | #include <linux/types.h> | |
26 | #include <linux/mm.h> | |
27 | #include <linux/jiffies.h> | |
28 | #include <linux/skbuff.h> | |
29 | #include <linux/list.h> | |
30 | #include <linux/ip.h> | |
31 | #include <linux/icmp.h> | |
32 | #include <linux/netdevice.h> | |
33 | #include <linux/jhash.h> | |
34 | #include <linux/random.h> | |
5a0e3ad6 | 35 | #include <linux/slab.h> |
e9017b55 SW |
36 | #include <net/route.h> |
37 | #include <net/dst.h> | |
1da177e4 LT |
38 | #include <net/sock.h> |
39 | #include <net/ip.h> | |
40 | #include <net/icmp.h> | |
41 | #include <net/checksum.h> | |
89cee8b1 | 42 | #include <net/inetpeer.h> |
5ab11c98 | 43 | #include <net/inet_frag.h> |
1da177e4 LT |
44 | #include <linux/tcp.h> |
45 | #include <linux/udp.h> | |
46 | #include <linux/inet.h> | |
47 | #include <linux/netfilter_ipv4.h> | |
6623e3b2 | 48 | #include <net/inet_ecn.h> |
1da177e4 LT |
49 | |
50 | /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6 | |
51 | * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c | |
52 | * as well. Or notify me, at least. --ANK | |
53 | */ | |
54 | ||
8d8354d2 | 55 | static int sysctl_ipfrag_max_dist __read_mostly = 64; |
89cee8b1 | 56 | |
1da177e4 LT |
57 | struct ipfrag_skb_cb |
58 | { | |
59 | struct inet_skb_parm h; | |
60 | int offset; | |
61 | }; | |
62 | ||
fd3f8c4c | 63 | #define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb)) |
1da177e4 LT |
64 | |
65 | /* Describe an entry in the "incomplete datagrams" queue. */ | |
66 | struct ipq { | |
5ab11c98 PE |
67 | struct inet_frag_queue q; |
68 | ||
1da177e4 | 69 | u32 user; |
18277770 AV |
70 | __be32 saddr; |
71 | __be32 daddr; | |
72 | __be16 id; | |
1da177e4 | 73 | u8 protocol; |
6623e3b2 | 74 | u8 ecn; /* RFC3168 support */ |
89cee8b1 HX |
75 | int iif; |
76 | unsigned int rid; | |
77 | struct inet_peer *peer; | |
1da177e4 LT |
78 | }; |
79 | ||
5173cc05 ED |
80 | /* RFC 3168 support : |
81 | * We want to check ECN values of all fragments, do detect invalid combinations. | |
82 | * In ipq->ecn, we store the OR value of each ip4_frag_ecn() fragment value. | |
83 | */ | |
1d1652cb DM |
84 | #define IPFRAG_ECN_NOT_ECT 0x01 /* one frag had ECN_NOT_ECT */ |
85 | #define IPFRAG_ECN_ECT_1 0x02 /* one frag had ECN_ECT_1 */ | |
86 | #define IPFRAG_ECN_ECT_0 0x04 /* one frag had ECN_ECT_0 */ | |
87 | #define IPFRAG_ECN_CE 0x08 /* one frag had ECN_CE */ | |
6623e3b2 ED |
88 | |
89 | static inline u8 ip4_frag_ecn(u8 tos) | |
90 | { | |
5173cc05 | 91 | return 1 << (tos & INET_ECN_MASK); |
6623e3b2 ED |
92 | } |
93 | ||
5173cc05 ED |
94 | /* Given the OR values of all fragments, apply RFC 3168 5.3 requirements |
95 | * Value : 0xff if frame should be dropped. | |
96 | * 0 or INET_ECN_CE value, to be ORed in to final iph->tos field | |
97 | */ | |
98 | static const u8 ip4_frag_ecn_table[16] = { | |
99 | /* at least one fragment had CE, and others ECT_0 or ECT_1 */ | |
100 | [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0] = INET_ECN_CE, | |
101 | [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_1] = INET_ECN_CE, | |
102 | [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = INET_ECN_CE, | |
103 | ||
104 | /* invalid combinations : drop frame */ | |
105 | [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE] = 0xff, | |
106 | [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_0] = 0xff, | |
107 | [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_1] = 0xff, | |
108 | [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = 0xff, | |
109 | [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0] = 0xff, | |
110 | [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_1] = 0xff, | |
111 | [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = 0xff, | |
112 | }; | |
113 | ||
7eb95156 | 114 | static struct inet_frags ip4_frags; |
1da177e4 | 115 | |
e5a2bb84 | 116 | int ip_frag_nqueues(struct net *net) |
7eb95156 | 117 | { |
e5a2bb84 | 118 | return net->ipv4.frags.nqueues; |
7eb95156 | 119 | } |
1da177e4 | 120 | |
6ddc0822 | 121 | int ip_frag_mem(struct net *net) |
7eb95156 | 122 | { |
6ddc0822 | 123 | return atomic_read(&net->ipv4.frags.mem); |
7eb95156 | 124 | } |
1da177e4 | 125 | |
1706d587 HX |
126 | static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev, |
127 | struct net_device *dev); | |
128 | ||
c6fda282 PE |
129 | struct ip4_create_arg { |
130 | struct iphdr *iph; | |
131 | u32 user; | |
132 | }; | |
133 | ||
18277770 | 134 | static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot) |
1da177e4 | 135 | { |
18277770 AV |
136 | return jhash_3words((__force u32)id << 16 | prot, |
137 | (__force u32)saddr, (__force u32)daddr, | |
7eb95156 | 138 | ip4_frags.rnd) & (INETFRAGS_HASHSZ - 1); |
1da177e4 LT |
139 | } |
140 | ||
321a3a99 | 141 | static unsigned int ip4_hashfn(struct inet_frag_queue *q) |
1da177e4 | 142 | { |
321a3a99 | 143 | struct ipq *ipq; |
1da177e4 | 144 | |
321a3a99 PE |
145 | ipq = container_of(q, struct ipq, q); |
146 | return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol); | |
1da177e4 LT |
147 | } |
148 | ||
abd6523d PE |
149 | static int ip4_frag_match(struct inet_frag_queue *q, void *a) |
150 | { | |
151 | struct ipq *qp; | |
152 | struct ip4_create_arg *arg = a; | |
153 | ||
154 | qp = container_of(q, struct ipq, q); | |
a02cec21 | 155 | return qp->id == arg->iph->id && |
abd6523d PE |
156 | qp->saddr == arg->iph->saddr && |
157 | qp->daddr == arg->iph->daddr && | |
158 | qp->protocol == arg->iph->protocol && | |
a02cec21 | 159 | qp->user == arg->user; |
abd6523d PE |
160 | } |
161 | ||
1da177e4 | 162 | /* Memory Tracking Functions. */ |
a95d8c88 | 163 | static void frag_kfree_skb(struct netns_frags *nf, struct sk_buff *skb) |
1da177e4 | 164 | { |
6ddc0822 | 165 | atomic_sub(skb->truesize, &nf->mem); |
1da177e4 LT |
166 | kfree_skb(skb); |
167 | } | |
168 | ||
c6fda282 PE |
169 | static void ip4_frag_init(struct inet_frag_queue *q, void *a) |
170 | { | |
171 | struct ipq *qp = container_of(q, struct ipq, q); | |
172 | struct ip4_create_arg *arg = a; | |
173 | ||
174 | qp->protocol = arg->iph->protocol; | |
175 | qp->id = arg->iph->id; | |
6623e3b2 | 176 | qp->ecn = ip4_frag_ecn(arg->iph->tos); |
c6fda282 PE |
177 | qp->saddr = arg->iph->saddr; |
178 | qp->daddr = arg->iph->daddr; | |
179 | qp->user = arg->user; | |
180 | qp->peer = sysctl_ipfrag_max_dist ? | |
b534ecf1 | 181 | inet_getpeer_v4(arg->iph->saddr, 1) : NULL; |
c6fda282 PE |
182 | } |
183 | ||
1e4b8287 | 184 | static __inline__ void ip4_frag_free(struct inet_frag_queue *q) |
1da177e4 | 185 | { |
1e4b8287 PE |
186 | struct ipq *qp; |
187 | ||
188 | qp = container_of(q, struct ipq, q); | |
189 | if (qp->peer) | |
190 | inet_putpeer(qp->peer); | |
1da177e4 LT |
191 | } |
192 | ||
1da177e4 LT |
193 | |
194 | /* Destruction primitives. */ | |
195 | ||
4b6cb5d8 | 196 | static __inline__ void ipq_put(struct ipq *ipq) |
1da177e4 | 197 | { |
762cc408 | 198 | inet_frag_put(&ipq->q, &ip4_frags); |
1da177e4 LT |
199 | } |
200 | ||
201 | /* Kill ipq entry. It is not destroyed immediately, | |
202 | * because caller (and someone more) holds reference count. | |
203 | */ | |
204 | static void ipq_kill(struct ipq *ipq) | |
205 | { | |
277e650d | 206 | inet_frag_kill(&ipq->q, &ip4_frags); |
1da177e4 LT |
207 | } |
208 | ||
e905a9ed | 209 | /* Memory limiting on fragments. Evictor trashes the oldest |
1da177e4 LT |
210 | * fragment queue until we are back under the threshold. |
211 | */ | |
6ddc0822 | 212 | static void ip_evictor(struct net *net) |
1da177e4 | 213 | { |
8e7999c4 PE |
214 | int evicted; |
215 | ||
6ddc0822 | 216 | evicted = inet_frag_evictor(&net->ipv4.frags, &ip4_frags); |
8e7999c4 | 217 | if (evicted) |
c5346fe3 | 218 | IP_ADD_STATS_BH(net, IPSTATS_MIB_REASMFAILS, evicted); |
1da177e4 LT |
219 | } |
220 | ||
221 | /* | |
222 | * Oops, a fragment queue timed out. Kill it and send an ICMP reply. | |
223 | */ | |
224 | static void ip_expire(unsigned long arg) | |
225 | { | |
e521db9d | 226 | struct ipq *qp; |
84a3aa00 | 227 | struct net *net; |
e521db9d PE |
228 | |
229 | qp = container_of((struct inet_frag_queue *) arg, struct ipq, q); | |
84a3aa00 | 230 | net = container_of(qp->q.net, struct net, ipv4.frags); |
1da177e4 | 231 | |
5ab11c98 | 232 | spin_lock(&qp->q.lock); |
1da177e4 | 233 | |
bc578a54 | 234 | if (qp->q.last_in & INET_FRAG_COMPLETE) |
1da177e4 LT |
235 | goto out; |
236 | ||
237 | ipq_kill(qp); | |
238 | ||
7c73a6fa PE |
239 | IP_INC_STATS_BH(net, IPSTATS_MIB_REASMTIMEOUT); |
240 | IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS); | |
1da177e4 | 241 | |
bc578a54 | 242 | if ((qp->q.last_in & INET_FRAG_FIRST_IN) && qp->q.fragments != NULL) { |
5ab11c98 | 243 | struct sk_buff *head = qp->q.fragments; |
64f3b9e2 ED |
244 | const struct iphdr *iph; |
245 | int err; | |
cb84663e | 246 | |
69df9d59 ED |
247 | rcu_read_lock(); |
248 | head->dev = dev_get_by_index_rcu(net, qp->iif); | |
e9017b55 SW |
249 | if (!head->dev) |
250 | goto out_rcu_unlock; | |
251 | ||
64f3b9e2 ED |
252 | /* skb dst is stale, drop it, and perform route lookup again */ |
253 | skb_dst_drop(head); | |
254 | iph = ip_hdr(head); | |
255 | err = ip_route_input_noref(head, iph->daddr, iph->saddr, | |
256 | iph->tos, head->dev); | |
257 | if (err) | |
258 | goto out_rcu_unlock; | |
259 | ||
e9017b55 | 260 | /* |
64f3b9e2 ED |
261 | * Only an end host needs to send an ICMP |
262 | * "Fragment Reassembly Timeout" message, per RFC792. | |
e9017b55 | 263 | */ |
64f3b9e2 ED |
264 | if (qp->user == IP_DEFRAG_CONNTRACK_IN && |
265 | skb_rtable(head)->rt_type != RTN_LOCAL) | |
266 | goto out_rcu_unlock; | |
267 | ||
e9017b55 SW |
268 | |
269 | /* Send an ICMP "Fragment Reassembly Timeout" message. */ | |
270 | icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0); | |
e9017b55 | 271 | out_rcu_unlock: |
d1c9ae6d PM |
272 | rcu_read_unlock(); |
273 | } | |
1da177e4 | 274 | out: |
5ab11c98 | 275 | spin_unlock(&qp->q.lock); |
4b6cb5d8 | 276 | ipq_put(qp); |
1da177e4 LT |
277 | } |
278 | ||
abd6523d PE |
279 | /* Find the correct entry in the "incomplete datagrams" queue for |
280 | * this IP datagram, and create new one, if nothing is found. | |
281 | */ | |
ac18e750 | 282 | static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user) |
1da177e4 | 283 | { |
c6fda282 PE |
284 | struct inet_frag_queue *q; |
285 | struct ip4_create_arg arg; | |
abd6523d | 286 | unsigned int hash; |
1da177e4 | 287 | |
c6fda282 PE |
288 | arg.iph = iph; |
289 | arg.user = user; | |
9a375803 PE |
290 | |
291 | read_lock(&ip4_frags.lock); | |
abd6523d | 292 | hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol); |
1da177e4 | 293 | |
ac18e750 | 294 | q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash); |
c6fda282 PE |
295 | if (q == NULL) |
296 | goto out_nomem; | |
1da177e4 | 297 | |
c6fda282 | 298 | return container_of(q, struct ipq, q); |
1da177e4 LT |
299 | |
300 | out_nomem: | |
64ce2073 | 301 | LIMIT_NETDEBUG(KERN_ERR "ip_frag_create: no memory left !\n"); |
1da177e4 LT |
302 | return NULL; |
303 | } | |
304 | ||
89cee8b1 HX |
305 | /* Is the fragment too far ahead to be part of ipq? */ |
306 | static inline int ip_frag_too_far(struct ipq *qp) | |
307 | { | |
308 | struct inet_peer *peer = qp->peer; | |
309 | unsigned int max = sysctl_ipfrag_max_dist; | |
310 | unsigned int start, end; | |
311 | ||
312 | int rc; | |
313 | ||
314 | if (!peer || !max) | |
315 | return 0; | |
316 | ||
317 | start = qp->rid; | |
318 | end = atomic_inc_return(&peer->rid); | |
319 | qp->rid = end; | |
320 | ||
5ab11c98 | 321 | rc = qp->q.fragments && (end - start) > max; |
89cee8b1 HX |
322 | |
323 | if (rc) { | |
7c73a6fa PE |
324 | struct net *net; |
325 | ||
326 | net = container_of(qp->q.net, struct net, ipv4.frags); | |
327 | IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS); | |
89cee8b1 HX |
328 | } |
329 | ||
330 | return rc; | |
331 | } | |
332 | ||
333 | static int ip_frag_reinit(struct ipq *qp) | |
334 | { | |
335 | struct sk_buff *fp; | |
336 | ||
b2fd5321 | 337 | if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) { |
5ab11c98 | 338 | atomic_inc(&qp->q.refcnt); |
89cee8b1 HX |
339 | return -ETIMEDOUT; |
340 | } | |
341 | ||
5ab11c98 | 342 | fp = qp->q.fragments; |
89cee8b1 HX |
343 | do { |
344 | struct sk_buff *xp = fp->next; | |
a95d8c88 | 345 | frag_kfree_skb(qp->q.net, fp); |
89cee8b1 HX |
346 | fp = xp; |
347 | } while (fp); | |
348 | ||
5ab11c98 PE |
349 | qp->q.last_in = 0; |
350 | qp->q.len = 0; | |
351 | qp->q.meat = 0; | |
352 | qp->q.fragments = NULL; | |
d6bebca9 | 353 | qp->q.fragments_tail = NULL; |
89cee8b1 | 354 | qp->iif = 0; |
6623e3b2 | 355 | qp->ecn = 0; |
89cee8b1 HX |
356 | |
357 | return 0; | |
358 | } | |
359 | ||
1da177e4 | 360 | /* Add new segment to existing queue. */ |
1706d587 | 361 | static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb) |
1da177e4 LT |
362 | { |
363 | struct sk_buff *prev, *next; | |
1706d587 | 364 | struct net_device *dev; |
1da177e4 LT |
365 | int flags, offset; |
366 | int ihl, end; | |
1706d587 | 367 | int err = -ENOENT; |
6623e3b2 | 368 | u8 ecn; |
1da177e4 | 369 | |
bc578a54 | 370 | if (qp->q.last_in & INET_FRAG_COMPLETE) |
1da177e4 LT |
371 | goto err; |
372 | ||
89cee8b1 | 373 | if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) && |
1706d587 HX |
374 | unlikely(ip_frag_too_far(qp)) && |
375 | unlikely(err = ip_frag_reinit(qp))) { | |
89cee8b1 HX |
376 | ipq_kill(qp); |
377 | goto err; | |
378 | } | |
379 | ||
6623e3b2 | 380 | ecn = ip4_frag_ecn(ip_hdr(skb)->tos); |
eddc9ec5 | 381 | offset = ntohs(ip_hdr(skb)->frag_off); |
1da177e4 LT |
382 | flags = offset & ~IP_OFFSET; |
383 | offset &= IP_OFFSET; | |
384 | offset <<= 3; /* offset is in 8-byte chunks */ | |
c9bdd4b5 | 385 | ihl = ip_hdrlen(skb); |
1da177e4 LT |
386 | |
387 | /* Determine the position of this fragment. */ | |
e905a9ed | 388 | end = offset + skb->len - ihl; |
1706d587 | 389 | err = -EINVAL; |
1da177e4 LT |
390 | |
391 | /* Is this the final fragment? */ | |
392 | if ((flags & IP_MF) == 0) { | |
393 | /* If we already have some bits beyond end | |
394 | * or have different end, the segment is corrrupted. | |
395 | */ | |
5ab11c98 | 396 | if (end < qp->q.len || |
bc578a54 | 397 | ((qp->q.last_in & INET_FRAG_LAST_IN) && end != qp->q.len)) |
1da177e4 | 398 | goto err; |
bc578a54 | 399 | qp->q.last_in |= INET_FRAG_LAST_IN; |
5ab11c98 | 400 | qp->q.len = end; |
1da177e4 LT |
401 | } else { |
402 | if (end&7) { | |
403 | end &= ~7; | |
404 | if (skb->ip_summed != CHECKSUM_UNNECESSARY) | |
405 | skb->ip_summed = CHECKSUM_NONE; | |
406 | } | |
5ab11c98 | 407 | if (end > qp->q.len) { |
1da177e4 | 408 | /* Some bits beyond end -> corruption. */ |
bc578a54 | 409 | if (qp->q.last_in & INET_FRAG_LAST_IN) |
1da177e4 | 410 | goto err; |
5ab11c98 | 411 | qp->q.len = end; |
1da177e4 LT |
412 | } |
413 | } | |
414 | if (end == offset) | |
415 | goto err; | |
416 | ||
1706d587 | 417 | err = -ENOMEM; |
1da177e4 LT |
418 | if (pskb_pull(skb, ihl) == NULL) |
419 | goto err; | |
1706d587 HX |
420 | |
421 | err = pskb_trim_rcsum(skb, end - offset); | |
422 | if (err) | |
1da177e4 LT |
423 | goto err; |
424 | ||
425 | /* Find out which fragments are in front and at the back of us | |
426 | * in the chain of fragments so far. We must know where to put | |
427 | * this fragment, right? | |
428 | */ | |
d6bebca9 CG |
429 | prev = qp->q.fragments_tail; |
430 | if (!prev || FRAG_CB(prev)->offset < offset) { | |
431 | next = NULL; | |
432 | goto found; | |
433 | } | |
1da177e4 | 434 | prev = NULL; |
5ab11c98 | 435 | for (next = qp->q.fragments; next != NULL; next = next->next) { |
1da177e4 LT |
436 | if (FRAG_CB(next)->offset >= offset) |
437 | break; /* bingo! */ | |
438 | prev = next; | |
439 | } | |
440 | ||
d6bebca9 | 441 | found: |
1da177e4 LT |
442 | /* We found where to put this one. Check for overlap with |
443 | * preceding fragment, and, if needed, align things so that | |
444 | * any overlaps are eliminated. | |
445 | */ | |
446 | if (prev) { | |
447 | int i = (FRAG_CB(prev)->offset + prev->len) - offset; | |
448 | ||
449 | if (i > 0) { | |
450 | offset += i; | |
1706d587 | 451 | err = -EINVAL; |
1da177e4 LT |
452 | if (end <= offset) |
453 | goto err; | |
1706d587 | 454 | err = -ENOMEM; |
1da177e4 LT |
455 | if (!pskb_pull(skb, i)) |
456 | goto err; | |
457 | if (skb->ip_summed != CHECKSUM_UNNECESSARY) | |
458 | skb->ip_summed = CHECKSUM_NONE; | |
459 | } | |
460 | } | |
461 | ||
1706d587 HX |
462 | err = -ENOMEM; |
463 | ||
1da177e4 LT |
464 | while (next && FRAG_CB(next)->offset < end) { |
465 | int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */ | |
466 | ||
467 | if (i < next->len) { | |
468 | /* Eat head of the next overlapped fragment | |
469 | * and leave the loop. The next ones cannot overlap. | |
470 | */ | |
471 | if (!pskb_pull(next, i)) | |
472 | goto err; | |
473 | FRAG_CB(next)->offset += i; | |
5ab11c98 | 474 | qp->q.meat -= i; |
1da177e4 LT |
475 | if (next->ip_summed != CHECKSUM_UNNECESSARY) |
476 | next->ip_summed = CHECKSUM_NONE; | |
477 | break; | |
478 | } else { | |
479 | struct sk_buff *free_it = next; | |
480 | ||
47c6bf77 | 481 | /* Old fragment is completely overridden with |
1da177e4 LT |
482 | * new one drop it. |
483 | */ | |
484 | next = next->next; | |
485 | ||
486 | if (prev) | |
487 | prev->next = next; | |
488 | else | |
5ab11c98 | 489 | qp->q.fragments = next; |
1da177e4 | 490 | |
5ab11c98 | 491 | qp->q.meat -= free_it->len; |
a95d8c88 | 492 | frag_kfree_skb(qp->q.net, free_it); |
1da177e4 LT |
493 | } |
494 | } | |
495 | ||
496 | FRAG_CB(skb)->offset = offset; | |
497 | ||
498 | /* Insert this fragment in the chain of fragments. */ | |
499 | skb->next = next; | |
d6bebca9 CG |
500 | if (!next) |
501 | qp->q.fragments_tail = skb; | |
1da177e4 LT |
502 | if (prev) |
503 | prev->next = skb; | |
504 | else | |
5ab11c98 | 505 | qp->q.fragments = skb; |
1da177e4 | 506 | |
1706d587 HX |
507 | dev = skb->dev; |
508 | if (dev) { | |
509 | qp->iif = dev->ifindex; | |
510 | skb->dev = NULL; | |
511 | } | |
5ab11c98 PE |
512 | qp->q.stamp = skb->tstamp; |
513 | qp->q.meat += skb->len; | |
6623e3b2 | 514 | qp->ecn |= ecn; |
6ddc0822 | 515 | atomic_add(skb->truesize, &qp->q.net->mem); |
1da177e4 | 516 | if (offset == 0) |
bc578a54 | 517 | qp->q.last_in |= INET_FRAG_FIRST_IN; |
1da177e4 | 518 | |
bc578a54 JP |
519 | if (qp->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && |
520 | qp->q.meat == qp->q.len) | |
1706d587 HX |
521 | return ip_frag_reasm(qp, prev, dev); |
522 | ||
7eb95156 | 523 | write_lock(&ip4_frags.lock); |
3140c25c | 524 | list_move_tail(&qp->q.lru_list, &qp->q.net->lru_list); |
7eb95156 | 525 | write_unlock(&ip4_frags.lock); |
1706d587 | 526 | return -EINPROGRESS; |
1da177e4 LT |
527 | |
528 | err: | |
529 | kfree_skb(skb); | |
1706d587 | 530 | return err; |
1da177e4 LT |
531 | } |
532 | ||
533 | ||
534 | /* Build a new IP datagram from all its fragments. */ | |
535 | ||
1706d587 HX |
536 | static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev, |
537 | struct net_device *dev) | |
1da177e4 | 538 | { |
2bad35b7 | 539 | struct net *net = container_of(qp->q.net, struct net, ipv4.frags); |
1da177e4 | 540 | struct iphdr *iph; |
5ab11c98 | 541 | struct sk_buff *fp, *head = qp->q.fragments; |
1da177e4 LT |
542 | int len; |
543 | int ihlen; | |
1706d587 | 544 | int err; |
5173cc05 | 545 | u8 ecn; |
1da177e4 LT |
546 | |
547 | ipq_kill(qp); | |
548 | ||
5173cc05 ED |
549 | ecn = ip4_frag_ecn_table[qp->ecn]; |
550 | if (unlikely(ecn == 0xff)) { | |
551 | err = -EINVAL; | |
552 | goto out_fail; | |
553 | } | |
1706d587 HX |
554 | /* Make the one we just received the head. */ |
555 | if (prev) { | |
556 | head = prev->next; | |
557 | fp = skb_clone(head, GFP_ATOMIC); | |
1706d587 HX |
558 | if (!fp) |
559 | goto out_nomem; | |
560 | ||
561 | fp->next = head->next; | |
d6bebca9 CG |
562 | if (!fp->next) |
563 | qp->q.fragments_tail = fp; | |
1706d587 HX |
564 | prev->next = fp; |
565 | ||
5ab11c98 PE |
566 | skb_morph(head, qp->q.fragments); |
567 | head->next = qp->q.fragments->next; | |
1706d587 | 568 | |
5ab11c98 PE |
569 | kfree_skb(qp->q.fragments); |
570 | qp->q.fragments = head; | |
1706d587 HX |
571 | } |
572 | ||
547b792c IJ |
573 | WARN_ON(head == NULL); |
574 | WARN_ON(FRAG_CB(head)->offset != 0); | |
1da177e4 LT |
575 | |
576 | /* Allocate a new buffer for the datagram. */ | |
c9bdd4b5 | 577 | ihlen = ip_hdrlen(head); |
5ab11c98 | 578 | len = ihlen + qp->q.len; |
1da177e4 | 579 | |
1706d587 | 580 | err = -E2BIG; |
132adf54 | 581 | if (len > 65535) |
1da177e4 LT |
582 | goto out_oversize; |
583 | ||
584 | /* Head of list must not be cloned. */ | |
585 | if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) | |
586 | goto out_nomem; | |
587 | ||
588 | /* If the first fragment is fragmented itself, we split | |
589 | * it to two chunks: the first with data and paged part | |
590 | * and the second, holding only fragments. */ | |
21dc3301 | 591 | if (skb_has_frag_list(head)) { |
1da177e4 LT |
592 | struct sk_buff *clone; |
593 | int i, plen = 0; | |
594 | ||
595 | if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) | |
596 | goto out_nomem; | |
597 | clone->next = head->next; | |
598 | head->next = clone; | |
599 | skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; | |
d7fcf1a5 | 600 | skb_frag_list_init(head); |
1da177e4 LT |
601 | for (i=0; i<skb_shinfo(head)->nr_frags; i++) |
602 | plen += skb_shinfo(head)->frags[i].size; | |
603 | clone->len = clone->data_len = head->data_len - plen; | |
604 | head->data_len -= clone->len; | |
605 | head->len -= clone->len; | |
606 | clone->csum = 0; | |
607 | clone->ip_summed = head->ip_summed; | |
6ddc0822 | 608 | atomic_add(clone->truesize, &qp->q.net->mem); |
1da177e4 LT |
609 | } |
610 | ||
611 | skb_shinfo(head)->frag_list = head->next; | |
d56f90a7 | 612 | skb_push(head, head->data - skb_network_header(head)); |
1da177e4 LT |
613 | |
614 | for (fp=head->next; fp; fp = fp->next) { | |
615 | head->data_len += fp->len; | |
616 | head->len += fp->len; | |
617 | if (head->ip_summed != fp->ip_summed) | |
618 | head->ip_summed = CHECKSUM_NONE; | |
84fa7933 | 619 | else if (head->ip_summed == CHECKSUM_COMPLETE) |
1da177e4 LT |
620 | head->csum = csum_add(head->csum, fp->csum); |
621 | head->truesize += fp->truesize; | |
1da177e4 | 622 | } |
d27f9b35 | 623 | atomic_sub(head->truesize, &qp->q.net->mem); |
1da177e4 LT |
624 | |
625 | head->next = NULL; | |
626 | head->dev = dev; | |
5ab11c98 | 627 | head->tstamp = qp->q.stamp; |
1da177e4 | 628 | |
eddc9ec5 | 629 | iph = ip_hdr(head); |
1da177e4 LT |
630 | iph->frag_off = 0; |
631 | iph->tot_len = htons(len); | |
5173cc05 | 632 | iph->tos |= ecn; |
2bad35b7 | 633 | IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS); |
5ab11c98 | 634 | qp->q.fragments = NULL; |
d6bebca9 | 635 | qp->q.fragments_tail = NULL; |
1706d587 | 636 | return 0; |
1da177e4 LT |
637 | |
638 | out_nomem: | |
e905a9ed | 639 | LIMIT_NETDEBUG(KERN_ERR "IP: queue_glue: no memory for gluing " |
64ce2073 | 640 | "queue %p\n", qp); |
45542479 | 641 | err = -ENOMEM; |
1da177e4 LT |
642 | goto out_fail; |
643 | out_oversize: | |
644 | if (net_ratelimit()) | |
673d57e7 HH |
645 | printk(KERN_INFO "Oversized IP packet from %pI4.\n", |
646 | &qp->saddr); | |
1da177e4 | 647 | out_fail: |
bbf31bf1 | 648 | IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS); |
1706d587 | 649 | return err; |
1da177e4 LT |
650 | } |
651 | ||
652 | /* Process an incoming IP datagram fragment. */ | |
776c729e | 653 | int ip_defrag(struct sk_buff *skb, u32 user) |
1da177e4 | 654 | { |
1da177e4 | 655 | struct ipq *qp; |
ac18e750 | 656 | struct net *net; |
e905a9ed | 657 | |
adf30907 | 658 | net = skb->dev ? dev_net(skb->dev) : dev_net(skb_dst(skb)->dev); |
7c73a6fa | 659 | IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS); |
1da177e4 LT |
660 | |
661 | /* Start by cleaning up the memory. */ | |
e31e0bdc | 662 | if (atomic_read(&net->ipv4.frags.mem) > net->ipv4.frags.high_thresh) |
6ddc0822 | 663 | ip_evictor(net); |
1da177e4 | 664 | |
1da177e4 | 665 | /* Lookup (or create) queue header */ |
ac18e750 | 666 | if ((qp = ip_find(net, ip_hdr(skb), user)) != NULL) { |
1706d587 | 667 | int ret; |
1da177e4 | 668 | |
5ab11c98 | 669 | spin_lock(&qp->q.lock); |
1da177e4 | 670 | |
1706d587 | 671 | ret = ip_frag_queue(qp, skb); |
1da177e4 | 672 | |
5ab11c98 | 673 | spin_unlock(&qp->q.lock); |
4b6cb5d8 | 674 | ipq_put(qp); |
776c729e | 675 | return ret; |
1da177e4 LT |
676 | } |
677 | ||
7c73a6fa | 678 | IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS); |
1da177e4 | 679 | kfree_skb(skb); |
776c729e | 680 | return -ENOMEM; |
1da177e4 | 681 | } |
4bc2f18b | 682 | EXPORT_SYMBOL(ip_defrag); |
1da177e4 | 683 | |
8d8354d2 PE |
684 | #ifdef CONFIG_SYSCTL |
685 | static int zero; | |
686 | ||
0a64b4b8 | 687 | static struct ctl_table ip4_frags_ns_ctl_table[] = { |
8d8354d2 | 688 | { |
8d8354d2 | 689 | .procname = "ipfrag_high_thresh", |
e31e0bdc | 690 | .data = &init_net.ipv4.frags.high_thresh, |
8d8354d2 PE |
691 | .maxlen = sizeof(int), |
692 | .mode = 0644, | |
6d9f239a | 693 | .proc_handler = proc_dointvec |
8d8354d2 PE |
694 | }, |
695 | { | |
8d8354d2 | 696 | .procname = "ipfrag_low_thresh", |
e31e0bdc | 697 | .data = &init_net.ipv4.frags.low_thresh, |
8d8354d2 PE |
698 | .maxlen = sizeof(int), |
699 | .mode = 0644, | |
6d9f239a | 700 | .proc_handler = proc_dointvec |
8d8354d2 PE |
701 | }, |
702 | { | |
8d8354d2 | 703 | .procname = "ipfrag_time", |
b2fd5321 | 704 | .data = &init_net.ipv4.frags.timeout, |
8d8354d2 PE |
705 | .maxlen = sizeof(int), |
706 | .mode = 0644, | |
6d9f239a | 707 | .proc_handler = proc_dointvec_jiffies, |
8d8354d2 | 708 | }, |
7d291ebb PE |
709 | { } |
710 | }; | |
711 | ||
712 | static struct ctl_table ip4_frags_ctl_table[] = { | |
8d8354d2 | 713 | { |
8d8354d2 | 714 | .procname = "ipfrag_secret_interval", |
3b4bc4a2 | 715 | .data = &ip4_frags.secret_interval, |
8d8354d2 PE |
716 | .maxlen = sizeof(int), |
717 | .mode = 0644, | |
6d9f239a | 718 | .proc_handler = proc_dointvec_jiffies, |
8d8354d2 PE |
719 | }, |
720 | { | |
721 | .procname = "ipfrag_max_dist", | |
722 | .data = &sysctl_ipfrag_max_dist, | |
723 | .maxlen = sizeof(int), | |
724 | .mode = 0644, | |
6d9f239a | 725 | .proc_handler = proc_dointvec_minmax, |
8d8354d2 PE |
726 | .extra1 = &zero |
727 | }, | |
728 | { } | |
729 | }; | |
730 | ||
2c8c1e72 | 731 | static int __net_init ip4_frags_ns_ctl_register(struct net *net) |
8d8354d2 | 732 | { |
e4a2d5c2 | 733 | struct ctl_table *table; |
8d8354d2 PE |
734 | struct ctl_table_header *hdr; |
735 | ||
0a64b4b8 | 736 | table = ip4_frags_ns_ctl_table; |
09ad9bc7 | 737 | if (!net_eq(net, &init_net)) { |
0a64b4b8 | 738 | table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL); |
e4a2d5c2 PE |
739 | if (table == NULL) |
740 | goto err_alloc; | |
741 | ||
e31e0bdc PE |
742 | table[0].data = &net->ipv4.frags.high_thresh; |
743 | table[1].data = &net->ipv4.frags.low_thresh; | |
b2fd5321 | 744 | table[2].data = &net->ipv4.frags.timeout; |
e4a2d5c2 PE |
745 | } |
746 | ||
747 | hdr = register_net_sysctl_table(net, net_ipv4_ctl_path, table); | |
748 | if (hdr == NULL) | |
749 | goto err_reg; | |
750 | ||
751 | net->ipv4.frags_hdr = hdr; | |
752 | return 0; | |
753 | ||
754 | err_reg: | |
09ad9bc7 | 755 | if (!net_eq(net, &init_net)) |
e4a2d5c2 PE |
756 | kfree(table); |
757 | err_alloc: | |
758 | return -ENOMEM; | |
759 | } | |
760 | ||
2c8c1e72 | 761 | static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net) |
e4a2d5c2 PE |
762 | { |
763 | struct ctl_table *table; | |
764 | ||
765 | table = net->ipv4.frags_hdr->ctl_table_arg; | |
766 | unregister_net_sysctl_table(net->ipv4.frags_hdr); | |
767 | kfree(table); | |
8d8354d2 | 768 | } |
7d291ebb PE |
769 | |
770 | static void ip4_frags_ctl_register(void) | |
771 | { | |
772 | register_net_sysctl_rotable(net_ipv4_ctl_path, ip4_frags_ctl_table); | |
773 | } | |
8d8354d2 | 774 | #else |
0a64b4b8 | 775 | static inline int ip4_frags_ns_ctl_register(struct net *net) |
8d8354d2 PE |
776 | { |
777 | return 0; | |
778 | } | |
e4a2d5c2 | 779 | |
0a64b4b8 | 780 | static inline void ip4_frags_ns_ctl_unregister(struct net *net) |
e4a2d5c2 PE |
781 | { |
782 | } | |
7d291ebb PE |
783 | |
784 | static inline void ip4_frags_ctl_register(void) | |
785 | { | |
786 | } | |
8d8354d2 PE |
787 | #endif |
788 | ||
2c8c1e72 | 789 | static int __net_init ipv4_frags_init_net(struct net *net) |
8d8354d2 | 790 | { |
e31e0bdc PE |
791 | /* |
792 | * Fragment cache limits. We will commit 256K at one time. Should we | |
793 | * cross that limit we will prune down to 192K. This should cope with | |
794 | * even the most extreme cases without allowing an attacker to | |
795 | * measurably harm machine performance. | |
796 | */ | |
797 | net->ipv4.frags.high_thresh = 256 * 1024; | |
798 | net->ipv4.frags.low_thresh = 192 * 1024; | |
b2fd5321 PE |
799 | /* |
800 | * Important NOTE! Fragment queue must be destroyed before MSL expires. | |
801 | * RFC791 is wrong proposing to prolongate timer each fragment arrival | |
802 | * by TTL. | |
803 | */ | |
804 | net->ipv4.frags.timeout = IP_FRAG_TIME; | |
805 | ||
e5a2bb84 PE |
806 | inet_frags_init_net(&net->ipv4.frags); |
807 | ||
0a64b4b8 | 808 | return ip4_frags_ns_ctl_register(net); |
8d8354d2 PE |
809 | } |
810 | ||
2c8c1e72 | 811 | static void __net_exit ipv4_frags_exit_net(struct net *net) |
81566e83 | 812 | { |
0a64b4b8 | 813 | ip4_frags_ns_ctl_unregister(net); |
81566e83 PE |
814 | inet_frags_exit_net(&net->ipv4.frags, &ip4_frags); |
815 | } | |
816 | ||
817 | static struct pernet_operations ip4_frags_ops = { | |
818 | .init = ipv4_frags_init_net, | |
819 | .exit = ipv4_frags_exit_net, | |
820 | }; | |
821 | ||
b7aa0bf7 | 822 | void __init ipfrag_init(void) |
1da177e4 | 823 | { |
7d291ebb | 824 | ip4_frags_ctl_register(); |
81566e83 | 825 | register_pernet_subsys(&ip4_frags_ops); |
321a3a99 | 826 | ip4_frags.hashfn = ip4_hashfn; |
c6fda282 | 827 | ip4_frags.constructor = ip4_frag_init; |
1e4b8287 PE |
828 | ip4_frags.destructor = ip4_frag_free; |
829 | ip4_frags.skb_free = NULL; | |
830 | ip4_frags.qsize = sizeof(struct ipq); | |
abd6523d | 831 | ip4_frags.match = ip4_frag_match; |
e521db9d | 832 | ip4_frags.frag_expire = ip_expire; |
3b4bc4a2 | 833 | ip4_frags.secret_interval = 10 * 60 * HZ; |
7eb95156 | 834 | inet_frags_init(&ip4_frags); |
1da177e4 | 835 | } |