reassembly.c 18 KB

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