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 },
120 int xfrm_xfrmproto_getbyname(char *name)
125 const struct typeent *t = &xfrmproto_types[i];
126 if (!t->t_name || t->t_type == -1)
129 if (strcmp(t->t_name, name) == 0)
136 const char *strxf_xfrmproto(__u8 proto)
141 const struct typeent *t = &xfrmproto_types[i];
142 if (!t->t_name || t->t_type == -1)
145 if (t->t_type == proto)
152 static const struct typeent algo_types[]= {
153 { "enc", XFRMA_ALG_CRYPT }, { "auth", XFRMA_ALG_AUTH },
154 { "comp", XFRMA_ALG_COMP }, { NULL, -1 }
157 int xfrm_algotype_getbyname(char *name)
162 const struct typeent *t = &algo_types[i];
163 if (!t->t_name || t->t_type == -1)
166 if (strcmp(t->t_name, name) == 0)
173 const char *strxf_algotype(int type)
178 const struct typeent *t = &algo_types[i];
179 if (!t->t_name || t->t_type == -1)
182 if (t->t_type == type)
189 const char *strxf_mask8(__u8 mask)
192 const int sn = sizeof(mask) * 8 - 1;
196 for (b = (1 << sn); b > 0; b >>= 1)
197 str[i++] = ((b & mask) ? '1' : '0');
203 const char *strxf_mask32(__u32 mask)
207 sprintf(str, "%.8x", mask);
212 const char *strxf_share(__u8 share)
220 case XFRM_SHARE_SESSION:
221 strcpy(str, "session");
223 case XFRM_SHARE_USER:
226 case XFRM_SHARE_UNIQUE:
227 strcpy(str, "unique");
230 sprintf(str, "%u", share);
237 const char *strxf_proto(__u8 proto)
243 pp = getprotobynumber(proto);
247 sprintf(buf, "%u", proto);
254 void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id,
255 __u8 mode, __u32 reqid, __u16 family, int force_spi,
256 FILE *fp, const char *prefix, const char *title)
263 memset(abuf, '\0', sizeof(abuf));
264 fprintf(fp, "src %s ", rt_addr_n2a(family, sizeof(*saddr),
265 saddr, abuf, sizeof(abuf)));
266 memset(abuf, '\0', sizeof(abuf));
267 fprintf(fp, "dst %s", rt_addr_n2a(family, sizeof(id->daddr),
268 &id->daddr, abuf, sizeof(abuf)));
269 fprintf(fp, "%s", _SL_);
275 fprintf(fp, "proto %s ", strxf_xfrmproto(id->proto));
277 if (show_stats > 0 || force_spi || id->spi) {
278 __u32 spi = ntohl(id->spi);
279 fprintf(fp, "spi 0x%08x", spi);
281 fprintf(fp, "(%u)", spi);
285 fprintf(fp, "reqid %u", reqid);
287 fprintf(fp, "(0x%08x)", reqid);
290 fprintf(fp, "mode ");
292 case XFRM_MODE_TRANSPORT:
293 fprintf(fp, "transport");
295 case XFRM_MODE_TUNNEL:
296 fprintf(fp, "tunnel");
298 case XFRM_MODE_ROUTEOPTIMIZATION:
301 case XFRM_MODE_IN_TRIGGER:
302 fprintf(fp, "in_trigger");
308 fprintf(fp, "%u", mode);
311 fprintf(fp, "%s", _SL_);
314 static const char *strxf_limit(__u64 limit)
317 if (limit == XFRM_INF)
318 strcpy(str, "(INF)");
320 sprintf(str, "%llu", (unsigned long long) limit);
325 void xfrm_stats_print(struct xfrm_stats *s, FILE *fp, const char *prefix)
329 fprintf(fp, "stats:");
330 fprintf(fp, "%s", _SL_);
335 fprintf(fp, "replay-window %u ", s->replay_window);
336 fprintf(fp, "replay %u ", s->replay);
337 fprintf(fp, "failed %u", s->integrity_failed);
338 fprintf(fp, "%s", _SL_);
341 static const char *strxf_time(__u64 time)
351 /* XXX: treat time in the same manner of kernel's
352 * net/xfrm/xfrm_{user,state}.c
357 strftime(str, sizeof(str), "%Y-%m-%d %T", tp);
363 void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg,
364 struct xfrm_lifetime_cur *cur,
365 FILE *fp, const char *prefix)
370 fprintf(fp, "lifetime config:");
371 fprintf(fp, "%s", _SL_);
376 fprintf(fp, "limit: ");
377 fprintf(fp, "soft ");
378 fprintf(fp, strxf_limit(cfg->soft_byte_limit));
379 fprintf(fp, "(bytes), hard ");
380 fprintf(fp, strxf_limit(cfg->hard_byte_limit));
381 fprintf(fp, "(bytes)");
382 fprintf(fp, "%s", _SL_);
387 fprintf(fp, "limit: ");
388 fprintf(fp, "soft ");
389 fprintf(fp, strxf_limit(cfg->soft_packet_limit));
390 fprintf(fp, "(packets), hard ");
391 fprintf(fp, strxf_limit(cfg->hard_packet_limit));
392 fprintf(fp, "(packets)");
393 fprintf(fp, "%s", _SL_);
398 fprintf(fp, "expire add: ");
399 fprintf(fp, "soft ");
400 fprintf(fp, "%llu", (unsigned long long) cfg->soft_add_expires_seconds);
401 fprintf(fp, "(sec), hard ");
402 fprintf(fp, "%llu", (unsigned long long) cfg->hard_add_expires_seconds);
403 fprintf(fp, "(sec)");
404 fprintf(fp, "%s", _SL_);
409 fprintf(fp, "expire use: ");
410 fprintf(fp, "soft ");
411 fprintf(fp, "%llu", (unsigned long long) cfg->soft_use_expires_seconds);
412 fprintf(fp, "(sec), hard ");
413 fprintf(fp, "%llu", (unsigned long long) cfg->hard_use_expires_seconds);
414 fprintf(fp, "(sec)");
415 fprintf(fp, "%s", _SL_);
420 fprintf(fp, "lifetime current:");
421 fprintf(fp, "%s", _SL_);
426 fprintf(fp, "%llu(bytes), ", (unsigned long long) cur->bytes);
427 fprintf(fp, "%llu(packets)", (unsigned long long) cur->packets);
428 fprintf(fp, "%s", _SL_);
433 fprintf(fp, "add %s ", strxf_time(cur->add_time));
434 fprintf(fp, "use %s", strxf_time(cur->use_time));
435 fprintf(fp, "%s", _SL_);
439 void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
440 FILE *fp, const char *prefix)
449 f = preferred_family;
454 memset(abuf, '\0', sizeof(abuf));
455 fprintf(fp, "src %s/%u ", rt_addr_n2a(f, sizeof(sel->saddr),
456 &sel->saddr, abuf, sizeof(abuf)),
459 memset(abuf, '\0', sizeof(abuf));
460 fprintf(fp, "dst %s/%u ", rt_addr_n2a(f, sizeof(sel->daddr),
461 &sel->daddr, abuf, sizeof(abuf)),
465 fprintf(fp, "proto %s ", strxf_proto(sel->proto));
466 switch (sel->proto) {
473 fprintf(fp, "sport %u ", ntohs(sel->sport));
475 fprintf(fp, "dport %u ", ntohs(sel->dport));
479 /* type/code is stored at sport/dport in selector */
481 fprintf(fp, "type %u ", ntohs(sel->sport));
483 fprintf(fp, "code %u ", ntohs(sel->dport));
487 fprintf(fp, "type %u ", ntohs(sel->sport));
488 if (sel->dport_mask) {
490 fprintf(fp, "(dport) 0x%.4x ", sel->dport);
495 if (sel->ifindex > 0)
496 fprintf(fp, "dev %s ", ll_index_to_name(sel->ifindex));
499 fprintf(fp, "uid %u", sel->user);
501 fprintf(fp, "%s", _SL_);
504 static void xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
505 FILE *fp, const char *prefix)
513 fprintf(fp, "%s ", strxf_algotype(type));
515 if (len < sizeof(*algo)) {
516 fprintf(fp, "(ERROR truncated)");
519 len -= sizeof(*algo);
521 fprintf(fp, "%s ", algo->alg_name);
523 keylen = algo->alg_key_len / 8;
525 fprintf(fp, "(ERROR truncated)");
530 for (i = 0; i < keylen; i ++)
531 fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]);
534 fprintf(fp, " (%d bits)", algo->alg_key_len);
537 fprintf(fp, "%s", _SL_);
540 static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int len,
541 __u16 family, FILE *fp, const char *prefix)
543 int ntmpls = len / sizeof(struct xfrm_user_tmpl);
549 fprintf(fp, "(ERROR \"tmpl\" truncated)");
550 fprintf(fp, "%s", _SL_);
554 for (i = 0; i < ntmpls; i++) {
555 struct xfrm_user_tmpl *tmpl = &tmpls[i];
560 xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode,
561 tmpl->reqid, family, 0, fp, prefix, "tmpl ");
563 if (show_stats > 0 || tmpl->optional) {
567 switch (tmpl->optional) {
570 fprintf(fp, "level required ");
573 fprintf(fp, "level use ");
576 fprintf(fp, "level %u ", tmpl->optional);
581 fprintf(fp, "share %s ", strxf_share(tmpl->share));
583 fprintf(fp, "%s", _SL_);
586 if (show_stats > 0) {
590 fprintf(fp, "%s-mask %s ",
591 strxf_algotype(XFRMA_ALG_CRYPT),
592 strxf_mask32(tmpl->ealgos));
593 fprintf(fp, "%s-mask %s ",
594 strxf_algotype(XFRMA_ALG_AUTH),
595 strxf_mask32(tmpl->aalgos));
596 fprintf(fp, "%s-mask %s",
597 strxf_algotype(XFRMA_ALG_COMP),
598 strxf_mask32(tmpl->calgos));
600 fprintf(fp, "%s", _SL_);
605 void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
606 FILE *fp, const char *prefix)
608 if (tb[XFRMA_ALG_AUTH]) {
609 struct rtattr *rta = tb[XFRMA_ALG_AUTH];
610 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
611 XFRMA_ALG_AUTH, RTA_PAYLOAD(rta), fp, prefix);
614 if (tb[XFRMA_ALG_CRYPT]) {
615 struct rtattr *rta = tb[XFRMA_ALG_CRYPT];
616 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
617 XFRMA_ALG_CRYPT, RTA_PAYLOAD(rta), fp, prefix);
620 if (tb[XFRMA_ALG_COMP]) {
621 struct rtattr *rta = tb[XFRMA_ALG_COMP];
622 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
623 XFRMA_ALG_COMP, RTA_PAYLOAD(rta), fp, prefix);
626 if (tb[XFRMA_ENCAP]) {
627 struct xfrm_encap_tmpl *e;
632 fprintf(fp, "encap ");
634 if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) {
635 fprintf(fp, "(ERROR truncated)");
636 fprintf(fp, "%s", _SL_);
639 e = (struct xfrm_encap_tmpl *) RTA_DATA(tb[XFRMA_ENCAP]);
641 fprintf(fp, "type ");
642 switch (e->encap_type) {
644 fprintf(fp, "espinudp-nonike ");
647 fprintf(fp, "espinudp ");
650 fprintf(fp, "%u ", e->encap_type);
653 fprintf(fp, "sport %u ", ntohs(e->encap_sport));
654 fprintf(fp, "dport %u ", ntohs(e->encap_dport));
656 memset(abuf, '\0', sizeof(abuf));
657 fprintf(fp, "addr %s",
658 rt_addr_n2a(family, sizeof(e->encap_oa),
659 &e->encap_oa, abuf, sizeof(abuf)));
660 fprintf(fp, "%s", _SL_);
663 if (tb[XFRMA_TMPL]) {
664 struct rtattr *rta = tb[XFRMA_TMPL];
665 xfrm_tmpl_print((struct xfrm_user_tmpl *) RTA_DATA(rta),
666 RTA_PAYLOAD(rta), family, fp, prefix);
669 if (tb[XFRMA_COADDR]) {
677 coa = (xfrm_address_t *)RTA_DATA(tb[XFRMA_COADDR]);
679 if (RTA_PAYLOAD(tb[XFRMA_COADDR]) < sizeof(*coa)) {
680 fprintf(fp, "(ERROR truncated)");
681 fprintf(fp, "%s", _SL_);
685 memset(abuf, '\0', sizeof(abuf));
687 rt_addr_n2a(family, sizeof(*coa), coa,
688 abuf, sizeof(abuf)));
689 fprintf(fp, "%s", _SL_);
692 if (tb[XFRMA_LASTUSED]) {
697 fprintf(fp, "lastused ");
699 if (RTA_PAYLOAD(tb[XFRMA_LASTUSED]) < sizeof(lastused)) {
700 fprintf(fp, "(ERROR truncated)");
701 fprintf(fp, "%s", _SL_);
705 lastused = *(__u64 *)RTA_DATA(tb[XFRMA_LASTUSED]);
707 fprintf(fp, "%s", strxf_time(lastused));
708 fprintf(fp, "%s", _SL_);
712 static int xfrm_selector_iszero(struct xfrm_selector *s)
714 struct xfrm_selector s0;
716 memset(&s0, 0, sizeof(s0));
718 return (memcmp(&s0, s, sizeof(s0)) == 0);
721 void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo,
722 struct rtattr *tb[], FILE *fp, const char *prefix,
725 char buf[STRBUF_SIZE];
726 int force_spi = xfrm_xfrmproto_is_ipsec(xsinfo->id.proto);
728 memset(buf, '\0', sizeof(buf));
730 xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode,
731 xsinfo->reqid, xsinfo->family, force_spi, fp,
735 STRBUF_CAT(buf, prefix);
736 STRBUF_CAT(buf, "\t");
739 fprintf(fp, "replay-window %u ", xsinfo->replay_window);
741 fprintf(fp, "seq 0x%08u ", xsinfo->seq);
742 if (show_stats > 0 || xsinfo->flags) {
743 __u8 flags = xsinfo->flags;
745 fprintf(fp, "flag ");
746 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOECN, "noecn");
747 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp");
748 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOPMTUDISC, "nopmtudisc");
749 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_WILDRECV, "wildrecv");
751 fprintf(fp, "%x", flags);
754 fprintf(fp, " (0x%s)", strxf_mask8(xsinfo->flags));
755 fprintf(fp, "%s", _SL_);
757 xfrm_xfrma_print(tb, xsinfo->family, fp, buf);
759 if (!xfrm_selector_iszero(&xsinfo->sel)) {
760 char sbuf[STRBUF_SIZE];
762 memcpy(sbuf, buf, sizeof(sbuf));
763 STRBUF_CAT(sbuf, "sel ");
765 xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, sbuf);
768 if (show_stats > 0) {
769 xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, buf);
770 xfrm_stats_print(&xsinfo->stats, fp, buf);
774 void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
775 struct rtattr *tb[], FILE *fp, const char *prefix,
778 char buf[STRBUF_SIZE];
779 __u8 ptype = XFRM_POLICY_TYPE_MAIN;
781 memset(buf, '\0', sizeof(buf));
783 xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title);
786 STRBUF_CAT(buf, prefix);
787 STRBUF_CAT(buf, "\t");
791 switch (xpinfo->dir) {
795 case XFRM_POLICY_OUT:
798 case XFRM_POLICY_FWD:
802 fprintf(fp, "%u", xpinfo->dir);
807 switch (xpinfo->action) {
808 case XFRM_POLICY_ALLOW:
810 fprintf(fp, "action allow ");
812 case XFRM_POLICY_BLOCK:
813 fprintf(fp, "action block ");
816 fprintf(fp, "action %u ", xpinfo->action);
821 fprintf(fp, "index %u ", xpinfo->index);
822 fprintf(fp, "priority %u ", xpinfo->priority);
824 fprintf(fp, "ptype ");
826 if (tb[XFRMA_POLICY_TYPE]) {
827 struct xfrm_userpolicy_type *upt;
829 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
830 fprintf(fp, "(ERROR truncated)");
832 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
837 case XFRM_POLICY_TYPE_MAIN:
840 case XFRM_POLICY_TYPE_SUB:
844 fprintf(fp, "%u", ptype);
850 fprintf(fp, "share %s ", strxf_share(xpinfo->share));
852 if (show_stats > 0 || xpinfo->flags) {
853 __u8 flags = xpinfo->flags;
855 fprintf(fp, "flag ");
856 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_LOCALOK, "localok");
858 fprintf(fp, "%x", flags);
861 fprintf(fp, " (0x%s)", strxf_mask8(xpinfo->flags));
862 fprintf(fp, "%s", _SL_);
865 xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf);
867 xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf);
870 int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
871 int loose, int *argcp, char ***argvp)
874 char **argv = *argvp;
878 memset(&dst, 0, sizeof(dst));
879 memset(&src, 0, sizeof(src));
882 if (strcmp(*argv, "src") == 0) {
885 get_prefix(&src, *argv, preferred_family);
886 if (src.family == AF_UNSPEC)
887 invarg("\"src\" address family is AF_UNSPEC", *argv);
889 *family = src.family;
891 memcpy(saddr, &src.data, sizeof(*saddr));
893 filter.id_src_mask = src.bitlen;
895 } else if (strcmp(*argv, "dst") == 0) {
898 get_prefix(&dst, *argv, preferred_family);
899 if (dst.family == AF_UNSPEC)
900 invarg("\"dst\" address family is AF_UNSPEC", *argv);
902 *family = dst.family;
904 memcpy(&id->daddr, &dst.data, sizeof(id->daddr));
906 filter.id_dst_mask = dst.bitlen;
908 } else if (strcmp(*argv, "proto") == 0) {
913 ret = xfrm_xfrmproto_getbyname(*argv);
915 invarg("\"XFRM_PROTO\" is invalid", *argv);
917 id->proto = (__u8)ret;
919 filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
921 } else if (strcmp(*argv, "spi") == 0) {
925 if (get_u32(&spi, *argv, 0))
926 invarg("\"SPI\" is invalid", *argv);
931 filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
934 PREV_ARG(); /* back track */
943 if (src.family && dst.family && (src.family != dst.family))
944 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
946 if (loose == 0 && id->proto == 0)
947 missarg("XFRM_PROTO");
957 int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp)
960 char **argv = *argvp;
962 if (matches(*argv, "transport") == 0)
963 *mode = XFRM_MODE_TRANSPORT;
964 else if (matches(*argv, "tunnel") == 0)
965 *mode = XFRM_MODE_TUNNEL;
966 else if (matches(*argv, "ro") == 0)
967 *mode = XFRM_MODE_ROUTEOPTIMIZATION;
968 else if (matches(*argv, "in_trigger") == 0)
969 *mode = XFRM_MODE_IN_TRIGGER;
970 else if (matches(*argv, "beet") == 0)
971 *mode = XFRM_MODE_BEET;
973 invarg("\"MODE\" is invalid", *argv);
981 int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp)
984 char **argv = *argvp;
986 if (strcmp(*argv, "espinudp-nonike") == 0)
988 else if (strcmp(*argv, "espinudp") == 0)
991 invarg("\"ENCAP-TYPE\" is invalid", *argv);
999 /* NOTE: reqid is used by host-byte order */
1000 int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp)
1003 char **argv = *argvp;
1005 if (get_u32(reqid, *argv, 0))
1006 invarg("\"REQID\" is invalid", *argv);
1014 static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
1015 int *argcp, char ***argvp)
1018 char **argv = *argvp;
1019 char *sportp = NULL;
1020 char *dportp = NULL;
1025 if (strcmp(*argv, "proto") == 0) {
1030 if (strcmp(*argv, "any") == 0)
1033 struct protoent *pp;
1034 pp = getprotobyname(*argv);
1036 upspec = pp->p_proto;
1038 if (get_u8(&upspec, *argv, 0))
1039 invarg("\"PROTO\" is invalid", *argv);
1042 sel->proto = upspec;
1044 filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL;
1046 } else if (strcmp(*argv, "sport") == 0) {
1051 if (get_u16(&sel->sport, *argv, 0))
1052 invarg("\"PORT\" is invalid", *argv);
1053 sel->sport = htons(sel->sport);
1055 sel->sport_mask = ~((__u16)0);
1057 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1059 } else if (strcmp(*argv, "dport") == 0) {
1064 if (get_u16(&sel->dport, *argv, 0))
1065 invarg("\"PORT\" is invalid", *argv);
1066 sel->dport = htons(sel->dport);
1068 sel->dport_mask = ~((__u16)0);
1070 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1072 } else if (strcmp(*argv, "type") == 0) {
1077 if (get_u16(&sel->sport, *argv, 0) ||
1078 (sel->sport & ~((__u16)0xff)))
1079 invarg("\"type\" value is invalid", *argv);
1080 sel->sport = htons(sel->sport);
1081 sel->sport_mask = ~((__u16)0);
1083 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1086 } else if (strcmp(*argv, "code") == 0) {
1091 if (get_u16(&sel->dport, *argv, 0) ||
1092 (sel->dport & ~((__u16)0xff)))
1093 invarg("\"code\" value is invalid", *argv);
1094 sel->dport = htons(sel->dport);
1095 sel->dport_mask = ~((__u16)0);
1097 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1100 PREV_ARG(); /* back track */
1110 if (sportp || dportp) {
1111 switch (sel->proto) {
1118 fprintf(stderr, "\"sport\" and \"dport\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1122 if (typep || codep) {
1123 switch (sel->proto) {
1125 case IPPROTO_ICMPV6:
1129 fprintf(stderr, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1140 int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
1143 char **argv = *argvp;
1146 char *upspecp = NULL;
1148 memset(&dst, 0, sizeof(dst));
1149 memset(&src, 0, sizeof(src));
1152 if (strcmp(*argv, "src") == 0) {
1155 get_prefix(&src, *argv, preferred_family);
1156 if (src.family == AF_UNSPEC)
1157 invarg("\"src\" address family is AF_UNSPEC", *argv);
1158 sel->family = src.family;
1160 memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
1161 sel->prefixlen_s = src.bitlen;
1163 filter.sel_src_mask = src.bitlen;
1165 } else if (strcmp(*argv, "dst") == 0) {
1168 get_prefix(&dst, *argv, preferred_family);
1169 if (dst.family == AF_UNSPEC)
1170 invarg("\"dst\" address family is AF_UNSPEC", *argv);
1171 sel->family = dst.family;
1173 memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
1174 sel->prefixlen_d = dst.bitlen;
1176 filter.sel_dst_mask = dst.bitlen;
1178 } else if (strcmp(*argv, "dev") == 0) {
1183 if (strcmp(*argv, "none") == 0)
1186 ifindex = ll_name_to_index(*argv);
1188 invarg("\"DEV\" is invalid", *argv);
1190 sel->ifindex = ifindex;
1192 filter.sel_dev_mask = XFRM_FILTER_MASK_FULL;
1196 PREV_ARG(); /* back track */
1200 xfrm_selector_upspec_parse(sel, &argc, &argv);
1210 if (src.family && dst.family && (src.family != dst.family))
1211 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
1214 missarg("SELECTOR");
1222 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft,
1223 int *argcp, char ***argvp)
1226 char **argv = *argvp;
1229 if (strcmp(*argv, "time-soft") == 0) {
1231 ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
1233 invarg("\"time-soft\" value is invalid", *argv);
1234 } else if (strcmp(*argv, "time-hard") == 0) {
1236 ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
1238 invarg("\"time-hard\" value is invalid", *argv);
1239 } else if (strcmp(*argv, "time-use-soft") == 0) {
1241 ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
1243 invarg("\"time-use-soft\" value is invalid", *argv);
1244 } else if (strcmp(*argv, "time-use-hard") == 0) {
1246 ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
1248 invarg("\"time-use-hard\" value is invalid", *argv);
1249 } else if (strcmp(*argv, "byte-soft") == 0) {
1251 ret = get_u64(&lft->soft_byte_limit, *argv, 0);
1253 invarg("\"byte-soft\" value is invalid", *argv);
1254 } else if (strcmp(*argv, "byte-hard") == 0) {
1256 ret = get_u64(&lft->hard_byte_limit, *argv, 0);
1258 invarg("\"byte-hard\" value is invalid", *argv);
1259 } else if (strcmp(*argv, "packet-soft") == 0) {
1261 ret = get_u64(&lft->soft_packet_limit, *argv, 0);
1263 invarg("\"packet-soft\" value is invalid", *argv);
1264 } else if (strcmp(*argv, "packet-hard") == 0) {
1266 ret = get_u64(&lft->hard_packet_limit, *argv, 0);
1268 invarg("\"packet-hard\" value is invalid", *argv);
1270 invarg("\"LIMIT\" is invalid", *argv);
1278 int do_xfrm(int argc, char **argv)
1280 memset(&filter, 0, sizeof(filter));
1285 if (matches(*argv, "state") == 0 ||
1286 matches(*argv, "sa") == 0)
1287 return do_xfrm_state(argc-1, argv+1);
1288 else if (matches(*argv, "policy") == 0)
1289 return do_xfrm_policy(argc-1, argv+1);
1290 else if (matches(*argv, "monitor") == 0)
1291 return do_xfrm_monitor(argc-1, argv+1);
1292 else if (matches(*argv, "help") == 0) {
1294 fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv);