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
];
127 if (!t
->t_name
|| t
->t_type
== -1)
130 if (strcmp(t
->t_name
, name
) == 0)
137 const char *strxf_xfrmproto(__u8 proto
)
143 const struct typeent
*t
= &xfrmproto_types
[i
];
144 if (!t
->t_name
|| t
->t_type
== -1)
147 if (t
->t_type
== proto
)
151 sprintf(str
, "%u", proto
);
155 static const struct typeent algo_types
[]= {
156 { "enc", XFRMA_ALG_CRYPT
}, { "auth", XFRMA_ALG_AUTH
},
157 { "comp", XFRMA_ALG_COMP
}, { "aead", XFRMA_ALG_AEAD
},
158 { "auth-trunc", XFRMA_ALG_AUTH_TRUNC
},
162 int xfrm_algotype_getbyname(char *name
)
167 const struct typeent
*t
= &algo_types
[i
];
168 if (!t
->t_name
|| t
->t_type
== -1)
171 if (strcmp(t
->t_name
, name
) == 0)
178 const char *strxf_algotype(int type
)
184 const struct typeent
*t
= &algo_types
[i
];
185 if (!t
->t_name
|| t
->t_type
== -1)
188 if (t
->t_type
== type
)
192 sprintf(str
, "%d", type
);
196 const char *strxf_mask8(__u8 mask
)
199 const int sn
= sizeof(mask
) * 8 - 1;
203 for (b
= (1 << sn
); b
> 0; b
>>= 1)
204 str
[i
++] = ((b
& mask
) ? '1' : '0');
210 const char *strxf_mask32(__u32 mask
)
214 sprintf(str
, "%.8x", mask
);
219 const char *strxf_share(__u8 share
)
227 case XFRM_SHARE_SESSION
:
228 strcpy(str
, "session");
230 case XFRM_SHARE_USER
:
233 case XFRM_SHARE_UNIQUE
:
234 strcpy(str
, "unique");
237 sprintf(str
, "%u", share
);
244 const char *strxf_proto(__u8 proto
)
250 pp
= getprotobynumber(proto
);
254 sprintf(buf
, "%u", proto
);
261 const char *strxf_ptype(__u8 ptype
)
266 case XFRM_POLICY_TYPE_MAIN
:
269 case XFRM_POLICY_TYPE_SUB
:
273 sprintf(str
, "%u", ptype
);
280 void xfrm_id_info_print(xfrm_address_t
*saddr
, struct xfrm_id
*id
,
281 __u8 mode
, __u32 reqid
, __u16 family
, int force_spi
,
282 FILE *fp
, const char *prefix
, const char *title
)
289 memset(abuf
, '\0', sizeof(abuf
));
290 fprintf(fp
, "src %s ", rt_addr_n2a(family
, sizeof(*saddr
),
291 saddr
, abuf
, sizeof(abuf
)));
292 memset(abuf
, '\0', sizeof(abuf
));
293 fprintf(fp
, "dst %s", rt_addr_n2a(family
, sizeof(id
->daddr
),
294 &id
->daddr
, abuf
, sizeof(abuf
)));
295 fprintf(fp
, "%s", _SL_
);
301 fprintf(fp
, "proto %s ", strxf_xfrmproto(id
->proto
));
303 if (show_stats
> 0 || force_spi
|| id
->spi
) {
304 __u32 spi
= ntohl(id
->spi
);
305 fprintf(fp
, "spi 0x%08x", spi
);
307 fprintf(fp
, "(%u)", spi
);
311 fprintf(fp
, "reqid %u", reqid
);
313 fprintf(fp
, "(0x%08x)", reqid
);
316 fprintf(fp
, "mode ");
318 case XFRM_MODE_TRANSPORT
:
319 fprintf(fp
, "transport");
321 case XFRM_MODE_TUNNEL
:
322 fprintf(fp
, "tunnel");
324 case XFRM_MODE_ROUTEOPTIMIZATION
:
327 case XFRM_MODE_IN_TRIGGER
:
328 fprintf(fp
, "in_trigger");
334 fprintf(fp
, "%u", mode
);
337 fprintf(fp
, "%s", _SL_
);
340 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
)
451 f
= preferred_family
;
456 memset(abuf
, '\0', sizeof(abuf
));
457 fprintf(fp
, "src %s/%u ",
458 rt_addr_n2a(f
, sizeof(sel
->saddr
), &sel
->saddr
,
462 memset(abuf
, '\0', sizeof(abuf
));
463 fprintf(fp
, "dst %s/%u ",
464 rt_addr_n2a(f
, sizeof(sel
->daddr
), &sel
->daddr
,
469 fprintf(fp
, "proto %s ", strxf_proto(sel
->proto
));
470 switch (sel
->proto
) {
477 fprintf(fp
, "sport %u ", ntohs(sel
->sport
));
479 fprintf(fp
, "dport %u ", ntohs(sel
->dport
));
483 /* type/code is stored at sport/dport in selector */
485 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
487 fprintf(fp
, "code %u ", ntohs(sel
->dport
));
490 if (sel
->sport_mask
|| sel
->dport_mask
)
491 fprintf(fp
, "key %u ",
492 (((__u32
)ntohs(sel
->sport
)) << 16) +
497 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
498 if (sel
->dport_mask
) {
500 fprintf(fp
, "(dport) 0x%.4x ", sel
->dport
);
505 if (sel
->ifindex
> 0)
506 fprintf(fp
, "dev %s ", ll_index_to_name(sel
->ifindex
));
509 fprintf(fp
, "uid %u", sel
->user
);
511 fprintf(fp
, "%s", _SL_
);
514 static void __xfrm_algo_print(struct xfrm_algo
*algo
, int type
, int len
,
515 FILE *fp
, const char *prefix
, int newline
)
523 fprintf(fp
, "%s ", strxf_algotype(type
));
525 if (len
< sizeof(*algo
)) {
526 fprintf(fp
, "(ERROR truncated)");
529 len
-= sizeof(*algo
);
531 fprintf(fp
, "%s ", algo
->alg_name
);
533 keylen
= algo
->alg_key_len
/ 8;
535 fprintf(fp
, "(ERROR truncated)");
541 for (i
= 0; i
< keylen
; i
++)
542 fprintf(fp
, "%.2x", (unsigned char)algo
->alg_key
[i
]);
545 fprintf(fp
, " (%d bits)", algo
->alg_key_len
);
550 fprintf(fp
, "%s", _SL_
);
553 static inline void xfrm_algo_print(struct xfrm_algo
*algo
, int type
, int len
,
554 FILE *fp
, const char *prefix
)
556 return __xfrm_algo_print(algo
, type
, len
, fp
, prefix
, 1);
559 static void xfrm_aead_print(struct xfrm_algo_aead
*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_AEAD
, len
, fp
, prefix
, 0);
570 fprintf(fp
, " %d", algo
->alg_icv_len
);
572 fprintf(fp
, "%s", _SL_
);
575 static void xfrm_auth_trunc_print(struct xfrm_algo_auth
*algo
, int len
,
576 FILE *fp
, const char *prefix
)
578 struct xfrm_algo
*base_algo
= alloca(sizeof(*base_algo
) + algo
->alg_key_len
/ 8);
580 memcpy(base_algo
->alg_name
, algo
->alg_name
, sizeof(base_algo
->alg_name
));
581 base_algo
->alg_key_len
= algo
->alg_key_len
;
582 memcpy(base_algo
->alg_key
, algo
->alg_key
, algo
->alg_key_len
/ 8);
584 __xfrm_algo_print(base_algo
, XFRMA_ALG_AUTH_TRUNC
, len
, fp
, prefix
, 0);
586 fprintf(fp
, " %d", algo
->alg_trunc_len
);
588 fprintf(fp
, "%s", _SL_
);
591 static void xfrm_tmpl_print(struct xfrm_user_tmpl
*tmpls
, int len
,
592 FILE *fp
, const char *prefix
)
594 int ntmpls
= len
/ sizeof(struct xfrm_user_tmpl
);
600 fprintf(fp
, "(ERROR \"tmpl\" truncated)");
601 fprintf(fp
, "%s", _SL_
);
605 for (i
= 0; i
< ntmpls
; i
++) {
606 struct xfrm_user_tmpl
*tmpl
= &tmpls
[i
];
611 xfrm_id_info_print(&tmpl
->saddr
, &tmpl
->id
, tmpl
->mode
,
612 tmpl
->reqid
, tmpl
->family
, 0, fp
, prefix
, "tmpl ");
614 if (show_stats
> 0 || tmpl
->optional
) {
618 switch (tmpl
->optional
) {
621 fprintf(fp
, "level required ");
624 fprintf(fp
, "level use ");
627 fprintf(fp
, "level %u ", tmpl
->optional
);
632 fprintf(fp
, "share %s ", strxf_share(tmpl
->share
));
634 fprintf(fp
, "%s", _SL_
);
637 if (show_stats
> 0) {
641 fprintf(fp
, "%s-mask %s ",
642 strxf_algotype(XFRMA_ALG_CRYPT
),
643 strxf_mask32(tmpl
->ealgos
));
644 fprintf(fp
, "%s-mask %s ",
645 strxf_algotype(XFRMA_ALG_AUTH
),
646 strxf_mask32(tmpl
->aalgos
));
647 fprintf(fp
, "%s-mask %s",
648 strxf_algotype(XFRMA_ALG_COMP
),
649 strxf_mask32(tmpl
->calgos
));
651 fprintf(fp
, "%s", _SL_
);
656 int xfrm_parse_mark(struct xfrm_mark
*mark
, int *argcp
, char ***argvp
)
659 char **argv
= *argvp
;
662 if (get_u32(&mark
->v
, *argv
, 0)) {
663 invarg("MARK value is invalid\n", *argv
);
667 else { /* last entry on parse line */
668 mark
->m
= 0xffffffff;
672 if (strcmp(*argv
, "mask") == 0) {
674 if (get_u32(&mark
->m
, *argv
, 0)) {
675 invarg("MASK value is invalid\n", *argv
);
678 mark
->m
= 0xffffffff;
689 void xfrm_xfrma_print(struct rtattr
*tb
[], __u16 family
,
690 FILE *fp
, const char *prefix
)
692 if (tb
[XFRMA_MARK
]) {
693 struct rtattr
*rta
= tb
[XFRMA_MARK
];
694 struct xfrm_mark
*m
= (struct xfrm_mark
*) RTA_DATA(rta
);
695 fprintf(fp
, "\tmark %#x/%#x", m
->v
, m
->m
);
696 fprintf(fp
, "%s", _SL_
);
699 if (tb
[XFRMA_ALG_AUTH
] && !tb
[XFRMA_ALG_AUTH_TRUNC
]) {
700 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH
];
701 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
702 XFRMA_ALG_AUTH
, RTA_PAYLOAD(rta
), fp
, prefix
);
705 if (tb
[XFRMA_ALG_AUTH_TRUNC
]) {
706 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH_TRUNC
];
707 xfrm_auth_trunc_print((struct xfrm_algo_auth
*) RTA_DATA(rta
),
708 RTA_PAYLOAD(rta
), fp
, prefix
);
711 if (tb
[XFRMA_ALG_AEAD
]) {
712 struct rtattr
*rta
= tb
[XFRMA_ALG_AEAD
];
713 xfrm_aead_print((struct xfrm_algo_aead
*)RTA_DATA(rta
),
714 RTA_PAYLOAD(rta
), fp
, prefix
);
717 if (tb
[XFRMA_ALG_CRYPT
]) {
718 struct rtattr
*rta
= tb
[XFRMA_ALG_CRYPT
];
719 xfrm_algo_print((struct xfrm_algo
*) 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
];
725 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
726 XFRMA_ALG_COMP
, RTA_PAYLOAD(rta
), fp
, prefix
);
729 if (tb
[XFRMA_ENCAP
]) {
730 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
= (struct xfrm_encap_tmpl
*) 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 memset(abuf
, '\0', sizeof(abuf
));
760 fprintf(fp
, "addr %s",
761 rt_addr_n2a(family
, sizeof(e
->encap_oa
), &e
->encap_oa
,
762 abuf
, sizeof(abuf
)));
763 fprintf(fp
, "%s", _SL_
);
766 if (tb
[XFRMA_TMPL
]) {
767 struct rtattr
*rta
= tb
[XFRMA_TMPL
];
768 xfrm_tmpl_print((struct xfrm_user_tmpl
*) RTA_DATA(rta
),
769 RTA_PAYLOAD(rta
), fp
, prefix
);
772 if (tb
[XFRMA_COADDR
]) {
780 coa
= (xfrm_address_t
*)RTA_DATA(tb
[XFRMA_COADDR
]);
782 if (RTA_PAYLOAD(tb
[XFRMA_COADDR
]) < sizeof(*coa
)) {
783 fprintf(fp
, "(ERROR truncated)");
784 fprintf(fp
, "%s", _SL_
);
788 memset(abuf
, '\0', sizeof(abuf
));
790 rt_addr_n2a(family
, sizeof(*coa
), coa
,
791 abuf
, sizeof(abuf
)));
792 fprintf(fp
, "%s", _SL_
);
795 if (tb
[XFRMA_LASTUSED
]) {
800 fprintf(fp
, "lastused ");
802 if (RTA_PAYLOAD(tb
[XFRMA_LASTUSED
]) < sizeof(lastused
)) {
803 fprintf(fp
, "(ERROR truncated)");
804 fprintf(fp
, "%s", _SL_
);
808 lastused
= rta_getattr_u64(tb
[XFRMA_LASTUSED
]);
810 fprintf(fp
, "%s", strxf_time(lastused
));
811 fprintf(fp
, "%s", _SL_
);
814 if (tb
[XFRMA_REPLAY_VAL
]) {
815 struct xfrm_replay_state
*replay
;
819 fprintf(fp
, "anti-replay context: ");
821 if (RTA_PAYLOAD(tb
[XFRMA_REPLAY_VAL
]) < sizeof(*replay
)) {
822 fprintf(fp
, "(ERROR truncated)");
823 fprintf(fp
, "%s", _SL_
);
827 replay
= (struct xfrm_replay_state
*)RTA_DATA(tb
[XFRMA_REPLAY_VAL
]);
828 fprintf(fp
, "seq 0x%x, oseq 0x%x, bitmap 0x%08x",
829 replay
->seq
, replay
->oseq
, replay
->bitmap
);
830 fprintf(fp
, "%s", _SL_
);
833 if (tb
[XFRMA_REPLAY_ESN_VAL
]) {
834 struct xfrm_replay_state_esn
*replay
;
839 fprintf(fp
, "anti-replay esn context:");
841 if (RTA_PAYLOAD(tb
[XFRMA_REPLAY_ESN_VAL
]) < sizeof(*replay
)) {
842 fprintf(fp
, "(ERROR truncated)");
843 fprintf(fp
, "%s", _SL_
);
846 fprintf(fp
, "%s", _SL_
);
848 replay
= (struct xfrm_replay_state_esn
*)RTA_DATA(tb
[XFRMA_REPLAY_ESN_VAL
]);
851 fprintf(fp
, " seq-hi 0x%x, seq 0x%x, oseq-hi 0x%0x, oseq 0x%0x",
852 replay
->seq_hi
, replay
->seq
, replay
->oseq_hi
,
854 fprintf(fp
, "%s", _SL_
);
857 fprintf(fp
, " replay_window %u, bitmap-length %u",
858 replay
->replay_window
, replay
->bmp_len
);
859 for (i
= replay
->bmp_len
, j
= 0; i
; i
--) {
861 fprintf(fp
, "%s", _SL_
);
866 fprintf(fp
, "%08x ", replay
->bmp
[i
- 1]);
868 fprintf(fp
, "%s", _SL_
);
872 static int xfrm_selector_iszero(struct xfrm_selector
*s
)
874 struct xfrm_selector s0
;
876 memset(&s0
, 0, sizeof(s0
));
878 return (memcmp(&s0
, s
, sizeof(s0
)) == 0);
881 void xfrm_state_info_print(struct xfrm_usersa_info
*xsinfo
,
882 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
885 char buf
[STRBUF_SIZE
];
886 int force_spi
= xfrm_xfrmproto_is_ipsec(xsinfo
->id
.proto
);
888 memset(buf
, '\0', sizeof(buf
));
890 xfrm_id_info_print(&xsinfo
->saddr
, &xsinfo
->id
, xsinfo
->mode
,
891 xsinfo
->reqid
, xsinfo
->family
, force_spi
, fp
,
895 STRBUF_CAT(buf
, prefix
);
896 STRBUF_CAT(buf
, "\t");
899 fprintf(fp
, "replay-window %u ", xsinfo
->replay_window
);
901 fprintf(fp
, "seq 0x%08u ", xsinfo
->seq
);
902 if (show_stats
> 0 || xsinfo
->flags
) {
903 __u8 flags
= xsinfo
->flags
;
905 fprintf(fp
, "flag ");
906 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOECN
, "noecn");
907 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_DECAP_DSCP
, "decap-dscp");
908 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOPMTUDISC
, "nopmtudisc");
909 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_WILDRECV
, "wildrecv");
910 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ICMP
, "icmp");
911 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_AF_UNSPEC
, "af-unspec");
912 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ALIGN4
, "align4");
913 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ESN
, "esn");
915 fprintf(fp
, "%x", flags
);
917 if (show_stats
> 0 && tb
[XFRMA_SA_EXTRA_FLAGS
]) {
918 __u32 extra_flags
= *(__u32
*)RTA_DATA(tb
[XFRMA_SA_EXTRA_FLAGS
]);
920 fprintf(fp
, "extra_flag ");
921 XFRM_FLAG_PRINT(fp
, extra_flags
,
922 XFRM_SA_XFLAG_DONT_ENCAP_DSCP
,
925 fprintf(fp
, "%x", extra_flags
);
928 fprintf(fp
, " (0x%s)", strxf_mask8(xsinfo
->flags
));
929 fprintf(fp
, "%s", _SL_
);
931 xfrm_xfrma_print(tb
, xsinfo
->family
, fp
, buf
);
933 if (!xfrm_selector_iszero(&xsinfo
->sel
)) {
934 char sbuf
[STRBUF_SIZE
];
936 memcpy(sbuf
, buf
, sizeof(sbuf
));
937 STRBUF_CAT(sbuf
, "sel ");
939 xfrm_selector_print(&xsinfo
->sel
, xsinfo
->family
, fp
, sbuf
);
942 if (show_stats
> 0) {
943 xfrm_lifetime_print(&xsinfo
->lft
, &xsinfo
->curlft
, fp
, buf
);
944 xfrm_stats_print(&xsinfo
->stats
, fp
, buf
);
947 if (tb
[XFRMA_SEC_CTX
]) {
948 struct xfrm_user_sec_ctx
*sctx
;
950 fprintf(fp
, "\tsecurity context ");
952 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
953 fprintf(fp
, "(ERROR truncated)");
955 sctx
= (struct xfrm_user_sec_ctx
*)RTA_DATA(tb
[XFRMA_SEC_CTX
]);
957 fprintf(fp
, "%s %s", (char *)(sctx
+ 1), _SL_
);
962 void xfrm_policy_info_print(struct xfrm_userpolicy_info
*xpinfo
,
963 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
966 char buf
[STRBUF_SIZE
];
968 memset(buf
, '\0', sizeof(buf
));
970 xfrm_selector_print(&xpinfo
->sel
, preferred_family
, fp
, title
);
972 if (tb
[XFRMA_SEC_CTX
]) {
973 struct xfrm_user_sec_ctx
*sctx
;
975 fprintf(fp
, "\tsecurity context ");
977 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
978 fprintf(fp
, "(ERROR truncated)");
980 sctx
= (struct xfrm_user_sec_ctx
*)RTA_DATA(tb
[XFRMA_SEC_CTX
]);
982 fprintf(fp
, "%s ", (char *)(sctx
+ 1));
983 fprintf(fp
, "%s", _SL_
);
987 STRBUF_CAT(buf
, prefix
);
988 STRBUF_CAT(buf
, "\t");
991 if (xpinfo
->dir
>= XFRM_POLICY_MAX
) {
992 xpinfo
->dir
-= XFRM_POLICY_MAX
;
993 fprintf(fp
, "socket ");
997 switch (xpinfo
->dir
) {
1001 case XFRM_POLICY_OUT
:
1004 case XFRM_POLICY_FWD
:
1008 fprintf(fp
, "%u", xpinfo
->dir
);
1013 switch (xpinfo
->action
) {
1014 case XFRM_POLICY_ALLOW
:
1016 fprintf(fp
, "action allow ");
1018 case XFRM_POLICY_BLOCK
:
1019 fprintf(fp
, "action block ");
1022 fprintf(fp
, "action %u ", xpinfo
->action
);
1027 fprintf(fp
, "index %u ", xpinfo
->index
);
1028 fprintf(fp
, "priority %u ", xpinfo
->priority
);
1030 if (tb
[XFRMA_POLICY_TYPE
]) {
1031 struct xfrm_userpolicy_type
*upt
;
1033 fprintf(fp
, "ptype ");
1035 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
))
1036 fprintf(fp
, "(ERROR truncated)");
1038 upt
= (struct xfrm_userpolicy_type
*)RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
1039 fprintf(fp
, "%s ", strxf_ptype(upt
->type
));
1043 fprintf(fp
, "share %s ", strxf_share(xpinfo
->share
));
1045 if (show_stats
> 0 || xpinfo
->flags
) {
1046 __u8 flags
= xpinfo
->flags
;
1048 fprintf(fp
, "flag ");
1049 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_LOCALOK
, "localok");
1050 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_ICMP
, "icmp");
1052 fprintf(fp
, "%x", flags
);
1055 fprintf(fp
, " (0x%s)", strxf_mask8(xpinfo
->flags
));
1056 fprintf(fp
, "%s", _SL_
);
1059 xfrm_lifetime_print(&xpinfo
->lft
, &xpinfo
->curlft
, fp
, buf
);
1061 xfrm_xfrma_print(tb
, xpinfo
->sel
.family
, fp
, buf
);
1064 int xfrm_id_parse(xfrm_address_t
*saddr
, struct xfrm_id
*id
, __u16
*family
,
1065 int loose
, int *argcp
, char ***argvp
)
1068 char **argv
= *argvp
;
1072 memset(&dst
, 0, sizeof(dst
));
1073 memset(&src
, 0, sizeof(src
));
1076 if (strcmp(*argv
, "src") == 0) {
1079 get_prefix(&src
, *argv
, preferred_family
);
1080 if (src
.family
== AF_UNSPEC
)
1081 invarg("value after \"src\" has an unrecognized address family", *argv
);
1083 *family
= src
.family
;
1085 memcpy(saddr
, &src
.data
, sizeof(*saddr
));
1087 filter
.id_src_mask
= src
.bitlen
;
1089 } else if (strcmp(*argv
, "dst") == 0) {
1092 get_prefix(&dst
, *argv
, preferred_family
);
1093 if (dst
.family
== AF_UNSPEC
)
1094 invarg("value after \"dst\" has an unrecognized address family", *argv
);
1096 *family
= dst
.family
;
1098 memcpy(&id
->daddr
, &dst
.data
, sizeof(id
->daddr
));
1100 filter
.id_dst_mask
= dst
.bitlen
;
1102 } else if (strcmp(*argv
, "proto") == 0) {
1107 ret
= xfrm_xfrmproto_getbyname(*argv
);
1109 invarg("XFRM-PROTO value is invalid", *argv
);
1111 id
->proto
= (__u8
)ret
;
1113 filter
.id_proto_mask
= XFRM_FILTER_MASK_FULL
;
1115 } else if (strcmp(*argv
, "spi") == 0) {
1119 if (get_u32(&spi
, *argv
, 0))
1120 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
;
1241 pp
= getprotobyname(*argv
);
1243 upspec
= pp
->p_proto
;
1245 if (get_u8(&upspec
, *argv
, 0))
1246 invarg("PROTO value is invalid", *argv
);
1249 sel
->proto
= upspec
;
1251 filter
.upspec_proto_mask
= XFRM_FILTER_MASK_FULL
;
1253 } else if (strcmp(*argv
, "sport") == 0) {
1258 if (get_u16(&sel
->sport
, *argv
, 0))
1259 invarg("value after \"sport\" is invalid", *argv
);
1260 sel
->sport
= htons(sel
->sport
);
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_u16(&sel
->dport
, *argv
, 0))
1272 invarg("value after \"dport\" is invalid", *argv
);
1273 sel
->dport
= htons(sel
->dport
);
1275 sel
->dport_mask
= ~((__u16
)0);
1277 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1279 } else if (strcmp(*argv
, "type") == 0) {
1284 if (get_u16(&sel
->sport
, *argv
, 0) ||
1285 (sel
->sport
& ~((__u16
)0xff)))
1286 invarg("value after \"type\" is invalid", *argv
);
1287 sel
->sport
= htons(sel
->sport
);
1288 sel
->sport_mask
= ~((__u16
)0);
1290 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1293 } else if (strcmp(*argv
, "code") == 0) {
1298 if (get_u16(&sel
->dport
, *argv
, 0) ||
1299 (sel
->dport
& ~((__u16
)0xff)))
1300 invarg("value after \"code\" is invalid", *argv
);
1301 sel
->dport
= htons(sel
->dport
);
1302 sel
->dport_mask
= ~((__u16
)0);
1304 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1306 } else if (strcmp(*argv
, "key") == 0) {
1313 if (strchr(*argv
, '.'))
1314 uval
= htonl(get_addr32(*argv
));
1316 if (get_unsigned(&uval
, *argv
, 0)<0) {
1317 fprintf(stderr
, "value after \"key\" is invalid\n");
1322 sel
->sport
= htons(uval
>> 16);
1323 sel
->dport
= htons(uval
& 0xffff);
1324 sel
->sport_mask
= ~((__u16
)0);
1325 sel
->dport_mask
= ~((__u16
)0);
1327 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1330 PREV_ARG(); /* back track */
1340 if (sportp
|| dportp
) {
1341 switch (sel
->proto
) {
1346 case IPPROTO_IP
: /* to allow shared SA for different protocols */
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
);