ip_fragment.c 18 KB

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