reassembly.c 19 KB

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