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 ]\n");
58 fprintf(stderr
, "Usage: ip xfrm policy { delete | get } dir DIR [ SELECTOR | index INDEX ] [ ptype PTYPE ]\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");
71 //fprintf(stderr, "DEV - device name(default=none)\n");
73 fprintf(stderr
, "ACTION := [ allow | block ](default=allow)\n");
75 //fprintf(stderr, "PRIORITY - priority value(default=0)\n");
77 fprintf(stderr
, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
78 fprintf(stderr
, "FLAG := [ localok ]\n");
80 fprintf(stderr
, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
81 fprintf(stderr
, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
82 fprintf(stderr
, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] NUMBER ]\n");
84 fprintf(stderr
, "TMPL-LIST := [ TMPL-LIST ] | [ tmpl TMPL ]\n");
85 fprintf(stderr
, "TMPL := ID [ mode MODE ] [ reqid REQID ] [ level LEVEL ]\n");
86 fprintf(stderr
, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n");
88 fprintf(stderr
, "XFRM_PROTO := [ ");
89 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_ESP
));
90 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_AH
));
91 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_COMP
));
92 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING
));
93 fprintf(stderr
, "%s ", strxf_xfrmproto(IPPROTO_DSTOPTS
));
94 fprintf(stderr
, "]\n");
96 fprintf(stderr
, "MODE := [ transport | tunnel | beet ](default=transport)\n");
97 //fprintf(stderr, "REQID - number(default=0)\n");
98 fprintf(stderr
, "LEVEL := [ required | use ](default=required)\n");
103 static int xfrm_policy_dir_parse(__u8
*dir
, int *argcp
, char ***argvp
)
106 char **argv
= *argvp
;
108 if (strcmp(*argv
, "in") == 0)
109 *dir
= XFRM_POLICY_IN
;
110 else if (strcmp(*argv
, "out") == 0)
111 *dir
= XFRM_POLICY_OUT
;
112 else if (strcmp(*argv
, "fwd") == 0)
113 *dir
= XFRM_POLICY_FWD
;
115 invarg("\"DIR\" is invalid", *argv
);
123 static int xfrm_policy_ptype_parse(__u8
*ptype
, int *argcp
, char ***argvp
)
126 char **argv
= *argvp
;
128 if (strcmp(*argv
, "main") == 0)
129 *ptype
= XFRM_POLICY_TYPE_MAIN
;
130 else if (strcmp(*argv
, "sub") == 0)
131 *ptype
= XFRM_POLICY_TYPE_SUB
;
133 invarg("\"PTYPE\" is invalid", *argv
);
141 static int xfrm_policy_flag_parse(__u8
*flags
, int *argcp
, char ***argvp
)
144 char **argv
= *argvp
;
145 int len
= strlen(*argv
);
147 if (len
> 2 && strncmp(*argv
, "0x", 2) == 0) {
150 if (get_u8(&val
, *argv
, 16))
151 invarg("\"FLAG\" is invalid", *argv
);
155 if (strcmp(*argv
, "localok") == 0)
156 *flags
|= XFRM_POLICY_LOCALOK
;
158 PREV_ARG(); /* back track */
174 static int xfrm_tmpl_parse(struct xfrm_user_tmpl
*tmpl
,
175 int *argcp
, char ***argvp
)
178 char **argv
= *argvp
;
182 if (strcmp(*argv
, "mode") == 0) {
184 xfrm_mode_parse(&tmpl
->mode
, &argc
, &argv
);
185 } else if (strcmp(*argv
, "reqid") == 0) {
187 xfrm_reqid_parse(&tmpl
->reqid
, &argc
, &argv
);
188 } else if (strcmp(*argv
, "level") == 0) {
191 if (strcmp(*argv
, "required") == 0)
193 else if (strcmp(*argv
, "use") == 0)
196 invarg("\"LEVEL\" is invalid\n", *argv
);
200 PREV_ARG(); /* back track */
204 xfrm_id_parse(&tmpl
->saddr
, &tmpl
->id
, &tmpl
->family
,
206 if (preferred_family
== AF_UNSPEC
)
207 preferred_family
= tmpl
->family
;
224 static int xfrm_policy_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
226 struct rtnl_handle rth
;
229 struct xfrm_userpolicy_info xpinfo
;
230 char buf
[RTA_BUF_SIZE
];
235 struct xfrm_userpolicy_type upt
;
236 char tmpls_buf
[XFRM_TMPLS_BUF_SIZE
];
239 memset(&req
, 0, sizeof(req
));
240 memset(&upt
, 0, sizeof(upt
));
241 memset(&tmpls_buf
, 0, sizeof(tmpls_buf
));
243 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xpinfo
));
244 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
245 req
.n
.nlmsg_type
= cmd
;
246 req
.xpinfo
.sel
.family
= preferred_family
;
248 req
.xpinfo
.lft
.soft_byte_limit
= XFRM_INF
;
249 req
.xpinfo
.lft
.hard_byte_limit
= XFRM_INF
;
250 req
.xpinfo
.lft
.soft_packet_limit
= XFRM_INF
;
251 req
.xpinfo
.lft
.hard_packet_limit
= XFRM_INF
;
254 if (strcmp(*argv
, "dir") == 0) {
256 duparg("dir", *argv
);
260 xfrm_policy_dir_parse(&req
.xpinfo
.dir
, &argc
, &argv
);
261 } else if (strcmp(*argv
, "index") == 0) {
263 if (get_u32(&req
.xpinfo
.index
, *argv
, 0))
264 invarg("\"INDEX\" is invalid", *argv
);
265 } else if (strcmp(*argv
, "ptype") == 0) {
267 duparg("ptype", *argv
);
271 xfrm_policy_ptype_parse(&upt
.type
, &argc
, &argv
);
272 } else if (strcmp(*argv
, "action") == 0) {
274 if (strcmp(*argv
, "allow") == 0)
275 req
.xpinfo
.action
= XFRM_POLICY_ALLOW
;
276 else if (strcmp(*argv
, "block") == 0)
277 req
.xpinfo
.action
= XFRM_POLICY_BLOCK
;
279 invarg("\"action\" value is invalid\n", *argv
);
280 } else if (strcmp(*argv
, "priority") == 0) {
282 if (get_u32(&req
.xpinfo
.priority
, *argv
, 0))
283 invarg("\"PRIORITY\" is invalid", *argv
);
284 } else if (strcmp(*argv
, "flag") == 0) {
286 xfrm_policy_flag_parse(&req
.xpinfo
.flags
, &argc
,
288 } else if (strcmp(*argv
, "limit") == 0) {
290 xfrm_lifetime_cfg_parse(&req
.xpinfo
.lft
, &argc
, &argv
);
291 } else if (strcmp(*argv
, "tmpl") == 0) {
292 struct xfrm_user_tmpl
*tmpl
;
294 if (tmpls_len
+ sizeof(*tmpl
) > sizeof(tmpls_buf
)) {
295 fprintf(stderr
, "Too many tmpls: buffer overflow\n");
298 tmpl
= (struct xfrm_user_tmpl
*)((char *)tmpls_buf
+ tmpls_len
);
300 tmpl
->family
= preferred_family
;
301 tmpl
->aalgos
= (~(__u32
)0);
302 tmpl
->ealgos
= (~(__u32
)0);
303 tmpl
->calgos
= (~(__u32
)0);
306 xfrm_tmpl_parse(tmpl
, &argc
, &argv
);
308 tmpls_len
+= sizeof(*tmpl
);
311 duparg("unknown", *argv
);
314 xfrm_selector_parse(&req
.xpinfo
.sel
, &argc
, &argv
);
315 if (preferred_family
== AF_UNSPEC
)
316 preferred_family
= req
.xpinfo
.sel
.family
;
323 fprintf(stderr
, "Not enough information: \"DIR\" is required.\n");
328 addattr_l(&req
.n
, sizeof(req
), XFRMA_POLICY_TYPE
,
329 (void *)&upt
, sizeof(upt
));
333 addattr_l(&req
.n
, sizeof(req
), XFRMA_TMPL
,
334 (void *)tmpls_buf
, tmpls_len
);
337 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
340 if (req
.xpinfo
.sel
.family
== AF_UNSPEC
)
341 req
.xpinfo
.sel
.family
= AF_INET
;
343 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
351 static int xfrm_policy_filter_match(struct xfrm_userpolicy_info
*xpinfo
,
357 if ((xpinfo
->dir
^filter
.xpinfo
.dir
)&filter
.dir_mask
)
360 if ((ptype
^filter
.ptype
)&filter
.ptype_mask
)
363 if (filter
.sel_src_mask
) {
364 if (xfrm_addr_match(&xpinfo
->sel
.saddr
, &filter
.xpinfo
.sel
.saddr
,
365 filter
.sel_src_mask
))
369 if (filter
.sel_dst_mask
) {
370 if (xfrm_addr_match(&xpinfo
->sel
.daddr
, &filter
.xpinfo
.sel
.daddr
,
371 filter
.sel_dst_mask
))
375 if ((xpinfo
->sel
.ifindex
^filter
.xpinfo
.sel
.ifindex
)&filter
.sel_dev_mask
)
378 if ((xpinfo
->sel
.proto
^filter
.xpinfo
.sel
.proto
)&filter
.upspec_proto_mask
)
381 if (filter
.upspec_sport_mask
) {
382 if ((xpinfo
->sel
.sport
^filter
.xpinfo
.sel
.sport
)&filter
.upspec_sport_mask
)
386 if (filter
.upspec_dport_mask
) {
387 if ((xpinfo
->sel
.dport
^filter
.xpinfo
.sel
.dport
)&filter
.upspec_dport_mask
)
391 if ((xpinfo
->index
^filter
.xpinfo
.index
)&filter
.index_mask
)
394 if ((xpinfo
->action
^filter
.xpinfo
.action
)&filter
.action_mask
)
397 if ((xpinfo
->priority
^filter
.xpinfo
.priority
)&filter
.priority_mask
)
400 if (filter
.policy_flags_mask
)
401 if ((xpinfo
->flags
& filter
.xpinfo
.flags
) == 0)
407 int xfrm_policy_print(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
410 struct rtattr
* tb
[XFRMA_MAX
+1];
412 struct xfrm_userpolicy_info
*xpinfo
= NULL
;
413 struct xfrm_user_polexpire
*xpexp
= NULL
;
414 struct xfrm_userpolicy_id
*xpid
= NULL
;
415 __u8 ptype
= XFRM_POLICY_TYPE_MAIN
;
416 FILE *fp
= (FILE*)arg
;
417 int len
= n
->nlmsg_len
;
419 if (n
->nlmsg_type
!= XFRM_MSG_NEWPOLICY
&&
420 n
->nlmsg_type
!= XFRM_MSG_DELPOLICY
&&
421 n
->nlmsg_type
!= XFRM_MSG_UPDPOLICY
&&
422 n
->nlmsg_type
!= XFRM_MSG_POLEXPIRE
) {
423 fprintf(stderr
, "Not a policy: %08x %08x %08x\n",
424 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
428 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
) {
429 xpid
= NLMSG_DATA(n
);
430 len
-= NLMSG_SPACE(sizeof(*xpid
));
431 } else if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
) {
432 xpexp
= NLMSG_DATA(n
);
433 xpinfo
= &xpexp
->pol
;
434 len
-= NLMSG_SPACE(sizeof(*xpexp
));
437 xpinfo
= NLMSG_DATA(n
);
438 len
-= NLMSG_SPACE(sizeof(*xpinfo
));
442 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
446 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
)
447 rta
= XFRMPID_RTA(xpid
);
448 else if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
)
449 rta
= XFRMPEXP_RTA(xpexp
);
451 rta
= XFRMP_RTA(xpinfo
);
453 parse_rtattr(tb
, XFRMA_MAX
, rta
, len
);
455 if (tb
[XFRMA_POLICY_TYPE
]) {
456 struct xfrm_userpolicy_type
*upt
;
458 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
)) {
459 fprintf(stderr
, "too short XFRMA_POLICY_TYPE len\n");
462 upt
= (struct xfrm_userpolicy_type
*)RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
466 if (xpinfo
&& !xfrm_policy_filter_match(xpinfo
, ptype
))
469 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
)
470 fprintf(fp
, "Deleted ");
471 else if (n
->nlmsg_type
== XFRM_MSG_UPDPOLICY
)
472 fprintf(fp
, "Updated ");
473 else if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
)
474 fprintf(fp
, "Expired ");
476 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
) {
477 //xfrm_policy_id_print();
478 if (!tb
[XFRMA_POLICY
]) {
479 fprintf(stderr
, "Buggy XFRM_MSG_DELPOLICY: no XFRMA_POLICY\n");
482 if (RTA_PAYLOAD(tb
[XFRMA_POLICY
]) < sizeof(*xpinfo
)) {
483 fprintf(stderr
, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
486 xpinfo
= (struct xfrm_userpolicy_info
*)RTA_DATA(tb
[XFRMA_POLICY
]);
489 xfrm_policy_info_print(xpinfo
, tb
, fp
, NULL
, NULL
);
491 if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
) {
493 fprintf(fp
, "hard %u", xpexp
->hard
);
494 fprintf(fp
, "%s", _SL_
);
504 static int xfrm_policy_get_or_delete(int argc
, char **argv
, int delete,
507 struct rtnl_handle rth
;
510 struct xfrm_userpolicy_id xpid
;
511 char buf
[RTA_BUF_SIZE
];
517 struct xfrm_userpolicy_type upt
;
519 memset(&req
, 0, sizeof(req
));
520 memset(&upt
, 0, sizeof(upt
));
522 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xpid
));
523 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
524 req
.n
.nlmsg_type
= delete ? XFRM_MSG_DELPOLICY
: XFRM_MSG_GETPOLICY
;
527 if (strcmp(*argv
, "dir") == 0) {
529 duparg("dir", *argv
);
533 xfrm_policy_dir_parse(&req
.xpid
.dir
, &argc
, &argv
);
535 } else if (strcmp(*argv
, "index") == 0) {
537 duparg("index", *argv
);
541 if (get_u32(&req
.xpid
.index
, *argv
, 0))
542 invarg("\"INDEX\" is invalid", *argv
);
544 } else if (strcmp(*argv
, "ptype") == 0) {
546 duparg("ptype", *argv
);
550 xfrm_policy_ptype_parse(&upt
.type
, &argc
, &argv
);
554 invarg("unknown", *argv
);
557 xfrm_selector_parse(&req
.xpid
.sel
, &argc
, &argv
);
558 if (preferred_family
== AF_UNSPEC
)
559 preferred_family
= req
.xpid
.sel
.family
;
567 fprintf(stderr
, "Not enough information: \"DIR\" is required.\n");
571 addattr_l(&req
.n
, sizeof(req
), XFRMA_POLICY_TYPE
,
572 (void *)&upt
, sizeof(upt
));
574 if (!selp
&& !indexp
) {
575 fprintf(stderr
, "Not enough information: either \"SELECTOR\" or \"INDEX\" is required.\n");
579 duparg2("SELECTOR", "INDEX");
581 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
584 if (req
.xpid
.sel
.family
== AF_UNSPEC
)
585 req
.xpid
.sel
.family
= AF_INET
;
587 if (rtnl_talk(&rth
, &req
.n
, 0, 0, res_nlbuf
, NULL
, NULL
) < 0)
595 static int xfrm_policy_delete(int argc
, char **argv
)
597 return xfrm_policy_get_or_delete(argc
, argv
, 1, NULL
);
600 static int xfrm_policy_get(int argc
, char **argv
)
602 char buf
[NLMSG_BUF_SIZE
];
603 struct nlmsghdr
*n
= (struct nlmsghdr
*)buf
;
605 memset(buf
, 0, sizeof(buf
));
607 xfrm_policy_get_or_delete(argc
, argv
, 0, n
);
609 if (xfrm_policy_print(NULL
, n
, (void*)stdout
) < 0) {
610 fprintf(stderr
, "An error :-)\n");
618 * With an existing policy of nlmsg, make new nlmsg for deleting the policy
619 * and store it to buffer.
621 static int xfrm_policy_keep(const struct sockaddr_nl
*who
,
625 struct xfrm_buffer
*xb
= (struct xfrm_buffer
*)arg
;
626 struct rtnl_handle
*rth
= xb
->rth
;
627 struct xfrm_userpolicy_info
*xpinfo
= NLMSG_DATA(n
);
628 int len
= n
->nlmsg_len
;
629 struct rtattr
*tb
[XFRMA_MAX
+1];
630 __u8 ptype
= XFRM_POLICY_TYPE_MAIN
;
631 struct nlmsghdr
*new_n
;
632 struct xfrm_userpolicy_id
*xpid
;
634 if (n
->nlmsg_type
!= XFRM_MSG_NEWPOLICY
) {
635 fprintf(stderr
, "Not a policy: %08x %08x %08x\n",
636 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
640 len
-= NLMSG_LENGTH(sizeof(*xpinfo
));
642 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
646 parse_rtattr(tb
, XFRMA_MAX
, XFRMP_RTA(xpinfo
), len
);
648 if (tb
[XFRMA_POLICY_TYPE
]) {
649 struct xfrm_userpolicy_type
*upt
;
651 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
)) {
652 fprintf(stderr
, "too short XFRMA_POLICY_TYPE len\n");
655 upt
= (struct xfrm_userpolicy_type
*)RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
659 if (!xfrm_policy_filter_match(xpinfo
, ptype
))
662 if (xb
->offset
> xb
->size
) {
663 fprintf(stderr
, "Policy buffer overflow\n");
667 new_n
= (struct nlmsghdr
*)(xb
->buf
+ xb
->offset
);
668 new_n
->nlmsg_len
= NLMSG_LENGTH(sizeof(*xpid
));
669 new_n
->nlmsg_flags
= NLM_F_REQUEST
;
670 new_n
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
671 new_n
->nlmsg_seq
= ++rth
->seq
;
673 xpid
= NLMSG_DATA(new_n
);
674 memcpy(&xpid
->sel
, &xpinfo
->sel
, sizeof(xpid
->sel
));
675 xpid
->dir
= xpinfo
->dir
;
676 xpid
->index
= xpinfo
->index
;
678 xb
->offset
+= new_n
->nlmsg_len
;
684 static int xfrm_policy_list_or_deleteall(int argc
, char **argv
, int deleteall
)
687 struct rtnl_handle rth
;
691 filter
.xpinfo
.sel
.family
= preferred_family
;
694 if (strcmp(*argv
, "dir") == 0) {
696 xfrm_policy_dir_parse(&filter
.xpinfo
.dir
, &argc
, &argv
);
698 filter
.dir_mask
= XFRM_FILTER_MASK_FULL
;
700 } else if (strcmp(*argv
, "index") == 0) {
702 if (get_u32(&filter
.xpinfo
.index
, *argv
, 0))
703 invarg("\"INDEX\" is invalid", *argv
);
705 filter
.index_mask
= XFRM_FILTER_MASK_FULL
;
707 } else if (strcmp(*argv
, "ptype") == 0) {
709 xfrm_policy_ptype_parse(&filter
.ptype
, &argc
, &argv
);
711 filter
.ptype_mask
= XFRM_FILTER_MASK_FULL
;
713 } else if (strcmp(*argv
, "action") == 0) {
715 if (strcmp(*argv
, "allow") == 0)
716 filter
.xpinfo
.action
= XFRM_POLICY_ALLOW
;
717 else if (strcmp(*argv
, "block") == 0)
718 filter
.xpinfo
.action
= XFRM_POLICY_BLOCK
;
720 invarg("\"ACTION\" is invalid\n", *argv
);
722 filter
.action_mask
= XFRM_FILTER_MASK_FULL
;
724 } else if (strcmp(*argv
, "priority") == 0) {
726 if (get_u32(&filter
.xpinfo
.priority
, *argv
, 0))
727 invarg("\"PRIORITY\" is invalid", *argv
);
729 filter
.priority_mask
= XFRM_FILTER_MASK_FULL
;
731 } else if (strcmp(*argv
, "flag") == 0) {
733 xfrm_policy_flag_parse(&filter
.xpinfo
.flags
, &argc
,
736 filter
.policy_flags_mask
= XFRM_FILTER_MASK_FULL
;
740 invarg("unknown", *argv
);
743 xfrm_selector_parse(&filter
.xpinfo
.sel
, &argc
, &argv
);
744 if (preferred_family
== AF_UNSPEC
)
745 preferred_family
= filter
.xpinfo
.sel
.family
;
752 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
756 struct xfrm_buffer xb
;
757 char buf
[NLMSG_DELETEALL_BUF_SIZE
];
761 xb
.size
= sizeof(buf
);
769 fprintf(stderr
, "Delete-all round = %d\n", i
);
771 if (rtnl_wilddump_request(&rth
, preferred_family
, XFRM_MSG_GETPOLICY
) < 0) {
772 perror("Cannot send dump request");
776 if (rtnl_dump_filter(&rth
, xfrm_policy_keep
, &xb
, NULL
, NULL
) < 0) {
777 fprintf(stderr
, "Delete-all terminated\n");
780 if (xb
.nlmsg_count
== 0) {
782 fprintf(stderr
, "Delete-all completed\n");
786 if (rtnl_send(&rth
, xb
.buf
, xb
.offset
) < 0) {
787 perror("Failed to send delete-all request");
791 fprintf(stderr
, "Delete-all nlmsg count = %d\n", xb
.nlmsg_count
);
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_print
, stdout
, NULL
, NULL
) < 0) {
803 fprintf(stderr
, "Dump terminated\n");
813 int print_spdinfo( struct nlmsghdr
*n
, void *arg
)
815 FILE *fp
= (FILE*)arg
;
816 __u32
*f
= NLMSG_DATA(n
);
817 struct rtattr
* tb
[XFRMA_SPD_MAX
+1];
820 int len
= n
->nlmsg_len
;
822 len
-= NLMSG_LENGTH(sizeof(__u32
));
824 fprintf(stderr
, "SPDinfo: Wrong len %d\n", len
);
828 rta
= XFRMSAPD_RTA(f
);
829 parse_rtattr(tb
, XFRMA_SPD_MAX
, rta
, len
);
831 fprintf(fp
,"\t SPD");
832 if (tb
[XFRMA_SPD_INFO
]) {
833 struct xfrmu_spdinfo
*si
;
835 if (RTA_PAYLOAD(tb
[XFRMA_SPD_INFO
]) < sizeof(*si
)) {
836 fprintf(stderr
, "SPDinfo: Wrong len %d\n", len
);
839 si
= RTA_DATA(tb
[XFRMA_SPD_INFO
]);
840 fprintf(fp
," IN %d", si
->incnt
);
841 fprintf(fp
," OUT %d", si
->outcnt
);
842 fprintf(fp
," FWD %d", si
->fwdcnt
);
845 fprintf(fp
," (Sock:");
846 fprintf(fp
," IN %d", si
->inscnt
);
847 fprintf(fp
," OUT %d", si
->outscnt
);
848 fprintf(fp
," FWD %d", si
->fwdscnt
);
854 if (show_stats
> 1) {
855 struct xfrmu_spdhinfo
*sh
;
857 if (tb
[XFRMA_SPD_HINFO
]) {
858 if (RTA_PAYLOAD(tb
[XFRMA_SPD_HINFO
]) < sizeof(*sh
)) {
859 fprintf(stderr
, "SPDinfo: Wrong len %d\n", len
);
862 sh
= RTA_DATA(tb
[XFRMA_SPD_HINFO
]);
863 fprintf(fp
,"\t SPD buckets:");
864 fprintf(fp
," count %d", sh
->spdhcnt
);
865 fprintf(fp
," Max %d", sh
->spdhmcnt
);
873 static int xfrm_spd_getinfo(int argc
, char **argv
)
875 struct rtnl_handle rth
;
882 memset(&req
, 0, sizeof(req
));
884 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(__u32
));
885 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
886 req
.n
.nlmsg_type
= XFRM_MSG_GETSPDINFO
;
887 req
.flags
= 0XFFFFFFFF;
889 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
892 if (rtnl_talk(&rth
, &req
.n
, 0, 0, &req
.n
, NULL
, NULL
) < 0)
895 print_spdinfo(&req
.n
, (void*)stdout
);
902 static int xfrm_policy_flush(int argc
, char **argv
)
904 struct rtnl_handle rth
;
907 char buf
[RTA_BUF_SIZE
];
910 struct xfrm_userpolicy_type upt
;
912 memset(&req
, 0, sizeof(req
));
913 memset(&upt
, 0, sizeof(upt
));
915 req
.n
.nlmsg_len
= NLMSG_LENGTH(0); /* nlmsg data is nothing */
916 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
917 req
.n
.nlmsg_type
= XFRM_MSG_FLUSHPOLICY
;
920 if (strcmp(*argv
, "ptype") == 0) {
922 duparg("ptype", *argv
);
926 xfrm_policy_ptype_parse(&upt
.type
, &argc
, &argv
);
928 invarg("unknown", *argv
);
934 addattr_l(&req
.n
, sizeof(req
), XFRMA_POLICY_TYPE
,
935 (void *)&upt
, sizeof(upt
));
938 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
942 fprintf(stderr
, "Flush policy\n");
944 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
952 int do_xfrm_policy(int argc
, char **argv
)
955 return xfrm_policy_list_or_deleteall(0, NULL
, 0);
957 if (matches(*argv
, "add") == 0)
958 return xfrm_policy_modify(XFRM_MSG_NEWPOLICY
, 0,
960 if (matches(*argv
, "update") == 0)
961 return xfrm_policy_modify(XFRM_MSG_UPDPOLICY
, 0,
963 if (matches(*argv
, "delete") == 0)
964 return xfrm_policy_delete(argc
-1, argv
+1);
965 if (matches(*argv
, "deleteall") == 0 || matches(*argv
, "delall") == 0)
966 return xfrm_policy_list_or_deleteall(argc
-1, argv
+1, 1);
967 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
968 || matches(*argv
, "lst") == 0)
969 return xfrm_policy_list_or_deleteall(argc
-1, argv
+1, 0);
970 if (matches(*argv
, "get") == 0)
971 return xfrm_policy_get(argc
-1, argv
+1);
972 if (matches(*argv
, "flush") == 0)
973 return xfrm_policy_flush(argc
-1, argv
+1);
974 if (matches(*argv
, "count") == 0)
975 return xfrm_spd_getinfo(argc
, argv
);
976 if (matches(*argv
, "help") == 0)
978 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv
);