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