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