reassembly.c 18 KB

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