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 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 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 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 void xfrm_stats_print(struct xfrm_stats
*s
, FILE *fp
, const char *prefix
)
344 fprintf(fp
, "stats:%s", _SL_
);
348 fprintf(fp
, " replay-window %u replay %u failed %u%s",
349 s
->replay_window
, s
->replay
, s
->integrity_failed
, _SL_
);
352 static const char *strxf_time(__u64 time
)
362 /* XXX: treat time in the same manner of kernel's
363 * net/xfrm/xfrm_{user,state}.c
368 strftime(str
, sizeof(str
), "%Y-%m-%d %T", tp
);
374 void xfrm_lifetime_print(struct xfrm_lifetime_cfg
*cfg
,
375 struct xfrm_lifetime_cur
*cur
,
376 FILE *fp
, const char *prefix
)
381 fprintf(fp
, "lifetime config:%s", _SL_
);
385 fprintf(fp
, " limit: soft %s(bytes),",
386 strxf_limit(cfg
->soft_byte_limit
));
387 fprintf(fp
, " hard %s(bytes)%s",
388 strxf_limit(cfg
->hard_byte_limit
), _SL_
);
392 fprintf(fp
, " limit: soft %s(packets),",
393 strxf_limit(cfg
->soft_packet_limit
));
394 fprintf(fp
, " hard %s(packets)%s",
395 strxf_limit(cfg
->hard_packet_limit
), _SL_
);
399 fprintf(fp
, " expire add: soft %llu(sec), hard %llu(sec)%s",
400 (unsigned long long) cfg
->soft_add_expires_seconds
,
401 (unsigned long long) cfg
->hard_add_expires_seconds
,
406 fprintf(fp
, " expire use: soft %llu(sec), hard %llu(sec)%s",
407 (unsigned long long) cfg
->soft_use_expires_seconds
,
408 (unsigned long long) cfg
->hard_use_expires_seconds
,
414 fprintf(fp
, "lifetime current:%s", _SL_
);
418 fprintf(fp
, " %llu(bytes), %llu(packets)%s",
419 (unsigned long long) cur
->bytes
,
420 (unsigned long long) cur
->packets
,
425 fprintf(fp
, " add %s ", strxf_time(cur
->add_time
));
426 fprintf(fp
, "use %s%s", strxf_time(cur
->use_time
), _SL_
);
430 void xfrm_selector_print(struct xfrm_selector
*sel
, __u16 family
,
431 FILE *fp
, const char *prefix
)
439 f
= preferred_family
;
444 fprintf(fp
, "src %s/%u ",
445 rt_addr_n2a(f
, sizeof(sel
->saddr
), &sel
->saddr
),
448 fprintf(fp
, "dst %s/%u ",
449 rt_addr_n2a(f
, sizeof(sel
->daddr
), &sel
->daddr
),
453 fprintf(fp
, "proto %s ", strxf_proto(sel
->proto
));
454 switch (sel
->proto
) {
461 fprintf(fp
, "sport %u ", ntohs(sel
->sport
));
463 fprintf(fp
, "dport %u ", ntohs(sel
->dport
));
467 /* type/code is stored at sport/dport in selector */
469 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
471 fprintf(fp
, "code %u ", ntohs(sel
->dport
));
474 if (sel
->sport_mask
|| sel
->dport_mask
)
475 fprintf(fp
, "key %u ",
476 (((__u32
)ntohs(sel
->sport
)) << 16) +
481 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
482 if (sel
->dport_mask
) {
484 fprintf(fp
, "(dport) 0x%.4x ", sel
->dport
);
489 if (sel
->ifindex
> 0)
490 fprintf(fp
, "dev %s ", ll_index_to_name(sel
->ifindex
));
493 fprintf(fp
, "uid %u", sel
->user
);
495 fprintf(fp
, "%s", _SL_
);
498 static void __xfrm_algo_print(struct xfrm_algo
*algo
, int type
, int len
,
499 FILE *fp
, const char *prefix
, int newline
)
507 fprintf(fp
, "%s ", strxf_algotype(type
));
509 if (len
< sizeof(*algo
)) {
510 fprintf(fp
, "(ERROR truncated)");
513 len
-= sizeof(*algo
);
515 fprintf(fp
, "%s ", algo
->alg_name
);
517 keylen
= algo
->alg_key_len
/ 8;
519 fprintf(fp
, "(ERROR truncated)");
525 for (i
= 0; i
< keylen
; i
++)
526 fprintf(fp
, "%.2x", (unsigned char)algo
->alg_key
[i
]);
529 fprintf(fp
, " (%d bits)", algo
->alg_key_len
);
534 fprintf(fp
, "%s", _SL_
);
537 static inline void xfrm_algo_print(struct xfrm_algo
*algo
, int type
, int len
,
538 FILE *fp
, const char *prefix
)
540 return __xfrm_algo_print(algo
, type
, len
, fp
, prefix
, 1);
543 static void xfrm_aead_print(struct xfrm_algo_aead
*algo
, int len
,
544 FILE *fp
, const char *prefix
)
546 struct xfrm_algo
*base_algo
= alloca(sizeof(*base_algo
) + algo
->alg_key_len
/ 8);
548 memcpy(base_algo
->alg_name
, algo
->alg_name
, sizeof(base_algo
->alg_name
));
549 base_algo
->alg_key_len
= algo
->alg_key_len
;
550 memcpy(base_algo
->alg_key
, algo
->alg_key
, algo
->alg_key_len
/ 8);
552 __xfrm_algo_print(base_algo
, XFRMA_ALG_AEAD
, len
, fp
, prefix
, 0);
554 fprintf(fp
, " %d", algo
->alg_icv_len
);
556 fprintf(fp
, "%s", _SL_
);
559 static void xfrm_auth_trunc_print(struct xfrm_algo_auth
*algo
, int len
,
560 FILE *fp
, const char *prefix
)
562 struct xfrm_algo
*base_algo
= alloca(sizeof(*base_algo
) + algo
->alg_key_len
/ 8);
564 memcpy(base_algo
->alg_name
, algo
->alg_name
, sizeof(base_algo
->alg_name
));
565 base_algo
->alg_key_len
= algo
->alg_key_len
;
566 memcpy(base_algo
->alg_key
, algo
->alg_key
, algo
->alg_key_len
/ 8);
568 __xfrm_algo_print(base_algo
, XFRMA_ALG_AUTH_TRUNC
, len
, fp
, prefix
, 0);
570 fprintf(fp
, " %d", algo
->alg_trunc_len
);
572 fprintf(fp
, "%s", _SL_
);
575 static void xfrm_tmpl_print(struct xfrm_user_tmpl
*tmpls
, int len
,
576 FILE *fp
, const char *prefix
)
578 int ntmpls
= len
/ sizeof(struct xfrm_user_tmpl
);
584 fprintf(fp
, "(ERROR \"tmpl\" truncated)");
585 fprintf(fp
, "%s", _SL_
);
589 for (i
= 0; i
< ntmpls
; i
++) {
590 struct xfrm_user_tmpl
*tmpl
= &tmpls
[i
];
595 xfrm_id_info_print(&tmpl
->saddr
, &tmpl
->id
, tmpl
->mode
,
596 tmpl
->reqid
, tmpl
->family
, 0, fp
, prefix
, "tmpl ");
598 if (show_stats
> 0 || tmpl
->optional
) {
602 switch (tmpl
->optional
) {
605 fprintf(fp
, "level required ");
608 fprintf(fp
, "level use ");
611 fprintf(fp
, "level %u ", tmpl
->optional
);
616 fprintf(fp
, "share %s ", strxf_share(tmpl
->share
));
618 fprintf(fp
, "%s", _SL_
);
621 if (show_stats
> 0) {
625 fprintf(fp
, "%s-mask %s ",
626 strxf_algotype(XFRMA_ALG_CRYPT
),
627 strxf_mask32(tmpl
->ealgos
));
628 fprintf(fp
, "%s-mask %s ",
629 strxf_algotype(XFRMA_ALG_AUTH
),
630 strxf_mask32(tmpl
->aalgos
));
631 fprintf(fp
, "%s-mask %s",
632 strxf_algotype(XFRMA_ALG_COMP
),
633 strxf_mask32(tmpl
->calgos
));
635 fprintf(fp
, "%s", _SL_
);
640 static void xfrm_output_mark_print(struct rtattr
*tb
[], FILE *fp
)
642 __u32 output_mark
= rta_getattr_u32(tb
[XFRMA_OUTPUT_MARK
]);
644 fprintf(fp
, "output-mark 0x%x", output_mark
);
647 int xfrm_parse_mark(struct xfrm_mark
*mark
, int *argcp
, char ***argvp
)
650 char **argv
= *argvp
;
653 if (get_u32(&mark
->v
, *argv
, 0)) {
654 invarg("MARK value is invalid\n", *argv
);
658 else { /* last entry on parse line */
659 mark
->m
= 0xffffffff;
663 if (strcmp(*argv
, "mask") == 0) {
665 if (get_u32(&mark
->m
, *argv
, 0)) {
666 invarg("MASK value is invalid\n", *argv
);
669 mark
->m
= 0xffffffff;
680 void xfrm_xfrma_print(struct rtattr
*tb
[], __u16 family
,
681 FILE *fp
, const char *prefix
)
683 if (tb
[XFRMA_MARK
]) {
684 struct rtattr
*rta
= tb
[XFRMA_MARK
];
685 struct xfrm_mark
*m
= RTA_DATA(rta
);
687 fprintf(fp
, "\tmark %#x/%#x ", m
->v
, m
->m
);
689 if (tb
[XFRMA_OUTPUT_MARK
])
690 xfrm_output_mark_print(tb
, fp
);
691 fprintf(fp
, "%s", _SL_
);
692 } else if (tb
[XFRMA_OUTPUT_MARK
]) {
695 xfrm_output_mark_print(tb
, fp
);
696 fprintf(fp
, "%s", _SL_
);
699 if (tb
[XFRMA_ALG_AUTH
] && !tb
[XFRMA_ALG_AUTH_TRUNC
]) {
700 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH
];
702 xfrm_algo_print(RTA_DATA(rta
),
703 XFRMA_ALG_AUTH
, RTA_PAYLOAD(rta
), fp
, prefix
);
706 if (tb
[XFRMA_ALG_AUTH_TRUNC
]) {
707 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH_TRUNC
];
709 xfrm_auth_trunc_print(RTA_DATA(rta
),
710 RTA_PAYLOAD(rta
), fp
, prefix
);
713 if (tb
[XFRMA_ALG_AEAD
]) {
714 struct rtattr
*rta
= tb
[XFRMA_ALG_AEAD
];
716 xfrm_aead_print(RTA_DATA(rta
),
717 RTA_PAYLOAD(rta
), fp
, prefix
);
720 if (tb
[XFRMA_ALG_CRYPT
]) {
721 struct rtattr
*rta
= tb
[XFRMA_ALG_CRYPT
];
723 xfrm_algo_print(RTA_DATA(rta
),
724 XFRMA_ALG_CRYPT
, RTA_PAYLOAD(rta
), fp
, prefix
);
727 if (tb
[XFRMA_ALG_COMP
]) {
728 struct rtattr
*rta
= tb
[XFRMA_ALG_COMP
];
730 xfrm_algo_print(RTA_DATA(rta
),
731 XFRMA_ALG_COMP
, RTA_PAYLOAD(rta
), fp
, prefix
);
734 if (tb
[XFRMA_ENCAP
]) {
735 struct xfrm_encap_tmpl
*e
;
739 fprintf(fp
, "encap ");
741 if (RTA_PAYLOAD(tb
[XFRMA_ENCAP
]) < sizeof(*e
)) {
742 fprintf(fp
, "(ERROR truncated)");
743 fprintf(fp
, "%s", _SL_
);
746 e
= RTA_DATA(tb
[XFRMA_ENCAP
]);
748 fprintf(fp
, "type ");
749 switch (e
->encap_type
) {
751 fprintf(fp
, "espinudp-nonike ");
754 fprintf(fp
, "espinudp ");
757 fprintf(fp
, "%u ", e
->encap_type
);
760 fprintf(fp
, "sport %u ", ntohs(e
->encap_sport
));
761 fprintf(fp
, "dport %u ", ntohs(e
->encap_dport
));
763 fprintf(fp
, "addr %s",
764 rt_addr_n2a(family
, sizeof(e
->encap_oa
), &e
->encap_oa
));
765 fprintf(fp
, "%s", _SL_
);
768 if (tb
[XFRMA_TMPL
]) {
769 struct rtattr
*rta
= tb
[XFRMA_TMPL
];
771 xfrm_tmpl_print(RTA_DATA(rta
),
772 RTA_PAYLOAD(rta
), fp
, prefix
);
775 if (tb
[XFRMA_COADDR
]) {
776 const xfrm_address_t
*coa
;
782 coa
= RTA_DATA(tb
[XFRMA_COADDR
]);
783 if (RTA_PAYLOAD(tb
[XFRMA_COADDR
]) < sizeof(*coa
)) {
784 fprintf(fp
, "(ERROR truncated)");
785 fprintf(fp
, "%s", _SL_
);
790 rt_addr_n2a(family
, sizeof(*coa
), coa
));
791 fprintf(fp
, "%s", _SL_
);
794 if (tb
[XFRMA_LASTUSED
]) {
799 fprintf(fp
, "lastused ");
801 if (RTA_PAYLOAD(tb
[XFRMA_LASTUSED
]) < sizeof(lastused
)) {
802 fprintf(fp
, "(ERROR truncated)");
803 fprintf(fp
, "%s", _SL_
);
807 lastused
= rta_getattr_u64(tb
[XFRMA_LASTUSED
]);
809 fprintf(fp
, "%s", strxf_time(lastused
));
810 fprintf(fp
, "%s", _SL_
);
813 if (tb
[XFRMA_REPLAY_VAL
]) {
814 struct xfrm_replay_state
*replay
;
818 fprintf(fp
, "anti-replay context: ");
820 if (RTA_PAYLOAD(tb
[XFRMA_REPLAY_VAL
]) < sizeof(*replay
)) {
821 fprintf(fp
, "(ERROR truncated)");
822 fprintf(fp
, "%s", _SL_
);
826 replay
= RTA_DATA(tb
[XFRMA_REPLAY_VAL
]);
827 fprintf(fp
, "seq 0x%x, oseq 0x%x, bitmap 0x%08x",
828 replay
->seq
, replay
->oseq
, replay
->bitmap
);
829 fprintf(fp
, "%s", _SL_
);
832 if (tb
[XFRMA_REPLAY_ESN_VAL
]) {
833 struct xfrm_replay_state_esn
*replay
;
838 fprintf(fp
, "anti-replay esn context:");
840 if (RTA_PAYLOAD(tb
[XFRMA_REPLAY_ESN_VAL
]) < sizeof(*replay
)) {
841 fprintf(fp
, "(ERROR truncated)");
842 fprintf(fp
, "%s", _SL_
);
845 fprintf(fp
, "%s", _SL_
);
847 replay
= RTA_DATA(tb
[XFRMA_REPLAY_ESN_VAL
]);
850 fprintf(fp
, " seq-hi 0x%x, seq 0x%x, oseq-hi 0x%0x, oseq 0x%0x",
851 replay
->seq_hi
, replay
->seq
, replay
->oseq_hi
,
853 fprintf(fp
, "%s", _SL_
);
856 fprintf(fp
, " replay_window %u, bitmap-length %u",
857 replay
->replay_window
, replay
->bmp_len
);
858 for (i
= replay
->bmp_len
, j
= 0; i
; i
--) {
860 fprintf(fp
, "%s", _SL_
);
865 fprintf(fp
, "%08x ", replay
->bmp
[i
- 1]);
867 fprintf(fp
, "%s", _SL_
);
869 if (tb
[XFRMA_OFFLOAD_DEV
]) {
870 struct xfrm_user_offload
*xuo
;
874 fprintf(fp
, "crypto offload parameters: ");
876 if (RTA_PAYLOAD(tb
[XFRMA_OFFLOAD_DEV
]) < sizeof(*xuo
)) {
877 fprintf(fp
, "(ERROR truncated)");
878 fprintf(fp
, "%s", _SL_
);
882 xuo
= (struct xfrm_user_offload
*)
883 RTA_DATA(tb
[XFRMA_OFFLOAD_DEV
]);
884 fprintf(fp
, "dev %s dir %s", ll_index_to_name(xuo
->ifindex
),
885 (xuo
->flags
& XFRM_OFFLOAD_INBOUND
) ? "in" : "out");
886 fprintf(fp
, "%s", _SL_
);
890 static int xfrm_selector_iszero(struct xfrm_selector
*s
)
892 struct xfrm_selector s0
= {};
894 return (memcmp(&s0
, s
, sizeof(s0
)) == 0);
897 void xfrm_state_info_print(struct xfrm_usersa_info
*xsinfo
,
898 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
901 char buf
[STRBUF_SIZE
] = {};
902 int force_spi
= xfrm_xfrmproto_is_ipsec(xsinfo
->id
.proto
);
904 xfrm_id_info_print(&xsinfo
->saddr
, &xsinfo
->id
, xsinfo
->mode
,
905 xsinfo
->reqid
, xsinfo
->family
, force_spi
, fp
,
909 strlcat(buf
, prefix
, sizeof(buf
));
910 strlcat(buf
, "\t", sizeof(buf
));
913 fprintf(fp
, "replay-window %u ", xsinfo
->replay_window
);
915 fprintf(fp
, "seq 0x%08u ", xsinfo
->seq
);
916 if (show_stats
> 0 || xsinfo
->flags
) {
917 __u8 flags
= xsinfo
->flags
;
919 fprintf(fp
, "flag ");
920 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOECN
, "noecn");
921 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_DECAP_DSCP
, "decap-dscp");
922 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOPMTUDISC
, "nopmtudisc");
923 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_WILDRECV
, "wildrecv");
924 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ICMP
, "icmp");
925 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_AF_UNSPEC
, "af-unspec");
926 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ALIGN4
, "align4");
927 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ESN
, "esn");
929 fprintf(fp
, "%x", flags
);
931 if (show_stats
> 0 && tb
[XFRMA_SA_EXTRA_FLAGS
]) {
932 __u32 extra_flags
= rta_getattr_u32(tb
[XFRMA_SA_EXTRA_FLAGS
]);
934 fprintf(fp
, "extra_flag ");
935 XFRM_FLAG_PRINT(fp
, extra_flags
,
936 XFRM_SA_XFLAG_DONT_ENCAP_DSCP
,
939 fprintf(fp
, "%x", extra_flags
);
942 fprintf(fp
, " (0x%s)", strxf_mask8(xsinfo
->flags
));
943 fprintf(fp
, "%s", _SL_
);
945 xfrm_xfrma_print(tb
, xsinfo
->family
, fp
, buf
);
947 if (!xfrm_selector_iszero(&xsinfo
->sel
)) {
948 char sbuf
[STRBUF_SIZE
];
950 memcpy(sbuf
, buf
, sizeof(sbuf
));
951 strlcat(sbuf
, "sel ", sizeof(sbuf
));
953 xfrm_selector_print(&xsinfo
->sel
, xsinfo
->family
, fp
, sbuf
);
956 if (show_stats
> 0) {
957 xfrm_lifetime_print(&xsinfo
->lft
, &xsinfo
->curlft
, fp
, buf
);
958 xfrm_stats_print(&xsinfo
->stats
, fp
, buf
);
961 if (tb
[XFRMA_SEC_CTX
]) {
962 struct xfrm_user_sec_ctx
*sctx
;
964 fprintf(fp
, "\tsecurity context ");
966 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
967 fprintf(fp
, "(ERROR truncated)");
969 sctx
= RTA_DATA(tb
[XFRMA_SEC_CTX
]);
971 fprintf(fp
, "%s %s", (char *)(sctx
+ 1), _SL_
);
976 void xfrm_policy_info_print(struct xfrm_userpolicy_info
*xpinfo
,
977 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
980 char buf
[STRBUF_SIZE
] = {};
982 xfrm_selector_print(&xpinfo
->sel
, preferred_family
, fp
, title
);
984 if (tb
[XFRMA_SEC_CTX
]) {
985 struct xfrm_user_sec_ctx
*sctx
;
987 fprintf(fp
, "\tsecurity context ");
989 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
990 fprintf(fp
, "(ERROR truncated)");
992 sctx
= RTA_DATA(tb
[XFRMA_SEC_CTX
]);
994 fprintf(fp
, "%s ", (char *)(sctx
+ 1));
995 fprintf(fp
, "%s", _SL_
);
999 strlcat(buf
, prefix
, sizeof(buf
));
1000 strlcat(buf
, "\t", sizeof(buf
));
1003 if (xpinfo
->dir
>= XFRM_POLICY_MAX
) {
1004 xpinfo
->dir
-= XFRM_POLICY_MAX
;
1005 fprintf(fp
, "socket ");
1007 fprintf(fp
, "dir ");
1009 switch (xpinfo
->dir
) {
1010 case XFRM_POLICY_IN
:
1013 case XFRM_POLICY_OUT
:
1016 case XFRM_POLICY_FWD
:
1020 fprintf(fp
, "%u", xpinfo
->dir
);
1025 switch (xpinfo
->action
) {
1026 case XFRM_POLICY_ALLOW
:
1028 fprintf(fp
, "action allow ");
1030 case XFRM_POLICY_BLOCK
:
1031 fprintf(fp
, "action block ");
1034 fprintf(fp
, "action %u ", xpinfo
->action
);
1039 fprintf(fp
, "index %u ", xpinfo
->index
);
1040 fprintf(fp
, "priority %u ", xpinfo
->priority
);
1042 if (tb
[XFRMA_POLICY_TYPE
]) {
1043 struct xfrm_userpolicy_type
*upt
;
1045 fprintf(fp
, "ptype ");
1047 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
))
1048 fprintf(fp
, "(ERROR truncated)");
1050 upt
= RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
1051 fprintf(fp
, "%s ", strxf_ptype(upt
->type
));
1055 fprintf(fp
, "share %s ", strxf_share(xpinfo
->share
));
1057 if (show_stats
> 0 || xpinfo
->flags
) {
1058 __u8 flags
= xpinfo
->flags
;
1060 fprintf(fp
, "flag ");
1061 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_LOCALOK
, "localok");
1062 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_ICMP
, "icmp");
1064 fprintf(fp
, "%x", flags
);
1067 fprintf(fp
, " (0x%s)", strxf_mask8(xpinfo
->flags
));
1068 fprintf(fp
, "%s", _SL_
);
1071 xfrm_lifetime_print(&xpinfo
->lft
, &xpinfo
->curlft
, fp
, buf
);
1073 xfrm_xfrma_print(tb
, xpinfo
->sel
.family
, fp
, buf
);
1076 int xfrm_id_parse(xfrm_address_t
*saddr
, struct xfrm_id
*id
, __u16
*family
,
1077 int loose
, int *argcp
, char ***argvp
)
1080 char **argv
= *argvp
;
1081 inet_prefix dst
= {};
1082 inet_prefix src
= {};
1085 if (strcmp(*argv
, "src") == 0) {
1088 get_prefix(&src
, *argv
, preferred_family
);
1089 if (src
.family
== AF_UNSPEC
)
1090 invarg("value after \"src\" has an unrecognized address family", *argv
);
1092 *family
= src
.family
;
1094 memcpy(saddr
, &src
.data
, sizeof(*saddr
));
1096 filter
.id_src_mask
= src
.bitlen
;
1098 } else if (strcmp(*argv
, "dst") == 0) {
1101 get_prefix(&dst
, *argv
, preferred_family
);
1102 if (dst
.family
== AF_UNSPEC
)
1103 invarg("value after \"dst\" has an unrecognized address family", *argv
);
1105 *family
= dst
.family
;
1107 memcpy(&id
->daddr
, &dst
.data
, sizeof(id
->daddr
));
1109 filter
.id_dst_mask
= dst
.bitlen
;
1111 } else if (strcmp(*argv
, "proto") == 0) {
1116 ret
= xfrm_xfrmproto_getbyname(*argv
);
1118 invarg("XFRM-PROTO value is invalid", *argv
);
1120 id
->proto
= (__u8
)ret
;
1122 filter
.id_proto_mask
= XFRM_FILTER_MASK_FULL
;
1124 } else if (strcmp(*argv
, "spi") == 0) {
1126 if (get_be32(&id
->spi
, *argv
, 0))
1127 invarg("SPI value is invalid", *argv
);
1129 filter
.id_spi_mask
= XFRM_FILTER_MASK_FULL
;
1132 PREV_ARG(); /* back track */
1141 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1142 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv
);
1144 if (id
->spi
&& id
->proto
) {
1145 if (xfrm_xfrmproto_is_ro(id
->proto
)) {
1146 fprintf(stderr
, "\"spi\" is invalid with XFRM-PROTO value \"%s\"\n",
1147 strxf_xfrmproto(id
->proto
));
1149 } else if (id
->proto
== IPPROTO_COMP
&& ntohl(id
->spi
) >= 0x10000) {
1150 fprintf(stderr
, "SPI value is too large with XFRM-PROTO value \"%s\"\n",
1151 strxf_xfrmproto(id
->proto
));
1156 if (loose
== 0 && id
->proto
== 0)
1157 missarg("XFRM-PROTO");
1167 int xfrm_mode_parse(__u8
*mode
, int *argcp
, char ***argvp
)
1170 char **argv
= *argvp
;
1172 if (matches(*argv
, "transport") == 0)
1173 *mode
= XFRM_MODE_TRANSPORT
;
1174 else if (matches(*argv
, "tunnel") == 0)
1175 *mode
= XFRM_MODE_TUNNEL
;
1176 else if (matches(*argv
, "ro") == 0)
1177 *mode
= XFRM_MODE_ROUTEOPTIMIZATION
;
1178 else if (matches(*argv
, "in_trigger") == 0)
1179 *mode
= XFRM_MODE_IN_TRIGGER
;
1180 else if (matches(*argv
, "beet") == 0)
1181 *mode
= XFRM_MODE_BEET
;
1183 invarg("MODE value is invalid", *argv
);
1191 int xfrm_encap_type_parse(__u16
*type
, int *argcp
, char ***argvp
)
1194 char **argv
= *argvp
;
1196 if (strcmp(*argv
, "espinudp-nonike") == 0)
1198 else if (strcmp(*argv
, "espinudp") == 0)
1201 invarg("ENCAP-TYPE value is invalid", *argv
);
1209 /* NOTE: reqid is used by host-byte order */
1210 int xfrm_reqid_parse(__u32
*reqid
, int *argcp
, char ***argvp
)
1213 char **argv
= *argvp
;
1215 if (get_u32(reqid
, *argv
, 0))
1216 invarg("REQID value is invalid", *argv
);
1224 static int xfrm_selector_upspec_parse(struct xfrm_selector
*sel
,
1225 int *argcp
, char ***argvp
)
1228 char **argv
= *argvp
;
1229 char *sportp
= NULL
;
1230 char *dportp
= NULL
;
1233 char *grekey
= NULL
;
1236 if (strcmp(*argv
, "proto") == 0) {
1241 if (strcmp(*argv
, "any") == 0)
1244 struct protoent
*pp
;
1246 pp
= getprotobyname(*argv
);
1248 upspec
= pp
->p_proto
;
1250 if (get_u8(&upspec
, *argv
, 0))
1251 invarg("PROTO value is invalid", *argv
);
1254 sel
->proto
= upspec
;
1256 filter
.upspec_proto_mask
= XFRM_FILTER_MASK_FULL
;
1258 } else if (strcmp(*argv
, "sport") == 0) {
1263 if (get_be16(&sel
->sport
, *argv
, 0))
1264 invarg("value after \"sport\" is invalid", *argv
);
1266 sel
->sport_mask
= ~((__u16
)0);
1268 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1270 } else if (strcmp(*argv
, "dport") == 0) {
1275 if (get_be16(&sel
->dport
, *argv
, 0))
1276 invarg("value after \"dport\" is invalid", *argv
);
1278 sel
->dport_mask
= ~((__u16
)0);
1280 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1282 } else if (strcmp(*argv
, "type") == 0) {
1287 if (get_u16(&sel
->sport
, *argv
, 0) ||
1288 (sel
->sport
& ~((__u16
)0xff)))
1289 invarg("value after \"type\" is invalid", *argv
);
1290 sel
->sport
= htons(sel
->sport
);
1291 sel
->sport_mask
= ~((__u16
)0);
1293 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1296 } else if (strcmp(*argv
, "code") == 0) {
1301 if (get_u16(&sel
->dport
, *argv
, 0) ||
1302 (sel
->dport
& ~((__u16
)0xff)))
1303 invarg("value after \"code\" is invalid", *argv
);
1304 sel
->dport
= htons(sel
->dport
);
1305 sel
->dport_mask
= ~((__u16
)0);
1307 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1309 } else if (strcmp(*argv
, "key") == 0) {
1316 if (strchr(*argv
, '.'))
1317 uval
= htonl(get_addr32(*argv
));
1319 if (get_unsigned(&uval
, *argv
, 0) < 0) {
1320 fprintf(stderr
, "value after \"key\" is invalid\n");
1325 sel
->sport
= htons(uval
>> 16);
1326 sel
->dport
= htons(uval
& 0xffff);
1327 sel
->sport_mask
= ~((__u16
)0);
1328 sel
->dport_mask
= ~((__u16
)0);
1330 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1333 PREV_ARG(); /* back track */
1343 if (sportp
|| dportp
) {
1344 switch (sel
->proto
) {
1349 case IPPROTO_IP
: /* to allow shared SA for different protocols */
1352 fprintf(stderr
, "\"sport\" and \"dport\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1356 if (typep
|| codep
) {
1357 switch (sel
->proto
) {
1359 case IPPROTO_ICMPV6
:
1363 fprintf(stderr
, "\"type\" and \"code\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1368 switch (sel
->proto
) {
1372 fprintf(stderr
, "\"key\" is invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1383 int xfrm_selector_parse(struct xfrm_selector
*sel
, int *argcp
, char ***argvp
)
1386 char **argv
= *argvp
;
1387 inet_prefix dst
= {};
1388 inet_prefix src
= {};
1389 char *upspecp
= NULL
;
1392 if (strcmp(*argv
, "src") == 0) {
1395 get_prefix(&src
, *argv
, preferred_family
);
1396 if (src
.family
== AF_UNSPEC
)
1397 invarg("value after \"src\" has an unrecognized address family", *argv
);
1398 sel
->family
= src
.family
;
1400 memcpy(&sel
->saddr
, &src
.data
, sizeof(sel
->saddr
));
1401 sel
->prefixlen_s
= src
.bitlen
;
1403 filter
.sel_src_mask
= src
.bitlen
;
1405 } else if (strcmp(*argv
, "dst") == 0) {
1408 get_prefix(&dst
, *argv
, preferred_family
);
1409 if (dst
.family
== AF_UNSPEC
)
1410 invarg("value after \"dst\" has an unrecognized address family", *argv
);
1411 sel
->family
= dst
.family
;
1413 memcpy(&sel
->daddr
, &dst
.data
, sizeof(sel
->daddr
));
1414 sel
->prefixlen_d
= dst
.bitlen
;
1416 filter
.sel_dst_mask
= dst
.bitlen
;
1418 } else if (strcmp(*argv
, "dev") == 0) {
1423 if (strcmp(*argv
, "none") == 0)
1426 ifindex
= ll_name_to_index(*argv
);
1428 invarg("DEV value is invalid", *argv
);
1430 sel
->ifindex
= ifindex
;
1432 filter
.sel_dev_mask
= XFRM_FILTER_MASK_FULL
;
1436 PREV_ARG(); /* back track */
1440 xfrm_selector_upspec_parse(sel
, &argc
, &argv
);
1450 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1451 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv
);
1454 missarg("SELECTOR");
1462 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg
*lft
,
1463 int *argcp
, char ***argvp
)
1466 char **argv
= *argvp
;
1469 if (strcmp(*argv
, "time-soft") == 0) {
1471 ret
= get_u64(&lft
->soft_add_expires_seconds
, *argv
, 0);
1473 invarg("value after \"time-soft\" is invalid", *argv
);
1474 } else if (strcmp(*argv
, "time-hard") == 0) {
1476 ret
= get_u64(&lft
->hard_add_expires_seconds
, *argv
, 0);
1478 invarg("value after \"time-hard\" is invalid", *argv
);
1479 } else if (strcmp(*argv
, "time-use-soft") == 0) {
1481 ret
= get_u64(&lft
->soft_use_expires_seconds
, *argv
, 0);
1483 invarg("value after \"time-use-soft\" is invalid", *argv
);
1484 } else if (strcmp(*argv
, "time-use-hard") == 0) {
1486 ret
= get_u64(&lft
->hard_use_expires_seconds
, *argv
, 0);
1488 invarg("value after \"time-use-hard\" is invalid", *argv
);
1489 } else if (strcmp(*argv
, "byte-soft") == 0) {
1491 ret
= get_u64(&lft
->soft_byte_limit
, *argv
, 0);
1493 invarg("value after \"byte-soft\" is invalid", *argv
);
1494 } else if (strcmp(*argv
, "byte-hard") == 0) {
1496 ret
= get_u64(&lft
->hard_byte_limit
, *argv
, 0);
1498 invarg("value after \"byte-hard\" is invalid", *argv
);
1499 } else if (strcmp(*argv
, "packet-soft") == 0) {
1501 ret
= get_u64(&lft
->soft_packet_limit
, *argv
, 0);
1503 invarg("value after \"packet-soft\" is invalid", *argv
);
1504 } else if (strcmp(*argv
, "packet-hard") == 0) {
1506 ret
= get_u64(&lft
->hard_packet_limit
, *argv
, 0);
1508 invarg("value after \"packet-hard\" is invalid", *argv
);
1510 invarg("LIMIT value is invalid", *argv
);
1518 int do_xfrm(int argc
, char **argv
)
1520 memset(&filter
, 0, sizeof(filter
));
1525 if (matches(*argv
, "state") == 0 ||
1526 matches(*argv
, "sa") == 0)
1527 return do_xfrm_state(argc
-1, argv
+1);
1528 else if (matches(*argv
, "policy") == 0)
1529 return do_xfrm_policy(argc
-1, argv
+1);
1530 else if (matches(*argv
, "monitor") == 0)
1531 return do_xfrm_monitor(argc
-1, argv
+1);
1532 else if (matches(*argv
, "help") == 0) {
1534 fprintf(stderr
, "xfrm Object \"%s\" is unknown.\n", *argv
);