ip_fragment.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:15k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  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.58.2.1 2002/01/12 07:53:15 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.  */
  23. #include <linux/config.h>
  24. #include <linux/types.h>
  25. #include <linux/mm.h>
  26. #include <linux/sched.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/ip.h>
  29. #include <linux/icmp.h>
  30. #include <linux/netdevice.h>
  31. #include <net/sock.h>
  32. #include <net/ip.h>
  33. #include <net/icmp.h>
  34. #include <net/checksum.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/inet.h>
  38. #include <linux/netfilter_ipv4.h>
  39. /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
  40.  * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
  41.  * as well. Or notify me, at least. --ANK
  42.  */
  43. /* Fragment cache limits. We will commit 256K at one time. Should we
  44.  * cross that limit we will prune down to 192K. This should cope with
  45.  * even the most extreme cases without allowing an attacker to measurably
  46.  * harm machine performance.
  47.  */
  48. int sysctl_ipfrag_high_thresh = 256*1024;
  49. int sysctl_ipfrag_low_thresh = 192*1024;
  50. /* Important NOTE! Fragment queue must be destroyed before MSL expires.
  51.  * RFC791 is wrong proposing to prolongate timer each fragment arrival by TTL.
  52.  */
  53. int sysctl_ipfrag_time = IP_FRAG_TIME;
  54. struct ipfrag_skb_cb
  55. {
  56. struct inet_skb_parm h;
  57. int offset;
  58. };
  59. #define FRAG_CB(skb) ((struct ipfrag_skb_cb*)((skb)->cb))
  60. /* Describe an entry in the "incomplete datagrams" queue. */
  61. struct ipq {
  62. struct ipq *next; /* linked list pointers */
  63. u32 saddr;
  64. u32 daddr;
  65. u16 id;
  66. u8 protocol;
  67. u8 last_in;
  68. #define COMPLETE 4
  69. #define FIRST_IN 2
  70. #define LAST_IN 1
  71. struct sk_buff *fragments; /* linked list of received fragments */
  72. int len; /* total length of original datagram */
  73. int meat;
  74. spinlock_t lock;
  75. atomic_t refcnt;
  76. struct timer_list timer; /* when will this queue expire? */
  77. struct ipq **pprev;
  78. int iif;
  79. struct timeval stamp;
  80. };
  81. /* Hash table. */
  82. #define IPQ_HASHSZ 64
  83. /* Per-bucket lock is easy to add now. */
  84. static struct ipq *ipq_hash[IPQ_HASHSZ];
  85. static rwlock_t ipfrag_lock = RW_LOCK_UNLOCKED;
  86. int ip_frag_nqueues = 0;
  87. static __inline__ void __ipq_unlink(struct ipq *qp)
  88. {
  89. if(qp->next)
  90. qp->next->pprev = qp->pprev;
  91. *qp->pprev = qp->next;
  92. ip_frag_nqueues--;
  93. }
  94. static __inline__ void ipq_unlink(struct ipq *ipq)
  95. {
  96. write_lock(&ipfrag_lock);
  97. __ipq_unlink(ipq);
  98. write_unlock(&ipfrag_lock);
  99. }
  100. /*
  101.  * Was: ((((id) >> 1) ^ (saddr) ^ (daddr) ^ (prot)) & (IPQ_HASHSZ - 1))
  102.  *
  103.  * I see, I see evil hand of bigendian mafia. On Intel all the packets hit
  104.  * one hash bucket with this hash function. 8)
  105.  */
  106. static __inline__ unsigned int ipqhashfn(u16 id, u32 saddr, u32 daddr, u8 prot)
  107. {
  108. unsigned int h = saddr ^ daddr;
  109. h ^= (h>>16)^id;
  110. h ^= (h>>8)^prot;
  111. return h & (IPQ_HASHSZ - 1);
  112. }
  113. atomic_t ip_frag_mem = ATOMIC_INIT(0); /* Memory used for fragments */
  114. /* Memory Tracking Functions. */
  115. static __inline__ void frag_kfree_skb(struct sk_buff *skb)
  116. {
  117. atomic_sub(skb->truesize, &ip_frag_mem);
  118. kfree_skb(skb);
  119. }
  120. static __inline__ void frag_free_queue(struct ipq *qp)
  121. {
  122. atomic_sub(sizeof(struct ipq), &ip_frag_mem);
  123. kfree(qp);
  124. }
  125. static __inline__ struct ipq *frag_alloc_queue(void)
  126. {
  127. struct ipq *qp = kmalloc(sizeof(struct ipq), GFP_ATOMIC);
  128. if(!qp)
  129. return NULL;
  130. atomic_add(sizeof(struct ipq), &ip_frag_mem);
  131. return qp;
  132. }
  133. /* Destruction primitives. */
  134. /* Complete destruction of ipq. */
  135. static void ip_frag_destroy(struct ipq *qp)
  136. {
  137. struct sk_buff *fp;
  138. BUG_TRAP(qp->last_in&COMPLETE);
  139. BUG_TRAP(del_timer(&qp->timer) == 0);
  140. /* Release all fragment data. */
  141. fp = qp->fragments;
  142. while (fp) {
  143. struct sk_buff *xp = fp->next;
  144. frag_kfree_skb(fp);
  145. fp = xp;
  146. }
  147. /* Finally, release the queue descriptor itself. */
  148. frag_free_queue(qp);
  149. }
  150. static __inline__ void ipq_put(struct ipq *ipq)
  151. {
  152. if (atomic_dec_and_test(&ipq->refcnt))
  153. ip_frag_destroy(ipq);
  154. }
  155. /* Kill ipq entry. It is not destroyed immediately,
  156.  * because caller (and someone more) holds reference count.
  157.  */
  158. static __inline__ void ipq_kill(struct ipq *ipq)
  159. {
  160. if (del_timer(&ipq->timer))
  161. atomic_dec(&ipq->refcnt);
  162. if (!(ipq->last_in & COMPLETE)) {
  163. ipq_unlink(ipq);
  164. atomic_dec(&ipq->refcnt);
  165. ipq->last_in |= COMPLETE;
  166. }
  167. }
  168. /* Memory limiting on fragments.  Evictor trashes the oldest 
  169.  * fragment queue until we are back under the low threshold.
  170.  */
  171. static void ip_evictor(void)
  172. {
  173. int i, progress;
  174. do {
  175. if (atomic_read(&ip_frag_mem) <= sysctl_ipfrag_low_thresh)
  176. return;
  177. progress = 0;
  178. /* FIXME: Make LRU queue of frag heads. -DaveM */
  179. for (i = 0; i < IPQ_HASHSZ; i++) {
  180. struct ipq *qp;
  181. if (ipq_hash[i] == NULL)
  182. continue;
  183. read_lock(&ipfrag_lock);
  184. if ((qp = ipq_hash[i]) != NULL) {
  185. /* find the oldest queue for this hash bucket */
  186. while (qp->next)
  187. qp = qp->next;
  188. atomic_inc(&qp->refcnt);
  189. read_unlock(&ipfrag_lock);
  190. spin_lock(&qp->lock);
  191. if (!(qp->last_in&COMPLETE))
  192. ipq_kill(qp);
  193. spin_unlock(&qp->lock);
  194. ipq_put(qp);
  195. IP_INC_STATS_BH(IpReasmFails);
  196. progress = 1;
  197. continue;
  198. }
  199. read_unlock(&ipfrag_lock);
  200. }
  201. } while (progress);
  202. }
  203. /*
  204.  * Oops, a fragment queue timed out.  Kill it and send an ICMP reply.
  205.  */
  206. static void ip_expire(unsigned long arg)
  207. {
  208. struct ipq *qp = (struct ipq *) arg;
  209. spin_lock(&qp->lock);
  210. if (qp->last_in & COMPLETE)
  211. goto out;
  212. ipq_kill(qp);
  213. IP_INC_STATS_BH(IpReasmTimeout);
  214. IP_INC_STATS_BH(IpReasmFails);
  215. if ((qp->last_in&FIRST_IN) && qp->fragments != NULL) {
  216. struct sk_buff *head = qp->fragments;
  217. /* Send an ICMP "Fragment Reassembly Timeout" message. */
  218. if ((head->dev = dev_get_by_index(qp->iif)) != NULL) {
  219. icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
  220. dev_put(head->dev);
  221. }
  222. }
  223. out:
  224. spin_unlock(&qp->lock);
  225. ipq_put(qp);
  226. }
  227. /* Creation primitives. */
  228. static struct ipq *ip_frag_intern(unsigned int hash, struct ipq *qp_in)
  229. {
  230. struct ipq *qp;
  231. write_lock(&ipfrag_lock);
  232. #ifdef CONFIG_SMP
  233. /* With SMP race we have to recheck hash table, because
  234.  * such entry could be created on other cpu, while we
  235.  * promoted read lock to write lock.
  236.  */
  237. for(qp = ipq_hash[hash]; qp; qp = qp->next) {
  238. if(qp->id == qp_in->id &&
  239.    qp->saddr == qp_in->saddr &&
  240.    qp->daddr == qp_in->daddr &&
  241.    qp->protocol == qp_in->protocol) {
  242. atomic_inc(&qp->refcnt);
  243. write_unlock(&ipfrag_lock);
  244. qp_in->last_in |= COMPLETE;
  245. ipq_put(qp_in);
  246. return qp;
  247. }
  248. }
  249. #endif
  250. qp = qp_in;
  251. if (!mod_timer(&qp->timer, jiffies + sysctl_ipfrag_time))
  252. atomic_inc(&qp->refcnt);
  253. atomic_inc(&qp->refcnt);
  254. if((qp->next = ipq_hash[hash]) != NULL)
  255. qp->next->pprev = &qp->next;
  256. ipq_hash[hash] = qp;
  257. qp->pprev = &ipq_hash[hash];
  258. ip_frag_nqueues++;
  259. write_unlock(&ipfrag_lock);
  260. return qp;
  261. }
  262. /* Add an entry to the 'ipq' queue for a newly received IP datagram. */
  263. static struct ipq *ip_frag_create(unsigned hash, struct iphdr *iph)
  264. {
  265. struct ipq *qp;
  266. if ((qp = frag_alloc_queue()) == NULL)
  267. goto out_nomem;
  268. qp->protocol = iph->protocol;
  269. qp->last_in = 0;
  270. qp->id = iph->id;
  271. qp->saddr = iph->saddr;
  272. qp->daddr = iph->daddr;
  273. qp->len = 0;
  274. qp->meat = 0;
  275. qp->fragments = NULL;
  276. qp->iif = 0;
  277. /* Initialize a timer for this entry. */
  278. init_timer(&qp->timer);
  279. qp->timer.data = (unsigned long) qp; /* pointer to queue */
  280. qp->timer.function = ip_expire; /* expire function */
  281. qp->lock = SPIN_LOCK_UNLOCKED;
  282. atomic_set(&qp->refcnt, 1);
  283. return ip_frag_intern(hash, qp);
  284. out_nomem:
  285. NETDEBUG(if (net_ratelimit()) printk(KERN_ERR "ip_frag_create: no memory left !n"));
  286. return NULL;
  287. }
  288. /* Find the correct entry in the "incomplete datagrams" queue for
  289.  * this IP datagram, and create new one, if nothing is found.
  290.  */
  291. static inline struct ipq *ip_find(struct iphdr *iph)
  292. {
  293. __u16 id = iph->id;
  294. __u32 saddr = iph->saddr;
  295. __u32 daddr = iph->daddr;
  296. __u8 protocol = iph->protocol;
  297. unsigned int hash = ipqhashfn(id, saddr, daddr, protocol);
  298. struct ipq *qp;
  299. read_lock(&ipfrag_lock);
  300. for(qp = ipq_hash[hash]; qp; qp = qp->next) {
  301. if(qp->id == id &&
  302.    qp->saddr == saddr &&
  303.    qp->daddr == daddr &&
  304.    qp->protocol == protocol) {
  305. atomic_inc(&qp->refcnt);
  306. read_unlock(&ipfrag_lock);
  307. return qp;
  308. }
  309. }
  310. read_unlock(&ipfrag_lock);
  311. return ip_frag_create(hash, iph);
  312. }
  313. /* Add new segment to existing queue. */
  314. static void ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
  315. {
  316. struct sk_buff *prev, *next;
  317. int flags, offset;
  318. int ihl, end;
  319. if (qp->last_in & COMPLETE)
  320. goto err;
  321.   offset = ntohs(skb->nh.iph->frag_off);
  322. flags = offset & ~IP_OFFSET;
  323. offset &= IP_OFFSET;
  324. offset <<= 3; /* offset is in 8-byte chunks */
  325.   ihl = skb->nh.iph->ihl * 4;
  326. /* Determine the position of this fragment. */
  327.   end = offset + skb->len - ihl;
  328. /* Is this the final fragment? */
  329. if ((flags & IP_MF) == 0) {
  330. /* If we already have some bits beyond end
  331.  * or have different end, the segment is corrrupted.
  332.  */
  333. if (end < qp->len ||
  334.     ((qp->last_in & LAST_IN) && end != qp->len))
  335. goto err;
  336. qp->last_in |= LAST_IN;
  337. qp->len = end;
  338. } else {
  339. if (end&7) {
  340. end &= ~7;
  341. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  342. skb->ip_summed = CHECKSUM_NONE;
  343. }
  344. if (end > qp->len) {
  345. /* Some bits beyond end -> corruption. */
  346. if (qp->last_in & LAST_IN)
  347. goto err;
  348. qp->len = end;
  349. }
  350. }
  351. if (end == offset)
  352. goto err;
  353. if (pskb_pull(skb, ihl) == NULL)
  354. goto err;
  355. if (pskb_trim(skb, end-offset))
  356. goto err;
  357. /* Find out which fragments are in front and at the back of us
  358.  * in the chain of fragments so far.  We must know where to put
  359.  * this fragment, right?
  360.  */
  361. prev = NULL;
  362. for(next = qp->fragments; next != NULL; next = next->next) {
  363. if (FRAG_CB(next)->offset >= offset)
  364. break; /* bingo! */
  365. prev = next;
  366. }
  367. /* We found where to put this one.  Check for overlap with
  368.  * preceding fragment, and, if needed, align things so that
  369.  * any overlaps are eliminated.
  370.  */
  371. if (prev) {
  372. int i = (FRAG_CB(prev)->offset + prev->len) - offset;
  373. if (i > 0) {
  374. offset += i;
  375. if (end <= offset)
  376. goto err;
  377. if (!pskb_pull(skb, i))
  378. goto err;
  379. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  380. skb->ip_summed = CHECKSUM_NONE;
  381. }
  382. }
  383. while (next && FRAG_CB(next)->offset < end) {
  384. int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
  385. if (i < next->len) {
  386. /* Eat head of the next overlapped fragment
  387.  * and leave the loop. The next ones cannot overlap.
  388.  */
  389. if (!pskb_pull(next, i))
  390. goto err;
  391. FRAG_CB(next)->offset += i;
  392. qp->meat -= i;
  393. if (next->ip_summed != CHECKSUM_UNNECESSARY)
  394. next->ip_summed = CHECKSUM_NONE;
  395. break;
  396. } else {
  397. struct sk_buff *free_it = next;
  398. /* Old fragmnet is completely overridden with
  399.  * new one drop it.
  400.  */
  401. next = next->next;
  402. if (prev)
  403. prev->next = next;
  404. else
  405. qp->fragments = next;
  406. qp->meat -= free_it->len;
  407. frag_kfree_skb(free_it);
  408. }
  409. }
  410. FRAG_CB(skb)->offset = offset;
  411. /* Insert this fragment in the chain of fragments. */
  412. skb->next = next;
  413. if (prev)
  414. prev->next = skb;
  415. else
  416. qp->fragments = skb;
  417.   if (skb->dev)
  418.   qp->iif = skb->dev->ifindex;
  419. skb->dev = NULL;
  420. qp->stamp = skb->stamp;
  421. qp->meat += skb->len;
  422. atomic_add(skb->truesize, &ip_frag_mem);
  423. if (offset == 0)
  424. qp->last_in |= FIRST_IN;
  425. return;
  426. err:
  427. kfree_skb(skb);
  428. }
  429. /* Build a new IP datagram from all its fragments. */
  430. static struct sk_buff *ip_frag_reasm(struct ipq *qp, struct net_device *dev)
  431. {
  432. struct iphdr *iph;
  433. struct sk_buff *fp, *head = qp->fragments;
  434. int len;
  435. int ihlen;
  436. ipq_kill(qp);
  437. BUG_TRAP(head != NULL);
  438. BUG_TRAP(FRAG_CB(head)->offset == 0);
  439. /* Allocate a new buffer for the datagram. */
  440. ihlen = head->nh.iph->ihl*4;
  441. len = ihlen + qp->len;
  442. if(len > 65535)
  443. goto out_oversize;
  444. /* Head of list must not be cloned. */
  445. if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
  446. goto out_nomem;
  447. /* If the first fragment is fragmented itself, we split
  448.  * it to two chunks: the first with data and paged part
  449.  * and the second, holding only fragments. */
  450. if (skb_shinfo(head)->frag_list) {
  451. struct sk_buff *clone;
  452. int i, plen = 0;
  453. if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
  454. goto out_nomem;
  455. clone->next = head->next;
  456. head->next = clone;
  457. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  458. skb_shinfo(head)->frag_list = NULL;
  459. for (i=0; i<skb_shinfo(head)->nr_frags; i++)
  460. plen += skb_shinfo(head)->frags[i].size;
  461. clone->len = clone->data_len = head->data_len - plen;
  462. head->data_len -= clone->len;
  463. head->len -= clone->len;
  464. clone->csum = 0;
  465. clone->ip_summed = head->ip_summed;
  466. atomic_add(clone->truesize, &ip_frag_mem);
  467. }
  468. skb_shinfo(head)->frag_list = head->next;
  469. skb_push(head, head->data - head->nh.raw);
  470. atomic_sub(head->truesize, &ip_frag_mem);
  471. for (fp=head->next; fp; fp = fp->next) {
  472. head->data_len += fp->len;
  473. head->len += fp->len;
  474. if (head->ip_summed != fp->ip_summed)
  475. head->ip_summed = CHECKSUM_NONE;
  476. else if (head->ip_summed == CHECKSUM_HW)
  477. head->csum = csum_add(head->csum, fp->csum);
  478. head->truesize += fp->truesize;
  479. atomic_sub(fp->truesize, &ip_frag_mem);
  480. }
  481. head->next = NULL;
  482. head->dev = dev;
  483. head->stamp = qp->stamp;
  484. iph = head->nh.iph;
  485. iph->frag_off = 0;
  486. iph->tot_len = htons(len);
  487. IP_INC_STATS_BH(IpReasmOKs);
  488. qp->fragments = NULL;
  489. return head;
  490. out_nomem:
  491.   NETDEBUG(if (net_ratelimit())
  492.          printk(KERN_ERR 
  493. "IP: queue_glue: no memory for gluing queue %pn",
  494. qp));
  495. goto out_fail;
  496. out_oversize:
  497. if (net_ratelimit())
  498. printk(KERN_INFO
  499. "Oversized IP packet from %d.%d.%d.%d.n",
  500. NIPQUAD(qp->saddr));
  501. out_fail:
  502. IP_INC_STATS_BH(IpReasmFails);
  503. return NULL;
  504. }
  505. /* Process an incoming IP datagram fragment. */
  506. struct sk_buff *ip_defrag(struct sk_buff *skb)
  507. {
  508. struct iphdr *iph = skb->nh.iph;
  509. struct ipq *qp;
  510. struct net_device *dev;
  511. IP_INC_STATS_BH(IpReasmReqds);
  512. /* Start by cleaning up the memory. */
  513. if (atomic_read(&ip_frag_mem) > sysctl_ipfrag_high_thresh)
  514. ip_evictor();
  515. dev = skb->dev;
  516. /* Lookup (or create) queue header */
  517. if ((qp = ip_find(iph)) != NULL) {
  518. struct sk_buff *ret = NULL;
  519. spin_lock(&qp->lock);
  520. ip_frag_queue(qp, skb);
  521. if (qp->last_in == (FIRST_IN|LAST_IN) &&
  522.     qp->meat == qp->len)
  523. ret = ip_frag_reasm(qp, dev);
  524. spin_unlock(&qp->lock);
  525. ipq_put(qp);
  526. return ret;
  527. }
  528. IP_INC_STATS_BH(IpReasmFails);
  529. kfree_skb(skb);
  530. return NULL;
  531. }