ip_fragment.c 18 KB

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