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