Simon Kelley | d1ced3a | 2018-01-01 22:18:03 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2018 Simon Kelley |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +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 | #include "dnsmasq.h" |
| 18 | |
| 19 | #ifdef HAVE_DHCP6 |
| 20 | |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 21 | #include <netinet/icmp6.h> |
| 22 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 23 | struct iface_param { |
| 24 | struct dhcp_context *current; |
Simon Kelley | ff7eea2 | 2013-09-04 18:01:38 +0100 | [diff] [blame] | 25 | struct dhcp_relay *relay; |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 26 | struct in6_addr fallback, relay_local, ll_addr, ula_addr; |
Simon Kelley | be6cfb4 | 2012-10-16 20:38:31 +0100 | [diff] [blame] | 27 | int ind, addr_match; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 30 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 31 | static int complete_context6(struct in6_addr *local, int prefix, |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 32 | int scope, int if_index, int flags, |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 33 | unsigned int preferred, unsigned int valid, void *vparam); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 34 | static int make_duid1(int index, unsigned int type, char *mac, size_t maclen, void *parm); |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 35 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 36 | void dhcp6_init(void) |
| 37 | { |
| 38 | int fd; |
| 39 | struct sockaddr_in6 saddr; |
Simon Kelley | 0e88d53 | 2012-03-28 22:22:05 +0100 | [diff] [blame] | 40 | #if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 41 | int class = IPTOS_CLASS_CS6; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 42 | #endif |
Simon Kelley | 2022310 | 2012-10-15 10:41:17 +0100 | [diff] [blame] | 43 | int oneopt = 1; |
| 44 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 45 | if ((fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP)) == -1 || |
Simon Kelley | 0e88d53 | 2012-03-28 22:22:05 +0100 | [diff] [blame] | 46 | #if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 47 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &class, sizeof(class)) == -1 || |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 48 | #endif |
Simon Kelley | 2022310 | 2012-10-15 10:41:17 +0100 | [diff] [blame] | 49 | setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &oneopt, sizeof(oneopt)) == -1 || |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 50 | !fix_fd(fd) || |
| 51 | !set_ipv6pktinfo(fd)) |
| 52 | die (_("cannot create DHCPv6 socket: %s"), NULL, EC_BADNET); |
| 53 | |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 54 | /* When bind-interfaces is set, there might be more than one dnsmasq |
Simon Kelley | 2022310 | 2012-10-15 10:41:17 +0100 | [diff] [blame] | 55 | instance binding port 547. That's OK if they serve different networks. |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 56 | Need to set REUSEADDR|REUSEPORT to make this possible. |
Simon Kelley | 56a1142 | 2013-04-02 17:02:58 +0100 | [diff] [blame] | 57 | Handle the case that REUSEPORT is defined, but the kernel doesn't |
| 58 | support it. This handles the introduction of REUSEPORT on Linux. */ |
Simon Kelley | 2022310 | 2012-10-15 10:41:17 +0100 | [diff] [blame] | 59 | if (option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND)) |
| 60 | { |
Simon Kelley | ffbad34 | 2013-08-14 15:53:57 +0100 | [diff] [blame] | 61 | int rc = 0; |
Simon Kelley | 56a1142 | 2013-04-02 17:02:58 +0100 | [diff] [blame] | 62 | |
Simon Kelley | 2022310 | 2012-10-15 10:41:17 +0100 | [diff] [blame] | 63 | #ifdef SO_REUSEPORT |
Simon Kelley | 56a1142 | 2013-04-02 17:02:58 +0100 | [diff] [blame] | 64 | if ((rc = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &oneopt, sizeof(oneopt))) == -1 && |
Simon Kelley | ffbad34 | 2013-08-14 15:53:57 +0100 | [diff] [blame] | 65 | errno == ENOPROTOOPT) |
| 66 | rc = 0; |
Simon Kelley | 2022310 | 2012-10-15 10:41:17 +0100 | [diff] [blame] | 67 | #endif |
Simon Kelley | 56a1142 | 2013-04-02 17:02:58 +0100 | [diff] [blame] | 68 | |
Simon Kelley | ffbad34 | 2013-08-14 15:53:57 +0100 | [diff] [blame] | 69 | if (rc != -1) |
Simon Kelley | 56a1142 | 2013-04-02 17:02:58 +0100 | [diff] [blame] | 70 | rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &oneopt, sizeof(oneopt)); |
| 71 | |
Simon Kelley | 2022310 | 2012-10-15 10:41:17 +0100 | [diff] [blame] | 72 | if (rc == -1) |
| 73 | die(_("failed to set SO_REUSE{ADDR|PORT} on DHCPv6 socket: %s"), NULL, EC_BADNET); |
| 74 | } |
| 75 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 76 | memset(&saddr, 0, sizeof(saddr)); |
| 77 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 78 | saddr.sin6_len = sizeof(struct sockaddr_in6); |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 79 | #endif |
| 80 | saddr.sin6_family = AF_INET6; |
| 81 | saddr.sin6_addr = in6addr_any; |
| 82 | saddr.sin6_port = htons(DHCPV6_SERVER_PORT); |
| 83 | |
| 84 | if (bind(fd, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in6))) |
| 85 | die(_("failed to bind DHCPv6 server socket: %s"), NULL, EC_BADNET); |
| 86 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 87 | daemon->dhcp6fd = fd; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 90 | void dhcp6_packet(time_t now) |
| 91 | { |
| 92 | struct dhcp_context *context; |
Simon Kelley | ff7eea2 | 2013-09-04 18:01:38 +0100 | [diff] [blame] | 93 | struct dhcp_relay *relay; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 94 | struct iface_param parm; |
| 95 | struct cmsghdr *cmptr; |
| 96 | struct msghdr msg; |
| 97 | int if_index = 0; |
| 98 | union { |
| 99 | struct cmsghdr align; /* this ensures alignment */ |
| 100 | char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
| 101 | } control_u; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 102 | struct sockaddr_in6 from; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 103 | ssize_t sz; |
| 104 | struct ifreq ifr; |
| 105 | struct iname *tmp; |
Simon Kelley | 1d0f91c | 2012-03-12 11:56:22 +0000 | [diff] [blame] | 106 | unsigned short port; |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 107 | struct in6_addr dst_addr; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 108 | |
Simon Kelley | 8f51a29 | 2013-09-21 14:07:12 +0100 | [diff] [blame] | 109 | memset(&dst_addr, 0, sizeof(dst_addr)); |
| 110 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 111 | msg.msg_control = control_u.control6; |
| 112 | msg.msg_controllen = sizeof(control_u); |
| 113 | msg.msg_flags = 0; |
| 114 | msg.msg_name = &from; |
| 115 | msg.msg_namelen = sizeof(from); |
| 116 | msg.msg_iov = &daemon->dhcp_packet; |
| 117 | msg.msg_iovlen = 1; |
| 118 | |
Simon Kelley | 1d0f91c | 2012-03-12 11:56:22 +0000 | [diff] [blame] | 119 | if ((sz = recv_dhcp_packet(daemon->dhcp6fd, &msg)) == -1) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 120 | return; |
| 121 | |
| 122 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 123 | if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo) |
| 124 | { |
| 125 | union { |
| 126 | unsigned char *c; |
| 127 | struct in6_pktinfo *p; |
| 128 | } p; |
| 129 | p.c = CMSG_DATA(cmptr); |
| 130 | |
| 131 | if_index = p.p->ipi6_ifindex; |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 132 | dst_addr = p.p->ipi6_addr; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | if (!indextoname(daemon->dhcp6fd, if_index, ifr.ifr_name)) |
| 136 | return; |
Simon Kelley | be6cfb4 | 2012-10-16 20:38:31 +0100 | [diff] [blame] | 137 | |
Simon Kelley | 18a6bdd | 2019-12-20 18:19:20 +0000 | [diff] [blame^] | 138 | if ((port = relay_reply6(&from, sz, ifr.ifr_name)) != 0) |
| 139 | { |
| 140 | from.sin6_port = htons(port); |
| 141 | while (retry_send(sendto(daemon->dhcp6fd, daemon->outpacket.iov_base, |
| 142 | save_counter(-1), 0, (struct sockaddr *)&from, |
| 143 | sizeof(from)))); |
| 144 | } |
| 145 | else |
Simon Kelley | be6cfb4 | 2012-10-16 20:38:31 +0100 | [diff] [blame] | 146 | { |
Neil Jerram | 0ddb876 | 2015-06-10 22:11:06 +0100 | [diff] [blame] | 147 | struct dhcp_bridge *bridge, *alias; |
| 148 | |
Simon Kelley | ff7eea2 | 2013-09-04 18:01:38 +0100 | [diff] [blame] | 149 | for (tmp = daemon->if_except; tmp; tmp = tmp->next) |
Simon Kelley | 49333cb | 2013-03-15 20:30:51 +0000 | [diff] [blame] | 150 | if (tmp->name && wildcard_match(tmp->name, ifr.ifr_name)) |
Simon Kelley | ff7eea2 | 2013-09-04 18:01:38 +0100 | [diff] [blame] | 151 | return; |
| 152 | |
| 153 | for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
| 154 | if (tmp->name && wildcard_match(tmp->name, ifr.ifr_name)) |
| 155 | return; |
| 156 | |
| 157 | parm.current = NULL; |
| 158 | parm.relay = NULL; |
| 159 | memset(&parm.relay_local, 0, IN6ADDRSZ); |
| 160 | parm.ind = if_index; |
| 161 | parm.addr_match = 0; |
| 162 | memset(&parm.fallback, 0, IN6ADDRSZ); |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 163 | memset(&parm.ll_addr, 0, IN6ADDRSZ); |
| 164 | memset(&parm.ula_addr, 0, IN6ADDRSZ); |
Neil Jerram | 0ddb876 | 2015-06-10 22:11:06 +0100 | [diff] [blame] | 165 | |
| 166 | /* If the interface on which the DHCPv6 request was received is |
| 167 | an alias of some other interface (as specified by the |
Neil Jerram | 4918bd5 | 2015-06-10 22:23:20 +0100 | [diff] [blame] | 168 | --bridge-interface option), change parm.ind so that we look |
Neil Jerram | 0ddb876 | 2015-06-10 22:11:06 +0100 | [diff] [blame] | 169 | for DHCPv6 contexts associated with the aliased interface |
| 170 | instead of with the aliasing one. */ |
| 171 | for (bridge = daemon->bridges; bridge; bridge = bridge->next) |
| 172 | { |
| 173 | for (alias = bridge->alias; alias; alias = alias->next) |
| 174 | if (wildcard_matchn(alias->iface, ifr.ifr_name, IF_NAMESIZE)) |
| 175 | { |
| 176 | parm.ind = if_nametoindex(bridge->iface); |
| 177 | if (!parm.ind) |
| 178 | { |
| 179 | my_syslog(MS_DHCP | LOG_WARNING, |
| 180 | _("unknown interface %s in bridge-interface"), |
| 181 | bridge->iface); |
| 182 | return; |
| 183 | } |
| 184 | break; |
| 185 | } |
| 186 | if (alias) |
| 187 | break; |
| 188 | } |
Simon Kelley | ff7eea2 | 2013-09-04 18:01:38 +0100 | [diff] [blame] | 189 | |
| 190 | for (context = daemon->dhcp6; context; context = context->next) |
| 191 | if (IN6_IS_ADDR_UNSPECIFIED(&context->start6) && context->prefix == 0) |
| 192 | { |
| 193 | /* wildcard context for DHCP-stateless only */ |
| 194 | parm.current = context; |
| 195 | context->current = NULL; |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | /* unlinked contexts are marked by context->current == context */ |
| 200 | context->current = context; |
| 201 | memset(&context->local6, 0, IN6ADDRSZ); |
| 202 | } |
Simon Kelley | be6cfb4 | 2012-10-16 20:38:31 +0100 | [diff] [blame] | 203 | |
Simon Kelley | ff7eea2 | 2013-09-04 18:01:38 +0100 | [diff] [blame] | 204 | for (relay = daemon->relay6; relay; relay = relay->next) |
| 205 | relay->current = relay; |
| 206 | |
| 207 | if (!iface_enumerate(AF_INET6, &parm, complete_context6)) |
Simon Kelley | be6cfb4 | 2012-10-16 20:38:31 +0100 | [diff] [blame] | 208 | return; |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 209 | |
Simon Kelley | ff7eea2 | 2013-09-04 18:01:38 +0100 | [diff] [blame] | 210 | if (daemon->if_names || daemon->if_addrs) |
| 211 | { |
| 212 | |
| 213 | for (tmp = daemon->if_names; tmp; tmp = tmp->next) |
| 214 | if (tmp->name && wildcard_match(tmp->name, ifr.ifr_name)) |
| 215 | break; |
| 216 | |
| 217 | if (!tmp && !parm.addr_match) |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | if (parm.relay) |
| 222 | { |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 223 | /* Ignore requests sent to the ALL_SERVERS multicast address for relay when |
| 224 | we're listening there for DHCPv6 server reasons. */ |
| 225 | struct in6_addr all_servers; |
| 226 | |
| 227 | inet_pton(AF_INET6, ALL_SERVERS, &all_servers); |
| 228 | |
| 229 | if (!IN6_ARE_ADDR_EQUAL(&dst_addr, &all_servers)) |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 230 | relay_upstream6(parm.relay, sz, &from.sin6_addr, from.sin6_scope_id, now); |
Simon Kelley | ff7eea2 | 2013-09-04 18:01:38 +0100 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
| 234 | /* May have configured relay, but not DHCP server */ |
| 235 | if (!daemon->doing_dhcp6) |
| 236 | return; |
Neil Jerram | 0ddb876 | 2015-06-10 22:11:06 +0100 | [diff] [blame] | 237 | |
Simon Kelley | ff7eea2 | 2013-09-04 18:01:38 +0100 | [diff] [blame] | 238 | lease_prune(NULL, now); /* lose any expired leases */ |
| 239 | |
| 240 | port = dhcp6_reply(parm.current, if_index, ifr.ifr_name, &parm.fallback, |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 241 | &parm.ll_addr, &parm.ula_addr, sz, &from.sin6_addr, now); |
Simon Kelley | ff7eea2 | 2013-09-04 18:01:38 +0100 | [diff] [blame] | 242 | |
Simon Kelley | 18a6bdd | 2019-12-20 18:19:20 +0000 | [diff] [blame^] | 243 | /* The port in the source address of the original request should |
| 244 | be correct, but at least once client sends from the server port, |
| 245 | so we explicitly send to the client port to a client, and the |
| 246 | server port to a relay. */ |
| 247 | if (port != 0) |
| 248 | { |
| 249 | from.sin6_port = htons(port); |
| 250 | while (retry_send(sendto(daemon->dhcp6fd, daemon->outpacket.iov_base, |
| 251 | save_counter(-1), 0, (struct sockaddr *)&from, |
| 252 | sizeof(from)))); |
| 253 | } |
| 254 | |
| 255 | /* These need to be called _after_ we send DHCPv6 packet, since lease_update_file() |
| 256 | may trigger sending an RA packet, which overwrites our buffer. */ |
Simon Kelley | ff7eea2 | 2013-09-04 18:01:38 +0100 | [diff] [blame] | 257 | lease_update_file(now); |
| 258 | lease_update_dns(0); |
Simon Kelley | be6cfb4 | 2012-10-16 20:38:31 +0100 | [diff] [blame] | 259 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 262 | void get_client_mac(struct in6_addr *client, int iface, unsigned char *mac, unsigned int *maclenp, unsigned int *mactypep, time_t now) |
Simon Kelley | 8939c95 | 2013-09-25 11:49:34 +0100 | [diff] [blame] | 263 | { |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 264 | /* Receiving a packet from a host does not populate the neighbour |
Simon Kelley | 8939c95 | 2013-09-25 11:49:34 +0100 | [diff] [blame] | 265 | cache, so we send a neighbour discovery request if we can't |
| 266 | find the sender. Repeat a few times in case of packet loss. */ |
| 267 | |
| 268 | struct neigh_packet neigh; |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 269 | union mysockaddr addr; |
| 270 | int i, maclen; |
Simon Kelley | 8939c95 | 2013-09-25 11:49:34 +0100 | [diff] [blame] | 271 | |
| 272 | neigh.type = ND_NEIGHBOR_SOLICIT; |
| 273 | neigh.code = 0; |
| 274 | neigh.reserved = 0; |
| 275 | neigh.target = *client; |
Tomas Hozza | 0705a7e | 2015-02-23 21:26:26 +0000 | [diff] [blame] | 276 | /* RFC4443 section-2.3: checksum has to be zero to be calculated */ |
| 277 | neigh.checksum = 0; |
| 278 | |
Simon Kelley | 8939c95 | 2013-09-25 11:49:34 +0100 | [diff] [blame] | 279 | memset(&addr, 0, sizeof(addr)); |
| 280 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 281 | addr.in6.sin6_len = sizeof(struct sockaddr_in6); |
Simon Kelley | 8939c95 | 2013-09-25 11:49:34 +0100 | [diff] [blame] | 282 | #endif |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 283 | addr.in6.sin6_family = AF_INET6; |
| 284 | addr.in6.sin6_port = htons(IPPROTO_ICMPV6); |
| 285 | addr.in6.sin6_addr = *client; |
| 286 | addr.in6.sin6_scope_id = iface; |
Simon Kelley | 8939c95 | 2013-09-25 11:49:34 +0100 | [diff] [blame] | 287 | |
| 288 | for (i = 0; i < 5; i++) |
| 289 | { |
| 290 | struct timespec ts; |
| 291 | |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 292 | if ((maclen = find_mac(&addr, mac, 0, now)) != 0) |
Simon Kelley | 8939c95 | 2013-09-25 11:49:34 +0100 | [diff] [blame] | 293 | break; |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 294 | |
| 295 | sendto(daemon->icmp6fd, &neigh, sizeof(neigh), 0, &addr.sa, sizeof(addr)); |
Simon Kelley | 8939c95 | 2013-09-25 11:49:34 +0100 | [diff] [blame] | 296 | |
| 297 | ts.tv_sec = 0; |
| 298 | ts.tv_nsec = 100000000; /* 100ms */ |
| 299 | nanosleep(&ts, NULL); |
| 300 | } |
| 301 | |
Simon Kelley | 11867dc | 2015-12-23 16:15:58 +0000 | [diff] [blame] | 302 | *maclenp = maclen; |
Simon Kelley | 8939c95 | 2013-09-25 11:49:34 +0100 | [diff] [blame] | 303 | *mactypep = ARPHRD_ETHER; |
| 304 | } |
| 305 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 306 | static int complete_context6(struct in6_addr *local, int prefix, |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 307 | int scope, int if_index, int flags, unsigned int preferred, |
| 308 | unsigned int valid, void *vparam) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 309 | { |
| 310 | struct dhcp_context *context; |
Simon Kelley | ae5b7e0 | 2019-03-27 22:33:28 +0000 | [diff] [blame] | 311 | struct shared_network *share; |
Simon Kelley | ff7eea2 | 2013-09-04 18:01:38 +0100 | [diff] [blame] | 312 | struct dhcp_relay *relay; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 313 | struct iface_param *param = vparam; |
Simon Kelley | be6cfb4 | 2012-10-16 20:38:31 +0100 | [diff] [blame] | 314 | struct iname *tmp; |
| 315 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 316 | (void)scope; /* warning */ |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 317 | |
Simon Kelley | ae5b7e0 | 2019-03-27 22:33:28 +0000 | [diff] [blame] | 318 | if (if_index != param->ind) |
| 319 | return 1; |
| 320 | |
| 321 | if (IN6_IS_ADDR_LINKLOCAL(local)) |
| 322 | param->ll_addr = *local; |
| 323 | else if (IN6_IS_ADDR_ULA(local)) |
| 324 | param->ula_addr = *local; |
Simon Kelley | ff7eea2 | 2013-09-04 18:01:38 +0100 | [diff] [blame] | 325 | |
Simon Kelley | ae5b7e0 | 2019-03-27 22:33:28 +0000 | [diff] [blame] | 326 | if (IN6_IS_ADDR_LOOPBACK(local) || |
| 327 | IN6_IS_ADDR_LINKLOCAL(local) || |
| 328 | IN6_IS_ADDR_MULTICAST(local)) |
| 329 | return 1; |
| 330 | |
| 331 | /* if we have --listen-address config, see if the |
| 332 | arrival interface has a matching address. */ |
| 333 | for (tmp = daemon->if_addrs; tmp; tmp = tmp->next) |
| 334 | if (tmp->addr.sa.sa_family == AF_INET6 && |
| 335 | IN6_ARE_ADDR_EQUAL(&tmp->addr.in6.sin6_addr, local)) |
| 336 | param->addr_match = 1; |
| 337 | |
| 338 | /* Determine a globally address on the arrival interface, even |
| 339 | if we have no matching dhcp-context, because we're only |
| 340 | allocating on remote subnets via relays. This |
| 341 | is used as a default for the DNS server option. */ |
| 342 | param->fallback = *local; |
| 343 | |
| 344 | for (context = daemon->dhcp6; context; context = context->next) |
| 345 | if ((context->flags & CONTEXT_DHCP) && |
| 346 | !(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) && |
| 347 | prefix <= context->prefix && |
| 348 | context->current == context) |
| 349 | { |
| 350 | if (is_same_net6(local, &context->start6, context->prefix) && |
| 351 | is_same_net6(local, &context->end6, context->prefix)) |
| 352 | { |
| 353 | struct dhcp_context *tmp, **up; |
| 354 | |
| 355 | /* use interface values only for constructed contexts */ |
| 356 | if (!(context->flags & CONTEXT_CONSTRUCTED)) |
| 357 | preferred = valid = 0xffffffff; |
| 358 | else if (flags & IFACE_DEPRECATED) |
| 359 | preferred = 0; |
| 360 | |
| 361 | if (context->flags & CONTEXT_DEPRECATE) |
| 362 | preferred = 0; |
| 363 | |
| 364 | /* order chain, longest preferred time first */ |
| 365 | for (up = ¶m->current, tmp = param->current; tmp; tmp = tmp->current) |
| 366 | if (tmp->preferred <= preferred) |
| 367 | break; |
| 368 | else |
| 369 | up = &tmp->current; |
| 370 | |
| 371 | context->current = *up; |
| 372 | *up = context; |
| 373 | context->local6 = *local; |
| 374 | context->preferred = preferred; |
| 375 | context->valid = valid; |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | for (share = daemon->shared_networks; share; share = share->next) |
| 380 | { |
| 381 | /* IPv4 shared_address - ignore */ |
| 382 | if (share->shared_addr.s_addr != 0) |
| 383 | continue; |
| 384 | |
| 385 | if (share->if_index != 0) |
| 386 | { |
| 387 | if (share->if_index != if_index) |
| 388 | continue; |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | if (!IN6_ARE_ADDR_EQUAL(&share->match_addr6, local)) |
| 393 | continue; |
| 394 | } |
| 395 | |
| 396 | if (is_same_net6(&share->shared_addr6, &context->start6, context->prefix) && |
| 397 | is_same_net6(&share->shared_addr6, &context->end6, context->prefix)) |
| 398 | { |
| 399 | context->current = param->current; |
| 400 | param->current = context; |
| 401 | context->local6 = *local; |
| 402 | context->preferred = context->flags & CONTEXT_DEPRECATE ? 0 :0xffffffff; |
| 403 | context->valid = 0xffffffff; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | for (relay = daemon->relay6; relay; relay = relay->next) |
| 410 | if (IN6_ARE_ADDR_EQUAL(local, &relay->local.addr6) && relay->current == relay && |
| 411 | (IN6_IS_ADDR_UNSPECIFIED(¶m->relay_local) || IN6_ARE_ADDR_EQUAL(local, ¶m->relay_local))) |
| 412 | { |
| 413 | relay->current = param->relay; |
| 414 | param->relay = relay; |
| 415 | param->relay_local = *local; |
| 416 | } |
| 417 | |
| 418 | return 1; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 419 | } |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 420 | |
| 421 | struct dhcp_config *config_find_by_address6(struct dhcp_config *configs, struct in6_addr *net, int prefix, u64 addr) |
| 422 | { |
| 423 | struct dhcp_config *config; |
| 424 | |
| 425 | for (config = configs; config; config = config->next) |
| 426 | if ((config->flags & CONFIG_ADDR6) && |
| 427 | is_same_net6(&config->addr6, net, prefix) && |
| 428 | (prefix == 128 || addr6part(&config->addr6) == addr)) |
| 429 | return config; |
| 430 | |
| 431 | return NULL; |
| 432 | } |
| 433 | |
Simon Kelley | 6586e83 | 2013-11-07 14:20:13 +0000 | [diff] [blame] | 434 | struct dhcp_context *address6_allocate(struct dhcp_context *context, unsigned char *clid, int clid_len, int temp_addr, |
Dominik DL6ER | 456a319 | 2019-10-20 18:51:52 +0200 | [diff] [blame] | 435 | unsigned int iaid, int serial, struct dhcp_netid *netids, int plain_range, struct in6_addr *ans) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 436 | { |
| 437 | /* Find a free address: exclude anything in use and anything allocated to |
| 438 | a particular hwaddr/clientid/hostname in our configuration. |
| 439 | Try to return from contexts which match netids first. |
| 440 | |
| 441 | Note that we assume the address prefix lengths are 64 or greater, so we can |
| 442 | get by with 64 bit arithmetic. |
| 443 | */ |
| 444 | |
| 445 | u64 start, addr; |
| 446 | struct dhcp_context *c, *d; |
| 447 | int i, pass; |
| 448 | u64 j; |
| 449 | |
| 450 | /* hash hwaddr: use the SDBM hashing algorithm. This works |
Simon Kelley | 6586e83 | 2013-11-07 14:20:13 +0000 | [diff] [blame] | 451 | for MAC addresses, let's see how it manages with client-ids! |
| 452 | For temporary addresses, we generate a new random one each time. */ |
| 453 | if (temp_addr) |
| 454 | j = rand64(); |
| 455 | else |
| 456 | for (j = iaid, i = 0; i < clid_len; i++) |
Simon Kelley | d6b749a | 2016-04-25 17:05:15 +0100 | [diff] [blame] | 457 | j = clid[i] + (j << 6) + (j << 16) - j; |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 458 | |
Simon Kelley | c630924 | 2013-03-07 20:59:28 +0000 | [diff] [blame] | 459 | for (pass = 0; pass <= plain_range ? 1 : 0; pass++) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 460 | for (c = context; c; c = c->current) |
Simon Kelley | a6ebfac | 2013-03-06 20:52:35 +0000 | [diff] [blame] | 461 | if (c->flags & (CONTEXT_DEPRECATE | CONTEXT_STATIC | CONTEXT_RA_STATELESS | CONTEXT_USED)) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 462 | continue; |
| 463 | else if (!match_netid(c->filter, netids, pass)) |
| 464 | continue; |
| 465 | else |
Simon Kelley | 0793380 | 2012-02-14 20:55:25 +0000 | [diff] [blame] | 466 | { |
Simon Kelley | 6586e83 | 2013-11-07 14:20:13 +0000 | [diff] [blame] | 467 | if (!temp_addr && option_bool(OPT_CONSEC_ADDR)) |
Simon Kelley | e7bfd55 | 2018-12-31 20:51:15 +0000 | [diff] [blame] | 468 | { |
| 469 | /* seed is largest extant lease addr in this context, |
| 470 | skip addresses equal to the number of addresses rejected |
| 471 | by clients. This should avoid the same client being offered the same |
| 472 | address after it has rjected it. */ |
Simon Kelley | 9c0d445 | 2019-01-09 17:57:56 +0000 | [diff] [blame] | 473 | start = lease_find_max_addr6(c) + 1 + serial + c->addr_epoch; |
Simon Kelley | e7bfd55 | 2018-12-31 20:51:15 +0000 | [diff] [blame] | 474 | if (c->addr_epoch) |
| 475 | c->addr_epoch--; |
| 476 | } |
Simon Kelley | 0793380 | 2012-02-14 20:55:25 +0000 | [diff] [blame] | 477 | else |
Simon Kelley | fdc97e1 | 2016-02-13 17:47:17 +0000 | [diff] [blame] | 478 | { |
| 479 | u64 range = 1 + addr6part(&c->end6) - addr6part(&c->start6); |
| 480 | u64 offset = j + c->addr_epoch; |
| 481 | |
| 482 | /* don't divide by zero if range is whole 2^64 */ |
| 483 | if (range != 0) |
| 484 | offset = offset % range; |
| 485 | |
| 486 | start = addr6part(&c->start6) + offset; |
| 487 | } |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 488 | |
| 489 | /* iterate until we find a free address. */ |
| 490 | addr = start; |
| 491 | |
| 492 | do { |
| 493 | /* eliminate addresses in use by the server. */ |
| 494 | for (d = context; d; d = d->current) |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 495 | if (addr == addr6part(&d->local6)) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 496 | break; |
| 497 | |
| 498 | if (!d && |
| 499 | !lease6_find_by_addr(&c->start6, c->prefix, addr) && |
| 500 | !config_find_by_address6(daemon->dhcp_conf, &c->start6, c->prefix, addr)) |
| 501 | { |
| 502 | *ans = c->start6; |
| 503 | setaddr6part (ans, addr); |
Simon Kelley | a6ebfac | 2013-03-06 20:52:35 +0000 | [diff] [blame] | 504 | return c; |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | addr++; |
| 508 | |
| 509 | if (addr == addr6part(&c->end6) + 1) |
| 510 | addr = addr6part(&c->start6); |
| 511 | |
| 512 | } while (addr != start); |
| 513 | } |
Simon Kelley | a6ebfac | 2013-03-06 20:52:35 +0000 | [diff] [blame] | 514 | |
Simon Kelley | 5b37aa8 | 2013-04-02 16:32:25 +0100 | [diff] [blame] | 515 | return NULL; |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Simon Kelley | 37c9cce | 2013-01-09 19:51:04 +0000 | [diff] [blame] | 518 | /* can dynamically allocate addr */ |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 519 | struct dhcp_context *address6_available(struct dhcp_context *context, |
| 520 | struct in6_addr *taddr, |
Simon Kelley | c630924 | 2013-03-07 20:59:28 +0000 | [diff] [blame] | 521 | struct dhcp_netid *netids, |
| 522 | int plain_range) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 523 | { |
| 524 | u64 start, end, addr = addr6part(taddr); |
| 525 | struct dhcp_context *tmp; |
| 526 | |
| 527 | for (tmp = context; tmp; tmp = tmp->current) |
| 528 | { |
| 529 | start = addr6part(&tmp->start6); |
| 530 | end = addr6part(&tmp->end6); |
| 531 | |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 532 | if (!(tmp->flags & (CONTEXT_STATIC | CONTEXT_RA_STATELESS)) && |
| 533 | is_same_net6(&tmp->start6, taddr, tmp->prefix) && |
| 534 | is_same_net6(&tmp->end6, taddr, tmp->prefix) && |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 535 | addr >= start && |
| 536 | addr <= end && |
Simon Kelley | c630924 | 2013-03-07 20:59:28 +0000 | [diff] [blame] | 537 | match_netid(tmp->filter, netids, plain_range)) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 538 | return tmp; |
| 539 | } |
| 540 | |
| 541 | return NULL; |
| 542 | } |
| 543 | |
Simon Kelley | 37c9cce | 2013-01-09 19:51:04 +0000 | [diff] [blame] | 544 | /* address OK if configured */ |
| 545 | struct dhcp_context *address6_valid(struct dhcp_context *context, |
Simon Kelley | c630924 | 2013-03-07 20:59:28 +0000 | [diff] [blame] | 546 | struct in6_addr *taddr, |
| 547 | struct dhcp_netid *netids, |
| 548 | int plain_range) |
Simon Kelley | 37c9cce | 2013-01-09 19:51:04 +0000 | [diff] [blame] | 549 | { |
| 550 | struct dhcp_context *tmp; |
| 551 | |
| 552 | for (tmp = context; tmp; tmp = tmp->current) |
Simon Kelley | baeb3ad | 2013-01-10 11:47:38 +0000 | [diff] [blame] | 553 | if (is_same_net6(&tmp->start6, taddr, tmp->prefix) && |
Simon Kelley | c630924 | 2013-03-07 20:59:28 +0000 | [diff] [blame] | 554 | match_netid(tmp->filter, netids, plain_range)) |
Simon Kelley | 37c9cce | 2013-01-09 19:51:04 +0000 | [diff] [blame] | 555 | return tmp; |
| 556 | |
| 557 | return NULL; |
| 558 | } |
| 559 | |
Simon Kelley | de92b47 | 2013-03-15 18:25:10 +0000 | [diff] [blame] | 560 | int config_valid(struct dhcp_config *config, struct dhcp_context *context, struct in6_addr *addr) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 561 | { |
Simon Kelley | de92b47 | 2013-03-15 18:25:10 +0000 | [diff] [blame] | 562 | if (!config || !(config->flags & CONFIG_ADDR6)) |
| 563 | return 0; |
| 564 | |
| 565 | if ((config->flags & CONFIG_WILDCARD) && context->prefix == 64) |
Simon Kelley | 3039310 | 2013-01-17 16:34:16 +0000 | [diff] [blame] | 566 | { |
Simon Kelley | de92b47 | 2013-03-15 18:25:10 +0000 | [diff] [blame] | 567 | *addr = context->start6; |
| 568 | setaddr6part(addr, addr6part(&config->addr6)); |
Simon Kelley | 3039310 | 2013-01-17 16:34:16 +0000 | [diff] [blame] | 569 | return 1; |
| 570 | } |
| 571 | |
Simon Kelley | de92b47 | 2013-03-15 18:25:10 +0000 | [diff] [blame] | 572 | if (is_same_net6(&context->start6, &config->addr6, context->prefix)) |
| 573 | { |
| 574 | *addr = config->addr6; |
| 575 | return 1; |
| 576 | } |
| 577 | |
Simon Kelley | b269221 | 2012-09-16 22:15:56 +0100 | [diff] [blame] | 578 | return 0; |
| 579 | } |
| 580 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 581 | void make_duid(time_t now) |
| 582 | { |
Simon Kelley | 3511a92 | 2013-11-07 10:28:11 +0000 | [diff] [blame] | 583 | (void)now; |
| 584 | |
Simon Kelley | 8b37270 | 2012-03-09 17:45:10 +0000 | [diff] [blame] | 585 | if (daemon->duid_config) |
| 586 | { |
| 587 | unsigned char *p; |
| 588 | |
| 589 | daemon->duid = p = safe_malloc(daemon->duid_config_len + 6); |
| 590 | daemon->duid_len = daemon->duid_config_len + 6; |
| 591 | PUTSHORT(2, p); /* DUID_EN */ |
| 592 | PUTLONG(daemon->duid_enterprise, p); |
| 593 | memcpy(p, daemon->duid_config, daemon->duid_config_len); |
| 594 | } |
| 595 | else |
| 596 | { |
Simon Kelley | 3511a92 | 2013-11-07 10:28:11 +0000 | [diff] [blame] | 597 | time_t newnow = 0; |
| 598 | |
| 599 | /* If we have no persistent lease database, or a non-stable RTC, use DUID_LL (newnow == 0) */ |
| 600 | #ifndef HAVE_BROKEN_RTC |
Simon Kelley | 8b37270 | 2012-03-09 17:45:10 +0000 | [diff] [blame] | 601 | /* rebase epoch to 1/1/2000 */ |
Simon Kelley | 3511a92 | 2013-11-07 10:28:11 +0000 | [diff] [blame] | 602 | if (!option_bool(OPT_LEASE_RO) || daemon->lease_change_command) |
| 603 | newnow = now - 946684800; |
| 604 | #endif |
Simon Kelley | 8b37270 | 2012-03-09 17:45:10 +0000 | [diff] [blame] | 605 | |
| 606 | iface_enumerate(AF_LOCAL, &newnow, make_duid1); |
| 607 | |
| 608 | if(!daemon->duid) |
| 609 | die("Cannot create DHCPv6 server DUID: %s", NULL, EC_MISC); |
| 610 | } |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 613 | static int make_duid1(int index, unsigned int type, char *mac, size_t maclen, void *parm) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 614 | { |
| 615 | /* create DUID as specified in RFC3315. We use the MAC of the |
Simon Kelley | 0f08983 | 2012-03-01 13:43:39 +0000 | [diff] [blame] | 616 | first interface we find that isn't loopback or P-to-P and |
| 617 | has address-type < 256. Address types above 256 are things like |
| 618 | tunnels which don't have usable MAC addresses. */ |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 619 | |
| 620 | unsigned char *p; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 621 | (void)index; |
Vladislav Grishenko | 408c368 | 2013-09-24 16:18:49 +0100 | [diff] [blame] | 622 | (void)parm; |
Simon Kelley | 3511a92 | 2013-11-07 10:28:11 +0000 | [diff] [blame] | 623 | time_t newnow = *((time_t *)parm); |
| 624 | |
Simon Kelley | 0f08983 | 2012-03-01 13:43:39 +0000 | [diff] [blame] | 625 | if (type >= 256) |
| 626 | return 1; |
| 627 | |
Simon Kelley | 3511a92 | 2013-11-07 10:28:11 +0000 | [diff] [blame] | 628 | if (newnow == 0) |
| 629 | { |
| 630 | daemon->duid = p = safe_malloc(maclen + 4); |
| 631 | daemon->duid_len = maclen + 4; |
| 632 | PUTSHORT(3, p); /* DUID_LL */ |
| 633 | PUTSHORT(type, p); /* address type */ |
| 634 | } |
| 635 | else |
| 636 | { |
| 637 | daemon->duid = p = safe_malloc(maclen + 8); |
| 638 | daemon->duid_len = maclen + 8; |
| 639 | PUTSHORT(1, p); /* DUID_LLT */ |
| 640 | PUTSHORT(type, p); /* address type */ |
| 641 | PUTLONG(*((time_t *)parm), p); /* time */ |
| 642 | } |
| 643 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 644 | memcpy(p, mac, maclen); |
| 645 | |
| 646 | return 0; |
| 647 | } |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 648 | |
| 649 | struct cparam { |
| 650 | time_t now; |
Simon Kelley | 0c05024 | 2012-12-22 22:13:19 +0000 | [diff] [blame] | 651 | int newone, newname; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 652 | }; |
| 653 | |
| 654 | static int construct_worker(struct in6_addr *local, int prefix, |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 655 | int scope, int if_index, int flags, |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 656 | int preferred, int valid, void *vparam) |
| 657 | { |
| 658 | char ifrn_name[IFNAMSIZ]; |
| 659 | struct in6_addr start6, end6; |
| 660 | struct dhcp_context *template, *context; |
| 661 | |
| 662 | (void)scope; |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 663 | (void)flags; |
Simon Kelley | ed8b68a | 2012-12-21 16:23:26 +0000 | [diff] [blame] | 664 | (void)valid; |
| 665 | (void)preferred; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 666 | |
| 667 | struct cparam *param = vparam; |
| 668 | |
| 669 | if (IN6_IS_ADDR_LOOPBACK(local) || |
| 670 | IN6_IS_ADDR_LINKLOCAL(local) || |
| 671 | IN6_IS_ADDR_MULTICAST(local)) |
| 672 | return 1; |
| 673 | |
Simon Kelley | 861c891 | 2013-09-25 15:30:30 +0100 | [diff] [blame] | 674 | if (!(flags & IFACE_PERMANENT)) |
| 675 | return 1; |
| 676 | |
| 677 | if (flags & IFACE_DEPRECATED) |
| 678 | return 1; |
| 679 | |
Simon Kelley | a810559 | 2013-09-25 15:36:00 +0100 | [diff] [blame] | 680 | if (!indextoname(daemon->icmp6fd, if_index, ifrn_name)) |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 681 | return 0; |
| 682 | |
| 683 | for (template = daemon->dhcp6; template; template = template->next) |
Simon Kelley | db0f488 | 2018-06-07 21:37:02 +0100 | [diff] [blame] | 684 | if (!(template->flags & (CONTEXT_TEMPLATE | CONTEXT_CONSTRUCTED))) |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 685 | { |
| 686 | /* non-template entries, just fill in interface and local addresses */ |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 687 | if (prefix <= template->prefix && |
| 688 | is_same_net6(local, &template->start6, template->prefix) && |
| 689 | is_same_net6(local, &template->end6, template->prefix)) |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 690 | { |
Maarten de Vries | 0a496f0 | 2018-05-11 23:20:58 +0100 | [diff] [blame] | 691 | /* First time found, do fast RA. */ |
| 692 | if (template->if_index != if_index || !IN6_ARE_ADDR_EQUAL(&template->local6, local)) |
| 693 | { |
| 694 | ra_start_unsolicited(param->now, template); |
| 695 | param->newone = 1; |
| 696 | } |
| 697 | |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 698 | template->if_index = if_index; |
| 699 | template->local6 = *local; |
| 700 | } |
| 701 | |
| 702 | } |
Simon Kelley | 486479e | 2013-10-14 17:18:03 +0100 | [diff] [blame] | 703 | else if (wildcard_match(template->template_interface, ifrn_name) && |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 704 | template->prefix >= prefix) |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 705 | { |
| 706 | start6 = *local; |
| 707 | setaddr6part(&start6, addr6part(&template->start6)); |
| 708 | end6 = *local; |
| 709 | setaddr6part(&end6, addr6part(&template->end6)); |
| 710 | |
| 711 | for (context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | c488b68 | 2018-06-02 13:06:00 +0100 | [diff] [blame] | 712 | if (!(context->flags & CONTEXT_TEMPLATE) && |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 713 | IN6_ARE_ADDR_EQUAL(&start6, &context->start6) && |
| 714 | IN6_ARE_ADDR_EQUAL(&end6, &context->end6)) |
| 715 | { |
Simon Kelley | db0f488 | 2018-06-07 21:37:02 +0100 | [diff] [blame] | 716 | /* If there's an absolute address context covering this address |
| 717 | then don't construct one as well. */ |
| 718 | if (!(context->flags & CONTEXT_CONSTRUCTED)) |
| 719 | break; |
| 720 | |
| 721 | if (context->if_index == if_index) |
Simon Kelley | f7a40ec | 2013-07-27 13:36:08 +0100 | [diff] [blame] | 722 | { |
Simon Kelley | c488b68 | 2018-06-02 13:06:00 +0100 | [diff] [blame] | 723 | int cflags = context->flags; |
| 724 | context->flags &= ~(CONTEXT_GC | CONTEXT_OLD); |
| 725 | if (cflags & CONTEXT_OLD) |
| 726 | { |
Simon Kelley | db0f488 | 2018-06-07 21:37:02 +0100 | [diff] [blame] | 727 | /* address went, now it's back, and on the same interface */ |
Simon Kelley | c488b68 | 2018-06-02 13:06:00 +0100 | [diff] [blame] | 728 | log_context(AF_INET6, context); |
| 729 | /* fast RAs for a while */ |
| 730 | ra_start_unsolicited(param->now, context); |
| 731 | param->newone = 1; |
| 732 | /* Add address to name again */ |
| 733 | if (context->flags & CONTEXT_RA_NAME) |
| 734 | param->newname = 1; |
Simon Kelley | db0f488 | 2018-06-07 21:37:02 +0100 | [diff] [blame] | 735 | |
Simon Kelley | c488b68 | 2018-06-02 13:06:00 +0100 | [diff] [blame] | 736 | } |
Simon Kelley | 05ff659 | 2018-06-12 16:03:09 +0100 | [diff] [blame] | 737 | break; |
Simon Kelley | f7a40ec | 2013-07-27 13:36:08 +0100 | [diff] [blame] | 738 | } |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | if (!context && (context = whine_malloc(sizeof (struct dhcp_context)))) |
| 742 | { |
| 743 | *context = *template; |
| 744 | context->start6 = start6; |
| 745 | context->end6 = end6; |
| 746 | context->flags &= ~CONTEXT_TEMPLATE; |
| 747 | context->flags |= CONTEXT_CONSTRUCTED; |
| 748 | context->if_index = if_index; |
| 749 | context->local6 = *local; |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 750 | context->saved_valid = 0; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 751 | |
| 752 | context->next = daemon->dhcp6; |
| 753 | daemon->dhcp6 = context; |
| 754 | |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 755 | ra_start_unsolicited(param->now, context); |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 756 | /* we created a new one, need to call |
| 757 | lease_update_file to get periodic functions called */ |
| 758 | param->newone = 1; |
Simon Kelley | 0c05024 | 2012-12-22 22:13:19 +0000 | [diff] [blame] | 759 | |
| 760 | /* Will need to add new putative SLAAC addresses to existing leases */ |
| 761 | if (context->flags & CONTEXT_RA_NAME) |
| 762 | param->newname = 1; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 763 | |
| 764 | log_context(AF_INET6, context); |
| 765 | } |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | return 1; |
| 769 | } |
| 770 | |
| 771 | void dhcp_construct_contexts(time_t now) |
| 772 | { |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 773 | struct dhcp_context *context, *tmp, **up; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 774 | struct cparam param; |
| 775 | param.newone = 0; |
Simon Kelley | 0c05024 | 2012-12-22 22:13:19 +0000 | [diff] [blame] | 776 | param.newname = 0; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 777 | param.now = now; |
| 778 | |
| 779 | for (context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 780 | if (context->flags & CONTEXT_CONSTRUCTED) |
| 781 | context->flags |= CONTEXT_GC; |
| 782 | |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 783 | iface_enumerate(AF_INET6, ¶m, construct_worker); |
| 784 | |
| 785 | for (up = &daemon->dhcp6, context = daemon->dhcp6; context; context = tmp) |
| 786 | { |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 787 | |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 788 | tmp = context->next; |
| 789 | |
| 790 | if (context->flags & CONTEXT_GC && !(context->flags & CONTEXT_OLD)) |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 791 | { |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 792 | if ((context->flags & CONTEXT_RA) || option_bool(OPT_RA)) |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 793 | { |
| 794 | /* previously constructed context has gone. advertise it's demise */ |
| 795 | context->flags |= CONTEXT_OLD; |
| 796 | context->address_lost_time = now; |
Simon Kelley | 9f48ffa | 2013-07-28 15:47:04 +0100 | [diff] [blame] | 797 | /* Apply same ceiling of configured lease time as in radv.c */ |
| 798 | if (context->saved_valid > context->lease_time) |
| 799 | context->saved_valid = context->lease_time; |
| 800 | /* maximum time is 2 hours, from RFC */ |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 801 | if (context->saved_valid > 7200) /* 2 hours */ |
| 802 | context->saved_valid = 7200; |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 803 | ra_start_unsolicited(now, context); |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 804 | param.newone = 1; /* include deletion */ |
| 805 | |
| 806 | if (context->flags & CONTEXT_RA_NAME) |
| 807 | param.newname = 1; |
| 808 | |
| 809 | log_context(AF_INET6, context); |
| 810 | |
| 811 | up = &context->next; |
| 812 | } |
| 813 | else |
| 814 | { |
| 815 | /* we were never doing RA for this, so free now */ |
| 816 | *up = context->next; |
| 817 | free(context); |
| 818 | } |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 819 | } |
| 820 | else |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 821 | up = &context->next; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | if (param.newone) |
Simon Kelley | 7558ecd | 2012-12-16 21:45:16 +0000 | [diff] [blame] | 825 | { |
| 826 | if (daemon->dhcp || daemon->doing_dhcp6) |
Simon Kelley | 0c05024 | 2012-12-22 22:13:19 +0000 | [diff] [blame] | 827 | { |
| 828 | if (param.newname) |
| 829 | lease_update_slaac(now); |
| 830 | lease_update_file(now); |
| 831 | } |
Simon Kelley | 7558ecd | 2012-12-16 21:45:16 +0000 | [diff] [blame] | 832 | else |
| 833 | /* Not doing DHCP, so no lease system, manage alarms for ra only */ |
| 834 | send_alarm(periodic_ra(now), now); |
| 835 | } |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 836 | } |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 837 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 838 | #endif |
| 839 | |
| 840 | |