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 */
225 static int xfrm_state_modify(int cmd, unsigned flags, int argc, char **argv)
227 struct rtnl_handle rth;
230 struct xfrm_usersa_info xsinfo;
231 char buf[RTA_BUF_SIZE];
239 memset(&req, 0, sizeof(req));
241 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsinfo));
242 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
243 req.n.nlmsg_type = cmd;
244 req.xsinfo.family = preferred_family;
246 req.xsinfo.lft.soft_byte_limit = XFRM_INF;
247 req.xsinfo.lft.hard_byte_limit = XFRM_INF;
248 req.xsinfo.lft.soft_packet_limit = XFRM_INF;
249 req.xsinfo.lft.hard_packet_limit = XFRM_INF;
252 if (strcmp(*argv, "mode") == 0) {
254 xfrm_mode_parse(&req.xsinfo.mode, &argc, &argv);
255 } else if (strcmp(*argv, "reqid") == 0) {
257 xfrm_reqid_parse(&req.xsinfo.reqid, &argc, &argv);
258 } else if (strcmp(*argv, "seq") == 0) {
260 xfrm_seq_parse(&req.xsinfo.seq, &argc, &argv);
261 } else if (strcmp(*argv, "replay-window") == 0) {
263 if (get_u8(&req.xsinfo.replay_window, *argv, 0))
264 invarg("\"replay-window\" value is invalid", *argv);
265 } else if (strcmp(*argv, "flag") == 0) {
267 xfrm_state_flag_parse(&req.xsinfo.flags, &argc, &argv);
268 } else if (strcmp(*argv, "sel") == 0) {
270 xfrm_selector_parse(&req.xsinfo.sel, &argc, &argv);
271 } else if (strcmp(*argv, "limit") == 0) {
273 xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv);
274 } else if (strcmp(*argv, "encap") == 0) {
275 struct xfrm_encap_tmpl encap;
278 xfrm_encap_type_parse(&encap.encap_type, &argc, &argv);
280 if (get_u16(&encap.encap_sport, *argv, 0))
281 invarg("\"encap\" sport value is invalid", *argv);
282 encap.encap_sport = htons(encap.encap_sport);
284 if (get_u16(&encap.encap_dport, *argv, 0))
285 invarg("\"encap\" dport value is invalid", *argv);
286 encap.encap_dport = htons(encap.encap_dport);
288 get_addr(&oa, *argv, AF_UNSPEC);
289 memcpy(&encap.encap_oa, &oa.data, sizeof(encap.encap_oa));
290 addattr_l(&req.n, sizeof(req.buf), XFRMA_ENCAP,
291 (void *)&encap, sizeof(encap));
292 } else if (strcmp(*argv, "coa") == 0) {
297 duparg("coa", *argv);
302 get_prefix(&coa, *argv, preferred_family);
303 if (coa.family == AF_UNSPEC)
304 invarg("\"coa\" address family is AF_UNSPEC", *argv);
305 if (coa.bytelen > sizeof(xcoa))
306 invarg("\"coa\" address length is too large", *argv);
308 memset(&xcoa, 0, sizeof(xcoa));
309 memcpy(&xcoa, &coa.data, coa.bytelen);
311 addattr_l(&req.n, sizeof(req.buf), XFRMA_COADDR,
312 (void *)&xcoa, sizeof(xcoa));
314 /* try to assume ALGO */
315 int type = xfrm_algotype_getbyname(*argv);
317 case XFRMA_ALG_CRYPT:
323 struct xfrm_algo alg;
324 char buf[XFRM_ALGO_KEY_BUF_SIZE];
331 case XFRMA_ALG_CRYPT:
333 duparg("ALGOTYPE", *argv);
338 duparg("ALGOTYPE", *argv);
343 duparg("ALGOTYPE", *argv);
348 invarg("\"ALGOTYPE\" is invalid\n", *argv);
361 memset(&alg, 0, sizeof(alg));
363 xfrm_algo_parse((void *)&alg, type, name, key,
365 len = sizeof(struct xfrm_algo) + alg.alg.alg_key_len;
367 addattr_l(&req.n, sizeof(req.buf), type,
372 /* try to assume ID */
374 invarg("unknown", *argv);
378 xfrm_id_parse(&req.xsinfo.saddr, &req.xsinfo.id,
379 &req.xsinfo.family, 0, &argc, &argv);
380 if (preferred_family == AF_UNSPEC)
381 preferred_family = req.xsinfo.family;
388 fprintf(stderr, "Not enough information: \"ID\" is required\n");
392 switch (req.xsinfo.mode) {
393 case XFRM_MODE_TRANSPORT:
394 case XFRM_MODE_TUNNEL:
395 if (!xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
396 fprintf(stderr, "\"mode\" is invalid with proto=%s\n",
397 strxf_xfrmproto(req.xsinfo.id.proto));
401 case XFRM_MODE_ROUTEOPTIMIZATION:
402 case XFRM_MODE_IN_TRIGGER:
403 if (!xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
404 fprintf(stderr, "\"mode\" is invalid with proto=%s\n",
405 strxf_xfrmproto(req.xsinfo.id.proto));
408 if (req.xsinfo.id.spi != 0) {
409 fprintf(stderr, "\"spi\" must be 0 with proto=%s\n",
410 strxf_xfrmproto(req.xsinfo.id.proto));
418 if (ealgop || aalgop || calgop) {
419 if (!xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
420 fprintf(stderr, "\"ALGO\" is invalid with proto=%s\n",
421 strxf_xfrmproto(req.xsinfo.id.proto));
425 if (xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
426 fprintf(stderr, "\"ALGO\" is required with proto=%s\n",
427 strxf_xfrmproto(req.xsinfo.id.proto));
433 if (!xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
434 fprintf(stderr, "\"coa\" is invalid with proto=%s\n",
435 strxf_xfrmproto(req.xsinfo.id.proto));
439 if (xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
440 fprintf(stderr, "\"coa\" is required with proto=%s\n",
441 strxf_xfrmproto(req.xsinfo.id.proto));
446 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
449 if (req.xsinfo.family == AF_UNSPEC)
450 req.xsinfo.family = AF_INET;
452 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
460 static int xfrm_state_allocspi(int argc, char **argv)
462 struct rtnl_handle rth;
465 struct xfrm_userspi_info xspi;
466 char buf[RTA_BUF_SIZE];
471 char res_buf[NLMSG_BUF_SIZE];
472 struct nlmsghdr *res_n = (struct nlmsghdr *)res_buf;
474 memset(res_buf, 0, sizeof(res_buf));
476 memset(&req, 0, sizeof(req));
478 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xspi));
479 req.n.nlmsg_flags = NLM_F_REQUEST;
480 req.n.nlmsg_type = XFRM_MSG_ALLOCSPI;
481 req.xspi.info.family = preferred_family;
484 req.xsinfo.lft.soft_byte_limit = XFRM_INF;
485 req.xsinfo.lft.hard_byte_limit = XFRM_INF;
486 req.xsinfo.lft.soft_packet_limit = XFRM_INF;
487 req.xsinfo.lft.hard_packet_limit = XFRM_INF;
491 if (strcmp(*argv, "mode") == 0) {
493 xfrm_mode_parse(&req.xspi.info.mode, &argc, &argv);
494 } else if (strcmp(*argv, "reqid") == 0) {
496 xfrm_reqid_parse(&req.xspi.info.reqid, &argc, &argv);
497 } else if (strcmp(*argv, "seq") == 0) {
499 xfrm_seq_parse(&req.xspi.info.seq, &argc, &argv);
500 } else if (strcmp(*argv, "min") == 0) {
502 duparg("min", *argv);
507 if (get_u32(&req.xspi.min, *argv, 0))
508 invarg("\"min\" value is invalid", *argv);
509 } else if (strcmp(*argv, "max") == 0) {
511 duparg("max", *argv);
516 if (get_u32(&req.xspi.max, *argv, 0))
517 invarg("\"max\" value is invalid", *argv);
519 /* try to assume ID */
521 invarg("unknown", *argv);
525 xfrm_id_parse(&req.xspi.info.saddr, &req.xspi.info.id,
526 &req.xspi.info.family, 0, &argc, &argv);
527 if (req.xspi.info.id.spi) {
528 fprintf(stderr, "\"SPI\" must be zero\n");
531 if (preferred_family == AF_UNSPEC)
532 preferred_family = req.xspi.info.family;
538 fprintf(stderr, "Not enough information: \"ID\" is required\n");
544 fprintf(stderr, "\"max\" is missing\n");
547 if (req.xspi.min > req.xspi.max) {
548 fprintf(stderr, "\"min\" valie is larger than \"max\" one\n");
553 fprintf(stderr, "\"min\" is missing\n");
557 /* XXX: Default value defined in PF_KEY;
558 * See kernel's net/key/af_key.c(pfkey_getspi).
560 req.xspi.min = 0x100;
561 req.xspi.max = 0x0fffffff;
563 /* XXX: IPCOMP spi is 16-bits;
564 * See kernel's net/xfrm/xfrm_user(verify_userspi_info).
566 if (req.xspi.info.id.proto == IPPROTO_COMP)
567 req.xspi.max = 0xffff;
570 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
573 if (req.xspi.info.family == AF_UNSPEC)
574 req.xspi.info.family = AF_INET;
577 if (rtnl_talk(&rth, &req.n, 0, 0, res_n, NULL, NULL) < 0)
580 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
581 fprintf(stderr, "An error :-)\n");
590 static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo)
595 if (filter.id_src_mask)
596 if (xfrm_addr_match(&xsinfo->saddr, &filter.xsinfo.saddr,
599 if (filter.id_dst_mask)
600 if (xfrm_addr_match(&xsinfo->id.daddr, &filter.xsinfo.id.daddr,
603 if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask)
605 if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask)
607 if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask)
609 if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask)
611 if (filter.state_flags_mask)
612 if ((xsinfo->flags & filter.xsinfo.flags) == 0)
618 int xfrm_state_print(const struct sockaddr_nl *who, struct nlmsghdr *n,
621 FILE *fp = (FILE*)arg;
622 struct rtattr * tb[XFRMA_MAX+1];
624 struct xfrm_usersa_info *xsinfo = NULL;
625 struct xfrm_user_expire *xexp = NULL;
626 struct xfrm_usersa_id *xsid = NULL;
627 int len = n->nlmsg_len;
629 if (n->nlmsg_type != XFRM_MSG_NEWSA &&
630 n->nlmsg_type != XFRM_MSG_DELSA &&
631 n->nlmsg_type != XFRM_MSG_UPDSA &&
632 n->nlmsg_type != XFRM_MSG_EXPIRE) {
633 fprintf(stderr, "Not a state: %08x %08x %08x\n",
634 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
638 if (n->nlmsg_type == XFRM_MSG_DELSA) {
639 /* Dont blame me for this .. Herbert made me do it */
640 xsid = NLMSG_DATA(n);
641 len -= NLMSG_SPACE(sizeof(*xsid));
642 } else if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
643 xexp = NLMSG_DATA(n);
644 xsinfo = &xexp->state;
645 len -= NLMSG_SPACE(sizeof(*xexp));
648 xsinfo = NLMSG_DATA(n);
649 len -= NLMSG_SPACE(sizeof(*xsinfo));
653 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
657 if (xsinfo && !xfrm_state_filter_match(xsinfo))
660 if (n->nlmsg_type == XFRM_MSG_DELSA)
661 fprintf(fp, "Deleted ");
662 else if (n->nlmsg_type == XFRM_MSG_UPDSA)
663 fprintf(fp, "Updated ");
664 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
665 fprintf(fp, "Expired ");
667 if (n->nlmsg_type == XFRM_MSG_DELSA)
668 rta = XFRMSID_RTA(xsid);
669 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
670 rta = XFRMEXP_RTA(xexp);
672 rta = XFRMS_RTA(xsinfo);
674 parse_rtattr(tb, XFRMA_MAX, rta, len);
676 if (n->nlmsg_type == XFRM_MSG_DELSA) {
677 //xfrm_policy_id_print();
680 fprintf(stderr, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
683 if (RTA_PAYLOAD(tb[XFRMA_SA]) < sizeof(*xsinfo)) {
684 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
687 xsinfo = RTA_DATA(tb[XFRMA_SA]);
690 xfrm_state_info_print(xsinfo, tb, fp, NULL, NULL);
692 if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
694 fprintf(fp, "hard %u", xexp->hard);
695 fprintf(fp, "%s", _SL_);
705 static int xfrm_state_get_or_delete(int argc, char **argv, int delete)
707 struct rtnl_handle rth;
710 struct xfrm_usersa_id xsid;
711 char buf[RTA_BUF_SIZE];
716 memset(&req, 0, sizeof(req));
718 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid));
719 req.n.nlmsg_flags = NLM_F_REQUEST;
720 req.n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA;
721 req.xsid.family = preferred_family;
724 xfrm_address_t saddr;
727 invarg("unknown", *argv);
731 memset(&id, 0, sizeof(id));
732 memset(&saddr, 0, sizeof(saddr));
733 xfrm_id_parse(&saddr, &id, &req.xsid.family, 0,
736 memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr));
737 req.xsid.spi = id.spi;
738 req.xsid.proto = id.proto;
740 addattr_l(&req.n, sizeof(req.buf), XFRMA_SRCADDR,
741 (void *)&saddr, sizeof(saddr));
746 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
749 if (req.xsid.family == AF_UNSPEC)
750 req.xsid.family = AF_INET;
753 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
756 char buf[NLMSG_BUF_SIZE];
757 struct nlmsghdr *res_n = (struct nlmsghdr *)buf;
759 memset(buf, 0, sizeof(buf));
761 if (rtnl_talk(&rth, &req.n, 0, 0, res_n, NULL, NULL) < 0)
764 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
765 fprintf(stderr, "An error :-)\n");
776 * With an existing state of nlmsg, make new nlmsg for deleting the state
777 * and store it to buffer.
779 static int xfrm_state_keep(const struct sockaddr_nl *who,
783 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
784 struct rtnl_handle *rth = xb->rth;
785 struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n);
786 int len = n->nlmsg_len;
787 struct nlmsghdr *new_n;
788 struct xfrm_usersa_id *xsid;
790 if (n->nlmsg_type != XFRM_MSG_NEWSA) {
791 fprintf(stderr, "Not a state: %08x %08x %08x\n",
792 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
796 len -= NLMSG_LENGTH(sizeof(*xsinfo));
798 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
802 if (!xfrm_state_filter_match(xsinfo))
805 if (xb->offset > xb->size) {
806 fprintf(stderr, "State buffer overflow\n");
810 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
811 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid));
812 new_n->nlmsg_flags = NLM_F_REQUEST;
813 new_n->nlmsg_type = XFRM_MSG_DELSA;
814 new_n->nlmsg_seq = ++rth->seq;
816 xsid = NLMSG_DATA(new_n);
817 xsid->family = xsinfo->family;
818 memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr));
819 xsid->spi = xsinfo->id.spi;
820 xsid->proto = xsinfo->id.proto;
822 addattr_l(new_n, xb->size, XFRMA_SRCADDR, &xsinfo->saddr,
823 sizeof(xsid->daddr));
825 xb->offset += new_n->nlmsg_len;
831 static int xfrm_state_list_or_deleteall(int argc, char **argv, int deleteall)
834 struct rtnl_handle rth;
838 filter.xsinfo.family = preferred_family;
841 if (strcmp(*argv, "mode") == 0) {
843 xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv);
845 filter.mode_mask = XFRM_FILTER_MASK_FULL;
847 } else if (strcmp(*argv, "reqid") == 0) {
849 xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv);
851 filter.reqid_mask = XFRM_FILTER_MASK_FULL;
853 } else if (strcmp(*argv, "flag") == 0) {
855 xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv);
857 filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
861 invarg("unknown", *argv);
865 xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id,
866 &filter.xsinfo.family, 1, &argc, &argv);
867 if (preferred_family == AF_UNSPEC)
868 preferred_family = filter.xsinfo.family;
873 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
877 struct xfrm_buffer xb;
878 char buf[NLMSG_DELETEALL_BUF_SIZE];
882 xb.size = sizeof(buf);
890 fprintf(stderr, "Delete-all round = %d\n", i);
892 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
893 perror("Cannot send dump request");
897 if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb, NULL, NULL) < 0) {
898 fprintf(stderr, "Delete-all terminated\n");
901 if (xb.nlmsg_count == 0) {
903 fprintf(stderr, "Delete-all completed\n");
907 if (rtnl_send(&rth, xb.buf, xb.offset) < 0) {
908 perror("Failed to send delete-all request\n");
912 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
919 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
920 perror("Cannot send dump request");
924 if (rtnl_dump_filter(&rth, xfrm_state_print, stdout, NULL, NULL) < 0) {
925 fprintf(stderr, "Dump terminated\n");
935 int print_sadinfo(struct nlmsghdr *n, void *arg)
937 FILE *fp = (FILE*)arg;
938 __u32 *f = NLMSG_DATA(n);
939 struct rtattr *tb[XFRMA_SAD_MAX+1];
943 int len = n->nlmsg_len;
945 len -= NLMSG_LENGTH(sizeof(__u32));
947 fprintf(stderr, "SADinfo: Wrong len %d\n", len);
951 rta = XFRMSAPD_RTA(f);
952 parse_rtattr(tb, XFRMA_SAD_MAX, rta, len);
954 if (tb[XFRMA_SAD_CNT]) {
955 fprintf(fp,"\t SAD");
956 cnt = (__u32 *)RTA_DATA(tb[XFRMA_SAD_CNT]);
957 fprintf(fp," count %d", *cnt);
959 fprintf(fp,"BAD SAD info returned\n");
964 if (tb[XFRMA_SAD_HINFO]) {
965 struct xfrmu_sadhinfo *si;
967 if (RTA_PAYLOAD(tb[XFRMA_SAD_HINFO]) < sizeof(*si)) {
968 fprintf(fp,"BAD SAD length returned\n");
972 si = RTA_DATA(tb[XFRMA_SAD_HINFO]);
973 fprintf(fp," (buckets ");
974 fprintf(fp,"count %d", si->sadhcnt);
975 fprintf(fp," Max %d", si->sadhmcnt);
984 static int xfrm_sad_getinfo(int argc, char **argv)
986 struct rtnl_handle rth;
993 memset(&req, 0, sizeof(req));
994 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.flags));
995 req.n.nlmsg_flags = NLM_F_REQUEST;
996 req.n.nlmsg_type = XFRM_MSG_GETSADINFO;
997 req.flags = 0XFFFFFFFF;
999 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1002 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0)
1005 print_sadinfo(&req.n, (void*)stdout);
1012 static int xfrm_state_flush(int argc, char **argv)
1014 struct rtnl_handle rth;
1017 struct xfrm_usersa_flush xsf;
1019 char *protop = NULL;
1021 memset(&req, 0, sizeof(req));
1023 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsf));
1024 req.n.nlmsg_flags = NLM_F_REQUEST;
1025 req.n.nlmsg_type = XFRM_MSG_FLUSHSA;
1029 if (strcmp(*argv, "proto") == 0) {
1033 duparg("proto", *argv);
1038 ret = xfrm_xfrmproto_getbyname(*argv);
1040 invarg("\"XFRM_PROTO\" is invalid", *argv);
1042 req.xsf.proto = (__u8)ret;
1044 invarg("unknown", *argv);
1049 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1053 fprintf(stderr, "Flush state proto=%s\n",
1054 (req.xsf.proto == IPSEC_PROTO_ANY) ? "any" :
1055 strxf_xfrmproto(req.xsf.proto));
1057 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
1065 int do_xfrm_state(int argc, char **argv)
1068 return xfrm_state_list_or_deleteall(0, NULL, 0);
1070 if (matches(*argv, "add") == 0)
1071 return xfrm_state_modify(XFRM_MSG_NEWSA, 0,
1073 if (matches(*argv, "update") == 0)
1074 return xfrm_state_modify(XFRM_MSG_UPDSA, 0,
1076 if (matches(*argv, "allocspi") == 0)
1077 return xfrm_state_allocspi(argc-1, argv+1);
1078 if (matches(*argv, "delete") == 0)
1079 return xfrm_state_get_or_delete(argc-1, argv+1, 1);
1080 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
1081 return xfrm_state_list_or_deleteall(argc-1, argv+1, 1);
1082 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1083 || matches(*argv, "lst") == 0)
1084 return xfrm_state_list_or_deleteall(argc-1, argv+1, 0);
1085 if (matches(*argv, "get") == 0)
1086 return xfrm_state_get_or_delete(argc-1, argv+1, 0);
1087 if (matches(*argv, "flush") == 0)
1088 return xfrm_state_flush(argc-1, argv+1);
1089 if (matches(*argv, "count") == 0) {
1090 return xfrm_sad_getinfo(argc, argv);
1092 if (matches(*argv, "help") == 0)
1094 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv);