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)
43 #define STRBUF_CAT(buf, str) \
45 int rest = sizeof(buf) - 1 - strlen(buf); \
47 int len = strlen(str); \
50 strncat(buf, str, len); \
51 buf[sizeof(buf) - 1] = '\0'; \
55 struct xfrm_filter filter
;
57 static void usage(void) __attribute__((noreturn
));
59 static void usage(void)
62 "Usage: ip xfrm XFRM-OBJECT { COMMAND | help }\n"
63 "where XFRM-OBJECT := state | policy | monitor\n");
67 /* This is based on utils.c(inet_addr_match) */
68 int xfrm_addr_match(xfrm_address_t
*x1
, xfrm_address_t
*x2
, int bits
)
70 __u32
*a1
= (__u32
*)x1
;
71 __u32
*a2
= (__u32
*)x2
;
72 int words
= bits
>> 0x05;
77 if (memcmp(a1
, a2
, words
<< 2))
87 mask
= htonl((0xffffffff) << (0x20 - bits
));
96 int xfrm_xfrmproto_is_ipsec(__u8 proto
)
98 return (proto
== IPPROTO_ESP
||
99 proto
== IPPROTO_AH
||
100 proto
== IPPROTO_COMP
);
103 int xfrm_xfrmproto_is_ro(__u8 proto
)
105 return (proto
== IPPROTO_ROUTING
||
106 proto
== IPPROTO_DSTOPTS
);
114 static const struct typeent xfrmproto_types
[] = {
115 { "esp", IPPROTO_ESP
}, { "ah", IPPROTO_AH
}, { "comp", IPPROTO_COMP
},
116 { "route2", IPPROTO_ROUTING
}, { "hao", IPPROTO_DSTOPTS
},
117 { "ipsec-any", IPSEC_PROTO_ANY
},
121 int xfrm_xfrmproto_getbyname(char *name
)
126 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
];
146 if (!t
->t_name
|| t
->t_type
== -1)
149 if (t
->t_type
== proto
)
153 sprintf(str
, "%u", proto
);
157 static const struct typeent algo_types
[] = {
158 { "enc", XFRMA_ALG_CRYPT
}, { "auth", XFRMA_ALG_AUTH
},
159 { "comp", XFRMA_ALG_COMP
}, { "aead", XFRMA_ALG_AEAD
},
160 { "auth-trunc", XFRMA_ALG_AUTH_TRUNC
},
164 int xfrm_algotype_getbyname(char *name
)
169 const struct typeent
*t
= &algo_types
[i
];
171 if (!t
->t_name
|| t
->t_type
== -1)
174 if (strcmp(t
->t_name
, name
) == 0)
181 const char *strxf_algotype(int type
)
187 const struct typeent
*t
= &algo_types
[i
];
189 if (!t
->t_name
|| t
->t_type
== -1)
192 if (t
->t_type
== type
)
196 sprintf(str
, "%d", type
);
200 const char *strxf_mask8(__u8 mask
)
203 const int sn
= sizeof(mask
) * 8 - 1;
207 for (b
= (1 << sn
); b
> 0; b
>>= 1)
208 str
[i
++] = ((b
& mask
) ? '1' : '0');
214 const char *strxf_mask32(__u32 mask
)
218 sprintf(str
, "%.8x", mask
);
223 const char *strxf_share(__u8 share
)
231 case XFRM_SHARE_SESSION
:
232 strcpy(str
, "session");
234 case XFRM_SHARE_USER
:
237 case XFRM_SHARE_UNIQUE
:
238 strcpy(str
, "unique");
241 sprintf(str
, "%u", share
);
248 const char *strxf_proto(__u8 proto
)
254 pp
= getprotobynumber(proto
);
258 sprintf(buf
, "%u", proto
);
265 const char *strxf_ptype(__u8 ptype
)
270 case XFRM_POLICY_TYPE_MAIN
:
273 case XFRM_POLICY_TYPE_SUB
:
277 sprintf(str
, "%u", ptype
);
284 void xfrm_id_info_print(xfrm_address_t
*saddr
, struct xfrm_id
*id
,
285 __u8 mode
, __u32 reqid
, __u16 family
, int force_spi
,
286 FILE *fp
, const char *prefix
, const char *title
)
291 fprintf(fp
, "src %s ", rt_addr_n2a(family
, sizeof(*saddr
), saddr
));
292 fprintf(fp
, "dst %s", rt_addr_n2a(family
, sizeof(id
->daddr
), &id
->daddr
));
293 fprintf(fp
, "%s", _SL_
);
299 fprintf(fp
, "proto %s ", strxf_xfrmproto(id
->proto
));
301 if (show_stats
> 0 || force_spi
|| id
->spi
) {
302 __u32 spi
= ntohl(id
->spi
);
304 fprintf(fp
, "spi 0x%08x", spi
);
306 fprintf(fp
, "(%u)", spi
);
310 fprintf(fp
, "reqid %u", reqid
);
312 fprintf(fp
, "(0x%08x)", reqid
);
315 fprintf(fp
, "mode ");
317 case XFRM_MODE_TRANSPORT
:
318 fprintf(fp
, "transport");
320 case XFRM_MODE_TUNNEL
:
321 fprintf(fp
, "tunnel");
323 case XFRM_MODE_ROUTEOPTIMIZATION
:
326 case XFRM_MODE_IN_TRIGGER
:
327 fprintf(fp
, "in_trigger");
333 fprintf(fp
, "%u", mode
);
336 fprintf(fp
, "%s", _SL_
);
339 static const char *strxf_limit(__u64 limit
)
343 if (limit
== XFRM_INF
)
344 strcpy(str
, "(INF)");
346 sprintf(str
, "%llu", (unsigned long long) limit
);
351 void xfrm_stats_print(struct xfrm_stats
*s
, FILE *fp
, const char *prefix
)
355 fprintf(fp
, "stats:%s", _SL_
);
359 fprintf(fp
, " replay-window %u replay %u failed %u%s",
360 s
->replay_window
, s
->replay
, s
->integrity_failed
, _SL_
);
363 static const char *strxf_time(__u64 time
)
373 /* XXX: treat time in the same manner of kernel's
374 * net/xfrm/xfrm_{user,state}.c
379 strftime(str
, sizeof(str
), "%Y-%m-%d %T", tp
);
385 void xfrm_lifetime_print(struct xfrm_lifetime_cfg
*cfg
,
386 struct xfrm_lifetime_cur
*cur
,
387 FILE *fp
, const char *prefix
)
392 fprintf(fp
, "lifetime config:%s", _SL_
);
396 fprintf(fp
, " limit: soft %s(bytes),",
397 strxf_limit(cfg
->soft_byte_limit
));
398 fprintf(fp
, " hard %s(bytes)%s",
399 strxf_limit(cfg
->hard_byte_limit
), _SL_
);
403 fprintf(fp
, " limit: soft %s(packets),",
404 strxf_limit(cfg
->soft_packet_limit
));
405 fprintf(fp
, " hard %s(packets)%s",
406 strxf_limit(cfg
->hard_packet_limit
), _SL_
);
410 fprintf(fp
, " expire add: soft %llu(sec), hard %llu(sec)%s",
411 (unsigned long long) cfg
->soft_add_expires_seconds
,
412 (unsigned long long) cfg
->hard_add_expires_seconds
,
417 fprintf(fp
, " expire use: soft %llu(sec), hard %llu(sec)%s",
418 (unsigned long long) cfg
->soft_use_expires_seconds
,
419 (unsigned long long) cfg
->hard_use_expires_seconds
,
425 fprintf(fp
, "lifetime current:%s", _SL_
);
429 fprintf(fp
, " %llu(bytes), %llu(packets)%s",
430 (unsigned long long) cur
->bytes
,
431 (unsigned long long) cur
->packets
,
436 fprintf(fp
, " add %s ", strxf_time(cur
->add_time
));
437 fprintf(fp
, "use %s%s", strxf_time(cur
->use_time
), _SL_
);
441 void xfrm_selector_print(struct xfrm_selector
*sel
, __u16 family
,
442 FILE *fp
, const char *prefix
)
450 f
= preferred_family
;
455 fprintf(fp
, "src %s/%u ",
456 rt_addr_n2a(f
, sizeof(sel
->saddr
), &sel
->saddr
),
459 fprintf(fp
, "dst %s/%u ",
460 rt_addr_n2a(f
, sizeof(sel
->daddr
), &sel
->daddr
),
464 fprintf(fp
, "proto %s ", strxf_proto(sel
->proto
));
465 switch (sel
->proto
) {
472 fprintf(fp
, "sport %u ", ntohs(sel
->sport
));
474 fprintf(fp
, "dport %u ", ntohs(sel
->dport
));
478 /* type/code is stored at sport/dport in selector */
480 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
482 fprintf(fp
, "code %u ", ntohs(sel
->dport
));
485 if (sel
->sport_mask
|| sel
->dport_mask
)
486 fprintf(fp
, "key %u ",
487 (((__u32
)ntohs(sel
->sport
)) << 16) +
492 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
493 if (sel
->dport_mask
) {
495 fprintf(fp
, "(dport) 0x%.4x ", sel
->dport
);
500 if (sel
->ifindex
> 0)
501 fprintf(fp
, "dev %s ", ll_index_to_name(sel
->ifindex
));
504 fprintf(fp
, "uid %u", sel
->user
);
506 fprintf(fp
, "%s", _SL_
);
509 static void __xfrm_algo_print(struct xfrm_algo
*algo
, int type
, int len
,
510 FILE *fp
, const char *prefix
, int newline
)
518 fprintf(fp
, "%s ", strxf_algotype(type
));
520 if (len
< sizeof(*algo
)) {
521 fprintf(fp
, "(ERROR truncated)");
524 len
-= sizeof(*algo
);
526 fprintf(fp
, "%s ", algo
->alg_name
);
528 keylen
= algo
->alg_key_len
/ 8;
530 fprintf(fp
, "(ERROR truncated)");
536 for (i
= 0; i
< keylen
; i
++)
537 fprintf(fp
, "%.2x", (unsigned char)algo
->alg_key
[i
]);
540 fprintf(fp
, " (%d bits)", algo
->alg_key_len
);
545 fprintf(fp
, "%s", _SL_
);
548 static inline void xfrm_algo_print(struct xfrm_algo
*algo
, int type
, int len
,
549 FILE *fp
, const char *prefix
)
551 return __xfrm_algo_print(algo
, type
, len
, fp
, prefix
, 1);
554 static void xfrm_aead_print(struct xfrm_algo_aead
*algo
, int len
,
555 FILE *fp
, const char *prefix
)
557 struct xfrm_algo
*base_algo
= alloca(sizeof(*base_algo
) + algo
->alg_key_len
/ 8);
559 memcpy(base_algo
->alg_name
, algo
->alg_name
, sizeof(base_algo
->alg_name
));
560 base_algo
->alg_key_len
= algo
->alg_key_len
;
561 memcpy(base_algo
->alg_key
, algo
->alg_key
, algo
->alg_key_len
/ 8);
563 __xfrm_algo_print(base_algo
, XFRMA_ALG_AEAD
, len
, fp
, prefix
, 0);
565 fprintf(fp
, " %d", algo
->alg_icv_len
);
567 fprintf(fp
, "%s", _SL_
);
570 static void xfrm_auth_trunc_print(struct xfrm_algo_auth
*algo
, int len
,
571 FILE *fp
, const char *prefix
)
573 struct xfrm_algo
*base_algo
= alloca(sizeof(*base_algo
) + algo
->alg_key_len
/ 8);
575 memcpy(base_algo
->alg_name
, algo
->alg_name
, sizeof(base_algo
->alg_name
));
576 base_algo
->alg_key_len
= algo
->alg_key_len
;
577 memcpy(base_algo
->alg_key
, algo
->alg_key
, algo
->alg_key_len
/ 8);
579 __xfrm_algo_print(base_algo
, XFRMA_ALG_AUTH_TRUNC
, len
, fp
, prefix
, 0);
581 fprintf(fp
, " %d", algo
->alg_trunc_len
);
583 fprintf(fp
, "%s", _SL_
);
586 static void xfrm_tmpl_print(struct xfrm_user_tmpl
*tmpls
, int len
,
587 FILE *fp
, const char *prefix
)
589 int ntmpls
= len
/ sizeof(struct xfrm_user_tmpl
);
595 fprintf(fp
, "(ERROR \"tmpl\" truncated)");
596 fprintf(fp
, "%s", _SL_
);
600 for (i
= 0; i
< ntmpls
; i
++) {
601 struct xfrm_user_tmpl
*tmpl
= &tmpls
[i
];
606 xfrm_id_info_print(&tmpl
->saddr
, &tmpl
->id
, tmpl
->mode
,
607 tmpl
->reqid
, tmpl
->family
, 0, fp
, prefix
, "tmpl ");
609 if (show_stats
> 0 || tmpl
->optional
) {
613 switch (tmpl
->optional
) {
616 fprintf(fp
, "level required ");
619 fprintf(fp
, "level use ");
622 fprintf(fp
, "level %u ", tmpl
->optional
);
627 fprintf(fp
, "share %s ", strxf_share(tmpl
->share
));
629 fprintf(fp
, "%s", _SL_
);
632 if (show_stats
> 0) {
636 fprintf(fp
, "%s-mask %s ",
637 strxf_algotype(XFRMA_ALG_CRYPT
),
638 strxf_mask32(tmpl
->ealgos
));
639 fprintf(fp
, "%s-mask %s ",
640 strxf_algotype(XFRMA_ALG_AUTH
),
641 strxf_mask32(tmpl
->aalgos
));
642 fprintf(fp
, "%s-mask %s",
643 strxf_algotype(XFRMA_ALG_COMP
),
644 strxf_mask32(tmpl
->calgos
));
646 fprintf(fp
, "%s", _SL_
);
651 int xfrm_parse_mark(struct xfrm_mark
*mark
, int *argcp
, char ***argvp
)
654 char **argv
= *argvp
;
657 if (get_u32(&mark
->v
, *argv
, 0)) {
658 invarg("MARK value is invalid\n", *argv
);
662 else { /* last entry on parse line */
663 mark
->m
= 0xffffffff;
667 if (strcmp(*argv
, "mask") == 0) {
669 if (get_u32(&mark
->m
, *argv
, 0)) {
670 invarg("MASK value is invalid\n", *argv
);
673 mark
->m
= 0xffffffff;
684 void xfrm_xfrma_print(struct rtattr
*tb
[], __u16 family
,
685 FILE *fp
, const char *prefix
)
687 if (tb
[XFRMA_MARK
]) {
688 struct rtattr
*rta
= tb
[XFRMA_MARK
];
689 struct xfrm_mark
*m
= RTA_DATA(rta
);
691 fprintf(fp
, "\tmark %#x/%#x", m
->v
, m
->m
);
692 fprintf(fp
, "%s", _SL_
);
695 if (tb
[XFRMA_ALG_AUTH
] && !tb
[XFRMA_ALG_AUTH_TRUNC
]) {
696 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH
];
698 xfrm_algo_print(RTA_DATA(rta
),
699 XFRMA_ALG_AUTH
, RTA_PAYLOAD(rta
), fp
, prefix
);
702 if (tb
[XFRMA_ALG_AUTH_TRUNC
]) {
703 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH_TRUNC
];
705 xfrm_auth_trunc_print(RTA_DATA(rta
),
706 RTA_PAYLOAD(rta
), fp
, prefix
);
709 if (tb
[XFRMA_ALG_AEAD
]) {
710 struct rtattr
*rta
= tb
[XFRMA_ALG_AEAD
];
712 xfrm_aead_print(RTA_DATA(rta
),
713 RTA_PAYLOAD(rta
), fp
, prefix
);
716 if (tb
[XFRMA_ALG_CRYPT
]) {
717 struct rtattr
*rta
= tb
[XFRMA_ALG_CRYPT
];
719 xfrm_algo_print(RTA_DATA(rta
),
720 XFRMA_ALG_CRYPT
, RTA_PAYLOAD(rta
), fp
, prefix
);
723 if (tb
[XFRMA_ALG_COMP
]) {
724 struct rtattr
*rta
= tb
[XFRMA_ALG_COMP
];
726 xfrm_algo_print(RTA_DATA(rta
),
727 XFRMA_ALG_COMP
, RTA_PAYLOAD(rta
), fp
, prefix
);
730 if (tb
[XFRMA_ENCAP
]) {
731 struct xfrm_encap_tmpl
*e
;
735 fprintf(fp
, "encap ");
737 if (RTA_PAYLOAD(tb
[XFRMA_ENCAP
]) < sizeof(*e
)) {
738 fprintf(fp
, "(ERROR truncated)");
739 fprintf(fp
, "%s", _SL_
);
742 e
= RTA_DATA(tb
[XFRMA_ENCAP
]);
744 fprintf(fp
, "type ");
745 switch (e
->encap_type
) {
747 fprintf(fp
, "espinudp-nonike ");
750 fprintf(fp
, "espinudp ");
753 fprintf(fp
, "%u ", e
->encap_type
);
756 fprintf(fp
, "sport %u ", ntohs(e
->encap_sport
));
757 fprintf(fp
, "dport %u ", ntohs(e
->encap_dport
));
759 fprintf(fp
, "addr %s",
760 rt_addr_n2a(family
, sizeof(e
->encap_oa
), &e
->encap_oa
));
761 fprintf(fp
, "%s", _SL_
);
764 if (tb
[XFRMA_TMPL
]) {
765 struct rtattr
*rta
= tb
[XFRMA_TMPL
];
767 xfrm_tmpl_print(RTA_DATA(rta
),
768 RTA_PAYLOAD(rta
), fp
, prefix
);
771 if (tb
[XFRMA_COADDR
]) {
772 const xfrm_address_t
*coa
;
778 coa
= RTA_DATA(tb
[XFRMA_COADDR
]);
779 if (RTA_PAYLOAD(tb
[XFRMA_COADDR
]) < sizeof(*coa
)) {
780 fprintf(fp
, "(ERROR truncated)");
781 fprintf(fp
, "%s", _SL_
);
786 rt_addr_n2a(family
, sizeof(*coa
), coa
));
787 fprintf(fp
, "%s", _SL_
);
790 if (tb
[XFRMA_LASTUSED
]) {
795 fprintf(fp
, "lastused ");
797 if (RTA_PAYLOAD(tb
[XFRMA_LASTUSED
]) < sizeof(lastused
)) {
798 fprintf(fp
, "(ERROR truncated)");
799 fprintf(fp
, "%s", _SL_
);
803 lastused
= rta_getattr_u64(tb
[XFRMA_LASTUSED
]);
805 fprintf(fp
, "%s", strxf_time(lastused
));
806 fprintf(fp
, "%s", _SL_
);
809 if (tb
[XFRMA_REPLAY_VAL
]) {
810 struct xfrm_replay_state
*replay
;
814 fprintf(fp
, "anti-replay context: ");
816 if (RTA_PAYLOAD(tb
[XFRMA_REPLAY_VAL
]) < sizeof(*replay
)) {
817 fprintf(fp
, "(ERROR truncated)");
818 fprintf(fp
, "%s", _SL_
);
822 replay
= RTA_DATA(tb
[XFRMA_REPLAY_VAL
]);
823 fprintf(fp
, "seq 0x%x, oseq 0x%x, bitmap 0x%08x",
824 replay
->seq
, replay
->oseq
, replay
->bitmap
);
825 fprintf(fp
, "%s", _SL_
);
828 if (tb
[XFRMA_REPLAY_ESN_VAL
]) {
829 struct xfrm_replay_state_esn
*replay
;
834 fprintf(fp
, "anti-replay esn context:");
836 if (RTA_PAYLOAD(tb
[XFRMA_REPLAY_ESN_VAL
]) < sizeof(*replay
)) {
837 fprintf(fp
, "(ERROR truncated)");
838 fprintf(fp
, "%s", _SL_
);
841 fprintf(fp
, "%s", _SL_
);
843 replay
= RTA_DATA(tb
[XFRMA_REPLAY_ESN_VAL
]);
846 fprintf(fp
, " seq-hi 0x%x, seq 0x%x, oseq-hi 0x%0x, oseq 0x%0x",
847 replay
->seq_hi
, replay
->seq
, replay
->oseq_hi
,
849 fprintf(fp
, "%s", _SL_
);
852 fprintf(fp
, " replay_window %u, bitmap-length %u",
853 replay
->replay_window
, replay
->bmp_len
);
854 for (i
= replay
->bmp_len
, j
= 0; i
; i
--) {
856 fprintf(fp
, "%s", _SL_
);
861 fprintf(fp
, "%08x ", replay
->bmp
[i
- 1]);
863 fprintf(fp
, "%s", _SL_
);
865 if (tb
[XFRMA_OFFLOAD_DEV
]) {
866 struct xfrm_user_offload
*xuo
;
870 fprintf(fp
, "crypto offload parameters: ");
872 if (RTA_PAYLOAD(tb
[XFRMA_OFFLOAD_DEV
]) < sizeof(*xuo
)) {
873 fprintf(fp
, "(ERROR truncated)");
874 fprintf(fp
, "%s", _SL_
);
878 xuo
= (struct xfrm_user_offload
*)
879 RTA_DATA(tb
[XFRMA_OFFLOAD_DEV
]);
880 fprintf(fp
, "dev %s dir %s", ll_index_to_name(xuo
->ifindex
),
881 (xuo
->flags
& XFRM_OFFLOAD_INBOUND
) ? "in" : "out");
882 fprintf(fp
, "%s", _SL_
);
886 static int xfrm_selector_iszero(struct xfrm_selector
*s
)
888 struct xfrm_selector s0
= {};
890 return (memcmp(&s0
, s
, sizeof(s0
)) == 0);
893 void xfrm_state_info_print(struct xfrm_usersa_info
*xsinfo
,
894 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
897 char buf
[STRBUF_SIZE
] = {};
898 int force_spi
= xfrm_xfrmproto_is_ipsec(xsinfo
->id
.proto
);
900 xfrm_id_info_print(&xsinfo
->saddr
, &xsinfo
->id
, xsinfo
->mode
,
901 xsinfo
->reqid
, xsinfo
->family
, force_spi
, fp
,
905 STRBUF_CAT(buf
, prefix
);
906 STRBUF_CAT(buf
, "\t");
909 fprintf(fp
, "replay-window %u ", xsinfo
->replay_window
);
911 fprintf(fp
, "seq 0x%08u ", xsinfo
->seq
);
912 if (show_stats
> 0 || xsinfo
->flags
) {
913 __u8 flags
= xsinfo
->flags
;
915 fprintf(fp
, "flag ");
916 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOECN
, "noecn");
917 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_DECAP_DSCP
, "decap-dscp");
918 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOPMTUDISC
, "nopmtudisc");
919 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_WILDRECV
, "wildrecv");
920 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ICMP
, "icmp");
921 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_AF_UNSPEC
, "af-unspec");
922 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ALIGN4
, "align4");
923 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ESN
, "esn");
925 fprintf(fp
, "%x", flags
);
927 if (show_stats
> 0 && tb
[XFRMA_SA_EXTRA_FLAGS
]) {
928 __u32 extra_flags
= rta_getattr_u32(tb
[XFRMA_SA_EXTRA_FLAGS
]);
930 fprintf(fp
, "extra_flag ");
931 XFRM_FLAG_PRINT(fp
, extra_flags
,
932 XFRM_SA_XFLAG_DONT_ENCAP_DSCP
,
935 fprintf(fp
, "%x", extra_flags
);
938 fprintf(fp
, " (0x%s)", strxf_mask8(xsinfo
->flags
));
939 fprintf(fp
, "%s", _SL_
);
941 xfrm_xfrma_print(tb
, xsinfo
->family
, fp
, buf
);
943 if (!xfrm_selector_iszero(&xsinfo
->sel
)) {
944 char sbuf
[STRBUF_SIZE
];
946 memcpy(sbuf
, buf
, sizeof(sbuf
));
947 STRBUF_CAT(sbuf
, "sel ");
949 xfrm_selector_print(&xsinfo
->sel
, xsinfo
->family
, fp
, sbuf
);
952 if (show_stats
> 0) {
953 xfrm_lifetime_print(&xsinfo
->lft
, &xsinfo
->curlft
, fp
, buf
);
954 xfrm_stats_print(&xsinfo
->stats
, fp
, buf
);
957 if (tb
[XFRMA_SEC_CTX
]) {
958 struct xfrm_user_sec_ctx
*sctx
;
960 fprintf(fp
, "\tsecurity context ");
962 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
963 fprintf(fp
, "(ERROR truncated)");
965 sctx
= RTA_DATA(tb
[XFRMA_SEC_CTX
]);
967 fprintf(fp
, "%s %s", (char *)(sctx
+ 1), _SL_
);
972 void xfrm_policy_info_print(struct xfrm_userpolicy_info
*xpinfo
,
973 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
976 char buf
[STRBUF_SIZE
] = {};
978 xfrm_selector_print(&xpinfo
->sel
, preferred_family
, fp
, title
);
980 if (tb
[XFRMA_SEC_CTX
]) {
981 struct xfrm_user_sec_ctx
*sctx
;
983 fprintf(fp
, "\tsecurity context ");
985 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
986 fprintf(fp
, "(ERROR truncated)");
988 sctx
= RTA_DATA(tb
[XFRMA_SEC_CTX
]);
990 fprintf(fp
, "%s ", (char *)(sctx
+ 1));
991 fprintf(fp
, "%s", _SL_
);
995 STRBUF_CAT(buf
, prefix
);
996 STRBUF_CAT(buf
, "\t");
999 if (xpinfo
->dir
>= XFRM_POLICY_MAX
) {
1000 xpinfo
->dir
-= XFRM_POLICY_MAX
;
1001 fprintf(fp
, "socket ");
1003 fprintf(fp
, "dir ");
1005 switch (xpinfo
->dir
) {
1006 case XFRM_POLICY_IN
:
1009 case XFRM_POLICY_OUT
:
1012 case XFRM_POLICY_FWD
:
1016 fprintf(fp
, "%u", xpinfo
->dir
);
1021 switch (xpinfo
->action
) {
1022 case XFRM_POLICY_ALLOW
:
1024 fprintf(fp
, "action allow ");
1026 case XFRM_POLICY_BLOCK
:
1027 fprintf(fp
, "action block ");
1030 fprintf(fp
, "action %u ", xpinfo
->action
);
1035 fprintf(fp
, "index %u ", xpinfo
->index
);
1036 fprintf(fp
, "priority %u ", xpinfo
->priority
);
1038 if (tb
[XFRMA_POLICY_TYPE
]) {
1039 struct xfrm_userpolicy_type
*upt
;
1041 fprintf(fp
, "ptype ");
1043 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
))
1044 fprintf(fp
, "(ERROR truncated)");
1046 upt
= RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
1047 fprintf(fp
, "%s ", strxf_ptype(upt
->type
));
1051 fprintf(fp
, "share %s ", strxf_share(xpinfo
->share
));
1053 if (show_stats
> 0 || xpinfo
->flags
) {
1054 __u8 flags
= xpinfo
->flags
;
1056 fprintf(fp
, "flag ");
1057 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_LOCALOK
, "localok");
1058 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_ICMP
, "icmp");
1060 fprintf(fp
, "%x", flags
);
1063 fprintf(fp
, " (0x%s)", strxf_mask8(xpinfo
->flags
));
1064 fprintf(fp
, "%s", _SL_
);
1067 xfrm_lifetime_print(&xpinfo
->lft
, &xpinfo
->curlft
, fp
, buf
);
1069 xfrm_xfrma_print(tb
, xpinfo
->sel
.family
, fp
, buf
);
1072 int xfrm_id_parse(xfrm_address_t
*saddr
, struct xfrm_id
*id
, __u16
*family
,
1073 int loose
, int *argcp
, char ***argvp
)
1076 char **argv
= *argvp
;
1077 inet_prefix dst
= {};
1078 inet_prefix src
= {};
1081 if (strcmp(*argv
, "src") == 0) {
1084 get_prefix(&src
, *argv
, preferred_family
);
1085 if (src
.family
== AF_UNSPEC
)
1086 invarg("value after \"src\" has an unrecognized address family", *argv
);
1088 *family
= src
.family
;
1090 memcpy(saddr
, &src
.data
, sizeof(*saddr
));
1092 filter
.id_src_mask
= src
.bitlen
;
1094 } else if (strcmp(*argv
, "dst") == 0) {
1097 get_prefix(&dst
, *argv
, preferred_family
);
1098 if (dst
.family
== AF_UNSPEC
)
1099 invarg("value after \"dst\" has an unrecognized address family", *argv
);
1101 *family
= dst
.family
;
1103 memcpy(&id
->daddr
, &dst
.data
, sizeof(id
->daddr
));
1105 filter
.id_dst_mask
= dst
.bitlen
;
1107 } else if (strcmp(*argv
, "proto") == 0) {
1112 ret
= xfrm_xfrmproto_getbyname(*argv
);
1114 invarg("XFRM-PROTO value is invalid", *argv
);
1116 id
->proto
= (__u8
)ret
;
1118 filter
.id_proto_mask
= XFRM_FILTER_MASK_FULL
;
1120 } else if (strcmp(*argv
, "spi") == 0) {
1122 if (get_be32(&id
->spi
, *argv
, 0))
1123 invarg("SPI value is invalid", *argv
);
1125 filter
.id_spi_mask
= XFRM_FILTER_MASK_FULL
;
1128 PREV_ARG(); /* back track */
1137 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1138 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv
);
1140 if (id
->spi
&& id
->proto
) {
1141 if (xfrm_xfrmproto_is_ro(id
->proto
)) {
1142 fprintf(stderr
, "\"spi\" is invalid with XFRM-PROTO value \"%s\"\n",
1143 strxf_xfrmproto(id
->proto
));
1145 } else if (id
->proto
== IPPROTO_COMP
&& ntohl(id
->spi
) >= 0x10000) {
1146 fprintf(stderr
, "SPI value is too large with XFRM-PROTO value \"%s\"\n",
1147 strxf_xfrmproto(id
->proto
));
1152 if (loose
== 0 && id
->proto
== 0)
1153 missarg("XFRM-PROTO");
1163 int xfrm_mode_parse(__u8
*mode
, int *argcp
, char ***argvp
)
1166 char **argv
= *argvp
;
1168 if (matches(*argv
, "transport") == 0)
1169 *mode
= XFRM_MODE_TRANSPORT
;
1170 else if (matches(*argv
, "tunnel") == 0)
1171 *mode
= XFRM_MODE_TUNNEL
;
1172 else if (matches(*argv
, "ro") == 0)
1173 *mode
= XFRM_MODE_ROUTEOPTIMIZATION
;
1174 else if (matches(*argv
, "in_trigger") == 0)
1175 *mode
= XFRM_MODE_IN_TRIGGER
;
1176 else if (matches(*argv
, "beet") == 0)
1177 *mode
= XFRM_MODE_BEET
;
1179 invarg("MODE value is invalid", *argv
);
1187 int xfrm_encap_type_parse(__u16
*type
, int *argcp
, char ***argvp
)
1190 char **argv
= *argvp
;
1192 if (strcmp(*argv
, "espinudp-nonike") == 0)
1194 else if (strcmp(*argv
, "espinudp") == 0)
1197 invarg("ENCAP-TYPE value is invalid", *argv
);
1205 /* NOTE: reqid is used by host-byte order */
1206 int xfrm_reqid_parse(__u32
*reqid
, int *argcp
, char ***argvp
)
1209 char **argv
= *argvp
;
1211 if (get_u32(reqid
, *argv
, 0))
1212 invarg("REQID value is invalid", *argv
);
1220 static int xfrm_selector_upspec_parse(struct xfrm_selector
*sel
,
1221 int *argcp
, char ***argvp
)
1224 char **argv
= *argvp
;
1225 char *sportp
= NULL
;
1226 char *dportp
= NULL
;
1229 char *grekey
= NULL
;
1232 if (strcmp(*argv
, "proto") == 0) {
1237 if (strcmp(*argv
, "any") == 0)
1240 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_be16(&sel
->sport
, *argv
, 0))
1260 invarg("value after \"sport\" is invalid", *argv
);
1262 sel
->sport_mask
= ~((__u16
)0);
1264 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1266 } else if (strcmp(*argv
, "dport") == 0) {
1271 if (get_be16(&sel
->dport
, *argv
, 0))
1272 invarg("value after \"dport\" is invalid", *argv
);
1274 sel
->dport_mask
= ~((__u16
)0);
1276 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1278 } else if (strcmp(*argv
, "type") == 0) {
1283 if (get_u16(&sel
->sport
, *argv
, 0) ||
1284 (sel
->sport
& ~((__u16
)0xff)))
1285 invarg("value after \"type\" is invalid", *argv
);
1286 sel
->sport
= htons(sel
->sport
);
1287 sel
->sport_mask
= ~((__u16
)0);
1289 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1292 } else if (strcmp(*argv
, "code") == 0) {
1297 if (get_u16(&sel
->dport
, *argv
, 0) ||
1298 (sel
->dport
& ~((__u16
)0xff)))
1299 invarg("value after \"code\" is invalid", *argv
);
1300 sel
->dport
= htons(sel
->dport
);
1301 sel
->dport_mask
= ~((__u16
)0);
1303 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1305 } else if (strcmp(*argv
, "key") == 0) {
1312 if (strchr(*argv
, '.'))
1313 uval
= htonl(get_addr32(*argv
));
1315 if (get_unsigned(&uval
, *argv
, 0) < 0) {
1316 fprintf(stderr
, "value after \"key\" is invalid\n");
1321 sel
->sport
= htons(uval
>> 16);
1322 sel
->dport
= htons(uval
& 0xffff);
1323 sel
->sport_mask
= ~((__u16
)0);
1324 sel
->dport_mask
= ~((__u16
)0);
1326 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1329 PREV_ARG(); /* back track */
1339 if (sportp
|| dportp
) {
1340 switch (sel
->proto
) {
1345 case IPPROTO_IP
: /* to allow shared SA for different protocols */
1348 fprintf(stderr
, "\"sport\" and \"dport\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1352 if (typep
|| codep
) {
1353 switch (sel
->proto
) {
1355 case IPPROTO_ICMPV6
:
1359 fprintf(stderr
, "\"type\" and \"code\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1364 switch (sel
->proto
) {
1368 fprintf(stderr
, "\"key\" is invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1379 int xfrm_selector_parse(struct xfrm_selector
*sel
, int *argcp
, char ***argvp
)
1382 char **argv
= *argvp
;
1383 inet_prefix dst
= {};
1384 inet_prefix src
= {};
1385 char *upspecp
= NULL
;
1388 if (strcmp(*argv
, "src") == 0) {
1391 get_prefix(&src
, *argv
, preferred_family
);
1392 if (src
.family
== AF_UNSPEC
)
1393 invarg("value after \"src\" has an unrecognized address family", *argv
);
1394 sel
->family
= src
.family
;
1396 memcpy(&sel
->saddr
, &src
.data
, sizeof(sel
->saddr
));
1397 sel
->prefixlen_s
= src
.bitlen
;
1399 filter
.sel_src_mask
= src
.bitlen
;
1401 } else if (strcmp(*argv
, "dst") == 0) {
1404 get_prefix(&dst
, *argv
, preferred_family
);
1405 if (dst
.family
== AF_UNSPEC
)
1406 invarg("value after \"dst\" has an unrecognized address family", *argv
);
1407 sel
->family
= dst
.family
;
1409 memcpy(&sel
->daddr
, &dst
.data
, sizeof(sel
->daddr
));
1410 sel
->prefixlen_d
= dst
.bitlen
;
1412 filter
.sel_dst_mask
= dst
.bitlen
;
1414 } else if (strcmp(*argv
, "dev") == 0) {
1419 if (strcmp(*argv
, "none") == 0)
1422 ifindex
= ll_name_to_index(*argv
);
1424 invarg("DEV value is invalid", *argv
);
1426 sel
->ifindex
= ifindex
;
1428 filter
.sel_dev_mask
= XFRM_FILTER_MASK_FULL
;
1432 PREV_ARG(); /* back track */
1436 xfrm_selector_upspec_parse(sel
, &argc
, &argv
);
1446 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1447 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv
);
1450 missarg("SELECTOR");
1458 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg
*lft
,
1459 int *argcp
, char ***argvp
)
1462 char **argv
= *argvp
;
1465 if (strcmp(*argv
, "time-soft") == 0) {
1467 ret
= get_u64(&lft
->soft_add_expires_seconds
, *argv
, 0);
1469 invarg("value after \"time-soft\" is invalid", *argv
);
1470 } else if (strcmp(*argv
, "time-hard") == 0) {
1472 ret
= get_u64(&lft
->hard_add_expires_seconds
, *argv
, 0);
1474 invarg("value after \"time-hard\" is invalid", *argv
);
1475 } else if (strcmp(*argv
, "time-use-soft") == 0) {
1477 ret
= get_u64(&lft
->soft_use_expires_seconds
, *argv
, 0);
1479 invarg("value after \"time-use-soft\" is invalid", *argv
);
1480 } else if (strcmp(*argv
, "time-use-hard") == 0) {
1482 ret
= get_u64(&lft
->hard_use_expires_seconds
, *argv
, 0);
1484 invarg("value after \"time-use-hard\" is invalid", *argv
);
1485 } else if (strcmp(*argv
, "byte-soft") == 0) {
1487 ret
= get_u64(&lft
->soft_byte_limit
, *argv
, 0);
1489 invarg("value after \"byte-soft\" is invalid", *argv
);
1490 } else if (strcmp(*argv
, "byte-hard") == 0) {
1492 ret
= get_u64(&lft
->hard_byte_limit
, *argv
, 0);
1494 invarg("value after \"byte-hard\" is invalid", *argv
);
1495 } else if (strcmp(*argv
, "packet-soft") == 0) {
1497 ret
= get_u64(&lft
->soft_packet_limit
, *argv
, 0);
1499 invarg("value after \"packet-soft\" is invalid", *argv
);
1500 } else if (strcmp(*argv
, "packet-hard") == 0) {
1502 ret
= get_u64(&lft
->hard_packet_limit
, *argv
, 0);
1504 invarg("value after \"packet-hard\" is invalid", *argv
);
1506 invarg("LIMIT value is invalid", *argv
);
1514 int do_xfrm(int argc
, char **argv
)
1516 memset(&filter
, 0, sizeof(filter
));
1521 if (matches(*argv
, "state") == 0 ||
1522 matches(*argv
, "sa") == 0)
1523 return do_xfrm_state(argc
-1, argv
+1);
1524 else if (matches(*argv
, "policy") == 0)
1525 return do_xfrm_policy(argc
-1, argv
+1);
1526 else if (matches(*argv
, "monitor") == 0)
1527 return do_xfrm_monitor(argc
-1, argv
+1);
1528 else if (matches(*argv
, "help") == 0) {
1530 fprintf(stderr
, "xfrm Object \"%s\" is unknown.\n", *argv
);