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-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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
/** List of IPv4 miniroutes */
|
2007-01-10 20:38:20 +00:00
|
|
|
struct list_head ipv4_miniroutes = LIST_HEAD_INIT ( ipv4_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 );
|
|
|
|
|
2007-01-04 03:10:21 +00:00
|
|
|
static void ipv4_forget_netdev ( struct reference *ref );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add IPv4 minirouting table entry
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v address IPv4 address
|
|
|
|
* @v netmask Subnet mask
|
|
|
|
* @v gateway Gateway address (or @c INADDR_NONE for no gateway)
|
|
|
|
* @ret miniroute Routing table entry, or NULL
|
|
|
|
*/
|
|
|
|
static struct ipv4_miniroute * add_ipv4_miniroute ( 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 ) {
|
|
|
|
|
|
|
|
DBG ( "IPv4 add %s", inet_ntoa ( address ) );
|
|
|
|
DBG ( "/%s ", inet_ntoa ( netmask ) );
|
|
|
|
if ( gateway.s_addr != INADDR_NONE )
|
|
|
|
DBG ( "gw %s ", inet_ntoa ( gateway ) );
|
2007-01-09 23:48:18 +00:00
|
|
|
DBG ( "via %s\n", netdev->name );
|
2007-01-04 03:10:21 +00:00
|
|
|
|
|
|
|
/* Record routing information */
|
|
|
|
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 ) {
|
2007-01-10 20:38:20 +00:00
|
|
|
list_add_tail ( &miniroute->list, &ipv4_miniroutes );
|
2007-01-04 03:10:21 +00:00
|
|
|
} else {
|
2007-01-10 20:38:20 +00:00
|
|
|
list_add ( &miniroute->list, &ipv4_miniroutes );
|
2007-01-04 03:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Record reference to net_device */
|
|
|
|
miniroute->netdev_ref.forget = ipv4_forget_netdev;
|
|
|
|
ref_add ( &miniroute->netdev_ref, &netdev->references );
|
|
|
|
}
|
|
|
|
|
|
|
|
return miniroute;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete IPv4 minirouting table entry
|
|
|
|
*
|
|
|
|
* @v miniroute Routing table entry
|
|
|
|
*/
|
|
|
|
static void del_ipv4_miniroute ( struct ipv4_miniroute *miniroute ) {
|
|
|
|
|
|
|
|
DBG ( "IPv4 del %s", inet_ntoa ( miniroute->address ) );
|
|
|
|
DBG ( "/%s ", inet_ntoa ( miniroute->netmask ) );
|
|
|
|
if ( miniroute->gateway.s_addr != INADDR_NONE )
|
|
|
|
DBG ( "gw %s ", inet_ntoa ( miniroute->gateway ) );
|
2007-01-09 23:48:18 +00:00
|
|
|
DBG ( "via %s\n", miniroute->netdev->name );
|
2007-01-04 03:10:21 +00:00
|
|
|
|
|
|
|
ref_del ( &miniroute->netdev_ref );
|
|
|
|
list_del ( &miniroute->list );
|
|
|
|
free ( miniroute );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forget reference to net_device
|
|
|
|
*
|
|
|
|
* @v ref Persistent reference
|
|
|
|
*/
|
|
|
|
static void ipv4_forget_netdev ( struct reference *ref ) {
|
|
|
|
struct ipv4_miniroute *miniroute
|
|
|
|
= container_of ( ref, struct ipv4_miniroute, netdev_ref );
|
|
|
|
|
|
|
|
del_ipv4_miniroute ( miniroute );
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2007-01-04 03:10:21 +00:00
|
|
|
/* Clear any existing address for this net device */
|
|
|
|
del_ipv4_address ( netdev );
|
|
|
|
|
|
|
|
/* Add new miniroute */
|
|
|
|
miniroute = add_ipv4_miniroute ( netdev, address, netmask, gateway );
|
2006-06-16 00:19:46 +00:00
|
|
|
if ( ! miniroute )
|
|
|
|
return -ENOMEM;
|
2007-01-04 03:10:21 +00:00
|
|
|
|
2006-06-16 00:19:46 +00:00
|
|
|
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
|
|
|
|
2007-01-10 20:38:20 +00:00
|
|
|
list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
|
2006-06-16 00:19:46 +00:00
|
|
|
if ( miniroute->netdev == netdev ) {
|
2007-01-04 03:10:21 +00:00
|
|
|
del_ipv4_miniroute ( miniroute );
|
2006-06-16 00:19:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-04-24 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
2006-06-26 16:01:24 +00:00
|
|
|
/**
|
2007-01-03 16:39:11 +00:00
|
|
|
* Perform IPv4 routing
|
2006-06-26 16:01:24 +00:00
|
|
|
*
|
2007-01-03 16:39:11 +00:00
|
|
|
* @v dest Final destination address
|
|
|
|
* @ret dest Next hop destination address
|
|
|
|
* @ret miniroute Routing table entry to use, or NULL if no route
|
2006-06-26 16:01:24 +00:00
|
|
|
*/
|
2007-01-03 16:39:11 +00:00
|
|
|
static struct ipv4_miniroute * ipv4_route ( struct in_addr *dest ) {
|
|
|
|
struct ipv4_miniroute *miniroute;
|
|
|
|
int local;
|
|
|
|
int has_gw;
|
2006-06-28 15:43:08 +00:00
|
|
|
|
2007-01-10 20:38:20 +00:00
|
|
|
list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
|
2007-01-03 16:39:11 +00:00
|
|
|
local = ( ( ( dest->s_addr ^ miniroute->address.s_addr )
|
|
|
|
& miniroute->netmask.s_addr ) == 0 );
|
|
|
|
has_gw = ( miniroute->gateway.s_addr != INADDR_NONE );
|
|
|
|
if ( local || has_gw ) {
|
|
|
|
if ( ! local )
|
|
|
|
*dest = miniroute->gateway;
|
|
|
|
return miniroute;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2006-06-26 16:01:24 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
2007-01-03 20:48:52 +00:00
|
|
|
* Add IPv4 pseudo-header checksum to existing checksum
|
2006-06-28 15:43:08 +00:00
|
|
|
*
|
2007-01-03 20:48:52 +00:00
|
|
|
* @v pkb Packet buffer
|
|
|
|
* @v csum Existing checksum
|
|
|
|
* @ret csum Updated checksum
|
2006-06-25 05:16:54 +00:00
|
|
|
*/
|
2007-01-03 20:48:52 +00:00
|
|
|
static uint16_t ipv4_pshdr_chksum ( struct pk_buff *pkb, uint16_t csum ) {
|
2006-06-28 09:59:27 +00:00
|
|
|
struct ipv4_pseudo_header pshdr;
|
2007-01-03 20:48:52 +00:00
|
|
|
struct iphdr *iphdr = pkb->data;
|
|
|
|
size_t hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
|
2006-06-26 13:45:24 +00:00
|
|
|
|
2007-01-03 20:48:52 +00:00
|
|
|
/* Build 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;
|
2007-01-03 20:48:52 +00:00
|
|
|
pshdr.len = htons ( pkb_len ( pkb ) - hdrlen );
|
2006-06-25 05:16:54 +00:00
|
|
|
|
|
|
|
/* Update the checksum value */
|
2007-01-03 20:48:52 +00:00
|
|
|
return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
|
2006-06-25 05:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-01-03 20:48:52 +00:00
|
|
|
* Determine link-layer address
|
|
|
|
*
|
|
|
|
* @v dest IPv4 destination address
|
|
|
|
* @v src IPv4 source address
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v ll_dest Link-layer destination address buffer
|
|
|
|
* @ret rc Return status code
|
2006-06-25 05:16:54 +00:00
|
|
|
*/
|
2007-01-03 20:48:52 +00:00
|
|
|
static int ipv4_ll_addr ( struct in_addr dest, struct in_addr src,
|
|
|
|
struct net_device *netdev, uint8_t *ll_dest ) {
|
|
|
|
struct ll_protocol *ll_protocol = netdev->ll_protocol;
|
|
|
|
uint8_t *dest_bytes = ( ( uint8_t * ) &dest );
|
|
|
|
|
|
|
|
if ( dest.s_addr == INADDR_BROADCAST ) {
|
|
|
|
/* Broadcast address */
|
|
|
|
memcpy ( ll_dest, ll_protocol->ll_broadcast,
|
|
|
|
ll_protocol->ll_addr_len );
|
|
|
|
return 0;
|
|
|
|
} else if ( IN_MULTICAST ( dest.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.
|
|
|
|
*/
|
|
|
|
ll_dest[0] = 0x01;
|
|
|
|
ll_dest[1] = 0x00;
|
|
|
|
ll_dest[2] = 0x5e;
|
|
|
|
ll_dest[3] = dest_bytes[1] & 0x7f;
|
|
|
|
ll_dest[4] = dest_bytes[2];
|
|
|
|
ll_dest[5] = dest_bytes[3];
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
/* Unicast address: resolve via ARP */
|
|
|
|
return arp_resolve ( netdev, &ipv4_protocol, &dest,
|
|
|
|
&src, ll_dest );
|
|
|
|
}
|
2006-06-25 05:16:54 +00:00
|
|
|
}
|
|
|
|
|
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
|
2007-01-10 02:25:11 +00:00
|
|
|
* @v netdev Network device (or NULL to route automatically)
|
2007-01-03 20:48:52 +00:00
|
|
|
* @v trans_csum Transport-layer checksum to complete, or NULL
|
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,
|
2007-01-10 02:25:11 +00:00
|
|
|
struct sockaddr_tcpip *st_dest,
|
|
|
|
struct net_device *netdev,
|
|
|
|
uint16_t *trans_csum ) {
|
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 in_addr next_hop;
|
2007-01-03 20:48:52 +00:00
|
|
|
uint8_t ll_dest[MAX_LL_ADDR_LEN];
|
2006-06-26 13:45:24 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Fill up the IP header, except source address */
|
2007-01-10 02:25:11 +00:00
|
|
|
memset ( iphdr, 0, sizeof ( *iphdr ) );
|
2007-01-03 20:48:52 +00:00
|
|
|
iphdr->verhdrlen = ( IP_VER | ( sizeof ( *iphdr ) / 4 ) );
|
2006-06-28 09:10:35 +00:00
|
|
|
iphdr->service = IP_TOS;
|
|
|
|
iphdr->len = htons ( pkb_len ( pkb ) );
|
2007-01-03 16:39:11 +00:00
|
|
|
iphdr->ident = htons ( ++next_ident );
|
2006-06-28 09:10:35 +00:00
|
|
|
iphdr->ttl = IP_TTL;
|
2006-08-02 00:02:21 +00:00
|
|
|
iphdr->protocol = tcpip_protocol->tcpip_proto;
|
|
|
|
iphdr->dest = sin_dest->sin_addr;
|
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;
|
2007-01-10 02:25:11 +00:00
|
|
|
if ( ( miniroute = ipv4_route ( &next_hop ) ) ) {
|
|
|
|
iphdr->src = miniroute->address;
|
|
|
|
netdev = miniroute->netdev;
|
|
|
|
}
|
|
|
|
if ( ! netdev ) {
|
2007-01-03 20:48:52 +00:00
|
|
|
DBG ( "IPv4 has no route to %s\n", inet_ntoa ( iphdr->dest ) );
|
2006-06-26 13:45:24 +00:00
|
|
|
rc = -EHOSTUNREACH;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2007-01-03 20:48:52 +00:00
|
|
|
/* Determine link-layer destination address */
|
2007-01-10 02:25:11 +00:00
|
|
|
if ( ( rc = ipv4_ll_addr ( next_hop, iphdr->src, netdev,
|
2007-01-03 20:48:52 +00:00
|
|
|
ll_dest ) ) != 0 ) {
|
|
|
|
DBG ( "IPv4 has no link-layer address for %s\n",
|
|
|
|
inet_ntoa ( iphdr->dest ) );
|
|
|
|
goto err;
|
|
|
|
}
|
2006-06-26 13:45:24 +00:00
|
|
|
|
2007-01-03 20:48:52 +00:00
|
|
|
/* Fix up checksums */
|
|
|
|
if ( trans_csum )
|
|
|
|
*trans_csum = ipv4_pshdr_chksum ( pkb, *trans_csum );
|
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 */
|
2007-01-03 16:39:11 +00:00
|
|
|
DBG ( "IPv4 TX %s->", inet_ntoa ( iphdr->src ) );
|
|
|
|
DBG ( "%s len %d proto %d id %04x csum %04x\n",
|
|
|
|
inet_ntoa ( iphdr->dest ), ntohs ( iphdr->len ), iphdr->protocol,
|
|
|
|
ntohs ( iphdr->ident ), ntohs ( iphdr->chksum ) );
|
2006-06-26 13:45:24 +00:00
|
|
|
|
|
|
|
/* Hand off to link layer */
|
2007-01-10 02:25:11 +00:00
|
|
|
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;
|
2007-01-03 20:48:52 +00:00
|
|
|
size_t hdrlen;
|
|
|
|
size_t len;
|
2006-08-02 00:02:21 +00:00
|
|
|
union {
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
struct sockaddr_tcpip st;
|
|
|
|
} src, dest;
|
2007-01-03 20:48:52 +00:00
|
|
|
uint16_t csum;
|
|
|
|
uint16_t pshdr_csum;
|
2006-06-25 05:16:54 +00:00
|
|
|
|
2007-01-03 20:48:52 +00:00
|
|
|
/* Sanity check the IPv4 header */
|
2006-06-28 09:10:35 +00:00
|
|
|
if ( pkb_len ( pkb ) < sizeof ( *iphdr ) ) {
|
2007-01-03 20:48:52 +00:00
|
|
|
DBG ( "IPv4 packet too short at %d bytes (min %d bytes)\n",
|
|
|
|
pkb_len ( pkb ), sizeof ( *iphdr ) );
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if ( ( iphdr->verhdrlen & IP_MASK_VER ) != IP_VER ) {
|
|
|
|
DBG ( "IPv4 version %#02x not supported\n", iphdr->verhdrlen );
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
|
|
|
|
if ( hdrlen < sizeof ( *iphdr ) ) {
|
|
|
|
DBG ( "IPv4 header too short at %d bytes (min %d bytes)\n",
|
|
|
|
hdrlen, sizeof ( *iphdr ) );
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if ( hdrlen > pkb_len ( pkb ) ) {
|
|
|
|
DBG ( "IPv4 header too long at %d bytes "
|
|
|
|
"(packet is %d bytes)\n", hdrlen, pkb_len ( pkb ) );
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if ( ( csum = tcpip_chksum ( iphdr, hdrlen ) ) != 0 ) {
|
|
|
|
DBG ( "IPv4 checksum incorrect (is %04x including checksum "
|
|
|
|
"field, should be 0000)\n", csum );
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
len = ntohs ( iphdr->len );
|
|
|
|
if ( len < hdrlen ) {
|
|
|
|
DBG ( "IPv4 length too short at %d bytes "
|
|
|
|
"(header is %d bytes)\n", len, hdrlen );
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if ( len > pkb_len ( pkb ) ) {
|
|
|
|
DBG ( "IPv4 length too long at %d bytes "
|
|
|
|
"(packet is %d bytes)\n", len, pkb_len ( pkb ) );
|
2006-08-09 01:24:32 +00:00
|
|
|
goto err;
|
2006-06-28 09:10:35 +00:00
|
|
|
}
|
|
|
|
|
2007-01-03 20:48:52 +00:00
|
|
|
/* Print IPv4 header for debugging */
|
2007-01-03 16:39:11 +00:00
|
|
|
DBG ( "IPv4 RX %s<-", inet_ntoa ( iphdr->dest ) );
|
|
|
|
DBG ( "%s len %d proto %d id %04x csum %04x\n",
|
|
|
|
inet_ntoa ( iphdr->src ), ntohs ( iphdr->len ), iphdr->protocol,
|
|
|
|
ntohs ( iphdr->ident ), ntohs ( iphdr->chksum ) );
|
2006-06-26 13:45:24 +00:00
|
|
|
|
2007-01-03 20:48:52 +00:00
|
|
|
/* Truncate packet to correct length, calculate pseudo-header
|
|
|
|
* checksum and then strip off the IPv4 header.
|
|
|
|
*/
|
|
|
|
pkb_unput ( pkb, ( pkb_len ( pkb ) - len ) );
|
|
|
|
pshdr_csum = ipv4_pshdr_chksum ( pkb, TCPIP_EMPTY_CSUM );
|
|
|
|
pkb_pull ( pkb, hdrlen );
|
2006-06-25 05:16:54 +00:00
|
|
|
|
2006-06-30 08:52:03 +00:00
|
|
|
/* Fragment reassembly */
|
2007-01-03 16:39:11 +00:00
|
|
|
if ( ( iphdr->frags & htons ( IP_MASK_MOREFRAGS ) ) ||
|
|
|
|
( ( iphdr->frags & htons ( IP_MASK_OFFSET ) ) != 0 ) ) {
|
2007-01-03 20:48:52 +00:00
|
|
|
/* Pass the fragment to ipv4_reassemble() which either
|
|
|
|
* returns a fully reassembled packet buffer or NULL.
|
2006-06-30 08:52:03 +00:00
|
|
|
*/
|
|
|
|
pkb = ipv4_reassemble ( pkb );
|
2007-01-03 20:48:52 +00:00
|
|
|
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
|
|
|
|
2007-01-03 20:48:52 +00:00
|
|
|
/* Construct socket addresses and hand off to transport layer */
|
2006-08-02 00:02:21 +00:00
|
|
|
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;
|
2007-01-03 20:48:52 +00:00
|
|
|
return tcpip_rx ( pkb, iphdr->protocol, &src.st, &dest.st, pshdr_csum);
|
2006-08-09 01:24:32 +00:00
|
|
|
|
|
|
|
err:
|
|
|
|
free_pkb ( pkb );
|
|
|
|
return -EINVAL;
|
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;
|
|
|
|
|
2007-01-10 20:38:20 +00:00
|
|
|
list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
|
2006-06-17 22:36:27 +00:00
|
|
|
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 */
|
2006-08-09 04:42:14 +00:00
|
|
|
struct net_protocol ipv4_protocol __net_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
|
|
|
};
|
|
|
|
|
2006-06-28 15:43:08 +00:00
|
|
|
/** IPv4 TCPIP net protocol */
|
2006-08-09 04:42:14 +00:00
|
|
|
struct tcpip_net_protocol ipv4_tcpip_protocol __tcpip_net_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
|
|
|
};
|
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
/** IPv4 ARP protocol */
|
2006-08-08 17:44:25 +00:00
|
|
|
struct arp_net_protocol ipv4_arp_protocol __arp_net_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
|
|
|
};
|