net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame^] | 1 | /* $USAGI: $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (C)2004 USAGI/WIDE Project |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | /* |
| 21 | * based on ip.c, iproute.c |
| 22 | */ |
| 23 | /* |
| 24 | * Authors: |
| 25 | * Masahide NAKAMURA @USAGI |
| 26 | */ |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <time.h> |
| 34 | #include <netdb.h> |
| 35 | #include <net/if.h> |
| 36 | #include <linux/netlink.h> |
| 37 | #include <linux/rtnetlink.h> |
| 38 | #include <linux/xfrm.h> |
| 39 | |
| 40 | #include "utils.h" |
| 41 | #include "xfrm.h" |
| 42 | |
| 43 | struct xfrm_filter filter; |
| 44 | |
| 45 | static void usage(void) __attribute__((noreturn)); |
| 46 | |
| 47 | static void usage(void) |
| 48 | { |
| 49 | fprintf(stderr, |
| 50 | "Usage: ip xfrm XFRM_OBJECT { COMMAND | help }\n" |
| 51 | "where XFRM_OBJECT := { state | policy }\n"); |
| 52 | exit(-1); |
| 53 | } |
| 54 | |
| 55 | const char *strxf_flags(__u8 flags) |
| 56 | { |
| 57 | static char str[16]; |
| 58 | const int sn = sizeof(flags) * 8 - 1; |
| 59 | __u8 b; |
| 60 | int i = 0; |
| 61 | |
| 62 | for (b = (1 << sn); b > 0; b >>= 1) |
| 63 | str[i++] = ((b & flags) ? '1' : '0'); |
| 64 | str[i] = '\0'; |
| 65 | |
| 66 | return str; |
| 67 | } |
| 68 | |
| 69 | const char *strxf_share(__u8 share) |
| 70 | { |
| 71 | static char str[32]; |
| 72 | |
| 73 | switch (share) { |
| 74 | case XFRM_SHARE_ANY: |
| 75 | strcpy(str, "any"); |
| 76 | break; |
| 77 | case XFRM_SHARE_SESSION: |
| 78 | strcpy(str, "session"); |
| 79 | break; |
| 80 | case XFRM_SHARE_USER: |
| 81 | strcpy(str, "user"); |
| 82 | break; |
| 83 | case XFRM_SHARE_UNIQUE: |
| 84 | strcpy(str, "unique"); |
| 85 | break; |
| 86 | default: |
| 87 | sprintf(str, "unknown-share(%d)", share); |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | return str; |
| 92 | } |
| 93 | |
| 94 | void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id, |
| 95 | __u8 mode, __u32 reqid, __u16 family, FILE *fp, |
| 96 | const char *prefix) |
| 97 | { |
| 98 | char abuf[256]; |
| 99 | __u32 spi; |
| 100 | struct protoent *pp; |
| 101 | char pbuf[32]; |
| 102 | char *p; |
| 103 | |
| 104 | if (prefix) |
| 105 | fprintf(fp, prefix); |
| 106 | |
| 107 | memset(abuf, '\0', sizeof(abuf)); |
| 108 | fprintf(fp, "%s ", rt_addr_n2a(family, sizeof(*saddr), |
| 109 | saddr, abuf, sizeof(abuf))); |
| 110 | memset(abuf, '\0', sizeof(abuf)); |
| 111 | fprintf(fp, "%s\n", rt_addr_n2a(family, sizeof(id->daddr), |
| 112 | &id->daddr, abuf, sizeof(abuf))); |
| 113 | |
| 114 | if (prefix) |
| 115 | fprintf(fp, prefix); |
| 116 | fprintf(fp, "\t"); |
| 117 | |
| 118 | pp = getprotobynumber(id->proto); |
| 119 | if (pp) |
| 120 | p = pp->p_name; |
| 121 | else { |
| 122 | sprintf(pbuf, "%d", id->proto); |
| 123 | p = pbuf; |
| 124 | } |
| 125 | |
| 126 | switch (id->proto) { |
| 127 | case IPPROTO_ESP: |
| 128 | case IPPROTO_AH: |
| 129 | case IPPROTO_COMP: |
| 130 | fprintf(fp, "%s ", p); |
| 131 | break; |
| 132 | default: |
| 133 | fprintf(fp, "unspec(%s)", p); |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | switch (id->proto) { |
| 138 | case IPPROTO_ESP: |
| 139 | case IPPROTO_AH: |
| 140 | case IPPROTO_COMP: |
| 141 | default: |
| 142 | spi = ntohl(id->spi); |
| 143 | fprintf(fp, "spi %d(0x%08x) ", spi, spi); |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | fprintf(fp, "reqid %d ", reqid); |
| 148 | fprintf(fp, "%s\n", (mode ? "tunnel" : "transport")); |
| 149 | } |
| 150 | |
| 151 | static const char *strxf_limit(__u64 limit) |
| 152 | { |
| 153 | static char str[32]; |
| 154 | if (limit == XFRM_INF) |
| 155 | strcpy(str, "(INF)"); |
| 156 | else |
| 157 | sprintf(str, "%llu", limit); |
| 158 | |
| 159 | return str; |
| 160 | } |
| 161 | |
| 162 | void xfrm_stats_print(struct xfrm_stats *s, FILE *fp, const char *prefix) |
| 163 | { |
| 164 | if (prefix) |
| 165 | fprintf(fp, prefix); |
| 166 | fprintf(fp, "stats:\n"); |
| 167 | |
| 168 | if (prefix) |
| 169 | fprintf(fp, prefix); |
| 170 | fprintf(fp, " "); |
| 171 | fprintf(fp, "replay-window %d ", s->replay_window); |
| 172 | fprintf(fp, "replay %d ", s->replay); |
| 173 | fprintf(fp, "failed %d", s->integrity_failed); |
| 174 | fprintf(fp, "\n"); |
| 175 | } |
| 176 | |
| 177 | static const char *strxf_time(__u64 time) |
| 178 | { |
| 179 | static char str[32]; |
| 180 | struct tm *tp; |
| 181 | time_t t; |
| 182 | |
| 183 | if (time == 0) { |
| 184 | strcpy(str, "(undefined)"); |
| 185 | } else { |
| 186 | /* XXX: treat time in the same manner of xfrm_{user,state}.c */ |
| 187 | t = (long)time; |
| 188 | tp = localtime(&t); |
| 189 | |
| 190 | sprintf(str, "%04d/%02d/%02d %02d:%02d:%02d", |
| 191 | tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday, |
| 192 | tp->tm_hour, tp->tm_min, tp->tm_sec); |
| 193 | } |
| 194 | |
| 195 | return str; |
| 196 | } |
| 197 | |
| 198 | void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg, |
| 199 | struct xfrm_lifetime_cur *cur, |
| 200 | FILE *fp, const char *prefix) |
| 201 | { |
| 202 | if (cfg) { |
| 203 | if (prefix) |
| 204 | fprintf(fp, prefix); |
| 205 | fprintf(fp, "lifetime config:\n"); |
| 206 | |
| 207 | if (prefix) |
| 208 | fprintf(fp, prefix); |
| 209 | fprintf(fp, " "); |
| 210 | fprintf(fp, "limit: "); |
| 211 | fprintf(fp, "soft "); |
| 212 | fprintf(fp, strxf_limit(cfg->soft_byte_limit)); |
| 213 | fprintf(fp, "(bytes), hard "); |
| 214 | fprintf(fp, strxf_limit(cfg->hard_byte_limit)); |
| 215 | fprintf(fp, "(bytes)\n"); |
| 216 | |
| 217 | if (prefix) |
| 218 | fprintf(fp, prefix); |
| 219 | fprintf(fp, " "); |
| 220 | fprintf(fp, "limit: "); |
| 221 | fprintf(fp, "soft "); |
| 222 | fprintf(fp, strxf_limit(cfg->soft_packet_limit)); |
| 223 | fprintf(fp, "(packets), hard "); |
| 224 | fprintf(fp, strxf_limit(cfg->hard_packet_limit)); |
| 225 | fprintf(fp, "(packets)\n"); |
| 226 | |
| 227 | if (prefix) |
| 228 | fprintf(fp, prefix); |
| 229 | fprintf(fp, " "); |
| 230 | fprintf(fp, "expire add: "); |
| 231 | fprintf(fp, "soft "); |
| 232 | fprintf(fp, "%llu", cfg->soft_add_expires_seconds); |
| 233 | fprintf(fp, "(sec), hard "); |
| 234 | fprintf(fp, "%llu", cfg->hard_add_expires_seconds); |
| 235 | fprintf(fp, "(sec)\n"); |
| 236 | |
| 237 | if (prefix) |
| 238 | fprintf(fp, prefix); |
| 239 | fprintf(fp, " "); |
| 240 | fprintf(fp, "expire use: "); |
| 241 | fprintf(fp, "soft "); |
| 242 | fprintf(fp, "%llu", cfg->soft_use_expires_seconds); |
| 243 | fprintf(fp, "(sec), hard "); |
| 244 | fprintf(fp, "%llu", cfg->hard_use_expires_seconds); |
| 245 | fprintf(fp, "(sec)\n"); |
| 246 | } |
| 247 | if (cur) { |
| 248 | if (prefix) |
| 249 | fprintf(fp, prefix); |
| 250 | fprintf(fp, "lifetime current:\n"); |
| 251 | |
| 252 | if (prefix) |
| 253 | fprintf(fp, prefix); |
| 254 | fprintf(fp, " "); |
| 255 | fprintf(fp, "%llu(bytes), ", cur->bytes); |
| 256 | fprintf(fp, "%llu(packets)\n", cur->packets); |
| 257 | if (prefix) |
| 258 | fprintf(fp, prefix); |
| 259 | fprintf(fp, " "); |
| 260 | fprintf(fp, "add %s ", strxf_time(cur->add_time)); |
| 261 | fprintf(fp, "use %s", strxf_time(cur->use_time)); |
| 262 | fprintf(fp, "\n"); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | void xfrm_selector_print(struct xfrm_selector *sel, __u16 family, |
| 267 | FILE *fp, const char *prefix) |
| 268 | { |
| 269 | char abuf[256]; |
| 270 | __u16 f; |
| 271 | |
| 272 | f = sel->family; |
| 273 | if (f == AF_UNSPEC) |
| 274 | f = family; |
| 275 | if (f == AF_UNSPEC) |
| 276 | f = preferred_family; |
| 277 | |
| 278 | if (prefix) |
| 279 | fprintf(fp, prefix); |
| 280 | |
| 281 | memset(abuf, '\0', sizeof(abuf)); |
| 282 | fprintf(fp, "%s/%d[%u]", rt_addr_n2a(f, sizeof(sel->saddr), |
| 283 | &sel->saddr, |
| 284 | abuf, sizeof(abuf)), |
| 285 | sel->prefixlen_s, sel->sport); |
| 286 | |
| 287 | memset(abuf, '\0', sizeof(abuf)); |
| 288 | fprintf(fp, " %s/%d[%u]", rt_addr_n2a(f, sizeof(sel->daddr), |
| 289 | &sel->daddr, |
| 290 | abuf, sizeof(abuf)), |
| 291 | sel->prefixlen_d, sel->dport); |
| 292 | |
| 293 | fprintf(fp, "\n"); |
| 294 | |
| 295 | if (prefix) |
| 296 | fprintf(fp, prefix); |
| 297 | fprintf(fp, "\t"); |
| 298 | |
| 299 | fprintf(fp, "upspec %u ", sel->proto); |
| 300 | |
| 301 | if (sel->ifindex > 0) { |
| 302 | char buf[IF_NAMESIZE]; |
| 303 | |
| 304 | memset(buf, '\0', sizeof(buf)); |
| 305 | if_indextoname(sel->ifindex, buf); |
| 306 | fprintf(fp, "dev %s ", buf); |
| 307 | } else |
| 308 | fprintf(fp, "dev (none) "); |
| 309 | |
| 310 | fprintf(fp, "uid %u", sel->user); |
| 311 | fprintf(fp, "\n"); |
| 312 | } |
| 313 | |
| 314 | static void xfrm_algo_print(struct xfrm_algo *algo, FILE *fp, |
| 315 | const char *prefix) |
| 316 | { |
| 317 | int len; |
| 318 | int i; |
| 319 | |
| 320 | if (prefix) |
| 321 | fprintf(fp, prefix); |
| 322 | |
| 323 | fprintf(fp, "%s", algo->alg_name); |
| 324 | |
| 325 | len = algo->alg_key_len / 8; |
| 326 | for (i = 0; i < len; i ++) { |
| 327 | if (i % 4 == 0) |
| 328 | fprintf(fp, " "); |
| 329 | fprintf(fp, "%x", algo->alg_key[i]); |
| 330 | } |
| 331 | |
| 332 | fprintf(fp, "\n"); |
| 333 | } |
| 334 | |
| 335 | static const char *strxf_mask(__u32 mask) |
| 336 | { |
| 337 | static char str[128]; |
| 338 | const int sn = sizeof(mask) * 8 - 1; |
| 339 | __u32 b; |
| 340 | int finish = 0; |
| 341 | int broken = 0; |
| 342 | int i = 0; |
| 343 | |
| 344 | for (b = (1 << sn); b > 0; b >>= 1) { |
| 345 | if ((b & mask) == 0) { |
| 346 | if (!finish) |
| 347 | finish = 1; |
| 348 | } else { |
| 349 | if (!finish) |
| 350 | i ++; |
| 351 | else { |
| 352 | broken = 1; |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if (!broken) |
| 359 | sprintf(str, "%u", i); |
| 360 | else |
| 361 | sprintf(str, "broken(%u)", mask); |
| 362 | |
| 363 | return str; |
| 364 | } |
| 365 | |
| 366 | static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int ntmpls, |
| 367 | __u16 family, FILE *fp, const char *prefix) |
| 368 | { |
| 369 | char buf[32]; |
| 370 | const char *p = NULL; |
| 371 | int i; |
| 372 | |
| 373 | if (prefix) { |
| 374 | strcpy(buf, prefix); |
| 375 | strcat(buf, " "); |
| 376 | } else |
| 377 | strcpy(buf, " "); |
| 378 | p = buf; |
| 379 | |
| 380 | for (i = 0; i < ntmpls; i++) { |
| 381 | struct xfrm_user_tmpl *tmpl = &tmpls[i]; |
| 382 | |
| 383 | if (prefix) |
| 384 | fprintf(fp, prefix); |
| 385 | fprintf(fp, "tmpl-%d:\n", i+1); |
| 386 | xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode, |
| 387 | tmpl->reqid, family, fp, p); |
| 388 | |
| 389 | fprintf(fp, p); |
| 390 | fprintf(fp, "\t"); |
| 391 | fprintf(fp, "level %s ", ((tmpl->optional == 0) ? "required" : |
| 392 | (tmpl->optional == 1) ? "use" : |
| 393 | "unknown-level")); |
| 394 | fprintf(fp, "share %s ", strxf_share(tmpl->share)); |
| 395 | fprintf(fp, "algo-mask:"); |
| 396 | fprintf(fp, "E=%s, ", strxf_mask(tmpl->ealgos)); |
| 397 | fprintf(fp, "A=%s, ", strxf_mask(tmpl->aalgos)); |
| 398 | fprintf(fp, "C=%s", strxf_mask(tmpl->calgos)); |
| 399 | fprintf(fp, "\n"); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | void xfrm_xfrma_print(struct rtattr *tb[], int ntb, __u16 family, |
| 404 | FILE *fp, const char *prefix) |
| 405 | { |
| 406 | int i; |
| 407 | |
| 408 | for (i = 0; i < ntb; i++) { |
| 409 | __u16 type = tb[i]->rta_type; |
| 410 | void *data = RTA_DATA(tb[i]); |
| 411 | |
| 412 | switch (type) { |
| 413 | case XFRMA_ALG_CRYPT: |
| 414 | if (prefix) |
| 415 | fprintf(fp, prefix); |
| 416 | xfrm_algo_print((struct xfrm_algo *)data, fp, "E:"); |
| 417 | break; |
| 418 | case XFRMA_ALG_AUTH: |
| 419 | if (prefix) |
| 420 | fprintf(fp, prefix); |
| 421 | xfrm_algo_print((struct xfrm_algo *)data, fp, "A:"); |
| 422 | break; |
| 423 | case XFRMA_ALG_COMP: |
| 424 | if (prefix) |
| 425 | fprintf(fp, prefix); |
| 426 | xfrm_algo_print((struct xfrm_algo *)data, fp, "C:"); |
| 427 | break; |
| 428 | case XFRMA_ENCAP: |
| 429 | if (prefix) |
| 430 | fprintf(fp, prefix); |
| 431 | /* XXX */ |
| 432 | fprintf(fp, "encap: (not implemented yet!)\n"); |
| 433 | break; |
| 434 | case XFRMA_TMPL: |
| 435 | { |
| 436 | int len = tb[i]->rta_len; |
| 437 | int ntmpls = len / sizeof(struct xfrm_user_tmpl); |
| 438 | |
| 439 | xfrm_tmpl_print((struct xfrm_user_tmpl *)data, |
| 440 | ntmpls, family, fp, prefix); |
| 441 | break; |
| 442 | } |
| 443 | default: |
| 444 | if (prefix) |
| 445 | fprintf(fp, prefix); |
| 446 | fprintf(fp, "unknown rta_type: %u\n", type); |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family, |
| 453 | int *argcp, char ***argvp) |
| 454 | { |
| 455 | int argc = *argcp; |
| 456 | char **argv = *argvp; |
| 457 | inet_prefix dst; |
| 458 | inet_prefix src; |
| 459 | __u8 proto = 0; |
| 460 | |
| 461 | memset(&dst, 0, sizeof(dst)); |
| 462 | memset(&src, 0, sizeof(src)); |
| 463 | |
| 464 | while (1) { |
| 465 | if (strcmp(*argv, "src") == 0) { |
| 466 | NEXT_ARG(); |
| 467 | |
| 468 | get_prefix(&src, *argv, preferred_family); |
| 469 | if (src.family == AF_UNSPEC) |
| 470 | invarg("\"SADDR\" address family is AF_UNSPEC", *argv); |
| 471 | if (family) |
| 472 | *family = src.family; |
| 473 | |
| 474 | memcpy(saddr, &src.data, sizeof(*saddr)); |
| 475 | |
| 476 | filter.id_src_mask = src.bitlen; |
| 477 | |
| 478 | } else if (strcmp(*argv, "dst") == 0) { |
| 479 | NEXT_ARG(); |
| 480 | |
| 481 | get_prefix(&dst, *argv, preferred_family); |
| 482 | if (dst.family == AF_UNSPEC) |
| 483 | invarg("\"DADDR\" address family is AF_UNSPEC", *argv); |
| 484 | if (family) |
| 485 | *family = dst.family; |
| 486 | |
| 487 | memcpy(&id->daddr, &dst.data, sizeof(id->daddr)); |
| 488 | |
| 489 | filter.id_dst_mask = dst.bitlen; |
| 490 | |
| 491 | } else if (strcmp(*argv, "proto") == 0) { |
| 492 | struct protoent *pp; |
| 493 | |
| 494 | NEXT_ARG(); |
| 495 | |
| 496 | pp = getprotobyname(*argv); |
| 497 | if (pp) |
| 498 | proto = pp->p_proto; |
| 499 | else { |
| 500 | if (get_u8(&proto, *argv, 0)) |
| 501 | invarg("\"PROTO\" is invalid", *argv); |
| 502 | } |
| 503 | |
| 504 | switch (proto) { |
| 505 | case IPPROTO_ESP: |
| 506 | case IPPROTO_AH: |
| 507 | case IPPROTO_COMP: |
| 508 | id->proto = proto; |
| 509 | break; |
| 510 | default: |
| 511 | invarg("\"PROTO\" is unsuppored proto", *argv); |
| 512 | } |
| 513 | |
| 514 | filter.id_proto_mask = XFRM_FILTER_MASK_FULL; |
| 515 | |
| 516 | } else if (strcmp(*argv, "spi") == 0) { |
| 517 | __u32 spi; |
| 518 | |
| 519 | NEXT_ARG(); |
| 520 | if (get_u32(&spi, *argv, 0)) |
| 521 | invarg("\"SPI\" is invalid", *argv); |
| 522 | |
| 523 | spi = htonl(spi); |
| 524 | id->spi = spi; |
| 525 | |
| 526 | filter.id_spi_mask = XFRM_FILTER_MASK_FULL; |
| 527 | |
| 528 | } else { |
| 529 | PREV_ARG(); /* back track */ |
| 530 | break; |
| 531 | } |
| 532 | |
| 533 | if (!NEXT_ARG_OK()) |
| 534 | break; |
| 535 | NEXT_ARG(); |
| 536 | } |
| 537 | |
| 538 | if (src.family && dst.family && (src.family != dst.family)) |
| 539 | invarg("the same address family is required between \"SADDR\" and \"DADDR\"", *argv); |
| 540 | if (proto == 0) |
| 541 | missarg("PROTO"); |
| 542 | |
| 543 | if (argc == *argcp) |
| 544 | missarg("ID"); |
| 545 | |
| 546 | *argcp = argc; |
| 547 | *argvp = argv; |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp) |
| 553 | { |
| 554 | int argc = *argcp; |
| 555 | char **argv = *argvp; |
| 556 | |
| 557 | if (matches(*argv, "transport") == 0) |
| 558 | *mode = 0; |
| 559 | else if (matches(*argv, "tunnel") == 0) |
| 560 | *mode = 1; |
| 561 | else |
| 562 | invarg("\"MODE\" is invalid", *argv); |
| 563 | |
| 564 | *argcp = argc; |
| 565 | *argvp = argv; |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | /* NOTE: reqid is used by host-byte order */ |
| 571 | int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp) |
| 572 | { |
| 573 | int argc = *argcp; |
| 574 | char **argv = *argvp; |
| 575 | |
| 576 | if (get_u32(reqid, *argv, 0)) |
| 577 | invarg("\"REQID\" is invalid", *argv); |
| 578 | |
| 579 | *argcp = argc; |
| 580 | *argvp = argv; |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | static int xfrm_selector_upspec_parse(struct xfrm_selector *sel, |
| 586 | int *argcp, char ***argvp) |
| 587 | { |
| 588 | int argc = *argcp; |
| 589 | char **argv = *argvp; |
| 590 | __u8 upspec; |
| 591 | |
| 592 | while (1) { |
| 593 | if (strcmp(*argv, "proto") == 0) { |
| 594 | NEXT_ARG(); |
| 595 | |
| 596 | if (strcmp(*argv, "any") == 0) |
| 597 | upspec = 0; |
| 598 | else { |
| 599 | struct protoent *pp; |
| 600 | pp = getprotobyname(*argv); |
| 601 | if (pp) |
| 602 | upspec = pp->p_proto; |
| 603 | else { |
| 604 | if (get_u8(&upspec, *argv, 0)) |
| 605 | invarg("\"UPSPEC\" is invalid", *argv); |
| 606 | } |
| 607 | } |
| 608 | sel->proto = upspec; |
| 609 | |
| 610 | filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL; |
| 611 | |
| 612 | } else if (strcmp(*argv, "sport") == 0) { |
| 613 | NEXT_ARG(); |
| 614 | |
| 615 | if (get_u16(&sel->sport, *argv, 0)) |
| 616 | invarg("\"PORT\" is invalid", *argv); |
| 617 | sel->sport = htons(sel->sport); |
| 618 | if (sel->sport) |
| 619 | sel->sport_mask = ~((__u16)0); |
| 620 | |
| 621 | filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL; |
| 622 | |
| 623 | } else if (strcmp(*argv, "dport") == 0) { |
| 624 | NEXT_ARG(); |
| 625 | |
| 626 | if (get_u16(&sel->dport, *argv, 0)) |
| 627 | invarg("\"PORT\" is invalid", *argv); |
| 628 | sel->dport = htons(sel->dport); |
| 629 | if (sel->dport) |
| 630 | sel->dport_mask = ~((__u16)0); |
| 631 | |
| 632 | filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL; |
| 633 | |
| 634 | } else { |
| 635 | PREV_ARG(); /* back track */ |
| 636 | break; |
| 637 | } |
| 638 | |
| 639 | if (!NEXT_ARG_OK()) |
| 640 | break; |
| 641 | NEXT_ARG(); |
| 642 | } |
| 643 | if (argc == *argcp) |
| 644 | missarg("UPSPEC"); |
| 645 | |
| 646 | *argcp = argc; |
| 647 | *argvp = argv; |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp) |
| 653 | { |
| 654 | int argc = *argcp; |
| 655 | char **argv = *argvp; |
| 656 | inet_prefix dst; |
| 657 | inet_prefix src; |
| 658 | |
| 659 | memset(&dst, 0, sizeof(dst)); |
| 660 | memset(&src, 0, sizeof(src)); |
| 661 | |
| 662 | while (1) { |
| 663 | if (strcmp(*argv, "src") == 0) { |
| 664 | NEXT_ARG(); |
| 665 | |
| 666 | get_prefix(&src, *argv, preferred_family); |
| 667 | if (src.family == AF_UNSPEC) |
| 668 | invarg("\"SADDR\" address family is AF_UNSPEC", *argv); |
| 669 | sel->family = src.family; |
| 670 | |
| 671 | memcpy(&sel->saddr, &src.data, sizeof(sel->saddr)); |
| 672 | sel->prefixlen_s = src.bitlen; |
| 673 | |
| 674 | filter.sel_src_mask = src.bitlen; |
| 675 | |
| 676 | } else if (strcmp(*argv, "dst") == 0) { |
| 677 | NEXT_ARG(); |
| 678 | |
| 679 | get_prefix(&dst, *argv, preferred_family); |
| 680 | if (dst.family == AF_UNSPEC) |
| 681 | invarg("\"DADDR\" address family is AF_UNSPEC", *argv); |
| 682 | sel->family = dst.family; |
| 683 | |
| 684 | memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr)); |
| 685 | sel->prefixlen_d = dst.bitlen; |
| 686 | |
| 687 | filter.sel_dst_mask = dst.bitlen; |
| 688 | |
| 689 | } else if (strcmp(*argv, "upspec") == 0) { |
| 690 | NEXT_ARG(); |
| 691 | |
| 692 | xfrm_selector_upspec_parse(sel, &argc, &argv); |
| 693 | |
| 694 | } else if (strcmp(*argv, "dev") == 0) { |
| 695 | int ifindex; |
| 696 | |
| 697 | NEXT_ARG(); |
| 698 | |
| 699 | if (strcmp(*argv, "none") == 0) |
| 700 | ifindex = 0; |
| 701 | else { |
| 702 | ifindex = if_nametoindex(*argv); |
| 703 | if (ifindex <= 0) |
| 704 | invarg("\"DEV\" is invalid", *argv); |
| 705 | } |
| 706 | sel->ifindex = ifindex; |
| 707 | |
| 708 | filter.sel_dev_mask = XFRM_FILTER_MASK_FULL; |
| 709 | |
| 710 | } else { |
| 711 | PREV_ARG(); /* back track */ |
| 712 | break; |
| 713 | } |
| 714 | |
| 715 | if (!NEXT_ARG_OK()) |
| 716 | break; |
| 717 | |
| 718 | NEXT_ARG(); |
| 719 | } |
| 720 | |
| 721 | if (src.family && dst.family && (src.family != dst.family)) |
| 722 | invarg("the same address family is required between \"SADDR\" and \"DADDR\"", *argv); |
| 723 | |
| 724 | if (argc == *argcp) |
| 725 | missarg("SELECTOR"); |
| 726 | |
| 727 | *argcp = argc; |
| 728 | *argvp = argv; |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft, |
| 734 | int *argcp, char ***argvp) |
| 735 | { |
| 736 | int argc = *argcp; |
| 737 | char **argv = *argvp; |
| 738 | int ret; |
| 739 | |
| 740 | if (strcmp(*argv, "time-soft") == 0) { |
| 741 | NEXT_ARG(); |
| 742 | ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0); |
| 743 | if (ret) |
| 744 | invarg("\"time-soft\" value is invalid", *argv); |
| 745 | } else if (strcmp(*argv, "time-hard") == 0) { |
| 746 | NEXT_ARG(); |
| 747 | ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0); |
| 748 | if (ret) |
| 749 | invarg("\"time-hard\" value is invalid", *argv); |
| 750 | } else if (strcmp(*argv, "time-use-soft") == 0) { |
| 751 | NEXT_ARG(); |
| 752 | ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0); |
| 753 | if (ret) |
| 754 | invarg("\"time-use-soft\" value is invalid", *argv); |
| 755 | } else if (strcmp(*argv, "time-use-hard") == 0) { |
| 756 | NEXT_ARG(); |
| 757 | ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0); |
| 758 | if (ret) |
| 759 | invarg("\"time-use-hard\" value is invalid", *argv); |
| 760 | } else if (strcmp(*argv, "byte-soft") == 0) { |
| 761 | NEXT_ARG(); |
| 762 | ret = get_u64(&lft->soft_byte_limit, *argv, 0); |
| 763 | if (ret) |
| 764 | invarg("\"byte-soft\" value is invalid", *argv); |
| 765 | } else if (strcmp(*argv, "byte-hard") == 0) { |
| 766 | NEXT_ARG(); |
| 767 | ret = get_u64(&lft->hard_byte_limit, *argv, 0); |
| 768 | if (ret) |
| 769 | invarg("\"byte-hard\" value is invalid", *argv); |
| 770 | } else if (strcmp(*argv, "packet-soft") == 0) { |
| 771 | NEXT_ARG(); |
| 772 | ret = get_u64(&lft->soft_packet_limit, *argv, 0); |
| 773 | if (ret) |
| 774 | invarg("\"packet-soft\" value is invalid", *argv); |
| 775 | } else if (strcmp(*argv, "packet-hard") == 0) { |
| 776 | NEXT_ARG(); |
| 777 | ret = get_u64(&lft->hard_packet_limit, *argv, 0); |
| 778 | if (ret) |
| 779 | invarg("\"packet-hard\" value is invalid", *argv); |
| 780 | } else |
| 781 | invarg("\"LIMIT\" is invalid", *argv); |
| 782 | |
| 783 | *argcp = argc; |
| 784 | *argvp = argv; |
| 785 | |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | int do_xfrm(int argc, char **argv) |
| 790 | { |
| 791 | memset(&filter, 0, sizeof(filter)); |
| 792 | |
| 793 | if (argc < 1) |
| 794 | usage(); |
| 795 | |
| 796 | if (strcmp(*argv, "state") == 0 || |
| 797 | strcmp(*argv, "sa") == 0) { |
| 798 | return do_xfrm_state(argc-1, argv+1); |
| 799 | } else if (strcmp(*argv, "policy") == 0 || |
| 800 | strcmp(*argv, "pol") == 0) { |
| 801 | return do_xfrm_policy(argc-1, argv+1); |
| 802 | } else if (strcmp(*argv, "help") == 0) { |
| 803 | usage(); |
| 804 | fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv); |
| 805 | exit(-1); |
| 806 | } |
| 807 | usage(); |
| 808 | } |