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, see <http://www.gnu.org/licenses>. 18 */ 19 /* 20 * based on iproute.c 21 */ 22 /* 23 * Authors: 24 * Masahide NAKAMURA @USAGI 25 */ 26 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <netdb.h> 31 #include <linux/netlink.h> 32 #include "utils.h" 33 #include "xfrm.h" 34 #include "ip_common.h" 35 36 //#define NLMSG_DELETEALL_BUF_SIZE (4096-512) 37 #define NLMSG_DELETEALL_BUF_SIZE 8192 38 39 /* 40 * Receiving buffer defines: 41 * nlmsg 42 * data = struct xfrm_userpolicy_info 43 * rtattr 44 * data = struct xfrm_user_tmpl[] 45 */ 46 #define NLMSG_BUF_SIZE 4096 47 #define RTA_BUF_SIZE 2048 48 #define XFRM_TMPLS_BUF_SIZE 1024 49 #define CTX_BUF_SIZE 256 50 51 static void usage(void) __attribute__((noreturn)); 52 53 static void usage(void) 54 { 55 fprintf(stderr, "Usage: ip xfrm policy { add | update } SELECTOR dir DIR [ ctx CTX ]\n"); 56 fprintf(stderr, " [ mark MARK [ mask MASK ] ] [ index INDEX ] [ ptype PTYPE ]\n"); 57 fprintf(stderr, " [ action ACTION ] [ priority PRIORITY ] [ flag FLAG-LIST ]\n"); 58 fprintf(stderr, " [ LIMIT-LIST ] [ TMPL-LIST ]\n"); 59 fprintf(stderr, "Usage: ip xfrm policy { delete | get } { SELECTOR | index INDEX } dir DIR\n"); 60 fprintf(stderr, " [ ctx CTX ] [ mark MARK [ mask MASK ] ] [ ptype PTYPE ]\n"); 61 fprintf(stderr, "Usage: ip xfrm policy { deleteall | list } [ SELECTOR ] [ dir DIR ]\n"); 62 fprintf(stderr, " [ index INDEX ] [ ptype PTYPE ] [ action ACTION ] [ priority PRIORITY ]\n"); 63 fprintf(stderr, " [ flag FLAG-LIST ]\n"); 64 fprintf(stderr, "Usage: ip xfrm policy flush [ ptype PTYPE ]\n"); 65 fprintf(stderr, "Usage: ip xfrm policy count\n"); 66 fprintf(stderr, "Usage: ip xfrm policy set [ hthresh4 LBITS RBITS ] [ hthresh6 LBITS RBITS ]\n"); 67 fprintf(stderr, "SELECTOR := [ src ADDR[/PLEN] ] [ dst ADDR[/PLEN] ] [ dev DEV ] [ UPSPEC ]\n"); 68 fprintf(stderr, "UPSPEC := proto { { "); 69 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_TCP)); 70 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_UDP)); 71 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_SCTP)); 72 fprintf(stderr, "%s", strxf_proto(IPPROTO_DCCP)); 73 fprintf(stderr, " } [ sport PORT ] [ dport PORT ] |\n"); 74 fprintf(stderr, " { "); 75 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_ICMP)); 76 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_ICMPV6)); 77 fprintf(stderr, "%s", strxf_proto(IPPROTO_MH)); 78 fprintf(stderr, " } [ type NUMBER ] [ code NUMBER ] |\n"); 79 fprintf(stderr, " %s", strxf_proto(IPPROTO_GRE)); 80 fprintf(stderr, " [ key { DOTTED-QUAD | NUMBER } ] | PROTO }\n"); 81 fprintf(stderr, "DIR := in | out | fwd\n"); 82 fprintf(stderr, "PTYPE := main | sub\n"); 83 fprintf(stderr, "ACTION := allow | block\n"); 84 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n"); 85 fprintf(stderr, "FLAG := localok | icmp\n"); 86 fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] limit LIMIT\n"); 87 fprintf(stderr, "LIMIT := { time-soft | time-hard | time-use-soft | time-use-hard } SECONDS |\n"); 88 fprintf(stderr, " { byte-soft | byte-hard } SIZE | { packet-soft | packet-hard } COUNT\n"); 89 fprintf(stderr, "TMPL-LIST := [ TMPL-LIST ] tmpl TMPL\n"); 90 fprintf(stderr, "TMPL := ID [ mode MODE ] [ reqid REQID ] [ level LEVEL ]\n"); 91 fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ]\n"); 92 fprintf(stderr, "XFRM-PROTO := "); 93 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP)); 94 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH)); 95 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_COMP)); 96 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING)); 97 fprintf(stderr, "%s\n", strxf_xfrmproto(IPPROTO_DSTOPTS)); 98 fprintf(stderr, "MODE := transport | tunnel | beet | ro | in_trigger\n"); 99 fprintf(stderr, "LEVEL := required | use\n"); 100 101 exit(-1); 102 } 103 104 static int xfrm_policy_dir_parse(__u8 *dir, int *argcp, char ***argvp) 105 { 106 int argc = *argcp; 107 char **argv = *argvp; 108 109 if (strcmp(*argv, "in") == 0) 110 *dir = XFRM_POLICY_IN; 111 else if (strcmp(*argv, "out") == 0) 112 *dir = XFRM_POLICY_OUT; 113 else if (strcmp(*argv, "fwd") == 0) 114 *dir = XFRM_POLICY_FWD; 115 else 116 invarg("DIR value is invalid", *argv); 117 118 *argcp = argc; 119 *argvp = argv; 120 121 return 0; 122 } 123 124 static int xfrm_policy_ptype_parse(__u8 *ptype, int *argcp, char ***argvp) 125 { 126 int argc = *argcp; 127 char **argv = *argvp; 128 129 if (strcmp(*argv, "main") == 0) 130 *ptype = XFRM_POLICY_TYPE_MAIN; 131 else if (strcmp(*argv, "sub") == 0) 132 *ptype = XFRM_POLICY_TYPE_SUB; 133 else 134 invarg("PTYPE value is invalid", *argv); 135 136 *argcp = argc; 137 *argvp = argv; 138 139 return 0; 140 } 141 142 static int xfrm_policy_flag_parse(__u8 *flags, int *argcp, char ***argvp) 143 { 144 int argc = *argcp; 145 char **argv = *argvp; 146 int len = strlen(*argv); 147 148 if (len > 2 && strncmp(*argv, "0x", 2) == 0) { 149 __u8 val = 0; 150 151 if (get_u8(&val, *argv, 16)) 152 invarg("FLAG value is invalid", *argv); 153 *flags = val; 154 } else { 155 while (1) { 156 if (strcmp(*argv, "localok") == 0) 157 *flags |= XFRM_POLICY_LOCALOK; 158 else if (strcmp(*argv, "icmp") == 0) 159 *flags |= XFRM_POLICY_ICMP; 160 else { 161 PREV_ARG(); /* back track */ 162 break; 163 } 164 165 if (!NEXT_ARG_OK()) 166 break; 167 NEXT_ARG(); 168 } 169 } 170 171 *argcp = argc; 172 *argvp = argv; 173 174 return 0; 175 } 176 177 static int xfrm_tmpl_parse(struct xfrm_user_tmpl *tmpl, 178 int *argcp, char ***argvp) 179 { 180 int argc = *argcp; 181 char **argv = *argvp; 182 char *idp = NULL; 183 184 while (1) { 185 if (strcmp(*argv, "mode") == 0) { 186 NEXT_ARG(); 187 xfrm_mode_parse(&tmpl->mode, &argc, &argv); 188 } else if (strcmp(*argv, "reqid") == 0) { 189 NEXT_ARG(); 190 xfrm_reqid_parse(&tmpl->reqid, &argc, &argv); 191 } else if (strcmp(*argv, "level") == 0) { 192 NEXT_ARG(); 193 194 if (strcmp(*argv, "required") == 0) 195 tmpl->optional = 0; 196 else if (strcmp(*argv, "use") == 0) 197 tmpl->optional = 1; 198 else 199 invarg("LEVEL value is invalid\n", *argv); 200 201 } else { 202 if (idp) { 203 PREV_ARG(); /* back track */ 204 break; 205 } 206 idp = *argv; 207 preferred_family = AF_UNSPEC; 208 xfrm_id_parse(&tmpl->saddr, &tmpl->id, &tmpl->family, 209 0, &argc, &argv); 210 preferred_family = tmpl->family; 211 } 212 213 if (!NEXT_ARG_OK()) 214 break; 215 216 NEXT_ARG(); 217 } 218 if (argc == *argcp) 219 missarg("TMPL"); 220 221 *argcp = argc; 222 *argvp = argv; 223 224 return 0; 225 } 226 227 int xfrm_sctx_parse(char *ctxstr, char *s, 228 struct xfrm_user_sec_ctx *sctx) 229 { 230 int slen; 231 232 slen = strlen(s) + 1; 233 234 sctx->exttype = XFRMA_SEC_CTX; 235 sctx->ctx_doi = 1; 236 sctx->ctx_alg = 1; 237 sctx->ctx_len = slen; 238 sctx->len = sizeof(struct xfrm_user_sec_ctx) + slen; 239 memcpy(ctxstr, s, slen); 240 241 return 0; 242 } 243 244 static int xfrm_policy_modify(int cmd, unsigned flags, int argc, char **argv) 245 { 246 struct rtnl_handle rth; 247 struct { 248 struct nlmsghdr n; 249 struct xfrm_userpolicy_info xpinfo; 250 char buf[RTA_BUF_SIZE]; 251 } req; 252 char *dirp = NULL; 253 char *selp = NULL; 254 char *ptypep = NULL; 255 char *sctxp = NULL; 256 struct xfrm_userpolicy_type upt; 257 char tmpls_buf[XFRM_TMPLS_BUF_SIZE]; 258 int tmpls_len = 0; 259 struct xfrm_mark mark = {0, 0}; 260 struct { 261 struct xfrm_user_sec_ctx sctx; 262 char str[CTX_BUF_SIZE]; 263 } ctx; 264 265 memset(&req, 0, sizeof(req)); 266 memset(&upt, 0, sizeof(upt)); 267 memset(&tmpls_buf, 0, sizeof(tmpls_buf)); 268 memset(&ctx, 0, sizeof(ctx)); 269 270 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpinfo)); 271 req.n.nlmsg_flags = NLM_F_REQUEST|flags; 272 req.n.nlmsg_type = cmd; 273 req.xpinfo.sel.family = preferred_family; 274 275 req.xpinfo.lft.soft_byte_limit = XFRM_INF; 276 req.xpinfo.lft.hard_byte_limit = XFRM_INF; 277 req.xpinfo.lft.soft_packet_limit = XFRM_INF; 278 req.xpinfo.lft.hard_packet_limit = XFRM_INF; 279 280 while (argc > 0) { 281 if (strcmp(*argv, "dir") == 0) { 282 if (dirp) 283 duparg("dir", *argv); 284 dirp = *argv; 285 286 NEXT_ARG(); 287 xfrm_policy_dir_parse(&req.xpinfo.dir, &argc, &argv); 288 } else if (strcmp(*argv, "ctx") == 0) { 289 char *context; 290 291 if (sctxp) 292 duparg("ctx", *argv); 293 sctxp = *argv; 294 NEXT_ARG(); 295 context = *argv; 296 xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx); 297 } else if (strcmp(*argv, "mark") == 0) { 298 xfrm_parse_mark(&mark, &argc, &argv); 299 } else if (strcmp(*argv, "index") == 0) { 300 NEXT_ARG(); 301 if (get_u32(&req.xpinfo.index, *argv, 0)) 302 invarg("INDEX value is invalid", *argv); 303 } else if (strcmp(*argv, "ptype") == 0) { 304 if (ptypep) 305 duparg("ptype", *argv); 306 ptypep = *argv; 307 308 NEXT_ARG(); 309 xfrm_policy_ptype_parse(&upt.type, &argc, &argv); 310 } else if (strcmp(*argv, "action") == 0) { 311 NEXT_ARG(); 312 if (strcmp(*argv, "allow") == 0) 313 req.xpinfo.action = XFRM_POLICY_ALLOW; 314 else if (strcmp(*argv, "block") == 0) 315 req.xpinfo.action = XFRM_POLICY_BLOCK; 316 else 317 invarg("ACTION value is invalid\n", *argv); 318 } else if (strcmp(*argv, "priority") == 0) { 319 NEXT_ARG(); 320 if (get_u32(&req.xpinfo.priority, *argv, 0)) 321 invarg("PRIORITY value is invalid", *argv); 322 } else if (strcmp(*argv, "flag") == 0) { 323 NEXT_ARG(); 324 xfrm_policy_flag_parse(&req.xpinfo.flags, &argc, 325 &argv); 326 } else if (strcmp(*argv, "limit") == 0) { 327 NEXT_ARG(); 328 xfrm_lifetime_cfg_parse(&req.xpinfo.lft, &argc, &argv); 329 } else if (strcmp(*argv, "tmpl") == 0) { 330 struct xfrm_user_tmpl *tmpl; 331 332 if (tmpls_len + sizeof(*tmpl) > sizeof(tmpls_buf)) { 333 fprintf(stderr, "Too many tmpls: buffer overflow\n"); 334 exit(1); 335 } 336 tmpl = (struct xfrm_user_tmpl *)((char *)tmpls_buf + tmpls_len); 337 338 tmpl->family = preferred_family; 339 tmpl->aalgos = (~(__u32)0); 340 tmpl->ealgos = (~(__u32)0); 341 tmpl->calgos = (~(__u32)0); 342 343 NEXT_ARG(); 344 xfrm_tmpl_parse(tmpl, &argc, &argv); 345 346 tmpls_len += sizeof(*tmpl); 347 } else { 348 if (selp) 349 duparg("unknown", *argv); 350 selp = *argv; 351 352 xfrm_selector_parse(&req.xpinfo.sel, &argc, &argv); 353 if (preferred_family == AF_UNSPEC) 354 preferred_family = req.xpinfo.sel.family; 355 } 356 357 argc--; argv++; 358 } 359 360 if (!dirp) { 361 fprintf(stderr, "Not enough information: DIR is required.\n"); 362 exit(1); 363 } 364 365 if (ptypep) { 366 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE, 367 (void *)&upt, sizeof(upt)); 368 } 369 370 if (tmpls_len > 0) { 371 addattr_l(&req.n, sizeof(req), XFRMA_TMPL, 372 (void *)tmpls_buf, tmpls_len); 373 } 374 375 if (mark.m) { 376 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK, 377 (void *)&mark, sizeof(mark)); 378 if (r < 0) { 379 fprintf(stderr, "%s: XFRMA_MARK failed\n",__func__); 380 exit(1); 381 } 382 } 383 384 if (sctxp) { 385 addattr_l(&req.n, sizeof(req), XFRMA_SEC_CTX, 386 (void *)&ctx, ctx.sctx.len); 387 } 388 389 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) 390 exit(1); 391 392 if (req.xpinfo.sel.family == AF_UNSPEC) 393 req.xpinfo.sel.family = AF_INET; 394 395 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) 396 exit(2); 397 398 rtnl_close(&rth); 399 400 return 0; 401 } 402 403 static int xfrm_policy_filter_match(struct xfrm_userpolicy_info *xpinfo, 404 __u8 ptype) 405 { 406 if (!filter.use) 407 return 1; 408 409 if ((xpinfo->dir^filter.xpinfo.dir)&filter.dir_mask) 410 return 0; 411 412 if ((ptype^filter.ptype)&filter.ptype_mask) 413 return 0; 414 415 if (filter.sel_src_mask) { 416 if (xfrm_addr_match(&xpinfo->sel.saddr, &filter.xpinfo.sel.saddr, 417 filter.sel_src_mask)) 418 return 0; 419 } 420 421 if (filter.sel_dst_mask) { 422 if (xfrm_addr_match(&xpinfo->sel.daddr, &filter.xpinfo.sel.daddr, 423 filter.sel_dst_mask)) 424 return 0; 425 } 426 427 if ((xpinfo->sel.ifindex^filter.xpinfo.sel.ifindex)&filter.sel_dev_mask) 428 return 0; 429 430 if ((xpinfo->sel.proto^filter.xpinfo.sel.proto)&filter.upspec_proto_mask) 431 return 0; 432 433 if (filter.upspec_sport_mask) { 434 if ((xpinfo->sel.sport^filter.xpinfo.sel.sport)&filter.upspec_sport_mask) 435 return 0; 436 } 437 438 if (filter.upspec_dport_mask) { 439 if ((xpinfo->sel.dport^filter.xpinfo.sel.dport)&filter.upspec_dport_mask) 440 return 0; 441 } 442 443 if ((xpinfo->index^filter.xpinfo.index)&filter.index_mask) 444 return 0; 445 446 if ((xpinfo->action^filter.xpinfo.action)&filter.action_mask) 447 return 0; 448 449 if ((xpinfo->priority^filter.xpinfo.priority)&filter.priority_mask) 450 return 0; 451 452 if (filter.policy_flags_mask) 453 if ((xpinfo->flags & filter.xpinfo.flags) == 0) 454 return 0; 455 456 return 1; 457 } 458 459 int xfrm_policy_print(const struct sockaddr_nl *who, struct nlmsghdr *n, 460 void *arg) 461 { 462 struct rtattr * tb[XFRMA_MAX+1]; 463 struct rtattr * rta; 464 struct xfrm_userpolicy_info *xpinfo = NULL; 465 struct xfrm_user_polexpire *xpexp = NULL; 466 struct xfrm_userpolicy_id *xpid = NULL; 467 __u8 ptype = XFRM_POLICY_TYPE_MAIN; 468 FILE *fp = (FILE*)arg; 469 int len = n->nlmsg_len; 470 471 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY && 472 n->nlmsg_type != XFRM_MSG_DELPOLICY && 473 n->nlmsg_type != XFRM_MSG_UPDPOLICY && 474 n->nlmsg_type != XFRM_MSG_POLEXPIRE) { 475 fprintf(stderr, "Not a policy: %08x %08x %08x\n", 476 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); 477 return 0; 478 } 479 480 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) { 481 xpid = NLMSG_DATA(n); 482 len -= NLMSG_SPACE(sizeof(*xpid)); 483 } else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) { 484 xpexp = NLMSG_DATA(n); 485 xpinfo = &xpexp->pol; 486 len -= NLMSG_SPACE(sizeof(*xpexp)); 487 } else { 488 xpexp = NULL; 489 xpinfo = NLMSG_DATA(n); 490 len -= NLMSG_SPACE(sizeof(*xpinfo)); 491 } 492 493 if (len < 0) { 494 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); 495 return -1; 496 } 497 498 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) 499 rta = XFRMPID_RTA(xpid); 500 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) 501 rta = XFRMPEXP_RTA(xpexp); 502 else 503 rta = XFRMP_RTA(xpinfo); 504 505 parse_rtattr(tb, XFRMA_MAX, rta, len); 506 507 if (tb[XFRMA_POLICY_TYPE]) { 508 struct xfrm_userpolicy_type *upt; 509 510 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) { 511 fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n"); 512 return -1; 513 } 514 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]); 515 ptype = upt->type; 516 } 517 518 if (xpinfo && !xfrm_policy_filter_match(xpinfo, ptype)) 519 return 0; 520 521 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) 522 fprintf(fp, "Deleted "); 523 else if (n->nlmsg_type == XFRM_MSG_UPDPOLICY) 524 fprintf(fp, "Updated "); 525 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) 526 fprintf(fp, "Expired "); 527 528 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) { 529 //xfrm_policy_id_print(); 530 if (!tb[XFRMA_POLICY]) { 531 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: no XFRMA_POLICY\n"); 532 return -1; 533 } 534 if (RTA_PAYLOAD(tb[XFRMA_POLICY]) < sizeof(*xpinfo)) { 535 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n"); 536 return -1; 537 } 538 xpinfo = (struct xfrm_userpolicy_info *)RTA_DATA(tb[XFRMA_POLICY]); 539 } 540 541 xfrm_policy_info_print(xpinfo, tb, fp, NULL, NULL); 542 543 if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) { 544 fprintf(fp, "\t"); 545 fprintf(fp, "hard %u", xpexp->hard); 546 fprintf(fp, "%s", _SL_); 547 } 548 549 if (oneline) 550 fprintf(fp, "\n"); 551 fflush(fp); 552 553 return 0; 554 } 555 556 static int xfrm_policy_get_or_delete(int argc, char **argv, int delete, 557 void *res_nlbuf, size_t res_size) 558 { 559 struct rtnl_handle rth; 560 struct { 561 struct nlmsghdr n; 562 struct xfrm_userpolicy_id xpid; 563 char buf[RTA_BUF_SIZE]; 564 } req; 565 char *dirp = NULL; 566 char *selp = NULL; 567 char *indexp = NULL; 568 char *ptypep = NULL; 569 char *sctxp = NULL; 570 struct xfrm_userpolicy_type upt; 571 struct xfrm_mark mark = {0, 0}; 572 struct { 573 struct xfrm_user_sec_ctx sctx; 574 char str[CTX_BUF_SIZE]; 575 } ctx; 576 577 578 memset(&req, 0, sizeof(req)); 579 memset(&upt, 0, sizeof(upt)); 580 memset(&ctx, 0, sizeof(ctx)); 581 582 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpid)); 583 req.n.nlmsg_flags = NLM_F_REQUEST; 584 req.n.nlmsg_type = delete ? XFRM_MSG_DELPOLICY : XFRM_MSG_GETPOLICY; 585 586 while (argc > 0) { 587 if (strcmp(*argv, "dir") == 0) { 588 if (dirp) 589 duparg("dir", *argv); 590 dirp = *argv; 591 592 NEXT_ARG(); 593 xfrm_policy_dir_parse(&req.xpid.dir, &argc, &argv); 594 595 } else if (strcmp(*argv, "ctx") == 0) { 596 char *context; 597 598 if (sctxp) 599 duparg("ctx", *argv); 600 sctxp = *argv; 601 NEXT_ARG(); 602 context = *argv; 603 xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx); 604 } else if (strcmp(*argv, "mark") == 0) { 605 xfrm_parse_mark(&mark, &argc, &argv); 606 } else if (strcmp(*argv, "index") == 0) { 607 if (indexp) 608 duparg("index", *argv); 609 indexp = *argv; 610 611 NEXT_ARG(); 612 if (get_u32(&req.xpid.index, *argv, 0)) 613 invarg("INDEX value is invalid", *argv); 614 615 } else if (strcmp(*argv, "ptype") == 0) { 616 if (ptypep) 617 duparg("ptype", *argv); 618 ptypep = *argv; 619 620 NEXT_ARG(); 621 xfrm_policy_ptype_parse(&upt.type, &argc, &argv); 622 623 } else { 624 if (selp) 625 invarg("unknown", *argv); 626 selp = *argv; 627 628 xfrm_selector_parse(&req.xpid.sel, &argc, &argv); 629 if (preferred_family == AF_UNSPEC) 630 preferred_family = req.xpid.sel.family; 631 632 } 633 634 argc--; argv++; 635 } 636 637 if (!dirp) { 638 fprintf(stderr, "Not enough information: DIR is required.\n"); 639 exit(1); 640 } 641 if (ptypep) { 642 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE, 643 (void *)&upt, sizeof(upt)); 644 } 645 if (!selp && !indexp) { 646 fprintf(stderr, "Not enough information: either SELECTOR or INDEX is required.\n"); 647 exit(1); 648 } 649 if (selp && indexp) 650 duparg2("SELECTOR", "INDEX"); 651 652 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) 653 exit(1); 654 655 if (req.xpid.sel.family == AF_UNSPEC) 656 req.xpid.sel.family = AF_INET; 657 658 if (mark.m & mark.v) { 659 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK, 660 (void *)&mark, sizeof(mark)); 661 if (r < 0) { 662 fprintf(stderr, "%s: XFRMA_MARK failed\n",__func__); 663 exit(1); 664 } 665 } 666 667 if (sctxp) { 668 addattr_l(&req.n, sizeof(req), XFRMA_SEC_CTX, 669 (void *)&ctx, ctx.sctx.len); 670 } 671 672 if (rtnl_talk(&rth, &req.n, res_nlbuf, res_size) < 0) 673 exit(2); 674 675 rtnl_close(&rth); 676 677 return 0; 678 } 679 680 static int xfrm_policy_delete(int argc, char **argv) 681 { 682 return xfrm_policy_get_or_delete(argc, argv, 1, NULL, 0); 683 } 684 685 static int xfrm_policy_get(int argc, char **argv) 686 { 687 char buf[NLMSG_BUF_SIZE]; 688 struct nlmsghdr *n = (struct nlmsghdr *)buf; 689 690 memset(buf, 0, sizeof(buf)); 691 692 xfrm_policy_get_or_delete(argc, argv, 0, n, sizeof(buf)); 693 694 if (xfrm_policy_print(NULL, n, (void*)stdout) < 0) { 695 fprintf(stderr, "An error :-)\n"); 696 exit(1); 697 } 698 699 return 0; 700 } 701 702 /* 703 * With an existing policy of nlmsg, make new nlmsg for deleting the policy 704 * and store it to buffer. 705 */ 706 static int xfrm_policy_keep(const struct sockaddr_nl *who, 707 struct nlmsghdr *n, 708 void *arg) 709 { 710 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg; 711 struct rtnl_handle *rth = xb->rth; 712 struct xfrm_userpolicy_info *xpinfo = NLMSG_DATA(n); 713 int len = n->nlmsg_len; 714 struct rtattr *tb[XFRMA_MAX+1]; 715 __u8 ptype = XFRM_POLICY_TYPE_MAIN; 716 struct nlmsghdr *new_n; 717 struct xfrm_userpolicy_id *xpid; 718 719 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY) { 720 fprintf(stderr, "Not a policy: %08x %08x %08x\n", 721 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); 722 return 0; 723 } 724 725 len -= NLMSG_LENGTH(sizeof(*xpinfo)); 726 if (len < 0) { 727 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); 728 return -1; 729 } 730 731 parse_rtattr(tb, XFRMA_MAX, XFRMP_RTA(xpinfo), len); 732 733 if (tb[XFRMA_POLICY_TYPE]) { 734 struct xfrm_userpolicy_type *upt; 735 736 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) { 737 fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n"); 738 return -1; 739 } 740 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]); 741 ptype = upt->type; 742 } 743 744 if (!xfrm_policy_filter_match(xpinfo, ptype)) 745 return 0; 746 747 if (xb->offset > xb->size) { 748 fprintf(stderr, "Policy buffer overflow\n"); 749 return -1; 750 } 751 752 new_n = (struct nlmsghdr *)(xb->buf + xb->offset); 753 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xpid)); 754 new_n->nlmsg_flags = NLM_F_REQUEST; 755 new_n->nlmsg_type = XFRM_MSG_DELPOLICY; 756 new_n->nlmsg_seq = ++rth->seq; 757 758 xpid = NLMSG_DATA(new_n); 759 memcpy(&xpid->sel, &xpinfo->sel, sizeof(xpid->sel)); 760 xpid->dir = xpinfo->dir; 761 xpid->index = xpinfo->index; 762 763 xb->offset += new_n->nlmsg_len; 764 xb->nlmsg_count ++; 765 766 return 0; 767 } 768 769 static int xfrm_policy_list_or_deleteall(int argc, char **argv, int deleteall) 770 { 771 char *selp = NULL; 772 struct rtnl_handle rth; 773 774 if (argc > 0) 775 filter.use = 1; 776 filter.xpinfo.sel.family = preferred_family; 777 778 while (argc > 0) { 779 if (strcmp(*argv, "dir") == 0) { 780 NEXT_ARG(); 781 xfrm_policy_dir_parse(&filter.xpinfo.dir, &argc, &argv); 782 783 filter.dir_mask = XFRM_FILTER_MASK_FULL; 784 785 } else if (strcmp(*argv, "index") == 0) { 786 NEXT_ARG(); 787 if (get_u32(&filter.xpinfo.index, *argv, 0)) 788 invarg("INDEX value is invalid", *argv); 789 790 filter.index_mask = XFRM_FILTER_MASK_FULL; 791 792 } else if (strcmp(*argv, "ptype") == 0) { 793 NEXT_ARG(); 794 xfrm_policy_ptype_parse(&filter.ptype, &argc, &argv); 795 796 filter.ptype_mask = XFRM_FILTER_MASK_FULL; 797 798 } else if (strcmp(*argv, "action") == 0) { 799 NEXT_ARG(); 800 if (strcmp(*argv, "allow") == 0) 801 filter.xpinfo.action = XFRM_POLICY_ALLOW; 802 else if (strcmp(*argv, "block") == 0) 803 filter.xpinfo.action = XFRM_POLICY_BLOCK; 804 else 805 invarg("ACTION value is invalid\n", *argv); 806 807 filter.action_mask = XFRM_FILTER_MASK_FULL; 808 809 } else if (strcmp(*argv, "priority") == 0) { 810 NEXT_ARG(); 811 if (get_u32(&filter.xpinfo.priority, *argv, 0)) 812 invarg("PRIORITY value is invalid", *argv); 813 814 filter.priority_mask = XFRM_FILTER_MASK_FULL; 815 816 } else if (strcmp(*argv, "flag") == 0) { 817 NEXT_ARG(); 818 xfrm_policy_flag_parse(&filter.xpinfo.flags, &argc, 819 &argv); 820 821 filter.policy_flags_mask = XFRM_FILTER_MASK_FULL; 822 823 } else { 824 if (selp) 825 invarg("unknown", *argv); 826 selp = *argv; 827 828 xfrm_selector_parse(&filter.xpinfo.sel, &argc, &argv); 829 if (preferred_family == AF_UNSPEC) 830 preferred_family = filter.xpinfo.sel.family; 831 832 } 833 834 argc--; argv++; 835 } 836 837 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) 838 exit(1); 839 840 if (deleteall) { 841 struct xfrm_buffer xb; 842 char buf[NLMSG_DELETEALL_BUF_SIZE]; 843 int i; 844 845 xb.buf = buf; 846 xb.size = sizeof(buf); 847 xb.rth = &rth; 848 849 for (i = 0; ; i++) { 850 struct { 851 struct nlmsghdr n; 852 char buf[NLMSG_BUF_SIZE]; 853 } req = { 854 .n.nlmsg_len = NLMSG_HDRLEN, 855 .n.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, 856 .n.nlmsg_type = XFRM_MSG_GETPOLICY, 857 .n.nlmsg_seq = rth.dump = ++rth.seq, 858 }; 859 860 xb.offset = 0; 861 xb.nlmsg_count = 0; 862 863 if (show_stats > 1) 864 fprintf(stderr, "Delete-all round = %d\n", i); 865 866 if (rtnl_send(&rth, (void *)&req, req.n.nlmsg_len) < 0) { 867 perror("Cannot send dump request"); 868 exit(1); 869 } 870 871 if (rtnl_dump_filter(&rth, xfrm_policy_keep, &xb) < 0) { 872 fprintf(stderr, "Delete-all terminated\n"); 873 exit(1); 874 } 875 if (xb.nlmsg_count == 0) { 876 if (show_stats > 1) 877 fprintf(stderr, "Delete-all completed\n"); 878 break; 879 } 880 881 if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) { 882 perror("Failed to send delete-all request"); 883 exit(1); 884 } 885 if (show_stats > 1) 886 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count); 887 888 xb.offset = 0; 889 xb.nlmsg_count = 0; 890 } 891 } else { 892 struct { 893 struct nlmsghdr n; 894 char buf[NLMSG_BUF_SIZE]; 895 } req = { 896 .n.nlmsg_len = NLMSG_HDRLEN, 897 .n.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, 898 .n.nlmsg_type = XFRM_MSG_GETPOLICY, 899 .n.nlmsg_seq = rth.dump = ++rth.seq, 900 }; 901 902 if (rtnl_send(&rth, (void *)&req, req.n.nlmsg_len) < 0) { 903 perror("Cannot send dump request"); 904 exit(1); 905 } 906 907 if (rtnl_dump_filter(&rth, xfrm_policy_print, stdout) < 0) { 908 fprintf(stderr, "Dump terminated\n"); 909 exit(1); 910 } 911 } 912 913 rtnl_close(&rth); 914 915 exit(0); 916 } 917 918 static int print_spdinfo( struct nlmsghdr *n, void *arg) 919 { 920 FILE *fp = (FILE*)arg; 921 __u32 *f = NLMSG_DATA(n); 922 struct rtattr * tb[XFRMA_SPD_MAX+1]; 923 struct rtattr * rta; 924 925 int len = n->nlmsg_len; 926 927 len -= NLMSG_LENGTH(sizeof(__u32)); 928 if (len < 0) { 929 fprintf(stderr, "SPDinfo: Wrong len %d\n", len); 930 return -1; 931 } 932 933 rta = XFRMSAPD_RTA(f); 934 parse_rtattr(tb, XFRMA_SPD_MAX, rta, len); 935 936 fprintf(fp,"\t SPD"); 937 if (tb[XFRMA_SPD_INFO]) { 938 struct xfrmu_spdinfo *si; 939 940 if (RTA_PAYLOAD(tb[XFRMA_SPD_INFO]) < sizeof(*si)) { 941 fprintf(stderr, "SPDinfo: Wrong len %d\n", len); 942 return -1; 943 } 944 si = RTA_DATA(tb[XFRMA_SPD_INFO]); 945 fprintf(fp," IN %d", si->incnt); 946 fprintf(fp," OUT %d", si->outcnt); 947 fprintf(fp," FWD %d", si->fwdcnt); 948 949 if (show_stats) { 950 fprintf(fp," (Sock:"); 951 fprintf(fp," IN %d", si->inscnt); 952 fprintf(fp," OUT %d", si->outscnt); 953 fprintf(fp," FWD %d", si->fwdscnt); 954 fprintf(fp,")"); 955 } 956 957 fprintf(fp, "%s", _SL_); 958 } 959 if (show_stats > 1) { 960 struct xfrmu_spdhinfo *sh; 961 962 if (tb[XFRMA_SPD_HINFO]) { 963 if (RTA_PAYLOAD(tb[XFRMA_SPD_HINFO]) < sizeof(*sh)) { 964 fprintf(stderr, "SPDinfo: Wrong len %d\n", len); 965 return -1; 966 } 967 sh = RTA_DATA(tb[XFRMA_SPD_HINFO]); 968 fprintf(fp,"\t SPD buckets:"); 969 fprintf(fp," count %d", sh->spdhcnt); 970 fprintf(fp," Max %d", sh->spdhmcnt); 971 fprintf(fp, "%s", _SL_); 972 } 973 if (tb[XFRMA_SPD_IPV4_HTHRESH]) { 974 struct xfrmu_spdhthresh *th; 975 if (RTA_PAYLOAD(tb[XFRMA_SPD_IPV4_HTHRESH]) < sizeof(*th)) { 976 fprintf(stderr, "SPDinfo: Wrong len %d\n", len); 977 return -1; 978 } 979 th = RTA_DATA(tb[XFRMA_SPD_IPV4_HTHRESH]); 980 fprintf(fp,"\t SPD IPv4 thresholds:"); 981 fprintf(fp," local %d", th->lbits); 982 fprintf(fp," remote %d", th->rbits); 983 fprintf(fp, "%s", _SL_); 984 985 } 986 if (tb[XFRMA_SPD_IPV6_HTHRESH]) { 987 struct xfrmu_spdhthresh *th; 988 if (RTA_PAYLOAD(tb[XFRMA_SPD_IPV6_HTHRESH]) < sizeof(*th)) { 989 fprintf(stderr, "SPDinfo: Wrong len %d\n", len); 990 return -1; 991 } 992 th = RTA_DATA(tb[XFRMA_SPD_IPV6_HTHRESH]); 993 fprintf(fp,"\t SPD IPv6 thresholds:"); 994 fprintf(fp," local %d", th->lbits); 995 fprintf(fp," remote %d", th->rbits); 996 fprintf(fp, "%s", _SL_); 997 } 998 } 999 1000 if (oneline) 1001 fprintf(fp, "\n"); 1002 1003 return 0; 1004 } 1005 1006 static int xfrm_spd_setinfo(int argc, char **argv) 1007 { 1008 struct rtnl_handle rth; 1009 struct { 1010 struct nlmsghdr n; 1011 __u32 flags; 1012 char buf[RTA_BUF_SIZE]; 1013 } req; 1014 1015 char *thr4 = NULL; 1016 char *thr6 = NULL; 1017 1018 memset(&req, 0, sizeof(req)); 1019 1020 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(__u32)); 1021 req.n.nlmsg_flags = NLM_F_REQUEST; 1022 req.n.nlmsg_type = XFRM_MSG_NEWSPDINFO; 1023 req.flags = 0XFFFFFFFF; 1024 1025 while (argc > 0) { 1026 if (strcmp(*argv, "hthresh4") == 0) { 1027 struct xfrmu_spdhthresh thr; 1028 1029 if (thr4) 1030 duparg("hthresh4", *argv); 1031 thr4 = *argv; 1032 NEXT_ARG(); 1033 if (get_u8(&thr.lbits, *argv, 0) || thr.lbits > 32) 1034 invarg("hthresh4 LBITS value is invalid", *argv); 1035 NEXT_ARG(); 1036 if (get_u8(&thr.rbits, *argv, 0) || thr.rbits > 32) 1037 invarg("hthresh4 RBITS value is invalid", *argv); 1038 1039 addattr_l(&req.n, sizeof(req), XFRMA_SPD_IPV4_HTHRESH, 1040 (void *)&thr, sizeof(thr)); 1041 } else if (strcmp(*argv, "hthresh6") == 0) { 1042 struct xfrmu_spdhthresh thr; 1043 1044 if (thr6) 1045 duparg("hthresh6", *argv); 1046 thr6 = *argv; 1047 NEXT_ARG(); 1048 if (get_u8(&thr.lbits, *argv, 0) || thr.lbits > 128) 1049 invarg("hthresh6 LBITS value is invalid", *argv); 1050 NEXT_ARG(); 1051 if (get_u8(&thr.rbits, *argv, 0) || thr.rbits > 128) 1052 invarg("hthresh6 RBITS value is invalid", *argv); 1053 1054 addattr_l(&req.n, sizeof(req), XFRMA_SPD_IPV6_HTHRESH, 1055 (void *)&thr, sizeof(thr)); 1056 } else { 1057 invarg("unknown", *argv); 1058 } 1059 1060 argc--; argv++; 1061 } 1062 1063 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) 1064 exit(1); 1065 1066 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) 1067 exit(2); 1068 1069 rtnl_close(&rth); 1070 1071 return 0; 1072 } 1073 1074 static int xfrm_spd_getinfo(int argc, char **argv) 1075 { 1076 struct rtnl_handle rth; 1077 struct { 1078 struct nlmsghdr n; 1079 __u32 flags; 1080 char ans[128]; 1081 } req; 1082 1083 memset(&req, 0, sizeof(req)); 1084 1085 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(__u32)); 1086 req.n.nlmsg_flags = NLM_F_REQUEST; 1087 req.n.nlmsg_type = XFRM_MSG_GETSPDINFO; 1088 req.flags = 0XFFFFFFFF; 1089 1090 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) 1091 exit(1); 1092 1093 if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) 1094 exit(2); 1095 1096 print_spdinfo(&req.n, (void*)stdout); 1097 1098 rtnl_close(&rth); 1099 1100 return 0; 1101 } 1102 1103 static int xfrm_policy_flush(int argc, char **argv) 1104 { 1105 struct rtnl_handle rth; 1106 struct { 1107 struct nlmsghdr n; 1108 char buf[RTA_BUF_SIZE]; 1109 } req; 1110 char *ptypep = NULL; 1111 struct xfrm_userpolicy_type upt; 1112 1113 memset(&req, 0, sizeof(req)); 1114 memset(&upt, 0, sizeof(upt)); 1115 1116 req.n.nlmsg_len = NLMSG_LENGTH(0); /* nlmsg data is nothing */ 1117 req.n.nlmsg_flags = NLM_F_REQUEST; 1118 req.n.nlmsg_type = XFRM_MSG_FLUSHPOLICY; 1119 1120 while (argc > 0) { 1121 if (strcmp(*argv, "ptype") == 0) { 1122 if (ptypep) 1123 duparg("ptype", *argv); 1124 ptypep = *argv; 1125 1126 NEXT_ARG(); 1127 xfrm_policy_ptype_parse(&upt.type, &argc, &argv); 1128 } else 1129 invarg("unknown", *argv); 1130 1131 argc--; argv++; 1132 } 1133 1134 if (ptypep) { 1135 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE, 1136 (void *)&upt, sizeof(upt)); 1137 } 1138 1139 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) 1140 exit(1); 1141 1142 if (show_stats > 1) 1143 fprintf(stderr, "Flush policy\n"); 1144 1145 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) 1146 exit(2); 1147 1148 rtnl_close(&rth); 1149 1150 return 0; 1151 } 1152 1153 int do_xfrm_policy(int argc, char **argv) 1154 { 1155 if (argc < 1) 1156 return xfrm_policy_list_or_deleteall(0, NULL, 0); 1157 1158 if (matches(*argv, "add") == 0) 1159 return xfrm_policy_modify(XFRM_MSG_NEWPOLICY, 0, 1160 argc-1, argv+1); 1161 if (matches(*argv, "update") == 0) 1162 return xfrm_policy_modify(XFRM_MSG_UPDPOLICY, 0, 1163 argc-1, argv+1); 1164 if (matches(*argv, "delete") == 0) 1165 return xfrm_policy_delete(argc-1, argv+1); 1166 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0) 1167 return xfrm_policy_list_or_deleteall(argc-1, argv+1, 1); 1168 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 1169 || matches(*argv, "lst") == 0) 1170 return xfrm_policy_list_or_deleteall(argc-1, argv+1, 0); 1171 if (matches(*argv, "get") == 0) 1172 return xfrm_policy_get(argc-1, argv+1); 1173 if (matches(*argv, "flush") == 0) 1174 return xfrm_policy_flush(argc-1, argv+1); 1175 if (matches(*argv, "count") == 0) 1176 return xfrm_spd_getinfo(argc, argv); 1177 if (matches(*argv, "set") == 0) 1178 return xfrm_spd_setinfo(argc-1, argv+1); 1179 if (matches(*argv, "help") == 0) 1180 usage(); 1181 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv); 1182 exit(-1); 1183 } 1184