Debian Codegraph @

src/dhcp-common.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 void dhcp_common_init(void)
22 {
23 /* These each hold a DHCP option max size 255
24 and get a terminating zero added */
25 daemon->dhcp_buff = safe_malloc(DHCP_BUFF_SZ);
26 daemon->dhcp_buff2 = safe_malloc(DHCP_BUFF_SZ);
27 daemon->dhcp_buff3 = safe_malloc(DHCP_BUFF_SZ);
28
29 /* dhcp_packet is used by v4 and v6, outpacket only by v6
30 sizeof(struct dhcp_packet) is as good an initial size as any,
31 even for v6 */
32 expand_buf(&daemon->dhcp_packet, sizeof(struct dhcp_packet));
33 #ifdef HAVE_DHCP6
34 if (daemon->dhcp6)
35 expand_buf(&daemon->outpacket, sizeof(struct dhcp_packet));
36 #endif
37 }
38
39 ssize_t recv_dhcp_packet(int fd, struct msghdr *msg)
40 {
41 ssize_t sz, new_sz;
42
43 while (1)
44 {
45 msg->msg_flags = 0;
46 while ((sz = recvmsg(fd, msg, MSG_PEEK | MSG_TRUNC)) == -1 && errno == EINTR);
47
48 if (sz == -1)
49 return -1;
50
51 if (!(msg->msg_flags & MSG_TRUNC))
52 break;
53
54 /* Very new Linux kernels return the actual size needed,
55 older ones always return truncated size */
56 if ((size_t)sz == msg->msg_iov->iov_len)
57 {
58 if (!expand_buf(msg->msg_iov, sz + 100))
59 return -1;
60 }
61 else
62 {
63 expand_buf(msg->msg_iov, sz);
64 break;
65 }
66 }
67
68 while ((new_sz = recvmsg(fd, msg, 0)) == -1 && errno == EINTR);
69
70 /* Some kernels seem to ignore MSG_PEEK, and dequeue the packet anyway.
71 If that happens we get EAGAIN here because the socket is non-blocking.
72 Use the result of the original testing recvmsg as long as the buffer
73 was big enough. There's a small race here that may lose the odd packet,
74 but it's UDP anyway. */
75
76 if (new_sz == -1 && (errno == EWOULDBLOCK || errno == EAGAIN))
77 new_sz = sz;
78
79 return (msg->msg_flags & MSG_TRUNC) ? -1 : new_sz;
80 }
81
82 /* like match_netid() except that the check can have a trailing * for wildcard */
83 /* started as a direct copy of match_netid() */
84 int match_netid_wild(struct dhcp_netid *check, struct dhcp_netid *pool)
85 {
86 struct dhcp_netid *tmp1;
87
88 for (; check; check = check->next)
89 {
90 const int check_len = strlen(check->net);
91 const int is_wc = (check_len > 0 && check->net[check_len - 1] == '*');
92
93 /* '#' for not is for backwards compat. */
94 if (check->net[0] != '!' && check->net[0] != '#')
95 {
96 for (tmp1 = pool; tmp1; tmp1 = tmp1->next)
97 if (is_wc ? (strncmp(check->net, tmp1->net, check_len-1) == 0) :
98 (strcmp(check->net, tmp1->net) == 0))
99 break;
100 if (!tmp1)
101 return 0;
102 }
103 else
104 for (tmp1 = pool; tmp1; tmp1 = tmp1->next)
105 if (is_wc ? (strncmp((check->net)+1, tmp1->net, check_len-2) == 0) :
106 (strcmp((check->net)+1, tmp1->net) == 0))
107 return 0;
108 }
109 return 1;
110 }
111
112 struct dhcp_netid *run_tag_if(struct dhcp_netid *tags)
113 {
114 struct tag_if *exprs;
115 struct dhcp_netid_list *list;
116
117 /* this now uses match_netid_wild() above so that tag_if can
118 * be used to set a 'group of interfaces' tag.
119 */
120 for (exprs = daemon->tag_if; exprs; exprs = exprs->next)
121 if (match_netid_wild(exprs->tag, tags))
122 for (list = exprs->set; list; list = list->next)
123 {
124 list->list->next = tags;
125 tags = list->list;
126 }
127
128 return tags;
129 }
130
131 /* pxemode == 0 -> don't include dhcp-option-pxe options.
132 pxemode == 1 -> do include dhcp-option-pxe options.
133 pxemode == 2 -> include ONLY dhcp-option-pxe options. */
134 int pxe_ok(struct dhcp_opt *opt, int pxemode)
135 {
136 if (opt->flags & DHOPT_PXE_OPT)
137 {
138 if (pxemode != 0)
139 return 1;
140 }
141 else
142 {
143 if (pxemode != 2)
144 return 1;
145 }
146
147 return 0;
148 }
149
150 struct dhcp_netid *option_filter(struct dhcp_netid *tags, struct dhcp_netid *context_tags, struct dhcp_opt *opts, int pxemode)
151 {
152 struct dhcp_netid *tagif = run_tag_if(tags);
153 struct dhcp_opt *opt;
154 struct dhcp_opt *tmp;
155
156 /* flag options which are valid with the current tag set (sans context tags) */
157 for (opt = opts; opt; opt = opt->next)
158 {
159 opt->flags &= ~DHOPT_TAGOK;
160 if (!(opt->flags & (DHOPT_ENCAPSULATE | DHOPT_VENDOR | DHOPT_RFC3925)) &&
161 match_netid(opt->netid, tagif, 0) &&
162 pxe_ok(opt, pxemode))
163 opt->flags |= DHOPT_TAGOK;
164 }
165
166 /* now flag options which are valid, including the context tags,
167 otherwise valid options are inhibited if we found a higher priority one above */
168 if (context_tags)
169 {
170 struct dhcp_netid *last_tag;
171
172 for (last_tag = context_tags; last_tag->next; last_tag = last_tag->next);
173 last_tag->next = tags;
174 tagif = run_tag_if(context_tags);
175
176 /* reset stuff with tag:!<tag> which now matches. */
177 for (opt = opts; opt; opt = opt->next)
178 if (!(opt->flags & (DHOPT_ENCAPSULATE | DHOPT_VENDOR | DHOPT_RFC3925)) &&
179 (opt->flags & DHOPT_TAGOK) &&
180 !match_netid(opt->netid, tagif, 0))
181 opt->flags &= ~DHOPT_TAGOK;
182
183 for (opt = opts; opt; opt = opt->next)
184 if (!(opt->flags & (DHOPT_ENCAPSULATE | DHOPT_VENDOR | DHOPT_RFC3925 | DHOPT_TAGOK)) &&
185 match_netid(opt->netid, tagif, 0) &&
186 pxe_ok(opt, pxemode))
187 {
188 struct dhcp_opt *tmp;
189 for (tmp = opts; tmp; tmp = tmp->next)
190 if (tmp->opt == opt->opt && opt->netid && (tmp->flags & DHOPT_TAGOK))
191 break;
192 if (!tmp)
193 opt->flags |= DHOPT_TAGOK;
194 }
195 }
196
197 /* now flag untagged options which are not overridden by tagged ones */
198 for (opt = opts; opt; opt = opt->next)
199 if (!(opt->flags & (DHOPT_ENCAPSULATE | DHOPT_VENDOR | DHOPT_RFC3925 | DHOPT_TAGOK)) &&
200 !opt->netid &&
201 pxe_ok(opt, pxemode))
202 {
203 for (tmp = opts; tmp; tmp = tmp->next)
204 if (tmp->opt == opt->opt && (tmp->flags & DHOPT_TAGOK))
205 break;
206 if (!tmp)
207 opt->flags |= DHOPT_TAGOK;
208 else if (!tmp->netid)
209 my_syslog(MS_DHCP | LOG_WARNING, _("Ignoring duplicate dhcp-option %d"), tmp->opt);
210 }
211
212 /* Finally, eliminate duplicate options later in the chain, and therefore earlier in the config file. */
213 for (opt = opts; opt; opt = opt->next)
214 if (opt->flags & DHOPT_TAGOK)
215 for (tmp = opt->next; tmp; tmp = tmp->next)
216 if (tmp->opt == opt->opt)
217 tmp->flags &= ~DHOPT_TAGOK;
218
219 return tagif;
220 }
221
222 /* Is every member of check matched by a member of pool?
223 If tagnotneeded, untagged is OK */
224 int match_netid(struct dhcp_netid *check, struct dhcp_netid *pool, int tagnotneeded)
225 {
226 struct dhcp_netid *tmp1;
227
228 if (!check && !tagnotneeded)
229 return 0;
230
231 for (; check; check = check->next)
232 {
233 /* '#' for not is for backwards compat. */
234 if (check->net[0] != '!' && check->net[0] != '#')
235 {
236 for (tmp1 = pool; tmp1; tmp1 = tmp1->next)
237 if (strcmp(check->net, tmp1->net) == 0)
238 break;
239 if (!tmp1)
240 return 0;
241 }
242 else
243 for (tmp1 = pool; tmp1; tmp1 = tmp1->next)
244 if (strcmp((check->net)+1, tmp1->net) == 0)
245 return 0;
246 }
247 return 1;
248 }
249
250 /* return domain or NULL if none. */
251 char *strip_hostname(char *hostname)
252 {
253 char *dot = strchr(hostname, '.');
254
255 if (!dot)
256 return NULL;
257
258 *dot = 0; /* truncate */
259 if (strlen(dot+1) != 0)
260 return dot+1;
261
262 return NULL;
263 }
264
265 void log_tags(struct dhcp_netid *netid, u32 xid)
266 {
267 if (netid && option_bool(OPT_LOG_OPTS))
268 {
269 char *s = daemon->namebuff;
270 for (*s = 0; netid; netid = netid->next)
271 {
272 /* kill dupes. */
273 struct dhcp_netid *n;
274
275 for (n = netid->next; n; n = n->next)
276 if (strcmp(netid->net, n->net) == 0)
277 break;
278
279 if (!n)
280 {
281 strncat (s, netid->net, MAXDNAMESTR - strlen(s));
282 if (netid->next)
283 strncat (s, ", ", MAXDNAMESTR - strlen(s));
284 }
285 }
286 my_syslog(MS_DHCP | LOG_INFO, _("%u tags: %s"), xid, s);
287 }
288 }
289
290 int match_bytes(struct dhcp_opt *o, unsigned char *p, int len)
291 {
292 int i;
293
294 if (o->len > len)
295 return 0;
296
297 if (o->len == 0)
298 return 1;
299
300 if (o->flags & DHOPT_HEX)
301 {
302 if (memcmp_masked(o->val, p, o->len, o->u.wildcard_mask))
303 return 1;
304 }
305 else
306 for (i = 0; i <= (len - o->len); )
307 {
308 if (memcmp(o->val, p + i, o->len) == 0)
309 return 1;
310
311 if (o->flags & DHOPT_STRING)
312 i++;
313 else
314 i += o->len;
315 }
316
317 return 0;
318 }
319
320 int config_has_mac(struct dhcp_config *config, unsigned char *hwaddr, int len, int type)
321 {
322 struct hwaddr_config *conf_addr;
323
324 for (conf_addr = config->hwaddr; conf_addr; conf_addr = conf_addr->next)
325 if (conf_addr->wildcard_mask == 0 &&
326 conf_addr->hwaddr_len == len &&
327 (conf_addr->hwaddr_type == type || conf_addr->hwaddr_type == 0) &&
328 memcmp(conf_addr->hwaddr, hwaddr, len) == 0)
329 return 1;
330
331 return 0;
332 }
333
334 static int is_config_in_context(struct dhcp_context *context, struct dhcp_config *config)
335 {
336 if (!context) /* called via find_config() from lease_update_from_configs() */
337 return 1;
338
339 if (!(config->flags & (CONFIG_ADDR | CONFIG_ADDR6)))
340 return 1;
341
342 #ifdef HAVE_DHCP6
343 if (context->flags & CONTEXT_V6)
344 {
345 struct addrlist *addr_list;
346
347 if (config->flags & CONFIG_ADDR6)
348 for (; context; context = context->current)
349 for (addr_list = config->addr6; addr_list; addr_list = addr_list->next)
350 {
351 if ((addr_list->flags & ADDRLIST_WILDCARD) && context->prefix == 64)
352 return 1;
353
354 if (is_same_net6(&addr_list->addr.addr6, &context->start6, context->prefix))
355 return 1;
356 }
357 }
358 else
359 #endif
360 {
361 for (; context; context = context->current)
362 if ((config->flags & CONFIG_ADDR) && is_same_net(config->addr, context->start, context->netmask))
363 return 1;
364 }
365
366 return 0;
367 }
368
369 static struct dhcp_config *find_config_match(struct dhcp_config *configs,
370 struct dhcp_context *context,
371 unsigned char *clid, int clid_len,
372 unsigned char *hwaddr, int hw_len,
373 int hw_type, char *hostname,
374 struct dhcp_netid *tags, int tag_not_needed)
375 {
376 int count, new;
377 struct dhcp_config *config, *candidate;
378 struct hwaddr_config *conf_addr;
379
380 if (clid)
381 for (config = configs; config; config = config->next)
382 if (config->flags & CONFIG_CLID)
383 {
384 if (config->clid_len == clid_len &&
385 memcmp(config->clid, clid, clid_len) == 0 &&
386 is_config_in_context(context, config) &&
387 match_netid(config->filter, tags, tag_not_needed))
388
389 return config;
390
391 /* dhcpcd prefixes ASCII client IDs by zero which is wrong, but we try and
392 cope with that here. This is IPv4 only. context==NULL implies IPv4,
393 see lease_update_from_configs() */
394 if ((!context || !(context->flags & CONTEXT_V6)) && *clid == 0 && config->clid_len == clid_len-1 &&
395 memcmp(config->clid, clid+1, clid_len-1) == 0 &&
396 is_config_in_context(context, config) &&
397 match_netid(config->filter, tags, tag_not_needed))
398 return config;
399 }
400
401
402 if (hwaddr)
403 for (config = configs; config; config = config->next)
404 if (config_has_mac(config, hwaddr, hw_len, hw_type) &&
405 is_config_in_context(context, config) &&
406 match_netid(config->filter, tags, tag_not_needed))
407 return config;
408
409 if (hostname && context)
410 for (config = configs; config; config = config->next)
411 if ((config->flags & CONFIG_NAME) &&
412 hostname_isequal(config->hostname, hostname) &&
413 is_config_in_context(context, config) &&
414 match_netid(config->filter, tags, tag_not_needed))
415 return config;
416
417
418 if (!hwaddr)
419 return NULL;
420
421 /* use match with fewest wildcard octets */
422 for (candidate = NULL, count = 0, config = configs; config; config = config->next)
423 if (is_config_in_context(context, config) &&
424 match_netid(config->filter, tags, tag_not_needed))
425 for (conf_addr = config->hwaddr; conf_addr; conf_addr = conf_addr->next)
426 if (conf_addr->wildcard_mask != 0 &&
427 conf_addr->hwaddr_len == hw_len &&
428 (conf_addr->hwaddr_type == hw_type || conf_addr->hwaddr_type == 0) &&
429 (new = memcmp_masked(conf_addr->hwaddr, hwaddr, hw_len, conf_addr->wildcard_mask)) > count)
430 {
431 count = new;
432 candidate = config;
433 }
434
435 return candidate;
436 }
437
438 /* Find tagged configs first. */
439 struct dhcp_config *find_config(struct dhcp_config *configs,
440 struct dhcp_context *context,
441 unsigned char *clid, int clid_len,
442 unsigned char *hwaddr, int hw_len,
443 int hw_type, char *hostname, struct dhcp_netid *tags)
444 {
445 struct dhcp_config *ret = find_config_match(configs, context, clid, clid_len, hwaddr, hw_len, hw_type, hostname, tags, 0);
446
447 if (!ret)
448 ret = find_config_match(configs, context, clid, clid_len, hwaddr, hw_len, hw_type, hostname, tags, 1);
449
450 return ret;
451 }
452
453 void dhcp_update_configs(struct dhcp_config *configs)
454 {
455 /* Some people like to keep all static IP addresses in /etc/hosts.
456 This goes through /etc/hosts and sets static addresses for any DHCP config
457 records which don't have an address and whose name matches.
458 We take care to maintain the invariant that any IP address can appear
459 in at most one dhcp-host. Since /etc/hosts can be re-read by SIGHUP,
460 restore the status-quo ante first. */
461
462 struct dhcp_config *config, *conf_tmp;
463 struct crec *crec;
464 int prot = AF_INET;
465
466 for (config = configs; config; config = config->next)
467 {
468 if (config->flags & CONFIG_ADDR_HOSTS)
469 config->flags &= ~(CONFIG_ADDR | CONFIG_ADDR_HOSTS);
470 #ifdef HAVE_DHCP6
471 if (config->flags & CONFIG_ADDR6_HOSTS)
472 config->flags &= ~(CONFIG_ADDR6 | CONFIG_ADDR6_HOSTS);
473 #endif
474 }
475
476 #ifdef HAVE_DHCP6
477 again:
478 #endif
479
480 if (daemon->port != 0)
481 for (config = configs; config; config = config->next)
482 {
483 int conflags = CONFIG_ADDR;
484 int cacheflags = F_IPV4;
485
486 #ifdef HAVE_DHCP6
487 if (prot == AF_INET6)
488 {
489 conflags = CONFIG_ADDR6;
490 cacheflags = F_IPV6;
491 }
492 #endif
493 if (!(config->flags & conflags) &&
494 (config->flags & CONFIG_NAME) &&
495 (crec = cache_find_by_name(NULL, config->hostname, 0, cacheflags)) &&
496 (crec->flags & F_HOSTS))
497 {
498 if (cache_find_by_name(crec, config->hostname, 0, cacheflags))
499 {
500 /* use primary (first) address */
501 while (crec && !(crec->flags & F_REVERSE))
502 crec = cache_find_by_name(crec, config->hostname, 0, cacheflags);
503 if (!crec)
504 continue; /* should be never */
505 inet_ntop(prot, &crec->addr, daemon->addrbuff, ADDRSTRLEN);
506 my_syslog(MS_DHCP | LOG_WARNING, _("%s has more than one address in hostsfile, using %s for DHCP"),
507 config->hostname, daemon->addrbuff);
508 }
509
510 if (prot == AF_INET &&
511 (!(conf_tmp = config_find_by_address(configs, crec->addr.addr4)) || conf_tmp == config))
512 {
513 config->addr = crec->addr.addr4;
514 config->flags |= CONFIG_ADDR | CONFIG_ADDR_HOSTS;
515 continue;
516 }
517
518 #ifdef HAVE_DHCP6
519 if (prot == AF_INET6 &&
520 (!(conf_tmp = config_find_by_address6(configs, NULL, 0, &crec->addr.addr6)) || conf_tmp == config))
521 {
522 /* host must have exactly one address if comming from /etc/hosts. */
523 if (!config->addr6 && (config->addr6 = whine_malloc(sizeof(struct addrlist))))
524 {
525 config->addr6->next = NULL;
526 config->addr6->flags = 0;
527 }
528
529 if (config->addr6 && !config->addr6->next && !(config->addr6->flags & (ADDRLIST_WILDCARD|ADDRLIST_PREFIX)))
530 {
531 memcpy(&config->addr6->addr.addr6, &crec->addr.addr6, IN6ADDRSZ);
532 config->flags |= CONFIG_ADDR6 | CONFIG_ADDR6_HOSTS;
533 }
534
535 continue;
536 }
537 #endif
538
539 inet_ntop(prot, &crec->addr, daemon->addrbuff, ADDRSTRLEN);
540 my_syslog(MS_DHCP | LOG_WARNING, _("duplicate IP address %s (%s) in dhcp-config directive"),
541 daemon->addrbuff, config->hostname);
542
543
544 }
545 }
546
547 #ifdef HAVE_DHCP6
548 if (prot == AF_INET)
549 {
550 prot = AF_INET6;
551 goto again;
552 }
553 #endif
554
555 }
556
557 #ifdef HAVE_LINUX_NETWORK
558 char *whichdevice(void)
559 {
560 /* If we are doing DHCP on exactly one interface, and running linux, do SO_BINDTODEVICE
561 to that device. This is for the use case of (eg) OpenStack, which runs a new
562 dnsmasq instance for each VLAN interface it creates. Without the BINDTODEVICE,
563 individual processes don't always see the packets they should.
564 SO_BINDTODEVICE is only available Linux.
565
566 Note that if wildcards are used in --interface, or --interface is not used at all,
567 or a configured interface doesn't yet exist, then more interfaces may arrive later,
568 so we can't safely assert there is only one interface and proceed.
569 */
570
571 struct irec *iface, *found;
572 struct iname *if_tmp;
573
574 if (!daemon->if_names)
575 return NULL;
576
577 for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next)
578 if (if_tmp->name && (!(if_tmp->flags & INAME_USED) || strchr(if_tmp->name, '*')))
579 return NULL;
580
581 for (found = NULL, iface = daemon->interfaces; iface; iface = iface->next)
582 if (iface->dhcp4_ok || iface->dhcp6_ok)
583 {
584 if (!found)
585 found = iface;
586 else if (strcmp(found->name, iface->name) != 0)
587 return NULL; /* more than one. */
588 }
589
590 if (found)
591 {
592 char *ret = safe_malloc(strlen(found->name)+1);
593 strcpy(ret, found->name);
594 return ret;
595 }
596
597 return NULL;
598 }
599
600 static int bindtodevice(char *device, int fd)
601 {
602 size_t len = strlen(device)+1;
603 if (len > IFNAMSIZ)
604 len = IFNAMSIZ;
605 /* only allowed by root. */
606 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, device, len) == -1 &&
607 errno != EPERM)
608 return 2;
609
610 return 1;
611 }
612
613 int bind_dhcp_devices(char *bound_device)
614 {
615 int ret = 0;
616
617 if (bound_device)
618 {
619 if (daemon->dhcp)
620 {
621 if (!daemon->relay4)
622 ret |= bindtodevice(bound_device, daemon->dhcpfd);
623
624 if (daemon->enable_pxe && daemon->pxefd != -1)
625 ret |= bindtodevice(bound_device, daemon->pxefd);
626 }
627
628 #if defined(HAVE_DHCP6)
629 if (daemon->doing_dhcp6 && !daemon->relay6)
630 ret |= bindtodevice(bound_device, daemon->dhcp6fd);
631 #endif
632 }
633
634 return ret;
635 }
636 #endif
637
638 static const struct opttab_t {
639 char *name;
640 u16 val, size;
641 } opttab[] = {
642 { "netmask", 1, OT_ADDR_LIST },
643 { "time-offset", 2, 4 },
644 { "router", 3, OT_ADDR_LIST },
645 { "dns-server", 6, OT_ADDR_LIST },
646 { "log-server", 7, OT_ADDR_LIST },
647 { "lpr-server", 9, OT_ADDR_LIST },
648 { "hostname", 12, OT_INTERNAL | OT_NAME },
649 { "boot-file-size", 13, 2 | OT_DEC },
650 { "domain-name", 15, OT_NAME },
651 { "swap-server", 16, OT_ADDR_LIST },
652 { "root-path", 17, OT_NAME },
653 { "extension-path", 18, OT_NAME },
654 { "ip-forward-enable", 19, 1 },
655 { "non-local-source-routing", 20, 1 },
656 { "policy-filter", 21, OT_ADDR_LIST },
657 { "max-datagram-reassembly", 22, 2 | OT_DEC },
658 { "default-ttl", 23, 1 | OT_DEC },
659 { "mtu", 26, 2 | OT_DEC },
660 { "all-subnets-local", 27, 1 },
661 { "broadcast", 28, OT_INTERNAL | OT_ADDR_LIST },
662 { "router-discovery", 31, 1 },
663 { "router-solicitation", 32, OT_ADDR_LIST },
664 { "static-route", 33, OT_ADDR_LIST },
665 { "trailer-encapsulation", 34, 1 },
666 { "arp-timeout", 35, 4 | OT_DEC },
667 { "ethernet-encap", 36, 1 },
668 { "tcp-ttl", 37, 1 },
669 { "tcp-keepalive", 38, 4 | OT_DEC },
670 { "nis-domain", 40, OT_NAME },
671 { "nis-server", 41, OT_ADDR_LIST },
672 { "ntp-server", 42, OT_ADDR_LIST },
673 { "vendor-encap", 43, OT_INTERNAL },
674 { "netbios-ns", 44, OT_ADDR_LIST },
675 { "netbios-dd", 45, OT_ADDR_LIST },
676 { "netbios-nodetype", 46, 1 },
677 { "netbios-scope", 47, 0 },
678 { "x-windows-fs", 48, OT_ADDR_LIST },
679 { "x-windows-dm", 49, OT_ADDR_LIST },
680 { "requested-address", 50, OT_INTERNAL | OT_ADDR_LIST },
681 { "lease-time", 51, OT_INTERNAL | OT_TIME },
682 { "option-overload", 52, OT_INTERNAL },
683 { "message-type", 53, OT_INTERNAL | OT_DEC },
684 { "server-identifier", 54, OT_INTERNAL | OT_ADDR_LIST },
685 { "parameter-request", 55, OT_INTERNAL },
686 { "message", 56, OT_INTERNAL },
687 { "max-message-size", 57, OT_INTERNAL },
688 { "T1", 58, OT_TIME},
689 { "T2", 59, OT_TIME},
690 { "vendor-class", 60, 0 },
691 { "client-id", 61, OT_INTERNAL },
692 { "nis+-domain", 64, OT_NAME },
693 { "nis+-server", 65, OT_ADDR_LIST },
694 { "tftp-server", 66, OT_NAME },
695 { "bootfile-name", 67, OT_NAME },
696 { "mobile-ip-home", 68, OT_ADDR_LIST },
697 { "smtp-server", 69, OT_ADDR_LIST },
698 { "pop3-server", 70, OT_ADDR_LIST },
699 { "nntp-server", 71, OT_ADDR_LIST },
700 { "irc-server", 74, OT_ADDR_LIST },
701 { "user-class", 77, 0 },
702 { "rapid-commit", 80, 0 },
703 { "FQDN", 81, OT_INTERNAL },
704 { "agent-info", 82, OT_INTERNAL },
705 { "last-transaction", 91, 4 | OT_TIME },
706 { "associated-ip", 92, OT_ADDR_LIST },
707 { "client-arch", 93, 2 | OT_DEC },
708 { "client-interface-id", 94, 0 },
709 { "client-machine-id", 97, 0 },
710 { "posix-timezone", 100, OT_NAME }, /* RFC 4833, Sec. 2 */
711 { "tzdb-timezone", 101, OT_NAME }, /* RFC 4833, Sec. 2 */
712 { "ipv6-only", 108, 4 | OT_DEC }, /* RFC 8925 */
713 { "captive-portal", 114, OT_NAME }, /* RFC 8910 */
714 { "subnet-select", 118, OT_INTERNAL },
715 { "domain-search", 119, OT_RFC1035_NAME },
716 { "sip-server", 120, 0 },
717 { "classless-static-route", 121, 0 },
718 { "vendor-id-encap", 125, 0 },
719 { "tftp-server-address", 150, OT_ADDR_LIST },
720 { "server-ip-address", 255, OT_ADDR_LIST }, /* special, internal only, sets siaddr */
721 { NULL, 0, 0 }
722 };
723
724 #ifdef HAVE_DHCP6
725 static const struct opttab_t opttab6[] = {
726 { "client-id", 1, OT_INTERNAL },
727 { "server-id", 2, OT_INTERNAL },
728 { "ia-na", 3, OT_INTERNAL },
729 { "ia-ta", 4, OT_INTERNAL },
730 { "iaaddr", 5, OT_INTERNAL },
731 { "oro", 6, OT_INTERNAL },
732 { "preference", 7, OT_INTERNAL | OT_DEC },
733 { "unicast", 12, OT_INTERNAL },
734 { "status", 13, OT_INTERNAL },
735 { "rapid-commit", 14, OT_INTERNAL },
736 { "user-class", 15, OT_INTERNAL | OT_CSTRING },
737 { "vendor-class", 16, OT_INTERNAL | OT_CSTRING },
738 { "vendor-opts", 17, OT_INTERNAL },
739 { "sip-server-domain", 21, OT_RFC1035_NAME },
740 { "sip-server", 22, OT_ADDR_LIST },
741 { "dns-server", 23, OT_ADDR_LIST },
742 { "domain-search", 24, OT_RFC1035_NAME },
743 { "nis-server", 27, OT_ADDR_LIST },
744 { "nis+-server", 28, OT_ADDR_LIST },
745 { "nis-domain", 29, OT_RFC1035_NAME },
746 { "nis+-domain", 30, OT_RFC1035_NAME },
747 { "sntp-server", 31, OT_ADDR_LIST },
748 { "information-refresh-time", 32, OT_TIME },
749 { "FQDN", 39, OT_INTERNAL | OT_RFC1035_NAME },
750 { "posix-timezone", 41, OT_NAME }, /* RFC 4833, Sec. 3 */
751 { "tzdb-timezone", 42, OT_NAME }, /* RFC 4833, Sec. 3 */
752 { "ntp-server", 56, 0 /* OT_ADDR_LIST | OT_RFC1035_NAME */ },
753 { "bootfile-url", 59, OT_NAME },
754 { "bootfile-param", 60, OT_CSTRING },
755 { "captive-portal", 103, OT_NAME }, /* RFC 8910 */
756 { NULL, 0, 0 }
757 };
758 #endif
759
760
761
762 void display_opts(void)
763 {
764 int i;
765
766 printf(_("Known DHCP options:\n"));
767
768 for (i = 0; opttab[i].name; i++)
769 if (!(opttab[i].size & OT_INTERNAL))
770 printf("%3d %s\n", opttab[i].val, opttab[i].name);
771 }
772
773 #ifdef HAVE_DHCP6
774 void display_opts6(void)
775 {
776 int i;
777 printf(_("Known DHCPv6 options:\n"));
778
779 for (i = 0; opttab6[i].name; i++)
780 if (!(opttab6[i].size & OT_INTERNAL))
781 printf("%3d %s\n", opttab6[i].val, opttab6[i].name);
782 }
783 #endif
784
785 int lookup_dhcp_opt(int prot, char *name)
786 {
787 const struct opttab_t *t;
788 int i;
789
790 (void)prot;
791
792 #ifdef HAVE_DHCP6
793 if (prot == AF_INET6)
794 t = opttab6;
795 else
796 #endif
797 t = opttab;
798
799 for (i = 0; t[i].name; i++)
800 if (strcasecmp(t[i].name, name) == 0)
801 return t[i].val;
802
803 return -1;
804 }
805
806 int lookup_dhcp_len(int prot, int val)
807 {
808 const struct opttab_t *t;
809 int i;
810
811 (void)prot;
812
813 #ifdef HAVE_DHCP6
814 if (prot == AF_INET6)
815 t = opttab6;
816 else
817 #endif
818 t = opttab;
819
820 for (i = 0; t[i].name; i++)
821 if (val == t[i].val)
822 return t[i].size & ~OT_DEC;
823
824 return 0;
825 }
826
827 char *option_string(int prot, unsigned int opt, unsigned char *val, int opt_len, char *buf, int buf_len)
828 {
829 int o, i, j, nodecode = 0;
830 const struct opttab_t *ot = opttab;
831
832 #ifdef HAVE_DHCP6
833 if (prot == AF_INET6)
834 ot = opttab6;
835 #endif
836
837 for (o = 0; ot[o].name; o++)
838 if (ot[o].val == opt)
839 {
840 if (buf)
841 {
842 memset(buf, 0, buf_len);
843
844 if (ot[o].size & OT_ADDR_LIST)
845 {
846 union all_addr addr;
847 int addr_len = INADDRSZ;
848
849 #ifdef HAVE_DHCP6
850 if (prot == AF_INET6)
851 addr_len = IN6ADDRSZ;
852 #endif
853 for (buf[0]= 0, i = 0; i <= opt_len - addr_len; i += addr_len)
854 {
855 if (i != 0)
856 strncat(buf, ", ", buf_len - strlen(buf));
857 /* align */
858 memcpy(&addr, &val[i], addr_len);
859 inet_ntop(prot, &val[i], daemon->addrbuff, ADDRSTRLEN);
860 strncat(buf, daemon->addrbuff, buf_len - strlen(buf));
861 }
862 }
863 else if (ot[o].size & OT_NAME)
864 for (i = 0, j = 0; i < opt_len && j < buf_len ; i++)
865 {
866 char c = val[i];
867 if (isprint((unsigned char)c))
868 buf[j++] = c;
869 }
870 #ifdef HAVE_DHCP6
871 /* We don't handle compressed rfc1035 names, so no good in IPv4 land */
872 else if ((ot[o].size & OT_RFC1035_NAME) && prot == AF_INET6)
873 {
874 i = 0, j = 0;
875 while (i < opt_len && val[i] != 0)
876 {
877 int k, l = i + val[i] + 1;
878 for (k = i + 1; k < opt_len && k < l && j < buf_len ; k++)
879 {
880 char c = val[k];
881 if (isprint((unsigned char)c))
882 buf[j++] = c;
883 }
884 i = l;
885 if (i < opt_len && val[i] != 0 && j < buf_len)
886 buf[j++] = '.';
887 }
888 }
889 else if ((ot[o].size & OT_CSTRING))
890 {
891 int k, len;
892 unsigned char *p;
893
894 i = 0, j = 0;
895 while (i + 2 <= opt_len)
896 {
897 p = &val[i];
898 GETSHORT(len, p);
899 if (i + 2 + len > opt_len)
900 break; /* malformed: body extends beyond option */
901 for (k = 0; k < len && j < buf_len; k++)
902 {
903 char c = *p++;
904 if (isprint((unsigned char)c))
905 buf[j++] = c;
906 }
907 i += len + 2;
908 if (i < opt_len && j < buf_len)
909 buf[j++] = ',';
910 }
911 }
912 #endif
913 else if ((ot[o].size & (OT_DEC | OT_TIME)) && opt_len != 0)
914 {
915 unsigned int dec = 0;
916
917 for (i = 0; i < opt_len; i++)
918 dec = (dec << 8) | val[i];
919
920 if (ot[o].size & OT_TIME)
921 prettyprint_time(buf, dec);
922 else
923 sprintf(buf, "%u", dec);
924 }
925 else
926 nodecode = 1;
927 }
928 break;
929 }
930
931 if (opt_len != 0 && buf && (!ot[o].name || nodecode))
932 {
933 int trunc = 0;
934 if (opt_len > 14)
935 {
936 trunc = 1;
937 opt_len = 14;
938 }
939
940 *buf = 0;
941 strncat(buf, print_mac(val, opt_len), buf_len);
942 if (trunc)
943 strncat(buf, "...", buf_len - strlen(buf));
944 }
945
946 return ot[o].name ? ot[o].name : "";
947
948 }
949
950 void log_context(int family, struct dhcp_context *context)
951 {
952 /* Cannot use dhcp_buff* for RA contexts */
953
954 void *start = &context->start;
955 void *end = &context->end;
956 char *template = "", *p = daemon->namebuff;
957
958 *p = 0;
959
960 #ifdef HAVE_DHCP6
961 if (family == AF_INET6)
962 {
963 struct in6_addr subnet = context->start6;
964 if (!(context->flags & CONTEXT_TEMPLATE))
965 setaddr6part(&subnet, 0);
966 inet_ntop(AF_INET6, &subnet, daemon->addrbuff, ADDRSTRLEN);
967 start = &context->start6;
968 end = &context->end6;
969 }
970 #endif
971
972 if (family != AF_INET && (context->flags & CONTEXT_DEPRECATE))
973 strcpy(daemon->namebuff, _(", prefix deprecated"));
974 else
975 {
976 p += sprintf(p, _(", lease time "));
977 prettyprint_time(p, context->lease_time);
978 p += strlen(p);
979 }
980
981 #ifdef HAVE_DHCP6
982 if (context->flags & CONTEXT_CONSTRUCTED)
983 {
984 char ifrn_name[IFNAMSIZ];
985
986 template = p;
987 p += sprintf(p, ", ");
988
989 if (indextoname(daemon->icmp6fd, context->if_index, ifrn_name))
990 sprintf(p, "%s for %s", (context->flags & CONTEXT_OLD) ? "old prefix" : "constructed", ifrn_name);
991 }
992 else if (context->flags & CONTEXT_TEMPLATE && !(context->flags & CONTEXT_RA_STATELESS))
993 {
994 template = p;
995 p += sprintf(p, ", ");
996
997 sprintf(p, "template for %s", context->template_interface);
998 }
999 #endif
1000
1001 if (!(context->flags & CONTEXT_OLD) &&
1002 ((context->flags & CONTEXT_DHCP) || family == AF_INET))
1003 {
1004 #ifdef HAVE_DHCP6
1005 if (context->flags & CONTEXT_RA_STATELESS)
1006 {
1007 if (context->flags & CONTEXT_TEMPLATE)
1008 strncpy(daemon->dhcp_buff, context->template_interface, DHCP_BUFF_SZ);
1009 else
1010 strcpy(daemon->dhcp_buff, daemon->addrbuff);
1011 }
1012 else
1013 #endif
1014 inet_ntop(family, start, daemon->dhcp_buff, DHCP_BUFF_SZ);
1015 inet_ntop(family, end, daemon->dhcp_buff3, DHCP_BUFF_SZ);
1016 my_syslog(MS_DHCP | LOG_INFO,
1017 (context->flags & CONTEXT_RA_STATELESS) ?
1018 _("%s stateless on %s%.0s%.0s%s") :
1019 (context->flags & CONTEXT_STATIC) ?
1020 _("%s, static leases only on %.0s%s%s%.0s") :
1021 (context->flags & CONTEXT_PROXY) ?
1022 _("%s, proxy on subnet %.0s%s%.0s%.0s") :
1023 _("%s, IP range %s -- %s%s%.0s"),
1024 (family != AF_INET) ? "DHCPv6" : "DHCP",
1025 daemon->dhcp_buff, daemon->dhcp_buff3, daemon->namebuff, template);
1026 }
1027
1028 #ifdef HAVE_DHCP6
1029 if (context->flags & CONTEXT_TEMPLATE)
1030 {
1031 strcpy(daemon->addrbuff, context->template_interface);
1032 template = "";
1033 }
1034
1035 if ((context->flags & CONTEXT_RA_NAME) && !(context->flags & CONTEXT_OLD))
1036 my_syslog(MS_DHCP | LOG_INFO, _("DHCPv4-derived IPv6 names on %s%s"), daemon->addrbuff, template);
1037
1038 if ((context->flags & CONTEXT_RA) || (option_bool(OPT_RA) && (context->flags & CONTEXT_DHCP) && family == AF_INET6))
1039 my_syslog(MS_DHCP | LOG_INFO, _("router advertisement on %s%s"), daemon->addrbuff, template);
1040 #endif
1041
1042 }
1043
1044 void log_relay(int family, struct dhcp_relay *relay)
1045 {
1046 int broadcast = relay->server.addr4.s_addr == 0;
1047 inet_ntop(family, &relay->local, daemon->addrbuff, ADDRSTRLEN);
1048 inet_ntop(family, &relay->server, daemon->namebuff, ADDRSTRLEN);
1049
1050 if (family == AF_INET && relay->port != DHCP_SERVER_PORT)
1051 sprintf(daemon->namebuff + strlen(daemon->namebuff), "#%u", relay->port);
1052
1053 #ifdef HAVE_DHCP6
1054 struct in6_addr multicast;
1055
1056 inet_pton(AF_INET6, ALL_SERVERS, &multicast);
1057
1058 if (family == AF_INET6)
1059 {
1060 broadcast = IN6_ARE_ADDR_EQUAL(&relay->server.addr6, &multicast);
1061 if (relay->port != DHCPV6_SERVER_PORT)
1062 sprintf(daemon->namebuff + strlen(daemon->namebuff), "#%u", relay->port);
1063 }
1064 #endif
1065
1066
1067 if (relay->interface)
1068 {
1069 if (broadcast)
1070 my_syslog(MS_DHCP | LOG_INFO, _("DHCP relay from %s via %s"), daemon->addrbuff, relay->interface);
1071 else if (relay->split_mode)
1072 my_syslog(MS_DHCP | LOG_INFO, _("DHCP split-relay from %s to %s via %s"), daemon->addrbuff, daemon->namebuff, relay->interface);
1073 else
1074 my_syslog(MS_DHCP | LOG_INFO, _("DHCP relay from %s to %s via %s"), daemon->addrbuff, daemon->namebuff, relay->interface);
1075 }
1076 else
1077 my_syslog(MS_DHCP | LOG_INFO, _("DHCP relay from %s to %s"), daemon->addrbuff, daemon->namebuff);
1078 }
1079
1080 #endif