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. __be32 saddr;
  73. __be32 daddr;
  74. __be16 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. ktime_t 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(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
  112. {
  113. return jhash_3words((__force u32)id << 16 | prot,
  114. (__force u32)saddr, (__force u32)daddr,
  115. ipfrag_hash_rnd) & (IPQ_HASHSZ - 1);
  116. }
  117. static struct timer_list ipfrag_secret_timer;
  118. int sysctl_ipfrag_secret_interval __read_mostly = 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(&init_net, 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(struct ipq *qp_in)
  257. {
  258. struct ipq *qp;
  259. #ifdef CONFIG_SMP
  260. struct hlist_node *n;
  261. #endif
  262. unsigned int hash;
  263. write_lock(&ipfrag_lock);
  264. hash = ipqhashfn(qp_in->id, qp_in->saddr, qp_in->daddr,
  265. qp_in->protocol);
  266. #ifdef CONFIG_SMP
  267. /* With SMP race we have to recheck hash table, because
  268. * such entry could be created on other cpu, while we
  269. * promoted read lock to write lock.
  270. */
  271. hlist_for_each_entry(qp, n, &ipq_hash[hash], list) {
  272. if (qp->id == qp_in->id &&
  273. qp->saddr == qp_in->saddr &&
  274. qp->daddr == qp_in->daddr &&
  275. qp->protocol == qp_in->protocol &&
  276. qp->user == qp_in->user) {
  277. atomic_inc(&qp->refcnt);
  278. write_unlock(&ipfrag_lock);
  279. qp_in->last_in |= COMPLETE;
  280. ipq_put(qp_in, NULL);
  281. return qp;
  282. }
  283. }
  284. #endif
  285. qp = qp_in;
  286. if (!mod_timer(&qp->timer, jiffies + sysctl_ipfrag_time))
  287. atomic_inc(&qp->refcnt);
  288. atomic_inc(&qp->refcnt);
  289. hlist_add_head(&qp->list, &ipq_hash[hash]);
  290. INIT_LIST_HEAD(&qp->lru_list);
  291. list_add_tail(&qp->lru_list, &ipq_lru_list);
  292. ip_frag_nqueues++;
  293. write_unlock(&ipfrag_lock);
  294. return qp;
  295. }
  296. /* Add an entry to the 'ipq' queue for a newly received IP datagram. */
  297. static struct ipq *ip_frag_create(struct iphdr *iph, u32 user)
  298. {
  299. struct ipq *qp;
  300. if ((qp = frag_alloc_queue()) == NULL)
  301. goto out_nomem;
  302. qp->protocol = iph->protocol;
  303. qp->last_in = 0;
  304. qp->id = iph->id;
  305. qp->saddr = iph->saddr;
  306. qp->daddr = iph->daddr;
  307. qp->user = user;
  308. qp->len = 0;
  309. qp->meat = 0;
  310. qp->fragments = NULL;
  311. qp->iif = 0;
  312. qp->peer = sysctl_ipfrag_max_dist ? inet_getpeer(iph->saddr, 1) : NULL;
  313. /* Initialize a timer for this entry. */
  314. init_timer(&qp->timer);
  315. qp->timer.data = (unsigned long) qp; /* pointer to queue */
  316. qp->timer.function = ip_expire; /* expire function */
  317. spin_lock_init(&qp->lock);
  318. atomic_set(&qp->refcnt, 1);
  319. return ip_frag_intern(qp);
  320. out_nomem:
  321. LIMIT_NETDEBUG(KERN_ERR "ip_frag_create: no memory left !\n");
  322. return NULL;
  323. }
  324. /* Find the correct entry in the "incomplete datagrams" queue for
  325. * this IP datagram, and create new one, if nothing is found.
  326. */
  327. static inline struct ipq *ip_find(struct iphdr *iph, u32 user)
  328. {
  329. __be16 id = iph->id;
  330. __be32 saddr = iph->saddr;
  331. __be32 daddr = iph->daddr;
  332. __u8 protocol = iph->protocol;
  333. unsigned int hash;
  334. struct ipq *qp;
  335. struct hlist_node *n;
  336. read_lock(&ipfrag_lock);
  337. hash = ipqhashfn(id, saddr, daddr, protocol);
  338. hlist_for_each_entry(qp, n, &ipq_hash[hash], list) {
  339. if (qp->id == id &&
  340. qp->saddr == saddr &&
  341. qp->daddr == daddr &&
  342. qp->protocol == protocol &&
  343. qp->user == user) {
  344. atomic_inc(&qp->refcnt);
  345. read_unlock(&ipfrag_lock);
  346. return qp;
  347. }
  348. }
  349. read_unlock(&ipfrag_lock);
  350. return ip_frag_create(iph, user);
  351. }
  352. /* Is the fragment too far ahead to be part of ipq? */
  353. static inline int ip_frag_too_far(struct ipq *qp)
  354. {
  355. struct inet_peer *peer = qp->peer;
  356. unsigned int max = sysctl_ipfrag_max_dist;
  357. unsigned int start, end;
  358. int rc;
  359. if (!peer || !max)
  360. return 0;
  361. start = qp->rid;
  362. end = atomic_inc_return(&peer->rid);
  363. qp->rid = end;
  364. rc = qp->fragments && (end - start) > max;
  365. if (rc) {
  366. IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
  367. }
  368. return rc;
  369. }
  370. static int ip_frag_reinit(struct ipq *qp)
  371. {
  372. struct sk_buff *fp;
  373. if (!mod_timer(&qp->timer, jiffies + sysctl_ipfrag_time)) {
  374. atomic_inc(&qp->refcnt);
  375. return -ETIMEDOUT;
  376. }
  377. fp = qp->fragments;
  378. do {
  379. struct sk_buff *xp = fp->next;
  380. frag_kfree_skb(fp, NULL);
  381. fp = xp;
  382. } while (fp);
  383. qp->last_in = 0;
  384. qp->len = 0;
  385. qp->meat = 0;
  386. qp->fragments = NULL;
  387. qp->iif = 0;
  388. return 0;
  389. }
  390. /* Add new segment to existing queue. */
  391. static void ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
  392. {
  393. struct sk_buff *prev, *next;
  394. int flags, offset;
  395. int ihl, end;
  396. if (qp->last_in & COMPLETE)
  397. goto err;
  398. if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
  399. unlikely(ip_frag_too_far(qp)) && unlikely(ip_frag_reinit(qp))) {
  400. ipq_kill(qp);
  401. goto err;
  402. }
  403. offset = ntohs(ip_hdr(skb)->frag_off);
  404. flags = offset & ~IP_OFFSET;
  405. offset &= IP_OFFSET;
  406. offset <<= 3; /* offset is in 8-byte chunks */
  407. ihl = ip_hdrlen(skb);
  408. /* Determine the position of this fragment. */
  409. end = offset + skb->len - ihl;
  410. /* Is this the final fragment? */
  411. if ((flags & IP_MF) == 0) {
  412. /* If we already have some bits beyond end
  413. * or have different end, the segment is corrrupted.
  414. */
  415. if (end < qp->len ||
  416. ((qp->last_in & LAST_IN) && end != qp->len))
  417. goto err;
  418. qp->last_in |= LAST_IN;
  419. qp->len = end;
  420. } else {
  421. if (end&7) {
  422. end &= ~7;
  423. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  424. skb->ip_summed = CHECKSUM_NONE;
  425. }
  426. if (end > qp->len) {
  427. /* Some bits beyond end -> corruption. */
  428. if (qp->last_in & LAST_IN)
  429. goto err;
  430. qp->len = end;
  431. }
  432. }
  433. if (end == offset)
  434. goto err;
  435. if (pskb_pull(skb, ihl) == NULL)
  436. goto err;
  437. if (pskb_trim_rcsum(skb, end-offset))
  438. goto err;
  439. /* Find out which fragments are in front and at the back of us
  440. * in the chain of fragments so far. We must know where to put
  441. * this fragment, right?
  442. */
  443. prev = NULL;
  444. for (next = qp->fragments; next != NULL; next = next->next) {
  445. if (FRAG_CB(next)->offset >= offset)
  446. break; /* bingo! */
  447. prev = next;
  448. }
  449. /* We found where to put this one. Check for overlap with
  450. * preceding fragment, and, if needed, align things so that
  451. * any overlaps are eliminated.
  452. */
  453. if (prev) {
  454. int i = (FRAG_CB(prev)->offset + prev->len) - offset;
  455. if (i > 0) {
  456. offset += i;
  457. if (end <= offset)
  458. goto err;
  459. if (!pskb_pull(skb, i))
  460. goto err;
  461. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  462. skb->ip_summed = CHECKSUM_NONE;
  463. }
  464. }
  465. while (next && FRAG_CB(next)->offset < end) {
  466. int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
  467. if (i < next->len) {
  468. /* Eat head of the next overlapped fragment
  469. * and leave the loop. The next ones cannot overlap.
  470. */
  471. if (!pskb_pull(next, i))
  472. goto err;
  473. FRAG_CB(next)->offset += i;
  474. qp->meat -= i;
  475. if (next->ip_summed != CHECKSUM_UNNECESSARY)
  476. next->ip_summed = CHECKSUM_NONE;
  477. break;
  478. } else {
  479. struct sk_buff *free_it = next;
  480. /* Old fragment is completely overridden with
  481. * new one drop it.
  482. */
  483. next = next->next;
  484. if (prev)
  485. prev->next = next;
  486. else
  487. qp->fragments = next;
  488. qp->meat -= free_it->len;
  489. frag_kfree_skb(free_it, NULL);
  490. }
  491. }
  492. FRAG_CB(skb)->offset = offset;
  493. /* Insert this fragment in the chain of fragments. */
  494. skb->next = next;
  495. if (prev)
  496. prev->next = skb;
  497. else
  498. qp->fragments = skb;
  499. if (skb->dev)
  500. qp->iif = skb->dev->ifindex;
  501. skb->dev = NULL;
  502. qp->stamp = skb->tstamp;
  503. qp->meat += skb->len;
  504. atomic_add(skb->truesize, &ip_frag_mem);
  505. if (offset == 0)
  506. qp->last_in |= FIRST_IN;
  507. write_lock(&ipfrag_lock);
  508. list_move_tail(&qp->lru_list, &ipq_lru_list);
  509. write_unlock(&ipfrag_lock);
  510. return;
  511. err:
  512. kfree_skb(skb);
  513. }
  514. /* Build a new IP datagram from all its fragments. */
  515. static struct sk_buff *ip_frag_reasm(struct ipq *qp, struct net_device *dev)
  516. {
  517. struct iphdr *iph;
  518. struct sk_buff *fp, *head = qp->fragments;
  519. int len;
  520. int ihlen;
  521. ipq_kill(qp);
  522. BUG_TRAP(head != NULL);
  523. BUG_TRAP(FRAG_CB(head)->offset == 0);
  524. /* Allocate a new buffer for the datagram. */
  525. ihlen = ip_hdrlen(head);
  526. len = ihlen + qp->len;
  527. if (len > 65535)
  528. goto out_oversize;
  529. /* Head of list must not be cloned. */
  530. if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
  531. goto out_nomem;
  532. /* If the first fragment is fragmented itself, we split
  533. * it to two chunks: the first with data and paged part
  534. * and the second, holding only fragments. */
  535. if (skb_shinfo(head)->frag_list) {
  536. struct sk_buff *clone;
  537. int i, plen = 0;
  538. if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
  539. goto out_nomem;
  540. clone->next = head->next;
  541. head->next = clone;
  542. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  543. skb_shinfo(head)->frag_list = NULL;
  544. for (i=0; i<skb_shinfo(head)->nr_frags; i++)
  545. plen += skb_shinfo(head)->frags[i].size;
  546. clone->len = clone->data_len = head->data_len - plen;
  547. head->data_len -= clone->len;
  548. head->len -= clone->len;
  549. clone->csum = 0;
  550. clone->ip_summed = head->ip_summed;
  551. atomic_add(clone->truesize, &ip_frag_mem);
  552. }
  553. skb_shinfo(head)->frag_list = head->next;
  554. skb_push(head, head->data - skb_network_header(head));
  555. atomic_sub(head->truesize, &ip_frag_mem);
  556. for (fp=head->next; fp; fp = fp->next) {
  557. head->data_len += fp->len;
  558. head->len += fp->len;
  559. if (head->ip_summed != fp->ip_summed)
  560. head->ip_summed = CHECKSUM_NONE;
  561. else if (head->ip_summed == CHECKSUM_COMPLETE)
  562. head->csum = csum_add(head->csum, fp->csum);
  563. head->truesize += fp->truesize;
  564. atomic_sub(fp->truesize, &ip_frag_mem);
  565. }
  566. head->next = NULL;
  567. head->dev = dev;
  568. head->tstamp = qp->stamp;
  569. iph = ip_hdr(head);
  570. iph->frag_off = 0;
  571. iph->tot_len = htons(len);
  572. IP_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
  573. qp->fragments = NULL;
  574. return head;
  575. out_nomem:
  576. LIMIT_NETDEBUG(KERN_ERR "IP: queue_glue: no memory for gluing "
  577. "queue %p\n", qp);
  578. goto out_fail;
  579. out_oversize:
  580. if (net_ratelimit())
  581. printk(KERN_INFO
  582. "Oversized IP packet from %d.%d.%d.%d.\n",
  583. NIPQUAD(qp->saddr));
  584. out_fail:
  585. IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
  586. return NULL;
  587. }
  588. /* Process an incoming IP datagram fragment. */
  589. struct sk_buff *ip_defrag(struct sk_buff *skb, u32 user)
  590. {
  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(ip_hdr(skb), 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 __init 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);