xref: /openbsd-src/usr.sbin/ospf6ctl/parser.c (revision 50027fe110c3c362514cbbf1128910104a00203e)
1 /*	$OpenBSD: parser.c,v 1.7 2009/11/02 20:25:27 claudio Exp $ */
2 
3 /*
4  * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
5  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <netdb.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 #include "ospf6d.h"
33 
34 #include "parser.h"
35 
36 enum token_type {
37 	NOTOKEN,
38 	ENDTOKEN,
39 	KEYWORD,
40 	ADDRESS,
41 	FLAG,
42 	PREFIX,
43 	IFNAME
44 };
45 
46 struct token {
47 	enum token_type		 type;
48 	const char		*keyword;
49 	int			 value;
50 	const struct token	*next;
51 };
52 
53 static const struct token t_main[];
54 static const struct token t_fib[];
55 static const struct token t_show[];
56 static const struct token t_show_iface[];
57 static const struct token t_show_db[];
58 static const struct token t_show_area[];
59 static const struct token t_show_nbr[];
60 static const struct token t_show_rib[];
61 static const struct token t_show_fib[];
62 static const struct token t_log[];
63 
64 static const struct token t_main[] = {
65 	{KEYWORD,	"reload",	RELOAD,		NULL},
66 	{KEYWORD,	"fib",		FIB,		t_fib},
67 	{KEYWORD,	"show",		SHOW,		t_show},
68 	{KEYWORD,	"log",		NONE,		t_log},
69 	{ENDTOKEN,	"",		NONE,		NULL}
70 };
71 
72 static const struct token t_fib[] = {
73 	{ KEYWORD,	"couple",	FIB_COUPLE,	NULL},
74 	{ KEYWORD,	"decouple",	FIB_DECOUPLE,	NULL},
75 	{ ENDTOKEN,	"",		NONE,		NULL}
76 };
77 
78 static const struct token t_show[] = {
79 	{NOTOKEN,	"",		NONE,		NULL},
80 	{KEYWORD,	"interfaces",	SHOW_IFACE,	t_show_iface},
81 	{KEYWORD,	"database",	SHOW_DB,	t_show_db},
82 	{KEYWORD,	"neighbor",	SHOW_NBR,	t_show_nbr},
83 	{KEYWORD,	"rib",		SHOW_RIB,	t_show_rib},
84 	{KEYWORD,	"fib",		SHOW_FIB,	t_show_fib},
85 	{KEYWORD,	"summary",	SHOW_SUM,	NULL},
86 	{ENDTOKEN,	"",		NONE,		NULL}
87 };
88 
89 static const struct token t_show_iface[] = {
90 	{NOTOKEN,	"",		NONE,			NULL},
91 	{KEYWORD,	"detail",	SHOW_IFACE_DTAIL,	NULL},
92 	{IFNAME,	"",		SHOW_IFACE_DTAIL,	NULL},
93 	{ENDTOKEN,	"",		NONE,			NULL}
94 };
95 
96 static const struct token t_show_db[] = {
97 	{NOTOKEN,	"",			NONE,		NULL},
98 	{KEYWORD,	"area",			SHOW_DBBYAREA,	t_show_area},
99 	{KEYWORD,	"asbr",			SHOW_DBASBR,	NULL},
100 	{KEYWORD,	"external",		SHOW_DBEXT,	NULL},
101 	{KEYWORD,	"link",			SHOW_DBLINK,	NULL},
102 	{KEYWORD,	"network",		SHOW_DBNET,	NULL},
103 	{KEYWORD,	"router",		SHOW_DBRTR,	NULL},
104 	{KEYWORD,	"intra",		SHOW_DBINTRA,	NULL},
105 	{KEYWORD,	"self-originated",	SHOW_DBSELF,	NULL},
106 	{KEYWORD,	"summary",		SHOW_DBSUM,	NULL},
107 	{ENDTOKEN,	"",			NONE,		NULL}
108 };
109 
110 static const struct token t_show_area[] = {
111 	{ADDRESS,	"",		NONE,		NULL},
112 	{ENDTOKEN,	"",		NONE,		NULL}
113 };
114 
115 static const struct token t_show_nbr[] = {
116 	{NOTOKEN,	"",		NONE,		NULL},
117 	{KEYWORD,	"detail",	SHOW_NBR_DTAIL,	NULL},
118 	{ENDTOKEN,	"",		NONE,		NULL}
119 };
120 
121 static const struct token t_show_rib[] = {
122 	{NOTOKEN,	"",		NONE,		NULL},
123 	{KEYWORD,	"detail",	SHOW_RIB_DTAIL,	NULL},
124 	{ENDTOKEN,	"",		NONE,		NULL}
125 };
126 
127 static const struct token t_show_fib[] = {
128 	{NOTOKEN,	"",		NONE,			NULL},
129 	{FLAG,		"connected",	F_CONNECTED,		t_show_fib},
130 	{FLAG,		"static",	F_STATIC,		t_show_fib},
131 	{FLAG,		"ospf",		F_OSPFD_INSERTED,	t_show_fib},
132 	{ADDRESS,	"",		NONE,			NULL},
133 	{ENDTOKEN,	"",		NONE,			NULL}
134 };
135 
136 static const struct token t_log[] = {
137 	{KEYWORD,	"verbose",	LOG_VERBOSE,		NULL},
138 	{KEYWORD,	"brief",	LOG_BRIEF,		NULL},
139 	{ENDTOKEN,	"",		NONE,			NULL}
140 };
141 
142 static struct parse_result	res;
143 
144 struct parse_result *
145 parse(int argc, char *argv[])
146 {
147 	const struct token	*table = t_main;
148 	const struct token	*match;
149 
150 	bzero(&res, sizeof(res));
151 
152 	while (argc >= 0) {
153 		if ((match = match_token(argv[0], table)) == NULL) {
154 			fprintf(stderr, "valid commands/args:\n");
155 			show_valid_args(table);
156 			return (NULL);
157 		}
158 
159 		argc--;
160 		argv++;
161 
162 		if (match->type == NOTOKEN || match->next == NULL)
163 			break;
164 
165 		table = match->next;
166 	}
167 
168 	if (argc > 0) {
169 		fprintf(stderr, "superfluous argument: %s\n", argv[0]);
170 		return (NULL);
171 	}
172 
173 	return (&res);
174 }
175 
176 const struct token *
177 match_token(const char *word, const struct token table[])
178 {
179 	u_int			 i, match;
180 	const struct token	*t = NULL;
181 
182 	match = 0;
183 
184 	for (i = 0; table[i].type != ENDTOKEN; i++) {
185 		switch (table[i].type) {
186 		case NOTOKEN:
187 			if (word == NULL || strlen(word) == 0) {
188 				match++;
189 				t = &table[i];
190 			}
191 			break;
192 		case KEYWORD:
193 			if (word != NULL && strncmp(word, table[i].keyword,
194 			    strlen(word)) == 0) {
195 				match++;
196 				t = &table[i];
197 				if (t->value)
198 					res.action = t->value;
199 			}
200 			break;
201 		case FLAG:
202 			if (word != NULL && strncmp(word, table[i].keyword,
203 			    strlen(word)) == 0) {
204 				match++;
205 				t = &table[i];
206 				res.flags |= t->value;
207 			}
208 			break;
209 		case ADDRESS:
210 			if (parse_addr(word, &res.addr)) {
211 				match++;
212 				t = &table[i];
213 				if (t->value)
214 					res.action = t->value;
215 			}
216 			break;
217 		case PREFIX:
218 			if (parse_prefix(word, &res.addr, &res.prefixlen)) {
219 				match++;
220 				t = &table[i];
221 				if (t->value)
222 					res.action = t->value;
223 			}
224 			break;
225 		case IFNAME:
226 			if (!match && word != NULL && strlen(word) > 0) {
227 				if (strlcpy(res.ifname, word,
228 				    sizeof(res.ifname)) >=
229 				    sizeof(res.ifname))
230 					err(1, "interface name too long");
231 				match++;
232 				t = &table[i];
233 				if (t->value)
234 					res.action = t->value;
235 			}
236 			break;
237 
238 		case ENDTOKEN:
239 			break;
240 		}
241 	}
242 
243 	if (match != 1) {
244 		if (word == NULL)
245 			fprintf(stderr, "missing argument:\n");
246 		else if (match > 1)
247 			fprintf(stderr, "ambiguous argument: %s\n", word);
248 		else if (match < 1)
249 			fprintf(stderr, "unknown argument: %s\n", word);
250 		return (NULL);
251 	}
252 
253 	return (t);
254 }
255 
256 void
257 show_valid_args(const struct token table[])
258 {
259 	int	i;
260 
261 	for (i = 0; table[i].type != ENDTOKEN; i++) {
262 		switch (table[i].type) {
263 		case NOTOKEN:
264 			fprintf(stderr, "  <cr>\n");
265 			break;
266 		case KEYWORD:
267 		case FLAG:
268 			fprintf(stderr, "  %s\n", table[i].keyword);
269 			break;
270 		case ADDRESS:
271 			fprintf(stderr, "  <address>\n");
272 			break;
273 		case PREFIX:
274 			fprintf(stderr, "  <address>[/<len>]\n");
275 			break;
276 		case IFNAME:
277 			fprintf(stderr, "  <interface>\n");
278 			break;
279 		case ENDTOKEN:
280 			break;
281 		}
282 	}
283 }
284 
285 /* XXX shared with parse.y should be merged */
286 int
287 parse_addr(const char *word, struct in6_addr *addr)
288 {
289 	struct addrinfo	hints, *r;
290 
291 	if (word == NULL)
292 		return (0);
293 
294 	bzero(addr, sizeof(struct in6_addr));
295 	bzero(&hints, sizeof(hints));
296 	hints.ai_family = AF_INET6;
297 	hints.ai_socktype = SOCK_DGRAM; /*dummy*/
298 	hints.ai_flags = AI_NUMERICHOST;
299 	if (getaddrinfo(word, "0", &hints, &r) == 0) {
300 		*addr = ((struct sockaddr_in6 *)r->ai_addr)->sin6_addr;
301 		/* XXX address scope !!! */
302 		/* ((struct sockaddr_in6 *)r->ai_addr)->sin6_scope_id */
303 		freeaddrinfo(r);
304 		return (1);
305 	}
306 	return (0);
307 }
308 
309 /* XXX shared with parse.y should be merged */
310 int
311 parse_prefix(const char *word, struct in6_addr *addr, u_int8_t *prefixlen)
312 {
313 	char		*p, *ps;
314 	const char	*errstr;
315 	int		 mask;
316 
317 	if (word == NULL)
318 		return (0);
319 
320 	if ((p = strrchr(word, '/')) != NULL) {
321 		mask = strtonum(p + 1, 0, 128, &errstr);
322 		if (errstr)
323 			errx(1, "invalid netmask: %s", errstr);
324 
325 		if ((ps = malloc(strlen(word) - strlen(p) + 1)) == NULL)
326 			err(1, "parse_prefix: malloc");
327 		strlcpy(ps, word, strlen(word) - strlen(p) + 1);
328 
329 		if (parse_addr(ps, addr) == 0) {
330 			free(ps);
331 			return (0);
332 		}
333 
334 		inet6applymask(addr, addr, mask);
335 		*prefixlen = mask;
336 		return (1);
337 	}
338 	*prefixlen = 128;
339 	return (parse_addr(word, addr));
340 }
341 
342 /* XXX prototype defined in ospfd.h and shared with the kroute.c version */
343 u_int8_t
344 mask2prefixlen(struct sockaddr_in6 *sa_in6)
345 {
346 	u_int8_t	l = 0, i, len;
347 
348 	/*
349 	 * sin6_len is the size of the sockaddr so substract the offset of
350 	 * the possibly truncated sin6_addr struct.
351 	 */
352 	len = sa_in6->sin6_len -
353 	    (u_int8_t)(&((struct sockaddr_in6 *)NULL)->sin6_addr);
354 	for (i = 0; i < len; i++) {
355 		/* this "beauty" is adopted from sbin/route/show.c ... */
356 		switch (sa_in6->sin6_addr.s6_addr[i]) {
357 		case 0xff:
358 			l += 8;
359 			break;
360 		case 0xfe:
361 			l += 7;
362 			return (l);
363 		case 0xfc:
364 			l += 6;
365 			return (l);
366 		case 0xf8:
367 			l += 5;
368 			return (l);
369 		case 0xf0:
370 			l += 4;
371 			return (l);
372 		case 0xe0:
373 			l += 3;
374 			return (l);
375 		case 0xc0:
376 			l += 2;
377 			return (l);
378 		case 0x80:
379 			l += 1;
380 			return (l);
381 		case 0x00:
382 			return (l);
383 		default:
384 			errx(1, "non continguous inet6 netmask");
385 		}
386 	}
387 
388 	return (l);
389 }
390 
391 /* XXX local copy from kroute.c, should go to shared file */
392 struct in6_addr *
393 prefixlen2mask(u_int8_t prefixlen)
394 {
395 	static struct in6_addr	mask;
396 	int			i;
397 
398 	bzero(&mask, sizeof(mask));
399 	for (i = 0; i < prefixlen / 8; i++)
400 		mask.s6_addr[i] = 0xff;
401 	i = prefixlen % 8;
402 	if (i)
403 		mask.s6_addr[prefixlen / 8] = 0xff00 >> i;
404 
405 	return (&mask);
406 }
407 
408 /* XXX local copy from kroute.c, should go to shared file */
409 void
410 inet6applymask(struct in6_addr *dest, const struct in6_addr *src, int prefixlen)
411 {
412 	struct in6_addr	mask;
413 	int		i;
414 
415 	bzero(&mask, sizeof(mask));
416 	for (i = 0; i < prefixlen / 8; i++)
417 		mask.s6_addr[i] = 0xff;
418 	i = prefixlen % 8;
419 	if (i)
420 		mask.s6_addr[prefixlen / 8] = 0xff00 >> i;
421 
422 	for (i = 0; i < 16; i++)
423 		dest->s6_addr[i] = src->s6_addr[i] & mask.s6_addr[i];
424 }
425