📄 ip.c
字号:
/* Fill in the structure. */
fp->offset = offset;
fp->end = end;
fp->len = end - offset;
fp->skb = skb;
fp->ptr = ptr;
return(fp);
}
/*
* Find the correct entry in the "incomplete datagrams" queue for
* this IP datagram, and return the queue entry address if found.
*/
static struct ipq *ip_find(struct iphdr *iph)
{
struct ipq *qp;
struct ipq *qplast;
cli();
qplast = NULL;
for(qp = ipqueue; qp != NULL; qplast = qp, qp = qp->next)
{
if (iph->id== qp->iph->id && iph->saddr == qp->iph->saddr &&
iph->daddr == qp->iph->daddr && iph->protocol == qp->iph->protocol)
{
del_timer(&qp->timer); /* So it doesnt vanish on us. The timer will be reset anyway */
sti();
return(qp);
}
}
sti();
return(NULL);
}
/*
* Remove an entry from the "incomplete datagrams" queue, either
* because we completed, reassembled and processed it, or because
* it timed out.
*/
static void ip_free(struct ipq *qp)
{
struct ipfrag *fp;
struct ipfrag *xp;
/* Stop the timer for this entry. */
/* printk("ip_free\n");*/
del_timer(&qp->timer);
/* Remove this entry from the "incomplete datagrams" queue. */
cli();
if (qp->prev == NULL)
{
ipqueue = qp->next;
if (ipqueue != NULL)
ipqueue->prev = NULL;
}
else
{
qp->prev->next = qp->next;
if (qp->next != NULL)
qp->next->prev = qp->prev;
}
/* Release all fragment data. */
/* printk("ip_free: kill frag data\n");*/
fp = qp->fragments;
while (fp != NULL)
{
xp = fp->next;
IS_SKB(fp->skb);
kfree_skb(fp->skb,FREE_READ);
kfree_s(fp, sizeof(struct ipfrag));
fp = xp;
}
/* printk("ip_free: cleanup\n");*/
/* Release the MAC header. */
kfree_s(qp->mac, qp->maclen);
/* Release the IP header. */
kfree_s(qp->iph, qp->ihlen + 8);
/* Finally, release the queue descriptor itself. */
kfree_s(qp, sizeof(struct ipq));
/* printk("ip_free:done\n");*/
sti();
}
/* Oops- a fragment queue timed out. Kill it and send an ICMP reply. */
static void ip_expire(unsigned long arg)
{
struct ipq *qp;
qp = (struct ipq *)arg;
DPRINTF((DBG_IP, "IP: queue_expire: fragment queue 0x%X timed out!\n", qp));
/* Send an ICMP "Fragment Reassembly Timeout" message. */
#if 0
icmp_send(qp->iph->ip_src.s_addr, ICMP_TIME_EXCEEDED,
ICMP_EXC_FRAGTIME, qp->iph);
#endif
if(qp->fragments!=NULL)
icmp_send(qp->fragments->skb,ICMP_TIME_EXCEEDED,
ICMP_EXC_FRAGTIME, qp->dev);
/* Nuke the fragment queue. */
ip_free(qp);
}
/*
* Add an entry to the 'ipq' queue for a newly received IP datagram.
* We will (hopefully :-) receive all other fragments of this datagram
* in time, so we just create a queue for this datagram, in which we
* will insert the received fragments at their respective positions.
*/
static struct ipq *ip_create(struct sk_buff *skb, struct iphdr *iph, struct device *dev)
{
struct ipq *qp;
int maclen;
int ihlen;
qp = (struct ipq *) kmalloc(sizeof(struct ipq), GFP_ATOMIC);
if (qp == NULL)
{
printk("IP: create: no memory left !\n");
return(NULL);
}
memset(qp, 0, sizeof(struct ipq));
/* Allocate memory for the MAC header. */
maclen = ((unsigned long) iph) - ((unsigned long) skb->data);
qp->mac = (unsigned char *) kmalloc(maclen, GFP_ATOMIC);
if (qp->mac == NULL)
{
printk("IP: create: no memory left !\n");
kfree_s(qp, sizeof(struct ipq));
return(NULL);
}
/* Allocate memory for the IP header (plus 8 octects for ICMP). */
ihlen = (iph->ihl * sizeof(unsigned long));
qp->iph = (struct iphdr *) kmalloc(ihlen + 8, GFP_ATOMIC);
if (qp->iph == NULL)
{
printk("IP: create: no memory left !\n");
kfree_s(qp->mac, maclen);
kfree_s(qp, sizeof(struct ipq));
return(NULL);
}
/* Fill in the structure. */
memcpy(qp->mac, skb->data, maclen);
memcpy(qp->iph, iph, ihlen + 8);
qp->len = 0;
qp->ihlen = ihlen;
qp->maclen = maclen;
qp->fragments = NULL;
qp->dev = dev;
/* printk("Protocol = %d\n",qp->iph->protocol);*/
/* Start a timer for this entry. */
qp->timer.expires = IP_FRAG_TIME; /* about 30 seconds */
qp->timer.data = (unsigned long) qp; /* pointer to queue */
qp->timer.function = ip_expire; /* expire function */
add_timer(&qp->timer);
/* Add this entry to the queue. */
qp->prev = NULL;
cli();
qp->next = ipqueue;
if (qp->next != NULL)
qp->next->prev = qp;
ipqueue = qp;
sti();
return(qp);
}
/* See if a fragment queue is complete. */
static int ip_done(struct ipq *qp)
{
struct ipfrag *fp;
int offset;
/* Only possible if we received the final fragment. */
if (qp->len == 0)
return(0);
/* Check all fragment offsets to see if they connect. */
fp = qp->fragments;
offset = 0;
while (fp != NULL)
{
if (fp->offset > offset)
return(0); /* fragment(s) missing */
offset = fp->end;
fp = fp->next;
}
/* All fragments are present. */
return(1);
}
/* Build a new IP datagram from all its fragments. */
static struct sk_buff *ip_glue(struct ipq *qp)
{
struct sk_buff *skb;
struct iphdr *iph;
struct ipfrag *fp;
unsigned char *ptr;
int count, len;
/* Allocate a new buffer for the datagram. */
len = sizeof(struct sk_buff)+qp->maclen + qp->ihlen + qp->len;
if ((skb = alloc_skb(len,GFP_ATOMIC)) == NULL)
{
printk("IP: queue_glue: no memory for glueing queue 0x%X\n", (int) qp);
ip_free(qp);
return(NULL);
}
/* Fill in the basic details. */
skb->len = (len - qp->maclen);
skb->h.raw = skb->data;
skb->free = 1;
/* Copy the original MAC and IP headers into the new buffer. */
ptr = (unsigned char *) skb->h.raw;
memcpy(ptr, ((unsigned char *) qp->mac), qp->maclen);
/* printk("Copied %d bytes of mac header.\n",qp->maclen);*/
ptr += qp->maclen;
memcpy(ptr, ((unsigned char *) qp->iph), qp->ihlen);
/* printk("Copied %d byte of ip header.\n",qp->ihlen);*/
ptr += qp->ihlen;
skb->h.raw += qp->maclen;
/* printk("Protocol = %d\n",skb->h.iph->protocol);*/
count = 0;
/* Copy the data portions of all fragments into the new buffer. */
fp = qp->fragments;
while(fp != NULL)
{
if(count+fp->len>skb->len)
{
printk("Invalid fragment list: Fragment over size.\n");
ip_free(qp);
kfree_skb(skb,FREE_WRITE);
return NULL;
}
/* printk("Fragment %d size %d\n",fp->offset,fp->len);*/
memcpy((ptr + fp->offset), fp->ptr, fp->len);
count += fp->len;
fp = fp->next;
}
/* We glued together all fragments, so remove the queue entry. */
ip_free(qp);
/* Done with all fragments. Fixup the new IP header. */
iph = skb->h.iph;
iph->frag_off = 0;
iph->tot_len = htons((iph->ihl * sizeof(unsigned long)) + count);
skb->ip_hdr = iph;
return(skb);
}
/* Process an incoming IP datagram fragment. */
static struct sk_buff *ip_defrag(struct iphdr *iph, struct sk_buff *skb, struct device *dev)
{
struct ipfrag *prev, *next;
struct ipfrag *tfp;
struct ipq *qp;
struct sk_buff *skb2;
unsigned char *ptr;
int flags, offset;
int i, ihl, end;
/* Find the entry of this IP datagram in the "incomplete datagrams" queue. */
qp = ip_find(iph);
/* Is this a non-fragmented datagram? */
offset = ntohs(iph->frag_off);
flags = offset & ~IP_OFFSET;
offset &= IP_OFFSET;
if (((flags & IP_MF) == 0) && (offset == 0))
{
if (qp != NULL)
ip_free(qp); /* Huh? How could this exist?? */
return(skb);
}
offset <<= 3; /* offset is in 8-byte chunks */
/*
* If the queue already existed, keep restarting its timer as long
* as we still are receiving fragments. Otherwise, create a fresh
* queue entry.
*/
if (qp != NULL)
{
del_timer(&qp->timer);
qp->timer.expires = IP_FRAG_TIME; /* about 30 seconds */
qp->timer.data = (unsigned long) qp; /* pointer to queue */
qp->timer.function = ip_expire; /* expire function */
add_timer(&qp->timer);
}
else
{
if ((qp = ip_create(skb, iph, dev)) == NULL)
return(NULL);
}
/* Determine the position of this fragment. */
ihl = (iph->ihl * sizeof(unsigned long));
end = offset + ntohs(iph->tot_len) - ihl;
/* Point into the IP datagram 'data' part. */
ptr = skb->data + dev->hard_header_len + ihl;
/* Is this the final fragment? */
if ((flags & IP_MF) == 0)
qp->len = end;
/*
* Find out which fragments are in front and at the back of us
* in the chain of fragments so far. We must know where to put
* this fragment, right?
*/
prev = NULL;
for(next = qp->fragments; next != NULL; next = next->next)
{
if (next->offset > offset)
break; /* bingo! */
prev = next;
}
/*
* We found where to put this one.
* Check for overlap with preceeding fragment, and, if needed,
* align things so that any overlaps are eliminated.
*/
if (prev != NULL && offset < prev->end)
{
i = prev->end - offset;
offset += i; /* ptr into datagram */
ptr += i; /* ptr into fragment data */
DPRINTF((DBG_IP, "IP: defrag: fixed low overlap %d bytes\n", i));
}
/*
* Look for overlap with succeeding segments.
* If we can merge fragments, do it.
*/
for(; next != NULL; next = tfp)
{
tfp = next->next;
if (next->offset >= end)
break; /* no overlaps at all */
i = end - next->offset; /* overlap is 'i' bytes */
next->len -= i; /* so reduce size of */
next->offset += i; /* next fragment */
next->ptr += i;
/* If we get a frag size of <= 0, remove it. */
if (next->len <= 0)
{
DPRINTF((DBG_IP, "IP: defrag: removing frag 0x%X (len %d)\n",
next, next->len));
if (next->prev != NULL)
next->prev->next = next->next;
else
qp->fragments = next->next;
if (tfp->next != NULL)
next->next->prev = next->prev;
kfree_s(next, sizeof(struct ipfrag));
}
DPRINTF((DBG_IP, "IP: defrag: fixed high overlap %d bytes\n", i));
}
/* Insert this fragment in the chain of fragments. */
tfp = NULL;
tfp = ip_frag_create(offset, end, skb, ptr);
tfp->prev = prev;
tfp->next = next;
if (prev != NULL)
prev->next = tfp;
else
qp->fragments = tfp;
if (next != NULL)
next->prev = tfp;
/*
* OK, so we inserted this new fragment into the chain.
* Check if we now have a full IP datagram which we can
* bump up to the IP layer...
*/
if (ip_done(qp))
{
skb2 = ip_glue(qp); /* glue together the fragments */
return(skb2);
}
return(NULL);
}
/*
* This IP datagram is too large to be sent in one piece. Break it up into
* smaller pieces (each of size equal to the MAC header plus IP header plus
* a block of the data of the original IP data part) that will yet fit in a
* single device frame, and queue such a frame for sending by calling the
* ip_queue_xmit(). Note that this is recursion, and bad things will happen
* if this function causes a loop...
*/
void ip_fragment(struct sock *sk, struct sk_buff *skb, struct device *dev, int is_frag)
{
struct iphdr *iph;
unsigned char *raw;
unsigned char *ptr;
struct sk_buff *skb2;
int left, mtu, hlen, len;
int offset;
/* Point into the IP datagram header. */
raw = skb->data;
iph = (struct iphdr *) (raw + dev->hard_header_len);
skb->ip_hdr = iph;
/* Setup starting values. */
hlen = (iph->ihl * sizeof(unsigned long));
left = ntohs(iph->tot_len) - hlen;
hlen += dev->hard_header_len;
mtu = (dev->mtu - hlen);
ptr = (raw + hlen);
DPRINTF((DBG_IP, "IP: Fragmentation Desired\n"));
DPRINTF((DBG_IP, " DEV=%s, MTU=%d, LEN=%d SRC=%s",
dev->name, dev->mtu, left, in_ntoa(iph->saddr)));
DPRINTF((DBG_IP, " DST=%s\n", in_ntoa(iph->daddr)));
/* Check for any "DF" flag. */
if (ntohs(iph->frag_off) & IP_DF)
{
DPRINTF((DBG_IP, "IP: Fragmentation Desired, but DF set !\n"));
DPRINTF((DBG_IP, " DEV=%s, MTU=%d, LEN=%d SRC=%s",
dev->name, dev->mtu, left, in_ntoa(iph->saddr)));
DPRINTF((DBG_IP, " DST=%s\n", in_ntoa(iph->daddr)));
/*
* FIXME:
* We should send an ICMP warning message here!
*/
icmp_send(skb,ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, dev);
return;
}
/* Fragment the datagram. */
if (is_frag & 2)
offset = (ntohs(iph->frag_off) & 0x1fff) << 3;
else
offset = 0;
while(left > 0)
{
len = left;
#ifdef OLD
if (len+8 > mtu)
len = (dev->mtu - hlen - 8);
if ((left - len) >= 8)
{
len /= 8;
len *= 8;
}
#else
/* IF: it doesn't fit, use 'mtu' - the data space left */
if (len > mtu)
len = mtu;
/* IF: we are not sending upto and including the packet end
then align the next start on an eight byte boundary */
if (len < left)
{
len/=8;
len*=8;
}
#endif
DPRINTF((DBG_IP,"IP: frag: creating fragment of %d bytes (%d total)\n",
len, len + hlen));
/* Allocate buffer. */
if ((skb2 = alloc_skb(sizeof(struct sk_buff) + len + hlen,GFP_ATOMIC)) == NULL)
{
printk("IP: frag: no memory for new fragment!\n");
return;
}
skb2->arp = skb->arp;
skb2->free = skb->free;
skb2->len = len + hlen;
skb2->h.raw=(char *) skb2->data;
if (sk)
sk->wmem_alloc += skb2->mem_len;
/* Copy the packet header into the new buffer. */
memcpy(skb2->h.raw, raw, hlen);
/* Copy a block of the IP datagram. */
memcpy(skb2->h.raw + hlen, ptr, len);
left -= len;
skb2->h.raw+=dev->hard_header_len;
/* Fill in the new header fields. */
iph = (struct iphdr *)(skb2->h.raw/*+dev->hard_header_len*/);
iph->frag_off = htons((offset >> 3));
/* Added AC : If we are fragmenting a fragment thats not the
last fragment then keep MF on each bit */
if (left > 0 || (is_frag & 1))
iph->frag_off |= htons(IP_MF);
ptr += len;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -