1/* dnsmasq is Copyright (c) 2000-2015 Simon Kelley 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 18#include "dnsmasq.h" 19 20#ifdef HAVE_DHCP6 21 22struct state { 23 unsigned char *clid; 24 int clid_len, iaid, ia_type, interface, hostname_auth, lease_allocate; 25 char *client_hostname, *hostname, *domain, *send_domain; 26 struct dhcp_context *context; 27 struct in6_addr *link_address, *fallback, *ll_addr, *ula_addr; 28 unsigned int xid, fqdn_flags; 29 char *iface_name; 30 void *packet_options, *end; 31 struct dhcp_netid *tags, *context_tags; 32 unsigned char mac[DHCP_CHADDR_MAX]; 33 unsigned int mac_len, mac_type; 34#ifdef OPTION6_PREFIX_CLASS 35 struct prefix_class *send_prefix_class; 36#endif 37}; 38 39static int dhcp6_maybe_relay(struct state *state, void *inbuff, size_t sz, 40 struct in6_addr *client_addr, int is_unicast, time_t now); 41static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_t sz, int is_unicast, time_t now); 42static void log6_opts(int nest, unsigned int xid, void *start_opts, void *end_opts); 43static void log6_packet(struct state *state, char *type, struct in6_addr *addr, char *string); 44static void log6_quiet(struct state *state, char *type, struct in6_addr *addr, char *string); 45static void *opt6_find (void *opts, void *end, unsigned int search, unsigned int minsize); 46static void *opt6_next(void *opts, void *end); 47static unsigned int opt6_uint(unsigned char *opt, int offset, int size); 48static void get_context_tag(struct state *state, struct dhcp_context *context); 49static int check_ia(struct state *state, void *opt, void **endp, void **ia_option); 50static int build_ia(struct state *state, int *t1cntr); 51static void end_ia(int t1cntr, unsigned int min_time, int do_fuzz); 52#ifdef OPTION6_PREFIX_CLASS 53static struct prefix_class *prefix_class_from_context(struct dhcp_context *context); 54#endif 55static void mark_context_used(struct state *state, struct in6_addr *addr); 56static void mark_config_used(struct dhcp_context *context, struct in6_addr *addr); 57static int check_address(struct state *state, struct in6_addr *addr); 58static void add_address(struct state *state, struct dhcp_context *context, unsigned int lease_time, void *ia_option, 59 unsigned int *min_time, struct in6_addr *addr, time_t now); 60static void update_leases(struct state *state, struct dhcp_context *context, struct in6_addr *addr, unsigned int lease_time, time_t now); 61static int add_local_addrs(struct dhcp_context *context); 62static struct dhcp_netid *add_options(struct state *state, int do_refresh); 63static void calculate_times(struct dhcp_context *context, unsigned int *min_time, unsigned int *valid_timep, 64 unsigned int *preferred_timep, unsigned int lease_time); 65 66#define opt6_len(opt) ((int)(opt6_uint(opt, -2, 2))) 67#define opt6_type(opt) (opt6_uint(opt, -4, 2)) 68#define opt6_ptr(opt, i) ((void *)&(((unsigned char *)(opt))[4+(i)])) 69 70#define opt6_user_vendor_ptr(opt, i) ((void *)&(((unsigned char *)(opt))[2+(i)])) 71#define opt6_user_vendor_len(opt) ((int)(opt6_uint(opt, -4, 2))) 72#define opt6_user_vendor_next(opt, end) (opt6_next(((void *) opt) - 2, end)) 73 74 75unsigned short dhcp6_reply(struct dhcp_context *context, int interface, char *iface_name, 76 struct in6_addr *fallback, struct in6_addr *ll_addr, struct in6_addr *ula_addr, 77 size_t sz, struct in6_addr *client_addr, time_t now) 78{ 79 struct dhcp_vendor *vendor; 80 int msg_type; 81 struct state state; 82 83 if (sz <= 4) 84 return 0; 85 86 msg_type = *((unsigned char *)daemon->dhcp_packet.iov_base); 87 88 /* Mark these so we only match each at most once, to avoid tangled linked lists */ 89 for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next) 90 vendor->netid.next = &vendor->netid; 91 92 save_counter(0); 93 state.context = context; 94 state.interface = interface; 95 state.iface_name = iface_name; 96 state.fallback = fallback; 97 state.ll_addr = ll_addr; 98 state.ula_addr = ula_addr; 99 state.mac_len = 0; 100 state.tags = NULL; 101 state.link_address = NULL; 102 103 if (dhcp6_maybe_relay(&state, daemon->dhcp_packet.iov_base, sz, client_addr, 104 IN6_IS_ADDR_MULTICAST(client_addr), now)) 105 return msg_type == DHCP6RELAYFORW ? DHCPV6_SERVER_PORT : DHCPV6_CLIENT_PORT; 106 107 return 0; 108} 109 110/* This cost me blood to write, it will probably cost you blood to understand - srk. */ 111static int dhcp6_maybe_relay(struct state *state, void *inbuff, size_t sz, 112 struct in6_addr *client_addr, int is_unicast, time_t now) 113{ 114 void *end = inbuff + sz; 115 void *opts = inbuff + 34; 116 int msg_type = *((unsigned char *)inbuff); 117 unsigned char *outmsgtypep; 118 void *opt; 119 struct dhcp_vendor *vendor; 120 121 /* if not an encaplsulated relayed message, just do the stuff */ 122 if (msg_type != DHCP6RELAYFORW) 123 { 124 /* if link_address != NULL if points to the link address field of the 125 innermost nested RELAYFORW message, which is where we find the 126 address of the network on which we can allocate an address. 127 Recalculate the available contexts using that information. 128 129 link_address == NULL means there's no relay in use, so we try and find the client's 130 MAC address from the local ND cache. */ 131 132 if (!state->link_address) 133 get_client_mac(client_addr, state->interface, state->mac, &state->mac_len, &state->mac_type); 134 else 135 { 136 struct dhcp_context *c; 137 state->context = NULL; 138 139 if (!IN6_IS_ADDR_LOOPBACK(state->link_address) && 140 !IN6_IS_ADDR_LINKLOCAL(state->link_address) && 141 !IN6_IS_ADDR_MULTICAST(state->link_address)) 142 for (c = daemon->dhcp6; c; c = c->next) 143 if ((c->flags & CONTEXT_DHCP) && 144 !(c->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) && 145 is_same_net6(state->link_address, &c->start6, c->prefix) && 146 is_same_net6(state->link_address, &c->end6, c->prefix)) 147 { 148 c->preferred = c->valid = 0xffffffff; 149 c->current = state->context; 150 state->context = c; 151 } 152 153 if (!state->context) 154 { 155 inet_ntop(AF_INET6, state->link_address, daemon->addrbuff, ADDRSTRLEN); 156 if (option_bool(OPT_LOG_OPTS)) 157 my_syslog(MS_DHCP | LOG_WARNING, 158 _("no address range available for DHCPv6 request from relay at %s"), 159 daemon->addrbuff); 160 return 0; 161 } 162 } 163 164 if (!state->context) 165 { 166 if (option_bool(OPT_LOG_OPTS)) 167 my_syslog(MS_DHCP | LOG_WARNING, 168 _("no address range available for DHCPv6 request via %s"), state->iface_name); 169 return 0; 170 } 171 172 return dhcp6_no_relay(state, msg_type, inbuff, sz, is_unicast, now); 173 } 174 175 /* must have at least msg_type+hopcount+link_address+peer_address+minimal size option 176 which is 1 + 1 + 16 + 16 + 2 + 2 = 38 */ 177 if (sz < 38) 178 return 0; 179 180 /* copy header stuff into reply message and set type to reply */ 181 if (!(outmsgtypep = put_opt6(inbuff, 34))) 182 return 0; 183 *outmsgtypep = DHCP6RELAYREPL; 184 185 /* look for relay options and set tags if found. */ 186 for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next) 187 { 188 int mopt; 189 190 if (vendor->match_type == MATCH_SUBSCRIBER) 191 mopt = OPTION6_SUBSCRIBER_ID; 192 else if (vendor->match_type == MATCH_REMOTE) 193 mopt = OPTION6_REMOTE_ID; 194 else 195 continue; 196 197 if ((opt = opt6_find(opts, end, mopt, 1)) && 198 vendor->len == opt6_len(opt) && 199 memcmp(vendor->data, opt6_ptr(opt, 0), vendor->len) == 0 && 200 vendor->netid.next != &vendor->netid) 201 { 202 vendor->netid.next = state->tags; 203 state->tags = &vendor->netid; 204 break; 205 } 206 } 207 208 /* RFC-6939 */ 209 if ((opt = opt6_find(opts, end, OPTION6_CLIENT_MAC, 3))) 210 { 211 state->mac_type = opt6_uint(opt, 0, 2); 212 state->mac_len = opt6_len(opt) - 2; 213 memcpy(&state->mac[0], opt6_ptr(opt, 2), state->mac_len); 214 } 215 216 for (opt = opts; opt; opt = opt6_next(opt, end)) 217 { 218 int o = new_opt6(opt6_type(opt)); 219 if (opt6_type(opt) == OPTION6_RELAY_MSG) 220 { 221 struct in6_addr align; 222 /* the packet data is unaligned, copy to aligned storage */ 223 memcpy(&align, inbuff + 2, IN6ADDRSZ); 224 state->link_address = &align; 225 /* zero is_unicast since that is now known to refer to the 226 relayed packet, not the original sent by the client */ 227 if (!dhcp6_maybe_relay(state, opt6_ptr(opt, 0), opt6_len(opt), client_addr, 0, now)) 228 return 0; 229 } 230 else if (opt6_type(opt) != OPTION6_CLIENT_MAC) 231 put_opt6(opt6_ptr(opt, 0), opt6_len(opt)); 232 end_opt6(o); 233 } 234 235 return 1; 236} 237 238static int dhcp6_no_relay(struct state *state, int msg_type, void *inbuff, size_t sz, int is_unicast, time_t now) 239{ 240 void *opt; 241 int i, o, o1, start_opts; 242 struct dhcp_opt *opt_cfg; 243 struct dhcp_netid *tagif; 244 struct dhcp_config *config = NULL; 245 struct dhcp_netid known_id, iface_id, v6_id; 246 unsigned char *outmsgtypep; 247 struct dhcp_vendor *vendor; 248 struct dhcp_context *context_tmp; 249 struct dhcp_mac *mac_opt; 250 unsigned int ignore = 0; 251#ifdef OPTION6_PREFIX_CLASS 252 struct prefix_class *p; 253 int dump_all_prefix_classes = 0; 254#endif 255 256 state->packet_options = inbuff + 4; 257 state->end = inbuff + sz; 258 state->clid = NULL; 259 state->clid_len = 0; 260 state->lease_allocate = 0; 261 state->context_tags = NULL; 262 state->domain = NULL; 263 state->send_domain = NULL; 264 state->hostname_auth = 0; 265 state->hostname = NULL; 266 state->client_hostname = NULL; 267 state->fqdn_flags = 0x01; /* default to send if we recieve no FQDN option */ 268#ifdef OPTION6_PREFIX_CLASS 269 state->send_prefix_class = NULL; 270#endif 271 272 /* set tag with name == interface */ 273 iface_id.net = state->iface_name; 274 iface_id.next = state->tags; 275 state->tags = &iface_id; 276 277 /* set tag "dhcpv6" */ 278 v6_id.net = "dhcpv6"; 279 v6_id.next = state->tags; 280 state->tags = &v6_id; 281 282 /* copy over transaction-id, and save pointer to message type */ 283 if (!(outmsgtypep = put_opt6(inbuff, 4))) 284 return 0; 285 start_opts = save_counter(-1); 286 state->xid = outmsgtypep[3] | outmsgtypep[2] << 8 | outmsgtypep[1] << 16; 287 288 /* We're going to be linking tags from all context we use. 289 mark them as unused so we don't link one twice and break the list */ 290 for (context_tmp = state->context; context_tmp; context_tmp = context_tmp->current) 291 { 292 context_tmp->netid.next = &context_tmp->netid; 293 294 if (option_bool(OPT_LOG_OPTS)) 295 { 296 inet_ntop(AF_INET6, &context_tmp->start6, daemon->dhcp_buff, ADDRSTRLEN); 297 inet_ntop(AF_INET6, &context_tmp->end6, daemon->dhcp_buff2, ADDRSTRLEN); 298 if (context_tmp->flags & (CONTEXT_STATIC)) 299 my_syslog(MS_DHCP | LOG_INFO, _("%u available DHCPv6 subnet: %s/%d"), 300 state->xid, daemon->dhcp_buff, context_tmp->prefix); 301 else 302 my_syslog(MS_DHCP | LOG_INFO, _("%u available DHCP range: %s -- %s"), 303 state->xid, daemon->dhcp_buff, daemon->dhcp_buff2); 304 } 305 } 306 307 if ((opt = opt6_find(state->packet_options, state->end, OPTION6_CLIENT_ID, 1))) 308 { 309 state->clid = opt6_ptr(opt, 0); 310 state->clid_len = opt6_len(opt); 311 o = new_opt6(OPTION6_CLIENT_ID); 312 put_opt6(state->clid, state->clid_len); 313 end_opt6(o); 314 } 315 else if (msg_type != DHCP6IREQ) 316 return 0; 317 318 /* server-id must match except for SOLICIT, CONFIRM and REBIND messages */ 319 if (msg_type != DHCP6SOLICIT && msg_type != DHCP6CONFIRM && msg_type != DHCP6IREQ && msg_type != DHCP6REBIND && 320 (!(opt = opt6_find(state->packet_options, state->end, OPTION6_SERVER_ID, 1)) || 321 opt6_len(opt) != daemon->duid_len || 322 memcmp(opt6_ptr(opt, 0), daemon->duid, daemon->duid_len) != 0)) 323 return 0; 324 325 o = new_opt6(OPTION6_SERVER_ID); 326 put_opt6(daemon->duid, daemon->duid_len); 327 end_opt6(o); 328 329 if (is_unicast && 330 (msg_type == DHCP6REQUEST || msg_type == DHCP6RENEW || msg_type == DHCP6RELEASE || msg_type == DHCP6DECLINE)) 331 332 { 333 *outmsgtypep = DHCP6REPLY; 334 o1 = new_opt6(OPTION6_STATUS_CODE); 335 put_opt6_short(DHCP6USEMULTI); 336 put_opt6_string("Use multicast"); 337 end_opt6(o1); 338 return 1; 339 } 340 341 /* match vendor and user class options */ 342 for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next) 343 { 344 int mopt; 345 346 if (vendor->match_type == MATCH_VENDOR) 347 mopt = OPTION6_VENDOR_CLASS; 348 else if (vendor->match_type == MATCH_USER) 349 mopt = OPTION6_USER_CLASS; 350 else 351 continue; 352 353 if ((opt = opt6_find(state->packet_options, state->end, mopt, 2))) 354 { 355 void *enc_opt, *enc_end = opt6_ptr(opt, opt6_len(opt)); 356 int offset = 0; 357 358 if (mopt == OPTION6_VENDOR_CLASS) 359 { 360 if (opt6_len(opt) < 4) 361 continue; 362 363 if (vendor->enterprise != opt6_uint(opt, 0, 4)) 364 continue; 365 366 offset = 4; 367 } 368 369 /* Note that format if user/vendor classes is different to DHCP options - no option types. */ 370 for (enc_opt = opt6_ptr(opt, offset); enc_opt; enc_opt = opt6_user_vendor_next(enc_opt, enc_end)) 371 for (i = 0; i <= (opt6_user_vendor_len(enc_opt) - vendor->len); i++) 372 if (memcmp(vendor->data, opt6_user_vendor_ptr(enc_opt, i), vendor->len) == 0) 373 { 374 vendor->netid.next = state->tags; 375 state->tags = &vendor->netid; 376 break; 377 } 378 } 379 } 380 381 if (option_bool(OPT_LOG_OPTS) && (opt = opt6_find(state->packet_options, state->end, OPTION6_VENDOR_CLASS, 4))) 382 my_syslog(MS_DHCP | LOG_INFO, _("%u vendor class: %u"), state->xid, opt6_uint(opt, 0, 4)); 383 384 /* dhcp-match. If we have hex-and-wildcards, look for a left-anchored match. 385 Otherwise assume the option is an array, and look for a matching element. 386 If no data given, existance of the option is enough. This code handles 387 V-I opts too. */ 388 for (opt_cfg = daemon->dhcp_match6; opt_cfg; opt_cfg = opt_cfg->next) 389 { 390 int match = 0; 391 392 if (opt_cfg->flags & DHOPT_RFC3925) 393 { 394 for (opt = opt6_find(state->packet_options, state->end, OPTION6_VENDOR_OPTS, 4); 395 opt; 396 opt = opt6_find(opt6_next(opt, state->end), state->end, OPTION6_VENDOR_OPTS, 4)) 397 { 398 void *vopt; 399 void *vend = opt6_ptr(opt, opt6_len(opt)); 400 401 for (vopt = opt6_find(opt6_ptr(opt, 4), vend, opt_cfg->opt, 0); 402 vopt; 403 vopt = opt6_find(opt6_next(vopt, vend), vend, opt_cfg->opt, 0)) 404 if ((match = match_bytes(opt_cfg, opt6_ptr(vopt, 0), opt6_len(vopt)))) 405 break; 406 } 407 if (match) 408 break; 409 } 410 else 411 { 412 if (!(opt = opt6_find(state->packet_options, state->end, opt_cfg->opt, 1))) 413 continue; 414 415 match = match_bytes(opt_cfg, opt6_ptr(opt, 0), opt6_len(opt)); 416 } 417 418 if (match) 419 { 420 opt_cfg->netid->next = state->tags; 421 state->tags = opt_cfg->netid; 422 } 423 } 424 425 if (state->mac_len != 0) 426 { 427 if (option_bool(OPT_LOG_OPTS)) 428 { 429 print_mac(daemon->dhcp_buff, state->mac, state->mac_len); 430 my_syslog(MS_DHCP | LOG_INFO, _("%u client MAC address: %s"), state->xid, daemon->dhcp_buff); 431 } 432 433 for (mac_opt = daemon->dhcp_macs; mac_opt; mac_opt = mac_opt->next) 434 if ((unsigned)mac_opt->hwaddr_len == state->mac_len && 435 ((unsigned)mac_opt->hwaddr_type == state->mac_type || mac_opt->hwaddr_type == 0) && 436 memcmp_masked(mac_opt->hwaddr, state->mac, state->mac_len, mac_opt->mask)) 437 { 438 mac_opt->netid.next = state->tags; 439 state->tags = &mac_opt->netid; 440 } 441 } 442 443 if ((opt = opt6_find(state->packet_options, state->end, OPTION6_FQDN, 1))) 444 { 445 /* RFC4704 refers */ 446 int len = opt6_len(opt) - 1; 447 448 state->fqdn_flags = opt6_uint(opt, 0, 1); 449 450 /* Always force update, since the client has no way to do it itself. */ 451 if (!option_bool(OPT_FQDN_UPDATE) && !(state->fqdn_flags & 0x01)) 452 state->fqdn_flags |= 0x03; 453 454 state->fqdn_flags &= ~0x04; 455 456 if (len != 0 && len < 255) 457 { 458 unsigned char *pp, *op = opt6_ptr(opt, 1); 459 char *pq = daemon->dhcp_buff; 460 461 pp = op; 462 while (*op != 0 && ((op + (*op)) - pp) < len) 463 { 464 memcpy(pq, op+1, *op); 465 pq += *op; 466 op += (*op)+1; 467 *(pq++) = '.'; 468 } 469 470 if (pq != daemon->dhcp_buff) 471 pq--; 472 *pq = 0; 473 474 if (legal_hostname(daemon->dhcp_buff)) 475 { 476 state->client_hostname = daemon->dhcp_buff; 477 if (option_bool(OPT_LOG_OPTS)) 478 my_syslog(MS_DHCP | LOG_INFO, _("%u client provides name: %s"), state->xid, state->client_hostname); 479 } 480 } 481 } 482 483 if (state->clid) 484 { 485 config = find_config(daemon->dhcp_conf, state->context, state->clid, state->clid_len, state->mac, state->mac_len, state->mac_type, NULL); 486 487 if (have_config(config, CONFIG_NAME)) 488 { 489 state->hostname = config->hostname; 490 state->domain = config->domain; 491 state->hostname_auth = 1; 492 } 493 else if (state->client_hostname) 494 { 495 state->domain = strip_hostname(state->client_hostname); 496 497 if (strlen(state->client_hostname) != 0) 498 { 499 state->hostname = state->client_hostname; 500 if (!config) 501 { 502 /* Search again now we have a hostname. 503 Only accept configs without CLID here, (it won't match) 504 to avoid impersonation by name. */ 505 struct dhcp_config *new = find_config(daemon->dhcp_conf, state->context, NULL, 0, NULL, 0, 0, state->hostname); 506 if (new && !have_config(new, CONFIG_CLID) && !new->hwaddr) 507 config = new; 508 } 509 } 510 } 511 } 512 513 if (config) 514 { 515 struct dhcp_netid_list *list; 516 517 for (list = config->netid; list; list = list->next) 518 { 519 list->list->next = state->tags; 520 state->tags = list->list; 521 } 522 523 /* set "known" tag for known hosts */ 524 known_id.net = "known"; 525 known_id.next = state->tags; 526 state->tags = &known_id; 527 528 if (have_config(config, CONFIG_DISABLE)) 529 ignore = 1; 530 } 531 532#ifdef OPTION6_PREFIX_CLASS 533 /* OPTION_PREFIX_CLASS in ORO, send addresses in all prefix classes */ 534 if (daemon->prefix_classes && (msg_type == DHCP6SOLICIT || msg_type == DHCP6REQUEST)) 535 { 536 void *oro; 537 538 if ((oro = opt6_find(state->packet_options, state->end, OPTION6_ORO, 0))) 539 for (i = 0; i < opt6_len(oro) - 1; i += 2) 540 if (opt6_uint(oro, i, 2) == OPTION6_PREFIX_CLASS) 541 { 542 dump_all_prefix_classes = 1; 543 break; 544 } 545 546 if (msg_type != DHCP6SOLICIT || dump_all_prefix_classes) 547 /* Add the tags associated with prefix classes so we can use the DHCP ranges. 548 Not done for SOLICIT as we add them one-at-time. */ 549 for (p = daemon->prefix_classes; p ; p = p->next) 550 { 551 p->tag.next = state->tags; 552 state->tags = &p->tag; 553 } 554 } 555#endif 556 557 tagif = run_tag_if(state->tags); 558 559 /* if all the netids in the ignore list are present, ignore this client */ 560 if (daemon->dhcp_ignore) 561 { 562 struct dhcp_netid_list *id_list; 563 564 for (id_list = daemon->dhcp_ignore; id_list; id_list = id_list->next) 565 if (match_netid(id_list->list, tagif, 0)) 566 ignore = 1; 567 } 568 569 /* if all the netids in the ignore_name list are present, ignore client-supplied name */ 570 if (!state->hostname_auth) 571 { 572 struct dhcp_netid_list *id_list; 573 574 for (id_list = daemon->dhcp_ignore_names; id_list; id_list = id_list->next) 575 if ((!id_list->list) || match_netid(id_list->list, tagif, 0)) 576 break; 577 if (id_list) 578 state->hostname = NULL; 579 } 580 581 582 switch (msg_type) 583 { 584 default: 585 return 0; 586 587 588 case DHCP6SOLICIT: 589 { 590 int address_assigned = 0; 591 /* tags without all prefix-class tags */ 592 struct dhcp_netid *solicit_tags; 593 struct dhcp_context *c; 594 595 *outmsgtypep = DHCP6ADVERTISE; 596 597 if (opt6_find(state->packet_options, state->end, OPTION6_RAPID_COMMIT, 0)) 598 { 599 *outmsgtypep = DHCP6REPLY; 600 state->lease_allocate = 1; 601 o = new_opt6(OPTION6_RAPID_COMMIT); 602 end_opt6(o); 603 } 604 605 log6_quiet(state, "DHCPSOLICIT", NULL, ignore ? _("ignored") : NULL); 606 607 request_no_address: 608 solicit_tags = tagif; 609 610 if (ignore) 611 return 0; 612 613 /* reset USED bits in leases */ 614 lease6_reset(); 615 616 /* Can use configured address max once per prefix */ 617 for (c = state->context; c; c = c->current) 618 c->flags &= ~CONTEXT_CONF_USED; 619 620 for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end)) 621 { 622 void *ia_option, *ia_end; 623 unsigned int min_time = 0xffffffff; 624 int t1cntr; 625 int ia_counter; 626 /* set unless we're sending a particular prefix-class, when we 627 want only dhcp-ranges with the correct tags set and not those without any tags. */ 628 int plain_range = 1; 629 u32 lease_time; 630 struct dhcp_lease *ltmp; 631 struct in6_addr *req_addr; 632 struct in6_addr addr; 633 634 if (!check_ia(state, opt, &ia_end, &ia_option)) 635 continue; 636 637 /* reset USED bits in contexts - one address per prefix per IAID */ 638 for (c = state->context; c; c = c->current) 639 c->flags &= ~CONTEXT_USED; 640 641#ifdef OPTION6_PREFIX_CLASS 642 if (daemon->prefix_classes && state->ia_type == OPTION6_IA_NA) 643 { 644 void *prefix_opt; 645 int prefix_class; 646 647 if (dump_all_prefix_classes) 648 /* OPTION_PREFIX_CLASS in ORO, send addresses in all prefix classes */ 649 plain_range = 0; 650 else 651 { 652 if ((prefix_opt = opt6_find(opt6_ptr(opt, 12), ia_end, OPTION6_PREFIX_CLASS, 2))) 653 { 654 655 prefix_class = opt6_uint(prefix_opt, 0, 2); 656 657 for (p = daemon->prefix_classes; p ; p = p->next) 658 if (p->class == prefix_class) 659 break; 660 661 if (!p) 662 my_syslog(MS_DHCP | LOG_WARNING, _("unknown prefix-class %d"), prefix_class); 663 else 664 { 665 /* add tag to list, and exclude undecorated dhcp-ranges */ 666 p->tag.next = state->tags; 667 solicit_tags = run_tag_if(&p->tag); 668 plain_range = 0; 669 state->send_prefix_class = p; 670 } 671 } 672 else 673 { 674 /* client didn't ask for a prefix class, lets see if we can find one. */ 675 for (p = daemon->prefix_classes; p ; p = p->next) 676 { 677 p->tag.next = NULL; 678 if (match_netid(&p->tag, solicit_tags, 1)) 679 break; 680 } 681 682 if (p) 683 { 684 plain_range = 0; 685 state->send_prefix_class = p; 686 } 687 } 688 689 if (p && option_bool(OPT_LOG_OPTS)) 690 my_syslog(MS_DHCP | LOG_INFO, "%u prefix class %d tag:%s", state->xid, p->class, p->tag.net); 691 } 692 } 693#endif 694 695 o = build_ia(state, &t1cntr); 696 if (address_assigned) 697 address_assigned = 2; 698 699 for (ia_counter = 0; ia_option; ia_counter++, ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24)) 700 { 701 req_addr = opt6_ptr(ia_option, 0); 702 703 if ((c = address6_valid(state->context, req_addr, solicit_tags, plain_range))) 704 { 705 lease_time = c->lease_time; 706 /* If the client asks for an address on the same network as a configured address, 707 offer the configured address instead, to make moving to newly-configured 708 addresses automatic. */ 709 if (!(c->flags & CONTEXT_CONF_USED) && config_valid(config, c, &addr) && check_address(state, &addr)) 710 { 711 req_addr = &addr; 712 mark_config_used(c, &addr); 713 if (have_config(config, CONFIG_TIME)) 714 lease_time = config->lease_time; 715 } 716 else if (!(c = address6_available(state->context, req_addr, solicit_tags, plain_range))) 717 continue; /* not an address we're allowed */ 718 else if (!check_address(state, req_addr)) 719 continue; /* address leased elsewhere */ 720 721 /* add address to output packet */ 722#ifdef OPTION6_PREFIX_CLASS 723 if (dump_all_prefix_classes && state->ia_type == OPTION6_IA_NA) 724 state->send_prefix_class = prefix_class_from_context(c); 725#endif 726 add_address(state, c, lease_time, ia_option, &min_time, req_addr, now); 727 mark_context_used(state, req_addr); 728 get_context_tag(state, c); 729 address_assigned = 1; 730 } 731 } 732 733 /* Suggest configured address(es) */ 734 for (c = state->context; c; c = c->current) 735 if (!(c->flags & CONTEXT_CONF_USED) && 736 match_netid(c->filter, solicit_tags, plain_range) && 737 config_valid(config, c, &addr) && 738 check_address(state, &addr)) 739 { 740 mark_config_used(state->context, &addr); 741 if (have_config(config, CONFIG_TIME)) 742 lease_time = config->lease_time; 743 else 744 lease_time = c->lease_time; 745 /* add address to output packet */ 746#ifdef OPTION6_PREFIX_CLASS 747 if (dump_all_prefix_classes && state->ia_type == OPTION6_IA_NA) 748 state->send_prefix_class = prefix_class_from_context(c); 749#endif 750 add_address(state, c, lease_time, NULL, &min_time, &addr, now); 751 mark_context_used(state, &addr); 752 get_context_tag(state, c); 753 address_assigned = 1; 754 } 755 756 /* return addresses for existing leases */ 757 ltmp = NULL; 758 while ((ltmp = lease6_find_by_client(ltmp, state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA, state->clid, state->clid_len, state->iaid))) 759 { 760 req_addr = <mp->addr6; 761 if ((c = address6_available(state->context, req_addr, solicit_tags, plain_range))) 762 { 763#ifdef OPTION6_PREFIX_CLASS 764 if (dump_all_prefix_classes && state->ia_type == OPTION6_IA_NA) 765 state->send_prefix_class = prefix_class_from_context(c); 766#endif 767 add_address(state, c, c->lease_time, NULL, &min_time, req_addr, now); 768 mark_context_used(state, req_addr); 769 get_context_tag(state, c); 770 address_assigned = 1; 771 } 772 } 773 774 /* Return addresses for all valid contexts which don't yet have one */ 775 while ((c = address6_allocate(state->context, state->clid, state->clid_len, state->ia_type == OPTION6_IA_TA, 776 state->iaid, ia_counter, solicit_tags, plain_range, &addr))) 777 { 778#ifdef OPTION6_PREFIX_CLASS 779 if (dump_all_prefix_classes && state->ia_type == OPTION6_IA_NA) 780 state->send_prefix_class = prefix_class_from_context(c); 781#endif 782 add_address(state, c, c->lease_time, NULL, &min_time, &addr, now); 783 mark_context_used(state, &addr); 784 get_context_tag(state, c); 785 address_assigned = 1; 786 } 787 788 if (address_assigned != 1) 789 { 790 /* If the server will not assign any addresses to any IAs in a 791 subsequent Request from the client, the server MUST send an Advertise 792 message to the client that doesn't include any IA options. */ 793 if (!state->lease_allocate) 794 { 795 save_counter(o); 796 continue; 797 } 798 799 /* If the server cannot assign any addresses to an IA in the message 800 from the client, the server MUST include the IA in the Reply message 801 with no addresses in the IA and a Status Code option in the IA 802 containing status code NoAddrsAvail. */ 803 o1 = new_opt6(OPTION6_STATUS_CODE); 804 put_opt6_short(DHCP6NOADDRS); 805 put_opt6_string(_("address unavailable")); 806 end_opt6(o1); 807 } 808 809 end_ia(t1cntr, min_time, 0); 810 end_opt6(o); 811 } 812 813 if (address_assigned) 814 { 815 o1 = new_opt6(OPTION6_STATUS_CODE); 816 put_opt6_short(DHCP6SUCCESS); 817 put_opt6_string(_("success")); 818 end_opt6(o1); 819 820 /* If --dhcp-authoritative is set, we can tell client not to wait for 821 other possible servers */ 822 o = new_opt6(OPTION6_PREFERENCE); 823 put_opt6_char(option_bool(OPT_AUTHORITATIVE) ? 255 : 0); 824 end_opt6(o); 825 tagif = add_options(state, 0); 826 } 827 else 828 { 829 /* no address, return error */ 830 o1 = new_opt6(OPTION6_STATUS_CODE); 831 put_opt6_short(DHCP6NOADDRS); 832 put_opt6_string(_("no addresses available")); 833 end_opt6(o1); 834 835 /* Some clients will ask repeatedly when we're not giving 836 out addresses because we're in stateless mode. Avoid spamming 837 the log in that case. */ 838 for (c = state->context; c; c = c->current) 839 if (!(c->flags & CONTEXT_RA_STATELESS)) 840 { 841 log6_packet(state, state->lease_allocate ? "DHCPREPLY" : "DHCPADVERTISE", NULL, _("no addresses available")); 842 break; 843 } 844 } 845 846 break; 847 } 848 849 case DHCP6REQUEST: 850 { 851 int address_assigned = 0; 852 int start = save_counter(-1); 853 854 /* set reply message type */ 855 *outmsgtypep = DHCP6REPLY; 856 state->lease_allocate = 1; 857 858 log6_quiet(state, "DHCPREQUEST", NULL, ignore ? _("ignored") : NULL); 859 860 if (ignore) 861 return 0; 862 863 for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end)) 864 { 865 void *ia_option, *ia_end; 866 unsigned int min_time = 0xffffffff; 867 int t1cntr; 868 869 if (!check_ia(state, opt, &ia_end, &ia_option)) 870 continue; 871 872 if (!ia_option) 873 { 874 /* If we get a request with a IA_*A without addresses, treat it exactly like 875 a SOLICT with rapid commit set. */ 876 save_counter(start); 877 goto request_no_address; 878 } 879 880 o = build_ia(state, &t1cntr); 881 882 for (; ia_option; ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24)) 883 { 884 struct in6_addr *req_addr = opt6_ptr(ia_option, 0); 885 struct dhcp_context *dynamic, *c; 886 unsigned int lease_time; 887 struct in6_addr addr; 888 int config_ok = 0; 889 890 if ((c = address6_valid(state->context, req_addr, tagif, 1))) 891 config_ok = config_valid(config, c, &addr) && IN6_ARE_ADDR_EQUAL(&addr, req_addr); 892 893 if ((dynamic = address6_available(state->context, req_addr, tagif, 1)) || c) 894 { 895 if (!dynamic && !config_ok) 896 { 897 /* Static range, not configured. */ 898 o1 = new_opt6(OPTION6_STATUS_CODE); 899 put_opt6_short(DHCP6NOADDRS); 900 put_opt6_string(_("address unavailable")); 901 end_opt6(o1); 902 } 903 else if (!check_address(state, req_addr)) 904 { 905 /* Address leased to another DUID/IAID */ 906 o1 = new_opt6(OPTION6_STATUS_CODE); 907 put_opt6_short(DHCP6UNSPEC); 908 put_opt6_string(_("address in use")); 909 end_opt6(o1); 910 } 911 else 912 { 913 if (!dynamic) 914 dynamic = c; 915 916 lease_time = dynamic->lease_time; 917 918 if (config_ok && have_config(config, CONFIG_TIME)) 919 lease_time = config->lease_time; 920 921#ifdef OPTION6_PREFIX_CLASS 922 if (dump_all_prefix_classes && state->ia_type == OPTION6_IA_NA) 923 state->send_prefix_class = prefix_class_from_context(c); 924#endif 925 add_address(state, dynamic, lease_time, ia_option, &min_time, req_addr, now); 926 get_context_tag(state, dynamic); 927 address_assigned = 1; 928 } 929 } 930 else 931 { 932 /* requested address not on the correct link */ 933 o1 = new_opt6(OPTION6_STATUS_CODE); 934 put_opt6_short(DHCP6NOTONLINK); 935 put_opt6_string(_("not on link")); 936 end_opt6(o1); 937 } 938 } 939 940 end_ia(t1cntr, min_time, 0); 941 end_opt6(o); 942 } 943 944 if (address_assigned) 945 { 946 o1 = new_opt6(OPTION6_STATUS_CODE); 947 put_opt6_short(DHCP6SUCCESS); 948 put_opt6_string(_("success")); 949 end_opt6(o1); 950 } 951 else 952 { 953 /* no address, return error */ 954 o1 = new_opt6(OPTION6_STATUS_CODE); 955 put_opt6_short(DHCP6NOADDRS); 956 put_opt6_string(_("no addresses available")); 957 end_opt6(o1); 958 log6_packet(state, "DHCPREPLY", NULL, _("no addresses available")); 959 } 960 961 tagif = add_options(state, 0); 962 break; 963 } 964 965 966 case DHCP6RENEW: 967 { 968 /* set reply message type */ 969 *outmsgtypep = DHCP6REPLY; 970 971 log6_quiet(state, "DHCPRENEW", NULL, NULL); 972 973 for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end)) 974 { 975 void *ia_option, *ia_end; 976 unsigned int min_time = 0xffffffff; 977 int t1cntr, iacntr; 978 979 if (!check_ia(state, opt, &ia_end, &ia_option)) 980 continue; 981 982 o = build_ia(state, &t1cntr); 983 iacntr = save_counter(-1); 984 985 for (; ia_option; ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24)) 986 { 987 struct dhcp_lease *lease = NULL; 988 struct in6_addr *req_addr = opt6_ptr(ia_option, 0); 989 unsigned int preferred_time = opt6_uint(ia_option, 16, 4); 990 unsigned int valid_time = opt6_uint(ia_option, 20, 4); 991 char *message = NULL; 992 struct dhcp_context *this_context; 993 994 if (!(lease = lease6_find(state->clid, state->clid_len, 995 state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA, 996 state->iaid, req_addr))) 997 { 998 /* If the server cannot find a client entry for the IA the server 999 returns the IA containing no addresses with a Status Code option set 1000 to NoBinding in the Reply message. */ 1001 save_counter(iacntr); 1002 t1cntr = 0; 1003 1004 log6_packet(state, "DHCPREPLY", req_addr, _("lease not found")); 1005 1006 o1 = new_opt6(OPTION6_STATUS_CODE); 1007 put_opt6_short(DHCP6NOBINDING); 1008 put_opt6_string(_("no binding found")); 1009 end_opt6(o1); 1010 1011 preferred_time = valid_time = 0; 1012 break; 1013 } 1014 1015 1016 if ((this_context = address6_available(state->context, req_addr, tagif, 1)) || 1017 (this_context = address6_valid(state->context, req_addr, tagif, 1))) 1018 { 1019 struct in6_addr addr; 1020 unsigned int lease_time; 1021 1022 get_context_tag(state, this_context); 1023 1024 if (config_valid(config, this_context, &addr) && IN6_ARE_ADDR_EQUAL(&addr, req_addr) && have_config(config, CONFIG_TIME)) 1025 lease_time = config->lease_time; 1026 else 1027 lease_time = this_context->lease_time; 1028 1029 calculate_times(this_context, &min_time, &valid_time, &preferred_time, lease_time); 1030 1031 lease_set_expires(lease, valid_time, now); 1032 /* Update MAC record in case it's new information. */ 1033 if (state->mac_len != 0) 1034 lease_set_hwaddr(lease, state->mac, state->clid, state->mac_len, state->mac_type, state->clid_len, now, 0); 1035 if (state->ia_type == OPTION6_IA_NA && state->hostname) 1036 { 1037 char *addr_domain = get_domain6(req_addr); 1038 if (!state->send_domain) 1039 state->send_domain = addr_domain; 1040 lease_set_hostname(lease, state->hostname, state->hostname_auth, addr_domain, state->domain); 1041 message = state->hostname; 1042 } 1043 1044 1045 if (preferred_time == 0) 1046 message = _("deprecated"); 1047 } 1048 else 1049 { 1050 preferred_time = valid_time = 0; 1051 message = _("address invalid"); 1052 } 1053 1054 if (message && (message != state->hostname)) 1055 log6_packet(state, "DHCPREPLY", req_addr, message); 1056 else 1057 log6_quiet(state, "DHCPREPLY", req_addr, message); 1058 1059 o1 = new_opt6(OPTION6_IAADDR); 1060 put_opt6(req_addr, sizeof(*req_addr)); 1061 put_opt6_long(preferred_time); 1062 put_opt6_long(valid_time); 1063 end_opt6(o1); 1064 } 1065 1066 end_ia(t1cntr, min_time, 1); 1067 end_opt6(o); 1068 } 1069 1070 tagif = add_options(state, 0); 1071 break; 1072 1073 } 1074 1075 case DHCP6CONFIRM: 1076 { 1077 int good_addr = 0; 1078 1079 /* set reply message type */ 1080 *outmsgtypep = DHCP6REPLY; 1081 1082 log6_quiet(state, "DHCPCONFIRM", NULL, NULL); 1083 1084 for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end)) 1085 { 1086 void *ia_option, *ia_end; 1087 1088 for (check_ia(state, opt, &ia_end, &ia_option); 1089 ia_option; 1090 ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24)) 1091 { 1092 struct in6_addr *req_addr = opt6_ptr(ia_option, 0); 1093 1094 if (!address6_valid(state->context, req_addr, tagif, 1)) 1095 { 1096 o1 = new_opt6(OPTION6_STATUS_CODE); 1097 put_opt6_short(DHCP6NOTONLINK); 1098 put_opt6_string(_("confirm failed")); 1099 end_opt6(o1); 1100 return 1; 1101 } 1102 1103 good_addr = 1; 1104 log6_quiet(state, "DHCPREPLY", req_addr, state->hostname); 1105 } 1106 } 1107 1108 /* No addresses, no reply: RFC 3315 18.2.2 */ 1109 if (!good_addr) 1110 return 0; 1111 1112 o1 = new_opt6(OPTION6_STATUS_CODE); 1113 put_opt6_short(DHCP6SUCCESS ); 1114 put_opt6_string(_("all addresses still on link")); 1115 end_opt6(o1); 1116 break; 1117 } 1118 1119 case DHCP6IREQ: 1120 { 1121 /* We can't discriminate contexts based on address, as we don't know it. 1122 If there is only one possible context, we can use its tags */ 1123 if (state->context && state->context->netid.net && !state->context->current) 1124 { 1125 state->context->netid.next = NULL; 1126 state->context_tags = &state->context->netid; 1127 } 1128 1129 /* Similarly, we can't determine domain from address, but if the FQDN is 1130 given in --dhcp-host, we can use that, and failing that we can use the 1131 unqualified configured domain, if any. */ 1132 if (state->hostname_auth) 1133 state->send_domain = state->domain; 1134 else 1135 state->send_domain = get_domain6(NULL); 1136 1137 if (ignore || option_bool(OPT_LOG_OPTS)) 1138 log6_quiet(state, "DHCPINFORMATION-REQUEST", NULL, ignore ? _("ignored") : state->hostname); 1139 if (ignore) 1140 return 0; 1141 *outmsgtypep = DHCP6REPLY; 1142 tagif = add_options(state, 1); 1143 break; 1144 } 1145 1146 1147 case DHCP6RELEASE: 1148 { 1149 /* set reply message type */ 1150 *outmsgtypep = DHCP6REPLY; 1151 1152 log6_quiet(state, "DHCPRELEASE", NULL, NULL); 1153 1154 for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end)) 1155 { 1156 void *ia_option, *ia_end; 1157 int made_ia = 0; 1158 1159 for (check_ia(state, opt, &ia_end, &ia_option); 1160 ia_option; 1161 ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24)) 1162 { 1163 struct dhcp_lease *lease; 1164 1165 if ((lease = lease6_find(state->clid, state->clid_len, state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA, 1166 state->iaid, opt6_ptr(ia_option, 0)))) 1167 lease_prune(lease, now); 1168 else 1169 { 1170 if (!made_ia) 1171 { 1172 o = new_opt6(state->ia_type); 1173 put_opt6_long(state->iaid); 1174 if (state->ia_type == OPTION6_IA_NA) 1175 { 1176 put_opt6_long(0); 1177 put_opt6_long(0); 1178 } 1179 made_ia = 1; 1180 } 1181 1182 o1 = new_opt6(OPTION6_IAADDR); 1183 put_opt6(opt6_ptr(ia_option, 0), IN6ADDRSZ); 1184 put_opt6_long(0); 1185 put_opt6_long(0); 1186 end_opt6(o1); 1187 } 1188 } 1189 1190 if (made_ia) 1191 { 1192 o1 = new_opt6(OPTION6_STATUS_CODE); 1193 put_opt6_short(DHCP6NOBINDING); 1194 put_opt6_string(_("no binding found")); 1195 end_opt6(o1); 1196 1197 end_opt6(o); 1198 } 1199 } 1200 1201 o1 = new_opt6(OPTION6_STATUS_CODE); 1202 put_opt6_short(DHCP6SUCCESS); 1203 put_opt6_string(_("release received")); 1204 end_opt6(o1); 1205 1206 break; 1207 } 1208 1209 case DHCP6DECLINE: 1210 { 1211 /* set reply message type */ 1212 *outmsgtypep = DHCP6REPLY; 1213 1214 log6_quiet(state, "DHCPDECLINE", NULL, NULL); 1215 1216 for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end)) 1217 { 1218 void *ia_option, *ia_end; 1219 int made_ia = 0; 1220 1221 for (check_ia(state, opt, &ia_end, &ia_option); 1222 ia_option; 1223 ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24)) 1224 { 1225 struct dhcp_lease *lease; 1226 struct in6_addr *addrp = opt6_ptr(ia_option, 0); 1227 1228 if (have_config(config, CONFIG_ADDR6) && IN6_ARE_ADDR_EQUAL(&config->addr6, addrp)) 1229 { 1230 prettyprint_time(daemon->dhcp_buff3, DECLINE_BACKOFF); 1231 inet_ntop(AF_INET6, addrp, daemon->addrbuff, ADDRSTRLEN); 1232 my_syslog(MS_DHCP | LOG_WARNING, _("disabling DHCP static address %s for %s"), 1233 daemon->addrbuff, daemon->dhcp_buff3); 1234 config->flags |= CONFIG_DECLINED; 1235 config->decline_time = now; 1236 } 1237 else 1238 /* make sure this host gets a different address next time. */ 1239 for (context_tmp = state->context; context_tmp; context_tmp = context_tmp->current) 1240 context_tmp->addr_epoch++; 1241 1242 if ((lease = lease6_find(state->clid, state->clid_len, state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA, 1243 state->iaid, opt6_ptr(ia_option, 0)))) 1244 lease_prune(lease, now); 1245 else 1246 { 1247 if (!made_ia) 1248 { 1249 o = new_opt6(state->ia_type); 1250 put_opt6_long(state->iaid); 1251 if (state->ia_type == OPTION6_IA_NA) 1252 { 1253 put_opt6_long(0); 1254 put_opt6_long(0); 1255 } 1256 made_ia = 1; 1257 } 1258 1259 o1 = new_opt6(OPTION6_IAADDR); 1260 put_opt6(opt6_ptr(ia_option, 0), IN6ADDRSZ); 1261 put_opt6_long(0); 1262 put_opt6_long(0); 1263 end_opt6(o1); 1264 } 1265 } 1266 1267 if (made_ia) 1268 { 1269 o1 = new_opt6(OPTION6_STATUS_CODE); 1270 put_opt6_short(DHCP6NOBINDING); 1271 put_opt6_string(_("no binding found")); 1272 end_opt6(o1); 1273 1274 end_opt6(o); 1275 } 1276 1277 } 1278 1279 /* We must anwser with 'success' in global section anyway */ 1280 o1 = new_opt6(OPTION6_STATUS_CODE); 1281 put_opt6_short(DHCP6SUCCESS); 1282 put_opt6_string(_("success")); 1283 end_opt6(o1); 1284 break; 1285 } 1286 1287 } 1288 1289 log_tags(tagif, state->xid); 1290 log6_opts(0, state->xid, daemon->outpacket.iov_base + start_opts, daemon->outpacket.iov_base + save_counter(-1)); 1291 1292 return 1; 1293 1294} 1295 1296static struct dhcp_netid *add_options(struct state *state, int do_refresh) 1297{ 1298 void *oro; 1299 /* filter options based on tags, those we want get DHOPT_TAGOK bit set */ 1300 struct dhcp_netid *tagif = option_filter(state->tags, state->context_tags, daemon->dhcp_opts6); 1301 struct dhcp_opt *opt_cfg; 1302 int done_dns = 0, done_refresh = !do_refresh, do_encap = 0; 1303 int i, o, o1; 1304 1305 oro = opt6_find(state->packet_options, state->end, OPTION6_ORO, 0); 1306 1307 for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next) 1308 { 1309 /* netids match and not encapsulated? */ 1310 if (!(opt_cfg->flags & DHOPT_TAGOK)) 1311 continue; 1312 1313 if (!(opt_cfg->flags & DHOPT_FORCE) && oro) 1314 { 1315 for (i = 0; i < opt6_len(oro) - 1; i += 2) 1316 if (opt6_uint(oro, i, 2) == (unsigned)opt_cfg->opt) 1317 break; 1318 1319 /* option not requested */ 1320 if (i >= opt6_len(oro) - 1) 1321 continue; 1322 } 1323 1324 if (opt_cfg->opt == OPTION6_REFRESH_TIME) 1325 done_refresh = 1; 1326 1327 if (opt_cfg->opt == OPTION6_DNS_SERVER) 1328 done_dns = 1; 1329 1330 if (opt_cfg->flags & DHOPT_ADDR6) 1331 { 1332 int len, j; 1333 struct in6_addr *a; 1334 1335 for (a = (struct in6_addr *)opt_cfg->val, len = opt_cfg->len, j = 0; 1336 j < opt_cfg->len; j += IN6ADDRSZ, a++) 1337 if ((IN6_IS_ADDR_ULA_ZERO(a) && IN6_IS_ADDR_UNSPECIFIED(state->ula_addr)) || 1338 (IN6_IS_ADDR_LINK_LOCAL_ZERO(a) && IN6_IS_ADDR_UNSPECIFIED(state->ll_addr))) 1339 len -= IN6ADDRSZ; 1340 1341 if (len != 0) 1342 { 1343 1344 o = new_opt6(opt_cfg->opt); 1345 1346 for (a = (struct in6_addr *)opt_cfg->val, j = 0; j < opt_cfg->len; j+=IN6ADDRSZ, a++) 1347 { 1348 if (IN6_IS_ADDR_UNSPECIFIED(a)) 1349 { 1350 if (!add_local_addrs(state->context)) 1351 put_opt6(state->fallback, IN6ADDRSZ); 1352 } 1353 else if (IN6_IS_ADDR_ULA_ZERO(a)) 1354 { 1355 if (!IN6_IS_ADDR_UNSPECIFIED(state->ula_addr)) 1356 put_opt6(state->ula_addr, IN6ADDRSZ); 1357 } 1358 else if (IN6_IS_ADDR_LINK_LOCAL_ZERO(a)) 1359 { 1360 if (!IN6_IS_ADDR_UNSPECIFIED(state->ll_addr)) 1361 put_opt6(state->ll_addr, IN6ADDRSZ); 1362 } 1363 else 1364 put_opt6(a, IN6ADDRSZ); 1365 } 1366 1367 end_opt6(o); 1368 } 1369 } 1370 else 1371 { 1372 o = new_opt6(opt_cfg->opt); 1373 if (opt_cfg->val) 1374 put_opt6(opt_cfg->val, opt_cfg->len); 1375 end_opt6(o); 1376 } 1377 } 1378 1379 if (daemon->port == NAMESERVER_PORT && !done_dns) 1380 { 1381 o = new_opt6(OPTION6_DNS_SERVER); 1382 if (!add_local_addrs(state->context)) 1383 put_opt6(state->fallback, IN6ADDRSZ); 1384 end_opt6(o); 1385 } 1386 1387 if (state->context && !done_refresh) 1388 { 1389 struct dhcp_context *c; 1390 unsigned int lease_time = 0xffffffff; 1391 1392 /* Find the smallest lease tie of all contexts, 1393 subjext to the RFC-4242 stipulation that this must not 1394 be less than 600. */ 1395 for (c = state->context; c; c = c->next) 1396 if (c->lease_time < lease_time) 1397 { 1398 if (c->lease_time < 600) 1399 lease_time = 600; 1400 else 1401 lease_time = c->lease_time; 1402 } 1403 1404 o = new_opt6(OPTION6_REFRESH_TIME); 1405 put_opt6_long(lease_time); 1406 end_opt6(o); 1407 } 1408 1409 /* handle vendor-identifying vendor-encapsulated options, 1410 dhcp-option = vi-encap:13,17,....... */ 1411 for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next) 1412 opt_cfg->flags &= ~DHOPT_ENCAP_DONE; 1413 1414 if (oro) 1415 for (i = 0; i < opt6_len(oro) - 1; i += 2) 1416 if (opt6_uint(oro, i, 2) == OPTION6_VENDOR_OPTS) 1417 do_encap = 1; 1418 1419 for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next) 1420 { 1421 if (opt_cfg->flags & DHOPT_RFC3925) 1422 { 1423 int found = 0; 1424 struct dhcp_opt *oc; 1425 1426 if (opt_cfg->flags & DHOPT_ENCAP_DONE) 1427 continue; 1428 1429 for (oc = daemon->dhcp_opts6; oc; oc = oc->next) 1430 { 1431 oc->flags &= ~DHOPT_ENCAP_MATCH; 1432 1433 if (!(oc->flags & DHOPT_RFC3925) || opt_cfg->u.encap != oc->u.encap) 1434 continue; 1435 1436 oc->flags |= DHOPT_ENCAP_DONE; 1437 if (match_netid(oc->netid, tagif, 1)) 1438 { 1439 /* option requested/forced? */ 1440 if (!oro || do_encap || (oc->flags & DHOPT_FORCE)) 1441 { 1442 oc->flags |= DHOPT_ENCAP_MATCH; 1443 found = 1; 1444 } 1445 } 1446 } 1447 1448 if (found) 1449 { 1450 o = new_opt6(OPTION6_VENDOR_OPTS); 1451 put_opt6_long(opt_cfg->u.encap); 1452 1453 for (oc = daemon->dhcp_opts6; oc; oc = oc->next) 1454 if (oc->flags & DHOPT_ENCAP_MATCH) 1455 { 1456 o1 = new_opt6(oc->opt); 1457 put_opt6(oc->val, oc->len); 1458 end_opt6(o1); 1459 } 1460 end_opt6(o); 1461 } 1462 } 1463 } 1464 1465 1466 if (state->hostname) 1467 { 1468 unsigned char *p; 1469 size_t len = strlen(state->hostname); 1470 1471 if (state->send_domain) 1472 len += strlen(state->send_domain) + 2; 1473 1474 o = new_opt6(OPTION6_FQDN); 1475 if ((p = expand(len + 2))) 1476 { 1477 *(p++) = state->fqdn_flags; 1478 p = do_rfc1035_name(p, state->hostname); 1479 if (state->send_domain) 1480 { 1481 p = do_rfc1035_name(p, state->send_domain); 1482 *p = 0; 1483 } 1484 } 1485 end_opt6(o); 1486 } 1487 1488 1489 /* logging */ 1490 if (option_bool(OPT_LOG_OPTS) && oro) 1491 { 1492 char *q = daemon->namebuff; 1493 for (i = 0; i < opt6_len(oro) - 1; i += 2) 1494 { 1495 char *s = option_string(AF_INET6, opt6_uint(oro, i, 2), NULL, 0, NULL, 0); 1496 q += snprintf(q, MAXDNAME - (q - daemon->namebuff), 1497 "%d%s%s%s", 1498 opt6_uint(oro, i, 2), 1499 strlen(s) != 0 ? ":" : "", 1500 s, 1501 (i > opt6_len(oro) - 3) ? "" : ", "); 1502 if ( i > opt6_len(oro) - 3 || (q - daemon->namebuff) > 40) 1503 { 1504 q = daemon->namebuff; 1505 my_syslog(MS_DHCP | LOG_INFO, _("%u requested options: %s"), state->xid, daemon->namebuff); 1506 } 1507 } 1508 } 1509 1510 return tagif; 1511} 1512 1513static int add_local_addrs(struct dhcp_context *context) 1514{ 1515 int done = 0; 1516 1517 for (; context; context = context->current) 1518 if ((context->flags & CONTEXT_USED) && !IN6_IS_ADDR_UNSPECIFIED(&context->local6)) 1519 { 1520 /* squash duplicates */ 1521 struct dhcp_context *c; 1522 for (c = context->current; c; c = c->current) 1523 if ((c->flags & CONTEXT_USED) && 1524 IN6_ARE_ADDR_EQUAL(&context->local6, &c->local6)) 1525 break; 1526 1527 if (!c) 1528 { 1529 done = 1; 1530 put_opt6(&context->local6, IN6ADDRSZ); 1531 } 1532 } 1533 1534 return done; 1535} 1536 1537 1538static void get_context_tag(struct state *state, struct dhcp_context *context) 1539{ 1540 /* get tags from context if we've not used it before */ 1541 if (context->netid.next == &context->netid && context->netid.net) 1542 { 1543 context->netid.next = state->context_tags; 1544 state->context_tags = &context->netid; 1545 if (!state->hostname_auth) 1546 { 1547 struct dhcp_netid_list *id_list; 1548 1549 for (id_list = daemon->dhcp_ignore_names; id_list; id_list = id_list->next) 1550 if ((!id_list->list) || match_netid(id_list->list, &context->netid, 0)) 1551 break; 1552 if (id_list) 1553 state->hostname = NULL; 1554 } 1555 } 1556} 1557 1558#ifdef OPTION6_PREFIX_CLASS 1559static struct prefix_class *prefix_class_from_context(struct dhcp_context *context) 1560{ 1561 struct prefix_class *p; 1562 struct dhcp_netid *t; 1563 1564 for (p = daemon->prefix_classes; p ; p = p->next) 1565 for (t = context->filter; t; t = t->next) 1566 if (strcmp(p->tag.net, t->net) == 0) 1567 return p; 1568 1569 return NULL; 1570} 1571#endif 1572 1573static int check_ia(struct state *state, void *opt, void **endp, void **ia_option) 1574{ 1575 state->ia_type = opt6_type(opt); 1576 *ia_option = NULL; 1577 1578 if (state->ia_type != OPTION6_IA_NA && state->ia_type != OPTION6_IA_TA) 1579 return 0; 1580 1581 if (state->ia_type == OPTION6_IA_NA && opt6_len(opt) < 12) 1582 return 0; 1583 1584 if (state->ia_type == OPTION6_IA_TA && opt6_len(opt) < 4) 1585 return 0; 1586 1587 *endp = opt6_ptr(opt, opt6_len(opt)); 1588 state->iaid = opt6_uint(opt, 0, 4); 1589 *ia_option = opt6_find(opt6_ptr(opt, state->ia_type == OPTION6_IA_NA ? 12 : 4), *endp, OPTION6_IAADDR, 24); 1590 1591 return 1; 1592} 1593 1594 1595static int build_ia(struct state *state, int *t1cntr) 1596{ 1597 int o = new_opt6(state->ia_type); 1598 1599 put_opt6_long(state->iaid); 1600 *t1cntr = 0; 1601 1602 if (state->ia_type == OPTION6_IA_NA) 1603 { 1604 /* save pointer */ 1605 *t1cntr = save_counter(-1); 1606 /* so we can fill these in later */ 1607 put_opt6_long(0); 1608 put_opt6_long(0); 1609 } 1610 1611 return o; 1612} 1613 1614static void end_ia(int t1cntr, unsigned int min_time, int do_fuzz) 1615{ 1616 if (t1cntr != 0) 1617 { 1618 /* go back an fill in fields in IA_NA option */ 1619 int sav = save_counter(t1cntr); 1620 unsigned int t1, t2, fuzz = 0; 1621 1622 if (do_fuzz) 1623 { 1624 fuzz = rand16(); 1625 1626 while (fuzz > (min_time/16)) 1627 fuzz = fuzz/2; 1628 } 1629 1630 t1 = (min_time == 0xffffffff) ? 0xffffffff : min_time/2 - fuzz; 1631 t2 = (min_time == 0xffffffff) ? 0xffffffff : ((min_time/8)*7) - fuzz; 1632 put_opt6_long(t1); 1633 put_opt6_long(t2); 1634 save_counter(sav); 1635 } 1636} 1637 1638static void add_address(struct state *state, struct dhcp_context *context, unsigned int lease_time, void *ia_option, 1639 unsigned int *min_time, struct in6_addr *addr, time_t now) 1640{ 1641 unsigned int valid_time = 0, preferred_time = 0; 1642 int o = new_opt6(OPTION6_IAADDR); 1643 struct dhcp_lease *lease; 1644 1645 /* get client requested times */ 1646 if (ia_option) 1647 { 1648 preferred_time = opt6_uint(ia_option, 16, 4); 1649 valid_time = opt6_uint(ia_option, 20, 4); 1650 } 1651 1652 calculate_times(context, min_time, &valid_time, &preferred_time, lease_time); 1653 1654 put_opt6(addr, sizeof(*addr)); 1655 put_opt6_long(preferred_time); 1656 put_opt6_long(valid_time); 1657 1658#ifdef OPTION6_PREFIX_CLASS 1659 if (state->send_prefix_class) 1660 { 1661 int o1 = new_opt6(OPTION6_PREFIX_CLASS); 1662 put_opt6_short(state->send_prefix_class->class); 1663 end_opt6(o1); 1664 } 1665#endif 1666 1667 end_opt6(o); 1668 1669 if (state->lease_allocate) 1670 update_leases(state, context, addr, valid_time, now); 1671 1672 if ((lease = lease6_find_by_addr(addr, 128, 0))) 1673 lease->flags |= LEASE_USED; 1674 1675 /* get tags from context if we've not used it before */ 1676 if (context->netid.next == &context->netid && context->netid.net) 1677 { 1678 context->netid.next = state->context_tags; 1679 state->context_tags = &context->netid; 1680 1681 if (!state->hostname_auth) 1682 { 1683 struct dhcp_netid_list *id_list; 1684 1685 for (id_list = daemon->dhcp_ignore_names; id_list; id_list = id_list->next) 1686 if ((!id_list->list) || match_netid(id_list->list, &context->netid, 0)) 1687 break; 1688 if (id_list) 1689 state->hostname = NULL; 1690 } 1691 } 1692 1693 log6_quiet(state, state->lease_allocate ? "DHCPREPLY" : "DHCPADVERTISE", addr, state->hostname); 1694 1695} 1696 1697static void mark_context_used(struct state *state, struct in6_addr *addr) 1698{ 1699 struct dhcp_context *context; 1700 1701 /* Mark that we have an address for this prefix. */ 1702#ifdef OPTION6_PREFIX_CLASS 1703 for (context = state->context; context; context = context->current) 1704 if (is_same_net6(addr, &context->start6, context->prefix) && 1705 (!state->send_prefix_class || state->send_prefix_class == prefix_class_from_context(context))) 1706 context->flags |= CONTEXT_USED; 1707#else 1708 for (context = state->context; context; context = context->current) 1709 if (is_same_net6(addr, &context->start6, context->prefix)) 1710 context->flags |= CONTEXT_USED; 1711#endif 1712} 1713 1714static void mark_config_used(struct dhcp_context *context, struct in6_addr *addr) 1715{ 1716 for (; context; context = context->current) 1717 if (is_same_net6(addr, &context->start6, context->prefix)) 1718 context->flags |= CONTEXT_CONF_USED; 1719} 1720 1721/* make sure address not leased to another CLID/IAID */ 1722static int check_address(struct state *state, struct in6_addr *addr) 1723{ 1724 struct dhcp_lease *lease; 1725 1726 if (!(lease = lease6_find_by_addr(addr, 128, 0))) 1727 return 1; 1728 1729 if (lease->clid_len != state->clid_len || 1730 memcmp(lease->clid, state->clid, state->clid_len) != 0 || 1731 lease->iaid != state->iaid) 1732 return 0; 1733 1734 return 1; 1735} 1736 1737 1738/* Calculate valid and preferred times to send in leases/renewals. 1739 1740 Inputs are: 1741 1742 *valid_timep, *preferred_timep - requested times from IAADDR options. 1743 context->valid, context->preferred - times associated with subnet address on local interface. 1744 context->flags | CONTEXT_DEPRECATE - "deprecated" flag in dhcp-range. 1745 lease_time - configured time for context for individual client. 1746 *min_time - smallest valid time sent so far. 1747 1748 Outputs are : 1749 1750 *valid_timep, *preferred_timep - times to be send in IAADDR option. 1751 *min_time - smallest valid time sent so far, to calculate T1 and T2. 1752 1753 */ 1754static void calculate_times(struct dhcp_context *context, unsigned int *min_time, unsigned int *valid_timep, 1755 unsigned int *preferred_timep, unsigned int lease_time) 1756{ 1757 unsigned int req_preferred = *preferred_timep, req_valid = *valid_timep; 1758 unsigned int valid_time = lease_time, preferred_time = lease_time; 1759 1760 /* RFC 3315: "A server ignores the lifetimes set 1761 by the client if the preferred lifetime is greater than the valid 1762 lifetime. */ 1763 if (req_preferred <= req_valid) 1764 { 1765 if (req_preferred != 0) 1766 { 1767 /* 0 == "no preference from client" */ 1768 if (req_preferred < 120u) 1769 req_preferred = 120u; /* sanity */ 1770 1771 if (req_preferred < preferred_time) 1772 preferred_time = req_preferred; 1773 } 1774 1775 if (req_valid != 0) 1776 /* 0 == "no preference from client" */ 1777 { 1778 if (req_valid < 120u) 1779 req_valid = 120u; /* sanity */ 1780 1781 if (req_valid < valid_time) 1782 valid_time = req_valid; 1783 } 1784 } 1785 1786 /* deprecate (preferred == 0) which configured, or when local address 1787 is deprecated */ 1788 if ((context->flags & CONTEXT_DEPRECATE) || context->preferred == 0) 1789 preferred_time = 0; 1790 1791 if (preferred_time != 0 && preferred_time < *min_time) 1792 *min_time = preferred_time; 1793 1794 if (valid_time != 0 && valid_time < *min_time) 1795 *min_time = valid_time; 1796 1797 *valid_timep = valid_time; 1798 *preferred_timep = preferred_time; 1799} 1800 1801static void update_leases(struct state *state, struct dhcp_context *context, struct in6_addr *addr, unsigned int lease_time, time_t now) 1802{ 1803 struct dhcp_lease *lease = lease6_find_by_addr(addr, 128, 0); 1804#ifdef HAVE_SCRIPT 1805 struct dhcp_netid *tagif = run_tag_if(state->tags); 1806#endif 1807 1808 (void)context; 1809 1810 if (!lease) 1811 lease = lease6_allocate(addr, state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA); 1812 1813 if (lease) 1814 { 1815 lease_set_expires(lease, lease_time, now); 1816 lease_set_iaid(lease, state->iaid); 1817 lease_set_hwaddr(lease, state->mac, state->clid, state->mac_len, state->mac_type, state->clid_len, now, 0); 1818 lease_set_interface(lease, state->interface, now); 1819 if (state->hostname && state->ia_type == OPTION6_IA_NA) 1820 { 1821 char *addr_domain = get_domain6(addr); 1822 if (!state->send_domain) 1823 state->send_domain = addr_domain; 1824 lease_set_hostname(lease, state->hostname, state->hostname_auth, addr_domain, state->domain); 1825 } 1826 1827#ifdef HAVE_SCRIPT 1828 if (daemon->lease_change_command) 1829 { 1830 void *class_opt; 1831 lease->flags |= LEASE_CHANGED; 1832 free(lease->extradata); 1833 lease->extradata = NULL; 1834 lease->extradata_size = lease->extradata_len = 0; 1835 lease->vendorclass_count = 0; 1836 1837 if ((class_opt = opt6_find(state->packet_options, state->end, OPTION6_VENDOR_CLASS, 4))) 1838 { 1839 void *enc_opt, *enc_end = opt6_ptr(class_opt, opt6_len(class_opt)); 1840 lease->vendorclass_count++; 1841 /* send enterprise number first */ 1842 sprintf(daemon->dhcp_buff2, "%u", opt6_uint(class_opt, 0, 4)); 1843 lease_add_extradata(lease, (unsigned char *)daemon->dhcp_buff2, strlen(daemon->dhcp_buff2), 0); 1844 1845 if (opt6_len(class_opt) >= 6) 1846 for (enc_opt = opt6_ptr(class_opt, 4); enc_opt; enc_opt = opt6_next(enc_opt, enc_end)) 1847 { 1848 lease->vendorclass_count++; 1849 lease_add_extradata(lease, opt6_ptr(enc_opt, 0), opt6_len(enc_opt), 0); 1850 } 1851 } 1852 1853 lease_add_extradata(lease, (unsigned char *)state->client_hostname, 1854 state->client_hostname ? strlen(state->client_hostname) : 0, 0); 1855 1856 /* space-concat tag set */ 1857 if (!tagif && !context->netid.net) 1858 lease_add_extradata(lease, NULL, 0, 0); 1859 else 1860 { 1861 if (context->netid.net) 1862 lease_add_extradata(lease, (unsigned char *)context->netid.net, strlen(context->netid.net), tagif ? ' ' : 0); 1863 1864 if (tagif) 1865 { 1866 struct dhcp_netid *n; 1867 for (n = tagif; n; n = n->next) 1868 { 1869 struct dhcp_netid *n1; 1870 /* kill dupes */ 1871 for (n1 = n->next; n1; n1 = n1->next) 1872 if (strcmp(n->net, n1->net) == 0) 1873 break; 1874 if (!n1) 1875 lease_add_extradata(lease, (unsigned char *)n->net, strlen(n->net), n->next ? ' ' : 0); 1876 } 1877 } 1878 } 1879 1880 if (state->link_address) 1881 inet_ntop(AF_INET6, state->link_address, daemon->addrbuff, ADDRSTRLEN); 1882 1883 lease_add_extradata(lease, (unsigned char *)daemon->addrbuff, state->link_address ? strlen(daemon->addrbuff) : 0, 0); 1884 1885 if ((class_opt = opt6_find(state->packet_options, state->end, OPTION6_USER_CLASS, 2))) 1886 { 1887 void *enc_opt, *enc_end = opt6_ptr(class_opt, opt6_len(class_opt)); 1888 for (enc_opt = opt6_ptr(class_opt, 0); enc_opt; enc_opt = opt6_next(enc_opt, enc_end)) 1889 lease_add_extradata(lease, opt6_ptr(enc_opt, 0), opt6_len(enc_opt), 0); 1890 } 1891 } 1892#endif 1893 1894 } 1895} 1896 1897 1898 1899static void log6_opts(int nest, unsigned int xid, void *start_opts, void *end_opts) 1900{ 1901 void *opt; 1902 char *desc = nest ? "nest" : "sent"; 1903 1904 if (!option_bool(OPT_LOG_OPTS) || start_opts == end_opts) 1905 return; 1906 1907 for (opt = start_opts; opt; opt = opt6_next(opt, end_opts)) 1908 { 1909 int type = opt6_type(opt); 1910 void *ia_options = NULL; 1911 char *optname; 1912 1913 if (type == OPTION6_IA_NA) 1914 { 1915 sprintf(daemon->namebuff, "IAID=%u T1=%u T2=%u", 1916 opt6_uint(opt, 0, 4), opt6_uint(opt, 4, 4), opt6_uint(opt, 8, 4)); 1917 optname = "ia-na"; 1918 ia_options = opt6_ptr(opt, 12); 1919 } 1920 else if (type == OPTION6_IA_TA) 1921 { 1922 sprintf(daemon->namebuff, "IAID=%u", opt6_uint(opt, 0, 4)); 1923 optname = "ia-ta"; 1924 ia_options = opt6_ptr(opt, 4); 1925 } 1926 else if (type == OPTION6_IAADDR) 1927 { 1928 inet_ntop(AF_INET6, opt6_ptr(opt, 0), daemon->addrbuff, ADDRSTRLEN); 1929 sprintf(daemon->namebuff, "%s PL=%u VL=%u", 1930 daemon->addrbuff, opt6_uint(opt, 16, 4), opt6_uint(opt, 20, 4)); 1931 optname = "iaaddr"; 1932 ia_options = opt6_ptr(opt, 24); 1933 } 1934#ifdef OPTION6_PREFIX_CLASS 1935 else if (type == OPTION6_PREFIX_CLASS) 1936 { 1937 optname = "prefix-class"; 1938 sprintf(daemon->namebuff, "class=%u", opt6_uint(opt, 0, 2)); 1939 } 1940#endif 1941 else if (type == OPTION6_STATUS_CODE) 1942 { 1943 int len = sprintf(daemon->namebuff, "%u ", opt6_uint(opt, 0, 2)); 1944 memcpy(daemon->namebuff + len, opt6_ptr(opt, 2), opt6_len(opt)-2); 1945 daemon->namebuff[len + opt6_len(opt) - 2] = 0; 1946 optname = "status"; 1947 } 1948 else 1949 { 1950 /* account for flag byte on FQDN */ 1951 int offset = type == OPTION6_FQDN ? 1 : 0; 1952 optname = option_string(AF_INET6, type, opt6_ptr(opt, offset), opt6_len(opt) - offset, daemon->namebuff, MAXDNAME); 1953 } 1954 1955 my_syslog(MS_DHCP | LOG_INFO, "%u %s size:%3d option:%3d %s %s", 1956 xid, desc, opt6_len(opt), type, optname, daemon->namebuff); 1957 1958 if (ia_options) 1959 log6_opts(1, xid, ia_options, opt6_ptr(opt, opt6_len(opt))); 1960 } 1961} 1962 1963static void log6_quiet(struct state *state, char *type, struct in6_addr *addr, char *string) 1964{ 1965 if (option_bool(OPT_LOG_OPTS) || !option_bool(OPT_QUIET_DHCP6)) 1966 log6_packet(state, type, addr, string); 1967} 1968 1969static void log6_packet(struct state *state, char *type, struct in6_addr *addr, char *string) 1970{ 1971 int clid_len = state->clid_len; 1972 1973 /* avoid buffer overflow */ 1974 if (clid_len > 100) 1975 clid_len = 100; 1976 1977 print_mac(daemon->namebuff, state->clid, clid_len); 1978 1979 if (addr) 1980 { 1981 inet_ntop(AF_INET6, addr, daemon->dhcp_buff2, 255); 1982 strcat(daemon->dhcp_buff2, " "); 1983 } 1984 else 1985 daemon->dhcp_buff2[0] = 0; 1986 1987 if(option_bool(OPT_LOG_OPTS)) 1988 my_syslog(MS_DHCP | LOG_INFO, "%u %s(%s) %s%s %s", 1989 state->xid, 1990 type, 1991 state->iface_name, 1992 daemon->dhcp_buff2, 1993 daemon->namebuff, 1994 string ? string : ""); 1995 else 1996 my_syslog(MS_DHCP | LOG_INFO, "%s(%s) %s%s %s", 1997 type, 1998 state->iface_name, 1999 daemon->dhcp_buff2, 2000 daemon->namebuff, 2001 string ? string : ""); 2002} 2003 2004static void *opt6_find (void *opts, void *end, unsigned int search, unsigned int minsize) 2005{ 2006 u16 opt, opt_len; 2007 void *start; 2008 2009 if (!opts) 2010 return NULL; 2011 2012 while (1) 2013 { 2014 if (end - opts < 4) 2015 return NULL; 2016 2017 start = opts; 2018 GETSHORT(opt, opts); 2019 GETSHORT(opt_len, opts); 2020 2021 if (opt_len > (end - opts)) 2022 return NULL; 2023 2024 if (opt == search && (opt_len >= minsize)) 2025 return start; 2026 2027 opts += opt_len; 2028 } 2029} 2030 2031static void *opt6_next(void *opts, void *end) 2032{ 2033 u16 opt_len; 2034 2035 if (end - opts < 4) 2036 return NULL; 2037 2038 opts += 2; 2039 GETSHORT(opt_len, opts); 2040 2041 if (opt_len >= (end - opts)) 2042 return NULL; 2043 2044 return opts + opt_len; 2045} 2046 2047static unsigned int opt6_uint(unsigned char *opt, int offset, int size) 2048{ 2049 /* this worries about unaligned data and byte order */ 2050 unsigned int ret = 0; 2051 int i; 2052 unsigned char *p = opt6_ptr(opt, offset); 2053 2054 for (i = 0; i < size; i++) 2055 ret = (ret << 8) | *p++; 2056 2057 return ret; 2058} 2059 2060void relay_upstream6(struct dhcp_relay *relay, ssize_t sz, struct in6_addr *peer_address, u32 scope_id) 2061{ 2062 /* ->local is same value for all relays on ->current chain */ 2063 2064 struct all_addr from; 2065 unsigned char *header; 2066 unsigned char *inbuff = daemon->dhcp_packet.iov_base; 2067 int msg_type = *inbuff; 2068 int hopcount; 2069 struct in6_addr multicast; 2070 unsigned int maclen, mactype; 2071 unsigned char mac[DHCP_CHADDR_MAX]; 2072 2073 inet_pton(AF_INET6, ALL_SERVERS, &multicast); 2074 get_client_mac(peer_address, scope_id, mac, &maclen, &mactype); 2075 2076 /* source address == relay address */ 2077 from.addr.addr6 = relay->local.addr.addr6; 2078 2079 /* Get hop count from nested relayed message */ 2080 if (msg_type == DHCP6RELAYFORW) 2081 hopcount = *((unsigned char *)inbuff+1) + 1; 2082 else 2083 hopcount = 0; 2084 2085 /* RFC 3315 HOP_COUNT_LIMIT */ 2086 if (hopcount > 32) 2087 return; 2088 2089 save_counter(0); 2090 2091 if ((header = put_opt6(NULL, 34))) 2092 { 2093 int o; 2094 2095 header[0] = DHCP6RELAYFORW; 2096 header[1] = hopcount; 2097 memcpy(&header[2], &relay->local.addr.addr6, IN6ADDRSZ); 2098 memcpy(&header[18], peer_address, IN6ADDRSZ); 2099 2100 /* RFC-6939 */ 2101 if (maclen != 0) 2102 { 2103 o = new_opt6(OPTION6_CLIENT_MAC); 2104 put_opt6_short(mactype); 2105 put_opt6(mac, maclen); 2106 end_opt6(o); 2107 } 2108 2109 o = new_opt6(OPTION6_RELAY_MSG); 2110 put_opt6(inbuff, sz); 2111 end_opt6(o); 2112 2113 for (; relay; relay = relay->current) 2114 { 2115 union mysockaddr to; 2116 2117 to.sa.sa_family = AF_INET6; 2118 to.in6.sin6_addr = relay->server.addr.addr6; 2119 to.in6.sin6_port = htons(DHCPV6_SERVER_PORT); 2120 to.in6.sin6_flowinfo = 0; 2121 to.in6.sin6_scope_id = 0; 2122 2123 if (IN6_ARE_ADDR_EQUAL(&relay->server.addr.addr6, &multicast)) 2124 { 2125 int multicast_iface; 2126 if (!relay->interface || strchr(relay->interface, '*') || 2127 (multicast_iface = if_nametoindex(relay->interface)) == 0 || 2128 setsockopt(daemon->dhcp6fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &multicast_iface, sizeof(multicast_iface)) == -1) 2129 my_syslog(MS_DHCP | LOG_ERR, _("Cannot multicast to DHCPv6 server without correct interface")); 2130 } 2131 2132 send_from(daemon->dhcp6fd, 0, daemon->outpacket.iov_base, save_counter(0), &to, &from, 0); 2133 2134 if (option_bool(OPT_LOG_OPTS)) 2135 { 2136 inet_ntop(AF_INET6, &relay->local, daemon->addrbuff, ADDRSTRLEN); 2137 inet_ntop(AF_INET6, &relay->server, daemon->namebuff, ADDRSTRLEN); 2138 my_syslog(MS_DHCP | LOG_INFO, _("DHCP relay %s -> %s"), daemon->addrbuff, daemon->namebuff); 2139 } 2140 2141 /* Save this for replies */ 2142 relay->iface_index = scope_id; 2143 } 2144 } 2145} 2146 2147unsigned short relay_reply6(struct sockaddr_in6 *peer, ssize_t sz, char *arrival_interface) 2148{ 2149 struct dhcp_relay *relay; 2150 struct in6_addr link; 2151 unsigned char *inbuff = daemon->dhcp_packet.iov_base; 2152 2153 /* must have at least msg_type+hopcount+link_address+peer_address+minimal size option 2154 which is 1 + 1 + 16 + 16 + 2 + 2 = 38 */ 2155 2156 if (sz < 38 || *inbuff != DHCP6RELAYREPL) 2157 return 0; 2158 2159 memcpy(&link, &inbuff[2], IN6ADDRSZ); 2160 2161 for (relay = daemon->relay6; relay; relay = relay->next) 2162 if (IN6_ARE_ADDR_EQUAL(&link, &relay->local.addr.addr6) && 2163 (!relay->interface || wildcard_match(relay->interface, arrival_interface))) 2164 break; 2165 2166 save_counter(0); 2167 2168 if (relay) 2169 { 2170 void *opt, *opts = inbuff + 34; 2171 void *end = inbuff + sz; 2172 for (opt = opts; opt; opt = opt6_next(opt, end)) 2173 if (opt6_type(opt) == OPTION6_RELAY_MSG && opt6_len(opt) > 0) 2174 { 2175 int encap_type = *((unsigned char *)opt6_ptr(opt, 0)); 2176 put_opt6(opt6_ptr(opt, 0), opt6_len(opt)); 2177 memcpy(&peer->sin6_addr, &inbuff[18], IN6ADDRSZ); 2178 peer->sin6_scope_id = relay->iface_index; 2179 return encap_type == DHCP6RELAYREPL ? DHCPV6_SERVER_PORT : DHCPV6_CLIENT_PORT; 2180 } 2181 } 2182 2183 return 0; 2184} 2185 2186#endif 2187