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/xfrm.h>
35 #include "ip_common.h"
37 //#define NLMSG_DELETEALL_BUF_SIZE (4096-512)
38 #define NLMSG_DELETEALL_BUF_SIZE 8192
41 * Receiving buffer defines:
43 * data = struct xfrm_usersa_info
46 * ... (max count of rtattr is XFRM_MAX+1
48 * each rtattr data = struct xfrm_algo(dynamic size) or xfrm_address_t
50 #define NLMSG_BUF_SIZE 4096
51 #define RTA_BUF_SIZE 2048
52 #define XFRM_ALGO_KEY_BUF_SIZE 512
53 #define CTX_BUF_SIZE 256
55 static void usage(void) __attribute__((noreturn
));
57 static void usage(void)
59 fprintf(stderr
, "Usage: ip xfrm state { add | update } ID [ ALGO-LIST ] [ mode MODE ]\n");
60 fprintf(stderr
, " [ mark MARK [ mask MASK ] ] [ reqid REQID ] [ seq SEQ ]\n");
61 fprintf(stderr
, " [ replay-window SIZE ] [ replay-seq SEQ ] [ replay-oseq SEQ ]\n");
62 fprintf(stderr
, " [ flag FLAG-LIST ] [ sel SELECTOR ] [ LIMIT-LIST ] [ encap ENCAP ]\n");
63 fprintf(stderr
, " [ coa ADDR[/PLEN] ] [ ctx CTX ]\n");
64 fprintf(stderr
, "Usage: ip xfrm state allocspi ID [ mode MODE ] [ mark MARK [ mask MASK ] ]\n");
65 fprintf(stderr
, " [ reqid REQID ] [ seq SEQ ] [ min SPI max SPI ]\n");
66 fprintf(stderr
, "Usage: ip xfrm state { delete | get } ID [ mark MARK [ mask MASK ] ]\n");
67 fprintf(stderr
, "Usage: ip xfrm state { deleteall | list } [ ID ] [ mode MODE ] [ reqid REQID ]\n");
68 fprintf(stderr
, " [ flag FLAG-LIST ]\n");
69 fprintf(stderr
, "Usage: ip xfrm state flush [ proto XFRM-PROTO ]\n");
70 fprintf(stderr
, "Usage: ip xfrm state count\n");
71 fprintf(stderr
, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ]\n");
72 fprintf(stderr
, "XFRM-PROTO := ");
73 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_ESP
));
74 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_AH
));
75 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_COMP
));
76 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING
));
77 fprintf(stderr
, "%s\n", strxf_xfrmproto(IPPROTO_DSTOPTS
));
78 fprintf(stderr
, "ALGO-LIST := [ ALGO-LIST ] ALGO\n");
79 fprintf(stderr
, "ALGO := { ");
80 fprintf(stderr
, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT
));
81 fprintf(stderr
, "%s | ", strxf_algotype(XFRMA_ALG_AUTH
));
82 fprintf(stderr
, "%s", strxf_algotype(XFRMA_ALG_COMP
));
83 fprintf(stderr
, " } ALGO-NAME ALGO-KEY |\n");
84 fprintf(stderr
, " %s", strxf_algotype(XFRMA_ALG_AEAD
));
85 fprintf(stderr
, " ALGO-NAME ALGO-KEY ALGO-ICV-LEN |\n");
86 fprintf(stderr
, " %s", strxf_algotype(XFRMA_ALG_AUTH_TRUNC
));
87 fprintf(stderr
, " ALGO-NAME ALGO-KEY ALGO-TRUNC-LEN\n");
88 fprintf(stderr
, "MODE := transport | tunnel | ro | in_trigger | beet\n");
89 fprintf(stderr
, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
90 fprintf(stderr
, "FLAG := noecn | decap-dscp | nopmtudisc | wildrecv | icmp | af-unspec | align4\n");
91 fprintf(stderr
, "SELECTOR := [ src ADDR[/PLEN] ] [ dst ADDR[/PLEN] ] [ dev DEV ] [ UPSPEC ]\n");
92 fprintf(stderr
, "UPSPEC := proto { { ");
93 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_TCP
));
94 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_UDP
));
95 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_SCTP
));
96 fprintf(stderr
, "%s", strxf_proto(IPPROTO_DCCP
));
97 fprintf(stderr
, " } [ sport PORT ] [ dport PORT ] |\n");
98 fprintf(stderr
, " { ");
99 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_ICMP
));
100 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_ICMPV6
));
101 fprintf(stderr
, "%s", strxf_proto(IPPROTO_MH
));
102 fprintf(stderr
, " } [ type NUMBER ] [ code NUMBER ] |\n");
103 fprintf(stderr
, " %s", strxf_proto(IPPROTO_GRE
));
104 fprintf(stderr
, " [ key { DOTTED-QUAD | NUMBER } ] | PROTO }\n");
105 fprintf(stderr
, "LIMIT-LIST := [ LIMIT-LIST ] limit LIMIT\n");
106 fprintf(stderr
, "LIMIT := { time-soft | time-hard | time-use-soft | time-use-hard } SECONDS |\n");
107 fprintf(stderr
, " { byte-soft | byte-hard } SIZE | { packet-soft | packet-hard } COUNT\n");
108 fprintf(stderr
, "ENCAP := { espinudp | espinudp-nonike } SPORT DPORT OADDR\n");
113 static int xfrm_algo_parse(struct xfrm_algo
*alg
, enum xfrm_attr_type_t type
,
114 char *name
, char *key
, char *buf
, int max
)
117 int slen
= strlen(key
);
120 /* XXX: verifying both name and key is required! */
121 fprintf(stderr
, "warning: ALGO-NAME/ALGO-KEY will send to kernel promiscuously! (verifying them isn't implemented yet)\n");
124 strncpy(alg
->alg_name
, name
, sizeof(alg
->alg_name
));
126 if (slen
> 2 && strncmp(key
, "0x", 2) == 0) {
127 /* split two chars "0x" from the top */
133 /* Converting hexadecimal numbered string into real key;
134 * Convert each two chars into one char(value). If number
135 * of the length is odd, add zero on the top for rounding.
138 /* calculate length of the converted values(real key) */
139 len
= (plen
+ 1) / 2;
141 invarg("\"ALGO-KEY\" makes buffer overflow\n", key
);
143 for (i
= - (plen
% 2), j
= 0; j
< len
; i
+= 2, j
++) {
147 vbuf
[0] = i
>= 0 ? p
[i
] : '0';
151 if (get_u8(&val
, vbuf
, 16))
152 invarg("\"ALGO-KEY\" is invalid", key
);
160 invarg("\"ALGO-KEY\" makes buffer overflow\n", key
);
162 strncpy(buf
, key
, len
);
166 alg
->alg_key_len
= len
* 8;
171 static int xfrm_seq_parse(__u32
*seq
, int *argcp
, char ***argvp
)
174 char **argv
= *argvp
;
176 if (get_u32(seq
, *argv
, 0))
177 invarg("\"SEQ\" is invalid", *argv
);
187 static int xfrm_state_flag_parse(__u8
*flags
, int *argcp
, char ***argvp
)
190 char **argv
= *argvp
;
191 int len
= strlen(*argv
);
193 if (len
> 2 && strncmp(*argv
, "0x", 2) == 0) {
196 if (get_u8(&val
, *argv
, 16))
197 invarg("\"FLAG\" is invalid", *argv
);
201 if (strcmp(*argv
, "noecn") == 0)
202 *flags
|= XFRM_STATE_NOECN
;
203 else if (strcmp(*argv
, "decap-dscp") == 0)
204 *flags
|= XFRM_STATE_DECAP_DSCP
;
205 else if (strcmp(*argv
, "nopmtudisc") == 0)
206 *flags
|= XFRM_STATE_NOPMTUDISC
;
207 else if (strcmp(*argv
, "wildrecv") == 0)
208 *flags
|= XFRM_STATE_WILDRECV
;
209 else if (strcmp(*argv
, "icmp") == 0)
210 *flags
|= XFRM_STATE_ICMP
;
211 else if (strcmp(*argv
, "af-unspec") == 0)
212 *flags
|= XFRM_STATE_AF_UNSPEC
;
213 else if (strcmp(*argv
, "align4") == 0)
214 *flags
|= XFRM_STATE_ALIGN4
;
216 PREV_ARG(); /* back track */
232 static int xfrm_state_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
234 struct rtnl_handle rth
;
237 struct xfrm_usersa_info xsinfo
;
238 char buf
[RTA_BUF_SIZE
];
240 struct xfrm_replay_state replay
;
248 struct xfrm_mark mark
= {0, 0};
250 struct xfrm_user_sec_ctx sctx
;
251 char str
[CTX_BUF_SIZE
];
254 memset(&req
, 0, sizeof(req
));
255 memset(&replay
, 0, sizeof(replay
));
256 memset(&ctx
, 0, sizeof(ctx
));
258 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsinfo
));
259 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
260 req
.n
.nlmsg_type
= cmd
;
261 req
.xsinfo
.family
= preferred_family
;
263 req
.xsinfo
.lft
.soft_byte_limit
= XFRM_INF
;
264 req
.xsinfo
.lft
.hard_byte_limit
= XFRM_INF
;
265 req
.xsinfo
.lft
.soft_packet_limit
= XFRM_INF
;
266 req
.xsinfo
.lft
.hard_packet_limit
= XFRM_INF
;
269 if (strcmp(*argv
, "mode") == 0) {
271 xfrm_mode_parse(&req
.xsinfo
.mode
, &argc
, &argv
);
272 } else if (strcmp(*argv
, "mark") == 0) {
273 xfrm_parse_mark(&mark
, &argc
, &argv
);
274 } else if (strcmp(*argv
, "reqid") == 0) {
276 xfrm_reqid_parse(&req
.xsinfo
.reqid
, &argc
, &argv
);
277 } else if (strcmp(*argv
, "seq") == 0) {
279 xfrm_seq_parse(&req
.xsinfo
.seq
, &argc
, &argv
);
280 } else if (strcmp(*argv
, "replay-window") == 0) {
282 if (get_u8(&req
.xsinfo
.replay_window
, *argv
, 0))
283 invarg("\"replay-window\" value is invalid", *argv
);
284 } else if (strcmp(*argv
, "replay-seq") == 0) {
286 if (get_u32(&replay
.seq
, *argv
, 0))
287 invarg("\"replay-seq\" value is invalid", *argv
);
288 } else if (strcmp(*argv
, "replay-oseq") == 0) {
290 if (get_u32(&replay
.oseq
, *argv
, 0))
291 invarg("\"replay-oseq\" value is invalid", *argv
);
292 } else if (strcmp(*argv
, "flag") == 0) {
294 xfrm_state_flag_parse(&req
.xsinfo
.flags
, &argc
, &argv
);
295 } else if (strcmp(*argv
, "sel") == 0) {
297 preferred_family
= AF_UNSPEC
;
298 xfrm_selector_parse(&req
.xsinfo
.sel
, &argc
, &argv
);
299 preferred_family
= req
.xsinfo
.sel
.family
;
300 } else if (strcmp(*argv
, "limit") == 0) {
302 xfrm_lifetime_cfg_parse(&req
.xsinfo
.lft
, &argc
, &argv
);
303 } else if (strcmp(*argv
, "encap") == 0) {
304 struct xfrm_encap_tmpl encap
;
307 xfrm_encap_type_parse(&encap
.encap_type
, &argc
, &argv
);
309 if (get_u16(&encap
.encap_sport
, *argv
, 0))
310 invarg("\"encap\" sport value is invalid", *argv
);
311 encap
.encap_sport
= htons(encap
.encap_sport
);
313 if (get_u16(&encap
.encap_dport
, *argv
, 0))
314 invarg("\"encap\" dport value is invalid", *argv
);
315 encap
.encap_dport
= htons(encap
.encap_dport
);
317 get_addr(&oa
, *argv
, AF_UNSPEC
);
318 memcpy(&encap
.encap_oa
, &oa
.data
, sizeof(encap
.encap_oa
));
319 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_ENCAP
,
320 (void *)&encap
, sizeof(encap
));
321 } else if (strcmp(*argv
, "coa") == 0) {
326 duparg("coa", *argv
);
331 get_prefix(&coa
, *argv
, preferred_family
);
332 if (coa
.family
== AF_UNSPEC
)
333 invarg("\"coa\" address family is AF_UNSPEC", *argv
);
334 if (coa
.bytelen
> sizeof(xcoa
))
335 invarg("\"coa\" address length is too large", *argv
);
337 memset(&xcoa
, 0, sizeof(xcoa
));
338 memcpy(&xcoa
, &coa
.data
, coa
.bytelen
);
340 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_COADDR
,
341 (void *)&xcoa
, sizeof(xcoa
));
342 } else if (strcmp(*argv
, "ctx") == 0) {
346 duparg("ctx", *argv
);
352 xfrm_sctx_parse((char *)&ctx
.str
, context
, &ctx
.sctx
);
353 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_SEC_CTX
,
354 (void *)&ctx
, ctx
.sctx
.len
);
356 /* try to assume ALGO */
357 int type
= xfrm_algotype_getbyname(*argv
);
360 case XFRMA_ALG_CRYPT
:
362 case XFRMA_ALG_AUTH_TRUNC
:
368 struct xfrm_algo alg
;
369 struct xfrm_algo_aead aead
;
370 struct xfrm_algo_auth auth
;
372 char buf
[XFRM_ALGO_KEY_BUF_SIZE
];
375 __u32 icvlen
, trunclen
;
382 if (ealgop
|| aalgop
|| aeadop
)
383 duparg("ALGO-TYPE", *argv
);
386 case XFRMA_ALG_CRYPT
:
387 if (ealgop
|| aeadop
)
388 duparg("ALGO-TYPE", *argv
);
392 case XFRMA_ALG_AUTH_TRUNC
:
393 if (aalgop
|| aeadop
)
394 duparg("ALGO-TYPE", *argv
);
399 duparg("ALGO-TYPE", *argv
);
404 invarg("\"ALGO-TYPE\" is invalid\n", *argv
);
408 missarg("ALGO-NAME");
417 buf
= alg
.u
.alg
.alg_key
;
418 len
= sizeof(alg
.u
.alg
);
423 missarg("ALGO-ICV-LEN");
425 if (get_u32(&icvlen
, *argv
, 0))
426 invarg("\"aead\" ICV length is invalid",
428 alg
.u
.aead
.alg_icv_len
= icvlen
;
430 buf
= alg
.u
.aead
.alg_key
;
431 len
= sizeof(alg
.u
.aead
);
433 case XFRMA_ALG_AUTH_TRUNC
:
435 missarg("ALGO-TRUNC-LEN");
437 if (get_u32(&trunclen
, *argv
, 0))
438 invarg("\"auth\" trunc length is invalid",
440 alg
.u
.auth
.alg_trunc_len
= trunclen
;
442 buf
= alg
.u
.auth
.alg_key
;
443 len
= sizeof(alg
.u
.auth
);
447 xfrm_algo_parse((void *)&alg
, type
, name
, key
,
448 buf
, sizeof(alg
.buf
));
449 len
+= alg
.u
.alg
.alg_key_len
;
451 addattr_l(&req
.n
, sizeof(req
.buf
), type
,
456 /* try to assume ID */
458 invarg("unknown", *argv
);
462 xfrm_id_parse(&req
.xsinfo
.saddr
, &req
.xsinfo
.id
,
463 &req
.xsinfo
.family
, 0, &argc
, &argv
);
464 if (preferred_family
== AF_UNSPEC
)
465 preferred_family
= req
.xsinfo
.family
;
471 if (replay
.seq
|| replay
.oseq
)
472 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_REPLAY_VAL
,
473 (void *)&replay
, sizeof(replay
));
476 fprintf(stderr
, "Not enough information: \"ID\" is required\n");
480 if (mark
.m
& mark
.v
) {
481 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
482 (void *)&mark
, sizeof(mark
));
484 fprintf(stderr
, "XFRMA_MARK failed\n");
489 switch (req
.xsinfo
.mode
) {
490 case XFRM_MODE_TRANSPORT
:
491 case XFRM_MODE_TUNNEL
:
492 if (!xfrm_xfrmproto_is_ipsec(req
.xsinfo
.id
.proto
)) {
493 fprintf(stderr
, "\"mode\" is invalid with proto=%s\n",
494 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
498 case XFRM_MODE_ROUTEOPTIMIZATION
:
499 case XFRM_MODE_IN_TRIGGER
:
500 if (!xfrm_xfrmproto_is_ro(req
.xsinfo
.id
.proto
)) {
501 fprintf(stderr
, "\"mode\" is invalid with proto=%s\n",
502 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
510 if (aeadop
|| ealgop
|| aalgop
|| calgop
) {
511 if (!xfrm_xfrmproto_is_ipsec(req
.xsinfo
.id
.proto
)) {
512 fprintf(stderr
, "\"ALGO\" is invalid with proto=%s\n",
513 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
517 if (xfrm_xfrmproto_is_ipsec(req
.xsinfo
.id
.proto
)) {
518 fprintf(stderr
, "\"ALGO\" is required with proto=%s\n",
519 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
525 if (!xfrm_xfrmproto_is_ro(req
.xsinfo
.id
.proto
)) {
526 fprintf(stderr
, "\"coa\" is invalid with proto=%s\n",
527 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
531 if (xfrm_xfrmproto_is_ro(req
.xsinfo
.id
.proto
)) {
532 fprintf(stderr
, "\"coa\" is required with proto=%s\n",
533 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
538 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
541 if (req
.xsinfo
.family
== AF_UNSPEC
)
542 req
.xsinfo
.family
= AF_INET
;
544 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
) < 0)
552 static int xfrm_state_allocspi(int argc
, char **argv
)
554 struct rtnl_handle rth
;
557 struct xfrm_userspi_info xspi
;
558 char buf
[RTA_BUF_SIZE
];
563 struct xfrm_mark mark
= {0, 0};
564 char res_buf
[NLMSG_BUF_SIZE
];
565 struct nlmsghdr
*res_n
= (struct nlmsghdr
*)res_buf
;
567 memset(res_buf
, 0, sizeof(res_buf
));
569 memset(&req
, 0, sizeof(req
));
571 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xspi
));
572 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
573 req
.n
.nlmsg_type
= XFRM_MSG_ALLOCSPI
;
574 req
.xspi
.info
.family
= preferred_family
;
577 req
.xsinfo
.lft
.soft_byte_limit
= XFRM_INF
;
578 req
.xsinfo
.lft
.hard_byte_limit
= XFRM_INF
;
579 req
.xsinfo
.lft
.soft_packet_limit
= XFRM_INF
;
580 req
.xsinfo
.lft
.hard_packet_limit
= XFRM_INF
;
584 if (strcmp(*argv
, "mode") == 0) {
586 xfrm_mode_parse(&req
.xspi
.info
.mode
, &argc
, &argv
);
587 } else if (strcmp(*argv
, "mark") == 0) {
588 xfrm_parse_mark(&mark
, &argc
, &argv
);
589 } else if (strcmp(*argv
, "reqid") == 0) {
591 xfrm_reqid_parse(&req
.xspi
.info
.reqid
, &argc
, &argv
);
592 } else if (strcmp(*argv
, "seq") == 0) {
594 xfrm_seq_parse(&req
.xspi
.info
.seq
, &argc
, &argv
);
595 } else if (strcmp(*argv
, "min") == 0) {
597 duparg("min", *argv
);
602 if (get_u32(&req
.xspi
.min
, *argv
, 0))
603 invarg("\"min\" value is invalid", *argv
);
604 } else if (strcmp(*argv
, "max") == 0) {
606 duparg("max", *argv
);
611 if (get_u32(&req
.xspi
.max
, *argv
, 0))
612 invarg("\"max\" value is invalid", *argv
);
614 /* try to assume ID */
616 invarg("unknown", *argv
);
620 xfrm_id_parse(&req
.xspi
.info
.saddr
, &req
.xspi
.info
.id
,
621 &req
.xspi
.info
.family
, 0, &argc
, &argv
);
622 if (req
.xspi
.info
.id
.spi
) {
623 fprintf(stderr
, "\"SPI\" must be zero\n");
626 if (preferred_family
== AF_UNSPEC
)
627 preferred_family
= req
.xspi
.info
.family
;
633 fprintf(stderr
, "Not enough information: \"ID\" is required\n");
639 fprintf(stderr
, "\"max\" is missing\n");
642 if (req
.xspi
.min
> req
.xspi
.max
) {
643 fprintf(stderr
, "\"min\" value is larger than \"max\" value\n");
648 fprintf(stderr
, "\"min\" is missing\n");
652 /* XXX: Default value defined in PF_KEY;
653 * See kernel's net/key/af_key.c(pfkey_getspi).
655 req
.xspi
.min
= 0x100;
656 req
.xspi
.max
= 0x0fffffff;
658 /* XXX: IPCOMP spi is 16-bits;
659 * See kernel's net/xfrm/xfrm_user(verify_userspi_info).
661 if (req
.xspi
.info
.id
.proto
== IPPROTO_COMP
)
662 req
.xspi
.max
= 0xffff;
665 if (mark
.m
& mark
.v
) {
666 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
667 (void *)&mark
, sizeof(mark
));
669 fprintf(stderr
, "XFRMA_MARK failed\n");
674 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
677 if (req
.xspi
.info
.family
== AF_UNSPEC
)
678 req
.xspi
.info
.family
= AF_INET
;
681 if (rtnl_talk(&rth
, &req
.n
, 0, 0, res_n
) < 0)
684 if (xfrm_state_print(NULL
, res_n
, (void*)stdout
) < 0) {
685 fprintf(stderr
, "An error :-)\n");
694 static int xfrm_state_filter_match(struct xfrm_usersa_info
*xsinfo
)
699 if (filter
.id_src_mask
)
700 if (xfrm_addr_match(&xsinfo
->saddr
, &filter
.xsinfo
.saddr
,
703 if (filter
.id_dst_mask
)
704 if (xfrm_addr_match(&xsinfo
->id
.daddr
, &filter
.xsinfo
.id
.daddr
,
707 if ((xsinfo
->id
.proto
^filter
.xsinfo
.id
.proto
)&filter
.id_proto_mask
)
709 if ((xsinfo
->id
.spi
^filter
.xsinfo
.id
.spi
)&filter
.id_spi_mask
)
711 if ((xsinfo
->mode
^filter
.xsinfo
.mode
)&filter
.mode_mask
)
713 if ((xsinfo
->reqid
^filter
.xsinfo
.reqid
)&filter
.reqid_mask
)
715 if (filter
.state_flags_mask
)
716 if ((xsinfo
->flags
& filter
.xsinfo
.flags
) == 0)
722 int xfrm_state_print(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
725 FILE *fp
= (FILE*)arg
;
726 struct rtattr
* tb
[XFRMA_MAX
+1];
728 struct xfrm_usersa_info
*xsinfo
= NULL
;
729 struct xfrm_user_expire
*xexp
= NULL
;
730 struct xfrm_usersa_id
*xsid
= NULL
;
731 int len
= n
->nlmsg_len
;
733 if (n
->nlmsg_type
!= XFRM_MSG_NEWSA
&&
734 n
->nlmsg_type
!= XFRM_MSG_DELSA
&&
735 n
->nlmsg_type
!= XFRM_MSG_UPDSA
&&
736 n
->nlmsg_type
!= XFRM_MSG_EXPIRE
) {
737 fprintf(stderr
, "Not a state: %08x %08x %08x\n",
738 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
742 if (n
->nlmsg_type
== XFRM_MSG_DELSA
) {
743 /* Dont blame me for this .. Herbert made me do it */
744 xsid
= NLMSG_DATA(n
);
745 len
-= NLMSG_SPACE(sizeof(*xsid
));
746 } else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
) {
747 xexp
= NLMSG_DATA(n
);
748 xsinfo
= &xexp
->state
;
749 len
-= NLMSG_SPACE(sizeof(*xexp
));
752 xsinfo
= NLMSG_DATA(n
);
753 len
-= NLMSG_SPACE(sizeof(*xsinfo
));
757 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
761 if (xsinfo
&& !xfrm_state_filter_match(xsinfo
))
764 if (n
->nlmsg_type
== XFRM_MSG_DELSA
)
765 fprintf(fp
, "Deleted ");
766 else if (n
->nlmsg_type
== XFRM_MSG_UPDSA
)
767 fprintf(fp
, "Updated ");
768 else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
)
769 fprintf(fp
, "Expired ");
771 if (n
->nlmsg_type
== XFRM_MSG_DELSA
)
772 rta
= XFRMSID_RTA(xsid
);
773 else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
)
774 rta
= XFRMEXP_RTA(xexp
);
776 rta
= XFRMS_RTA(xsinfo
);
778 parse_rtattr(tb
, XFRMA_MAX
, rta
, len
);
780 if (n
->nlmsg_type
== XFRM_MSG_DELSA
) {
781 //xfrm_policy_id_print();
784 fprintf(stderr
, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
787 if (RTA_PAYLOAD(tb
[XFRMA_SA
]) < sizeof(*xsinfo
)) {
788 fprintf(stderr
, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
791 xsinfo
= RTA_DATA(tb
[XFRMA_SA
]);
794 xfrm_state_info_print(xsinfo
, tb
, fp
, NULL
, NULL
);
796 if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
) {
798 fprintf(fp
, "hard %u", xexp
->hard
);
799 fprintf(fp
, "%s", _SL_
);
809 static int xfrm_state_get_or_delete(int argc
, char **argv
, int delete)
811 struct rtnl_handle rth
;
814 struct xfrm_usersa_id xsid
;
815 char buf
[RTA_BUF_SIZE
];
819 struct xfrm_mark mark
= {0, 0};
821 memset(&req
, 0, sizeof(req
));
823 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsid
));
824 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
825 req
.n
.nlmsg_type
= delete ? XFRM_MSG_DELSA
: XFRM_MSG_GETSA
;
826 req
.xsid
.family
= preferred_family
;
829 xfrm_address_t saddr
;
831 if (strcmp(*argv
, "mark") == 0) {
832 xfrm_parse_mark(&mark
, &argc
, &argv
);
835 invarg("unknown", *argv
);
839 memset(&id
, 0, sizeof(id
));
840 memset(&saddr
, 0, sizeof(saddr
));
841 xfrm_id_parse(&saddr
, &id
, &req
.xsid
.family
, 0,
844 memcpy(&req
.xsid
.daddr
, &id
.daddr
, sizeof(req
.xsid
.daddr
));
845 req
.xsid
.spi
= id
.spi
;
846 req
.xsid
.proto
= id
.proto
;
848 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_SRCADDR
,
849 (void *)&saddr
, sizeof(saddr
));
855 if (mark
.m
& mark
.v
) {
856 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
857 (void *)&mark
, sizeof(mark
));
859 fprintf(stderr
, "XFRMA_MARK failed\n");
864 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
867 if (req
.xsid
.family
== AF_UNSPEC
)
868 req
.xsid
.family
= AF_INET
;
871 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
) < 0)
874 char buf
[NLMSG_BUF_SIZE
];
875 struct nlmsghdr
*res_n
= (struct nlmsghdr
*)buf
;
877 memset(buf
, 0, sizeof(buf
));
879 if (rtnl_talk(&rth
, &req
.n
, 0, 0, res_n
) < 0)
882 if (xfrm_state_print(NULL
, res_n
, (void*)stdout
) < 0) {
883 fprintf(stderr
, "An error :-)\n");
894 * With an existing state of nlmsg, make new nlmsg for deleting the state
895 * and store it to buffer.
897 static int xfrm_state_keep(const struct sockaddr_nl
*who
,
901 struct xfrm_buffer
*xb
= (struct xfrm_buffer
*)arg
;
902 struct rtnl_handle
*rth
= xb
->rth
;
903 struct xfrm_usersa_info
*xsinfo
= NLMSG_DATA(n
);
904 int len
= n
->nlmsg_len
;
905 struct nlmsghdr
*new_n
;
906 struct xfrm_usersa_id
*xsid
;
908 if (n
->nlmsg_type
!= XFRM_MSG_NEWSA
) {
909 fprintf(stderr
, "Not a state: %08x %08x %08x\n",
910 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
914 len
-= NLMSG_LENGTH(sizeof(*xsinfo
));
916 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
920 if (!xfrm_state_filter_match(xsinfo
))
923 if (xb
->offset
> xb
->size
) {
924 fprintf(stderr
, "State buffer overflow\n");
928 new_n
= (struct nlmsghdr
*)(xb
->buf
+ xb
->offset
);
929 new_n
->nlmsg_len
= NLMSG_LENGTH(sizeof(*xsid
));
930 new_n
->nlmsg_flags
= NLM_F_REQUEST
;
931 new_n
->nlmsg_type
= XFRM_MSG_DELSA
;
932 new_n
->nlmsg_seq
= ++rth
->seq
;
934 xsid
= NLMSG_DATA(new_n
);
935 xsid
->family
= xsinfo
->family
;
936 memcpy(&xsid
->daddr
, &xsinfo
->id
.daddr
, sizeof(xsid
->daddr
));
937 xsid
->spi
= xsinfo
->id
.spi
;
938 xsid
->proto
= xsinfo
->id
.proto
;
940 addattr_l(new_n
, xb
->size
, XFRMA_SRCADDR
, &xsinfo
->saddr
,
941 sizeof(xsid
->daddr
));
943 xb
->offset
+= new_n
->nlmsg_len
;
949 static int xfrm_state_list_or_deleteall(int argc
, char **argv
, int deleteall
)
952 struct rtnl_handle rth
;
956 filter
.xsinfo
.family
= preferred_family
;
959 if (strcmp(*argv
, "mode") == 0) {
961 xfrm_mode_parse(&filter
.xsinfo
.mode
, &argc
, &argv
);
963 filter
.mode_mask
= XFRM_FILTER_MASK_FULL
;
965 } else if (strcmp(*argv
, "reqid") == 0) {
967 xfrm_reqid_parse(&filter
.xsinfo
.reqid
, &argc
, &argv
);
969 filter
.reqid_mask
= XFRM_FILTER_MASK_FULL
;
971 } else if (strcmp(*argv
, "flag") == 0) {
973 xfrm_state_flag_parse(&filter
.xsinfo
.flags
, &argc
, &argv
);
975 filter
.state_flags_mask
= XFRM_FILTER_MASK_FULL
;
979 invarg("unknown", *argv
);
983 xfrm_id_parse(&filter
.xsinfo
.saddr
, &filter
.xsinfo
.id
,
984 &filter
.xsinfo
.family
, 1, &argc
, &argv
);
985 if (preferred_family
== AF_UNSPEC
)
986 preferred_family
= filter
.xsinfo
.family
;
991 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
995 struct xfrm_buffer xb
;
996 char buf
[NLMSG_DELETEALL_BUF_SIZE
];
1000 xb
.size
= sizeof(buf
);
1003 for (i
= 0; ; i
++) {
1008 fprintf(stderr
, "Delete-all round = %d\n", i
);
1010 if (rtnl_wilddump_request(&rth
, preferred_family
, XFRM_MSG_GETSA
) < 0) {
1011 perror("Cannot send dump request");
1015 if (rtnl_dump_filter(&rth
, xfrm_state_keep
, &xb
) < 0) {
1016 fprintf(stderr
, "Delete-all terminated\n");
1019 if (xb
.nlmsg_count
== 0) {
1021 fprintf(stderr
, "Delete-all completed\n");
1025 if (rtnl_send_check(&rth
, xb
.buf
, xb
.offset
) < 0) {
1026 perror("Failed to send delete-all request\n");
1030 fprintf(stderr
, "Delete-all nlmsg count = %d\n", xb
.nlmsg_count
);
1037 if (rtnl_wilddump_request(&rth
, preferred_family
, XFRM_MSG_GETSA
) < 0) {
1038 perror("Cannot send dump request");
1042 if (rtnl_dump_filter(&rth
, xfrm_state_print
, stdout
) < 0) {
1043 fprintf(stderr
, "Dump terminated\n");
1053 static int print_sadinfo(struct nlmsghdr
*n
, void *arg
)
1055 FILE *fp
= (FILE*)arg
;
1056 __u32
*f
= NLMSG_DATA(n
);
1057 struct rtattr
*tb
[XFRMA_SAD_MAX
+1];
1061 int len
= n
->nlmsg_len
;
1063 len
-= NLMSG_LENGTH(sizeof(__u32
));
1065 fprintf(stderr
, "SADinfo: Wrong len %d\n", len
);
1069 rta
= XFRMSAPD_RTA(f
);
1070 parse_rtattr(tb
, XFRMA_SAD_MAX
, rta
, len
);
1072 if (tb
[XFRMA_SAD_CNT
]) {
1073 fprintf(fp
,"\t SAD");
1074 cnt
= (__u32
*)RTA_DATA(tb
[XFRMA_SAD_CNT
]);
1075 fprintf(fp
," count %d", *cnt
);
1077 fprintf(fp
,"BAD SAD info returned\n");
1082 if (tb
[XFRMA_SAD_HINFO
]) {
1083 struct xfrmu_sadhinfo
*si
;
1085 if (RTA_PAYLOAD(tb
[XFRMA_SAD_HINFO
]) < sizeof(*si
)) {
1086 fprintf(fp
,"BAD SAD length returned\n");
1090 si
= RTA_DATA(tb
[XFRMA_SAD_HINFO
]);
1091 fprintf(fp
," (buckets ");
1092 fprintf(fp
,"count %d", si
->sadhcnt
);
1093 fprintf(fp
," Max %d", si
->sadhmcnt
);
1102 static int xfrm_sad_getinfo(int argc
, char **argv
)
1104 struct rtnl_handle rth
;
1111 memset(&req
, 0, sizeof(req
));
1112 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.flags
));
1113 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1114 req
.n
.nlmsg_type
= XFRM_MSG_GETSADINFO
;
1115 req
.flags
= 0XFFFFFFFF;
1117 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1120 if (rtnl_talk(&rth
, &req
.n
, 0, 0, &req
.n
) < 0)
1123 print_sadinfo(&req
.n
, (void*)stdout
);
1130 static int xfrm_state_flush(int argc
, char **argv
)
1132 struct rtnl_handle rth
;
1135 struct xfrm_usersa_flush xsf
;
1137 char *protop
= NULL
;
1139 memset(&req
, 0, sizeof(req
));
1141 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsf
));
1142 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1143 req
.n
.nlmsg_type
= XFRM_MSG_FLUSHSA
;
1147 if (strcmp(*argv
, "proto") == 0) {
1151 duparg("proto", *argv
);
1156 ret
= xfrm_xfrmproto_getbyname(*argv
);
1158 invarg("\"XFRM-PROTO\" is invalid", *argv
);
1160 req
.xsf
.proto
= (__u8
)ret
;
1162 invarg("unknown", *argv
);
1167 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1171 fprintf(stderr
, "Flush state proto=%s\n",
1172 strxf_xfrmproto(req
.xsf
.proto
));
1174 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
) < 0)
1182 int do_xfrm_state(int argc
, char **argv
)
1185 return xfrm_state_list_or_deleteall(0, NULL
, 0);
1187 if (matches(*argv
, "add") == 0)
1188 return xfrm_state_modify(XFRM_MSG_NEWSA
, 0,
1190 if (matches(*argv
, "update") == 0)
1191 return xfrm_state_modify(XFRM_MSG_UPDSA
, 0,
1193 if (matches(*argv
, "allocspi") == 0)
1194 return xfrm_state_allocspi(argc
-1, argv
+1);
1195 if (matches(*argv
, "delete") == 0)
1196 return xfrm_state_get_or_delete(argc
-1, argv
+1, 1);
1197 if (matches(*argv
, "deleteall") == 0 || matches(*argv
, "delall") == 0)
1198 return xfrm_state_list_or_deleteall(argc
-1, argv
+1, 1);
1199 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
1200 || matches(*argv
, "lst") == 0)
1201 return xfrm_state_list_or_deleteall(argc
-1, argv
+1, 0);
1202 if (matches(*argv
, "get") == 0)
1203 return xfrm_state_get_or_delete(argc
-1, argv
+1, 0);
1204 if (matches(*argv
, "flush") == 0)
1205 return xfrm_state_flush(argc
-1, argv
+1);
1206 if (matches(*argv
, "count") == 0) {
1207 return xfrm_sad_getinfo(argc
, argv
);
1209 if (matches(*argv
, "help") == 0)
1211 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv
);