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:%s", _SL_);
358 fprintf(fp, " replay-window %u replay %u failed %u%s",
359 s->replay_window, s->replay, s->integrity_failed, _SL_);
362 static const char *strxf_time(__u64 time)
372 /* XXX: treat time in the same manner of kernel's
373 * net/xfrm/xfrm_{user,state}.c
378 strftime(str, sizeof(str), "%Y-%m-%d %T", tp);
384 void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg,
385 struct xfrm_lifetime_cur *cur,
386 FILE *fp, const char *prefix)
391 fprintf(fp, "lifetime config:%s",_SL_);
395 fprintf(fp, " limit: soft %s(bytes),",
396 strxf_limit(cfg->soft_byte_limit));
397 fprintf(fp, " hard %s(bytes)%s",
398 strxf_limit(cfg->hard_byte_limit), _SL_);
402 fprintf(fp, " limit: soft %s(packets),",
403 strxf_limit(cfg->soft_packet_limit));
404 fprintf(fp, " hard %s(packets)%s",
405 strxf_limit(cfg->hard_packet_limit), _SL_);
409 fprintf(fp, " expire add: soft %llu(sec), hard %llu(sec)%s",
410 (unsigned long long) cfg->soft_add_expires_seconds,
411 (unsigned long long) cfg->hard_add_expires_seconds,
416 fprintf(fp, " expire use: soft %llu(sec), hard %llu(sec)%s",
417 (unsigned long long) cfg->soft_use_expires_seconds,
418 (unsigned long long) cfg->hard_use_expires_seconds,
424 fprintf(fp, "lifetime current:%s", _SL_);
428 fprintf(fp, " %llu(bytes), %llu(packets)%s",
429 (unsigned long long) cur->bytes,
430 (unsigned long long) cur->packets,
435 fprintf(fp, " add %s ", strxf_time(cur->add_time));
436 fprintf(fp, "use %s%s", strxf_time(cur->use_time), _SL_);
440 void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
441 FILE *fp, const char *prefix)
450 f = preferred_family;
455 memset(abuf, '\0', sizeof(abuf));
456 fprintf(fp, "src %s/%u ", rt_addr_n2a(f, sizeof(sel->saddr),
457 &sel->saddr, abuf, sizeof(abuf)),
460 memset(abuf, '\0', sizeof(abuf));
461 fprintf(fp, "dst %s/%u ", rt_addr_n2a(f, sizeof(sel->daddr),
462 &sel->daddr, abuf, sizeof(abuf)),
466 fprintf(fp, "proto %s ", strxf_proto(sel->proto));
467 switch (sel->proto) {
474 fprintf(fp, "sport %u ", ntohs(sel->sport));
476 fprintf(fp, "dport %u ", ntohs(sel->dport));
480 /* type/code is stored at sport/dport in selector */
482 fprintf(fp, "type %u ", ntohs(sel->sport));
484 fprintf(fp, "code %u ", ntohs(sel->dport));
488 fprintf(fp, "type %u ", ntohs(sel->sport));
489 if (sel->dport_mask) {
491 fprintf(fp, "(dport) 0x%.4x ", sel->dport);
496 if (sel->ifindex > 0)
497 fprintf(fp, "dev %s ", ll_index_to_name(sel->ifindex));
500 fprintf(fp, "uid %u", sel->user);
502 fprintf(fp, "%s", _SL_);
505 static void __xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
506 FILE *fp, const char *prefix, int newline)
514 fprintf(fp, "%s ", strxf_algotype(type));
516 if (len < sizeof(*algo)) {
517 fprintf(fp, "(ERROR truncated)");
520 len -= sizeof(*algo);
522 fprintf(fp, "%s ", algo->alg_name);
524 keylen = algo->alg_key_len / 8;
526 fprintf(fp, "(ERROR truncated)");
531 for (i = 0; i < keylen; i ++)
532 fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]);
535 fprintf(fp, " (%d bits)", algo->alg_key_len);
539 fprintf(fp, "%s", _SL_);
542 static inline void xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
543 FILE *fp, const char *prefix)
545 return __xfrm_algo_print(algo, type, len, fp, prefix, 1);
548 static void xfrm_aead_print(struct xfrm_algo_aead *algo, int len,
549 FILE *fp, const char *prefix)
552 struct xfrm_algo algo;
553 char key[algo->alg_key_len / 8];
556 memcpy(base.algo.alg_name, algo->alg_name, sizeof(base.algo.alg_name));
557 base.algo.alg_key_len = algo->alg_key_len;
558 memcpy(base.algo.alg_key, algo->alg_key, algo->alg_key_len / 8);
560 __xfrm_algo_print(&base.algo, XFRMA_ALG_AEAD, len, fp, prefix, 0);
562 fprintf(fp, " %d", algo->alg_icv_len);
564 fprintf(fp, "%s", _SL_);
567 static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int len,
568 __u16 family, FILE *fp, const char *prefix)
570 int ntmpls = len / sizeof(struct xfrm_user_tmpl);
576 fprintf(fp, "(ERROR \"tmpl\" truncated)");
577 fprintf(fp, "%s", _SL_);
581 for (i = 0; i < ntmpls; i++) {
582 struct xfrm_user_tmpl *tmpl = &tmpls[i];
587 xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode,
588 tmpl->reqid, tmpl->family, 0, fp, prefix, "tmpl ");
590 if (show_stats > 0 || tmpl->optional) {
594 switch (tmpl->optional) {
597 fprintf(fp, "level required ");
600 fprintf(fp, "level use ");
603 fprintf(fp, "level %u ", tmpl->optional);
608 fprintf(fp, "share %s ", strxf_share(tmpl->share));
610 fprintf(fp, "%s", _SL_);
613 if (show_stats > 0) {
617 fprintf(fp, "%s-mask %s ",
618 strxf_algotype(XFRMA_ALG_CRYPT),
619 strxf_mask32(tmpl->ealgos));
620 fprintf(fp, "%s-mask %s ",
621 strxf_algotype(XFRMA_ALG_AUTH),
622 strxf_mask32(tmpl->aalgos));
623 fprintf(fp, "%s-mask %s",
624 strxf_algotype(XFRMA_ALG_COMP),
625 strxf_mask32(tmpl->calgos));
627 fprintf(fp, "%s", _SL_);
632 int xfrm_parse_mark(struct xfrm_mark *mark, int *argcp, char ***argvp)
635 char **argv = *argvp;
638 if (get_u32(&mark->v, *argv, 0)) {
639 invarg("Illegal \"mark\" value\n", *argv);
643 else { /* last entry on parse line */
644 mark->m = 0xffffffff;
648 if (strcmp(*argv, "mask") == 0) {
650 if (get_u32(&mark->m, *argv, 0)) {
651 invarg("Illegal \"mark\" mask\n", *argv);
654 mark->m = 0xffffffff;
665 void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
666 FILE *fp, const char *prefix)
668 if (tb[XFRMA_MARK]) {
669 struct rtattr *rta = tb[XFRMA_MARK];
670 struct xfrm_mark *m = (struct xfrm_mark *) RTA_DATA(rta);
671 fprintf(fp, "\tmark %d/0x%x\n", m->v, m->m);
674 if (tb[XFRMA_ALG_AUTH]) {
675 struct rtattr *rta = tb[XFRMA_ALG_AUTH];
676 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
677 XFRMA_ALG_AUTH, RTA_PAYLOAD(rta), fp, prefix);
680 if (tb[XFRMA_ALG_AEAD]) {
681 struct rtattr *rta = tb[XFRMA_ALG_AEAD];
682 xfrm_aead_print((struct xfrm_algo_aead *)RTA_DATA(rta),
683 RTA_PAYLOAD(rta), fp, prefix);
686 if (tb[XFRMA_ALG_CRYPT]) {
687 struct rtattr *rta = tb[XFRMA_ALG_CRYPT];
688 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
689 XFRMA_ALG_CRYPT, RTA_PAYLOAD(rta), fp, prefix);
692 if (tb[XFRMA_ALG_COMP]) {
693 struct rtattr *rta = tb[XFRMA_ALG_COMP];
694 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
695 XFRMA_ALG_COMP, RTA_PAYLOAD(rta), fp, prefix);
698 if (tb[XFRMA_ENCAP]) {
699 struct xfrm_encap_tmpl *e;
704 fprintf(fp, "encap ");
706 if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) {
707 fprintf(fp, "(ERROR truncated)");
708 fprintf(fp, "%s", _SL_);
711 e = (struct xfrm_encap_tmpl *) RTA_DATA(tb[XFRMA_ENCAP]);
713 fprintf(fp, "type ");
714 switch (e->encap_type) {
716 fprintf(fp, "espinudp-nonike ");
719 fprintf(fp, "espinudp ");
722 fprintf(fp, "%u ", e->encap_type);
725 fprintf(fp, "sport %u ", ntohs(e->encap_sport));
726 fprintf(fp, "dport %u ", ntohs(e->encap_dport));
728 memset(abuf, '\0', sizeof(abuf));
729 fprintf(fp, "addr %s",
730 rt_addr_n2a(family, sizeof(e->encap_oa),
731 &e->encap_oa, abuf, sizeof(abuf)));
732 fprintf(fp, "%s", _SL_);
735 if (tb[XFRMA_TMPL]) {
736 struct rtattr *rta = tb[XFRMA_TMPL];
737 xfrm_tmpl_print((struct xfrm_user_tmpl *) RTA_DATA(rta),
738 RTA_PAYLOAD(rta), family, fp, prefix);
741 if (tb[XFRMA_COADDR]) {
749 coa = (xfrm_address_t *)RTA_DATA(tb[XFRMA_COADDR]);
751 if (RTA_PAYLOAD(tb[XFRMA_COADDR]) < sizeof(*coa)) {
752 fprintf(fp, "(ERROR truncated)");
753 fprintf(fp, "%s", _SL_);
757 memset(abuf, '\0', sizeof(abuf));
759 rt_addr_n2a(family, sizeof(*coa), coa,
760 abuf, sizeof(abuf)));
761 fprintf(fp, "%s", _SL_);
764 if (tb[XFRMA_LASTUSED]) {
769 fprintf(fp, "lastused ");
771 if (RTA_PAYLOAD(tb[XFRMA_LASTUSED]) < sizeof(lastused)) {
772 fprintf(fp, "(ERROR truncated)");
773 fprintf(fp, "%s", _SL_);
777 lastused = *(__u64 *)RTA_DATA(tb[XFRMA_LASTUSED]);
779 fprintf(fp, "%s", strxf_time(lastused));
780 fprintf(fp, "%s", _SL_);
785 static int xfrm_selector_iszero(struct xfrm_selector *s)
787 struct xfrm_selector s0;
789 memset(&s0, 0, sizeof(s0));
791 return (memcmp(&s0, s, sizeof(s0)) == 0);
794 void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo,
795 struct rtattr *tb[], FILE *fp, const char *prefix,
798 char buf[STRBUF_SIZE];
799 int force_spi = xfrm_xfrmproto_is_ipsec(xsinfo->id.proto);
801 memset(buf, '\0', sizeof(buf));
803 xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode,
804 xsinfo->reqid, xsinfo->family, force_spi, fp,
808 STRBUF_CAT(buf, prefix);
809 STRBUF_CAT(buf, "\t");
812 fprintf(fp, "replay-window %u ", xsinfo->replay_window);
814 fprintf(fp, "seq 0x%08u ", xsinfo->seq);
815 if (show_stats > 0 || xsinfo->flags) {
816 __u8 flags = xsinfo->flags;
818 fprintf(fp, "flag ");
819 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOECN, "noecn");
820 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp");
821 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOPMTUDISC, "nopmtudisc");
822 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_WILDRECV, "wildrecv");
823 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ICMP, "icmp");
824 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_AF_UNSPEC, "af-unspec");
826 fprintf(fp, "%x", flags);
829 fprintf(fp, " (0x%s)", strxf_mask8(xsinfo->flags));
830 fprintf(fp, "%s", _SL_);
832 xfrm_xfrma_print(tb, xsinfo->family, fp, buf);
834 if (!xfrm_selector_iszero(&xsinfo->sel)) {
835 char sbuf[STRBUF_SIZE];
837 memcpy(sbuf, buf, sizeof(sbuf));
838 STRBUF_CAT(sbuf, "sel ");
840 xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, sbuf);
843 if (show_stats > 0) {
844 xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, buf);
845 xfrm_stats_print(&xsinfo->stats, fp, buf);
849 void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
850 struct rtattr *tb[], FILE *fp, const char *prefix,
853 char buf[STRBUF_SIZE];
855 memset(buf, '\0', sizeof(buf));
857 xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title);
860 STRBUF_CAT(buf, prefix);
861 STRBUF_CAT(buf, "\t");
865 switch (xpinfo->dir) {
869 case XFRM_POLICY_OUT:
872 case XFRM_POLICY_FWD:
876 fprintf(fp, "%u", xpinfo->dir);
881 switch (xpinfo->action) {
882 case XFRM_POLICY_ALLOW:
884 fprintf(fp, "action allow ");
886 case XFRM_POLICY_BLOCK:
887 fprintf(fp, "action block ");
890 fprintf(fp, "action %u ", xpinfo->action);
895 fprintf(fp, "index %u ", xpinfo->index);
896 fprintf(fp, "priority %u ", xpinfo->priority);
898 if (tb[XFRMA_POLICY_TYPE]) {
899 struct xfrm_userpolicy_type *upt;
901 fprintf(fp, "ptype ");
903 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
904 fprintf(fp, "(ERROR truncated)");
906 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
907 fprintf(fp, "%s ", strxf_ptype(upt->type));
911 fprintf(fp, "share %s ", strxf_share(xpinfo->share));
913 if (show_stats > 0 || xpinfo->flags) {
914 __u8 flags = xpinfo->flags;
916 fprintf(fp, "flag ");
917 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_LOCALOK, "localok");
919 fprintf(fp, "%x", flags);
922 fprintf(fp, " (0x%s)", strxf_mask8(xpinfo->flags));
923 fprintf(fp, "%s", _SL_);
926 xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf);
928 xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf);
931 int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
932 int loose, int *argcp, char ***argvp)
935 char **argv = *argvp;
939 memset(&dst, 0, sizeof(dst));
940 memset(&src, 0, sizeof(src));
943 if (strcmp(*argv, "src") == 0) {
946 get_prefix(&src, *argv, preferred_family);
947 if (src.family == AF_UNSPEC)
948 invarg("\"src\" address family is AF_UNSPEC", *argv);
950 *family = src.family;
952 memcpy(saddr, &src.data, sizeof(*saddr));
954 filter.id_src_mask = src.bitlen;
956 } else if (strcmp(*argv, "dst") == 0) {
959 get_prefix(&dst, *argv, preferred_family);
960 if (dst.family == AF_UNSPEC)
961 invarg("\"dst\" address family is AF_UNSPEC", *argv);
963 *family = dst.family;
965 memcpy(&id->daddr, &dst.data, sizeof(id->daddr));
967 filter.id_dst_mask = dst.bitlen;
969 } else if (strcmp(*argv, "proto") == 0) {
974 ret = xfrm_xfrmproto_getbyname(*argv);
976 invarg("\"XFRM_PROTO\" is invalid", *argv);
978 id->proto = (__u8)ret;
980 filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
982 } else if (strcmp(*argv, "spi") == 0) {
986 if (get_u32(&spi, *argv, 0))
987 invarg("\"SPI\" is invalid", *argv);
992 filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
995 PREV_ARG(); /* back track */
1004 if (src.family && dst.family && (src.family != dst.family))
1005 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
1007 if (loose == 0 && id->proto == 0)
1008 missarg("XFRM_PROTO");
1018 int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp)
1021 char **argv = *argvp;
1023 if (matches(*argv, "transport") == 0)
1024 *mode = XFRM_MODE_TRANSPORT;
1025 else if (matches(*argv, "tunnel") == 0)
1026 *mode = XFRM_MODE_TUNNEL;
1027 else if (matches(*argv, "ro") == 0)
1028 *mode = XFRM_MODE_ROUTEOPTIMIZATION;
1029 else if (matches(*argv, "in_trigger") == 0)
1030 *mode = XFRM_MODE_IN_TRIGGER;
1031 else if (matches(*argv, "beet") == 0)
1032 *mode = XFRM_MODE_BEET;
1034 invarg("\"MODE\" is invalid", *argv);
1042 int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp)
1045 char **argv = *argvp;
1047 if (strcmp(*argv, "espinudp-nonike") == 0)
1049 else if (strcmp(*argv, "espinudp") == 0)
1052 invarg("\"ENCAP-TYPE\" is invalid", *argv);
1060 /* NOTE: reqid is used by host-byte order */
1061 int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp)
1064 char **argv = *argvp;
1066 if (get_u32(reqid, *argv, 0))
1067 invarg("\"REQID\" is invalid", *argv);
1075 static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
1076 int *argcp, char ***argvp)
1079 char **argv = *argvp;
1080 char *sportp = NULL;
1081 char *dportp = NULL;
1086 if (strcmp(*argv, "proto") == 0) {
1091 if (strcmp(*argv, "any") == 0)
1094 struct protoent *pp;
1095 pp = getprotobyname(*argv);
1097 upspec = pp->p_proto;
1099 if (get_u8(&upspec, *argv, 0))
1100 invarg("\"PROTO\" is invalid", *argv);
1103 sel->proto = upspec;
1105 filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL;
1107 } else if (strcmp(*argv, "sport") == 0) {
1112 if (get_u16(&sel->sport, *argv, 0))
1113 invarg("\"PORT\" is invalid", *argv);
1114 sel->sport = htons(sel->sport);
1116 sel->sport_mask = ~((__u16)0);
1118 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1120 } else if (strcmp(*argv, "dport") == 0) {
1125 if (get_u16(&sel->dport, *argv, 0))
1126 invarg("\"PORT\" is invalid", *argv);
1127 sel->dport = htons(sel->dport);
1129 sel->dport_mask = ~((__u16)0);
1131 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1133 } else if (strcmp(*argv, "type") == 0) {
1138 if (get_u16(&sel->sport, *argv, 0) ||
1139 (sel->sport & ~((__u16)0xff)))
1140 invarg("\"type\" value is invalid", *argv);
1141 sel->sport = htons(sel->sport);
1142 sel->sport_mask = ~((__u16)0);
1144 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1147 } else if (strcmp(*argv, "code") == 0) {
1152 if (get_u16(&sel->dport, *argv, 0) ||
1153 (sel->dport & ~((__u16)0xff)))
1154 invarg("\"code\" value is invalid", *argv);
1155 sel->dport = htons(sel->dport);
1156 sel->dport_mask = ~((__u16)0);
1158 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1161 PREV_ARG(); /* back track */
1171 if (sportp || dportp) {
1172 switch (sel->proto) {
1179 fprintf(stderr, "\"sport\" and \"dport\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1183 if (typep || codep) {
1184 switch (sel->proto) {
1186 case IPPROTO_ICMPV6:
1190 fprintf(stderr, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1201 int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
1204 char **argv = *argvp;
1207 char *upspecp = NULL;
1209 memset(&dst, 0, sizeof(dst));
1210 memset(&src, 0, sizeof(src));
1213 if (strcmp(*argv, "src") == 0) {
1216 get_prefix(&src, *argv, preferred_family);
1217 if (src.family == AF_UNSPEC)
1218 invarg("\"src\" address family is AF_UNSPEC", *argv);
1219 sel->family = src.family;
1221 memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
1222 sel->prefixlen_s = src.bitlen;
1224 filter.sel_src_mask = src.bitlen;
1226 } else if (strcmp(*argv, "dst") == 0) {
1229 get_prefix(&dst, *argv, preferred_family);
1230 if (dst.family == AF_UNSPEC)
1231 invarg("\"dst\" address family is AF_UNSPEC", *argv);
1232 sel->family = dst.family;
1234 memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
1235 sel->prefixlen_d = dst.bitlen;
1237 filter.sel_dst_mask = dst.bitlen;
1239 } else if (strcmp(*argv, "dev") == 0) {
1244 if (strcmp(*argv, "none") == 0)
1247 ifindex = ll_name_to_index(*argv);
1249 invarg("\"DEV\" is invalid", *argv);
1251 sel->ifindex = ifindex;
1253 filter.sel_dev_mask = XFRM_FILTER_MASK_FULL;
1257 PREV_ARG(); /* back track */
1261 xfrm_selector_upspec_parse(sel, &argc, &argv);
1271 if (src.family && dst.family && (src.family != dst.family))
1272 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
1275 missarg("SELECTOR");
1283 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft,
1284 int *argcp, char ***argvp)
1287 char **argv = *argvp;
1290 if (strcmp(*argv, "time-soft") == 0) {
1292 ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
1294 invarg("\"time-soft\" value is invalid", *argv);
1295 } else if (strcmp(*argv, "time-hard") == 0) {
1297 ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
1299 invarg("\"time-hard\" value is invalid", *argv);
1300 } else if (strcmp(*argv, "time-use-soft") == 0) {
1302 ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
1304 invarg("\"time-use-soft\" value is invalid", *argv);
1305 } else if (strcmp(*argv, "time-use-hard") == 0) {
1307 ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
1309 invarg("\"time-use-hard\" value is invalid", *argv);
1310 } else if (strcmp(*argv, "byte-soft") == 0) {
1312 ret = get_u64(&lft->soft_byte_limit, *argv, 0);
1314 invarg("\"byte-soft\" value is invalid", *argv);
1315 } else if (strcmp(*argv, "byte-hard") == 0) {
1317 ret = get_u64(&lft->hard_byte_limit, *argv, 0);
1319 invarg("\"byte-hard\" value is invalid", *argv);
1320 } else if (strcmp(*argv, "packet-soft") == 0) {
1322 ret = get_u64(&lft->soft_packet_limit, *argv, 0);
1324 invarg("\"packet-soft\" value is invalid", *argv);
1325 } else if (strcmp(*argv, "packet-hard") == 0) {
1327 ret = get_u64(&lft->hard_packet_limit, *argv, 0);
1329 invarg("\"packet-hard\" value is invalid", *argv);
1331 invarg("\"LIMIT\" is invalid", *argv);
1339 int do_xfrm(int argc, char **argv)
1341 memset(&filter, 0, sizeof(filter));
1346 if (matches(*argv, "state") == 0 ||
1347 matches(*argv, "sa") == 0)
1348 return do_xfrm_state(argc-1, argv+1);
1349 else if (matches(*argv, "policy") == 0)
1350 return do_xfrm_policy(argc-1, argv+1);
1351 else if (matches(*argv, "monitor") == 0)
1352 return do_xfrm_monitor(argc-1, argv+1);
1353 else if (matches(*argv, "help") == 0) {
1355 fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv);