reassembly.c 18 KB

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