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