reassembly.c 19 KB

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