reassembly.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*
  2. * IPv6 fragment reassembly
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * Based on: net/ipv4/ip_fragment.c
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. /*
  16. * Fixes:
  17. * Andi Kleen Make it work with multiple hosts.
  18. * More RFC compliance.
  19. *
  20. * Horst von Brand Add missing #include <linux/string.h>
  21. * Alexey Kuznetsov SMP races, threading, cleanup.
  22. * Patrick McHardy LRU queue of frag heads for evictor.
  23. * Mitsuru KANDA @USAGI Register inet6_protocol{}.
  24. * David Stevens and
  25. * YOSHIFUJI,H. @USAGI Always remove fragment header to
  26. * calculate ICV correctly.
  27. */
  28. #define pr_fmt(fmt) "IPv6: " fmt
  29. #include <linux/errno.h>
  30. #include <linux/types.h>
  31. #include <linux/string.h>
  32. #include <linux/socket.h>
  33. #include <linux/sockios.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/net.h>
  36. #include <linux/list.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/in6.h>
  39. #include <linux/ipv6.h>
  40. #include <linux/icmpv6.h>
  41. #include <linux/random.h>
  42. #include <linux/jhash.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/slab.h>
  45. #include <linux/export.h>
  46. #include <net/sock.h>
  47. #include <net/snmp.h>
  48. #include <net/ipv6.h>
  49. #include <net/ip6_route.h>
  50. #include <net/protocol.h>
  51. #include <net/transp_v6.h>
  52. #include <net/rawv6.h>
  53. #include <net/ndisc.h>
  54. #include <net/addrconf.h>
  55. #include <net/ipv6_frag.h>
  56. #include <net/inet_ecn.h>
  57. static const char ip6_frag_cache_name[] = "ip6-frags";
  58. static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
  59. {
  60. return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
  61. }
  62. static struct inet_frags ip6_frags;
  63. static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
  64. struct net_device *dev);
  65. static void ip6_frag_expire(struct timer_list *t)
  66. {
  67. struct inet_frag_queue *frag = from_timer(frag, t, timer);
  68. struct frag_queue *fq;
  69. struct net *net;
  70. fq = container_of(frag, struct frag_queue, q);
  71. net = container_of(fq->q.net, struct net, ipv6.frags);
  72. ip6frag_expire_frag_queue(net, fq);
  73. }
  74. static struct frag_queue *
  75. fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif)
  76. {
  77. struct frag_v6_compare_key key = {
  78. .id = id,
  79. .saddr = hdr->saddr,
  80. .daddr = hdr->daddr,
  81. .user = IP6_DEFRAG_LOCAL_DELIVER,
  82. .iif = iif,
  83. };
  84. struct inet_frag_queue *q;
  85. if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST |
  86. IPV6_ADDR_LINKLOCAL)))
  87. key.iif = 0;
  88. q = inet_frag_find(&net->ipv6.frags, &key);
  89. if (!q)
  90. return NULL;
  91. return container_of(q, struct frag_queue, q);
  92. }
  93. static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
  94. struct frag_hdr *fhdr, int nhoff,
  95. u32 *prob_offset)
  96. {
  97. struct sk_buff *prev, *next;
  98. struct net_device *dev;
  99. int offset, end, fragsize;
  100. struct net *net = dev_net(skb_dst(skb)->dev);
  101. u8 ecn;
  102. if (fq->q.flags & INET_FRAG_COMPLETE)
  103. goto err;
  104. offset = ntohs(fhdr->frag_off) & ~0x7;
  105. end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
  106. ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
  107. if ((unsigned int)end > IPV6_MAXPLEN) {
  108. *prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb);
  109. return -1;
  110. }
  111. ecn = ip6_frag_ecn(ipv6_hdr(skb));
  112. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  113. const unsigned char *nh = skb_network_header(skb);
  114. skb->csum = csum_sub(skb->csum,
  115. csum_partial(nh, (u8 *)(fhdr + 1) - nh,
  116. 0));
  117. }
  118. /* Is this the final fragment? */
  119. if (!(fhdr->frag_off & htons(IP6_MF))) {
  120. /* If we already have some bits beyond end
  121. * or have different end, the segment is corrupted.
  122. */
  123. if (end < fq->q.len ||
  124. ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
  125. goto discard_fq;
  126. fq->q.flags |= INET_FRAG_LAST_IN;
  127. fq->q.len = end;
  128. } else {
  129. /* Check if the fragment is rounded to 8 bytes.
  130. * Required by the RFC.
  131. */
  132. if (end & 0x7) {
  133. /* RFC2460 says always send parameter problem in
  134. * this case. -DaveM
  135. */
  136. *prob_offset = offsetof(struct ipv6hdr, payload_len);
  137. return -1;
  138. }
  139. if (end > fq->q.len) {
  140. /* Some bits beyond end -> corruption. */
  141. if (fq->q.flags & INET_FRAG_LAST_IN)
  142. goto discard_fq;
  143. fq->q.len = end;
  144. }
  145. }
  146. if (end == offset)
  147. goto discard_fq;
  148. /* Point into the IP datagram 'data' part. */
  149. if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
  150. goto discard_fq;
  151. if (pskb_trim_rcsum(skb, end - offset))
  152. goto discard_fq;
  153. /* Find out which fragments are in front and at the back of us
  154. * in the chain of fragments so far. We must know where to put
  155. * this fragment, right?
  156. */
  157. prev = fq->q.fragments_tail;
  158. if (!prev || prev->ip_defrag_offset < offset) {
  159. next = NULL;
  160. goto found;
  161. }
  162. prev = NULL;
  163. for (next = fq->q.fragments; next != NULL; next = next->next) {
  164. if (next->ip_defrag_offset >= offset)
  165. break; /* bingo! */
  166. prev = next;
  167. }
  168. found:
  169. /* RFC5722, Section 4, amended by Errata ID : 3089
  170. * When reassembling an IPv6 datagram, if
  171. * one or more its constituent fragments is determined to be an
  172. * overlapping fragment, the entire datagram (and any constituent
  173. * fragments) MUST be silently discarded.
  174. */
  175. /* Check for overlap with preceding fragment. */
  176. if (prev &&
  177. (prev->ip_defrag_offset + prev->len) > offset)
  178. goto discard_fq;
  179. /* Look for overlap with succeeding segment. */
  180. if (next && next->ip_defrag_offset < end)
  181. goto discard_fq;
  182. /* Note : skb->ip_defrag_offset and skb->dev share the same location */
  183. dev = skb->dev;
  184. if (dev)
  185. fq->iif = dev->ifindex;
  186. /* Makes sure compiler wont do silly aliasing games */
  187. barrier();
  188. skb->ip_defrag_offset = offset;
  189. /* Insert this fragment in the chain of fragments. */
  190. skb->next = next;
  191. if (!next)
  192. fq->q.fragments_tail = skb;
  193. if (prev)
  194. prev->next = skb;
  195. else
  196. fq->q.fragments = skb;
  197. fq->q.stamp = skb->tstamp;
  198. fq->q.meat += skb->len;
  199. fq->ecn |= ecn;
  200. add_frag_mem_limit(fq->q.net, skb->truesize);
  201. fragsize = -skb_network_offset(skb) + skb->len;
  202. if (fragsize > fq->q.max_size)
  203. fq->q.max_size = fragsize;
  204. /* The first fragment.
  205. * nhoffset is obtained from the first fragment, of course.
  206. */
  207. if (offset == 0) {
  208. fq->nhoffset = nhoff;
  209. fq->q.flags |= INET_FRAG_FIRST_IN;
  210. }
  211. if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
  212. fq->q.meat == fq->q.len) {
  213. int res;
  214. unsigned long orefdst = skb->_skb_refdst;
  215. skb->_skb_refdst = 0UL;
  216. res = ip6_frag_reasm(fq, prev, dev);
  217. skb->_skb_refdst = orefdst;
  218. return res;
  219. }
  220. skb_dst_drop(skb);
  221. return -1;
  222. discard_fq:
  223. inet_frag_kill(&fq->q);
  224. err:
  225. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  226. IPSTATS_MIB_REASMFAILS);
  227. kfree_skb(skb);
  228. return -1;
  229. }
  230. /*
  231. * Check if this packet is complete.
  232. * Returns NULL on failure by any reason, and pointer
  233. * to current nexthdr field in reassembled frame.
  234. *
  235. * It is called with locked fq, and caller must check that
  236. * queue is eligible for reassembly i.e. it is not COMPLETE,
  237. * the last and the first frames arrived and all the bits are here.
  238. */
  239. static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
  240. struct net_device *dev)
  241. {
  242. struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
  243. struct sk_buff *fp, *head = fq->q.fragments;
  244. int payload_len, delta;
  245. unsigned int nhoff;
  246. int sum_truesize;
  247. u8 ecn;
  248. inet_frag_kill(&fq->q);
  249. ecn = ip_frag_ecn_table[fq->ecn];
  250. if (unlikely(ecn == 0xff))
  251. goto out_fail;
  252. /* Make the one we just received the head. */
  253. if (prev) {
  254. head = prev->next;
  255. fp = skb_clone(head, GFP_ATOMIC);
  256. if (!fp)
  257. goto out_oom;
  258. fp->next = head->next;
  259. if (!fp->next)
  260. fq->q.fragments_tail = fp;
  261. prev->next = fp;
  262. skb_morph(head, fq->q.fragments);
  263. head->next = fq->q.fragments->next;
  264. consume_skb(fq->q.fragments);
  265. fq->q.fragments = head;
  266. }
  267. WARN_ON(head == NULL);
  268. WARN_ON(head->ip_defrag_offset != 0);
  269. /* Unfragmented part is taken from the first segment. */
  270. payload_len = ((head->data - skb_network_header(head)) -
  271. sizeof(struct ipv6hdr) + fq->q.len -
  272. sizeof(struct frag_hdr));
  273. if (payload_len > IPV6_MAXPLEN)
  274. goto out_oversize;
  275. delta = - head->truesize;
  276. /* Head of list must not be cloned. */
  277. if (skb_unclone(head, GFP_ATOMIC))
  278. goto out_oom;
  279. delta += head->truesize;
  280. if (delta)
  281. add_frag_mem_limit(fq->q.net, delta);
  282. /* If the first fragment is fragmented itself, we split
  283. * it to two chunks: the first with data and paged part
  284. * and the second, holding only fragments. */
  285. if (skb_has_frag_list(head)) {
  286. struct sk_buff *clone;
  287. int i, plen = 0;
  288. clone = alloc_skb(0, GFP_ATOMIC);
  289. if (!clone)
  290. goto out_oom;
  291. clone->next = head->next;
  292. head->next = clone;
  293. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  294. skb_frag_list_init(head);
  295. for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
  296. plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
  297. clone->len = clone->data_len = head->data_len - plen;
  298. head->data_len -= clone->len;
  299. head->len -= clone->len;
  300. clone->csum = 0;
  301. clone->ip_summed = head->ip_summed;
  302. add_frag_mem_limit(fq->q.net, clone->truesize);
  303. }
  304. /* We have to remove fragment header from datagram and to relocate
  305. * header in order to calculate ICV correctly. */
  306. nhoff = fq->nhoffset;
  307. skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
  308. memmove(head->head + sizeof(struct frag_hdr), head->head,
  309. (head->data - head->head) - sizeof(struct frag_hdr));
  310. if (skb_mac_header_was_set(head))
  311. head->mac_header += sizeof(struct frag_hdr);
  312. head->network_header += sizeof(struct frag_hdr);
  313. skb_reset_transport_header(head);
  314. skb_push(head, head->data - skb_network_header(head));
  315. sum_truesize = head->truesize;
  316. for (fp = head->next; fp;) {
  317. bool headstolen;
  318. int delta;
  319. struct sk_buff *next = fp->next;
  320. sum_truesize += fp->truesize;
  321. if (head->ip_summed != fp->ip_summed)
  322. head->ip_summed = CHECKSUM_NONE;
  323. else if (head->ip_summed == CHECKSUM_COMPLETE)
  324. head->csum = csum_add(head->csum, fp->csum);
  325. if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
  326. kfree_skb_partial(fp, headstolen);
  327. } else {
  328. if (!skb_shinfo(head)->frag_list)
  329. skb_shinfo(head)->frag_list = fp;
  330. head->data_len += fp->len;
  331. head->len += fp->len;
  332. head->truesize += fp->truesize;
  333. }
  334. fp = next;
  335. }
  336. sub_frag_mem_limit(fq->q.net, sum_truesize);
  337. skb_mark_not_on_list(head);
  338. head->dev = dev;
  339. head->tstamp = fq->q.stamp;
  340. ipv6_hdr(head)->payload_len = htons(payload_len);
  341. ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
  342. IP6CB(head)->nhoff = nhoff;
  343. IP6CB(head)->flags |= IP6SKB_FRAGMENTED;
  344. IP6CB(head)->frag_max_size = fq->q.max_size;
  345. /* Yes, and fold redundant checksum back. 8) */
  346. skb_postpush_rcsum(head, skb_network_header(head),
  347. skb_network_header_len(head));
  348. rcu_read_lock();
  349. __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
  350. rcu_read_unlock();
  351. fq->q.fragments = NULL;
  352. fq->q.rb_fragments = RB_ROOT;
  353. fq->q.fragments_tail = NULL;
  354. return 1;
  355. out_oversize:
  356. net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
  357. goto out_fail;
  358. out_oom:
  359. net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
  360. out_fail:
  361. rcu_read_lock();
  362. __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
  363. rcu_read_unlock();
  364. inet_frag_kill(&fq->q);
  365. return -1;
  366. }
  367. static int ipv6_frag_rcv(struct sk_buff *skb)
  368. {
  369. struct frag_hdr *fhdr;
  370. struct frag_queue *fq;
  371. const struct ipv6hdr *hdr = ipv6_hdr(skb);
  372. struct net *net = dev_net(skb_dst(skb)->dev);
  373. int iif;
  374. if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
  375. goto fail_hdr;
  376. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
  377. /* Jumbo payload inhibits frag. header */
  378. if (hdr->payload_len == 0)
  379. goto fail_hdr;
  380. if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
  381. sizeof(struct frag_hdr))))
  382. goto fail_hdr;
  383. hdr = ipv6_hdr(skb);
  384. fhdr = (struct frag_hdr *)skb_transport_header(skb);
  385. if (!(fhdr->frag_off & htons(0xFFF9))) {
  386. /* It is not a fragmented frame */
  387. skb->transport_header += sizeof(struct frag_hdr);
  388. __IP6_INC_STATS(net,
  389. ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
  390. IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
  391. IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
  392. return 1;
  393. }
  394. if (skb->len - skb_network_offset(skb) < IPV6_MIN_MTU &&
  395. fhdr->frag_off & htons(IP6_MF))
  396. goto fail_hdr;
  397. iif = skb->dev ? skb->dev->ifindex : 0;
  398. fq = fq_find(net, fhdr->identification, hdr, iif);
  399. if (fq) {
  400. u32 prob_offset = 0;
  401. int ret;
  402. spin_lock(&fq->q.lock);
  403. fq->iif = iif;
  404. ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff,
  405. &prob_offset);
  406. spin_unlock(&fq->q.lock);
  407. inet_frag_put(&fq->q);
  408. if (prob_offset) {
  409. __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
  410. IPSTATS_MIB_INHDRERRORS);
  411. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset);
  412. }
  413. return ret;
  414. }
  415. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
  416. kfree_skb(skb);
  417. return -1;
  418. fail_hdr:
  419. __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
  420. IPSTATS_MIB_INHDRERRORS);
  421. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
  422. return -1;
  423. }
  424. static const struct inet6_protocol frag_protocol = {
  425. .handler = ipv6_frag_rcv,
  426. .flags = INET6_PROTO_NOPOLICY,
  427. };
  428. #ifdef CONFIG_SYSCTL
  429. static struct ctl_table ip6_frags_ns_ctl_table[] = {
  430. {
  431. .procname = "ip6frag_high_thresh",
  432. .data = &init_net.ipv6.frags.high_thresh,
  433. .maxlen = sizeof(unsigned long),
  434. .mode = 0644,
  435. .proc_handler = proc_doulongvec_minmax,
  436. .extra1 = &init_net.ipv6.frags.low_thresh
  437. },
  438. {
  439. .procname = "ip6frag_low_thresh",
  440. .data = &init_net.ipv6.frags.low_thresh,
  441. .maxlen = sizeof(unsigned long),
  442. .mode = 0644,
  443. .proc_handler = proc_doulongvec_minmax,
  444. .extra2 = &init_net.ipv6.frags.high_thresh
  445. },
  446. {
  447. .procname = "ip6frag_time",
  448. .data = &init_net.ipv6.frags.timeout,
  449. .maxlen = sizeof(int),
  450. .mode = 0644,
  451. .proc_handler = proc_dointvec_jiffies,
  452. },
  453. { }
  454. };
  455. /* secret interval has been deprecated */
  456. static int ip6_frags_secret_interval_unused;
  457. static struct ctl_table ip6_frags_ctl_table[] = {
  458. {
  459. .procname = "ip6frag_secret_interval",
  460. .data = &ip6_frags_secret_interval_unused,
  461. .maxlen = sizeof(int),
  462. .mode = 0644,
  463. .proc_handler = proc_dointvec_jiffies,
  464. },
  465. { }
  466. };
  467. static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
  468. {
  469. struct ctl_table *table;
  470. struct ctl_table_header *hdr;
  471. table = ip6_frags_ns_ctl_table;
  472. if (!net_eq(net, &init_net)) {
  473. table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
  474. if (!table)
  475. goto err_alloc;
  476. table[0].data = &net->ipv6.frags.high_thresh;
  477. table[0].extra1 = &net->ipv6.frags.low_thresh;
  478. table[1].data = &net->ipv6.frags.low_thresh;
  479. table[1].extra2 = &net->ipv6.frags.high_thresh;
  480. table[2].data = &net->ipv6.frags.timeout;
  481. }
  482. hdr = register_net_sysctl(net, "net/ipv6", table);
  483. if (!hdr)
  484. goto err_reg;
  485. net->ipv6.sysctl.frags_hdr = hdr;
  486. return 0;
  487. err_reg:
  488. if (!net_eq(net, &init_net))
  489. kfree(table);
  490. err_alloc:
  491. return -ENOMEM;
  492. }
  493. static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
  494. {
  495. struct ctl_table *table;
  496. table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
  497. unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
  498. if (!net_eq(net, &init_net))
  499. kfree(table);
  500. }
  501. static struct ctl_table_header *ip6_ctl_header;
  502. static int ip6_frags_sysctl_register(void)
  503. {
  504. ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
  505. ip6_frags_ctl_table);
  506. return ip6_ctl_header == NULL ? -ENOMEM : 0;
  507. }
  508. static void ip6_frags_sysctl_unregister(void)
  509. {
  510. unregister_net_sysctl_table(ip6_ctl_header);
  511. }
  512. #else
  513. static int ip6_frags_ns_sysctl_register(struct net *net)
  514. {
  515. return 0;
  516. }
  517. static void ip6_frags_ns_sysctl_unregister(struct net *net)
  518. {
  519. }
  520. static int ip6_frags_sysctl_register(void)
  521. {
  522. return 0;
  523. }
  524. static void ip6_frags_sysctl_unregister(void)
  525. {
  526. }
  527. #endif
  528. static int __net_init ipv6_frags_init_net(struct net *net)
  529. {
  530. int res;
  531. net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
  532. net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
  533. net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
  534. net->ipv6.frags.f = &ip6_frags;
  535. res = inet_frags_init_net(&net->ipv6.frags);
  536. if (res < 0)
  537. return res;
  538. res = ip6_frags_ns_sysctl_register(net);
  539. if (res < 0)
  540. inet_frags_exit_net(&net->ipv6.frags);
  541. return res;
  542. }
  543. static void __net_exit ipv6_frags_exit_net(struct net *net)
  544. {
  545. ip6_frags_ns_sysctl_unregister(net);
  546. inet_frags_exit_net(&net->ipv6.frags);
  547. }
  548. static struct pernet_operations ip6_frags_ops = {
  549. .init = ipv6_frags_init_net,
  550. .exit = ipv6_frags_exit_net,
  551. };
  552. static const struct rhashtable_params ip6_rhash_params = {
  553. .head_offset = offsetof(struct inet_frag_queue, node),
  554. .hashfn = ip6frag_key_hashfn,
  555. .obj_hashfn = ip6frag_obj_hashfn,
  556. .obj_cmpfn = ip6frag_obj_cmpfn,
  557. .automatic_shrinking = true,
  558. };
  559. int __init ipv6_frag_init(void)
  560. {
  561. int ret;
  562. ip6_frags.constructor = ip6frag_init;
  563. ip6_frags.destructor = NULL;
  564. ip6_frags.qsize = sizeof(struct frag_queue);
  565. ip6_frags.frag_expire = ip6_frag_expire;
  566. ip6_frags.frags_cache_name = ip6_frag_cache_name;
  567. ip6_frags.rhash_params = ip6_rhash_params;
  568. ret = inet_frags_init(&ip6_frags);
  569. if (ret)
  570. goto out;
  571. ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
  572. if (ret)
  573. goto err_protocol;
  574. ret = ip6_frags_sysctl_register();
  575. if (ret)
  576. goto err_sysctl;
  577. ret = register_pernet_subsys(&ip6_frags_ops);
  578. if (ret)
  579. goto err_pernet;
  580. out:
  581. return ret;
  582. err_pernet:
  583. ip6_frags_sysctl_unregister();
  584. err_sysctl:
  585. inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
  586. err_protocol:
  587. inet_frags_fini(&ip6_frags);
  588. goto out;
  589. }
  590. void ipv6_frag_exit(void)
  591. {
  592. inet_frags_fini(&ip6_frags);
  593. ip6_frags_sysctl_unregister();
  594. unregister_pernet_subsys(&ip6_frags_ops);
  595. inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
  596. }