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>
36 #include <linux/netlink.h>
37 #include <linux/rtnetlink.h>
38 #include <linux/xfrm.h>
43 #define STRBUF_SIZE (128)
44 #define STRBUF_CAT(buf, str) \
46 int rest = sizeof(buf) - 1 - strlen(buf); \
48 int len = strlen(str); \
51 strncat(buf, str, len); \
52 buf[sizeof(buf) - 1] = '\0'; \
56 struct xfrm_filter filter
;
58 static void usage(void) __attribute__((noreturn
));
60 static void usage(void)
63 "Usage: ip xfrm XFRM_OBJECT { COMMAND | help }\n"
64 "where XFRM_OBJECT := { state | policy | monitor }\n");
68 /* This is based on utils.c(inet_addr_match) */
69 int xfrm_addr_match(xfrm_address_t
*x1
, xfrm_address_t
*x2
, int bits
)
71 __u32
*a1
= (__u32
*)x1
;
72 __u32
*a2
= (__u32
*)x2
;
73 int words
= bits
>> 0x05;
78 if (memcmp(a1
, a2
, words
<< 2))
88 mask
= htonl((0xffffffff) << (0x20 - bits
));
97 int xfrm_xfrmproto_is_ipsec(__u8 proto
)
99 return (proto
== IPPROTO_ESP
||
100 proto
== IPPROTO_AH
||
101 proto
== IPPROTO_COMP
);
104 int xfrm_xfrmproto_is_ro(__u8 proto
)
106 return (proto
== IPPROTO_ROUTING
||
107 proto
== IPPROTO_DSTOPTS
);
115 static const struct typeent xfrmproto_types
[]= {
116 { "esp", IPPROTO_ESP
}, { "ah", IPPROTO_AH
}, { "comp", IPPROTO_COMP
},
117 { "route2", IPPROTO_ROUTING
}, { "hao", IPPROTO_DSTOPTS
},
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
)
142 const struct typeent
*t
= &xfrmproto_types
[i
];
143 if (!t
->t_name
|| t
->t_type
== -1)
146 if (t
->t_type
== proto
)
153 static const struct typeent algo_types
[]= {
154 { "enc", XFRMA_ALG_CRYPT
}, { "auth", XFRMA_ALG_AUTH
},
155 { "comp", XFRMA_ALG_COMP
}, { NULL
, -1 }
158 int xfrm_algotype_getbyname(char *name
)
163 const struct typeent
*t
= &algo_types
[i
];
164 if (!t
->t_name
|| t
->t_type
== -1)
167 if (strcmp(t
->t_name
, name
) == 0)
174 const char *strxf_algotype(int type
)
179 const struct typeent
*t
= &algo_types
[i
];
180 if (!t
->t_name
|| t
->t_type
== -1)
183 if (t
->t_type
== type
)
190 const char *strxf_mask8(__u8 mask
)
193 const int sn
= sizeof(mask
) * 8 - 1;
197 for (b
= (1 << sn
); b
> 0; b
>>= 1)
198 str
[i
++] = ((b
& mask
) ? '1' : '0');
204 const char *strxf_mask32(__u32 mask
)
208 sprintf(str
, "%.8x", mask
);
213 const char *strxf_share(__u8 share
)
221 case XFRM_SHARE_SESSION
:
222 strcpy(str
, "session");
224 case XFRM_SHARE_USER
:
227 case XFRM_SHARE_UNIQUE
:
228 strcpy(str
, "unique");
231 sprintf(str
, "%u", share
);
238 const char *strxf_proto(__u8 proto
)
244 pp
= getprotobynumber(proto
);
248 sprintf(buf
, "%u", proto
);
255 void xfrm_id_info_print(xfrm_address_t
*saddr
, struct xfrm_id
*id
,
256 __u8 mode
, __u32 reqid
, __u16 family
, int force_spi
,
257 FILE *fp
, const char *prefix
, const char *title
)
264 memset(abuf
, '\0', sizeof(abuf
));
265 fprintf(fp
, "src %s ", rt_addr_n2a(family
, sizeof(*saddr
),
266 saddr
, abuf
, sizeof(abuf
)));
267 memset(abuf
, '\0', sizeof(abuf
));
268 fprintf(fp
, "dst %s", rt_addr_n2a(family
, sizeof(id
->daddr
),
269 &id
->daddr
, abuf
, sizeof(abuf
)));
270 fprintf(fp
, "%s", _SL_
);
276 fprintf(fp
, "proto %s ", strxf_xfrmproto(id
->proto
));
278 if (show_stats
> 0 || force_spi
|| id
->spi
) {
279 __u32 spi
= ntohl(id
->spi
);
280 fprintf(fp
, "spi 0x%08x", spi
);
282 fprintf(fp
, "(%u)", spi
);
286 fprintf(fp
, "reqid %u", reqid
);
288 fprintf(fp
, "(0x%08x)", reqid
);
291 fprintf(fp
, "mode ");
293 case XFRM_MODE_TRANSPORT
:
294 fprintf(fp
, "transport");
296 case XFRM_MODE_TUNNEL
:
297 fprintf(fp
, "tunnel");
299 case XFRM_MODE_ROUTEOPTIMIZATION
:
302 case XFRM_MODE_IN_TRIGGER
:
303 fprintf(fp
, "in_trigger");
309 fprintf(fp
, "%u", mode
);
312 fprintf(fp
, "%s", _SL_
);
315 static const char *strxf_limit(__u64 limit
)
318 if (limit
== XFRM_INF
)
319 strcpy(str
, "(INF)");
321 sprintf(str
, "%llu", (unsigned long long) limit
);
326 void xfrm_stats_print(struct xfrm_stats
*s
, FILE *fp
, const char *prefix
)
330 fprintf(fp
, "stats:");
331 fprintf(fp
, "%s", _SL_
);
336 fprintf(fp
, "replay-window %u ", s
->replay_window
);
337 fprintf(fp
, "replay %u ", s
->replay
);
338 fprintf(fp
, "failed %u", s
->integrity_failed
);
339 fprintf(fp
, "%s", _SL_
);
342 static const char *strxf_time(__u64 time
)
352 /* XXX: treat time in the same manner of kernel's
353 * net/xfrm/xfrm_{user,state}.c
358 strftime(str
, sizeof(str
), "%Y-%m-%d %T", tp
);
364 void xfrm_lifetime_print(struct xfrm_lifetime_cfg
*cfg
,
365 struct xfrm_lifetime_cur
*cur
,
366 FILE *fp
, const char *prefix
)
371 fprintf(fp
, "lifetime config:");
372 fprintf(fp
, "%s", _SL_
);
377 fprintf(fp
, "limit: ");
378 fprintf(fp
, "soft ");
379 fprintf(fp
, strxf_limit(cfg
->soft_byte_limit
));
380 fprintf(fp
, "(bytes), hard ");
381 fprintf(fp
, strxf_limit(cfg
->hard_byte_limit
));
382 fprintf(fp
, "(bytes)");
383 fprintf(fp
, "%s", _SL_
);
388 fprintf(fp
, "limit: ");
389 fprintf(fp
, "soft ");
390 fprintf(fp
, strxf_limit(cfg
->soft_packet_limit
));
391 fprintf(fp
, "(packets), hard ");
392 fprintf(fp
, strxf_limit(cfg
->hard_packet_limit
));
393 fprintf(fp
, "(packets)");
394 fprintf(fp
, "%s", _SL_
);
399 fprintf(fp
, "expire add: ");
400 fprintf(fp
, "soft ");
401 fprintf(fp
, "%llu", (unsigned long long) cfg
->soft_add_expires_seconds
);
402 fprintf(fp
, "(sec), hard ");
403 fprintf(fp
, "%llu", (unsigned long long) cfg
->hard_add_expires_seconds
);
404 fprintf(fp
, "(sec)");
405 fprintf(fp
, "%s", _SL_
);
410 fprintf(fp
, "expire use: ");
411 fprintf(fp
, "soft ");
412 fprintf(fp
, "%llu", (unsigned long long) cfg
->soft_use_expires_seconds
);
413 fprintf(fp
, "(sec), hard ");
414 fprintf(fp
, "%llu", (unsigned long long) cfg
->hard_use_expires_seconds
);
415 fprintf(fp
, "(sec)");
416 fprintf(fp
, "%s", _SL_
);
421 fprintf(fp
, "lifetime current:");
422 fprintf(fp
, "%s", _SL_
);
427 fprintf(fp
, "%llu(bytes), ", (unsigned long long) cur
->bytes
);
428 fprintf(fp
, "%llu(packets)", (unsigned long long) cur
->packets
);
429 fprintf(fp
, "%s", _SL_
);
434 fprintf(fp
, "add %s ", strxf_time(cur
->add_time
));
435 fprintf(fp
, "use %s", strxf_time(cur
->use_time
));
436 fprintf(fp
, "%s", _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) {
499 memset(buf
, '\0', sizeof(buf
));
500 if_indextoname(sel
->ifindex
, buf
);
501 fprintf(fp
, "dev %s ", buf
);
505 fprintf(fp
, "uid %u", sel
->user
);
507 fprintf(fp
, "%s", _SL_
);
510 static void xfrm_algo_print(struct xfrm_algo
*algo
, int type
, int len
,
511 FILE *fp
, const char *prefix
)
519 fprintf(fp
, "%s ", strxf_algotype(type
));
521 if (len
< sizeof(*algo
)) {
522 fprintf(fp
, "(ERROR truncated)");
525 len
-= sizeof(*algo
);
527 fprintf(fp
, "%s ", algo
->alg_name
);
529 keylen
= algo
->alg_key_len
/ 8;
531 fprintf(fp
, "(ERROR truncated)");
536 for (i
= 0; i
< keylen
; i
++)
537 fprintf(fp
, "%.2x", (unsigned char)algo
->alg_key
[i
]);
540 fprintf(fp
, " (%d bits)", algo
->alg_key_len
);
543 fprintf(fp
, "%s", _SL_
);
546 static void xfrm_tmpl_print(struct xfrm_user_tmpl
*tmpls
, int len
,
547 __u16 family
, FILE *fp
, const char *prefix
)
549 int ntmpls
= len
/ sizeof(struct xfrm_user_tmpl
);
555 fprintf(fp
, "(ERROR \"tmpl\" truncated)");
556 fprintf(fp
, "%s", _SL_
);
560 for (i
= 0; i
< ntmpls
; i
++) {
561 struct xfrm_user_tmpl
*tmpl
= &tmpls
[i
];
566 xfrm_id_info_print(&tmpl
->saddr
, &tmpl
->id
, tmpl
->mode
,
567 tmpl
->reqid
, family
, 0, fp
, prefix
, "tmpl ");
569 if (show_stats
> 0 || tmpl
->optional
) {
573 switch (tmpl
->optional
) {
576 fprintf(fp
, "level required ");
579 fprintf(fp
, "level use ");
582 fprintf(fp
, "level %u ", tmpl
->optional
);
587 fprintf(fp
, "share %s ", strxf_share(tmpl
->share
));
589 fprintf(fp
, "%s", _SL_
);
592 if (show_stats
> 0) {
596 fprintf(fp
, "%s-mask %s ",
597 strxf_algotype(XFRMA_ALG_CRYPT
),
598 strxf_mask32(tmpl
->ealgos
));
599 fprintf(fp
, "%s-mask %s ",
600 strxf_algotype(XFRMA_ALG_AUTH
),
601 strxf_mask32(tmpl
->aalgos
));
602 fprintf(fp
, "%s-mask %s",
603 strxf_algotype(XFRMA_ALG_COMP
),
604 strxf_mask32(tmpl
->calgos
));
606 fprintf(fp
, "%s", _SL_
);
611 void xfrm_xfrma_print(struct rtattr
*tb
[], __u16 family
,
612 FILE *fp
, const char *prefix
)
614 if (tb
[XFRMA_ALG_AUTH
]) {
615 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH
];
616 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
617 XFRMA_ALG_AUTH
, RTA_PAYLOAD(rta
), fp
, prefix
);
620 if (tb
[XFRMA_ALG_CRYPT
]) {
621 struct rtattr
*rta
= tb
[XFRMA_ALG_CRYPT
];
622 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
623 XFRMA_ALG_CRYPT
, RTA_PAYLOAD(rta
), fp
, prefix
);
626 if (tb
[XFRMA_ALG_COMP
]) {
627 struct rtattr
*rta
= tb
[XFRMA_ALG_COMP
];
628 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
629 XFRMA_ALG_COMP
, RTA_PAYLOAD(rta
), fp
, prefix
);
632 if (tb
[XFRMA_ENCAP
]) {
633 struct xfrm_encap_tmpl
*e
;
638 fprintf(fp
, "encap ");
640 if (RTA_PAYLOAD(tb
[XFRMA_ENCAP
]) < sizeof(*e
)) {
641 fprintf(fp
, "(ERROR truncated)");
642 fprintf(fp
, "%s", _SL_
);
645 e
= (struct xfrm_encap_tmpl
*) RTA_DATA(tb
[XFRMA_ENCAP
]);
647 fprintf(fp
, "type ");
648 switch (e
->encap_type
) {
650 fprintf(fp
, "espinudp-nonike ");
653 fprintf(fp
, "espinudp ");
656 fprintf(fp
, "%u ", e
->encap_type
);
659 fprintf(fp
, "sport %u ", ntohs(e
->encap_sport
));
660 fprintf(fp
, "dport %u ", ntohs(e
->encap_dport
));
662 memset(abuf
, '\0', sizeof(abuf
));
663 fprintf(fp
, "addr %s",
664 rt_addr_n2a(family
, sizeof(e
->encap_oa
),
665 &e
->encap_oa
, abuf
, sizeof(abuf
)));
666 fprintf(fp
, "%s", _SL_
);
669 if (tb
[XFRMA_TMPL
]) {
670 struct rtattr
*rta
= tb
[XFRMA_TMPL
];
671 xfrm_tmpl_print((struct xfrm_user_tmpl
*) RTA_DATA(rta
),
672 RTA_PAYLOAD(rta
), family
, fp
, prefix
);
675 if (tb
[XFRMA_COADDR
]) {
683 coa
= (xfrm_address_t
*)RTA_DATA(tb
[XFRMA_COADDR
]);
685 if (RTA_PAYLOAD(tb
[XFRMA_COADDR
]) < sizeof(*coa
)) {
686 fprintf(fp
, "(ERROR truncated)");
687 fprintf(fp
, "%s", _SL_
);
691 memset(abuf
, '\0', sizeof(abuf
));
693 rt_addr_n2a(family
, sizeof(*coa
), coa
,
694 abuf
, sizeof(abuf
)));
695 fprintf(fp
, "%s", _SL_
);
698 if (tb
[XFRMA_LASTUSED
]) {
703 fprintf(fp
, "lastused ");
705 if (RTA_PAYLOAD(tb
[XFRMA_LASTUSED
]) < sizeof(lastused
)) {
706 fprintf(fp
, "(ERROR truncated)");
707 fprintf(fp
, "%s", _SL_
);
711 lastused
= *(__u64
*)RTA_DATA(tb
[XFRMA_LASTUSED
]);
713 fprintf(fp
, "%s", strxf_time(lastused
));
714 fprintf(fp
, "%s", _SL_
);
718 static int xfrm_selector_iszero(struct xfrm_selector
*s
)
720 struct xfrm_selector s0
;
722 memset(&s0
, 0, sizeof(s0
));
724 return (memcmp(&s0
, s
, sizeof(s0
)) == 0);
727 void xfrm_state_info_print(struct xfrm_usersa_info
*xsinfo
,
728 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
731 char buf
[STRBUF_SIZE
];
732 int force_spi
= xfrm_xfrmproto_is_ipsec(xsinfo
->id
.proto
);
734 memset(buf
, '\0', sizeof(buf
));
736 xfrm_id_info_print(&xsinfo
->saddr
, &xsinfo
->id
, xsinfo
->mode
,
737 xsinfo
->reqid
, xsinfo
->family
, force_spi
, fp
,
741 STRBUF_CAT(buf
, prefix
);
742 STRBUF_CAT(buf
, "\t");
745 fprintf(fp
, "replay-window %u ", xsinfo
->replay_window
);
747 fprintf(fp
, "seq 0x%08u ", xsinfo
->seq
);
748 if (show_stats
> 0 || xsinfo
->flags
) {
749 __u8 flags
= xsinfo
->flags
;
751 fprintf(fp
, "flag ");
752 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOECN
, "noecn");
753 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_DECAP_DSCP
, "decap-dscp");
754 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_WILDRECV
, "wildrecv");
756 fprintf(fp
, "%x", flags
);
758 fprintf(fp
, " (0x%s)", strxf_mask8(flags
));
760 fprintf(fp
, "%s", _SL_
);
762 xfrm_xfrma_print(tb
, xsinfo
->family
, fp
, buf
);
764 if (!xfrm_selector_iszero(&xsinfo
->sel
)) {
765 char sbuf
[STRBUF_SIZE
];
767 memcpy(sbuf
, buf
, sizeof(sbuf
));
768 STRBUF_CAT(sbuf
, "sel ");
770 xfrm_selector_print(&xsinfo
->sel
, xsinfo
->family
, fp
, sbuf
);
773 if (show_stats
> 0) {
774 xfrm_lifetime_print(&xsinfo
->lft
, &xsinfo
->curlft
, fp
, buf
);
775 xfrm_stats_print(&xsinfo
->stats
, fp
, buf
);
779 void xfrm_policy_info_print(struct xfrm_userpolicy_info
*xpinfo
,
780 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
783 char buf
[STRBUF_SIZE
];
784 __u8 ptype
= XFRM_POLICY_TYPE_MAIN
;
786 memset(buf
, '\0', sizeof(buf
));
788 xfrm_selector_print(&xpinfo
->sel
, preferred_family
, fp
, title
);
791 STRBUF_CAT(buf
, prefix
);
792 STRBUF_CAT(buf
, "\t");
796 switch (xpinfo
->dir
) {
800 case XFRM_POLICY_OUT
:
803 case XFRM_POLICY_FWD
:
807 fprintf(fp
, "%u", xpinfo
->dir
);
812 switch (xpinfo
->action
) {
813 case XFRM_POLICY_ALLOW
:
815 fprintf(fp
, "action allow ");
817 case XFRM_POLICY_BLOCK
:
818 fprintf(fp
, "action block ");
821 fprintf(fp
, "action %u ", xpinfo
->action
);
826 fprintf(fp
, "index %u ", xpinfo
->index
);
827 fprintf(fp
, "priority %u ", xpinfo
->priority
);
829 fprintf(fp
, "ptype ");
831 if (tb
[XFRMA_POLICY_TYPE
]) {
832 struct xfrm_userpolicy_type
*upt
;
834 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
))
835 fprintf(fp
, "(ERROR truncated)");
837 upt
= (struct xfrm_userpolicy_type
*)RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
842 case XFRM_POLICY_TYPE_MAIN
:
845 case XFRM_POLICY_TYPE_SUB
:
849 fprintf(fp
, "%u", ptype
);
854 if (show_stats
> 0) {
855 fprintf(fp
, "share %s ", strxf_share(xpinfo
->share
));
856 fprintf(fp
, "flag 0x%s", strxf_mask8(xpinfo
->flags
));
858 fprintf(fp
, "%s", _SL_
);
861 xfrm_lifetime_print(&xpinfo
->lft
, &xpinfo
->curlft
, fp
, buf
);
863 xfrm_xfrma_print(tb
, xpinfo
->sel
.family
, fp
, buf
);
866 int xfrm_id_parse(xfrm_address_t
*saddr
, struct xfrm_id
*id
, __u16
*family
,
867 int loose
, int *argcp
, char ***argvp
)
870 char **argv
= *argvp
;
874 memset(&dst
, 0, sizeof(dst
));
875 memset(&src
, 0, sizeof(src
));
878 if (strcmp(*argv
, "src") == 0) {
881 get_prefix(&src
, *argv
, preferred_family
);
882 if (src
.family
== AF_UNSPEC
)
883 invarg("\"src\" address family is AF_UNSPEC", *argv
);
885 *family
= src
.family
;
887 memcpy(saddr
, &src
.data
, sizeof(*saddr
));
889 filter
.id_src_mask
= src
.bitlen
;
891 } else if (strcmp(*argv
, "dst") == 0) {
894 get_prefix(&dst
, *argv
, preferred_family
);
895 if (dst
.family
== AF_UNSPEC
)
896 invarg("\"dst\" address family is AF_UNSPEC", *argv
);
898 *family
= dst
.family
;
900 memcpy(&id
->daddr
, &dst
.data
, sizeof(id
->daddr
));
902 filter
.id_dst_mask
= dst
.bitlen
;
904 } else if (strcmp(*argv
, "proto") == 0) {
909 ret
= xfrm_xfrmproto_getbyname(*argv
);
911 invarg("\"XFRM_PROTO\" is invalid", *argv
);
913 id
->proto
= (__u8
)ret
;
915 filter
.id_proto_mask
= XFRM_FILTER_MASK_FULL
;
917 } else if (strcmp(*argv
, "spi") == 0) {
921 if (get_u32(&spi
, *argv
, 0))
922 invarg("\"SPI\" is invalid", *argv
);
927 filter
.id_spi_mask
= XFRM_FILTER_MASK_FULL
;
930 PREV_ARG(); /* back track */
939 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
940 invarg("the same address family is required between \"src\" and \"dst\"", *argv
);
942 if (loose
== 0 && id
->proto
== 0)
943 missarg("XFRM_PROTO");
953 int xfrm_mode_parse(__u8
*mode
, int *argcp
, char ***argvp
)
956 char **argv
= *argvp
;
958 if (matches(*argv
, "transport") == 0)
959 *mode
= XFRM_MODE_TRANSPORT
;
960 else if (matches(*argv
, "tunnel") == 0)
961 *mode
= XFRM_MODE_TUNNEL
;
962 else if (matches(*argv
, "ro") == 0)
963 *mode
= XFRM_MODE_ROUTEOPTIMIZATION
;
964 else if (matches(*argv
, "in_trigger") == 0)
965 *mode
= XFRM_MODE_IN_TRIGGER
;
966 else if (matches(*argv
, "beet") == 0)
967 *mode
= XFRM_MODE_BEET
;
969 invarg("\"MODE\" is invalid", *argv
);
977 int xfrm_encap_type_parse(__u16
*type
, int *argcp
, char ***argvp
)
980 char **argv
= *argvp
;
982 if (strcmp(*argv
, "espinudp-nonike") == 0)
984 else if (strcmp(*argv
, "espinudp") == 0)
987 invarg("\"ENCAP-TYPE\" is invalid", *argv
);
995 /* NOTE: reqid is used by host-byte order */
996 int xfrm_reqid_parse(__u32
*reqid
, int *argcp
, char ***argvp
)
999 char **argv
= *argvp
;
1001 if (get_u32(reqid
, *argv
, 0))
1002 invarg("\"REQID\" is invalid", *argv
);
1010 static int xfrm_selector_upspec_parse(struct xfrm_selector
*sel
,
1011 int *argcp
, char ***argvp
)
1014 char **argv
= *argvp
;
1015 char *sportp
= NULL
;
1016 char *dportp
= NULL
;
1021 if (strcmp(*argv
, "proto") == 0) {
1026 if (strcmp(*argv
, "any") == 0)
1029 struct protoent
*pp
;
1030 pp
= getprotobyname(*argv
);
1032 upspec
= pp
->p_proto
;
1034 if (get_u8(&upspec
, *argv
, 0))
1035 invarg("\"PROTO\" is invalid", *argv
);
1038 sel
->proto
= upspec
;
1040 filter
.upspec_proto_mask
= XFRM_FILTER_MASK_FULL
;
1042 } else if (strcmp(*argv
, "sport") == 0) {
1047 if (get_u16(&sel
->sport
, *argv
, 0))
1048 invarg("\"PORT\" is invalid", *argv
);
1049 sel
->sport
= htons(sel
->sport
);
1051 sel
->sport_mask
= ~((__u16
)0);
1053 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1055 } else if (strcmp(*argv
, "dport") == 0) {
1060 if (get_u16(&sel
->dport
, *argv
, 0))
1061 invarg("\"PORT\" is invalid", *argv
);
1062 sel
->dport
= htons(sel
->dport
);
1064 sel
->dport_mask
= ~((__u16
)0);
1066 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1068 } else if (strcmp(*argv
, "type") == 0) {
1073 if (get_u16(&sel
->sport
, *argv
, 0) ||
1074 (sel
->sport
& ~((__u16
)0xff)))
1075 invarg("\"type\" value is invalid", *argv
);
1076 sel
->sport
= htons(sel
->sport
);
1077 sel
->sport_mask
= ~((__u16
)0);
1079 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1082 } else if (strcmp(*argv
, "code") == 0) {
1087 if (get_u16(&sel
->dport
, *argv
, 0) ||
1088 (sel
->dport
& ~((__u16
)0xff)))
1089 invarg("\"code\" value is invalid", *argv
);
1090 sel
->dport
= htons(sel
->dport
);
1091 sel
->dport_mask
= ~((__u16
)0);
1093 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1096 PREV_ARG(); /* back track */
1106 if (sportp
|| dportp
) {
1107 switch (sel
->proto
) {
1114 fprintf(stderr
, "\"sport\" and \"dport\" are invalid with proto=%s\n", strxf_proto(sel
->proto
));
1118 if (typep
|| codep
) {
1119 switch (sel
->proto
) {
1121 case IPPROTO_ICMPV6
:
1125 fprintf(stderr
, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel
->proto
));
1136 int xfrm_selector_parse(struct xfrm_selector
*sel
, int *argcp
, char ***argvp
)
1139 char **argv
= *argvp
;
1142 char *upspecp
= NULL
;
1144 memset(&dst
, 0, sizeof(dst
));
1145 memset(&src
, 0, sizeof(src
));
1148 if (strcmp(*argv
, "src") == 0) {
1151 get_prefix(&src
, *argv
, preferred_family
);
1152 if (src
.family
== AF_UNSPEC
)
1153 invarg("\"src\" address family is AF_UNSPEC", *argv
);
1154 sel
->family
= src
.family
;
1156 memcpy(&sel
->saddr
, &src
.data
, sizeof(sel
->saddr
));
1157 sel
->prefixlen_s
= src
.bitlen
;
1159 filter
.sel_src_mask
= src
.bitlen
;
1161 } else if (strcmp(*argv
, "dst") == 0) {
1164 get_prefix(&dst
, *argv
, preferred_family
);
1165 if (dst
.family
== AF_UNSPEC
)
1166 invarg("\"dst\" address family is AF_UNSPEC", *argv
);
1167 sel
->family
= dst
.family
;
1169 memcpy(&sel
->daddr
, &dst
.data
, sizeof(sel
->daddr
));
1170 sel
->prefixlen_d
= dst
.bitlen
;
1172 filter
.sel_dst_mask
= dst
.bitlen
;
1174 } else if (strcmp(*argv
, "dev") == 0) {
1179 if (strcmp(*argv
, "none") == 0)
1182 ifindex
= if_nametoindex(*argv
);
1184 invarg("\"DEV\" is invalid", *argv
);
1186 sel
->ifindex
= ifindex
;
1188 filter
.sel_dev_mask
= XFRM_FILTER_MASK_FULL
;
1192 PREV_ARG(); /* back track */
1196 xfrm_selector_upspec_parse(sel
, &argc
, &argv
);
1206 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1207 invarg("the same address family is required between \"src\" and \"dst\"", *argv
);
1210 missarg("SELECTOR");
1218 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg
*lft
,
1219 int *argcp
, char ***argvp
)
1222 char **argv
= *argvp
;
1225 if (strcmp(*argv
, "time-soft") == 0) {
1227 ret
= get_u64(&lft
->soft_add_expires_seconds
, *argv
, 0);
1229 invarg("\"time-soft\" value is invalid", *argv
);
1230 } else if (strcmp(*argv
, "time-hard") == 0) {
1232 ret
= get_u64(&lft
->hard_add_expires_seconds
, *argv
, 0);
1234 invarg("\"time-hard\" value is invalid", *argv
);
1235 } else if (strcmp(*argv
, "time-use-soft") == 0) {
1237 ret
= get_u64(&lft
->soft_use_expires_seconds
, *argv
, 0);
1239 invarg("\"time-use-soft\" value is invalid", *argv
);
1240 } else if (strcmp(*argv
, "time-use-hard") == 0) {
1242 ret
= get_u64(&lft
->hard_use_expires_seconds
, *argv
, 0);
1244 invarg("\"time-use-hard\" value is invalid", *argv
);
1245 } else if (strcmp(*argv
, "byte-soft") == 0) {
1247 ret
= get_u64(&lft
->soft_byte_limit
, *argv
, 0);
1249 invarg("\"byte-soft\" value is invalid", *argv
);
1250 } else if (strcmp(*argv
, "byte-hard") == 0) {
1252 ret
= get_u64(&lft
->hard_byte_limit
, *argv
, 0);
1254 invarg("\"byte-hard\" value is invalid", *argv
);
1255 } else if (strcmp(*argv
, "packet-soft") == 0) {
1257 ret
= get_u64(&lft
->soft_packet_limit
, *argv
, 0);
1259 invarg("\"packet-soft\" value is invalid", *argv
);
1260 } else if (strcmp(*argv
, "packet-hard") == 0) {
1262 ret
= get_u64(&lft
->hard_packet_limit
, *argv
, 0);
1264 invarg("\"packet-hard\" value is invalid", *argv
);
1266 invarg("\"LIMIT\" is invalid", *argv
);
1274 int do_xfrm(int argc
, char **argv
)
1276 memset(&filter
, 0, sizeof(filter
));
1281 if (matches(*argv
, "state") == 0 ||
1282 matches(*argv
, "sa") == 0)
1283 return do_xfrm_state(argc
-1, argv
+1);
1284 else if (matches(*argv
, "policy") == 0)
1285 return do_xfrm_policy(argc
-1, argv
+1);
1286 else if (matches(*argv
, "monitor") == 0)
1287 return do_xfrm_monitor(argc
-1, argv
+1);
1288 else if (matches(*argv
, "help") == 0) {
1290 fprintf(stderr
, "xfrm Object \"%s\" is unknown.\n", *argv
);