3 * Configuration file parser for mrouted.
5 * Written by Bill Fenner, NRL, 1994
7 * $FreeBSD: src/usr.sbin/mrouted/cfparse.y,v 1.10.2.2 2001/07/19 01:41:11 kris Exp $
8 * $DragonFly: src/usr.sbin/mrouted/cfparse.y,v 1.2 2003/06/17 04:29:57 dillon Exp $
9 * cfparse.y,v 3.8.4.30 1998/03/01 01:48:58 fenner Exp
21 * Local function declarations
23 static void fatal __P((char *fmt, ...)) __printflike(1, 2);
24 static void warn __P((char *fmt, ...)) __printflike(1, 2);;
25 static void yyerror __P((char *s));
26 static char * next_word __P((void));
27 static int yylex __P((void));
28 static u_int32 valid_if __P((char *s));
29 static struct ifreq * ifconfaddr __P((struct ifconf *ifcp, u_int32 a));
30 int yyparse __P((void));
34 char *configfilename = _PATH_MROUTED_CONF;
36 extern int cache_lifetime;
37 extern int prune_lifetime;
39 /* imported from config.c, with slight memory leak */
40 extern struct ifconf ifc;
42 int allow_black_holes = 0;
46 static struct uvif *v;
48 static int order, state;
49 static int noflood = 0;
50 static int rexmit = VIFF_REXMIT_PRUNES;
59 struct addrmask bound;
64 struct boundnam boundlist[MAXBOUNDS]; /* Max. of 20 named boundaries */
65 int numbounds = 0; /* Number of named boundaries */
73 struct addrmask addrmask;
75 struct vf_element *filterelem;
78 %token CACHE_LIFETIME PRUNE_LIFETIME PRUNING BLACK_HOLE NOFLOOD
79 %token PHYINT TUNNEL NAME
80 %token DISABLE IGMPV1 SRCRT BESIDE
81 %token METRIC THRESHOLD RATE_LIMIT BOUNDARY NETMASK ALTNET ADVERT_METRIC
82 %token FILTER ACCEPT DENY EXACT BIDIR REXMIT_PRUNES REXMIT_PRUNES2
83 %token PASSIVE ALLOW_NONPRUNERS
84 %token NOTRANSIT BLASTER FORCE_LEAF
85 %token PRUNE_LIFETIME2 NOFLOOD2
86 %token SYSNAM SYSCONTACT SYSVERSION SYSLOCATION
90 %token <addrmask> ADDRMASK
93 %type <addr> interface addrname
94 %type <addrmask> bound boundary addrmask
95 %type <filterelem> filter filtlist filtelement filtelem
116 fatal("phyints must appear before tunnels");
118 for (vifi = 0, v = uvifs;
121 if (!(v->uv_flags & VIFF_TUNNEL) &&
122 $2 == v->uv_lcl_addr)
126 fatal("%s is not a configured interface",
131 | TUNNEL interface addrname {
139 ifr = ifconfaddr(&ifc, $2);
141 fatal("Tunnel local address %s is not mine",
144 if (((ntohl($2) & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT) ==
146 fatal("Tunnel local address %s is a loopback address",
149 if (ifconfaddr(&ifc, $3) != 0)
150 fatal("Tunnel remote address %s is one of mine",
153 for (vifi = 0, v = uvifs;
156 if (v->uv_flags & VIFF_TUNNEL) {
157 if ($3 == v->uv_rmt_addr)
158 fatal("Duplicate tunnel to %s",
160 } else if (!(v->uv_flags & VIFF_DISABLED)) {
161 if (($3 & v->uv_subnetmask) == v->uv_subnet)
162 fatal("Unnecessary tunnel to %s, same subnet as vif %d (%s)",
163 inet_fmt($3,s1), vifi, v->uv_name);
166 if (numvifs == MAXVIFS)
167 fatal("too many vifs");
169 strncpy(ffr.ifr_name, ifr->ifr_name, IFNAMSIZ);
170 if (ioctl(udp_socket, SIOCGIFFLAGS, (char *)&ffr)<0)
171 fatal("ioctl SIOCGIFFLAGS on %s", ffr.ifr_name);
175 v->uv_flags = VIFF_TUNNEL | rexmit | noflood;
176 v->uv_flags |= VIFF_OTUNNEL; /*XXX*/
180 strncpy(v->uv_name, ffr.ifr_name, IFNAMSIZ);
181 v->uv_name[IFNAMSIZ-1]='\0';
183 if (!(ffr.ifr_flags & IFF_UP)) {
184 v->uv_flags |= VIFF_DOWN;
191 if (!(v->uv_flags & VIFF_OTUNNEL)) {
196 "installing tunnel from %s to %s as vif #%u - rate=%d",
197 inet_fmt($2, s1), inet_fmt($3, s2),
198 numvifs, v->uv_rate_limit);
203 | CACHE_LIFETIME NUMBER {
205 if ($2 < MIN_CACHE_LIFETIME) {
206 warn("cache_lifetime %d must be at least %d",
207 $2, MIN_CACHE_LIFETIME);
213 | PRUNE_LIFETIME NUMBER {
215 if ($2 < MIN_PRUNE_LIFETIME) {
216 warn("prune_lifetime %d must be at least %d",
217 $2, MIN_PRUNE_LIFETIME);
226 warn("Disabling pruning is no longer supported");
231 #ifdef ALLOW_BLACK_HOLES
232 allow_black_holes = 1;
236 * Turn off initial flooding (until subordinateness is learned
237 * via route exchange) on all phyints and set the default for
238 * all further tunnels.
244 noflood = VIFF_NOFLOOD;
245 for (vifi = 0, v = uvifs;
248 v->uv_flags |= VIFF_NOFLOOD;
252 * Turn on prune retransmission on all interfaces.
253 * Tunnels default to retransmitting, so this just
254 * needs to turn on phyints.
260 for (vifi = 0, v = uvifs;
263 v->uv_flags |= VIFF_REXMIT_PRUNES;
267 * If true, do as above. If false, no need to turn
268 * it off for phyints since they default to not
269 * rexmit; need to set flag to not rexmit on tunnels.
271 | REXMIT_PRUNES BOOLEAN {
276 for (vifi = 0, v = uvifs;
279 v->uv_flags |= VIFF_REXMIT_PRUNES;
285 | NAME STRING boundary { if (numbounds >= MAXBOUNDS) {
286 fatal("Too many named boundaries (max %d)", MAXBOUNDS);
289 boundlist[numbounds].name = malloc(strlen($2) + 1);
290 strcpy(boundlist[numbounds].name, $2);
291 boundlist[numbounds++].bound = $3;
298 | SYSCONTACT STRING {
303 | SYSVERSION STRING {
308 | SYSLOCATION STRING {
315 tunnelmods : /* empty */
316 | tunnelmods tunnelmod
320 | BESIDE { v->uv_flags |= VIFF_OTUNNEL; }
324 v->uv_flags |= VIFF_OTUNNEL;
326 v->uv_flags &= ~VIFF_OTUNNEL;
330 | SRCRT { fatal("Source-route tunnels not supported"); }
338 | DISABLE { v->uv_flags |= VIFF_DISABLED; }
339 | IGMPV1 { v->uv_flags |= VIFF_IGMPV1; }
341 u_int32 subnet, mask;
344 subnet = v->uv_lcl_addr & mask;
345 if (!inet_valid_subnet(subnet, mask))
346 fatal("Invalid netmask");
347 v->uv_subnet = subnet;
348 v->uv_subnetmask = mask;
349 v->uv_subnetbcast = subnet | ~mask;
353 warn("Expected address after netmask keyword, ignored");
360 ph = (struct phaddr *)malloc(sizeof(struct phaddr));
362 fatal("out of memory");
364 VAL_TO_MASK(ph->pa_subnetmask, $2.mask);
366 ph->pa_subnetmask = v->uv_subnetmask;
367 ph->pa_subnet = $2.addr & ph->pa_subnetmask;
368 ph->pa_subnetbcast = ph->pa_subnet | ~ph->pa_subnetmask;
369 if ($2.addr & ~ph->pa_subnetmask)
370 warn("Extra subnet %s/%d has host bits set",
371 inet_fmt($2.addr,s1), $2.mask);
372 ph->pa_next = v->uv_addrs;
378 warn("Expected address after altnet keyword, ignored");
383 v->uv_flags |= VIFF_FORCE_LEAF;
386 | FORCE_LEAF BOOLEAN {
389 v->uv_flags |= VIFF_FORCE_LEAF;
391 v->uv_flags &= ~VIFF_FORCE_LEAF;
397 mod : THRESHOLD NUMBER { if ($2 < 1 || $2 > 255)
398 fatal("Invalid threshold %d",$2);
399 v->uv_threshold = $2;
403 warn("Expected number after threshold keyword, ignored");
406 | METRIC NUMBER { if ($2 < 1 || $2 > UNREACHABLE)
407 fatal("Invalid metric %d",$2);
412 warn("Expected number after metric keyword, ignored");
415 | ADVERT_METRIC NUMBER { if ($2 < 0 || $2 > UNREACHABLE - 1)
416 fatal("Invalid advert_metric %d", $2);
421 warn("Expected number after advert_metric keyword, ignored");
424 | RATE_LIMIT NUMBER { if ($2 > MAX_RATE_LIMIT)
425 fatal("Invalid rate_limit %d",$2);
426 v->uv_rate_limit = $2;
430 warn("Expected number after rate_limit keyword, ignored");
435 struct vif_acl *v_acl;
437 v_acl = (struct vif_acl *)malloc(sizeof(struct vif_acl));
439 fatal("out of memory");
440 VAL_TO_MASK(v_acl->acl_mask, $2.mask);
441 v_acl->acl_addr = $2.addr & v_acl->acl_mask;
442 if ($2.addr & ~v_acl->acl_mask)
443 warn("Boundary spec %s/%d has host bits set",
444 inet_fmt($2.addr,s1),$2.mask);
445 v_acl->acl_next = v->uv_acl;
451 warn("Expected boundary spec after boundary keyword, ignored");
456 v->uv_flags |= VIFF_REXMIT_PRUNES;
459 | REXMIT_PRUNES2 BOOLEAN {
462 v->uv_flags |= VIFF_REXMIT_PRUNES;
464 v->uv_flags &= ~VIFF_REXMIT_PRUNES;
470 v->uv_flags |= VIFF_PASSIVE;
475 v->uv_flags |= VIFF_NOFLOOD;
480 v->uv_flags |= VIFF_NOTRANSIT;
485 v->uv_flags |= VIFF_BLASTER;
486 blaster_alloc(v - uvifs);
491 v->uv_flags |= VIFF_ALLOW_NONPRUNERS;
494 | PRUNE_LIFETIME2 NUMBER {
496 if ($2 < MIN_PRUNE_LIFETIME) {
497 warn("prune_lifetime %d must be at least %d",
498 $2, MIN_PRUNE_LIFETIME);
500 v->uv_prune_lifetime = $2;
506 if (v->uv_filter == NULL) {
507 struct vif_filter *v_filter;
509 v_filter = (struct vif_filter *)malloc(sizeof(struct vif_filter));
510 if (v_filter == NULL)
511 fatal("out of memory");
512 v_filter->vf_flags = 0;
513 v_filter->vf_type = VFT_ACCEPT;
514 v_filter->vf_filter = $2;
515 v->uv_filter = v_filter;
516 } else if (v->uv_filter->vf_type != VFT_ACCEPT) {
517 fatal("can't accept and deny");
519 struct vf_element *p;
521 p = v->uv_filter->vf_filter;
530 warn("Expected filter spec after accept keyword, ignored");
535 if (v->uv_filter == NULL) {
536 struct vif_filter *v_filter;
538 v_filter = (struct vif_filter *)malloc(sizeof(struct vif_filter));
539 if (v_filter == NULL)
540 fatal("out of memory");
541 v_filter->vf_flags = 0;
542 v_filter->vf_type = VFT_DENY;
543 v_filter->vf_filter = $2;
544 v->uv_filter = v_filter;
545 } else if (v->uv_filter->vf_type != VFT_DENY) {
546 fatal("can't accept and deny");
548 struct vf_element *p;
550 p = v->uv_filter->vf_filter;
559 warn("Expected filter spec after deny keyword, ignored");
564 if (v->uv_filter == NULL) {
565 fatal("bidir goes after filters");
567 v->uv_filter->vf_flags |= VFF_BIDIR;
572 interface : ADDR { $$ = $1; }
576 fatal("Invalid interface name %s",$1);
580 addrname : ADDR { $$ = $1; }
581 | STRING { struct hostent *hp;
583 if ((hp = gethostbyname($1)) == NULL ||
584 hp->h_length != sizeof($$))
585 fatal("No such host %s", $1);
587 if (hp->h_addr_list[1])
588 fatal("Hostname %s does not %s",
589 $1, "map to a unique address");
591 bcopy(hp->h_addr_list[0], &$$,
595 bound : boundary { $$ = $1; }
598 for (i=0; i < numbounds; i++) {
599 if (!strcmp(boundlist[i].name, $1)) {
600 $$ = boundlist[i].bound;
604 if (i == numbounds) {
605 fatal("Invalid boundary name %s",$1);
610 boundary : ADDRMASK {
612 #ifdef ALLOW_BLACK_HOLES
613 if (!allow_black_holes)
615 if ((ntohl($1.addr) & 0xff000000) != 0xef000000) {
616 fatal("Boundaries must be 239.x.x.x, not %s/%d",
617 inet_fmt($1.addr, s1), $1.mask);
624 addrmask : ADDRMASK { $$ = $1; }
625 | ADDR { $$.addr = $1; $$.mask = 0; }
628 filter : filtlist { $$ = $1; }
629 | STRING { fatal("named filters no implemented yet"); }
632 filtlist : filtelement { $$ = $1; }
633 | filtelement filtlist { $1->vfe_next = $2; $$ = $1; }
636 filtelement : filtelem { $$ = $1; }
637 | filtelem EXACT { $1->vfe_flags |= VFEF_EXACT; $$ = $1; }
640 filtelem : ADDRMASK {
642 struct vf_element *vfe;
644 vfe = (struct vf_element *)malloc(sizeof(struct vf_element));
646 fatal("out of memory");
648 vfe->vfe_addr = $1.addr;
649 VAL_TO_MASK(vfe->vfe_mask, $1.mask);
651 vfe->vfe_next = NULL;
659 fatal(char *fmt, ...)
662 char buf[MAXHOSTNAMELEN + 100];
673 char buf[MAXHOSTNAMELEN + 100];
677 vsnprintf(buf, sizeof(buf), fmt, ap);
680 log(LOG_ERR,0,"%s: %s near line %d", configfilename, buf, lineno);
703 vsnprintf(buf, sizeof(buf), fmt, ap);
706 log(LOG_WARNING,0,"%s: %s near line %d", configfilename, buf, lineno);
713 log(LOG_ERR, 0, "%s: %s near line %d", configfilename, s, lineno);
719 static char buf[1024];
726 if (fgets(buf, sizeof(buf), f) == NULL)
730 while (*p && (*p == ' ' || *p == '\t')) /* skip whitespace */
733 p = NULL; /* skip comments */
740 while (*p && *p != '"' && *p != '\n')
741 p++; /* find next whitespace */
746 while (*p && *p != ' ' && *p != '\t' && *p != '\n')
747 p++; /* find next whitespace */
748 *p++ = '\0'; /* null-terminate string */
752 continue; /* if 0-length string, read another line */
760 * List of keywords. Must have an empty record at the end to terminate
761 * list. If a second value is specified, the first is used at the beginning
762 * of the file and the second is used while parsing interfaces (e.g. after
763 * the first "phyint" or "tunnel" keyword).
765 static struct keyword {
770 { "cache_lifetime", CACHE_LIFETIME },
771 { "prune_lifetime", PRUNE_LIFETIME, PRUNE_LIFETIME2 },
772 { "pruning", PRUNING },
773 { "phyint", PHYINT },
774 { "tunnel", TUNNEL },
775 { "disable", DISABLE },
776 { "metric", METRIC },
777 { "advert_metric", ADVERT_METRIC },
778 { "threshold", THRESHOLD },
779 { "rate_limit", RATE_LIMIT },
780 { "force_leaf", FORCE_LEAF },
782 { "sourceroute", SRCRT },
783 { "boundary", BOUNDARY },
784 { "netmask", NETMASK },
785 { "igmpv1", IGMPV1 },
786 { "altnet", ALTNET },
788 { "accept", ACCEPT },
792 { "allow_nonpruners", ALLOW_NONPRUNERS },
793 #ifdef ALLOW_BLACK_HOLES
794 { "allow_black_holes", BLACK_HOLE },
796 { "noflood", NOFLOOD, NOFLOOD2},
797 { "notransit", NOTRANSIT },
798 { "blaster", BLASTER },
799 { "rexmit_prunes", REXMIT_PRUNES, REXMIT_PRUNES2 },
800 { "passive", PASSIVE },
801 { "beside", BESIDE },
803 { "sysName", SYSNAM },
804 { "sysContact", SYSCONTACT },
805 { "sysVersion", SYSVERSION },
806 { "sysLocation", SYSLOCATION },
820 if ((q = next_word()) == NULL) {
824 for (w = words; w->word; w++)
825 if (!strcmp(q, w->word))
826 return (state && w->val2) ? w->val2 : w->val1;
828 if (!strcmp(q,"on") || !strcmp(q,"yes")) {
832 if (!strcmp(q,"off") || !strcmp(q,"no")) {
836 if (!strcmp(q,"default")) {
837 yylval.addrmask.mask = 0;
838 yylval.addrmask.addr = 0;
841 if (sscanf(q,"%[.0-9]/%d%c",s1,&n,s2) == 2) {
842 if ((addr = inet_parse(s1,1)) != 0xffffffff) {
843 yylval.addrmask.mask = n;
844 yylval.addrmask.addr = addr;
847 /* fall through to returning STRING */
849 if (sscanf(q,"%[.0-9]%c",s1,s2) == 1) {
850 if ((addr = inet_parse(s1,4)) != 0xffffffff &&
851 inet_valid_host(addr)) {
856 if (sscanf(q,"0x%8x%c",&n,s1) == 1) {
860 if (sscanf(q,"%d%c",&n,s1) == 1) {
866 if (q[ strlen(q)-1 ]=='"')
867 q[ strlen(q)-1 ]='\0'; /* trash trailing quote */
877 config_vifs_from_file()
884 if ((f = fopen(configfilename, "r")) == NULL) {
886 log(LOG_ERR, errno, "can't open %s", configfilename);
899 register vifi_t vifi;
900 register struct uvif *v;
902 for (vifi=0, v=uvifs; vifi<numvifs; vifi++, v++)
903 if (!strcmp(v->uv_name, s))
904 return v->uv_lcl_addr;
909 static struct ifreq *
915 struct ifreq *ifrp = (struct ifreq *)ifcp->ifc_buf;
916 struct ifreq *ifend = (struct ifreq *)((char *)ifrp + ifcp->ifc_len);
918 while (ifrp < ifend) {
919 if (ifrp->ifr_addr.sa_family == AF_INET &&
920 ((struct sockaddr_in *)&ifrp->ifr_addr)->sin_addr.s_addr == a)
923 n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
924 if (n < sizeof(*ifrp))
927 ifrp = (struct ifreq *)((char *)ifrp + n);