Debian Codegraph @

src/rfc2131.c

raw
1 /* dnsmasq is Copyright (c) 2000-2026 Simon Kelley
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, or
6 (at your option) version 3 dated 29 June, 2007.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include "dnsmasq.h"
18
19 #ifdef HAVE_DHCP
20
21 #define option_len(opt) ((int)(((unsigned char *)(opt))[1]))
22 #define option_ptr(opt, i) ((void *)&(((unsigned char *)(opt))[2u+(unsigned int)(i)]))
23
24 #ifdef HAVE_SCRIPT
25 static void add_extradata_opt(struct dhcp_lease *lease, unsigned char *opt);
26 #endif
27
28 static int sanitise(unsigned char *opt, char *buf);
29 static struct in_addr server_id(struct dhcp_context *context, struct in_addr override, struct in_addr fallback);
30 static unsigned int calc_time(struct dhcp_context *context, struct dhcp_config *config, unsigned char *opt);
31 static void option_put(struct dhcp_packet *mess, unsigned char *end, int opt, int len, unsigned int val);
32 static void option_put_string(struct dhcp_packet *mess, unsigned char *end,
33 int opt, const char *string, int null_term);
34 static struct in_addr option_addr(unsigned char *opt);
35 static unsigned int option_uint(unsigned char *opt, int offset, int size);
36 static void log_packet(char *type, void *addr, unsigned char *ext_mac,
37 int mac_len, char *interface, char *string, char *err, u32 xid);
38 static unsigned char *option_find(struct dhcp_packet *mess, size_t size, int opt_type, int minsize);
39 static unsigned char *option_find1(unsigned char *p, unsigned char *end, int opt, int minsize);
40 static size_t dhcp_packet_size(struct dhcp_packet *mess, unsigned char *agent_id, unsigned char *real_end);
41 static void clear_packet(struct dhcp_packet *mess, unsigned char *end);
42 static int in_list(unsigned char *list, int opt);
43 static unsigned char *free_space(struct dhcp_packet *mess, unsigned char *end, int opt, int len);
44 static void do_options(struct dhcp_context *context,
45 struct dhcp_packet *mess,
46 unsigned char *end,
47 unsigned char *req_options,
48 char *hostname,
49 char *domain,
50 struct dhcp_netid *netid,
51 struct in_addr subnet_addr,
52 unsigned char fqdn_flags,
53 int null_term, int pxe_arch,
54 unsigned char *uuid,
55 int vendor_class_len,
56 time_t now,
57 unsigned int lease_time,
58 unsigned short fuzz,
59 const char *pxevendor,
60 int leasequery);
61
62
63 static void match_vendor_opts(unsigned char *opt, struct dhcp_opt *dopt);
64 static int do_encap_opts(struct dhcp_opt *opt, int encap, int flag, struct dhcp_packet *mess, unsigned char *end, int null_term);
65 static void pxe_misc(struct dhcp_packet *mess, unsigned char *end, unsigned char *uuid, const char *pxevendor);
66 static int prune_vendor_opts(struct dhcp_netid *netid);
67 static struct dhcp_opt *pxe_opts(int pxe_arch, struct dhcp_netid *netid, struct in_addr local, time_t now);
68 struct dhcp_boot *find_boot(struct dhcp_netid *netid);
69 static int pxe_uefi_workaround(int pxe_arch, struct dhcp_netid *netid, struct dhcp_packet *mess, struct in_addr local, time_t now, int pxe);
70 static void apply_delay(u32 xid, time_t recvtime, struct dhcp_netid *netid);
71 static int is_pxe_client(struct dhcp_packet *mess, size_t sz, const char **pxe_vendor);
72 static int do_opt(struct dhcp_opt *opt, unsigned char *p, struct dhcp_context *context, int null_term);
73 static void handle_encap(struct dhcp_packet *mess, unsigned char *end, unsigned char *req_options, int null_term, struct dhcp_netid *tagif, int pxemode);
74
75 size_t dhcp_reply(struct dhcp_context *context, char *iface_name, int int_index,
76 size_t sz, time_t now, int unicast_dest, int loopback,
77 int *is_inform, int pxe, struct in_addr fallback, time_t recvtime, struct in_addr leasequery_source)
78 {
79 unsigned char *opt, *clid = NULL;
80 struct dhcp_lease *ltmp, *lease = NULL;
81 struct dhcp_vendor *vendor;
82 struct dhcp_mac *mac;
83 struct dhcp_netid_list *id_list;
84 int clid_len = 0, ignore = 0, do_classes = 0, rapid_commit = 0, selecting = 0, pxearch = -1;
85 const char *pxevendor = NULL;
86 struct dhcp_packet *mess = (struct dhcp_packet *)daemon->dhcp_packet.iov_base;
87 unsigned char *end = (unsigned char *)(mess + 1);
88 unsigned char *real_end = (unsigned char *)(mess + 1);
89 char *hostname = NULL, *offer_hostname = NULL, *client_hostname = NULL, *domain = NULL;
90 int hostname_auth = 0, borken_opt = 0;
91 unsigned char *req_options = NULL;
92 char *message = NULL;
93 unsigned int time;
94 struct dhcp_config *config;
95 struct dhcp_netid *netid, *tagif_netid;
96 struct in_addr subnet_addr, override;
97 unsigned short fuzz = 0;
98 unsigned int mess_type = 0;
99 unsigned char fqdn_flags = 0;
100 unsigned char *agent_id = NULL, *uuid = NULL;
101 unsigned char *emac = NULL;
102 int vendor_class_len = 0, emac_len = 0;
103 struct dhcp_netid known_id, iface_id, cpewan_id;
104 struct dhcp_opt *o;
105 unsigned char pxe_uuid[17];
106 unsigned char *oui = NULL, *serial = NULL;
107 #ifdef HAVE_SCRIPT
108 unsigned char *class = NULL;
109 #endif
110
111 subnet_addr.s_addr = override.s_addr = 0;
112
113 /* set tag with name == interface */
114 iface_id.net = iface_name;
115 iface_id.next = NULL;
116 netid = &iface_id;
117
118 if (mess->op != BOOTREQUEST || mess->hlen > DHCP_CHADDR_MAX)
119 return 0;
120
121 if (mess->htype == 0 && mess->hlen != 0)
122 return 0;
123
124 /* check for DHCP rather than BOOTP */
125 if ((opt = option_find(mess, sz, OPTION_MESSAGE_TYPE, 1)))
126 {
127 u32 cookie = htonl(DHCP_COOKIE);
128
129 /* only insist on a cookie for DHCP. */
130 if (memcmp(mess->options, &cookie, sizeof(u32)) != 0)
131 return 0;
132
133 mess_type = option_uint(opt, 0, 1);
134
135 /* two things to note here: expand_buf may move the packet,
136 so reassign mess from daemon->packet. Also, the size
137 sent includes the IP and UDP headers, hence the magic "-28" */
138 if ((opt = option_find(mess, sz, OPTION_MAXMESSAGE, 2)))
139 {
140 size_t size = (size_t)option_uint(opt, 0, 2) - 28;
141
142 if (size > DHCP_PACKET_MAX)
143 size = DHCP_PACKET_MAX;
144 else if (size < sizeof(struct dhcp_packet))
145 size = sizeof(struct dhcp_packet);
146
147 if (expand_buf(&daemon->dhcp_packet, size))
148 {
149 mess = (struct dhcp_packet *)daemon->dhcp_packet.iov_base;
150 real_end = end = ((unsigned char *)mess) + size;
151 }
152 }
153
154 /* Some buggy clients set ciaddr when they shouldn't, so clear that here since
155 it can affect the context-determination code. */
156 if ((option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ) || mess_type == DHCPDISCOVER))
157 mess->ciaddr.s_addr = 0;
158
159 /* search for device identity from CPEWAN devices, we pass this through to the script */
160 if ((opt = option_find(mess, sz, OPTION_VENDOR_IDENT_OPT, 5)))
161 {
162 unsigned int elen, offset, len = option_len(opt);
163
164 for (offset = 0; offset < (len - 5); offset += elen + 5)
165 {
166 elen = option_uint(opt, offset + 4 , 1);
167 if (option_uint(opt, offset, 4) == BRDBAND_FORUM_IANA && offset + elen + 5 <= len)
168 {
169 unsigned char *x = option_ptr(opt, offset + 5);
170 unsigned char *y = option_ptr(opt, offset + elen + 5);
171 oui = option_find1(x, y, 1, 1);
172 serial = option_find1(x, y, 2, 1);
173 #ifdef HAVE_SCRIPT
174 class = option_find1(x, y, 3, 1);
175 #endif
176 /* If TR069-id is present set the tag "cpewan-id" to facilitate echoing
177 the gateway id back. Note that the device class is optional */
178 if (oui && serial)
179 {
180 cpewan_id.net = "cpewan-id";
181 cpewan_id.next = netid;
182 netid = &cpewan_id;
183 }
184 break;
185 }
186 }
187 }
188
189 if (mess_type != DHCPLEASEQUERY && (opt = option_find(mess, sz, OPTION_AGENT_ID, 1)))
190 {
191 /* Any agent-id needs to be copied back out, verbatim, as the last option
192 in the packet. Here, we shift it to the very end of the buffer, if it doesn't
193 get overwritten, then it will be shuffled back at the end of processing.
194 Note that the incoming options must not be overwritten here, so there has to
195 be enough free space at the end of the packet to copy the option. */
196 unsigned char *sopt;
197 unsigned int total = option_len(opt) + 2;
198 unsigned char *last_opt = option_find1(&mess->options[0] + sizeof(u32), ((unsigned char *)mess) + sz,
199 OPTION_END, 0);
200 if (last_opt && last_opt < end - total)
201 {
202 end -= total;
203 agent_id = end;
204 memcpy(agent_id, opt, total);
205 }
206
207 /* look for RFC5010 flags sub-option */
208 if ((sopt = option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_FLAGS, INADDRSZ)))
209 unicast_dest = !!(option_uint(opt, 0, 1) & 0x80);
210
211 /* look for RFC3527 Link selection sub-option */
212 if ((sopt = option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_SUBNET_SELECT, INADDRSZ)))
213 subnet_addr = option_addr(sopt);
214
215 /* look for RFC5107 server-identifier-override */
216 if ((sopt = option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_SERVER_OR, INADDRSZ)))
217 override = option_addr(sopt);
218
219 /* if a circuit-id or remote-is option is provided, exact-match to options. */
220 for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next)
221 {
222 int search;
223
224 if (vendor->match_type == MATCH_CIRCUIT)
225 search = SUBOPT_CIRCUIT_ID;
226 else if (vendor->match_type == MATCH_REMOTE)
227 search = SUBOPT_REMOTE_ID;
228 else if (vendor->match_type == MATCH_SUBSCRIBER)
229 search = SUBOPT_SUBSCR_ID;
230 else
231 continue;
232
233 if ((sopt = option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), search, 1)) &&
234 vendor->len == option_len(sopt) &&
235 memcmp(option_ptr(sopt, 0), vendor->data, vendor->len) == 0)
236 {
237 vendor->netid.next = netid;
238 netid = &vendor->netid;
239 }
240 }
241 }
242
243 /* Check for RFC3011 subnet selector - only if RFC3527 one not present */
244 if (subnet_addr.s_addr == 0 && (opt = option_find(mess, sz, OPTION_SUBNET_SELECT, INADDRSZ)))
245 subnet_addr = option_addr(opt);
246
247 /* If there is no client identifier option, use the hardware address */
248 if (!option_bool(OPT_IGNORE_CLID) && (opt = option_find(mess, sz, OPTION_CLIENT_ID, 1)))
249 {
250 clid_len = option_len(opt);
251 clid = option_ptr(opt, 0);
252 }
253
254 /* do we have a lease in store? */
255 lease = lease_find_by_client(mess->chaddr, mess->hlen, mess->htype, clid, clid_len);
256
257 /* If this request is missing a clid, but we've seen one before,
258 use it again for option matching etc. */
259 if (lease && !clid && lease->clid)
260 {
261 clid_len = lease->clid_len;
262 clid = lease->clid;
263 }
264
265 /* find mac to use for logging and hashing */
266 emac = extended_hwaddr(mess->htype, mess->hlen, mess->chaddr, clid_len, clid, &emac_len);
267 }
268
269 for (mac = daemon->dhcp_macs; mac; mac = mac->next)
270 if (mac->hwaddr_len == mess->hlen &&
271 (mac->hwaddr_type == mess->htype || mac->hwaddr_type == 0) &&
272 memcmp_masked(mac->hwaddr, mess->chaddr, mess->hlen, mac->mask))
273 {
274 mac->netid.next = netid;
275 netid = &mac->netid;
276 }
277
278 /* Determine network for this packet. Our caller will have already linked all the
279 contexts which match the addresses of the receiving interface but if the
280 machine has an address already, or came via a relay, or we have a subnet selector,
281 we search again. If we don't have have a giaddr or explicit subnet selector,
282 use the ciaddr. This is necessary because a machine which got a lease via a
283 relay won't use the relay to renew. If matching a ciaddr fails but we have a context
284 from the physical network, continue using that to allow correct DHCPNAK generation later. */
285 if (mess->giaddr.s_addr || subnet_addr.s_addr || mess->ciaddr.s_addr)
286 {
287 struct dhcp_context *context_tmp, *context_new = NULL;
288 struct shared_network *share = NULL;
289 struct in_addr addr;
290 int force = 0, via_relay = 0;
291
292 if (subnet_addr.s_addr)
293 {
294 addr = subnet_addr;
295 force = 1;
296 if (mess->giaddr.s_addr)
297 via_relay = 1;
298 }
299 else if (mess->giaddr.s_addr)
300 {
301 addr = mess->giaddr;
302 force = 1;
303 via_relay = 1;
304 }
305 else
306 {
307 /* If ciaddr is in the hardware derived set of contexts, leave that unchanged */
308 addr = mess->ciaddr;
309 for (context_tmp = context; context_tmp; context_tmp = context_tmp->current)
310 if (context_tmp->netmask.s_addr &&
311 is_same_net(addr, context_tmp->start, context_tmp->netmask) &&
312 is_same_net(addr, context_tmp->end, context_tmp->netmask))
313 {
314 context_new = context;
315 break;
316 }
317 }
318
319 if (!context_new)
320 {
321 for (context_tmp = daemon->dhcp; context_tmp; context_tmp = context_tmp->next)
322 {
323 struct in_addr netmask = context_tmp->netmask;
324
325 /* guess the netmask for relayed networks */
326 if (!(context_tmp->flags & CONTEXT_NETMASK) && context_tmp->netmask.s_addr == 0)
327 {
328 if (IN_CLASSA(ntohl(context_tmp->start.s_addr)) && IN_CLASSA(ntohl(context_tmp->end.s_addr)))
329 netmask.s_addr = htonl(0xff000000);
330 else if (IN_CLASSB(ntohl(context_tmp->start.s_addr)) && IN_CLASSB(ntohl(context_tmp->end.s_addr)))
331 netmask.s_addr = htonl(0xffff0000);
332 else if (IN_CLASSC(ntohl(context_tmp->start.s_addr)) && IN_CLASSC(ntohl(context_tmp->end.s_addr)))
333 netmask.s_addr = htonl(0xffffff00);
334 }
335
336 /* check to see is a context is OK because of a shared address on
337 the relayed subnet. */
338 if (via_relay)
339 for (share = daemon->shared_networks; share; share = share->next)
340 {
341 #ifdef HAVE_DHCP6
342 if (share->shared_addr.s_addr == 0)
343 continue;
344 #endif
345 if (share->if_index != 0 ||
346 share->match_addr.s_addr != mess->giaddr.s_addr)
347 continue;
348
349 if (netmask.s_addr != 0 &&
350 is_same_net(share->shared_addr, context_tmp->start, netmask) &&
351 is_same_net(share->shared_addr, context_tmp->end, netmask))
352 break;
353 }
354
355 /* This section fills in context mainly when a client which is on a remote (relayed)
356 network renews a lease without using the relay, after dnsmasq has restarted. */
357 if (share ||
358 (netmask.s_addr != 0 &&
359 is_same_net(addr, context_tmp->start, netmask) &&
360 is_same_net(addr, context_tmp->end, netmask)))
361 {
362 context_tmp->netmask = netmask;
363 if (context_tmp->local.s_addr == 0)
364 context_tmp->local = fallback;
365 if (context_tmp->router.s_addr == 0 && !share)
366 {
367 if (override.s_addr)
368 context_tmp->router = override;
369 else
370 context_tmp->router = mess->giaddr;
371 }
372
373 /* fill in missing broadcast addresses for relayed ranges */
374 if (!(context_tmp->flags & CONTEXT_BRDCAST) && context_tmp->broadcast.s_addr == 0 )
375 context_tmp->broadcast.s_addr = context_tmp->start.s_addr | ~context_tmp->netmask.s_addr;
376
377 context_tmp->current = context_new;
378 context_new = context_tmp;
379 }
380
381 }
382 }
383
384 if (context_new || force)
385 context = context_new;
386 }
387
388 if (mess_type != DHCPLEASEQUERY)
389 {
390 if (!context)
391 {
392 const char *via;
393 if (subnet_addr.s_addr)
394 {
395 via = _("with subnet selector");
396 inet_ntop(AF_INET, &subnet_addr, daemon->addrbuff, ADDRSTRLEN);
397 }
398 else
399 {
400 via = _("via");
401 if (mess->giaddr.s_addr)
402 inet_ntop(AF_INET, &mess->giaddr, daemon->addrbuff, ADDRSTRLEN);
403 else
404 safe_strncpy(daemon->addrbuff, iface_name, ADDRSTRLEN);
405 }
406 my_syslog(MS_DHCP | LOG_WARNING, _("no address range available for DHCP request %s %s"),
407 via, daemon->addrbuff);
408 return 0;
409 }
410
411 if (option_bool(OPT_LOG_OPTS))
412 {
413 struct dhcp_context *context_tmp;
414 for (context_tmp = context; context_tmp; context_tmp = context_tmp->current)
415 {
416 inet_ntop(AF_INET, &context_tmp->start, daemon->namebuff, MAXDNAMESTR);
417 if (context_tmp->flags & (CONTEXT_STATIC | CONTEXT_PROXY))
418 {
419 inet_ntop(AF_INET, &context_tmp->netmask, daemon->addrbuff, ADDRSTRLEN);
420 my_syslog(MS_DHCP | LOG_INFO, _("%u available DHCP subnet: %s/%s"),
421 ntohl(mess->xid), daemon->namebuff, daemon->addrbuff);
422 }
423 else
424 {
425 inet_ntop(AF_INET, &context_tmp->end, daemon->addrbuff, ADDRSTRLEN);
426 my_syslog(MS_DHCP | LOG_INFO, _("%u available DHCP range: %s -- %s"),
427 ntohl(mess->xid), daemon->namebuff, daemon->addrbuff);
428 }
429 }
430 }
431 }
432
433 /* dhcp-match. If we have hex-and-wildcards, look for a left-anchored match.
434 Otherwise assume the option is an array, and look for a matching element.
435 If no data given, existence of the option is enough. This code handles
436 rfc3925 V-I classes too. */
437 for (o = daemon->dhcp_match; o; o = o->next)
438 {
439 unsigned int len, elen, match = 0;
440 size_t offset, o2;
441
442 if (o->flags & DHOPT_RFC3925)
443 {
444 if (!(opt = option_find(mess, sz, OPTION_VENDOR_IDENT, 5)))
445 continue;
446
447 for (offset = 0; offset < (option_len(opt) - 5u); offset += len + 5)
448 {
449 len = option_uint(opt, offset + 4 , 1);
450 /* Need to take care that bad data can't run us off the end of the packet */
451 if ((offset + len + 5 <= (unsigned)(option_len(opt))) &&
452 (option_uint(opt, offset, 4) == (unsigned int)o->u.encap))
453 for (o2 = offset + 5; o2 < offset + len + 5; o2 += elen + 1)
454 {
455 elen = option_uint(opt, o2, 1);
456 if ((o2 + elen + 1 <= (unsigned)option_len(opt)) &&
457 (match = match_bytes(o, option_ptr(opt, o2 + 1), elen)))
458 break;
459 }
460 if (match)
461 break;
462 }
463 }
464 else
465 {
466 if (!(opt = option_find(mess, sz, o->opt, 1)))
467 continue;
468
469 match = match_bytes(o, option_ptr(opt, 0), option_len(opt));
470 }
471
472 if (match)
473 {
474 o->netid->next = netid;
475 netid = o->netid;
476 }
477 }
478
479 /* user-class options are, according to RFC3004, supposed to contain
480 a set of counted strings. Here we check that this is so (by seeing
481 if the counts are consistent with the overall option length) and if
482 so zero the counts so that we don't get spurious matches between
483 the vendor string and the counts. If the lengths don't add up, we
484 assume that the option is a single string and non RFC3004 compliant
485 and just do the substring match. dhclient provides these broken options.
486 The code, later, which sends user-class data to the lease-change script
487 relies on the transformation done here.
488 */
489
490 if ((opt = option_find(mess, sz, OPTION_USER_CLASS, 1)))
491 {
492 unsigned char *ucp = option_ptr(opt, 0);
493 int tmp, j;
494 for (j = 0; j < option_len(opt); j += ucp[j] + 1);
495 if (j == option_len(opt))
496 for (j = 0; j < option_len(opt); j = tmp)
497 {
498 tmp = j + ucp[j] + 1;
499 ucp[j] = 0;
500 }
501 }
502
503 for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next)
504 {
505 int mopt;
506
507 if (vendor->match_type == MATCH_VENDOR)
508 mopt = OPTION_VENDOR_ID;
509 else if (vendor->match_type == MATCH_USER)
510 mopt = OPTION_USER_CLASS;
511 else
512 continue;
513
514 if ((opt = option_find(mess, sz, mopt, 1)))
515 {
516 int i;
517 for (i = 0; i <= (option_len(opt) - vendor->len); i++)
518 if (memcmp(vendor->data, option_ptr(opt, i), vendor->len) == 0)
519 {
520 vendor->netid.next = netid;
521 netid = &vendor->netid;
522 break;
523 }
524 }
525 }
526
527 /* mark vendor-encapsulated options which match the client-supplied vendor class,
528 save client-supplied vendor class */
529 if ((opt = option_find(mess, sz, OPTION_VENDOR_ID, 1)))
530 {
531 memcpy(daemon->dhcp_buff3, option_ptr(opt, 0), option_len(opt));
532 vendor_class_len = option_len(opt);
533 }
534 match_vendor_opts(opt, daemon->dhcp_opts);
535
536 if (option_bool(OPT_LOG_OPTS))
537 {
538 if (sanitise(opt, daemon->namebuff))
539 my_syslog(MS_DHCP | LOG_INFO, _("%u vendor class: %s"), ntohl(mess->xid), daemon->namebuff);
540 if (sanitise(option_find(mess, sz, OPTION_USER_CLASS, 1), daemon->namebuff))
541 my_syslog(MS_DHCP | LOG_INFO, _("%u user class: %s"), ntohl(mess->xid), daemon->namebuff);
542 }
543
544 mess->op = BOOTREPLY;
545
546 config = find_config(daemon->dhcp_conf, context, clid, clid_len,
547 mess->chaddr, mess->hlen, mess->htype, NULL, run_tag_if(netid));
548
549 /* set "known" tag for known hosts */
550 if (config)
551 {
552 known_id.net = "known";
553 known_id.next = netid;
554 netid = &known_id;
555 }
556 else if (find_config(daemon->dhcp_conf, NULL, clid, clid_len,
557 mess->chaddr, mess->hlen, mess->htype, NULL, run_tag_if(netid)))
558 {
559 known_id.net = "known-othernet";
560 known_id.next = netid;
561 netid = &known_id;
562 }
563
564 if (mess_type == 0 && !pxe)
565 {
566 /* BOOTP request */
567 struct dhcp_netid id, bootp_id;
568 struct in_addr *logaddr = NULL;
569
570 /* must have a MAC addr for bootp */
571 if (mess->htype == 0 || mess->hlen == 0 || (context->flags & CONTEXT_PROXY))
572 return 0;
573
574 if (have_config(config, CONFIG_DISABLE))
575 message = _("disabled");
576
577 end = mess->options + 64; /* BOOTP vend area is only 64 bytes */
578
579 if (have_config(config, CONFIG_NAME))
580 {
581 hostname = config->hostname;
582 domain = config->domain;
583 }
584
585 if (config)
586 {
587 struct dhcp_netid_list *list;
588
589 for (list = config->netid; list; list = list->next)
590 {
591 list->list->next = netid;
592 netid = list->list;
593 }
594 }
595
596 /* Match incoming filename field as a netid. */
597 if (mess->file[0])
598 {
599 memcpy(daemon->dhcp_buff2, mess->file, sizeof(mess->file));
600 daemon->dhcp_buff2[sizeof(mess->file)] = 0; /* ensure zero term. */
601 id.net = (char *)daemon->dhcp_buff2;
602 id.next = netid;
603 netid = &id;
604 }
605
606 /* Add "bootp" as a tag to allow different options, address ranges etc
607 for BOOTP clients */
608 bootp_id.net = "bootp";
609 bootp_id.next = netid;
610 netid = &bootp_id;
611
612 tagif_netid = run_tag_if(netid);
613
614 for (id_list = daemon->dhcp_ignore; id_list; id_list = id_list->next)
615 if (match_netid(id_list->list, tagif_netid, 0))
616 message = _("ignored");
617
618 if (!message)
619 {
620 int nailed = 0;
621
622 if (have_config(config, CONFIG_ADDR))
623 {
624 nailed = 1;
625 logaddr = &config->addr;
626 mess->yiaddr = config->addr;
627 if ((lease = lease_find_by_addr(config->addr)) &&
628 (lease->hwaddr_len != mess->hlen ||
629 lease->hwaddr_type != mess->htype ||
630 memcmp(lease->hwaddr, mess->chaddr, lease->hwaddr_len) != 0))
631 message = _("address in use");
632 }
633 else
634 {
635 if (!(lease = lease_find_by_client(mess->chaddr, mess->hlen, mess->htype, NULL, 0)) ||
636 !address_available(context, lease->addr, tagif_netid))
637 {
638 if (lease)
639 {
640 /* lease exists, wrong network. */
641 lease_prune(lease, now);
642 lease = NULL;
643 }
644 if (!address_allocate(context, &mess->yiaddr, mess->chaddr, mess->hlen, tagif_netid, now, loopback))
645 message = _("no address available");
646 }
647 else
648 mess->yiaddr = lease->addr;
649 }
650
651 if (!message && !(context = narrow_context(context, mess->yiaddr, netid)))
652 message = _("wrong network");
653 else if (context->netid.net)
654 {
655 context->netid.next = netid;
656 tagif_netid = run_tag_if(&context->netid);
657 }
658
659 log_tags(tagif_netid, ntohl(mess->xid));
660
661 if (!message && !nailed)
662 {
663 for (id_list = daemon->bootp_dynamic; id_list; id_list = id_list->next)
664 if ((!id_list->list) || match_netid(id_list->list, tagif_netid, 0))
665 break;
666 if (!id_list)
667 message = _("no address configured");
668 }
669
670 if (!message &&
671 !lease &&
672 (!(lease = lease4_allocate(mess->yiaddr))))
673 message = _("no leases left");
674
675 if (!message)
676 {
677 logaddr = &mess->yiaddr;
678
679 lease_set_hwaddr(lease, mess->chaddr, NULL, mess->hlen, mess->htype, 0, now, 1);
680 if (hostname)
681 lease_set_hostname(lease, hostname, 1, get_domain(lease->addr), domain);
682 /* infinite lease unless nailed in dhcp-host line. */
683 lease_set_expires(lease,
684 have_config(config, CONFIG_TIME) ? config->lease_time : 0xffffffff,
685 now);
686 lease_set_interface(lease, int_index, now);
687
688 clear_packet(mess, end);
689 do_options(context, mess, end, NULL, hostname, get_domain(mess->yiaddr),
690 netid, subnet_addr, 0, 0, -1, NULL, vendor_class_len, now, 0xffffffff, 0, NULL, 0);
691 }
692 }
693
694 daemon->metrics[METRIC_BOOTP]++;
695 log_packet("BOOTP", logaddr, mess->chaddr, mess->hlen, iface_name, NULL, message, mess->xid);
696
697 return message ? 0 : dhcp_packet_size(mess, agent_id, real_end);
698 }
699
700 if ((opt = option_find(mess, sz, OPTION_CLIENT_FQDN, 3)))
701 {
702 /* http://tools.ietf.org/wg/dhc/draft-ietf-dhc-fqdn-option/draft-ietf-dhc-fqdn-option-10.txt */
703 int len = option_len(opt);
704 char *pq = daemon->dhcp_buff;
705 unsigned char *pp, *op = option_ptr(opt, 0);
706
707 fqdn_flags = *op;
708 len -= 3;
709 op += 3;
710 pp = op;
711
712 /* NB, the following always sets at least one bit */
713 if (option_bool(OPT_FQDN_UPDATE))
714 {
715 if (fqdn_flags & 0x01)
716 {
717 fqdn_flags |= 0x02; /* set O */
718 fqdn_flags &= ~0x01; /* clear S */
719 }
720 fqdn_flags |= 0x08; /* set N */
721 }
722 else
723 {
724 if (!(fqdn_flags & 0x01))
725 fqdn_flags |= 0x03; /* set S and O */
726 fqdn_flags &= ~0x08; /* clear N */
727 }
728
729 if (fqdn_flags & 0x04)
730 while (*op != 0 && ((op + (*op)) - pp) < len)
731 {
732 memcpy(pq, op+1, *op);
733 pq += *op;
734 op += (*op)+1;
735 *(pq++) = '.';
736 }
737 else
738 {
739 memcpy(pq, op, len);
740 if (len > 0 && op[len-1] == 0)
741 borken_opt = 1;
742 pq += len + 1;
743 }
744
745 if (pq != daemon->dhcp_buff)
746 pq--;
747
748 *pq = 0;
749
750 if (legal_hostname(daemon->dhcp_buff))
751 offer_hostname = client_hostname = daemon->dhcp_buff;
752 }
753 else if ((opt = option_find(mess, sz, OPTION_HOSTNAME, 1)))
754 {
755 int len = option_len(opt);
756 memcpy(daemon->dhcp_buff, option_ptr(opt, 0), len);
757 /* Microsoft clients are broken, and need zero-terminated strings
758 in options. We detect this state here, and do the same in
759 any options we send */
760 if (len > 0 && daemon->dhcp_buff[len-1] == 0)
761 borken_opt = 1;
762 else
763 daemon->dhcp_buff[len] = 0;
764 if (legal_hostname(daemon->dhcp_buff))
765 client_hostname = daemon->dhcp_buff;
766 }
767
768 if (client_hostname)
769 {
770 struct dhcp_match_name *m;
771 size_t nl = strlen(client_hostname);
772
773 if (option_bool(OPT_LOG_OPTS))
774 my_syslog(MS_DHCP | LOG_INFO, _("%u client provides name: %s"), ntohl(mess->xid), client_hostname);
775 for (m = daemon->dhcp_name_match; m; m = m->next)
776 {
777 size_t ml = strlen(m->name);
778 char save = 0;
779
780 if (nl < ml)
781 continue;
782 if (nl > ml)
783 {
784 save = client_hostname[ml];
785 client_hostname[ml] = 0;
786 }
787
788 if (hostname_isequal(client_hostname, m->name) &&
789 (save == 0 || m->wildcard))
790 {
791 m->netid->next = netid;
792 netid = m->netid;
793 }
794
795 if (save != 0)
796 client_hostname[ml] = save;
797 }
798 }
799
800 if (have_config(config, CONFIG_NAME))
801 {
802 hostname = config->hostname;
803 domain = config->domain;
804 hostname_auth = 1;
805 /* be careful not to send an OFFER with a hostname not matching the DISCOVER. */
806 if (fqdn_flags != 0 || !client_hostname || hostname_isequal(hostname, client_hostname))
807 offer_hostname = hostname;
808 }
809 else if (client_hostname)
810 {
811 domain = strip_hostname(client_hostname);
812
813 if (strlen(client_hostname) != 0)
814 {
815 hostname = client_hostname;
816
817 if (!config)
818 {
819 /* Search again now we have a hostname.
820 Only accept configs without CLID and HWADDR here, (they won't match)
821 to avoid impersonation by name. */
822 struct dhcp_config *new = find_config(daemon->dhcp_conf, context, NULL, 0,
823 mess->chaddr, mess->hlen,
824 mess->htype, hostname, run_tag_if(netid));
825 if (new && !have_config(new, CONFIG_CLID) && !new->hwaddr)
826 {
827 config = new;
828 /* set "known" tag for known hosts */
829 known_id.net = "known";
830 known_id.next = netid;
831 netid = &known_id;
832 }
833 }
834 }
835 }
836
837 if (mess_type != DHCPLEASEQUERY && config)
838 {
839 struct dhcp_netid_list *list;
840
841 for (list = config->netid; list; list = list->next)
842 {
843 list->list->next = netid;
844 netid = list->list;
845 }
846 }
847
848 tagif_netid = run_tag_if(netid);
849
850 /* if all the netids in the ignore list are present, ignore this client */
851 for (id_list = daemon->dhcp_ignore; id_list; id_list = id_list->next)
852 if (match_netid(id_list->list, tagif_netid, 0))
853 ignore = 1;
854
855 /* If configured, we can override the server-id to be the address of the relay,
856 so that all traffic goes via the relay and can pick up agent-id info. This can be
857 configured for all relays, or by address. */
858 if (daemon->override && mess->giaddr.s_addr != 0 && override.s_addr == 0)
859 {
860 if (!daemon->override_relays)
861 override = mess->giaddr;
862 else
863 {
864 struct addr_list *l;
865 for (l = daemon->override_relays; l; l = l->next)
866 if (l->addr.s_addr == mess->giaddr.s_addr)
867 break;
868 if (l)
869 override = mess->giaddr;
870 }
871 }
872
873 /* Can have setting to ignore the client ID for a particular MAC address or hostname */
874 if (have_config(config, CONFIG_NOCLID))
875 clid = NULL;
876
877 /* Check if client is PXE client. */
878 if (mess_type != DHCPLEASEQUERY &&
879 daemon->enable_pxe &&
880 is_pxe_client(mess, sz, &pxevendor))
881 {
882 if ((opt = option_find(mess, sz, OPTION_PXE_UUID, 17)))
883 {
884 memcpy(pxe_uuid, option_ptr(opt, 0), 17);
885 uuid = pxe_uuid;
886 }
887
888 /* Check if this is really a PXE bootserver request, and handle specially if so. */
889 if ((mess_type == DHCPREQUEST || mess_type == DHCPINFORM) &&
890 (opt = option_find(mess, sz, OPTION_VENDOR_CLASS_OPT, 1)) &&
891 (opt = option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_PXE_BOOT_ITEM, 4)))
892 {
893 struct pxe_service *service;
894 int type = option_uint(opt, 0, 2);
895 int layer = option_uint(opt, 2, 2);
896 unsigned char save71[4];
897 struct dhcp_opt opt71;
898
899 if (ignore)
900 return 0;
901
902 if (layer & 0x8000)
903 {
904 my_syslog(MS_DHCP | LOG_ERR, _("PXE BIS not supported"));
905 return 0;
906 }
907
908 memcpy(save71, option_ptr(opt, 0), 4);
909
910 for (service = daemon->pxe_services; service; service = service->next)
911 if (service->type == type)
912 break;
913
914 for (; context; context = context->current)
915 if (match_netid(context->filter, tagif_netid, 1) &&
916 is_same_net(mess->ciaddr, context->start, context->netmask))
917 break;
918
919 if (!service || !service->basename || !context)
920 return 0;
921
922 clear_packet(mess, end);
923
924 mess->yiaddr = mess->ciaddr;
925 mess->ciaddr.s_addr = 0;
926 if (service->sname)
927 mess->siaddr = a_record_from_hosts(service->sname, now);
928 else if (service->server.s_addr != 0)
929 mess->siaddr = service->server;
930 else
931 mess->siaddr = context->local;
932
933 if (strchr(service->basename, '.'))
934 snprintf((char *)mess->file, sizeof(mess->file),
935 "%s", service->basename);
936 else
937 snprintf((char *)mess->file, sizeof(mess->file),
938 "%s.%d", service->basename, layer);
939
940 option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPACK);
941 option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, htonl(context->local.s_addr));
942 pxe_misc(mess, end, uuid, pxevendor);
943
944 prune_vendor_opts(tagif_netid);
945 opt71.val = save71;
946 opt71.opt = SUBOPT_PXE_BOOT_ITEM;
947 opt71.len = 4;
948 opt71.flags = DHOPT_VENDOR_MATCH;
949 opt71.netid = NULL;
950 opt71.next = daemon->dhcp_opts;
951 do_encap_opts(&opt71, OPTION_VENDOR_CLASS_OPT, DHOPT_VENDOR_MATCH, mess, end, 0);
952
953 daemon->metrics[METRIC_PXE]++;
954 log_packet("PXE", &mess->yiaddr, emac, emac_len, iface_name, (char *)mess->file, NULL, mess->xid);
955 log_tags(tagif_netid, ntohl(mess->xid));
956 return dhcp_packet_size(mess, agent_id, real_end);
957 }
958
959 if ((opt = option_find(mess, sz, OPTION_ARCH, 2)))
960 {
961 pxearch = option_uint(opt, 0, 2);
962
963 /* proxy DHCP here. */
964 if ((mess_type == DHCPDISCOVER || (pxe && mess_type == DHCPREQUEST)))
965 {
966 struct dhcp_context *tmp;
967 int workaround = 0;
968
969 for (tmp = context; tmp; tmp = tmp->current)
970 if ((tmp->flags & CONTEXT_PROXY) &&
971 match_netid(tmp->filter, tagif_netid, 1))
972 break;
973
974 if (tmp)
975 {
976 struct dhcp_boot *boot;
977 int redirect4011 = 0;
978 struct dhcp_opt *option;
979
980 /* OK only dhcp-option-pxe options. */
981 tagif_netid = option_filter(netid, tmp->netid.net ? &tmp->netid : NULL, daemon->dhcp_opts, 2);
982 boot = find_boot(tagif_netid);
983
984 mess->yiaddr.s_addr = 0;
985 if (mess_type == DHCPDISCOVER || mess->ciaddr.s_addr == 0)
986 {
987 mess->ciaddr.s_addr = 0;
988 mess->flags |= htons(0x8000); /* broadcast */
989 }
990
991 clear_packet(mess, end);
992
993 /* Redirect EFI clients to port 4011 */
994 if (pxearch >= 6)
995 {
996 redirect4011 = 1;
997 mess->siaddr = tmp->local;
998 }
999
1000 /* Returns true if only one matching service is available. On port 4011,
1001 it also inserts the boot file and server name. */
1002 workaround = pxe_uefi_workaround(pxearch, tagif_netid, mess, tmp->local, now, pxe);
1003
1004 if (!workaround && boot)
1005 {
1006 /* Provide the bootfile here, for iPXE, and in case we have no menu items
1007 and set discovery_control = 8 */
1008 if (boot->next_server.s_addr)
1009 mess->siaddr = boot->next_server;
1010 else if (boot->tftp_sname)
1011 mess->siaddr = a_record_from_hosts(boot->tftp_sname, now);
1012
1013 if (boot->file)
1014 safe_strncpy((char *)mess->file, boot->file, sizeof(mess->file));
1015 }
1016
1017 option_put(mess, end, OPTION_MESSAGE_TYPE, 1,
1018 mess_type == DHCPDISCOVER ? DHCPOFFER : DHCPACK);
1019 option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, htonl(tmp->local.s_addr));
1020 pxe_misc(mess, end, uuid, pxevendor);
1021 prune_vendor_opts(tagif_netid);
1022 if ((pxe && !workaround) || !redirect4011)
1023 do_encap_opts(pxe_opts(pxearch, tagif_netid, tmp->local, now), OPTION_VENDOR_CLASS_OPT, DHOPT_VENDOR_MATCH, mess, end, 0);
1024
1025 /* dhcp-option-pxe ONLY */
1026 for (option = daemon->dhcp_opts; option; option = option->next)
1027 {
1028 int len;
1029 unsigned char *p;
1030
1031 if (!(option->flags & DHOPT_TAGOK))
1032 continue;
1033
1034 len = do_opt(option, NULL, tmp, borken_opt);
1035
1036 if ((p = free_space(mess, end, option->opt, len)))
1037 do_opt(option, p, tmp, borken_opt);
1038 }
1039
1040 handle_encap(mess, end, req_options, borken_opt, tagif_netid, 2);
1041
1042 daemon->metrics[METRIC_PXE]++;
1043 log_packet("PXE", NULL, emac, emac_len, iface_name, ignore ? "proxy-ignored" : "proxy", NULL, mess->xid);
1044 log_tags(tagif_netid, ntohl(mess->xid));
1045 if (!ignore)
1046 apply_delay(mess->xid, recvtime, tagif_netid);
1047 return ignore ? 0 : dhcp_packet_size(mess, agent_id, real_end);
1048 }
1049 }
1050 }
1051 }
1052
1053 /* if we're just a proxy server, go no further */
1054 if (mess_type != DHCPLEASEQUERY &&
1055 ((context->flags & CONTEXT_PROXY) || pxe))
1056 return 0;
1057
1058 if ((opt = option_find(mess, sz, OPTION_REQUESTED_OPTIONS, 0)))
1059 {
1060 req_options = (unsigned char *)daemon->dhcp_buff2;
1061 memcpy(req_options, option_ptr(opt, 0), option_len(opt));
1062 req_options[option_len(opt)] = OPTION_END;
1063 }
1064
1065 switch (mess_type)
1066 {
1067 case DHCPLEASEQUERY:
1068 mess_type = DHCPLEASEUNKNOWN;
1069
1070 if (!option_bool(OPT_LEASEQUERY))
1071 return 0;
1072
1073 if (leasequery_source.s_addr == 0)
1074 return 0;
1075
1076 inet_ntop(AF_INET, &leasequery_source, daemon->workspacename, ADDRSTRLEN);
1077
1078 if (daemon->leasequery_addr)
1079 {
1080 struct bogus_addr *baddrp;
1081
1082 for (baddrp = daemon->leasequery_addr; baddrp; baddrp = baddrp->next)
1083 if (!baddrp->is6 && is_same_net_prefix(leasequery_source, baddrp->addr.addr4, baddrp->prefix))
1084 break;
1085
1086 if (!baddrp)
1087 {
1088 my_syslog(MS_DHCP | LOG_WARNING, _("leasequery from %s not permitted"), daemon->workspacename);
1089 return 0;
1090 }
1091 }
1092
1093 daemon->metrics[METRIC_DHCPLEASEQUERY]++;
1094 log_packet("DHCPLEASEQUERY", mess->ciaddr.s_addr ? &mess->ciaddr : NULL, emac_len != 0 ? emac : NULL, emac_len,
1095 iface_name, "from ", daemon->workspacename, mess->xid);
1096
1097 /* Put all the contexts on the ->current list for the next stages. */
1098 for (context = daemon->dhcp; context; context = context->next)
1099 context->current = context->next;
1100
1101 /* Have maybe already found the lease by MAC or clid. */
1102 if (mess->ciaddr.s_addr != 0 &&
1103 !(lease = lease_find_by_addr(mess->ciaddr)) &&
1104 address_available(daemon->dhcp, mess->ciaddr, tagif_netid))
1105 {
1106 mess_type = DHCPLEASEUNASSIGNED;
1107 daemon->metrics[METRIC_DHCPLEASEUNASSIGNED]++;
1108 }
1109
1110 if (lease)
1111 {
1112 /* RFC4388 para 6.4.2 */
1113 if (lease->agent_id)
1114 {
1115 unsigned char *sopt ;
1116
1117 for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next)
1118 {
1119 int search;
1120
1121 if (vendor->match_type == MATCH_CIRCUIT)
1122 search = SUBOPT_CIRCUIT_ID;
1123 else if (vendor->match_type == MATCH_REMOTE)
1124 search = SUBOPT_REMOTE_ID;
1125 else if (vendor->match_type == MATCH_SUBSCRIBER)
1126 search = SUBOPT_SUBSCR_ID;
1127 else
1128 continue;
1129
1130 if ((sopt = option_find1(lease->agent_id, lease->agent_id + lease->agent_id_len, search, 1)) &&
1131 vendor->len == option_len(sopt) &&
1132 memcmp(option_ptr(sopt, 0), vendor->data, vendor->len) == 0)
1133 {
1134 vendor->netid.next = netid;
1135 netid = &vendor->netid;
1136 }
1137 }
1138
1139 tagif_netid = run_tag_if(netid);
1140 }
1141
1142 /* Now find the context for this lease and config for this host. */
1143 if ((context = narrow_context(daemon->dhcp, lease->addr, tagif_netid)))
1144 {
1145 if ((config = find_config(daemon->dhcp_conf, context, lease->clid, lease->clid_len,
1146 lease->hwaddr, lease->hwaddr_len, lease->hwaddr_type, lease->hostname, tagif_netid)))
1147 {
1148 struct dhcp_netid_list *list;
1149
1150 for (list = config->netid; list; list = list->next)
1151 {
1152 list->list->next = netid;
1153 netid = list->list;
1154 }
1155
1156 tagif_netid = run_tag_if(netid);
1157 }
1158
1159 if (context->netid.net)
1160 {
1161 context->netid.next = netid;
1162 tagif_netid = run_tag_if(&context->netid);
1163 }
1164
1165 log_tags(tagif_netid, ntohl(mess->xid));
1166 emac = extended_hwaddr(lease->hwaddr_type, lease->hwaddr_len, lease->hwaddr, lease->clid_len, lease->clid, &emac_len);
1167 mess_type = DHCPLEASEACTIVE;
1168 daemon->metrics[METRIC_DHCPLEASEACTIVE]++;
1169 }
1170 }
1171
1172 log_packet(mess_type == DHCPLEASEACTIVE ? "DHCPLEASEACTIVE" : (mess_type == DHCPLEASEUNASSIGNED ? "DHCPLEASEUNASSIGNED" : "DHCPLEASEUNKNOWN"),
1173 mess_type == DHCPLEASEACTIVE ? &lease->addr : (mess->ciaddr.s_addr != 0 ? &mess->ciaddr : NULL),
1174 emac_len != 0 ? emac : NULL, emac_len,
1175 iface_name, mess_type == DHCPLEASEACTIVE ? lease->hostname : NULL, NULL, mess->xid);
1176
1177 clear_packet(mess, end);
1178 option_put(mess, end, OPTION_MESSAGE_TYPE, 1, mess_type);
1179
1180 if (mess_type == DHCPLEASEUNKNOWN)
1181 {
1182 daemon->metrics[METRIC_DHCPLEASEUNKNOWN]++;
1183 mess->ciaddr.s_addr = 0;
1184 }
1185
1186 if (mess_type == DHCPLEASEACTIVE)
1187 {
1188 unsigned char *p;
1189
1190 mess->ciaddr = lease->addr;
1191 mess->hlen = lease->hwaddr_len;
1192 mess->htype = lease->hwaddr_type;
1193 memcpy(mess->chaddr, lease->hwaddr, lease->hwaddr_len);
1194
1195 if (lease->clid && in_list(req_options, OPTION_CLIENT_ID) &&
1196 (p = free_space(mess, end, OPTION_CLIENT_ID, lease->clid_len)))
1197 memcpy(p, lease->clid, lease->clid_len);
1198
1199 if (in_list(req_options, OPTION_LEASE_TIME))
1200 {
1201 if (lease->expires == 0) /* infinite lease */
1202 option_put(mess, end, OPTION_LEASE_TIME, 4, 0xffffffff);
1203 else
1204 option_put(mess, end, OPTION_LEASE_TIME, 4, (unsigned int)(lease->expires - now));
1205 }
1206
1207 if (lease->expires != 0)
1208 {
1209 time = calc_time(context, config, NULL);
1210
1211 if (in_list(req_options, OPTION_T1) && (lease->expires - now) > time/2)
1212 option_put(mess, end, OPTION_T1, 4, ((unsigned int)(lease->expires - now)) - time/2);
1213 if (in_list(req_options, OPTION_T2) && (lease->expires - now) > time/8)
1214 option_put(mess, end, OPTION_T2, 4, ((unsigned int)(lease->expires - now)) - time/8);
1215 if (in_list(req_options, OPTION_LAST_TRANSACTION) && (lease->expires - now) < time)
1216 option_put(mess, end, OPTION_LAST_TRANSACTION, 4, time - ((unsigned int)(lease->expires - now)));
1217 }
1218
1219 if (lease->vendorclass)
1220 {
1221 memcpy(daemon->dhcp_buff3, lease->vendorclass, lease->vendorclass_len);
1222 vendor_class_len = lease->vendorclass_len;
1223 }
1224
1225 subnet_addr.s_addr = 0;
1226 do_options(context, mess, end, req_options, lease->hostname, get_domain(lease->addr), netid, subnet_addr,
1227 0, 0, -1, NULL, vendor_class_len, now, 0xffffffff, 0, NULL, 1);
1228
1229 /* Does this have to be last for leasequery replies also? RFC 4388 is silent on the subject. */
1230 if (lease->agent_id && in_list(req_options, OPTION_AGENT_ID) &&
1231 (p = free_space(mess, end, OPTION_AGENT_ID, lease->agent_id_len)))
1232 memcpy(p, lease->agent_id, lease->agent_id_len);
1233 }
1234
1235 return dhcp_packet_size(mess, NULL, real_end);
1236
1237 case DHCPDECLINE:
1238 if (!(opt = option_find(mess, sz, OPTION_SERVER_IDENTIFIER, INADDRSZ)) ||
1239 option_addr(opt).s_addr != server_id(context, override, fallback).s_addr)
1240 return 0;
1241
1242 /* sanitise any message. Paranoid? Moi? */
1243 sanitise(option_find(mess, sz, OPTION_MESSAGE, 1), daemon->dhcp_buff);
1244
1245 if (!(opt = option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ)))
1246 return 0;
1247
1248 daemon->metrics[METRIC_DHCPDECLINE]++;
1249 log_packet("DHCPDECLINE", option_ptr(opt, 0), emac, emac_len, iface_name, NULL, daemon->dhcp_buff, mess->xid);
1250
1251 if (lease && lease->addr.s_addr == option_addr(opt).s_addr)
1252 lease_prune(lease, now);
1253
1254 if (have_config(config, CONFIG_ADDR) &&
1255 config->addr.s_addr == option_addr(opt).s_addr)
1256 {
1257 prettyprint_time(daemon->dhcp_buff, DECLINE_BACKOFF);
1258 inet_ntop(AF_INET, &config->addr, daemon->addrbuff, ADDRSTRLEN);
1259 my_syslog(MS_DHCP | LOG_WARNING, _("disabling DHCP static address %s for %s"),
1260 daemon->addrbuff, daemon->dhcp_buff);
1261 config->flags |= CONFIG_DECLINED;
1262 config->decline_time = now;
1263 }
1264 else
1265 /* make sure this host gets a different address next time. */
1266 for (; context; context = context->current)
1267 context->addr_epoch++;
1268
1269 return 0;
1270
1271 case DHCPRELEASE:
1272 if (!(context = narrow_context(context, mess->ciaddr, tagif_netid)) ||
1273 !(opt = option_find(mess, sz, OPTION_SERVER_IDENTIFIER, INADDRSZ)) ||
1274 option_addr(opt).s_addr != server_id(context, override, fallback).s_addr)
1275 return 0;
1276
1277 if (lease && lease->addr.s_addr == mess->ciaddr.s_addr)
1278 lease_prune(lease, now);
1279 else
1280 message = _("unknown lease");
1281
1282 daemon->metrics[METRIC_DHCPRELEASE]++;
1283 log_packet("DHCPRELEASE", &mess->ciaddr, emac, emac_len, iface_name, NULL, message, mess->xid);
1284
1285 return 0;
1286
1287 case DHCPDISCOVER:
1288 if (ignore || have_config(config, CONFIG_DISABLE))
1289 {
1290 if (option_bool(OPT_QUIET_DHCP))
1291 return 0;
1292 message = _("ignored");
1293 opt = NULL;
1294 }
1295 else
1296 {
1297 struct in_addr addr, conf;
1298
1299 addr.s_addr = conf.s_addr = 0;
1300
1301 if ((opt = option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ)))
1302 addr = option_addr(opt);
1303
1304 if (have_config(config, CONFIG_ADDR))
1305 {
1306 inet_ntop(AF_INET, &config->addr, daemon->addrbuff, ADDRSTRLEN);
1307
1308 if ((ltmp = lease_find_by_addr(config->addr)) &&
1309 ltmp != lease &&
1310 !config_has_mac(config, ltmp->hwaddr, ltmp->hwaddr_len, ltmp->hwaddr_type))
1311 {
1312 int len;
1313 unsigned char *mac = extended_hwaddr(ltmp->hwaddr_type, ltmp->hwaddr_len,
1314 ltmp->hwaddr, ltmp->clid_len, ltmp->clid, &len);
1315 my_syslog(MS_DHCP | LOG_WARNING, _("not using configured address %s because it is leased to %s"),
1316 daemon->addrbuff, print_mac(mac, len));
1317 }
1318 else
1319 {
1320 struct dhcp_context *tmp;
1321 for (tmp = context; tmp; tmp = tmp->current)
1322 if (context->router.s_addr == config->addr.s_addr)
1323 break;
1324 if (tmp)
1325 my_syslog(MS_DHCP | LOG_WARNING, _("not using configured address %s because it is in use by the server or relay"), daemon->addrbuff);
1326 else if (have_config(config, CONFIG_DECLINED) &&
1327 difftime(now, config->decline_time) < (float)DECLINE_BACKOFF)
1328 my_syslog(MS_DHCP | LOG_WARNING, _("not using configured address %s because it was previously declined"), daemon->addrbuff);
1329 else
1330 conf = config->addr;
1331 }
1332 }
1333
1334 if (conf.s_addr)
1335 mess->yiaddr = conf;
1336 else if (lease &&
1337 address_available(context, lease->addr, tagif_netid) &&
1338 !config_find_by_address(daemon->dhcp_conf, lease->addr))
1339 mess->yiaddr = lease->addr;
1340 else if (opt && address_available(context, addr, tagif_netid) && !lease_find_by_addr(addr) &&
1341 !config_find_by_address(daemon->dhcp_conf, addr) && do_icmp_ping(now, addr, 0, loopback))
1342 mess->yiaddr = addr;
1343 else if (emac_len == 0)
1344 message = _("no unique-id");
1345 else if (!address_allocate(context, &mess->yiaddr, emac, emac_len, tagif_netid, now, loopback))
1346 message = _("no address available");
1347 }
1348
1349 daemon->metrics[METRIC_DHCPDISCOVER]++;
1350 log_packet("DHCPDISCOVER", opt ? option_ptr(opt, 0) : NULL, emac, emac_len, iface_name, NULL, message, mess->xid);
1351
1352 if (message || !(context = narrow_context(context, mess->yiaddr, tagif_netid)))
1353 return 0;
1354
1355 if (context->netid.net)
1356 {
1357 context->netid.next = netid;
1358 tagif_netid = run_tag_if(&context->netid);
1359 }
1360
1361 apply_delay(mess->xid, recvtime, tagif_netid);
1362
1363 if (option_bool(OPT_RAPID_COMMIT) && option_find(mess, sz, OPTION_RAPID_COMMIT, 0))
1364 {
1365 rapid_commit = 1;
1366 /* If a lease exists for this host and another address, squash it. */
1367 if (lease && lease->addr.s_addr != mess->yiaddr.s_addr)
1368 {
1369 lease_prune(lease, now);
1370 lease = NULL;
1371 }
1372 goto rapid_commit;
1373 }
1374
1375 log_tags(tagif_netid, ntohl(mess->xid));
1376
1377 daemon->metrics[METRIC_DHCPOFFER]++;
1378 log_packet("DHCPOFFER" , &mess->yiaddr, emac, emac_len, iface_name, NULL, NULL, mess->xid);
1379
1380 time = calc_time(context, config, option_find(mess, sz, OPTION_LEASE_TIME, 4));
1381 clear_packet(mess, end);
1382 option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPOFFER);
1383 option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, ntohl(server_id(context, override, fallback).s_addr));
1384 option_put(mess, end, OPTION_LEASE_TIME, 4, time);
1385 /* T1 and T2 are required in DHCPOFFER by HP's wacky Jetdirect client. */
1386 do_options(context, mess, end, req_options, offer_hostname, get_domain(mess->yiaddr),
1387 netid, subnet_addr, fqdn_flags, borken_opt, pxearch, uuid, vendor_class_len, now, time, fuzz, pxevendor, 0);
1388
1389 return dhcp_packet_size(mess, agent_id, real_end);
1390
1391
1392 case DHCPREQUEST:
1393 if (ignore || have_config(config, CONFIG_DISABLE))
1394 return 0;
1395 if ((opt = option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ)))
1396 {
1397 /* SELECTING or INIT_REBOOT */
1398 mess->yiaddr = option_addr(opt);
1399
1400 /* send vendor and user class info for new or recreated lease */
1401 do_classes = 1;
1402
1403 if ((opt = option_find(mess, sz, OPTION_SERVER_IDENTIFIER, INADDRSZ)))
1404 {
1405 /* SELECTING */
1406 selecting = 1;
1407
1408 if (override.s_addr != 0)
1409 {
1410 if (option_addr(opt).s_addr != override.s_addr)
1411 return 0;
1412 }
1413 else
1414 {
1415 for (; context; context = context->current)
1416 if (context->local.s_addr == option_addr(opt).s_addr)
1417 break;
1418
1419 if (!context)
1420 {
1421 /* Handle very strange configs where clients have more than one route to the server.
1422 If a clients idea of its server-id matches any of our DHCP interfaces, we let it pass.
1423 Have to set override to make sure we echo back the correct server-id */
1424 struct irec *intr;
1425
1426 enumerate_interfaces(0);
1427
1428 for (intr = daemon->interfaces; intr; intr = intr->next)
1429 if (intr->addr.sa.sa_family == AF_INET &&
1430 intr->addr.in.sin_addr.s_addr == option_addr(opt).s_addr &&
1431 intr->tftp_ok)
1432 break;
1433
1434 if (intr)
1435 override = intr->addr.in.sin_addr;
1436 else
1437 {
1438 /* In auth mode, a REQUEST sent to the wrong server
1439 should be faulted, so that the client establishes
1440 communication with us, otherwise, silently ignore. */
1441 if (!option_bool(OPT_AUTHORITATIVE))
1442 return 0;
1443 message = _("wrong server-ID");
1444 }
1445 }
1446 }
1447
1448 /* If a lease exists for this host and another address, squash it. */
1449 if (lease && lease->addr.s_addr != mess->yiaddr.s_addr)
1450 {
1451 lease_prune(lease, now);
1452 lease = NULL;
1453 }
1454 }
1455 else
1456 {
1457 /* INIT-REBOOT */
1458 if (!lease && !option_bool(OPT_AUTHORITATIVE))
1459 return 0;
1460
1461 if (lease && lease->addr.s_addr != mess->yiaddr.s_addr)
1462 message = _("wrong address");
1463 }
1464 }
1465 else
1466 {
1467 /* RENEWING or REBINDING */
1468 /* Check existing lease for this address.
1469 We allow it to be missing if dhcp-authoritative mode
1470 as long as we can allocate the lease now - checked below.
1471 This makes for a smooth recovery from a lost lease DB */
1472 if ((lease && mess->ciaddr.s_addr != lease->addr.s_addr) ||
1473 (!lease && !option_bool(OPT_AUTHORITATIVE)))
1474 {
1475 /* A client rebinding will broadcast the request, so we may see it even
1476 if the lease is held by another server. Just ignore it in that case.
1477 If the request is unicast to us, then somethings wrong, NAK */
1478 if (!unicast_dest)
1479 return 0;
1480 message = _("lease not found");
1481 /* ensure we broadcast NAK */
1482 unicast_dest = 0;
1483 }
1484
1485 /* desynchronise renewals */
1486 fuzz = rand16();
1487 mess->yiaddr = mess->ciaddr;
1488 }
1489
1490 daemon->metrics[METRIC_DHCPREQUEST]++;
1491 log_packet("DHCPREQUEST", &mess->yiaddr, emac, emac_len, iface_name, NULL, NULL, mess->xid);
1492
1493 rapid_commit:
1494 if (!message)
1495 {
1496 struct dhcp_config *addr_config;
1497 struct dhcp_context *tmp = NULL;
1498
1499 if (have_config(config, CONFIG_ADDR))
1500 for (tmp = context; tmp; tmp = tmp->current)
1501 if (context->router.s_addr == config->addr.s_addr)
1502 break;
1503
1504 if (!(context = narrow_context(context, mess->yiaddr, tagif_netid)))
1505 {
1506 /* If a machine moves networks whilst it has a lease, we catch that here. */
1507 message = _("wrong network");
1508 /* ensure we broadcast NAK */
1509 unicast_dest = 0;
1510 }
1511
1512 /* Check for renewal of a lease which is outside the allowed range. */
1513 else if (!address_available(context, mess->yiaddr, tagif_netid) &&
1514 (!have_config(config, CONFIG_ADDR) || config->addr.s_addr != mess->yiaddr.s_addr))
1515 message = _("address not available");
1516
1517 /* Check if a new static address has been configured. Be very sure that
1518 when the client does DISCOVER, it will get the static address, otherwise
1519 an endless protocol loop will ensue. */
1520 else if (!tmp && !selecting &&
1521 have_config(config, CONFIG_ADDR) &&
1522 (!have_config(config, CONFIG_DECLINED) ||
1523 difftime(now, config->decline_time) > (float)DECLINE_BACKOFF) &&
1524 config->addr.s_addr != mess->yiaddr.s_addr &&
1525 (!(ltmp = lease_find_by_addr(config->addr)) || ltmp == lease))
1526 message = _("static lease available");
1527
1528 /* Check to see if the address is reserved as a static address for another host */
1529 else if ((addr_config = config_find_by_address(daemon->dhcp_conf, mess->yiaddr)) && addr_config != config)
1530 message = _("address reserved");
1531
1532 else if (!lease && (ltmp = lease_find_by_addr(mess->yiaddr)))
1533 {
1534 /* If a host is configured with more than one MAC address, it's OK to 'nix
1535 a lease from one of its MACs to give the address to another. */
1536 if (config && config_has_mac(config, ltmp->hwaddr, ltmp->hwaddr_len, ltmp->hwaddr_type))
1537 {
1538 inet_ntop(AF_INET, &ltmp->addr, daemon->addrbuff, ADDRSTRLEN);
1539 my_syslog(MS_DHCP | LOG_INFO, _("abandoning lease to %s of %s"),
1540 print_mac(ltmp->hwaddr, ltmp->hwaddr_len),
1541 daemon->addrbuff);
1542 lease = ltmp;
1543 }
1544 else
1545 message = _("address in use");
1546 }
1547
1548 if (!message)
1549 {
1550 if (emac_len == 0)
1551 message = _("no unique-id");
1552
1553 else if (!lease)
1554 {
1555 if ((lease = lease4_allocate(mess->yiaddr)))
1556 do_classes = 1;
1557 else
1558 message = _("no leases left");
1559 }
1560 }
1561 }
1562
1563 if (message)
1564 {
1565 daemon->metrics[rapid_commit ? METRIC_NOANSWER : METRIC_DHCPNAK]++;
1566 log_packet(rapid_commit ? "NOANSWER" : "DHCPNAK", &mess->yiaddr, emac, emac_len, iface_name, NULL, message, mess->xid);
1567
1568 /* rapid commit case: lease allocate failed but don't send DHCPNAK */
1569 if (rapid_commit)
1570 return 0;
1571
1572 mess->yiaddr.s_addr = 0;
1573 clear_packet(mess, end);
1574 option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPNAK);
1575 option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, ntohl(server_id(context, override, fallback).s_addr));
1576 option_put_string(mess, end, OPTION_MESSAGE, message, borken_opt);
1577 /* This fixes a problem with the DHCP spec, broadcasting a NAK to a host on
1578 a distant subnet which unicast a REQ to us won't work. */
1579 if (!unicast_dest || mess->giaddr.s_addr != 0 ||
1580 mess->ciaddr.s_addr == 0 || is_same_net(context->local, mess->ciaddr, context->netmask))
1581 {
1582 mess->flags |= htons(0x8000); /* broadcast */
1583 mess->ciaddr.s_addr = 0;
1584 }
1585 }
1586 else
1587 {
1588 if (context->netid.net)
1589 {
1590 context->netid.next = netid;
1591 tagif_netid = run_tag_if( &context->netid);
1592 }
1593
1594 log_tags(tagif_netid, ntohl(mess->xid));
1595
1596 if (do_classes)
1597 {
1598 /* pick up INIT-REBOOT events. */
1599 lease->flags |= LEASE_CHANGED;
1600
1601 #ifdef HAVE_SCRIPT
1602 if (daemon->lease_change_command)
1603 {
1604 struct dhcp_netid *n;
1605
1606 if (mess->giaddr.s_addr)
1607 lease->giaddr = mess->giaddr;
1608
1609 free(lease->extradata);
1610 lease->extradata = NULL;
1611 lease->extradata_size = lease->extradata_len = 0;
1612
1613 add_extradata_opt(lease, option_find(mess, sz, OPTION_VENDOR_ID, 1));
1614 add_extradata_opt(lease, option_find(mess, sz, OPTION_HOSTNAME, 1));
1615 add_extradata_opt(lease, oui);
1616 add_extradata_opt(lease, serial);
1617 add_extradata_opt(lease, class);
1618
1619 if ((opt = option_find(mess, sz, OPTION_AGENT_ID, 1)))
1620 {
1621 add_extradata_opt(lease, option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_CIRCUIT_ID, 1));
1622 add_extradata_opt(lease, option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_SUBSCR_ID, 1));
1623 add_extradata_opt(lease, option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_REMOTE_ID, 1));
1624 }
1625 else
1626 {
1627 add_extradata_opt(lease, NULL);
1628 add_extradata_opt(lease, NULL);
1629 add_extradata_opt(lease, NULL);
1630 }
1631
1632 /* DNSMASQ_REQUESTED_OPTIONS */
1633 if ((opt = option_find(mess, sz, OPTION_REQUESTED_OPTIONS, 1)))
1634 {
1635 int i, len = option_len(opt);
1636 unsigned char *rop = option_ptr(opt, 0);
1637
1638 for (i = 0; i < len; i++)
1639 lease_add_extradata(lease, (unsigned char *)daemon->namebuff,
1640 sprintf(daemon->namebuff, "%u", rop[i]), (i + 1) == len ? 0 : ',');
1641 }
1642 else
1643 lease_add_extradata(lease, NULL, 0, 0);
1644
1645 add_extradata_opt(lease, option_find(mess, sz, OPTION_MUD_URL_V4, 1));
1646
1647 /* space-concat tag set */
1648 if (!tagif_netid)
1649 add_extradata_opt(lease, NULL);
1650 else
1651 for (n = tagif_netid; n; n = n->next)
1652 {
1653 struct dhcp_netid *n1;
1654 /* kill dupes */
1655 for (n1 = n->next; n1; n1 = n1->next)
1656 if (strcmp(n->net, n1->net) == 0)
1657 break;
1658 if (!n1)
1659 lease_add_extradata(lease, (unsigned char *)n->net, strlen(n->net), n->next ? ' ' : 0);
1660 }
1661
1662 if ((opt = option_find(mess, sz, OPTION_USER_CLASS, 1)))
1663 {
1664 int len = option_len(opt);
1665 unsigned char *ucp = option_ptr(opt, 0);
1666 /* If the user-class option started as counted strings, the first byte will be zero. */
1667 if (len != 0 && ucp[0] == 0)
1668 ucp++, len--;
1669 lease_add_extradata(lease, ucp, len, -1);
1670 }
1671 }
1672 #endif
1673 }
1674
1675 if (!hostname_auth && (client_hostname = host_from_dns(mess->yiaddr)))
1676 {
1677 domain = get_domain(mess->yiaddr);
1678 hostname = client_hostname;
1679 hostname_auth = 1;
1680 }
1681
1682 time = calc_time(context, config, option_find(mess, sz, OPTION_LEASE_TIME, 4));
1683 lease_set_hwaddr(lease, mess->chaddr, clid, mess->hlen, mess->htype, clid_len, now, do_classes);
1684
1685 /* if all the netids in the ignore_name list are present, ignore client-supplied name */
1686 if (!hostname_auth)
1687 {
1688 for (id_list = daemon->dhcp_ignore_names; id_list; id_list = id_list->next)
1689 if ((!id_list->list) || match_netid(id_list->list, tagif_netid, 0))
1690 break;
1691 if (id_list)
1692 hostname = NULL;
1693 }
1694
1695 /* Last ditch, if configured, generate hostname from mac address */
1696 if (!hostname && emac_len != 0)
1697 {
1698 for (id_list = daemon->dhcp_gen_names; id_list; id_list = id_list->next)
1699 if ((!id_list->list) || match_netid(id_list->list, tagif_netid, 0))
1700 break;
1701 if (id_list)
1702 {
1703 int i;
1704
1705 hostname = daemon->dhcp_buff;
1706 /* buffer is 256 bytes, 3 bytes per octet */
1707 for (i = 0; (i < emac_len) && (i < 80); i++)
1708 hostname += sprintf(hostname, "%.2x%s", emac[i], (i == emac_len - 1) ? "" : "-");
1709 hostname = daemon->dhcp_buff;
1710 }
1711 }
1712
1713 if (hostname)
1714 lease_set_hostname(lease, hostname, hostname_auth, get_domain(lease->addr), domain);
1715
1716 lease_set_expires(lease, time, now);
1717 lease_set_interface(lease, int_index, now);
1718
1719 if (option_bool(OPT_LEASEQUERY))
1720 {
1721 if (agent_id)
1722 lease_set_agent_id(lease, option_ptr(agent_id, 0), option_len(agent_id));
1723 if (vendor_class_len != 0)
1724 lease_set_vendorclass(lease, (unsigned char *)daemon->dhcp_buff3, vendor_class_len);
1725 }
1726 else
1727 {
1728 /* if leasequery no longer enabled, remove stuff that may have been stored when it was. */
1729 lease_set_agent_id(lease, NULL, 0);
1730 lease_set_vendorclass(lease, NULL, 0);
1731 }
1732
1733 if (override.s_addr != 0)
1734 lease->override = override;
1735 else
1736 override = lease->override;
1737
1738 daemon->metrics[METRIC_DHCPACK]++;
1739 log_packet("DHCPACK", &mess->yiaddr, emac, emac_len, iface_name, hostname, NULL, mess->xid);
1740
1741 clear_packet(mess, end);
1742 option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPACK);
1743 option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, ntohl(server_id(context, override, fallback).s_addr));
1744 option_put(mess, end, OPTION_LEASE_TIME, 4, time);
1745 if (rapid_commit)
1746 option_put(mess, end, OPTION_RAPID_COMMIT, 0, 0);
1747 do_options(context, mess, end, req_options, hostname, get_domain(mess->yiaddr),
1748 netid, subnet_addr, fqdn_flags, borken_opt, pxearch, uuid, vendor_class_len, now, time, fuzz, pxevendor, 0);
1749 }
1750
1751 return dhcp_packet_size(mess, agent_id, real_end);
1752
1753 case DHCPINFORM:
1754 if (ignore || have_config(config, CONFIG_DISABLE))
1755 message = _("ignored");
1756
1757 daemon->metrics[METRIC_DHCPINFORM]++;
1758 log_packet("DHCPINFORM", &mess->ciaddr, emac, emac_len, iface_name, message, NULL, mess->xid);
1759
1760 if (message || mess->ciaddr.s_addr == 0)
1761 return 0;
1762
1763 /* For DHCPINFORM only, cope without a valid context */
1764 context = narrow_context(context, mess->ciaddr, tagif_netid);
1765
1766 /* Find a least based on IP address if we didn't
1767 get one from MAC address/client-d */
1768 if (!lease &&
1769 (lease = lease_find_by_addr(mess->ciaddr)) &&
1770 lease->hostname)
1771 hostname = lease->hostname;
1772
1773 if (!hostname)
1774 hostname = host_from_dns(mess->ciaddr);
1775
1776 if (context && context->netid.net)
1777 {
1778 context->netid.next = netid;
1779 tagif_netid = run_tag_if(&context->netid);
1780 }
1781
1782 log_tags(tagif_netid, ntohl(mess->xid));
1783
1784 daemon->metrics[METRIC_DHCPACK]++;
1785 log_packet("DHCPACK", &mess->ciaddr, emac, emac_len, iface_name, hostname, NULL, mess->xid);
1786
1787 if (lease)
1788 {
1789 lease_set_interface(lease, int_index, now);
1790 if (override.s_addr != 0)
1791 lease->override = override;
1792 else
1793 override = lease->override;
1794 }
1795
1796 clear_packet(mess, end);
1797 option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPACK);
1798 option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, ntohl(server_id(context, override, fallback).s_addr));
1799
1800 /* RFC 2131 says that DHCPINFORM shouldn't include lease-time parameters, but
1801 we supply a utility which makes DHCPINFORM requests to get this information.
1802 Only include lease time if OPTION_LEASE_TIME is in the parameter request list,
1803 which won't be true for ordinary clients, but will be true for the
1804 dhcp_lease_time utility. */
1805 if (lease && in_list(req_options, OPTION_LEASE_TIME))
1806 {
1807 if (lease->expires == 0)
1808 time = 0xffffffff;
1809 else
1810 time = (unsigned int)difftime(lease->expires, now);
1811 option_put(mess, end, OPTION_LEASE_TIME, 4, time);
1812 }
1813
1814 do_options(context, mess, end, req_options, hostname, get_domain(mess->ciaddr),
1815 netid, subnet_addr, fqdn_flags, borken_opt, pxearch, uuid, vendor_class_len, now, 0xffffffff, 0, pxevendor, 0);
1816
1817 *is_inform = 1; /* handle reply differently */
1818 return dhcp_packet_size(mess, agent_id, real_end);
1819 }
1820
1821 return 0;
1822 }
1823
1824 /* find a good value to use as MAC address for logging and address-allocation hashing.
1825 This is normally just the chaddr field from the DHCP packet,
1826 but eg Firewire will have hlen == 0 and use the client-id instead.
1827 This could be anything, but will normally be EUI64 for Firewire.
1828 We assume that if the first byte of the client-id equals the htype byte
1829 then the client-id is using the usual encoding and use the rest of the
1830 client-id: if not we can use the whole client-id. This should give
1831 sane MAC address logs. */
1832 unsigned char *extended_hwaddr(int hwtype, int hwlen, unsigned char *hwaddr,
1833 int clid_len, unsigned char *clid, int *len_out)
1834 {
1835 if (hwlen == 0 && clid && clid_len > 3)
1836 {
1837 if (clid[0] == hwtype)
1838 {
1839 *len_out = clid_len - 1 ;
1840 return clid + 1;
1841 }
1842
1843 #if defined(ARPHRD_EUI64) && defined(ARPHRD_IEEE1394)
1844 if (clid[0] == ARPHRD_EUI64 && hwtype == ARPHRD_IEEE1394)
1845 {
1846 *len_out = clid_len - 1 ;
1847 return clid + 1;
1848 }
1849 #endif
1850
1851 *len_out = clid_len;
1852 return clid;
1853 }
1854
1855 *len_out = hwlen;
1856 return hwaddr;
1857 }
1858
1859 static unsigned int calc_time(struct dhcp_context *context, struct dhcp_config *config, unsigned char *opt)
1860 {
1861 unsigned int time = have_config(config, CONFIG_TIME) ? config->lease_time : context->lease_time;
1862
1863 if (opt)
1864 {
1865 unsigned int req_time = option_uint(opt, 0, 4);
1866 if (req_time < 120 )
1867 req_time = 120; /* sanity */
1868 if (time == 0xffffffff || (req_time != 0xffffffff && req_time < time))
1869 time = req_time;
1870 }
1871
1872 return time;
1873 }
1874
1875 static struct in_addr server_id(struct dhcp_context *context, struct in_addr override, struct in_addr fallback)
1876 {
1877 if (override.s_addr != 0)
1878 return override;
1879 else if (context && context->local.s_addr != 0)
1880 return context->local;
1881 else
1882 return fallback;
1883 }
1884
1885 static int sanitise(unsigned char *opt, char *buf)
1886 {
1887 char *p;
1888 int i;
1889
1890 *buf = 0;
1891
1892 if (!opt)
1893 return 0;
1894
1895 p = option_ptr(opt, 0);
1896
1897 for (i = option_len(opt); i > 0; i--)
1898 {
1899 char c = *p++;
1900 if (isprint((unsigned char)c))
1901 *buf++ = c;
1902 }
1903 *buf = 0; /* add terminator */
1904
1905 return 1;
1906 }
1907
1908 #ifdef HAVE_SCRIPT
1909 static void add_extradata_opt(struct dhcp_lease *lease, unsigned char *opt)
1910 {
1911 if (!opt)
1912 lease_add_extradata(lease, NULL, 0, 0);
1913 else
1914 lease_add_extradata(lease, option_ptr(opt, 0), option_len(opt), 0);
1915 }
1916 #endif
1917
1918 static void log_packet(char *type, void *addr, unsigned char *ext_mac,
1919 int mac_len, char *interface, char *string, char *err, u32 xid)
1920 {
1921 char *mac = "";
1922
1923 if (!err && !option_bool(OPT_LOG_OPTS) && option_bool(OPT_QUIET_DHCP))
1924 return;
1925
1926 daemon->addrbuff[0] = 0;
1927
1928 if (addr)
1929 inet_ntop(AF_INET, addr, daemon->addrbuff, ADDRSTRLEN);
1930
1931 if (ext_mac)
1932 mac = print_mac(ext_mac, mac_len);
1933
1934 if (option_bool(OPT_LOG_OPTS))
1935 my_syslog(MS_DHCP | LOG_INFO, "%u %s(%s) %s%s%s%s%s%s",
1936 ntohl(xid),
1937 type,
1938 interface,
1939 daemon->addrbuff,
1940 addr ? " " : "",
1941 mac,
1942 ext_mac ? " " : "",
1943 string ? string : "",
1944 err ? err : "");
1945 else
1946 my_syslog(MS_DHCP | LOG_INFO, "%s(%s) %s%s%s%s%s%s",
1947 type,
1948 interface,
1949 daemon->addrbuff,
1950 addr ? " " : "",
1951 mac,
1952 ext_mac ? " " : "",
1953 string ? string : "",
1954 err ? err : "");
1955
1956 #ifdef HAVE_UBUS
1957 if (!strcmp(type, "DHCPACK"))
1958 ubus_event_bcast("dhcp.ack", mac, addr ? daemon->addrbuff : NULL, string, interface);
1959 else if (!strcmp(type, "DHCPRELEASE"))
1960 ubus_event_bcast("dhcp.release", mac, addr ? daemon->addrbuff : NULL, string, interface);
1961 #endif
1962 }
1963
1964 static void log_options(unsigned char *start, u32 xid)
1965 {
1966 while (*start != OPTION_END)
1967 {
1968 char *optname = option_string(AF_INET, start[0], option_ptr(start, 0), option_len(start), daemon->namebuff, MAXDNAMESTR);
1969
1970 my_syslog(MS_DHCP | LOG_INFO, "%u sent size:%3d option:%3d %s %s",
1971 ntohl(xid), option_len(start), start[0], optname, daemon->namebuff);
1972 start += start[1] + 2;
1973 }
1974 }
1975
1976 static unsigned char *option_find1(unsigned char *p, unsigned char *end, int opt, int minsize)
1977 {
1978 while (1)
1979 {
1980 if (p >= end)
1981 return NULL;
1982 else if (*p == OPTION_END)
1983 return opt == OPTION_END ? p : NULL;
1984 else if (*p == OPTION_PAD)
1985 p++;
1986 else
1987 {
1988 int opt_len;
1989 if (p > end - 2)
1990 return NULL; /* malformed packet */
1991 opt_len = option_len(p);
1992 if (p > end - (2 + opt_len))
1993 return NULL; /* malformed packet */
1994 if (*p == opt && opt_len >= minsize)
1995 return p;
1996 p += opt_len + 2;
1997 }
1998 }
1999 }
2000
2001 static unsigned char *option_find(struct dhcp_packet *mess, size_t size, int opt_type, int minsize)
2002 {
2003 unsigned char *ret, *overload;
2004
2005 /* skip over DHCP cookie; */
2006 if ((ret = option_find1(&mess->options[0] + sizeof(u32), ((unsigned char *)mess) + size, opt_type, minsize)))
2007 return ret;
2008
2009 /* look for overload option. */
2010 if (!(overload = option_find1(&mess->options[0] + sizeof(u32), ((unsigned char *)mess) + size, OPTION_OVERLOAD, 1)))
2011 return NULL;
2012
2013 /* Can we look in filename area ? */
2014 if ((overload[2] & 1) &&
2015 (ret = option_find1(&mess->file[0], &mess->file[128], opt_type, minsize)))
2016 return ret;
2017
2018 /* finally try sname area */
2019 if ((overload[2] & 2) &&
2020 (ret = option_find1(&mess->sname[0], &mess->sname[64], opt_type, minsize)))
2021 return ret;
2022
2023 return NULL;
2024 }
2025
2026 static struct in_addr option_addr(unsigned char *opt)
2027 {
2028 /* this worries about unaligned data in the option. */
2029 /* struct in_addr is network byte order */
2030 struct in_addr ret;
2031
2032 memcpy(&ret, option_ptr(opt, 0), INADDRSZ);
2033
2034 return ret;
2035 }
2036
2037 static unsigned int option_uint(unsigned char *opt, int offset, int size)
2038 {
2039 /* this worries about unaligned data and byte order */
2040 unsigned int ret = 0;
2041 int i;
2042 unsigned char *p = option_ptr(opt, offset);
2043
2044 for (i = 0; i < size; i++)
2045 ret = (ret << 8) | *p++;
2046
2047 return ret;
2048 }
2049
2050 static unsigned char *dhcp_skip_opts(unsigned char *start)
2051 {
2052 while (*start != 0)
2053 start += start[1] + 2;
2054 return start;
2055 }
2056
2057 /* only for use when building packet: doesn't check for bad data. */
2058 static unsigned char *find_overload(struct dhcp_packet *mess)
2059 {
2060 unsigned char *p = &mess->options[0] + sizeof(u32);
2061
2062 while (*p != 0)
2063 {
2064 if (*p == OPTION_OVERLOAD)
2065 return p;
2066 p += p[1] + 2;
2067 }
2068 return NULL;
2069 }
2070
2071 static size_t dhcp_packet_size(struct dhcp_packet *mess, unsigned char *agent_id, unsigned char *real_end)
2072 {
2073 unsigned char *p = dhcp_skip_opts(&mess->options[0] + sizeof(u32));
2074 unsigned char *overload;
2075 size_t ret;
2076
2077 /* move agent_id back down to the end of the packet */
2078 if (agent_id)
2079 {
2080 memmove(p, agent_id, real_end - agent_id);
2081 p += real_end - agent_id;
2082 memset(p, 0, real_end - p); /* in case of overlap */
2083 }
2084
2085 /* add END options to the regions. */
2086 overload = find_overload(mess);
2087
2088 if (overload && (option_uint(overload, 0, 1) & 1))
2089 {
2090 *dhcp_skip_opts(mess->file) = OPTION_END;
2091 if (option_bool(OPT_LOG_OPTS))
2092 log_options(mess->file, mess->xid);
2093 }
2094 else if (option_bool(OPT_LOG_OPTS) && strlen((char *)mess->file) != 0)
2095 my_syslog(MS_DHCP | LOG_INFO, _("%u bootfile name: %s"), ntohl(mess->xid), (char *)mess->file);
2096
2097 if (overload && (option_uint(overload, 0, 1) & 2))
2098 {
2099 *dhcp_skip_opts(mess->sname) = OPTION_END;
2100 if (option_bool(OPT_LOG_OPTS))
2101 log_options(mess->sname, mess->xid);
2102 }
2103 else if (option_bool(OPT_LOG_OPTS) && strlen((char *)mess->sname) != 0)
2104 my_syslog(MS_DHCP | LOG_INFO, _("%u server name: %s"), ntohl(mess->xid), (char *)mess->sname);
2105
2106
2107 *p++ = OPTION_END;
2108
2109 if (option_bool(OPT_LOG_OPTS))
2110 {
2111 if (mess->siaddr.s_addr != 0)
2112 {
2113 inet_ntop(AF_INET, &mess->siaddr, daemon->addrbuff, ADDRSTRLEN);
2114 my_syslog(MS_DHCP | LOG_INFO, _("%u next server: %s"), ntohl(mess->xid), daemon->addrbuff);
2115 }
2116
2117 if ((mess->flags & htons(0x8000)) && mess->ciaddr.s_addr == 0)
2118 my_syslog(MS_DHCP | LOG_INFO, _("%u broadcast response"), ntohl(mess->xid));
2119
2120 log_options(&mess->options[0] + sizeof(u32), mess->xid);
2121 }
2122
2123 ret = (size_t)(p - (unsigned char *)mess);
2124
2125 if (ret < MIN_PACKETSZ)
2126 ret = MIN_PACKETSZ;
2127
2128 return ret;
2129 }
2130
2131 static unsigned char *free_space(struct dhcp_packet *mess, unsigned char *end, int opt, int len)
2132 {
2133 unsigned char *p = dhcp_skip_opts(&mess->options[0] + sizeof(u32));
2134
2135 if (p + len + 3 >= end)
2136 /* not enough space in options area, try and use overload, if poss */
2137 {
2138 unsigned char *overload;
2139
2140 if (!(overload = find_overload(mess)) &&
2141 (mess->file[0] == 0 || mess->sname[0] == 0))
2142 {
2143 /* attempt to overload fname and sname areas, we've reserved space for the
2144 overflow option previuously. */
2145 overload = p;
2146 *(p++) = OPTION_OVERLOAD;
2147 *(p++) = 1;
2148 }
2149
2150 p = NULL;
2151
2152 /* using filename field ? */
2153 if (overload)
2154 {
2155 if (mess->file[0] == 0)
2156 overload[2] |= 1;
2157
2158 if (overload[2] & 1)
2159 {
2160 p = dhcp_skip_opts(mess->file);
2161 if (p + len + 3 >= mess->file + sizeof(mess->file))
2162 p = NULL;
2163 }
2164
2165 if (!p)
2166 {
2167 /* try to bring sname into play (it may be already) */
2168 if (mess->sname[0] == 0)
2169 overload[2] |= 2;
2170
2171 if (overload[2] & 2)
2172 {
2173 p = dhcp_skip_opts(mess->sname);
2174 if (p + len + 3 >= mess->sname + sizeof(mess->sname))
2175 p = NULL;
2176 }
2177 }
2178 }
2179
2180 if (!p)
2181 my_syslog(MS_DHCP | LOG_WARNING, _("cannot send DHCP/BOOTP option %d: no space left in packet"), opt);
2182 }
2183
2184 if (p)
2185 {
2186 *(p++) = opt;
2187 *(p++) = len;
2188 }
2189
2190 return p;
2191 }
2192
2193 static void option_put(struct dhcp_packet *mess, unsigned char *end, int opt, int len, unsigned int val)
2194 {
2195 int i;
2196 unsigned char *p = free_space(mess, end, opt, len);
2197
2198 if (p)
2199 for (i = 0; i < len; i++)
2200 *(p++) = val >> (8 * (len - (i + 1)));
2201 }
2202
2203 static void option_put_string(struct dhcp_packet *mess, unsigned char *end, int opt,
2204 const char *string, int null_term)
2205 {
2206 unsigned char *p;
2207 size_t len = strlen(string);
2208
2209 if (null_term && len != 255)
2210 len++;
2211
2212 if ((p = free_space(mess, end, opt, len)))
2213 memcpy(p, string, len);
2214 }
2215
2216 /* return length, note this only does the data part */
2217 static int do_opt(struct dhcp_opt *opt, unsigned char *p, struct dhcp_context *context, int null_term)
2218 {
2219 int len = opt->len;
2220
2221 if ((opt->flags & DHOPT_STRING) && null_term && len != 255)
2222 len++;
2223
2224 if (p && len != 0)
2225 {
2226 if (context && (opt->flags & DHOPT_ADDR))
2227 {
2228 int j;
2229 struct in_addr *a = (struct in_addr *)opt->val;
2230 for (j = 0; j < opt->len; j+=INADDRSZ, a++)
2231 {
2232 /* zero means "self" (but not in vendorclass options.) */
2233 if (a->s_addr == 0)
2234 memcpy(p, &context->local, INADDRSZ);
2235 else
2236 memcpy(p, a, INADDRSZ);
2237 p += INADDRSZ;
2238 }
2239 }
2240 else
2241 /* empty string may be extended to "\0" by null_term */
2242 memcpy(p, opt->val ? opt->val : (unsigned char *)"", len);
2243 }
2244 return len;
2245 }
2246
2247 static int in_list(unsigned char *list, int opt)
2248 {
2249 int i;
2250
2251 /* If no requested options, send everything, not nothing. */
2252 if (!list)
2253 return 1;
2254
2255 for (i = 0; list[i] != OPTION_END; i++)
2256 if (opt == list[i])
2257 return 1;
2258
2259 return 0;
2260 }
2261
2262 static struct dhcp_opt *option_find2(int opt)
2263 {
2264 struct dhcp_opt *opts;
2265
2266 for (opts = daemon->dhcp_opts; opts; opts = opts->next)
2267 if (opts->opt == opt && (opts->flags & DHOPT_TAGOK))
2268 return opts;
2269
2270 return NULL;
2271 }
2272
2273 /* mark vendor-encapsulated options which match the client-supplied or
2274 config-supplied vendor class */
2275 static void match_vendor_opts(unsigned char *opt, struct dhcp_opt *dopt)
2276 {
2277 for (; dopt; dopt = dopt->next)
2278 {
2279 dopt->flags &= ~DHOPT_VENDOR_MATCH;
2280 if (opt && (dopt->flags & DHOPT_VENDOR))
2281 {
2282 const struct dhcp_pxe_vendor *pv;
2283 struct dhcp_pxe_vendor dummy_vendor = {
2284 .data = (char *)dopt->u.vendor_class,
2285 .next = NULL,
2286 };
2287 if (dopt->flags & DHOPT_VENDOR_PXE)
2288 pv = daemon->dhcp_pxe_vendors;
2289 else
2290 pv = &dummy_vendor;
2291 for (; pv; pv = pv->next)
2292 {
2293 int i, len = 0, matched = 0;
2294 if (pv->data)
2295 len = strlen(pv->data);
2296 for (i = 0; i <= (option_len(opt) - len); i++)
2297 if (len == 0 || memcmp(pv->data, option_ptr(opt, i), len) == 0)
2298 {
2299 matched = 1;
2300 break;
2301 }
2302 if (matched)
2303 {
2304 dopt->flags |= DHOPT_VENDOR_MATCH;
2305 break;
2306 }
2307 }
2308 }
2309 }
2310 }
2311
2312 static int do_encap_opts(struct dhcp_opt *opt, int encap, int flag,
2313 struct dhcp_packet *mess, unsigned char *end, int null_term)
2314 {
2315 int len, enc_len, ret = 0;
2316 struct dhcp_opt *start;
2317 unsigned char *p;
2318
2319 /* find size in advance */
2320 for (enc_len = 0, start = opt; opt; opt = opt->next)
2321 if (opt->flags & flag)
2322 {
2323 int new = do_opt(opt, NULL, NULL, null_term) + 2;
2324 ret = 1;
2325 if (enc_len + new <= 255)
2326 enc_len += new;
2327 else
2328 {
2329 p = free_space(mess, end, encap, enc_len);
2330 for (; start && start != opt; start = start->next)
2331 if (p && (start->flags & flag))
2332 {
2333 len = do_opt(start, p + 2, NULL, null_term);
2334 *(p++) = start->opt;
2335 *(p++) = len;
2336 p += len;
2337 }
2338 enc_len = new;
2339 start = opt;
2340 }
2341 }
2342
2343 if (enc_len != 0 &&
2344 (p = free_space(mess, end, encap, enc_len + 1)))
2345 {
2346 for (; start; start = start->next)
2347 if (start->flags & flag)
2348 {
2349 len = do_opt(start, p + 2, NULL, null_term);
2350 *(p++) = start->opt;
2351 *(p++) = len;
2352 p += len;
2353 }
2354 *p = OPTION_END;
2355 }
2356
2357 return ret;
2358 }
2359
2360 static void pxe_misc(struct dhcp_packet *mess, unsigned char *end, unsigned char *uuid, const char *pxevendor)
2361 {
2362 unsigned char *p;
2363
2364 if (!pxevendor)
2365 pxevendor="PXEClient";
2366 option_put_string(mess, end, OPTION_VENDOR_ID, pxevendor, 0);
2367 if (uuid && (p = free_space(mess, end, OPTION_PXE_UUID, 17)))
2368 memcpy(p, uuid, 17);
2369 }
2370
2371 static int prune_vendor_opts(struct dhcp_netid *netid)
2372 {
2373 int force = 0;
2374 struct dhcp_opt *opt;
2375
2376 /* prune vendor-encapsulated options based on netid, and look if we're forcing them to be sent */
2377 for (opt = daemon->dhcp_opts; opt; opt = opt->next)
2378 if (opt->flags & DHOPT_VENDOR_MATCH)
2379 {
2380 if (!match_netid(opt->netid, netid, 1))
2381 opt->flags &= ~DHOPT_VENDOR_MATCH;
2382 else if (opt->flags & DHOPT_FORCE)
2383 force = 1;
2384 }
2385 return force;
2386 }
2387
2388
2389 /* Many UEFI PXE implementations have badly broken menu code.
2390 If there's exactly one relevant menu item, we abandon the menu system,
2391 and jamb the data direct into the DHCP file, siaddr and sname fields.
2392 Note that in this case, we have to assume that layer zero would be requested
2393 by the client PXE stack. */
2394 static int pxe_uefi_workaround(int pxe_arch, struct dhcp_netid *netid, struct dhcp_packet *mess, struct in_addr local, time_t now, int pxe)
2395 {
2396 struct pxe_service *service, *found;
2397
2398 /* Only workaround UEFI archs. */
2399 if (pxe_arch < 6)
2400 return 0;
2401
2402 for (found = NULL, service = daemon->pxe_services; service; service = service->next)
2403 if (pxe_arch == service->CSA && service->basename && match_netid(service->netid, netid, 1))
2404 {
2405 if (found)
2406 return 0; /* More than one relevant menu item */
2407
2408 found = service;
2409 }
2410
2411 if (!found)
2412 return 0; /* No relevant menu items. */
2413
2414 if (!pxe)
2415 return 1;
2416
2417 if (found->sname)
2418 {
2419 mess->siaddr = a_record_from_hosts(found->sname, now);
2420 snprintf((char *)mess->sname, sizeof(mess->sname), "%s", found->sname);
2421 }
2422 else
2423 {
2424 if (found->server.s_addr != 0)
2425 mess->siaddr = found->server;
2426 else
2427 mess->siaddr = local;
2428
2429 inet_ntop(AF_INET, &mess->siaddr, (char *)mess->sname, INET_ADDRSTRLEN);
2430 }
2431
2432 if (found->basename)
2433 snprintf((char *)mess->file, sizeof(mess->file),
2434 strchr(found->basename, '.') ? "%s" : "%s.0", found->basename);
2435
2436 return 1;
2437 }
2438
2439 static struct dhcp_opt *pxe_opts(int pxe_arch, struct dhcp_netid *netid, struct in_addr local, time_t now)
2440 {
2441 #define NUM_OPTS 4
2442
2443 unsigned char *p, *q;
2444 struct pxe_service *service;
2445 static struct dhcp_opt *o, *ret;
2446 int i, j = NUM_OPTS - 1;
2447 struct in_addr boot_server;
2448
2449 /* We pass back references to these, hence they are declared static */
2450 static unsigned char discovery_control;
2451 static unsigned char fake_prompt[] = { 0, 'P', 'X', 'E' };
2452 static struct dhcp_opt *fake_opts = NULL;
2453
2454 /* Disable multicast, since we don't support it, and broadcast
2455 unless we need it */
2456 discovery_control = 3;
2457
2458 ret = daemon->dhcp_opts;
2459
2460 if (!fake_opts && !(fake_opts = whine_malloc(NUM_OPTS * sizeof(struct dhcp_opt))))
2461 return ret;
2462
2463 for (i = 0; i < NUM_OPTS; i++)
2464 {
2465 fake_opts[i].flags = DHOPT_VENDOR_MATCH;
2466 fake_opts[i].netid = NULL;
2467 fake_opts[i].next = i == (NUM_OPTS - 1) ? ret : &fake_opts[i+1];
2468 }
2469
2470 /* create the data for the PXE_MENU and PXE_SERVERS options. */
2471 p = (unsigned char *)daemon->dhcp_buff;
2472 q = (unsigned char *)daemon->dhcp_buff3;
2473
2474 for (i = 0, service = daemon->pxe_services; service; service = service->next)
2475 if (pxe_arch == service->CSA && match_netid(service->netid, netid, 1))
2476 {
2477 size_t len = strlen(service->menu);
2478 /* opt 43 max size is 255. encapsulated option has type and length
2479 bytes, so its max size is 253. */
2480 if (p - (unsigned char *)daemon->dhcp_buff + len + 3 < 253)
2481 {
2482 *(p++) = service->type >> 8;
2483 *(p++) = service->type;
2484 *(p++) = len;
2485 memcpy(p, service->menu, len);
2486 p += len;
2487 i++;
2488 }
2489 else
2490 {
2491 toobig:
2492 my_syslog(MS_DHCP | LOG_ERR, _("PXE menu too large"));
2493 return daemon->dhcp_opts;
2494 }
2495
2496 boot_server = service->basename ? local :
2497 (service->sname ? a_record_from_hosts(service->sname, now) : service->server);
2498
2499 if (boot_server.s_addr != 0)
2500 {
2501 if (q - (unsigned char *)daemon->dhcp_buff3 + 3 + INADDRSZ >= 253)
2502 goto toobig;
2503
2504 /* Boot service with known address - give it */
2505 *(q++) = service->type >> 8;
2506 *(q++) = service->type;
2507 *(q++) = 1;
2508 /* dest misaligned */
2509 memcpy(q, &boot_server.s_addr, INADDRSZ);
2510 q += INADDRSZ;
2511 }
2512 else if (service->type != 0)
2513 /* We don't know the server for a service type, so we'll
2514 allow the client to broadcast for it */
2515 discovery_control = 2;
2516 }
2517
2518 /* if no prompt, wait forever if there's a choice */
2519 fake_prompt[0] = (i > 1) ? 255 : 0;
2520
2521 if (i == 0)
2522 discovery_control = 8; /* no menu - just use use mess->filename */
2523 else
2524 {
2525 ret = &fake_opts[j--];
2526 ret->len = p - (unsigned char *)daemon->dhcp_buff;
2527 ret->val = (unsigned char *)daemon->dhcp_buff;
2528 ret->opt = SUBOPT_PXE_MENU;
2529
2530 if (q - (unsigned char *)daemon->dhcp_buff3 != 0)
2531 {
2532 ret = &fake_opts[j--];
2533 ret->len = q - (unsigned char *)daemon->dhcp_buff3;
2534 ret->val = (unsigned char *)daemon->dhcp_buff3;
2535 ret->opt = SUBOPT_PXE_SERVERS;
2536 }
2537 }
2538
2539 for (o = daemon->dhcp_opts; o; o = o->next)
2540 if ((o->flags & DHOPT_VENDOR_MATCH) && o->opt == SUBOPT_PXE_MENU_PROMPT)
2541 break;
2542
2543 if (!o)
2544 {
2545 ret = &fake_opts[j--];
2546 ret->len = sizeof(fake_prompt);
2547 ret->val = fake_prompt;
2548 ret->opt = SUBOPT_PXE_MENU_PROMPT;
2549 }
2550
2551 ret = &fake_opts[j--];
2552 ret->len = 1;
2553 ret->opt = SUBOPT_PXE_DISCOVERY;
2554 ret->val= &discovery_control;
2555
2556 return ret;
2557 }
2558
2559 static void clear_packet(struct dhcp_packet *mess, unsigned char *end)
2560 {
2561 memset(mess->sname, 0, sizeof(mess->sname));
2562 memset(mess->file, 0, sizeof(mess->file));
2563 memset(&mess->options[0] + sizeof(u32), 0, end - (&mess->options[0] + sizeof(u32)));
2564 mess->siaddr.s_addr = 0;
2565 }
2566
2567 struct dhcp_boot *find_boot(struct dhcp_netid *netid)
2568 {
2569 struct dhcp_boot *boot;
2570
2571 /* decide which dhcp-boot option we're using */
2572 for (boot = daemon->boot_config; boot; boot = boot->next)
2573 if (match_netid(boot->netid, netid, 0))
2574 break;
2575 if (!boot)
2576 /* No match, look for one without a netid */
2577 for (boot = daemon->boot_config; boot; boot = boot->next)
2578 if (match_netid(boot->netid, netid, 1))
2579 break;
2580
2581 return boot;
2582 }
2583
2584 static int is_pxe_client(struct dhcp_packet *mess, size_t sz, const char **pxe_vendor)
2585 {
2586 const unsigned char *opt = NULL;
2587 ssize_t conf_len = 0;
2588 const struct dhcp_pxe_vendor *conf = daemon->dhcp_pxe_vendors;
2589 opt = option_find(mess, sz, OPTION_VENDOR_ID, 0);
2590 if (!opt)
2591 return 0;
2592 for (; conf; conf = conf->next)
2593 {
2594 conf_len = strlen(conf->data);
2595 if (option_len(opt) < conf_len)
2596 continue;
2597 if (strncmp(option_ptr(opt, 0), conf->data, conf_len) == 0)
2598 {
2599 if (pxe_vendor)
2600 *pxe_vendor = conf->data;
2601 return 1;
2602 }
2603 }
2604 return 0;
2605 }
2606
2607 static void do_options(struct dhcp_context *context,
2608 struct dhcp_packet *mess,
2609 unsigned char *end,
2610 unsigned char *req_options,
2611 char *hostname,
2612 char *domain,
2613 struct dhcp_netid *netid,
2614 struct in_addr subnet_addr,
2615 unsigned char fqdn_flags,
2616 int null_term, int pxe_arch,
2617 unsigned char *uuid,
2618 int vendor_class_len,
2619 time_t now,
2620 unsigned int lease_time,
2621 unsigned short fuzz,
2622 const char *pxevendor,
2623 int leasequery)
2624 {
2625 struct dhcp_opt *opt, *config_opts = daemon->dhcp_opts;
2626 struct dhcp_boot *boot;
2627 unsigned char *p;
2628 int i, len, force_encap = 0;
2629 unsigned char f0 = 0, s0 = 0;
2630 int done_file = 0, done_server = 0;
2631 int done_vendor_class = 0;
2632 struct dhcp_netid *tagif;
2633 struct dhcp_netid_list *id_list;
2634
2635 /* filter options based on tags, those we want get DHOPT_TAGOK bit set */
2636 if (context)
2637 context->netid.next = NULL;
2638 tagif = option_filter(netid, context && context->netid.net ? &context->netid : NULL, config_opts, pxe_arch != -1);
2639
2640 /* logging */
2641 if (option_bool(OPT_LOG_OPTS) && req_options)
2642 {
2643 char *q = daemon->namebuff;
2644 for (i = 0; req_options[i] != OPTION_END; i++)
2645 {
2646 char *s = option_string(AF_INET, req_options[i], NULL, 0, NULL, 0);
2647 q += snprintf(q, MAXDNAMESTR - (q - daemon->namebuff),
2648 "%d%s%s%s",
2649 req_options[i],
2650 strlen(s) != 0 ? ":" : "",
2651 s,
2652 req_options[i+1] == OPTION_END ? "" : ", ");
2653 if (req_options[i+1] == OPTION_END || (q - daemon->namebuff) > 40)
2654 {
2655 q = daemon->namebuff;
2656 my_syslog(MS_DHCP | LOG_INFO, _("%u requested options: %s"), ntohl(mess->xid), daemon->namebuff);
2657 }
2658 }
2659 }
2660
2661 if (!leasequery)
2662 {
2663 for (id_list = daemon->force_broadcast; id_list; id_list = id_list->next)
2664 if ((!id_list->list) || match_netid(id_list->list, netid, 0))
2665 break;
2666 if (id_list)
2667 mess->flags |= htons(0x8000); /* force broadcast */
2668
2669 if (context)
2670 mess->siaddr = context->local;
2671
2672 /* See if we can send the boot stuff as options.
2673 To do this we need a requested option list, BOOTP
2674 and very old DHCP clients won't have this, we also
2675 provide a manual option to disable it.
2676 Some PXE ROMs have bugs (surprise!) and need zero-terminated
2677 names, so we always send those. */
2678 if ((boot = find_boot(tagif)))
2679 {
2680 if (boot->sname)
2681 {
2682 if (!option_bool(OPT_NO_OVERRIDE) &&
2683 req_options &&
2684 in_list(req_options, OPTION_SNAME))
2685 option_put_string(mess, end, OPTION_SNAME, boot->sname, 1);
2686 else
2687 safe_strncpy((char *)mess->sname, boot->sname, sizeof(mess->sname));
2688 }
2689
2690 if (boot->file)
2691 {
2692 if (!option_bool(OPT_NO_OVERRIDE) &&
2693 req_options &&
2694 in_list(req_options, OPTION_FILENAME))
2695 option_put_string(mess, end, OPTION_FILENAME, boot->file, 1);
2696 else
2697 safe_strncpy((char *)mess->file, boot->file, sizeof(mess->file));
2698 }
2699
2700 if (boot->next_server.s_addr)
2701 mess->siaddr = boot->next_server;
2702 else if (boot->tftp_sname)
2703 mess->siaddr = a_record_from_hosts(boot->tftp_sname, now);
2704 }
2705 else
2706 /* Use the values of the relevant options if no dhcp-boot given and
2707 they're not explicitly asked for as options. OPTION_END is used
2708 as an internal way to specify siaddr without using dhcp-boot, for use in
2709 dhcp-optsfile. */
2710 {
2711 if ((!req_options || !in_list(req_options, OPTION_FILENAME)) &&
2712 (opt = option_find2(OPTION_FILENAME)) && !(opt->flags & DHOPT_FORCE))
2713 {
2714 safe_strncpy((char *)mess->file, (char *)opt->val, sizeof(mess->file));
2715 done_file = 1;
2716 }
2717
2718 if ((!req_options || !in_list(req_options, OPTION_SNAME)) &&
2719 (opt = option_find2(OPTION_SNAME)) && !(opt->flags & DHOPT_FORCE))
2720 {
2721 safe_strncpy((char *)mess->sname, (char *)opt->val, sizeof(mess->sname));
2722 done_server = 1;
2723 }
2724
2725 if ((opt = option_find2(OPTION_END)))
2726 mess->siaddr.s_addr = ((struct in_addr *)opt->val)->s_addr;
2727 }
2728 }
2729
2730 /* We don't want to do option-overload for BOOTP, so make the file and sname
2731 fields look like they are in use, even when they aren't. This gets restored
2732 at the end of this function. */
2733
2734 if (!req_options || option_bool(OPT_NO_OVERRIDE))
2735 {
2736 f0 = mess->file[0];
2737 mess->file[0] = 1;
2738 s0 = mess->sname[0];
2739 mess->sname[0] = 1;
2740 }
2741
2742 /* At this point, if mess->sname or mess->file are zeroed, they are available
2743 for option overload, reserve space for the overload option. */
2744 if (mess->file[0] == 0 || mess->sname[0] == 0)
2745 end -= 3;
2746
2747 /* rfc3011 says this doesn't need to be in the requested options list. */
2748 if (!leasequery && subnet_addr.s_addr)
2749 option_put(mess, end, OPTION_SUBNET_SELECT, INADDRSZ, ntohl(subnet_addr.s_addr));
2750
2751 if (lease_time != 0xffffffff)
2752 {
2753 unsigned int t1val = lease_time/2;
2754 unsigned int t2val = (lease_time*7)/8;
2755 unsigned int hval;
2756
2757 /* If set by user, sanity check, so not longer than lease. */
2758 if ((opt = option_find2(OPTION_T1)))
2759 {
2760 hval = ntohl(*((unsigned int *)opt->val));
2761 if (hval < lease_time && hval > 2)
2762 t1val = hval;
2763 }
2764
2765 if ((opt = option_find2(OPTION_T2)))
2766 {
2767 hval = ntohl(*((unsigned int *)opt->val));
2768 if (hval < lease_time && hval > 2)
2769 t2val = hval;
2770 }
2771
2772 /* ensure T1 is still < T2 */
2773 if (t2val <= t1val)
2774 t1val = t2val - 1;
2775
2776 while (fuzz > (t1val/8))
2777 fuzz = fuzz/2;
2778
2779 t1val -= fuzz;
2780 t2val -= fuzz;
2781
2782 option_put(mess, end, OPTION_T1, 4, t1val);
2783 option_put(mess, end, OPTION_T2, 4, t2val);
2784 }
2785
2786 /* replies to DHCPINFORM may not have a valid context */
2787 if (context)
2788 {
2789 /* Netmask and broadcast always sent, except leasequery. */
2790 if (!option_find2(OPTION_NETMASK) &&
2791 (!leasequery || in_list(req_options, OPTION_NETMASK)))
2792 option_put(mess, end, OPTION_NETMASK, INADDRSZ, ntohl(context->netmask.s_addr));
2793
2794 /* May not have a "guessed" broadcast address if we got no packets via a relay
2795 from this net yet (ie just unicast renewals after a restart */
2796 if (context->broadcast.s_addr &&
2797 !option_find2(OPTION_BROADCAST) &&
2798 (!leasequery || in_list(req_options, OPTION_BROADCAST)))
2799 option_put(mess, end, OPTION_BROADCAST, INADDRSZ, ntohl(context->broadcast.s_addr));
2800
2801 /* Same comments as broadcast apply, and also may not be able to get a sensible
2802 default when using subnet select. User must configure by steam in that case. */
2803 if (context->router.s_addr &&
2804 in_list(req_options, OPTION_ROUTER) &&
2805 !option_find2(OPTION_ROUTER))
2806 option_put(mess, end, OPTION_ROUTER, INADDRSZ, ntohl(context->router.s_addr));
2807
2808 if (daemon->port == NAMESERVER_PORT &&
2809 in_list(req_options, OPTION_DNSSERVER) &&
2810 !option_find2(OPTION_DNSSERVER))
2811 option_put(mess, end, OPTION_DNSSERVER, INADDRSZ, ntohl(context->local.s_addr));
2812 }
2813
2814 if (domain && in_list(req_options, OPTION_DOMAINNAME) &&
2815 !option_find2(OPTION_DOMAINNAME))
2816 option_put_string(mess, end, OPTION_DOMAINNAME, domain, null_term);
2817
2818 /* Note that we ignore attempts to set the fqdn using --dhc-option=81,<name> */
2819 if (hostname)
2820 {
2821 if (in_list(req_options, OPTION_HOSTNAME) &&
2822 !option_find2(OPTION_HOSTNAME))
2823 option_put_string(mess, end, OPTION_HOSTNAME, hostname, null_term);
2824
2825 if (fqdn_flags != 0)
2826 {
2827 len = strlen(hostname) + 3;
2828
2829 if (fqdn_flags & 0x04)
2830 len += 2;
2831 else if (null_term)
2832 len++;
2833
2834 if (domain)
2835 len += strlen(domain) + 1;
2836 else if (fqdn_flags & 0x04)
2837 len--;
2838
2839 if ((p = free_space(mess, end, OPTION_CLIENT_FQDN, len)))
2840 {
2841 *(p++) = fqdn_flags & 0x0f; /* MBZ bits to zero */
2842 *(p++) = 255;
2843 *(p++) = 255;
2844
2845 if (fqdn_flags & 0x04)
2846 {
2847 p = do_rfc1035_name(p, hostname, NULL);
2848 if (domain)
2849 {
2850 p = do_rfc1035_name(p, domain, NULL);
2851 *p++ = 0;
2852 }
2853 }
2854 else
2855 {
2856 memcpy(p, hostname, strlen(hostname));
2857 p += strlen(hostname);
2858 if (domain)
2859 {
2860 *(p++) = '.';
2861 memcpy(p, domain, strlen(domain));
2862 p += strlen(domain);
2863 }
2864 if (null_term)
2865 *(p++) = 0;
2866 }
2867 }
2868 }
2869 }
2870
2871 for (opt = config_opts; opt; opt = opt->next)
2872 {
2873 int optno = opt->opt;
2874
2875 /* netids match and not encapsulated? */
2876 if (!(opt->flags & DHOPT_TAGOK))
2877 continue;
2878
2879 /* was it asked for, or are we sending it anyway? */
2880 if ((!(opt->flags & DHOPT_FORCE) || leasequery) && !in_list(req_options, optno))
2881 continue;
2882
2883 /* prohibit some used-internally options. T1 and T2 already handled. */
2884 if (optno == OPTION_CLIENT_FQDN ||
2885 optno == OPTION_MAXMESSAGE ||
2886 optno == OPTION_OVERLOAD ||
2887 optno == OPTION_PAD ||
2888 optno == OPTION_END ||
2889 optno == OPTION_T1 ||
2890 optno == OPTION_T2)
2891 continue;
2892
2893 if (optno == OPTION_SNAME && done_server)
2894 continue;
2895
2896 if (optno == OPTION_FILENAME && done_file)
2897 continue;
2898
2899 /* For the options we have default values on
2900 dhc-option=<optionno> means "don't include this option"
2901 not "include a zero-length option" */
2902 if (opt->len == 0 &&
2903 (optno == OPTION_NETMASK ||
2904 optno == OPTION_BROADCAST ||
2905 optno == OPTION_ROUTER ||
2906 optno == OPTION_DNSSERVER ||
2907 optno == OPTION_DOMAINNAME ||
2908 optno == OPTION_HOSTNAME))
2909 continue;
2910
2911 /* vendor-class comes from elsewhere for PXE */
2912 if (pxe_arch != -1 && optno == OPTION_VENDOR_ID)
2913 continue;
2914
2915 /* always force null-term for filename and servername - buggy PXE again. */
2916 len = do_opt(opt, NULL, context,
2917 (optno == OPTION_SNAME || optno == OPTION_FILENAME) ? 1 : null_term);
2918
2919 if ((p = free_space(mess, end, optno, len)))
2920 {
2921 do_opt(opt, p, context,
2922 (optno == OPTION_SNAME || optno == OPTION_FILENAME) ? 1 : null_term);
2923
2924 /* If we send a vendor-id, revisit which vendor-ops we consider
2925 it appropriate to send. */
2926 if (optno == OPTION_VENDOR_ID)
2927 {
2928 match_vendor_opts(p - 2, config_opts);
2929 done_vendor_class = 1;
2930 }
2931 }
2932 }
2933
2934 /* encapsulated options. */
2935 handle_encap(mess, end, req_options, null_term, tagif, pxe_arch != 1);
2936
2937 force_encap = prune_vendor_opts(tagif);
2938
2939 if (context && pxe_arch != -1)
2940 {
2941 pxe_misc(mess, end, uuid, pxevendor);
2942 if (!pxe_uefi_workaround(pxe_arch, tagif, mess, context->local, now, 0))
2943 config_opts = pxe_opts(pxe_arch, tagif, context->local, now);
2944 }
2945
2946 if ((force_encap || in_list(req_options, OPTION_VENDOR_CLASS_OPT) || in_list(req_options, OPTION_VENDOR_ID)) &&
2947 (leasequery || do_encap_opts(config_opts, OPTION_VENDOR_CLASS_OPT, DHOPT_VENDOR_MATCH, mess, end, null_term)) &&
2948 pxe_arch == -1 && !done_vendor_class && vendor_class_len != 0 &&
2949 (p = free_space(mess, end, OPTION_VENDOR_ID, vendor_class_len)))
2950 /* If we send vendor encapsulated options, and haven't already sent option 60,
2951 echo back the value we got from the client. */
2952 memcpy(p, daemon->dhcp_buff3, vendor_class_len);
2953
2954 /* restore BOOTP anti-overload hack */
2955 if (!req_options || option_bool(OPT_NO_OVERRIDE))
2956 {
2957 mess->file[0] = f0;
2958 mess->sname[0] = s0;
2959 }
2960 }
2961
2962 static void handle_encap(struct dhcp_packet *mess, unsigned char *end, unsigned char *req_options,
2963 int null_term, struct dhcp_netid *tagif, int pxemode)
2964 {
2965 /* Send options to be encapsulated in arbitrary options,
2966 eg dhcp-option=encap:172,17,.......
2967 Also handle vendor-identifying vendor-encapsulated options,
2968 dhcp-option = vi-encap:13,17,.......
2969 The may be more that one "outer" to do, so group
2970 all the options which match each outer in turn. */
2971
2972 struct dhcp_opt *opt, *config_opts = daemon->dhcp_opts;
2973 unsigned char *p;
2974 int len;
2975
2976 for (opt = config_opts; opt; opt = opt->next)
2977 opt->flags &= ~DHOPT_ENCAP_DONE;
2978
2979 for (opt = config_opts; opt; opt = opt->next)
2980 {
2981 int flags;
2982
2983 if ((flags = (opt->flags & (DHOPT_ENCAPSULATE | DHOPT_RFC3925))))
2984 {
2985 int found = 0;
2986 struct dhcp_opt *o;
2987
2988 if (opt->flags & DHOPT_ENCAP_DONE)
2989 continue;
2990
2991 for (len = 0, o = config_opts; o; o = o->next)
2992 {
2993 int outer = flags & DHOPT_ENCAPSULATE ? o->u.encap : OPTION_VENDOR_IDENT_OPT;
2994
2995 o->flags &= ~DHOPT_ENCAP_MATCH;
2996
2997 if (!(o->flags & flags) || opt->u.encap != o->u.encap)
2998 continue;
2999
3000 o->flags |= DHOPT_ENCAP_DONE;
3001 if (match_netid(o->netid, tagif, 1) &&
3002 pxe_ok(o, pxemode) &&
3003 ((o->flags & DHOPT_FORCE) || in_list(req_options, outer)))
3004 {
3005 o->flags |= DHOPT_ENCAP_MATCH;
3006 found = 1;
3007 len += do_opt(o, NULL, NULL, 0) + 2;
3008 }
3009 }
3010
3011 if (found)
3012 {
3013 if (flags & DHOPT_ENCAPSULATE)
3014 do_encap_opts(config_opts, opt->u.encap, DHOPT_ENCAP_MATCH, mess, end, null_term);
3015 else if (len > 250)
3016 my_syslog(MS_DHCP | LOG_WARNING, _("cannot send RFC3925 option: too many options for enterprise number %d"), opt->u.encap);
3017 else if ((p = free_space(mess, end, OPTION_VENDOR_IDENT_OPT, len + 5)))
3018 {
3019 int swap_ent = htonl(opt->u.encap);
3020 memcpy(p, &swap_ent, 4);
3021 p += 4;
3022 *(p++) = len;
3023 for (o = config_opts; o; o = o->next)
3024 if (o->flags & DHOPT_ENCAP_MATCH)
3025 {
3026 len = do_opt(o, p + 2, NULL, 0);
3027 *(p++) = o->opt;
3028 *(p++) = len;
3029 p += len;
3030 }
3031 }
3032 }
3033 }
3034 }
3035 }
3036
3037 static void apply_delay(u32 xid, time_t recvtime, struct dhcp_netid *netid)
3038 {
3039 struct delay_config *delay_conf;
3040
3041 /* Decide which delay_config option we're using */
3042 for (delay_conf = daemon->delay_conf; delay_conf; delay_conf = delay_conf->next)
3043 if (match_netid(delay_conf->netid, netid, 0))
3044 break;
3045
3046 if (!delay_conf)
3047 /* No match, look for one without a netid */
3048 for (delay_conf = daemon->delay_conf; delay_conf; delay_conf = delay_conf->next)
3049 if (match_netid(delay_conf->netid, netid, 1))
3050 break;
3051
3052 if (delay_conf)
3053 {
3054 if (!option_bool(OPT_QUIET_DHCP))
3055 my_syslog(MS_DHCP | LOG_INFO, _("%u reply delay: %d"), ntohl(xid), delay_conf->delay);
3056 delay_dhcp(recvtime, delay_conf->delay, -1, 0, 0);
3057 }
3058 }
3059
3060 void relay_upstream4(struct in_addr iface_addr, int iface_index, struct dhcp_packet *mess, size_t sz, int unicast)
3061 {
3062 struct in_addr giaddr = mess->giaddr;
3063 u8 hops = mess->hops;
3064 struct dhcp_relay *relay;
3065 size_t orig_sz = sz;
3066 unsigned char *endopt = NULL;
3067
3068 if (mess->op != BOOTREQUEST || (mess->hops++) > 20)
3069 return;
3070
3071 for (relay = daemon->relay4; relay; relay = relay->next)
3072 {
3073 union mysockaddr to;
3074 union all_addr from;
3075 struct ifreq ifr;
3076
3077 /* restore orig packet */
3078 mess->giaddr = giaddr;
3079 if (endopt)
3080 *endopt = OPTION_END;
3081 sz = orig_sz;
3082
3083 if (relay->interface)
3084 {
3085 safe_strncpy(ifr.ifr_name, relay->interface, IF_NAMESIZE);
3086 ifr.ifr_addr.sa_family = AF_INET;
3087 }
3088
3089 if (!relay->split_mode && relay->iface_index && relay->iface_index == iface_index)
3090 {
3091 /* already gatewayed ? */
3092 if (giaddr.s_addr)
3093 {
3094 /* if so check if by us, to stomp on loops. */
3095 if (giaddr.s_addr == relay->local.addr4.s_addr)
3096 continue;
3097 }
3098 else
3099 /* plug in our address */
3100 mess->giaddr = relay->local.addr4;
3101
3102 from.addr4 = relay->local.addr4;
3103 }
3104 else if (relay->split_mode && relay->local.addr4.s_addr == iface_addr.s_addr)
3105 {
3106 /* Split mode. We put our address on the server-facing interface
3107 or a directly specified third address into giaddr for the server to talk back to us on.
3108
3109 Our address on client-facing interface goes into agent-id subnet-selector subopt,
3110 so that the server allocates the correct address. We also send a
3111 remote-id with the interface on which the request arrived,
3112 so that we can send the reply back the same way. */
3113 unsigned int net_index = htonl(iface_index);
3114
3115 if (relay->interface)
3116 {
3117 /* get our address on the server-facing interface. */
3118 if (ioctl(daemon->dhcpfd, SIOCGIFADDR, &ifr) == -1)
3119 {
3120 my_syslog(MS_DHCP | LOG_ERR, _("Cannot send to server via interface %s: %s"), relay->interface, strerror(errno));
3121 continue;
3122 }
3123
3124 relay->uplink.addr4 = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr;
3125 }
3126
3127 /* already gatewayed ? */
3128 if (giaddr.s_addr)
3129 {
3130 /* if so check if by us, to stomp on loops. */
3131 if (giaddr.s_addr == relay->uplink.addr4.s_addr)
3132 continue;
3133 }
3134 else
3135 {
3136 /* giaddr is our address on the outgoing interface in split mode. */
3137 mess->giaddr = relay->uplink.addr4;
3138
3139 if (!endopt)
3140 {
3141 /* Add an RFC3026 relay agent information option (2 bytes) at the very end of the options.
3142 Said option to contain a RFC 3527 link selection sub option (6 bytes) and
3143 RFC 5017 serverid-override option (6 bytes) and RFC5010 flags (3 bytes) and
3144 an RFC3046 remote-id which holds an interface index (6 bytes)
3145
3146 New END option is a 24th byte, so we need 24 bytes free.
3147 We only need to do this once, and poke the address/interface/flags into the same place each time. */
3148
3149 if (!(endopt = option_find1((&mess->options[0] + sizeof(u32)), ((unsigned char *)mess) + sz, OPTION_END, 0)) ||
3150 (endopt + 24 > (unsigned char *)(mess + 1)))
3151 continue;
3152
3153 endopt[1] = 21; /* length */
3154 endopt[2] = SUBOPT_SUBNET_SELECT;
3155 endopt[3] = 4; /* length */
3156 endopt[8] = SUBOPT_SERVER_OR;
3157 endopt[9] = 4;
3158 endopt[14] = SUBOPT_FLAGS;
3159 endopt[15] = 1; /* length */
3160 endopt[17] = SUBOPT_REMOTE_ID;
3161 endopt[18] = 4; /* length */
3162 endopt[23] = OPTION_END;
3163 sz = (endopt - (unsigned char *)mess) + 24;
3164 }
3165
3166 /* IP address is already in network byte order */
3167 memcpy(&endopt[4], &relay->local.addr4.s_addr, INADDRSZ);
3168 memcpy(&endopt[10], &relay->local.addr4.s_addr, INADDRSZ);
3169 endopt[16] = unicast ? 0x80 : 0x00;
3170 memcpy(&endopt[19], &net_index, 4);
3171 endopt[0] = OPTION_AGENT_ID;
3172 }
3173
3174 from.addr4 = relay->uplink.addr4;
3175 }
3176 else
3177 continue;
3178
3179 to.sa.sa_family = AF_INET;
3180 to.in.sin_addr = relay->server.addr4;
3181 to.in.sin_port = htons(relay->port);
3182 #ifdef HAVE_SOCKADDR_SA_LEN
3183 to.in.sin_len = sizeof(struct sockaddr_in);
3184 #endif
3185
3186 /* Broadcasting to server. */
3187 if (relay->server.addr4.s_addr == 0)
3188 {
3189 if (ioctl(daemon->dhcpfd, SIOCGIFBRDADDR, &ifr) == -1)
3190 {
3191 my_syslog(MS_DHCP | LOG_ERR, _("Cannot broadcast DHCP relay via interface %s: %s"), relay->interface, strerror(errno));
3192 continue;
3193 }
3194
3195 to.in.sin_addr = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr;
3196 }
3197
3198 #ifdef HAVE_DUMPFILE
3199 {
3200 union mysockaddr fromsock;
3201 fromsock.in.sin_port = htons(daemon->dhcp_server_port);
3202 fromsock.in.sin_addr = from.addr4;
3203 fromsock.sa.sa_family = AF_INET;
3204
3205 dump_packet_udp(DUMP_DHCP, (void *)mess, sz, &fromsock, &to, -1);
3206 }
3207 #endif
3208
3209 send_from(daemon->dhcpfd, 0, (char *)mess, sz, &to, &from, 0);
3210
3211 if (option_bool(OPT_LOG_OPTS))
3212 {
3213 inet_ntop(AF_INET, &relay->local, daemon->addrbuff, ADDRSTRLEN);
3214 if (relay->server.addr4.s_addr == 0)
3215 snprintf(daemon->dhcp_buff2, DHCP_BUFF_SZ, _("broadcast via %s"), relay->interface);
3216 else
3217 inet_ntop(AF_INET, &relay->server.addr4, daemon->dhcp_buff2, DHCP_BUFF_SZ);
3218 my_syslog(MS_DHCP | LOG_INFO, _("DHCP relay at %s -> %s"), daemon->addrbuff, daemon->dhcp_buff2);
3219 }
3220 }
3221
3222 /* restore in case of a local reply. */
3223 mess->hops = hops;
3224 mess->giaddr = giaddr;
3225 if (endopt)
3226 *endopt = OPTION_END;
3227 }
3228
3229 unsigned int relay_reply4(struct dhcp_packet *mess, size_t sz, char *arrival_interface)
3230 {
3231 struct dhcp_relay *relay;
3232
3233 if (mess->giaddr.s_addr == 0 || mess->op != BOOTREPLY)
3234 return 0;
3235
3236 for (relay = daemon->relay4; relay; relay = relay->next)
3237 {
3238 unsigned int return_iface = 0;
3239
3240 if (relay->split_mode)
3241 {
3242 unsigned char *opt, *sopt;
3243
3244 /* giaddr is our address on the returning interface in split mode. */
3245 if (mess->giaddr.s_addr == relay->uplink.addr4.s_addr &&
3246 (opt = option_find(mess, sz, OPTION_AGENT_ID, 1)))
3247 {
3248 if ((sopt = option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_REMOTE_ID, sizeof(unsigned int))))
3249 return_iface = option_uint(sopt, 0, sizeof(unsigned int));
3250
3251 /* delete agent info before return RFC 3046 para 2.1 */
3252 *opt = OPTION_END;
3253 memset(opt + 1, 0, option_len(opt) + 1);
3254 }
3255 }
3256 else if (mess->giaddr.s_addr == relay->local.addr4.s_addr)
3257 return_iface = relay->iface_index;
3258
3259 if (return_iface && (!relay->interface || wildcard_match(relay->interface, arrival_interface)))
3260 return return_iface;
3261 }
3262
3263 return 0;
3264 }
3265
3266
3267 #endif /* HAVE_DHCP */