dhclient - Add 6-byte MAC address to the log entries for DHCP ACK/NAK/OFFER.
[dragonfly.git] / sbin / dhclient / options.c
1 /* $OpenBSD: src/sbin/dhclient/options.c,v 1.37 2009/03/10 23:19:36 krw Exp $ */
2
3 /* DHCP options parsing and reassembly. */
4
5 /*
6  * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of The Internet Software Consortium nor the names
19  *    of its contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * This software has been written for the Internet Software Consortium
37  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
38  * Enterprises.  To learn more about the Internet Software Consortium,
39  * see ``http://www.vix.com/isc''.  To learn more about Vixie
40  * Enterprises, see ``http://www.vix.com''.
41  */
42
43 #include <ctype.h>
44
45 #include "dhcpd.h"
46
47 int parse_option_buffer(struct option_data *, unsigned char *, int);
48
49 /*
50  * Parse options out of the specified buffer, storing addresses of
51  * option values in options and setting client->options_valid if
52  * no errors are encountered.
53  */
54 int
55 parse_option_buffer(struct option_data *options, unsigned char *buffer,
56     int length)
57 {
58         unsigned char *s, *t, *end = buffer + length;
59         int len, code;
60
61         for (s = buffer; *s != DHO_END && s < end; ) {
62                 code = s[0];
63
64                 /* Pad options don't have a length - just skip them. */
65                 if (code == DHO_PAD) {
66                         s++;
67                         continue;
68                 }
69
70                 /*
71                  * All options other than DHO_PAD and DHO_END have a
72                  * one-byte length field.
73                  */
74                 if (s + 2 > end)
75                         len = 0;
76                 else
77                         len = s[1];
78
79                 /*
80                  * If the option claims to extend beyond the end of the buffer
81                  * then mark the options buffer bad.
82                  */
83                 if (s + len + 2 > end) {
84                         warning("option %s (%d) larger than buffer.",
85                             dhcp_options[code].name, len);
86                         warning("rejecting bogus offer.");
87                         return (0);
88                 }
89                 /*
90                  * If we haven't seen this option before, just make
91                  * space for it and copy it there.
92                  */
93                 if (!options[code].data) {
94                         if (!(t = calloc(1, len + 1)))
95                                 error("Can't allocate storage for option %s.",
96                                     dhcp_options[code].name);
97                         /*
98                          * Copy and NUL-terminate the option (in case
99                          * it's an ASCII string).
100                          */
101                         memcpy(t, &s[2], len);
102                         t[len] = 0;
103                         options[code].len = len;
104                         options[code].data = t;
105                 } else {
106                         /*
107                          * If it's a repeat, concatenate it to whatever
108                          * we last saw.   This is really only required
109                          * for clients, but what the heck...
110                          */
111                         t = calloc(1, len + options[code].len + 1);
112                         if (!t)
113                                 error("Can't expand storage for option %s.",
114                                     dhcp_options[code].name);
115                         memcpy(t, options[code].data, options[code].len);
116                         memcpy(t + options[code].len, &s[2], len);
117                         options[code].len += len;
118                         t[options[code].len] = 0;
119                         free(options[code].data);
120                         options[code].data = t;
121                 }
122                 s += len + 2;
123         }
124
125         return (1);
126 }
127
128 /*
129  * Copy as many options as fit in buflen bytes of buf. Return the
130  * offset of the start of the last option copied. A caller can check
131  * to see if it's DHO_END to decide if all the options were copied.
132  */
133 int
134 cons_options(unsigned char *buf, const int buflen, struct option_data *options)
135 {
136         int ix, incr, length, bufix, code, lastopt = -1;
137
138         bzero(buf, buflen);
139
140         if (buflen > 3)
141                 memcpy(buf, DHCP_OPTIONS_COOKIE, 4);
142         bufix = 4;
143
144         for (code = DHO_SUBNET_MASK; code < DHO_END; code++) {
145                 if (!options[code].data)
146                         continue;
147
148                 length = options[code].len;
149                 if (bufix + length + 2*((length+254)/255) >= buflen)
150                         return (lastopt);
151
152                 lastopt = bufix;
153                 ix = 0;
154
155                 while (length) {
156                         incr = length > 255 ? 255 : length;
157
158                         buf[bufix++] = code;
159                         buf[bufix++] = incr;
160                         memcpy(buf + bufix, options[code].data + ix, incr);
161
162                         length -= incr;
163                         ix += incr;
164                         bufix += incr;
165                 }
166         }
167
168         if (bufix < buflen) {
169                 buf[bufix] = DHO_END;
170                 lastopt = bufix;
171         }
172
173         return (lastopt);
174 }
175
176 /*
177  * Format the specified option so that a human can easily read it.
178  */
179 char *
180 pretty_print_option(unsigned int code, unsigned char *data, int len,
181     int emit_commas, int emit_quotes)
182 {
183         static char optbuf[32768]; /* XXX */
184         int hunksize = 0, numhunk = -1, numelem = 0;
185         char fmtbuf[32], *op = optbuf;
186         int i, j, k, opleft = sizeof(optbuf);
187         unsigned char *dp = data;
188         struct in_addr foo;
189         char comma;
190
191         /* Code should be between 0 and 255. */
192         if (code > 255)
193                 error("pretty_print_option: bad code %d", code);
194
195         if (emit_commas)
196                 comma = ',';
197         else
198                 comma = ' ';
199
200         /* Figure out the size of the data. */
201         for (i = 0; dhcp_options[code].format[i]; i++) {
202                 if (!numhunk) {
203                         warning("%s: Excess information in format string: %s",
204                             dhcp_options[code].name,
205                             &(dhcp_options[code].format[i]));
206                         break;
207                 }
208                 numelem++;
209                 fmtbuf[i] = dhcp_options[code].format[i];
210                 switch (dhcp_options[code].format[i]) {
211                 case 'A':
212                         --numelem;
213                         fmtbuf[i] = 0;
214                         numhunk = 0;
215                         break;
216                 case 'X':
217                         for (k = 0; k < len; k++)
218                                 if (!isascii(data[k]) ||
219                                     !isprint(data[k]))
220                                         break;
221                         if (k == len) {
222                                 fmtbuf[i] = 't';
223                                 numhunk = -2;
224                         } else {
225                                 fmtbuf[i] = 'x';
226                                 hunksize++;
227                                 comma = ':';
228                                 numhunk = 0;
229                         }
230                         fmtbuf[i + 1] = 0;
231                         break;
232                 case 't':
233                         fmtbuf[i] = 't';
234                         fmtbuf[i + 1] = 0;
235                         numhunk = -2;
236                         break;
237                 case 'I':
238                 case 'l':
239                 case 'L':
240                         hunksize += 4;
241                         break;
242                 case 's':
243                 case 'S':
244                         hunksize += 2;
245                         break;
246                 case 'b':
247                 case 'B':
248                 case 'f':
249                         hunksize++;
250                         break;
251                 case 'e':
252                         break;
253                 default:
254                         warning("%s: garbage in format string: %s",
255                             dhcp_options[code].name,
256                             &(dhcp_options[code].format[i]));
257                         break;
258                 }
259         }
260
261         /* Check for too few bytes... */
262         if (hunksize > len) {
263                 warning("%s: expecting at least %d bytes; got %d",
264                     dhcp_options[code].name, hunksize, len);
265                 return ("<error>");
266         }
267         /* Check for too many bytes... */
268         if (numhunk == -1 && hunksize < len)
269                 warning("%s: %d extra bytes",
270                     dhcp_options[code].name, len - hunksize);
271
272         /* If this is an array, compute its size. */
273         if (!numhunk)
274                 numhunk = len / hunksize;
275         /* See if we got an exact number of hunks. */
276         if (numhunk > 0 && numhunk * hunksize < len)
277                 warning("%s: %d extra bytes at end of array",
278                     dhcp_options[code].name, len - numhunk * hunksize);
279
280         /* A one-hunk array prints the same as a single hunk. */
281         if (numhunk < 0)
282                 numhunk = 1;
283
284         /* Cycle through the array (or hunk) printing the data. */
285         for (i = 0; i < numhunk; i++) {
286                 for (j = 0; j < numelem; j++) {
287                         int opcount;
288                         size_t oplen;
289                         switch (fmtbuf[j]) {
290                         case 't':
291                                 if (emit_quotes) {
292                                         *op++ = '"';
293                                         opleft--;
294                                 }
295                                 for (; dp < data + len; dp++) {
296                                         if (!isascii(*dp) ||
297                                             !isprint(*dp)) {
298                                                 if (dp + 1 != data + len ||
299                                                     *dp != 0) {
300                                                         size_t oplen;
301                                                         snprintf(op, opleft,
302                                                             "\\%03o", *dp);
303                                                         oplen = strlen(op);
304                                                         op += oplen;
305                                                         opleft -= oplen;
306                                                 }
307                                         } else if (*dp == '"' ||
308                                             *dp == '\'' ||
309                                             *dp == '$' ||
310                                             *dp == '`' ||
311                                             *dp == '\\') {
312                                                 *op++ = '\\';
313                                                 *op++ = *dp;
314                                                 opleft -= 2;
315                                         } else {
316                                                 *op++ = *dp;
317                                                 opleft--;
318                                         }
319                                 }
320                                 if (emit_quotes) {
321                                         *op++ = '"';
322                                         opleft--;
323                                 }
324
325                                 *op = 0;
326                                 break;
327                         case 'I':
328                                 foo.s_addr = htonl(getULong(dp));
329                                 opcount = strlcpy(op, inet_ntoa(foo), opleft);
330                                 if (opcount >= opleft)
331                                         goto toobig;
332                                 opleft -= opcount;
333                                 dp += 4;
334                                 break;
335                         case 'l':
336                                 opcount = snprintf(op, opleft, "%ld",
337                                     (long)getLong(dp));
338                                 if (opcount >= opleft || opcount == -1)
339                                         goto toobig;
340                                 opleft -= opcount;
341                                 dp += 4;
342                                 break;
343                         case 'L':
344                                 opcount = snprintf(op, opleft, "%ld",
345                                     (unsigned long)getULong(dp));
346                                 if (opcount >= opleft || opcount == -1)
347                                         goto toobig;
348                                 opleft -= opcount;
349                                 dp += 4;
350                                 break;
351                         case 's':
352                                 opcount = snprintf(op, opleft, "%d",
353                                     getShort(dp));
354                                 if (opcount >= opleft || opcount == -1)
355                                         goto toobig;
356                                 opleft -= opcount;
357                                 dp += 2;
358                                 break;
359                         case 'S':
360                                 opcount = snprintf(op, opleft, "%d",
361                                     getUShort(dp));
362                                 if (opcount >= opleft || opcount == -1)
363                                         goto toobig;
364                                 opleft -= opcount;
365                                 dp += 2;
366                                 break;
367                         case 'b':
368                                 opcount = snprintf(op, opleft, "%d",
369                                     *(char *)dp++);
370                                 if (opcount >= opleft || opcount == -1)
371                                         goto toobig;
372                                 opleft -= opcount;
373                                 break;
374                         case 'B':
375                                 opcount = snprintf(op, opleft, "%d", *dp++);
376                                 if (opcount >= opleft || opcount == -1)
377                                         goto toobig;
378                                 opleft -= opcount;
379                                 break;
380                         case 'x':
381                                 opcount = snprintf(op, opleft, "%x", *dp++);
382                                 if (opcount >= opleft || opcount == -1)
383                                         goto toobig;
384                                 opleft -= opcount;
385                                 break;
386                         case 'f':
387                                 opcount = strlcpy(op,
388                                     *dp++ ? "true" : "false", opleft);
389                                 if (opcount >= opleft)
390                                         goto toobig;
391                                 opleft -= opcount;
392                                 break;
393                         default:
394                                 warning("Unexpected format code %c", fmtbuf[j]);
395                         }
396                         oplen = strlen(op);
397                         op += oplen;
398                         opleft -= oplen;
399                         if (opleft < 1)
400                                 goto toobig;
401                         if (j + 1 < numelem && comma != ':') {
402                                 *op++ = ' ';
403                                 opleft--;
404                         }
405                 }
406                 if (i + 1 < numhunk) {
407                         *op++ = comma;
408                         opleft--;
409                 }
410                 if (opleft < 1)
411                         goto toobig;
412
413         }
414         return (optbuf);
415  toobig:
416         warning("dhcp option too large");
417         return ("<error>");
418 }
419
420 void
421 do_packet(int len, unsigned int from_port, struct iaddr from,
422     struct hardware *hfrom)
423 {
424         struct dhcp_packet *packet = &client->packet;
425         struct option_data options[256];
426         struct iaddrlist *ap;
427         void (*handler)(struct iaddr, struct option_data *);
428         char *type;
429         int i, options_valid = 1;
430
431         if (packet->hlen > sizeof(packet->chaddr)) {
432                 note("Discarding packet with invalid hlen.");
433                 return;
434         }
435
436         /*
437          * Silently drop the packet if the client hardware address in the
438          * packet is not the hardware address of the interface being managed.
439          */
440         if ((ifi->hw_address.hlen != packet->hlen) ||
441             (memcmp(ifi->hw_address.haddr, packet->chaddr, packet->hlen)))
442                 return;
443
444         memset(options, 0, sizeof(options));
445
446         if (memcmp(&packet->options, DHCP_OPTIONS_COOKIE, 4) == 0) {
447                 /* Parse the BOOTP/DHCP options field. */
448                 options_valid = parse_option_buffer(options,
449                     &packet->options[4], sizeof(packet->options) - 4);
450
451                 /* Only DHCP packets have overload areas for options. */
452                 if (options_valid &&
453                     options[DHO_DHCP_MESSAGE_TYPE].data &&
454                     options[DHO_DHCP_OPTION_OVERLOAD].data) {
455                         if (options[DHO_DHCP_OPTION_OVERLOAD].data[0] & 1)
456                                 options_valid = parse_option_buffer(options,
457                                     (unsigned char *)packet->file,
458                                     sizeof(packet->file));
459                         if (options_valid &&
460                             options[DHO_DHCP_OPTION_OVERLOAD].data[0] & 2)
461                                 options_valid = parse_option_buffer(options,
462                                     (unsigned char *)packet->sname,
463                                     sizeof(packet->sname));
464                 }
465         }
466
467         type = "";
468         handler = NULL;
469
470         if (options[DHO_DHCP_MESSAGE_TYPE].data) {
471                 /* Always try a DHCP packet, even if a bad option was seen. */
472                 switch (options[DHO_DHCP_MESSAGE_TYPE].data[0]) {
473                 case DHCPOFFER:
474                         handler = dhcpoffer;
475                         type = "DHCPOFFER";
476                         break;
477                 case DHCPNAK:
478                         handler = dhcpnak;
479                         type = "DHCPNACK";
480                         break;
481                 case DHCPACK:
482                         handler = dhcpack;
483                         type = "DHCPACK";
484                         break;
485                 default:
486                         break;
487                 }
488         } else if (options_valid && packet->op == BOOTREPLY) {
489                 handler = dhcpoffer;
490                 type = "BOOTREPLY";
491         }
492
493         if (handler && client->xid == client->packet.xid) {
494                 if (hfrom->hlen == 6)
495                         note("%s from %s (%s)", type, piaddr(from),
496                             ether_ntoa((struct ether_addr *)hfrom->haddr));
497                 else
498                         note("%s from %s", type, piaddr(from));
499         } else
500                 handler = NULL;
501
502         for (ap = config->reject_list; ap && handler; ap = ap->next)
503                 if (addr_eq(from, ap->addr)) {
504                         note("%s from %s rejected.", type, piaddr(from));
505                         handler = NULL;
506                 }
507
508         if (handler)
509                 (*handler)(from, options);
510
511         for (i = 0; i < 256; i++)
512                 if (options[i].len && options[i].data)
513                         free(options[i].data);
514 }