kernel - Minor cleanup swtch.s
[dragonfly.git] / sbin / routed / parms.c
blobe005e762450cb2c90bbb2f16150f55ef75cdcfcc
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * $FreeBSD: src/sbin/routed/parms.c,v 1.7.2.1 2000/08/14 17:00:03 sheldonh Exp $
32 #include "defs.h"
33 #include "pathnames.h"
34 #include <sys/stat.h>
36 #if !defined(sgi) && !defined(__NetBSD__)
37 static char sccsid[] __attribute__((unused)) = "@(#)if.c 8.1 (Berkeley) 6/5/93";
38 #elif defined(__NetBSD__)
39 __RCSID("$NetBSD$");
40 #endif
43 struct parm *parms;
44 struct intnet *intnets;
45 struct r1net *r1nets;
46 struct tgate *tgates;
49 /* use configured parameters
51 void
52 get_parms(struct interface *ifp)
54 static int warned_auth_in, warned_auth_out;
55 struct parm *parmp;
56 int i, num_passwds = 0;
58 /* get all relevant parameters
60 for (parmp = parms; parmp != NULL; parmp = parmp->parm_next) {
61 if (parmp->parm_name[0] == '\0'
62 || !strcmp(ifp->int_name, parmp->parm_name)
63 || (parmp->parm_name[0] == '\n'
64 && on_net(ifp->int_addr,
65 parmp->parm_net, parmp->parm_mask))) {
67 /* This group of parameters is relevant,
68 * so get its settings
70 ifp->int_state |= parmp->parm_int_state;
71 for (i = 0; i < MAX_AUTH_KEYS; i++) {
72 if (parmp->parm_auth[0].type == RIP_AUTH_NONE
73 || num_passwds >= MAX_AUTH_KEYS)
74 break;
75 memcpy(&ifp->int_auth[num_passwds++],
76 &parmp->parm_auth[i],
77 sizeof(ifp->int_auth[0]));
79 if (parmp->parm_rdisc_pref != 0)
80 ifp->int_rdisc_pref = parmp->parm_rdisc_pref;
81 if (parmp->parm_rdisc_int != 0)
82 ifp->int_rdisc_int = parmp->parm_rdisc_int;
83 if (parmp->parm_d_metric != 0)
84 ifp->int_d_metric = parmp->parm_d_metric;
88 /* Set general defaults.
90 * Default poor-man's router discovery to a metric that will
91 * be heard by old versions of `routed`. They ignored received
92 * routes with metric 15.
94 if ((ifp->int_state & IS_PM_RDISC)
95 && ifp->int_d_metric == 0)
96 ifp->int_d_metric = FAKE_METRIC;
98 if (ifp->int_rdisc_int == 0)
99 ifp->int_rdisc_int = DefMaxAdvertiseInterval;
101 if (!(ifp->int_if_flags & IFF_MULTICAST)
102 && !(ifp->int_state & IS_REMOTE))
103 ifp->int_state |= IS_BCAST_RDISC;
105 if (ifp->int_if_flags & IFF_POINTOPOINT) {
106 ifp->int_state |= IS_BCAST_RDISC;
107 /* By default, point-to-point links should be passive
108 * about router-discovery for the sake of demand-dialing.
110 if (0 == (ifp->int_state & GROUP_IS_SOL_OUT))
111 ifp->int_state |= IS_NO_SOL_OUT;
112 if (0 == (ifp->int_state & GROUP_IS_ADV_OUT))
113 ifp->int_state |= IS_NO_ADV_OUT;
116 if (0 != (ifp->int_state & (IS_PASSIVE | IS_REMOTE)))
117 ifp->int_state |= IS_NO_RDISC;
118 if (ifp->int_state & IS_PASSIVE)
119 ifp->int_state |= IS_NO_RIP;
121 if (!IS_RIP_IN_OFF(ifp->int_state)
122 && ifp->int_auth[0].type != RIP_AUTH_NONE
123 && !(ifp->int_state & IS_NO_RIPV1_IN)
124 && !warned_auth_in) {
125 msglog("Warning: RIPv1 input via %s"
126 " will be accepted without authentication",
127 ifp->int_name);
128 warned_auth_in = 1;
130 if (!IS_RIP_OUT_OFF(ifp->int_state)
131 && ifp->int_auth[0].type != RIP_AUTH_NONE
132 && !(ifp->int_state & IS_NO_RIPV1_OUT)) {
133 if (!warned_auth_out) {
134 msglog("Warning: RIPv1 output via %s"
135 " will be sent without authentication",
136 ifp->int_name);
137 warned_auth_out = 1;
143 /* Read a list of gateways from /etc/gateways and add them to our tables.
145 * This file contains a list of "remote" gateways. That is usually
146 * a gateway which we cannot immediately determine if it is present or
147 * not as we can do for those provided by directly connected hardware.
149 * If a gateway is marked "passive" in the file, then we assume it
150 * does not understand RIP and assume it is always present. Those
151 * not marked passive are treated as if they were directly connected
152 * and assumed to be broken if they do not send us advertisements.
153 * All remote interfaces are added to our list, and those not marked
154 * passive are sent routing updates.
156 * A passive interface can also be local, hardware interface exempt
157 * from RIP.
159 void
160 gwkludge(void)
162 FILE *fp;
163 char *p, *lptr;
164 const char *cp;
165 char lbuf[200], net_host[5], dname[64+1+64+1];
166 char gname[GNAME_LEN+1], qual[9];
167 struct interface *ifp;
168 naddr dst, netmask, gate;
169 int metric, n, lnum;
170 struct stat sb;
171 u_int state;
172 const char *type;
175 fp = fopen(_PATH_GATEWAYS, "r");
176 if (fp == NULL)
177 return;
179 if (0 > fstat(fileno(fp), &sb)) {
180 msglog("could not stat() "_PATH_GATEWAYS);
181 fclose(fp);
182 return;
185 for (lnum = 1; ; lnum++) {
186 if (0 == fgets(lbuf, sizeof(lbuf), fp))
187 break;
188 lptr = lbuf;
189 while (*lptr == ' ')
190 lptr++;
191 p = lptr+strlen(lptr)-1;
192 while (*p == '\n'
193 || (*p == ' ' && (p == lptr+1 || *(p-1) != '\\')))
194 *p-- = '\0';
195 if (*lptr == '\0' /* ignore null and comment lines */
196 || *lptr == '#')
197 continue;
199 /* notice newfangled parameter lines
201 if (strncasecmp("net", lptr, 3)
202 && strncasecmp("host", lptr, 4)) {
203 cp = parse_parms(lptr,
204 (sb.st_uid == 0
205 && !(sb.st_mode&(S_IRWXG|S_IRWXO))));
206 if (cp != NULL)
207 msglog("%s in line %d of "_PATH_GATEWAYS,
208 cp, lnum);
209 continue;
212 /* {net | host} XX[/M] XX gateway XX metric DD [passive | external]\n */
213 qual[0] = '\0';
214 /* the '64' here must be GNAME_LEN */
215 n = sscanf(lptr, "%4s %129[^ \t] gateway"
216 " %64[^ / \t] metric %u %8s\n",
217 net_host, dname, gname, &metric, qual);
218 if (n != 4 && n != 5) {
219 msglog("bad "_PATH_GATEWAYS" entry \"%s\"; %d values",
220 lptr, n);
221 continue;
223 if (metric >= HOPCNT_INFINITY) {
224 msglog("bad metric in "_PATH_GATEWAYS" entry \"%s\"",
225 lptr);
226 continue;
228 if (!strcasecmp(net_host, "host")) {
229 if (!gethost(dname, &dst)) {
230 msglog("bad host \"%s\" in "_PATH_GATEWAYS
231 " entry \"%s\"", dname, lptr);
232 continue;
234 netmask = HOST_MASK;
235 } else if (!strcasecmp(net_host, "net")) {
236 if (!getnet(dname, &dst, &netmask)) {
237 msglog("bad net \"%s\" in "_PATH_GATEWAYS
238 " entry \"%s\"", dname, lptr);
239 continue;
241 if (dst == RIP_DEFAULT) {
242 msglog("bad net \"%s\" in "_PATH_GATEWAYS
243 " entry \"%s\"--cannot be default",
244 dname, lptr);
245 continue;
247 dst = htonl(dst); /* make network # into IP address */
248 } else {
249 msglog("bad \"%s\" in "_PATH_GATEWAYS
250 " entry \"%s\"", net_host, lptr);
251 continue;
254 if (!gethost(gname, &gate)) {
255 msglog("bad gateway \"%s\" in "_PATH_GATEWAYS
256 " entry \"%s\"", gname, lptr);
257 continue;
260 if (!strcasecmp(qual, type = "passive")) {
261 /* Passive entries are not placed in our tables,
262 * only the kernel's, so we don't copy all of the
263 * external routing information within a net.
264 * Internal machines should use the default
265 * route to a suitable gateway (like us).
267 state = IS_REMOTE | IS_PASSIVE;
268 if (metric == 0)
269 metric = 1;
271 } else if (!strcasecmp(qual, type = "external")) {
272 /* External entries are handled by other means
273 * such as EGP, and are placed only in the daemon
274 * tables to prevent overriding them with something
275 * else.
277 strcpy(qual,"external");
278 state = IS_REMOTE | IS_PASSIVE | IS_EXTERNAL;
279 if (metric == 0)
280 metric = 1;
282 } else if (!strcasecmp(qual, "active")
283 || qual[0] == '\0') {
284 if (metric != 0) {
285 /* Entries that are neither "passive" nor
286 * "external" are "remote" and must behave
287 * like physical interfaces. If they are not
288 * heard from regularly, they are deleted.
290 state = IS_REMOTE;
291 type = "remote";
292 } else {
293 /* "remote" entries with a metric of 0
294 * are aliases for our own interfaces
296 state = IS_REMOTE | IS_PASSIVE | IS_ALIAS;
297 type = "alias";
300 } else {
301 msglog("bad "_PATH_GATEWAYS" entry \"%s\";"
302 " unknown type %s", lptr, qual);
303 continue;
306 if (0 != (state & (IS_PASSIVE | IS_REMOTE)))
307 state |= IS_NO_RDISC;
308 if (state & IS_PASSIVE)
309 state |= IS_NO_RIP;
311 ifp = check_dup(gate,dst,netmask,0);
312 if (ifp != NULL) {
313 msglog("duplicate "_PATH_GATEWAYS" entry \"%s\"",lptr);
314 continue;
317 ifp = (struct interface *)rtmalloc(sizeof(*ifp), "gwkludge()");
318 memset(ifp, 0, sizeof(*ifp));
320 ifp->int_state = state;
321 if (netmask == HOST_MASK)
322 ifp->int_if_flags = IFF_POINTOPOINT | IFF_UP;
323 else
324 ifp->int_if_flags = IFF_UP;
325 ifp->int_act_time = NEVER;
326 ifp->int_addr = gate;
327 ifp->int_dstaddr = dst;
328 ifp->int_mask = netmask;
329 ifp->int_ripv1_mask = netmask;
330 ifp->int_std_mask = std_mask(gate);
331 ifp->int_net = ntohl(dst);
332 ifp->int_std_net = ifp->int_net & ifp->int_std_mask;
333 ifp->int_std_addr = htonl(ifp->int_std_net);
334 ifp->int_metric = metric;
335 if (!(state & IS_EXTERNAL)
336 && ifp->int_mask != ifp->int_std_mask)
337 ifp->int_state |= IS_SUBNET;
338 sprintf(ifp->int_name, "%s(%s)", type, gname);
339 ifp->int_index = -1;
341 if_link(ifp);
344 /* After all of the parameter lines have been read,
345 * apply them to any remote interfaces.
347 for (ifp = ifnet; NULL != ifp; ifp = ifp->int_next) {
348 get_parms(ifp);
350 tot_interfaces++;
351 if (!IS_RIP_OFF(ifp->int_state))
352 rip_interfaces++;
354 trace_if("Add", ifp);
357 fclose(fp);
361 /* like strtok(), but honoring backslash and not changing the source string
363 static int /* 0=ok, -1=bad */
364 parse_quote(char **linep, /* look here */
365 const char *delims, /* for these delimiters */
366 char *delimp, /* 0 or put found delimiter here */
367 char *buf, /* copy token to here */
368 int lim) /* at most this many bytes */
370 char c = '\0', *pc;
371 const char *p;
374 pc = *linep;
375 if (*pc == '\0')
376 return -1;
378 while (lim != 0) {
379 c = *pc++;
380 if (c == '\0')
381 break;
383 if (c == '\\' && *pc != '\0') {
384 if ((c = *pc++) == 'n') {
385 c = '\n';
386 } else if (c == 'r') {
387 c = '\r';
388 } else if (c == 't') {
389 c = '\t';
390 } else if (c == 'b') {
391 c = '\b';
392 } else if (c >= '0' && c <= '7') {
393 c -= '0';
394 if (*pc >= '0' && *pc <= '7') {
395 c = (c<<3)+(*pc++ - '0');
396 if (*pc >= '0' && *pc <= '7')
397 c = (c<<3)+(*pc++ - '0');
401 } else {
402 for (p = delims; *p != '\0'; ++p) {
403 if (*p == c)
404 goto exit;
408 *buf++ = c;
409 --lim;
411 exit:
412 if (lim == 0)
413 return -1;
415 *buf = '\0'; /* terminate copy of token */
416 if (delimp != NULL)
417 *delimp = c; /* return delimiter */
418 *linep = pc-1; /* say where we ended */
419 return 0;
423 /* Parse password timestamp
425 static char *
426 parse_ts(time_t *tp,
427 char **valp,
428 char *val0,
429 char *delimp,
430 char *buf,
431 u_int bufsize)
433 struct tm tm;
434 #if defined(sgi) || defined(__NetBSD__)
435 char *ptr;
436 #endif
438 if (0 > parse_quote(valp, "| ,\n\r", delimp,
439 buf,bufsize)
440 || buf[bufsize-1] != '\0'
441 || buf[bufsize-2] != '\0') {
442 sprintf(buf,"bad timestamp %.25s", val0);
443 return buf;
445 strcat(buf,"\n");
446 memset(&tm, 0, sizeof(tm));
447 #if defined(sgi) || defined(__NetBSD__)
448 ptr = strptime(buf, "%y/%m/%d@%H:%M\n", &tm);
449 if (ptr == NULL || *ptr != '\0') {
450 sprintf(buf,"bad timestamp %.25s", val0);
451 return buf;
453 #else
454 if (5 != sscanf(buf, "%u/%u/%u@%u:%u\n",
455 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
456 &tm.tm_hour, &tm.tm_min)
457 || tm.tm_mon < 1 || tm.tm_mon > 12
458 || tm.tm_mday < 1 || tm.tm_mday > 31) {
459 sprintf(buf,"bad timestamp %.25s", val0);
460 return buf;
462 tm.tm_mon--;
463 if (tm.tm_year <= 37) /* assume small years are in the */
464 tm.tm_year += 100; /* 3rd millenium */
465 #endif
467 if ((*tp = mktime(&tm)) == -1) {
468 sprintf(buf,"bad timestamp %.25s", val0);
469 return buf;
472 return 0;
476 /* Get a password, key ID, and expiration date in the format
477 * passwd|keyID|year/mon/day@hour:min|year/mon/day@hour:min
479 static const char * /* 0 or error message */
480 get_passwd(char *tgt,
481 char *val,
482 struct parm *parmp,
483 u_int16_t type,
484 int safe) /* 1=from secure file */
486 static char buf[80];
487 char *val0, *p, delim;
488 struct auth k, *ap, *ap2;
489 int i;
490 u_long l;
493 if (!safe)
494 return "ignore unsafe password";
496 for (ap = parmp->parm_auth, i = 0;
497 ap->type != RIP_AUTH_NONE; i++, ap++) {
498 if (i >= MAX_AUTH_KEYS)
499 return "too many passwords";
502 memset(&k, 0, sizeof(k));
503 k.type = type;
504 k.end = -1-DAY;
506 val0 = val;
507 if (0 > parse_quote(&val, "| ,\n\r", &delim,
508 (char *)k.key, sizeof(k.key)))
509 return tgt;
511 if (delim != '|') {
512 if (type == RIP_AUTH_MD5)
513 return "missing Keyid";
514 } else {
515 val0 = ++val;
516 buf[sizeof(buf)-1] = '\0';
517 if (0 > parse_quote(&val, "| ,\n\r", &delim, buf,sizeof(buf))
518 || buf[sizeof(buf)-1] != '\0'
519 || (l = strtoul(buf,&p,0)) > 255
520 || *p != '\0') {
521 sprintf(buf,"bad KeyID \"%.20s\"", val0);
522 return buf;
524 for (ap2 = parmp->parm_auth; ap2 < ap; ap2++) {
525 if (ap2->keyid == l) {
526 sprintf(buf,"duplicate KeyID \"%.20s\"", val0);
527 return buf;
530 k.keyid = (int)l;
532 if (delim == '|') {
533 val0 = ++val;
534 if (NULL != (p = parse_ts(&k.start,&val,val0,&delim,
535 buf,sizeof(buf))))
536 return p;
537 if (delim != '|')
538 return "missing second timestamp";
539 val0 = ++val;
540 if (NULL != (p = parse_ts(&k.end,&val,val0,&delim,
541 buf,sizeof(buf))))
542 return p;
543 if ((u_long)k.start > (u_long)k.end) {
544 sprintf(buf,"out of order timestamp %.30s",
545 val0);
546 return buf;
550 if (delim != '\0')
551 return tgt;
553 memmove(ap, &k, sizeof(*ap));
554 return 0;
558 static const char *
559 bad_str(const char *estr)
561 static char buf[100+8];
563 sprintf(buf, "bad \"%.100s\"", estr);
564 return buf;
568 /* Parse a set of parameters for an interface.
570 const char * /* 0 or error message */
571 parse_parms(char *line,
572 int safe) /* 1=from secure file */
574 #define PARS(str) (!strcasecmp(tgt, str))
575 #define PARSEQ(str) (!strncasecmp(tgt, str"=", sizeof(str)))
576 #define CKF(g,b) {if (0 != (parm.parm_int_state & ((g) & ~(b)))) break; \
577 parm.parm_int_state |= (b);}
578 struct parm parm;
579 struct intnet *intnetp;
580 struct r1net *r1netp;
581 struct tgate *tg;
582 naddr addr, mask;
583 char delim, *val0 = NULL, *tgt, *val, *p;
584 const char *msg;
585 char buf[BUFSIZ], buf2[BUFSIZ];
586 int i;
589 /* "subnet=x.y.z.u/mask[,metric]" must be alone on the line */
590 if (!strncasecmp(line, "subnet=", sizeof("subnet=")-1)
591 && *(val = &line[sizeof("subnet=")-1]) != '\0') {
592 if (0 > parse_quote(&val, ",", &delim, buf, sizeof(buf)))
593 return bad_str(line);
594 intnetp = (struct intnet*)rtmalloc(sizeof(*intnetp),
595 "parse_parms subnet");
596 intnetp->intnet_metric = 1;
597 if (delim == ',') {
598 intnetp->intnet_metric = (int)strtol(val+1,&p,0);
599 if (*p != '\0'
600 || intnetp->intnet_metric <= 0
601 || intnetp->intnet_metric >= HOPCNT_INFINITY)
602 return bad_str(line);
604 if (!getnet(buf, &intnetp->intnet_addr, &intnetp->intnet_mask)
605 || intnetp->intnet_mask == HOST_MASK
606 || intnetp->intnet_addr == RIP_DEFAULT) {
607 free(intnetp);
608 return bad_str(line);
610 intnetp->intnet_addr = htonl(intnetp->intnet_addr);
611 intnetp->intnet_next = intnets;
612 intnets = intnetp;
613 return 0;
616 /* "ripv1_mask=x.y.z.u/mask1,mask2" must be alone on the line.
617 * This requires that x.y.z.u/mask1 be considered a subnet of
618 * x.y.z.u/mask2, as if x.y.z.u/mask2 were a class-full network.
620 if (!strncasecmp(line, "ripv1_mask=", sizeof("ripv1_mask=")-1)
621 && *(val = &line[sizeof("ripv1_mask=")-1]) != '\0') {
622 if (0 > parse_quote(&val, ",", &delim, buf, sizeof(buf))
623 || delim == '\0')
624 return bad_str(line);
625 if ((i = (int)strtol(val+1, &p, 0)) <= 0
626 || i > 32 || *p != '\0')
627 return bad_str(line);
628 r1netp = (struct r1net *)rtmalloc(sizeof(*r1netp),
629 "parse_parms ripv1_mask");
630 r1netp->r1net_mask = HOST_MASK << (32-i);
631 if (!getnet(buf, &r1netp->r1net_net, &r1netp->r1net_match)
632 || r1netp->r1net_net == RIP_DEFAULT
633 || r1netp->r1net_mask > r1netp->r1net_match) {
634 free(r1netp);
635 return bad_str(line);
637 r1netp->r1net_next = r1nets;
638 r1nets = r1netp;
639 return 0;
642 memset(&parm, 0, sizeof(parm));
644 for (;;) {
645 tgt = line + strspn(line, " ,\n\r");
646 if (*tgt == '\0' || *tgt == '#')
647 break;
648 line = tgt+strcspn(tgt, "= #,\n\r");
649 delim = *line;
650 if (delim == '=') {
651 val0 = ++line;
652 if (0 > parse_quote(&line, " #,\n\r",&delim,
653 buf,sizeof(buf)))
654 return bad_str(tgt);
656 if (delim != '\0') {
657 for (;;) {
658 *line = '\0';
659 if (delim == '#')
660 break;
661 ++line;
662 if (delim != ' '
663 || (delim = *line) != ' ')
664 break;
668 if (PARSEQ("if")) {
669 if (parm.parm_name[0] != '\0'
670 || strlen(buf) > IF_NAME_LEN)
671 return bad_str(tgt);
672 strcpy(parm.parm_name, buf);
674 } else if (PARSEQ("addr")) {
675 /* This is a bad idea, because the address based
676 * sets of parameters cannot be checked for
677 * consistency with the interface name parameters.
678 * The parm_net stuff is needed to allow several
679 * -F settings.
681 if (!getnet(val0, &addr, &mask)
682 || parm.parm_name[0] != '\0')
683 return bad_str(tgt);
684 parm.parm_net = addr;
685 parm.parm_mask = mask;
686 parm.parm_name[0] = '\n';
688 } else if (PARSEQ("passwd")) {
689 /* since cleartext passwords are so weak allow
690 * them anywhere
692 msg = get_passwd(tgt,val0,&parm,RIP_AUTH_PW,1);
693 if (msg) {
694 *val0 = '\0';
695 return bad_str(msg);
698 } else if (PARSEQ("md5_passwd")) {
699 msg = get_passwd(tgt,val0,&parm,RIP_AUTH_MD5,safe);
700 if (msg) {
701 *val0 = '\0';
702 return bad_str(msg);
705 } else if (PARS("no_ag")) {
706 parm.parm_int_state |= (IS_NO_AG | IS_NO_SUPER_AG);
708 } else if (PARS("no_super_ag")) {
709 parm.parm_int_state |= IS_NO_SUPER_AG;
711 } else if (PARS("no_ripv1_in")) {
712 parm.parm_int_state |= IS_NO_RIPV1_IN;
714 } else if (PARS("no_ripv2_in")) {
715 parm.parm_int_state |= IS_NO_RIPV2_IN;
717 } else if (PARS("ripv2_out")) {
718 if (parm.parm_int_state & IS_NO_RIPV2_OUT)
719 return bad_str(tgt);
720 parm.parm_int_state |= IS_NO_RIPV1_OUT;
722 } else if (PARS("ripv2")) {
723 if ((parm.parm_int_state & IS_NO_RIPV2_OUT)
724 || (parm.parm_int_state & IS_NO_RIPV2_IN))
725 return bad_str(tgt);
726 parm.parm_int_state |= (IS_NO_RIPV1_IN
727 | IS_NO_RIPV1_OUT);
729 } else if (PARS("no_rip")) {
730 CKF(IS_PM_RDISC, IS_NO_RIP);
732 } else if (PARS("no_rip_mcast")) {
733 parm.parm_int_state |= IS_NO_RIP_MCAST;
735 } else if (PARS("no_rdisc")) {
736 CKF((GROUP_IS_SOL_OUT|GROUP_IS_ADV_OUT), IS_NO_RDISC);
738 } else if (PARS("no_solicit")) {
739 CKF(GROUP_IS_SOL_OUT, IS_NO_SOL_OUT);
741 } else if (PARS("send_solicit")) {
742 CKF(GROUP_IS_SOL_OUT, IS_SOL_OUT);
744 } else if (PARS("no_rdisc_adv")) {
745 CKF(GROUP_IS_ADV_OUT, IS_NO_ADV_OUT);
747 } else if (PARS("rdisc_adv")) {
748 CKF(GROUP_IS_ADV_OUT, IS_ADV_OUT);
750 } else if (PARS("bcast_rdisc")) {
751 parm.parm_int_state |= IS_BCAST_RDISC;
753 } else if (PARS("passive")) {
754 CKF((GROUP_IS_SOL_OUT|GROUP_IS_ADV_OUT), IS_NO_RDISC);
755 parm.parm_int_state |= IS_NO_RIP| IS_PASSIVE;
757 } else if (PARSEQ("rdisc_pref")) {
758 if (parm.parm_rdisc_pref != 0
759 || (parm.parm_rdisc_pref = (int)strtol(buf,&p,0),
760 *p != '\0'))
761 return bad_str(tgt);
763 } else if (PARS("pm_rdisc")) {
764 if (IS_RIP_OUT_OFF(parm.parm_int_state))
765 return bad_str(tgt);
766 parm.parm_int_state |= IS_PM_RDISC;
768 } else if (PARSEQ("rdisc_interval")) {
769 if (parm.parm_rdisc_int != 0
770 || (parm.parm_rdisc_int = (int)strtoul(buf,&p,0),
771 *p != '\0')
772 || parm.parm_rdisc_int < MinMaxAdvertiseInterval
773 || parm.parm_rdisc_int > MaxMaxAdvertiseInterval)
774 return bad_str(tgt);
776 } else if (PARSEQ("fake_default")) {
777 if (parm.parm_d_metric != 0
778 || IS_RIP_OUT_OFF(parm.parm_int_state)
779 || (parm.parm_d_metric = (int)strtoul(buf,&p,0),
780 *p != '\0')
781 || parm.parm_d_metric > HOPCNT_INFINITY-1)
782 return bad_str(tgt);
784 } else if (PARSEQ("trust_gateway")) {
785 /* look for trust_gateway=x.y.z|net/mask|...) */
786 p = buf;
787 if (0 > parse_quote(&p, "|", &delim,
788 buf2, sizeof(buf2))
789 || !gethost(buf2,&addr))
790 return bad_str(tgt);
791 tg = (struct tgate *)rtmalloc(sizeof(*tg),
792 "parse_parms"
793 "trust_gateway");
794 memset(tg, 0, sizeof(*tg));
795 tg->tgate_addr = addr;
796 i = 0;
797 /* The default is to trust all routes. */
798 while (delim == '|') {
799 p++;
800 if (i >= MAX_TGATE_NETS
801 || 0 > parse_quote(&p, "|", &delim,
802 buf2, sizeof(buf2))
803 || !getnet(buf2, &tg->tgate_nets[i].net,
804 &tg->tgate_nets[i].mask)
805 || tg->tgate_nets[i].net == RIP_DEFAULT
806 || tg->tgate_nets[i].mask == 0)
807 return bad_str(tgt);
808 i++;
810 tg->tgate_next = tgates;
811 tgates = tg;
812 parm.parm_int_state |= IS_DISTRUST;
814 } else if (PARS("redirect_ok")) {
815 parm.parm_int_state |= IS_REDIRECT_OK;
817 } else {
818 return bad_str(tgt); /* error */
822 return check_parms(&parm);
823 #undef PARS
824 #undef PARSEQ
828 /* check for duplicate parameter specifications */
829 const char * /* 0 or error message */
830 check_parms(struct parm *new)
832 struct parm *parmp, **parmpp;
833 int i, num_passwds;
835 /* set implicit values
837 if (new->parm_int_state & IS_NO_ADV_IN)
838 new->parm_int_state |= IS_NO_SOL_OUT;
839 if (new->parm_int_state & IS_NO_SOL_OUT)
840 new->parm_int_state |= IS_NO_ADV_IN;
842 for (i = num_passwds = 0; i < MAX_AUTH_KEYS; i++) {
843 if (new->parm_auth[i].type != RIP_AUTH_NONE)
844 num_passwds++;
847 /* compare with existing sets of parameters
849 for (parmpp = &parms;
850 (parmp = *parmpp) != NULL;
851 parmpp = &parmp->parm_next) {
852 if (strcmp(new->parm_name, parmp->parm_name))
853 continue;
854 if (!on_net(htonl(parmp->parm_net),
855 new->parm_net, new->parm_mask)
856 && !on_net(htonl(new->parm_net),
857 parmp->parm_net, parmp->parm_mask))
858 continue;
860 for (i = 0; i < MAX_AUTH_KEYS; i++) {
861 if (parmp->parm_auth[i].type != RIP_AUTH_NONE)
862 num_passwds++;
864 if (num_passwds > MAX_AUTH_KEYS)
865 return "too many conflicting passwords";
867 if ((0 != (new->parm_int_state & GROUP_IS_SOL_OUT)
868 && 0 != (parmp->parm_int_state & GROUP_IS_SOL_OUT)
869 && 0 != ((new->parm_int_state ^ parmp->parm_int_state)
870 & GROUP_IS_SOL_OUT))
871 || (0 != (new->parm_int_state & GROUP_IS_ADV_OUT)
872 && 0 != (parmp->parm_int_state & GROUP_IS_ADV_OUT)
873 && 0 != ((new->parm_int_state ^ parmp->parm_int_state)
874 & GROUP_IS_ADV_OUT))
875 || (new->parm_rdisc_pref != 0
876 && parmp->parm_rdisc_pref != 0
877 && new->parm_rdisc_pref != parmp->parm_rdisc_pref)
878 || (new->parm_rdisc_int != 0
879 && parmp->parm_rdisc_int != 0
880 && new->parm_rdisc_int != parmp->parm_rdisc_int)) {
881 return ("conflicting, duplicate router discovery"
882 " parameters");
886 if (new->parm_d_metric != 0
887 && parmp->parm_d_metric != 0
888 && new->parm_d_metric != parmp->parm_d_metric) {
889 return ("conflicting, duplicate poor man's router"
890 " discovery or fake default metric");
894 /* link new entry on the so that when the entries are scanned,
895 * they affect the result in the order the operator specified.
897 parmp = (struct parm*)rtmalloc(sizeof(*parmp), "check_parms");
898 memcpy(parmp, new, sizeof(*parmp));
899 *parmpp = parmp;
901 return 0;
905 /* get a network number as a name or a number, with an optional "/xx"
906 * netmask.
908 int /* 0=bad */
909 getnet(char *name,
910 naddr *netp, /* network in host byte order */
911 naddr *maskp) /* masks are always in host order */
913 int i;
914 struct netent *np;
915 naddr mask; /* in host byte order */
916 struct in_addr in; /* a network and so host byte order */
917 char hname[MAXHOSTNAMELEN+1];
918 char *mname, *p;
921 /* Detect and separate "1.2.3.4/24"
923 if (NULL != (mname = strrchr(name,'/'))) {
924 i = (int)(mname - name);
925 if (i > (int)sizeof(hname)-1) /* name too long */
926 return 0;
927 memmove(hname, name, i);
928 hname[i] = '\0';
929 mname++;
930 name = hname;
933 np = getnetbyname(name);
934 if (np != NULL) {
935 in.s_addr = (naddr)np->n_net;
936 if (0 == (in.s_addr & 0xff000000))
937 in.s_addr <<= 8;
938 if (0 == (in.s_addr & 0xff000000))
939 in.s_addr <<= 8;
940 if (0 == (in.s_addr & 0xff000000))
941 in.s_addr <<= 8;
942 } else if (inet_aton(name, &in) == 1) {
943 in.s_addr = ntohl(in.s_addr);
944 } else if (!mname && !strcasecmp(name,"default")) {
945 in.s_addr = RIP_DEFAULT;
946 } else {
947 return 0;
950 if (!mname) {
951 /* we cannot use the interfaces here because we have not
952 * looked at them yet.
954 mask = std_mask(htonl(in.s_addr));
955 if ((~mask & in.s_addr) != 0)
956 mask = HOST_MASK;
957 } else {
958 mask = (naddr)strtoul(mname, &p, 0);
959 if (*p != '\0' || mask > 32)
960 return 0;
961 if (mask != 0)
962 mask = HOST_MASK << (32-mask);
965 /* must have mask of 0 with default */
966 if (mask != 0 && in.s_addr == RIP_DEFAULT)
967 return 0;
968 /* no host bits allowed in a network number */
969 if ((~mask & in.s_addr) != 0)
970 return 0;
971 /* require non-zero network number */
972 if ((mask & in.s_addr) == 0 && in.s_addr != RIP_DEFAULT)
973 return 0;
974 if (in.s_addr>>24 == 0 && in.s_addr != RIP_DEFAULT)
975 return 0;
976 if (in.s_addr>>24 == 0xff)
977 return 0;
979 *netp = in.s_addr;
980 *maskp = mask;
981 return 1;
985 int /* 0=bad */
986 gethost(char *name,
987 naddr *addrp)
989 struct hostent *hp;
990 struct in_addr in;
993 /* Try for a number first, even in IRIX where gethostbyname()
994 * is smart. This avoids hitting the name server which
995 * might be sick because routing is.
997 if (inet_aton(name, &in) == 1) {
998 /* get a good number, but check that it it makes some
999 * sense.
1001 if (ntohl(in.s_addr)>>24 == 0
1002 || ntohl(in.s_addr)>>24 == 0xff)
1003 return 0;
1004 *addrp = in.s_addr;
1005 return 1;
1008 hp = gethostbyname(name);
1009 if (hp) {
1010 memcpy(addrp, hp->h_addr, sizeof(*addrp));
1011 return 1;
1014 return 0;