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 void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
633 FILE *fp, const char *prefix)
635 if (tb[XFRMA_ALG_AUTH]) {
636 struct rtattr *rta = tb[XFRMA_ALG_AUTH];
637 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
638 XFRMA_ALG_AUTH, RTA_PAYLOAD(rta), fp, prefix);
641 if (tb[XFRMA_ALG_AEAD]) {
642 struct rtattr *rta = tb[XFRMA_ALG_AEAD];
643 xfrm_aead_print((struct xfrm_algo_aead *)RTA_DATA(rta),
644 RTA_PAYLOAD(rta), fp, prefix);
647 if (tb[XFRMA_ALG_CRYPT]) {
648 struct rtattr *rta = tb[XFRMA_ALG_CRYPT];
649 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
650 XFRMA_ALG_CRYPT, RTA_PAYLOAD(rta), fp, prefix);
653 if (tb[XFRMA_ALG_COMP]) {
654 struct rtattr *rta = tb[XFRMA_ALG_COMP];
655 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
656 XFRMA_ALG_COMP, RTA_PAYLOAD(rta), fp, prefix);
659 if (tb[XFRMA_ENCAP]) {
660 struct xfrm_encap_tmpl *e;
665 fprintf(fp, "encap ");
667 if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) {
668 fprintf(fp, "(ERROR truncated)");
669 fprintf(fp, "%s", _SL_);
672 e = (struct xfrm_encap_tmpl *) RTA_DATA(tb[XFRMA_ENCAP]);
674 fprintf(fp, "type ");
675 switch (e->encap_type) {
677 fprintf(fp, "espinudp-nonike ");
680 fprintf(fp, "espinudp ");
683 fprintf(fp, "%u ", e->encap_type);
686 fprintf(fp, "sport %u ", ntohs(e->encap_sport));
687 fprintf(fp, "dport %u ", ntohs(e->encap_dport));
689 memset(abuf, '\0', sizeof(abuf));
690 fprintf(fp, "addr %s",
691 rt_addr_n2a(family, sizeof(e->encap_oa),
692 &e->encap_oa, abuf, sizeof(abuf)));
693 fprintf(fp, "%s", _SL_);
696 if (tb[XFRMA_TMPL]) {
697 struct rtattr *rta = tb[XFRMA_TMPL];
698 xfrm_tmpl_print((struct xfrm_user_tmpl *) RTA_DATA(rta),
699 RTA_PAYLOAD(rta), family, fp, prefix);
702 if (tb[XFRMA_COADDR]) {
710 coa = (xfrm_address_t *)RTA_DATA(tb[XFRMA_COADDR]);
712 if (RTA_PAYLOAD(tb[XFRMA_COADDR]) < sizeof(*coa)) {
713 fprintf(fp, "(ERROR truncated)");
714 fprintf(fp, "%s", _SL_);
718 memset(abuf, '\0', sizeof(abuf));
720 rt_addr_n2a(family, sizeof(*coa), coa,
721 abuf, sizeof(abuf)));
722 fprintf(fp, "%s", _SL_);
725 if (tb[XFRMA_LASTUSED]) {
730 fprintf(fp, "lastused ");
732 if (RTA_PAYLOAD(tb[XFRMA_LASTUSED]) < sizeof(lastused)) {
733 fprintf(fp, "(ERROR truncated)");
734 fprintf(fp, "%s", _SL_);
738 lastused = *(__u64 *)RTA_DATA(tb[XFRMA_LASTUSED]);
740 fprintf(fp, "%s", strxf_time(lastused));
741 fprintf(fp, "%s", _SL_);
745 static int xfrm_selector_iszero(struct xfrm_selector *s)
747 struct xfrm_selector s0;
749 memset(&s0, 0, sizeof(s0));
751 return (memcmp(&s0, s, sizeof(s0)) == 0);
754 void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo,
755 struct rtattr *tb[], FILE *fp, const char *prefix,
758 char buf[STRBUF_SIZE];
759 int force_spi = xfrm_xfrmproto_is_ipsec(xsinfo->id.proto);
761 memset(buf, '\0', sizeof(buf));
763 xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode,
764 xsinfo->reqid, xsinfo->family, force_spi, fp,
768 STRBUF_CAT(buf, prefix);
769 STRBUF_CAT(buf, "\t");
772 fprintf(fp, "replay-window %u ", xsinfo->replay_window);
774 fprintf(fp, "seq 0x%08u ", xsinfo->seq);
775 if (show_stats > 0 || xsinfo->flags) {
776 __u8 flags = xsinfo->flags;
778 fprintf(fp, "flag ");
779 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOECN, "noecn");
780 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp");
781 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOPMTUDISC, "nopmtudisc");
782 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_WILDRECV, "wildrecv");
784 fprintf(fp, "%x", flags);
787 fprintf(fp, " (0x%s)", strxf_mask8(xsinfo->flags));
788 fprintf(fp, "%s", _SL_);
790 xfrm_xfrma_print(tb, xsinfo->family, fp, buf);
792 if (!xfrm_selector_iszero(&xsinfo->sel)) {
793 char sbuf[STRBUF_SIZE];
795 memcpy(sbuf, buf, sizeof(sbuf));
796 STRBUF_CAT(sbuf, "sel ");
798 xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, sbuf);
801 if (show_stats > 0) {
802 xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, buf);
803 xfrm_stats_print(&xsinfo->stats, fp, buf);
807 void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
808 struct rtattr *tb[], FILE *fp, const char *prefix,
811 char buf[STRBUF_SIZE];
813 memset(buf, '\0', sizeof(buf));
815 xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title);
818 STRBUF_CAT(buf, prefix);
819 STRBUF_CAT(buf, "\t");
823 switch (xpinfo->dir) {
827 case XFRM_POLICY_OUT:
830 case XFRM_POLICY_FWD:
834 fprintf(fp, "%u", xpinfo->dir);
839 switch (xpinfo->action) {
840 case XFRM_POLICY_ALLOW:
842 fprintf(fp, "action allow ");
844 case XFRM_POLICY_BLOCK:
845 fprintf(fp, "action block ");
848 fprintf(fp, "action %u ", xpinfo->action);
853 fprintf(fp, "index %u ", xpinfo->index);
854 fprintf(fp, "priority %u ", xpinfo->priority);
856 if (tb[XFRMA_POLICY_TYPE]) {
857 struct xfrm_userpolicy_type *upt;
859 fprintf(fp, "ptype ");
861 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
862 fprintf(fp, "(ERROR truncated)");
864 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
865 fprintf(fp, "%s ", strxf_ptype(upt->type));
869 fprintf(fp, "share %s ", strxf_share(xpinfo->share));
871 if (show_stats > 0 || xpinfo->flags) {
872 __u8 flags = xpinfo->flags;
874 fprintf(fp, "flag ");
875 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_LOCALOK, "localok");
877 fprintf(fp, "%x", flags);
880 fprintf(fp, " (0x%s)", strxf_mask8(xpinfo->flags));
881 fprintf(fp, "%s", _SL_);
884 xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf);
886 xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf);
889 int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
890 int loose, int *argcp, char ***argvp)
893 char **argv = *argvp;
897 memset(&dst, 0, sizeof(dst));
898 memset(&src, 0, sizeof(src));
901 if (strcmp(*argv, "src") == 0) {
904 get_prefix(&src, *argv, preferred_family);
905 if (src.family == AF_UNSPEC)
906 invarg("\"src\" address family is AF_UNSPEC", *argv);
908 *family = src.family;
910 memcpy(saddr, &src.data, sizeof(*saddr));
912 filter.id_src_mask = src.bitlen;
914 } else if (strcmp(*argv, "dst") == 0) {
917 get_prefix(&dst, *argv, preferred_family);
918 if (dst.family == AF_UNSPEC)
919 invarg("\"dst\" address family is AF_UNSPEC", *argv);
921 *family = dst.family;
923 memcpy(&id->daddr, &dst.data, sizeof(id->daddr));
925 filter.id_dst_mask = dst.bitlen;
927 } else if (strcmp(*argv, "proto") == 0) {
932 ret = xfrm_xfrmproto_getbyname(*argv);
934 invarg("\"XFRM_PROTO\" is invalid", *argv);
936 id->proto = (__u8)ret;
938 filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
940 } else if (strcmp(*argv, "spi") == 0) {
944 if (get_u32(&spi, *argv, 0))
945 invarg("\"SPI\" is invalid", *argv);
950 filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
953 PREV_ARG(); /* back track */
962 if (src.family && dst.family && (src.family != dst.family))
963 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
965 if (loose == 0 && id->proto == 0)
966 missarg("XFRM_PROTO");
976 int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp)
979 char **argv = *argvp;
981 if (matches(*argv, "transport") == 0)
982 *mode = XFRM_MODE_TRANSPORT;
983 else if (matches(*argv, "tunnel") == 0)
984 *mode = XFRM_MODE_TUNNEL;
985 else if (matches(*argv, "ro") == 0)
986 *mode = XFRM_MODE_ROUTEOPTIMIZATION;
987 else if (matches(*argv, "in_trigger") == 0)
988 *mode = XFRM_MODE_IN_TRIGGER;
989 else if (matches(*argv, "beet") == 0)
990 *mode = XFRM_MODE_BEET;
992 invarg("\"MODE\" is invalid", *argv);
1000 int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp)
1003 char **argv = *argvp;
1005 if (strcmp(*argv, "espinudp-nonike") == 0)
1007 else if (strcmp(*argv, "espinudp") == 0)
1010 invarg("\"ENCAP-TYPE\" is invalid", *argv);
1018 /* NOTE: reqid is used by host-byte order */
1019 int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp)
1022 char **argv = *argvp;
1024 if (get_u32(reqid, *argv, 0))
1025 invarg("\"REQID\" is invalid", *argv);
1033 static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
1034 int *argcp, char ***argvp)
1037 char **argv = *argvp;
1038 char *sportp = NULL;
1039 char *dportp = NULL;
1044 if (strcmp(*argv, "proto") == 0) {
1049 if (strcmp(*argv, "any") == 0)
1052 struct protoent *pp;
1053 pp = getprotobyname(*argv);
1055 upspec = pp->p_proto;
1057 if (get_u8(&upspec, *argv, 0))
1058 invarg("\"PROTO\" is invalid", *argv);
1061 sel->proto = upspec;
1063 filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL;
1065 } else if (strcmp(*argv, "sport") == 0) {
1070 if (get_u16(&sel->sport, *argv, 0))
1071 invarg("\"PORT\" is invalid", *argv);
1072 sel->sport = htons(sel->sport);
1074 sel->sport_mask = ~((__u16)0);
1076 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1078 } else if (strcmp(*argv, "dport") == 0) {
1083 if (get_u16(&sel->dport, *argv, 0))
1084 invarg("\"PORT\" is invalid", *argv);
1085 sel->dport = htons(sel->dport);
1087 sel->dport_mask = ~((__u16)0);
1089 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1091 } else if (strcmp(*argv, "type") == 0) {
1096 if (get_u16(&sel->sport, *argv, 0) ||
1097 (sel->sport & ~((__u16)0xff)))
1098 invarg("\"type\" value is invalid", *argv);
1099 sel->sport = htons(sel->sport);
1100 sel->sport_mask = ~((__u16)0);
1102 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1105 } else if (strcmp(*argv, "code") == 0) {
1110 if (get_u16(&sel->dport, *argv, 0) ||
1111 (sel->dport & ~((__u16)0xff)))
1112 invarg("\"code\" value is invalid", *argv);
1113 sel->dport = htons(sel->dport);
1114 sel->dport_mask = ~((__u16)0);
1116 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1119 PREV_ARG(); /* back track */
1129 if (sportp || dportp) {
1130 switch (sel->proto) {
1137 fprintf(stderr, "\"sport\" and \"dport\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1141 if (typep || codep) {
1142 switch (sel->proto) {
1144 case IPPROTO_ICMPV6:
1148 fprintf(stderr, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1159 int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
1162 char **argv = *argvp;
1165 char *upspecp = NULL;
1167 memset(&dst, 0, sizeof(dst));
1168 memset(&src, 0, sizeof(src));
1171 if (strcmp(*argv, "src") == 0) {
1174 get_prefix(&src, *argv, preferred_family);
1175 if (src.family == AF_UNSPEC)
1176 invarg("\"src\" address family is AF_UNSPEC", *argv);
1177 sel->family = src.family;
1179 memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
1180 sel->prefixlen_s = src.bitlen;
1182 filter.sel_src_mask = src.bitlen;
1184 } else if (strcmp(*argv, "dst") == 0) {
1187 get_prefix(&dst, *argv, preferred_family);
1188 if (dst.family == AF_UNSPEC)
1189 invarg("\"dst\" address family is AF_UNSPEC", *argv);
1190 sel->family = dst.family;
1192 memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
1193 sel->prefixlen_d = dst.bitlen;
1195 filter.sel_dst_mask = dst.bitlen;
1197 } else if (strcmp(*argv, "dev") == 0) {
1202 if (strcmp(*argv, "none") == 0)
1205 ifindex = ll_name_to_index(*argv);
1207 invarg("\"DEV\" is invalid", *argv);
1209 sel->ifindex = ifindex;
1211 filter.sel_dev_mask = XFRM_FILTER_MASK_FULL;
1215 PREV_ARG(); /* back track */
1219 xfrm_selector_upspec_parse(sel, &argc, &argv);
1229 if (src.family && dst.family && (src.family != dst.family))
1230 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
1233 missarg("SELECTOR");
1241 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft,
1242 int *argcp, char ***argvp)
1245 char **argv = *argvp;
1248 if (strcmp(*argv, "time-soft") == 0) {
1250 ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
1252 invarg("\"time-soft\" value is invalid", *argv);
1253 } else if (strcmp(*argv, "time-hard") == 0) {
1255 ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
1257 invarg("\"time-hard\" value is invalid", *argv);
1258 } else if (strcmp(*argv, "time-use-soft") == 0) {
1260 ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
1262 invarg("\"time-use-soft\" value is invalid", *argv);
1263 } else if (strcmp(*argv, "time-use-hard") == 0) {
1265 ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
1267 invarg("\"time-use-hard\" value is invalid", *argv);
1268 } else if (strcmp(*argv, "byte-soft") == 0) {
1270 ret = get_u64(&lft->soft_byte_limit, *argv, 0);
1272 invarg("\"byte-soft\" value is invalid", *argv);
1273 } else if (strcmp(*argv, "byte-hard") == 0) {
1275 ret = get_u64(&lft->hard_byte_limit, *argv, 0);
1277 invarg("\"byte-hard\" value is invalid", *argv);
1278 } else if (strcmp(*argv, "packet-soft") == 0) {
1280 ret = get_u64(&lft->soft_packet_limit, *argv, 0);
1282 invarg("\"packet-soft\" value is invalid", *argv);
1283 } else if (strcmp(*argv, "packet-hard") == 0) {
1285 ret = get_u64(&lft->hard_packet_limit, *argv, 0);
1287 invarg("\"packet-hard\" value is invalid", *argv);
1289 invarg("\"LIMIT\" is invalid", *argv);
1297 int do_xfrm(int argc, char **argv)
1299 memset(&filter, 0, sizeof(filter));
1304 if (matches(*argv, "state") == 0 ||
1305 matches(*argv, "sa") == 0)
1306 return do_xfrm_state(argc-1, argv+1);
1307 else if (matches(*argv, "policy") == 0)
1308 return do_xfrm_policy(argc-1, argv+1);
1309 else if (matches(*argv, "monitor") == 0)
1310 return do_xfrm_monitor(argc-1, argv+1);
1311 else if (matches(*argv, "help") == 0) {
1313 fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv);