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
, " } ALGO-NAME ALGO-KEY |\n");
83 fprintf(stderr
, " %s", strxf_algotype(XFRMA_ALG_AUTH_TRUNC
));
84 fprintf(stderr
, " ALGO-NAME ALGO-KEY ALGO-TRUNC-LEN |\n");
85 fprintf(stderr
, " %s", strxf_algotype(XFRMA_ALG_AEAD
));
86 fprintf(stderr
, " ALGO-NAME ALGO-KEY ALGO-ICV-LEN |\n");
87 fprintf(stderr
, " %s", strxf_algotype(XFRMA_ALG_COMP
));
88 fprintf(stderr
, " ALGO-NAME\n");
89 fprintf(stderr
, "MODE := transport | tunnel | ro | in_trigger | beet\n");
90 fprintf(stderr
, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
91 fprintf(stderr
, "FLAG := noecn | decap-dscp | nopmtudisc | wildrecv | icmp | af-unspec | align4\n");
92 fprintf(stderr
, "SELECTOR := [ src ADDR[/PLEN] ] [ dst ADDR[/PLEN] ] [ dev DEV ] [ UPSPEC ]\n");
93 fprintf(stderr
, "UPSPEC := proto { { ");
94 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_TCP
));
95 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_UDP
));
96 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_SCTP
));
97 fprintf(stderr
, "%s", strxf_proto(IPPROTO_DCCP
));
98 fprintf(stderr
, " } [ sport PORT ] [ dport PORT ] |\n");
99 fprintf(stderr
, " { ");
100 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_ICMP
));
101 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_ICMPV6
));
102 fprintf(stderr
, "%s", strxf_proto(IPPROTO_MH
));
103 fprintf(stderr
, " } [ type NUMBER ] [ code NUMBER ] |\n");
104 fprintf(stderr
, " %s", strxf_proto(IPPROTO_GRE
));
105 fprintf(stderr
, " [ key { DOTTED-QUAD | NUMBER } ] | PROTO }\n");
106 fprintf(stderr
, "LIMIT-LIST := [ LIMIT-LIST ] limit LIMIT\n");
107 fprintf(stderr
, "LIMIT := { time-soft | time-hard | time-use-soft | time-use-hard } SECONDS |\n");
108 fprintf(stderr
, " { byte-soft | byte-hard } SIZE | { packet-soft | packet-hard } COUNT\n");
109 fprintf(stderr
, "ENCAP := { espinudp | espinudp-nonike } SPORT DPORT OADDR\n");
114 static int xfrm_algo_parse(struct xfrm_algo
*alg
, enum xfrm_attr_type_t type
,
115 char *name
, char *key
, char *buf
, int max
)
118 int slen
= strlen(key
);
121 /* XXX: verifying both name and key is required! */
122 fprintf(stderr
, "warning: ALGO-NAME/ALGO-KEY will send to kernel promiscuously! (verifying them isn't implemented yet)\n");
125 strncpy(alg
->alg_name
, name
, sizeof(alg
->alg_name
));
127 if (slen
> 2 && strncmp(key
, "0x", 2) == 0) {
128 /* split two chars "0x" from the top */
134 /* Converting hexadecimal numbered string into real key;
135 * Convert each two chars into one char(value). If number
136 * of the length is odd, add zero on the top for rounding.
139 /* calculate length of the converted values(real key) */
140 len
= (plen
+ 1) / 2;
142 invarg("\"ALGO-KEY\" makes buffer overflow\n", key
);
144 for (i
= - (plen
% 2), j
= 0; j
< len
; i
+= 2, j
++) {
148 vbuf
[0] = i
>= 0 ? p
[i
] : '0';
152 if (get_u8(&val
, vbuf
, 16))
153 invarg("\"ALGO-KEY\" is invalid", key
);
161 invarg("\"ALGO-KEY\" makes buffer overflow\n", key
);
163 strncpy(buf
, key
, len
);
167 alg
->alg_key_len
= len
* 8;
172 static int xfrm_seq_parse(__u32
*seq
, int *argcp
, char ***argvp
)
175 char **argv
= *argvp
;
177 if (get_u32(seq
, *argv
, 0))
178 invarg("\"SEQ\" is invalid", *argv
);
188 static int xfrm_state_flag_parse(__u8
*flags
, int *argcp
, char ***argvp
)
191 char **argv
= *argvp
;
192 int len
= strlen(*argv
);
194 if (len
> 2 && strncmp(*argv
, "0x", 2) == 0) {
197 if (get_u8(&val
, *argv
, 16))
198 invarg("\"FLAG\" is invalid", *argv
);
202 if (strcmp(*argv
, "noecn") == 0)
203 *flags
|= XFRM_STATE_NOECN
;
204 else if (strcmp(*argv
, "decap-dscp") == 0)
205 *flags
|= XFRM_STATE_DECAP_DSCP
;
206 else if (strcmp(*argv
, "nopmtudisc") == 0)
207 *flags
|= XFRM_STATE_NOPMTUDISC
;
208 else if (strcmp(*argv
, "wildrecv") == 0)
209 *flags
|= XFRM_STATE_WILDRECV
;
210 else if (strcmp(*argv
, "icmp") == 0)
211 *flags
|= XFRM_STATE_ICMP
;
212 else if (strcmp(*argv
, "af-unspec") == 0)
213 *flags
|= XFRM_STATE_AF_UNSPEC
;
214 else if (strcmp(*argv
, "align4") == 0)
215 *flags
|= XFRM_STATE_ALIGN4
;
217 PREV_ARG(); /* back track */
233 static int xfrm_state_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
235 struct rtnl_handle rth
;
238 struct xfrm_usersa_info xsinfo
;
239 char buf
[RTA_BUF_SIZE
];
241 struct xfrm_replay_state replay
;
249 struct xfrm_mark mark
= {0, 0};
251 struct xfrm_user_sec_ctx sctx
;
252 char str
[CTX_BUF_SIZE
];
255 memset(&req
, 0, sizeof(req
));
256 memset(&replay
, 0, sizeof(replay
));
257 memset(&ctx
, 0, sizeof(ctx
));
259 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsinfo
));
260 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
261 req
.n
.nlmsg_type
= cmd
;
262 req
.xsinfo
.family
= preferred_family
;
264 req
.xsinfo
.lft
.soft_byte_limit
= XFRM_INF
;
265 req
.xsinfo
.lft
.hard_byte_limit
= XFRM_INF
;
266 req
.xsinfo
.lft
.soft_packet_limit
= XFRM_INF
;
267 req
.xsinfo
.lft
.hard_packet_limit
= XFRM_INF
;
270 if (strcmp(*argv
, "mode") == 0) {
272 xfrm_mode_parse(&req
.xsinfo
.mode
, &argc
, &argv
);
273 } else if (strcmp(*argv
, "mark") == 0) {
274 xfrm_parse_mark(&mark
, &argc
, &argv
);
275 } else if (strcmp(*argv
, "reqid") == 0) {
277 xfrm_reqid_parse(&req
.xsinfo
.reqid
, &argc
, &argv
);
278 } else if (strcmp(*argv
, "seq") == 0) {
280 xfrm_seq_parse(&req
.xsinfo
.seq
, &argc
, &argv
);
281 } else if (strcmp(*argv
, "replay-window") == 0) {
283 if (get_u8(&req
.xsinfo
.replay_window
, *argv
, 0))
284 invarg("\"replay-window\" value is invalid", *argv
);
285 } else if (strcmp(*argv
, "replay-seq") == 0) {
287 if (get_u32(&replay
.seq
, *argv
, 0))
288 invarg("\"replay-seq\" value is invalid", *argv
);
289 } else if (strcmp(*argv
, "replay-oseq") == 0) {
291 if (get_u32(&replay
.oseq
, *argv
, 0))
292 invarg("\"replay-oseq\" value is invalid", *argv
);
293 } else if (strcmp(*argv
, "flag") == 0) {
295 xfrm_state_flag_parse(&req
.xsinfo
.flags
, &argc
, &argv
);
296 } else if (strcmp(*argv
, "sel") == 0) {
298 preferred_family
= AF_UNSPEC
;
299 xfrm_selector_parse(&req
.xsinfo
.sel
, &argc
, &argv
);
300 preferred_family
= req
.xsinfo
.sel
.family
;
301 } else if (strcmp(*argv
, "limit") == 0) {
303 xfrm_lifetime_cfg_parse(&req
.xsinfo
.lft
, &argc
, &argv
);
304 } else if (strcmp(*argv
, "encap") == 0) {
305 struct xfrm_encap_tmpl encap
;
308 xfrm_encap_type_parse(&encap
.encap_type
, &argc
, &argv
);
310 if (get_u16(&encap
.encap_sport
, *argv
, 0))
311 invarg("\"encap\" sport value is invalid", *argv
);
312 encap
.encap_sport
= htons(encap
.encap_sport
);
314 if (get_u16(&encap
.encap_dport
, *argv
, 0))
315 invarg("\"encap\" dport value is invalid", *argv
);
316 encap
.encap_dport
= htons(encap
.encap_dport
);
318 get_addr(&oa
, *argv
, AF_UNSPEC
);
319 memcpy(&encap
.encap_oa
, &oa
.data
, sizeof(encap
.encap_oa
));
320 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_ENCAP
,
321 (void *)&encap
, sizeof(encap
));
322 } else if (strcmp(*argv
, "coa") == 0) {
327 duparg("coa", *argv
);
332 get_prefix(&coa
, *argv
, preferred_family
);
333 if (coa
.family
== AF_UNSPEC
)
334 invarg("\"coa\" address family is AF_UNSPEC", *argv
);
335 if (coa
.bytelen
> sizeof(xcoa
))
336 invarg("\"coa\" address length is too large", *argv
);
338 memset(&xcoa
, 0, sizeof(xcoa
));
339 memcpy(&xcoa
, &coa
.data
, coa
.bytelen
);
341 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_COADDR
,
342 (void *)&xcoa
, sizeof(xcoa
));
343 } else if (strcmp(*argv
, "ctx") == 0) {
347 duparg("ctx", *argv
);
353 xfrm_sctx_parse((char *)&ctx
.str
, context
, &ctx
.sctx
);
354 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_SEC_CTX
,
355 (void *)&ctx
, ctx
.sctx
.len
);
357 /* try to assume ALGO */
358 int type
= xfrm_algotype_getbyname(*argv
);
361 case XFRMA_ALG_CRYPT
:
363 case XFRMA_ALG_AUTH_TRUNC
:
369 struct xfrm_algo alg
;
370 struct xfrm_algo_aead aead
;
371 struct xfrm_algo_auth auth
;
373 char buf
[XFRM_ALGO_KEY_BUF_SIZE
];
376 __u32 icvlen
, trunclen
;
383 if (ealgop
|| aalgop
|| aeadop
)
384 duparg("ALGO-TYPE", *argv
);
387 case XFRMA_ALG_CRYPT
:
388 if (ealgop
|| aeadop
)
389 duparg("ALGO-TYPE", *argv
);
393 case XFRMA_ALG_AUTH_TRUNC
:
394 if (aalgop
|| aeadop
)
395 duparg("ALGO-TYPE", *argv
);
400 duparg("ALGO-TYPE", *argv
);
405 invarg("\"ALGO-TYPE\" is invalid\n", *argv
);
409 missarg("ALGO-NAME");
415 case XFRMA_ALG_CRYPT
:
417 case XFRMA_ALG_AUTH_TRUNC
:
425 buf
= alg
.u
.alg
.alg_key
;
426 len
= sizeof(alg
.u
.alg
);
431 missarg("ALGO-ICV-LEN");
433 if (get_u32(&icvlen
, *argv
, 0))
434 invarg("\"aead\" ICV length is invalid",
436 alg
.u
.aead
.alg_icv_len
= icvlen
;
438 buf
= alg
.u
.aead
.alg_key
;
439 len
= sizeof(alg
.u
.aead
);
441 case XFRMA_ALG_AUTH_TRUNC
:
443 missarg("ALGO-TRUNC-LEN");
445 if (get_u32(&trunclen
, *argv
, 0))
446 invarg("\"auth\" trunc length is invalid",
448 alg
.u
.auth
.alg_trunc_len
= trunclen
;
450 buf
= alg
.u
.auth
.alg_key
;
451 len
= sizeof(alg
.u
.auth
);
455 xfrm_algo_parse((void *)&alg
, type
, name
, key
,
456 buf
, sizeof(alg
.buf
));
457 len
+= alg
.u
.alg
.alg_key_len
;
459 addattr_l(&req
.n
, sizeof(req
.buf
), type
,
464 /* try to assume ID */
466 invarg("unknown", *argv
);
470 xfrm_id_parse(&req
.xsinfo
.saddr
, &req
.xsinfo
.id
,
471 &req
.xsinfo
.family
, 0, &argc
, &argv
);
472 if (preferred_family
== AF_UNSPEC
)
473 preferred_family
= req
.xsinfo
.family
;
479 if (replay
.seq
|| replay
.oseq
)
480 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_REPLAY_VAL
,
481 (void *)&replay
, sizeof(replay
));
484 fprintf(stderr
, "Not enough information: \"ID\" is required\n");
488 if (mark
.m
& mark
.v
) {
489 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
490 (void *)&mark
, sizeof(mark
));
492 fprintf(stderr
, "XFRMA_MARK failed\n");
497 if (xfrm_xfrmproto_is_ipsec(req
.xsinfo
.id
.proto
)) {
498 switch (req
.xsinfo
.mode
) {
499 case XFRM_MODE_TRANSPORT
:
500 case XFRM_MODE_TUNNEL
:
503 if (req
.xsinfo
.id
.proto
== IPPROTO_ESP
)
506 fprintf(stderr
, "MODE value is invalid with XFRM-PROTO value \"%s\"\n",
507 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
511 switch (req
.xsinfo
.id
.proto
) {
514 fprintf(stderr
, "ALGO-TYPE value \"%s\" is invalid with XFRM-PROTO value \"%s\"\n",
515 strxf_algotype(XFRMA_ALG_COMP
),
516 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
519 if (!ealgop
&& !aeadop
) {
520 fprintf(stderr
, "ALGO-TYPE value \"%s\" or \"%s\" is required with XFRM-PROTO value \"%s\"\n",
521 strxf_algotype(XFRMA_ALG_CRYPT
),
522 strxf_algotype(XFRMA_ALG_AEAD
),
523 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
528 if (ealgop
|| aeadop
|| calgop
) {
529 fprintf(stderr
, "ALGO-TYPE values \"%s\", \"%s\", and \"%s\" are invalid with XFRM-PROTO value \"%s\"\n",
530 strxf_algotype(XFRMA_ALG_CRYPT
),
531 strxf_algotype(XFRMA_ALG_AEAD
),
532 strxf_algotype(XFRMA_ALG_COMP
),
533 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
537 fprintf(stderr
, "ALGO-TYPE value \"%s\" or \"%s\" is required with XFRM-PROTO value \"%s\"\n",
538 strxf_algotype(XFRMA_ALG_AUTH
),
539 strxf_algotype(XFRMA_ALG_AUTH_TRUNC
),
540 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
545 if (ealgop
|| aalgop
|| aeadop
) {
546 fprintf(stderr
, "ALGO-TYPE values \"%s\", \"%s\", \"%s\", and \"%s\" are invalid with XFRM-PROTO value \"%s\"\n",
547 strxf_algotype(XFRMA_ALG_CRYPT
),
548 strxf_algotype(XFRMA_ALG_AUTH
),
549 strxf_algotype(XFRMA_ALG_AUTH_TRUNC
),
550 strxf_algotype(XFRMA_ALG_AEAD
),
551 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
555 fprintf(stderr
, "ALGO-TYPE value \"%s\" is required with XFRM-PROTO value \"%s\"\n",
556 strxf_algotype(XFRMA_ALG_COMP
),
557 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
563 if (ealgop
|| aalgop
|| aeadop
|| calgop
) {
564 fprintf(stderr
, "ALGO is invalid with XFRM-PROTO value \"%s\"\n",
565 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
570 if (xfrm_xfrmproto_is_ro(req
.xsinfo
.id
.proto
)) {
571 switch (req
.xsinfo
.mode
) {
572 case XFRM_MODE_ROUTEOPTIMIZATION
:
573 case XFRM_MODE_IN_TRIGGER
:
576 fprintf(stderr
, "\"mode\" is required with XFRM-PROTO value \"%s\"\n",
577 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
580 fprintf(stderr
, "MODE value is invalid with XFRM-PROTO value \"%s\"\n",
581 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
586 fprintf(stderr
, "\"coa\" is required with XFRM-PROTO value \"%s\"\n",
587 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
592 fprintf(stderr
, "\"coa\" is invalid with XFRM-PROTO value \"%s\"\n",
593 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
598 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
601 if (req
.xsinfo
.family
== AF_UNSPEC
)
602 req
.xsinfo
.family
= AF_INET
;
604 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
) < 0)
612 static int xfrm_state_allocspi(int argc
, char **argv
)
614 struct rtnl_handle rth
;
617 struct xfrm_userspi_info xspi
;
618 char buf
[RTA_BUF_SIZE
];
623 struct xfrm_mark mark
= {0, 0};
624 char res_buf
[NLMSG_BUF_SIZE
];
625 struct nlmsghdr
*res_n
= (struct nlmsghdr
*)res_buf
;
627 memset(res_buf
, 0, sizeof(res_buf
));
629 memset(&req
, 0, sizeof(req
));
631 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xspi
));
632 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
633 req
.n
.nlmsg_type
= XFRM_MSG_ALLOCSPI
;
634 req
.xspi
.info
.family
= preferred_family
;
637 req
.xsinfo
.lft
.soft_byte_limit
= XFRM_INF
;
638 req
.xsinfo
.lft
.hard_byte_limit
= XFRM_INF
;
639 req
.xsinfo
.lft
.soft_packet_limit
= XFRM_INF
;
640 req
.xsinfo
.lft
.hard_packet_limit
= XFRM_INF
;
644 if (strcmp(*argv
, "mode") == 0) {
646 xfrm_mode_parse(&req
.xspi
.info
.mode
, &argc
, &argv
);
647 } else if (strcmp(*argv
, "mark") == 0) {
648 xfrm_parse_mark(&mark
, &argc
, &argv
);
649 } else if (strcmp(*argv
, "reqid") == 0) {
651 xfrm_reqid_parse(&req
.xspi
.info
.reqid
, &argc
, &argv
);
652 } else if (strcmp(*argv
, "seq") == 0) {
654 xfrm_seq_parse(&req
.xspi
.info
.seq
, &argc
, &argv
);
655 } else if (strcmp(*argv
, "min") == 0) {
657 duparg("min", *argv
);
662 if (get_u32(&req
.xspi
.min
, *argv
, 0))
663 invarg("\"min\" value is invalid", *argv
);
664 } else if (strcmp(*argv
, "max") == 0) {
666 duparg("max", *argv
);
671 if (get_u32(&req
.xspi
.max
, *argv
, 0))
672 invarg("\"max\" value is invalid", *argv
);
674 /* try to assume ID */
676 invarg("unknown", *argv
);
680 xfrm_id_parse(&req
.xspi
.info
.saddr
, &req
.xspi
.info
.id
,
681 &req
.xspi
.info
.family
, 0, &argc
, &argv
);
682 if (req
.xspi
.info
.id
.spi
) {
683 fprintf(stderr
, "\"SPI\" must be zero\n");
686 if (preferred_family
== AF_UNSPEC
)
687 preferred_family
= req
.xspi
.info
.family
;
693 fprintf(stderr
, "Not enough information: \"ID\" is required\n");
699 fprintf(stderr
, "\"max\" is missing\n");
702 if (req
.xspi
.min
> req
.xspi
.max
) {
703 fprintf(stderr
, "\"min\" value is larger than \"max\" value\n");
708 fprintf(stderr
, "\"min\" is missing\n");
712 /* XXX: Default value defined in PF_KEY;
713 * See kernel's net/key/af_key.c(pfkey_getspi).
715 req
.xspi
.min
= 0x100;
716 req
.xspi
.max
= 0x0fffffff;
718 /* XXX: IPCOMP spi is 16-bits;
719 * See kernel's net/xfrm/xfrm_user(verify_userspi_info).
721 if (req
.xspi
.info
.id
.proto
== IPPROTO_COMP
)
722 req
.xspi
.max
= 0xffff;
725 if (mark
.m
& mark
.v
) {
726 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
727 (void *)&mark
, sizeof(mark
));
729 fprintf(stderr
, "XFRMA_MARK failed\n");
734 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
737 if (req
.xspi
.info
.family
== AF_UNSPEC
)
738 req
.xspi
.info
.family
= AF_INET
;
741 if (rtnl_talk(&rth
, &req
.n
, 0, 0, res_n
) < 0)
744 if (xfrm_state_print(NULL
, res_n
, (void*)stdout
) < 0) {
745 fprintf(stderr
, "An error :-)\n");
754 static int xfrm_state_filter_match(struct xfrm_usersa_info
*xsinfo
)
759 if (filter
.id_src_mask
)
760 if (xfrm_addr_match(&xsinfo
->saddr
, &filter
.xsinfo
.saddr
,
763 if (filter
.id_dst_mask
)
764 if (xfrm_addr_match(&xsinfo
->id
.daddr
, &filter
.xsinfo
.id
.daddr
,
767 if ((xsinfo
->id
.proto
^filter
.xsinfo
.id
.proto
)&filter
.id_proto_mask
)
769 if ((xsinfo
->id
.spi
^filter
.xsinfo
.id
.spi
)&filter
.id_spi_mask
)
771 if ((xsinfo
->mode
^filter
.xsinfo
.mode
)&filter
.mode_mask
)
773 if ((xsinfo
->reqid
^filter
.xsinfo
.reqid
)&filter
.reqid_mask
)
775 if (filter
.state_flags_mask
)
776 if ((xsinfo
->flags
& filter
.xsinfo
.flags
) == 0)
782 int xfrm_state_print(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
785 FILE *fp
= (FILE*)arg
;
786 struct rtattr
* tb
[XFRMA_MAX
+1];
788 struct xfrm_usersa_info
*xsinfo
= NULL
;
789 struct xfrm_user_expire
*xexp
= NULL
;
790 struct xfrm_usersa_id
*xsid
= NULL
;
791 int len
= n
->nlmsg_len
;
793 if (n
->nlmsg_type
!= XFRM_MSG_NEWSA
&&
794 n
->nlmsg_type
!= XFRM_MSG_DELSA
&&
795 n
->nlmsg_type
!= XFRM_MSG_UPDSA
&&
796 n
->nlmsg_type
!= XFRM_MSG_EXPIRE
) {
797 fprintf(stderr
, "Not a state: %08x %08x %08x\n",
798 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
802 if (n
->nlmsg_type
== XFRM_MSG_DELSA
) {
803 /* Dont blame me for this .. Herbert made me do it */
804 xsid
= NLMSG_DATA(n
);
805 len
-= NLMSG_SPACE(sizeof(*xsid
));
806 } else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
) {
807 xexp
= NLMSG_DATA(n
);
808 xsinfo
= &xexp
->state
;
809 len
-= NLMSG_SPACE(sizeof(*xexp
));
812 xsinfo
= NLMSG_DATA(n
);
813 len
-= NLMSG_SPACE(sizeof(*xsinfo
));
817 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
821 if (xsinfo
&& !xfrm_state_filter_match(xsinfo
))
824 if (n
->nlmsg_type
== XFRM_MSG_DELSA
)
825 fprintf(fp
, "Deleted ");
826 else if (n
->nlmsg_type
== XFRM_MSG_UPDSA
)
827 fprintf(fp
, "Updated ");
828 else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
)
829 fprintf(fp
, "Expired ");
831 if (n
->nlmsg_type
== XFRM_MSG_DELSA
)
832 rta
= XFRMSID_RTA(xsid
);
833 else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
)
834 rta
= XFRMEXP_RTA(xexp
);
836 rta
= XFRMS_RTA(xsinfo
);
838 parse_rtattr(tb
, XFRMA_MAX
, rta
, len
);
840 if (n
->nlmsg_type
== XFRM_MSG_DELSA
) {
841 //xfrm_policy_id_print();
844 fprintf(stderr
, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
847 if (RTA_PAYLOAD(tb
[XFRMA_SA
]) < sizeof(*xsinfo
)) {
848 fprintf(stderr
, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
851 xsinfo
= RTA_DATA(tb
[XFRMA_SA
]);
854 xfrm_state_info_print(xsinfo
, tb
, fp
, NULL
, NULL
);
856 if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
) {
858 fprintf(fp
, "hard %u", xexp
->hard
);
859 fprintf(fp
, "%s", _SL_
);
869 static int xfrm_state_get_or_delete(int argc
, char **argv
, int delete)
871 struct rtnl_handle rth
;
874 struct xfrm_usersa_id xsid
;
875 char buf
[RTA_BUF_SIZE
];
879 struct xfrm_mark mark
= {0, 0};
881 memset(&req
, 0, sizeof(req
));
883 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsid
));
884 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
885 req
.n
.nlmsg_type
= delete ? XFRM_MSG_DELSA
: XFRM_MSG_GETSA
;
886 req
.xsid
.family
= preferred_family
;
889 xfrm_address_t saddr
;
891 if (strcmp(*argv
, "mark") == 0) {
892 xfrm_parse_mark(&mark
, &argc
, &argv
);
895 invarg("unknown", *argv
);
899 memset(&id
, 0, sizeof(id
));
900 memset(&saddr
, 0, sizeof(saddr
));
901 xfrm_id_parse(&saddr
, &id
, &req
.xsid
.family
, 0,
904 memcpy(&req
.xsid
.daddr
, &id
.daddr
, sizeof(req
.xsid
.daddr
));
905 req
.xsid
.spi
= id
.spi
;
906 req
.xsid
.proto
= id
.proto
;
908 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_SRCADDR
,
909 (void *)&saddr
, sizeof(saddr
));
915 if (mark
.m
& mark
.v
) {
916 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
917 (void *)&mark
, sizeof(mark
));
919 fprintf(stderr
, "XFRMA_MARK failed\n");
924 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
927 if (req
.xsid
.family
== AF_UNSPEC
)
928 req
.xsid
.family
= AF_INET
;
931 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
) < 0)
934 char buf
[NLMSG_BUF_SIZE
];
935 struct nlmsghdr
*res_n
= (struct nlmsghdr
*)buf
;
937 memset(buf
, 0, sizeof(buf
));
939 if (rtnl_talk(&rth
, &req
.n
, 0, 0, res_n
) < 0)
942 if (xfrm_state_print(NULL
, res_n
, (void*)stdout
) < 0) {
943 fprintf(stderr
, "An error :-)\n");
954 * With an existing state of nlmsg, make new nlmsg for deleting the state
955 * and store it to buffer.
957 static int xfrm_state_keep(const struct sockaddr_nl
*who
,
961 struct xfrm_buffer
*xb
= (struct xfrm_buffer
*)arg
;
962 struct rtnl_handle
*rth
= xb
->rth
;
963 struct xfrm_usersa_info
*xsinfo
= NLMSG_DATA(n
);
964 int len
= n
->nlmsg_len
;
965 struct nlmsghdr
*new_n
;
966 struct xfrm_usersa_id
*xsid
;
968 if (n
->nlmsg_type
!= XFRM_MSG_NEWSA
) {
969 fprintf(stderr
, "Not a state: %08x %08x %08x\n",
970 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
974 len
-= NLMSG_LENGTH(sizeof(*xsinfo
));
976 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
980 if (!xfrm_state_filter_match(xsinfo
))
983 if (xb
->offset
> xb
->size
) {
984 fprintf(stderr
, "State buffer overflow\n");
988 new_n
= (struct nlmsghdr
*)(xb
->buf
+ xb
->offset
);
989 new_n
->nlmsg_len
= NLMSG_LENGTH(sizeof(*xsid
));
990 new_n
->nlmsg_flags
= NLM_F_REQUEST
;
991 new_n
->nlmsg_type
= XFRM_MSG_DELSA
;
992 new_n
->nlmsg_seq
= ++rth
->seq
;
994 xsid
= NLMSG_DATA(new_n
);
995 xsid
->family
= xsinfo
->family
;
996 memcpy(&xsid
->daddr
, &xsinfo
->id
.daddr
, sizeof(xsid
->daddr
));
997 xsid
->spi
= xsinfo
->id
.spi
;
998 xsid
->proto
= xsinfo
->id
.proto
;
1000 addattr_l(new_n
, xb
->size
, XFRMA_SRCADDR
, &xsinfo
->saddr
,
1001 sizeof(xsid
->daddr
));
1003 xb
->offset
+= new_n
->nlmsg_len
;
1009 static int xfrm_state_list_or_deleteall(int argc
, char **argv
, int deleteall
)
1012 struct rtnl_handle rth
;
1016 filter
.xsinfo
.family
= preferred_family
;
1019 if (strcmp(*argv
, "mode") == 0) {
1021 xfrm_mode_parse(&filter
.xsinfo
.mode
, &argc
, &argv
);
1023 filter
.mode_mask
= XFRM_FILTER_MASK_FULL
;
1025 } else if (strcmp(*argv
, "reqid") == 0) {
1027 xfrm_reqid_parse(&filter
.xsinfo
.reqid
, &argc
, &argv
);
1029 filter
.reqid_mask
= XFRM_FILTER_MASK_FULL
;
1031 } else if (strcmp(*argv
, "flag") == 0) {
1033 xfrm_state_flag_parse(&filter
.xsinfo
.flags
, &argc
, &argv
);
1035 filter
.state_flags_mask
= XFRM_FILTER_MASK_FULL
;
1039 invarg("unknown", *argv
);
1043 xfrm_id_parse(&filter
.xsinfo
.saddr
, &filter
.xsinfo
.id
,
1044 &filter
.xsinfo
.family
, 1, &argc
, &argv
);
1045 if (preferred_family
== AF_UNSPEC
)
1046 preferred_family
= filter
.xsinfo
.family
;
1051 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1055 struct xfrm_buffer xb
;
1056 char buf
[NLMSG_DELETEALL_BUF_SIZE
];
1060 xb
.size
= sizeof(buf
);
1063 for (i
= 0; ; i
++) {
1068 fprintf(stderr
, "Delete-all round = %d\n", i
);
1070 if (rtnl_wilddump_request(&rth
, preferred_family
, XFRM_MSG_GETSA
) < 0) {
1071 perror("Cannot send dump request");
1075 if (rtnl_dump_filter(&rth
, xfrm_state_keep
, &xb
) < 0) {
1076 fprintf(stderr
, "Delete-all terminated\n");
1079 if (xb
.nlmsg_count
== 0) {
1081 fprintf(stderr
, "Delete-all completed\n");
1085 if (rtnl_send_check(&rth
, xb
.buf
, xb
.offset
) < 0) {
1086 perror("Failed to send delete-all request\n");
1090 fprintf(stderr
, "Delete-all nlmsg count = %d\n", xb
.nlmsg_count
);
1097 if (rtnl_wilddump_request(&rth
, preferred_family
, XFRM_MSG_GETSA
) < 0) {
1098 perror("Cannot send dump request");
1102 if (rtnl_dump_filter(&rth
, xfrm_state_print
, stdout
) < 0) {
1103 fprintf(stderr
, "Dump terminated\n");
1113 static int print_sadinfo(struct nlmsghdr
*n
, void *arg
)
1115 FILE *fp
= (FILE*)arg
;
1116 __u32
*f
= NLMSG_DATA(n
);
1117 struct rtattr
*tb
[XFRMA_SAD_MAX
+1];
1121 int len
= n
->nlmsg_len
;
1123 len
-= NLMSG_LENGTH(sizeof(__u32
));
1125 fprintf(stderr
, "SADinfo: Wrong len %d\n", len
);
1129 rta
= XFRMSAPD_RTA(f
);
1130 parse_rtattr(tb
, XFRMA_SAD_MAX
, rta
, len
);
1132 if (tb
[XFRMA_SAD_CNT
]) {
1133 fprintf(fp
,"\t SAD");
1134 cnt
= (__u32
*)RTA_DATA(tb
[XFRMA_SAD_CNT
]);
1135 fprintf(fp
," count %d", *cnt
);
1137 fprintf(fp
,"BAD SAD info returned\n");
1142 if (tb
[XFRMA_SAD_HINFO
]) {
1143 struct xfrmu_sadhinfo
*si
;
1145 if (RTA_PAYLOAD(tb
[XFRMA_SAD_HINFO
]) < sizeof(*si
)) {
1146 fprintf(fp
,"BAD SAD length returned\n");
1150 si
= RTA_DATA(tb
[XFRMA_SAD_HINFO
]);
1151 fprintf(fp
," (buckets ");
1152 fprintf(fp
,"count %d", si
->sadhcnt
);
1153 fprintf(fp
," Max %d", si
->sadhmcnt
);
1162 static int xfrm_sad_getinfo(int argc
, char **argv
)
1164 struct rtnl_handle rth
;
1171 memset(&req
, 0, sizeof(req
));
1172 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.flags
));
1173 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1174 req
.n
.nlmsg_type
= XFRM_MSG_GETSADINFO
;
1175 req
.flags
= 0XFFFFFFFF;
1177 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1180 if (rtnl_talk(&rth
, &req
.n
, 0, 0, &req
.n
) < 0)
1183 print_sadinfo(&req
.n
, (void*)stdout
);
1190 static int xfrm_state_flush(int argc
, char **argv
)
1192 struct rtnl_handle rth
;
1195 struct xfrm_usersa_flush xsf
;
1197 char *protop
= NULL
;
1199 memset(&req
, 0, sizeof(req
));
1201 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsf
));
1202 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1203 req
.n
.nlmsg_type
= XFRM_MSG_FLUSHSA
;
1207 if (strcmp(*argv
, "proto") == 0) {
1211 duparg("proto", *argv
);
1216 ret
= xfrm_xfrmproto_getbyname(*argv
);
1218 invarg("\"XFRM-PROTO\" is invalid", *argv
);
1220 req
.xsf
.proto
= (__u8
)ret
;
1222 invarg("unknown", *argv
);
1227 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1231 fprintf(stderr
, "Flush state proto=%s\n",
1232 strxf_xfrmproto(req
.xsf
.proto
));
1234 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
) < 0)
1242 int do_xfrm_state(int argc
, char **argv
)
1245 return xfrm_state_list_or_deleteall(0, NULL
, 0);
1247 if (matches(*argv
, "add") == 0)
1248 return xfrm_state_modify(XFRM_MSG_NEWSA
, 0,
1250 if (matches(*argv
, "update") == 0)
1251 return xfrm_state_modify(XFRM_MSG_UPDSA
, 0,
1253 if (matches(*argv
, "allocspi") == 0)
1254 return xfrm_state_allocspi(argc
-1, argv
+1);
1255 if (matches(*argv
, "delete") == 0)
1256 return xfrm_state_get_or_delete(argc
-1, argv
+1, 1);
1257 if (matches(*argv
, "deleteall") == 0 || matches(*argv
, "delall") == 0)
1258 return xfrm_state_list_or_deleteall(argc
-1, argv
+1, 1);
1259 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
1260 || matches(*argv
, "lst") == 0)
1261 return xfrm_state_list_or_deleteall(argc
-1, argv
+1, 0);
1262 if (matches(*argv
, "get") == 0)
1263 return xfrm_state_get_or_delete(argc
-1, argv
+1, 0);
1264 if (matches(*argv
, "flush") == 0)
1265 return xfrm_state_flush(argc
-1, argv
+1);
1266 if (matches(*argv
, "count") == 0) {
1267 return xfrm_sad_getinfo(argc
, argv
);
1269 if (matches(*argv
, "help") == 0)
1271 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv
);