1: /* dnsmasq is Copyright (c) 2000-2016 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, 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 i, 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 void do_options(struct dhcp_context *context,
44: struct dhcp_packet *mess,
45: unsigned char *real_end,
46: unsigned char *req_options,
47: char *hostname,
48: char *config_domain,
49: struct dhcp_netid *netid,
50: struct in_addr subnet_addr,
51: unsigned char fqdn_flags,
52: int null_term, int pxearch,
53: unsigned char *uuid,
54: int vendor_class_len,
55: time_t now,
56: unsigned int lease_time,
57: unsigned short fuzz);
58:
59:
60: static void match_vendor_opts(unsigned char *opt, struct dhcp_opt *dopt);
61: static int do_encap_opts(struct dhcp_opt *opts, int encap, int flag, struct dhcp_packet *mess, unsigned char *end, int null_term);
62: static void pxe_misc(struct dhcp_packet *mess, unsigned char *end, unsigned char *uuid);
63: static int prune_vendor_opts(struct dhcp_netid *netid);
64: static struct dhcp_opt *pxe_opts(int pxe_arch, struct dhcp_netid *netid, struct in_addr local, time_t now);
65: struct dhcp_boot *find_boot(struct dhcp_netid *netid);
66: 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);
67:
68: size_t dhcp_reply(struct dhcp_context *context, char *iface_name, int int_index,
69: size_t sz, time_t now, int unicast_dest, int *is_inform, int pxe, struct in_addr fallback)
70: {
71: unsigned char *opt, *clid = NULL;
72: struct dhcp_lease *ltmp, *lease = NULL;
73: struct dhcp_vendor *vendor;
74: struct dhcp_mac *mac;
75: struct dhcp_netid_list *id_list;
76: int clid_len = 0, ignore = 0, do_classes = 0, selecting = 0, pxearch = -1;
77: struct dhcp_packet *mess = (struct dhcp_packet *)daemon->dhcp_packet.iov_base;
78: unsigned char *end = (unsigned char *)(mess + 1);
79: unsigned char *real_end = (unsigned char *)(mess + 1);
80: char *hostname = NULL, *offer_hostname = NULL, *client_hostname = NULL, *domain = NULL;
81: int hostname_auth = 0, borken_opt = 0;
82: unsigned char *req_options = NULL;
83: char *message = NULL;
84: unsigned int time;
85: struct dhcp_config *config;
86: struct dhcp_netid *netid, *tagif_netid;
87: struct in_addr subnet_addr, override;
88: unsigned short fuzz = 0;
89: unsigned int mess_type = 0;
90: unsigned char fqdn_flags = 0;
91: unsigned char *agent_id = NULL, *uuid = NULL;
92: unsigned char *emac = NULL;
93: int vendor_class_len = 0, emac_len = 0;
94: struct dhcp_netid known_id, iface_id, cpewan_id;
95: struct dhcp_opt *o;
96: unsigned char pxe_uuid[17];
97: unsigned char *oui = NULL, *serial = NULL;
98: #ifdef HAVE_SCRIPT
99: unsigned char *class = NULL;
100: #endif
101:
102: subnet_addr.s_addr = override.s_addr = 0;
103:
104: /* set tag with name == interface */
105: iface_id.net = iface_name;
106: iface_id.next = NULL;
107: netid = &iface_id;
108:
109: if (mess->op != BOOTREQUEST || mess->hlen > DHCP_CHADDR_MAX)
110: return 0;
111:
112: if (mess->htype == 0 && mess->hlen != 0)
113: return 0;
114:
115: /* check for DHCP rather than BOOTP */
116: if ((opt = option_find(mess, sz, OPTION_MESSAGE_TYPE, 1)))
117: {
118: u32 cookie = htonl(DHCP_COOKIE);
119:
120: /* only insist on a cookie for DHCP. */
121: if (memcmp(mess->options, &cookie, sizeof(u32)) != 0)
122: return 0;
123:
124: mess_type = option_uint(opt, 0, 1);
125:
126: /* two things to note here: expand_buf may move the packet,
127: so reassign mess from daemon->packet. Also, the size
128: sent includes the IP and UDP headers, hence the magic "-28" */
129: if ((opt = option_find(mess, sz, OPTION_MAXMESSAGE, 2)))
130: {
131: size_t size = (size_t)option_uint(opt, 0, 2) - 28;
132:
133: if (size > DHCP_PACKET_MAX)
134: size = DHCP_PACKET_MAX;
135: else if (size < sizeof(struct dhcp_packet))
136: size = sizeof(struct dhcp_packet);
137:
138: if (expand_buf(&daemon->dhcp_packet, size))
139: {
140: mess = (struct dhcp_packet *)daemon->dhcp_packet.iov_base;
141: real_end = end = ((unsigned char *)mess) + size;
142: }
143: }
144:
145: /* Some buggy clients set ciaddr when they shouldn't, so clear that here since
146: it can affect the context-determination code. */
147: if ((option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ) || mess_type == DHCPDISCOVER))
148: mess->ciaddr.s_addr = 0;
149:
150: /* search for device identity from CPEWAN devices, we pass this through to the script */
151: if ((opt = option_find(mess, sz, OPTION_VENDOR_IDENT_OPT, 5)))
152: {
153: unsigned int elen, offset, len = option_len(opt);
154:
155: for (offset = 0; offset < (len - 5); offset += elen + 5)
156: {
157: elen = option_uint(opt, offset + 4 , 1);
158: if (option_uint(opt, offset, 4) == BRDBAND_FORUM_IANA)
159: {
160: unsigned char *x = option_ptr(opt, offset + 5);
161: unsigned char *y = option_ptr(opt, offset + elen + 5);
162: oui = option_find1(x, y, 1, 1);
163: serial = option_find1(x, y, 2, 1);
164: #ifdef HAVE_SCRIPT
165: class = option_find1(x, y, 3, 1);
166: #endif
167: /* If TR069-id is present set the tag "cpewan-id" to facilitate echoing
168: the gateway id back. Note that the device class is optional */
169: if (oui && serial)
170: {
171: cpewan_id.net = "cpewan-id";
172: cpewan_id.next = netid;
173: netid = &cpewan_id;
174: }
175: break;
176: }
177: }
178: }
179:
180: if ((opt = option_find(mess, sz, OPTION_AGENT_ID, 1)))
181: {
182: /* Any agent-id needs to be copied back out, verbatim, as the last option
183: in the packet. Here, we shift it to the very end of the buffer, if it doesn't
184: get overwritten, then it will be shuffled back at the end of processing.
185: Note that the incoming options must not be overwritten here, so there has to
186: be enough free space at the end of the packet to copy the option. */
187: unsigned char *sopt;
188: unsigned int total = option_len(opt) + 2;
189: unsigned char *last_opt = option_find(mess, sz, OPTION_END, 0);
190: if (last_opt && last_opt < end - total)
191: {
192: end -= total;
193: agent_id = end;
194: memcpy(agent_id, opt, total);
195: }
196:
197: /* look for RFC3527 Link selection sub-option */
198: if ((sopt = option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_SUBNET_SELECT, INADDRSZ)))
199: subnet_addr = option_addr(sopt);
200:
201: /* look for RFC5107 server-identifier-override */
202: if ((sopt = option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_SERVER_OR, INADDRSZ)))
203: override = option_addr(sopt);
204:
205: /* if a circuit-id or remote-is option is provided, exact-match to options. */
206: for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next)
207: {
208: int search;
209:
210: if (vendor->match_type == MATCH_CIRCUIT)
211: search = SUBOPT_CIRCUIT_ID;
212: else if (vendor->match_type == MATCH_REMOTE)
213: search = SUBOPT_REMOTE_ID;
214: else if (vendor->match_type == MATCH_SUBSCRIBER)
215: search = SUBOPT_SUBSCR_ID;
216: else
217: continue;
218:
219: if ((sopt = option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), search, 1)) &&
220: vendor->len == option_len(sopt) &&
221: memcmp(option_ptr(sopt, 0), vendor->data, vendor->len) == 0)
222: {
223: vendor->netid.next = netid;
224: netid = &vendor->netid;
225: }
226: }
227: }
228:
229: /* Check for RFC3011 subnet selector - only if RFC3527 one not present */
230: if (subnet_addr.s_addr == 0 && (opt = option_find(mess, sz, OPTION_SUBNET_SELECT, INADDRSZ)))
231: subnet_addr = option_addr(opt);
232:
233: /* If there is no client identifier option, use the hardware address */
234: if ((opt = option_find(mess, sz, OPTION_CLIENT_ID, 1)))
235: {
236: clid_len = option_len(opt);
237: clid = option_ptr(opt, 0);
238: }
239:
240: /* do we have a lease in store? */
241: lease = lease_find_by_client(mess->chaddr, mess->hlen, mess->htype, clid, clid_len);
242:
243: /* If this request is missing a clid, but we've seen one before,
244: use it again for option matching etc. */
245: if (lease && !clid && lease->clid)
246: {
247: clid_len = lease->clid_len;
248: clid = lease->clid;
249: }
250:
251: /* find mac to use for logging and hashing */
252: emac = extended_hwaddr(mess->htype, mess->hlen, mess->chaddr, clid_len, clid, &emac_len);
253: }
254:
255: for (mac = daemon->dhcp_macs; mac; mac = mac->next)
256: if (mac->hwaddr_len == mess->hlen &&
257: (mac->hwaddr_type == mess->htype || mac->hwaddr_type == 0) &&
258: memcmp_masked(mac->hwaddr, mess->chaddr, mess->hlen, mac->mask))
259: {
260: mac->netid.next = netid;
261: netid = &mac->netid;
262: }
263:
264: /* Determine network for this packet. Our caller will have already linked all the
265: contexts which match the addresses of the receiving interface but if the
266: machine has an address already, or came via a relay, or we have a subnet selector,
267: we search again. If we don't have have a giaddr or explicit subnet selector,
268: use the ciaddr. This is necessary because a machine which got a lease via a
269: relay won't use the relay to renew. If matching a ciaddr fails but we have a context
270: from the physical network, continue using that to allow correct DHCPNAK generation later. */
271: if (mess->giaddr.s_addr || subnet_addr.s_addr || mess->ciaddr.s_addr)
272: {
273: struct dhcp_context *context_tmp, *context_new = NULL;
274: struct in_addr addr;
275: int force = 0;
276:
277: if (subnet_addr.s_addr)
278: {
279: addr = subnet_addr;
280: force = 1;
281: }
282: else if (mess->giaddr.s_addr)
283: {
284: addr = mess->giaddr;
285: force = 1;
286: }
287: else
288: {
289: /* If ciaddr is in the hardware derived set of contexts, leave that unchanged */
290: addr = mess->ciaddr;
291: for (context_tmp = context; context_tmp; context_tmp = context_tmp->current)
292: if (context_tmp->netmask.s_addr &&
293: is_same_net(addr, context_tmp->start, context_tmp->netmask) &&
294: is_same_net(addr, context_tmp->end, context_tmp->netmask))
295: {
296: context_new = context;
297: break;
298: }
299: }
300:
301: if (!context_new)
302: for (context_tmp = daemon->dhcp; context_tmp; context_tmp = context_tmp->next)
303: {
304: struct in_addr netmask = context_tmp->netmask;
305:
306: /* guess the netmask for relayed networks */
307: if (!(context_tmp->flags & CONTEXT_NETMASK) && context_tmp->netmask.s_addr == 0)
308: {
309: if (IN_CLASSA(ntohl(context_tmp->start.s_addr)) && IN_CLASSA(ntohl(context_tmp->end.s_addr)))
310: netmask.s_addr = htonl(0xff000000);
311: else if (IN_CLASSB(ntohl(context_tmp->start.s_addr)) && IN_CLASSB(ntohl(context_tmp->end.s_addr)))
312: netmask.s_addr = htonl(0xffff0000);
313: else if (IN_CLASSC(ntohl(context_tmp->start.s_addr)) && IN_CLASSC(ntohl(context_tmp->end.s_addr)))
314: netmask.s_addr = htonl(0xffffff00);
315: }
316:
317: /* This section fills in context mainly when a client which is on a remote (relayed)
318: network renews a lease without using the relay, after dnsmasq has restarted. */
319: if (netmask.s_addr != 0 &&
320: is_same_net(addr, context_tmp->start, netmask) &&
321: is_same_net(addr, context_tmp->end, netmask))
322: {
323: context_tmp->netmask = netmask;
324: if (context_tmp->local.s_addr == 0)
325: context_tmp->local = fallback;
326: if (context_tmp->router.s_addr == 0)
327: context_tmp->router = mess->giaddr;
328:
329: /* fill in missing broadcast addresses for relayed ranges */
330: if (!(context_tmp->flags & CONTEXT_BRDCAST) && context_tmp->broadcast.s_addr == 0 )
331: context_tmp->broadcast.s_addr = context_tmp->start.s_addr | ~context_tmp->netmask.s_addr;
332:
333: context_tmp->current = context_new;
334: context_new = context_tmp;
335: }
336: }
337:
338: if (context_new || force)
339: context = context_new;
340: }
341:
342: if (!context)
343: {
344: my_syslog(MS_DHCP | LOG_WARNING, _("no address range available for DHCP request %s %s"),
345: subnet_addr.s_addr ? _("with subnet selector") : _("via"),
346: subnet_addr.s_addr ? inet_ntoa(subnet_addr) : (mess->giaddr.s_addr ? inet_ntoa(mess->giaddr) : iface_name));
347: return 0;
348: }
349:
350: if (option_bool(OPT_LOG_OPTS))
351: {
352: struct dhcp_context *context_tmp;
353: for (context_tmp = context; context_tmp; context_tmp = context_tmp->current)
354: {
355: strcpy(daemon->namebuff, inet_ntoa(context_tmp->start));
356: if (context_tmp->flags & (CONTEXT_STATIC | CONTEXT_PROXY))
357: my_syslog(MS_DHCP | LOG_INFO, _("%u available DHCP subnet: %s/%s"),
358: ntohl(mess->xid), daemon->namebuff, inet_ntoa(context_tmp->netmask));
359: else
360: my_syslog(MS_DHCP | LOG_INFO, _("%u available DHCP range: %s -- %s"),
361: ntohl(mess->xid), daemon->namebuff, inet_ntoa(context_tmp->end));
362: }
363: }
364:
365: /* dhcp-match. If we have hex-and-wildcards, look for a left-anchored match.
366: Otherwise assume the option is an array, and look for a matching element.
367: If no data given, existance of the option is enough. This code handles
368: rfc3925 V-I classes too. */
369: for (o = daemon->dhcp_match; o; o = o->next)
370: {
371: unsigned int len, elen, match = 0;
372: size_t offset, o2;
373:
374: if (o->flags & DHOPT_RFC3925)
375: {
376: if (!(opt = option_find(mess, sz, OPTION_VENDOR_IDENT, 5)))
377: continue;
378:
379: for (offset = 0; offset < (option_len(opt) - 5u); offset += len + 5)
380: {
381: len = option_uint(opt, offset + 4 , 1);
382: /* Need to take care that bad data can't run us off the end of the packet */
383: if ((offset + len + 5 <= (option_len(opt))) &&
384: (option_uint(opt, offset, 4) == (unsigned int)o->u.encap))
385: for (o2 = offset + 5; o2 < offset + len + 5; o2 += elen + 1)
386: {
387: elen = option_uint(opt, o2, 1);
388: if ((o2 + elen + 1 <= option_len(opt)) &&
389: (match = match_bytes(o, option_ptr(opt, o2 + 1), elen)))
390: break;
391: }
392: if (match)
393: break;
394: }
395: }
396: else
397: {
398: if (!(opt = option_find(mess, sz, o->opt, 1)))
399: continue;
400:
401: match = match_bytes(o, option_ptr(opt, 0), option_len(opt));
402: }
403:
404: if (match)
405: {
406: o->netid->next = netid;
407: netid = o->netid;
408: }
409: }
410:
411: /* user-class options are, according to RFC3004, supposed to contain
412: a set of counted strings. Here we check that this is so (by seeing
413: if the counts are consistent with the overall option length) and if
414: so zero the counts so that we don't get spurious matches between
415: the vendor string and the counts. If the lengths don't add up, we
416: assume that the option is a single string and non RFC3004 compliant
417: and just do the substring match. dhclient provides these broken options.
418: The code, later, which sends user-class data to the lease-change script
419: relies on the transformation done here.
420: */
421:
422: if ((opt = option_find(mess, sz, OPTION_USER_CLASS, 1)))
423: {
424: unsigned char *ucp = option_ptr(opt, 0);
425: int tmp, j;
426: for (j = 0; j < option_len(opt); j += ucp[j] + 1);
427: if (j == option_len(opt))
428: for (j = 0; j < option_len(opt); j = tmp)
429: {
430: tmp = j + ucp[j] + 1;
431: ucp[j] = 0;
432: }
433: }
434:
435: for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next)
436: {
437: int mopt;
438:
439: if (vendor->match_type == MATCH_VENDOR)
440: mopt = OPTION_VENDOR_ID;
441: else if (vendor->match_type == MATCH_USER)
442: mopt = OPTION_USER_CLASS;
443: else
444: continue;
445:
446: if ((opt = option_find(mess, sz, mopt, 1)))
447: {
448: int i;
449: for (i = 0; i <= (option_len(opt) - vendor->len); i++)
450: if (memcmp(vendor->data, option_ptr(opt, i), vendor->len) == 0)
451: {
452: vendor->netid.next = netid;
453: netid = &vendor->netid;
454: break;
455: }
456: }
457: }
458:
459: /* mark vendor-encapsulated options which match the client-supplied vendor class,
460: save client-supplied vendor class */
461: if ((opt = option_find(mess, sz, OPTION_VENDOR_ID, 1)))
462: {
463: memcpy(daemon->dhcp_buff3, option_ptr(opt, 0), option_len(opt));
464: vendor_class_len = option_len(opt);
465: }
466: match_vendor_opts(opt, daemon->dhcp_opts);
467:
468: if (option_bool(OPT_LOG_OPTS))
469: {
470: if (sanitise(opt, daemon->namebuff))
471: my_syslog(MS_DHCP | LOG_INFO, _("%u vendor class: %s"), ntohl(mess->xid), daemon->namebuff);
472: if (sanitise(option_find(mess, sz, OPTION_USER_CLASS, 1), daemon->namebuff))
473: my_syslog(MS_DHCP | LOG_INFO, _("%u user class: %s"), ntohl(mess->xid), daemon->namebuff);
474: }
475:
476: mess->op = BOOTREPLY;
477:
478: config = find_config(daemon->dhcp_conf, context, clid, clid_len,
479: mess->chaddr, mess->hlen, mess->htype, NULL);
480:
481: /* set "known" tag for known hosts */
482: if (config)
483: {
484: known_id.net = "known";
485: known_id.next = netid;
486: netid = &known_id;
487: }
488:
489: if (mess_type == 0 && !pxe)
490: {
491: /* BOOTP request */
492: struct dhcp_netid id, bootp_id;
493: struct in_addr *logaddr = NULL;
494:
495: /* must have a MAC addr for bootp */
496: if (mess->htype == 0 || mess->hlen == 0 || (context->flags & CONTEXT_PROXY))
497: return 0;
498:
499: if (have_config(config, CONFIG_DISABLE))
500: message = _("disabled");
501:
502: end = mess->options + 64; /* BOOTP vend area is only 64 bytes */
503:
504: if (have_config(config, CONFIG_NAME))
505: {
506: hostname = config->hostname;
507: domain = config->domain;
508: }
509:
510: if (config)
511: {
512: struct dhcp_netid_list *list;
513:
514: for (list = config->netid; list; list = list->next)
515: {
516: list->list->next = netid;
517: netid = list->list;
518: }
519: }
520:
521: /* Match incoming filename field as a netid. */
522: if (mess->file[0])
523: {
524: memcpy(daemon->dhcp_buff2, mess->file, sizeof(mess->file));
525: daemon->dhcp_buff2[sizeof(mess->file) + 1] = 0; /* ensure zero term. */
526: id.net = (char *)daemon->dhcp_buff2;
527: id.next = netid;
528: netid = &id;
529: }
530:
531: /* Add "bootp" as a tag to allow different options, address ranges etc
532: for BOOTP clients */
533: bootp_id.net = "bootp";
534: bootp_id.next = netid;
535: netid = &bootp_id;
536:
537: tagif_netid = run_tag_if(netid);
538:
539: for (id_list = daemon->dhcp_ignore; id_list; id_list = id_list->next)
540: if (match_netid(id_list->list, tagif_netid, 0))
541: message = _("ignored");
542:
543: if (!message)
544: {
545: int nailed = 0;
546:
547: if (have_config(config, CONFIG_ADDR))
548: {
549: nailed = 1;
550: logaddr = &config->addr;
551: mess->yiaddr = config->addr;
552: if ((lease = lease_find_by_addr(config->addr)) &&
553: (lease->hwaddr_len != mess->hlen ||
554: lease->hwaddr_type != mess->htype ||
555: memcmp(lease->hwaddr, mess->chaddr, lease->hwaddr_len) != 0))
556: message = _("address in use");
557: }
558: else
559: {
560: if (!(lease = lease_find_by_client(mess->chaddr, mess->hlen, mess->htype, NULL, 0)) ||
561: !address_available(context, lease->addr, tagif_netid))
562: {
563: if (lease)
564: {
565: /* lease exists, wrong network. */
566: lease_prune(lease, now);
567: lease = NULL;
568: }
569: if (!address_allocate(context, &mess->yiaddr, mess->chaddr, mess->hlen, tagif_netid, now))
570: message = _("no address available");
571: }
572: else
573: mess->yiaddr = lease->addr;
574: }
575:
576: if (!message && !(context = narrow_context(context, mess->yiaddr, netid)))
577: message = _("wrong network");
578: else if (context->netid.net)
579: {
580: context->netid.next = netid;
581: tagif_netid = run_tag_if(&context->netid);
582: }
583:
584: log_tags(tagif_netid, ntohl(mess->xid));
585:
586: if (!message && !nailed)
587: {
588: for (id_list = daemon->bootp_dynamic; id_list; id_list = id_list->next)
589: if ((!id_list->list) || match_netid(id_list->list, tagif_netid, 0))
590: break;
591: if (!id_list)
592: message = _("no address configured");
593: }
594:
595: if (!message &&
596: !lease &&
597: (!(lease = lease4_allocate(mess->yiaddr))))
598: message = _("no leases left");
599:
600: if (!message)
601: {
602: logaddr = &mess->yiaddr;
603:
604: lease_set_hwaddr(lease, mess->chaddr, NULL, mess->hlen, mess->htype, 0, now, 1);
605: if (hostname)
606: lease_set_hostname(lease, hostname, 1, get_domain(lease->addr), domain);
607: /* infinite lease unless nailed in dhcp-host line. */
608: lease_set_expires(lease,
609: have_config(config, CONFIG_TIME) ? config->lease_time : 0xffffffff,
610: now);
611: lease_set_interface(lease, int_index, now);
612:
613: clear_packet(mess, end);
614: do_options(context, mess, end, NULL, hostname, get_domain(mess->yiaddr),
615: netid, subnet_addr, 0, 0, -1, NULL, vendor_class_len, now, 0xffffffff, 0);
616: }
617: }
618:
619: log_packet("BOOTP", logaddr, mess->chaddr, mess->hlen, iface_name, NULL, message, mess->xid);
620:
621: return message ? 0 : dhcp_packet_size(mess, agent_id, real_end);
622: }
623:
624: if ((opt = option_find(mess, sz, OPTION_CLIENT_FQDN, 3)))
625: {
626: /* http://tools.ietf.org/wg/dhc/draft-ietf-dhc-fqdn-option/draft-ietf-dhc-fqdn-option-10.txt */
627: int len = option_len(opt);
628: char *pq = daemon->dhcp_buff;
629: unsigned char *pp, *op = option_ptr(opt, 0);
630:
631: fqdn_flags = *op;
632: len -= 3;
633: op += 3;
634: pp = op;
635:
636: /* NB, the following always sets at least one bit */
637: if (option_bool(OPT_FQDN_UPDATE))
638: {
639: if (fqdn_flags & 0x01)
640: {
641: fqdn_flags |= 0x02; /* set O */
642: fqdn_flags &= ~0x01; /* clear S */
643: }
644: fqdn_flags |= 0x08; /* set N */
645: }
646: else
647: {
648: if (!(fqdn_flags & 0x01))
649: fqdn_flags |= 0x03; /* set S and O */
650: fqdn_flags &= ~0x08; /* clear N */
651: }
652:
653: if (fqdn_flags & 0x04)
654: while (*op != 0 && ((op + (*op)) - pp) < len)
655: {
656: memcpy(pq, op+1, *op);
657: pq += *op;
658: op += (*op)+1;
659: *(pq++) = '.';
660: }
661: else
662: {
663: memcpy(pq, op, len);
664: if (len > 0 && op[len-1] == 0)
665: borken_opt = 1;
666: pq += len + 1;
667: }
668:
669: if (pq != daemon->dhcp_buff)
670: pq--;
671:
672: *pq = 0;
673:
674: if (legal_hostname(daemon->dhcp_buff))
675: offer_hostname = client_hostname = daemon->dhcp_buff;
676: }
677: else if ((opt = option_find(mess, sz, OPTION_HOSTNAME, 1)))
678: {
679: int len = option_len(opt);
680: memcpy(daemon->dhcp_buff, option_ptr(opt, 0), len);
681: /* Microsoft clients are broken, and need zero-terminated strings
682: in options. We detect this state here, and do the same in
683: any options we send */
684: if (len > 0 && daemon->dhcp_buff[len-1] == 0)
685: borken_opt = 1;
686: else
687: daemon->dhcp_buff[len] = 0;
688: if (legal_hostname(daemon->dhcp_buff))
689: client_hostname = daemon->dhcp_buff;
690: }
691:
692: if (client_hostname && option_bool(OPT_LOG_OPTS))
693: my_syslog(MS_DHCP | LOG_INFO, _("%u client provides name: %s"), ntohl(mess->xid), client_hostname);
694:
695: if (have_config(config, CONFIG_NAME))
696: {
697: hostname = config->hostname;
698: domain = config->domain;
699: hostname_auth = 1;
700: /* be careful not to send an OFFER with a hostname not matching the DISCOVER. */
701: if (fqdn_flags != 0 || !client_hostname || hostname_isequal(hostname, client_hostname))
702: offer_hostname = hostname;
703: }
704: else if (client_hostname)
705: {
706: domain = strip_hostname(client_hostname);
707:
708: if (strlen(client_hostname) != 0)
709: {
710: hostname = client_hostname;
711: if (!config)
712: {
713: /* Search again now we have a hostname.
714: Only accept configs without CLID and HWADDR here, (they won't match)
715: to avoid impersonation by name. */
716: struct dhcp_config *new = find_config(daemon->dhcp_conf, context, NULL, 0,
717: mess->chaddr, mess->hlen,
718: mess->htype, hostname);
719: if (new && !have_config(new, CONFIG_CLID) && !new->hwaddr)
720: {
721: config = new;
722: /* set "known" tag for known hosts */
723: known_id.net = "known";
724: known_id.next = netid;
725: netid = &known_id;
726: }
727: }
728: }
729: }
730:
731: if (config)
732: {
733: struct dhcp_netid_list *list;
734:
735: for (list = config->netid; list; list = list->next)
736: {
737: list->list->next = netid;
738: netid = list->list;
739: }
740: }
741:
742: tagif_netid = run_tag_if(netid);
743:
744: /* if all the netids in the ignore list are present, ignore this client */
745: for (id_list = daemon->dhcp_ignore; id_list; id_list = id_list->next)
746: if (match_netid(id_list->list, tagif_netid, 0))
747: ignore = 1;
748:
749: /* If configured, we can override the server-id to be the address of the relay,
750: so that all traffic goes via the relay and can pick up agent-id info. This can be
751: configured for all relays, or by address. */
752: if (daemon->override && mess->giaddr.s_addr != 0 && override.s_addr == 0)
753: {
754: if (!daemon->override_relays)
755: override = mess->giaddr;
756: else
757: {
758: struct addr_list *l;
759: for (l = daemon->override_relays; l; l = l->next)
760: if (l->addr.s_addr == mess->giaddr.s_addr)
761: break;
762: if (l)
763: override = mess->giaddr;
764: }
765: }
766:
767: /* Can have setting to ignore the client ID for a particular MAC address or hostname */
768: if (have_config(config, CONFIG_NOCLID))
769: clid = NULL;
770:
771: /* Check if client is PXE client. */
772: if (daemon->enable_pxe &&
773: (opt = option_find(mess, sz, OPTION_VENDOR_ID, 9)) &&
774: strncmp(option_ptr(opt, 0), "PXEClient", 9) == 0)
775: {
776: if ((opt = option_find(mess, sz, OPTION_PXE_UUID, 17)))
777: {
778: memcpy(pxe_uuid, option_ptr(opt, 0), 17);
779: uuid = pxe_uuid;
780: }
781:
782: /* Check if this is really a PXE bootserver request, and handle specially if so. */
783: if ((mess_type == DHCPREQUEST || mess_type == DHCPINFORM) &&
784: (opt = option_find(mess, sz, OPTION_VENDOR_CLASS_OPT, 1)) &&
785: (opt = option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_PXE_BOOT_ITEM, 4)))
786: {
787: struct pxe_service *service;
788: int type = option_uint(opt, 0, 2);
789: int layer = option_uint(opt, 2, 2);
790: unsigned char save71[4];
791: struct dhcp_opt opt71;
792:
793: if (ignore)
794: return 0;
795:
796: if (layer & 0x8000)
797: {
798: my_syslog(MS_DHCP | LOG_ERR, _("PXE BIS not supported"));
799: return 0;
800: }
801:
802: memcpy(save71, option_ptr(opt, 0), 4);
803:
804: for (service = daemon->pxe_services; service; service = service->next)
805: if (service->type == type)
806: break;
807:
808: for (; context; context = context->current)
809: if (match_netid(context->filter, tagif_netid, 1) &&
810: is_same_net(mess->ciaddr, context->start, context->netmask))
811: break;
812:
813: if (!service || !service->basename || !context)
814: return 0;
815:
816: clear_packet(mess, end);
817:
818: mess->yiaddr = mess->ciaddr;
819: mess->ciaddr.s_addr = 0;
820: if (service->sname)
821: mess->siaddr = a_record_from_hosts(service->sname, now);
822: else if (service->server.s_addr != 0)
823: mess->siaddr = service->server;
824: else
825: mess->siaddr = context->local;
826:
827: snprintf((char *)mess->file, sizeof(mess->file),
828: strchr(service->basename, '.') ? "%s" :"%s.%d",
829: service->basename, layer);
830:
831: option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPACK);
832: option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, htonl(context->local.s_addr));
833: pxe_misc(mess, end, uuid);
834:
835: prune_vendor_opts(tagif_netid);
836: opt71.val = save71;
837: opt71.opt = SUBOPT_PXE_BOOT_ITEM;
838: opt71.len = 4;
839: opt71.flags = DHOPT_VENDOR_MATCH;
840: opt71.netid = NULL;
841: opt71.next = daemon->dhcp_opts;
842: do_encap_opts(&opt71, OPTION_VENDOR_CLASS_OPT, DHOPT_VENDOR_MATCH, mess, end, 0);
843:
844: log_packet("PXE", &mess->yiaddr, emac, emac_len, iface_name, (char *)mess->file, NULL, mess->xid);
845: log_tags(tagif_netid, ntohl(mess->xid));
846: return dhcp_packet_size(mess, agent_id, real_end);
847: }
848:
849: if ((opt = option_find(mess, sz, OPTION_ARCH, 2)))
850: {
851: pxearch = option_uint(opt, 0, 2);
852:
853: /* proxy DHCP here. */
854: if ((mess_type == DHCPDISCOVER || (pxe && mess_type == DHCPREQUEST)))
855: {
856: struct dhcp_context *tmp;
857: int workaround = 0;
858:
859: for (tmp = context; tmp; tmp = tmp->current)
860: if ((tmp->flags & CONTEXT_PROXY) &&
861: match_netid(tmp->filter, tagif_netid, 1))
862: break;
863:
864: if (tmp)
865: {
866: struct dhcp_boot *boot;
867: int redirect4011 = 0;
868:
869: if (tmp->netid.net)
870: {
871: tmp->netid.next = netid;
872: tagif_netid = run_tag_if(&tmp->netid);
873: }
874:
875: boot = find_boot(tagif_netid);
876:
877: mess->yiaddr.s_addr = 0;
878: if (mess_type == DHCPDISCOVER || mess->ciaddr.s_addr == 0)
879: {
880: mess->ciaddr.s_addr = 0;
881: mess->flags |= htons(0x8000); /* broadcast */
882: }
883:
884: clear_packet(mess, end);
885:
886: /* Redirect EFI clients to port 4011 */
887: if (pxearch >= 6)
888: {
889: redirect4011 = 1;
890: mess->siaddr = tmp->local;
891: }
892:
893: /* Returns true if only one matching service is available. On port 4011,
894: it also inserts the boot file and server name. */
895: workaround = pxe_uefi_workaround(pxearch, tagif_netid, mess, tmp->local, now, pxe);
896:
897: if (!workaround && boot)
898: {
899: /* Provide the bootfile here, for gPXE, and in case we have no menu items
900: and set discovery_control = 8 */
901: if (boot->next_server.s_addr)
902: mess->siaddr = boot->next_server;
903: else if (boot->tftp_sname)
904: mess->siaddr = a_record_from_hosts(boot->tftp_sname, now);
905:
906: if (boot->file)
907: strncpy((char *)mess->file, boot->file, sizeof(mess->file)-1);
908: }
909:
910: option_put(mess, end, OPTION_MESSAGE_TYPE, 1,
911: mess_type == DHCPDISCOVER ? DHCPOFFER : DHCPACK);
912: option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, htonl(tmp->local.s_addr));
913: pxe_misc(mess, end, uuid);
914: prune_vendor_opts(tagif_netid);
915: if ((pxe && !workaround) || !redirect4011)
916: do_encap_opts(pxe_opts(pxearch, tagif_netid, tmp->local, now), OPTION_VENDOR_CLASS_OPT, DHOPT_VENDOR_MATCH, mess, end, 0);
917:
918: log_packet("PXE", NULL, emac, emac_len, iface_name, ignore ? "proxy-ignored" : "proxy", NULL, mess->xid);
919: log_tags(tagif_netid, ntohl(mess->xid));
920: return ignore ? 0 : dhcp_packet_size(mess, agent_id, real_end);
921: }
922: }
923: }
924: }
925:
926: /* if we're just a proxy server, go no further */
927: if ((context->flags & CONTEXT_PROXY) || pxe)
928: return 0;
929:
930: if ((opt = option_find(mess, sz, OPTION_REQUESTED_OPTIONS, 0)))
931: {
932: req_options = (unsigned char *)daemon->dhcp_buff2;
933: memcpy(req_options, option_ptr(opt, 0), option_len(opt));
934: req_options[option_len(opt)] = OPTION_END;
935: }
936:
937: switch (mess_type)
938: {
939: case DHCPDECLINE:
940: if (!(opt = option_find(mess, sz, OPTION_SERVER_IDENTIFIER, INADDRSZ)) ||
941: option_addr(opt).s_addr != server_id(context, override, fallback).s_addr)
942: return 0;
943:
944: /* sanitise any message. Paranoid? Moi? */
945: sanitise(option_find(mess, sz, OPTION_MESSAGE, 1), daemon->dhcp_buff);
946:
947: if (!(opt = option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ)))
948: return 0;
949:
950: log_packet("DHCPDECLINE", option_ptr(opt, 0), emac, emac_len, iface_name, NULL, daemon->dhcp_buff, mess->xid);
951:
952: if (lease && lease->addr.s_addr == option_addr(opt).s_addr)
953: lease_prune(lease, now);
954:
955: if (have_config(config, CONFIG_ADDR) &&
956: config->addr.s_addr == option_addr(opt).s_addr)
957: {
958: prettyprint_time(daemon->dhcp_buff, DECLINE_BACKOFF);
959: my_syslog(MS_DHCP | LOG_WARNING, _("disabling DHCP static address %s for %s"),
960: inet_ntoa(config->addr), daemon->dhcp_buff);
961: config->flags |= CONFIG_DECLINED;
962: config->decline_time = now;
963: }
964: else
965: /* make sure this host gets a different address next time. */
966: for (; context; context = context->current)
967: context->addr_epoch++;
968:
969: return 0;
970:
971: case DHCPRELEASE:
972: if (!(context = narrow_context(context, mess->ciaddr, tagif_netid)) ||
973: !(opt = option_find(mess, sz, OPTION_SERVER_IDENTIFIER, INADDRSZ)) ||
974: option_addr(opt).s_addr != server_id(context, override, fallback).s_addr)
975: return 0;
976:
977: if (lease && lease->addr.s_addr == mess->ciaddr.s_addr)
978: lease_prune(lease, now);
979: else
980: message = _("unknown lease");
981:
982: log_packet("DHCPRELEASE", &mess->ciaddr, emac, emac_len, iface_name, NULL, message, mess->xid);
983:
984: return 0;
985:
986: case DHCPDISCOVER:
987: if (ignore || have_config(config, CONFIG_DISABLE))
988: {
989: if (option_bool(OPT_QUIET_DHCP))
990: return 0;
991: message = _("ignored");
992: opt = NULL;
993: }
994: else
995: {
996: struct in_addr addr, conf;
997:
998: addr.s_addr = conf.s_addr = 0;
999:
1000: if ((opt = option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ)))
1001: addr = option_addr(opt);
1002:
1003: if (have_config(config, CONFIG_ADDR))
1004: {
1005: char *addrs = inet_ntoa(config->addr);
1006:
1007: if ((ltmp = lease_find_by_addr(config->addr)) &&
1008: ltmp != lease &&
1009: !config_has_mac(config, ltmp->hwaddr, ltmp->hwaddr_len, ltmp->hwaddr_type))
1010: {
1011: int len;
1012: unsigned char *mac = extended_hwaddr(ltmp->hwaddr_type, ltmp->hwaddr_len,
1013: ltmp->hwaddr, ltmp->clid_len, ltmp->clid, &len);
1014: my_syslog(MS_DHCP | LOG_WARNING, _("not using configured address %s because it is leased to %s"),
1015: addrs, print_mac(daemon->namebuff, mac, len));
1016: }
1017: else
1018: {
1019: struct dhcp_context *tmp;
1020: for (tmp = context; tmp; tmp = tmp->current)
1021: if (context->router.s_addr == config->addr.s_addr)
1022: break;
1023: if (tmp)
1024: my_syslog(MS_DHCP | LOG_WARNING, _("not using configured address %s because it is in use by the server or relay"), addrs);
1025: else if (have_config(config, CONFIG_DECLINED) &&
1026: difftime(now, config->decline_time) < (float)DECLINE_BACKOFF)
1027: my_syslog(MS_DHCP | LOG_WARNING, _("not using configured address %s because it was previously declined"), addrs);
1028: else
1029: conf = config->addr;
1030: }
1031: }
1032:
1033: if (conf.s_addr)
1034: mess->yiaddr = conf;
1035: else if (lease &&
1036: address_available(context, lease->addr, tagif_netid) &&
1037: !config_find_by_address(daemon->dhcp_conf, lease->addr))
1038: mess->yiaddr = lease->addr;
1039: else if (opt && address_available(context, addr, tagif_netid) && !lease_find_by_addr(addr) &&
1040: !config_find_by_address(daemon->dhcp_conf, addr))
1041: mess->yiaddr = addr;
1042: else if (emac_len == 0)
1043: message = _("no unique-id");
1044: else if (!address_allocate(context, &mess->yiaddr, emac, emac_len, tagif_netid, now))
1045: message = _("no address available");
1046: }
1047:
1048: log_packet("DHCPDISCOVER", opt ? option_ptr(opt, 0) : NULL, emac, emac_len, iface_name, NULL, message, mess->xid);
1049:
1050: if (message || !(context = narrow_context(context, mess->yiaddr, tagif_netid)))
1051: return 0;
1052:
1053: if (context->netid.net)
1054: {
1055: context->netid.next = netid;
1056: tagif_netid = run_tag_if(&context->netid);
1057: }
1058:
1059: log_tags(tagif_netid, ntohl(mess->xid));
1060:
1061: log_packet("DHCPOFFER" , &mess->yiaddr, emac, emac_len, iface_name, NULL, NULL, mess->xid);
1062:
1063: time = calc_time(context, config, option_find(mess, sz, OPTION_LEASE_TIME, 4));
1064: clear_packet(mess, end);
1065: option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPOFFER);
1066: option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, ntohl(server_id(context, override, fallback).s_addr));
1067: option_put(mess, end, OPTION_LEASE_TIME, 4, time);
1068: /* T1 and T2 are required in DHCPOFFER by HP's wacky Jetdirect client. */
1069: do_options(context, mess, end, req_options, offer_hostname, get_domain(mess->yiaddr),
1070: netid, subnet_addr, fqdn_flags, borken_opt, pxearch, uuid, vendor_class_len, now, time, fuzz);
1071:
1072: return dhcp_packet_size(mess, agent_id, real_end);
1073:
1074: case DHCPREQUEST:
1075: if (ignore || have_config(config, CONFIG_DISABLE))
1076: return 0;
1077: if ((opt = option_find(mess, sz, OPTION_REQUESTED_IP, INADDRSZ)))
1078: {
1079: /* SELECTING or INIT_REBOOT */
1080: mess->yiaddr = option_addr(opt);
1081:
1082: /* send vendor and user class info for new or recreated lease */
1083: do_classes = 1;
1084:
1085: if ((opt = option_find(mess, sz, OPTION_SERVER_IDENTIFIER, INADDRSZ)))
1086: {
1087: /* SELECTING */
1088: selecting = 1;
1089:
1090: if (override.s_addr != 0)
1091: {
1092: if (option_addr(opt).s_addr != override.s_addr)
1093: return 0;
1094: }
1095: else
1096: {
1097: for (; context; context = context->current)
1098: if (context->local.s_addr == option_addr(opt).s_addr)
1099: break;
1100:
1101: if (!context)
1102: {
1103: /* Handle very strange configs where clients have more than one route to the server.
1104: If a clients idea of its server-id matches any of our DHCP interfaces, we let it pass.
1105: Have to set override to make sure we echo back the correct server-id */
1106: struct irec *intr;
1107:
1108: enumerate_interfaces(0);
1109:
1110: for (intr = daemon->interfaces; intr; intr = intr->next)
1111: if (intr->addr.sa.sa_family == AF_INET &&
1112: intr->addr.in.sin_addr.s_addr == option_addr(opt).s_addr &&
1113: intr->tftp_ok)
1114: break;
1115:
1116: if (intr)
1117: override = intr->addr.in.sin_addr;
1118: else
1119: {
1120: /* In auth mode, a REQUEST sent to the wrong server
1121: should be faulted, so that the client establishes
1122: communication with us, otherwise, silently ignore. */
1123: if (!option_bool(OPT_AUTHORITATIVE))
1124: return 0;
1125: message = _("wrong server-ID");
1126: }
1127: }
1128: }
1129:
1130: /* If a lease exists for this host and another address, squash it. */
1131: if (lease && lease->addr.s_addr != mess->yiaddr.s_addr)
1132: {
1133: lease_prune(lease, now);
1134: lease = NULL;
1135: }
1136: }
1137: else
1138: {
1139: /* INIT-REBOOT */
1140: if (!lease && !option_bool(OPT_AUTHORITATIVE))
1141: return 0;
1142:
1143: if (lease && lease->addr.s_addr != mess->yiaddr.s_addr)
1144: message = _("wrong address");
1145: }
1146: }
1147: else
1148: {
1149: /* RENEWING or REBINDING */
1150: /* Check existing lease for this address.
1151: We allow it to be missing if dhcp-authoritative mode
1152: as long as we can allocate the lease now - checked below.
1153: This makes for a smooth recovery from a lost lease DB */
1154: if ((lease && mess->ciaddr.s_addr != lease->addr.s_addr) ||
1155: (!lease && !option_bool(OPT_AUTHORITATIVE)))
1156: {
1157: /* A client rebinding will broadcast the request, so we may see it even
1158: if the lease is held by another server. Just ignore it in that case.
1159: If the request is unicast to us, then somethings wrong, NAK */
1160: if (!unicast_dest)
1161: return 0;
1162: message = _("lease not found");
1163: /* ensure we broadcast NAK */
1164: unicast_dest = 0;
1165: }
1166:
1167: /* desynchronise renewals */
1168: fuzz = rand16();
1169: mess->yiaddr = mess->ciaddr;
1170: }
1171:
1172: log_packet("DHCPREQUEST", &mess->yiaddr, emac, emac_len, iface_name, NULL, NULL, mess->xid);
1173:
1174: if (!message)
1175: {
1176: struct dhcp_config *addr_config;
1177: struct dhcp_context *tmp = NULL;
1178:
1179: if (have_config(config, CONFIG_ADDR))
1180: for (tmp = context; tmp; tmp = tmp->current)
1181: if (context->router.s_addr == config->addr.s_addr)
1182: break;
1183:
1184: if (!(context = narrow_context(context, mess->yiaddr, tagif_netid)))
1185: {
1186: /* If a machine moves networks whilst it has a lease, we catch that here. */
1187: message = _("wrong network");
1188: /* ensure we broadcast NAK */
1189: unicast_dest = 0;
1190: }
1191:
1192: /* Check for renewal of a lease which is outside the allowed range. */
1193: else if (!address_available(context, mess->yiaddr, tagif_netid) &&
1194: (!have_config(config, CONFIG_ADDR) || config->addr.s_addr != mess->yiaddr.s_addr))
1195: message = _("address not available");
1196:
1197: /* Check if a new static address has been configured. Be very sure that
1198: when the client does DISCOVER, it will get the static address, otherwise
1199: an endless protocol loop will ensue. */
1200: else if (!tmp && !selecting &&
1201: have_config(config, CONFIG_ADDR) &&
1202: (!have_config(config, CONFIG_DECLINED) ||
1203: difftime(now, config->decline_time) > (float)DECLINE_BACKOFF) &&
1204: config->addr.s_addr != mess->yiaddr.s_addr &&
1205: (!(ltmp = lease_find_by_addr(config->addr)) || ltmp == lease))
1206: message = _("static lease available");
1207:
1208: /* Check to see if the address is reserved as a static address for another host */
1209: else if ((addr_config = config_find_by_address(daemon->dhcp_conf, mess->yiaddr)) && addr_config != config)
1210: message = _("address reserved");
1211:
1212: else if (!lease && (ltmp = lease_find_by_addr(mess->yiaddr)))
1213: {
1214: /* If a host is configured with more than one MAC address, it's OK to 'nix
1215: a lease from one of it's MACs to give the address to another. */
1216: if (config && config_has_mac(config, ltmp->hwaddr, ltmp->hwaddr_len, ltmp->hwaddr_type))
1217: {
1218: my_syslog(MS_DHCP | LOG_INFO, _("abandoning lease to %s of %s"),
1219: print_mac(daemon->namebuff, ltmp->hwaddr, ltmp->hwaddr_len),
1220: inet_ntoa(ltmp->addr));
1221: lease = ltmp;
1222: }
1223: else
1224: message = _("address in use");
1225: }
1226:
1227: if (!message)
1228: {
1229: if (emac_len == 0)
1230: message = _("no unique-id");
1231:
1232: else if (!lease)
1233: {
1234: if ((lease = lease4_allocate(mess->yiaddr)))
1235: do_classes = 1;
1236: else
1237: message = _("no leases left");
1238: }
1239: }
1240: }
1241:
1242: if (message)
1243: {
1244: log_packet("DHCPNAK", &mess->yiaddr, emac, emac_len, iface_name, NULL, message, mess->xid);
1245:
1246: mess->yiaddr.s_addr = 0;
1247: clear_packet(mess, end);
1248: option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPNAK);
1249: option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, ntohl(server_id(context, override, fallback).s_addr));
1250: option_put_string(mess, end, OPTION_MESSAGE, message, borken_opt);
1251: /* This fixes a problem with the DHCP spec, broadcasting a NAK to a host on
1252: a distant subnet which unicast a REQ to us won't work. */
1253: if (!unicast_dest || mess->giaddr.s_addr != 0 ||
1254: mess->ciaddr.s_addr == 0 || is_same_net(context->local, mess->ciaddr, context->netmask))
1255: {
1256: mess->flags |= htons(0x8000); /* broadcast */
1257: mess->ciaddr.s_addr = 0;
1258: }
1259: }
1260: else
1261: {
1262: if (context->netid.net)
1263: {
1264: context->netid.next = netid;
1265: tagif_netid = run_tag_if( &context->netid);
1266: }
1267:
1268: log_tags(tagif_netid, ntohl(mess->xid));
1269:
1270: if (do_classes)
1271: {
1272: /* pick up INIT-REBOOT events. */
1273: lease->flags |= LEASE_CHANGED;
1274:
1275: #ifdef HAVE_SCRIPT
1276: if (daemon->lease_change_command)
1277: {
1278: struct dhcp_netid *n;
1279:
1280: if (mess->giaddr.s_addr)
1281: lease->giaddr = mess->giaddr;
1282:
1283: free(lease->extradata);
1284: lease->extradata = NULL;
1285: lease->extradata_size = lease->extradata_len = 0;
1286:
1287: add_extradata_opt(lease, option_find(mess, sz, OPTION_VENDOR_ID, 1));
1288: add_extradata_opt(lease, option_find(mess, sz, OPTION_HOSTNAME, 1));
1289: add_extradata_opt(lease, oui);
1290: add_extradata_opt(lease, serial);
1291: add_extradata_opt(lease, class);
1292:
1293: if ((opt = option_find(mess, sz, OPTION_AGENT_ID, 1)))
1294: {
1295: add_extradata_opt(lease, option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_CIRCUIT_ID, 1));
1296: add_extradata_opt(lease, option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_SUBSCR_ID, 1));
1297: add_extradata_opt(lease, option_find1(option_ptr(opt, 0), option_ptr(opt, option_len(opt)), SUBOPT_REMOTE_ID, 1));
1298: }
1299: else
1300: {
1301: add_extradata_opt(lease, NULL);
1302: add_extradata_opt(lease, NULL);
1303: add_extradata_opt(lease, NULL);
1304: }
1305:
1306: /* space-concat tag set */
1307: if (!tagif_netid)
1308: add_extradata_opt(lease, NULL);
1309: else
1310: for (n = tagif_netid; n; n = n->next)
1311: {
1312: struct dhcp_netid *n1;
1313: /* kill dupes */
1314: for (n1 = n->next; n1; n1 = n1->next)
1315: if (strcmp(n->net, n1->net) == 0)
1316: break;
1317: if (!n1)
1318: lease_add_extradata(lease, (unsigned char *)n->net, strlen(n->net), n->next ? ' ' : 0);
1319: }
1320:
1321: if ((opt = option_find(mess, sz, OPTION_USER_CLASS, 1)))
1322: {
1323: int len = option_len(opt);
1324: unsigned char *ucp = option_ptr(opt, 0);
1325: /* If the user-class option started as counted strings, the first byte will be zero. */
1326: if (len != 0 && ucp[0] == 0)
1327: ucp++, len--;
1328: lease_add_extradata(lease, ucp, len, -1);
1329: }
1330: }
1331: #endif
1332: }
1333:
1334: if (!hostname_auth && (client_hostname = host_from_dns(mess->yiaddr)))
1335: {
1336: domain = get_domain(mess->yiaddr);
1337: hostname = client_hostname;
1338: hostname_auth = 1;
1339: }
1340:
1341: time = calc_time(context, config, option_find(mess, sz, OPTION_LEASE_TIME, 4));
1342: lease_set_hwaddr(lease, mess->chaddr, clid, mess->hlen, mess->htype, clid_len, now, do_classes);
1343:
1344: /* if all the netids in the ignore_name list are present, ignore client-supplied name */
1345: if (!hostname_auth)
1346: {
1347: for (id_list = daemon->dhcp_ignore_names; id_list; id_list = id_list->next)
1348: if ((!id_list->list) || match_netid(id_list->list, tagif_netid, 0))
1349: break;
1350: if (id_list)
1351: hostname = NULL;
1352: }
1353:
1354: /* Last ditch, if configured, generate hostname from mac address */
1355: if (!hostname && emac_len != 0)
1356: {
1357: for (id_list = daemon->dhcp_gen_names; id_list; id_list = id_list->next)
1358: if ((!id_list->list) || match_netid(id_list->list, tagif_netid, 0))
1359: break;
1360: if (id_list)
1361: {
1362: int i;
1363:
1364: hostname = daemon->dhcp_buff;
1365: /* buffer is 256 bytes, 3 bytes per octet */
1366: for (i = 0; (i < emac_len) && (i < 80); i++)
1367: hostname += sprintf(hostname, "%.2x%s", emac[i], (i == emac_len - 1) ? "" : "-");
1368: hostname = daemon->dhcp_buff;
1369: }
1370: }
1371:
1372: if (hostname)
1373: lease_set_hostname(lease, hostname, hostname_auth, get_domain(lease->addr), domain);
1374:
1375: lease_set_expires(lease, time, now);
1376: lease_set_interface(lease, int_index, now);
1377:
1378: if (override.s_addr != 0)
1379: lease->override = override;
1380: else
1381: override = lease->override;
1382:
1383: log_packet("DHCPACK", &mess->yiaddr, emac, emac_len, iface_name, hostname, NULL, mess->xid);
1384:
1385: clear_packet(mess, end);
1386: option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPACK);
1387: option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, ntohl(server_id(context, override, fallback).s_addr));
1388: option_put(mess, end, OPTION_LEASE_TIME, 4, time);
1389: do_options(context, mess, end, req_options, hostname, get_domain(mess->yiaddr),
1390: netid, subnet_addr, fqdn_flags, borken_opt, pxearch, uuid, vendor_class_len, now, time, fuzz);
1391: }
1392:
1393: return dhcp_packet_size(mess, agent_id, real_end);
1394:
1395: case DHCPINFORM:
1396: if (ignore || have_config(config, CONFIG_DISABLE))
1397: message = _("ignored");
1398:
1399: log_packet("DHCPINFORM", &mess->ciaddr, emac, emac_len, iface_name, message, NULL, mess->xid);
1400:
1401: if (message || mess->ciaddr.s_addr == 0)
1402: return 0;
1403:
1404: /* For DHCPINFORM only, cope without a valid context */
1405: context = narrow_context(context, mess->ciaddr, tagif_netid);
1406:
1407: /* Find a least based on IP address if we didn't
1408: get one from MAC address/client-d */
1409: if (!lease &&
1410: (lease = lease_find_by_addr(mess->ciaddr)) &&
1411: lease->hostname)
1412: hostname = lease->hostname;
1413:
1414: if (!hostname)
1415: hostname = host_from_dns(mess->ciaddr);
1416:
1417: if (context && context->netid.net)
1418: {
1419: context->netid.next = netid;
1420: tagif_netid = run_tag_if(&context->netid);
1421: }
1422:
1423: log_tags(tagif_netid, ntohl(mess->xid));
1424:
1425: log_packet("DHCPACK", &mess->ciaddr, emac, emac_len, iface_name, hostname, NULL, mess->xid);
1426:
1427: if (lease)
1428: {
1429: lease_set_interface(lease, int_index, now);
1430: if (override.s_addr != 0)
1431: lease->override = override;
1432: else
1433: override = lease->override;
1434: }
1435:
1436: clear_packet(mess, end);
1437: option_put(mess, end, OPTION_MESSAGE_TYPE, 1, DHCPACK);
1438: option_put(mess, end, OPTION_SERVER_IDENTIFIER, INADDRSZ, ntohl(server_id(context, override, fallback).s_addr));
1439:
1440: /* RFC 2131 says that DHCPINFORM shouldn't include lease-time parameters, but
1441: we supply a utility which makes DHCPINFORM requests to get this information.
1442: Only include lease time if OPTION_LEASE_TIME is in the parameter request list,
1443: which won't be true for ordinary clients, but will be true for the
1444: dhcp_lease_time utility. */
1445: if (lease && in_list(req_options, OPTION_LEASE_TIME))
1446: {
1447: if (lease->expires == 0)
1448: time = 0xffffffff;
1449: else
1450: time = (unsigned int)difftime(lease->expires, now);
1451: option_put(mess, end, OPTION_LEASE_TIME, 4, time);
1452: }
1453:
1454: do_options(context, mess, end, req_options, hostname, get_domain(mess->ciaddr),
1455: netid, subnet_addr, fqdn_flags, borken_opt, pxearch, uuid, vendor_class_len, now, 0xffffffff, 0);
1456:
1457: *is_inform = 1; /* handle reply differently */
1458: return dhcp_packet_size(mess, agent_id, real_end);
1459: }
1460:
1461: return 0;
1462: }
1463:
1464: /* find a good value to use as MAC address for logging and address-allocation hashing.
1465: This is normally just the chaddr field from the DHCP packet,
1466: but eg Firewire will have hlen == 0 and use the client-id instead.
1467: This could be anything, but will normally be EUI64 for Firewire.
1468: We assume that if the first byte of the client-id equals the htype byte
1469: then the client-id is using the usual encoding and use the rest of the
1470: client-id: if not we can use the whole client-id. This should give
1471: sane MAC address logs. */
1472: unsigned char *extended_hwaddr(int hwtype, int hwlen, unsigned char *hwaddr,
1473: int clid_len, unsigned char *clid, int *len_out)
1474: {
1475: if (hwlen == 0 && clid && clid_len > 3)
1476: {
1477: if (clid[0] == hwtype)
1478: {
1479: *len_out = clid_len - 1 ;
1480: return clid + 1;
1481: }
1482:
1483: #if defined(ARPHRD_EUI64) && defined(ARPHRD_IEEE1394)
1484: if (clid[0] == ARPHRD_EUI64 && hwtype == ARPHRD_IEEE1394)
1485: {
1486: *len_out = clid_len - 1 ;
1487: return clid + 1;
1488: }
1489: #endif
1490:
1491: *len_out = clid_len;
1492: return clid;
1493: }
1494:
1495: *len_out = hwlen;
1496: return hwaddr;
1497: }
1498:
1499: static unsigned int calc_time(struct dhcp_context *context, struct dhcp_config *config, unsigned char *opt)
1500: {
1501: unsigned int time = have_config(config, CONFIG_TIME) ? config->lease_time : context->lease_time;
1502:
1503: if (opt)
1504: {
1505: unsigned int req_time = option_uint(opt, 0, 4);
1506: if (req_time < 120 )
1507: req_time = 120; /* sanity */
1508: if (time == 0xffffffff || (req_time != 0xffffffff && req_time < time))
1509: time = req_time;
1510: }
1511:
1512: return time;
1513: }
1514:
1515: static struct in_addr server_id(struct dhcp_context *context, struct in_addr override, struct in_addr fallback)
1516: {
1517: if (override.s_addr != 0)
1518: return override;
1519: else if (context && context->local.s_addr != 0)
1520: return context->local;
1521: else
1522: return fallback;
1523: }
1524:
1525: static int sanitise(unsigned char *opt, char *buf)
1526: {
1527: char *p;
1528: int i;
1529:
1530: *buf = 0;
1531:
1532: if (!opt)
1533: return 0;
1534:
1535: p = option_ptr(opt, 0);
1536:
1537: for (i = option_len(opt); i > 0; i--)
1538: {
1539: char c = *p++;
1540: if (isprint((int)c))
1541: *buf++ = c;
1542: }
1543: *buf = 0; /* add terminator */
1544:
1545: return 1;
1546: }
1547:
1548: #ifdef HAVE_SCRIPT
1549: static void add_extradata_opt(struct dhcp_lease *lease, unsigned char *opt)
1550: {
1551: if (!opt)
1552: lease_add_extradata(lease, NULL, 0, 0);
1553: else
1554: lease_add_extradata(lease, option_ptr(opt, 0), option_len(opt), 0);
1555: }
1556: #endif
1557:
1558: static void log_packet(char *type, void *addr, unsigned char *ext_mac,
1559: int mac_len, char *interface, char *string, char *err, u32 xid)
1560: {
1561: struct in_addr a;
1562:
1563: if (!err && !option_bool(OPT_LOG_OPTS) && option_bool(OPT_QUIET_DHCP))
1564: return;
1565:
1566: /* addr may be misaligned */
1567: if (addr)
1568: memcpy(&a, addr, sizeof(a));
1569:
1570: print_mac(daemon->namebuff, ext_mac, mac_len);
1571:
1572: if(option_bool(OPT_LOG_OPTS))
1573: my_syslog(MS_DHCP | LOG_INFO, "%u %s(%s) %s%s%s %s%s",
1574: ntohl(xid),
1575: type,
1576: interface,
1577: addr ? inet_ntoa(a) : "",
1578: addr ? " " : "",
1579: daemon->namebuff,
1580: string ? string : "",
1581: err ? err : "");
1582: else
1583: my_syslog(MS_DHCP | LOG_INFO, "%s(%s) %s%s%s %s%s",
1584: type,
1585: interface,
1586: addr ? inet_ntoa(a) : "",
1587: addr ? " " : "",
1588: daemon->namebuff,
1589: string ? string : "",
1590: err ? err : "");
1591: }
1592:
1593: static void log_options(unsigned char *start, u32 xid)
1594: {
1595: while (*start != OPTION_END)
1596: {
1597: char *optname = option_string(AF_INET, start[0], option_ptr(start, 0), option_len(start), daemon->namebuff, MAXDNAME);
1598:
1599: my_syslog(MS_DHCP | LOG_INFO, "%u sent size:%3d option:%3d %s %s",
1600: ntohl(xid), option_len(start), start[0], optname, daemon->namebuff);
1601: start += start[1] + 2;
1602: }
1603: }
1604:
1605: static unsigned char *option_find1(unsigned char *p, unsigned char *end, int opt, int minsize)
1606: {
1607: while (1)
1608: {
1609: if (p > end)
1610: return NULL;
1611: else if (*p == OPTION_END)
1612: return opt == OPTION_END ? p : NULL;
1613: else if (*p == OPTION_PAD)
1614: p++;
1615: else
1616: {
1617: int opt_len;
1618: if (p > end - 2)
1619: return NULL; /* malformed packet */
1620: opt_len = option_len(p);
1621: if (p > end - (2 + opt_len))
1622: return NULL; /* malformed packet */
1623: if (*p == opt && opt_len >= minsize)
1624: return p;
1625: p += opt_len + 2;
1626: }
1627: }
1628: }
1629:
1630: static unsigned char *option_find(struct dhcp_packet *mess, size_t size, int opt_type, int minsize)
1631: {
1632: unsigned char *ret, *overload;
1633:
1634: /* skip over DHCP cookie; */
1635: if ((ret = option_find1(&mess->options[0] + sizeof(u32), ((unsigned char *)mess) + size, opt_type, minsize)))
1636: return ret;
1637:
1638: /* look for overload option. */
1639: if (!(overload = option_find1(&mess->options[0] + sizeof(u32), ((unsigned char *)mess) + size, OPTION_OVERLOAD, 1)))
1640: return NULL;
1641:
1642: /* Can we look in filename area ? */
1643: if ((overload[2] & 1) &&
1644: (ret = option_find1(&mess->file[0], &mess->file[128], opt_type, minsize)))
1645: return ret;
1646:
1647: /* finally try sname area */
1648: if ((overload[2] & 2) &&
1649: (ret = option_find1(&mess->sname[0], &mess->sname[64], opt_type, minsize)))
1650: return ret;
1651:
1652: return NULL;
1653: }
1654:
1655: static struct in_addr option_addr(unsigned char *opt)
1656: {
1657: /* this worries about unaligned data in the option. */
1658: /* struct in_addr is network byte order */
1659: struct in_addr ret;
1660:
1661: memcpy(&ret, option_ptr(opt, 0), INADDRSZ);
1662:
1663: return ret;
1664: }
1665:
1666: static unsigned int option_uint(unsigned char *opt, int offset, int size)
1667: {
1668: /* this worries about unaligned data and byte order */
1669: unsigned int ret = 0;
1670: int i;
1671: unsigned char *p = option_ptr(opt, offset);
1672:
1673: for (i = 0; i < size; i++)
1674: ret = (ret << 8) | *p++;
1675:
1676: return ret;
1677: }
1678:
1679: static unsigned char *dhcp_skip_opts(unsigned char *start)
1680: {
1681: while (*start != 0)
1682: start += start[1] + 2;
1683: return start;
1684: }
1685:
1686: /* only for use when building packet: doesn't check for bad data. */
1687: static unsigned char *find_overload(struct dhcp_packet *mess)
1688: {
1689: unsigned char *p = &mess->options[0] + sizeof(u32);
1690:
1691: while (*p != 0)
1692: {
1693: if (*p == OPTION_OVERLOAD)
1694: return p;
1695: p += p[1] + 2;
1696: }
1697: return NULL;
1698: }
1699:
1700: static size_t dhcp_packet_size(struct dhcp_packet *mess, unsigned char *agent_id, unsigned char *real_end)
1701: {
1702: unsigned char *p = dhcp_skip_opts(&mess->options[0] + sizeof(u32));
1703: unsigned char *overload;
1704: size_t ret;
1705:
1706: /* move agent_id back down to the end of the packet */
1707: if (agent_id)
1708: {
1709: memmove(p, agent_id, real_end - agent_id);
1710: p += real_end - agent_id;
1711: memset(p, 0, real_end - p); /* in case of overlap */
1712: }
1713:
1714: /* add END options to the regions. */
1715: overload = find_overload(mess);
1716:
1717: if (overload && (option_uint(overload, 0, 1) & 1))
1718: {
1719: *dhcp_skip_opts(mess->file) = OPTION_END;
1720: if (option_bool(OPT_LOG_OPTS))
1721: log_options(mess->file, mess->xid);
1722: }
1723: else if (option_bool(OPT_LOG_OPTS) && strlen((char *)mess->file) != 0)
1724: my_syslog(MS_DHCP | LOG_INFO, _("%u bootfile name: %s"), ntohl(mess->xid), (char *)mess->file);
1725:
1726: if (overload && (option_uint(overload, 0, 1) & 2))
1727: {
1728: *dhcp_skip_opts(mess->sname) = OPTION_END;
1729: if (option_bool(OPT_LOG_OPTS))
1730: log_options(mess->sname, mess->xid);
1731: }
1732: else if (option_bool(OPT_LOG_OPTS) && strlen((char *)mess->sname) != 0)
1733: my_syslog(MS_DHCP | LOG_INFO, _("%u server name: %s"), ntohl(mess->xid), (char *)mess->sname);
1734:
1735:
1736: *p++ = OPTION_END;
1737:
1738: if (option_bool(OPT_LOG_OPTS))
1739: {
1740: if (mess->siaddr.s_addr != 0)
1741: my_syslog(MS_DHCP | LOG_INFO, _("%u next server: %s"), ntohl(mess->xid), inet_ntoa(mess->siaddr));
1742:
1743: if ((mess->flags & htons(0x8000)) && mess->ciaddr.s_addr == 0)
1744: my_syslog(MS_DHCP | LOG_INFO, _("%u broadcast response"), ntohl(mess->xid));
1745:
1746: log_options(&mess->options[0] + sizeof(u32), mess->xid);
1747: }
1748:
1749: ret = (size_t)(p - (unsigned char *)mess);
1750:
1751: if (ret < MIN_PACKETSZ)
1752: ret = MIN_PACKETSZ;
1753:
1754: return ret;
1755: }
1756:
1757: static unsigned char *free_space(struct dhcp_packet *mess, unsigned char *end, int opt, int len)
1758: {
1759: unsigned char *p = dhcp_skip_opts(&mess->options[0] + sizeof(u32));
1760:
1761: if (p + len + 3 >= end)
1762: /* not enough space in options area, try and use overload, if poss */
1763: {
1764: unsigned char *overload;
1765:
1766: if (!(overload = find_overload(mess)) &&
1767: (mess->file[0] == 0 || mess->sname[0] == 0))
1768: {
1769: /* attempt to overload fname and sname areas, we've reserved space for the
1770: overflow option previuously. */
1771: overload = p;
1772: *(p++) = OPTION_OVERLOAD;
1773: *(p++) = 1;
1774: }
1775:
1776: p = NULL;
1777:
1778: /* using filename field ? */
1779: if (overload)
1780: {
1781: if (mess->file[0] == 0)
1782: overload[2] |= 1;
1783:
1784: if (overload[2] & 1)
1785: {
1786: p = dhcp_skip_opts(mess->file);
1787: if (p + len + 3 >= mess->file + sizeof(mess->file))
1788: p = NULL;
1789: }
1790:
1791: if (!p)
1792: {
1793: /* try to bring sname into play (it may be already) */
1794: if (mess->sname[0] == 0)
1795: overload[2] |= 2;
1796:
1797: if (overload[2] & 2)
1798: {
1799: p = dhcp_skip_opts(mess->sname);
1800: if (p + len + 3 >= mess->sname + sizeof(mess->sname))
1801: p = NULL;
1802: }
1803: }
1804: }
1805:
1806: if (!p)
1807: my_syslog(MS_DHCP | LOG_WARNING, _("cannot send DHCP/BOOTP option %d: no space left in packet"), opt);
1808: }
1809:
1810: if (p)
1811: {
1812: *(p++) = opt;
1813: *(p++) = len;
1814: }
1815:
1816: return p;
1817: }
1818:
1819: static void option_put(struct dhcp_packet *mess, unsigned char *end, int opt, int len, unsigned int val)
1820: {
1821: int i;
1822: unsigned char *p = free_space(mess, end, opt, len);
1823:
1824: if (p)
1825: for (i = 0; i < len; i++)
1826: *(p++) = val >> (8 * (len - (i + 1)));
1827: }
1828:
1829: static void option_put_string(struct dhcp_packet *mess, unsigned char *end, int opt,
1830: char *string, int null_term)
1831: {
1832: unsigned char *p;
1833: size_t len = strlen(string);
1834:
1835: if (null_term && len != 255)
1836: len++;
1837:
1838: if ((p = free_space(mess, end, opt, len)))
1839: memcpy(p, string, len);
1840: }
1841:
1842: /* return length, note this only does the data part */
1843: static int do_opt(struct dhcp_opt *opt, unsigned char *p, struct dhcp_context *context, int null_term)
1844: {
1845: int len = opt->len;
1846:
1847: if ((opt->flags & DHOPT_STRING) && null_term && len != 255)
1848: len++;
1849:
1850: if (p && len != 0)
1851: {
1852: if (context && (opt->flags & DHOPT_ADDR))
1853: {
1854: int j;
1855: struct in_addr *a = (struct in_addr *)opt->val;
1856: for (j = 0; j < opt->len; j+=INADDRSZ, a++)
1857: {
1858: /* zero means "self" (but not in vendorclass options.) */
1859: if (a->s_addr == 0)
1860: memcpy(p, &context->local, INADDRSZ);
1861: else
1862: memcpy(p, a, INADDRSZ);
1863: p += INADDRSZ;
1864: }
1865: }
1866: else
1867: /* empty string may be extended to "\0" by null_term */
1868: memcpy(p, opt->val ? opt->val : (unsigned char *)"", len);
1869: }
1870: return len;
1871: }
1872:
1873: static int in_list(unsigned char *list, int opt)
1874: {
1875: int i;
1876:
1877: /* If no requested options, send everything, not nothing. */
1878: if (!list)
1879: return 1;
1880:
1881: for (i = 0; list[i] != OPTION_END; i++)
1882: if (opt == list[i])
1883: return 1;
1884:
1885: return 0;
1886: }
1887:
1888: static struct dhcp_opt *option_find2(int opt)
1889: {
1890: struct dhcp_opt *opts;
1891:
1892: for (opts = daemon->dhcp_opts; opts; opts = opts->next)
1893: if (opts->opt == opt && (opts->flags & DHOPT_TAGOK))
1894: return opts;
1895:
1896: return NULL;
1897: }
1898:
1899: /* mark vendor-encapsulated options which match the client-supplied or
1900: config-supplied vendor class */
1901: static void match_vendor_opts(unsigned char *opt, struct dhcp_opt *dopt)
1902: {
1903: for (; dopt; dopt = dopt->next)
1904: {
1905: dopt->flags &= ~DHOPT_VENDOR_MATCH;
1906: if (opt && (dopt->flags & DHOPT_VENDOR))
1907: {
1908: int i, len = 0;
1909: if (dopt->u.vendor_class)
1910: len = strlen((char *)dopt->u.vendor_class);
1911: for (i = 0; i <= (option_len(opt) - len); i++)
1912: if (len == 0 || memcmp(dopt->u.vendor_class, option_ptr(opt, i), len) == 0)
1913: {
1914: dopt->flags |= DHOPT_VENDOR_MATCH;
1915: break;
1916: }
1917: }
1918: }
1919: }
1920:
1921: static int do_encap_opts(struct dhcp_opt *opt, int encap, int flag,
1922: struct dhcp_packet *mess, unsigned char *end, int null_term)
1923: {
1924: int len, enc_len, ret = 0;
1925: struct dhcp_opt *start;
1926: unsigned char *p;
1927:
1928: /* find size in advance */
1929: for (enc_len = 0, start = opt; opt; opt = opt->next)
1930: if (opt->flags & flag)
1931: {
1932: int new = do_opt(opt, NULL, NULL, null_term) + 2;
1933: ret = 1;
1934: if (enc_len + new <= 255)
1935: enc_len += new;
1936: else
1937: {
1938: p = free_space(mess, end, encap, enc_len);
1939: for (; start && start != opt; start = start->next)
1940: if (p && (start->flags & flag))
1941: {
1942: len = do_opt(start, p + 2, NULL, null_term);
1943: *(p++) = start->opt;
1944: *(p++) = len;
1945: p += len;
1946: }
1947: enc_len = new;
1948: start = opt;
1949: }
1950: }
1951:
1952: if (enc_len != 0 &&
1953: (p = free_space(mess, end, encap, enc_len + 1)))
1954: {
1955: for (; start; start = start->next)
1956: if (start->flags & flag)
1957: {
1958: len = do_opt(start, p + 2, NULL, null_term);
1959: *(p++) = start->opt;
1960: *(p++) = len;
1961: p += len;
1962: }
1963: *p = OPTION_END;
1964: }
1965:
1966: return ret;
1967: }
1968:
1969: static void pxe_misc(struct dhcp_packet *mess, unsigned char *end, unsigned char *uuid)
1970: {
1971: unsigned char *p;
1972:
1973: option_put_string(mess, end, OPTION_VENDOR_ID, "PXEClient", 0);
1974: if (uuid && (p = free_space(mess, end, OPTION_PXE_UUID, 17)))
1975: memcpy(p, uuid, 17);
1976: }
1977:
1978: static int prune_vendor_opts(struct dhcp_netid *netid)
1979: {
1980: int force = 0;
1981: struct dhcp_opt *opt;
1982:
1983: /* prune vendor-encapsulated options based on netid, and look if we're forcing them to be sent */
1984: for (opt = daemon->dhcp_opts; opt; opt = opt->next)
1985: if (opt->flags & DHOPT_VENDOR_MATCH)
1986: {
1987: if (!match_netid(opt->netid, netid, 1))
1988: opt->flags &= ~DHOPT_VENDOR_MATCH;
1989: else if (opt->flags & DHOPT_FORCE)
1990: force = 1;
1991: }
1992: return force;
1993: }
1994:
1995:
1996: /* Many UEFI PXE implementations have badly broken menu code.
1997: If there's exactly one relevant menu item, we abandon the menu system,
1998: and jamb the data direct into the DHCP file, siaddr and sname fields.
1999: Note that in this case, we have to assume that layer zero would be requested
2000: by the client PXE stack. */
2001: 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)
2002: {
2003: struct pxe_service *service, *found;
2004:
2005: /* Only workaround UEFI archs. */
2006: if (pxe_arch < 6)
2007: return 0;
2008:
2009: for (found = NULL, service = daemon->pxe_services; service; service = service->next)
2010: if (pxe_arch == service->CSA && service->basename && match_netid(service->netid, netid, 1))
2011: {
2012: if (found)
2013: return 0; /* More than one relevant menu item */
2014:
2015: found = service;
2016: }
2017:
2018: if (!found)
2019: return 0; /* No relevant menu items. */
2020:
2021: if (!pxe)
2022: return 1;
2023:
2024: if (found->sname)
2025: {
2026: mess->siaddr = a_record_from_hosts(found->sname, now);
2027: snprintf((char *)mess->sname, sizeof(mess->sname), "%s", found->sname);
2028: }
2029: else
2030: {
2031: if (found->server.s_addr != 0)
2032: mess->siaddr = found->server;
2033: else
2034: mess->siaddr = local;
2035:
2036: inet_ntop(AF_INET, &mess->siaddr, (char *)mess->sname, INET_ADDRSTRLEN);
2037: }
2038:
2039: snprintf((char *)mess->file, sizeof(mess->file),
2040: strchr(found->basename, '.') ? "%s" : "%s.0", found->basename);
2041:
2042: return 1;
2043: }
2044:
2045: static struct dhcp_opt *pxe_opts(int pxe_arch, struct dhcp_netid *netid, struct in_addr local, time_t now)
2046: {
2047: #define NUM_OPTS 4
2048:
2049: unsigned char *p, *q;
2050: struct pxe_service *service;
2051: static struct dhcp_opt *o, *ret;
2052: int i, j = NUM_OPTS - 1;
2053: struct in_addr boot_server;
2054:
2055: /* We pass back references to these, hence they are declared static */
2056: static unsigned char discovery_control;
2057: static unsigned char fake_prompt[] = { 0, 'P', 'X', 'E' };
2058: static struct dhcp_opt *fake_opts = NULL;
2059:
2060: /* Disable multicast, since we don't support it, and broadcast
2061: unless we need it */
2062: discovery_control = 3;
2063:
2064: ret = daemon->dhcp_opts;
2065:
2066: if (!fake_opts && !(fake_opts = whine_malloc(NUM_OPTS * sizeof(struct dhcp_opt))))
2067: return ret;
2068:
2069: for (i = 0; i < NUM_OPTS; i++)
2070: {
2071: fake_opts[i].flags = DHOPT_VENDOR_MATCH;
2072: fake_opts[i].netid = NULL;
2073: fake_opts[i].next = i == (NUM_OPTS - 1) ? ret : &fake_opts[i+1];
2074: }
2075:
2076: /* create the data for the PXE_MENU and PXE_SERVERS options. */
2077: p = (unsigned char *)daemon->dhcp_buff;
2078: q = (unsigned char *)daemon->dhcp_buff3;
2079:
2080: for (i = 0, service = daemon->pxe_services; service; service = service->next)
2081: if (pxe_arch == service->CSA && match_netid(service->netid, netid, 1))
2082: {
2083: size_t len = strlen(service->menu);
2084: /* opt 43 max size is 255. encapsulated option has type and length
2085: bytes, so its max size is 253. */
2086: if (p - (unsigned char *)daemon->dhcp_buff + len + 3 < 253)
2087: {
2088: *(p++) = service->type >> 8;
2089: *(p++) = service->type;
2090: *(p++) = len;
2091: memcpy(p, service->menu, len);
2092: p += len;
2093: i++;
2094: }
2095: else
2096: {
2097: toobig:
2098: my_syslog(MS_DHCP | LOG_ERR, _("PXE menu too large"));
2099: return daemon->dhcp_opts;
2100: }
2101:
2102: boot_server = service->basename ? local :
2103: (service->sname ? a_record_from_hosts(service->sname, now) : service->server);
2104:
2105: if (boot_server.s_addr != 0)
2106: {
2107: if (q - (unsigned char *)daemon->dhcp_buff3 + 3 + INADDRSZ >= 253)
2108: goto toobig;
2109:
2110: /* Boot service with known address - give it */
2111: *(q++) = service->type >> 8;
2112: *(q++) = service->type;
2113: *(q++) = 1;
2114: /* dest misaligned */
2115: memcpy(q, &boot_server.s_addr, INADDRSZ);
2116: q += INADDRSZ;
2117: }
2118: else if (service->type != 0)
2119: /* We don't know the server for a service type, so we'll
2120: allow the client to broadcast for it */
2121: discovery_control = 2;
2122: }
2123:
2124: /* if no prompt, wait forever if there's a choice */
2125: fake_prompt[0] = (i > 1) ? 255 : 0;
2126:
2127: if (i == 0)
2128: discovery_control = 8; /* no menu - just use use mess->filename */
2129: else
2130: {
2131: ret = &fake_opts[j--];
2132: ret->len = p - (unsigned char *)daemon->dhcp_buff;
2133: ret->val = (unsigned char *)daemon->dhcp_buff;
2134: ret->opt = SUBOPT_PXE_MENU;
2135:
2136: if (q - (unsigned char *)daemon->dhcp_buff3 != 0)
2137: {
2138: ret = &fake_opts[j--];
2139: ret->len = q - (unsigned char *)daemon->dhcp_buff3;
2140: ret->val = (unsigned char *)daemon->dhcp_buff3;
2141: ret->opt = SUBOPT_PXE_SERVERS;
2142: }
2143: }
2144:
2145: for (o = daemon->dhcp_opts; o; o = o->next)
2146: if ((o->flags & DHOPT_VENDOR_MATCH) && o->opt == SUBOPT_PXE_MENU_PROMPT)
2147: break;
2148:
2149: if (!o)
2150: {
2151: ret = &fake_opts[j--];
2152: ret->len = sizeof(fake_prompt);
2153: ret->val = fake_prompt;
2154: ret->opt = SUBOPT_PXE_MENU_PROMPT;
2155: }
2156:
2157: ret = &fake_opts[j--];
2158: ret->len = 1;
2159: ret->opt = SUBOPT_PXE_DISCOVERY;
2160: ret->val= &discovery_control;
2161:
2162: return ret;
2163: }
2164:
2165: static void clear_packet(struct dhcp_packet *mess, unsigned char *end)
2166: {
2167: memset(mess->sname, 0, sizeof(mess->sname));
2168: memset(mess->file, 0, sizeof(mess->file));
2169: memset(&mess->options[0] + sizeof(u32), 0, end - (&mess->options[0] + sizeof(u32)));
2170: mess->siaddr.s_addr = 0;
2171: }
2172:
2173: struct dhcp_boot *find_boot(struct dhcp_netid *netid)
2174: {
2175: struct dhcp_boot *boot;
2176:
2177: /* decide which dhcp-boot option we're using */
2178: for (boot = daemon->boot_config; boot; boot = boot->next)
2179: if (match_netid(boot->netid, netid, 0))
2180: break;
2181: if (!boot)
2182: /* No match, look for one without a netid */
2183: for (boot = daemon->boot_config; boot; boot = boot->next)
2184: if (match_netid(boot->netid, netid, 1))
2185: break;
2186:
2187: return boot;
2188: }
2189:
2190: static void do_options(struct dhcp_context *context,
2191: struct dhcp_packet *mess,
2192: unsigned char *end,
2193: unsigned char *req_options,
2194: char *hostname,
2195: char *domain,
2196: struct dhcp_netid *netid,
2197: struct in_addr subnet_addr,
2198: unsigned char fqdn_flags,
2199: int null_term, int pxe_arch,
2200: unsigned char *uuid,
2201: int vendor_class_len,
2202: time_t now,
2203: unsigned int lease_time,
2204: unsigned short fuzz)
2205: {
2206: struct dhcp_opt *opt, *config_opts = daemon->dhcp_opts;
2207: struct dhcp_boot *boot;
2208: unsigned char *p;
2209: int i, len, force_encap = 0;
2210: unsigned char f0 = 0, s0 = 0;
2211: int done_file = 0, done_server = 0;
2212: int done_vendor_class = 0;
2213: struct dhcp_netid *tagif;
2214: struct dhcp_netid_list *id_list;
2215:
2216: /* filter options based on tags, those we want get DHOPT_TAGOK bit set */
2217: if (context)
2218: context->netid.next = NULL;
2219: tagif = option_filter(netid, context && context->netid.net ? &context->netid : NULL, config_opts);
2220:
2221: /* logging */
2222: if (option_bool(OPT_LOG_OPTS) && req_options)
2223: {
2224: char *q = daemon->namebuff;
2225: for (i = 0; req_options[i] != OPTION_END; i++)
2226: {
2227: char *s = option_string(AF_INET, req_options[i], NULL, 0, NULL, 0);
2228: q += snprintf(q, MAXDNAME - (q - daemon->namebuff),
2229: "%d%s%s%s",
2230: req_options[i],
2231: strlen(s) != 0 ? ":" : "",
2232: s,
2233: req_options[i+1] == OPTION_END ? "" : ", ");
2234: if (req_options[i+1] == OPTION_END || (q - daemon->namebuff) > 40)
2235: {
2236: q = daemon->namebuff;
2237: my_syslog(MS_DHCP | LOG_INFO, _("%u requested options: %s"), ntohl(mess->xid), daemon->namebuff);
2238: }
2239: }
2240: }
2241:
2242: for (id_list = daemon->force_broadcast; id_list; id_list = id_list->next)
2243: if ((!id_list->list) || match_netid(id_list->list, netid, 0))
2244: break;
2245: if (id_list)
2246: mess->flags |= htons(0x8000); /* force broadcast */
2247:
2248: if (context)
2249: mess->siaddr = context->local;
2250:
2251: /* See if we can send the boot stuff as options.
2252: To do this we need a requested option list, BOOTP
2253: and very old DHCP clients won't have this, we also
2254: provide an manual option to disable it.
2255: Some PXE ROMs have bugs (surprise!) and need zero-terminated
2256: names, so we always send those. */
2257: if ((boot = find_boot(tagif)))
2258: {
2259: if (boot->sname)
2260: {
2261: if (!option_bool(OPT_NO_OVERRIDE) &&
2262: req_options &&
2263: in_list(req_options, OPTION_SNAME))
2264: option_put_string(mess, end, OPTION_SNAME, boot->sname, 1);
2265: else
2266: strncpy((char *)mess->sname, boot->sname, sizeof(mess->sname)-1);
2267: }
2268:
2269: if (boot->file)
2270: {
2271: if (!option_bool(OPT_NO_OVERRIDE) &&
2272: req_options &&
2273: in_list(req_options, OPTION_FILENAME))
2274: option_put_string(mess, end, OPTION_FILENAME, boot->file, 1);
2275: else
2276: strncpy((char *)mess->file, boot->file, sizeof(mess->file)-1);
2277: }
2278:
2279: if (boot->next_server.s_addr)
2280: mess->siaddr = boot->next_server;
2281: else if (boot->tftp_sname)
2282: mess->siaddr = a_record_from_hosts(boot->tftp_sname, now);
2283: }
2284: else
2285: /* Use the values of the relevant options if no dhcp-boot given and
2286: they're not explicitly asked for as options. OPTION_END is used
2287: as an internal way to specify siaddr without using dhcp-boot, for use in
2288: dhcp-optsfile. */
2289: {
2290: if ((!req_options || !in_list(req_options, OPTION_FILENAME)) &&
2291: (opt = option_find2(OPTION_FILENAME)) && !(opt->flags & DHOPT_FORCE))
2292: {
2293: strncpy((char *)mess->file, (char *)opt->val, sizeof(mess->file)-1);
2294: done_file = 1;
2295: }
2296:
2297: if ((!req_options || !in_list(req_options, OPTION_SNAME)) &&
2298: (opt = option_find2(OPTION_SNAME)) && !(opt->flags & DHOPT_FORCE))
2299: {
2300: strncpy((char *)mess->sname, (char *)opt->val, sizeof(mess->sname)-1);
2301: done_server = 1;
2302: }
2303:
2304: if ((opt = option_find2(OPTION_END)))
2305: mess->siaddr.s_addr = ((struct in_addr *)opt->val)->s_addr;
2306: }
2307:
2308: /* We don't want to do option-overload for BOOTP, so make the file and sname
2309: fields look like they are in use, even when they aren't. This gets restored
2310: at the end of this function. */
2311:
2312: if (!req_options || option_bool(OPT_NO_OVERRIDE))
2313: {
2314: f0 = mess->file[0];
2315: mess->file[0] = 1;
2316: s0 = mess->sname[0];
2317: mess->sname[0] = 1;
2318: }
2319:
2320: /* At this point, if mess->sname or mess->file are zeroed, they are available
2321: for option overload, reserve space for the overload option. */
2322: if (mess->file[0] == 0 || mess->sname[0] == 0)
2323: end -= 3;
2324:
2325: /* rfc3011 says this doesn't need to be in the requested options list. */
2326: if (subnet_addr.s_addr)
2327: option_put(mess, end, OPTION_SUBNET_SELECT, INADDRSZ, ntohl(subnet_addr.s_addr));
2328:
2329: if (lease_time != 0xffffffff)
2330: {
2331: unsigned int t1val = lease_time/2;
2332: unsigned int t2val = (lease_time*7)/8;
2333: unsigned int hval;
2334:
2335: /* If set by user, sanity check, so not longer than lease. */
2336: if ((opt = option_find2(OPTION_T1)))
2337: {
2338: hval = ntohl(*((unsigned int *)opt->val));
2339: if (hval < lease_time && hval > 2)
2340: t1val = hval;
2341: }
2342:
2343: if ((opt = option_find2(OPTION_T2)))
2344: {
2345: hval = ntohl(*((unsigned int *)opt->val));
2346: if (hval < lease_time && hval > 2)
2347: t2val = hval;
2348: }
2349:
2350: /* ensure T1 is still < T2 */
2351: if (t2val <= t1val)
2352: t1val = t2val - 1;
2353:
2354: while (fuzz > (t1val/8))
2355: fuzz = fuzz/2;
2356:
2357: t1val -= fuzz;
2358: t2val -= fuzz;
2359:
2360: option_put(mess, end, OPTION_T1, 4, t1val);
2361: option_put(mess, end, OPTION_T2, 4, t2val);
2362: }
2363:
2364: /* replies to DHCPINFORM may not have a valid context */
2365: if (context)
2366: {
2367: if (!option_find2(OPTION_NETMASK))
2368: option_put(mess, end, OPTION_NETMASK, INADDRSZ, ntohl(context->netmask.s_addr));
2369:
2370: /* May not have a "guessed" broadcast address if we got no packets via a relay
2371: from this net yet (ie just unicast renewals after a restart */
2372: if (context->broadcast.s_addr &&
2373: !option_find2(OPTION_BROADCAST))
2374: option_put(mess, end, OPTION_BROADCAST, INADDRSZ, ntohl(context->broadcast.s_addr));
2375:
2376: /* Same comments as broadcast apply, and also may not be able to get a sensible
2377: default when using subnet select. User must configure by steam in that case. */
2378: if (context->router.s_addr &&
2379: in_list(req_options, OPTION_ROUTER) &&
2380: !option_find2(OPTION_ROUTER))
2381: option_put(mess, end, OPTION_ROUTER, INADDRSZ, ntohl(context->router.s_addr));
2382:
2383: if (daemon->port == NAMESERVER_PORT &&
2384: in_list(req_options, OPTION_DNSSERVER) &&
2385: !option_find2(OPTION_DNSSERVER))
2386: option_put(mess, end, OPTION_DNSSERVER, INADDRSZ, ntohl(context->local.s_addr));
2387: }
2388:
2389: if (domain && in_list(req_options, OPTION_DOMAINNAME) &&
2390: !option_find2(OPTION_DOMAINNAME))
2391: option_put_string(mess, end, OPTION_DOMAINNAME, domain, null_term);
2392:
2393: /* Note that we ignore attempts to set the fqdn using --dhc-option=81,<name> */
2394: if (hostname)
2395: {
2396: if (in_list(req_options, OPTION_HOSTNAME) &&
2397: !option_find2(OPTION_HOSTNAME))
2398: option_put_string(mess, end, OPTION_HOSTNAME, hostname, null_term);
2399:
2400: if (fqdn_flags != 0)
2401: {
2402: len = strlen(hostname) + 3;
2403:
2404: if (fqdn_flags & 0x04)
2405: len += 2;
2406: else if (null_term)
2407: len++;
2408:
2409: if (domain)
2410: len += strlen(domain) + 1;
2411: else if (fqdn_flags & 0x04)
2412: len--;
2413:
2414: if ((p = free_space(mess, end, OPTION_CLIENT_FQDN, len)))
2415: {
2416: *(p++) = fqdn_flags & 0x0f; /* MBZ bits to zero */
2417: *(p++) = 255;
2418: *(p++) = 255;
2419:
2420: if (fqdn_flags & 0x04)
2421: {
2422: p = do_rfc1035_name(p, hostname);
2423: if (domain)
2424: {
2425: p = do_rfc1035_name(p, domain);
2426: *p++ = 0;
2427: }
2428: }
2429: else
2430: {
2431: memcpy(p, hostname, strlen(hostname));
2432: p += strlen(hostname);
2433: if (domain)
2434: {
2435: *(p++) = '.';
2436: memcpy(p, domain, strlen(domain));
2437: p += strlen(domain);
2438: }
2439: if (null_term)
2440: *(p++) = 0;
2441: }
2442: }
2443: }
2444: }
2445:
2446: for (opt = config_opts; opt; opt = opt->next)
2447: {
2448: int optno = opt->opt;
2449:
2450: /* netids match and not encapsulated? */
2451: if (!(opt->flags & DHOPT_TAGOK))
2452: continue;
2453:
2454: /* was it asked for, or are we sending it anyway? */
2455: if (!(opt->flags & DHOPT_FORCE) && !in_list(req_options, optno))
2456: continue;
2457:
2458: /* prohibit some used-internally options. T1 and T2 already handled. */
2459: if (optno == OPTION_CLIENT_FQDN ||
2460: optno == OPTION_MAXMESSAGE ||
2461: optno == OPTION_OVERLOAD ||
2462: optno == OPTION_PAD ||
2463: optno == OPTION_END ||
2464: optno == OPTION_T1 ||
2465: optno == OPTION_T2)
2466: continue;
2467:
2468: if (optno == OPTION_SNAME && done_server)
2469: continue;
2470:
2471: if (optno == OPTION_FILENAME && done_file)
2472: continue;
2473:
2474: /* For the options we have default values on
2475: dhc-option=<optionno> means "don't include this option"
2476: not "include a zero-length option" */
2477: if (opt->len == 0 &&
2478: (optno == OPTION_NETMASK ||
2479: optno == OPTION_BROADCAST ||
2480: optno == OPTION_ROUTER ||
2481: optno == OPTION_DNSSERVER ||
2482: optno == OPTION_DOMAINNAME ||
2483: optno == OPTION_HOSTNAME))
2484: continue;
2485:
2486: /* vendor-class comes from elsewhere for PXE */
2487: if (pxe_arch != -1 && optno == OPTION_VENDOR_ID)
2488: continue;
2489:
2490: /* always force null-term for filename and servername - buggy PXE again. */
2491: len = do_opt(opt, NULL, context,
2492: (optno == OPTION_SNAME || optno == OPTION_FILENAME) ? 1 : null_term);
2493:
2494: if ((p = free_space(mess, end, optno, len)))
2495: {
2496: do_opt(opt, p, context,
2497: (optno == OPTION_SNAME || optno == OPTION_FILENAME) ? 1 : null_term);
2498:
2499: /* If we send a vendor-id, revisit which vendor-ops we consider
2500: it appropriate to send. */
2501: if (optno == OPTION_VENDOR_ID)
2502: {
2503: match_vendor_opts(p - 2, config_opts);
2504: done_vendor_class = 1;
2505: }
2506: }
2507: }
2508:
2509: /* Now send options to be encapsulated in arbitrary options,
2510: eg dhcp-option=encap:172,17,.......
2511: Also handle vendor-identifying vendor-encapsulated options,
2512: dhcp-option = vi-encap:13,17,.......
2513: The may be more that one "outer" to do, so group
2514: all the options which match each outer in turn. */
2515: for (opt = config_opts; opt; opt = opt->next)
2516: opt->flags &= ~DHOPT_ENCAP_DONE;
2517:
2518: for (opt = config_opts; opt; opt = opt->next)
2519: {
2520: int flags;
2521:
2522: if ((flags = (opt->flags & (DHOPT_ENCAPSULATE | DHOPT_RFC3925))))
2523: {
2524: int found = 0;
2525: struct dhcp_opt *o;
2526:
2527: if (opt->flags & DHOPT_ENCAP_DONE)
2528: continue;
2529:
2530: for (len = 0, o = config_opts; o; o = o->next)
2531: {
2532: int outer = flags & DHOPT_ENCAPSULATE ? o->u.encap : OPTION_VENDOR_IDENT_OPT;
2533:
2534: o->flags &= ~DHOPT_ENCAP_MATCH;
2535:
2536: if (!(o->flags & flags) || opt->u.encap != o->u.encap)
2537: continue;
2538:
2539: o->flags |= DHOPT_ENCAP_DONE;
2540: if (match_netid(o->netid, tagif, 1) &&
2541: ((o->flags & DHOPT_FORCE) || in_list(req_options, outer)))
2542: {
2543: o->flags |= DHOPT_ENCAP_MATCH;
2544: found = 1;
2545: len += do_opt(o, NULL, NULL, 0) + 2;
2546: }
2547: }
2548:
2549: if (found)
2550: {
2551: if (flags & DHOPT_ENCAPSULATE)
2552: do_encap_opts(config_opts, opt->u.encap, DHOPT_ENCAP_MATCH, mess, end, null_term);
2553: else if (len > 250)
2554: my_syslog(MS_DHCP | LOG_WARNING, _("cannot send RFC3925 option: too many options for enterprise number %d"), opt->u.encap);
2555: else if ((p = free_space(mess, end, OPTION_VENDOR_IDENT_OPT, len + 5)))
2556: {
2557: int swap_ent = htonl(opt->u.encap);
2558: memcpy(p, &swap_ent, 4);
2559: p += 4;
2560: *(p++) = len;
2561: for (o = config_opts; o; o = o->next)
2562: if (o->flags & DHOPT_ENCAP_MATCH)
2563: {
2564: len = do_opt(o, p + 2, NULL, 0);
2565: *(p++) = o->opt;
2566: *(p++) = len;
2567: p += len;
2568: }
2569: }
2570: }
2571: }
2572: }
2573:
2574: force_encap = prune_vendor_opts(tagif);
2575:
2576: if (context && pxe_arch != -1)
2577: {
2578: pxe_misc(mess, end, uuid);
2579: if (!pxe_uefi_workaround(pxe_arch, tagif, mess, context->local, now, 0))
2580: config_opts = pxe_opts(pxe_arch, tagif, context->local, now);
2581: }
2582:
2583: if ((force_encap || in_list(req_options, OPTION_VENDOR_CLASS_OPT)) &&
2584: do_encap_opts(config_opts, OPTION_VENDOR_CLASS_OPT, DHOPT_VENDOR_MATCH, mess, end, null_term) &&
2585: pxe_arch == -1 && !done_vendor_class && vendor_class_len != 0 &&
2586: (p = free_space(mess, end, OPTION_VENDOR_ID, vendor_class_len)))
2587: /* If we send vendor encapsulated options, and haven't already sent option 60,
2588: echo back the value we got from the client. */
2589: memcpy(p, daemon->dhcp_buff3, vendor_class_len);
2590:
2591: /* restore BOOTP anti-overload hack */
2592: if (!req_options || option_bool(OPT_NO_OVERRIDE))
2593: {
2594: mess->file[0] = f0;
2595: mess->sname[0] = s0;
2596: }
2597: }
2598:
2599: #endif
2600:
2601:
2602:
2603:
2604:
2605:
2606:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>