blob: 13e0afcc2a99d09206caacca73f8b012a4adb9dc [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);
Tarun Kundu01293a72022-07-18 23:50:36 -070071int64_t timespecdiff(struct timespec const *a, struct timespec const *b);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000072
Simon Kelleyc5379c12012-02-24 20:05:52 +000073static int hop_limit;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000074
75void ra_init(time_t now)
76{
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000077 struct icmp6_filter filter;
78 int fd;
Simon Kelley0e88d532012-03-28 22:22:05 +010079#if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000080 int class = IPTOS_CLASS_CS6;
81#endif
82 int val = 255; /* radvd uses this value */
Tarun Kundu024baf12022-06-28 10:56:47 -070083 socklen_t len = sizeof(hop_limit);
Simon Kelley353ae4d2012-03-19 20:07:51 +000084 struct dhcp_context *context;
85
Simon Kelley843c96b2012-02-27 17:42:38 +000086 /* ensure this is around even if we're not doing DHCPv6 */
87 expand_buf(&daemon->outpacket, sizeof(struct dhcp_packet));
Simon Kelley353ae4d2012-03-19 20:07:51 +000088
Josh Soref730c6742017-02-06 16:14:04 +000089 /* See if we're guessing SLAAC addresses, if so we need to receive ping replies */
Simon Kelley1f776932012-12-16 19:46:08 +000090 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley353ae4d2012-03-19 20:07:51 +000091 if ((context->flags & CONTEXT_RA_NAME))
92 break;
93
Simon Kelley89500e32013-09-20 16:29:20 +010094 /* Need ICMP6 socket for transmission for DHCPv6 even when not doing RA. */
95
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000096 ICMP6_FILTER_SETBLOCKALL(&filter);
Simon Kelley89500e32013-09-20 16:29:20 +010097 if (daemon->doing_ra)
98 {
99 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);
100 if (context)
101 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filter);
102 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000103
104 if ((fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1 ||
Simon Kelleyc5379c12012-02-24 20:05:52 +0000105 getsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hop_limit, &len) ||
Simon Kelley0e88d532012-03-28 22:22:05 +0100106#if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000107 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &class, sizeof(class)) == -1 ||
108#endif
109 !fix_fd(fd) ||
110 !set_ipv6pktinfo(fd) ||
111 setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val)) ||
112 setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val)) ||
113 setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) == -1)
114 die (_("cannot create ICMPv6 socket: %s"), NULL, EC_BADNET);
115
116 daemon->icmp6fd = fd;
117
Simon Kelley89500e32013-09-20 16:29:20 +0100118 if (daemon->doing_ra)
Josh Soref730c6742017-02-06 16:14:04 +0000119 ra_start_unsolicited(now, NULL);
Tarun Kundu15048b52022-07-05 12:16:58 -0700120
121 /* Intializes random number generator */
122 srand(time(0));
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000123}
124
Josh Soref730c6742017-02-06 16:14:04 +0000125void ra_start_unsolicited(time_t now, struct dhcp_context *context)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000126{
Simon Kelley353ae4d2012-03-19 20:07:51 +0000127 /* 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 +0000128 if it's not appropriate to advertise those contexts.
129 This gets re-called on a netlink route-change to re-do the advertisement
130 and pick up new interfaces */
Simon Kelley6e3dba32012-12-16 21:52:45 +0000131
Simon Kelley353ae4d2012-03-19 20:07:51 +0000132 if (context)
Simon Kelley1b75c1e2012-12-18 19:55:25 +0000133 context->ra_short_period_start = context->ra_time = now;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000134 else
Simon Kelley1f776932012-12-16 19:46:08 +0000135 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley6e3dba32012-12-16 21:52:45 +0000136 if (!(context->flags & CONTEXT_TEMPLATE))
Simon Kelley1b75c1e2012-12-18 19:55:25 +0000137 {
138 context->ra_time = now + (rand16()/13000); /* range 0 - 5 */
139 /* re-do frequently for a minute or so, in case the first gets lost. */
140 context->ra_short_period_start = now;
141 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000142}
143
Simon Kelley1f776932012-12-16 19:46:08 +0000144void icmp6_packet(time_t now)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000145{
146 char interface[IF_NAMESIZE+1];
147 ssize_t sz;
148 int if_index = 0;
149 struct cmsghdr *cmptr;
150 struct msghdr msg;
151 union {
152 struct cmsghdr align; /* this ensures alignment */
153 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
154 } control_u;
155 struct sockaddr_in6 from;
Simon Kelley5ef33272012-03-30 15:10:28 +0100156 unsigned char *packet;
Tarun Kundu01293a72022-07-18 23:50:36 -0700157 struct iname *tmp, *if_tmp;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000158
159 /* Note: use outpacket for input buffer */
160 msg.msg_control = control_u.control6;
161 msg.msg_controllen = sizeof(control_u);
162 msg.msg_flags = 0;
163 msg.msg_name = &from;
164 msg.msg_namelen = sizeof(from);
165 msg.msg_iov = &daemon->outpacket;
166 msg.msg_iovlen = 1;
167
168 if ((sz = recv_dhcp_packet(daemon->icmp6fd, &msg)) == -1 || sz < 8)
169 return;
Simon Kelley5ef33272012-03-30 15:10:28 +0100170
171 packet = (unsigned char *)daemon->outpacket.iov_base;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000172
173 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
174 if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
175 {
176 union {
177 unsigned char *c;
178 struct in6_pktinfo *p;
179 } p;
180 p.c = CMSG_DATA(cmptr);
181
182 if_index = p.p->ipi6_ifindex;
183 }
184
185 if (!indextoname(daemon->icmp6fd, if_index, interface))
186 return;
187
Simon Kelley4f7b3042012-11-28 21:27:02 +0000188 if (!iface_check(AF_LOCAL, NULL, interface, NULL))
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000189 return;
190
191 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
Simon Kelley49333cb2013-03-15 20:30:51 +0000192 if (tmp->name && wildcard_match(tmp->name, interface))
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000193 return;
194
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100195 if (packet[1] != 0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000196 return;
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100197
Simon Kelley5ef33272012-03-30 15:10:28 +0100198 if (packet[0] == ICMP6_ECHO_REPLY)
199 lease_ping_reply(&from.sin6_addr, packet, interface);
200 else if (packet[0] == ND_ROUTER_SOLICIT)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000201 {
Simon Kelley5ef33272012-03-30 15:10:28 +0100202 char *mac = "";
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100203 struct dhcp_bridge *bridge, *alias;
Tarun Kundu01293a72022-07-18 23:50:36 -0700204 struct timespec ts, ts_delay;
Tarun Kundu15048b52022-07-05 12:16:58 -0700205 int tms = 0;
Simon Kelley5ef33272012-03-30 15:10:28 +0100206
207 /* look for link-layer address option for logging */
208 if (sz >= 16 && packet[8] == ICMP6_OPT_SOURCE_MAC && (packet[9] * 8) + 8 <= sz)
209 {
Simon Kelley24036ea2017-09-25 18:47:15 +0100210 if ((packet[9] * 8 - 2) * 3 - 1 >= MAXDNAME) {
211 return;
212 }
Simon Kelley5ef33272012-03-30 15:10:28 +0100213 print_mac(daemon->namebuff, &packet[10], (packet[9] * 8) - 2);
214 mac = daemon->namebuff;
215 }
216
Kevin Darbyshire-Bryant8c0b73d2013-10-11 11:56:33 +0100217 if (!option_bool(OPT_QUIET_RA))
218 my_syslog(MS_DHCP | LOG_INFO, "RTR-SOLICIT(%s) %s", interface, mac);
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100219
Tarun Kundu01293a72022-07-18 23:50:36 -0700220 for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next)
221 {
222 if ((strcmp(if_tmp->name, interface) == 0)) {
223 clock_gettime(CLOCK_MONOTONIC, &ts);
224 if (timespecdiff(&ts, &if_tmp->last_ra_for_rtr_sol) / 1000.0 < MIN_DELAY_BETWEEN_RAS) {
225 /* last RA for RTR Solicit was sent only a few moments ago,
226 don't send another immediately. */
227 ts_delay.tv_sec = MIN_DELAY_BETWEEN_RAS - ts.tv_sec + if_tmp->last_ra_for_rtr_sol.tv_sec;
228 ts_delay.tv_nsec = 0;
229 nanosleep(&ts_delay, NULL);
230 }
231 clock_gettime(CLOCK_MONOTONIC, &if_tmp->last_ra_for_rtr_sol);
232 }
233 }
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100234 /* If the incoming interface is an alias of some other one (as
Neil Jerram4918bd52015-06-10 22:23:20 +0100235 specified by the --bridge-interface option), send an RA using
236 the context of the aliased interface. */
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100237 for (bridge = daemon->bridges; bridge; bridge = bridge->next)
238 {
239 int bridge_index = if_nametoindex(bridge->iface);
240 if (bridge_index)
241 {
242 for (alias = bridge->alias; alias; alias = alias->next)
243 if (wildcard_matchn(alias->iface, interface, IF_NAMESIZE))
244 {
245 /* Send an RA on if_index with information from
246 bridge_index. */
247 send_ra_alias(now, bridge_index, bridge->iface, NULL, if_index);
248 break;
249 }
250 if (alias)
251 break;
252 }
253 }
254
255 /* If the incoming interface wasn't an alias, send an RA using
256 the context of the incoming interface. */
257 if (!bridge)
258 /* source address may not be valid in solicit request. */
259 send_ra(now, if_index, interface, !IN6_IS_ADDR_UNSPECIFIED(&from.sin6_addr) ? &from.sin6_addr : NULL);
Tarun Kundu15048b52022-07-05 12:16:58 -0700260 tms = random_num_in_range(MAX_RA_DELAY_TIME);
261 ts.tv_sec = 0;
262 ts.tv_nsec = tms * 1000000;
263 nanosleep(&ts, NULL);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000264 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000265}
266
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100267static 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 +0000268{
269 struct ra_packet *ra;
270 struct ra_param parm;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000271 struct sockaddr_in6 addr;
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100272 struct dhcp_context *context, *tmp, **up;
Simon Kelley18f0fb02012-03-31 21:18:55 +0100273 struct dhcp_netid iface_id;
274 struct dhcp_opt *opt_cfg;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100275 struct ra_interface *ra_param = find_iface_param(iface_name);
David Flamand005c46d2017-04-11 11:49:54 +0100276 int done_dns = 0, old_prefix = 0, mtu = 0;
Simon Kelleyc3a04082014-01-11 22:18:19 +0000277 unsigned int min_pref_time;
Simon Kelley3b436462012-12-28 11:55:45 +0000278#ifdef HAVE_LINUX_NETWORK
279 FILE *f;
280#endif
Tarun Kundu024baf12022-06-28 10:56:47 -0700281
282 struct iname *if_tmp;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000283 parm.ind = iface;
284 parm.managed = 0;
Simon Kelley30cd9662012-03-25 20:44:38 +0100285 parm.other = 0;
S La18bf312016-02-12 17:36:20 +0000286 parm.found_context = NULL;
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100287 parm.adv_router = 0;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000288 parm.if_name = iface_name;
289 parm.first = 1;
Simon Kelley1f776932012-12-16 19:46:08 +0000290 parm.now = now;
Simon Kelleyc3a04082014-01-11 22:18:19 +0000291 parm.glob_pref_time = parm.link_pref_time = parm.ula_pref_time = 0;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100292 parm.adv_interval = calc_interval(ra_param);
Ilya Ponetaev5bf50af2014-09-09 12:46:21 +0100293 parm.prio = calc_prio(ra_param);
Simon Kelley1f776932012-12-16 19:46:08 +0000294
Simon Kelleyfa785732016-07-22 20:56:01 +0100295 reset_counter();
Simon Kelleyce7845b2016-07-06 21:42:27 +0100296
297 if (!(ra = expand(sizeof(struct ra_packet))))
298 return;
Ilya Ponetaev5bf50af2014-09-09 12:46:21 +0100299
300 ra->type = ND_ROUTER_ADVERT;
301 ra->code = 0;
Ilya Ponetaev5bf50af2014-09-09 12:46:21 +0100302 ra->flags = parm.prio;
303 ra->lifetime = htons(calc_lifetime(ra_param));
Tarun Kundu024baf12022-06-28 10:56:47 -0700304 ra->reachable_time = htonl(calc_reachable_time(ra_param));
305 ra->hop_limit = calc_hop_limit(ra_param);
Tarun Kundu41aa4882022-07-05 10:44:13 -0700306 ra->retrans_time = htonl(calc_retrans_time(ra_param));
Ilya Ponetaev5bf50af2014-09-09 12:46:21 +0100307
Simon Kelley18f0fb02012-03-31 21:18:55 +0100308 /* set tag with name == interface */
309 iface_id.net = iface_name;
310 iface_id.next = NULL;
311 parm.tags = &iface_id;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000312
Tarun Kundu74fb2b62022-07-06 09:11:18 -0700313 for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next)
314 {
315 if ((strcmp(if_tmp->name, iface_name) == 0) && (if_tmp->is_ra_adv_dis == true)) {
316 if (if_tmp->final_ra_sent_count < MAX_FINAL_RTR_ADVERTISEMENTS) {
317 ra->lifetime = htons(0);
318 if_tmp->final_ra_sent_count++;
319 break;
320 }
321 else {
322 return;
323 }
324 }
325 }
326
Simon Kelley1f776932012-12-16 19:46:08 +0000327 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley18f0fb02012-03-31 21:18:55 +0100328 {
329 context->flags &= ~CONTEXT_RA_DONE;
330 context->netid.next = &context->netid;
331 }
332
Simon Kelley378fa562020-01-05 17:23:19 +0000333 /* If no link-local address then we can't advertise since source address of
334 advertisement must be link local address: RFC 4861 para 6.1.2. */
335 if (!iface_enumerate(AF_INET6, &parm, add_prefixes) ||
336 parm.link_pref_time == 0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000337 return;
338
Simon Kelleyc3a04082014-01-11 22:18:19 +0000339 /* Find smallest preferred time within address classes,
340 to use as lifetime for options. This is a rather arbitrary choice. */
341 min_pref_time = 0xffffffff;
342 if (parm.glob_pref_time != 0 && parm.glob_pref_time < min_pref_time)
343 min_pref_time = parm.glob_pref_time;
344
345 if (parm.ula_pref_time != 0 && parm.ula_pref_time < min_pref_time)
346 min_pref_time = parm.ula_pref_time;
347
348 if (parm.link_pref_time != 0 && parm.link_pref_time < min_pref_time)
349 min_pref_time = parm.link_pref_time;
350
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100351 /* Look for constructed contexts associated with addresses which have gone,
352 and advertise them with preferred_time == 0 RFC 6204 4.3 L-13 */
353 for (up = &daemon->dhcp6, context = daemon->dhcp6; context; context = tmp)
354 {
355 tmp = context->next;
Simon Kelley3b436462012-12-28 11:55:45 +0000356
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100357 if (context->if_index == iface && (context->flags & CONTEXT_OLD))
358 {
359 unsigned int old = difftime(now, context->address_lost_time);
360
361 if (old > context->saved_valid)
S La18bf312016-02-12 17:36:20 +0000362 {
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100363 /* We've advertised this enough, time to go */
S La18bf312016-02-12 17:36:20 +0000364
365 /* If this context held the timeout, and there's another context in use
366 transfer the timeout there. */
367 if (context->ra_time != 0 && parm.found_context && parm.found_context->ra_time == 0)
368 new_timeout(parm.found_context, iface_name, now);
369
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100370 *up = context->next;
371 free(context);
372 }
373 else
374 {
375 struct prefix_opt *opt;
376 struct in6_addr local = context->start6;
377 int do_slaac = 0;
378
Simon Kelley44522922013-11-15 14:45:04 +0000379 old_prefix = 1;
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100380
381 /* zero net part of address */
382 setaddr6part(&local, addr6part(&local) & ~((context->prefix == 64) ? (u64)-1LL : (1LLU << (128 - context->prefix)) - 1LLU));
383
Simon Kelley6ea1f232013-12-19 15:45:12 +0000384
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100385 if (context->flags & CONTEXT_RA)
Simon Kelley6ea1f232013-12-19 15:45:12 +0000386 {
387 do_slaac = 1;
388 if (context->flags & CONTEXT_DHCP)
389 {
390 parm.other = 1;
391 if (!(context->flags & CONTEXT_RA_STATELESS))
392 parm.managed = 1;
393 }
394 }
395 else
396 {
397 /* don't do RA for non-ra-only unless --enable-ra is set */
398 if (option_bool(OPT_RA))
399 {
400 parm.managed = 1;
401 parm.other = 1;
402 }
403 }
404
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100405 if ((opt = expand(sizeof(struct prefix_opt))))
406 {
407 opt->type = ICMP6_OPT_PREFIX;
408 opt->len = 4;
409 opt->prefix_len = context->prefix;
Neil Jerram2fd5bc92015-06-10 22:13:06 +0100410 /* autonomous only if we're not doing dhcp, set
411 "on-link" unless "off-link" was specified */
412 opt->flags = (do_slaac ? 0x40 : 0) |
413 ((context->flags & CONTEXT_RA_OFF_LINK) ? 0 : 0x80);
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100414 opt->valid_lifetime = htonl(context->saved_valid - old);
415 opt->preferred_lifetime = htonl(0);
416 opt->reserved = 0;
417 opt->prefix = local;
418
419 inet_ntop(AF_INET6, &local, daemon->addrbuff, ADDRSTRLEN);
Kevin Darbyshire-Bryant8c0b73d2013-10-11 11:56:33 +0100420 if (!option_bool(OPT_QUIET_RA))
421 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s old prefix", iface_name, daemon->addrbuff);
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100422 }
423
424 up = &context->next;
425 }
426 }
427 else
428 up = &context->next;
429 }
430
Simon Kelley44522922013-11-15 14:45:04 +0000431 /* If we're advertising only old prefixes, set router lifetime to zero. */
432 if (old_prefix && !parm.found_context)
433 ra->lifetime = htons(0);
434
435 /* No prefixes to advertise. */
436 if (!old_prefix && !parm.found_context)
437 return;
438
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100439 /* If we're sending router address instead of prefix in at least on prefix,
440 include the advertisement interval option. */
441 if (parm.adv_router)
442 {
443 put_opt6_char(ICMP6_OPT_ADV_INTERVAL);
444 put_opt6_char(1);
445 put_opt6_short(0);
446 /* interval value is in milliseconds */
447 put_opt6_long(1000 * calc_interval(find_iface_param(iface_name)));
448 }
449
David Flamand005c46d2017-04-11 11:49:54 +0100450 /* Set the MTU from ra_param if any, an MTU of 0 mean automatic for linux, */
451 /* an MTU of -1 prevents the option from being sent. */
452 if (ra_param)
453 mtu = ra_param->mtu;
Simon Kelley3b436462012-12-28 11:55:45 +0000454#ifdef HAVE_LINUX_NETWORK
Ville Skyttäfaaf3062018-01-14 17:32:52 +0000455 /* Note that IPv6 MTU is not necessarily the same as the IPv4 MTU
Simon Kelley3b436462012-12-28 11:55:45 +0000456 available from SIOCGIFMTU */
David Flamand005c46d2017-04-11 11:49:54 +0100457 if (mtu == 0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000458 {
Vladislav Grishenko6ec5f5c2017-04-24 22:34:45 +0100459 char *mtu_name = ra_param ? ra_param->mtu_name : NULL;
Simon Kelley5ed82ae2019-03-05 16:38:34 +0000460 sprintf(daemon->namebuff, "/proc/sys/net/ipv6/conf/%s/mtu", mtu_name ? mtu_name : iface_name);
David Flamand005c46d2017-04-11 11:49:54 +0100461 if ((f = fopen(daemon->namebuff, "r")))
462 {
463 if (fgets(daemon->namebuff, MAXDNAME, f))
464 mtu = atoi(daemon->namebuff);
465 fclose(f);
466 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000467 }
Simon Kelley3b436462012-12-28 11:55:45 +0000468#endif
David Flamand005c46d2017-04-11 11:49:54 +0100469 if (mtu > 0)
470 {
471 put_opt6_char(ICMP6_OPT_MTU);
472 put_opt6_char(1);
473 put_opt6_short(0);
474 put_opt6_long(mtu);
475 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000476
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100477 iface_enumerate(AF_LOCAL, &send_iface, add_lla);
Simon Kelley18f0fb02012-03-31 21:18:55 +0100478
479 /* RDNSS, RFC 6106, use relevant DHCP6 options */
480 (void)option_filter(parm.tags, NULL, daemon->dhcp_opts6);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000481
Simon Kelley18f0fb02012-03-31 21:18:55 +0100482 for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next)
483 {
484 int i;
485
486 /* netids match and not encapsulated? */
487 if (!(opt_cfg->flags & DHOPT_TAGOK))
488 continue;
489
490 if (opt_cfg->opt == OPTION6_DNS_SERVER)
491 {
Simon Kelleyc3a04082014-01-11 22:18:19 +0000492 struct in6_addr *a;
493 int len;
494
Simon Kelley18f0fb02012-03-31 21:18:55 +0100495 done_dns = 1;
Simon Kelleyc3a04082014-01-11 22:18:19 +0000496
497 if (opt_cfg->len == 0)
Simon Kelley806cf782013-10-14 14:08:44 +0100498 continue;
Simon Kelley18f0fb02012-03-31 21:18:55 +0100499
Simon Kelleyc3a04082014-01-11 22:18:19 +0000500 /* reduce len for any addresses we can't substitute */
501 for (a = (struct in6_addr *)opt_cfg->val, len = opt_cfg->len, i = 0;
502 i < opt_cfg->len; i += IN6ADDRSZ, a++)
503 if ((IN6_IS_ADDR_UNSPECIFIED(a) && parm.glob_pref_time == 0) ||
504 (IN6_IS_ADDR_ULA_ZERO(a) && parm.ula_pref_time == 0) ||
505 (IN6_IS_ADDR_LINK_LOCAL_ZERO(a) && parm.link_pref_time == 0))
506 len -= IN6ADDRSZ;
507
508 if (len != 0)
509 {
510 put_opt6_char(ICMP6_OPT_RDNSS);
511 put_opt6_char((len/8) + 1);
512 put_opt6_short(0);
513 put_opt6_long(min_pref_time);
514
515 for (a = (struct in6_addr *)opt_cfg->val, i = 0; i < opt_cfg->len; i += IN6ADDRSZ, a++)
516 if (IN6_IS_ADDR_UNSPECIFIED(a))
517 {
518 if (parm.glob_pref_time != 0)
519 put_opt6(&parm.link_global, IN6ADDRSZ);
520 }
521 else if (IN6_IS_ADDR_ULA_ZERO(a))
522 {
523 if (parm.ula_pref_time != 0)
524 put_opt6(&parm.ula, IN6ADDRSZ);
525 }
526 else if (IN6_IS_ADDR_LINK_LOCAL_ZERO(a))
527 {
528 if (parm.link_pref_time != 0)
529 put_opt6(&parm.link_local, IN6ADDRSZ);
530 }
531 else
532 put_opt6(a, IN6ADDRSZ);
533 }
Simon Kelley18f0fb02012-03-31 21:18:55 +0100534 }
535
536 if (opt_cfg->opt == OPTION6_DOMAIN_SEARCH && opt_cfg->len != 0)
537 {
538 int len = ((opt_cfg->len+7)/8);
539
540 put_opt6_char(ICMP6_OPT_DNSSL);
541 put_opt6_char(len + 1);
542 put_opt6_short(0);
Simon Kelleyc3a04082014-01-11 22:18:19 +0000543 put_opt6_long(min_pref_time);
Simon Kelley18f0fb02012-03-31 21:18:55 +0100544 put_opt6(opt_cfg->val, opt_cfg->len);
545
546 /* pad */
547 for (i = opt_cfg->len; i < len * 8; i++)
548 put_opt6_char(0);
549 }
550 }
551
Simon Kelleyc3a04082014-01-11 22:18:19 +0000552 if (daemon->port == NAMESERVER_PORT && !done_dns && parm.link_pref_time != 0)
Simon Kelley18f0fb02012-03-31 21:18:55 +0100553 {
Simon Kelleyab915f82013-04-30 10:41:28 +0100554 /* default == us, as long as we are supplying DNS service. */
Simon Kelley18f0fb02012-03-31 21:18:55 +0100555 put_opt6_char(ICMP6_OPT_RDNSS);
556 put_opt6_char(3);
557 put_opt6_short(0);
Simon Kelleyc3a04082014-01-11 22:18:19 +0000558 put_opt6_long(min_pref_time);
Simon Kelley806cf782013-10-14 14:08:44 +0100559 put_opt6(&parm.link_local, IN6ADDRSZ);
Simon Kelley18f0fb02012-03-31 21:18:55 +0100560 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000561
562 /* set managed bits unless we're providing only RA on this link */
563 if (parm.managed)
Simon Kelley30cd9662012-03-25 20:44:38 +0100564 ra->flags |= 0x80; /* M flag, managed, */
565 if (parm.other)
566 ra->flags |= 0x40; /* O flag, other */
567
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000568 /* decide where we're sending */
569 memset(&addr, 0, sizeof(addr));
Simon Kelley22d904d2012-02-26 20:13:45 +0000570#ifdef HAVE_SOCKADDR_SA_LEN
571 addr.sin6_len = sizeof(struct sockaddr_in6);
572#endif
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000573 addr.sin6_family = AF_INET6;
574 addr.sin6_port = htons(IPPROTO_ICMPV6);
575 if (dest)
576 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000577 addr.sin6_addr = *dest;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000578 if (IN6_IS_ADDR_LINKLOCAL(dest) ||
579 IN6_IS_ADDR_MC_LINKLOCAL(dest))
580 addr.sin6_scope_id = iface;
581 }
582 else
Simon Kelley8f3194f2013-09-30 15:04:58 +0100583 {
584 inet_pton(AF_INET6, ALL_NODES, &addr.sin6_addr);
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100585 setsockopt(daemon->icmp6fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &send_iface, sizeof(send_iface));
Simon Kelley8f3194f2013-09-30 15:04:58 +0100586 }
Simon Kelleyff841eb2015-03-11 21:36:30 +0000587 while (retry_send(sendto(daemon->icmp6fd, daemon->outpacket.iov_base,
Simon Kelley6b1c4642016-07-22 20:59:16 +0100588 save_counter(-1), 0, (struct sockaddr *)&addr,
Simon Kelleyff841eb2015-03-11 21:36:30 +0000589 sizeof(addr))));
Tarun Kundu74fb2b62022-07-06 09:11:18 -0700590 ra_param->ra_sent_count++;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000591}
592
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100593static void send_ra(time_t now, int iface, char *iface_name, struct in6_addr *dest)
594{
595 /* Send an RA on the same interface that the RA content is based
596 on. */
597 send_ra_alias(now, iface, iface_name, dest, iface);
598}
599
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000600static int add_prefixes(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +0000601 int scope, int if_index, int flags,
Simon Kelley55b42f62012-12-21 16:53:15 +0000602 unsigned int preferred, unsigned int valid, void *vparam)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000603{
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000604 struct ra_param *param = vparam;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000605
606 (void)scope; /* warning */
Tarun Kundu024baf12022-06-28 10:56:47 -0700607
608 struct iname *if_tmp;
609 for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next)
610 {
611 if ((strcmp(if_tmp->name, param->if_name) == 0) && (if_tmp->is_ra_adv_dis == true)) {
Tarun Kundu74fb2b62022-07-06 09:11:18 -0700612 if (if_tmp->final_ra_sent_count < MAX_FINAL_RTR_ADVERTISEMENTS) {
613 break;
614 }
615 else {
616 return 1;
617 }
Tarun Kundu024baf12022-06-28 10:56:47 -0700618 }
619 }
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000620
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000621 if (if_index == param->ind)
622 {
623 if (IN6_IS_ADDR_LINKLOCAL(local))
Simon Kelleyc3a04082014-01-11 22:18:19 +0000624 {
625 /* Can there be more than one LL address?
626 Select the one with the longest preferred time
627 if there is. */
628 if (preferred > param->link_pref_time)
629 {
630 param->link_pref_time = preferred;
631 param->link_local = *local;
632 }
633 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000634 else if (!IN6_IS_ADDR_LOOPBACK(local) &&
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000635 !IN6_IS_ADDR_MULTICAST(local))
636 {
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000637 int real_prefix = 0;
Simon Kelleyc8257542012-03-28 21:15:41 +0100638 int do_slaac = 0;
639 int deprecate = 0;
Simon Kelley3bc0d932012-12-28 11:31:44 +0000640 int constructed = 0;
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100641 int adv_router = 0;
Neil Jerram2fd5bc92015-06-10 22:13:06 +0100642 int off_link = 0;
Kyle Swenson9d238072021-12-01 08:13:17 -0700643 unsigned int valid_lifetime = 0xffffffff;
Simon Kelleyc8257542012-03-28 21:15:41 +0100644 unsigned int time = 0xffffffff;
Simon Kelley1e02a852012-03-29 11:07:25 +0100645 struct dhcp_context *context;
646
Simon Kelley1f776932012-12-16 19:46:08 +0000647 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100648 if (!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) &&
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000649 prefix <= context->prefix &&
650 is_same_net6(local, &context->start6, context->prefix) &&
651 is_same_net6(local, &context->end6, context->prefix))
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000652 {
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100653 context->saved_valid = valid;
654
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100655 if (context->flags & CONTEXT_RA)
Simon Kelley30cd9662012-03-25 20:44:38 +0100656 {
657 do_slaac = 1;
Simon Kelley4723d492012-03-30 21:04:17 +0100658 if (context->flags & CONTEXT_DHCP)
Simon Kelley05e92e52012-03-30 22:24:15 +0100659 {
660 param->other = 1;
661 if (!(context->flags & CONTEXT_RA_STATELESS))
662 param->managed = 1;
663 }
Simon Kelley30cd9662012-03-25 20:44:38 +0100664 }
Simon Kelley884a6df2012-03-20 16:20:22 +0000665 else
Simon Kelley0010b472012-02-29 12:18:30 +0000666 {
667 /* don't do RA for non-ra-only unless --enable-ra is set */
668 if (!option_bool(OPT_RA))
669 continue;
670 param->managed = 1;
Simon Kelley30cd9662012-03-25 20:44:38 +0100671 param->other = 1;
Simon Kelley0010b472012-02-29 12:18:30 +0000672 }
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100673
674 /* Configured to advertise router address, not prefix. See RFC 3775 7.2
675 In this case we do all addresses associated with a context,
676 hence the real_prefix setting here. */
677 if (context->flags & CONTEXT_RA_ROUTER)
678 {
679 adv_router = 1;
680 param->adv_router = 1;
681 real_prefix = context->prefix;
682 }
683
Simon Kelley4d85e402020-07-12 22:45:46 +0100684 /* find floor time, don't reduce below 3 * RA interval.
685 If the lease time has been left as default, don't
686 use that as a floor. */
687 if ((context->flags & CONTEXT_SETLEASE) &&
688 time > context->lease_time)
Simon Kelley7f035f52012-12-22 21:27:08 +0000689 {
Kyle Swenson9d238072021-12-01 08:13:17 -0700690
691 if ((context->flags & CONTEXT_SETVALID))
692 valid_lifetime = context->valid_lifetime;
693 else
694 valid_lifetime = context->lease_time;
695
Simon Kelley7f035f52012-12-22 21:27:08 +0000696 time = context->lease_time;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100697 if (time < ((unsigned int)(3 * param->adv_interval)))
698 time = 3 * param->adv_interval;
Simon Kelley7f035f52012-12-22 21:27:08 +0000699 }
700
Simon Kelleyc8257542012-03-28 21:15:41 +0100701 if (context->flags & CONTEXT_DEPRECATE)
702 deprecate = 1;
Simon Kelley3bc0d932012-12-28 11:31:44 +0000703
704 if (context->flags & CONTEXT_CONSTRUCTED)
705 constructed = 1;
706
Simon Kelleyc8257542012-03-28 21:15:41 +0100707
Simon Kelley18f0fb02012-03-31 21:18:55 +0100708 /* collect dhcp-range tags */
709 if (context->netid.next == &context->netid && context->netid.net)
710 {
711 context->netid.next = param->tags;
712 param->tags = &context->netid;
713 }
714
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100715 /* subsequent prefixes on the same interface
716 and subsequent instances of this prefix don't need timers.
717 Be careful not to find the same prefix twice with different
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100718 addresses unless we're advertising the actual addresses. */
Simon Kelley1e02a852012-03-29 11:07:25 +0100719 if (!(context->flags & CONTEXT_RA_DONE))
720 {
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100721 if (!param->first)
722 context->ra_time = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100723 context->flags |= CONTEXT_RA_DONE;
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000724 real_prefix = context->prefix;
Neil Jerram2fd5bc92015-06-10 22:13:06 +0100725 off_link = (context->flags & CONTEXT_RA_OFF_LINK);
Simon Kelley1e02a852012-03-29 11:07:25 +0100726 }
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100727
S La18bf312016-02-12 17:36:20 +0000728 param->first = 0;
729 /* found_context is the _last_ one we found, so if there's
730 more than one, it's not the first. */
731 param->found_context = context;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000732 }
Simon Kelley1f776932012-12-16 19:46:08 +0000733
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000734 /* configured time is ceiling */
Simon Kelley3bc0d932012-12-28 11:31:44 +0000735 if (!constructed || valid > time)
Kyle Swenson9d238072021-12-01 08:13:17 -0700736 valid = valid_lifetime;
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000737
Simon Kelley3bc0d932012-12-28 11:31:44 +0000738 if (flags & IFACE_DEPRECATED)
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000739 preferred = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100740
Simon Kelley3bc0d932012-12-28 11:31:44 +0000741 if (deprecate)
742 time = 0;
743
744 /* configured time is ceiling */
745 if (!constructed || preferred > time)
746 preferred = time;
Simon Kelleyc3a04082014-01-11 22:18:19 +0000747
748 if (IN6_IS_ADDR_ULA(local))
Simon Kelley55b42f62012-12-21 16:53:15 +0000749 {
Simon Kelleyc3a04082014-01-11 22:18:19 +0000750 if (preferred > param->ula_pref_time)
751 {
752 param->ula_pref_time = preferred;
753 param->ula = *local;
754 }
755 }
756 else
757 {
758 if (preferred > param->glob_pref_time)
759 {
760 param->glob_pref_time = preferred;
761 param->link_global = *local;
762 }
Simon Kelley55b42f62012-12-21 16:53:15 +0000763 }
764
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000765 if (real_prefix != 0)
Simon Kelleyc8257542012-03-28 21:15:41 +0100766 {
Simon Kelley1e02a852012-03-29 11:07:25 +0100767 struct prefix_opt *opt;
768
769 if ((opt = expand(sizeof(struct prefix_opt))))
770 {
Simon Kelley5ef33272012-03-30 15:10:28 +0100771 /* zero net part of address */
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100772 if (!adv_router)
773 setaddr6part(local, addr6part(local) & ~((real_prefix == 64) ? (u64)-1LL : (1LLU << (128 - real_prefix)) - 1LLU));
Simon Kelley5ef33272012-03-30 15:10:28 +0100774
Simon Kelley1e02a852012-03-29 11:07:25 +0100775 opt->type = ICMP6_OPT_PREFIX;
776 opt->len = 4;
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000777 opt->prefix_len = real_prefix;
Neil Jerram2fd5bc92015-06-10 22:13:06 +0100778 /* autonomous only if we're not doing dhcp, set
779 "on-link" unless "off-link" was specified */
780 opt->flags = (off_link ? 0 : 0x80);
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100781 if (do_slaac)
782 opt->flags |= 0x40;
783 if (adv_router)
784 opt->flags |= 0x20;
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000785 opt->valid_lifetime = htonl(valid);
786 opt->preferred_lifetime = htonl(preferred);
Simon Kelley5ef33272012-03-30 15:10:28 +0100787 opt->reserved = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100788 opt->prefix = *local;
Simon Kelley1e02a852012-03-29 11:07:25 +0100789
Simon Kelley5ef33272012-03-30 15:10:28 +0100790 inet_ntop(AF_INET6, local, daemon->addrbuff, ADDRSTRLEN);
Kevin Darbyshire-Bryant8c0b73d2013-10-11 11:56:33 +0100791 if (!option_bool(OPT_QUIET_RA))
792 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s", param->if_name, daemon->addrbuff);
Simon Kelley1e02a852012-03-29 11:07:25 +0100793 }
Simon Kelleyc8257542012-03-28 21:15:41 +0100794 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000795 }
796 }
797 return 1;
798}
799
800static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm)
801{
802 (void)type;
803
804 if (index == *((int *)parm))
805 {
806 /* size is in units of 8 octets and includes type and length (2 bytes)
807 add 7 to round up */
808 int len = (maclen + 9) >> 3;
809 unsigned char *p = expand(len << 3);
810 memset(p, 0, len << 3);
811 *p++ = ICMP6_OPT_SOURCE_MAC;
812 *p++ = len;
813 memcpy(p, mac, maclen);
814
815 return 0;
816 }
817
818 return 1;
819}
820
821time_t periodic_ra(time_t now)
822{
823 struct search_param param;
824 struct dhcp_context *context;
825 time_t next_event;
Neil Jerram9bad3392015-06-10 22:16:35 +0100826 struct alias_param aparam;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100827
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000828 param.now = now;
Simon Kelley29d28dd2012-12-03 14:05:59 +0000829 param.iface = 0;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000830
831 while (1)
832 {
833 /* find overdue events, and time of first future event */
Simon Kelley1f776932012-12-16 19:46:08 +0000834 for (next_event = 0, context = daemon->dhcp6; context; context = context->next)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000835 if (context->ra_time != 0)
836 {
Simon Kelley7dbe9812012-03-25 14:49:54 +0100837 if (difftime(context->ra_time, now) <= 0.0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000838 break; /* overdue */
839
Simon Kelley7dbe9812012-03-25 14:49:54 +0100840 if (next_event == 0 || difftime(next_event, context->ra_time) > 0.0)
841 next_event = context->ra_time;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000842 }
843
844 /* none overdue */
845 if (!context)
846 break;
847
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100848 if ((context->flags & CONTEXT_OLD) &&
849 context->if_index != 0 &&
Simon Kelleydd9d9ce2013-11-15 11:24:00 +0000850 indextoname(daemon->icmp6fd, context->if_index, param.name))
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100851 {
852 /* A context for an old address. We'll not find the interface by
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100853 looking for addresses, but we know it anyway, since the context is
854 constructed */
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100855 param.iface = context->if_index;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100856 new_timeout(context, param.name, now);
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100857 }
858 else if (iface_enumerate(AF_INET6, &param, iface_search))
859 /* There's a context overdue, but we can't find an interface
860 associated with it, because it's for a subnet we dont
861 have an interface on. Probably we're doing DHCP on
862 a remote subnet via a relay. Zero the timer, since we won't
Josh Soref730c6742017-02-06 16:14:04 +0000863 ever be able to send ra's and satisfy it. */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000864 context->ra_time = 0;
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100865
866 if (param.iface != 0 &&
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100867 iface_check(AF_LOCAL, NULL, param.name, NULL))
Simon Kelley421594f2012-12-02 12:17:35 +0000868 {
869 struct iname *tmp;
870 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100871 if (tmp->name && wildcard_match(tmp->name, param.name))
Simon Kelley421594f2012-12-02 12:17:35 +0000872 break;
873 if (!tmp)
Neil Jerram9bad3392015-06-10 22:16:35 +0100874 {
Tarun Kundu024baf12022-06-28 10:56:47 -0700875 struct iname *if_tmp;
876 for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next)
877 {
Tarun Kundu74fb2b62022-07-06 09:11:18 -0700878 if ((strcmp(if_tmp->name, param.name) == 0) &&
879 ((if_tmp->is_ra_adv_dis == false) ||
880 (if_tmp->final_ra_sent_count < MAX_FINAL_RTR_ADVERTISEMENTS))) {
Tarun Kundu024baf12022-06-28 10:56:47 -0700881 send_ra(now, param.iface, param.name, NULL);
Neil Jerram9bad3392015-06-10 22:16:35 +0100882
Tarun Kundu024baf12022-06-28 10:56:47 -0700883 /* Also send on all interfaces that are aliases of this
Neil Jerram9bad3392015-06-10 22:16:35 +0100884 one. */
Tarun Kundu024baf12022-06-28 10:56:47 -0700885 for (aparam.bridge = daemon->bridges;
Neil Jerram9bad3392015-06-10 22:16:35 +0100886 aparam.bridge;
887 aparam.bridge = aparam.bridge->next)
Tarun Kundu024baf12022-06-28 10:56:47 -0700888 if ((int)if_nametoindex(aparam.bridge->iface) == param.iface)
Neil Jerram9bad3392015-06-10 22:16:35 +0100889 {
890 /* Count the number of alias interfaces for this
891 'bridge', by calling iface_enumerate with
892 send_ra_to_aliases and NULL alias_ifs. */
893 aparam.iface = param.iface;
894 aparam.alias_ifs = NULL;
895 aparam.num_alias_ifs = 0;
896 iface_enumerate(AF_LOCAL, &aparam, send_ra_to_aliases);
897 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s => %d alias(es)",
898 param.name, daemon->addrbuff, aparam.num_alias_ifs);
899
900 /* Allocate memory to store the alias interface
901 indices. */
902 aparam.alias_ifs = (int *)whine_malloc(aparam.num_alias_ifs *
903 sizeof(int));
904 if (aparam.alias_ifs)
905 {
906 /* Use iface_enumerate again to get the alias
907 interface indices, then send on each of
908 those. */
909 aparam.max_alias_ifs = aparam.num_alias_ifs;
910 aparam.num_alias_ifs = 0;
911 iface_enumerate(AF_LOCAL, &aparam, send_ra_to_aliases);
912 for (; aparam.num_alias_ifs; aparam.num_alias_ifs--)
913 {
914 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s => i/f %d",
915 param.name, daemon->addrbuff,
916 aparam.alias_ifs[aparam.num_alias_ifs - 1]);
917 send_ra_alias(now,
918 param.iface,
919 param.name,
920 NULL,
921 aparam.alias_ifs[aparam.num_alias_ifs - 1]);
922 }
923 free(aparam.alias_ifs);
924 }
925
926 /* The source interface can only appear in at most
Neil Jerram4918bd52015-06-10 22:23:20 +0100927 one --bridge-interface. */
Neil Jerram9bad3392015-06-10 22:16:35 +0100928 break;
929 }
Tarun Kundu024baf12022-06-28 10:56:47 -0700930 }
931 }
Neil Jerram9bad3392015-06-10 22:16:35 +0100932 }
Simon Kelley421594f2012-12-02 12:17:35 +0000933 }
934 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000935 return next_event;
936}
Neil Jerram9bad3392015-06-10 22:16:35 +0100937
938static int send_ra_to_aliases(int index, unsigned int type, char *mac, size_t maclen, void *parm)
939{
940 struct alias_param *aparam = (struct alias_param *)parm;
941 char ifrn_name[IFNAMSIZ];
942 struct dhcp_bridge *alias;
943
944 (void)type;
945 (void)mac;
946 (void)maclen;
947
948 if (if_indextoname(index, ifrn_name))
949 for (alias = aparam->bridge->alias; alias; alias = alias->next)
950 if (wildcard_matchn(alias->iface, ifrn_name, IFNAMSIZ))
951 {
952 if (aparam->alias_ifs && (aparam->num_alias_ifs < aparam->max_alias_ifs))
953 aparam->alias_ifs[aparam->num_alias_ifs] = index;
954 aparam->num_alias_ifs++;
955 }
956
957 return 1;
958}
959
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000960static int iface_search(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +0000961 int scope, int if_index, int flags,
Simon Kelley1f776932012-12-16 19:46:08 +0000962 int preferred, int valid, void *vparam)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000963{
964 struct search_param *param = vparam;
Simon Kelley741c2952012-02-25 13:09:18 +0000965 struct dhcp_context *context;
Simon Kelleycd672932020-01-27 22:53:07 +0000966 struct iname *tmp;
967
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000968 (void)scope;
Simon Kelley1f776932012-12-16 19:46:08 +0000969 (void)preferred;
970 (void)valid;
Simon Kelleycd672932020-01-27 22:53:07 +0000971
972 /* ignore interfaces we're not doing DHCP on. */
973 if (!indextoname(daemon->icmp6fd, if_index, param->name) ||
974 !iface_check(AF_LOCAL, NULL, param->name, NULL))
975 return 1;
976
977 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
978 if (tmp->name && wildcard_match(tmp->name, param->name))
979 return 1;
980
Simon Kelley1f776932012-12-16 19:46:08 +0000981 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100982 if (!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) &&
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000983 prefix <= context->prefix &&
984 is_same_net6(local, &context->start6, context->prefix) &&
985 is_same_net6(local, &context->end6, context->prefix) &&
Simon Kelley6e3dba32012-12-16 21:52:45 +0000986 context->ra_time != 0 &&
987 difftime(context->ra_time, param->now) <= 0.0)
988 {
989 /* found an interface that's overdue for RA determine new
990 timeout value and arrange for RA to be sent unless interface is
991 still doing DAD.*/
Simon Kelleybad7b872012-12-20 22:00:39 +0000992 if (!(flags & IFACE_TENTATIVE))
Simon Kelley6e3dba32012-12-16 21:52:45 +0000993 param->iface = if_index;
994
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100995 new_timeout(context, param->name, param->now);
Simon Kelley6e3dba32012-12-16 21:52:45 +0000996
997 /* zero timers for other contexts on the same subnet, so they don't timeout
998 independently */
999 for (context = context->next; context; context = context->next)
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +00001000 if (prefix <= context->prefix &&
1001 is_same_net6(local, &context->start6, context->prefix) &&
1002 is_same_net6(local, &context->end6, context->prefix))
Simon Kelley6e3dba32012-12-16 21:52:45 +00001003 context->ra_time = 0;
1004
1005 return 0; /* found, abort */
1006 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +00001007
1008 return 1; /* keep searching */
1009}
Simon Kelleyef1a94a2013-07-26 13:59:03 +01001010
Simon Kelleyc4cd95d2013-10-10 20:58:11 +01001011static void new_timeout(struct dhcp_context *context, char *iface_name, time_t now)
Simon Kelleyef1a94a2013-07-26 13:59:03 +01001012{
Simon Kelleyc4cd95d2013-10-10 20:58:11 +01001013 if (difftime(now, context->ra_short_period_start) < 60.0)
Simon Kelleyef1a94a2013-07-26 13:59:03 +01001014 /* range 5 - 20 */
1015 context->ra_time = now + 5 + (rand16()/4400);
1016 else
Simon Kelleyc4cd95d2013-10-10 20:58:11 +01001017 {
1018 /* range 3/4 - 1 times MaxRtrAdvInterval */
1019 unsigned int adv_interval = calc_interval(find_iface_param(iface_name));
1020 context->ra_time = now + (3 * adv_interval)/4 + ((adv_interval * (unsigned int)rand16()) >> 18);
1021 }
1022}
1023
1024static struct ra_interface *find_iface_param(char *iface)
1025{
1026 struct ra_interface *ra;
1027
1028 for (ra = daemon->ra_interfaces; ra; ra = ra->next)
1029 if (wildcard_match(ra->name, iface))
1030 return ra;
1031
1032 return NULL;
1033}
1034
1035static unsigned int calc_interval(struct ra_interface *ra)
1036{
Tarun Kundu74fb2b62022-07-06 09:11:18 -07001037 int interval = DEFAULT_RTR_ADV_INTERVAL;
Tarun Kundu36e87102022-07-20 14:43:44 -07001038 if (ra) {
Tarun Kundu74fb2b62022-07-06 09:11:18 -07001039 unsigned int min_rtr_interval = ra->min_ra_adv_interval;
1040 unsigned int max_rtr_interval = ra->max_ra_adv_interval;
1041
1042 if (max_rtr_interval > MAX_RTR_ADV_INTERVAL_UPPER_BOUND)
1043 max_rtr_interval = MAX_RTR_ADV_INTERVAL_UPPER_BOUND;
1044 else if (max_rtr_interval < MAX_RTR_ADV_INTERVAL_LOWER_BOUND)
1045 max_rtr_interval = MAX_RTR_ADV_INTERVAL_LOWER_BOUND;
1046
1047 if (min_rtr_interval < MIN_RTR_ADV_INTERVAL)
1048 min_rtr_interval = MIN_RTR_ADV_INTERVAL;
1049
1050 interval = (rand() % (max_rtr_interval - min_rtr_interval + 1)) + min_rtr_interval;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +01001051
Tarun Kundu74fb2b62022-07-06 09:11:18 -07001052 if (((ra->ra_sent_count + 1) < MAX_INITIAL_RTR_ADVERTISEMENTS) &&
1053 interval > MAX_INITIAL_RTR_ADVERT_INTERVAL)
1054 interval = random_num_in_range(MAX_INITIAL_RTR_ADVERT_INTERVAL);
Tarun Kundu36e87102022-07-20 14:43:44 -07001055 }
Simon Kelleyc4cd95d2013-10-10 20:58:11 +01001056 return (unsigned int)interval;
1057}
1058
1059static unsigned int calc_lifetime(struct ra_interface *ra)
1060{
1061 int lifetime, interval = (int)calc_interval(ra);
1062
1063 if (!ra || ra->lifetime == -1) /* not specified */
1064 lifetime = 3 * interval;
1065 else
1066 {
1067 lifetime = ra->lifetime;
1068 if (lifetime < interval && lifetime != 0)
1069 lifetime = interval;
1070 else if (lifetime > 9000)
1071 lifetime = 9000;
1072 }
1073
1074 return (unsigned int)lifetime;
1075}
1076
1077static unsigned int calc_prio(struct ra_interface *ra)
1078{
1079 if (ra)
1080 return ra->prio;
1081
1082 return 0;
Simon Kelleyef1a94a2013-07-26 13:59:03 +01001083}
Simon Kelley801ca9a2012-03-06 19:30:17 +00001084
Tarun Kundu024baf12022-06-28 10:56:47 -07001085static unsigned char calc_hop_limit(struct ra_interface *ra)
1086{
1087 if (ra) {
1088 return ra->hop_limit;
1089 }
1090 return (unsigned char)hop_limit;
1091}
1092
Tarun Kundu41aa4882022-07-05 10:44:13 -07001093static unsigned int calc_retrans_time(struct ra_interface *ra)
1094{
1095 if (ra) {
1096 return ra->retrans_time;
1097 }
1098 return 0;
1099}
1100
Tarun Kundu024baf12022-06-28 10:56:47 -07001101static unsigned int calc_reachable_time(struct ra_interface *ra)
1102{
1103 if (ra) {
1104 return ra->reachable_time;
1105 }
1106 return 0;
1107}
Tarun Kundu01293a72022-07-18 23:50:36 -07001108
1109int64_t timespecdiff(struct timespec const *a, struct timespec const *b)
1110{
1111 int64_t msec;
1112 msec = ((int64_t)a->tv_sec - b->tv_sec) * 1000;
1113 msec += ((int64_t)a->tv_nsec - b->tv_nsec) / (1000 * 1000);
1114 return msec;
1115}
Simon Kelleyc5ad4e72012-02-24 16:06:20 +00001116#endif