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
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 * 4. 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
33 #include "pathnames.h"
38 #elif defined(__FreeBSD__)
41 __RCSID("$Revision: 2.26 $");
42 #ident "$Revision: 2.26 $"
47 struct intnet *intnets;
52 /* use configured parameters
55 get_parms(struct interface *ifp)
57 static int warned_auth_in, warned_auth_out;
59 int i, num_passwds = 0;
61 /* get all relevant parameters
63 for (parmp = parms; parmp != 0; parmp = parmp->parm_next) {
64 if (parmp->parm_name[0] == '\0'
65 || !strcmp(ifp->int_name, parmp->parm_name)
66 || (parmp->parm_name[0] == '\n'
67 && on_net(ifp->int_addr,
68 parmp->parm_net, parmp->parm_mask))) {
70 /* This group of parameters is relevant,
73 ifp->int_state |= parmp->parm_int_state;
74 for (i = 0; i < MAX_AUTH_KEYS; i++) {
75 if (parmp->parm_auth[0].type == RIP_AUTH_NONE
76 || num_passwds >= MAX_AUTH_KEYS)
78 memcpy(&ifp->int_auth[num_passwds++],
80 sizeof(ifp->int_auth[0]));
82 if (parmp->parm_rdisc_pref != 0)
83 ifp->int_rdisc_pref = parmp->parm_rdisc_pref;
84 if (parmp->parm_rdisc_int != 0)
85 ifp->int_rdisc_int = parmp->parm_rdisc_int;
86 if (parmp->parm_adj_inmetric != 0)
87 ifp->int_adj_inmetric = parmp->parm_adj_inmetric;
88 if (parmp->parm_adj_outmetric != 0)
89 ifp->int_adj_outmetric = parmp->parm_adj_outmetric;
93 /* Set general defaults.
95 * Default poor-man's router discovery to a metric that will
96 * be heard by old versions of `routed`. They ignored received
97 * routes with metric 15.
99 if ((ifp->int_state & IS_PM_RDISC)
100 && ifp->int_d_metric == 0)
101 ifp->int_d_metric = FAKE_METRIC;
103 if (ifp->int_rdisc_int == 0)
104 ifp->int_rdisc_int = DefMaxAdvertiseInterval;
106 if (!(ifp->int_if_flags & IFF_MULTICAST)
107 && !(ifp->int_state & IS_REMOTE))
108 ifp->int_state |= IS_BCAST_RDISC;
110 if (ifp->int_if_flags & IFF_POINTOPOINT) {
111 ifp->int_state |= IS_BCAST_RDISC;
112 /* By default, point-to-point links should be passive
113 * about router-discovery for the sake of demand-dialing.
115 if (0 == (ifp->int_state & GROUP_IS_SOL_OUT))
116 ifp->int_state |= IS_NO_SOL_OUT;
117 if (0 == (ifp->int_state & GROUP_IS_ADV_OUT))
118 ifp->int_state |= IS_NO_ADV_OUT;
121 if (0 != (ifp->int_state & (IS_PASSIVE | IS_REMOTE)))
122 ifp->int_state |= IS_NO_RDISC;
123 if (ifp->int_state & IS_PASSIVE)
124 ifp->int_state |= IS_NO_RIP;
126 if (!IS_RIP_IN_OFF(ifp->int_state)
127 && ifp->int_auth[0].type != RIP_AUTH_NONE
128 && !(ifp->int_state & IS_NO_RIPV1_IN)
129 && !warned_auth_in) {
130 msglog("Warning: RIPv1 input via %s"
131 " will be accepted without authentication",
135 if (!IS_RIP_OUT_OFF(ifp->int_state)
136 && ifp->int_auth[0].type != RIP_AUTH_NONE
137 && !(ifp->int_state & IS_NO_RIPV1_OUT)) {
138 if (!warned_auth_out) {
139 msglog("Warning: RIPv1 output via %s"
140 " will be sent without authentication",
148 /* Read a list of gateways from /etc/gateways and add them to our tables.
150 * This file contains a list of "remote" gateways. That is usually
151 * a gateway which we cannot immediately determine if it is present or
152 * not as we can do for those provided by directly connected hardware.
154 * If a gateway is marked "passive" in the file, then we assume it
155 * does not understand RIP and assume it is always present. Those
156 * not marked passive are treated as if they were directly connected
157 * and assumed to be broken if they do not send us advertisements.
158 * All remote interfaces are added to our list, and those not marked
159 * passive are sent routing updates.
161 * A passive interface can also be local, hardware interface exempt
170 char lbuf[200], net_host[5], dname[64+1+64+1];
171 char gname[GNAME_LEN+1], qual[9];
172 struct interface *ifp;
173 naddr dst, netmask, gate;
180 fp = fopen(_PATH_GATEWAYS, "r");
184 if (0 > fstat(fileno(fp), &sb)) {
185 msglog("could not stat() "_PATH_GATEWAYS);
190 for (lnum = 1; ; lnum++) {
191 if (0 == fgets(lbuf, sizeof(lbuf), fp))
196 p = lptr+strlen(lptr)-1;
198 || (*p == ' ' && (p == lptr+1 || *(p-1) != '\\')))
200 if (*lptr == '\0' /* ignore null and comment lines */
204 /* notice newfangled parameter lines
206 if (strncasecmp("net", lptr, 3)
207 && strncasecmp("host", lptr, 4)) {
208 cp = parse_parms(lptr,
210 && !(sb.st_mode&(S_IRWXG|S_IRWXO))));
212 msglog("%s in line %d of "_PATH_GATEWAYS,
217 /* {net | host} XX[/M] XX gateway XX metric DD [passive | external]\n */
219 /* the '64' here must be GNAME_LEN */
220 n = sscanf(lptr, "%4s %129[^ \t] gateway"
221 " %64[^ / \t] metric %u %8s\n",
222 net_host, dname, gname, &metric, qual);
223 if (n != 4 && n != 5) {
224 msglog("bad "_PATH_GATEWAYS" entry \"%s\"; %d values",
228 if (metric >= HOPCNT_INFINITY) {
229 msglog("bad metric in "_PATH_GATEWAYS" entry \"%s\"",
233 if (!strcasecmp(net_host, "host")) {
234 if (!gethost(dname, &dst)) {
235 msglog("bad host \"%s\" in "_PATH_GATEWAYS
236 " entry \"%s\"", dname, lptr);
240 } else if (!strcasecmp(net_host, "net")) {
241 if (!getnet(dname, &dst, &netmask)) {
242 msglog("bad net \"%s\" in "_PATH_GATEWAYS
243 " entry \"%s\"", dname, lptr);
246 if (dst == RIP_DEFAULT) {
247 msglog("bad net \"%s\" in "_PATH_GATEWAYS
248 " entry \"%s\"--cannot be default",
252 /* Turn network # into IP address. */
255 msglog("bad \"%s\" in "_PATH_GATEWAYS
256 " entry \"%s\"", net_host, lptr);
260 if (!gethost(gname, &gate)) {
261 msglog("bad gateway \"%s\" in "_PATH_GATEWAYS
262 " entry \"%s\"", gname, lptr);
266 if (!strcasecmp(qual, type = "passive")) {
267 /* Passive entries are not placed in our tables,
268 * only the kernel's, so we don't copy all of the
269 * external routing information within a net.
270 * Internal machines should use the default
271 * route to a suitable gateway (like us).
273 state = IS_REMOTE | IS_PASSIVE;
277 } else if (!strcasecmp(qual, type = "external")) {
278 /* External entries are handled by other means
279 * such as EGP, and are placed only in the daemon
280 * tables to prevent overriding them with something
283 strcpy(qual,"external");
284 state = IS_REMOTE | IS_PASSIVE | IS_EXTERNAL;
288 } else if (!strcasecmp(qual, "active")
289 || qual[0] == '\0') {
291 /* Entries that are neither "passive" nor
292 * "external" are "remote" and must behave
293 * like physical interfaces. If they are not
294 * heard from regularly, they are deleted.
299 /* "remote" entries with a metric of 0
300 * are aliases for our own interfaces
302 state = IS_REMOTE | IS_PASSIVE | IS_ALIAS;
307 msglog("bad "_PATH_GATEWAYS" entry \"%s\";"
308 " unknown type %s", lptr, qual);
312 if (0 != (state & (IS_PASSIVE | IS_REMOTE)))
313 state |= IS_NO_RDISC;
314 if (state & IS_PASSIVE)
317 ifp = check_dup(gate,dst,netmask,state);
319 msglog("duplicate "_PATH_GATEWAYS" entry \"%s\"",lptr);
323 ifp = (struct interface *)rtmalloc(sizeof(*ifp), "gwkludge()");
324 memset(ifp, 0, sizeof(*ifp));
326 ifp->int_state = state;
327 if (netmask == HOST_MASK)
328 ifp->int_if_flags = IFF_POINTOPOINT | IFF_UP;
330 ifp->int_if_flags = IFF_UP;
331 ifp->int_act_time = NEVER;
332 ifp->int_addr = gate;
333 ifp->int_dstaddr = dst;
334 ifp->int_mask = netmask;
335 ifp->int_ripv1_mask = netmask;
336 ifp->int_std_mask = std_mask(gate);
337 ifp->int_net = ntohl(dst);
338 ifp->int_std_net = ifp->int_net & ifp->int_std_mask;
339 ifp->int_std_addr = htonl(ifp->int_std_net);
340 ifp->int_metric = metric;
341 if (!(state & IS_EXTERNAL)
342 && ifp->int_mask != ifp->int_std_mask)
343 ifp->int_state |= IS_SUBNET;
344 (void)sprintf(ifp->int_name, "%s(%s)", type, gname);
350 /* After all of the parameter lines have been read,
351 * apply them to any remote interfaces.
353 for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
357 if (!IS_RIP_OFF(ifp->int_state))
360 trace_if("Add", ifp);
367 /* like strtok(), but honoring backslash and not changing the source string
369 static int /* 0=ok, -1=bad */
370 parse_quote(char **linep, /* look here */
371 const char *delims, /* for these delimiters */
372 char *delimp, /* 0 or put found delimiter here */
373 char *buf, /* copy token to here */
374 int lim) /* at most this many bytes */
389 if (c == '\\' && *pc != '\0') {
390 if ((c = *pc++) == 'n') {
392 } else if (c == 'r') {
394 } else if (c == 't') {
396 } else if (c == 'b') {
398 } else if (c >= '0' && c <= '7') {
400 if (*pc >= '0' && *pc <= '7') {
401 c = (c<<3)+(*pc++ - '0');
402 if (*pc >= '0' && *pc <= '7')
403 c = (c<<3)+(*pc++ - '0');
408 for (p = delims; *p != '\0'; ++p) {
421 *buf = '\0'; /* terminate copy of token */
423 *delimp = c; /* return delimiter */
424 *linep = pc-1; /* say where we ended */
429 /* Parse password timestamp
440 #if defined(sgi) || defined(__NetBSD__)
444 if (0 > parse_quote(valp, "| ,\n\r", delimp,
446 || buf[bufsize-1] != '\0'
447 || buf[bufsize-2] != '\0') {
448 sprintf(buf,"bad timestamp %.25s", val0);
452 memset(&tm, 0, sizeof(tm));
453 #if defined(sgi) || defined(__NetBSD__)
454 ptr = strptime(buf, "%y/%m/%d@%H:%M\n", &tm);
455 if (ptr == NULL || *ptr != '\0') {
456 sprintf(buf,"bad timestamp %.25s", val0);
460 if (5 != sscanf(buf, "%u/%u/%u@%u:%u\n",
461 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
462 &tm.tm_hour, &tm.tm_min)
463 || tm.tm_mon < 1 || tm.tm_mon > 12
464 || tm.tm_mday < 1 || tm.tm_mday > 31) {
465 sprintf(buf,"bad timestamp %.25s", val0);
469 if (tm.tm_year <= 37) /* assume small years are in the */
470 tm.tm_year += 100; /* 3rd millenium */
473 if ((*tp = mktime(&tm)) == -1) {
474 sprintf(buf,"bad timestamp %.25s", val0);
482 /* Get a password, key ID, and expiration date in the format
483 * passwd|keyID|year/mon/day@hour:min|year/mon/day@hour:min
485 static const char * /* 0 or error message */
486 get_passwd(char *tgt,
490 int safe) /* 1=from secure file */
493 char *val0, *p, delim;
494 struct auth k, *ap, *ap2;
500 return "ignore unsafe password";
502 for (ap = parmp->parm_auth, i = 0;
503 ap->type != RIP_AUTH_NONE; i++, ap++) {
504 if (i >= MAX_AUTH_KEYS)
505 return "too many passwords";
508 memset(&k, 0, sizeof(k));
513 if (0 > parse_quote(&val, "| ,\n\r", &delim,
514 (char *)k.key, sizeof(k.key)))
518 if (type == RIP_AUTH_MD5)
519 return "missing Keyid";
522 buf[sizeof(buf)-1] = '\0';
523 if (0 > parse_quote(&val, "| ,\n\r", &delim, buf,sizeof(buf))
524 || buf[sizeof(buf)-1] != '\0'
525 || (l = strtoul(buf,&p,0)) > 255
527 sprintf(buf,"bad KeyID \"%.20s\"", val0);
530 for (ap2 = parmp->parm_auth; ap2 < ap; ap2++) {
531 if (ap2->keyid == l) {
532 sprintf(buf,"duplicate KeyID \"%.20s\"", val0);
540 if (0 != (p = parse_ts(&k.start,&val,val0,&delim,
544 return "missing second timestamp";
546 if (0 != (p = parse_ts(&k.end,&val,val0,&delim,
549 if ((u_long)k.start > (u_long)k.end) {
550 sprintf(buf,"out of order timestamp %.30s",
559 memmove(ap, &k, sizeof(*ap));
565 bad_str(const char *estr)
567 static char buf[100+8];
569 sprintf(buf, "bad \"%.100s\"", estr);
574 /* Parse a set of parameters for an interface.
576 const char * /* 0 or error message */
577 parse_parms(char *line,
578 int safe) /* 1=from secure file */
580 #define PARS(str) (!strcasecmp(tgt, str))
581 #define PARSEQ(str) (!strncasecmp(tgt, str"=", sizeof(str)))
582 #define CKF(g,b) {if (0 != (parm.parm_int_state & ((g) & ~(b)))) break; \
583 parm.parm_int_state |= (b);}
585 struct intnet *intnetp;
586 struct r1net *r1netp;
589 char delim, *val0 = 0, *tgt, *val, *p;
591 char buf[BUFSIZ], buf2[BUFSIZ];
595 /* "subnet=x.y.z.u/mask[,metric]" must be alone on the line */
596 if (!strncasecmp(line, "subnet=", sizeof("subnet=")-1)
597 && *(val = &line[sizeof("subnet=")-1]) != '\0') {
598 if (0 > parse_quote(&val, ",", &delim, buf, sizeof(buf)))
599 return bad_str(line);
600 intnetp = (struct intnet*)rtmalloc(sizeof(*intnetp),
601 "parse_parms subnet");
602 intnetp->intnet_metric = 1;
604 intnetp->intnet_metric = (int)strtol(val+1,&p,0);
606 || intnetp->intnet_metric <= 0
607 || intnetp->intnet_metric >= HOPCNT_INFINITY)
608 return bad_str(line);
610 if (!getnet(buf, &intnetp->intnet_addr, &intnetp->intnet_mask)
611 || intnetp->intnet_mask == HOST_MASK
612 || intnetp->intnet_addr == RIP_DEFAULT) {
614 return bad_str(line);
616 intnetp->intnet_addr = htonl(intnetp->intnet_addr);
617 intnetp->intnet_next = intnets;
622 /* "ripv1_mask=x.y.z.u/mask1,mask2" must be alone on the line.
623 * This requires that x.y.z.u/mask1 be considered a subnet of
624 * x.y.z.u/mask2, as if x.y.z.u/mask2 were a class-full network.
626 if (!strncasecmp(line, "ripv1_mask=", sizeof("ripv1_mask=")-1)
627 && *(val = &line[sizeof("ripv1_mask=")-1]) != '\0') {
628 if (0 > parse_quote(&val, ",", &delim, buf, sizeof(buf))
630 return bad_str(line);
631 if ((i = (int)strtol(val+1, &p, 0)) <= 0
632 || i > 32 || *p != '\0')
633 return bad_str(line);
634 r1netp = (struct r1net *)rtmalloc(sizeof(*r1netp),
635 "parse_parms ripv1_mask");
636 r1netp->r1net_mask = HOST_MASK << (32-i);
637 if (!getnet(buf, &r1netp->r1net_net, &r1netp->r1net_match)
638 || r1netp->r1net_net == RIP_DEFAULT
639 || r1netp->r1net_mask > r1netp->r1net_match) {
641 return bad_str(line);
643 r1netp->r1net_next = r1nets;
648 memset(&parm, 0, sizeof(parm));
651 tgt = line + strspn(line, " ,\n\r");
652 if (*tgt == '\0' || *tgt == '#')
654 line = tgt+strcspn(tgt, "= #,\n\r");
658 if (0 > parse_quote(&line, " #,\n\r",&delim,
669 || (delim = *line) != ' ')
675 if (parm.parm_name[0] != '\0'
676 || strlen(buf) > IF_NAME_LEN)
678 strcpy(parm.parm_name, buf);
680 } else if (PARSEQ("addr")) {
681 /* This is a bad idea, because the address based
682 * sets of parameters cannot be checked for
683 * consistency with the interface name parameters.
684 * The parm_net stuff is needed to allow several
687 if (!getnet(val0, &addr, &mask)
688 || parm.parm_name[0] != '\0')
690 parm.parm_net = addr;
691 parm.parm_mask = mask;
692 parm.parm_name[0] = '\n';
694 } else if (PARSEQ("passwd")) {
695 /* since cleartext passwords are so weak allow
698 msg = get_passwd(tgt,val0,&parm,RIP_AUTH_PW,1);
704 } else if (PARSEQ("md5_passwd")) {
705 msg = get_passwd(tgt,val0,&parm,RIP_AUTH_MD5,safe);
711 } else if (PARS("no_ag")) {
712 parm.parm_int_state |= (IS_NO_AG | IS_NO_SUPER_AG);
714 } else if (PARS("no_super_ag")) {
715 parm.parm_int_state |= IS_NO_SUPER_AG;
717 } else if (PARS("no_rip_out")) {
718 parm.parm_int_state |= IS_NO_RIP_OUT;
720 } else if (PARS("no_ripv1_in")) {
721 parm.parm_int_state |= IS_NO_RIPV1_IN;
723 } else if (PARS("no_ripv2_in")) {
724 parm.parm_int_state |= IS_NO_RIPV2_IN;
726 } else if (PARS("ripv2_out")) {
727 if (parm.parm_int_state & IS_NO_RIPV2_OUT)
729 parm.parm_int_state |= IS_NO_RIPV1_OUT;
731 } else if (PARS("ripv2")) {
732 if ((parm.parm_int_state & IS_NO_RIPV2_OUT)
733 || (parm.parm_int_state & IS_NO_RIPV2_IN))
735 parm.parm_int_state |= (IS_NO_RIPV1_IN
738 } else if (PARS("no_rip")) {
739 CKF(IS_PM_RDISC, IS_NO_RIP);
741 } else if (PARS("no_rip_mcast")) {
742 parm.parm_int_state |= IS_NO_RIP_MCAST;
744 } else if (PARS("no_rdisc")) {
745 CKF((GROUP_IS_SOL_OUT|GROUP_IS_ADV_OUT), IS_NO_RDISC);
747 } else if (PARS("no_solicit")) {
748 CKF(GROUP_IS_SOL_OUT, IS_NO_SOL_OUT);
750 } else if (PARS("send_solicit")) {
751 CKF(GROUP_IS_SOL_OUT, IS_SOL_OUT);
753 } else if (PARS("no_rdisc_adv")) {
754 CKF(GROUP_IS_ADV_OUT, IS_NO_ADV_OUT);
756 } else if (PARS("rdisc_adv")) {
757 CKF(GROUP_IS_ADV_OUT, IS_ADV_OUT);
759 } else if (PARS("bcast_rdisc")) {
760 parm.parm_int_state |= IS_BCAST_RDISC;
762 } else if (PARS("passive")) {
763 CKF((GROUP_IS_SOL_OUT|GROUP_IS_ADV_OUT), IS_NO_RDISC);
764 parm.parm_int_state |= IS_NO_RIP | IS_PASSIVE;
766 } else if (PARSEQ("rdisc_pref")) {
767 if (parm.parm_rdisc_pref != 0
768 || (parm.parm_rdisc_pref = (int)strtol(buf,&p,0),
772 } else if (PARS("pm_rdisc")) {
773 if (IS_RIP_OUT_OFF(parm.parm_int_state))
775 parm.parm_int_state |= IS_PM_RDISC;
777 } else if (PARSEQ("rdisc_interval")) {
778 if (parm.parm_rdisc_int != 0
779 || (parm.parm_rdisc_int = (int)strtoul(buf,&p,0),
781 || parm.parm_rdisc_int < MinMaxAdvertiseInterval
782 || parm.parm_rdisc_int > MaxMaxAdvertiseInterval)
785 } else if (PARSEQ("fake_default")) {
786 if (parm.parm_d_metric != 0
787 || IS_RIP_OUT_OFF(parm.parm_int_state)
788 || (i = strtoul(buf,&p,0), *p != '\0')
789 || i > HOPCNT_INFINITY-1)
791 parm.parm_d_metric = i;
793 } else if (PARSEQ("adj_inmetric")) {
794 if (parm.parm_adj_inmetric != 0
795 || (i = strtoul(buf,&p,0), *p != '\0')
796 || i > HOPCNT_INFINITY-1)
798 parm.parm_adj_inmetric = i;
800 } else if (PARSEQ("adj_outmetric")) {
801 if (parm.parm_adj_outmetric != 0
802 || (i = strtoul(buf,&p,0), *p != '\0')
803 || i > HOPCNT_INFINITY-1)
805 parm.parm_adj_outmetric = i;
807 } else if (PARSEQ("trust_gateway")) {
808 /* look for trust_gateway=x.y.z|net/mask|...) */
810 if (0 > parse_quote(&p, "|", &delim,
812 || !gethost(buf2,&addr))
814 tg = (struct tgate *)rtmalloc(sizeof(*tg),
817 memset(tg, 0, sizeof(*tg));
818 tg->tgate_addr = addr;
820 /* The default is to trust all routes. */
821 while (delim == '|') {
823 if (i >= MAX_TGATE_NETS
824 || 0 > parse_quote(&p, "|", &delim,
826 || !getnet(buf2, &tg->tgate_nets[i].net,
827 &tg->tgate_nets[i].mask)
828 || tg->tgate_nets[i].net == RIP_DEFAULT
829 || tg->tgate_nets[i].mask == 0)
833 tg->tgate_next = tgates;
835 parm.parm_int_state |= IS_DISTRUST;
837 } else if (PARS("redirect_ok")) {
838 parm.parm_int_state |= IS_REDIRECT_OK;
841 return bad_str(tgt); /* error */
845 return check_parms(&parm);
851 /* check for duplicate parameter specifications */
852 const char * /* 0 or error message */
853 check_parms(struct parm *new)
855 struct parm *parmp, **parmpp;
858 /* set implicit values
860 if (new->parm_int_state & IS_NO_ADV_IN)
861 new->parm_int_state |= IS_NO_SOL_OUT;
862 if (new->parm_int_state & IS_NO_SOL_OUT)
863 new->parm_int_state |= IS_NO_ADV_IN;
865 for (i = num_passwds = 0; i < MAX_AUTH_KEYS; i++) {
866 if (new->parm_auth[i].type != RIP_AUTH_NONE)
870 /* compare with existing sets of parameters
872 for (parmpp = &parms;
873 (parmp = *parmpp) != 0;
874 parmpp = &parmp->parm_next) {
875 if (strcmp(new->parm_name, parmp->parm_name))
877 if (!on_net(htonl(parmp->parm_net),
878 new->parm_net, new->parm_mask)
879 && !on_net(htonl(new->parm_net),
880 parmp->parm_net, parmp->parm_mask))
883 for (i = 0; i < MAX_AUTH_KEYS; i++) {
884 if (parmp->parm_auth[i].type != RIP_AUTH_NONE)
887 if (num_passwds > MAX_AUTH_KEYS)
888 return "too many conflicting passwords";
890 if ((0 != (new->parm_int_state & GROUP_IS_SOL_OUT)
891 && 0 != (parmp->parm_int_state & GROUP_IS_SOL_OUT)
892 && 0 != ((new->parm_int_state ^ parmp->parm_int_state)
893 && GROUP_IS_SOL_OUT))
894 || (0 != (new->parm_int_state & GROUP_IS_ADV_OUT)
895 && 0 != (parmp->parm_int_state & GROUP_IS_ADV_OUT)
896 && 0 != ((new->parm_int_state ^ parmp->parm_int_state)
897 && GROUP_IS_ADV_OUT))
898 || (new->parm_rdisc_pref != 0
899 && parmp->parm_rdisc_pref != 0
900 && new->parm_rdisc_pref != parmp->parm_rdisc_pref)
901 || (new->parm_rdisc_int != 0
902 && parmp->parm_rdisc_int != 0
903 && new->parm_rdisc_int != parmp->parm_rdisc_int)) {
904 return ("conflicting, duplicate router discovery"
909 if (new->parm_d_metric != 0
910 && parmp->parm_d_metric != 0
911 && new->parm_d_metric != parmp->parm_d_metric) {
912 return ("conflicting, duplicate poor man's router"
913 " discovery or fake default metric");
916 if (new->parm_adj_inmetric != 0
917 && parmp->parm_adj_inmetric != 0
918 && new->parm_adj_inmetric != parmp->parm_adj_inmetric) {
919 return ("conflicting interface input "
920 "metric adjustments");
923 if (new->parm_adj_outmetric != 0
924 && parmp->parm_adj_outmetric != 0
925 && new->parm_adj_outmetric != parmp->parm_adj_outmetric) {
926 return ("conflicting interface output "
927 "metric adjustments");
931 /* link new entry on the list so that when the entries are scanned,
932 * they affect the result in the order the operator specified.
934 parmp = (struct parm*)rtmalloc(sizeof(*parmp), "check_parms");
935 memcpy(parmp, new, sizeof(*parmp));
942 /* get a network number as a name or a number, with an optional "/xx"
947 naddr *netp, /* network in host byte order */
948 naddr *maskp) /* masks are always in host order */
952 naddr mask; /* in host byte order */
953 struct in_addr in; /* a network and so host byte order */
954 char hname[MAXHOSTNAMELEN+1];
958 /* Detect and separate "1.2.3.4/24"
960 if (0 != (mname = strrchr(name,'/'))) {
961 i = (int)(mname - name);
962 if (i > (int)sizeof(hname)-1) /* name too long */
964 memmove(hname, name, i);
970 np = getnetbyname(name);
972 in.s_addr = (naddr)np->n_net;
973 if (0 == (in.s_addr & 0xff000000))
975 if (0 == (in.s_addr & 0xff000000))
977 if (0 == (in.s_addr & 0xff000000))
979 } else if (inet_aton(name, &in) == 1) {
980 in.s_addr = ntohl(in.s_addr);
981 } else if (!mname && !strcasecmp(name,"default")) {
982 in.s_addr = RIP_DEFAULT;
988 /* we cannot use the interfaces here because we have not
989 * looked at them yet.
991 mask = std_mask(htonl(in.s_addr));
992 if ((~mask & in.s_addr) != 0)
995 mask = (naddr)strtoul(mname, &p, 0);
996 if (*p != '\0' || mask > 32)
999 mask = HOST_MASK << (32-mask);
1002 /* must have mask of 0 with default */
1003 if (mask != 0 && in.s_addr == RIP_DEFAULT)
1005 /* no host bits allowed in a network number */
1006 if ((~mask & in.s_addr) != 0)
1008 /* require non-zero network number */
1009 if ((mask & in.s_addr) == 0 && in.s_addr != RIP_DEFAULT)
1011 if (in.s_addr>>24 == 0 && in.s_addr != RIP_DEFAULT)
1013 if (in.s_addr>>24 == 0xff)
1030 /* Try for a number first, even in IRIX where gethostbyname()
1031 * is smart. This avoids hitting the name server which
1032 * might be sick because routing is.
1034 if (inet_aton(name, &in) == 1) {
1035 /* get a good number, but check that it it makes some
1038 if (ntohl(in.s_addr)>>24 == 0
1039 || ntohl(in.s_addr)>>24 == 0xff)
1045 hp = gethostbyname(name);
1047 memcpy(addrp, hp->h_addr, sizeof(*addrp));