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