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, see <http://www.gnu.org/licenses>.
20 * based on ip.c, iproute.c
24 * 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/udp.h>
41 #include "ip_common.h"
43 #define STRBUF_SIZE (128)
45 struct xfrm_filter filter
;
47 static void usage(void) __attribute__((noreturn
));
49 static void usage(void)
52 "Usage: ip xfrm XFRM-OBJECT { COMMAND | help }\n"
53 "where XFRM-OBJECT := state | policy | monitor\n");
57 /* This is based on utils.c(inet_addr_match) */
58 int xfrm_addr_match(xfrm_address_t
*x1
, xfrm_address_t
*x2
, int bits
)
60 __u32
*a1
= (__u32
*)x1
;
61 __u32
*a2
= (__u32
*)x2
;
62 int words
= bits
>> 0x05;
67 if (memcmp(a1
, a2
, words
<< 2))
77 mask
= htonl((0xffffffff) << (0x20 - bits
));
86 int xfrm_xfrmproto_is_ipsec(__u8 proto
)
88 return (proto
== IPPROTO_ESP
||
89 proto
== IPPROTO_AH
||
90 proto
== IPPROTO_COMP
);
93 int xfrm_xfrmproto_is_ro(__u8 proto
)
95 return (proto
== IPPROTO_ROUTING
||
96 proto
== IPPROTO_DSTOPTS
);
104 static const struct typeent xfrmproto_types
[] = {
105 { "esp", IPPROTO_ESP
}, { "ah", IPPROTO_AH
}, { "comp", IPPROTO_COMP
},
106 { "route2", IPPROTO_ROUTING
}, { "hao", IPPROTO_DSTOPTS
},
107 { "ipsec-any", IPSEC_PROTO_ANY
},
111 int xfrm_xfrmproto_getbyname(char *name
)
116 const struct typeent
*t
= &xfrmproto_types
[i
];
118 if (!t
->t_name
|| t
->t_type
== -1)
121 if (strcmp(t
->t_name
, name
) == 0)
128 const char *strxf_xfrmproto(__u8 proto
)
134 const struct typeent
*t
= &xfrmproto_types
[i
];
136 if (!t
->t_name
|| t
->t_type
== -1)
139 if (t
->t_type
== proto
)
143 sprintf(str
, "%u", proto
);
147 static const struct typeent algo_types
[] = {
148 { "enc", XFRMA_ALG_CRYPT
}, { "auth", XFRMA_ALG_AUTH
},
149 { "comp", XFRMA_ALG_COMP
}, { "aead", XFRMA_ALG_AEAD
},
150 { "auth-trunc", XFRMA_ALG_AUTH_TRUNC
},
154 int xfrm_algotype_getbyname(char *name
)
159 const struct typeent
*t
= &algo_types
[i
];
161 if (!t
->t_name
|| t
->t_type
== -1)
164 if (strcmp(t
->t_name
, name
) == 0)
171 const char *strxf_algotype(int type
)
177 const struct typeent
*t
= &algo_types
[i
];
179 if (!t
->t_name
|| t
->t_type
== -1)
182 if (t
->t_type
== type
)
186 sprintf(str
, "%d", type
);
190 static 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 static 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 const char *strxf_ptype(__u8 ptype
)
260 case XFRM_POLICY_TYPE_MAIN
:
263 case XFRM_POLICY_TYPE_SUB
:
267 sprintf(str
, "%u", ptype
);
274 static void xfrm_id_info_print(xfrm_address_t
*saddr
, struct xfrm_id
*id
,
275 __u8 mode
, __u32 reqid
, __u16 family
, int force_spi
,
276 FILE *fp
, const char *prefix
, const char *title
)
281 fprintf(fp
, "src %s ", rt_addr_n2a(family
, sizeof(*saddr
), saddr
));
282 fprintf(fp
, "dst %s", rt_addr_n2a(family
, sizeof(id
->daddr
), &id
->daddr
));
283 fprintf(fp
, "%s", _SL_
);
289 fprintf(fp
, "proto %s ", strxf_xfrmproto(id
->proto
));
291 if (show_stats
> 0 || force_spi
|| id
->spi
) {
292 __u32 spi
= ntohl(id
->spi
);
294 fprintf(fp
, "spi 0x%08x", spi
);
296 fprintf(fp
, "(%u)", spi
);
300 fprintf(fp
, "reqid %u", reqid
);
302 fprintf(fp
, "(0x%08x)", reqid
);
305 fprintf(fp
, "mode ");
307 case XFRM_MODE_TRANSPORT
:
308 fprintf(fp
, "transport");
310 case XFRM_MODE_TUNNEL
:
311 fprintf(fp
, "tunnel");
313 case XFRM_MODE_ROUTEOPTIMIZATION
:
316 case XFRM_MODE_IN_TRIGGER
:
317 fprintf(fp
, "in_trigger");
323 fprintf(fp
, "%u", mode
);
326 fprintf(fp
, "%s", _SL_
);
329 static const char *strxf_limit(__u64 limit
)
333 if (limit
== XFRM_INF
)
334 strcpy(str
, "(INF)");
336 sprintf(str
, "%llu", (unsigned long long) limit
);
341 static void xfrm_stats_print(struct xfrm_stats
*s
, FILE *fp
,
346 fprintf(fp
, "stats:%s", _SL_
);
350 fprintf(fp
, " replay-window %u replay %u failed %u%s",
351 s
->replay_window
, s
->replay
, s
->integrity_failed
, _SL_
);
354 static const char *strxf_time(__u64 time
)
364 /* XXX: treat time in the same manner of kernel's
365 * net/xfrm/xfrm_{user,state}.c
370 strftime(str
, sizeof(str
), "%Y-%m-%d %T", tp
);
376 static void xfrm_lifetime_print(struct xfrm_lifetime_cfg
*cfg
,
377 struct xfrm_lifetime_cur
*cur
,
378 FILE *fp
, const char *prefix
)
383 fprintf(fp
, "lifetime config:%s", _SL_
);
387 fprintf(fp
, " limit: soft %s(bytes),",
388 strxf_limit(cfg
->soft_byte_limit
));
389 fprintf(fp
, " hard %s(bytes)%s",
390 strxf_limit(cfg
->hard_byte_limit
), _SL_
);
394 fprintf(fp
, " limit: soft %s(packets),",
395 strxf_limit(cfg
->soft_packet_limit
));
396 fprintf(fp
, " hard %s(packets)%s",
397 strxf_limit(cfg
->hard_packet_limit
), _SL_
);
401 fprintf(fp
, " expire add: soft %llu(sec), hard %llu(sec)%s",
402 (unsigned long long) cfg
->soft_add_expires_seconds
,
403 (unsigned long long) cfg
->hard_add_expires_seconds
,
408 fprintf(fp
, " expire use: soft %llu(sec), hard %llu(sec)%s",
409 (unsigned long long) cfg
->soft_use_expires_seconds
,
410 (unsigned long long) cfg
->hard_use_expires_seconds
,
416 fprintf(fp
, "lifetime current:%s", _SL_
);
420 fprintf(fp
, " %llu(bytes), %llu(packets)%s",
421 (unsigned long long) cur
->bytes
,
422 (unsigned long long) cur
->packets
,
427 fprintf(fp
, " add %s ", strxf_time(cur
->add_time
));
428 fprintf(fp
, "use %s%s", strxf_time(cur
->use_time
), _SL_
);
432 void xfrm_selector_print(struct xfrm_selector
*sel
, __u16 family
,
433 FILE *fp
, const char *prefix
)
441 f
= preferred_family
;
446 fprintf(fp
, "src %s/%u ",
447 rt_addr_n2a(f
, sizeof(sel
->saddr
), &sel
->saddr
),
450 fprintf(fp
, "dst %s/%u ",
451 rt_addr_n2a(f
, sizeof(sel
->daddr
), &sel
->daddr
),
455 fprintf(fp
, "proto %s ", strxf_proto(sel
->proto
));
456 switch (sel
->proto
) {
463 fprintf(fp
, "sport %u ", ntohs(sel
->sport
));
465 fprintf(fp
, "dport %u ", ntohs(sel
->dport
));
469 /* type/code is stored at sport/dport in selector */
471 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
473 fprintf(fp
, "code %u ", ntohs(sel
->dport
));
476 if (sel
->sport_mask
|| sel
->dport_mask
)
477 fprintf(fp
, "key %u ",
478 (((__u32
)ntohs(sel
->sport
)) << 16) +
483 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
484 if (sel
->dport_mask
) {
486 fprintf(fp
, "(dport) 0x%.4x ", sel
->dport
);
491 if (sel
->ifindex
> 0)
492 fprintf(fp
, "dev %s ", ll_index_to_name(sel
->ifindex
));
495 fprintf(fp
, "uid %u", sel
->user
);
497 fprintf(fp
, "%s", _SL_
);
500 static void __xfrm_algo_print(struct xfrm_algo
*algo
, int type
, int len
,
501 FILE *fp
, const char *prefix
, int newline
,
510 fprintf(fp
, "%s ", strxf_algotype(type
));
512 if (len
< sizeof(*algo
)) {
513 fprintf(fp
, "(ERROR truncated)");
516 len
-= sizeof(*algo
);
518 fprintf(fp
, "%s ", algo
->alg_name
);
520 keylen
= algo
->alg_key_len
/ 8;
522 fprintf(fp
, "(ERROR truncated)");
527 fprintf(fp
, "<<Keys hidden>>");
528 else if (keylen
> 0) {
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
);
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
, bool nokeys
)
545 return __xfrm_algo_print(algo
, type
, len
, fp
, prefix
, 1, nokeys
);
548 static void xfrm_aead_print(struct xfrm_algo_aead
*algo
, int len
,
549 FILE *fp
, const char *prefix
, bool nokeys
)
551 struct xfrm_algo
*base_algo
= alloca(sizeof(*base_algo
) + algo
->alg_key_len
/ 8);
553 memcpy(base_algo
->alg_name
, algo
->alg_name
, sizeof(base_algo
->alg_name
));
554 base_algo
->alg_key_len
= algo
->alg_key_len
;
555 memcpy(base_algo
->alg_key
, algo
->alg_key
, algo
->alg_key_len
/ 8);
557 __xfrm_algo_print(base_algo
, XFRMA_ALG_AEAD
, len
, fp
, prefix
, 0,
560 fprintf(fp
, " %d", algo
->alg_icv_len
);
562 fprintf(fp
, "%s", _SL_
);
565 static void xfrm_auth_trunc_print(struct xfrm_algo_auth
*algo
, int len
,
566 FILE *fp
, const char *prefix
, bool nokeys
)
568 struct xfrm_algo
*base_algo
= alloca(sizeof(*base_algo
) + algo
->alg_key_len
/ 8);
570 memcpy(base_algo
->alg_name
, algo
->alg_name
, sizeof(base_algo
->alg_name
));
571 base_algo
->alg_key_len
= algo
->alg_key_len
;
572 memcpy(base_algo
->alg_key
, algo
->alg_key
, algo
->alg_key_len
/ 8);
574 __xfrm_algo_print(base_algo
, XFRMA_ALG_AUTH_TRUNC
, len
, fp
, prefix
, 0,
577 fprintf(fp
, " %d", algo
->alg_trunc_len
);
579 fprintf(fp
, "%s", _SL_
);
582 static void xfrm_tmpl_print(struct xfrm_user_tmpl
*tmpls
, int len
,
583 FILE *fp
, const char *prefix
)
585 int ntmpls
= len
/ sizeof(struct xfrm_user_tmpl
);
591 fprintf(fp
, "(ERROR \"tmpl\" truncated)");
592 fprintf(fp
, "%s", _SL_
);
596 for (i
= 0; i
< ntmpls
; i
++) {
597 struct xfrm_user_tmpl
*tmpl
= &tmpls
[i
];
602 xfrm_id_info_print(&tmpl
->saddr
, &tmpl
->id
, tmpl
->mode
,
603 tmpl
->reqid
, tmpl
->family
, 0, fp
, prefix
, "tmpl ");
605 if (show_stats
> 0 || tmpl
->optional
) {
609 switch (tmpl
->optional
) {
612 fprintf(fp
, "level required ");
615 fprintf(fp
, "level use ");
618 fprintf(fp
, "level %u ", tmpl
->optional
);
623 fprintf(fp
, "share %s ", strxf_share(tmpl
->share
));
625 fprintf(fp
, "%s", _SL_
);
628 if (show_stats
> 0) {
632 fprintf(fp
, "%s-mask %s ",
633 strxf_algotype(XFRMA_ALG_CRYPT
),
634 strxf_mask32(tmpl
->ealgos
));
635 fprintf(fp
, "%s-mask %s ",
636 strxf_algotype(XFRMA_ALG_AUTH
),
637 strxf_mask32(tmpl
->aalgos
));
638 fprintf(fp
, "%s-mask %s",
639 strxf_algotype(XFRMA_ALG_COMP
),
640 strxf_mask32(tmpl
->calgos
));
642 fprintf(fp
, "%s", _SL_
);
647 static void xfrm_output_mark_print(struct rtattr
*tb
[], FILE *fp
)
649 __u32 output_mark
= rta_getattr_u32(tb
[XFRMA_OUTPUT_MARK
]);
651 fprintf(fp
, "output-mark 0x%x", output_mark
);
652 if (tb
[XFRMA_SET_MARK_MASK
]) {
653 __u32 mask
= rta_getattr_u32(tb
[XFRMA_SET_MARK_MASK
]);
654 fprintf(fp
, "/0x%x", mask
);
658 int xfrm_parse_mark(struct xfrm_mark
*mark
, int *argcp
, char ***argvp
)
661 char **argv
= *argvp
;
664 if (get_u32(&mark
->v
, *argv
, 0)) {
665 invarg("MARK value is invalid\n", *argv
);
669 else { /* last entry on parse line */
670 mark
->m
= 0xffffffff;
674 if (strcmp(*argv
, "mask") == 0) {
676 if (get_u32(&mark
->m
, *argv
, 0)) {
677 invarg("MASK value is invalid\n", *argv
);
680 mark
->m
= 0xffffffff;
691 void xfrm_xfrma_print(struct rtattr
*tb
[], __u16 family
,
692 FILE *fp
, const char *prefix
, bool nokeys
)
694 if (tb
[XFRMA_MARK
]) {
695 struct rtattr
*rta
= tb
[XFRMA_MARK
];
696 struct xfrm_mark
*m
= RTA_DATA(rta
);
698 fprintf(fp
, "\tmark %#x/%#x ", m
->v
, m
->m
);
700 if (tb
[XFRMA_OUTPUT_MARK
])
701 xfrm_output_mark_print(tb
, fp
);
702 fprintf(fp
, "%s", _SL_
);
703 } else if (tb
[XFRMA_OUTPUT_MARK
]) {
706 xfrm_output_mark_print(tb
, fp
);
707 fprintf(fp
, "%s", _SL_
);
710 if (tb
[XFRMA_ALG_AUTH
] && !tb
[XFRMA_ALG_AUTH_TRUNC
]) {
711 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH
];
713 xfrm_algo_print(RTA_DATA(rta
), XFRMA_ALG_AUTH
, RTA_PAYLOAD(rta
),
717 if (tb
[XFRMA_ALG_AUTH_TRUNC
]) {
718 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH_TRUNC
];
720 xfrm_auth_trunc_print(RTA_DATA(rta
), RTA_PAYLOAD(rta
), fp
,
724 if (tb
[XFRMA_ALG_AEAD
]) {
725 struct rtattr
*rta
= tb
[XFRMA_ALG_AEAD
];
727 xfrm_aead_print(RTA_DATA(rta
), RTA_PAYLOAD(rta
), fp
, prefix
,
731 if (tb
[XFRMA_ALG_CRYPT
]) {
732 struct rtattr
*rta
= tb
[XFRMA_ALG_CRYPT
];
734 xfrm_algo_print(RTA_DATA(rta
), XFRMA_ALG_CRYPT
,
735 RTA_PAYLOAD(rta
), fp
, prefix
, nokeys
);
738 if (tb
[XFRMA_ALG_COMP
]) {
739 struct rtattr
*rta
= tb
[XFRMA_ALG_COMP
];
741 xfrm_algo_print(RTA_DATA(rta
), XFRMA_ALG_COMP
, RTA_PAYLOAD(rta
),
745 if (tb
[XFRMA_ENCAP
]) {
746 struct xfrm_encap_tmpl
*e
;
750 fprintf(fp
, "encap ");
752 if (RTA_PAYLOAD(tb
[XFRMA_ENCAP
]) < sizeof(*e
)) {
753 fprintf(fp
, "(ERROR truncated)");
754 fprintf(fp
, "%s", _SL_
);
757 e
= RTA_DATA(tb
[XFRMA_ENCAP
]);
759 fprintf(fp
, "type ");
760 switch (e
->encap_type
) {
761 case UDP_ENCAP_ESPINUDP_NON_IKE
:
762 fprintf(fp
, "espinudp-nonike ");
764 case UDP_ENCAP_ESPINUDP
:
765 fprintf(fp
, "espinudp ");
767 case TCP_ENCAP_ESPINTCP
:
768 fprintf(fp
, "espintcp ");
771 fprintf(fp
, "%u ", e
->encap_type
);
774 fprintf(fp
, "sport %u ", ntohs(e
->encap_sport
));
775 fprintf(fp
, "dport %u ", ntohs(e
->encap_dport
));
777 fprintf(fp
, "addr %s",
778 rt_addr_n2a(family
, sizeof(e
->encap_oa
), &e
->encap_oa
));
779 fprintf(fp
, "%s", _SL_
);
782 if (tb
[XFRMA_TMPL
]) {
783 struct rtattr
*rta
= tb
[XFRMA_TMPL
];
785 xfrm_tmpl_print(RTA_DATA(rta
),
786 RTA_PAYLOAD(rta
), fp
, prefix
);
789 if (tb
[XFRMA_COADDR
]) {
790 const xfrm_address_t
*coa
;
796 coa
= RTA_DATA(tb
[XFRMA_COADDR
]);
797 if (RTA_PAYLOAD(tb
[XFRMA_COADDR
]) < sizeof(*coa
)) {
798 fprintf(fp
, "(ERROR truncated)");
799 fprintf(fp
, "%s", _SL_
);
804 rt_addr_n2a(family
, sizeof(*coa
), coa
));
805 fprintf(fp
, "%s", _SL_
);
808 if (tb
[XFRMA_LASTUSED
]) {
813 fprintf(fp
, "lastused ");
815 if (RTA_PAYLOAD(tb
[XFRMA_LASTUSED
]) < sizeof(lastused
)) {
816 fprintf(fp
, "(ERROR truncated)");
817 fprintf(fp
, "%s", _SL_
);
821 lastused
= rta_getattr_u64(tb
[XFRMA_LASTUSED
]);
823 fprintf(fp
, "%s", strxf_time(lastused
));
824 fprintf(fp
, "%s", _SL_
);
827 if (tb
[XFRMA_REPLAY_VAL
]) {
828 struct xfrm_replay_state
*replay
;
832 fprintf(fp
, "anti-replay context: ");
834 if (RTA_PAYLOAD(tb
[XFRMA_REPLAY_VAL
]) < sizeof(*replay
)) {
835 fprintf(fp
, "(ERROR truncated)");
836 fprintf(fp
, "%s", _SL_
);
840 replay
= RTA_DATA(tb
[XFRMA_REPLAY_VAL
]);
841 fprintf(fp
, "seq 0x%x, oseq 0x%x, bitmap 0x%08x",
842 replay
->seq
, replay
->oseq
, replay
->bitmap
);
843 fprintf(fp
, "%s", _SL_
);
846 if (tb
[XFRMA_REPLAY_ESN_VAL
]) {
847 struct xfrm_replay_state_esn
*replay
;
852 fprintf(fp
, "anti-replay esn context:");
854 if (RTA_PAYLOAD(tb
[XFRMA_REPLAY_ESN_VAL
]) < sizeof(*replay
)) {
855 fprintf(fp
, "(ERROR truncated)");
856 fprintf(fp
, "%s", _SL_
);
859 fprintf(fp
, "%s", _SL_
);
861 replay
= RTA_DATA(tb
[XFRMA_REPLAY_ESN_VAL
]);
864 fprintf(fp
, " seq-hi 0x%x, seq 0x%x, oseq-hi 0x%0x, oseq 0x%0x",
865 replay
->seq_hi
, replay
->seq
, replay
->oseq_hi
,
867 fprintf(fp
, "%s", _SL_
);
870 fprintf(fp
, " replay_window %u, bitmap-length %u",
871 replay
->replay_window
, replay
->bmp_len
);
872 for (i
= replay
->bmp_len
, j
= 0; i
; i
--) {
874 fprintf(fp
, "%s", _SL_
);
879 fprintf(fp
, "%08x ", replay
->bmp
[i
- 1]);
881 fprintf(fp
, "%s", _SL_
);
883 if (tb
[XFRMA_OFFLOAD_DEV
]) {
884 struct xfrm_user_offload
*xuo
;
888 fprintf(fp
, "crypto offload parameters: ");
890 if (RTA_PAYLOAD(tb
[XFRMA_OFFLOAD_DEV
]) < sizeof(*xuo
)) {
891 fprintf(fp
, "(ERROR truncated)");
892 fprintf(fp
, "%s", _SL_
);
896 xuo
= (struct xfrm_user_offload
*)
897 RTA_DATA(tb
[XFRMA_OFFLOAD_DEV
]);
898 fprintf(fp
, "dev %s dir %s", ll_index_to_name(xuo
->ifindex
),
899 (xuo
->flags
& XFRM_OFFLOAD_INBOUND
) ? "in" : "out");
900 fprintf(fp
, "%s", _SL_
);
902 if (tb
[XFRMA_IF_ID
]) {
903 __u32 if_id
= rta_getattr_u32(tb
[XFRMA_IF_ID
]);
907 fprintf(fp
, "if_id %#x", if_id
);
908 fprintf(fp
, "%s", _SL_
);
912 static int xfrm_selector_iszero(struct xfrm_selector
*s
)
914 struct xfrm_selector s0
= {};
916 return (memcmp(&s0
, s
, sizeof(s0
)) == 0);
919 void xfrm_state_info_print(struct xfrm_usersa_info
*xsinfo
,
920 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
921 const char *title
, bool nokeys
)
923 char buf
[STRBUF_SIZE
] = {};
924 int force_spi
= xfrm_xfrmproto_is_ipsec(xsinfo
->id
.proto
);
926 xfrm_id_info_print(&xsinfo
->saddr
, &xsinfo
->id
, xsinfo
->mode
,
927 xsinfo
->reqid
, xsinfo
->family
, force_spi
, fp
,
931 strlcat(buf
, prefix
, sizeof(buf
));
932 strlcat(buf
, "\t", sizeof(buf
));
935 fprintf(fp
, "replay-window %u ", xsinfo
->replay_window
);
937 fprintf(fp
, "seq 0x%08u ", xsinfo
->seq
);
938 if (show_stats
> 0 || xsinfo
->flags
) {
939 __u8 flags
= xsinfo
->flags
;
941 fprintf(fp
, "flag ");
942 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOECN
, "noecn");
943 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_DECAP_DSCP
, "decap-dscp");
944 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOPMTUDISC
, "nopmtudisc");
945 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_WILDRECV
, "wildrecv");
946 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ICMP
, "icmp");
947 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_AF_UNSPEC
, "af-unspec");
948 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ALIGN4
, "align4");
949 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ESN
, "esn");
951 fprintf(fp
, "%x", flags
);
953 if (show_stats
> 0 && tb
[XFRMA_SA_EXTRA_FLAGS
]) {
954 __u32 extra_flags
= rta_getattr_u32(tb
[XFRMA_SA_EXTRA_FLAGS
]);
956 fprintf(fp
, "extra_flag ");
957 XFRM_FLAG_PRINT(fp
, extra_flags
,
958 XFRM_SA_XFLAG_DONT_ENCAP_DSCP
,
960 XFRM_FLAG_PRINT(fp
, extra_flags
,
961 XFRM_SA_XFLAG_OSEQ_MAY_WRAP
,
964 fprintf(fp
, "%x", extra_flags
);
967 fprintf(fp
, " (0x%s)", strxf_mask8(xsinfo
->flags
));
968 fprintf(fp
, "%s", _SL_
);
970 xfrm_xfrma_print(tb
, xsinfo
->family
, fp
, buf
, nokeys
);
972 if (!xfrm_selector_iszero(&xsinfo
->sel
)) {
973 char sbuf
[STRBUF_SIZE
];
975 memcpy(sbuf
, buf
, sizeof(sbuf
));
976 strlcat(sbuf
, "sel ", sizeof(sbuf
));
978 xfrm_selector_print(&xsinfo
->sel
, xsinfo
->family
, fp
, sbuf
);
981 if (show_stats
> 0) {
982 xfrm_lifetime_print(&xsinfo
->lft
, &xsinfo
->curlft
, fp
, buf
);
983 xfrm_stats_print(&xsinfo
->stats
, fp
, buf
);
986 if (tb
[XFRMA_SEC_CTX
]) {
987 struct xfrm_user_sec_ctx
*sctx
;
989 fprintf(fp
, "\tsecurity context ");
991 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
992 fprintf(fp
, "(ERROR truncated)");
994 sctx
= RTA_DATA(tb
[XFRMA_SEC_CTX
]);
996 fprintf(fp
, "%s %s", (char *)(sctx
+ 1), _SL_
);
1001 void xfrm_policy_info_print(struct xfrm_userpolicy_info
*xpinfo
,
1002 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
1005 char buf
[STRBUF_SIZE
] = {};
1007 xfrm_selector_print(&xpinfo
->sel
, preferred_family
, fp
, title
);
1009 if (tb
[XFRMA_SEC_CTX
]) {
1010 struct xfrm_user_sec_ctx
*sctx
;
1012 fprintf(fp
, "\tsecurity context ");
1014 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
1015 fprintf(fp
, "(ERROR truncated)");
1017 sctx
= RTA_DATA(tb
[XFRMA_SEC_CTX
]);
1019 fprintf(fp
, "%s ", (char *)(sctx
+ 1));
1020 fprintf(fp
, "%s", _SL_
);
1024 strlcat(buf
, prefix
, sizeof(buf
));
1025 strlcat(buf
, "\t", sizeof(buf
));
1028 if (xpinfo
->dir
>= XFRM_POLICY_MAX
) {
1029 xpinfo
->dir
-= XFRM_POLICY_MAX
;
1030 fprintf(fp
, "socket ");
1032 fprintf(fp
, "dir ");
1034 switch (xpinfo
->dir
) {
1035 case XFRM_POLICY_IN
:
1038 case XFRM_POLICY_OUT
:
1041 case XFRM_POLICY_FWD
:
1045 fprintf(fp
, "%u", xpinfo
->dir
);
1050 switch (xpinfo
->action
) {
1051 case XFRM_POLICY_ALLOW
:
1053 fprintf(fp
, "action allow ");
1055 case XFRM_POLICY_BLOCK
:
1056 fprintf(fp
, "action block ");
1059 fprintf(fp
, "action %u ", xpinfo
->action
);
1064 fprintf(fp
, "index %u ", xpinfo
->index
);
1065 fprintf(fp
, "priority %u ", xpinfo
->priority
);
1067 if (tb
[XFRMA_POLICY_TYPE
]) {
1068 struct xfrm_userpolicy_type
*upt
;
1070 fprintf(fp
, "ptype ");
1072 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
))
1073 fprintf(fp
, "(ERROR truncated)");
1075 upt
= RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
1076 fprintf(fp
, "%s ", strxf_ptype(upt
->type
));
1080 fprintf(fp
, "share %s ", strxf_share(xpinfo
->share
));
1082 if (show_stats
> 0 || xpinfo
->flags
) {
1083 __u8 flags
= xpinfo
->flags
;
1085 fprintf(fp
, "flag ");
1086 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_LOCALOK
, "localok");
1087 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_ICMP
, "icmp");
1089 fprintf(fp
, "%x", flags
);
1092 fprintf(fp
, " (0x%s)", strxf_mask8(xpinfo
->flags
));
1093 fprintf(fp
, "%s", _SL_
);
1096 xfrm_lifetime_print(&xpinfo
->lft
, &xpinfo
->curlft
, fp
, buf
);
1098 xfrm_xfrma_print(tb
, xpinfo
->sel
.family
, fp
, buf
, false);
1101 int xfrm_id_parse(xfrm_address_t
*saddr
, struct xfrm_id
*id
, __u16
*family
,
1102 int loose
, int *argcp
, char ***argvp
)
1105 char **argv
= *argvp
;
1106 inet_prefix dst
= {};
1107 inet_prefix src
= {};
1110 if (strcmp(*argv
, "src") == 0) {
1113 get_prefix(&src
, *argv
, preferred_family
);
1114 if (src
.family
== AF_UNSPEC
)
1115 invarg("value after \"src\" has an unrecognized address family", *argv
);
1117 *family
= src
.family
;
1119 memcpy(saddr
, &src
.data
, sizeof(*saddr
));
1121 filter
.id_src_mask
= src
.bitlen
;
1123 } else if (strcmp(*argv
, "dst") == 0) {
1126 get_prefix(&dst
, *argv
, preferred_family
);
1127 if (dst
.family
== AF_UNSPEC
)
1128 invarg("value after \"dst\" has an unrecognized address family", *argv
);
1130 *family
= dst
.family
;
1132 memcpy(&id
->daddr
, &dst
.data
, sizeof(id
->daddr
));
1134 filter
.id_dst_mask
= dst
.bitlen
;
1136 } else if (strcmp(*argv
, "proto") == 0) {
1141 ret
= xfrm_xfrmproto_getbyname(*argv
);
1143 invarg("XFRM-PROTO value is invalid", *argv
);
1145 id
->proto
= (__u8
)ret
;
1147 filter
.id_proto_mask
= XFRM_FILTER_MASK_FULL
;
1149 } else if (strcmp(*argv
, "spi") == 0) {
1151 if (get_be32(&id
->spi
, *argv
, 0))
1152 invarg("SPI value is invalid", *argv
);
1154 filter
.id_spi_mask
= XFRM_FILTER_MASK_FULL
;
1157 PREV_ARG(); /* back track */
1166 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1167 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv
);
1169 if (id
->spi
&& id
->proto
) {
1170 if (xfrm_xfrmproto_is_ro(id
->proto
)) {
1171 fprintf(stderr
, "\"spi\" is invalid with XFRM-PROTO value \"%s\"\n",
1172 strxf_xfrmproto(id
->proto
));
1174 } else if (id
->proto
== IPPROTO_COMP
&& ntohl(id
->spi
) >= 0x10000) {
1175 fprintf(stderr
, "SPI value is too large with XFRM-PROTO value \"%s\"\n",
1176 strxf_xfrmproto(id
->proto
));
1181 if (loose
== 0 && id
->proto
== 0)
1182 missarg("XFRM-PROTO");
1192 int xfrm_mode_parse(__u8
*mode
, int *argcp
, char ***argvp
)
1195 char **argv
= *argvp
;
1197 if (matches(*argv
, "transport") == 0)
1198 *mode
= XFRM_MODE_TRANSPORT
;
1199 else if (matches(*argv
, "tunnel") == 0)
1200 *mode
= XFRM_MODE_TUNNEL
;
1201 else if (matches(*argv
, "ro") == 0)
1202 *mode
= XFRM_MODE_ROUTEOPTIMIZATION
;
1203 else if (matches(*argv
, "in_trigger") == 0)
1204 *mode
= XFRM_MODE_IN_TRIGGER
;
1205 else if (matches(*argv
, "beet") == 0)
1206 *mode
= XFRM_MODE_BEET
;
1208 invarg("MODE value is invalid", *argv
);
1216 int xfrm_encap_type_parse(__u16
*type
, int *argcp
, char ***argvp
)
1219 char **argv
= *argvp
;
1221 if (strcmp(*argv
, "espinudp-nonike") == 0)
1222 *type
= UDP_ENCAP_ESPINUDP_NON_IKE
;
1223 else if (strcmp(*argv
, "espinudp") == 0)
1224 *type
= UDP_ENCAP_ESPINUDP
;
1225 else if (strcmp(*argv
, "espintcp") == 0)
1226 *type
= TCP_ENCAP_ESPINTCP
;
1228 invarg("ENCAP-TYPE value is invalid", *argv
);
1236 /* NOTE: reqid is used by host-byte order */
1237 int xfrm_reqid_parse(__u32
*reqid
, int *argcp
, char ***argvp
)
1240 char **argv
= *argvp
;
1242 if (get_u32(reqid
, *argv
, 0))
1243 invarg("REQID value is invalid", *argv
);
1251 static int xfrm_selector_upspec_parse(struct xfrm_selector
*sel
,
1252 int *argcp
, char ***argvp
)
1255 char **argv
= *argvp
;
1256 char *sportp
= NULL
;
1257 char *dportp
= NULL
;
1260 char *grekey
= NULL
;
1263 if (strcmp(*argv
, "proto") == 0) {
1268 if (strcmp(*argv
, "any") == 0)
1271 struct protoent
*pp
;
1273 pp
= getprotobyname(*argv
);
1275 upspec
= pp
->p_proto
;
1277 if (get_u8(&upspec
, *argv
, 0))
1278 invarg("PROTO value is invalid", *argv
);
1281 sel
->proto
= upspec
;
1283 filter
.upspec_proto_mask
= XFRM_FILTER_MASK_FULL
;
1285 } else if (strcmp(*argv
, "sport") == 0) {
1290 if (get_be16(&sel
->sport
, *argv
, 0))
1291 invarg("value after \"sport\" is invalid", *argv
);
1293 sel
->sport_mask
= ~((__u16
)0);
1295 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1297 } else if (strcmp(*argv
, "dport") == 0) {
1302 if (get_be16(&sel
->dport
, *argv
, 0))
1303 invarg("value after \"dport\" is invalid", *argv
);
1305 sel
->dport_mask
= ~((__u16
)0);
1307 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1309 } else if (strcmp(*argv
, "type") == 0) {
1314 if (get_u16(&sel
->sport
, *argv
, 0) ||
1315 (sel
->sport
& ~((__u16
)0xff)))
1316 invarg("value after \"type\" is invalid", *argv
);
1317 sel
->sport
= htons(sel
->sport
);
1318 sel
->sport_mask
= ~((__u16
)0);
1320 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1323 } else if (strcmp(*argv
, "code") == 0) {
1328 if (get_u16(&sel
->dport
, *argv
, 0) ||
1329 (sel
->dport
& ~((__u16
)0xff)))
1330 invarg("value after \"code\" is invalid", *argv
);
1331 sel
->dport
= htons(sel
->dport
);
1332 sel
->dport_mask
= ~((__u16
)0);
1334 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1336 } else if (strcmp(*argv
, "key") == 0) {
1343 if (strchr(*argv
, '.'))
1344 uval
= htonl(get_addr32(*argv
));
1346 if (get_unsigned(&uval
, *argv
, 0) < 0) {
1347 fprintf(stderr
, "value after \"key\" is invalid\n");
1352 sel
->sport
= htons(uval
>> 16);
1353 sel
->dport
= htons(uval
& 0xffff);
1354 sel
->sport_mask
= ~((__u16
)0);
1355 sel
->dport_mask
= ~((__u16
)0);
1357 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1360 PREV_ARG(); /* back track */
1370 if (sportp
|| dportp
) {
1371 switch (sel
->proto
) {
1376 case IPPROTO_IP
: /* to allow shared SA for different protocols */
1379 fprintf(stderr
, "\"sport\" and \"dport\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1383 if (typep
|| codep
) {
1384 switch (sel
->proto
) {
1386 case IPPROTO_ICMPV6
:
1390 fprintf(stderr
, "\"type\" and \"code\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1395 switch (sel
->proto
) {
1399 fprintf(stderr
, "\"key\" is invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1410 int xfrm_selector_parse(struct xfrm_selector
*sel
, int *argcp
, char ***argvp
)
1413 char **argv
= *argvp
;
1414 inet_prefix dst
= {};
1415 inet_prefix src
= {};
1416 char *upspecp
= NULL
;
1419 if (strcmp(*argv
, "src") == 0) {
1422 get_prefix(&src
, *argv
, preferred_family
);
1423 if (src
.family
== AF_UNSPEC
)
1424 invarg("value after \"src\" has an unrecognized address family", *argv
);
1425 sel
->family
= src
.family
;
1427 memcpy(&sel
->saddr
, &src
.data
, sizeof(sel
->saddr
));
1428 sel
->prefixlen_s
= src
.bitlen
;
1430 filter
.sel_src_mask
= src
.bitlen
;
1432 } else if (strcmp(*argv
, "dst") == 0) {
1435 get_prefix(&dst
, *argv
, preferred_family
);
1436 if (dst
.family
== AF_UNSPEC
)
1437 invarg("value after \"dst\" has an unrecognized address family", *argv
);
1438 sel
->family
= dst
.family
;
1440 memcpy(&sel
->daddr
, &dst
.data
, sizeof(sel
->daddr
));
1441 sel
->prefixlen_d
= dst
.bitlen
;
1443 filter
.sel_dst_mask
= dst
.bitlen
;
1445 } else if (strcmp(*argv
, "dev") == 0) {
1450 if (strcmp(*argv
, "none") == 0)
1453 ifindex
= ll_name_to_index(*argv
);
1455 invarg("DEV value is invalid", *argv
);
1457 sel
->ifindex
= ifindex
;
1459 filter
.sel_dev_mask
= XFRM_FILTER_MASK_FULL
;
1463 PREV_ARG(); /* back track */
1467 xfrm_selector_upspec_parse(sel
, &argc
, &argv
);
1477 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1478 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv
);
1481 missarg("SELECTOR");
1489 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg
*lft
,
1490 int *argcp
, char ***argvp
)
1493 char **argv
= *argvp
;
1496 if (strcmp(*argv
, "time-soft") == 0) {
1498 ret
= get_u64(&lft
->soft_add_expires_seconds
, *argv
, 0);
1500 invarg("value after \"time-soft\" is invalid", *argv
);
1501 } else if (strcmp(*argv
, "time-hard") == 0) {
1503 ret
= get_u64(&lft
->hard_add_expires_seconds
, *argv
, 0);
1505 invarg("value after \"time-hard\" is invalid", *argv
);
1506 } else if (strcmp(*argv
, "time-use-soft") == 0) {
1508 ret
= get_u64(&lft
->soft_use_expires_seconds
, *argv
, 0);
1510 invarg("value after \"time-use-soft\" is invalid", *argv
);
1511 } else if (strcmp(*argv
, "time-use-hard") == 0) {
1513 ret
= get_u64(&lft
->hard_use_expires_seconds
, *argv
, 0);
1515 invarg("value after \"time-use-hard\" is invalid", *argv
);
1516 } else if (strcmp(*argv
, "byte-soft") == 0) {
1518 ret
= get_u64(&lft
->soft_byte_limit
, *argv
, 0);
1520 invarg("value after \"byte-soft\" is invalid", *argv
);
1521 } else if (strcmp(*argv
, "byte-hard") == 0) {
1523 ret
= get_u64(&lft
->hard_byte_limit
, *argv
, 0);
1525 invarg("value after \"byte-hard\" is invalid", *argv
);
1526 } else if (strcmp(*argv
, "packet-soft") == 0) {
1528 ret
= get_u64(&lft
->soft_packet_limit
, *argv
, 0);
1530 invarg("value after \"packet-soft\" is invalid", *argv
);
1531 } else if (strcmp(*argv
, "packet-hard") == 0) {
1533 ret
= get_u64(&lft
->hard_packet_limit
, *argv
, 0);
1535 invarg("value after \"packet-hard\" is invalid", *argv
);
1537 invarg("LIMIT value is invalid", *argv
);
1545 int do_xfrm(int argc
, char **argv
)
1547 memset(&filter
, 0, sizeof(filter
));
1552 if (matches(*argv
, "state") == 0 ||
1553 matches(*argv
, "sa") == 0)
1554 return do_xfrm_state(argc
-1, argv
+1);
1555 else if (matches(*argv
, "policy") == 0)
1556 return do_xfrm_policy(argc
-1, argv
+1);
1557 else if (matches(*argv
, "monitor") == 0)
1558 return do_xfrm_monitor(argc
-1, argv
+1);
1559 else if (matches(*argv
, "help") == 0) {
1561 fprintf(stderr
, "xfrm Object \"%s\" is unknown.\n", *argv
);