2006-04-24 15:38:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
2006-05-27 13:43:56 +00:00
|
|
|
#include <errno.h>
|
2006-04-24 15:38:53 +00:00
|
|
|
#include <byteswap.h>
|
2006-06-16 00:19:46 +00:00
|
|
|
#include <malloc.h>
|
2006-04-28 14:13:50 +00:00
|
|
|
#include <vsprintf.h>
|
2006-06-16 00:19:46 +00:00
|
|
|
#include <gpxe/list.h>
|
2006-04-24 15:38:53 +00:00
|
|
|
#include <gpxe/in.h>
|
2006-06-16 00:19:46 +00:00
|
|
|
#include <gpxe/arp.h>
|
2006-04-24 15:38:53 +00:00
|
|
|
#include <gpxe/if_ether.h>
|
|
|
|
#include <gpxe/pkbuff.h>
|
|
|
|
#include <gpxe/netdevice.h>
|
2006-04-30 01:16:37 +00:00
|
|
|
#include "uip/uip.h"
|
2006-06-16 00:19:46 +00:00
|
|
|
#include <gpxe/ip.h>
|
2006-08-01 14:18:09 +00:00
|
|
|
#include <gpxe/tcpip.h>
|
2006-04-24 15:38:53 +00:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* IPv4 protocol
|
|
|
|
*
|
|
|
|
* The gPXE IP stack is currently implemented on top of the uIP
|
|
|
|
* protocol stack. This file provides wrappers around uIP so that
|
|
|
|
* higher-level protocol implementations do not need to talk directly
|
|
|
|
* to uIP (which has a somewhat baroque API).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-06-25 05:16:54 +00:00
|
|
|
/* Unique IP datagram identification number */
|
|
|
|
static uint16_t next_ident = 0;
|
|
|
|
|
2006-05-27 13:43:56 +00:00
|
|
|
struct net_protocol ipv4_protocol;
|
|
|
|
|
2006-06-16 00:19:46 +00:00
|
|
|
/** An IPv4 address/routing table entry */
|
|
|
|
struct ipv4_miniroute {
|
|
|
|
/** List of miniroutes */
|
|
|
|
struct list_head list;
|
|
|
|
/** Network device */
|
|
|
|
struct net_device *netdev;
|
|
|
|
/** IPv4 address */
|
|
|
|
struct in_addr address;
|
2006-04-24 15:38:53 +00:00
|
|
|
/** Subnet mask */
|
|
|
|
struct in_addr netmask;
|
|
|
|
/** Gateway address */
|
|
|
|
struct in_addr gateway;
|
|
|
|
};
|
|
|
|
|
2006-06-16 00:19:46 +00:00
|
|
|
/** List of IPv4 miniroutes */
|
|
|
|
static LIST_HEAD ( miniroutes );
|
2006-04-24 15:38:53 +00:00
|
|
|
|
2006-06-30 08:52:03 +00:00
|
|
|
/** List of fragment reassembly buffers */
|
|
|
|
static LIST_HEAD ( frag_buffers );
|
|
|
|
|
2006-04-24 15:38:53 +00:00
|
|
|
/**
|
2006-06-16 00:19:46 +00:00
|
|
|
* Add IPv4 interface
|
2006-04-24 15:38:53 +00:00
|
|
|
*
|
2006-06-16 00:19:46 +00:00
|
|
|
* @v netdev Network device
|
|
|
|
* @v address IPv4 address
|
|
|
|
* @v netmask Subnet mask
|
|
|
|
* @v gateway Gateway address (or @c INADDR_NONE for no gateway)
|
|
|
|
* @ret rc Return status code
|
2006-04-24 15:38:53 +00:00
|
|
|
*
|
|
|
|
*/
|
2006-06-16 00:19:46 +00:00
|
|
|
int add_ipv4_address ( struct net_device *netdev, struct in_addr address,
|
|
|
|
struct in_addr netmask, struct in_addr gateway ) {
|
|
|
|
struct ipv4_miniroute *miniroute;
|
|
|
|
|
|
|
|
/* Allocate and populate miniroute structure */
|
|
|
|
miniroute = malloc ( sizeof ( *miniroute ) );
|
|
|
|
if ( ! miniroute )
|
|
|
|
return -ENOMEM;
|
|
|
|
miniroute->netdev = netdev;
|
|
|
|
miniroute->address = address;
|
|
|
|
miniroute->netmask = netmask;
|
|
|
|
miniroute->gateway = gateway;
|
|
|
|
|
|
|
|
/* Add to end of list if we have a gateway, otherwise to start
|
|
|
|
* of list.
|
|
|
|
*/
|
|
|
|
if ( gateway.s_addr != INADDR_NONE ) {
|
|
|
|
list_add_tail ( &miniroute->list, &miniroutes );
|
|
|
|
} else {
|
|
|
|
list_add ( &miniroute->list, &miniroutes );
|
|
|
|
}
|
|
|
|
return 0;
|
2006-04-24 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-06-16 00:19:46 +00:00
|
|
|
* Remove IPv4 interface
|
2006-04-24 15:38:53 +00:00
|
|
|
*
|
2006-06-16 00:19:46 +00:00
|
|
|
* @v netdev Network device
|
2006-04-24 15:38:53 +00:00
|
|
|
*/
|
2006-06-16 00:19:46 +00:00
|
|
|
void del_ipv4_address ( struct net_device *netdev ) {
|
|
|
|
struct ipv4_miniroute *miniroute;
|
2006-04-24 15:38:53 +00:00
|
|
|
|
2006-06-16 00:19:46 +00:00
|
|
|
list_for_each_entry ( miniroute, &miniroutes, list ) {
|
|
|
|
if ( miniroute->netdev == netdev ) {
|
|
|
|
list_del ( &miniroute->list );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-04-24 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
2006-06-26 16:01:24 +00:00
|
|
|
/**
|
|
|
|
* Dump IPv4 packet header
|
|
|
|
*
|
|
|
|
* @v iphdr IPv4 header
|
|
|
|
*/
|
|
|
|
static void ipv4_dump ( struct iphdr *iphdr __unused ) {
|
2006-06-28 15:43:08 +00:00
|
|
|
|
2006-07-20 00:01:50 +00:00
|
|
|
DBG ( "IP4 header at %p+%#zx\n", iphdr, sizeof ( *iphdr ) );
|
2006-06-26 16:01:24 +00:00
|
|
|
DBG ( "\tVersion = %d\n", ( iphdr->verhdrlen & IP_MASK_VER ) / 16 );
|
|
|
|
DBG ( "\tHeader length = %d\n", iphdr->verhdrlen & IP_MASK_HLEN );
|
|
|
|
DBG ( "\tService = %d\n", iphdr->service );
|
2006-06-28 09:10:35 +00:00
|
|
|
DBG ( "\tTotal length = %d\n", ntohs ( iphdr->len ) );
|
|
|
|
DBG ( "\tIdent = %d\n", ntohs ( iphdr->ident ) );
|
|
|
|
DBG ( "\tFrags/Offset = %d\n", ntohs ( iphdr->frags ) );
|
2006-06-26 16:01:24 +00:00
|
|
|
DBG ( "\tIP TTL = %d\n", iphdr->ttl );
|
|
|
|
DBG ( "\tProtocol = %d\n", iphdr->protocol );
|
2006-06-28 09:10:35 +00:00
|
|
|
DBG ( "\tHeader Checksum (at %p) = %x\n", &iphdr->chksum,
|
|
|
|
ntohs ( iphdr->chksum ) );
|
2006-06-26 16:01:24 +00:00
|
|
|
DBG ( "\tSource = %s\n", inet_ntoa ( iphdr->src ) );
|
|
|
|
DBG ( "\tDestination = %s\n", inet_ntoa ( iphdr->dest ) );
|
|
|
|
}
|
|
|
|
|
2006-06-30 08:52:03 +00:00
|
|
|
/**
|
|
|
|
* Fragment reassembly counter timeout
|
|
|
|
*
|
|
|
|
* @v timer Retry timer
|
|
|
|
* @v over If asserted, the timer is greater than @c MAX_TIMEOUT
|
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
static void ipv4_frag_expired ( struct retry_timer *timer __unused,
|
|
|
|
int over ) {
|
2006-06-30 08:52:03 +00:00
|
|
|
if ( over ) {
|
|
|
|
DBG ( "Fragment reassembly timeout" );
|
|
|
|
/* Free the fragment buffer */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free fragment buffer
|
|
|
|
*
|
|
|
|
* @v fragbug Fragment buffer
|
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
static void free_fragbuf ( struct frag_buffer *fragbuf ) {
|
2006-06-30 08:52:03 +00:00
|
|
|
if ( fragbuf ) {
|
|
|
|
free_dma ( fragbuf, sizeof ( *fragbuf ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fragment reassembler
|
|
|
|
*
|
|
|
|
* @v pkb Packet buffer, fragment of the datagram
|
|
|
|
* @ret frag_pkb Reassembled packet, or NULL
|
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
static struct pk_buff * ipv4_reassemble ( struct pk_buff * pkb ) {
|
2006-06-30 08:52:03 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-25 05:16:54 +00:00
|
|
|
/**
|
|
|
|
* Complete the transport-layer checksum
|
2006-06-28 15:43:08 +00:00
|
|
|
*
|
|
|
|
* @v pkb Packet buffer
|
|
|
|
* @v tcpip Transport-layer protocol
|
|
|
|
*
|
|
|
|
* This function calculates the tcpip
|
2006-06-25 05:16:54 +00:00
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
static void ipv4_tx_csum ( struct pk_buff *pkb,
|
|
|
|
struct tcpip_protocol *tcpip ) {
|
2006-06-26 13:45:24 +00:00
|
|
|
struct iphdr *iphdr = pkb->data;
|
2006-06-28 09:59:27 +00:00
|
|
|
struct ipv4_pseudo_header pshdr;
|
2006-07-19 23:38:05 +00:00
|
|
|
uint16_t *csum = ( ( ( void * ) iphdr ) + sizeof ( *iphdr )
|
|
|
|
+ tcpip->csum_offset );
|
2006-06-26 13:45:24 +00:00
|
|
|
|
|
|
|
/* Calculate pseudo header */
|
2006-06-28 09:59:27 +00:00
|
|
|
pshdr.src = iphdr->src;
|
|
|
|
pshdr.dest = iphdr->dest;
|
|
|
|
pshdr.zero_padding = 0x00;
|
|
|
|
pshdr.protocol = iphdr->protocol;
|
2006-08-07 07:37:23 +00:00
|
|
|
/* This is only valid when IPv4 does not have options */
|
2006-06-28 09:59:27 +00:00
|
|
|
pshdr.len = htons ( pkb_len ( pkb ) - sizeof ( *iphdr ) );
|
2006-06-25 05:16:54 +00:00
|
|
|
|
|
|
|
/* Update the checksum value */
|
2006-07-19 23:38:05 +00:00
|
|
|
*csum = tcpip_continue_chksum ( *csum, &pshdr, sizeof ( pshdr ) );
|
2006-06-25 05:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the transport-layer checksum while processing packets
|
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
static uint16_t ipv4_rx_csum ( struct pk_buff *pkb __unused,
|
|
|
|
uint8_t trans_proto __unused ) {
|
2006-06-28 15:43:08 +00:00
|
|
|
/**
|
|
|
|
* This function needs to be implemented. Until then, it will return
|
|
|
|
* 0xffffffff every time
|
|
|
|
*/
|
2006-06-25 05:16:54 +00:00
|
|
|
return 0xffff;
|
|
|
|
}
|
|
|
|
|
2006-04-24 15:38:53 +00:00
|
|
|
/**
|
2006-08-02 00:02:21 +00:00
|
|
|
* Transmit IP packet
|
2006-06-25 05:16:54 +00:00
|
|
|
*
|
|
|
|
* @v pkb Packet buffer
|
2006-06-28 15:43:08 +00:00
|
|
|
* @v tcpip Transport-layer protocol
|
2006-08-02 00:02:21 +00:00
|
|
|
* @v st_dest Destination network-layer address
|
2006-06-25 05:16:54 +00:00
|
|
|
* @ret rc Status
|
|
|
|
*
|
|
|
|
* This function expects a transport-layer segment and prepends the IP header
|
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
static int ipv4_tx ( struct pk_buff *pkb,
|
|
|
|
struct tcpip_protocol *tcpip_protocol,
|
|
|
|
struct sockaddr_tcpip *st_dest ) {
|
2006-06-28 09:10:35 +00:00
|
|
|
struct iphdr *iphdr = pkb_push ( pkb, sizeof ( *iphdr ) );
|
2006-08-02 00:02:21 +00:00
|
|
|
struct sockaddr_in *sin_dest = ( ( struct sockaddr_in * ) st_dest );
|
2006-06-26 13:45:24 +00:00
|
|
|
struct ipv4_miniroute *miniroute;
|
|
|
|
struct net_device *netdev = NULL;
|
|
|
|
struct in_addr next_hop;
|
|
|
|
uint8_t ll_dest_buf[MAX_LL_ADDR_LEN];
|
|
|
|
const uint8_t *ll_dest = ll_dest_buf;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Fill up the IP header, except source address */
|
2006-06-28 09:10:35 +00:00
|
|
|
iphdr->verhdrlen = ( IP_VER << 4 ) | ( sizeof ( *iphdr ) / 4 );
|
|
|
|
iphdr->service = IP_TOS;
|
|
|
|
iphdr->len = htons ( pkb_len ( pkb ) );
|
|
|
|
iphdr->ident = htons ( next_ident++ );
|
|
|
|
iphdr->frags = 0;
|
|
|
|
iphdr->ttl = IP_TTL;
|
2006-08-02 00:02:21 +00:00
|
|
|
iphdr->protocol = tcpip_protocol->tcpip_proto;
|
2006-06-25 05:16:54 +00:00
|
|
|
|
|
|
|
/* Copy destination address */
|
2006-08-02 00:02:21 +00:00
|
|
|
iphdr->dest = sin_dest->sin_addr;
|
2006-06-25 05:16:54 +00:00
|
|
|
|
|
|
|
/**
|
2006-06-28 09:10:35 +00:00
|
|
|
* All fields in the IP header filled in except the source network
|
|
|
|
* address (which requires routing) and the header checksum (which
|
|
|
|
* requires the source network address). As the pseudo header requires
|
|
|
|
* the source address as well and the transport-layer checksum is
|
|
|
|
* updated after routing.
|
2006-06-25 05:16:54 +00:00
|
|
|
*/
|
|
|
|
|
2006-06-26 13:45:24 +00:00
|
|
|
/* Use routing table to identify next hop and transmitting netdev */
|
|
|
|
next_hop = iphdr->dest;
|
|
|
|
list_for_each_entry ( miniroute, &miniroutes, list ) {
|
|
|
|
if ( ( ( ( iphdr->dest.s_addr ^ miniroute->address.s_addr ) &
|
|
|
|
miniroute->netmask.s_addr ) == 0 ) ||
|
|
|
|
( miniroute->gateway.s_addr != INADDR_NONE ) ) {
|
|
|
|
netdev = miniroute->netdev;
|
2006-06-26 16:10:34 +00:00
|
|
|
iphdr->src = miniroute->address;
|
2006-06-26 13:45:24 +00:00
|
|
|
if ( miniroute->gateway.s_addr != INADDR_NONE )
|
|
|
|
next_hop = miniroute->gateway;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Abort if no network device identified */
|
|
|
|
if ( ! netdev ) {
|
|
|
|
DBG ( "No route to %s\n", inet_ntoa ( iphdr->dest ) );
|
|
|
|
rc = -EHOSTUNREACH;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the transport layer checksum */
|
2006-08-02 00:02:21 +00:00
|
|
|
if ( tcpip_protocol->csum_offset > 0 ) {
|
|
|
|
ipv4_tx_csum ( pkb, tcpip_protocol );
|
2006-06-30 08:52:03 +00:00
|
|
|
}
|
2006-06-26 13:45:24 +00:00
|
|
|
|
2006-06-28 09:10:35 +00:00
|
|
|
/* Calculate header checksum, in network byte order */
|
|
|
|
iphdr->chksum = 0;
|
2006-07-19 23:38:05 +00:00
|
|
|
iphdr->chksum = tcpip_chksum ( iphdr, sizeof ( *iphdr ) );
|
2006-06-28 09:10:35 +00:00
|
|
|
|
2006-06-26 13:45:24 +00:00
|
|
|
/* Print IP4 header for debugging */
|
2006-06-26 16:01:24 +00:00
|
|
|
ipv4_dump ( iphdr );
|
2006-06-26 13:45:24 +00:00
|
|
|
|
|
|
|
/* Determine link-layer destination address */
|
|
|
|
if ( next_hop.s_addr == INADDR_BROADCAST ) {
|
|
|
|
/* Broadcast address */
|
|
|
|
ll_dest = netdev->ll_protocol->ll_broadcast;
|
|
|
|
} else if ( IN_MULTICAST ( next_hop.s_addr ) ) {
|
|
|
|
/* Special case: IPv4 multicast over Ethernet. This
|
|
|
|
* code may need to be generalised once we find out
|
|
|
|
* what happens for other link layers.
|
|
|
|
*/
|
|
|
|
uint8_t *next_hop_bytes = ( uint8_t * ) &next_hop;
|
|
|
|
ll_dest_buf[0] = 0x01;
|
|
|
|
ll_dest_buf[0] = 0x00;
|
|
|
|
ll_dest_buf[0] = 0x5e;
|
|
|
|
ll_dest_buf[3] = next_hop_bytes[1] & 0x7f;
|
|
|
|
ll_dest_buf[4] = next_hop_bytes[2];
|
|
|
|
ll_dest_buf[5] = next_hop_bytes[3];
|
|
|
|
} else {
|
|
|
|
/* Unicast address: resolve via ARP */
|
2006-06-26 16:10:34 +00:00
|
|
|
if ( ( rc = arp_resolve ( netdev, &ipv4_protocol, &next_hop,
|
|
|
|
&iphdr->src, ll_dest_buf ) ) != 0 ) {
|
|
|
|
DBG ( "No ARP entry for %s\n",
|
|
|
|
inet_ntoa ( iphdr->dest ) );
|
2006-06-26 13:45:24 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2006-06-25 05:16:54 +00:00
|
|
|
}
|
2006-06-26 13:45:24 +00:00
|
|
|
|
|
|
|
/* Hand off to link layer */
|
|
|
|
return net_tx ( pkb, netdev, &ipv4_protocol, ll_dest );
|
2006-06-25 05:16:54 +00:00
|
|
|
|
|
|
|
err:
|
2006-06-26 13:45:24 +00:00
|
|
|
free_pkb ( pkb );
|
|
|
|
return rc;
|
2006-06-25 05:16:54 +00:00
|
|
|
}
|
|
|
|
|
2006-04-24 15:38:53 +00:00
|
|
|
/**
|
2006-08-02 00:02:21 +00:00
|
|
|
* Process incoming packets
|
2006-06-25 05:16:54 +00:00
|
|
|
*
|
|
|
|
* @v pkb Packet buffer
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v ll_source Link-layer destination source
|
|
|
|
*
|
2006-06-28 09:10:35 +00:00
|
|
|
* This function expects an IP4 network datagram. It processes the headers
|
|
|
|
* and sends it to the transport layer.
|
2006-06-25 05:16:54 +00:00
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
static int ipv4_rx ( struct pk_buff *pkb, struct net_device *netdev __unused,
|
|
|
|
const void *ll_source __unused ) {
|
2006-06-26 13:45:24 +00:00
|
|
|
struct iphdr *iphdr = pkb->data;
|
2006-08-02 00:02:21 +00:00
|
|
|
union {
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
struct sockaddr_tcpip st;
|
|
|
|
} src, dest;
|
2006-06-25 05:16:54 +00:00
|
|
|
uint16_t chksum;
|
|
|
|
|
2006-06-28 09:10:35 +00:00
|
|
|
/* Sanity check */
|
|
|
|
if ( pkb_len ( pkb ) < sizeof ( *iphdr ) ) {
|
|
|
|
DBG ( "IP datagram too short (%d bytes)\n",
|
|
|
|
pkb_len ( pkb ) );
|
2006-08-02 00:02:21 +00:00
|
|
|
return -EINVAL;
|
2006-06-28 09:10:35 +00:00
|
|
|
}
|
|
|
|
|
2006-06-26 13:45:24 +00:00
|
|
|
/* Print IP4 header for debugging */
|
2006-06-26 16:01:24 +00:00
|
|
|
ipv4_dump ( iphdr );
|
2006-06-26 13:45:24 +00:00
|
|
|
|
2006-06-28 09:10:35 +00:00
|
|
|
/* Validate version and header length */
|
2006-06-25 05:16:54 +00:00
|
|
|
if ( iphdr->verhdrlen != 0x45 ) {
|
|
|
|
DBG ( "Bad version and header length %x\n", iphdr->verhdrlen );
|
2006-08-02 00:02:21 +00:00
|
|
|
return -EINVAL;
|
2006-06-25 05:16:54 +00:00
|
|
|
}
|
|
|
|
|
2006-06-28 09:10:35 +00:00
|
|
|
/* Validate length of IP packet */
|
2006-08-02 00:02:21 +00:00
|
|
|
if ( ntohs ( iphdr->len ) > pkb_len ( pkb ) ) {
|
2006-06-28 09:10:35 +00:00
|
|
|
DBG ( "Inconsistent packet length %d\n",
|
2006-08-02 00:02:21 +00:00
|
|
|
ntohs ( iphdr->len ) );
|
|
|
|
return -EINVAL;
|
2006-06-25 05:16:54 +00:00
|
|
|
}
|
|
|
|
|
2006-06-28 09:10:35 +00:00
|
|
|
/* Verify the checksum */
|
|
|
|
if ( ( chksum = ipv4_rx_csum ( pkb, iphdr->protocol ) ) != 0xffff ) {
|
2006-06-25 05:16:54 +00:00
|
|
|
DBG ( "Bad checksum %x\n", chksum );
|
|
|
|
}
|
2006-06-30 08:52:03 +00:00
|
|
|
/* 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 ) {
|
2006-08-02 00:02:21 +00:00
|
|
|
return 0;
|
2006-06-30 08:52:03 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-25 05:16:54 +00:00
|
|
|
|
|
|
|
/* To reduce code size, the following functions are not implemented:
|
|
|
|
* 1. Check the destination address
|
|
|
|
* 2. Check the TTL field
|
|
|
|
* 3. Check the service field
|
|
|
|
*/
|
|
|
|
|
2006-08-02 00:02:21 +00:00
|
|
|
/* Construct socket addresses */
|
|
|
|
memset ( &src, 0, sizeof ( src ) );
|
|
|
|
src.sin.sin_family = AF_INET;
|
|
|
|
src.sin.sin_addr = iphdr->src;
|
|
|
|
memset ( &dest, 0, sizeof ( dest ) );
|
|
|
|
dest.sin.sin_family = AF_INET;
|
|
|
|
dest.sin.sin_addr = iphdr->dest;
|
|
|
|
|
2006-06-26 13:45:24 +00:00
|
|
|
/* Strip header */
|
2006-06-28 09:10:35 +00:00
|
|
|
pkb_pull ( pkb, sizeof ( *iphdr ) );
|
2006-06-25 05:16:54 +00:00
|
|
|
|
2006-06-26 13:45:24 +00:00
|
|
|
/* Send it to the transport layer */
|
2006-08-02 00:02:21 +00:00
|
|
|
return tcpip_rx ( pkb, iphdr->protocol, &src.st, &dest.st );
|
2006-06-25 05:16:54 +00:00
|
|
|
}
|
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
/**
|
|
|
|
* Check existence of IPv4 address for ARP
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v net_addr Network-layer address
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int ipv4_arp_check ( struct net_device *netdev, const void *net_addr ) {
|
|
|
|
const struct in_addr *address = net_addr;
|
|
|
|
struct ipv4_miniroute *miniroute;
|
|
|
|
|
|
|
|
list_for_each_entry ( miniroute, &miniroutes, list ) {
|
|
|
|
if ( ( miniroute->netdev == netdev ) &&
|
|
|
|
( miniroute->address.s_addr == address->s_addr ) ) {
|
|
|
|
/* Found matching address */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2006-04-24 15:38:53 +00:00
|
|
|
/**
|
2006-06-16 00:19:46 +00:00
|
|
|
* Convert IPv4 address to dotted-quad notation
|
2006-04-24 15:38:53 +00:00
|
|
|
*
|
2006-06-16 00:19:46 +00:00
|
|
|
* @v in IP address
|
|
|
|
* @ret string IP address in dotted-quad notation
|
2006-04-24 15:38:53 +00:00
|
|
|
*/
|
2006-06-16 00:19:46 +00:00
|
|
|
char * inet_ntoa ( struct in_addr in ) {
|
|
|
|
static char buf[16]; /* "xxx.xxx.xxx.xxx" */
|
|
|
|
uint8_t *bytes = ( uint8_t * ) ∈
|
|
|
|
|
|
|
|
sprintf ( buf, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3] );
|
|
|
|
return buf;
|
2006-04-24 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
2006-04-28 14:13:50 +00:00
|
|
|
/**
|
|
|
|
* Transcribe IP address
|
|
|
|
*
|
|
|
|
* @v net_addr IP address
|
|
|
|
* @ret string IP address in dotted-quad notation
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static const char * ipv4_ntoa ( const void *net_addr ) {
|
2006-06-16 00:19:46 +00:00
|
|
|
return inet_ntoa ( * ( ( struct in_addr * ) net_addr ) );
|
2006-04-28 14:13:50 +00:00
|
|
|
}
|
|
|
|
|
2006-04-24 15:38:53 +00:00
|
|
|
/** IPv4 protocol */
|
|
|
|
struct net_protocol ipv4_protocol = {
|
2006-04-28 14:13:50 +00:00
|
|
|
.name = "IP",
|
|
|
|
.net_proto = htons ( ETH_P_IP ),
|
2006-04-24 15:38:53 +00:00
|
|
|
.net_addr_len = sizeof ( struct in_addr ),
|
2006-06-17 22:36:27 +00:00
|
|
|
.rx = ipv4_rx,
|
2006-04-28 14:13:50 +00:00
|
|
|
.ntoa = ipv4_ntoa,
|
2006-04-24 15:38:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
NET_PROTOCOL ( ipv4_protocol );
|
|
|
|
|
2006-06-28 15:43:08 +00:00
|
|
|
/** IPv4 TCPIP net protocol */
|
|
|
|
struct tcpip_net_protocol ipv4_tcpip_protocol = {
|
2006-08-02 00:02:21 +00:00
|
|
|
.name = "IPv4",
|
2006-08-01 20:27:26 +00:00
|
|
|
.sa_family = AF_INET,
|
|
|
|
.tx = ipv4_tx,
|
2006-06-28 15:43:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TCPIP_NET_PROTOCOL ( ipv4_tcpip_protocol );
|
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
/** IPv4 ARP protocol */
|
|
|
|
struct arp_net_protocol ipv4_arp_protocol = {
|
2006-04-24 15:38:53 +00:00
|
|
|
.net_protocol = &ipv4_protocol,
|
2006-06-17 22:36:27 +00:00
|
|
|
.check = ipv4_arp_check,
|
2006-04-24 15:38:53 +00:00
|
|
|
};
|
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
ARP_NET_PROTOCOL ( ipv4_arp_protocol );
|