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