Simon Kelley | 5954608 | 2012-01-06 20:02:04 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2012 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 | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 24 | int ind; |
| 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 | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 28 | int scope, int if_index, int dad, void *vparam); |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 29 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 30 | 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] | 31 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 32 | void dhcp6_init(void) |
| 33 | { |
| 34 | int fd; |
| 35 | struct sockaddr_in6 saddr; |
Simon Kelley | 0e88d53 | 2012-03-28 22:22:05 +0100 | [diff] [blame] | 36 | #if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 37 | int class = IPTOS_CLASS_CS6; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 38 | #endif |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 39 | |
| 40 | if ((fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP)) == -1 || |
Simon Kelley | 0e88d53 | 2012-03-28 22:22:05 +0100 | [diff] [blame] | 41 | #if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 42 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &class, sizeof(class)) == -1 || |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 43 | #endif |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 44 | !fix_fd(fd) || |
| 45 | !set_ipv6pktinfo(fd)) |
| 46 | die (_("cannot create DHCPv6 socket: %s"), NULL, EC_BADNET); |
| 47 | |
| 48 | memset(&saddr, 0, sizeof(saddr)); |
| 49 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 50 | saddr.sin6_len = sizeof(struct sockaddr_in6); |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 51 | #endif |
| 52 | saddr.sin6_family = AF_INET6; |
| 53 | saddr.sin6_addr = in6addr_any; |
| 54 | saddr.sin6_port = htons(DHCPV6_SERVER_PORT); |
| 55 | |
| 56 | if (bind(fd, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in6))) |
| 57 | die(_("failed to bind DHCPv6 server socket: %s"), NULL, EC_BADNET); |
| 58 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 59 | daemon->dhcp6fd = fd; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 62 | void dhcp6_packet(time_t now) |
| 63 | { |
| 64 | struct dhcp_context *context; |
| 65 | struct iface_param parm; |
| 66 | struct cmsghdr *cmptr; |
| 67 | struct msghdr msg; |
| 68 | int if_index = 0; |
| 69 | union { |
| 70 | struct cmsghdr align; /* this ensures alignment */ |
| 71 | char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
| 72 | } control_u; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 73 | struct sockaddr_in6 from; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 74 | struct all_addr dest; |
| 75 | ssize_t sz; |
| 76 | struct ifreq ifr; |
| 77 | struct iname *tmp; |
Simon Kelley | 1d0f91c | 2012-03-12 11:56:22 +0000 | [diff] [blame] | 78 | unsigned short port; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 79 | |
| 80 | msg.msg_control = control_u.control6; |
| 81 | msg.msg_controllen = sizeof(control_u); |
| 82 | msg.msg_flags = 0; |
| 83 | msg.msg_name = &from; |
| 84 | msg.msg_namelen = sizeof(from); |
| 85 | msg.msg_iov = &daemon->dhcp_packet; |
| 86 | msg.msg_iovlen = 1; |
| 87 | |
Simon Kelley | 1d0f91c | 2012-03-12 11:56:22 +0000 | [diff] [blame] | 88 | if ((sz = recv_dhcp_packet(daemon->dhcp6fd, &msg)) == -1) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 89 | return; |
| 90 | |
| 91 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 92 | if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo) |
| 93 | { |
| 94 | union { |
| 95 | unsigned char *c; |
| 96 | struct in6_pktinfo *p; |
| 97 | } p; |
| 98 | p.c = CMSG_DATA(cmptr); |
| 99 | |
| 100 | if_index = p.p->ipi6_ifindex; |
| 101 | dest.addr.addr6 = p.p->ipi6_addr; |
| 102 | } |
| 103 | |
| 104 | if (!indextoname(daemon->dhcp6fd, if_index, ifr.ifr_name)) |
| 105 | return; |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 106 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 107 | if (!iface_check(AF_INET6, (struct all_addr *)&dest, ifr.ifr_name)) |
| 108 | return; |
| 109 | |
| 110 | for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
| 111 | if (tmp->name && (strcmp(tmp->name, ifr.ifr_name) == 0)) |
| 112 | return; |
| 113 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 114 | /* unlinked contexts are marked by context->current == context */ |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 115 | for (context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 116 | { |
| 117 | context->current = context; |
| 118 | memset(&context->local6, 0, IN6ADDRSZ); |
| 119 | } |
| 120 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 121 | parm.current = NULL; |
| 122 | parm.ind = if_index; |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 123 | memset(&parm.fallback, 0, IN6ADDRSZ); |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 124 | |
| 125 | if (!iface_enumerate(AF_INET6, &parm, complete_context6)) |
| 126 | return; |
| 127 | |
| 128 | lease_prune(NULL, now); /* lose any expired leases */ |
| 129 | |
Simon Kelley | 1d0f91c | 2012-03-12 11:56:22 +0000 | [diff] [blame] | 130 | port = dhcp6_reply(parm.current, if_index, ifr.ifr_name, &parm.fallback, |
| 131 | sz, IN6_IS_ADDR_MULTICAST(&from.sin6_addr), now); |
Simon Kelley | 6277978 | 2012-02-10 21:19:25 +0000 | [diff] [blame] | 132 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 133 | lease_update_file(now); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 134 | lease_update_dns(0); |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 135 | |
Simon Kelley | 1d0f91c | 2012-03-12 11:56:22 +0000 | [diff] [blame] | 136 | /* The port in the source address of the original request should |
| 137 | be correct, but at least once client sends from the server port, |
| 138 | so we explicitly send to the client port to a client, and the |
| 139 | server port to a relay. */ |
| 140 | if (port != 0) |
| 141 | { |
| 142 | from.sin6_port = htons(port); |
| 143 | while (sendto(daemon->dhcp6fd, daemon->outpacket.iov_base, save_counter(0), |
| 144 | 0, (struct sockaddr *)&from, sizeof(from)) == -1 && |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 145 | retry_send()); |
Simon Kelley | 1d0f91c | 2012-03-12 11:56:22 +0000 | [diff] [blame] | 146 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static int complete_context6(struct in6_addr *local, int prefix, |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 150 | int scope, int if_index, int dad, void *vparam) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 151 | { |
| 152 | struct dhcp_context *context; |
| 153 | struct iface_param *param = vparam; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 154 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 155 | (void)scope; /* warning */ |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 156 | (void)dad; |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 157 | |
| 158 | if (if_index == param->ind && |
| 159 | !IN6_IS_ADDR_LOOPBACK(local) && |
| 160 | !IN6_IS_ADDR_LINKLOCAL(local) && |
| 161 | !IN6_IS_ADDR_MULTICAST(local)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 162 | { |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 163 | /* Determine a globally address on the arrival interface, even |
| 164 | if we have no matching dhcp-context, because we're only |
| 165 | allocating on remote subnets via relays. This |
| 166 | is used as a default for the DNS server option. */ |
Simon Kelley | 270dc2e | 2012-02-19 20:53:20 +0000 | [diff] [blame] | 167 | param->fallback = *local; |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 168 | |
| 169 | for (context = daemon->dhcp6; context; context = context->next) |
| 170 | { |
| 171 | if (prefix == context->prefix && |
| 172 | is_same_net6(local, &context->start6, prefix) && |
| 173 | is_same_net6(local, &context->end6, prefix)) |
| 174 | { |
| 175 | /* link it onto the current chain if we've not seen it before */ |
| 176 | if (context->current == context) |
| 177 | { |
| 178 | context->current = param->current; |
| 179 | param->current = context; |
| 180 | context->local6 = *local; |
| 181 | } |
| 182 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | return 1; |
| 186 | } |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 187 | |
| 188 | struct dhcp_config *config_find_by_address6(struct dhcp_config *configs, struct in6_addr *net, int prefix, u64 addr) |
| 189 | { |
| 190 | struct dhcp_config *config; |
| 191 | |
| 192 | for (config = configs; config; config = config->next) |
| 193 | if ((config->flags & CONFIG_ADDR6) && |
| 194 | is_same_net6(&config->addr6, net, prefix) && |
| 195 | (prefix == 128 || addr6part(&config->addr6) == addr)) |
| 196 | return config; |
| 197 | |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | int address6_allocate(struct dhcp_context *context, unsigned char *clid, int clid_len, |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 202 | int serial, struct dhcp_netid *netids, struct in6_addr *ans) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 203 | { |
| 204 | /* Find a free address: exclude anything in use and anything allocated to |
| 205 | a particular hwaddr/clientid/hostname in our configuration. |
| 206 | Try to return from contexts which match netids first. |
| 207 | |
| 208 | Note that we assume the address prefix lengths are 64 or greater, so we can |
| 209 | get by with 64 bit arithmetic. |
| 210 | */ |
| 211 | |
| 212 | u64 start, addr; |
| 213 | struct dhcp_context *c, *d; |
| 214 | int i, pass; |
| 215 | u64 j; |
| 216 | |
| 217 | /* hash hwaddr: use the SDBM hashing algorithm. This works |
| 218 | for MAC addresses, let's see how it manages with client-ids! */ |
| 219 | for (j = 0, i = 0; i < clid_len; i++) |
| 220 | j += clid[i] + (j << 6) + (j << 16) - j; |
| 221 | |
| 222 | for (pass = 0; pass <= 1; pass++) |
| 223 | for (c = context; c; c = c->current) |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 224 | if (c->flags & (CONTEXT_DEPRECATE | CONTEXT_STATIC | CONTEXT_RA_STATELESS)) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 225 | continue; |
| 226 | else if (!match_netid(c->filter, netids, pass)) |
| 227 | continue; |
| 228 | else |
Simon Kelley | 0793380 | 2012-02-14 20:55:25 +0000 | [diff] [blame] | 229 | { |
| 230 | if (option_bool(OPT_CONSEC_ADDR)) |
| 231 | /* seed is largest extant lease addr in this context */ |
| 232 | start = lease_find_max_addr6(c) + serial; |
| 233 | else |
| 234 | start = addr6part(&c->start6) + ((j + c->addr_epoch + serial) % (1 + addr6part(&c->end6) - addr6part(&c->start6))); |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 235 | |
| 236 | /* iterate until we find a free address. */ |
| 237 | addr = start; |
| 238 | |
| 239 | do { |
| 240 | /* eliminate addresses in use by the server. */ |
| 241 | for (d = context; d; d = d->current) |
Simon Kelley | e44ddca | 2012-02-18 17:08:50 +0000 | [diff] [blame] | 242 | if (addr == addr6part(&d->local6)) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 243 | break; |
| 244 | |
| 245 | if (!d && |
| 246 | !lease6_find_by_addr(&c->start6, c->prefix, addr) && |
| 247 | !config_find_by_address6(daemon->dhcp_conf, &c->start6, c->prefix, addr)) |
| 248 | { |
| 249 | *ans = c->start6; |
| 250 | setaddr6part (ans, addr); |
| 251 | return 1; |
| 252 | } |
| 253 | |
| 254 | addr++; |
| 255 | |
| 256 | if (addr == addr6part(&c->end6) + 1) |
| 257 | addr = addr6part(&c->start6); |
| 258 | |
| 259 | } while (addr != start); |
| 260 | } |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | struct dhcp_context *address6_available(struct dhcp_context *context, |
| 266 | struct in6_addr *taddr, |
| 267 | struct dhcp_netid *netids) |
| 268 | { |
| 269 | u64 start, end, addr = addr6part(taddr); |
| 270 | struct dhcp_context *tmp; |
| 271 | |
| 272 | for (tmp = context; tmp; tmp = tmp->current) |
| 273 | { |
| 274 | start = addr6part(&tmp->start6); |
| 275 | end = addr6part(&tmp->end6); |
| 276 | |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 277 | if (!(tmp->flags & (CONTEXT_STATIC | CONTEXT_RA_STATELESS)) && |
| 278 | is_same_net6(&tmp->start6, taddr, tmp->prefix) && |
| 279 | is_same_net6(&tmp->end6, taddr, tmp->prefix) && |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 280 | addr >= start && |
| 281 | addr <= end && |
| 282 | match_netid(tmp->filter, netids, 1)) |
| 283 | return tmp; |
| 284 | } |
| 285 | |
| 286 | return NULL; |
| 287 | } |
| 288 | |
| 289 | struct dhcp_context *narrow_context6(struct dhcp_context *context, |
| 290 | struct in6_addr *taddr, |
| 291 | struct dhcp_netid *netids) |
| 292 | { |
| 293 | /* We start of with a set of possible contexts, all on the current physical interface. |
| 294 | These are chained on ->current. |
| 295 | Here we have an address, and return the actual context correponding to that |
| 296 | address. Note that none may fit, if the address came a dhcp-host and is outside |
| 297 | any dhcp-range. In that case we return a static range if possible, or failing that, |
| 298 | any context on the correct subnet. (If there's more than one, this is a dodgy |
| 299 | configuration: maybe there should be a warning.) */ |
| 300 | |
| 301 | struct dhcp_context *tmp; |
| 302 | |
| 303 | if (!(tmp = address6_available(context, taddr, netids))) |
| 304 | { |
| 305 | for (tmp = context; tmp; tmp = tmp->current) |
| 306 | if (match_netid(tmp->filter, netids, 1) && |
| 307 | is_same_net6(taddr, &tmp->start6, tmp->prefix) && |
| 308 | (tmp->flags & CONTEXT_STATIC)) |
| 309 | break; |
| 310 | |
| 311 | if (!tmp) |
| 312 | for (tmp = context; tmp; tmp = tmp->current) |
| 313 | if (match_netid(tmp->filter, netids, 1) && |
| 314 | is_same_net6(taddr, &tmp->start6, tmp->prefix) && |
| 315 | !(tmp->flags & CONTEXT_PROXY)) |
| 316 | break; |
| 317 | } |
| 318 | |
| 319 | /* Only one context allowed now */ |
| 320 | if (tmp) |
| 321 | tmp->current = NULL; |
| 322 | |
| 323 | return tmp; |
| 324 | } |
| 325 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 326 | static int is_addr_in_context6(struct dhcp_context *context, struct dhcp_config *config) |
| 327 | { |
| 328 | if (!context) /* called via find_config() from lease_update_from_configs() */ |
| 329 | return 1; |
| 330 | if (!(config->flags & CONFIG_ADDR6)) |
| 331 | return 1; |
| 332 | for (; context; context = context->current) |
| 333 | if (is_same_net6(&config->addr6, &context->start6, context->prefix)) |
| 334 | return 1; |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | struct dhcp_config *find_config6(struct dhcp_config *configs, |
| 341 | struct dhcp_context *context, |
| 342 | unsigned char *duid, int duid_len, |
| 343 | char *hostname) |
| 344 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 345 | struct dhcp_config *config; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 346 | |
Simon Kelley | 60ac5af | 2012-02-15 10:41:03 +0000 | [diff] [blame] | 347 | if (duid) |
| 348 | for (config = configs; config; config = config->next) |
| 349 | if (config->flags & CONFIG_CLID) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 350 | { |
Simon Kelley | 60ac5af | 2012-02-15 10:41:03 +0000 | [diff] [blame] | 351 | if (config->clid_len == duid_len && |
| 352 | memcmp(config->clid, duid, duid_len) == 0 && |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 353 | is_addr_in_context6(context, config)) |
| 354 | return config; |
Simon Kelley | 60ac5af | 2012-02-15 10:41:03 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 357 | if (hostname && context) |
| 358 | for (config = configs; config; config = config->next) |
| 359 | if ((config->flags & CONFIG_NAME) && |
| 360 | hostname_isequal(config->hostname, hostname) && |
| 361 | is_addr_in_context6(context, config)) |
| 362 | return config; |
| 363 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 364 | return NULL; |
| 365 | } |
| 366 | |
| 367 | void make_duid(time_t now) |
| 368 | { |
Simon Kelley | 8b37270 | 2012-03-09 17:45:10 +0000 | [diff] [blame] | 369 | if (daemon->duid_config) |
| 370 | { |
| 371 | unsigned char *p; |
| 372 | |
| 373 | daemon->duid = p = safe_malloc(daemon->duid_config_len + 6); |
| 374 | daemon->duid_len = daemon->duid_config_len + 6; |
| 375 | PUTSHORT(2, p); /* DUID_EN */ |
| 376 | PUTLONG(daemon->duid_enterprise, p); |
| 377 | memcpy(p, daemon->duid_config, daemon->duid_config_len); |
| 378 | } |
| 379 | else |
| 380 | { |
| 381 | /* rebase epoch to 1/1/2000 */ |
| 382 | time_t newnow = now - 946684800; |
| 383 | |
| 384 | iface_enumerate(AF_LOCAL, &newnow, make_duid1); |
| 385 | |
| 386 | if(!daemon->duid) |
| 387 | die("Cannot create DHCPv6 server DUID: %s", NULL, EC_MISC); |
| 388 | } |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 391 | 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] | 392 | { |
| 393 | /* create DUID as specified in RFC3315. We use the MAC of the |
Simon Kelley | 0f08983 | 2012-03-01 13:43:39 +0000 | [diff] [blame] | 394 | first interface we find that isn't loopback or P-to-P and |
| 395 | has address-type < 256. Address types above 256 are things like |
| 396 | tunnels which don't have usable MAC addresses. */ |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 397 | |
| 398 | unsigned char *p; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 399 | (void)index; |
| 400 | |
Simon Kelley | 0f08983 | 2012-03-01 13:43:39 +0000 | [diff] [blame] | 401 | if (type >= 256) |
| 402 | return 1; |
| 403 | |
Simon Kelley | e3e8634 | 2012-03-01 10:35:34 +0000 | [diff] [blame] | 404 | #ifdef HAVE_BROKEN_RTC |
| 405 | daemon->duid = p = safe_malloc(maclen + 4); |
| 406 | daemon->duid_len = maclen + 4; |
| 407 | PUTSHORT(3, p); /* DUID_LL */ |
| 408 | PUTSHORT(type, p); /* address type */ |
| 409 | #else |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 410 | daemon->duid = p = safe_malloc(maclen + 8); |
| 411 | daemon->duid_len = maclen + 8; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 412 | PUTSHORT(1, p); /* DUID_LLT */ |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 413 | PUTSHORT(type, p); /* address type */ |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 414 | PUTLONG(*((time_t *)parm), p); /* time */ |
| 415 | #endif |
| 416 | |
| 417 | memcpy(p, mac, maclen); |
| 418 | |
| 419 | return 0; |
| 420 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 421 | #endif |
| 422 | |
| 423 | |