reassembly.c 19 KB

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