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 [ ALGO-LIST ] [ mode MODE ]\n");
60 fprintf(stderr, " [ mark MARK [ mask MASK ] ] [ reqid REQID ] [ seq SEQ ]\n");
61 fprintf(stderr, " [ replay-window SIZE ] [ replay-seq SEQ ] [ replay-oseq SEQ ]\n");
62 fprintf(stderr, " [ flag FLAG-LIST ] [ sel SELECTOR ] [ LIMIT-LIST ] [ encap ENCAP ]\n");
63 fprintf(stderr, " [ coa ADDR[/PLEN] ] [ ctx CTX ]\n");
64 fprintf(stderr, "Usage: ip xfrm state allocspi ID [ mode MODE ] [ mark MARK [ mask MASK ] ]\n");
65 fprintf(stderr, " [ reqid REQID ] [ seq SEQ ] [ min SPI max SPI ]\n");
66 fprintf(stderr, "Usage: ip xfrm state { delete | get } ID [ mark MARK [ mask MASK ] ]\n");
67 fprintf(stderr, "Usage: ip xfrm state { deleteall | list } [ ID ] [ mode MODE ] [ reqid REQID ]\n");
68 fprintf(stderr, " [ flag FLAG-LIST ]\n");
69 fprintf(stderr, "Usage: ip xfrm state flush [ proto XFRM-PROTO ]\n");
70 fprintf(stderr, "Usage: ip xfrm state count\n");
71 fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ]\n");
72 fprintf(stderr, "XFRM-PROTO := ");
73 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP));
74 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH));
75 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_COMP));
76 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING));
77 fprintf(stderr, "%s\n", strxf_xfrmproto(IPPROTO_DSTOPTS));
78 fprintf(stderr, "ALGO-LIST := [ ALGO-LIST ] ALGO\n");
79 fprintf(stderr, "ALGO := { ");
80 fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT));
81 fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_AUTH));
82 fprintf(stderr, "%s", strxf_algotype(XFRMA_ALG_COMP));
83 fprintf(stderr, " } ALGO-NAME ALGO-KEY |\n");
84 fprintf(stderr, " %s", strxf_algotype(XFRMA_ALG_AEAD));
85 fprintf(stderr, " ALGO-NAME ALGO-KEY ALGO-ICV-LEN |\n");
86 fprintf(stderr, " %s", strxf_algotype(XFRMA_ALG_AUTH_TRUNC));
87 fprintf(stderr, " ALGO-NAME ALGO-KEY ALGO-TRUNC-LEN\n");
88 fprintf(stderr, "MODE := transport | tunnel | ro | in_trigger | beet\n");
89 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
90 fprintf(stderr, "FLAG := noecn | decap-dscp | nopmtudisc | wildrecv | icmp | af-unspec | align4\n");
91 fprintf(stderr, "SELECTOR := [ src ADDR[/PLEN] ] [ dst ADDR[/PLEN] ] [ dev DEV ] [ UPSPEC ]\n");
92 fprintf(stderr, "UPSPEC := proto { { ");
93 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_TCP));
94 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_UDP));
95 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_SCTP));
96 fprintf(stderr, "%s", strxf_proto(IPPROTO_DCCP));
97 fprintf(stderr, " } [ sport PORT ] [ dport PORT ] |\n");
98 fprintf(stderr, " { ");
99 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_ICMP));
100 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_ICMPV6));
101 fprintf(stderr, "%s", strxf_proto(IPPROTO_MH));
102 fprintf(stderr, " } [ type NUMBER ] [ code NUMBER ] |\n");
103 fprintf(stderr, " %s", strxf_proto(IPPROTO_GRE));
104 fprintf(stderr, " [ key { DOTTED-QUAD | NUMBER } ] | PROTO }\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");
108 fprintf(stderr, "ENCAP := { espinudp | espinudp-nonike } SPORT DPORT OADDR\n");
113 static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type,
114 char *name, char *key, char *buf, int max)
117 int slen = strlen(key);
120 /* XXX: verifying both name and key is required! */
121 fprintf(stderr, "warning: ALGO-NAME/ALGO-KEY will send to kernel promiscuously! (verifying them isn't implemented yet)\n");
124 strncpy(alg->alg_name, name, sizeof(alg->alg_name));
126 if (slen > 2 && strncmp(key, "0x", 2) == 0) {
127 /* split two chars "0x" from the top */
133 /* Converting hexadecimal numbered string into real key;
134 * Convert each two chars into one char(value). If number
135 * of the length is odd, add zero on the top for rounding.
138 /* calculate length of the converted values(real key) */
139 len = (plen + 1) / 2;
141 invarg("\"ALGO-KEY\" makes buffer overflow\n", key);
143 for (i = - (plen % 2), j = 0; j < len; i += 2, j++) {
147 vbuf[0] = i >= 0 ? p[i] : '0';
151 if (get_u8(&val, vbuf, 16))
152 invarg("\"ALGO-KEY\" is invalid", key);
160 invarg("\"ALGO-KEY\" makes buffer overflow\n", key);
162 strncpy(buf, key, len);
166 alg->alg_key_len = len * 8;
171 static int xfrm_seq_parse(__u32 *seq, int *argcp, char ***argvp)
174 char **argv = *argvp;
176 if (get_u32(seq, *argv, 0))
177 invarg("\"SEQ\" is invalid", *argv);
187 static int xfrm_state_flag_parse(__u8 *flags, int *argcp, char ***argvp)
190 char **argv = *argvp;
191 int len = strlen(*argv);
193 if (len > 2 && strncmp(*argv, "0x", 2) == 0) {
196 if (get_u8(&val, *argv, 16))
197 invarg("\"FLAG\" is invalid", *argv);
201 if (strcmp(*argv, "noecn") == 0)
202 *flags |= XFRM_STATE_NOECN;
203 else if (strcmp(*argv, "decap-dscp") == 0)
204 *flags |= XFRM_STATE_DECAP_DSCP;
205 else if (strcmp(*argv, "nopmtudisc") == 0)
206 *flags |= XFRM_STATE_NOPMTUDISC;
207 else if (strcmp(*argv, "wildrecv") == 0)
208 *flags |= XFRM_STATE_WILDRECV;
209 else if (strcmp(*argv, "icmp") == 0)
210 *flags |= XFRM_STATE_ICMP;
211 else if (strcmp(*argv, "af-unspec") == 0)
212 *flags |= XFRM_STATE_AF_UNSPEC;
213 else if (strcmp(*argv, "align4") == 0)
214 *flags |= XFRM_STATE_ALIGN4;
216 PREV_ARG(); /* back track */
232 static int xfrm_state_modify(int cmd, unsigned flags, int argc, char **argv)
234 struct rtnl_handle rth;
237 struct xfrm_usersa_info xsinfo;
238 char buf[RTA_BUF_SIZE];
240 struct xfrm_replay_state replay;
248 struct xfrm_mark mark = {0, 0};
250 struct xfrm_user_sec_ctx sctx;
251 char str[CTX_BUF_SIZE];
254 memset(&req, 0, sizeof(req));
255 memset(&replay, 0, sizeof(replay));
256 memset(&ctx, 0, sizeof(ctx));
258 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsinfo));
259 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
260 req.n.nlmsg_type = cmd;
261 req.xsinfo.family = preferred_family;
263 req.xsinfo.lft.soft_byte_limit = XFRM_INF;
264 req.xsinfo.lft.hard_byte_limit = XFRM_INF;
265 req.xsinfo.lft.soft_packet_limit = XFRM_INF;
266 req.xsinfo.lft.hard_packet_limit = XFRM_INF;
269 if (strcmp(*argv, "mode") == 0) {
271 xfrm_mode_parse(&req.xsinfo.mode, &argc, &argv);
272 } else if (strcmp(*argv, "mark") == 0) {
273 xfrm_parse_mark(&mark, &argc, &argv);
274 } else if (strcmp(*argv, "reqid") == 0) {
276 xfrm_reqid_parse(&req.xsinfo.reqid, &argc, &argv);
277 } else if (strcmp(*argv, "seq") == 0) {
279 xfrm_seq_parse(&req.xsinfo.seq, &argc, &argv);
280 } else if (strcmp(*argv, "replay-window") == 0) {
282 if (get_u8(&req.xsinfo.replay_window, *argv, 0))
283 invarg("\"replay-window\" value is invalid", *argv);
284 } else if (strcmp(*argv, "replay-seq") == 0) {
286 if (get_u32(&replay.seq, *argv, 0))
287 invarg("\"replay-seq\" value is invalid", *argv);
288 } else if (strcmp(*argv, "replay-oseq") == 0) {
290 if (get_u32(&replay.oseq, *argv, 0))
291 invarg("\"replay-oseq\" value is invalid", *argv);
292 } else if (strcmp(*argv, "flag") == 0) {
294 xfrm_state_flag_parse(&req.xsinfo.flags, &argc, &argv);
295 } else if (strcmp(*argv, "sel") == 0) {
297 xfrm_selector_parse(&req.xsinfo.sel, &argc, &argv);
298 } else if (strcmp(*argv, "limit") == 0) {
300 xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv);
301 } else if (strcmp(*argv, "encap") == 0) {
302 struct xfrm_encap_tmpl encap;
305 xfrm_encap_type_parse(&encap.encap_type, &argc, &argv);
307 if (get_u16(&encap.encap_sport, *argv, 0))
308 invarg("\"encap\" sport value is invalid", *argv);
309 encap.encap_sport = htons(encap.encap_sport);
311 if (get_u16(&encap.encap_dport, *argv, 0))
312 invarg("\"encap\" dport value is invalid", *argv);
313 encap.encap_dport = htons(encap.encap_dport);
315 get_addr(&oa, *argv, AF_UNSPEC);
316 memcpy(&encap.encap_oa, &oa.data, sizeof(encap.encap_oa));
317 addattr_l(&req.n, sizeof(req.buf), XFRMA_ENCAP,
318 (void *)&encap, sizeof(encap));
319 } else if (strcmp(*argv, "coa") == 0) {
324 duparg("coa", *argv);
329 get_prefix(&coa, *argv, preferred_family);
330 if (coa.family == AF_UNSPEC)
331 invarg("\"coa\" address family is AF_UNSPEC", *argv);
332 if (coa.bytelen > sizeof(xcoa))
333 invarg("\"coa\" address length is too large", *argv);
335 memset(&xcoa, 0, sizeof(xcoa));
336 memcpy(&xcoa, &coa.data, coa.bytelen);
338 addattr_l(&req.n, sizeof(req.buf), XFRMA_COADDR,
339 (void *)&xcoa, sizeof(xcoa));
340 } else if (strcmp(*argv, "ctx") == 0) {
344 duparg("ctx", *argv);
350 xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx);
351 addattr_l(&req.n, sizeof(req.buf), XFRMA_SEC_CTX,
352 (void *)&ctx, ctx.sctx.len);
354 /* try to assume ALGO */
355 int type = xfrm_algotype_getbyname(*argv);
358 case XFRMA_ALG_CRYPT:
360 case XFRMA_ALG_AUTH_TRUNC:
366 struct xfrm_algo alg;
367 struct xfrm_algo_aead aead;
368 struct xfrm_algo_auth auth;
370 char buf[XFRM_ALGO_KEY_BUF_SIZE];
373 __u32 icvlen, trunclen;
381 duparg("ALGO-TYPE", *argv);
384 case XFRMA_ALG_CRYPT:
386 duparg("ALGO-TYPE", *argv);
390 case XFRMA_ALG_AUTH_TRUNC:
392 duparg("ALGO-TYPE", *argv);
397 duparg("ALGO-TYPE", *argv);
402 invarg("\"ALGO-TYPE\" is invalid\n", *argv);
406 missarg("ALGO-NAME");
415 buf = alg.u.alg.alg_key;
416 len = sizeof(alg.u.alg);
421 missarg("ALGO-ICV-LEN");
423 if (get_u32(&icvlen, *argv, 0))
424 invarg("\"aead\" ICV length is invalid",
426 alg.u.aead.alg_icv_len = icvlen;
428 buf = alg.u.aead.alg_key;
429 len = sizeof(alg.u.aead);
431 case XFRMA_ALG_AUTH_TRUNC:
433 missarg("ALGO-TRUNC-LEN");
435 if (get_u32(&trunclen, *argv, 0))
436 invarg("\"auth\" trunc length is invalid",
438 alg.u.auth.alg_trunc_len = trunclen;
440 buf = alg.u.auth.alg_key;
441 len = sizeof(alg.u.auth);
445 xfrm_algo_parse((void *)&alg, type, name, key,
446 buf, sizeof(alg.buf));
447 len += alg.u.alg.alg_key_len;
449 addattr_l(&req.n, sizeof(req.buf), type,
454 /* try to assume ID */
456 invarg("unknown", *argv);
460 xfrm_id_parse(&req.xsinfo.saddr, &req.xsinfo.id,
461 &req.xsinfo.family, 0, &argc, &argv);
462 if (preferred_family == AF_UNSPEC)
463 preferred_family = req.xsinfo.family;
469 if (replay.seq || replay.oseq)
470 addattr_l(&req.n, sizeof(req.buf), XFRMA_REPLAY_VAL,
471 (void *)&replay, sizeof(replay));
474 fprintf(stderr, "Not enough information: \"ID\" is required\n");
478 if (mark.m & mark.v) {
479 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
480 (void *)&mark, sizeof(mark));
482 fprintf(stderr, "XFRMA_MARK failed\n");
487 switch (req.xsinfo.mode) {
488 case XFRM_MODE_TRANSPORT:
489 case XFRM_MODE_TUNNEL:
490 if (!xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
491 fprintf(stderr, "\"mode\" is invalid with proto=%s\n",
492 strxf_xfrmproto(req.xsinfo.id.proto));
496 case XFRM_MODE_ROUTEOPTIMIZATION:
497 case XFRM_MODE_IN_TRIGGER:
498 if (!xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
499 fprintf(stderr, "\"mode\" is invalid with proto=%s\n",
500 strxf_xfrmproto(req.xsinfo.id.proto));
503 if (req.xsinfo.id.spi != 0) {
504 fprintf(stderr, "\"spi\" must be 0 with proto=%s\n",
505 strxf_xfrmproto(req.xsinfo.id.proto));
513 if (aeadop || ealgop || aalgop || calgop) {
514 if (!xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
515 fprintf(stderr, "\"ALGO\" is invalid with proto=%s\n",
516 strxf_xfrmproto(req.xsinfo.id.proto));
520 if (xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
521 fprintf(stderr, "\"ALGO\" is required with proto=%s\n",
522 strxf_xfrmproto(req.xsinfo.id.proto));
528 if (!xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
529 fprintf(stderr, "\"coa\" is invalid with proto=%s\n",
530 strxf_xfrmproto(req.xsinfo.id.proto));
534 if (xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
535 fprintf(stderr, "\"coa\" is required with proto=%s\n",
536 strxf_xfrmproto(req.xsinfo.id.proto));
541 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
544 if (req.xsinfo.family == AF_UNSPEC)
545 req.xsinfo.family = AF_INET;
547 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
555 static int xfrm_state_allocspi(int argc, char **argv)
557 struct rtnl_handle rth;
560 struct xfrm_userspi_info xspi;
561 char buf[RTA_BUF_SIZE];
566 struct xfrm_mark mark = {0, 0};
567 char res_buf[NLMSG_BUF_SIZE];
568 struct nlmsghdr *res_n = (struct nlmsghdr *)res_buf;
570 memset(res_buf, 0, sizeof(res_buf));
572 memset(&req, 0, sizeof(req));
574 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xspi));
575 req.n.nlmsg_flags = NLM_F_REQUEST;
576 req.n.nlmsg_type = XFRM_MSG_ALLOCSPI;
577 req.xspi.info.family = preferred_family;
580 req.xsinfo.lft.soft_byte_limit = XFRM_INF;
581 req.xsinfo.lft.hard_byte_limit = XFRM_INF;
582 req.xsinfo.lft.soft_packet_limit = XFRM_INF;
583 req.xsinfo.lft.hard_packet_limit = XFRM_INF;
587 if (strcmp(*argv, "mode") == 0) {
589 xfrm_mode_parse(&req.xspi.info.mode, &argc, &argv);
590 } else if (strcmp(*argv, "mark") == 0) {
591 xfrm_parse_mark(&mark, &argc, &argv);
592 } else if (strcmp(*argv, "reqid") == 0) {
594 xfrm_reqid_parse(&req.xspi.info.reqid, &argc, &argv);
595 } else if (strcmp(*argv, "seq") == 0) {
597 xfrm_seq_parse(&req.xspi.info.seq, &argc, &argv);
598 } else if (strcmp(*argv, "min") == 0) {
600 duparg("min", *argv);
605 if (get_u32(&req.xspi.min, *argv, 0))
606 invarg("\"min\" value is invalid", *argv);
607 } else if (strcmp(*argv, "max") == 0) {
609 duparg("max", *argv);
614 if (get_u32(&req.xspi.max, *argv, 0))
615 invarg("\"max\" value is invalid", *argv);
617 /* try to assume ID */
619 invarg("unknown", *argv);
623 xfrm_id_parse(&req.xspi.info.saddr, &req.xspi.info.id,
624 &req.xspi.info.family, 0, &argc, &argv);
625 if (req.xspi.info.id.spi) {
626 fprintf(stderr, "\"SPI\" must be zero\n");
629 if (preferred_family == AF_UNSPEC)
630 preferred_family = req.xspi.info.family;
636 fprintf(stderr, "Not enough information: \"ID\" is required\n");
642 fprintf(stderr, "\"max\" is missing\n");
645 if (req.xspi.min > req.xspi.max) {
646 fprintf(stderr, "\"min\" value is larger than \"max\" value\n");
651 fprintf(stderr, "\"min\" is missing\n");
655 /* XXX: Default value defined in PF_KEY;
656 * See kernel's net/key/af_key.c(pfkey_getspi).
658 req.xspi.min = 0x100;
659 req.xspi.max = 0x0fffffff;
661 /* XXX: IPCOMP spi is 16-bits;
662 * See kernel's net/xfrm/xfrm_user(verify_userspi_info).
664 if (req.xspi.info.id.proto == IPPROTO_COMP)
665 req.xspi.max = 0xffff;
668 if (mark.m & mark.v) {
669 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
670 (void *)&mark, sizeof(mark));
672 fprintf(stderr, "XFRMA_MARK failed\n");
677 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
680 if (req.xspi.info.family == AF_UNSPEC)
681 req.xspi.info.family = AF_INET;
684 if (rtnl_talk(&rth, &req.n, 0, 0, res_n) < 0)
687 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
688 fprintf(stderr, "An error :-)\n");
697 static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo)
702 if (filter.id_src_mask)
703 if (xfrm_addr_match(&xsinfo->saddr, &filter.xsinfo.saddr,
706 if (filter.id_dst_mask)
707 if (xfrm_addr_match(&xsinfo->id.daddr, &filter.xsinfo.id.daddr,
710 if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask)
712 if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask)
714 if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask)
716 if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask)
718 if (filter.state_flags_mask)
719 if ((xsinfo->flags & filter.xsinfo.flags) == 0)
725 int xfrm_state_print(const struct sockaddr_nl *who, struct nlmsghdr *n,
728 FILE *fp = (FILE*)arg;
729 struct rtattr * tb[XFRMA_MAX+1];
731 struct xfrm_usersa_info *xsinfo = NULL;
732 struct xfrm_user_expire *xexp = NULL;
733 struct xfrm_usersa_id *xsid = NULL;
734 int len = n->nlmsg_len;
736 if (n->nlmsg_type != XFRM_MSG_NEWSA &&
737 n->nlmsg_type != XFRM_MSG_DELSA &&
738 n->nlmsg_type != XFRM_MSG_UPDSA &&
739 n->nlmsg_type != XFRM_MSG_EXPIRE) {
740 fprintf(stderr, "Not a state: %08x %08x %08x\n",
741 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
745 if (n->nlmsg_type == XFRM_MSG_DELSA) {
746 /* Dont blame me for this .. Herbert made me do it */
747 xsid = NLMSG_DATA(n);
748 len -= NLMSG_SPACE(sizeof(*xsid));
749 } else if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
750 xexp = NLMSG_DATA(n);
751 xsinfo = &xexp->state;
752 len -= NLMSG_SPACE(sizeof(*xexp));
755 xsinfo = NLMSG_DATA(n);
756 len -= NLMSG_SPACE(sizeof(*xsinfo));
760 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
764 if (xsinfo && !xfrm_state_filter_match(xsinfo))
767 if (n->nlmsg_type == XFRM_MSG_DELSA)
768 fprintf(fp, "Deleted ");
769 else if (n->nlmsg_type == XFRM_MSG_UPDSA)
770 fprintf(fp, "Updated ");
771 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
772 fprintf(fp, "Expired ");
774 if (n->nlmsg_type == XFRM_MSG_DELSA)
775 rta = XFRMSID_RTA(xsid);
776 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
777 rta = XFRMEXP_RTA(xexp);
779 rta = XFRMS_RTA(xsinfo);
781 parse_rtattr(tb, XFRMA_MAX, rta, len);
783 if (n->nlmsg_type == XFRM_MSG_DELSA) {
784 //xfrm_policy_id_print();
787 fprintf(stderr, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
790 if (RTA_PAYLOAD(tb[XFRMA_SA]) < sizeof(*xsinfo)) {
791 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
794 xsinfo = RTA_DATA(tb[XFRMA_SA]);
797 xfrm_state_info_print(xsinfo, tb, fp, NULL, NULL);
799 if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
801 fprintf(fp, "hard %u", xexp->hard);
802 fprintf(fp, "%s", _SL_);
812 static int xfrm_state_get_or_delete(int argc, char **argv, int delete)
814 struct rtnl_handle rth;
817 struct xfrm_usersa_id xsid;
818 char buf[RTA_BUF_SIZE];
822 struct xfrm_mark mark = {0, 0};
824 memset(&req, 0, sizeof(req));
826 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid));
827 req.n.nlmsg_flags = NLM_F_REQUEST;
828 req.n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA;
829 req.xsid.family = preferred_family;
832 xfrm_address_t saddr;
834 if (strcmp(*argv, "mark") == 0) {
835 xfrm_parse_mark(&mark, &argc, &argv);
838 invarg("unknown", *argv);
842 memset(&id, 0, sizeof(id));
843 memset(&saddr, 0, sizeof(saddr));
844 xfrm_id_parse(&saddr, &id, &req.xsid.family, 0,
847 memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr));
848 req.xsid.spi = id.spi;
849 req.xsid.proto = id.proto;
851 addattr_l(&req.n, sizeof(req.buf), XFRMA_SRCADDR,
852 (void *)&saddr, sizeof(saddr));
858 if (mark.m & mark.v) {
859 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
860 (void *)&mark, sizeof(mark));
862 fprintf(stderr, "XFRMA_MARK failed\n");
867 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
870 if (req.xsid.family == AF_UNSPEC)
871 req.xsid.family = AF_INET;
874 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
877 char buf[NLMSG_BUF_SIZE];
878 struct nlmsghdr *res_n = (struct nlmsghdr *)buf;
880 memset(buf, 0, sizeof(buf));
882 if (rtnl_talk(&rth, &req.n, 0, 0, res_n) < 0)
885 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
886 fprintf(stderr, "An error :-)\n");
897 * With an existing state of nlmsg, make new nlmsg for deleting the state
898 * and store it to buffer.
900 static int xfrm_state_keep(const struct sockaddr_nl *who,
904 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
905 struct rtnl_handle *rth = xb->rth;
906 struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n);
907 int len = n->nlmsg_len;
908 struct nlmsghdr *new_n;
909 struct xfrm_usersa_id *xsid;
911 if (n->nlmsg_type != XFRM_MSG_NEWSA) {
912 fprintf(stderr, "Not a state: %08x %08x %08x\n",
913 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
917 len -= NLMSG_LENGTH(sizeof(*xsinfo));
919 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
923 if (!xfrm_state_filter_match(xsinfo))
926 if (xb->offset > xb->size) {
927 fprintf(stderr, "State buffer overflow\n");
931 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
932 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid));
933 new_n->nlmsg_flags = NLM_F_REQUEST;
934 new_n->nlmsg_type = XFRM_MSG_DELSA;
935 new_n->nlmsg_seq = ++rth->seq;
937 xsid = NLMSG_DATA(new_n);
938 xsid->family = xsinfo->family;
939 memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr));
940 xsid->spi = xsinfo->id.spi;
941 xsid->proto = xsinfo->id.proto;
943 addattr_l(new_n, xb->size, XFRMA_SRCADDR, &xsinfo->saddr,
944 sizeof(xsid->daddr));
946 xb->offset += new_n->nlmsg_len;
952 static int xfrm_state_list_or_deleteall(int argc, char **argv, int deleteall)
955 struct rtnl_handle rth;
959 filter.xsinfo.family = preferred_family;
962 if (strcmp(*argv, "mode") == 0) {
964 xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv);
966 filter.mode_mask = XFRM_FILTER_MASK_FULL;
968 } else if (strcmp(*argv, "reqid") == 0) {
970 xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv);
972 filter.reqid_mask = XFRM_FILTER_MASK_FULL;
974 } else if (strcmp(*argv, "flag") == 0) {
976 xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv);
978 filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
982 invarg("unknown", *argv);
986 xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id,
987 &filter.xsinfo.family, 1, &argc, &argv);
988 if (preferred_family == AF_UNSPEC)
989 preferred_family = filter.xsinfo.family;
994 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
998 struct xfrm_buffer xb;
999 char buf[NLMSG_DELETEALL_BUF_SIZE];
1003 xb.size = sizeof(buf);
1006 for (i = 0; ; i++) {
1011 fprintf(stderr, "Delete-all round = %d\n", i);
1013 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
1014 perror("Cannot send dump request");
1018 if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb) < 0) {
1019 fprintf(stderr, "Delete-all terminated\n");
1022 if (xb.nlmsg_count == 0) {
1024 fprintf(stderr, "Delete-all completed\n");
1028 if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) {
1029 perror("Failed to send delete-all request\n");
1033 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
1040 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
1041 perror("Cannot send dump request");
1045 if (rtnl_dump_filter(&rth, xfrm_state_print, stdout) < 0) {
1046 fprintf(stderr, "Dump terminated\n");
1056 int print_sadinfo(struct nlmsghdr *n, void *arg)
1058 FILE *fp = (FILE*)arg;
1059 __u32 *f = NLMSG_DATA(n);
1060 struct rtattr *tb[XFRMA_SAD_MAX+1];
1064 int len = n->nlmsg_len;
1066 len -= NLMSG_LENGTH(sizeof(__u32));
1068 fprintf(stderr, "SADinfo: Wrong len %d\n", len);
1072 rta = XFRMSAPD_RTA(f);
1073 parse_rtattr(tb, XFRMA_SAD_MAX, rta, len);
1075 if (tb[XFRMA_SAD_CNT]) {
1076 fprintf(fp,"\t SAD");
1077 cnt = (__u32 *)RTA_DATA(tb[XFRMA_SAD_CNT]);
1078 fprintf(fp," count %d", *cnt);
1080 fprintf(fp,"BAD SAD info returned\n");
1085 if (tb[XFRMA_SAD_HINFO]) {
1086 struct xfrmu_sadhinfo *si;
1088 if (RTA_PAYLOAD(tb[XFRMA_SAD_HINFO]) < sizeof(*si)) {
1089 fprintf(fp,"BAD SAD length returned\n");
1093 si = RTA_DATA(tb[XFRMA_SAD_HINFO]);
1094 fprintf(fp," (buckets ");
1095 fprintf(fp,"count %d", si->sadhcnt);
1096 fprintf(fp," Max %d", si->sadhmcnt);
1105 static int xfrm_sad_getinfo(int argc, char **argv)
1107 struct rtnl_handle rth;
1114 memset(&req, 0, sizeof(req));
1115 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.flags));
1116 req.n.nlmsg_flags = NLM_F_REQUEST;
1117 req.n.nlmsg_type = XFRM_MSG_GETSADINFO;
1118 req.flags = 0XFFFFFFFF;
1120 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1123 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0)
1126 print_sadinfo(&req.n, (void*)stdout);
1133 static int xfrm_state_flush(int argc, char **argv)
1135 struct rtnl_handle rth;
1138 struct xfrm_usersa_flush xsf;
1140 char *protop = NULL;
1142 memset(&req, 0, sizeof(req));
1144 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsf));
1145 req.n.nlmsg_flags = NLM_F_REQUEST;
1146 req.n.nlmsg_type = XFRM_MSG_FLUSHSA;
1150 if (strcmp(*argv, "proto") == 0) {
1154 duparg("proto", *argv);
1159 ret = xfrm_xfrmproto_getbyname(*argv);
1161 invarg("\"XFRM-PROTO\" is invalid", *argv);
1163 req.xsf.proto = (__u8)ret;
1165 invarg("unknown", *argv);
1170 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1174 fprintf(stderr, "Flush state proto=%s\n",
1175 strxf_xfrmproto(req.xsf.proto));
1177 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
1185 int do_xfrm_state(int argc, char **argv)
1188 return xfrm_state_list_or_deleteall(0, NULL, 0);
1190 if (matches(*argv, "add") == 0)
1191 return xfrm_state_modify(XFRM_MSG_NEWSA, 0,
1193 if (matches(*argv, "update") == 0)
1194 return xfrm_state_modify(XFRM_MSG_UPDSA, 0,
1196 if (matches(*argv, "allocspi") == 0)
1197 return xfrm_state_allocspi(argc-1, argv+1);
1198 if (matches(*argv, "delete") == 0)
1199 return xfrm_state_get_or_delete(argc-1, argv+1, 1);
1200 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
1201 return xfrm_state_list_or_deleteall(argc-1, argv+1, 1);
1202 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1203 || matches(*argv, "lst") == 0)
1204 return xfrm_state_list_or_deleteall(argc-1, argv+1, 0);
1205 if (matches(*argv, "get") == 0)
1206 return xfrm_state_get_or_delete(argc-1, argv+1, 0);
1207 if (matches(*argv, "flush") == 0)
1208 return xfrm_state_flush(argc-1, argv+1);
1209 if (matches(*argv, "count") == 0) {
1210 return xfrm_sad_getinfo(argc, argv);
1212 if (matches(*argv, "help") == 0)
1214 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv);