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