reassembly.c 19 KB

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