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
}, { NULL
, -1 }
160 int xfrm_algotype_getbyname(char *name
)
165 const struct typeent
*t
= &algo_types
[i
];
166 if (!t
->t_name
|| t
->t_type
== -1)
169 if (strcmp(t
->t_name
, name
) == 0)
176 const char *strxf_algotype(int type
)
182 const struct typeent
*t
= &algo_types
[i
];
183 if (!t
->t_name
|| t
->t_type
== -1)
186 if (t
->t_type
== type
)
190 sprintf(str
, "%d", type
);
194 const char *strxf_mask8(__u8 mask
)
197 const int sn
= sizeof(mask
) * 8 - 1;
201 for (b
= (1 << sn
); b
> 0; b
>>= 1)
202 str
[i
++] = ((b
& mask
) ? '1' : '0');
208 const char *strxf_mask32(__u32 mask
)
212 sprintf(str
, "%.8x", mask
);
217 const char *strxf_share(__u8 share
)
225 case XFRM_SHARE_SESSION
:
226 strcpy(str
, "session");
228 case XFRM_SHARE_USER
:
231 case XFRM_SHARE_UNIQUE
:
232 strcpy(str
, "unique");
235 sprintf(str
, "%u", share
);
242 const char *strxf_proto(__u8 proto
)
248 pp
= getprotobynumber(proto
);
252 sprintf(buf
, "%u", proto
);
259 const char *strxf_ptype(__u8 ptype
)
264 case XFRM_POLICY_TYPE_MAIN
:
267 case XFRM_POLICY_TYPE_SUB
:
271 sprintf(str
, "%u", ptype
);
278 void xfrm_id_info_print(xfrm_address_t
*saddr
, struct xfrm_id
*id
,
279 __u8 mode
, __u32 reqid
, __u16 family
, int force_spi
,
280 FILE *fp
, const char *prefix
, const char *title
)
287 memset(abuf
, '\0', sizeof(abuf
));
288 fprintf(fp
, "src %s ", rt_addr_n2a(family
, sizeof(*saddr
),
289 saddr
, abuf
, sizeof(abuf
)));
290 memset(abuf
, '\0', sizeof(abuf
));
291 fprintf(fp
, "dst %s", rt_addr_n2a(family
, sizeof(id
->daddr
),
292 &id
->daddr
, abuf
, sizeof(abuf
)));
293 fprintf(fp
, "%s", _SL_
);
299 fprintf(fp
, "proto %s ", strxf_xfrmproto(id
->proto
));
301 if (show_stats
> 0 || force_spi
|| id
->spi
) {
302 __u32 spi
= ntohl(id
->spi
);
303 fprintf(fp
, "spi 0x%08x", spi
);
305 fprintf(fp
, "(%u)", spi
);
309 fprintf(fp
, "reqid %u", reqid
);
311 fprintf(fp
, "(0x%08x)", reqid
);
314 fprintf(fp
, "mode ");
316 case XFRM_MODE_TRANSPORT
:
317 fprintf(fp
, "transport");
319 case XFRM_MODE_TUNNEL
:
320 fprintf(fp
, "tunnel");
322 case XFRM_MODE_ROUTEOPTIMIZATION
:
325 case XFRM_MODE_IN_TRIGGER
:
326 fprintf(fp
, "in_trigger");
332 fprintf(fp
, "%u", mode
);
335 fprintf(fp
, "%s", _SL_
);
338 static const char *strxf_limit(__u64 limit
)
341 if (limit
== XFRM_INF
)
342 strcpy(str
, "(INF)");
344 sprintf(str
, "%llu", (unsigned long long) limit
);
349 void xfrm_stats_print(struct xfrm_stats
*s
, FILE *fp
, const char *prefix
)
353 fprintf(fp
, "stats:");
354 fprintf(fp
, "%s", _SL_
);
359 fprintf(fp
, "replay-window %u ", s
->replay_window
);
360 fprintf(fp
, "replay %u ", s
->replay
);
361 fprintf(fp
, "failed %u", s
->integrity_failed
);
362 fprintf(fp
, "%s", _SL_
);
365 static const char *strxf_time(__u64 time
)
375 /* XXX: treat time in the same manner of kernel's
376 * net/xfrm/xfrm_{user,state}.c
381 strftime(str
, sizeof(str
), "%Y-%m-%d %T", tp
);
387 void xfrm_lifetime_print(struct xfrm_lifetime_cfg
*cfg
,
388 struct xfrm_lifetime_cur
*cur
,
389 FILE *fp
, const char *prefix
)
394 fprintf(fp
, "lifetime config:");
395 fprintf(fp
, "%s", _SL_
);
400 fprintf(fp
, "limit: ");
401 fprintf(fp
, "soft ");
402 fprintf(fp
, strxf_limit(cfg
->soft_byte_limit
));
403 fprintf(fp
, "(bytes), hard ");
404 fprintf(fp
, strxf_limit(cfg
->hard_byte_limit
));
405 fprintf(fp
, "(bytes)");
406 fprintf(fp
, "%s", _SL_
);
411 fprintf(fp
, "limit: ");
412 fprintf(fp
, "soft ");
413 fprintf(fp
, strxf_limit(cfg
->soft_packet_limit
));
414 fprintf(fp
, "(packets), hard ");
415 fprintf(fp
, strxf_limit(cfg
->hard_packet_limit
));
416 fprintf(fp
, "(packets)");
417 fprintf(fp
, "%s", _SL_
);
422 fprintf(fp
, "expire add: ");
423 fprintf(fp
, "soft ");
424 fprintf(fp
, "%llu", (unsigned long long) cfg
->soft_add_expires_seconds
);
425 fprintf(fp
, "(sec), hard ");
426 fprintf(fp
, "%llu", (unsigned long long) cfg
->hard_add_expires_seconds
);
427 fprintf(fp
, "(sec)");
428 fprintf(fp
, "%s", _SL_
);
433 fprintf(fp
, "expire use: ");
434 fprintf(fp
, "soft ");
435 fprintf(fp
, "%llu", (unsigned long long) cfg
->soft_use_expires_seconds
);
436 fprintf(fp
, "(sec), hard ");
437 fprintf(fp
, "%llu", (unsigned long long) cfg
->hard_use_expires_seconds
);
438 fprintf(fp
, "(sec)");
439 fprintf(fp
, "%s", _SL_
);
444 fprintf(fp
, "lifetime current:");
445 fprintf(fp
, "%s", _SL_
);
450 fprintf(fp
, "%llu(bytes), ", (unsigned long long) cur
->bytes
);
451 fprintf(fp
, "%llu(packets)", (unsigned long long) cur
->packets
);
452 fprintf(fp
, "%s", _SL_
);
457 fprintf(fp
, "add %s ", strxf_time(cur
->add_time
));
458 fprintf(fp
, "use %s", strxf_time(cur
->use_time
));
459 fprintf(fp
, "%s", _SL_
);
463 void xfrm_selector_print(struct xfrm_selector
*sel
, __u16 family
,
464 FILE *fp
, const char *prefix
)
473 f
= preferred_family
;
478 memset(abuf
, '\0', sizeof(abuf
));
479 fprintf(fp
, "src %s/%u ", rt_addr_n2a(f
, sizeof(sel
->saddr
),
480 &sel
->saddr
, abuf
, sizeof(abuf
)),
483 memset(abuf
, '\0', sizeof(abuf
));
484 fprintf(fp
, "dst %s/%u ", rt_addr_n2a(f
, sizeof(sel
->daddr
),
485 &sel
->daddr
, abuf
, sizeof(abuf
)),
489 fprintf(fp
, "proto %s ", strxf_proto(sel
->proto
));
490 switch (sel
->proto
) {
497 fprintf(fp
, "sport %u ", ntohs(sel
->sport
));
499 fprintf(fp
, "dport %u ", ntohs(sel
->dport
));
503 /* type/code is stored at sport/dport in selector */
505 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
507 fprintf(fp
, "code %u ", ntohs(sel
->dport
));
511 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
512 if (sel
->dport_mask
) {
514 fprintf(fp
, "(dport) 0x%.4x ", sel
->dport
);
519 if (sel
->ifindex
> 0)
520 fprintf(fp
, "dev %s ", ll_index_to_name(sel
->ifindex
));
523 fprintf(fp
, "uid %u", sel
->user
);
525 fprintf(fp
, "%s", _SL_
);
528 static void xfrm_algo_print(struct xfrm_algo
*algo
, int type
, int len
,
529 FILE *fp
, const char *prefix
)
537 fprintf(fp
, "%s ", strxf_algotype(type
));
539 if (len
< sizeof(*algo
)) {
540 fprintf(fp
, "(ERROR truncated)");
543 len
-= sizeof(*algo
);
545 fprintf(fp
, "%s ", algo
->alg_name
);
547 keylen
= algo
->alg_key_len
/ 8;
549 fprintf(fp
, "(ERROR truncated)");
554 for (i
= 0; i
< keylen
; i
++)
555 fprintf(fp
, "%.2x", (unsigned char)algo
->alg_key
[i
]);
558 fprintf(fp
, " (%d bits)", algo
->alg_key_len
);
561 fprintf(fp
, "%s", _SL_
);
564 static void xfrm_tmpl_print(struct xfrm_user_tmpl
*tmpls
, int len
,
565 __u16 family
, FILE *fp
, const char *prefix
)
567 int ntmpls
= len
/ sizeof(struct xfrm_user_tmpl
);
573 fprintf(fp
, "(ERROR \"tmpl\" truncated)");
574 fprintf(fp
, "%s", _SL_
);
578 for (i
= 0; i
< ntmpls
; i
++) {
579 struct xfrm_user_tmpl
*tmpl
= &tmpls
[i
];
584 xfrm_id_info_print(&tmpl
->saddr
, &tmpl
->id
, tmpl
->mode
,
585 tmpl
->reqid
, family
, 0, fp
, prefix
, "tmpl ");
587 if (show_stats
> 0 || tmpl
->optional
) {
591 switch (tmpl
->optional
) {
594 fprintf(fp
, "level required ");
597 fprintf(fp
, "level use ");
600 fprintf(fp
, "level %u ", tmpl
->optional
);
605 fprintf(fp
, "share %s ", strxf_share(tmpl
->share
));
607 fprintf(fp
, "%s", _SL_
);
610 if (show_stats
> 0) {
614 fprintf(fp
, "%s-mask %s ",
615 strxf_algotype(XFRMA_ALG_CRYPT
),
616 strxf_mask32(tmpl
->ealgos
));
617 fprintf(fp
, "%s-mask %s ",
618 strxf_algotype(XFRMA_ALG_AUTH
),
619 strxf_mask32(tmpl
->aalgos
));
620 fprintf(fp
, "%s-mask %s",
621 strxf_algotype(XFRMA_ALG_COMP
),
622 strxf_mask32(tmpl
->calgos
));
624 fprintf(fp
, "%s", _SL_
);
629 void xfrm_xfrma_print(struct rtattr
*tb
[], __u16 family
,
630 FILE *fp
, const char *prefix
)
632 if (tb
[XFRMA_ALG_AUTH
]) {
633 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH
];
634 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
635 XFRMA_ALG_AUTH
, RTA_PAYLOAD(rta
), fp
, prefix
);
638 if (tb
[XFRMA_ALG_CRYPT
]) {
639 struct rtattr
*rta
= tb
[XFRMA_ALG_CRYPT
];
640 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
641 XFRMA_ALG_CRYPT
, RTA_PAYLOAD(rta
), fp
, prefix
);
644 if (tb
[XFRMA_ALG_COMP
]) {
645 struct rtattr
*rta
= tb
[XFRMA_ALG_COMP
];
646 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
647 XFRMA_ALG_COMP
, RTA_PAYLOAD(rta
), fp
, prefix
);
650 if (tb
[XFRMA_ENCAP
]) {
651 struct xfrm_encap_tmpl
*e
;
656 fprintf(fp
, "encap ");
658 if (RTA_PAYLOAD(tb
[XFRMA_ENCAP
]) < sizeof(*e
)) {
659 fprintf(fp
, "(ERROR truncated)");
660 fprintf(fp
, "%s", _SL_
);
663 e
= (struct xfrm_encap_tmpl
*) RTA_DATA(tb
[XFRMA_ENCAP
]);
665 fprintf(fp
, "type ");
666 switch (e
->encap_type
) {
668 fprintf(fp
, "espinudp-nonike ");
671 fprintf(fp
, "espinudp ");
674 fprintf(fp
, "%u ", e
->encap_type
);
677 fprintf(fp
, "sport %u ", ntohs(e
->encap_sport
));
678 fprintf(fp
, "dport %u ", ntohs(e
->encap_dport
));
680 memset(abuf
, '\0', sizeof(abuf
));
681 fprintf(fp
, "addr %s",
682 rt_addr_n2a(family
, sizeof(e
->encap_oa
),
683 &e
->encap_oa
, abuf
, sizeof(abuf
)));
684 fprintf(fp
, "%s", _SL_
);
687 if (tb
[XFRMA_TMPL
]) {
688 struct rtattr
*rta
= tb
[XFRMA_TMPL
];
689 xfrm_tmpl_print((struct xfrm_user_tmpl
*) RTA_DATA(rta
),
690 RTA_PAYLOAD(rta
), family
, fp
, prefix
);
693 if (tb
[XFRMA_COADDR
]) {
701 coa
= (xfrm_address_t
*)RTA_DATA(tb
[XFRMA_COADDR
]);
703 if (RTA_PAYLOAD(tb
[XFRMA_COADDR
]) < sizeof(*coa
)) {
704 fprintf(fp
, "(ERROR truncated)");
705 fprintf(fp
, "%s", _SL_
);
709 memset(abuf
, '\0', sizeof(abuf
));
711 rt_addr_n2a(family
, sizeof(*coa
), coa
,
712 abuf
, sizeof(abuf
)));
713 fprintf(fp
, "%s", _SL_
);
716 if (tb
[XFRMA_LASTUSED
]) {
721 fprintf(fp
, "lastused ");
723 if (RTA_PAYLOAD(tb
[XFRMA_LASTUSED
]) < sizeof(lastused
)) {
724 fprintf(fp
, "(ERROR truncated)");
725 fprintf(fp
, "%s", _SL_
);
729 lastused
= *(__u64
*)RTA_DATA(tb
[XFRMA_LASTUSED
]);
731 fprintf(fp
, "%s", strxf_time(lastused
));
732 fprintf(fp
, "%s", _SL_
);
736 static int xfrm_selector_iszero(struct xfrm_selector
*s
)
738 struct xfrm_selector s0
;
740 memset(&s0
, 0, sizeof(s0
));
742 return (memcmp(&s0
, s
, sizeof(s0
)) == 0);
745 void xfrm_state_info_print(struct xfrm_usersa_info
*xsinfo
,
746 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
749 char buf
[STRBUF_SIZE
];
750 int force_spi
= xfrm_xfrmproto_is_ipsec(xsinfo
->id
.proto
);
752 memset(buf
, '\0', sizeof(buf
));
754 xfrm_id_info_print(&xsinfo
->saddr
, &xsinfo
->id
, xsinfo
->mode
,
755 xsinfo
->reqid
, xsinfo
->family
, force_spi
, fp
,
759 STRBUF_CAT(buf
, prefix
);
760 STRBUF_CAT(buf
, "\t");
763 fprintf(fp
, "replay-window %u ", xsinfo
->replay_window
);
765 fprintf(fp
, "seq 0x%08u ", xsinfo
->seq
);
766 if (show_stats
> 0 || xsinfo
->flags
) {
767 __u8 flags
= xsinfo
->flags
;
769 fprintf(fp
, "flag ");
770 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOECN
, "noecn");
771 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_DECAP_DSCP
, "decap-dscp");
772 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOPMTUDISC
, "nopmtudisc");
773 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_WILDRECV
, "wildrecv");
775 fprintf(fp
, "%x", flags
);
778 fprintf(fp
, " (0x%s)", strxf_mask8(xsinfo
->flags
));
779 fprintf(fp
, "%s", _SL_
);
781 xfrm_xfrma_print(tb
, xsinfo
->family
, fp
, buf
);
783 if (!xfrm_selector_iszero(&xsinfo
->sel
)) {
784 char sbuf
[STRBUF_SIZE
];
786 memcpy(sbuf
, buf
, sizeof(sbuf
));
787 STRBUF_CAT(sbuf
, "sel ");
789 xfrm_selector_print(&xsinfo
->sel
, xsinfo
->family
, fp
, sbuf
);
792 if (show_stats
> 0) {
793 xfrm_lifetime_print(&xsinfo
->lft
, &xsinfo
->curlft
, fp
, buf
);
794 xfrm_stats_print(&xsinfo
->stats
, fp
, buf
);
798 void xfrm_policy_info_print(struct xfrm_userpolicy_info
*xpinfo
,
799 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
802 char buf
[STRBUF_SIZE
];
804 memset(buf
, '\0', sizeof(buf
));
806 xfrm_selector_print(&xpinfo
->sel
, preferred_family
, fp
, title
);
809 STRBUF_CAT(buf
, prefix
);
810 STRBUF_CAT(buf
, "\t");
814 switch (xpinfo
->dir
) {
818 case XFRM_POLICY_OUT
:
821 case XFRM_POLICY_FWD
:
825 fprintf(fp
, "%u", xpinfo
->dir
);
830 switch (xpinfo
->action
) {
831 case XFRM_POLICY_ALLOW
:
833 fprintf(fp
, "action allow ");
835 case XFRM_POLICY_BLOCK
:
836 fprintf(fp
, "action block ");
839 fprintf(fp
, "action %u ", xpinfo
->action
);
844 fprintf(fp
, "index %u ", xpinfo
->index
);
845 fprintf(fp
, "priority %u ", xpinfo
->priority
);
847 if (tb
[XFRMA_POLICY_TYPE
]) {
848 struct xfrm_userpolicy_type
*upt
;
850 fprintf(fp
, "ptype ");
852 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
))
853 fprintf(fp
, "(ERROR truncated)");
855 upt
= (struct xfrm_userpolicy_type
*)RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
856 fprintf(fp
, "%s ", strxf_ptype(upt
->type
));
860 fprintf(fp
, "share %s ", strxf_share(xpinfo
->share
));
862 if (show_stats
> 0 || xpinfo
->flags
) {
863 __u8 flags
= xpinfo
->flags
;
865 fprintf(fp
, "flag ");
866 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_LOCALOK
, "localok");
868 fprintf(fp
, "%x", flags
);
871 fprintf(fp
, " (0x%s)", strxf_mask8(xpinfo
->flags
));
872 fprintf(fp
, "%s", _SL_
);
875 xfrm_lifetime_print(&xpinfo
->lft
, &xpinfo
->curlft
, fp
, buf
);
877 xfrm_xfrma_print(tb
, xpinfo
->sel
.family
, fp
, buf
);
880 int xfrm_id_parse(xfrm_address_t
*saddr
, struct xfrm_id
*id
, __u16
*family
,
881 int loose
, int *argcp
, char ***argvp
)
884 char **argv
= *argvp
;
888 memset(&dst
, 0, sizeof(dst
));
889 memset(&src
, 0, sizeof(src
));
892 if (strcmp(*argv
, "src") == 0) {
895 get_prefix(&src
, *argv
, preferred_family
);
896 if (src
.family
== AF_UNSPEC
)
897 invarg("\"src\" address family is AF_UNSPEC", *argv
);
899 *family
= src
.family
;
901 memcpy(saddr
, &src
.data
, sizeof(*saddr
));
903 filter
.id_src_mask
= src
.bitlen
;
905 } else if (strcmp(*argv
, "dst") == 0) {
908 get_prefix(&dst
, *argv
, preferred_family
);
909 if (dst
.family
== AF_UNSPEC
)
910 invarg("\"dst\" address family is AF_UNSPEC", *argv
);
912 *family
= dst
.family
;
914 memcpy(&id
->daddr
, &dst
.data
, sizeof(id
->daddr
));
916 filter
.id_dst_mask
= dst
.bitlen
;
918 } else if (strcmp(*argv
, "proto") == 0) {
923 ret
= xfrm_xfrmproto_getbyname(*argv
);
925 invarg("\"XFRM_PROTO\" is invalid", *argv
);
927 id
->proto
= (__u8
)ret
;
929 filter
.id_proto_mask
= XFRM_FILTER_MASK_FULL
;
931 } else if (strcmp(*argv
, "spi") == 0) {
935 if (get_u32(&spi
, *argv
, 0))
936 invarg("\"SPI\" is invalid", *argv
);
941 filter
.id_spi_mask
= XFRM_FILTER_MASK_FULL
;
944 PREV_ARG(); /* back track */
953 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
954 invarg("the same address family is required between \"src\" and \"dst\"", *argv
);
956 if (loose
== 0 && id
->proto
== 0)
957 missarg("XFRM_PROTO");
967 int xfrm_mode_parse(__u8
*mode
, int *argcp
, char ***argvp
)
970 char **argv
= *argvp
;
972 if (matches(*argv
, "transport") == 0)
973 *mode
= XFRM_MODE_TRANSPORT
;
974 else if (matches(*argv
, "tunnel") == 0)
975 *mode
= XFRM_MODE_TUNNEL
;
976 else if (matches(*argv
, "ro") == 0)
977 *mode
= XFRM_MODE_ROUTEOPTIMIZATION
;
978 else if (matches(*argv
, "in_trigger") == 0)
979 *mode
= XFRM_MODE_IN_TRIGGER
;
980 else if (matches(*argv
, "beet") == 0)
981 *mode
= XFRM_MODE_BEET
;
983 invarg("\"MODE\" is invalid", *argv
);
991 int xfrm_encap_type_parse(__u16
*type
, int *argcp
, char ***argvp
)
994 char **argv
= *argvp
;
996 if (strcmp(*argv
, "espinudp-nonike") == 0)
998 else if (strcmp(*argv
, "espinudp") == 0)
1001 invarg("\"ENCAP-TYPE\" is invalid", *argv
);
1009 /* NOTE: reqid is used by host-byte order */
1010 int xfrm_reqid_parse(__u32
*reqid
, int *argcp
, char ***argvp
)
1013 char **argv
= *argvp
;
1015 if (get_u32(reqid
, *argv
, 0))
1016 invarg("\"REQID\" is invalid", *argv
);
1024 static int xfrm_selector_upspec_parse(struct xfrm_selector
*sel
,
1025 int *argcp
, char ***argvp
)
1028 char **argv
= *argvp
;
1029 char *sportp
= NULL
;
1030 char *dportp
= NULL
;
1035 if (strcmp(*argv
, "proto") == 0) {
1040 if (strcmp(*argv
, "any") == 0)
1043 struct protoent
*pp
;
1044 pp
= getprotobyname(*argv
);
1046 upspec
= pp
->p_proto
;
1048 if (get_u8(&upspec
, *argv
, 0))
1049 invarg("\"PROTO\" is invalid", *argv
);
1052 sel
->proto
= upspec
;
1054 filter
.upspec_proto_mask
= XFRM_FILTER_MASK_FULL
;
1056 } else if (strcmp(*argv
, "sport") == 0) {
1061 if (get_u16(&sel
->sport
, *argv
, 0))
1062 invarg("\"PORT\" is invalid", *argv
);
1063 sel
->sport
= htons(sel
->sport
);
1065 sel
->sport_mask
= ~((__u16
)0);
1067 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1069 } else if (strcmp(*argv
, "dport") == 0) {
1074 if (get_u16(&sel
->dport
, *argv
, 0))
1075 invarg("\"PORT\" is invalid", *argv
);
1076 sel
->dport
= htons(sel
->dport
);
1078 sel
->dport_mask
= ~((__u16
)0);
1080 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1082 } else if (strcmp(*argv
, "type") == 0) {
1087 if (get_u16(&sel
->sport
, *argv
, 0) ||
1088 (sel
->sport
& ~((__u16
)0xff)))
1089 invarg("\"type\" value is invalid", *argv
);
1090 sel
->sport
= htons(sel
->sport
);
1091 sel
->sport_mask
= ~((__u16
)0);
1093 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1096 } else if (strcmp(*argv
, "code") == 0) {
1101 if (get_u16(&sel
->dport
, *argv
, 0) ||
1102 (sel
->dport
& ~((__u16
)0xff)))
1103 invarg("\"code\" value is invalid", *argv
);
1104 sel
->dport
= htons(sel
->dport
);
1105 sel
->dport_mask
= ~((__u16
)0);
1107 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1110 PREV_ARG(); /* back track */
1120 if (sportp
|| dportp
) {
1121 switch (sel
->proto
) {
1128 fprintf(stderr
, "\"sport\" and \"dport\" are invalid with proto=%s\n", strxf_proto(sel
->proto
));
1132 if (typep
|| codep
) {
1133 switch (sel
->proto
) {
1135 case IPPROTO_ICMPV6
:
1139 fprintf(stderr
, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel
->proto
));
1150 int xfrm_selector_parse(struct xfrm_selector
*sel
, int *argcp
, char ***argvp
)
1153 char **argv
= *argvp
;
1156 char *upspecp
= NULL
;
1158 memset(&dst
, 0, sizeof(dst
));
1159 memset(&src
, 0, sizeof(src
));
1162 if (strcmp(*argv
, "src") == 0) {
1165 get_prefix(&src
, *argv
, preferred_family
);
1166 if (src
.family
== AF_UNSPEC
)
1167 invarg("\"src\" address family is AF_UNSPEC", *argv
);
1168 sel
->family
= src
.family
;
1170 memcpy(&sel
->saddr
, &src
.data
, sizeof(sel
->saddr
));
1171 sel
->prefixlen_s
= src
.bitlen
;
1173 filter
.sel_src_mask
= src
.bitlen
;
1175 } else if (strcmp(*argv
, "dst") == 0) {
1178 get_prefix(&dst
, *argv
, preferred_family
);
1179 if (dst
.family
== AF_UNSPEC
)
1180 invarg("\"dst\" address family is AF_UNSPEC", *argv
);
1181 sel
->family
= dst
.family
;
1183 memcpy(&sel
->daddr
, &dst
.data
, sizeof(sel
->daddr
));
1184 sel
->prefixlen_d
= dst
.bitlen
;
1186 filter
.sel_dst_mask
= dst
.bitlen
;
1188 } else if (strcmp(*argv
, "dev") == 0) {
1193 if (strcmp(*argv
, "none") == 0)
1196 ifindex
= ll_name_to_index(*argv
);
1198 invarg("\"DEV\" is invalid", *argv
);
1200 sel
->ifindex
= ifindex
;
1202 filter
.sel_dev_mask
= XFRM_FILTER_MASK_FULL
;
1206 PREV_ARG(); /* back track */
1210 xfrm_selector_upspec_parse(sel
, &argc
, &argv
);
1220 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1221 invarg("the same address family is required between \"src\" and \"dst\"", *argv
);
1224 missarg("SELECTOR");
1232 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg
*lft
,
1233 int *argcp
, char ***argvp
)
1236 char **argv
= *argvp
;
1239 if (strcmp(*argv
, "time-soft") == 0) {
1241 ret
= get_u64(&lft
->soft_add_expires_seconds
, *argv
, 0);
1243 invarg("\"time-soft\" value is invalid", *argv
);
1244 } else if (strcmp(*argv
, "time-hard") == 0) {
1246 ret
= get_u64(&lft
->hard_add_expires_seconds
, *argv
, 0);
1248 invarg("\"time-hard\" value is invalid", *argv
);
1249 } else if (strcmp(*argv
, "time-use-soft") == 0) {
1251 ret
= get_u64(&lft
->soft_use_expires_seconds
, *argv
, 0);
1253 invarg("\"time-use-soft\" value is invalid", *argv
);
1254 } else if (strcmp(*argv
, "time-use-hard") == 0) {
1256 ret
= get_u64(&lft
->hard_use_expires_seconds
, *argv
, 0);
1258 invarg("\"time-use-hard\" value is invalid", *argv
);
1259 } else if (strcmp(*argv
, "byte-soft") == 0) {
1261 ret
= get_u64(&lft
->soft_byte_limit
, *argv
, 0);
1263 invarg("\"byte-soft\" value is invalid", *argv
);
1264 } else if (strcmp(*argv
, "byte-hard") == 0) {
1266 ret
= get_u64(&lft
->hard_byte_limit
, *argv
, 0);
1268 invarg("\"byte-hard\" value is invalid", *argv
);
1269 } else if (strcmp(*argv
, "packet-soft") == 0) {
1271 ret
= get_u64(&lft
->soft_packet_limit
, *argv
, 0);
1273 invarg("\"packet-soft\" value is invalid", *argv
);
1274 } else if (strcmp(*argv
, "packet-hard") == 0) {
1276 ret
= get_u64(&lft
->hard_packet_limit
, *argv
, 0);
1278 invarg("\"packet-hard\" value is invalid", *argv
);
1280 invarg("\"LIMIT\" is invalid", *argv
);
1288 int do_xfrm(int argc
, char **argv
)
1290 memset(&filter
, 0, sizeof(filter
));
1295 if (matches(*argv
, "state") == 0 ||
1296 matches(*argv
, "sa") == 0)
1297 return do_xfrm_state(argc
-1, argv
+1);
1298 else if (matches(*argv
, "policy") == 0)
1299 return do_xfrm_policy(argc
-1, argv
+1);
1300 else if (matches(*argv
, "monitor") == 0)
1301 return do_xfrm_monitor(argc
-1, argv
+1);
1302 else if (matches(*argv
, "help") == 0) {
1304 fprintf(stderr
, "xfrm Object \"%s\" is unknown.\n", *argv
);