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 );
33 static void ipv4_forget_netdev ( struct reference *ref );
36 * Add IPv4 minirouting table entry
38 * @v netdev Network device
39 * @v address IPv4 address
40 * @v netmask Subnet mask
41 * @v gateway Gateway address (or @c INADDR_NONE for no gateway)
42 * @ret miniroute Routing table entry, or NULL
44 static struct ipv4_miniroute * add_ipv4_miniroute ( struct net_device *netdev,
45 struct in_addr address,
46 struct in_addr netmask,
47 struct in_addr gateway ) {
48 struct ipv4_miniroute *miniroute;
50 DBG ( "IPv4 add %s", inet_ntoa ( address ) );
51 DBG ( "/%s ", inet_ntoa ( netmask ) );
52 if ( gateway.s_addr != INADDR_NONE )
53 DBG ( "gw %s ", inet_ntoa ( gateway ) );
54 DBG ( "via %s\n", netdev->name );
56 /* Allocate and populate miniroute structure */
57 miniroute = malloc ( sizeof ( *miniroute ) );
59 DBG ( "IPv4 could not add miniroute\n" );
63 /* Record routing information */
64 miniroute->netdev = netdev;
65 miniroute->address = address;
66 miniroute->netmask = netmask;
67 miniroute->gateway = gateway;
69 /* Add to end of list if we have a gateway, otherwise
72 if ( gateway.s_addr != INADDR_NONE ) {
73 list_add_tail ( &miniroute->list, &ipv4_miniroutes );
75 list_add ( &miniroute->list, &ipv4_miniroutes );
78 /* Record reference to net_device */
79 miniroute->netdev_ref.forget = ipv4_forget_netdev;
80 ref_add ( &miniroute->netdev_ref, &netdev->references );
86 * Delete IPv4 minirouting table entry
88 * @v miniroute Routing table entry
90 static void del_ipv4_miniroute ( struct ipv4_miniroute *miniroute ) {
92 DBG ( "IPv4 del %s", inet_ntoa ( miniroute->address ) );
93 DBG ( "/%s ", inet_ntoa ( miniroute->netmask ) );
94 if ( miniroute->gateway.s_addr != INADDR_NONE )
95 DBG ( "gw %s ", inet_ntoa ( miniroute->gateway ) );
96 DBG ( "via %s\n", miniroute->netdev->name );
98 ref_del ( &miniroute->netdev_ref );
99 list_del ( &miniroute->list );
104 * Forget reference to net_device
106 * @v ref Persistent reference
108 static void ipv4_forget_netdev ( struct reference *ref ) {
109 struct ipv4_miniroute *miniroute
110 = container_of ( ref, struct ipv4_miniroute, netdev_ref );
112 del_ipv4_miniroute ( miniroute );
118 * @v netdev Network device
119 * @v address IPv4 address
120 * @v netmask Subnet mask
121 * @v gateway Gateway address (or @c INADDR_NONE for no gateway)
122 * @ret rc Return status code
125 int add_ipv4_address ( struct net_device *netdev, struct in_addr address,
126 struct in_addr netmask, struct in_addr gateway ) {
127 struct ipv4_miniroute *miniroute;
129 /* Clear any existing address for this net device */
130 del_ipv4_address ( netdev );
132 /* Add new miniroute */
133 miniroute = add_ipv4_miniroute ( netdev, address, netmask, gateway );
141 * Remove IPv4 interface
143 * @v netdev Network device
145 void del_ipv4_address ( struct net_device *netdev ) {
146 struct ipv4_miniroute *miniroute;
148 list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
149 if ( miniroute->netdev == netdev ) {
150 del_ipv4_miniroute ( miniroute );
157 * Perform IPv4 routing
159 * @v dest Final destination address
160 * @ret dest Next hop destination address
161 * @ret miniroute Routing table entry to use, or NULL if no route
163 static struct ipv4_miniroute * ipv4_route ( struct in_addr *dest ) {
164 struct ipv4_miniroute *miniroute;
168 list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
169 local = ( ( ( dest->s_addr ^ miniroute->address.s_addr )
170 & miniroute->netmask.s_addr ) == 0 );
171 has_gw = ( miniroute->gateway.s_addr != INADDR_NONE );
172 if ( local || has_gw ) {
174 *dest = miniroute->gateway;
183 * Fragment reassembly counter timeout
185 * @v timer Retry timer
186 * @v over If asserted, the timer is greater than @c MAX_TIMEOUT
188 static void ipv4_frag_expired ( struct retry_timer *timer __unused,
191 DBG ( "Fragment reassembly timeout" );
192 /* Free the fragment buffer */
197 * Free fragment buffer
199 * @v fragbug Fragment buffer
201 static void free_fragbuf ( struct frag_buffer *fragbuf ) {
206 * Fragment reassembler
208 * @v iobuf I/O buffer, fragment of the datagram
209 * @ret frag_iob Reassembled packet, or NULL
211 static struct io_buffer * ipv4_reassemble ( struct io_buffer * iobuf ) {
212 struct iphdr *iphdr = iobuf->data;
213 struct frag_buffer *fragbuf;
216 * Check if the fragment belongs to any fragment series
218 list_for_each_entry ( fragbuf, &frag_buffers, list ) {
219 if ( fragbuf->ident == iphdr->ident &&
220 fragbuf->src.s_addr == iphdr->src.s_addr ) {
222 * Check if the packet is the expected fragment
224 * The offset of the new packet must be equal to the
225 * length of the data accumulated so far (the length of
226 * the reassembled I/O buffer
228 if ( iob_len ( fragbuf->frag_iob ) ==
229 ( iphdr->frags & IP_MASK_OFFSET ) ) {
231 * Append the contents of the fragment to the
232 * reassembled I/O buffer
234 iob_pull ( iobuf, sizeof ( *iphdr ) );
235 memcpy ( iob_put ( fragbuf->frag_iob,
237 iobuf->data, iob_len ( iobuf ) );
240 /** Check if the fragment series is over */
241 if ( !iphdr->frags & IP_MASK_MOREFRAGS ) {
242 iobuf = fragbuf->frag_iob;
243 free_fragbuf ( fragbuf );
248 /* Discard the fragment series */
249 free_fragbuf ( fragbuf );
256 /** Check if the fragment is the first in the fragment series */
257 if ( iphdr->frags & IP_MASK_MOREFRAGS &&
258 ( ( iphdr->frags & IP_MASK_OFFSET ) == 0 ) ) {
260 /** Create a new fragment buffer */
261 fragbuf = ( struct frag_buffer* ) malloc ( sizeof( *fragbuf ) );
262 fragbuf->ident = iphdr->ident;
263 fragbuf->src = iphdr->src;
265 /* Set up the reassembly I/O buffer */
266 fragbuf->frag_iob = alloc_iob ( IP_FRAG_IOB_SIZE );
267 iob_pull ( iobuf, sizeof ( *iphdr ) );
268 memcpy ( iob_put ( fragbuf->frag_iob, iob_len ( iobuf ) ),
269 iobuf->data, iob_len ( iobuf ) );
272 /* Set the reassembly timer */
273 fragbuf->frag_timer.timeout = IP_FRAG_TIMEOUT;
274 fragbuf->frag_timer.expired = ipv4_frag_expired;
275 start_timer ( &fragbuf->frag_timer );
277 /* Add the fragment buffer to the list of fragment buffers */
278 list_add ( &fragbuf->list, &frag_buffers );
285 * Add IPv4 pseudo-header checksum to existing checksum
287 * @v iobuf I/O buffer
288 * @v csum Existing checksum
289 * @ret csum Updated checksum
291 static uint16_t ipv4_pshdr_chksum ( struct io_buffer *iobuf, uint16_t csum ) {
292 struct ipv4_pseudo_header pshdr;
293 struct iphdr *iphdr = iobuf->data;
294 size_t hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
296 /* Build pseudo-header */
297 pshdr.src = iphdr->src;
298 pshdr.dest = iphdr->dest;
299 pshdr.zero_padding = 0x00;
300 pshdr.protocol = iphdr->protocol;
301 pshdr.len = htons ( iob_len ( iobuf ) - hdrlen );
303 /* Update the checksum value */
304 return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
308 * Determine link-layer address
310 * @v dest IPv4 destination address
311 * @v src IPv4 source address
312 * @v netdev Network device
313 * @v ll_dest Link-layer destination address buffer
314 * @ret rc Return status code
316 static int ipv4_ll_addr ( struct in_addr dest, struct in_addr src,
317 struct net_device *netdev, uint8_t *ll_dest ) {
318 struct ll_protocol *ll_protocol = netdev->ll_protocol;
319 uint8_t *dest_bytes = ( ( uint8_t * ) &dest );
321 if ( dest.s_addr == INADDR_BROADCAST ) {
322 /* Broadcast address */
323 memcpy ( ll_dest, ll_protocol->ll_broadcast,
324 ll_protocol->ll_addr_len );
326 } else if ( IN_MULTICAST ( dest.s_addr ) ) {
327 /* Special case: IPv4 multicast over Ethernet. This
328 * code may need to be generalised once we find out
329 * what happens for other link layers.
334 ll_dest[3] = dest_bytes[1] & 0x7f;
335 ll_dest[4] = dest_bytes[2];
336 ll_dest[5] = dest_bytes[3];
339 /* Unicast address: resolve via ARP */
340 return arp_resolve ( netdev, &ipv4_protocol, &dest,
348 * @v iobuf I/O buffer
349 * @v tcpip Transport-layer protocol
350 * @v st_dest Destination network-layer address
351 * @v netdev Network device to use if no route found, or NULL
352 * @v trans_csum Transport-layer checksum to complete, or NULL
355 * This function expects a transport-layer segment and prepends the IP header
357 static int ipv4_tx ( struct io_buffer *iobuf,
358 struct tcpip_protocol *tcpip_protocol,
359 struct sockaddr_tcpip *st_dest,
360 struct net_device *netdev,
361 uint16_t *trans_csum ) {
362 struct iphdr *iphdr = iob_push ( iobuf, sizeof ( *iphdr ) );
363 struct sockaddr_in *sin_dest = ( ( struct sockaddr_in * ) st_dest );
364 struct ipv4_miniroute *miniroute;
365 struct in_addr next_hop;
366 uint8_t ll_dest[MAX_LL_ADDR_LEN];
369 /* Fill up the IP header, except source address */
370 memset ( iphdr, 0, sizeof ( *iphdr ) );
371 iphdr->verhdrlen = ( IP_VER | ( sizeof ( *iphdr ) / 4 ) );
372 iphdr->service = IP_TOS;
373 iphdr->len = htons ( iob_len ( iobuf ) );
374 iphdr->ident = htons ( ++next_ident );
376 iphdr->protocol = tcpip_protocol->tcpip_proto;
377 iphdr->dest = sin_dest->sin_addr;
379 /* Use routing table to identify next hop and transmitting netdev */
380 next_hop = iphdr->dest;
381 if ( ( miniroute = ipv4_route ( &next_hop ) ) ) {
382 iphdr->src = miniroute->address;
383 netdev = miniroute->netdev;
386 DBG ( "IPv4 has no route to %s\n", inet_ntoa ( iphdr->dest ) );
391 /* Determine link-layer destination address */
392 if ( ( rc = ipv4_ll_addr ( next_hop, iphdr->src, netdev,
394 DBG ( "IPv4 has no link-layer address for %s: %s\n",
395 inet_ntoa ( next_hop ), strerror ( rc ) );
399 /* Fix up checksums */
401 *trans_csum = ipv4_pshdr_chksum ( iobuf, *trans_csum );
402 iphdr->chksum = tcpip_chksum ( iphdr, sizeof ( *iphdr ) );
404 /* Print IP4 header for debugging */
405 DBG ( "IPv4 TX %s->", inet_ntoa ( iphdr->src ) );
406 DBG ( "%s len %d proto %d id %04x csum %04x\n",
407 inet_ntoa ( iphdr->dest ), ntohs ( iphdr->len ), iphdr->protocol,
408 ntohs ( iphdr->ident ), ntohs ( iphdr->chksum ) );
410 /* Hand off to link layer */
411 if ( ( rc = net_tx ( iobuf, netdev, &ipv4_protocol, ll_dest ) ) != 0 ) {
412 DBG ( "IPv4 could not transmit packet via %s: %s\n",
413 netdev->name, strerror ( rc ) );
425 * Process incoming packets
427 * @v iobuf I/O buffer
428 * @v netdev Network device
429 * @v ll_source Link-layer destination source
431 * This function expects an IP4 network datagram. It processes the headers
432 * and sends it to the transport layer.
434 static int ipv4_rx ( struct io_buffer *iobuf, struct net_device *netdev __unused,
435 const void *ll_source __unused ) {
436 struct iphdr *iphdr = iobuf->data;
440 struct sockaddr_in sin;
441 struct sockaddr_tcpip st;
447 /* Sanity check the IPv4 header */
448 if ( iob_len ( iobuf ) < sizeof ( *iphdr ) ) {
449 DBG ( "IPv4 packet too short at %d bytes (min %d bytes)\n",
450 iob_len ( iobuf ), sizeof ( *iphdr ) );
453 if ( ( iphdr->verhdrlen & IP_MASK_VER ) != IP_VER ) {
454 DBG ( "IPv4 version %#02x not supported\n", iphdr->verhdrlen );
457 hdrlen = ( ( iphdr->verhdrlen & IP_MASK_HLEN ) * 4 );
458 if ( hdrlen < sizeof ( *iphdr ) ) {
459 DBG ( "IPv4 header too short at %d bytes (min %d bytes)\n",
460 hdrlen, sizeof ( *iphdr ) );
463 if ( hdrlen > iob_len ( iobuf ) ) {
464 DBG ( "IPv4 header too long at %d bytes "
465 "(packet is %d bytes)\n", hdrlen, iob_len ( iobuf ) );
468 if ( ( csum = tcpip_chksum ( iphdr, hdrlen ) ) != 0 ) {
469 DBG ( "IPv4 checksum incorrect (is %04x including checksum "
470 "field, should be 0000)\n", csum );
473 len = ntohs ( iphdr->len );
474 if ( len < hdrlen ) {
475 DBG ( "IPv4 length too short at %d bytes "
476 "(header is %d bytes)\n", len, hdrlen );
479 if ( len > iob_len ( iobuf ) ) {
480 DBG ( "IPv4 length too long at %d bytes "
481 "(packet is %d bytes)\n", len, iob_len ( iobuf ) );
485 /* Print IPv4 header for debugging */
486 DBG ( "IPv4 RX %s<-", inet_ntoa ( iphdr->dest ) );
487 DBG ( "%s len %d proto %d id %04x csum %04x\n",
488 inet_ntoa ( iphdr->src ), ntohs ( iphdr->len ), iphdr->protocol,
489 ntohs ( iphdr->ident ), ntohs ( iphdr->chksum ) );
491 /* Truncate packet to correct length, calculate pseudo-header
492 * checksum and then strip off the IPv4 header.
494 iob_unput ( iobuf, ( iob_len ( iobuf ) - len ) );
495 pshdr_csum = ipv4_pshdr_chksum ( iobuf, TCPIP_EMPTY_CSUM );
496 iob_pull ( iobuf, hdrlen );
498 /* Fragment reassembly */
499 if ( ( iphdr->frags & htons ( IP_MASK_MOREFRAGS ) ) ||
500 ( ( iphdr->frags & htons ( IP_MASK_OFFSET ) ) != 0 ) ) {
501 /* Pass the fragment to ipv4_reassemble() which either
502 * returns a fully reassembled I/O buffer or NULL.
504 iobuf = ipv4_reassemble ( iobuf );
509 /* Construct socket addresses and hand off to transport layer */
510 memset ( &src, 0, sizeof ( src ) );
511 src.sin.sin_family = AF_INET;
512 src.sin.sin_addr = iphdr->src;
513 memset ( &dest, 0, sizeof ( dest ) );
514 dest.sin.sin_family = AF_INET;
515 dest.sin.sin_addr = iphdr->dest;
516 if ( ( rc = tcpip_rx ( iobuf, iphdr->protocol, &src.st,
517 &dest.st, pshdr_csum ) ) != 0 ) {
518 DBG ( "IPv4 received packet rejected by stack: %s\n",
531 * Check existence of IPv4 address for ARP
533 * @v netdev Network device
534 * @v net_addr Network-layer address
535 * @ret rc Return status code
537 static int ipv4_arp_check ( struct net_device *netdev, const void *net_addr ) {
538 const struct in_addr *address = net_addr;
539 struct ipv4_miniroute *miniroute;
541 list_for_each_entry ( miniroute, &ipv4_miniroutes, list ) {
542 if ( ( miniroute->netdev == netdev ) &&
543 ( miniroute->address.s_addr == address->s_addr ) ) {
544 /* Found matching address */
552 * Convert IPv4 address to dotted-quad notation
555 * @ret string IP address in dotted-quad notation
557 char * inet_ntoa ( struct in_addr in ) {
558 static char buf[16]; /* "xxx.xxx.xxx.xxx" */
559 uint8_t *bytes = ( uint8_t * ) ∈
561 sprintf ( buf, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3] );
566 * Transcribe IP address
568 * @v net_addr IP address
569 * @ret string IP address in dotted-quad notation
572 static const char * ipv4_ntoa ( const void *net_addr ) {
573 return inet_ntoa ( * ( ( struct in_addr * ) net_addr ) );
577 struct net_protocol ipv4_protocol __net_protocol = {
579 .net_proto = htons ( ETH_P_IP ),
580 .net_addr_len = sizeof ( struct in_addr ),
585 /** IPv4 TCPIP net protocol */
586 struct tcpip_net_protocol ipv4_tcpip_protocol __tcpip_net_protocol = {
588 .sa_family = AF_INET,
592 /** IPv4 ARP protocol */
593 struct arp_net_protocol ipv4_arp_protocol __arp_net_protocol = {
594 .net_protocol = &ipv4_protocol,
595 .check = ipv4_arp_check,