reassembly.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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/inet_frag.h>
  56. struct ip6frag_skb_cb
  57. {
  58. struct inet6_skb_parm h;
  59. int offset;
  60. };
  61. #define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb))
  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. /*
  66. * callers should be careful not to use the hash value outside the ipfrag_lock
  67. * as doing so could race with ipfrag_hash_rnd being recalculated.
  68. */
  69. unsigned int inet6_hash_frag(__be32 id, const struct in6_addr *saddr,
  70. const struct in6_addr *daddr, u32 rnd)
  71. {
  72. u32 c;
  73. c = jhash_3words(ipv6_addr_hash(saddr), ipv6_addr_hash(daddr),
  74. (__force u32)id, rnd);
  75. return c & (INETFRAGS_HASHSZ - 1);
  76. }
  77. EXPORT_SYMBOL_GPL(inet6_hash_frag);
  78. static unsigned int ip6_hashfn(struct inet_frag_queue *q)
  79. {
  80. struct frag_queue *fq;
  81. fq = container_of(q, struct frag_queue, q);
  82. return inet6_hash_frag(fq->id, &fq->saddr, &fq->daddr, ip6_frags.rnd);
  83. }
  84. bool ip6_frag_match(struct inet_frag_queue *q, void *a)
  85. {
  86. struct frag_queue *fq;
  87. struct ip6_create_arg *arg = a;
  88. fq = container_of(q, struct frag_queue, q);
  89. return fq->id == arg->id &&
  90. fq->user == arg->user &&
  91. ipv6_addr_equal(&fq->saddr, arg->src) &&
  92. ipv6_addr_equal(&fq->daddr, arg->dst);
  93. }
  94. EXPORT_SYMBOL(ip6_frag_match);
  95. void ip6_frag_init(struct inet_frag_queue *q, void *a)
  96. {
  97. struct frag_queue *fq = container_of(q, struct frag_queue, q);
  98. struct ip6_create_arg *arg = a;
  99. fq->id = arg->id;
  100. fq->user = arg->user;
  101. fq->saddr = *arg->src;
  102. fq->daddr = *arg->dst;
  103. }
  104. EXPORT_SYMBOL(ip6_frag_init);
  105. void ip6_expire_frag_queue(struct net *net, struct frag_queue *fq,
  106. struct inet_frags *frags)
  107. {
  108. struct net_device *dev = NULL;
  109. spin_lock(&fq->q.lock);
  110. if (fq->q.last_in & INET_FRAG_COMPLETE)
  111. goto out;
  112. inet_frag_kill(&fq->q, frags);
  113. rcu_read_lock();
  114. dev = dev_get_by_index_rcu(net, fq->iif);
  115. if (!dev)
  116. goto out_rcu_unlock;
  117. IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
  118. IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
  119. /* Don't send error if the first segment did not arrive. */
  120. if (!(fq->q.last_in & INET_FRAG_FIRST_IN) || !fq->q.fragments)
  121. goto out_rcu_unlock;
  122. /*
  123. But use as source device on which LAST ARRIVED
  124. segment was received. And do not use fq->dev
  125. pointer directly, device might already disappeared.
  126. */
  127. fq->q.fragments->dev = dev;
  128. icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0);
  129. out_rcu_unlock:
  130. rcu_read_unlock();
  131. out:
  132. spin_unlock(&fq->q.lock);
  133. inet_frag_put(&fq->q, frags);
  134. }
  135. EXPORT_SYMBOL(ip6_expire_frag_queue);
  136. static void ip6_frag_expire(unsigned long data)
  137. {
  138. struct frag_queue *fq;
  139. struct net *net;
  140. fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
  141. net = container_of(fq->q.net, struct net, ipv6.frags);
  142. ip6_expire_frag_queue(net, fq, &ip6_frags);
  143. }
  144. static __inline__ struct frag_queue *
  145. fq_find(struct net *net, __be32 id, const struct in6_addr *src, const struct in6_addr *dst)
  146. {
  147. struct inet_frag_queue *q;
  148. struct ip6_create_arg arg;
  149. unsigned int hash;
  150. arg.id = id;
  151. arg.user = IP6_DEFRAG_LOCAL_DELIVER;
  152. arg.src = src;
  153. arg.dst = dst;
  154. read_lock(&ip6_frags.lock);
  155. hash = inet6_hash_frag(id, src, dst, ip6_frags.rnd);
  156. q = inet_frag_find(&net->ipv6.frags, &ip6_frags, &arg, hash);
  157. if (IS_ERR_OR_NULL(q)) {
  158. inet_frag_maybe_warn_overflow(q, pr_fmt());
  159. return NULL;
  160. }
  161. return container_of(q, struct frag_queue, q);
  162. }
  163. static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
  164. struct frag_hdr *fhdr, int nhoff)
  165. {
  166. struct sk_buff *prev, *next;
  167. struct net_device *dev;
  168. int offset, end;
  169. struct net *net = dev_net(skb_dst(skb)->dev);
  170. if (fq->q.last_in & INET_FRAG_COMPLETE)
  171. goto err;
  172. offset = ntohs(fhdr->frag_off) & ~0x7;
  173. end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
  174. ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
  175. if ((unsigned int)end > IPV6_MAXPLEN) {
  176. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  177. IPSTATS_MIB_INHDRERRORS);
  178. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  179. ((u8 *)&fhdr->frag_off -
  180. skb_network_header(skb)));
  181. return -1;
  182. }
  183. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  184. const unsigned char *nh = skb_network_header(skb);
  185. skb->csum = csum_sub(skb->csum,
  186. csum_partial(nh, (u8 *)(fhdr + 1) - nh,
  187. 0));
  188. }
  189. /* Is this the final fragment? */
  190. if (!(fhdr->frag_off & htons(IP6_MF))) {
  191. /* If we already have some bits beyond end
  192. * or have different end, the segment is corrupted.
  193. */
  194. if (end < fq->q.len ||
  195. ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len))
  196. goto err;
  197. fq->q.last_in |= INET_FRAG_LAST_IN;
  198. fq->q.len = end;
  199. } else {
  200. /* Check if the fragment is rounded to 8 bytes.
  201. * Required by the RFC.
  202. */
  203. if (end & 0x7) {
  204. /* RFC2460 says always send parameter problem in
  205. * this case. -DaveM
  206. */
  207. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  208. IPSTATS_MIB_INHDRERRORS);
  209. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  210. offsetof(struct ipv6hdr, payload_len));
  211. return -1;
  212. }
  213. if (end > fq->q.len) {
  214. /* Some bits beyond end -> corruption. */
  215. if (fq->q.last_in & INET_FRAG_LAST_IN)
  216. goto err;
  217. fq->q.len = end;
  218. }
  219. }
  220. if (end == offset)
  221. goto err;
  222. /* Point into the IP datagram 'data' part. */
  223. if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
  224. goto err;
  225. if (pskb_trim_rcsum(skb, end - offset))
  226. goto err;
  227. /* Find out which fragments are in front and at the back of us
  228. * in the chain of fragments so far. We must know where to put
  229. * this fragment, right?
  230. */
  231. prev = fq->q.fragments_tail;
  232. if (!prev || FRAG6_CB(prev)->offset < offset) {
  233. next = NULL;
  234. goto found;
  235. }
  236. prev = NULL;
  237. for(next = fq->q.fragments; next != NULL; next = next->next) {
  238. if (FRAG6_CB(next)->offset >= offset)
  239. break; /* bingo! */
  240. prev = next;
  241. }
  242. found:
  243. /* RFC5722, Section 4, amended by Errata ID : 3089
  244. * When reassembling an IPv6 datagram, if
  245. * one or more its constituent fragments is determined to be an
  246. * overlapping fragment, the entire datagram (and any constituent
  247. * fragments) MUST be silently discarded.
  248. */
  249. /* Check for overlap with preceding fragment. */
  250. if (prev &&
  251. (FRAG6_CB(prev)->offset + prev->len) > offset)
  252. goto discard_fq;
  253. /* Look for overlap with succeeding segment. */
  254. if (next && FRAG6_CB(next)->offset < end)
  255. goto discard_fq;
  256. FRAG6_CB(skb)->offset = offset;
  257. /* Insert this fragment in the chain of fragments. */
  258. skb->next = next;
  259. if (!next)
  260. fq->q.fragments_tail = skb;
  261. if (prev)
  262. prev->next = skb;
  263. else
  264. fq->q.fragments = skb;
  265. dev = skb->dev;
  266. if (dev) {
  267. fq->iif = dev->ifindex;
  268. skb->dev = NULL;
  269. }
  270. fq->q.stamp = skb->tstamp;
  271. fq->q.meat += skb->len;
  272. add_frag_mem_limit(&fq->q, skb->truesize);
  273. /* The first fragment.
  274. * nhoffset is obtained from the first fragment, of course.
  275. */
  276. if (offset == 0) {
  277. fq->nhoffset = nhoff;
  278. fq->q.last_in |= INET_FRAG_FIRST_IN;
  279. }
  280. if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
  281. fq->q.meat == fq->q.len) {
  282. int res;
  283. unsigned long orefdst = skb->_skb_refdst;
  284. skb->_skb_refdst = 0UL;
  285. res = ip6_frag_reasm(fq, prev, dev);
  286. skb->_skb_refdst = orefdst;
  287. return res;
  288. }
  289. skb_dst_drop(skb);
  290. inet_frag_lru_move(&fq->q);
  291. return -1;
  292. discard_fq:
  293. inet_frag_kill(&fq->q, &ip6_frags);
  294. err:
  295. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  296. IPSTATS_MIB_REASMFAILS);
  297. kfree_skb(skb);
  298. return -1;
  299. }
  300. /*
  301. * Check if this packet is complete.
  302. * Returns NULL on failure by any reason, and pointer
  303. * to current nexthdr field in reassembled frame.
  304. *
  305. * It is called with locked fq, and caller must check that
  306. * queue is eligible for reassembly i.e. it is not COMPLETE,
  307. * the last and the first frames arrived and all the bits are here.
  308. */
  309. static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
  310. struct net_device *dev)
  311. {
  312. struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
  313. struct sk_buff *fp, *head = fq->q.fragments;
  314. int payload_len;
  315. unsigned int nhoff;
  316. int sum_truesize;
  317. inet_frag_kill(&fq->q, &ip6_frags);
  318. /* Make the one we just received the head. */
  319. if (prev) {
  320. head = prev->next;
  321. fp = skb_clone(head, GFP_ATOMIC);
  322. if (!fp)
  323. goto out_oom;
  324. fp->next = head->next;
  325. if (!fp->next)
  326. fq->q.fragments_tail = fp;
  327. prev->next = fp;
  328. skb_morph(head, fq->q.fragments);
  329. head->next = fq->q.fragments->next;
  330. consume_skb(fq->q.fragments);
  331. fq->q.fragments = head;
  332. }
  333. WARN_ON(head == NULL);
  334. WARN_ON(FRAG6_CB(head)->offset != 0);
  335. /* Unfragmented part is taken from the first segment. */
  336. payload_len = ((head->data - skb_network_header(head)) -
  337. sizeof(struct ipv6hdr) + fq->q.len -
  338. sizeof(struct frag_hdr));
  339. if (payload_len > IPV6_MAXPLEN)
  340. goto out_oversize;
  341. /* Head of list must not be cloned. */
  342. if (skb_unclone(head, GFP_ATOMIC))
  343. goto out_oom;
  344. /* If the first fragment is fragmented itself, we split
  345. * it to two chunks: the first with data and paged part
  346. * and the second, holding only fragments. */
  347. if (skb_has_frag_list(head)) {
  348. struct sk_buff *clone;
  349. int i, plen = 0;
  350. if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
  351. goto out_oom;
  352. clone->next = head->next;
  353. head->next = clone;
  354. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  355. skb_frag_list_init(head);
  356. for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
  357. plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
  358. clone->len = clone->data_len = head->data_len - plen;
  359. head->data_len -= clone->len;
  360. head->len -= clone->len;
  361. clone->csum = 0;
  362. clone->ip_summed = head->ip_summed;
  363. add_frag_mem_limit(&fq->q, clone->truesize);
  364. }
  365. /* We have to remove fragment header from datagram and to relocate
  366. * header in order to calculate ICV correctly. */
  367. nhoff = fq->nhoffset;
  368. skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
  369. memmove(head->head + sizeof(struct frag_hdr), head->head,
  370. (head->data - head->head) - sizeof(struct frag_hdr));
  371. head->mac_header += sizeof(struct frag_hdr);
  372. head->network_header += sizeof(struct frag_hdr);
  373. skb_reset_transport_header(head);
  374. skb_push(head, head->data - skb_network_header(head));
  375. sum_truesize = head->truesize;
  376. for (fp = head->next; fp;) {
  377. bool headstolen;
  378. int delta;
  379. struct sk_buff *next = fp->next;
  380. sum_truesize += fp->truesize;
  381. if (head->ip_summed != fp->ip_summed)
  382. head->ip_summed = CHECKSUM_NONE;
  383. else if (head->ip_summed == CHECKSUM_COMPLETE)
  384. head->csum = csum_add(head->csum, fp->csum);
  385. if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
  386. kfree_skb_partial(fp, headstolen);
  387. } else {
  388. if (!skb_shinfo(head)->frag_list)
  389. skb_shinfo(head)->frag_list = fp;
  390. head->data_len += fp->len;
  391. head->len += fp->len;
  392. head->truesize += fp->truesize;
  393. }
  394. fp = next;
  395. }
  396. sub_frag_mem_limit(&fq->q, sum_truesize);
  397. head->next = NULL;
  398. head->dev = dev;
  399. head->tstamp = fq->q.stamp;
  400. ipv6_hdr(head)->payload_len = htons(payload_len);
  401. IP6CB(head)->nhoff = nhoff;
  402. /* Yes, and fold redundant checksum back. 8) */
  403. if (head->ip_summed == CHECKSUM_COMPLETE)
  404. head->csum = csum_partial(skb_network_header(head),
  405. skb_network_header_len(head),
  406. head->csum);
  407. rcu_read_lock();
  408. IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
  409. rcu_read_unlock();
  410. fq->q.fragments = NULL;
  411. fq->q.fragments_tail = NULL;
  412. return 1;
  413. out_oversize:
  414. net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
  415. goto out_fail;
  416. out_oom:
  417. net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
  418. out_fail:
  419. rcu_read_lock();
  420. IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
  421. rcu_read_unlock();
  422. return -1;
  423. }
  424. static int ipv6_frag_rcv(struct sk_buff *skb)
  425. {
  426. struct frag_hdr *fhdr;
  427. struct frag_queue *fq;
  428. const struct ipv6hdr *hdr = ipv6_hdr(skb);
  429. struct net *net = dev_net(skb_dst(skb)->dev);
  430. int evicted;
  431. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
  432. /* Jumbo payload inhibits frag. header */
  433. if (hdr->payload_len==0)
  434. goto fail_hdr;
  435. if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
  436. sizeof(struct frag_hdr))))
  437. goto fail_hdr;
  438. hdr = ipv6_hdr(skb);
  439. fhdr = (struct frag_hdr *)skb_transport_header(skb);
  440. if (!(fhdr->frag_off & htons(0xFFF9))) {
  441. /* It is not a fragmented frame */
  442. skb->transport_header += sizeof(struct frag_hdr);
  443. IP6_INC_STATS_BH(net,
  444. ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
  445. IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
  446. return 1;
  447. }
  448. evicted = inet_frag_evictor(&net->ipv6.frags, &ip6_frags, false);
  449. if (evicted)
  450. IP6_ADD_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  451. IPSTATS_MIB_REASMFAILS, evicted);
  452. fq = fq_find(net, fhdr->identification, &hdr->saddr, &hdr->daddr);
  453. if (fq != NULL) {
  454. int ret;
  455. spin_lock(&fq->q.lock);
  456. ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
  457. spin_unlock(&fq->q.lock);
  458. inet_frag_put(&fq->q, &ip6_frags);
  459. return ret;
  460. }
  461. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
  462. kfree_skb(skb);
  463. return -1;
  464. fail_hdr:
  465. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
  466. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
  467. return -1;
  468. }
  469. static const struct inet6_protocol frag_protocol =
  470. {
  471. .handler = ipv6_frag_rcv,
  472. .flags = INET6_PROTO_NOPOLICY,
  473. };
  474. #ifdef CONFIG_SYSCTL
  475. static struct ctl_table ip6_frags_ns_ctl_table[] = {
  476. {
  477. .procname = "ip6frag_high_thresh",
  478. .data = &init_net.ipv6.frags.high_thresh,
  479. .maxlen = sizeof(int),
  480. .mode = 0644,
  481. .proc_handler = proc_dointvec
  482. },
  483. {
  484. .procname = "ip6frag_low_thresh",
  485. .data = &init_net.ipv6.frags.low_thresh,
  486. .maxlen = sizeof(int),
  487. .mode = 0644,
  488. .proc_handler = proc_dointvec
  489. },
  490. {
  491. .procname = "ip6frag_time",
  492. .data = &init_net.ipv6.frags.timeout,
  493. .maxlen = sizeof(int),
  494. .mode = 0644,
  495. .proc_handler = proc_dointvec_jiffies,
  496. },
  497. { }
  498. };
  499. static struct ctl_table ip6_frags_ctl_table[] = {
  500. {
  501. .procname = "ip6frag_secret_interval",
  502. .data = &ip6_frags.secret_interval,
  503. .maxlen = sizeof(int),
  504. .mode = 0644,
  505. .proc_handler = proc_dointvec_jiffies,
  506. },
  507. { }
  508. };
  509. static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
  510. {
  511. struct ctl_table *table;
  512. struct ctl_table_header *hdr;
  513. table = ip6_frags_ns_ctl_table;
  514. if (!net_eq(net, &init_net)) {
  515. table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
  516. if (table == NULL)
  517. goto err_alloc;
  518. table[0].data = &net->ipv6.frags.high_thresh;
  519. table[1].data = &net->ipv6.frags.low_thresh;
  520. table[2].data = &net->ipv6.frags.timeout;
  521. /* Don't export sysctls to unprivileged users */
  522. if (net->user_ns != &init_user_ns)
  523. table[0].procname = NULL;
  524. }
  525. hdr = register_net_sysctl(net, "net/ipv6", table);
  526. if (hdr == NULL)
  527. goto err_reg;
  528. net->ipv6.sysctl.frags_hdr = hdr;
  529. return 0;
  530. err_reg:
  531. if (!net_eq(net, &init_net))
  532. kfree(table);
  533. err_alloc:
  534. return -ENOMEM;
  535. }
  536. static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
  537. {
  538. struct ctl_table *table;
  539. table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
  540. unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
  541. if (!net_eq(net, &init_net))
  542. kfree(table);
  543. }
  544. static struct ctl_table_header *ip6_ctl_header;
  545. static int ip6_frags_sysctl_register(void)
  546. {
  547. ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
  548. ip6_frags_ctl_table);
  549. return ip6_ctl_header == NULL ? -ENOMEM : 0;
  550. }
  551. static void ip6_frags_sysctl_unregister(void)
  552. {
  553. unregister_net_sysctl_table(ip6_ctl_header);
  554. }
  555. #else
  556. static inline int ip6_frags_ns_sysctl_register(struct net *net)
  557. {
  558. return 0;
  559. }
  560. static inline void ip6_frags_ns_sysctl_unregister(struct net *net)
  561. {
  562. }
  563. static inline int ip6_frags_sysctl_register(void)
  564. {
  565. return 0;
  566. }
  567. static inline void ip6_frags_sysctl_unregister(void)
  568. {
  569. }
  570. #endif
  571. static int __net_init ipv6_frags_init_net(struct net *net)
  572. {
  573. net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
  574. net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
  575. net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
  576. inet_frags_init_net(&net->ipv6.frags);
  577. return ip6_frags_ns_sysctl_register(net);
  578. }
  579. static void __net_exit ipv6_frags_exit_net(struct net *net)
  580. {
  581. ip6_frags_ns_sysctl_unregister(net);
  582. inet_frags_exit_net(&net->ipv6.frags, &ip6_frags);
  583. }
  584. static struct pernet_operations ip6_frags_ops = {
  585. .init = ipv6_frags_init_net,
  586. .exit = ipv6_frags_exit_net,
  587. };
  588. int __init ipv6_frag_init(void)
  589. {
  590. int ret;
  591. ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
  592. if (ret)
  593. goto out;
  594. ret = ip6_frags_sysctl_register();
  595. if (ret)
  596. goto err_sysctl;
  597. ret = register_pernet_subsys(&ip6_frags_ops);
  598. if (ret)
  599. goto err_pernet;
  600. ip6_frags.hashfn = ip6_hashfn;
  601. ip6_frags.constructor = ip6_frag_init;
  602. ip6_frags.destructor = NULL;
  603. ip6_frags.skb_free = NULL;
  604. ip6_frags.qsize = sizeof(struct frag_queue);
  605. ip6_frags.match = ip6_frag_match;
  606. ip6_frags.frag_expire = ip6_frag_expire;
  607. ip6_frags.secret_interval = 10 * 60 * HZ;
  608. inet_frags_init(&ip6_frags);
  609. out:
  610. return ret;
  611. err_pernet:
  612. ip6_frags_sysctl_unregister();
  613. err_sysctl:
  614. inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
  615. goto out;
  616. }
  617. void ipv6_frag_exit(void)
  618. {
  619. inet_frags_fini(&ip6_frags);
  620. ip6_frags_sysctl_unregister();
  621. unregister_pernet_subsys(&ip6_frags_ops);
  622. inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
  623. }