Simon Kelley | 6174435 | 2013-01-31 14:34:40 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2013 Simon Kelley |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +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 |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 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. |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 12 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 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/>. |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 17 | #include "dnsmasq.h" |
| 18 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 19 | #ifdef HAVE_DHCP |
| 20 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 21 | #define option_len(opt) ((int)(((unsigned char *)(opt))[1])) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 22 | #define option_ptr(opt, i) ((void *)&(((unsigned char *)(opt))[2u+(unsigned int)(i)])) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 23 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 24 | #ifdef HAVE_SCRIPT |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 25 | static void add_extradata_opt(struct dhcp_lease *lease, unsigned char *opt); |
| 26 | #endif |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 27 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 28 | static int sanitise(unsigned char *opt, char *buf); |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 29 | static struct in_addr server_id(struct dhcp_context *context, struct in_addr override, struct in_addr fallback); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 30 | static unsigned int calc_time(struct dhcp_context *context, struct dhcp_config *config, unsigned char *opt); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 31 | static void option_put(struct dhcp_packet *mess, unsigned char *end, int opt, int len, unsigned int val); |
| 32 | static void option_put_string(struct dhcp_packet *mess, unsigned char *end, |
| 33 | int opt, char *string, int null_term); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 34 | static struct in_addr option_addr(unsigned char *opt); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 35 | static unsigned int option_uint(unsigned char *opt, int i, int size); |
| 36 | static void log_packet(char *type, void *addr, unsigned char *ext_mac, |
| 37 | int mac_len, char *interface, char *string, u32 xid); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 38 | static unsigned char *option_find(struct dhcp_packet *mess, size_t size, int opt_type, int minsize); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 39 | static unsigned char *option_find1(unsigned char *p, unsigned char *end, int opt, int minsize); |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 40 | static size_t dhcp_packet_size(struct dhcp_packet *mess, unsigned char *agent_id, unsigned char *real_end); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 41 | static void clear_packet(struct dhcp_packet *mess, unsigned char *end); |
Simon Kelley | aa63a21 | 2013-04-22 15:01:52 +0100 | [diff] [blame] | 42 | static int in_list(unsigned char *list, int opt); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 43 | static void do_options(struct dhcp_context *context, |
| 44 | struct dhcp_packet *mess, |
| 45 | unsigned char *real_end, |
| 46 | unsigned char *req_options, |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 47 | char *hostname, |
Simon Kelley | 70c5e3e | 2012-02-06 22:05:15 +0000 | [diff] [blame] | 48 | char *config_domain, |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 49 | struct dhcp_netid *netid, |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 50 | struct in_addr subnet_addr, |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 51 | unsigned char fqdn_flags, |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 52 | int null_term, int pxearch, |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 53 | unsigned char *uuid, |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 54 | int vendor_class_len, |
| 55 | time_t now); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 56 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 57 | |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 58 | static void match_vendor_opts(unsigned char *opt, struct dhcp_opt *dopt); |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 59 | static int do_encap_opts(struct dhcp_opt *opts, int encap, int flag, struct dhcp_packet *mess, unsigned char *end, int null_term); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 60 | static void pxe_misc(struct dhcp_packet *mess, unsigned char *end, unsigned char *uuid); |
| 61 | static int prune_vendor_opts(struct dhcp_netid *netid); |
Simon Kelley | 751d6f4 | 2012-02-10 15:24:51 +0000 | [diff] [blame] | 62 | static struct dhcp_opt *pxe_opts(int pxe_arch, struct dhcp_netid *netid, struct in_addr local, time_t now); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 63 | struct dhcp_boot *find_boot(struct dhcp_netid *netid); |
| 64 | |
| 65 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 66 | size_t dhcp_reply(struct dhcp_context *context, char *iface_name, int int_index, |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 67 | size_t sz, time_t now, int unicast_dest, int *is_inform, int pxe, struct in_addr fallback) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 68 | { |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 69 | unsigned char *opt, *clid = NULL; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 70 | struct dhcp_lease *ltmp, *lease = NULL; |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 71 | struct dhcp_vendor *vendor; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 72 | struct dhcp_mac *mac; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 73 | struct dhcp_netid_list *id_list; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 74 | int clid_len = 0, ignore = 0, do_classes = 0, selecting = 0, pxearch = -1; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 75 | struct dhcp_packet *mess = (struct dhcp_packet *)daemon->dhcp_packet.iov_base; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 76 | unsigned char *end = (unsigned char *)(mess + 1); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 77 | unsigned char *real_end = (unsigned char *)(mess + 1); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 78 | char *hostname = NULL, *offer_hostname = NULL, *client_hostname = NULL, *domain = NULL; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 79 | int hostname_auth = 0, borken_opt = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 80 | unsigned char *req_options = NULL; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 81 | char *message = NULL; |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 82 | unsigned int time; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 83 | struct dhcp_config *config; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 84 | struct dhcp_netid *netid, *tagif_netid; |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 85 | struct in_addr subnet_addr, override; |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 86 | unsigned short fuzz = 0; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 87 | unsigned int mess_type = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 88 | unsigned char fqdn_flags = 0; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 89 | unsigned char *agent_id = NULL, *uuid = NULL; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 90 | unsigned char *emac = NULL; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 91 | int vendor_class_len = 0, emac_len = 0; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 92 | struct dhcp_netid known_id, iface_id, cpewan_id; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 93 | struct dhcp_opt *o; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 94 | unsigned char pxe_uuid[17]; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 95 | unsigned char *oui = NULL, *serial = NULL, *class = NULL; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 96 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 97 | subnet_addr.s_addr = override.s_addr = 0; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 98 | |
| 99 | /* set tag with name == interface */ |
| 100 | iface_id.net = iface_name; |
| 101 | iface_id.next = NULL; |
| 102 | netid = &iface_id; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 103 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 104 | if (mess->op != BOOTREQUEST || mess->hlen > DHCP_CHADDR_MAX) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 105 | return 0; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 106 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 107 | if (mess->htype == 0 && mess->hlen != 0) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 108 | return 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 109 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 110 | /* check for DHCP rather than BOOTP */ |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 111 | if ((opt = option_find(mess, sz, OPTION_MESSAGE_TYPE, 1))) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 112 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 113 | u32 cookie = htonl(DHCP_COOKIE); |
| 114 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 115 | /* only insist on a cookie for DHCP. */ |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 116 | if (memcmp(mess->options, &cookie, sizeof(u32)) != 0) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 117 | return 0; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 118 | |
| 119 | mess_type = option_uint(opt, 0, 1); |
| 120 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 121 | /* two things to note here: expand_buf may move the packet, |
| 122 | so reassign mess from daemon->packet. Also, the size |
| 123 | sent includes the IP and UDP headers, hence the magic "-28" */ |
| 124 | if ((opt = option_find(mess, sz, OPTION_MAXMESSAGE, 2))) |
| 125 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 126 | size_t size = (size_t)option_uint(opt, 0, 2) - 28; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 127 | |
| 128 | if (size > DHCP_PACKET_MAX) |
| 129 | size = DHCP_PACKET_MAX; |
| 130 | else if (size < sizeof(struct dhcp_packet)) |
| 131 | size = sizeof(struct dhcp_packet); |
| 132 | |
| 133 | if (expand_buf(&daemon->dhcp_packet, size)) |
| 134 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 135 | mess = (struct dhcp_packet *)daemon->dhcp_packet.iov_base; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 136 | real_end = end = ((unsigned char *)mess) + size; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 140 | /* Some buggy clients set ciaddr when they shouldn't, so clear that here since |
| 141 | it can affect the context-determination code. */ |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 142 | if ((option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ) || mess_type == DHCPDISCOVER)) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 143 | mess->ciaddr.s_addr = 0; |
| 144 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 145 | /* search for device identity from CPEWAN devices, we pass this through to the script */ |
| 146 | if ((opt = option_find(mess, sz, OPTION_VENDOR_IDENT_OPT, 5))) |
| 147 | { |
| 148 | unsigned int elen, offset, len = option_len(opt); |
| 149 | |
| 150 | for (offset = 0; offset < (len - 5); offset += elen + 5) |
| 151 | { |
| 152 | elen = option_uint(opt, offset + 4 , 1); |
| 153 | if (option_uint(opt, offset, 4) == BRDBAND_FORUM_IANA) |
| 154 | { |
| 155 | unsigned char *x = option_ptr(opt, offset + 5); |
| 156 | unsigned char *y = option_ptr(opt, offset + elen + 5); |
| 157 | oui = option_find1(x, y, 1, 1); |
| 158 | serial = option_find1(x, y, 2, 1); |
| 159 | class = option_find1(x, y, 3, 1); |
| 160 | |
| 161 | /* If TR069-id is present set the tag "cpewan-id" to facilitate echoing |
| 162 | the gateway id back. Note that the device class is optional */ |
| 163 | if (oui && serial) |
| 164 | { |
| 165 | cpewan_id.net = "cpewan-id"; |
| 166 | cpewan_id.next = netid; |
| 167 | netid = &cpewan_id; |
| 168 | } |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 174 | if ((opt = option_find(mess, sz, OPTION_AGENT_ID, 1))) |
| 175 | { |
| 176 | /* Any agent-id needs to be copied back out, verbatim, as the last option |
| 177 | in the packet. Here, we shift it to the very end of the buffer, if it doesn't |
| 178 | get overwritten, then it will be shuffled back at the end of processing. |
| 179 | Note that the incoming options must not be overwritten here, so there has to |
| 180 | be enough free space at the end of the packet to copy the option. */ |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 181 | unsigned char *sopt; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 182 | unsigned int total = option_len(opt) + 2; |
| 183 | unsigned char *last_opt = option_find(mess, sz, OPTION_END, 0); |
| 184 | if (last_opt && last_opt < end - total) |
| 185 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 186 | end -= total; |
| 187 | agent_id = end; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 188 | memcpy(agent_id, opt, total); |
| 189 | } |
| 190 | |
| 191 | /* look for RFC3527 Link selection sub-option */ |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 192 | if ((sopt = option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_SUBNET_SELECT, INADDRSZ))) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 193 | subnet_addr = option_addr(sopt); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 194 | |
| 195 | /* look for RFC5107 server-identifier-override */ |
| 196 | if ((sopt = option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_SERVER_OR, INADDRSZ))) |
| 197 | override = option_addr(sopt); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 198 | |
| 199 | /* if a circuit-id or remote-is option is provided, exact-match to options. */ |
| 200 | for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next) |
| 201 | { |
| 202 | int search; |
| 203 | |
| 204 | if (vendor->match_type == MATCH_CIRCUIT) |
| 205 | search = SUBOPT_CIRCUIT_ID; |
| 206 | else if (vendor->match_type == MATCH_REMOTE) |
| 207 | search = SUBOPT_REMOTE_ID; |
| 208 | else if (vendor->match_type == MATCH_SUBSCRIBER) |
| 209 | search = SUBOPT_SUBSCR_ID; |
| 210 | else |
| 211 | continue; |
| 212 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 213 | if ((sopt = option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), search, 1)) && |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 214 | vendor->len == option_len(sopt) && |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 215 | memcmp(option_ptr(sopt, 0), vendor->data, vendor->len) == 0) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 216 | { |
| 217 | vendor->netid.next = netid; |
| 218 | netid = &vendor->netid; |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 219 | } |
| 220 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 221 | } |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 222 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 223 | /* Check for RFC3011 subnet selector - only if RFC3527 one not present */ |
| 224 | if (subnet_addr.s_addr == 0 && (opt = option_find(mess, sz, OPTION_SUBNET_SELECT, INADDRSZ))) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 225 | subnet_addr = option_addr(opt); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 226 | |
| 227 | /* If there is no client identifier option, use the hardware address */ |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 228 | if ((opt = option_find(mess, sz, OPTION_CLIENT_ID, 1))) |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 229 | { |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 230 | clid_len = option_len(opt); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 231 | clid = option_ptr(opt, 0); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 232 | } |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 233 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 234 | /* do we have a lease in store? */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 235 | lease = lease_find_by_client(mess->chaddr, mess->hlen, mess->htype, clid, clid_len); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 236 | |
| 237 | /* If this request is missing a clid, but we've seen one before, |
| 238 | use it again for option matching etc. */ |
| 239 | if (lease && !clid && lease->clid) |
| 240 | { |
| 241 | clid_len = lease->clid_len; |
| 242 | clid = lease->clid; |
| 243 | } |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 244 | |
| 245 | /* find mac to use for logging and hashing */ |
| 246 | emac = extended_hwaddr(mess->htype, mess->hlen, mess->chaddr, clid_len, clid, &emac_len); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 247 | } |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 248 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 249 | for (mac = daemon->dhcp_macs; mac; mac = mac->next) |
| 250 | if (mac->hwaddr_len == mess->hlen && |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 251 | (mac->hwaddr_type == mess->htype || mac->hwaddr_type == 0) && |
| 252 | memcmp_masked(mac->hwaddr, mess->chaddr, mess->hlen, mac->mask)) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 253 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 254 | mac->netid.next = netid; |
| 255 | netid = &mac->netid; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 256 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 257 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 258 | /* Determine network for this packet. Our caller will have already linked all the |
| 259 | contexts which match the addresses of the receiving interface but if the |
| 260 | machine has an address already, or came via a relay, or we have a subnet selector, |
| 261 | we search again. If we don't have have a giaddr or explicit subnet selector, |
| 262 | use the ciaddr. This is necessary because a machine which got a lease via a |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 263 | relay won't use the relay to renew. If matching a ciaddr fails but we have a context |
| 264 | from the physical network, continue using that to allow correct DHCPNAK generation later. */ |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 265 | if (mess->giaddr.s_addr || subnet_addr.s_addr || mess->ciaddr.s_addr) |
| 266 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 267 | struct dhcp_context *context_tmp, *context_new = NULL; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 268 | struct in_addr addr; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 269 | int force = 0; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 270 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 271 | if (subnet_addr.s_addr) |
| 272 | { |
| 273 | addr = subnet_addr; |
| 274 | force = 1; |
| 275 | } |
| 276 | else if (mess->giaddr.s_addr) |
| 277 | { |
| 278 | addr = mess->giaddr; |
| 279 | force = 1; |
| 280 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 281 | else |
| 282 | { |
| 283 | /* If ciaddr is in the hardware derived set of contexts, leave that unchanged */ |
| 284 | addr = mess->ciaddr; |
| 285 | for (context_tmp = context; context_tmp; context_tmp = context_tmp->current) |
| 286 | if (context_tmp->netmask.s_addr && |
| 287 | is_same_net(addr, context_tmp->start, context_tmp->netmask) && |
| 288 | is_same_net(addr, context_tmp->end, context_tmp->netmask)) |
| 289 | { |
| 290 | context_new = context; |
| 291 | break; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if (!context_new) |
| 296 | for (context_tmp = daemon->dhcp; context_tmp; context_tmp = context_tmp->next) |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 297 | { |
| 298 | struct in_addr netmask = context_tmp->netmask; |
| 299 | |
| 300 | /* guess the netmask for relayed networks */ |
| 301 | if (!(context_tmp->flags & CONTEXT_NETMASK) && context_tmp->netmask.s_addr == 0) |
| 302 | { |
| 303 | if (IN_CLASSA(ntohl(context_tmp->start.s_addr)) && IN_CLASSA(ntohl(context_tmp->end.s_addr))) |
| 304 | netmask.s_addr = htonl(0xff000000); |
| 305 | else if (IN_CLASSB(ntohl(context_tmp->start.s_addr)) && IN_CLASSB(ntohl(context_tmp->end.s_addr))) |
| 306 | netmask.s_addr = htonl(0xffff0000); |
| 307 | else if (IN_CLASSC(ntohl(context_tmp->start.s_addr)) && IN_CLASSC(ntohl(context_tmp->end.s_addr))) |
| 308 | netmask.s_addr = htonl(0xffffff00); |
| 309 | } |
| 310 | |
| 311 | /* This section fills in context mainly when a client which is on a remote (relayed) |
| 312 | network renews a lease without using the relay, after dnsmasq has restarted. */ |
| 313 | if (netmask.s_addr != 0 && |
| 314 | is_same_net(addr, context_tmp->start, netmask) && |
| 315 | is_same_net(addr, context_tmp->end, netmask)) |
| 316 | { |
| 317 | context_tmp->netmask = netmask; |
| 318 | if (context_tmp->local.s_addr == 0) |
| 319 | context_tmp->local = fallback; |
| 320 | if (context_tmp->router.s_addr == 0) |
| 321 | context_tmp->router = mess->giaddr; |
| 322 | |
| 323 | /* fill in missing broadcast addresses for relayed ranges */ |
| 324 | if (!(context_tmp->flags & CONTEXT_BRDCAST) && context_tmp->broadcast.s_addr == 0 ) |
| 325 | context_tmp->broadcast.s_addr = context_tmp->start.s_addr | ~context_tmp->netmask.s_addr; |
| 326 | |
| 327 | context_tmp->current = context_new; |
| 328 | context_new = context_tmp; |
| 329 | } |
| 330 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 331 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 332 | if (context_new || force) |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 333 | context = context_new; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 334 | } |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 335 | |
| 336 | if (!context) |
| 337 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 338 | my_syslog(MS_DHCP | LOG_WARNING, _("no address range available for DHCP request %s %s"), |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 339 | subnet_addr.s_addr ? _("with subnet selector") : _("via"), |
| 340 | subnet_addr.s_addr ? inet_ntoa(subnet_addr) : (mess->giaddr.s_addr ? inet_ntoa(mess->giaddr) : iface_name)); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 341 | return 0; |
| 342 | } |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 343 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 344 | if (option_bool(OPT_LOG_OPTS)) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 345 | { |
| 346 | struct dhcp_context *context_tmp; |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 347 | for (context_tmp = context; context_tmp; context_tmp = context_tmp->current) |
| 348 | { |
| 349 | strcpy(daemon->namebuff, inet_ntoa(context_tmp->start)); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 350 | if (context_tmp->flags & (CONTEXT_STATIC | CONTEXT_PROXY)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 351 | my_syslog(MS_DHCP | LOG_INFO, _("%u available DHCP subnet: %s/%s"), |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 352 | ntohl(mess->xid), daemon->namebuff, inet_ntoa(context_tmp->netmask)); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 353 | else |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 354 | my_syslog(MS_DHCP | LOG_INFO, _("%u available DHCP range: %s -- %s"), |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 355 | ntohl(mess->xid), daemon->namebuff, inet_ntoa(context_tmp->end)); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 359 | mess->op = BOOTREPLY; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 360 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 361 | config = find_config(daemon->dhcp_conf, context, clid, clid_len, |
| 362 | mess->chaddr, mess->hlen, mess->htype, NULL); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 363 | |
| 364 | /* set "known" tag for known hosts */ |
| 365 | if (config) |
| 366 | { |
| 367 | known_id.net = "known"; |
| 368 | known_id.next = netid; |
| 369 | netid = &known_id; |
| 370 | } |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 371 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 372 | if (mess_type == 0 && !pxe) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 373 | { |
| 374 | /* BOOTP request */ |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 375 | struct dhcp_netid id, bootp_id; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 376 | struct in_addr *logaddr = NULL; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 377 | |
| 378 | /* must have a MAC addr for bootp */ |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 379 | if (mess->htype == 0 || mess->hlen == 0 || (context->flags & CONTEXT_PROXY)) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 380 | return 0; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 381 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 382 | if (have_config(config, CONFIG_DISABLE)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 383 | message = _("disabled"); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 384 | |
| 385 | end = mess->options + 64; /* BOOTP vend area is only 64 bytes */ |
| 386 | |
| 387 | if (have_config(config, CONFIG_NAME)) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 388 | { |
| 389 | hostname = config->hostname; |
| 390 | domain = config->domain; |
| 391 | } |
| 392 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 393 | if (config) |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 394 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 395 | struct dhcp_netid_list *list; |
| 396 | |
| 397 | for (list = config->netid; list; list = list->next) |
| 398 | { |
| 399 | list->list->next = netid; |
| 400 | netid = list->list; |
| 401 | } |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /* Match incoming filename field as a netid. */ |
| 405 | if (mess->file[0]) |
| 406 | { |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 407 | memcpy(daemon->dhcp_buff2, mess->file, sizeof(mess->file)); |
| 408 | daemon->dhcp_buff2[sizeof(mess->file) + 1] = 0; /* ensure zero term. */ |
| 409 | id.net = (char *)daemon->dhcp_buff2; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 410 | id.next = netid; |
| 411 | netid = &id; |
| 412 | } |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 413 | |
| 414 | /* Add "bootp" as a tag to allow different options, address ranges etc |
| 415 | for BOOTP clients */ |
| 416 | bootp_id.net = "bootp"; |
| 417 | bootp_id.next = netid; |
| 418 | netid = &bootp_id; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 419 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 420 | tagif_netid = run_tag_if(netid); |
| 421 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 422 | for (id_list = daemon->dhcp_ignore; id_list; id_list = id_list->next) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 423 | if (match_netid(id_list->list, tagif_netid, 0)) |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 424 | message = _("ignored"); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 425 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 426 | if (!message) |
| 427 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 428 | int nailed = 0; |
| 429 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 430 | if (have_config(config, CONFIG_ADDR)) |
| 431 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 432 | nailed = 1; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 433 | logaddr = &config->addr; |
| 434 | mess->yiaddr = config->addr; |
| 435 | if ((lease = lease_find_by_addr(config->addr)) && |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 436 | (lease->hwaddr_len != mess->hlen || |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 437 | lease->hwaddr_type != mess->htype || |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 438 | memcmp(lease->hwaddr, mess->chaddr, lease->hwaddr_len) != 0)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 439 | message = _("address in use"); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 440 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 441 | else |
| 442 | { |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 443 | if (!(lease = lease_find_by_client(mess->chaddr, mess->hlen, mess->htype, NULL, 0)) || |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 444 | !address_available(context, lease->addr, tagif_netid)) |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 445 | { |
| 446 | if (lease) |
| 447 | { |
| 448 | /* lease exists, wrong network. */ |
| 449 | lease_prune(lease, now); |
| 450 | lease = NULL; |
| 451 | } |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 452 | if (!address_allocate(context, &mess->yiaddr, mess->chaddr, mess->hlen, tagif_netid, now)) |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 453 | message = _("no address available"); |
| 454 | } |
| 455 | else |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 456 | mess->yiaddr = lease->addr; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 457 | } |
| 458 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 459 | if (!message && !(context = narrow_context(context, mess->yiaddr, netid))) |
| 460 | message = _("wrong network"); |
| 461 | else if (context->netid.net) |
| 462 | { |
| 463 | context->netid.next = netid; |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 464 | tagif_netid = run_tag_if(&context->netid); |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 465 | } |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 466 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 467 | log_tags(tagif_netid, ntohl(mess->xid)); |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 468 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 469 | if (!message && !nailed) |
| 470 | { |
| 471 | for (id_list = daemon->bootp_dynamic; id_list; id_list = id_list->next) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 472 | if ((!id_list->list) || match_netid(id_list->list, tagif_netid, 0)) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 473 | break; |
| 474 | if (!id_list) |
| 475 | message = _("no address configured"); |
| 476 | } |
| 477 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 478 | if (!message && |
| 479 | !lease && |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 480 | (!(lease = lease4_allocate(mess->yiaddr)))) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 481 | message = _("no leases left"); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 482 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 483 | if (!message) |
| 484 | { |
| 485 | logaddr = &mess->yiaddr; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 486 | |
Simon Kelley | a9ab732 | 2012-04-28 11:29:37 +0100 | [diff] [blame] | 487 | lease_set_hwaddr(lease, mess->chaddr, NULL, mess->hlen, mess->htype, 0, now, 1); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 488 | if (hostname) |
Simon Kelley | 70c5e3e | 2012-02-06 22:05:15 +0000 | [diff] [blame] | 489 | lease_set_hostname(lease, hostname, 1, get_domain(lease->addr), domain); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 490 | /* infinite lease unless nailed in dhcp-host line. */ |
| 491 | lease_set_expires(lease, |
| 492 | have_config(config, CONFIG_TIME) ? config->lease_time : 0xffffffff, |
| 493 | now); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 494 | lease_set_interface(lease, int_index, now); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 495 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 496 | clear_packet(mess, end); |
Simon Kelley | 4d0f5b4 | 2012-09-05 23:29:30 +0100 | [diff] [blame] | 497 | match_vendor_opts(NULL, daemon->dhcp_opts); /* clear flags */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 498 | do_options(context, mess, end, NULL, hostname, get_domain(mess->yiaddr), |
Simon Kelley | 4d0f5b4 | 2012-09-05 23:29:30 +0100 | [diff] [blame] | 499 | netid, subnet_addr, 0, 0, -1, NULL, 0, now); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 503 | log_packet("BOOTP", logaddr, mess->chaddr, mess->hlen, iface_name, message, mess->xid); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 504 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 505 | return message ? 0 : dhcp_packet_size(mess, agent_id, real_end); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 506 | } |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 507 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 508 | if ((opt = option_find(mess, sz, OPTION_CLIENT_FQDN, 4))) |
| 509 | { |
| 510 | /* http://tools.ietf.org/wg/dhc/draft-ietf-dhc-fqdn-option/draft-ietf-dhc-fqdn-option-10.txt */ |
| 511 | int len = option_len(opt); |
| 512 | char *pq = daemon->dhcp_buff; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 513 | unsigned char *pp, *op = option_ptr(opt, 0); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 514 | |
Simon Kelley | 9fed0f7 | 2012-08-30 11:43:35 +0100 | [diff] [blame] | 515 | fqdn_flags = *op; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 516 | len -= 3; |
| 517 | op += 3; |
| 518 | pp = op; |
| 519 | |
Simon Kelley | 9fed0f7 | 2012-08-30 11:43:35 +0100 | [diff] [blame] | 520 | /* NB, the following always sets at least one bit */ |
| 521 | if (option_bool(OPT_FQDN_UPDATE)) |
| 522 | { |
| 523 | if (fqdn_flags & 0x01) |
| 524 | { |
| 525 | fqdn_flags |= 0x02; /* set O */ |
| 526 | fqdn_flags &= ~0x01; /* clear S */ |
| 527 | } |
| 528 | fqdn_flags |= 0x08; /* set N */ |
| 529 | } |
| 530 | else |
| 531 | { |
| 532 | if (!(fqdn_flags & 0x01)) |
| 533 | fqdn_flags |= 0x03; /* set S and O */ |
| 534 | fqdn_flags &= ~0x08; /* clear N */ |
| 535 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 536 | |
| 537 | if (fqdn_flags & 0x04) |
| 538 | while (*op != 0 && ((op + (*op) + 1) - pp) < len) |
| 539 | { |
| 540 | memcpy(pq, op+1, *op); |
| 541 | pq += *op; |
| 542 | op += (*op)+1; |
| 543 | *(pq++) = '.'; |
| 544 | } |
| 545 | else |
| 546 | { |
| 547 | memcpy(pq, op, len); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 548 | if (len > 0 && op[len-1] == 0) |
| 549 | borken_opt = 1; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 550 | pq += len + 1; |
| 551 | } |
| 552 | |
| 553 | if (pq != daemon->dhcp_buff) |
| 554 | pq--; |
| 555 | |
| 556 | *pq = 0; |
| 557 | |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 558 | if (legal_hostname(daemon->dhcp_buff)) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 559 | offer_hostname = client_hostname = daemon->dhcp_buff; |
| 560 | } |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 561 | else if ((opt = option_find(mess, sz, OPTION_HOSTNAME, 1))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 562 | { |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 563 | int len = option_len(opt); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 564 | memcpy(daemon->dhcp_buff, option_ptr(opt, 0), len); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 565 | /* Microsoft clients are broken, and need zero-terminated strings |
| 566 | in options. We detect this state here, and do the same in |
| 567 | any options we send */ |
| 568 | if (len > 0 && daemon->dhcp_buff[len-1] == 0) |
| 569 | borken_opt = 1; |
| 570 | else |
| 571 | daemon->dhcp_buff[len] = 0; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 572 | if (legal_hostname(daemon->dhcp_buff)) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 573 | client_hostname = daemon->dhcp_buff; |
| 574 | } |
| 575 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 576 | if (client_hostname && option_bool(OPT_LOG_OPTS)) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 577 | my_syslog(MS_DHCP | LOG_INFO, _("%u client provides name: %s"), ntohl(mess->xid), client_hostname); |
| 578 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 579 | if (have_config(config, CONFIG_NAME)) |
| 580 | { |
| 581 | hostname = config->hostname; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 582 | domain = config->domain; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 583 | hostname_auth = 1; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 584 | /* be careful not to send an OFFER with a hostname not matching the DISCOVER. */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 585 | if (fqdn_flags != 0 || !client_hostname || hostname_isequal(hostname, client_hostname)) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 586 | offer_hostname = hostname; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 587 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 588 | else if (client_hostname) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 589 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 590 | domain = strip_hostname(client_hostname); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 591 | |
| 592 | if (strlen(client_hostname) != 0) |
| 593 | { |
| 594 | hostname = client_hostname; |
| 595 | if (!config) |
| 596 | { |
| 597 | /* Search again now we have a hostname. |
| 598 | Only accept configs without CLID and HWADDR here, (they won't match) |
| 599 | to avoid impersonation by name. */ |
| 600 | struct dhcp_config *new = find_config(daemon->dhcp_conf, context, NULL, 0, |
| 601 | mess->chaddr, mess->hlen, |
| 602 | mess->htype, hostname); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 603 | if (new && !have_config(new, CONFIG_CLID) && !new->hwaddr) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 604 | { |
| 605 | config = new; |
| 606 | /* set "known" tag for known hosts */ |
| 607 | known_id.net = "known"; |
| 608 | known_id.next = netid; |
| 609 | netid = &known_id; |
| 610 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 611 | } |
| 612 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 613 | } |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 614 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 615 | if (config) |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 616 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 617 | struct dhcp_netid_list *list; |
| 618 | |
| 619 | for (list = config->netid; list; list = list->next) |
| 620 | { |
| 621 | list->list->next = netid; |
| 622 | netid = list->list; |
| 623 | } |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 624 | } |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 625 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 626 | /* dhcp-match. If we have hex-and-wildcards, look for a left-anchored match. |
| 627 | Otherwise assume the option is an array, and look for a matching element. |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 628 | If no data given, existance of the option is enough. This code handles |
| 629 | rfc3925 V-I classes too. */ |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 630 | for (o = daemon->dhcp_match; o; o = o->next) |
| 631 | { |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 632 | unsigned int len, elen, match = 0; |
| 633 | size_t offset, o2; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 634 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 635 | if (o->flags & DHOPT_RFC3925) |
| 636 | { |
| 637 | if (!(opt = option_find(mess, sz, OPTION_VENDOR_IDENT, 5))) |
| 638 | continue; |
| 639 | |
| 640 | for (offset = 0; offset < (option_len(opt) - 5u); offset += len + 5) |
| 641 | { |
| 642 | len = option_uint(opt, offset + 4 , 1); |
| 643 | /* Need to take care that bad data can't run us off the end of the packet */ |
| 644 | if ((offset + len + 5 <= (option_len(opt))) && |
| 645 | (option_uint(opt, offset, 4) == (unsigned int)o->u.encap)) |
| 646 | for (o2 = offset + 5; o2 < offset + len + 5; o2 += elen + 1) |
| 647 | { |
| 648 | elen = option_uint(opt, o2, 1); |
| 649 | if ((o2 + elen + 1 <= option_len(opt)) && |
| 650 | (match = match_bytes(o, option_ptr(opt, o2 + 1), elen))) |
| 651 | break; |
| 652 | } |
| 653 | if (match) |
| 654 | break; |
| 655 | } |
| 656 | } |
| 657 | else |
| 658 | { |
| 659 | if (!(opt = option_find(mess, sz, o->opt, 1))) |
| 660 | continue; |
| 661 | |
| 662 | match = match_bytes(o, option_ptr(opt, 0), option_len(opt)); |
| 663 | } |
| 664 | |
| 665 | if (match) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 666 | { |
| 667 | o->netid->next = netid; |
| 668 | netid = o->netid; |
| 669 | } |
| 670 | } |
| 671 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 672 | /* user-class options are, according to RFC3004, supposed to contain |
| 673 | a set of counted strings. Here we check that this is so (by seeing |
| 674 | if the counts are consistent with the overall option length) and if |
| 675 | so zero the counts so that we don't get spurious matches between |
| 676 | the vendor string and the counts. If the lengths don't add up, we |
| 677 | assume that the option is a single string and non RFC3004 compliant |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 678 | and just do the substring match. dhclient provides these broken options. |
| 679 | The code, later, which sends user-class data to the lease-change script |
| 680 | relies on the transformation done here. |
| 681 | */ |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 682 | |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 683 | if ((opt = option_find(mess, sz, OPTION_USER_CLASS, 1))) |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 684 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 685 | unsigned char *ucp = option_ptr(opt, 0); |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 686 | int tmp, j; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 687 | for (j = 0; j < option_len(opt); j += ucp[j] + 1); |
| 688 | if (j == option_len(opt)) |
| 689 | for (j = 0; j < option_len(opt); j = tmp) |
| 690 | { |
| 691 | tmp = j + ucp[j] + 1; |
| 692 | ucp[j] = 0; |
| 693 | } |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 694 | } |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 695 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 696 | for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 697 | { |
| 698 | int mopt; |
| 699 | |
| 700 | if (vendor->match_type == MATCH_VENDOR) |
| 701 | mopt = OPTION_VENDOR_ID; |
| 702 | else if (vendor->match_type == MATCH_USER) |
| 703 | mopt = OPTION_USER_CLASS; |
| 704 | else |
| 705 | continue; |
| 706 | |
| 707 | if ((opt = option_find(mess, sz, mopt, 1))) |
| 708 | { |
| 709 | int i; |
| 710 | for (i = 0; i <= (option_len(opt) - vendor->len); i++) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 711 | if (memcmp(vendor->data, option_ptr(opt, i), vendor->len) == 0) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 712 | { |
| 713 | vendor->netid.next = netid; |
| 714 | netid = &vendor->netid; |
| 715 | break; |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 720 | /* mark vendor-encapsulated options which match the client-supplied vendor class, |
| 721 | save client-supplied vendor class */ |
| 722 | if ((opt = option_find(mess, sz, OPTION_VENDOR_ID, 1))) |
| 723 | { |
| 724 | memcpy(daemon->dhcp_buff3, option_ptr(opt, 0), option_len(opt)); |
| 725 | vendor_class_len = option_len(opt); |
| 726 | } |
| 727 | match_vendor_opts(opt, daemon->dhcp_opts); |
| 728 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 729 | if (option_bool(OPT_LOG_OPTS)) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 730 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 731 | if (sanitise(opt, daemon->namebuff)) |
| 732 | my_syslog(MS_DHCP | LOG_INFO, _("%u vendor class: %s"), ntohl(mess->xid), daemon->namebuff); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 733 | if (sanitise(option_find(mess, sz, OPTION_USER_CLASS, 1), daemon->namebuff)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 734 | my_syslog(MS_DHCP | LOG_INFO, _("%u user class: %s"), ntohl(mess->xid), daemon->namebuff); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 735 | } |
| 736 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 737 | tagif_netid = run_tag_if(netid); |
| 738 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 739 | /* if all the netids in the ignore list are present, ignore this client */ |
| 740 | for (id_list = daemon->dhcp_ignore; id_list; id_list = id_list->next) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 741 | if (match_netid(id_list->list, tagif_netid, 0)) |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 742 | ignore = 1; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 743 | |
| 744 | /* If configured, we can override the server-id to be the address of the relay, |
| 745 | so that all traffic goes via the relay and can pick up agent-id info. This can be |
| 746 | configured for all relays, or by address. */ |
| 747 | if (daemon->override && mess->giaddr.s_addr != 0 && override.s_addr == 0) |
| 748 | { |
| 749 | if (!daemon->override_relays) |
| 750 | override = mess->giaddr; |
| 751 | else |
| 752 | { |
| 753 | struct addr_list *l; |
| 754 | for (l = daemon->override_relays; l; l = l->next) |
| 755 | if (l->addr.s_addr == mess->giaddr.s_addr) |
| 756 | break; |
| 757 | if (l) |
| 758 | override = mess->giaddr; |
| 759 | } |
| 760 | } |
| 761 | |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 762 | /* Can have setting to ignore the client ID for a particular MAC address or hostname */ |
| 763 | if (have_config(config, CONFIG_NOCLID)) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 764 | clid = NULL; |
| 765 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 766 | /* Check if client is PXE client. */ |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 767 | if (daemon->enable_pxe && |
| 768 | (opt = option_find(mess, sz, OPTION_VENDOR_ID, 9)) && |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 769 | strncmp(option_ptr(opt, 0), "PXEClient", 9) == 0) |
| 770 | { |
| 771 | if ((opt = option_find(mess, sz, OPTION_PXE_UUID, 17))) |
| 772 | { |
| 773 | memcpy(pxe_uuid, option_ptr(opt, 0), 17); |
| 774 | uuid = pxe_uuid; |
| 775 | } |
| 776 | |
| 777 | /* Check if this is really a PXE bootserver request, and handle specially if so. */ |
| 778 | if ((mess_type == DHCPREQUEST || mess_type == DHCPINFORM) && |
| 779 | (opt = option_find(mess, sz, OPTION_VENDOR_CLASS_OPT, 1)) && |
| 780 | (opt = option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_PXE_BOOT_ITEM, 4))) |
| 781 | { |
| 782 | struct pxe_service *service; |
| 783 | int type = option_uint(opt, 0, 2); |
| 784 | int layer = option_uint(opt, 2, 2); |
| 785 | unsigned char save71[4]; |
| 786 | struct dhcp_opt opt71; |
| 787 | |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 788 | if (ignore) |
| 789 | return 0; |
| 790 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 791 | if (layer & 0x8000) |
| 792 | { |
| 793 | my_syslog(MS_DHCP | LOG_ERR, _("PXE BIS not supported")); |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | memcpy(save71, option_ptr(opt, 0), 4); |
| 798 | |
| 799 | for (service = daemon->pxe_services; service; service = service->next) |
| 800 | if (service->type == type) |
| 801 | break; |
| 802 | |
| 803 | if (!service || !service->basename) |
| 804 | return 0; |
| 805 | |
| 806 | clear_packet(mess, end); |
| 807 | |
| 808 | mess->yiaddr = mess->ciaddr; |
| 809 | mess->ciaddr.s_addr = 0; |
Simon Kelley | 751d6f4 | 2012-02-10 15:24:51 +0000 | [diff] [blame] | 810 | if (service->sname) |
| 811 | mess->siaddr = a_record_from_hosts(service->sname, now); |
| 812 | else if (service->server.s_addr != 0) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 813 | mess->siaddr = service->server; |
| 814 | else |
| 815 | mess->siaddr = context->local; |
| 816 | |
| 817 | snprintf((char *)mess->file, sizeof(mess->file), "%s.%d", service->basename, layer); |
| 818 | option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPACK); |
| 819 | option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, htonl(context->local.s_addr)); |
| 820 | pxe_misc(mess, end, uuid); |
| 821 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 822 | prune_vendor_opts(tagif_netid); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 823 | opt71.val = save71; |
| 824 | opt71.opt = SUBOPT_PXE_BOOT_ITEM; |
| 825 | opt71.len = 4; |
| 826 | opt71.flags = DHOPT_VENDOR_MATCH; |
| 827 | opt71.netid = NULL; |
| 828 | opt71.next = daemon->dhcp_opts; |
| 829 | do_encap_opts(&opt71, OPTION_VENDOR_CLASS_OPT, DHOPT_VENDOR_MATCH, mess, end, 0); |
| 830 | |
| 831 | log_packet("PXE", &mess->yiaddr, emac, emac_len, iface_name, (char *)mess->file, mess->xid); |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 832 | log_tags(tagif_netid, ntohl(mess->xid)); |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 833 | return dhcp_packet_size(mess, agent_id, real_end); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | if ((opt = option_find(mess, sz, OPTION_ARCH, 2))) |
| 837 | { |
| 838 | pxearch = option_uint(opt, 0, 2); |
| 839 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 840 | /* proxy DHCP here. */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 841 | if ((mess_type == DHCPDISCOVER || (pxe && mess_type == DHCPREQUEST))) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 842 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 843 | struct dhcp_context *tmp; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 844 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 845 | for (tmp = context; tmp; tmp = tmp->current) |
| 846 | if ((tmp->flags & CONTEXT_PROXY) && |
| 847 | match_netid(tmp->filter, tagif_netid, 1)) |
| 848 | break; |
| 849 | |
| 850 | if (tmp) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 851 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 852 | struct dhcp_boot *boot = find_boot(tagif_netid); |
| 853 | |
| 854 | mess->yiaddr.s_addr = 0; |
| 855 | if (mess_type == DHCPDISCOVER || mess->ciaddr.s_addr == 0) |
| 856 | { |
| 857 | mess->ciaddr.s_addr = 0; |
| 858 | mess->flags |= htons(0x8000); /* broadcast */ |
| 859 | } |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 860 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 861 | clear_packet(mess, end); |
| 862 | |
| 863 | /* Provide the bootfile here, for gPXE, and in case we have no menu items |
| 864 | and set discovery_control = 8 */ |
| 865 | if (boot) |
| 866 | { |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 867 | if (boot->next_server.s_addr) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 868 | mess->siaddr = boot->next_server; |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 869 | else if (boot->tftp_sname) |
| 870 | mess->siaddr = a_record_from_hosts(boot->tftp_sname, now); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 871 | |
| 872 | if (boot->file) |
| 873 | strncpy((char *)mess->file, boot->file, sizeof(mess->file)-1); |
| 874 | } |
| 875 | |
| 876 | option_put(mess, end, OPTION_MESSAGE_TYPE, 1, |
| 877 | mess_type == DHCPDISCOVER ? DHCPOFFER : DHCPACK); |
| 878 | option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, htonl(context->local.s_addr)); |
| 879 | pxe_misc(mess, end, uuid); |
| 880 | prune_vendor_opts(tagif_netid); |
Simon Kelley | 751d6f4 | 2012-02-10 15:24:51 +0000 | [diff] [blame] | 881 | do_encap_opts(pxe_opts(pxearch, tagif_netid, context->local, now), OPTION_VENDOR_CLASS_OPT, DHOPT_VENDOR_MATCH, mess, end, 0); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 882 | |
| 883 | log_packet("PXE", NULL, emac, emac_len, iface_name, ignore ? "proxy-ignored" : "proxy", mess->xid); |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 884 | log_tags(tagif_netid, ntohl(mess->xid)); |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 885 | return ignore ? 0 : dhcp_packet_size(mess, agent_id, real_end); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 886 | } |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | /* if we're just a proxy server, go no further */ |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 892 | if ((context->flags & CONTEXT_PROXY) || pxe) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 893 | return 0; |
| 894 | |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 895 | if ((opt = option_find(mess, sz, OPTION_REQUESTED_OPTIONS, 0))) |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 896 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 897 | req_options = (unsigned char *)daemon->dhcp_buff2; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 898 | memcpy(req_options, option_ptr(opt, 0), option_len(opt)); |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 899 | req_options[option_len(opt)] = OPTION_END; |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 900 | } |
| 901 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 902 | switch (mess_type) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 903 | { |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 904 | case DHCPDECLINE: |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 905 | if (!(opt = option_find(mess, sz, OPTION_SERVER_IDENTIFIER, INADDRSZ)) || |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 906 | option_addr(opt).s_addr != server_id(context, override, fallback).s_addr) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 907 | return 0; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 908 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 909 | /* sanitise any message. Paranoid? Moi? */ |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 910 | sanitise(option_find(mess, sz, OPTION_MESSAGE, 1), daemon->dhcp_buff); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 911 | |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 912 | if (!(opt = option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ))) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 913 | return 0; |
| 914 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 915 | log_packet("DHCPDECLINE", option_ptr(opt, 0), emac, emac_len, iface_name, daemon->dhcp_buff, mess->xid); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 916 | |
| 917 | if (lease && lease->addr.s_addr == option_addr(opt).s_addr) |
| 918 | lease_prune(lease, now); |
| 919 | |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 920 | if (have_config(config, CONFIG_ADDR) && |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 921 | config->addr.s_addr == option_addr(opt).s_addr) |
| 922 | { |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 923 | prettyprint_time(daemon->dhcp_buff, DECLINE_BACKOFF); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 924 | my_syslog(MS_DHCP | LOG_WARNING, _("disabling DHCP static address %s for %s"), |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 925 | inet_ntoa(config->addr), daemon->dhcp_buff); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 926 | config->flags |= CONFIG_DECLINED; |
| 927 | config->decline_time = now; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 928 | } |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 929 | else |
| 930 | /* make sure this host gets a different address next time. */ |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 931 | for (; context; context = context->current) |
| 932 | context->addr_epoch++; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 933 | |
| 934 | return 0; |
| 935 | |
| 936 | case DHCPRELEASE: |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 937 | if (!(context = narrow_context(context, mess->ciaddr, tagif_netid)) || |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 938 | !(opt = option_find(mess, sz, OPTION_SERVER_IDENTIFIER, INADDRSZ)) || |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 939 | option_addr(opt).s_addr != server_id(context, override, fallback).s_addr) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 940 | return 0; |
| 941 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 942 | if (lease && lease->addr.s_addr == mess->ciaddr.s_addr) |
| 943 | lease_prune(lease, now); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 944 | else |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 945 | message = _("unknown lease"); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 946 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 947 | log_packet("DHCPRELEASE", &mess->ciaddr, emac, emac_len, iface_name, message, mess->xid); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 948 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 949 | return 0; |
| 950 | |
| 951 | case DHCPDISCOVER: |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 952 | if (ignore || have_config(config, CONFIG_DISABLE)) |
| 953 | { |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 954 | message = _("ignored"); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 955 | opt = NULL; |
| 956 | } |
| 957 | else |
| 958 | { |
| 959 | struct in_addr addr, conf; |
| 960 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 961 | addr.s_addr = conf.s_addr = 0; |
| 962 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 963 | if ((opt = option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ))) |
| 964 | addr = option_addr(opt); |
| 965 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 966 | if (have_config(config, CONFIG_ADDR)) |
| 967 | { |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 968 | char *addrs = inet_ntoa(config->addr); |
| 969 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 970 | if ((ltmp = lease_find_by_addr(config->addr)) && |
| 971 | ltmp != lease && |
| 972 | !config_has_mac(config, ltmp->hwaddr, ltmp->hwaddr_len, ltmp->hwaddr_type)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 973 | { |
| 974 | int len; |
| 975 | unsigned char *mac = extended_hwaddr(ltmp->hwaddr_type, ltmp->hwaddr_len, |
| 976 | ltmp->hwaddr, ltmp->clid_len, ltmp->clid, &len); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 977 | my_syslog(MS_DHCP | LOG_WARNING, _("not using configured address %s because it is leased to %s"), |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 978 | addrs, print_mac(daemon->namebuff, mac, len)); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 979 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 980 | else |
| 981 | { |
| 982 | struct dhcp_context *tmp; |
| 983 | for (tmp = context; tmp; tmp = tmp->current) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 984 | if (context->router.s_addr == config->addr.s_addr) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 985 | break; |
| 986 | if (tmp) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 987 | my_syslog(MS_DHCP | LOG_WARNING, _("not using configured address %s because it is in use by the server or relay"), addrs); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 988 | else if (have_config(config, CONFIG_DECLINED) && |
| 989 | difftime(now, config->decline_time) < (float)DECLINE_BACKOFF) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 990 | my_syslog(MS_DHCP | LOG_WARNING, _("not using configured address %s because it was previously declined"), addrs); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 991 | else |
| 992 | conf = config->addr; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | if (conf.s_addr) |
| 997 | mess->yiaddr = conf; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 998 | else if (lease && |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 999 | address_available(context, lease->addr, tagif_netid) && |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1000 | !config_find_by_address(daemon->dhcp_conf, lease->addr)) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1001 | mess->yiaddr = lease->addr; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1002 | else if (opt && address_available(context, addr, tagif_netid) && !lease_find_by_addr(addr) && |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1003 | !config_find_by_address(daemon->dhcp_conf, addr)) |
| 1004 | mess->yiaddr = addr; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1005 | else if (emac_len == 0) |
| 1006 | message = _("no unique-id"); |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1007 | else if (!address_allocate(context, &mess->yiaddr, emac, emac_len, tagif_netid, now)) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1008 | message = _("no address available"); |
| 1009 | } |
| 1010 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1011 | log_packet("DHCPDISCOVER", opt ? option_ptr(opt, 0) : NULL, emac, emac_len, iface_name, message, mess->xid); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1012 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1013 | if (message || !(context = narrow_context(context, mess->yiaddr, tagif_netid))) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 1014 | return 0; |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 1015 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1016 | if (context->netid.net) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 1017 | { |
| 1018 | context->netid.next = netid; |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1019 | tagif_netid = run_tag_if(&context->netid); |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 1020 | } |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1021 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1022 | log_tags(tagif_netid, ntohl(mess->xid)); |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1023 | |
| 1024 | log_packet("DHCPOFFER" , &mess->yiaddr, emac, emac_len, iface_name, NULL, mess->xid); |
| 1025 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1026 | time = calc_time(context, config, option_find(mess, sz, OPTION_LEASE_TIME, 4)); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1027 | clear_packet(mess, end); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1028 | option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPOFFER); |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1029 | option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, ntohl(server_id(context, override, fallback).s_addr)); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1030 | option_put(mess, end, OPTION_LEASE_TIME, 4, time); |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1031 | /* T1 and T2 are required in DHCPOFFER by HP's wacky Jetdirect client. */ |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 1032 | if (time != 0xffffffff) |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1033 | { |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1034 | option_put(mess, end, OPTION_T1, 4, (time/2)); |
| 1035 | option_put(mess, end, OPTION_T2, 4, (time*7)/8); |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1036 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1037 | do_options(context, mess, end, req_options, offer_hostname, get_domain(mess->yiaddr), |
Simon Kelley | 70c5e3e | 2012-02-06 22:05:15 +0000 | [diff] [blame] | 1038 | netid, subnet_addr, fqdn_flags, borken_opt, pxearch, uuid, vendor_class_len, now); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1039 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1040 | return dhcp_packet_size(mess, agent_id, real_end); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1041 | |
| 1042 | case DHCPREQUEST: |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 1043 | if (ignore || have_config(config, CONFIG_DISABLE)) |
| 1044 | return 0; |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 1045 | if ((opt = option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1046 | { |
| 1047 | /* SELECTING or INIT_REBOOT */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1048 | mess->yiaddr = option_addr(opt); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1049 | |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 1050 | /* send vendor and user class info for new or recreated lease */ |
| 1051 | do_classes = 1; |
| 1052 | |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 1053 | if ((opt = option_find(mess, sz, OPTION_SERVER_IDENTIFIER, INADDRSZ))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1054 | { |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1055 | /* SELECTING */ |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1056 | selecting = 1; |
| 1057 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1058 | if (override.s_addr != 0) |
| 1059 | { |
| 1060 | if (option_addr(opt).s_addr != override.s_addr) |
| 1061 | return 0; |
| 1062 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1063 | else |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1064 | { |
| 1065 | for (; context; context = context->current) |
| 1066 | if (context->local.s_addr == option_addr(opt).s_addr) |
| 1067 | break; |
| 1068 | |
| 1069 | if (!context) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1070 | { |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1071 | /* Handle very strange configs where clients have more than one route to the server. |
| 1072 | If a clients idea of its server-id matches any of our DHCP interfaces, we let it pass. |
| 1073 | Have to set override to make sure we echo back the correct server-id */ |
| 1074 | struct irec *intr; |
| 1075 | |
| 1076 | enumerate_interfaces(); |
| 1077 | |
| 1078 | for (intr = daemon->interfaces; intr; intr = intr->next) |
| 1079 | if (intr->addr.sa.sa_family == AF_INET && |
| 1080 | intr->addr.in.sin_addr.s_addr == option_addr(opt).s_addr && |
| 1081 | intr->tftp_ok) |
| 1082 | break; |
| 1083 | |
| 1084 | if (intr) |
| 1085 | override = intr->addr.in.sin_addr; |
| 1086 | else |
| 1087 | { |
| 1088 | /* In auth mode, a REQUEST sent to the wrong server |
| 1089 | should be faulted, so that the client establishes |
| 1090 | communication with us, otherwise, silently ignore. */ |
| 1091 | if (!option_bool(OPT_AUTHORITATIVE)) |
| 1092 | return 0; |
| 1093 | message = _("wrong server-ID"); |
| 1094 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1095 | } |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1096 | } |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 1097 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1098 | /* If a lease exists for this host and another address, squash it. */ |
| 1099 | if (lease && lease->addr.s_addr != mess->yiaddr.s_addr) |
| 1100 | { |
| 1101 | lease_prune(lease, now); |
| 1102 | lease = NULL; |
| 1103 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1104 | } |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1105 | else |
| 1106 | { |
| 1107 | /* INIT-REBOOT */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1108 | if (!lease && !option_bool(OPT_AUTHORITATIVE)) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1109 | return 0; |
| 1110 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1111 | if (lease && lease->addr.s_addr != mess->yiaddr.s_addr) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1112 | message = _("wrong address"); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1113 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1114 | } |
| 1115 | else |
| 1116 | { |
| 1117 | /* RENEWING or REBINDING */ |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1118 | /* Check existing lease for this address. |
| 1119 | We allow it to be missing if dhcp-authoritative mode |
| 1120 | as long as we can allocate the lease now - checked below. |
| 1121 | This makes for a smooth recovery from a lost lease DB */ |
| 1122 | if ((lease && mess->ciaddr.s_addr != lease->addr.s_addr) || |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1123 | (!lease && !option_bool(OPT_AUTHORITATIVE))) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1124 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1125 | /* A client rebinding will broadcast the request, so we may see it even |
| 1126 | if the lease is held by another server. Just ignore it in that case. |
| 1127 | If the request is unicast to us, then somethings wrong, NAK */ |
| 1128 | if (!unicast_dest) |
| 1129 | return 0; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1130 | message = _("lease not found"); |
| 1131 | /* ensure we broadcast NAK */ |
| 1132 | unicast_dest = 0; |
| 1133 | } |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1134 | |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1135 | /* desynchronise renewals */ |
| 1136 | fuzz = rand16(); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1137 | mess->yiaddr = mess->ciaddr; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1140 | log_packet("DHCPREQUEST", &mess->yiaddr, emac, emac_len, iface_name, NULL, mess->xid); |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 1141 | |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 1142 | if (!message) |
| 1143 | { |
| 1144 | struct dhcp_config *addr_config; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1145 | struct dhcp_context *tmp = NULL; |
| 1146 | |
| 1147 | if (have_config(config, CONFIG_ADDR)) |
| 1148 | for (tmp = context; tmp; tmp = tmp->current) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1149 | if (context->router.s_addr == config->addr.s_addr) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1150 | break; |
Simon Kelley | aedef83 | 2006-01-22 14:02:31 +0000 | [diff] [blame] | 1151 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1152 | if (!(context = narrow_context(context, mess->yiaddr, tagif_netid))) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1153 | { |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 1154 | /* If a machine moves networks whilst it has a lease, we catch that here. */ |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1155 | message = _("wrong network"); |
| 1156 | /* ensure we broadcast NAK */ |
| 1157 | unicast_dest = 0; |
| 1158 | } |
| 1159 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1160 | /* Check for renewal of a lease which is outside the allowed range. */ |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1161 | else if (!address_available(context, mess->yiaddr, tagif_netid) && |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 1162 | (!have_config(config, CONFIG_ADDR) || config->addr.s_addr != mess->yiaddr.s_addr)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1163 | message = _("address not available"); |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 1164 | |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 1165 | /* Check if a new static address has been configured. Be very sure that |
| 1166 | when the client does DISCOVER, it will get the static address, otherwise |
| 1167 | an endless protocol loop will ensue. */ |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1168 | else if (!tmp && !selecting && |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1169 | have_config(config, CONFIG_ADDR) && |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1170 | (!have_config(config, CONFIG_DECLINED) || |
| 1171 | difftime(now, config->decline_time) > (float)DECLINE_BACKOFF) && |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1172 | config->addr.s_addr != mess->yiaddr.s_addr && |
| 1173 | (!(ltmp = lease_find_by_addr(config->addr)) || ltmp == lease)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1174 | message = _("static lease available"); |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 1175 | |
| 1176 | /* Check to see if the address is reserved as a static address for another host */ |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1177 | else if ((addr_config = config_find_by_address(daemon->dhcp_conf, mess->yiaddr)) && addr_config != config) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1178 | message = _("address reserved"); |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 1179 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1180 | else if (!lease && (ltmp = lease_find_by_addr(mess->yiaddr))) |
| 1181 | { |
| 1182 | /* If a host is configured with more than one MAC address, it's OK to 'nix |
| 1183 | a lease from one of it's MACs to give the address to another. */ |
| 1184 | if (config && config_has_mac(config, ltmp->hwaddr, ltmp->hwaddr_len, ltmp->hwaddr_type)) |
| 1185 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1186 | my_syslog(MS_DHCP | LOG_INFO, _("abandoning lease to %s of %s"), |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1187 | print_mac(daemon->namebuff, ltmp->hwaddr, ltmp->hwaddr_len), |
| 1188 | inet_ntoa(ltmp->addr)); |
| 1189 | lease = ltmp; |
| 1190 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1191 | else |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1192 | message = _("address in use"); |
| 1193 | } |
| 1194 | |
| 1195 | if (!message) |
| 1196 | { |
| 1197 | if (emac_len == 0) |
| 1198 | message = _("no unique-id"); |
| 1199 | |
| 1200 | else if (!lease) |
| 1201 | { |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 1202 | if ((lease = lease4_allocate(mess->yiaddr))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1203 | do_classes = 1; |
| 1204 | else |
| 1205 | message = _("no leases left"); |
| 1206 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1207 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1208 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1209 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1210 | if (message) |
| 1211 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1212 | log_packet("DHCPNAK", &mess->yiaddr, emac, emac_len, iface_name, message, mess->xid); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1213 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1214 | mess->yiaddr.s_addr = 0; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1215 | clear_packet(mess, end); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1216 | option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPNAK); |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1217 | option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, ntohl(server_id(context, override, fallback).s_addr)); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1218 | option_put_string(mess, end, OPTION_MESSAGE, message, borken_opt); |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1219 | /* This fixes a problem with the DHCP spec, broadcasting a NAK to a host on |
| 1220 | a distant subnet which unicast a REQ to us won't work. */ |
| 1221 | if (!unicast_dest || mess->giaddr.s_addr != 0 || |
| 1222 | mess->ciaddr.s_addr == 0 || is_same_net(context->local, mess->ciaddr, context->netmask)) |
| 1223 | { |
| 1224 | mess->flags |= htons(0x8000); /* broadcast */ |
| 1225 | mess->ciaddr.s_addr = 0; |
| 1226 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1227 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1228 | else |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1229 | { |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1230 | if (context->netid.net) |
| 1231 | { |
| 1232 | context->netid.next = netid; |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1233 | tagif_netid = run_tag_if( &context->netid); |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1234 | } |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1235 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1236 | log_tags(tagif_netid, ntohl(mess->xid)); |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1237 | |
Simon Kelley | dcffad2 | 2012-04-24 15:25:18 +0100 | [diff] [blame] | 1238 | if (do_classes) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1239 | { |
Simon Kelley | dcffad2 | 2012-04-24 15:25:18 +0100 | [diff] [blame] | 1240 | /* pick up INIT-REBOOT events. */ |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1241 | lease->flags |= LEASE_CHANGED; |
Simon Kelley | 39bec5f | 2012-01-06 22:36:58 +0000 | [diff] [blame] | 1242 | |
Simon Kelley | dcffad2 | 2012-04-24 15:25:18 +0100 | [diff] [blame] | 1243 | #ifdef HAVE_SCRIPT |
| 1244 | if (daemon->lease_change_command) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1245 | { |
Simon Kelley | dcffad2 | 2012-04-24 15:25:18 +0100 | [diff] [blame] | 1246 | struct dhcp_netid *n; |
| 1247 | |
| 1248 | if (mess->giaddr.s_addr) |
| 1249 | lease->giaddr = mess->giaddr; |
| 1250 | |
| 1251 | free(lease->extradata); |
| 1252 | lease->extradata = NULL; |
| 1253 | lease->extradata_size = lease->extradata_len = 0; |
| 1254 | |
| 1255 | add_extradata_opt(lease, option_find(mess, sz, OPTION_VENDOR_ID, 1)); |
| 1256 | add_extradata_opt(lease, option_find(mess, sz, OPTION_HOSTNAME, 1)); |
| 1257 | add_extradata_opt(lease, oui); |
| 1258 | add_extradata_opt(lease, serial); |
| 1259 | add_extradata_opt(lease, class); |
Simon Kelley | dd1721c | 2013-02-18 21:04:04 +0000 | [diff] [blame] | 1260 | |
| 1261 | if ((opt = option_find(mess, sz, OPTION_AGENT_ID, 1))) |
| 1262 | { |
| 1263 | add_extradata_opt(lease, option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_CIRCUIT_ID, 1)); |
| 1264 | add_extradata_opt(lease, option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_SUBSCR_ID, 1)); |
| 1265 | add_extradata_opt(lease, option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_REMOTE_ID, 1)); |
| 1266 | } |
| 1267 | else |
| 1268 | { |
| 1269 | add_extradata_opt(lease, NULL); |
| 1270 | add_extradata_opt(lease, NULL); |
| 1271 | add_extradata_opt(lease, NULL); |
| 1272 | } |
| 1273 | |
Simon Kelley | dcffad2 | 2012-04-24 15:25:18 +0100 | [diff] [blame] | 1274 | /* space-concat tag set */ |
| 1275 | if (!tagif_netid) |
| 1276 | add_extradata_opt(lease, NULL); |
| 1277 | else |
| 1278 | for (n = tagif_netid; n; n = n->next) |
| 1279 | { |
| 1280 | struct dhcp_netid *n1; |
| 1281 | /* kill dupes */ |
| 1282 | for (n1 = n->next; n1; n1 = n1->next) |
| 1283 | if (strcmp(n->net, n1->net) == 0) |
| 1284 | break; |
| 1285 | if (!n1) |
| 1286 | lease_add_extradata(lease, (unsigned char *)n->net, strlen(n->net), n->next ? ' ' : 0); |
| 1287 | } |
| 1288 | |
| 1289 | if ((opt = option_find(mess, sz, OPTION_USER_CLASS, 1))) |
| 1290 | { |
| 1291 | int len = option_len(opt); |
| 1292 | unsigned char *ucp = option_ptr(opt, 0); |
| 1293 | /* If the user-class option started as counted strings, the first byte will be zero. */ |
| 1294 | if (len != 0 && ucp[0] == 0) |
| 1295 | ucp++, len--; |
| 1296 | lease_add_extradata(lease, ucp, len, 0); |
| 1297 | } |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1298 | } |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1299 | #endif |
Simon Kelley | dcffad2 | 2012-04-24 15:25:18 +0100 | [diff] [blame] | 1300 | } |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1301 | |
| 1302 | if (!hostname_auth && (client_hostname = host_from_dns(mess->yiaddr))) |
| 1303 | { |
| 1304 | domain = get_domain(mess->yiaddr); |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1305 | hostname = client_hostname; |
| 1306 | hostname_auth = 1; |
| 1307 | } |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1308 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1309 | time = calc_time(context, config, option_find(mess, sz, OPTION_LEASE_TIME, 4)); |
Simon Kelley | a9ab732 | 2012-04-28 11:29:37 +0100 | [diff] [blame] | 1310 | lease_set_hwaddr(lease, mess->chaddr, clid, mess->hlen, mess->htype, clid_len, now, do_classes); |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1311 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1312 | /* if all the netids in the ignore_name list are present, ignore client-supplied name */ |
| 1313 | if (!hostname_auth) |
| 1314 | { |
| 1315 | for (id_list = daemon->dhcp_ignore_names; id_list; id_list = id_list->next) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1316 | if ((!id_list->list) || match_netid(id_list->list, tagif_netid, 0)) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1317 | break; |
| 1318 | if (id_list) |
| 1319 | hostname = NULL; |
| 1320 | } |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1321 | |
| 1322 | /* Last ditch, if configured, generate hostname from mac address */ |
| 1323 | if (!hostname && emac_len != 0) |
| 1324 | { |
| 1325 | for (id_list = daemon->dhcp_gen_names; id_list; id_list = id_list->next) |
| 1326 | if ((!id_list->list) || match_netid(id_list->list, tagif_netid, 0)) |
| 1327 | break; |
| 1328 | if (id_list) |
| 1329 | { |
| 1330 | int i; |
| 1331 | |
| 1332 | hostname = daemon->dhcp_buff; |
| 1333 | /* buffer is 256 bytes, 3 bytes per octet */ |
| 1334 | for (i = 0; (i < emac_len) && (i < 80); i++) |
| 1335 | hostname += sprintf(hostname, "%.2x%s", emac[i], (i == emac_len - 1) ? "" : "-"); |
| 1336 | hostname = daemon->dhcp_buff; |
| 1337 | } |
| 1338 | } |
| 1339 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1340 | if (hostname) |
Simon Kelley | 70c5e3e | 2012-02-06 22:05:15 +0000 | [diff] [blame] | 1341 | lease_set_hostname(lease, hostname, hostname_auth, get_domain(lease->addr), domain); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1342 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1343 | lease_set_expires(lease, time, now); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 1344 | lease_set_interface(lease, int_index, now); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1345 | |
| 1346 | if (override.s_addr != 0) |
| 1347 | lease->override = override; |
| 1348 | else |
| 1349 | override = lease->override; |
| 1350 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1351 | log_packet("DHCPACK", &mess->yiaddr, emac, emac_len, iface_name, hostname, mess->xid); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1352 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1353 | clear_packet(mess, end); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1354 | option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPACK); |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1355 | option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, ntohl(server_id(context, override, fallback).s_addr)); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1356 | option_put(mess, end, OPTION_LEASE_TIME, 4, time); |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 1357 | if (time != 0xffffffff) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1358 | { |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 1359 | while (fuzz > (time/16)) |
| 1360 | fuzz = fuzz/2; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1361 | option_put(mess, end, OPTION_T1, 4, (time/2) - fuzz); |
| 1362 | option_put(mess, end, OPTION_T2, 4, ((time/8)*7) - fuzz); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1363 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1364 | do_options(context, mess, end, req_options, hostname, get_domain(mess->yiaddr), |
Simon Kelley | 70c5e3e | 2012-02-06 22:05:15 +0000 | [diff] [blame] | 1365 | netid, subnet_addr, fqdn_flags, borken_opt, pxearch, uuid, vendor_class_len, now); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1366 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1367 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1368 | return dhcp_packet_size(mess, agent_id, real_end); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1369 | |
| 1370 | case DHCPINFORM: |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 1371 | if (ignore || have_config(config, CONFIG_DISABLE)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1372 | message = _("ignored"); |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 1373 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1374 | log_packet("DHCPINFORM", &mess->ciaddr, emac, emac_len, iface_name, message, mess->xid); |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1375 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1376 | if (message || mess->ciaddr.s_addr == 0) |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1377 | return 0; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1378 | |
| 1379 | /* For DHCPINFORM only, cope without a valid context */ |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1380 | context = narrow_context(context, mess->ciaddr, tagif_netid); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1381 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1382 | /* Find a least based on IP address if we didn't |
| 1383 | get one from MAC address/client-d */ |
| 1384 | if (!lease && |
| 1385 | (lease = lease_find_by_addr(mess->ciaddr)) && |
| 1386 | lease->hostname) |
| 1387 | hostname = lease->hostname; |
| 1388 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1389 | if (!hostname && (hostname = host_from_dns(mess->ciaddr))) |
| 1390 | domain = get_domain(mess->ciaddr); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1391 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1392 | if (context && context->netid.net) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 1393 | { |
| 1394 | context->netid.next = netid; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1395 | tagif_netid = run_tag_if(&context->netid); |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 1396 | } |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1397 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1398 | log_tags(tagif_netid, ntohl(mess->xid)); |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1399 | |
| 1400 | log_packet("DHCPACK", &mess->ciaddr, emac, emac_len, iface_name, hostname, mess->xid); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1401 | |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 1402 | if (lease) |
| 1403 | { |
Simon Kelley | d1a5975 | 2012-11-05 16:50:30 +0000 | [diff] [blame] | 1404 | lease_set_interface(lease, int_index, now); |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 1405 | if (override.s_addr != 0) |
| 1406 | lease->override = override; |
| 1407 | else |
| 1408 | override = lease->override; |
| 1409 | } |
| 1410 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1411 | clear_packet(mess, end); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1412 | option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPACK); |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1413 | option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, ntohl(server_id(context, override, fallback).s_addr)); |
Simon Kelley | aa63a21 | 2013-04-22 15:01:52 +0100 | [diff] [blame] | 1414 | |
| 1415 | /* RFC 2131 says that DHCPINFORM shouldn't include lease-time parameters, but |
| 1416 | we supply a utility which makes DHCPINFORM requests to get this information. |
| 1417 | Only include lease time if OPTION_LEASE_TIME is in the parameter request list, |
| 1418 | which won't be true for ordinary clients, but will be true for the |
| 1419 | dhcp_lease_time utility. */ |
| 1420 | if (lease && in_list(req_options, OPTION_LEASE_TIME)) |
| 1421 | { |
| 1422 | if (lease->expires == 0) |
| 1423 | time = 0xffffffff; |
| 1424 | else |
| 1425 | time = (unsigned int)difftime(lease->expires, now); |
| 1426 | option_put(mess, end, OPTION_LEASE_TIME, 4, time); |
| 1427 | } |
| 1428 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1429 | do_options(context, mess, end, req_options, hostname, get_domain(mess->ciaddr), |
Simon Kelley | 70c5e3e | 2012-02-06 22:05:15 +0000 | [diff] [blame] | 1430 | netid, subnet_addr, fqdn_flags, borken_opt, pxearch, uuid, vendor_class_len, now); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1431 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1432 | *is_inform = 1; /* handle reply differently */ |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1433 | return dhcp_packet_size(mess, agent_id, real_end); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | return 0; |
| 1437 | } |
| 1438 | |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1439 | /* find a good value to use as MAC address for logging and address-allocation hashing. |
| 1440 | This is normally just the chaddr field from the DHCP packet, |
| 1441 | but eg Firewire will have hlen == 0 and use the client-id instead. |
| 1442 | This could be anything, but will normally be EUI64 for Firewire. |
| 1443 | We assume that if the first byte of the client-id equals the htype byte |
| 1444 | then the client-id is using the usual encoding and use the rest of the |
| 1445 | client-id: if not we can use the whole client-id. This should give |
| 1446 | sane MAC address logs. */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1447 | unsigned char *extended_hwaddr(int hwtype, int hwlen, unsigned char *hwaddr, |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1448 | int clid_len, unsigned char *clid, int *len_out) |
| 1449 | { |
| 1450 | if (hwlen == 0 && clid && clid_len > 3) |
| 1451 | { |
| 1452 | if (clid[0] == hwtype) |
| 1453 | { |
| 1454 | *len_out = clid_len - 1 ; |
| 1455 | return clid + 1; |
| 1456 | } |
| 1457 | |
| 1458 | #if defined(ARPHRD_EUI64) && defined(ARPHRD_IEEE1394) |
| 1459 | if (clid[0] == ARPHRD_EUI64 && hwtype == ARPHRD_IEEE1394) |
| 1460 | { |
| 1461 | *len_out = clid_len - 1 ; |
| 1462 | return clid + 1; |
| 1463 | } |
| 1464 | #endif |
| 1465 | |
| 1466 | *len_out = clid_len; |
| 1467 | return clid; |
| 1468 | } |
| 1469 | |
| 1470 | *len_out = hwlen; |
| 1471 | return hwaddr; |
| 1472 | } |
| 1473 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1474 | static unsigned int calc_time(struct dhcp_context *context, struct dhcp_config *config, unsigned char *opt) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1475 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1476 | unsigned int time = have_config(config, CONFIG_TIME) ? config->lease_time : context->lease_time; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1477 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1478 | if (opt) |
| 1479 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1480 | unsigned int req_time = option_uint(opt, 0, 4); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1481 | if (req_time < 120 ) |
| 1482 | req_time = 120; /* sanity */ |
| 1483 | if (time == 0xffffffff || (req_time != 0xffffffff && req_time < time)) |
| 1484 | time = req_time; |
| 1485 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1486 | |
| 1487 | return time; |
| 1488 | } |
| 1489 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1490 | static struct in_addr server_id(struct dhcp_context *context, struct in_addr override, struct in_addr fallback) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1491 | { |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1492 | if (override.s_addr != 0) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1493 | return override; |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1494 | else if (context && context->local.s_addr != 0) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1495 | return context->local; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1496 | else |
| 1497 | return fallback; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1498 | } |
| 1499 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1500 | static int sanitise(unsigned char *opt, char *buf) |
| 1501 | { |
| 1502 | char *p; |
| 1503 | int i; |
| 1504 | |
| 1505 | *buf = 0; |
| 1506 | |
| 1507 | if (!opt) |
| 1508 | return 0; |
| 1509 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1510 | p = option_ptr(opt, 0); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1511 | |
| 1512 | for (i = option_len(opt); i > 0; i--) |
| 1513 | { |
| 1514 | char c = *p++; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1515 | if (isprint((int)c)) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1516 | *buf++ = c; |
| 1517 | } |
| 1518 | *buf = 0; /* add terminator */ |
| 1519 | |
| 1520 | return 1; |
| 1521 | } |
| 1522 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1523 | #ifdef HAVE_SCRIPT |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1524 | static void add_extradata_opt(struct dhcp_lease *lease, unsigned char *opt) |
| 1525 | { |
| 1526 | if (!opt) |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 1527 | lease_add_extradata(lease, NULL, 0, 0); |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1528 | else |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 1529 | lease_add_extradata(lease, option_ptr(opt, 0), option_len(opt), 0); |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1530 | } |
| 1531 | #endif |
| 1532 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1533 | static void log_packet(char *type, void *addr, unsigned char *ext_mac, |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1534 | int mac_len, char *interface, char *string, u32 xid) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1535 | { |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1536 | struct in_addr a; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1537 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1538 | /* addr may be misaligned */ |
| 1539 | if (addr) |
| 1540 | memcpy(&a, addr, sizeof(a)); |
| 1541 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1542 | print_mac(daemon->namebuff, ext_mac, mac_len); |
| 1543 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1544 | if(option_bool(OPT_LOG_OPTS)) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1545 | my_syslog(MS_DHCP | LOG_INFO, "%u %s(%s) %s%s%s %s", |
| 1546 | ntohl(xid), |
| 1547 | type, |
| 1548 | interface, |
| 1549 | addr ? inet_ntoa(a) : "", |
| 1550 | addr ? " " : "", |
| 1551 | daemon->namebuff, |
| 1552 | string ? string : ""); |
| 1553 | else |
| 1554 | my_syslog(MS_DHCP | LOG_INFO, "%s(%s) %s%s%s %s", |
| 1555 | type, |
| 1556 | interface, |
| 1557 | addr ? inet_ntoa(a) : "", |
| 1558 | addr ? " " : "", |
| 1559 | daemon->namebuff, |
| 1560 | string ? string : ""); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1561 | } |
| 1562 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1563 | static void log_options(unsigned char *start, u32 xid) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1564 | { |
| 1565 | while (*start != OPTION_END) |
| 1566 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1567 | char *optname = option_string(AF_INET, start[0], option_ptr(start, 0), option_len(start), daemon->namebuff, MAXDNAME); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1568 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1569 | my_syslog(MS_DHCP | LOG_INFO, "%u sent size:%3d option:%3d %s %s", |
| 1570 | ntohl(xid), option_len(start), start[0], optname, daemon->namebuff); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1571 | start += start[1] + 2; |
| 1572 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1575 | static unsigned char *option_find1(unsigned char *p, unsigned char *end, int opt, int minsize) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1576 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1577 | while (1) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1578 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1579 | if (p > end) |
| 1580 | return NULL; |
| 1581 | else if (*p == OPTION_END) |
| 1582 | return opt == OPTION_END ? p : NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1583 | else if (*p == OPTION_PAD) |
| 1584 | p++; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1585 | else |
| 1586 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1587 | int opt_len; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1588 | if (p > end - 2) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1589 | return NULL; /* malformed packet */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1590 | opt_len = option_len(p); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1591 | if (p > end - (2 + opt_len)) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1592 | return NULL; /* malformed packet */ |
| 1593 | if (*p == opt && opt_len >= minsize) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1594 | return p; |
| 1595 | p += opt_len + 2; |
| 1596 | } |
| 1597 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1600 | static unsigned char *option_find(struct dhcp_packet *mess, size_t size, int opt_type, int minsize) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1601 | { |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1602 | unsigned char *ret, *overload; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1603 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1604 | /* skip over DHCP cookie; */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1605 | if ((ret = option_find1(&mess->options[0] + sizeof(u32), ((unsigned char *)mess) + size, opt_type, minsize))) |
| 1606 | return ret; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1607 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1608 | /* look for overload option. */ |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1609 | if (!(overload = option_find1(&mess->options[0] + sizeof(u32), ((unsigned char *)mess) + size, OPTION_OVERLOAD, 1))) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1610 | return NULL; |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 1611 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1612 | /* Can we look in filename area ? */ |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1613 | if ((overload[2] & 1) && |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1614 | (ret = option_find1(&mess->file[0], &mess->file[128], opt_type, minsize))) |
| 1615 | return ret; |
| 1616 | |
| 1617 | /* finally try sname area */ |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1618 | if ((overload[2] & 2) && |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1619 | (ret = option_find1(&mess->sname[0], &mess->sname[64], opt_type, minsize))) |
| 1620 | return ret; |
| 1621 | |
| 1622 | return NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1625 | static struct in_addr option_addr(unsigned char *opt) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1626 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1627 | /* this worries about unaligned data in the option. */ |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1628 | /* struct in_addr is network byte order */ |
| 1629 | struct in_addr ret; |
| 1630 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1631 | memcpy(&ret, option_ptr(opt, 0), INADDRSZ); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1632 | |
| 1633 | return ret; |
| 1634 | } |
| 1635 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1636 | static unsigned int option_uint(unsigned char *opt, int offset, int size) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1637 | { |
| 1638 | /* this worries about unaligned data and byte order */ |
| 1639 | unsigned int ret = 0; |
| 1640 | int i; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1641 | unsigned char *p = option_ptr(opt, offset); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1642 | |
| 1643 | for (i = 0; i < size; i++) |
| 1644 | ret = (ret << 8) | *p++; |
| 1645 | |
| 1646 | return ret; |
| 1647 | } |
| 1648 | |
| 1649 | static unsigned char *dhcp_skip_opts(unsigned char *start) |
| 1650 | { |
| 1651 | while (*start != 0) |
| 1652 | start += start[1] + 2; |
| 1653 | return start; |
| 1654 | } |
| 1655 | |
| 1656 | /* only for use when building packet: doesn't check for bad data. */ |
| 1657 | static unsigned char *find_overload(struct dhcp_packet *mess) |
| 1658 | { |
| 1659 | unsigned char *p = &mess->options[0] + sizeof(u32); |
| 1660 | |
| 1661 | while (*p != 0) |
| 1662 | { |
| 1663 | if (*p == OPTION_OVERLOAD) |
| 1664 | return p; |
| 1665 | p += p[1] + 2; |
| 1666 | } |
| 1667 | return NULL; |
| 1668 | } |
| 1669 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1670 | static size_t dhcp_packet_size(struct dhcp_packet *mess, unsigned char *agent_id, unsigned char *real_end) |
| 1671 | { |
| 1672 | unsigned char *p = dhcp_skip_opts(&mess->options[0] + sizeof(u32)); |
| 1673 | unsigned char *overload; |
| 1674 | size_t ret; |
| 1675 | |
| 1676 | /* move agent_id back down to the end of the packet */ |
| 1677 | if (agent_id) |
| 1678 | { |
| 1679 | memmove(p, agent_id, real_end - agent_id); |
| 1680 | p += real_end - agent_id; |
| 1681 | memset(p, 0, real_end - p); /* in case of overlap */ |
| 1682 | } |
| 1683 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1684 | /* add END options to the regions. */ |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1685 | overload = find_overload(mess); |
| 1686 | |
| 1687 | if (overload && (option_uint(overload, 0, 1) & 1)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1688 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1689 | *dhcp_skip_opts(mess->file) = OPTION_END; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1690 | if (option_bool(OPT_LOG_OPTS)) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1691 | log_options(mess->file, mess->xid); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1692 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1693 | else if (option_bool(OPT_LOG_OPTS) && strlen((char *)mess->file) != 0) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1694 | my_syslog(MS_DHCP | LOG_INFO, _("%u bootfile name: %s"), ntohl(mess->xid), (char *)mess->file); |
| 1695 | |
| 1696 | if (overload && (option_uint(overload, 0, 1) & 2)) |
| 1697 | { |
| 1698 | *dhcp_skip_opts(mess->sname) = OPTION_END; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1699 | if (option_bool(OPT_LOG_OPTS)) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1700 | log_options(mess->sname, mess->xid); |
| 1701 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1702 | else if (option_bool(OPT_LOG_OPTS) && strlen((char *)mess->sname) != 0) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1703 | my_syslog(MS_DHCP | LOG_INFO, _("%u server name: %s"), ntohl(mess->xid), (char *)mess->sname); |
| 1704 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1705 | |
| 1706 | *p++ = OPTION_END; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1707 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1708 | if (option_bool(OPT_LOG_OPTS)) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1709 | { |
| 1710 | if (mess->siaddr.s_addr != 0) |
| 1711 | my_syslog(MS_DHCP | LOG_INFO, _("%u next server: %s"), ntohl(mess->xid), inet_ntoa(mess->siaddr)); |
| 1712 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1713 | if ((mess->flags & htons(0x8000)) && mess->ciaddr.s_addr == 0) |
| 1714 | my_syslog(MS_DHCP | LOG_INFO, _("%u broadcast response"), ntohl(mess->xid)); |
| 1715 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1716 | log_options(&mess->options[0] + sizeof(u32), mess->xid); |
| 1717 | } |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1718 | |
| 1719 | ret = (size_t)(p - (unsigned char *)mess); |
| 1720 | |
| 1721 | if (ret < MIN_PACKETSZ) |
| 1722 | ret = MIN_PACKETSZ; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1723 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1724 | return ret; |
| 1725 | } |
| 1726 | |
| 1727 | static unsigned char *free_space(struct dhcp_packet *mess, unsigned char *end, int opt, int len) |
| 1728 | { |
| 1729 | unsigned char *p = dhcp_skip_opts(&mess->options[0] + sizeof(u32)); |
| 1730 | |
| 1731 | if (p + len + 3 >= end) |
| 1732 | /* not enough space in options area, try and use overload, if poss */ |
| 1733 | { |
| 1734 | unsigned char *overload; |
| 1735 | |
| 1736 | if (!(overload = find_overload(mess)) && |
| 1737 | (mess->file[0] == 0 || mess->sname[0] == 0)) |
| 1738 | { |
| 1739 | /* attempt to overload fname and sname areas, we've reserved space for the |
| 1740 | overflow option previuously. */ |
| 1741 | overload = p; |
| 1742 | *(p++) = OPTION_OVERLOAD; |
| 1743 | *(p++) = 1; |
| 1744 | } |
| 1745 | |
| 1746 | p = NULL; |
| 1747 | |
| 1748 | /* using filename field ? */ |
| 1749 | if (overload) |
| 1750 | { |
| 1751 | if (mess->file[0] == 0) |
| 1752 | overload[2] |= 1; |
| 1753 | |
| 1754 | if (overload[2] & 1) |
| 1755 | { |
| 1756 | p = dhcp_skip_opts(mess->file); |
| 1757 | if (p + len + 3 >= mess->file + sizeof(mess->file)) |
| 1758 | p = NULL; |
| 1759 | } |
| 1760 | |
| 1761 | if (!p) |
| 1762 | { |
| 1763 | /* try to bring sname into play (it may be already) */ |
| 1764 | if (mess->sname[0] == 0) |
| 1765 | overload[2] |= 2; |
| 1766 | |
| 1767 | if (overload[2] & 2) |
| 1768 | { |
| 1769 | p = dhcp_skip_opts(mess->sname); |
Simon Kelley | ffa3d7d | 2013-02-04 21:35:43 +0000 | [diff] [blame] | 1770 | if (p + len + 3 >= mess->sname + sizeof(mess->sname)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1771 | p = NULL; |
| 1772 | } |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | if (!p) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1777 | my_syslog(MS_DHCP | LOG_WARNING, _("cannot send DHCP/BOOTP option %d: no space left in packet"), opt); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | if (p) |
| 1781 | { |
| 1782 | *(p++) = opt; |
| 1783 | *(p++) = len; |
| 1784 | } |
| 1785 | |
| 1786 | return p; |
| 1787 | } |
| 1788 | |
| 1789 | static void option_put(struct dhcp_packet *mess, unsigned char *end, int opt, int len, unsigned int val) |
| 1790 | { |
| 1791 | int i; |
| 1792 | unsigned char *p = free_space(mess, end, opt, len); |
| 1793 | |
| 1794 | if (p) |
| 1795 | for (i = 0; i < len; i++) |
| 1796 | *(p++) = val >> (8 * (len - (i + 1))); |
| 1797 | } |
| 1798 | |
| 1799 | static void option_put_string(struct dhcp_packet *mess, unsigned char *end, int opt, |
| 1800 | char *string, int null_term) |
| 1801 | { |
| 1802 | unsigned char *p; |
| 1803 | size_t len = strlen(string); |
| 1804 | |
| 1805 | if (null_term && len != 255) |
| 1806 | len++; |
| 1807 | |
| 1808 | if ((p = free_space(mess, end, opt, len))) |
| 1809 | memcpy(p, string, len); |
| 1810 | } |
| 1811 | |
| 1812 | /* return length, note this only does the data part */ |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1813 | static int do_opt(struct dhcp_opt *opt, unsigned char *p, struct dhcp_context *context, int null_term) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1814 | { |
| 1815 | int len = opt->len; |
| 1816 | |
| 1817 | if ((opt->flags & DHOPT_STRING) && null_term && len != 255) |
| 1818 | len++; |
| 1819 | |
| 1820 | if (p && len != 0) |
| 1821 | { |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1822 | if (context && (opt->flags & DHOPT_ADDR)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1823 | { |
| 1824 | int j; |
| 1825 | struct in_addr *a = (struct in_addr *)opt->val; |
| 1826 | for (j = 0; j < opt->len; j+=INADDRSZ, a++) |
| 1827 | { |
| 1828 | /* zero means "self" (but not in vendorclass options.) */ |
| 1829 | if (a->s_addr == 0) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1830 | memcpy(p, &context->local, INADDRSZ); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1831 | else |
| 1832 | memcpy(p, a, INADDRSZ); |
| 1833 | p += INADDRSZ; |
| 1834 | } |
| 1835 | } |
| 1836 | else |
| 1837 | memcpy(p, opt->val, len); |
| 1838 | } |
| 1839 | return len; |
| 1840 | } |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1841 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1842 | static int in_list(unsigned char *list, int opt) |
| 1843 | { |
| 1844 | int i; |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1845 | |
| 1846 | /* If no requested options, send everything, not nothing. */ |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1847 | if (!list) |
| 1848 | return 1; |
| 1849 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1850 | for (i = 0; list[i] != OPTION_END; i++) |
| 1851 | if (opt == list[i]) |
| 1852 | return 1; |
| 1853 | |
| 1854 | return 0; |
| 1855 | } |
| 1856 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1857 | static struct dhcp_opt *option_find2(int opt) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1858 | { |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1859 | struct dhcp_opt *opts; |
| 1860 | |
| 1861 | for (opts = daemon->dhcp_opts; opts; opts = opts->next) |
| 1862 | if (opts->opt == opt && (opts->flags & DHOPT_TAGOK)) |
| 1863 | return opts; |
| 1864 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1865 | return NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1868 | /* mark vendor-encapsulated options which match the client-supplied or |
| 1869 | config-supplied vendor class */ |
| 1870 | static void match_vendor_opts(unsigned char *opt, struct dhcp_opt *dopt) |
| 1871 | { |
| 1872 | for (; dopt; dopt = dopt->next) |
| 1873 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1874 | dopt->flags &= ~DHOPT_VENDOR_MATCH; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1875 | if (opt && (dopt->flags & DHOPT_VENDOR)) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1876 | { |
| 1877 | int i, len = 0; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1878 | if (dopt->u.vendor_class) |
| 1879 | len = strlen((char *)dopt->u.vendor_class); |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1880 | for (i = 0; i <= (option_len(opt) - len); i++) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1881 | if (len == 0 || memcmp(dopt->u.vendor_class, option_ptr(opt, i), len) == 0) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1882 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1883 | dopt->flags |= DHOPT_VENDOR_MATCH; |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1884 | break; |
| 1885 | } |
| 1886 | } |
| 1887 | } |
| 1888 | } |
| 1889 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1890 | static int do_encap_opts(struct dhcp_opt *opt, int encap, int flag, |
| 1891 | struct dhcp_packet *mess, unsigned char *end, int null_term) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1892 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1893 | int len, enc_len, ret = 0; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1894 | struct dhcp_opt *start; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1895 | unsigned char *p; |
| 1896 | |
| 1897 | /* find size in advance */ |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1898 | for (enc_len = 0, start = opt; opt; opt = opt->next) |
| 1899 | if (opt->flags & flag) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1900 | { |
| 1901 | int new = do_opt(opt, NULL, NULL, null_term) + 2; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1902 | ret = 1; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1903 | if (enc_len + new <= 255) |
| 1904 | enc_len += new; |
| 1905 | else |
| 1906 | { |
| 1907 | p = free_space(mess, end, encap, enc_len); |
| 1908 | for (; start && start != opt; start = start->next) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1909 | if (p && (start->flags & flag)) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1910 | { |
| 1911 | len = do_opt(start, p + 2, NULL, null_term); |
| 1912 | *(p++) = start->opt; |
| 1913 | *(p++) = len; |
| 1914 | p += len; |
| 1915 | } |
| 1916 | enc_len = new; |
| 1917 | start = opt; |
| 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | if (enc_len != 0 && |
| 1922 | (p = free_space(mess, end, encap, enc_len + 1))) |
| 1923 | { |
| 1924 | for (; start; start = start->next) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1925 | if (start->flags & flag) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1926 | { |
| 1927 | len = do_opt(start, p + 2, NULL, null_term); |
| 1928 | *(p++) = start->opt; |
| 1929 | *(p++) = len; |
| 1930 | p += len; |
| 1931 | } |
| 1932 | *p = OPTION_END; |
| 1933 | } |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1934 | |
| 1935 | return ret; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1938 | static void pxe_misc(struct dhcp_packet *mess, unsigned char *end, unsigned char *uuid) |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1939 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1940 | unsigned char *p; |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 1941 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1942 | option_put_string(mess, end, OPTION_VENDOR_ID, "PXEClient", 0); |
| 1943 | if (uuid && (p = free_space(mess, end, OPTION_PXE_UUID, 17))) |
| 1944 | memcpy(p, uuid, 17); |
| 1945 | } |
| 1946 | |
| 1947 | static int prune_vendor_opts(struct dhcp_netid *netid) |
| 1948 | { |
| 1949 | int force = 0; |
| 1950 | struct dhcp_opt *opt; |
| 1951 | |
| 1952 | /* prune vendor-encapsulated options based on netid, and look if we're forcing them to be sent */ |
| 1953 | for (opt = daemon->dhcp_opts; opt; opt = opt->next) |
| 1954 | if (opt->flags & DHOPT_VENDOR_MATCH) |
| 1955 | { |
| 1956 | if (!match_netid(opt->netid, netid, 1)) |
| 1957 | opt->flags &= ~DHOPT_VENDOR_MATCH; |
| 1958 | else if (opt->flags & DHOPT_FORCE) |
| 1959 | force = 1; |
| 1960 | } |
| 1961 | return force; |
| 1962 | } |
| 1963 | |
Simon Kelley | 751d6f4 | 2012-02-10 15:24:51 +0000 | [diff] [blame] | 1964 | static struct dhcp_opt *pxe_opts(int pxe_arch, struct dhcp_netid *netid, struct in_addr local, time_t now) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1965 | { |
| 1966 | #define NUM_OPTS 4 |
| 1967 | |
| 1968 | unsigned char *p, *q; |
| 1969 | struct pxe_service *service; |
| 1970 | static struct dhcp_opt *o, *ret; |
| 1971 | int i, j = NUM_OPTS - 1; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1972 | struct in_addr boot_server; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1973 | |
| 1974 | /* We pass back references to these, hence they are declared static */ |
| 1975 | static unsigned char discovery_control; |
| 1976 | static unsigned char fake_prompt[] = { 0, 'P', 'X', 'E' }; |
| 1977 | static struct dhcp_opt *fake_opts = NULL; |
| 1978 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1979 | /* Disable multicast, since we don't support it, and broadcast |
| 1980 | unless we need it */ |
| 1981 | discovery_control = 3; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1982 | |
| 1983 | ret = daemon->dhcp_opts; |
| 1984 | |
| 1985 | if (!fake_opts && !(fake_opts = whine_malloc(NUM_OPTS * sizeof(struct dhcp_opt)))) |
| 1986 | return ret; |
| 1987 | |
| 1988 | for (i = 0; i < NUM_OPTS; i++) |
| 1989 | { |
| 1990 | fake_opts[i].flags = DHOPT_VENDOR_MATCH; |
| 1991 | fake_opts[i].netid = NULL; |
| 1992 | fake_opts[i].next = i == (NUM_OPTS - 1) ? ret : &fake_opts[i+1]; |
| 1993 | } |
| 1994 | |
| 1995 | /* create the data for the PXE_MENU and PXE_SERVERS options. */ |
| 1996 | p = (unsigned char *)daemon->dhcp_buff; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1997 | q = (unsigned char *)daemon->dhcp_buff3; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1998 | |
| 1999 | for (i = 0, service = daemon->pxe_services; service; service = service->next) |
| 2000 | if (pxe_arch == service->CSA && match_netid(service->netid, netid, 1)) |
| 2001 | { |
| 2002 | size_t len = strlen(service->menu); |
| 2003 | /* opt 43 max size is 255. encapsulated option has type and length |
| 2004 | bytes, so its max size is 253. */ |
| 2005 | if (p - (unsigned char *)daemon->dhcp_buff + len + 3 < 253) |
| 2006 | { |
| 2007 | *(p++) = service->type >> 8; |
| 2008 | *(p++) = service->type; |
| 2009 | *(p++) = len; |
| 2010 | memcpy(p, service->menu, len); |
| 2011 | p += len; |
| 2012 | i++; |
| 2013 | } |
| 2014 | else |
| 2015 | { |
| 2016 | toobig: |
| 2017 | my_syslog(MS_DHCP | LOG_ERR, _("PXE menu too large")); |
| 2018 | return daemon->dhcp_opts; |
| 2019 | } |
| 2020 | |
Simon Kelley | 751d6f4 | 2012-02-10 15:24:51 +0000 | [diff] [blame] | 2021 | boot_server = service->basename ? local : |
| 2022 | (service->sname ? a_record_from_hosts(service->sname, now) : service->server); |
| 2023 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 2024 | if (boot_server.s_addr != 0) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2025 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 2026 | if (q - (unsigned char *)daemon->dhcp_buff3 + 3 + INADDRSZ >= 253) |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 2027 | goto toobig; |
| 2028 | |
| 2029 | /* Boot service with known address - give it */ |
| 2030 | *(q++) = service->type >> 8; |
| 2031 | *(q++) = service->type; |
| 2032 | *(q++) = 1; |
| 2033 | /* dest misaligned */ |
| 2034 | memcpy(q, &boot_server.s_addr, INADDRSZ); |
| 2035 | q += INADDRSZ; |
| 2036 | } |
| 2037 | else if (service->type != 0) |
| 2038 | /* We don't know the server for a service type, so we'll |
| 2039 | allow the client to broadcast for it */ |
| 2040 | discovery_control = 2; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | /* if no prompt, wait forever if there's a choice */ |
| 2044 | fake_prompt[0] = (i > 1) ? 255 : 0; |
| 2045 | |
| 2046 | if (i == 0) |
| 2047 | discovery_control = 8; /* no menu - just use use mess->filename */ |
| 2048 | else |
| 2049 | { |
| 2050 | ret = &fake_opts[j--]; |
| 2051 | ret->len = p - (unsigned char *)daemon->dhcp_buff; |
| 2052 | ret->val = (unsigned char *)daemon->dhcp_buff; |
| 2053 | ret->opt = SUBOPT_PXE_MENU; |
| 2054 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 2055 | if (q - (unsigned char *)daemon->dhcp_buff3 != 0) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2056 | { |
| 2057 | ret = &fake_opts[j--]; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 2058 | ret->len = q - (unsigned char *)daemon->dhcp_buff3; |
| 2059 | ret->val = (unsigned char *)daemon->dhcp_buff3; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2060 | ret->opt = SUBOPT_PXE_SERVERS; |
| 2061 | } |
| 2062 | } |
| 2063 | |
| 2064 | for (o = daemon->dhcp_opts; o; o = o->next) |
| 2065 | if ((o->flags & DHOPT_VENDOR_MATCH) && o->opt == SUBOPT_PXE_MENU_PROMPT) |
| 2066 | break; |
| 2067 | |
| 2068 | if (!o) |
| 2069 | { |
| 2070 | ret = &fake_opts[j--]; |
| 2071 | ret->len = sizeof(fake_prompt); |
| 2072 | ret->val = fake_prompt; |
| 2073 | ret->opt = SUBOPT_PXE_MENU_PROMPT; |
| 2074 | } |
| 2075 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 2076 | ret = &fake_opts[j--]; |
| 2077 | ret->len = 1; |
| 2078 | ret->opt = SUBOPT_PXE_DISCOVERY; |
| 2079 | ret->val= &discovery_control; |
| 2080 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2081 | return ret; |
| 2082 | } |
| 2083 | |
| 2084 | static void clear_packet(struct dhcp_packet *mess, unsigned char *end) |
| 2085 | { |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2086 | memset(mess->sname, 0, sizeof(mess->sname)); |
| 2087 | memset(mess->file, 0, sizeof(mess->file)); |
| 2088 | memset(&mess->options[0] + sizeof(u32), 0, end - (&mess->options[0] + sizeof(u32))); |
| 2089 | mess->siaddr.s_addr = 0; |
| 2090 | } |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 2091 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2092 | struct dhcp_boot *find_boot(struct dhcp_netid *netid) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 2093 | { |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2094 | struct dhcp_boot *boot; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2095 | |
| 2096 | /* decide which dhcp-boot option we're using */ |
| 2097 | for (boot = daemon->boot_config; boot; boot = boot->next) |
| 2098 | if (match_netid(boot->netid, netid, 0)) |
| 2099 | break; |
| 2100 | if (!boot) |
| 2101 | /* No match, look for one without a netid */ |
| 2102 | for (boot = daemon->boot_config; boot; boot = boot->next) |
| 2103 | if (match_netid(boot->netid, netid, 1)) |
| 2104 | break; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2105 | |
| 2106 | return boot; |
| 2107 | } |
| 2108 | |
| 2109 | static void do_options(struct dhcp_context *context, |
| 2110 | struct dhcp_packet *mess, |
| 2111 | unsigned char *end, |
| 2112 | unsigned char *req_options, |
| 2113 | char *hostname, |
Simon Kelley | 70c5e3e | 2012-02-06 22:05:15 +0000 | [diff] [blame] | 2114 | char *domain, |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2115 | struct dhcp_netid *netid, |
| 2116 | struct in_addr subnet_addr, |
| 2117 | unsigned char fqdn_flags, |
| 2118 | int null_term, int pxe_arch, |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 2119 | unsigned char *uuid, |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2120 | int vendor_class_len, |
| 2121 | time_t now) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2122 | { |
| 2123 | struct dhcp_opt *opt, *config_opts = daemon->dhcp_opts; |
| 2124 | struct dhcp_boot *boot; |
| 2125 | unsigned char *p; |
| 2126 | int i, len, force_encap = 0; |
| 2127 | unsigned char f0 = 0, s0 = 0; |
| 2128 | int done_file = 0, done_server = 0; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 2129 | int done_vendor_class = 0; |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2130 | struct dhcp_netid *tagif; |
| 2131 | struct dhcp_netid_list *id_list; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2132 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 2133 | /* filter options based on tags, those we want get DHOPT_TAGOK bit set */ |
Simon Kelley | 7d2b5c9 | 2012-03-23 10:00:02 +0000 | [diff] [blame] | 2134 | if (context) |
| 2135 | context->netid.next = NULL; |
Simon Kelley | 57f460d | 2012-02-16 20:00:32 +0000 | [diff] [blame] | 2136 | tagif = option_filter(netid, context && context->netid.net ? &context->netid : NULL, config_opts); |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2137 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2138 | /* logging */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 2139 | if (option_bool(OPT_LOG_OPTS) && req_options) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2140 | { |
| 2141 | char *q = daemon->namebuff; |
| 2142 | for (i = 0; req_options[i] != OPTION_END; i++) |
| 2143 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 2144 | char *s = option_string(AF_INET, req_options[i], NULL, 0, NULL, 0); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2145 | q += snprintf(q, MAXDNAME - (q - daemon->namebuff), |
| 2146 | "%d%s%s%s", |
| 2147 | req_options[i], |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 2148 | strlen(s) != 0 ? ":" : "", |
| 2149 | s, |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2150 | req_options[i+1] == OPTION_END ? "" : ", "); |
| 2151 | if (req_options[i+1] == OPTION_END || (q - daemon->namebuff) > 40) |
| 2152 | { |
| 2153 | q = daemon->namebuff; |
| 2154 | my_syslog(MS_DHCP | LOG_INFO, _("%u requested options: %s"), ntohl(mess->xid), daemon->namebuff); |
| 2155 | } |
| 2156 | } |
| 2157 | } |
| 2158 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2159 | for (id_list = daemon->force_broadcast; id_list; id_list = id_list->next) |
| 2160 | if ((!id_list->list) || match_netid(id_list->list, netid, 0)) |
| 2161 | break; |
| 2162 | if (id_list) |
| 2163 | mess->flags |= htons(0x8000); /* force broadcast */ |
| 2164 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 2165 | if (context) |
| 2166 | mess->siaddr = context->local; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2167 | |
| 2168 | /* See if we can send the boot stuff as options. |
| 2169 | To do this we need a requested option list, BOOTP |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2170 | and very old DHCP clients won't have this, we also |
| 2171 | provide an manual option to disable it. |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2172 | Some PXE ROMs have bugs (surprise!) and need zero-terminated |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2173 | names, so we always send those. */ |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2174 | if ((boot = find_boot(tagif))) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2175 | { |
| 2176 | if (boot->sname) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2177 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 2178 | if (!option_bool(OPT_NO_OVERRIDE) && |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2179 | req_options && |
| 2180 | in_list(req_options, OPTION_SNAME)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2181 | option_put_string(mess, end, OPTION_SNAME, boot->sname, 1); |
| 2182 | else |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2183 | strncpy((char *)mess->sname, boot->sname, sizeof(mess->sname)-1); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | if (boot->file) |
| 2187 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 2188 | if (!option_bool(OPT_NO_OVERRIDE) && |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2189 | req_options && |
| 2190 | in_list(req_options, OPTION_FILENAME)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2191 | option_put_string(mess, end, OPTION_FILENAME, boot->file, 1); |
| 2192 | else |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2193 | strncpy((char *)mess->file, boot->file, sizeof(mess->file)-1); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2196 | if (boot->next_server.s_addr) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2197 | mess->siaddr = boot->next_server; |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2198 | else if (boot->tftp_sname) |
| 2199 | mess->siaddr = a_record_from_hosts(boot->tftp_sname, now); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2200 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2201 | else |
| 2202 | /* Use the values of the relevant options if no dhcp-boot given and |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 2203 | they're not explicitly asked for as options. OPTION_END is used |
| 2204 | as an internal way to specify siaddr without using dhcp-boot, for use in |
| 2205 | dhcp-optsfile. */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2206 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 2207 | if ((!req_options || !in_list(req_options, OPTION_FILENAME)) && |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2208 | (opt = option_find2(OPTION_FILENAME)) && !(opt->flags & DHOPT_FORCE)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2209 | { |
| 2210 | strncpy((char *)mess->file, (char *)opt->val, sizeof(mess->file)-1); |
| 2211 | done_file = 1; |
| 2212 | } |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2213 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2214 | if ((!req_options || !in_list(req_options, OPTION_SNAME)) && |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2215 | (opt = option_find2(OPTION_SNAME)) && !(opt->flags & DHOPT_FORCE)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2216 | { |
| 2217 | strncpy((char *)mess->sname, (char *)opt->val, sizeof(mess->sname)-1); |
| 2218 | done_server = 1; |
| 2219 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 2220 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2221 | if ((opt = option_find2(OPTION_END))) |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 2222 | mess->siaddr.s_addr = ((struct in_addr *)opt->val)->s_addr; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2223 | } |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2224 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2225 | /* We don't want to do option-overload for BOOTP, so make the file and sname |
| 2226 | fields look like they are in use, even when they aren't. This gets restored |
| 2227 | at the end of this function. */ |
| 2228 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 2229 | if (!req_options || option_bool(OPT_NO_OVERRIDE)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2230 | { |
| 2231 | f0 = mess->file[0]; |
| 2232 | mess->file[0] = 1; |
| 2233 | s0 = mess->sname[0]; |
| 2234 | mess->sname[0] = 1; |
| 2235 | } |
| 2236 | |
| 2237 | /* At this point, if mess->sname or mess->file are zeroed, they are available |
| 2238 | for option overload, reserve space for the overload option. */ |
| 2239 | if (mess->file[0] == 0 || mess->sname[0] == 0) |
| 2240 | end -= 3; |
| 2241 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 2242 | /* rfc3011 says this doesn't need to be in the requested options list. */ |
| 2243 | if (subnet_addr.s_addr) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2244 | option_put(mess, end, OPTION_SUBNET_SELECT, INADDRSZ, ntohl(subnet_addr.s_addr)); |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 2245 | |
| 2246 | /* replies to DHCPINFORM may not have a valid context */ |
| 2247 | if (context) |
| 2248 | { |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2249 | if (!option_find2(OPTION_NETMASK)) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 2250 | option_put(mess, end, OPTION_NETMASK, INADDRSZ, ntohl(context->netmask.s_addr)); |
| 2251 | |
| 2252 | /* May not have a "guessed" broadcast address if we got no packets via a relay |
| 2253 | from this net yet (ie just unicast renewals after a restart */ |
| 2254 | if (context->broadcast.s_addr && |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2255 | !option_find2(OPTION_BROADCAST)) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 2256 | option_put(mess, end, OPTION_BROADCAST, INADDRSZ, ntohl(context->broadcast.s_addr)); |
| 2257 | |
| 2258 | /* Same comments as broadcast apply, and also may not be able to get a sensible |
| 2259 | default when using subnet select. User must configure by steam in that case. */ |
| 2260 | if (context->router.s_addr && |
| 2261 | in_list(req_options, OPTION_ROUTER) && |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2262 | !option_find2(OPTION_ROUTER)) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 2263 | option_put(mess, end, OPTION_ROUTER, INADDRSZ, ntohl(context->router.s_addr)); |
| 2264 | |
Simon Kelley | a21e27b | 2013-02-17 16:41:35 +0000 | [diff] [blame] | 2265 | if (daemon->port == NAMESERVER_PORT && |
| 2266 | in_list(req_options, OPTION_DNSSERVER) && |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2267 | !option_find2(OPTION_DNSSERVER)) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 2268 | option_put(mess, end, OPTION_DNSSERVER, INADDRSZ, ntohl(context->local.s_addr)); |
| 2269 | } |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 2270 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 2271 | if (domain && in_list(req_options, OPTION_DOMAINNAME) && |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2272 | !option_find2(OPTION_DOMAINNAME)) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 2273 | option_put_string(mess, end, OPTION_DOMAINNAME, domain, null_term); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 2274 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2275 | /* Note that we ignore attempts to set the fqdn using --dhc-option=81,<name> */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 2276 | if (hostname) |
| 2277 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2278 | if (in_list(req_options, OPTION_HOSTNAME) && |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2279 | !option_find2(OPTION_HOSTNAME)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2280 | option_put_string(mess, end, OPTION_HOSTNAME, hostname, null_term); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 2281 | |
| 2282 | if (fqdn_flags != 0) |
| 2283 | { |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 2284 | len = strlen(hostname) + 3; |
| 2285 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 2286 | if (fqdn_flags & 0x04) |
| 2287 | len += 2; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 2288 | else if (null_term) |
| 2289 | len++; |
| 2290 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 2291 | if (domain) |
| 2292 | len += strlen(domain) + 1; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 2293 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2294 | if ((p = free_space(mess, end, OPTION_CLIENT_FQDN, len))) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 2295 | { |
Simon Kelley | 2e34ac1 | 2012-08-29 14:15:25 +0100 | [diff] [blame] | 2296 | *(p++) = fqdn_flags & 0x0f; /* MBZ bits to zero */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 2297 | *(p++) = 255; |
| 2298 | *(p++) = 255; |
| 2299 | |
| 2300 | if (fqdn_flags & 0x04) |
| 2301 | { |
| 2302 | p = do_rfc1035_name(p, hostname); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 2303 | if (domain) |
| 2304 | p = do_rfc1035_name(p, domain); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 2305 | *p++ = 0; |
| 2306 | } |
| 2307 | else |
| 2308 | { |
| 2309 | memcpy(p, hostname, strlen(hostname)); |
| 2310 | p += strlen(hostname); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 2311 | if (domain) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 2312 | { |
| 2313 | *(p++) = '.'; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 2314 | memcpy(p, domain, strlen(domain)); |
| 2315 | p += strlen(domain); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 2316 | } |
| 2317 | if (null_term) |
| 2318 | *(p++) = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 2319 | } |
| 2320 | } |
| 2321 | } |
| 2322 | } |
| 2323 | |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 2324 | for (opt = config_opts; opt; opt = opt->next) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 2325 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2326 | int optno = opt->opt; |
| 2327 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2328 | /* netids match and not encapsulated? */ |
| 2329 | if (!(opt->flags & DHOPT_TAGOK)) |
| 2330 | continue; |
| 2331 | |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 2332 | /* was it asked for, or are we sending it anyway? */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2333 | if (!(opt->flags & DHOPT_FORCE) && !in_list(req_options, optno)) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 2334 | continue; |
| 2335 | |
| 2336 | /* prohibit some used-internally options */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2337 | if (optno == OPTION_CLIENT_FQDN || |
| 2338 | optno == OPTION_MAXMESSAGE || |
| 2339 | optno == OPTION_OVERLOAD || |
| 2340 | optno == OPTION_PAD || |
| 2341 | optno == OPTION_END) |
| 2342 | continue; |
| 2343 | |
| 2344 | if (optno == OPTION_SNAME && done_server) |
| 2345 | continue; |
| 2346 | |
| 2347 | if (optno == OPTION_FILENAME && done_file) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 2348 | continue; |
| 2349 | |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 2350 | /* For the options we have default values on |
| 2351 | dhc-option=<optionno> means "don't include this option" |
| 2352 | not "include a zero-length option" */ |
| 2353 | if (opt->len == 0 && |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2354 | (optno == OPTION_NETMASK || |
| 2355 | optno == OPTION_BROADCAST || |
| 2356 | optno == OPTION_ROUTER || |
| 2357 | optno == OPTION_DNSSERVER || |
| 2358 | optno == OPTION_DOMAINNAME || |
| 2359 | optno == OPTION_HOSTNAME)) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 2360 | continue; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2361 | |
| 2362 | /* vendor-class comes from elsewhere for PXE */ |
| 2363 | if (pxe_arch != -1 && optno == OPTION_VENDOR_ID) |
| 2364 | continue; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2365 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2366 | /* always force null-term for filename and servername - buggy PXE again. */ |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 2367 | len = do_opt(opt, NULL, context, |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2368 | (optno == OPTION_SNAME || optno == OPTION_FILENAME) ? 1 : null_term); |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 2369 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2370 | if ((p = free_space(mess, end, optno, len))) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 2371 | { |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 2372 | do_opt(opt, p, context, |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2373 | (optno == OPTION_SNAME || optno == OPTION_FILENAME) ? 1 : null_term); |
| 2374 | |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 2375 | /* If we send a vendor-id, revisit which vendor-ops we consider |
| 2376 | it appropriate to send. */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 2377 | if (optno == OPTION_VENDOR_ID) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 2378 | { |
| 2379 | match_vendor_opts(p - 2, config_opts); |
| 2380 | done_vendor_class = 1; |
| 2381 | } |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 2382 | } |
| 2383 | } |
| 2384 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 2385 | /* Now send options to be encapsulated in arbitrary options, |
| 2386 | eg dhcp-option=encap:172,17,....... |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 2387 | Also handle vendor-identifying vendor-encapsulated options, |
| 2388 | dhcp-option = vi-encap:13,17,....... |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 2389 | The may be more that one "outer" to do, so group |
| 2390 | all the options which match each outer in turn. */ |
| 2391 | for (opt = config_opts; opt; opt = opt->next) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2392 | opt->flags &= ~DHOPT_ENCAP_DONE; |
| 2393 | |
| 2394 | for (opt = config_opts; opt; opt = opt->next) |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 2395 | { |
| 2396 | int flags; |
| 2397 | |
| 2398 | if ((flags = (opt->flags & (DHOPT_ENCAPSULATE | DHOPT_RFC3925)))) |
| 2399 | { |
| 2400 | int found = 0; |
| 2401 | struct dhcp_opt *o; |
| 2402 | |
| 2403 | if (opt->flags & DHOPT_ENCAP_DONE) |
| 2404 | continue; |
| 2405 | |
| 2406 | for (len = 0, o = config_opts; o; o = o->next) |
| 2407 | { |
| 2408 | int outer = flags & DHOPT_ENCAPSULATE ? o->u.encap : OPTION_VENDOR_IDENT_OPT; |
| 2409 | |
| 2410 | o->flags &= ~DHOPT_ENCAP_MATCH; |
| 2411 | |
| 2412 | if (!(o->flags & flags) || opt->u.encap != o->u.encap) |
| 2413 | continue; |
| 2414 | |
| 2415 | o->flags |= DHOPT_ENCAP_DONE; |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2416 | if (match_netid(o->netid, tagif, 1) && |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 2417 | ((o->flags & DHOPT_FORCE) || in_list(req_options, outer))) |
| 2418 | { |
| 2419 | o->flags |= DHOPT_ENCAP_MATCH; |
| 2420 | found = 1; |
| 2421 | len += do_opt(o, NULL, NULL, 0) + 2; |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | if (found) |
| 2426 | { |
| 2427 | if (flags & DHOPT_ENCAPSULATE) |
| 2428 | do_encap_opts(config_opts, opt->u.encap, DHOPT_ENCAP_MATCH, mess, end, null_term); |
| 2429 | else if (len > 250) |
| 2430 | my_syslog(MS_DHCP | LOG_WARNING, _("cannot send RFC3925 option: too many options for enterprise number %d"), opt->u.encap); |
| 2431 | else if ((p = free_space(mess, end, OPTION_VENDOR_IDENT_OPT, len + 5))) |
| 2432 | { |
| 2433 | int swap_ent = htonl(opt->u.encap); |
| 2434 | memcpy(p, &swap_ent, 4); |
| 2435 | p += 4; |
| 2436 | *(p++) = len; |
| 2437 | for (o = config_opts; o; o = o->next) |
| 2438 | if (o->flags & DHOPT_ENCAP_MATCH) |
| 2439 | { |
| 2440 | len = do_opt(o, p + 2, NULL, 0); |
| 2441 | *(p++) = o->opt; |
| 2442 | *(p++) = len; |
| 2443 | p += len; |
| 2444 | } |
| 2445 | } |
| 2446 | } |
| 2447 | } |
| 2448 | } |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 2449 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 2450 | force_encap = prune_vendor_opts(tagif); |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 2451 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 2452 | if (context && pxe_arch != -1) |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2453 | { |
| 2454 | pxe_misc(mess, end, uuid); |
Simon Kelley | 751d6f4 | 2012-02-10 15:24:51 +0000 | [diff] [blame] | 2455 | config_opts = pxe_opts(pxe_arch, tagif, context->local, now); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2456 | } |
| 2457 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 2458 | if ((force_encap || in_list(req_options, OPTION_VENDOR_CLASS_OPT)) && |
| 2459 | do_encap_opts(config_opts, OPTION_VENDOR_CLASS_OPT, DHOPT_VENDOR_MATCH, mess, end, null_term) && |
| 2460 | pxe_arch == -1 && !done_vendor_class && vendor_class_len != 0 && |
| 2461 | (p = free_space(mess, end, OPTION_VENDOR_ID, vendor_class_len))) |
| 2462 | /* If we send vendor encapsulated options, and haven't already sent option 60, |
| 2463 | echo back the value we got from the client. */ |
| 2464 | memcpy(p, daemon->dhcp_buff3, vendor_class_len); |
| 2465 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2466 | /* restore BOOTP anti-overload hack */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 2467 | if (!req_options || option_bool(OPT_NO_OVERRIDE)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 2468 | { |
| 2469 | mess->file[0] = f0; |
| 2470 | mess->sname[0] = s0; |
| 2471 | } |
| 2472 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 2473 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 2474 | #endif |
| 2475 | |
| 2476 | |
| 2477 | |
| 2478 | |
| 2479 | |
| 2480 | |
| 2481 | |