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 ] [ replay-seq SEQ ]\n");
61 fprintf(stderr, " [ replay-oseq SEQ ] [ LIMIT-LIST ]\n");
62 fprintf(stderr, "Usage: ip xfrm state allocspi ID [ mode MODE ] [ reqid REQID ] [ seq SEQ ]\n");
63 fprintf(stderr, " [ min SPI max SPI ]\n");
64 fprintf(stderr, "Usage: ip xfrm state { delete | get } ID\n");
65 fprintf(stderr, "Usage: ip xfrm state { deleteall | list } [ ID ] [ mode MODE ] [ reqid REQID ]\n");
66 fprintf(stderr, " [ flag FLAG-LIST ]\n");
67 fprintf(stderr, "Usage: ip xfrm state flush [ proto XFRM_PROTO ]\n");
68 fprintf(stderr, "Usage: ip xfrm state count \n");
70 fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ] [mark MARK [mask MASK]]\n");
71 //fprintf(stderr, "XFRM_PROTO := [ esp | ah | comp ]\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 ", strxf_xfrmproto(IPPROTO_DSTOPTS));
78 fprintf(stderr, "]\n");
80 //fprintf(stderr, "SPI - security parameter index(default=0)\n");
82 fprintf(stderr, "MODE := [ transport | tunnel | ro | beet ](default=transport)\n");
83 //fprintf(stderr, "REQID - number(default=0)\n");
85 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
86 fprintf(stderr, "FLAG := [ noecn | decap-dscp | nopmtudisc | wildrecv | icmp | af-unspec ]\n");
88 fprintf(stderr, "ENCAP := ENCAP-TYPE SPORT DPORT OADDR\n");
89 fprintf(stderr, "ENCAP-TYPE := espinudp | espinudp-nonike\n");
91 fprintf(stderr, "ALGO-LIST := [ ALGO-LIST ] | [ ALGO ]\n");
92 fprintf(stderr, "ALGO := ALGO_TYPE ALGO_NAME ALGO_KEY "
93 "[ ALGO_ICV_LEN ]\n");
94 fprintf(stderr, "ALGO_TYPE := [ ");
95 fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_AEAD));
96 fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT));
97 fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_AUTH));
98 fprintf(stderr, "%s ", strxf_algotype(XFRMA_ALG_COMP));
99 fprintf(stderr, "]\n");
101 //fprintf(stderr, "ALGO_NAME - algorithm name\n");
102 //fprintf(stderr, "ALGO_KEY - algorithm key\n");
104 fprintf(stderr, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ UPSPEC ] [ dev DEV ]\n");
106 fprintf(stderr, "UPSPEC := proto PROTO [ [ sport PORT ] [ dport PORT ] |\n");
107 fprintf(stderr, " [ type NUMBER ] [ code NUMBER ] ]\n");
110 //fprintf(stderr, "DEV - device name(default=none)\n");
111 fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
112 fprintf(stderr, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
113 fprintf(stderr, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] COUNT ]\n");
117 static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type,
118 char *name, char *key, char *buf, int max)
121 int slen = strlen(key);
124 /* XXX: verifying both name and key is required! */
125 fprintf(stderr, "warning: ALGONAME/ALGOKEY will send to kernel promiscuously!(verifying them isn't implemented yet)\n");
128 strncpy(alg->alg_name, name, sizeof(alg->alg_name));
130 if (slen > 2 && strncmp(key, "0x", 2) == 0) {
131 /* split two chars "0x" from the top */
137 /* Converting hexadecimal numbered string into real key;
138 * Convert each two chars into one char(value). If number
139 * of the length is odd, add zero on the top for rounding.
142 /* calculate length of the converted values(real key) */
143 len = (plen + 1) / 2;
145 invarg("\"ALGOKEY\" makes buffer overflow\n", key);
147 for (i = - (plen % 2), j = 0; j < len; i += 2, j++) {
151 vbuf[0] = i >= 0 ? p[i] : '0';
155 if (get_u8(&val, vbuf, 16))
156 invarg("\"ALGOKEY\" is invalid", key);
164 invarg("\"ALGOKEY\" makes buffer overflow\n", key);
166 strncpy(buf, key, len);
170 alg->alg_key_len = len * 8;
175 static int xfrm_seq_parse(__u32 *seq, int *argcp, char ***argvp)
178 char **argv = *argvp;
180 if (get_u32(seq, *argv, 0))
181 invarg("\"SEQ\" is invalid", *argv);
191 static int xfrm_state_flag_parse(__u8 *flags, int *argcp, char ***argvp)
194 char **argv = *argvp;
195 int len = strlen(*argv);
197 if (len > 2 && strncmp(*argv, "0x", 2) == 0) {
200 if (get_u8(&val, *argv, 16))
201 invarg("\"FLAG\" is invalid", *argv);
205 if (strcmp(*argv, "noecn") == 0)
206 *flags |= XFRM_STATE_NOECN;
207 else if (strcmp(*argv, "decap-dscp") == 0)
208 *flags |= XFRM_STATE_DECAP_DSCP;
209 else if (strcmp(*argv, "nopmtudisc") == 0)
210 *flags |= XFRM_STATE_NOPMTUDISC;
211 else if (strcmp(*argv, "wildrecv") == 0)
212 *flags |= XFRM_STATE_WILDRECV;
213 else if (strcmp(*argv, "icmp") == 0)
214 *flags |= XFRM_STATE_ICMP;
215 else if (strcmp(*argv, "af-unspec") == 0)
216 *flags |= XFRM_STATE_AF_UNSPEC;
218 PREV_ARG(); /* back track */
234 static int xfrm_state_modify(int cmd, unsigned flags, int argc, char **argv)
236 struct rtnl_handle rth;
239 struct xfrm_usersa_info xsinfo;
240 char buf[RTA_BUF_SIZE];
242 struct xfrm_replay_state replay;
249 struct xfrm_mark mark = {0, 0};
251 memset(&req, 0, sizeof(req));
252 memset(&replay, 0, sizeof(replay));
254 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsinfo));
255 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
256 req.n.nlmsg_type = cmd;
257 req.xsinfo.family = preferred_family;
259 req.xsinfo.lft.soft_byte_limit = XFRM_INF;
260 req.xsinfo.lft.hard_byte_limit = XFRM_INF;
261 req.xsinfo.lft.soft_packet_limit = XFRM_INF;
262 req.xsinfo.lft.hard_packet_limit = XFRM_INF;
265 if (strcmp(*argv, "mode") == 0) {
267 xfrm_mode_parse(&req.xsinfo.mode, &argc, &argv);
268 } else if (strcmp(*argv, "mark") == 0) {
269 xfrm_parse_mark(&mark, &argc, &argv);
270 } else if (strcmp(*argv, "reqid") == 0) {
272 xfrm_reqid_parse(&req.xsinfo.reqid, &argc, &argv);
273 } else if (strcmp(*argv, "seq") == 0) {
275 xfrm_seq_parse(&req.xsinfo.seq, &argc, &argv);
276 } else if (strcmp(*argv, "replay-window") == 0) {
278 if (get_u8(&req.xsinfo.replay_window, *argv, 0))
279 invarg("\"replay-window\" value is invalid", *argv);
280 } else if (strcmp(*argv, "replay-seq") == 0) {
282 if (get_u32(&replay.seq, *argv, 0))
283 invarg("\"replay-seq\" value is invalid", *argv);
284 } else if (strcmp(*argv, "replay-oseq") == 0) {
286 if (get_u32(&replay.oseq, *argv, 0))
287 invarg("\"replay-oseq\" value is invalid", *argv);
288 } else if (strcmp(*argv, "flag") == 0) {
290 xfrm_state_flag_parse(&req.xsinfo.flags, &argc, &argv);
291 } else if (strcmp(*argv, "sel") == 0) {
293 xfrm_selector_parse(&req.xsinfo.sel, &argc, &argv);
294 } else if (strcmp(*argv, "limit") == 0) {
296 xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv);
297 } else if (strcmp(*argv, "encap") == 0) {
298 struct xfrm_encap_tmpl encap;
301 xfrm_encap_type_parse(&encap.encap_type, &argc, &argv);
303 if (get_u16(&encap.encap_sport, *argv, 0))
304 invarg("\"encap\" sport value is invalid", *argv);
305 encap.encap_sport = htons(encap.encap_sport);
307 if (get_u16(&encap.encap_dport, *argv, 0))
308 invarg("\"encap\" dport value is invalid", *argv);
309 encap.encap_dport = htons(encap.encap_dport);
311 get_addr(&oa, *argv, AF_UNSPEC);
312 memcpy(&encap.encap_oa, &oa.data, sizeof(encap.encap_oa));
313 addattr_l(&req.n, sizeof(req.buf), XFRMA_ENCAP,
314 (void *)&encap, sizeof(encap));
315 } else if (strcmp(*argv, "coa") == 0) {
320 duparg("coa", *argv);
325 get_prefix(&coa, *argv, preferred_family);
326 if (coa.family == AF_UNSPEC)
327 invarg("\"coa\" address family is AF_UNSPEC", *argv);
328 if (coa.bytelen > sizeof(xcoa))
329 invarg("\"coa\" address length is too large", *argv);
331 memset(&xcoa, 0, sizeof(xcoa));
332 memcpy(&xcoa, &coa.data, coa.bytelen);
334 addattr_l(&req.n, sizeof(req.buf), XFRMA_COADDR,
335 (void *)&xcoa, sizeof(xcoa));
337 /* try to assume ALGO */
338 int type = xfrm_algotype_getbyname(*argv);
341 case XFRMA_ALG_CRYPT:
348 struct xfrm_algo alg;
349 struct xfrm_algo_aead aead;
351 char buf[XFRM_ALGO_KEY_BUF_SIZE];
362 duparg("ALGOTYPE", *argv);
365 case XFRMA_ALG_CRYPT:
367 duparg("ALGOTYPE", *argv);
372 duparg("ALGOTYPE", *argv);
377 duparg("ALGOTYPE", *argv);
382 invarg("\"ALGOTYPE\" is invalid\n", *argv);
395 buf = alg.u.alg.alg_key;
396 len = sizeof(alg.u.alg);
398 if (type != XFRMA_ALG_AEAD)
402 missarg("ALGOICVLEN");
404 if (get_u32(&icvlen, *argv, 0))
405 invarg("\"aead\" ICV length is invalid",
407 alg.u.aead.alg_icv_len = icvlen;
409 buf = alg.u.aead.alg_key;
410 len = sizeof(alg.u.aead);
413 xfrm_algo_parse((void *)&alg, type, name, key,
414 buf, sizeof(alg.buf));
415 len += alg.u.alg.alg_key_len;
417 addattr_l(&req.n, sizeof(req.buf), type,
422 /* try to assume ID */
424 invarg("unknown", *argv);
428 xfrm_id_parse(&req.xsinfo.saddr, &req.xsinfo.id,
429 &req.xsinfo.family, 0, &argc, &argv);
430 if (preferred_family == AF_UNSPEC)
431 preferred_family = req.xsinfo.family;
437 if (replay.seq || replay.oseq)
438 addattr_l(&req.n, sizeof(req.buf), XFRMA_REPLAY_VAL,
439 (void *)&replay, sizeof(replay));
442 fprintf(stderr, "Not enough information: \"ID\" is required\n");
446 if (mark.m & mark.v) {
447 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
448 (void *)&mark, sizeof(mark));
450 fprintf(stderr, "XFRMA_MARK failed\n");
455 switch (req.xsinfo.mode) {
456 case XFRM_MODE_TRANSPORT:
457 case XFRM_MODE_TUNNEL:
458 if (!xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
459 fprintf(stderr, "\"mode\" is invalid with proto=%s\n",
460 strxf_xfrmproto(req.xsinfo.id.proto));
464 case XFRM_MODE_ROUTEOPTIMIZATION:
465 case XFRM_MODE_IN_TRIGGER:
466 if (!xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
467 fprintf(stderr, "\"mode\" is invalid with proto=%s\n",
468 strxf_xfrmproto(req.xsinfo.id.proto));
471 if (req.xsinfo.id.spi != 0) {
472 fprintf(stderr, "\"spi\" must be 0 with proto=%s\n",
473 strxf_xfrmproto(req.xsinfo.id.proto));
481 if (aeadop || ealgop || aalgop || calgop) {
482 if (!xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
483 fprintf(stderr, "\"ALGO\" is invalid with proto=%s\n",
484 strxf_xfrmproto(req.xsinfo.id.proto));
488 if (xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
489 fprintf(stderr, "\"ALGO\" is required with proto=%s\n",
490 strxf_xfrmproto(req.xsinfo.id.proto));
496 if (!xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
497 fprintf(stderr, "\"coa\" is invalid with proto=%s\n",
498 strxf_xfrmproto(req.xsinfo.id.proto));
502 if (xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
503 fprintf(stderr, "\"coa\" is required with proto=%s\n",
504 strxf_xfrmproto(req.xsinfo.id.proto));
509 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
512 if (req.xsinfo.family == AF_UNSPEC)
513 req.xsinfo.family = AF_INET;
515 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
523 static int xfrm_state_allocspi(int argc, char **argv)
525 struct rtnl_handle rth;
528 struct xfrm_userspi_info xspi;
529 char buf[RTA_BUF_SIZE];
534 struct xfrm_mark mark = {0, 0};
535 char res_buf[NLMSG_BUF_SIZE];
536 struct nlmsghdr *res_n = (struct nlmsghdr *)res_buf;
538 memset(res_buf, 0, sizeof(res_buf));
540 memset(&req, 0, sizeof(req));
542 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xspi));
543 req.n.nlmsg_flags = NLM_F_REQUEST;
544 req.n.nlmsg_type = XFRM_MSG_ALLOCSPI;
545 req.xspi.info.family = preferred_family;
548 req.xsinfo.lft.soft_byte_limit = XFRM_INF;
549 req.xsinfo.lft.hard_byte_limit = XFRM_INF;
550 req.xsinfo.lft.soft_packet_limit = XFRM_INF;
551 req.xsinfo.lft.hard_packet_limit = XFRM_INF;
555 if (strcmp(*argv, "mode") == 0) {
557 xfrm_mode_parse(&req.xspi.info.mode, &argc, &argv);
558 } else if (strcmp(*argv, "mark") == 0) {
559 xfrm_parse_mark(&mark, &argc, &argv);
560 } else if (strcmp(*argv, "reqid") == 0) {
562 xfrm_reqid_parse(&req.xspi.info.reqid, &argc, &argv);
563 } else if (strcmp(*argv, "seq") == 0) {
565 xfrm_seq_parse(&req.xspi.info.seq, &argc, &argv);
566 } else if (strcmp(*argv, "min") == 0) {
568 duparg("min", *argv);
573 if (get_u32(&req.xspi.min, *argv, 0))
574 invarg("\"min\" value is invalid", *argv);
575 } else if (strcmp(*argv, "max") == 0) {
577 duparg("max", *argv);
582 if (get_u32(&req.xspi.max, *argv, 0))
583 invarg("\"max\" value is invalid", *argv);
585 /* try to assume ID */
587 invarg("unknown", *argv);
591 xfrm_id_parse(&req.xspi.info.saddr, &req.xspi.info.id,
592 &req.xspi.info.family, 0, &argc, &argv);
593 if (req.xspi.info.id.spi) {
594 fprintf(stderr, "\"SPI\" must be zero\n");
597 if (preferred_family == AF_UNSPEC)
598 preferred_family = req.xspi.info.family;
604 fprintf(stderr, "Not enough information: \"ID\" is required\n");
610 fprintf(stderr, "\"max\" is missing\n");
613 if (req.xspi.min > req.xspi.max) {
614 fprintf(stderr, "\"min\" valie is larger than \"max\" one\n");
619 fprintf(stderr, "\"min\" is missing\n");
623 /* XXX: Default value defined in PF_KEY;
624 * See kernel's net/key/af_key.c(pfkey_getspi).
626 req.xspi.min = 0x100;
627 req.xspi.max = 0x0fffffff;
629 /* XXX: IPCOMP spi is 16-bits;
630 * See kernel's net/xfrm/xfrm_user(verify_userspi_info).
632 if (req.xspi.info.id.proto == IPPROTO_COMP)
633 req.xspi.max = 0xffff;
636 if (mark.m & mark.v) {
637 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
638 (void *)&mark, sizeof(mark));
640 fprintf(stderr, "XFRMA_MARK failed\n");
645 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
648 if (req.xspi.info.family == AF_UNSPEC)
649 req.xspi.info.family = AF_INET;
652 if (rtnl_talk(&rth, &req.n, 0, 0, res_n, NULL, NULL) < 0)
655 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
656 fprintf(stderr, "An error :-)\n");
665 static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo)
670 if (filter.id_src_mask)
671 if (xfrm_addr_match(&xsinfo->saddr, &filter.xsinfo.saddr,
674 if (filter.id_dst_mask)
675 if (xfrm_addr_match(&xsinfo->id.daddr, &filter.xsinfo.id.daddr,
678 if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask)
680 if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask)
682 if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask)
684 if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask)
686 if (filter.state_flags_mask)
687 if ((xsinfo->flags & filter.xsinfo.flags) == 0)
693 int xfrm_state_print(const struct sockaddr_nl *who, struct nlmsghdr *n,
696 FILE *fp = (FILE*)arg;
697 struct rtattr * tb[XFRMA_MAX+1];
699 struct xfrm_usersa_info *xsinfo = NULL;
700 struct xfrm_user_expire *xexp = NULL;
701 struct xfrm_usersa_id *xsid = NULL;
702 int len = n->nlmsg_len;
704 if (n->nlmsg_type != XFRM_MSG_NEWSA &&
705 n->nlmsg_type != XFRM_MSG_DELSA &&
706 n->nlmsg_type != XFRM_MSG_UPDSA &&
707 n->nlmsg_type != XFRM_MSG_EXPIRE) {
708 fprintf(stderr, "Not a state: %08x %08x %08x\n",
709 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
713 if (n->nlmsg_type == XFRM_MSG_DELSA) {
714 /* Dont blame me for this .. Herbert made me do it */
715 xsid = NLMSG_DATA(n);
716 len -= NLMSG_SPACE(sizeof(*xsid));
717 } else if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
718 xexp = NLMSG_DATA(n);
719 xsinfo = &xexp->state;
720 len -= NLMSG_SPACE(sizeof(*xexp));
723 xsinfo = NLMSG_DATA(n);
724 len -= NLMSG_SPACE(sizeof(*xsinfo));
728 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
732 if (xsinfo && !xfrm_state_filter_match(xsinfo))
735 if (n->nlmsg_type == XFRM_MSG_DELSA)
736 fprintf(fp, "Deleted ");
737 else if (n->nlmsg_type == XFRM_MSG_UPDSA)
738 fprintf(fp, "Updated ");
739 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
740 fprintf(fp, "Expired ");
742 if (n->nlmsg_type == XFRM_MSG_DELSA)
743 rta = XFRMSID_RTA(xsid);
744 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
745 rta = XFRMEXP_RTA(xexp);
747 rta = XFRMS_RTA(xsinfo);
749 parse_rtattr(tb, XFRMA_MAX, rta, len);
751 if (n->nlmsg_type == XFRM_MSG_DELSA) {
752 //xfrm_policy_id_print();
755 fprintf(stderr, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
758 if (RTA_PAYLOAD(tb[XFRMA_SA]) < sizeof(*xsinfo)) {
759 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
762 xsinfo = RTA_DATA(tb[XFRMA_SA]);
765 xfrm_state_info_print(xsinfo, tb, fp, NULL, NULL);
767 if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
769 fprintf(fp, "hard %u", xexp->hard);
770 fprintf(fp, "%s", _SL_);
780 static int xfrm_state_get_or_delete(int argc, char **argv, int delete)
782 struct rtnl_handle rth;
785 struct xfrm_usersa_id xsid;
786 char buf[RTA_BUF_SIZE];
790 struct xfrm_mark mark = {0, 0};
792 memset(&req, 0, sizeof(req));
794 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid));
795 req.n.nlmsg_flags = NLM_F_REQUEST;
796 req.n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA;
797 req.xsid.family = preferred_family;
800 xfrm_address_t saddr;
802 if (strcmp(*argv, "mark") == 0) {
803 xfrm_parse_mark(&mark, &argc, &argv);
806 invarg("unknown", *argv);
810 memset(&id, 0, sizeof(id));
811 memset(&saddr, 0, sizeof(saddr));
812 xfrm_id_parse(&saddr, &id, &req.xsid.family, 0,
815 memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr));
816 req.xsid.spi = id.spi;
817 req.xsid.proto = id.proto;
819 addattr_l(&req.n, sizeof(req.buf), XFRMA_SRCADDR,
820 (void *)&saddr, sizeof(saddr));
826 if (mark.m & mark.v) {
827 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
828 (void *)&mark, sizeof(mark));
830 fprintf(stderr, "XFRMA_MARK failed\n");
835 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
838 if (req.xsid.family == AF_UNSPEC)
839 req.xsid.family = AF_INET;
842 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
845 char buf[NLMSG_BUF_SIZE];
846 struct nlmsghdr *res_n = (struct nlmsghdr *)buf;
848 memset(buf, 0, sizeof(buf));
850 if (rtnl_talk(&rth, &req.n, 0, 0, res_n, NULL, NULL) < 0)
853 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
854 fprintf(stderr, "An error :-)\n");
865 * With an existing state of nlmsg, make new nlmsg for deleting the state
866 * and store it to buffer.
868 static int xfrm_state_keep(const struct sockaddr_nl *who,
872 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
873 struct rtnl_handle *rth = xb->rth;
874 struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n);
875 int len = n->nlmsg_len;
876 struct nlmsghdr *new_n;
877 struct xfrm_usersa_id *xsid;
879 if (n->nlmsg_type != XFRM_MSG_NEWSA) {
880 fprintf(stderr, "Not a state: %08x %08x %08x\n",
881 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
885 len -= NLMSG_LENGTH(sizeof(*xsinfo));
887 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
891 if (!xfrm_state_filter_match(xsinfo))
894 if (xb->offset > xb->size) {
895 fprintf(stderr, "State buffer overflow\n");
899 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
900 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid));
901 new_n->nlmsg_flags = NLM_F_REQUEST;
902 new_n->nlmsg_type = XFRM_MSG_DELSA;
903 new_n->nlmsg_seq = ++rth->seq;
905 xsid = NLMSG_DATA(new_n);
906 xsid->family = xsinfo->family;
907 memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr));
908 xsid->spi = xsinfo->id.spi;
909 xsid->proto = xsinfo->id.proto;
911 addattr_l(new_n, xb->size, XFRMA_SRCADDR, &xsinfo->saddr,
912 sizeof(xsid->daddr));
914 xb->offset += new_n->nlmsg_len;
920 static int xfrm_state_list_or_deleteall(int argc, char **argv, int deleteall)
923 struct rtnl_handle rth;
927 filter.xsinfo.family = preferred_family;
930 if (strcmp(*argv, "mode") == 0) {
932 xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv);
934 filter.mode_mask = XFRM_FILTER_MASK_FULL;
936 } else if (strcmp(*argv, "reqid") == 0) {
938 xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv);
940 filter.reqid_mask = XFRM_FILTER_MASK_FULL;
942 } else if (strcmp(*argv, "flag") == 0) {
944 xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv);
946 filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
950 invarg("unknown", *argv);
954 xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id,
955 &filter.xsinfo.family, 1, &argc, &argv);
956 if (preferred_family == AF_UNSPEC)
957 preferred_family = filter.xsinfo.family;
962 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
966 struct xfrm_buffer xb;
967 char buf[NLMSG_DELETEALL_BUF_SIZE];
971 xb.size = sizeof(buf);
979 fprintf(stderr, "Delete-all round = %d\n", i);
981 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
982 perror("Cannot send dump request");
986 if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb, NULL, NULL) < 0) {
987 fprintf(stderr, "Delete-all terminated\n");
990 if (xb.nlmsg_count == 0) {
992 fprintf(stderr, "Delete-all completed\n");
996 if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) {
997 perror("Failed to send delete-all request\n");
1001 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
1008 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
1009 perror("Cannot send dump request");
1013 if (rtnl_dump_filter(&rth, xfrm_state_print, stdout, NULL, NULL) < 0) {
1014 fprintf(stderr, "Dump terminated\n");
1024 int print_sadinfo(struct nlmsghdr *n, void *arg)
1026 FILE *fp = (FILE*)arg;
1027 __u32 *f = NLMSG_DATA(n);
1028 struct rtattr *tb[XFRMA_SAD_MAX+1];
1032 int len = n->nlmsg_len;
1034 len -= NLMSG_LENGTH(sizeof(__u32));
1036 fprintf(stderr, "SADinfo: Wrong len %d\n", len);
1040 rta = XFRMSAPD_RTA(f);
1041 parse_rtattr(tb, XFRMA_SAD_MAX, rta, len);
1043 if (tb[XFRMA_SAD_CNT]) {
1044 fprintf(fp,"\t SAD");
1045 cnt = (__u32 *)RTA_DATA(tb[XFRMA_SAD_CNT]);
1046 fprintf(fp," count %d", *cnt);
1048 fprintf(fp,"BAD SAD info returned\n");
1053 if (tb[XFRMA_SAD_HINFO]) {
1054 struct xfrmu_sadhinfo *si;
1056 if (RTA_PAYLOAD(tb[XFRMA_SAD_HINFO]) < sizeof(*si)) {
1057 fprintf(fp,"BAD SAD length returned\n");
1061 si = RTA_DATA(tb[XFRMA_SAD_HINFO]);
1062 fprintf(fp," (buckets ");
1063 fprintf(fp,"count %d", si->sadhcnt);
1064 fprintf(fp," Max %d", si->sadhmcnt);
1073 static int xfrm_sad_getinfo(int argc, char **argv)
1075 struct rtnl_handle rth;
1082 memset(&req, 0, sizeof(req));
1083 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.flags));
1084 req.n.nlmsg_flags = NLM_F_REQUEST;
1085 req.n.nlmsg_type = XFRM_MSG_GETSADINFO;
1086 req.flags = 0XFFFFFFFF;
1088 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1091 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0)
1094 print_sadinfo(&req.n, (void*)stdout);
1101 static int xfrm_state_flush(int argc, char **argv)
1103 struct rtnl_handle rth;
1106 struct xfrm_usersa_flush xsf;
1108 char *protop = NULL;
1110 memset(&req, 0, sizeof(req));
1112 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsf));
1113 req.n.nlmsg_flags = NLM_F_REQUEST;
1114 req.n.nlmsg_type = XFRM_MSG_FLUSHSA;
1118 if (strcmp(*argv, "proto") == 0) {
1122 duparg("proto", *argv);
1127 ret = xfrm_xfrmproto_getbyname(*argv);
1129 invarg("\"XFRM_PROTO\" is invalid", *argv);
1131 req.xsf.proto = (__u8)ret;
1133 invarg("unknown", *argv);
1138 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1142 fprintf(stderr, "Flush state proto=%s\n",
1143 strxf_xfrmproto(req.xsf.proto));
1145 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
1153 int do_xfrm_state(int argc, char **argv)
1156 return xfrm_state_list_or_deleteall(0, NULL, 0);
1158 if (matches(*argv, "add") == 0)
1159 return xfrm_state_modify(XFRM_MSG_NEWSA, 0,
1161 if (matches(*argv, "update") == 0)
1162 return xfrm_state_modify(XFRM_MSG_UPDSA, 0,
1164 if (matches(*argv, "allocspi") == 0)
1165 return xfrm_state_allocspi(argc-1, argv+1);
1166 if (matches(*argv, "delete") == 0)
1167 return xfrm_state_get_or_delete(argc-1, argv+1, 1);
1168 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
1169 return xfrm_state_list_or_deleteall(argc-1, argv+1, 1);
1170 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1171 || matches(*argv, "lst") == 0)
1172 return xfrm_state_list_or_deleteall(argc-1, argv+1, 0);
1173 if (matches(*argv, "get") == 0)
1174 return xfrm_state_get_or_delete(argc-1, argv+1, 0);
1175 if (matches(*argv, "flush") == 0)
1176 return xfrm_state_flush(argc-1, argv+1);
1177 if (matches(*argv, "count") == 0) {
1178 return xfrm_sad_getinfo(argc, argv);
1180 if (matches(*argv, "help") == 0)
1182 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv);