summaryrefslogtreecommitdiffstats
path: root/src/net/ipv4.c
diff options
context:
space:
mode:
authorNikhil Chandru Rao2006-06-30 10:52:03 +0200
committerNikhil Chandru Rao2006-06-30 10:52:03 +0200
commit5f651f862232a63ca44833041deb381f305febc6 (patch)
tree5b675a1a6d26ae34e4bb95b1d9808e3980332de9 /src/net/ipv4.c
parentfix printf format args (diff)
downloadipxe-5f651f862232a63ca44833041deb381f305febc6.tar.gz
ipxe-5f651f862232a63ca44833041deb381f305febc6.tar.xz
ipxe-5f651f862232a63ca44833041deb381f305febc6.zip
Added fragment reassembly code
Diffstat (limited to 'src/net/ipv4.c')
-rw-r--r--src/net/ipv4.c123
1 files changed, 122 insertions, 1 deletions
diff --git a/src/net/ipv4.c b/src/net/ipv4.c
index 9669b07d..19e56440 100644
--- a/src/net/ipv4.c
+++ b/src/net/ipv4.c
@@ -47,6 +47,9 @@ struct ipv4_miniroute {
/** List of IPv4 miniroutes */
static LIST_HEAD ( miniroutes );
+/** List of fragment reassembly buffers */
+static LIST_HEAD ( frag_buffers );
+
/**
* Add IPv4 interface
*
@@ -120,6 +123,110 @@ static void ipv4_dump ( struct iphdr *iphdr __unused ) {
}
/**
+ * Fragment reassembly counter timeout
+ *
+ * @v timer Retry timer
+ * @v over If asserted, the timer is greater than @c MAX_TIMEOUT
+ */
+void ipv4_frag_expired ( struct retry_timer *timer __unused , int over ) {
+ if ( over ) {
+ DBG ( "Fragment reassembly timeout" );
+ /* Free the fragment buffer */
+ }
+}
+
+/**
+ * Free fragment buffer
+ *
+ * @v fragbug Fragment buffer
+ */
+void free_fragbuf ( struct frag_buffer *fragbuf ) {
+ if ( fragbuf ) {
+ free_dma ( fragbuf, sizeof ( *fragbuf ) );
+ }
+}
+
+/**
+ * Fragment reassembler
+ *
+ * @v pkb Packet buffer, fragment of the datagram
+ * @ret frag_pkb Reassembled packet, or NULL
+ */
+struct pk_buff * ipv4_reassemble ( struct pk_buff * pkb ) {
+ struct iphdr *iphdr = pkb->data;
+ struct frag_buffer *fragbuf;
+
+ /**
+ * Check if the fragment belongs to any fragment series
+ */
+ list_for_each_entry ( fragbuf, &frag_buffers, list ) {
+ if ( fragbuf->ident == iphdr->ident &&
+ fragbuf->src.s_addr == iphdr->src.s_addr ) {
+ /**
+ * Check if the packet is the expected fragment
+ *
+ * The offset of the new packet must be equal to the
+ * length of the data accumulated so far (the length of
+ * the reassembled packet buffer
+ */
+ if ( pkb_len ( fragbuf->frag_pkb ) ==
+ ( iphdr->frags & IP_MASK_OFFSET ) ) {
+ /**
+ * Append the contents of the fragment to the
+ * reassembled packet buffer
+ */
+ pkb_pull ( pkb, sizeof ( *iphdr ) );
+ memcpy ( pkb_put ( fragbuf->frag_pkb,
+ pkb_len ( pkb ) ),
+ pkb->data, pkb_len ( pkb ) );
+ free_pkb ( pkb );
+
+ /** Check if the fragment series is over */
+ if ( !iphdr->frags & IP_MASK_MOREFRAGS ) {
+ pkb = fragbuf->frag_pkb;
+ free_fragbuf ( fragbuf );
+ return pkb;
+ }
+
+ } else {
+ /* Discard the fragment series */
+ free_fragbuf ( fragbuf );
+ free_pkb ( pkb );
+ }
+ return NULL;
+ }
+ }
+
+ /** Check if the fragment is the first in the fragment series */
+ if ( iphdr->frags & IP_MASK_MOREFRAGS &&
+ ( ( iphdr->frags & IP_MASK_OFFSET ) == 0 ) ) {
+
+ /** Create a new fragment buffer */
+ fragbuf = ( struct frag_buffer* ) malloc ( sizeof( *fragbuf ) );
+ fragbuf->ident = iphdr->ident;
+ fragbuf->src = iphdr->src;
+
+ /* Set up the reassembly packet buffer */
+ fragbuf->frag_pkb = alloc_pkb ( IP_FRAG_PKB_SIZE );
+ pkb_pull ( pkb, sizeof ( *iphdr ) );
+ memcpy ( pkb_put ( fragbuf->frag_pkb, pkb_len ( pkb ) ),
+ pkb->data, pkb_len ( pkb ) );
+ free_pkb ( pkb );
+
+ /* Set the reassembly timer */
+ fragbuf->frag_timer.timeout = IP_FRAG_TIMEOUT;
+ fragbuf->frag_timer.expired = ipv4_frag_expired;
+ start_timer ( &fragbuf->frag_timer );
+
+ /* Add the fragment buffer to the list of fragment buffers */
+ list_add ( &fragbuf->list, &frag_buffers );
+ }
+
+ return NULL;
+}
+
+
+/**
* Complete the transport-layer checksum
*
* @v pkb Packet buffer
@@ -294,7 +401,9 @@ int ipv4_tx ( struct pk_buff *pkb, struct tcpip_protocol *tcpip,
}
/* Calculate the transport layer checksum */
- ipv4_tx_csum ( pkb, tcpip );
+ if ( tcpip->csum_offset > 0 ) {
+ ipv4_tx_csum ( pkb, tcpip );
+ }
/* Calculate header checksum, in network byte order */
iphdr->chksum = 0;
@@ -416,6 +525,18 @@ void ipv4_rx ( struct pk_buff *pkb, struct net_device *netdev __unused,
if ( ( chksum = ipv4_rx_csum ( pkb, iphdr->protocol ) ) != 0xffff ) {
DBG ( "Bad checksum %x\n", chksum );
}
+ /* Fragment reassembly */
+ if ( iphdr->frags & IP_MASK_MOREFRAGS ||
+ ( !iphdr->frags & IP_MASK_MOREFRAGS &&
+ iphdr->frags & IP_MASK_OFFSET != 0 ) ) {
+ /* Pass the fragment to the reassembler ipv4_ressable() which
+ * either returns a fully reassembled packet buffer or NULL.
+ */
+ pkb = ipv4_reassemble ( pkb );
+ if ( !pkb ) {
+ return;
+ }
+ }
/* To reduce code size, the following functions are not implemented:
* 1. Check the destination address