1 /* dnsmasq is Copyright (c) 2000-2013 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
, 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
;
28 unsigned int xid
, fqdn_flags
;
30 void *packet_options
, *end
;
31 struct dhcp_netid
*tags
, *context_tags
;
32 #ifdef OPTION6_PREFIX_CLASS
33 struct prefix_class
*send_prefix_class
;
37 static int dhcp6_maybe_relay(struct in6_addr
*link_address
, struct dhcp_netid
**relay_tagsp
, struct dhcp_context
*context
,
38 int interface
, char *iface_name
, struct in6_addr
*fallback
, void *inbuff
, size_t sz
, int is_unicast
, time_t now
);
39 static int dhcp6_no_relay(int msg_type
, struct in6_addr
*link_address
, struct dhcp_netid
*tags
, struct dhcp_context
*context
,
40 int interface
, char *iface_name
, struct in6_addr
*fallback
, void *inbuff
, size_t sz
, int is_unicast
, time_t now
);
41 static void log6_opts(int nest
, unsigned int xid
, void *start_opts
, void *end_opts
);
42 static void log6_packet(struct state
*state
, char *type
, struct in6_addr
*addr
, char *string
);
44 static void *opt6_find (void *opts
, void *end
, unsigned int search
, unsigned int minsize
);
45 static void *opt6_next(void *opts
, void *end
);
46 static unsigned int opt6_uint(unsigned char *opt
, int offset
, int size
);
47 static void get_context_tag(struct state
*state
, struct dhcp_context
*context
);
48 static int check_ia(struct state
*state
, void *opt
, void **endp
, void **ia_option
);
49 static int build_ia(struct state
*state
, int *t1cntr
);
50 static void end_ia(int t1cntr
, unsigned int min_time
, int do_fuzz
);
51 #ifdef OPTION6_PREFIX_CLASS
52 static struct prefix_class
*prefix_class_from_context(struct dhcp_context
*context
);
54 static void mark_context_used(struct state
*state
, struct dhcp_context
*context
, struct in6_addr
*addr
);
55 static void mark_config_used(struct dhcp_context
*context
, struct in6_addr
*addr
);
56 static int check_address(struct state
*state
, struct in6_addr
*addr
);
57 static void add_address(struct state
*state
, struct dhcp_context
*context
, unsigned int lease_time
, void *ia_option
,
58 unsigned int *min_time
, struct in6_addr
*addr
, time_t now
);
59 static void update_leases(struct state
*state
, struct dhcp_context
*context
, struct in6_addr
*addr
, unsigned int lease_time
, time_t now
);
60 static int add_local_addrs(struct dhcp_context
*context
);
61 static struct dhcp_netid
*add_options(struct state
*state
, struct in6_addr
*fallback
, struct dhcp_context
*context
, int do_refresh
);
62 static void calculate_times(struct dhcp_context
*context
, unsigned int *min_time
, unsigned int *valid_timep
,
63 unsigned int *preferred_timep
, unsigned int lease_time
);
65 #define opt6_len(opt) ((int)(opt6_uint(opt, -2, 2)))
66 #define opt6_type(opt) (opt6_uint(opt, -4, 2))
67 #define opt6_ptr(opt, i) ((void *)&(((unsigned char *)(opt))[4+(i)]))
70 unsigned short dhcp6_reply(struct dhcp_context
*context
, int interface
, char *iface_name
,
71 struct in6_addr
*fallback
, size_t sz
, int is_unicast
, time_t now
)
73 struct dhcp_netid
*relay_tags
= NULL
;
74 struct dhcp_vendor
*vendor
;
80 msg_type
= *((unsigned char *)daemon
->dhcp_packet
.iov_base
);
82 /* Mark these so we only match each at most once, to avoid tangled linked lists */
83 for (vendor
= daemon
->dhcp_vendors
; vendor
; vendor
= vendor
->next
)
84 vendor
->netid
.next
= &vendor
->netid
;
88 if (dhcp6_maybe_relay(NULL
, &relay_tags
, context
, interface
, iface_name
, fallback
, daemon
->dhcp_packet
.iov_base
, sz
, is_unicast
, now
))
89 return msg_type
== DHCP6RELAYFORW
? DHCPV6_SERVER_PORT
: DHCPV6_CLIENT_PORT
;
94 /* This cost me blood to write, it will probably cost you blood to understand - srk. */
95 static int dhcp6_maybe_relay(struct in6_addr
*link_address
, struct dhcp_netid
**relay_tagsp
, struct dhcp_context
*context
,
96 int interface
, char *iface_name
, struct in6_addr
*fallback
, void *inbuff
, size_t sz
, int is_unicast
, time_t now
)
98 void *end
= inbuff
+ sz
;
99 void *opts
= inbuff
+ 34;
100 int msg_type
= *((unsigned char *)inbuff
);
101 unsigned char *outmsgtypep
;
103 struct dhcp_vendor
*vendor
;
105 /* if not an encaplsulated relayed message, just do the stuff */
106 if (msg_type
!= DHCP6RELAYFORW
)
108 /* if link_address != NULL if points to the link address field of the
109 innermost nested RELAYFORW message, which is where we find the
110 address of the network on which we can allocate an address.
111 Recalculate the available contexts using that information. */
115 struct dhcp_context
*c
;
118 if (!IN6_IS_ADDR_LOOPBACK(link_address
) &&
119 !IN6_IS_ADDR_LINKLOCAL(link_address
) &&
120 !IN6_IS_ADDR_MULTICAST(link_address
))
121 for (c
= daemon
->dhcp6
; c
; c
= c
->next
)
122 if ((c
->flags
& CONTEXT_DHCP
) &&
123 !(c
->flags
& (CONTEXT_TEMPLATE
| CONTEXT_OLD
)) &&
124 is_same_net6(link_address
, &c
->start6
, c
->prefix
) &&
125 is_same_net6(link_address
, &c
->end6
, c
->prefix
))
127 c
->preferred
= c
->valid
= 0xffffffff;
128 c
->current
= context
;
134 inet_ntop(AF_INET6
, link_address
, daemon
->addrbuff
, ADDRSTRLEN
);
135 my_syslog(MS_DHCP
| LOG_WARNING
,
136 _("no address range available for DHCPv6 request from relay at %s"),
144 my_syslog(MS_DHCP
| LOG_WARNING
,
145 _("no address range available for DHCPv6 request via %s"), iface_name
);
149 return dhcp6_no_relay(msg_type
, link_address
, *relay_tagsp
, context
, interface
, iface_name
, fallback
, inbuff
, sz
, is_unicast
, now
);
152 /* must have at least msg_type+hopcount+link_address+peer_address+minimal size option
153 which is 1 + 1 + 16 + 16 + 2 + 2 = 38 */
157 /* copy header stuff into reply message and set type to reply */
158 if (!(outmsgtypep
= put_opt6(inbuff
, 34)))
160 *outmsgtypep
= DHCP6RELAYREPL
;
162 /* look for relay options and set tags if found. */
163 for (vendor
= daemon
->dhcp_vendors
; vendor
; vendor
= vendor
->next
)
167 if (vendor
->match_type
== MATCH_SUBSCRIBER
)
168 mopt
= OPTION6_SUBSCRIBER_ID
;
169 else if (vendor
->match_type
== MATCH_REMOTE
)
170 mopt
= OPTION6_REMOTE_ID
;
174 if ((opt
= opt6_find(opts
, end
, mopt
, 1)) &&
175 vendor
->len
== opt6_len(opt
) &&
176 memcmp(vendor
->data
, opt6_ptr(opt
, 0), vendor
->len
) == 0 &&
177 vendor
->netid
.next
!= &vendor
->netid
)
179 vendor
->netid
.next
= *relay_tagsp
;
180 *relay_tagsp
= &vendor
->netid
;
185 for (opt
= opts
; opt
; opt
= opt6_next(opt
, end
))
187 int o
= new_opt6(opt6_type(opt
));
188 if (opt6_type(opt
) == OPTION6_RELAY_MSG
)
190 struct in6_addr link_address
;
191 /* the packet data is unaligned, copy to aligned storage */
192 memcpy(&link_address
, inbuff
+ 2, IN6ADDRSZ
);
193 /* Not, zero is_unicast since that is now known to refer to the
194 relayed packet, not the original sent by the client */
195 if (!dhcp6_maybe_relay(&link_address
, relay_tagsp
, context
, interface
, iface_name
, fallback
, opt6_ptr(opt
, 0), opt6_len(opt
), 0, now
))
199 put_opt6(opt6_ptr(opt
, 0), opt6_len(opt
));
206 static int dhcp6_no_relay(int msg_type
, struct in6_addr
*link_address
, struct dhcp_netid
*tags
, struct dhcp_context
*context
,
207 int interface
, char *iface_name
, struct in6_addr
*fallback
, void *inbuff
, size_t sz
, int is_unicast
, time_t now
)
210 int i
, o
, o1
, start_opts
;
211 struct dhcp_opt
*opt_cfg
;
212 struct dhcp_netid
*tagif
;
213 struct dhcp_config
*config
= NULL
;
214 struct dhcp_netid known_id
, iface_id
, v6_id
;
215 unsigned char *outmsgtypep
;
216 struct dhcp_vendor
*vendor
;
217 struct dhcp_context
*context_tmp
;
218 unsigned int ignore
= 0;
220 #ifdef OPTION6_PREFIX_CLASS
221 struct prefix_class
*p
;
222 int dump_all_prefix_classes
= 0;
225 state
.packet_options
= inbuff
+ 4;
226 state
.end
= inbuff
+ sz
;
229 state
.lease_allocate
= 0;
230 state
.context_tags
= NULL
;
232 state
.link_address
= link_address
;
233 state
.interface
= interface
;
235 state
.send_domain
= NULL
;
236 state
.context
= context
;
237 state
.hostname_auth
= 0;
238 state
.hostname
= NULL
;
239 state
.client_hostname
= NULL
;
240 state
.iface_name
= iface_name
;
241 state
.fqdn_flags
= 0x01; /* default to send if we recieve no FQDN option */
242 #ifdef OPTION6_PREFIX_CLASS
243 state
.send_prefix_class
= NULL
;
246 /* set tag with name == interface */
247 iface_id
.net
= iface_name
;
248 iface_id
.next
= state
.tags
;
249 state
.tags
= &iface_id
;
251 /* set tag "dhcpv6" */
252 v6_id
.net
= "dhcpv6";
253 v6_id
.next
= state
.tags
;
256 /* copy over transaction-id, and save pointer to message type */
257 if (!(outmsgtypep
= put_opt6(inbuff
, 4)))
259 start_opts
= save_counter(-1);
260 state
.xid
= outmsgtypep
[3] | outmsgtypep
[2] << 8 | outmsgtypep
[1] << 16;
262 /* We're going to be linking tags from all context we use.
263 mark them as unused so we don't link one twice and break the list */
264 for (context_tmp
= context
; context_tmp
; context_tmp
= context_tmp
->current
)
266 context
->netid
.next
= &context
->netid
;
268 if (option_bool(OPT_LOG_OPTS
))
270 inet_ntop(AF_INET6
, &context_tmp
->start6
, daemon
->dhcp_buff
, ADDRSTRLEN
);
271 inet_ntop(AF_INET6
, &context_tmp
->end6
, daemon
->dhcp_buff2
, ADDRSTRLEN
);
272 if (context_tmp
->flags
& (CONTEXT_STATIC
))
273 my_syslog(MS_DHCP
| LOG_INFO
, _("%u available DHCPv6 subnet: %s/%d"),
274 state
.xid
, daemon
->dhcp_buff
, context_tmp
->prefix
);
276 my_syslog(MS_DHCP
| LOG_INFO
, _("%u available DHCP range: %s -- %s"),
277 state
.xid
, daemon
->dhcp_buff
, daemon
->dhcp_buff2
);
281 if ((opt
= opt6_find(state
.packet_options
, state
.end
, OPTION6_CLIENT_ID
, 1)))
283 state
.clid
= opt6_ptr(opt
, 0);
284 state
.clid_len
= opt6_len(opt
);
285 o
= new_opt6(OPTION6_CLIENT_ID
);
286 put_opt6(state
.clid
, state
.clid_len
);
289 else if (msg_type
!= DHCP6IREQ
)
292 /* server-id must match except for SOLICIT and CONFIRM messages */
293 if (msg_type
!= DHCP6SOLICIT
&& msg_type
!= DHCP6CONFIRM
&& msg_type
!= DHCP6IREQ
&&
294 (!(opt
= opt6_find(state
.packet_options
, state
.end
, OPTION6_SERVER_ID
, 1)) ||
295 opt6_len(opt
) != daemon
->duid_len
||
296 memcmp(opt6_ptr(opt
, 0), daemon
->duid
, daemon
->duid_len
) != 0))
299 o
= new_opt6(OPTION6_SERVER_ID
);
300 put_opt6(daemon
->duid
, daemon
->duid_len
);
304 (msg_type
== DHCP6REQUEST
|| msg_type
== DHCP6RENEW
|| msg_type
== DHCP6RELEASE
|| msg_type
== DHCP6DECLINE
))
307 o1
= new_opt6(OPTION6_STATUS_CODE
);
308 put_opt6_short(DHCP6USEMULTI
);
309 put_opt6_string("Use multicast");
314 /* match vendor and user class options */
315 for (vendor
= daemon
->dhcp_vendors
; vendor
; vendor
= vendor
->next
)
319 if (vendor
->match_type
== MATCH_VENDOR
)
320 mopt
= OPTION6_VENDOR_CLASS
;
321 else if (vendor
->match_type
== MATCH_USER
)
322 mopt
= OPTION6_USER_CLASS
;
326 if ((opt
= opt6_find(state
.packet_options
, state
.end
, mopt
, 2)))
328 void *enc_opt
, *enc_end
= opt6_ptr(opt
, opt6_len(opt
));
331 if (mopt
== OPTION6_VENDOR_CLASS
)
333 if (opt6_len(opt
) < 4)
336 if (vendor
->enterprise
!= opt6_uint(opt
, 0, 4))
342 for (enc_opt
= opt6_ptr(opt
, offset
); enc_opt
; enc_opt
= opt6_next(enc_opt
, enc_end
))
343 for (i
= 0; i
<= (opt6_len(enc_opt
) - vendor
->len
); i
++)
344 if (memcmp(vendor
->data
, opt6_ptr(enc_opt
, i
), vendor
->len
) == 0)
346 vendor
->netid
.next
= state
.tags
;
347 state
.tags
= &vendor
->netid
;
353 if (option_bool(OPT_LOG_OPTS
) && (opt
= opt6_find(state
.packet_options
, state
.end
, OPTION6_VENDOR_CLASS
, 4)))
354 my_syslog(MS_DHCP
| LOG_INFO
, _("%u vendor class: %u"), state
.xid
, opt6_uint(opt
, 0, 4));
356 /* dhcp-match. If we have hex-and-wildcards, look for a left-anchored match.
357 Otherwise assume the option is an array, and look for a matching element.
358 If no data given, existance of the option is enough. This code handles
360 for (opt_cfg
= daemon
->dhcp_match6
; opt_cfg
; opt_cfg
= opt_cfg
->next
)
364 if (opt_cfg
->flags
& DHOPT_RFC3925
)
366 for (opt
= opt6_find(state
.packet_options
, state
.end
, OPTION6_VENDOR_OPTS
, 4);
368 opt
= opt6_find(opt6_next(opt
, state
.end
), state
.end
, OPTION6_VENDOR_OPTS
, 4))
371 void *vend
= opt6_ptr(opt
, opt6_len(opt
));
373 for (vopt
= opt6_find(opt6_ptr(opt
, 4), vend
, opt_cfg
->opt
, 0);
375 vopt
= opt6_find(opt6_next(vopt
, vend
), vend
, opt_cfg
->opt
, 0))
376 if ((match
= match_bytes(opt_cfg
, opt6_ptr(vopt
, 0), opt6_len(vopt
))))
384 if (!(opt
= opt6_find(state
.packet_options
, state
.end
, opt_cfg
->opt
, 1)))
387 match
= match_bytes(opt_cfg
, opt6_ptr(opt
, 0), opt6_len(opt
));
392 opt_cfg
->netid
->next
= state
.tags
;
393 state
.tags
= opt_cfg
->netid
;
397 if ((opt
= opt6_find(state
.packet_options
, state
.end
, OPTION6_FQDN
, 1)))
400 int len
= opt6_len(opt
) - 1;
402 state
.fqdn_flags
= opt6_uint(opt
, 0, 1);
404 /* Always force update, since the client has no way to do it itself. */
405 if (!option_bool(OPT_FQDN_UPDATE
) && !(state
.fqdn_flags
& 0x01))
406 state
.fqdn_flags
|= 0x03;
408 state
.fqdn_flags
&= ~0x04;
410 if (len
!= 0 && len
< 255)
412 unsigned char *pp
, *op
= opt6_ptr(opt
, 1);
413 char *pq
= daemon
->dhcp_buff
;
416 while (*op
!= 0 && ((op
+ (*op
)) - pp
) < len
)
418 memcpy(pq
, op
+1, *op
);
424 if (pq
!= daemon
->dhcp_buff
)
428 if (legal_hostname(daemon
->dhcp_buff
))
430 state
.client_hostname
= daemon
->dhcp_buff
;
431 if (option_bool(OPT_LOG_OPTS
))
432 my_syslog(MS_DHCP
| LOG_INFO
, _("%u client provides name: %s"), state
.xid
, state
.client_hostname
);
439 config
= find_config6(daemon
->dhcp_conf
, context
, state
.clid
, state
.clid_len
, NULL
);
441 if (have_config(config
, CONFIG_NAME
))
443 state
.hostname
= config
->hostname
;
444 state
.domain
= config
->domain
;
445 state
.hostname_auth
= 1;
447 else if (state
.client_hostname
)
449 state
.domain
= strip_hostname(state
.client_hostname
);
451 if (strlen(state
.client_hostname
) != 0)
453 state
.hostname
= state
.client_hostname
;
456 /* Search again now we have a hostname.
457 Only accept configs without CLID here, (it won't match)
458 to avoid impersonation by name. */
459 struct dhcp_config
*new = find_config6(daemon
->dhcp_conf
, context
, NULL
, 0, state
.hostname
);
460 if (new && !have_config(new, CONFIG_CLID
) && !new->hwaddr
)
469 struct dhcp_netid_list
*list
;
471 for (list
= config
->netid
; list
; list
= list
->next
)
473 list
->list
->next
= state
.tags
;
474 state
.tags
= list
->list
;
477 /* set "known" tag for known hosts */
478 known_id
.net
= "known";
479 known_id
.next
= state
.tags
;
480 state
.tags
= &known_id
;
482 if (have_config(config
, CONFIG_DISABLE
))
486 #ifdef OPTION6_PREFIX_CLASS
487 /* OPTION_PREFIX_CLASS in ORO, send addresses in all prefix classes */
488 if (daemon
->prefix_classes
&& (msg_type
== DHCP6SOLICIT
|| msg_type
== DHCP6REQUEST
))
492 if ((oro
= opt6_find(state
.packet_options
, state
.end
, OPTION6_ORO
, 0)))
493 for (i
= 0; i
< opt6_len(oro
) - 1; i
+= 2)
494 if (opt6_uint(oro
, i
, 2) == OPTION6_PREFIX_CLASS
)
496 dump_all_prefix_classes
= 1;
500 if (msg_type
!= DHCP6SOLICIT
|| dump_all_prefix_classes
)
501 /* Add the tags associated with prefix classes so we can use the DHCP ranges.
502 Not done for SOLICIT as we add them one-at-time. */
503 for (p
= daemon
->prefix_classes
; p
; p
= p
->next
)
505 p
->tag
.next
= state
.tags
;
506 state
.tags
= &p
->tag
;
511 tagif
= run_tag_if(state
.tags
);
513 /* if all the netids in the ignore list are present, ignore this client */
514 if (daemon
->dhcp_ignore
)
516 struct dhcp_netid_list
*id_list
;
518 for (id_list
= daemon
->dhcp_ignore
; id_list
; id_list
= id_list
->next
)
519 if (match_netid(id_list
->list
, tagif
, 0))
523 /* if all the netids in the ignore_name list are present, ignore client-supplied name */
524 if (!state
.hostname_auth
)
526 struct dhcp_netid_list
*id_list
;
528 for (id_list
= daemon
->dhcp_ignore_names
; id_list
; id_list
= id_list
->next
)
529 if ((!id_list
->list
) || match_netid(id_list
->list
, tagif
, 0))
532 state
.hostname
= NULL
;
544 int address_assigned
= 0;
545 /* tags without all prefix-class tags */
546 struct dhcp_netid
*solicit_tags
= tagif
;
547 struct dhcp_context
*c
;
549 *outmsgtypep
= DHCP6ADVERTISE
;
551 if (opt6_find(state
.packet_options
, state
.end
, OPTION6_RAPID_COMMIT
, 0))
553 *outmsgtypep
= DHCP6REPLY
;
554 state
.lease_allocate
= 1;
555 o
= new_opt6(OPTION6_RAPID_COMMIT
);
559 log6_packet(&state
, "DHCPSOLICIT", NULL
, ignore
? _("ignored") : NULL
);
563 #ifdef HAVE_QUIET_DHCP
564 if (!option_bool(OPT_QUIET_DHCP6
))
569 /* reset USED bits in leases */
572 /* Can use configured address max once per prefix */
573 for (c
= context
; c
; c
= c
->current
)
574 c
->flags
&= ~CONTEXT_CONF_USED
;
576 for (opt
= state
.packet_options
; opt
; opt
= opt6_next(opt
, state
.end
))
578 void *ia_option
, *ia_end
;
579 unsigned int min_time
= 0xffffffff;
582 /* set unless we're sending a particular prefix-class, when we
583 want only dhcp-ranges with the correct tags set and not those without any tags. */
586 struct dhcp_lease
*ltmp
;
587 struct in6_addr
*req_addr
;
588 struct in6_addr addr
;
590 if (!check_ia(&state
, opt
, &ia_end
, &ia_option
))
593 /* reset USED bits in contexts - one address per prefix per IAID */
594 for (c
= context
; c
; c
= c
->current
)
595 c
->flags
&= ~CONTEXT_USED
;
597 #ifdef OPTION6_PREFIX_CLASS
598 if (daemon
->prefix_classes
&& state
.ia_type
== OPTION6_IA_NA
)
603 if (dump_all_prefix_classes
)
604 /* OPTION_PREFIX_CLASS in ORO, send addresses in all prefix classes */
608 if ((prefix_opt
= opt6_find(opt6_ptr(opt
, 12), ia_end
, OPTION6_PREFIX_CLASS
, 2)))
611 prefix_class
= opt6_uint(prefix_opt
, 0, 2);
613 for (p
= daemon
->prefix_classes
; p
; p
= p
->next
)
614 if (p
->class == prefix_class
)
618 my_syslog(MS_DHCP
| LOG_WARNING
, _("unknown prefix-class %d"), prefix_class
);
621 /* add tag to list, and exclude undecorated dhcp-ranges */
622 p
->tag
.next
= state
.tags
;
623 solicit_tags
= run_tag_if(&p
->tag
);
625 state
.send_prefix_class
= p
;
630 /* client didn't ask for a prefix class, lets see if we can find one. */
631 for (p
= daemon
->prefix_classes
; p
; p
= p
->next
)
634 if (match_netid(&p
->tag
, solicit_tags
, 1))
641 state
.send_prefix_class
= p
;
645 if (p
&& option_bool(OPT_LOG_OPTS
))
646 my_syslog(MS_DHCP
| LOG_INFO
, "%u prefix class %d tag:%s", state
.xid
, p
->class, p
->tag
.net
);
651 o
= build_ia(&state
, &t1cntr
);
653 for (ia_counter
= 0; ia_option
; ia_counter
++, ia_option
= opt6_find(opt6_next(ia_option
, ia_end
), ia_end
, OPTION6_IAADDR
, 24))
655 req_addr
= opt6_ptr(ia_option
, 0);
657 if ((c
= address6_valid(context
, req_addr
, solicit_tags
, plain_range
)))
659 lease_time
= c
->lease_time
;
660 /* If the client asks for an address on the same network as a configured address,
661 offer the configured address instead, to make moving to newly-configured
662 addresses automatic. */
663 if (!(c
->flags
& CONTEXT_CONF_USED
) && config_valid(config
, c
, &addr
) && check_address(&state
, &addr
))
666 mark_config_used(c
, &addr
);
667 if (have_config(config
, CONFIG_TIME
))
668 lease_time
= config
->lease_time
;
670 else if (!(c
= address6_available(context
, req_addr
, solicit_tags
, plain_range
)))
671 continue; /* not an address we're allowed */
672 else if (!check_address(&state
, req_addr
))
673 continue; /* address leased elsewhere */
675 /* add address to output packet */
676 #ifdef OPTION6_PREFIX_CLASS
677 if (dump_all_prefix_classes
&& state
.ia_type
== OPTION6_IA_NA
)
678 state
.send_prefix_class
= prefix_class_from_context(c
);
680 add_address(&state
, c
, lease_time
, ia_option
, &min_time
, req_addr
, now
);
681 mark_context_used(&state
, context
, req_addr
);
682 get_context_tag(&state
, c
);
683 address_assigned
= 1;
687 /* Suggest configured address(es) */
688 for (c
= context
; c
; c
= c
->current
)
689 if (!(c
->flags
& CONTEXT_CONF_USED
) &&
690 match_netid(c
->filter
, solicit_tags
, plain_range
) &&
691 config_valid(config
, c
, &addr
) &&
692 check_address(&state
, &addr
))
694 mark_config_used(context
, &addr
);
695 if (have_config(config
, CONFIG_TIME
))
696 lease_time
= config
->lease_time
;
698 lease_time
= c
->lease_time
;
699 /* add address to output packet */
700 #ifdef OPTION6_PREFIX_CLASS
701 if (dump_all_prefix_classes
&& state
.ia_type
== OPTION6_IA_NA
)
702 state
.send_prefix_class
= prefix_class_from_context(c
);
704 add_address(&state
, c
, lease_time
, NULL
, &min_time
, &addr
, now
);
705 mark_context_used(&state
, context
, &addr
);
706 get_context_tag(&state
, c
);
707 address_assigned
= 1;
710 /* return addresses for existing leases */
712 while ((ltmp
= lease6_find_by_client(ltmp
, state
.ia_type
== OPTION6_IA_NA
? LEASE_NA
: LEASE_TA
, state
.clid
, state
.clid_len
, state
.iaid
)))
714 req_addr
= (struct in6_addr
*)ltmp
->hwaddr
;
715 if ((c
= address6_available(context
, req_addr
, solicit_tags
, plain_range
)))
717 #ifdef OPTION6_PREFIX_CLASS
718 if (dump_all_prefix_classes
&& state
.ia_type
== OPTION6_IA_NA
)
719 state
.send_prefix_class
= prefix_class_from_context(c
);
721 add_address(&state
, c
, c
->lease_time
, NULL
, &min_time
, req_addr
, now
);
722 mark_context_used(&state
, context
, req_addr
);
723 get_context_tag(&state
, c
);
724 address_assigned
= 1;
728 /* Return addresses for all valid contexts which don't yet have one */
729 while ((c
= address6_allocate(context
, state
.clid
, state
.clid_len
, state
.iaid
, ia_counter
, solicit_tags
, plain_range
, &addr
)))
731 #ifdef OPTION6_PREFIX_CLASS
732 if (dump_all_prefix_classes
&& state
.ia_type
== OPTION6_IA_NA
)
733 state
.send_prefix_class
= prefix_class_from_context(c
);
735 add_address(&state
, c
, c
->lease_time
, NULL
, &min_time
, &addr
, now
);
736 mark_context_used(&state
, context
, &addr
);
737 get_context_tag(&state
, c
);
738 address_assigned
= 1;
741 end_ia(t1cntr
, min_time
, 0);
745 if (address_assigned
)
747 o1
= new_opt6(OPTION6_STATUS_CODE
);
748 put_opt6_short(DHCP6SUCCESS
);
749 put_opt6_string(_("success"));
752 /* If --dhcp-authoritative is set, we can tell client not to wait for
753 other possible servers */
754 o
= new_opt6(OPTION6_PREFERENCE
);
755 put_opt6_char(option_bool(OPT_AUTHORITATIVE
) ? 255 : 0);
757 tagif
= add_options(&state
, fallback
, context
, 0);
761 /* no address, return error */
762 o1
= new_opt6(OPTION6_STATUS_CODE
);
763 put_opt6_short(DHCP6NOADDRS
);
764 put_opt6_string(_("no addresses available"));
766 #ifdef HAVE_QUIET_DHCP
767 if (!option_bool(OPT_QUIET_DHCP6
))
769 log6_packet(&state
, "DHCPADVERTISE", NULL
, _("no addresses available"));
777 int address_assigned
= 0;
778 int start
= save_counter(-1);
780 /* set reply message type */
781 *outmsgtypep
= DHCP6REPLY
;
782 state
.lease_allocate
= 1;
784 #ifdef HAVE_QUIET_DHCP
785 if (!option_bool(OPT_QUIET_DHCP6
))
787 log6_packet(&state
, "DHCPREQUEST", NULL
, ignore
? _("ignored") : NULL
);
792 for (opt
= state
.packet_options
; opt
; opt
= opt6_next(opt
, state
.end
))
794 void *ia_option
, *ia_end
;
795 unsigned int min_time
= 0xffffffff;
798 if (!check_ia(&state
, opt
, &ia_end
, &ia_option
))
803 /* If we get a request with a IA_*A without addresses, treat it exactly like
804 a SOLICT with rapid commit set. */
806 goto request_no_address
;
809 o
= build_ia(&state
, &t1cntr
);
811 for (; ia_option
; ia_option
= opt6_find(opt6_next(ia_option
, ia_end
), ia_end
, OPTION6_IAADDR
, 24))
813 struct in6_addr
*req_addr
= opt6_ptr(ia_option
, 0);
814 struct dhcp_context
*dynamic
, *c
;
815 unsigned int lease_time
;
816 struct in6_addr addr
;
819 if ((c
= address6_valid(context
, req_addr
, tagif
, 1)))
820 config_ok
= config_valid(config
, c
, &addr
) && IN6_ARE_ADDR_EQUAL(&addr
, req_addr
);
822 if ((dynamic
= address6_available(context
, req_addr
, tagif
, 1)) || c
)
824 if (!dynamic
&& !config_ok
)
826 /* Static range, not configured. */
827 o1
= new_opt6(OPTION6_STATUS_CODE
);
828 put_opt6_short(DHCP6UNSPEC
);
829 put_opt6_string(_("address unavailable"));
832 else if (!check_address(&state
, req_addr
))
834 /* Address leased to another DUID/IAID */
835 o1
= new_opt6(OPTION6_STATUS_CODE
);
836 put_opt6_short(DHCP6UNSPEC
);
837 put_opt6_string(_("address in use"));
845 lease_time
= dynamic
->lease_time
;
847 if (config_ok
&& have_config(config
, CONFIG_TIME
))
848 lease_time
= config
->lease_time
;
850 #ifdef OPTION6_PREFIX_CLASS
851 if (dump_all_prefix_classes
&& state
.ia_type
== OPTION6_IA_NA
)
852 state
.send_prefix_class
= prefix_class_from_context(c
);
854 add_address(&state
, dynamic
, lease_time
, ia_option
, &min_time
, req_addr
, now
);
855 get_context_tag(&state
, dynamic
);
856 address_assigned
= 1;
861 /* requested address not on the correct link */
862 o1
= new_opt6(OPTION6_STATUS_CODE
);
863 put_opt6_short(DHCP6NOTONLINK
);
864 put_opt6_string(_("not on link"));
869 end_ia(t1cntr
, min_time
, 0);
873 if (address_assigned
)
875 o1
= new_opt6(OPTION6_STATUS_CODE
);
876 put_opt6_short(DHCP6SUCCESS
);
877 put_opt6_string(_("success"));
882 /* no address, return error */
883 o1
= new_opt6(OPTION6_STATUS_CODE
);
884 put_opt6_short(DHCP6NOADDRS
);
885 put_opt6_string(_("no addresses available"));
887 #ifdef HAVE_QUIET_DHCP
888 if (!option_bool(OPT_QUIET_DHCP6
))
890 log6_packet(&state
, "DHCPREPLY", NULL
, _("no addresses available"));
893 tagif
= add_options(&state
, fallback
, context
, 0);
900 /* set reply message type */
901 *outmsgtypep
= DHCP6REPLY
;
903 #ifdef HAVE_QUIET_DHCP
904 if (!option_bool(OPT_QUIET_DHCP6
))
906 log6_packet(&state
, "DHCPRENEW", NULL
, NULL
);
908 for (opt
= state
.packet_options
; opt
; opt
= opt6_next(opt
, state
.end
))
910 void *ia_option
, *ia_end
;
911 unsigned int min_time
= 0xffffffff;
914 if (!check_ia(&state
, opt
, &ia_end
, &ia_option
))
917 o
= build_ia(&state
, &t1cntr
);
918 iacntr
= save_counter(-1);
920 for (; ia_option
; ia_option
= opt6_find(opt6_next(ia_option
, ia_end
), ia_end
, OPTION6_IAADDR
, 24))
922 struct dhcp_lease
*lease
= NULL
;
923 struct in6_addr
*req_addr
= opt6_ptr(ia_option
, 0);
924 unsigned int preferred_time
= opt6_uint(ia_option
, 16, 4);
925 unsigned int valid_time
= opt6_uint(ia_option
, 20, 4);
926 char *message
= NULL
;
927 struct dhcp_context
*this_context
;
929 if (!(lease
= lease6_find(state
.clid
, state
.clid_len
,
930 state
.ia_type
== OPTION6_IA_NA
? LEASE_NA
: LEASE_TA
,
931 state
.iaid
, req_addr
)))
933 /* If the server cannot find a client entry for the IA the server
934 returns the IA containing no addresses with a Status Code option set
935 to NoBinding in the Reply message. */
936 save_counter(iacntr
);
939 #ifdef HAVE_QUIET_DHCP
940 if (!option_bool(OPT_QUIET_DHCP6
))
942 log6_packet(&state
, "DHCPREPLY", req_addr
, _("lease not found"));
944 o1
= new_opt6(OPTION6_STATUS_CODE
);
945 put_opt6_short(DHCP6NOBINDING
);
946 put_opt6_string(_("no binding found"));
949 preferred_time
= valid_time
= 0;
954 if ((this_context
= address6_available(context
, req_addr
, tagif
, 1)) ||
955 (this_context
= address6_valid(context
, req_addr
, tagif
, 1)))
957 struct in6_addr addr
;
958 unsigned int lease_time
;
960 get_context_tag(&state
, this_context
);
962 if (config_valid(config
, this_context
, &addr
) && IN6_ARE_ADDR_EQUAL(&addr
, req_addr
) && have_config(config
, CONFIG_TIME
))
963 lease_time
= config
->lease_time
;
965 lease_time
= this_context
->lease_time
;
967 calculate_times(this_context
, &min_time
, &valid_time
, &preferred_time
, lease_time
);
969 lease_set_expires(lease
, valid_time
, now
);
970 if (state
.ia_type
== OPTION6_IA_NA
&& state
.hostname
)
972 char *addr_domain
= get_domain6(req_addr
);
973 if (!state
.send_domain
)
974 state
.send_domain
= addr_domain
;
975 lease_set_hostname(lease
, state
.hostname
, state
.hostname_auth
, addr_domain
, state
.domain
);
976 message
= state
.hostname
;
980 if (preferred_time
== 0)
981 message
= _("deprecated");
985 preferred_time
= valid_time
= 0;
986 message
= _("address invalid");
989 #ifdef HAVE_QUIET_DHCP
990 if (!option_bool(OPT_QUIET_DHCP6
))
992 log6_packet(&state
, "DHCPREPLY", req_addr
, message
);
994 o1
= new_opt6(OPTION6_IAADDR
);
995 put_opt6(req_addr
, sizeof(*req_addr
));
996 put_opt6_long(preferred_time
);
997 put_opt6_long(valid_time
);
1001 end_ia(t1cntr
, min_time
, 1);
1005 tagif
= add_options(&state
, fallback
, context
, 0);
1012 /* set reply message type */
1013 *outmsgtypep
= DHCP6REPLY
;
1015 #ifdef HAVE_QUIET_DHCP
1016 if (!option_bool(OPT_QUIET_DHCP6
))
1018 log6_packet(&state
, "DHCPCONFIRM", NULL
, NULL
);
1020 for (opt
= state
.packet_options
; opt
; opt
= opt6_next(opt
, state
.end
))
1022 void *ia_option
, *ia_end
;
1024 for (check_ia(&state
, opt
, &ia_end
, &ia_option
);
1026 ia_option
= opt6_find(opt6_next(ia_option
, ia_end
), ia_end
, OPTION6_IAADDR
, 24))
1028 struct in6_addr
*req_addr
= opt6_ptr(ia_option
, 0);
1030 if (!address6_available(context
, req_addr
, tagif
, 1))
1032 o1
= new_opt6(OPTION6_STATUS_CODE
);
1033 put_opt6_short(DHCP6NOTONLINK
);
1034 put_opt6_string(_("confirm failed"));
1039 #ifdef HAVE_QUIET_DHCP
1040 if (!option_bool(OPT_QUIET_DHCP6
))
1042 log6_packet(&state
, "DHCPREPLY", req_addr
, state
.hostname
);
1046 o1
= new_opt6(OPTION6_STATUS_CODE
);
1047 put_opt6_short(DHCP6SUCCESS
);
1048 put_opt6_string(_("all addresses still on link"));
1055 /* We can't discriminate contexts based on address, as we don't know it.
1056 If there is only one possible context, we can use its tags */
1057 if (context
&& context
->netid
.net
&& !context
->current
)
1059 context
->netid
.next
= NULL
;
1060 state
.context_tags
= &context
->netid
;
1063 /* Similarly, we can't determine domain from address, but if the FQDN is
1064 given in --dhcp-host, we can use that, and failing that we can use the
1065 unqualified configured domain, if any. */
1066 if (state
.hostname_auth
)
1067 state
.send_domain
= state
.domain
;
1069 state
.send_domain
= get_domain6(NULL
);
1071 #ifdef HAVE_QUIET_DHCP
1072 if (!option_bool(OPT_QUIET_DHCP6
))
1074 log6_packet(&state
, "DHCPINFORMATION-REQUEST", NULL
, ignore
? _("ignored") : state
.hostname
);
1077 *outmsgtypep
= DHCP6REPLY
;
1078 tagif
= add_options(&state
, fallback
, context
, 1);
1085 /* set reply message type */
1086 *outmsgtypep
= DHCP6REPLY
;
1088 #ifdef HAVE_QUIET_DHCP
1089 if (!option_bool(OPT_QUIET_DHCP6
))
1091 log6_packet(&state
, "DHCPRELEASE", NULL
, NULL
);
1093 for (opt
= state
.packet_options
; opt
; opt
= opt6_next(opt
, state
.end
))
1095 void *ia_option
, *ia_end
;
1098 for (check_ia(&state
, opt
, &ia_end
, &ia_option
);
1100 ia_option
= opt6_find(opt6_next(ia_option
, ia_end
), ia_end
, OPTION6_IAADDR
, 24))
1102 struct dhcp_lease
*lease
;
1104 if ((lease
= lease6_find(state
.clid
, state
.clid_len
, state
.ia_type
== OPTION6_IA_NA
? LEASE_NA
: LEASE_TA
,
1105 state
.iaid
, opt6_ptr(ia_option
, 0))))
1106 lease_prune(lease
, now
);
1111 o
= new_opt6(state
.ia_type
);
1112 put_opt6_long(state
.iaid
);
1113 if (state
.ia_type
== OPTION6_IA_NA
)
1121 o1
= new_opt6(OPTION6_IAADDR
);
1122 put_opt6(opt6_ptr(ia_option
, 0), IN6ADDRSZ
);
1131 o1
= new_opt6(OPTION6_STATUS_CODE
);
1132 put_opt6_short(DHCP6NOBINDING
);
1133 put_opt6_string(_("no binding found"));
1140 o1
= new_opt6(OPTION6_STATUS_CODE
);
1141 put_opt6_short(DHCP6SUCCESS
);
1142 put_opt6_string(_("release received"));
1150 /* set reply message type */
1151 *outmsgtypep
= DHCP6REPLY
;
1153 #ifdef HAVE_QUIET_DHCP
1154 if (!option_bool(OPT_QUIET_DHCP6
))
1156 log6_packet(&state
, "DHCPDECLINE", NULL
, NULL
);
1158 for (opt
= state
.packet_options
; opt
; opt
= opt6_next(opt
, state
.end
))
1160 void *ia_option
, *ia_end
;
1163 for (check_ia(&state
, opt
, &ia_end
, &ia_option
);
1165 ia_option
= opt6_find(opt6_next(ia_option
, ia_end
), ia_end
, OPTION6_IAADDR
, 24))
1167 struct dhcp_lease
*lease
;
1168 struct in6_addr
*addrp
= opt6_ptr(ia_option
, 0);
1170 if (have_config(config
, CONFIG_ADDR6
) && IN6_ARE_ADDR_EQUAL(&config
->addr6
, addrp
))
1172 prettyprint_time(daemon
->dhcp_buff3
, DECLINE_BACKOFF
);
1173 inet_ntop(AF_INET6
, addrp
, daemon
->addrbuff
, ADDRSTRLEN
);
1174 my_syslog(MS_DHCP
| LOG_WARNING
, _("disabling DHCP static address %s for %s"),
1175 daemon
->addrbuff
, daemon
->dhcp_buff3
);
1176 config
->flags
|= CONFIG_DECLINED
;
1177 config
->decline_time
= now
;
1180 /* make sure this host gets a different address next time. */
1181 for (; context
; context
= context
->current
)
1182 context
->addr_epoch
++;
1184 if ((lease
= lease6_find(state
.clid
, state
.clid_len
, state
.ia_type
== OPTION6_IA_NA
? LEASE_NA
: LEASE_TA
,
1185 state
.iaid
, opt6_ptr(ia_option
, 0))))
1186 lease_prune(lease
, now
);
1191 o
= new_opt6(state
.ia_type
);
1192 put_opt6_long(state
.iaid
);
1193 if (state
.ia_type
== OPTION6_IA_NA
)
1201 o1
= new_opt6(OPTION6_IAADDR
);
1202 put_opt6(opt6_ptr(ia_option
, 0), IN6ADDRSZ
);
1211 o1
= new_opt6(OPTION6_STATUS_CODE
);
1212 put_opt6_short(DHCP6NOBINDING
);
1213 put_opt6_string(_("no binding found"));
1225 log_tags(tagif
, state
.xid
);
1227 if (option_bool(OPT_LOG_OPTS
))
1228 log6_opts(0, state
.xid
, daemon
->outpacket
.iov_base
+ start_opts
, daemon
->outpacket
.iov_base
+ save_counter(-1));
1234 static struct dhcp_netid
*add_options(struct state
*state
,
1235 struct in6_addr
*fallback
,
1236 struct dhcp_context
*context
,
1240 /* filter options based on tags, those we want get DHOPT_TAGOK bit set */
1241 struct dhcp_netid
*tagif
= option_filter(state
->tags
, state
->context_tags
, daemon
->dhcp_opts6
);
1242 struct dhcp_opt
*opt_cfg
;
1243 int done_dns
= 0, done_refresh
= !do_refresh
, do_encap
= 0;
1246 oro
= opt6_find(state
->packet_options
, state
->end
, OPTION6_ORO
, 0);
1248 for (opt_cfg
= daemon
->dhcp_opts6
; opt_cfg
; opt_cfg
= opt_cfg
->next
)
1250 /* netids match and not encapsulated? */
1251 if (!(opt_cfg
->flags
& DHOPT_TAGOK
))
1254 if (!(opt_cfg
->flags
& DHOPT_FORCE
) && oro
)
1256 for (i
= 0; i
< opt6_len(oro
) - 1; i
+= 2)
1257 if (opt6_uint(oro
, i
, 2) == (unsigned)opt_cfg
->opt
)
1260 /* option not requested */
1261 if (i
>= opt6_len(oro
) - 1)
1265 if (opt_cfg
->opt
== OPTION6_DNS_SERVER
)
1268 if (opt_cfg
->len
== 0)
1272 if (opt_cfg
->opt
== OPTION6_REFRESH_TIME
)
1275 o
= new_opt6(opt_cfg
->opt
);
1276 if (opt_cfg
->flags
& DHOPT_ADDR6
)
1279 struct in6_addr
*a
= (struct in6_addr
*)opt_cfg
->val
;
1280 for (j
= 0; j
< opt_cfg
->len
; j
+=IN6ADDRSZ
, a
++)
1282 /* zero means "self" (but not in vendorclass options.) */
1283 if (IN6_IS_ADDR_UNSPECIFIED(a
))
1285 if (!add_local_addrs(context
))
1286 put_opt6(fallback
, IN6ADDRSZ
);
1289 put_opt6(a
, IN6ADDRSZ
);
1292 else if (opt_cfg
->val
)
1293 put_opt6(opt_cfg
->val
, opt_cfg
->len
);
1297 if (daemon
->port
== NAMESERVER_PORT
&& !done_dns
)
1299 o
= new_opt6(OPTION6_DNS_SERVER
);
1300 if (!add_local_addrs(context
))
1301 put_opt6(fallback
, IN6ADDRSZ
);
1305 if (context
&& !done_refresh
)
1307 struct dhcp_context
*c
;
1308 unsigned int lease_time
= 0xffffffff;
1310 /* Find the smallest lease tie of all contexts,
1311 subjext to the RFC-4242 stipulation that this must not
1312 be less than 600. */
1313 for (c
= context
; c
; c
= c
->next
)
1314 if (c
->lease_time
< lease_time
)
1316 if (c
->lease_time
< 600)
1319 lease_time
= c
->lease_time
;
1322 o
= new_opt6(OPTION6_REFRESH_TIME
);
1323 put_opt6_long(lease_time
);
1327 /* handle vendor-identifying vendor-encapsulated options,
1328 dhcp-option = vi-encap:13,17,....... */
1329 for (opt_cfg
= daemon
->dhcp_opts6
; opt_cfg
; opt_cfg
= opt_cfg
->next
)
1330 opt_cfg
->flags
&= ~DHOPT_ENCAP_DONE
;
1333 for (i
= 0; i
< opt6_len(oro
) - 1; i
+= 2)
1334 if (opt6_uint(oro
, i
, 2) == OPTION6_VENDOR_OPTS
)
1337 for (opt_cfg
= daemon
->dhcp_opts6
; opt_cfg
; opt_cfg
= opt_cfg
->next
)
1339 if (opt_cfg
->flags
& DHOPT_RFC3925
)
1342 struct dhcp_opt
*oc
;
1344 if (opt_cfg
->flags
& DHOPT_ENCAP_DONE
)
1347 for (oc
= daemon
->dhcp_opts6
; oc
; oc
= oc
->next
)
1349 oc
->flags
&= ~DHOPT_ENCAP_MATCH
;
1351 if (!(oc
->flags
& DHOPT_RFC3925
) || opt_cfg
->u
.encap
!= oc
->u
.encap
)
1354 oc
->flags
|= DHOPT_ENCAP_DONE
;
1355 if (match_netid(oc
->netid
, tagif
, 1))
1357 /* option requested/forced? */
1358 if (!oro
|| do_encap
|| (oc
->flags
& DHOPT_FORCE
))
1360 oc
->flags
|= DHOPT_ENCAP_MATCH
;
1368 o
= new_opt6(OPTION6_VENDOR_OPTS
);
1369 put_opt6_long(opt_cfg
->u
.encap
);
1371 for (oc
= daemon
->dhcp_opts6
; oc
; oc
= oc
->next
)
1372 if (oc
->flags
& DHOPT_ENCAP_MATCH
)
1374 o1
= new_opt6(oc
->opt
);
1375 put_opt6(oc
->val
, oc
->len
);
1384 if (state
->hostname
)
1387 size_t len
= strlen(state
->hostname
);
1389 if (state
->send_domain
)
1390 len
+= strlen(state
->send_domain
) + 2;
1392 o
= new_opt6(OPTION6_FQDN
);
1393 if ((p
= expand(len
+ 2)))
1395 *(p
++) = state
->fqdn_flags
;
1396 p
= do_rfc1035_name(p
, state
->hostname
);
1397 if (state
->send_domain
)
1399 p
= do_rfc1035_name(p
, state
->send_domain
);
1408 if (option_bool(OPT_LOG_OPTS
) && oro
)
1410 char *q
= daemon
->namebuff
;
1411 for (i
= 0; i
< opt6_len(oro
) - 1; i
+= 2)
1413 char *s
= option_string(AF_INET6
, opt6_uint(oro
, i
, 2), NULL
, 0, NULL
, 0);
1414 q
+= snprintf(q
, MAXDNAME
- (q
- daemon
->namebuff
),
1416 opt6_uint(oro
, i
, 2),
1417 strlen(s
) != 0 ? ":" : "",
1419 (i
> opt6_len(oro
) - 3) ? "" : ", ");
1420 if ( i
> opt6_len(oro
) - 3 || (q
- daemon
->namebuff
) > 40)
1422 q
= daemon
->namebuff
;
1423 my_syslog(MS_DHCP
| LOG_INFO
, _("%u requested options: %s"), state
->xid
, daemon
->namebuff
);
1431 static int add_local_addrs(struct dhcp_context
*context
)
1435 for (; context
; context
= context
->current
)
1436 if ((context
->flags
& CONTEXT_USED
) && !IN6_IS_ADDR_UNSPECIFIED(&context
->local6
))
1438 /* squash duplicates */
1439 struct dhcp_context
*c
;
1440 for (c
= context
->current
; c
; c
= c
->current
)
1441 if ((c
->flags
& CONTEXT_USED
) &&
1442 IN6_ARE_ADDR_EQUAL(&context
->local6
, &c
->local6
))
1448 put_opt6(&context
->local6
, IN6ADDRSZ
);
1456 static void get_context_tag(struct state
*state
, struct dhcp_context
*context
)
1458 /* get tags from context if we've not used it before */
1459 if (context
->netid
.next
== &context
->netid
&& context
->netid
.net
)
1461 context
->netid
.next
= state
->context_tags
;
1462 state
->context_tags
= &context
->netid
;
1463 if (!state
->hostname_auth
)
1465 struct dhcp_netid_list
*id_list
;
1467 for (id_list
= daemon
->dhcp_ignore_names
; id_list
; id_list
= id_list
->next
)
1468 if ((!id_list
->list
) || match_netid(id_list
->list
, &context
->netid
, 0))
1471 state
->hostname
= NULL
;
1476 #ifdef OPTION6_PREFIX_CLASS
1477 static struct prefix_class
*prefix_class_from_context(struct dhcp_context
*context
)
1479 struct prefix_class
*p
;
1480 struct dhcp_netid
*t
;
1482 for (p
= daemon
->prefix_classes
; p
; p
= p
->next
)
1483 for (t
= context
->filter
; t
; t
= t
->next
)
1484 if (strcmp(p
->tag
.net
, t
->net
) == 0)
1491 static int check_ia(struct state
*state
, void *opt
, void **endp
, void **ia_option
)
1493 state
->ia_type
= opt6_type(opt
);
1496 if (state
->ia_type
!= OPTION6_IA_NA
&& state
->ia_type
!= OPTION6_IA_TA
)
1499 if (state
->ia_type
== OPTION6_IA_NA
&& opt6_len(opt
) < 12)
1502 if (state
->ia_type
== OPTION6_IA_TA
&& opt6_len(opt
) < 4)
1505 *endp
= opt6_ptr(opt
, opt6_len(opt
));
1506 state
->iaid
= opt6_uint(opt
, 0, 4);
1507 *ia_option
= opt6_find(opt6_ptr(opt
, state
->ia_type
== OPTION6_IA_NA
? 12 : 4), *endp
, OPTION6_IAADDR
, 24);
1513 static int build_ia(struct state
*state
, int *t1cntr
)
1515 int o
= new_opt6(state
->ia_type
);
1517 put_opt6_long(state
->iaid
);
1520 if (state
->ia_type
== OPTION6_IA_NA
)
1523 *t1cntr
= save_counter(-1);
1524 /* so we can fill these in later */
1532 static void end_ia(int t1cntr
, unsigned int min_time
, int do_fuzz
)
1536 /* go back an fill in fields in IA_NA option */
1537 int sav
= save_counter(t1cntr
);
1538 unsigned int t1
, t2
, fuzz
= 0;
1544 while (fuzz
> (min_time
/16))
1548 t1
= (min_time
== 0xffffffff) ? 0xffffffff : min_time
/2 - fuzz
;
1549 t2
= (min_time
== 0xffffffff) ? 0xffffffff : ((min_time
/8)*7) - fuzz
;
1556 static void add_address(struct state
*state
, struct dhcp_context
*context
, unsigned int lease_time
, void *ia_option
,
1557 unsigned int *min_time
, struct in6_addr
*addr
, time_t now
)
1559 unsigned int valid_time
= 0, preferred_time
= 0;
1560 int o
= new_opt6(OPTION6_IAADDR
);
1561 struct dhcp_lease
*lease
;
1563 /* get client requested times */
1566 preferred_time
= opt6_uint(ia_option
, 16, 4);
1567 valid_time
= opt6_uint(ia_option
, 20, 4);
1570 calculate_times(context
, min_time
, &valid_time
, &preferred_time
, lease_time
);
1572 put_opt6(addr
, sizeof(*addr
));
1573 put_opt6_long(preferred_time
);
1574 put_opt6_long(valid_time
);
1576 #ifdef OPTION6_PREFIX_CLASS
1577 if (state
->send_prefix_class
)
1579 int o1
= new_opt6(OPTION6_PREFIX_CLASS
);
1580 put_opt6_short(state
->send_prefix_class
->class);
1587 if (state
->lease_allocate
)
1588 update_leases(state
, context
, addr
, valid_time
, now
);
1590 if ((lease
= lease6_find_by_addr(addr
, 128, 0)))
1591 lease
->flags
|= LEASE_USED
;
1593 /* get tags from context if we've not used it before */
1594 if (context
->netid
.next
== &context
->netid
&& context
->netid
.net
)
1596 context
->netid
.next
= state
->context_tags
;
1597 state
->context_tags
= &context
->netid
;
1599 if (!state
->hostname_auth
)
1601 struct dhcp_netid_list
*id_list
;
1603 for (id_list
= daemon
->dhcp_ignore_names
; id_list
; id_list
= id_list
->next
)
1604 if ((!id_list
->list
) || match_netid(id_list
->list
, &context
->netid
, 0))
1607 state
->hostname
= NULL
;
1611 #ifdef HAVE_QUIET_DHCP
1612 if (!option_bool(OPT_QUIET_DHCP6
))
1614 log6_packet(state
, state
->lease_allocate
? "DHCPREPLY" : "DHCPADVERTISE", addr
, state
->hostname
);
1617 static void mark_context_used(struct state
*state
, struct dhcp_context
*context
, struct in6_addr
*addr
)
1619 /* Mark that we have an address for this prefix. */
1620 #ifdef OPTION6_PREFIX_CLASS
1621 for (; context
; context
= context
->current
)
1622 if (is_same_net6(addr
, &context
->start6
, context
->prefix
) &&
1623 (!state
->send_prefix_class
|| state
->send_prefix_class
== prefix_class_from_context(context
)))
1624 context
->flags
|= CONTEXT_USED
;
1626 (void)state
; /* warning */
1627 for (; context
; context
= context
->current
)
1628 if (is_same_net6(addr
, &context
->start6
, context
->prefix
))
1629 context
->flags
|= CONTEXT_USED
;
1633 static void mark_config_used(struct dhcp_context
*context
, struct in6_addr
*addr
)
1635 for (; context
; context
= context
->current
)
1636 if (is_same_net6(addr
, &context
->start6
, context
->prefix
))
1637 context
->flags
|= CONTEXT_CONF_USED
;
1640 /* make sure address not leased to another CLID/IAID */
1641 static int check_address(struct state
*state
, struct in6_addr
*addr
)
1643 struct dhcp_lease
*lease
;
1645 if (!(lease
= lease6_find_by_addr(addr
, 128, 0)))
1648 if (lease
->clid_len
!= state
->clid_len
||
1649 memcmp(lease
->clid
, state
->clid
, state
->clid_len
) != 0 ||
1650 lease
->hwaddr_type
!= state
->iaid
)
1657 /* Calculate valid and preferred times to send in leases/renewals.
1661 *valid_timep, *preferred_timep - requested times from IAADDR options.
1662 context->valid, context->preferred - times associated with subnet address on local interface.
1663 context->flags | CONTEXT_DEPRECATE - "deprecated" flag in dhcp-range.
1664 lease_time - configured time for context for individual client.
1665 *min_time - smallest valid time sent so far.
1669 *valid_timep, *preferred_timep - times to be send in IAADDR option.
1670 *min_time - smallest valid time sent so far, to calculate T1 and T2.
1673 static void calculate_times(struct dhcp_context
*context
, unsigned int *min_time
, unsigned int *valid_timep
,
1674 unsigned int *preferred_timep
, unsigned int lease_time
)
1676 unsigned int req_preferred
= *preferred_timep
, req_valid
= *valid_timep
;
1677 unsigned int valid_time
= lease_time
, preferred_time
= lease_time
;
1679 /* RFC 3315: "A server ignores the lifetimes set
1680 by the client if the preferred lifetime is greater than the valid
1682 if (req_preferred
<= req_valid
)
1684 if (req_preferred
!= 0)
1686 /* 0 == "no preference from client" */
1687 if (req_preferred
< 120u)
1688 req_preferred
= 120u; /* sanity */
1690 if (req_preferred
< preferred_time
)
1691 preferred_time
= req_preferred
;
1695 /* 0 == "no preference from client" */
1697 if (req_valid
< 120u)
1698 req_valid
= 120u; /* sanity */
1700 if (req_valid
< valid_time
)
1701 valid_time
= req_valid
;
1705 /* deprecate (preferred == 0) which configured, or when local address
1707 if ((context
->flags
& CONTEXT_DEPRECATE
) || context
->preferred
== 0)
1710 if (preferred_time
!= 0 && preferred_time
< *min_time
)
1711 *min_time
= preferred_time
;
1713 if (valid_time
!= 0 && valid_time
< *min_time
)
1714 *min_time
= valid_time
;
1716 *valid_timep
= valid_time
;
1717 *preferred_timep
= preferred_time
;
1720 static void update_leases(struct state
*state
, struct dhcp_context
*context
, struct in6_addr
*addr
, unsigned int lease_time
, time_t now
)
1722 struct dhcp_lease
*lease
= lease6_find_by_addr(addr
, 128, 0);
1723 struct dhcp_netid
*tagif
= run_tag_if(state
->tags
);
1726 lease
= lease6_allocate(addr
, state
->ia_type
== OPTION6_IA_NA
? LEASE_NA
: LEASE_TA
);
1730 lease_set_expires(lease
, lease_time
, now
);
1731 lease_set_hwaddr(lease
, NULL
, state
->clid
, 0, state
->iaid
, state
->clid_len
, now
, 0);
1732 lease_set_interface(lease
, state
->interface
, now
);
1733 if (state
->hostname
&& state
->ia_type
== OPTION6_IA_NA
)
1735 char *addr_domain
= get_domain6(addr
);
1736 if (!state
->send_domain
)
1737 state
->send_domain
= addr_domain
;
1738 lease_set_hostname(lease
, state
->hostname
, state
->hostname_auth
, addr_domain
, state
->domain
);
1742 if (daemon
->lease_change_command
)
1745 lease
->flags
|= LEASE_CHANGED
;
1746 free(lease
->extradata
);
1747 lease
->extradata
= NULL
;
1748 lease
->extradata_size
= lease
->extradata_len
= 0;
1749 lease
->vendorclass_count
= 0;
1751 if ((class_opt
= opt6_find(state
->packet_options
, state
->end
, OPTION6_VENDOR_CLASS
, 4)))
1753 void *enc_opt
, *enc_end
= opt6_ptr(class_opt
, opt6_len(class_opt
));
1754 lease
->vendorclass_count
++;
1755 /* send enterprise number first */
1756 sprintf(daemon
->dhcp_buff2
, "%u", opt6_uint(class_opt
, 0, 4));
1757 lease_add_extradata(lease
, (unsigned char *)daemon
->dhcp_buff2
, strlen(daemon
->dhcp_buff2
), 0);
1759 if (opt6_len(class_opt
) >= 6)
1760 for (enc_opt
= opt6_ptr(class_opt
, 4); enc_opt
; enc_opt
= opt6_next(enc_opt
, enc_end
))
1762 lease
->vendorclass_count
++;
1763 lease_add_extradata(lease
, opt6_ptr(enc_opt
, 0), opt6_len(enc_opt
), 0);
1767 lease_add_extradata(lease
, (unsigned char *)state
->client_hostname
,
1768 state
->client_hostname
? strlen(state
->client_hostname
) : 0, 0);
1770 /* space-concat tag set */
1771 if (!tagif
&& !context
->netid
.net
)
1772 lease_add_extradata(lease
, NULL
, 0, 0);
1775 if (context
->netid
.net
)
1776 lease_add_extradata(lease
, (unsigned char *)context
->netid
.net
, strlen(context
->netid
.net
), tagif
? ' ' : 0);
1780 struct dhcp_netid
*n
;
1781 for (n
= tagif
; n
; n
= n
->next
)
1783 struct dhcp_netid
*n1
;
1785 for (n1
= n
->next
; n1
; n1
= n1
->next
)
1786 if (strcmp(n
->net
, n1
->net
) == 0)
1789 lease_add_extradata(lease
, (unsigned char *)n
->net
, strlen(n
->net
), n
->next
? ' ' : 0);
1794 if (state
->link_address
)
1795 inet_ntop(AF_INET6
, state
->link_address
, daemon
->addrbuff
, ADDRSTRLEN
);
1797 lease_add_extradata(lease
, (unsigned char *)daemon
->addrbuff
, state
->link_address
? strlen(daemon
->addrbuff
) : 0, 0);
1799 if ((class_opt
= opt6_find(state
->packet_options
, state
->end
, OPTION6_USER_CLASS
, 2)))
1801 void *enc_opt
, *enc_end
= opt6_ptr(class_opt
, opt6_len(class_opt
));
1802 for (enc_opt
= opt6_ptr(class_opt
, 0); enc_opt
; enc_opt
= opt6_next(enc_opt
, enc_end
))
1803 lease_add_extradata(lease
, opt6_ptr(enc_opt
, 0), opt6_len(enc_opt
), 0);
1813 static void log6_opts(int nest
, unsigned int xid
, void *start_opts
, void *end_opts
)
1816 char *desc
= nest
? "nest" : "sent";
1818 if (start_opts
== end_opts
)
1821 for (opt
= start_opts
; opt
; opt
= opt6_next(opt
, end_opts
))
1823 int type
= opt6_type(opt
);
1824 void *ia_options
= NULL
;
1827 if (type
== OPTION6_IA_NA
)
1829 sprintf(daemon
->namebuff
, "IAID=%u T1=%u T2=%u",
1830 opt6_uint(opt
, 0, 4), opt6_uint(opt
, 4, 4), opt6_uint(opt
, 8, 4));
1832 ia_options
= opt6_ptr(opt
, 12);
1834 else if (type
== OPTION6_IA_TA
)
1836 sprintf(daemon
->namebuff
, "IAID=%u", opt6_uint(opt
, 0, 4));
1838 ia_options
= opt6_ptr(opt
, 4);
1840 else if (type
== OPTION6_IAADDR
)
1842 inet_ntop(AF_INET6
, opt6_ptr(opt
, 0), daemon
->addrbuff
, ADDRSTRLEN
);
1843 sprintf(daemon
->namebuff
, "%s PL=%u VL=%u",
1844 daemon
->addrbuff
, opt6_uint(opt
, 16, 4), opt6_uint(opt
, 20, 4));
1846 ia_options
= opt6_ptr(opt
, 24);
1848 #ifdef OPTION6_PREFIX_CLASS
1849 else if (type
== OPTION6_PREFIX_CLASS
)
1851 optname
= "prefix-class";
1852 sprintf(daemon
->namebuff
, "class=%u", opt6_uint(opt
, 0, 2));
1855 else if (type
== OPTION6_STATUS_CODE
)
1857 int len
= sprintf(daemon
->namebuff
, "%u ", opt6_uint(opt
, 0, 2));
1858 memcpy(daemon
->namebuff
+ len
, opt6_ptr(opt
, 2), opt6_len(opt
)-2);
1859 daemon
->namebuff
[len
+ opt6_len(opt
) - 2] = 0;
1864 /* account for flag byte on FQDN */
1865 int offset
= type
== OPTION6_FQDN
? 1 : 0;
1866 optname
= option_string(AF_INET6
, type
, opt6_ptr(opt
, offset
), opt6_len(opt
) - offset
, daemon
->namebuff
, MAXDNAME
);
1869 my_syslog(MS_DHCP
| LOG_INFO
, "%u %s size:%3d option:%3d %s %s",
1870 xid
, desc
, opt6_len(opt
), type
, optname
, daemon
->namebuff
);
1873 log6_opts(1, xid
, ia_options
, opt6_ptr(opt
, opt6_len(opt
)));
1877 static void log6_packet(struct state
*state
, char *type
, struct in6_addr
*addr
, char *string
)
1879 int clid_len
= state
->clid_len
;
1881 /* avoid buffer overflow */
1885 print_mac(daemon
->namebuff
, state
->clid
, clid_len
);
1889 inet_ntop(AF_INET6
, addr
, daemon
->dhcp_buff2
, 255);
1890 strcat(daemon
->dhcp_buff2
, " ");
1893 daemon
->dhcp_buff2
[0] = 0;
1895 if(option_bool(OPT_LOG_OPTS
))
1896 my_syslog(MS_DHCP
| LOG_INFO
, "%u %s(%s) %s%s %s",
1902 string
? string
: "");
1904 my_syslog(MS_DHCP
| LOG_INFO
, "%s(%s) %s%s %s",
1909 string
? string
: "");
1912 static void *opt6_find (void *opts
, void *end
, unsigned int search
, unsigned int minsize
)
1926 GETSHORT(opt
, opts
);
1927 GETSHORT(opt_len
, opts
);
1929 if (opt_len
> (end
- opts
))
1932 if (opt
== search
&& (opt_len
>= minsize
))
1939 static void *opt6_next(void *opts
, void *end
)
1947 GETSHORT(opt_len
, opts
);
1949 if (opt_len
>= (end
- opts
))
1952 return opts
+ opt_len
;
1955 static unsigned int opt6_uint(unsigned char *opt
, int offset
, int size
)
1957 /* this worries about unaligned data and byte order */
1958 unsigned int ret
= 0;
1960 unsigned char *p
= opt6_ptr(opt
, offset
);
1962 for (i
= 0; i
< size
; i
++)
1963 ret
= (ret
<< 8) | *p
++;
1968 void relay_upstream6(struct dhcp_relay
*relay
, ssize_t sz
, struct in6_addr
*peer_address
, u32 scope_id
)
1970 /* ->local is same value for all relays on ->current chain */
1972 struct all_addr from
;
1973 unsigned char *header
;
1974 unsigned char *inbuff
= daemon
->dhcp_packet
.iov_base
;
1975 int msg_type
= *inbuff
;
1977 struct in6_addr multicast
;
1979 inet_pton(AF_INET6
, ALL_SERVERS
, &multicast
);
1981 /* source address == relay address */
1982 from
.addr
.addr6
= relay
->local
.addr
.addr6
;
1984 /* Get hop count from nested relayed message */
1985 if (msg_type
== DHCP6RELAYFORW
)
1986 hopcount
= *((unsigned char *)inbuff
+1) + 1;
1990 /* RFC 3315 HOP_COUNT_LIMIT */
1996 if ((header
= put_opt6(NULL
, 34)))
2000 header
[0] = DHCP6RELAYFORW
;
2001 header
[1] = hopcount
;
2002 memcpy(&header
[2], &relay
->local
.addr
.addr6
, IN6ADDRSZ
);
2003 memcpy(&header
[18], peer_address
, IN6ADDRSZ
);
2005 o
= new_opt6(OPTION6_RELAY_MSG
);
2006 put_opt6(inbuff
, sz
);
2009 for (; relay
; relay
= relay
->current
)
2011 union mysockaddr to
;
2013 to
.sa
.sa_family
= AF_INET6
;
2014 to
.in6
.sin6_addr
= relay
->server
.addr
.addr6
;
2015 to
.in6
.sin6_port
= htons(DHCPV6_SERVER_PORT
);
2016 to
.in6
.sin6_flowinfo
= 0;
2017 to
.in6
.sin6_scope_id
= 0;
2019 if (IN6_ARE_ADDR_EQUAL(&relay
->server
.addr
.addr6
, &multicast
))
2021 int multicast_iface
;
2022 if (!relay
->interface
|| strchr(relay
->interface
, '*') ||
2023 (multicast_iface
= if_nametoindex(relay
->interface
)) == 0 ||
2024 setsockopt(daemon
->dhcp6fd
, IPPROTO_IPV6
, IPV6_MULTICAST_IF
, &multicast_iface
, sizeof(multicast_iface
)) == -1)
2025 my_syslog(MS_DHCP
| LOG_ERR
, _("Cannot multicast to DHCPv6 server without correct interface"));
2028 send_from(daemon
->dhcp6fd
, 0, daemon
->outpacket
.iov_base
, save_counter(0), &to
, &from
, 0);
2030 if (option_bool(OPT_LOG_OPTS
))
2032 inet_ntop(AF_INET6
, &relay
->local
, daemon
->addrbuff
, ADDRSTRLEN
);
2033 inet_ntop(AF_INET6
, &relay
->server
, daemon
->namebuff
, ADDRSTRLEN
);
2034 my_syslog(MS_DHCP
| LOG_INFO
, _("DHCP relay %s -> %s"), daemon
->addrbuff
, daemon
->namebuff
);
2037 /* Save this for replies */
2038 relay
->iface_index
= scope_id
;
2043 unsigned short relay_reply6(struct sockaddr_in6
*peer
, ssize_t sz
, char *arrival_interface
)
2045 struct dhcp_relay
*relay
;
2046 struct in6_addr link
;
2047 unsigned char *inbuff
= daemon
->dhcp_packet
.iov_base
;
2049 /* must have at least msg_type+hopcount+link_address+peer_address+minimal size option
2050 which is 1 + 1 + 16 + 16 + 2 + 2 = 38 */
2052 if (sz
< 38 || *inbuff
!= DHCP6RELAYREPL
)
2055 memcpy(&link
, &inbuff
[2], IN6ADDRSZ
);
2057 for (relay
= daemon
->relay6
; relay
; relay
= relay
->next
)
2058 if (IN6_ARE_ADDR_EQUAL(&link
, &relay
->local
.addr
.addr6
) &&
2059 (!relay
->interface
|| wildcard_match(relay
->interface
, arrival_interface
)))
2066 void *opt
, *opts
= inbuff
+ 34;
2067 void *end
= inbuff
+ sz
;
2068 for (opt
= opts
; opt
; opt
= opt6_next(opt
, end
))
2069 if (opt6_type(opt
) == OPTION6_RELAY_MSG
&& opt6_len(opt
) > 0)
2071 int encap_type
= *((unsigned char *)opt6_ptr(opt
, 0));
2072 put_opt6(opt6_ptr(opt
, 0), opt6_len(opt
));
2073 memcpy(&peer
->sin6_addr
, &inbuff
[18], IN6ADDRSZ
);
2074 peer
->sin6_scope_id
= relay
->iface_index
;
2075 return encap_type
== DHCP6RELAYREPL
? DHCPV6_SERVER_PORT
: DHCPV6_CLIENT_PORT
;