Simon Kelley | c8e8f5c | 2021-01-24 21:59:37 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2021 Simon Kelley |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 2 | |
| 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 Kelley | 843c96b | 2012-02-27 17:42:38 +0000 | [diff] [blame] | 18 | /* NB. This code may be called during a DHCPv4 or transaction which is in ping-wait |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 19 | It therefore cannot use any DHCP buffer resources except outpacket, which is |
Simon Kelley | 843c96b | 2012-02-27 17:42:38 +0000 | [diff] [blame] | 20 | 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 Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 22 | |
| 23 | #include "dnsmasq.h" |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 24 | |
| 25 | #ifdef HAVE_DHCP6 |
| 26 | |
Simon Kelley | 22d904d | 2012-02-26 20:13:45 +0000 | [diff] [blame] | 27 | #include <netinet/icmp6.h> |
| 28 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 29 | struct ra_param { |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 30 | time_t now; |
S L | a18bf31 | 2016-02-12 17:36:20 +0000 | [diff] [blame] | 31 | int ind, managed, other, first, adv_router; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 32 | char *if_name; |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 33 | struct dhcp_netid *tags; |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 34 | struct in6_addr link_local, link_global, ula; |
Ilya Ponetaev | 5bf50af | 2014-09-09 12:46:21 +0100 | [diff] [blame] | 35 | unsigned int glob_pref_time, link_pref_time, ula_pref_time, adv_interval, prio; |
S L | a18bf31 | 2016-02-12 17:36:20 +0000 | [diff] [blame] | 36 | struct dhcp_context *found_context; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | struct search_param { |
| 40 | time_t now; int iface; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 41 | char name[IF_NAMESIZE+1]; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 44 | struct 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 Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 52 | static void send_ra(time_t now, int iface, char *iface_name, struct in6_addr *dest); |
Neil Jerram | ba4fc0f | 2015-06-10 22:14:49 +0100 | [diff] [blame] | 53 | static void send_ra_alias(time_t now, int iface, char *iface_name, struct in6_addr *dest, |
| 54 | int send_iface); |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 55 | static int send_ra_to_aliases(int index, unsigned int type, char *mac, size_t maclen, void *parm); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 56 | static int add_prefixes(struct in6_addr *local, int prefix, |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 57 | int scope, int if_index, int flags, |
Simon Kelley | 55b42f6 | 2012-12-21 16:53:15 +0000 | [diff] [blame] | 58 | unsigned int preferred, unsigned int valid, void *vparam); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 59 | static int iface_search(struct in6_addr *local, int prefix, |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 60 | int scope, int if_index, int flags, |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 61 | int prefered, int valid, void *vparam); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 62 | static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm); |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 63 | static void new_timeout(struct dhcp_context *context, char *iface_name, time_t now); |
| 64 | static unsigned int calc_lifetime(struct ra_interface *ra); |
| 65 | static unsigned int calc_interval(struct ra_interface *ra); |
| 66 | static unsigned int calc_prio(struct ra_interface *ra); |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 67 | static unsigned char calc_hop_limit(struct ra_interface *ra); |
| 68 | static unsigned int calc_reachable_time(struct ra_interface *ra); |
Tarun Kundu | 41aa488 | 2022-07-05 10:44:13 -0700 | [diff] [blame] | 69 | static unsigned int calc_retrans_time(struct ra_interface *ra); |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 70 | static struct ra_interface *find_iface_param(char *iface); |
Tarun Kundu | 01293a7 | 2022-07-18 23:50:36 -0700 | [diff] [blame] | 71 | int64_t timespecdiff(struct timespec const *a, struct timespec const *b); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 72 | |
Simon Kelley | c5379c1 | 2012-02-24 20:05:52 +0000 | [diff] [blame] | 73 | static int hop_limit; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 74 | |
| 75 | void ra_init(time_t now) |
| 76 | { |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 77 | struct icmp6_filter filter; |
| 78 | int fd; |
Simon Kelley | 0e88d53 | 2012-03-28 22:22:05 +0100 | [diff] [blame] | 79 | #if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 80 | int class = IPTOS_CLASS_CS6; |
| 81 | #endif |
| 82 | int val = 255; /* radvd uses this value */ |
tarun.kundu | dc8b7b4 | 2022-11-14 17:12:23 -0800 | [diff] [blame^] | 83 | #ifdef IPV6_RECVHOPLIMIT |
| 84 | int on_ipv6_recvhoplimit = 1; |
| 85 | #endif |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 86 | socklen_t len = sizeof(hop_limit); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 87 | struct dhcp_context *context; |
| 88 | |
Simon Kelley | 843c96b | 2012-02-27 17:42:38 +0000 | [diff] [blame] | 89 | /* ensure this is around even if we're not doing DHCPv6 */ |
| 90 | expand_buf(&daemon->outpacket, sizeof(struct dhcp_packet)); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 91 | |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 92 | /* See if we're guessing SLAAC addresses, if so we need to receive ping replies */ |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 93 | for (context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 94 | if ((context->flags & CONTEXT_RA_NAME)) |
| 95 | break; |
| 96 | |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 97 | /* Need ICMP6 socket for transmission for DHCPv6 even when not doing RA. */ |
| 98 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 99 | ICMP6_FILTER_SETBLOCKALL(&filter); |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 100 | 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 Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 106 | |
| 107 | if ((fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1 || |
Simon Kelley | c5379c1 | 2012-02-24 20:05:52 +0000 | [diff] [blame] | 108 | getsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hop_limit, &len) || |
Simon Kelley | 0e88d53 | 2012-03-28 22:22:05 +0100 | [diff] [blame] | 109 | #if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 110 | 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.kundu | dc8b7b4 | 2022-11-14 17:12:23 -0800 | [diff] [blame^] | 116 | #ifdef IPV6_RECVHOPLIMIT |
| 117 | setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on_ipv6_recvhoplimit, sizeof(on_ipv6_recvhoplimit)) || |
| 118 | #endif |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 119 | 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 Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 124 | if (daemon->doing_ra) |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 125 | ra_start_unsolicited(now, NULL); |
Tarun Kundu | 15048b5 | 2022-07-05 12:16:58 -0700 | [diff] [blame] | 126 | |
| 127 | /* Intializes random number generator */ |
| 128 | srand(time(0)); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 131 | void ra_start_unsolicited(time_t now, struct dhcp_context *context) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 132 | { |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 133 | /* init timers so that we do ra's for some/all soon. some ra_times will end up zeroed |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 134 | 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 Kelley | 6e3dba3 | 2012-12-16 21:52:45 +0000 | [diff] [blame] | 137 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 138 | if (context) |
Simon Kelley | 1b75c1e | 2012-12-18 19:55:25 +0000 | [diff] [blame] | 139 | context->ra_short_period_start = context->ra_time = now; |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 140 | else |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 141 | for (context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | 6e3dba3 | 2012-12-16 21:52:45 +0000 | [diff] [blame] | 142 | if (!(context->flags & CONTEXT_TEMPLATE)) |
Simon Kelley | 1b75c1e | 2012-12-18 19:55:25 +0000 | [diff] [blame] | 143 | { |
| 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 Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 150 | void icmp6_packet(time_t now) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 151 | { |
| 152 | char interface[IF_NAMESIZE+1]; |
| 153 | ssize_t sz; |
| 154 | int if_index = 0; |
tarun.kundu | dc8b7b4 | 2022-11-14 17:12:23 -0800 | [diff] [blame^] | 155 | int hoplimit = 255; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 156 | struct cmsghdr *cmptr; |
| 157 | struct msghdr msg; |
| 158 | union { |
tarun.kundu | dc8b7b4 | 2022-11-14 17:12:23 -0800 | [diff] [blame^] | 159 | 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 Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 163 | } control_u; |
| 164 | struct sockaddr_in6 from; |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 165 | unsigned char *packet; |
Tarun Kundu | 01293a7 | 2022-07-18 23:50:36 -0700 | [diff] [blame] | 166 | struct iname *tmp, *if_tmp; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 167 | |
| 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 Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 179 | |
| 180 | packet = (unsigned char *)daemon->outpacket.iov_base; |
tarun.kundu | dc8b7b4 | 2022-11-14 17:12:23 -0800 | [diff] [blame^] | 181 | 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 Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 190 | 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.kundu | dc8b7b4 | 2022-11-14 17:12:23 -0800 | [diff] [blame^] | 200 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 201 | |
| 202 | if (!indextoname(daemon->icmp6fd, if_index, interface)) |
| 203 | return; |
| 204 | |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 205 | if (!iface_check(AF_LOCAL, NULL, interface, NULL)) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 206 | return; |
| 207 | |
| 208 | for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
Simon Kelley | 49333cb | 2013-03-15 20:30:51 +0000 | [diff] [blame] | 209 | if (tmp->name && wildcard_match(tmp->name, interface)) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 210 | return; |
| 211 | |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 212 | if (packet[1] != 0) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 213 | return; |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 214 | |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 215 | if (packet[0] == ICMP6_ECHO_REPLY) |
| 216 | lease_ping_reply(&from.sin6_addr, packet, interface); |
tarun.kundu | dc8b7b4 | 2022-11-14 17:12:23 -0800 | [diff] [blame^] | 217 | else if ((packet[0] == ND_ROUTER_SOLICIT) && (hoplimit == 255)) |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 218 | { |
| 219 | inet_ntop(AF_INET6, &from.sin6_addr, daemon->addrbuff, ADDRSTRLEN); |
| 220 | if ((!((strncmp(daemon->addrbuff, "::",2)==0) && (packet[8] == ICMP6_OPT_SOURCE_MAC))) || |
| 221 | (!(packet[9] == 0))) |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 222 | { |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 223 | char *mac = ""; |
Neil Jerram | ba4fc0f | 2015-06-10 22:14:49 +0100 | [diff] [blame] | 224 | struct dhcp_bridge *bridge, *alias; |
Tarun Kundu | 01293a7 | 2022-07-18 23:50:36 -0700 | [diff] [blame] | 225 | struct timespec ts, ts_delay; |
Tarun Kundu | 15048b5 | 2022-07-05 12:16:58 -0700 | [diff] [blame] | 226 | int tms = 0; |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 227 | |
| 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 Kelley | 24036ea | 2017-09-25 18:47:15 +0100 | [diff] [blame] | 231 | if ((packet[9] * 8 - 2) * 3 - 1 >= MAXDNAME) { |
| 232 | return; |
| 233 | } |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 234 | print_mac(daemon->namebuff, &packet[10], (packet[9] * 8) - 2); |
| 235 | mac = daemon->namebuff; |
| 236 | } |
| 237 | |
Kevin Darbyshire-Bryant | 8c0b73d | 2013-10-11 11:56:33 +0100 | [diff] [blame] | 238 | if (!option_bool(OPT_QUIET_RA)) |
| 239 | my_syslog(MS_DHCP | LOG_INFO, "RTR-SOLICIT(%s) %s", interface, mac); |
Neil Jerram | ba4fc0f | 2015-06-10 22:14:49 +0100 | [diff] [blame] | 240 | |
Tarun Kundu | 01293a7 | 2022-07-18 23:50:36 -0700 | [diff] [blame] | 241 | 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 Jerram | ba4fc0f | 2015-06-10 22:14:49 +0100 | [diff] [blame] | 255 | /* If the incoming interface is an alias of some other one (as |
Neil Jerram | 4918bd5 | 2015-06-10 22:23:20 +0100 | [diff] [blame] | 256 | specified by the --bridge-interface option), send an RA using |
| 257 | the context of the aliased interface. */ |
Neil Jerram | ba4fc0f | 2015-06-10 22:14:49 +0100 | [diff] [blame] | 258 | 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 Kundu | 15048b5 | 2022-07-05 12:16:58 -0700 | [diff] [blame] | 281 | 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 Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 285 | } |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 286 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Neil Jerram | ba4fc0f | 2015-06-10 22:14:49 +0100 | [diff] [blame] | 289 | static void send_ra_alias(time_t now, int iface, char *iface_name, struct in6_addr *dest, int send_iface) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 290 | { |
| 291 | struct ra_packet *ra; |
| 292 | struct ra_param parm; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 293 | struct sockaddr_in6 addr; |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 294 | struct dhcp_context *context, *tmp, **up; |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 295 | struct dhcp_netid iface_id; |
| 296 | struct dhcp_opt *opt_cfg; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 297 | struct ra_interface *ra_param = find_iface_param(iface_name); |
David Flamand | 005c46d | 2017-04-11 11:49:54 +0100 | [diff] [blame] | 298 | int done_dns = 0, old_prefix = 0, mtu = 0; |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 299 | unsigned int min_pref_time; |
Simon Kelley | 3b43646 | 2012-12-28 11:55:45 +0000 | [diff] [blame] | 300 | #ifdef HAVE_LINUX_NETWORK |
| 301 | FILE *f; |
| 302 | #endif |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 303 | |
| 304 | struct iname *if_tmp; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 305 | parm.ind = iface; |
| 306 | parm.managed = 0; |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 307 | parm.other = 0; |
S L | a18bf31 | 2016-02-12 17:36:20 +0000 | [diff] [blame] | 308 | parm.found_context = NULL; |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 309 | parm.adv_router = 0; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 310 | parm.if_name = iface_name; |
| 311 | parm.first = 1; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 312 | parm.now = now; |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 313 | parm.glob_pref_time = parm.link_pref_time = parm.ula_pref_time = 0; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 314 | parm.adv_interval = calc_interval(ra_param); |
Ilya Ponetaev | 5bf50af | 2014-09-09 12:46:21 +0100 | [diff] [blame] | 315 | parm.prio = calc_prio(ra_param); |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 316 | |
Simon Kelley | fa78573 | 2016-07-22 20:56:01 +0100 | [diff] [blame] | 317 | reset_counter(); |
Simon Kelley | ce7845b | 2016-07-06 21:42:27 +0100 | [diff] [blame] | 318 | |
| 319 | if (!(ra = expand(sizeof(struct ra_packet)))) |
| 320 | return; |
Ilya Ponetaev | 5bf50af | 2014-09-09 12:46:21 +0100 | [diff] [blame] | 321 | |
| 322 | ra->type = ND_ROUTER_ADVERT; |
| 323 | ra->code = 0; |
Ilya Ponetaev | 5bf50af | 2014-09-09 12:46:21 +0100 | [diff] [blame] | 324 | ra->flags = parm.prio; |
| 325 | ra->lifetime = htons(calc_lifetime(ra_param)); |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 326 | ra->reachable_time = htonl(calc_reachable_time(ra_param)); |
| 327 | ra->hop_limit = calc_hop_limit(ra_param); |
Tarun Kundu | 41aa488 | 2022-07-05 10:44:13 -0700 | [diff] [blame] | 328 | ra->retrans_time = htonl(calc_retrans_time(ra_param)); |
Ilya Ponetaev | 5bf50af | 2014-09-09 12:46:21 +0100 | [diff] [blame] | 329 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 330 | /* set tag with name == interface */ |
| 331 | iface_id.net = iface_name; |
| 332 | iface_id.next = NULL; |
| 333 | parm.tags = &iface_id; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 334 | |
Tarun Kundu | 74fb2b6 | 2022-07-06 09:11:18 -0700 | [diff] [blame] | 335 | for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next) |
| 336 | { |
| 337 | if ((strcmp(if_tmp->name, iface_name) == 0) && (if_tmp->is_ra_adv_dis == true)) { |
| 338 | if (if_tmp->final_ra_sent_count < MAX_FINAL_RTR_ADVERTISEMENTS) { |
| 339 | ra->lifetime = htons(0); |
| 340 | if_tmp->final_ra_sent_count++; |
| 341 | break; |
| 342 | } |
| 343 | else { |
| 344 | return; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 349 | for (context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 350 | { |
| 351 | context->flags &= ~CONTEXT_RA_DONE; |
| 352 | context->netid.next = &context->netid; |
| 353 | } |
| 354 | |
Simon Kelley | 378fa56 | 2020-01-05 17:23:19 +0000 | [diff] [blame] | 355 | /* If no link-local address then we can't advertise since source address of |
| 356 | advertisement must be link local address: RFC 4861 para 6.1.2. */ |
| 357 | if (!iface_enumerate(AF_INET6, &parm, add_prefixes) || |
| 358 | parm.link_pref_time == 0) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 359 | return; |
| 360 | |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 361 | /* Find smallest preferred time within address classes, |
| 362 | to use as lifetime for options. This is a rather arbitrary choice. */ |
| 363 | min_pref_time = 0xffffffff; |
| 364 | if (parm.glob_pref_time != 0 && parm.glob_pref_time < min_pref_time) |
| 365 | min_pref_time = parm.glob_pref_time; |
| 366 | |
| 367 | if (parm.ula_pref_time != 0 && parm.ula_pref_time < min_pref_time) |
| 368 | min_pref_time = parm.ula_pref_time; |
| 369 | |
| 370 | if (parm.link_pref_time != 0 && parm.link_pref_time < min_pref_time) |
| 371 | min_pref_time = parm.link_pref_time; |
| 372 | |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 373 | /* Look for constructed contexts associated with addresses which have gone, |
| 374 | and advertise them with preferred_time == 0 RFC 6204 4.3 L-13 */ |
| 375 | for (up = &daemon->dhcp6, context = daemon->dhcp6; context; context = tmp) |
| 376 | { |
| 377 | tmp = context->next; |
Simon Kelley | 3b43646 | 2012-12-28 11:55:45 +0000 | [diff] [blame] | 378 | |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 379 | if (context->if_index == iface && (context->flags & CONTEXT_OLD)) |
| 380 | { |
| 381 | unsigned int old = difftime(now, context->address_lost_time); |
| 382 | |
| 383 | if (old > context->saved_valid) |
S L | a18bf31 | 2016-02-12 17:36:20 +0000 | [diff] [blame] | 384 | { |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 385 | /* We've advertised this enough, time to go */ |
S L | a18bf31 | 2016-02-12 17:36:20 +0000 | [diff] [blame] | 386 | |
| 387 | /* If this context held the timeout, and there's another context in use |
| 388 | transfer the timeout there. */ |
| 389 | if (context->ra_time != 0 && parm.found_context && parm.found_context->ra_time == 0) |
| 390 | new_timeout(parm.found_context, iface_name, now); |
| 391 | |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 392 | *up = context->next; |
| 393 | free(context); |
| 394 | } |
| 395 | else |
| 396 | { |
| 397 | struct prefix_opt *opt; |
| 398 | struct in6_addr local = context->start6; |
| 399 | int do_slaac = 0; |
| 400 | |
Simon Kelley | 4452292 | 2013-11-15 14:45:04 +0000 | [diff] [blame] | 401 | old_prefix = 1; |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 402 | |
| 403 | /* zero net part of address */ |
| 404 | setaddr6part(&local, addr6part(&local) & ~((context->prefix == 64) ? (u64)-1LL : (1LLU << (128 - context->prefix)) - 1LLU)); |
| 405 | |
Simon Kelley | 6ea1f23 | 2013-12-19 15:45:12 +0000 | [diff] [blame] | 406 | |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 407 | if (context->flags & CONTEXT_RA) |
Simon Kelley | 6ea1f23 | 2013-12-19 15:45:12 +0000 | [diff] [blame] | 408 | { |
| 409 | do_slaac = 1; |
| 410 | if (context->flags & CONTEXT_DHCP) |
| 411 | { |
| 412 | parm.other = 1; |
| 413 | if (!(context->flags & CONTEXT_RA_STATELESS)) |
| 414 | parm.managed = 1; |
| 415 | } |
| 416 | } |
| 417 | else |
| 418 | { |
| 419 | /* don't do RA for non-ra-only unless --enable-ra is set */ |
| 420 | if (option_bool(OPT_RA)) |
| 421 | { |
| 422 | parm.managed = 1; |
| 423 | parm.other = 1; |
| 424 | } |
| 425 | } |
| 426 | |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 427 | if ((opt = expand(sizeof(struct prefix_opt)))) |
| 428 | { |
| 429 | opt->type = ICMP6_OPT_PREFIX; |
| 430 | opt->len = 4; |
| 431 | opt->prefix_len = context->prefix; |
Neil Jerram | 2fd5bc9 | 2015-06-10 22:13:06 +0100 | [diff] [blame] | 432 | /* autonomous only if we're not doing dhcp, set |
| 433 | "on-link" unless "off-link" was specified */ |
| 434 | opt->flags = (do_slaac ? 0x40 : 0) | |
tarun.kundu | 6603119 | 2022-11-10 14:39:45 -0800 | [diff] [blame] | 435 | ((context->flags & CONTEXT_RA_OFF_LINK) ? 0 : 0x80) | |
| 436 | ((context->flags & CONTEXT_RA_AUTO_OFF) ? 0 : 0x40); |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 437 | opt->valid_lifetime = htonl(context->saved_valid - old); |
| 438 | opt->preferred_lifetime = htonl(0); |
| 439 | opt->reserved = 0; |
| 440 | opt->prefix = local; |
| 441 | |
| 442 | inet_ntop(AF_INET6, &local, daemon->addrbuff, ADDRSTRLEN); |
Kevin Darbyshire-Bryant | 8c0b73d | 2013-10-11 11:56:33 +0100 | [diff] [blame] | 443 | if (!option_bool(OPT_QUIET_RA)) |
| 444 | my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s old prefix", iface_name, daemon->addrbuff); |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | up = &context->next; |
| 448 | } |
| 449 | } |
| 450 | else |
| 451 | up = &context->next; |
| 452 | } |
| 453 | |
Simon Kelley | 4452292 | 2013-11-15 14:45:04 +0000 | [diff] [blame] | 454 | /* If we're advertising only old prefixes, set router lifetime to zero. */ |
| 455 | if (old_prefix && !parm.found_context) |
| 456 | ra->lifetime = htons(0); |
| 457 | |
| 458 | /* No prefixes to advertise. */ |
| 459 | if (!old_prefix && !parm.found_context) |
| 460 | return; |
| 461 | |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 462 | /* If we're sending router address instead of prefix in at least on prefix, |
| 463 | include the advertisement interval option. */ |
| 464 | if (parm.adv_router) |
| 465 | { |
| 466 | put_opt6_char(ICMP6_OPT_ADV_INTERVAL); |
| 467 | put_opt6_char(1); |
| 468 | put_opt6_short(0); |
| 469 | /* interval value is in milliseconds */ |
| 470 | put_opt6_long(1000 * calc_interval(find_iface_param(iface_name))); |
| 471 | } |
| 472 | |
David Flamand | 005c46d | 2017-04-11 11:49:54 +0100 | [diff] [blame] | 473 | /* Set the MTU from ra_param if any, an MTU of 0 mean automatic for linux, */ |
| 474 | /* an MTU of -1 prevents the option from being sent. */ |
Simon Kelley | 3b43646 | 2012-12-28 11:55:45 +0000 | [diff] [blame] | 475 | #ifdef HAVE_LINUX_NETWORK |
Ville Skyttä | faaf306 | 2018-01-14 17:32:52 +0000 | [diff] [blame] | 476 | /* Note that IPv6 MTU is not necessarily the same as the IPv4 MTU |
Simon Kelley | 3b43646 | 2012-12-28 11:55:45 +0000 | [diff] [blame] | 477 | available from SIOCGIFMTU */ |
Vladislav Grishenko | 6ec5f5c | 2017-04-24 22:34:45 +0100 | [diff] [blame] | 478 | char *mtu_name = ra_param ? ra_param->mtu_name : NULL; |
Simon Kelley | 5ed82ae | 2019-03-05 16:38:34 +0000 | [diff] [blame] | 479 | sprintf(daemon->namebuff, "/proc/sys/net/ipv6/conf/%s/mtu", mtu_name ? mtu_name : iface_name); |
David Flamand | 005c46d | 2017-04-11 11:49:54 +0100 | [diff] [blame] | 480 | if ((f = fopen(daemon->namebuff, "r"))) |
| 481 | { |
| 482 | if (fgets(daemon->namebuff, MAXDNAME, f)) |
| 483 | mtu = atoi(daemon->namebuff); |
| 484 | fclose(f); |
| 485 | } |
Simon Kelley | 3b43646 | 2012-12-28 11:55:45 +0000 | [diff] [blame] | 486 | #endif |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 487 | if (ra_param) |
| 488 | mtu = ra_param->mtu; |
David Flamand | 005c46d | 2017-04-11 11:49:54 +0100 | [diff] [blame] | 489 | if (mtu > 0) |
| 490 | { |
| 491 | put_opt6_char(ICMP6_OPT_MTU); |
| 492 | put_opt6_char(1); |
| 493 | put_opt6_short(0); |
| 494 | put_opt6_long(mtu); |
| 495 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 496 | |
Neil Jerram | ba4fc0f | 2015-06-10 22:14:49 +0100 | [diff] [blame] | 497 | iface_enumerate(AF_LOCAL, &send_iface, add_lla); |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 498 | |
| 499 | /* RDNSS, RFC 6106, use relevant DHCP6 options */ |
| 500 | (void)option_filter(parm.tags, NULL, daemon->dhcp_opts6); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 501 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 502 | for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next) |
| 503 | { |
| 504 | int i; |
| 505 | |
| 506 | /* netids match and not encapsulated? */ |
| 507 | if (!(opt_cfg->flags & DHOPT_TAGOK)) |
| 508 | continue; |
| 509 | |
| 510 | if (opt_cfg->opt == OPTION6_DNS_SERVER) |
| 511 | { |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 512 | struct in6_addr *a; |
| 513 | int len; |
| 514 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 515 | done_dns = 1; |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 516 | |
| 517 | if (opt_cfg->len == 0) |
Simon Kelley | 806cf78 | 2013-10-14 14:08:44 +0100 | [diff] [blame] | 518 | continue; |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 519 | |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 520 | /* reduce len for any addresses we can't substitute */ |
| 521 | for (a = (struct in6_addr *)opt_cfg->val, len = opt_cfg->len, i = 0; |
| 522 | i < opt_cfg->len; i += IN6ADDRSZ, a++) |
| 523 | if ((IN6_IS_ADDR_UNSPECIFIED(a) && parm.glob_pref_time == 0) || |
| 524 | (IN6_IS_ADDR_ULA_ZERO(a) && parm.ula_pref_time == 0) || |
| 525 | (IN6_IS_ADDR_LINK_LOCAL_ZERO(a) && parm.link_pref_time == 0)) |
| 526 | len -= IN6ADDRSZ; |
| 527 | |
| 528 | if (len != 0) |
| 529 | { |
| 530 | put_opt6_char(ICMP6_OPT_RDNSS); |
| 531 | put_opt6_char((len/8) + 1); |
| 532 | put_opt6_short(0); |
| 533 | put_opt6_long(min_pref_time); |
| 534 | |
| 535 | for (a = (struct in6_addr *)opt_cfg->val, i = 0; i < opt_cfg->len; i += IN6ADDRSZ, a++) |
| 536 | if (IN6_IS_ADDR_UNSPECIFIED(a)) |
| 537 | { |
| 538 | if (parm.glob_pref_time != 0) |
| 539 | put_opt6(&parm.link_global, IN6ADDRSZ); |
| 540 | } |
| 541 | else if (IN6_IS_ADDR_ULA_ZERO(a)) |
| 542 | { |
| 543 | if (parm.ula_pref_time != 0) |
| 544 | put_opt6(&parm.ula, IN6ADDRSZ); |
| 545 | } |
| 546 | else if (IN6_IS_ADDR_LINK_LOCAL_ZERO(a)) |
| 547 | { |
| 548 | if (parm.link_pref_time != 0) |
| 549 | put_opt6(&parm.link_local, IN6ADDRSZ); |
| 550 | } |
| 551 | else |
| 552 | put_opt6(a, IN6ADDRSZ); |
| 553 | } |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | if (opt_cfg->opt == OPTION6_DOMAIN_SEARCH && opt_cfg->len != 0) |
| 557 | { |
| 558 | int len = ((opt_cfg->len+7)/8); |
| 559 | |
| 560 | put_opt6_char(ICMP6_OPT_DNSSL); |
| 561 | put_opt6_char(len + 1); |
| 562 | put_opt6_short(0); |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 563 | put_opt6_long(min_pref_time); |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 564 | put_opt6(opt_cfg->val, opt_cfg->len); |
| 565 | |
| 566 | /* pad */ |
| 567 | for (i = opt_cfg->len; i < len * 8; i++) |
| 568 | put_opt6_char(0); |
| 569 | } |
| 570 | } |
| 571 | |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 572 | if (daemon->port == NAMESERVER_PORT && !done_dns && parm.link_pref_time != 0) |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 573 | { |
Simon Kelley | ab915f8 | 2013-04-30 10:41:28 +0100 | [diff] [blame] | 574 | /* default == us, as long as we are supplying DNS service. */ |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 575 | put_opt6_char(ICMP6_OPT_RDNSS); |
| 576 | put_opt6_char(3); |
| 577 | put_opt6_short(0); |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 578 | put_opt6_long(min_pref_time); |
Simon Kelley | 806cf78 | 2013-10-14 14:08:44 +0100 | [diff] [blame] | 579 | put_opt6(&parm.link_local, IN6ADDRSZ); |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 580 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 581 | |
| 582 | /* set managed bits unless we're providing only RA on this link */ |
| 583 | if (parm.managed) |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 584 | ra->flags |= 0x80; /* M flag, managed, */ |
| 585 | if (parm.other) |
| 586 | ra->flags |= 0x40; /* O flag, other */ |
| 587 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 588 | /* decide where we're sending */ |
| 589 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | 22d904d | 2012-02-26 20:13:45 +0000 | [diff] [blame] | 590 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 591 | addr.sin6_len = sizeof(struct sockaddr_in6); |
| 592 | #endif |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 593 | addr.sin6_family = AF_INET6; |
| 594 | addr.sin6_port = htons(IPPROTO_ICMPV6); |
| 595 | if (dest) |
| 596 | { |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 597 | addr.sin6_addr = *dest; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 598 | if (IN6_IS_ADDR_LINKLOCAL(dest) || |
| 599 | IN6_IS_ADDR_MC_LINKLOCAL(dest)) |
| 600 | addr.sin6_scope_id = iface; |
| 601 | } |
| 602 | else |
Simon Kelley | 8f3194f | 2013-09-30 15:04:58 +0100 | [diff] [blame] | 603 | { |
| 604 | inet_pton(AF_INET6, ALL_NODES, &addr.sin6_addr); |
Neil Jerram | ba4fc0f | 2015-06-10 22:14:49 +0100 | [diff] [blame] | 605 | setsockopt(daemon->icmp6fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &send_iface, sizeof(send_iface)); |
Simon Kelley | 8f3194f | 2013-09-30 15:04:58 +0100 | [diff] [blame] | 606 | } |
Simon Kelley | ff841eb | 2015-03-11 21:36:30 +0000 | [diff] [blame] | 607 | while (retry_send(sendto(daemon->icmp6fd, daemon->outpacket.iov_base, |
Simon Kelley | 6b1c464 | 2016-07-22 20:59:16 +0100 | [diff] [blame] | 608 | save_counter(-1), 0, (struct sockaddr *)&addr, |
Simon Kelley | ff841eb | 2015-03-11 21:36:30 +0000 | [diff] [blame] | 609 | sizeof(addr)))); |
Tarun Kundu | 74fb2b6 | 2022-07-06 09:11:18 -0700 | [diff] [blame] | 610 | ra_param->ra_sent_count++; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Neil Jerram | ba4fc0f | 2015-06-10 22:14:49 +0100 | [diff] [blame] | 613 | static void send_ra(time_t now, int iface, char *iface_name, struct in6_addr *dest) |
| 614 | { |
| 615 | /* Send an RA on the same interface that the RA content is based |
| 616 | on. */ |
| 617 | send_ra_alias(now, iface, iface_name, dest, iface); |
| 618 | } |
| 619 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 620 | static int add_prefixes(struct in6_addr *local, int prefix, |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 621 | int scope, int if_index, int flags, |
Simon Kelley | 55b42f6 | 2012-12-21 16:53:15 +0000 | [diff] [blame] | 622 | unsigned int preferred, unsigned int valid, void *vparam) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 623 | { |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 624 | struct ra_param *param = vparam; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 625 | |
| 626 | (void)scope; /* warning */ |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 627 | |
| 628 | struct iname *if_tmp; |
| 629 | for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next) |
| 630 | { |
| 631 | if ((strcmp(if_tmp->name, param->if_name) == 0) && (if_tmp->is_ra_adv_dis == true)) { |
Tarun Kundu | 74fb2b6 | 2022-07-06 09:11:18 -0700 | [diff] [blame] | 632 | if (if_tmp->final_ra_sent_count < MAX_FINAL_RTR_ADVERTISEMENTS) { |
| 633 | break; |
| 634 | } |
| 635 | else { |
| 636 | return 1; |
| 637 | } |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 638 | } |
| 639 | } |
Simon Kelley | ed8b68a | 2012-12-21 16:23:26 +0000 | [diff] [blame] | 640 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 641 | if (if_index == param->ind) |
| 642 | { |
| 643 | if (IN6_IS_ADDR_LINKLOCAL(local)) |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 644 | { |
| 645 | /* Can there be more than one LL address? |
| 646 | Select the one with the longest preferred time |
| 647 | if there is. */ |
| 648 | if (preferred > param->link_pref_time) |
| 649 | { |
| 650 | param->link_pref_time = preferred; |
| 651 | param->link_local = *local; |
| 652 | } |
| 653 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 654 | else if (!IN6_IS_ADDR_LOOPBACK(local) && |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 655 | !IN6_IS_ADDR_MULTICAST(local)) |
| 656 | { |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 657 | int real_prefix = 0; |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 658 | int do_slaac = 0; |
| 659 | int deprecate = 0; |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 660 | int constructed = 0; |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 661 | int adv_router = 0; |
Neil Jerram | 2fd5bc9 | 2015-06-10 22:13:06 +0100 | [diff] [blame] | 662 | int off_link = 0; |
tarun.kundu | 6603119 | 2022-11-10 14:39:45 -0800 | [diff] [blame] | 663 | int auto_off = 0; |
Kyle Swenson | 9d23807 | 2021-12-01 08:13:17 -0700 | [diff] [blame] | 664 | unsigned int valid_lifetime = 0xffffffff; |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 665 | unsigned int time = 0xffffffff; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 666 | struct dhcp_context *context; |
| 667 | |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 668 | for (context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 669 | if (!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) && |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 670 | prefix <= context->prefix && |
| 671 | is_same_net6(local, &context->start6, context->prefix) && |
| 672 | is_same_net6(local, &context->end6, context->prefix)) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 673 | { |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 674 | context->saved_valid = valid; |
| 675 | |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 676 | if (context->flags & CONTEXT_RA) |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 677 | { |
| 678 | do_slaac = 1; |
Simon Kelley | 4723d49 | 2012-03-30 21:04:17 +0100 | [diff] [blame] | 679 | if (context->flags & CONTEXT_DHCP) |
Simon Kelley | 05e92e5 | 2012-03-30 22:24:15 +0100 | [diff] [blame] | 680 | { |
| 681 | param->other = 1; |
| 682 | if (!(context->flags & CONTEXT_RA_STATELESS)) |
| 683 | param->managed = 1; |
| 684 | } |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 685 | } |
Simon Kelley | 884a6df | 2012-03-20 16:20:22 +0000 | [diff] [blame] | 686 | else |
Simon Kelley | 0010b47 | 2012-02-29 12:18:30 +0000 | [diff] [blame] | 687 | { |
| 688 | /* don't do RA for non-ra-only unless --enable-ra is set */ |
| 689 | if (!option_bool(OPT_RA)) |
| 690 | continue; |
| 691 | param->managed = 1; |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 692 | param->other = 1; |
Simon Kelley | 0010b47 | 2012-02-29 12:18:30 +0000 | [diff] [blame] | 693 | } |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 694 | |
| 695 | /* Configured to advertise router address, not prefix. See RFC 3775 7.2 |
| 696 | In this case we do all addresses associated with a context, |
| 697 | hence the real_prefix setting here. */ |
| 698 | if (context->flags & CONTEXT_RA_ROUTER) |
| 699 | { |
| 700 | adv_router = 1; |
| 701 | param->adv_router = 1; |
| 702 | real_prefix = context->prefix; |
| 703 | } |
| 704 | |
Simon Kelley | 4d85e40 | 2020-07-12 22:45:46 +0100 | [diff] [blame] | 705 | /* find floor time, don't reduce below 3 * RA interval. |
| 706 | If the lease time has been left as default, don't |
| 707 | use that as a floor. */ |
| 708 | if ((context->flags & CONTEXT_SETLEASE) && |
| 709 | time > context->lease_time) |
Simon Kelley | 7f035f5 | 2012-12-22 21:27:08 +0000 | [diff] [blame] | 710 | { |
Kyle Swenson | 9d23807 | 2021-12-01 08:13:17 -0700 | [diff] [blame] | 711 | |
| 712 | if ((context->flags & CONTEXT_SETVALID)) |
| 713 | valid_lifetime = context->valid_lifetime; |
| 714 | else |
| 715 | valid_lifetime = context->lease_time; |
| 716 | |
Simon Kelley | 7f035f5 | 2012-12-22 21:27:08 +0000 | [diff] [blame] | 717 | time = context->lease_time; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 718 | if (time < ((unsigned int)(3 * param->adv_interval))) |
| 719 | time = 3 * param->adv_interval; |
Simon Kelley | 7f035f5 | 2012-12-22 21:27:08 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 722 | if (context->flags & CONTEXT_DEPRECATE) |
| 723 | deprecate = 1; |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 724 | |
| 725 | if (context->flags & CONTEXT_CONSTRUCTED) |
| 726 | constructed = 1; |
| 727 | |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 728 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 729 | /* collect dhcp-range tags */ |
| 730 | if (context->netid.next == &context->netid && context->netid.net) |
| 731 | { |
| 732 | context->netid.next = param->tags; |
| 733 | param->tags = &context->netid; |
| 734 | } |
| 735 | |
Simon Kelley | 5ae34bf | 2012-06-04 21:14:03 +0100 | [diff] [blame] | 736 | /* subsequent prefixes on the same interface |
| 737 | and subsequent instances of this prefix don't need timers. |
| 738 | Be careful not to find the same prefix twice with different |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 739 | addresses unless we're advertising the actual addresses. */ |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 740 | if (!(context->flags & CONTEXT_RA_DONE)) |
| 741 | { |
Simon Kelley | 5ae34bf | 2012-06-04 21:14:03 +0100 | [diff] [blame] | 742 | if (!param->first) |
| 743 | context->ra_time = 0; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 744 | context->flags |= CONTEXT_RA_DONE; |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 745 | real_prefix = context->prefix; |
Neil Jerram | 2fd5bc9 | 2015-06-10 22:13:06 +0100 | [diff] [blame] | 746 | off_link = (context->flags & CONTEXT_RA_OFF_LINK); |
tarun.kundu | 6603119 | 2022-11-10 14:39:45 -0800 | [diff] [blame] | 747 | auto_off = (context->flags & CONTEXT_RA_AUTO_OFF); |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 748 | } |
Simon Kelley | 5ae34bf | 2012-06-04 21:14:03 +0100 | [diff] [blame] | 749 | |
S L | a18bf31 | 2016-02-12 17:36:20 +0000 | [diff] [blame] | 750 | param->first = 0; |
| 751 | /* found_context is the _last_ one we found, so if there's |
| 752 | more than one, it's not the first. */ |
| 753 | param->found_context = context; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 754 | } |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 755 | |
Simon Kelley | ed8b68a | 2012-12-21 16:23:26 +0000 | [diff] [blame] | 756 | /* configured time is ceiling */ |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 757 | if (!constructed || valid > time) |
Kyle Swenson | 9d23807 | 2021-12-01 08:13:17 -0700 | [diff] [blame] | 758 | valid = valid_lifetime; |
Simon Kelley | ed8b68a | 2012-12-21 16:23:26 +0000 | [diff] [blame] | 759 | |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 760 | if (flags & IFACE_DEPRECATED) |
Simon Kelley | ed8b68a | 2012-12-21 16:23:26 +0000 | [diff] [blame] | 761 | preferred = 0; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 762 | |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 763 | if (deprecate) |
| 764 | time = 0; |
| 765 | |
| 766 | /* configured time is ceiling */ |
| 767 | if (!constructed || preferred > time) |
| 768 | preferred = time; |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 769 | |
| 770 | if (IN6_IS_ADDR_ULA(local)) |
Simon Kelley | 55b42f6 | 2012-12-21 16:53:15 +0000 | [diff] [blame] | 771 | { |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 772 | if (preferred > param->ula_pref_time) |
| 773 | { |
| 774 | param->ula_pref_time = preferred; |
| 775 | param->ula = *local; |
| 776 | } |
| 777 | } |
| 778 | else |
| 779 | { |
| 780 | if (preferred > param->glob_pref_time) |
| 781 | { |
| 782 | param->glob_pref_time = preferred; |
| 783 | param->link_global = *local; |
| 784 | } |
Simon Kelley | 55b42f6 | 2012-12-21 16:53:15 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 787 | if (real_prefix != 0) |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 788 | { |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 789 | struct prefix_opt *opt; |
| 790 | |
| 791 | if ((opt = expand(sizeof(struct prefix_opt)))) |
| 792 | { |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 793 | /* zero net part of address */ |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 794 | if (!adv_router) |
| 795 | setaddr6part(local, addr6part(local) & ~((real_prefix == 64) ? (u64)-1LL : (1LLU << (128 - real_prefix)) - 1LLU)); |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 796 | |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 797 | opt->type = ICMP6_OPT_PREFIX; |
| 798 | opt->len = 4; |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 799 | opt->prefix_len = real_prefix; |
Neil Jerram | 2fd5bc9 | 2015-06-10 22:13:06 +0100 | [diff] [blame] | 800 | /* autonomous only if we're not doing dhcp, set |
| 801 | "on-link" unless "off-link" was specified */ |
| 802 | opt->flags = (off_link ? 0 : 0x80); |
tarun.kundu | 6603119 | 2022-11-10 14:39:45 -0800 | [diff] [blame] | 803 | if (do_slaac && !auto_off) |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 804 | opt->flags |= 0x40; |
| 805 | if (adv_router) |
| 806 | opt->flags |= 0x20; |
Simon Kelley | ed8b68a | 2012-12-21 16:23:26 +0000 | [diff] [blame] | 807 | opt->valid_lifetime = htonl(valid); |
| 808 | opt->preferred_lifetime = htonl(preferred); |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 809 | opt->reserved = 0; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 810 | opt->prefix = *local; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 811 | |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 812 | inet_ntop(AF_INET6, local, daemon->addrbuff, ADDRSTRLEN); |
Kevin Darbyshire-Bryant | 8c0b73d | 2013-10-11 11:56:33 +0100 | [diff] [blame] | 813 | if (!option_bool(OPT_QUIET_RA)) |
| 814 | my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s", param->if_name, daemon->addrbuff); |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 815 | } |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 816 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | return 1; |
| 820 | } |
| 821 | |
| 822 | static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm) |
| 823 | { |
| 824 | (void)type; |
| 825 | |
| 826 | if (index == *((int *)parm)) |
| 827 | { |
| 828 | /* size is in units of 8 octets and includes type and length (2 bytes) |
| 829 | add 7 to round up */ |
| 830 | int len = (maclen + 9) >> 3; |
| 831 | unsigned char *p = expand(len << 3); |
| 832 | memset(p, 0, len << 3); |
| 833 | *p++ = ICMP6_OPT_SOURCE_MAC; |
| 834 | *p++ = len; |
| 835 | memcpy(p, mac, maclen); |
| 836 | |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | return 1; |
| 841 | } |
| 842 | |
| 843 | time_t periodic_ra(time_t now) |
| 844 | { |
| 845 | struct search_param param; |
| 846 | struct dhcp_context *context; |
| 847 | time_t next_event; |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 848 | struct alias_param aparam; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 849 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 850 | param.now = now; |
Simon Kelley | 29d28dd | 2012-12-03 14:05:59 +0000 | [diff] [blame] | 851 | param.iface = 0; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 852 | |
| 853 | while (1) |
| 854 | { |
| 855 | /* find overdue events, and time of first future event */ |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 856 | for (next_event = 0, context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 857 | if (context->ra_time != 0) |
| 858 | { |
Simon Kelley | 7dbe981 | 2012-03-25 14:49:54 +0100 | [diff] [blame] | 859 | if (difftime(context->ra_time, now) <= 0.0) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 860 | break; /* overdue */ |
| 861 | |
Simon Kelley | 7dbe981 | 2012-03-25 14:49:54 +0100 | [diff] [blame] | 862 | if (next_event == 0 || difftime(next_event, context->ra_time) > 0.0) |
| 863 | next_event = context->ra_time; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /* none overdue */ |
| 867 | if (!context) |
| 868 | break; |
| 869 | |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 870 | if ((context->flags & CONTEXT_OLD) && |
| 871 | context->if_index != 0 && |
Simon Kelley | dd9d9ce | 2013-11-15 11:24:00 +0000 | [diff] [blame] | 872 | indextoname(daemon->icmp6fd, context->if_index, param.name)) |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 873 | { |
| 874 | /* A context for an old address. We'll not find the interface by |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 875 | looking for addresses, but we know it anyway, since the context is |
| 876 | constructed */ |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 877 | param.iface = context->if_index; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 878 | new_timeout(context, param.name, now); |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 879 | } |
| 880 | else if (iface_enumerate(AF_INET6, ¶m, iface_search)) |
| 881 | /* There's a context overdue, but we can't find an interface |
| 882 | associated with it, because it's for a subnet we dont |
| 883 | have an interface on. Probably we're doing DHCP on |
| 884 | a remote subnet via a relay. Zero the timer, since we won't |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 885 | ever be able to send ra's and satisfy it. */ |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 886 | context->ra_time = 0; |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 887 | |
| 888 | if (param.iface != 0 && |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 889 | iface_check(AF_LOCAL, NULL, param.name, NULL)) |
Simon Kelley | 421594f | 2012-12-02 12:17:35 +0000 | [diff] [blame] | 890 | { |
| 891 | struct iname *tmp; |
| 892 | for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 893 | if (tmp->name && wildcard_match(tmp->name, param.name)) |
Simon Kelley | 421594f | 2012-12-02 12:17:35 +0000 | [diff] [blame] | 894 | break; |
| 895 | if (!tmp) |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 896 | { |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 897 | struct iname *if_tmp; |
| 898 | for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next) |
| 899 | { |
Tarun Kundu | 74fb2b6 | 2022-07-06 09:11:18 -0700 | [diff] [blame] | 900 | if ((strcmp(if_tmp->name, param.name) == 0) && |
| 901 | ((if_tmp->is_ra_adv_dis == false) || |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 902 | (if_tmp->final_ra_sent_count < MAX_FINAL_RTR_ADVERTISEMENTS)) && |
| 903 | (if_tmp->first_ra_delayed)) { |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 904 | send_ra(now, param.iface, param.name, NULL); |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 905 | |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 906 | /* Also send on all interfaces that are aliases of this |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 907 | one. */ |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 908 | for (aparam.bridge = daemon->bridges; |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 909 | aparam.bridge; |
| 910 | aparam.bridge = aparam.bridge->next) |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 911 | if ((int)if_nametoindex(aparam.bridge->iface) == param.iface) |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 912 | { |
| 913 | /* Count the number of alias interfaces for this |
| 914 | 'bridge', by calling iface_enumerate with |
| 915 | send_ra_to_aliases and NULL alias_ifs. */ |
| 916 | aparam.iface = param.iface; |
| 917 | aparam.alias_ifs = NULL; |
| 918 | aparam.num_alias_ifs = 0; |
| 919 | iface_enumerate(AF_LOCAL, &aparam, send_ra_to_aliases); |
| 920 | my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s => %d alias(es)", |
| 921 | param.name, daemon->addrbuff, aparam.num_alias_ifs); |
| 922 | |
| 923 | /* Allocate memory to store the alias interface |
| 924 | indices. */ |
| 925 | aparam.alias_ifs = (int *)whine_malloc(aparam.num_alias_ifs * |
| 926 | sizeof(int)); |
| 927 | if (aparam.alias_ifs) |
| 928 | { |
| 929 | /* Use iface_enumerate again to get the alias |
| 930 | interface indices, then send on each of |
| 931 | those. */ |
| 932 | aparam.max_alias_ifs = aparam.num_alias_ifs; |
| 933 | aparam.num_alias_ifs = 0; |
| 934 | iface_enumerate(AF_LOCAL, &aparam, send_ra_to_aliases); |
| 935 | for (; aparam.num_alias_ifs; aparam.num_alias_ifs--) |
| 936 | { |
| 937 | my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s => i/f %d", |
| 938 | param.name, daemon->addrbuff, |
| 939 | aparam.alias_ifs[aparam.num_alias_ifs - 1]); |
| 940 | send_ra_alias(now, |
| 941 | param.iface, |
| 942 | param.name, |
| 943 | NULL, |
| 944 | aparam.alias_ifs[aparam.num_alias_ifs - 1]); |
| 945 | } |
| 946 | free(aparam.alias_ifs); |
| 947 | } |
| 948 | |
| 949 | /* The source interface can only appear in at most |
Neil Jerram | 4918bd5 | 2015-06-10 22:23:20 +0100 | [diff] [blame] | 950 | one --bridge-interface. */ |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 951 | break; |
| 952 | } |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 953 | } else { |
| 954 | if_tmp->first_ra_delayed = true; |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 955 | } |
| 956 | } |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 957 | } |
Simon Kelley | 421594f | 2012-12-02 12:17:35 +0000 | [diff] [blame] | 958 | } |
| 959 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 960 | return next_event; |
| 961 | } |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 962 | |
| 963 | static int send_ra_to_aliases(int index, unsigned int type, char *mac, size_t maclen, void *parm) |
| 964 | { |
| 965 | struct alias_param *aparam = (struct alias_param *)parm; |
| 966 | char ifrn_name[IFNAMSIZ]; |
| 967 | struct dhcp_bridge *alias; |
| 968 | |
| 969 | (void)type; |
| 970 | (void)mac; |
| 971 | (void)maclen; |
| 972 | |
| 973 | if (if_indextoname(index, ifrn_name)) |
| 974 | for (alias = aparam->bridge->alias; alias; alias = alias->next) |
| 975 | if (wildcard_matchn(alias->iface, ifrn_name, IFNAMSIZ)) |
| 976 | { |
| 977 | if (aparam->alias_ifs && (aparam->num_alias_ifs < aparam->max_alias_ifs)) |
| 978 | aparam->alias_ifs[aparam->num_alias_ifs] = index; |
| 979 | aparam->num_alias_ifs++; |
| 980 | } |
| 981 | |
| 982 | return 1; |
| 983 | } |
| 984 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 985 | static int iface_search(struct in6_addr *local, int prefix, |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 986 | int scope, int if_index, int flags, |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 987 | int preferred, int valid, void *vparam) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 988 | { |
| 989 | struct search_param *param = vparam; |
Simon Kelley | 741c295 | 2012-02-25 13:09:18 +0000 | [diff] [blame] | 990 | struct dhcp_context *context; |
Simon Kelley | cd67293 | 2020-01-27 22:53:07 +0000 | [diff] [blame] | 991 | struct iname *tmp; |
| 992 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 993 | (void)scope; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 994 | (void)preferred; |
| 995 | (void)valid; |
Simon Kelley | cd67293 | 2020-01-27 22:53:07 +0000 | [diff] [blame] | 996 | |
| 997 | /* ignore interfaces we're not doing DHCP on. */ |
| 998 | if (!indextoname(daemon->icmp6fd, if_index, param->name) || |
| 999 | !iface_check(AF_LOCAL, NULL, param->name, NULL)) |
| 1000 | return 1; |
| 1001 | |
| 1002 | for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
| 1003 | if (tmp->name && wildcard_match(tmp->name, param->name)) |
| 1004 | return 1; |
| 1005 | |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 1006 | for (context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 1007 | if (!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) && |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 1008 | prefix <= context->prefix && |
| 1009 | is_same_net6(local, &context->start6, context->prefix) && |
| 1010 | is_same_net6(local, &context->end6, context->prefix) && |
Simon Kelley | 6e3dba3 | 2012-12-16 21:52:45 +0000 | [diff] [blame] | 1011 | context->ra_time != 0 && |
| 1012 | difftime(context->ra_time, param->now) <= 0.0) |
| 1013 | { |
| 1014 | /* found an interface that's overdue for RA determine new |
| 1015 | timeout value and arrange for RA to be sent unless interface is |
| 1016 | still doing DAD.*/ |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 1017 | if (!(flags & IFACE_TENTATIVE)) |
Simon Kelley | 6e3dba3 | 2012-12-16 21:52:45 +0000 | [diff] [blame] | 1018 | param->iface = if_index; |
| 1019 | |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 1020 | new_timeout(context, param->name, param->now); |
Simon Kelley | 6e3dba3 | 2012-12-16 21:52:45 +0000 | [diff] [blame] | 1021 | |
| 1022 | /* zero timers for other contexts on the same subnet, so they don't timeout |
| 1023 | independently */ |
| 1024 | for (context = context->next; context; context = context->next) |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 1025 | if (prefix <= context->prefix && |
| 1026 | is_same_net6(local, &context->start6, context->prefix) && |
| 1027 | is_same_net6(local, &context->end6, context->prefix)) |
Simon Kelley | 6e3dba3 | 2012-12-16 21:52:45 +0000 | [diff] [blame] | 1028 | context->ra_time = 0; |
| 1029 | |
| 1030 | return 0; /* found, abort */ |
| 1031 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 1032 | |
| 1033 | return 1; /* keep searching */ |
| 1034 | } |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 1035 | |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 1036 | static void new_timeout(struct dhcp_context *context, char *iface_name, time_t now) |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 1037 | { |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 1038 | struct ra_interface *ra = find_iface_param(iface_name); |
| 1039 | if (ra->ra_sent_count < (MAX_INITIAL_RTR_ADVERTISEMENTS-1)) |
| 1040 | context->ra_time = now + MAX_INITIAL_RTR_ADVERT_INTERVAL; |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 1041 | else |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 1042 | { |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 1043 | unsigned int adv_interval = calc_interval(ra); |
| 1044 | context->ra_time = now + adv_interval; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | static struct ra_interface *find_iface_param(char *iface) |
| 1049 | { |
| 1050 | struct ra_interface *ra; |
| 1051 | |
| 1052 | for (ra = daemon->ra_interfaces; ra; ra = ra->next) |
| 1053 | if (wildcard_match(ra->name, iface)) |
| 1054 | return ra; |
| 1055 | |
| 1056 | return NULL; |
| 1057 | } |
| 1058 | |
| 1059 | static unsigned int calc_interval(struct ra_interface *ra) |
| 1060 | { |
Tarun Kundu | 74fb2b6 | 2022-07-06 09:11:18 -0700 | [diff] [blame] | 1061 | int interval = DEFAULT_RTR_ADV_INTERVAL; |
Tarun Kundu | 36e8710 | 2022-07-20 14:43:44 -0700 | [diff] [blame] | 1062 | if (ra) { |
Tarun Kundu | 74fb2b6 | 2022-07-06 09:11:18 -0700 | [diff] [blame] | 1063 | unsigned int min_rtr_interval = ra->min_ra_adv_interval; |
| 1064 | unsigned int max_rtr_interval = ra->max_ra_adv_interval; |
| 1065 | |
| 1066 | if (max_rtr_interval > MAX_RTR_ADV_INTERVAL_UPPER_BOUND) |
| 1067 | max_rtr_interval = MAX_RTR_ADV_INTERVAL_UPPER_BOUND; |
| 1068 | else if (max_rtr_interval < MAX_RTR_ADV_INTERVAL_LOWER_BOUND) |
| 1069 | max_rtr_interval = MAX_RTR_ADV_INTERVAL_LOWER_BOUND; |
| 1070 | |
| 1071 | if (min_rtr_interval < MIN_RTR_ADV_INTERVAL) |
| 1072 | min_rtr_interval = MIN_RTR_ADV_INTERVAL; |
| 1073 | |
| 1074 | interval = (rand() % (max_rtr_interval - min_rtr_interval + 1)) + min_rtr_interval; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 1075 | |
Tarun Kundu | 74fb2b6 | 2022-07-06 09:11:18 -0700 | [diff] [blame] | 1076 | if (((ra->ra_sent_count + 1) < MAX_INITIAL_RTR_ADVERTISEMENTS) && |
| 1077 | interval > MAX_INITIAL_RTR_ADVERT_INTERVAL) |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 1078 | interval = MAX_INITIAL_RTR_ADVERT_INTERVAL; |
Tarun Kundu | 36e8710 | 2022-07-20 14:43:44 -0700 | [diff] [blame] | 1079 | } |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 1080 | return (unsigned int)interval; |
| 1081 | } |
| 1082 | |
| 1083 | static unsigned int calc_lifetime(struct ra_interface *ra) |
| 1084 | { |
| 1085 | int lifetime, interval = (int)calc_interval(ra); |
| 1086 | |
| 1087 | if (!ra || ra->lifetime == -1) /* not specified */ |
| 1088 | lifetime = 3 * interval; |
| 1089 | else |
| 1090 | { |
| 1091 | lifetime = ra->lifetime; |
| 1092 | if (lifetime < interval && lifetime != 0) |
| 1093 | lifetime = interval; |
| 1094 | else if (lifetime > 9000) |
| 1095 | lifetime = 9000; |
| 1096 | } |
| 1097 | |
| 1098 | return (unsigned int)lifetime; |
| 1099 | } |
| 1100 | |
| 1101 | static unsigned int calc_prio(struct ra_interface *ra) |
| 1102 | { |
| 1103 | if (ra) |
| 1104 | return ra->prio; |
| 1105 | |
| 1106 | return 0; |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 1107 | } |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 1108 | |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 1109 | static unsigned char calc_hop_limit(struct ra_interface *ra) |
| 1110 | { |
| 1111 | if (ra) { |
| 1112 | return ra->hop_limit; |
| 1113 | } |
| 1114 | return (unsigned char)hop_limit; |
| 1115 | } |
| 1116 | |
Tarun Kundu | 41aa488 | 2022-07-05 10:44:13 -0700 | [diff] [blame] | 1117 | static unsigned int calc_retrans_time(struct ra_interface *ra) |
| 1118 | { |
| 1119 | if (ra) { |
| 1120 | return ra->retrans_time; |
| 1121 | } |
| 1122 | return 0; |
| 1123 | } |
| 1124 | |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 1125 | static unsigned int calc_reachable_time(struct ra_interface *ra) |
| 1126 | { |
| 1127 | if (ra) { |
| 1128 | return ra->reachable_time; |
| 1129 | } |
| 1130 | return 0; |
| 1131 | } |
Tarun Kundu | 01293a7 | 2022-07-18 23:50:36 -0700 | [diff] [blame] | 1132 | |
| 1133 | int64_t timespecdiff(struct timespec const *a, struct timespec const *b) |
| 1134 | { |
| 1135 | int64_t msec; |
| 1136 | msec = ((int64_t)a->tv_sec - b->tv_sec) * 1000; |
| 1137 | msec += ((int64_t)a->tv_nsec - b->tv_nsec) / (1000 * 1000); |
| 1138 | return msec; |
| 1139 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 1140 | #endif |