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