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