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