Home | History | Annotate | Line # | Download | only in mrouted
cfparse.y revision 1.1
      1 %{
      2 /*
      3  * Configuration file parser for mrouted.
      4  *
      5  * Written by Bill Fenner, NRL, 1994
      6  *
      7  * $Id: cfparse.y,v 1.1 1995/06/01 02:25:48 mycroft Exp $
      8  */
      9 #include <stdio.h>
     10 #include <varargs.h>
     11 #include "defs.h"
     12 
     13 static FILE *f;
     14 
     15 extern int udp_socket;
     16 char *configfilename = _PATH_MROUTED_CONF;
     17 
     18 extern int cache_lifetime;
     19 extern int max_prune_lifetime;
     20 
     21 static int lineno;
     22 static struct ifreq ifbuf[32];
     23 static struct ifconf ifc;
     24 
     25 static struct uvif *v;
     26 
     27 static int order;
     28 
     29 struct addrmask {
     30 	u_int32_t	addr;
     31 	int	mask;
     32 };
     33 
     34 struct boundnam {
     35 	char		*name;
     36 	struct addrmask	 bound;
     37 };
     38 
     39 #define MAXBOUNDS 20
     40 
     41 struct boundnam boundlist[MAXBOUNDS];	/* Max. of 20 named boundaries */
     42 int numbounds = 0;			/* Number of named boundaries */
     43 
     44 %}
     45 
     46 %union
     47 {
     48 	int num;
     49 	char *ptr;
     50 	struct addrmask addrmask;
     51 	u_int32_t addr;
     52 };
     53 
     54 %token CACHE_LIFETIME PRUNING
     55 %token PHYINT TUNNEL NAME
     56 %token DISABLE METRIC THRESHOLD RATE_LIMIT SRCRT BOUNDARY NETMASK ALTNET
     57 %token <num> BOOLEAN
     58 %token <num> NUMBER
     59 %token <ptr> STRING
     60 %token <addrmask> ADDRMASK
     61 %token <addr> ADDR
     62 
     63 %type <addr> interface
     64 %type <addrmask> bound boundary addrmask
     65 
     66 %start conf
     67 
     68 %%
     69 
     70 conf	: stmts
     71 	;
     72 
     73 stmts	: /* Empty */
     74 	| stmts stmt
     75 	;
     76 
     77 stmt	: error
     78 	| PHYINT interface 		{
     79 
     80 			vifi_t vifi;
     81 
     82 			if (order)
     83 			    fatal("phyints must appear before tunnels");
     84 
     85 			for (vifi = 0, v = uvifs;
     86 			     vifi < numvifs;
     87 			     ++vifi, ++v)
     88 			    if (!(v->uv_flags & VIFF_TUNNEL) &&
     89 				$2 == v->uv_lcl_addr)
     90 				break;
     91 
     92 			if (vifi == numvifs)
     93 			    fatal("%s is not a configured interface",
     94 				inet_fmt($2,s1));
     95 
     96 			/*log(LOG_INFO, 0, "phyint: %x\n", v);*/
     97 					}
     98 		ifmods
     99 	| TUNNEL interface ADDR		{
    100 
    101 			struct ifreq *ifr;
    102 			struct ifreq ffr;
    103 			vifi_t vifi;
    104 
    105 			order++;
    106 
    107 			ifr = ifconfaddr(&ifc, $2);
    108 			if (ifr == 0)
    109 			    fatal("Tunnel local address %s is not mine",
    110 				inet_fmt($2, s1));
    111 
    112 			strncpy(ffr.ifr_name, ifr->ifr_name, IFNAMSIZ);
    113 			if (ioctl(udp_socket, SIOCGIFFLAGS, (char *)&ffr)<0)
    114 			    fatal("ioctl SIOCGIFFLAGS on %s",ffr.ifr_name);
    115 			if (ffr.ifr_flags & IFF_LOOPBACK)
    116 			    fatal("Tunnel local address %s is a loopback interface",
    117 				inet_fmt($2, s1));
    118 
    119 			if (ifconfaddr(&ifc, $3) != 0)
    120 			    fatal("Tunnel remote address %s is one of mine",
    121 				inet_fmt($3, s1));
    122 
    123 			for (vifi = 0, v = uvifs;
    124 			     vifi < numvifs;
    125 			     ++vifi, ++v)
    126 			    if (v->uv_flags & VIFF_TUNNEL) {
    127 				if ($3 == v->uv_rmt_addr)
    128 				    fatal("Duplicate tunnel to %s",
    129 					inet_fmt($3, s1));
    130 			    } else if (!(v->uv_flags & VIFF_DISABLED)) {
    131 				if (($3 & v->uv_subnetmask) == v->uv_subnet)
    132 				    fatal("Unnecessary tunnel to %s",
    133 					inet_fmt($3,s1));
    134 			    }
    135 
    136 			if (numvifs == MAXVIFS)
    137 			    fatal("too many vifs");
    138 
    139 			v = &uvifs[numvifs];
    140 			v->uv_flags	= VIFF_TUNNEL;
    141 			v->uv_metric	= DEFAULT_METRIC;
    142 			v->uv_rate_limit= DEFAULT_TUN_RATE_LIMIT;
    143 			v->uv_threshold	= DEFAULT_THRESHOLD;
    144 			v->uv_lcl_addr	= $2;
    145 			v->uv_rmt_addr	= $3;
    146 			v->uv_subnet	= 0;
    147 			v->uv_subnetmask= 0;
    148 			v->uv_subnetbcast= 0;
    149 			strncpy(v->uv_name, ffr.ifr_name, IFNAMSIZ);
    150 			v->uv_groups	= NULL;
    151 			v->uv_neighbors	= NULL;
    152 			v->uv_acl	= NULL;
    153 			v->uv_addrs	= NULL;
    154 
    155 			if (!(ffr.ifr_flags & IFF_UP)) {
    156 			    v->uv_flags |= VIFF_DOWN;
    157 			    vifs_down = TRUE;
    158 			}
    159 			/*log(LOG_INFO, 0, "tunnel: %x\n", v);*/
    160 					}
    161 		tunnelmods
    162 					{
    163 			log(LOG_INFO, 0,
    164 			    "installing tunnel from %s to %s as vif #%u - rate=%d",
    165 			    inet_fmt($2, s1), inet_fmt($3, s2),
    166 			    numvifs, v->uv_rate_limit);
    167 
    168 			++numvifs;
    169 					}
    170 	| PRUNING BOOLEAN	    { pruning = $2; }
    171 	| CACHE_LIFETIME NUMBER     { cache_lifetime = $2;
    172 				      max_prune_lifetime = cache_lifetime * 2;
    173 				    }
    174 	| NAME STRING boundary	    { if (numbounds >= MAXBOUNDS) {
    175 					fatal("Too many named boundaries (max %d)", MAXBOUNDS);
    176 				      }
    177 
    178 				      boundlist[numbounds].name = malloc(strlen($2) + 1);
    179 				      strcpy(boundlist[numbounds].name, $2);
    180 				      boundlist[numbounds++].bound = $3;
    181 				    }
    182 	;
    183 
    184 tunnelmods	: /* empty */
    185 	| tunnelmods /*{ log(LOG_INFO, 0, "tunnelmod: %x", v); }*/ tunnelmod
    186 	;
    187 
    188 tunnelmod	: mod
    189 	| SRCRT			{ fatal("Source-route tunnels not supported"); }
    190 	;
    191 
    192 ifmods	: /* empty */
    193 	| ifmods /*{ log(LOG_INFO, 0, "ifmod: %x", v); }*/ ifmod
    194 	;
    195 
    196 ifmod	: mod
    197 	| DISABLE		{ v->uv_flags |= VIFF_DISABLED; }
    198 	| NETMASK ADDR		{ v->uv_subnetmask = $2; }
    199 	| ALTNET addrmask	{
    200 
    201 		    struct phaddr *ph;
    202 
    203 		    ph = (struct phaddr *)malloc(sizeof(struct phaddr));
    204 		    if (ph == NULL)
    205 			fatal("out of memory");
    206 		    if ($2.mask) {
    207 			VAL_TO_MASK(ph->pa_mask, $2.mask);
    208 		    } else
    209 			ph->pa_mask = v->uv_subnetmask;
    210 		    ph->pa_addr = $2.addr & ph->pa_mask;
    211 		    if ($2.addr & ~ph->pa_mask)
    212 			warn("Extra addr %s/%d has host bits set",
    213 				inet_fmt($2.addr,s1), $2.mask);
    214 		    ph->pa_next = v->uv_addrs;
    215 		    v->uv_addrs = ph;
    216 
    217 				}
    218 	;
    219 
    220 mod	: THRESHOLD NUMBER	{ if ($2 < 1 || $2 > 255)
    221 				    fatal("Invalid threshold %d",$2);
    222 				  v->uv_threshold = $2;
    223 				}
    224 	| THRESHOLD		{
    225 
    226 		    warn("Expected number after threshold keyword");
    227 
    228 				}
    229 	| METRIC NUMBER		{ if ($2 < 1 || $2 > UNREACHABLE)
    230 				    fatal("Invalid metric %d",$2);
    231 				  v->uv_metric = $2;
    232 				}
    233 	| METRIC		{
    234 
    235 		    warn("Expected number after metric keyword");
    236 
    237 				}
    238 	| RATE_LIMIT NUMBER	{ if ($2 > MAX_RATE_LIMIT)
    239 				    fatal("Invalid rate_limit %d",$2);
    240 				  v->uv_rate_limit = $2;
    241 				}
    242 	| RATE_LIMIT		{
    243 
    244 		    warn("Expected number after rate_limit keyword");
    245 
    246 				}
    247 	| BOUNDARY bound	{
    248 
    249 		    struct vif_acl *v_acl;
    250 
    251 		    v_acl = (struct vif_acl *)malloc(sizeof(struct vif_acl));
    252 		    if (v_acl == NULL)
    253 			fatal("out of memory");
    254 		    VAL_TO_MASK(v_acl->acl_mask, $2.mask);
    255 		    v_acl->acl_addr = $2.addr & v_acl->acl_mask;
    256 		    if ($2.addr & ~v_acl->acl_mask)
    257 			warn("Boundary spec %s/%d has host bits set",
    258 				inet_fmt($2.addr,s1),$2.mask);
    259 		    v_acl->acl_next = v->uv_acl;
    260 		    v->uv_acl = v_acl;
    261 
    262 				}
    263 	| BOUNDARY		{
    264 
    265 		    warn("Expected boundary spec after boundary keyword");
    266 
    267 				}
    268 	;
    269 
    270 interface	: ADDR		{ $$ = $1; }
    271 	| STRING		{
    272 				  $$ = valid_if($1);
    273 				  if ($$ == 0)
    274 					fatal("Invalid interface name %s",$1);
    275 				}
    276 	;
    277 
    278 bound	: boundary		{ $$ = $1; }
    279 	| STRING		{ int i;
    280 
    281 				  for (i=0; i < numbounds; i++) {
    282 				    if (!strcmp(boundlist[i].name, $1)) {
    283 					$$ = boundlist[i].bound;
    284 					break;
    285 				    }
    286 				  }
    287 				  if (i == numbounds) {
    288 				    fatal("Invalid boundary name %s",$1);
    289 				  }
    290 				}
    291 	;
    292 
    293 boundary	: ADDRMASK	{
    294 
    295 			if ((ntohl($1.addr) & 0xff000000) != 0xef000000) {
    296 			    fatal("Boundaries must be 239.x.x.x, not %s/%d",
    297 				inet_fmt($1.addr, s1), $1.mask);
    298 			}
    299 			$$ = $1;
    300 
    301 				}
    302 	;
    303 
    304 addrmask	: ADDRMASK	{ $$ = $1; }
    305 	| ADDR			{ $$.addr = $1; $$.mask = 0; }
    306 	;
    307 %%
    308 /*VARARGS1*/
    309 static void fatal(fmt, va_alist)
    310 char *fmt;
    311 va_dcl
    312 {
    313 	va_list ap;
    314 	char buf[200];
    315 
    316 	va_start(ap);
    317 	vsprintf(buf, fmt, ap);
    318 	va_end(ap);
    319 
    320 	log(LOG_ERR,0,"%s: %s near line %d", configfilename, buf, lineno);
    321 }
    322 
    323 /*VARARGS1*/
    324 static void warn(fmt, va_alist)
    325 char *fmt;
    326 va_dcl
    327 {
    328 	va_list ap;
    329 	char buf[200];
    330 
    331 	va_start(ap);
    332 	vsprintf(buf, fmt, ap);
    333 	va_end(ap);
    334 
    335 	log(LOG_WARNING,0,"%s: %s near line %d", configfilename, buf, lineno);
    336 }
    337 
    338 void yyerror(s)
    339 char *s;
    340 {
    341 	log(LOG_ERR, 0, "%s: %s near line %d", configfilename, s, lineno);
    342 }
    343 
    344 char *next_word()
    345 {
    346 	static char buf[1024];
    347 	static char *p=NULL;
    348 	extern FILE *f;
    349 	char *q;
    350 
    351 	while (1) {
    352 	    if (!p || !*p) {
    353 		lineno++;
    354 		if (fgets(buf, sizeof(buf), f) == NULL)
    355 		    return NULL;
    356 		p = buf;
    357 	    }
    358 	    while (*p && (*p == ' ' || *p == '\t'))	/* skip whitespace */
    359 		p++;
    360 	    if (*p == '#') {
    361 		p = NULL;		/* skip comments */
    362 		continue;
    363 	    }
    364 	    q = p;
    365 	    while (*p && *p != ' ' && *p != '\t' && *p != '\n')
    366 		p++;		/* find next whitespace */
    367 	    *p++ = '\0';	/* null-terminate string */
    368 
    369 	    if (!*q) {
    370 		p = NULL;
    371 		continue;	/* if 0-length string, read another line */
    372 	    }
    373 
    374 	    return q;
    375 	}
    376 }
    377 
    378 int yylex()
    379 {
    380 	int n;
    381 	u_int32_t addr;
    382 	char *q;
    383 
    384 	if ((q = next_word()) == NULL) {
    385 		return 0;
    386 	}
    387 
    388 	if (!strcmp(q,"cache_lifetime"))
    389 		return CACHE_LIFETIME;
    390 	if (!strcmp(q,"pruning"))
    391 		return PRUNING;
    392 	if (!strcmp(q,"phyint"))
    393 		return PHYINT;
    394 	if (!strcmp(q,"tunnel"))
    395 		return TUNNEL;
    396 	if (!strcmp(q,"disable"))
    397 		return DISABLE;
    398 	if (!strcmp(q,"metric"))
    399 		return METRIC;
    400 	if (!strcmp(q,"threshold"))
    401 		return THRESHOLD;
    402 	if (!strcmp(q,"rate_limit"))
    403 		return RATE_LIMIT;
    404 	if (!strcmp(q,"srcrt") || !strcmp(q,"sourceroute"))
    405 		return SRCRT;
    406 	if (!strcmp(q,"boundary"))
    407 		return BOUNDARY;
    408 	if (!strcmp(q,"netmask"))
    409 		return NETMASK;
    410 	if (!strcmp(q,"name"))
    411 		return NAME;
    412 	if (!strcmp(q,"altnet"))
    413 		return ALTNET;
    414 	if (!strcmp(q,"on") || !strcmp(q,"yes")) {
    415 		yylval.num = 1;
    416 		return BOOLEAN;
    417 	}
    418 	if (!strcmp(q,"off") || !strcmp(q,"no")) {
    419 		yylval.num = 0;
    420 		return BOOLEAN;
    421 	}
    422 	if (sscanf(q,"%[.0-9]/%d%c",s1,&n,s2) == 2) {
    423 		if ((addr = inet_parse(s1)) != 0xffffffff) {
    424 			yylval.addrmask.mask = n;
    425 			yylval.addrmask.addr = addr;
    426 			return ADDRMASK;
    427 		}
    428 		/* fall through to returning STRING */
    429 	}
    430 	if (sscanf(q,"%[.0-9]%c",s1,s2) == 1) {
    431 		if ((addr = inet_parse(s1)) != 0xffffffff &&
    432 		    inet_valid_host(addr)) {
    433 			yylval.addr = addr;
    434 			return ADDR;
    435 		}
    436 	}
    437 	if (sscanf(q,"0x%8x%c",&n,s1) == 1) {
    438 		yylval.addr = n;
    439 		return ADDR;
    440 	}
    441 	if (sscanf(q,"%d%c",&n,s1) == 1) {
    442 		yylval.num = n;
    443 		return NUMBER;
    444 	}
    445 	yylval.ptr = q;
    446 	return STRING;
    447 }
    448 
    449 void config_vifs_from_file()
    450 {
    451 	extern FILE *f;
    452 
    453 	order = 0;
    454 	numbounds = 0;
    455 	lineno = 0;
    456 
    457 	if ((f = fopen(configfilename, "r")) == NULL) {
    458 	    if (errno != ENOENT)
    459 		log(LOG_ERR, errno, "can't open %s", configfilename);
    460 	    return;
    461 	}
    462 
    463 	ifc.ifc_buf = (char *)ifbuf;
    464 	ifc.ifc_len = sizeof(ifbuf);
    465 	if (ioctl(udp_socket, SIOCGIFCONF, (char *)&ifc) < 0)
    466 	    log(LOG_ERR, errno, "ioctl SIOCGIFCONF");
    467 
    468 	yyparse();
    469 
    470 	close(f);
    471 }
    472 
    473 static u_int32_t
    474 valid_if(s)
    475 char *s;
    476 {
    477 	register vifi_t vifi;
    478 	register struct uvif *v;
    479 
    480 	for (vifi=0, v=uvifs; vifi<numvifs; vifi++, v++)
    481 	    if (!strcmp(v->uv_name, s))
    482 		return v->uv_lcl_addr;
    483 
    484 	return 0;
    485 }
    486 
    487 static struct ifreq *
    488 ifconfaddr(ifcp, a)
    489     struct ifconf *ifcp;
    490     u_int32_t a;
    491 {
    492     int n;
    493     struct ifreq *ifrp = (struct ifreq *)ifcp->ifc_buf;
    494     struct ifreq *ifend = (struct ifreq *)((char *)ifrp + ifcp->ifc_len);
    495 
    496     while (ifrp < ifend) {
    497 	    if (ifrp->ifr_addr.sa_family == AF_INET &&
    498 		((struct sockaddr_in *)&ifrp->ifr_addr)->sin_addr.s_addr == a)
    499 		    return (ifrp);
    500 #if (defined(BSD) && (BSD >= 199006))
    501 		n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
    502 		if (n < sizeof(*ifrp))
    503 			++ifrp;
    504 		else
    505 			ifrp = (struct ifreq *)((char *)ifrp + n);
    506 #else
    507 		++ifrp;
    508 #endif
    509     }
    510     return (0);
    511 }
    512