xref: /openbsd-src/usr.sbin/bgpctl/parser.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /*	$OpenBSD: parser.c,v 1.88 2018/12/19 15:27:29 claudio Exp $ */
2 
3 /*
4  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5  * Copyright (c) 2016 Job Snijders <job@instituut.net>
6  * Copyright (c) 2016 Peter Hessler <phessler@openbsd.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include <sys/types.h>
22 
23 #include <err.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <netdb.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 
33 #include "parser.h"
34 #include "irrfilter.h"
35 
36 enum token_type {
37 	NOTOKEN,
38 	ENDTOKEN,
39 	KEYWORD,
40 	ADDRESS,
41 	PEERADDRESS,
42 	FLAG,
43 	ASNUM,
44 	ASTYPE,
45 	PREFIX,
46 	PEERDESC,
47 	RIBNAME,
48 	SHUTDOWN_COMMUNICATION,
49 	COMMUNITY,
50 	EXTCOMMUNITY,
51 	EXTCOM_SUBTYPE,
52 	LARGE_COMMUNITY,
53 	LOCALPREF,
54 	MED,
55 	NEXTHOP,
56 	PFTABLE,
57 	PREPNBR,
58 	PREPSELF,
59 	WEIGHT,
60 	FAMILY,
61 	GETOPT,
62 	RTABLE,
63 	FILENAME,
64 	BULK
65 };
66 
67 enum getopts {
68 	GETOPT_NONE,
69 	GETOPT_IRRFILTER
70 };
71 
72 struct token {
73 	enum token_type		 type;
74 	const char		*keyword;
75 	int			 value;
76 	const struct token	*next;
77 };
78 
79 static const struct token t_main[];
80 static const struct token t_show[];
81 static const struct token t_show_summary[];
82 static const struct token t_show_fib[];
83 static const struct token t_show_rib[];
84 static const struct token t_show_ovs[];
85 static const struct token t_show_mrt[];
86 static const struct token t_show_mrt_file[];
87 static const struct token t_show_rib_neigh[];
88 static const struct token t_show_mrt_neigh[];
89 static const struct token t_show_rib_rib[];
90 static const struct token t_show_neighbor[];
91 static const struct token t_show_neighbor_modifiers[];
92 static const struct token t_fib[];
93 static const struct token t_neighbor[];
94 static const struct token t_neighbor_modifiers[];
95 static const struct token t_show_rib_as[];
96 static const struct token t_show_mrt_as[];
97 static const struct token t_show_prefix[];
98 static const struct token t_show_ip[];
99 static const struct token t_show_community[];
100 static const struct token t_show_extcommunity[];
101 static const struct token t_show_ext_subtype[];
102 static const struct token t_show_largecommunity[];
103 static const struct token t_network[];
104 static const struct token t_network_show[];
105 static const struct token t_prefix[];
106 static const struct token t_set[];
107 static const struct token t_community[];
108 static const struct token t_extcommunity[];
109 static const struct token t_ext_subtype[];
110 static const struct token t_largecommunity[];
111 static const struct token t_localpref[];
112 static const struct token t_med[];
113 static const struct token t_nexthop[];
114 static const struct token t_pftable[];
115 static const struct token t_prepnbr[];
116 static const struct token t_prepself[];
117 static const struct token t_weight[];
118 static const struct token t_irrfilter[];
119 static const struct token t_irrfilter_opts[];
120 static const struct token t_log[];
121 static const struct token t_fib_table[];
122 static const struct token t_show_fib_table[];
123 
124 static const struct token t_main[] = {
125 	{ KEYWORD,	"reload",	RELOAD,		NULL},
126 	{ KEYWORD,	"show",		SHOW,		t_show},
127 	{ KEYWORD,	"fib",		FIB,		t_fib},
128 	{ KEYWORD,	"neighbor",	NEIGHBOR,	t_neighbor},
129 	{ KEYWORD,	"network",	NONE,		t_network},
130 	{ KEYWORD,	"irrfilter",	IRRFILTER,	t_irrfilter},
131 	{ KEYWORD,	"log",		NONE,		t_log},
132 	{ ENDTOKEN,	"",		NONE,		NULL}
133 };
134 
135 static const struct token t_show[] = {
136 	{ NOTOKEN,	"",		NONE,		NULL},
137 	{ KEYWORD,	"fib",		SHOW_FIB,	t_show_fib},
138 	{ KEYWORD,	"interfaces",	SHOW_INTERFACE,	NULL},
139 	{ KEYWORD,	"neighbor",	SHOW_NEIGHBOR,	t_show_neighbor},
140 	{ KEYWORD,	"network",	NETWORK_SHOW,	t_network_show},
141 	{ KEYWORD,	"nexthop",	SHOW_NEXTHOP,	NULL},
142 	{ KEYWORD,	"rib",		SHOW_RIB,	t_show_rib},
143 	{ KEYWORD,	"tables",	SHOW_FIB_TABLES, NULL},
144 	{ KEYWORD,	"ip",		NONE,		t_show_ip},
145 	{ KEYWORD,	"summary",	SHOW_SUMMARY,	t_show_summary},
146 	{ KEYWORD,	"mrt",		SHOW_MRT,	t_show_mrt},
147 	{ ENDTOKEN,	"",		NONE,		NULL}
148 };
149 
150 static const struct token t_show_summary[] = {
151 	{ NOTOKEN,	"",		NONE,			NULL},
152 	{ KEYWORD,	"terse",	SHOW_SUMMARY_TERSE,	NULL},
153 	{ ENDTOKEN,	"",		NONE,			NULL}
154 };
155 
156 static const struct token t_show_fib[] = {
157 	{ NOTOKEN,	"",		NONE,		 NULL},
158 	{ FLAG,		"connected",	F_CONNECTED,	 t_show_fib},
159 	{ FLAG,		"static",	F_STATIC,	 t_show_fib},
160 	{ FLAG,		"bgp",		F_BGPD_INSERTED, t_show_fib},
161 	{ FLAG,		"nexthop",	F_NEXTHOP,	 t_show_fib},
162 	{ KEYWORD,	"table",	NONE,		 t_show_fib_table},
163 	{ FAMILY,	"",		NONE,		 t_show_fib},
164 	{ ADDRESS,	"",		NONE,		 NULL},
165 	{ ENDTOKEN,	"",		NONE,		 NULL}
166 };
167 
168 static const struct token t_show_rib[] = {
169 	{ NOTOKEN,	"",		NONE,		NULL},
170 	{ ASTYPE,	"as",		AS_ALL,		t_show_rib_as},
171 	{ ASTYPE,	"source-as",	AS_SOURCE,	t_show_rib_as},
172 	{ ASTYPE,	"transit-as",	AS_TRANSIT,	t_show_rib_as},
173 	{ ASTYPE,	"peer-as",	AS_PEER,	t_show_rib_as},
174 	{ ASTYPE,	"empty-as",	AS_EMPTY,	t_show_rib},
175 	{ KEYWORD,	"community",	NONE,		t_show_community},
176 	{ KEYWORD,	"ext-community", NONE,		t_show_extcommunity},
177 	{ KEYWORD,	"large-community", NONE,	t_show_largecommunity},
178 	{ FLAG,		"best",		F_CTL_ACTIVE,	t_show_rib},
179 	{ FLAG,		"selected",	F_CTL_ACTIVE,	t_show_rib},
180 	{ FLAG,		"detail",	F_CTL_DETAIL,	t_show_rib},
181 	{ FLAG,		"error",	F_CTL_INVALID,	t_show_rib},
182 	{ FLAG,		"ssv"	,	F_CTL_SSV,	t_show_rib},
183 	{ FLAG,		"in",		F_CTL_ADJ_IN,	t_show_rib},
184 	{ FLAG,		"out",		F_CTL_ADJ_OUT,	t_show_rib},
185 	{ KEYWORD,	"neighbor",	NONE,		t_show_rib_neigh},
186 	{ KEYWORD,	"table",	NONE,		t_show_rib_rib},
187 	{ KEYWORD,	"summary",	SHOW_SUMMARY,	t_show_summary},
188 	{ KEYWORD,	"memory",	SHOW_RIB_MEM,	NULL},
189 	{ KEYWORD,	"ovs",		NONE,		t_show_ovs},
190 	{ FAMILY,	"",		NONE,		t_show_rib},
191 	{ PREFIX,	"",		NONE,		t_show_prefix},
192 	{ ENDTOKEN,	"",		NONE,		NULL}
193 };
194 
195 static const struct token t_show_ovs[] = {
196 	{ FLAG,		"valid"	,	F_CTL_OVS_VALID,	t_show_rib},
197 	{ FLAG,		"invalid",	F_CTL_OVS_INVALID,	t_show_rib},
198 	{ FLAG,		"not-found",	F_CTL_OVS_NOTFOUND,	t_show_rib},
199 	{ ENDTOKEN,	"",		NONE,		NULL}
200 };
201 
202 static const struct token t_show_mrt[] = {
203 	{ NOTOKEN,	"",		NONE,		NULL},
204 	{ ASTYPE,	"as",		AS_ALL,		t_show_mrt_as},
205 	{ ASTYPE,	"source-as",	AS_SOURCE,	t_show_mrt_as},
206 	{ ASTYPE,	"transit-as",	AS_TRANSIT,	t_show_mrt_as},
207 	{ ASTYPE,	"peer-as",	AS_PEER,	t_show_mrt_as},
208 	{ ASTYPE,	"empty-as",	AS_EMPTY,	t_show_mrt},
209 	{ FLAG,		"detail",	F_CTL_DETAIL,	t_show_mrt},
210 	{ FLAG,		"ssv"	,	F_CTL_SSV,	t_show_mrt},
211 	{ KEYWORD,	"neighbor",	NONE,		t_show_mrt_neigh},
212 	{ KEYWORD,	"file",		NONE,		t_show_mrt_file},
213 	{ FAMILY,	"",		NONE,		t_show_mrt},
214 	{ PREFIX,	"",		NONE,		t_show_prefix},
215 	{ ENDTOKEN,	"",		NONE,		NULL}
216 };
217 
218 static const struct token t_show_mrt_file[] = {
219 	{ FILENAME,	"",		NONE,		t_show_mrt},
220 	{ ENDTOKEN,	"",		NONE,	NULL}
221 };
222 
223 static const struct token t_show_rib_neigh[] = {
224 	{ PEERADDRESS,	"",		NONE,	t_show_rib},
225 	{ PEERDESC,	"",		NONE,	t_show_rib},
226 	{ ENDTOKEN,	"",		NONE,	NULL}
227 };
228 
229 static const struct token t_show_mrt_neigh[] = {
230 	{ PEERADDRESS,	"",		NONE,	t_show_mrt},
231 	{ ENDTOKEN,	"",		NONE,	NULL}
232 };
233 
234 static const struct token t_show_rib_rib[] = {
235 	{ RIBNAME,	"",		NONE,	t_show_rib},
236 	{ ENDTOKEN,	"",		NONE,	NULL}
237 };
238 
239 static const struct token t_show_neighbor[] = {
240 	{ NOTOKEN,	"",		NONE,	NULL},
241 	{ PEERADDRESS,	"",		NONE,	t_show_neighbor_modifiers},
242 	{ PEERDESC,	"",		NONE,	t_show_neighbor_modifiers},
243 	{ ENDTOKEN,	"",		NONE,	NULL}
244 };
245 
246 static const struct token t_show_neighbor_modifiers[] = {
247 	{ NOTOKEN,	"",		NONE,			NULL},
248 	{ KEYWORD,	"timers",	SHOW_NEIGHBOR_TIMERS,	NULL},
249 	{ KEYWORD,	"messages",	SHOW_NEIGHBOR,		NULL},
250 	{ KEYWORD,	"terse",	SHOW_NEIGHBOR_TERSE,	NULL},
251 	{ ENDTOKEN,	"",		NONE,			NULL}
252 };
253 
254 static const struct token t_fib[] = {
255 	{ KEYWORD,	"couple",	FIB_COUPLE,	NULL},
256 	{ KEYWORD,	"decouple",	FIB_DECOUPLE,	NULL},
257 	{ KEYWORD,	"table",	NONE,		t_fib_table},
258 	{ ENDTOKEN,	"",		NONE,		NULL}
259 };
260 
261 static const struct token t_neighbor[] = {
262 	{ PEERADDRESS,	"",		NONE,		t_neighbor_modifiers},
263 	{ PEERDESC,	"",		NONE,		t_neighbor_modifiers},
264 	{ ENDTOKEN,	"",		NONE,		NULL}
265 };
266 
267 static const struct token t_nei_mod_shutc[] = {
268 	{ NOTOKEN,	"",		NONE,		NULL},
269 	{ SHUTDOWN_COMMUNICATION, "",	NONE,		NULL},
270 	{ ENDTOKEN,	"",		NONE,		NULL}
271 };
272 
273 static const struct token t_neighbor_modifiers[] = {
274 	{ KEYWORD,	"up",		NEIGHBOR_UP,		NULL},
275 	{ KEYWORD,	"down",		NEIGHBOR_DOWN,		t_nei_mod_shutc},
276 	{ KEYWORD,	"clear",	NEIGHBOR_CLEAR,		t_nei_mod_shutc},
277 	{ KEYWORD,	"refresh",	NEIGHBOR_RREFRESH,	NULL},
278 	{ KEYWORD,	"destroy",	NEIGHBOR_DESTROY,	NULL},
279 	{ ENDTOKEN,	"",		NONE,			NULL}
280 };
281 
282 static const struct token t_show_rib_as[] = {
283 	{ ASNUM,	"",		NONE,		t_show_rib},
284 	{ ENDTOKEN,	"",		NONE,		NULL}
285 };
286 
287 static const struct token t_show_mrt_as[] = {
288 	{ ASNUM,	"",		NONE,		t_show_mrt},
289 	{ ENDTOKEN,	"",		NONE,		NULL}
290 };
291 
292 static const struct token t_show_prefix[] = {
293 	{ NOTOKEN,	"",		NONE,		NULL},
294 	{ FLAG,		"all",		F_LONGER,	NULL},
295 	{ FLAG,		"longer-prefixes", F_LONGER,	NULL},
296 	{ ENDTOKEN,	"",		NONE,		NULL}
297 };
298 
299 static const struct token t_show_ip[] = {
300 	{ KEYWORD,	"bgp",		SHOW_RIB,	t_show_rib},
301 	{ ENDTOKEN,	"",		NONE,		NULL}
302 };
303 
304 static const struct token t_show_community[] = {
305 	{ COMMUNITY,	"",		NONE,		t_show_rib},
306 	{ ENDTOKEN,	"",		NONE,		NULL}
307 };
308 
309 static const struct token t_show_extcommunity[] = {
310 	{ EXTCOM_SUBTYPE,	"bdc",		NONE,	t_show_ext_subtype},
311 	{ EXTCOM_SUBTYPE,	"defgw",	NONE,	t_show_ext_subtype},
312 	{ EXTCOM_SUBTYPE,	"esi-lab",	NONE,	t_show_ext_subtype},
313 	{ EXTCOM_SUBTYPE,	"esi-rt",	NONE,	t_show_ext_subtype},
314 	{ EXTCOM_SUBTYPE,	"l2vid",	NONE,	t_show_ext_subtype},
315 	{ EXTCOM_SUBTYPE,	"mac-mob",	NONE,	t_show_ext_subtype},
316 	{ EXTCOM_SUBTYPE,	"odi",		NONE,	t_show_ext_subtype},
317 	{ EXTCOM_SUBTYPE,	"ort",		NONE,	t_show_ext_subtype},
318 	{ EXTCOM_SUBTYPE,	"ori",		NONE,	t_show_ext_subtype},
319 	{ EXTCOM_SUBTYPE,	"ovs",		NONE,	t_show_ext_subtype},
320 	{ EXTCOM_SUBTYPE,	"rt",		NONE,	t_show_ext_subtype},
321 	{ EXTCOM_SUBTYPE,	"soo",		NONE,	t_show_ext_subtype},
322 	{ EXTCOM_SUBTYPE,	"srcas",	NONE,	t_show_ext_subtype},
323 	{ EXTCOM_SUBTYPE,	"vrfri",	NONE,	t_show_ext_subtype},
324 	{ ENDTOKEN,	"",	NONE,	NULL}
325 };
326 
327 static const struct token t_show_ext_subtype[] = {
328 	{ EXTCOMMUNITY,	"",	NONE,	t_show_rib},
329 	{ ENDTOKEN,	"",	NONE,	NULL}
330 };
331 
332 static const struct token t_show_largecommunity[] = {
333 	{ LARGE_COMMUNITY,	"",	NONE,		t_show_rib},
334 	{ ENDTOKEN,	"",		NONE,		NULL}
335 };
336 
337 static const struct token t_network[] = {
338 	{ KEYWORD,	"add",		NETWORK_ADD,	t_prefix},
339 	{ KEYWORD,	"delete",	NETWORK_REMOVE,	t_prefix},
340 	{ KEYWORD,	"flush",	NETWORK_FLUSH,	NULL},
341 	{ KEYWORD,	"show",		NETWORK_SHOW,	t_network_show},
342 	{ KEYWORD,	"mrt",		NETWORK_MRT,	t_show_mrt},
343 	{ KEYWORD,	"bulk",		NETWORK_BULK_ADD,	t_set},
344 	{ ENDTOKEN,	"",		NONE,		NULL}
345 };
346 
347 static const struct token t_prefix[] = {
348 	{ PREFIX,	"",		NONE,		t_set},
349 	{ ENDTOKEN,	"",		NONE,		NULL}
350 };
351 
352 static const struct token t_network_show[] = {
353 	{ NOTOKEN,	"",		NONE,			NULL},
354 	{ FAMILY,	"",		NONE,			NULL},
355 	{ ENDTOKEN,	"",		NONE,			NULL}
356 };
357 
358 static const struct token t_set[] = {
359 	{ NOTOKEN,	"",			NONE,	NULL},
360 	{ KEYWORD,	"community",		NONE,	t_community},
361 	{ KEYWORD,	"ext-community",	NONE,	t_extcommunity},
362 	{ KEYWORD,	"large-community",	NONE,	t_largecommunity},
363 	{ KEYWORD,	"localpref",		NONE,	t_localpref},
364 	{ KEYWORD,	"med",			NONE,	t_med},
365 	{ KEYWORD,	"metric",		NONE,	t_med},
366 	{ KEYWORD,	"nexthop",		NONE,	t_nexthop},
367 	{ KEYWORD,	"pftable",		NONE,	t_pftable},
368 	{ KEYWORD,	"prepend-neighbor",	NONE,	t_prepnbr},
369 	{ KEYWORD,	"prepend-self",		NONE,	t_prepself},
370 	{ KEYWORD,	"weight",		NONE,	t_weight},
371 	{ KEYWORD,	"add",			NETWORK_BULK_ADD,	NULL},
372 	{ KEYWORD,	"delete",		NETWORK_BULK_REMOVE,	NULL},
373 	{ ENDTOKEN,	"",			NONE,	NULL}
374 };
375 
376 static const struct token t_community[] = {
377 	{ COMMUNITY,	"",			NONE,	t_set},
378 	{ ENDTOKEN,	"",			NONE,	NULL}
379 };
380 
381 static const struct token t_extcommunity[] = {
382 	{ EXTCOM_SUBTYPE,	"bdc",		NONE,	t_ext_subtype},
383 	{ EXTCOM_SUBTYPE,	"defgw",	NONE,	t_ext_subtype},
384 	{ EXTCOM_SUBTYPE,	"esi-lab",	NONE,	t_ext_subtype},
385 	{ EXTCOM_SUBTYPE,	"esi-rt",	NONE,	t_ext_subtype},
386 	{ EXTCOM_SUBTYPE,	"l2vid",	NONE,	t_ext_subtype},
387 	{ EXTCOM_SUBTYPE,	"mac-mob",	NONE,	t_ext_subtype},
388 	{ EXTCOM_SUBTYPE,	"odi",		NONE,	t_ext_subtype},
389 	{ EXTCOM_SUBTYPE,	"ort",		NONE,	t_ext_subtype},
390 	{ EXTCOM_SUBTYPE,	"ori",		NONE,	t_ext_subtype},
391 	{ EXTCOM_SUBTYPE,	"ovs",		NONE,	t_ext_subtype},
392 	{ EXTCOM_SUBTYPE,	"rt",		NONE,	t_ext_subtype},
393 	{ EXTCOM_SUBTYPE,	"soo",		NONE,	t_ext_subtype},
394 	{ EXTCOM_SUBTYPE,	"srcas",	NONE,	t_ext_subtype},
395 	{ EXTCOM_SUBTYPE,	"vrfri",	NONE,	t_ext_subtype},
396 	{ ENDTOKEN,	"",	NONE,	NULL}
397 };
398 
399 static const struct token t_ext_subtype[] = {
400 	{ EXTCOMMUNITY,	"",	NONE,	t_set},
401 	{ ENDTOKEN,	"",	NONE,	NULL}
402 };
403 
404 static const struct token t_largecommunity[] = {
405 	{ LARGE_COMMUNITY,	"",		NONE,	t_set},
406 	{ ENDTOKEN,	"",			NONE,	NULL}
407 };
408 
409 static const struct token t_localpref[] = {
410 	{ LOCALPREF,	"",			NONE,	t_set},
411 	{ ENDTOKEN,	"",			NONE,	NULL}
412 };
413 
414 static const struct token t_med[] = {
415 	{ MED,		"",			NONE,	t_set},
416 	{ ENDTOKEN,	"",			NONE,	NULL}
417 };
418 
419 static const struct token t_nexthop[] = {
420 	{ NEXTHOP,	"",			NONE,	t_set},
421 	{ ENDTOKEN,	"",			NONE,	NULL}
422 };
423 
424 static const struct token t_pftable[] = {
425 	{ PFTABLE,	"",			NONE,	t_set},
426 	{ ENDTOKEN,	"",			NONE,	NULL}
427 };
428 
429 static const struct token t_prepnbr[] = {
430 	{ PREPNBR,	"",			NONE,	t_set},
431 	{ ENDTOKEN,	"",			NONE,	NULL}
432 };
433 
434 static const struct token t_prepself[] = {
435 	{ PREPSELF,	"",			NONE,	t_set},
436 	{ ENDTOKEN,	"",			NONE,	NULL}
437 };
438 
439 static const struct token t_weight[] = {
440 	{ WEIGHT,	"",			NONE,	t_set},
441 	{ ENDTOKEN,	"",			NONE,	NULL}
442 };
443 
444 static const struct token t_irrfilter[] = {
445 	{ GETOPT,	"",	GETOPT_IRRFILTER,	t_irrfilter},
446 	{ ASNUM,	"",	NONE,			t_irrfilter_opts},
447 	{ ENDTOKEN,	"",	NONE,			NULL}
448 };
449 
450 static const struct token t_irrfilter_opts[] = {
451 	{ NOTOKEN,	"",		NONE,			NULL},
452 	{ FLAG,		"importonly",	F_IMPORTONLY,		t_irrfilter_opts},
453 	{ ENDTOKEN,	"",		NONE,			NULL}
454 };
455 
456 static const struct token t_log[] = {
457 	{ KEYWORD,	"verbose",	LOG_VERBOSE,	NULL},
458 	{ KEYWORD,	"brief",	LOG_BRIEF,	NULL},
459 	{ ENDTOKEN,	"",		NONE,		NULL}
460 };
461 
462 static const struct token t_fib_table[] = {
463 	{ RTABLE,	"",			NONE,	t_fib},
464 	{ ENDTOKEN,	"",			NONE,	NULL}
465 };
466 
467 static const struct token t_show_fib_table[] = {
468 	{ RTABLE,	"",			NONE,	t_show_fib},
469 	{ ENDTOKEN,	"",			NONE,	NULL}
470 };
471 
472 static struct parse_result	res;
473 
474 const struct token	*match_token(int *argc, char **argv[],
475 			    const struct token []);
476 void			 show_valid_args(const struct token []);
477 int			 parse_addr(const char *, struct bgpd_addr *);
478 int			 parse_asnum(const char *, size_t, u_int32_t *);
479 int			 parse_number(const char *, struct parse_result *,
480 			     enum token_type);
481 int			 parse_community(const char *, struct parse_result *);
482 int			 parsesubtype(const char *, u_int8_t *, u_int8_t *);
483 int			 parseextvalue(const char *, u_int32_t *);
484 u_int			 parseextcommunity(const char *, struct parse_result *);
485 int			 parse_largecommunity(const char *,
486 			     struct parse_result *);
487 int			 parse_nexthop(const char *, struct parse_result *);
488 int			 bgpctl_getopt(int *, char **[], int);
489 
490 struct parse_result *
491 parse(int argc, char *argv[])
492 {
493 	const struct token	*table = t_main;
494 	const struct token	*match;
495 
496 	bzero(&res, sizeof(res));
497 	res.rtableid = getrtable();
498 	TAILQ_INIT(&res.set);
499 	if ((res.irr_outdir = getcwd(NULL, 0)) == NULL) {
500 		fprintf(stderr, "getcwd failed: %s\n", strerror(errno));
501 		return (NULL);
502 	}
503 
504 	while (argc >= 0) {
505 		if ((match = match_token(&argc, &argv, table)) == NULL) {
506 			fprintf(stderr, "valid commands/args:\n");
507 			show_valid_args(table);
508 			return (NULL);
509 		}
510 
511 		argc--;
512 		argv++;
513 
514 		if (match->type == NOTOKEN || match->next == NULL)
515 			break;
516 
517 		table = match->next;
518 	}
519 
520 	if (argc > 0) {
521 		fprintf(stderr, "superfluous argument: %s\n", argv[0]);
522 		return (NULL);
523 	}
524 
525 	return (&res);
526 }
527 
528 const struct token *
529 match_token(int *argc, char **argv[], const struct token table[])
530 {
531 	u_int			 i, match;
532 	const struct token	*t = NULL;
533 	struct filter_set	*fs;
534 	const char		*word = *argv[0];
535 	size_t			wordlen = 0;
536 
537 	match = 0;
538 	if (word != NULL)
539 		wordlen = strlen(word);
540 	for (i = 0; table[i].type != ENDTOKEN; i++) {
541 		switch (table[i].type) {
542 		case NOTOKEN:
543 			if (word == NULL || wordlen == 0) {
544 				match++;
545 				t = &table[i];
546 			}
547 			break;
548 		case KEYWORD:
549 			if (word != NULL && strncmp(word, table[i].keyword,
550 			    wordlen) == 0) {
551 				match++;
552 				t = &table[i];
553 				if (t->value)
554 					res.action = t->value;
555 			}
556 			break;
557 		case FLAG:
558 			if (word != NULL && strncmp(word, table[i].keyword,
559 			    wordlen) == 0) {
560 				match++;
561 				t = &table[i];
562 				res.flags |= t->value;
563 			}
564 			break;
565 		case FAMILY:
566 			if (word == NULL)
567 				break;
568 			if (!strcmp(word, "inet") ||
569 			    !strcasecmp(word, "IPv4")) {
570 				match++;
571 				t = &table[i];
572 				res.aid = AID_INET;
573 			}
574 			if (!strcmp(word, "inet6") ||
575 			    !strcasecmp(word, "IPv6")) {
576 				match++;
577 				t = &table[i];
578 				res.aid = AID_INET6;
579 			}
580 			if (!strcasecmp(word, "VPNv4")) {
581 				match++;
582 				t = &table[i];
583 				res.aid = AID_VPN_IPv4;
584 			}
585 			break;
586 		case ADDRESS:
587 			if (parse_addr(word, &res.addr)) {
588 				match++;
589 				t = &table[i];
590 				if (t->value)
591 					res.action = t->value;
592 			}
593 			break;
594 		case PEERADDRESS:
595 			if (parse_addr(word, &res.peeraddr)) {
596 				match++;
597 				t = &table[i];
598 				if (t->value)
599 					res.action = t->value;
600 			}
601 			break;
602 		case PREFIX:
603 			if (parse_prefix(word, wordlen, &res.addr, &res.prefixlen)) {
604 				match++;
605 				t = &table[i];
606 				if (t->value)
607 					res.action = t->value;
608 			}
609 			break;
610 		case ASTYPE:
611 			if (word != NULL && strncmp(word, table[i].keyword,
612 			    wordlen) == 0) {
613 				match++;
614 				t = &table[i];
615 				res.as.type = t->value;
616 			}
617 			break;
618 		case ASNUM:
619 			if (parse_asnum(word, wordlen, &res.as.as_min)) {
620 				res.as.as_max = res.as.as_min;
621 				match++;
622 				t = &table[i];
623 			}
624 			break;
625 		case PEERDESC:
626 			if (!match && word != NULL && wordlen > 0) {
627 				if (strlcpy(res.peerdesc, word,
628 				    sizeof(res.peerdesc)) >=
629 				    sizeof(res.peerdesc))
630 					errx(1, "neighbor description too "
631 					    "long");
632 				match++;
633 				t = &table[i];
634 			}
635 			break;
636 		case RIBNAME:
637 			if (!match && word != NULL && wordlen > 0) {
638 				if (strlcpy(res.rib, word, sizeof(res.rib)) >=
639 				    sizeof(res.rib))
640 					errx(1, "rib name too long");
641 				match++;
642 				t = &table[i];
643 			}
644 			break;
645 		case SHUTDOWN_COMMUNICATION:
646 			if (!match && word != NULL && wordlen > 0) {
647 				if (strlcpy(res.shutcomm, word,
648 				    sizeof(res.shutcomm)) >=
649 				    sizeof(res.shutcomm))
650 					errx(1, "shutdown reason too long");
651 				match++;
652 				t = &table[i];
653 			}
654 			break;
655 		case COMMUNITY:
656 			if (word != NULL && wordlen > 0 &&
657 			    parse_community(word, &res)) {
658 				match++;
659 				t = &table[i];
660 			}
661 			break;
662 		case EXTCOM_SUBTYPE:
663 			if (word != NULL && strncmp(word, table[i].keyword,
664 			    wordlen) == 0) {
665 				if (parsesubtype(word, &res.community.c.e.type,
666 				    &res.community.c.e.subtype) == 0)
667 					errx(1, "Bad ext-community unknown "
668 					    "type");
669 				match++;
670 				t = &table[i];
671 			}
672 			break;
673 		case EXTCOMMUNITY:
674 			if (word != NULL && wordlen > 0 &&
675 			    parseextcommunity(word, &res)) {
676 				match++;
677 				t = &table[i];
678 			}
679 			break;
680 		case LARGE_COMMUNITY:
681 			if (word != NULL && wordlen > 0 &&
682 			    parse_largecommunity(word, &res)) {
683 				match++;
684 				t = &table[i];
685 			}
686 			break;
687 		case LOCALPREF:
688 		case MED:
689 		case PREPNBR:
690 		case PREPSELF:
691 		case WEIGHT:
692 		case RTABLE:
693 			if (word != NULL && wordlen > 0 &&
694 			    parse_number(word, &res, table[i].type)) {
695 				match++;
696 				t = &table[i];
697 			}
698 			break;
699 		case NEXTHOP:
700 			if (word != NULL && wordlen > 0 &&
701 			    parse_nexthop(word, &res)) {
702 				match++;
703 				t = &table[i];
704 			}
705 			break;
706 		case PFTABLE:
707 			if (word != NULL && wordlen > 0) {
708 				if ((fs = calloc(1,
709 				    sizeof(struct filter_set))) == NULL)
710 					err(1, NULL);
711 				if (strlcpy(fs->action.pftable, word,
712 				    sizeof(fs->action.pftable)) >=
713 				    sizeof(fs->action.pftable))
714 					errx(1, "pftable name too long");
715 				TAILQ_INSERT_TAIL(&res.set, fs, entry);
716 				match++;
717 				t = &table[i];
718 			}
719 			break;
720 		case GETOPT:
721 			if (bgpctl_getopt(argc, argv, table[i].value)) {
722 				match++;
723 				t = &table[i];
724 			}
725 			break;
726 		case FILENAME:
727 			if (word != NULL && wordlen > 0) {
728 				if ((res.mrtfd = open(word, O_RDONLY)) == -1) {
729 					/*
730 					 * ignore error if path has no / and
731 					 * does not exist. In hope to print
732 					 * usage.
733 					 */
734 					if (errno == ENOENT &&
735 					    !strchr(word, '/'))
736 						break;
737 					err(1, "mrt open(%s)", word);
738 				}
739 				match++;
740 				t = &table[i];
741 			}
742 			break;
743 		case BULK:
744 			match++;
745 			t = &table[i];
746 			break;
747 		case ENDTOKEN:
748 			break;
749 		}
750 	}
751 
752 	if (match != 1) {
753 		if (word == NULL)
754 			fprintf(stderr, "missing argument:\n");
755 		else if (match > 1)
756 			fprintf(stderr, "ambiguous argument: %s\n", word);
757 		else if (match < 1)
758 			fprintf(stderr, "unknown argument: %s\n", word);
759 		return (NULL);
760 	}
761 
762 	return (t);
763 }
764 
765 void
766 show_valid_args(const struct token table[])
767 {
768 	int	i;
769 
770 	for (i = 0; table[i].type != ENDTOKEN; i++) {
771 		switch (table[i].type) {
772 		case NOTOKEN:
773 			fprintf(stderr, "  <cr>\n");
774 			break;
775 		case KEYWORD:
776 		case FLAG:
777 		case ASTYPE:
778 		case EXTCOM_SUBTYPE:
779 			fprintf(stderr, "  %s\n", table[i].keyword);
780 			break;
781 		case ADDRESS:
782 		case PEERADDRESS:
783 			fprintf(stderr, "  <address>\n");
784 			break;
785 		case PREFIX:
786 			fprintf(stderr, "  <address>[/<len>]\n");
787 			break;
788 		case ASNUM:
789 			fprintf(stderr, "  <asnum>\n");
790 			break;
791 		case PEERDESC:
792 			fprintf(stderr, "  <neighbor description>\n");
793 			break;
794 		case RIBNAME:
795 			fprintf(stderr, "  <rib name>\n");
796 			break;
797 		case SHUTDOWN_COMMUNICATION:
798 			fprintf(stderr, "  <shutdown reason>\n");
799 			break;
800 		case COMMUNITY:
801 			fprintf(stderr, "  <community>\n");
802 			break;
803 		case EXTCOMMUNITY:
804 			fprintf(stderr, "  <extended-community>\n");
805 			break;
806 		case LARGE_COMMUNITY:
807 			fprintf(stderr, "  <large-community>\n");
808 			break;
809 		case LOCALPREF:
810 		case MED:
811 		case PREPNBR:
812 		case PREPSELF:
813 		case WEIGHT:
814 			fprintf(stderr, "  <number>\n");
815 			break;
816 		case RTABLE:
817 			fprintf(stderr, "  <rtableid>\n");
818 			break;
819 		case NEXTHOP:
820 			fprintf(stderr, "  <address>\n");
821 			break;
822 		case PFTABLE:
823 			fprintf(stderr, "  <pftable>\n");
824 			break;
825 		case FAMILY:
826 			fprintf(stderr, "  [ inet | inet6 | IPv4 | IPv6 | VPNv4 ]\n");
827 			break;
828 		case GETOPT:
829 			fprintf(stderr, "  <options>\n");
830 			break;
831 		case FILENAME:
832 			fprintf(stderr, "  <filename>\n");
833 			break;
834 		case BULK:
835 		case ENDTOKEN:
836 			break;
837 		}
838 	}
839 }
840 
841 int
842 parse_addr(const char *word, struct bgpd_addr *addr)
843 {
844 	struct in_addr	ina;
845 	struct addrinfo	hints, *r;
846 
847 	if (word == NULL)
848 		return (0);
849 
850 	bzero(addr, sizeof(struct bgpd_addr));
851 	bzero(&ina, sizeof(ina));
852 
853 	if (inet_net_pton(AF_INET, word, &ina, sizeof(ina)) != -1) {
854 		addr->aid = AID_INET;
855 		addr->v4 = ina;
856 		return (1);
857 	}
858 
859 	bzero(&hints, sizeof(hints));
860 	hints.ai_family = AF_INET6;
861 	hints.ai_socktype = SOCK_DGRAM; /*dummy*/
862 	hints.ai_flags = AI_NUMERICHOST;
863 	if (getaddrinfo(word, "0", &hints, &r) == 0) {
864 		sa2addr(r->ai_addr, addr);
865 		freeaddrinfo(r);
866 		return (1);
867 	}
868 
869 	return (0);
870 }
871 
872 int
873 parse_prefix(const char *word, size_t wordlen, struct bgpd_addr *addr, u_int8_t *prefixlen)
874 {
875 	char		*p, *ps;
876 	const char	*errstr;
877 	int		 mask = -1;
878 
879 	if (word == NULL)
880 		return (0);
881 
882 	bzero(addr, sizeof(struct bgpd_addr));
883 
884 	if ((p = strrchr(word, '/')) != NULL) {
885 		size_t plen = strlen(p);
886 		mask = strtonum(p + 1, 0, 128, &errstr);
887 		if (errstr)
888 			errx(1, "netmask %s", errstr);
889 
890 		if ((ps = malloc(wordlen - plen + 1)) == NULL)
891 			err(1, "parse_prefix: malloc");
892 		strlcpy(ps, word, wordlen - plen + 1);
893 
894 		if (parse_addr(ps, addr) == 0) {
895 			free(ps);
896 			return (0);
897 		}
898 
899 		free(ps);
900 	} else
901 		if (parse_addr(word, addr) == 0)
902 			return (0);
903 
904 	switch (addr->aid) {
905 	case AID_INET:
906 		if (mask == -1)
907 			mask = 32;
908 		if (mask > 32)
909 			errx(1, "invalid netmask: too large");
910 		addr->v4.s_addr = addr->v4.s_addr & htonl(prefixlen2mask(mask));
911 		break;
912 	case AID_INET6:
913 		if (mask == -1)
914 			mask = 128;
915 		inet6applymask(&addr->v6, &addr->v6, mask);
916 		break;
917 	default:
918 		return (0);
919 	}
920 
921 	*prefixlen = mask;
922 	return (1);
923 }
924 
925 int
926 parse_asnum(const char *word, size_t wordlen, u_int32_t *asnum)
927 {
928 	const char	*errstr;
929 	char		*dot;
930 	u_int32_t	 uval, uvalh = 0;
931 
932 	if (word == NULL)
933 		return (0);
934 
935 	if (wordlen < 1 || word[0] < '0' || word[0] > '9')
936 		return (0);
937 
938 	if ((dot = strchr(word,'.')) != NULL) {
939 		*dot++ = '\0';
940 		uvalh = strtonum(word, 0, USHRT_MAX, &errstr);
941 		if (errstr)
942 			errx(1, "AS number is %s: %s", errstr, word);
943 		uval = strtonum(dot, 0, USHRT_MAX, &errstr);
944 		if (errstr)
945 			errx(1, "AS number is %s: %s", errstr, word);
946 	} else {
947 		uval = strtonum(word, 0, UINT_MAX, &errstr);
948 		if (errstr)
949 			errx(1, "AS number is %s: %s", errstr, word);
950 	}
951 
952 	*asnum = uval | (uvalh << 16);
953 	return (1);
954 }
955 
956 int
957 parse_number(const char *word, struct parse_result *r, enum token_type type)
958 {
959 	struct filter_set	*fs;
960 	const char		*errstr;
961 	u_int			 uval;
962 
963 	if (word == NULL)
964 		return (0);
965 
966 	uval = strtonum(word, 0, UINT_MAX, &errstr);
967 	if (errstr)
968 		errx(1, "number is %s: %s", errstr, word);
969 
970 	/* number was parseable */
971 	if (type == RTABLE) {
972 		r->rtableid = uval;
973 		return (1);
974 	}
975 
976 	if ((fs = calloc(1, sizeof(struct filter_set))) == NULL)
977 		err(1, NULL);
978 	switch (type) {
979 	case LOCALPREF:
980 		fs->type = ACTION_SET_LOCALPREF;
981 		fs->action.metric = uval;
982 		break;
983 	case MED:
984 		fs->type = ACTION_SET_MED;
985 		fs->action.metric = uval;
986 		break;
987 	case PREPNBR:
988 		if (uval > 128) {
989 			free(fs);
990 			return (0);
991 		}
992 		fs->type = ACTION_SET_PREPEND_PEER;
993 		fs->action.prepend = uval;
994 		break;
995 	case PREPSELF:
996 		if (uval > 128) {
997 			free(fs);
998 			return (0);
999 		}
1000 		fs->type = ACTION_SET_PREPEND_SELF;
1001 		fs->action.prepend = uval;
1002 		break;
1003 	case WEIGHT:
1004 		fs->type = ACTION_SET_WEIGHT;
1005 		fs->action.metric = uval;
1006 		break;
1007 	default:
1008 		errx(1, "king bula sez bad things happen");
1009 	}
1010 
1011 	TAILQ_INSERT_TAIL(&r->set, fs, entry);
1012 	return (1);
1013 }
1014 
1015 static u_int32_t
1016 getcommunity(const char *s, int large, u_int8_t *flag)
1017 {
1018 	int64_t		 max = USHRT_MAX;
1019 	const char	*errstr;
1020 	u_int32_t	 uval;
1021 
1022 	if (strcmp(s, "*") == 0) {
1023 		*flag = COMMUNITY_ANY;
1024 		return (0);
1025 	}
1026 
1027 	if (large)
1028 		max = UINT_MAX;
1029 
1030 	uval = strtonum(s, 0, max, &errstr);
1031 	if (errstr)
1032 		errx(1, "Community is %s: %s", errstr, s);
1033 
1034 	*flag = 0;
1035 	return (uval);
1036 }
1037 
1038 int
1039 parse_community(const char *word, struct parse_result *r)
1040 {
1041 	struct filter_set	*fs;
1042 	char			*p;
1043 	u_int32_t		 as, type;
1044 	u_int8_t		 asflag, tflag;
1045 
1046 	/* Well-known communities */
1047 	if (strcasecmp(word, "GRACEFUL_SHUTDOWN") == 0) {
1048 		as = COMMUNITY_WELLKNOWN;
1049 		type = COMMUNITY_GRACEFUL_SHUTDOWN;
1050 		goto done;
1051 	} else if (strcasecmp(word, "NO_EXPORT") == 0) {
1052 		as = COMMUNITY_WELLKNOWN;
1053 		type = COMMUNITY_NO_EXPORT;
1054 		goto done;
1055 	} else if (strcasecmp(word, "NO_ADVERTISE") == 0) {
1056 		as = COMMUNITY_WELLKNOWN;
1057 		type = COMMUNITY_NO_ADVERTISE;
1058 		goto done;
1059 	} else if (strcasecmp(word, "NO_EXPORT_SUBCONFED") == 0) {
1060 		as = COMMUNITY_WELLKNOWN;
1061 		type = COMMUNITY_NO_EXPSUBCONFED;
1062 		goto done;
1063 	} else if (strcasecmp(word, "NO_PEER") == 0) {
1064 		as = COMMUNITY_WELLKNOWN;
1065 		type = COMMUNITY_NO_PEER;
1066 		goto done;
1067 	} else if (strcasecmp(word, "BLACKHOLE") == 0) {
1068 		as = COMMUNITY_WELLKNOWN;
1069 		type = COMMUNITY_BLACKHOLE;
1070 		goto done;
1071 	}
1072 
1073 	if ((p = strchr(word, ':')) == NULL) {
1074 		fprintf(stderr, "Bad community syntax\n");
1075 		return (0);
1076 	}
1077 	*p++ = 0;
1078 
1079 	as = getcommunity(word, 0, &asflag);
1080 	type = getcommunity(p, 0, &tflag);
1081 
1082 done:
1083 	if ((fs = calloc(1, sizeof(struct filter_set))) == NULL)
1084 		err(1, NULL);
1085 	fs->type = ACTION_SET_COMMUNITY;
1086 	fs->action.community.type = COMMUNITY_TYPE_BASIC;
1087 	fs->action.community.c.b.data1 = as;
1088 	fs->action.community.c.b.data2 = type;
1089 	fs->action.community.dflag1 = asflag;
1090 	fs->action.community.dflag2 = tflag;
1091 
1092 	r->community = fs->action.community;
1093 
1094 	TAILQ_INSERT_TAIL(&r->set, fs, entry);
1095 	return (1);
1096 }
1097 
1098 int
1099 parsesubtype(const char *name, u_int8_t *type, u_int8_t *subtype)
1100 {
1101 	const struct ext_comm_pairs *cp;
1102 	int found = 0;
1103 
1104 	for (cp = iana_ext_comms; cp->subname != NULL; cp++) {
1105 		if (strcmp(name, cp->subname) == 0) {
1106 			if (found == 0) {
1107 				*type = cp->type;
1108 				*subtype = cp->subtype;
1109 			}
1110 			found++;
1111 		}
1112 	}
1113 	if (found > 1)
1114 		*type = -1;
1115 	return (found);
1116 }
1117 
1118 int
1119 parseextvalue(const char *s, u_int32_t *v)
1120 {
1121 	const char	*errstr;
1122 	char		*p;
1123 	struct in_addr	 ip;
1124 	u_int32_t	 uvalh = 0, uval;
1125 
1126 	if ((p = strchr(s, '.')) == NULL) {
1127 		/* AS_PLAIN number (4 or 2 byte) */
1128 		uval = strtonum(s, 0, UINT_MAX, &errstr);
1129 		if (errstr) {
1130 			fprintf(stderr, "Bad ext-community: %s is %s\n", s,
1131 			    errstr);
1132 			return (-1);
1133 		}
1134 		*v = uval;
1135 		if (uval <= USHRT_MAX)
1136 			return (EXT_COMMUNITY_TRANS_TWO_AS);
1137 		else
1138 			return (EXT_COMMUNITY_TRANS_FOUR_AS);
1139 	} else if (strchr(p + 1, '.') == NULL) {
1140 		/* AS_DOT number (4-byte) */
1141 		*p++ = '\0';
1142 		uvalh = strtonum(s, 0, USHRT_MAX, &errstr);
1143 		if (errstr) {
1144 			fprintf(stderr, "Bad ext-community: %s is %s\n", s,
1145 			    errstr);
1146 			return (-1);
1147 		}
1148 		uval = strtonum(p, 0, USHRT_MAX, &errstr);
1149 		if (errstr) {
1150 			fprintf(stderr, "Bad ext-community: %s is %s\n", p,
1151 			    errstr);
1152 			return (-1);
1153 		}
1154 		*v = uval | (uvalh << 16);
1155 		return (EXT_COMMUNITY_TRANS_FOUR_AS);
1156 	} else {
1157 		/* more than one dot -> IP address */
1158 		if (inet_aton(s, &ip) == 0) {
1159 			fprintf(stderr, "Bad ext-community: %s not parseable\n",
1160 			    s);
1161 			return (-1);
1162 		}
1163 		*v = ntohl(ip.s_addr);
1164 		return (EXT_COMMUNITY_TRANS_IPV4);
1165 	}
1166 	return (-1);
1167 }
1168 
1169 u_int
1170 parseextcommunity(const char *word, struct parse_result *r)
1171 {
1172 	struct filter_set		*fs;
1173 	const struct ext_comm_pairs	*cp;
1174 	const char			*errstr;
1175 	u_int64_t			 ullval;
1176 	u_int32_t			 uval;
1177 	char				*p, *ep;
1178 	int				 type;
1179 
1180 	type = r->community.c.e.type;
1181 
1182 	switch (type) {
1183 	case 0xff:
1184 		if ((p = strchr(word, ':')) == NULL) {
1185 			fprintf(stderr, "Bad ext-community: %s\n", word);
1186 			return (0);
1187 		}
1188 		*p++ = '\0';
1189 		if ((type = parseextvalue(word, &uval)) == -1)
1190 			return (0);
1191 		switch (type) {
1192 		case EXT_COMMUNITY_TRANS_TWO_AS:
1193 			ullval = strtonum(p, 0, UINT_MAX, &errstr);
1194 			break;
1195 		case EXT_COMMUNITY_TRANS_IPV4:
1196 		case EXT_COMMUNITY_TRANS_FOUR_AS:
1197 			ullval = strtonum(p, 0, USHRT_MAX, &errstr);
1198 			break;
1199 		default:
1200 			fprintf(stderr, "parseextcommunity: unexpected "
1201 			    "result\n");
1202 			return (0);
1203 		}
1204 		if (errstr) {
1205 			fprintf(stderr, "Bad ext-community: %s is %s\n", p,
1206 			    errstr);
1207 			return (0);
1208 		}
1209 		switch (type) {
1210 		case EXT_COMMUNITY_TRANS_TWO_AS:
1211 			r->community.c.e.data1 = uval;
1212 			r->community.c.e.data2 = ullval;
1213 			break;
1214 		case EXT_COMMUNITY_TRANS_IPV4:
1215 		case EXT_COMMUNITY_TRANS_FOUR_AS:
1216 			r->community.c.e.data1 = uval;
1217 			r->community.c.e.data2 = ullval;
1218 			break;
1219 		}
1220 		break;
1221 	case EXT_COMMUNITY_TRANS_OPAQUE:
1222 	case EXT_COMMUNITY_TRANS_EVPN:
1223 		errno = 0;
1224 		ullval = strtoull(word, &ep, 0);
1225 		if (word[0] == '\0' || *ep != '\0') {
1226 			fprintf(stderr, "Bad ext-community: bad value\n");
1227 			return (0);
1228 		}
1229 		if (errno == ERANGE && ullval > EXT_COMMUNITY_OPAQUE_MAX) {
1230 			fprintf(stderr, "Bad ext-community: too big\n");
1231 			return (0);
1232 		}
1233 		r->community.c.e.data2 = ullval;
1234 		break;
1235 	case EXT_COMMUNITY_NON_TRANS_OPAQUE:
1236 		if (strcmp(word, "valid") == 0)
1237 			r->community.c.e.data2 = EXT_COMMUNITY_OVS_VALID;
1238 		else if (strcmp(word, "invalid") == 0)
1239 			r->community.c.e.data2 = EXT_COMMUNITY_OVS_INVALID;
1240 		else if (strcmp(word, "not-found") == 0)
1241 			r->community.c.e.data2 = EXT_COMMUNITY_OVS_NOTFOUND;
1242 		else {
1243 			fprintf(stderr, "Bad ext-community value: %s\n", word);
1244 			return (0);
1245 		}
1246 		break;
1247 	}
1248 	r->community.c.e.type = type;
1249 
1250 	/* verify type/subtype combo */
1251 	for (cp = iana_ext_comms; cp->subname != NULL; cp++) {
1252 		if (cp->type == r->community.c.e.type &&
1253 		    cp->subtype == r->community.c.e.subtype) {
1254 			r->community.type = COMMUNITY_TYPE_EXT;;
1255 			if ((fs = calloc(1, sizeof(struct filter_set))) == NULL)
1256 				err(1, NULL);
1257 
1258 			fs->type = ACTION_SET_COMMUNITY;
1259 			memcpy(&fs->action.community, &r->community,
1260 			    sizeof(struct filter_community));
1261 
1262 			TAILQ_INSERT_TAIL(&r->set, fs, entry);
1263 			return (1);
1264 		}
1265 	}
1266 
1267 	fprintf(stderr, "Bad ext-community: bad format for type\n");
1268 	return (0);
1269 }
1270 
1271 int
1272 parse_largecommunity(const char *word, struct parse_result *r)
1273 {
1274 	struct filter_set *fs;
1275 	char		*p, *po = strdup(word);
1276 	char		*array[3] = { NULL, NULL, NULL };
1277 	char		*val;
1278 	u_int32_t	 as, ld1, ld2;
1279 	u_int8_t	 asflag, ld1flag, ld2flag;
1280 	int		 i = 0;
1281 
1282 	p = po;
1283 	while ((p != NULL) && (i < 3)) {
1284 		val = strsep(&p, ":");
1285 		array[i++] = val;
1286 	}
1287 
1288 	if ((p != NULL) || !(array[0] && array[1] && array[2]))
1289 		errx(1, "Invalid Large-Community syntax");
1290 
1291 	as = getcommunity(array[0], 1, &asflag);
1292 	ld1 = getcommunity(array[1], 1, &ld1flag);
1293 	ld2 = getcommunity(array[2], 1, &ld2flag);
1294 
1295 	free(po);
1296 
1297 	if ((fs = calloc(1, sizeof(struct filter_set))) == NULL)
1298 		err(1, NULL);
1299 	fs->type = ACTION_SET_COMMUNITY;
1300 	fs->action.community.type = COMMUNITY_TYPE_LARGE;
1301 	fs->action.community.c.l.data1 = as;
1302 	fs->action.community.c.l.data2 = ld1;
1303 	fs->action.community.c.l.data3 = ld2;
1304 	fs->action.community.dflag1 = asflag;
1305 	fs->action.community.dflag2 = ld1flag;
1306 	fs->action.community.dflag3 = ld2flag;
1307 
1308 	r->community = fs->action.community;
1309 
1310 	TAILQ_INSERT_TAIL(&r->set, fs, entry);
1311 	return (1);
1312 }
1313 
1314 int
1315 parse_nexthop(const char *word, struct parse_result *r)
1316 {
1317 	struct filter_set	*fs;
1318 
1319 	if ((fs = calloc(1, sizeof(struct filter_set))) == NULL)
1320 		err(1, NULL);
1321 
1322 	if (strcmp(word, "blackhole") == 0)
1323 		fs->type = ACTION_SET_NEXTHOP_BLACKHOLE;
1324 	else if (strcmp(word, "reject") == 0)
1325 		fs->type = ACTION_SET_NEXTHOP_REJECT;
1326 	else if (strcmp(word, "no-modify") == 0)
1327 		fs->type = ACTION_SET_NEXTHOP_NOMODIFY;
1328 	else if (parse_addr(word, &fs->action.nexthop)) {
1329 		fs->type = ACTION_SET_NEXTHOP;
1330 	} else {
1331 		free(fs);
1332 		return (0);
1333 	}
1334 
1335 	TAILQ_INSERT_TAIL(&r->set, fs, entry);
1336 	return (1);
1337 }
1338 
1339 int
1340 bgpctl_getopt(int *argc, char **argv[], int type)
1341 {
1342 	int	  ch;
1343 
1344 	optind = optreset = 1;
1345 	while ((ch = getopt((*argc) + 1, (*argv) - 1, "46o:")) != -1) {
1346 		switch (ch) {
1347 		case '4':
1348 			res.flags = (res.flags | F_IPV4) & ~F_IPV6;
1349 			break;
1350 		case '6':
1351 			res.flags = (res.flags | F_IPV6) & ~F_IPV4;
1352 			break;
1353 		case 'o':
1354 			res.irr_outdir = optarg;
1355 			break;
1356 		default:
1357 			usage();
1358 			/* NOTREACHED */
1359 		}
1360 	}
1361 
1362 	if (optind > 1) {
1363 		(*argc) -= (optind - 1);
1364 		(*argv) += (optind - 1);
1365 
1366 		/* need to move one backwards as calling code moves forward */
1367 		(*argc)++;
1368 		(*argv)--;
1369 		return (1);
1370 	} else
1371 		return (0);
1372 }
1373