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