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 } dir DIR SELECTOR [ ctx SEC_CTX ][ index INDEX ] [ ptype PTYPE ]\n");
58 fprintf(stderr, " [ action ACTION ] [ priority PRIORITY ] [ flag FLAG-LIST ] [ LIMIT-LIST ] [ TMPL-LIST ] [mark MARK [mask MASK]]\n");
59 fprintf(stderr, "Usage: ip xfrm policy { delete | get } dir DIR [ SELECTOR | index INDEX ] [ ctx SEC_CTX ][ ptype PTYPE ] [mark MARK [mask MASK]]\n");
60 fprintf(stderr, "Usage: ip xfrm policy { deleteall | list } [ dir DIR ] [ SELECTOR ]\n");
61 fprintf(stderr, " [ index INDEX ] [ action ACTION ] [ priority PRIORITY ] [ flag FLAG-LIST ]\n");
62 fprintf(stderr, "Usage: ip xfrm policy flush [ ptype PTYPE ]\n");
63 fprintf(stderr, "Usage: ip xfrm count\n");
64 fprintf(stderr, "PTYPE := [ main | sub ](default=main)\n");
65 fprintf(stderr, "DIR := [ in | out | fwd ]\n");
67 fprintf(stderr, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ UPSPEC ] [ dev DEV ]\n");
69 fprintf(stderr, "UPSPEC := proto PROTO [ [ sport PORT ] [ dport PORT ] |\n");
70 fprintf(stderr, " [ type NUMBER ] [ code NUMBER ] |\n");
71 fprintf(stderr, " [ key { DOTTED_QUAD | NUMBER } ] ]\n");
73 //fprintf(stderr, "DEV - device name(default=none)\n");
75 fprintf(stderr, "ACTION := [ allow | block ](default=allow)\n");
77 //fprintf(stderr, "PRIORITY - priority value(default=0)\n");
79 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
80 fprintf(stderr, "FLAG := [ localok ]\n");
82 fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
83 fprintf(stderr, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
84 fprintf(stderr, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] NUMBER ]\n");
86 fprintf(stderr, "TMPL-LIST := [ TMPL-LIST ] | [ tmpl TMPL ]\n");
87 fprintf(stderr, "TMPL := ID [ mode MODE ] [ reqid REQID ] [ level LEVEL ]\n");
88 fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n");
90 fprintf(stderr, "XFRM_PROTO := [ ");
91 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP));
92 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH));
93 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_COMP));
94 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING));
95 fprintf(stderr, "%s ", strxf_xfrmproto(IPPROTO_DSTOPTS));
96 fprintf(stderr, "]\n");
98 fprintf(stderr, "MODE := [ transport | tunnel | beet ](default=transport)\n");
99 //fprintf(stderr, "REQID - number(default=0)\n");
100 fprintf(stderr, "LEVEL := [ required | use ](default=required)\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;
160 PREV_ARG(); /* back track */
176 static int xfrm_tmpl_parse(struct xfrm_user_tmpl *tmpl,
177 int *argcp, char ***argvp)
180 char **argv = *argvp;
184 if (strcmp(*argv, "mode") == 0) {
186 xfrm_mode_parse(&tmpl->mode, &argc, &argv);
187 } else if (strcmp(*argv, "reqid") == 0) {
189 xfrm_reqid_parse(&tmpl->reqid, &argc, &argv);
190 } else if (strcmp(*argv, "level") == 0) {
193 if (strcmp(*argv, "required") == 0)
195 else if (strcmp(*argv, "use") == 0)
198 invarg("\"LEVEL\" is invalid\n", *argv);
202 PREV_ARG(); /* back track */
206 preferred_family = AF_UNSPEC;
207 xfrm_id_parse(&tmpl->saddr, &tmpl->id, &tmpl->family,
209 preferred_family = tmpl->family;
226 int xfrm_sctx_parse(char *ctxstr, char *s,
227 struct xfrm_user_sec_ctx *sctx)
231 slen = strlen(s) + 1;
233 sctx->exttype = XFRMA_SEC_CTX;
236 sctx->ctx_len = slen;
237 sctx->len = sizeof(struct xfrm_user_sec_ctx) + slen;
238 memcpy(ctxstr, s, slen);
243 static int xfrm_policy_modify(int cmd, unsigned flags, int argc, char **argv)
245 struct rtnl_handle rth;
248 struct xfrm_userpolicy_info xpinfo;
249 char buf[RTA_BUF_SIZE];
255 struct xfrm_userpolicy_type upt;
256 char tmpls_buf[XFRM_TMPLS_BUF_SIZE];
258 struct xfrm_mark mark = {0, 0};
260 struct xfrm_user_sec_ctx sctx;
261 char str[CTX_BUF_SIZE];
264 memset(&req, 0, sizeof(req));
265 memset(&upt, 0, sizeof(upt));
266 memset(&tmpls_buf, 0, sizeof(tmpls_buf));
267 memset(&ctx, 0, sizeof(ctx));
269 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpinfo));
270 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
271 req.n.nlmsg_type = cmd;
272 req.xpinfo.sel.family = preferred_family;
274 req.xpinfo.lft.soft_byte_limit = XFRM_INF;
275 req.xpinfo.lft.hard_byte_limit = XFRM_INF;
276 req.xpinfo.lft.soft_packet_limit = XFRM_INF;
277 req.xpinfo.lft.hard_packet_limit = XFRM_INF;
280 if (strcmp(*argv, "dir") == 0) {
282 duparg("dir", *argv);
286 xfrm_policy_dir_parse(&req.xpinfo.dir, &argc, &argv);
287 } else if (strcmp(*argv, "ctx") == 0) {
291 duparg("ctx", *argv);
295 xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx);
296 } else if (strcmp(*argv, "mark") == 0) {
297 xfrm_parse_mark(&mark, &argc, &argv);
298 } else if (strcmp(*argv, "index") == 0) {
300 if (get_u32(&req.xpinfo.index, *argv, 0))
301 invarg("\"INDEX\" is invalid", *argv);
302 } else if (strcmp(*argv, "ptype") == 0) {
304 duparg("ptype", *argv);
308 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
309 } else if (strcmp(*argv, "action") == 0) {
311 if (strcmp(*argv, "allow") == 0)
312 req.xpinfo.action = XFRM_POLICY_ALLOW;
313 else if (strcmp(*argv, "block") == 0)
314 req.xpinfo.action = XFRM_POLICY_BLOCK;
316 invarg("\"action\" value is invalid\n", *argv);
317 } else if (strcmp(*argv, "priority") == 0) {
319 if (get_u32(&req.xpinfo.priority, *argv, 0))
320 invarg("\"PRIORITY\" is invalid", *argv);
321 } else if (strcmp(*argv, "flag") == 0) {
323 xfrm_policy_flag_parse(&req.xpinfo.flags, &argc,
325 } else if (strcmp(*argv, "limit") == 0) {
327 xfrm_lifetime_cfg_parse(&req.xpinfo.lft, &argc, &argv);
328 } else if (strcmp(*argv, "tmpl") == 0) {
329 struct xfrm_user_tmpl *tmpl;
331 if (tmpls_len + sizeof(*tmpl) > sizeof(tmpls_buf)) {
332 fprintf(stderr, "Too many tmpls: buffer overflow\n");
335 tmpl = (struct xfrm_user_tmpl *)((char *)tmpls_buf + tmpls_len);
337 tmpl->family = preferred_family;
338 tmpl->aalgos = (~(__u32)0);
339 tmpl->ealgos = (~(__u32)0);
340 tmpl->calgos = (~(__u32)0);
343 xfrm_tmpl_parse(tmpl, &argc, &argv);
345 tmpls_len += sizeof(*tmpl);
348 duparg("unknown", *argv);
351 xfrm_selector_parse(&req.xpinfo.sel, &argc, &argv);
352 if (preferred_family == AF_UNSPEC)
353 preferred_family = req.xpinfo.sel.family;
360 fprintf(stderr, "Not enough information: \"DIR\" is required.\n");
365 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
366 (void *)&upt, sizeof(upt));
370 addattr_l(&req.n, sizeof(req), XFRMA_TMPL,
371 (void *)tmpls_buf, tmpls_len);
374 if (mark.m & mark.v) {
375 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
376 (void *)&mark, sizeof(mark));
378 fprintf(stderr, "%s: XFRMA_MARK failed\n",__func__);
384 addattr_l(&req.n, sizeof(req), XFRMA_SEC_CTX,
385 (void *)&ctx, ctx.sctx.len);
388 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
391 if (req.xpinfo.sel.family == AF_UNSPEC)
392 req.xpinfo.sel.family = AF_INET;
394 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
402 static int xfrm_policy_filter_match(struct xfrm_userpolicy_info *xpinfo,
408 if ((xpinfo->dir^filter.xpinfo.dir)&filter.dir_mask)
411 if ((ptype^filter.ptype)&filter.ptype_mask)
414 if (filter.sel_src_mask) {
415 if (xfrm_addr_match(&xpinfo->sel.saddr, &filter.xpinfo.sel.saddr,
416 filter.sel_src_mask))
420 if (filter.sel_dst_mask) {
421 if (xfrm_addr_match(&xpinfo->sel.daddr, &filter.xpinfo.sel.daddr,
422 filter.sel_dst_mask))
426 if ((xpinfo->sel.ifindex^filter.xpinfo.sel.ifindex)&filter.sel_dev_mask)
429 if ((xpinfo->sel.proto^filter.xpinfo.sel.proto)&filter.upspec_proto_mask)
432 if (filter.upspec_sport_mask) {
433 if ((xpinfo->sel.sport^filter.xpinfo.sel.sport)&filter.upspec_sport_mask)
437 if (filter.upspec_dport_mask) {
438 if ((xpinfo->sel.dport^filter.xpinfo.sel.dport)&filter.upspec_dport_mask)
442 if ((xpinfo->index^filter.xpinfo.index)&filter.index_mask)
445 if ((xpinfo->action^filter.xpinfo.action)&filter.action_mask)
448 if ((xpinfo->priority^filter.xpinfo.priority)&filter.priority_mask)
451 if (filter.policy_flags_mask)
452 if ((xpinfo->flags & filter.xpinfo.flags) == 0)
458 int xfrm_policy_print(const struct sockaddr_nl *who, struct nlmsghdr *n,
461 struct rtattr * tb[XFRMA_MAX+1];
463 struct xfrm_userpolicy_info *xpinfo = NULL;
464 struct xfrm_user_polexpire *xpexp = NULL;
465 struct xfrm_userpolicy_id *xpid = NULL;
466 __u8 ptype = XFRM_POLICY_TYPE_MAIN;
467 FILE *fp = (FILE*)arg;
468 int len = n->nlmsg_len;
470 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY &&
471 n->nlmsg_type != XFRM_MSG_DELPOLICY &&
472 n->nlmsg_type != XFRM_MSG_UPDPOLICY &&
473 n->nlmsg_type != XFRM_MSG_POLEXPIRE) {
474 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
475 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
479 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) {
480 xpid = NLMSG_DATA(n);
481 len -= NLMSG_SPACE(sizeof(*xpid));
482 } else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
483 xpexp = NLMSG_DATA(n);
484 xpinfo = &xpexp->pol;
485 len -= NLMSG_SPACE(sizeof(*xpexp));
488 xpinfo = NLMSG_DATA(n);
489 len -= NLMSG_SPACE(sizeof(*xpinfo));
493 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
497 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
498 rta = XFRMPID_RTA(xpid);
499 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
500 rta = XFRMPEXP_RTA(xpexp);
502 rta = XFRMP_RTA(xpinfo);
504 parse_rtattr(tb, XFRMA_MAX, rta, len);
506 if (tb[XFRMA_POLICY_TYPE]) {
507 struct xfrm_userpolicy_type *upt;
509 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) {
510 fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n");
513 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
517 if (xpinfo && !xfrm_policy_filter_match(xpinfo, ptype))
520 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
521 fprintf(fp, "Deleted ");
522 else if (n->nlmsg_type == XFRM_MSG_UPDPOLICY)
523 fprintf(fp, "Updated ");
524 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
525 fprintf(fp, "Expired ");
527 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) {
528 //xfrm_policy_id_print();
529 if (!tb[XFRMA_POLICY]) {
530 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: no XFRMA_POLICY\n");
533 if (RTA_PAYLOAD(tb[XFRMA_POLICY]) < sizeof(*xpinfo)) {
534 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
537 xpinfo = (struct xfrm_userpolicy_info *)RTA_DATA(tb[XFRMA_POLICY]);
540 xfrm_policy_info_print(xpinfo, tb, fp, NULL, NULL);
542 if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
544 fprintf(fp, "hard %u", xpexp->hard);
545 fprintf(fp, "%s", _SL_);
555 static int xfrm_policy_get_or_delete(int argc, char **argv, int delete,
558 struct rtnl_handle rth;
561 struct xfrm_userpolicy_id xpid;
562 char buf[RTA_BUF_SIZE];
569 struct xfrm_userpolicy_type upt;
570 struct xfrm_mark mark = {0, 0};
572 struct xfrm_user_sec_ctx sctx;
573 char str[CTX_BUF_SIZE];
577 memset(&req, 0, sizeof(req));
578 memset(&upt, 0, sizeof(upt));
579 memset(&ctx, 0, sizeof(ctx));
581 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpid));
582 req.n.nlmsg_flags = NLM_F_REQUEST;
583 req.n.nlmsg_type = delete ? XFRM_MSG_DELPOLICY : XFRM_MSG_GETPOLICY;
586 if (strcmp(*argv, "dir") == 0) {
588 duparg("dir", *argv);
592 xfrm_policy_dir_parse(&req.xpid.dir, &argc, &argv);
594 } else if (strcmp(*argv, "ctx") == 0) {
598 duparg("ctx", *argv);
602 xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx);
603 } else if (strcmp(*argv, "mark") == 0) {
604 xfrm_parse_mark(&mark, &argc, &argv);
605 } else if (strcmp(*argv, "index") == 0) {
607 duparg("index", *argv);
611 if (get_u32(&req.xpid.index, *argv, 0))
612 invarg("\"INDEX\" is invalid", *argv);
614 } else if (strcmp(*argv, "ptype") == 0) {
616 duparg("ptype", *argv);
620 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
624 invarg("unknown", *argv);
627 xfrm_selector_parse(&req.xpid.sel, &argc, &argv);
628 if (preferred_family == AF_UNSPEC)
629 preferred_family = req.xpid.sel.family;
637 fprintf(stderr, "Not enough information: \"DIR\" is required.\n");
641 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
642 (void *)&upt, sizeof(upt));
644 if (!selp && !indexp) {
645 fprintf(stderr, "Not enough information: either \"SELECTOR\" or \"INDEX\" is required.\n");
649 duparg2("SELECTOR", "INDEX");
651 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
654 if (req.xpid.sel.family == AF_UNSPEC)
655 req.xpid.sel.family = AF_INET;
657 if (mark.m & mark.v) {
658 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
659 (void *)&mark, sizeof(mark));
661 fprintf(stderr, "%s: XFRMA_MARK failed\n",__func__);
667 addattr_l(&req.n, sizeof(req), XFRMA_SEC_CTX,
668 (void *)&ctx, ctx.sctx.len);
671 if (rtnl_talk(&rth, &req.n, 0, 0, res_nlbuf, NULL, NULL) < 0)
679 static int xfrm_policy_delete(int argc, char **argv)
681 return xfrm_policy_get_or_delete(argc, argv, 1, NULL);
684 static int xfrm_policy_get(int argc, char **argv)
686 char buf[NLMSG_BUF_SIZE];
687 struct nlmsghdr *n = (struct nlmsghdr *)buf;
689 memset(buf, 0, sizeof(buf));
691 xfrm_policy_get_or_delete(argc, argv, 0, n);
693 if (xfrm_policy_print(NULL, n, (void*)stdout) < 0) {
694 fprintf(stderr, "An error :-)\n");
702 * With an existing policy of nlmsg, make new nlmsg for deleting the policy
703 * and store it to buffer.
705 static int xfrm_policy_keep(const struct sockaddr_nl *who,
709 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
710 struct rtnl_handle *rth = xb->rth;
711 struct xfrm_userpolicy_info *xpinfo = NLMSG_DATA(n);
712 int len = n->nlmsg_len;
713 struct rtattr *tb[XFRMA_MAX+1];
714 __u8 ptype = XFRM_POLICY_TYPE_MAIN;
715 struct nlmsghdr *new_n;
716 struct xfrm_userpolicy_id *xpid;
718 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY) {
719 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
720 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
724 len -= NLMSG_LENGTH(sizeof(*xpinfo));
726 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
730 parse_rtattr(tb, XFRMA_MAX, XFRMP_RTA(xpinfo), len);
732 if (tb[XFRMA_POLICY_TYPE]) {
733 struct xfrm_userpolicy_type *upt;
735 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) {
736 fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n");
739 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
743 if (!xfrm_policy_filter_match(xpinfo, ptype))
746 if (xb->offset > xb->size) {
747 fprintf(stderr, "Policy buffer overflow\n");
751 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
752 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xpid));
753 new_n->nlmsg_flags = NLM_F_REQUEST;
754 new_n->nlmsg_type = XFRM_MSG_DELPOLICY;
755 new_n->nlmsg_seq = ++rth->seq;
757 xpid = NLMSG_DATA(new_n);
758 memcpy(&xpid->sel, &xpinfo->sel, sizeof(xpid->sel));
759 xpid->dir = xpinfo->dir;
760 xpid->index = xpinfo->index;
762 xb->offset += new_n->nlmsg_len;
768 static int xfrm_policy_list_or_deleteall(int argc, char **argv, int deleteall)
771 struct rtnl_handle rth;
775 filter.xpinfo.sel.family = preferred_family;
778 if (strcmp(*argv, "dir") == 0) {
780 xfrm_policy_dir_parse(&filter.xpinfo.dir, &argc, &argv);
782 filter.dir_mask = XFRM_FILTER_MASK_FULL;
784 } else if (strcmp(*argv, "index") == 0) {
786 if (get_u32(&filter.xpinfo.index, *argv, 0))
787 invarg("\"INDEX\" is invalid", *argv);
789 filter.index_mask = XFRM_FILTER_MASK_FULL;
791 } else if (strcmp(*argv, "ptype") == 0) {
793 xfrm_policy_ptype_parse(&filter.ptype, &argc, &argv);
795 filter.ptype_mask = XFRM_FILTER_MASK_FULL;
797 } else if (strcmp(*argv, "action") == 0) {
799 if (strcmp(*argv, "allow") == 0)
800 filter.xpinfo.action = XFRM_POLICY_ALLOW;
801 else if (strcmp(*argv, "block") == 0)
802 filter.xpinfo.action = XFRM_POLICY_BLOCK;
804 invarg("\"ACTION\" is invalid\n", *argv);
806 filter.action_mask = XFRM_FILTER_MASK_FULL;
808 } else if (strcmp(*argv, "priority") == 0) {
810 if (get_u32(&filter.xpinfo.priority, *argv, 0))
811 invarg("\"PRIORITY\" is invalid", *argv);
813 filter.priority_mask = XFRM_FILTER_MASK_FULL;
815 } else if (strcmp(*argv, "flag") == 0) {
817 xfrm_policy_flag_parse(&filter.xpinfo.flags, &argc,
820 filter.policy_flags_mask = XFRM_FILTER_MASK_FULL;
824 invarg("unknown", *argv);
827 xfrm_selector_parse(&filter.xpinfo.sel, &argc, &argv);
828 if (preferred_family == AF_UNSPEC)
829 preferred_family = filter.xpinfo.sel.family;
836 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
840 struct xfrm_buffer xb;
841 char buf[NLMSG_DELETEALL_BUF_SIZE];
845 xb.size = sizeof(buf);
853 fprintf(stderr, "Delete-all round = %d\n", i);
855 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
856 perror("Cannot send dump request");
860 if (rtnl_dump_filter(&rth, xfrm_policy_keep, &xb, NULL, NULL) < 0) {
861 fprintf(stderr, "Delete-all terminated\n");
864 if (xb.nlmsg_count == 0) {
866 fprintf(stderr, "Delete-all completed\n");
870 if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) {
871 perror("Failed to send delete-all request");
875 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
881 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
882 perror("Cannot send dump request");
886 if (rtnl_dump_filter(&rth, xfrm_policy_print, stdout, NULL, NULL) < 0) {
887 fprintf(stderr, "Dump terminated\n");
897 int print_spdinfo( struct nlmsghdr *n, void *arg)
899 FILE *fp = (FILE*)arg;
900 __u32 *f = NLMSG_DATA(n);
901 struct rtattr * tb[XFRMA_SPD_MAX+1];
904 int len = n->nlmsg_len;
906 len -= NLMSG_LENGTH(sizeof(__u32));
908 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
912 rta = XFRMSAPD_RTA(f);
913 parse_rtattr(tb, XFRMA_SPD_MAX, rta, len);
915 fprintf(fp,"\t SPD");
916 if (tb[XFRMA_SPD_INFO]) {
917 struct xfrmu_spdinfo *si;
919 if (RTA_PAYLOAD(tb[XFRMA_SPD_INFO]) < sizeof(*si)) {
920 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
923 si = RTA_DATA(tb[XFRMA_SPD_INFO]);
924 fprintf(fp," IN %d", si->incnt);
925 fprintf(fp," OUT %d", si->outcnt);
926 fprintf(fp," FWD %d", si->fwdcnt);
929 fprintf(fp," (Sock:");
930 fprintf(fp," IN %d", si->inscnt);
931 fprintf(fp," OUT %d", si->outscnt);
932 fprintf(fp," FWD %d", si->fwdscnt);
938 if (show_stats > 1) {
939 struct xfrmu_spdhinfo *sh;
941 if (tb[XFRMA_SPD_HINFO]) {
942 if (RTA_PAYLOAD(tb[XFRMA_SPD_HINFO]) < sizeof(*sh)) {
943 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
946 sh = RTA_DATA(tb[XFRMA_SPD_HINFO]);
947 fprintf(fp,"\t SPD buckets:");
948 fprintf(fp," count %d", sh->spdhcnt);
949 fprintf(fp," Max %d", sh->spdhmcnt);
957 static int xfrm_spd_getinfo(int argc, char **argv)
959 struct rtnl_handle rth;
966 memset(&req, 0, sizeof(req));
968 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(__u32));
969 req.n.nlmsg_flags = NLM_F_REQUEST;
970 req.n.nlmsg_type = XFRM_MSG_GETSPDINFO;
971 req.flags = 0XFFFFFFFF;
973 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
976 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0)
979 print_spdinfo(&req.n, (void*)stdout);
986 static int xfrm_policy_flush(int argc, char **argv)
988 struct rtnl_handle rth;
991 char buf[RTA_BUF_SIZE];
994 struct xfrm_userpolicy_type upt;
996 memset(&req, 0, sizeof(req));
997 memset(&upt, 0, sizeof(upt));
999 req.n.nlmsg_len = NLMSG_LENGTH(0); /* nlmsg data is nothing */
1000 req.n.nlmsg_flags = NLM_F_REQUEST;
1001 req.n.nlmsg_type = XFRM_MSG_FLUSHPOLICY;
1004 if (strcmp(*argv, "ptype") == 0) {
1006 duparg("ptype", *argv);
1010 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
1012 invarg("unknown", *argv);
1018 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
1019 (void *)&upt, sizeof(upt));
1022 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1026 fprintf(stderr, "Flush policy\n");
1028 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
1036 int do_xfrm_policy(int argc, char **argv)
1039 return xfrm_policy_list_or_deleteall(0, NULL, 0);
1041 if (matches(*argv, "add") == 0)
1042 return xfrm_policy_modify(XFRM_MSG_NEWPOLICY, 0,
1044 if (matches(*argv, "update") == 0)
1045 return xfrm_policy_modify(XFRM_MSG_UPDPOLICY, 0,
1047 if (matches(*argv, "delete") == 0)
1048 return xfrm_policy_delete(argc-1, argv+1);
1049 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
1050 return xfrm_policy_list_or_deleteall(argc-1, argv+1, 1);
1051 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1052 || matches(*argv, "lst") == 0)
1053 return xfrm_policy_list_or_deleteall(argc-1, argv+1, 0);
1054 if (matches(*argv, "get") == 0)
1055 return xfrm_policy_get(argc-1, argv+1);
1056 if (matches(*argv, "flush") == 0)
1057 return xfrm_policy_flush(argc-1, argv+1);
1058 if (matches(*argv, "count") == 0)
1059 return xfrm_spd_getinfo(argc, argv);
1060 if (matches(*argv, "help") == 0)
1062 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv);