ip_fragment.c 17 KB

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