reassembly.c 19 KB

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