reassembly.c 19 KB

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