reassembly.c 19 KB

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