10 #include <gpxe/if_ether.h>
11 #include <gpxe/iobuf.h>
12 #include <gpxe/netdevice.h>
14 #include <gpxe/tcpip.h>
22 /* Unique IP datagram identification number */
23 static uint16_t next_ident = 0;
25 struct net_protocol ipv4_protocol;
27 /** List of IPv4 miniroutes */
28 struct list_head ipv4_miniroutes = LIST_HEAD_INIT ( ipv4_miniroutes );
30 /** List of fragment reassembly buffers */
31 static LIST_HEAD ( frag_buffers );
34 * Add IPv4 minirouting table entry
36 * @v netdev Network device
37 * @v address IPv4 address
38 * @v netmask Subnet mask
39 * @v gateway Gateway address (or @c INADDR_NONE for no gateway)
40 * @ret miniroute Routing table entry, or NULL
42 static struct ipv4_miniroute * add_ipv4_miniroute ( struct net_device *netdev,
43 struct in_addr address,
44 struct in_addr netmask,
45 struct in_addr gateway ) {
46 struct ipv4_miniroute *miniroute;
48 DBG ( "IPv4 add %s", inet_ntoa ( address ) );
49 DBG ( "/%s ", inet_ntoa ( netmask ) );
50 if ( gateway.s_addr != INADDR_NONE )
51 DBG ( "gw %s ", inet_ntoa ( gateway ) );
52 DBG ( "via %s\n", netdev->name );
54 /* Allocate and populate miniroute structure */
55 miniroute = malloc ( sizeof ( *miniroute ) );
57 DBG ( "IPv4 could not add miniroute\n" );
61 /* Record routing information */
62 miniroute->netdev = netdev_get ( netdev );
63 miniroute->address = address;
64 miniroute->netmask = netmask;
65 miniroute->gateway = gateway;
67 /* Add to end of list if we have a gateway, otherwise
70 if ( gateway.s_addr != INADDR_NONE ) {
71 list_add_tail ( &miniroute->list, &ipv4_miniroutes );
73 list_add ( &miniroute->list, &ipv4_miniroutes );
80 * Delete IPv4 minirouting table entry
82 * @v miniroute Routing table entry
84 static void del_ipv4_miniroute ( struct ipv4_miniroute *miniroute ) {
86 DBG ( "IPv4 del %s", inet_ntoa ( miniroute->address ) );
87 DBG ( "/%s ", inet_ntoa ( miniroute->netmask ) );
88 if ( miniroute->gateway.s_addr != INADDR_NONE )
89 DBG ( "gw %s ", inet_ntoa ( miniroute->gateway ) );
90 DBG ( "via %s\n", miniroute->netdev->name );
92 netdev_put ( miniroute->netdev );
93 list_del ( &miniroute->list );
100 * @v netdev Network device
101 * @v address IPv4 address
102 * @v netmask Subnet mask
103 * @v gateway Gateway address (or @c INADDR_NONE for no gateway)
104 * @ret rc Return status code
107 int add_ipv4_address ( struct net_device *netdev, struct in_addr address,
108 struct in_addr netmask, struct in_addr gateway ) {
109 struct ipv4_miniroute *miniroute;
111 /* Clear any existing address for this net device */
112 del_ipv4_address ( netdev );
114 /* Add new miniroute */
115 miniroute = add_ipv4_miniroute ( netdev, address, netmask, gateway );
123 * Remove IPv4 interface
125 * @v netdev Network device
127 void del_ipv4_address ( struct net_device *netdev ) {
128 struct ipv4_miniroute *miniroute;
130 list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
131 if ( miniroute->netdev == netdev ) {
132 del_ipv4_miniroute ( miniroute );
139 * Perform IPv4 routing
141 * @v dest Final destination address
142 * @ret dest Next hop destination address
143 * @ret miniroute Routing table entry to use, or NULL if no route
145 * If the route requires use of a gateway, the next hop destination
146 * address will be overwritten with the gateway address.
148 static struct ipv4_miniroute * ipv4_route ( struct in_addr *dest ) {
149 struct ipv4_miniroute *miniroute;
153 /* Never attempt to route the broadcast address */
154 if ( dest->s_addr == INADDR_BROADCAST )
157 /* Find first usable route in routing table */
158 list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
159 local = ( ( ( dest->s_addr ^ miniroute->address.s_addr )
160 & miniroute->netmask.s_addr ) == 0 );
161 has_gw = ( miniroute->gateway.s_addr != INADDR_NONE );
162 if ( local || has_gw ) {
164 *dest = miniroute->gateway;
173 * Fragment reassembly counter timeout
175 * @v timer Retry timer
176 * @v over If asserted, the timer is greater than @c MAX_TIMEOUT
178 static void ipv4_frag_expired ( struct retry_timer *timer __unused,
181 DBG ( "Fragment reassembly timeout" );
182 /* Free the fragment buffer */
187 * Free fragment buffer
189 * @v fragbug Fragment buffer
191 static void free_fragbuf ( struct frag_buffer *fragbuf ) {
196 * Fragment reassembler
198 * @v iobuf I/O buffer, fragment of the datagram
199 * @ret frag_iob Reassembled packet, or NULL
201 static struct io_buffer * ipv4_reassemble ( struct io_buffer * iobuf ) {
202 struct iphdr *iphdr = iobuf->data;
203 struct frag_buffer *fragbuf;
206 * Check if the fragment belongs to any fragment series
208 list_for_each_entry ( fragbuf, &frag_buffers, list ) {
209 if ( fragbuf->ident == iphdr->ident &&
210 fragbuf->src.s_addr == iphdr->src.s_addr ) {
212 * Check if the packet is the expected fragment
214 * The offset of the new packet must be equal to the
215 * length of the data accumulated so far (the length of
216 * the reassembled I/O buffer
218 if ( iob_len ( fragbuf->frag_iob ) ==
219 ( iphdr->frags & IP_MASK_OFFSET ) ) {
221 * Append the contents of the fragment to the
222 * reassembled I/O buffer
224 iob_pull ( iobuf, sizeof ( *iphdr ) );
225 memcpy ( iob_put ( fragbuf->frag_iob,
227 iobuf->data, iob_len ( iobuf ) );
230 /** Check if the fragment series is over */
231 if ( !iphdr->frags & IP_MASK_MOREFRAGS ) {
232 iobuf = fragbuf->frag_iob;
233 free_fragbuf ( fragbuf );
238 /* Discard the fragment series */
239 free_fragbuf ( fragbuf );
246 /** Check if the fragment is the first in the fragment series */
247 if ( iphdr->frags & IP_MASK_MOREFRAGS &&
248 ( ( iphdr->frags & IP_MASK_OFFSET ) == 0 ) ) {
250 /** Create a new fragment buffer */
251 fragbuf = ( struct frag_buffer* ) malloc ( sizeof( *fragbuf ) );
252 fragbuf->ident = iphdr->ident;
253 fragbuf->src = iphdr->src;
255 /* Set up the reassembly I/O buffer */
256 fragbuf->frag_iob = alloc_iob ( IP_FRAG_IOB_SIZE );
257 iob_pull ( iobuf, sizeof ( *iphdr ) );
258 memcpy ( iob_put ( fragbuf->frag_iob, iob_len ( iobuf ) ),
259 iobuf->data, iob_len ( iobuf ) );
262 /* Set the reassembly timer */
263 fragbuf->frag_timer.timeout = IP_FRAG_TIMEOUT;
264 fragbuf->frag_timer.expired = ipv4_frag_expired;
265 start_timer ( &fragbuf->frag_timer );
267 /* Add the fragment buffer to the list of fragment buffers */
268 list_add ( &fragbuf->list, &frag_buffers );
275 * Add IPv4 pseudo-header checksum to existing checksum
277 * @v iobuf I/O buffer
278 * @v csum Existing checksum
279 * @ret csum Updated checksum
281 static uint16_t ipv4_pshdr_chksum ( struct io_buffer *iobuf, uint16_t csum ) {
282 struct ipv4_pseudo_header pshdr;
283 struct iphdr *iphdr = iobuf->data;
284 size_t hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
286 /* Build pseudo-header */
287 pshdr.src = iphdr->src;
288 pshdr.dest = iphdr->dest;
289 pshdr.zero_padding = 0x00;
290 pshdr.protocol = iphdr->protocol;
291 pshdr.len = htons ( iob_len ( iobuf ) - hdrlen );
293 /* Update the checksum value */
294 return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
298 * Determine link-layer address
300 * @v dest IPv4 destination address
301 * @v src IPv4 source address
302 * @v netdev Network device
303 * @v ll_dest Link-layer destination address buffer
304 * @ret rc Return status code
306 static int ipv4_ll_addr ( struct in_addr dest, struct in_addr src,
307 struct net_device *netdev, uint8_t *ll_dest ) {
308 struct ll_protocol *ll_protocol = netdev->ll_protocol;
309 uint8_t *dest_bytes = ( ( uint8_t * ) &dest );
311 if ( dest.s_addr == INADDR_BROADCAST ) {
312 /* Broadcast address */
313 memcpy ( ll_dest, ll_protocol->ll_broadcast,
314 ll_protocol->ll_addr_len );
316 } else if ( IN_MULTICAST ( dest.s_addr ) ) {
317 /* Special case: IPv4 multicast over Ethernet. This
318 * code may need to be generalised once we find out
319 * what happens for other link layers.
324 ll_dest[3] = dest_bytes[1] & 0x7f;
325 ll_dest[4] = dest_bytes[2];
326 ll_dest[5] = dest_bytes[3];
329 /* Unicast address: resolve via ARP */
330 return arp_resolve ( netdev, &ipv4_protocol, &dest,
338 * @v iobuf I/O buffer
339 * @v tcpip Transport-layer protocol
340 * @v st_dest Destination network-layer address
341 * @v netdev Network device to use if no route found, or NULL
342 * @v trans_csum Transport-layer checksum to complete, or NULL
345 * This function expects a transport-layer segment and prepends the IP header
347 static int ipv4_tx ( struct io_buffer *iobuf,
348 struct tcpip_protocol *tcpip_protocol,
349 struct sockaddr_tcpip *st_dest,
350 struct net_device *netdev,
351 uint16_t *trans_csum ) {
352 struct iphdr *iphdr = iob_push ( iobuf, sizeof ( *iphdr ) );
353 struct sockaddr_in *sin_dest = ( ( struct sockaddr_in * ) st_dest );
354 struct ipv4_miniroute *miniroute;
355 struct in_addr next_hop;
356 uint8_t ll_dest[MAX_LL_ADDR_LEN];
359 /* Fill up the IP header, except source address */
360 memset ( iphdr, 0, sizeof ( *iphdr ) );
361 iphdr->verhdrlen = ( IP_VER | ( sizeof ( *iphdr ) / 4 ) );
362 iphdr->service = IP_TOS;
363 iphdr->len = htons ( iob_len ( iobuf ) );
364 iphdr->ident = htons ( ++next_ident );
366 iphdr->protocol = tcpip_protocol->tcpip_proto;
367 iphdr->dest = sin_dest->sin_addr;
369 /* Use routing table to identify next hop and transmitting netdev */
370 next_hop = iphdr->dest;
371 if ( ( miniroute = ipv4_route ( &next_hop ) ) ) {
372 iphdr->src = miniroute->address;
373 netdev = miniroute->netdev;
376 DBG ( "IPv4 has no route to %s\n", inet_ntoa ( iphdr->dest ) );
381 /* Determine link-layer destination address */
382 if ( ( rc = ipv4_ll_addr ( next_hop, iphdr->src, netdev,
384 DBG ( "IPv4 has no link-layer address for %s: %s\n",
385 inet_ntoa ( next_hop ), strerror ( rc ) );
389 /* Fix up checksums */
391 *trans_csum = ipv4_pshdr_chksum ( iobuf, *trans_csum );
392 iphdr->chksum = tcpip_chksum ( iphdr, sizeof ( *iphdr ) );
394 /* Print IP4 header for debugging */
395 DBG ( "IPv4 TX %s->", inet_ntoa ( iphdr->src ) );
396 DBG ( "%s len %d proto %d id %04x csum %04x\n",
397 inet_ntoa ( iphdr->dest ), ntohs ( iphdr->len ), iphdr->protocol,
398 ntohs ( iphdr->ident ), ntohs ( iphdr->chksum ) );
400 /* Hand off to link layer */
401 if ( ( rc = net_tx ( iobuf, netdev, &ipv4_protocol, ll_dest ) ) != 0 ) {
402 DBG ( "IPv4 could not transmit packet via %s: %s\n",
403 netdev->name, strerror ( rc ) );
415 * Process incoming packets
417 * @v iobuf I/O buffer
418 * @v netdev Network device
419 * @v ll_source Link-layer destination source
421 * This function expects an IP4 network datagram. It processes the headers
422 * and sends it to the transport layer.
424 static int ipv4_rx ( struct io_buffer *iobuf, struct net_device *netdev __unused,
425 const void *ll_source __unused ) {
426 struct iphdr *iphdr = iobuf->data;
430 struct sockaddr_in sin;
431 struct sockaddr_tcpip st;
437 /* Sanity check the IPv4 header */
438 if ( iob_len ( iobuf ) < sizeof ( *iphdr ) ) {
439 DBG ( "IPv4 packet too short at %d bytes (min %d bytes)\n",
440 iob_len ( iobuf ), sizeof ( *iphdr ) );
443 if ( ( iphdr->verhdrlen & IP_MASK_VER ) != IP_VER ) {
444 DBG ( "IPv4 version %#02x not supported\n", iphdr->verhdrlen );
447 hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
448 if ( hdrlen < sizeof ( *iphdr ) ) {
449 DBG ( "IPv4 header too short at %d bytes (min %d bytes)\n",
450 hdrlen, sizeof ( *iphdr ) );
453 if ( hdrlen > iob_len ( iobuf ) ) {
454 DBG ( "IPv4 header too long at %d bytes "
455 "(packet is %d bytes)\n", hdrlen, iob_len ( iobuf ) );
458 if ( ( csum = tcpip_chksum ( iphdr, hdrlen ) ) != 0 ) {
459 DBG ( "IPv4 checksum incorrect (is %04x including checksum "
460 "field, should be 0000)\n", csum );
463 len = ntohs ( iphdr->len );
464 if ( len < hdrlen ) {
465 DBG ( "IPv4 length too short at %d bytes "
466 "(header is %d bytes)\n", len, hdrlen );
469 if ( len > iob_len ( iobuf ) ) {
470 DBG ( "IPv4 length too long at %d bytes "
471 "(packet is %d bytes)\n", len, iob_len ( iobuf ) );
475 /* Print IPv4 header for debugging */
476 DBG ( "IPv4 RX %s<-", inet_ntoa ( iphdr->dest ) );
477 DBG ( "%s len %d proto %d id %04x csum %04x\n",
478 inet_ntoa ( iphdr->src ), ntohs ( iphdr->len ), iphdr->protocol,
479 ntohs ( iphdr->ident ), ntohs ( iphdr->chksum ) );
481 /* Truncate packet to correct length, calculate pseudo-header
482 * checksum and then strip off the IPv4 header.
484 iob_unput ( iobuf, ( iob_len ( iobuf ) - len ) );
485 pshdr_csum = ipv4_pshdr_chksum ( iobuf, TCPIP_EMPTY_CSUM );
486 iob_pull ( iobuf, hdrlen );
488 /* Fragment reassembly */
489 if ( ( iphdr->frags & htons ( IP_MASK_MOREFRAGS ) ) ||
490 ( ( iphdr->frags & htons ( IP_MASK_OFFSET ) ) != 0 ) ) {
491 /* Pass the fragment to ipv4_reassemble() which either
492 * returns a fully reassembled I/O buffer or NULL.
494 iobuf = ipv4_reassemble ( iobuf );
499 /* Construct socket addresses and hand off to transport layer */
500 memset ( &src, 0, sizeof ( src ) );
501 src.sin.sin_family = AF_INET;
502 src.sin.sin_addr = iphdr->src;
503 memset ( &dest, 0, sizeof ( dest ) );
504 dest.sin.sin_family = AF_INET;
505 dest.sin.sin_addr = iphdr->dest;
506 if ( ( rc = tcpip_rx ( iobuf, iphdr->protocol, &src.st,
507 &dest.st, pshdr_csum ) ) != 0 ) {
508 DBG ( "IPv4 received packet rejected by stack: %s\n",
521 * Check existence of IPv4 address for ARP
523 * @v netdev Network device
524 * @v net_addr Network-layer address
525 * @ret rc Return status code
527 static int ipv4_arp_check ( struct net_device *netdev, const void *net_addr ) {
528 const struct in_addr *address = net_addr;
529 struct ipv4_miniroute *miniroute;
531 list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
532 if ( ( miniroute->netdev == netdev ) &&
533 ( miniroute->address.s_addr == address->s_addr ) ) {
534 /* Found matching address */
542 * Convert IPv4 address to dotted-quad notation
545 * @ret string IP address in dotted-quad notation
547 char * inet_ntoa ( struct in_addr in ) {
548 static char buf[16]; /* "xxx.xxx.xxx.xxx" */
549 uint8_t *bytes = ( uint8_t * ) ∈
551 sprintf ( buf, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3] );
556 * Transcribe IP address
558 * @v net_addr IP address
559 * @ret string IP address in dotted-quad notation
562 static const char * ipv4_ntoa ( const void *net_addr ) {
563 return inet_ntoa ( * ( ( struct in_addr * ) net_addr ) );
567 struct net_protocol ipv4_protocol __net_protocol = {
569 .net_proto = htons ( ETH_P_IP ),
570 .net_addr_len = sizeof ( struct in_addr ),
575 /** IPv4 TCPIP net protocol */
576 struct tcpip_net_protocol ipv4_tcpip_protocol __tcpip_net_protocol = {
578 .sa_family = AF_INET,
582 /** IPv4 ARP protocol */
583 struct arp_net_protocol ipv4_arp_protocol __arp_net_protocol = {
584 .net_protocol = &ipv4_protocol,
585 .check = ipv4_arp_check,