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