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 [ XFRM_OPT ] [ ctx SEC_CTX ] [ mode MODE ]\n");
60 fprintf(stderr
, " [ reqid REQID ] [ seq SEQ ] [ replay-window SIZE ] [ flag FLAG-LIST ]\n");
61 fprintf(stderr
, " [ encap ENCAP ] [ sel SELECTOR ] [ replay-seq SEQ ]\n");
62 fprintf(stderr
, " [ replay-oseq SEQ ] [ LIMIT-LIST ]\n");
63 fprintf(stderr
, "Usage: ip xfrm state allocspi ID [ mode MODE ] [ reqid REQID ] [ seq SEQ ]\n");
64 fprintf(stderr
, " [ min SPI max SPI ]\n");
65 fprintf(stderr
, "Usage: ip xfrm state { delete | get } ID\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");
71 fprintf(stderr
, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ] [mark MARK [mask MASK]]\n");
72 //fprintf(stderr, "XFRM_PROTO := [ esp | ah | comp ]\n");
73 fprintf(stderr
, "XFRM_PROTO := [ ");
74 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_ESP
));
75 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_AH
));
76 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_COMP
));
77 fprintf(stderr
, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING
));
78 fprintf(stderr
, "%s ", strxf_xfrmproto(IPPROTO_DSTOPTS
));
79 fprintf(stderr
, "]\n");
81 //fprintf(stderr, "SPI - security parameter index(default=0)\n");
83 fprintf(stderr
, "MODE := [ transport | tunnel | ro | beet ](default=transport)\n");
84 //fprintf(stderr, "REQID - number(default=0)\n");
86 fprintf(stderr
, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
87 fprintf(stderr
, "FLAG := [ noecn | decap-dscp | nopmtudisc | wildrecv | icmp | af-unspec ]\n");
89 fprintf(stderr
, "ENCAP := ENCAP-TYPE SPORT DPORT OADDR\n");
90 fprintf(stderr
, "ENCAP-TYPE := espinudp | espinudp-nonike\n");
92 fprintf(stderr
, "ALGO-LIST := [ ALGO-LIST ] | [ ALGO ]\n");
93 fprintf(stderr
, "ALGO := ALGO_TYPE ALGO_NAME ALGO_KEY "
94 "[ ALGO_ICV_LEN ]\n");
95 fprintf(stderr
, "ALGO_TYPE := [ ");
96 fprintf(stderr
, "%s | ", strxf_algotype(XFRMA_ALG_AEAD
));
97 fprintf(stderr
, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT
));
98 fprintf(stderr
, "%s | ", strxf_algotype(XFRMA_ALG_AUTH
));
99 fprintf(stderr
, "%s ", strxf_algotype(XFRMA_ALG_COMP
));
100 fprintf(stderr
, "]\n");
102 //fprintf(stderr, "ALGO_NAME - algorithm name\n");
103 //fprintf(stderr, "ALGO_KEY - algorithm key\n");
105 fprintf(stderr
, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ UPSPEC ] [ dev DEV ]\n");
107 fprintf(stderr
, "UPSPEC := proto PROTO [ [ sport PORT ] [ dport PORT ] |\n");
108 fprintf(stderr
, " [ type NUMBER ] [ code NUMBER ] ]\n");
111 //fprintf(stderr, "DEV - device name(default=none)\n");
112 fprintf(stderr
, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
113 fprintf(stderr
, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
114 fprintf(stderr
, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] COUNT ]\n");
118 static int xfrm_algo_parse(struct xfrm_algo
*alg
, enum xfrm_attr_type_t type
,
119 char *name
, char *key
, char *buf
, int max
)
122 int slen
= strlen(key
);
125 /* XXX: verifying both name and key is required! */
126 fprintf(stderr
, "warning: ALGONAME/ALGOKEY will send to kernel promiscuously!(verifying them isn't implemented yet)\n");
129 strncpy(alg
->alg_name
, name
, sizeof(alg
->alg_name
));
131 if (slen
> 2 && strncmp(key
, "0x", 2) == 0) {
132 /* split two chars "0x" from the top */
138 /* Converting hexadecimal numbered string into real key;
139 * Convert each two chars into one char(value). If number
140 * of the length is odd, add zero on the top for rounding.
143 /* calculate length of the converted values(real key) */
144 len
= (plen
+ 1) / 2;
146 invarg("\"ALGOKEY\" makes buffer overflow\n", key
);
148 for (i
= - (plen
% 2), j
= 0; j
< len
; i
+= 2, j
++) {
152 vbuf
[0] = i
>= 0 ? p
[i
] : '0';
156 if (get_u8(&val
, vbuf
, 16))
157 invarg("\"ALGOKEY\" is invalid", key
);
165 invarg("\"ALGOKEY\" makes buffer overflow\n", key
);
167 strncpy(buf
, key
, len
);
171 alg
->alg_key_len
= len
* 8;
176 static int xfrm_seq_parse(__u32
*seq
, int *argcp
, char ***argvp
)
179 char **argv
= *argvp
;
181 if (get_u32(seq
, *argv
, 0))
182 invarg("\"SEQ\" is invalid", *argv
);
192 static int xfrm_state_flag_parse(__u8
*flags
, int *argcp
, char ***argvp
)
195 char **argv
= *argvp
;
196 int len
= strlen(*argv
);
198 if (len
> 2 && strncmp(*argv
, "0x", 2) == 0) {
201 if (get_u8(&val
, *argv
, 16))
202 invarg("\"FLAG\" is invalid", *argv
);
206 if (strcmp(*argv
, "noecn") == 0)
207 *flags
|= XFRM_STATE_NOECN
;
208 else if (strcmp(*argv
, "decap-dscp") == 0)
209 *flags
|= XFRM_STATE_DECAP_DSCP
;
210 else if (strcmp(*argv
, "nopmtudisc") == 0)
211 *flags
|= XFRM_STATE_NOPMTUDISC
;
212 else if (strcmp(*argv
, "wildrecv") == 0)
213 *flags
|= XFRM_STATE_WILDRECV
;
214 else if (strcmp(*argv
, "icmp") == 0)
215 *flags
|= XFRM_STATE_ICMP
;
216 else if (strcmp(*argv
, "af-unspec") == 0)
217 *flags
|= XFRM_STATE_AF_UNSPEC
;
219 PREV_ARG(); /* back track */
235 static int xfrm_state_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
237 struct rtnl_handle rth
;
240 struct xfrm_usersa_info xsinfo
;
241 char buf
[RTA_BUF_SIZE
];
243 struct xfrm_replay_state replay
;
251 struct xfrm_mark mark
= {0, 0};
253 struct xfrm_user_sec_ctx sctx
;
254 char str
[CTX_BUF_SIZE
];
257 memset(&req
, 0, sizeof(req
));
258 memset(&replay
, 0, sizeof(replay
));
259 memset(&ctx
, 0, sizeof(ctx
));
261 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsinfo
));
262 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
263 req
.n
.nlmsg_type
= cmd
;
264 req
.xsinfo
.family
= preferred_family
;
266 req
.xsinfo
.lft
.soft_byte_limit
= XFRM_INF
;
267 req
.xsinfo
.lft
.hard_byte_limit
= XFRM_INF
;
268 req
.xsinfo
.lft
.soft_packet_limit
= XFRM_INF
;
269 req
.xsinfo
.lft
.hard_packet_limit
= XFRM_INF
;
272 if (strcmp(*argv
, "mode") == 0) {
274 xfrm_mode_parse(&req
.xsinfo
.mode
, &argc
, &argv
);
275 } else if (strcmp(*argv
, "mark") == 0) {
276 xfrm_parse_mark(&mark
, &argc
, &argv
);
277 } else if (strcmp(*argv
, "reqid") == 0) {
279 xfrm_reqid_parse(&req
.xsinfo
.reqid
, &argc
, &argv
);
280 } else if (strcmp(*argv
, "seq") == 0) {
282 xfrm_seq_parse(&req
.xsinfo
.seq
, &argc
, &argv
);
283 } else if (strcmp(*argv
, "replay-window") == 0) {
285 if (get_u8(&req
.xsinfo
.replay_window
, *argv
, 0))
286 invarg("\"replay-window\" value is invalid", *argv
);
287 } else if (strcmp(*argv
, "replay-seq") == 0) {
289 if (get_u32(&replay
.seq
, *argv
, 0))
290 invarg("\"replay-seq\" value is invalid", *argv
);
291 } else if (strcmp(*argv
, "replay-oseq") == 0) {
293 if (get_u32(&replay
.oseq
, *argv
, 0))
294 invarg("\"replay-oseq\" value is invalid", *argv
);
295 } else if (strcmp(*argv
, "flag") == 0) {
297 xfrm_state_flag_parse(&req
.xsinfo
.flags
, &argc
, &argv
);
298 } else if (strcmp(*argv
, "sel") == 0) {
300 xfrm_selector_parse(&req
.xsinfo
.sel
, &argc
, &argv
);
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
:
368 struct xfrm_algo alg
;
369 struct xfrm_algo_aead aead
;
371 char buf
[XFRM_ALGO_KEY_BUF_SIZE
];
382 duparg("ALGOTYPE", *argv
);
385 case XFRMA_ALG_CRYPT
:
387 duparg("ALGOTYPE", *argv
);
392 duparg("ALGOTYPE", *argv
);
397 duparg("ALGOTYPE", *argv
);
402 invarg("\"ALGOTYPE\" is invalid\n", *argv
);
415 buf
= alg
.u
.alg
.alg_key
;
416 len
= sizeof(alg
.u
.alg
);
418 if (type
!= XFRMA_ALG_AEAD
)
422 missarg("ALGOICVLEN");
424 if (get_u32(&icvlen
, *argv
, 0))
425 invarg("\"aead\" ICV length is invalid",
427 alg
.u
.aead
.alg_icv_len
= icvlen
;
429 buf
= alg
.u
.aead
.alg_key
;
430 len
= sizeof(alg
.u
.aead
);
433 xfrm_algo_parse((void *)&alg
, type
, name
, key
,
434 buf
, sizeof(alg
.buf
));
435 len
+= alg
.u
.alg
.alg_key_len
;
437 addattr_l(&req
.n
, sizeof(req
.buf
), type
,
442 /* try to assume ID */
444 invarg("unknown", *argv
);
448 xfrm_id_parse(&req
.xsinfo
.saddr
, &req
.xsinfo
.id
,
449 &req
.xsinfo
.family
, 0, &argc
, &argv
);
450 if (preferred_family
== AF_UNSPEC
)
451 preferred_family
= req
.xsinfo
.family
;
457 if (replay
.seq
|| replay
.oseq
)
458 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_REPLAY_VAL
,
459 (void *)&replay
, sizeof(replay
));
462 fprintf(stderr
, "Not enough information: \"ID\" is required\n");
466 if (mark
.m
& mark
.v
) {
467 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
468 (void *)&mark
, sizeof(mark
));
470 fprintf(stderr
, "XFRMA_MARK failed\n");
475 switch (req
.xsinfo
.mode
) {
476 case XFRM_MODE_TRANSPORT
:
477 case XFRM_MODE_TUNNEL
:
478 if (!xfrm_xfrmproto_is_ipsec(req
.xsinfo
.id
.proto
)) {
479 fprintf(stderr
, "\"mode\" is invalid with proto=%s\n",
480 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
484 case XFRM_MODE_ROUTEOPTIMIZATION
:
485 case XFRM_MODE_IN_TRIGGER
:
486 if (!xfrm_xfrmproto_is_ro(req
.xsinfo
.id
.proto
)) {
487 fprintf(stderr
, "\"mode\" is invalid with proto=%s\n",
488 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
491 if (req
.xsinfo
.id
.spi
!= 0) {
492 fprintf(stderr
, "\"spi\" must be 0 with proto=%s\n",
493 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
501 if (aeadop
|| ealgop
|| aalgop
|| calgop
) {
502 if (!xfrm_xfrmproto_is_ipsec(req
.xsinfo
.id
.proto
)) {
503 fprintf(stderr
, "\"ALGO\" is invalid with proto=%s\n",
504 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
508 if (xfrm_xfrmproto_is_ipsec(req
.xsinfo
.id
.proto
)) {
509 fprintf(stderr
, "\"ALGO\" is required with proto=%s\n",
510 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
516 if (!xfrm_xfrmproto_is_ro(req
.xsinfo
.id
.proto
)) {
517 fprintf(stderr
, "\"coa\" is invalid with proto=%s\n",
518 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
522 if (xfrm_xfrmproto_is_ro(req
.xsinfo
.id
.proto
)) {
523 fprintf(stderr
, "\"coa\" is required with proto=%s\n",
524 strxf_xfrmproto(req
.xsinfo
.id
.proto
));
529 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
532 if (req
.xsinfo
.family
== AF_UNSPEC
)
533 req
.xsinfo
.family
= AF_INET
;
535 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
543 static int xfrm_state_allocspi(int argc
, char **argv
)
545 struct rtnl_handle rth
;
548 struct xfrm_userspi_info xspi
;
549 char buf
[RTA_BUF_SIZE
];
554 struct xfrm_mark mark
= {0, 0};
555 char res_buf
[NLMSG_BUF_SIZE
];
556 struct nlmsghdr
*res_n
= (struct nlmsghdr
*)res_buf
;
558 memset(res_buf
, 0, sizeof(res_buf
));
560 memset(&req
, 0, sizeof(req
));
562 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xspi
));
563 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
564 req
.n
.nlmsg_type
= XFRM_MSG_ALLOCSPI
;
565 req
.xspi
.info
.family
= preferred_family
;
568 req
.xsinfo
.lft
.soft_byte_limit
= XFRM_INF
;
569 req
.xsinfo
.lft
.hard_byte_limit
= XFRM_INF
;
570 req
.xsinfo
.lft
.soft_packet_limit
= XFRM_INF
;
571 req
.xsinfo
.lft
.hard_packet_limit
= XFRM_INF
;
575 if (strcmp(*argv
, "mode") == 0) {
577 xfrm_mode_parse(&req
.xspi
.info
.mode
, &argc
, &argv
);
578 } else if (strcmp(*argv
, "mark") == 0) {
579 xfrm_parse_mark(&mark
, &argc
, &argv
);
580 } else if (strcmp(*argv
, "reqid") == 0) {
582 xfrm_reqid_parse(&req
.xspi
.info
.reqid
, &argc
, &argv
);
583 } else if (strcmp(*argv
, "seq") == 0) {
585 xfrm_seq_parse(&req
.xspi
.info
.seq
, &argc
, &argv
);
586 } else if (strcmp(*argv
, "min") == 0) {
588 duparg("min", *argv
);
593 if (get_u32(&req
.xspi
.min
, *argv
, 0))
594 invarg("\"min\" value is invalid", *argv
);
595 } else if (strcmp(*argv
, "max") == 0) {
597 duparg("max", *argv
);
602 if (get_u32(&req
.xspi
.max
, *argv
, 0))
603 invarg("\"max\" value is invalid", *argv
);
605 /* try to assume ID */
607 invarg("unknown", *argv
);
611 xfrm_id_parse(&req
.xspi
.info
.saddr
, &req
.xspi
.info
.id
,
612 &req
.xspi
.info
.family
, 0, &argc
, &argv
);
613 if (req
.xspi
.info
.id
.spi
) {
614 fprintf(stderr
, "\"SPI\" must be zero\n");
617 if (preferred_family
== AF_UNSPEC
)
618 preferred_family
= req
.xspi
.info
.family
;
624 fprintf(stderr
, "Not enough information: \"ID\" is required\n");
630 fprintf(stderr
, "\"max\" is missing\n");
633 if (req
.xspi
.min
> req
.xspi
.max
) {
634 fprintf(stderr
, "\"min\" valie is larger than \"max\" one\n");
639 fprintf(stderr
, "\"min\" is missing\n");
643 /* XXX: Default value defined in PF_KEY;
644 * See kernel's net/key/af_key.c(pfkey_getspi).
646 req
.xspi
.min
= 0x100;
647 req
.xspi
.max
= 0x0fffffff;
649 /* XXX: IPCOMP spi is 16-bits;
650 * See kernel's net/xfrm/xfrm_user(verify_userspi_info).
652 if (req
.xspi
.info
.id
.proto
== IPPROTO_COMP
)
653 req
.xspi
.max
= 0xffff;
656 if (mark
.m
& mark
.v
) {
657 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
658 (void *)&mark
, sizeof(mark
));
660 fprintf(stderr
, "XFRMA_MARK failed\n");
665 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
668 if (req
.xspi
.info
.family
== AF_UNSPEC
)
669 req
.xspi
.info
.family
= AF_INET
;
672 if (rtnl_talk(&rth
, &req
.n
, 0, 0, res_n
, NULL
, NULL
) < 0)
675 if (xfrm_state_print(NULL
, res_n
, (void*)stdout
) < 0) {
676 fprintf(stderr
, "An error :-)\n");
685 static int xfrm_state_filter_match(struct xfrm_usersa_info
*xsinfo
)
690 if (filter
.id_src_mask
)
691 if (xfrm_addr_match(&xsinfo
->saddr
, &filter
.xsinfo
.saddr
,
694 if (filter
.id_dst_mask
)
695 if (xfrm_addr_match(&xsinfo
->id
.daddr
, &filter
.xsinfo
.id
.daddr
,
698 if ((xsinfo
->id
.proto
^filter
.xsinfo
.id
.proto
)&filter
.id_proto_mask
)
700 if ((xsinfo
->id
.spi
^filter
.xsinfo
.id
.spi
)&filter
.id_spi_mask
)
702 if ((xsinfo
->mode
^filter
.xsinfo
.mode
)&filter
.mode_mask
)
704 if ((xsinfo
->reqid
^filter
.xsinfo
.reqid
)&filter
.reqid_mask
)
706 if (filter
.state_flags_mask
)
707 if ((xsinfo
->flags
& filter
.xsinfo
.flags
) == 0)
713 int xfrm_state_print(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
716 FILE *fp
= (FILE*)arg
;
717 struct rtattr
* tb
[XFRMA_MAX
+1];
719 struct xfrm_usersa_info
*xsinfo
= NULL
;
720 struct xfrm_user_expire
*xexp
= NULL
;
721 struct xfrm_usersa_id
*xsid
= NULL
;
722 int len
= n
->nlmsg_len
;
724 if (n
->nlmsg_type
!= XFRM_MSG_NEWSA
&&
725 n
->nlmsg_type
!= XFRM_MSG_DELSA
&&
726 n
->nlmsg_type
!= XFRM_MSG_UPDSA
&&
727 n
->nlmsg_type
!= XFRM_MSG_EXPIRE
) {
728 fprintf(stderr
, "Not a state: %08x %08x %08x\n",
729 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
733 if (n
->nlmsg_type
== XFRM_MSG_DELSA
) {
734 /* Dont blame me for this .. Herbert made me do it */
735 xsid
= NLMSG_DATA(n
);
736 len
-= NLMSG_SPACE(sizeof(*xsid
));
737 } else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
) {
738 xexp
= NLMSG_DATA(n
);
739 xsinfo
= &xexp
->state
;
740 len
-= NLMSG_SPACE(sizeof(*xexp
));
743 xsinfo
= NLMSG_DATA(n
);
744 len
-= NLMSG_SPACE(sizeof(*xsinfo
));
748 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
752 if (xsinfo
&& !xfrm_state_filter_match(xsinfo
))
755 if (n
->nlmsg_type
== XFRM_MSG_DELSA
)
756 fprintf(fp
, "Deleted ");
757 else if (n
->nlmsg_type
== XFRM_MSG_UPDSA
)
758 fprintf(fp
, "Updated ");
759 else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
)
760 fprintf(fp
, "Expired ");
762 if (n
->nlmsg_type
== XFRM_MSG_DELSA
)
763 rta
= XFRMSID_RTA(xsid
);
764 else if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
)
765 rta
= XFRMEXP_RTA(xexp
);
767 rta
= XFRMS_RTA(xsinfo
);
769 parse_rtattr(tb
, XFRMA_MAX
, rta
, len
);
771 if (n
->nlmsg_type
== XFRM_MSG_DELSA
) {
772 //xfrm_policy_id_print();
775 fprintf(stderr
, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
778 if (RTA_PAYLOAD(tb
[XFRMA_SA
]) < sizeof(*xsinfo
)) {
779 fprintf(stderr
, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
782 xsinfo
= RTA_DATA(tb
[XFRMA_SA
]);
785 xfrm_state_info_print(xsinfo
, tb
, fp
, NULL
, NULL
);
787 if (n
->nlmsg_type
== XFRM_MSG_EXPIRE
) {
789 fprintf(fp
, "hard %u", xexp
->hard
);
790 fprintf(fp
, "%s", _SL_
);
800 static int xfrm_state_get_or_delete(int argc
, char **argv
, int delete)
802 struct rtnl_handle rth
;
805 struct xfrm_usersa_id xsid
;
806 char buf
[RTA_BUF_SIZE
];
810 struct xfrm_mark mark
= {0, 0};
812 memset(&req
, 0, sizeof(req
));
814 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsid
));
815 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
816 req
.n
.nlmsg_type
= delete ? XFRM_MSG_DELSA
: XFRM_MSG_GETSA
;
817 req
.xsid
.family
= preferred_family
;
820 xfrm_address_t saddr
;
822 if (strcmp(*argv
, "mark") == 0) {
823 xfrm_parse_mark(&mark
, &argc
, &argv
);
826 invarg("unknown", *argv
);
830 memset(&id
, 0, sizeof(id
));
831 memset(&saddr
, 0, sizeof(saddr
));
832 xfrm_id_parse(&saddr
, &id
, &req
.xsid
.family
, 0,
835 memcpy(&req
.xsid
.daddr
, &id
.daddr
, sizeof(req
.xsid
.daddr
));
836 req
.xsid
.spi
= id
.spi
;
837 req
.xsid
.proto
= id
.proto
;
839 addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_SRCADDR
,
840 (void *)&saddr
, sizeof(saddr
));
846 if (mark
.m
& mark
.v
) {
847 int r
= addattr_l(&req
.n
, sizeof(req
.buf
), XFRMA_MARK
,
848 (void *)&mark
, sizeof(mark
));
850 fprintf(stderr
, "XFRMA_MARK failed\n");
855 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
858 if (req
.xsid
.family
== AF_UNSPEC
)
859 req
.xsid
.family
= AF_INET
;
862 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
865 char buf
[NLMSG_BUF_SIZE
];
866 struct nlmsghdr
*res_n
= (struct nlmsghdr
*)buf
;
868 memset(buf
, 0, sizeof(buf
));
870 if (rtnl_talk(&rth
, &req
.n
, 0, 0, res_n
, NULL
, NULL
) < 0)
873 if (xfrm_state_print(NULL
, res_n
, (void*)stdout
) < 0) {
874 fprintf(stderr
, "An error :-)\n");
885 * With an existing state of nlmsg, make new nlmsg for deleting the state
886 * and store it to buffer.
888 static int xfrm_state_keep(const struct sockaddr_nl
*who
,
892 struct xfrm_buffer
*xb
= (struct xfrm_buffer
*)arg
;
893 struct rtnl_handle
*rth
= xb
->rth
;
894 struct xfrm_usersa_info
*xsinfo
= NLMSG_DATA(n
);
895 int len
= n
->nlmsg_len
;
896 struct nlmsghdr
*new_n
;
897 struct xfrm_usersa_id
*xsid
;
899 if (n
->nlmsg_type
!= XFRM_MSG_NEWSA
) {
900 fprintf(stderr
, "Not a state: %08x %08x %08x\n",
901 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
905 len
-= NLMSG_LENGTH(sizeof(*xsinfo
));
907 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
911 if (!xfrm_state_filter_match(xsinfo
))
914 if (xb
->offset
> xb
->size
) {
915 fprintf(stderr
, "State buffer overflow\n");
919 new_n
= (struct nlmsghdr
*)(xb
->buf
+ xb
->offset
);
920 new_n
->nlmsg_len
= NLMSG_LENGTH(sizeof(*xsid
));
921 new_n
->nlmsg_flags
= NLM_F_REQUEST
;
922 new_n
->nlmsg_type
= XFRM_MSG_DELSA
;
923 new_n
->nlmsg_seq
= ++rth
->seq
;
925 xsid
= NLMSG_DATA(new_n
);
926 xsid
->family
= xsinfo
->family
;
927 memcpy(&xsid
->daddr
, &xsinfo
->id
.daddr
, sizeof(xsid
->daddr
));
928 xsid
->spi
= xsinfo
->id
.spi
;
929 xsid
->proto
= xsinfo
->id
.proto
;
931 addattr_l(new_n
, xb
->size
, XFRMA_SRCADDR
, &xsinfo
->saddr
,
932 sizeof(xsid
->daddr
));
934 xb
->offset
+= new_n
->nlmsg_len
;
940 static int xfrm_state_list_or_deleteall(int argc
, char **argv
, int deleteall
)
943 struct rtnl_handle rth
;
947 filter
.xsinfo
.family
= preferred_family
;
950 if (strcmp(*argv
, "mode") == 0) {
952 xfrm_mode_parse(&filter
.xsinfo
.mode
, &argc
, &argv
);
954 filter
.mode_mask
= XFRM_FILTER_MASK_FULL
;
956 } else if (strcmp(*argv
, "reqid") == 0) {
958 xfrm_reqid_parse(&filter
.xsinfo
.reqid
, &argc
, &argv
);
960 filter
.reqid_mask
= XFRM_FILTER_MASK_FULL
;
962 } else if (strcmp(*argv
, "flag") == 0) {
964 xfrm_state_flag_parse(&filter
.xsinfo
.flags
, &argc
, &argv
);
966 filter
.state_flags_mask
= XFRM_FILTER_MASK_FULL
;
970 invarg("unknown", *argv
);
974 xfrm_id_parse(&filter
.xsinfo
.saddr
, &filter
.xsinfo
.id
,
975 &filter
.xsinfo
.family
, 1, &argc
, &argv
);
976 if (preferred_family
== AF_UNSPEC
)
977 preferred_family
= filter
.xsinfo
.family
;
982 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
986 struct xfrm_buffer xb
;
987 char buf
[NLMSG_DELETEALL_BUF_SIZE
];
991 xb
.size
= sizeof(buf
);
999 fprintf(stderr
, "Delete-all round = %d\n", i
);
1001 if (rtnl_wilddump_request(&rth
, preferred_family
, XFRM_MSG_GETSA
) < 0) {
1002 perror("Cannot send dump request");
1006 if (rtnl_dump_filter(&rth
, xfrm_state_keep
, &xb
, NULL
, NULL
) < 0) {
1007 fprintf(stderr
, "Delete-all terminated\n");
1010 if (xb
.nlmsg_count
== 0) {
1012 fprintf(stderr
, "Delete-all completed\n");
1016 if (rtnl_send_check(&rth
, xb
.buf
, xb
.offset
) < 0) {
1017 perror("Failed to send delete-all request\n");
1021 fprintf(stderr
, "Delete-all nlmsg count = %d\n", xb
.nlmsg_count
);
1028 if (rtnl_wilddump_request(&rth
, preferred_family
, XFRM_MSG_GETSA
) < 0) {
1029 perror("Cannot send dump request");
1033 if (rtnl_dump_filter(&rth
, xfrm_state_print
, stdout
, NULL
, NULL
) < 0) {
1034 fprintf(stderr
, "Dump terminated\n");
1044 int print_sadinfo(struct nlmsghdr
*n
, void *arg
)
1046 FILE *fp
= (FILE*)arg
;
1047 __u32
*f
= NLMSG_DATA(n
);
1048 struct rtattr
*tb
[XFRMA_SAD_MAX
+1];
1052 int len
= n
->nlmsg_len
;
1054 len
-= NLMSG_LENGTH(sizeof(__u32
));
1056 fprintf(stderr
, "SADinfo: Wrong len %d\n", len
);
1060 rta
= XFRMSAPD_RTA(f
);
1061 parse_rtattr(tb
, XFRMA_SAD_MAX
, rta
, len
);
1063 if (tb
[XFRMA_SAD_CNT
]) {
1064 fprintf(fp
,"\t SAD");
1065 cnt
= (__u32
*)RTA_DATA(tb
[XFRMA_SAD_CNT
]);
1066 fprintf(fp
," count %d", *cnt
);
1068 fprintf(fp
,"BAD SAD info returned\n");
1073 if (tb
[XFRMA_SAD_HINFO
]) {
1074 struct xfrmu_sadhinfo
*si
;
1076 if (RTA_PAYLOAD(tb
[XFRMA_SAD_HINFO
]) < sizeof(*si
)) {
1077 fprintf(fp
,"BAD SAD length returned\n");
1081 si
= RTA_DATA(tb
[XFRMA_SAD_HINFO
]);
1082 fprintf(fp
," (buckets ");
1083 fprintf(fp
,"count %d", si
->sadhcnt
);
1084 fprintf(fp
," Max %d", si
->sadhmcnt
);
1093 static int xfrm_sad_getinfo(int argc
, char **argv
)
1095 struct rtnl_handle rth
;
1102 memset(&req
, 0, sizeof(req
));
1103 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.flags
));
1104 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1105 req
.n
.nlmsg_type
= XFRM_MSG_GETSADINFO
;
1106 req
.flags
= 0XFFFFFFFF;
1108 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1111 if (rtnl_talk(&rth
, &req
.n
, 0, 0, &req
.n
, NULL
, NULL
) < 0)
1114 print_sadinfo(&req
.n
, (void*)stdout
);
1121 static int xfrm_state_flush(int argc
, char **argv
)
1123 struct rtnl_handle rth
;
1126 struct xfrm_usersa_flush xsf
;
1128 char *protop
= NULL
;
1130 memset(&req
, 0, sizeof(req
));
1132 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsf
));
1133 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1134 req
.n
.nlmsg_type
= XFRM_MSG_FLUSHSA
;
1138 if (strcmp(*argv
, "proto") == 0) {
1142 duparg("proto", *argv
);
1147 ret
= xfrm_xfrmproto_getbyname(*argv
);
1149 invarg("\"XFRM_PROTO\" is invalid", *argv
);
1151 req
.xsf
.proto
= (__u8
)ret
;
1153 invarg("unknown", *argv
);
1158 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
1162 fprintf(stderr
, "Flush state proto=%s\n",
1163 strxf_xfrmproto(req
.xsf
.proto
));
1165 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
1173 int do_xfrm_state(int argc
, char **argv
)
1176 return xfrm_state_list_or_deleteall(0, NULL
, 0);
1178 if (matches(*argv
, "add") == 0)
1179 return xfrm_state_modify(XFRM_MSG_NEWSA
, 0,
1181 if (matches(*argv
, "update") == 0)
1182 return xfrm_state_modify(XFRM_MSG_UPDSA
, 0,
1184 if (matches(*argv
, "allocspi") == 0)
1185 return xfrm_state_allocspi(argc
-1, argv
+1);
1186 if (matches(*argv
, "delete") == 0)
1187 return xfrm_state_get_or_delete(argc
-1, argv
+1, 1);
1188 if (matches(*argv
, "deleteall") == 0 || matches(*argv
, "delall") == 0)
1189 return xfrm_state_list_or_deleteall(argc
-1, argv
+1, 1);
1190 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
1191 || matches(*argv
, "lst") == 0)
1192 return xfrm_state_list_or_deleteall(argc
-1, argv
+1, 0);
1193 if (matches(*argv
, "get") == 0)
1194 return xfrm_state_get_or_delete(argc
-1, argv
+1, 0);
1195 if (matches(*argv
, "flush") == 0)
1196 return xfrm_state_flush(argc
-1, argv
+1);
1197 if (matches(*argv
, "count") == 0) {
1198 return xfrm_sad_getinfo(argc
, argv
);
1200 if (matches(*argv
, "help") == 0)
1202 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv
);