reassembly.c 19 KB

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