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>
40 #include "ip_common.h"
42 #define STRBUF_SIZE (128)
44 struct xfrm_filter filter
;
46 static void usage(void) __attribute__((noreturn
));
48 static void usage(void)
51 "Usage: ip xfrm XFRM-OBJECT { COMMAND | help }\n"
52 "where XFRM-OBJECT := state | policy | monitor\n");
56 /* This is based on utils.c(inet_addr_match) */
57 int xfrm_addr_match(xfrm_address_t
*x1
, xfrm_address_t
*x2
, int bits
)
59 __u32
*a1
= (__u32
*)x1
;
60 __u32
*a2
= (__u32
*)x2
;
61 int words
= bits
>> 0x05;
66 if (memcmp(a1
, a2
, words
<< 2))
76 mask
= htonl((0xffffffff) << (0x20 - bits
));
85 int xfrm_xfrmproto_is_ipsec(__u8 proto
)
87 return (proto
== IPPROTO_ESP
||
88 proto
== IPPROTO_AH
||
89 proto
== IPPROTO_COMP
);
92 int xfrm_xfrmproto_is_ro(__u8 proto
)
94 return (proto
== IPPROTO_ROUTING
||
95 proto
== IPPROTO_DSTOPTS
);
103 static const struct typeent xfrmproto_types
[] = {
104 { "esp", IPPROTO_ESP
}, { "ah", IPPROTO_AH
}, { "comp", IPPROTO_COMP
},
105 { "route2", IPPROTO_ROUTING
}, { "hao", IPPROTO_DSTOPTS
},
106 { "ipsec-any", IPSEC_PROTO_ANY
},
110 int xfrm_xfrmproto_getbyname(char *name
)
115 const struct typeent
*t
= &xfrmproto_types
[i
];
117 if (!t
->t_name
|| t
->t_type
== -1)
120 if (strcmp(t
->t_name
, name
) == 0)
127 const char *strxf_xfrmproto(__u8 proto
)
133 const struct typeent
*t
= &xfrmproto_types
[i
];
135 if (!t
->t_name
|| t
->t_type
== -1)
138 if (t
->t_type
== proto
)
142 sprintf(str
, "%u", proto
);
146 static const struct typeent algo_types
[] = {
147 { "enc", XFRMA_ALG_CRYPT
}, { "auth", XFRMA_ALG_AUTH
},
148 { "comp", XFRMA_ALG_COMP
}, { "aead", XFRMA_ALG_AEAD
},
149 { "auth-trunc", XFRMA_ALG_AUTH_TRUNC
},
153 int xfrm_algotype_getbyname(char *name
)
158 const struct typeent
*t
= &algo_types
[i
];
160 if (!t
->t_name
|| t
->t_type
== -1)
163 if (strcmp(t
->t_name
, name
) == 0)
170 const char *strxf_algotype(int type
)
176 const struct typeent
*t
= &algo_types
[i
];
178 if (!t
->t_name
|| t
->t_type
== -1)
181 if (t
->t_type
== type
)
185 sprintf(str
, "%d", type
);
189 static const char *strxf_mask8(__u8 mask
)
192 const int sn
= sizeof(mask
) * 8 - 1;
196 for (b
= (1 << sn
); b
> 0; b
>>= 1)
197 str
[i
++] = ((b
& mask
) ? '1' : '0');
203 const char *strxf_mask32(__u32 mask
)
207 sprintf(str
, "%.8x", mask
);
212 static const char *strxf_share(__u8 share
)
220 case XFRM_SHARE_SESSION
:
221 strcpy(str
, "session");
223 case XFRM_SHARE_USER
:
226 case XFRM_SHARE_UNIQUE
:
227 strcpy(str
, "unique");
230 sprintf(str
, "%u", share
);
237 const char *strxf_proto(__u8 proto
)
243 pp
= getprotobynumber(proto
);
247 sprintf(buf
, "%u", proto
);
254 const char *strxf_ptype(__u8 ptype
)
259 case XFRM_POLICY_TYPE_MAIN
:
262 case XFRM_POLICY_TYPE_SUB
:
266 sprintf(str
, "%u", ptype
);
273 static void xfrm_id_info_print(xfrm_address_t
*saddr
, struct xfrm_id
*id
,
274 __u8 mode
, __u32 reqid
, __u16 family
, int force_spi
,
275 FILE *fp
, const char *prefix
, const char *title
)
280 fprintf(fp
, "src %s ", rt_addr_n2a(family
, sizeof(*saddr
), saddr
));
281 fprintf(fp
, "dst %s", rt_addr_n2a(family
, sizeof(id
->daddr
), &id
->daddr
));
282 fprintf(fp
, "%s", _SL_
);
288 fprintf(fp
, "proto %s ", strxf_xfrmproto(id
->proto
));
290 if (show_stats
> 0 || force_spi
|| id
->spi
) {
291 __u32 spi
= ntohl(id
->spi
);
293 fprintf(fp
, "spi 0x%08x", spi
);
295 fprintf(fp
, "(%u)", spi
);
299 fprintf(fp
, "reqid %u", reqid
);
301 fprintf(fp
, "(0x%08x)", reqid
);
304 fprintf(fp
, "mode ");
306 case XFRM_MODE_TRANSPORT
:
307 fprintf(fp
, "transport");
309 case XFRM_MODE_TUNNEL
:
310 fprintf(fp
, "tunnel");
312 case XFRM_MODE_ROUTEOPTIMIZATION
:
315 case XFRM_MODE_IN_TRIGGER
:
316 fprintf(fp
, "in_trigger");
322 fprintf(fp
, "%u", mode
);
325 fprintf(fp
, "%s", _SL_
);
328 static const char *strxf_limit(__u64 limit
)
332 if (limit
== XFRM_INF
)
333 strcpy(str
, "(INF)");
335 sprintf(str
, "%llu", (unsigned long long) limit
);
340 static void xfrm_stats_print(struct xfrm_stats
*s
, FILE *fp
,
345 fprintf(fp
, "stats:%s", _SL_
);
349 fprintf(fp
, " replay-window %u replay %u failed %u%s",
350 s
->replay_window
, s
->replay
, s
->integrity_failed
, _SL_
);
353 static const char *strxf_time(__u64 time
)
363 /* XXX: treat time in the same manner of kernel's
364 * net/xfrm/xfrm_{user,state}.c
369 strftime(str
, sizeof(str
), "%Y-%m-%d %T", tp
);
375 static void xfrm_lifetime_print(struct xfrm_lifetime_cfg
*cfg
,
376 struct xfrm_lifetime_cur
*cur
,
377 FILE *fp
, const char *prefix
)
382 fprintf(fp
, "lifetime config:%s", _SL_
);
386 fprintf(fp
, " limit: soft %s(bytes),",
387 strxf_limit(cfg
->soft_byte_limit
));
388 fprintf(fp
, " hard %s(bytes)%s",
389 strxf_limit(cfg
->hard_byte_limit
), _SL_
);
393 fprintf(fp
, " limit: soft %s(packets),",
394 strxf_limit(cfg
->soft_packet_limit
));
395 fprintf(fp
, " hard %s(packets)%s",
396 strxf_limit(cfg
->hard_packet_limit
), _SL_
);
400 fprintf(fp
, " expire add: soft %llu(sec), hard %llu(sec)%s",
401 (unsigned long long) cfg
->soft_add_expires_seconds
,
402 (unsigned long long) cfg
->hard_add_expires_seconds
,
407 fprintf(fp
, " expire use: soft %llu(sec), hard %llu(sec)%s",
408 (unsigned long long) cfg
->soft_use_expires_seconds
,
409 (unsigned long long) cfg
->hard_use_expires_seconds
,
415 fprintf(fp
, "lifetime current:%s", _SL_
);
419 fprintf(fp
, " %llu(bytes), %llu(packets)%s",
420 (unsigned long long) cur
->bytes
,
421 (unsigned long long) cur
->packets
,
426 fprintf(fp
, " add %s ", strxf_time(cur
->add_time
));
427 fprintf(fp
, "use %s%s", strxf_time(cur
->use_time
), _SL_
);
431 void xfrm_selector_print(struct xfrm_selector
*sel
, __u16 family
,
432 FILE *fp
, const char *prefix
)
440 f
= preferred_family
;
445 fprintf(fp
, "src %s/%u ",
446 rt_addr_n2a(f
, sizeof(sel
->saddr
), &sel
->saddr
),
449 fprintf(fp
, "dst %s/%u ",
450 rt_addr_n2a(f
, sizeof(sel
->daddr
), &sel
->daddr
),
454 fprintf(fp
, "proto %s ", strxf_proto(sel
->proto
));
455 switch (sel
->proto
) {
462 fprintf(fp
, "sport %u ", ntohs(sel
->sport
));
464 fprintf(fp
, "dport %u ", ntohs(sel
->dport
));
468 /* type/code is stored at sport/dport in selector */
470 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
472 fprintf(fp
, "code %u ", ntohs(sel
->dport
));
475 if (sel
->sport_mask
|| sel
->dport_mask
)
476 fprintf(fp
, "key %u ",
477 (((__u32
)ntohs(sel
->sport
)) << 16) +
482 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
483 if (sel
->dport_mask
) {
485 fprintf(fp
, "(dport) 0x%.4x ", sel
->dport
);
490 if (sel
->ifindex
> 0)
491 fprintf(fp
, "dev %s ", ll_index_to_name(sel
->ifindex
));
494 fprintf(fp
, "uid %u", sel
->user
);
496 fprintf(fp
, "%s", _SL_
);
499 static void __xfrm_algo_print(struct xfrm_algo
*algo
, int type
, int len
,
500 FILE *fp
, const char *prefix
, int newline
,
509 fprintf(fp
, "%s ", strxf_algotype(type
));
511 if (len
< sizeof(*algo
)) {
512 fprintf(fp
, "(ERROR truncated)");
515 len
-= sizeof(*algo
);
517 fprintf(fp
, "%s ", algo
->alg_name
);
519 keylen
= algo
->alg_key_len
/ 8;
521 fprintf(fp
, "(ERROR truncated)");
526 fprintf(fp
, "<<Keys hidden>>");
527 else if (keylen
> 0) {
529 for (i
= 0; i
< keylen
; i
++)
530 fprintf(fp
, "%.2x", (unsigned char)algo
->alg_key
[i
]);
533 fprintf(fp
, " (%d bits)", algo
->alg_key_len
);
538 fprintf(fp
, "%s", _SL_
);
541 static inline void xfrm_algo_print(struct xfrm_algo
*algo
, int type
, int len
,
542 FILE *fp
, const char *prefix
, bool nokeys
)
544 return __xfrm_algo_print(algo
, type
, len
, fp
, prefix
, 1, nokeys
);
547 static void xfrm_aead_print(struct xfrm_algo_aead
*algo
, int len
,
548 FILE *fp
, const char *prefix
, bool nokeys
)
550 struct xfrm_algo
*base_algo
= alloca(sizeof(*base_algo
) + algo
->alg_key_len
/ 8);
552 memcpy(base_algo
->alg_name
, algo
->alg_name
, sizeof(base_algo
->alg_name
));
553 base_algo
->alg_key_len
= algo
->alg_key_len
;
554 memcpy(base_algo
->alg_key
, algo
->alg_key
, algo
->alg_key_len
/ 8);
556 __xfrm_algo_print(base_algo
, XFRMA_ALG_AEAD
, len
, fp
, prefix
, 0,
559 fprintf(fp
, " %d", algo
->alg_icv_len
);
561 fprintf(fp
, "%s", _SL_
);
564 static void xfrm_auth_trunc_print(struct xfrm_algo_auth
*algo
, int len
,
565 FILE *fp
, const char *prefix
, bool nokeys
)
567 struct xfrm_algo
*base_algo
= alloca(sizeof(*base_algo
) + algo
->alg_key_len
/ 8);
569 memcpy(base_algo
->alg_name
, algo
->alg_name
, sizeof(base_algo
->alg_name
));
570 base_algo
->alg_key_len
= algo
->alg_key_len
;
571 memcpy(base_algo
->alg_key
, algo
->alg_key
, algo
->alg_key_len
/ 8);
573 __xfrm_algo_print(base_algo
, XFRMA_ALG_AUTH_TRUNC
, len
, fp
, prefix
, 0,
576 fprintf(fp
, " %d", algo
->alg_trunc_len
);
578 fprintf(fp
, "%s", _SL_
);
581 static void xfrm_tmpl_print(struct xfrm_user_tmpl
*tmpls
, int len
,
582 FILE *fp
, const char *prefix
)
584 int ntmpls
= len
/ sizeof(struct xfrm_user_tmpl
);
590 fprintf(fp
, "(ERROR \"tmpl\" truncated)");
591 fprintf(fp
, "%s", _SL_
);
595 for (i
= 0; i
< ntmpls
; i
++) {
596 struct xfrm_user_tmpl
*tmpl
= &tmpls
[i
];
601 xfrm_id_info_print(&tmpl
->saddr
, &tmpl
->id
, tmpl
->mode
,
602 tmpl
->reqid
, tmpl
->family
, 0, fp
, prefix
, "tmpl ");
604 if (show_stats
> 0 || tmpl
->optional
) {
608 switch (tmpl
->optional
) {
611 fprintf(fp
, "level required ");
614 fprintf(fp
, "level use ");
617 fprintf(fp
, "level %u ", tmpl
->optional
);
622 fprintf(fp
, "share %s ", strxf_share(tmpl
->share
));
624 fprintf(fp
, "%s", _SL_
);
627 if (show_stats
> 0) {
631 fprintf(fp
, "%s-mask %s ",
632 strxf_algotype(XFRMA_ALG_CRYPT
),
633 strxf_mask32(tmpl
->ealgos
));
634 fprintf(fp
, "%s-mask %s ",
635 strxf_algotype(XFRMA_ALG_AUTH
),
636 strxf_mask32(tmpl
->aalgos
));
637 fprintf(fp
, "%s-mask %s",
638 strxf_algotype(XFRMA_ALG_COMP
),
639 strxf_mask32(tmpl
->calgos
));
641 fprintf(fp
, "%s", _SL_
);
646 static void xfrm_output_mark_print(struct rtattr
*tb
[], FILE *fp
)
648 __u32 output_mark
= rta_getattr_u32(tb
[XFRMA_OUTPUT_MARK
]);
650 fprintf(fp
, "output-mark 0x%x", output_mark
);
653 int xfrm_parse_mark(struct xfrm_mark
*mark
, int *argcp
, char ***argvp
)
656 char **argv
= *argvp
;
659 if (get_u32(&mark
->v
, *argv
, 0)) {
660 invarg("MARK value is invalid\n", *argv
);
664 else { /* last entry on parse line */
665 mark
->m
= 0xffffffff;
669 if (strcmp(*argv
, "mask") == 0) {
671 if (get_u32(&mark
->m
, *argv
, 0)) {
672 invarg("MASK value is invalid\n", *argv
);
675 mark
->m
= 0xffffffff;
686 void xfrm_xfrma_print(struct rtattr
*tb
[], __u16 family
,
687 FILE *fp
, const char *prefix
, bool nokeys
)
689 if (tb
[XFRMA_MARK
]) {
690 struct rtattr
*rta
= tb
[XFRMA_MARK
];
691 struct xfrm_mark
*m
= RTA_DATA(rta
);
693 fprintf(fp
, "\tmark %#x/%#x ", m
->v
, m
->m
);
695 if (tb
[XFRMA_OUTPUT_MARK
])
696 xfrm_output_mark_print(tb
, fp
);
697 fprintf(fp
, "%s", _SL_
);
698 } else if (tb
[XFRMA_OUTPUT_MARK
]) {
701 xfrm_output_mark_print(tb
, fp
);
702 fprintf(fp
, "%s", _SL_
);
705 if (tb
[XFRMA_ALG_AUTH
] && !tb
[XFRMA_ALG_AUTH_TRUNC
]) {
706 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH
];
708 xfrm_algo_print(RTA_DATA(rta
), XFRMA_ALG_AUTH
, RTA_PAYLOAD(rta
),
712 if (tb
[XFRMA_ALG_AUTH_TRUNC
]) {
713 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH_TRUNC
];
715 xfrm_auth_trunc_print(RTA_DATA(rta
), RTA_PAYLOAD(rta
), fp
,
719 if (tb
[XFRMA_ALG_AEAD
]) {
720 struct rtattr
*rta
= tb
[XFRMA_ALG_AEAD
];
722 xfrm_aead_print(RTA_DATA(rta
), RTA_PAYLOAD(rta
), fp
, prefix
,
726 if (tb
[XFRMA_ALG_CRYPT
]) {
727 struct rtattr
*rta
= tb
[XFRMA_ALG_CRYPT
];
729 xfrm_algo_print(RTA_DATA(rta
), XFRMA_ALG_CRYPT
,
730 RTA_PAYLOAD(rta
), fp
, prefix
, nokeys
);
733 if (tb
[XFRMA_ALG_COMP
]) {
734 struct rtattr
*rta
= tb
[XFRMA_ALG_COMP
];
736 xfrm_algo_print(RTA_DATA(rta
), XFRMA_ALG_COMP
, RTA_PAYLOAD(rta
),
740 if (tb
[XFRMA_ENCAP
]) {
741 struct xfrm_encap_tmpl
*e
;
745 fprintf(fp
, "encap ");
747 if (RTA_PAYLOAD(tb
[XFRMA_ENCAP
]) < sizeof(*e
)) {
748 fprintf(fp
, "(ERROR truncated)");
749 fprintf(fp
, "%s", _SL_
);
752 e
= RTA_DATA(tb
[XFRMA_ENCAP
]);
754 fprintf(fp
, "type ");
755 switch (e
->encap_type
) {
757 fprintf(fp
, "espinudp-nonike ");
760 fprintf(fp
, "espinudp ");
763 fprintf(fp
, "%u ", e
->encap_type
);
766 fprintf(fp
, "sport %u ", ntohs(e
->encap_sport
));
767 fprintf(fp
, "dport %u ", ntohs(e
->encap_dport
));
769 fprintf(fp
, "addr %s",
770 rt_addr_n2a(family
, sizeof(e
->encap_oa
), &e
->encap_oa
));
771 fprintf(fp
, "%s", _SL_
);
774 if (tb
[XFRMA_TMPL
]) {
775 struct rtattr
*rta
= tb
[XFRMA_TMPL
];
777 xfrm_tmpl_print(RTA_DATA(rta
),
778 RTA_PAYLOAD(rta
), fp
, prefix
);
781 if (tb
[XFRMA_COADDR
]) {
782 const xfrm_address_t
*coa
;
788 coa
= RTA_DATA(tb
[XFRMA_COADDR
]);
789 if (RTA_PAYLOAD(tb
[XFRMA_COADDR
]) < sizeof(*coa
)) {
790 fprintf(fp
, "(ERROR truncated)");
791 fprintf(fp
, "%s", _SL_
);
796 rt_addr_n2a(family
, sizeof(*coa
), coa
));
797 fprintf(fp
, "%s", _SL_
);
800 if (tb
[XFRMA_LASTUSED
]) {
805 fprintf(fp
, "lastused ");
807 if (RTA_PAYLOAD(tb
[XFRMA_LASTUSED
]) < sizeof(lastused
)) {
808 fprintf(fp
, "(ERROR truncated)");
809 fprintf(fp
, "%s", _SL_
);
813 lastused
= rta_getattr_u64(tb
[XFRMA_LASTUSED
]);
815 fprintf(fp
, "%s", strxf_time(lastused
));
816 fprintf(fp
, "%s", _SL_
);
819 if (tb
[XFRMA_REPLAY_VAL
]) {
820 struct xfrm_replay_state
*replay
;
824 fprintf(fp
, "anti-replay context: ");
826 if (RTA_PAYLOAD(tb
[XFRMA_REPLAY_VAL
]) < sizeof(*replay
)) {
827 fprintf(fp
, "(ERROR truncated)");
828 fprintf(fp
, "%s", _SL_
);
832 replay
= RTA_DATA(tb
[XFRMA_REPLAY_VAL
]);
833 fprintf(fp
, "seq 0x%x, oseq 0x%x, bitmap 0x%08x",
834 replay
->seq
, replay
->oseq
, replay
->bitmap
);
835 fprintf(fp
, "%s", _SL_
);
838 if (tb
[XFRMA_REPLAY_ESN_VAL
]) {
839 struct xfrm_replay_state_esn
*replay
;
844 fprintf(fp
, "anti-replay esn context:");
846 if (RTA_PAYLOAD(tb
[XFRMA_REPLAY_ESN_VAL
]) < sizeof(*replay
)) {
847 fprintf(fp
, "(ERROR truncated)");
848 fprintf(fp
, "%s", _SL_
);
851 fprintf(fp
, "%s", _SL_
);
853 replay
= RTA_DATA(tb
[XFRMA_REPLAY_ESN_VAL
]);
856 fprintf(fp
, " seq-hi 0x%x, seq 0x%x, oseq-hi 0x%0x, oseq 0x%0x",
857 replay
->seq_hi
, replay
->seq
, replay
->oseq_hi
,
859 fprintf(fp
, "%s", _SL_
);
862 fprintf(fp
, " replay_window %u, bitmap-length %u",
863 replay
->replay_window
, replay
->bmp_len
);
864 for (i
= replay
->bmp_len
, j
= 0; i
; i
--) {
866 fprintf(fp
, "%s", _SL_
);
871 fprintf(fp
, "%08x ", replay
->bmp
[i
- 1]);
873 fprintf(fp
, "%s", _SL_
);
875 if (tb
[XFRMA_OFFLOAD_DEV
]) {
876 struct xfrm_user_offload
*xuo
;
880 fprintf(fp
, "crypto offload parameters: ");
882 if (RTA_PAYLOAD(tb
[XFRMA_OFFLOAD_DEV
]) < sizeof(*xuo
)) {
883 fprintf(fp
, "(ERROR truncated)");
884 fprintf(fp
, "%s", _SL_
);
888 xuo
= (struct xfrm_user_offload
*)
889 RTA_DATA(tb
[XFRMA_OFFLOAD_DEV
]);
890 fprintf(fp
, "dev %s dir %s", ll_index_to_name(xuo
->ifindex
),
891 (xuo
->flags
& XFRM_OFFLOAD_INBOUND
) ? "in" : "out");
892 fprintf(fp
, "%s", _SL_
);
896 static int xfrm_selector_iszero(struct xfrm_selector
*s
)
898 struct xfrm_selector s0
= {};
900 return (memcmp(&s0
, s
, sizeof(s0
)) == 0);
903 void xfrm_state_info_print(struct xfrm_usersa_info
*xsinfo
,
904 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
905 const char *title
, bool nokeys
)
907 char buf
[STRBUF_SIZE
] = {};
908 int force_spi
= xfrm_xfrmproto_is_ipsec(xsinfo
->id
.proto
);
910 xfrm_id_info_print(&xsinfo
->saddr
, &xsinfo
->id
, xsinfo
->mode
,
911 xsinfo
->reqid
, xsinfo
->family
, force_spi
, fp
,
915 strlcat(buf
, prefix
, sizeof(buf
));
916 strlcat(buf
, "\t", sizeof(buf
));
919 fprintf(fp
, "replay-window %u ", xsinfo
->replay_window
);
921 fprintf(fp
, "seq 0x%08u ", xsinfo
->seq
);
922 if (show_stats
> 0 || xsinfo
->flags
) {
923 __u8 flags
= xsinfo
->flags
;
925 fprintf(fp
, "flag ");
926 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOECN
, "noecn");
927 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_DECAP_DSCP
, "decap-dscp");
928 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOPMTUDISC
, "nopmtudisc");
929 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_WILDRECV
, "wildrecv");
930 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ICMP
, "icmp");
931 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_AF_UNSPEC
, "af-unspec");
932 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ALIGN4
, "align4");
933 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ESN
, "esn");
935 fprintf(fp
, "%x", flags
);
937 if (show_stats
> 0 && tb
[XFRMA_SA_EXTRA_FLAGS
]) {
938 __u32 extra_flags
= rta_getattr_u32(tb
[XFRMA_SA_EXTRA_FLAGS
]);
940 fprintf(fp
, "extra_flag ");
941 XFRM_FLAG_PRINT(fp
, extra_flags
,
942 XFRM_SA_XFLAG_DONT_ENCAP_DSCP
,
945 fprintf(fp
, "%x", extra_flags
);
948 fprintf(fp
, " (0x%s)", strxf_mask8(xsinfo
->flags
));
949 fprintf(fp
, "%s", _SL_
);
951 xfrm_xfrma_print(tb
, xsinfo
->family
, fp
, buf
, nokeys
);
953 if (!xfrm_selector_iszero(&xsinfo
->sel
)) {
954 char sbuf
[STRBUF_SIZE
];
956 memcpy(sbuf
, buf
, sizeof(sbuf
));
957 strlcat(sbuf
, "sel ", sizeof(sbuf
));
959 xfrm_selector_print(&xsinfo
->sel
, xsinfo
->family
, fp
, sbuf
);
962 if (show_stats
> 0) {
963 xfrm_lifetime_print(&xsinfo
->lft
, &xsinfo
->curlft
, fp
, buf
);
964 xfrm_stats_print(&xsinfo
->stats
, fp
, buf
);
967 if (tb
[XFRMA_SEC_CTX
]) {
968 struct xfrm_user_sec_ctx
*sctx
;
970 fprintf(fp
, "\tsecurity context ");
972 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
973 fprintf(fp
, "(ERROR truncated)");
975 sctx
= RTA_DATA(tb
[XFRMA_SEC_CTX
]);
977 fprintf(fp
, "%s %s", (char *)(sctx
+ 1), _SL_
);
982 void xfrm_policy_info_print(struct xfrm_userpolicy_info
*xpinfo
,
983 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
986 char buf
[STRBUF_SIZE
] = {};
988 xfrm_selector_print(&xpinfo
->sel
, preferred_family
, fp
, title
);
990 if (tb
[XFRMA_SEC_CTX
]) {
991 struct xfrm_user_sec_ctx
*sctx
;
993 fprintf(fp
, "\tsecurity context ");
995 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
996 fprintf(fp
, "(ERROR truncated)");
998 sctx
= RTA_DATA(tb
[XFRMA_SEC_CTX
]);
1000 fprintf(fp
, "%s ", (char *)(sctx
+ 1));
1001 fprintf(fp
, "%s", _SL_
);
1005 strlcat(buf
, prefix
, sizeof(buf
));
1006 strlcat(buf
, "\t", sizeof(buf
));
1009 if (xpinfo
->dir
>= XFRM_POLICY_MAX
) {
1010 xpinfo
->dir
-= XFRM_POLICY_MAX
;
1011 fprintf(fp
, "socket ");
1013 fprintf(fp
, "dir ");
1015 switch (xpinfo
->dir
) {
1016 case XFRM_POLICY_IN
:
1019 case XFRM_POLICY_OUT
:
1022 case XFRM_POLICY_FWD
:
1026 fprintf(fp
, "%u", xpinfo
->dir
);
1031 switch (xpinfo
->action
) {
1032 case XFRM_POLICY_ALLOW
:
1034 fprintf(fp
, "action allow ");
1036 case XFRM_POLICY_BLOCK
:
1037 fprintf(fp
, "action block ");
1040 fprintf(fp
, "action %u ", xpinfo
->action
);
1045 fprintf(fp
, "index %u ", xpinfo
->index
);
1046 fprintf(fp
, "priority %u ", xpinfo
->priority
);
1048 if (tb
[XFRMA_POLICY_TYPE
]) {
1049 struct xfrm_userpolicy_type
*upt
;
1051 fprintf(fp
, "ptype ");
1053 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
))
1054 fprintf(fp
, "(ERROR truncated)");
1056 upt
= RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
1057 fprintf(fp
, "%s ", strxf_ptype(upt
->type
));
1061 fprintf(fp
, "share %s ", strxf_share(xpinfo
->share
));
1063 if (show_stats
> 0 || xpinfo
->flags
) {
1064 __u8 flags
= xpinfo
->flags
;
1066 fprintf(fp
, "flag ");
1067 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_LOCALOK
, "localok");
1068 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_ICMP
, "icmp");
1070 fprintf(fp
, "%x", flags
);
1073 fprintf(fp
, " (0x%s)", strxf_mask8(xpinfo
->flags
));
1074 fprintf(fp
, "%s", _SL_
);
1077 xfrm_lifetime_print(&xpinfo
->lft
, &xpinfo
->curlft
, fp
, buf
);
1079 xfrm_xfrma_print(tb
, xpinfo
->sel
.family
, fp
, buf
, false);
1082 int xfrm_id_parse(xfrm_address_t
*saddr
, struct xfrm_id
*id
, __u16
*family
,
1083 int loose
, int *argcp
, char ***argvp
)
1086 char **argv
= *argvp
;
1087 inet_prefix dst
= {};
1088 inet_prefix src
= {};
1091 if (strcmp(*argv
, "src") == 0) {
1094 get_prefix(&src
, *argv
, preferred_family
);
1095 if (src
.family
== AF_UNSPEC
)
1096 invarg("value after \"src\" has an unrecognized address family", *argv
);
1098 *family
= src
.family
;
1100 memcpy(saddr
, &src
.data
, sizeof(*saddr
));
1102 filter
.id_src_mask
= src
.bitlen
;
1104 } else if (strcmp(*argv
, "dst") == 0) {
1107 get_prefix(&dst
, *argv
, preferred_family
);
1108 if (dst
.family
== AF_UNSPEC
)
1109 invarg("value after \"dst\" has an unrecognized address family", *argv
);
1111 *family
= dst
.family
;
1113 memcpy(&id
->daddr
, &dst
.data
, sizeof(id
->daddr
));
1115 filter
.id_dst_mask
= dst
.bitlen
;
1117 } else if (strcmp(*argv
, "proto") == 0) {
1122 ret
= xfrm_xfrmproto_getbyname(*argv
);
1124 invarg("XFRM-PROTO value is invalid", *argv
);
1126 id
->proto
= (__u8
)ret
;
1128 filter
.id_proto_mask
= XFRM_FILTER_MASK_FULL
;
1130 } else if (strcmp(*argv
, "spi") == 0) {
1132 if (get_be32(&id
->spi
, *argv
, 0))
1133 invarg("SPI value is invalid", *argv
);
1135 filter
.id_spi_mask
= XFRM_FILTER_MASK_FULL
;
1138 PREV_ARG(); /* back track */
1147 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1148 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv
);
1150 if (id
->spi
&& id
->proto
) {
1151 if (xfrm_xfrmproto_is_ro(id
->proto
)) {
1152 fprintf(stderr
, "\"spi\" is invalid with XFRM-PROTO value \"%s\"\n",
1153 strxf_xfrmproto(id
->proto
));
1155 } else if (id
->proto
== IPPROTO_COMP
&& ntohl(id
->spi
) >= 0x10000) {
1156 fprintf(stderr
, "SPI value is too large with XFRM-PROTO value \"%s\"\n",
1157 strxf_xfrmproto(id
->proto
));
1162 if (loose
== 0 && id
->proto
== 0)
1163 missarg("XFRM-PROTO");
1173 int xfrm_mode_parse(__u8
*mode
, int *argcp
, char ***argvp
)
1176 char **argv
= *argvp
;
1178 if (matches(*argv
, "transport") == 0)
1179 *mode
= XFRM_MODE_TRANSPORT
;
1180 else if (matches(*argv
, "tunnel") == 0)
1181 *mode
= XFRM_MODE_TUNNEL
;
1182 else if (matches(*argv
, "ro") == 0)
1183 *mode
= XFRM_MODE_ROUTEOPTIMIZATION
;
1184 else if (matches(*argv
, "in_trigger") == 0)
1185 *mode
= XFRM_MODE_IN_TRIGGER
;
1186 else if (matches(*argv
, "beet") == 0)
1187 *mode
= XFRM_MODE_BEET
;
1189 invarg("MODE value is invalid", *argv
);
1197 int xfrm_encap_type_parse(__u16
*type
, int *argcp
, char ***argvp
)
1200 char **argv
= *argvp
;
1202 if (strcmp(*argv
, "espinudp-nonike") == 0)
1204 else if (strcmp(*argv
, "espinudp") == 0)
1207 invarg("ENCAP-TYPE value is invalid", *argv
);
1215 /* NOTE: reqid is used by host-byte order */
1216 int xfrm_reqid_parse(__u32
*reqid
, int *argcp
, char ***argvp
)
1219 char **argv
= *argvp
;
1221 if (get_u32(reqid
, *argv
, 0))
1222 invarg("REQID value is invalid", *argv
);
1230 static int xfrm_selector_upspec_parse(struct xfrm_selector
*sel
,
1231 int *argcp
, char ***argvp
)
1234 char **argv
= *argvp
;
1235 char *sportp
= NULL
;
1236 char *dportp
= NULL
;
1239 char *grekey
= NULL
;
1242 if (strcmp(*argv
, "proto") == 0) {
1247 if (strcmp(*argv
, "any") == 0)
1250 struct protoent
*pp
;
1252 pp
= getprotobyname(*argv
);
1254 upspec
= pp
->p_proto
;
1256 if (get_u8(&upspec
, *argv
, 0))
1257 invarg("PROTO value is invalid", *argv
);
1260 sel
->proto
= upspec
;
1262 filter
.upspec_proto_mask
= XFRM_FILTER_MASK_FULL
;
1264 } else if (strcmp(*argv
, "sport") == 0) {
1269 if (get_be16(&sel
->sport
, *argv
, 0))
1270 invarg("value after \"sport\" is invalid", *argv
);
1272 sel
->sport_mask
= ~((__u16
)0);
1274 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1276 } else if (strcmp(*argv
, "dport") == 0) {
1281 if (get_be16(&sel
->dport
, *argv
, 0))
1282 invarg("value after \"dport\" is invalid", *argv
);
1284 sel
->dport_mask
= ~((__u16
)0);
1286 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1288 } else if (strcmp(*argv
, "type") == 0) {
1293 if (get_u16(&sel
->sport
, *argv
, 0) ||
1294 (sel
->sport
& ~((__u16
)0xff)))
1295 invarg("value after \"type\" is invalid", *argv
);
1296 sel
->sport
= htons(sel
->sport
);
1297 sel
->sport_mask
= ~((__u16
)0);
1299 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1302 } else if (strcmp(*argv
, "code") == 0) {
1307 if (get_u16(&sel
->dport
, *argv
, 0) ||
1308 (sel
->dport
& ~((__u16
)0xff)))
1309 invarg("value after \"code\" is invalid", *argv
);
1310 sel
->dport
= htons(sel
->dport
);
1311 sel
->dport_mask
= ~((__u16
)0);
1313 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1315 } else if (strcmp(*argv
, "key") == 0) {
1322 if (strchr(*argv
, '.'))
1323 uval
= htonl(get_addr32(*argv
));
1325 if (get_unsigned(&uval
, *argv
, 0) < 0) {
1326 fprintf(stderr
, "value after \"key\" is invalid\n");
1331 sel
->sport
= htons(uval
>> 16);
1332 sel
->dport
= htons(uval
& 0xffff);
1333 sel
->sport_mask
= ~((__u16
)0);
1334 sel
->dport_mask
= ~((__u16
)0);
1336 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1339 PREV_ARG(); /* back track */
1349 if (sportp
|| dportp
) {
1350 switch (sel
->proto
) {
1355 case IPPROTO_IP
: /* to allow shared SA for different protocols */
1358 fprintf(stderr
, "\"sport\" and \"dport\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1362 if (typep
|| codep
) {
1363 switch (sel
->proto
) {
1365 case IPPROTO_ICMPV6
:
1369 fprintf(stderr
, "\"type\" and \"code\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1374 switch (sel
->proto
) {
1378 fprintf(stderr
, "\"key\" is invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1389 int xfrm_selector_parse(struct xfrm_selector
*sel
, int *argcp
, char ***argvp
)
1392 char **argv
= *argvp
;
1393 inet_prefix dst
= {};
1394 inet_prefix src
= {};
1395 char *upspecp
= NULL
;
1398 if (strcmp(*argv
, "src") == 0) {
1401 get_prefix(&src
, *argv
, preferred_family
);
1402 if (src
.family
== AF_UNSPEC
)
1403 invarg("value after \"src\" has an unrecognized address family", *argv
);
1404 sel
->family
= src
.family
;
1406 memcpy(&sel
->saddr
, &src
.data
, sizeof(sel
->saddr
));
1407 sel
->prefixlen_s
= src
.bitlen
;
1409 filter
.sel_src_mask
= src
.bitlen
;
1411 } else if (strcmp(*argv
, "dst") == 0) {
1414 get_prefix(&dst
, *argv
, preferred_family
);
1415 if (dst
.family
== AF_UNSPEC
)
1416 invarg("value after \"dst\" has an unrecognized address family", *argv
);
1417 sel
->family
= dst
.family
;
1419 memcpy(&sel
->daddr
, &dst
.data
, sizeof(sel
->daddr
));
1420 sel
->prefixlen_d
= dst
.bitlen
;
1422 filter
.sel_dst_mask
= dst
.bitlen
;
1424 } else if (strcmp(*argv
, "dev") == 0) {
1429 if (strcmp(*argv
, "none") == 0)
1432 ifindex
= ll_name_to_index(*argv
);
1434 invarg("DEV value is invalid", *argv
);
1436 sel
->ifindex
= ifindex
;
1438 filter
.sel_dev_mask
= XFRM_FILTER_MASK_FULL
;
1442 PREV_ARG(); /* back track */
1446 xfrm_selector_upspec_parse(sel
, &argc
, &argv
);
1456 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1457 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv
);
1460 missarg("SELECTOR");
1468 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg
*lft
,
1469 int *argcp
, char ***argvp
)
1472 char **argv
= *argvp
;
1475 if (strcmp(*argv
, "time-soft") == 0) {
1477 ret
= get_u64(&lft
->soft_add_expires_seconds
, *argv
, 0);
1479 invarg("value after \"time-soft\" is invalid", *argv
);
1480 } else if (strcmp(*argv
, "time-hard") == 0) {
1482 ret
= get_u64(&lft
->hard_add_expires_seconds
, *argv
, 0);
1484 invarg("value after \"time-hard\" is invalid", *argv
);
1485 } else if (strcmp(*argv
, "time-use-soft") == 0) {
1487 ret
= get_u64(&lft
->soft_use_expires_seconds
, *argv
, 0);
1489 invarg("value after \"time-use-soft\" is invalid", *argv
);
1490 } else if (strcmp(*argv
, "time-use-hard") == 0) {
1492 ret
= get_u64(&lft
->hard_use_expires_seconds
, *argv
, 0);
1494 invarg("value after \"time-use-hard\" is invalid", *argv
);
1495 } else if (strcmp(*argv
, "byte-soft") == 0) {
1497 ret
= get_u64(&lft
->soft_byte_limit
, *argv
, 0);
1499 invarg("value after \"byte-soft\" is invalid", *argv
);
1500 } else if (strcmp(*argv
, "byte-hard") == 0) {
1502 ret
= get_u64(&lft
->hard_byte_limit
, *argv
, 0);
1504 invarg("value after \"byte-hard\" is invalid", *argv
);
1505 } else if (strcmp(*argv
, "packet-soft") == 0) {
1507 ret
= get_u64(&lft
->soft_packet_limit
, *argv
, 0);
1509 invarg("value after \"packet-soft\" is invalid", *argv
);
1510 } else if (strcmp(*argv
, "packet-hard") == 0) {
1512 ret
= get_u64(&lft
->hard_packet_limit
, *argv
, 0);
1514 invarg("value after \"packet-hard\" is invalid", *argv
);
1516 invarg("LIMIT value is invalid", *argv
);
1524 int do_xfrm(int argc
, char **argv
)
1526 memset(&filter
, 0, sizeof(filter
));
1531 if (matches(*argv
, "state") == 0 ||
1532 matches(*argv
, "sa") == 0)
1533 return do_xfrm_state(argc
-1, argv
+1);
1534 else if (matches(*argv
, "policy") == 0)
1535 return do_xfrm_policy(argc
-1, argv
+1);
1536 else if (matches(*argv
, "monitor") == 0)
1537 return do_xfrm_monitor(argc
-1, argv
+1);
1538 else if (matches(*argv
, "help") == 0) {
1540 fprintf(stderr
, "xfrm Object \"%s\" is unknown.\n", *argv
);