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+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 ] [ replay-window SIZE ] [ flag FLAG-LIST ]\n");
60 fprintf(stderr, " [ encap ENCAP ] [ 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 FLAG_LIST ]\n");
65 fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n");
66 //fprintf(stderr, "XFRM_PROTO := [ esp | ah | comp ]\n");
67 fprintf(stderr, "XFRM_PROTO := [ ");
68 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP));
69 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH));
70 fprintf(stderr, "%s ", strxf_xfrmproto(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\n");
79 fprintf(stderr, "FLAG := [ noecn | decap-dscp ]\n");
81 fprintf(stderr, "ENCAP := ENCAP-TYPE SPORT DPORT OADDR\n");
82 fprintf(stderr, "ENCAP-TYPE := espinudp | espinudp-nonike\n");
84 fprintf(stderr, "ALGO-LIST := [ ALGO-LIST ] | [ ALGO ]\n");
85 fprintf(stderr, "ALGO := ALGO_TYPE ALGO_NAME ALGO_KEY\n");
86 fprintf(stderr, "ALGO_TYPE := [ ");
87 fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT));
88 fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_AUTH));
89 fprintf(stderr, "%s ", strxf_algotype(XFRMA_ALG_COMP));
90 fprintf(stderr, "]\n");
92 //fprintf(stderr, "ALGO_NAME - algorithm name\n");
93 //fprintf(stderr, "ALGO_KEY - algorithm key\n");
95 fprintf(stderr, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ UPSPEC ] [ dev DEV ]\n");
97 fprintf(stderr, "UPSPEC := proto PROTO [ [ sport PORT ] [ dport PORT ] |\n");
98 fprintf(stderr, " [ type NUMBER ] [ code NUMBER ] ]\n");
101 //fprintf(stderr, "DEV - device name(default=none)\n");
102 fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
103 fprintf(stderr, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
104 fprintf(stderr, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] COUNT ]\n");
108 static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type,
109 char *name, char *key, int max)
112 int slen = strlen(key);
115 /* XXX: verifying both name and key is required! */
116 fprintf(stderr, "warning: ALGONAME/ALGOKEY will send to kernel promiscuously!(verifying them isn't implemented yet)\n");
119 strncpy(alg->alg_name, name, sizeof(alg->alg_name));
121 if (slen > 2 && strncmp(key, "0x", 2) == 0) {
122 /* split two chars "0x" from the top */
128 /* Converting hexadecimal numbered string into real key;
129 * Convert each two chars into one char(value). If number
130 * of the length is odd, add zero on the top for rounding.
133 /* calculate length of the converted values(real key) */
134 len = (plen + 1) / 2;
136 invarg("\"ALGOKEY\" makes buffer overflow\n", key);
138 for (i = - (plen % 2), j = 0; j < len; i += 2, j++) {
142 vbuf[0] = i >= 0 ? p[i] : '0';
146 if (get_u8(&val, vbuf, 16))
147 invarg("\"ALGOKEY\" is invalid", key);
149 alg->alg_key[j] = val;
155 invarg("\"ALGOKEY\" makes buffer overflow\n", key);
157 strncpy(alg->alg_key, key, len);
161 alg->alg_key_len = len * 8;
166 static int xfrm_state_flag_parse(__u8 *flags, int *argcp, char ***argvp)
169 char **argv = *argvp;
170 int len = strlen(*argv);
172 if (len > 2 && strncmp(*argv, "0x", 2) == 0) {
175 if (get_u8(&val, *argv, 16))
176 invarg("\"FLAG\" is invalid", *argv);
180 if (strcmp(*argv, "noecn") == 0)
181 *flags |= XFRM_STATE_NOECN;
182 else if (strcmp(*argv, "decap-dscp") == 0)
183 *flags |= XFRM_STATE_DECAP_DSCP;
185 PREV_ARG(); /* back track */
195 filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
203 static int xfrm_state_modify(int cmd, unsigned flags, int argc, char **argv)
205 struct rtnl_handle rth;
208 struct xfrm_usersa_info xsinfo;
209 char buf[RTA_BUF_SIZE];
216 memset(&req, 0, sizeof(req));
218 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsinfo));
219 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
220 req.n.nlmsg_type = cmd;
221 req.xsinfo.family = preferred_family;
223 req.xsinfo.lft.soft_byte_limit = XFRM_INF;
224 req.xsinfo.lft.hard_byte_limit = XFRM_INF;
225 req.xsinfo.lft.soft_packet_limit = XFRM_INF;
226 req.xsinfo.lft.hard_packet_limit = XFRM_INF;
229 if (strcmp(*argv, "mode") == 0) {
231 xfrm_mode_parse(&req.xsinfo.mode, &argc, &argv);
232 } else if (strcmp(*argv, "reqid") == 0) {
234 xfrm_reqid_parse(&req.xsinfo.reqid, &argc, &argv);
235 } else if (strcmp(*argv, "replay-window") == 0) {
237 if (get_u8(&req.xsinfo.replay_window, *argv, 0))
238 invarg("\"replay-window\" value is invalid", *argv);
239 } else if (strcmp(*argv, "flag") == 0) {
241 xfrm_state_flag_parse(&req.xsinfo.flags, &argc, &argv);
242 } else if (strcmp(*argv, "sel") == 0) {
244 xfrm_selector_parse(&req.xsinfo.sel, &argc, &argv);
245 } else if (strcmp(*argv, "limit") == 0) {
247 xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv);
248 } else if (strcmp(*argv, "encap") == 0) {
249 struct xfrm_encap_tmpl encap;
252 xfrm_encap_type_parse(&encap.encap_type, &argc, &argv);
254 if (get_u16(&encap.encap_sport, *argv, 0))
255 invarg("\"encap\" sport value is invalid", *argv);
256 encap.encap_sport = htons(encap.encap_sport);
258 if (get_u16(&encap.encap_dport, *argv, 0))
259 invarg("\"encap\" dport value is invalid", *argv);
260 encap.encap_dport = htons(encap.encap_dport);
262 get_addr(&oa, *argv, AF_UNSPEC);
263 memcpy(&encap.encap_oa, &oa.data, sizeof(encap.encap_oa));
264 addattr_l(&req.n, sizeof(req.buf), XFRMA_ENCAP,
265 (void *)&encap, sizeof(encap));
267 /* try to assume ALGO */
268 int type = xfrm_algotype_getbyname(*argv);
270 case XFRMA_ALG_CRYPT:
276 struct xfrm_algo alg;
277 char buf[XFRM_ALGO_KEY_BUF_SIZE];
284 case XFRMA_ALG_CRYPT:
286 duparg("ALGOTYPE", *argv);
291 duparg("ALGOTYPE", *argv);
296 duparg("ALGOTYPE", *argv);
301 invarg("\"ALGOTYPE\" is invalid\n", *argv);
314 memset(&alg, 0, sizeof(alg));
316 xfrm_algo_parse((void *)&alg, type, name, key,
318 len = sizeof(struct xfrm_algo) + alg.alg.alg_key_len;
320 addattr_l(&req.n, sizeof(req.buf), type,
325 /* try to assume ID */
327 invarg("unknown", *argv);
331 xfrm_id_parse(&req.xsinfo.saddr, &req.xsinfo.id,
332 &req.xsinfo.family, 0, &argc, &argv);
333 if (preferred_family == AF_UNSPEC)
334 preferred_family = req.xsinfo.family;
341 fprintf(stderr, "Not enough information: \"ID\" is required\n");
345 if (ealgop || aalgop || calgop) {
346 if (req.xsinfo.id.proto != IPPROTO_ESP &&
347 req.xsinfo.id.proto != IPPROTO_AH &&
348 req.xsinfo.id.proto != IPPROTO_COMP) {
349 fprintf(stderr, "\"ALGO\" is invalid with proto=%s\n", strxf_xfrmproto(req.xsinfo.id.proto));
353 if (req.xsinfo.id.proto == IPPROTO_ESP ||
354 req.xsinfo.id.proto == IPPROTO_AH ||
355 req.xsinfo.id.proto == IPPROTO_COMP) {
356 fprintf(stderr, "\"ALGO\" is required with proto=%s\n", strxf_xfrmproto(req.xsinfo.id.proto));
361 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
364 if (req.xsinfo.family == AF_UNSPEC)
365 req.xsinfo.family = AF_INET;
367 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
375 static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo)
380 if (filter.id_src_mask)
381 if (xfrm_addr_match(&xsinfo->saddr, &filter.xsinfo.saddr,
384 if (filter.id_dst_mask)
385 if (xfrm_addr_match(&xsinfo->id.daddr, &filter.xsinfo.id.daddr,
388 if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask)
390 if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask)
392 if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask)
394 if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask)
396 if (filter.state_flags_mask)
397 if ((xsinfo->flags & filter.xsinfo.flags) == 0)
403 static int xfrm_selector_iszero(struct xfrm_selector *s)
405 struct xfrm_selector s0;
407 memset(&s0, 0, sizeof(s0));
409 return (memcmp(&s0, s, sizeof(s0)) == 0);
412 static int xfrm_state_print(const struct sockaddr_nl *who,
416 FILE *fp = (FILE*)arg;
417 struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n);
418 int len = n->nlmsg_len;
419 struct rtattr * tb[XFRMA_MAX+1];
421 if (n->nlmsg_type != XFRM_MSG_NEWSA &&
422 n->nlmsg_type != XFRM_MSG_DELSA) {
423 fprintf(stderr, "Not a state: %08x %08x %08x\n",
424 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
428 len -= NLMSG_LENGTH(sizeof(*xsinfo));
430 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
434 if (!xfrm_state_filter_match(xsinfo))
437 parse_rtattr(tb, XFRMA_MAX, XFRMS_RTA(xsinfo), len);
439 if (n->nlmsg_type == XFRM_MSG_DELSA)
440 fprintf(fp, "Deleted ");
442 xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode,
443 xsinfo->reqid, xsinfo->family, 1, fp, NULL);
446 fprintf(fp, "replay-window %u ", xsinfo->replay_window);
448 fprintf(fp, "seq 0x%08u ", xsinfo->seq);
449 if (show_stats > 0 || xsinfo->flags) {
450 __u8 flags = xsinfo->flags;
452 fprintf(fp, "flag ");
453 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOECN, "noecn");
454 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp");
456 fprintf(fp, "%x", flags);
458 fprintf(fp, " (0x%s)", strxf_mask8(flags));
460 fprintf(fp, "%s", _SL_);
462 xfrm_xfrma_print(tb, xsinfo->family, fp, "\t");
464 if (!xfrm_selector_iszero(&xsinfo->sel))
465 xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, "\tsel ");
467 if (show_stats > 0) {
468 xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, "\t");
469 xfrm_stats_print(&xsinfo->stats, fp, "\t");
478 static int xfrm_state_get_or_delete(int argc, char **argv, int delete)
480 struct rtnl_handle rth;
483 struct xfrm_usersa_id xsid;
488 memset(&req, 0, sizeof(req));
490 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid));
491 req.n.nlmsg_flags = NLM_F_REQUEST;
492 req.n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA;
493 req.xsid.family = preferred_family;
497 * XXX: Source address is not used and ignore it to follow
498 * XXX: a manner of setkey e.g. in the case of deleting/getting
499 * XXX: message of IPsec SA.
501 xfrm_address_t ignore_saddr;
504 invarg("unknown", *argv);
508 memset(&id, 0, sizeof(id));
509 xfrm_id_parse(&ignore_saddr, &id, &req.xsid.family, 0,
512 memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr));
513 req.xsid.spi = id.spi;
514 req.xsid.proto = id.proto;
519 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
522 if (req.xsid.family == AF_UNSPEC)
523 req.xsid.family = AF_INET;
526 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
529 char buf[NLMSG_BUF_SIZE];
530 struct nlmsghdr *res_n = (struct nlmsghdr *)buf;
532 memset(buf, 0, sizeof(buf));
534 if (rtnl_talk(&rth, &req.n, 0, 0, res_n, NULL, NULL) < 0)
537 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
538 fprintf(stderr, "An error :-)\n");
549 * With an existing state of nlmsg, make new nlmsg for deleting the state
550 * and store it to buffer.
552 static int xfrm_state_keep(const struct sockaddr_nl *who,
556 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
557 struct rtnl_handle *rth = xb->rth;
558 struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n);
559 int len = n->nlmsg_len;
560 struct nlmsghdr *new_n;
561 struct xfrm_usersa_id *xsid;
563 if (n->nlmsg_type != XFRM_MSG_NEWSA) {
564 fprintf(stderr, "Not a state: %08x %08x %08x\n",
565 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
569 len -= NLMSG_LENGTH(sizeof(*xsinfo));
571 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
575 if (!xfrm_state_filter_match(xsinfo))
578 if (xb->offset > xb->size) {
579 fprintf(stderr, "Flush buffer overflow\n");
583 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
584 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid));
585 new_n->nlmsg_flags = NLM_F_REQUEST;
586 new_n->nlmsg_type = XFRM_MSG_DELSA;
587 new_n->nlmsg_seq = ++rth->seq;
589 xsid = NLMSG_DATA(new_n);
590 xsid->family = xsinfo->family;
591 memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr));
592 xsid->spi = xsinfo->id.spi;
593 xsid->proto = xsinfo->id.proto;
595 xb->offset += new_n->nlmsg_len;
601 static int xfrm_state_list_or_flush(int argc, char **argv, int flush)
604 struct rtnl_handle rth;
608 filter.xsinfo.family = preferred_family;
611 if (strcmp(*argv, "mode") == 0) {
613 xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv);
615 filter.mode_mask = XFRM_FILTER_MASK_FULL;
617 } else if (strcmp(*argv, "reqid") == 0) {
619 xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv);
621 filter.reqid_mask = XFRM_FILTER_MASK_FULL;
623 } else if (strcmp(*argv, "flag") == 0) {
625 xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv);
627 filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
631 invarg("unknown", *argv);
635 xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id,
636 &filter.xsinfo.family, 1, &argc, &argv);
637 if (preferred_family == AF_UNSPEC)
638 preferred_family = filter.xsinfo.family;
643 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
647 struct xfrm_buffer xb;
648 char buf[NLMSG_FLUSH_BUF_SIZE];
652 xb.size = sizeof(buf);
660 fprintf(stderr, "Flush round = %d\n", i);
662 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
663 perror("Cannot send dump request");
667 if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb, NULL, NULL) < 0) {
668 fprintf(stderr, "Flush terminated\n");
671 if (xb.nlmsg_count == 0) {
673 fprintf(stderr, "Flush completed\n");
677 if (rtnl_send(&rth, xb.buf, xb.offset) < 0) {
678 perror("Failed to send flush request\n");
682 fprintf(stderr, "Flushed nlmsg count = %d\n", xb.nlmsg_count);
689 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
690 perror("Cannot send dump request");
694 if (rtnl_dump_filter(&rth, xfrm_state_print, stdout, NULL, NULL) < 0) {
695 fprintf(stderr, "Dump terminated\n");
705 static int xfrm_state_flush_all(void)
707 struct rtnl_handle rth;
710 struct xfrm_usersa_flush xsf;
713 memset(&req, 0, sizeof(req));
715 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsf));
716 req.n.nlmsg_flags = NLM_F_REQUEST;
717 req.n.nlmsg_type = XFRM_MSG_FLUSHSA;
718 req.xsf.proto = IPSEC_PROTO_ANY;
720 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
724 fprintf(stderr, "Flush all\n");
726 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
734 int do_xfrm_state(int argc, char **argv)
737 return xfrm_state_list_or_flush(0, NULL, 0);
739 if (matches(*argv, "add") == 0)
740 return xfrm_state_modify(XFRM_MSG_NEWSA, 0,
742 if (matches(*argv, "update") == 0)
743 return xfrm_state_modify(XFRM_MSG_UPDSA, 0,
745 if (matches(*argv, "delete") == 0 || matches(*argv, "del") == 0)
746 return xfrm_state_get_or_delete(argc-1, argv+1, 1);
747 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
748 || matches(*argv, "lst") == 0)
749 return xfrm_state_list_or_flush(argc-1, argv+1, 0);
750 if (matches(*argv, "get") == 0)
751 return xfrm_state_get_or_delete(argc-1, argv+1, 0);
752 if (matches(*argv, "flush") == 0) {
754 return xfrm_state_flush_all();
756 return xfrm_state_list_or_flush(argc-1, argv+1, 1);
758 if (matches(*argv, "help") == 0)
760 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv);