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