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