2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER
);
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>
36 #include <ipxe/ipv6.h>
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" )
58 /** List of IPv6 miniroutes */
59 struct list_head ipv6_miniroutes
= LIST_HEAD_INIT ( ipv6_miniroutes
);
61 /** IPv6 statistics */
62 static struct ip_statistics ipv6_stats
;
64 /** IPv6 statistics family */
65 struct ip_statistics_family
66 ipv6_statistics_family
__ip_statistics_family ( IP_STATISTICS_IPV6
) = {
72 * Determine debugging colour for IPv6 debug messages
75 * @ret col Debugging colour (for DBGC())
77 static uint32_t ipv6col ( struct in6_addr
*in
) {
78 return crc32_le ( 0, in
, sizeof ( *in
) );
82 * Dump IPv6 routing table entry
84 * @v miniroute Routing table entry
86 static inline __attribute__ (( always_inline
)) void
87 ipv6_dump_miniroute ( struct ipv6_miniroute
*miniroute
) {
88 struct net_device
*netdev
= miniroute
->netdev
;
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" );
100 * Check if network device has a specific IPv6 address
102 * @v netdev Network device
103 * @v addr IPv6 address
104 * @ret has_addr Network device has this IPv6 address
106 int ipv6_has_addr ( struct net_device
*netdev
, struct in6_addr
*addr
) {
107 struct ipv6_miniroute
*miniroute
;
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 */
122 * Check if IPv6 address is within a routing table entry's local network
124 * @v miniroute Routing table entry
125 * @v address IPv6 address
126 * @ret is_on_link Address is within this entry's local network
128 static int ipv6_is_on_link ( struct ipv6_miniroute
*miniroute
,
129 struct in6_addr
*address
) {
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 )
142 * Find IPv6 routing table entry for a given address
144 * @v netdev Network device
145 * @v address IPv6 address
146 * @ret miniroute Routing table entry, or NULL if not found
148 static struct ipv6_miniroute
* ipv6_miniroute ( struct net_device
*netdev
,
149 struct in6_addr
*address
) {
150 struct ipv6_miniroute
*miniroute
;
152 list_for_each_entry ( miniroute
, &ipv6_miniroutes
, list
) {
153 if ( ( miniroute
->netdev
== netdev
) &&
154 ipv6_is_on_link ( miniroute
, address
) ) {
162 * Add IPv6 routing table entry
164 * @v netdev Network device
165 * @v address IPv6 address (or prefix)
166 * @v prefix_len Prefix length
168 * @ret miniroute Routing table entry, or NULL on failure
170 static struct ipv6_miniroute
* ipv6_add_miniroute ( struct net_device
*netdev
,
171 struct in6_addr
*address
,
172 unsigned int prefix_len
,
173 unsigned int flags
) {
174 struct ipv6_miniroute
*miniroute
;
175 uint8_t *prefix_mask
;
177 /* Create routing table entry */
178 miniroute
= zalloc ( sizeof ( *miniroute
) );
181 miniroute
->netdev
= netdev_get ( netdev
);
182 memcpy ( &miniroute
->address
, address
, sizeof ( miniroute
->address
) );
183 miniroute
->prefix_len
= prefix_len
;
184 assert ( prefix_len
<= ( 8 * sizeof ( miniroute
->prefix_mask
) ) );
185 for ( prefix_mask
= miniroute
->prefix_mask
.s6_addr
; prefix_len
>= 8 ;
186 prefix_mask
++, prefix_len
-= 8 ) {
190 *prefix_mask
<<= ( 8 - prefix_len
);
191 miniroute
->flags
= flags
;
192 list_add ( &miniroute
->list
, &ipv6_miniroutes
);
193 ipv6_dump_miniroute ( miniroute
);
199 * Define IPv6 on-link prefix
201 * @v netdev Network device
202 * @v prefix IPv6 address prefix
203 * @v prefix_len Prefix length
204 * @v router Router address (or NULL)
205 * @ret rc Return status code
207 int ipv6_set_prefix ( struct net_device
*netdev
, struct in6_addr
*prefix
,
208 unsigned int prefix_len
, struct in6_addr
*router
) {
209 struct ipv6_miniroute
*miniroute
;
212 /* Find or create routing table entry */
213 miniroute
= ipv6_miniroute ( netdev
, prefix
);
215 miniroute
= ipv6_add_miniroute ( netdev
, prefix
, prefix_len
, 0);
219 /* Record router and add to start or end of list as appropriate */
220 list_del ( &miniroute
->list
);
222 changed
= ( ( ! ( miniroute
->flags
& IPV6_HAS_ROUTER
) ) ||
223 ( memcmp ( &miniroute
->router
, router
,
224 sizeof ( miniroute
->router
) ) != 0 ) );
225 miniroute
->flags
|= IPV6_HAS_ROUTER
;
226 memcpy ( &miniroute
->router
, router
,
227 sizeof ( miniroute
->router
) );
228 list_add_tail ( &miniroute
->list
, &ipv6_miniroutes
);
230 changed
= ( miniroute
->flags
& IPV6_HAS_ROUTER
);
231 miniroute
->flags
&= ~IPV6_HAS_ROUTER
;
232 list_add ( &miniroute
->list
, &ipv6_miniroutes
);
235 ipv6_dump_miniroute ( miniroute
);
241 * Add IPv6 on-link address
243 * @v netdev Network device
244 * @v address IPv6 address
245 * @ret rc Return status code
247 * An on-link prefix for the address must already exist.
249 int ipv6_set_address ( struct net_device
*netdev
, struct in6_addr
*address
) {
250 struct ipv6_miniroute
*miniroute
;
253 /* Find routing table entry */
254 miniroute
= ipv6_miniroute ( netdev
, address
);
256 return -EADDRNOTAVAIL
;
259 changed
= ( ( ! ( miniroute
->flags
& IPV6_HAS_ADDRESS
) ) ||
260 ( memcmp ( &miniroute
->address
, address
,
261 sizeof ( miniroute
->address
) ) != 0 ) );
262 memcpy ( &miniroute
->address
, address
, sizeof ( miniroute
->address
) );
263 miniroute
->flags
|= IPV6_HAS_ADDRESS
;
265 ipv6_dump_miniroute ( miniroute
);
271 * Perform IPv6 routing
273 * @v scope_id Destination address scope ID (for link-local addresses)
274 * @v dest Final destination address
275 * @ret dest Next hop destination address
276 * @ret miniroute Routing table entry to use, or NULL if no route
278 static struct ipv6_miniroute
* ipv6_route ( unsigned int scope_id
,
279 struct in6_addr
**dest
) {
280 struct ipv6_miniroute
*miniroute
;
282 /* Find first usable route in routing table */
283 list_for_each_entry ( miniroute
, &ipv6_miniroutes
, list
) {
285 /* Skip closed network devices */
286 if ( ! netdev_is_open ( miniroute
->netdev
) )
289 /* Skip routing table entries with no usable source address */
290 if ( ! ( miniroute
->flags
& IPV6_HAS_ADDRESS
) )
293 if ( IN6_IS_ADDR_NONGLOBAL ( *dest
) ) {
295 /* If destination is non-global, and the scope ID
296 * matches this network device, then use this route.
298 if ( miniroute
->netdev
->index
== scope_id
)
303 /* If destination is an on-link global
304 * address, then use this route.
306 if ( ipv6_is_on_link ( miniroute
, *dest
) )
309 /* If destination is an off-link global
310 * address, and we have a default gateway,
311 * then use this route.
313 if ( miniroute
->flags
& IPV6_HAS_ROUTER
) {
314 *dest
= &miniroute
->router
;
324 * Determine transmitting network device
326 * @v st_dest Destination network-layer address
327 * @ret netdev Transmitting network device, or NULL
329 static struct net_device
* ipv6_netdev ( struct sockaddr_tcpip
*st_dest
) {
330 struct sockaddr_in6
*sin6_dest
= ( ( struct sockaddr_in6
* ) st_dest
);
331 struct in6_addr
*dest
= &sin6_dest
->sin6_addr
;
332 struct ipv6_miniroute
*miniroute
;
334 /* Find routing table entry */
335 miniroute
= ipv6_route ( sin6_dest
->sin6_scope_id
, &dest
);
339 return miniroute
->netdev
;
343 * Check that received options can be safely ignored
345 * @v iphdr IPv6 header
346 * @v options Options extension header
347 * @v len Maximum length of header
348 * @ret rc Return status code
350 static int ipv6_check_options ( struct ipv6_header
*iphdr
,
351 struct ipv6_options_header
*options
,
353 struct ipv6_option
*option
= options
->options
;
354 struct ipv6_option
*end
= ( ( ( void * ) options
) + len
);
356 while ( option
< end
) {
357 if ( ! IPV6_CAN_IGNORE_OPT ( option
->type
) ) {
358 DBGC ( ipv6col ( &iphdr
->src
), "IPv6 unrecognised "
359 "option type %#02x:\n", option
->type
);
360 DBGC_HDA ( ipv6col ( &iphdr
->src
), 0,
364 if ( option
->type
== IPV6_OPT_PAD1
) {
365 option
= ( ( ( void * ) option
) + 1 );
367 option
= ( ( ( void * ) option
->value
) + option
->len
);
374 * Check if fragment matches fragment reassembly buffer
376 * @v fragment Fragment reassembly buffer
377 * @v iobuf I/O buffer
378 * @v hdrlen Length of non-fragmentable potion of I/O buffer
379 * @ret is_fragment Fragment matches this reassembly buffer
381 static int ipv6_is_fragment ( struct fragment
*fragment
,
382 struct io_buffer
*iobuf
, size_t hdrlen
) {
383 struct ipv6_header
*frag_iphdr
= fragment
->iobuf
->data
;
384 struct ipv6_fragment_header
*frag_fhdr
=
385 ( fragment
->iobuf
->data
+ fragment
->hdrlen
-
386 sizeof ( *frag_fhdr
) );
387 struct ipv6_header
*iphdr
= iobuf
->data
;
388 struct ipv6_fragment_header
*fhdr
=
389 ( iobuf
->data
+ hdrlen
- sizeof ( *fhdr
) );
391 return ( ( memcmp ( &iphdr
->src
, &frag_iphdr
->src
,
392 sizeof ( iphdr
->src
) ) == 0 ) &&
393 ( fhdr
->ident
== frag_fhdr
->ident
) );
397 * Get fragment offset
399 * @v iobuf I/O buffer
400 * @v hdrlen Length of non-fragmentable potion of I/O buffer
403 static size_t ipv6_fragment_offset ( struct io_buffer
*iobuf
, size_t hdrlen
) {
404 struct ipv6_fragment_header
*fhdr
=
405 ( iobuf
->data
+ hdrlen
- sizeof ( *fhdr
) );
407 return ( ntohs ( fhdr
->offset_more
) & IPV6_MASK_OFFSET
);
411 * Check if more fragments exist
413 * @v iobuf I/O buffer
414 * @v hdrlen Length of non-fragmentable potion of I/O buffer
415 * @ret more_frags More fragments exist
417 static int ipv6_more_fragments ( struct io_buffer
*iobuf
, size_t hdrlen
) {
418 struct ipv6_fragment_header
*fhdr
=
419 ( iobuf
->data
+ hdrlen
- sizeof ( *fhdr
) );
421 return ( fhdr
->offset_more
& htons ( IPV6_MASK_MOREFRAGS
) );
424 /** Fragment reassembler */
425 static struct fragment_reassembler ipv6_reassembler
= {
426 .list
= LIST_HEAD_INIT ( ipv6_reassembler
.list
),
427 .is_fragment
= ipv6_is_fragment
,
428 .fragment_offset
= ipv6_fragment_offset
,
429 .more_fragments
= ipv6_more_fragments
,
430 .stats
= &ipv6_stats
,
434 * Calculate IPv6 pseudo-header checksum
436 * @v iphdr IPv6 header
437 * @v len Payload length
438 * @v next_header Next header type
439 * @v csum Existing checksum
440 * @ret csum Updated checksum
442 static uint16_t ipv6_pshdr_chksum ( struct ipv6_header
*iphdr
, size_t len
,
443 int next_header
, uint16_t csum
) {
444 struct ipv6_pseudo_header pshdr
;
446 /* Build pseudo-header */
447 memcpy ( &pshdr
.src
, &iphdr
->src
, sizeof ( pshdr
.src
) );
448 memcpy ( &pshdr
.dest
, &iphdr
->dest
, sizeof ( pshdr
.dest
) );
449 pshdr
.len
= htonl ( len
);
450 memset ( pshdr
.zero
, 0, sizeof ( pshdr
.zero
) );
451 pshdr
.next_header
= next_header
;
453 /* Update the checksum value */
454 return tcpip_continue_chksum ( csum
, &pshdr
, sizeof ( pshdr
) );
458 * Transmit IPv6 packet
460 * @v iobuf I/O buffer
461 * @v tcpip Transport-layer protocol
462 * @v st_src Source network-layer address
463 * @v st_dest Destination network-layer address
464 * @v netdev Network device to use if no route found, or NULL
465 * @v trans_csum Transport-layer checksum to complete, or NULL
468 * This function expects a transport-layer segment and prepends the
471 static int ipv6_tx ( struct io_buffer
*iobuf
,
472 struct tcpip_protocol
*tcpip_protocol
,
473 struct sockaddr_tcpip
*st_src
,
474 struct sockaddr_tcpip
*st_dest
,
475 struct net_device
*netdev
,
476 uint16_t *trans_csum
) {
477 struct sockaddr_in6
*sin6_src
= ( ( struct sockaddr_in6
* ) st_src
);
478 struct sockaddr_in6
*sin6_dest
= ( ( struct sockaddr_in6
* ) st_dest
);
479 struct ipv6_miniroute
*miniroute
;
480 struct ipv6_header
*iphdr
;
481 struct in6_addr
*src
= NULL
;
482 struct in6_addr
*next_hop
;
483 uint8_t ll_dest_buf
[MAX_LL_ADDR_LEN
];
488 /* Update statistics */
489 ipv6_stats
.out_requests
++;
491 /* Fill up the IPv6 header, except source address */
492 len
= iob_len ( iobuf
);
493 iphdr
= iob_push ( iobuf
, sizeof ( *iphdr
) );
494 memset ( iphdr
, 0, sizeof ( *iphdr
) );
495 iphdr
->ver_tc_label
= htonl ( IPV6_VER
);
496 iphdr
->len
= htons ( len
);
497 iphdr
->next_header
= tcpip_protocol
->tcpip_proto
;
498 iphdr
->hop_limit
= IPV6_HOP_LIMIT
;
499 memcpy ( &iphdr
->dest
, &sin6_dest
->sin6_addr
, sizeof ( iphdr
->dest
) );
501 /* Use routing table to identify next hop and transmitting netdev */
502 next_hop
= &iphdr
->dest
;
503 if ( ( miniroute
= ipv6_route ( sin6_dest
->sin6_scope_id
,
504 &next_hop
) ) != NULL
) {
505 src
= &miniroute
->address
;
506 netdev
= miniroute
->netdev
;
509 DBGC ( ipv6col ( &iphdr
->dest
), "IPv6 has no route to %s\n",
510 inet6_ntoa ( &iphdr
->dest
) );
511 ipv6_stats
.out_no_routes
++;
515 if ( sin6_src
&& ! IN6_IS_ADDR_UNSPECIFIED ( &sin6_src
->sin6_addr
) )
516 src
= &sin6_src
->sin6_addr
;
518 memcpy ( &iphdr
->src
, src
, sizeof ( iphdr
->src
) );
520 /* Fix up checksums */
522 *trans_csum
= ipv6_pshdr_chksum ( iphdr
, len
,
523 tcpip_protocol
->tcpip_proto
,
527 /* Print IPv6 header for debugging */
528 DBGC2 ( ipv6col ( &iphdr
->dest
), "IPv6 TX %s->",
529 inet6_ntoa ( &iphdr
->src
) );
530 DBGC2 ( ipv6col ( &iphdr
->dest
), "%s len %zd next %d\n",
531 inet6_ntoa ( &iphdr
->dest
), len
, iphdr
->next_header
);
533 /* Calculate link-layer destination address, if possible */
534 if ( IN6_IS_ADDR_MULTICAST ( next_hop
) ) {
535 /* Multicast address */
536 ipv6_stats
.out_mcast_pkts
++;
537 if ( ( rc
= netdev
->ll_protocol
->mc_hash ( AF_INET6
, next_hop
,
538 ll_dest_buf
) ) !=0){
539 DBGC ( ipv6col ( &iphdr
->dest
), "IPv6 could not hash "
540 "multicast %s: %s\n", inet6_ntoa ( next_hop
),
544 ll_dest
= ll_dest_buf
;
546 /* Unicast address */
550 /* Update statistics */
551 ipv6_stats
.out_transmits
++;
552 ipv6_stats
.out_octets
+= iob_len ( iobuf
);
554 /* Hand off to link layer (via NDP if applicable) */
556 if ( ( rc
= net_tx ( iobuf
, netdev
, &ipv6_protocol
, ll_dest
,
557 netdev
->ll_addr
) ) != 0 ) {
558 DBGC ( ipv6col ( &iphdr
->dest
), "IPv6 could not "
559 "transmit packet via %s: %s\n",
560 netdev
->name
, strerror ( rc
) );
564 if ( ( rc
= ndp_tx ( iobuf
, netdev
, next_hop
, &iphdr
->src
,
565 netdev
->ll_addr
) ) != 0 ) {
566 DBGC ( ipv6col ( &iphdr
->dest
), "IPv6 could not "
567 "transmit packet via %s: %s\n",
568 netdev
->name
, strerror ( rc
) );
581 * Process incoming IPv6 packets
583 * @v iobuf I/O buffer
584 * @v netdev Network device
585 * @v ll_dest Link-layer destination address
586 * @v ll_source Link-layer destination source
587 * @v flags Packet flags
588 * @ret rc Return status code
590 * This function expects an IPv6 network datagram. It processes the
591 * headers and sends it to the transport layer.
593 static int ipv6_rx ( struct io_buffer
*iobuf
, struct net_device
*netdev
,
594 const void *ll_dest __unused
,
595 const void *ll_source __unused
,
596 unsigned int flags __unused
) {
597 struct ipv6_header
*iphdr
= iobuf
->data
;
598 union ipv6_extension_header
*ext
;
600 struct sockaddr_in6 sin6
;
601 struct sockaddr_tcpip st
;
611 /* Update statistics */
612 ipv6_stats
.in_receives
++;
613 ipv6_stats
.in_octets
+= iob_len ( iobuf
);
614 if ( flags
& LL_BROADCAST
) {
615 ipv6_stats
.in_bcast_pkts
++;
616 } else if ( flags
& LL_MULTICAST
) {
617 ipv6_stats
.in_mcast_pkts
++;
620 /* Sanity check the IPv6 header */
621 if ( iob_len ( iobuf
) < sizeof ( *iphdr
) ) {
622 DBGC ( ipv6col ( &iphdr
->src
), "IPv6 packet too short at %zd "
623 "bytes (min %zd bytes)\n", iob_len ( iobuf
),
628 if ( ( iphdr
->ver_tc_label
& htonl ( IPV6_MASK_VER
) ) !=
629 htonl ( IPV6_VER
) ) {
630 DBGC ( ipv6col ( &iphdr
->src
), "IPv6 version %#08x not "
631 "supported\n", ntohl ( iphdr
->ver_tc_label
) );
636 /* Truncate packet to specified length */
637 len
= ntohs ( iphdr
->len
);
638 if ( len
> iob_len ( iobuf
) ) {
639 DBGC ( ipv6col ( &iphdr
->src
), "IPv6 length too long at %zd "
640 "bytes (packet is %zd bytes)\n", len
, iob_len ( iobuf
));
641 ipv6_stats
.in_truncated_pkts
++;
645 iob_unput ( iobuf
, ( iob_len ( iobuf
) - len
- sizeof ( *iphdr
) ) );
646 hdrlen
= sizeof ( *iphdr
);
648 /* Print IPv6 header for debugging */
649 DBGC2 ( ipv6col ( &iphdr
->src
), "IPv6 RX %s<-",
650 inet6_ntoa ( &iphdr
->dest
) );
651 DBGC2 ( ipv6col ( &iphdr
->src
), "%s len %zd next %d\n",
652 inet6_ntoa ( &iphdr
->src
), len
, iphdr
->next_header
);
654 /* Discard unicast packets not destined for us */
655 if ( ( ! ( flags
& LL_MULTICAST
) ) &&
656 ( ! ipv6_has_addr ( netdev
, &iphdr
->dest
) ) ) {
657 DBGC ( ipv6col ( &iphdr
->src
), "IPv6 discarding non-local "
658 "unicast packet for %s\n", inet6_ntoa ( &iphdr
->dest
) );
659 ipv6_stats
.in_addr_errors
++;
664 /* Process any extension headers */
665 next_header
= iphdr
->next_header
;
668 /* Extract extension header */
669 this_header
= next_header
;
670 ext
= ( iobuf
->data
+ hdrlen
);
671 extlen
= sizeof ( ext
->pad
);
672 if ( iob_len ( iobuf
) < ( hdrlen
+ extlen
) ) {
673 DBGC ( ipv6col ( &iphdr
->src
), "IPv6 too short for "
674 "extension header type %d at %zd bytes (min "
675 "%zd bytes)\n", this_header
,
676 ( iob_len ( iobuf
) - hdrlen
), extlen
);
681 /* Determine size of extension header (if applicable) */
682 if ( ( this_header
== IPV6_HOPBYHOP
) ||
683 ( this_header
== IPV6_DESTINATION
) ||
684 ( this_header
== IPV6_ROUTING
) ) {
685 /* Length field is present */
686 extlen
+= ext
->common
.len
;
687 } else if ( this_header
== IPV6_FRAGMENT
) {
688 /* Length field is reserved and ignored (RFC2460) */
690 /* Not an extension header; assume rest is payload */
693 if ( iob_len ( iobuf
) < ( hdrlen
+ extlen
) ) {
694 DBGC ( ipv6col ( &iphdr
->src
), "IPv6 too short for "
695 "extension header type %d at %zd bytes (min "
696 "%zd bytes)\n", this_header
,
697 ( iob_len ( iobuf
) - hdrlen
), extlen
);
702 next_header
= ext
->common
.next_header
;
703 DBGC2 ( ipv6col ( &iphdr
->src
), "IPv6 RX %s<-",
704 inet6_ntoa ( &iphdr
->dest
) );
705 DBGC2 ( ipv6col ( &iphdr
->src
), "%s ext type %d len %zd next "
706 "%d\n", inet6_ntoa ( &iphdr
->src
), this_header
,
707 extlen
, next_header
);
709 /* Process this extension header */
710 if ( ( this_header
== IPV6_HOPBYHOP
) ||
711 ( this_header
== IPV6_DESTINATION
) ) {
713 /* Check that all options can be ignored */
714 if ( ( rc
= ipv6_check_options ( iphdr
, &ext
->options
,
718 } else if ( this_header
== IPV6_FRAGMENT
) {
720 /* Reassemble fragments */
721 iobuf
= fragment_reassemble ( &ipv6_reassembler
, iobuf
,
729 /* Construct socket address, calculate pseudo-header checksum,
730 * and hand off to transport layer
732 memset ( &src
, 0, sizeof ( src
) );
733 src
.sin6
.sin6_family
= AF_INET6
;
734 memcpy ( &src
.sin6
.sin6_addr
, &iphdr
->src
,
735 sizeof ( src
.sin6
.sin6_addr
) );
736 src
.sin6
.sin6_scope_id
= netdev
->index
;
737 memset ( &dest
, 0, sizeof ( dest
) );
738 dest
.sin6
.sin6_family
= AF_INET6
;
739 memcpy ( &dest
.sin6
.sin6_addr
, &iphdr
->dest
,
740 sizeof ( dest
.sin6
.sin6_addr
) );
741 dest
.sin6
.sin6_scope_id
= netdev
->index
;
742 iob_pull ( iobuf
, hdrlen
);
743 pshdr_csum
= ipv6_pshdr_chksum ( iphdr
, iob_len ( iobuf
),
744 next_header
, TCPIP_EMPTY_CSUM
);
745 if ( ( rc
= tcpip_rx ( iobuf
, netdev
, next_header
, &src
.st
, &dest
.st
,
746 pshdr_csum
, &ipv6_stats
) ) != 0 ) {
747 DBGC ( ipv6col ( &src
.sin6
.sin6_addr
), "IPv6 received packet "
748 "rejected by stack: %s\n", strerror ( rc
) );
755 ipv6_stats
.in_hdr_errors
++;
764 * @v string IPv6 address string
765 * @ret in IPv6 address to fill in
766 * @ret rc Return status code
768 int inet6_aton ( const char *string
, struct in6_addr
*in
) {
769 uint16_t *word
= in
->s6_addr16
;
770 uint16_t *end
= ( word
+ ( sizeof ( in
->s6_addr16
) /
771 sizeof ( in
->s6_addr16
[0] ) ) );
772 uint16_t *pad
= NULL
;
773 const char *nptr
= string
;
782 /* Parse current word */
783 value
= strtoul ( nptr
, &endptr
, 16 );
784 if ( value
> 0xffff ) {
785 DBG ( "IPv6 invalid word value %#lx in \"%s\"\n",
789 *(word
++) = htons ( value
);
791 /* Parse separator */
794 if ( *endptr
!= ':' ) {
795 DBG ( "IPv6 invalid separator '%c' in \"%s\"\n",
799 if ( ( endptr
== nptr
) && ( nptr
!= string
) ) {
801 DBG ( "IPv6 invalid multiple \"::\" in "
802 "\"%s\"\n", string
);
807 nptr
= ( endptr
+ 1 );
809 /* Check for overrun */
811 DBG ( "IPv6 too many words in \"%s\"\n", string
);
816 /* Insert padding if specified */
818 move_len
= ( ( ( void * ) word
) - ( ( void * ) pad
) );
819 pad_len
= ( ( ( void * ) end
) - ( ( void * ) word
) );
820 memmove ( ( ( ( void * ) pad
) + pad_len
), pad
, move_len
);
821 memset ( pad
, 0, pad_len
);
822 } else if ( word
!= end
) {
823 DBG ( "IPv6 underlength address \"%s\"\n", string
);
831 * Convert IPv6 address to standard notation
834 * @ret string IPv6 address string in canonical format
836 * RFC5952 defines the canonical format for IPv6 textual representation.
838 char * inet6_ntoa ( const struct in6_addr
*in
) {
839 static char buf
[41]; /* ":xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" */
841 char *longest_start
= NULL
;
849 /* Format address, keeping track of longest run of zeros */
850 for ( i
= 0 ; i
< ( sizeof ( in
->s6_addr16
) /
851 sizeof ( in
->s6_addr16
[0] ) ) ; i
++ ) {
852 value
= ntohs ( in
->s6_addr16
[i
] );
856 if ( len
> longest_len
) {
857 longest_start
= start
;
863 out
+= sprintf ( out
, ":%x", value
);
866 /* Abbreviate longest run of zeros, if applicable */
867 if ( longest_start
) {
868 dest
= strcpy ( ( longest_start
+ 1 ),
869 ( longest_start
+ ( 2 * longest_len
) ) );
870 if ( dest
[0] == '\0' )
874 return ( ( longest_start
== buf
) ? buf
: ( buf
+ 1 ) );
878 * Transcribe IPv6 address
880 * @v net_addr IPv6 address
881 * @ret string IPv6 address in standard notation
884 static const char * ipv6_ntoa ( const void *net_addr
) {
885 return inet6_ntoa ( net_addr
);
889 * Transcribe IPv6 socket address
891 * @v sa Socket address
892 * @ret string Socket address in standard notation
894 static const char * ipv6_sock_ntoa ( struct sockaddr
*sa
) {
895 static char buf
[ 39 /* "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" */ +
896 1 /* "%" */ + NETDEV_NAME_LEN
+ 1 /* NUL */ ];
897 struct sockaddr_in6
*sin6
= ( ( struct sockaddr_in6
* ) sa
);
898 struct in6_addr
*in
= &sin6
->sin6_addr
;
899 struct net_device
*netdev
;
900 const char *netdev_name
;
902 /* Identify network device, if applicable */
903 if ( IN6_IS_ADDR_NONGLOBAL ( in
) ) {
904 netdev
= find_netdev_by_index ( sin6
->sin6_scope_id
);
905 netdev_name
= ( netdev ? netdev
->name
: "UNKNOWN" );
910 /* Format socket address */
911 snprintf ( buf
, sizeof ( buf
), "%s%s%s", inet6_ntoa ( in
),
912 ( netdev_name ?
"%" : "" ),
913 ( netdev_name ? netdev_name
: "" ) );
918 * Parse IPv6 socket address
920 * @v string Socket address string
921 * @v sa Socket address to fill in
922 * @ret rc Return status code
924 static int ipv6_sock_aton ( const char *string
, struct sockaddr
*sa
) {
925 struct sockaddr_in6
*sin6
= ( ( struct sockaddr_in6
* ) sa
);
927 struct net_device
*netdev
;
934 /* Create modifiable copy of string */
935 tmp
= strdup ( string
);
942 /* Strip surrounding "[...]", if present */
943 len
= strlen ( in_string
);
944 if ( ( in_string
[0] == '[' ) && ( in_string
[ len
- 1 ] == ']' ) ) {
945 in_string
[ len
- 1 ] = '\0';
949 /* Split at network device name, if present */
950 netdev_string
= strchr ( in_string
, '%' );
952 *(netdev_string
++) = '\0';
954 /* Parse IPv6 address portion */
955 if ( ( rc
= inet6_aton ( in_string
, &in
) ) != 0 )
958 /* Parse scope ID, if applicable */
959 if ( netdev_string
) {
961 /* Parse explicit network device name, if present */
962 netdev
= find_netdev ( netdev_string
);
965 goto err_find_netdev
;
967 sin6
->sin6_scope_id
= netdev
->index
;
969 } else if ( IN6_IS_ADDR_NONGLOBAL ( &in
) ) {
971 /* If no network device is explicitly specified for a
972 * link-local or multicast address, default to using
973 * "netX" (if existent).
975 netdev
= last_opened_netdev();
977 sin6
->sin6_scope_id
= netdev
->index
;
980 /* Copy IPv6 address portion to socket address */
981 memcpy ( &sin6
->sin6_addr
, &in
, sizeof ( sin6
->sin6_addr
) );
991 struct net_protocol ipv6_protocol __net_protocol
= {
993 .net_proto
= htons ( ETH_P_IPV6
),
994 .net_addr_len
= sizeof ( struct in6_addr
),
999 /** IPv6 TCPIP net protocol */
1000 struct tcpip_net_protocol ipv6_tcpip_protocol __tcpip_net_protocol
= {
1002 .sa_family
= AF_INET6
,
1003 .header_len
= sizeof ( struct ipv6_header
),
1005 .netdev
= ipv6_netdev
,
1008 /** IPv6 socket address converter */
1009 struct sockaddr_converter ipv6_sockaddr_converter __sockaddr_converter
= {
1011 .ntoa
= ipv6_sock_ntoa
,
1012 .aton
= ipv6_sock_aton
,
1016 * Parse IPv6 address setting value
1018 * @v type Setting type
1019 * @v value Formatted setting value
1020 * @v buf Buffer to contain raw value
1021 * @v len Length of buffer
1022 * @ret len Length of raw value, or negative error
1024 int parse_ipv6_setting ( const struct setting_type
*type __unused
,
1025 const char *value
, void *buf
, size_t len
) {
1026 struct in6_addr ipv6
;
1029 /* Parse IPv6 address */
1030 if ( ( rc
= inet6_aton ( value
, &ipv6
) ) != 0 )
1033 /* Copy to buffer */
1034 if ( len
> sizeof ( ipv6
) )
1035 len
= sizeof ( ipv6
);
1036 memcpy ( buf
, &ipv6
, len
);
1038 return ( sizeof ( ipv6
) );
1042 * Format IPv6 address setting value
1044 * @v type Setting type
1045 * @v raw Raw setting value
1046 * @v raw_len Length of raw setting value
1047 * @v buf Buffer to contain formatted value
1048 * @v len Length of buffer
1049 * @ret len Length of formatted value, or negative error
1051 int format_ipv6_setting ( const struct setting_type
*type __unused
,
1052 const void *raw
, size_t raw_len
, char *buf
,
1054 const struct in6_addr
*ipv6
= raw
;
1056 if ( raw_len
< sizeof ( *ipv6
) )
1058 return snprintf ( buf
, len
, "%s", inet6_ntoa ( ipv6
) );
1062 * Create IPv6 network device
1064 * @v netdev Network device
1065 * @ret rc Return status code
1067 static int ipv6_probe ( struct net_device
*netdev
) {
1068 struct ipv6_miniroute
*miniroute
;
1069 struct in6_addr address
;
1073 /* Construct link-local address from EUI-64 as per RFC 2464 */
1074 memset ( &address
, 0, sizeof ( address
) );
1075 prefix_len
= ipv6_link_local ( &address
, netdev
);
1076 if ( prefix_len
< 0 ) {
1078 DBGC ( netdev
, "IPv6 %s could not construct link-local "
1079 "address: %s\n", netdev
->name
, strerror ( rc
) );
1083 /* Create link-local address for this network device */
1084 miniroute
= ipv6_add_miniroute ( netdev
, &address
, prefix_len
,
1093 * Destroy IPv6 network device
1095 * @v netdev Network device
1097 static void ipv6_remove ( struct net_device
*netdev
) {
1098 struct ipv6_miniroute
*miniroute
;
1099 struct ipv6_miniroute
*tmp
;
1101 /* Delete all miniroutes for this network device */
1102 list_for_each_entry_safe ( miniroute
, tmp
, &ipv6_miniroutes
, list
) {
1103 if ( miniroute
->netdev
== netdev
) {
1104 netdev_put ( miniroute
->netdev
);
1105 list_del ( &miniroute
->list
);
1111 /** IPv6 network device driver */
1112 struct net_driver ipv6_driver __net_driver
= {
1114 .probe
= ipv6_probe
,
1115 .remove
= ipv6_remove
,
1118 /* Drag in objects via ipv6_protocol */
1119 REQUIRING_SYMBOL ( ipv6_protocol
);
1121 /* Drag in ICMPv6 */
1122 REQUIRE_OBJECT ( icmpv6
);
1125 REQUIRE_OBJECT ( ndp
);