2 * Copyright (C) 2011 Matthew Iselin <matthew@theiselins.net>.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
24 #include <gpxe/netdevice.h>
27 #include <gpxe/icmp6.h>
28 #include <gpxe/monojob.h>
29 #include <gpxe/process.h>
31 #include <usr/ifmgmt.h>
32 #include <usr/ip6mgmt.h>
33 #include <gpxe/dhcp6.h>
35 #define LINK_WAIT_MS 15000
37 /* Maximum length of the link-layer address we'll insert as an EUI-64. */
38 #define AUTOCONF_LL_MAX 6
40 int ip6_autoconf ( struct net_device
*netdev
) {
41 struct in6_addr ip6addr
, ip6zero
;
44 int use_dhcp
= 0, onlyinfo
= 0;
46 /* Check we can open the interface first */
47 if ( ( rc
= ifopen ( netdev
) ) != 0 )
50 /* Wait for link-up */
51 if ( ( rc
= iflinkwait ( netdev
, LINK_WAIT_MS
) ) != 0 )
54 /* Create the host ID part of the IPv6 address from the Link-Layer
55 * address on the netdevice. */
56 memset ( &ip6addr
, 0, sizeof (struct in6_addr
) );
57 memset ( &ip6zero
, 0, sizeof (struct in6_addr
) );
59 ll_size
= netdev
->ll_protocol
->ll_addr_len
;
61 memcpy ( ip6addr
.s6_addr
+ (8 - ll_size
), netdev
->ll_addr
, ll_size
);
63 ipv6_generate_eui64 ( ip6addr
.s6_addr
+ 8, netdev
->ll_addr
);
66 /* Fill in the link-local prefix. */
67 ip6addr
.s6_addr
[0] = 0xFE;
68 ip6addr
.s6_addr
[1] = 0x80;
70 /* TODO: send a few neighbour solicits on this address before we take
71 * it (once NDP is implemented). */
73 DBG ( "ipv6 autoconfig address is %s\n", inet6_ntoa(ip6addr
) );
75 /* Add as a route. It turns out Linux actually uses /64 for these, even
76 * though they are technically a /10. It does make routing easier, as
77 * /10 straddles a byte boundary. */
78 add_ipv6_address ( netdev
, ip6addr
, 64, ip6addr
, ip6zero
);
80 /* Solicit routers on the network. */
81 struct rsolicit_info router
;
82 if ( ( rc
= ndp_send_rsolicit ( netdev
, &monojob
, &router
) ) == 0 ) {
83 rc
= monojob_wait ( "finding routers and attempting stateless autoconfiguration" );
87 DBG ( "ipv6: router solicitation failed\n" );
91 if ( router
.flags
& RSOLICIT_CODE_MANAGED
) {
92 DBG ( "ipv6: should use dhcp6 server\n" );
94 } else if ( router
.flags
& RSOLICIT_CODE_OTHERCONF
) {
95 DBG ( "ipv6: use dhcp6 server for DNS settings\n" );
99 DBG ( "ipv6: autoconfiguration complete\n" );
103 /* Attempt DHCPv6 now, for addresses (if we don't already have one) and
104 * DNS configuration. */
106 start_dhcp6 ( &monojob
, netdev
, onlyinfo
, &router
);
107 rc
= monojob_wait ( "dhcp6" );