reassembly.c 20 KB

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