reassembly.c 20 KB

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