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