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