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, see <http://www.gnu.org/licenses>.
24 * Masahide NAKAMURA @USAGI
31 #include <linux/netlink.h>
34 #include "ip_common.h"
36 /* #define NLMSG_DELETEALL_BUF_SIZE (4096-512) */
37 #define NLMSG_DELETEALL_BUF_SIZE 8192
40 * Receiving buffer defines:
42 * data = struct xfrm_userpolicy_info
44 * data = struct xfrm_user_tmpl[]
46 #define NLMSG_BUF_SIZE 4096
47 #define RTA_BUF_SIZE 2048
48 #define XFRM_TMPLS_BUF_SIZE 1024
49 #define CTX_BUF_SIZE 256
51 static void usage(void) __attribute__((noreturn
));
53 static void usage(void)
55 fprintf(stderr
, "Usage: ip xfrm policy { add | update } SELECTOR dir DIR [ ctx CTX ]\n");
56 fprintf(stderr
, " [ mark MARK [ mask MASK ] ] [ index INDEX ] [ ptype PTYPE ]\n");
57 fprintf(stderr
, " [ action ACTION ] [ priority PRIORITY ] [ flag FLAG-LIST ]\n");
58 fprintf(stderr
, " [ LIMIT-LIST ] [ TMPL-LIST ]\n");
59 fprintf(stderr
, "Usage: ip xfrm policy { delete | get } { SELECTOR | index INDEX } dir DIR\n");
60 fprintf(stderr
, " [ ctx CTX ] [ mark MARK [ mask MASK ] ] [ ptype PTYPE ]\n");
61 fprintf(stderr
, "Usage: ip xfrm policy { deleteall | list } [ nosock ] [ SELECTOR ] [ dir DIR ]\n");
62 fprintf(stderr
, " [ index INDEX ] [ ptype PTYPE ] [ action ACTION ] [ priority PRIORITY ]\n");
63 fprintf(stderr
, " [ flag FLAG-LIST ]\n");
64 fprintf(stderr
, "Usage: ip xfrm policy flush [ ptype PTYPE ]\n");
65 fprintf(stderr
, "Usage: ip xfrm policy count\n");
66 fprintf(stderr
, "Usage: ip xfrm policy set [ hthresh4 LBITS RBITS ] [ hthresh6 LBITS RBITS ]\n");
67 fprintf(stderr
, "SELECTOR := [ src ADDR[/PLEN] ] [ dst ADDR[/PLEN] ] [ dev DEV ] [ UPSPEC ]\n");
68 fprintf(stderr
, "UPSPEC := proto { { ");
69 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_TCP
));
70 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_UDP
));
71 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_SCTP
));
72 fprintf(stderr
, "%s", strxf_proto(IPPROTO_DCCP
));
73 fprintf(stderr
, " } [ sport PORT ] [ dport PORT ] |\n");
74 fprintf(stderr
, " { ");
75 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_ICMP
));
76 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_ICMPV6
));
77 fprintf(stderr
, "%s", strxf_proto(IPPROTO_MH
));
78 fprintf(stderr
, " } [ type NUMBER ] [ code NUMBER ] |\n");
79 fprintf(stderr
, " %s", strxf_proto(IPPROTO_GRE
));
80 fprintf(stderr
, " [ key { DOTTED-QUAD | NUMBER } ] | PROTO }\n");
81 fprintf(stderr
, "DIR := in | out | fwd\n");
82 fprintf(stderr
, "PTYPE := main | sub\n");
83 fprintf(stderr
, "ACTION := allow | block\n");
84 fprintf(stderr
, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
85 fprintf(stderr
, "FLAG := localok | icmp\n");
86 fprintf(stderr
, "LIMIT-LIST := [ LIMIT-LIST ] limit LIMIT\n");
87 fprintf(stderr
, "LIMIT := { time-soft | time-hard | time-use-soft | time-use-hard } SECONDS |\n");
88 fprintf(stderr
, " { byte-soft | byte-hard } SIZE | { packet-soft | packet-hard } COUNT\n");
89 fprintf(stderr
, "TMPL-LIST := [ TMPL-LIST ] tmpl TMPL\n");
90 fprintf(stderr
, "TMPL := ID [ mode MODE ] [ reqid REQID ] [ level LEVEL ]\n");
91 fprintf(stderr
, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ]\n");
92 fprintf(stderr
, "XFRM-PROTO := ");
93 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_ESP
));
94 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_AH
));
95 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_COMP
));
96 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING
));
97 fprintf(stderr
, "%s\n", strxf_xfrmproto(IPPROTO_DSTOPTS
));
98 fprintf(stderr
, "MODE := transport | tunnel | beet | ro | in_trigger\n");
99 fprintf(stderr
, "LEVEL := required | use\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 value 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 value 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 value is invalid", *argv
);
156 if (strcmp(*argv
, "localok") == 0)
157 *flags
|= XFRM_POLICY_LOCALOK
;
158 else if (strcmp(*argv
, "icmp") == 0)
159 *flags
|= XFRM_POLICY_ICMP
;
161 PREV_ARG(); /* back track */
177 static int xfrm_tmpl_parse(struct xfrm_user_tmpl
*tmpl
,
178 int *argcp
, char ***argvp
)
181 char **argv
= *argvp
;
185 if (strcmp(*argv
, "mode") == 0) {
187 xfrm_mode_parse(&tmpl
->mode
, &argc
, &argv
);
188 } else if (strcmp(*argv
, "reqid") == 0) {
190 xfrm_reqid_parse(&tmpl
->reqid
, &argc
, &argv
);
191 } else if (strcmp(*argv
, "level") == 0) {
194 if (strcmp(*argv
, "required") == 0)
196 else if (strcmp(*argv
, "use") == 0)
199 invarg("LEVEL value is invalid\n", *argv
);
203 PREV_ARG(); /* back track */
207 preferred_family
= AF_UNSPEC
;
208 xfrm_id_parse(&tmpl
->saddr
, &tmpl
->id
, &tmpl
->family
,
210 preferred_family
= tmpl
->family
;
227 int xfrm_sctx_parse(char *ctxstr
, char *s
,
228 struct xfrm_user_sec_ctx
*sctx
)
232 slen
= strlen(s
) + 1;
234 sctx
->exttype
= XFRMA_SEC_CTX
;
237 sctx
->ctx_len
= slen
;
238 sctx
->len
= sizeof(struct xfrm_user_sec_ctx
) + slen
;
239 memcpy(ctxstr
, s
, slen
);
244 static int xfrm_policy_modify(int cmd
, unsigned int flags
, int argc
, char **argv
)
246 struct rtnl_handle rth
;
249 struct xfrm_userpolicy_info xpinfo
;
250 char buf
[RTA_BUF_SIZE
];
252 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xpinfo
)),
253 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
255 .xpinfo
.sel
.family
= preferred_family
,
256 .xpinfo
.lft
.soft_byte_limit
= XFRM_INF
,
257 .xpinfo
.lft
.hard_byte_limit
= XFRM_INF
,
258 .xpinfo
.lft
.soft_packet_limit
= XFRM_INF
,
259 .xpinfo
.lft
.hard_packet_limit
= XFRM_INF
,
265 struct xfrm_userpolicy_type upt
= {};
266 char tmpls_buf
[XFRM_TMPLS_BUF_SIZE
] = {};
268 struct xfrm_mark mark
= {0, 0};
270 struct xfrm_user_sec_ctx sctx
;
271 char str
[CTX_BUF_SIZE
];
275 if (strcmp(*argv
, "dir") == 0) {
277 duparg("dir", *argv
);
281 xfrm_policy_dir_parse(&req
.xpinfo
.dir
, &argc
, &argv
);
282 } else if (strcmp(*argv
, "ctx") == 0) {
286 duparg("ctx", *argv
);
290 xfrm_sctx_parse((char *)&ctx
.str
, context
, &ctx
.sctx
);
291 } else if (strcmp(*argv
, "mark") == 0) {
292 xfrm_parse_mark(&mark
, &argc
, &argv
);
293 } else if (strcmp(*argv
, "index") == 0) {
295 if (get_u32(&req
.xpinfo
.index
, *argv
, 0))
296 invarg("INDEX value is invalid", *argv
);
297 } else if (strcmp(*argv
, "ptype") == 0) {
299 duparg("ptype", *argv
);
303 xfrm_policy_ptype_parse(&upt
.type
, &argc
, &argv
);
304 } else if (strcmp(*argv
, "action") == 0) {
306 if (strcmp(*argv
, "allow") == 0)
307 req
.xpinfo
.action
= XFRM_POLICY_ALLOW
;
308 else if (strcmp(*argv
, "block") == 0)
309 req
.xpinfo
.action
= XFRM_POLICY_BLOCK
;
311 invarg("ACTION value is invalid\n", *argv
);
312 } else if (strcmp(*argv
, "priority") == 0) {
314 if (get_u32(&req
.xpinfo
.priority
, *argv
, 0))
315 invarg("PRIORITY value is invalid", *argv
);
316 } else if (strcmp(*argv
, "flag") == 0) {
318 xfrm_policy_flag_parse(&req
.xpinfo
.flags
, &argc
,
320 } else if (strcmp(*argv
, "limit") == 0) {
322 xfrm_lifetime_cfg_parse(&req
.xpinfo
.lft
, &argc
, &argv
);
323 } else if (strcmp(*argv
, "tmpl") == 0) {
324 struct xfrm_user_tmpl
*tmpl
;
326 if (tmpls_len
+ sizeof(*tmpl
) > sizeof(tmpls_buf
)) {
327 fprintf(stderr
, "Too many tmpls: buffer overflow\n");
330 tmpl
= (struct xfrm_user_tmpl
*)((char *)tmpls_buf
+ tmpls_len
);
332 tmpl
->family
= preferred_family
;
333 tmpl
->aalgos
= (~(__u32
)0);
334 tmpl
->ealgos
= (~(__u32
)0);
335 tmpl
->calgos
= (~(__u32
)0);
338 xfrm_tmpl_parse(tmpl
, &argc
, &argv
);
340 tmpls_len
+= sizeof(*tmpl
);
343 duparg("unknown", *argv
);
346 xfrm_selector_parse(&req
.xpinfo
.sel
, &argc
, &argv
);
347 if (preferred_family
== AF_UNSPEC
)
348 preferred_family
= req
.xpinfo
.sel
.family
;
355 fprintf(stderr
, "Not enough information: DIR is required.\n");
360 addattr_l(&req
.n
, sizeof(req
), XFRMA_POLICY_TYPE
,
361 (void *)&upt
, sizeof(upt
));
365 addattr_l(&req
.n
, sizeof(req
), XFRMA_TMPL
,
366 (void *)tmpls_buf
, tmpls_len
);
370 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
371 (void *)&mark
, sizeof(mark
));
373 fprintf(stderr
, "%s: XFRMA_MARK failed\n", __func__
);
379 addattr_l(&req
.n
, sizeof(req
), XFRMA_SEC_CTX
,
380 (void *)&ctx
, ctx
.sctx
.len
);
383 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
386 if (req
.xpinfo
.sel
.family
== AF_UNSPEC
)
387 req
.xpinfo
.sel
.family
= AF_INET
;
389 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
397 static int xfrm_policy_filter_match(struct xfrm_userpolicy_info
*xpinfo
,
403 if ((xpinfo
->dir
^filter
.xpinfo
.dir
)&filter
.dir_mask
)
406 if (filter
.filter_socket
&& (xpinfo
->dir
>= XFRM_POLICY_MAX
))
409 if ((ptype
^filter
.ptype
)&filter
.ptype_mask
)
412 if (filter
.sel_src_mask
) {
413 if (xfrm_addr_match(&xpinfo
->sel
.saddr
, &filter
.xpinfo
.sel
.saddr
,
414 filter
.sel_src_mask
))
418 if (filter
.sel_dst_mask
) {
419 if (xfrm_addr_match(&xpinfo
->sel
.daddr
, &filter
.xpinfo
.sel
.daddr
,
420 filter
.sel_dst_mask
))
424 if ((xpinfo
->sel
.ifindex
^filter
.xpinfo
.sel
.ifindex
)&filter
.sel_dev_mask
)
427 if ((xpinfo
->sel
.proto
^filter
.xpinfo
.sel
.proto
)&filter
.upspec_proto_mask
)
430 if (filter
.upspec_sport_mask
) {
431 if ((xpinfo
->sel
.sport
^filter
.xpinfo
.sel
.sport
)&filter
.upspec_sport_mask
)
435 if (filter
.upspec_dport_mask
) {
436 if ((xpinfo
->sel
.dport
^filter
.xpinfo
.sel
.dport
)&filter
.upspec_dport_mask
)
440 if ((xpinfo
->index
^filter
.xpinfo
.index
)&filter
.index_mask
)
443 if ((xpinfo
->action
^filter
.xpinfo
.action
)&filter
.action_mask
)
446 if ((xpinfo
->priority
^filter
.xpinfo
.priority
)&filter
.priority_mask
)
449 if (filter
.policy_flags_mask
)
450 if ((xpinfo
->flags
& filter
.xpinfo
.flags
) == 0)
456 int xfrm_policy_print(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
459 struct rtattr
*tb
[XFRMA_MAX
+1];
461 struct xfrm_userpolicy_info
*xpinfo
= NULL
;
462 struct xfrm_user_polexpire
*xpexp
= NULL
;
463 struct xfrm_userpolicy_id
*xpid
= NULL
;
464 __u8 ptype
= XFRM_POLICY_TYPE_MAIN
;
465 FILE *fp
= (FILE *)arg
;
466 int len
= n
->nlmsg_len
;
468 if (n
->nlmsg_type
!= XFRM_MSG_NEWPOLICY
&&
469 n
->nlmsg_type
!= XFRM_MSG_DELPOLICY
&&
470 n
->nlmsg_type
!= XFRM_MSG_UPDPOLICY
&&
471 n
->nlmsg_type
!= XFRM_MSG_POLEXPIRE
) {
472 fprintf(stderr
, "Not a policy: %08x %08x %08x\n",
473 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
477 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
) {
478 xpid
= NLMSG_DATA(n
);
479 len
-= NLMSG_SPACE(sizeof(*xpid
));
480 } else if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
) {
481 xpexp
= NLMSG_DATA(n
);
482 xpinfo
= &xpexp
->pol
;
483 len
-= NLMSG_SPACE(sizeof(*xpexp
));
486 xpinfo
= NLMSG_DATA(n
);
487 len
-= NLMSG_SPACE(sizeof(*xpinfo
));
491 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
495 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
)
496 rta
= XFRMPID_RTA(xpid
);
497 else if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
)
498 rta
= XFRMPEXP_RTA(xpexp
);
500 rta
= XFRMP_RTA(xpinfo
);
502 parse_rtattr(tb
, XFRMA_MAX
, rta
, len
);
504 if (tb
[XFRMA_POLICY_TYPE
]) {
505 struct xfrm_userpolicy_type
*upt
;
507 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
)) {
508 fprintf(stderr
, "too short XFRMA_POLICY_TYPE len\n");
511 upt
= RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
515 if (xpinfo
&& !xfrm_policy_filter_match(xpinfo
, ptype
))
518 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
)
519 fprintf(fp
, "Deleted ");
520 else if (n
->nlmsg_type
== XFRM_MSG_UPDPOLICY
)
521 fprintf(fp
, "Updated ");
522 else if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
)
523 fprintf(fp
, "Expired ");
525 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
) {
526 /* xfrm_policy_id_print(); */
527 if (!tb
[XFRMA_POLICY
]) {
528 fprintf(stderr
, "Buggy XFRM_MSG_DELPOLICY: no XFRMA_POLICY\n");
531 if (RTA_PAYLOAD(tb
[XFRMA_POLICY
]) < sizeof(*xpinfo
)) {
532 fprintf(stderr
, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
535 xpinfo
= RTA_DATA(tb
[XFRMA_POLICY
]);
538 xfrm_policy_info_print(xpinfo
, tb
, fp
, NULL
, NULL
);
540 if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
) {
542 fprintf(fp
, "hard %u", xpexp
->hard
);
543 fprintf(fp
, "%s", _SL_
);
553 static int xfrm_policy_get_or_delete(int argc
, char **argv
, int delete,
554 struct nlmsghdr
**answer
)
556 struct rtnl_handle rth
;
559 struct xfrm_userpolicy_id xpid
;
560 char buf
[RTA_BUF_SIZE
];
562 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xpid
)),
563 .n
.nlmsg_flags
= NLM_F_REQUEST
,
564 .n
.nlmsg_type
= delete ? XFRM_MSG_DELPOLICY
565 : XFRM_MSG_GETPOLICY
,
572 struct xfrm_userpolicy_type upt
= {};
573 struct xfrm_mark mark
= {0, 0};
575 struct xfrm_user_sec_ctx sctx
;
576 char str
[CTX_BUF_SIZE
];
580 if (strcmp(*argv
, "dir") == 0) {
582 duparg("dir", *argv
);
586 xfrm_policy_dir_parse(&req
.xpid
.dir
, &argc
, &argv
);
588 } else if (strcmp(*argv
, "ctx") == 0) {
592 duparg("ctx", *argv
);
596 xfrm_sctx_parse((char *)&ctx
.str
, context
, &ctx
.sctx
);
597 } else if (strcmp(*argv
, "mark") == 0) {
598 xfrm_parse_mark(&mark
, &argc
, &argv
);
599 } else if (strcmp(*argv
, "index") == 0) {
601 duparg("index", *argv
);
605 if (get_u32(&req
.xpid
.index
, *argv
, 0))
606 invarg("INDEX value is invalid", *argv
);
608 } else if (strcmp(*argv
, "ptype") == 0) {
610 duparg("ptype", *argv
);
614 xfrm_policy_ptype_parse(&upt
.type
, &argc
, &argv
);
618 invarg("unknown", *argv
);
621 xfrm_selector_parse(&req
.xpid
.sel
, &argc
, &argv
);
622 if (preferred_family
== AF_UNSPEC
)
623 preferred_family
= req
.xpid
.sel
.family
;
631 fprintf(stderr
, "Not enough information: DIR is required.\n");
635 addattr_l(&req
.n
, sizeof(req
), XFRMA_POLICY_TYPE
,
636 (void *)&upt
, sizeof(upt
));
638 if (!selp
&& !indexp
) {
639 fprintf(stderr
, "Not enough information: either SELECTOR or INDEX is required.\n");
643 duparg2("SELECTOR", "INDEX");
645 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
648 if (req
.xpid
.sel
.family
== AF_UNSPEC
)
649 req
.xpid
.sel
.family
= AF_INET
;
651 if (mark
.m
& mark
.v
) {
652 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
653 (void *)&mark
, sizeof(mark
));
655 fprintf(stderr
, "%s: XFRMA_MARK failed\n", __func__
);
661 addattr_l(&req
.n
, sizeof(req
), XFRMA_SEC_CTX
,
662 (void *)&ctx
, ctx
.sctx
.len
);
665 if (rtnl_talk(&rth
, &req
.n
, answer
) < 0)
673 static int xfrm_policy_delete(int argc
, char **argv
)
675 return xfrm_policy_get_or_delete(argc
, argv
, 1, NULL
);
678 static int xfrm_policy_get(int argc
, char **argv
)
680 struct nlmsghdr
*n
= NULL
;
682 xfrm_policy_get_or_delete(argc
, argv
, 0, &n
);
684 if (xfrm_policy_print(NULL
, n
, (void *)stdout
) < 0) {
685 fprintf(stderr
, "An error :-)\n");
694 * With an existing policy of nlmsg, make new nlmsg for deleting the policy
695 * and store it to buffer.
697 static int xfrm_policy_keep(const struct sockaddr_nl
*who
,
701 struct xfrm_buffer
*xb
= (struct xfrm_buffer
*)arg
;
702 struct rtnl_handle
*rth
= xb
->rth
;
703 struct xfrm_userpolicy_info
*xpinfo
= NLMSG_DATA(n
);
704 int len
= n
->nlmsg_len
;
705 struct rtattr
*tb
[XFRMA_MAX
+1];
706 __u8 ptype
= XFRM_POLICY_TYPE_MAIN
;
707 struct nlmsghdr
*new_n
;
708 struct xfrm_userpolicy_id
*xpid
;
710 if (n
->nlmsg_type
!= XFRM_MSG_NEWPOLICY
) {
711 fprintf(stderr
, "Not a policy: %08x %08x %08x\n",
712 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
716 len
-= NLMSG_LENGTH(sizeof(*xpinfo
));
718 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
722 parse_rtattr(tb
, XFRMA_MAX
, XFRMP_RTA(xpinfo
), len
);
724 if (tb
[XFRMA_POLICY_TYPE
]) {
725 struct xfrm_userpolicy_type
*upt
;
727 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
)) {
728 fprintf(stderr
, "too short XFRMA_POLICY_TYPE len\n");
731 upt
= RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
735 if (!xfrm_policy_filter_match(xpinfo
, ptype
))
738 /* can't delete socket policies */
739 if (xpinfo
->dir
>= XFRM_POLICY_MAX
)
742 if (xb
->offset
+ NLMSG_LENGTH(sizeof(*xpid
)) > xb
->size
)
745 new_n
= (struct nlmsghdr
*)(xb
->buf
+ xb
->offset
);
746 new_n
->nlmsg_len
= NLMSG_LENGTH(sizeof(*xpid
));
747 new_n
->nlmsg_flags
= NLM_F_REQUEST
;
748 new_n
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
749 new_n
->nlmsg_seq
= ++rth
->seq
;
751 xpid
= NLMSG_DATA(new_n
);
752 memcpy(&xpid
->sel
, &xpinfo
->sel
, sizeof(xpid
->sel
));
753 xpid
->dir
= xpinfo
->dir
;
754 xpid
->index
= xpinfo
->index
;
756 if (tb
[XFRMA_MARK
]) {
757 int r
= addattr_l(new_n
, xb
->size
, XFRMA_MARK
,
758 (void *)RTA_DATA(tb
[XFRMA_MARK
]), tb
[XFRMA_MARK
]->rta_len
);
760 fprintf(stderr
, "%s: XFRMA_MARK failed\n", __func__
);
765 xb
->offset
+= new_n
->nlmsg_len
;
771 static int xfrm_policy_list_or_deleteall(int argc
, char **argv
, int deleteall
)
774 struct rtnl_handle rth
;
778 filter
.xpinfo
.sel
.family
= preferred_family
;
781 if (strcmp(*argv
, "dir") == 0) {
783 xfrm_policy_dir_parse(&filter
.xpinfo
.dir
, &argc
, &argv
);
785 filter
.dir_mask
= XFRM_FILTER_MASK_FULL
;
787 } else if (strcmp(*argv
, "index") == 0) {
789 if (get_u32(&filter
.xpinfo
.index
, *argv
, 0))
790 invarg("INDEX value is invalid", *argv
);
792 filter
.index_mask
= XFRM_FILTER_MASK_FULL
;
794 } else if (strcmp(*argv
, "ptype") == 0) {
796 xfrm_policy_ptype_parse(&filter
.ptype
, &argc
, &argv
);
798 filter
.ptype_mask
= XFRM_FILTER_MASK_FULL
;
800 } else if (strcmp(*argv
, "action") == 0) {
802 if (strcmp(*argv
, "allow") == 0)
803 filter
.xpinfo
.action
= XFRM_POLICY_ALLOW
;
804 else if (strcmp(*argv
, "block") == 0)
805 filter
.xpinfo
.action
= XFRM_POLICY_BLOCK
;
807 invarg("ACTION value is invalid\n", *argv
);
809 filter
.action_mask
= XFRM_FILTER_MASK_FULL
;
811 } else if (strcmp(*argv
, "priority") == 0) {
813 if (get_u32(&filter
.xpinfo
.priority
, *argv
, 0))
814 invarg("PRIORITY value is invalid", *argv
);
816 filter
.priority_mask
= XFRM_FILTER_MASK_FULL
;
818 } else if (strcmp(*argv
, "flag") == 0) {
820 xfrm_policy_flag_parse(&filter
.xpinfo
.flags
, &argc
,
823 filter
.policy_flags_mask
= XFRM_FILTER_MASK_FULL
;
825 } else if (strcmp(*argv
, "nosock") == 0) {
826 /* filter all socket-based policies */
827 filter
.filter_socket
= 1;
830 invarg("unknown", *argv
);
833 xfrm_selector_parse(&filter
.xpinfo
.sel
, &argc
, &argv
);
834 if (preferred_family
== AF_UNSPEC
)
835 preferred_family
= filter
.xpinfo
.sel
.family
;
842 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
846 struct xfrm_buffer xb
;
847 char buf
[NLMSG_DELETEALL_BUF_SIZE
];
851 xb
.size
= sizeof(buf
);
857 char buf
[NLMSG_BUF_SIZE
];
859 .n
.nlmsg_len
= NLMSG_HDRLEN
,
860 .n
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
861 .n
.nlmsg_type
= XFRM_MSG_GETPOLICY
,
862 .n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
,
869 fprintf(stderr
, "Delete-all round = %d\n", i
);
871 if (rtnl_send(&rth
, (void *)&req
, req
.n
.nlmsg_len
) < 0) {
872 perror("Cannot send dump request");
876 if (rtnl_dump_filter(&rth
, xfrm_policy_keep
, &xb
) < 0) {
877 fprintf(stderr
, "Delete-all terminated\n");
880 if (xb
.nlmsg_count
== 0) {
882 fprintf(stderr
, "Delete-all completed\n");
886 if (rtnl_send_check(&rth
, xb
.buf
, xb
.offset
) < 0) {
887 perror("Failed to send delete-all request");
891 fprintf(stderr
, "Delete-all nlmsg count = %d\n", xb
.nlmsg_count
);
899 char buf
[NLMSG_BUF_SIZE
];
901 .n
.nlmsg_len
= NLMSG_HDRLEN
,
902 .n
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
903 .n
.nlmsg_type
= XFRM_MSG_GETPOLICY
,
904 .n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
,
907 if (rtnl_send(&rth
, (void *)&req
, req
.n
.nlmsg_len
) < 0) {
908 perror("Cannot send dump request");
912 if (rtnl_dump_filter(&rth
, xfrm_policy_print
, stdout
) < 0) {
913 fprintf(stderr
, "Dump terminated\n");
923 static int print_spdinfo(struct nlmsghdr
*n
, void *arg
)
925 FILE *fp
= (FILE *)arg
;
926 __u32
*f
= NLMSG_DATA(n
);
927 struct rtattr
*tb
[XFRMA_SPD_MAX
+1];
930 int len
= n
->nlmsg_len
;
932 len
-= NLMSG_LENGTH(sizeof(__u32
));
934 fprintf(stderr
, "SPDinfo: Wrong len %d\n", len
);
938 rta
= XFRMSAPD_RTA(f
);
939 parse_rtattr(tb
, XFRMA_SPD_MAX
, rta
, len
);
941 fprintf(fp
, "\t SPD");
942 if (tb
[XFRMA_SPD_INFO
]) {
943 struct xfrmu_spdinfo
*si
;
945 if (RTA_PAYLOAD(tb
[XFRMA_SPD_INFO
]) < sizeof(*si
)) {
946 fprintf(stderr
, "SPDinfo: Wrong len %d\n", len
);
949 si
= RTA_DATA(tb
[XFRMA_SPD_INFO
]);
950 fprintf(fp
, " IN %d", si
->incnt
);
951 fprintf(fp
, " OUT %d", si
->outcnt
);
952 fprintf(fp
, " FWD %d", si
->fwdcnt
);
955 fprintf(fp
, " (Sock:");
956 fprintf(fp
, " IN %d", si
->inscnt
);
957 fprintf(fp
, " OUT %d", si
->outscnt
);
958 fprintf(fp
, " FWD %d", si
->fwdscnt
);
962 fprintf(fp
, "%s", _SL_
);
964 if (show_stats
> 1) {
965 struct xfrmu_spdhinfo
*sh
;
967 if (tb
[XFRMA_SPD_HINFO
]) {
968 if (RTA_PAYLOAD(tb
[XFRMA_SPD_HINFO
]) < sizeof(*sh
)) {
969 fprintf(stderr
, "SPDinfo: Wrong len %d\n", len
);
972 sh
= RTA_DATA(tb
[XFRMA_SPD_HINFO
]);
973 fprintf(fp
, "\t SPD buckets:");
974 fprintf(fp
, " count %d", sh
->spdhcnt
);
975 fprintf(fp
, " Max %d", sh
->spdhmcnt
);
976 fprintf(fp
, "%s", _SL_
);
978 if (tb
[XFRMA_SPD_IPV4_HTHRESH
]) {
979 struct xfrmu_spdhthresh
*th
;
981 if (RTA_PAYLOAD(tb
[XFRMA_SPD_IPV4_HTHRESH
]) < sizeof(*th
)) {
982 fprintf(stderr
, "SPDinfo: Wrong len %d\n", len
);
985 th
= RTA_DATA(tb
[XFRMA_SPD_IPV4_HTHRESH
]);
986 fprintf(fp
, "\t SPD IPv4 thresholds:");
987 fprintf(fp
, " local %d", th
->lbits
);
988 fprintf(fp
, " remote %d", th
->rbits
);
989 fprintf(fp
, "%s", _SL_
);
992 if (tb
[XFRMA_SPD_IPV6_HTHRESH
]) {
993 struct xfrmu_spdhthresh
*th
;
995 if (RTA_PAYLOAD(tb
[XFRMA_SPD_IPV6_HTHRESH
]) < sizeof(*th
)) {
996 fprintf(stderr
, "SPDinfo: Wrong len %d\n", len
);
999 th
= RTA_DATA(tb
[XFRMA_SPD_IPV6_HTHRESH
]);
1000 fprintf(fp
, "\t SPD IPv6 thresholds:");
1001 fprintf(fp
, " local %d", th
->lbits
);
1002 fprintf(fp
, " remote %d", th
->rbits
);
1003 fprintf(fp
, "%s", _SL_
);
1013 static int xfrm_spd_setinfo(int argc
, char **argv
)
1015 struct rtnl_handle rth
;
1019 char buf
[RTA_BUF_SIZE
];
1021 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(__u32
)),
1022 .n
.nlmsg_flags
= NLM_F_REQUEST
,
1023 .n
.nlmsg_type
= XFRM_MSG_NEWSPDINFO
,
1024 .flags
= 0XFFFFFFFF,
1031 if (strcmp(*argv
, "hthresh4") == 0) {
1032 struct xfrmu_spdhthresh thr
;
1035 duparg("hthresh4", *argv
);
1038 if (get_u8(&thr
.lbits
, *argv
, 0) || thr
.lbits
> 32)
1039 invarg("hthresh4 LBITS value is invalid", *argv
);
1041 if (get_u8(&thr
.rbits
, *argv
, 0) || thr
.rbits
> 32)
1042 invarg("hthresh4 RBITS value is invalid", *argv
);
1044 addattr_l(&req
.n
, sizeof(req
), XFRMA_SPD_IPV4_HTHRESH
,
1045 (void *)&thr
, sizeof(thr
));
1046 } else if (strcmp(*argv
, "hthresh6") == 0) {
1047 struct xfrmu_spdhthresh thr
;
1050 duparg("hthresh6", *argv
);
1053 if (get_u8(&thr
.lbits
, *argv
, 0) || thr
.lbits
> 128)
1054 invarg("hthresh6 LBITS value is invalid", *argv
);
1056 if (get_u8(&thr
.rbits
, *argv
, 0) || thr
.rbits
> 128)
1057 invarg("hthresh6 RBITS value is invalid", *argv
);
1059 addattr_l(&req
.n
, sizeof(req
), XFRMA_SPD_IPV6_HTHRESH
,
1060 (void *)&thr
, sizeof(thr
));
1062 invarg("unknown", *argv
);
1068 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1071 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
1079 static int xfrm_spd_getinfo(int argc
, char **argv
)
1081 struct rtnl_handle rth
;
1086 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(__u32
)),
1087 .n
.nlmsg_flags
= NLM_F_REQUEST
,
1088 .n
.nlmsg_type
= XFRM_MSG_GETSPDINFO
,
1089 .flags
= 0XFFFFFFFF,
1091 struct nlmsghdr
*answer
;
1093 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1096 if (rtnl_talk(&rth
, &req
.n
, &answer
) < 0)
1099 print_spdinfo(answer
, (void *)stdout
);
1107 static int xfrm_policy_flush(int argc
, char **argv
)
1109 struct rtnl_handle rth
;
1112 char buf
[RTA_BUF_SIZE
];
1114 .n
.nlmsg_len
= NLMSG_LENGTH(0), /* nlmsg data is nothing */
1115 .n
.nlmsg_flags
= NLM_F_REQUEST
,
1116 .n
.nlmsg_type
= XFRM_MSG_FLUSHPOLICY
,
1118 char *ptypep
= NULL
;
1119 struct xfrm_userpolicy_type upt
= {};
1122 if (strcmp(*argv
, "ptype") == 0) {
1124 duparg("ptype", *argv
);
1128 xfrm_policy_ptype_parse(&upt
.type
, &argc
, &argv
);
1130 invarg("unknown", *argv
);
1136 addattr_l(&req
.n
, sizeof(req
), XFRMA_POLICY_TYPE
,
1137 (void *)&upt
, sizeof(upt
));
1140 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1144 fprintf(stderr
, "Flush policy\n");
1146 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
1154 int do_xfrm_policy(int argc
, char **argv
)
1157 return xfrm_policy_list_or_deleteall(0, NULL
, 0);
1159 if (matches(*argv
, "add") == 0)
1160 return xfrm_policy_modify(XFRM_MSG_NEWPOLICY
, 0,
1162 if (matches(*argv
, "update") == 0)
1163 return xfrm_policy_modify(XFRM_MSG_UPDPOLICY
, 0,
1165 if (matches(*argv
, "delete") == 0)
1166 return xfrm_policy_delete(argc
-1, argv
+1);
1167 if (matches(*argv
, "deleteall") == 0 || matches(*argv
, "delall") == 0)
1168 return xfrm_policy_list_or_deleteall(argc
-1, argv
+1, 1);
1169 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
1170 || matches(*argv
, "lst") == 0)
1171 return xfrm_policy_list_or_deleteall(argc
-1, argv
+1, 0);
1172 if (matches(*argv
, "get") == 0)
1173 return xfrm_policy_get(argc
-1, argv
+1);
1174 if (matches(*argv
, "flush") == 0)
1175 return xfrm_policy_flush(argc
-1, argv
+1);
1176 if (matches(*argv
, "count") == 0)
1177 return xfrm_spd_getinfo(argc
, argv
);
1178 if (matches(*argv
, "set") == 0)
1179 return xfrm_spd_setinfo(argc
-1, argv
+1);
1180 if (matches(*argv
, "help") == 0)
1182 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv
);