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