1841c49b93819e305d1bcc6b15e4fc1d18d00629
[cascardo/ovs.git] / datapath / linux / compat / ip_fragment.c
1 /*
2  * IP fragmentation backport, heavily based on linux/net/ipv4/ip_fragment.c,
3  * copied from Linux 192132b9a034 net: Add support for VRFs to inetpeer cache
4  *
5  * INET         An implementation of the TCP/IP protocol suite for the LINUX
6  *              operating system.  INET is implemented using the  BSD Socket
7  *              interface as the means of communication with the user level.
8  *
9  *              The IP fragmentation functionality.
10  *
11  * Authors:     Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
12  *              Alan Cox <alan@lxorguk.ukuu.org.uk>
13  *
14  * Fixes:
15  *              Alan Cox        :       Split from ip.c , see ip_input.c for history.
16  *              David S. Miller :       Begin massive cleanup...
17  *              Andi Kleen      :       Add sysctls.
18  *              xxxx            :       Overlapfrag bug.
19  *              Ultima          :       ip_expire() kernel panic.
20  *              Bill Hawes      :       Frag accounting and evictor fixes.
21  *              John McDonald   :       0 length frag bug.
22  *              Alexey Kuznetsov:       SMP races, threading, cleanup.
23  *              Patrick McHardy :       LRU queue of frag heads for evictor.
24  */
25
26 #include <linux/version.h>
27
28 #ifdef OVS_FRAGMENT_BACKPORT
29
30 #define pr_fmt(fmt) "IPv4: " fmt
31
32 #include <linux/compiler.h>
33 #include <linux/module.h>
34 #include <linux/types.h>
35 #include <linux/mm.h>
36 #include <linux/jiffies.h>
37 #include <linux/skbuff.h>
38 #include <linux/list.h>
39 #include <linux/ip.h>
40 #include <linux/icmp.h>
41 #include <linux/netdevice.h>
42 #include <linux/jhash.h>
43 #include <linux/random.h>
44 #include <linux/slab.h>
45 #include <net/route.h>
46 #include <net/dst.h>
47 #include <net/sock.h>
48 #include <net/ip.h>
49 #include <net/icmp.h>
50 #include <net/checksum.h>
51 #include <net/inetpeer.h>
52 #include <net/inet_frag.h>
53 #include <linux/tcp.h>
54 #include <linux/udp.h>
55 #include <linux/inet.h>
56 #include <linux/netfilter_ipv4.h>
57 #include <net/inet_ecn.h>
58 #include <net/vrf.h>
59 #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
60
61 /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
62  * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
63  * as well. Or notify me, at least. --ANK
64  */
65
66 static int sysctl_ipfrag_max_dist __read_mostly = 64;
67 static const char ip_frag_cache_name[] = "ovs-frag4";
68
69 struct ipfrag_skb_cb
70 {
71         struct inet_skb_parm    h;
72         int                     offset;
73 };
74
75 #define FRAG_CB(skb)    ((struct ipfrag_skb_cb *)((skb)->cb))
76
77 /* Describe an entry in the "incomplete datagrams" queue. */
78 struct ipq {
79         union {
80                 struct inet_frag_queue q;
81 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
82                 struct ovs_inet_frag_queue oq;
83 #endif
84         };
85
86         u32             user;
87         __be32          saddr;
88         __be32          daddr;
89         __be16          id;
90         u8              protocol;
91         u8              ecn; /* RFC3168 support */
92         u16             max_df_size; /* largest frag with DF set seen */
93         int             iif;
94         int             vif;   /* VRF device index */
95         unsigned int    rid;
96         struct inet_peer *peer;
97 };
98
99 static u8 ip4_frag_ecn(u8 tos)
100 {
101         return 1 << (tos & INET_ECN_MASK);
102 }
103
104 static struct inet_frags ip4_frags;
105
106 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
107                          struct net_device *dev);
108
109 struct ip4_create_arg {
110         struct iphdr *iph;
111         u32 user;
112         int vif;
113 };
114
115 static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
116 {
117         net_get_random_once(&ip4_frags.rnd, sizeof(ip4_frags.rnd));
118         return jhash_3words((__force u32)id << 16 | prot,
119                             (__force u32)saddr, (__force u32)daddr,
120                             ip4_frags.rnd);
121 }
122
123 #ifdef HAVE_INET_FRAGS_CONST
124 static unsigned int ip4_hashfn(const struct inet_frag_queue *q)
125 #else
126 static unsigned int ip4_hashfn(struct inet_frag_queue *q)
127 #endif
128 {
129         const struct ipq *ipq;
130
131         ipq = container_of(q, struct ipq, q);
132         return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
133 }
134
135 #ifdef HAVE_INET_FRAGS_CONST
136 static bool ip4_frag_match(const struct inet_frag_queue *q, const void *a)
137 #else
138 static bool ip4_frag_match(struct inet_frag_queue *q, void *a)
139 #endif
140 {
141         const struct ipq *qp;
142         const struct ip4_create_arg *arg = a;
143
144         qp = container_of(q, struct ipq, q);
145         return  qp->id == arg->iph->id &&
146                 qp->saddr == arg->iph->saddr &&
147                 qp->daddr == arg->iph->daddr &&
148                 qp->protocol == arg->iph->protocol &&
149                 qp->user == arg->user &&
150                 qp->vif == arg->vif;
151 }
152
153 #ifdef HAVE_INET_FRAGS_CONST
154 static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
155 #else
156 static void ip4_frag_init(struct inet_frag_queue *q, void *a)
157 #endif
158 {
159         struct ipq *qp = container_of(q, struct ipq, q);
160         struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
161                                                frags);
162         struct net *net = container_of(ipv4, struct net, ipv4);
163
164         const struct ip4_create_arg *arg = a;
165
166         qp->protocol = arg->iph->protocol;
167         qp->id = arg->iph->id;
168         qp->ecn = ip4_frag_ecn(arg->iph->tos);
169         qp->saddr = arg->iph->saddr;
170         qp->daddr = arg->iph->daddr;
171         qp->vif = arg->vif;
172         qp->user = arg->user;
173         qp->peer = sysctl_ipfrag_max_dist ?
174                 inet_getpeer_v4(net->ipv4.peers, arg->iph->saddr, arg->vif, 1) :
175                 NULL;
176 }
177
178 static void ip4_frag_free(struct inet_frag_queue *q)
179 {
180         struct ipq *qp;
181
182         qp = container_of(q, struct ipq, q);
183         if (qp->peer)
184                 inet_putpeer(qp->peer);
185 }
186
187
188 /* Destruction primitives. */
189
190 static void ipq_put(struct ipq *ipq)
191 {
192         inet_frag_put(&ipq->q, &ip4_frags);
193 }
194
195 /* Kill ipq entry. It is not destroyed immediately,
196  * because caller (and someone more) holds reference count.
197  */
198 static void ipq_kill(struct ipq *ipq)
199 {
200         inet_frag_kill(&ipq->q, &ip4_frags);
201 }
202
203 static bool frag_expire_skip_icmp(u32 user)
204 {
205         return user == IP_DEFRAG_AF_PACKET ||
206                ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
207                                          __IP_DEFRAG_CONNTRACK_IN_END) ||
208                ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
209                                          __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
210 }
211
212 /*
213  * Oops, a fragment queue timed out.  Kill it and send an ICMP reply.
214  */
215 static void ip_expire(unsigned long arg)
216 {
217         struct ipq *qp;
218         struct net *net;
219
220         qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
221         net = container_of(qp->q.net, struct net, ipv4.frags);
222
223         spin_lock(&qp->q.lock);
224
225         if (qp_flags(qp) & INET_FRAG_COMPLETE)
226                 goto out;
227
228         ipq_kill(qp);
229         IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
230
231         if (!inet_frag_evicting(&qp->q)) {
232                 struct sk_buff *head = qp->q.fragments;
233                 const struct iphdr *iph;
234                 int err;
235
236                 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMTIMEOUT);
237
238                 if (!(qp_flags(qp) & INET_FRAG_FIRST_IN) || !qp->q.fragments)
239                         goto out;
240
241                 rcu_read_lock();
242                 head->dev = dev_get_by_index_rcu(net, qp->iif);
243                 if (!head->dev)
244                         goto out_rcu_unlock;
245
246                 /* skb has no dst, perform route lookup again */
247                 iph = ip_hdr(head);
248                 err = ip_route_input_noref(head, iph->daddr, iph->saddr,
249                                            iph->tos, head->dev);
250                 if (err)
251                         goto out_rcu_unlock;
252
253                 /* Only an end host needs to send an ICMP
254                  * "Fragment Reassembly Timeout" message, per RFC792.
255                  */
256                 if (frag_expire_skip_icmp(qp->user) &&
257                     (skb_rtable(head)->rt_type != RTN_LOCAL))
258                         goto out_rcu_unlock;
259
260                 /* Send an ICMP "Fragment Reassembly Timeout" message. */
261                 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
262 out_rcu_unlock:
263                 rcu_read_unlock();
264         }
265 out:
266         spin_unlock(&qp->q.lock);
267         ipq_put(qp);
268 }
269
270 /* Find the correct entry in the "incomplete datagrams" queue for
271  * this IP datagram, and create new one, if nothing is found.
272  */
273 static struct ipq *ip_find(struct net *net, struct iphdr *iph,
274                            u32 user, int vif)
275 {
276         struct inet_frag_queue *q;
277         struct ip4_create_arg arg;
278         unsigned int hash;
279
280         arg.iph = iph;
281         arg.user = user;
282         arg.vif = vif;
283
284         hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
285
286         q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
287         if (IS_ERR_OR_NULL(q)) {
288                 inet_frag_maybe_warn_overflow(q, pr_fmt());
289                 return NULL;
290         }
291         return container_of(q, struct ipq, q);
292 }
293
294 /* Is the fragment too far ahead to be part of ipq? */
295 static int ip_frag_too_far(struct ipq *qp)
296 {
297         struct inet_peer *peer = qp->peer;
298         unsigned int max = sysctl_ipfrag_max_dist;
299         unsigned int start, end;
300
301         int rc;
302
303         if (!peer || !max)
304                 return 0;
305
306         start = qp->rid;
307         end = atomic_inc_return(&peer->rid);
308         qp->rid = end;
309
310         rc = qp->q.fragments && (end - start) > max;
311
312         if (rc) {
313                 struct net *net;
314
315                 net = container_of(qp->q.net, struct net, ipv4.frags);
316                 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
317         }
318
319         return rc;
320 }
321
322 static int ip_frag_reinit(struct ipq *qp)
323 {
324         struct sk_buff *fp;
325         unsigned int sum_truesize = 0;
326
327         if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
328                 atomic_inc(&qp->q.refcnt);
329                 return -ETIMEDOUT;
330         }
331
332         fp = qp->q.fragments;
333         do {
334                 struct sk_buff *xp = fp->next;
335
336                 sum_truesize += fp->truesize;
337                 kfree_skb(fp);
338                 fp = xp;
339         } while (fp);
340         sub_frag_mem_limit(qp->q.net, sum_truesize);
341
342         qp_flags(qp) = 0;
343         qp->q.len = 0;
344         qp->q.meat = 0;
345         qp->q.fragments = NULL;
346         qp->q.fragments_tail = NULL;
347         qp->iif = 0;
348         qp->ecn = 0;
349
350         return 0;
351 }
352
353 /* Add new segment to existing queue. */
354 static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
355 {
356         struct sk_buff *prev, *next;
357         struct net_device *dev;
358         unsigned int fragsize;
359         int flags, offset;
360         int ihl, end;
361         int err = -ENOENT;
362         u8 ecn;
363
364         if (qp_flags(qp) & INET_FRAG_COMPLETE)
365                 goto err;
366
367         if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
368             unlikely(ip_frag_too_far(qp)) &&
369             unlikely(err = ip_frag_reinit(qp))) {
370                 ipq_kill(qp);
371                 goto err;
372         }
373
374         ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
375         offset = ntohs(ip_hdr(skb)->frag_off);
376         flags = offset & ~IP_OFFSET;
377         offset &= IP_OFFSET;
378         offset <<= 3;           /* offset is in 8-byte chunks */
379         ihl = ip_hdrlen(skb);
380
381         /* Determine the position of this fragment. */
382         end = offset + skb->len - skb_network_offset(skb) - ihl;
383         err = -EINVAL;
384
385         /* Is this the final fragment? */
386         if ((flags & IP_MF) == 0) {
387                 /* If we already have some bits beyond end
388                  * or have different end, the segment is corrupted.
389                  */
390                 if (end < qp->q.len ||
391                     ((qp_flags(qp) & INET_FRAG_LAST_IN) && end != qp->q.len))
392                         goto err;
393                 qp_flags(qp) |= INET_FRAG_LAST_IN;
394                 qp->q.len = end;
395         } else {
396                 if (end&7) {
397                         end &= ~7;
398                         if (skb->ip_summed != CHECKSUM_UNNECESSARY)
399                                 skb->ip_summed = CHECKSUM_NONE;
400                 }
401                 if (end > qp->q.len) {
402                         /* Some bits beyond end -> corruption. */
403                         if (qp_flags(qp) & INET_FRAG_LAST_IN)
404                                 goto err;
405                         qp->q.len = end;
406                 }
407         }
408         if (end == offset)
409                 goto err;
410
411         err = -ENOMEM;
412         if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
413                 goto err;
414
415         err = pskb_trim_rcsum(skb, end - offset);
416         if (err)
417                 goto err;
418
419         /* Find out which fragments are in front and at the back of us
420          * in the chain of fragments so far.  We must know where to put
421          * this fragment, right?
422          */
423         prev = qp->q.fragments_tail;
424         if (!prev || FRAG_CB(prev)->offset < offset) {
425                 next = NULL;
426                 goto found;
427         }
428         prev = NULL;
429         for (next = qp->q.fragments; next != NULL; next = next->next) {
430                 if (FRAG_CB(next)->offset >= offset)
431                         break;  /* bingo! */
432                 prev = next;
433         }
434
435 found:
436         /* We found where to put this one.  Check for overlap with
437          * preceding fragment, and, if needed, align things so that
438          * any overlaps are eliminated.
439          */
440         if (prev) {
441                 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
442
443                 if (i > 0) {
444                         offset += i;
445                         err = -EINVAL;
446                         if (end <= offset)
447                                 goto err;
448                         err = -ENOMEM;
449                         if (!pskb_pull(skb, i))
450                                 goto err;
451                         if (skb->ip_summed != CHECKSUM_UNNECESSARY)
452                                 skb->ip_summed = CHECKSUM_NONE;
453                 }
454         }
455
456         err = -ENOMEM;
457
458         while (next && FRAG_CB(next)->offset < end) {
459                 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
460
461                 if (i < next->len) {
462                         /* Eat head of the next overlapped fragment
463                          * and leave the loop. The next ones cannot overlap.
464                          */
465                         if (!pskb_pull(next, i))
466                                 goto err;
467                         FRAG_CB(next)->offset += i;
468                         qp->q.meat -= i;
469                         if (next->ip_summed != CHECKSUM_UNNECESSARY)
470                                 next->ip_summed = CHECKSUM_NONE;
471                         break;
472                 } else {
473                         struct sk_buff *free_it = next;
474
475                         /* Old fragment is completely overridden with
476                          * new one drop it.
477                          */
478                         next = next->next;
479
480                         if (prev)
481                                 prev->next = next;
482                         else
483                                 qp->q.fragments = next;
484
485                         qp->q.meat -= free_it->len;
486                         sub_frag_mem_limit(qp->q.net, free_it->truesize);
487                         kfree_skb(free_it);
488                 }
489         }
490
491         FRAG_CB(skb)->offset = offset;
492
493         /* Insert this fragment in the chain of fragments. */
494         skb->next = next;
495         if (!next)
496                 qp->q.fragments_tail = skb;
497         if (prev)
498                 prev->next = skb;
499         else
500                 qp->q.fragments = skb;
501
502         dev = skb->dev;
503         if (dev) {
504                 qp->iif = dev->ifindex;
505                 skb->dev = NULL;
506         }
507         qp->q.stamp = skb->tstamp;
508         qp->q.meat += skb->len;
509         qp->ecn |= ecn;
510         add_frag_mem_limit(qp->q.net, skb->truesize);
511         if (offset == 0)
512                 qp_flags(qp) |= INET_FRAG_FIRST_IN;
513
514         fragsize = skb->len + ihl;
515
516         if (fragsize > qp->q.max_size)
517                 qp->q.max_size = fragsize;
518
519         if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
520             fragsize > qp->max_df_size)
521                 qp->max_df_size = fragsize;
522
523         if (qp_flags(qp) == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
524             qp->q.meat == qp->q.len) {
525                 unsigned long orefdst = skb->_skb_refdst;
526
527                 skb->_skb_refdst = 0UL;
528                 err = ip_frag_reasm(qp, prev, dev);
529                 skb->_skb_refdst = orefdst;
530                 return err;
531         }
532
533         skb_dst_drop(skb);
534         return -EINPROGRESS;
535
536 err:
537         kfree_skb(skb);
538         return err;
539 }
540
541
542 /* Build a new IP datagram from all its fragments. */
543
544 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
545                          struct net_device *dev)
546 {
547         struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
548         struct iphdr *iph;
549         struct sk_buff *fp, *head = qp->q.fragments;
550         int len;
551         int ihlen;
552         int err;
553         u8 ecn;
554
555         ipq_kill(qp);
556
557         ecn = ip_frag_ecn_table[qp->ecn];
558         if (unlikely(ecn == 0xff)) {
559                 err = -EINVAL;
560                 goto out_fail;
561         }
562         /* Make the one we just received the head. */
563         if (prev) {
564                 head = prev->next;
565                 fp = skb_clone(head, GFP_ATOMIC);
566                 if (!fp)
567                         goto out_nomem;
568
569                 fp->next = head->next;
570                 if (!fp->next)
571                         qp->q.fragments_tail = fp;
572                 prev->next = fp;
573
574                 skb_morph(head, qp->q.fragments);
575                 head->next = qp->q.fragments->next;
576
577                 consume_skb(qp->q.fragments);
578                 qp->q.fragments = head;
579         }
580
581         WARN_ON(!head);
582         WARN_ON(FRAG_CB(head)->offset != 0);
583
584         /* Allocate a new buffer for the datagram. */
585         ihlen = ip_hdrlen(head);
586         len = ihlen + qp->q.len;
587
588         err = -E2BIG;
589         if (len > 65535)
590                 goto out_oversize;
591
592         /* Head of list must not be cloned. */
593         if (skb_unclone(head, GFP_ATOMIC))
594                 goto out_nomem;
595
596         /* If the first fragment is fragmented itself, we split
597          * it to two chunks: the first with data and paged part
598          * and the second, holding only fragments. */
599         if (skb_has_frag_list(head)) {
600                 struct sk_buff *clone;
601                 int i, plen = 0;
602
603                 clone = alloc_skb(0, GFP_ATOMIC);
604                 if (!clone)
605                         goto out_nomem;
606                 clone->next = head->next;
607                 head->next = clone;
608                 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
609                 skb_frag_list_init(head);
610                 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
611                         plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
612                 clone->len = clone->data_len = head->data_len - plen;
613                 head->data_len -= clone->len;
614                 head->len -= clone->len;
615                 clone->csum = 0;
616                 clone->ip_summed = head->ip_summed;
617                 add_frag_mem_limit(qp->q.net, clone->truesize);
618         }
619
620         skb_shinfo(head)->frag_list = head->next;
621         skb_push(head, head->data - skb_network_header(head));
622
623         for (fp=head->next; fp; fp = fp->next) {
624                 head->data_len += fp->len;
625                 head->len += fp->len;
626                 if (head->ip_summed != fp->ip_summed)
627                         head->ip_summed = CHECKSUM_NONE;
628                 else if (head->ip_summed == CHECKSUM_COMPLETE)
629                         head->csum = csum_add(head->csum, fp->csum);
630                 head->truesize += fp->truesize;
631         }
632         sub_frag_mem_limit(qp->q.net, head->truesize);
633
634         head->next = NULL;
635         head->dev = dev;
636         head->tstamp = qp->q.stamp;
637         IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
638
639         iph = ip_hdr(head);
640         iph->tot_len = htons(len);
641         iph->tos |= ecn;
642
643         /* When we set IP_DF on a refragmented skb we must also force a
644          * call to ip_fragment to avoid forwarding a DF-skb of size s while
645          * original sender only sent fragments of size f (where f < s).
646          *
647          * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
648          * frag seen to avoid sending tiny DF-fragments in case skb was built
649          * from one very small df-fragment and one large non-df frag.
650          */
651         if (qp->max_df_size == qp->q.max_size) {
652                 IPCB(head)->flags |= IPSKB_FRAG_PMTU;
653                 iph->frag_off = htons(IP_DF);
654         } else {
655                 iph->frag_off = 0;
656         }
657
658         ip_send_check(iph);
659
660         IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS);
661         qp->q.fragments = NULL;
662         qp->q.fragments_tail = NULL;
663         return 0;
664
665 out_nomem:
666         net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
667         err = -ENOMEM;
668         goto out_fail;
669 out_oversize:
670         net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->saddr);
671 out_fail:
672         IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
673         return err;
674 }
675
676 /* Process an incoming IP datagram fragment. */
677 int rpl_ip_defrag(struct sk_buff *skb, u32 user)
678 {
679         struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
680         int vif = vrf_master_ifindex_rcu(dev);
681         struct net *net = dev_net(dev);
682         struct ipq *qp;
683
684         IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS);
685         skb_orphan(skb);
686
687         /* Lookup (or create) queue header */
688         qp = ip_find(net, ip_hdr(skb), user, vif);
689         if (qp) {
690                 int ret;
691
692                 spin_lock(&qp->q.lock);
693
694                 ret = ip_frag_queue(qp, skb);
695
696                 spin_unlock(&qp->q.lock);
697                 ipq_put(qp);
698                 return ret;
699         }
700
701         IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
702         kfree_skb(skb);
703         return -ENOMEM;
704 }
705 EXPORT_SYMBOL_GPL(rpl_ip_defrag);
706
707 static int __net_init ipv4_frags_init_net(struct net *net)
708 {
709         nf_defrag_ipv4_enable();
710
711         return 0;
712 }
713
714 static void __net_exit ipv4_frags_exit_net(struct net *net)
715 {
716         inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
717 }
718
719 static struct pernet_operations ip4_frags_ops = {
720         .init = ipv4_frags_init_net,
721         .exit = ipv4_frags_exit_net,
722 };
723
724 int __init rpl_ipfrag_init(void)
725 {
726         register_pernet_subsys(&ip4_frags_ops);
727         ip4_frags.hashfn = ip4_hashfn;
728         ip4_frags.constructor = ip4_frag_init;
729         ip4_frags.destructor = ip4_frag_free;
730         ip4_frags.skb_free = NULL;
731         ip4_frags.qsize = sizeof(struct ipq);
732         ip4_frags.match = ip4_frag_match;
733         ip4_frags.frag_expire = ip_expire;
734 #ifdef HAVE_INET_FRAGS_WITH_FRAGS_WORK
735         ip4_frags.frags_cache_name = ip_frag_cache_name;
736 #endif
737         if (inet_frags_init(&ip4_frags)) {
738                 pr_warn("IP: failed to allocate ip4_frags cache\n");
739                 return -ENOMEM;
740         }
741         return 0;
742 }
743
744 void rpl_ipfrag_fini(void)
745 {
746         inet_frags_fini(&ip4_frags);
747         unregister_pernet_subsys(&ip4_frags_ops);
748 }
749
750 #endif /* OVS_FRAGMENT_BACKPORT */