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_FLUSH_BUF_SIZE (4096-512)
38 #define NLMSG_FLUSH_BUF_SIZE 8192
41 * Receiving buffer defines:
43 * data = struct xfrm_usersa_info
46 * ... (max count of rtattr is XFRM_MAX_DEPTH)
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 ] [ FLAG-LIST ] [ sel SELECTOR ] [ LIMIT-LIST ]\n");
61 fprintf(stderr
, "Usage: ip xfrm state { delete | get } ID\n");
62 fprintf(stderr
, "Usage: ip xfrm state { flush | list } [ ID ] [ mode MODE ] [ reqid REQID ]\n");
63 fprintf(stderr
, " [ FLAG_LIST ]\n");
65 fprintf(stderr
, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n");
66 //fprintf(stderr, "XFRM_PROTO := [ esp | ah | ipcomp ]\n");
67 fprintf(stderr
, "XFRM_PROTO := [ ");
68 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_ESP
));
69 fprintf(stderr
, "%s | ", strxf_proto(IPPROTO_AH
));
70 fprintf(stderr
, "%s ", strxf_proto(IPPROTO_COMP
));
71 fprintf(stderr
, "]\n");
73 //fprintf(stderr, "SPI - security parameter index(default=0)\n");
75 fprintf(stderr
, "MODE := [ transport | tunnel ](default=transport)\n");
76 //fprintf(stderr, "REQID - number(default=0)\n");
78 fprintf(stderr
, "FLAG-LIST := [ FLAG-LIST ] [ flag FLAG ]\n");
79 fprintf(stderr
, "FLAG := [ noecn ]\n");
81 fprintf(stderr
, "ALGO-LIST := [ ALGO-LIST ] | [ ALGO ]\n");
82 fprintf(stderr
, "ALGO := ALGO_TYPE ALGO_NAME ALGO_KEY\n");
83 fprintf(stderr
, "ALGO_TYPE := [ ");
84 fprintf(stderr
, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT
));
85 fprintf(stderr
, "%s | ", strxf_algotype(XFRMA_ALG_AUTH
));
86 fprintf(stderr
, "%s ", strxf_algotype(XFRMA_ALG_COMP
));
87 fprintf(stderr
, "]\n");
89 //fprintf(stderr, "ALGO_NAME - algorithm name\n");
90 //fprintf(stderr, "ALGO_KEY - algorithm key\n");
92 fprintf(stderr
, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ upspec UPSPEC ] [ dev DEV ]\n");
94 fprintf(stderr
, "UPSPEC := proto PROTO [ sport PORT ] [ dport PORT ]\n");
96 //fprintf(stderr, "DEV - device name(default=none)\n");
97 fprintf(stderr
, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
98 fprintf(stderr
, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
99 fprintf(stderr
, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] COUNT ]\n");
103 static int xfrm_algo_parse(struct xfrm_algo
*alg
, enum xfrm_attr_type_t type
,
104 char *name
, char *key
, int max
)
107 int slen
= strlen(key
);
110 /* XXX: verifying both name and key is required! */
111 fprintf(stderr
, "warning: ALGONAME/ALGOKEY will send to kernel promiscuously!(verifying them isn't implemented yet)\n");
114 strncpy(alg
->alg_name
, name
, sizeof(alg
->alg_name
));
116 if (slen
> 2 && strncmp(key
, "0x", 2) == 0) {
117 /* split two chars "0x" from the top */
123 /* Converting hexadecimal numbered string into real key;
124 * Convert each two chars into one char(value). If number
125 * of the length is odd, add zero on the top for rounding.
128 /* calculate length of the converted values(real key) */
129 len
= (plen
+ 1) / 2;
131 invarg("\"ALGOKEY\" makes buffer overflow\n", key
);
133 for (i
= - (plen
% 2), j
= 0; j
< len
; i
+= 2, j
++) {
137 vbuf
[0] = i
>= 0 ? p
[i
] : '0';
141 if (get_u8(&val
, vbuf
, 16))
142 invarg("\"ALGOKEY\" is invalid", key
);
144 alg
->alg_key
[j
] = val
;
150 invarg("\"ALGOKEY\" makes buffer overflow\n", key
);
152 strncpy(alg
->alg_key
, key
, len
);
156 alg
->alg_key_len
= len
* 8;
161 static int xfrm_state_flag_parse(__u8
*flags
, int *argcp
, char ***argvp
)
164 char **argv
= *argvp
;
165 int len
= strlen(*argv
);
167 if (len
> 2 && strncmp(*argv
, "0x", 2) == 0) {
170 if (get_u8(&val
, *argv
, 16))
171 invarg("\"FLAG\" is invalid", *argv
);
174 if (strcmp(*argv
, "noecn") == 0)
175 *flags
|= XFRM_STATE_NOECN
;
177 invarg("\"FLAG\" is invalid", *argv
);
180 filter
.state_flags_mask
= XFRM_FILTER_MASK_FULL
;
188 static int xfrm_state_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
190 struct rtnl_handle rth
;
193 struct xfrm_usersa_info xsinfo
;
194 char buf
[RTA_BUF_SIZE
];
201 memset(&req
, 0, sizeof(req
));
203 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsinfo
));
204 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
205 req
.n
.nlmsg_type
= cmd
;
206 req
.xsinfo
.family
= preferred_family
;
208 req
.xsinfo
.lft
.soft_byte_limit
= XFRM_INF
;
209 req
.xsinfo
.lft
.hard_byte_limit
= XFRM_INF
;
210 req
.xsinfo
.lft
.soft_packet_limit
= XFRM_INF
;
211 req
.xsinfo
.lft
.hard_packet_limit
= XFRM_INF
;
214 if (strcmp(*argv
, "mode") == 0) {
216 xfrm_mode_parse(&req
.xsinfo
.mode
, &argc
, &argv
);
217 } else if (strcmp(*argv
, "reqid") == 0) {
219 xfrm_reqid_parse(&req
.xsinfo
.reqid
, &argc
, &argv
);
220 } else if (strcmp(*argv
, "flag") == 0) {
222 xfrm_state_flag_parse(&req
.xsinfo
.flags
, &argc
, &argv
);
223 } else if (strcmp(*argv
, "sel") == 0) {
225 xfrm_selector_parse(&req
.xsinfo
.sel
, &argc
, &argv
);
226 } else if (strcmp(*argv
, "limit") == 0) {
228 xfrm_lifetime_cfg_parse(&req
.xsinfo
.lft
, &argc
, &argv
);
230 /* try to assume ALGO */
231 int type
= xfrm_algotype_getbyname(*argv
);
233 case XFRMA_ALG_CRYPT
:
239 struct xfrm_algo alg
;
240 char buf
[XFRM_ALGO_KEY_BUF_SIZE
];
247 case XFRMA_ALG_CRYPT
:
249 duparg("ALGOTYPE", *argv
);
254 duparg("ALGOTYPE", *argv
);
259 duparg("ALGOTYPE", *argv
);
264 invarg("\"ALGOTYPE\" is invalid\n", *argv
);
277 memset(&alg
, 0, sizeof(alg
));
279 xfrm_algo_parse((void *)&alg
, type
, name
, key
,
281 len
= sizeof(struct xfrm_algo
) + alg
.alg
.alg_key_len
;
283 addattr_l(&req
.n
, sizeof(req
.buf
), type
,
288 /* try to assume ID */
290 invarg("unknown", *argv
);
294 xfrm_id_parse(&req
.xsinfo
.saddr
, &req
.xsinfo
.id
,
295 &req
.xsinfo
.family
, 0, &argc
, &argv
);
296 if (preferred_family
== AF_UNSPEC
)
297 preferred_family
= req
.xsinfo
.family
;
304 fprintf(stderr
, "Not enough information: \"ID\" is required\n");
308 if (ealgop
|| aalgop
|| calgop
) {
309 if (req
.xsinfo
.id
.proto
!= IPPROTO_ESP
&&
310 req
.xsinfo
.id
.proto
!= IPPROTO_AH
&&
311 req
.xsinfo
.id
.proto
!= IPPROTO_COMP
) {
312 fprintf(stderr
, "\"ALGO\" is invalid with proto=%s\n", strxf_proto(req
.xsinfo
.id
.proto
));
316 if (req
.xsinfo
.id
.proto
== IPPROTO_ESP
||
317 req
.xsinfo
.id
.proto
== IPPROTO_AH
||
318 req
.xsinfo
.id
.proto
== IPPROTO_COMP
) {
319 fprintf(stderr
, "\"ALGO\" is required with proto=%s\n", strxf_proto(req
.xsinfo
.id
.proto
));
324 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
327 if (req
.xsinfo
.family
== AF_UNSPEC
)
328 req
.xsinfo
.family
= AF_INET
;
330 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
338 static int xfrm_state_filter_match(struct xfrm_usersa_info
*xsinfo
)
343 if (filter
.id_src_mask
)
344 if (memcmp(&xsinfo
->saddr
, &filter
.xsinfo
.saddr
,
345 filter
.id_src_mask
) != 0)
347 if (filter
.id_dst_mask
)
348 if (memcmp(&xsinfo
->id
.daddr
, &filter
.xsinfo
.id
.daddr
,
349 filter
.id_dst_mask
) != 0)
351 if ((xsinfo
->id
.proto
^filter
.xsinfo
.id
.proto
)&filter
.id_proto_mask
)
353 if ((xsinfo
->id
.spi
^filter
.xsinfo
.id
.spi
)&filter
.id_spi_mask
)
355 if ((xsinfo
->mode
^filter
.xsinfo
.mode
)&filter
.mode_mask
)
357 if ((xsinfo
->reqid
^filter
.xsinfo
.reqid
)&filter
.reqid_mask
)
359 if (filter
.state_flags_mask
)
360 if ((xsinfo
->flags
& filter
.xsinfo
.flags
) == 0)
366 static int xfrm_selector_iszero(struct xfrm_selector
*s
)
368 struct xfrm_selector s0
;
370 memset(&s0
, 0, sizeof(s0
));
372 return (memcmp(&s0
, s
, sizeof(s0
)) == 0);
375 static int xfrm_state_print(const struct sockaddr_nl
*who
,
376 const struct nlmsghdr
*n
,
379 FILE *fp
= (FILE*)arg
;
380 struct xfrm_usersa_info
*xsinfo
= NLMSG_DATA(n
);
381 int len
= n
->nlmsg_len
;
382 struct rtattr
* tb
[XFRMA_MAX
+1];
385 if (n
->nlmsg_type
!= XFRM_MSG_NEWSA
&&
386 n
->nlmsg_type
!= XFRM_MSG_DELSA
) {
387 fprintf(stderr
, "Not a state: %08x %08x %08x\n",
388 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
392 len
-= NLMSG_LENGTH(sizeof(*xsinfo
));
394 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
398 if (!xfrm_state_filter_match(xsinfo
))
401 memset(tb
, 0, sizeof(tb
));
402 ntb
= parse_rtattr_byindex(tb
, XFRM_MAX_DEPTH
, XFRMS_RTA(xsinfo
), len
);
404 if (n
->nlmsg_type
== XFRM_MSG_DELSA
)
405 fprintf(fp
, "Deleted ");
407 xfrm_id_info_print(&xsinfo
->saddr
, &xsinfo
->id
, xsinfo
->mode
,
408 xsinfo
->reqid
, xsinfo
->family
, fp
, NULL
);
411 fprintf(fp
, "replay-window %d ", xsinfo
->replay_window
);
413 fprintf(fp
, "seq 0x%08u ", xsinfo
->seq
);
415 fprintf(fp
, "flag 0x%s", strxf_flags(xsinfo
->flags
));
416 if (show_stats
> 0) {
419 if (xsinfo
->flags
& XFRM_STATE_NOECN
)
420 fprintf(fp
, "noecn");
425 fprintf(fp
, "%s", _SL_
);
427 xfrm_xfrma_print(tb
, ntb
, xsinfo
->family
, fp
, "\t");
429 if (!xfrm_selector_iszero(&xsinfo
->sel
))
430 xfrm_selector_print(&xsinfo
->sel
, xsinfo
->family
, fp
, "\tsel ");
432 if (show_stats
> 0) {
433 xfrm_lifetime_print(&xsinfo
->lft
, &xsinfo
->curlft
, fp
, "\t");
434 xfrm_stats_print(&xsinfo
->stats
, fp
, "\t");
443 static int xfrm_state_get_or_delete(int argc
, char **argv
, int delete)
445 struct rtnl_handle rth
;
448 struct xfrm_usersa_id xsid
;
453 memset(&req
, 0, sizeof(req
));
455 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsid
));
456 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
457 req
.n
.nlmsg_type
= delete ? XFRM_MSG_DELSA
: XFRM_MSG_GETSA
;
458 req
.xsid
.family
= preferred_family
;
462 * XXX: Source address is not used and ignore it to follow
463 * XXX: a manner of setkey e.g. in the case of deleting/getting
464 * XXX: message of IPsec SA.
466 xfrm_address_t ignore_saddr
;
469 invarg("unknown", *argv
);
473 memset(&id
, 0, sizeof(id
));
474 xfrm_id_parse(&ignore_saddr
, &id
, &req
.xsid
.family
, 0,
477 memcpy(&req
.xsid
.daddr
, &id
.daddr
, sizeof(req
.xsid
.daddr
));
478 req
.xsid
.spi
= id
.spi
;
479 req
.xsid
.proto
= id
.proto
;
484 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
487 if (req
.xsid
.family
== AF_UNSPEC
)
488 req
.xsid
.family
= AF_INET
;
491 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
494 char buf
[NLMSG_BUF_SIZE
];
495 struct nlmsghdr
*res_n
= (struct nlmsghdr
*)buf
;
497 memset(buf
, 0, sizeof(buf
));
499 if (rtnl_talk(&rth
, &req
.n
, 0, 0, res_n
, NULL
, NULL
) < 0)
502 if (xfrm_state_print(NULL
, res_n
, (void*)stdout
) < 0) {
503 fprintf(stderr
, "An error :-)\n");
514 * With an existing state of nlmsg, make new nlmsg for deleting the state
515 * and store it to buffer.
517 static int xfrm_state_keep(const struct sockaddr_nl
*who
,
518 const struct nlmsghdr
*n
,
521 struct xfrm_buffer
*xb
= (struct xfrm_buffer
*)arg
;
522 struct rtnl_handle
*rth
= xb
->rth
;
523 struct xfrm_usersa_info
*xsinfo
= NLMSG_DATA(n
);
524 int len
= n
->nlmsg_len
;
525 struct nlmsghdr
*new_n
;
526 struct xfrm_usersa_id
*xsid
;
528 if (n
->nlmsg_type
!= XFRM_MSG_NEWSA
) {
529 fprintf(stderr
, "Not a state: %08x %08x %08x\n",
530 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
534 len
-= NLMSG_LENGTH(sizeof(*xsinfo
));
536 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
540 if (!xfrm_state_filter_match(xsinfo
))
543 if (xb
->offset
> xb
->size
) {
544 fprintf(stderr
, "Flush buffer overflow\n");
548 new_n
= (struct nlmsghdr
*)(xb
->buf
+ xb
->offset
);
549 new_n
->nlmsg_len
= NLMSG_LENGTH(sizeof(*xsid
));
550 new_n
->nlmsg_flags
= NLM_F_REQUEST
;
551 new_n
->nlmsg_type
= XFRM_MSG_DELSA
;
552 new_n
->nlmsg_seq
= ++rth
->seq
;
554 xsid
= NLMSG_DATA(new_n
);
555 xsid
->family
= xsinfo
->family
;
556 memcpy(&xsid
->daddr
, &xsinfo
->id
.daddr
, sizeof(xsid
->daddr
));
557 xsid
->spi
= xsinfo
->id
.spi
;
558 xsid
->proto
= xsinfo
->id
.proto
;
560 xb
->offset
+= new_n
->nlmsg_len
;
566 static int xfrm_state_list_or_flush(int argc
, char **argv
, int flush
)
569 struct rtnl_handle rth
;
573 filter
.xsinfo
.family
= preferred_family
;
576 if (strcmp(*argv
, "mode") == 0) {
578 xfrm_mode_parse(&filter
.xsinfo
.mode
, &argc
, &argv
);
580 filter
.mode_mask
= XFRM_FILTER_MASK_FULL
;
582 } else if (strcmp(*argv
, "reqid") == 0) {
584 xfrm_reqid_parse(&filter
.xsinfo
.reqid
, &argc
, &argv
);
586 filter
.reqid_mask
= XFRM_FILTER_MASK_FULL
;
588 } else if (strcmp(*argv
, "flag") == 0) {
590 xfrm_state_flag_parse(&filter
.xsinfo
.flags
, &argc
, &argv
);
592 filter
.state_flags_mask
= XFRM_FILTER_MASK_FULL
;
596 invarg("unknown", *argv
);
600 xfrm_id_parse(&filter
.xsinfo
.saddr
, &filter
.xsinfo
.id
,
601 &filter
.xsinfo
.family
, 1, &argc
, &argv
);
602 if (preferred_family
== AF_UNSPEC
)
603 preferred_family
= filter
.xsinfo
.family
;
608 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
612 struct xfrm_buffer xb
;
613 char buf
[NLMSG_FLUSH_BUF_SIZE
];
617 xb
.size
= sizeof(buf
);
625 fprintf(stderr
, "Flush round = %d\n", i
);
627 if (rtnl_wilddump_request(&rth
, preferred_family
, XFRM_MSG_GETSA
) < 0) {
628 perror("Cannot send dump request");
632 if (rtnl_dump_filter(&rth
, xfrm_state_keep
, &xb
, NULL
, NULL
) < 0) {
633 fprintf(stderr
, "Flush terminated\n");
636 if (xb
.nlmsg_count
== 0) {
638 fprintf(stderr
, "Flush completed\n");
642 if (rtnl_send(&rth
, xb
.buf
, xb
.offset
) < 0) {
643 perror("Failed to send flush request\n");
647 fprintf(stderr
, "Flushed nlmsg count = %d\n", xb
.nlmsg_count
);
654 if (rtnl_wilddump_request(&rth
, preferred_family
, XFRM_MSG_GETSA
) < 0) {
655 perror("Cannot send dump request");
659 if (rtnl_dump_filter(&rth
, xfrm_state_print
, stdout
, NULL
, NULL
) < 0) {
660 fprintf(stderr
, "Dump terminated\n");
670 static int xfrm_state_flush_all(void)
672 struct rtnl_handle rth
;
675 struct xfrm_usersa_flush xsf
;
678 memset(&req
, 0, sizeof(req
));
680 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(req
.xsf
));
681 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
682 req
.n
.nlmsg_type
= XFRM_MSG_FLUSHSA
;
683 req
.xsf
.proto
= IPSEC_PROTO_ANY
;
685 if (rtnl_open_byproto(&rth
, 0, NETLINK_XFRM
) < 0)
689 fprintf(stderr
, "Flush all\n");
691 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
699 int do_xfrm_state(int argc
, char **argv
)
702 return xfrm_state_list_or_flush(0, NULL
, 0);
704 if (matches(*argv
, "add") == 0)
705 return xfrm_state_modify(XFRM_MSG_NEWSA
, 0,
707 if (matches(*argv
, "update") == 0)
708 return xfrm_state_modify(XFRM_MSG_UPDSA
, 0,
710 if (matches(*argv
, "delete") == 0 || matches(*argv
, "del") == 0)
711 return xfrm_state_get_or_delete(argc
-1, argv
+1, 1);
712 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
713 || matches(*argv
, "lst") == 0)
714 return xfrm_state_list_or_flush(argc
-1, argv
+1, 0);
715 if (matches(*argv
, "get") == 0)
716 return xfrm_state_get_or_delete(argc
-1, argv
+1, 0);
717 if (matches(*argv
, "flush") == 0) {
719 return xfrm_state_flush_all();
721 return xfrm_state_list_or_flush(argc
-1, argv
+1, 1);
723 if (matches(*argv
, "help") == 0)
725 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv
);