reassembly.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. return ip6_frag_reasm(fq, prev, dev);
  283. inet_frag_lru_move(&fq->q);
  284. return -1;
  285. discard_fq:
  286. inet_frag_kill(&fq->q, &ip6_frags);
  287. err:
  288. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  289. IPSTATS_MIB_REASMFAILS);
  290. kfree_skb(skb);
  291. return -1;
  292. }
  293. /*
  294. * Check if this packet is complete.
  295. * Returns NULL on failure by any reason, and pointer
  296. * to current nexthdr field in reassembled frame.
  297. *
  298. * It is called with locked fq, and caller must check that
  299. * queue is eligible for reassembly i.e. it is not COMPLETE,
  300. * the last and the first frames arrived and all the bits are here.
  301. */
  302. static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
  303. struct net_device *dev)
  304. {
  305. struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
  306. struct sk_buff *fp, *head = fq->q.fragments;
  307. int payload_len;
  308. unsigned int nhoff;
  309. int sum_truesize;
  310. inet_frag_kill(&fq->q, &ip6_frags);
  311. /* Make the one we just received the head. */
  312. if (prev) {
  313. head = prev->next;
  314. fp = skb_clone(head, GFP_ATOMIC);
  315. if (!fp)
  316. goto out_oom;
  317. fp->next = head->next;
  318. if (!fp->next)
  319. fq->q.fragments_tail = fp;
  320. prev->next = fp;
  321. skb_morph(head, fq->q.fragments);
  322. head->next = fq->q.fragments->next;
  323. consume_skb(fq->q.fragments);
  324. fq->q.fragments = head;
  325. }
  326. WARN_ON(head == NULL);
  327. WARN_ON(FRAG6_CB(head)->offset != 0);
  328. /* Unfragmented part is taken from the first segment. */
  329. payload_len = ((head->data - skb_network_header(head)) -
  330. sizeof(struct ipv6hdr) + fq->q.len -
  331. sizeof(struct frag_hdr));
  332. if (payload_len > IPV6_MAXPLEN)
  333. goto out_oversize;
  334. /* Head of list must not be cloned. */
  335. if (skb_unclone(head, GFP_ATOMIC))
  336. goto out_oom;
  337. /* If the first fragment is fragmented itself, we split
  338. * it to two chunks: the first with data and paged part
  339. * and the second, holding only fragments. */
  340. if (skb_has_frag_list(head)) {
  341. struct sk_buff *clone;
  342. int i, plen = 0;
  343. if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
  344. goto out_oom;
  345. clone->next = head->next;
  346. head->next = clone;
  347. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  348. skb_frag_list_init(head);
  349. for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
  350. plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
  351. clone->len = clone->data_len = head->data_len - plen;
  352. head->data_len -= clone->len;
  353. head->len -= clone->len;
  354. clone->csum = 0;
  355. clone->ip_summed = head->ip_summed;
  356. add_frag_mem_limit(&fq->q, clone->truesize);
  357. }
  358. /* We have to remove fragment header from datagram and to relocate
  359. * header in order to calculate ICV correctly. */
  360. nhoff = fq->nhoffset;
  361. skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
  362. memmove(head->head + sizeof(struct frag_hdr), head->head,
  363. (head->data - head->head) - sizeof(struct frag_hdr));
  364. head->mac_header += sizeof(struct frag_hdr);
  365. head->network_header += sizeof(struct frag_hdr);
  366. skb_reset_transport_header(head);
  367. skb_push(head, head->data - skb_network_header(head));
  368. sum_truesize = head->truesize;
  369. for (fp = head->next; fp;) {
  370. bool headstolen;
  371. int delta;
  372. struct sk_buff *next = fp->next;
  373. sum_truesize += fp->truesize;
  374. if (head->ip_summed != fp->ip_summed)
  375. head->ip_summed = CHECKSUM_NONE;
  376. else if (head->ip_summed == CHECKSUM_COMPLETE)
  377. head->csum = csum_add(head->csum, fp->csum);
  378. if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
  379. kfree_skb_partial(fp, headstolen);
  380. } else {
  381. if (!skb_shinfo(head)->frag_list)
  382. skb_shinfo(head)->frag_list = fp;
  383. head->data_len += fp->len;
  384. head->len += fp->len;
  385. head->truesize += fp->truesize;
  386. }
  387. fp = next;
  388. }
  389. sub_frag_mem_limit(&fq->q, sum_truesize);
  390. head->next = NULL;
  391. head->dev = dev;
  392. head->tstamp = fq->q.stamp;
  393. ipv6_hdr(head)->payload_len = htons(payload_len);
  394. IP6CB(head)->nhoff = nhoff;
  395. /* Yes, and fold redundant checksum back. 8) */
  396. if (head->ip_summed == CHECKSUM_COMPLETE)
  397. head->csum = csum_partial(skb_network_header(head),
  398. skb_network_header_len(head),
  399. head->csum);
  400. rcu_read_lock();
  401. IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
  402. rcu_read_unlock();
  403. fq->q.fragments = NULL;
  404. fq->q.fragments_tail = NULL;
  405. return 1;
  406. out_oversize:
  407. net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
  408. goto out_fail;
  409. out_oom:
  410. net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
  411. out_fail:
  412. rcu_read_lock();
  413. IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
  414. rcu_read_unlock();
  415. return -1;
  416. }
  417. static int ipv6_frag_rcv(struct sk_buff *skb)
  418. {
  419. struct frag_hdr *fhdr;
  420. struct frag_queue *fq;
  421. const struct ipv6hdr *hdr = ipv6_hdr(skb);
  422. struct net *net = dev_net(skb_dst(skb)->dev);
  423. int evicted;
  424. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
  425. /* Jumbo payload inhibits frag. header */
  426. if (hdr->payload_len==0)
  427. goto fail_hdr;
  428. if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
  429. sizeof(struct frag_hdr))))
  430. goto fail_hdr;
  431. hdr = ipv6_hdr(skb);
  432. fhdr = (struct frag_hdr *)skb_transport_header(skb);
  433. if (!(fhdr->frag_off & htons(0xFFF9))) {
  434. /* It is not a fragmented frame */
  435. skb->transport_header += sizeof(struct frag_hdr);
  436. IP6_INC_STATS_BH(net,
  437. ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
  438. IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
  439. return 1;
  440. }
  441. evicted = inet_frag_evictor(&net->ipv6.frags, &ip6_frags, false);
  442. if (evicted)
  443. IP6_ADD_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  444. IPSTATS_MIB_REASMFAILS, evicted);
  445. fq = fq_find(net, fhdr->identification, &hdr->saddr, &hdr->daddr);
  446. if (fq != NULL) {
  447. int ret;
  448. spin_lock(&fq->q.lock);
  449. ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
  450. spin_unlock(&fq->q.lock);
  451. inet_frag_put(&fq->q, &ip6_frags);
  452. return ret;
  453. }
  454. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
  455. kfree_skb(skb);
  456. return -1;
  457. fail_hdr:
  458. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
  459. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
  460. return -1;
  461. }
  462. static const struct inet6_protocol frag_protocol =
  463. {
  464. .handler = ipv6_frag_rcv,
  465. .flags = INET6_PROTO_NOPOLICY,
  466. };
  467. #ifdef CONFIG_SYSCTL
  468. static struct ctl_table ip6_frags_ns_ctl_table[] = {
  469. {
  470. .procname = "ip6frag_high_thresh",
  471. .data = &init_net.ipv6.frags.high_thresh,
  472. .maxlen = sizeof(int),
  473. .mode = 0644,
  474. .proc_handler = proc_dointvec
  475. },
  476. {
  477. .procname = "ip6frag_low_thresh",
  478. .data = &init_net.ipv6.frags.low_thresh,
  479. .maxlen = sizeof(int),
  480. .mode = 0644,
  481. .proc_handler = proc_dointvec
  482. },
  483. {
  484. .procname = "ip6frag_time",
  485. .data = &init_net.ipv6.frags.timeout,
  486. .maxlen = sizeof(int),
  487. .mode = 0644,
  488. .proc_handler = proc_dointvec_jiffies,
  489. },
  490. { }
  491. };
  492. static struct ctl_table ip6_frags_ctl_table[] = {
  493. {
  494. .procname = "ip6frag_secret_interval",
  495. .data = &ip6_frags.secret_interval,
  496. .maxlen = sizeof(int),
  497. .mode = 0644,
  498. .proc_handler = proc_dointvec_jiffies,
  499. },
  500. { }
  501. };
  502. static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
  503. {
  504. struct ctl_table *table;
  505. struct ctl_table_header *hdr;
  506. table = ip6_frags_ns_ctl_table;
  507. if (!net_eq(net, &init_net)) {
  508. table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
  509. if (table == NULL)
  510. goto err_alloc;
  511. table[0].data = &net->ipv6.frags.high_thresh;
  512. table[1].data = &net->ipv6.frags.low_thresh;
  513. table[2].data = &net->ipv6.frags.timeout;
  514. /* Don't export sysctls to unprivileged users */
  515. if (net->user_ns != &init_user_ns)
  516. table[0].procname = NULL;
  517. }
  518. hdr = register_net_sysctl(net, "net/ipv6", table);
  519. if (hdr == NULL)
  520. goto err_reg;
  521. net->ipv6.sysctl.frags_hdr = hdr;
  522. return 0;
  523. err_reg:
  524. if (!net_eq(net, &init_net))
  525. kfree(table);
  526. err_alloc:
  527. return -ENOMEM;
  528. }
  529. static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
  530. {
  531. struct ctl_table *table;
  532. table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
  533. unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
  534. if (!net_eq(net, &init_net))
  535. kfree(table);
  536. }
  537. static struct ctl_table_header *ip6_ctl_header;
  538. static int ip6_frags_sysctl_register(void)
  539. {
  540. ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
  541. ip6_frags_ctl_table);
  542. return ip6_ctl_header == NULL ? -ENOMEM : 0;
  543. }
  544. static void ip6_frags_sysctl_unregister(void)
  545. {
  546. unregister_net_sysctl_table(ip6_ctl_header);
  547. }
  548. #else
  549. static inline int ip6_frags_ns_sysctl_register(struct net *net)
  550. {
  551. return 0;
  552. }
  553. static inline void ip6_frags_ns_sysctl_unregister(struct net *net)
  554. {
  555. }
  556. static inline int ip6_frags_sysctl_register(void)
  557. {
  558. return 0;
  559. }
  560. static inline void ip6_frags_sysctl_unregister(void)
  561. {
  562. }
  563. #endif
  564. static int __net_init ipv6_frags_init_net(struct net *net)
  565. {
  566. net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
  567. net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
  568. net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
  569. inet_frags_init_net(&net->ipv6.frags);
  570. return ip6_frags_ns_sysctl_register(net);
  571. }
  572. static void __net_exit ipv6_frags_exit_net(struct net *net)
  573. {
  574. ip6_frags_ns_sysctl_unregister(net);
  575. inet_frags_exit_net(&net->ipv6.frags, &ip6_frags);
  576. }
  577. static struct pernet_operations ip6_frags_ops = {
  578. .init = ipv6_frags_init_net,
  579. .exit = ipv6_frags_exit_net,
  580. };
  581. int __init ipv6_frag_init(void)
  582. {
  583. int ret;
  584. ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
  585. if (ret)
  586. goto out;
  587. ret = ip6_frags_sysctl_register();
  588. if (ret)
  589. goto err_sysctl;
  590. ret = register_pernet_subsys(&ip6_frags_ops);
  591. if (ret)
  592. goto err_pernet;
  593. ip6_frags.hashfn = ip6_hashfn;
  594. ip6_frags.constructor = ip6_frag_init;
  595. ip6_frags.destructor = NULL;
  596. ip6_frags.skb_free = NULL;
  597. ip6_frags.qsize = sizeof(struct frag_queue);
  598. ip6_frags.match = ip6_frag_match;
  599. ip6_frags.frag_expire = ip6_frag_expire;
  600. ip6_frags.secret_interval = 10 * 60 * HZ;
  601. inet_frags_init(&ip6_frags);
  602. out:
  603. return ret;
  604. err_pernet:
  605. ip6_frags_sysctl_unregister();
  606. err_sysctl:
  607. inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
  608. goto out;
  609. }
  610. void ipv6_frag_exit(void)
  611. {
  612. inet_frags_fini(&ip6_frags);
  613. ip6_frags_sysctl_unregister();
  614. unregister_pernet_subsys(&ip6_frags_ops);
  615. inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
  616. }