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/xfrm.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_usersa_info
45 * ... (max count of rtattr is XFRM_MAX+1
47 * each rtattr data = struct xfrm_algo(dynamic size) or xfrm_address_t
49 #define NLMSG_BUF_SIZE 4096
50 #define RTA_BUF_SIZE 2048
51 #define XFRM_ALGO_KEY_BUF_SIZE 512
52 #define CTX_BUF_SIZE 256
54 static void usage(void) __attribute__((noreturn
));
56 static void usage(void)
58 fprintf(stderr
, "Usage: ip xfrm state { add | update } ID [ ALGO-LIST ] [ mode MODE ]\n");
59 fprintf(stderr
, " [ mark MARK [ mask MASK ] ] [ reqid REQID ] [ seq SEQ ]\n");
60 fprintf(stderr
, " [ replay-window SIZE ] [ replay-seq SEQ ] [ replay-oseq SEQ ]\n");
61 fprintf(stderr
, " [ flag FLAG-LIST ] [ sel SELECTOR ] [ LIMIT-LIST ] [ encap ENCAP ]\n");
62 fprintf(stderr
, " [ coa ADDR[/PLEN] ] [ ctx CTX ] [ extra-flag EXTRA-FLAG-LIST ]\n");
63 fprintf(stderr
, "Usage: ip xfrm state allocspi ID [ mode MODE ] [ mark MARK [ mask MASK ] ]\n");
64 fprintf(stderr
, " [ reqid REQID ] [ seq SEQ ] [ min SPI max SPI ]\n");
65 fprintf(stderr
, "Usage: ip xfrm state { delete | get } ID [ mark MARK [ mask MASK ] ]\n");
66 fprintf(stderr
, "Usage: ip xfrm state { deleteall | list } [ ID ] [ mode MODE ] [ reqid REQID ]\n");
67 fprintf(stderr
, " [ flag FLAG-LIST ]\n");
68 fprintf(stderr
, "Usage: ip xfrm state flush [ proto XFRM-PROTO ]\n");
69 fprintf(stderr
, "Usage: ip xfrm state count\n");
70 fprintf(stderr
, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ]\n");
71 fprintf(stderr
, "XFRM-PROTO := ");
72 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_ESP
));
73 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_AH
));
74 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_COMP
));
75 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING
));
76 fprintf(stderr
, "%s\n", strxf_xfrmproto(IPPROTO_DSTOPTS
));
77 fprintf(stderr
, "ALGO-LIST := [ ALGO-LIST ] ALGO\n");
78 fprintf(stderr
, "ALGO := { ");
79 fprintf(stderr
, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT
));
80 fprintf(stderr
, "%s", strxf_algotype(XFRMA_ALG_AUTH
));
81 fprintf(stderr
, " } ALGO-NAME ALGO-KEYMAT |\n");
82 fprintf(stderr
, " %s", strxf_algotype(XFRMA_ALG_AUTH_TRUNC
));
83 fprintf(stderr
, " ALGO-NAME ALGO-KEYMAT ALGO-TRUNC-LEN |\n");
84 fprintf(stderr
, " %s", strxf_algotype(XFRMA_ALG_AEAD
));
85 fprintf(stderr
, " ALGO-NAME ALGO-KEYMAT ALGO-ICV-LEN |\n");
86 fprintf(stderr
, " %s", strxf_algotype(XFRMA_ALG_COMP
));
87 fprintf(stderr
, " ALGO-NAME\n");
88 fprintf(stderr
, "MODE := transport | tunnel | beet | ro | in_trigger\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
, "EXTRA-FLAG-LIST := [ EXTRA-FLAG-LIST ] EXTRA-FLAG\n");
92 fprintf(stderr
, "EXTRA-FLAG := dont-encap-dscp\n");
93 fprintf(stderr
, "SELECTOR := [ src ADDR[/PLEN] ] [ dst ADDR[/PLEN] ] [ dev DEV ] [ UPSPEC ]\n");
94 fprintf(stderr
, "UPSPEC := proto { { ");
95 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_TCP
));
96 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_UDP
));
97 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_SCTP
));
98 fprintf(stderr
, "%s", strxf_proto(IPPROTO_DCCP
));
99 fprintf(stderr
, " } [ sport PORT ] [ dport PORT ] |\n");
100 fprintf(stderr
, " { ");
101 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_ICMP
));
102 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_ICMPV6
));
103 fprintf(stderr
, "%s", strxf_proto(IPPROTO_MH
));
104 fprintf(stderr
, " } [ type NUMBER ] [ code NUMBER ] |\n");
105 fprintf(stderr
, " %s", strxf_proto(IPPROTO_GRE
));
106 fprintf(stderr
, " [ key { DOTTED-QUAD | NUMBER } ] | PROTO }\n");
107 fprintf(stderr
, "LIMIT-LIST := [ LIMIT-LIST ] limit LIMIT\n");
108 fprintf(stderr
, "LIMIT := { time-soft | time-hard | time-use-soft | time-use-hard } SECONDS |\n");
109 fprintf(stderr
, " { byte-soft | byte-hard } SIZE | { packet-soft | packet-hard } COUNT\n");
110 fprintf(stderr
, "ENCAP := { espinudp | espinudp-nonike } SPORT DPORT OADDR\n");
115 static int xfrm_algo_parse(struct xfrm_algo
*alg
, enum xfrm_attr_type_t type
,
116 char *name
, char *key
, char *buf
, int max
)
119 int slen
= strlen(key
);
122 /* XXX: verifying both name and key is required! */
123 fprintf(stderr
, "warning: ALGO-NAME/ALGO-KEYMAT values will be sent to the kernel promiscuously! (verifying them isn't implemented yet)\n");
126 strncpy(alg
->alg_name
, name
, sizeof(alg
->alg_name
));
128 if (slen
> 2 && strncmp(key
, "0x", 2) == 0) {
129 /* split two chars "0x" from the top */
135 /* Converting hexadecimal numbered string into real key;
136 * Convert each two chars into one char(value). If number
137 * of the length is odd, add zero on the top for rounding.
140 /* calculate length of the converted values(real key) */
141 len
= (plen
+ 1) / 2;
143 invarg("ALGO-KEYMAT value makes buffer overflow\n", key
);
145 for (i
= - (plen
% 2), j
= 0; j
< len
; i
+= 2, j
++) {
149 vbuf
[0] = i
>= 0 ? p
[i
] : '0';
153 if (get_u8(&val
, vbuf
, 16))
154 invarg("ALGO-KEYMAT value is invalid", key
);
162 invarg("ALGO-KEYMAT value makes buffer overflow\n", key
);
164 memcpy(buf
, key
, len
);
168 alg
->alg_key_len
= len
* 8;
173 static int xfrm_seq_parse(__u32
*seq
, int *argcp
, char ***argvp
)
176 char **argv
= *argvp
;
178 if (get_u32(seq
, *argv
, 0))
179 invarg("SEQ value is invalid", *argv
);
189 static int xfrm_state_flag_parse(__u8
*flags
, int *argcp
, char ***argvp
)
192 char **argv
= *argvp
;
193 int len
= strlen(*argv
);
195 if (len
> 2 && strncmp(*argv
, "0x", 2) == 0) {
198 if (get_u8(&val
, *argv
, 16))
199 invarg("FLAG value is invalid", *argv
);
203 if (strcmp(*argv
, "noecn") == 0)
204 *flags
|= XFRM_STATE_NOECN
;
205 else if (strcmp(*argv
, "decap-dscp") == 0)
206 *flags
|= XFRM_STATE_DECAP_DSCP
;
207 else if (strcmp(*argv
, "nopmtudisc") == 0)
208 *flags
|= XFRM_STATE_NOPMTUDISC
;
209 else if (strcmp(*argv
, "wildrecv") == 0)
210 *flags
|= XFRM_STATE_WILDRECV
;
211 else if (strcmp(*argv
, "icmp") == 0)
212 *flags
|= XFRM_STATE_ICMP
;
213 else if (strcmp(*argv
, "af-unspec") == 0)
214 *flags
|= XFRM_STATE_AF_UNSPEC
;
215 else if (strcmp(*argv
, "align4") == 0)
216 *flags
|= XFRM_STATE_ALIGN4
;
218 PREV_ARG(); /* back track */
234 static int xfrm_state_extra_flag_parse(__u32
*extra_flags
, int *argcp
, char ***argvp
)
237 char **argv
= *argvp
;
238 int len
= strlen(*argv
);
240 if (len
> 2 && strncmp(*argv
, "0x", 2) == 0) {
243 if (get_u32(&val
, *argv
, 16))
244 invarg("\"EXTRA-FLAG\" is invalid", *argv
);
248 if (strcmp(*argv
, "dont-encap-dscp") == 0)
249 *extra_flags
|= XFRM_SA_XFLAG_DONT_ENCAP_DSCP
;
251 PREV_ARG(); /* back track */
267 static int xfrm_state_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
269 struct rtnl_handle rth
;
272 struct xfrm_usersa_info xsinfo
;
273 char buf
[RTA_BUF_SIZE
];
275 struct xfrm_replay_state replay
;
283 __u32 extra_flags
= 0;
284 struct xfrm_mark mark
= {0, 0};
286 struct xfrm_user_sec_ctx sctx
;
287 char str
[CTX_BUF_SIZE
];
290 memset(&req
, 0, sizeof(req
));
291 memset(&replay
, 0, sizeof(replay
));
292 memset(&ctx
, 0, sizeof(ctx
));
294 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsinfo
));
295 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
296 req
.n
.nlmsg_type
= cmd
;
297 req
.xsinfo
.family
= preferred_family
;
299 req
.xsinfo
.lft
.soft_byte_limit
= XFRM_INF
;
300 req
.xsinfo
.lft
.hard_byte_limit
= XFRM_INF
;
301 req
.xsinfo
.lft
.soft_packet_limit
= XFRM_INF
;
302 req
.xsinfo
.lft
.hard_packet_limit
= XFRM_INF
;
305 if (strcmp(*argv
, "mode") == 0) {
307 xfrm_mode_parse(&req
.xsinfo
.mode
, &argc
, &argv
);
308 } else if (strcmp(*argv
, "mark") == 0) {
309 xfrm_parse_mark(&mark
, &argc
, &argv
);
310 } else if (strcmp(*argv
, "reqid") == 0) {
312 xfrm_reqid_parse(&req
.xsinfo
.reqid
, &argc
, &argv
);
313 } else if (strcmp(*argv
, "seq") == 0) {
315 xfrm_seq_parse(&req
.xsinfo
.seq
, &argc
, &argv
);
316 } else if (strcmp(*argv
, "replay-window") == 0) {
318 if (get_u8(&req
.xsinfo
.replay_window
, *argv
, 0))
319 invarg("value after \"replay-window\" is invalid", *argv
);
320 } else if (strcmp(*argv
, "replay-seq") == 0) {
322 if (get_u32(&replay
.seq
, *argv
, 0))
323 invarg("value after \"replay-seq\" is invalid", *argv
);
324 } else if (strcmp(*argv
, "replay-oseq") == 0) {
326 if (get_u32(&replay
.oseq
, *argv
, 0))
327 invarg("value after \"replay-oseq\" is invalid", *argv
);
328 } else if (strcmp(*argv
, "flag") == 0) {
330 xfrm_state_flag_parse(&req
.xsinfo
.flags
, &argc
, &argv
);
331 } else if (strcmp(*argv
, "extra-flag") == 0) {
333 xfrm_state_extra_flag_parse(&extra_flags
, &argc
, &argv
);
334 } else if (strcmp(*argv
, "sel") == 0) {
336 preferred_family
= AF_UNSPEC
;
337 xfrm_selector_parse(&req
.xsinfo
.sel
, &argc
, &argv
);
338 preferred_family
= req
.xsinfo
.sel
.family
;
339 } else if (strcmp(*argv
, "limit") == 0) {
341 xfrm_lifetime_cfg_parse(&req
.xsinfo
.lft
, &argc
, &argv
);
342 } else if (strcmp(*argv
, "encap") == 0) {
343 struct xfrm_encap_tmpl encap
;
346 xfrm_encap_type_parse(&encap
.encap_type
, &argc
, &argv
);
348 if (get_u16(&encap
.encap_sport
, *argv
, 0))
349 invarg("SPORT value after \"encap\" is invalid", *argv
);
350 encap
.encap_sport
= htons(encap
.encap_sport
);
352 if (get_u16(&encap
.encap_dport
, *argv
, 0))
353 invarg("DPORT value after \"encap\" is invalid", *argv
);
354 encap
.encap_dport
= htons(encap
.encap_dport
);
356 get_addr(&oa
, *argv
, AF_UNSPEC
);
357 memcpy(&encap
.encap_oa
, &oa
.data
, sizeof(encap
.encap_oa
));
358 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_ENCAP
,
359 (void *)&encap
, sizeof(encap
));
360 } else if (strcmp(*argv
, "coa") == 0) {
365 duparg("coa", *argv
);
370 get_prefix(&coa
, *argv
, preferred_family
);
371 if (coa
.family
== AF_UNSPEC
)
372 invarg("value after \"coa\" has an unrecognized address family", *argv
);
373 if (coa
.bytelen
> sizeof(xcoa
))
374 invarg("value after \"coa\" is too large", *argv
);
376 memset(&xcoa
, 0, sizeof(xcoa
));
377 memcpy(&xcoa
, &coa
.data
, coa
.bytelen
);
379 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_COADDR
,
380 (void *)&xcoa
, sizeof(xcoa
));
381 } else if (strcmp(*argv
, "ctx") == 0) {
385 duparg("ctx", *argv
);
391 xfrm_sctx_parse((char *)&ctx
.str
, context
, &ctx
.sctx
);
392 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_SEC_CTX
,
393 (void *)&ctx
, ctx
.sctx
.len
);
395 /* try to assume ALGO */
396 int type
= xfrm_algotype_getbyname(*argv
);
399 case XFRMA_ALG_CRYPT
:
401 case XFRMA_ALG_AUTH_TRUNC
:
407 struct xfrm_algo alg
;
408 struct xfrm_algo_aead aead
;
409 struct xfrm_algo_auth auth
;
411 char buf
[XFRM_ALGO_KEY_BUF_SIZE
];
414 __u32 icvlen
, trunclen
;
421 if (ealgop
|| aalgop
|| aeadop
)
422 duparg("ALGO-TYPE", *argv
);
425 case XFRMA_ALG_CRYPT
:
426 if (ealgop
|| aeadop
)
427 duparg("ALGO-TYPE", *argv
);
431 case XFRMA_ALG_AUTH_TRUNC
:
432 if (aalgop
|| aeadop
)
433 duparg("ALGO-TYPE", *argv
);
438 duparg("ALGO-TYPE", *argv
);
443 invarg("ALGO-TYPE value is invalid\n", *argv
);
447 missarg("ALGO-NAME");
453 case XFRMA_ALG_CRYPT
:
455 case XFRMA_ALG_AUTH_TRUNC
:
457 missarg("ALGO-KEYMAT");
463 buf
= alg
.u
.alg
.alg_key
;
464 len
= sizeof(alg
.u
.alg
);
469 missarg("ALGO-ICV-LEN");
471 if (get_u32(&icvlen
, *argv
, 0))
472 invarg("ALGO-ICV-LEN value is invalid",
474 alg
.u
.aead
.alg_icv_len
= icvlen
;
476 buf
= alg
.u
.aead
.alg_key
;
477 len
= sizeof(alg
.u
.aead
);
479 case XFRMA_ALG_AUTH_TRUNC
:
481 missarg("ALGO-TRUNC-LEN");
483 if (get_u32(&trunclen
, *argv
, 0))
484 invarg("ALGO-TRUNC-LEN value is invalid",
486 alg
.u
.auth
.alg_trunc_len
= trunclen
;
488 buf
= alg
.u
.auth
.alg_key
;
489 len
= sizeof(alg
.u
.auth
);
493 xfrm_algo_parse((void *)&alg
, type
, name
, key
,
494 buf
, sizeof(alg
.buf
));
495 len
+= alg
.u
.alg
.alg_key_len
;
497 addattr_l(&req
.n
, sizeof(req
.buf
), type
,
502 /* try to assume ID */
504 invarg("unknown", *argv
);
508 xfrm_id_parse(&req
.xsinfo
.saddr
, &req
.xsinfo
.id
,
509 &req
.xsinfo
.family
, 0, &argc
, &argv
);
510 if (preferred_family
== AF_UNSPEC
)
511 preferred_family
= req
.xsinfo
.family
;
517 if (replay
.seq
|| replay
.oseq
)
518 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_REPLAY_VAL
,
519 (void *)&replay
, sizeof(replay
));
522 addattr32(&req
.n
, sizeof(req
.buf
), XFRMA_SA_EXTRA_FLAGS
,
526 fprintf(stderr
, "Not enough information: ID is required\n");
531 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
532 (void *)&mark
, sizeof(mark
));
534 fprintf(stderr
, "XFRMA_MARK failed\n");
539 if (xfrm_xfrmproto_is_ipsec(req
.xsinfo
.id
.proto
)) {
540 switch (req
.xsinfo
.mode
) {
541 case XFRM_MODE_TRANSPORT
:
542 case XFRM_MODE_TUNNEL
:
545 if (req
.xsinfo
.id
.proto
== IPPROTO_ESP
)
548 fprintf(stderr
, "MODE value is invalid with XFRM-PROTO value \"%s\"\n",
549 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
553 switch (req
.xsinfo
.id
.proto
) {
556 fprintf(stderr
, "ALGO-TYPE value \"%s\" is invalid with XFRM-PROTO value \"%s\"\n",
557 strxf_algotype(XFRMA_ALG_COMP
),
558 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
561 if (!ealgop
&& !aeadop
) {
562 fprintf(stderr
, "ALGO-TYPE value \"%s\" or \"%s\" is required with XFRM-PROTO value \"%s\"\n",
563 strxf_algotype(XFRMA_ALG_CRYPT
),
564 strxf_algotype(XFRMA_ALG_AEAD
),
565 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
570 if (ealgop
|| aeadop
|| calgop
) {
571 fprintf(stderr
, "ALGO-TYPE values \"%s\", \"%s\", and \"%s\" are invalid with XFRM-PROTO value \"%s\"\n",
572 strxf_algotype(XFRMA_ALG_CRYPT
),
573 strxf_algotype(XFRMA_ALG_AEAD
),
574 strxf_algotype(XFRMA_ALG_COMP
),
575 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
579 fprintf(stderr
, "ALGO-TYPE value \"%s\" or \"%s\" is required with XFRM-PROTO value \"%s\"\n",
580 strxf_algotype(XFRMA_ALG_AUTH
),
581 strxf_algotype(XFRMA_ALG_AUTH_TRUNC
),
582 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
587 if (ealgop
|| aalgop
|| aeadop
) {
588 fprintf(stderr
, "ALGO-TYPE values \"%s\", \"%s\", \"%s\", and \"%s\" are invalid with XFRM-PROTO value \"%s\"\n",
589 strxf_algotype(XFRMA_ALG_CRYPT
),
590 strxf_algotype(XFRMA_ALG_AUTH
),
591 strxf_algotype(XFRMA_ALG_AUTH_TRUNC
),
592 strxf_algotype(XFRMA_ALG_AEAD
),
593 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
597 fprintf(stderr
, "ALGO-TYPE value \"%s\" is required with XFRM-PROTO value \"%s\"\n",
598 strxf_algotype(XFRMA_ALG_COMP
),
599 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
605 if (ealgop
|| aalgop
|| aeadop
|| calgop
) {
606 fprintf(stderr
, "ALGO is invalid with XFRM-PROTO value \"%s\"\n",
607 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
612 if (xfrm_xfrmproto_is_ro(req
.xsinfo
.id
.proto
)) {
613 switch (req
.xsinfo
.mode
) {
614 case XFRM_MODE_ROUTEOPTIMIZATION
:
615 case XFRM_MODE_IN_TRIGGER
:
618 fprintf(stderr
, "\"mode\" is required with XFRM-PROTO value \"%s\"\n",
619 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
622 fprintf(stderr
, "MODE value is invalid with XFRM-PROTO value \"%s\"\n",
623 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
628 fprintf(stderr
, "\"coa\" is required with XFRM-PROTO value \"%s\"\n",
629 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
634 fprintf(stderr
, "\"coa\" is invalid with XFRM-PROTO value \"%s\"\n",
635 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
640 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
643 if (req
.xsinfo
.family
== AF_UNSPEC
)
644 req
.xsinfo
.family
= AF_INET
;
646 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
) < 0)
654 static int xfrm_state_allocspi(int argc
, char **argv
)
656 struct rtnl_handle rth
;
659 struct xfrm_userspi_info xspi
;
660 char buf
[RTA_BUF_SIZE
];
665 struct xfrm_mark mark
= {0, 0};
666 char res_buf
[NLMSG_BUF_SIZE
];
667 struct nlmsghdr
*res_n
= (struct nlmsghdr
*)res_buf
;
669 memset(res_buf
, 0, sizeof(res_buf
));
671 memset(&req
, 0, sizeof(req
));
673 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xspi
));
674 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
675 req
.n
.nlmsg_type
= XFRM_MSG_ALLOCSPI
;
676 req
.xspi
.info
.family
= preferred_family
;
679 req
.xsinfo
.lft
.soft_byte_limit
= XFRM_INF
;
680 req
.xsinfo
.lft
.hard_byte_limit
= XFRM_INF
;
681 req
.xsinfo
.lft
.soft_packet_limit
= XFRM_INF
;
682 req
.xsinfo
.lft
.hard_packet_limit
= XFRM_INF
;
686 if (strcmp(*argv
, "mode") == 0) {
688 xfrm_mode_parse(&req
.xspi
.info
.mode
, &argc
, &argv
);
689 } else if (strcmp(*argv
, "mark") == 0) {
690 xfrm_parse_mark(&mark
, &argc
, &argv
);
691 } else if (strcmp(*argv
, "reqid") == 0) {
693 xfrm_reqid_parse(&req
.xspi
.info
.reqid
, &argc
, &argv
);
694 } else if (strcmp(*argv
, "seq") == 0) {
696 xfrm_seq_parse(&req
.xspi
.info
.seq
, &argc
, &argv
);
697 } else if (strcmp(*argv
, "min") == 0) {
699 duparg("min", *argv
);
704 if (get_u32(&req
.xspi
.min
, *argv
, 0))
705 invarg("value after \"min\" is invalid", *argv
);
706 } else if (strcmp(*argv
, "max") == 0) {
708 duparg("max", *argv
);
713 if (get_u32(&req
.xspi
.max
, *argv
, 0))
714 invarg("value after \"max\" is invalid", *argv
);
716 /* try to assume ID */
718 invarg("unknown", *argv
);
722 xfrm_id_parse(&req
.xspi
.info
.saddr
, &req
.xspi
.info
.id
,
723 &req
.xspi
.info
.family
, 0, &argc
, &argv
);
724 if (req
.xspi
.info
.id
.spi
) {
725 fprintf(stderr
, "\"spi\" is invalid\n");
728 if (preferred_family
== AF_UNSPEC
)
729 preferred_family
= req
.xspi
.info
.family
;
735 fprintf(stderr
, "Not enough information: ID is required\n");
741 fprintf(stderr
, "\"max\" is missing\n");
744 if (req
.xspi
.min
> req
.xspi
.max
) {
745 fprintf(stderr
, "value after \"min\" is larger than value after \"max\"\n");
750 fprintf(stderr
, "\"min\" is missing\n");
754 /* XXX: Default value defined in PF_KEY;
755 * See kernel's net/key/af_key.c(pfkey_getspi).
757 req
.xspi
.min
= 0x100;
758 req
.xspi
.max
= 0x0fffffff;
760 /* XXX: IPCOMP spi is 16-bits;
761 * See kernel's net/xfrm/xfrm_user(verify_userspi_info).
763 if (req
.xspi
.info
.id
.proto
== IPPROTO_COMP
)
764 req
.xspi
.max
= 0xffff;
767 if (mark
.m
& mark
.v
) {
768 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
769 (void *)&mark
, sizeof(mark
));
771 fprintf(stderr
, "XFRMA_MARK failed\n");
776 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
779 if (req
.xspi
.info
.family
== AF_UNSPEC
)
780 req
.xspi
.info
.family
= AF_INET
;
783 if (rtnl_talk(&rth
, &req
.n
, 0, 0, res_n
) < 0)
786 if (xfrm_state_print(NULL
, res_n
, (void*)stdout
) < 0) {
787 fprintf(stderr
, "An error :-)\n");
796 static int xfrm_state_filter_match(struct xfrm_usersa_info
*xsinfo
)
801 if (filter
.id_src_mask
)
802 if (xfrm_addr_match(&xsinfo
->saddr
, &filter
.xsinfo
.saddr
,
805 if (filter
.id_dst_mask
)
806 if (xfrm_addr_match(&xsinfo
->id
.daddr
, &filter
.xsinfo
.id
.daddr
,
809 if ((xsinfo
->id
.proto
^filter
.xsinfo
.id
.proto
)&filter
.id_proto_mask
)
811 if ((xsinfo
->id
.spi
^filter
.xsinfo
.id
.spi
)&filter
.id_spi_mask
)
813 if ((xsinfo
->mode
^filter
.xsinfo
.mode
)&filter
.mode_mask
)
815 if ((xsinfo
->reqid
^filter
.xsinfo
.reqid
)&filter
.reqid_mask
)
817 if (filter
.state_flags_mask
)
818 if ((xsinfo
->flags
& filter
.xsinfo
.flags
) == 0)
824 int xfrm_state_print(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
827 FILE *fp
= (FILE*)arg
;
828 struct rtattr
* tb
[XFRMA_MAX
+1];
830 struct xfrm_usersa_info
*xsinfo
= NULL
;
831 struct xfrm_user_expire
*xexp
= NULL
;
832 struct xfrm_usersa_id
*xsid
= NULL
;
833 int len
= n
->nlmsg_len
;
835 if (n
->nlmsg_type
!= XFRM_MSG_NEWSA
&&
836 n
->nlmsg_type
!= XFRM_MSG_DELSA
&&
837 n
->nlmsg_type
!= XFRM_MSG_UPDSA
&&
838 n
->nlmsg_type
!= XFRM_MSG_EXPIRE
) {
839 fprintf(stderr
, "Not a state: %08x %08x %08x\n",
840 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
844 if (n
->nlmsg_type
== XFRM_MSG_DELSA
) {
845 /* Dont blame me for this .. Herbert made me do it */
846 xsid
= NLMSG_DATA(n
);
847 len
-= NLMSG_SPACE(sizeof(*xsid
));
848 } else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
) {
849 xexp
= NLMSG_DATA(n
);
850 xsinfo
= &xexp
->state
;
851 len
-= NLMSG_SPACE(sizeof(*xexp
));
854 xsinfo
= NLMSG_DATA(n
);
855 len
-= NLMSG_SPACE(sizeof(*xsinfo
));
859 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
863 if (xsinfo
&& !xfrm_state_filter_match(xsinfo
))
866 if (n
->nlmsg_type
== XFRM_MSG_DELSA
)
867 fprintf(fp
, "Deleted ");
868 else if (n
->nlmsg_type
== XFRM_MSG_UPDSA
)
869 fprintf(fp
, "Updated ");
870 else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
)
871 fprintf(fp
, "Expired ");
873 if (n
->nlmsg_type
== XFRM_MSG_DELSA
)
874 rta
= XFRMSID_RTA(xsid
);
875 else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
)
876 rta
= XFRMEXP_RTA(xexp
);
878 rta
= XFRMS_RTA(xsinfo
);
880 parse_rtattr(tb
, XFRMA_MAX
, rta
, len
);
882 if (n
->nlmsg_type
== XFRM_MSG_DELSA
) {
883 //xfrm_policy_id_print();
886 fprintf(stderr
, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
889 if (RTA_PAYLOAD(tb
[XFRMA_SA
]) < sizeof(*xsinfo
)) {
890 fprintf(stderr
, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
893 xsinfo
= RTA_DATA(tb
[XFRMA_SA
]);
896 xfrm_state_info_print(xsinfo
, tb
, fp
, NULL
, NULL
);
898 if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
) {
900 fprintf(fp
, "hard %u", xexp
->hard
);
901 fprintf(fp
, "%s", _SL_
);
911 static int xfrm_state_get_or_delete(int argc
, char **argv
, int delete)
913 struct rtnl_handle rth
;
916 struct xfrm_usersa_id xsid
;
917 char buf
[RTA_BUF_SIZE
];
921 struct xfrm_mark mark
= {0, 0};
923 memset(&req
, 0, sizeof(req
));
925 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsid
));
926 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
927 req
.n
.nlmsg_type
= delete ? XFRM_MSG_DELSA
: XFRM_MSG_GETSA
;
928 req
.xsid
.family
= preferred_family
;
931 xfrm_address_t saddr
;
933 if (strcmp(*argv
, "mark") == 0) {
934 xfrm_parse_mark(&mark
, &argc
, &argv
);
937 invarg("unknown", *argv
);
941 memset(&id
, 0, sizeof(id
));
942 memset(&saddr
, 0, sizeof(saddr
));
943 xfrm_id_parse(&saddr
, &id
, &req
.xsid
.family
, 0,
946 memcpy(&req
.xsid
.daddr
, &id
.daddr
, sizeof(req
.xsid
.daddr
));
947 req
.xsid
.spi
= id
.spi
;
948 req
.xsid
.proto
= id
.proto
;
950 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_SRCADDR
,
951 (void *)&saddr
, sizeof(saddr
));
957 if (mark
.m
& mark
.v
) {
958 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
959 (void *)&mark
, sizeof(mark
));
961 fprintf(stderr
, "XFRMA_MARK failed\n");
966 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
969 if (req
.xsid
.family
== AF_UNSPEC
)
970 req
.xsid
.family
= AF_INET
;
973 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
) < 0)
976 char buf
[NLMSG_BUF_SIZE
];
977 struct nlmsghdr
*res_n
= (struct nlmsghdr
*)buf
;
979 memset(buf
, 0, sizeof(buf
));
981 if (rtnl_talk(&rth
, &req
.n
, 0, 0, res_n
) < 0)
984 if (xfrm_state_print(NULL
, res_n
, (void*)stdout
) < 0) {
985 fprintf(stderr
, "An error :-)\n");
996 * With an existing state of nlmsg, make new nlmsg for deleting the state
997 * and store it to buffer.
999 static int xfrm_state_keep(const struct sockaddr_nl
*who
,
1003 struct xfrm_buffer
*xb
= (struct xfrm_buffer
*)arg
;
1004 struct rtnl_handle
*rth
= xb
->rth
;
1005 struct xfrm_usersa_info
*xsinfo
= NLMSG_DATA(n
);
1006 int len
= n
->nlmsg_len
;
1007 struct nlmsghdr
*new_n
;
1008 struct xfrm_usersa_id
*xsid
;
1010 if (n
->nlmsg_type
!= XFRM_MSG_NEWSA
) {
1011 fprintf(stderr
, "Not a state: %08x %08x %08x\n",
1012 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
1016 len
-= NLMSG_LENGTH(sizeof(*xsinfo
));
1018 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
1022 if (!xfrm_state_filter_match(xsinfo
))
1025 if (xb
->offset
> xb
->size
) {
1026 fprintf(stderr
, "State buffer overflow\n");
1030 new_n
= (struct nlmsghdr
*)(xb
->buf
+ xb
->offset
);
1031 new_n
->nlmsg_len
= NLMSG_LENGTH(sizeof(*xsid
));
1032 new_n
->nlmsg_flags
= NLM_F_REQUEST
;
1033 new_n
->nlmsg_type
= XFRM_MSG_DELSA
;
1034 new_n
->nlmsg_seq
= ++rth
->seq
;
1036 xsid
= NLMSG_DATA(new_n
);
1037 xsid
->family
= xsinfo
->family
;
1038 memcpy(&xsid
->daddr
, &xsinfo
->id
.daddr
, sizeof(xsid
->daddr
));
1039 xsid
->spi
= xsinfo
->id
.spi
;
1040 xsid
->proto
= xsinfo
->id
.proto
;
1042 addattr_l(new_n
, xb
->size
, XFRMA_SRCADDR
, &xsinfo
->saddr
,
1043 sizeof(xsid
->daddr
));
1045 xb
->offset
+= new_n
->nlmsg_len
;
1051 static int xfrm_state_list_or_deleteall(int argc
, char **argv
, int deleteall
)
1054 struct rtnl_handle rth
;
1058 filter
.xsinfo
.family
= preferred_family
;
1061 if (strcmp(*argv
, "mode") == 0) {
1063 xfrm_mode_parse(&filter
.xsinfo
.mode
, &argc
, &argv
);
1065 filter
.mode_mask
= XFRM_FILTER_MASK_FULL
;
1067 } else if (strcmp(*argv
, "reqid") == 0) {
1069 xfrm_reqid_parse(&filter
.xsinfo
.reqid
, &argc
, &argv
);
1071 filter
.reqid_mask
= XFRM_FILTER_MASK_FULL
;
1073 } else if (strcmp(*argv
, "flag") == 0) {
1075 xfrm_state_flag_parse(&filter
.xsinfo
.flags
, &argc
, &argv
);
1077 filter
.state_flags_mask
= XFRM_FILTER_MASK_FULL
;
1081 invarg("unknown", *argv
);
1085 xfrm_id_parse(&filter
.xsinfo
.saddr
, &filter
.xsinfo
.id
,
1086 &filter
.xsinfo
.family
, 1, &argc
, &argv
);
1087 if (preferred_family
== AF_UNSPEC
)
1088 preferred_family
= filter
.xsinfo
.family
;
1093 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1097 struct xfrm_buffer xb
;
1098 char buf
[NLMSG_DELETEALL_BUF_SIZE
];
1102 xb
.size
= sizeof(buf
);
1105 for (i
= 0; ; i
++) {
1110 fprintf(stderr
, "Delete-all round = %d\n", i
);
1112 if (rtnl_wilddump_request(&rth
, preferred_family
, XFRM_MSG_GETSA
) < 0) {
1113 perror("Cannot send dump request");
1117 if (rtnl_dump_filter(&rth
, xfrm_state_keep
, &xb
) < 0) {
1118 fprintf(stderr
, "Delete-all terminated\n");
1121 if (xb
.nlmsg_count
== 0) {
1123 fprintf(stderr
, "Delete-all completed\n");
1127 if (rtnl_send_check(&rth
, xb
.buf
, xb
.offset
) < 0) {
1128 perror("Failed to send delete-all request\n");
1132 fprintf(stderr
, "Delete-all nlmsg count = %d\n", xb
.nlmsg_count
);
1139 struct xfrm_address_filter addrfilter
= {
1140 .saddr
= filter
.xsinfo
.saddr
,
1141 .daddr
= filter
.xsinfo
.id
.daddr
,
1142 .family
= filter
.xsinfo
.family
,
1143 .splen
= filter
.id_src_mask
,
1144 .dplen
= filter
.id_dst_mask
,
1148 char buf
[NLMSG_BUF_SIZE
];
1150 .n
.nlmsg_len
= NLMSG_HDRLEN
,
1151 .n
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
1152 .n
.nlmsg_type
= XFRM_MSG_GETSA
,
1153 .n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
,
1156 if (filter
.xsinfo
.id
.proto
)
1157 addattr8(&req
.n
, sizeof(req
), XFRMA_PROTO
,
1158 filter
.xsinfo
.id
.proto
);
1159 addattr_l(&req
.n
, sizeof(req
), XFRMA_ADDRESS_FILTER
,
1160 &addrfilter
, sizeof(addrfilter
));
1162 if (rtnl_send(&rth
, (void *)&req
, req
.n
.nlmsg_len
) < 0) {
1163 perror("Cannot send dump request");
1167 if (rtnl_dump_filter(&rth
, xfrm_state_print
, stdout
) < 0) {
1168 fprintf(stderr
, "Dump terminated\n");
1178 static int print_sadinfo(struct nlmsghdr
*n
, void *arg
)
1180 FILE *fp
= (FILE*)arg
;
1181 __u32
*f
= NLMSG_DATA(n
);
1182 struct rtattr
*tb
[XFRMA_SAD_MAX
+1];
1186 int len
= n
->nlmsg_len
;
1188 len
-= NLMSG_LENGTH(sizeof(__u32
));
1190 fprintf(stderr
, "SADinfo: Wrong len %d\n", len
);
1194 rta
= XFRMSAPD_RTA(f
);
1195 parse_rtattr(tb
, XFRMA_SAD_MAX
, rta
, len
);
1197 if (tb
[XFRMA_SAD_CNT
]) {
1198 fprintf(fp
,"\t SAD");
1199 cnt
= (__u32
*)RTA_DATA(tb
[XFRMA_SAD_CNT
]);
1200 fprintf(fp
," count %d", *cnt
);
1202 fprintf(fp
,"BAD SAD info returned\n");
1207 if (tb
[XFRMA_SAD_HINFO
]) {
1208 struct xfrmu_sadhinfo
*si
;
1210 if (RTA_PAYLOAD(tb
[XFRMA_SAD_HINFO
]) < sizeof(*si
)) {
1211 fprintf(fp
,"BAD SAD length returned\n");
1215 si
= RTA_DATA(tb
[XFRMA_SAD_HINFO
]);
1216 fprintf(fp
," (buckets ");
1217 fprintf(fp
,"count %d", si
->sadhcnt
);
1218 fprintf(fp
," Max %d", si
->sadhmcnt
);
1227 static int xfrm_sad_getinfo(int argc
, char **argv
)
1229 struct rtnl_handle rth
;
1236 memset(&req
, 0, sizeof(req
));
1237 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.flags
));
1238 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1239 req
.n
.nlmsg_type
= XFRM_MSG_GETSADINFO
;
1240 req
.flags
= 0XFFFFFFFF;
1242 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1245 if (rtnl_talk(&rth
, &req
.n
, 0, 0, &req
.n
) < 0)
1248 print_sadinfo(&req
.n
, (void*)stdout
);
1255 static int xfrm_state_flush(int argc
, char **argv
)
1257 struct rtnl_handle rth
;
1260 struct xfrm_usersa_flush xsf
;
1262 char *protop
= NULL
;
1264 memset(&req
, 0, sizeof(req
));
1266 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsf
));
1267 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1268 req
.n
.nlmsg_type
= XFRM_MSG_FLUSHSA
;
1272 if (strcmp(*argv
, "proto") == 0) {
1276 duparg("proto", *argv
);
1281 ret
= xfrm_xfrmproto_getbyname(*argv
);
1283 invarg("XFRM-PROTO value is invalid", *argv
);
1285 req
.xsf
.proto
= (__u8
)ret
;
1287 invarg("unknown", *argv
);
1292 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1296 fprintf(stderr
, "Flush state with XFRM-PROTO value \"%s\"\n",
1297 strxf_xfrmproto(req
.xsf
.proto
));
1299 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
) < 0)
1307 int do_xfrm_state(int argc
, char **argv
)
1310 return xfrm_state_list_or_deleteall(0, NULL
, 0);
1312 if (matches(*argv
, "add") == 0)
1313 return xfrm_state_modify(XFRM_MSG_NEWSA
, 0,
1315 if (matches(*argv
, "update") == 0)
1316 return xfrm_state_modify(XFRM_MSG_UPDSA
, 0,
1318 if (matches(*argv
, "allocspi") == 0)
1319 return xfrm_state_allocspi(argc
-1, argv
+1);
1320 if (matches(*argv
, "delete") == 0)
1321 return xfrm_state_get_or_delete(argc
-1, argv
+1, 1);
1322 if (matches(*argv
, "deleteall") == 0 || matches(*argv
, "delall") == 0)
1323 return xfrm_state_list_or_deleteall(argc
-1, argv
+1, 1);
1324 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
1325 || matches(*argv
, "lst") == 0)
1326 return xfrm_state_list_or_deleteall(argc
-1, argv
+1, 0);
1327 if (matches(*argv
, "get") == 0)
1328 return xfrm_state_get_or_delete(argc
-1, argv
+1, 0);
1329 if (matches(*argv
, "flush") == 0)
1330 return xfrm_state_flush(argc
-1, argv
+1);
1331 if (matches(*argv
, "count") == 0) {
1332 return xfrm_sad_getinfo(argc
, argv
);
1334 if (matches(*argv
, "help") == 0)
1336 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv
);