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
52 static void usage(void) __attribute__((noreturn));
54 static void usage(void)
56 fprintf(stderr, "Usage: ip xfrm policy { add | update } dir DIR SELECTOR [ index INDEX ] [ ptype PTYPE ]\n");
57 fprintf(stderr, " [ action ACTION ] [ priority PRIORITY ] [ flag FLAG-LIST ] [ LIMIT-LIST ] [ TMPL-LIST ] [mark MARK [mask MASK]]\n");
58 fprintf(stderr, "Usage: ip xfrm policy { delete | get } dir DIR [ SELECTOR | index INDEX ] [ ptype PTYPE ] [mark MARK [mask MASK]]\n");
59 fprintf(stderr, "Usage: ip xfrm policy { deleteall | list } [ dir DIR ] [ SELECTOR ]\n");
60 fprintf(stderr, " [ index INDEX ] [ action ACTION ] [ priority PRIORITY ] [ flag FLAG-LIST ]\n");
61 fprintf(stderr, "Usage: ip xfrm policy flush [ ptype PTYPE ]\n");
62 fprintf(stderr, "Usage: ip xfrm count\n");
63 fprintf(stderr, "PTYPE := [ main | sub ](default=main)\n");
64 fprintf(stderr, "DIR := [ in | out | fwd ]\n");
66 fprintf(stderr, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ UPSPEC ] [ dev DEV ]\n");
68 fprintf(stderr, "UPSPEC := proto PROTO [ [ sport PORT ] [ dport PORT ] |\n");
69 fprintf(stderr, " [ type NUMBER ] [ code NUMBER ] |\n");
70 fprintf(stderr, " [ key { DOTTED_QUAD | NUMBER } ] ]\n");
72 //fprintf(stderr, "DEV - device name(default=none)\n");
74 fprintf(stderr, "ACTION := [ allow | block ](default=allow)\n");
76 //fprintf(stderr, "PRIORITY - priority value(default=0)\n");
78 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
79 fprintf(stderr, "FLAG := [ localok ]\n");
81 fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
82 fprintf(stderr, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
83 fprintf(stderr, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] NUMBER ]\n");
85 fprintf(stderr, "TMPL-LIST := [ TMPL-LIST ] | [ tmpl TMPL ]\n");
86 fprintf(stderr, "TMPL := ID [ mode MODE ] [ reqid REQID ] [ level LEVEL ]\n");
87 fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n");
89 fprintf(stderr, "XFRM_PROTO := [ ");
90 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP));
91 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH));
92 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_COMP));
93 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING));
94 fprintf(stderr, "%s ", strxf_xfrmproto(IPPROTO_DSTOPTS));
95 fprintf(stderr, "]\n");
97 fprintf(stderr, "MODE := [ transport | tunnel | beet ](default=transport)\n");
98 //fprintf(stderr, "REQID - number(default=0)\n");
99 fprintf(stderr, "LEVEL := [ required | use ](default=required)\n");
104 static int xfrm_policy_dir_parse(__u8 *dir, int *argcp, char ***argvp)
107 char **argv = *argvp;
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;
116 invarg("\"DIR\" is invalid", *argv);
124 static int xfrm_policy_ptype_parse(__u8 *ptype, int *argcp, char ***argvp)
127 char **argv = *argvp;
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;
134 invarg("\"PTYPE\" is invalid", *argv);
142 static int xfrm_policy_flag_parse(__u8 *flags, int *argcp, char ***argvp)
145 char **argv = *argvp;
146 int len = strlen(*argv);
148 if (len > 2 && strncmp(*argv, "0x", 2) == 0) {
151 if (get_u8(&val, *argv, 16))
152 invarg("\"FLAG\" is invalid", *argv);
156 if (strcmp(*argv, "localok") == 0)
157 *flags |= XFRM_POLICY_LOCALOK;
159 PREV_ARG(); /* back track */
175 static int xfrm_tmpl_parse(struct xfrm_user_tmpl *tmpl,
176 int *argcp, char ***argvp)
179 char **argv = *argvp;
183 if (strcmp(*argv, "mode") == 0) {
185 xfrm_mode_parse(&tmpl->mode, &argc, &argv);
186 } else if (strcmp(*argv, "reqid") == 0) {
188 xfrm_reqid_parse(&tmpl->reqid, &argc, &argv);
189 } else if (strcmp(*argv, "level") == 0) {
192 if (strcmp(*argv, "required") == 0)
194 else if (strcmp(*argv, "use") == 0)
197 invarg("\"LEVEL\" is invalid\n", *argv);
201 PREV_ARG(); /* back track */
205 preferred_family = AF_UNSPEC;
206 xfrm_id_parse(&tmpl->saddr, &tmpl->id, &tmpl->family,
208 preferred_family = tmpl->family;
225 static int xfrm_policy_modify(int cmd, unsigned flags, int argc, char **argv)
227 struct rtnl_handle rth;
230 struct xfrm_userpolicy_info xpinfo;
231 char buf[RTA_BUF_SIZE];
236 struct xfrm_userpolicy_type upt;
237 char tmpls_buf[XFRM_TMPLS_BUF_SIZE];
239 struct xfrm_mark mark = {0, 0};
241 memset(&req, 0, sizeof(req));
242 memset(&upt, 0, sizeof(upt));
243 memset(&tmpls_buf, 0, sizeof(tmpls_buf));
245 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpinfo));
246 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
247 req.n.nlmsg_type = cmd;
248 req.xpinfo.sel.family = preferred_family;
250 req.xpinfo.lft.soft_byte_limit = XFRM_INF;
251 req.xpinfo.lft.hard_byte_limit = XFRM_INF;
252 req.xpinfo.lft.soft_packet_limit = XFRM_INF;
253 req.xpinfo.lft.hard_packet_limit = XFRM_INF;
256 if (strcmp(*argv, "dir") == 0) {
258 duparg("dir", *argv);
262 xfrm_policy_dir_parse(&req.xpinfo.dir, &argc, &argv);
263 } else if (strcmp(*argv, "mark") == 0) {
264 xfrm_parse_mark(&mark, &argc, &argv);
265 } else if (strcmp(*argv, "index") == 0) {
267 if (get_u32(&req.xpinfo.index, *argv, 0))
268 invarg("\"INDEX\" is invalid", *argv);
269 } else if (strcmp(*argv, "ptype") == 0) {
271 duparg("ptype", *argv);
275 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
276 } else if (strcmp(*argv, "action") == 0) {
278 if (strcmp(*argv, "allow") == 0)
279 req.xpinfo.action = XFRM_POLICY_ALLOW;
280 else if (strcmp(*argv, "block") == 0)
281 req.xpinfo.action = XFRM_POLICY_BLOCK;
283 invarg("\"action\" value is invalid\n", *argv);
284 } else if (strcmp(*argv, "priority") == 0) {
286 if (get_u32(&req.xpinfo.priority, *argv, 0))
287 invarg("\"PRIORITY\" is invalid", *argv);
288 } else if (strcmp(*argv, "flag") == 0) {
290 xfrm_policy_flag_parse(&req.xpinfo.flags, &argc,
292 } else if (strcmp(*argv, "limit") == 0) {
294 xfrm_lifetime_cfg_parse(&req.xpinfo.lft, &argc, &argv);
295 } else if (strcmp(*argv, "tmpl") == 0) {
296 struct xfrm_user_tmpl *tmpl;
298 if (tmpls_len + sizeof(*tmpl) > sizeof(tmpls_buf)) {
299 fprintf(stderr, "Too many tmpls: buffer overflow\n");
302 tmpl = (struct xfrm_user_tmpl *)((char *)tmpls_buf + tmpls_len);
304 tmpl->family = preferred_family;
305 tmpl->aalgos = (~(__u32)0);
306 tmpl->ealgos = (~(__u32)0);
307 tmpl->calgos = (~(__u32)0);
310 xfrm_tmpl_parse(tmpl, &argc, &argv);
312 tmpls_len += sizeof(*tmpl);
315 duparg("unknown", *argv);
318 xfrm_selector_parse(&req.xpinfo.sel, &argc, &argv);
319 if (preferred_family == AF_UNSPEC)
320 preferred_family = req.xpinfo.sel.family;
327 fprintf(stderr, "Not enough information: \"DIR\" is required.\n");
332 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
333 (void *)&upt, sizeof(upt));
337 addattr_l(&req.n, sizeof(req), XFRMA_TMPL,
338 (void *)tmpls_buf, tmpls_len);
341 if (mark.m & mark.v) {
342 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
343 (void *)&mark, sizeof(mark));
345 fprintf(stderr, "%s: XFRMA_MARK failed\n",__func__);
351 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
354 if (req.xpinfo.sel.family == AF_UNSPEC)
355 req.xpinfo.sel.family = AF_INET;
357 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
365 static int xfrm_policy_filter_match(struct xfrm_userpolicy_info *xpinfo,
371 if ((xpinfo->dir^filter.xpinfo.dir)&filter.dir_mask)
374 if ((ptype^filter.ptype)&filter.ptype_mask)
377 if (filter.sel_src_mask) {
378 if (xfrm_addr_match(&xpinfo->sel.saddr, &filter.xpinfo.sel.saddr,
379 filter.sel_src_mask))
383 if (filter.sel_dst_mask) {
384 if (xfrm_addr_match(&xpinfo->sel.daddr, &filter.xpinfo.sel.daddr,
385 filter.sel_dst_mask))
389 if ((xpinfo->sel.ifindex^filter.xpinfo.sel.ifindex)&filter.sel_dev_mask)
392 if ((xpinfo->sel.proto^filter.xpinfo.sel.proto)&filter.upspec_proto_mask)
395 if (filter.upspec_sport_mask) {
396 if ((xpinfo->sel.sport^filter.xpinfo.sel.sport)&filter.upspec_sport_mask)
400 if (filter.upspec_dport_mask) {
401 if ((xpinfo->sel.dport^filter.xpinfo.sel.dport)&filter.upspec_dport_mask)
405 if ((xpinfo->index^filter.xpinfo.index)&filter.index_mask)
408 if ((xpinfo->action^filter.xpinfo.action)&filter.action_mask)
411 if ((xpinfo->priority^filter.xpinfo.priority)&filter.priority_mask)
414 if (filter.policy_flags_mask)
415 if ((xpinfo->flags & filter.xpinfo.flags) == 0)
421 int xfrm_policy_print(const struct sockaddr_nl *who, struct nlmsghdr *n,
424 struct rtattr * tb[XFRMA_MAX+1];
426 struct xfrm_userpolicy_info *xpinfo = NULL;
427 struct xfrm_user_polexpire *xpexp = NULL;
428 struct xfrm_userpolicy_id *xpid = NULL;
429 __u8 ptype = XFRM_POLICY_TYPE_MAIN;
430 FILE *fp = (FILE*)arg;
431 int len = n->nlmsg_len;
433 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY &&
434 n->nlmsg_type != XFRM_MSG_DELPOLICY &&
435 n->nlmsg_type != XFRM_MSG_UPDPOLICY &&
436 n->nlmsg_type != XFRM_MSG_POLEXPIRE) {
437 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
438 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
442 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) {
443 xpid = NLMSG_DATA(n);
444 len -= NLMSG_SPACE(sizeof(*xpid));
445 } else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
446 xpexp = NLMSG_DATA(n);
447 xpinfo = &xpexp->pol;
448 len -= NLMSG_SPACE(sizeof(*xpexp));
451 xpinfo = NLMSG_DATA(n);
452 len -= NLMSG_SPACE(sizeof(*xpinfo));
456 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
460 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
461 rta = XFRMPID_RTA(xpid);
462 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
463 rta = XFRMPEXP_RTA(xpexp);
465 rta = XFRMP_RTA(xpinfo);
467 parse_rtattr(tb, XFRMA_MAX, rta, len);
469 if (tb[XFRMA_POLICY_TYPE]) {
470 struct xfrm_userpolicy_type *upt;
472 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) {
473 fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n");
476 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
480 if (xpinfo && !xfrm_policy_filter_match(xpinfo, ptype))
483 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
484 fprintf(fp, "Deleted ");
485 else if (n->nlmsg_type == XFRM_MSG_UPDPOLICY)
486 fprintf(fp, "Updated ");
487 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
488 fprintf(fp, "Expired ");
490 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) {
491 //xfrm_policy_id_print();
492 if (!tb[XFRMA_POLICY]) {
493 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: no XFRMA_POLICY\n");
496 if (RTA_PAYLOAD(tb[XFRMA_POLICY]) < sizeof(*xpinfo)) {
497 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
500 xpinfo = (struct xfrm_userpolicy_info *)RTA_DATA(tb[XFRMA_POLICY]);
503 xfrm_policy_info_print(xpinfo, tb, fp, NULL, NULL);
505 if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
507 fprintf(fp, "hard %u", xpexp->hard);
508 fprintf(fp, "%s", _SL_);
518 static int xfrm_policy_get_or_delete(int argc, char **argv, int delete,
521 struct rtnl_handle rth;
524 struct xfrm_userpolicy_id xpid;
525 char buf[RTA_BUF_SIZE];
531 struct xfrm_userpolicy_type upt;
532 struct xfrm_mark mark = {0, 0};
534 memset(&req, 0, sizeof(req));
535 memset(&upt, 0, sizeof(upt));
537 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpid));
538 req.n.nlmsg_flags = NLM_F_REQUEST;
539 req.n.nlmsg_type = delete ? XFRM_MSG_DELPOLICY : XFRM_MSG_GETPOLICY;
542 if (strcmp(*argv, "dir") == 0) {
544 duparg("dir", *argv);
548 xfrm_policy_dir_parse(&req.xpid.dir, &argc, &argv);
550 } else if (strcmp(*argv, "mark") == 0) {
551 xfrm_parse_mark(&mark, &argc, &argv);
552 } else if (strcmp(*argv, "index") == 0) {
554 duparg("index", *argv);
558 if (get_u32(&req.xpid.index, *argv, 0))
559 invarg("\"INDEX\" is invalid", *argv);
561 } else if (strcmp(*argv, "ptype") == 0) {
563 duparg("ptype", *argv);
567 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
571 invarg("unknown", *argv);
574 xfrm_selector_parse(&req.xpid.sel, &argc, &argv);
575 if (preferred_family == AF_UNSPEC)
576 preferred_family = req.xpid.sel.family;
584 fprintf(stderr, "Not enough information: \"DIR\" is required.\n");
588 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
589 (void *)&upt, sizeof(upt));
591 if (!selp && !indexp) {
592 fprintf(stderr, "Not enough information: either \"SELECTOR\" or \"INDEX\" is required.\n");
596 duparg2("SELECTOR", "INDEX");
598 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
601 if (req.xpid.sel.family == AF_UNSPEC)
602 req.xpid.sel.family = AF_INET;
604 if (mark.m & mark.v) {
605 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
606 (void *)&mark, sizeof(mark));
608 fprintf(stderr, "%s: XFRMA_MARK failed\n",__func__);
613 if (rtnl_talk(&rth, &req.n, 0, 0, res_nlbuf, NULL, NULL) < 0)
621 static int xfrm_policy_delete(int argc, char **argv)
623 return xfrm_policy_get_or_delete(argc, argv, 1, NULL);
626 static int xfrm_policy_get(int argc, char **argv)
628 char buf[NLMSG_BUF_SIZE];
629 struct nlmsghdr *n = (struct nlmsghdr *)buf;
631 memset(buf, 0, sizeof(buf));
633 xfrm_policy_get_or_delete(argc, argv, 0, n);
635 if (xfrm_policy_print(NULL, n, (void*)stdout) < 0) {
636 fprintf(stderr, "An error :-)\n");
644 * With an existing policy of nlmsg, make new nlmsg for deleting the policy
645 * and store it to buffer.
647 static int xfrm_policy_keep(const struct sockaddr_nl *who,
651 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
652 struct rtnl_handle *rth = xb->rth;
653 struct xfrm_userpolicy_info *xpinfo = NLMSG_DATA(n);
654 int len = n->nlmsg_len;
655 struct rtattr *tb[XFRMA_MAX+1];
656 __u8 ptype = XFRM_POLICY_TYPE_MAIN;
657 struct nlmsghdr *new_n;
658 struct xfrm_userpolicy_id *xpid;
660 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY) {
661 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
662 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
666 len -= NLMSG_LENGTH(sizeof(*xpinfo));
668 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
672 parse_rtattr(tb, XFRMA_MAX, XFRMP_RTA(xpinfo), len);
674 if (tb[XFRMA_POLICY_TYPE]) {
675 struct xfrm_userpolicy_type *upt;
677 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) {
678 fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n");
681 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
685 if (!xfrm_policy_filter_match(xpinfo, ptype))
688 if (xb->offset > xb->size) {
689 fprintf(stderr, "Policy buffer overflow\n");
693 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
694 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xpid));
695 new_n->nlmsg_flags = NLM_F_REQUEST;
696 new_n->nlmsg_type = XFRM_MSG_DELPOLICY;
697 new_n->nlmsg_seq = ++rth->seq;
699 xpid = NLMSG_DATA(new_n);
700 memcpy(&xpid->sel, &xpinfo->sel, sizeof(xpid->sel));
701 xpid->dir = xpinfo->dir;
702 xpid->index = xpinfo->index;
704 xb->offset += new_n->nlmsg_len;
710 static int xfrm_policy_list_or_deleteall(int argc, char **argv, int deleteall)
713 struct rtnl_handle rth;
717 filter.xpinfo.sel.family = preferred_family;
720 if (strcmp(*argv, "dir") == 0) {
722 xfrm_policy_dir_parse(&filter.xpinfo.dir, &argc, &argv);
724 filter.dir_mask = XFRM_FILTER_MASK_FULL;
726 } else if (strcmp(*argv, "index") == 0) {
728 if (get_u32(&filter.xpinfo.index, *argv, 0))
729 invarg("\"INDEX\" is invalid", *argv);
731 filter.index_mask = XFRM_FILTER_MASK_FULL;
733 } else if (strcmp(*argv, "ptype") == 0) {
735 xfrm_policy_ptype_parse(&filter.ptype, &argc, &argv);
737 filter.ptype_mask = XFRM_FILTER_MASK_FULL;
739 } else if (strcmp(*argv, "action") == 0) {
741 if (strcmp(*argv, "allow") == 0)
742 filter.xpinfo.action = XFRM_POLICY_ALLOW;
743 else if (strcmp(*argv, "block") == 0)
744 filter.xpinfo.action = XFRM_POLICY_BLOCK;
746 invarg("\"ACTION\" is invalid\n", *argv);
748 filter.action_mask = XFRM_FILTER_MASK_FULL;
750 } else if (strcmp(*argv, "priority") == 0) {
752 if (get_u32(&filter.xpinfo.priority, *argv, 0))
753 invarg("\"PRIORITY\" is invalid", *argv);
755 filter.priority_mask = XFRM_FILTER_MASK_FULL;
757 } else if (strcmp(*argv, "flag") == 0) {
759 xfrm_policy_flag_parse(&filter.xpinfo.flags, &argc,
762 filter.policy_flags_mask = XFRM_FILTER_MASK_FULL;
766 invarg("unknown", *argv);
769 xfrm_selector_parse(&filter.xpinfo.sel, &argc, &argv);
770 if (preferred_family == AF_UNSPEC)
771 preferred_family = filter.xpinfo.sel.family;
778 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
782 struct xfrm_buffer xb;
783 char buf[NLMSG_DELETEALL_BUF_SIZE];
787 xb.size = sizeof(buf);
795 fprintf(stderr, "Delete-all round = %d\n", i);
797 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
798 perror("Cannot send dump request");
802 if (rtnl_dump_filter(&rth, xfrm_policy_keep, &xb, NULL, NULL) < 0) {
803 fprintf(stderr, "Delete-all terminated\n");
806 if (xb.nlmsg_count == 0) {
808 fprintf(stderr, "Delete-all completed\n");
812 if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) {
813 perror("Failed to send delete-all request");
817 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
823 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
824 perror("Cannot send dump request");
828 if (rtnl_dump_filter(&rth, xfrm_policy_print, stdout, NULL, NULL) < 0) {
829 fprintf(stderr, "Dump terminated\n");
839 int print_spdinfo( struct nlmsghdr *n, void *arg)
841 FILE *fp = (FILE*)arg;
842 __u32 *f = NLMSG_DATA(n);
843 struct rtattr * tb[XFRMA_SPD_MAX+1];
846 int len = n->nlmsg_len;
848 len -= NLMSG_LENGTH(sizeof(__u32));
850 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
854 rta = XFRMSAPD_RTA(f);
855 parse_rtattr(tb, XFRMA_SPD_MAX, rta, len);
857 fprintf(fp,"\t SPD");
858 if (tb[XFRMA_SPD_INFO]) {
859 struct xfrmu_spdinfo *si;
861 if (RTA_PAYLOAD(tb[XFRMA_SPD_INFO]) < sizeof(*si)) {
862 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
865 si = RTA_DATA(tb[XFRMA_SPD_INFO]);
866 fprintf(fp," IN %d", si->incnt);
867 fprintf(fp," OUT %d", si->outcnt);
868 fprintf(fp," FWD %d", si->fwdcnt);
871 fprintf(fp," (Sock:");
872 fprintf(fp," IN %d", si->inscnt);
873 fprintf(fp," OUT %d", si->outscnt);
874 fprintf(fp," FWD %d", si->fwdscnt);
880 if (show_stats > 1) {
881 struct xfrmu_spdhinfo *sh;
883 if (tb[XFRMA_SPD_HINFO]) {
884 if (RTA_PAYLOAD(tb[XFRMA_SPD_HINFO]) < sizeof(*sh)) {
885 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
888 sh = RTA_DATA(tb[XFRMA_SPD_HINFO]);
889 fprintf(fp,"\t SPD buckets:");
890 fprintf(fp," count %d", sh->spdhcnt);
891 fprintf(fp," Max %d", sh->spdhmcnt);
899 static int xfrm_spd_getinfo(int argc, char **argv)
901 struct rtnl_handle rth;
908 memset(&req, 0, sizeof(req));
910 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(__u32));
911 req.n.nlmsg_flags = NLM_F_REQUEST;
912 req.n.nlmsg_type = XFRM_MSG_GETSPDINFO;
913 req.flags = 0XFFFFFFFF;
915 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
918 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0)
921 print_spdinfo(&req.n, (void*)stdout);
928 static int xfrm_policy_flush(int argc, char **argv)
930 struct rtnl_handle rth;
933 char buf[RTA_BUF_SIZE];
936 struct xfrm_userpolicy_type upt;
938 memset(&req, 0, sizeof(req));
939 memset(&upt, 0, sizeof(upt));
941 req.n.nlmsg_len = NLMSG_LENGTH(0); /* nlmsg data is nothing */
942 req.n.nlmsg_flags = NLM_F_REQUEST;
943 req.n.nlmsg_type = XFRM_MSG_FLUSHPOLICY;
946 if (strcmp(*argv, "ptype") == 0) {
948 duparg("ptype", *argv);
952 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
954 invarg("unknown", *argv);
960 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
961 (void *)&upt, sizeof(upt));
964 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
968 fprintf(stderr, "Flush policy\n");
970 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
978 int do_xfrm_policy(int argc, char **argv)
981 return xfrm_policy_list_or_deleteall(0, NULL, 0);
983 if (matches(*argv, "add") == 0)
984 return xfrm_policy_modify(XFRM_MSG_NEWPOLICY, 0,
986 if (matches(*argv, "update") == 0)
987 return xfrm_policy_modify(XFRM_MSG_UPDPOLICY, 0,
989 if (matches(*argv, "delete") == 0)
990 return xfrm_policy_delete(argc-1, argv+1);
991 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
992 return xfrm_policy_list_or_deleteall(argc-1, argv+1, 1);
993 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
994 || matches(*argv, "lst") == 0)
995 return xfrm_policy_list_or_deleteall(argc-1, argv+1, 0);
996 if (matches(*argv, "get") == 0)
997 return xfrm_policy_get(argc-1, argv+1);
998 if (matches(*argv, "flush") == 0)
999 return xfrm_policy_flush(argc-1, argv+1);
1000 if (matches(*argv, "count") == 0)
1001 return xfrm_spd_getinfo(argc, argv);
1002 if (matches(*argv, "help") == 0)
1004 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv);