1 /* dnsmasq is Copyright (c) 2000-2022 Simon Kelley
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.
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.
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/>.
24 int clid_len, 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, iaid;
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;
36 static int dhcp6_maybe_relay(struct state *state, unsigned char *inbuff, size_t sz,
37 struct in6_addr *client_addr, int is_unicast, time_t now);
38 static int dhcp6_no_relay(struct state *state, int msg_type, unsigned char *inbuff, size_t sz, int is_unicast, time_t now);
39 static void log6_opts(int nest, unsigned int xid, void *start_opts, void *end_opts);
40 static void log6_packet(struct state *state, char *type, struct in6_addr *addr, char *string);
41 static void log6_quiet(struct state *state, char *type, struct in6_addr *addr, char *string);
42 static void *opt6_find (void *opts, void *end, unsigned int search, unsigned int minsize);
43 static void *opt6_next(void *opts, void *end);
44 static unsigned int opt6_uint(unsigned char *opt, int offset, int size);
45 static void get_context_tag(struct state *state, struct dhcp_context *context);
46 static int check_ia(struct state *state, void *opt, void **endp, void **ia_option);
47 static int build_ia(struct state *state, int *t1cntr);
48 static void end_ia(int t1cntr, unsigned int min_time, int do_fuzz);
49 static void mark_context_used(struct state *state, struct in6_addr *addr);
50 static void mark_config_used(struct dhcp_context *context, struct in6_addr *addr);
51 static int check_address(struct state *state, struct in6_addr *addr);
52 static int config_valid(struct dhcp_config *config, struct dhcp_context *context, struct in6_addr *addr, struct state *state, time_t now);
53 static struct addrlist *config_implies(struct dhcp_config *config, struct dhcp_context *context, struct in6_addr *addr);
54 static void add_address(struct state *state, struct dhcp_context *context, unsigned int lease_time, void *ia_option,
55 unsigned int *min_time, struct in6_addr *addr, time_t now);
56 static void update_leases(struct state *state, struct dhcp_context *context, struct in6_addr *addr, unsigned int lease_time, time_t now);
57 static int add_local_addrs(struct dhcp_context *context);
58 static struct dhcp_netid *add_options(struct state *state, int do_refresh);
59 static void calculate_times(struct dhcp_context *context, unsigned int *min_time, unsigned int *valid_timep,
60 unsigned int *preferred_timep, unsigned int lease_time);
62 #define opt6_len(opt) ((int)(opt6_uint(opt, -2, 2)))
63 #define opt6_type(opt) (opt6_uint(opt, -4, 2))
64 #define opt6_ptr(opt, i) ((void *)&(((unsigned char *)(opt))[4+(i)]))
66 #define opt6_user_vendor_ptr(opt, i) ((void *)&(((unsigned char *)(opt))[2+(i)]))
67 #define opt6_user_vendor_len(opt) ((int)(opt6_uint(opt, -4, 2)))
68 #define opt6_user_vendor_next(opt, end) (opt6_next(((void *) opt) - 2, end))
71 unsigned short dhcp6_reply(struct dhcp_context *context, int interface, char *iface_name,
72 struct in6_addr *fallback, struct in6_addr *ll_addr, struct in6_addr *ula_addr,
73 size_t sz, struct in6_addr *client_addr, time_t now)
75 struct dhcp_vendor *vendor;
82 msg_type = *((unsigned char *)daemon->dhcp_packet.iov_base);
84 /* Mark these so we only match each at most once, to avoid tangled linked lists */
85 for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next)
86 vendor->netid.next = &vendor->netid;
89 state.context = context;
90 state.interface = interface;
91 state.iface_name = iface_name;
92 state.fallback = fallback;
93 state.ll_addr = ll_addr;
94 state.ula_addr = ula_addr;
97 state.link_address = NULL;
99 if (dhcp6_maybe_relay(&state, daemon->dhcp_packet.iov_base, sz, client_addr,
100 IN6_IS_ADDR_MULTICAST(client_addr), now))
101 return msg_type == DHCP6RELAYFORW ? DHCPV6_SERVER_PORT : DHCPV6_CLIENT_PORT;
106 /* This cost me blood to write, it will probably cost you blood to understand - srk. */
107 static int dhcp6_maybe_relay(struct state *state, unsigned char *inbuff, size_t sz,
108 struct in6_addr *client_addr, int is_unicast, time_t now)
110 void *end = inbuff + sz;
111 void *opts = inbuff + 34;
112 int msg_type = *inbuff;
113 unsigned char *outmsgtypep;
115 struct dhcp_vendor *vendor;
117 /* if not an encapsulated relayed message, just do the stuff */
118 if (msg_type != DHCP6RELAYFORW)
120 /* if link_address != NULL if points to the link address field of the
121 innermost nested RELAYFORW message, which is where we find the
122 address of the network on which we can allocate an address.
123 Recalculate the available contexts using that information.
125 link_address == NULL means there's no relay in use, so we try and find the client's
126 MAC address from the local ND cache. */
128 if (!state->link_address)
129 get_client_mac(client_addr, state->interface, state->mac, &state->mac_len, &state->mac_type, now);
132 struct dhcp_context *c;
133 struct shared_network *share = NULL;
134 state->context = NULL;
136 if (!IN6_IS_ADDR_LOOPBACK(state->link_address) &&
137 !IN6_IS_ADDR_LINKLOCAL(state->link_address) &&
138 !IN6_IS_ADDR_MULTICAST(state->link_address))
139 for (c = daemon->dhcp6; c; c = c->next)
141 for (share = daemon->shared_networks; share; share = share->next)
143 if (share->shared_addr.s_addr != 0)
146 if (share->if_index != 0 ||
147 !IN6_ARE_ADDR_EQUAL(state->link_address, &share->match_addr6))
150 if ((c->flags & CONTEXT_DHCP) &&
151 !(c->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) &&
152 is_same_net6(&share->shared_addr6, &c->start6, c->prefix) &&
153 is_same_net6(&share->shared_addr6, &c->end6, c->prefix))
158 ((c->flags & CONTEXT_DHCP) &&
159 !(c->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) &&
160 is_same_net6(state->link_address, &c->start6, c->prefix) &&
161 is_same_net6(state->link_address, &c->end6, c->prefix)))
163 c->preferred = c->valid = 0xffffffff;
164 c->current = state->context;
171 inet_ntop(AF_INET6, state->link_address, daemon->addrbuff, ADDRSTRLEN);
172 my_syslog(MS_DHCP | LOG_WARNING,
173 _("no address range available for DHCPv6 request from relay at %s"),
181 my_syslog(MS_DHCP | LOG_WARNING,
182 _("no address range available for DHCPv6 request via %s"), state->iface_name);
186 return dhcp6_no_relay(state, msg_type, inbuff, sz, is_unicast, now);
189 /* must have at least msg_type+hopcount+link_address+peer_address+minimal size option
190 which is 1 + 1 + 16 + 16 + 2 + 2 = 38 */
194 /* copy header stuff into reply message and set type to reply */
195 if (!(outmsgtypep = put_opt6(inbuff, 34)))
197 *outmsgtypep = DHCP6RELAYREPL;
199 /* look for relay options and set tags if found. */
200 for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next)
204 if (vendor->match_type == MATCH_SUBSCRIBER)
205 mopt = OPTION6_SUBSCRIBER_ID;
206 else if (vendor->match_type == MATCH_REMOTE)
207 mopt = OPTION6_REMOTE_ID;
211 if ((opt = opt6_find(opts, end, mopt, 1)) &&
212 vendor->len == opt6_len(opt) &&
213 memcmp(vendor->data, opt6_ptr(opt, 0), vendor->len) == 0 &&
214 vendor->netid.next != &vendor->netid)
216 vendor->netid.next = state->tags;
217 state->tags = &vendor->netid;
223 if ((opt = opt6_find(opts, end, OPTION6_CLIENT_MAC, 3)))
225 if (opt6_len(opt) - 2 > DHCP_CHADDR_MAX) {
228 state->mac_type = opt6_uint(opt, 0, 2);
229 state->mac_len = opt6_len(opt) - 2;
230 memcpy(&state->mac[0], opt6_ptr(opt, 2), state->mac_len);
233 for (opt = opts; opt; opt = opt6_next(opt, end))
235 if (opt6_ptr(opt, 0) + opt6_len(opt) > end)
238 /* Don't copy MAC address into reply. */
239 if (opt6_type(opt) != OPTION6_CLIENT_MAC)
241 int o = new_opt6(opt6_type(opt));
242 if (opt6_type(opt) == OPTION6_RELAY_MSG)
244 struct in6_addr align;
245 /* the packet data is unaligned, copy to aligned storage */
246 memcpy(&align, inbuff + 2, IN6ADDRSZ);
247 state->link_address = &align;
248 /* zero is_unicast since that is now known to refer to the
249 relayed packet, not the original sent by the client */
250 if (!dhcp6_maybe_relay(state, opt6_ptr(opt, 0), opt6_len(opt), client_addr, 0, now))
254 put_opt6(opt6_ptr(opt, 0), opt6_len(opt));
262 static int dhcp6_no_relay(struct state *state, int msg_type, unsigned char *inbuff, size_t sz, int is_unicast, time_t now)
265 int i, o, o1, start_opts, start_msg;
266 struct dhcp_opt *opt_cfg;
267 struct dhcp_netid *tagif;
268 struct dhcp_config *config = NULL;
269 struct dhcp_netid known_id, iface_id, v6_id;
270 unsigned char outmsgtype;
271 struct dhcp_vendor *vendor;
272 struct dhcp_context *context_tmp;
273 struct dhcp_mac *mac_opt;
274 unsigned int ignore = 0;
276 state->packet_options = inbuff + 4;
277 state->end = inbuff + sz;
280 state->lease_allocate = 0;
281 state->context_tags = NULL;
282 state->domain = NULL;
283 state->send_domain = NULL;
284 state->hostname_auth = 0;
285 state->hostname = NULL;
286 state->client_hostname = NULL;
287 state->fqdn_flags = 0x01; /* default to send if we receive no FQDN option */
289 /* set tag with name == interface */
290 iface_id.net = state->iface_name;
291 iface_id.next = state->tags;
292 state->tags = &iface_id;
294 /* set tag "dhcpv6" */
295 v6_id.net = "dhcpv6";
296 v6_id.next = state->tags;
297 state->tags = &v6_id;
299 start_msg = save_counter(-1);
300 /* copy over transaction-id */
301 if (!put_opt6(inbuff, 4))
303 start_opts = save_counter(-1);
304 state->xid = inbuff[3] | inbuff[2] << 8 | inbuff[1] << 16;
306 /* We're going to be linking tags from all context we use.
307 mark them as unused so we don't link one twice and break the list */
308 for (context_tmp = state->context; context_tmp; context_tmp = context_tmp->current)
310 context_tmp->netid.next = &context_tmp->netid;
312 if (option_bool(OPT_LOG_OPTS))
314 inet_ntop(AF_INET6, &context_tmp->start6, daemon->dhcp_buff, ADDRSTRLEN);
315 inet_ntop(AF_INET6, &context_tmp->end6, daemon->dhcp_buff2, ADDRSTRLEN);
316 if (context_tmp->flags & (CONTEXT_STATIC))
317 my_syslog(MS_DHCP | LOG_INFO, _("%u available DHCPv6 subnet: %s/%d"),
318 state->xid, daemon->dhcp_buff, context_tmp->prefix);
320 my_syslog(MS_DHCP | LOG_INFO, _("%u available DHCP range: %s -- %s"),
321 state->xid, daemon->dhcp_buff, daemon->dhcp_buff2);
325 if ((opt = opt6_find(state->packet_options, state->end, OPTION6_CLIENT_ID, 1)))
327 state->clid = opt6_ptr(opt, 0);
328 state->clid_len = opt6_len(opt);
329 o = new_opt6(OPTION6_CLIENT_ID);
330 put_opt6(state->clid, state->clid_len);
333 else if (msg_type != DHCP6IREQ)
336 /* server-id must match except for SOLICIT, CONFIRM and REBIND messages */
337 if (msg_type != DHCP6SOLICIT && msg_type != DHCP6CONFIRM && msg_type != DHCP6IREQ && msg_type != DHCP6REBIND &&
338 (!(opt = opt6_find(state->packet_options, state->end, OPTION6_SERVER_ID, 1)) ||
339 opt6_len(opt) != daemon->duid_len ||
340 memcmp(opt6_ptr(opt, 0), daemon->duid, daemon->duid_len) != 0))
343 o = new_opt6(OPTION6_SERVER_ID);
344 put_opt6(daemon->duid, daemon->duid_len);
348 (msg_type == DHCP6REQUEST || msg_type == DHCP6RENEW || msg_type == DHCP6RELEASE || msg_type == DHCP6DECLINE))
351 outmsgtype = DHCP6REPLY;
352 o1 = new_opt6(OPTION6_STATUS_CODE);
353 put_opt6_short(DHCP6USEMULTI);
354 put_opt6_string("Use multicast");
359 /* match vendor and user class options */
360 for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next)
364 if (vendor->match_type == MATCH_VENDOR)
365 mopt = OPTION6_VENDOR_CLASS;
366 else if (vendor->match_type == MATCH_USER)
367 mopt = OPTION6_USER_CLASS;
371 if ((opt = opt6_find(state->packet_options, state->end, mopt, 2)))
373 void *enc_opt, *enc_end = opt6_ptr(opt, opt6_len(opt));
376 if (mopt == OPTION6_VENDOR_CLASS)
378 if (opt6_len(opt) < 4)
381 if (vendor->enterprise != opt6_uint(opt, 0, 4))
387 /* Note that format if user/vendor classes is different to DHCP options - no option types. */
388 for (enc_opt = opt6_ptr(opt, offset); enc_opt; enc_opt = opt6_user_vendor_next(enc_opt, enc_end))
389 for (i = 0; i <= (opt6_user_vendor_len(enc_opt) - vendor->len); i++)
390 if (memcmp(vendor->data, opt6_user_vendor_ptr(enc_opt, i), vendor->len) == 0)
392 vendor->netid.next = state->tags;
393 state->tags = &vendor->netid;
399 if (option_bool(OPT_LOG_OPTS) && (opt = opt6_find(state->packet_options, state->end, OPTION6_VENDOR_CLASS, 4)))
400 my_syslog(MS_DHCP | LOG_INFO, _("%u vendor class: %u"), state->xid, opt6_uint(opt, 0, 4));
402 /* dhcp-match. If we have hex-and-wildcards, look for a left-anchored match.
403 Otherwise assume the option is an array, and look for a matching element.
404 If no data given, existence of the option is enough. This code handles
406 for (opt_cfg = daemon->dhcp_match6; opt_cfg; opt_cfg = opt_cfg->next)
410 if (opt_cfg->flags & DHOPT_RFC3925)
412 for (opt = opt6_find(state->packet_options, state->end, OPTION6_VENDOR_OPTS, 4);
414 opt = opt6_find(opt6_next(opt, state->end), state->end, OPTION6_VENDOR_OPTS, 4))
417 void *vend = opt6_ptr(opt, opt6_len(opt));
419 for (vopt = opt6_find(opt6_ptr(opt, 4), vend, opt_cfg->opt, 0);
421 vopt = opt6_find(opt6_next(vopt, vend), vend, opt_cfg->opt, 0))
422 if ((match = match_bytes(opt_cfg, opt6_ptr(vopt, 0), opt6_len(vopt))))
430 if (!(opt = opt6_find(state->packet_options, state->end, opt_cfg->opt, 1)))
433 match = match_bytes(opt_cfg, opt6_ptr(opt, 0), opt6_len(opt));
438 opt_cfg->netid->next = state->tags;
439 state->tags = opt_cfg->netid;
443 if (state->mac_len != 0)
445 if (option_bool(OPT_LOG_OPTS))
447 print_mac(daemon->dhcp_buff, state->mac, state->mac_len);
448 my_syslog(MS_DHCP | LOG_INFO, _("%u client MAC address: %s"), state->xid, daemon->dhcp_buff);
451 for (mac_opt = daemon->dhcp_macs; mac_opt; mac_opt = mac_opt->next)
452 if ((unsigned)mac_opt->hwaddr_len == state->mac_len &&
453 ((unsigned)mac_opt->hwaddr_type == state->mac_type || mac_opt->hwaddr_type == 0) &&
454 memcmp_masked(mac_opt->hwaddr, state->mac, state->mac_len, mac_opt->mask))
456 mac_opt->netid.next = state->tags;
457 state->tags = &mac_opt->netid;
461 if ((opt = opt6_find(state->packet_options, state->end, OPTION6_FQDN, 1)))
464 int len = opt6_len(opt) - 1;
466 state->fqdn_flags = opt6_uint(opt, 0, 1);
468 /* Always force update, since the client has no way to do it itself. */
469 if (!option_bool(OPT_FQDN_UPDATE) && !(state->fqdn_flags & 0x01))
470 state->fqdn_flags |= 0x03;
472 state->fqdn_flags &= ~0x04;
474 if (len != 0 && len < 255)
476 unsigned char *pp, *op = opt6_ptr(opt, 1);
477 char *pq = daemon->dhcp_buff;
480 while (*op != 0 && ((op + (*op)) - pp) < len)
482 memcpy(pq, op+1, *op);
488 if (pq != daemon->dhcp_buff)
492 if (legal_hostname(daemon->dhcp_buff))
494 struct dhcp_match_name *m;
495 size_t nl = strlen(daemon->dhcp_buff);
497 state->client_hostname = daemon->dhcp_buff;
499 if (option_bool(OPT_LOG_OPTS))
500 my_syslog(MS_DHCP | LOG_INFO, _("%u client provides name: %s"), state->xid, state->client_hostname);
502 for (m = daemon->dhcp_name_match; m; m = m->next)
504 size_t ml = strlen(m->name);
511 save = state->client_hostname[ml];
512 state->client_hostname[ml] = 0;
515 if (hostname_isequal(state->client_hostname, m->name) &&
516 (save == 0 || m->wildcard))
518 m->netid->next = state->tags;
519 state->tags = m->netid;
523 state->client_hostname[ml] = save;
530 (config = find_config(daemon->dhcp_conf, state->context, state->clid, state->clid_len,
531 state->mac, state->mac_len, state->mac_type, NULL, run_tag_if(state->tags))) &&
532 have_config(config, CONFIG_NAME))
534 state->hostname = config->hostname;
535 state->domain = config->domain;
536 state->hostname_auth = 1;
538 else if (state->client_hostname)
540 state->domain = strip_hostname(state->client_hostname);
542 if (strlen(state->client_hostname) != 0)
544 state->hostname = state->client_hostname;
548 /* Search again now we have a hostname.
549 Only accept configs without CLID here, (it won't match)
550 to avoid impersonation by name. */
551 struct dhcp_config *new = find_config(daemon->dhcp_conf, state->context, NULL, 0, NULL, 0, 0, state->hostname, run_tag_if(state->tags));
552 if (new && !have_config(new, CONFIG_CLID) && !new->hwaddr)
560 struct dhcp_netid_list *list;
562 for (list = config->netid; list; list = list->next)
564 list->list->next = state->tags;
565 state->tags = list->list;
568 /* set "known" tag for known hosts */
569 known_id.net = "known";
570 known_id.next = state->tags;
571 state->tags = &known_id;
573 if (have_config(config, CONFIG_DISABLE))
576 else if (state->clid &&
577 find_config(daemon->dhcp_conf, NULL, state->clid, state->clid_len,
578 state->mac, state->mac_len, state->mac_type, NULL, run_tag_if(state->tags)))
580 known_id.net = "known-othernet";
581 known_id.next = state->tags;
582 state->tags = &known_id;
585 tagif = run_tag_if(state->tags);
587 /* if all the netids in the ignore list are present, ignore this client */
588 if (daemon->dhcp_ignore)
590 struct dhcp_netid_list *id_list;
592 for (id_list = daemon->dhcp_ignore; id_list; id_list = id_list->next)
593 if (match_netid(id_list->list, tagif, 0))
597 /* if all the netids in the ignore_name list are present, ignore client-supplied name */
598 if (!state->hostname_auth)
600 struct dhcp_netid_list *id_list;
602 for (id_list = daemon->dhcp_ignore_names; id_list; id_list = id_list->next)
603 if ((!id_list->list) || match_netid(id_list->list, tagif, 0))
606 state->hostname = NULL;
618 int address_assigned = 0;
619 /* tags without all prefix-class tags */
620 struct dhcp_netid *solicit_tags;
621 struct dhcp_context *c;
623 outmsgtype = DHCP6ADVERTISE;
625 if (opt6_find(state->packet_options, state->end, OPTION6_RAPID_COMMIT, 0))
627 outmsgtype = DHCP6REPLY;
628 state->lease_allocate = 1;
629 o = new_opt6(OPTION6_RAPID_COMMIT);
633 log6_quiet(state, "DHCPSOLICIT", NULL, ignore ? _("ignored") : NULL);
636 solicit_tags = tagif;
641 /* reset USED bits in leases */
644 /* Can use configured address max once per prefix */
645 for (c = state->context; c; c = c->current)
646 c->flags &= ~CONTEXT_CONF_USED;
648 for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end))
650 void *ia_option, *ia_end;
651 unsigned int min_time = 0xffffffff;
654 /* set unless we're sending a particular prefix-class, when we
655 want only dhcp-ranges with the correct tags set and not those without any tags. */
658 struct dhcp_lease *ltmp;
659 struct in6_addr req_addr, addr;
661 if (!check_ia(state, opt, &ia_end, &ia_option))
664 /* reset USED bits in contexts - one address per prefix per IAID */
665 for (c = state->context; c; c = c->current)
666 c->flags &= ~CONTEXT_USED;
668 o = build_ia(state, &t1cntr);
669 if (address_assigned)
670 address_assigned = 2;
672 for (ia_counter = 0; ia_option; ia_counter++, ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24))
674 /* worry about alignment here. */
675 memcpy(&req_addr, opt6_ptr(ia_option, 0), IN6ADDRSZ);
677 if ((c = address6_valid(state->context, &req_addr, solicit_tags, plain_range)))
679 lease_time = c->lease_time;
680 /* If the client asks for an address on the same network as a configured address,
681 offer the configured address instead, to make moving to newly-configured
682 addresses automatic. */
683 if (!(c->flags & CONTEXT_CONF_USED) && config_valid(config, c, &addr, state, now))
686 mark_config_used(c, &addr);
687 if (have_config(config, CONFIG_TIME))
688 lease_time = config->lease_time;
690 else if (!(c = address6_available(state->context, &req_addr, solicit_tags, plain_range)))
691 continue; /* not an address we're allowed */
692 else if (!check_address(state, &req_addr))
693 continue; /* address leased elsewhere */
695 /* add address to output packet */
696 add_address(state, c, lease_time, ia_option, &min_time, &req_addr, now);
697 mark_context_used(state, &req_addr);
698 get_context_tag(state, c);
699 address_assigned = 1;
703 /* Suggest configured address(es) */
704 for (c = state->context; c; c = c->current)
705 if (!(c->flags & CONTEXT_CONF_USED) &&
706 match_netid(c->filter, solicit_tags, plain_range) &&
707 config_valid(config, c, &addr, state, now))
709 mark_config_used(state->context, &addr);
710 if (have_config(config, CONFIG_TIME))
711 lease_time = config->lease_time;
713 lease_time = c->lease_time;
715 /* add address to output packet */
716 add_address(state, c, lease_time, NULL, &min_time, &addr, now);
717 mark_context_used(state, &addr);
718 get_context_tag(state, c);
719 address_assigned = 1;
722 /* return addresses for existing leases */
724 while ((ltmp = lease6_find_by_client(ltmp, state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA, state->clid, state->clid_len, state->iaid)))
726 req_addr = ltmp->addr6;
727 if ((c = address6_available(state->context, &req_addr, solicit_tags, plain_range)))
729 add_address(state, c, c->lease_time, NULL, &min_time, &req_addr, now);
730 mark_context_used(state, &req_addr);
731 get_context_tag(state, c);
732 address_assigned = 1;
736 /* Return addresses for all valid contexts which don't yet have one */
737 while ((c = address6_allocate(state->context, state->clid, state->clid_len, state->ia_type == OPTION6_IA_TA,
738 state->iaid, ia_counter, solicit_tags, plain_range, &addr)))
740 add_address(state, c, c->lease_time, NULL, &min_time, &addr, now);
741 mark_context_used(state, &addr);
742 get_context_tag(state, c);
743 address_assigned = 1;
746 if (address_assigned != 1)
748 /* If the server will not assign any addresses to any IAs in a
749 subsequent Request from the client, the server MUST send an Advertise
750 message to the client that doesn't include any IA options. */
751 if (!state->lease_allocate)
757 /* If the server cannot assign any addresses to an IA in the message
758 from the client, the server MUST include the IA in the Reply message
759 with no addresses in the IA and a Status Code option in the IA
760 containing status code NoAddrsAvail. */
761 o1 = new_opt6(OPTION6_STATUS_CODE);
762 put_opt6_short(DHCP6NOADDRS);
763 put_opt6_string(_("address unavailable"));
767 end_ia(t1cntr, min_time, 0);
771 if (address_assigned)
773 o1 = new_opt6(OPTION6_STATUS_CODE);
774 put_opt6_short(DHCP6SUCCESS);
775 put_opt6_string(_("success"));
778 /* If --dhcp-authoritative is set, we can tell client not to wait for
779 other possible servers */
780 o = new_opt6(OPTION6_PREFERENCE);
781 put_opt6_char(option_bool(OPT_AUTHORITATIVE) ? 255 : 0);
783 tagif = add_options(state, 0);
787 /* no address, return error */
788 o1 = new_opt6(OPTION6_STATUS_CODE);
789 put_opt6_short(DHCP6NOADDRS);
790 put_opt6_string(_("no addresses available"));
793 /* Some clients will ask repeatedly when we're not giving
794 out addresses because we're in stateless mode. Avoid spamming
795 the log in that case. */
796 for (c = state->context; c; c = c->current)
797 if (!(c->flags & CONTEXT_RA_STATELESS))
799 log6_packet(state, state->lease_allocate ? "DHCPREPLY" : "DHCPADVERTISE", NULL, _("no addresses available"));
809 int address_assigned = 0;
810 int start = save_counter(-1);
812 /* set reply message type */
813 outmsgtype = DHCP6REPLY;
814 state->lease_allocate = 1;
816 log6_quiet(state, "DHCPREQUEST", NULL, ignore ? _("ignored") : NULL);
821 for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end))
823 void *ia_option, *ia_end;
824 unsigned int min_time = 0xffffffff;
827 if (!check_ia(state, opt, &ia_end, &ia_option))
832 /* If we get a request with an IA_*A without addresses, treat it exactly like
833 a SOLICT with rapid commit set. */
835 goto request_no_address;
838 o = build_ia(state, &t1cntr);
840 for (; ia_option; ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24))
842 struct in6_addr req_addr;
843 struct dhcp_context *dynamic, *c;
844 unsigned int lease_time;
848 memcpy(&req_addr, opt6_ptr(ia_option, 0), IN6ADDRSZ);
850 if ((c = address6_valid(state->context, &req_addr, tagif, 1)))
851 config_ok = (config_implies(config, c, &req_addr) != NULL);
853 if ((dynamic = address6_available(state->context, &req_addr, tagif, 1)) || c)
855 if (!dynamic && !config_ok)
857 /* Static range, not configured. */
858 o1 = new_opt6(OPTION6_STATUS_CODE);
859 put_opt6_short(DHCP6NOADDRS);
860 put_opt6_string(_("address unavailable"));
863 else if (!check_address(state, &req_addr))
865 /* Address leased to another DUID/IAID */
866 o1 = new_opt6(OPTION6_STATUS_CODE);
867 put_opt6_short(DHCP6UNSPEC);
868 put_opt6_string(_("address in use"));
876 lease_time = dynamic->lease_time;
878 if (config_ok && have_config(config, CONFIG_TIME))
879 lease_time = config->lease_time;
881 add_address(state, dynamic, lease_time, ia_option, &min_time, &req_addr, now);
882 get_context_tag(state, dynamic);
883 address_assigned = 1;
888 /* requested address not on the correct link */
889 o1 = new_opt6(OPTION6_STATUS_CODE);
890 put_opt6_short(DHCP6NOTONLINK);
891 put_opt6_string(_("not on link"));
896 end_ia(t1cntr, min_time, 0);
900 if (address_assigned)
902 o1 = new_opt6(OPTION6_STATUS_CODE);
903 put_opt6_short(DHCP6SUCCESS);
904 put_opt6_string(_("success"));
909 /* no address, return error */
910 o1 = new_opt6(OPTION6_STATUS_CODE);
911 put_opt6_short(DHCP6NOADDRS);
912 put_opt6_string(_("no addresses available"));
914 log6_packet(state, "DHCPREPLY", NULL, _("no addresses available"));
917 tagif = add_options(state, 0);
925 int address_assigned = 0;
927 /* set reply message type */
928 outmsgtype = DHCP6REPLY;
930 log6_quiet(state, msg_type == DHCP6RENEW ? "DHCPRENEW" : "DHCPREBIND", NULL, NULL);
932 for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end))
934 void *ia_option, *ia_end;
935 unsigned int min_time = 0xffffffff;
938 if (!check_ia(state, opt, &ia_end, &ia_option))
941 o = build_ia(state, &t1cntr);
942 iacntr = save_counter(-1);
944 for (; ia_option; ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24))
946 struct dhcp_lease *lease = NULL;
947 struct in6_addr req_addr;
948 unsigned int preferred_time = opt6_uint(ia_option, 16, 4);
949 unsigned int valid_time = opt6_uint(ia_option, 20, 4);
950 char *message = NULL;
951 struct dhcp_context *this_context;
953 memcpy(&req_addr, opt6_ptr(ia_option, 0), IN6ADDRSZ);
955 if (!(lease = lease6_find(state->clid, state->clid_len,
956 state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA,
957 state->iaid, &req_addr)))
959 if (msg_type == DHCP6REBIND)
961 /* When rebinding, we can create a lease if it doesn't exist. */
962 lease = lease6_allocate(&req_addr, state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA);
964 lease_set_iaid(lease, state->iaid);
970 /* If the server cannot find a client entry for the IA the server
971 returns the IA containing no addresses with a Status Code option set
972 to NoBinding in the Reply message. */
973 save_counter(iacntr);
976 log6_packet(state, "DHCPREPLY", &req_addr, _("lease not found"));
978 o1 = new_opt6(OPTION6_STATUS_CODE);
979 put_opt6_short(DHCP6NOBINDING);
980 put_opt6_string(_("no binding found"));
983 preferred_time = valid_time = 0;
988 if ((this_context = address6_available(state->context, &req_addr, tagif, 1)) ||
989 (this_context = address6_valid(state->context, &req_addr, tagif, 1)))
991 unsigned int lease_time;
993 get_context_tag(state, this_context);
995 if (config_implies(config, this_context, &req_addr) && have_config(config, CONFIG_TIME))
996 lease_time = config->lease_time;
998 lease_time = this_context->lease_time;
1000 calculate_times(this_context, &min_time, &valid_time, &preferred_time, lease_time);
1002 lease_set_expires(lease, valid_time, now);
1003 /* Update MAC record in case it's new information. */
1004 if (state->mac_len != 0)
1005 lease_set_hwaddr(lease, state->mac, state->clid, state->mac_len, state->mac_type, state->clid_len, now, 0);
1006 if (state->ia_type == OPTION6_IA_NA && state->hostname)
1008 char *addr_domain = get_domain6(&req_addr);
1009 if (!state->send_domain)
1010 state->send_domain = addr_domain;
1011 lease_set_hostname(lease, state->hostname, state->hostname_auth, addr_domain, state->domain);
1012 message = state->hostname;
1016 if (preferred_time == 0)
1017 message = _("deprecated");
1019 address_assigned = 1;
1023 preferred_time = valid_time = 0;
1024 message = _("address invalid");
1027 if (message && (message != state->hostname))
1028 log6_packet(state, "DHCPREPLY", &req_addr, message);
1030 log6_quiet(state, "DHCPREPLY", &req_addr, message);
1032 o1 = new_opt6(OPTION6_IAADDR);
1033 put_opt6(&req_addr, sizeof(req_addr));
1034 put_opt6_long(preferred_time);
1035 put_opt6_long(valid_time);
1039 end_ia(t1cntr, min_time, 1);
1043 if (!address_assigned && msg_type == DHCP6REBIND)
1045 /* can't create lease for any address, return error */
1046 o1 = new_opt6(OPTION6_STATUS_CODE);
1047 put_opt6_short(DHCP6NOADDRS);
1048 put_opt6_string(_("no addresses available"));
1052 tagif = add_options(state, 0);
1060 /* set reply message type */
1061 outmsgtype = DHCP6REPLY;
1063 log6_quiet(state, "DHCPCONFIRM", NULL, NULL);
1065 for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end))
1067 void *ia_option, *ia_end;
1069 for (check_ia(state, opt, &ia_end, &ia_option);
1071 ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24))
1073 struct in6_addr req_addr;
1076 memcpy(&req_addr, opt6_ptr(ia_option, 0), IN6ADDRSZ);
1078 if (!address6_valid(state->context, &req_addr, tagif, 1))
1080 o1 = new_opt6(OPTION6_STATUS_CODE);
1081 put_opt6_short(DHCP6NOTONLINK);
1082 put_opt6_string(_("confirm failed"));
1084 log6_quiet(state, "DHCPREPLY", &req_addr, _("confirm failed"));
1089 log6_quiet(state, "DHCPREPLY", &req_addr, state->hostname);
1093 /* No addresses, no reply: RFC 3315 18.2.2 */
1097 o1 = new_opt6(OPTION6_STATUS_CODE);
1098 put_opt6_short(DHCP6SUCCESS );
1099 put_opt6_string(_("all addresses still on link"));
1106 /* We can't discriminate contexts based on address, as we don't know it.
1107 If there is only one possible context, we can use its tags */
1108 if (state->context && state->context->netid.net && !state->context->current)
1110 state->context->netid.next = NULL;
1111 state->context_tags = &state->context->netid;
1114 /* Similarly, we can't determine domain from address, but if the FQDN is
1115 given in --dhcp-host, we can use that, and failing that we can use the
1116 unqualified configured domain, if any. */
1117 if (state->hostname_auth)
1118 state->send_domain = state->domain;
1120 state->send_domain = get_domain6(NULL);
1122 log6_quiet(state, "DHCPINFORMATION-REQUEST", NULL, ignore ? _("ignored") : state->hostname);
1125 outmsgtype = DHCP6REPLY;
1126 tagif = add_options(state, 1);
1133 /* set reply message type */
1134 outmsgtype = DHCP6REPLY;
1136 log6_quiet(state, "DHCPRELEASE", NULL, NULL);
1138 for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end))
1140 void *ia_option, *ia_end;
1143 for (check_ia(state, opt, &ia_end, &ia_option);
1145 ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24))
1147 struct dhcp_lease *lease;
1148 struct in6_addr addr;
1151 memcpy(&addr, opt6_ptr(ia_option, 0), IN6ADDRSZ);
1152 if ((lease = lease6_find(state->clid, state->clid_len, state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA,
1153 state->iaid, &addr)))
1154 lease_prune(lease, now);
1159 o = new_opt6(state->ia_type);
1160 put_opt6_long(state->iaid);
1161 if (state->ia_type == OPTION6_IA_NA)
1169 o1 = new_opt6(OPTION6_IAADDR);
1170 put_opt6(&addr, IN6ADDRSZ);
1179 o1 = new_opt6(OPTION6_STATUS_CODE);
1180 put_opt6_short(DHCP6NOBINDING);
1181 put_opt6_string(_("no binding found"));
1188 o1 = new_opt6(OPTION6_STATUS_CODE);
1189 put_opt6_short(DHCP6SUCCESS);
1190 put_opt6_string(_("release received"));
1198 /* set reply message type */
1199 outmsgtype = DHCP6REPLY;
1201 log6_quiet(state, "DHCPDECLINE", NULL, NULL);
1203 for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end))
1205 void *ia_option, *ia_end;
1208 for (check_ia(state, opt, &ia_end, &ia_option);
1210 ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24))
1212 struct dhcp_lease *lease;
1213 struct in6_addr addr;
1214 struct addrlist *addr_list;
1217 memcpy(&addr, opt6_ptr(ia_option, 0), IN6ADDRSZ);
1219 if ((addr_list = config_implies(config, state->context, &addr)))
1221 prettyprint_time(daemon->dhcp_buff3, DECLINE_BACKOFF);
1222 inet_ntop(AF_INET6, &addr, daemon->addrbuff, ADDRSTRLEN);
1223 my_syslog(MS_DHCP | LOG_WARNING, _("disabling DHCP static address %s for %s"),
1224 daemon->addrbuff, daemon->dhcp_buff3);
1225 addr_list->flags |= ADDRLIST_DECLINED;
1226 addr_list->decline_time = now;
1229 /* make sure this host gets a different address next time. */
1230 for (context_tmp = state->context; context_tmp; context_tmp = context_tmp->current)
1231 context_tmp->addr_epoch++;
1233 if ((lease = lease6_find(state->clid, state->clid_len, state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA,
1234 state->iaid, &addr)))
1235 lease_prune(lease, now);
1240 o = new_opt6(state->ia_type);
1241 put_opt6_long(state->iaid);
1242 if (state->ia_type == OPTION6_IA_NA)
1250 o1 = new_opt6(OPTION6_IAADDR);
1251 put_opt6(&addr, IN6ADDRSZ);
1260 o1 = new_opt6(OPTION6_STATUS_CODE);
1261 put_opt6_short(DHCP6NOBINDING);
1262 put_opt6_string(_("no binding found"));
1270 /* We must answer with 'success' in global section anyway */
1271 o1 = new_opt6(OPTION6_STATUS_CODE);
1272 put_opt6_short(DHCP6SUCCESS);
1273 put_opt6_string(_("success"));
1280 /* Fill in the message type. Note that we store the offset,
1281 not a direct pointer, since the packet memory may have been
1283 ((unsigned char *)(daemon->outpacket.iov_base))[start_msg] = outmsgtype;
1285 log_tags(tagif, state->xid);
1286 log6_opts(0, state->xid, daemon->outpacket.iov_base + start_opts, daemon->outpacket.iov_base + save_counter(-1));
1292 static struct dhcp_netid *add_options(struct state *state, int do_refresh)
1295 /* filter options based on tags, those we want get DHOPT_TAGOK bit set */
1296 struct dhcp_netid *tagif = option_filter(state->tags, state->context_tags, daemon->dhcp_opts6);
1297 struct dhcp_opt *opt_cfg;
1298 int done_dns = 0, done_refresh = !do_refresh, do_encap = 0;
1301 oro = opt6_find(state->packet_options, state->end, OPTION6_ORO, 0);
1303 for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next)
1305 /* netids match and not encapsulated? */
1306 if (!(opt_cfg->flags & DHOPT_TAGOK))
1309 if (!(opt_cfg->flags & DHOPT_FORCE) && oro)
1311 for (i = 0; i < opt6_len(oro) - 1; i += 2)
1312 if (opt6_uint(oro, i, 2) == (unsigned)opt_cfg->opt)
1315 /* option not requested */
1316 if (i >= opt6_len(oro) - 1)
1320 if (opt_cfg->opt == OPTION6_REFRESH_TIME)
1323 if (opt_cfg->opt == OPTION6_DNS_SERVER)
1326 if (opt_cfg->flags & DHOPT_ADDR6)
1331 for (a = (struct in6_addr *)opt_cfg->val, len = opt_cfg->len, j = 0;
1332 j < opt_cfg->len; j += IN6ADDRSZ, a++)
1333 if ((IN6_IS_ADDR_ULA_ZERO(a) && IN6_IS_ADDR_UNSPECIFIED(state->ula_addr)) ||
1334 (IN6_IS_ADDR_LINK_LOCAL_ZERO(a) && IN6_IS_ADDR_UNSPECIFIED(state->ll_addr)))
1340 o = new_opt6(opt_cfg->opt);
1342 for (a = (struct in6_addr *)opt_cfg->val, j = 0; j < opt_cfg->len; j+=IN6ADDRSZ, a++)
1344 struct in6_addr *p = NULL;
1346 if (IN6_IS_ADDR_UNSPECIFIED(a))
1348 if (!add_local_addrs(state->context))
1349 p = state->fallback;
1351 else if (IN6_IS_ADDR_ULA_ZERO(a))
1353 if (!IN6_IS_ADDR_UNSPECIFIED(state->ula_addr))
1354 p = state->ula_addr;
1356 else if (IN6_IS_ADDR_LINK_LOCAL_ZERO(a))
1358 if (!IN6_IS_ADDR_UNSPECIFIED(state->ll_addr))
1366 else if (opt_cfg->opt == OPTION6_NTP_SERVER)
1368 if (IN6_IS_ADDR_MULTICAST(p))
1369 o1 = new_opt6(NTP_SUBOPTION_MC_ADDR);
1371 o1 = new_opt6(NTP_SUBOPTION_SRV_ADDR);
1372 put_opt6(p, IN6ADDRSZ);
1376 put_opt6(p, IN6ADDRSZ);
1384 o = new_opt6(opt_cfg->opt);
1386 put_opt6(opt_cfg->val, opt_cfg->len);
1391 if (daemon->port == NAMESERVER_PORT && !done_dns)
1393 o = new_opt6(OPTION6_DNS_SERVER);
1394 if (!add_local_addrs(state->context))
1395 put_opt6(state->fallback, IN6ADDRSZ);
1399 if (state->context && !done_refresh)
1401 struct dhcp_context *c;
1402 unsigned int lease_time = 0xffffffff;
1404 /* Find the smallest lease tie of all contexts,
1405 subject to the RFC-4242 stipulation that this must not
1406 be less than 600. */
1407 for (c = state->context; c; c = c->next)
1408 if (c->lease_time < lease_time)
1410 if (c->lease_time < 600)
1413 lease_time = c->lease_time;
1416 o = new_opt6(OPTION6_REFRESH_TIME);
1417 put_opt6_long(lease_time);
1421 /* handle vendor-identifying vendor-encapsulated options,
1422 dhcp-option = vi-encap:13,17,....... */
1423 for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next)
1424 opt_cfg->flags &= ~DHOPT_ENCAP_DONE;
1427 for (i = 0; i < opt6_len(oro) - 1; i += 2)
1428 if (opt6_uint(oro, i, 2) == OPTION6_VENDOR_OPTS)
1431 for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next)
1433 if (opt_cfg->flags & DHOPT_RFC3925)
1436 struct dhcp_opt *oc;
1438 if (opt_cfg->flags & DHOPT_ENCAP_DONE)
1441 for (oc = daemon->dhcp_opts6; oc; oc = oc->next)
1443 oc->flags &= ~DHOPT_ENCAP_MATCH;
1445 if (!(oc->flags & DHOPT_RFC3925) || opt_cfg->u.encap != oc->u.encap)
1448 oc->flags |= DHOPT_ENCAP_DONE;
1449 if (match_netid(oc->netid, tagif, 1))
1451 /* option requested/forced? */
1452 if (!oro || do_encap || (oc->flags & DHOPT_FORCE))
1454 oc->flags |= DHOPT_ENCAP_MATCH;
1462 o = new_opt6(OPTION6_VENDOR_OPTS);
1463 put_opt6_long(opt_cfg->u.encap);
1465 for (oc = daemon->dhcp_opts6; oc; oc = oc->next)
1466 if (oc->flags & DHOPT_ENCAP_MATCH)
1468 o1 = new_opt6(oc->opt);
1469 put_opt6(oc->val, oc->len);
1478 if (state->hostname)
1481 size_t len = strlen(state->hostname);
1483 if (state->send_domain)
1484 len += strlen(state->send_domain) + 2;
1486 o = new_opt6(OPTION6_FQDN);
1487 if ((p = expand(len + 2)))
1489 *(p++) = state->fqdn_flags;
1490 p = do_rfc1035_name(p, state->hostname, NULL);
1491 if (state->send_domain)
1493 p = do_rfc1035_name(p, state->send_domain, NULL);
1502 if (option_bool(OPT_LOG_OPTS) && oro)
1504 char *q = daemon->namebuff;
1505 for (i = 0; i < opt6_len(oro) - 1; i += 2)
1507 char *s = option_string(AF_INET6, opt6_uint(oro, i, 2), NULL, 0, NULL, 0);
1508 q += snprintf(q, MAXDNAME - (q - daemon->namebuff),
1510 opt6_uint(oro, i, 2),
1511 strlen(s) != 0 ? ":" : "",
1513 (i > opt6_len(oro) - 3) ? "" : ", ");
1514 if ( i > opt6_len(oro) - 3 || (q - daemon->namebuff) > 40)
1516 q = daemon->namebuff;
1517 my_syslog(MS_DHCP | LOG_INFO, _("%u requested options: %s"), state->xid, daemon->namebuff);
1525 static int add_local_addrs(struct dhcp_context *context)
1529 for (; context; context = context->current)
1530 if ((context->flags & CONTEXT_USED) && !IN6_IS_ADDR_UNSPECIFIED(&context->local6))
1532 /* squash duplicates */
1533 struct dhcp_context *c;
1534 for (c = context->current; c; c = c->current)
1535 if ((c->flags & CONTEXT_USED) &&
1536 IN6_ARE_ADDR_EQUAL(&context->local6, &c->local6))
1542 put_opt6(&context->local6, IN6ADDRSZ);
1550 static void get_context_tag(struct state *state, struct dhcp_context *context)
1552 /* get tags from context if we've not used it before */
1553 if (context->netid.next == &context->netid && context->netid.net)
1555 context->netid.next = state->context_tags;
1556 state->context_tags = &context->netid;
1557 if (!state->hostname_auth)
1559 struct dhcp_netid_list *id_list;
1561 for (id_list = daemon->dhcp_ignore_names; id_list; id_list = id_list->next)
1562 if ((!id_list->list) || match_netid(id_list->list, &context->netid, 0))
1565 state->hostname = NULL;
1570 static int check_ia(struct state *state, void *opt, void **endp, void **ia_option)
1572 state->ia_type = opt6_type(opt);
1575 if (state->ia_type != OPTION6_IA_NA && state->ia_type != OPTION6_IA_TA)
1578 if (state->ia_type == OPTION6_IA_NA && opt6_len(opt) < 12)
1581 if (state->ia_type == OPTION6_IA_TA && opt6_len(opt) < 4)
1584 *endp = opt6_ptr(opt, opt6_len(opt));
1585 state->iaid = opt6_uint(opt, 0, 4);
1586 *ia_option = opt6_find(opt6_ptr(opt, state->ia_type == OPTION6_IA_NA ? 12 : 4), *endp, OPTION6_IAADDR, 24);
1592 static int build_ia(struct state *state, int *t1cntr)
1594 int o = new_opt6(state->ia_type);
1596 put_opt6_long(state->iaid);
1599 if (state->ia_type == OPTION6_IA_NA)
1602 *t1cntr = save_counter(-1);
1603 /* so we can fill these in later */
1611 static void end_ia(int t1cntr, unsigned int min_time, int do_fuzz)
1615 /* go back and fill in fields in IA_NA option */
1616 int sav = save_counter(t1cntr);
1617 unsigned int t1, t2, fuzz = 0;
1623 while (fuzz > (min_time/16))
1627 t1 = (min_time == 0xffffffff) ? 0xffffffff : min_time/2 - fuzz;
1628 t2 = (min_time == 0xffffffff) ? 0xffffffff : ((min_time/8)*7) - fuzz;
1635 static void add_address(struct state *state, struct dhcp_context *context, unsigned int lease_time, void *ia_option,
1636 unsigned int *min_time, struct in6_addr *addr, time_t now)
1638 unsigned int valid_time = 0, preferred_time = 0;
1639 int o = new_opt6(OPTION6_IAADDR);
1640 struct dhcp_lease *lease;
1642 /* get client requested times */
1645 preferred_time = opt6_uint(ia_option, 16, 4);
1646 valid_time = opt6_uint(ia_option, 20, 4);
1649 calculate_times(context, min_time, &valid_time, &preferred_time, lease_time);
1651 put_opt6(addr, sizeof(*addr));
1652 put_opt6_long(preferred_time);
1653 put_opt6_long(valid_time);
1656 if (state->lease_allocate)
1657 update_leases(state, context, addr, valid_time, now);
1659 if ((lease = lease6_find_by_addr(addr, 128, 0)))
1660 lease->flags |= LEASE_USED;
1662 /* get tags from context if we've not used it before */
1663 if (context->netid.next == &context->netid && context->netid.net)
1665 context->netid.next = state->context_tags;
1666 state->context_tags = &context->netid;
1668 if (!state->hostname_auth)
1670 struct dhcp_netid_list *id_list;
1672 for (id_list = daemon->dhcp_ignore_names; id_list; id_list = id_list->next)
1673 if ((!id_list->list) || match_netid(id_list->list, &context->netid, 0))
1676 state->hostname = NULL;
1680 log6_quiet(state, state->lease_allocate ? "DHCPREPLY" : "DHCPADVERTISE", addr, state->hostname);
1684 static void mark_context_used(struct state *state, struct in6_addr *addr)
1686 struct dhcp_context *context;
1688 /* Mark that we have an address for this prefix. */
1689 for (context = state->context; context; context = context->current)
1690 if (is_same_net6(addr, &context->start6, context->prefix))
1691 context->flags |= CONTEXT_USED;
1694 static void mark_config_used(struct dhcp_context *context, struct in6_addr *addr)
1696 for (; context; context = context->current)
1697 if (is_same_net6(addr, &context->start6, context->prefix))
1698 context->flags |= CONTEXT_CONF_USED;
1701 /* make sure address not leased to another CLID/IAID */
1702 static int check_address(struct state *state, struct in6_addr *addr)
1704 struct dhcp_lease *lease;
1706 if (!(lease = lease6_find_by_addr(addr, 128, 0)))
1709 if (lease->clid_len != state->clid_len ||
1710 memcmp(lease->clid, state->clid, state->clid_len) != 0 ||
1711 lease->iaid != state->iaid)
1718 /* return true of *addr could have been generated from config. */
1719 static struct addrlist *config_implies(struct dhcp_config *config, struct dhcp_context *context, struct in6_addr *addr)
1722 struct in6_addr wild_addr;
1723 struct addrlist *addr_list;
1725 if (!config || !(config->flags & CONFIG_ADDR6))
1728 for (addr_list = config->addr6; addr_list; addr_list = addr_list->next)
1730 prefix = (addr_list->flags & ADDRLIST_PREFIX) ? addr_list->prefixlen : 128;
1731 wild_addr = addr_list->addr.addr6;
1733 if ((addr_list->flags & ADDRLIST_WILDCARD) && context->prefix == 64)
1735 wild_addr = context->start6;
1736 setaddr6part(&wild_addr, addr6part(&addr_list->addr.addr6));
1738 else if (!is_same_net6(&context->start6, addr, context->prefix))
1741 if (is_same_net6(&wild_addr, addr, prefix))
1748 static int config_valid(struct dhcp_config *config, struct dhcp_context *context, struct in6_addr *addr, struct state *state, time_t now)
1750 u64 addrpart, i, addresses;
1751 struct addrlist *addr_list;
1753 if (!config || !(config->flags & CONFIG_ADDR6))
1756 for (addr_list = config->addr6; addr_list; addr_list = addr_list->next)
1757 if (!(addr_list->flags & ADDRLIST_DECLINED) ||
1758 difftime(now, addr_list->decline_time) >= (float)DECLINE_BACKOFF)
1760 addrpart = addr6part(&addr_list->addr.addr6);
1763 if (addr_list->flags & ADDRLIST_PREFIX)
1764 addresses = (u64)1<<(128-addr_list->prefixlen);
1766 if ((addr_list->flags & ADDRLIST_WILDCARD))
1768 if (context->prefix != 64)
1771 *addr = context->start6;
1773 else if (is_same_net6(&context->start6, &addr_list->addr.addr6, context->prefix))
1774 *addr = addr_list->addr.addr6;
1778 for (i = 0 ; i < addresses; i++)
1780 setaddr6part(addr, addrpart+i);
1782 if (check_address(state, addr))
1790 /* Calculate valid and preferred times to send in leases/renewals.
1794 *valid_timep, *preferred_timep - requested times from IAADDR options.
1795 context->valid, context->preferred - times associated with subnet address on local interface.
1796 context->flags | CONTEXT_DEPRECATE - "deprecated" flag in dhcp-range.
1797 lease_time - configured time for context for individual client.
1798 *min_time - smallest valid time sent so far.
1802 *valid_timep, *preferred_timep - times to be send in IAADDR option.
1803 *min_time - smallest valid time sent so far, to calculate T1 and T2.
1806 static void calculate_times(struct dhcp_context *context, unsigned int *min_time, unsigned int *valid_timep,
1807 unsigned int *preferred_timep, unsigned int lease_time)
1809 unsigned int req_preferred = *preferred_timep, req_valid = *valid_timep;
1810 unsigned int valid_time = lease_time, preferred_time = lease_time;
1812 /* RFC 3315: "A server ignores the lifetimes set
1813 by the client if the preferred lifetime is greater than the valid
1815 if (req_preferred <= req_valid)
1817 if (req_preferred != 0)
1819 /* 0 == "no preference from client" */
1820 if (req_preferred < 120u)
1821 req_preferred = 120u; /* sanity */
1823 if (req_preferred < preferred_time)
1824 preferred_time = req_preferred;
1828 /* 0 == "no preference from client" */
1830 if (req_valid < 120u)
1831 req_valid = 120u; /* sanity */
1833 if (req_valid < valid_time)
1834 valid_time = req_valid;
1838 /* deprecate (preferred == 0) which configured, or when local address
1840 if ((context->flags & CONTEXT_DEPRECATE) || context->preferred == 0)
1843 if (preferred_time != 0 && preferred_time < *min_time)
1844 *min_time = preferred_time;
1846 if (valid_time != 0 && valid_time < *min_time)
1847 *min_time = valid_time;
1849 *valid_timep = valid_time;
1850 *preferred_timep = preferred_time;
1853 static void update_leases(struct state *state, struct dhcp_context *context, struct in6_addr *addr, unsigned int lease_time, time_t now)
1855 struct dhcp_lease *lease = lease6_find_by_addr(addr, 128, 0);
1857 struct dhcp_netid *tagif = run_tag_if(state->tags);
1863 lease = lease6_allocate(addr, state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA);
1867 lease_set_expires(lease, lease_time, now);
1868 lease_set_iaid(lease, state->iaid);
1869 lease_set_hwaddr(lease, state->mac, state->clid, state->mac_len, state->mac_type, state->clid_len, now, 0);
1870 lease_set_interface(lease, state->interface, now);
1871 if (state->hostname && state->ia_type == OPTION6_IA_NA)
1873 char *addr_domain = get_domain6(addr);
1874 if (!state->send_domain)
1875 state->send_domain = addr_domain;
1876 lease_set_hostname(lease, state->hostname, state->hostname_auth, addr_domain, state->domain);
1880 if (daemon->lease_change_command)
1884 lease->flags |= LEASE_CHANGED;
1885 free(lease->extradata);
1886 lease->extradata = NULL;
1887 lease->extradata_size = lease->extradata_len = 0;
1888 lease->vendorclass_count = 0;
1890 if ((opt = opt6_find(state->packet_options, state->end, OPTION6_VENDOR_CLASS, 4)))
1892 void *enc_opt, *enc_end = opt6_ptr(opt, opt6_len(opt));
1893 lease->vendorclass_count++;
1894 /* send enterprise number first */
1895 sprintf(daemon->dhcp_buff2, "%u", opt6_uint(opt, 0, 4));
1896 lease_add_extradata(lease, (unsigned char *)daemon->dhcp_buff2, strlen(daemon->dhcp_buff2), 0);
1898 if (opt6_len(opt) >= 6)
1899 for (enc_opt = opt6_ptr(opt, 4); enc_opt; enc_opt = opt6_next(enc_opt, enc_end))
1901 lease->vendorclass_count++;
1902 lease_add_extradata(lease, opt6_ptr(enc_opt, 0), opt6_len(enc_opt), 0);
1906 lease_add_extradata(lease, (unsigned char *)state->client_hostname,
1907 state->client_hostname ? strlen(state->client_hostname) : 0, 0);
1909 /* DNSMASQ_REQUESTED_OPTIONS */
1910 if ((opt = opt6_find(state->packet_options, state->end, OPTION6_ORO, 2)))
1912 int i, len = opt6_len(opt)/2;
1913 u16 *rop = opt6_ptr(opt, 0);
1915 for (i = 0; i < len; i++)
1916 lease_add_extradata(lease, (unsigned char *)daemon->namebuff,
1917 sprintf(daemon->namebuff, "%u", ntohs(rop[i])), (i + 1) == len ? 0 : ',');
1920 lease_add_extradata(lease, NULL, 0, 0);
1922 if ((opt = opt6_find(state->packet_options, state->end, OPTION6_MUD_URL, 1)))
1923 lease_add_extradata(lease, opt6_ptr(opt, 0), opt6_len(opt), 0);
1925 lease_add_extradata(lease, NULL, 0, 0);
1927 /* space-concat tag set */
1928 if (!tagif && !context->netid.net)
1929 lease_add_extradata(lease, NULL, 0, 0);
1932 if (context->netid.net)
1933 lease_add_extradata(lease, (unsigned char *)context->netid.net, strlen(context->netid.net), tagif ? ' ' : 0);
1937 struct dhcp_netid *n;
1938 for (n = tagif; n; n = n->next)
1940 struct dhcp_netid *n1;
1942 for (n1 = n->next; n1; n1 = n1->next)
1943 if (strcmp(n->net, n1->net) == 0)
1946 lease_add_extradata(lease, (unsigned char *)n->net, strlen(n->net), n->next ? ' ' : 0);
1951 if (state->link_address)
1952 inet_ntop(AF_INET6, state->link_address, daemon->addrbuff, ADDRSTRLEN);
1954 lease_add_extradata(lease, (unsigned char *)daemon->addrbuff, state->link_address ? strlen(daemon->addrbuff) : 0, 0);
1956 if ((opt = opt6_find(state->packet_options, state->end, OPTION6_USER_CLASS, 2)))
1958 void *enc_opt, *enc_end = opt6_ptr(opt, opt6_len(opt));
1959 for (enc_opt = opt6_ptr(opt, 0); enc_opt; enc_opt = opt6_next(enc_opt, enc_end))
1960 lease_add_extradata(lease, opt6_ptr(enc_opt, 0), opt6_len(enc_opt), 0);
1970 static void log6_opts(int nest, unsigned int xid, void *start_opts, void *end_opts)
1973 char *desc = nest ? "nest" : "sent";
1975 if (!option_bool(OPT_LOG_OPTS) || start_opts == end_opts)
1978 for (opt = start_opts; opt; opt = opt6_next(opt, end_opts))
1980 int type = opt6_type(opt);
1981 void *ia_options = NULL;
1984 if (type == OPTION6_IA_NA)
1986 sprintf(daemon->namebuff, "IAID=%u T1=%u T2=%u",
1987 opt6_uint(opt, 0, 4), opt6_uint(opt, 4, 4), opt6_uint(opt, 8, 4));
1989 ia_options = opt6_ptr(opt, 12);
1991 else if (type == OPTION6_IA_TA)
1993 sprintf(daemon->namebuff, "IAID=%u", opt6_uint(opt, 0, 4));
1995 ia_options = opt6_ptr(opt, 4);
1997 else if (type == OPTION6_IAADDR)
1999 struct in6_addr addr;
2002 memcpy(&addr, opt6_ptr(opt, 0), IN6ADDRSZ);
2003 inet_ntop(AF_INET6, &addr, daemon->addrbuff, ADDRSTRLEN);
2004 sprintf(daemon->namebuff, "%s PL=%u VL=%u",
2005 daemon->addrbuff, opt6_uint(opt, 16, 4), opt6_uint(opt, 20, 4));
2007 ia_options = opt6_ptr(opt, 24);
2009 else if (type == OPTION6_STATUS_CODE)
2011 int len = sprintf(daemon->namebuff, "%u ", opt6_uint(opt, 0, 2));
2012 memcpy(daemon->namebuff + len, opt6_ptr(opt, 2), opt6_len(opt)-2);
2013 daemon->namebuff[len + opt6_len(opt) - 2] = 0;
2018 /* account for flag byte on FQDN */
2019 int offset = type == OPTION6_FQDN ? 1 : 0;
2020 optname = option_string(AF_INET6, type, opt6_ptr(opt, offset), opt6_len(opt) - offset, daemon->namebuff, MAXDNAME);
2023 my_syslog(MS_DHCP | LOG_INFO, "%u %s size:%3d option:%3d %s %s",
2024 xid, desc, opt6_len(opt), type, optname, daemon->namebuff);
2027 log6_opts(1, xid, ia_options, opt6_ptr(opt, opt6_len(opt)));
2031 static void log6_quiet(struct state *state, char *type, struct in6_addr *addr, char *string)
2033 if (option_bool(OPT_LOG_OPTS) || !option_bool(OPT_QUIET_DHCP6))
2034 log6_packet(state, type, addr, string);
2037 static void log6_packet(struct state *state, char *type, struct in6_addr *addr, char *string)
2039 int clid_len = state->clid_len;
2041 /* avoid buffer overflow */
2045 print_mac(daemon->namebuff, state->clid, clid_len);
2049 inet_ntop(AF_INET6, addr, daemon->dhcp_buff2, DHCP_BUFF_SZ - 1);
2050 strcat(daemon->dhcp_buff2, " ");
2053 daemon->dhcp_buff2[0] = 0;
2055 if(option_bool(OPT_LOG_OPTS))
2056 my_syslog(MS_DHCP | LOG_INFO, "%u %s(%s) %s%s %s",
2062 string ? string : "");
2064 my_syslog(MS_DHCP | LOG_INFO, "%s(%s) %s%s %s",
2069 string ? string : "");
2072 static void *opt6_find (void *opts, void *end, unsigned int search, unsigned int minsize)
2086 GETSHORT(opt, opts);
2087 GETSHORT(opt_len, opts);
2089 if (opt_len > (end - opts))
2092 if (opt == search && (opt_len >= minsize))
2099 static void *opt6_next(void *opts, void *end)
2107 GETSHORT(opt_len, opts);
2109 if (opt_len >= (end - opts))
2112 return opts + opt_len;
2115 static unsigned int opt6_uint(unsigned char *opt, int offset, int size)
2117 /* this worries about unaligned data and byte order */
2118 unsigned int ret = 0;
2120 unsigned char *p = opt6_ptr(opt, offset);
2122 for (i = 0; i < size; i++)
2123 ret = (ret << 8) | *p++;
2128 int relay_upstream6(int iface_index, ssize_t sz,
2129 struct in6_addr *peer_address, u32 scope_id, time_t now)
2131 unsigned char *header;
2132 unsigned char *inbuff = daemon->dhcp_packet.iov_base;
2133 int msg_type = *inbuff;
2135 struct in6_addr multicast;
2136 unsigned int maclen, mactype;
2137 unsigned char mac[DHCP_CHADDR_MAX];
2138 struct dhcp_relay *relay;
2140 for (relay = daemon->relay6; relay; relay = relay->next)
2141 if (relay->iface_index != 0 && relay->iface_index == iface_index)
2144 /* No relay config. */
2148 inet_pton(AF_INET6, ALL_SERVERS, &multicast);
2149 get_client_mac(peer_address, scope_id, mac, &maclen, &mactype, now);
2151 /* Get hop count from nested relayed message */
2152 if (msg_type == DHCP6RELAYFORW)
2153 hopcount = *((unsigned char *)inbuff+1) + 1;
2159 /* RFC 3315 HOP_COUNT_LIMIT */
2160 if (hopcount > 32 || !(header = put_opt6(NULL, 34)))
2163 header[0] = DHCP6RELAYFORW;
2164 header[1] = hopcount;
2165 memcpy(&header[18], peer_address, IN6ADDRSZ);
2170 o = new_opt6(OPTION6_CLIENT_MAC);
2171 put_opt6_short(mactype);
2172 put_opt6(mac, maclen);
2176 o = new_opt6(OPTION6_RELAY_MSG);
2177 put_opt6(inbuff, sz);
2180 for (; relay; relay = relay->next)
2181 if (relay->iface_index != 0 && relay->iface_index == iface_index)
2183 union mysockaddr to;
2185 memcpy(&header[2], &relay->local.addr6, IN6ADDRSZ);
2187 to.sa.sa_family = AF_INET6;
2188 to.in6.sin6_addr = relay->server.addr6;
2189 to.in6.sin6_port = htons(relay->port);
2190 to.in6.sin6_flowinfo = 0;
2191 to.in6.sin6_scope_id = 0;
2193 if (IN6_ARE_ADDR_EQUAL(&relay->server.addr6, &multicast))
2195 int multicast_iface;
2196 if (!relay->interface || strchr(relay->interface, '*') ||
2197 (multicast_iface = if_nametoindex(relay->interface)) == 0 ||
2198 setsockopt(daemon->dhcp6fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &multicast_iface, sizeof(multicast_iface)) == -1)
2200 my_syslog(MS_DHCP | LOG_ERR, _("Cannot multicast DHCP relay via interface %s"), relay->interface);
2205 #ifdef HAVE_DUMPFILE
2206 dump_packet_udp(DUMP_DHCPV6, (void *)daemon->outpacket.iov_base, save_counter(-1), NULL, &to, daemon->dhcp6fd);
2209 while (retry_send(sendto(daemon->dhcp6fd, (void *)daemon->outpacket.iov_base, save_counter(-1),
2210 0, (struct sockaddr *)&to, sa_len(&to))));
2212 if (option_bool(OPT_LOG_OPTS))
2214 inet_ntop(AF_INET6, &relay->local, daemon->addrbuff, ADDRSTRLEN);
2215 if (IN6_ARE_ADDR_EQUAL(&relay->server.addr6, &multicast))
2216 snprintf(daemon->namebuff, MAXDNAME, _("multicast via %s"), relay->interface);
2218 inet_ntop(AF_INET6, &relay->server, daemon->namebuff, ADDRSTRLEN);
2219 my_syslog(MS_DHCP | LOG_INFO, _("DHCP relay at %s -> %s"), daemon->addrbuff, daemon->namebuff);
2227 int relay_reply6(struct sockaddr_in6 *peer, ssize_t sz, char *arrival_interface)
2229 struct dhcp_relay *relay;
2230 struct in6_addr link;
2231 unsigned char *inbuff = daemon->dhcp_packet.iov_base;
2233 /* must have at least msg_type+hopcount+link_address+peer_address+minimal size option
2234 which is 1 + 1 + 16 + 16 + 2 + 2 = 38 */
2236 if (sz < 38 || *inbuff != DHCP6RELAYREPL)
2239 memcpy(&link, &inbuff[2], IN6ADDRSZ);
2241 for (relay = daemon->relay6; relay; relay = relay->next)
2242 if (IN6_ARE_ADDR_EQUAL(&link, &relay->local.addr6) &&
2243 (!relay->interface || wildcard_match(relay->interface, arrival_interface)))
2250 void *opt, *opts = inbuff + 34;
2251 void *end = inbuff + sz;
2252 for (opt = opts; opt; opt = opt6_next(opt, end))
2253 if (opt6_type(opt) == OPTION6_RELAY_MSG && opt6_len(opt) > 0)
2255 int encap_type = *((unsigned char *)opt6_ptr(opt, 0));
2256 put_opt6(opt6_ptr(opt, 0), opt6_len(opt));
2257 memcpy(&peer->sin6_addr, &inbuff[18], IN6ADDRSZ);
2258 peer->sin6_scope_id = relay->iface_index;
2260 if (encap_type == DHCP6RELAYREPL)
2262 peer->sin6_port = ntohs(DHCPV6_SERVER_PORT);
2266 peer->sin6_port = ntohs(DHCPV6_CLIENT_PORT);
2269 if (daemon->lease_change_command && encap_type == DHCP6REPLY)
2271 /* decapsulate relayed message */
2272 opts = opt6_ptr(opt, 4);
2273 end = opt6_ptr(opt, opt6_len(opt));
2275 for (opt = opts; opt; opt = opt6_next(opt, end))
2276 if (opt6_type(opt) == OPTION6_IA_PD && opt6_len(opt) > 12)
2278 void *ia_opts = opt6_ptr(opt, 12);
2279 void *ia_end = opt6_ptr(opt, opt6_len(opt));
2282 for (ia_opt = ia_opts; ia_opt; ia_opt = opt6_next(ia_opt, ia_end))
2283 /* valid lifetime must not be zero. */
2284 if (opt6_type(ia_opt) == OPTION6_IAPREFIX && opt6_len(ia_opt) >= 25 && opt6_uint(ia_opt, 4, 4) != 0)
2286 if (daemon->free_snoops ||
2287 (daemon->free_snoops = whine_malloc(sizeof(struct snoop_record))))
2289 struct snoop_record *snoop = daemon->free_snoops;
2291 daemon->free_snoops = snoop->next;
2292 snoop->client = peer->sin6_addr;
2293 snoop->prefix_len = opt6_uint(ia_opt, 8, 1);
2294 memcpy(&snoop->prefix, opt6_ptr(ia_opt, 9), IN6ADDRSZ);
2295 snoop->next = relay->snoop_records;
2296 relay->snoop_records = snoop;
2311 int do_snoop_script_run(void)
2313 struct dhcp_relay *relay;
2314 struct snoop_record *snoop;
2316 for (relay = daemon->relay6; relay; relay = relay->next)
2317 if ((snoop = relay->snoop_records))
2319 relay->snoop_records = snoop->next;
2320 snoop->next = daemon->free_snoops;
2321 daemon->free_snoops = snoop;
2323 queue_relay_snoop(&snoop->client, relay->iface_index, &snoop->prefix, snoop->prefix_len);