reassembly.c 20 KB

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