ip_fragment.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * The IP fragmentation functionality.
  7. *
  8. * Version: $Id: ip_fragment.c,v 1.59 2002/01/12 07:54:56 davem Exp $
  9. *
  10. * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
  11. * Alan Cox <Alan.Cox@linux.org>
  12. *
  13. * Fixes:
  14. * Alan Cox : Split from ip.c , see ip_input.c for history.
  15. * David S. Miller : Begin massive cleanup...
  16. * Andi Kleen : Add sysctls.
  17. * xxxx : Overlapfrag bug.
  18. * Ultima : ip_expire() kernel panic.
  19. * Bill Hawes : Frag accounting and evictor fixes.
  20. * John McDonald : 0 length frag bug.
  21. * Alexey Kuznetsov: SMP races, threading, cleanup.
  22. * Patrick McHardy : LRU queue of frag heads for evictor.
  23. */
  24. #include <linux/compiler.h>
  25. #include <linux/config.h>
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/mm.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/list.h>
  32. #include <linux/ip.h>
  33. #include <linux/icmp.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/jhash.h>
  36. #include <linux/random.h>
  37. #include <net/sock.h>
  38. #include <net/ip.h>
  39. #include <net/icmp.h>
  40. #include <net/checksum.h>
  41. #include <net/inetpeer.h>
  42. #include <linux/tcp.h>
  43. #include <linux/udp.h>
  44. #include <linux/inet.h>
  45. #include <linux/netfilter_ipv4.h>
  46. /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
  47. * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
  48. * as well. Or notify me, at least. --ANK
  49. */
  50. /* Fragment cache limits. We will commit 256K at one time. Should we
  51. * cross that limit we will prune down to 192K. This should cope with
  52. * even the most extreme cases without allowing an attacker to measurably
  53. * harm machine performance.
  54. */
  55. int sysctl_ipfrag_high_thresh = 256*1024;
  56. int sysctl_ipfrag_low_thresh = 192*1024;
  57. int sysctl_ipfrag_max_dist = 64;
  58. /* Important NOTE! Fragment queue must be destroyed before MSL expires.
  59. * RFC791 is wrong proposing to prolongate timer each fragment arrival by TTL.
  60. */
  61. int sysctl_ipfrag_time = IP_FRAG_TIME;
  62. struct ipfrag_skb_cb
  63. {
  64. struct inet_skb_parm h;
  65. int offset;
  66. };
  67. #define FRAG_CB(skb) ((struct ipfrag_skb_cb*)((skb)->cb))
  68. /* Describe an entry in the "incomplete datagrams" queue. */
  69. struct ipq {
  70. struct hlist_node list;
  71. struct list_head lru_list; /* lru list member */
  72. u32 user;
  73. u32 saddr;
  74. u32 daddr;
  75. u16 id;
  76. u8 protocol;
  77. u8 last_in;
  78. #define COMPLETE 4
  79. #define FIRST_IN 2
  80. #define LAST_IN 1
  81. struct sk_buff *fragments; /* linked list of received fragments */
  82. int len; /* total length of original datagram */
  83. int meat;
  84. spinlock_t lock;
  85. atomic_t refcnt;
  86. struct timer_list timer; /* when will this queue expire? */
  87. struct timeval stamp;
  88. int iif;
  89. unsigned int rid;
  90. struct inet_peer *peer;
  91. };
  92. /* Hash table. */
  93. #define IPQ_HASHSZ 64
  94. /* Per-bucket lock is easy to add now. */
  95. static struct hlist_head ipq_hash[IPQ_HASHSZ];
  96. static DEFINE_RWLOCK(ipfrag_lock);
  97. static u32 ipfrag_hash_rnd;
  98. static LIST_HEAD(ipq_lru_list);
  99. int ip_frag_nqueues = 0;
  100. static __inline__ void __ipq_unlink(struct ipq *qp)
  101. {
  102. hlist_del(&qp->list);
  103. list_del(&qp->lru_list);
  104. ip_frag_nqueues--;
  105. }
  106. static __inline__ void ipq_unlink(struct ipq *ipq)
  107. {
  108. write_lock(&ipfrag_lock);
  109. __ipq_unlink(ipq);
  110. write_unlock(&ipfrag_lock);
  111. }
  112. static unsigned int ipqhashfn(u16 id, u32 saddr, u32 daddr, u8 prot)
  113. {
  114. return jhash_3words((u32)id << 16 | prot, saddr, daddr,
  115. ipfrag_hash_rnd) & (IPQ_HASHSZ - 1);
  116. }
  117. static struct timer_list ipfrag_secret_timer;
  118. int sysctl_ipfrag_secret_interval = 10 * 60 * HZ;
  119. static void ipfrag_secret_rebuild(unsigned long dummy)
  120. {
  121. unsigned long now = jiffies;
  122. int i;
  123. write_lock(&ipfrag_lock);
  124. get_random_bytes(&ipfrag_hash_rnd, sizeof(u32));
  125. for (i = 0; i < IPQ_HASHSZ; i++) {
  126. struct ipq *q;
  127. struct hlist_node *p, *n;
  128. hlist_for_each_entry_safe(q, p, n, &ipq_hash[i], list) {
  129. unsigned int hval = ipqhashfn(q->id, q->saddr,
  130. q->daddr, q->protocol);
  131. if (hval != i) {
  132. hlist_del(&q->list);
  133. /* Relink to new hash chain. */
  134. hlist_add_head(&q->list, &ipq_hash[hval]);
  135. }
  136. }
  137. }
  138. write_unlock(&ipfrag_lock);
  139. mod_timer(&ipfrag_secret_timer, now + sysctl_ipfrag_secret_interval);
  140. }
  141. atomic_t ip_frag_mem = ATOMIC_INIT(0); /* Memory used for fragments */
  142. /* Memory Tracking Functions. */
  143. static __inline__ void frag_kfree_skb(struct sk_buff *skb, int *work)
  144. {
  145. if (work)
  146. *work -= skb->truesize;
  147. atomic_sub(skb->truesize, &ip_frag_mem);
  148. kfree_skb(skb);
  149. }
  150. static __inline__ void frag_free_queue(struct ipq *qp, int *work)
  151. {
  152. if (work)
  153. *work -= sizeof(struct ipq);
  154. atomic_sub(sizeof(struct ipq), &ip_frag_mem);
  155. kfree(qp);
  156. }
  157. static __inline__ struct ipq *frag_alloc_queue(void)
  158. {
  159. struct ipq *qp = kmalloc(sizeof(struct ipq), GFP_ATOMIC);
  160. if(!qp)
  161. return NULL;
  162. atomic_add(sizeof(struct ipq), &ip_frag_mem);
  163. return qp;
  164. }
  165. /* Destruction primitives. */
  166. /* Complete destruction of ipq. */
  167. static void ip_frag_destroy(struct ipq *qp, int *work)
  168. {
  169. struct sk_buff *fp;
  170. BUG_TRAP(qp->last_in&COMPLETE);
  171. BUG_TRAP(del_timer(&qp->timer) == 0);
  172. if (qp->peer)
  173. inet_putpeer(qp->peer);
  174. /* Release all fragment data. */
  175. fp = qp->fragments;
  176. while (fp) {
  177. struct sk_buff *xp = fp->next;
  178. frag_kfree_skb(fp, work);
  179. fp = xp;
  180. }
  181. /* Finally, release the queue descriptor itself. */
  182. frag_free_queue(qp, work);
  183. }
  184. static __inline__ void ipq_put(struct ipq *ipq, int *work)
  185. {
  186. if (atomic_dec_and_test(&ipq->refcnt))
  187. ip_frag_destroy(ipq, work);
  188. }
  189. /* Kill ipq entry. It is not destroyed immediately,
  190. * because caller (and someone more) holds reference count.
  191. */
  192. static void ipq_kill(struct ipq *ipq)
  193. {
  194. if (del_timer(&ipq->timer))
  195. atomic_dec(&ipq->refcnt);
  196. if (!(ipq->last_in & COMPLETE)) {
  197. ipq_unlink(ipq);
  198. atomic_dec(&ipq->refcnt);
  199. ipq->last_in |= COMPLETE;
  200. }
  201. }
  202. /* Memory limiting on fragments. Evictor trashes the oldest
  203. * fragment queue until we are back under the threshold.
  204. */
  205. static void ip_evictor(void)
  206. {
  207. struct ipq *qp;
  208. struct list_head *tmp;
  209. int work;
  210. work = atomic_read(&ip_frag_mem) - sysctl_ipfrag_low_thresh;
  211. if (work <= 0)
  212. return;
  213. while (work > 0) {
  214. read_lock(&ipfrag_lock);
  215. if (list_empty(&ipq_lru_list)) {
  216. read_unlock(&ipfrag_lock);
  217. return;
  218. }
  219. tmp = ipq_lru_list.next;
  220. qp = list_entry(tmp, struct ipq, lru_list);
  221. atomic_inc(&qp->refcnt);
  222. read_unlock(&ipfrag_lock);
  223. spin_lock(&qp->lock);
  224. if (!(qp->last_in&COMPLETE))
  225. ipq_kill(qp);
  226. spin_unlock(&qp->lock);
  227. ipq_put(qp, &work);
  228. IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
  229. }
  230. }
  231. /*
  232. * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
  233. */
  234. static void ip_expire(unsigned long arg)
  235. {
  236. struct ipq *qp = (struct ipq *) arg;
  237. spin_lock(&qp->lock);
  238. if (qp->last_in & COMPLETE)
  239. goto out;
  240. ipq_kill(qp);
  241. IP_INC_STATS_BH(IPSTATS_MIB_REASMTIMEOUT);
  242. IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
  243. if ((qp->last_in&FIRST_IN) && qp->fragments != NULL) {
  244. struct sk_buff *head = qp->fragments;
  245. /* Send an ICMP "Fragment Reassembly Timeout" message. */
  246. if ((head->dev = dev_get_by_index(qp->iif)) != NULL) {
  247. icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
  248. dev_put(head->dev);
  249. }
  250. }
  251. out:
  252. spin_unlock(&qp->lock);
  253. ipq_put(qp, NULL);
  254. }
  255. /* Creation primitives. */
  256. static struct ipq *ip_frag_intern(unsigned int hash, struct ipq *qp_in)
  257. {
  258. struct ipq *qp;
  259. #ifdef CONFIG_SMP
  260. struct hlist_node *n;
  261. #endif
  262. write_lock(&ipfrag_lock);
  263. #ifdef CONFIG_SMP
  264. /* With SMP race we have to recheck hash table, because
  265. * such entry could be created on other cpu, while we
  266. * promoted read lock to write lock.
  267. */
  268. hlist_for_each_entry(qp, n, &ipq_hash[hash], list) {
  269. if(qp->id == qp_in->id &&
  270. qp->saddr == qp_in->saddr &&
  271. qp->daddr == qp_in->daddr &&
  272. qp->protocol == qp_in->protocol &&
  273. qp->user == qp_in->user) {
  274. atomic_inc(&qp->refcnt);
  275. write_unlock(&ipfrag_lock);
  276. qp_in->last_in |= COMPLETE;
  277. ipq_put(qp_in, NULL);
  278. return qp;
  279. }
  280. }
  281. #endif
  282. qp = qp_in;
  283. if (!mod_timer(&qp->timer, jiffies + sysctl_ipfrag_time))
  284. atomic_inc(&qp->refcnt);
  285. atomic_inc(&qp->refcnt);
  286. hlist_add_head(&qp->list, &ipq_hash[hash]);
  287. INIT_LIST_HEAD(&qp->lru_list);
  288. list_add_tail(&qp->lru_list, &ipq_lru_list);
  289. ip_frag_nqueues++;
  290. write_unlock(&ipfrag_lock);
  291. return qp;
  292. }
  293. /* Add an entry to the 'ipq' queue for a newly received IP datagram. */
  294. static struct ipq *ip_frag_create(unsigned hash, struct iphdr *iph, u32 user)
  295. {
  296. struct ipq *qp;
  297. if ((qp = frag_alloc_queue()) == NULL)
  298. goto out_nomem;
  299. qp->protocol = iph->protocol;
  300. qp->last_in = 0;
  301. qp->id = iph->id;
  302. qp->saddr = iph->saddr;
  303. qp->daddr = iph->daddr;
  304. qp->user = user;
  305. qp->len = 0;
  306. qp->meat = 0;
  307. qp->fragments = NULL;
  308. qp->iif = 0;
  309. qp->peer = sysctl_ipfrag_max_dist ? inet_getpeer(iph->saddr, 1) : NULL;
  310. /* Initialize a timer for this entry. */
  311. init_timer(&qp->timer);
  312. qp->timer.data = (unsigned long) qp; /* pointer to queue */
  313. qp->timer.function = ip_expire; /* expire function */
  314. spin_lock_init(&qp->lock);
  315. atomic_set(&qp->refcnt, 1);
  316. return ip_frag_intern(hash, qp);
  317. out_nomem:
  318. LIMIT_NETDEBUG(KERN_ERR "ip_frag_create: no memory left !\n");
  319. return NULL;
  320. }
  321. /* Find the correct entry in the "incomplete datagrams" queue for
  322. * this IP datagram, and create new one, if nothing is found.
  323. */
  324. static inline struct ipq *ip_find(struct iphdr *iph, u32 user)
  325. {
  326. __be16 id = iph->id;
  327. __u32 saddr = iph->saddr;
  328. __u32 daddr = iph->daddr;
  329. __u8 protocol = iph->protocol;
  330. unsigned int hash = ipqhashfn(id, saddr, daddr, protocol);
  331. struct ipq *qp;
  332. struct hlist_node *n;
  333. read_lock(&ipfrag_lock);
  334. hlist_for_each_entry(qp, n, &ipq_hash[hash], list) {
  335. if(qp->id == id &&
  336. qp->saddr == saddr &&
  337. qp->daddr == daddr &&
  338. qp->protocol == protocol &&
  339. qp->user == user) {
  340. atomic_inc(&qp->refcnt);
  341. read_unlock(&ipfrag_lock);
  342. return qp;
  343. }
  344. }
  345. read_unlock(&ipfrag_lock);
  346. return ip_frag_create(hash, iph, user);
  347. }
  348. /* Is the fragment too far ahead to be part of ipq? */
  349. static inline int ip_frag_too_far(struct ipq *qp)
  350. {
  351. struct inet_peer *peer = qp->peer;
  352. unsigned int max = sysctl_ipfrag_max_dist;
  353. unsigned int start, end;
  354. int rc;
  355. if (!peer || !max)
  356. return 0;
  357. start = qp->rid;
  358. end = atomic_inc_return(&peer->rid);
  359. qp->rid = end;
  360. rc = qp->fragments && (end - start) > max;
  361. if (rc) {
  362. IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
  363. }
  364. return rc;
  365. }
  366. static int ip_frag_reinit(struct ipq *qp)
  367. {
  368. struct sk_buff *fp;
  369. if (!mod_timer(&qp->timer, jiffies + sysctl_ipfrag_time)) {
  370. atomic_inc(&qp->refcnt);
  371. return -ETIMEDOUT;
  372. }
  373. fp = qp->fragments;
  374. do {
  375. struct sk_buff *xp = fp->next;
  376. frag_kfree_skb(fp, NULL);
  377. fp = xp;
  378. } while (fp);
  379. qp->last_in = 0;
  380. qp->len = 0;
  381. qp->meat = 0;
  382. qp->fragments = NULL;
  383. qp->iif = 0;
  384. return 0;
  385. }
  386. /* Add new segment to existing queue. */
  387. static void ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
  388. {
  389. struct sk_buff *prev, *next;
  390. int flags, offset;
  391. int ihl, end;
  392. if (qp->last_in & COMPLETE)
  393. goto err;
  394. if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
  395. unlikely(ip_frag_too_far(qp)) && unlikely(ip_frag_reinit(qp))) {
  396. ipq_kill(qp);
  397. goto err;
  398. }
  399. offset = ntohs(skb->nh.iph->frag_off);
  400. flags = offset & ~IP_OFFSET;
  401. offset &= IP_OFFSET;
  402. offset <<= 3; /* offset is in 8-byte chunks */
  403. ihl = skb->nh.iph->ihl * 4;
  404. /* Determine the position of this fragment. */
  405. end = offset + skb->len - ihl;
  406. /* Is this the final fragment? */
  407. if ((flags & IP_MF) == 0) {
  408. /* If we already have some bits beyond end
  409. * or have different end, the segment is corrrupted.
  410. */
  411. if (end < qp->len ||
  412. ((qp->last_in & LAST_IN) && end != qp->len))
  413. goto err;
  414. qp->last_in |= LAST_IN;
  415. qp->len = end;
  416. } else {
  417. if (end&7) {
  418. end &= ~7;
  419. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  420. skb->ip_summed = CHECKSUM_NONE;
  421. }
  422. if (end > qp->len) {
  423. /* Some bits beyond end -> corruption. */
  424. if (qp->last_in & LAST_IN)
  425. goto err;
  426. qp->len = end;
  427. }
  428. }
  429. if (end == offset)
  430. goto err;
  431. if (pskb_pull(skb, ihl) == NULL)
  432. goto err;
  433. if (pskb_trim_rcsum(skb, end-offset))
  434. goto err;
  435. /* Find out which fragments are in front and at the back of us
  436. * in the chain of fragments so far. We must know where to put
  437. * this fragment, right?
  438. */
  439. prev = NULL;
  440. for(next = qp->fragments; next != NULL; next = next->next) {
  441. if (FRAG_CB(next)->offset >= offset)
  442. break; /* bingo! */
  443. prev = next;
  444. }
  445. /* We found where to put this one. Check for overlap with
  446. * preceding fragment, and, if needed, align things so that
  447. * any overlaps are eliminated.
  448. */
  449. if (prev) {
  450. int i = (FRAG_CB(prev)->offset + prev->len) - offset;
  451. if (i > 0) {
  452. offset += i;
  453. if (end <= offset)
  454. goto err;
  455. if (!pskb_pull(skb, i))
  456. goto err;
  457. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  458. skb->ip_summed = CHECKSUM_NONE;
  459. }
  460. }
  461. while (next && FRAG_CB(next)->offset < end) {
  462. int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
  463. if (i < next->len) {
  464. /* Eat head of the next overlapped fragment
  465. * and leave the loop. The next ones cannot overlap.
  466. */
  467. if (!pskb_pull(next, i))
  468. goto err;
  469. FRAG_CB(next)->offset += i;
  470. qp->meat -= i;
  471. if (next->ip_summed != CHECKSUM_UNNECESSARY)
  472. next->ip_summed = CHECKSUM_NONE;
  473. break;
  474. } else {
  475. struct sk_buff *free_it = next;
  476. /* Old fragmnet is completely overridden with
  477. * new one drop it.
  478. */
  479. next = next->next;
  480. if (prev)
  481. prev->next = next;
  482. else
  483. qp->fragments = next;
  484. qp->meat -= free_it->len;
  485. frag_kfree_skb(free_it, NULL);
  486. }
  487. }
  488. FRAG_CB(skb)->offset = offset;
  489. /* Insert this fragment in the chain of fragments. */
  490. skb->next = next;
  491. if (prev)
  492. prev->next = skb;
  493. else
  494. qp->fragments = skb;
  495. if (skb->dev)
  496. qp->iif = skb->dev->ifindex;
  497. skb->dev = NULL;
  498. skb_get_timestamp(skb, &qp->stamp);
  499. qp->meat += skb->len;
  500. atomic_add(skb->truesize, &ip_frag_mem);
  501. if (offset == 0)
  502. qp->last_in |= FIRST_IN;
  503. write_lock(&ipfrag_lock);
  504. list_move_tail(&qp->lru_list, &ipq_lru_list);
  505. write_unlock(&ipfrag_lock);
  506. return;
  507. err:
  508. kfree_skb(skb);
  509. }
  510. /* Build a new IP datagram from all its fragments. */
  511. static struct sk_buff *ip_frag_reasm(struct ipq *qp, struct net_device *dev)
  512. {
  513. struct iphdr *iph;
  514. struct sk_buff *fp, *head = qp->fragments;
  515. int len;
  516. int ihlen;
  517. ipq_kill(qp);
  518. BUG_TRAP(head != NULL);
  519. BUG_TRAP(FRAG_CB(head)->offset == 0);
  520. /* Allocate a new buffer for the datagram. */
  521. ihlen = head->nh.iph->ihl*4;
  522. len = ihlen + qp->len;
  523. if(len > 65535)
  524. goto out_oversize;
  525. /* Head of list must not be cloned. */
  526. if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
  527. goto out_nomem;
  528. /* If the first fragment is fragmented itself, we split
  529. * it to two chunks: the first with data and paged part
  530. * and the second, holding only fragments. */
  531. if (skb_shinfo(head)->frag_list) {
  532. struct sk_buff *clone;
  533. int i, plen = 0;
  534. if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
  535. goto out_nomem;
  536. clone->next = head->next;
  537. head->next = clone;
  538. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  539. skb_shinfo(head)->frag_list = NULL;
  540. for (i=0; i<skb_shinfo(head)->nr_frags; i++)
  541. plen += skb_shinfo(head)->frags[i].size;
  542. clone->len = clone->data_len = head->data_len - plen;
  543. head->data_len -= clone->len;
  544. head->len -= clone->len;
  545. clone->csum = 0;
  546. clone->ip_summed = head->ip_summed;
  547. atomic_add(clone->truesize, &ip_frag_mem);
  548. }
  549. skb_shinfo(head)->frag_list = head->next;
  550. skb_push(head, head->data - head->nh.raw);
  551. atomic_sub(head->truesize, &ip_frag_mem);
  552. for (fp=head->next; fp; fp = fp->next) {
  553. head->data_len += fp->len;
  554. head->len += fp->len;
  555. if (head->ip_summed != fp->ip_summed)
  556. head->ip_summed = CHECKSUM_NONE;
  557. else if (head->ip_summed == CHECKSUM_HW)
  558. head->csum = csum_add(head->csum, fp->csum);
  559. head->truesize += fp->truesize;
  560. atomic_sub(fp->truesize, &ip_frag_mem);
  561. }
  562. head->next = NULL;
  563. head->dev = dev;
  564. skb_set_timestamp(head, &qp->stamp);
  565. iph = head->nh.iph;
  566. iph->frag_off = 0;
  567. iph->tot_len = htons(len);
  568. IP_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
  569. qp->fragments = NULL;
  570. return head;
  571. out_nomem:
  572. LIMIT_NETDEBUG(KERN_ERR "IP: queue_glue: no memory for gluing "
  573. "queue %p\n", qp);
  574. goto out_fail;
  575. out_oversize:
  576. if (net_ratelimit())
  577. printk(KERN_INFO
  578. "Oversized IP packet from %d.%d.%d.%d.\n",
  579. NIPQUAD(qp->saddr));
  580. out_fail:
  581. IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
  582. return NULL;
  583. }
  584. /* Process an incoming IP datagram fragment. */
  585. struct sk_buff *ip_defrag(struct sk_buff *skb, u32 user)
  586. {
  587. struct iphdr *iph = skb->nh.iph;
  588. struct ipq *qp;
  589. struct net_device *dev;
  590. IP_INC_STATS_BH(IPSTATS_MIB_REASMREQDS);
  591. /* Start by cleaning up the memory. */
  592. if (atomic_read(&ip_frag_mem) > sysctl_ipfrag_high_thresh)
  593. ip_evictor();
  594. dev = skb->dev;
  595. /* Lookup (or create) queue header */
  596. if ((qp = ip_find(iph, user)) != NULL) {
  597. struct sk_buff *ret = NULL;
  598. spin_lock(&qp->lock);
  599. ip_frag_queue(qp, skb);
  600. if (qp->last_in == (FIRST_IN|LAST_IN) &&
  601. qp->meat == qp->len)
  602. ret = ip_frag_reasm(qp, dev);
  603. spin_unlock(&qp->lock);
  604. ipq_put(qp, NULL);
  605. return ret;
  606. }
  607. IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
  608. kfree_skb(skb);
  609. return NULL;
  610. }
  611. void ipfrag_init(void)
  612. {
  613. ipfrag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
  614. (jiffies ^ (jiffies >> 6)));
  615. init_timer(&ipfrag_secret_timer);
  616. ipfrag_secret_timer.function = ipfrag_secret_rebuild;
  617. ipfrag_secret_timer.expires = jiffies + sysctl_ipfrag_secret_interval;
  618. add_timer(&ipfrag_secret_timer);
  619. }
  620. EXPORT_SYMBOL(ip_defrag);