source: trunk/minix/commands/dhcpd/tags.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 24.4 KB
Line 
1/* tags.c - Obtain DHCP tags from the config file
2 * Author: Kees J. Bot
3 * 16 Dec 2000
4 */
5#include <sys/types.h>
6#include <stdio.h>
7#include <stddef.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <fcntl.h>
11#include <errno.h>
12#include <signal.h>
13#include <string.h>
14#include <time.h>
15#include <limits.h>
16#include <configfile.h>
17#include <sys/ioctl.h>
18#include <sys/asynchio.h>
19#include <net/hton.h>
20#include <net/gen/socket.h>
21#include <net/gen/netdb.h>
22#include <net/gen/in.h>
23#include <net/gen/inet.h>
24#include <net/gen/ether.h>
25#include <net/gen/if_ether.h>
26#include <net/gen/eth_hdr.h>
27#include <net/gen/ip_hdr.h>
28#include <net/gen/udp.h>
29#include <net/gen/udp_hdr.h>
30#include <net/gen/dhcp.h>
31#include "dhcpd.h"
32
33#define doff(field) offsetof(dhcp_t, field)
34
35void settag(dhcp_t *dp, int tag, void *data, size_t len)
36{
37 if (!dhcp_settag(dp, tag, data, len)) {
38 /* Oops, it didn't fit? Is this really Minix??? */
39 fprintf(stderr,
40 "%s: DHCP packet too big, please trim the configuration\n",
41 program);
42 exit(1);
43 }
44}
45
46static int name2ip(ipaddr_t *pip, const char *name, ipaddr_t ifip)
47{
48 /* Translate a name to an IP address, preferably from the hosts file,
49 * but also from the DNS if being a server. Prefer the address closest
50 * to the interface with IP address 'ifip' if there are choices..
51 */
52 extern struct hostent *_gethostent(void); /* File reading versions. */
53 extern void _endhostent(void);
54 struct hostent *he;
55 size_t len= strlen(name);
56 u32_t d, distance= -1;
57 ipaddr_t ip;
58 int i;
59 char *hn;
60
61 /* Already an IP address? */
62 if (inet_aton(name, pip)) return 1;
63
64 /* In the hosts file? */
65 while ((he= _gethostent()) != nil) {
66 hn= he->h_name;
67 i= -1;
68 do {
69 if (strncasecmp(name, hn, len) == 0
70 && (hn[len] == 0 || hn[len] == '.')
71 ) {
72 memcpy(&ip, he->h_addr, sizeof(ip));
73 d= ntohl(ip) ^ ntohl(ifip);
74 if (d < distance) {
75 *pip= ip;
76 distance= d;
77 }
78 break;
79 }
80 } while ((hn= he->h_aliases[++i]) != nil);
81 }
82 _endhostent();
83 if (distance < -1) return 1;
84
85 /* Nothing? Try the real DNS if being a server. */
86 if (serving) {
87 if ((he= gethostbyname(name)) != nil && he->h_addrtype == AF_INET) {
88 /* Select the address closest to 'ifip'. */
89 for (i= 0; he->h_addr_list[i] != nil; i++) {
90 memcpy(&ip, he->h_addr_list[i], sizeof(ip));
91 d= ntohl(ip) ^ ntohl(ifip);
92 if (d < distance) {
93 *pip= ip;
94 distance= d;
95 }
96 }
97 return 1;
98 }
99 }
100 return 0;
101}
102
103static char *ip2name(ipaddr_t ip)
104{
105 /* Translate an IP address to a name, etc, etc. */
106 extern struct hostent *_gethostent(void); /* File reading versions. */
107 extern void _endhostent(void);
108 struct hostent *he;
109
110 /* In the hosts file? */
111 while ((he= _gethostent()) != nil) {
112 if (memcmp(he->h_addr, &ip, sizeof(ip)) == 0) break;
113 }
114 _endhostent();
115
116 /* Nothing? Try the real DNS if being a server. */
117 if (he == nil && serving) {
118 he= gethostbyaddr((char *) &ip, sizeof(ip), AF_INET);
119 }
120 return he != nil ? he->h_name : nil;
121}
122
123static int cidr_aton(const char *cidr, ipaddr_t *addr, ipaddr_t *mask)
124{
125 char *slash, *check;
126 ipaddr_t a;
127 int ok;
128 unsigned long len;
129
130 if ((slash= strchr(cidr, '/')) == nil) return 0;
131
132 *slash++= 0;
133 ok= inet_aton(cidr, &a);
134
135 len= strtoul(slash, &check, 10);
136 if (check == slash || *check != 0 || len > 32) ok= 0;
137
138 *--slash= '/';
139 if (!ok) return 0;
140 *addr= a;
141 *mask= htonl(len == 0 ? 0 : (0xFFFFFFFFUL << (32-len)) & 0xFFFFFFFFUL);
142 return 1;
143}
144
145char *cidr_ntoa(ipaddr_t addr, ipaddr_t mask)
146{
147 ipaddr_t testmask= 0xFFFFFFFFUL;
148 int n;
149 static char result[sizeof("255.255.255.255/255.255.255.255")];
150
151 for (n= 32; n >= 0; n--) {
152 if (mask == htonl(testmask)) break;
153 testmask= (testmask << 1) & 0xFFFFFFFFUL;
154 }
155
156 sprintf(result, "%s/%-2d", inet_ntoa(addr), n);
157 if (n == -1) strcpy(strchr(result, '/')+1, inet_ntoa(mask));
158 return result;
159}
160
161static size_t ascii2octet(u8_t *b, size_t size, const char *a)
162{
163 /* Convert a series of hex digit pairs to an octet (binary) array at
164 * 'b' with length 'size'. Return the number of octets in 'a' or
165 * -1 on error.
166 */
167 size_t len;
168 int n, c;
169
170 len= 0;
171 n= 0;
172 while ((c= *a++) != 0) {
173 if (between('0', c, '9')) c= (c - '0') + 0x0;
174 else
175 if (between('a', c, 'f')) c= (c - 'a') + 0xa;
176 else
177 if (between('A', c, 'F')) c= (c - 'A') + 0xA;
178 else {
179 return -1;
180 }
181
182 if (n == 0) {
183 if (len < size) b[len] = c << 4;
184 } else {
185 if (len < size) b[len] |= c;
186 len++;
187 }
188 n ^= 1;
189 }
190 return n == 0 ? len : -1;
191}
192
193void ether2clid(u8_t *clid, ether_addr_t *eth)
194{
195 /* Convert an Ethernet address to the default client ID form. */
196 clid[0]= DHCP_HTYPE_ETH;
197 memcpy(clid+1, eth, DHCP_HLEN_ETH);
198}
199
200static size_t ascii2clid(u8_t *clid, const char *a)
201{
202 /* Convert an ethernet address, or a series of hex digits to a client ID.
203 * Return its length if ok, otherwise -1.
204 */
205 size_t len;
206 ether_addr_t *eth;
207
208 if ((eth= ether_aton(a)) != nil) {
209 ether2clid(clid, eth);
210 len= 1+DHCP_HLEN_ETH;
211 } else {
212 len= ascii2octet(clid, CLID_MAX, a);
213 }
214 return len;
215}
216
217static config_t *dhcpconf; /* In-core DHCP configuration. */
218
219/* DHCP tag types. */
220typedef enum { TT_ASCII, TT_BOOLEAN, TT_IP, TT_NUMBER, TT_OCTET } tagtype_t;
221
222/* DHCP/BOOTP tag definitions. */
223typedef struct tagdef {
224 u8_t tag; /* Tag number. */
225 u8_t type; /* Type and flags. */
226 u8_t gran; /* Granularity. */
227 u8_t max; /* Maximum number of arguments. */
228 const char *name; /* Defined name. */
229} tagdef_t;
230
231#define TF_TYPE 0x07 /* To mask out the type. */
232#define TF_STATIC 0x08 /* "Static", i.e. a struct field. */
233#define TF_RO 0x10 /* Read-only, user can't set. */
234
235/* List of static DHCP fields. The tag field is misused here as an offset
236 * into the DHCP structure.
237 */
238static tagdef_t statictag[] = {
239 { doff(op), TT_NUMBER|TF_STATIC|TF_RO, 1, 1, "op" },
240 { doff(htype), TT_NUMBER|TF_STATIC|TF_RO, 1, 1, "htype" },
241 { doff(hlen), TT_NUMBER|TF_STATIC|TF_RO, 1, 1, "hlen" },
242 { doff(hops), TT_NUMBER|TF_STATIC|TF_RO, 1, 1, "hops" },
243 { doff(xid), TT_NUMBER|TF_STATIC|TF_RO, 4, 1, "xid" },
244 { doff(secs), TT_NUMBER|TF_STATIC|TF_RO, 2, 1, "secs" },
245 { doff(flags), TT_NUMBER|TF_STATIC|TF_RO, 2, 1, "flags" },
246 { doff(ciaddr), TT_IP|TF_STATIC|TF_RO, 1, 1, "ciaddr" },
247 { doff(yiaddr), TT_IP|TF_STATIC|TF_RO, 1, 1, "yiaddr" },
248 { doff(siaddr), TT_IP|TF_STATIC, 1, 1, "siaddr" },
249 { doff(giaddr), TT_IP|TF_STATIC|TF_RO, 1, 1, "giaddr" },
250 { doff(chaddr), TT_OCTET|TF_STATIC|TF_RO, 1, 16, "chaddr" },
251 { doff(sname), TT_ASCII|TF_STATIC, 1, 64, "sname" },
252 { doff(file), TT_ASCII|TF_STATIC, 1, 128, "file" },
253};
254#define N_STATIC arraysize(statictag)
255
256static tagdef_t alltagdef[N_STATIC + 254]; /* List of tag definitions. */
257#define tagdef (alltagdef+N_STATIC-1) /* Just the optional ones. */
258
259#define tagdefined(tp) ((tp)->name != nil)
260
261static void inittagdef(void)
262{
263 /* Initialize the tag definitions from the "tag" commands in the config
264 * file.
265 */
266 int t;
267 tagdef_t *tp;
268 static tagdef_t predef[] = {
269 { DHCP_TAG_NETMASK, TT_IP, 1, 1, "netmask" },
270 { DHCP_TAG_GATEWAY, TT_IP, 1, 255, "gateway" },
271 { DHCP_TAG_DNS, TT_IP, 1, 255, "DNSserver" },
272 };
273 static char *typenames[] = { "ascii", "boolean", "ip", "number", "octet" };
274 config_t *cfg;
275 static u8_t rotags[] = {
276 DHCP_TAG_REQIP, DHCP_TAG_OVERLOAD, DHCP_TAG_TYPE, DHCP_TAG_SERVERID,
277 DHCP_TAG_REQPAR, DHCP_TAG_MESSAGE, DHCP_TAG_MAXDHCP
278 };
279
280 for (t= 1; t <= 254; t++) {
281 tp= &tagdef[t];
282 tp->tag= t;
283 tp->type= TT_OCTET;
284 tp->name= nil;
285 }
286
287 /* Set the static and "all Minix needs" tags. */
288 memcpy(alltagdef, statictag, sizeof(statictag));
289 for (tp= predef; tp < arraylimit(predef); tp++) tagdef[tp->tag] = *tp;
290
291 /* Search for tag definitions in the config file. */
292 for (cfg= dhcpconf; cfg != nil; cfg= cfg->next) {
293 config_t *cmd= cfg->list;
294
295 if (strcasecmp(cmd->word, "tag") == 0) {
296 if (config_length(cmd) == 6
297 && (cmd->next->flags & CFG_DULONG)
298 && config_isatom(cmd->next->next)
299 && config_isatom(cmd->next->next->next)
300 && (cmd->next->next->next->next->flags & CFG_DULONG)
301 && (cmd->next->next->next->next->next->flags & CFG_DULONG)
302 ) {
303 unsigned long tag, gran, max;
304 const char *name, *typename;
305 unsigned type;
306
307 tag= strtoul(cmd->next->word, nil, 10);
308 name= cmd->next->next->word;
309 typename= cmd->next->next->next->word;
310 gran= strtoul(cmd->next->next->next->next->word, nil, 10);
311 max= strtoul(cmd->next->next->next->next->next->word, nil, 10);
312
313 for (type= 0; type < arraysize(typenames); type++) {
314 if (strcasecmp(typename, typenames[type]) == 0) break;
315 }
316
317 if (!(1 <= tag && tag <= 254)
318 || !(type < arraysize(typenames))
319 || !((type == TT_NUMBER
320 && (gran == 1 || gran == 2 || gran == 4))
321 || (type != TT_NUMBER && 1 <= gran && gran <= 16))
322 || !(max <= 255)
323 ) {
324 fprintf(stderr,
325 "\"%s\", line %u: Tag definition is incorrect\n",
326 cmd->file, cmd->line);
327 exit(1);
328 }
329
330 tp= &tagdef[(int)tag];
331 tp->type= type;
332 tp->name= name;
333 tp->gran= gran;
334 tp->max= max;
335 } else {
336 fprintf(stderr,
337 "\"%s\", line %u: Usage: tag number name type granularity max\n",
338 cmd->file, cmd->line);
339 exit(1);
340 }
341 }
342 }
343
344 /* Many DHCP tags are not for the user to play with. */
345 for (t= 0; t < arraysize(rotags); t++) tagdef[rotags[t]].type |= TF_RO;
346}
347
348static tagdef_t *tagdefbyname(const char *name)
349{
350 /* Find a tag definition by the name of the tag. Return null if not
351 * defined.
352 */
353 tagdef_t *tp;
354
355 for (tp= alltagdef; tp < arraylimit(alltagdef); tp++) {
356 if (tagdefined(tp) && strcasecmp(tp->name, name) == 0) return tp;
357 }
358 return nil;
359}
360
361void initdhcpconf(void)
362{
363 /* Read/refresh configuration from the DHCP configuration file. */
364 dhcpconf= config_read(configfile, 0, dhcpconf);
365 if (config_renewed(dhcpconf)) inittagdef();
366}
367
368static void configtag(dhcp_t *dp, config_t *cmd, ipaddr_t ifip)
369{
370 /* Add a tag to a DHCP packet from the config file. */
371 tagdef_t *tp;
372 u8_t data[260], *d;
373 size_t i;
374 int delete= 0;
375
376 if (strcasecmp(cmd->word, "no") == 0) {
377 if (config_length(cmd) != 2 || !config_isatom(cmd->next)) {
378 fprintf(stderr, "\"%s\", line %u: Usage: no tag-name\n",
379 cmd->file, cmd->line);
380 exit(1);
381 }
382 cmd= cmd->next;
383 delete= 1;
384 }
385
386 if ((tp= tagdefbyname(cmd->word)) == nil) {
387 fprintf(stderr, "\"%s\", line %u: Unknown tag '%s'\n",
388 cmd->file, cmd->line, cmd->word);
389 exit(1);
390 }
391
392 if (tp->type & TF_RO) {
393 fprintf(stderr, "\"%s\", line %u: Tag '%s' can't be configured\n",
394 cmd->file, cmd->line, cmd->word);
395 exit(1);
396 }
397
398 i= 0;
399 d= data;
400 if (!delete) {
401 config_t *arg= cmd->next;
402 do {
403 switch (tp->type & TF_TYPE) {
404 case TT_ASCII: {
405 if (arg == nil || !config_isatom(arg) || arg->next != nil) {
406 fprintf(stderr, "\"%s\", line %u: Usage: %s string\n",
407 cmd->file, cmd->line, cmd->word);
408 exit(1);
409 }
410 strncpy((char *) data, arg->word, sizeof(data));
411 d += i = strnlen((char *) data, sizeof(data));
412 break;}
413 case TT_BOOLEAN: {
414 if (arg == nil || !config_isatom(arg)
415 || !(strcasecmp(arg->word, "false") == 0
416 || strcasecmp(arg->word, "true") == 0)
417 ) {
418 fprintf(stderr,
419 "\"%s\", line %u: Usage: %s false|true ...\n",
420 cmd->file, cmd->line, cmd->word);
421 exit(1);
422 }
423 if (d < arraylimit(data)) {
424 *d++ = (arg->word[0] != 'f' && arg->word[0] != 'F');
425 }
426 i++;
427 break;}
428 case TT_IP: {
429 ipaddr_t ip;
430 unsigned long len;
431 char *end;
432
433 if (arg == nil || !config_isatom(arg)) {
434 fprintf(stderr, "\"%s\", line %u: Usage: %s host ...\n",
435 cmd->file, cmd->line, cmd->word);
436 exit(1);
437 }
438 if (arg->word[0] == '/'
439 && between(1, len= strtoul(arg->word+1, &end, 10), 31)
440 && *end == 0
441 ) {
442 ip= htonl((0xFFFFFFFFUL << (32-len)) & 0xFFFFFFFFUL);
443 } else
444 if (!name2ip(&ip, arg->word, ifip)) {
445 fprintf(stderr,
446 "\"%s\", line %u: Can't translate %s to an IP address\n",
447 arg->file, arg->line, arg->word);
448 exit(1);
449 }
450 if (d <= arraylimit(data) - sizeof(ip)) {
451 memcpy(d, &ip, sizeof(ip));
452 d += sizeof(ip);
453 }
454 i++;
455 break;}
456 case TT_NUMBER: {
457 unsigned long n;
458 int g;
459
460 if (arg == nil || !(arg->flags & CFG_CLONG)) {
461 fprintf(stderr, "\"%s\", line %u: Usage: %s number ...\n",
462 cmd->file, cmd->line, cmd->word);
463 exit(1);
464 }
465 n= strtoul(arg->word, nil, 0);
466 g= tp->gran;
467 do {
468 if (d <= arraylimit(data)) *d++ = (n >> (--g * 8)) & 0xFF;
469 } while (g != 0);
470 i++;
471 break;}
472 case TT_OCTET: {
473 if (arg == nil || !config_isatom(arg) || arg->next != nil) {
474 fprintf(stderr, "\"%s\", line %u: Usage: %s hexdigits\n",
475 cmd->file, cmd->line, cmd->word);
476 exit(1);
477 }
478 i= ascii2octet(data, sizeof(data), arg->word);
479 if (i == -1) {
480 fprintf(stderr,
481 "\"%s\", line %u: %s: Bad hexdigit string\n",
482 arg->file, arg->line, arg->word);
483 exit(1);
484 }
485 d= data + i;
486 break;}
487 }
488 } while ((arg= arg->next) != nil);
489
490 if (d > data + 255) {
491 fprintf(stderr, "\"%s\", line %u: Tag value is way too big\n",
492 cmd->file, cmd->line);
493 exit(1);
494 }
495 if ((tp->type & TF_TYPE) != TT_NUMBER && (i % tp->gran) != 0) {
496 fprintf(stderr,
497 "\"%s\", line %u: Expected a multiple of %d initializers\n",
498 cmd->file, cmd->line, tp->gran);
499 exit(1);
500 }
501 if (tp->max != 0 && i > tp->max) {
502 fprintf(stderr,
503 "\"%s\", line %u: Got %d initializers, can have only %d\n",
504 cmd->file, cmd->line, (int) i, tp->max);
505 exit(1);
506 }
507 }
508 if (tp->type & TF_STATIC) {
509 size_t len= tp->gran * tp->max;
510 if ((tp->type & TF_TYPE) == TT_IP) len *= sizeof(ipaddr_t);
511 memset(B(dp) + tp->tag, 0, len);
512 memcpy(B(dp) + tp->tag, data, (d - data));
513 } else {
514 settag(dp, tp->tag, data, (d - data));
515 }
516}
517
518int makedhcp(dhcp_t *dp, u8_t *class, size_t calen, u8_t *client, size_t cilen,
519 ipaddr_t ip, ipaddr_t ifip, network_t *np)
520{
521 /* Fill in a DHCP packet at 'dp' for the host identified by the
522 * (class, client, ip) combination. Makedhcp is normally called twice,
523 * once to find the IP address (so ip == 0) and once again to find all
524 * data that goes with that IP address (ip != 0). On the first call the
525 * return value of this function should be ignored and only 'yiaddr'
526 * checked and used as 'ip' on the next pass. True is returned iff there
527 * is information for the client on the network at interface address
528 * 'ifip', by checking if the 'ip' and 'ifip' are on the same network.
529 * If np is nonnull then we are working for one of our own interfaces, so
530 * options can be set and adjourning interfaces can be programmed.
531 */
532 config_t *todo[16];
533 size_t ntodo= 0;
534 ipaddr_t hip, mask;
535 u8_t *pmask;
536 char *hostname;
537 u32_t distance= -1;
538
539 initdhcpconf();
540
541 /* Start creating a packet. */
542 dhcp_init(dp);
543 dp->op= DHCP_BOOTREPLY;
544
545 /* The initial TODO list is the whole DHCP config. */
546 todo[ntodo++]= dhcpconf;
547
548 while (ntodo > 0) {
549 config_t *cmd, *follow;
550
551 if (todo[ntodo-1] == nil) { ntodo--; continue; }
552 cmd= todo[ntodo-1]->list;
553 todo[ntodo-1]= todo[ntodo-1]->next;
554
555 follow= nil; /* Macro or list to follow next? */
556
557 if (strcasecmp(cmd->word, "client") == 0) {
558 u8_t cfgid[CLID_MAX];
559 size_t cfglen;
560 char *name;
561 int ifno;
562 u32_t d;
563
564 if (between(3, config_length(cmd), 5)
565 && config_isatom(cmd->next)
566 && (cfglen= ascii2clid(cfgid, cmd->next->word)) != -1
567 && config_isatom(cmd->next->next)
568 && (((ifno= ifname2if(cmd->next->next->word)) == -1
569 && config_length(cmd) <= 4)
570 || ((ifno= ifname2if(cmd->next->next->word)) != -1
571 && config_length(cmd) >= 4
572 && config_isatom(cmd->next->next->next)))
573 ) {
574 if (cilen == cfglen && memcmp(client, cfgid, cilen) == 0
575 && (ifno == -1 || np == nil || ifno == np->n)
576 ) {
577 config_t *atname= cmd->next->next;
578 if (ifno != -1) atname= atname->next;
579 name= atname->word;
580
581 if (name2ip(&hip, name, ifip) && (ip == 0 || ip == hip)) {
582 d= ntohl(hip) ^ ntohl(ifip);
583 if (d < distance) {
584 dp->yiaddr= hip;
585 follow= atname->next;
586 distance= d;
587 }
588 }
589 }
590 } else {
591 fprintf(stderr,
592 "\"%s\", line %u: Usage: client ID [ip#] host [macro|{params}]\n",
593 cmd->file, cmd->line);
594 exit(1);
595 }
596 } else
597 if (strcasecmp(cmd->word, "class") == 0) {
598 config_t *clist;
599 int match;
600
601 match= 0;
602 for (clist= cmd->next; clist != nil
603 && clist->next != nil
604 && config_isatom(clist); clist= clist->next) {
605 if (calen > 0
606 && strncmp(clist->word, (char *) class, calen) == 0
607 ) {
608 match= 1;
609 }
610 }
611 if (clist == cmd->next || clist->next != nil) {
612 fprintf(stderr,
613 "\"%s\", line %u: Usage: class class-name ... macro|{params}\n",
614 cmd->file, cmd->line);
615 }
616 if (match) follow= clist;
617 } else
618 if (strcasecmp(cmd->word, "host") == 0) {
619 if (config_length(cmd) == 3
620 && config_isatom(cmd->next)
621 ) {
622 if (ip != 0) {
623 if (cidr_aton(cmd->next->word, &hip, &mask)) {
624 if (((hip ^ ip) & mask) == 0) {
625 if (!gettag(dp, DHCP_TAG_NETMASK, nil, nil)) {
626 settag(dp, DHCP_TAG_NETMASK,
627 &mask, sizeof(mask));
628 }
629 dp->yiaddr= ip;
630 follow= cmd->next->next;
631 }
632 } else
633 if (name2ip(&hip, cmd->next->word, ifip)) {
634 if (hip == ip) {
635 dp->yiaddr= ip;
636 follow= cmd->next->next;
637 }
638 }
639 }
640 } else {
641 fprintf(stderr,
642 "\"%s\", line %u: Usage: host host-spec macro|{params}\n",
643 cmd->file, cmd->line);
644 exit(1);
645 }
646 } else
647 if (strcasecmp(cmd->word, "interface") == 0) {
648 if (between(3, config_length(cmd), 4)
649 && config_isatom(cmd->next)
650 && config_isatom(cmd->next->next)
651 ) {
652 network_t *ifnp;
653
654 if (np != nil) {
655 if ((ifnp= if2net(ifname2if(cmd->next->word))) == nil) {
656 fprintf(stderr,
657 "\"%s\", line %u: Can't find interface %s\n",
658 cmd->next->file, cmd->next->line, cmd->next->word);
659 exit(1);
660 }
661 if (!name2ip(&hip, cmd->next->next->word, 0)) {
662 fprintf(stderr,
663 "\"%s\", line %u: Can't find IP address of %s\n",
664 cmd->next->next->file, cmd->next->next->line,
665 cmd->next->next->word);
666 exit(1);
667 }
668 ifnp->ip= hip;
669 if (ifnp == np) {
670 dp->yiaddr= hip;
671 follow= cmd->next->next->next;
672 }
673 }
674 } else {
675 fprintf(stderr,
676 "\"%s\", line %u: Usage: interface ip# host%s\n",
677 cmd->file, cmd->line, ntodo==1 ? " [macro|{params}]" : "");
678 exit(1);
679 }
680 } else
681 if (strcasecmp(cmd->word, "macro") == 0) {
682 if (config_length(cmd) == 2 && config_isatom(cmd->next)) {
683 follow= cmd->next;
684 } else
685 if (ntodo > 1) {
686 fprintf(stderr, "\"%s\", line %u: Usage: macro macro-name\n",
687 cmd->file, cmd->line);
688 exit(1);
689 }
690 } else
691 if (strcasecmp(cmd->word, "tag") == 0) {
692 if (ntodo > 1) {
693 fprintf(stderr,
694 "\"%s\", line %u: A %s can't be defined here\n",
695 cmd->file, cmd->line, cmd->word);
696 exit(1);
697 }
698 } else
699 if (strcasecmp(cmd->word, "option") == 0) {
700 int ifno;
701 network_t *ifnp;
702 config_t *opt;
703
704 if ((opt= cmd->next) != nil
705 && config_isatom(opt)
706 && (ifno= ifname2if(opt->word)) != -1
707 ) {
708 if ((ifnp= if2net(ifno)) == nil) {
709 fprintf(stderr,
710 "\"%s\", line %u: Interface %s is not enabled\n",
711 opt->file, opt->line, opt->word);
712 exit(1);
713 }
714 opt= opt->next;
715 } else {
716 ifnp= np;
717 }
718
719 if (between(1, config_length(opt), 2)
720 && config_isatom(opt)
721 && strcasecmp(opt->word, "server") == 0
722 && (opt->next == nil
723 || strcasecmp(opt->next->word, "inform") == 0)
724 ) {
725 if (np != nil) {
726 ifnp->flags |= NF_SERVING;
727 if (opt->next != nil) ifnp->flags |= NF_INFORM;
728 }
729 } else
730 if (config_length(opt) == 2
731 && config_isatom(opt)
732 && strcasecmp(opt->word, "relay") == 0
733 && config_isatom(opt->next)
734 ) {
735 if (np != nil) {
736 if (!name2ip(&hip, opt->next->word, ifip)) {
737 fprintf(stderr,
738 "\"%s\", line %u: Can't find IP address of %s\n",
739 opt->next->file, opt->next->line,
740 opt->next->word);
741 exit(1);
742 }
743 ifnp->flags |= NF_RELAYING;
744 ifnp->server= hip;
745 }
746 } else
747 if (config_length(opt) == 1
748 && config_isatom(opt)
749 && strcasecmp(opt->word, "possessive") == 0
750 ) {
751 if (np != nil) ifnp->flags |= NF_POSSESSIVE;
752 } else
753 if (config_length(opt) == 2
754 && config_isatom(opt)
755 && strcasecmp(opt->word, "hostname") == 0
756 && config_isatom(opt->next)
757 ) {
758 if (np != nil) np->hostname= opt->next->word;
759 } else {
760 fprintf(stderr, "\"%s\", line %u: Unknown option\n",
761 cmd->file, cmd->line);
762 exit(1);
763 }
764 } else {
765 /* Must be an actual data carrying tag. */
766 configtag(dp, cmd, ifip);
767 }
768
769 if (follow != nil) {
770 /* A client/class/host entry selects a macro or list that must
771 * be followed next.
772 */
773 config_t *macro;
774
775 if (config_isatom(follow)) { /* Macro name */
776 config_t *cfg;
777
778 for (cfg= dhcpconf; cfg != nil; cfg= cfg->next) {
779 macro= cfg->list;
780
781 if (strcasecmp(macro->word, "macro") == 0) {
782 if (config_length(macro) == 3
783 && config_isatom(macro->next)
784 && config_issub(macro->next->next)
785 ) {
786 if (strcasecmp(macro->next->word, follow->word) == 0
787 ) {
788 break;
789 }
790 } else {
791 fprintf(stderr,
792 "\"%s\", line %u: Usage: macro macro-name {params}\n",
793 macro->file, macro->line);
794 }
795 }
796 }
797 follow= cfg == nil ? nil : macro->next->next->list;
798 } else {
799 /* Simply a list of more tags and stuff. */
800 follow= follow->list;
801 }
802
803 if (ntodo == arraysize(todo)) {
804 fprintf(stderr, "\"%s\", line %u: Nesting is too deep\n",
805 follow->file, follow->line);
806 exit(1);
807 }
808 todo[ntodo++]= follow;
809 }
810 }
811
812 /* Check if the IP and netmask are OK for the interface. */
813 if (!gettag(dp, DHCP_TAG_NETMASK, &pmask, nil)) return 0;
814 memcpy(&mask, pmask, sizeof(mask));
815 if (((ip ^ ifip) & mask) != 0) return 0;
816
817 /* Fill in the hostname and/or domain. */
818 if ((hostname= ip2name(ip)) != nil) {
819 char *domain;
820
821 if ((domain= strchr(hostname, '.')) != nil) *domain++ = 0;
822
823 if (!gettag(dp, DHCP_TAG_HOSTNAME, nil, nil)) {
824 settag(dp, DHCP_TAG_HOSTNAME, hostname, strlen(hostname));
825 }
826
827 if (domain != nil && !gettag(dp, DHCP_TAG_DOMAIN, nil, nil)) {
828 settag(dp, DHCP_TAG_DOMAIN, domain, strlen(domain));
829 }
830 }
831
832 return 1;
833}
834
835static char *dhcpopname(int op)
836{
837 static char *onames[] = { "??\?", "REQUEST", "REPLY" };
838 return onames[op < arraysize(onames) ? op : 0];
839}
840
841char *dhcptypename(int type)
842{
843 static char *tnames[] = {
844 "??\?", "DISCOVER", "OFFER", "REQUEST", "DECLINE",
845 "ACK", "NAK", "RELEASE", "INFORM"
846 };
847 return tnames[type < arraysize(tnames) ? type : 0];
848}
849
850void printdhcp(dhcp_t *dp)
851{
852 /* Print the contents of a DHCP packet, usually for debug purposes. */
853 tagdef_t *tp;
854 u8_t *data, *ovld;
855 size_t i, len;
856
857 for (tp= alltagdef; tp < arraylimit(alltagdef); tp++) {
858 if (tp->type & TF_STATIC) {
859 data= B(dp) + tp->tag;
860 len= tp->gran * tp->max;
861 if ((tp->type & TF_TYPE) == TT_IP) len *= sizeof(ipaddr_t);
862 if (tp->tag == doff(chaddr)) len= dp->hlen;
863
864 /* Don't show uninteresting stuff. */
865 if (tp->tag == doff(htype) && dp->htype == DHCP_HTYPE_ETH) continue;
866
867 if (tp->tag == doff(hlen) && dp->hlen == DHCP_HLEN_ETH) continue;
868
869 if ((tp->tag == doff(file) || tp->tag == doff(sname))
870 && gettag(dp, DHCP_TAG_OVERLOAD, &ovld, nil)
871 && (ovld[0] & (tp->tag == doff(file) ? 1 : 2))
872 ) {
873 continue;
874 }
875 for (i= 0; i < len && data[i] == 0; i++) {}
876 if (i == len) continue;
877 } else {
878 if (!gettag(dp, tp->tag, &data, &len)) continue;
879 }
880
881 if (tagdefined(tp)) {
882 printf("\t%s =", tp->name);
883 } else {
884 printf("\tT%d =", tp->tag);
885 }
886
887 i= 0;
888 while (i < len) {
889 switch (tp->type & TF_TYPE) {
890 case TT_ASCII: {
891 printf(" \"%.*s\"", (int) len, data);
892 i= len;
893 break;}
894 case TT_BOOLEAN: {
895 printf(data[i++] == 0 ? " false" : " true");
896 break;}
897 case TT_IP: {
898 ipaddr_t ip;
899 memcpy(&ip, data+i, sizeof(ip));
900 printf(" %s", inet_ntoa(ip));
901 i += sizeof(ip);
902 break;}
903 case TT_NUMBER: {
904 u32_t n= 0;
905 int g= tp->gran;
906
907 do n= (n << 8) | data[i++]; while (--g != 0);
908 printf(" %lu", (unsigned long) n);
909 if ((tp->type & TF_STATIC) && tp->tag == doff(op)) {
910 printf(" (%s)", dhcpopname(n));
911 }
912 if (!(tp->type & TF_STATIC) && tp->tag == DHCP_TAG_TYPE) {
913 printf(" (%s)", dhcptypename(n));
914 }
915 break;}
916 case TT_OCTET: {
917 if (i == 0) fputc(' ', stdout);
918 printf("%02X", data[i++]);
919 break;}
920 }
921 }
922 fputc('\n', stdout);
923 }
924}
Note: See TracBrowser for help on using the repository browser.