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 | { |
tarun.kundu | 1c6e7fa | 2022-11-16 14:00:17 -0800 | [diff] [blame] | 219 | if (!((sz > 8) && (packet[9] == 0))) { |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 220 | inet_ntop(AF_INET6, &from.sin6_addr, daemon->addrbuff, ADDRSTRLEN); |
tarun.kundu | 1c6e7fa | 2022-11-16 14:00:17 -0800 | [diff] [blame] | 221 | if (!((strncmp(daemon->addrbuff, "::",2)==0) && (packet[8] == ICMP6_OPT_SOURCE_MAC))) |
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 | 1c6e7fa | 2022-11-16 14:00:17 -0800 | [diff] [blame] | 286 | } |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 287 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Neil Jerram | ba4fc0f | 2015-06-10 22:14:49 +0100 | [diff] [blame] | 290 | 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] | 291 | { |
| 292 | struct ra_packet *ra; |
| 293 | struct ra_param parm; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 294 | struct sockaddr_in6 addr; |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 295 | struct dhcp_context *context, *tmp, **up; |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 296 | struct dhcp_netid iface_id; |
| 297 | struct dhcp_opt *opt_cfg; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 298 | struct ra_interface *ra_param = find_iface_param(iface_name); |
David Flamand | 005c46d | 2017-04-11 11:49:54 +0100 | [diff] [blame] | 299 | int done_dns = 0, old_prefix = 0, mtu = 0; |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 300 | unsigned int min_pref_time; |
Simon Kelley | 3b43646 | 2012-12-28 11:55:45 +0000 | [diff] [blame] | 301 | #ifdef HAVE_LINUX_NETWORK |
| 302 | FILE *f; |
| 303 | #endif |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 304 | |
| 305 | struct iname *if_tmp; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 306 | parm.ind = iface; |
| 307 | parm.managed = 0; |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 308 | parm.other = 0; |
S L | a18bf31 | 2016-02-12 17:36:20 +0000 | [diff] [blame] | 309 | parm.found_context = NULL; |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 310 | parm.adv_router = 0; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 311 | parm.if_name = iface_name; |
| 312 | parm.first = 1; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 313 | parm.now = now; |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 314 | 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] | 315 | parm.adv_interval = calc_interval(ra_param); |
Ilya Ponetaev | 5bf50af | 2014-09-09 12:46:21 +0100 | [diff] [blame] | 316 | parm.prio = calc_prio(ra_param); |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 317 | |
Simon Kelley | fa78573 | 2016-07-22 20:56:01 +0100 | [diff] [blame] | 318 | reset_counter(); |
Simon Kelley | ce7845b | 2016-07-06 21:42:27 +0100 | [diff] [blame] | 319 | |
| 320 | if (!(ra = expand(sizeof(struct ra_packet)))) |
| 321 | return; |
Ilya Ponetaev | 5bf50af | 2014-09-09 12:46:21 +0100 | [diff] [blame] | 322 | |
| 323 | ra->type = ND_ROUTER_ADVERT; |
| 324 | ra->code = 0; |
Ilya Ponetaev | 5bf50af | 2014-09-09 12:46:21 +0100 | [diff] [blame] | 325 | ra->flags = parm.prio; |
| 326 | ra->lifetime = htons(calc_lifetime(ra_param)); |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 327 | ra->reachable_time = htonl(calc_reachable_time(ra_param)); |
| 328 | ra->hop_limit = calc_hop_limit(ra_param); |
Tarun Kundu | 41aa488 | 2022-07-05 10:44:13 -0700 | [diff] [blame] | 329 | ra->retrans_time = htonl(calc_retrans_time(ra_param)); |
Ilya Ponetaev | 5bf50af | 2014-09-09 12:46:21 +0100 | [diff] [blame] | 330 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 331 | /* set tag with name == interface */ |
| 332 | iface_id.net = iface_name; |
| 333 | iface_id.next = NULL; |
| 334 | parm.tags = &iface_id; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 335 | |
Tarun Kundu | 74fb2b6 | 2022-07-06 09:11:18 -0700 | [diff] [blame] | 336 | 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 Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 350 | for (context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 351 | { |
| 352 | context->flags &= ~CONTEXT_RA_DONE; |
| 353 | context->netid.next = &context->netid; |
| 354 | } |
| 355 | |
Simon Kelley | 378fa56 | 2020-01-05 17:23:19 +0000 | [diff] [blame] | 356 | /* 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 Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 360 | return; |
| 361 | |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 362 | /* 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 Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 374 | /* 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 Kelley | 3b43646 | 2012-12-28 11:55:45 +0000 | [diff] [blame] | 379 | |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 380 | 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 L | a18bf31 | 2016-02-12 17:36:20 +0000 | [diff] [blame] | 385 | { |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 386 | /* We've advertised this enough, time to go */ |
S L | a18bf31 | 2016-02-12 17:36:20 +0000 | [diff] [blame] | 387 | |
| 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 Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 393 | *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 Kelley | 4452292 | 2013-11-15 14:45:04 +0000 | [diff] [blame] | 402 | old_prefix = 1; |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 403 | |
| 404 | /* zero net part of address */ |
| 405 | setaddr6part(&local, addr6part(&local) & ~((context->prefix == 64) ? (u64)-1LL : (1LLU << (128 - context->prefix)) - 1LLU)); |
| 406 | |
Simon Kelley | 6ea1f23 | 2013-12-19 15:45:12 +0000 | [diff] [blame] | 407 | |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 408 | if (context->flags & CONTEXT_RA) |
Simon Kelley | 6ea1f23 | 2013-12-19 15:45:12 +0000 | [diff] [blame] | 409 | { |
| 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 Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 428 | 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 Jerram | 2fd5bc9 | 2015-06-10 22:13:06 +0100 | [diff] [blame] | 433 | /* 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.kundu | 6603119 | 2022-11-10 14:39:45 -0800 | [diff] [blame] | 436 | ((context->flags & CONTEXT_RA_OFF_LINK) ? 0 : 0x80) | |
| 437 | ((context->flags & CONTEXT_RA_AUTO_OFF) ? 0 : 0x40); |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 438 | 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-Bryant | 8c0b73d | 2013-10-11 11:56:33 +0100 | [diff] [blame] | 444 | if (!option_bool(OPT_QUIET_RA)) |
| 445 | 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] | 446 | } |
| 447 | |
| 448 | up = &context->next; |
| 449 | } |
| 450 | } |
| 451 | else |
| 452 | up = &context->next; |
| 453 | } |
| 454 | |
Simon Kelley | 4452292 | 2013-11-15 14:45:04 +0000 | [diff] [blame] | 455 | /* 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 Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 463 | /* 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 Flamand | 005c46d | 2017-04-11 11:49:54 +0100 | [diff] [blame] | 474 | /* 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 Kelley | 3b43646 | 2012-12-28 11:55:45 +0000 | [diff] [blame] | 476 | #ifdef HAVE_LINUX_NETWORK |
Ville Skyttä | faaf306 | 2018-01-14 17:32:52 +0000 | [diff] [blame] | 477 | /* 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] | 478 | available from SIOCGIFMTU */ |
Vladislav Grishenko | 6ec5f5c | 2017-04-24 22:34:45 +0100 | [diff] [blame] | 479 | char *mtu_name = ra_param ? ra_param->mtu_name : NULL; |
Simon Kelley | 5ed82ae | 2019-03-05 16:38:34 +0000 | [diff] [blame] | 480 | 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] | 481 | if ((f = fopen(daemon->namebuff, "r"))) |
| 482 | { |
| 483 | if (fgets(daemon->namebuff, MAXDNAME, f)) |
| 484 | mtu = atoi(daemon->namebuff); |
| 485 | fclose(f); |
| 486 | } |
Simon Kelley | 3b43646 | 2012-12-28 11:55:45 +0000 | [diff] [blame] | 487 | #endif |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 488 | if (ra_param) |
| 489 | mtu = ra_param->mtu; |
David Flamand | 005c46d | 2017-04-11 11:49:54 +0100 | [diff] [blame] | 490 | 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 Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 497 | |
Neil Jerram | ba4fc0f | 2015-06-10 22:14:49 +0100 | [diff] [blame] | 498 | iface_enumerate(AF_LOCAL, &send_iface, add_lla); |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 499 | |
| 500 | /* RDNSS, RFC 6106, use relevant DHCP6 options */ |
| 501 | (void)option_filter(parm.tags, NULL, daemon->dhcp_opts6); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 502 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 503 | 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 Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 513 | struct in6_addr *a; |
| 514 | int len; |
| 515 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 516 | done_dns = 1; |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 517 | |
| 518 | if (opt_cfg->len == 0) |
Simon Kelley | 806cf78 | 2013-10-14 14:08:44 +0100 | [diff] [blame] | 519 | continue; |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 520 | |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 521 | /* 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 Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 555 | } |
| 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 Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 564 | put_opt6_long(min_pref_time); |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 565 | 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 Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 573 | if (daemon->port == NAMESERVER_PORT && !done_dns && parm.link_pref_time != 0) |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 574 | { |
Simon Kelley | ab915f8 | 2013-04-30 10:41:28 +0100 | [diff] [blame] | 575 | /* default == us, as long as we are supplying DNS service. */ |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 576 | put_opt6_char(ICMP6_OPT_RDNSS); |
| 577 | put_opt6_char(3); |
| 578 | put_opt6_short(0); |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 579 | put_opt6_long(min_pref_time); |
Simon Kelley | 806cf78 | 2013-10-14 14:08:44 +0100 | [diff] [blame] | 580 | put_opt6(&parm.link_local, IN6ADDRSZ); |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 581 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 582 | |
| 583 | /* set managed bits unless we're providing only RA on this link */ |
| 584 | if (parm.managed) |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 585 | ra->flags |= 0x80; /* M flag, managed, */ |
| 586 | if (parm.other) |
| 587 | ra->flags |= 0x40; /* O flag, other */ |
| 588 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 589 | /* decide where we're sending */ |
| 590 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | 22d904d | 2012-02-26 20:13:45 +0000 | [diff] [blame] | 591 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 592 | addr.sin6_len = sizeof(struct sockaddr_in6); |
| 593 | #endif |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 594 | addr.sin6_family = AF_INET6; |
| 595 | addr.sin6_port = htons(IPPROTO_ICMPV6); |
| 596 | if (dest) |
| 597 | { |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 598 | addr.sin6_addr = *dest; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 599 | if (IN6_IS_ADDR_LINKLOCAL(dest) || |
| 600 | IN6_IS_ADDR_MC_LINKLOCAL(dest)) |
| 601 | addr.sin6_scope_id = iface; |
| 602 | } |
| 603 | else |
Simon Kelley | 8f3194f | 2013-09-30 15:04:58 +0100 | [diff] [blame] | 604 | { |
| 605 | inet_pton(AF_INET6, ALL_NODES, &addr.sin6_addr); |
Neil Jerram | ba4fc0f | 2015-06-10 22:14:49 +0100 | [diff] [blame] | 606 | 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] | 607 | } |
Simon Kelley | ff841eb | 2015-03-11 21:36:30 +0000 | [diff] [blame] | 608 | while (retry_send(sendto(daemon->icmp6fd, daemon->outpacket.iov_base, |
Simon Kelley | 6b1c464 | 2016-07-22 20:59:16 +0100 | [diff] [blame] | 609 | save_counter(-1), 0, (struct sockaddr *)&addr, |
Simon Kelley | ff841eb | 2015-03-11 21:36:30 +0000 | [diff] [blame] | 610 | sizeof(addr)))); |
Tarun Kundu | 74fb2b6 | 2022-07-06 09:11:18 -0700 | [diff] [blame] | 611 | ra_param->ra_sent_count++; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Neil Jerram | ba4fc0f | 2015-06-10 22:14:49 +0100 | [diff] [blame] | 614 | static 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 Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 621 | static int add_prefixes(struct in6_addr *local, int prefix, |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 622 | int scope, int if_index, int flags, |
Simon Kelley | 55b42f6 | 2012-12-21 16:53:15 +0000 | [diff] [blame] | 623 | unsigned int preferred, unsigned int valid, void *vparam) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 624 | { |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 625 | struct ra_param *param = vparam; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 626 | |
| 627 | (void)scope; /* warning */ |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 628 | |
| 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 Kundu | 74fb2b6 | 2022-07-06 09:11:18 -0700 | [diff] [blame] | 633 | if (if_tmp->final_ra_sent_count < MAX_FINAL_RTR_ADVERTISEMENTS) { |
| 634 | break; |
| 635 | } |
| 636 | else { |
| 637 | return 1; |
| 638 | } |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 639 | } |
| 640 | } |
Simon Kelley | ed8b68a | 2012-12-21 16:23:26 +0000 | [diff] [blame] | 641 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 642 | if (if_index == param->ind) |
| 643 | { |
| 644 | if (IN6_IS_ADDR_LINKLOCAL(local)) |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 645 | { |
| 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 Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 655 | else if (!IN6_IS_ADDR_LOOPBACK(local) && |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 656 | !IN6_IS_ADDR_MULTICAST(local)) |
| 657 | { |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 658 | int real_prefix = 0; |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 659 | int do_slaac = 0; |
| 660 | int deprecate = 0; |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 661 | int constructed = 0; |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 662 | int adv_router = 0; |
Neil Jerram | 2fd5bc9 | 2015-06-10 22:13:06 +0100 | [diff] [blame] | 663 | int off_link = 0; |
tarun.kundu | 6603119 | 2022-11-10 14:39:45 -0800 | [diff] [blame] | 664 | int auto_off = 0; |
Kyle Swenson | 9d23807 | 2021-12-01 08:13:17 -0700 | [diff] [blame] | 665 | unsigned int valid_lifetime = 0xffffffff; |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 666 | unsigned int time = 0xffffffff; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 667 | struct dhcp_context *context; |
| 668 | |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 669 | for (context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 670 | if (!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) && |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 671 | prefix <= context->prefix && |
| 672 | is_same_net6(local, &context->start6, context->prefix) && |
| 673 | is_same_net6(local, &context->end6, context->prefix)) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 674 | { |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 675 | context->saved_valid = valid; |
| 676 | |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 677 | if (context->flags & CONTEXT_RA) |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 678 | { |
| 679 | do_slaac = 1; |
Simon Kelley | 4723d49 | 2012-03-30 21:04:17 +0100 | [diff] [blame] | 680 | if (context->flags & CONTEXT_DHCP) |
Simon Kelley | 05e92e5 | 2012-03-30 22:24:15 +0100 | [diff] [blame] | 681 | { |
| 682 | param->other = 1; |
| 683 | if (!(context->flags & CONTEXT_RA_STATELESS)) |
| 684 | param->managed = 1; |
| 685 | } |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 686 | } |
Simon Kelley | 884a6df | 2012-03-20 16:20:22 +0000 | [diff] [blame] | 687 | else |
Simon Kelley | 0010b47 | 2012-02-29 12:18:30 +0000 | [diff] [blame] | 688 | { |
| 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 Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 693 | param->other = 1; |
Simon Kelley | 0010b47 | 2012-02-29 12:18:30 +0000 | [diff] [blame] | 694 | } |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 695 | |
| 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 Kelley | 4d85e40 | 2020-07-12 22:45:46 +0100 | [diff] [blame] | 706 | /* 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 Kelley | 7f035f5 | 2012-12-22 21:27:08 +0000 | [diff] [blame] | 711 | { |
Kyle Swenson | 9d23807 | 2021-12-01 08:13:17 -0700 | [diff] [blame] | 712 | |
| 713 | if ((context->flags & CONTEXT_SETVALID)) |
| 714 | valid_lifetime = context->valid_lifetime; |
| 715 | else |
| 716 | valid_lifetime = context->lease_time; |
| 717 | |
Simon Kelley | 7f035f5 | 2012-12-22 21:27:08 +0000 | [diff] [blame] | 718 | time = context->lease_time; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 719 | if (time < ((unsigned int)(3 * param->adv_interval))) |
| 720 | time = 3 * param->adv_interval; |
Simon Kelley | 7f035f5 | 2012-12-22 21:27:08 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 723 | if (context->flags & CONTEXT_DEPRECATE) |
| 724 | deprecate = 1; |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 725 | |
| 726 | if (context->flags & CONTEXT_CONSTRUCTED) |
| 727 | constructed = 1; |
| 728 | |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 729 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 730 | /* 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 Kelley | 5ae34bf | 2012-06-04 21:14:03 +0100 | [diff] [blame] | 737 | /* 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 Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 740 | addresses unless we're advertising the actual addresses. */ |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 741 | if (!(context->flags & CONTEXT_RA_DONE)) |
| 742 | { |
Simon Kelley | 5ae34bf | 2012-06-04 21:14:03 +0100 | [diff] [blame] | 743 | if (!param->first) |
| 744 | context->ra_time = 0; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 745 | context->flags |= CONTEXT_RA_DONE; |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 746 | real_prefix = context->prefix; |
Neil Jerram | 2fd5bc9 | 2015-06-10 22:13:06 +0100 | [diff] [blame] | 747 | off_link = (context->flags & CONTEXT_RA_OFF_LINK); |
tarun.kundu | 6603119 | 2022-11-10 14:39:45 -0800 | [diff] [blame] | 748 | auto_off = (context->flags & CONTEXT_RA_AUTO_OFF); |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 749 | } |
Simon Kelley | 5ae34bf | 2012-06-04 21:14:03 +0100 | [diff] [blame] | 750 | |
S L | a18bf31 | 2016-02-12 17:36:20 +0000 | [diff] [blame] | 751 | 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 Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 755 | } |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 756 | |
Simon Kelley | ed8b68a | 2012-12-21 16:23:26 +0000 | [diff] [blame] | 757 | /* configured time is ceiling */ |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 758 | if (!constructed || valid > time) |
Kyle Swenson | 9d23807 | 2021-12-01 08:13:17 -0700 | [diff] [blame] | 759 | valid = valid_lifetime; |
Simon Kelley | ed8b68a | 2012-12-21 16:23:26 +0000 | [diff] [blame] | 760 | |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 761 | if (flags & IFACE_DEPRECATED) |
Simon Kelley | ed8b68a | 2012-12-21 16:23:26 +0000 | [diff] [blame] | 762 | preferred = 0; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 763 | |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 764 | if (deprecate) |
| 765 | time = 0; |
| 766 | |
| 767 | /* configured time is ceiling */ |
| 768 | if (!constructed || preferred > time) |
| 769 | preferred = time; |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 770 | |
| 771 | if (IN6_IS_ADDR_ULA(local)) |
Simon Kelley | 55b42f6 | 2012-12-21 16:53:15 +0000 | [diff] [blame] | 772 | { |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 773 | 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 Kelley | 55b42f6 | 2012-12-21 16:53:15 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 788 | if (real_prefix != 0) |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 789 | { |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 790 | struct prefix_opt *opt; |
| 791 | |
| 792 | if ((opt = expand(sizeof(struct prefix_opt)))) |
| 793 | { |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 794 | /* zero net part of address */ |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 795 | if (!adv_router) |
| 796 | 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] | 797 | |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 798 | opt->type = ICMP6_OPT_PREFIX; |
| 799 | opt->len = 4; |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 800 | opt->prefix_len = real_prefix; |
Neil Jerram | 2fd5bc9 | 2015-06-10 22:13:06 +0100 | [diff] [blame] | 801 | /* 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.kundu | 6603119 | 2022-11-10 14:39:45 -0800 | [diff] [blame] | 804 | if (do_slaac && !auto_off) |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 805 | opt->flags |= 0x40; |
| 806 | if (adv_router) |
| 807 | opt->flags |= 0x20; |
Simon Kelley | ed8b68a | 2012-12-21 16:23:26 +0000 | [diff] [blame] | 808 | opt->valid_lifetime = htonl(valid); |
| 809 | opt->preferred_lifetime = htonl(preferred); |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 810 | opt->reserved = 0; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 811 | opt->prefix = *local; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 812 | |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 813 | inet_ntop(AF_INET6, local, daemon->addrbuff, ADDRSTRLEN); |
Kevin Darbyshire-Bryant | 8c0b73d | 2013-10-11 11:56:33 +0100 | [diff] [blame] | 814 | if (!option_bool(OPT_QUIET_RA)) |
| 815 | 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] | 816 | } |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 817 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | return 1; |
| 821 | } |
| 822 | |
| 823 | static 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 | |
| 844 | time_t periodic_ra(time_t now) |
| 845 | { |
| 846 | struct search_param param; |
| 847 | struct dhcp_context *context; |
| 848 | time_t next_event; |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 849 | struct alias_param aparam; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 850 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 851 | param.now = now; |
Simon Kelley | 29d28dd | 2012-12-03 14:05:59 +0000 | [diff] [blame] | 852 | param.iface = 0; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 853 | |
| 854 | while (1) |
| 855 | { |
| 856 | /* find overdue events, and time of first future event */ |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 857 | for (next_event = 0, context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 858 | if (context->ra_time != 0) |
| 859 | { |
Simon Kelley | 7dbe981 | 2012-03-25 14:49:54 +0100 | [diff] [blame] | 860 | if (difftime(context->ra_time, now) <= 0.0) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 861 | break; /* overdue */ |
| 862 | |
Simon Kelley | 7dbe981 | 2012-03-25 14:49:54 +0100 | [diff] [blame] | 863 | if (next_event == 0 || difftime(next_event, context->ra_time) > 0.0) |
| 864 | next_event = context->ra_time; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | /* none overdue */ |
| 868 | if (!context) |
| 869 | break; |
| 870 | |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 871 | if ((context->flags & CONTEXT_OLD) && |
| 872 | context->if_index != 0 && |
Simon Kelley | dd9d9ce | 2013-11-15 11:24:00 +0000 | [diff] [blame] | 873 | indextoname(daemon->icmp6fd, context->if_index, param.name)) |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 874 | { |
| 875 | /* 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] | 876 | looking for addresses, but we know it anyway, since the context is |
| 877 | constructed */ |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 878 | param.iface = context->if_index; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 879 | new_timeout(context, param.name, now); |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 880 | } |
| 881 | else if (iface_enumerate(AF_INET6, ¶m, 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 Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 886 | ever be able to send ra's and satisfy it. */ |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 887 | context->ra_time = 0; |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 888 | |
| 889 | if (param.iface != 0 && |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 890 | iface_check(AF_LOCAL, NULL, param.name, NULL)) |
Simon Kelley | 421594f | 2012-12-02 12:17:35 +0000 | [diff] [blame] | 891 | { |
| 892 | struct iname *tmp; |
| 893 | for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 894 | if (tmp->name && wildcard_match(tmp->name, param.name)) |
Simon Kelley | 421594f | 2012-12-02 12:17:35 +0000 | [diff] [blame] | 895 | break; |
| 896 | if (!tmp) |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 897 | { |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 898 | struct iname *if_tmp; |
| 899 | for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next) |
| 900 | { |
Tarun Kundu | 74fb2b6 | 2022-07-06 09:11:18 -0700 | [diff] [blame] | 901 | if ((strcmp(if_tmp->name, param.name) == 0) && |
| 902 | ((if_tmp->is_ra_adv_dis == false) || |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 903 | (if_tmp->final_ra_sent_count < MAX_FINAL_RTR_ADVERTISEMENTS)) && |
| 904 | (if_tmp->first_ra_delayed)) { |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 905 | send_ra(now, param.iface, param.name, NULL); |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 906 | |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 907 | /* Also send on all interfaces that are aliases of this |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 908 | one. */ |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 909 | for (aparam.bridge = daemon->bridges; |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 910 | aparam.bridge; |
| 911 | aparam.bridge = aparam.bridge->next) |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 912 | if ((int)if_nametoindex(aparam.bridge->iface) == param.iface) |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 913 | { |
| 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 Jerram | 4918bd5 | 2015-06-10 22:23:20 +0100 | [diff] [blame] | 951 | one --bridge-interface. */ |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 952 | break; |
| 953 | } |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 954 | } else { |
| 955 | if_tmp->first_ra_delayed = true; |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 956 | } |
| 957 | } |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 958 | } |
Simon Kelley | 421594f | 2012-12-02 12:17:35 +0000 | [diff] [blame] | 959 | } |
| 960 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 961 | return next_event; |
| 962 | } |
Neil Jerram | 9bad339 | 2015-06-10 22:16:35 +0100 | [diff] [blame] | 963 | |
| 964 | static 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 Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 986 | static int iface_search(struct in6_addr *local, int prefix, |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 987 | int scope, int if_index, int flags, |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 988 | int preferred, int valid, void *vparam) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 989 | { |
| 990 | struct search_param *param = vparam; |
Simon Kelley | 741c295 | 2012-02-25 13:09:18 +0000 | [diff] [blame] | 991 | struct dhcp_context *context; |
Simon Kelley | cd67293 | 2020-01-27 22:53:07 +0000 | [diff] [blame] | 992 | struct iname *tmp; |
| 993 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 994 | (void)scope; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 995 | (void)preferred; |
| 996 | (void)valid; |
Simon Kelley | cd67293 | 2020-01-27 22:53:07 +0000 | [diff] [blame] | 997 | |
| 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 Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 1007 | for (context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 1008 | if (!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) && |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 1009 | prefix <= context->prefix && |
| 1010 | is_same_net6(local, &context->start6, context->prefix) && |
| 1011 | is_same_net6(local, &context->end6, context->prefix) && |
Simon Kelley | 6e3dba3 | 2012-12-16 21:52:45 +0000 | [diff] [blame] | 1012 | 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 Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 1018 | if (!(flags & IFACE_TENTATIVE)) |
Simon Kelley | 6e3dba3 | 2012-12-16 21:52:45 +0000 | [diff] [blame] | 1019 | param->iface = if_index; |
| 1020 | |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 1021 | new_timeout(context, param->name, param->now); |
Simon Kelley | 6e3dba3 | 2012-12-16 21:52:45 +0000 | [diff] [blame] | 1022 | |
| 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 Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 1026 | if (prefix <= context->prefix && |
| 1027 | is_same_net6(local, &context->start6, context->prefix) && |
| 1028 | is_same_net6(local, &context->end6, context->prefix)) |
Simon Kelley | 6e3dba3 | 2012-12-16 21:52:45 +0000 | [diff] [blame] | 1029 | context->ra_time = 0; |
| 1030 | |
| 1031 | return 0; /* found, abort */ |
| 1032 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 1033 | |
| 1034 | return 1; /* keep searching */ |
| 1035 | } |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 1036 | |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 1037 | 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] | 1038 | { |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 1039 | struct ra_interface *ra = find_iface_param(iface_name); |
tarun.kundu | a0e036f | 2022-12-14 07:02:58 -0800 | [diff] [blame] | 1040 | if (ra && ra->ra_sent_count < (MAX_INITIAL_RTR_ADVERTISEMENTS-1)) |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 1041 | context->ra_time = now + MAX_INITIAL_RTR_ADVERT_INTERVAL; |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 1042 | else |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 1043 | { |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 1044 | unsigned int adv_interval = calc_interval(ra); |
| 1045 | context->ra_time = now + adv_interval; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | static 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 | |
| 1060 | static unsigned int calc_interval(struct ra_interface *ra) |
| 1061 | { |
Tarun Kundu | 74fb2b6 | 2022-07-06 09:11:18 -0700 | [diff] [blame] | 1062 | int interval = DEFAULT_RTR_ADV_INTERVAL; |
Tarun Kundu | 36e8710 | 2022-07-20 14:43:44 -0700 | [diff] [blame] | 1063 | if (ra) { |
Tarun Kundu | 74fb2b6 | 2022-07-06 09:11:18 -0700 | [diff] [blame] | 1064 | 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 Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 1076 | |
Tarun Kundu | 74fb2b6 | 2022-07-06 09:11:18 -0700 | [diff] [blame] | 1077 | if (((ra->ra_sent_count + 1) < MAX_INITIAL_RTR_ADVERTISEMENTS) && |
| 1078 | interval > MAX_INITIAL_RTR_ADVERT_INTERVAL) |
tarun.kundu | 7297198 | 2022-11-10 08:42:59 -0800 | [diff] [blame] | 1079 | interval = MAX_INITIAL_RTR_ADVERT_INTERVAL; |
Tarun Kundu | 36e8710 | 2022-07-20 14:43:44 -0700 | [diff] [blame] | 1080 | } |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 1081 | return (unsigned int)interval; |
| 1082 | } |
| 1083 | |
| 1084 | static 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 | |
| 1102 | static unsigned int calc_prio(struct ra_interface *ra) |
| 1103 | { |
| 1104 | if (ra) |
| 1105 | return ra->prio; |
| 1106 | |
| 1107 | return 0; |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 1108 | } |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 1109 | |
Tarun Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 1110 | static 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 Kundu | 41aa488 | 2022-07-05 10:44:13 -0700 | [diff] [blame] | 1118 | static 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 Kundu | 024baf1 | 2022-06-28 10:56:47 -0700 | [diff] [blame] | 1126 | static unsigned int calc_reachable_time(struct ra_interface *ra) |
| 1127 | { |
| 1128 | if (ra) { |
| 1129 | return ra->reachable_time; |
| 1130 | } |
| 1131 | return 0; |
| 1132 | } |
Tarun Kundu | 01293a7 | 2022-07-18 23:50:36 -0700 | [diff] [blame] | 1133 | |
| 1134 | int64_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 Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 1141 | #endif |