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 ] [ 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 ]\n");
61 fprintf(stderr
, "Usage: ip xfrm policy flush [ ptype PTYPE ]\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
, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
78 fprintf(stderr
, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
79 fprintf(stderr
, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] NUMBER ]\n");
81 fprintf(stderr
, "TMPL-LIST := [ TMPL-LIST ] | [ tmpl TMPL ]\n");
82 fprintf(stderr
, "TMPL := ID [ mode MODE ] [ reqid REQID ] [ level LEVEL ]\n");
83 fprintf(stderr
, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n");
85 //fprintf(stderr, "XFRM_PROTO := [ esp | ah | comp ]\n");
86 fprintf(stderr
, "XFRM_PROTO := [ ");
87 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_ESP
));
88 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_AH
));
89 fprintf(stderr
, "%s", strxf_xfrmproto(IPPROTO_COMP
));
90 fprintf(stderr
, " ]\n");
92 fprintf(stderr
, "MODE := [ transport | tunnel | beet ](default=transport)\n");
93 //fprintf(stderr, "REQID - number(default=0)\n");
94 fprintf(stderr
, "LEVEL := [ required | use ](default=required)\n");
99 static int xfrm_policy_dir_parse(__u8
*dir
, int *argcp
, char ***argvp
)
102 char **argv
= *argvp
;
104 if (strcmp(*argv
, "in") == 0)
105 *dir
= XFRM_POLICY_IN
;
106 else if (strcmp(*argv
, "out") == 0)
107 *dir
= XFRM_POLICY_OUT
;
108 else if (strcmp(*argv
, "fwd") == 0)
109 *dir
= XFRM_POLICY_FWD
;
111 invarg("\"DIR\" is invalid", *argv
);
119 static int xfrm_policy_ptype_parse(__u8
*ptype
, int *argcp
, char ***argvp
)
122 char **argv
= *argvp
;
124 if (strcmp(*argv
, "main") == 0)
125 *ptype
= XFRM_POLICY_TYPE_MAIN
;
126 else if (strcmp(*argv
, "sub") == 0)
127 *ptype
= XFRM_POLICY_TYPE_SUB
;
129 invarg("\"PTYPE\" is invalid", *argv
);
137 static int xfrm_tmpl_parse(struct xfrm_user_tmpl
*tmpl
,
138 int *argcp
, char ***argvp
)
141 char **argv
= *argvp
;
145 if (strcmp(*argv
, "mode") == 0) {
147 xfrm_mode_parse(&tmpl
->mode
, &argc
, &argv
);
148 } else if (strcmp(*argv
, "reqid") == 0) {
150 xfrm_reqid_parse(&tmpl
->reqid
, &argc
, &argv
);
151 } else if (strcmp(*argv
, "level") == 0) {
154 if (strcmp(*argv
, "required") == 0)
156 else if (strcmp(*argv
, "use") == 0)
159 invarg("\"LEVEL\" is invalid\n", *argv
);
163 PREV_ARG(); /* back track */
167 xfrm_id_parse(&tmpl
->saddr
, &tmpl
->id
, &tmpl
->family
,
169 if (preferred_family
== AF_UNSPEC
)
170 preferred_family
= tmpl
->family
;
187 static int xfrm_policy_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
189 struct rtnl_handle rth
;
192 struct xfrm_userpolicy_info xpinfo
;
193 char buf
[RTA_BUF_SIZE
];
198 struct xfrm_userpolicy_type upt
;
199 char tmpls_buf
[XFRM_TMPLS_BUF_SIZE
];
202 memset(&req
, 0, sizeof(req
));
203 memset(&upt
, 0, sizeof(upt
));
204 memset(&tmpls_buf
, 0, sizeof(tmpls_buf
));
206 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xpinfo
));
207 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
208 req
.n
.nlmsg_type
= cmd
;
209 req
.xpinfo
.sel
.family
= preferred_family
;
211 req
.xpinfo
.lft
.soft_byte_limit
= XFRM_INF
;
212 req
.xpinfo
.lft
.hard_byte_limit
= XFRM_INF
;
213 req
.xpinfo
.lft
.soft_packet_limit
= XFRM_INF
;
214 req
.xpinfo
.lft
.hard_packet_limit
= XFRM_INF
;
217 if (strcmp(*argv
, "dir") == 0) {
219 duparg("dir", *argv
);
223 xfrm_policy_dir_parse(&req
.xpinfo
.dir
, &argc
, &argv
);
225 filter
.dir_mask
= XFRM_FILTER_MASK_FULL
;
227 } else if (strcmp(*argv
, "index") == 0) {
229 if (get_u32(&req
.xpinfo
.index
, *argv
, 0))
230 invarg("\"INDEX\" is invalid", *argv
);
232 filter
.index_mask
= XFRM_FILTER_MASK_FULL
;
234 } else if (strcmp(*argv
, "ptype") == 0) {
236 duparg("ptype", *argv
);
240 xfrm_policy_ptype_parse(&upt
.type
, &argc
, &argv
);
242 filter
.dir_mask
= XFRM_FILTER_MASK_FULL
;
244 } else if (strcmp(*argv
, "action") == 0) {
246 if (strcmp(*argv
, "allow") == 0)
247 req
.xpinfo
.action
= XFRM_POLICY_ALLOW
;
248 else if (strcmp(*argv
, "block") == 0)
249 req
.xpinfo
.action
= XFRM_POLICY_BLOCK
;
251 invarg("\"action\" value is invalid\n", *argv
);
253 filter
.action_mask
= XFRM_FILTER_MASK_FULL
;
255 } else if (strcmp(*argv
, "priority") == 0) {
257 if (get_u32(&req
.xpinfo
.priority
, *argv
, 0))
258 invarg("\"PRIORITY\" is invalid", *argv
);
260 filter
.priority_mask
= XFRM_FILTER_MASK_FULL
;
262 } else if (strcmp(*argv
, "limit") == 0) {
264 xfrm_lifetime_cfg_parse(&req
.xpinfo
.lft
, &argc
, &argv
);
265 } else if (strcmp(*argv
, "tmpl") == 0) {
266 struct xfrm_user_tmpl
*tmpl
;
268 if (tmpls_len
+ sizeof(*tmpl
) > sizeof(tmpls_buf
)) {
269 fprintf(stderr
, "Too many tmpls: buffer overflow\n");
272 tmpl
= (struct xfrm_user_tmpl
*)((char *)tmpls_buf
+ tmpls_len
);
274 tmpl
->family
= preferred_family
;
275 tmpl
->aalgos
= (~(__u32
)0);
276 tmpl
->ealgos
= (~(__u32
)0);
277 tmpl
->calgos
= (~(__u32
)0);
280 xfrm_tmpl_parse(tmpl
, &argc
, &argv
);
282 tmpls_len
+= sizeof(*tmpl
);
285 duparg("unknown", *argv
);
288 xfrm_selector_parse(&req
.xpinfo
.sel
, &argc
, &argv
);
289 if (preferred_family
== AF_UNSPEC
)
290 preferred_family
= req
.xpinfo
.sel
.family
;
297 fprintf(stderr
, "Not enough information: \"DIR\" is required.\n");
302 addattr_l(&req
.n
, sizeof(req
), XFRMA_POLICY_TYPE
,
303 (void *)&upt
, sizeof(upt
));
307 addattr_l(&req
.n
, sizeof(req
), XFRMA_TMPL
,
308 (void *)tmpls_buf
, tmpls_len
);
311 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
314 if (req
.xpinfo
.sel
.family
== AF_UNSPEC
)
315 req
.xpinfo
.sel
.family
= AF_INET
;
317 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
325 static int xfrm_policy_filter_match(struct xfrm_userpolicy_info
*xpinfo
,
331 if ((xpinfo
->dir
^filter
.xpinfo
.dir
)&filter
.dir_mask
)
334 if ((ptype
^filter
.ptype
)&filter
.ptype_mask
)
337 if (filter
.sel_src_mask
) {
338 if (xfrm_addr_match(&xpinfo
->sel
.saddr
, &filter
.xpinfo
.sel
.saddr
,
339 filter
.sel_src_mask
))
343 if (filter
.sel_dst_mask
) {
344 if (xfrm_addr_match(&xpinfo
->sel
.daddr
, &filter
.xpinfo
.sel
.daddr
,
345 filter
.sel_dst_mask
))
349 if ((xpinfo
->sel
.ifindex
^filter
.xpinfo
.sel
.ifindex
)&filter
.sel_dev_mask
)
352 if ((xpinfo
->sel
.proto
^filter
.xpinfo
.sel
.proto
)&filter
.upspec_proto_mask
)
355 if (filter
.upspec_sport_mask
) {
356 if ((xpinfo
->sel
.sport
^filter
.xpinfo
.sel
.sport
)&filter
.upspec_sport_mask
)
360 if (filter
.upspec_dport_mask
) {
361 if ((xpinfo
->sel
.dport
^filter
.xpinfo
.sel
.dport
)&filter
.upspec_dport_mask
)
365 if ((xpinfo
->index
^filter
.xpinfo
.index
)&filter
.index_mask
)
368 if ((xpinfo
->action
^filter
.xpinfo
.action
)&filter
.action_mask
)
371 if ((xpinfo
->priority
^filter
.xpinfo
.priority
)&filter
.priority_mask
)
377 int xfrm_policy_print(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
380 struct rtattr
* tb
[XFRMA_MAX
+1];
382 struct xfrm_userpolicy_info
*xpinfo
= NULL
;
383 struct xfrm_user_polexpire
*xpexp
= NULL
;
384 struct xfrm_userpolicy_id
*xpid
= NULL
;
385 __u8 ptype
= XFRM_POLICY_TYPE_MAIN
;
386 FILE *fp
= (FILE*)arg
;
387 int len
= n
->nlmsg_len
;
389 if (n
->nlmsg_type
!= XFRM_MSG_NEWPOLICY
&&
390 n
->nlmsg_type
!= XFRM_MSG_DELPOLICY
&&
391 n
->nlmsg_type
!= XFRM_MSG_UPDPOLICY
&&
392 n
->nlmsg_type
!= XFRM_MSG_POLEXPIRE
) {
393 fprintf(stderr
, "Not a policy: %08x %08x %08x\n",
394 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
398 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
) {
399 xpid
= NLMSG_DATA(n
);
400 len
-= NLMSG_SPACE(sizeof(*xpid
));
401 } else if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
) {
402 xpexp
= NLMSG_DATA(n
);
403 xpinfo
= &xpexp
->pol
;
404 len
-= NLMSG_SPACE(sizeof(*xpexp
));
407 xpinfo
= NLMSG_DATA(n
);
408 len
-= NLMSG_SPACE(sizeof(*xpinfo
));
412 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
416 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
)
417 rta
= XFRMPID_RTA(xpid
);
418 else if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
)
419 rta
= XFRMPEXP_RTA(xpexp
);
421 rta
= XFRMP_RTA(xpinfo
);
423 parse_rtattr(tb
, XFRMA_MAX
, rta
, len
);
425 if (tb
[XFRMA_POLICY_TYPE
]) {
426 struct xfrm_userpolicy_type
*upt
;
428 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
)) {
429 fprintf(stderr
, "too short XFRMA_POLICY_TYPE len\n");
432 upt
= (struct xfrm_userpolicy_type
*)RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
436 if (xpinfo
&& !xfrm_policy_filter_match(xpinfo
, ptype
))
439 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
)
440 fprintf(fp
, "Deleted ");
441 else if (n
->nlmsg_type
== XFRM_MSG_UPDPOLICY
)
442 fprintf(fp
, "Updated ");
443 else if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
)
444 fprintf(fp
, "Expired ");
446 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
) {
447 //xfrm_policy_id_print();
448 if (!tb
[XFRMA_POLICY
]) {
449 fprintf(stderr
, "Buggy XFRM_MSG_DELPOLICY: no XFRMA_POLICY\n");
452 if (RTA_PAYLOAD(tb
[XFRMA_POLICY
]) < sizeof(*xpinfo
)) {
453 fprintf(stderr
, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
456 xpinfo
= (struct xfrm_userpolicy_info
*)RTA_DATA(tb
[XFRMA_POLICY
]);
459 xfrm_policy_info_print(xpinfo
, tb
, fp
, NULL
, NULL
);
461 if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
) {
463 fprintf(fp
, "hard %u", xpexp
->hard
);
464 fprintf(fp
, "%s", _SL_
);
474 static int xfrm_policy_get_or_delete(int argc
, char **argv
, int delete,
477 struct rtnl_handle rth
;
480 struct xfrm_userpolicy_id xpid
;
481 char buf
[RTA_BUF_SIZE
];
487 struct xfrm_userpolicy_type upt
;
489 memset(&req
, 0, sizeof(req
));
490 memset(&upt
, 0, sizeof(upt
));
492 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xpid
));
493 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
494 req
.n
.nlmsg_type
= delete ? XFRM_MSG_DELPOLICY
: XFRM_MSG_GETPOLICY
;
497 if (strcmp(*argv
, "dir") == 0) {
499 duparg("dir", *argv
);
503 xfrm_policy_dir_parse(&req
.xpid
.dir
, &argc
, &argv
);
505 } else if (strcmp(*argv
, "index") == 0) {
507 duparg("index", *argv
);
511 if (get_u32(&req
.xpid
.index
, *argv
, 0))
512 invarg("\"INDEX\" is invalid", *argv
);
514 } else if (strcmp(*argv
, "ptype") == 0) {
516 duparg("ptype", *argv
);
520 xfrm_policy_ptype_parse(&upt
.type
, &argc
, &argv
);
524 invarg("unknown", *argv
);
527 xfrm_selector_parse(&req
.xpid
.sel
, &argc
, &argv
);
528 if (preferred_family
== AF_UNSPEC
)
529 preferred_family
= req
.xpid
.sel
.family
;
537 fprintf(stderr
, "Not enough information: \"DIR\" is required.\n");
541 addattr_l(&req
.n
, sizeof(req
), XFRMA_POLICY_TYPE
,
542 (void *)&upt
, sizeof(upt
));
544 if (!selp
&& !indexp
) {
545 fprintf(stderr
, "Not enough information: either \"SELECTOR\" or \"INDEX\" is required.\n");
549 duparg2("SELECTOR", "INDEX");
551 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
554 if (req
.xpid
.sel
.family
== AF_UNSPEC
)
555 req
.xpid
.sel
.family
= AF_INET
;
557 if (rtnl_talk(&rth
, &req
.n
, 0, 0, res_nlbuf
, NULL
, NULL
) < 0)
565 static int xfrm_policy_delete(int argc
, char **argv
)
567 return xfrm_policy_get_or_delete(argc
, argv
, 1, NULL
);
570 static int xfrm_policy_get(int argc
, char **argv
)
572 char buf
[NLMSG_BUF_SIZE
];
573 struct nlmsghdr
*n
= (struct nlmsghdr
*)buf
;
575 memset(buf
, 0, sizeof(buf
));
577 xfrm_policy_get_or_delete(argc
, argv
, 0, n
);
579 if (xfrm_policy_print(NULL
, n
, (void*)stdout
) < 0) {
580 fprintf(stderr
, "An error :-)\n");
588 * With an existing policy of nlmsg, make new nlmsg for deleting the policy
589 * and store it to buffer.
591 static int xfrm_policy_keep(const struct sockaddr_nl
*who
,
595 struct xfrm_buffer
*xb
= (struct xfrm_buffer
*)arg
;
596 struct rtnl_handle
*rth
= xb
->rth
;
597 struct xfrm_userpolicy_info
*xpinfo
= NLMSG_DATA(n
);
598 int len
= n
->nlmsg_len
;
599 struct rtattr
*tb
[XFRMA_MAX
+1];
600 __u8 ptype
= XFRM_POLICY_TYPE_MAIN
;
601 struct nlmsghdr
*new_n
;
602 struct xfrm_userpolicy_id
*xpid
;
604 if (n
->nlmsg_type
!= XFRM_MSG_NEWPOLICY
) {
605 fprintf(stderr
, "Not a policy: %08x %08x %08x\n",
606 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
610 len
-= NLMSG_LENGTH(sizeof(*xpinfo
));
612 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
616 parse_rtattr(tb
, XFRMA_MAX
, XFRMP_RTA(xpinfo
), len
);
618 if (tb
[XFRMA_POLICY_TYPE
]) {
619 struct xfrm_userpolicy_type
*upt
;
621 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
)) {
622 fprintf(stderr
, "too short XFRMA_POLICY_TYPE len\n");
625 upt
= (struct xfrm_userpolicy_type
*)RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
629 if (!xfrm_policy_filter_match(xpinfo
, ptype
))
632 if (xb
->offset
> xb
->size
) {
633 fprintf(stderr
, "Policy buffer overflow\n");
637 new_n
= (struct nlmsghdr
*)(xb
->buf
+ xb
->offset
);
638 new_n
->nlmsg_len
= NLMSG_LENGTH(sizeof(*xpid
));
639 new_n
->nlmsg_flags
= NLM_F_REQUEST
;
640 new_n
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
641 new_n
->nlmsg_seq
= ++rth
->seq
;
643 xpid
= NLMSG_DATA(new_n
);
644 memcpy(&xpid
->sel
, &xpinfo
->sel
, sizeof(xpid
->sel
));
645 xpid
->dir
= xpinfo
->dir
;
646 xpid
->index
= xpinfo
->index
;
648 xb
->offset
+= new_n
->nlmsg_len
;
654 static int xfrm_policy_list_or_deleteall(int argc
, char **argv
, int deleteall
)
657 struct rtnl_handle rth
;
661 filter
.xpinfo
.sel
.family
= preferred_family
;
664 if (strcmp(*argv
, "dir") == 0) {
666 xfrm_policy_dir_parse(&filter
.xpinfo
.dir
, &argc
, &argv
);
668 filter
.dir_mask
= XFRM_FILTER_MASK_FULL
;
670 } else if (strcmp(*argv
, "index") == 0) {
672 if (get_u32(&filter
.xpinfo
.index
, *argv
, 0))
673 invarg("\"INDEX\" is invalid", *argv
);
675 filter
.index_mask
= XFRM_FILTER_MASK_FULL
;
677 } else if (strcmp(*argv
, "ptype") == 0) {
679 xfrm_policy_ptype_parse(&filter
.ptype
, &argc
, &argv
);
681 filter
.ptype_mask
= XFRM_FILTER_MASK_FULL
;
683 } else if (strcmp(*argv
, "action") == 0) {
685 if (strcmp(*argv
, "allow") == 0)
686 filter
.xpinfo
.action
= XFRM_POLICY_ALLOW
;
687 else if (strcmp(*argv
, "block") == 0)
688 filter
.xpinfo
.action
= XFRM_POLICY_BLOCK
;
690 invarg("\"ACTION\" is invalid\n", *argv
);
692 filter
.action_mask
= XFRM_FILTER_MASK_FULL
;
694 } else if (strcmp(*argv
, "priority") == 0) {
696 if (get_u32(&filter
.xpinfo
.priority
, *argv
, 0))
697 invarg("\"PRIORITY\" is invalid", *argv
);
699 filter
.priority_mask
= XFRM_FILTER_MASK_FULL
;
703 invarg("unknown", *argv
);
706 xfrm_selector_parse(&filter
.xpinfo
.sel
, &argc
, &argv
);
707 if (preferred_family
== AF_UNSPEC
)
708 preferred_family
= filter
.xpinfo
.sel
.family
;
715 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
719 struct xfrm_buffer xb
;
720 char buf
[NLMSG_DELETEALL_BUF_SIZE
];
724 xb
.size
= sizeof(buf
);
732 fprintf(stderr
, "Delete-all round = %d\n", i
);
734 if (rtnl_wilddump_request(&rth
, preferred_family
, XFRM_MSG_GETPOLICY
) < 0) {
735 perror("Cannot send dump request");
739 if (rtnl_dump_filter(&rth
, xfrm_policy_keep
, &xb
, NULL
, NULL
) < 0) {
740 fprintf(stderr
, "Delete-all terminated\n");
743 if (xb
.nlmsg_count
== 0) {
745 fprintf(stderr
, "Delete-all completed\n");
749 if (rtnl_send(&rth
, xb
.buf
, xb
.offset
) < 0) {
750 perror("Failed to send delete-all request\n");
754 fprintf(stderr
, "Delete-all nlmsg count = %d\n", xb
.nlmsg_count
);
760 if (rtnl_wilddump_request(&rth
, preferred_family
, XFRM_MSG_GETPOLICY
) < 0) {
761 perror("Cannot send dump request");
765 if (rtnl_dump_filter(&rth
, xfrm_policy_print
, stdout
, NULL
, NULL
) < 0) {
766 fprintf(stderr
, "Dump terminated\n");
776 static int xfrm_policy_flush(int argc
, char **argv
)
778 struct rtnl_handle rth
;
781 char buf
[RTA_BUF_SIZE
];
784 struct xfrm_userpolicy_type upt
;
786 memset(&req
, 0, sizeof(req
));
787 memset(&upt
, 0, sizeof(upt
));
789 req
.n
.nlmsg_len
= NLMSG_LENGTH(0); /* nlmsg data is nothing */
790 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
791 req
.n
.nlmsg_type
= XFRM_MSG_FLUSHPOLICY
;
794 if (strcmp(*argv
, "ptype") == 0) {
796 duparg("ptype", *argv
);
800 xfrm_policy_ptype_parse(&upt
.type
, &argc
, &argv
);
802 filter
.dir_mask
= XFRM_FILTER_MASK_FULL
;
804 invarg("unknown", *argv
);
810 addattr_l(&req
.n
, sizeof(req
), XFRMA_POLICY_TYPE
,
811 (void *)&upt
, sizeof(upt
));
814 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
818 fprintf(stderr
, "Flush policy\n");
820 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
828 int do_xfrm_policy(int argc
, char **argv
)
831 return xfrm_policy_list_or_deleteall(0, NULL
, 0);
833 if (matches(*argv
, "add") == 0)
834 return xfrm_policy_modify(XFRM_MSG_NEWPOLICY
, 0,
836 if (matches(*argv
, "update") == 0)
837 return xfrm_policy_modify(XFRM_MSG_UPDPOLICY
, 0,
839 if (matches(*argv
, "delete") == 0)
840 return xfrm_policy_delete(argc
-1, argv
+1);
841 if (matches(*argv
, "deleteall") == 0 || matches(*argv
, "delall") == 0)
842 return xfrm_policy_list_or_deleteall(argc
-1, argv
+1, 1);
843 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
844 || matches(*argv
, "lst") == 0)
845 return xfrm_policy_list_or_deleteall(argc
-1, argv
+1, 0);
846 if (matches(*argv
, "get") == 0)
847 return xfrm_policy_get(argc
-1, argv
+1);
848 if (matches(*argv
, "flush") == 0)
849 return xfrm_policy_flush(argc
-1, argv
+1);
850 if (matches(*argv
, "help") == 0)
852 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv
);