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 } [ 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
];
256 struct xfrm_userpolicy_type upt
;
257 char tmpls_buf
[XFRM_TMPLS_BUF_SIZE
];
259 struct xfrm_mark mark
= {0, 0};
261 struct xfrm_user_sec_ctx sctx
;
262 char str
[CTX_BUF_SIZE
];
265 memset(&req
, 0, sizeof(req
));
266 memset(&upt
, 0, sizeof(upt
));
267 memset(&tmpls_buf
, 0, sizeof(tmpls_buf
));
268 memset(&ctx
, 0, sizeof(ctx
));
270 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xpinfo
));
271 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
272 req
.n
.nlmsg_type
= cmd
;
273 req
.xpinfo
.sel
.family
= preferred_family
;
275 req
.xpinfo
.lft
.soft_byte_limit
= XFRM_INF
;
276 req
.xpinfo
.lft
.hard_byte_limit
= XFRM_INF
;
277 req
.xpinfo
.lft
.soft_packet_limit
= XFRM_INF
;
278 req
.xpinfo
.lft
.hard_packet_limit
= XFRM_INF
;
281 if (strcmp(*argv
, "dir") == 0) {
283 duparg("dir", *argv
);
287 xfrm_policy_dir_parse(&req
.xpinfo
.dir
, &argc
, &argv
);
288 } else if (strcmp(*argv
, "ctx") == 0) {
292 duparg("ctx", *argv
);
296 xfrm_sctx_parse((char *)&ctx
.str
, context
, &ctx
.sctx
);
297 } else if (strcmp(*argv
, "mark") == 0) {
298 xfrm_parse_mark(&mark
, &argc
, &argv
);
299 } else if (strcmp(*argv
, "index") == 0) {
301 if (get_u32(&req
.xpinfo
.index
, *argv
, 0))
302 invarg("INDEX value is invalid", *argv
);
303 } else if (strcmp(*argv
, "ptype") == 0) {
305 duparg("ptype", *argv
);
309 xfrm_policy_ptype_parse(&upt
.type
, &argc
, &argv
);
310 } else if (strcmp(*argv
, "action") == 0) {
312 if (strcmp(*argv
, "allow") == 0)
313 req
.xpinfo
.action
= XFRM_POLICY_ALLOW
;
314 else if (strcmp(*argv
, "block") == 0)
315 req
.xpinfo
.action
= XFRM_POLICY_BLOCK
;
317 invarg("ACTION value is invalid\n", *argv
);
318 } else if (strcmp(*argv
, "priority") == 0) {
320 if (get_u32(&req
.xpinfo
.priority
, *argv
, 0))
321 invarg("PRIORITY value is invalid", *argv
);
322 } else if (strcmp(*argv
, "flag") == 0) {
324 xfrm_policy_flag_parse(&req
.xpinfo
.flags
, &argc
,
326 } else if (strcmp(*argv
, "limit") == 0) {
328 xfrm_lifetime_cfg_parse(&req
.xpinfo
.lft
, &argc
, &argv
);
329 } else if (strcmp(*argv
, "tmpl") == 0) {
330 struct xfrm_user_tmpl
*tmpl
;
332 if (tmpls_len
+ sizeof(*tmpl
) > sizeof(tmpls_buf
)) {
333 fprintf(stderr
, "Too many tmpls: buffer overflow\n");
336 tmpl
= (struct xfrm_user_tmpl
*)((char *)tmpls_buf
+ tmpls_len
);
338 tmpl
->family
= preferred_family
;
339 tmpl
->aalgos
= (~(__u32
)0);
340 tmpl
->ealgos
= (~(__u32
)0);
341 tmpl
->calgos
= (~(__u32
)0);
344 xfrm_tmpl_parse(tmpl
, &argc
, &argv
);
346 tmpls_len
+= sizeof(*tmpl
);
349 duparg("unknown", *argv
);
352 xfrm_selector_parse(&req
.xpinfo
.sel
, &argc
, &argv
);
353 if (preferred_family
== AF_UNSPEC
)
354 preferred_family
= req
.xpinfo
.sel
.family
;
361 fprintf(stderr
, "Not enough information: DIR is required.\n");
366 addattr_l(&req
.n
, sizeof(req
), XFRMA_POLICY_TYPE
,
367 (void *)&upt
, sizeof(upt
));
371 addattr_l(&req
.n
, sizeof(req
), XFRMA_TMPL
,
372 (void *)tmpls_buf
, tmpls_len
);
376 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
377 (void *)&mark
, sizeof(mark
));
379 fprintf(stderr
, "%s: XFRMA_MARK failed\n", __func__
);
385 addattr_l(&req
.n
, sizeof(req
), XFRMA_SEC_CTX
,
386 (void *)&ctx
, ctx
.sctx
.len
);
389 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
392 if (req
.xpinfo
.sel
.family
== AF_UNSPEC
)
393 req
.xpinfo
.sel
.family
= AF_INET
;
395 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
403 static int xfrm_policy_filter_match(struct xfrm_userpolicy_info
*xpinfo
,
409 if ((xpinfo
->dir
^filter
.xpinfo
.dir
)&filter
.dir_mask
)
412 if ((ptype
^filter
.ptype
)&filter
.ptype_mask
)
415 if (filter
.sel_src_mask
) {
416 if (xfrm_addr_match(&xpinfo
->sel
.saddr
, &filter
.xpinfo
.sel
.saddr
,
417 filter
.sel_src_mask
))
421 if (filter
.sel_dst_mask
) {
422 if (xfrm_addr_match(&xpinfo
->sel
.daddr
, &filter
.xpinfo
.sel
.daddr
,
423 filter
.sel_dst_mask
))
427 if ((xpinfo
->sel
.ifindex
^filter
.xpinfo
.sel
.ifindex
)&filter
.sel_dev_mask
)
430 if ((xpinfo
->sel
.proto
^filter
.xpinfo
.sel
.proto
)&filter
.upspec_proto_mask
)
433 if (filter
.upspec_sport_mask
) {
434 if ((xpinfo
->sel
.sport
^filter
.xpinfo
.sel
.sport
)&filter
.upspec_sport_mask
)
438 if (filter
.upspec_dport_mask
) {
439 if ((xpinfo
->sel
.dport
^filter
.xpinfo
.sel
.dport
)&filter
.upspec_dport_mask
)
443 if ((xpinfo
->index
^filter
.xpinfo
.index
)&filter
.index_mask
)
446 if ((xpinfo
->action
^filter
.xpinfo
.action
)&filter
.action_mask
)
449 if ((xpinfo
->priority
^filter
.xpinfo
.priority
)&filter
.priority_mask
)
452 if (filter
.policy_flags_mask
)
453 if ((xpinfo
->flags
& filter
.xpinfo
.flags
) == 0)
459 int xfrm_policy_print(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
462 struct rtattr
*tb
[XFRMA_MAX
+1];
464 struct xfrm_userpolicy_info
*xpinfo
= NULL
;
465 struct xfrm_user_polexpire
*xpexp
= NULL
;
466 struct xfrm_userpolicy_id
*xpid
= NULL
;
467 __u8 ptype
= XFRM_POLICY_TYPE_MAIN
;
468 FILE *fp
= (FILE *)arg
;
469 int len
= n
->nlmsg_len
;
471 if (n
->nlmsg_type
!= XFRM_MSG_NEWPOLICY
&&
472 n
->nlmsg_type
!= XFRM_MSG_DELPOLICY
&&
473 n
->nlmsg_type
!= XFRM_MSG_UPDPOLICY
&&
474 n
->nlmsg_type
!= XFRM_MSG_POLEXPIRE
) {
475 fprintf(stderr
, "Not a policy: %08x %08x %08x\n",
476 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
480 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
) {
481 xpid
= NLMSG_DATA(n
);
482 len
-= NLMSG_SPACE(sizeof(*xpid
));
483 } else if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
) {
484 xpexp
= NLMSG_DATA(n
);
485 xpinfo
= &xpexp
->pol
;
486 len
-= NLMSG_SPACE(sizeof(*xpexp
));
489 xpinfo
= NLMSG_DATA(n
);
490 len
-= NLMSG_SPACE(sizeof(*xpinfo
));
494 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
498 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
)
499 rta
= XFRMPID_RTA(xpid
);
500 else if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
)
501 rta
= XFRMPEXP_RTA(xpexp
);
503 rta
= XFRMP_RTA(xpinfo
);
505 parse_rtattr(tb
, XFRMA_MAX
, rta
, len
);
507 if (tb
[XFRMA_POLICY_TYPE
]) {
508 struct xfrm_userpolicy_type
*upt
;
510 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
)) {
511 fprintf(stderr
, "too short XFRMA_POLICY_TYPE len\n");
514 upt
= (struct xfrm_userpolicy_type
*)RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
518 if (xpinfo
&& !xfrm_policy_filter_match(xpinfo
, ptype
))
521 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
)
522 fprintf(fp
, "Deleted ");
523 else if (n
->nlmsg_type
== XFRM_MSG_UPDPOLICY
)
524 fprintf(fp
, "Updated ");
525 else if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
)
526 fprintf(fp
, "Expired ");
528 if (n
->nlmsg_type
== XFRM_MSG_DELPOLICY
) {
529 /* xfrm_policy_id_print(); */
530 if (!tb
[XFRMA_POLICY
]) {
531 fprintf(stderr
, "Buggy XFRM_MSG_DELPOLICY: no XFRMA_POLICY\n");
534 if (RTA_PAYLOAD(tb
[XFRMA_POLICY
]) < sizeof(*xpinfo
)) {
535 fprintf(stderr
, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
538 xpinfo
= (struct xfrm_userpolicy_info
*)RTA_DATA(tb
[XFRMA_POLICY
]);
541 xfrm_policy_info_print(xpinfo
, tb
, fp
, NULL
, NULL
);
543 if (n
->nlmsg_type
== XFRM_MSG_POLEXPIRE
) {
545 fprintf(fp
, "hard %u", xpexp
->hard
);
546 fprintf(fp
, "%s", _SL_
);
556 static int xfrm_policy_get_or_delete(int argc
, char **argv
, int delete,
557 void *res_nlbuf
, size_t res_size
)
559 struct rtnl_handle rth
;
562 struct xfrm_userpolicy_id xpid
;
563 char buf
[RTA_BUF_SIZE
];
570 struct xfrm_userpolicy_type upt
;
571 struct xfrm_mark mark
= {0, 0};
573 struct xfrm_user_sec_ctx sctx
;
574 char str
[CTX_BUF_SIZE
];
578 memset(&req
, 0, sizeof(req
));
579 memset(&upt
, 0, sizeof(upt
));
580 memset(&ctx
, 0, sizeof(ctx
));
582 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xpid
));
583 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
584 req
.n
.nlmsg_type
= delete ? XFRM_MSG_DELPOLICY
: XFRM_MSG_GETPOLICY
;
587 if (strcmp(*argv
, "dir") == 0) {
589 duparg("dir", *argv
);
593 xfrm_policy_dir_parse(&req
.xpid
.dir
, &argc
, &argv
);
595 } else if (strcmp(*argv
, "ctx") == 0) {
599 duparg("ctx", *argv
);
603 xfrm_sctx_parse((char *)&ctx
.str
, context
, &ctx
.sctx
);
604 } else if (strcmp(*argv
, "mark") == 0) {
605 xfrm_parse_mark(&mark
, &argc
, &argv
);
606 } else if (strcmp(*argv
, "index") == 0) {
608 duparg("index", *argv
);
612 if (get_u32(&req
.xpid
.index
, *argv
, 0))
613 invarg("INDEX value is invalid", *argv
);
615 } else if (strcmp(*argv
, "ptype") == 0) {
617 duparg("ptype", *argv
);
621 xfrm_policy_ptype_parse(&upt
.type
, &argc
, &argv
);
625 invarg("unknown", *argv
);
628 xfrm_selector_parse(&req
.xpid
.sel
, &argc
, &argv
);
629 if (preferred_family
== AF_UNSPEC
)
630 preferred_family
= req
.xpid
.sel
.family
;
638 fprintf(stderr
, "Not enough information: DIR is required.\n");
642 addattr_l(&req
.n
, sizeof(req
), XFRMA_POLICY_TYPE
,
643 (void *)&upt
, sizeof(upt
));
645 if (!selp
&& !indexp
) {
646 fprintf(stderr
, "Not enough information: either SELECTOR or INDEX is required.\n");
650 duparg2("SELECTOR", "INDEX");
652 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
655 if (req
.xpid
.sel
.family
== AF_UNSPEC
)
656 req
.xpid
.sel
.family
= AF_INET
;
658 if (mark
.m
& mark
.v
) {
659 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
660 (void *)&mark
, sizeof(mark
));
662 fprintf(stderr
, "%s: XFRMA_MARK failed\n", __func__
);
668 addattr_l(&req
.n
, sizeof(req
), XFRMA_SEC_CTX
,
669 (void *)&ctx
, ctx
.sctx
.len
);
672 if (rtnl_talk(&rth
, &req
.n
, res_nlbuf
, res_size
) < 0)
680 static int xfrm_policy_delete(int argc
, char **argv
)
682 return xfrm_policy_get_or_delete(argc
, argv
, 1, NULL
, 0);
685 static int xfrm_policy_get(int argc
, char **argv
)
687 char buf
[NLMSG_BUF_SIZE
];
688 struct nlmsghdr
*n
= (struct nlmsghdr
*)buf
;
690 memset(buf
, 0, sizeof(buf
));
692 xfrm_policy_get_or_delete(argc
, argv
, 0, n
, sizeof(buf
));
694 if (xfrm_policy_print(NULL
, n
, (void *)stdout
) < 0) {
695 fprintf(stderr
, "An error :-)\n");
703 * With an existing policy of nlmsg, make new nlmsg for deleting the policy
704 * and store it to buffer.
706 static int xfrm_policy_keep(const struct sockaddr_nl
*who
,
710 struct xfrm_buffer
*xb
= (struct xfrm_buffer
*)arg
;
711 struct rtnl_handle
*rth
= xb
->rth
;
712 struct xfrm_userpolicy_info
*xpinfo
= NLMSG_DATA(n
);
713 int len
= n
->nlmsg_len
;
714 struct rtattr
*tb
[XFRMA_MAX
+1];
715 __u8 ptype
= XFRM_POLICY_TYPE_MAIN
;
716 struct nlmsghdr
*new_n
;
717 struct xfrm_userpolicy_id
*xpid
;
719 if (n
->nlmsg_type
!= XFRM_MSG_NEWPOLICY
) {
720 fprintf(stderr
, "Not a policy: %08x %08x %08x\n",
721 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
725 len
-= NLMSG_LENGTH(sizeof(*xpinfo
));
727 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
731 parse_rtattr(tb
, XFRMA_MAX
, XFRMP_RTA(xpinfo
), len
);
733 if (tb
[XFRMA_POLICY_TYPE
]) {
734 struct xfrm_userpolicy_type
*upt
;
736 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
)) {
737 fprintf(stderr
, "too short XFRMA_POLICY_TYPE len\n");
740 upt
= (struct xfrm_userpolicy_type
*)RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
744 if (!xfrm_policy_filter_match(xpinfo
, ptype
))
747 if (xb
->offset
> xb
->size
) {
748 fprintf(stderr
, "Policy buffer overflow\n");
752 new_n
= (struct nlmsghdr
*)(xb
->buf
+ xb
->offset
);
753 new_n
->nlmsg_len
= NLMSG_LENGTH(sizeof(*xpid
));
754 new_n
->nlmsg_flags
= NLM_F_REQUEST
;
755 new_n
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
756 new_n
->nlmsg_seq
= ++rth
->seq
;
758 xpid
= NLMSG_DATA(new_n
);
759 memcpy(&xpid
->sel
, &xpinfo
->sel
, sizeof(xpid
->sel
));
760 xpid
->dir
= xpinfo
->dir
;
761 xpid
->index
= xpinfo
->index
;
763 xb
->offset
+= new_n
->nlmsg_len
;
769 static int xfrm_policy_list_or_deleteall(int argc
, char **argv
, int deleteall
)
772 struct rtnl_handle rth
;
776 filter
.xpinfo
.sel
.family
= preferred_family
;
779 if (strcmp(*argv
, "dir") == 0) {
781 xfrm_policy_dir_parse(&filter
.xpinfo
.dir
, &argc
, &argv
);
783 filter
.dir_mask
= XFRM_FILTER_MASK_FULL
;
785 } else if (strcmp(*argv
, "index") == 0) {
787 if (get_u32(&filter
.xpinfo
.index
, *argv
, 0))
788 invarg("INDEX value is invalid", *argv
);
790 filter
.index_mask
= XFRM_FILTER_MASK_FULL
;
792 } else if (strcmp(*argv
, "ptype") == 0) {
794 xfrm_policy_ptype_parse(&filter
.ptype
, &argc
, &argv
);
796 filter
.ptype_mask
= XFRM_FILTER_MASK_FULL
;
798 } else if (strcmp(*argv
, "action") == 0) {
800 if (strcmp(*argv
, "allow") == 0)
801 filter
.xpinfo
.action
= XFRM_POLICY_ALLOW
;
802 else if (strcmp(*argv
, "block") == 0)
803 filter
.xpinfo
.action
= XFRM_POLICY_BLOCK
;
805 invarg("ACTION value is invalid\n", *argv
);
807 filter
.action_mask
= XFRM_FILTER_MASK_FULL
;
809 } else if (strcmp(*argv
, "priority") == 0) {
811 if (get_u32(&filter
.xpinfo
.priority
, *argv
, 0))
812 invarg("PRIORITY value is invalid", *argv
);
814 filter
.priority_mask
= XFRM_FILTER_MASK_FULL
;
816 } else if (strcmp(*argv
, "flag") == 0) {
818 xfrm_policy_flag_parse(&filter
.xpinfo
.flags
, &argc
,
821 filter
.policy_flags_mask
= XFRM_FILTER_MASK_FULL
;
825 invarg("unknown", *argv
);
828 xfrm_selector_parse(&filter
.xpinfo
.sel
, &argc
, &argv
);
829 if (preferred_family
== AF_UNSPEC
)
830 preferred_family
= filter
.xpinfo
.sel
.family
;
837 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
841 struct xfrm_buffer xb
;
842 char buf
[NLMSG_DELETEALL_BUF_SIZE
];
846 xb
.size
= sizeof(buf
);
852 char buf
[NLMSG_BUF_SIZE
];
854 .n
.nlmsg_len
= NLMSG_HDRLEN
,
855 .n
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
856 .n
.nlmsg_type
= XFRM_MSG_GETPOLICY
,
857 .n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
,
864 fprintf(stderr
, "Delete-all round = %d\n", i
);
866 if (rtnl_send(&rth
, (void *)&req
, req
.n
.nlmsg_len
) < 0) {
867 perror("Cannot send dump request");
871 if (rtnl_dump_filter(&rth
, xfrm_policy_keep
, &xb
) < 0) {
872 fprintf(stderr
, "Delete-all terminated\n");
875 if (xb
.nlmsg_count
== 0) {
877 fprintf(stderr
, "Delete-all completed\n");
881 if (rtnl_send_check(&rth
, xb
.buf
, xb
.offset
) < 0) {
882 perror("Failed to send delete-all request");
886 fprintf(stderr
, "Delete-all nlmsg count = %d\n", xb
.nlmsg_count
);
894 char buf
[NLMSG_BUF_SIZE
];
896 .n
.nlmsg_len
= NLMSG_HDRLEN
,
897 .n
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
898 .n
.nlmsg_type
= XFRM_MSG_GETPOLICY
,
899 .n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
,
902 if (rtnl_send(&rth
, (void *)&req
, req
.n
.nlmsg_len
) < 0) {
903 perror("Cannot send dump request");
907 if (rtnl_dump_filter(&rth
, xfrm_policy_print
, stdout
) < 0) {
908 fprintf(stderr
, "Dump terminated\n");
918 static int print_spdinfo(struct nlmsghdr
*n
, void *arg
)
920 FILE *fp
= (FILE *)arg
;
921 __u32
*f
= NLMSG_DATA(n
);
922 struct rtattr
*tb
[XFRMA_SPD_MAX
+1];
925 int len
= n
->nlmsg_len
;
927 len
-= NLMSG_LENGTH(sizeof(__u32
));
929 fprintf(stderr
, "SPDinfo: Wrong len %d\n", len
);
933 rta
= XFRMSAPD_RTA(f
);
934 parse_rtattr(tb
, XFRMA_SPD_MAX
, rta
, len
);
936 fprintf(fp
, "\t SPD");
937 if (tb
[XFRMA_SPD_INFO
]) {
938 struct xfrmu_spdinfo
*si
;
940 if (RTA_PAYLOAD(tb
[XFRMA_SPD_INFO
]) < sizeof(*si
)) {
941 fprintf(stderr
, "SPDinfo: Wrong len %d\n", len
);
944 si
= RTA_DATA(tb
[XFRMA_SPD_INFO
]);
945 fprintf(fp
, " IN %d", si
->incnt
);
946 fprintf(fp
, " OUT %d", si
->outcnt
);
947 fprintf(fp
, " FWD %d", si
->fwdcnt
);
950 fprintf(fp
, " (Sock:");
951 fprintf(fp
, " IN %d", si
->inscnt
);
952 fprintf(fp
, " OUT %d", si
->outscnt
);
953 fprintf(fp
, " FWD %d", si
->fwdscnt
);
957 fprintf(fp
, "%s", _SL_
);
959 if (show_stats
> 1) {
960 struct xfrmu_spdhinfo
*sh
;
962 if (tb
[XFRMA_SPD_HINFO
]) {
963 if (RTA_PAYLOAD(tb
[XFRMA_SPD_HINFO
]) < sizeof(*sh
)) {
964 fprintf(stderr
, "SPDinfo: Wrong len %d\n", len
);
967 sh
= RTA_DATA(tb
[XFRMA_SPD_HINFO
]);
968 fprintf(fp
, "\t SPD buckets:");
969 fprintf(fp
, " count %d", sh
->spdhcnt
);
970 fprintf(fp
, " Max %d", sh
->spdhmcnt
);
971 fprintf(fp
, "%s", _SL_
);
973 if (tb
[XFRMA_SPD_IPV4_HTHRESH
]) {
974 struct xfrmu_spdhthresh
*th
;
976 if (RTA_PAYLOAD(tb
[XFRMA_SPD_IPV4_HTHRESH
]) < sizeof(*th
)) {
977 fprintf(stderr
, "SPDinfo: Wrong len %d\n", len
);
980 th
= RTA_DATA(tb
[XFRMA_SPD_IPV4_HTHRESH
]);
981 fprintf(fp
, "\t SPD IPv4 thresholds:");
982 fprintf(fp
, " local %d", th
->lbits
);
983 fprintf(fp
, " remote %d", th
->rbits
);
984 fprintf(fp
, "%s", _SL_
);
987 if (tb
[XFRMA_SPD_IPV6_HTHRESH
]) {
988 struct xfrmu_spdhthresh
*th
;
990 if (RTA_PAYLOAD(tb
[XFRMA_SPD_IPV6_HTHRESH
]) < sizeof(*th
)) {
991 fprintf(stderr
, "SPDinfo: Wrong len %d\n", len
);
994 th
= RTA_DATA(tb
[XFRMA_SPD_IPV6_HTHRESH
]);
995 fprintf(fp
, "\t SPD IPv6 thresholds:");
996 fprintf(fp
, " local %d", th
->lbits
);
997 fprintf(fp
, " remote %d", th
->rbits
);
998 fprintf(fp
, "%s", _SL_
);
1008 static int xfrm_spd_setinfo(int argc
, char **argv
)
1010 struct rtnl_handle rth
;
1014 char buf
[RTA_BUF_SIZE
];
1020 memset(&req
, 0, sizeof(req
));
1022 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(__u32
));
1023 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1024 req
.n
.nlmsg_type
= XFRM_MSG_NEWSPDINFO
;
1025 req
.flags
= 0XFFFFFFFF;
1028 if (strcmp(*argv
, "hthresh4") == 0) {
1029 struct xfrmu_spdhthresh thr
;
1032 duparg("hthresh4", *argv
);
1035 if (get_u8(&thr
.lbits
, *argv
, 0) || thr
.lbits
> 32)
1036 invarg("hthresh4 LBITS value is invalid", *argv
);
1038 if (get_u8(&thr
.rbits
, *argv
, 0) || thr
.rbits
> 32)
1039 invarg("hthresh4 RBITS value is invalid", *argv
);
1041 addattr_l(&req
.n
, sizeof(req
), XFRMA_SPD_IPV4_HTHRESH
,
1042 (void *)&thr
, sizeof(thr
));
1043 } else if (strcmp(*argv
, "hthresh6") == 0) {
1044 struct xfrmu_spdhthresh thr
;
1047 duparg("hthresh6", *argv
);
1050 if (get_u8(&thr
.lbits
, *argv
, 0) || thr
.lbits
> 128)
1051 invarg("hthresh6 LBITS value is invalid", *argv
);
1053 if (get_u8(&thr
.rbits
, *argv
, 0) || thr
.rbits
> 128)
1054 invarg("hthresh6 RBITS value is invalid", *argv
);
1056 addattr_l(&req
.n
, sizeof(req
), XFRMA_SPD_IPV6_HTHRESH
,
1057 (void *)&thr
, sizeof(thr
));
1059 invarg("unknown", *argv
);
1065 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1068 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
1076 static int xfrm_spd_getinfo(int argc
, char **argv
)
1078 struct rtnl_handle rth
;
1085 memset(&req
, 0, sizeof(req
));
1087 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(__u32
));
1088 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1089 req
.n
.nlmsg_type
= XFRM_MSG_GETSPDINFO
;
1090 req
.flags
= 0XFFFFFFFF;
1092 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1095 if (rtnl_talk(&rth
, &req
.n
, &req
.n
, sizeof(req
)) < 0)
1098 print_spdinfo(&req
.n
, (void *)stdout
);
1105 static int xfrm_policy_flush(int argc
, char **argv
)
1107 struct rtnl_handle rth
;
1110 char buf
[RTA_BUF_SIZE
];
1112 char *ptypep
= NULL
;
1113 struct xfrm_userpolicy_type upt
;
1115 memset(&req
, 0, sizeof(req
));
1116 memset(&upt
, 0, sizeof(upt
));
1118 req
.n
.nlmsg_len
= NLMSG_LENGTH(0); /* nlmsg data is nothing */
1119 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1120 req
.n
.nlmsg_type
= XFRM_MSG_FLUSHPOLICY
;
1123 if (strcmp(*argv
, "ptype") == 0) {
1125 duparg("ptype", *argv
);
1129 xfrm_policy_ptype_parse(&upt
.type
, &argc
, &argv
);
1131 invarg("unknown", *argv
);
1137 addattr_l(&req
.n
, sizeof(req
), XFRMA_POLICY_TYPE
,
1138 (void *)&upt
, sizeof(upt
));
1141 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1145 fprintf(stderr
, "Flush policy\n");
1147 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
1155 int do_xfrm_policy(int argc
, char **argv
)
1158 return xfrm_policy_list_or_deleteall(0, NULL
, 0);
1160 if (matches(*argv
, "add") == 0)
1161 return xfrm_policy_modify(XFRM_MSG_NEWPOLICY
, 0,
1163 if (matches(*argv
, "update") == 0)
1164 return xfrm_policy_modify(XFRM_MSG_UPDPOLICY
, 0,
1166 if (matches(*argv
, "delete") == 0)
1167 return xfrm_policy_delete(argc
-1, argv
+1);
1168 if (matches(*argv
, "deleteall") == 0 || matches(*argv
, "delall") == 0)
1169 return xfrm_policy_list_or_deleteall(argc
-1, argv
+1, 1);
1170 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
1171 || matches(*argv
, "lst") == 0)
1172 return xfrm_policy_list_or_deleteall(argc
-1, argv
+1, 0);
1173 if (matches(*argv
, "get") == 0)
1174 return xfrm_policy_get(argc
-1, argv
+1);
1175 if (matches(*argv
, "flush") == 0)
1176 return xfrm_policy_flush(argc
-1, argv
+1);
1177 if (matches(*argv
, "count") == 0)
1178 return xfrm_spd_getinfo(argc
, argv
);
1179 if (matches(*argv
, "set") == 0)
1180 return xfrm_spd_setinfo(argc
-1, argv
+1);
1181 if (matches(*argv
, "help") == 0)
1183 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv
);