ip_fragment.c 17 KB

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