blob: 43c2c38f83566c52630f6abc9c700c9f1698f3df [file] [log] [blame]
Simon Kelley61744352013-01-31 14:34:40 +00001/* dnsmasq is Copyright (c) 2000-2013 Simon Kelley
Simon Kelley353ae4d2012-03-19 20:07:51 +00002
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
15*/
16
17#include "dnsmasq.h"
18
19#ifdef HAVE_DHCP6
20
21#include <netinet/icmp6.h>
22
Simon Kelley353ae4d2012-03-19 20:07:51 +000023static int ping_id = 0;
24
Simon Kelleya9ab7322012-04-28 11:29:37 +010025void slaac_add_addrs(struct dhcp_lease *lease, time_t now, int force)
Simon Kelley353ae4d2012-03-19 20:07:51 +000026{
27 struct slaac_address *slaac, *old, **up;
28 struct dhcp_context *context;
Simon Kelley7f61b3a2012-04-28 17:42:31 +010029 int dns_dirty = 0;
Simon Kelley353ae4d2012-03-19 20:07:51 +000030
31 if (!(lease->flags & LEASE_HAVE_HWADDR) ||
Simon Kelleya9ab7322012-04-28 11:29:37 +010032 (lease->flags & (LEASE_TA | LEASE_NA)) ||
Simon Kelley353ae4d2012-03-19 20:07:51 +000033 lease->last_interface == 0 ||
34 !lease->hostname)
35 return ;
36
37 old = lease->slaac_address;
38 lease->slaac_address = NULL;
39
Simon Kelley1f776932012-12-16 19:46:08 +000040 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelleyef1a94a2013-07-26 13:59:03 +010041 if ((context->flags & CONTEXT_RA_NAME) &&
42 !(context->flags & CONTEXT_OLD) &&
43 lease->last_interface == context->if_index)
Simon Kelley353ae4d2012-03-19 20:07:51 +000044 {
45 struct in6_addr addr = context->start6;
46 if (lease->hwaddr_len == 6 &&
47 (lease->hwaddr_type == ARPHRD_ETHER || lease->hwaddr_type == ARPHRD_IEEE802))
48 {
49 /* convert MAC address to EUI-64 */
50 memcpy(&addr.s6_addr[8], lease->hwaddr, 3);
51 memcpy(&addr.s6_addr[13], &lease->hwaddr[3], 3);
52 addr.s6_addr[11] = 0xff;
53 addr.s6_addr[12] = 0xfe;
54 }
55#if defined(ARPHRD_EUI64)
56 else if (lease->hwaddr_len == 8 &&
57 lease->hwaddr_type == ARPHRD_EUI64)
58 memcpy(&addr.s6_addr[8], lease->hwaddr, 8);
59#endif
60#if defined(ARPHRD_IEEE1394) && defined(ARPHRD_EUI64)
61 else if (lease->clid_len == 9 &&
62 lease->clid[0] == ARPHRD_EUI64 &&
63 lease->hwaddr_type == ARPHRD_IEEE1394)
64 /* firewire has EUI-64 identifier as clid */
65 memcpy(&addr.s6_addr[8], &lease->clid[1], 8);
66#endif
67 else
68 continue;
69
70 addr.s6_addr[8] ^= 0x02;
71
72 /* check if we already have this one */
73 for (up = &old, slaac = old; slaac; slaac = slaac->next)
74 {
75 if (IN6_ARE_ADDR_EQUAL(&addr, &slaac->addr))
76 {
77 *up = slaac->next;
Simon Kelleya9ab7322012-04-28 11:29:37 +010078 /* recheck when DHCPv4 goes through init-reboot */
79 if (force)
80 {
81 slaac->ping_time = now;
82 slaac->backoff = 1;
Simon Kelley7f61b3a2012-04-28 17:42:31 +010083 dns_dirty = 1;
Simon Kelleya9ab7322012-04-28 11:29:37 +010084 }
Simon Kelley353ae4d2012-03-19 20:07:51 +000085 break;
86 }
87 up = &slaac->next;
88 }
89
90 /* No, make new one */
91 if (!slaac && (slaac = whine_malloc(sizeof(struct slaac_address))))
92 {
93 slaac->ping_time = now;
94 slaac->backoff = 1;
95 slaac->addr = addr;
96 slaac->local = context->local6;
97 /* Do RA's to prod it */
98 ra_start_unsolicted(now, context);
99 }
100
101 if (slaac)
102 {
103 slaac->next = lease->slaac_address;
104 lease->slaac_address = slaac;
105 }
106 }
107
Simon Kelley7f61b3a2012-04-28 17:42:31 +0100108 if (old || dns_dirty)
109 lease_update_dns(1);
110
Simon Kelley353ae4d2012-03-19 20:07:51 +0000111 /* Free any no reused */
112 for (; old; old = slaac)
113 {
114 slaac = old->next;
115 free(old);
116 }
117}
118
119
120time_t periodic_slaac(time_t now, struct dhcp_lease *leases)
121{
122 struct dhcp_context *context;
123 struct dhcp_lease *lease;
Simon Kelley89382ba2012-04-04 20:48:16 +0100124 struct slaac_address *slaac;
125 time_t next_event = 0;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000126
Simon Kelley1f776932012-12-16 19:46:08 +0000127 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100128 if ((context->flags & CONTEXT_RA_NAME) && !(context->flags & CONTEXT_OLD))
Simon Kelley353ae4d2012-03-19 20:07:51 +0000129 break;
130
131 /* nothing configured */
132 if (!context)
133 return 0;
134
135 while (ping_id == 0)
136 ping_id = rand16();
137
Simon Kelley353ae4d2012-03-19 20:07:51 +0000138 for (lease = leases; lease; lease = lease->next)
139 for (slaac = lease->slaac_address; slaac; slaac = slaac->next)
140 {
Simon Kelley29689cf2012-03-22 14:01:00 +0000141 /* confirmed or given up? */
142 if (slaac->backoff == 0 || slaac->ping_time == 0)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000143 continue;
144
145 if (difftime(slaac->ping_time, now) <= 0.0)
146 {
147 struct ping_packet *ping;
148 struct sockaddr_in6 addr;
Simon Kelley89382ba2012-04-04 20:48:16 +0100149
Simon Kelley353ae4d2012-03-19 20:07:51 +0000150 save_counter(0);
151 ping = expand(sizeof(struct ping_packet));
152 ping->type = ICMP6_ECHO_REQUEST;
153 ping->code = 0;
154 ping->identifier = ping_id;
155 ping->sequence_no = slaac->backoff;
156
157 memset(&addr, 0, sizeof(addr));
158#ifdef HAVE_SOCKADDR_SA_LEN
159 addr.sin6_len = sizeof(struct sockaddr_in6);
160#endif
161 addr.sin6_family = AF_INET6;
162 addr.sin6_port = htons(IPPROTO_ICMPV6);
163 addr.sin6_addr = slaac->addr;
164
Simon Kelley89382ba2012-04-04 20:48:16 +0100165 if (sendto(daemon->icmp6fd, daemon->outpacket.iov_base, save_counter(0), 0,
166 (struct sockaddr *)&addr, sizeof(addr)) == -1 &&
167 errno == EHOSTUNREACH)
Simon Kelley50303b12012-04-04 22:13:17 +0100168 slaac->ping_time = 0; /* Give up */
Simon Kelley89382ba2012-04-04 20:48:16 +0100169 else
Simon Kelley29689cf2012-03-22 14:01:00 +0000170 {
171 slaac->ping_time += (1 << (slaac->backoff - 1)) + (rand16()/21785); /* 0 - 3 */
172 if (slaac->backoff > 4)
173 slaac->ping_time += rand16()/4000; /* 0 - 15 */
174 if (slaac->backoff < 12)
175 slaac->backoff++;
176 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000177 }
178
Simon Kelley29689cf2012-03-22 14:01:00 +0000179 if (slaac->ping_time != 0 &&
180 (next_event == 0 || difftime(next_event, slaac->ping_time) >= 0.0))
Simon Kelley353ae4d2012-03-19 20:07:51 +0000181 next_event = slaac->ping_time;
182 }
183
184 return next_event;
185}
186
187
188void slaac_ping_reply(struct in6_addr *sender, unsigned char *packet, char *interface, struct dhcp_lease *leases)
189{
190 struct dhcp_lease *lease;
191 struct slaac_address *slaac;
192 struct ping_packet *ping = (struct ping_packet *)packet;
193 int gotone = 0;
194
195 if (ping->identifier == ping_id)
196 for (lease = leases; lease; lease = lease->next)
197 for (slaac = lease->slaac_address; slaac; slaac = slaac->next)
198 if (slaac->backoff != 0 && IN6_ARE_ADDR_EQUAL(sender, &slaac->addr))
199 {
200 slaac->backoff = 0;
201 gotone = 1;
202 inet_ntop(AF_INET6, sender, daemon->addrbuff, ADDRSTRLEN);
Kevin Darbyshire-Bryant8c0b73d2013-10-11 11:56:33 +0100203 if (!option_bool(OPT_QUIET_DHCP6))
204 my_syslog(MS_DHCP | LOG_INFO, "SLAAC-CONFIRM(%s) %s %s", interface, daemon->addrbuff, lease->hostname);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000205 }
206
207 lease_update_dns(gotone);
208}
209
Simon Kelley353ae4d2012-03-19 20:07:51 +0000210#endif