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
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
, " [ reqid REQID ] [ seq SEQ ] [ replay-window SIZE ] [ flag FLAG-LIST ]\n");
60 fprintf(stderr
, " [ encap ENCAP ] [ sel SELECTOR ] [ LIMIT-LIST ]\n");
61 fprintf(stderr
, "Usage: ip xfrm state allocspi ID [ mode MODE ] [ reqid REQID ] [ seq SEQ ]\n");
62 fprintf(stderr
, " [ min SPI max SPI ]\n");
63 fprintf(stderr
, "Usage: ip xfrm state { delete | get } ID\n");
64 fprintf(stderr
, "Usage: ip xfrm state { deleteall | list } [ ID ] [ mode MODE ] [ reqid REQID ]\n");
65 fprintf(stderr
, " [ flag FLAG_LIST ]\n");
66 fprintf(stderr
, "Usage: ip xfrm state flush [ proto XFRM_PROTO ]\n");
68 fprintf(stderr
, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n");
69 //fprintf(stderr, "XFRM_PROTO := [ esp | ah | comp ]\n");
70 fprintf(stderr
, "XFRM_PROTO := [ ");
71 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_ESP
));
72 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_AH
));
73 fprintf(stderr
, "%s ", strxf_xfrmproto(IPPROTO_COMP
));
74 fprintf(stderr
, "]\n");
76 //fprintf(stderr, "SPI - security parameter index(default=0)\n");
78 fprintf(stderr
, "MODE := [ transport | tunnel ](default=transport)\n");
79 //fprintf(stderr, "REQID - number(default=0)\n");
81 fprintf(stderr
, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
82 fprintf(stderr
, "FLAG := [ noecn | decap-dscp ]\n");
84 fprintf(stderr
, "ENCAP := ENCAP-TYPE SPORT DPORT OADDR\n");
85 fprintf(stderr
, "ENCAP-TYPE := espinudp | espinudp-nonike\n");
87 fprintf(stderr
, "ALGO-LIST := [ ALGO-LIST ] | [ ALGO ]\n");
88 fprintf(stderr
, "ALGO := ALGO_TYPE ALGO_NAME ALGO_KEY\n");
89 fprintf(stderr
, "ALGO_TYPE := [ ");
90 fprintf(stderr
, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT
));
91 fprintf(stderr
, "%s | ", strxf_algotype(XFRMA_ALG_AUTH
));
92 fprintf(stderr
, "%s ", strxf_algotype(XFRMA_ALG_COMP
));
93 fprintf(stderr
, "]\n");
95 //fprintf(stderr, "ALGO_NAME - algorithm name\n");
96 //fprintf(stderr, "ALGO_KEY - algorithm key\n");
98 fprintf(stderr
, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ UPSPEC ] [ dev DEV ]\n");
100 fprintf(stderr
, "UPSPEC := proto PROTO [ [ sport PORT ] [ dport PORT ] |\n");
101 fprintf(stderr
, " [ type NUMBER ] [ code NUMBER ] ]\n");
104 //fprintf(stderr, "DEV - device name(default=none)\n");
105 fprintf(stderr
, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
106 fprintf(stderr
, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
107 fprintf(stderr
, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] COUNT ]\n");
111 static int xfrm_algo_parse(struct xfrm_algo
*alg
, enum xfrm_attr_type_t type
,
112 char *name
, char *key
, int max
)
115 int slen
= strlen(key
);
118 /* XXX: verifying both name and key is required! */
119 fprintf(stderr
, "warning: ALGONAME/ALGOKEY will send to kernel promiscuously!(verifying them isn't implemented yet)\n");
122 strncpy(alg
->alg_name
, name
, sizeof(alg
->alg_name
));
124 if (slen
> 2 && strncmp(key
, "0x", 2) == 0) {
125 /* split two chars "0x" from the top */
131 /* Converting hexadecimal numbered string into real key;
132 * Convert each two chars into one char(value). If number
133 * of the length is odd, add zero on the top for rounding.
136 /* calculate length of the converted values(real key) */
137 len
= (plen
+ 1) / 2;
139 invarg("\"ALGOKEY\" makes buffer overflow\n", key
);
141 for (i
= - (plen
% 2), j
= 0; j
< len
; i
+= 2, j
++) {
145 vbuf
[0] = i
>= 0 ? p
[i
] : '0';
149 if (get_u8(&val
, vbuf
, 16))
150 invarg("\"ALGOKEY\" is invalid", key
);
152 alg
->alg_key
[j
] = val
;
158 invarg("\"ALGOKEY\" makes buffer overflow\n", key
);
160 strncpy(alg
->alg_key
, key
, len
);
164 alg
->alg_key_len
= len
* 8;
169 static int xfrm_seq_parse(__u32
*seq
, int *argcp
, char ***argvp
)
172 char **argv
= *argvp
;
174 if (get_u32(seq
, *argv
, 0))
175 invarg("\"SEQ\" is invalid", *argv
);
185 static int xfrm_state_flag_parse(__u8
*flags
, int *argcp
, char ***argvp
)
188 char **argv
= *argvp
;
189 int len
= strlen(*argv
);
191 if (len
> 2 && strncmp(*argv
, "0x", 2) == 0) {
194 if (get_u8(&val
, *argv
, 16))
195 invarg("\"FLAG\" is invalid", *argv
);
199 if (strcmp(*argv
, "noecn") == 0)
200 *flags
|= XFRM_STATE_NOECN
;
201 else if (strcmp(*argv
, "decap-dscp") == 0)
202 *flags
|= XFRM_STATE_DECAP_DSCP
;
204 PREV_ARG(); /* back track */
214 filter
.state_flags_mask
= XFRM_FILTER_MASK_FULL
;
222 static int xfrm_state_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
224 struct rtnl_handle rth
;
227 struct xfrm_usersa_info xsinfo
;
228 char buf
[RTA_BUF_SIZE
];
235 memset(&req
, 0, sizeof(req
));
237 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsinfo
));
238 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
239 req
.n
.nlmsg_type
= cmd
;
240 req
.xsinfo
.family
= preferred_family
;
242 req
.xsinfo
.lft
.soft_byte_limit
= XFRM_INF
;
243 req
.xsinfo
.lft
.hard_byte_limit
= XFRM_INF
;
244 req
.xsinfo
.lft
.soft_packet_limit
= XFRM_INF
;
245 req
.xsinfo
.lft
.hard_packet_limit
= XFRM_INF
;
248 if (strcmp(*argv
, "mode") == 0) {
250 xfrm_mode_parse(&req
.xsinfo
.mode
, &argc
, &argv
);
251 } else if (strcmp(*argv
, "reqid") == 0) {
253 xfrm_reqid_parse(&req
.xsinfo
.reqid
, &argc
, &argv
);
254 } else if (strcmp(*argv
, "seq") == 0) {
256 xfrm_seq_parse(&req
.xsinfo
.seq
, &argc
, &argv
);
257 } else if (strcmp(*argv
, "replay-window") == 0) {
259 if (get_u8(&req
.xsinfo
.replay_window
, *argv
, 0))
260 invarg("\"replay-window\" value is invalid", *argv
);
261 } else if (strcmp(*argv
, "flag") == 0) {
263 xfrm_state_flag_parse(&req
.xsinfo
.flags
, &argc
, &argv
);
264 } else if (strcmp(*argv
, "sel") == 0) {
266 xfrm_selector_parse(&req
.xsinfo
.sel
, &argc
, &argv
);
267 } else if (strcmp(*argv
, "limit") == 0) {
269 xfrm_lifetime_cfg_parse(&req
.xsinfo
.lft
, &argc
, &argv
);
270 } else if (strcmp(*argv
, "encap") == 0) {
271 struct xfrm_encap_tmpl encap
;
274 xfrm_encap_type_parse(&encap
.encap_type
, &argc
, &argv
);
276 if (get_u16(&encap
.encap_sport
, *argv
, 0))
277 invarg("\"encap\" sport value is invalid", *argv
);
278 encap
.encap_sport
= htons(encap
.encap_sport
);
280 if (get_u16(&encap
.encap_dport
, *argv
, 0))
281 invarg("\"encap\" dport value is invalid", *argv
);
282 encap
.encap_dport
= htons(encap
.encap_dport
);
284 get_addr(&oa
, *argv
, AF_UNSPEC
);
285 memcpy(&encap
.encap_oa
, &oa
.data
, sizeof(encap
.encap_oa
));
286 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_ENCAP
,
287 (void *)&encap
, sizeof(encap
));
289 /* try to assume ALGO */
290 int type
= xfrm_algotype_getbyname(*argv
);
292 case XFRMA_ALG_CRYPT
:
298 struct xfrm_algo alg
;
299 char buf
[XFRM_ALGO_KEY_BUF_SIZE
];
306 case XFRMA_ALG_CRYPT
:
308 duparg("ALGOTYPE", *argv
);
313 duparg("ALGOTYPE", *argv
);
318 duparg("ALGOTYPE", *argv
);
323 invarg("\"ALGOTYPE\" is invalid\n", *argv
);
336 memset(&alg
, 0, sizeof(alg
));
338 xfrm_algo_parse((void *)&alg
, type
, name
, key
,
340 len
= sizeof(struct xfrm_algo
) + alg
.alg
.alg_key_len
;
342 addattr_l(&req
.n
, sizeof(req
.buf
), type
,
347 /* try to assume ID */
349 invarg("unknown", *argv
);
353 xfrm_id_parse(&req
.xsinfo
.saddr
, &req
.xsinfo
.id
,
354 &req
.xsinfo
.family
, 0, &argc
, &argv
);
355 if (preferred_family
== AF_UNSPEC
)
356 preferred_family
= req
.xsinfo
.family
;
363 fprintf(stderr
, "Not enough information: \"ID\" is required\n");
367 if (ealgop
|| aalgop
|| calgop
) {
368 if (req
.xsinfo
.id
.proto
!= IPPROTO_ESP
&&
369 req
.xsinfo
.id
.proto
!= IPPROTO_AH
&&
370 req
.xsinfo
.id
.proto
!= IPPROTO_COMP
) {
371 fprintf(stderr
, "\"ALGO\" is invalid with proto=%s\n", strxf_xfrmproto(req
.xsinfo
.id
.proto
));
375 if (req
.xsinfo
.id
.proto
== IPPROTO_ESP
||
376 req
.xsinfo
.id
.proto
== IPPROTO_AH
||
377 req
.xsinfo
.id
.proto
== IPPROTO_COMP
) {
378 fprintf(stderr
, "\"ALGO\" is required with proto=%s\n", strxf_xfrmproto(req
.xsinfo
.id
.proto
));
383 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
386 if (req
.xsinfo
.family
== AF_UNSPEC
)
387 req
.xsinfo
.family
= AF_INET
;
389 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
397 static int xfrm_state_allocspi(int argc
, char **argv
)
399 struct rtnl_handle rth
;
402 struct xfrm_userspi_info xspi
;
403 char buf
[RTA_BUF_SIZE
];
408 char res_buf
[NLMSG_BUF_SIZE
];
409 struct nlmsghdr
*res_n
= (struct nlmsghdr
*)res_buf
;
411 memset(res_buf
, 0, sizeof(res_buf
));
413 memset(&req
, 0, sizeof(req
));
415 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xspi
));
416 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
417 req
.n
.nlmsg_type
= XFRM_MSG_ALLOCSPI
;
418 req
.xspi
.info
.family
= preferred_family
;
421 req
.xsinfo
.lft
.soft_byte_limit
= XFRM_INF
;
422 req
.xsinfo
.lft
.hard_byte_limit
= XFRM_INF
;
423 req
.xsinfo
.lft
.soft_packet_limit
= XFRM_INF
;
424 req
.xsinfo
.lft
.hard_packet_limit
= XFRM_INF
;
428 if (strcmp(*argv
, "mode") == 0) {
430 xfrm_mode_parse(&req
.xspi
.info
.mode
, &argc
, &argv
);
431 } else if (strcmp(*argv
, "reqid") == 0) {
433 xfrm_reqid_parse(&req
.xspi
.info
.reqid
, &argc
, &argv
);
434 } else if (strcmp(*argv
, "seq") == 0) {
436 xfrm_seq_parse(&req
.xspi
.info
.seq
, &argc
, &argv
);
437 } else if (strcmp(*argv
, "min") == 0) {
439 duparg("min", *argv
);
444 if (get_u32(&req
.xspi
.min
, *argv
, 0))
445 invarg("\"min\" value is invalid", *argv
);
446 } else if (strcmp(*argv
, "max") == 0) {
448 duparg("max", *argv
);
453 if (get_u32(&req
.xspi
.max
, *argv
, 0))
454 invarg("\"max\" value is invalid", *argv
);
456 /* try to assume ID */
458 invarg("unknown", *argv
);
462 xfrm_id_parse(&req
.xspi
.info
.saddr
, &req
.xspi
.info
.id
,
463 &req
.xspi
.info
.family
, 0, &argc
, &argv
);
464 if (req
.xspi
.info
.id
.spi
) {
465 fprintf(stderr
, "\"SPI\" must be zero\n");
468 if (preferred_family
== AF_UNSPEC
)
469 preferred_family
= req
.xspi
.info
.family
;
475 fprintf(stderr
, "Not enough information: \"ID\" is required\n");
481 fprintf(stderr
, "\"max\" is missing\n");
484 if (req
.xspi
.min
> req
.xspi
.max
) {
485 fprintf(stderr
, "\"min\" valie is larger than \"max\" one\n");
490 fprintf(stderr
, "\"min\" is missing\n");
494 /* XXX: Default value defined in PF_KEY;
495 * See kernel's net/key/af_key.c(pfkey_getspi).
497 req
.xspi
.min
= 0x100;
498 req
.xspi
.max
= 0x0fffffff;
500 /* XXX: IPCOMP spi is 16-bits;
501 * See kernel's net/xfrm/xfrm_user(verify_userspi_info).
503 if (req
.xspi
.info
.id
.proto
== IPPROTO_COMP
)
504 req
.xspi
.max
= 0xffff;
507 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
510 if (req
.xspi
.info
.family
== AF_UNSPEC
)
511 req
.xspi
.info
.family
= AF_INET
;
514 if (rtnl_talk(&rth
, &req
.n
, 0, 0, res_n
, NULL
, NULL
) < 0)
517 if (xfrm_state_print(NULL
, res_n
, (void*)stdout
) < 0) {
518 fprintf(stderr
, "An error :-)\n");
527 static int xfrm_state_filter_match(struct xfrm_usersa_info
*xsinfo
)
532 if (filter
.id_src_mask
)
533 if (xfrm_addr_match(&xsinfo
->saddr
, &filter
.xsinfo
.saddr
,
536 if (filter
.id_dst_mask
)
537 if (xfrm_addr_match(&xsinfo
->id
.daddr
, &filter
.xsinfo
.id
.daddr
,
540 if ((xsinfo
->id
.proto
^filter
.xsinfo
.id
.proto
)&filter
.id_proto_mask
)
542 if ((xsinfo
->id
.spi
^filter
.xsinfo
.id
.spi
)&filter
.id_spi_mask
)
544 if ((xsinfo
->mode
^filter
.xsinfo
.mode
)&filter
.mode_mask
)
546 if ((xsinfo
->reqid
^filter
.xsinfo
.reqid
)&filter
.reqid_mask
)
548 if (filter
.state_flags_mask
)
549 if ((xsinfo
->flags
& filter
.xsinfo
.flags
) == 0)
555 int xfrm_state_print(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
558 FILE *fp
= (FILE*)arg
;
559 struct rtattr
* tb
[XFRMA_MAX
+1];
561 struct xfrm_usersa_info
*xsinfo
= NULL
;
562 struct xfrm_user_expire
*xexp
= NULL
;
563 struct xfrm_usersa_id
*xsid
= NULL
;
564 int len
= n
->nlmsg_len
;
566 if (n
->nlmsg_type
!= XFRM_MSG_NEWSA
&&
567 n
->nlmsg_type
!= XFRM_MSG_DELSA
&&
568 n
->nlmsg_type
!= XFRM_MSG_UPDSA
&&
569 n
->nlmsg_type
!= XFRM_MSG_EXPIRE
) {
570 fprintf(stderr
, "Not a state: %08x %08x %08x\n",
571 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
575 if (n
->nlmsg_type
== XFRM_MSG_DELSA
) {
576 /* Dont blame me for this .. Herbert made me do it */
577 xsid
= NLMSG_DATA(n
);
578 len
-= NLMSG_SPACE(sizeof(*xsid
));
579 } else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
) {
580 xexp
= NLMSG_DATA(n
);
581 xsinfo
= &xexp
->state
;
582 len
-= NLMSG_SPACE(sizeof(*xexp
));
585 xsinfo
= NLMSG_DATA(n
);
586 len
-= NLMSG_SPACE(sizeof(*xsinfo
));
590 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
594 if (xsinfo
&& !xfrm_state_filter_match(xsinfo
))
597 if (n
->nlmsg_type
== XFRM_MSG_DELSA
)
598 fprintf(fp
, "Deleted ");
599 else if (n
->nlmsg_type
== XFRM_MSG_UPDSA
)
600 fprintf(fp
, "Updated ");
601 else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
)
602 fprintf(fp
, "Expired ");
604 if (n
->nlmsg_type
== XFRM_MSG_DELSA
)
605 rta
= XFRMSID_RTA(xsid
);
606 else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
)
607 rta
= XFRMEXP_RTA(xexp
);
609 rta
= XFRMS_RTA(xsinfo
);
611 parse_rtattr(tb
, XFRMA_MAX
, rta
, len
);
613 if (n
->nlmsg_type
== XFRM_MSG_DELSA
) {
614 //xfrm_policy_id_print();
617 fprintf(stderr
, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
620 if (RTA_PAYLOAD(tb
[XFRMA_SA
]) < sizeof(*xsinfo
)) {
621 fprintf(stderr
, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
624 xsinfo
= (struct xfrm_usersa_info
*)RTA_DATA(tb
[XFRMA_SA
]);
627 xfrm_state_info_print(xsinfo
, tb
, fp
, NULL
, NULL
);
629 if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
) {
631 fprintf(fp
, "hard %u", xexp
->hard
);
632 fprintf(fp
, "%s", _SL_
);
642 static int xfrm_state_get_or_delete(int argc
, char **argv
, int delete)
644 struct rtnl_handle rth
;
647 struct xfrm_usersa_id xsid
;
652 memset(&req
, 0, sizeof(req
));
654 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsid
));
655 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
656 req
.n
.nlmsg_type
= delete ? XFRM_MSG_DELSA
: XFRM_MSG_GETSA
;
657 req
.xsid
.family
= preferred_family
;
661 * XXX: Source address is not used and ignore it to follow
662 * XXX: a manner of setkey e.g. in the case of deleting/getting
663 * XXX: message of IPsec SA.
665 xfrm_address_t ignore_saddr
;
668 invarg("unknown", *argv
);
672 memset(&id
, 0, sizeof(id
));
673 xfrm_id_parse(&ignore_saddr
, &id
, &req
.xsid
.family
, 0,
676 memcpy(&req
.xsid
.daddr
, &id
.daddr
, sizeof(req
.xsid
.daddr
));
677 req
.xsid
.spi
= id
.spi
;
678 req
.xsid
.proto
= id
.proto
;
683 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
686 if (req
.xsid
.family
== AF_UNSPEC
)
687 req
.xsid
.family
= AF_INET
;
690 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
693 char buf
[NLMSG_BUF_SIZE
];
694 struct nlmsghdr
*res_n
= (struct nlmsghdr
*)buf
;
696 memset(buf
, 0, sizeof(buf
));
698 if (rtnl_talk(&rth
, &req
.n
, 0, 0, res_n
, NULL
, NULL
) < 0)
701 if (xfrm_state_print(NULL
, res_n
, (void*)stdout
) < 0) {
702 fprintf(stderr
, "An error :-)\n");
713 * With an existing state of nlmsg, make new nlmsg for deleting the state
714 * and store it to buffer.
716 static int xfrm_state_keep(const struct sockaddr_nl
*who
,
720 struct xfrm_buffer
*xb
= (struct xfrm_buffer
*)arg
;
721 struct rtnl_handle
*rth
= xb
->rth
;
722 struct xfrm_usersa_info
*xsinfo
= NLMSG_DATA(n
);
723 int len
= n
->nlmsg_len
;
724 struct nlmsghdr
*new_n
;
725 struct xfrm_usersa_id
*xsid
;
727 if (n
->nlmsg_type
!= XFRM_MSG_NEWSA
) {
728 fprintf(stderr
, "Not a state: %08x %08x %08x\n",
729 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
733 len
-= NLMSG_LENGTH(sizeof(*xsinfo
));
735 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
739 if (!xfrm_state_filter_match(xsinfo
))
742 if (xb
->offset
> xb
->size
) {
743 fprintf(stderr
, "State buffer overflow\n");
747 new_n
= (struct nlmsghdr
*)(xb
->buf
+ xb
->offset
);
748 new_n
->nlmsg_len
= NLMSG_LENGTH(sizeof(*xsid
));
749 new_n
->nlmsg_flags
= NLM_F_REQUEST
;
750 new_n
->nlmsg_type
= XFRM_MSG_DELSA
;
751 new_n
->nlmsg_seq
= ++rth
->seq
;
753 xsid
= NLMSG_DATA(new_n
);
754 xsid
->family
= xsinfo
->family
;
755 memcpy(&xsid
->daddr
, &xsinfo
->id
.daddr
, sizeof(xsid
->daddr
));
756 xsid
->spi
= xsinfo
->id
.spi
;
757 xsid
->proto
= xsinfo
->id
.proto
;
759 xb
->offset
+= new_n
->nlmsg_len
;
765 static int xfrm_state_list_or_deleteall(int argc
, char **argv
, int deleteall
)
768 struct rtnl_handle rth
;
772 filter
.xsinfo
.family
= preferred_family
;
775 if (strcmp(*argv
, "mode") == 0) {
777 xfrm_mode_parse(&filter
.xsinfo
.mode
, &argc
, &argv
);
779 filter
.mode_mask
= XFRM_FILTER_MASK_FULL
;
781 } else if (strcmp(*argv
, "reqid") == 0) {
783 xfrm_reqid_parse(&filter
.xsinfo
.reqid
, &argc
, &argv
);
785 filter
.reqid_mask
= XFRM_FILTER_MASK_FULL
;
787 } else if (strcmp(*argv
, "flag") == 0) {
789 xfrm_state_flag_parse(&filter
.xsinfo
.flags
, &argc
, &argv
);
791 filter
.state_flags_mask
= XFRM_FILTER_MASK_FULL
;
795 invarg("unknown", *argv
);
799 xfrm_id_parse(&filter
.xsinfo
.saddr
, &filter
.xsinfo
.id
,
800 &filter
.xsinfo
.family
, 1, &argc
, &argv
);
801 if (preferred_family
== AF_UNSPEC
)
802 preferred_family
= filter
.xsinfo
.family
;
807 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
811 struct xfrm_buffer xb
;
812 char buf
[NLMSG_DELETEALL_BUF_SIZE
];
816 xb
.size
= sizeof(buf
);
824 fprintf(stderr
, "Delete-all round = %d\n", i
);
826 if (rtnl_wilddump_request(&rth
, preferred_family
, XFRM_MSG_GETSA
) < 0) {
827 perror("Cannot send dump request");
831 if (rtnl_dump_filter(&rth
, xfrm_state_keep
, &xb
, NULL
, NULL
) < 0) {
832 fprintf(stderr
, "Delete-all terminated\n");
835 if (xb
.nlmsg_count
== 0) {
837 fprintf(stderr
, "Delete-all completed\n");
841 if (rtnl_send(&rth
, xb
.buf
, xb
.offset
) < 0) {
842 perror("Failed to send delete-all request\n");
846 fprintf(stderr
, "Delete-all nlmsg count = %d\n", xb
.nlmsg_count
);
853 if (rtnl_wilddump_request(&rth
, preferred_family
, XFRM_MSG_GETSA
) < 0) {
854 perror("Cannot send dump request");
858 if (rtnl_dump_filter(&rth
, xfrm_state_print
, stdout
, NULL
, NULL
) < 0) {
859 fprintf(stderr
, "Dump terminated\n");
869 static int xfrm_state_flush(int argc
, char **argv
)
871 struct rtnl_handle rth
;
874 struct xfrm_usersa_flush xsf
;
878 memset(&req
, 0, sizeof(req
));
880 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsf
));
881 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
882 req
.n
.nlmsg_type
= XFRM_MSG_FLUSHSA
;
883 req
.xsf
.proto
= IPSEC_PROTO_ANY
;
886 if (strcmp(*argv
, "proto") == 0) {
890 duparg("proto", *argv
);
895 ret
= xfrm_xfrmproto_getbyname(*argv
);
897 invarg("\"XFRM_PROTO\" is invalid", *argv
);
899 req
.xsf
.proto
= (__u8
)ret
;
901 invarg("unknown", *argv
);
906 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
910 fprintf(stderr
, "Flush state proto=%s\n",
911 (req
.xsf
.proto
== IPSEC_PROTO_ANY
) ? "any" :
912 strxf_xfrmproto(req
.xsf
.proto
));
914 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
922 int do_xfrm_state(int argc
, char **argv
)
925 return xfrm_state_list_or_deleteall(0, NULL
, 0);
927 if (matches(*argv
, "add") == 0)
928 return xfrm_state_modify(XFRM_MSG_NEWSA
, 0,
930 if (matches(*argv
, "update") == 0)
931 return xfrm_state_modify(XFRM_MSG_UPDSA
, 0,
933 if (matches(*argv
, "allocspi") == 0)
934 return xfrm_state_allocspi(argc
-1, argv
+1);
935 if (matches(*argv
, "delete") == 0)
936 return xfrm_state_get_or_delete(argc
-1, argv
+1, 1);
937 if (matches(*argv
, "deleteall") == 0 || matches(*argv
, "delall") == 0)
938 return xfrm_state_list_or_deleteall(argc
-1, argv
+1, 1);
939 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
940 || matches(*argv
, "lst") == 0)
941 return xfrm_state_list_or_deleteall(argc
-1, argv
+1, 0);
942 if (matches(*argv
, "get") == 0)
943 return xfrm_state_get_or_delete(argc
-1, argv
+1, 0);
944 if (matches(*argv
, "flush") == 0)
945 return xfrm_state_flush(argc
-1, argv
+1);
946 if (matches(*argv
, "help") == 0)
948 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv
);