ip_fragment.c 14 KB

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