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
);
654 int xfrm_parse_mark(struct xfrm_mark
*mark
, int *argcp
, char ***argvp
)
657 char **argv
= *argvp
;
660 if (get_u32(&mark
->v
, *argv
, 0)) {
661 invarg("MARK value is invalid\n", *argv
);
665 else { /* last entry on parse line */
666 mark
->m
= 0xffffffff;
670 if (strcmp(*argv
, "mask") == 0) {
672 if (get_u32(&mark
->m
, *argv
, 0)) {
673 invarg("MASK value is invalid\n", *argv
);
676 mark
->m
= 0xffffffff;
687 void xfrm_xfrma_print(struct rtattr
*tb
[], __u16 family
,
688 FILE *fp
, const char *prefix
, bool nokeys
)
690 if (tb
[XFRMA_MARK
]) {
691 struct rtattr
*rta
= tb
[XFRMA_MARK
];
692 struct xfrm_mark
*m
= RTA_DATA(rta
);
694 fprintf(fp
, "\tmark %#x/%#x ", m
->v
, m
->m
);
696 if (tb
[XFRMA_OUTPUT_MARK
])
697 xfrm_output_mark_print(tb
, fp
);
698 fprintf(fp
, "%s", _SL_
);
699 } else if (tb
[XFRMA_OUTPUT_MARK
]) {
702 xfrm_output_mark_print(tb
, fp
);
703 fprintf(fp
, "%s", _SL_
);
706 if (tb
[XFRMA_ALG_AUTH
] && !tb
[XFRMA_ALG_AUTH_TRUNC
]) {
707 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH
];
709 xfrm_algo_print(RTA_DATA(rta
), XFRMA_ALG_AUTH
, RTA_PAYLOAD(rta
),
713 if (tb
[XFRMA_ALG_AUTH_TRUNC
]) {
714 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH_TRUNC
];
716 xfrm_auth_trunc_print(RTA_DATA(rta
), RTA_PAYLOAD(rta
), fp
,
720 if (tb
[XFRMA_ALG_AEAD
]) {
721 struct rtattr
*rta
= tb
[XFRMA_ALG_AEAD
];
723 xfrm_aead_print(RTA_DATA(rta
), RTA_PAYLOAD(rta
), fp
, prefix
,
727 if (tb
[XFRMA_ALG_CRYPT
]) {
728 struct rtattr
*rta
= tb
[XFRMA_ALG_CRYPT
];
730 xfrm_algo_print(RTA_DATA(rta
), XFRMA_ALG_CRYPT
,
731 RTA_PAYLOAD(rta
), fp
, prefix
, nokeys
);
734 if (tb
[XFRMA_ALG_COMP
]) {
735 struct rtattr
*rta
= tb
[XFRMA_ALG_COMP
];
737 xfrm_algo_print(RTA_DATA(rta
), XFRMA_ALG_COMP
, RTA_PAYLOAD(rta
),
741 if (tb
[XFRMA_ENCAP
]) {
742 struct xfrm_encap_tmpl
*e
;
746 fprintf(fp
, "encap ");
748 if (RTA_PAYLOAD(tb
[XFRMA_ENCAP
]) < sizeof(*e
)) {
749 fprintf(fp
, "(ERROR truncated)");
750 fprintf(fp
, "%s", _SL_
);
753 e
= RTA_DATA(tb
[XFRMA_ENCAP
]);
755 fprintf(fp
, "type ");
756 switch (e
->encap_type
) {
757 case UDP_ENCAP_ESPINUDP_NON_IKE
:
758 fprintf(fp
, "espinudp-nonike ");
760 case UDP_ENCAP_ESPINUDP
:
761 fprintf(fp
, "espinudp ");
763 case TCP_ENCAP_ESPINTCP
:
764 fprintf(fp
, "espintcp ");
767 fprintf(fp
, "%u ", e
->encap_type
);
770 fprintf(fp
, "sport %u ", ntohs(e
->encap_sport
));
771 fprintf(fp
, "dport %u ", ntohs(e
->encap_dport
));
773 fprintf(fp
, "addr %s",
774 rt_addr_n2a(family
, sizeof(e
->encap_oa
), &e
->encap_oa
));
775 fprintf(fp
, "%s", _SL_
);
778 if (tb
[XFRMA_TMPL
]) {
779 struct rtattr
*rta
= tb
[XFRMA_TMPL
];
781 xfrm_tmpl_print(RTA_DATA(rta
),
782 RTA_PAYLOAD(rta
), fp
, prefix
);
785 if (tb
[XFRMA_COADDR
]) {
786 const xfrm_address_t
*coa
;
792 coa
= RTA_DATA(tb
[XFRMA_COADDR
]);
793 if (RTA_PAYLOAD(tb
[XFRMA_COADDR
]) < sizeof(*coa
)) {
794 fprintf(fp
, "(ERROR truncated)");
795 fprintf(fp
, "%s", _SL_
);
800 rt_addr_n2a(family
, sizeof(*coa
), coa
));
801 fprintf(fp
, "%s", _SL_
);
804 if (tb
[XFRMA_LASTUSED
]) {
809 fprintf(fp
, "lastused ");
811 if (RTA_PAYLOAD(tb
[XFRMA_LASTUSED
]) < sizeof(lastused
)) {
812 fprintf(fp
, "(ERROR truncated)");
813 fprintf(fp
, "%s", _SL_
);
817 lastused
= rta_getattr_u64(tb
[XFRMA_LASTUSED
]);
819 fprintf(fp
, "%s", strxf_time(lastused
));
820 fprintf(fp
, "%s", _SL_
);
823 if (tb
[XFRMA_REPLAY_VAL
]) {
824 struct xfrm_replay_state
*replay
;
828 fprintf(fp
, "anti-replay context: ");
830 if (RTA_PAYLOAD(tb
[XFRMA_REPLAY_VAL
]) < sizeof(*replay
)) {
831 fprintf(fp
, "(ERROR truncated)");
832 fprintf(fp
, "%s", _SL_
);
836 replay
= RTA_DATA(tb
[XFRMA_REPLAY_VAL
]);
837 fprintf(fp
, "seq 0x%x, oseq 0x%x, bitmap 0x%08x",
838 replay
->seq
, replay
->oseq
, replay
->bitmap
);
839 fprintf(fp
, "%s", _SL_
);
842 if (tb
[XFRMA_REPLAY_ESN_VAL
]) {
843 struct xfrm_replay_state_esn
*replay
;
848 fprintf(fp
, "anti-replay esn context:");
850 if (RTA_PAYLOAD(tb
[XFRMA_REPLAY_ESN_VAL
]) < sizeof(*replay
)) {
851 fprintf(fp
, "(ERROR truncated)");
852 fprintf(fp
, "%s", _SL_
);
855 fprintf(fp
, "%s", _SL_
);
857 replay
= RTA_DATA(tb
[XFRMA_REPLAY_ESN_VAL
]);
860 fprintf(fp
, " seq-hi 0x%x, seq 0x%x, oseq-hi 0x%0x, oseq 0x%0x",
861 replay
->seq_hi
, replay
->seq
, replay
->oseq_hi
,
863 fprintf(fp
, "%s", _SL_
);
866 fprintf(fp
, " replay_window %u, bitmap-length %u",
867 replay
->replay_window
, replay
->bmp_len
);
868 for (i
= replay
->bmp_len
, j
= 0; i
; i
--) {
870 fprintf(fp
, "%s", _SL_
);
875 fprintf(fp
, "%08x ", replay
->bmp
[i
- 1]);
877 fprintf(fp
, "%s", _SL_
);
879 if (tb
[XFRMA_OFFLOAD_DEV
]) {
880 struct xfrm_user_offload
*xuo
;
884 fprintf(fp
, "crypto offload parameters: ");
886 if (RTA_PAYLOAD(tb
[XFRMA_OFFLOAD_DEV
]) < sizeof(*xuo
)) {
887 fprintf(fp
, "(ERROR truncated)");
888 fprintf(fp
, "%s", _SL_
);
892 xuo
= (struct xfrm_user_offload
*)
893 RTA_DATA(tb
[XFRMA_OFFLOAD_DEV
]);
894 fprintf(fp
, "dev %s dir %s", ll_index_to_name(xuo
->ifindex
),
895 (xuo
->flags
& XFRM_OFFLOAD_INBOUND
) ? "in" : "out");
896 fprintf(fp
, "%s", _SL_
);
898 if (tb
[XFRMA_IF_ID
]) {
899 __u32 if_id
= rta_getattr_u32(tb
[XFRMA_IF_ID
]);
903 fprintf(fp
, "if_id %#x", if_id
);
904 fprintf(fp
, "%s", _SL_
);
908 static int xfrm_selector_iszero(struct xfrm_selector
*s
)
910 struct xfrm_selector s0
= {};
912 return (memcmp(&s0
, s
, sizeof(s0
)) == 0);
915 void xfrm_state_info_print(struct xfrm_usersa_info
*xsinfo
,
916 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
917 const char *title
, bool nokeys
)
919 char buf
[STRBUF_SIZE
] = {};
920 int force_spi
= xfrm_xfrmproto_is_ipsec(xsinfo
->id
.proto
);
922 xfrm_id_info_print(&xsinfo
->saddr
, &xsinfo
->id
, xsinfo
->mode
,
923 xsinfo
->reqid
, xsinfo
->family
, force_spi
, fp
,
927 strlcat(buf
, prefix
, sizeof(buf
));
928 strlcat(buf
, "\t", sizeof(buf
));
931 fprintf(fp
, "replay-window %u ", xsinfo
->replay_window
);
933 fprintf(fp
, "seq 0x%08u ", xsinfo
->seq
);
934 if (show_stats
> 0 || xsinfo
->flags
) {
935 __u8 flags
= xsinfo
->flags
;
937 fprintf(fp
, "flag ");
938 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOECN
, "noecn");
939 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_DECAP_DSCP
, "decap-dscp");
940 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOPMTUDISC
, "nopmtudisc");
941 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_WILDRECV
, "wildrecv");
942 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ICMP
, "icmp");
943 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_AF_UNSPEC
, "af-unspec");
944 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ALIGN4
, "align4");
945 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ESN
, "esn");
947 fprintf(fp
, "%x", flags
);
949 if (show_stats
> 0 && tb
[XFRMA_SA_EXTRA_FLAGS
]) {
950 __u32 extra_flags
= rta_getattr_u32(tb
[XFRMA_SA_EXTRA_FLAGS
]);
952 fprintf(fp
, "extra_flag ");
953 XFRM_FLAG_PRINT(fp
, extra_flags
,
954 XFRM_SA_XFLAG_DONT_ENCAP_DSCP
,
956 XFRM_FLAG_PRINT(fp
, extra_flags
,
957 XFRM_SA_XFLAG_OSEQ_MAY_WRAP
,
960 fprintf(fp
, "%x", extra_flags
);
963 fprintf(fp
, " (0x%s)", strxf_mask8(xsinfo
->flags
));
964 fprintf(fp
, "%s", _SL_
);
966 xfrm_xfrma_print(tb
, xsinfo
->family
, fp
, buf
, nokeys
);
968 if (!xfrm_selector_iszero(&xsinfo
->sel
)) {
969 char sbuf
[STRBUF_SIZE
];
971 memcpy(sbuf
, buf
, sizeof(sbuf
));
972 strlcat(sbuf
, "sel ", sizeof(sbuf
));
974 xfrm_selector_print(&xsinfo
->sel
, xsinfo
->family
, fp
, sbuf
);
977 if (show_stats
> 0) {
978 xfrm_lifetime_print(&xsinfo
->lft
, &xsinfo
->curlft
, fp
, buf
);
979 xfrm_stats_print(&xsinfo
->stats
, fp
, buf
);
982 if (tb
[XFRMA_SEC_CTX
]) {
983 struct xfrm_user_sec_ctx
*sctx
;
985 fprintf(fp
, "\tsecurity context ");
987 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
988 fprintf(fp
, "(ERROR truncated)");
990 sctx
= RTA_DATA(tb
[XFRMA_SEC_CTX
]);
992 fprintf(fp
, "%s %s", (char *)(sctx
+ 1), _SL_
);
997 void xfrm_policy_info_print(struct xfrm_userpolicy_info
*xpinfo
,
998 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
1001 char buf
[STRBUF_SIZE
] = {};
1003 xfrm_selector_print(&xpinfo
->sel
, preferred_family
, fp
, title
);
1005 if (tb
[XFRMA_SEC_CTX
]) {
1006 struct xfrm_user_sec_ctx
*sctx
;
1008 fprintf(fp
, "\tsecurity context ");
1010 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
1011 fprintf(fp
, "(ERROR truncated)");
1013 sctx
= RTA_DATA(tb
[XFRMA_SEC_CTX
]);
1015 fprintf(fp
, "%s ", (char *)(sctx
+ 1));
1016 fprintf(fp
, "%s", _SL_
);
1020 strlcat(buf
, prefix
, sizeof(buf
));
1021 strlcat(buf
, "\t", sizeof(buf
));
1024 if (xpinfo
->dir
>= XFRM_POLICY_MAX
) {
1025 xpinfo
->dir
-= XFRM_POLICY_MAX
;
1026 fprintf(fp
, "socket ");
1028 fprintf(fp
, "dir ");
1030 switch (xpinfo
->dir
) {
1031 case XFRM_POLICY_IN
:
1034 case XFRM_POLICY_OUT
:
1037 case XFRM_POLICY_FWD
:
1041 fprintf(fp
, "%u", xpinfo
->dir
);
1046 switch (xpinfo
->action
) {
1047 case XFRM_POLICY_ALLOW
:
1049 fprintf(fp
, "action allow ");
1051 case XFRM_POLICY_BLOCK
:
1052 fprintf(fp
, "action block ");
1055 fprintf(fp
, "action %u ", xpinfo
->action
);
1060 fprintf(fp
, "index %u ", xpinfo
->index
);
1061 fprintf(fp
, "priority %u ", xpinfo
->priority
);
1063 if (tb
[XFRMA_POLICY_TYPE
]) {
1064 struct xfrm_userpolicy_type
*upt
;
1066 fprintf(fp
, "ptype ");
1068 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
))
1069 fprintf(fp
, "(ERROR truncated)");
1071 upt
= RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
1072 fprintf(fp
, "%s ", strxf_ptype(upt
->type
));
1076 fprintf(fp
, "share %s ", strxf_share(xpinfo
->share
));
1078 if (show_stats
> 0 || xpinfo
->flags
) {
1079 __u8 flags
= xpinfo
->flags
;
1081 fprintf(fp
, "flag ");
1082 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_LOCALOK
, "localok");
1083 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_ICMP
, "icmp");
1085 fprintf(fp
, "%x", flags
);
1088 fprintf(fp
, " (0x%s)", strxf_mask8(xpinfo
->flags
));
1089 fprintf(fp
, "%s", _SL_
);
1092 xfrm_lifetime_print(&xpinfo
->lft
, &xpinfo
->curlft
, fp
, buf
);
1094 xfrm_xfrma_print(tb
, xpinfo
->sel
.family
, fp
, buf
, false);
1097 int xfrm_id_parse(xfrm_address_t
*saddr
, struct xfrm_id
*id
, __u16
*family
,
1098 int loose
, int *argcp
, char ***argvp
)
1101 char **argv
= *argvp
;
1102 inet_prefix dst
= {};
1103 inet_prefix src
= {};
1106 if (strcmp(*argv
, "src") == 0) {
1109 get_prefix(&src
, *argv
, preferred_family
);
1110 if (src
.family
== AF_UNSPEC
)
1111 invarg("value after \"src\" has an unrecognized address family", *argv
);
1113 *family
= src
.family
;
1115 memcpy(saddr
, &src
.data
, sizeof(*saddr
));
1117 filter
.id_src_mask
= src
.bitlen
;
1119 } else if (strcmp(*argv
, "dst") == 0) {
1122 get_prefix(&dst
, *argv
, preferred_family
);
1123 if (dst
.family
== AF_UNSPEC
)
1124 invarg("value after \"dst\" has an unrecognized address family", *argv
);
1126 *family
= dst
.family
;
1128 memcpy(&id
->daddr
, &dst
.data
, sizeof(id
->daddr
));
1130 filter
.id_dst_mask
= dst
.bitlen
;
1132 } else if (strcmp(*argv
, "proto") == 0) {
1137 ret
= xfrm_xfrmproto_getbyname(*argv
);
1139 invarg("XFRM-PROTO value is invalid", *argv
);
1141 id
->proto
= (__u8
)ret
;
1143 filter
.id_proto_mask
= XFRM_FILTER_MASK_FULL
;
1145 } else if (strcmp(*argv
, "spi") == 0) {
1147 if (get_be32(&id
->spi
, *argv
, 0))
1148 invarg("SPI value is invalid", *argv
);
1150 filter
.id_spi_mask
= XFRM_FILTER_MASK_FULL
;
1153 PREV_ARG(); /* back track */
1162 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1163 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv
);
1165 if (id
->spi
&& id
->proto
) {
1166 if (xfrm_xfrmproto_is_ro(id
->proto
)) {
1167 fprintf(stderr
, "\"spi\" is invalid with XFRM-PROTO value \"%s\"\n",
1168 strxf_xfrmproto(id
->proto
));
1170 } else if (id
->proto
== IPPROTO_COMP
&& ntohl(id
->spi
) >= 0x10000) {
1171 fprintf(stderr
, "SPI value is too large with XFRM-PROTO value \"%s\"\n",
1172 strxf_xfrmproto(id
->proto
));
1177 if (loose
== 0 && id
->proto
== 0)
1178 missarg("XFRM-PROTO");
1188 int xfrm_mode_parse(__u8
*mode
, int *argcp
, char ***argvp
)
1191 char **argv
= *argvp
;
1193 if (matches(*argv
, "transport") == 0)
1194 *mode
= XFRM_MODE_TRANSPORT
;
1195 else if (matches(*argv
, "tunnel") == 0)
1196 *mode
= XFRM_MODE_TUNNEL
;
1197 else if (matches(*argv
, "ro") == 0)
1198 *mode
= XFRM_MODE_ROUTEOPTIMIZATION
;
1199 else if (matches(*argv
, "in_trigger") == 0)
1200 *mode
= XFRM_MODE_IN_TRIGGER
;
1201 else if (matches(*argv
, "beet") == 0)
1202 *mode
= XFRM_MODE_BEET
;
1204 invarg("MODE value is invalid", *argv
);
1212 int xfrm_encap_type_parse(__u16
*type
, int *argcp
, char ***argvp
)
1215 char **argv
= *argvp
;
1217 if (strcmp(*argv
, "espinudp-nonike") == 0)
1218 *type
= UDP_ENCAP_ESPINUDP_NON_IKE
;
1219 else if (strcmp(*argv
, "espinudp") == 0)
1220 *type
= UDP_ENCAP_ESPINUDP
;
1221 else if (strcmp(*argv
, "espintcp") == 0)
1222 *type
= TCP_ENCAP_ESPINTCP
;
1224 invarg("ENCAP-TYPE value is invalid", *argv
);
1232 /* NOTE: reqid is used by host-byte order */
1233 int xfrm_reqid_parse(__u32
*reqid
, int *argcp
, char ***argvp
)
1236 char **argv
= *argvp
;
1238 if (get_u32(reqid
, *argv
, 0))
1239 invarg("REQID value is invalid", *argv
);
1247 static int xfrm_selector_upspec_parse(struct xfrm_selector
*sel
,
1248 int *argcp
, char ***argvp
)
1251 char **argv
= *argvp
;
1252 char *sportp
= NULL
;
1253 char *dportp
= NULL
;
1256 char *grekey
= NULL
;
1259 if (strcmp(*argv
, "proto") == 0) {
1264 if (strcmp(*argv
, "any") == 0)
1267 struct protoent
*pp
;
1269 pp
= getprotobyname(*argv
);
1271 upspec
= pp
->p_proto
;
1273 if (get_u8(&upspec
, *argv
, 0))
1274 invarg("PROTO value is invalid", *argv
);
1277 sel
->proto
= upspec
;
1279 filter
.upspec_proto_mask
= XFRM_FILTER_MASK_FULL
;
1281 } else if (strcmp(*argv
, "sport") == 0) {
1286 if (get_be16(&sel
->sport
, *argv
, 0))
1287 invarg("value after \"sport\" is invalid", *argv
);
1289 sel
->sport_mask
= ~((__u16
)0);
1291 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1293 } else if (strcmp(*argv
, "dport") == 0) {
1298 if (get_be16(&sel
->dport
, *argv
, 0))
1299 invarg("value after \"dport\" is invalid", *argv
);
1301 sel
->dport_mask
= ~((__u16
)0);
1303 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1305 } else if (strcmp(*argv
, "type") == 0) {
1310 if (get_u16(&sel
->sport
, *argv
, 0) ||
1311 (sel
->sport
& ~((__u16
)0xff)))
1312 invarg("value after \"type\" is invalid", *argv
);
1313 sel
->sport
= htons(sel
->sport
);
1314 sel
->sport_mask
= ~((__u16
)0);
1316 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1319 } else if (strcmp(*argv
, "code") == 0) {
1324 if (get_u16(&sel
->dport
, *argv
, 0) ||
1325 (sel
->dport
& ~((__u16
)0xff)))
1326 invarg("value after \"code\" is invalid", *argv
);
1327 sel
->dport
= htons(sel
->dport
);
1328 sel
->dport_mask
= ~((__u16
)0);
1330 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1332 } else if (strcmp(*argv
, "key") == 0) {
1339 if (strchr(*argv
, '.'))
1340 uval
= htonl(get_addr32(*argv
));
1342 if (get_unsigned(&uval
, *argv
, 0) < 0) {
1343 fprintf(stderr
, "value after \"key\" is invalid\n");
1348 sel
->sport
= htons(uval
>> 16);
1349 sel
->dport
= htons(uval
& 0xffff);
1350 sel
->sport_mask
= ~((__u16
)0);
1351 sel
->dport_mask
= ~((__u16
)0);
1353 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1356 PREV_ARG(); /* back track */
1366 if (sportp
|| dportp
) {
1367 switch (sel
->proto
) {
1372 case IPPROTO_IP
: /* to allow shared SA for different protocols */
1375 fprintf(stderr
, "\"sport\" and \"dport\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1379 if (typep
|| codep
) {
1380 switch (sel
->proto
) {
1382 case IPPROTO_ICMPV6
:
1386 fprintf(stderr
, "\"type\" and \"code\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1391 switch (sel
->proto
) {
1395 fprintf(stderr
, "\"key\" is invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1406 int xfrm_selector_parse(struct xfrm_selector
*sel
, int *argcp
, char ***argvp
)
1409 char **argv
= *argvp
;
1410 inet_prefix dst
= {};
1411 inet_prefix src
= {};
1412 char *upspecp
= NULL
;
1415 if (strcmp(*argv
, "src") == 0) {
1418 get_prefix(&src
, *argv
, preferred_family
);
1419 if (src
.family
== AF_UNSPEC
)
1420 invarg("value after \"src\" has an unrecognized address family", *argv
);
1421 sel
->family
= src
.family
;
1423 memcpy(&sel
->saddr
, &src
.data
, sizeof(sel
->saddr
));
1424 sel
->prefixlen_s
= src
.bitlen
;
1426 filter
.sel_src_mask
= src
.bitlen
;
1428 } else if (strcmp(*argv
, "dst") == 0) {
1431 get_prefix(&dst
, *argv
, preferred_family
);
1432 if (dst
.family
== AF_UNSPEC
)
1433 invarg("value after \"dst\" has an unrecognized address family", *argv
);
1434 sel
->family
= dst
.family
;
1436 memcpy(&sel
->daddr
, &dst
.data
, sizeof(sel
->daddr
));
1437 sel
->prefixlen_d
= dst
.bitlen
;
1439 filter
.sel_dst_mask
= dst
.bitlen
;
1441 } else if (strcmp(*argv
, "dev") == 0) {
1446 if (strcmp(*argv
, "none") == 0)
1449 ifindex
= ll_name_to_index(*argv
);
1451 invarg("DEV value is invalid", *argv
);
1453 sel
->ifindex
= ifindex
;
1455 filter
.sel_dev_mask
= XFRM_FILTER_MASK_FULL
;
1459 PREV_ARG(); /* back track */
1463 xfrm_selector_upspec_parse(sel
, &argc
, &argv
);
1473 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1474 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv
);
1477 missarg("SELECTOR");
1485 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg
*lft
,
1486 int *argcp
, char ***argvp
)
1489 char **argv
= *argvp
;
1492 if (strcmp(*argv
, "time-soft") == 0) {
1494 ret
= get_u64(&lft
->soft_add_expires_seconds
, *argv
, 0);
1496 invarg("value after \"time-soft\" is invalid", *argv
);
1497 } else if (strcmp(*argv
, "time-hard") == 0) {
1499 ret
= get_u64(&lft
->hard_add_expires_seconds
, *argv
, 0);
1501 invarg("value after \"time-hard\" is invalid", *argv
);
1502 } else if (strcmp(*argv
, "time-use-soft") == 0) {
1504 ret
= get_u64(&lft
->soft_use_expires_seconds
, *argv
, 0);
1506 invarg("value after \"time-use-soft\" is invalid", *argv
);
1507 } else if (strcmp(*argv
, "time-use-hard") == 0) {
1509 ret
= get_u64(&lft
->hard_use_expires_seconds
, *argv
, 0);
1511 invarg("value after \"time-use-hard\" is invalid", *argv
);
1512 } else if (strcmp(*argv
, "byte-soft") == 0) {
1514 ret
= get_u64(&lft
->soft_byte_limit
, *argv
, 0);
1516 invarg("value after \"byte-soft\" is invalid", *argv
);
1517 } else if (strcmp(*argv
, "byte-hard") == 0) {
1519 ret
= get_u64(&lft
->hard_byte_limit
, *argv
, 0);
1521 invarg("value after \"byte-hard\" is invalid", *argv
);
1522 } else if (strcmp(*argv
, "packet-soft") == 0) {
1524 ret
= get_u64(&lft
->soft_packet_limit
, *argv
, 0);
1526 invarg("value after \"packet-soft\" is invalid", *argv
);
1527 } else if (strcmp(*argv
, "packet-hard") == 0) {
1529 ret
= get_u64(&lft
->hard_packet_limit
, *argv
, 0);
1531 invarg("value after \"packet-hard\" is invalid", *argv
);
1533 invarg("LIMIT value is invalid", *argv
);
1541 int do_xfrm(int argc
, char **argv
)
1543 memset(&filter
, 0, sizeof(filter
));
1548 if (matches(*argv
, "state") == 0 ||
1549 matches(*argv
, "sa") == 0)
1550 return do_xfrm_state(argc
-1, argv
+1);
1551 else if (matches(*argv
, "policy") == 0)
1552 return do_xfrm_policy(argc
-1, argv
+1);
1553 else if (matches(*argv
, "monitor") == 0)
1554 return do_xfrm_monitor(argc
-1, argv
+1);
1555 else if (matches(*argv
, "help") == 0) {
1557 fprintf(stderr
, "xfrm Object \"%s\" is unknown.\n", *argv
);