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 },
161 int xfrm_algotype_getbyname(char *name)
166 const struct typeent *t = &algo_types[i];
167 if (!t->t_name || t->t_type == -1)
170 if (strcmp(t->t_name, name) == 0)
177 const char *strxf_algotype(int type)
183 const struct typeent *t = &algo_types[i];
184 if (!t->t_name || t->t_type == -1)
187 if (t->t_type == type)
191 sprintf(str, "%d", type);
195 const char *strxf_mask8(__u8 mask)
198 const int sn = sizeof(mask) * 8 - 1;
202 for (b = (1 << sn); b > 0; b >>= 1)
203 str[i++] = ((b & mask) ? '1' : '0');
209 const char *strxf_mask32(__u32 mask)
213 sprintf(str, "%.8x", mask);
218 const char *strxf_share(__u8 share)
226 case XFRM_SHARE_SESSION:
227 strcpy(str, "session");
229 case XFRM_SHARE_USER:
232 case XFRM_SHARE_UNIQUE:
233 strcpy(str, "unique");
236 sprintf(str, "%u", share);
243 const char *strxf_proto(__u8 proto)
249 pp = getprotobynumber(proto);
253 sprintf(buf, "%u", proto);
260 const char *strxf_ptype(__u8 ptype)
265 case XFRM_POLICY_TYPE_MAIN:
268 case XFRM_POLICY_TYPE_SUB:
272 sprintf(str, "%u", ptype);
279 void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id,
280 __u8 mode, __u32 reqid, __u16 family, int force_spi,
281 FILE *fp, const char *prefix, const char *title)
288 memset(abuf, '\0', sizeof(abuf));
289 fprintf(fp, "src %s ", rt_addr_n2a(family, sizeof(*saddr),
290 saddr, abuf, sizeof(abuf)));
291 memset(abuf, '\0', sizeof(abuf));
292 fprintf(fp, "dst %s", rt_addr_n2a(family, sizeof(id->daddr),
293 &id->daddr, abuf, sizeof(abuf)));
294 fprintf(fp, "%s", _SL_);
300 fprintf(fp, "proto %s ", strxf_xfrmproto(id->proto));
302 if (show_stats > 0 || force_spi || id->spi) {
303 __u32 spi = ntohl(id->spi);
304 fprintf(fp, "spi 0x%08x", spi);
306 fprintf(fp, "(%u)", spi);
310 fprintf(fp, "reqid %u", reqid);
312 fprintf(fp, "(0x%08x)", reqid);
315 fprintf(fp, "mode ");
317 case XFRM_MODE_TRANSPORT:
318 fprintf(fp, "transport");
320 case XFRM_MODE_TUNNEL:
321 fprintf(fp, "tunnel");
323 case XFRM_MODE_ROUTEOPTIMIZATION:
326 case XFRM_MODE_IN_TRIGGER:
327 fprintf(fp, "in_trigger");
333 fprintf(fp, "%u", mode);
336 fprintf(fp, "%s", _SL_);
339 static const char *strxf_limit(__u64 limit)
342 if (limit == XFRM_INF)
343 strcpy(str, "(INF)");
345 sprintf(str, "%llu", (unsigned long long) limit);
350 void xfrm_stats_print(struct xfrm_stats *s, FILE *fp, const char *prefix)
354 fprintf(fp, "stats:");
355 fprintf(fp, "%s", _SL_);
360 fprintf(fp, "replay-window %u ", s->replay_window);
361 fprintf(fp, "replay %u ", s->replay);
362 fprintf(fp, "failed %u", s->integrity_failed);
363 fprintf(fp, "%s", _SL_);
366 static const char *strxf_time(__u64 time)
376 /* XXX: treat time in the same manner of kernel's
377 * net/xfrm/xfrm_{user,state}.c
382 strftime(str, sizeof(str), "%Y-%m-%d %T", tp);
388 void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg,
389 struct xfrm_lifetime_cur *cur,
390 FILE *fp, const char *prefix)
395 fprintf(fp, "lifetime config:");
396 fprintf(fp, "%s", _SL_);
401 fprintf(fp, "limit: ");
402 fprintf(fp, "soft ");
403 fprintf(fp, strxf_limit(cfg->soft_byte_limit));
404 fprintf(fp, "(bytes), hard ");
405 fprintf(fp, strxf_limit(cfg->hard_byte_limit));
406 fprintf(fp, "(bytes)");
407 fprintf(fp, "%s", _SL_);
412 fprintf(fp, "limit: ");
413 fprintf(fp, "soft ");
414 fprintf(fp, strxf_limit(cfg->soft_packet_limit));
415 fprintf(fp, "(packets), hard ");
416 fprintf(fp, strxf_limit(cfg->hard_packet_limit));
417 fprintf(fp, "(packets)");
418 fprintf(fp, "%s", _SL_);
423 fprintf(fp, "expire add: ");
424 fprintf(fp, "soft ");
425 fprintf(fp, "%llu", (unsigned long long) cfg->soft_add_expires_seconds);
426 fprintf(fp, "(sec), hard ");
427 fprintf(fp, "%llu", (unsigned long long) cfg->hard_add_expires_seconds);
428 fprintf(fp, "(sec)");
429 fprintf(fp, "%s", _SL_);
434 fprintf(fp, "expire use: ");
435 fprintf(fp, "soft ");
436 fprintf(fp, "%llu", (unsigned long long) cfg->soft_use_expires_seconds);
437 fprintf(fp, "(sec), hard ");
438 fprintf(fp, "%llu", (unsigned long long) cfg->hard_use_expires_seconds);
439 fprintf(fp, "(sec)");
440 fprintf(fp, "%s", _SL_);
445 fprintf(fp, "lifetime current:");
446 fprintf(fp, "%s", _SL_);
451 fprintf(fp, "%llu(bytes), ", (unsigned long long) cur->bytes);
452 fprintf(fp, "%llu(packets)", (unsigned long long) cur->packets);
453 fprintf(fp, "%s", _SL_);
458 fprintf(fp, "add %s ", strxf_time(cur->add_time));
459 fprintf(fp, "use %s", strxf_time(cur->use_time));
460 fprintf(fp, "%s", _SL_);
464 void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
465 FILE *fp, const char *prefix)
474 f = preferred_family;
479 memset(abuf, '\0', sizeof(abuf));
480 fprintf(fp, "src %s/%u ", rt_addr_n2a(f, sizeof(sel->saddr),
481 &sel->saddr, abuf, sizeof(abuf)),
484 memset(abuf, '\0', sizeof(abuf));
485 fprintf(fp, "dst %s/%u ", rt_addr_n2a(f, sizeof(sel->daddr),
486 &sel->daddr, abuf, sizeof(abuf)),
490 fprintf(fp, "proto %s ", strxf_proto(sel->proto));
491 switch (sel->proto) {
498 fprintf(fp, "sport %u ", ntohs(sel->sport));
500 fprintf(fp, "dport %u ", ntohs(sel->dport));
504 /* type/code is stored at sport/dport in selector */
506 fprintf(fp, "type %u ", ntohs(sel->sport));
508 fprintf(fp, "code %u ", ntohs(sel->dport));
512 fprintf(fp, "type %u ", ntohs(sel->sport));
513 if (sel->dport_mask) {
515 fprintf(fp, "(dport) 0x%.4x ", sel->dport);
520 if (sel->ifindex > 0)
521 fprintf(fp, "dev %s ", ll_index_to_name(sel->ifindex));
524 fprintf(fp, "uid %u", sel->user);
526 fprintf(fp, "%s", _SL_);
529 static void __xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
530 FILE *fp, const char *prefix, int newline)
538 fprintf(fp, "%s ", strxf_algotype(type));
540 if (len < sizeof(*algo)) {
541 fprintf(fp, "(ERROR truncated)");
544 len -= sizeof(*algo);
546 fprintf(fp, "%s ", algo->alg_name);
548 keylen = algo->alg_key_len / 8;
550 fprintf(fp, "(ERROR truncated)");
555 for (i = 0; i < keylen; i ++)
556 fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]);
559 fprintf(fp, " (%d bits)", algo->alg_key_len);
563 fprintf(fp, "%s", _SL_);
566 static inline void xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
567 FILE *fp, const char *prefix)
569 return __xfrm_algo_print(algo, type, len, fp, prefix, 1);
572 static void xfrm_aead_print(struct xfrm_algo_aead *algo, int len,
573 FILE *fp, const char *prefix)
576 struct xfrm_algo algo;
577 char key[algo->alg_key_len / 8];
580 memcpy(base.algo.alg_name, algo->alg_name, sizeof(base.algo.alg_name));
581 base.algo.alg_key_len = algo->alg_key_len;
582 memcpy(base.algo.alg_key, algo->alg_key, algo->alg_key_len / 8);
584 __xfrm_algo_print(&base.algo, XFRMA_ALG_AEAD, len, fp, prefix, 0);
586 fprintf(fp, " %d", algo->alg_icv_len);
588 fprintf(fp, "%s", _SL_);
591 static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int len,
592 __u16 family, FILE *fp, const char *prefix)
594 int ntmpls = len / sizeof(struct xfrm_user_tmpl);
600 fprintf(fp, "(ERROR \"tmpl\" truncated)");
601 fprintf(fp, "%s", _SL_);
605 for (i = 0; i < ntmpls; i++) {
606 struct xfrm_user_tmpl *tmpl = &tmpls[i];
611 xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode,
612 tmpl->reqid, family, 0, fp, prefix, "tmpl ");
614 if (show_stats > 0 || tmpl->optional) {
618 switch (tmpl->optional) {
621 fprintf(fp, "level required ");
624 fprintf(fp, "level use ");
627 fprintf(fp, "level %u ", tmpl->optional);
632 fprintf(fp, "share %s ", strxf_share(tmpl->share));
634 fprintf(fp, "%s", _SL_);
637 if (show_stats > 0) {
641 fprintf(fp, "%s-mask %s ",
642 strxf_algotype(XFRMA_ALG_CRYPT),
643 strxf_mask32(tmpl->ealgos));
644 fprintf(fp, "%s-mask %s ",
645 strxf_algotype(XFRMA_ALG_AUTH),
646 strxf_mask32(tmpl->aalgos));
647 fprintf(fp, "%s-mask %s",
648 strxf_algotype(XFRMA_ALG_COMP),
649 strxf_mask32(tmpl->calgos));
651 fprintf(fp, "%s", _SL_);
656 void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
657 FILE *fp, const char *prefix)
659 if (tb[XFRMA_ALG_AUTH]) {
660 struct rtattr *rta = tb[XFRMA_ALG_AUTH];
661 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
662 XFRMA_ALG_AUTH, RTA_PAYLOAD(rta), fp, prefix);
665 if (tb[XFRMA_ALG_AEAD]) {
666 struct rtattr *rta = tb[XFRMA_ALG_AEAD];
667 xfrm_aead_print((struct xfrm_algo_aead *)RTA_DATA(rta),
668 RTA_PAYLOAD(rta), fp, prefix);
671 if (tb[XFRMA_ALG_CRYPT]) {
672 struct rtattr *rta = tb[XFRMA_ALG_CRYPT];
673 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
674 XFRMA_ALG_CRYPT, RTA_PAYLOAD(rta), fp, prefix);
677 if (tb[XFRMA_ALG_COMP]) {
678 struct rtattr *rta = tb[XFRMA_ALG_COMP];
679 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
680 XFRMA_ALG_COMP, RTA_PAYLOAD(rta), fp, prefix);
683 if (tb[XFRMA_ENCAP]) {
684 struct xfrm_encap_tmpl *e;
689 fprintf(fp, "encap ");
691 if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) {
692 fprintf(fp, "(ERROR truncated)");
693 fprintf(fp, "%s", _SL_);
696 e = (struct xfrm_encap_tmpl *) RTA_DATA(tb[XFRMA_ENCAP]);
698 fprintf(fp, "type ");
699 switch (e->encap_type) {
701 fprintf(fp, "espinudp-nonike ");
704 fprintf(fp, "espinudp ");
707 fprintf(fp, "%u ", e->encap_type);
710 fprintf(fp, "sport %u ", ntohs(e->encap_sport));
711 fprintf(fp, "dport %u ", ntohs(e->encap_dport));
713 memset(abuf, '\0', sizeof(abuf));
714 fprintf(fp, "addr %s",
715 rt_addr_n2a(family, sizeof(e->encap_oa),
716 &e->encap_oa, abuf, sizeof(abuf)));
717 fprintf(fp, "%s", _SL_);
720 if (tb[XFRMA_TMPL]) {
721 struct rtattr *rta = tb[XFRMA_TMPL];
722 xfrm_tmpl_print((struct xfrm_user_tmpl *) RTA_DATA(rta),
723 RTA_PAYLOAD(rta), family, fp, prefix);
726 if (tb[XFRMA_COADDR]) {
734 coa = (xfrm_address_t *)RTA_DATA(tb[XFRMA_COADDR]);
736 if (RTA_PAYLOAD(tb[XFRMA_COADDR]) < sizeof(*coa)) {
737 fprintf(fp, "(ERROR truncated)");
738 fprintf(fp, "%s", _SL_);
742 memset(abuf, '\0', sizeof(abuf));
744 rt_addr_n2a(family, sizeof(*coa), coa,
745 abuf, sizeof(abuf)));
746 fprintf(fp, "%s", _SL_);
749 if (tb[XFRMA_LASTUSED]) {
754 fprintf(fp, "lastused ");
756 if (RTA_PAYLOAD(tb[XFRMA_LASTUSED]) < sizeof(lastused)) {
757 fprintf(fp, "(ERROR truncated)");
758 fprintf(fp, "%s", _SL_);
762 lastused = *(__u64 *)RTA_DATA(tb[XFRMA_LASTUSED]);
764 fprintf(fp, "%s", strxf_time(lastused));
765 fprintf(fp, "%s", _SL_);
769 static int xfrm_selector_iszero(struct xfrm_selector *s)
771 struct xfrm_selector s0;
773 memset(&s0, 0, sizeof(s0));
775 return (memcmp(&s0, s, sizeof(s0)) == 0);
778 void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo,
779 struct rtattr *tb[], FILE *fp, const char *prefix,
782 char buf[STRBUF_SIZE];
783 int force_spi = xfrm_xfrmproto_is_ipsec(xsinfo->id.proto);
785 memset(buf, '\0', sizeof(buf));
787 xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode,
788 xsinfo->reqid, xsinfo->family, force_spi, fp,
792 STRBUF_CAT(buf, prefix);
793 STRBUF_CAT(buf, "\t");
796 fprintf(fp, "replay-window %u ", xsinfo->replay_window);
798 fprintf(fp, "seq 0x%08u ", xsinfo->seq);
799 if (show_stats > 0 || xsinfo->flags) {
800 __u8 flags = xsinfo->flags;
802 fprintf(fp, "flag ");
803 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOECN, "noecn");
804 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp");
805 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOPMTUDISC, "nopmtudisc");
806 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_WILDRECV, "wildrecv");
808 fprintf(fp, "%x", flags);
811 fprintf(fp, " (0x%s)", strxf_mask8(xsinfo->flags));
812 fprintf(fp, "%s", _SL_);
814 xfrm_xfrma_print(tb, xsinfo->family, fp, buf);
816 if (!xfrm_selector_iszero(&xsinfo->sel)) {
817 char sbuf[STRBUF_SIZE];
819 memcpy(sbuf, buf, sizeof(sbuf));
820 STRBUF_CAT(sbuf, "sel ");
822 xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, sbuf);
825 if (show_stats > 0) {
826 xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, buf);
827 xfrm_stats_print(&xsinfo->stats, fp, buf);
831 void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
832 struct rtattr *tb[], FILE *fp, const char *prefix,
835 char buf[STRBUF_SIZE];
837 memset(buf, '\0', sizeof(buf));
839 xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title);
842 STRBUF_CAT(buf, prefix);
843 STRBUF_CAT(buf, "\t");
847 switch (xpinfo->dir) {
851 case XFRM_POLICY_OUT:
854 case XFRM_POLICY_FWD:
858 fprintf(fp, "%u", xpinfo->dir);
863 switch (xpinfo->action) {
864 case XFRM_POLICY_ALLOW:
866 fprintf(fp, "action allow ");
868 case XFRM_POLICY_BLOCK:
869 fprintf(fp, "action block ");
872 fprintf(fp, "action %u ", xpinfo->action);
877 fprintf(fp, "index %u ", xpinfo->index);
878 fprintf(fp, "priority %u ", xpinfo->priority);
880 if (tb[XFRMA_POLICY_TYPE]) {
881 struct xfrm_userpolicy_type *upt;
883 fprintf(fp, "ptype ");
885 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
886 fprintf(fp, "(ERROR truncated)");
888 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
889 fprintf(fp, "%s ", strxf_ptype(upt->type));
893 fprintf(fp, "share %s ", strxf_share(xpinfo->share));
895 if (show_stats > 0 || xpinfo->flags) {
896 __u8 flags = xpinfo->flags;
898 fprintf(fp, "flag ");
899 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_LOCALOK, "localok");
901 fprintf(fp, "%x", flags);
904 fprintf(fp, " (0x%s)", strxf_mask8(xpinfo->flags));
905 fprintf(fp, "%s", _SL_);
908 xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf);
910 xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf);
913 int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
914 int loose, int *argcp, char ***argvp)
917 char **argv = *argvp;
921 memset(&dst, 0, sizeof(dst));
922 memset(&src, 0, sizeof(src));
925 if (strcmp(*argv, "src") == 0) {
928 get_prefix(&src, *argv, preferred_family);
929 if (src.family == AF_UNSPEC)
930 invarg("\"src\" address family is AF_UNSPEC", *argv);
932 *family = src.family;
934 memcpy(saddr, &src.data, sizeof(*saddr));
936 filter.id_src_mask = src.bitlen;
938 } else if (strcmp(*argv, "dst") == 0) {
941 get_prefix(&dst, *argv, preferred_family);
942 if (dst.family == AF_UNSPEC)
943 invarg("\"dst\" address family is AF_UNSPEC", *argv);
945 *family = dst.family;
947 memcpy(&id->daddr, &dst.data, sizeof(id->daddr));
949 filter.id_dst_mask = dst.bitlen;
951 } else if (strcmp(*argv, "proto") == 0) {
956 ret = xfrm_xfrmproto_getbyname(*argv);
958 invarg("\"XFRM_PROTO\" is invalid", *argv);
960 id->proto = (__u8)ret;
962 filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
964 } else if (strcmp(*argv, "spi") == 0) {
968 if (get_u32(&spi, *argv, 0))
969 invarg("\"SPI\" is invalid", *argv);
974 filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
977 PREV_ARG(); /* back track */
986 if (src.family && dst.family && (src.family != dst.family))
987 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
989 if (loose == 0 && id->proto == 0)
990 missarg("XFRM_PROTO");
1000 int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp)
1003 char **argv = *argvp;
1005 if (matches(*argv, "transport") == 0)
1006 *mode = XFRM_MODE_TRANSPORT;
1007 else if (matches(*argv, "tunnel") == 0)
1008 *mode = XFRM_MODE_TUNNEL;
1009 else if (matches(*argv, "ro") == 0)
1010 *mode = XFRM_MODE_ROUTEOPTIMIZATION;
1011 else if (matches(*argv, "in_trigger") == 0)
1012 *mode = XFRM_MODE_IN_TRIGGER;
1013 else if (matches(*argv, "beet") == 0)
1014 *mode = XFRM_MODE_BEET;
1016 invarg("\"MODE\" is invalid", *argv);
1024 int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp)
1027 char **argv = *argvp;
1029 if (strcmp(*argv, "espinudp-nonike") == 0)
1031 else if (strcmp(*argv, "espinudp") == 0)
1034 invarg("\"ENCAP-TYPE\" is invalid", *argv);
1042 /* NOTE: reqid is used by host-byte order */
1043 int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp)
1046 char **argv = *argvp;
1048 if (get_u32(reqid, *argv, 0))
1049 invarg("\"REQID\" is invalid", *argv);
1057 static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
1058 int *argcp, char ***argvp)
1061 char **argv = *argvp;
1062 char *sportp = NULL;
1063 char *dportp = NULL;
1068 if (strcmp(*argv, "proto") == 0) {
1073 if (strcmp(*argv, "any") == 0)
1076 struct protoent *pp;
1077 pp = getprotobyname(*argv);
1079 upspec = pp->p_proto;
1081 if (get_u8(&upspec, *argv, 0))
1082 invarg("\"PROTO\" is invalid", *argv);
1085 sel->proto = upspec;
1087 filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL;
1089 } else if (strcmp(*argv, "sport") == 0) {
1094 if (get_u16(&sel->sport, *argv, 0))
1095 invarg("\"PORT\" is invalid", *argv);
1096 sel->sport = htons(sel->sport);
1098 sel->sport_mask = ~((__u16)0);
1100 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1102 } else if (strcmp(*argv, "dport") == 0) {
1107 if (get_u16(&sel->dport, *argv, 0))
1108 invarg("\"PORT\" is invalid", *argv);
1109 sel->dport = htons(sel->dport);
1111 sel->dport_mask = ~((__u16)0);
1113 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1115 } else if (strcmp(*argv, "type") == 0) {
1120 if (get_u16(&sel->sport, *argv, 0) ||
1121 (sel->sport & ~((__u16)0xff)))
1122 invarg("\"type\" value is invalid", *argv);
1123 sel->sport = htons(sel->sport);
1124 sel->sport_mask = ~((__u16)0);
1126 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1129 } else if (strcmp(*argv, "code") == 0) {
1134 if (get_u16(&sel->dport, *argv, 0) ||
1135 (sel->dport & ~((__u16)0xff)))
1136 invarg("\"code\" value is invalid", *argv);
1137 sel->dport = htons(sel->dport);
1138 sel->dport_mask = ~((__u16)0);
1140 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1143 PREV_ARG(); /* back track */
1153 if (sportp || dportp) {
1154 switch (sel->proto) {
1161 fprintf(stderr, "\"sport\" and \"dport\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1165 if (typep || codep) {
1166 switch (sel->proto) {
1168 case IPPROTO_ICMPV6:
1172 fprintf(stderr, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1183 int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
1186 char **argv = *argvp;
1189 char *upspecp = NULL;
1191 memset(&dst, 0, sizeof(dst));
1192 memset(&src, 0, sizeof(src));
1195 if (strcmp(*argv, "src") == 0) {
1198 get_prefix(&src, *argv, preferred_family);
1199 if (src.family == AF_UNSPEC)
1200 invarg("\"src\" address family is AF_UNSPEC", *argv);
1201 sel->family = src.family;
1203 memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
1204 sel->prefixlen_s = src.bitlen;
1206 filter.sel_src_mask = src.bitlen;
1208 } else if (strcmp(*argv, "dst") == 0) {
1211 get_prefix(&dst, *argv, preferred_family);
1212 if (dst.family == AF_UNSPEC)
1213 invarg("\"dst\" address family is AF_UNSPEC", *argv);
1214 sel->family = dst.family;
1216 memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
1217 sel->prefixlen_d = dst.bitlen;
1219 filter.sel_dst_mask = dst.bitlen;
1221 } else if (strcmp(*argv, "dev") == 0) {
1226 if (strcmp(*argv, "none") == 0)
1229 ifindex = ll_name_to_index(*argv);
1231 invarg("\"DEV\" is invalid", *argv);
1233 sel->ifindex = ifindex;
1235 filter.sel_dev_mask = XFRM_FILTER_MASK_FULL;
1239 PREV_ARG(); /* back track */
1243 xfrm_selector_upspec_parse(sel, &argc, &argv);
1253 if (src.family && dst.family && (src.family != dst.family))
1254 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
1257 missarg("SELECTOR");
1265 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft,
1266 int *argcp, char ***argvp)
1269 char **argv = *argvp;
1272 if (strcmp(*argv, "time-soft") == 0) {
1274 ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
1276 invarg("\"time-soft\" value is invalid", *argv);
1277 } else if (strcmp(*argv, "time-hard") == 0) {
1279 ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
1281 invarg("\"time-hard\" value is invalid", *argv);
1282 } else if (strcmp(*argv, "time-use-soft") == 0) {
1284 ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
1286 invarg("\"time-use-soft\" value is invalid", *argv);
1287 } else if (strcmp(*argv, "time-use-hard") == 0) {
1289 ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
1291 invarg("\"time-use-hard\" value is invalid", *argv);
1292 } else if (strcmp(*argv, "byte-soft") == 0) {
1294 ret = get_u64(&lft->soft_byte_limit, *argv, 0);
1296 invarg("\"byte-soft\" value is invalid", *argv);
1297 } else if (strcmp(*argv, "byte-hard") == 0) {
1299 ret = get_u64(&lft->hard_byte_limit, *argv, 0);
1301 invarg("\"byte-hard\" value is invalid", *argv);
1302 } else if (strcmp(*argv, "packet-soft") == 0) {
1304 ret = get_u64(&lft->soft_packet_limit, *argv, 0);
1306 invarg("\"packet-soft\" value is invalid", *argv);
1307 } else if (strcmp(*argv, "packet-hard") == 0) {
1309 ret = get_u64(&lft->hard_packet_limit, *argv, 0);
1311 invarg("\"packet-hard\" value is invalid", *argv);
1313 invarg("\"LIMIT\" is invalid", *argv);
1321 int do_xfrm(int argc, char **argv)
1323 memset(&filter, 0, sizeof(filter));
1328 if (matches(*argv, "state") == 0 ||
1329 matches(*argv, "sa") == 0)
1330 return do_xfrm_state(argc-1, argv+1);
1331 else if (matches(*argv, "policy") == 0)
1332 return do_xfrm_policy(argc-1, argv+1);
1333 else if (matches(*argv, "monitor") == 0)
1334 return do_xfrm_monitor(argc-1, argv+1);
1335 else if (matches(*argv, "help") == 0) {
1337 fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv);