reassembly.c 19 KB

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