4 * Copyright (C)2004 USAGI/WIDE Project
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.
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.
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
25 * Masahide NAKAMURA @USAGI
32 #include <linux/netlink.h>
33 #include <linux/xfrm.h>
36 #include "ip_common.h"
38 //#define NLMSG_DELETEALL_BUF_SIZE (4096-512)
39 #define NLMSG_DELETEALL_BUF_SIZE 8192
42 * Receiving buffer defines:
44 * data = struct xfrm_userpolicy_info
46 * data = struct xfrm_user_tmpl[]
48 #define NLMSG_BUF_SIZE 4096
49 #define RTA_BUF_SIZE 2048
50 #define XFRM_TMPLS_BUF_SIZE 1024
51 #define CTX_BUF_SIZE 256
53 static void usage(void) __attribute__((noreturn));
55 static void usage(void)
57 fprintf(stderr, "Usage: ip xfrm policy { add | update } SELECTOR dir DIR [ ctx CTX ]\n");
58 fprintf(stderr, " [ mark MARK [ mask MASK ] ] [ index INDEX ] [ ptype PTYPE ]\n");
59 fprintf(stderr, " [ action ACTION ] [ priority PRIORITY ] [ flag FLAG-LIST ]\n");
60 fprintf(stderr, " [ LIMIT-LIST ] [ TMPL-LIST ]\n");
61 fprintf(stderr, "Usage: ip xfrm policy { delete | get } { SELECTOR | index INDEX } dir DIR\n");
62 fprintf(stderr, " [ ctx CTX ] [ mark MARK [ mask MASK ] ] [ ptype PTYPE ]\n");
63 fprintf(stderr, "Usage: ip xfrm policy { deleteall | list } [ SELECTOR ] [ dir DIR ]\n");
64 fprintf(stderr, " [ index INDEX ] [ ptype PTYPE ] [ action ACTION ] [ priority PRIORITY ]\n");
65 fprintf(stderr, " [ flag FLAG-LIST ]\n");
66 fprintf(stderr, "Usage: ip xfrm policy flush [ ptype PTYPE ]\n");
67 fprintf(stderr, "Usage: ip xfrm count\n");
68 fprintf(stderr, "SELECTOR := [ src ADDR[/PLEN] ] [ dst ADDR[/PLEN] ] [ dev DEV ] [ UPSPEC ]\n");
69 fprintf(stderr, "UPSPEC := proto { { ");
70 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_TCP));
71 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_UDP));
72 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_SCTP));
73 fprintf(stderr, "%s", strxf_proto(IPPROTO_DCCP));
74 fprintf(stderr, " } [ sport PORT ] [ dport PORT ] |\n");
75 fprintf(stderr, " { ");
76 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_ICMP));
77 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_ICMPV6));
78 fprintf(stderr, "%s", strxf_proto(IPPROTO_MH));
79 fprintf(stderr, " } [ type NUMBER ] [ code NUMBER ] |\n");
80 fprintf(stderr, " %s", strxf_proto(IPPROTO_GRE));
81 fprintf(stderr, " [ key { DOTTED-QUAD | NUMBER } ] | PROTO }\n");
82 fprintf(stderr, "DIR := in | out | fwd\n");
83 fprintf(stderr, "PTYPE := main | sub\n");
84 fprintf(stderr, "ACTION := allow | block\n");
85 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
86 fprintf(stderr, "FLAG := localok | icmp\n");
87 fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] limit LIMIT\n");
88 fprintf(stderr, "LIMIT := { time-soft | time-hard | time-use-soft | time-use-hard } SECONDS |\n");
89 fprintf(stderr, " { byte-soft | byte-hard } SIZE | { packet-soft | packet-hard } COUNT\n");
90 fprintf(stderr, "TMPL-LIST := [ TMPL-LIST ] tmpl TMPL\n");
91 fprintf(stderr, "TMPL := ID [ mode MODE ] [ reqid REQID ] [ level LEVEL ]\n");
92 fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ]\n");
93 fprintf(stderr, "XFRM-PROTO := ");
94 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP));
95 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH));
96 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_COMP));
97 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING));
98 fprintf(stderr, "%s\n", strxf_xfrmproto(IPPROTO_DSTOPTS));
99 fprintf(stderr, "MODE := transport | tunnel | ro | in_trigger | beet\n");
100 fprintf(stderr, "LEVEL := required | use\n");
105 static int xfrm_policy_dir_parse(__u8 *dir, int *argcp, char ***argvp)
108 char **argv = *argvp;
110 if (strcmp(*argv, "in") == 0)
111 *dir = XFRM_POLICY_IN;
112 else if (strcmp(*argv, "out") == 0)
113 *dir = XFRM_POLICY_OUT;
114 else if (strcmp(*argv, "fwd") == 0)
115 *dir = XFRM_POLICY_FWD;
117 invarg("\"DIR\" is invalid", *argv);
125 static int xfrm_policy_ptype_parse(__u8 *ptype, int *argcp, char ***argvp)
128 char **argv = *argvp;
130 if (strcmp(*argv, "main") == 0)
131 *ptype = XFRM_POLICY_TYPE_MAIN;
132 else if (strcmp(*argv, "sub") == 0)
133 *ptype = XFRM_POLICY_TYPE_SUB;
135 invarg("\"PTYPE\" is invalid", *argv);
143 static int xfrm_policy_flag_parse(__u8 *flags, int *argcp, char ***argvp)
146 char **argv = *argvp;
147 int len = strlen(*argv);
149 if (len > 2 && strncmp(*argv, "0x", 2) == 0) {
152 if (get_u8(&val, *argv, 16))
153 invarg("\"FLAG\" is invalid", *argv);
157 if (strcmp(*argv, "localok") == 0)
158 *flags |= XFRM_POLICY_LOCALOK;
159 else if (strcmp(*argv, "icmp") == 0)
160 *flags |= XFRM_POLICY_ICMP;
162 PREV_ARG(); /* back track */
178 static int xfrm_tmpl_parse(struct xfrm_user_tmpl *tmpl,
179 int *argcp, char ***argvp)
182 char **argv = *argvp;
186 if (strcmp(*argv, "mode") == 0) {
188 xfrm_mode_parse(&tmpl->mode, &argc, &argv);
189 } else if (strcmp(*argv, "reqid") == 0) {
191 xfrm_reqid_parse(&tmpl->reqid, &argc, &argv);
192 } else if (strcmp(*argv, "level") == 0) {
195 if (strcmp(*argv, "required") == 0)
197 else if (strcmp(*argv, "use") == 0)
200 invarg("\"LEVEL\" is invalid\n", *argv);
204 PREV_ARG(); /* back track */
208 preferred_family = AF_UNSPEC;
209 xfrm_id_parse(&tmpl->saddr, &tmpl->id, &tmpl->family,
211 preferred_family = tmpl->family;
228 int xfrm_sctx_parse(char *ctxstr, char *s,
229 struct xfrm_user_sec_ctx *sctx)
233 slen = strlen(s) + 1;
235 sctx->exttype = XFRMA_SEC_CTX;
238 sctx->ctx_len = slen;
239 sctx->len = sizeof(struct xfrm_user_sec_ctx) + slen;
240 memcpy(ctxstr, s, slen);
245 static int xfrm_policy_modify(int cmd, unsigned flags, int argc, char **argv)
247 struct rtnl_handle rth;
250 struct xfrm_userpolicy_info xpinfo;
251 char buf[RTA_BUF_SIZE];
257 struct xfrm_userpolicy_type upt;
258 char tmpls_buf[XFRM_TMPLS_BUF_SIZE];
260 struct xfrm_mark mark = {0, 0};
262 struct xfrm_user_sec_ctx sctx;
263 char str[CTX_BUF_SIZE];
266 memset(&req, 0, sizeof(req));
267 memset(&upt, 0, sizeof(upt));
268 memset(&tmpls_buf, 0, sizeof(tmpls_buf));
269 memset(&ctx, 0, sizeof(ctx));
271 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpinfo));
272 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
273 req.n.nlmsg_type = cmd;
274 req.xpinfo.sel.family = preferred_family;
276 req.xpinfo.lft.soft_byte_limit = XFRM_INF;
277 req.xpinfo.lft.hard_byte_limit = XFRM_INF;
278 req.xpinfo.lft.soft_packet_limit = XFRM_INF;
279 req.xpinfo.lft.hard_packet_limit = XFRM_INF;
282 if (strcmp(*argv, "dir") == 0) {
284 duparg("dir", *argv);
288 xfrm_policy_dir_parse(&req.xpinfo.dir, &argc, &argv);
289 } else if (strcmp(*argv, "ctx") == 0) {
293 duparg("ctx", *argv);
297 xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx);
298 } else if (strcmp(*argv, "mark") == 0) {
299 xfrm_parse_mark(&mark, &argc, &argv);
300 } else if (strcmp(*argv, "index") == 0) {
302 if (get_u32(&req.xpinfo.index, *argv, 0))
303 invarg("\"INDEX\" is invalid", *argv);
304 } else if (strcmp(*argv, "ptype") == 0) {
306 duparg("ptype", *argv);
310 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
311 } else if (strcmp(*argv, "action") == 0) {
313 if (strcmp(*argv, "allow") == 0)
314 req.xpinfo.action = XFRM_POLICY_ALLOW;
315 else if (strcmp(*argv, "block") == 0)
316 req.xpinfo.action = XFRM_POLICY_BLOCK;
318 invarg("\"action\" value is invalid\n", *argv);
319 } else if (strcmp(*argv, "priority") == 0) {
321 if (get_u32(&req.xpinfo.priority, *argv, 0))
322 invarg("\"PRIORITY\" is invalid", *argv);
323 } else if (strcmp(*argv, "flag") == 0) {
325 xfrm_policy_flag_parse(&req.xpinfo.flags, &argc,
327 } else if (strcmp(*argv, "limit") == 0) {
329 xfrm_lifetime_cfg_parse(&req.xpinfo.lft, &argc, &argv);
330 } else if (strcmp(*argv, "tmpl") == 0) {
331 struct xfrm_user_tmpl *tmpl;
333 if (tmpls_len + sizeof(*tmpl) > sizeof(tmpls_buf)) {
334 fprintf(stderr, "Too many tmpls: buffer overflow\n");
337 tmpl = (struct xfrm_user_tmpl *)((char *)tmpls_buf + tmpls_len);
339 tmpl->family = preferred_family;
340 tmpl->aalgos = (~(__u32)0);
341 tmpl->ealgos = (~(__u32)0);
342 tmpl->calgos = (~(__u32)0);
345 xfrm_tmpl_parse(tmpl, &argc, &argv);
347 tmpls_len += sizeof(*tmpl);
350 duparg("unknown", *argv);
353 xfrm_selector_parse(&req.xpinfo.sel, &argc, &argv);
354 if (preferred_family == AF_UNSPEC)
355 preferred_family = req.xpinfo.sel.family;
362 fprintf(stderr, "Not enough information: \"DIR\" is required.\n");
367 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
368 (void *)&upt, sizeof(upt));
372 addattr_l(&req.n, sizeof(req), XFRMA_TMPL,
373 (void *)tmpls_buf, tmpls_len);
376 if (mark.m & mark.v) {
377 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
378 (void *)&mark, sizeof(mark));
380 fprintf(stderr, "%s: XFRMA_MARK failed\n",__func__);
386 addattr_l(&req.n, sizeof(req), XFRMA_SEC_CTX,
387 (void *)&ctx, ctx.sctx.len);
390 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
393 if (req.xpinfo.sel.family == AF_UNSPEC)
394 req.xpinfo.sel.family = AF_INET;
396 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
404 static int xfrm_policy_filter_match(struct xfrm_userpolicy_info *xpinfo,
410 if ((xpinfo->dir^filter.xpinfo.dir)&filter.dir_mask)
413 if ((ptype^filter.ptype)&filter.ptype_mask)
416 if (filter.sel_src_mask) {
417 if (xfrm_addr_match(&xpinfo->sel.saddr, &filter.xpinfo.sel.saddr,
418 filter.sel_src_mask))
422 if (filter.sel_dst_mask) {
423 if (xfrm_addr_match(&xpinfo->sel.daddr, &filter.xpinfo.sel.daddr,
424 filter.sel_dst_mask))
428 if ((xpinfo->sel.ifindex^filter.xpinfo.sel.ifindex)&filter.sel_dev_mask)
431 if ((xpinfo->sel.proto^filter.xpinfo.sel.proto)&filter.upspec_proto_mask)
434 if (filter.upspec_sport_mask) {
435 if ((xpinfo->sel.sport^filter.xpinfo.sel.sport)&filter.upspec_sport_mask)
439 if (filter.upspec_dport_mask) {
440 if ((xpinfo->sel.dport^filter.xpinfo.sel.dport)&filter.upspec_dport_mask)
444 if ((xpinfo->index^filter.xpinfo.index)&filter.index_mask)
447 if ((xpinfo->action^filter.xpinfo.action)&filter.action_mask)
450 if ((xpinfo->priority^filter.xpinfo.priority)&filter.priority_mask)
453 if (filter.policy_flags_mask)
454 if ((xpinfo->flags & filter.xpinfo.flags) == 0)
460 int xfrm_policy_print(const struct sockaddr_nl *who, struct nlmsghdr *n,
463 struct rtattr * tb[XFRMA_MAX+1];
465 struct xfrm_userpolicy_info *xpinfo = NULL;
466 struct xfrm_user_polexpire *xpexp = NULL;
467 struct xfrm_userpolicy_id *xpid = NULL;
468 __u8 ptype = XFRM_POLICY_TYPE_MAIN;
469 FILE *fp = (FILE*)arg;
470 int len = n->nlmsg_len;
472 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY &&
473 n->nlmsg_type != XFRM_MSG_DELPOLICY &&
474 n->nlmsg_type != XFRM_MSG_UPDPOLICY &&
475 n->nlmsg_type != XFRM_MSG_POLEXPIRE) {
476 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
477 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
481 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) {
482 xpid = NLMSG_DATA(n);
483 len -= NLMSG_SPACE(sizeof(*xpid));
484 } else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
485 xpexp = NLMSG_DATA(n);
486 xpinfo = &xpexp->pol;
487 len -= NLMSG_SPACE(sizeof(*xpexp));
490 xpinfo = NLMSG_DATA(n);
491 len -= NLMSG_SPACE(sizeof(*xpinfo));
495 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
499 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
500 rta = XFRMPID_RTA(xpid);
501 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
502 rta = XFRMPEXP_RTA(xpexp);
504 rta = XFRMP_RTA(xpinfo);
506 parse_rtattr(tb, XFRMA_MAX, rta, len);
508 if (tb[XFRMA_POLICY_TYPE]) {
509 struct xfrm_userpolicy_type *upt;
511 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) {
512 fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n");
515 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
519 if (xpinfo && !xfrm_policy_filter_match(xpinfo, ptype))
522 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
523 fprintf(fp, "Deleted ");
524 else if (n->nlmsg_type == XFRM_MSG_UPDPOLICY)
525 fprintf(fp, "Updated ");
526 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
527 fprintf(fp, "Expired ");
529 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) {
530 //xfrm_policy_id_print();
531 if (!tb[XFRMA_POLICY]) {
532 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: no XFRMA_POLICY\n");
535 if (RTA_PAYLOAD(tb[XFRMA_POLICY]) < sizeof(*xpinfo)) {
536 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
539 xpinfo = (struct xfrm_userpolicy_info *)RTA_DATA(tb[XFRMA_POLICY]);
542 xfrm_policy_info_print(xpinfo, tb, fp, NULL, NULL);
544 if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
546 fprintf(fp, "hard %u", xpexp->hard);
547 fprintf(fp, "%s", _SL_);
557 static int xfrm_policy_get_or_delete(int argc, char **argv, int delete,
560 struct rtnl_handle rth;
563 struct xfrm_userpolicy_id xpid;
564 char buf[RTA_BUF_SIZE];
571 struct xfrm_userpolicy_type upt;
572 struct xfrm_mark mark = {0, 0};
574 struct xfrm_user_sec_ctx sctx;
575 char str[CTX_BUF_SIZE];
579 memset(&req, 0, sizeof(req));
580 memset(&upt, 0, sizeof(upt));
581 memset(&ctx, 0, sizeof(ctx));
583 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpid));
584 req.n.nlmsg_flags = NLM_F_REQUEST;
585 req.n.nlmsg_type = delete ? XFRM_MSG_DELPOLICY : XFRM_MSG_GETPOLICY;
588 if (strcmp(*argv, "dir") == 0) {
590 duparg("dir", *argv);
594 xfrm_policy_dir_parse(&req.xpid.dir, &argc, &argv);
596 } else if (strcmp(*argv, "ctx") == 0) {
600 duparg("ctx", *argv);
604 xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx);
605 } else if (strcmp(*argv, "mark") == 0) {
606 xfrm_parse_mark(&mark, &argc, &argv);
607 } else if (strcmp(*argv, "index") == 0) {
609 duparg("index", *argv);
613 if (get_u32(&req.xpid.index, *argv, 0))
614 invarg("\"INDEX\" is invalid", *argv);
616 } else if (strcmp(*argv, "ptype") == 0) {
618 duparg("ptype", *argv);
622 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
626 invarg("unknown", *argv);
629 xfrm_selector_parse(&req.xpid.sel, &argc, &argv);
630 if (preferred_family == AF_UNSPEC)
631 preferred_family = req.xpid.sel.family;
639 fprintf(stderr, "Not enough information: \"DIR\" is required.\n");
643 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
644 (void *)&upt, sizeof(upt));
646 if (!selp && !indexp) {
647 fprintf(stderr, "Not enough information: either \"SELECTOR\" or \"INDEX\" is required.\n");
651 duparg2("SELECTOR", "INDEX");
653 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
656 if (req.xpid.sel.family == AF_UNSPEC)
657 req.xpid.sel.family = AF_INET;
659 if (mark.m & mark.v) {
660 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
661 (void *)&mark, sizeof(mark));
663 fprintf(stderr, "%s: XFRMA_MARK failed\n",__func__);
669 addattr_l(&req.n, sizeof(req), XFRMA_SEC_CTX,
670 (void *)&ctx, ctx.sctx.len);
673 if (rtnl_talk(&rth, &req.n, 0, 0, res_nlbuf) < 0)
681 static int xfrm_policy_delete(int argc, char **argv)
683 return xfrm_policy_get_or_delete(argc, argv, 1, NULL);
686 static int xfrm_policy_get(int argc, char **argv)
688 char buf[NLMSG_BUF_SIZE];
689 struct nlmsghdr *n = (struct nlmsghdr *)buf;
691 memset(buf, 0, sizeof(buf));
693 xfrm_policy_get_or_delete(argc, argv, 0, n);
695 if (xfrm_policy_print(NULL, n, (void*)stdout) < 0) {
696 fprintf(stderr, "An error :-)\n");
704 * With an existing policy of nlmsg, make new nlmsg for deleting the policy
705 * and store it to buffer.
707 static int xfrm_policy_keep(const struct sockaddr_nl *who,
711 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
712 struct rtnl_handle *rth = xb->rth;
713 struct xfrm_userpolicy_info *xpinfo = NLMSG_DATA(n);
714 int len = n->nlmsg_len;
715 struct rtattr *tb[XFRMA_MAX+1];
716 __u8 ptype = XFRM_POLICY_TYPE_MAIN;
717 struct nlmsghdr *new_n;
718 struct xfrm_userpolicy_id *xpid;
720 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY) {
721 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
722 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
726 len -= NLMSG_LENGTH(sizeof(*xpinfo));
728 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
732 parse_rtattr(tb, XFRMA_MAX, XFRMP_RTA(xpinfo), len);
734 if (tb[XFRMA_POLICY_TYPE]) {
735 struct xfrm_userpolicy_type *upt;
737 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) {
738 fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n");
741 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
745 if (!xfrm_policy_filter_match(xpinfo, ptype))
748 if (xb->offset > xb->size) {
749 fprintf(stderr, "Policy buffer overflow\n");
753 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
754 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xpid));
755 new_n->nlmsg_flags = NLM_F_REQUEST;
756 new_n->nlmsg_type = XFRM_MSG_DELPOLICY;
757 new_n->nlmsg_seq = ++rth->seq;
759 xpid = NLMSG_DATA(new_n);
760 memcpy(&xpid->sel, &xpinfo->sel, sizeof(xpid->sel));
761 xpid->dir = xpinfo->dir;
762 xpid->index = xpinfo->index;
764 xb->offset += new_n->nlmsg_len;
770 static int xfrm_policy_list_or_deleteall(int argc, char **argv, int deleteall)
773 struct rtnl_handle rth;
777 filter.xpinfo.sel.family = preferred_family;
780 if (strcmp(*argv, "dir") == 0) {
782 xfrm_policy_dir_parse(&filter.xpinfo.dir, &argc, &argv);
784 filter.dir_mask = XFRM_FILTER_MASK_FULL;
786 } else if (strcmp(*argv, "index") == 0) {
788 if (get_u32(&filter.xpinfo.index, *argv, 0))
789 invarg("\"INDEX\" is invalid", *argv);
791 filter.index_mask = XFRM_FILTER_MASK_FULL;
793 } else if (strcmp(*argv, "ptype") == 0) {
795 xfrm_policy_ptype_parse(&filter.ptype, &argc, &argv);
797 filter.ptype_mask = XFRM_FILTER_MASK_FULL;
799 } else if (strcmp(*argv, "action") == 0) {
801 if (strcmp(*argv, "allow") == 0)
802 filter.xpinfo.action = XFRM_POLICY_ALLOW;
803 else if (strcmp(*argv, "block") == 0)
804 filter.xpinfo.action = XFRM_POLICY_BLOCK;
806 invarg("\"ACTION\" is invalid\n", *argv);
808 filter.action_mask = XFRM_FILTER_MASK_FULL;
810 } else if (strcmp(*argv, "priority") == 0) {
812 if (get_u32(&filter.xpinfo.priority, *argv, 0))
813 invarg("\"PRIORITY\" is invalid", *argv);
815 filter.priority_mask = XFRM_FILTER_MASK_FULL;
817 } else if (strcmp(*argv, "flag") == 0) {
819 xfrm_policy_flag_parse(&filter.xpinfo.flags, &argc,
822 filter.policy_flags_mask = XFRM_FILTER_MASK_FULL;
826 invarg("unknown", *argv);
829 xfrm_selector_parse(&filter.xpinfo.sel, &argc, &argv);
830 if (preferred_family == AF_UNSPEC)
831 preferred_family = filter.xpinfo.sel.family;
838 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
842 struct xfrm_buffer xb;
843 char buf[NLMSG_DELETEALL_BUF_SIZE];
847 xb.size = sizeof(buf);
855 fprintf(stderr, "Delete-all round = %d\n", i);
857 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
858 perror("Cannot send dump request");
862 if (rtnl_dump_filter(&rth, xfrm_policy_keep, &xb) < 0) {
863 fprintf(stderr, "Delete-all terminated\n");
866 if (xb.nlmsg_count == 0) {
868 fprintf(stderr, "Delete-all completed\n");
872 if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) {
873 perror("Failed to send delete-all request");
877 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
883 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
884 perror("Cannot send dump request");
888 if (rtnl_dump_filter(&rth, xfrm_policy_print, stdout) < 0) {
889 fprintf(stderr, "Dump terminated\n");
899 int print_spdinfo( struct nlmsghdr *n, void *arg)
901 FILE *fp = (FILE*)arg;
902 __u32 *f = NLMSG_DATA(n);
903 struct rtattr * tb[XFRMA_SPD_MAX+1];
906 int len = n->nlmsg_len;
908 len -= NLMSG_LENGTH(sizeof(__u32));
910 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
914 rta = XFRMSAPD_RTA(f);
915 parse_rtattr(tb, XFRMA_SPD_MAX, rta, len);
917 fprintf(fp,"\t SPD");
918 if (tb[XFRMA_SPD_INFO]) {
919 struct xfrmu_spdinfo *si;
921 if (RTA_PAYLOAD(tb[XFRMA_SPD_INFO]) < sizeof(*si)) {
922 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
925 si = RTA_DATA(tb[XFRMA_SPD_INFO]);
926 fprintf(fp," IN %d", si->incnt);
927 fprintf(fp," OUT %d", si->outcnt);
928 fprintf(fp," FWD %d", si->fwdcnt);
931 fprintf(fp," (Sock:");
932 fprintf(fp," IN %d", si->inscnt);
933 fprintf(fp," OUT %d", si->outscnt);
934 fprintf(fp," FWD %d", si->fwdscnt);
940 if (show_stats > 1) {
941 struct xfrmu_spdhinfo *sh;
943 if (tb[XFRMA_SPD_HINFO]) {
944 if (RTA_PAYLOAD(tb[XFRMA_SPD_HINFO]) < sizeof(*sh)) {
945 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
948 sh = RTA_DATA(tb[XFRMA_SPD_HINFO]);
949 fprintf(fp,"\t SPD buckets:");
950 fprintf(fp," count %d", sh->spdhcnt);
951 fprintf(fp," Max %d", sh->spdhmcnt);
959 static int xfrm_spd_getinfo(int argc, char **argv)
961 struct rtnl_handle rth;
968 memset(&req, 0, sizeof(req));
970 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(__u32));
971 req.n.nlmsg_flags = NLM_F_REQUEST;
972 req.n.nlmsg_type = XFRM_MSG_GETSPDINFO;
973 req.flags = 0XFFFFFFFF;
975 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
978 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0)
981 print_spdinfo(&req.n, (void*)stdout);
988 static int xfrm_policy_flush(int argc, char **argv)
990 struct rtnl_handle rth;
993 char buf[RTA_BUF_SIZE];
996 struct xfrm_userpolicy_type upt;
998 memset(&req, 0, sizeof(req));
999 memset(&upt, 0, sizeof(upt));
1001 req.n.nlmsg_len = NLMSG_LENGTH(0); /* nlmsg data is nothing */
1002 req.n.nlmsg_flags = NLM_F_REQUEST;
1003 req.n.nlmsg_type = XFRM_MSG_FLUSHPOLICY;
1006 if (strcmp(*argv, "ptype") == 0) {
1008 duparg("ptype", *argv);
1012 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
1014 invarg("unknown", *argv);
1020 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
1021 (void *)&upt, sizeof(upt));
1024 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1028 fprintf(stderr, "Flush policy\n");
1030 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
1038 int do_xfrm_policy(int argc, char **argv)
1041 return xfrm_policy_list_or_deleteall(0, NULL, 0);
1043 if (matches(*argv, "add") == 0)
1044 return xfrm_policy_modify(XFRM_MSG_NEWPOLICY, 0,
1046 if (matches(*argv, "update") == 0)
1047 return xfrm_policy_modify(XFRM_MSG_UPDPOLICY, 0,
1049 if (matches(*argv, "delete") == 0)
1050 return xfrm_policy_delete(argc-1, argv+1);
1051 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
1052 return xfrm_policy_list_or_deleteall(argc-1, argv+1, 1);
1053 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1054 || matches(*argv, "lst") == 0)
1055 return xfrm_policy_list_or_deleteall(argc-1, argv+1, 0);
1056 if (matches(*argv, "get") == 0)
1057 return xfrm_policy_get(argc-1, argv+1);
1058 if (matches(*argv, "flush") == 0)
1059 return xfrm_policy_flush(argc-1, argv+1);
1060 if (matches(*argv, "count") == 0)
1061 return xfrm_spd_getinfo(argc, argv);
1062 if (matches(*argv, "help") == 0)
1064 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv);