Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1 | /* dnsmasq is Copyright (c) 2000-2009 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 | |
| 19 | #define BOOTREQUEST 1 |
| 20 | #define BOOTREPLY 2 |
| 21 | #define DHCP_COOKIE 0x63825363 |
| 22 | |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 23 | /* The Linux in-kernel DHCP client silently ignores any packet |
| 24 | smaller than this. Sigh........... */ |
| 25 | #define MIN_PACKETSZ 300 |
| 26 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 27 | #define OPTION_PAD 0 |
| 28 | #define OPTION_NETMASK 1 |
| 29 | #define OPTION_ROUTER 3 |
| 30 | #define OPTION_DNSSERVER 6 |
| 31 | #define OPTION_HOSTNAME 12 |
| 32 | #define OPTION_DOMAINNAME 15 |
| 33 | #define OPTION_BROADCAST 28 |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 34 | #define OPTION_VENDOR_CLASS_OPT 43 |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 35 | #define OPTION_REQUESTED_IP 50 |
| 36 | #define OPTION_LEASE_TIME 51 |
| 37 | #define OPTION_OVERLOAD 52 |
| 38 | #define OPTION_MESSAGE_TYPE 53 |
| 39 | #define OPTION_SERVER_IDENTIFIER 54 |
| 40 | #define OPTION_REQUESTED_OPTIONS 55 |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 41 | #define OPTION_MESSAGE 56 |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 42 | #define OPTION_MAXMESSAGE 57 |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 43 | #define OPTION_T1 58 |
| 44 | #define OPTION_T2 59 |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 45 | #define OPTION_VENDOR_ID 60 |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 46 | #define OPTION_CLIENT_ID 61 |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 47 | #define OPTION_SNAME 66 |
| 48 | #define OPTION_FILENAME 67 |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 49 | #define OPTION_USER_CLASS 77 |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 50 | #define OPTION_CLIENT_FQDN 81 |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 51 | #define OPTION_AGENT_ID 82 |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 52 | #define OPTION_SUBNET_SELECT 118 |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 53 | #define OPTION_END 255 |
| 54 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 55 | #define SUBOPT_CIRCUIT_ID 1 |
| 56 | #define SUBOPT_REMOTE_ID 2 |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 57 | #define SUBOPT_SUBNET_SELECT 5 /* RFC 3527 */ |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 58 | #define SUBOPT_SUBSCR_ID 6 /* RFC 3393 */ |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 59 | #define SUBOPT_SERVER_OR 11 /* RFC 5107 */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 60 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 61 | #define DHCPDISCOVER 1 |
| 62 | #define DHCPOFFER 2 |
| 63 | #define DHCPREQUEST 3 |
| 64 | #define DHCPDECLINE 4 |
| 65 | #define DHCPACK 5 |
| 66 | #define DHCPNAK 6 |
| 67 | #define DHCPRELEASE 7 |
| 68 | #define DHCPINFORM 8 |
| 69 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 70 | #define have_config(config, mask) ((config) && ((config)->flags & (mask))) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 71 | #define option_len(opt) ((int)(((unsigned char *)(opt))[1])) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 72 | #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] | 73 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 74 | static int sanitise(unsigned char *opt, char *buf); |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 75 | 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] | 76 | 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] | 77 | static void option_put(struct dhcp_packet *mess, unsigned char *end, int opt, int len, unsigned int val); |
| 78 | static void option_put_string(struct dhcp_packet *mess, unsigned char *end, |
| 79 | int opt, char *string, int null_term); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 80 | static struct in_addr option_addr(unsigned char *opt); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 81 | static unsigned int option_uint(unsigned char *opt, int size); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 82 | static void log_packet(char *type, void *addr, |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 83 | unsigned char *ext_mac, int mac_len, char *interface, char *string); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 84 | 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] | 85 | static unsigned char *option_find1(unsigned char *p, unsigned char *end, int opt, int minsize); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 86 | static size_t dhcp_packet_size(struct dhcp_packet *mess, struct dhcp_netid *netid); |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 87 | static void clear_packet(struct dhcp_packet *mess, unsigned char *end, unsigned char *agent_id); |
| 88 | static void restore_agent_id(unsigned char *agent_id, struct dhcp_packet *mess, unsigned char *real_end); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 89 | static void do_options(struct dhcp_context *context, |
| 90 | struct dhcp_packet *mess, |
| 91 | unsigned char *real_end, |
| 92 | unsigned char *req_options, |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 93 | char *hostname, |
| 94 | char *domain, char *config_domain, |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 95 | struct dhcp_netid *netid, |
| 96 | struct in_addr subnet_addr, |
| 97 | unsigned char fqdn_flags, |
| 98 | int null_term, |
| 99 | unsigned char *agent_id); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 100 | |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 101 | static void match_vendor_opts(unsigned char *opt, struct dhcp_opt *dopt); |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 102 | static void do_encap_opts(int encap, struct dhcp_packet *mess, unsigned char *end, int null_term); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 103 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 104 | size_t dhcp_reply(struct dhcp_context *context, char *iface_name, int int_index, |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 105 | size_t sz, time_t now, int unicast_dest, int *is_inform) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 106 | { |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 107 | unsigned char *opt, *clid = NULL; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 108 | struct dhcp_lease *ltmp, *lease = NULL; |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 109 | struct dhcp_vendor *vendor; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 110 | struct dhcp_mac *mac; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 111 | struct dhcp_netid_list *id_list; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 112 | int clid_len = 0, ignore = 0, do_classes = 0, selecting = 0; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 113 | struct dhcp_packet *mess = (struct dhcp_packet *)daemon->dhcp_packet.iov_base; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 114 | unsigned char *end = (unsigned char *)(mess + 1); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 115 | char *hostname = NULL, *offer_hostname = NULL, *client_hostname = NULL, *domain = NULL; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 116 | int hostname_auth = 0, borken_opt = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 117 | unsigned char *req_options = NULL; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 118 | char *message = NULL; |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 119 | unsigned int time; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 120 | struct dhcp_config *config; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 121 | struct dhcp_netid *netid; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 122 | struct in_addr subnet_addr, fallback, override; |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 123 | unsigned short fuzz = 0; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 124 | unsigned int mess_type = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 125 | unsigned char fqdn_flags = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 126 | unsigned char *agent_id = NULL; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 127 | unsigned char *emac = NULL; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 128 | int emac_len = 0; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 129 | struct dhcp_netid known_id, iface_id; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 130 | struct dhcp_opt *o; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 131 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 132 | subnet_addr.s_addr = override.s_addr = 0; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 133 | |
| 134 | /* set tag with name == interface */ |
| 135 | iface_id.net = iface_name; |
| 136 | iface_id.next = NULL; |
| 137 | netid = &iface_id; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 138 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 139 | if (mess->op != BOOTREQUEST || mess->hlen > DHCP_CHADDR_MAX) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 140 | return 0; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 141 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 142 | if (mess->htype == 0 && mess->hlen != 0) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 143 | return 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 144 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 145 | /* check for DHCP rather than BOOTP */ |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 146 | if ((opt = option_find(mess, sz, OPTION_MESSAGE_TYPE, 1))) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 147 | { |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 148 | mess_type = option_uint(opt, 1); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 149 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 150 | /* only insist on a cookie for DHCP. */ |
| 151 | if (*((u32 *)&mess->options) != htonl(DHCP_COOKIE)) |
| 152 | return 0; |
| 153 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 154 | /* two things to note here: expand_buf may move the packet, |
| 155 | so reassign mess from daemon->packet. Also, the size |
| 156 | sent includes the IP and UDP headers, hence the magic "-28" */ |
| 157 | if ((opt = option_find(mess, sz, OPTION_MAXMESSAGE, 2))) |
| 158 | { |
| 159 | size_t size = (size_t)option_uint(opt, 2) - 28; |
| 160 | |
| 161 | if (size > DHCP_PACKET_MAX) |
| 162 | size = DHCP_PACKET_MAX; |
| 163 | else if (size < sizeof(struct dhcp_packet)) |
| 164 | size = sizeof(struct dhcp_packet); |
| 165 | |
| 166 | if (expand_buf(&daemon->dhcp_packet, size)) |
| 167 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 168 | mess = (struct dhcp_packet *)daemon->dhcp_packet.iov_base; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 169 | end = ((unsigned char *)mess) + size; |
| 170 | } |
| 171 | } |
| 172 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 173 | /* Some buggy clients set ciaddr when they shouldn't, so clear that here since |
| 174 | it can affect the context-determination code. */ |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 175 | if ((option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ) || mess_type == DHCPDISCOVER)) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 176 | mess->ciaddr.s_addr = 0; |
| 177 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 178 | if ((opt = option_find(mess, sz, OPTION_AGENT_ID, 1))) |
| 179 | { |
| 180 | /* Any agent-id needs to be copied back out, verbatim, as the last option |
| 181 | in the packet. Here, we shift it to the very end of the buffer, if it doesn't |
| 182 | get overwritten, then it will be shuffled back at the end of processing. |
| 183 | Note that the incoming options must not be overwritten here, so there has to |
| 184 | be enough free space at the end of the packet to copy the option. */ |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 185 | unsigned char *sopt; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 186 | unsigned int total = option_len(opt) + 2; |
| 187 | unsigned char *last_opt = option_find(mess, sz, OPTION_END, 0); |
| 188 | if (last_opt && last_opt < end - total) |
| 189 | { |
| 190 | agent_id = end - total; |
| 191 | memcpy(agent_id, opt, total); |
| 192 | } |
| 193 | |
| 194 | /* look for RFC3527 Link selection sub-option */ |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 195 | 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] | 196 | subnet_addr = option_addr(sopt); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 197 | |
| 198 | /* look for RFC5107 server-identifier-override */ |
| 199 | if ((sopt = option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_SERVER_OR, INADDRSZ))) |
| 200 | override = option_addr(sopt); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 201 | |
| 202 | /* if a circuit-id or remote-is option is provided, exact-match to options. */ |
| 203 | for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next) |
| 204 | { |
| 205 | int search; |
| 206 | |
| 207 | if (vendor->match_type == MATCH_CIRCUIT) |
| 208 | search = SUBOPT_CIRCUIT_ID; |
| 209 | else if (vendor->match_type == MATCH_REMOTE) |
| 210 | search = SUBOPT_REMOTE_ID; |
| 211 | else if (vendor->match_type == MATCH_SUBSCRIBER) |
| 212 | search = SUBOPT_SUBSCR_ID; |
| 213 | else |
| 214 | continue; |
| 215 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 216 | 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] | 217 | vendor->len == option_len(sopt) && |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 218 | memcmp(option_ptr(sopt, 0), vendor->data, vendor->len) == 0) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 219 | { |
| 220 | vendor->netid.next = netid; |
| 221 | netid = &vendor->netid; |
| 222 | break; |
| 223 | } |
| 224 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 225 | } |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 226 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 227 | /* Check for RFC3011 subnet selector - only if RFC3527 one not present */ |
| 228 | if (subnet_addr.s_addr == 0 && (opt = option_find(mess, sz, OPTION_SUBNET_SELECT, INADDRSZ))) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 229 | subnet_addr = option_addr(opt); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 230 | |
| 231 | /* If there is no client identifier option, use the hardware address */ |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 232 | if ((opt = option_find(mess, sz, OPTION_CLIENT_ID, 1))) |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 233 | { |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 234 | clid_len = option_len(opt); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 235 | clid = option_ptr(opt, 0); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 236 | } |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 237 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 238 | /* do we have a lease in store? */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 239 | lease = lease_find_by_client(mess->chaddr, mess->hlen, mess->htype, clid, clid_len); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 240 | |
| 241 | /* If this request is missing a clid, but we've seen one before, |
| 242 | use it again for option matching etc. */ |
| 243 | if (lease && !clid && lease->clid) |
| 244 | { |
| 245 | clid_len = lease->clid_len; |
| 246 | clid = lease->clid; |
| 247 | } |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 248 | |
| 249 | /* find mac to use for logging and hashing */ |
| 250 | emac = extended_hwaddr(mess->htype, mess->hlen, mess->chaddr, clid_len, clid, &emac_len); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 251 | } |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 252 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 253 | for (mac = daemon->dhcp_macs; mac; mac = mac->next) |
| 254 | if (mac->hwaddr_len == mess->hlen && |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 255 | (mac->hwaddr_type == mess->htype || mac->hwaddr_type == 0) && |
| 256 | memcmp_masked(mac->hwaddr, mess->chaddr, mess->hlen, mac->mask)) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 257 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 258 | mac->netid.next = netid; |
| 259 | netid = &mac->netid; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 260 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 261 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 262 | /* Determine network for this packet. Our caller will have already linked all the |
| 263 | contexts which match the addresses of the receiving interface but if the |
| 264 | machine has an address already, or came via a relay, or we have a subnet selector, |
| 265 | we search again. If we don't have have a giaddr or explicit subnet selector, |
| 266 | use the ciaddr. This is necessary because a machine which got a lease via a |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 267 | relay won't use the relay to renew. If matching a ciaddr fails but we have a context |
| 268 | from the physical network, continue using that to allow correct DHCPNAK generation later. */ |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 269 | if (mess->giaddr.s_addr || subnet_addr.s_addr || mess->ciaddr.s_addr) |
| 270 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 271 | struct dhcp_context *context_tmp, *context_new = NULL; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 272 | struct in_addr addr; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 273 | int force = 0; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 274 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 275 | if (subnet_addr.s_addr) |
| 276 | { |
| 277 | addr = subnet_addr; |
| 278 | force = 1; |
| 279 | } |
| 280 | else if (mess->giaddr.s_addr) |
| 281 | { |
| 282 | addr = mess->giaddr; |
| 283 | force = 1; |
| 284 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 285 | else |
| 286 | { |
| 287 | /* If ciaddr is in the hardware derived set of contexts, leave that unchanged */ |
| 288 | addr = mess->ciaddr; |
| 289 | for (context_tmp = context; context_tmp; context_tmp = context_tmp->current) |
| 290 | if (context_tmp->netmask.s_addr && |
| 291 | is_same_net(addr, context_tmp->start, context_tmp->netmask) && |
| 292 | is_same_net(addr, context_tmp->end, context_tmp->netmask)) |
| 293 | { |
| 294 | context_new = context; |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if (!context_new) |
| 300 | for (context_tmp = daemon->dhcp; context_tmp; context_tmp = context_tmp->next) |
| 301 | if (context_tmp->netmask.s_addr && |
| 302 | is_same_net(addr, context_tmp->start, context_tmp->netmask) && |
| 303 | is_same_net(addr, context_tmp->end, context_tmp->netmask)) |
| 304 | { |
| 305 | context_tmp->current = context_new; |
| 306 | context_new = context_tmp; |
| 307 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 308 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 309 | if (context_new || force) |
| 310 | context = context_new; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 311 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 312 | } |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 313 | |
| 314 | if (!context) |
| 315 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 316 | my_syslog(LOG_WARNING, _("no address range available for DHCP request %s %s"), |
| 317 | subnet_addr.s_addr ? _("with subnet selector") : _("via"), |
| 318 | 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] | 319 | return 0; |
| 320 | } |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 321 | |
Simon Kelley | aedef83 | 2006-01-22 14:02:31 +0000 | [diff] [blame] | 322 | /* keep _a_ local address available. */ |
| 323 | fallback = context->local; |
| 324 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 325 | if (daemon->options & OPT_LOG_OPTS) |
| 326 | { |
| 327 | struct dhcp_context *context_tmp; |
| 328 | my_syslog(LOG_INFO, _("DHCP packet: transaction-id is %u"), mess->xid); |
| 329 | for (context_tmp = context; context_tmp; context_tmp = context_tmp->current) |
| 330 | { |
| 331 | strcpy(daemon->namebuff, inet_ntoa(context_tmp->start)); |
| 332 | if (context_tmp->flags & CONTEXT_STATIC) |
| 333 | my_syslog(LOG_INFO, _("Available DHCP subnet: %s/%s"), daemon->namebuff, inet_ntoa(context_tmp->netmask)); |
| 334 | else |
| 335 | my_syslog(LOG_INFO, _("Available DHCP range: %s -- %s"), daemon->namebuff, inet_ntoa(context_tmp->end)); |
| 336 | } |
| 337 | } |
| 338 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 339 | mess->op = BOOTREPLY; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 340 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 341 | config = find_config(daemon->dhcp_conf, context, clid, clid_len, |
| 342 | mess->chaddr, mess->hlen, mess->htype, NULL); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 343 | |
| 344 | /* set "known" tag for known hosts */ |
| 345 | if (config) |
| 346 | { |
| 347 | known_id.net = "known"; |
| 348 | known_id.next = netid; |
| 349 | netid = &known_id; |
| 350 | } |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 351 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 352 | if (mess_type == 0) |
| 353 | { |
| 354 | /* BOOTP request */ |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 355 | struct dhcp_netid id, bootp_id; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 356 | struct in_addr *logaddr = NULL; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 357 | |
| 358 | /* must have a MAC addr for bootp */ |
| 359 | if (mess->htype == 0 || mess->hlen == 0) |
| 360 | return 0; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 361 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 362 | if (have_config(config, CONFIG_DISABLE)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 363 | message = _("disabled"); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 364 | |
| 365 | end = mess->options + 64; /* BOOTP vend area is only 64 bytes */ |
| 366 | |
| 367 | if (have_config(config, CONFIG_NAME)) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 368 | { |
| 369 | hostname = config->hostname; |
| 370 | domain = config->domain; |
| 371 | } |
| 372 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 373 | if (have_config(config, CONFIG_NETID)) |
| 374 | { |
| 375 | config->netid.next = netid; |
| 376 | netid = &config->netid; |
| 377 | } |
| 378 | |
| 379 | /* Match incoming filename field as a netid. */ |
| 380 | if (mess->file[0]) |
| 381 | { |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 382 | memcpy(daemon->dhcp_buff2, mess->file, sizeof(mess->file)); |
| 383 | daemon->dhcp_buff2[sizeof(mess->file) + 1] = 0; /* ensure zero term. */ |
| 384 | id.net = (char *)daemon->dhcp_buff2; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 385 | id.next = netid; |
| 386 | netid = &id; |
| 387 | } |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 388 | |
| 389 | /* Add "bootp" as a tag to allow different options, address ranges etc |
| 390 | for BOOTP clients */ |
| 391 | bootp_id.net = "bootp"; |
| 392 | bootp_id.next = netid; |
| 393 | netid = &bootp_id; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 394 | |
| 395 | for (id_list = daemon->dhcp_ignore; id_list; id_list = id_list->next) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 396 | if (match_netid(id_list->list, netid, 0)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 397 | message = _("disabled"); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 398 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 399 | if (!message) |
| 400 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 401 | int nailed = 0; |
| 402 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 403 | if (have_config(config, CONFIG_ADDR)) |
| 404 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 405 | nailed = 1; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 406 | logaddr = &config->addr; |
| 407 | mess->yiaddr = config->addr; |
| 408 | if ((lease = lease_find_by_addr(config->addr)) && |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 409 | (lease->hwaddr_len != mess->hlen || |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 410 | lease->hwaddr_type != mess->htype || |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 411 | memcmp(lease->hwaddr, mess->chaddr, lease->hwaddr_len) != 0)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 412 | message = _("address in use"); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 413 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 414 | else |
| 415 | { |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 416 | if (!(lease = lease_find_by_client(mess->chaddr, mess->hlen, mess->htype, NULL, 0)) || |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 417 | !address_available(context, lease->addr, netid)) |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 418 | { |
| 419 | if (lease) |
| 420 | { |
| 421 | /* lease exists, wrong network. */ |
| 422 | lease_prune(lease, now); |
| 423 | lease = NULL; |
| 424 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 425 | if (!address_allocate(context, &mess->yiaddr, mess->chaddr, mess->hlen, netid, now)) |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 426 | message = _("no address available"); |
| 427 | } |
| 428 | else |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 429 | mess->yiaddr = lease->addr; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 430 | } |
| 431 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 432 | if (!message && !(context = narrow_context(context, mess->yiaddr, netid))) |
| 433 | message = _("wrong network"); |
| 434 | else if (context->netid.net) |
| 435 | { |
| 436 | context->netid.next = netid; |
| 437 | netid = &context->netid; |
| 438 | } |
| 439 | |
| 440 | if (!message && !nailed) |
| 441 | { |
| 442 | for (id_list = daemon->bootp_dynamic; id_list; id_list = id_list->next) |
| 443 | if ((!id_list->list) || match_netid(id_list->list, netid, 0)) |
| 444 | break; |
| 445 | if (!id_list) |
| 446 | message = _("no address configured"); |
| 447 | } |
| 448 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 449 | if (!message && |
| 450 | !lease && |
| 451 | (!(lease = lease_allocate(mess->yiaddr)))) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 452 | message = _("no leases left"); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 453 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 454 | if (!message) |
| 455 | { |
| 456 | logaddr = &mess->yiaddr; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 457 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 458 | lease_set_hwaddr(lease, mess->chaddr, NULL, mess->hlen, mess->htype, 0); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 459 | if (hostname) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 460 | lease_set_hostname(lease, hostname, 1); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 461 | /* infinite lease unless nailed in dhcp-host line. */ |
| 462 | lease_set_expires(lease, |
| 463 | have_config(config, CONFIG_TIME) ? config->lease_time : 0xffffffff, |
| 464 | now); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 465 | lease_set_interface(lease, int_index); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 466 | |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 467 | clear_packet(mess, end, NULL); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 468 | do_options(context, mess, end, NULL, hostname, get_domain(mess->yiaddr), |
| 469 | domain, netid, subnet_addr, 0, 0, NULL); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 473 | log_packet(NULL, logaddr, mess->chaddr, mess->hlen, iface_name, message); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 474 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 475 | return message ? 0 : dhcp_packet_size(mess, netid); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 476 | } |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 477 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 478 | if ((opt = option_find(mess, sz, OPTION_CLIENT_FQDN, 4))) |
| 479 | { |
| 480 | /* http://tools.ietf.org/wg/dhc/draft-ietf-dhc-fqdn-option/draft-ietf-dhc-fqdn-option-10.txt */ |
| 481 | int len = option_len(opt); |
| 482 | char *pq = daemon->dhcp_buff; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 483 | unsigned char *pp, *op = option_ptr(opt, 0); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 484 | |
| 485 | fqdn_flags = *op; |
| 486 | len -= 3; |
| 487 | op += 3; |
| 488 | pp = op; |
| 489 | |
| 490 | /* Always force update, since the client has no way to do it itself. */ |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 491 | if (!(fqdn_flags & 0x01)) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 492 | fqdn_flags |= 0x02; |
| 493 | |
| 494 | fqdn_flags &= ~0x08; |
| 495 | fqdn_flags |= 0x01; |
| 496 | |
| 497 | if (fqdn_flags & 0x04) |
| 498 | while (*op != 0 && ((op + (*op) + 1) - pp) < len) |
| 499 | { |
| 500 | memcpy(pq, op+1, *op); |
| 501 | pq += *op; |
| 502 | op += (*op)+1; |
| 503 | *(pq++) = '.'; |
| 504 | } |
| 505 | else |
| 506 | { |
| 507 | memcpy(pq, op, len); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 508 | if (len > 0 && op[len-1] == 0) |
| 509 | borken_opt = 1; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 510 | pq += len + 1; |
| 511 | } |
| 512 | |
| 513 | if (pq != daemon->dhcp_buff) |
| 514 | pq--; |
| 515 | |
| 516 | *pq = 0; |
| 517 | |
| 518 | if (canonicalise(daemon->dhcp_buff)) |
| 519 | offer_hostname = client_hostname = daemon->dhcp_buff; |
| 520 | } |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 521 | else if ((opt = option_find(mess, sz, OPTION_HOSTNAME, 1))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 522 | { |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 523 | int len = option_len(opt); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 524 | memcpy(daemon->dhcp_buff, option_ptr(opt, 0), len); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 525 | /* Microsoft clients are broken, and need zero-terminated strings |
| 526 | in options. We detect this state here, and do the same in |
| 527 | any options we send */ |
| 528 | if (len > 0 && daemon->dhcp_buff[len-1] == 0) |
| 529 | borken_opt = 1; |
| 530 | else |
| 531 | daemon->dhcp_buff[len] = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 532 | if (canonicalise(daemon->dhcp_buff)) |
| 533 | client_hostname = daemon->dhcp_buff; |
| 534 | } |
| 535 | |
| 536 | if (have_config(config, CONFIG_NAME)) |
| 537 | { |
| 538 | hostname = config->hostname; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 539 | domain = config->domain; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 540 | hostname_auth = 1; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 541 | /* 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] | 542 | if (fqdn_flags != 0 || !client_hostname || hostname_isequal(hostname, client_hostname)) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 543 | offer_hostname = hostname; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 544 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 545 | else if (client_hostname) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 546 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 547 | domain = strip_hostname(client_hostname); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 548 | |
| 549 | if (strlen(client_hostname) != 0) |
| 550 | { |
| 551 | hostname = client_hostname; |
| 552 | if (!config) |
| 553 | { |
| 554 | /* Search again now we have a hostname. |
| 555 | Only accept configs without CLID and HWADDR here, (they won't match) |
| 556 | to avoid impersonation by name. */ |
| 557 | struct dhcp_config *new = find_config(daemon->dhcp_conf, context, NULL, 0, |
| 558 | mess->chaddr, mess->hlen, |
| 559 | mess->htype, hostname); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 560 | if (new && !have_config(new, CONFIG_CLID) && !new->hwaddr) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 561 | { |
| 562 | config = new; |
| 563 | /* set "known" tag for known hosts */ |
| 564 | known_id.net = "known"; |
| 565 | known_id.next = netid; |
| 566 | netid = &known_id; |
| 567 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 568 | } |
| 569 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 570 | } |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 571 | |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 572 | if (have_config(config, CONFIG_NETID)) |
| 573 | { |
| 574 | config->netid.next = netid; |
| 575 | netid = &config->netid; |
| 576 | } |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 577 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 578 | /* dhcp-match. If we have hex-and-wildcards, look for a left-anchored match. |
| 579 | Otherwise assume the option is an array, and look for a matching element. |
| 580 | If no data given, existance of the option is enough. */ |
| 581 | for (o = daemon->dhcp_match; o; o = o->next) |
| 582 | { |
| 583 | int i, matched = 0; |
| 584 | |
| 585 | if (!(opt = option_find(mess, sz, o->opt, 1)) || |
| 586 | o->len > option_len(opt)) |
| 587 | continue; |
| 588 | |
| 589 | if (o->len == 0) |
| 590 | matched = 1; |
| 591 | else if (o->flags & DHOPT_HEX) |
| 592 | { |
| 593 | if (memcmp_masked(o->val, option_ptr(opt, 0), o->len, o->u.wildcard_mask)) |
| 594 | matched = 1; |
| 595 | } |
| 596 | else |
| 597 | for (i = 0; i <= (option_len(opt) - o->len); ) |
| 598 | { |
| 599 | if (memcmp(o->val, option_ptr(opt, i), o->len) == 0) |
| 600 | { |
| 601 | matched = 1; |
| 602 | break; |
| 603 | } |
| 604 | |
| 605 | if (o->flags & DHOPT_STRING) |
| 606 | i++; |
| 607 | else |
| 608 | i += o->len; |
| 609 | } |
| 610 | |
| 611 | if (matched) |
| 612 | { |
| 613 | o->netid->next = netid; |
| 614 | netid = o->netid; |
| 615 | } |
| 616 | } |
| 617 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 618 | /* user-class options are, according to RFC3004, supposed to contain |
| 619 | a set of counted strings. Here we check that this is so (by seeing |
| 620 | if the counts are consistent with the overall option length) and if |
| 621 | so zero the counts so that we don't get spurious matches between |
| 622 | the vendor string and the counts. If the lengths don't add up, we |
| 623 | assume that the option is a single string and non RFC3004 compliant |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 624 | and just do the substring match. dhclient provides these broken options. |
| 625 | The code, later, which sends user-class data to the lease-change script |
| 626 | relies on the transformation done here. |
| 627 | */ |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 628 | |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 629 | if ((opt = option_find(mess, sz, OPTION_USER_CLASS, 1))) |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 630 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 631 | unsigned char *ucp = option_ptr(opt, 0); |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 632 | int tmp, j; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 633 | for (j = 0; j < option_len(opt); j += ucp[j] + 1); |
| 634 | if (j == option_len(opt)) |
| 635 | for (j = 0; j < option_len(opt); j = tmp) |
| 636 | { |
| 637 | tmp = j + ucp[j] + 1; |
| 638 | ucp[j] = 0; |
| 639 | } |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 640 | } |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 641 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 642 | for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 643 | { |
| 644 | int mopt; |
| 645 | |
| 646 | if (vendor->match_type == MATCH_VENDOR) |
| 647 | mopt = OPTION_VENDOR_ID; |
| 648 | else if (vendor->match_type == MATCH_USER) |
| 649 | mopt = OPTION_USER_CLASS; |
| 650 | else |
| 651 | continue; |
| 652 | |
| 653 | if ((opt = option_find(mess, sz, mopt, 1))) |
| 654 | { |
| 655 | int i; |
| 656 | for (i = 0; i <= (option_len(opt) - vendor->len); i++) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 657 | if (memcmp(vendor->data, option_ptr(opt, i), vendor->len) == 0) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 658 | { |
| 659 | vendor->netid.next = netid; |
| 660 | netid = &vendor->netid; |
| 661 | break; |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 666 | /* mark vendor-encapsulated options which match the client-supplied vendor class */ |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 667 | match_vendor_opts(option_find(mess, sz, OPTION_VENDOR_ID, 1), daemon->dhcp_opts); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 668 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 669 | if (daemon->options & OPT_LOG_OPTS) |
| 670 | { |
| 671 | if (sanitise(option_find(mess, sz, OPTION_VENDOR_ID, 1), daemon->namebuff)) |
| 672 | my_syslog(LOG_INFO, _("Vendor class: %s"), daemon->namebuff); |
| 673 | if (sanitise(option_find(mess, sz, OPTION_USER_CLASS, 1), daemon->namebuff)) |
| 674 | my_syslog(LOG_INFO, _("User class: %s"), daemon->namebuff); |
| 675 | } |
| 676 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 677 | /* if all the netids in the ignore list are present, ignore this client */ |
| 678 | for (id_list = daemon->dhcp_ignore; id_list; id_list = id_list->next) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 679 | if (match_netid(id_list->list, netid, 0)) |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 680 | ignore = 1; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 681 | |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 682 | /* Can have setting to ignore the client ID for a particular MAC address or hostname */ |
| 683 | if (have_config(config, CONFIG_NOCLID)) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 684 | clid = NULL; |
| 685 | |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 686 | if ((opt = option_find(mess, sz, OPTION_REQUESTED_OPTIONS, 0))) |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 687 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 688 | req_options = (unsigned char *)daemon->dhcp_buff2; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 689 | memcpy(req_options, option_ptr(opt, 0), option_len(opt)); |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 690 | req_options[option_len(opt)] = OPTION_END; |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 691 | } |
| 692 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 693 | switch (mess_type) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 694 | { |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 695 | case DHCPDECLINE: |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 696 | if (!(opt = option_find(mess, sz, OPTION_SERVER_IDENTIFIER, INADDRSZ)) || |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 697 | option_addr(opt).s_addr != server_id(context, override, fallback).s_addr) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 698 | return 0; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 699 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 700 | /* sanitise any message. Paranoid? Moi? */ |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 701 | sanitise(option_find(mess, sz, OPTION_MESSAGE, 1), daemon->dhcp_buff); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 702 | |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 703 | if (!(opt = option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ))) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 704 | return 0; |
| 705 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 706 | log_packet("DECLINE", option_ptr(opt, 0), emac, emac_len, iface_name, daemon->dhcp_buff); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 707 | |
| 708 | if (lease && lease->addr.s_addr == option_addr(opt).s_addr) |
| 709 | lease_prune(lease, now); |
| 710 | |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 711 | if (have_config(config, CONFIG_ADDR) && |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 712 | config->addr.s_addr == option_addr(opt).s_addr) |
| 713 | { |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 714 | prettyprint_time(daemon->dhcp_buff, DECLINE_BACKOFF); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 715 | my_syslog(LOG_WARNING, _("disabling DHCP static address %s for %s"), |
| 716 | inet_ntoa(config->addr), daemon->dhcp_buff); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 717 | config->flags |= CONFIG_DECLINED; |
| 718 | config->decline_time = now; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 719 | } |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 720 | else |
| 721 | /* make sure this host gets a different address next time. */ |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 722 | for (; context; context = context->current) |
| 723 | context->addr_epoch++; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 724 | |
| 725 | return 0; |
| 726 | |
| 727 | case DHCPRELEASE: |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 728 | if (!(context = narrow_context(context, mess->ciaddr, netid)) || |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 729 | !(opt = option_find(mess, sz, OPTION_SERVER_IDENTIFIER, INADDRSZ)) || |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 730 | option_addr(opt).s_addr != server_id(context, override, fallback).s_addr) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 731 | return 0; |
| 732 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 733 | if (lease && lease->addr.s_addr == mess->ciaddr.s_addr) |
| 734 | lease_prune(lease, now); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 735 | else |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 736 | message = _("unknown lease"); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 737 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 738 | log_packet("RELEASE", &mess->ciaddr, emac, emac_len, iface_name, message); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 739 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 740 | return 0; |
| 741 | |
| 742 | case DHCPDISCOVER: |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 743 | if (ignore || have_config(config, CONFIG_DISABLE)) |
| 744 | { |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 745 | message = _("ignored"); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 746 | opt = NULL; |
| 747 | } |
| 748 | else |
| 749 | { |
| 750 | struct in_addr addr, conf; |
| 751 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 752 | addr.s_addr = conf.s_addr = 0; |
| 753 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 754 | if ((opt = option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ))) |
| 755 | addr = option_addr(opt); |
| 756 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 757 | if (have_config(config, CONFIG_ADDR)) |
| 758 | { |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 759 | char *addrs = inet_ntoa(config->addr); |
| 760 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 761 | if ((ltmp = lease_find_by_addr(config->addr)) && |
| 762 | ltmp != lease && |
| 763 | !config_has_mac(config, ltmp->hwaddr, ltmp->hwaddr_len, ltmp->hwaddr_type)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 764 | { |
| 765 | int len; |
| 766 | unsigned char *mac = extended_hwaddr(ltmp->hwaddr_type, ltmp->hwaddr_len, |
| 767 | ltmp->hwaddr, ltmp->clid_len, ltmp->clid, &len); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 768 | my_syslog(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] | 769 | addrs, print_mac(daemon->namebuff, mac, len)); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 770 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 771 | else |
| 772 | { |
| 773 | struct dhcp_context *tmp; |
| 774 | for (tmp = context; tmp; tmp = tmp->current) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 775 | if (context->router.s_addr == config->addr.s_addr) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 776 | break; |
| 777 | if (tmp) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 778 | my_syslog(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] | 779 | else if (have_config(config, CONFIG_DECLINED) && |
| 780 | difftime(now, config->decline_time) < (float)DECLINE_BACKOFF) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 781 | my_syslog(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] | 782 | else |
| 783 | conf = config->addr; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | if (conf.s_addr) |
| 788 | mess->yiaddr = conf; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 789 | else if (lease && address_available(context, lease->addr, netid)) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 790 | mess->yiaddr = lease->addr; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 791 | else if (opt && address_available(context, addr, netid) && !lease_find_by_addr(addr) && |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 792 | !config_find_by_address(daemon->dhcp_conf, addr)) |
| 793 | mess->yiaddr = addr; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 794 | else if (emac_len == 0) |
| 795 | message = _("no unique-id"); |
| 796 | else if (!address_allocate(context, &mess->yiaddr, emac, emac_len, netid, now)) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 797 | message = _("no address available"); |
| 798 | } |
| 799 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 800 | log_packet("DISCOVER", opt ? option_ptr(opt, 0) : NULL, emac, emac_len, iface_name, message); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 801 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 802 | if (message || !(context = narrow_context(context, mess->yiaddr, netid))) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 803 | return 0; |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 804 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 805 | log_packet("OFFER" , &mess->yiaddr, emac, emac_len, iface_name, NULL); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 806 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 807 | if (context->netid.net) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 808 | { |
| 809 | context->netid.next = netid; |
| 810 | netid = &context->netid; |
| 811 | } |
| 812 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 813 | time = calc_time(context, config, option_find(mess, sz, OPTION_LEASE_TIME, 4)); |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 814 | clear_packet(mess, end, agent_id); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 815 | option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPOFFER); |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 816 | 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] | 817 | option_put(mess, end, OPTION_LEASE_TIME, 4, time); |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 818 | /* 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] | 819 | if (time != 0xffffffff) |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 820 | { |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 821 | option_put(mess, end, OPTION_T1, 4, (time/2)); |
| 822 | option_put(mess, end, OPTION_T2, 4, (time*7)/8); |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 823 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 824 | do_options(context, mess, end, req_options, offer_hostname, get_domain(mess->yiaddr), |
| 825 | domain, netid, subnet_addr, fqdn_flags, borken_opt, agent_id); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 826 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 827 | return dhcp_packet_size(mess, netid); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 828 | |
| 829 | case DHCPREQUEST: |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 830 | if (ignore || have_config(config, CONFIG_DISABLE)) |
| 831 | return 0; |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 832 | if ((opt = option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 833 | { |
| 834 | /* SELECTING or INIT_REBOOT */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 835 | mess->yiaddr = option_addr(opt); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 836 | |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 837 | /* send vendor and user class info for new or recreated lease */ |
| 838 | do_classes = 1; |
| 839 | |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 840 | if ((opt = option_find(mess, sz, OPTION_SERVER_IDENTIFIER, INADDRSZ))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 841 | { |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 842 | /* SELECTING */ |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 843 | selecting = 1; |
| 844 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 845 | if (override.s_addr != 0) |
| 846 | { |
| 847 | if (option_addr(opt).s_addr != override.s_addr) |
| 848 | return 0; |
| 849 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 850 | else |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 851 | { |
| 852 | for (; context; context = context->current) |
| 853 | if (context->local.s_addr == option_addr(opt).s_addr) |
| 854 | break; |
| 855 | |
| 856 | if (!context) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 857 | { |
| 858 | /* In auth mode, a REQUEST sent to the wrong server |
| 859 | should be faulted, so that the client establishes |
| 860 | communication with us, otherwise, silently ignore. */ |
| 861 | if (!(daemon->options & OPT_AUTHORITATIVE)) |
| 862 | return 0; |
| 863 | message = _("wrong server-ID"); |
| 864 | } |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 865 | } |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 866 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 867 | /* If a lease exists for this host and another address, squash it. */ |
| 868 | if (lease && lease->addr.s_addr != mess->yiaddr.s_addr) |
| 869 | { |
| 870 | lease_prune(lease, now); |
| 871 | lease = NULL; |
| 872 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 873 | } |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 874 | else |
| 875 | { |
| 876 | /* INIT-REBOOT */ |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 877 | if (!lease && !(daemon->options & OPT_AUTHORITATIVE)) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 878 | return 0; |
| 879 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 880 | if (lease && lease->addr.s_addr != mess->yiaddr.s_addr) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 881 | { |
| 882 | message = _("wrong address"); |
| 883 | /* avoid loops when client brain-dead */ |
| 884 | lease_prune(lease, now); |
| 885 | lease = NULL; |
| 886 | } |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 887 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 888 | } |
| 889 | else |
| 890 | { |
| 891 | /* RENEWING or REBINDING */ |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 892 | /* Check existing lease for this address. |
| 893 | We allow it to be missing if dhcp-authoritative mode |
| 894 | as long as we can allocate the lease now - checked below. |
| 895 | This makes for a smooth recovery from a lost lease DB */ |
| 896 | if ((lease && mess->ciaddr.s_addr != lease->addr.s_addr) || |
| 897 | (!lease && !(daemon->options & OPT_AUTHORITATIVE))) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 898 | { |
| 899 | message = _("lease not found"); |
| 900 | /* ensure we broadcast NAK */ |
| 901 | unicast_dest = 0; |
| 902 | } |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 903 | /* desynchronise renewals */ |
| 904 | fuzz = rand16(); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 905 | mess->yiaddr = mess->ciaddr; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 908 | log_packet("REQUEST", &mess->yiaddr, emac, emac_len, iface_name, NULL); |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 909 | |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 910 | if (!message) |
| 911 | { |
| 912 | struct dhcp_config *addr_config; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 913 | struct dhcp_context *tmp = NULL; |
| 914 | |
| 915 | if (have_config(config, CONFIG_ADDR)) |
| 916 | for (tmp = context; tmp; tmp = tmp->current) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 917 | if (context->router.s_addr == config->addr.s_addr) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 918 | break; |
Simon Kelley | aedef83 | 2006-01-22 14:02:31 +0000 | [diff] [blame] | 919 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 920 | if (!(context = narrow_context(context, mess->yiaddr, netid))) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 921 | { |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 922 | /* 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] | 923 | message = _("wrong network"); |
| 924 | /* ensure we broadcast NAK */ |
| 925 | unicast_dest = 0; |
| 926 | } |
| 927 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 928 | /* Check for renewal of a lease which is outside the allowed range. */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 929 | else if (!address_available(context, mess->yiaddr, netid) && |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 930 | (!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] | 931 | message = _("address not available"); |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 932 | |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 933 | /* Check if a new static address has been configured. Be very sure that |
| 934 | when the client does DISCOVER, it will get the static address, otherwise |
| 935 | an endless protocol loop will ensue. */ |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 936 | else if (!tmp && !selecting && |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 937 | have_config(config, CONFIG_ADDR) && |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 938 | (!have_config(config, CONFIG_DECLINED) || |
| 939 | difftime(now, config->decline_time) > (float)DECLINE_BACKOFF) && |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 940 | config->addr.s_addr != mess->yiaddr.s_addr && |
| 941 | (!(ltmp = lease_find_by_addr(config->addr)) || ltmp == lease)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 942 | message = _("static lease available"); |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 943 | |
| 944 | /* 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] | 945 | 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] | 946 | message = _("address reserved"); |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 947 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 948 | else if (!lease && (ltmp = lease_find_by_addr(mess->yiaddr))) |
| 949 | { |
| 950 | /* If a host is configured with more than one MAC address, it's OK to 'nix |
| 951 | a lease from one of it's MACs to give the address to another. */ |
| 952 | if (config && config_has_mac(config, ltmp->hwaddr, ltmp->hwaddr_len, ltmp->hwaddr_type)) |
| 953 | { |
| 954 | my_syslog(LOG_INFO, _("abandoning lease to %s of %s"), |
| 955 | print_mac(daemon->namebuff, ltmp->hwaddr, ltmp->hwaddr_len), |
| 956 | inet_ntoa(ltmp->addr)); |
| 957 | lease = ltmp; |
| 958 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 959 | else |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 960 | message = _("address in use"); |
| 961 | } |
| 962 | |
| 963 | if (!message) |
| 964 | { |
| 965 | if (emac_len == 0) |
| 966 | message = _("no unique-id"); |
| 967 | |
| 968 | else if (!lease) |
| 969 | { |
| 970 | if ((lease = lease_allocate(mess->yiaddr))) |
| 971 | do_classes = 1; |
| 972 | else |
| 973 | message = _("no leases left"); |
| 974 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 975 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 976 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 977 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 978 | if (message) |
| 979 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 980 | log_packet("NAK", &mess->yiaddr, emac, emac_len, iface_name, message); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 981 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 982 | mess->yiaddr.s_addr = 0; |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 983 | clear_packet(mess, end, agent_id); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 984 | option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPNAK); |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 985 | 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] | 986 | option_put_string(mess, end, OPTION_MESSAGE, message, borken_opt); |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 987 | /* DHCPNAK gets agent-id too */ |
| 988 | restore_agent_id(agent_id, mess, end); |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 989 | /* This fixes a problem with the DHCP spec, broadcasting a NAK to a host on |
| 990 | a distant subnet which unicast a REQ to us won't work. */ |
| 991 | if (!unicast_dest || mess->giaddr.s_addr != 0 || |
| 992 | mess->ciaddr.s_addr == 0 || is_same_net(context->local, mess->ciaddr, context->netmask)) |
| 993 | { |
| 994 | mess->flags |= htons(0x8000); /* broadcast */ |
| 995 | mess->ciaddr.s_addr = 0; |
| 996 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 997 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 998 | else |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 999 | { |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 1000 | if (do_classes) |
| 1001 | { |
| 1002 | lease->changed = 1; |
| 1003 | /* copy user-class and vendor class into new lease, for the script */ |
| 1004 | if ((opt = option_find(mess, sz, OPTION_USER_CLASS, 1))) |
| 1005 | { |
| 1006 | int len = option_len(opt); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1007 | unsigned char *ucp = option_ptr(opt, 0); |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 1008 | /* If the user-class option started as counted strings, the first byte will be zero. */ |
| 1009 | if (len != 0 && ucp[0] == 0) |
| 1010 | ucp++, len--; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1011 | free(lease->userclass); |
| 1012 | if ((lease->userclass = whine_malloc(len+1))) |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 1013 | { |
| 1014 | memcpy(lease->userclass, ucp, len); |
| 1015 | lease->userclass[len] = 0; |
| 1016 | lease->userclass_len = len+1; |
| 1017 | } |
| 1018 | } |
| 1019 | if ((opt = option_find(mess, sz, OPTION_VENDOR_ID, 1))) |
| 1020 | { |
| 1021 | int len = option_len(opt); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1022 | unsigned char *ucp = option_ptr(opt, 0); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1023 | free(lease->vendorclass); |
| 1024 | if ((lease->vendorclass = whine_malloc(len+1))) |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 1025 | { |
| 1026 | memcpy(lease->vendorclass, ucp, len); |
| 1027 | lease->vendorclass[len] = 0; |
| 1028 | lease->vendorclass_len = len+1; |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1033 | if (!hostname_auth && (client_hostname = host_from_dns(mess->yiaddr))) |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 1034 | { |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1035 | hostname = client_hostname; |
| 1036 | hostname_auth = 1; |
| 1037 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1038 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1039 | if (context->netid.net) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 1040 | { |
| 1041 | context->netid.next = netid; |
| 1042 | netid = &context->netid; |
| 1043 | } |
| 1044 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1045 | 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] | 1046 | lease_set_hwaddr(lease, mess->chaddr, clid, mess->hlen, mess->htype, clid_len); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1047 | |
| 1048 | /* if all the netids in the ignore_name list are present, ignore client-supplied name */ |
| 1049 | if (!hostname_auth) |
| 1050 | { |
| 1051 | for (id_list = daemon->dhcp_ignore_names; id_list; id_list = id_list->next) |
| 1052 | if ((!id_list->list) || match_netid(id_list->list, netid, 0)) |
| 1053 | break; |
| 1054 | if (id_list) |
| 1055 | hostname = NULL; |
| 1056 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1057 | if (hostname) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1058 | lease_set_hostname(lease, hostname, hostname_auth); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1059 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1060 | lease_set_expires(lease, time, now); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1061 | lease_set_interface(lease, int_index); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1062 | |
| 1063 | if (override.s_addr != 0) |
| 1064 | lease->override = override; |
| 1065 | else |
| 1066 | override = lease->override; |
| 1067 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1068 | log_packet("ACK", &mess->yiaddr, emac, emac_len, iface_name, hostname); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1069 | |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 1070 | clear_packet(mess, end, agent_id); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1071 | option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPACK); |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1072 | 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] | 1073 | option_put(mess, end, OPTION_LEASE_TIME, 4, time); |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 1074 | if (time != 0xffffffff) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1075 | { |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 1076 | while (fuzz > (time/16)) |
| 1077 | fuzz = fuzz/2; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1078 | option_put(mess, end, OPTION_T1, 4, (time/2) - fuzz); |
| 1079 | option_put(mess, end, OPTION_T2, 4, ((time/8)*7) - fuzz); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1080 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1081 | do_options(context, mess, end, req_options, hostname, get_domain(mess->yiaddr), |
| 1082 | domain, netid, subnet_addr, fqdn_flags, borken_opt, agent_id); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1083 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1084 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1085 | return dhcp_packet_size(mess, netid); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1086 | |
| 1087 | case DHCPINFORM: |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 1088 | if (ignore || have_config(config, CONFIG_DISABLE)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1089 | message = _("ignored"); |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 1090 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1091 | log_packet("INFORM", &mess->ciaddr, emac, emac_len, iface_name, message); |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1092 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1093 | if (message || mess->ciaddr.s_addr == 0) |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1094 | return 0; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1095 | |
| 1096 | /* For DHCPINFORM only, cope without a valid context */ |
| 1097 | context = narrow_context(context, mess->ciaddr, netid); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1098 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1099 | /* Find a least based on IP address if we didn't |
| 1100 | get one from MAC address/client-d */ |
| 1101 | if (!lease && |
| 1102 | (lease = lease_find_by_addr(mess->ciaddr)) && |
| 1103 | lease->hostname) |
| 1104 | hostname = lease->hostname; |
| 1105 | |
| 1106 | if (!hostname) |
| 1107 | hostname = host_from_dns(mess->ciaddr); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1108 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1109 | log_packet("ACK", &mess->ciaddr, emac, emac_len, iface_name, hostname); |
| 1110 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1111 | if (context && context->netid.net) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 1112 | { |
| 1113 | context->netid.next = netid; |
| 1114 | netid = &context->netid; |
| 1115 | } |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1116 | |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 1117 | if (lease) |
| 1118 | { |
| 1119 | if (override.s_addr != 0) |
| 1120 | lease->override = override; |
| 1121 | else |
| 1122 | override = lease->override; |
| 1123 | } |
| 1124 | |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 1125 | clear_packet(mess, end, agent_id); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1126 | option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPACK); |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1127 | option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, ntohl(server_id(context, override, fallback).s_addr)); |
| 1128 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1129 | if (lease) |
| 1130 | { |
| 1131 | if (lease->expires == 0) |
| 1132 | time = 0xffffffff; |
| 1133 | else |
| 1134 | time = (unsigned int)difftime(lease->expires, now); |
| 1135 | option_put(mess, end, OPTION_LEASE_TIME, 4, time); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1136 | lease_set_interface(lease, int_index); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1137 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1138 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1139 | do_options(context, mess, end, req_options, hostname, get_domain(mess->ciaddr), |
| 1140 | domain, netid, subnet_addr, fqdn_flags, borken_opt, agent_id); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1141 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1142 | *is_inform = 1; /* handle reply differently */ |
| 1143 | return dhcp_packet_size(mess, netid); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1149 | /* find a good value to use as MAC address for logging and address-allocation hashing. |
| 1150 | This is normally just the chaddr field from the DHCP packet, |
| 1151 | but eg Firewire will have hlen == 0 and use the client-id instead. |
| 1152 | This could be anything, but will normally be EUI64 for Firewire. |
| 1153 | We assume that if the first byte of the client-id equals the htype byte |
| 1154 | then the client-id is using the usual encoding and use the rest of the |
| 1155 | client-id: if not we can use the whole client-id. This should give |
| 1156 | sane MAC address logs. */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1157 | unsigned char *extended_hwaddr(int hwtype, int hwlen, unsigned char *hwaddr, |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1158 | int clid_len, unsigned char *clid, int *len_out) |
| 1159 | { |
| 1160 | if (hwlen == 0 && clid && clid_len > 3) |
| 1161 | { |
| 1162 | if (clid[0] == hwtype) |
| 1163 | { |
| 1164 | *len_out = clid_len - 1 ; |
| 1165 | return clid + 1; |
| 1166 | } |
| 1167 | |
| 1168 | #if defined(ARPHRD_EUI64) && defined(ARPHRD_IEEE1394) |
| 1169 | if (clid[0] == ARPHRD_EUI64 && hwtype == ARPHRD_IEEE1394) |
| 1170 | { |
| 1171 | *len_out = clid_len - 1 ; |
| 1172 | return clid + 1; |
| 1173 | } |
| 1174 | #endif |
| 1175 | |
| 1176 | *len_out = clid_len; |
| 1177 | return clid; |
| 1178 | } |
| 1179 | |
| 1180 | *len_out = hwlen; |
| 1181 | return hwaddr; |
| 1182 | } |
| 1183 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1184 | 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] | 1185 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1186 | 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] | 1187 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1188 | if (opt) |
| 1189 | { |
| 1190 | unsigned int req_time = option_uint(opt, 4); |
| 1191 | if (req_time < 120 ) |
| 1192 | req_time = 120; /* sanity */ |
| 1193 | if (time == 0xffffffff || (req_time != 0xffffffff && req_time < time)) |
| 1194 | time = req_time; |
| 1195 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1196 | |
| 1197 | return time; |
| 1198 | } |
| 1199 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1200 | 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] | 1201 | { |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1202 | if (override.s_addr != 0) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1203 | return override; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1204 | else if (context) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1205 | return context->local; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1206 | else |
| 1207 | return fallback; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1208 | } |
| 1209 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1210 | static int sanitise(unsigned char *opt, char *buf) |
| 1211 | { |
| 1212 | char *p; |
| 1213 | int i; |
| 1214 | |
| 1215 | *buf = 0; |
| 1216 | |
| 1217 | if (!opt) |
| 1218 | return 0; |
| 1219 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1220 | p = option_ptr(opt, 0); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1221 | |
| 1222 | for (i = option_len(opt); i > 0; i--) |
| 1223 | { |
| 1224 | char c = *p++; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1225 | if (isprint((int)c)) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1226 | *buf++ = c; |
| 1227 | } |
| 1228 | *buf = 0; /* add terminator */ |
| 1229 | |
| 1230 | return 1; |
| 1231 | } |
| 1232 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1233 | static void log_packet(char *type, void *addr, unsigned char *ext_mac, |
| 1234 | int mac_len, char *interface, char *string) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1235 | { |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1236 | struct in_addr a; |
| 1237 | |
| 1238 | /* addr may be misaligned */ |
| 1239 | if (addr) |
| 1240 | memcpy(&a, addr, sizeof(a)); |
| 1241 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1242 | my_syslog(LOG_INFO, "%s%s(%s) %s%s%s %s", |
| 1243 | type ? "DHCP" : "BOOTP", |
| 1244 | type ? type : "", |
| 1245 | interface, |
| 1246 | addr ? inet_ntoa(a) : "", |
| 1247 | addr ? " " : "", |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1248 | print_mac(daemon->namebuff, ext_mac, mac_len), |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1249 | string ? string : ""); |
| 1250 | } |
| 1251 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1252 | static void log_options(unsigned char *start) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1253 | { |
| 1254 | while (*start != OPTION_END) |
| 1255 | { |
| 1256 | char *text = option_string(start[0]); |
| 1257 | unsigned char trunc = start[1] < 13 ? start[1] : 13; |
| 1258 | my_syslog(LOG_INFO, "sent size:%3d option:%3d%s%s%s%s%s", |
| 1259 | start[1], start[0], |
| 1260 | text ? ":" : "", text ? text : "", |
| 1261 | start[1] == 0 ? "" : " ", |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1262 | start[1] == 0 ? "" : print_mac(daemon->namebuff, &start[2], trunc), |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1263 | trunc == start[1] ? "" : "..."); |
| 1264 | start += start[1] + 2; |
| 1265 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1268 | 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] | 1269 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1270 | while (1) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1271 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1272 | if (p > end) |
| 1273 | return NULL; |
| 1274 | else if (*p == OPTION_END) |
| 1275 | return opt == OPTION_END ? p : NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1276 | else if (*p == OPTION_PAD) |
| 1277 | p++; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1278 | else |
| 1279 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1280 | int opt_len; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1281 | if (p > end - 2) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1282 | return NULL; /* malformed packet */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1283 | opt_len = option_len(p); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1284 | if (p > end - (2 + opt_len)) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1285 | return NULL; /* malformed packet */ |
| 1286 | if (*p == opt && opt_len >= minsize) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1287 | return p; |
| 1288 | p += opt_len + 2; |
| 1289 | } |
| 1290 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1293 | 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] | 1294 | { |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1295 | unsigned char *ret, *overload; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1296 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1297 | /* skip over DHCP cookie; */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1298 | if ((ret = option_find1(&mess->options[0] + sizeof(u32), ((unsigned char *)mess) + size, opt_type, minsize))) |
| 1299 | return ret; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1300 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1301 | /* look for overload option. */ |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1302 | 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] | 1303 | return NULL; |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 1304 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1305 | /* Can we look in filename area ? */ |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1306 | if ((overload[2] & 1) && |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1307 | (ret = option_find1(&mess->file[0], &mess->file[128], opt_type, minsize))) |
| 1308 | return ret; |
| 1309 | |
| 1310 | /* finally try sname area */ |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1311 | if ((overload[2] & 2) && |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1312 | (ret = option_find1(&mess->sname[0], &mess->sname[64], opt_type, minsize))) |
| 1313 | return ret; |
| 1314 | |
| 1315 | return NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1318 | static struct in_addr option_addr(unsigned char *opt) |
| 1319 | { |
| 1320 | /* this worries about unaligned data in the option. */ |
| 1321 | /* struct in_addr is network byte order */ |
| 1322 | struct in_addr ret; |
| 1323 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1324 | memcpy(&ret, option_ptr(opt, 0), INADDRSZ); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1325 | |
| 1326 | return ret; |
| 1327 | } |
| 1328 | |
| 1329 | static unsigned int option_uint(unsigned char *opt, int size) |
| 1330 | { |
| 1331 | /* this worries about unaligned data and byte order */ |
| 1332 | unsigned int ret = 0; |
| 1333 | int i; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1334 | unsigned char *p = option_ptr(opt, 0); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1335 | |
| 1336 | for (i = 0; i < size; i++) |
| 1337 | ret = (ret << 8) | *p++; |
| 1338 | |
| 1339 | return ret; |
| 1340 | } |
| 1341 | |
| 1342 | static unsigned char *dhcp_skip_opts(unsigned char *start) |
| 1343 | { |
| 1344 | while (*start != 0) |
| 1345 | start += start[1] + 2; |
| 1346 | return start; |
| 1347 | } |
| 1348 | |
| 1349 | /* only for use when building packet: doesn't check for bad data. */ |
| 1350 | static unsigned char *find_overload(struct dhcp_packet *mess) |
| 1351 | { |
| 1352 | unsigned char *p = &mess->options[0] + sizeof(u32); |
| 1353 | |
| 1354 | while (*p != 0) |
| 1355 | { |
| 1356 | if (*p == OPTION_OVERLOAD) |
| 1357 | return p; |
| 1358 | p += p[1] + 2; |
| 1359 | } |
| 1360 | return NULL; |
| 1361 | } |
| 1362 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1363 | static size_t dhcp_packet_size(struct dhcp_packet *mess, struct dhcp_netid *netid) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1364 | { |
| 1365 | unsigned char *p = dhcp_skip_opts(&mess->options[0] + sizeof(u32)); |
| 1366 | unsigned char *overload; |
| 1367 | size_t ret; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1368 | struct dhcp_netid_list *id_list; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1369 | struct dhcp_netid *n; |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1370 | |
| 1371 | /* We do logging too */ |
| 1372 | if (netid && (daemon->options & OPT_LOG_OPTS)) |
| 1373 | { |
| 1374 | char *p = daemon->namebuff; |
| 1375 | *p = 0; |
| 1376 | for (; netid; netid = netid->next) |
| 1377 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1378 | /* kill dupes. */ |
| 1379 | for (n = netid->next; n; n = n->next) |
| 1380 | if (strcmp(netid->net, n->net) == 0) |
| 1381 | break; |
| 1382 | |
| 1383 | if (!n) |
| 1384 | { |
| 1385 | strncat (p, netid->net, MAXDNAME); |
| 1386 | if (netid->next) |
| 1387 | strncat (p, ", ", MAXDNAME); |
| 1388 | } |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1389 | } |
| 1390 | p[MAXDNAME - 1] = 0; |
| 1391 | my_syslog(LOG_INFO, _("tags: %s"), p); |
| 1392 | } |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1393 | |
| 1394 | /* add END options to the regions. */ |
| 1395 | if ((overload = find_overload(mess))) |
| 1396 | { |
| 1397 | if (option_uint(overload, 1) & 1) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1398 | { |
| 1399 | *dhcp_skip_opts(mess->file) = OPTION_END; |
| 1400 | if (daemon->options & OPT_LOG_OPTS) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1401 | log_options(mess->file); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1402 | } |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1403 | if (option_uint(overload, 1) & 2) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1404 | { |
| 1405 | *dhcp_skip_opts(mess->sname) = OPTION_END; |
| 1406 | if (daemon->options & OPT_LOG_OPTS) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1407 | log_options(mess->sname); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1408 | } |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | *p++ = OPTION_END; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1412 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1413 | if (daemon->options & OPT_LOG_OPTS) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1414 | log_options(&mess->options[0] + sizeof(u32)); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1415 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1416 | for (id_list = daemon->force_broadcast; id_list; id_list = id_list->next) |
| 1417 | if (match_netid(id_list->list, netid, 0)) |
| 1418 | mess->flags |= htons(0x8000); /* force broadcast */ |
| 1419 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1420 | ret = (size_t)(p - (unsigned char *)mess); |
| 1421 | |
| 1422 | if (ret < MIN_PACKETSZ) |
| 1423 | ret = MIN_PACKETSZ; |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1424 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1425 | return ret; |
| 1426 | } |
| 1427 | |
| 1428 | static unsigned char *free_space(struct dhcp_packet *mess, unsigned char *end, int opt, int len) |
| 1429 | { |
| 1430 | unsigned char *p = dhcp_skip_opts(&mess->options[0] + sizeof(u32)); |
| 1431 | |
| 1432 | if (p + len + 3 >= end) |
| 1433 | /* not enough space in options area, try and use overload, if poss */ |
| 1434 | { |
| 1435 | unsigned char *overload; |
| 1436 | |
| 1437 | if (!(overload = find_overload(mess)) && |
| 1438 | (mess->file[0] == 0 || mess->sname[0] == 0)) |
| 1439 | { |
| 1440 | /* attempt to overload fname and sname areas, we've reserved space for the |
| 1441 | overflow option previuously. */ |
| 1442 | overload = p; |
| 1443 | *(p++) = OPTION_OVERLOAD; |
| 1444 | *(p++) = 1; |
| 1445 | } |
| 1446 | |
| 1447 | p = NULL; |
| 1448 | |
| 1449 | /* using filename field ? */ |
| 1450 | if (overload) |
| 1451 | { |
| 1452 | if (mess->file[0] == 0) |
| 1453 | overload[2] |= 1; |
| 1454 | |
| 1455 | if (overload[2] & 1) |
| 1456 | { |
| 1457 | p = dhcp_skip_opts(mess->file); |
| 1458 | if (p + len + 3 >= mess->file + sizeof(mess->file)) |
| 1459 | p = NULL; |
| 1460 | } |
| 1461 | |
| 1462 | if (!p) |
| 1463 | { |
| 1464 | /* try to bring sname into play (it may be already) */ |
| 1465 | if (mess->sname[0] == 0) |
| 1466 | overload[2] |= 2; |
| 1467 | |
| 1468 | if (overload[2] & 2) |
| 1469 | { |
| 1470 | p = dhcp_skip_opts(mess->sname); |
| 1471 | if (p + len + 3 >= mess->sname + sizeof(mess->file)) |
| 1472 | p = NULL; |
| 1473 | } |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | if (!p) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1478 | my_syslog(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] | 1479 | } |
| 1480 | |
| 1481 | if (p) |
| 1482 | { |
| 1483 | *(p++) = opt; |
| 1484 | *(p++) = len; |
| 1485 | } |
| 1486 | |
| 1487 | return p; |
| 1488 | } |
| 1489 | |
| 1490 | static void option_put(struct dhcp_packet *mess, unsigned char *end, int opt, int len, unsigned int val) |
| 1491 | { |
| 1492 | int i; |
| 1493 | unsigned char *p = free_space(mess, end, opt, len); |
| 1494 | |
| 1495 | if (p) |
| 1496 | for (i = 0; i < len; i++) |
| 1497 | *(p++) = val >> (8 * (len - (i + 1))); |
| 1498 | } |
| 1499 | |
| 1500 | static void option_put_string(struct dhcp_packet *mess, unsigned char *end, int opt, |
| 1501 | char *string, int null_term) |
| 1502 | { |
| 1503 | unsigned char *p; |
| 1504 | size_t len = strlen(string); |
| 1505 | |
| 1506 | if (null_term && len != 255) |
| 1507 | len++; |
| 1508 | |
| 1509 | if ((p = free_space(mess, end, opt, len))) |
| 1510 | memcpy(p, string, len); |
| 1511 | } |
| 1512 | |
| 1513 | /* return length, note this only does the data part */ |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1514 | 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] | 1515 | { |
| 1516 | int len = opt->len; |
| 1517 | |
| 1518 | if ((opt->flags & DHOPT_STRING) && null_term && len != 255) |
| 1519 | len++; |
| 1520 | |
| 1521 | if (p && len != 0) |
| 1522 | { |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1523 | if (context && (opt->flags & DHOPT_ADDR)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1524 | { |
| 1525 | int j; |
| 1526 | struct in_addr *a = (struct in_addr *)opt->val; |
| 1527 | for (j = 0; j < opt->len; j+=INADDRSZ, a++) |
| 1528 | { |
| 1529 | /* zero means "self" (but not in vendorclass options.) */ |
| 1530 | if (a->s_addr == 0) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1531 | memcpy(p, &context->local, INADDRSZ); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1532 | else |
| 1533 | memcpy(p, a, INADDRSZ); |
| 1534 | p += INADDRSZ; |
| 1535 | } |
| 1536 | } |
| 1537 | else |
| 1538 | memcpy(p, opt->val, len); |
| 1539 | } |
| 1540 | return len; |
| 1541 | } |
| 1542 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1543 | static int in_list(unsigned char *list, int opt) |
| 1544 | { |
| 1545 | int i; |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1546 | |
| 1547 | /* If no requested options, send everything, not nothing. */ |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1548 | if (!list) |
| 1549 | return 1; |
| 1550 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1551 | for (i = 0; list[i] != OPTION_END; i++) |
| 1552 | if (opt == list[i]) |
| 1553 | return 1; |
| 1554 | |
| 1555 | return 0; |
| 1556 | } |
| 1557 | |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 1558 | static struct dhcp_opt *option_find2(struct dhcp_netid *netid, struct dhcp_opt *opts, int opt) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1559 | { |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1560 | struct dhcp_opt *tmp; |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1561 | for (tmp = opts; tmp; tmp = tmp->next) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1562 | if (tmp->opt == opt && !(tmp->flags & (DHOPT_ENCAPSULATE | DHOPT_VENDOR))) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1563 | if (match_netid(tmp->netid, netid, 1) || match_netid(tmp->netid, netid, 0)) |
| 1564 | return tmp; |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 1565 | |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1566 | return netid ? option_find2(NULL, opts, opt) : NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1569 | /* mark vendor-encapsulated options which match the client-supplied or |
| 1570 | config-supplied vendor class */ |
| 1571 | static void match_vendor_opts(unsigned char *opt, struct dhcp_opt *dopt) |
| 1572 | { |
| 1573 | for (; dopt; dopt = dopt->next) |
| 1574 | { |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1575 | dopt->flags &= ~(DHOPT_ENCAP_MATCH | DHOPT_ENCAP_DONE); |
| 1576 | if (opt && (dopt->flags & DHOPT_VENDOR)) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1577 | { |
| 1578 | int i, len = 0; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1579 | if (dopt->u.vendor_class) |
| 1580 | len = strlen((char *)dopt->u.vendor_class); |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1581 | for (i = 0; i <= (option_len(opt) - len); i++) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1582 | 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] | 1583 | { |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1584 | dopt->flags |= DHOPT_ENCAP_MATCH; |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1585 | break; |
| 1586 | } |
| 1587 | } |
| 1588 | } |
| 1589 | } |
| 1590 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1591 | static void do_encap_opts(int encap, struct dhcp_packet *mess, unsigned char *end, int null_term) |
| 1592 | { |
| 1593 | int len, enc_len; |
| 1594 | struct dhcp_opt *opt, *start; |
| 1595 | unsigned char *p; |
| 1596 | |
| 1597 | /* find size in advance */ |
| 1598 | for (enc_len = 0, start = opt = daemon->dhcp_opts; opt; opt = opt->next) |
| 1599 | if (opt->flags & DHOPT_ENCAP_MATCH) |
| 1600 | { |
| 1601 | int new = do_opt(opt, NULL, NULL, null_term) + 2; |
| 1602 | if (enc_len + new <= 255) |
| 1603 | enc_len += new; |
| 1604 | else |
| 1605 | { |
| 1606 | p = free_space(mess, end, encap, enc_len); |
| 1607 | for (; start && start != opt; start = start->next) |
| 1608 | if (p && (start->flags & DHOPT_ENCAP_MATCH)) |
| 1609 | { |
| 1610 | len = do_opt(start, p + 2, NULL, null_term); |
| 1611 | *(p++) = start->opt; |
| 1612 | *(p++) = len; |
| 1613 | p += len; |
| 1614 | } |
| 1615 | enc_len = new; |
| 1616 | start = opt; |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | if (enc_len != 0 && |
| 1621 | (p = free_space(mess, end, encap, enc_len + 1))) |
| 1622 | { |
| 1623 | for (; start; start = start->next) |
| 1624 | if (start->flags & DHOPT_ENCAP_MATCH) |
| 1625 | { |
| 1626 | len = do_opt(start, p + 2, NULL, null_term); |
| 1627 | *(p++) = start->opt; |
| 1628 | *(p++) = len; |
| 1629 | p += len; |
| 1630 | } |
| 1631 | *p = OPTION_END; |
| 1632 | } |
| 1633 | } |
| 1634 | |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 1635 | static void clear_packet(struct dhcp_packet *mess, unsigned char *end, unsigned char *agent_id) |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1636 | { |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 1637 | /* don't clear agent_id */ |
| 1638 | if (agent_id) |
| 1639 | end = agent_id; |
| 1640 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1641 | memset(mess->sname, 0, sizeof(mess->sname)); |
| 1642 | memset(mess->file, 0, sizeof(mess->file)); |
| 1643 | memset(&mess->options[0] + sizeof(u32), 0, end - (&mess->options[0] + sizeof(u32))); |
| 1644 | mess->siaddr.s_addr = 0; |
| 1645 | } |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1646 | |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 1647 | static void restore_agent_id(unsigned char *agent_id, struct dhcp_packet *mess, unsigned char *real_end) |
| 1648 | { |
| 1649 | if (agent_id) |
| 1650 | { |
| 1651 | unsigned char *p = dhcp_skip_opts(&mess->options[0] + sizeof(u32)); |
| 1652 | memmove(p, agent_id, real_end - agent_id); |
| 1653 | p += real_end - agent_id; |
| 1654 | memset(p, 0, real_end - p); /* in case of overlap */ |
| 1655 | } |
| 1656 | } |
| 1657 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1658 | static void do_options(struct dhcp_context *context, |
| 1659 | struct dhcp_packet *mess, |
| 1660 | unsigned char *real_end, |
| 1661 | unsigned char *req_options, |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1662 | char *hostname, |
| 1663 | char *domain, char *config_domain, |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1664 | struct dhcp_netid *netid, |
| 1665 | struct in_addr subnet_addr, |
| 1666 | unsigned char fqdn_flags, |
| 1667 | int null_term, |
| 1668 | unsigned char *agent_id) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1669 | { |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1670 | struct dhcp_opt *opt, *config_opts = daemon->dhcp_opts; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1671 | struct dhcp_boot *boot; |
| 1672 | unsigned char *p, *end = agent_id ? agent_id : real_end; |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1673 | int i, len, force_encap = 0; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1674 | unsigned char f0 = 0, s0 = 0; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1675 | int done_file = 0, done_server = 0; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1676 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1677 | if (config_domain && (!domain || !hostname_isequal(domain, config_domain))) |
| 1678 | my_syslog(LOG_WARNING, _("Ignoring domain %s for DHCP host name %s"), config_domain, hostname); |
| 1679 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1680 | /* logging */ |
| 1681 | if ((daemon->options & OPT_LOG_OPTS) && req_options) |
| 1682 | { |
| 1683 | char *q = daemon->namebuff; |
| 1684 | for (i = 0; req_options[i] != OPTION_END; i++) |
| 1685 | { |
| 1686 | char *s = option_string(req_options[i]); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1687 | q += snprintf(q, MAXDNAME - (q - daemon->namebuff), |
| 1688 | "%d%s%s%s", |
| 1689 | req_options[i], |
| 1690 | s ? ":" : "", |
| 1691 | s ? s : "", |
| 1692 | req_options[i+1] == OPTION_END ? "" : ", "); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1693 | if (req_options[i+1] == OPTION_END || (q - daemon->namebuff) > 40) |
| 1694 | { |
| 1695 | q = daemon->namebuff; |
| 1696 | my_syslog(LOG_INFO, _("requested options: %s"), daemon->namebuff); |
| 1697 | } |
| 1698 | } |
| 1699 | } |
| 1700 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1701 | /* decide which dhcp-boot option we're using */ |
| 1702 | for (boot = daemon->boot_config; boot; boot = boot->next) |
| 1703 | if (match_netid(boot->netid, netid, 0)) |
| 1704 | break; |
| 1705 | if (!boot) |
| 1706 | /* No match, look for one without a netid */ |
| 1707 | for (boot = daemon->boot_config; boot; boot = boot->next) |
| 1708 | if (match_netid(boot->netid, netid, 1)) |
| 1709 | break; |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 1710 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1711 | if (context) |
| 1712 | mess->siaddr = context->local; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1713 | |
| 1714 | /* See if we can send the boot stuff as options. |
| 1715 | To do this we need a requested option list, BOOTP |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1716 | and very old DHCP clients won't have this, we also |
| 1717 | provide an manual option to disable it. |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1718 | Some PXE ROMs have bugs (surprise!) and need zero-terminated |
| 1719 | names, so we always send those. */ |
| 1720 | if (boot) |
| 1721 | { |
| 1722 | if (boot->sname) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1723 | { |
| 1724 | if (!(daemon->options & OPT_NO_OVERRIDE) && |
| 1725 | req_options && |
| 1726 | in_list(req_options, OPTION_SNAME)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1727 | option_put_string(mess, end, OPTION_SNAME, boot->sname, 1); |
| 1728 | else |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1729 | strncpy((char *)mess->sname, boot->sname, sizeof(mess->sname)-1); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | if (boot->file) |
| 1733 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1734 | if (!(daemon->options & OPT_NO_OVERRIDE) && |
| 1735 | req_options && |
| 1736 | in_list(req_options, OPTION_FILENAME)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1737 | option_put_string(mess, end, OPTION_FILENAME, boot->file, 1); |
| 1738 | else |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1739 | strncpy((char *)mess->file, boot->file, sizeof(mess->file)-1); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | if (boot->next_server.s_addr) |
| 1743 | mess->siaddr = boot->next_server; |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1744 | |
| 1745 | if (daemon->options & OPT_LOG_OPTS) |
| 1746 | my_syslog(LOG_INFO, _("next server: %s"), inet_ntoa(mess->siaddr)); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1747 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1748 | else |
| 1749 | /* Use the values of the relevant options if no dhcp-boot given and |
| 1750 | they're no explicitly asked for as options. */ |
| 1751 | { |
| 1752 | if ((!req_options || !in_list(req_options, OPTION_FILENAME)) && |
| 1753 | (opt = option_find2(netid, config_opts, OPTION_FILENAME))) |
| 1754 | { |
| 1755 | strncpy((char *)mess->file, (char *)opt->val, sizeof(mess->file)-1); |
| 1756 | done_file = 1; |
| 1757 | } |
| 1758 | |
| 1759 | if ((!req_options || !in_list(req_options, OPTION_SNAME)) && |
| 1760 | (opt = option_find2(netid, config_opts, OPTION_SNAME))) |
| 1761 | { |
| 1762 | strncpy((char *)mess->sname, (char *)opt->val, sizeof(mess->sname)-1); |
| 1763 | done_server = 1; |
| 1764 | } |
| 1765 | } |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1766 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1767 | if (daemon->options & OPT_LOG_OPTS) |
| 1768 | { |
| 1769 | if (strlen((char *)mess->file) != 0) |
| 1770 | my_syslog(LOG_INFO, _("bootfile name: %s"), (char *)mess->file); |
| 1771 | |
| 1772 | if (strlen((char *)mess->sname) != 0) |
| 1773 | my_syslog(LOG_INFO, _("server name: %s"), (char *)mess->sname); |
| 1774 | } |
| 1775 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1776 | /* We don't want to do option-overload for BOOTP, so make the file and sname |
| 1777 | fields look like they are in use, even when they aren't. This gets restored |
| 1778 | at the end of this function. */ |
| 1779 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1780 | if (!req_options || (daemon->options & OPT_NO_OVERRIDE)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1781 | { |
| 1782 | f0 = mess->file[0]; |
| 1783 | mess->file[0] = 1; |
| 1784 | s0 = mess->sname[0]; |
| 1785 | mess->sname[0] = 1; |
| 1786 | } |
| 1787 | |
| 1788 | /* At this point, if mess->sname or mess->file are zeroed, they are available |
| 1789 | for option overload, reserve space for the overload option. */ |
| 1790 | if (mess->file[0] == 0 || mess->sname[0] == 0) |
| 1791 | end -= 3; |
| 1792 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1793 | /* rfc3011 says this doesn't need to be in the requested options list. */ |
| 1794 | if (subnet_addr.s_addr) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1795 | 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^] | 1796 | |
| 1797 | /* replies to DHCPINFORM may not have a valid context */ |
| 1798 | if (context) |
| 1799 | { |
| 1800 | if (!option_find2(netid, config_opts, OPTION_NETMASK)) |
| 1801 | option_put(mess, end, OPTION_NETMASK, INADDRSZ, ntohl(context->netmask.s_addr)); |
| 1802 | |
| 1803 | /* May not have a "guessed" broadcast address if we got no packets via a relay |
| 1804 | from this net yet (ie just unicast renewals after a restart */ |
| 1805 | if (context->broadcast.s_addr && |
| 1806 | !option_find2(netid, config_opts, OPTION_BROADCAST)) |
| 1807 | option_put(mess, end, OPTION_BROADCAST, INADDRSZ, ntohl(context->broadcast.s_addr)); |
| 1808 | |
| 1809 | /* Same comments as broadcast apply, and also may not be able to get a sensible |
| 1810 | default when using subnet select. User must configure by steam in that case. */ |
| 1811 | if (context->router.s_addr && |
| 1812 | in_list(req_options, OPTION_ROUTER) && |
| 1813 | !option_find2(netid, config_opts, OPTION_ROUTER)) |
| 1814 | option_put(mess, end, OPTION_ROUTER, INADDRSZ, ntohl(context->router.s_addr)); |
| 1815 | |
| 1816 | if (in_list(req_options, OPTION_DNSSERVER) && |
| 1817 | !option_find2(netid, config_opts, OPTION_DNSSERVER)) |
| 1818 | option_put(mess, end, OPTION_DNSSERVER, INADDRSZ, ntohl(context->local.s_addr)); |
| 1819 | } |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1820 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1821 | if (domain && in_list(req_options, OPTION_DOMAINNAME) && |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 1822 | !option_find2(netid, config_opts, OPTION_DOMAINNAME)) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1823 | option_put_string(mess, end, OPTION_DOMAINNAME, domain, null_term); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1824 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1825 | /* 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] | 1826 | if (hostname) |
| 1827 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1828 | if (in_list(req_options, OPTION_HOSTNAME) && |
| 1829 | !option_find2(netid, config_opts, OPTION_HOSTNAME)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1830 | option_put_string(mess, end, OPTION_HOSTNAME, hostname, null_term); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1831 | |
| 1832 | if (fqdn_flags != 0) |
| 1833 | { |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1834 | len = strlen(hostname) + 3; |
| 1835 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1836 | if (fqdn_flags & 0x04) |
| 1837 | len += 2; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1838 | else if (null_term) |
| 1839 | len++; |
| 1840 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1841 | if (domain) |
| 1842 | len += strlen(domain) + 1; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1843 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1844 | if ((p = free_space(mess, end, OPTION_CLIENT_FQDN, len))) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1845 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1846 | *(p++) = fqdn_flags; |
| 1847 | *(p++) = 255; |
| 1848 | *(p++) = 255; |
| 1849 | |
| 1850 | if (fqdn_flags & 0x04) |
| 1851 | { |
| 1852 | p = do_rfc1035_name(p, hostname); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1853 | if (domain) |
| 1854 | p = do_rfc1035_name(p, domain); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1855 | *p++ = 0; |
| 1856 | } |
| 1857 | else |
| 1858 | { |
| 1859 | memcpy(p, hostname, strlen(hostname)); |
| 1860 | p += strlen(hostname); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1861 | if (domain) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1862 | { |
| 1863 | *(p++) = '.'; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1864 | memcpy(p, domain, strlen(domain)); |
| 1865 | p += strlen(domain); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1866 | } |
| 1867 | if (null_term) |
| 1868 | *(p++) = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1869 | } |
| 1870 | } |
| 1871 | } |
| 1872 | } |
| 1873 | |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1874 | for (opt = config_opts; opt; opt = opt->next) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1875 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1876 | int optno = opt->opt; |
| 1877 | |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1878 | /* was it asked for, or are we sending it anyway? */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1879 | if (!(opt->flags & DHOPT_FORCE) && !in_list(req_options, optno)) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1880 | continue; |
| 1881 | |
| 1882 | /* prohibit some used-internally options */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1883 | if (optno == OPTION_CLIENT_FQDN || |
| 1884 | optno == OPTION_MAXMESSAGE || |
| 1885 | optno == OPTION_OVERLOAD || |
| 1886 | optno == OPTION_PAD || |
| 1887 | optno == OPTION_END) |
| 1888 | continue; |
| 1889 | |
| 1890 | if (optno == OPTION_SNAME && done_server) |
| 1891 | continue; |
| 1892 | |
| 1893 | if (optno == OPTION_FILENAME && done_file) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1894 | continue; |
| 1895 | |
| 1896 | /* netids match and not encapsulated? */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1897 | if (opt != option_find2(netid, config_opts, optno)) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 1898 | continue; |
| 1899 | |
| 1900 | /* For the options we have default values on |
| 1901 | dhc-option=<optionno> means "don't include this option" |
| 1902 | not "include a zero-length option" */ |
| 1903 | if (opt->len == 0 && |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1904 | (optno == OPTION_NETMASK || |
| 1905 | optno == OPTION_BROADCAST || |
| 1906 | optno == OPTION_ROUTER || |
| 1907 | optno == OPTION_DNSSERVER || |
| 1908 | optno == OPTION_DOMAINNAME || |
| 1909 | optno == OPTION_HOSTNAME)) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 1910 | continue; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1911 | |
| 1912 | /* always force null-term for filename ans servername - buggy PXE again. */ |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1913 | len = do_opt(opt, NULL, context, |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1914 | (optno == OPTION_SNAME || optno == OPTION_FILENAME) ? 1 : null_term); |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1915 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1916 | if ((p = free_space(mess, end, optno, len))) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1917 | { |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1918 | do_opt(opt, p, context, |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1919 | (optno == OPTION_SNAME || optno == OPTION_FILENAME) ? 1 : null_term); |
| 1920 | |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1921 | /* If we send a vendor-id, revisit which vendor-ops we consider |
| 1922 | it appropriate to send. */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1923 | if (optno == OPTION_VENDOR_ID) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1924 | match_vendor_opts(p - 2, config_opts); |
| 1925 | } |
| 1926 | } |
| 1927 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1928 | /* prune vendor-encapsulated options based on netid, and look if we're forcing them to be sent */ |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1929 | for (opt = config_opts; opt; opt = opt->next) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1930 | if (opt->flags & DHOPT_ENCAP_MATCH) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1931 | { |
| 1932 | if (!match_netid(opt->netid, netid, 1) && !match_netid(opt->netid, netid, 0)) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1933 | opt->flags &= ~DHOPT_ENCAP_MATCH; |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1934 | else if (opt->flags & DHOPT_FORCE) |
| 1935 | force_encap = 1; |
| 1936 | } |
| 1937 | |
| 1938 | if (force_encap || in_list(req_options, OPTION_VENDOR_CLASS_OPT)) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1939 | do_encap_opts(OPTION_VENDOR_CLASS_OPT, mess, end, null_term); |
| 1940 | |
| 1941 | /* Now send options to be encapsulated in arbitrary options, |
| 1942 | eg dhcp-option=encap:172,17,....... |
| 1943 | The may be more that one "outer" to do, so group |
| 1944 | all the options which match each outer in turn. */ |
| 1945 | for (opt = config_opts; opt; opt = opt->next) |
| 1946 | if ((opt->flags & (DHOPT_ENCAPSULATE | DHOPT_ENCAP_DONE)) == DHOPT_ENCAPSULATE) |
| 1947 | { |
| 1948 | struct dhcp_opt *o; |
| 1949 | int found = 0; |
| 1950 | |
| 1951 | for (o = config_opts; o; o = o->next) |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1952 | { |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1953 | o->flags &= ~DHOPT_ENCAP_MATCH; |
| 1954 | if ((o->flags & DHOPT_ENCAPSULATE) && opt->u.encap == o->u.encap) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1955 | { |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1956 | o->flags |= DHOPT_ENCAP_DONE; |
| 1957 | if ((match_netid(o->netid, netid, 1) || match_netid(o->netid, netid, 0)) && |
| 1958 | (o->flags & DHOPT_FORCE || in_list(req_options, o->u.encap))) |
| 1959 | { |
| 1960 | o->flags |= DHOPT_ENCAP_MATCH; |
| 1961 | found = 1; |
| 1962 | } |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1963 | } |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1964 | } |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame^] | 1965 | |
| 1966 | if (found) |
| 1967 | do_encap_opts(opt->u.encap, mess, end, null_term); |
| 1968 | } |
| 1969 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1970 | /* move agent_id back down to the end of the packet */ |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 1971 | restore_agent_id(agent_id, mess, real_end); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1972 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1973 | /* restore BOOTP anti-overload hack */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1974 | if (!req_options || (daemon->options & OPT_NO_OVERRIDE)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 1975 | { |
| 1976 | mess->file[0] = f0; |
| 1977 | mess->sname[0] = s0; |
| 1978 | } |
| 1979 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1980 | |