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 [ XFRM_OPT ] [ 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");
67 fprintf(stderr, "Usage: ip xfrm state count \n");
69 fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n");
70 //fprintf(stderr, "XFRM_PROTO := [ esp | ah | comp ]\n");
71 fprintf(stderr, "XFRM_PROTO := [ ");
72 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP));
73 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH));
74 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_COMP));
75 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING));
76 fprintf(stderr, "%s ", strxf_xfrmproto(IPPROTO_DSTOPTS));
77 fprintf(stderr, "]\n");
79 //fprintf(stderr, "SPI - security parameter index(default=0)\n");
81 fprintf(stderr, "MODE := [ transport | tunnel | ro | beet ](default=transport)\n");
82 //fprintf(stderr, "REQID - number(default=0)\n");
84 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
85 fprintf(stderr, "FLAG := [ noecn | decap-dscp | wildrecv ]\n");
87 fprintf(stderr, "ENCAP := ENCAP-TYPE SPORT DPORT OADDR\n");
88 fprintf(stderr, "ENCAP-TYPE := espinudp | espinudp-nonike\n");
90 fprintf(stderr, "ALGO-LIST := [ ALGO-LIST ] | [ ALGO ]\n");
91 fprintf(stderr, "ALGO := ALGO_TYPE ALGO_NAME ALGO_KEY\n");
92 fprintf(stderr, "ALGO_TYPE := [ ");
93 fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT));
94 fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_AUTH));
95 fprintf(stderr, "%s ", strxf_algotype(XFRMA_ALG_COMP));
96 fprintf(stderr, "]\n");
98 //fprintf(stderr, "ALGO_NAME - algorithm name\n");
99 //fprintf(stderr, "ALGO_KEY - algorithm key\n");
101 fprintf(stderr, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ UPSPEC ] [ dev DEV ]\n");
103 fprintf(stderr, "UPSPEC := proto PROTO [ [ sport PORT ] [ dport PORT ] |\n");
104 fprintf(stderr, " [ type NUMBER ] [ code NUMBER ] ]\n");
107 //fprintf(stderr, "DEV - device name(default=none)\n");
108 fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
109 fprintf(stderr, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
110 fprintf(stderr, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] COUNT ]\n");
114 static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type,
115 char *name, char *key, int max)
118 int slen = strlen(key);
121 /* XXX: verifying both name and key is required! */
122 fprintf(stderr, "warning: ALGONAME/ALGOKEY will send to kernel promiscuously!(verifying them isn't implemented yet)\n");
125 strncpy(alg->alg_name, name, sizeof(alg->alg_name));
127 if (slen > 2 && strncmp(key, "0x", 2) == 0) {
128 /* split two chars "0x" from the top */
134 /* Converting hexadecimal numbered string into real key;
135 * Convert each two chars into one char(value). If number
136 * of the length is odd, add zero on the top for rounding.
139 /* calculate length of the converted values(real key) */
140 len = (plen + 1) / 2;
142 invarg("\"ALGOKEY\" makes buffer overflow\n", key);
144 for (i = - (plen % 2), j = 0; j < len; i += 2, j++) {
148 vbuf[0] = i >= 0 ? p[i] : '0';
152 if (get_u8(&val, vbuf, 16))
153 invarg("\"ALGOKEY\" is invalid", key);
155 alg->alg_key[j] = val;
161 invarg("\"ALGOKEY\" makes buffer overflow\n", key);
163 strncpy(alg->alg_key, key, len);
167 alg->alg_key_len = len * 8;
172 static int xfrm_seq_parse(__u32 *seq, int *argcp, char ***argvp)
175 char **argv = *argvp;
177 if (get_u32(seq, *argv, 0))
178 invarg("\"SEQ\" is invalid", *argv);
188 static int xfrm_state_flag_parse(__u8 *flags, int *argcp, char ***argvp)
191 char **argv = *argvp;
192 int len = strlen(*argv);
194 if (len > 2 && strncmp(*argv, "0x", 2) == 0) {
197 if (get_u8(&val, *argv, 16))
198 invarg("\"FLAG\" is invalid", *argv);
202 if (strcmp(*argv, "noecn") == 0)
203 *flags |= XFRM_STATE_NOECN;
204 else if (strcmp(*argv, "decap-dscp") == 0)
205 *flags |= XFRM_STATE_DECAP_DSCP;
206 else if (strcmp(*argv, "wildrecv") == 0)
207 *flags |= XFRM_STATE_WILDRECV;
209 PREV_ARG(); /* back track */
219 filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
227 static int xfrm_state_modify(int cmd, unsigned flags, int argc, char **argv)
229 struct rtnl_handle rth;
232 struct xfrm_usersa_info xsinfo;
233 char buf[RTA_BUF_SIZE];
241 memset(&req, 0, sizeof(req));
243 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsinfo));
244 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
245 req.n.nlmsg_type = cmd;
246 req.xsinfo.family = preferred_family;
248 req.xsinfo.lft.soft_byte_limit = XFRM_INF;
249 req.xsinfo.lft.hard_byte_limit = XFRM_INF;
250 req.xsinfo.lft.soft_packet_limit = XFRM_INF;
251 req.xsinfo.lft.hard_packet_limit = XFRM_INF;
254 if (strcmp(*argv, "mode") == 0) {
256 xfrm_mode_parse(&req.xsinfo.mode, &argc, &argv);
257 } else if (strcmp(*argv, "reqid") == 0) {
259 xfrm_reqid_parse(&req.xsinfo.reqid, &argc, &argv);
260 } else if (strcmp(*argv, "seq") == 0) {
262 xfrm_seq_parse(&req.xsinfo.seq, &argc, &argv);
263 } else if (strcmp(*argv, "replay-window") == 0) {
265 if (get_u8(&req.xsinfo.replay_window, *argv, 0))
266 invarg("\"replay-window\" value is invalid", *argv);
267 } else if (strcmp(*argv, "flag") == 0) {
269 xfrm_state_flag_parse(&req.xsinfo.flags, &argc, &argv);
270 } else if (strcmp(*argv, "sel") == 0) {
272 xfrm_selector_parse(&req.xsinfo.sel, &argc, &argv);
273 } else if (strcmp(*argv, "limit") == 0) {
275 xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv);
276 } else if (strcmp(*argv, "encap") == 0) {
277 struct xfrm_encap_tmpl encap;
280 xfrm_encap_type_parse(&encap.encap_type, &argc, &argv);
282 if (get_u16(&encap.encap_sport, *argv, 0))
283 invarg("\"encap\" sport value is invalid", *argv);
284 encap.encap_sport = htons(encap.encap_sport);
286 if (get_u16(&encap.encap_dport, *argv, 0))
287 invarg("\"encap\" dport value is invalid", *argv);
288 encap.encap_dport = htons(encap.encap_dport);
290 get_addr(&oa, *argv, AF_UNSPEC);
291 memcpy(&encap.encap_oa, &oa.data, sizeof(encap.encap_oa));
292 addattr_l(&req.n, sizeof(req.buf), XFRMA_ENCAP,
293 (void *)&encap, sizeof(encap));
294 } else if (strcmp(*argv, "coa") == 0) {
299 duparg("coa", *argv);
304 get_prefix(&coa, *argv, preferred_family);
305 if (coa.family == AF_UNSPEC)
306 invarg("\"coa\" address family is AF_UNSPEC", *argv);
307 if (coa.bytelen > sizeof(xcoa))
308 invarg("\"coa\" address length is too large", *argv);
310 memset(&xcoa, 0, sizeof(xcoa));
311 memcpy(&xcoa, &coa.data, coa.bytelen);
313 addattr_l(&req.n, sizeof(req.buf), XFRMA_COADDR,
314 (void *)&xcoa, sizeof(xcoa));
316 /* try to assume ALGO */
317 int type = xfrm_algotype_getbyname(*argv);
319 case XFRMA_ALG_CRYPT:
325 struct xfrm_algo alg;
326 char buf[XFRM_ALGO_KEY_BUF_SIZE];
333 case XFRMA_ALG_CRYPT:
335 duparg("ALGOTYPE", *argv);
340 duparg("ALGOTYPE", *argv);
345 duparg("ALGOTYPE", *argv);
350 invarg("\"ALGOTYPE\" is invalid\n", *argv);
363 memset(&alg, 0, sizeof(alg));
365 xfrm_algo_parse((void *)&alg, type, name, key,
367 len = sizeof(struct xfrm_algo) + alg.alg.alg_key_len;
369 addattr_l(&req.n, sizeof(req.buf), type,
374 /* try to assume ID */
376 invarg("unknown", *argv);
380 xfrm_id_parse(&req.xsinfo.saddr, &req.xsinfo.id,
381 &req.xsinfo.family, 0, &argc, &argv);
382 if (preferred_family == AF_UNSPEC)
383 preferred_family = req.xsinfo.family;
390 fprintf(stderr, "Not enough information: \"ID\" is required\n");
394 switch (req.xsinfo.mode) {
395 case XFRM_MODE_TRANSPORT:
396 case XFRM_MODE_TUNNEL:
397 if (!xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
398 fprintf(stderr, "\"mode\" is invalid with proto=%s\n",
399 strxf_xfrmproto(req.xsinfo.id.proto));
403 case XFRM_MODE_ROUTEOPTIMIZATION:
404 case XFRM_MODE_IN_TRIGGER:
405 if (!xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
406 fprintf(stderr, "\"mode\" is invalid with proto=%s\n",
407 strxf_xfrmproto(req.xsinfo.id.proto));
410 if (req.xsinfo.id.spi != 0) {
411 fprintf(stderr, "\"spi\" must be 0 with proto=%s\n",
412 strxf_xfrmproto(req.xsinfo.id.proto));
420 if (ealgop || aalgop || calgop) {
421 if (!xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
422 fprintf(stderr, "\"ALGO\" is invalid with proto=%s\n",
423 strxf_xfrmproto(req.xsinfo.id.proto));
427 if (xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
428 fprintf(stderr, "\"ALGO\" is required with proto=%s\n",
429 strxf_xfrmproto(req.xsinfo.id.proto));
435 if (!xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
436 fprintf(stderr, "\"coa\" is invalid with proto=%s\n",
437 strxf_xfrmproto(req.xsinfo.id.proto));
441 if (xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
442 fprintf(stderr, "\"coa\" is required with proto=%s\n",
443 strxf_xfrmproto(req.xsinfo.id.proto));
448 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
451 if (req.xsinfo.family == AF_UNSPEC)
452 req.xsinfo.family = AF_INET;
454 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
462 static int xfrm_state_allocspi(int argc, char **argv)
464 struct rtnl_handle rth;
467 struct xfrm_userspi_info xspi;
468 char buf[RTA_BUF_SIZE];
473 char res_buf[NLMSG_BUF_SIZE];
474 struct nlmsghdr *res_n = (struct nlmsghdr *)res_buf;
476 memset(res_buf, 0, sizeof(res_buf));
478 memset(&req, 0, sizeof(req));
480 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xspi));
481 req.n.nlmsg_flags = NLM_F_REQUEST;
482 req.n.nlmsg_type = XFRM_MSG_ALLOCSPI;
483 req.xspi.info.family = preferred_family;
486 req.xsinfo.lft.soft_byte_limit = XFRM_INF;
487 req.xsinfo.lft.hard_byte_limit = XFRM_INF;
488 req.xsinfo.lft.soft_packet_limit = XFRM_INF;
489 req.xsinfo.lft.hard_packet_limit = XFRM_INF;
493 if (strcmp(*argv, "mode") == 0) {
495 xfrm_mode_parse(&req.xspi.info.mode, &argc, &argv);
496 } else if (strcmp(*argv, "reqid") == 0) {
498 xfrm_reqid_parse(&req.xspi.info.reqid, &argc, &argv);
499 } else if (strcmp(*argv, "seq") == 0) {
501 xfrm_seq_parse(&req.xspi.info.seq, &argc, &argv);
502 } else if (strcmp(*argv, "min") == 0) {
504 duparg("min", *argv);
509 if (get_u32(&req.xspi.min, *argv, 0))
510 invarg("\"min\" value is invalid", *argv);
511 } else if (strcmp(*argv, "max") == 0) {
513 duparg("max", *argv);
518 if (get_u32(&req.xspi.max, *argv, 0))
519 invarg("\"max\" value is invalid", *argv);
521 /* try to assume ID */
523 invarg("unknown", *argv);
527 xfrm_id_parse(&req.xspi.info.saddr, &req.xspi.info.id,
528 &req.xspi.info.family, 0, &argc, &argv);
529 if (req.xspi.info.id.spi) {
530 fprintf(stderr, "\"SPI\" must be zero\n");
533 if (preferred_family == AF_UNSPEC)
534 preferred_family = req.xspi.info.family;
540 fprintf(stderr, "Not enough information: \"ID\" is required\n");
546 fprintf(stderr, "\"max\" is missing\n");
549 if (req.xspi.min > req.xspi.max) {
550 fprintf(stderr, "\"min\" valie is larger than \"max\" one\n");
555 fprintf(stderr, "\"min\" is missing\n");
559 /* XXX: Default value defined in PF_KEY;
560 * See kernel's net/key/af_key.c(pfkey_getspi).
562 req.xspi.min = 0x100;
563 req.xspi.max = 0x0fffffff;
565 /* XXX: IPCOMP spi is 16-bits;
566 * See kernel's net/xfrm/xfrm_user(verify_userspi_info).
568 if (req.xspi.info.id.proto == IPPROTO_COMP)
569 req.xspi.max = 0xffff;
572 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
575 if (req.xspi.info.family == AF_UNSPEC)
576 req.xspi.info.family = AF_INET;
579 if (rtnl_talk(&rth, &req.n, 0, 0, res_n, NULL, NULL) < 0)
582 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
583 fprintf(stderr, "An error :-)\n");
592 static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo)
597 if (filter.id_src_mask)
598 if (xfrm_addr_match(&xsinfo->saddr, &filter.xsinfo.saddr,
601 if (filter.id_dst_mask)
602 if (xfrm_addr_match(&xsinfo->id.daddr, &filter.xsinfo.id.daddr,
605 if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask)
607 if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask)
609 if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask)
611 if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask)
613 if (filter.state_flags_mask)
614 if ((xsinfo->flags & filter.xsinfo.flags) == 0)
620 int xfrm_state_print(const struct sockaddr_nl *who, struct nlmsghdr *n,
623 FILE *fp = (FILE*)arg;
624 struct rtattr * tb[XFRMA_MAX+1];
626 struct xfrm_usersa_info *xsinfo = NULL;
627 struct xfrm_user_expire *xexp = NULL;
628 struct xfrm_usersa_id *xsid = NULL;
629 int len = n->nlmsg_len;
631 if (n->nlmsg_type != XFRM_MSG_NEWSA &&
632 n->nlmsg_type != XFRM_MSG_DELSA &&
633 n->nlmsg_type != XFRM_MSG_UPDSA &&
634 n->nlmsg_type != XFRM_MSG_EXPIRE) {
635 fprintf(stderr, "Not a state: %08x %08x %08x\n",
636 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
640 if (n->nlmsg_type == XFRM_MSG_DELSA) {
641 /* Dont blame me for this .. Herbert made me do it */
642 xsid = NLMSG_DATA(n);
643 len -= NLMSG_SPACE(sizeof(*xsid));
644 } else if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
645 xexp = NLMSG_DATA(n);
646 xsinfo = &xexp->state;
647 len -= NLMSG_SPACE(sizeof(*xexp));
650 xsinfo = NLMSG_DATA(n);
651 len -= NLMSG_SPACE(sizeof(*xsinfo));
655 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
659 if (xsinfo && !xfrm_state_filter_match(xsinfo))
662 if (n->nlmsg_type == XFRM_MSG_DELSA)
663 fprintf(fp, "Deleted ");
664 else if (n->nlmsg_type == XFRM_MSG_UPDSA)
665 fprintf(fp, "Updated ");
666 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
667 fprintf(fp, "Expired ");
669 if (n->nlmsg_type == XFRM_MSG_DELSA)
670 rta = XFRMSID_RTA(xsid);
671 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
672 rta = XFRMEXP_RTA(xexp);
674 rta = XFRMS_RTA(xsinfo);
676 parse_rtattr(tb, XFRMA_MAX, rta, len);
678 if (n->nlmsg_type == XFRM_MSG_DELSA) {
679 //xfrm_policy_id_print();
682 fprintf(stderr, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
685 if (RTA_PAYLOAD(tb[XFRMA_SA]) < sizeof(*xsinfo)) {
686 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
689 xsinfo = (struct xfrm_usersa_info *)RTA_DATA(tb[XFRMA_SA]);
692 xfrm_state_info_print(xsinfo, tb, fp, NULL, NULL);
694 if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
696 fprintf(fp, "hard %u", xexp->hard);
697 fprintf(fp, "%s", _SL_);
707 static int xfrm_state_get_or_delete(int argc, char **argv, int delete)
709 struct rtnl_handle rth;
712 struct xfrm_usersa_id xsid;
713 char buf[RTA_BUF_SIZE];
718 memset(&req, 0, sizeof(req));
720 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid));
721 req.n.nlmsg_flags = NLM_F_REQUEST;
722 req.n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA;
723 req.xsid.family = preferred_family;
726 xfrm_address_t saddr;
729 invarg("unknown", *argv);
733 memset(&id, 0, sizeof(id));
734 memset(&saddr, 0, sizeof(saddr));
735 xfrm_id_parse(&saddr, &id, &req.xsid.family, 0,
738 memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr));
739 req.xsid.spi = id.spi;
740 req.xsid.proto = id.proto;
742 addattr_l(&req.n, sizeof(req.buf), XFRMA_SRCADDR,
743 (void *)&saddr, sizeof(saddr));
748 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
751 if (req.xsid.family == AF_UNSPEC)
752 req.xsid.family = AF_INET;
755 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
758 char buf[NLMSG_BUF_SIZE];
759 struct nlmsghdr *res_n = (struct nlmsghdr *)buf;
761 memset(buf, 0, sizeof(buf));
763 if (rtnl_talk(&rth, &req.n, 0, 0, res_n, NULL, NULL) < 0)
766 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
767 fprintf(stderr, "An error :-)\n");
778 * With an existing state of nlmsg, make new nlmsg for deleting the state
779 * and store it to buffer.
781 static int xfrm_state_keep(const struct sockaddr_nl *who,
785 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
786 struct rtnl_handle *rth = xb->rth;
787 struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n);
788 int len = n->nlmsg_len;
789 struct nlmsghdr *new_n;
790 struct xfrm_usersa_id *xsid;
792 if (n->nlmsg_type != XFRM_MSG_NEWSA) {
793 fprintf(stderr, "Not a state: %08x %08x %08x\n",
794 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
798 len -= NLMSG_LENGTH(sizeof(*xsinfo));
800 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
804 if (!xfrm_state_filter_match(xsinfo))
807 if (xb->offset > xb->size) {
808 fprintf(stderr, "State buffer overflow\n");
812 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
813 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid));
814 new_n->nlmsg_flags = NLM_F_REQUEST;
815 new_n->nlmsg_type = XFRM_MSG_DELSA;
816 new_n->nlmsg_seq = ++rth->seq;
818 xsid = NLMSG_DATA(new_n);
819 xsid->family = xsinfo->family;
820 memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr));
821 xsid->spi = xsinfo->id.spi;
822 xsid->proto = xsinfo->id.proto;
824 addattr_l(new_n, xb->size, XFRMA_SRCADDR, &xsinfo->saddr,
825 sizeof(xsid->daddr));
827 xb->offset += new_n->nlmsg_len;
833 static int xfrm_state_list_or_deleteall(int argc, char **argv, int deleteall)
836 struct rtnl_handle rth;
840 filter.xsinfo.family = preferred_family;
843 if (strcmp(*argv, "mode") == 0) {
845 xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv);
847 filter.mode_mask = XFRM_FILTER_MASK_FULL;
849 } else if (strcmp(*argv, "reqid") == 0) {
851 xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv);
853 filter.reqid_mask = XFRM_FILTER_MASK_FULL;
855 } else if (strcmp(*argv, "flag") == 0) {
857 xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv);
859 filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
863 invarg("unknown", *argv);
867 xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id,
868 &filter.xsinfo.family, 1, &argc, &argv);
869 if (preferred_family == AF_UNSPEC)
870 preferred_family = filter.xsinfo.family;
875 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
879 struct xfrm_buffer xb;
880 char buf[NLMSG_DELETEALL_BUF_SIZE];
884 xb.size = sizeof(buf);
892 fprintf(stderr, "Delete-all round = %d\n", i);
894 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
895 perror("Cannot send dump request");
899 if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb, NULL, NULL) < 0) {
900 fprintf(stderr, "Delete-all terminated\n");
903 if (xb.nlmsg_count == 0) {
905 fprintf(stderr, "Delete-all completed\n");
909 if (rtnl_send(&rth, xb.buf, xb.offset) < 0) {
910 perror("Failed to send delete-all request\n");
914 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
921 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
922 perror("Cannot send dump request");
926 if (rtnl_dump_filter(&rth, xfrm_state_print, stdout, NULL, NULL) < 0) {
927 fprintf(stderr, "Dump terminated\n");
937 int print_sadinfo(struct nlmsghdr *n, void *arg)
939 FILE *fp = (FILE*)arg;
940 __u32 *f = NLMSG_DATA(n);
941 struct rtattr *tb[XFRMA_SAD_MAX+1];
943 struct xfrmu_sadhinfo *si;
946 int len = n->nlmsg_len;
948 len -= NLMSG_LENGTH(sizeof(__u32));
950 fprintf(stderr, "SADinfo: Wrong len %d\n", len);
954 rta = XFRMSAPD_RTA(f);
955 parse_rtattr(tb, XFRMA_SAD_MAX, rta, len);
957 if (tb[XFRMA_SADCNT]) {
958 fprintf(fp,"\t SAD");
959 cnt = (__u32 *)RTA_DATA(tb[XFRMA_SADCNT]);
960 fprintf(fp," count %d", *cnt);
962 fprintf(fp,"BAD SAD info returned\n");
967 if (!tb[XFRMA_SADHINFO]) {
969 if (RTA_PAYLOAD(tb[XFRMA_SADHINFO]) < sizeof(*si)) {
970 fprintf(fp,"BAD SAD length returned\n");
974 si = (struct xfrmu_sadhinfo *)RTA_DATA(tb[XFRMA_SADHINFO]);
975 fprintf(fp," (buckets ");
976 fprintf(fp,"count %d", si->sadhcnt);
977 fprintf(fp," Max %d", si->sadhmcnt);
985 static int xfrm_sad_getinfo(int argc, char **argv)
987 struct rtnl_handle rth;
994 memset(&req, 0, sizeof(req));
995 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.flags));
996 req.n.nlmsg_flags = NLM_F_REQUEST;
997 req.n.nlmsg_type = XFRM_MSG_GETSADINFO;
998 req.flags = 0XFFFFFFFF;
1000 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1003 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0)
1006 print_sadinfo(&req.n, (void*)stdout);
1013 static int xfrm_state_flush(int argc, char **argv)
1015 struct rtnl_handle rth;
1018 struct xfrm_usersa_flush xsf;
1020 char *protop = NULL;
1022 memset(&req, 0, sizeof(req));
1024 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsf));
1025 req.n.nlmsg_flags = NLM_F_REQUEST;
1026 req.n.nlmsg_type = XFRM_MSG_FLUSHSA;
1030 if (strcmp(*argv, "proto") == 0) {
1034 duparg("proto", *argv);
1039 ret = xfrm_xfrmproto_getbyname(*argv);
1041 invarg("\"XFRM_PROTO\" is invalid", *argv);
1043 req.xsf.proto = (__u8)ret;
1045 invarg("unknown", *argv);
1050 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1054 fprintf(stderr, "Flush state proto=%s\n",
1055 (req.xsf.proto == IPSEC_PROTO_ANY) ? "any" :
1056 strxf_xfrmproto(req.xsf.proto));
1058 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
1066 int do_xfrm_state(int argc, char **argv)
1069 return xfrm_state_list_or_deleteall(0, NULL, 0);
1071 if (matches(*argv, "add") == 0)
1072 return xfrm_state_modify(XFRM_MSG_NEWSA, 0,
1074 if (matches(*argv, "update") == 0)
1075 return xfrm_state_modify(XFRM_MSG_UPDSA, 0,
1077 if (matches(*argv, "allocspi") == 0)
1078 return xfrm_state_allocspi(argc-1, argv+1);
1079 if (matches(*argv, "delete") == 0)
1080 return xfrm_state_get_or_delete(argc-1, argv+1, 1);
1081 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
1082 return xfrm_state_list_or_deleteall(argc-1, argv+1, 1);
1083 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1084 || matches(*argv, "lst") == 0)
1085 return xfrm_state_list_or_deleteall(argc-1, argv+1, 0);
1086 if (matches(*argv, "get") == 0)
1087 return xfrm_state_get_or_delete(argc-1, argv+1, 0);
1088 if (matches(*argv, "flush") == 0)
1089 return xfrm_state_flush(argc-1, argv+1);
1090 if (matches(*argv, "count") == 0) {
1091 return xfrm_sad_getinfo(argc, argv);
1093 if (matches(*argv, "help") == 0)
1095 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv);