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