[ipv6] Create routing table based on IPv6 settings
[ipxe.git] / src / net / ipv6.c
1 /*
2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <assert.h>
28 #include <byteswap.h>
29 #include <ipxe/iobuf.h>
30 #include <ipxe/tcpip.h>
31 #include <ipxe/if_ether.h>
32 #include <ipxe/crc32.h>
33 #include <ipxe/fragment.h>
34 #include <ipxe/ipstat.h>
35 #include <ipxe/ndp.h>
36 #include <ipxe/ipv6.h>
37
38 /** @file
39 *
40 * IPv6 protocol
41 *
42 */
43
44 /* Disambiguate the various error causes */
45 #define EINVAL_LEN __einfo_error ( EINFO_EINVAL_LEN )
46 #define EINFO_EINVAL_LEN \
47 __einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid length" )
48 #define ENOTSUP_VER __einfo_error ( EINFO_ENOTSUP_VER )
49 #define EINFO_ENOTSUP_VER \
50 __einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Unsupported version" )
51 #define ENOTSUP_HDR __einfo_error ( EINFO_ENOTSUP_HDR )
52 #define EINFO_ENOTSUP_HDR \
53 __einfo_uniqify ( EINFO_ENOTSUP, 0x02, "Unsupported header type" )
54 #define ENOTSUP_OPT __einfo_error ( EINFO_ENOTSUP_OPT )
55 #define EINFO_ENOTSUP_OPT \
56 __einfo_uniqify ( EINFO_ENOTSUP, 0x03, "Unsupported option" )
57
58 /** List of IPv6 miniroutes */
59 struct list_head ipv6_miniroutes = LIST_HEAD_INIT ( ipv6_miniroutes );
60
61 /** IPv6 statistics */
62 static struct ip_statistics ipv6_stats;
63
64 /** IPv6 statistics family */
65 struct ip_statistics_family
66 ipv6_statistics_family __ip_statistics_family ( IP_STATISTICS_IPV6 ) = {
67 .version = 6,
68 .stats = &ipv6_stats,
69 };
70
71 /**
72 * Determine debugging colour for IPv6 debug messages
73 *
74 * @v in IPv6 address
75 * @ret col Debugging colour (for DBGC())
76 */
77 static uint32_t ipv6col ( struct in6_addr *in ) {
78 return crc32_le ( 0, in, sizeof ( *in ) );
79 }
80
81 /**
82 * Dump IPv6 routing table entry
83 *
84 * @v miniroute Routing table entry
85 */
86 static inline __attribute__ (( always_inline )) void
87 ipv6_dump_miniroute ( struct ipv6_miniroute *miniroute ) {
88 struct net_device *netdev = miniroute->netdev;
89
90 DBGC ( netdev, "IPv6 %s has %s %s/%d", netdev->name,
91 ( ( miniroute->flags & IPV6_HAS_ADDRESS ) ?
92 "address" : "prefix" ),
93 inet6_ntoa ( &miniroute->address ), miniroute->prefix_len );
94 if ( miniroute->flags & IPV6_HAS_ROUTER )
95 DBGC ( netdev, " router %s", inet6_ntoa ( &miniroute->router ));
96 DBGC ( netdev, "\n" );
97 }
98
99 /**
100 * Check if network device has a specific IPv6 address
101 *
102 * @v netdev Network device
103 * @v addr IPv6 address
104 * @ret has_addr Network device has this IPv6 address
105 */
106 int ipv6_has_addr ( struct net_device *netdev, struct in6_addr *addr ) {
107 struct ipv6_miniroute *miniroute;
108
109 list_for_each_entry ( miniroute, &ipv6_miniroutes, list ) {
110 if ( ( miniroute->netdev == netdev ) &&
111 ( miniroute->flags & IPV6_HAS_ADDRESS ) &&
112 ( memcmp ( &miniroute->address, addr,
113 sizeof ( miniroute->address ) ) == 0 ) ) {
114 /* Found matching address */
115 return 1;
116 }
117 }
118 return 0;
119 }
120
121 /**
122 * Check if IPv6 address is within a routing table entry's local network
123 *
124 * @v miniroute Routing table entry
125 * @v address IPv6 address
126 * @ret is_on_link Address is within this entry's local network
127 */
128 static int ipv6_is_on_link ( struct ipv6_miniroute *miniroute,
129 struct in6_addr *address ) {
130 unsigned int i;
131
132 for ( i = 0 ; i < ( sizeof ( address->s6_addr32 ) /
133 sizeof ( address->s6_addr32[0] ) ) ; i++ ) {
134 if ( (( address->s6_addr32[i] ^ miniroute->address.s6_addr32[i])
135 & miniroute->prefix_mask.s6_addr32[i] ) != 0 )
136 return 0;
137 }
138 return 1;
139 }
140
141 /**
142 * Find IPv6 routing table entry for a given address
143 *
144 * @v netdev Network device
145 * @v address IPv6 address
146 * @ret miniroute Routing table entry, or NULL if not found
147 */
148 static struct ipv6_miniroute * ipv6_miniroute ( struct net_device *netdev,
149 struct in6_addr *address ) {
150 struct ipv6_miniroute *miniroute;
151
152 list_for_each_entry ( miniroute, &ipv6_miniroutes, list ) {
153 if ( ( miniroute->netdev == netdev ) &&
154 ipv6_is_on_link ( miniroute, address ) ) {
155 return miniroute;
156 }
157 }
158 return NULL;
159 }
160
161 /**
162 * Add IPv6 routing table entry
163 *
164 * @v netdev Network device
165 * @v address IPv6 address (or prefix)
166 * @v prefix_len Prefix length
167 * @v router Router address (if any)
168 * @ret rc Return status code
169 */
170 static int ipv6_add_miniroute ( struct net_device *netdev,
171 struct in6_addr *address,
172 unsigned int prefix_len,
173 struct in6_addr *router ) {
174 struct ipv6_miniroute *miniroute;
175 uint8_t *prefix_mask;
176 unsigned int remaining;
177 unsigned int i;
178
179 /* Find or create routing table entry */
180 miniroute = ipv6_miniroute ( netdev, address );
181 if ( ! miniroute ) {
182
183 /* Create new routing table entry */
184 miniroute = zalloc ( sizeof ( *miniroute ) );
185 if ( ! miniroute )
186 return -ENOMEM;
187 miniroute->netdev = netdev_get ( netdev );
188 memcpy ( &miniroute->address, address,
189 sizeof ( miniroute->address ) );
190
191 /* Default to prefix length of 64 if none specified */
192 if ( ! prefix_len )
193 prefix_len = IPV6_DEFAULT_PREFIX_LEN;
194 miniroute->prefix_len = prefix_len;
195 assert ( prefix_len <= IPV6_MAX_PREFIX_LEN );
196
197 /* Construct prefix mask */
198 remaining = prefix_len;
199 for ( prefix_mask = miniroute->prefix_mask.s6_addr ;
200 remaining >= 8 ; prefix_mask++, remaining -= 8 ) {
201 *prefix_mask = 0xff;
202 }
203 if ( remaining )
204 *prefix_mask <<= ( 8 - remaining );
205
206 /* Add to list of routes */
207 list_add ( &miniroute->list, &ipv6_miniroutes );
208 }
209
210 /* Set or update address, if applicable */
211 for ( i = 0 ; i < ( sizeof ( address->s6_addr32 ) /
212 sizeof ( address->s6_addr32[0] ) ) ; i++ ) {
213 if ( ( address->s6_addr32[i] &
214 ~miniroute->prefix_mask.s6_addr32[i] ) != 0 ) {
215 memcpy ( &miniroute->address, address,
216 sizeof ( miniroute->address ) );
217 miniroute->flags |= IPV6_HAS_ADDRESS;
218 }
219 }
220 if ( miniroute->prefix_len == IPV6_MAX_PREFIX_LEN )
221 miniroute->flags |= IPV6_HAS_ADDRESS;
222
223 /* Set or update router, if applicable */
224 if ( router ) {
225 memcpy ( &miniroute->router, router,
226 sizeof ( miniroute->router ) );
227 miniroute->flags |= IPV6_HAS_ROUTER;
228 list_del ( &miniroute->list );
229 list_add_tail ( &miniroute->list, &ipv6_miniroutes );
230 }
231
232 ipv6_dump_miniroute ( miniroute );
233 return 0;
234 }
235
236 /**
237 * Delete IPv6 minirouting table entry
238 *
239 * @v miniroute Routing table entry
240 */
241 static void ipv6_del_miniroute ( struct ipv6_miniroute *miniroute ) {
242
243 netdev_put ( miniroute->netdev );
244 list_del ( &miniroute->list );
245 free ( miniroute );
246 }
247
248 /**
249 * Perform IPv6 routing
250 *
251 * @v scope_id Destination address scope ID (for link-local addresses)
252 * @v dest Final destination address
253 * @ret dest Next hop destination address
254 * @ret miniroute Routing table entry to use, or NULL if no route
255 */
256 static struct ipv6_miniroute * ipv6_route ( unsigned int scope_id,
257 struct in6_addr **dest ) {
258 struct ipv6_miniroute *miniroute;
259
260 /* Find first usable route in routing table */
261 list_for_each_entry ( miniroute, &ipv6_miniroutes, list ) {
262
263 /* Skip closed network devices */
264 if ( ! netdev_is_open ( miniroute->netdev ) )
265 continue;
266
267 /* Skip routing table entries with no usable source address */
268 if ( ! ( miniroute->flags & IPV6_HAS_ADDRESS ) )
269 continue;
270
271 if ( IN6_IS_ADDR_NONGLOBAL ( *dest ) ) {
272
273 /* If destination is non-global, and the scope ID
274 * matches this network device, then use this route.
275 */
276 if ( miniroute->netdev->index == scope_id )
277 return miniroute;
278
279 } else {
280
281 /* If destination is an on-link global
282 * address, then use this route.
283 */
284 if ( ipv6_is_on_link ( miniroute, *dest ) )
285 return miniroute;
286
287 /* If destination is an off-link global
288 * address, and we have a default gateway,
289 * then use this route.
290 */
291 if ( miniroute->flags & IPV6_HAS_ROUTER ) {
292 *dest = &miniroute->router;
293 return miniroute;
294 }
295 }
296 }
297
298 return NULL;
299 }
300
301 /**
302 * Determine transmitting network device
303 *
304 * @v st_dest Destination network-layer address
305 * @ret netdev Transmitting network device, or NULL
306 */
307 static struct net_device * ipv6_netdev ( struct sockaddr_tcpip *st_dest ) {
308 struct sockaddr_in6 *sin6_dest = ( ( struct sockaddr_in6 * ) st_dest );
309 struct in6_addr *dest = &sin6_dest->sin6_addr;
310 struct ipv6_miniroute *miniroute;
311
312 /* Find routing table entry */
313 miniroute = ipv6_route ( sin6_dest->sin6_scope_id, &dest );
314 if ( ! miniroute )
315 return NULL;
316
317 return miniroute->netdev;
318 }
319
320 /**
321 * Check that received options can be safely ignored
322 *
323 * @v iphdr IPv6 header
324 * @v options Options extension header
325 * @v len Maximum length of header
326 * @ret rc Return status code
327 */
328 static int ipv6_check_options ( struct ipv6_header *iphdr,
329 struct ipv6_options_header *options,
330 size_t len ) {
331 struct ipv6_option *option = options->options;
332 struct ipv6_option *end = ( ( ( void * ) options ) + len );
333
334 while ( option < end ) {
335 if ( ! IPV6_CAN_IGNORE_OPT ( option->type ) ) {
336 DBGC ( ipv6col ( &iphdr->src ), "IPv6 unrecognised "
337 "option type %#02x:\n", option->type );
338 DBGC_HDA ( ipv6col ( &iphdr->src ), 0,
339 options, len );
340 return -ENOTSUP_OPT;
341 }
342 if ( option->type == IPV6_OPT_PAD1 ) {
343 option = ( ( ( void * ) option ) + 1 );
344 } else {
345 option = ( ( ( void * ) option->value ) + option->len );
346 }
347 }
348 return 0;
349 }
350
351 /**
352 * Check if fragment matches fragment reassembly buffer
353 *
354 * @v fragment Fragment reassembly buffer
355 * @v iobuf I/O buffer
356 * @v hdrlen Length of non-fragmentable potion of I/O buffer
357 * @ret is_fragment Fragment matches this reassembly buffer
358 */
359 static int ipv6_is_fragment ( struct fragment *fragment,
360 struct io_buffer *iobuf, size_t hdrlen ) {
361 struct ipv6_header *frag_iphdr = fragment->iobuf->data;
362 struct ipv6_fragment_header *frag_fhdr =
363 ( fragment->iobuf->data + fragment->hdrlen -
364 sizeof ( *frag_fhdr ) );
365 struct ipv6_header *iphdr = iobuf->data;
366 struct ipv6_fragment_header *fhdr =
367 ( iobuf->data + hdrlen - sizeof ( *fhdr ) );
368
369 return ( ( memcmp ( &iphdr->src, &frag_iphdr->src,
370 sizeof ( iphdr->src ) ) == 0 ) &&
371 ( fhdr->ident == frag_fhdr->ident ) );
372 }
373
374 /**
375 * Get fragment offset
376 *
377 * @v iobuf I/O buffer
378 * @v hdrlen Length of non-fragmentable potion of I/O buffer
379 * @ret offset Offset
380 */
381 static size_t ipv6_fragment_offset ( struct io_buffer *iobuf, size_t hdrlen ) {
382 struct ipv6_fragment_header *fhdr =
383 ( iobuf->data + hdrlen - sizeof ( *fhdr ) );
384
385 return ( ntohs ( fhdr->offset_more ) & IPV6_MASK_OFFSET );
386 }
387
388 /**
389 * Check if more fragments exist
390 *
391 * @v iobuf I/O buffer
392 * @v hdrlen Length of non-fragmentable potion of I/O buffer
393 * @ret more_frags More fragments exist
394 */
395 static int ipv6_more_fragments ( struct io_buffer *iobuf, size_t hdrlen ) {
396 struct ipv6_fragment_header *fhdr =
397 ( iobuf->data + hdrlen - sizeof ( *fhdr ) );
398
399 return ( fhdr->offset_more & htons ( IPV6_MASK_MOREFRAGS ) );
400 }
401
402 /** Fragment reassembler */
403 static struct fragment_reassembler ipv6_reassembler = {
404 .list = LIST_HEAD_INIT ( ipv6_reassembler.list ),
405 .is_fragment = ipv6_is_fragment,
406 .fragment_offset = ipv6_fragment_offset,
407 .more_fragments = ipv6_more_fragments,
408 .stats = &ipv6_stats,
409 };
410
411 /**
412 * Calculate IPv6 pseudo-header checksum
413 *
414 * @v iphdr IPv6 header
415 * @v len Payload length
416 * @v next_header Next header type
417 * @v csum Existing checksum
418 * @ret csum Updated checksum
419 */
420 static uint16_t ipv6_pshdr_chksum ( struct ipv6_header *iphdr, size_t len,
421 int next_header, uint16_t csum ) {
422 struct ipv6_pseudo_header pshdr;
423
424 /* Build pseudo-header */
425 memcpy ( &pshdr.src, &iphdr->src, sizeof ( pshdr.src ) );
426 memcpy ( &pshdr.dest, &iphdr->dest, sizeof ( pshdr.dest ) );
427 pshdr.len = htonl ( len );
428 memset ( pshdr.zero, 0, sizeof ( pshdr.zero ) );
429 pshdr.next_header = next_header;
430
431 /* Update the checksum value */
432 return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
433 }
434
435 /**
436 * Transmit IPv6 packet
437 *
438 * @v iobuf I/O buffer
439 * @v tcpip Transport-layer protocol
440 * @v st_src Source network-layer address
441 * @v st_dest Destination network-layer address
442 * @v netdev Network device to use if no route found, or NULL
443 * @v trans_csum Transport-layer checksum to complete, or NULL
444 * @ret rc Status
445 *
446 * This function expects a transport-layer segment and prepends the
447 * IPv6 header
448 */
449 static int ipv6_tx ( struct io_buffer *iobuf,
450 struct tcpip_protocol *tcpip_protocol,
451 struct sockaddr_tcpip *st_src,
452 struct sockaddr_tcpip *st_dest,
453 struct net_device *netdev,
454 uint16_t *trans_csum ) {
455 struct sockaddr_in6 *sin6_src = ( ( struct sockaddr_in6 * ) st_src );
456 struct sockaddr_in6 *sin6_dest = ( ( struct sockaddr_in6 * ) st_dest );
457 struct ipv6_miniroute *miniroute;
458 struct ipv6_header *iphdr;
459 struct in6_addr *src = NULL;
460 struct in6_addr *next_hop;
461 uint8_t ll_dest_buf[MAX_LL_ADDR_LEN];
462 const void *ll_dest;
463 size_t len;
464 int rc;
465
466 /* Update statistics */
467 ipv6_stats.out_requests++;
468
469 /* Fill up the IPv6 header, except source address */
470 len = iob_len ( iobuf );
471 iphdr = iob_push ( iobuf, sizeof ( *iphdr ) );
472 memset ( iphdr, 0, sizeof ( *iphdr ) );
473 iphdr->ver_tc_label = htonl ( IPV6_VER );
474 iphdr->len = htons ( len );
475 iphdr->next_header = tcpip_protocol->tcpip_proto;
476 iphdr->hop_limit = IPV6_HOP_LIMIT;
477 memcpy ( &iphdr->dest, &sin6_dest->sin6_addr, sizeof ( iphdr->dest ) );
478
479 /* Use routing table to identify next hop and transmitting netdev */
480 next_hop = &iphdr->dest;
481 if ( ( miniroute = ipv6_route ( sin6_dest->sin6_scope_id,
482 &next_hop ) ) != NULL ) {
483 src = &miniroute->address;
484 netdev = miniroute->netdev;
485 }
486 if ( ! netdev ) {
487 DBGC ( ipv6col ( &iphdr->dest ), "IPv6 has no route to %s\n",
488 inet6_ntoa ( &iphdr->dest ) );
489 ipv6_stats.out_no_routes++;
490 rc = -ENETUNREACH;
491 goto err;
492 }
493 if ( sin6_src && ! IN6_IS_ADDR_UNSPECIFIED ( &sin6_src->sin6_addr ) )
494 src = &sin6_src->sin6_addr;
495 if ( src )
496 memcpy ( &iphdr->src, src, sizeof ( iphdr->src ) );
497
498 /* Fix up checksums */
499 if ( trans_csum ) {
500 *trans_csum = ipv6_pshdr_chksum ( iphdr, len,
501 tcpip_protocol->tcpip_proto,
502 *trans_csum );
503 if ( ! *trans_csum )
504 *trans_csum = tcpip_protocol->zero_csum;
505 }
506
507 /* Print IPv6 header for debugging */
508 DBGC2 ( ipv6col ( &iphdr->dest ), "IPv6 TX %s->",
509 inet6_ntoa ( &iphdr->src ) );
510 DBGC2 ( ipv6col ( &iphdr->dest ), "%s len %zd next %d\n",
511 inet6_ntoa ( &iphdr->dest ), len, iphdr->next_header );
512
513 /* Calculate link-layer destination address, if possible */
514 if ( IN6_IS_ADDR_MULTICAST ( next_hop ) ) {
515 /* Multicast address */
516 ipv6_stats.out_mcast_pkts++;
517 if ( ( rc = netdev->ll_protocol->mc_hash ( AF_INET6, next_hop,
518 ll_dest_buf ) ) !=0){
519 DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not hash "
520 "multicast %s: %s\n", inet6_ntoa ( next_hop ),
521 strerror ( rc ) );
522 goto err;
523 }
524 ll_dest = ll_dest_buf;
525 } else {
526 /* Unicast address */
527 ll_dest = NULL;
528 }
529
530 /* Update statistics */
531 ipv6_stats.out_transmits++;
532 ipv6_stats.out_octets += iob_len ( iobuf );
533
534 /* Hand off to link layer (via NDP if applicable) */
535 if ( ll_dest ) {
536 if ( ( rc = net_tx ( iobuf, netdev, &ipv6_protocol, ll_dest,
537 netdev->ll_addr ) ) != 0 ) {
538 DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not "
539 "transmit packet via %s: %s\n",
540 netdev->name, strerror ( rc ) );
541 return rc;
542 }
543 } else {
544 if ( ( rc = ndp_tx ( iobuf, netdev, next_hop, &iphdr->src,
545 netdev->ll_addr ) ) != 0 ) {
546 DBGC ( ipv6col ( &iphdr->dest ), "IPv6 could not "
547 "transmit packet via %s: %s\n",
548 netdev->name, strerror ( rc ) );
549 return rc;
550 }
551 }
552
553 return 0;
554
555 err:
556 free_iob ( iobuf );
557 return rc;
558 }
559
560 /**
561 * Process incoming IPv6 packets
562 *
563 * @v iobuf I/O buffer
564 * @v netdev Network device
565 * @v ll_dest Link-layer destination address
566 * @v ll_source Link-layer destination source
567 * @v flags Packet flags
568 * @ret rc Return status code
569 *
570 * This function expects an IPv6 network datagram. It processes the
571 * headers and sends it to the transport layer.
572 */
573 static int ipv6_rx ( struct io_buffer *iobuf, struct net_device *netdev,
574 const void *ll_dest __unused,
575 const void *ll_source __unused,
576 unsigned int flags __unused ) {
577 struct ipv6_header *iphdr = iobuf->data;
578 union ipv6_extension_header *ext;
579 union {
580 struct sockaddr_in6 sin6;
581 struct sockaddr_tcpip st;
582 } src, dest;
583 uint16_t pshdr_csum;
584 size_t len;
585 size_t hdrlen;
586 size_t extlen;
587 int this_header;
588 int next_header;
589 int rc;
590
591 /* Update statistics */
592 ipv6_stats.in_receives++;
593 ipv6_stats.in_octets += iob_len ( iobuf );
594 if ( flags & LL_BROADCAST ) {
595 ipv6_stats.in_bcast_pkts++;
596 } else if ( flags & LL_MULTICAST ) {
597 ipv6_stats.in_mcast_pkts++;
598 }
599
600 /* Sanity check the IPv6 header */
601 if ( iob_len ( iobuf ) < sizeof ( *iphdr ) ) {
602 DBGC ( ipv6col ( &iphdr->src ), "IPv6 packet too short at %zd "
603 "bytes (min %zd bytes)\n", iob_len ( iobuf ),
604 sizeof ( *iphdr ) );
605 rc = -EINVAL_LEN;
606 goto err_header;
607 }
608 if ( ( iphdr->ver_tc_label & htonl ( IPV6_MASK_VER ) ) !=
609 htonl ( IPV6_VER ) ) {
610 DBGC ( ipv6col ( &iphdr->src ), "IPv6 version %#08x not "
611 "supported\n", ntohl ( iphdr->ver_tc_label ) );
612 rc = -ENOTSUP_VER;
613 goto err_header;
614 }
615
616 /* Truncate packet to specified length */
617 len = ntohs ( iphdr->len );
618 if ( len > iob_len ( iobuf ) ) {
619 DBGC ( ipv6col ( &iphdr->src ), "IPv6 length too long at %zd "
620 "bytes (packet is %zd bytes)\n", len, iob_len ( iobuf ));
621 ipv6_stats.in_truncated_pkts++;
622 rc = -EINVAL_LEN;
623 goto err_other;
624 }
625 iob_unput ( iobuf, ( iob_len ( iobuf ) - len - sizeof ( *iphdr ) ) );
626 hdrlen = sizeof ( *iphdr );
627
628 /* Print IPv6 header for debugging */
629 DBGC2 ( ipv6col ( &iphdr->src ), "IPv6 RX %s<-",
630 inet6_ntoa ( &iphdr->dest ) );
631 DBGC2 ( ipv6col ( &iphdr->src ), "%s len %zd next %d\n",
632 inet6_ntoa ( &iphdr->src ), len, iphdr->next_header );
633
634 /* Discard unicast packets not destined for us */
635 if ( ( ! ( flags & LL_MULTICAST ) ) &&
636 ( ! ipv6_has_addr ( netdev, &iphdr->dest ) ) ) {
637 DBGC ( ipv6col ( &iphdr->src ), "IPv6 discarding non-local "
638 "unicast packet for %s\n", inet6_ntoa ( &iphdr->dest ) );
639 ipv6_stats.in_addr_errors++;
640 rc = -EPIPE;
641 goto err_other;
642 }
643
644 /* Process any extension headers */
645 next_header = iphdr->next_header;
646 while ( 1 ) {
647
648 /* Extract extension header */
649 this_header = next_header;
650 ext = ( iobuf->data + hdrlen );
651 extlen = sizeof ( ext->pad );
652 if ( iob_len ( iobuf ) < ( hdrlen + extlen ) ) {
653 DBGC ( ipv6col ( &iphdr->src ), "IPv6 too short for "
654 "extension header type %d at %zd bytes (min "
655 "%zd bytes)\n", this_header,
656 ( iob_len ( iobuf ) - hdrlen ), extlen );
657 rc = -EINVAL_LEN;
658 goto err_header;
659 }
660
661 /* Determine size of extension header (if applicable) */
662 if ( ( this_header == IPV6_HOPBYHOP ) ||
663 ( this_header == IPV6_DESTINATION ) ||
664 ( this_header == IPV6_ROUTING ) ) {
665 /* Length field is present */
666 extlen += ext->common.len;
667 } else if ( this_header == IPV6_FRAGMENT ) {
668 /* Length field is reserved and ignored (RFC2460) */
669 } else {
670 /* Not an extension header; assume rest is payload */
671 break;
672 }
673 if ( iob_len ( iobuf ) < ( hdrlen + extlen ) ) {
674 DBGC ( ipv6col ( &iphdr->src ), "IPv6 too short for "
675 "extension header type %d at %zd bytes (min "
676 "%zd bytes)\n", this_header,
677 ( iob_len ( iobuf ) - hdrlen ), extlen );
678 rc = -EINVAL_LEN;
679 goto err_header;
680 }
681 hdrlen += extlen;
682 next_header = ext->common.next_header;
683 DBGC2 ( ipv6col ( &iphdr->src ), "IPv6 RX %s<-",
684 inet6_ntoa ( &iphdr->dest ) );
685 DBGC2 ( ipv6col ( &iphdr->src ), "%s ext type %d len %zd next "
686 "%d\n", inet6_ntoa ( &iphdr->src ), this_header,
687 extlen, next_header );
688
689 /* Process this extension header */
690 if ( ( this_header == IPV6_HOPBYHOP ) ||
691 ( this_header == IPV6_DESTINATION ) ) {
692
693 /* Check that all options can be ignored */
694 if ( ( rc = ipv6_check_options ( iphdr, &ext->options,
695 extlen ) ) != 0 )
696 goto err_header;
697
698 } else if ( this_header == IPV6_FRAGMENT ) {
699
700 /* Reassemble fragments */
701 iobuf = fragment_reassemble ( &ipv6_reassembler, iobuf,
702 &hdrlen );
703 if ( ! iobuf )
704 return 0;
705 iphdr = iobuf->data;
706 }
707 }
708
709 /* Construct socket address, calculate pseudo-header checksum,
710 * and hand off to transport layer
711 */
712 memset ( &src, 0, sizeof ( src ) );
713 src.sin6.sin6_family = AF_INET6;
714 memcpy ( &src.sin6.sin6_addr, &iphdr->src,
715 sizeof ( src.sin6.sin6_addr ) );
716 src.sin6.sin6_scope_id = netdev->index;
717 memset ( &dest, 0, sizeof ( dest ) );
718 dest.sin6.sin6_family = AF_INET6;
719 memcpy ( &dest.sin6.sin6_addr, &iphdr->dest,
720 sizeof ( dest.sin6.sin6_addr ) );
721 dest.sin6.sin6_scope_id = netdev->index;
722 iob_pull ( iobuf, hdrlen );
723 pshdr_csum = ipv6_pshdr_chksum ( iphdr, iob_len ( iobuf ),
724 next_header, TCPIP_EMPTY_CSUM );
725 if ( ( rc = tcpip_rx ( iobuf, netdev, next_header, &src.st, &dest.st,
726 pshdr_csum, &ipv6_stats ) ) != 0 ) {
727 DBGC ( ipv6col ( &src.sin6.sin6_addr ), "IPv6 received packet "
728 "rejected by stack: %s\n", strerror ( rc ) );
729 return rc;
730 }
731
732 return 0;
733
734 err_header:
735 ipv6_stats.in_hdr_errors++;
736 err_other:
737 free_iob ( iobuf );
738 return rc;
739 }
740
741 /**
742 * Parse IPv6 address
743 *
744 * @v string IPv6 address string
745 * @ret in IPv6 address to fill in
746 * @ret rc Return status code
747 */
748 int inet6_aton ( const char *string, struct in6_addr *in ) {
749 uint16_t *word = in->s6_addr16;
750 uint16_t *end = ( word + ( sizeof ( in->s6_addr16 ) /
751 sizeof ( in->s6_addr16[0] ) ) );
752 uint16_t *pad = NULL;
753 const char *nptr = string;
754 char *endptr;
755 unsigned long value;
756 size_t pad_len;
757 size_t move_len;
758
759 /* Parse string */
760 while ( 1 ) {
761
762 /* Parse current word */
763 value = strtoul ( nptr, &endptr, 16 );
764 if ( value > 0xffff ) {
765 DBG ( "IPv6 invalid word value %#lx in \"%s\"\n",
766 value, string );
767 return -EINVAL;
768 }
769 *(word++) = htons ( value );
770
771 /* Parse separator */
772 if ( ! *endptr )
773 break;
774 if ( *endptr != ':' ) {
775 DBG ( "IPv6 invalid separator '%c' in \"%s\"\n",
776 *endptr, string );
777 return -EINVAL;
778 }
779 if ( ( endptr == nptr ) && ( nptr != string ) ) {
780 if ( pad ) {
781 DBG ( "IPv6 invalid multiple \"::\" in "
782 "\"%s\"\n", string );
783 return -EINVAL;
784 }
785 pad = word;
786 }
787 nptr = ( endptr + 1 );
788
789 /* Check for overrun */
790 if ( word == end ) {
791 DBG ( "IPv6 too many words in \"%s\"\n", string );
792 return -EINVAL;
793 }
794 }
795
796 /* Insert padding if specified */
797 if ( pad ) {
798 move_len = ( ( ( void * ) word ) - ( ( void * ) pad ) );
799 pad_len = ( ( ( void * ) end ) - ( ( void * ) word ) );
800 memmove ( ( ( ( void * ) pad ) + pad_len ), pad, move_len );
801 memset ( pad, 0, pad_len );
802 } else if ( word != end ) {
803 DBG ( "IPv6 underlength address \"%s\"\n", string );
804 return -EINVAL;
805 }
806
807 return 0;
808 }
809
810 /**
811 * Convert IPv6 address to standard notation
812 *
813 * @v in IPv6 address
814 * @ret string IPv6 address string in canonical format
815 *
816 * RFC5952 defines the canonical format for IPv6 textual representation.
817 */
818 char * inet6_ntoa ( const struct in6_addr *in ) {
819 static char buf[41]; /* ":xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" */
820 char *out = buf;
821 char *longest_start = NULL;
822 char *start = NULL;
823 int longest_len = 1;
824 int len = 0;
825 char *dest;
826 unsigned int i;
827 uint16_t value;
828
829 /* Format address, keeping track of longest run of zeros */
830 for ( i = 0 ; i < ( sizeof ( in->s6_addr16 ) /
831 sizeof ( in->s6_addr16[0] ) ) ; i++ ) {
832 value = ntohs ( in->s6_addr16[i] );
833 if ( value == 0 ) {
834 if ( len++ == 0 )
835 start = out;
836 if ( len > longest_len ) {
837 longest_start = start;
838 longest_len = len;
839 }
840 } else {
841 len = 0;
842 }
843 out += sprintf ( out, ":%x", value );
844 }
845
846 /* Abbreviate longest run of zeros, if applicable */
847 if ( longest_start ) {
848 dest = strcpy ( ( longest_start + 1 ),
849 ( longest_start + ( 2 * longest_len ) ) );
850 if ( dest[0] == '\0' )
851 dest[1] = '\0';
852 dest[0] = ':';
853 }
854 return ( ( longest_start == buf ) ? buf : ( buf + 1 ) );
855 }
856
857 /**
858 * Transcribe IPv6 address
859 *
860 * @v net_addr IPv6 address
861 * @ret string IPv6 address in standard notation
862 *
863 */
864 static const char * ipv6_ntoa ( const void *net_addr ) {
865 return inet6_ntoa ( net_addr );
866 }
867
868 /**
869 * Transcribe IPv6 socket address
870 *
871 * @v sa Socket address
872 * @ret string Socket address in standard notation
873 */
874 static const char * ipv6_sock_ntoa ( struct sockaddr *sa ) {
875 static char buf[ 39 /* "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" */ +
876 1 /* "%" */ + NETDEV_NAME_LEN + 1 /* NUL */ ];
877 struct sockaddr_in6 *sin6 = ( ( struct sockaddr_in6 * ) sa );
878 struct in6_addr *in = &sin6->sin6_addr;
879 struct net_device *netdev;
880 const char *netdev_name;
881
882 /* Identify network device, if applicable */
883 if ( IN6_IS_ADDR_NONGLOBAL ( in ) ) {
884 netdev = find_netdev_by_index ( sin6->sin6_scope_id );
885 netdev_name = ( netdev ? netdev->name : "UNKNOWN" );
886 } else {
887 netdev_name = NULL;
888 }
889
890 /* Format socket address */
891 snprintf ( buf, sizeof ( buf ), "%s%s%s", inet6_ntoa ( in ),
892 ( netdev_name ? "%" : "" ),
893 ( netdev_name ? netdev_name : "" ) );
894 return buf;
895 }
896
897 /**
898 * Parse IPv6 socket address
899 *
900 * @v string Socket address string
901 * @v sa Socket address to fill in
902 * @ret rc Return status code
903 */
904 static int ipv6_sock_aton ( const char *string, struct sockaddr *sa ) {
905 struct sockaddr_in6 *sin6 = ( ( struct sockaddr_in6 * ) sa );
906 struct in6_addr in;
907 struct net_device *netdev;
908 size_t len;
909 char *tmp;
910 char *in_string;
911 char *netdev_string;
912 int rc;
913
914 /* Create modifiable copy of string */
915 tmp = strdup ( string );
916 if ( ! tmp ) {
917 rc = -ENOMEM;
918 goto err_alloc;
919 }
920 in_string = tmp;
921
922 /* Strip surrounding "[...]", if present */
923 len = strlen ( in_string );
924 if ( ( in_string[0] == '[' ) && ( in_string[ len - 1 ] == ']' ) ) {
925 in_string[ len - 1 ] = '\0';
926 in_string++;
927 }
928
929 /* Split at network device name, if present */
930 netdev_string = strchr ( in_string, '%' );
931 if ( netdev_string )
932 *(netdev_string++) = '\0';
933
934 /* Parse IPv6 address portion */
935 if ( ( rc = inet6_aton ( in_string, &in ) ) != 0 )
936 goto err_inet6_aton;
937
938 /* Parse scope ID, if applicable */
939 if ( netdev_string ) {
940
941 /* Parse explicit network device name, if present */
942 netdev = find_netdev ( netdev_string );
943 if ( ! netdev ) {
944 rc = -ENODEV;
945 goto err_find_netdev;
946 }
947 sin6->sin6_scope_id = netdev->index;
948
949 } else if ( IN6_IS_ADDR_NONGLOBAL ( &in ) ) {
950
951 /* If no network device is explicitly specified for a
952 * link-local or multicast address, default to using
953 * "netX" (if existent).
954 */
955 netdev = last_opened_netdev();
956 if ( netdev )
957 sin6->sin6_scope_id = netdev->index;
958 }
959
960 /* Copy IPv6 address portion to socket address */
961 memcpy ( &sin6->sin6_addr, &in, sizeof ( sin6->sin6_addr ) );
962
963 err_find_netdev:
964 err_inet6_aton:
965 free ( tmp );
966 err_alloc:
967 return rc;
968 }
969
970 /** IPv6 protocol */
971 struct net_protocol ipv6_protocol __net_protocol = {
972 .name = "IPv6",
973 .net_proto = htons ( ETH_P_IPV6 ),
974 .net_addr_len = sizeof ( struct in6_addr ),
975 .rx = ipv6_rx,
976 .ntoa = ipv6_ntoa,
977 };
978
979 /** IPv6 TCPIP net protocol */
980 struct tcpip_net_protocol ipv6_tcpip_protocol __tcpip_net_protocol = {
981 .name = "IPv6",
982 .sa_family = AF_INET6,
983 .header_len = sizeof ( struct ipv6_header ),
984 .net_protocol = &ipv6_protocol,
985 .tx = ipv6_tx,
986 .netdev = ipv6_netdev,
987 };
988
989 /** IPv6 socket address converter */
990 struct sockaddr_converter ipv6_sockaddr_converter __sockaddr_converter = {
991 .family = AF_INET6,
992 .ntoa = ipv6_sock_ntoa,
993 .aton = ipv6_sock_aton,
994 };
995
996 /**
997 * Parse IPv6 address setting value
998 *
999 * @v type Setting type
1000 * @v value Formatted setting value
1001 * @v buf Buffer to contain raw value
1002 * @v len Length of buffer
1003 * @ret len Length of raw value, or negative error
1004 */
1005 int parse_ipv6_setting ( const struct setting_type *type __unused,
1006 const char *value, void *buf, size_t len ) {
1007 struct in6_addr ipv6;
1008 int rc;
1009
1010 /* Parse IPv6 address */
1011 if ( ( rc = inet6_aton ( value, &ipv6 ) ) != 0 )
1012 return rc;
1013
1014 /* Copy to buffer */
1015 if ( len > sizeof ( ipv6 ) )
1016 len = sizeof ( ipv6 );
1017 memcpy ( buf, &ipv6, len );
1018
1019 return ( sizeof ( ipv6 ) );
1020 }
1021
1022 /**
1023 * Format IPv6 address setting value
1024 *
1025 * @v type Setting type
1026 * @v raw Raw setting value
1027 * @v raw_len Length of raw setting value
1028 * @v buf Buffer to contain formatted value
1029 * @v len Length of buffer
1030 * @ret len Length of formatted value, or negative error
1031 */
1032 int format_ipv6_setting ( const struct setting_type *type __unused,
1033 const void *raw, size_t raw_len, char *buf,
1034 size_t len ) {
1035 const struct in6_addr *ipv6 = raw;
1036
1037 if ( raw_len < sizeof ( *ipv6 ) )
1038 return -EINVAL;
1039 return snprintf ( buf, len, "%s", inet6_ntoa ( ipv6 ) );
1040 }
1041
1042 /** IPv6 settings scope */
1043 const struct settings_scope ipv6_scope;
1044
1045 /** IPv6 address setting */
1046 const struct setting ip6_setting __setting ( SETTING_IP6, ip6 ) = {
1047 .name = "ip6",
1048 .description = "IPv6 address",
1049 .type = &setting_type_ipv6,
1050 .scope = &ipv6_scope,
1051 };
1052
1053 /** IPv6 prefix length setting */
1054 const struct setting len6_setting __setting ( SETTING_IP6, len6 ) = {
1055 .name = "len6",
1056 .description = "IPv6 prefix length",
1057 .type = &setting_type_int8,
1058 .scope = &ipv6_scope,
1059 };
1060
1061 /** Default gateway setting */
1062 const struct setting gateway6_setting __setting ( SETTING_IP6, gateway6 ) = {
1063 .name = "gateway6",
1064 .description = "IPv6 gateway",
1065 .type = &setting_type_ipv6,
1066 .scope = &ipv6_scope,
1067 };
1068
1069 /**
1070 * Check applicability of IPv6 link-local address setting
1071 *
1072 * @v settings Settings block
1073 * @v setting Setting to fetch
1074 * @ret applies Setting applies within this settings block
1075 */
1076 static int ipv6_applies ( struct settings *settings __unused,
1077 const struct setting *setting ) {
1078
1079 return ( setting->scope == &ipv6_scope );
1080 }
1081
1082 /**
1083 * Fetch IPv6 link-local address setting
1084 *
1085 * @v settings Settings block
1086 * @v setting Setting to fetch
1087 * @v data Buffer to fill with setting data
1088 * @v len Length of buffer
1089 * @ret len Length of setting data, or negative error
1090 */
1091 static int ipv6_fetch ( struct settings *settings, struct setting *setting,
1092 void *data, size_t len ) {
1093 struct net_device *netdev =
1094 container_of ( settings->parent, struct net_device,
1095 settings.settings );
1096 struct in6_addr ip6;
1097 uint8_t *len6;
1098 int prefix_len;
1099 int rc;
1100
1101 /* Construct link-local address from EUI-64 as per RFC 2464 */
1102 memset ( &ip6, 0, sizeof ( ip6 ) );
1103 prefix_len = ipv6_link_local ( &ip6, netdev );
1104 if ( prefix_len < 0 ) {
1105 rc = prefix_len;
1106 return rc;
1107 }
1108
1109 /* Handle setting */
1110 if ( setting_cmp ( setting, &ip6_setting ) == 0 ) {
1111
1112 /* Return link-local ip6 */
1113 if ( len > sizeof ( ip6 ) )
1114 len = sizeof ( ip6 );
1115 memcpy ( data, &ip6, len );
1116 return sizeof ( ip6 );
1117
1118 } else if ( setting_cmp ( setting, &len6_setting ) == 0 ) {
1119
1120 /* Return prefix length */
1121 if ( len ) {
1122 len6 = data;
1123 *len6 = prefix_len;
1124 }
1125 return sizeof ( *len6 );
1126
1127 }
1128
1129 return -ENOENT;
1130 }
1131
1132 /** IPv6 link-local address settings operations */
1133 static struct settings_operations ipv6_settings_operations = {
1134 .applies = ipv6_applies,
1135 .fetch = ipv6_fetch,
1136 };
1137
1138 /** IPv6 link-local address settings */
1139 struct ipv6_settings {
1140 /** Reference counter */
1141 struct refcnt refcnt;
1142 /** Settings interface */
1143 struct settings settings;
1144 };
1145
1146 /**
1147 * Register IPv6 link-local address settings
1148 *
1149 * @v netdev Network device
1150 * @ret rc Return status code
1151 */
1152 static int ipv6_register_settings ( struct net_device *netdev ) {
1153 struct settings *parent = netdev_settings ( netdev );
1154 struct ipv6_settings *ipv6set;
1155 int rc;
1156
1157 /* Allocate and initialise structure */
1158 ipv6set = zalloc ( sizeof ( *ipv6set ) );
1159 if ( ! ipv6set ) {
1160 rc = -ENOMEM;
1161 goto err_alloc;
1162 }
1163 ref_init ( &ipv6set->refcnt, NULL );
1164 settings_init ( &ipv6set->settings, &ipv6_settings_operations,
1165 &ipv6set->refcnt, &ipv6_scope );
1166 ipv6set->settings.order = IPV6_ORDER_LINK_LOCAL;
1167
1168 /* Register settings */
1169 if ( ( rc = register_settings ( &ipv6set->settings, parent,
1170 IPV6_SETTINGS_NAME ) ) != 0 )
1171 goto err_register;
1172
1173 err_register:
1174 ref_put ( &ipv6set->refcnt );
1175 err_alloc:
1176 return rc;
1177 }
1178
1179 /** IPv6 network device driver */
1180 struct net_driver ipv6_driver __net_driver = {
1181 .name = "IPv6",
1182 .probe = ipv6_register_settings,
1183 };
1184
1185 /**
1186 * Create IPv6 routing table based on configured settings
1187 *
1188 * @v netdev Network device
1189 * @v settings Settings block
1190 * @ret rc Return status code
1191 */
1192 static int ipv6_create_routes ( struct net_device *netdev,
1193 struct settings *settings ) {
1194 struct settings *child;
1195 struct settings *origin;
1196 struct in6_addr ip6_buf;
1197 struct in6_addr gateway6_buf;
1198 struct in6_addr *ip6 = &ip6_buf;
1199 struct in6_addr *gateway6 = &gateway6_buf;
1200 uint8_t len6;
1201 size_t len;
1202 int rc;
1203
1204 /* First, create routing table for any child settings. We do
1205 * this depth-first and in reverse order so that the end
1206 * result reflects the relative priorities of the settings
1207 * blocks.
1208 */
1209 list_for_each_entry_reverse ( child, &settings->children, siblings )
1210 ipv6_create_routes ( netdev, child );
1211
1212 /* Fetch IPv6 address, if any */
1213 len = fetch_setting ( settings, &ip6_setting, &origin, NULL,
1214 ip6, sizeof ( *ip6 ) );
1215 if ( ( len != sizeof ( *ip6 ) ) || ( origin != settings ) )
1216 return 0;
1217
1218 /* Fetch prefix length, if defined */
1219 len = fetch_setting ( settings, &len6_setting, &origin, NULL,
1220 &len6, sizeof ( len6 ) );
1221 if ( ( len != sizeof ( len6 ) ) || ( origin != settings ) )
1222 len6 = 0;
1223 if ( len6 > IPV6_MAX_PREFIX_LEN )
1224 len6 = IPV6_MAX_PREFIX_LEN;
1225
1226 /* Fetch gateway, if defined */
1227 len = fetch_setting ( settings, &gateway6_setting, &origin, NULL,
1228 gateway6, sizeof ( *gateway6 ) );
1229 if ( ( len != sizeof ( *gateway6 ) ) || ( origin != settings ) )
1230 gateway6 = NULL;
1231
1232 /* Create or update route */
1233 if ( ( rc = ipv6_add_miniroute ( netdev, ip6, len6, gateway6 ) ) != 0){
1234 DBGC ( netdev, "IPv6 %s could not add route: %s\n",
1235 netdev->name, strerror ( rc ) );
1236 return rc;
1237 }
1238
1239 return 0;
1240 }
1241
1242 /**
1243 * Create IPv6 routing table based on configured settings
1244 *
1245 * @ret rc Return status code
1246 */
1247 static int ipv6_create_all_routes ( void ) {
1248 struct ipv6_miniroute *miniroute;
1249 struct ipv6_miniroute *tmp;
1250 struct net_device *netdev;
1251 struct settings *settings;
1252 int rc;
1253
1254 /* Delete all existing routes */
1255 list_for_each_entry_safe ( miniroute, tmp, &ipv6_miniroutes, list )
1256 ipv6_del_miniroute ( miniroute );
1257
1258 /* Create routes for each configured network device */
1259 for_each_netdev ( netdev ) {
1260 settings = netdev_settings ( netdev );
1261 if ( ( rc = ipv6_create_routes ( netdev, settings ) ) != 0 )
1262 return rc;
1263 }
1264
1265 return 0;
1266 }
1267
1268 /** IPv6 settings applicator */
1269 struct settings_applicator ipv6_settings_applicator __settings_applicator = {
1270 .apply = ipv6_create_all_routes,
1271 };
1272
1273 /* Drag in objects via ipv6_protocol */
1274 REQUIRING_SYMBOL ( ipv6_protocol );
1275
1276 /* Drag in ICMPv6 */
1277 REQUIRE_OBJECT ( icmpv6 );
1278
1279 /* Drag in NDP */
1280 REQUIRE_OBJECT ( ndp );