ip_fragment.c 17 KB

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