reassembly.c 18 KB

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