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 | |
353c9cb3 PO |
60 | /* Use skb->cb to track consecutive/adjacent fragments coming at |
61 | * the end of the queue. Nodes in the rb-tree queue will | |
62 | * contain "runs" of one or more adjacent fragments. | |
63 | * | |
64 | * Invariants: | |
65 | * - next_frag is NULL at the tail of a "run"; | |
66 | * - the head of a "run" has the sum of all fragment lengths in frag_run_len. | |
67 | */ | |
68 | struct ipfrag_skb_cb { | |
69 | struct inet_skb_parm h; | |
70 | struct sk_buff *next_frag; | |
71 | int frag_run_len; | |
72 | }; | |
73 | ||
74 | #define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb)) | |
75 | ||
76 | static void ip4_frag_init_run(struct sk_buff *skb) | |
77 | { | |
78 | BUILD_BUG_ON(sizeof(struct ipfrag_skb_cb) > sizeof(skb->cb)); | |
79 | ||
80 | FRAG_CB(skb)->next_frag = NULL; | |
81 | FRAG_CB(skb)->frag_run_len = skb->len; | |
82 | } | |
83 | ||
84 | /* Append skb to the last "run". */ | |
85 | static void ip4_frag_append_to_last_run(struct inet_frag_queue *q, | |
86 | struct sk_buff *skb) | |
87 | { | |
88 | RB_CLEAR_NODE(&skb->rbnode); | |
89 | FRAG_CB(skb)->next_frag = NULL; | |
90 | ||
91 | FRAG_CB(q->last_run_head)->frag_run_len += skb->len; | |
92 | FRAG_CB(q->fragments_tail)->next_frag = skb; | |
93 | q->fragments_tail = skb; | |
94 | } | |
95 | ||
96 | /* Create a new "run" with the skb. */ | |
97 | static void ip4_frag_create_run(struct inet_frag_queue *q, struct sk_buff *skb) | |
98 | { | |
99 | if (q->last_run_head) | |
100 | rb_link_node(&skb->rbnode, &q->last_run_head->rbnode, | |
101 | &q->last_run_head->rbnode.rb_right); | |
102 | else | |
103 | rb_link_node(&skb->rbnode, NULL, &q->rb_fragments.rb_node); | |
104 | rb_insert_color(&skb->rbnode, &q->rb_fragments); | |
105 | ||
106 | ip4_frag_init_run(skb); | |
107 | q->fragments_tail = skb; | |
108 | q->last_run_head = skb; | |
109 | } | |
110 | ||
1da177e4 LT |
111 | /* Describe an entry in the "incomplete datagrams" queue. */ |
112 | struct ipq { | |
5ab11c98 PE |
113 | struct inet_frag_queue q; |
114 | ||
6623e3b2 | 115 | u8 ecn; /* RFC3168 support */ |
d6b915e2 | 116 | u16 max_df_size; /* largest frag with DF set seen */ |
89cee8b1 HX |
117 | int iif; |
118 | unsigned int rid; | |
119 | struct inet_peer *peer; | |
1da177e4 LT |
120 | }; |
121 | ||
aa1f731e | 122 | static u8 ip4_frag_ecn(u8 tos) |
6623e3b2 | 123 | { |
5173cc05 | 124 | return 1 << (tos & INET_ECN_MASK); |
6623e3b2 ED |
125 | } |
126 | ||
7eb95156 | 127 | static struct inet_frags ip4_frags; |
1da177e4 | 128 | |
a4fd284a PO |
129 | static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb, |
130 | struct sk_buff *prev_tail, struct net_device *dev); | |
1706d587 | 131 | |
abd6523d | 132 | |
36c77782 | 133 | static void ip4_frag_init(struct inet_frag_queue *q, const void *a) |
c6fda282 PE |
134 | { |
135 | struct ipq *qp = container_of(q, struct ipq, q); | |
54db0cc2 G |
136 | struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4, |
137 | frags); | |
138 | struct net *net = container_of(ipv4, struct net, ipv4); | |
139 | ||
648700f7 | 140 | const struct frag_v4_compare_key *key = a; |
c6fda282 | 141 | |
648700f7 ED |
142 | q->key.v4 = *key; |
143 | qp->ecn = 0; | |
0fbf4cb2 | 144 | qp->peer = q->net->max_dist ? |
648700f7 | 145 | inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif, 1) : |
192132b9 | 146 | NULL; |
c6fda282 PE |
147 | } |
148 | ||
aa1f731e | 149 | static void ip4_frag_free(struct inet_frag_queue *q) |
1da177e4 | 150 | { |
1e4b8287 PE |
151 | struct ipq *qp; |
152 | ||
153 | qp = container_of(q, struct ipq, q); | |
154 | if (qp->peer) | |
155 | inet_putpeer(qp->peer); | |
1da177e4 LT |
156 | } |
157 | ||
1da177e4 LT |
158 | |
159 | /* Destruction primitives. */ | |
160 | ||
aa1f731e | 161 | static void ipq_put(struct ipq *ipq) |
1da177e4 | 162 | { |
093ba729 | 163 | inet_frag_put(&ipq->q); |
1da177e4 LT |
164 | } |
165 | ||
166 | /* Kill ipq entry. It is not destroyed immediately, | |
167 | * because caller (and someone more) holds reference count. | |
168 | */ | |
169 | static void ipq_kill(struct ipq *ipq) | |
170 | { | |
093ba729 | 171 | inet_frag_kill(&ipq->q); |
1da177e4 LT |
172 | } |
173 | ||
5cf42280 AZ |
174 | static bool frag_expire_skip_icmp(u32 user) |
175 | { | |
176 | return user == IP_DEFRAG_AF_PACKET || | |
177 | ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN, | |
8bc04864 AZ |
178 | __IP_DEFRAG_CONNTRACK_IN_END) || |
179 | ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN, | |
180 | __IP_DEFRAG_CONNTRACK_BRIDGE_IN); | |
5cf42280 AZ |
181 | } |
182 | ||
1da177e4 LT |
183 | /* |
184 | * Oops, a fragment queue timed out. Kill it and send an ICMP reply. | |
185 | */ | |
78802011 | 186 | static void ip_expire(struct timer_list *t) |
1da177e4 | 187 | { |
78802011 | 188 | struct inet_frag_queue *frag = from_timer(frag, t, timer); |
399d1404 | 189 | const struct iphdr *iph; |
fa0f5273 | 190 | struct sk_buff *head = NULL; |
84a3aa00 | 191 | struct net *net; |
399d1404 ED |
192 | struct ipq *qp; |
193 | int err; | |
e521db9d | 194 | |
78802011 | 195 | qp = container_of(frag, struct ipq, q); |
84a3aa00 | 196 | net = container_of(qp->q.net, struct net, ipv4.frags); |
1da177e4 | 197 | |
ec4fbd64 | 198 | rcu_read_lock(); |
5ab11c98 | 199 | spin_lock(&qp->q.lock); |
1da177e4 | 200 | |
06aa8b8a | 201 | if (qp->q.flags & INET_FRAG_COMPLETE) |
1da177e4 LT |
202 | goto out; |
203 | ||
204 | ipq_kill(qp); | |
b45386ef | 205 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); |
399d1404 | 206 | __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT); |
2e404f63 | 207 | |
70837ffe | 208 | if (!(qp->q.flags & INET_FRAG_FIRST_IN)) |
399d1404 | 209 | goto out; |
2e404f63 | 210 | |
fa0f5273 PO |
211 | /* sk_buff::dev and sk_buff::rbnode are unionized. So we |
212 | * pull the head out of the tree in order to be able to | |
213 | * deal with head->dev. | |
214 | */ | |
215 | if (qp->q.fragments) { | |
216 | head = qp->q.fragments; | |
217 | qp->q.fragments = head->next; | |
218 | } else { | |
219 | head = skb_rb_first(&qp->q.rb_fragments); | |
220 | if (!head) | |
221 | goto out; | |
a4fd284a PO |
222 | if (FRAG_CB(head)->next_frag) |
223 | rb_replace_node(&head->rbnode, | |
224 | &FRAG_CB(head)->next_frag->rbnode, | |
225 | &qp->q.rb_fragments); | |
226 | else | |
227 | rb_erase(&head->rbnode, &qp->q.rb_fragments); | |
fa0f5273 PO |
228 | memset(&head->rbnode, 0, sizeof(head->rbnode)); |
229 | barrier(); | |
230 | } | |
231 | if (head == qp->q.fragments_tail) | |
232 | qp->q.fragments_tail = NULL; | |
233 | ||
234 | sub_frag_mem_limit(qp->q.net, head->truesize); | |
235 | ||
399d1404 ED |
236 | head->dev = dev_get_by_index_rcu(net, qp->iif); |
237 | if (!head->dev) | |
238 | goto out; | |
ec4fbd64 | 239 | |
e9017b55 | 240 | |
399d1404 ED |
241 | /* skb has no dst, perform route lookup again */ |
242 | iph = ip_hdr(head); | |
243 | err = ip_route_input_noref(head, iph->daddr, iph->saddr, | |
c6cffba4 | 244 | iph->tos, head->dev); |
399d1404 ED |
245 | if (err) |
246 | goto out; | |
247 | ||
248 | /* Only an end host needs to send an ICMP | |
249 | * "Fragment Reassembly Timeout" message, per RFC792. | |
250 | */ | |
251 | if (frag_expire_skip_icmp(qp->q.key.v4.user) && | |
252 | (skb_rtable(head)->rt_type != RTN_LOCAL)) | |
253 | goto out; | |
254 | ||
1eec5d56 ED |
255 | spin_unlock(&qp->q.lock); |
256 | icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0); | |
1eec5d56 | 257 | goto out_rcu_unlock; |
399d1404 | 258 | |
1da177e4 | 259 | out: |
5ab11c98 | 260 | spin_unlock(&qp->q.lock); |
ec4fbd64 ED |
261 | out_rcu_unlock: |
262 | rcu_read_unlock(); | |
fa0f5273 PO |
263 | if (head) |
264 | kfree_skb(head); | |
4b6cb5d8 | 265 | ipq_put(qp); |
1da177e4 LT |
266 | } |
267 | ||
abd6523d PE |
268 | /* Find the correct entry in the "incomplete datagrams" queue for |
269 | * this IP datagram, and create new one, if nothing is found. | |
270 | */ | |
9972f134 DA |
271 | static struct ipq *ip_find(struct net *net, struct iphdr *iph, |
272 | u32 user, int vif) | |
1da177e4 | 273 | { |
648700f7 ED |
274 | struct frag_v4_compare_key key = { |
275 | .saddr = iph->saddr, | |
276 | .daddr = iph->daddr, | |
277 | .user = user, | |
278 | .vif = vif, | |
279 | .id = iph->id, | |
280 | .protocol = iph->protocol, | |
281 | }; | |
c6fda282 | 282 | struct inet_frag_queue *q; |
9a375803 | 283 | |
648700f7 | 284 | q = inet_frag_find(&net->ipv4.frags, &key); |
2d44ed22 | 285 | if (!q) |
5a3da1fe | 286 | return NULL; |
2d44ed22 | 287 | |
c6fda282 | 288 | return container_of(q, struct ipq, q); |
1da177e4 LT |
289 | } |
290 | ||
89cee8b1 | 291 | /* Is the fragment too far ahead to be part of ipq? */ |
aa1f731e | 292 | static int ip_frag_too_far(struct ipq *qp) |
89cee8b1 HX |
293 | { |
294 | struct inet_peer *peer = qp->peer; | |
0fbf4cb2 | 295 | unsigned int max = qp->q.net->max_dist; |
89cee8b1 HX |
296 | unsigned int start, end; |
297 | ||
298 | int rc; | |
299 | ||
300 | if (!peer || !max) | |
301 | return 0; | |
302 | ||
303 | start = qp->rid; | |
304 | end = atomic_inc_return(&peer->rid); | |
305 | qp->rid = end; | |
306 | ||
fa0f5273 | 307 | rc = qp->q.fragments_tail && (end - start) > max; |
89cee8b1 HX |
308 | |
309 | if (rc) { | |
7c73a6fa PE |
310 | struct net *net; |
311 | ||
312 | net = container_of(qp->q.net, struct net, ipv4.frags); | |
b45386ef | 313 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); |
89cee8b1 HX |
314 | } |
315 | ||
316 | return rc; | |
317 | } | |
318 | ||
319 | static int ip_frag_reinit(struct ipq *qp) | |
320 | { | |
d433673e | 321 | unsigned int sum_truesize = 0; |
89cee8b1 | 322 | |
b2fd5321 | 323 | if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) { |
edcb6918 | 324 | refcount_inc(&qp->q.refcnt); |
89cee8b1 HX |
325 | return -ETIMEDOUT; |
326 | } | |
327 | ||
a4fd284a | 328 | sum_truesize = inet_frag_rbtree_purge(&qp->q.rb_fragments); |
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; | |
fa0f5273 | 335 | qp->q.rb_fragments = RB_ROOT; |
d6bebca9 | 336 | qp->q.fragments_tail = NULL; |
a4fd284a | 337 | qp->q.last_run_head = NULL; |
89cee8b1 | 338 | qp->iif = 0; |
6623e3b2 | 339 | qp->ecn = 0; |
89cee8b1 HX |
340 | |
341 | return 0; | |
342 | } | |
343 | ||
1da177e4 | 344 | /* Add new segment to existing queue. */ |
1706d587 | 345 | static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb) |
1da177e4 | 346 | { |
7969e5c4 | 347 | struct net *net = container_of(qp->q.net, struct net, ipv4.frags); |
fa0f5273 | 348 | struct rb_node **rbn, *parent; |
a4fd284a | 349 | struct sk_buff *skb1, *prev_tail; |
1706d587 | 350 | struct net_device *dev; |
d6b915e2 | 351 | unsigned int fragsize; |
1da177e4 LT |
352 | int flags, offset; |
353 | int ihl, end; | |
1706d587 | 354 | int err = -ENOENT; |
6623e3b2 | 355 | u8 ecn; |
1da177e4 | 356 | |
06aa8b8a | 357 | if (qp->q.flags & INET_FRAG_COMPLETE) |
1da177e4 LT |
358 | goto err; |
359 | ||
89cee8b1 | 360 | if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) && |
1706d587 HX |
361 | unlikely(ip_frag_too_far(qp)) && |
362 | unlikely(err = ip_frag_reinit(qp))) { | |
89cee8b1 HX |
363 | ipq_kill(qp); |
364 | goto err; | |
365 | } | |
366 | ||
6623e3b2 | 367 | ecn = ip4_frag_ecn(ip_hdr(skb)->tos); |
eddc9ec5 | 368 | offset = ntohs(ip_hdr(skb)->frag_off); |
1da177e4 LT |
369 | flags = offset & ~IP_OFFSET; |
370 | offset &= IP_OFFSET; | |
371 | offset <<= 3; /* offset is in 8-byte chunks */ | |
c9bdd4b5 | 372 | ihl = ip_hdrlen(skb); |
1da177e4 LT |
373 | |
374 | /* Determine the position of this fragment. */ | |
0848f642 | 375 | end = offset + skb->len - skb_network_offset(skb) - ihl; |
1706d587 | 376 | err = -EINVAL; |
1da177e4 LT |
377 | |
378 | /* Is this the final fragment? */ | |
379 | if ((flags & IP_MF) == 0) { | |
380 | /* If we already have some bits beyond end | |
42b2aa86 | 381 | * or have different end, the segment is corrupted. |
1da177e4 | 382 | */ |
5ab11c98 | 383 | if (end < qp->q.len || |
06aa8b8a | 384 | ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len)) |
1da177e4 | 385 | goto err; |
06aa8b8a | 386 | qp->q.flags |= INET_FRAG_LAST_IN; |
5ab11c98 | 387 | qp->q.len = end; |
1da177e4 LT |
388 | } else { |
389 | if (end&7) { | |
390 | end &= ~7; | |
391 | if (skb->ip_summed != CHECKSUM_UNNECESSARY) | |
392 | skb->ip_summed = CHECKSUM_NONE; | |
393 | } | |
5ab11c98 | 394 | if (end > qp->q.len) { |
1da177e4 | 395 | /* Some bits beyond end -> corruption. */ |
06aa8b8a | 396 | if (qp->q.flags & INET_FRAG_LAST_IN) |
1da177e4 | 397 | goto err; |
5ab11c98 | 398 | qp->q.len = end; |
1da177e4 LT |
399 | } |
400 | } | |
401 | if (end == offset) | |
402 | goto err; | |
403 | ||
1706d587 | 404 | err = -ENOMEM; |
0848f642 | 405 | if (!pskb_pull(skb, skb_network_offset(skb) + ihl)) |
1da177e4 | 406 | goto err; |
1706d587 HX |
407 | |
408 | err = pskb_trim_rcsum(skb, end - offset); | |
409 | if (err) | |
1da177e4 LT |
410 | goto err; |
411 | ||
fa0f5273 PO |
412 | /* Note : skb->rbnode and skb->dev share the same location. */ |
413 | dev = skb->dev; | |
414 | /* Makes sure compiler wont do silly aliasing games */ | |
415 | barrier(); | |
1da177e4 | 416 | |
7969e5c4 PO |
417 | /* RFC5722, Section 4, amended by Errata ID : 3089 |
418 | * When reassembling an IPv6 datagram, if | |
419 | * one or more its constituent fragments is determined to be an | |
420 | * overlapping fragment, the entire datagram (and any constituent | |
421 | * fragments) MUST be silently discarded. | |
422 | * | |
fa0f5273 | 423 | * We do the same here for IPv4 (and increment an snmp counter). |
1da177e4 | 424 | */ |
1da177e4 | 425 | |
fa0f5273 | 426 | /* Find out where to put this fragment. */ |
a4fd284a PO |
427 | prev_tail = qp->q.fragments_tail; |
428 | if (!prev_tail) | |
429 | ip4_frag_create_run(&qp->q, skb); /* First fragment. */ | |
430 | else if (prev_tail->ip_defrag_offset + prev_tail->len < end) { | |
431 | /* This is the common case: skb goes to the end. */ | |
fa0f5273 | 432 | /* Detect and discard overlaps. */ |
a4fd284a | 433 | if (offset < prev_tail->ip_defrag_offset + prev_tail->len) |
fa0f5273 | 434 | goto discard_qp; |
a4fd284a PO |
435 | if (offset == prev_tail->ip_defrag_offset + prev_tail->len) |
436 | ip4_frag_append_to_last_run(&qp->q, skb); | |
437 | else | |
438 | ip4_frag_create_run(&qp->q, skb); | |
fa0f5273 | 439 | } else { |
a4fd284a PO |
440 | /* Binary search. Note that skb can become the first fragment, |
441 | * but not the last (covered above). | |
442 | */ | |
fa0f5273 PO |
443 | rbn = &qp->q.rb_fragments.rb_node; |
444 | do { | |
445 | parent = *rbn; | |
446 | skb1 = rb_to_skb(parent); | |
447 | if (end <= skb1->ip_defrag_offset) | |
448 | rbn = &parent->rb_left; | |
a4fd284a PO |
449 | else if (offset >= skb1->ip_defrag_offset + |
450 | FRAG_CB(skb1)->frag_run_len) | |
fa0f5273 PO |
451 | rbn = &parent->rb_right; |
452 | else /* Found an overlap with skb1. */ | |
453 | goto discard_qp; | |
454 | } while (*rbn); | |
455 | /* Here we have parent properly set, and rbn pointing to | |
a4fd284a PO |
456 | * one of its NULL left/right children. Insert skb. |
457 | */ | |
458 | ip4_frag_init_run(skb); | |
fa0f5273 | 459 | rb_link_node(&skb->rbnode, parent, rbn); |
a4fd284a | 460 | rb_insert_color(&skb->rbnode, &qp->q.rb_fragments); |
fa0f5273 | 461 | } |
1da177e4 | 462 | |
bf663371 ED |
463 | if (dev) |
464 | qp->iif = dev->ifindex; | |
bf663371 | 465 | skb->ip_defrag_offset = offset; |
1da177e4 | 466 | |
5ab11c98 PE |
467 | qp->q.stamp = skb->tstamp; |
468 | qp->q.meat += skb->len; | |
6623e3b2 | 469 | qp->ecn |= ecn; |
0e60d245 | 470 | add_frag_mem_limit(qp->q.net, skb->truesize); |
1da177e4 | 471 | if (offset == 0) |
06aa8b8a | 472 | qp->q.flags |= INET_FRAG_FIRST_IN; |
1da177e4 | 473 | |
d6b915e2 FW |
474 | fragsize = skb->len + ihl; |
475 | ||
476 | if (fragsize > qp->q.max_size) | |
477 | qp->q.max_size = fragsize; | |
478 | ||
5f2d04f1 | 479 | if (ip_hdr(skb)->frag_off & htons(IP_DF) && |
d6b915e2 FW |
480 | fragsize > qp->max_df_size) |
481 | qp->max_df_size = fragsize; | |
5f2d04f1 | 482 | |
06aa8b8a | 483 | if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && |
97599dc7 ED |
484 | qp->q.meat == qp->q.len) { |
485 | unsigned long orefdst = skb->_skb_refdst; | |
1706d587 | 486 | |
97599dc7 | 487 | skb->_skb_refdst = 0UL; |
a4fd284a | 488 | err = ip_frag_reasm(qp, skb, prev_tail, dev); |
97599dc7 ED |
489 | skb->_skb_refdst = orefdst; |
490 | return err; | |
491 | } | |
492 | ||
493 | skb_dst_drop(skb); | |
1706d587 | 494 | return -EINPROGRESS; |
1da177e4 | 495 | |
7969e5c4 PO |
496 | discard_qp: |
497 | inet_frag_kill(&qp->q); | |
498 | err = -EINVAL; | |
499 | __IP_INC_STATS(net, IPSTATS_MIB_REASM_OVERLAPS); | |
1da177e4 LT |
500 | err: |
501 | kfree_skb(skb); | |
1706d587 | 502 | return err; |
1da177e4 LT |
503 | } |
504 | ||
1da177e4 | 505 | /* Build a new IP datagram from all its fragments. */ |
fa0f5273 | 506 | static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb, |
a4fd284a | 507 | struct sk_buff *prev_tail, struct net_device *dev) |
1da177e4 | 508 | { |
2bad35b7 | 509 | struct net *net = container_of(qp->q.net, struct net, ipv4.frags); |
1da177e4 | 510 | struct iphdr *iph; |
fa0f5273 PO |
511 | struct sk_buff *fp, *head = skb_rb_first(&qp->q.rb_fragments); |
512 | struct sk_buff **nextp; /* To build frag_list. */ | |
513 | struct rb_node *rbn; | |
1da177e4 LT |
514 | int len; |
515 | int ihlen; | |
1706d587 | 516 | int err; |
5173cc05 | 517 | u8 ecn; |
1da177e4 LT |
518 | |
519 | ipq_kill(qp); | |
520 | ||
be991971 | 521 | ecn = ip_frag_ecn_table[qp->ecn]; |
5173cc05 ED |
522 | if (unlikely(ecn == 0xff)) { |
523 | err = -EINVAL; | |
524 | goto out_fail; | |
525 | } | |
1706d587 | 526 | /* Make the one we just received the head. */ |
fa0f5273 PO |
527 | if (head != skb) { |
528 | fp = skb_clone(skb, GFP_ATOMIC); | |
1706d587 HX |
529 | if (!fp) |
530 | goto out_nomem; | |
a4fd284a PO |
531 | FRAG_CB(fp)->next_frag = FRAG_CB(skb)->next_frag; |
532 | if (RB_EMPTY_NODE(&skb->rbnode)) | |
533 | FRAG_CB(prev_tail)->next_frag = fp; | |
534 | else | |
535 | rb_replace_node(&skb->rbnode, &fp->rbnode, | |
536 | &qp->q.rb_fragments); | |
fa0f5273 | 537 | if (qp->q.fragments_tail == skb) |
d6bebca9 | 538 | qp->q.fragments_tail = fp; |
fa0f5273 | 539 | skb_morph(skb, head); |
a4fd284a | 540 | FRAG_CB(skb)->next_frag = FRAG_CB(head)->next_frag; |
fa0f5273 PO |
541 | rb_replace_node(&head->rbnode, &skb->rbnode, |
542 | &qp->q.rb_fragments); | |
543 | consume_skb(head); | |
544 | head = skb; | |
1706d587 HX |
545 | } |
546 | ||
bf663371 | 547 | WARN_ON(head->ip_defrag_offset != 0); |
1da177e4 LT |
548 | |
549 | /* Allocate a new buffer for the datagram. */ | |
c9bdd4b5 | 550 | ihlen = ip_hdrlen(head); |
5ab11c98 | 551 | len = ihlen + qp->q.len; |
1da177e4 | 552 | |
1706d587 | 553 | err = -E2BIG; |
132adf54 | 554 | if (len > 65535) |
1da177e4 LT |
555 | goto out_oversize; |
556 | ||
557 | /* Head of list must not be cloned. */ | |
14bbd6a5 | 558 | if (skb_unclone(head, GFP_ATOMIC)) |
1da177e4 LT |
559 | goto out_nomem; |
560 | ||
561 | /* If the first fragment is fragmented itself, we split | |
562 | * it to two chunks: the first with data and paged part | |
563 | * and the second, holding only fragments. */ | |
21dc3301 | 564 | if (skb_has_frag_list(head)) { |
1da177e4 LT |
565 | struct sk_buff *clone; |
566 | int i, plen = 0; | |
567 | ||
51456b29 IM |
568 | clone = alloc_skb(0, GFP_ATOMIC); |
569 | if (!clone) | |
1da177e4 | 570 | goto out_nomem; |
1da177e4 | 571 | skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; |
d7fcf1a5 | 572 | skb_frag_list_init(head); |
9e903e08 ED |
573 | for (i = 0; i < skb_shinfo(head)->nr_frags; i++) |
574 | plen += skb_frag_size(&skb_shinfo(head)->frags[i]); | |
1da177e4 | 575 | clone->len = clone->data_len = head->data_len - plen; |
a4fd284a | 576 | head->truesize += clone->truesize; |
1da177e4 LT |
577 | clone->csum = 0; |
578 | clone->ip_summed = head->ip_summed; | |
0e60d245 | 579 | add_frag_mem_limit(qp->q.net, clone->truesize); |
fa0f5273 PO |
580 | skb_shinfo(head)->frag_list = clone; |
581 | nextp = &clone->next; | |
582 | } else { | |
583 | nextp = &skb_shinfo(head)->frag_list; | |
1da177e4 LT |
584 | } |
585 | ||
d56f90a7 | 586 | skb_push(head, head->data - skb_network_header(head)); |
1da177e4 | 587 | |
fa0f5273 | 588 | /* Traverse the tree in order, to build frag_list. */ |
a4fd284a | 589 | fp = FRAG_CB(head)->next_frag; |
fa0f5273 PO |
590 | rbn = rb_next(&head->rbnode); |
591 | rb_erase(&head->rbnode, &qp->q.rb_fragments); | |
a4fd284a PO |
592 | while (rbn || fp) { |
593 | /* fp points to the next sk_buff in the current run; | |
594 | * rbn points to the next run. | |
595 | */ | |
596 | /* Go through the current run. */ | |
597 | while (fp) { | |
598 | *nextp = fp; | |
599 | nextp = &fp->next; | |
600 | fp->prev = NULL; | |
601 | memset(&fp->rbnode, 0, sizeof(fp->rbnode)); | |
5d407b07 | 602 | fp->sk = NULL; |
a4fd284a PO |
603 | head->data_len += fp->len; |
604 | head->len += fp->len; | |
605 | if (head->ip_summed != fp->ip_summed) | |
606 | head->ip_summed = CHECKSUM_NONE; | |
607 | else if (head->ip_summed == CHECKSUM_COMPLETE) | |
608 | head->csum = csum_add(head->csum, fp->csum); | |
609 | head->truesize += fp->truesize; | |
610 | fp = FRAG_CB(fp)->next_frag; | |
611 | } | |
612 | /* Move to the next run. */ | |
613 | if (rbn) { | |
614 | struct rb_node *rbnext = rb_next(rbn); | |
615 | ||
616 | fp = rb_to_skb(rbn); | |
617 | rb_erase(rbn, &qp->q.rb_fragments); | |
618 | rbn = rbnext; | |
619 | } | |
1da177e4 | 620 | } |
5510b3c2 | 621 | sub_frag_mem_limit(qp->q.net, head->truesize); |
1da177e4 | 622 | |
fa0f5273 | 623 | *nextp = NULL; |
1da177e4 | 624 | head->next = NULL; |
fa0f5273 | 625 | head->prev = NULL; |
1da177e4 | 626 | head->dev = dev; |
5ab11c98 | 627 | head->tstamp = qp->q.stamp; |
d6b915e2 | 628 | IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size); |
1da177e4 | 629 | |
eddc9ec5 | 630 | iph = ip_hdr(head); |
1da177e4 | 631 | iph->tot_len = htons(len); |
5173cc05 | 632 | iph->tos |= ecn; |
d6b915e2 FW |
633 | |
634 | /* When we set IP_DF on a refragmented skb we must also force a | |
635 | * call to ip_fragment to avoid forwarding a DF-skb of size s while | |
636 | * original sender only sent fragments of size f (where f < s). | |
637 | * | |
638 | * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest | |
639 | * frag seen to avoid sending tiny DF-fragments in case skb was built | |
640 | * from one very small df-fragment and one large non-df frag. | |
641 | */ | |
642 | if (qp->max_df_size == qp->q.max_size) { | |
643 | IPCB(head)->flags |= IPSKB_FRAG_PMTU; | |
644 | iph->frag_off = htons(IP_DF); | |
645 | } else { | |
646 | iph->frag_off = 0; | |
647 | } | |
648 | ||
0848f642 EHJ |
649 | ip_send_check(iph); |
650 | ||
b45386ef | 651 | __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS); |
5ab11c98 | 652 | qp->q.fragments = NULL; |
fa0f5273 | 653 | qp->q.rb_fragments = RB_ROOT; |
d6bebca9 | 654 | qp->q.fragments_tail = NULL; |
a4fd284a | 655 | qp->q.last_run_head = NULL; |
1706d587 | 656 | return 0; |
1da177e4 LT |
657 | |
658 | out_nomem: | |
ba7a46f1 | 659 | net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp); |
45542479 | 660 | err = -ENOMEM; |
1da177e4 LT |
661 | goto out_fail; |
662 | out_oversize: | |
648700f7 | 663 | net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr); |
1da177e4 | 664 | out_fail: |
b45386ef | 665 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); |
1706d587 | 666 | return err; |
1da177e4 LT |
667 | } |
668 | ||
669 | /* Process an incoming IP datagram fragment. */ | |
19bcf9f2 | 670 | int ip_defrag(struct net *net, struct sk_buff *skb, u32 user) |
1da177e4 | 671 | { |
9972f134 | 672 | struct net_device *dev = skb->dev ? : skb_dst(skb)->dev; |
385add90 | 673 | int vif = l3mdev_master_ifindex_rcu(dev); |
1da177e4 | 674 | struct ipq *qp; |
e905a9ed | 675 | |
b45386ef | 676 | __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS); |
8282f274 | 677 | skb_orphan(skb); |
1da177e4 | 678 | |
1da177e4 | 679 | /* Lookup (or create) queue header */ |
9972f134 | 680 | qp = ip_find(net, ip_hdr(skb), user, vif); |
00db4124 | 681 | if (qp) { |
1706d587 | 682 | int ret; |
1da177e4 | 683 | |
5ab11c98 | 684 | spin_lock(&qp->q.lock); |
1da177e4 | 685 | |
1706d587 | 686 | ret = ip_frag_queue(qp, skb); |
1da177e4 | 687 | |
5ab11c98 | 688 | spin_unlock(&qp->q.lock); |
4b6cb5d8 | 689 | ipq_put(qp); |
776c729e | 690 | return ret; |
1da177e4 LT |
691 | } |
692 | ||
b45386ef | 693 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); |
1da177e4 | 694 | kfree_skb(skb); |
776c729e | 695 | return -ENOMEM; |
1da177e4 | 696 | } |
4bc2f18b | 697 | EXPORT_SYMBOL(ip_defrag); |
1da177e4 | 698 | |
19bcf9f2 | 699 | struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user) |
bc416d97 | 700 | { |
1bf3751e | 701 | struct iphdr iph; |
3e32e733 | 702 | int netoff; |
bc416d97 ED |
703 | u32 len; |
704 | ||
705 | if (skb->protocol != htons(ETH_P_IP)) | |
706 | return skb; | |
707 | ||
3e32e733 AD |
708 | netoff = skb_network_offset(skb); |
709 | ||
710 | if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0) | |
bc416d97 ED |
711 | return skb; |
712 | ||
1bf3751e | 713 | if (iph.ihl < 5 || iph.version != 4) |
bc416d97 | 714 | return skb; |
1bf3751e JB |
715 | |
716 | len = ntohs(iph.tot_len); | |
3e32e733 | 717 | if (skb->len < netoff + len || len < (iph.ihl * 4)) |
bc416d97 ED |
718 | return skb; |
719 | ||
1bf3751e | 720 | if (ip_is_fragment(&iph)) { |
bc416d97 ED |
721 | skb = skb_share_check(skb, GFP_ATOMIC); |
722 | if (skb) { | |
3e32e733 | 723 | if (!pskb_may_pull(skb, netoff + iph.ihl * 4)) |
1bf3751e | 724 | return skb; |
3e32e733 | 725 | if (pskb_trim_rcsum(skb, netoff + len)) |
bc416d97 ED |
726 | return skb; |
727 | memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); | |
19bcf9f2 | 728 | if (ip_defrag(net, skb, user)) |
bc416d97 | 729 | return NULL; |
7539fadc | 730 | skb_clear_hash(skb); |
bc416d97 ED |
731 | } |
732 | } | |
733 | return skb; | |
734 | } | |
735 | EXPORT_SYMBOL(ip_check_defrag); | |
736 | ||
353c9cb3 PO |
737 | unsigned int inet_frag_rbtree_purge(struct rb_root *root) |
738 | { | |
739 | struct rb_node *p = rb_first(root); | |
740 | unsigned int sum = 0; | |
741 | ||
742 | while (p) { | |
743 | struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); | |
744 | ||
745 | p = rb_next(p); | |
746 | rb_erase(&skb->rbnode, root); | |
747 | while (skb) { | |
748 | struct sk_buff *next = FRAG_CB(skb)->next_frag; | |
749 | ||
750 | sum += skb->truesize; | |
751 | kfree_skb(skb); | |
752 | skb = next; | |
753 | } | |
754 | } | |
755 | return sum; | |
756 | } | |
757 | EXPORT_SYMBOL(inet_frag_rbtree_purge); | |
758 | ||
8d8354d2 | 759 | #ifdef CONFIG_SYSCTL |
3d234012 | 760 | static int dist_min; |
8d8354d2 | 761 | |
0a64b4b8 | 762 | static struct ctl_table ip4_frags_ns_ctl_table[] = { |
8d8354d2 | 763 | { |
8d8354d2 | 764 | .procname = "ipfrag_high_thresh", |
e31e0bdc | 765 | .data = &init_net.ipv4.frags.high_thresh, |
3e67f106 | 766 | .maxlen = sizeof(unsigned long), |
8d8354d2 | 767 | .mode = 0644, |
3e67f106 | 768 | .proc_handler = proc_doulongvec_minmax, |
1bab4c75 | 769 | .extra1 = &init_net.ipv4.frags.low_thresh |
8d8354d2 PE |
770 | }, |
771 | { | |
8d8354d2 | 772 | .procname = "ipfrag_low_thresh", |
e31e0bdc | 773 | .data = &init_net.ipv4.frags.low_thresh, |
3e67f106 | 774 | .maxlen = sizeof(unsigned long), |
8d8354d2 | 775 | .mode = 0644, |
3e67f106 | 776 | .proc_handler = proc_doulongvec_minmax, |
1bab4c75 | 777 | .extra2 = &init_net.ipv4.frags.high_thresh |
8d8354d2 PE |
778 | }, |
779 | { | |
8d8354d2 | 780 | .procname = "ipfrag_time", |
b2fd5321 | 781 | .data = &init_net.ipv4.frags.timeout, |
8d8354d2 PE |
782 | .maxlen = sizeof(int), |
783 | .mode = 0644, | |
6d9f239a | 784 | .proc_handler = proc_dointvec_jiffies, |
8d8354d2 | 785 | }, |
0fbf4cb2 NB |
786 | { |
787 | .procname = "ipfrag_max_dist", | |
788 | .data = &init_net.ipv4.frags.max_dist, | |
789 | .maxlen = sizeof(int), | |
790 | .mode = 0644, | |
791 | .proc_handler = proc_dointvec_minmax, | |
3d234012 | 792 | .extra1 = &dist_min, |
0fbf4cb2 | 793 | }, |
7d291ebb PE |
794 | { } |
795 | }; | |
796 | ||
e3a57d18 FW |
797 | /* secret interval has been deprecated */ |
798 | static int ip4_frags_secret_interval_unused; | |
7d291ebb | 799 | static struct ctl_table ip4_frags_ctl_table[] = { |
8d8354d2 | 800 | { |
8d8354d2 | 801 | .procname = "ipfrag_secret_interval", |
e3a57d18 | 802 | .data = &ip4_frags_secret_interval_unused, |
8d8354d2 PE |
803 | .maxlen = sizeof(int), |
804 | .mode = 0644, | |
6d9f239a | 805 | .proc_handler = proc_dointvec_jiffies, |
8d8354d2 | 806 | }, |
8d8354d2 PE |
807 | { } |
808 | }; | |
809 | ||
2c8c1e72 | 810 | static int __net_init ip4_frags_ns_ctl_register(struct net *net) |
8d8354d2 | 811 | { |
e4a2d5c2 | 812 | struct ctl_table *table; |
8d8354d2 PE |
813 | struct ctl_table_header *hdr; |
814 | ||
0a64b4b8 | 815 | table = ip4_frags_ns_ctl_table; |
09ad9bc7 | 816 | if (!net_eq(net, &init_net)) { |
0a64b4b8 | 817 | table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL); |
51456b29 | 818 | if (!table) |
e4a2d5c2 PE |
819 | goto err_alloc; |
820 | ||
e31e0bdc | 821 | table[0].data = &net->ipv4.frags.high_thresh; |
1bab4c75 NA |
822 | table[0].extra1 = &net->ipv4.frags.low_thresh; |
823 | table[0].extra2 = &init_net.ipv4.frags.high_thresh; | |
e31e0bdc | 824 | table[1].data = &net->ipv4.frags.low_thresh; |
1bab4c75 | 825 | table[1].extra2 = &net->ipv4.frags.high_thresh; |
b2fd5321 | 826 | table[2].data = &net->ipv4.frags.timeout; |
0fbf4cb2 | 827 | table[3].data = &net->ipv4.frags.max_dist; |
e4a2d5c2 PE |
828 | } |
829 | ||
ec8f23ce | 830 | hdr = register_net_sysctl(net, "net/ipv4", table); |
51456b29 | 831 | if (!hdr) |
e4a2d5c2 PE |
832 | goto err_reg; |
833 | ||
834 | net->ipv4.frags_hdr = hdr; | |
835 | return 0; | |
836 | ||
837 | err_reg: | |
09ad9bc7 | 838 | if (!net_eq(net, &init_net)) |
e4a2d5c2 PE |
839 | kfree(table); |
840 | err_alloc: | |
841 | return -ENOMEM; | |
842 | } | |
843 | ||
2c8c1e72 | 844 | static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net) |
e4a2d5c2 PE |
845 | { |
846 | struct ctl_table *table; | |
847 | ||
848 | table = net->ipv4.frags_hdr->ctl_table_arg; | |
849 | unregister_net_sysctl_table(net->ipv4.frags_hdr); | |
850 | kfree(table); | |
8d8354d2 | 851 | } |
7d291ebb | 852 | |
57a02c39 | 853 | static void __init ip4_frags_ctl_register(void) |
7d291ebb | 854 | { |
43444757 | 855 | register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table); |
7d291ebb | 856 | } |
8d8354d2 | 857 | #else |
aa1f731e | 858 | static int ip4_frags_ns_ctl_register(struct net *net) |
8d8354d2 PE |
859 | { |
860 | return 0; | |
861 | } | |
e4a2d5c2 | 862 | |
aa1f731e | 863 | static void ip4_frags_ns_ctl_unregister(struct net *net) |
e4a2d5c2 PE |
864 | { |
865 | } | |
7d291ebb | 866 | |
aa1f731e | 867 | static void __init ip4_frags_ctl_register(void) |
7d291ebb PE |
868 | { |
869 | } | |
8d8354d2 PE |
870 | #endif |
871 | ||
2c8c1e72 | 872 | static int __net_init ipv4_frags_init_net(struct net *net) |
8d8354d2 | 873 | { |
787bea77 ED |
874 | int res; |
875 | ||
c2a93660 JDB |
876 | /* Fragment cache limits. |
877 | * | |
878 | * The fragment memory accounting code, (tries to) account for | |
879 | * the real memory usage, by measuring both the size of frag | |
880 | * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue)) | |
881 | * and the SKB's truesize. | |
882 | * | |
883 | * A 64K fragment consumes 129736 bytes (44*2944)+200 | |
884 | * (1500 truesize == 2944, sizeof(struct ipq) == 200) | |
885 | * | |
886 | * We will commit 4MB at one time. Should we cross that limit | |
887 | * we will prune down to 3MB, making room for approx 8 big 64K | |
888 | * fragments 8x128k. | |
e31e0bdc | 889 | */ |
c2a93660 JDB |
890 | net->ipv4.frags.high_thresh = 4 * 1024 * 1024; |
891 | net->ipv4.frags.low_thresh = 3 * 1024 * 1024; | |
b2fd5321 PE |
892 | /* |
893 | * Important NOTE! Fragment queue must be destroyed before MSL expires. | |
894 | * RFC791 is wrong proposing to prolongate timer each fragment arrival | |
895 | * by TTL. | |
896 | */ | |
897 | net->ipv4.frags.timeout = IP_FRAG_TIME; | |
898 | ||
0fbf4cb2 | 899 | net->ipv4.frags.max_dist = 64; |
093ba729 | 900 | net->ipv4.frags.f = &ip4_frags; |
0fbf4cb2 | 901 | |
787bea77 ED |
902 | res = inet_frags_init_net(&net->ipv4.frags); |
903 | if (res < 0) | |
904 | return res; | |
905 | res = ip4_frags_ns_ctl_register(net); | |
906 | if (res < 0) | |
093ba729 | 907 | inet_frags_exit_net(&net->ipv4.frags); |
787bea77 | 908 | return res; |
8d8354d2 PE |
909 | } |
910 | ||
2c8c1e72 | 911 | static void __net_exit ipv4_frags_exit_net(struct net *net) |
81566e83 | 912 | { |
0a64b4b8 | 913 | ip4_frags_ns_ctl_unregister(net); |
093ba729 | 914 | inet_frags_exit_net(&net->ipv4.frags); |
81566e83 PE |
915 | } |
916 | ||
917 | static struct pernet_operations ip4_frags_ops = { | |
918 | .init = ipv4_frags_init_net, | |
919 | .exit = ipv4_frags_exit_net, | |
920 | }; | |
921 | ||
648700f7 ED |
922 | |
923 | static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed) | |
924 | { | |
925 | return jhash2(data, | |
926 | sizeof(struct frag_v4_compare_key) / sizeof(u32), seed); | |
927 | } | |
928 | ||
929 | static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed) | |
930 | { | |
931 | const struct inet_frag_queue *fq = data; | |
932 | ||
933 | return jhash2((const u32 *)&fq->key.v4, | |
934 | sizeof(struct frag_v4_compare_key) / sizeof(u32), seed); | |
935 | } | |
936 | ||
937 | static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr) | |
938 | { | |
939 | const struct frag_v4_compare_key *key = arg->key; | |
940 | const struct inet_frag_queue *fq = ptr; | |
941 | ||
942 | return !!memcmp(&fq->key, key, sizeof(*key)); | |
943 | } | |
944 | ||
945 | static const struct rhashtable_params ip4_rhash_params = { | |
946 | .head_offset = offsetof(struct inet_frag_queue, node), | |
947 | .key_offset = offsetof(struct inet_frag_queue, key), | |
948 | .key_len = sizeof(struct frag_v4_compare_key), | |
949 | .hashfn = ip4_key_hashfn, | |
950 | .obj_hashfn = ip4_obj_hashfn, | |
951 | .obj_cmpfn = ip4_obj_cmpfn, | |
952 | .automatic_shrinking = true, | |
953 | }; | |
954 | ||
b7aa0bf7 | 955 | void __init ipfrag_init(void) |
1da177e4 | 956 | { |
c6fda282 | 957 | ip4_frags.constructor = ip4_frag_init; |
1e4b8287 | 958 | ip4_frags.destructor = ip4_frag_free; |
1e4b8287 | 959 | ip4_frags.qsize = sizeof(struct ipq); |
e521db9d | 960 | ip4_frags.frag_expire = ip_expire; |
d4ad4d22 | 961 | ip4_frags.frags_cache_name = ip_frag_cache_name; |
648700f7 | 962 | ip4_frags.rhash_params = ip4_rhash_params; |
d4ad4d22 NA |
963 | if (inet_frags_init(&ip4_frags)) |
964 | panic("IP: failed to allocate ip4_frags cache\n"); | |
483a6e4f ED |
965 | ip4_frags_ctl_register(); |
966 | register_pernet_subsys(&ip4_frags_ops); | |
1da177e4 | 967 | } |