blob: fdc674ba8570c813d3c4fbb2cd3fb70802281ff4 [file] [log] [blame]
Simon Kelleyc8e8f5c2021-01-24 21:59:37 +00001/* dnsmasq is Copyright (c) 2000-2021 Simon Kelley
Simon Kelleyc5ad4e72012-02-24 16:06:20 +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
Simon Kelley843c96b2012-02-27 17:42:38 +000018/* NB. This code may be called during a DHCPv4 or transaction which is in ping-wait
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000019 It therefore cannot use any DHCP buffer resources except outpacket, which is
Simon Kelley843c96b2012-02-27 17:42:38 +000020 not used by DHCPv4 code. This code may also be called when DHCP 4 or 6 isn't
21 active, so we ensure that outpacket is allocated here too */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000022
23#include "dnsmasq.h"
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000024
25#ifdef HAVE_DHCP6
26
Simon Kelley22d904d2012-02-26 20:13:45 +000027#include <netinet/icmp6.h>
28
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000029struct ra_param {
Simon Kelley1f776932012-12-16 19:46:08 +000030 time_t now;
S La18bf312016-02-12 17:36:20 +000031 int ind, managed, other, first, adv_router;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000032 char *if_name;
Simon Kelley18f0fb02012-03-31 21:18:55 +010033 struct dhcp_netid *tags;
Simon Kelleyc3a04082014-01-11 22:18:19 +000034 struct in6_addr link_local, link_global, ula;
Ilya Ponetaev5bf50af2014-09-09 12:46:21 +010035 unsigned int glob_pref_time, link_pref_time, ula_pref_time, adv_interval, prio;
S La18bf312016-02-12 17:36:20 +000036 struct dhcp_context *found_context;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000037};
38
39struct search_param {
40 time_t now; int iface;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +010041 char name[IF_NAMESIZE+1];
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000042};
43
Neil Jerram9bad3392015-06-10 22:16:35 +010044struct alias_param {
45 int iface;
46 struct dhcp_bridge *bridge;
47 int num_alias_ifs;
48 int max_alias_ifs;
49 int *alias_ifs;
50};
51
Simon Kelley1f776932012-12-16 19:46:08 +000052static void send_ra(time_t now, int iface, char *iface_name, struct in6_addr *dest);
Neil Jerramba4fc0f2015-06-10 22:14:49 +010053static void send_ra_alias(time_t now, int iface, char *iface_name, struct in6_addr *dest,
54 int send_iface);
Neil Jerram9bad3392015-06-10 22:16:35 +010055static int send_ra_to_aliases(int index, unsigned int type, char *mac, size_t maclen, void *parm);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000056static int add_prefixes(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +000057 int scope, int if_index, int flags,
Simon Kelley55b42f62012-12-21 16:53:15 +000058 unsigned int preferred, unsigned int valid, void *vparam);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000059static int iface_search(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +000060 int scope, int if_index, int flags,
Simon Kelley1f776932012-12-16 19:46:08 +000061 int prefered, int valid, void *vparam);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000062static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm);
Simon Kelleyc4cd95d2013-10-10 20:58:11 +010063static void new_timeout(struct dhcp_context *context, char *iface_name, time_t now);
64static unsigned int calc_lifetime(struct ra_interface *ra);
65static unsigned int calc_interval(struct ra_interface *ra);
66static unsigned int calc_prio(struct ra_interface *ra);
Tarun Kundu024baf12022-06-28 10:56:47 -070067static unsigned char calc_hop_limit(struct ra_interface *ra);
68static unsigned int calc_reachable_time(struct ra_interface *ra);
Tarun Kundu41aa4882022-07-05 10:44:13 -070069static unsigned int calc_retrans_time(struct ra_interface *ra);
Simon Kelleyc4cd95d2013-10-10 20:58:11 +010070static struct ra_interface *find_iface_param(char *iface);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000071
Simon Kelleyc5379c12012-02-24 20:05:52 +000072static int hop_limit;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000073
74void ra_init(time_t now)
75{
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000076 struct icmp6_filter filter;
77 int fd;
Simon Kelley0e88d532012-03-28 22:22:05 +010078#if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000079 int class = IPTOS_CLASS_CS6;
80#endif
81 int val = 255; /* radvd uses this value */
Tarun Kundu024baf12022-06-28 10:56:47 -070082 socklen_t len = sizeof(hop_limit);
Simon Kelley353ae4d2012-03-19 20:07:51 +000083 struct dhcp_context *context;
84
Simon Kelley843c96b2012-02-27 17:42:38 +000085 /* ensure this is around even if we're not doing DHCPv6 */
86 expand_buf(&daemon->outpacket, sizeof(struct dhcp_packet));
Simon Kelley353ae4d2012-03-19 20:07:51 +000087
Josh Soref730c6742017-02-06 16:14:04 +000088 /* See if we're guessing SLAAC addresses, if so we need to receive ping replies */
Simon Kelley1f776932012-12-16 19:46:08 +000089 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley353ae4d2012-03-19 20:07:51 +000090 if ((context->flags & CONTEXT_RA_NAME))
91 break;
92
Simon Kelley89500e32013-09-20 16:29:20 +010093 /* Need ICMP6 socket for transmission for DHCPv6 even when not doing RA. */
94
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000095 ICMP6_FILTER_SETBLOCKALL(&filter);
Simon Kelley89500e32013-09-20 16:29:20 +010096 if (daemon->doing_ra)
97 {
98 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);
99 if (context)
100 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filter);
101 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000102
103 if ((fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1 ||
Simon Kelleyc5379c12012-02-24 20:05:52 +0000104 getsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hop_limit, &len) ||
Simon Kelley0e88d532012-03-28 22:22:05 +0100105#if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000106 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &class, sizeof(class)) == -1 ||
107#endif
108 !fix_fd(fd) ||
109 !set_ipv6pktinfo(fd) ||
110 setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val)) ||
111 setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val)) ||
112 setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) == -1)
113 die (_("cannot create ICMPv6 socket: %s"), NULL, EC_BADNET);
114
115 daemon->icmp6fd = fd;
116
Simon Kelley89500e32013-09-20 16:29:20 +0100117 if (daemon->doing_ra)
Josh Soref730c6742017-02-06 16:14:04 +0000118 ra_start_unsolicited(now, NULL);
Tarun Kundu15048b52022-07-05 12:16:58 -0700119
120 /* Intializes random number generator */
121 srand(time(0));
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000122}
123
Josh Soref730c6742017-02-06 16:14:04 +0000124void ra_start_unsolicited(time_t now, struct dhcp_context *context)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000125{
Simon Kelley353ae4d2012-03-19 20:07:51 +0000126 /* init timers so that we do ra's for some/all soon. some ra_times will end up zeroed
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000127 if it's not appropriate to advertise those contexts.
128 This gets re-called on a netlink route-change to re-do the advertisement
129 and pick up new interfaces */
Simon Kelley6e3dba32012-12-16 21:52:45 +0000130
Simon Kelley353ae4d2012-03-19 20:07:51 +0000131 if (context)
Simon Kelley1b75c1e2012-12-18 19:55:25 +0000132 context->ra_short_period_start = context->ra_time = now;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000133 else
Simon Kelley1f776932012-12-16 19:46:08 +0000134 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley6e3dba32012-12-16 21:52:45 +0000135 if (!(context->flags & CONTEXT_TEMPLATE))
Simon Kelley1b75c1e2012-12-18 19:55:25 +0000136 {
137 context->ra_time = now + (rand16()/13000); /* range 0 - 5 */
138 /* re-do frequently for a minute or so, in case the first gets lost. */
139 context->ra_short_period_start = now;
140 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000141}
142
Simon Kelley1f776932012-12-16 19:46:08 +0000143void icmp6_packet(time_t now)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000144{
145 char interface[IF_NAMESIZE+1];
146 ssize_t sz;
147 int if_index = 0;
148 struct cmsghdr *cmptr;
149 struct msghdr msg;
150 union {
151 struct cmsghdr align; /* this ensures alignment */
152 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
153 } control_u;
154 struct sockaddr_in6 from;
Simon Kelley5ef33272012-03-30 15:10:28 +0100155 unsigned char *packet;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000156 struct iname *tmp;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000157
158 /* Note: use outpacket for input buffer */
159 msg.msg_control = control_u.control6;
160 msg.msg_controllen = sizeof(control_u);
161 msg.msg_flags = 0;
162 msg.msg_name = &from;
163 msg.msg_namelen = sizeof(from);
164 msg.msg_iov = &daemon->outpacket;
165 msg.msg_iovlen = 1;
166
167 if ((sz = recv_dhcp_packet(daemon->icmp6fd, &msg)) == -1 || sz < 8)
168 return;
Simon Kelley5ef33272012-03-30 15:10:28 +0100169
170 packet = (unsigned char *)daemon->outpacket.iov_base;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000171
172 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
173 if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
174 {
175 union {
176 unsigned char *c;
177 struct in6_pktinfo *p;
178 } p;
179 p.c = CMSG_DATA(cmptr);
180
181 if_index = p.p->ipi6_ifindex;
182 }
183
184 if (!indextoname(daemon->icmp6fd, if_index, interface))
185 return;
186
Simon Kelley4f7b3042012-11-28 21:27:02 +0000187 if (!iface_check(AF_LOCAL, NULL, interface, NULL))
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000188 return;
189
190 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
Simon Kelley49333cb2013-03-15 20:30:51 +0000191 if (tmp->name && wildcard_match(tmp->name, interface))
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000192 return;
193
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100194 if (packet[1] != 0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000195 return;
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100196
Simon Kelley5ef33272012-03-30 15:10:28 +0100197 if (packet[0] == ICMP6_ECHO_REPLY)
198 lease_ping_reply(&from.sin6_addr, packet, interface);
199 else if (packet[0] == ND_ROUTER_SOLICIT)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000200 {
Simon Kelley5ef33272012-03-30 15:10:28 +0100201 char *mac = "";
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100202 struct dhcp_bridge *bridge, *alias;
Tarun Kundu15048b52022-07-05 12:16:58 -0700203 struct timespec ts;
204 int tms = 0;
Simon Kelley5ef33272012-03-30 15:10:28 +0100205
206 /* look for link-layer address option for logging */
207 if (sz >= 16 && packet[8] == ICMP6_OPT_SOURCE_MAC && (packet[9] * 8) + 8 <= sz)
208 {
Simon Kelley24036ea2017-09-25 18:47:15 +0100209 if ((packet[9] * 8 - 2) * 3 - 1 >= MAXDNAME) {
210 return;
211 }
Simon Kelley5ef33272012-03-30 15:10:28 +0100212 print_mac(daemon->namebuff, &packet[10], (packet[9] * 8) - 2);
213 mac = daemon->namebuff;
214 }
215
Kevin Darbyshire-Bryant8c0b73d2013-10-11 11:56:33 +0100216 if (!option_bool(OPT_QUIET_RA))
217 my_syslog(MS_DHCP | LOG_INFO, "RTR-SOLICIT(%s) %s", interface, mac);
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100218
219 /* If the incoming interface is an alias of some other one (as
Neil Jerram4918bd52015-06-10 22:23:20 +0100220 specified by the --bridge-interface option), send an RA using
221 the context of the aliased interface. */
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100222 for (bridge = daemon->bridges; bridge; bridge = bridge->next)
223 {
224 int bridge_index = if_nametoindex(bridge->iface);
225 if (bridge_index)
226 {
227 for (alias = bridge->alias; alias; alias = alias->next)
228 if (wildcard_matchn(alias->iface, interface, IF_NAMESIZE))
229 {
230 /* Send an RA on if_index with information from
231 bridge_index. */
232 send_ra_alias(now, bridge_index, bridge->iface, NULL, if_index);
233 break;
234 }
235 if (alias)
236 break;
237 }
238 }
239
240 /* If the incoming interface wasn't an alias, send an RA using
241 the context of the incoming interface. */
242 if (!bridge)
243 /* source address may not be valid in solicit request. */
244 send_ra(now, if_index, interface, !IN6_IS_ADDR_UNSPECIFIED(&from.sin6_addr) ? &from.sin6_addr : NULL);
Tarun Kundu15048b52022-07-05 12:16:58 -0700245 tms = random_num_in_range(MAX_RA_DELAY_TIME);
246 ts.tv_sec = 0;
247 ts.tv_nsec = tms * 1000000;
248 nanosleep(&ts, NULL);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000249 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000250}
251
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100252static void send_ra_alias(time_t now, int iface, char *iface_name, struct in6_addr *dest, int send_iface)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000253{
254 struct ra_packet *ra;
255 struct ra_param parm;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000256 struct sockaddr_in6 addr;
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100257 struct dhcp_context *context, *tmp, **up;
Simon Kelley18f0fb02012-03-31 21:18:55 +0100258 struct dhcp_netid iface_id;
259 struct dhcp_opt *opt_cfg;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100260 struct ra_interface *ra_param = find_iface_param(iface_name);
David Flamand005c46d2017-04-11 11:49:54 +0100261 int done_dns = 0, old_prefix = 0, mtu = 0;
Simon Kelleyc3a04082014-01-11 22:18:19 +0000262 unsigned int min_pref_time;
Simon Kelley3b436462012-12-28 11:55:45 +0000263#ifdef HAVE_LINUX_NETWORK
264 FILE *f;
265#endif
Tarun Kundu024baf12022-06-28 10:56:47 -0700266
267 struct iname *if_tmp;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000268 parm.ind = iface;
269 parm.managed = 0;
Simon Kelley30cd9662012-03-25 20:44:38 +0100270 parm.other = 0;
S La18bf312016-02-12 17:36:20 +0000271 parm.found_context = NULL;
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100272 parm.adv_router = 0;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000273 parm.if_name = iface_name;
274 parm.first = 1;
Simon Kelley1f776932012-12-16 19:46:08 +0000275 parm.now = now;
Simon Kelleyc3a04082014-01-11 22:18:19 +0000276 parm.glob_pref_time = parm.link_pref_time = parm.ula_pref_time = 0;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100277 parm.adv_interval = calc_interval(ra_param);
Ilya Ponetaev5bf50af2014-09-09 12:46:21 +0100278 parm.prio = calc_prio(ra_param);
Simon Kelley1f776932012-12-16 19:46:08 +0000279
Simon Kelleyfa785732016-07-22 20:56:01 +0100280 reset_counter();
Simon Kelleyce7845b2016-07-06 21:42:27 +0100281
282 if (!(ra = expand(sizeof(struct ra_packet))))
283 return;
Ilya Ponetaev5bf50af2014-09-09 12:46:21 +0100284
285 ra->type = ND_ROUTER_ADVERT;
286 ra->code = 0;
Ilya Ponetaev5bf50af2014-09-09 12:46:21 +0100287 ra->flags = parm.prio;
288 ra->lifetime = htons(calc_lifetime(ra_param));
Tarun Kundu024baf12022-06-28 10:56:47 -0700289 ra->reachable_time = htonl(calc_reachable_time(ra_param));
290 ra->hop_limit = calc_hop_limit(ra_param);
Tarun Kundu41aa4882022-07-05 10:44:13 -0700291 ra->retrans_time = htonl(calc_retrans_time(ra_param));
Ilya Ponetaev5bf50af2014-09-09 12:46:21 +0100292
Simon Kelley18f0fb02012-03-31 21:18:55 +0100293 /* set tag with name == interface */
294 iface_id.net = iface_name;
295 iface_id.next = NULL;
296 parm.tags = &iface_id;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000297
Tarun Kundu74fb2b62022-07-06 09:11:18 -0700298 for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next)
299 {
300 if ((strcmp(if_tmp->name, iface_name) == 0) && (if_tmp->is_ra_adv_dis == true)) {
301 if (if_tmp->final_ra_sent_count < MAX_FINAL_RTR_ADVERTISEMENTS) {
302 ra->lifetime = htons(0);
303 if_tmp->final_ra_sent_count++;
304 break;
305 }
306 else {
307 return;
308 }
309 }
310 }
311
Simon Kelley1f776932012-12-16 19:46:08 +0000312 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley18f0fb02012-03-31 21:18:55 +0100313 {
314 context->flags &= ~CONTEXT_RA_DONE;
315 context->netid.next = &context->netid;
316 }
317
Simon Kelley378fa562020-01-05 17:23:19 +0000318 /* If no link-local address then we can't advertise since source address of
319 advertisement must be link local address: RFC 4861 para 6.1.2. */
320 if (!iface_enumerate(AF_INET6, &parm, add_prefixes) ||
321 parm.link_pref_time == 0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000322 return;
323
Simon Kelleyc3a04082014-01-11 22:18:19 +0000324 /* Find smallest preferred time within address classes,
325 to use as lifetime for options. This is a rather arbitrary choice. */
326 min_pref_time = 0xffffffff;
327 if (parm.glob_pref_time != 0 && parm.glob_pref_time < min_pref_time)
328 min_pref_time = parm.glob_pref_time;
329
330 if (parm.ula_pref_time != 0 && parm.ula_pref_time < min_pref_time)
331 min_pref_time = parm.ula_pref_time;
332
333 if (parm.link_pref_time != 0 && parm.link_pref_time < min_pref_time)
334 min_pref_time = parm.link_pref_time;
335
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100336 /* Look for constructed contexts associated with addresses which have gone,
337 and advertise them with preferred_time == 0 RFC 6204 4.3 L-13 */
338 for (up = &daemon->dhcp6, context = daemon->dhcp6; context; context = tmp)
339 {
340 tmp = context->next;
Simon Kelley3b436462012-12-28 11:55:45 +0000341
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100342 if (context->if_index == iface && (context->flags & CONTEXT_OLD))
343 {
344 unsigned int old = difftime(now, context->address_lost_time);
345
346 if (old > context->saved_valid)
S La18bf312016-02-12 17:36:20 +0000347 {
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100348 /* We've advertised this enough, time to go */
S La18bf312016-02-12 17:36:20 +0000349
350 /* If this context held the timeout, and there's another context in use
351 transfer the timeout there. */
352 if (context->ra_time != 0 && parm.found_context && parm.found_context->ra_time == 0)
353 new_timeout(parm.found_context, iface_name, now);
354
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100355 *up = context->next;
356 free(context);
357 }
358 else
359 {
360 struct prefix_opt *opt;
361 struct in6_addr local = context->start6;
362 int do_slaac = 0;
363
Simon Kelley44522922013-11-15 14:45:04 +0000364 old_prefix = 1;
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100365
366 /* zero net part of address */
367 setaddr6part(&local, addr6part(&local) & ~((context->prefix == 64) ? (u64)-1LL : (1LLU << (128 - context->prefix)) - 1LLU));
368
Simon Kelley6ea1f232013-12-19 15:45:12 +0000369
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100370 if (context->flags & CONTEXT_RA)
Simon Kelley6ea1f232013-12-19 15:45:12 +0000371 {
372 do_slaac = 1;
373 if (context->flags & CONTEXT_DHCP)
374 {
375 parm.other = 1;
376 if (!(context->flags & CONTEXT_RA_STATELESS))
377 parm.managed = 1;
378 }
379 }
380 else
381 {
382 /* don't do RA for non-ra-only unless --enable-ra is set */
383 if (option_bool(OPT_RA))
384 {
385 parm.managed = 1;
386 parm.other = 1;
387 }
388 }
389
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100390 if ((opt = expand(sizeof(struct prefix_opt))))
391 {
392 opt->type = ICMP6_OPT_PREFIX;
393 opt->len = 4;
394 opt->prefix_len = context->prefix;
Neil Jerram2fd5bc92015-06-10 22:13:06 +0100395 /* autonomous only if we're not doing dhcp, set
396 "on-link" unless "off-link" was specified */
397 opt->flags = (do_slaac ? 0x40 : 0) |
398 ((context->flags & CONTEXT_RA_OFF_LINK) ? 0 : 0x80);
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100399 opt->valid_lifetime = htonl(context->saved_valid - old);
400 opt->preferred_lifetime = htonl(0);
401 opt->reserved = 0;
402 opt->prefix = local;
403
404 inet_ntop(AF_INET6, &local, daemon->addrbuff, ADDRSTRLEN);
Kevin Darbyshire-Bryant8c0b73d2013-10-11 11:56:33 +0100405 if (!option_bool(OPT_QUIET_RA))
406 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s old prefix", iface_name, daemon->addrbuff);
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100407 }
408
409 up = &context->next;
410 }
411 }
412 else
413 up = &context->next;
414 }
415
Simon Kelley44522922013-11-15 14:45:04 +0000416 /* If we're advertising only old prefixes, set router lifetime to zero. */
417 if (old_prefix && !parm.found_context)
418 ra->lifetime = htons(0);
419
420 /* No prefixes to advertise. */
421 if (!old_prefix && !parm.found_context)
422 return;
423
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100424 /* If we're sending router address instead of prefix in at least on prefix,
425 include the advertisement interval option. */
426 if (parm.adv_router)
427 {
428 put_opt6_char(ICMP6_OPT_ADV_INTERVAL);
429 put_opt6_char(1);
430 put_opt6_short(0);
431 /* interval value is in milliseconds */
432 put_opt6_long(1000 * calc_interval(find_iface_param(iface_name)));
433 }
434
David Flamand005c46d2017-04-11 11:49:54 +0100435 /* Set the MTU from ra_param if any, an MTU of 0 mean automatic for linux, */
436 /* an MTU of -1 prevents the option from being sent. */
437 if (ra_param)
438 mtu = ra_param->mtu;
Simon Kelley3b436462012-12-28 11:55:45 +0000439#ifdef HAVE_LINUX_NETWORK
Ville Skyttäfaaf3062018-01-14 17:32:52 +0000440 /* Note that IPv6 MTU is not necessarily the same as the IPv4 MTU
Simon Kelley3b436462012-12-28 11:55:45 +0000441 available from SIOCGIFMTU */
David Flamand005c46d2017-04-11 11:49:54 +0100442 if (mtu == 0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000443 {
Vladislav Grishenko6ec5f5c2017-04-24 22:34:45 +0100444 char *mtu_name = ra_param ? ra_param->mtu_name : NULL;
Simon Kelley5ed82ae2019-03-05 16:38:34 +0000445 sprintf(daemon->namebuff, "/proc/sys/net/ipv6/conf/%s/mtu", mtu_name ? mtu_name : iface_name);
David Flamand005c46d2017-04-11 11:49:54 +0100446 if ((f = fopen(daemon->namebuff, "r")))
447 {
448 if (fgets(daemon->namebuff, MAXDNAME, f))
449 mtu = atoi(daemon->namebuff);
450 fclose(f);
451 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000452 }
Simon Kelley3b436462012-12-28 11:55:45 +0000453#endif
David Flamand005c46d2017-04-11 11:49:54 +0100454 if (mtu > 0)
455 {
456 put_opt6_char(ICMP6_OPT_MTU);
457 put_opt6_char(1);
458 put_opt6_short(0);
459 put_opt6_long(mtu);
460 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000461
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100462 iface_enumerate(AF_LOCAL, &send_iface, add_lla);
Simon Kelley18f0fb02012-03-31 21:18:55 +0100463
464 /* RDNSS, RFC 6106, use relevant DHCP6 options */
465 (void)option_filter(parm.tags, NULL, daemon->dhcp_opts6);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000466
Simon Kelley18f0fb02012-03-31 21:18:55 +0100467 for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next)
468 {
469 int i;
470
471 /* netids match and not encapsulated? */
472 if (!(opt_cfg->flags & DHOPT_TAGOK))
473 continue;
474
475 if (opt_cfg->opt == OPTION6_DNS_SERVER)
476 {
Simon Kelleyc3a04082014-01-11 22:18:19 +0000477 struct in6_addr *a;
478 int len;
479
Simon Kelley18f0fb02012-03-31 21:18:55 +0100480 done_dns = 1;
Simon Kelleyc3a04082014-01-11 22:18:19 +0000481
482 if (opt_cfg->len == 0)
Simon Kelley806cf782013-10-14 14:08:44 +0100483 continue;
Simon Kelley18f0fb02012-03-31 21:18:55 +0100484
Simon Kelleyc3a04082014-01-11 22:18:19 +0000485 /* reduce len for any addresses we can't substitute */
486 for (a = (struct in6_addr *)opt_cfg->val, len = opt_cfg->len, i = 0;
487 i < opt_cfg->len; i += IN6ADDRSZ, a++)
488 if ((IN6_IS_ADDR_UNSPECIFIED(a) && parm.glob_pref_time == 0) ||
489 (IN6_IS_ADDR_ULA_ZERO(a) && parm.ula_pref_time == 0) ||
490 (IN6_IS_ADDR_LINK_LOCAL_ZERO(a) && parm.link_pref_time == 0))
491 len -= IN6ADDRSZ;
492
493 if (len != 0)
494 {
495 put_opt6_char(ICMP6_OPT_RDNSS);
496 put_opt6_char((len/8) + 1);
497 put_opt6_short(0);
498 put_opt6_long(min_pref_time);
499
500 for (a = (struct in6_addr *)opt_cfg->val, i = 0; i < opt_cfg->len; i += IN6ADDRSZ, a++)
501 if (IN6_IS_ADDR_UNSPECIFIED(a))
502 {
503 if (parm.glob_pref_time != 0)
504 put_opt6(&parm.link_global, IN6ADDRSZ);
505 }
506 else if (IN6_IS_ADDR_ULA_ZERO(a))
507 {
508 if (parm.ula_pref_time != 0)
509 put_opt6(&parm.ula, IN6ADDRSZ);
510 }
511 else if (IN6_IS_ADDR_LINK_LOCAL_ZERO(a))
512 {
513 if (parm.link_pref_time != 0)
514 put_opt6(&parm.link_local, IN6ADDRSZ);
515 }
516 else
517 put_opt6(a, IN6ADDRSZ);
518 }
Simon Kelley18f0fb02012-03-31 21:18:55 +0100519 }
520
521 if (opt_cfg->opt == OPTION6_DOMAIN_SEARCH && opt_cfg->len != 0)
522 {
523 int len = ((opt_cfg->len+7)/8);
524
525 put_opt6_char(ICMP6_OPT_DNSSL);
526 put_opt6_char(len + 1);
527 put_opt6_short(0);
Simon Kelleyc3a04082014-01-11 22:18:19 +0000528 put_opt6_long(min_pref_time);
Simon Kelley18f0fb02012-03-31 21:18:55 +0100529 put_opt6(opt_cfg->val, opt_cfg->len);
530
531 /* pad */
532 for (i = opt_cfg->len; i < len * 8; i++)
533 put_opt6_char(0);
534 }
535 }
536
Simon Kelleyc3a04082014-01-11 22:18:19 +0000537 if (daemon->port == NAMESERVER_PORT && !done_dns && parm.link_pref_time != 0)
Simon Kelley18f0fb02012-03-31 21:18:55 +0100538 {
Simon Kelleyab915f82013-04-30 10:41:28 +0100539 /* default == us, as long as we are supplying DNS service. */
Simon Kelley18f0fb02012-03-31 21:18:55 +0100540 put_opt6_char(ICMP6_OPT_RDNSS);
541 put_opt6_char(3);
542 put_opt6_short(0);
Simon Kelleyc3a04082014-01-11 22:18:19 +0000543 put_opt6_long(min_pref_time);
Simon Kelley806cf782013-10-14 14:08:44 +0100544 put_opt6(&parm.link_local, IN6ADDRSZ);
Simon Kelley18f0fb02012-03-31 21:18:55 +0100545 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000546
547 /* set managed bits unless we're providing only RA on this link */
548 if (parm.managed)
Simon Kelley30cd9662012-03-25 20:44:38 +0100549 ra->flags |= 0x80; /* M flag, managed, */
550 if (parm.other)
551 ra->flags |= 0x40; /* O flag, other */
552
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000553 /* decide where we're sending */
554 memset(&addr, 0, sizeof(addr));
Simon Kelley22d904d2012-02-26 20:13:45 +0000555#ifdef HAVE_SOCKADDR_SA_LEN
556 addr.sin6_len = sizeof(struct sockaddr_in6);
557#endif
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000558 addr.sin6_family = AF_INET6;
559 addr.sin6_port = htons(IPPROTO_ICMPV6);
560 if (dest)
561 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000562 addr.sin6_addr = *dest;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000563 if (IN6_IS_ADDR_LINKLOCAL(dest) ||
564 IN6_IS_ADDR_MC_LINKLOCAL(dest))
565 addr.sin6_scope_id = iface;
566 }
567 else
Simon Kelley8f3194f2013-09-30 15:04:58 +0100568 {
569 inet_pton(AF_INET6, ALL_NODES, &addr.sin6_addr);
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100570 setsockopt(daemon->icmp6fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &send_iface, sizeof(send_iface));
Simon Kelley8f3194f2013-09-30 15:04:58 +0100571 }
Simon Kelleyff841eb2015-03-11 21:36:30 +0000572 while (retry_send(sendto(daemon->icmp6fd, daemon->outpacket.iov_base,
Simon Kelley6b1c4642016-07-22 20:59:16 +0100573 save_counter(-1), 0, (struct sockaddr *)&addr,
Simon Kelleyff841eb2015-03-11 21:36:30 +0000574 sizeof(addr))));
Tarun Kundu74fb2b62022-07-06 09:11:18 -0700575 ra_param->ra_sent_count++;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000576}
577
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100578static void send_ra(time_t now, int iface, char *iface_name, struct in6_addr *dest)
579{
580 /* Send an RA on the same interface that the RA content is based
581 on. */
582 send_ra_alias(now, iface, iface_name, dest, iface);
583}
584
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000585static int add_prefixes(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +0000586 int scope, int if_index, int flags,
Simon Kelley55b42f62012-12-21 16:53:15 +0000587 unsigned int preferred, unsigned int valid, void *vparam)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000588{
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000589 struct ra_param *param = vparam;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000590
591 (void)scope; /* warning */
Tarun Kundu024baf12022-06-28 10:56:47 -0700592
593 struct iname *if_tmp;
594 for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next)
595 {
596 if ((strcmp(if_tmp->name, param->if_name) == 0) && (if_tmp->is_ra_adv_dis == true)) {
Tarun Kundu74fb2b62022-07-06 09:11:18 -0700597 if (if_tmp->final_ra_sent_count < MAX_FINAL_RTR_ADVERTISEMENTS) {
598 break;
599 }
600 else {
601 return 1;
602 }
Tarun Kundu024baf12022-06-28 10:56:47 -0700603 }
604 }
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000605
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000606 if (if_index == param->ind)
607 {
608 if (IN6_IS_ADDR_LINKLOCAL(local))
Simon Kelleyc3a04082014-01-11 22:18:19 +0000609 {
610 /* Can there be more than one LL address?
611 Select the one with the longest preferred time
612 if there is. */
613 if (preferred > param->link_pref_time)
614 {
615 param->link_pref_time = preferred;
616 param->link_local = *local;
617 }
618 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000619 else if (!IN6_IS_ADDR_LOOPBACK(local) &&
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000620 !IN6_IS_ADDR_MULTICAST(local))
621 {
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000622 int real_prefix = 0;
Simon Kelleyc8257542012-03-28 21:15:41 +0100623 int do_slaac = 0;
624 int deprecate = 0;
Simon Kelley3bc0d932012-12-28 11:31:44 +0000625 int constructed = 0;
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100626 int adv_router = 0;
Neil Jerram2fd5bc92015-06-10 22:13:06 +0100627 int off_link = 0;
Kyle Swenson9d238072021-12-01 08:13:17 -0700628 unsigned int valid_lifetime = 0xffffffff;
Simon Kelleyc8257542012-03-28 21:15:41 +0100629 unsigned int time = 0xffffffff;
Simon Kelley1e02a852012-03-29 11:07:25 +0100630 struct dhcp_context *context;
631
Simon Kelley1f776932012-12-16 19:46:08 +0000632 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100633 if (!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) &&
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000634 prefix <= context->prefix &&
635 is_same_net6(local, &context->start6, context->prefix) &&
636 is_same_net6(local, &context->end6, context->prefix))
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000637 {
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100638 context->saved_valid = valid;
639
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100640 if (context->flags & CONTEXT_RA)
Simon Kelley30cd9662012-03-25 20:44:38 +0100641 {
642 do_slaac = 1;
Simon Kelley4723d492012-03-30 21:04:17 +0100643 if (context->flags & CONTEXT_DHCP)
Simon Kelley05e92e52012-03-30 22:24:15 +0100644 {
645 param->other = 1;
646 if (!(context->flags & CONTEXT_RA_STATELESS))
647 param->managed = 1;
648 }
Simon Kelley30cd9662012-03-25 20:44:38 +0100649 }
Simon Kelley884a6df2012-03-20 16:20:22 +0000650 else
Simon Kelley0010b472012-02-29 12:18:30 +0000651 {
652 /* don't do RA for non-ra-only unless --enable-ra is set */
653 if (!option_bool(OPT_RA))
654 continue;
655 param->managed = 1;
Simon Kelley30cd9662012-03-25 20:44:38 +0100656 param->other = 1;
Simon Kelley0010b472012-02-29 12:18:30 +0000657 }
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100658
659 /* Configured to advertise router address, not prefix. See RFC 3775 7.2
660 In this case we do all addresses associated with a context,
661 hence the real_prefix setting here. */
662 if (context->flags & CONTEXT_RA_ROUTER)
663 {
664 adv_router = 1;
665 param->adv_router = 1;
666 real_prefix = context->prefix;
667 }
668
Simon Kelley4d85e402020-07-12 22:45:46 +0100669 /* find floor time, don't reduce below 3 * RA interval.
670 If the lease time has been left as default, don't
671 use that as a floor. */
672 if ((context->flags & CONTEXT_SETLEASE) &&
673 time > context->lease_time)
Simon Kelley7f035f52012-12-22 21:27:08 +0000674 {
Kyle Swenson9d238072021-12-01 08:13:17 -0700675
676 if ((context->flags & CONTEXT_SETVALID))
677 valid_lifetime = context->valid_lifetime;
678 else
679 valid_lifetime = context->lease_time;
680
Simon Kelley7f035f52012-12-22 21:27:08 +0000681 time = context->lease_time;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100682 if (time < ((unsigned int)(3 * param->adv_interval)))
683 time = 3 * param->adv_interval;
Simon Kelley7f035f52012-12-22 21:27:08 +0000684 }
685
Simon Kelleyc8257542012-03-28 21:15:41 +0100686 if (context->flags & CONTEXT_DEPRECATE)
687 deprecate = 1;
Simon Kelley3bc0d932012-12-28 11:31:44 +0000688
689 if (context->flags & CONTEXT_CONSTRUCTED)
690 constructed = 1;
691
Simon Kelleyc8257542012-03-28 21:15:41 +0100692
Simon Kelley18f0fb02012-03-31 21:18:55 +0100693 /* collect dhcp-range tags */
694 if (context->netid.next == &context->netid && context->netid.net)
695 {
696 context->netid.next = param->tags;
697 param->tags = &context->netid;
698 }
699
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100700 /* subsequent prefixes on the same interface
701 and subsequent instances of this prefix don't need timers.
702 Be careful not to find the same prefix twice with different
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100703 addresses unless we're advertising the actual addresses. */
Simon Kelley1e02a852012-03-29 11:07:25 +0100704 if (!(context->flags & CONTEXT_RA_DONE))
705 {
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100706 if (!param->first)
707 context->ra_time = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100708 context->flags |= CONTEXT_RA_DONE;
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000709 real_prefix = context->prefix;
Neil Jerram2fd5bc92015-06-10 22:13:06 +0100710 off_link = (context->flags & CONTEXT_RA_OFF_LINK);
Simon Kelley1e02a852012-03-29 11:07:25 +0100711 }
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100712
S La18bf312016-02-12 17:36:20 +0000713 param->first = 0;
714 /* found_context is the _last_ one we found, so if there's
715 more than one, it's not the first. */
716 param->found_context = context;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000717 }
Simon Kelley1f776932012-12-16 19:46:08 +0000718
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000719 /* configured time is ceiling */
Simon Kelley3bc0d932012-12-28 11:31:44 +0000720 if (!constructed || valid > time)
Kyle Swenson9d238072021-12-01 08:13:17 -0700721 valid = valid_lifetime;
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000722
Simon Kelley3bc0d932012-12-28 11:31:44 +0000723 if (flags & IFACE_DEPRECATED)
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000724 preferred = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100725
Simon Kelley3bc0d932012-12-28 11:31:44 +0000726 if (deprecate)
727 time = 0;
728
729 /* configured time is ceiling */
730 if (!constructed || preferred > time)
731 preferred = time;
Simon Kelleyc3a04082014-01-11 22:18:19 +0000732
733 if (IN6_IS_ADDR_ULA(local))
Simon Kelley55b42f62012-12-21 16:53:15 +0000734 {
Simon Kelleyc3a04082014-01-11 22:18:19 +0000735 if (preferred > param->ula_pref_time)
736 {
737 param->ula_pref_time = preferred;
738 param->ula = *local;
739 }
740 }
741 else
742 {
743 if (preferred > param->glob_pref_time)
744 {
745 param->glob_pref_time = preferred;
746 param->link_global = *local;
747 }
Simon Kelley55b42f62012-12-21 16:53:15 +0000748 }
749
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000750 if (real_prefix != 0)
Simon Kelleyc8257542012-03-28 21:15:41 +0100751 {
Simon Kelley1e02a852012-03-29 11:07:25 +0100752 struct prefix_opt *opt;
753
754 if ((opt = expand(sizeof(struct prefix_opt))))
755 {
Simon Kelley5ef33272012-03-30 15:10:28 +0100756 /* zero net part of address */
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100757 if (!adv_router)
758 setaddr6part(local, addr6part(local) & ~((real_prefix == 64) ? (u64)-1LL : (1LLU << (128 - real_prefix)) - 1LLU));
Simon Kelley5ef33272012-03-30 15:10:28 +0100759
Simon Kelley1e02a852012-03-29 11:07:25 +0100760 opt->type = ICMP6_OPT_PREFIX;
761 opt->len = 4;
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000762 opt->prefix_len = real_prefix;
Neil Jerram2fd5bc92015-06-10 22:13:06 +0100763 /* autonomous only if we're not doing dhcp, set
764 "on-link" unless "off-link" was specified */
765 opt->flags = (off_link ? 0 : 0x80);
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100766 if (do_slaac)
767 opt->flags |= 0x40;
768 if (adv_router)
769 opt->flags |= 0x20;
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000770 opt->valid_lifetime = htonl(valid);
771 opt->preferred_lifetime = htonl(preferred);
Simon Kelley5ef33272012-03-30 15:10:28 +0100772 opt->reserved = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100773 opt->prefix = *local;
Simon Kelley1e02a852012-03-29 11:07:25 +0100774
Simon Kelley5ef33272012-03-30 15:10:28 +0100775 inet_ntop(AF_INET6, local, daemon->addrbuff, ADDRSTRLEN);
Kevin Darbyshire-Bryant8c0b73d2013-10-11 11:56:33 +0100776 if (!option_bool(OPT_QUIET_RA))
777 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s", param->if_name, daemon->addrbuff);
Simon Kelley1e02a852012-03-29 11:07:25 +0100778 }
Simon Kelleyc8257542012-03-28 21:15:41 +0100779 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000780 }
781 }
782 return 1;
783}
784
785static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm)
786{
787 (void)type;
788
789 if (index == *((int *)parm))
790 {
791 /* size is in units of 8 octets and includes type and length (2 bytes)
792 add 7 to round up */
793 int len = (maclen + 9) >> 3;
794 unsigned char *p = expand(len << 3);
795 memset(p, 0, len << 3);
796 *p++ = ICMP6_OPT_SOURCE_MAC;
797 *p++ = len;
798 memcpy(p, mac, maclen);
799
800 return 0;
801 }
802
803 return 1;
804}
805
806time_t periodic_ra(time_t now)
807{
808 struct search_param param;
809 struct dhcp_context *context;
810 time_t next_event;
Neil Jerram9bad3392015-06-10 22:16:35 +0100811 struct alias_param aparam;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100812
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000813 param.now = now;
Simon Kelley29d28dd2012-12-03 14:05:59 +0000814 param.iface = 0;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000815
816 while (1)
817 {
818 /* find overdue events, and time of first future event */
Simon Kelley1f776932012-12-16 19:46:08 +0000819 for (next_event = 0, context = daemon->dhcp6; context; context = context->next)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000820 if (context->ra_time != 0)
821 {
Simon Kelley7dbe9812012-03-25 14:49:54 +0100822 if (difftime(context->ra_time, now) <= 0.0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000823 break; /* overdue */
824
Simon Kelley7dbe9812012-03-25 14:49:54 +0100825 if (next_event == 0 || difftime(next_event, context->ra_time) > 0.0)
826 next_event = context->ra_time;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000827 }
828
829 /* none overdue */
830 if (!context)
831 break;
832
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100833 if ((context->flags & CONTEXT_OLD) &&
834 context->if_index != 0 &&
Simon Kelleydd9d9ce2013-11-15 11:24:00 +0000835 indextoname(daemon->icmp6fd, context->if_index, param.name))
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100836 {
837 /* A context for an old address. We'll not find the interface by
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100838 looking for addresses, but we know it anyway, since the context is
839 constructed */
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100840 param.iface = context->if_index;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100841 new_timeout(context, param.name, now);
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100842 }
843 else if (iface_enumerate(AF_INET6, &param, iface_search))
844 /* There's a context overdue, but we can't find an interface
845 associated with it, because it's for a subnet we dont
846 have an interface on. Probably we're doing DHCP on
847 a remote subnet via a relay. Zero the timer, since we won't
Josh Soref730c6742017-02-06 16:14:04 +0000848 ever be able to send ra's and satisfy it. */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000849 context->ra_time = 0;
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100850
851 if (param.iface != 0 &&
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100852 iface_check(AF_LOCAL, NULL, param.name, NULL))
Simon Kelley421594f2012-12-02 12:17:35 +0000853 {
854 struct iname *tmp;
855 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100856 if (tmp->name && wildcard_match(tmp->name, param.name))
Simon Kelley421594f2012-12-02 12:17:35 +0000857 break;
858 if (!tmp)
Neil Jerram9bad3392015-06-10 22:16:35 +0100859 {
Tarun Kundu024baf12022-06-28 10:56:47 -0700860 struct iname *if_tmp;
861 for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next)
862 {
Tarun Kundu74fb2b62022-07-06 09:11:18 -0700863 if ((strcmp(if_tmp->name, param.name) == 0) &&
864 ((if_tmp->is_ra_adv_dis == false) ||
865 (if_tmp->final_ra_sent_count < MAX_FINAL_RTR_ADVERTISEMENTS))) {
Tarun Kundu024baf12022-06-28 10:56:47 -0700866 send_ra(now, param.iface, param.name, NULL);
Neil Jerram9bad3392015-06-10 22:16:35 +0100867
Tarun Kundu024baf12022-06-28 10:56:47 -0700868 /* Also send on all interfaces that are aliases of this
Neil Jerram9bad3392015-06-10 22:16:35 +0100869 one. */
Tarun Kundu024baf12022-06-28 10:56:47 -0700870 for (aparam.bridge = daemon->bridges;
Neil Jerram9bad3392015-06-10 22:16:35 +0100871 aparam.bridge;
872 aparam.bridge = aparam.bridge->next)
Tarun Kundu024baf12022-06-28 10:56:47 -0700873 if ((int)if_nametoindex(aparam.bridge->iface) == param.iface)
Neil Jerram9bad3392015-06-10 22:16:35 +0100874 {
875 /* Count the number of alias interfaces for this
876 'bridge', by calling iface_enumerate with
877 send_ra_to_aliases and NULL alias_ifs. */
878 aparam.iface = param.iface;
879 aparam.alias_ifs = NULL;
880 aparam.num_alias_ifs = 0;
881 iface_enumerate(AF_LOCAL, &aparam, send_ra_to_aliases);
882 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s => %d alias(es)",
883 param.name, daemon->addrbuff, aparam.num_alias_ifs);
884
885 /* Allocate memory to store the alias interface
886 indices. */
887 aparam.alias_ifs = (int *)whine_malloc(aparam.num_alias_ifs *
888 sizeof(int));
889 if (aparam.alias_ifs)
890 {
891 /* Use iface_enumerate again to get the alias
892 interface indices, then send on each of
893 those. */
894 aparam.max_alias_ifs = aparam.num_alias_ifs;
895 aparam.num_alias_ifs = 0;
896 iface_enumerate(AF_LOCAL, &aparam, send_ra_to_aliases);
897 for (; aparam.num_alias_ifs; aparam.num_alias_ifs--)
898 {
899 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s => i/f %d",
900 param.name, daemon->addrbuff,
901 aparam.alias_ifs[aparam.num_alias_ifs - 1]);
902 send_ra_alias(now,
903 param.iface,
904 param.name,
905 NULL,
906 aparam.alias_ifs[aparam.num_alias_ifs - 1]);
907 }
908 free(aparam.alias_ifs);
909 }
910
911 /* The source interface can only appear in at most
Neil Jerram4918bd52015-06-10 22:23:20 +0100912 one --bridge-interface. */
Neil Jerram9bad3392015-06-10 22:16:35 +0100913 break;
914 }
Tarun Kundu024baf12022-06-28 10:56:47 -0700915 }
916 }
Neil Jerram9bad3392015-06-10 22:16:35 +0100917 }
Simon Kelley421594f2012-12-02 12:17:35 +0000918 }
919 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000920 return next_event;
921}
Neil Jerram9bad3392015-06-10 22:16:35 +0100922
923static int send_ra_to_aliases(int index, unsigned int type, char *mac, size_t maclen, void *parm)
924{
925 struct alias_param *aparam = (struct alias_param *)parm;
926 char ifrn_name[IFNAMSIZ];
927 struct dhcp_bridge *alias;
928
929 (void)type;
930 (void)mac;
931 (void)maclen;
932
933 if (if_indextoname(index, ifrn_name))
934 for (alias = aparam->bridge->alias; alias; alias = alias->next)
935 if (wildcard_matchn(alias->iface, ifrn_name, IFNAMSIZ))
936 {
937 if (aparam->alias_ifs && (aparam->num_alias_ifs < aparam->max_alias_ifs))
938 aparam->alias_ifs[aparam->num_alias_ifs] = index;
939 aparam->num_alias_ifs++;
940 }
941
942 return 1;
943}
944
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000945static int iface_search(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +0000946 int scope, int if_index, int flags,
Simon Kelley1f776932012-12-16 19:46:08 +0000947 int preferred, int valid, void *vparam)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000948{
949 struct search_param *param = vparam;
Simon Kelley741c2952012-02-25 13:09:18 +0000950 struct dhcp_context *context;
Simon Kelleycd672932020-01-27 22:53:07 +0000951 struct iname *tmp;
952
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000953 (void)scope;
Simon Kelley1f776932012-12-16 19:46:08 +0000954 (void)preferred;
955 (void)valid;
Simon Kelleycd672932020-01-27 22:53:07 +0000956
957 /* ignore interfaces we're not doing DHCP on. */
958 if (!indextoname(daemon->icmp6fd, if_index, param->name) ||
959 !iface_check(AF_LOCAL, NULL, param->name, NULL))
960 return 1;
961
962 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
963 if (tmp->name && wildcard_match(tmp->name, param->name))
964 return 1;
965
Simon Kelley1f776932012-12-16 19:46:08 +0000966 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100967 if (!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) &&
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000968 prefix <= context->prefix &&
969 is_same_net6(local, &context->start6, context->prefix) &&
970 is_same_net6(local, &context->end6, context->prefix) &&
Simon Kelley6e3dba32012-12-16 21:52:45 +0000971 context->ra_time != 0 &&
972 difftime(context->ra_time, param->now) <= 0.0)
973 {
974 /* found an interface that's overdue for RA determine new
975 timeout value and arrange for RA to be sent unless interface is
976 still doing DAD.*/
Simon Kelleybad7b872012-12-20 22:00:39 +0000977 if (!(flags & IFACE_TENTATIVE))
Simon Kelley6e3dba32012-12-16 21:52:45 +0000978 param->iface = if_index;
979
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100980 new_timeout(context, param->name, param->now);
Simon Kelley6e3dba32012-12-16 21:52:45 +0000981
982 /* zero timers for other contexts on the same subnet, so they don't timeout
983 independently */
984 for (context = context->next; context; context = context->next)
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000985 if (prefix <= context->prefix &&
986 is_same_net6(local, &context->start6, context->prefix) &&
987 is_same_net6(local, &context->end6, context->prefix))
Simon Kelley6e3dba32012-12-16 21:52:45 +0000988 context->ra_time = 0;
989
990 return 0; /* found, abort */
991 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000992
993 return 1; /* keep searching */
994}
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100995
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100996static void new_timeout(struct dhcp_context *context, char *iface_name, time_t now)
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100997{
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100998 if (difftime(now, context->ra_short_period_start) < 60.0)
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100999 /* range 5 - 20 */
1000 context->ra_time = now + 5 + (rand16()/4400);
1001 else
Simon Kelleyc4cd95d2013-10-10 20:58:11 +01001002 {
1003 /* range 3/4 - 1 times MaxRtrAdvInterval */
1004 unsigned int adv_interval = calc_interval(find_iface_param(iface_name));
1005 context->ra_time = now + (3 * adv_interval)/4 + ((adv_interval * (unsigned int)rand16()) >> 18);
1006 }
1007}
1008
1009static struct ra_interface *find_iface_param(char *iface)
1010{
1011 struct ra_interface *ra;
1012
1013 for (ra = daemon->ra_interfaces; ra; ra = ra->next)
1014 if (wildcard_match(ra->name, iface))
1015 return ra;
1016
1017 return NULL;
1018}
1019
1020static unsigned int calc_interval(struct ra_interface *ra)
1021{
Tarun Kundu74fb2b62022-07-06 09:11:18 -07001022 int interval = DEFAULT_RTR_ADV_INTERVAL;
1023 unsigned int min_rtr_interval = ra->min_ra_adv_interval;
1024 unsigned int max_rtr_interval = ra->max_ra_adv_interval;
1025
1026 if (max_rtr_interval > MAX_RTR_ADV_INTERVAL_UPPER_BOUND)
1027 max_rtr_interval = MAX_RTR_ADV_INTERVAL_UPPER_BOUND;
1028 else if (max_rtr_interval < MAX_RTR_ADV_INTERVAL_LOWER_BOUND)
1029 max_rtr_interval = MAX_RTR_ADV_INTERVAL_LOWER_BOUND;
1030
1031 if (min_rtr_interval < MIN_RTR_ADV_INTERVAL)
1032 min_rtr_interval = MIN_RTR_ADV_INTERVAL;
1033
1034 interval = (rand() % (max_rtr_interval - min_rtr_interval + 1)) + min_rtr_interval;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +01001035
Tarun Kundu74fb2b62022-07-06 09:11:18 -07001036 if (((ra->ra_sent_count + 1) < MAX_INITIAL_RTR_ADVERTISEMENTS) &&
1037 interval > MAX_INITIAL_RTR_ADVERT_INTERVAL)
1038 interval = random_num_in_range(MAX_INITIAL_RTR_ADVERT_INTERVAL);
Simon Kelleyc4cd95d2013-10-10 20:58:11 +01001039 return (unsigned int)interval;
1040}
1041
1042static unsigned int calc_lifetime(struct ra_interface *ra)
1043{
1044 int lifetime, interval = (int)calc_interval(ra);
1045
1046 if (!ra || ra->lifetime == -1) /* not specified */
1047 lifetime = 3 * interval;
1048 else
1049 {
1050 lifetime = ra->lifetime;
1051 if (lifetime < interval && lifetime != 0)
1052 lifetime = interval;
1053 else if (lifetime > 9000)
1054 lifetime = 9000;
1055 }
1056
1057 return (unsigned int)lifetime;
1058}
1059
1060static unsigned int calc_prio(struct ra_interface *ra)
1061{
1062 if (ra)
1063 return ra->prio;
1064
1065 return 0;
Simon Kelleyef1a94a2013-07-26 13:59:03 +01001066}
Simon Kelley801ca9a2012-03-06 19:30:17 +00001067
Tarun Kundu024baf12022-06-28 10:56:47 -07001068static unsigned char calc_hop_limit(struct ra_interface *ra)
1069{
1070 if (ra) {
1071 return ra->hop_limit;
1072 }
1073 return (unsigned char)hop_limit;
1074}
1075
Tarun Kundu41aa4882022-07-05 10:44:13 -07001076static unsigned int calc_retrans_time(struct ra_interface *ra)
1077{
1078 if (ra) {
1079 return ra->retrans_time;
1080 }
1081 return 0;
1082}
1083
Tarun Kundu024baf12022-06-28 10:56:47 -07001084static unsigned int calc_reachable_time(struct ra_interface *ra)
1085{
1086 if (ra) {
1087 return ra->reachable_time;
1088 }
1089 return 0;
1090}
Simon Kelleyc5ad4e72012-02-24 16:06:20 +00001091#endif