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
21 * based on ip.c, iproute.c
25 * Masahide NAKAMURA @USAGI
31 #include <sys/types.h>
32 #include <sys/socket.h>
35 #include <linux/netlink.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/xfrm.h>
42 #define STRBUF_SIZE (128)
43 #define STRBUF_CAT(buf, str) \
45 int rest = sizeof(buf) - 1 - strlen(buf); \
47 int len = strlen(str); \
50 strncat(buf, str, len); \
51 buf[sizeof(buf) - 1] = '\0'; \
55 struct xfrm_filter filter;
57 static void usage(void) __attribute__((noreturn));
59 static void usage(void)
62 "Usage: ip xfrm XFRM-OBJECT { COMMAND | help }\n"
63 "where XFRM-OBJECT := state | policy | monitor\n");
67 /* This is based on utils.c(inet_addr_match) */
68 int xfrm_addr_match(xfrm_address_t *x1, xfrm_address_t *x2, int bits)
70 __u32 *a1 = (__u32 *)x1;
71 __u32 *a2 = (__u32 *)x2;
72 int words = bits >> 0x05;
77 if (memcmp(a1, a2, words << 2))
87 mask = htonl((0xffffffff) << (0x20 - bits));
96 int xfrm_xfrmproto_is_ipsec(__u8 proto)
98 return (proto == IPPROTO_ESP ||
99 proto == IPPROTO_AH ||
100 proto == IPPROTO_COMP);
103 int xfrm_xfrmproto_is_ro(__u8 proto)
105 return (proto == IPPROTO_ROUTING ||
106 proto == IPPROTO_DSTOPTS);
114 static const struct typeent xfrmproto_types[]= {
115 { "esp", IPPROTO_ESP }, { "ah", IPPROTO_AH }, { "comp", IPPROTO_COMP },
116 { "route2", IPPROTO_ROUTING }, { "hao", IPPROTO_DSTOPTS },
117 { "ipsec-any", IPSEC_PROTO_ANY },
121 int xfrm_xfrmproto_getbyname(char *name)
126 const struct typeent *t = &xfrmproto_types[i];
127 if (!t->t_name || t->t_type == -1)
130 if (strcmp(t->t_name, name) == 0)
137 const char *strxf_xfrmproto(__u8 proto)
143 const struct typeent *t = &xfrmproto_types[i];
144 if (!t->t_name || t->t_type == -1)
147 if (t->t_type == proto)
151 sprintf(str, "%u", proto);
155 static const struct typeent algo_types[]= {
156 { "enc", XFRMA_ALG_CRYPT }, { "auth", XFRMA_ALG_AUTH },
157 { "comp", XFRMA_ALG_COMP }, { "aead", XFRMA_ALG_AEAD },
158 { "auth-trunc", XFRMA_ALG_AUTH_TRUNC },
162 int xfrm_algotype_getbyname(char *name)
167 const struct typeent *t = &algo_types[i];
168 if (!t->t_name || t->t_type == -1)
171 if (strcmp(t->t_name, name) == 0)
178 const char *strxf_algotype(int type)
184 const struct typeent *t = &algo_types[i];
185 if (!t->t_name || t->t_type == -1)
188 if (t->t_type == type)
192 sprintf(str, "%d", type);
196 const char *strxf_mask8(__u8 mask)
199 const int sn = sizeof(mask) * 8 - 1;
203 for (b = (1 << sn); b > 0; b >>= 1)
204 str[i++] = ((b & mask) ? '1' : '0');
210 const char *strxf_mask32(__u32 mask)
214 sprintf(str, "%.8x", mask);
219 const char *strxf_share(__u8 share)
227 case XFRM_SHARE_SESSION:
228 strcpy(str, "session");
230 case XFRM_SHARE_USER:
233 case XFRM_SHARE_UNIQUE:
234 strcpy(str, "unique");
237 sprintf(str, "%u", share);
244 const char *strxf_proto(__u8 proto)
250 pp = getprotobynumber(proto);
254 sprintf(buf, "%u", proto);
261 const char *strxf_ptype(__u8 ptype)
266 case XFRM_POLICY_TYPE_MAIN:
269 case XFRM_POLICY_TYPE_SUB:
273 sprintf(str, "%u", ptype);
280 void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id,
281 __u8 mode, __u32 reqid, __u16 family, int force_spi,
282 FILE *fp, const char *prefix, const char *title)
289 memset(abuf, '\0', sizeof(abuf));
290 fprintf(fp, "src %s ", rt_addr_n2a(family, sizeof(*saddr),
291 saddr, abuf, sizeof(abuf)));
292 memset(abuf, '\0', sizeof(abuf));
293 fprintf(fp, "dst %s", rt_addr_n2a(family, sizeof(id->daddr),
294 &id->daddr, abuf, sizeof(abuf)));
295 fprintf(fp, "%s", _SL_);
301 fprintf(fp, "proto %s ", strxf_xfrmproto(id->proto));
303 if (show_stats > 0 || force_spi || id->spi) {
304 __u32 spi = ntohl(id->spi);
305 fprintf(fp, "spi 0x%08x", spi);
307 fprintf(fp, "(%u)", spi);
311 fprintf(fp, "reqid %u", reqid);
313 fprintf(fp, "(0x%08x)", reqid);
316 fprintf(fp, "mode ");
318 case XFRM_MODE_TRANSPORT:
319 fprintf(fp, "transport");
321 case XFRM_MODE_TUNNEL:
322 fprintf(fp, "tunnel");
324 case XFRM_MODE_ROUTEOPTIMIZATION:
327 case XFRM_MODE_IN_TRIGGER:
328 fprintf(fp, "in_trigger");
334 fprintf(fp, "%u", mode);
337 fprintf(fp, "%s", _SL_);
340 static const char *strxf_limit(__u64 limit)
343 if (limit == XFRM_INF)
344 strcpy(str, "(INF)");
346 sprintf(str, "%llu", (unsigned long long) limit);
351 void xfrm_stats_print(struct xfrm_stats *s, FILE *fp, const char *prefix)
355 fprintf(fp, "stats:%s", _SL_);
359 fprintf(fp, " replay-window %u replay %u failed %u%s",
360 s->replay_window, s->replay, s->integrity_failed, _SL_);
363 static const char *strxf_time(__u64 time)
373 /* XXX: treat time in the same manner of kernel's
374 * net/xfrm/xfrm_{user,state}.c
379 strftime(str, sizeof(str), "%Y-%m-%d %T", tp);
385 void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg,
386 struct xfrm_lifetime_cur *cur,
387 FILE *fp, const char *prefix)
392 fprintf(fp, "lifetime config:%s",_SL_);
396 fprintf(fp, " limit: soft %s(bytes),",
397 strxf_limit(cfg->soft_byte_limit));
398 fprintf(fp, " hard %s(bytes)%s",
399 strxf_limit(cfg->hard_byte_limit), _SL_);
403 fprintf(fp, " limit: soft %s(packets),",
404 strxf_limit(cfg->soft_packet_limit));
405 fprintf(fp, " hard %s(packets)%s",
406 strxf_limit(cfg->hard_packet_limit), _SL_);
410 fprintf(fp, " expire add: soft %llu(sec), hard %llu(sec)%s",
411 (unsigned long long) cfg->soft_add_expires_seconds,
412 (unsigned long long) cfg->hard_add_expires_seconds,
417 fprintf(fp, " expire use: soft %llu(sec), hard %llu(sec)%s",
418 (unsigned long long) cfg->soft_use_expires_seconds,
419 (unsigned long long) cfg->hard_use_expires_seconds,
425 fprintf(fp, "lifetime current:%s", _SL_);
429 fprintf(fp, " %llu(bytes), %llu(packets)%s",
430 (unsigned long long) cur->bytes,
431 (unsigned long long) cur->packets,
436 fprintf(fp, " add %s ", strxf_time(cur->add_time));
437 fprintf(fp, "use %s%s", strxf_time(cur->use_time), _SL_);
441 void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
442 FILE *fp, const char *prefix)
451 f = preferred_family;
456 memset(abuf, '\0', sizeof(abuf));
457 fprintf(fp, "src %s/%u ", rt_addr_n2a(f, sizeof(sel->saddr),
458 &sel->saddr, abuf, sizeof(abuf)),
461 memset(abuf, '\0', sizeof(abuf));
462 fprintf(fp, "dst %s/%u ", rt_addr_n2a(f, sizeof(sel->daddr),
463 &sel->daddr, abuf, sizeof(abuf)),
467 fprintf(fp, "proto %s ", strxf_proto(sel->proto));
468 switch (sel->proto) {
475 fprintf(fp, "sport %u ", ntohs(sel->sport));
477 fprintf(fp, "dport %u ", ntohs(sel->dport));
481 /* type/code is stored at sport/dport in selector */
483 fprintf(fp, "type %u ", ntohs(sel->sport));
485 fprintf(fp, "code %u ", ntohs(sel->dport));
488 if (sel->sport_mask || sel->dport_mask)
489 fprintf(fp, "key %u ",
490 (((__u32)ntohs(sel->sport)) << 16) +
495 fprintf(fp, "type %u ", ntohs(sel->sport));
496 if (sel->dport_mask) {
498 fprintf(fp, "(dport) 0x%.4x ", sel->dport);
503 if (sel->ifindex > 0)
504 fprintf(fp, "dev %s ", ll_index_to_name(sel->ifindex));
507 fprintf(fp, "uid %u", sel->user);
509 fprintf(fp, "%s", _SL_);
512 static void __xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
513 FILE *fp, const char *prefix, int newline)
521 fprintf(fp, "%s ", strxf_algotype(type));
523 if (len < sizeof(*algo)) {
524 fprintf(fp, "(ERROR truncated)");
527 len -= sizeof(*algo);
529 fprintf(fp, "%s ", algo->alg_name);
531 keylen = algo->alg_key_len / 8;
533 fprintf(fp, "(ERROR truncated)");
538 for (i = 0; i < keylen; i ++)
539 fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]);
542 fprintf(fp, " (%d bits)", algo->alg_key_len);
546 fprintf(fp, "%s", _SL_);
549 static inline void xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
550 FILE *fp, const char *prefix)
552 return __xfrm_algo_print(algo, type, len, fp, prefix, 1);
555 static void xfrm_aead_print(struct xfrm_algo_aead *algo, int len,
556 FILE *fp, const char *prefix)
559 struct xfrm_algo algo;
560 char key[algo->alg_key_len / 8];
563 memcpy(base.algo.alg_name, algo->alg_name, sizeof(base.algo.alg_name));
564 base.algo.alg_key_len = algo->alg_key_len;
565 memcpy(base.algo.alg_key, algo->alg_key, algo->alg_key_len / 8);
567 __xfrm_algo_print(&base.algo, XFRMA_ALG_AEAD, len, fp, prefix, 0);
569 fprintf(fp, " %d", algo->alg_icv_len);
571 fprintf(fp, "%s", _SL_);
574 static void xfrm_auth_trunc_print(struct xfrm_algo_auth *algo, int len,
575 FILE *fp, const char *prefix)
578 struct xfrm_algo algo;
579 char key[algo->alg_key_len / 8];
582 memcpy(base.algo.alg_name, algo->alg_name, sizeof(base.algo.alg_name));
583 base.algo.alg_key_len = algo->alg_key_len;
584 memcpy(base.algo.alg_key, algo->alg_key, algo->alg_key_len / 8);
586 __xfrm_algo_print(&base.algo, XFRMA_ALG_AUTH_TRUNC, len, fp, prefix, 0);
588 fprintf(fp, " %d", algo->alg_trunc_len);
590 fprintf(fp, "%s", _SL_);
593 static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int len,
594 __u16 family, FILE *fp, const char *prefix)
596 int ntmpls = len / sizeof(struct xfrm_user_tmpl);
602 fprintf(fp, "(ERROR \"tmpl\" truncated)");
603 fprintf(fp, "%s", _SL_);
607 for (i = 0; i < ntmpls; i++) {
608 struct xfrm_user_tmpl *tmpl = &tmpls[i];
613 xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode,
614 tmpl->reqid, tmpl->family, 0, fp, prefix, "tmpl ");
616 if (show_stats > 0 || tmpl->optional) {
620 switch (tmpl->optional) {
623 fprintf(fp, "level required ");
626 fprintf(fp, "level use ");
629 fprintf(fp, "level %u ", tmpl->optional);
634 fprintf(fp, "share %s ", strxf_share(tmpl->share));
636 fprintf(fp, "%s", _SL_);
639 if (show_stats > 0) {
643 fprintf(fp, "%s-mask %s ",
644 strxf_algotype(XFRMA_ALG_CRYPT),
645 strxf_mask32(tmpl->ealgos));
646 fprintf(fp, "%s-mask %s ",
647 strxf_algotype(XFRMA_ALG_AUTH),
648 strxf_mask32(tmpl->aalgos));
649 fprintf(fp, "%s-mask %s",
650 strxf_algotype(XFRMA_ALG_COMP),
651 strxf_mask32(tmpl->calgos));
653 fprintf(fp, "%s", _SL_);
658 int xfrm_parse_mark(struct xfrm_mark *mark, int *argcp, char ***argvp)
661 char **argv = *argvp;
664 if (get_u32(&mark->v, *argv, 0)) {
665 invarg("Illegal \"mark\" value\n", *argv);
669 else { /* last entry on parse line */
670 mark->m = 0xffffffff;
674 if (strcmp(*argv, "mask") == 0) {
676 if (get_u32(&mark->m, *argv, 0)) {
677 invarg("Illegal \"mark\" mask\n", *argv);
680 mark->m = 0xffffffff;
691 void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
692 FILE *fp, const char *prefix)
694 if (tb[XFRMA_MARK]) {
695 struct rtattr *rta = tb[XFRMA_MARK];
696 struct xfrm_mark *m = (struct xfrm_mark *) RTA_DATA(rta);
697 fprintf(fp, "\tmark %d/0x%x\n", m->v, m->m);
700 if (tb[XFRMA_ALG_AUTH] && !tb[XFRMA_ALG_AUTH_TRUNC]) {
701 struct rtattr *rta = tb[XFRMA_ALG_AUTH];
702 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
703 XFRMA_ALG_AUTH, RTA_PAYLOAD(rta), fp, prefix);
706 if (tb[XFRMA_ALG_AUTH_TRUNC]) {
707 struct rtattr *rta = tb[XFRMA_ALG_AUTH_TRUNC];
708 xfrm_auth_trunc_print((struct xfrm_algo_auth *) RTA_DATA(rta),
709 RTA_PAYLOAD(rta), fp, prefix);
712 if (tb[XFRMA_ALG_AEAD]) {
713 struct rtattr *rta = tb[XFRMA_ALG_AEAD];
714 xfrm_aead_print((struct xfrm_algo_aead *)RTA_DATA(rta),
715 RTA_PAYLOAD(rta), fp, prefix);
718 if (tb[XFRMA_ALG_CRYPT]) {
719 struct rtattr *rta = tb[XFRMA_ALG_CRYPT];
720 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
721 XFRMA_ALG_CRYPT, RTA_PAYLOAD(rta), fp, prefix);
724 if (tb[XFRMA_ALG_COMP]) {
725 struct rtattr *rta = tb[XFRMA_ALG_COMP];
726 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
727 XFRMA_ALG_COMP, RTA_PAYLOAD(rta), fp, prefix);
730 if (tb[XFRMA_ENCAP]) {
731 struct xfrm_encap_tmpl *e;
736 fprintf(fp, "encap ");
738 if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) {
739 fprintf(fp, "(ERROR truncated)");
740 fprintf(fp, "%s", _SL_);
743 e = (struct xfrm_encap_tmpl *) RTA_DATA(tb[XFRMA_ENCAP]);
745 fprintf(fp, "type ");
746 switch (e->encap_type) {
748 fprintf(fp, "espinudp-nonike ");
751 fprintf(fp, "espinudp ");
754 fprintf(fp, "%u ", e->encap_type);
757 fprintf(fp, "sport %u ", ntohs(e->encap_sport));
758 fprintf(fp, "dport %u ", ntohs(e->encap_dport));
760 memset(abuf, '\0', sizeof(abuf));
761 fprintf(fp, "addr %s",
762 rt_addr_n2a(family, sizeof(e->encap_oa),
763 &e->encap_oa, abuf, sizeof(abuf)));
764 fprintf(fp, "%s", _SL_);
767 if (tb[XFRMA_TMPL]) {
768 struct rtattr *rta = tb[XFRMA_TMPL];
769 xfrm_tmpl_print((struct xfrm_user_tmpl *) RTA_DATA(rta),
770 RTA_PAYLOAD(rta), family, fp, prefix);
773 if (tb[XFRMA_COADDR]) {
781 coa = (xfrm_address_t *)RTA_DATA(tb[XFRMA_COADDR]);
783 if (RTA_PAYLOAD(tb[XFRMA_COADDR]) < sizeof(*coa)) {
784 fprintf(fp, "(ERROR truncated)");
785 fprintf(fp, "%s", _SL_);
789 memset(abuf, '\0', sizeof(abuf));
791 rt_addr_n2a(family, sizeof(*coa), coa,
792 abuf, sizeof(abuf)));
793 fprintf(fp, "%s", _SL_);
796 if (tb[XFRMA_LASTUSED]) {
801 fprintf(fp, "lastused ");
803 if (RTA_PAYLOAD(tb[XFRMA_LASTUSED]) < sizeof(lastused)) {
804 fprintf(fp, "(ERROR truncated)");
805 fprintf(fp, "%s", _SL_);
809 lastused = rta_getattr_u64(tb[XFRMA_LASTUSED]);
811 fprintf(fp, "%s", strxf_time(lastused));
812 fprintf(fp, "%s", _SL_);
817 static int xfrm_selector_iszero(struct xfrm_selector *s)
819 struct xfrm_selector s0;
821 memset(&s0, 0, sizeof(s0));
823 return (memcmp(&s0, s, sizeof(s0)) == 0);
826 void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo,
827 struct rtattr *tb[], FILE *fp, const char *prefix,
830 char buf[STRBUF_SIZE];
831 int force_spi = xfrm_xfrmproto_is_ipsec(xsinfo->id.proto);
833 memset(buf, '\0', sizeof(buf));
835 xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode,
836 xsinfo->reqid, xsinfo->family, force_spi, fp,
840 STRBUF_CAT(buf, prefix);
841 STRBUF_CAT(buf, "\t");
844 fprintf(fp, "replay-window %u ", xsinfo->replay_window);
846 fprintf(fp, "seq 0x%08u ", xsinfo->seq);
847 if (show_stats > 0 || xsinfo->flags) {
848 __u8 flags = xsinfo->flags;
850 fprintf(fp, "flag ");
851 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOECN, "noecn");
852 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp");
853 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOPMTUDISC, "nopmtudisc");
854 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_WILDRECV, "wildrecv");
855 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ICMP, "icmp");
856 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_AF_UNSPEC, "af-unspec");
857 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ALIGN4, "align4");
859 fprintf(fp, "%x", flags);
862 fprintf(fp, " (0x%s)", strxf_mask8(xsinfo->flags));
863 fprintf(fp, "%s", _SL_);
865 xfrm_xfrma_print(tb, xsinfo->family, fp, buf);
867 if (!xfrm_selector_iszero(&xsinfo->sel)) {
868 char sbuf[STRBUF_SIZE];
870 memcpy(sbuf, buf, sizeof(sbuf));
871 STRBUF_CAT(sbuf, "sel ");
873 xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, sbuf);
876 if (show_stats > 0) {
877 xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, buf);
878 xfrm_stats_print(&xsinfo->stats, fp, buf);
881 if (tb[XFRMA_SEC_CTX]) {
882 struct xfrm_user_sec_ctx *sctx;
884 fprintf(fp, "\tsecurity context ");
886 if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
887 fprintf(fp, "(ERROR truncated)");
889 sctx = (struct xfrm_user_sec_ctx *)RTA_DATA(tb[XFRMA_SEC_CTX]);
891 fprintf(fp, "%s %s", (char *)(sctx + 1), _SL_);
896 void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
897 struct rtattr *tb[], FILE *fp, const char *prefix,
900 char buf[STRBUF_SIZE];
902 memset(buf, '\0', sizeof(buf));
904 xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title);
906 if (tb[XFRMA_SEC_CTX]) {
907 struct xfrm_user_sec_ctx *sctx;
909 fprintf(fp, "\tsecurity context ");
911 if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
912 fprintf(fp, "(ERROR truncated)");
914 sctx = (struct xfrm_user_sec_ctx *)RTA_DATA(tb[XFRMA_SEC_CTX]);
916 fprintf(fp, "%s ", (char *)(sctx + 1));
917 fprintf(fp, "%s", _SL_);
921 STRBUF_CAT(buf, prefix);
922 STRBUF_CAT(buf, "\t");
925 if (xpinfo->dir >= XFRM_POLICY_MAX) {
926 xpinfo->dir -= XFRM_POLICY_MAX;
927 fprintf(fp, "socket ");
931 switch (xpinfo->dir) {
935 case XFRM_POLICY_OUT:
938 case XFRM_POLICY_FWD:
942 fprintf(fp, "%u", xpinfo->dir);
947 switch (xpinfo->action) {
948 case XFRM_POLICY_ALLOW:
950 fprintf(fp, "action allow ");
952 case XFRM_POLICY_BLOCK:
953 fprintf(fp, "action block ");
956 fprintf(fp, "action %u ", xpinfo->action);
961 fprintf(fp, "index %u ", xpinfo->index);
962 fprintf(fp, "priority %u ", xpinfo->priority);
964 if (tb[XFRMA_POLICY_TYPE]) {
965 struct xfrm_userpolicy_type *upt;
967 fprintf(fp, "ptype ");
969 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
970 fprintf(fp, "(ERROR truncated)");
972 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
973 fprintf(fp, "%s ", strxf_ptype(upt->type));
977 fprintf(fp, "share %s ", strxf_share(xpinfo->share));
979 if (show_stats > 0 || xpinfo->flags) {
980 __u8 flags = xpinfo->flags;
982 fprintf(fp, "flag ");
983 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_LOCALOK, "localok");
984 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_ICMP, "icmp");
986 fprintf(fp, "%x", flags);
989 fprintf(fp, " (0x%s)", strxf_mask8(xpinfo->flags));
990 fprintf(fp, "%s", _SL_);
993 xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf);
995 xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf);
998 int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
999 int loose, int *argcp, char ***argvp)
1002 char **argv = *argvp;
1006 memset(&dst, 0, sizeof(dst));
1007 memset(&src, 0, sizeof(src));
1010 if (strcmp(*argv, "src") == 0) {
1013 get_prefix(&src, *argv, preferred_family);
1014 if (src.family == AF_UNSPEC)
1015 invarg("\"src\" address family is AF_UNSPEC", *argv);
1017 *family = src.family;
1019 memcpy(saddr, &src.data, sizeof(*saddr));
1021 filter.id_src_mask = src.bitlen;
1023 } else if (strcmp(*argv, "dst") == 0) {
1026 get_prefix(&dst, *argv, preferred_family);
1027 if (dst.family == AF_UNSPEC)
1028 invarg("\"dst\" address family is AF_UNSPEC", *argv);
1030 *family = dst.family;
1032 memcpy(&id->daddr, &dst.data, sizeof(id->daddr));
1034 filter.id_dst_mask = dst.bitlen;
1036 } else if (strcmp(*argv, "proto") == 0) {
1041 ret = xfrm_xfrmproto_getbyname(*argv);
1043 invarg("\"XFRM-PROTO\" is invalid", *argv);
1045 id->proto = (__u8)ret;
1047 filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
1049 } else if (strcmp(*argv, "spi") == 0) {
1053 if (get_u32(&spi, *argv, 0))
1054 invarg("\"SPI\" is invalid", *argv);
1059 filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
1062 PREV_ARG(); /* back track */
1071 if (src.family && dst.family && (src.family != dst.family))
1072 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
1074 if (loose == 0 && id->proto == 0)
1075 missarg("XFRM-PROTO");
1085 int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp)
1088 char **argv = *argvp;
1090 if (matches(*argv, "transport") == 0)
1091 *mode = XFRM_MODE_TRANSPORT;
1092 else if (matches(*argv, "tunnel") == 0)
1093 *mode = XFRM_MODE_TUNNEL;
1094 else if (matches(*argv, "ro") == 0)
1095 *mode = XFRM_MODE_ROUTEOPTIMIZATION;
1096 else if (matches(*argv, "in_trigger") == 0)
1097 *mode = XFRM_MODE_IN_TRIGGER;
1098 else if (matches(*argv, "beet") == 0)
1099 *mode = XFRM_MODE_BEET;
1101 invarg("\"MODE\" is invalid", *argv);
1109 int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp)
1112 char **argv = *argvp;
1114 if (strcmp(*argv, "espinudp-nonike") == 0)
1116 else if (strcmp(*argv, "espinudp") == 0)
1119 invarg("\"ENCAP-TYPE\" is invalid", *argv);
1127 /* NOTE: reqid is used by host-byte order */
1128 int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp)
1131 char **argv = *argvp;
1133 if (get_u32(reqid, *argv, 0))
1134 invarg("\"REQID\" is invalid", *argv);
1142 static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
1143 int *argcp, char ***argvp)
1146 char **argv = *argvp;
1147 char *sportp = NULL;
1148 char *dportp = NULL;
1151 char *grekey = NULL;
1154 if (strcmp(*argv, "proto") == 0) {
1159 if (strcmp(*argv, "any") == 0)
1162 struct protoent *pp;
1163 pp = getprotobyname(*argv);
1165 upspec = pp->p_proto;
1167 if (get_u8(&upspec, *argv, 0))
1168 invarg("\"PROTO\" is invalid", *argv);
1171 sel->proto = upspec;
1173 filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL;
1175 } else if (strcmp(*argv, "sport") == 0) {
1180 if (get_u16(&sel->sport, *argv, 0))
1181 invarg("\"PORT\" is invalid", *argv);
1182 sel->sport = htons(sel->sport);
1184 sel->sport_mask = ~((__u16)0);
1186 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1188 } else if (strcmp(*argv, "dport") == 0) {
1193 if (get_u16(&sel->dport, *argv, 0))
1194 invarg("\"PORT\" is invalid", *argv);
1195 sel->dport = htons(sel->dport);
1197 sel->dport_mask = ~((__u16)0);
1199 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1201 } else if (strcmp(*argv, "type") == 0) {
1206 if (get_u16(&sel->sport, *argv, 0) ||
1207 (sel->sport & ~((__u16)0xff)))
1208 invarg("\"type\" value is invalid", *argv);
1209 sel->sport = htons(sel->sport);
1210 sel->sport_mask = ~((__u16)0);
1212 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1215 } else if (strcmp(*argv, "code") == 0) {
1220 if (get_u16(&sel->dport, *argv, 0) ||
1221 (sel->dport & ~((__u16)0xff)))
1222 invarg("\"code\" value is invalid", *argv);
1223 sel->dport = htons(sel->dport);
1224 sel->dport_mask = ~((__u16)0);
1226 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1228 } else if (strcmp(*argv, "key") == 0) {
1235 if (strchr(*argv, '.'))
1236 uval = htonl(get_addr32(*argv));
1238 if (get_unsigned(&uval, *argv, 0)<0) {
1239 fprintf(stderr, "invalid value of \"key\"\n");
1244 sel->sport = htons(uval >> 16);
1245 sel->dport = htons(uval & 0xffff);
1246 sel->sport_mask = ~((__u16)0);
1247 sel->dport_mask = ~((__u16)0);
1249 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1252 PREV_ARG(); /* back track */
1262 if (sportp || dportp) {
1263 switch (sel->proto) {
1270 fprintf(stderr, "\"sport\" and \"dport\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1274 if (typep || codep) {
1275 switch (sel->proto) {
1277 case IPPROTO_ICMPV6:
1281 fprintf(stderr, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1286 switch (sel->proto) {
1290 fprintf(stderr, "\"key\" is invalid with proto=%s\n", strxf_proto(sel->proto));
1301 int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
1304 char **argv = *argvp;
1307 char *upspecp = NULL;
1309 memset(&dst, 0, sizeof(dst));
1310 memset(&src, 0, sizeof(src));
1313 if (strcmp(*argv, "src") == 0) {
1316 get_prefix(&src, *argv, preferred_family);
1317 if (src.family == AF_UNSPEC)
1318 invarg("\"src\" address family is AF_UNSPEC", *argv);
1319 sel->family = src.family;
1321 memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
1322 sel->prefixlen_s = src.bitlen;
1324 filter.sel_src_mask = src.bitlen;
1326 } else if (strcmp(*argv, "dst") == 0) {
1329 get_prefix(&dst, *argv, preferred_family);
1330 if (dst.family == AF_UNSPEC)
1331 invarg("\"dst\" address family is AF_UNSPEC", *argv);
1332 sel->family = dst.family;
1334 memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
1335 sel->prefixlen_d = dst.bitlen;
1337 filter.sel_dst_mask = dst.bitlen;
1339 } else if (strcmp(*argv, "dev") == 0) {
1344 if (strcmp(*argv, "none") == 0)
1347 ifindex = ll_name_to_index(*argv);
1349 invarg("\"DEV\" is invalid", *argv);
1351 sel->ifindex = ifindex;
1353 filter.sel_dev_mask = XFRM_FILTER_MASK_FULL;
1357 PREV_ARG(); /* back track */
1361 xfrm_selector_upspec_parse(sel, &argc, &argv);
1371 if (src.family && dst.family && (src.family != dst.family))
1372 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
1375 missarg("SELECTOR");
1383 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft,
1384 int *argcp, char ***argvp)
1387 char **argv = *argvp;
1390 if (strcmp(*argv, "time-soft") == 0) {
1392 ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
1394 invarg("\"time-soft\" value is invalid", *argv);
1395 } else if (strcmp(*argv, "time-hard") == 0) {
1397 ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
1399 invarg("\"time-hard\" value is invalid", *argv);
1400 } else if (strcmp(*argv, "time-use-soft") == 0) {
1402 ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
1404 invarg("\"time-use-soft\" value is invalid", *argv);
1405 } else if (strcmp(*argv, "time-use-hard") == 0) {
1407 ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
1409 invarg("\"time-use-hard\" value is invalid", *argv);
1410 } else if (strcmp(*argv, "byte-soft") == 0) {
1412 ret = get_u64(&lft->soft_byte_limit, *argv, 0);
1414 invarg("\"byte-soft\" value is invalid", *argv);
1415 } else if (strcmp(*argv, "byte-hard") == 0) {
1417 ret = get_u64(&lft->hard_byte_limit, *argv, 0);
1419 invarg("\"byte-hard\" value is invalid", *argv);
1420 } else if (strcmp(*argv, "packet-soft") == 0) {
1422 ret = get_u64(&lft->soft_packet_limit, *argv, 0);
1424 invarg("\"packet-soft\" value is invalid", *argv);
1425 } else if (strcmp(*argv, "packet-hard") == 0) {
1427 ret = get_u64(&lft->hard_packet_limit, *argv, 0);
1429 invarg("\"packet-hard\" value is invalid", *argv);
1431 invarg("\"LIMIT\" is invalid", *argv);
1439 int do_xfrm(int argc, char **argv)
1441 memset(&filter, 0, sizeof(filter));
1446 if (matches(*argv, "state") == 0 ||
1447 matches(*argv, "sa") == 0)
1448 return do_xfrm_state(argc-1, argv+1);
1449 else if (matches(*argv, "policy") == 0)
1450 return do_xfrm_policy(argc-1, argv+1);
1451 else if (matches(*argv, "monitor") == 0)
1452 return do_xfrm_monitor(argc-1, argv+1);
1453 else if (matches(*argv, "help") == 0) {
1455 fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv);