1 /* $OpenBSD: parser.c,v 1.113 2022/06/22 12:27:46 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, 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 break; 972 case AID_INET6: 973 if (mask == -1) 974 mask = 128; 975 break; 976 default: 977 return (0); 978 } 979 980 applymask(addr, addr, mask); 981 *prefixlen = mask; 982 return (1); 983 } 984 985 int 986 parse_asnum(const char *word, size_t wordlen, uint32_t *asnum) 987 { 988 const char *errstr; 989 char *dot, *parseword; 990 uint32_t uval, uvalh = 0; 991 992 if (word == NULL) 993 return (0); 994 995 if (wordlen < 1 || word[0] < '0' || word[0] > '9') 996 return (0); 997 998 parseword = strdup(word); 999 if ((dot = strchr(parseword, '.')) != NULL) { 1000 *dot++ = '\0'; 1001 uvalh = strtonum(parseword, 0, USHRT_MAX, &errstr); 1002 if (errstr) 1003 errx(1, "AS number is %s: %s", errstr, word); 1004 uval = strtonum(dot, 0, USHRT_MAX, &errstr); 1005 if (errstr) 1006 errx(1, "AS number is %s: %s", errstr, word); 1007 } else { 1008 uval = strtonum(parseword, 0, UINT_MAX, &errstr); 1009 if (errstr) 1010 errx(1, "AS number is %s: %s", errstr, word); 1011 } 1012 1013 free(parseword); 1014 *asnum = uval | (uvalh << 16); 1015 return (1); 1016 } 1017 1018 int 1019 parse_number(const char *word, struct parse_result *r, enum token_type type) 1020 { 1021 struct filter_set *fs; 1022 const char *errstr; 1023 u_int uval; 1024 1025 if (word == NULL) 1026 return (0); 1027 1028 uval = strtonum(word, 0, UINT_MAX, &errstr); 1029 if (errstr) 1030 errx(1, "number is %s: %s", errstr, word); 1031 1032 /* number was parseable */ 1033 switch (type) { 1034 case RTABLE: 1035 r->rtableid = uval; 1036 return (1); 1037 case PATHID: 1038 r->pathid = uval; 1039 r->flags |= F_CTL_HAS_PATHID; 1040 return (1); 1041 default: 1042 break; 1043 } 1044 1045 if ((fs = calloc(1, sizeof(struct filter_set))) == NULL) 1046 err(1, NULL); 1047 switch (type) { 1048 case LOCALPREF: 1049 fs->type = ACTION_SET_LOCALPREF; 1050 fs->action.metric = uval; 1051 break; 1052 case MED: 1053 fs->type = ACTION_SET_MED; 1054 fs->action.metric = uval; 1055 break; 1056 case PREPNBR: 1057 if (uval > 128) { 1058 free(fs); 1059 return (0); 1060 } 1061 fs->type = ACTION_SET_PREPEND_PEER; 1062 fs->action.prepend = uval; 1063 break; 1064 case PREPSELF: 1065 if (uval > 128) { 1066 free(fs); 1067 return (0); 1068 } 1069 fs->type = ACTION_SET_PREPEND_SELF; 1070 fs->action.prepend = uval; 1071 break; 1072 case WEIGHT: 1073 fs->type = ACTION_SET_WEIGHT; 1074 fs->action.metric = uval; 1075 break; 1076 default: 1077 errx(1, "king bula sez bad things happen"); 1078 } 1079 1080 TAILQ_INSERT_TAIL(&r->set, fs, entry); 1081 return (1); 1082 } 1083 1084 static void 1085 getcommunity(char *s, int large, uint32_t *val, uint32_t *flag) 1086 { 1087 long long max = USHRT_MAX; 1088 const char *errstr; 1089 1090 *flag = 0; 1091 *val = 0; 1092 if (strcmp(s, "*") == 0) { 1093 *flag = COMMUNITY_ANY; 1094 return; 1095 } else if (strcmp(s, "neighbor-as") == 0) { 1096 *flag = COMMUNITY_NEIGHBOR_AS; 1097 return; 1098 } else if (strcmp(s, "local-as") == 0) { 1099 *flag = COMMUNITY_LOCAL_AS; 1100 return; 1101 } 1102 if (large) 1103 max = UINT_MAX; 1104 *val = strtonum(s, 0, max, &errstr); 1105 if (errstr) 1106 errx(1, "Community %s is %s (max: %llu)", s, errstr, max); 1107 } 1108 1109 static void 1110 setcommunity(struct community *c, uint32_t as, uint32_t data, 1111 uint32_t asflag, uint32_t dataflag) 1112 { 1113 c->flags = COMMUNITY_TYPE_BASIC; 1114 c->flags |= asflag << 8; 1115 c->flags |= dataflag << 16; 1116 c->data1 = as; 1117 c->data2 = data; 1118 c->data3 = 0; 1119 } 1120 1121 static void 1122 parselargecommunity(struct community *c, char *s) 1123 { 1124 char *p, *q; 1125 uint32_t dflag1, dflag2, dflag3; 1126 1127 if ((p = strchr(s, ':')) == NULL) 1128 errx(1, "Bad community syntax"); 1129 *p++ = 0; 1130 1131 if ((q = strchr(p, ':')) == NULL) 1132 errx(1, "Bad community syntax"); 1133 *q++ = 0; 1134 1135 getcommunity(s, 1, &c->data1, &dflag1); 1136 getcommunity(p, 1, &c->data2, &dflag2); 1137 getcommunity(q, 1, &c->data3, &dflag3); 1138 1139 c->flags = COMMUNITY_TYPE_LARGE; 1140 c->flags |= dflag1 << 8; 1141 c->flags |= dflag2 << 16; 1142 c->flags |= dflag3 << 24; 1143 } 1144 1145 void 1146 parsecommunity(struct community *c, int type, char *s) 1147 { 1148 char *p; 1149 uint32_t as, data, asflag, dataflag; 1150 1151 if (type == COMMUNITY_TYPE_LARGE) { 1152 parselargecommunity(c, s); 1153 return; 1154 } 1155 1156 /* Well-known communities */ 1157 if (strcasecmp(s, "GRACEFUL_SHUTDOWN") == 0) { 1158 setcommunity(c, COMMUNITY_WELLKNOWN, 1159 COMMUNITY_GRACEFUL_SHUTDOWN, 0, 0); 1160 return; 1161 } else if (strcasecmp(s, "NO_EXPORT") == 0) { 1162 setcommunity(c, COMMUNITY_WELLKNOWN, 1163 COMMUNITY_NO_EXPORT, 0, 0); 1164 return; 1165 } else if (strcasecmp(s, "NO_ADVERTISE") == 0) { 1166 setcommunity(c, COMMUNITY_WELLKNOWN, 1167 COMMUNITY_NO_ADVERTISE, 0, 0); 1168 return; 1169 } else if (strcasecmp(s, "NO_EXPORT_SUBCONFED") == 0) { 1170 setcommunity(c, COMMUNITY_WELLKNOWN, 1171 COMMUNITY_NO_EXPSUBCONFED, 0, 0); 1172 return; 1173 } else if (strcasecmp(s, "NO_PEER") == 0) { 1174 setcommunity(c, COMMUNITY_WELLKNOWN, 1175 COMMUNITY_NO_PEER, 0, 0); 1176 return; 1177 } else if (strcasecmp(s, "BLACKHOLE") == 0) { 1178 setcommunity(c, COMMUNITY_WELLKNOWN, 1179 COMMUNITY_BLACKHOLE, 0, 0); 1180 return; 1181 } 1182 1183 if ((p = strchr(s, ':')) == NULL) 1184 errx(1, "Bad community syntax"); 1185 *p++ = 0; 1186 1187 getcommunity(s, 0, &as, &asflag); 1188 getcommunity(p, 0, &data, &dataflag); 1189 setcommunity(c, as, data, asflag, dataflag); 1190 } 1191 1192 static int 1193 parsesubtype(const char *name, int *type, int *subtype) 1194 { 1195 const struct ext_comm_pairs *cp; 1196 int found = 0; 1197 1198 for (cp = iana_ext_comms; cp->subname != NULL; cp++) { 1199 if (strcmp(name, cp->subname) == 0) { 1200 if (found == 0) { 1201 *type = cp->type; 1202 *subtype = cp->subtype; 1203 } 1204 found++; 1205 } 1206 } 1207 if (found > 1) 1208 *type = -1; 1209 return (found); 1210 } 1211 1212 static int 1213 parseextvalue(int type, char *s, uint32_t *v, uint32_t *flag) 1214 { 1215 const char *errstr; 1216 char *p; 1217 struct in_addr ip; 1218 uint32_t uvalh, uval; 1219 1220 if (type != -1) { 1221 /* nothing */ 1222 } else if (strcmp(s, "neighbor-as") == 0) { 1223 *flag = COMMUNITY_NEIGHBOR_AS; 1224 *v = 0; 1225 return EXT_COMMUNITY_TRANS_FOUR_AS; 1226 } else if (strcmp(s, "local-as") == 0) { 1227 *flag = COMMUNITY_LOCAL_AS; 1228 *v = 0; 1229 return EXT_COMMUNITY_TRANS_FOUR_AS; 1230 } else if ((p = strchr(s, '.')) == NULL) { 1231 /* AS_PLAIN number (4 or 2 byte) */ 1232 strtonum(s, 0, USHRT_MAX, &errstr); 1233 if (errstr == NULL) 1234 type = EXT_COMMUNITY_TRANS_TWO_AS; 1235 else 1236 type = EXT_COMMUNITY_TRANS_FOUR_AS; 1237 } else if (strchr(p + 1, '.') == NULL) { 1238 /* AS_DOT number (4-byte) */ 1239 type = EXT_COMMUNITY_TRANS_FOUR_AS; 1240 } else { 1241 /* more than one dot -> IP address */ 1242 type = EXT_COMMUNITY_TRANS_IPV4; 1243 } 1244 1245 switch (type) { 1246 case EXT_COMMUNITY_TRANS_TWO_AS: 1247 uval = strtonum(s, 0, USHRT_MAX, &errstr); 1248 if (errstr) 1249 errx(1, "Bad ext-community %s is %s", s, errstr); 1250 *v = uval; 1251 break; 1252 case EXT_COMMUNITY_TRANS_FOUR_AS: 1253 if ((p = strchr(s, '.')) == NULL) { 1254 uval = strtonum(s, 0, UINT_MAX, &errstr); 1255 if (errstr) 1256 errx(1, "Bad ext-community %s is %s", s, 1257 errstr); 1258 *v = uval; 1259 break; 1260 } 1261 *p++ = '\0'; 1262 uvalh = strtonum(s, 0, USHRT_MAX, &errstr); 1263 if (errstr) 1264 errx(1, "Bad ext-community %s is %s", s, errstr); 1265 uval = strtonum(p, 0, USHRT_MAX, &errstr); 1266 if (errstr) 1267 errx(1, "Bad ext-community %s is %s", p, errstr); 1268 *v = uval | (uvalh << 16); 1269 break; 1270 case EXT_COMMUNITY_TRANS_IPV4: 1271 if (inet_aton(s, &ip) == 0) 1272 errx(1, "Bad ext-community %s not parseable", s); 1273 *v = ntohl(ip.s_addr); 1274 break; 1275 default: 1276 errx(1, "%s: unexpected type %d", __func__, type); 1277 } 1278 return (type); 1279 } 1280 1281 void 1282 parseextcommunity(struct community *c, const char *t, char *s) 1283 { 1284 const struct ext_comm_pairs *cp; 1285 char *p, *ep; 1286 uint64_t ullval; 1287 uint32_t uval, uval2, dflag1 = 0, dflag2 = 0; 1288 int type = 0, subtype = 0; 1289 1290 if (strcmp(t, "*") == 0 && strcmp(s, "*") == 0) { 1291 c->flags = COMMUNITY_TYPE_EXT; 1292 c->flags |= COMMUNITY_ANY << 24; 1293 return; 1294 } 1295 if (parsesubtype(t, &type, &subtype) == 0) 1296 errx(1, "Bad ext-community unknown type"); 1297 1298 switch (type) { 1299 case EXT_COMMUNITY_TRANS_TWO_AS: 1300 case EXT_COMMUNITY_TRANS_FOUR_AS: 1301 case EXT_COMMUNITY_TRANS_IPV4: 1302 case -1: 1303 if (strcmp(s, "*") == 0) { 1304 dflag1 = COMMUNITY_ANY; 1305 break; 1306 } 1307 if ((p = strchr(s, ':')) == NULL) 1308 errx(1, "Bad ext-community %s", s); 1309 *p++ = '\0'; 1310 type = parseextvalue(type, s, &uval, &dflag1); 1311 1312 switch (type) { 1313 case EXT_COMMUNITY_TRANS_TWO_AS: 1314 getcommunity(p, 1, &uval2, &dflag2); 1315 break; 1316 case EXT_COMMUNITY_TRANS_IPV4: 1317 case EXT_COMMUNITY_TRANS_FOUR_AS: 1318 getcommunity(p, 0, &uval2, &dflag2); 1319 break; 1320 default: 1321 errx(1, "parseextcommunity: unexpected result"); 1322 } 1323 1324 c->data1 = uval; 1325 c->data2 = uval2; 1326 break; 1327 case EXT_COMMUNITY_TRANS_OPAQUE: 1328 case EXT_COMMUNITY_TRANS_EVPN: 1329 if (strcmp(s, "*") == 0) { 1330 dflag1 = COMMUNITY_ANY; 1331 break; 1332 } 1333 errno = 0; 1334 ullval = strtoull(s, &ep, 0); 1335 if (s[0] == '\0' || *ep != '\0') 1336 errx(1, "Bad ext-community bad value"); 1337 if (errno == ERANGE && ullval > EXT_COMMUNITY_OPAQUE_MAX) 1338 errx(1, "Bad ext-community value too big"); 1339 c->data1 = ullval >> 32; 1340 c->data2 = ullval; 1341 break; 1342 case EXT_COMMUNITY_NON_TRANS_OPAQUE: 1343 if (subtype == EXT_COMMUNITY_SUBTYPE_OVS) { 1344 if (strcmp(s, "valid") == 0) { 1345 c->data2 = EXT_COMMUNITY_OVS_VALID; 1346 break; 1347 } else if (strcmp(s, "invalid") == 0) { 1348 c->data2 = EXT_COMMUNITY_OVS_INVALID; 1349 break; 1350 } else if (strcmp(s, "not-found") == 0) { 1351 c->data2 = EXT_COMMUNITY_OVS_NOTFOUND; 1352 break; 1353 } else if (strcmp(s, "*") == 0) { 1354 dflag1 = COMMUNITY_ANY; 1355 break; 1356 } 1357 } 1358 errx(1, "Bad ext-community %s", s); 1359 } 1360 1361 c->data3 = type << 8 | subtype; 1362 1363 /* special handling of ext-community rt * since type is not known */ 1364 if (dflag1 == COMMUNITY_ANY && type == -1) { 1365 c->flags = COMMUNITY_TYPE_EXT; 1366 c->flags |= dflag1 << 8; 1367 return; 1368 } 1369 1370 /* verify type/subtype combo */ 1371 for (cp = iana_ext_comms; cp->subname != NULL; cp++) { 1372 if (cp->type == type && cp->subtype == subtype) { 1373 c->flags = COMMUNITY_TYPE_EXT; 1374 c->flags |= dflag1 << 8; 1375 c->flags |= dflag2 << 16; 1376 return; 1377 } 1378 } 1379 1380 errx(1, "Bad ext-community bad format for type"); 1381 } 1382 1383 int 1384 parse_nexthop(const char *word, struct parse_result *r) 1385 { 1386 struct filter_set *fs; 1387 1388 if ((fs = calloc(1, sizeof(struct filter_set))) == NULL) 1389 err(1, NULL); 1390 1391 if (strcmp(word, "blackhole") == 0) 1392 fs->type = ACTION_SET_NEXTHOP_BLACKHOLE; 1393 else if (strcmp(word, "reject") == 0) 1394 fs->type = ACTION_SET_NEXTHOP_REJECT; 1395 else if (strcmp(word, "no-modify") == 0) 1396 fs->type = ACTION_SET_NEXTHOP_NOMODIFY; 1397 else if (parse_addr(word, &fs->action.nexthop)) { 1398 fs->type = ACTION_SET_NEXTHOP; 1399 } else { 1400 free(fs); 1401 return (0); 1402 } 1403 1404 TAILQ_INSERT_TAIL(&r->set, fs, entry); 1405 return (1); 1406 } 1407