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 );
27 #include <ipxe/iobuf.h>
28 #include <ipxe/tcpip.h>
29 #include <ipxe/ipv6.h>
30 #include <ipxe/icmpv6.h>
31 #include <ipxe/neighbour.h>
32 #include <ipxe/dhcpv6.h>
37 * IPv6 neighbour discovery protocol
42 ipv6conf_rx_router_advertisement ( struct net_device *netdev,
43 struct ndp_router_advertisement_header *radv,
47 * Transmit NDP packet with link-layer address option
49 * @v netdev Network device
50 * @v sin6_src Source socket address
51 * @v sin6_dest Destination socket address
53 * @v len Size of NDP header
54 * @v option_type NDP option type
55 * @ret rc Return status code
57 static int ndp_tx_ll_addr ( struct net_device *netdev,
58 struct sockaddr_in6 *sin6_src,
59 struct sockaddr_in6 *sin6_dest,
60 const void *data, size_t len,
61 unsigned int option_type ) {
62 struct sockaddr_tcpip *st_src =
63 ( ( struct sockaddr_tcpip * ) sin6_src );
64 struct sockaddr_tcpip *st_dest =
65 ( ( struct sockaddr_tcpip * ) sin6_dest );
66 struct ll_protocol *ll_protocol = netdev->ll_protocol;
67 struct io_buffer *iobuf;
68 struct ndp_ll_addr_option *ll_addr_opt;
69 union ndp_header *ndp;
73 /* Allocate and populate buffer */
74 option_len = ( ( sizeof ( *ll_addr_opt ) +
75 ll_protocol->ll_addr_len + NDP_OPTION_BLKSZ - 1 ) &
76 ~( NDP_OPTION_BLKSZ - 1 ) );
77 iobuf = alloc_iob ( MAX_LL_NET_HEADER_LEN + len + option_len );
80 iob_reserve ( iobuf, MAX_LL_NET_HEADER_LEN );
81 memcpy ( iob_put ( iobuf, len ), data, len );
82 ll_addr_opt = iob_put ( iobuf, option_len );
83 ll_addr_opt->header.type = option_type;
84 ll_addr_opt->header.blocks = ( option_len / NDP_OPTION_BLKSZ );
85 memcpy ( ll_addr_opt->ll_addr, netdev->ll_addr,
86 ll_protocol->ll_addr_len );
88 ndp->icmp.chksum = tcpip_chksum ( ndp, ( len + option_len ) );
91 if ( ( rc = tcpip_tx ( iobuf, &icmpv6_protocol, st_src, st_dest,
92 netdev, &ndp->icmp.chksum ) ) != 0 ) {
93 DBGC ( netdev, "NDP %s could not transmit packet: %s\n",
94 netdev->name, strerror ( rc ) );
102 * Transmit NDP neighbour discovery request
104 * @v netdev Network device
105 * @v net_protocol Network-layer protocol
106 * @v net_dest Destination network-layer address
107 * @v net_source Source network-layer address
108 * @ret rc Return status code
110 static int ndp_tx_request ( struct net_device *netdev,
111 struct net_protocol *net_protocol __unused,
112 const void *net_dest, const void *net_source ) {
113 struct sockaddr_in6 sin6_src;
114 struct sockaddr_in6 sin6_dest;
115 struct ndp_neighbour_header neigh;
118 /* Construct source address */
119 memset ( &sin6_src, 0, sizeof ( sin6_src ) );
120 sin6_src.sin6_family = AF_INET6;
121 memcpy ( &sin6_src.sin6_addr, net_source,
122 sizeof ( sin6_src.sin6_addr ) );
124 /* Construct multicast destination address */
125 memset ( &sin6_dest, 0, sizeof ( sin6_dest ) );
126 sin6_dest.sin6_family = AF_INET6;
127 sin6_dest.sin6_scope_id = netdev->index;
128 ipv6_solicited_node ( &sin6_dest.sin6_addr, net_dest );
130 /* Construct neighbour header */
131 memset ( &neigh, 0, sizeof ( neigh ) );
132 neigh.icmp.type = ICMPV6_NEIGHBOUR_SOLICITATION;
133 memcpy ( &neigh.target, net_dest, sizeof ( neigh.target ) );
135 /* Transmit neighbour discovery packet */
136 if ( ( rc = ndp_tx_ll_addr ( netdev, &sin6_src, &sin6_dest, &neigh,
138 NDP_OPT_LL_SOURCE ) ) != 0 )
144 /** NDP neighbour discovery protocol */
145 struct neighbour_discovery ndp_discovery = {
147 .tx_request = ndp_tx_request,
151 * Transmit NDP router solicitation
153 * @v netdev Network device
154 * @ret rc Return status code
156 static int ndp_tx_router_solicitation ( struct net_device *netdev ) {
157 struct ndp_router_solicitation_header rsol;
158 struct sockaddr_in6 sin6_dest;
161 /* Construct multicast destination address */
162 memset ( &sin6_dest, 0, sizeof ( sin6_dest ) );
163 sin6_dest.sin6_family = AF_INET6;
164 sin6_dest.sin6_scope_id = netdev->index;
165 ipv6_all_routers ( &sin6_dest.sin6_addr );
167 /* Construct router solicitation */
168 memset ( &rsol, 0, sizeof ( rsol ) );
169 rsol.icmp.type = ICMPV6_ROUTER_SOLICITATION;
171 /* Transmit packet */
172 if ( ( rc = ndp_tx_ll_addr ( netdev, NULL, &sin6_dest, &rsol,
173 sizeof ( rsol ), NDP_OPT_LL_SOURCE ) ) !=0)
180 * Process NDP neighbour solicitation source link-layer address option
182 * @v netdev Network device
183 * @v sin6_src Source socket address
185 * @v option NDP option
186 * @v len NDP option length
187 * @ret rc Return status code
190 ndp_rx_neighbour_solicitation_ll_source ( struct net_device *netdev,
191 struct sockaddr_in6 *sin6_src,
192 union ndp_header *ndp,
193 union ndp_option *option,
195 struct ndp_neighbour_header *neigh = &ndp->neigh;
196 struct ndp_ll_addr_option *ll_addr_opt = &option->ll_addr;
197 struct ll_protocol *ll_protocol = netdev->ll_protocol;
200 /* Silently ignore neighbour solicitations for addresses we do
203 if ( ! ipv6_has_addr ( netdev, &neigh->target ) )
207 if ( offsetof ( typeof ( *ll_addr_opt ),
208 ll_addr[ll_protocol->ll_addr_len] ) > len ) {
209 DBGC ( netdev, "NDP %s neighbour solicitation link-layer "
210 "address option too short at %zd bytes\n",
215 /* Create or update neighbour cache entry */
216 if ( ( rc = neighbour_define ( netdev, &ipv6_protocol,
217 &sin6_src->sin6_addr,
218 ll_addr_opt->ll_addr ) ) != 0 ) {
219 DBGC ( netdev, "NDP %s could not define %s => %s: %s\n",
220 netdev->name, inet6_ntoa ( &sin6_src->sin6_addr ),
221 ll_protocol->ntoa ( ll_addr_opt->ll_addr ),
226 /* Convert neighbour header to advertisement */
227 memset ( neigh, 0, offsetof ( typeof ( *neigh ), target ) );
228 neigh->icmp.type = ICMPV6_NEIGHBOUR_ADVERTISEMENT;
229 neigh->flags = ( NDP_NEIGHBOUR_SOLICITED | NDP_NEIGHBOUR_OVERRIDE );
231 /* Send neighbour advertisement */
232 if ( ( rc = ndp_tx_ll_addr ( netdev, NULL, sin6_src, neigh,
234 NDP_OPT_LL_TARGET ) ) != 0 )
241 * Process NDP neighbour advertisement target link-layer address option
243 * @v netdev Network device
244 * @v sin6_src Source socket address
246 * @v option NDP option
247 * @v len NDP option length
248 * @ret rc Return status code
251 ndp_rx_neighbour_advertisement_ll_target ( struct net_device *netdev,
252 struct sockaddr_in6 *sin6_src
254 union ndp_header *ndp,
255 union ndp_option *option,
257 struct ndp_neighbour_header *neigh = &ndp->neigh;
258 struct ndp_ll_addr_option *ll_addr_opt = &option->ll_addr;
259 struct ll_protocol *ll_protocol = netdev->ll_protocol;
263 if ( offsetof ( typeof ( *ll_addr_opt ),
264 ll_addr[ll_protocol->ll_addr_len] ) > len ) {
265 DBGC ( netdev, "NDP %s neighbour advertisement link-layer "
266 "address option too short at %zd bytes\n",
271 /* Update neighbour cache entry, if any */
272 if ( ( rc = neighbour_update ( netdev, &ipv6_protocol, &neigh->target,
273 ll_addr_opt->ll_addr ) ) != 0 ) {
274 DBGC ( netdev, "NDP %s could not update %s => %s: %s\n",
275 netdev->name, inet6_ntoa ( &neigh->target ),
276 ll_protocol->ntoa ( ll_addr_opt->ll_addr ),
285 * Process NDP router advertisement source link-layer address option
287 * @v netdev Network device
288 * @v sin6_src Source socket address
290 * @v option NDP option
291 * @v len NDP option length
292 * @ret rc Return status code
295 ndp_rx_router_advertisement_ll_source ( struct net_device *netdev,
296 struct sockaddr_in6 *sin6_src,
297 union ndp_header *ndp __unused,
298 union ndp_option *option, size_t len ) {
299 struct ndp_ll_addr_option *ll_addr_opt = &option->ll_addr;
300 struct ll_protocol *ll_protocol = netdev->ll_protocol;
304 if ( offsetof ( typeof ( *ll_addr_opt ),
305 ll_addr[ll_protocol->ll_addr_len] ) > len ) {
306 DBGC ( netdev, "NDP %s router advertisement link-layer address "
307 "option too short at %zd bytes\n", netdev->name, len );
311 /* Define neighbour cache entry */
312 if ( ( rc = neighbour_define ( netdev, &ipv6_protocol,
313 &sin6_src->sin6_addr,
314 ll_addr_opt->ll_addr ) ) != 0 ) {
315 DBGC ( netdev, "NDP %s could not define %s => %s: %s\n",
316 netdev->name, inet6_ntoa ( &sin6_src->sin6_addr ),
317 ll_protocol->ntoa ( ll_addr_opt->ll_addr ),
326 * Process NDP router advertisement prefix information option
328 * @v netdev Network device
329 * @v sin6_src Source socket address
331 * @v option NDP option
332 * @v len NDP option length
333 * @ret rc Return status code
336 ndp_rx_router_advertisement_prefix ( struct net_device *netdev,
337 struct sockaddr_in6 *sin6_src,
338 union ndp_header *ndp,
339 union ndp_option *option, size_t len ) {
340 struct ndp_router_advertisement_header *radv = &ndp->radv;
341 struct ndp_prefix_information_option *prefix_opt = &option->prefix;
342 struct in6_addr *router = &sin6_src->sin6_addr;
343 struct in6_addr address;
348 if ( sizeof ( *prefix_opt ) > len ) {
349 DBGC ( netdev, "NDP %s router advertisement prefix option too "
350 "short at %zd bytes\n", netdev->name, len );
353 DBGC ( netdev, "NDP %s found %sdefault router %s ",
354 netdev->name, ( radv->lifetime ? "" : "non-" ),
355 inet6_ntoa ( &sin6_src->sin6_addr ) );
356 DBGC ( netdev, "for %s-link %sautonomous prefix %s/%d\n",
357 ( ( prefix_opt->flags & NDP_PREFIX_ON_LINK ) ? "on" : "off" ),
358 ( ( prefix_opt->flags & NDP_PREFIX_AUTONOMOUS ) ? "" : "non-" ),
359 inet6_ntoa ( &prefix_opt->prefix ),
360 prefix_opt->prefix_len );
362 /* Ignore off-link prefixes */
363 if ( ! ( prefix_opt->flags & NDP_PREFIX_ON_LINK ) )
367 if ( ( rc = ipv6_set_prefix ( netdev, &prefix_opt->prefix,
368 prefix_opt->prefix_len,
370 router : NULL ) ) ) != 0 ) {
371 DBGC ( netdev, "NDP %s could not define prefix %s/%d: %s\n",
372 netdev->name, inet6_ntoa ( &prefix_opt->prefix ),
373 prefix_opt->prefix_len, strerror ( rc ) );
377 /* Perform stateless address autoconfiguration, if applicable */
378 if ( prefix_opt->flags & NDP_PREFIX_AUTONOMOUS ) {
379 memcpy ( &address, &prefix_opt->prefix, sizeof ( address ) );
380 prefix_len = ipv6_eui64 ( &address, netdev );
381 if ( prefix_len < 0 ) {
383 DBGC ( netdev, "NDP %s could not construct SLAAC "
384 "address: %s\n", netdev->name, strerror ( rc ) );
387 if ( prefix_len != prefix_opt->prefix_len ) {
388 DBGC ( netdev, "NDP %s incorrect SLAAC prefix length "
389 "%d (expected %d)\n", netdev->name,
390 prefix_opt->prefix_len, prefix_len );
393 if ( ( rc = ipv6_set_address ( netdev, &address ) ) != 0 ) {
394 DBGC ( netdev, "NDP %s could not set address %s: %s\n",
395 netdev->name, inet6_ntoa ( &address ),
404 /** An NDP option handler */
405 struct ndp_option_handler {
411 * Handle received option
413 * @v netdev Network device
414 * @v sin6_src Source socket address
416 * @v option NDP option
417 * @ret rc Return status code
419 int ( * rx ) ( struct net_device *netdev, struct sockaddr_in6 *sin6_src,
420 union ndp_header *ndp, union ndp_option *option,
424 /** NDP option handlers */
425 static struct ndp_option_handler ndp_option_handlers[] = {
427 .icmp_type = ICMPV6_NEIGHBOUR_SOLICITATION,
428 .option_type = NDP_OPT_LL_SOURCE,
429 .rx = ndp_rx_neighbour_solicitation_ll_source,
432 .icmp_type = ICMPV6_NEIGHBOUR_ADVERTISEMENT,
433 .option_type = NDP_OPT_LL_TARGET,
434 .rx = ndp_rx_neighbour_advertisement_ll_target,
437 .icmp_type = ICMPV6_ROUTER_ADVERTISEMENT,
438 .option_type = NDP_OPT_LL_SOURCE,
439 .rx = ndp_rx_router_advertisement_ll_source,
442 .icmp_type = ICMPV6_ROUTER_ADVERTISEMENT,
443 .option_type = NDP_OPT_PREFIX,
444 .rx = ndp_rx_router_advertisement_prefix,
449 * Process received NDP option
451 * @v netdev Network device
452 * @v sin6_src Source socket address
454 * @v option NDP option
455 * @v len Option length
456 * @ret rc Return status code
458 static int ndp_rx_option ( struct net_device *netdev,
459 struct sockaddr_in6 *sin6_src, union ndp_header *ndp,
460 union ndp_option *option, size_t len ) {
461 struct ndp_option_handler *handler;
464 /* Locate a suitable option handler, if any */
465 for ( i = 0 ; i < ( sizeof ( ndp_option_handlers ) /
466 sizeof ( ndp_option_handlers[0] ) ) ; i++ ) {
467 handler = &ndp_option_handlers[i];
468 if ( ( handler->icmp_type == ndp->icmp.type ) &&
469 ( handler->option_type == option->header.type ) ) {
470 return handler->rx ( netdev, sin6_src, ndp,
475 /* Silently ignore unknown options as per RFC 4861 */
480 * Process received NDP packet options
482 * @v netdev Network device
483 * @v sin6_src Source socket address
485 * @v offset Offset to NDP options
486 * @v len Length of NDP packet
487 * @ret rc Return status code
489 static int ndp_rx_options ( struct net_device *netdev,
490 struct sockaddr_in6 *sin6_src,
491 union ndp_header *ndp, size_t offset, size_t len ) {
492 union ndp_option *option;
498 if ( len < offset ) {
499 DBGC ( netdev, "NDP %s packet too short at %zd bytes (min %zd "
500 "bytes)\n", netdev->name, len, offset );
504 /* Search for option */
505 option = ( ( ( void * ) ndp ) + offset );
506 remaining = ( len - offset );
507 while ( remaining ) {
510 if ( ( remaining < sizeof ( option->header ) ) ||
511 ( option->header.blocks == 0 ) ||
512 ( remaining < ( option->header.blocks *
513 NDP_OPTION_BLKSZ ) ) ) {
514 DBGC ( netdev, "NDP %s bad option length:\n",
516 DBGC_HDA ( netdev, 0, option, remaining );
519 option_len = ( option->header.blocks * NDP_OPTION_BLKSZ );
522 if ( ( rc = ndp_rx_option ( netdev, sin6_src, ndp, option,
523 option_len ) ) != 0 )
526 /* Move to next option */
527 option = ( ( ( void * ) option ) + option_len );
528 remaining -= option_len;
535 * Process received NDP neighbour solicitation or advertisement
537 * @v iobuf I/O buffer
538 * @v netdev Network device
539 * @v sin6_src Source socket address
540 * @v sin6_dest Destination socket address
541 * @ret rc Return status code
543 static int ndp_rx_neighbour ( struct io_buffer *iobuf,
544 struct net_device *netdev,
545 struct sockaddr_in6 *sin6_src,
546 struct sockaddr_in6 *sin6_dest __unused ) {
547 union ndp_header *ndp = iobuf->data;
548 struct ndp_neighbour_header *neigh = &ndp->neigh;
549 size_t len = iob_len ( iobuf );
552 /* Process options */
553 if ( ( rc = ndp_rx_options ( netdev, sin6_src, ndp,
554 offsetof ( typeof ( *neigh ), option ),
564 * Process received NDP router advertisement
566 * @v iobuf I/O buffer
567 * @v netdev Network device
568 * @v sin6_src Source socket address
569 * @v sin6_dest Destination socket address
570 * @ret rc Return status code
573 ndp_rx_router_advertisement ( struct io_buffer *iobuf,
574 struct net_device *netdev,
575 struct sockaddr_in6 *sin6_src,
576 struct sockaddr_in6 *sin6_dest __unused ) {
577 union ndp_header *ndp = iobuf->data;
578 struct ndp_router_advertisement_header *radv = &ndp->radv;
579 size_t len = iob_len ( iobuf );
582 /* Process options */
583 if ( ( rc = ndp_rx_options ( netdev, sin6_src, ndp,
584 offsetof ( typeof ( *radv ), option ),
588 /* Pass to IPv6 autoconfiguration */
589 if ( ( rc = ipv6conf_rx_router_advertisement ( netdev, radv,
599 /** NDP ICMPv6 handlers */
600 struct icmpv6_handler ndp_handlers[] __icmpv6_handler = {
602 .type = ICMPV6_NEIGHBOUR_SOLICITATION,
603 .rx = ndp_rx_neighbour,
606 .type = ICMPV6_NEIGHBOUR_ADVERTISEMENT,
607 .rx = ndp_rx_neighbour,
610 .type = ICMPV6_ROUTER_ADVERTISEMENT,
611 .rx = ndp_rx_router_advertisement,
615 /****************************************************************************
621 /** An NDP settings block */
622 struct ndp_settings {
623 /** Reference counter */
624 struct refcnt refcnt;
625 /** Settings interface */
626 struct settings settings;
627 /** Length of NDP options */
630 union ndp_option option[0];
633 /** NDP settings scope */
634 static const struct settings_scope ndp_settings_scope;
639 * @v type NDP option type
640 * @v offset Starting offset of data
643 #define NDP_TAG( type, offset ) ( ( (offset) << 8 ) | (type) )
646 * Extract NDP tag type
649 * @ret type NDP option type
651 #define NDP_TAG_TYPE( tag ) ( (tag) & 0xff )
654 * Extract NDP tag offset
657 * @ret offset Starting offset of data
659 #define NDP_TAG_OFFSET( tag ) ( (tag) >> 8 )
662 * Check applicability of NDP setting
664 * @v settings Settings block
665 * @v setting Setting to fetch
666 * @ret applies Setting applies within this settings block
668 static int ndp_applies ( struct settings *settings __unused,
669 const struct setting *setting ) {
671 return ( setting->scope == &ndp_settings_scope );
675 * Fetch value of NDP setting
677 * @v settings Settings block
678 * @v setting Setting to fetch
679 * @v data Buffer to fill with setting data
680 * @v len Length of buffer
681 * @ret len Length of setting data, or negative error
683 static int ndp_fetch ( struct settings *settings,
684 struct setting *setting,
685 void *data, size_t len ) {
686 struct ndp_settings *ndpset =
687 container_of ( settings, struct ndp_settings, settings );
688 struct net_device *netdev =
689 container_of ( settings->parent, struct net_device,
691 union ndp_option *option;
692 unsigned int type = NDP_TAG_TYPE ( setting->tag );
693 unsigned int offset = NDP_TAG_OFFSET ( setting->tag );
698 /* Scan through NDP options for requested type. We can assume
699 * that the options are well-formed, otherwise they would have
700 * been rejected prior to being stored.
702 option = ndpset->option;
703 remaining = ndpset->len;
704 while ( remaining ) {
706 /* Calculate option length */
707 option_len = ( option->header.blocks * NDP_OPTION_BLKSZ );
709 /* If this is the requested option, return it */
710 if ( option->header.type == type ) {
713 if ( offset > option_len ) {
714 DBGC ( netdev, "NDP %s option %d too short\n",
715 netdev->name, type );
718 payload_len = ( option_len - offset );
720 /* Copy data to output buffer */
721 if ( len > payload_len )
723 memcpy ( data, ( ( ( void * ) option ) + offset ), len);
727 /* Move to next option */
728 option = ( ( ( void * ) option ) + option_len );
729 remaining -= option_len;
735 /** NDP settings operations */
736 static struct settings_operations ndp_settings_operations = {
737 .applies = ndp_applies,
742 * Register NDP settings
744 * @v netdev Network device
745 * @v option NDP options
746 * @v len Length of options
747 * @ret rc Return status code
749 static int ndp_register_settings ( struct net_device *netdev,
750 union ndp_option *option, size_t len ) {
751 struct settings *parent = netdev_settings ( netdev );
752 struct ndp_settings *ndpset;
755 /* Allocate and initialise structure */
756 ndpset = zalloc ( sizeof ( *ndpset ) + len );
761 ref_init ( &ndpset->refcnt, NULL );
762 settings_init ( &ndpset->settings, &ndp_settings_operations,
763 &ndpset->refcnt, &ndp_settings_scope );
765 memcpy ( ndpset->option, option, len );
767 /* Register settings */
768 if ( ( rc = register_settings ( &ndpset->settings, parent,
769 NDP_SETTINGS_NAME ) ) != 0 )
773 ref_put ( &ndpset->refcnt );
778 /** DNS server setting */
779 const struct setting ndp_dns6_setting __setting ( SETTING_IP_EXTRA, dns6 ) = {
781 .description = "DNS server",
782 .tag = NDP_TAG ( NDP_OPT_RDNSS,
783 offsetof ( struct ndp_rdnss_option, addresses ) ),
784 .type = &setting_type_ipv6,
785 .scope = &ndp_settings_scope,
788 /** DNS search list setting */
789 const struct setting ndp_dnssl_setting __setting ( SETTING_IP_EXTRA, dnssl ) = {
791 .description = "DNS search list",
792 .tag = NDP_TAG ( NDP_OPT_DNSSL,
793 offsetof ( struct ndp_dnssl_option, names ) ),
794 .type = &setting_type_dnssl,
795 .scope = &ndp_settings_scope,
798 /****************************************************************************
800 * IPv6 autoconfiguration
804 /** An IPv6 configurator */
806 /** Reference count */
807 struct refcnt refcnt;
808 /** List of configurators */
809 struct list_head list;
811 /** Job control interface */
812 struct interface job;
813 /** DHCPv6 interface */
814 struct interface dhcp;
816 /** Network device being configured */
817 struct net_device *netdev;
819 /** Retransmission timer */
820 struct retry_timer timer;
823 /** List of IPv6 configurators */
824 static LIST_HEAD ( ipv6confs );
827 * Free IPv6 configurator
829 * @v refcnt Reference count
831 static void ipv6conf_free ( struct refcnt *refcnt ) {
832 struct ipv6conf *ipv6conf =
833 container_of ( refcnt, struct ipv6conf, refcnt );
835 netdev_put ( ipv6conf->netdev );
840 * Identify IPv6 configurator by network device
842 * @v netdev Network device
843 * @ret ipv6 IPv6 configurator, or NULL
845 static struct ipv6conf * ipv6conf_demux ( struct net_device *netdev ) {
846 struct ipv6conf *ipv6conf;
848 list_for_each_entry ( ipv6conf, &ipv6confs, list ) {
849 if ( ipv6conf->netdev == netdev )
856 * Finish IPv6 autoconfiguration
858 * @v ipv6 IPv6 configurator
859 * @v rc Reason for finishing
861 static void ipv6conf_done ( struct ipv6conf *ipv6conf, int rc ) {
863 /* Shut down interfaces */
864 intf_shutdown ( &ipv6conf->job, rc );
865 intf_shutdown ( &ipv6conf->dhcp, rc );
868 stop_timer ( &ipv6conf->timer );
870 /* Remove from list and drop list's reference */
871 list_del ( &ipv6conf->list );
872 ref_put ( &ipv6conf->refcnt );
876 * Handle IPv6 configurator timer expiry
878 * @v timer Retry timer
879 * @v fail Failure indicator
881 static void ipv6conf_expired ( struct retry_timer *timer, int fail ) {
882 struct ipv6conf *ipv6conf =
883 container_of ( timer, struct ipv6conf, timer );
885 /* If we have failed, terminate autoconfiguration */
887 ipv6conf_done ( ipv6conf, -ETIMEDOUT );
891 /* Otherwise, transmit router solicitation and restart timer */
892 start_timer ( &ipv6conf->timer );
893 ndp_tx_router_solicitation ( ipv6conf->netdev );
897 * Handle router advertisement during IPv6 autoconfiguration
899 * @v netdev Network device
900 * @v radv Router advertisement
901 * @v len Length of router advertisement
902 * @ret rc Return status code
904 * This function assumes that the router advertisement is well-formed,
905 * since it must have already passed through option processing.
908 ipv6conf_rx_router_advertisement ( struct net_device *netdev,
909 struct ndp_router_advertisement_header *radv,
911 struct ipv6conf *ipv6conf;
916 /* Identify IPv6 configurator, if any */
917 ipv6conf = ipv6conf_demux ( netdev );
919 /* Not an error; router advertisements are processed
920 * as a background activity even when no explicit
921 * autoconfiguration is taking place.
926 /* If this is not the first solicited router advertisement, ignore it */
927 if ( ! timer_running ( &ipv6conf->timer ) )
930 /* Stop router solicitation timer */
931 stop_timer ( &ipv6conf->timer );
933 /* Register NDP settings */
934 option_len = ( len - offsetof ( typeof ( *radv ), option ) );
935 if ( ( rc = ndp_register_settings ( netdev, radv->option,
936 option_len ) ) != 0 )
939 /* Start DHCPv6 if required */
940 if ( radv->flags & ( NDP_ROUTER_MANAGED | NDP_ROUTER_OTHER ) ) {
941 stateful = ( radv->flags & NDP_ROUTER_MANAGED );
942 if ( ( rc = start_dhcpv6 ( &ipv6conf->dhcp, netdev,
943 stateful ) ) != 0 ) {
944 DBGC ( netdev, "NDP %s could not start state%s DHCPv6: "
945 "%s\n", netdev->name,
946 ( stateful ? "ful" : "less" ), strerror ( rc ) );
947 ipv6conf_done ( ipv6conf, rc );
953 /* Otherwise, terminate autoconfiguration */
954 ipv6conf_done ( ipv6conf, 0 );
959 /** IPv6 configurator job interface operations */
960 static struct interface_operation ipv6conf_job_op[] = {
961 INTF_OP ( intf_close, struct ipv6conf *, ipv6conf_done ),
964 /** IPv6 configurator job interface descriptor */
965 static struct interface_descriptor ipv6conf_job_desc =
966 INTF_DESC ( struct ipv6conf, job, ipv6conf_job_op );
968 /** IPv6 configurator DHCPv6 interface operations */
969 static struct interface_operation ipv6conf_dhcp_op[] = {
970 INTF_OP ( intf_close, struct ipv6conf *, ipv6conf_done ),
973 /** IPv6 configurator DHCPv6 interface descriptor */
974 static struct interface_descriptor ipv6conf_dhcp_desc =
975 INTF_DESC ( struct ipv6conf, dhcp, ipv6conf_dhcp_op );
978 * Start IPv6 autoconfiguration
980 * @v job Job control interface
981 * @v netdev Network device
982 * @ret rc Return status code
984 int start_ipv6conf ( struct interface *job, struct net_device *netdev ) {
985 struct ipv6conf *ipv6conf;
987 /* Allocate and initialise structure */
988 ipv6conf = zalloc ( sizeof ( *ipv6conf ) );
991 ref_init ( &ipv6conf->refcnt, ipv6conf_free );
992 intf_init ( &ipv6conf->job, &ipv6conf_job_desc, &ipv6conf->refcnt );
993 intf_init ( &ipv6conf->dhcp, &ipv6conf_dhcp_desc, &ipv6conf->refcnt );
994 timer_init ( &ipv6conf->timer, ipv6conf_expired, &ipv6conf->refcnt );
995 ipv6conf->netdev = netdev_get ( netdev );
997 /* Start timer to initiate router solicitation */
998 start_timer_nodelay ( &ipv6conf->timer );
1000 /* Attach parent interface, transfer reference to list, and return */
1001 intf_plug_plug ( &ipv6conf->job, job );
1002 list_add ( &ipv6conf->list, &ipv6confs );
1006 /** IPv6 network device configurator */
1007 struct net_device_configurator ipv6_configurator __net_device_configurator = {
1009 .start = start_ipv6conf,