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/xfrm.h>
41 #include "ip_common.h"
43 #define STRBUF_SIZE (128)
44 #define STRBUF_CAT(buf, str) \
46 int rest = sizeof(buf) - 1 - strlen(buf); \
48 int len = strlen(str); \
51 strncat(buf, str, len); \
52 buf[sizeof(buf) - 1] = '\0'; \
56 struct xfrm_filter filter
;
58 static void usage(void) __attribute__((noreturn
));
60 static void usage(void)
63 "Usage: ip xfrm XFRM-OBJECT { COMMAND | help }\n"
64 "where XFRM-OBJECT := state | policy | monitor\n");
68 /* This is based on utils.c(inet_addr_match) */
69 int xfrm_addr_match(xfrm_address_t
*x1
, xfrm_address_t
*x2
, int bits
)
71 __u32
*a1
= (__u32
*)x1
;
72 __u32
*a2
= (__u32
*)x2
;
73 int words
= bits
>> 0x05;
78 if (memcmp(a1
, a2
, words
<< 2))
88 mask
= htonl((0xffffffff) << (0x20 - bits
));
97 int xfrm_xfrmproto_is_ipsec(__u8 proto
)
99 return (proto
== IPPROTO_ESP
||
100 proto
== IPPROTO_AH
||
101 proto
== IPPROTO_COMP
);
104 int xfrm_xfrmproto_is_ro(__u8 proto
)
106 return (proto
== IPPROTO_ROUTING
||
107 proto
== IPPROTO_DSTOPTS
);
115 static const struct typeent xfrmproto_types
[]= {
116 { "esp", IPPROTO_ESP
}, { "ah", IPPROTO_AH
}, { "comp", IPPROTO_COMP
},
117 { "route2", IPPROTO_ROUTING
}, { "hao", IPPROTO_DSTOPTS
},
118 { "ipsec-any", IPSEC_PROTO_ANY
},
122 int xfrm_xfrmproto_getbyname(char *name
)
127 const struct typeent
*t
= &xfrmproto_types
[i
];
128 if (!t
->t_name
|| t
->t_type
== -1)
131 if (strcmp(t
->t_name
, name
) == 0)
138 const char *strxf_xfrmproto(__u8 proto
)
144 const struct typeent
*t
= &xfrmproto_types
[i
];
145 if (!t
->t_name
|| t
->t_type
== -1)
148 if (t
->t_type
== proto
)
152 sprintf(str
, "%u", proto
);
156 static const struct typeent algo_types
[]= {
157 { "enc", XFRMA_ALG_CRYPT
}, { "auth", XFRMA_ALG_AUTH
},
158 { "comp", XFRMA_ALG_COMP
}, { "aead", XFRMA_ALG_AEAD
},
159 { "auth-trunc", XFRMA_ALG_AUTH_TRUNC
},
163 int xfrm_algotype_getbyname(char *name
)
168 const struct typeent
*t
= &algo_types
[i
];
169 if (!t
->t_name
|| t
->t_type
== -1)
172 if (strcmp(t
->t_name
, name
) == 0)
179 const char *strxf_algotype(int type
)
185 const struct typeent
*t
= &algo_types
[i
];
186 if (!t
->t_name
|| t
->t_type
== -1)
189 if (t
->t_type
== type
)
193 sprintf(str
, "%d", type
);
197 const char *strxf_mask8(__u8 mask
)
200 const int sn
= sizeof(mask
) * 8 - 1;
204 for (b
= (1 << sn
); b
> 0; b
>>= 1)
205 str
[i
++] = ((b
& mask
) ? '1' : '0');
211 const char *strxf_mask32(__u32 mask
)
215 sprintf(str
, "%.8x", mask
);
220 const char *strxf_share(__u8 share
)
228 case XFRM_SHARE_SESSION
:
229 strcpy(str
, "session");
231 case XFRM_SHARE_USER
:
234 case XFRM_SHARE_UNIQUE
:
235 strcpy(str
, "unique");
238 sprintf(str
, "%u", share
);
245 const char *strxf_proto(__u8 proto
)
251 pp
= getprotobynumber(proto
);
255 sprintf(buf
, "%u", proto
);
262 const char *strxf_ptype(__u8 ptype
)
267 case XFRM_POLICY_TYPE_MAIN
:
270 case XFRM_POLICY_TYPE_SUB
:
274 sprintf(str
, "%u", ptype
);
281 void xfrm_id_info_print(xfrm_address_t
*saddr
, struct xfrm_id
*id
,
282 __u8 mode
, __u32 reqid
, __u16 family
, int force_spi
,
283 FILE *fp
, const char *prefix
, const char *title
)
290 memset(abuf
, '\0', sizeof(abuf
));
291 fprintf(fp
, "src %s ", rt_addr_n2a(family
, sizeof(*saddr
),
292 saddr
, abuf
, sizeof(abuf
)));
293 memset(abuf
, '\0', sizeof(abuf
));
294 fprintf(fp
, "dst %s", rt_addr_n2a(family
, sizeof(id
->daddr
),
295 &id
->daddr
, abuf
, sizeof(abuf
)));
296 fprintf(fp
, "%s", _SL_
);
302 fprintf(fp
, "proto %s ", strxf_xfrmproto(id
->proto
));
304 if (show_stats
> 0 || force_spi
|| id
->spi
) {
305 __u32 spi
= ntohl(id
->spi
);
306 fprintf(fp
, "spi 0x%08x", spi
);
308 fprintf(fp
, "(%u)", spi
);
312 fprintf(fp
, "reqid %u", reqid
);
314 fprintf(fp
, "(0x%08x)", reqid
);
317 fprintf(fp
, "mode ");
319 case XFRM_MODE_TRANSPORT
:
320 fprintf(fp
, "transport");
322 case XFRM_MODE_TUNNEL
:
323 fprintf(fp
, "tunnel");
325 case XFRM_MODE_ROUTEOPTIMIZATION
:
328 case XFRM_MODE_IN_TRIGGER
:
329 fprintf(fp
, "in_trigger");
335 fprintf(fp
, "%u", mode
);
338 fprintf(fp
, "%s", _SL_
);
341 static const char *strxf_limit(__u64 limit
)
344 if (limit
== XFRM_INF
)
345 strcpy(str
, "(INF)");
347 sprintf(str
, "%llu", (unsigned long long) limit
);
352 void xfrm_stats_print(struct xfrm_stats
*s
, FILE *fp
, const char *prefix
)
356 fprintf(fp
, "stats:%s", _SL_
);
360 fprintf(fp
, " replay-window %u replay %u failed %u%s",
361 s
->replay_window
, s
->replay
, s
->integrity_failed
, _SL_
);
364 static const char *strxf_time(__u64 time
)
374 /* XXX: treat time in the same manner of kernel's
375 * net/xfrm/xfrm_{user,state}.c
380 strftime(str
, sizeof(str
), "%Y-%m-%d %T", tp
);
386 void xfrm_lifetime_print(struct xfrm_lifetime_cfg
*cfg
,
387 struct xfrm_lifetime_cur
*cur
,
388 FILE *fp
, const char *prefix
)
393 fprintf(fp
, "lifetime config:%s",_SL_
);
397 fprintf(fp
, " limit: soft %s(bytes),",
398 strxf_limit(cfg
->soft_byte_limit
));
399 fprintf(fp
, " hard %s(bytes)%s",
400 strxf_limit(cfg
->hard_byte_limit
), _SL_
);
404 fprintf(fp
, " limit: soft %s(packets),",
405 strxf_limit(cfg
->soft_packet_limit
));
406 fprintf(fp
, " hard %s(packets)%s",
407 strxf_limit(cfg
->hard_packet_limit
), _SL_
);
411 fprintf(fp
, " expire add: soft %llu(sec), hard %llu(sec)%s",
412 (unsigned long long) cfg
->soft_add_expires_seconds
,
413 (unsigned long long) cfg
->hard_add_expires_seconds
,
418 fprintf(fp
, " expire use: soft %llu(sec), hard %llu(sec)%s",
419 (unsigned long long) cfg
->soft_use_expires_seconds
,
420 (unsigned long long) cfg
->hard_use_expires_seconds
,
426 fprintf(fp
, "lifetime current:%s", _SL_
);
430 fprintf(fp
, " %llu(bytes), %llu(packets)%s",
431 (unsigned long long) cur
->bytes
,
432 (unsigned long long) cur
->packets
,
437 fprintf(fp
, " add %s ", strxf_time(cur
->add_time
));
438 fprintf(fp
, "use %s%s", strxf_time(cur
->use_time
), _SL_
);
442 void xfrm_selector_print(struct xfrm_selector
*sel
, __u16 family
,
443 FILE *fp
, const char *prefix
)
452 f
= preferred_family
;
457 memset(abuf
, '\0', sizeof(abuf
));
458 fprintf(fp
, "src %s/%u ",
459 rt_addr_n2a(f
, sizeof(sel
->saddr
), &sel
->saddr
,
463 memset(abuf
, '\0', sizeof(abuf
));
464 fprintf(fp
, "dst %s/%u ",
465 rt_addr_n2a(f
, sizeof(sel
->daddr
), &sel
->daddr
,
470 fprintf(fp
, "proto %s ", strxf_proto(sel
->proto
));
471 switch (sel
->proto
) {
478 fprintf(fp
, "sport %u ", ntohs(sel
->sport
));
480 fprintf(fp
, "dport %u ", ntohs(sel
->dport
));
484 /* type/code is stored at sport/dport in selector */
486 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
488 fprintf(fp
, "code %u ", ntohs(sel
->dport
));
491 if (sel
->sport_mask
|| sel
->dport_mask
)
492 fprintf(fp
, "key %u ",
493 (((__u32
)ntohs(sel
->sport
)) << 16) +
498 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
499 if (sel
->dport_mask
) {
501 fprintf(fp
, "(dport) 0x%.4x ", sel
->dport
);
506 if (sel
->ifindex
> 0)
507 fprintf(fp
, "dev %s ", ll_index_to_name(sel
->ifindex
));
510 fprintf(fp
, "uid %u", sel
->user
);
512 fprintf(fp
, "%s", _SL_
);
515 static void __xfrm_algo_print(struct xfrm_algo
*algo
, int type
, int len
,
516 FILE *fp
, const char *prefix
, int newline
)
524 fprintf(fp
, "%s ", strxf_algotype(type
));
526 if (len
< sizeof(*algo
)) {
527 fprintf(fp
, "(ERROR truncated)");
530 len
-= sizeof(*algo
);
532 fprintf(fp
, "%s ", algo
->alg_name
);
534 keylen
= algo
->alg_key_len
/ 8;
536 fprintf(fp
, "(ERROR truncated)");
542 for (i
= 0; i
< keylen
; i
++)
543 fprintf(fp
, "%.2x", (unsigned char)algo
->alg_key
[i
]);
546 fprintf(fp
, " (%d bits)", algo
->alg_key_len
);
551 fprintf(fp
, "%s", _SL_
);
554 static inline void xfrm_algo_print(struct xfrm_algo
*algo
, int type
, int len
,
555 FILE *fp
, const char *prefix
)
557 return __xfrm_algo_print(algo
, type
, len
, fp
, prefix
, 1);
560 static void xfrm_aead_print(struct xfrm_algo_aead
*algo
, int len
,
561 FILE *fp
, const char *prefix
)
563 struct xfrm_algo
*base_algo
= alloca(sizeof(*base_algo
) + algo
->alg_key_len
/ 8);
565 memcpy(base_algo
->alg_name
, algo
->alg_name
, sizeof(base_algo
->alg_name
));
566 base_algo
->alg_key_len
= algo
->alg_key_len
;
567 memcpy(base_algo
->alg_key
, algo
->alg_key
, algo
->alg_key_len
/ 8);
569 __xfrm_algo_print(base_algo
, XFRMA_ALG_AEAD
, len
, fp
, prefix
, 0);
571 fprintf(fp
, " %d", algo
->alg_icv_len
);
573 fprintf(fp
, "%s", _SL_
);
576 static void xfrm_auth_trunc_print(struct xfrm_algo_auth
*algo
, int len
,
577 FILE *fp
, const char *prefix
)
579 struct xfrm_algo
*base_algo
= alloca(sizeof(*base_algo
) + algo
->alg_key_len
/ 8);
581 memcpy(base_algo
->alg_name
, algo
->alg_name
, sizeof(base_algo
->alg_name
));
582 base_algo
->alg_key_len
= algo
->alg_key_len
;
583 memcpy(base_algo
->alg_key
, algo
->alg_key
, algo
->alg_key_len
/ 8);
585 __xfrm_algo_print(base_algo
, XFRMA_ALG_AUTH_TRUNC
, len
, fp
, prefix
, 0);
587 fprintf(fp
, " %d", algo
->alg_trunc_len
);
589 fprintf(fp
, "%s", _SL_
);
592 static void xfrm_tmpl_print(struct xfrm_user_tmpl
*tmpls
, int len
,
593 FILE *fp
, const char *prefix
)
595 int ntmpls
= len
/ sizeof(struct xfrm_user_tmpl
);
601 fprintf(fp
, "(ERROR \"tmpl\" truncated)");
602 fprintf(fp
, "%s", _SL_
);
606 for (i
= 0; i
< ntmpls
; i
++) {
607 struct xfrm_user_tmpl
*tmpl
= &tmpls
[i
];
612 xfrm_id_info_print(&tmpl
->saddr
, &tmpl
->id
, tmpl
->mode
,
613 tmpl
->reqid
, tmpl
->family
, 0, fp
, prefix
, "tmpl ");
615 if (show_stats
> 0 || tmpl
->optional
) {
619 switch (tmpl
->optional
) {
622 fprintf(fp
, "level required ");
625 fprintf(fp
, "level use ");
628 fprintf(fp
, "level %u ", tmpl
->optional
);
633 fprintf(fp
, "share %s ", strxf_share(tmpl
->share
));
635 fprintf(fp
, "%s", _SL_
);
638 if (show_stats
> 0) {
642 fprintf(fp
, "%s-mask %s ",
643 strxf_algotype(XFRMA_ALG_CRYPT
),
644 strxf_mask32(tmpl
->ealgos
));
645 fprintf(fp
, "%s-mask %s ",
646 strxf_algotype(XFRMA_ALG_AUTH
),
647 strxf_mask32(tmpl
->aalgos
));
648 fprintf(fp
, "%s-mask %s",
649 strxf_algotype(XFRMA_ALG_COMP
),
650 strxf_mask32(tmpl
->calgos
));
652 fprintf(fp
, "%s", _SL_
);
657 int xfrm_parse_mark(struct xfrm_mark
*mark
, int *argcp
, char ***argvp
)
660 char **argv
= *argvp
;
663 if (get_u32(&mark
->v
, *argv
, 0)) {
664 invarg("MARK value is invalid\n", *argv
);
668 else { /* last entry on parse line */
669 mark
->m
= 0xffffffff;
673 if (strcmp(*argv
, "mask") == 0) {
675 if (get_u32(&mark
->m
, *argv
, 0)) {
676 invarg("MASK value is invalid\n", *argv
);
679 mark
->m
= 0xffffffff;
690 void xfrm_xfrma_print(struct rtattr
*tb
[], __u16 family
,
691 FILE *fp
, const char *prefix
)
693 if (tb
[XFRMA_MARK
]) {
694 struct rtattr
*rta
= tb
[XFRMA_MARK
];
695 struct xfrm_mark
*m
= (struct xfrm_mark
*) RTA_DATA(rta
);
696 fprintf(fp
, "\tmark %#x/%#x", m
->v
, m
->m
);
697 fprintf(fp
, "%s", _SL_
);
700 if (tb
[XFRMA_ALG_AUTH
] && !tb
[XFRMA_ALG_AUTH_TRUNC
]) {
701 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH
];
702 xfrm_algo_print((struct xfrm_algo
*) 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
];
708 xfrm_auth_trunc_print((struct xfrm_algo_auth
*) RTA_DATA(rta
),
709 RTA_PAYLOAD(rta
), fp
, prefix
);
712 if (tb
[XFRMA_ALG_AEAD
]) {
713 struct rtattr
*rta
= tb
[XFRMA_ALG_AEAD
];
714 xfrm_aead_print((struct xfrm_algo_aead
*)RTA_DATA(rta
),
715 RTA_PAYLOAD(rta
), fp
, prefix
);
718 if (tb
[XFRMA_ALG_CRYPT
]) {
719 struct rtattr
*rta
= tb
[XFRMA_ALG_CRYPT
];
720 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
721 XFRMA_ALG_CRYPT
, RTA_PAYLOAD(rta
), fp
, prefix
);
724 if (tb
[XFRMA_ALG_COMP
]) {
725 struct rtattr
*rta
= tb
[XFRMA_ALG_COMP
];
726 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
727 XFRMA_ALG_COMP
, RTA_PAYLOAD(rta
), fp
, prefix
);
730 if (tb
[XFRMA_ENCAP
]) {
731 struct xfrm_encap_tmpl
*e
;
736 fprintf(fp
, "encap ");
738 if (RTA_PAYLOAD(tb
[XFRMA_ENCAP
]) < sizeof(*e
)) {
739 fprintf(fp
, "(ERROR truncated)");
740 fprintf(fp
, "%s", _SL_
);
743 e
= (struct xfrm_encap_tmpl
*) RTA_DATA(tb
[XFRMA_ENCAP
]);
745 fprintf(fp
, "type ");
746 switch (e
->encap_type
) {
748 fprintf(fp
, "espinudp-nonike ");
751 fprintf(fp
, "espinudp ");
754 fprintf(fp
, "%u ", e
->encap_type
);
757 fprintf(fp
, "sport %u ", ntohs(e
->encap_sport
));
758 fprintf(fp
, "dport %u ", ntohs(e
->encap_dport
));
760 memset(abuf
, '\0', sizeof(abuf
));
761 fprintf(fp
, "addr %s",
762 rt_addr_n2a(family
, sizeof(e
->encap_oa
), &e
->encap_oa
,
763 abuf
, sizeof(abuf
)));
764 fprintf(fp
, "%s", _SL_
);
767 if (tb
[XFRMA_TMPL
]) {
768 struct rtattr
*rta
= tb
[XFRMA_TMPL
];
769 xfrm_tmpl_print((struct xfrm_user_tmpl
*) RTA_DATA(rta
),
770 RTA_PAYLOAD(rta
), fp
, prefix
);
773 if (tb
[XFRMA_COADDR
]) {
781 coa
= (xfrm_address_t
*)RTA_DATA(tb
[XFRMA_COADDR
]);
783 if (RTA_PAYLOAD(tb
[XFRMA_COADDR
]) < sizeof(*coa
)) {
784 fprintf(fp
, "(ERROR truncated)");
785 fprintf(fp
, "%s", _SL_
);
789 memset(abuf
, '\0', sizeof(abuf
));
791 rt_addr_n2a(family
, sizeof(*coa
), coa
,
792 abuf
, sizeof(abuf
)));
793 fprintf(fp
, "%s", _SL_
);
796 if (tb
[XFRMA_LASTUSED
]) {
801 fprintf(fp
, "lastused ");
803 if (RTA_PAYLOAD(tb
[XFRMA_LASTUSED
]) < sizeof(lastused
)) {
804 fprintf(fp
, "(ERROR truncated)");
805 fprintf(fp
, "%s", _SL_
);
809 lastused
= rta_getattr_u64(tb
[XFRMA_LASTUSED
]);
811 fprintf(fp
, "%s", strxf_time(lastused
));
812 fprintf(fp
, "%s", _SL_
);
815 if (tb
[XFRMA_REPLAY_VAL
]) {
816 struct xfrm_replay_state
*replay
;
820 fprintf(fp
, "anti-replay context: ");
822 if (RTA_PAYLOAD(tb
[XFRMA_REPLAY_VAL
]) < sizeof(*replay
)) {
823 fprintf(fp
, "(ERROR truncated)");
824 fprintf(fp
, "%s", _SL_
);
828 replay
= (struct xfrm_replay_state
*)RTA_DATA(tb
[XFRMA_REPLAY_VAL
]);
829 fprintf(fp
, "seq 0x%x, oseq 0x%x, bitmap 0x%08x",
830 replay
->seq
, replay
->oseq
, replay
->bitmap
);
831 fprintf(fp
, "%s", _SL_
);
834 if (tb
[XFRMA_REPLAY_ESN_VAL
]) {
835 struct xfrm_replay_state_esn
*replay
;
840 fprintf(fp
, "anti-replay esn context:");
842 if (RTA_PAYLOAD(tb
[XFRMA_REPLAY_ESN_VAL
]) < sizeof(*replay
)) {
843 fprintf(fp
, "(ERROR truncated)");
844 fprintf(fp
, "%s", _SL_
);
847 fprintf(fp
, "%s", _SL_
);
849 replay
= (struct xfrm_replay_state_esn
*)RTA_DATA(tb
[XFRMA_REPLAY_ESN_VAL
]);
852 fprintf(fp
, " seq-hi 0x%x, seq 0x%x, oseq-hi 0x%0x, oseq 0x%0x",
853 replay
->seq_hi
, replay
->seq
, replay
->oseq_hi
,
855 fprintf(fp
, "%s", _SL_
);
858 fprintf(fp
, " replay_window %u, bitmap-length %u",
859 replay
->replay_window
, replay
->bmp_len
);
860 for (i
= replay
->bmp_len
, j
= 0; i
; i
--) {
862 fprintf(fp
, "%s", _SL_
);
867 fprintf(fp
, "%08x ", replay
->bmp
[i
- 1]);
869 fprintf(fp
, "%s", _SL_
);
873 static int xfrm_selector_iszero(struct xfrm_selector
*s
)
875 struct xfrm_selector s0
;
877 memset(&s0
, 0, sizeof(s0
));
879 return (memcmp(&s0
, s
, sizeof(s0
)) == 0);
882 void xfrm_state_info_print(struct xfrm_usersa_info
*xsinfo
,
883 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
886 char buf
[STRBUF_SIZE
];
887 int force_spi
= xfrm_xfrmproto_is_ipsec(xsinfo
->id
.proto
);
889 memset(buf
, '\0', sizeof(buf
));
891 xfrm_id_info_print(&xsinfo
->saddr
, &xsinfo
->id
, xsinfo
->mode
,
892 xsinfo
->reqid
, xsinfo
->family
, force_spi
, fp
,
896 STRBUF_CAT(buf
, prefix
);
897 STRBUF_CAT(buf
, "\t");
900 fprintf(fp
, "replay-window %u ", xsinfo
->replay_window
);
902 fprintf(fp
, "seq 0x%08u ", xsinfo
->seq
);
903 if (show_stats
> 0 || xsinfo
->flags
) {
904 __u8 flags
= xsinfo
->flags
;
906 fprintf(fp
, "flag ");
907 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOECN
, "noecn");
908 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_DECAP_DSCP
, "decap-dscp");
909 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOPMTUDISC
, "nopmtudisc");
910 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_WILDRECV
, "wildrecv");
911 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ICMP
, "icmp");
912 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_AF_UNSPEC
, "af-unspec");
913 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ALIGN4
, "align4");
914 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ESN
, "esn");
916 fprintf(fp
, "%x", flags
);
918 if (show_stats
> 0 && tb
[XFRMA_SA_EXTRA_FLAGS
]) {
919 __u32 extra_flags
= *(__u32
*)RTA_DATA(tb
[XFRMA_SA_EXTRA_FLAGS
]);
921 fprintf(fp
, "extra_flag ");
922 XFRM_FLAG_PRINT(fp
, extra_flags
,
923 XFRM_SA_XFLAG_DONT_ENCAP_DSCP
,
926 fprintf(fp
, "%x", extra_flags
);
929 fprintf(fp
, " (0x%s)", strxf_mask8(xsinfo
->flags
));
930 fprintf(fp
, "%s", _SL_
);
932 xfrm_xfrma_print(tb
, xsinfo
->family
, fp
, buf
);
934 if (!xfrm_selector_iszero(&xsinfo
->sel
)) {
935 char sbuf
[STRBUF_SIZE
];
937 memcpy(sbuf
, buf
, sizeof(sbuf
));
938 STRBUF_CAT(sbuf
, "sel ");
940 xfrm_selector_print(&xsinfo
->sel
, xsinfo
->family
, fp
, sbuf
);
943 if (show_stats
> 0) {
944 xfrm_lifetime_print(&xsinfo
->lft
, &xsinfo
->curlft
, fp
, buf
);
945 xfrm_stats_print(&xsinfo
->stats
, fp
, buf
);
948 if (tb
[XFRMA_SEC_CTX
]) {
949 struct xfrm_user_sec_ctx
*sctx
;
951 fprintf(fp
, "\tsecurity context ");
953 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
954 fprintf(fp
, "(ERROR truncated)");
956 sctx
= (struct xfrm_user_sec_ctx
*)RTA_DATA(tb
[XFRMA_SEC_CTX
]);
958 fprintf(fp
, "%s %s", (char *)(sctx
+ 1), _SL_
);
963 void xfrm_policy_info_print(struct xfrm_userpolicy_info
*xpinfo
,
964 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
967 char buf
[STRBUF_SIZE
];
969 memset(buf
, '\0', sizeof(buf
));
971 xfrm_selector_print(&xpinfo
->sel
, preferred_family
, fp
, title
);
973 if (tb
[XFRMA_SEC_CTX
]) {
974 struct xfrm_user_sec_ctx
*sctx
;
976 fprintf(fp
, "\tsecurity context ");
978 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
979 fprintf(fp
, "(ERROR truncated)");
981 sctx
= (struct xfrm_user_sec_ctx
*)RTA_DATA(tb
[XFRMA_SEC_CTX
]);
983 fprintf(fp
, "%s ", (char *)(sctx
+ 1));
984 fprintf(fp
, "%s", _SL_
);
988 STRBUF_CAT(buf
, prefix
);
989 STRBUF_CAT(buf
, "\t");
992 if (xpinfo
->dir
>= XFRM_POLICY_MAX
) {
993 xpinfo
->dir
-= XFRM_POLICY_MAX
;
994 fprintf(fp
, "socket ");
998 switch (xpinfo
->dir
) {
1002 case XFRM_POLICY_OUT
:
1005 case XFRM_POLICY_FWD
:
1009 fprintf(fp
, "%u", xpinfo
->dir
);
1014 switch (xpinfo
->action
) {
1015 case XFRM_POLICY_ALLOW
:
1017 fprintf(fp
, "action allow ");
1019 case XFRM_POLICY_BLOCK
:
1020 fprintf(fp
, "action block ");
1023 fprintf(fp
, "action %u ", xpinfo
->action
);
1028 fprintf(fp
, "index %u ", xpinfo
->index
);
1029 fprintf(fp
, "priority %u ", xpinfo
->priority
);
1031 if (tb
[XFRMA_POLICY_TYPE
]) {
1032 struct xfrm_userpolicy_type
*upt
;
1034 fprintf(fp
, "ptype ");
1036 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
))
1037 fprintf(fp
, "(ERROR truncated)");
1039 upt
= (struct xfrm_userpolicy_type
*)RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
1040 fprintf(fp
, "%s ", strxf_ptype(upt
->type
));
1044 fprintf(fp
, "share %s ", strxf_share(xpinfo
->share
));
1046 if (show_stats
> 0 || xpinfo
->flags
) {
1047 __u8 flags
= xpinfo
->flags
;
1049 fprintf(fp
, "flag ");
1050 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_LOCALOK
, "localok");
1051 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_ICMP
, "icmp");
1053 fprintf(fp
, "%x", flags
);
1056 fprintf(fp
, " (0x%s)", strxf_mask8(xpinfo
->flags
));
1057 fprintf(fp
, "%s", _SL_
);
1060 xfrm_lifetime_print(&xpinfo
->lft
, &xpinfo
->curlft
, fp
, buf
);
1062 xfrm_xfrma_print(tb
, xpinfo
->sel
.family
, fp
, buf
);
1065 int xfrm_id_parse(xfrm_address_t
*saddr
, struct xfrm_id
*id
, __u16
*family
,
1066 int loose
, int *argcp
, char ***argvp
)
1069 char **argv
= *argvp
;
1073 memset(&dst
, 0, sizeof(dst
));
1074 memset(&src
, 0, sizeof(src
));
1077 if (strcmp(*argv
, "src") == 0) {
1080 get_prefix(&src
, *argv
, preferred_family
);
1081 if (src
.family
== AF_UNSPEC
)
1082 invarg("value after \"src\" has an unrecognized address family", *argv
);
1084 *family
= src
.family
;
1086 memcpy(saddr
, &src
.data
, sizeof(*saddr
));
1088 filter
.id_src_mask
= src
.bitlen
;
1090 } else if (strcmp(*argv
, "dst") == 0) {
1093 get_prefix(&dst
, *argv
, preferred_family
);
1094 if (dst
.family
== AF_UNSPEC
)
1095 invarg("value after \"dst\" has an unrecognized address family", *argv
);
1097 *family
= dst
.family
;
1099 memcpy(&id
->daddr
, &dst
.data
, sizeof(id
->daddr
));
1101 filter
.id_dst_mask
= dst
.bitlen
;
1103 } else if (strcmp(*argv
, "proto") == 0) {
1108 ret
= xfrm_xfrmproto_getbyname(*argv
);
1110 invarg("XFRM-PROTO value is invalid", *argv
);
1112 id
->proto
= (__u8
)ret
;
1114 filter
.id_proto_mask
= XFRM_FILTER_MASK_FULL
;
1116 } else if (strcmp(*argv
, "spi") == 0) {
1120 if (get_u32(&spi
, *argv
, 0))
1121 invarg("SPI value is invalid", *argv
);
1126 filter
.id_spi_mask
= XFRM_FILTER_MASK_FULL
;
1129 PREV_ARG(); /* back track */
1138 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1139 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv
);
1141 if (id
->spi
&& id
->proto
) {
1142 if (xfrm_xfrmproto_is_ro(id
->proto
)) {
1143 fprintf(stderr
, "\"spi\" is invalid with XFRM-PROTO value \"%s\"\n",
1144 strxf_xfrmproto(id
->proto
));
1146 } else if (id
->proto
== IPPROTO_COMP
&& ntohl(id
->spi
) >= 0x10000) {
1147 fprintf(stderr
, "SPI value is too large with XFRM-PROTO value \"%s\"\n",
1148 strxf_xfrmproto(id
->proto
));
1153 if (loose
== 0 && id
->proto
== 0)
1154 missarg("XFRM-PROTO");
1164 int xfrm_mode_parse(__u8
*mode
, int *argcp
, char ***argvp
)
1167 char **argv
= *argvp
;
1169 if (matches(*argv
, "transport") == 0)
1170 *mode
= XFRM_MODE_TRANSPORT
;
1171 else if (matches(*argv
, "tunnel") == 0)
1172 *mode
= XFRM_MODE_TUNNEL
;
1173 else if (matches(*argv
, "ro") == 0)
1174 *mode
= XFRM_MODE_ROUTEOPTIMIZATION
;
1175 else if (matches(*argv
, "in_trigger") == 0)
1176 *mode
= XFRM_MODE_IN_TRIGGER
;
1177 else if (matches(*argv
, "beet") == 0)
1178 *mode
= XFRM_MODE_BEET
;
1180 invarg("MODE value is invalid", *argv
);
1188 int xfrm_encap_type_parse(__u16
*type
, int *argcp
, char ***argvp
)
1191 char **argv
= *argvp
;
1193 if (strcmp(*argv
, "espinudp-nonike") == 0)
1195 else if (strcmp(*argv
, "espinudp") == 0)
1198 invarg("ENCAP-TYPE value is invalid", *argv
);
1206 /* NOTE: reqid is used by host-byte order */
1207 int xfrm_reqid_parse(__u32
*reqid
, int *argcp
, char ***argvp
)
1210 char **argv
= *argvp
;
1212 if (get_u32(reqid
, *argv
, 0))
1213 invarg("REQID value is invalid", *argv
);
1221 static int xfrm_selector_upspec_parse(struct xfrm_selector
*sel
,
1222 int *argcp
, char ***argvp
)
1225 char **argv
= *argvp
;
1226 char *sportp
= NULL
;
1227 char *dportp
= NULL
;
1230 char *grekey
= NULL
;
1233 if (strcmp(*argv
, "proto") == 0) {
1238 if (strcmp(*argv
, "any") == 0)
1241 struct protoent
*pp
;
1242 pp
= getprotobyname(*argv
);
1244 upspec
= pp
->p_proto
;
1246 if (get_u8(&upspec
, *argv
, 0))
1247 invarg("PROTO value is invalid", *argv
);
1250 sel
->proto
= upspec
;
1252 filter
.upspec_proto_mask
= XFRM_FILTER_MASK_FULL
;
1254 } else if (strcmp(*argv
, "sport") == 0) {
1259 if (get_u16(&sel
->sport
, *argv
, 0))
1260 invarg("value after \"sport\" is invalid", *argv
);
1261 sel
->sport
= htons(sel
->sport
);
1263 sel
->sport_mask
= ~((__u16
)0);
1265 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1267 } else if (strcmp(*argv
, "dport") == 0) {
1272 if (get_u16(&sel
->dport
, *argv
, 0))
1273 invarg("value after \"dport\" is invalid", *argv
);
1274 sel
->dport
= htons(sel
->dport
);
1276 sel
->dport_mask
= ~((__u16
)0);
1278 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1280 } else if (strcmp(*argv
, "type") == 0) {
1285 if (get_u16(&sel
->sport
, *argv
, 0) ||
1286 (sel
->sport
& ~((__u16
)0xff)))
1287 invarg("value after \"type\" is invalid", *argv
);
1288 sel
->sport
= htons(sel
->sport
);
1289 sel
->sport_mask
= ~((__u16
)0);
1291 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1294 } else if (strcmp(*argv
, "code") == 0) {
1299 if (get_u16(&sel
->dport
, *argv
, 0) ||
1300 (sel
->dport
& ~((__u16
)0xff)))
1301 invarg("value after \"code\" is invalid", *argv
);
1302 sel
->dport
= htons(sel
->dport
);
1303 sel
->dport_mask
= ~((__u16
)0);
1305 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1307 } else if (strcmp(*argv
, "key") == 0) {
1314 if (strchr(*argv
, '.'))
1315 uval
= htonl(get_addr32(*argv
));
1317 if (get_unsigned(&uval
, *argv
, 0)<0) {
1318 fprintf(stderr
, "value after \"key\" is invalid\n");
1323 sel
->sport
= htons(uval
>> 16);
1324 sel
->dport
= htons(uval
& 0xffff);
1325 sel
->sport_mask
= ~((__u16
)0);
1326 sel
->dport_mask
= ~((__u16
)0);
1328 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1331 PREV_ARG(); /* back track */
1341 if (sportp
|| dportp
) {
1342 switch (sel
->proto
) {
1349 fprintf(stderr
, "\"sport\" and \"dport\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1353 if (typep
|| codep
) {
1354 switch (sel
->proto
) {
1356 case IPPROTO_ICMPV6
:
1360 fprintf(stderr
, "\"type\" and \"code\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1365 switch (sel
->proto
) {
1369 fprintf(stderr
, "\"key\" is invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1380 int xfrm_selector_parse(struct xfrm_selector
*sel
, int *argcp
, char ***argvp
)
1383 char **argv
= *argvp
;
1386 char *upspecp
= NULL
;
1388 memset(&dst
, 0, sizeof(dst
));
1389 memset(&src
, 0, sizeof(src
));
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
);