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 := [ ");
86 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_ESP
));
87 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_AH
));
88 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_COMP
));
89 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING
));
90 fprintf(stderr
, "%s ", strxf_xfrmproto(IPPROTO_DSTOPTS
));
91 fprintf(stderr
, "]\n");
93 fprintf(stderr
, "MODE := [ transport | tunnel | beet ](default=transport)\n");
94 //fprintf(stderr, "REQID - number(default=0)\n");
95 fprintf(stderr
, "LEVEL := [ required | use ](default=required)\n");
100 static int xfrm_policy_dir_parse(__u8
*dir
, int *argcp
, char ***argvp
)
103 char **argv
= *argvp
;
105 if (strcmp(*argv
, "in") == 0)
106 *dir
= XFRM_POLICY_IN
;
107 else if (strcmp(*argv
, "out") == 0)
108 *dir
= XFRM_POLICY_OUT
;
109 else if (strcmp(*argv
, "fwd") == 0)
110 *dir
= XFRM_POLICY_FWD
;
112 invarg("\"DIR\" is invalid", *argv
);
120 static int xfrm_policy_ptype_parse(__u8
*ptype
, int *argcp
, char ***argvp
)
123 char **argv
= *argvp
;
125 if (strcmp(*argv
, "main") == 0)
126 *ptype
= XFRM_POLICY_TYPE_MAIN
;
127 else if (strcmp(*argv
, "sub") == 0)
128 *ptype
= XFRM_POLICY_TYPE_SUB
;
130 invarg("\"PTYPE\" is invalid", *argv
);
138 static int xfrm_tmpl_parse(struct xfrm_user_tmpl
*tmpl
,
139 int *argcp
, char ***argvp
)
142 char **argv
= *argvp
;
146 if (strcmp(*argv
, "mode") == 0) {
148 xfrm_mode_parse(&tmpl
->mode
, &argc
, &argv
);
149 } else if (strcmp(*argv
, "reqid") == 0) {
151 xfrm_reqid_parse(&tmpl
->reqid
, &argc
, &argv
);
152 } else if (strcmp(*argv
, "level") == 0) {
155 if (strcmp(*argv
, "required") == 0)
157 else if (strcmp(*argv
, "use") == 0)
160 invarg("\"LEVEL\" is invalid\n", *argv
);
164 PREV_ARG(); /* back track */
168 xfrm_id_parse(&tmpl
->saddr
, &tmpl
->id
, &tmpl
->family
,
170 if (preferred_family
== AF_UNSPEC
)
171 preferred_family
= tmpl
->family
;
188 static int xfrm_policy_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
190 struct rtnl_handle rth
;
193 struct xfrm_userpolicy_info xpinfo
;
194 char buf
[RTA_BUF_SIZE
];
199 struct xfrm_userpolicy_type upt
;
200 char tmpls_buf
[XFRM_TMPLS_BUF_SIZE
];
203 memset(&req
, 0, sizeof(req
));
204 memset(&upt
, 0, sizeof(upt
));
205 memset(&tmpls_buf
, 0, sizeof(tmpls_buf
));
207 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xpinfo
));
208 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
209 req
.n
.nlmsg_type
= cmd
;
210 req
.xpinfo
.sel
.family
= preferred_family
;
212 req
.xpinfo
.lft
.soft_byte_limit
= XFRM_INF
;
213 req
.xpinfo
.lft
.hard_byte_limit
= XFRM_INF
;
214 req
.xpinfo
.lft
.soft_packet_limit
= XFRM_INF
;
215 req
.xpinfo
.lft
.hard_packet_limit
= XFRM_INF
;
218 if (strcmp(*argv
, "dir") == 0) {
220 duparg("dir", *argv
);
224 xfrm_policy_dir_parse(&req
.xpinfo
.dir
, &argc
, &argv
);
226 filter
.dir_mask
= XFRM_FILTER_MASK_FULL
;
228 } else if (strcmp(*argv
, "index") == 0) {
230 if (get_u32(&req
.xpinfo
.index
, *argv
, 0))
231 invarg("\"INDEX\" is invalid", *argv
);
233 filter
.index_mask
= XFRM_FILTER_MASK_FULL
;
235 } else if (strcmp(*argv
, "ptype") == 0) {
237 duparg("ptype", *argv
);
241 xfrm_policy_ptype_parse(&upt
.type
, &argc
, &argv
);
243 filter
.dir_mask
= XFRM_FILTER_MASK_FULL
;
245 } else if (strcmp(*argv
, "action") == 0) {
247 if (strcmp(*argv
, "allow") == 0)
248 req
.xpinfo
.action
= XFRM_POLICY_ALLOW
;
249 else if (strcmp(*argv
, "block") == 0)
250 req
.xpinfo
.action
= XFRM_POLICY_BLOCK
;
252 invarg("\"action\" value is invalid\n", *argv
);
254 filter
.action_mask
= XFRM_FILTER_MASK_FULL
;
256 } else if (strcmp(*argv
, "priority") == 0) {
258 if (get_u32(&req
.xpinfo
.priority
, *argv
, 0))
259 invarg("\"PRIORITY\" is invalid", *argv
);
261 filter
.priority_mask
= XFRM_FILTER_MASK_FULL
;
263 } else if (strcmp(*argv
, "limit") == 0) {
265 xfrm_lifetime_cfg_parse(&req
.xpinfo
.lft
, &argc
, &argv
);
266 } else if (strcmp(*argv
, "tmpl") == 0) {
267 struct xfrm_user_tmpl
*tmpl
;
269 if (tmpls_len
+ sizeof(*tmpl
) > sizeof(tmpls_buf
)) {
270 fprintf(stderr
, "Too many tmpls: buffer overflow\n");
273 tmpl
= (struct xfrm_user_tmpl
*)((char *)tmpls_buf
+ tmpls_len
);
275 tmpl
->family
= preferred_family
;
276 tmpl
->aalgos
= (~(__u32
)0);
277 tmpl
->ealgos
= (~(__u32
)0);
278 tmpl
->calgos
= (~(__u32
)0);
281 xfrm_tmpl_parse(tmpl
, &argc
, &argv
);
283 tmpls_len
+= sizeof(*tmpl
);
286 duparg("unknown", *argv
);
289 xfrm_selector_parse(&req
.xpinfo
.sel
, &argc
, &argv
);
290 if (preferred_family
== AF_UNSPEC
)
291 preferred_family
= req
.xpinfo
.sel
.family
;
298 fprintf(stderr
, "Not enough information: \"DIR\" is required.\n");
303 addattr_l(&req
.n
, sizeof(req
), XFRMA_POLICY_TYPE
,
304 (void *)&upt
, sizeof(upt
));
308 addattr_l(&req
.n
, sizeof(req
), XFRMA_TMPL
,
309 (void *)tmpls_buf
, tmpls_len
);
312 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
315 if (req
.xpinfo
.sel
.family
== AF_UNSPEC
)
316 req
.xpinfo
.sel
.family
= AF_INET
;
318 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
326 static int xfrm_policy_filter_match(struct xfrm_userpolicy_info
*xpinfo
,
332 if ((xpinfo
->dir
^filter
.xpinfo
.dir
)&filter
.dir_mask
)
335 if ((ptype
^filter
.ptype
)&filter
.ptype_mask
)
338 if (filter
.sel_src_mask
) {
339 if (xfrm_addr_match(&xpinfo
->sel
.saddr
, &filter
.xpinfo
.sel
.saddr
,
340 filter
.sel_src_mask
))
344 if (filter
.sel_dst_mask
) {
345 if (xfrm_addr_match(&xpinfo
->sel
.daddr
, &filter
.xpinfo
.sel
.daddr
,
346 filter
.sel_dst_mask
))
350 if ((xpinfo
->sel
.ifindex
^filter
.xpinfo
.sel
.ifindex
)&filter
.sel_dev_mask
)
353 if ((xpinfo
->sel
.proto
^filter
.xpinfo
.sel
.proto
)&filter
.upspec_proto_mask
)
356 if (filter
.upspec_sport_mask
) {
357 if ((xpinfo
->sel
.sport
^filter
.xpinfo
.sel
.sport
)&filter
.upspec_sport_mask
)
361 if (filter
.upspec_dport_mask
) {
362 if ((xpinfo
->sel
.dport
^filter
.xpinfo
.sel
.dport
)&filter
.upspec_dport_mask
)
366 if ((xpinfo
->index
^filter
.xpinfo
.index
)&filter
.index_mask
)
369 if ((xpinfo
->action
^filter
.xpinfo
.action
)&filter
.action_mask
)
372 if ((xpinfo
->priority
^filter
.xpinfo
.priority
)&filter
.priority_mask
)
378 int xfrm_policy_print(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
381 struct rtattr
* tb
[XFRMA_MAX
+1];
383 struct xfrm_userpolicy_info
*xpinfo
= NULL
;
384 struct xfrm_user_polexpire
*xpexp
= NULL
;
385 struct xfrm_userpolicy_id
*xpid
= NULL
;
386 __u8 ptype
= XFRM_POLICY_TYPE_MAIN
;
387 FILE *fp
= (FILE*)arg
;
388 int len
= n
->nlmsg_len
;
390 if (n
->nlmsg_type
!= XFRM_MSG_NEWPOLICY
&&
391 n
->nlmsg_type
!= XFRM_MSG_DELPOLICY
&&
392 n
->nlmsg_type
!= XFRM_MSG_UPDPOLICY
&&
393 n
->nlmsg_type
!= XFRM_MSG_POLEXPIRE
) {
394 fprintf(stderr
, "Not a policy: %08x %08x %08x\n",
395 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
399 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
) {
400 xpid
= NLMSG_DATA(n
);
401 len
-= NLMSG_SPACE(sizeof(*xpid
));
402 } else if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
) {
403 xpexp
= NLMSG_DATA(n
);
404 xpinfo
= &xpexp
->pol
;
405 len
-= NLMSG_SPACE(sizeof(*xpexp
));
408 xpinfo
= NLMSG_DATA(n
);
409 len
-= NLMSG_SPACE(sizeof(*xpinfo
));
413 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
417 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
)
418 rta
= XFRMPID_RTA(xpid
);
419 else if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
)
420 rta
= XFRMPEXP_RTA(xpexp
);
422 rta
= XFRMP_RTA(xpinfo
);
424 parse_rtattr(tb
, XFRMA_MAX
, rta
, len
);
426 if (tb
[XFRMA_POLICY_TYPE
]) {
427 struct xfrm_userpolicy_type
*upt
;
429 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
)) {
430 fprintf(stderr
, "too short XFRMA_POLICY_TYPE len\n");
433 upt
= (struct xfrm_userpolicy_type
*)RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
437 if (xpinfo
&& !xfrm_policy_filter_match(xpinfo
, ptype
))
440 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
)
441 fprintf(fp
, "Deleted ");
442 else if (n
->nlmsg_type
== XFRM_MSG_UPDPOLICY
)
443 fprintf(fp
, "Updated ");
444 else if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
)
445 fprintf(fp
, "Expired ");
447 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
) {
448 //xfrm_policy_id_print();
449 if (!tb
[XFRMA_POLICY
]) {
450 fprintf(stderr
, "Buggy XFRM_MSG_DELPOLICY: no XFRMA_POLICY\n");
453 if (RTA_PAYLOAD(tb
[XFRMA_POLICY
]) < sizeof(*xpinfo
)) {
454 fprintf(stderr
, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
457 xpinfo
= (struct xfrm_userpolicy_info
*)RTA_DATA(tb
[XFRMA_POLICY
]);
460 xfrm_policy_info_print(xpinfo
, tb
, fp
, NULL
, NULL
);
462 if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
) {
464 fprintf(fp
, "hard %u", xpexp
->hard
);
465 fprintf(fp
, "%s", _SL_
);
475 static int xfrm_policy_get_or_delete(int argc
, char **argv
, int delete,
478 struct rtnl_handle rth
;
481 struct xfrm_userpolicy_id xpid
;
482 char buf
[RTA_BUF_SIZE
];
488 struct xfrm_userpolicy_type upt
;
490 memset(&req
, 0, sizeof(req
));
491 memset(&upt
, 0, sizeof(upt
));
493 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xpid
));
494 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
495 req
.n
.nlmsg_type
= delete ? XFRM_MSG_DELPOLICY
: XFRM_MSG_GETPOLICY
;
498 if (strcmp(*argv
, "dir") == 0) {
500 duparg("dir", *argv
);
504 xfrm_policy_dir_parse(&req
.xpid
.dir
, &argc
, &argv
);
506 } else if (strcmp(*argv
, "index") == 0) {
508 duparg("index", *argv
);
512 if (get_u32(&req
.xpid
.index
, *argv
, 0))
513 invarg("\"INDEX\" is invalid", *argv
);
515 } else if (strcmp(*argv
, "ptype") == 0) {
517 duparg("ptype", *argv
);
521 xfrm_policy_ptype_parse(&upt
.type
, &argc
, &argv
);
525 invarg("unknown", *argv
);
528 xfrm_selector_parse(&req
.xpid
.sel
, &argc
, &argv
);
529 if (preferred_family
== AF_UNSPEC
)
530 preferred_family
= req
.xpid
.sel
.family
;
538 fprintf(stderr
, "Not enough information: \"DIR\" is required.\n");
542 addattr_l(&req
.n
, sizeof(req
), XFRMA_POLICY_TYPE
,
543 (void *)&upt
, sizeof(upt
));
545 if (!selp
&& !indexp
) {
546 fprintf(stderr
, "Not enough information: either \"SELECTOR\" or \"INDEX\" is required.\n");
550 duparg2("SELECTOR", "INDEX");
552 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
555 if (req
.xpid
.sel
.family
== AF_UNSPEC
)
556 req
.xpid
.sel
.family
= AF_INET
;
558 if (rtnl_talk(&rth
, &req
.n
, 0, 0, res_nlbuf
, NULL
, NULL
) < 0)
566 static int xfrm_policy_delete(int argc
, char **argv
)
568 return xfrm_policy_get_or_delete(argc
, argv
, 1, NULL
);
571 static int xfrm_policy_get(int argc
, char **argv
)
573 char buf
[NLMSG_BUF_SIZE
];
574 struct nlmsghdr
*n
= (struct nlmsghdr
*)buf
;
576 memset(buf
, 0, sizeof(buf
));
578 xfrm_policy_get_or_delete(argc
, argv
, 0, n
);
580 if (xfrm_policy_print(NULL
, n
, (void*)stdout
) < 0) {
581 fprintf(stderr
, "An error :-)\n");
589 * With an existing policy of nlmsg, make new nlmsg for deleting the policy
590 * and store it to buffer.
592 static int xfrm_policy_keep(const struct sockaddr_nl
*who
,
596 struct xfrm_buffer
*xb
= (struct xfrm_buffer
*)arg
;
597 struct rtnl_handle
*rth
= xb
->rth
;
598 struct xfrm_userpolicy_info
*xpinfo
= NLMSG_DATA(n
);
599 int len
= n
->nlmsg_len
;
600 struct rtattr
*tb
[XFRMA_MAX
+1];
601 __u8 ptype
= XFRM_POLICY_TYPE_MAIN
;
602 struct nlmsghdr
*new_n
;
603 struct xfrm_userpolicy_id
*xpid
;
605 if (n
->nlmsg_type
!= XFRM_MSG_NEWPOLICY
) {
606 fprintf(stderr
, "Not a policy: %08x %08x %08x\n",
607 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
611 len
-= NLMSG_LENGTH(sizeof(*xpinfo
));
613 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
617 parse_rtattr(tb
, XFRMA_MAX
, XFRMP_RTA(xpinfo
), len
);
619 if (tb
[XFRMA_POLICY_TYPE
]) {
620 struct xfrm_userpolicy_type
*upt
;
622 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
)) {
623 fprintf(stderr
, "too short XFRMA_POLICY_TYPE len\n");
626 upt
= (struct xfrm_userpolicy_type
*)RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
630 if (!xfrm_policy_filter_match(xpinfo
, ptype
))
633 if (xb
->offset
> xb
->size
) {
634 fprintf(stderr
, "Policy buffer overflow\n");
638 new_n
= (struct nlmsghdr
*)(xb
->buf
+ xb
->offset
);
639 new_n
->nlmsg_len
= NLMSG_LENGTH(sizeof(*xpid
));
640 new_n
->nlmsg_flags
= NLM_F_REQUEST
;
641 new_n
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
642 new_n
->nlmsg_seq
= ++rth
->seq
;
644 xpid
= NLMSG_DATA(new_n
);
645 memcpy(&xpid
->sel
, &xpinfo
->sel
, sizeof(xpid
->sel
));
646 xpid
->dir
= xpinfo
->dir
;
647 xpid
->index
= xpinfo
->index
;
649 xb
->offset
+= new_n
->nlmsg_len
;
655 static int xfrm_policy_list_or_deleteall(int argc
, char **argv
, int deleteall
)
658 struct rtnl_handle rth
;
662 filter
.xpinfo
.sel
.family
= preferred_family
;
665 if (strcmp(*argv
, "dir") == 0) {
667 xfrm_policy_dir_parse(&filter
.xpinfo
.dir
, &argc
, &argv
);
669 filter
.dir_mask
= XFRM_FILTER_MASK_FULL
;
671 } else if (strcmp(*argv
, "index") == 0) {
673 if (get_u32(&filter
.xpinfo
.index
, *argv
, 0))
674 invarg("\"INDEX\" is invalid", *argv
);
676 filter
.index_mask
= XFRM_FILTER_MASK_FULL
;
678 } else if (strcmp(*argv
, "ptype") == 0) {
680 xfrm_policy_ptype_parse(&filter
.ptype
, &argc
, &argv
);
682 filter
.ptype_mask
= XFRM_FILTER_MASK_FULL
;
684 } else if (strcmp(*argv
, "action") == 0) {
686 if (strcmp(*argv
, "allow") == 0)
687 filter
.xpinfo
.action
= XFRM_POLICY_ALLOW
;
688 else if (strcmp(*argv
, "block") == 0)
689 filter
.xpinfo
.action
= XFRM_POLICY_BLOCK
;
691 invarg("\"ACTION\" is invalid\n", *argv
);
693 filter
.action_mask
= XFRM_FILTER_MASK_FULL
;
695 } else if (strcmp(*argv
, "priority") == 0) {
697 if (get_u32(&filter
.xpinfo
.priority
, *argv
, 0))
698 invarg("\"PRIORITY\" is invalid", *argv
);
700 filter
.priority_mask
= XFRM_FILTER_MASK_FULL
;
704 invarg("unknown", *argv
);
707 xfrm_selector_parse(&filter
.xpinfo
.sel
, &argc
, &argv
);
708 if (preferred_family
== AF_UNSPEC
)
709 preferred_family
= filter
.xpinfo
.sel
.family
;
716 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
720 struct xfrm_buffer xb
;
721 char buf
[NLMSG_DELETEALL_BUF_SIZE
];
725 xb
.size
= sizeof(buf
);
733 fprintf(stderr
, "Delete-all round = %d\n", i
);
735 if (rtnl_wilddump_request(&rth
, preferred_family
, XFRM_MSG_GETPOLICY
) < 0) {
736 perror("Cannot send dump request");
740 if (rtnl_dump_filter(&rth
, xfrm_policy_keep
, &xb
, NULL
, NULL
) < 0) {
741 fprintf(stderr
, "Delete-all terminated\n");
744 if (xb
.nlmsg_count
== 0) {
746 fprintf(stderr
, "Delete-all completed\n");
750 if (rtnl_send(&rth
, xb
.buf
, xb
.offset
) < 0) {
751 perror("Failed to send delete-all request\n");
755 fprintf(stderr
, "Delete-all nlmsg count = %d\n", xb
.nlmsg_count
);
761 if (rtnl_wilddump_request(&rth
, preferred_family
, XFRM_MSG_GETPOLICY
) < 0) {
762 perror("Cannot send dump request");
766 if (rtnl_dump_filter(&rth
, xfrm_policy_print
, stdout
, NULL
, NULL
) < 0) {
767 fprintf(stderr
, "Dump terminated\n");
777 static int xfrm_policy_flush(int argc
, char **argv
)
779 struct rtnl_handle rth
;
782 char buf
[RTA_BUF_SIZE
];
785 struct xfrm_userpolicy_type upt
;
787 memset(&req
, 0, sizeof(req
));
788 memset(&upt
, 0, sizeof(upt
));
790 req
.n
.nlmsg_len
= NLMSG_LENGTH(0); /* nlmsg data is nothing */
791 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
792 req
.n
.nlmsg_type
= XFRM_MSG_FLUSHPOLICY
;
795 if (strcmp(*argv
, "ptype") == 0) {
797 duparg("ptype", *argv
);
801 xfrm_policy_ptype_parse(&upt
.type
, &argc
, &argv
);
803 filter
.dir_mask
= XFRM_FILTER_MASK_FULL
;
805 invarg("unknown", *argv
);
811 addattr_l(&req
.n
, sizeof(req
), XFRMA_POLICY_TYPE
,
812 (void *)&upt
, sizeof(upt
));
815 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
819 fprintf(stderr
, "Flush policy\n");
821 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
829 int do_xfrm_policy(int argc
, char **argv
)
832 return xfrm_policy_list_or_deleteall(0, NULL
, 0);
834 if (matches(*argv
, "add") == 0)
835 return xfrm_policy_modify(XFRM_MSG_NEWPOLICY
, 0,
837 if (matches(*argv
, "update") == 0)
838 return xfrm_policy_modify(XFRM_MSG_UPDPOLICY
, 0,
840 if (matches(*argv
, "delete") == 0)
841 return xfrm_policy_delete(argc
-1, argv
+1);
842 if (matches(*argv
, "deleteall") == 0 || matches(*argv
, "delall") == 0)
843 return xfrm_policy_list_or_deleteall(argc
-1, argv
+1, 1);
844 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
845 || matches(*argv
, "lst") == 0)
846 return xfrm_policy_list_or_deleteall(argc
-1, argv
+1, 0);
847 if (matches(*argv
, "get") == 0)
848 return xfrm_policy_get(argc
-1, argv
+1);
849 if (matches(*argv
, "flush") == 0)
850 return xfrm_policy_flush(argc
-1, argv
+1);
851 if (matches(*argv
, "help") == 0)
853 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv
);