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
33 #include "ip_common.h"
35 /* #define NLMSG_DELETEALL_BUF_SIZE (4096-512) */
36 #define NLMSG_DELETEALL_BUF_SIZE 8192
39 * Receiving buffer defines:
41 * data = struct xfrm_usersa_info
44 * ... (max count of rtattr is XFRM_MAX+1
46 * each rtattr data = struct xfrm_algo(dynamic size) or xfrm_address_t
48 #define NLMSG_BUF_SIZE 4096
49 #define RTA_BUF_SIZE 2048
50 #define XFRM_ALGO_KEY_BUF_SIZE 512
51 #define CTX_BUF_SIZE 256
53 static void usage(void) __attribute__((noreturn
));
55 static void usage(void)
58 "Usage: ip xfrm state { add | update } ID [ ALGO-LIST ] [ mode MODE ]\n"
59 " [ mark MARK [ mask MASK ] ] [ reqid REQID ] [ seq SEQ ]\n"
60 " [ replay-window SIZE ] [ replay-seq SEQ ] [ replay-oseq SEQ ]\n"
61 " [ replay-seq-hi SEQ ] [ replay-oseq-hi SEQ ]\n"
62 " [ flag FLAG-LIST ] [ sel SELECTOR ] [ LIMIT-LIST ] [ encap ENCAP ]\n"
63 " [ coa ADDR[/PLEN] ] [ ctx CTX ] [ extra-flag EXTRA-FLAG-LIST ]\n"
64 " [ offload [dev DEV] dir DIR ]\n"
65 " [ output-mark OUTPUT-MARK ]\n"
67 "Usage: ip xfrm state allocspi ID [ mode MODE ] [ mark MARK [ mask MASK ] ]\n"
68 " [ reqid REQID ] [ seq SEQ ] [ min SPI max SPI ]\n"
69 "Usage: ip xfrm state { delete | get } ID [ mark MARK [ mask MASK ] ]\n"
70 "Usage: ip xfrm state deleteall [ ID ] [ mode MODE ] [ reqid REQID ]\n"
71 " [ flag FLAG-LIST ]\n"
72 "Usage: ip xfrm state list [ nokeys ] [ ID ] [ mode MODE ] [ reqid REQID ]\n"
73 " [ flag FLAG-LIST ]\n"
74 "Usage: ip xfrm state flush [ proto XFRM-PROTO ]\n"
75 "Usage: ip xfrm state count\n"
76 "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ]\n"
79 "%s | %s | %s | %s | %s\n",
80 strxf_xfrmproto(IPPROTO_ESP
),
81 strxf_xfrmproto(IPPROTO_AH
),
82 strxf_xfrmproto(IPPROTO_COMP
),
83 strxf_xfrmproto(IPPROTO_ROUTING
),
84 strxf_xfrmproto(IPPROTO_DSTOPTS
));
86 "ALGO-LIST := [ ALGO-LIST ] ALGO\n"
90 strxf_algotype(XFRMA_ALG_CRYPT
),
91 strxf_algotype(XFRMA_ALG_AUTH
));
93 " } ALGO-NAME ALGO-KEYMAT |\n"
94 " %s", strxf_algotype(XFRMA_ALG_AUTH_TRUNC
));
96 " ALGO-NAME ALGO-KEYMAT ALGO-TRUNC-LEN |\n"
97 " %s", strxf_algotype(XFRMA_ALG_AEAD
));
99 " ALGO-NAME ALGO-KEYMAT ALGO-ICV-LEN |\n"
100 " %s", strxf_algotype(XFRMA_ALG_COMP
));
103 "MODE := transport | tunnel | beet | ro | in_trigger\n"
104 "FLAG-LIST := [ FLAG-LIST ] FLAG\n"
105 "FLAG := noecn | decap-dscp | nopmtudisc | wildrecv | icmp | af-unspec | align4 | esn\n"
106 "EXTRA-FLAG-LIST := [ EXTRA-FLAG-LIST ] EXTRA-FLAG\n"
107 "EXTRA-FLAG := dont-encap-dscp\n"
108 "SELECTOR := [ src ADDR[/PLEN] ] [ dst ADDR[/PLEN] ] [ dev DEV ] [ UPSPEC ]\n"
109 "UPSPEC := proto { { tcp | udp | sctp | dccp } [ sport PORT ] [ dport PORT ] |\n"
110 " { icmp | ipv6-icmp | mobility-header } [ type NUMBER ] [ code NUMBER ] |\n"
111 " gre [ key { DOTTED-QUAD | NUMBER } ] | PROTO }\n"
112 "LIMIT-LIST := [ LIMIT-LIST ] limit LIMIT\n"
113 "LIMIT := { time-soft | time-hard | time-use-soft | time-use-hard } SECONDS |\n"
114 " { byte-soft | byte-hard } SIZE | { packet-soft | packet-hard } COUNT\n"
115 "ENCAP := { espinudp | espinudp-nonike | espintcp } SPORT DPORT OADDR\n"
116 "DIR := in | out\n");
121 static int xfrm_algo_parse(struct xfrm_algo
*alg
, enum xfrm_attr_type_t type
,
122 char *name
, char *key
, char *buf
, int max
)
125 int slen
= strlen(key
);
128 /* XXX: verifying both name and key is required! */
129 fprintf(stderr
, "warning: ALGO-NAME/ALGO-KEYMAT values will be sent to the kernel promiscuously! (verifying them isn't implemented yet)\n");
132 strlcpy(alg
->alg_name
, name
, sizeof(alg
->alg_name
));
134 if (slen
> 2 && strncmp(key
, "0x", 2) == 0) {
135 /* split two chars "0x" from the top */
141 /* Converting hexadecimal numbered string into real key;
142 * Convert each two chars into one char(value). If number
143 * of the length is odd, add zero on the top for rounding.
146 /* calculate length of the converted values(real key) */
147 len
= (plen
+ 1) / 2;
149 invarg("ALGO-KEYMAT value makes buffer overflow\n", key
);
151 for (i
= -(plen
% 2), j
= 0; j
< len
; i
+= 2, j
++) {
155 vbuf
[0] = i
>= 0 ? p
[i
] : '0';
159 if (get_u8(&val
, vbuf
, 16))
160 invarg("ALGO-KEYMAT value is invalid", key
);
168 invarg("ALGO-KEYMAT value makes buffer overflow\n", key
);
170 memcpy(buf
, key
, len
);
174 alg
->alg_key_len
= len
* 8;
179 static int xfrm_seq_parse(__u32
*seq
, int *argcp
, char ***argvp
)
182 char **argv
= *argvp
;
184 if (get_be32(seq
, *argv
, 0))
185 invarg("SEQ value is invalid", *argv
);
193 static int xfrm_state_flag_parse(__u8
*flags
, int *argcp
, char ***argvp
)
196 char **argv
= *argvp
;
197 int len
= strlen(*argv
);
199 if (len
> 2 && strncmp(*argv
, "0x", 2) == 0) {
202 if (get_u8(&val
, *argv
, 16))
203 invarg("FLAG value is invalid", *argv
);
207 if (strcmp(*argv
, "noecn") == 0)
208 *flags
|= XFRM_STATE_NOECN
;
209 else if (strcmp(*argv
, "decap-dscp") == 0)
210 *flags
|= XFRM_STATE_DECAP_DSCP
;
211 else if (strcmp(*argv
, "nopmtudisc") == 0)
212 *flags
|= XFRM_STATE_NOPMTUDISC
;
213 else if (strcmp(*argv
, "wildrecv") == 0)
214 *flags
|= XFRM_STATE_WILDRECV
;
215 else if (strcmp(*argv
, "icmp") == 0)
216 *flags
|= XFRM_STATE_ICMP
;
217 else if (strcmp(*argv
, "af-unspec") == 0)
218 *flags
|= XFRM_STATE_AF_UNSPEC
;
219 else if (strcmp(*argv
, "align4") == 0)
220 *flags
|= XFRM_STATE_ALIGN4
;
221 else if (strcmp(*argv
, "esn") == 0)
222 *flags
|= XFRM_STATE_ESN
;
224 PREV_ARG(); /* back track */
240 static int xfrm_state_extra_flag_parse(__u32
*extra_flags
, int *argcp
, char ***argvp
)
243 char **argv
= *argvp
;
244 int len
= strlen(*argv
);
246 if (len
> 2 && strncmp(*argv
, "0x", 2) == 0) {
249 if (get_u32(&val
, *argv
, 16))
250 invarg("\"EXTRA-FLAG\" is invalid", *argv
);
254 if (strcmp(*argv
, "dont-encap-dscp") == 0)
255 *extra_flags
|= XFRM_SA_XFLAG_DONT_ENCAP_DSCP
;
257 PREV_ARG(); /* back track */
273 static int xfrm_offload_dir_parse(__u8
*dir
, int *argcp
, char ***argvp
)
276 char **argv
= *argvp
;
278 if (strcmp(*argv
, "in") == 0)
279 *dir
= XFRM_OFFLOAD_INBOUND
;
280 else if (strcmp(*argv
, "out") == 0)
283 invarg("DIR value is invalid", *argv
);
291 static int xfrm_state_modify(int cmd
, unsigned int flags
, int argc
, char **argv
)
293 struct rtnl_handle rth
;
296 struct xfrm_usersa_info xsinfo
;
297 char buf
[RTA_BUF_SIZE
];
299 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsinfo
)),
300 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
302 .xsinfo
.family
= preferred_family
,
303 .xsinfo
.lft
.soft_byte_limit
= XFRM_INF
,
304 .xsinfo
.lft
.hard_byte_limit
= XFRM_INF
,
305 .xsinfo
.lft
.soft_packet_limit
= XFRM_INF
,
306 .xsinfo
.lft
.hard_packet_limit
= XFRM_INF
,
308 struct xfrm_replay_state replay
= {};
309 struct xfrm_replay_state_esn replay_esn
= {};
310 struct xfrm_user_offload xuo
= {};
311 unsigned int ifindex
= 0;
313 bool is_offload
= false;
314 __u32 replay_window
= 0;
315 __u32 seq
= 0, oseq
= 0, seq_hi
= 0, oseq_hi
= 0;
323 __u32 extra_flags
= 0;
324 struct xfrm_mark mark
= {0, 0};
326 struct xfrm_user_sec_ctx sctx
;
327 char str
[CTX_BUF_SIZE
];
329 __u32 output_mark
= 0;
330 bool is_if_id_set
= false;
334 if (strcmp(*argv
, "mode") == 0) {
336 xfrm_mode_parse(&req
.xsinfo
.mode
, &argc
, &argv
);
337 } else if (strcmp(*argv
, "mark") == 0) {
338 xfrm_parse_mark(&mark
, &argc
, &argv
);
339 } else if (strcmp(*argv
, "reqid") == 0) {
341 xfrm_reqid_parse(&req
.xsinfo
.reqid
, &argc
, &argv
);
342 } else if (strcmp(*argv
, "seq") == 0) {
344 xfrm_seq_parse(&req
.xsinfo
.seq
, &argc
, &argv
);
345 } else if (strcmp(*argv
, "replay-window") == 0) {
347 if (get_u32(&replay_window
, *argv
, 0))
348 invarg("value after \"replay-window\" is invalid", *argv
);
349 } else if (strcmp(*argv
, "replay-seq") == 0) {
351 if (get_u32(&seq
, *argv
, 0))
352 invarg("value after \"replay-seq\" is invalid", *argv
);
353 } else if (strcmp(*argv
, "replay-seq-hi") == 0) {
355 if (get_u32(&seq_hi
, *argv
, 0))
356 invarg("value after \"replay-seq-hi\" is invalid", *argv
);
357 } else if (strcmp(*argv
, "replay-oseq") == 0) {
359 if (get_u32(&oseq
, *argv
, 0))
360 invarg("value after \"replay-oseq\" is invalid", *argv
);
361 } else if (strcmp(*argv
, "replay-oseq-hi") == 0) {
363 if (get_u32(&oseq_hi
, *argv
, 0))
364 invarg("value after \"replay-oseq-hi\" is invalid", *argv
);
365 } else if (strcmp(*argv
, "flag") == 0) {
367 xfrm_state_flag_parse(&req
.xsinfo
.flags
, &argc
, &argv
);
368 } else if (strcmp(*argv
, "extra-flag") == 0) {
370 xfrm_state_extra_flag_parse(&extra_flags
, &argc
, &argv
);
371 } else if (strcmp(*argv
, "sel") == 0) {
373 preferred_family
= AF_UNSPEC
;
374 xfrm_selector_parse(&req
.xsinfo
.sel
, &argc
, &argv
);
375 preferred_family
= req
.xsinfo
.sel
.family
;
376 } else if (strcmp(*argv
, "limit") == 0) {
378 xfrm_lifetime_cfg_parse(&req
.xsinfo
.lft
, &argc
, &argv
);
379 } else if (strcmp(*argv
, "encap") == 0) {
380 struct xfrm_encap_tmpl encap
;
383 xfrm_encap_type_parse(&encap
.encap_type
, &argc
, &argv
);
385 if (get_be16(&encap
.encap_sport
, *argv
, 0))
386 invarg("SPORT value after \"encap\" is invalid", *argv
);
388 if (get_be16(&encap
.encap_dport
, *argv
, 0))
389 invarg("DPORT value after \"encap\" is invalid", *argv
);
391 get_addr(&oa
, *argv
, AF_UNSPEC
);
392 memcpy(&encap
.encap_oa
, &oa
.data
, sizeof(encap
.encap_oa
));
393 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_ENCAP
,
394 (void *)&encap
, sizeof(encap
));
395 } else if (strcmp(*argv
, "coa") == 0) {
397 xfrm_address_t xcoa
= {};
400 duparg("coa", *argv
);
405 get_prefix(&coa
, *argv
, preferred_family
);
406 if (coa
.family
== AF_UNSPEC
)
407 invarg("value after \"coa\" has an unrecognized address family", *argv
);
408 if (coa
.bytelen
> sizeof(xcoa
))
409 invarg("value after \"coa\" is too large", *argv
);
411 memcpy(&xcoa
, &coa
.data
, coa
.bytelen
);
413 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_COADDR
,
414 (void *)&xcoa
, sizeof(xcoa
));
415 } else if (strcmp(*argv
, "ctx") == 0) {
419 duparg("ctx", *argv
);
425 xfrm_sctx_parse((char *)&ctx
.str
, context
, &ctx
.sctx
);
426 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_SEC_CTX
,
427 (void *)&ctx
, ctx
.sctx
.len
);
428 } else if (strcmp(*argv
, "offload") == 0) {
431 if (strcmp(*argv
, "dev") == 0) {
433 ifindex
= ll_name_to_index(*argv
);
435 invarg("value after \"offload dev\" is invalid", *argv
);
440 if (strcmp(*argv
, "dir") == 0) {
442 xfrm_offload_dir_parse(&dir
, &argc
, &argv
);
444 invarg("value after \"offload dir\" is invalid", *argv
);
447 } else if (strcmp(*argv
, "output-mark") == 0) {
449 if (get_u32(&output_mark
, *argv
, 0))
450 invarg("value after \"output-mark\" is invalid", *argv
);
451 } else if (strcmp(*argv
, "if_id") == 0) {
453 if (get_u32(&if_id
, *argv
, 0))
454 invarg("value after \"if_id\" is invalid", *argv
);
457 /* try to assume ALGO */
458 int type
= xfrm_algotype_getbyname(*argv
);
462 case XFRMA_ALG_CRYPT
:
464 case XFRMA_ALG_AUTH_TRUNC
:
470 struct xfrm_algo alg
;
471 struct xfrm_algo_aead aead
;
472 struct xfrm_algo_auth auth
;
474 char buf
[XFRM_ALGO_KEY_BUF_SIZE
];
477 __u32 icvlen
, trunclen
;
484 if (ealgop
|| aalgop
|| aeadop
)
485 duparg("ALGO-TYPE", *argv
);
488 case XFRMA_ALG_CRYPT
:
489 if (ealgop
|| aeadop
)
490 duparg("ALGO-TYPE", *argv
);
494 case XFRMA_ALG_AUTH_TRUNC
:
495 if (aalgop
|| aeadop
)
496 duparg("ALGO-TYPE", *argv
);
501 duparg("ALGO-TYPE", *argv
);
506 invarg("ALGO-TYPE value is invalid\n", *argv
);
510 missarg("ALGO-NAME");
516 case XFRMA_ALG_CRYPT
:
518 case XFRMA_ALG_AUTH_TRUNC
:
520 missarg("ALGO-KEYMAT");
526 buf
= alg
.u
.alg
.alg_key
;
527 len
= sizeof(alg
.u
.alg
);
532 missarg("ALGO-ICV-LEN");
534 if (get_u32(&icvlen
, *argv
, 0))
535 invarg("ALGO-ICV-LEN value is invalid",
537 alg
.u
.aead
.alg_icv_len
= icvlen
;
539 buf
= alg
.u
.aead
.alg_key
;
540 len
= sizeof(alg
.u
.aead
);
542 case XFRMA_ALG_AUTH_TRUNC
:
544 missarg("ALGO-TRUNC-LEN");
546 if (get_u32(&trunclen
, *argv
, 0))
547 invarg("ALGO-TRUNC-LEN value is invalid",
549 alg
.u
.auth
.alg_trunc_len
= trunclen
;
551 buf
= alg
.u
.auth
.alg_key
;
552 len
= sizeof(alg
.u
.auth
);
556 xfrm_algo_parse((void *)&alg
, type
, name
, key
,
557 buf
, sizeof(alg
.buf
));
558 len
+= alg
.u
.alg
.alg_key_len
/ 8;
560 addattr_l(&req
.n
, sizeof(req
.buf
), type
,
565 /* try to assume ID */
567 invarg("unknown", *argv
);
571 xfrm_id_parse(&req
.xsinfo
.saddr
, &req
.xsinfo
.id
,
572 &req
.xsinfo
.family
, 0, &argc
, &argv
);
573 if (preferred_family
== AF_UNSPEC
)
574 preferred_family
= req
.xsinfo
.family
;
580 if (req
.xsinfo
.flags
& XFRM_STATE_ESN
&&
581 replay_window
== 0) {
582 fprintf(stderr
, "Error: esn flag set without replay-window.\n");
586 if (replay_window
> XFRMA_REPLAY_ESN_MAX
) {
588 "Error: replay-window (%u) > XFRMA_REPLAY_ESN_MAX (%u).\n",
589 replay_window
, XFRMA_REPLAY_ESN_MAX
);
594 xuo
.ifindex
= ifindex
;
596 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_OFFLOAD_DEV
, &xuo
,
599 if (req
.xsinfo
.flags
& XFRM_STATE_ESN
||
600 replay_window
> (sizeof(replay
.bitmap
) * 8)) {
601 replay_esn
.seq
= seq
;
602 replay_esn
.oseq
= oseq
;
603 replay_esn
.seq_hi
= seq_hi
;
604 replay_esn
.oseq_hi
= oseq_hi
;
605 replay_esn
.replay_window
= replay_window
;
606 replay_esn
.bmp_len
= (replay_window
+ sizeof(__u32
) * 8 - 1) /
608 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_REPLAY_ESN_VAL
,
609 &replay_esn
, sizeof(replay_esn
));
614 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_REPLAY_VAL
,
615 &replay
, sizeof(replay
));
617 req
.xsinfo
.replay_window
= replay_window
;
621 addattr32(&req
.n
, sizeof(req
.buf
), XFRMA_SA_EXTRA_FLAGS
,
625 fprintf(stderr
, "Not enough information: ID is required\n");
630 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
631 (void *)&mark
, sizeof(mark
));
633 fprintf(stderr
, "XFRMA_MARK failed\n");
639 addattr32(&req
.n
, sizeof(req
.buf
), XFRMA_IF_ID
, if_id
);
641 if (xfrm_xfrmproto_is_ipsec(req
.xsinfo
.id
.proto
)) {
642 switch (req
.xsinfo
.mode
) {
643 case XFRM_MODE_TRANSPORT
:
644 case XFRM_MODE_TUNNEL
:
647 if (req
.xsinfo
.id
.proto
== IPPROTO_ESP
)
650 fprintf(stderr
, "MODE value is invalid with XFRM-PROTO value \"%s\"\n",
651 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
655 switch (req
.xsinfo
.id
.proto
) {
658 fprintf(stderr
, "ALGO-TYPE value \"%s\" is invalid with XFRM-PROTO value \"%s\"\n",
659 strxf_algotype(XFRMA_ALG_COMP
),
660 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
663 if (!ealgop
&& !aeadop
) {
664 fprintf(stderr
, "ALGO-TYPE value \"%s\" or \"%s\" is required with XFRM-PROTO value \"%s\"\n",
665 strxf_algotype(XFRMA_ALG_CRYPT
),
666 strxf_algotype(XFRMA_ALG_AEAD
),
667 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
672 if (ealgop
|| aeadop
|| calgop
) {
673 fprintf(stderr
, "ALGO-TYPE values \"%s\", \"%s\", and \"%s\" are invalid with XFRM-PROTO value \"%s\"\n",
674 strxf_algotype(XFRMA_ALG_CRYPT
),
675 strxf_algotype(XFRMA_ALG_AEAD
),
676 strxf_algotype(XFRMA_ALG_COMP
),
677 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
681 fprintf(stderr
, "ALGO-TYPE value \"%s\" or \"%s\" is required with XFRM-PROTO value \"%s\"\n",
682 strxf_algotype(XFRMA_ALG_AUTH
),
683 strxf_algotype(XFRMA_ALG_AUTH_TRUNC
),
684 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
689 if (ealgop
|| aalgop
|| aeadop
) {
690 fprintf(stderr
, "ALGO-TYPE values \"%s\", \"%s\", \"%s\", and \"%s\" are invalid with XFRM-PROTO value \"%s\"\n",
691 strxf_algotype(XFRMA_ALG_CRYPT
),
692 strxf_algotype(XFRMA_ALG_AUTH
),
693 strxf_algotype(XFRMA_ALG_AUTH_TRUNC
),
694 strxf_algotype(XFRMA_ALG_AEAD
),
695 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
699 fprintf(stderr
, "ALGO-TYPE value \"%s\" is required with XFRM-PROTO value \"%s\"\n",
700 strxf_algotype(XFRMA_ALG_COMP
),
701 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
707 if (ealgop
|| aalgop
|| aeadop
|| calgop
) {
708 fprintf(stderr
, "ALGO is invalid with XFRM-PROTO value \"%s\"\n",
709 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
714 if (xfrm_xfrmproto_is_ro(req
.xsinfo
.id
.proto
)) {
715 switch (req
.xsinfo
.mode
) {
716 case XFRM_MODE_ROUTEOPTIMIZATION
:
717 case XFRM_MODE_IN_TRIGGER
:
720 fprintf(stderr
, "\"mode\" is required with XFRM-PROTO value \"%s\"\n",
721 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
724 fprintf(stderr
, "MODE value is invalid with XFRM-PROTO value \"%s\"\n",
725 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
730 fprintf(stderr
, "\"coa\" is required with XFRM-PROTO value \"%s\"\n",
731 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
736 fprintf(stderr
, "\"coa\" is invalid with XFRM-PROTO value \"%s\"\n",
737 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
743 addattr32(&req
.n
, sizeof(req
.buf
), XFRMA_OUTPUT_MARK
, output_mark
);
745 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
748 if (req
.xsinfo
.family
== AF_UNSPEC
)
749 req
.xsinfo
.family
= AF_INET
;
751 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
759 static int xfrm_state_allocspi(int argc
, char **argv
)
761 struct rtnl_handle rth
;
764 struct xfrm_userspi_info xspi
;
765 char buf
[RTA_BUF_SIZE
];
767 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xspi
)),
768 .n
.nlmsg_flags
= NLM_F_REQUEST
,
769 .n
.nlmsg_type
= XFRM_MSG_ALLOCSPI
,
770 .xspi
.info
.family
= preferred_family
,
772 .xspi
.lft
.soft_byte_limit
= XFRM_INF
,
773 .xspi
.lft
.hard_byte_limit
= XFRM_INF
,
774 .xspi
.lft
.soft_packet_limit
= XFRM_INF
,
775 .xspi
.lft
.hard_packet_limit
= XFRM_INF
,
781 struct xfrm_mark mark
= {0, 0};
782 struct nlmsghdr
*answer
;
785 if (strcmp(*argv
, "mode") == 0) {
787 xfrm_mode_parse(&req
.xspi
.info
.mode
, &argc
, &argv
);
788 } else if (strcmp(*argv
, "mark") == 0) {
789 xfrm_parse_mark(&mark
, &argc
, &argv
);
790 } else if (strcmp(*argv
, "reqid") == 0) {
792 xfrm_reqid_parse(&req
.xspi
.info
.reqid
, &argc
, &argv
);
793 } else if (strcmp(*argv
, "seq") == 0) {
795 xfrm_seq_parse(&req
.xspi
.info
.seq
, &argc
, &argv
);
796 } else if (strcmp(*argv
, "min") == 0) {
798 duparg("min", *argv
);
803 if (get_u32(&req
.xspi
.min
, *argv
, 0))
804 invarg("value after \"min\" is invalid", *argv
);
805 } else if (strcmp(*argv
, "max") == 0) {
807 duparg("max", *argv
);
812 if (get_u32(&req
.xspi
.max
, *argv
, 0))
813 invarg("value after \"max\" is invalid", *argv
);
815 /* try to assume ID */
817 invarg("unknown", *argv
);
821 xfrm_id_parse(&req
.xspi
.info
.saddr
, &req
.xspi
.info
.id
,
822 &req
.xspi
.info
.family
, 0, &argc
, &argv
);
823 if (req
.xspi
.info
.id
.spi
) {
824 fprintf(stderr
, "\"spi\" is invalid\n");
827 if (preferred_family
== AF_UNSPEC
)
828 preferred_family
= req
.xspi
.info
.family
;
834 fprintf(stderr
, "Not enough information: ID is required\n");
840 fprintf(stderr
, "\"max\" is missing\n");
843 if (req
.xspi
.min
> req
.xspi
.max
) {
844 fprintf(stderr
, "value after \"min\" is larger than value after \"max\"\n");
849 fprintf(stderr
, "\"min\" is missing\n");
853 /* XXX: Default value defined in PF_KEY;
854 * See kernel's net/key/af_key.c(pfkey_getspi).
856 req
.xspi
.min
= 0x100;
857 req
.xspi
.max
= 0x0fffffff;
859 /* XXX: IPCOMP spi is 16-bits;
860 * See kernel's net/xfrm/xfrm_user(verify_userspi_info).
862 if (req
.xspi
.info
.id
.proto
== IPPROTO_COMP
)
863 req
.xspi
.max
= 0xffff;
866 if (mark
.m
& mark
.v
) {
867 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
868 (void *)&mark
, sizeof(mark
));
870 fprintf(stderr
, "XFRMA_MARK failed\n");
875 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
878 if (req
.xspi
.info
.family
== AF_UNSPEC
)
879 req
.xspi
.info
.family
= AF_INET
;
882 if (rtnl_talk(&rth
, &req
.n
, &answer
) < 0)
885 if (xfrm_state_print(answer
, (void *)stdout
) < 0) {
886 fprintf(stderr
, "An error :-)\n");
896 static int xfrm_state_filter_match(struct xfrm_usersa_info
*xsinfo
)
901 if (filter
.xsinfo
.family
!= AF_UNSPEC
&&
902 filter
.xsinfo
.family
!= xsinfo
->family
)
905 if (filter
.id_src_mask
)
906 if (xfrm_addr_match(&xsinfo
->saddr
, &filter
.xsinfo
.saddr
,
909 if (filter
.id_dst_mask
)
910 if (xfrm_addr_match(&xsinfo
->id
.daddr
, &filter
.xsinfo
.id
.daddr
,
913 if ((xsinfo
->id
.proto
^filter
.xsinfo
.id
.proto
)&filter
.id_proto_mask
)
915 if ((xsinfo
->id
.spi
^filter
.xsinfo
.id
.spi
)&filter
.id_spi_mask
)
917 if ((xsinfo
->mode
^filter
.xsinfo
.mode
)&filter
.mode_mask
)
919 if ((xsinfo
->reqid
^filter
.xsinfo
.reqid
)&filter
.reqid_mask
)
921 if (filter
.state_flags_mask
)
922 if ((xsinfo
->flags
& filter
.xsinfo
.flags
) == 0)
928 static int __do_xfrm_state_print(struct nlmsghdr
*n
, void *arg
, bool nokeys
)
930 FILE *fp
= (FILE *)arg
;
931 struct rtattr
*tb
[XFRMA_MAX
+1];
933 struct xfrm_usersa_info
*xsinfo
= NULL
;
934 struct xfrm_user_expire
*xexp
= NULL
;
935 struct xfrm_usersa_id
*xsid
= NULL
;
936 int len
= n
->nlmsg_len
;
938 if (n
->nlmsg_type
!= XFRM_MSG_NEWSA
&&
939 n
->nlmsg_type
!= XFRM_MSG_DELSA
&&
940 n
->nlmsg_type
!= XFRM_MSG_UPDSA
&&
941 n
->nlmsg_type
!= XFRM_MSG_EXPIRE
) {
942 fprintf(stderr
, "Not a state: %08x %08x %08x\n",
943 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
947 if (n
->nlmsg_type
== XFRM_MSG_DELSA
) {
948 /* Don't blame me for this .. Herbert made me do it */
949 xsid
= NLMSG_DATA(n
);
950 len
-= NLMSG_SPACE(sizeof(*xsid
));
951 } else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
) {
952 xexp
= NLMSG_DATA(n
);
953 xsinfo
= &xexp
->state
;
954 len
-= NLMSG_SPACE(sizeof(*xexp
));
957 xsinfo
= NLMSG_DATA(n
);
958 len
-= NLMSG_SPACE(sizeof(*xsinfo
));
962 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
966 if (xsinfo
&& !xfrm_state_filter_match(xsinfo
))
969 if (n
->nlmsg_type
== XFRM_MSG_DELSA
)
970 fprintf(fp
, "Deleted ");
971 else if (n
->nlmsg_type
== XFRM_MSG_UPDSA
)
972 fprintf(fp
, "Updated ");
973 else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
)
974 fprintf(fp
, "Expired ");
976 if (n
->nlmsg_type
== XFRM_MSG_DELSA
)
977 rta
= XFRMSID_RTA(xsid
);
978 else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
)
979 rta
= XFRMEXP_RTA(xexp
);
981 rta
= XFRMS_RTA(xsinfo
);
983 parse_rtattr(tb
, XFRMA_MAX
, rta
, len
);
985 if (n
->nlmsg_type
== XFRM_MSG_DELSA
) {
986 /* xfrm_policy_id_print(); */
989 fprintf(stderr
, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
992 if (RTA_PAYLOAD(tb
[XFRMA_SA
]) < sizeof(*xsinfo
)) {
993 fprintf(stderr
, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
996 xsinfo
= RTA_DATA(tb
[XFRMA_SA
]);
999 xfrm_state_info_print(xsinfo
, tb
, fp
, NULL
, NULL
, nokeys
);
1001 if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
) {
1003 fprintf(fp
, "hard %u", xexp
->hard
);
1004 fprintf(fp
, "%s", _SL_
);
1014 int xfrm_state_print(struct nlmsghdr
*n
, void *arg
)
1016 return __do_xfrm_state_print(n
, arg
, false);
1019 int xfrm_state_print_nokeys(struct nlmsghdr
*n
, void *arg
)
1021 return __do_xfrm_state_print(n
, arg
, true);
1024 static int xfrm_state_get_or_delete(int argc
, char **argv
, int delete)
1026 struct rtnl_handle rth
;
1029 struct xfrm_usersa_id xsid
;
1030 char buf
[RTA_BUF_SIZE
];
1032 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsid
)),
1033 .n
.nlmsg_flags
= NLM_F_REQUEST
,
1034 .n
.nlmsg_type
= delete ? XFRM_MSG_DELSA
: XFRM_MSG_GETSA
,
1035 .xsid
.family
= preferred_family
,
1039 struct xfrm_mark mark
= {0, 0};
1042 xfrm_address_t saddr
;
1044 if (strcmp(*argv
, "mark") == 0) {
1045 xfrm_parse_mark(&mark
, &argc
, &argv
);
1048 invarg("unknown", *argv
);
1052 memset(&id
, 0, sizeof(id
));
1053 memset(&saddr
, 0, sizeof(saddr
));
1054 xfrm_id_parse(&saddr
, &id
, &req
.xsid
.family
, 0,
1057 memcpy(&req
.xsid
.daddr
, &id
.daddr
, sizeof(req
.xsid
.daddr
));
1058 req
.xsid
.spi
= id
.spi
;
1059 req
.xsid
.proto
= id
.proto
;
1061 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_SRCADDR
,
1062 (void *)&saddr
, sizeof(saddr
));
1068 if (mark
.m
& mark
.v
) {
1069 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
1070 (void *)&mark
, sizeof(mark
));
1072 fprintf(stderr
, "XFRMA_MARK failed\n");
1077 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1080 if (req
.xsid
.family
== AF_UNSPEC
)
1081 req
.xsid
.family
= AF_INET
;
1084 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
1087 struct nlmsghdr
*answer
;
1089 if (rtnl_talk(&rth
, &req
.n
, &answer
) < 0)
1092 if (xfrm_state_print(answer
, (void *)stdout
) < 0) {
1093 fprintf(stderr
, "An error :-)\n");
1106 * With an existing state of nlmsg, make new nlmsg for deleting the state
1107 * and store it to buffer.
1109 static int xfrm_state_keep(struct nlmsghdr
*n
, void *arg
)
1111 struct xfrm_buffer
*xb
= (struct xfrm_buffer
*)arg
;
1112 struct rtnl_handle
*rth
= xb
->rth
;
1113 struct xfrm_usersa_info
*xsinfo
= NLMSG_DATA(n
);
1114 int len
= n
->nlmsg_len
;
1115 struct nlmsghdr
*new_n
;
1116 struct xfrm_usersa_id
*xsid
;
1117 struct rtattr
*tb
[XFRMA_MAX
+1];
1119 if (n
->nlmsg_type
!= XFRM_MSG_NEWSA
) {
1120 fprintf(stderr
, "Not a state: %08x %08x %08x\n",
1121 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
1125 len
-= NLMSG_LENGTH(sizeof(*xsinfo
));
1127 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
1131 if (!xfrm_state_filter_match(xsinfo
))
1134 if (xsinfo
->id
.proto
== IPPROTO_IPIP
||
1135 xsinfo
->id
.proto
== IPPROTO_IPV6
)
1138 if (xb
->offset
> xb
->size
) {
1139 fprintf(stderr
, "State buffer overflow\n");
1143 new_n
= (struct nlmsghdr
*)(xb
->buf
+ xb
->offset
);
1144 new_n
->nlmsg_len
= NLMSG_LENGTH(sizeof(*xsid
));
1145 new_n
->nlmsg_flags
= NLM_F_REQUEST
;
1146 new_n
->nlmsg_type
= XFRM_MSG_DELSA
;
1147 new_n
->nlmsg_seq
= ++rth
->seq
;
1149 xsid
= NLMSG_DATA(new_n
);
1150 xsid
->family
= xsinfo
->family
;
1151 memcpy(&xsid
->daddr
, &xsinfo
->id
.daddr
, sizeof(xsid
->daddr
));
1152 xsid
->spi
= xsinfo
->id
.spi
;
1153 xsid
->proto
= xsinfo
->id
.proto
;
1155 addattr_l(new_n
, xb
->size
, XFRMA_SRCADDR
, &xsinfo
->saddr
,
1156 sizeof(xsid
->daddr
));
1158 parse_rtattr(tb
, XFRMA_MAX
, XFRMS_RTA(xsinfo
), len
);
1160 if (tb
[XFRMA_MARK
]) {
1161 int r
= addattr_l(new_n
, xb
->size
, XFRMA_MARK
,
1162 (void *)RTA_DATA(tb
[XFRMA_MARK
]), tb
[XFRMA_MARK
]->rta_len
);
1164 fprintf(stderr
, "%s: XFRMA_MARK failed\n", __func__
);
1169 xb
->offset
+= new_n
->nlmsg_len
;
1175 static int xfrm_state_list_or_deleteall(int argc
, char **argv
, int deleteall
)
1178 struct rtnl_handle rth
;
1179 bool nokeys
= false;
1181 if (argc
> 0 || preferred_family
!= AF_UNSPEC
)
1183 filter
.xsinfo
.family
= preferred_family
;
1186 if (strcmp(*argv
, "nokeys") == 0) {
1188 } else if (strcmp(*argv
, "mode") == 0) {
1190 xfrm_mode_parse(&filter
.xsinfo
.mode
, &argc
, &argv
);
1192 filter
.mode_mask
= XFRM_FILTER_MASK_FULL
;
1194 } else if (strcmp(*argv
, "reqid") == 0) {
1196 xfrm_reqid_parse(&filter
.xsinfo
.reqid
, &argc
, &argv
);
1198 filter
.reqid_mask
= XFRM_FILTER_MASK_FULL
;
1200 } else if (strcmp(*argv
, "flag") == 0) {
1202 xfrm_state_flag_parse(&filter
.xsinfo
.flags
, &argc
, &argv
);
1204 filter
.state_flags_mask
= XFRM_FILTER_MASK_FULL
;
1208 invarg("unknown", *argv
);
1212 xfrm_id_parse(&filter
.xsinfo
.saddr
, &filter
.xsinfo
.id
,
1213 &filter
.xsinfo
.family
, 1, &argc
, &argv
);
1214 if (preferred_family
== AF_UNSPEC
)
1215 preferred_family
= filter
.xsinfo
.family
;
1220 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1224 struct xfrm_buffer xb
;
1225 char buf
[NLMSG_DELETEALL_BUF_SIZE
];
1229 xb
.size
= sizeof(buf
);
1232 for (i
= 0; ; i
++) {
1235 char buf
[NLMSG_BUF_SIZE
];
1237 .n
.nlmsg_len
= NLMSG_HDRLEN
,
1238 .n
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
1239 .n
.nlmsg_type
= XFRM_MSG_GETSA
,
1240 .n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
,
1247 fprintf(stderr
, "Delete-all round = %d\n", i
);
1249 if (rtnl_send(&rth
, (void *)&req
, req
.n
.nlmsg_len
) < 0) {
1250 perror("Cannot send dump request");
1254 if (rtnl_dump_filter(&rth
, xfrm_state_keep
, &xb
) < 0) {
1255 fprintf(stderr
, "Delete-all terminated\n");
1258 if (xb
.nlmsg_count
== 0) {
1260 fprintf(stderr
, "Delete-all completed\n");
1264 if (rtnl_send_check(&rth
, xb
.buf
, xb
.offset
) < 0) {
1265 perror("Failed to send delete-all request\n");
1269 fprintf(stderr
, "Delete-all nlmsg count = %d\n", xb
.nlmsg_count
);
1276 struct xfrm_address_filter addrfilter
= {
1277 .saddr
= filter
.xsinfo
.saddr
,
1278 .daddr
= filter
.xsinfo
.id
.daddr
,
1279 .family
= filter
.xsinfo
.family
,
1280 .splen
= filter
.id_src_mask
,
1281 .dplen
= filter
.id_dst_mask
,
1285 char buf
[NLMSG_BUF_SIZE
];
1287 .n
.nlmsg_len
= NLMSG_HDRLEN
,
1288 .n
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
1289 .n
.nlmsg_type
= XFRM_MSG_GETSA
,
1290 .n
.nlmsg_seq
= rth
.dump
= ++rth
.seq
,
1293 if (filter
.xsinfo
.id
.proto
)
1294 addattr8(&req
.n
, sizeof(req
), XFRMA_PROTO
,
1295 filter
.xsinfo
.id
.proto
);
1296 addattr_l(&req
.n
, sizeof(req
), XFRMA_ADDRESS_FILTER
,
1297 &addrfilter
, sizeof(addrfilter
));
1299 if (rtnl_send(&rth
, (void *)&req
, req
.n
.nlmsg_len
) < 0) {
1300 perror("Cannot send dump request");
1304 rtnl_filter_t filter
= nokeys
?
1305 xfrm_state_print_nokeys
: xfrm_state_print
;
1306 if (rtnl_dump_filter(&rth
, filter
, stdout
) < 0) {
1307 fprintf(stderr
, "Dump terminated\n");
1317 static int print_sadinfo(struct nlmsghdr
*n
, void *arg
)
1319 FILE *fp
= (FILE *)arg
;
1320 __u32
*f
= NLMSG_DATA(n
);
1321 struct rtattr
*tb
[XFRMA_SAD_MAX
+1];
1323 int len
= n
->nlmsg_len
;
1325 len
-= NLMSG_LENGTH(sizeof(__u32
));
1327 fprintf(stderr
, "SADinfo: Wrong len %d\n", len
);
1331 rta
= XFRMSAPD_RTA(f
);
1332 parse_rtattr(tb
, XFRMA_SAD_MAX
, rta
, len
);
1334 if (tb
[XFRMA_SAD_CNT
]) {
1337 fprintf(fp
, "\t SAD");
1338 cnt
= rta_getattr_u32(tb
[XFRMA_SAD_CNT
]);
1339 fprintf(fp
, " count %u", cnt
);
1341 fprintf(fp
, "BAD SAD info returned\n");
1346 if (tb
[XFRMA_SAD_HINFO
]) {
1347 struct xfrmu_sadhinfo
*si
;
1349 if (RTA_PAYLOAD(tb
[XFRMA_SAD_HINFO
]) < sizeof(*si
)) {
1350 fprintf(fp
, "BAD SAD length returned\n");
1354 si
= RTA_DATA(tb
[XFRMA_SAD_HINFO
]);
1355 fprintf(fp
, " (buckets ");
1356 fprintf(fp
, "count %d", si
->sadhcnt
);
1357 fprintf(fp
, " Max %d", si
->sadhmcnt
);
1366 static int xfrm_sad_getinfo(int argc
, char **argv
)
1368 struct rtnl_handle rth
;
1373 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.flags
)),
1374 .n
.nlmsg_flags
= NLM_F_REQUEST
,
1375 .n
.nlmsg_type
= XFRM_MSG_GETSADINFO
,
1376 .flags
= 0XFFFFFFFF,
1378 struct nlmsghdr
*answer
;
1380 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1383 if (rtnl_talk(&rth
, &req
.n
, &answer
) < 0)
1386 print_sadinfo(answer
, (void *)stdout
);
1394 static int xfrm_state_flush(int argc
, char **argv
)
1396 struct rtnl_handle rth
;
1399 struct xfrm_usersa_flush xsf
;
1401 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsf
)),
1402 .n
.nlmsg_flags
= NLM_F_REQUEST
,
1403 .n
.nlmsg_type
= XFRM_MSG_FLUSHSA
,
1405 char *protop
= NULL
;
1408 if (strcmp(*argv
, "proto") == 0) {
1412 duparg("proto", *argv
);
1417 ret
= xfrm_xfrmproto_getbyname(*argv
);
1419 invarg("XFRM-PROTO value is invalid", *argv
);
1421 req
.xsf
.proto
= (__u8
)ret
;
1423 invarg("unknown", *argv
);
1428 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1432 fprintf(stderr
, "Flush state with XFRM-PROTO value \"%s\"\n",
1433 strxf_xfrmproto(req
.xsf
.proto
));
1435 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
1443 int do_xfrm_state(int argc
, char **argv
)
1446 return xfrm_state_list_or_deleteall(0, NULL
, 0);
1448 if (matches(*argv
, "add") == 0)
1449 return xfrm_state_modify(XFRM_MSG_NEWSA
, 0,
1451 if (matches(*argv
, "update") == 0)
1452 return xfrm_state_modify(XFRM_MSG_UPDSA
, 0,
1454 if (matches(*argv
, "allocspi") == 0)
1455 return xfrm_state_allocspi(argc
-1, argv
+1);
1456 if (matches(*argv
, "delete") == 0)
1457 return xfrm_state_get_or_delete(argc
-1, argv
+1, 1);
1458 if (matches(*argv
, "deleteall") == 0 || matches(*argv
, "delall") == 0)
1459 return xfrm_state_list_or_deleteall(argc
-1, argv
+1, 1);
1460 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
1461 || matches(*argv
, "lst") == 0)
1462 return xfrm_state_list_or_deleteall(argc
-1, argv
+1, 0);
1463 if (matches(*argv
, "get") == 0)
1464 return xfrm_state_get_or_delete(argc
-1, argv
+1, 0);
1465 if (matches(*argv
, "flush") == 0)
1466 return xfrm_state_flush(argc
-1, argv
+1);
1467 if (matches(*argv
, "count") == 0) {
1468 return xfrm_sad_getinfo(argc
, argv
);
1470 if (matches(*argv
, "help") == 0)
1472 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv
);