Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2006 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 |
| 5 | the Free Software Foundation; version 2 dated June, 1991. |
| 6 | |
| 7 | This program is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU General Public License for more details. |
| 11 | */ |
| 12 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 13 | #include "dnsmasq.h" |
| 14 | |
| 15 | #define BOOTREQUEST 1 |
| 16 | #define BOOTREPLY 2 |
| 17 | #define DHCP_COOKIE 0x63825363 |
| 18 | |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 19 | /* The Linux in-kernel DHCP client silently ignores any packet |
| 20 | smaller than this. Sigh........... */ |
| 21 | #define MIN_PACKETSZ 300 |
| 22 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 23 | #define OPTION_PAD 0 |
| 24 | #define OPTION_NETMASK 1 |
| 25 | #define OPTION_ROUTER 3 |
| 26 | #define OPTION_DNSSERVER 6 |
| 27 | #define OPTION_HOSTNAME 12 |
| 28 | #define OPTION_DOMAINNAME 15 |
| 29 | #define OPTION_BROADCAST 28 |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 30 | #define OPTION_VENDOR_CLASS_OPT 43 |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 31 | #define OPTION_REQUESTED_IP 50 |
| 32 | #define OPTION_LEASE_TIME 51 |
| 33 | #define OPTION_OVERLOAD 52 |
| 34 | #define OPTION_MESSAGE_TYPE 53 |
| 35 | #define OPTION_SERVER_IDENTIFIER 54 |
| 36 | #define OPTION_REQUESTED_OPTIONS 55 |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 37 | #define OPTION_MESSAGE 56 |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 38 | #define OPTION_MAXMESSAGE 57 |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 39 | #define OPTION_T1 58 |
| 40 | #define OPTION_T2 59 |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 41 | #define OPTION_VENDOR_ID 60 |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 42 | #define OPTION_CLIENT_ID 61 |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 43 | #define OPTION_USER_CLASS 77 |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 44 | #define OPTION_CLIENT_FQDN 81 |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 45 | #define OPTION_AGENT_ID 82 |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 46 | #define OPTION_SUBNET_SELECT 118 |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 47 | #define OPTION_END 255 |
| 48 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 49 | #define SUBOPT_SUBNET_SELECT 5 /* RFC 3527 */ |
| 50 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 51 | #define DHCPDISCOVER 1 |
| 52 | #define DHCPOFFER 2 |
| 53 | #define DHCPREQUEST 3 |
| 54 | #define DHCPDECLINE 4 |
| 55 | #define DHCPACK 5 |
| 56 | #define DHCPNAK 6 |
| 57 | #define DHCPRELEASE 7 |
| 58 | #define DHCPINFORM 8 |
| 59 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 60 | #define have_config(config, mask) ((config) && ((config)->flags & (mask))) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 61 | #define option_len(opt) ((int)(((unsigned char *)(opt))[1])) |
| 62 | #define option_ptr(opt) ((void *)&(((unsigned char *)(opt))[2])) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 63 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 64 | static unsigned int calc_time(struct dhcp_context *context, struct dhcp_config *config, |
| 65 | struct dhcp_lease *lease, unsigned char *opt, time_t now); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 66 | static unsigned char *option_put(unsigned char *p, unsigned char *end, int opt, int len, unsigned int val); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 67 | static unsigned char *option_end(unsigned char *p, unsigned char *end, |
| 68 | unsigned char *agent_id, struct dhcp_packet *start); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 69 | static unsigned char *option_put_string(unsigned char *p, unsigned char *end, |
| 70 | int opt, char *string, int null_term); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 71 | static void bootp_option_put(struct dhcp_packet *mess, |
| 72 | struct dhcp_boot *boot_opts, struct dhcp_netid *netids); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 73 | static struct in_addr option_addr(unsigned char *opt); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 74 | static unsigned int option_uint(unsigned char *opt, int size); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame^] | 75 | static void log_packet(struct daemon *daemon, char *type, void *addr, |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 76 | struct dhcp_packet *mess, char *interface, char *string); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 77 | 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] | 78 | 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] | 79 | static unsigned char *do_req_options(struct dhcp_context *context, |
| 80 | unsigned char *p, unsigned char *end, |
| 81 | unsigned char *req_options, |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 82 | struct daemon *daemon, |
| 83 | char *hostname, |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 84 | struct dhcp_netid *netid, |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 85 | struct in_addr subnet_addr, |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 86 | unsigned char fqdn_flags, |
| 87 | int null_term); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 88 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 89 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 90 | size_t dhcp_reply(struct daemon *daemon, struct dhcp_context *context, char *iface_name, |
| 91 | size_t sz, time_t now, int unicast_dest) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 92 | { |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 93 | unsigned char *opt, *clid = NULL; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 94 | struct dhcp_lease *ltmp, *lease = NULL; |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 95 | struct dhcp_vendor *vendor; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 96 | struct dhcp_mac *mac; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 97 | struct dhcp_netid_list *id_list; |
| 98 | int clid_len = 0, ignore = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 99 | struct dhcp_packet *mess = daemon->dhcp_packet.iov_base; |
| 100 | unsigned char *p, *end = (unsigned char *)(mess + 1); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 101 | char *hostname = NULL, *offer_hostname = NULL, *client_hostname = NULL; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 102 | int hostname_auth = 0, borken_opt = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 103 | unsigned char *req_options = NULL; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 104 | char *message = NULL; |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 105 | unsigned int time; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 106 | struct dhcp_config *config; |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 107 | struct dhcp_netid *netid = NULL; |
Simon Kelley | aedef83 | 2006-01-22 14:02:31 +0000 | [diff] [blame] | 108 | struct in_addr subnet_addr, fallback; |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 109 | unsigned short fuzz = 0; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 110 | unsigned int mess_type = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 111 | unsigned char fqdn_flags = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 112 | unsigned char *agent_id = NULL; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 113 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 114 | subnet_addr.s_addr = 0; |
| 115 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 116 | if (mess->op != BOOTREQUEST || mess->hlen > DHCP_CHADDR_MAX) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 117 | return 0; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 118 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 119 | if (mess->htype == 0 && mess->hlen != 0) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 120 | return 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 121 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 122 | /* check for DHCP rather than BOOTP */ |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 123 | if ((opt = option_find(mess, sz, OPTION_MESSAGE_TYPE, 1))) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 124 | { |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 125 | mess_type = option_uint(opt, 1); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 126 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 127 | /* only insist on a cookie for DHCP. */ |
| 128 | if (*((u32 *)&mess->options) != htonl(DHCP_COOKIE)) |
| 129 | return 0; |
| 130 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 131 | /* two things to note here: expand_buf may move the packet, |
| 132 | so reassign mess from daemon->packet. Also, the size |
| 133 | sent includes the IP and UDP headers, hence the magic "-28" */ |
| 134 | if ((opt = option_find(mess, sz, OPTION_MAXMESSAGE, 2))) |
| 135 | { |
| 136 | size_t size = (size_t)option_uint(opt, 2) - 28; |
| 137 | |
| 138 | if (size > DHCP_PACKET_MAX) |
| 139 | size = DHCP_PACKET_MAX; |
| 140 | else if (size < sizeof(struct dhcp_packet)) |
| 141 | size = sizeof(struct dhcp_packet); |
| 142 | |
| 143 | if (expand_buf(&daemon->dhcp_packet, size)) |
| 144 | { |
| 145 | mess = daemon->dhcp_packet.iov_base; |
| 146 | end = ((unsigned char *)mess) + size; |
| 147 | } |
| 148 | } |
| 149 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 150 | /* Some buggy clients set ciaddr when they shouldn't, so clear that here since |
| 151 | it can affect the context-determination code. */ |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 152 | if ((option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ) || mess_type == DHCPDISCOVER)) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 153 | mess->ciaddr.s_addr = 0; |
| 154 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 155 | if ((opt = option_find(mess, sz, OPTION_AGENT_ID, 1))) |
| 156 | { |
| 157 | /* Any agent-id needs to be copied back out, verbatim, as the last option |
| 158 | in the packet. Here, we shift it to the very end of the buffer, if it doesn't |
| 159 | get overwritten, then it will be shuffled back at the end of processing. |
| 160 | Note that the incoming options must not be overwritten here, so there has to |
| 161 | be enough free space at the end of the packet to copy the option. */ |
| 162 | unsigned int total = option_len(opt) + 2; |
| 163 | unsigned char *last_opt = option_find(mess, sz, OPTION_END, 0); |
| 164 | if (last_opt && last_opt < end - total) |
| 165 | { |
| 166 | agent_id = end - total; |
| 167 | memcpy(agent_id, opt, total); |
| 168 | } |
| 169 | |
| 170 | /* look for RFC3527 Link selection sub-option */ |
| 171 | if ((opt = option_find1(option_ptr(opt), option_ptr(opt) + option_len(opt), SUBOPT_SUBNET_SELECT, INADDRSZ))) |
| 172 | subnet_addr = option_addr(opt); |
| 173 | } |
| 174 | |
| 175 | /* Check for RFC3011 subnet selector - only if RFC3527 one not present */ |
| 176 | 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] | 177 | subnet_addr = option_addr(opt); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 178 | |
| 179 | /* If there is no client identifier option, use the hardware address */ |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 180 | if ((opt = option_find(mess, sz, OPTION_CLIENT_ID, 1))) |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 181 | { |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 182 | clid_len = option_len(opt); |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 183 | clid = option_ptr(opt); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 184 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 185 | |
| 186 | /* do we have a lease in store? */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 187 | 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] | 188 | |
| 189 | /* If this request is missing a clid, but we've seen one before, |
| 190 | use it again for option matching etc. */ |
| 191 | if (lease && !clid && lease->clid) |
| 192 | { |
| 193 | clid_len = lease->clid_len; |
| 194 | clid = lease->clid; |
| 195 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 196 | } |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 197 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 198 | for (mac = daemon->dhcp_macs; mac; mac = mac->next) |
| 199 | if (mac->hwaddr_len == mess->hlen && |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 200 | (mac->hwaddr_type == mess->htype || mac->hwaddr_type == 0) && |
| 201 | memcmp_masked(mac->hwaddr, mess->chaddr, mess->hlen, mac->mask)) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 202 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 203 | mac->netid.next = netid; |
| 204 | netid = &mac->netid; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 205 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 206 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 207 | /* Determine network for this packet. Our caller will have already linked all the |
| 208 | contexts which match the addresses of the receiving interface but if the |
| 209 | machine has an address already, or came via a relay, or we have a subnet selector, |
| 210 | we search again. If we don't have have a giaddr or explicit subnet selector, |
| 211 | 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] | 212 | relay won't use the relay to renew. If matching a ciaddr fails but we have a context |
| 213 | 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] | 214 | if (mess->giaddr.s_addr || subnet_addr.s_addr || mess->ciaddr.s_addr) |
| 215 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 216 | struct dhcp_context *context_tmp, *context_new = NULL; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame^] | 217 | struct in_addr addr; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 218 | int force = 0; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 219 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 220 | if (subnet_addr.s_addr) |
| 221 | { |
| 222 | addr = subnet_addr; |
| 223 | force = 1; |
| 224 | } |
| 225 | else if (mess->giaddr.s_addr) |
| 226 | { |
| 227 | addr = mess->giaddr; |
| 228 | force = 1; |
| 229 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame^] | 230 | else |
| 231 | { |
| 232 | /* If ciaddr is in the hardware derived set of contexts, leave that unchanged */ |
| 233 | addr = mess->ciaddr; |
| 234 | for (context_tmp = context; context_tmp; context_tmp = context_tmp->current) |
| 235 | if (context_tmp->netmask.s_addr && |
| 236 | is_same_net(addr, context_tmp->start, context_tmp->netmask) && |
| 237 | is_same_net(addr, context_tmp->end, context_tmp->netmask)) |
| 238 | { |
| 239 | context_new = context; |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (!context_new) |
| 245 | for (context_tmp = daemon->dhcp; context_tmp; context_tmp = context_tmp->next) |
| 246 | if (context_tmp->netmask.s_addr && |
| 247 | is_same_net(addr, context_tmp->start, context_tmp->netmask) && |
| 248 | is_same_net(addr, context_tmp->end, context_tmp->netmask)) |
| 249 | { |
| 250 | context_tmp->current = context_new; |
| 251 | context_new = context_tmp; |
| 252 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 253 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 254 | if (context_new || force) |
| 255 | context = context_new; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame^] | 256 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 257 | } |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 258 | |
| 259 | if (!context) |
| 260 | { |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 261 | syslog(LOG_WARNING, _("no address range available for DHCP request %s %s"), |
| 262 | subnet_addr.s_addr ? _("with subnet selector") : _("via"), |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 263 | 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] | 264 | return 0; |
| 265 | } |
| 266 | |
Simon Kelley | aedef83 | 2006-01-22 14:02:31 +0000 | [diff] [blame] | 267 | /* keep _a_ local address available. */ |
| 268 | fallback = context->local; |
| 269 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 270 | mess->op = BOOTREPLY; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 271 | p = mess->options + sizeof(u32); /* skip cookie */ |
| 272 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 273 | config = find_config(daemon->dhcp_conf, context, clid, clid_len, |
| 274 | mess->chaddr, mess->hlen, mess->htype, NULL); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 275 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 276 | if (mess_type == 0) |
| 277 | { |
| 278 | /* BOOTP request */ |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 279 | struct dhcp_netid id; |
| 280 | char save = mess->file[128]; |
| 281 | struct in_addr *logaddr = NULL; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 282 | |
| 283 | /* must have a MAC addr for bootp */ |
| 284 | if (mess->htype == 0 || mess->hlen == 0) |
| 285 | return 0; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 286 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 287 | if (have_config(config, CONFIG_DISABLE)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 288 | message = _("disabled"); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 289 | |
| 290 | end = mess->options + 64; /* BOOTP vend area is only 64 bytes */ |
| 291 | |
| 292 | if (have_config(config, CONFIG_NAME)) |
| 293 | hostname = config->hostname; |
| 294 | |
| 295 | if (have_config(config, CONFIG_NETID)) |
| 296 | { |
| 297 | config->netid.next = netid; |
| 298 | netid = &config->netid; |
| 299 | } |
| 300 | |
| 301 | /* Match incoming filename field as a netid. */ |
| 302 | if (mess->file[0]) |
| 303 | { |
| 304 | mess->file[128] = 0; /* ensure zero term. */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 305 | id.net = (char *)mess->file; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 306 | id.next = netid; |
| 307 | netid = &id; |
| 308 | } |
| 309 | |
| 310 | 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] | 311 | if (match_netid(id_list->list, netid, 0)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 312 | message = _("disabled"); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 313 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 314 | if (!message) |
| 315 | { |
| 316 | if (have_config(config, CONFIG_ADDR)) |
| 317 | { |
| 318 | logaddr = &config->addr; |
| 319 | mess->yiaddr = config->addr; |
| 320 | if ((lease = lease_find_by_addr(config->addr)) && |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 321 | (lease->hwaddr_len != mess->hlen || |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 322 | lease->hwaddr_type != mess->htype || |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 323 | memcmp(lease->hwaddr, mess->chaddr, lease->hwaddr_len) != 0)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 324 | message = _("address in use"); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 325 | } |
| 326 | else if (!(daemon->options & OPT_BOOTP_DYNAMIC)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 327 | message = _("no address configured"); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 328 | else |
| 329 | { |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 330 | if (!(lease = lease_find_by_client(mess->chaddr, mess->hlen, mess->htype, NULL, 0)) || |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 331 | !address_available(context, lease->addr)) |
| 332 | { |
| 333 | if (lease) |
| 334 | { |
| 335 | /* lease exists, wrong network. */ |
| 336 | lease_prune(lease, now); |
| 337 | lease = NULL; |
| 338 | } |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 339 | if (!address_allocate(context, daemon, &mess->yiaddr, mess->chaddr, mess->hlen, netid, now)) |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 340 | message = _("no address available"); |
| 341 | } |
| 342 | else |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 343 | mess->yiaddr = lease->addr; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 344 | } |
| 345 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 346 | if (!message && |
| 347 | !lease && |
| 348 | (!(lease = lease_allocate(mess->yiaddr)))) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 349 | message = _("no leases left"); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 350 | |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 351 | if (!message && !(context = narrow_context(context, mess->yiaddr))) |
| 352 | message = _("wrong network"); |
| 353 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 354 | if (!message) |
| 355 | { |
| 356 | logaddr = &mess->yiaddr; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 357 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 358 | if (context->netid.net) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 359 | { |
| 360 | context->netid.next = netid; |
| 361 | netid = &context->netid; |
| 362 | } |
| 363 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 364 | lease_set_hwaddr(lease, mess->chaddr, NULL, mess->hlen, mess->htype, 0); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 365 | if (hostname) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 366 | lease_set_hostname(lease, hostname, daemon->domain_suffix, 1); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 367 | lease_set_expires(lease, 0xffffffff, now); /* infinite lease */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 368 | |
| 369 | p = do_req_options(context, p, end, NULL, daemon, |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 370 | hostname, netid, subnet_addr, fqdn_flags, 0); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 371 | /* must do this after do_req_options since it overwrites filename field. */ |
| 372 | mess->siaddr = context->local; |
| 373 | bootp_option_put(mess, daemon->boot_config, netid); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 374 | p = option_end(p, end, NULL, mess); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 378 | log_packet(daemon, NULL, logaddr, mess, iface_name, message); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 379 | mess->file[128] = save; |
| 380 | |
| 381 | if (message) |
| 382 | return 0; |
| 383 | else |
| 384 | return p - (unsigned char *)mess; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 385 | } |
| 386 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 387 | if ((opt = option_find(mess, sz, OPTION_CLIENT_FQDN, 4))) |
| 388 | { |
| 389 | /* http://tools.ietf.org/wg/dhc/draft-ietf-dhc-fqdn-option/draft-ietf-dhc-fqdn-option-10.txt */ |
| 390 | int len = option_len(opt); |
| 391 | char *pq = daemon->dhcp_buff; |
| 392 | unsigned char *pp, *op = option_ptr(opt); |
| 393 | |
| 394 | fqdn_flags = *op; |
| 395 | len -= 3; |
| 396 | op += 3; |
| 397 | pp = op; |
| 398 | |
| 399 | /* 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] | 400 | if (!(fqdn_flags & 0x01)) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 401 | fqdn_flags |= 0x02; |
| 402 | |
| 403 | fqdn_flags &= ~0x08; |
| 404 | fqdn_flags |= 0x01; |
| 405 | |
| 406 | if (fqdn_flags & 0x04) |
| 407 | while (*op != 0 && ((op + (*op) + 1) - pp) < len) |
| 408 | { |
| 409 | memcpy(pq, op+1, *op); |
| 410 | pq += *op; |
| 411 | op += (*op)+1; |
| 412 | *(pq++) = '.'; |
| 413 | } |
| 414 | else |
| 415 | { |
| 416 | memcpy(pq, op, len); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 417 | if (len > 0 && op[len-1] == 0) |
| 418 | borken_opt = 1; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 419 | pq += len + 1; |
| 420 | } |
| 421 | |
| 422 | if (pq != daemon->dhcp_buff) |
| 423 | pq--; |
| 424 | |
| 425 | *pq = 0; |
| 426 | |
| 427 | if (canonicalise(daemon->dhcp_buff)) |
| 428 | offer_hostname = client_hostname = daemon->dhcp_buff; |
| 429 | } |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 430 | else if ((opt = option_find(mess, sz, OPTION_HOSTNAME, 1))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 431 | { |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 432 | int len = option_len(opt); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 433 | memcpy(daemon->dhcp_buff, option_ptr(opt), len); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 434 | /* Microsoft clients are broken, and need zero-terminated strings |
| 435 | in options. We detect this state here, and do the same in |
| 436 | any options we send */ |
| 437 | if (len > 0 && daemon->dhcp_buff[len-1] == 0) |
| 438 | borken_opt = 1; |
| 439 | else |
| 440 | daemon->dhcp_buff[len] = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 441 | if (canonicalise(daemon->dhcp_buff)) |
| 442 | client_hostname = daemon->dhcp_buff; |
| 443 | } |
| 444 | |
| 445 | if (have_config(config, CONFIG_NAME)) |
| 446 | { |
| 447 | hostname = config->hostname; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 448 | hostname_auth = 1; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 449 | /* be careful not to send an OFFER with a hostname not |
| 450 | matching the DISCOVER. */ |
| 451 | if (fqdn_flags != 0 || !client_hostname || hostname_isequal(hostname, client_hostname)) |
| 452 | offer_hostname = hostname; |
| 453 | } |
| 454 | else if (client_hostname && (hostname = strip_hostname(daemon, client_hostname)) && !config) |
| 455 | { |
| 456 | /* Search again now we have a hostname. |
| 457 | Only accept configs without CLID and HWADDR here, (they won't match) |
| 458 | to avoid impersonation by name. */ |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 459 | struct dhcp_config *new = find_config(daemon->dhcp_conf, context, NULL, 0, |
| 460 | mess->chaddr, mess->hlen, |
| 461 | mess->htype, hostname); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 462 | if (!have_config(new, CONFIG_CLID) && !have_config(new, CONFIG_HWADDR)) |
| 463 | config = new; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 464 | } |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 465 | |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 466 | if (have_config(config, CONFIG_NETID)) |
| 467 | { |
| 468 | config->netid.next = netid; |
| 469 | netid = &config->netid; |
| 470 | } |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 471 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 472 | /* user-class options are, according to RFC3004, supposed to contain |
| 473 | a set of counted strings. Here we check that this is so (by seeing |
| 474 | if the counts are consistent with the overall option length) and if |
| 475 | so zero the counts so that we don't get spurious matches between |
| 476 | the vendor string and the counts. If the lengths don't add up, we |
| 477 | assume that the option is a single string and non RFC3004 compliant |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame^] | 478 | and just do the substring match. dhclient provides these broken options. |
| 479 | The code, later, which sends user-class data to the lease-change script |
| 480 | relies on the transformation done here. |
| 481 | */ |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 482 | |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 483 | if ((opt = option_find(mess, sz, OPTION_USER_CLASS, 1))) |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 484 | { |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 485 | unsigned char *ucp = option_ptr(opt); |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 486 | int tmp, j; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 487 | for (j = 0; j < option_len(opt); j += ucp[j] + 1); |
| 488 | if (j == option_len(opt)) |
| 489 | for (j = 0; j < option_len(opt); j = tmp) |
| 490 | { |
| 491 | tmp = j + ucp[j] + 1; |
| 492 | ucp[j] = 0; |
| 493 | } |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 494 | } |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 495 | |
| 496 | for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next) |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 497 | if ((opt = option_find(mess, sz, vendor->is_vendor ? OPTION_VENDOR_ID : OPTION_USER_CLASS, 1))) |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 498 | { |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 499 | int i; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 500 | for (i = 0; i <= (option_len(opt) - vendor->len); i++) |
| 501 | if (memcmp(vendor->data, option_ptr(opt)+i, vendor->len) == 0) |
| 502 | { |
| 503 | vendor->netid.next = netid; |
| 504 | netid = &vendor->netid; |
| 505 | break; |
| 506 | } |
| 507 | } |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 508 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 509 | /* if all the netids in the ignore list are present, ignore this client */ |
| 510 | 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] | 511 | if (match_netid(id_list->list, netid, 0)) |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 512 | ignore = 1; |
| 513 | |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 514 | /* Can have setting to ignore the client ID for a particular MAC address or hostname */ |
| 515 | if (have_config(config, CONFIG_NOCLID)) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 516 | clid = NULL; |
| 517 | |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 518 | if ((opt = option_find(mess, sz, OPTION_REQUESTED_OPTIONS, 0))) |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 519 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 520 | req_options = (unsigned char *)daemon->dhcp_buff2; |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 521 | memcpy(req_options, option_ptr(opt), option_len(opt)); |
| 522 | req_options[option_len(opt)] = OPTION_END; |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 523 | } |
| 524 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 525 | switch (mess_type) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 526 | { |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 527 | case DHCPDECLINE: |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 528 | if (!(opt = option_find(mess, sz, OPTION_SERVER_IDENTIFIER, INADDRSZ)) || |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 529 | (context->local.s_addr != option_addr(opt).s_addr)) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 530 | return 0; |
| 531 | |
| 532 | /* sanitise any message. Paranoid? Moi? */ |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 533 | if ((opt = option_find(mess, sz, OPTION_MESSAGE, 1))) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 534 | { |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 535 | char *p = option_ptr(opt), *q = daemon->dhcp_buff; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 536 | int i; |
| 537 | |
| 538 | for (i = option_len(opt); i > 0; i--) |
| 539 | { |
| 540 | char c = *p++; |
| 541 | if (isprint(c)) |
| 542 | *q++ = c; |
| 543 | } |
| 544 | *q++ = 0; /* add terminator */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 545 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 546 | |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 547 | if (!(opt = option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ))) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 548 | return 0; |
| 549 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 550 | log_packet(daemon, "DECLINE", option_ptr(opt), mess, iface_name, daemon->dhcp_buff); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 551 | |
| 552 | if (lease && lease->addr.s_addr == option_addr(opt).s_addr) |
| 553 | lease_prune(lease, now); |
| 554 | |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 555 | if (have_config(config, CONFIG_ADDR) && |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 556 | config->addr.s_addr == option_addr(opt).s_addr) |
| 557 | { |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 558 | prettyprint_time(daemon->dhcp_buff, DECLINE_BACKOFF); |
| 559 | syslog(LOG_WARNING, _("disabling DHCP static address %s for %s"), |
| 560 | inet_ntoa(config->addr), daemon->dhcp_buff); |
| 561 | config->flags |= CONFIG_DECLINED; |
| 562 | config->decline_time = now; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 563 | } |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 564 | else |
| 565 | /* make sure this host gets a different address next time. */ |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 566 | for (; context; context = context->current) |
| 567 | context->addr_epoch++; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 568 | |
| 569 | return 0; |
| 570 | |
| 571 | case DHCPRELEASE: |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame^] | 572 | if (!(context = narrow_context(context, mess->ciaddr)) || |
| 573 | !(opt = option_find(mess, sz, OPTION_SERVER_IDENTIFIER, INADDRSZ)) || |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 574 | (context->local.s_addr != option_addr(opt).s_addr)) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 575 | return 0; |
| 576 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 577 | if (lease && lease->addr.s_addr == mess->ciaddr.s_addr) |
| 578 | lease_prune(lease, now); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 579 | else |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 580 | message = _("unknown lease"); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 581 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 582 | log_packet(daemon, "RELEASE", &mess->ciaddr, mess, iface_name, message); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 583 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 584 | return 0; |
| 585 | |
| 586 | case DHCPDISCOVER: |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 587 | if (ignore || have_config(config, CONFIG_DISABLE)) |
| 588 | { |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 589 | message = _("ignored"); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 590 | opt = NULL; |
| 591 | } |
| 592 | else |
| 593 | { |
| 594 | struct in_addr addr, conf; |
| 595 | |
| 596 | if ((opt = option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ))) |
| 597 | addr = option_addr(opt); |
| 598 | |
| 599 | conf.s_addr = 0; |
| 600 | if (have_config(config, CONFIG_ADDR)) |
| 601 | { |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 602 | char *addrs = inet_ntoa(config->addr); |
| 603 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 604 | if ((ltmp = lease_find_by_addr(config->addr)) && ltmp != lease) |
| 605 | syslog(LOG_WARNING, _("not using configured address %s because it is leased to %s"), |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 606 | addrs, print_mac(daemon, ltmp->hwaddr, ltmp->hwaddr_len)); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 607 | else |
| 608 | { |
| 609 | struct dhcp_context *tmp; |
| 610 | for (tmp = context; tmp; tmp = tmp->current) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 611 | if (context->router.s_addr == config->addr.s_addr) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 612 | break; |
| 613 | if (tmp) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 614 | syslog(LOG_WARNING, _("not using configured address %s because it is in use by the server or relay"), addrs); |
| 615 | else if (have_config(config, CONFIG_DECLINED) && |
| 616 | difftime(now, config->decline_time) < (float)DECLINE_BACKOFF) |
| 617 | 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] | 618 | else |
| 619 | conf = config->addr; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | if (conf.s_addr) |
| 624 | mess->yiaddr = conf; |
| 625 | else if (lease && address_available(context, lease->addr)) |
| 626 | mess->yiaddr = lease->addr; |
| 627 | else if (opt && address_available(context, addr) && !lease_find_by_addr(addr) && |
| 628 | !config_find_by_address(daemon->dhcp_conf, addr)) |
| 629 | mess->yiaddr = addr; |
| 630 | else if (!address_allocate(context, daemon, &mess->yiaddr, mess->chaddr, mess->hlen, netid, now)) |
| 631 | message = _("no address available"); |
| 632 | } |
| 633 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame^] | 634 | log_packet(daemon, "DISCOVER", opt ? option_ptr(opt) : NULL, mess, iface_name, message); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 635 | |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 636 | if (message || !(context = narrow_context(context, mess->yiaddr))) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 637 | return 0; |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 638 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 639 | if (context->netid.net) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 640 | { |
| 641 | context->netid.next = netid; |
| 642 | netid = &context->netid; |
| 643 | } |
| 644 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 645 | time = calc_time(context, config, lease, option_find(mess, sz, OPTION_LEASE_TIME, 4), now); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 646 | mess->siaddr = context->local; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 647 | bootp_option_put(mess, daemon->boot_config, netid); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 648 | p = option_put(p, end, OPTION_MESSAGE_TYPE, 1, DHCPOFFER); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 649 | p = option_put(p, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, ntohl(context->local.s_addr)); |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 650 | p = option_put(p, end, OPTION_LEASE_TIME, 4, time); |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 651 | /* 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] | 652 | if (time != 0xffffffff) |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 653 | { |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 654 | p = option_put(p, end, OPTION_T1, 4, (time/2)); |
| 655 | p = option_put(p, end, OPTION_T2, 4, (time*7)/8); |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 656 | } |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 657 | p = do_req_options(context, p, end, req_options, daemon, |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 658 | offer_hostname, netid, subnet_addr, fqdn_flags, borken_opt); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 659 | p = option_end(p, end, agent_id, mess); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 660 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 661 | log_packet(daemon, "OFFER" , &mess->yiaddr, mess, iface_name, NULL); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 662 | return p - (unsigned char *)mess; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 663 | |
| 664 | case DHCPREQUEST: |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 665 | if (ignore || have_config(config, CONFIG_DISABLE)) |
| 666 | return 0; |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 667 | if ((opt = option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 668 | { |
| 669 | /* SELECTING or INIT_REBOOT */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 670 | mess->yiaddr = option_addr(opt); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 671 | |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 672 | if ((opt = option_find(mess, sz, OPTION_SERVER_IDENTIFIER, INADDRSZ))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 673 | { |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 674 | /* SELECTING */ |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 675 | for (; context; context = context->current) |
| 676 | if (context->local.s_addr == option_addr(opt).s_addr) |
| 677 | break; |
| 678 | |
| 679 | if (!context) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 680 | return 0; |
| 681 | |
| 682 | /* If a lease exists for this host and another address, squash it. */ |
| 683 | if (lease && lease->addr.s_addr != mess->yiaddr.s_addr) |
| 684 | { |
| 685 | lease_prune(lease, now); |
| 686 | lease = NULL; |
| 687 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 688 | } |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 689 | else |
| 690 | { |
| 691 | /* INIT-REBOOT */ |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 692 | if (!lease && !(daemon->options & OPT_AUTHORITATIVE)) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 693 | return 0; |
| 694 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 695 | if (lease && lease->addr.s_addr != mess->yiaddr.s_addr) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 696 | message = _("wrong address"); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 697 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 698 | } |
| 699 | else |
| 700 | { |
| 701 | /* RENEWING or REBINDING */ |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 702 | /* Check existing lease for this address. |
| 703 | We allow it to be missing if dhcp-authoritative mode |
| 704 | as long as we can allocate the lease now - checked below. |
| 705 | This makes for a smooth recovery from a lost lease DB */ |
| 706 | if ((lease && mess->ciaddr.s_addr != lease->addr.s_addr) || |
| 707 | (!lease && !(daemon->options & OPT_AUTHORITATIVE))) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 708 | { |
| 709 | message = _("lease not found"); |
| 710 | /* ensure we broadcast NAK */ |
| 711 | unicast_dest = 0; |
| 712 | } |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 713 | /* desynchronise renewals */ |
| 714 | fuzz = rand16(); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 715 | mess->yiaddr = mess->ciaddr; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 718 | log_packet(daemon, "REQUEST", &mess->yiaddr, mess, iface_name, NULL); |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 719 | |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 720 | if (!message) |
| 721 | { |
| 722 | struct dhcp_config *addr_config; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 723 | struct dhcp_context *tmp = NULL; |
| 724 | |
| 725 | if (have_config(config, CONFIG_ADDR)) |
| 726 | for (tmp = context; tmp; tmp = tmp->current) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 727 | if (context->router.s_addr == config->addr.s_addr) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 728 | break; |
Simon Kelley | aedef83 | 2006-01-22 14:02:31 +0000 | [diff] [blame] | 729 | |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 730 | if (!(context = narrow_context(context, mess->yiaddr))) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 731 | { |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 732 | /* 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] | 733 | message = _("wrong network"); |
| 734 | /* ensure we broadcast NAK */ |
| 735 | unicast_dest = 0; |
| 736 | } |
| 737 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 738 | /* Check for renewal of a lease which is outside the allowed range. */ |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 739 | else if (!address_available(context, mess->yiaddr) && |
| 740 | (!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] | 741 | message = _("address not available"); |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 742 | |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 743 | /* Check if a new static address has been configured. Be very sure that |
| 744 | when the client does DISCOVER, it will get the static address, otherwise |
| 745 | an endless protocol loop will ensue. */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 746 | else if (!tmp && |
| 747 | have_config(config, CONFIG_ADDR) && |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 748 | (!have_config(config, CONFIG_DECLINED) || |
| 749 | difftime(now, config->decline_time) > (float)DECLINE_BACKOFF) && |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 750 | config->addr.s_addr != mess->yiaddr.s_addr && |
| 751 | (!(ltmp = lease_find_by_addr(config->addr)) || ltmp == lease)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 752 | message = _("static lease available"); |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 753 | |
| 754 | /* 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] | 755 | 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] | 756 | message = _("address reserved"); |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 757 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 758 | else if ((ltmp = lease_find_by_addr(mess->yiaddr)) && ltmp != lease) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 759 | message = _("address in use"); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 760 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 761 | else if (!clid && mess->hlen == 0) |
| 762 | message = _("no unique-id"); |
| 763 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame^] | 764 | else if (!lease) |
| 765 | { |
| 766 | if (!(lease = lease_allocate(mess->yiaddr))) |
| 767 | message = _("no leases left"); |
| 768 | else |
| 769 | { |
| 770 | /* copy user-class and vendor class into new lease, for the script */ |
| 771 | if ((opt = option_find(mess, sz, OPTION_USER_CLASS, 1))) |
| 772 | { |
| 773 | int len = option_len(opt); |
| 774 | unsigned char *ucp = option_ptr(opt); |
| 775 | /* If the user-class option started as counted strings, the first byte will be zero. */ |
| 776 | if (len != 0 && ucp[0] == 0) |
| 777 | ucp++, len--; |
| 778 | if ((lease->userclass = malloc(len+1))) |
| 779 | { |
| 780 | memcpy(lease->userclass, ucp, len); |
| 781 | lease->userclass[len] = 0; |
| 782 | lease->userclass_len = len+1; |
| 783 | } |
| 784 | } |
| 785 | if ((opt = option_find(mess, sz, OPTION_VENDOR_ID, 1))) |
| 786 | { |
| 787 | int len = option_len(opt); |
| 788 | unsigned char *ucp = option_ptr(opt); |
| 789 | if ((lease->vendorclass = malloc(len+1))) |
| 790 | { |
| 791 | memcpy(lease->vendorclass, ucp, len); |
| 792 | lease->vendorclass[len] = 0; |
| 793 | lease->vendorclass_len = len+1; |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 798 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame^] | 799 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 800 | if (message) |
| 801 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 802 | log_packet(daemon, "NAK", &mess->yiaddr, mess, iface_name, message); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 803 | |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 804 | mess->siaddr.s_addr = mess->yiaddr.s_addr = 0; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 805 | bootp_option_put(mess, NULL, NULL); |
| 806 | p = option_put(p, end, OPTION_MESSAGE_TYPE, 1, DHCPNAK); |
Simon Kelley | aedef83 | 2006-01-22 14:02:31 +0000 | [diff] [blame] | 807 | p = option_put(p, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, |
| 808 | ntohl(context ? context->local.s_addr : fallback.s_addr)); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 809 | p = option_put_string(p, end, OPTION_MESSAGE, message, borken_opt); |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 810 | /* This fixes a problem with the DHCP spec, broadcasting a NAK to a host on |
| 811 | a distant subnet which unicast a REQ to us won't work. */ |
| 812 | if (!unicast_dest || mess->giaddr.s_addr != 0 || |
| 813 | mess->ciaddr.s_addr == 0 || is_same_net(context->local, mess->ciaddr, context->netmask)) |
| 814 | { |
| 815 | mess->flags |= htons(0x8000); /* broadcast */ |
| 816 | mess->ciaddr.s_addr = 0; |
| 817 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 818 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 819 | else |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 820 | { |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 821 | if (!hostname_auth && (client_hostname = host_from_dns(daemon, mess->yiaddr))) |
| 822 | { |
| 823 | hostname = client_hostname; |
| 824 | hostname_auth = 1; |
| 825 | } |
| 826 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 827 | log_packet(daemon, "ACK", &mess->yiaddr, mess, iface_name, hostname); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 828 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 829 | if (context->netid.net) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 830 | { |
| 831 | context->netid.next = netid; |
| 832 | netid = &context->netid; |
| 833 | } |
| 834 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 835 | time = calc_time(context, config, NULL, option_find(mess, sz, OPTION_LEASE_TIME, 4), now); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 836 | lease_set_hwaddr(lease, mess->chaddr, clid, mess->hlen, mess->htype, clid_len); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 837 | if (hostname) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 838 | lease_set_hostname(lease, hostname, daemon->domain_suffix, hostname_auth); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 839 | lease_set_expires(lease, time, now); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 840 | |
| 841 | mess->siaddr = context->local; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 842 | bootp_option_put(mess, daemon->boot_config, netid); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 843 | p = option_put(p, end, OPTION_MESSAGE_TYPE, 1, DHCPACK); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 844 | p = option_put(p, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, ntohl(context->local.s_addr)); |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 845 | p = option_put(p, end, OPTION_LEASE_TIME, 4, time); |
| 846 | if (time != 0xffffffff) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 847 | { |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 848 | while (fuzz > (time/16)) |
| 849 | fuzz = fuzz/2; |
| 850 | p = option_put(p, end, OPTION_T1, 4, (time/2) - fuzz); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 851 | p = option_put(p, end, OPTION_T2, 4, ((time/8)*7) - fuzz); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 852 | } |
| 853 | p = do_req_options(context, p, end, req_options, daemon, |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 854 | hostname, netid, subnet_addr, fqdn_flags, borken_opt); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 855 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 856 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 857 | p = option_end(p, end, agent_id, mess); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 858 | return p - (unsigned char *)mess; |
| 859 | |
| 860 | case DHCPINFORM: |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 861 | if (ignore || have_config(config, CONFIG_DISABLE)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 862 | message = _("ignored"); |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 863 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 864 | log_packet(daemon, "INFORM", &mess->ciaddr, mess, iface_name, message); |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 865 | |
Simon Kelley | e17fb62 | 2006-01-14 20:33:46 +0000 | [diff] [blame] | 866 | if (message || mess->ciaddr.s_addr == 0 || |
| 867 | !(context = narrow_context(context, mess->ciaddr))) |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 868 | return 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 869 | |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 870 | if (context->netid.net) |
| 871 | { |
| 872 | context->netid.next = netid; |
| 873 | netid = &context->netid; |
| 874 | } |
| 875 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 876 | mess->siaddr = context->local; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 877 | bootp_option_put(mess, daemon->boot_config, netid); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 878 | p = option_put(p, end, OPTION_MESSAGE_TYPE, 1, DHCPACK); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 879 | p = option_put(p, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, ntohl(context->local.s_addr)); |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 880 | if (!hostname) |
| 881 | hostname = host_from_dns(daemon, mess->yiaddr); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 882 | p = do_req_options(context, p, end, req_options, daemon, |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 883 | hostname, netid, subnet_addr, fqdn_flags, borken_opt); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 884 | p = option_end(p, end, agent_id, mess); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 885 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 886 | log_packet(daemon, "ACK", &mess->ciaddr, mess, iface_name, hostname); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 887 | return p - (unsigned char *)mess; |
| 888 | } |
| 889 | |
| 890 | return 0; |
| 891 | } |
| 892 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 893 | static unsigned int calc_time(struct dhcp_context *context, struct dhcp_config *config, |
| 894 | struct dhcp_lease *lease, unsigned char *opt, time_t now) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 895 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 896 | 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] | 897 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 898 | if (opt) |
| 899 | { |
| 900 | unsigned int req_time = option_uint(opt, 4); |
| 901 | if (req_time < 120 ) |
| 902 | req_time = 120; /* sanity */ |
| 903 | if (time == 0xffffffff || (req_time != 0xffffffff && req_time < time)) |
| 904 | time = req_time; |
| 905 | } |
| 906 | else if (lease && lease->expires != 0 && difftime(lease->expires, now) > 0.0) |
| 907 | { |
| 908 | unsigned int lease_time = (unsigned int)difftime(lease->expires, now); |
| 909 | |
| 910 | /* put a floor on lease-remaining time. */ |
| 911 | if (lease_time < 360 ) |
| 912 | lease_time = 360; |
| 913 | |
| 914 | if (time > lease_time) |
| 915 | time = lease_time; |
| 916 | } |
| 917 | |
| 918 | return time; |
| 919 | } |
| 920 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame^] | 921 | static void log_packet(struct daemon *daemon, char *type, void *addr, |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 922 | struct dhcp_packet *mess, char *interface, char *string) |
| 923 | { |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame^] | 924 | struct in_addr a; |
| 925 | |
| 926 | /* addr may be misaligned */ |
| 927 | if (addr) |
| 928 | memcpy(&a, addr, sizeof(a)); |
| 929 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 930 | syslog(LOG_INFO, "%s%s(%s) %s%s%s %s", |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 931 | type ? "DHCP" : "BOOTP", |
| 932 | type ? type : "", |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 933 | interface, |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame^] | 934 | addr ? inet_ntoa(a) : "", |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 935 | addr ? " " : "", |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 936 | print_mac(daemon, mess->chaddr, mess->hlen), |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 937 | string ? string : ""); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 940 | static struct in_addr option_addr(unsigned char *opt) |
| 941 | { |
| 942 | /* this worries about unaligned data in the option. */ |
| 943 | /* struct in_addr is network byte order */ |
| 944 | struct in_addr ret; |
| 945 | |
| 946 | memcpy(&ret, option_ptr(opt), INADDRSZ); |
| 947 | |
| 948 | return ret; |
| 949 | } |
| 950 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 951 | static unsigned int option_uint(unsigned char *opt, int size) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 952 | { |
| 953 | /* this worries about unaligned data and byte order */ |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 954 | unsigned int ret = 0; |
| 955 | int i; |
| 956 | unsigned char *p = option_ptr(opt); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 957 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 958 | for (i = 0; i < size; i++) |
| 959 | ret = (ret << 8) | *p++; |
| 960 | |
| 961 | return ret; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 964 | static void bootp_option_put(struct dhcp_packet *mess, |
| 965 | struct dhcp_boot *boot_opts, struct dhcp_netid *netids) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 966 | { |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 967 | struct dhcp_boot *tmp; |
| 968 | |
| 969 | for (tmp = boot_opts; tmp; tmp = tmp->next) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 970 | if (match_netid(tmp->netid, netids, 0)) |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 971 | break; |
| 972 | if (!tmp) |
| 973 | /* No match, look for one without a netid */ |
| 974 | for (tmp = boot_opts; tmp; tmp = tmp->next) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 975 | if (match_netid(tmp->netid, netids, 1)) |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 976 | break; |
| 977 | |
| 978 | /* Do this _after_ the matching above, since in |
| 979 | BOOTP mode, one if the things we match is the filename. */ |
| 980 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 981 | memset(mess->sname, 0, sizeof(mess->sname)); |
| 982 | memset(mess->file, 0, sizeof(mess->file)); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 983 | |
| 984 | if (tmp) |
| 985 | { |
| 986 | if (tmp->sname) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 987 | strncpy((char *)mess->sname, tmp->sname, sizeof(mess->sname)-1); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 988 | if (tmp->file) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 989 | strncpy((char *)mess->file, tmp->file, sizeof(mess->file)-1); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 990 | if (tmp->next_server.s_addr) |
| 991 | mess->siaddr = tmp->next_server; |
| 992 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 995 | static int check_space(unsigned char *p, unsigned char *end, int len, int opt) |
| 996 | { |
| 997 | /* always keep one octet space for the END option. */ |
| 998 | if (p + len + 3 >= end) |
| 999 | { |
| 1000 | syslog(LOG_WARNING, _("cannot send DHCP option %d: no space left in packet"), opt); |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | return 1; |
| 1005 | } |
| 1006 | |
| 1007 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1008 | static unsigned char *option_put(unsigned char *p, unsigned char *end, int opt, int len, unsigned int val) |
| 1009 | { |
| 1010 | int i; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1011 | |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1012 | if (check_space(p, end, len, opt)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1013 | { |
| 1014 | *(p++) = opt; |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 1015 | *(p++) = len; |
| 1016 | |
| 1017 | for (i = 0; i < len; i++) |
| 1018 | *(p++) = val >> (8 * (len - (i + 1))); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1019 | } |
| 1020 | return p; |
| 1021 | } |
| 1022 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1023 | static unsigned char *option_end(unsigned char *p, unsigned char *end, |
| 1024 | unsigned char *agent_id, struct dhcp_packet *start) |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 1025 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1026 | /* shuffle agent-id back down if we have room for it */ |
| 1027 | if (agent_id && p < agent_id) |
| 1028 | { |
| 1029 | memmove(p, agent_id, end - agent_id); |
| 1030 | p += end - agent_id; |
| 1031 | } |
| 1032 | |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 1033 | *(p++) = OPTION_END; |
| 1034 | while ((p < end) && (p - ((unsigned char *)start) < MIN_PACKETSZ)) |
| 1035 | *p++ = 0; |
| 1036 | |
| 1037 | return p; |
| 1038 | } |
| 1039 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1040 | static unsigned char *option_put_string(unsigned char *p, unsigned char *end, int opt, |
| 1041 | char *string, int null_term) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1042 | { |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1043 | size_t len = strlen(string); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1044 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1045 | if (null_term && len != 255) |
| 1046 | len++; |
| 1047 | |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1048 | if (check_space(p, end, len, opt)) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1049 | { |
| 1050 | *(p++) = opt; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1051 | *(p++) = len; |
| 1052 | memcpy(p, string, len); |
| 1053 | p += len; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1054 | } |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1055 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1056 | return p; |
| 1057 | } |
| 1058 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1059 | 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] | 1060 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1061 | while (*p != OPTION_END) |
| 1062 | { |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 1063 | if (p >= end) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1064 | return NULL; /* malformed packet */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1065 | else if (*p == OPTION_PAD) |
| 1066 | p++; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1067 | else |
| 1068 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1069 | int opt_len; |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 1070 | if (p >= end - 2) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1071 | return NULL; /* malformed packet */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1072 | opt_len = option_len(p); |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 1073 | if (p >= end - (2 + opt_len)) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1074 | return NULL; /* malformed packet */ |
| 1075 | if (*p == opt && opt_len >= minsize) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1076 | return p; |
| 1077 | p += opt_len + 2; |
| 1078 | } |
| 1079 | } |
| 1080 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1081 | return opt == OPTION_END ? p : NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1084 | 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] | 1085 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1086 | unsigned char *ret, *opt; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1087 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1088 | /* skip over DHCP cookie; */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1089 | if ((ret = option_find1(&mess->options[0] + sizeof(u32), ((unsigned char *)mess) + size, opt_type, minsize))) |
| 1090 | return ret; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1091 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1092 | /* look for overload option. */ |
| 1093 | if (!(opt = option_find1(&mess->options[0] + sizeof(u32), ((unsigned char *)mess) + size, OPTION_OVERLOAD, 1))) |
| 1094 | return NULL; |
Simon Kelley | bb01cb9 | 2004-12-13 20:56:23 +0000 | [diff] [blame] | 1095 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1096 | /* Can we look in filename area ? */ |
| 1097 | if ((option_uint(opt, 1) & 1) && |
| 1098 | (ret = option_find1(&mess->file[0], &mess->file[128], opt_type, minsize))) |
| 1099 | return ret; |
| 1100 | |
| 1101 | /* finally try sname area */ |
| 1102 | if ((option_uint(opt, 1) & 2) && |
| 1103 | (ret = option_find1(&mess->sname[0], &mess->sname[64], opt_type, minsize))) |
| 1104 | return ret; |
| 1105 | |
| 1106 | return NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | static int in_list(unsigned char *list, int opt) |
| 1110 | { |
| 1111 | int i; |
| 1112 | |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1113 | /* If no requested options, send everything, not nothing. */ |
| 1114 | if (!list) |
| 1115 | return 1; |
| 1116 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1117 | for (i = 0; list[i] != OPTION_END; i++) |
| 1118 | if (opt == list[i]) |
| 1119 | return 1; |
| 1120 | |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 1124 | 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] | 1125 | { |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1126 | struct dhcp_opt *tmp; |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1127 | for (tmp = opts; tmp; tmp = tmp->next) |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 1128 | if (tmp->opt == opt) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1129 | if (match_netid(tmp->netid, netid, 1) || match_netid(tmp->netid, netid, 0)) |
| 1130 | return tmp; |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 1131 | |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1132 | return netid ? option_find2(NULL, opts, opt) : NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1135 | static unsigned char *do_opt(struct dhcp_opt *opt, unsigned char *p, unsigned char *end, |
| 1136 | struct in_addr local, int null_term) |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1137 | { |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1138 | int len = opt->len; |
| 1139 | |
| 1140 | if ((opt->flags & DHOPT_STRING) && null_term && len != 255) |
| 1141 | len++; |
| 1142 | |
| 1143 | if (!check_space(p, end, len, opt->opt)) |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1144 | return p; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1145 | |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1146 | *(p++) = opt->opt; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1147 | *(p++) = len; |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1148 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1149 | if (len == 0) |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1150 | return p; |
| 1151 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1152 | if (opt->flags & DHOPT_ADDR) |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1153 | { |
| 1154 | int j; |
| 1155 | struct in_addr *a = (struct in_addr *)opt->val; |
| 1156 | for (j = 0; j < opt->len; j+=INADDRSZ, a++) |
| 1157 | { |
| 1158 | /* zero means "self" (but not in vendorclass options.) */ |
| 1159 | if (a->s_addr == 0) |
| 1160 | memcpy(p, &local, INADDRSZ); |
| 1161 | else |
| 1162 | memcpy(p, a, INADDRSZ); |
| 1163 | p += INADDRSZ; |
| 1164 | } |
| 1165 | } |
| 1166 | else |
| 1167 | { |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1168 | memcpy(p, opt->val, len); |
| 1169 | p += len; |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | return p; |
| 1173 | } |
| 1174 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1175 | static unsigned char *do_req_options(struct dhcp_context *context, |
| 1176 | unsigned char *p, unsigned char *end, |
| 1177 | unsigned char *req_options, |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1178 | struct daemon *daemon, |
| 1179 | char *hostname, |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1180 | struct dhcp_netid *netid, |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1181 | struct in_addr subnet_addr, |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1182 | unsigned char fqdn_flags, |
| 1183 | int null_term) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1184 | { |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1185 | struct dhcp_opt *opt, *config_opts = daemon->dhcp_opts; |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1186 | char *vendor_class = NULL; |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 1187 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1188 | /* rfc3011 says this doesn't need to be in the requested options list. */ |
| 1189 | if (subnet_addr.s_addr) |
| 1190 | p = option_put(p, end, OPTION_SUBNET_SELECT, INADDRSZ, ntohl(subnet_addr.s_addr)); |
| 1191 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1192 | if (in_list(req_options, OPTION_NETMASK) && |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 1193 | !option_find2(netid, config_opts, OPTION_NETMASK)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1194 | p = option_put(p, end, OPTION_NETMASK, INADDRSZ, ntohl(context->netmask.s_addr)); |
| 1195 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1196 | /* May not have a "guessed" broadcast address if we got no packets via a relay |
| 1197 | from this net yet (ie just unicast renewals after a restart */ |
| 1198 | if (context->broadcast.s_addr && |
| 1199 | in_list(req_options, OPTION_BROADCAST) && |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 1200 | !option_find2(netid, config_opts, OPTION_BROADCAST)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1201 | p = option_put(p, end, OPTION_BROADCAST, INADDRSZ, ntohl(context->broadcast.s_addr)); |
| 1202 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1203 | /* Same comments as broadcast apply, and also may not be able to get a sensible |
| 1204 | default when using subnet select. User must configure by steam in that case. */ |
| 1205 | if (context->router.s_addr && |
| 1206 | in_list(req_options, OPTION_ROUTER) && |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 1207 | !option_find2(netid, config_opts, OPTION_ROUTER)) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1208 | p = option_put(p, end, OPTION_ROUTER, INADDRSZ, ntohl(context->router.s_addr)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1209 | |
| 1210 | if (in_list(req_options, OPTION_DNSSERVER) && |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 1211 | !option_find2(netid, config_opts, OPTION_DNSSERVER)) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 1212 | p = option_put(p, end, OPTION_DNSSERVER, INADDRSZ, ntohl(context->local.s_addr)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1213 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 1214 | if (daemon->domain_suffix && in_list(req_options, OPTION_DOMAINNAME) && |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 1215 | !option_find2(netid, config_opts, OPTION_DOMAINNAME)) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1216 | p = option_put_string(p, end, OPTION_DOMAINNAME, daemon->domain_suffix, null_term); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1217 | |
| 1218 | /* Note that we ignore attempts to set the hostname using |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1219 | --dhcp-option=12,<name> and the fqdn using |
| 1220 | --dhc-option=81,<name> */ |
| 1221 | if (hostname) |
| 1222 | { |
| 1223 | if (in_list(req_options, OPTION_HOSTNAME)) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1224 | p = option_put_string(p, end, OPTION_HOSTNAME, hostname, null_term); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1225 | |
| 1226 | if (fqdn_flags != 0) |
| 1227 | { |
| 1228 | int len = strlen(hostname) + 3; |
| 1229 | if (fqdn_flags & 0x04) |
| 1230 | len += 2; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1231 | else if (null_term) |
| 1232 | len++; |
| 1233 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1234 | if (daemon->domain_suffix) |
| 1235 | len += strlen(daemon->domain_suffix) + 1; |
| 1236 | |
| 1237 | if (p + len + 1 < end) |
| 1238 | { |
| 1239 | *(p++) = OPTION_CLIENT_FQDN; |
| 1240 | *(p++) = len; |
| 1241 | *(p++) = fqdn_flags; |
| 1242 | *(p++) = 255; |
| 1243 | *(p++) = 255; |
| 1244 | |
| 1245 | if (fqdn_flags & 0x04) |
| 1246 | { |
| 1247 | p = do_rfc1035_name(p, hostname); |
| 1248 | if (daemon->domain_suffix) |
| 1249 | p = do_rfc1035_name(p, daemon->domain_suffix); |
| 1250 | *p++ = 0; |
| 1251 | } |
| 1252 | else |
| 1253 | { |
| 1254 | memcpy(p, hostname, strlen(hostname)); |
| 1255 | p += strlen(hostname); |
| 1256 | if (daemon->domain_suffix) |
| 1257 | { |
| 1258 | *(p++) = '.'; |
| 1259 | memcpy(p, daemon->domain_suffix, strlen(daemon->domain_suffix)); |
| 1260 | p += strlen(daemon->domain_suffix); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1261 | } |
| 1262 | if (null_term) |
| 1263 | *(p++) = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | } |
| 1267 | } |
| 1268 | |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1269 | for (opt=config_opts; opt; opt = opt->next) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1270 | { |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1271 | if (opt->opt == OPTION_HOSTNAME || |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1272 | opt->opt == OPTION_CLIENT_FQDN || |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 1273 | opt->opt == OPTION_MAXMESSAGE || |
| 1274 | !in_list(req_options, opt->opt) || |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1275 | opt != option_find2(netid, config_opts, opt->opt)) |
Simon Kelley | 33820b7 | 2004-04-03 21:10:00 +0100 | [diff] [blame] | 1276 | continue; |
| 1277 | |
| 1278 | /* For the options we have default values on |
| 1279 | dhc-option=<optionno> means "don't include this option" |
| 1280 | not "include a zero-length option" */ |
| 1281 | if (opt->len == 0 && |
| 1282 | (opt->opt == OPTION_NETMASK || |
| 1283 | opt->opt == OPTION_BROADCAST || |
| 1284 | opt->opt == OPTION_ROUTER || |
| 1285 | opt->opt == OPTION_DNSSERVER)) |
| 1286 | continue; |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1287 | |
| 1288 | /* opt->val has terminating zero */ |
| 1289 | if (opt->opt == OPTION_VENDOR_ID) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1290 | vendor_class = (char *)opt->val; |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1291 | else |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1292 | p = do_opt(opt, p, end, context->local, null_term); |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | if (in_list(req_options, OPTION_VENDOR_ID)) |
| 1296 | { |
| 1297 | for (opt = daemon->vendor_opts; opt; opt = opt->next) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1298 | if (match_netid(opt->netid, netid, 1) || match_netid(opt->netid, netid, 0)) |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1299 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1300 | if (vendor_class && strcmp(vendor_class, (char *)opt->vendor_class) != 0) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 1301 | syslog(LOG_WARNING, _("More than one vendor class matches, using %s"), vendor_class); |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1302 | else |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1303 | vendor_class = (char *)opt->vendor_class; |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | if (vendor_class) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1307 | { |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1308 | p = option_put_string(p, end, OPTION_VENDOR_ID, vendor_class, 0); |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1309 | |
| 1310 | if (in_list(req_options, OPTION_VENDOR_CLASS_OPT)) |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 1311 | { |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1312 | unsigned char *plen, *oend = end; |
| 1313 | |
| 1314 | /* encapsulated options can only be 256 bytes, |
| 1315 | even of the packet is larger */ |
| 1316 | if (p + 256 < end) |
| 1317 | oend = p + 256; |
| 1318 | |
| 1319 | if (p + 3 >= oend) |
| 1320 | return p; |
| 1321 | |
| 1322 | *(p++) = OPTION_VENDOR_CLASS_OPT; |
| 1323 | plen = p++; /* fill in later */ |
| 1324 | |
| 1325 | for (opt = daemon->vendor_opts; opt; opt = opt->next) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1326 | if ((match_netid(opt->netid, netid, 1) || match_netid(opt->netid, netid, 0)) && |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1327 | strcmp(vendor_class, (char *)opt->vendor_class) == 0) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 1328 | p = do_opt(opt, p, oend, context->local, null_term); |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1329 | |
| 1330 | *plen = p - plen - 1; |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 1331 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1332 | } |
Simon Kelley | 91dccd0 | 2005-03-31 17:48:32 +0100 | [diff] [blame] | 1333 | } |
| 1334 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1335 | return p; |
| 1336 | } |
| 1337 | |
| 1338 | |