blob: 67ee482f0906f19094b3946961bb2e3d6dcf618d [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.kundudc8b7b42022-11-14 17:12:23 -080083#ifdef IPV6_RECVHOPLIMIT
84 int on_ipv6_recvhoplimit = 1;
85#endif
Tarun Kundu024baf12022-06-28 10:56:47 -070086 socklen_t len = sizeof(hop_limit);
Simon Kelley353ae4d2012-03-19 20:07:51 +000087 struct dhcp_context *context;
88
Simon Kelley843c96b2012-02-27 17:42:38 +000089 /* ensure this is around even if we're not doing DHCPv6 */
90 expand_buf(&daemon->outpacket, sizeof(struct dhcp_packet));
Simon Kelley353ae4d2012-03-19 20:07:51 +000091
Josh Soref730c6742017-02-06 16:14:04 +000092 /* See if we're guessing SLAAC addresses, if so we need to receive ping replies */
Simon Kelley1f776932012-12-16 19:46:08 +000093 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley353ae4d2012-03-19 20:07:51 +000094 if ((context->flags & CONTEXT_RA_NAME))
95 break;
96
Simon Kelley89500e32013-09-20 16:29:20 +010097 /* Need ICMP6 socket for transmission for DHCPv6 even when not doing RA. */
98
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000099 ICMP6_FILTER_SETBLOCKALL(&filter);
Simon Kelley89500e32013-09-20 16:29:20 +0100100 if (daemon->doing_ra)
101 {
102 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);
103 if (context)
104 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filter);
105 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000106
107 if ((fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1 ||
Simon Kelleyc5379c12012-02-24 20:05:52 +0000108 getsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hop_limit, &len) ||
Simon Kelley0e88d532012-03-28 22:22:05 +0100109#if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000110 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &class, sizeof(class)) == -1 ||
111#endif
112 !fix_fd(fd) ||
113 !set_ipv6pktinfo(fd) ||
114 setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val)) ||
115 setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val)) ||
tarun.kundudc8b7b42022-11-14 17:12:23 -0800116#ifdef IPV6_RECVHOPLIMIT
117 setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on_ipv6_recvhoplimit, sizeof(on_ipv6_recvhoplimit)) ||
118#endif
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000119 setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) == -1)
120 die (_("cannot create ICMPv6 socket: %s"), NULL, EC_BADNET);
121
122 daemon->icmp6fd = fd;
123
Simon Kelley89500e32013-09-20 16:29:20 +0100124 if (daemon->doing_ra)
Josh Soref730c6742017-02-06 16:14:04 +0000125 ra_start_unsolicited(now, NULL);
Tarun Kundu15048b52022-07-05 12:16:58 -0700126
127 /* Intializes random number generator */
128 srand(time(0));
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000129}
130
Josh Soref730c6742017-02-06 16:14:04 +0000131void ra_start_unsolicited(time_t now, struct dhcp_context *context)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000132{
Simon Kelley353ae4d2012-03-19 20:07:51 +0000133 /* 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 +0000134 if it's not appropriate to advertise those contexts.
135 This gets re-called on a netlink route-change to re-do the advertisement
136 and pick up new interfaces */
Simon Kelley6e3dba32012-12-16 21:52:45 +0000137
Simon Kelley353ae4d2012-03-19 20:07:51 +0000138 if (context)
Simon Kelley1b75c1e2012-12-18 19:55:25 +0000139 context->ra_short_period_start = context->ra_time = now;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000140 else
Simon Kelley1f776932012-12-16 19:46:08 +0000141 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley6e3dba32012-12-16 21:52:45 +0000142 if (!(context->flags & CONTEXT_TEMPLATE))
Simon Kelley1b75c1e2012-12-18 19:55:25 +0000143 {
144 context->ra_time = now + (rand16()/13000); /* range 0 - 5 */
145 /* re-do frequently for a minute or so, in case the first gets lost. */
146 context->ra_short_period_start = now;
147 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000148}
149
Simon Kelley1f776932012-12-16 19:46:08 +0000150void icmp6_packet(time_t now)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000151{
152 char interface[IF_NAMESIZE+1];
153 ssize_t sz;
154 int if_index = 0;
tarun.kundudc8b7b42022-11-14 17:12:23 -0800155 int hoplimit = 255;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000156 struct cmsghdr *cmptr;
157 struct msghdr msg;
158 union {
tarun.kundudc8b7b42022-11-14 17:12:23 -0800159 struct cmsghdr align;
160 /* Extending control6 array to include hoplimit information
161 in addition to in6_pktinfo information */
162 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(sizeof(int))];
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000163 } control_u;
164 struct sockaddr_in6 from;
Simon Kelley5ef33272012-03-30 15:10:28 +0100165 unsigned char *packet;
Tarun Kundu01293a72022-07-18 23:50:36 -0700166 struct iname *tmp, *if_tmp;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000167
168 /* Note: use outpacket for input buffer */
169 msg.msg_control = control_u.control6;
170 msg.msg_controllen = sizeof(control_u);
171 msg.msg_flags = 0;
172 msg.msg_name = &from;
173 msg.msg_namelen = sizeof(from);
174 msg.msg_iov = &daemon->outpacket;
175 msg.msg_iovlen = 1;
176
177 if ((sz = recv_dhcp_packet(daemon->icmp6fd, &msg)) == -1 || sz < 8)
178 return;
Simon Kelley5ef33272012-03-30 15:10:28 +0100179
180 packet = (unsigned char *)daemon->outpacket.iov_base;
tarun.kundudc8b7b42022-11-14 17:12:23 -0800181 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) {
182#ifdef IPV6_HOPLIMIT
183 if (cmptr->cmsg_level == IPPROTO_IPV6 && (cmptr->cmsg_type == IPV6_HOPLIMIT)) {
184 if ((cmptr->cmsg_len == CMSG_LEN(sizeof(int))) &&
185 (*(int *)CMSG_DATA(cmptr) >= 0) && (*(int *)CMSG_DATA(cmptr) < 256)) {
186 hoplimit = *(int *)CMSG_DATA(cmptr);
187 }
188 }
189#endif /* IPV6_HOPLIMIT */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000190 if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
191 {
192 union {
193 unsigned char *c;
194 struct in6_pktinfo *p;
195 } p;
196 p.c = CMSG_DATA(cmptr);
197
198 if_index = p.p->ipi6_ifindex;
199 }
tarun.kundudc8b7b42022-11-14 17:12:23 -0800200 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000201
202 if (!indextoname(daemon->icmp6fd, if_index, interface))
203 return;
204
Simon Kelley4f7b3042012-11-28 21:27:02 +0000205 if (!iface_check(AF_LOCAL, NULL, interface, NULL))
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000206 return;
207
208 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
Simon Kelley49333cb2013-03-15 20:30:51 +0000209 if (tmp->name && wildcard_match(tmp->name, interface))
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000210 return;
211
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100212 if (packet[1] != 0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000213 return;
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100214
Simon Kelley5ef33272012-03-30 15:10:28 +0100215 if (packet[0] == ICMP6_ECHO_REPLY)
216 lease_ping_reply(&from.sin6_addr, packet, interface);
tarun.kundudc8b7b42022-11-14 17:12:23 -0800217 else if ((packet[0] == ND_ROUTER_SOLICIT) && (hoplimit == 255))
tarun.kundu72971982022-11-10 08:42:59 -0800218 {
tarun.kundu1c6e7fa2022-11-16 14:00:17 -0800219 if (!((sz > 8) && (packet[9] == 0))) {
tarun.kundu72971982022-11-10 08:42:59 -0800220 inet_ntop(AF_INET6, &from.sin6_addr, daemon->addrbuff, ADDRSTRLEN);
tarun.kundu1c6e7fa2022-11-16 14:00:17 -0800221 if (!((strncmp(daemon->addrbuff, "::",2)==0) && (packet[8] == ICMP6_OPT_SOURCE_MAC)))
Simon Kelley353ae4d2012-03-19 20:07:51 +0000222 {
Simon Kelley5ef33272012-03-30 15:10:28 +0100223 char *mac = "";
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100224 struct dhcp_bridge *bridge, *alias;
Tarun Kundu01293a72022-07-18 23:50:36 -0700225 struct timespec ts, ts_delay;
Tarun Kundu15048b52022-07-05 12:16:58 -0700226 int tms = 0;
Simon Kelley5ef33272012-03-30 15:10:28 +0100227
228 /* look for link-layer address option for logging */
229 if (sz >= 16 && packet[8] == ICMP6_OPT_SOURCE_MAC && (packet[9] * 8) + 8 <= sz)
230 {
Simon Kelley24036ea2017-09-25 18:47:15 +0100231 if ((packet[9] * 8 - 2) * 3 - 1 >= MAXDNAME) {
232 return;
233 }
Simon Kelley5ef33272012-03-30 15:10:28 +0100234 print_mac(daemon->namebuff, &packet[10], (packet[9] * 8) - 2);
235 mac = daemon->namebuff;
236 }
237
Kevin Darbyshire-Bryant8c0b73d2013-10-11 11:56:33 +0100238 if (!option_bool(OPT_QUIET_RA))
239 my_syslog(MS_DHCP | LOG_INFO, "RTR-SOLICIT(%s) %s", interface, mac);
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100240
Tarun Kundu01293a72022-07-18 23:50:36 -0700241 for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next)
242 {
243 if ((strcmp(if_tmp->name, interface) == 0)) {
244 clock_gettime(CLOCK_MONOTONIC, &ts);
245 if (timespecdiff(&ts, &if_tmp->last_ra_for_rtr_sol) / 1000.0 < MIN_DELAY_BETWEEN_RAS) {
246 /* last RA for RTR Solicit was sent only a few moments ago,
247 don't send another immediately. */
248 ts_delay.tv_sec = MIN_DELAY_BETWEEN_RAS - ts.tv_sec + if_tmp->last_ra_for_rtr_sol.tv_sec;
249 ts_delay.tv_nsec = 0;
250 nanosleep(&ts_delay, NULL);
251 }
252 clock_gettime(CLOCK_MONOTONIC, &if_tmp->last_ra_for_rtr_sol);
253 }
254 }
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100255 /* If the incoming interface is an alias of some other one (as
Neil Jerram4918bd52015-06-10 22:23:20 +0100256 specified by the --bridge-interface option), send an RA using
257 the context of the aliased interface. */
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100258 for (bridge = daemon->bridges; bridge; bridge = bridge->next)
259 {
260 int bridge_index = if_nametoindex(bridge->iface);
261 if (bridge_index)
262 {
263 for (alias = bridge->alias; alias; alias = alias->next)
264 if (wildcard_matchn(alias->iface, interface, IF_NAMESIZE))
265 {
266 /* Send an RA on if_index with information from
267 bridge_index. */
268 send_ra_alias(now, bridge_index, bridge->iface, NULL, if_index);
269 break;
270 }
271 if (alias)
272 break;
273 }
274 }
275
276 /* If the incoming interface wasn't an alias, send an RA using
277 the context of the incoming interface. */
278 if (!bridge)
279 /* source address may not be valid in solicit request. */
280 send_ra(now, if_index, interface, !IN6_IS_ADDR_UNSPECIFIED(&from.sin6_addr) ? &from.sin6_addr : NULL);
Tarun Kundu15048b52022-07-05 12:16:58 -0700281 tms = random_num_in_range(MAX_RA_DELAY_TIME);
282 ts.tv_sec = 0;
283 ts.tv_nsec = tms * 1000000;
284 nanosleep(&ts, NULL);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000285 }
tarun.kundu1c6e7fa2022-11-16 14:00:17 -0800286 }
tarun.kundu72971982022-11-10 08:42:59 -0800287 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000288}
289
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100290static 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 +0000291{
292 struct ra_packet *ra;
293 struct ra_param parm;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000294 struct sockaddr_in6 addr;
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100295 struct dhcp_context *context, *tmp, **up;
Simon Kelley18f0fb02012-03-31 21:18:55 +0100296 struct dhcp_netid iface_id;
297 struct dhcp_opt *opt_cfg;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100298 struct ra_interface *ra_param = find_iface_param(iface_name);
David Flamand005c46d2017-04-11 11:49:54 +0100299 int done_dns = 0, old_prefix = 0, mtu = 0;
Simon Kelleyc3a04082014-01-11 22:18:19 +0000300 unsigned int min_pref_time;
Simon Kelley3b436462012-12-28 11:55:45 +0000301#ifdef HAVE_LINUX_NETWORK
302 FILE *f;
303#endif
Tarun Kundu024baf12022-06-28 10:56:47 -0700304
305 struct iname *if_tmp;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000306 parm.ind = iface;
307 parm.managed = 0;
Simon Kelley30cd9662012-03-25 20:44:38 +0100308 parm.other = 0;
S La18bf312016-02-12 17:36:20 +0000309 parm.found_context = NULL;
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100310 parm.adv_router = 0;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000311 parm.if_name = iface_name;
312 parm.first = 1;
Simon Kelley1f776932012-12-16 19:46:08 +0000313 parm.now = now;
Simon Kelleyc3a04082014-01-11 22:18:19 +0000314 parm.glob_pref_time = parm.link_pref_time = parm.ula_pref_time = 0;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100315 parm.adv_interval = calc_interval(ra_param);
Ilya Ponetaev5bf50af2014-09-09 12:46:21 +0100316 parm.prio = calc_prio(ra_param);
Simon Kelley1f776932012-12-16 19:46:08 +0000317
Simon Kelleyfa785732016-07-22 20:56:01 +0100318 reset_counter();
Simon Kelleyce7845b2016-07-06 21:42:27 +0100319
320 if (!(ra = expand(sizeof(struct ra_packet))))
321 return;
Ilya Ponetaev5bf50af2014-09-09 12:46:21 +0100322
323 ra->type = ND_ROUTER_ADVERT;
324 ra->code = 0;
Ilya Ponetaev5bf50af2014-09-09 12:46:21 +0100325 ra->flags = parm.prio;
326 ra->lifetime = htons(calc_lifetime(ra_param));
Tarun Kundu024baf12022-06-28 10:56:47 -0700327 ra->reachable_time = htonl(calc_reachable_time(ra_param));
328 ra->hop_limit = calc_hop_limit(ra_param);
Tarun Kundu41aa4882022-07-05 10:44:13 -0700329 ra->retrans_time = htonl(calc_retrans_time(ra_param));
Ilya Ponetaev5bf50af2014-09-09 12:46:21 +0100330
Simon Kelley18f0fb02012-03-31 21:18:55 +0100331 /* set tag with name == interface */
332 iface_id.net = iface_name;
333 iface_id.next = NULL;
334 parm.tags = &iface_id;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000335
Tarun Kundu74fb2b62022-07-06 09:11:18 -0700336 for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next)
337 {
338 if ((strcmp(if_tmp->name, iface_name) == 0) && (if_tmp->is_ra_adv_dis == true)) {
339 if (if_tmp->final_ra_sent_count < MAX_FINAL_RTR_ADVERTISEMENTS) {
340 ra->lifetime = htons(0);
341 if_tmp->final_ra_sent_count++;
342 break;
343 }
344 else {
345 return;
346 }
347 }
348 }
349
Simon Kelley1f776932012-12-16 19:46:08 +0000350 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley18f0fb02012-03-31 21:18:55 +0100351 {
352 context->flags &= ~CONTEXT_RA_DONE;
353 context->netid.next = &context->netid;
354 }
355
Simon Kelley378fa562020-01-05 17:23:19 +0000356 /* If no link-local address then we can't advertise since source address of
357 advertisement must be link local address: RFC 4861 para 6.1.2. */
358 if (!iface_enumerate(AF_INET6, &parm, add_prefixes) ||
359 parm.link_pref_time == 0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000360 return;
361
Simon Kelleyc3a04082014-01-11 22:18:19 +0000362 /* Find smallest preferred time within address classes,
363 to use as lifetime for options. This is a rather arbitrary choice. */
364 min_pref_time = 0xffffffff;
365 if (parm.glob_pref_time != 0 && parm.glob_pref_time < min_pref_time)
366 min_pref_time = parm.glob_pref_time;
367
368 if (parm.ula_pref_time != 0 && parm.ula_pref_time < min_pref_time)
369 min_pref_time = parm.ula_pref_time;
370
371 if (parm.link_pref_time != 0 && parm.link_pref_time < min_pref_time)
372 min_pref_time = parm.link_pref_time;
373
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100374 /* Look for constructed contexts associated with addresses which have gone,
375 and advertise them with preferred_time == 0 RFC 6204 4.3 L-13 */
376 for (up = &daemon->dhcp6, context = daemon->dhcp6; context; context = tmp)
377 {
378 tmp = context->next;
Simon Kelley3b436462012-12-28 11:55:45 +0000379
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100380 if (context->if_index == iface && (context->flags & CONTEXT_OLD))
381 {
382 unsigned int old = difftime(now, context->address_lost_time);
383
384 if (old > context->saved_valid)
S La18bf312016-02-12 17:36:20 +0000385 {
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100386 /* We've advertised this enough, time to go */
S La18bf312016-02-12 17:36:20 +0000387
388 /* If this context held the timeout, and there's another context in use
389 transfer the timeout there. */
390 if (context->ra_time != 0 && parm.found_context && parm.found_context->ra_time == 0)
391 new_timeout(parm.found_context, iface_name, now);
392
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100393 *up = context->next;
394 free(context);
395 }
396 else
397 {
398 struct prefix_opt *opt;
399 struct in6_addr local = context->start6;
400 int do_slaac = 0;
401
Simon Kelley44522922013-11-15 14:45:04 +0000402 old_prefix = 1;
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100403
404 /* zero net part of address */
405 setaddr6part(&local, addr6part(&local) & ~((context->prefix == 64) ? (u64)-1LL : (1LLU << (128 - context->prefix)) - 1LLU));
406
Simon Kelley6ea1f232013-12-19 15:45:12 +0000407
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100408 if (context->flags & CONTEXT_RA)
Simon Kelley6ea1f232013-12-19 15:45:12 +0000409 {
410 do_slaac = 1;
411 if (context->flags & CONTEXT_DHCP)
412 {
413 parm.other = 1;
414 if (!(context->flags & CONTEXT_RA_STATELESS))
415 parm.managed = 1;
416 }
417 }
418 else
419 {
420 /* don't do RA for non-ra-only unless --enable-ra is set */
421 if (option_bool(OPT_RA))
422 {
423 parm.managed = 1;
424 parm.other = 1;
425 }
426 }
427
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100428 if ((opt = expand(sizeof(struct prefix_opt))))
429 {
430 opt->type = ICMP6_OPT_PREFIX;
431 opt->len = 4;
432 opt->prefix_len = context->prefix;
Neil Jerram2fd5bc92015-06-10 22:13:06 +0100433 /* autonomous only if we're not doing dhcp, set
434 "on-link" unless "off-link" was specified */
435 opt->flags = (do_slaac ? 0x40 : 0) |
tarun.kundu66031192022-11-10 14:39:45 -0800436 ((context->flags & CONTEXT_RA_OFF_LINK) ? 0 : 0x80) |
437 ((context->flags & CONTEXT_RA_AUTO_OFF) ? 0 : 0x40);
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100438 opt->valid_lifetime = htonl(context->saved_valid - old);
439 opt->preferred_lifetime = htonl(0);
440 opt->reserved = 0;
441 opt->prefix = local;
442
443 inet_ntop(AF_INET6, &local, daemon->addrbuff, ADDRSTRLEN);
Kevin Darbyshire-Bryant8c0b73d2013-10-11 11:56:33 +0100444 if (!option_bool(OPT_QUIET_RA))
445 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s old prefix", iface_name, daemon->addrbuff);
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100446 }
447
448 up = &context->next;
449 }
450 }
451 else
452 up = &context->next;
453 }
454
Simon Kelley44522922013-11-15 14:45:04 +0000455 /* If we're advertising only old prefixes, set router lifetime to zero. */
456 if (old_prefix && !parm.found_context)
457 ra->lifetime = htons(0);
458
459 /* No prefixes to advertise. */
460 if (!old_prefix && !parm.found_context)
461 return;
462
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100463 /* If we're sending router address instead of prefix in at least on prefix,
464 include the advertisement interval option. */
465 if (parm.adv_router)
466 {
467 put_opt6_char(ICMP6_OPT_ADV_INTERVAL);
468 put_opt6_char(1);
469 put_opt6_short(0);
470 /* interval value is in milliseconds */
471 put_opt6_long(1000 * calc_interval(find_iface_param(iface_name)));
472 }
473
David Flamand005c46d2017-04-11 11:49:54 +0100474 /* Set the MTU from ra_param if any, an MTU of 0 mean automatic for linux, */
475 /* an MTU of -1 prevents the option from being sent. */
Simon Kelley3b436462012-12-28 11:55:45 +0000476#ifdef HAVE_LINUX_NETWORK
Ville Skyttäfaaf3062018-01-14 17:32:52 +0000477 /* Note that IPv6 MTU is not necessarily the same as the IPv4 MTU
Simon Kelley3b436462012-12-28 11:55:45 +0000478 available from SIOCGIFMTU */
Vladislav Grishenko6ec5f5c2017-04-24 22:34:45 +0100479 char *mtu_name = ra_param ? ra_param->mtu_name : NULL;
Simon Kelley5ed82ae2019-03-05 16:38:34 +0000480 sprintf(daemon->namebuff, "/proc/sys/net/ipv6/conf/%s/mtu", mtu_name ? mtu_name : iface_name);
David Flamand005c46d2017-04-11 11:49:54 +0100481 if ((f = fopen(daemon->namebuff, "r")))
482 {
483 if (fgets(daemon->namebuff, MAXDNAME, f))
484 mtu = atoi(daemon->namebuff);
485 fclose(f);
486 }
Simon Kelley3b436462012-12-28 11:55:45 +0000487#endif
tarun.kundu72971982022-11-10 08:42:59 -0800488if (ra_param)
489 mtu = ra_param->mtu;
David Flamand005c46d2017-04-11 11:49:54 +0100490 if (mtu > 0)
491 {
492 put_opt6_char(ICMP6_OPT_MTU);
493 put_opt6_char(1);
494 put_opt6_short(0);
495 put_opt6_long(mtu);
496 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000497
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100498 iface_enumerate(AF_LOCAL, &send_iface, add_lla);
Simon Kelley18f0fb02012-03-31 21:18:55 +0100499
500 /* RDNSS, RFC 6106, use relevant DHCP6 options */
501 (void)option_filter(parm.tags, NULL, daemon->dhcp_opts6);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000502
Simon Kelley18f0fb02012-03-31 21:18:55 +0100503 for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next)
504 {
505 int i;
506
507 /* netids match and not encapsulated? */
508 if (!(opt_cfg->flags & DHOPT_TAGOK))
509 continue;
510
511 if (opt_cfg->opt == OPTION6_DNS_SERVER)
512 {
Simon Kelleyc3a04082014-01-11 22:18:19 +0000513 struct in6_addr *a;
514 int len;
515
Simon Kelley18f0fb02012-03-31 21:18:55 +0100516 done_dns = 1;
Simon Kelleyc3a04082014-01-11 22:18:19 +0000517
518 if (opt_cfg->len == 0)
Simon Kelley806cf782013-10-14 14:08:44 +0100519 continue;
Simon Kelley18f0fb02012-03-31 21:18:55 +0100520
Simon Kelleyc3a04082014-01-11 22:18:19 +0000521 /* reduce len for any addresses we can't substitute */
522 for (a = (struct in6_addr *)opt_cfg->val, len = opt_cfg->len, i = 0;
523 i < opt_cfg->len; i += IN6ADDRSZ, a++)
524 if ((IN6_IS_ADDR_UNSPECIFIED(a) && parm.glob_pref_time == 0) ||
525 (IN6_IS_ADDR_ULA_ZERO(a) && parm.ula_pref_time == 0) ||
526 (IN6_IS_ADDR_LINK_LOCAL_ZERO(a) && parm.link_pref_time == 0))
527 len -= IN6ADDRSZ;
528
529 if (len != 0)
530 {
531 put_opt6_char(ICMP6_OPT_RDNSS);
532 put_opt6_char((len/8) + 1);
533 put_opt6_short(0);
534 put_opt6_long(min_pref_time);
535
536 for (a = (struct in6_addr *)opt_cfg->val, i = 0; i < opt_cfg->len; i += IN6ADDRSZ, a++)
537 if (IN6_IS_ADDR_UNSPECIFIED(a))
538 {
539 if (parm.glob_pref_time != 0)
540 put_opt6(&parm.link_global, IN6ADDRSZ);
541 }
542 else if (IN6_IS_ADDR_ULA_ZERO(a))
543 {
544 if (parm.ula_pref_time != 0)
545 put_opt6(&parm.ula, IN6ADDRSZ);
546 }
547 else if (IN6_IS_ADDR_LINK_LOCAL_ZERO(a))
548 {
549 if (parm.link_pref_time != 0)
550 put_opt6(&parm.link_local, IN6ADDRSZ);
551 }
552 else
553 put_opt6(a, IN6ADDRSZ);
554 }
Simon Kelley18f0fb02012-03-31 21:18:55 +0100555 }
556
557 if (opt_cfg->opt == OPTION6_DOMAIN_SEARCH && opt_cfg->len != 0)
558 {
559 int len = ((opt_cfg->len+7)/8);
560
561 put_opt6_char(ICMP6_OPT_DNSSL);
562 put_opt6_char(len + 1);
563 put_opt6_short(0);
Simon Kelleyc3a04082014-01-11 22:18:19 +0000564 put_opt6_long(min_pref_time);
Simon Kelley18f0fb02012-03-31 21:18:55 +0100565 put_opt6(opt_cfg->val, opt_cfg->len);
566
567 /* pad */
568 for (i = opt_cfg->len; i < len * 8; i++)
569 put_opt6_char(0);
570 }
571 }
572
Simon Kelleyc3a04082014-01-11 22:18:19 +0000573 if (daemon->port == NAMESERVER_PORT && !done_dns && parm.link_pref_time != 0)
Simon Kelley18f0fb02012-03-31 21:18:55 +0100574 {
Simon Kelleyab915f82013-04-30 10:41:28 +0100575 /* default == us, as long as we are supplying DNS service. */
Simon Kelley18f0fb02012-03-31 21:18:55 +0100576 put_opt6_char(ICMP6_OPT_RDNSS);
577 put_opt6_char(3);
578 put_opt6_short(0);
Simon Kelleyc3a04082014-01-11 22:18:19 +0000579 put_opt6_long(min_pref_time);
Simon Kelley806cf782013-10-14 14:08:44 +0100580 put_opt6(&parm.link_local, IN6ADDRSZ);
Simon Kelley18f0fb02012-03-31 21:18:55 +0100581 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000582
583 /* set managed bits unless we're providing only RA on this link */
584 if (parm.managed)
Simon Kelley30cd9662012-03-25 20:44:38 +0100585 ra->flags |= 0x80; /* M flag, managed, */
586 if (parm.other)
587 ra->flags |= 0x40; /* O flag, other */
588
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000589 /* decide where we're sending */
590 memset(&addr, 0, sizeof(addr));
Simon Kelley22d904d2012-02-26 20:13:45 +0000591#ifdef HAVE_SOCKADDR_SA_LEN
592 addr.sin6_len = sizeof(struct sockaddr_in6);
593#endif
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000594 addr.sin6_family = AF_INET6;
595 addr.sin6_port = htons(IPPROTO_ICMPV6);
596 if (dest)
597 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000598 addr.sin6_addr = *dest;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000599 if (IN6_IS_ADDR_LINKLOCAL(dest) ||
600 IN6_IS_ADDR_MC_LINKLOCAL(dest))
601 addr.sin6_scope_id = iface;
602 }
603 else
Simon Kelley8f3194f2013-09-30 15:04:58 +0100604 {
605 inet_pton(AF_INET6, ALL_NODES, &addr.sin6_addr);
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100606 setsockopt(daemon->icmp6fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &send_iface, sizeof(send_iface));
Simon Kelley8f3194f2013-09-30 15:04:58 +0100607 }
Simon Kelleyff841eb2015-03-11 21:36:30 +0000608 while (retry_send(sendto(daemon->icmp6fd, daemon->outpacket.iov_base,
Simon Kelley6b1c4642016-07-22 20:59:16 +0100609 save_counter(-1), 0, (struct sockaddr *)&addr,
Simon Kelleyff841eb2015-03-11 21:36:30 +0000610 sizeof(addr))));
Tarun Kundu74fb2b62022-07-06 09:11:18 -0700611 ra_param->ra_sent_count++;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000612}
613
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100614static void send_ra(time_t now, int iface, char *iface_name, struct in6_addr *dest)
615{
616 /* Send an RA on the same interface that the RA content is based
617 on. */
618 send_ra_alias(now, iface, iface_name, dest, iface);
619}
620
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000621static int add_prefixes(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +0000622 int scope, int if_index, int flags,
Simon Kelley55b42f62012-12-21 16:53:15 +0000623 unsigned int preferred, unsigned int valid, void *vparam)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000624{
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000625 struct ra_param *param = vparam;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000626
627 (void)scope; /* warning */
Tarun Kundu024baf12022-06-28 10:56:47 -0700628
629 struct iname *if_tmp;
630 for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next)
631 {
632 if ((strcmp(if_tmp->name, param->if_name) == 0) && (if_tmp->is_ra_adv_dis == true)) {
Tarun Kundu74fb2b62022-07-06 09:11:18 -0700633 if (if_tmp->final_ra_sent_count < MAX_FINAL_RTR_ADVERTISEMENTS) {
634 break;
635 }
636 else {
637 return 1;
638 }
Tarun Kundu024baf12022-06-28 10:56:47 -0700639 }
640 }
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000641
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000642 if (if_index == param->ind)
643 {
644 if (IN6_IS_ADDR_LINKLOCAL(local))
Simon Kelleyc3a04082014-01-11 22:18:19 +0000645 {
646 /* Can there be more than one LL address?
647 Select the one with the longest preferred time
648 if there is. */
649 if (preferred > param->link_pref_time)
650 {
651 param->link_pref_time = preferred;
652 param->link_local = *local;
653 }
654 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000655 else if (!IN6_IS_ADDR_LOOPBACK(local) &&
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000656 !IN6_IS_ADDR_MULTICAST(local))
657 {
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000658 int real_prefix = 0;
Simon Kelleyc8257542012-03-28 21:15:41 +0100659 int do_slaac = 0;
660 int deprecate = 0;
Simon Kelley3bc0d932012-12-28 11:31:44 +0000661 int constructed = 0;
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100662 int adv_router = 0;
Neil Jerram2fd5bc92015-06-10 22:13:06 +0100663 int off_link = 0;
tarun.kundu66031192022-11-10 14:39:45 -0800664 int auto_off = 0;
Kyle Swenson9d238072021-12-01 08:13:17 -0700665 unsigned int valid_lifetime = 0xffffffff;
Simon Kelleyc8257542012-03-28 21:15:41 +0100666 unsigned int time = 0xffffffff;
Simon Kelley1e02a852012-03-29 11:07:25 +0100667 struct dhcp_context *context;
668
Simon Kelley1f776932012-12-16 19:46:08 +0000669 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100670 if (!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) &&
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000671 prefix <= context->prefix &&
672 is_same_net6(local, &context->start6, context->prefix) &&
673 is_same_net6(local, &context->end6, context->prefix))
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000674 {
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100675 context->saved_valid = valid;
676
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100677 if (context->flags & CONTEXT_RA)
Simon Kelley30cd9662012-03-25 20:44:38 +0100678 {
679 do_slaac = 1;
Simon Kelley4723d492012-03-30 21:04:17 +0100680 if (context->flags & CONTEXT_DHCP)
Simon Kelley05e92e52012-03-30 22:24:15 +0100681 {
682 param->other = 1;
683 if (!(context->flags & CONTEXT_RA_STATELESS))
684 param->managed = 1;
685 }
Simon Kelley30cd9662012-03-25 20:44:38 +0100686 }
Simon Kelley884a6df2012-03-20 16:20:22 +0000687 else
Simon Kelley0010b472012-02-29 12:18:30 +0000688 {
689 /* don't do RA for non-ra-only unless --enable-ra is set */
690 if (!option_bool(OPT_RA))
691 continue;
692 param->managed = 1;
Simon Kelley30cd9662012-03-25 20:44:38 +0100693 param->other = 1;
Simon Kelley0010b472012-02-29 12:18:30 +0000694 }
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100695
696 /* Configured to advertise router address, not prefix. See RFC 3775 7.2
697 In this case we do all addresses associated with a context,
698 hence the real_prefix setting here. */
699 if (context->flags & CONTEXT_RA_ROUTER)
700 {
701 adv_router = 1;
702 param->adv_router = 1;
703 real_prefix = context->prefix;
704 }
705
Simon Kelley4d85e402020-07-12 22:45:46 +0100706 /* find floor time, don't reduce below 3 * RA interval.
707 If the lease time has been left as default, don't
708 use that as a floor. */
709 if ((context->flags & CONTEXT_SETLEASE) &&
710 time > context->lease_time)
Simon Kelley7f035f52012-12-22 21:27:08 +0000711 {
Kyle Swenson9d238072021-12-01 08:13:17 -0700712
713 if ((context->flags & CONTEXT_SETVALID))
714 valid_lifetime = context->valid_lifetime;
715 else
716 valid_lifetime = context->lease_time;
717
Simon Kelley7f035f52012-12-22 21:27:08 +0000718 time = context->lease_time;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100719 if (time < ((unsigned int)(3 * param->adv_interval)))
720 time = 3 * param->adv_interval;
Simon Kelley7f035f52012-12-22 21:27:08 +0000721 }
722
Simon Kelleyc8257542012-03-28 21:15:41 +0100723 if (context->flags & CONTEXT_DEPRECATE)
724 deprecate = 1;
Simon Kelley3bc0d932012-12-28 11:31:44 +0000725
726 if (context->flags & CONTEXT_CONSTRUCTED)
727 constructed = 1;
728
Simon Kelleyc8257542012-03-28 21:15:41 +0100729
Simon Kelley18f0fb02012-03-31 21:18:55 +0100730 /* collect dhcp-range tags */
731 if (context->netid.next == &context->netid && context->netid.net)
732 {
733 context->netid.next = param->tags;
734 param->tags = &context->netid;
735 }
736
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100737 /* subsequent prefixes on the same interface
738 and subsequent instances of this prefix don't need timers.
739 Be careful not to find the same prefix twice with different
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100740 addresses unless we're advertising the actual addresses. */
Simon Kelley1e02a852012-03-29 11:07:25 +0100741 if (!(context->flags & CONTEXT_RA_DONE))
742 {
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100743 if (!param->first)
744 context->ra_time = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100745 context->flags |= CONTEXT_RA_DONE;
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000746 real_prefix = context->prefix;
Neil Jerram2fd5bc92015-06-10 22:13:06 +0100747 off_link = (context->flags & CONTEXT_RA_OFF_LINK);
tarun.kundu66031192022-11-10 14:39:45 -0800748 auto_off = (context->flags & CONTEXT_RA_AUTO_OFF);
Simon Kelley1e02a852012-03-29 11:07:25 +0100749 }
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100750
S La18bf312016-02-12 17:36:20 +0000751 param->first = 0;
752 /* found_context is the _last_ one we found, so if there's
753 more than one, it's not the first. */
754 param->found_context = context;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000755 }
Simon Kelley1f776932012-12-16 19:46:08 +0000756
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000757 /* configured time is ceiling */
Simon Kelley3bc0d932012-12-28 11:31:44 +0000758 if (!constructed || valid > time)
Kyle Swenson9d238072021-12-01 08:13:17 -0700759 valid = valid_lifetime;
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000760
Simon Kelley3bc0d932012-12-28 11:31:44 +0000761 if (flags & IFACE_DEPRECATED)
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000762 preferred = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100763
Simon Kelley3bc0d932012-12-28 11:31:44 +0000764 if (deprecate)
765 time = 0;
766
767 /* configured time is ceiling */
768 if (!constructed || preferred > time)
769 preferred = time;
Simon Kelleyc3a04082014-01-11 22:18:19 +0000770
771 if (IN6_IS_ADDR_ULA(local))
Simon Kelley55b42f62012-12-21 16:53:15 +0000772 {
Simon Kelleyc3a04082014-01-11 22:18:19 +0000773 if (preferred > param->ula_pref_time)
774 {
775 param->ula_pref_time = preferred;
776 param->ula = *local;
777 }
778 }
779 else
780 {
781 if (preferred > param->glob_pref_time)
782 {
783 param->glob_pref_time = preferred;
784 param->link_global = *local;
785 }
Simon Kelley55b42f62012-12-21 16:53:15 +0000786 }
787
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000788 if (real_prefix != 0)
Simon Kelleyc8257542012-03-28 21:15:41 +0100789 {
Simon Kelley1e02a852012-03-29 11:07:25 +0100790 struct prefix_opt *opt;
791
792 if ((opt = expand(sizeof(struct prefix_opt))))
793 {
Simon Kelley5ef33272012-03-30 15:10:28 +0100794 /* zero net part of address */
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100795 if (!adv_router)
796 setaddr6part(local, addr6part(local) & ~((real_prefix == 64) ? (u64)-1LL : (1LLU << (128 - real_prefix)) - 1LLU));
Simon Kelley5ef33272012-03-30 15:10:28 +0100797
Simon Kelley1e02a852012-03-29 11:07:25 +0100798 opt->type = ICMP6_OPT_PREFIX;
799 opt->len = 4;
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000800 opt->prefix_len = real_prefix;
Neil Jerram2fd5bc92015-06-10 22:13:06 +0100801 /* autonomous only if we're not doing dhcp, set
802 "on-link" unless "off-link" was specified */
803 opt->flags = (off_link ? 0 : 0x80);
tarun.kundu66031192022-11-10 14:39:45 -0800804 if (do_slaac && !auto_off)
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100805 opt->flags |= 0x40;
806 if (adv_router)
807 opt->flags |= 0x20;
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000808 opt->valid_lifetime = htonl(valid);
809 opt->preferred_lifetime = htonl(preferred);
Simon Kelley5ef33272012-03-30 15:10:28 +0100810 opt->reserved = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100811 opt->prefix = *local;
Simon Kelley1e02a852012-03-29 11:07:25 +0100812
Simon Kelley5ef33272012-03-30 15:10:28 +0100813 inet_ntop(AF_INET6, local, daemon->addrbuff, ADDRSTRLEN);
Kevin Darbyshire-Bryant8c0b73d2013-10-11 11:56:33 +0100814 if (!option_bool(OPT_QUIET_RA))
815 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s", param->if_name, daemon->addrbuff);
Simon Kelley1e02a852012-03-29 11:07:25 +0100816 }
Simon Kelleyc8257542012-03-28 21:15:41 +0100817 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000818 }
819 }
820 return 1;
821}
822
823static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm)
824{
825 (void)type;
826
827 if (index == *((int *)parm))
828 {
829 /* size is in units of 8 octets and includes type and length (2 bytes)
830 add 7 to round up */
831 int len = (maclen + 9) >> 3;
832 unsigned char *p = expand(len << 3);
833 memset(p, 0, len << 3);
834 *p++ = ICMP6_OPT_SOURCE_MAC;
835 *p++ = len;
836 memcpy(p, mac, maclen);
837
838 return 0;
839 }
840
841 return 1;
842}
843
844time_t periodic_ra(time_t now)
845{
846 struct search_param param;
847 struct dhcp_context *context;
848 time_t next_event;
Neil Jerram9bad3392015-06-10 22:16:35 +0100849 struct alias_param aparam;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100850
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000851 param.now = now;
Simon Kelley29d28dd2012-12-03 14:05:59 +0000852 param.iface = 0;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000853
854 while (1)
855 {
856 /* find overdue events, and time of first future event */
Simon Kelley1f776932012-12-16 19:46:08 +0000857 for (next_event = 0, context = daemon->dhcp6; context; context = context->next)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000858 if (context->ra_time != 0)
859 {
Simon Kelley7dbe9812012-03-25 14:49:54 +0100860 if (difftime(context->ra_time, now) <= 0.0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000861 break; /* overdue */
862
Simon Kelley7dbe9812012-03-25 14:49:54 +0100863 if (next_event == 0 || difftime(next_event, context->ra_time) > 0.0)
864 next_event = context->ra_time;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000865 }
866
867 /* none overdue */
868 if (!context)
869 break;
870
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100871 if ((context->flags & CONTEXT_OLD) &&
872 context->if_index != 0 &&
Simon Kelleydd9d9ce2013-11-15 11:24:00 +0000873 indextoname(daemon->icmp6fd, context->if_index, param.name))
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100874 {
875 /* A context for an old address. We'll not find the interface by
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100876 looking for addresses, but we know it anyway, since the context is
877 constructed */
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100878 param.iface = context->if_index;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100879 new_timeout(context, param.name, now);
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100880 }
881 else if (iface_enumerate(AF_INET6, &param, iface_search))
882 /* There's a context overdue, but we can't find an interface
883 associated with it, because it's for a subnet we dont
884 have an interface on. Probably we're doing DHCP on
885 a remote subnet via a relay. Zero the timer, since we won't
Josh Soref730c6742017-02-06 16:14:04 +0000886 ever be able to send ra's and satisfy it. */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000887 context->ra_time = 0;
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100888
889 if (param.iface != 0 &&
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100890 iface_check(AF_LOCAL, NULL, param.name, NULL))
Simon Kelley421594f2012-12-02 12:17:35 +0000891 {
892 struct iname *tmp;
893 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100894 if (tmp->name && wildcard_match(tmp->name, param.name))
Simon Kelley421594f2012-12-02 12:17:35 +0000895 break;
896 if (!tmp)
Neil Jerram9bad3392015-06-10 22:16:35 +0100897 {
Tarun Kundu024baf12022-06-28 10:56:47 -0700898 struct iname *if_tmp;
899 for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next)
900 {
Tarun Kundu74fb2b62022-07-06 09:11:18 -0700901 if ((strcmp(if_tmp->name, param.name) == 0) &&
902 ((if_tmp->is_ra_adv_dis == false) ||
tarun.kundu72971982022-11-10 08:42:59 -0800903 (if_tmp->final_ra_sent_count < MAX_FINAL_RTR_ADVERTISEMENTS)) &&
904 (if_tmp->first_ra_delayed)) {
Tarun Kundu024baf12022-06-28 10:56:47 -0700905 send_ra(now, param.iface, param.name, NULL);
Neil Jerram9bad3392015-06-10 22:16:35 +0100906
Tarun Kundu024baf12022-06-28 10:56:47 -0700907 /* Also send on all interfaces that are aliases of this
Neil Jerram9bad3392015-06-10 22:16:35 +0100908 one. */
Tarun Kundu024baf12022-06-28 10:56:47 -0700909 for (aparam.bridge = daemon->bridges;
Neil Jerram9bad3392015-06-10 22:16:35 +0100910 aparam.bridge;
911 aparam.bridge = aparam.bridge->next)
Tarun Kundu024baf12022-06-28 10:56:47 -0700912 if ((int)if_nametoindex(aparam.bridge->iface) == param.iface)
Neil Jerram9bad3392015-06-10 22:16:35 +0100913 {
914 /* Count the number of alias interfaces for this
915 'bridge', by calling iface_enumerate with
916 send_ra_to_aliases and NULL alias_ifs. */
917 aparam.iface = param.iface;
918 aparam.alias_ifs = NULL;
919 aparam.num_alias_ifs = 0;
920 iface_enumerate(AF_LOCAL, &aparam, send_ra_to_aliases);
921 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s => %d alias(es)",
922 param.name, daemon->addrbuff, aparam.num_alias_ifs);
923
924 /* Allocate memory to store the alias interface
925 indices. */
926 aparam.alias_ifs = (int *)whine_malloc(aparam.num_alias_ifs *
927 sizeof(int));
928 if (aparam.alias_ifs)
929 {
930 /* Use iface_enumerate again to get the alias
931 interface indices, then send on each of
932 those. */
933 aparam.max_alias_ifs = aparam.num_alias_ifs;
934 aparam.num_alias_ifs = 0;
935 iface_enumerate(AF_LOCAL, &aparam, send_ra_to_aliases);
936 for (; aparam.num_alias_ifs; aparam.num_alias_ifs--)
937 {
938 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s => i/f %d",
939 param.name, daemon->addrbuff,
940 aparam.alias_ifs[aparam.num_alias_ifs - 1]);
941 send_ra_alias(now,
942 param.iface,
943 param.name,
944 NULL,
945 aparam.alias_ifs[aparam.num_alias_ifs - 1]);
946 }
947 free(aparam.alias_ifs);
948 }
949
950 /* The source interface can only appear in at most
Neil Jerram4918bd52015-06-10 22:23:20 +0100951 one --bridge-interface. */
Neil Jerram9bad3392015-06-10 22:16:35 +0100952 break;
953 }
tarun.kundu72971982022-11-10 08:42:59 -0800954 } else {
955 if_tmp->first_ra_delayed = true;
Tarun Kundu024baf12022-06-28 10:56:47 -0700956 }
957 }
Neil Jerram9bad3392015-06-10 22:16:35 +0100958 }
Simon Kelley421594f2012-12-02 12:17:35 +0000959 }
960 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000961 return next_event;
962}
Neil Jerram9bad3392015-06-10 22:16:35 +0100963
964static int send_ra_to_aliases(int index, unsigned int type, char *mac, size_t maclen, void *parm)
965{
966 struct alias_param *aparam = (struct alias_param *)parm;
967 char ifrn_name[IFNAMSIZ];
968 struct dhcp_bridge *alias;
969
970 (void)type;
971 (void)mac;
972 (void)maclen;
973
974 if (if_indextoname(index, ifrn_name))
975 for (alias = aparam->bridge->alias; alias; alias = alias->next)
976 if (wildcard_matchn(alias->iface, ifrn_name, IFNAMSIZ))
977 {
978 if (aparam->alias_ifs && (aparam->num_alias_ifs < aparam->max_alias_ifs))
979 aparam->alias_ifs[aparam->num_alias_ifs] = index;
980 aparam->num_alias_ifs++;
981 }
982
983 return 1;
984}
985
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000986static int iface_search(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +0000987 int scope, int if_index, int flags,
Simon Kelley1f776932012-12-16 19:46:08 +0000988 int preferred, int valid, void *vparam)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000989{
990 struct search_param *param = vparam;
Simon Kelley741c2952012-02-25 13:09:18 +0000991 struct dhcp_context *context;
Simon Kelleycd672932020-01-27 22:53:07 +0000992 struct iname *tmp;
993
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000994 (void)scope;
Simon Kelley1f776932012-12-16 19:46:08 +0000995 (void)preferred;
996 (void)valid;
Simon Kelleycd672932020-01-27 22:53:07 +0000997
998 /* ignore interfaces we're not doing DHCP on. */
999 if (!indextoname(daemon->icmp6fd, if_index, param->name) ||
1000 !iface_check(AF_LOCAL, NULL, param->name, NULL))
1001 return 1;
1002
1003 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
1004 if (tmp->name && wildcard_match(tmp->name, param->name))
1005 return 1;
1006
Simon Kelley1f776932012-12-16 19:46:08 +00001007 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelleyef1a94a2013-07-26 13:59:03 +01001008 if (!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) &&
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +00001009 prefix <= context->prefix &&
1010 is_same_net6(local, &context->start6, context->prefix) &&
1011 is_same_net6(local, &context->end6, context->prefix) &&
Simon Kelley6e3dba32012-12-16 21:52:45 +00001012 context->ra_time != 0 &&
1013 difftime(context->ra_time, param->now) <= 0.0)
1014 {
1015 /* found an interface that's overdue for RA determine new
1016 timeout value and arrange for RA to be sent unless interface is
1017 still doing DAD.*/
Simon Kelleybad7b872012-12-20 22:00:39 +00001018 if (!(flags & IFACE_TENTATIVE))
Simon Kelley6e3dba32012-12-16 21:52:45 +00001019 param->iface = if_index;
1020
Simon Kelleyc4cd95d2013-10-10 20:58:11 +01001021 new_timeout(context, param->name, param->now);
Simon Kelley6e3dba32012-12-16 21:52:45 +00001022
1023 /* zero timers for other contexts on the same subnet, so they don't timeout
1024 independently */
1025 for (context = context->next; context; context = context->next)
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +00001026 if (prefix <= context->prefix &&
1027 is_same_net6(local, &context->start6, context->prefix) &&
1028 is_same_net6(local, &context->end6, context->prefix))
Simon Kelley6e3dba32012-12-16 21:52:45 +00001029 context->ra_time = 0;
1030
1031 return 0; /* found, abort */
1032 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +00001033
1034 return 1; /* keep searching */
1035}
Simon Kelleyef1a94a2013-07-26 13:59:03 +01001036
Simon Kelleyc4cd95d2013-10-10 20:58:11 +01001037static void new_timeout(struct dhcp_context *context, char *iface_name, time_t now)
Simon Kelleyef1a94a2013-07-26 13:59:03 +01001038{
tarun.kundu72971982022-11-10 08:42:59 -08001039 struct ra_interface *ra = find_iface_param(iface_name);
tarun.kundua0e036f2022-12-14 07:02:58 -08001040 if (ra && ra->ra_sent_count < (MAX_INITIAL_RTR_ADVERTISEMENTS-1))
tarun.kundu72971982022-11-10 08:42:59 -08001041 context->ra_time = now + MAX_INITIAL_RTR_ADVERT_INTERVAL;
Simon Kelleyef1a94a2013-07-26 13:59:03 +01001042 else
Simon Kelleyc4cd95d2013-10-10 20:58:11 +01001043 {
tarun.kundu72971982022-11-10 08:42:59 -08001044 unsigned int adv_interval = calc_interval(ra);
1045 context->ra_time = now + adv_interval;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +01001046 }
1047}
1048
1049static struct ra_interface *find_iface_param(char *iface)
1050{
1051 struct ra_interface *ra;
1052
1053 for (ra = daemon->ra_interfaces; ra; ra = ra->next)
1054 if (wildcard_match(ra->name, iface))
1055 return ra;
1056
1057 return NULL;
1058}
1059
1060static unsigned int calc_interval(struct ra_interface *ra)
1061{
Tarun Kundu74fb2b62022-07-06 09:11:18 -07001062 int interval = DEFAULT_RTR_ADV_INTERVAL;
Tarun Kundu36e87102022-07-20 14:43:44 -07001063 if (ra) {
Tarun Kundu74fb2b62022-07-06 09:11:18 -07001064 unsigned int min_rtr_interval = ra->min_ra_adv_interval;
1065 unsigned int max_rtr_interval = ra->max_ra_adv_interval;
1066
1067 if (max_rtr_interval > MAX_RTR_ADV_INTERVAL_UPPER_BOUND)
1068 max_rtr_interval = MAX_RTR_ADV_INTERVAL_UPPER_BOUND;
1069 else if (max_rtr_interval < MAX_RTR_ADV_INTERVAL_LOWER_BOUND)
1070 max_rtr_interval = MAX_RTR_ADV_INTERVAL_LOWER_BOUND;
1071
1072 if (min_rtr_interval < MIN_RTR_ADV_INTERVAL)
1073 min_rtr_interval = MIN_RTR_ADV_INTERVAL;
1074
1075 interval = (rand() % (max_rtr_interval - min_rtr_interval + 1)) + min_rtr_interval;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +01001076
Tarun Kundu74fb2b62022-07-06 09:11:18 -07001077 if (((ra->ra_sent_count + 1) < MAX_INITIAL_RTR_ADVERTISEMENTS) &&
1078 interval > MAX_INITIAL_RTR_ADVERT_INTERVAL)
tarun.kundu72971982022-11-10 08:42:59 -08001079 interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
Tarun Kundu36e87102022-07-20 14:43:44 -07001080 }
Simon Kelleyc4cd95d2013-10-10 20:58:11 +01001081 return (unsigned int)interval;
1082}
1083
1084static unsigned int calc_lifetime(struct ra_interface *ra)
1085{
1086 int lifetime, interval = (int)calc_interval(ra);
1087
1088 if (!ra || ra->lifetime == -1) /* not specified */
1089 lifetime = 3 * interval;
1090 else
1091 {
1092 lifetime = ra->lifetime;
1093 if (lifetime < interval && lifetime != 0)
1094 lifetime = interval;
1095 else if (lifetime > 9000)
1096 lifetime = 9000;
1097 }
1098
1099 return (unsigned int)lifetime;
1100}
1101
1102static unsigned int calc_prio(struct ra_interface *ra)
1103{
1104 if (ra)
1105 return ra->prio;
1106
1107 return 0;
Simon Kelleyef1a94a2013-07-26 13:59:03 +01001108}
Simon Kelley801ca9a2012-03-06 19:30:17 +00001109
Tarun Kundu024baf12022-06-28 10:56:47 -07001110static unsigned char calc_hop_limit(struct ra_interface *ra)
1111{
1112 if (ra) {
1113 return ra->hop_limit;
1114 }
1115 return (unsigned char)hop_limit;
1116}
1117
Tarun Kundu41aa4882022-07-05 10:44:13 -07001118static unsigned int calc_retrans_time(struct ra_interface *ra)
1119{
1120 if (ra) {
1121 return ra->retrans_time;
1122 }
1123 return 0;
1124}
1125
Tarun Kundu024baf12022-06-28 10:56:47 -07001126static unsigned int calc_reachable_time(struct ra_interface *ra)
1127{
1128 if (ra) {
1129 return ra->reachable_time;
1130 }
1131 return 0;
1132}
Tarun Kundu01293a72022-07-18 23:50:36 -07001133
1134int64_t timespecdiff(struct timespec const *a, struct timespec const *b)
1135{
1136 int64_t msec;
1137 msec = ((int64_t)a->tv_sec - b->tv_sec) * 1000;
1138 msec += ((int64_t)a->tv_nsec - b->tv_nsec) / (1000 * 1000);
1139 return msec;
1140}
Simon Kelleyc5ad4e72012-02-24 16:06:20 +00001141#endif