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