ip_fragment.c 18 KB

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