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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * based on ip.c, iproute.c
25 * Masahide NAKAMURA @USAGI
32 #include <sys/types.h>
33 #include <sys/socket.h>
36 #include <linux/netlink.h>
37 #include <linux/rtnetlink.h>
38 #include <linux/xfrm.h>
42 #include "ip_common.h"
44 #define STRBUF_SIZE (128)
45 #define STRBUF_CAT(buf, str) \
47 int rest = sizeof(buf) - 1 - strlen(buf); \
49 int len = strlen(str); \
52 strncat(buf, str, len); \
53 buf[sizeof(buf) - 1] = '\0'; \
57 struct xfrm_filter filter
;
59 static void usage(void) __attribute__((noreturn
));
61 static void usage(void)
64 "Usage: ip xfrm XFRM-OBJECT { COMMAND | help }\n"
65 "where XFRM-OBJECT := state | policy | monitor\n");
69 /* This is based on utils.c(inet_addr_match) */
70 int xfrm_addr_match(xfrm_address_t
*x1
, xfrm_address_t
*x2
, int bits
)
72 __u32
*a1
= (__u32
*)x1
;
73 __u32
*a2
= (__u32
*)x2
;
74 int words
= bits
>> 0x05;
79 if (memcmp(a1
, a2
, words
<< 2))
89 mask
= htonl((0xffffffff) << (0x20 - bits
));
98 int xfrm_xfrmproto_is_ipsec(__u8 proto
)
100 return (proto
== IPPROTO_ESP
||
101 proto
== IPPROTO_AH
||
102 proto
== IPPROTO_COMP
);
105 int xfrm_xfrmproto_is_ro(__u8 proto
)
107 return (proto
== IPPROTO_ROUTING
||
108 proto
== IPPROTO_DSTOPTS
);
116 static const struct typeent xfrmproto_types
[]= {
117 { "esp", IPPROTO_ESP
}, { "ah", IPPROTO_AH
}, { "comp", IPPROTO_COMP
},
118 { "route2", IPPROTO_ROUTING
}, { "hao", IPPROTO_DSTOPTS
},
119 { "ipsec-any", IPSEC_PROTO_ANY
},
123 int xfrm_xfrmproto_getbyname(char *name
)
128 const struct typeent
*t
= &xfrmproto_types
[i
];
129 if (!t
->t_name
|| t
->t_type
== -1)
132 if (strcmp(t
->t_name
, name
) == 0)
139 const char *strxf_xfrmproto(__u8 proto
)
145 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
];
170 if (!t
->t_name
|| t
->t_type
== -1)
173 if (strcmp(t
->t_name
, name
) == 0)
180 const char *strxf_algotype(int type
)
186 const struct typeent
*t
= &algo_types
[i
];
187 if (!t
->t_name
|| t
->t_type
== -1)
190 if (t
->t_type
== type
)
194 sprintf(str
, "%d", type
);
198 const char *strxf_mask8(__u8 mask
)
201 const int sn
= sizeof(mask
) * 8 - 1;
205 for (b
= (1 << sn
); b
> 0; b
>>= 1)
206 str
[i
++] = ((b
& mask
) ? '1' : '0');
212 const char *strxf_mask32(__u32 mask
)
216 sprintf(str
, "%.8x", mask
);
221 const char *strxf_share(__u8 share
)
229 case XFRM_SHARE_SESSION
:
230 strcpy(str
, "session");
232 case XFRM_SHARE_USER
:
235 case XFRM_SHARE_UNIQUE
:
236 strcpy(str
, "unique");
239 sprintf(str
, "%u", share
);
246 const char *strxf_proto(__u8 proto
)
252 pp
= getprotobynumber(proto
);
256 sprintf(buf
, "%u", proto
);
263 const char *strxf_ptype(__u8 ptype
)
268 case XFRM_POLICY_TYPE_MAIN
:
271 case XFRM_POLICY_TYPE_SUB
:
275 sprintf(str
, "%u", ptype
);
282 void xfrm_id_info_print(xfrm_address_t
*saddr
, struct xfrm_id
*id
,
283 __u8 mode
, __u32 reqid
, __u16 family
, int force_spi
,
284 FILE *fp
, const char *prefix
, const char *title
)
291 memset(abuf
, '\0', sizeof(abuf
));
292 fprintf(fp
, "src %s ", rt_addr_n2a(family
, sizeof(*saddr
),
293 saddr
, abuf
, sizeof(abuf
)));
294 memset(abuf
, '\0', sizeof(abuf
));
295 fprintf(fp
, "dst %s", rt_addr_n2a(family
, sizeof(id
->daddr
),
296 &id
->daddr
, abuf
, sizeof(abuf
)));
297 fprintf(fp
, "%s", _SL_
);
303 fprintf(fp
, "proto %s ", strxf_xfrmproto(id
->proto
));
305 if (show_stats
> 0 || force_spi
|| id
->spi
) {
306 __u32 spi
= ntohl(id
->spi
);
307 fprintf(fp
, "spi 0x%08x", spi
);
309 fprintf(fp
, "(%u)", spi
);
313 fprintf(fp
, "reqid %u", reqid
);
315 fprintf(fp
, "(0x%08x)", reqid
);
318 fprintf(fp
, "mode ");
320 case XFRM_MODE_TRANSPORT
:
321 fprintf(fp
, "transport");
323 case XFRM_MODE_TUNNEL
:
324 fprintf(fp
, "tunnel");
326 case XFRM_MODE_ROUTEOPTIMIZATION
:
329 case XFRM_MODE_IN_TRIGGER
:
330 fprintf(fp
, "in_trigger");
336 fprintf(fp
, "%u", mode
);
339 fprintf(fp
, "%s", _SL_
);
342 static const char *strxf_limit(__u64 limit
)
345 if (limit
== XFRM_INF
)
346 strcpy(str
, "(INF)");
348 sprintf(str
, "%llu", (unsigned long long) limit
);
353 void xfrm_stats_print(struct xfrm_stats
*s
, FILE *fp
, const char *prefix
)
357 fprintf(fp
, "stats:%s", _SL_
);
361 fprintf(fp
, " replay-window %u replay %u failed %u%s",
362 s
->replay_window
, s
->replay
, s
->integrity_failed
, _SL_
);
365 static const char *strxf_time(__u64 time
)
375 /* XXX: treat time in the same manner of kernel's
376 * net/xfrm/xfrm_{user,state}.c
381 strftime(str
, sizeof(str
), "%Y-%m-%d %T", tp
);
387 void xfrm_lifetime_print(struct xfrm_lifetime_cfg
*cfg
,
388 struct xfrm_lifetime_cur
*cur
,
389 FILE *fp
, const char *prefix
)
394 fprintf(fp
, "lifetime config:%s",_SL_
);
398 fprintf(fp
, " limit: soft %s(bytes),",
399 strxf_limit(cfg
->soft_byte_limit
));
400 fprintf(fp
, " hard %s(bytes)%s",
401 strxf_limit(cfg
->hard_byte_limit
), _SL_
);
405 fprintf(fp
, " limit: soft %s(packets),",
406 strxf_limit(cfg
->soft_packet_limit
));
407 fprintf(fp
, " hard %s(packets)%s",
408 strxf_limit(cfg
->hard_packet_limit
), _SL_
);
412 fprintf(fp
, " expire add: soft %llu(sec), hard %llu(sec)%s",
413 (unsigned long long) cfg
->soft_add_expires_seconds
,
414 (unsigned long long) cfg
->hard_add_expires_seconds
,
419 fprintf(fp
, " expire use: soft %llu(sec), hard %llu(sec)%s",
420 (unsigned long long) cfg
->soft_use_expires_seconds
,
421 (unsigned long long) cfg
->hard_use_expires_seconds
,
427 fprintf(fp
, "lifetime current:%s", _SL_
);
431 fprintf(fp
, " %llu(bytes), %llu(packets)%s",
432 (unsigned long long) cur
->bytes
,
433 (unsigned long long) cur
->packets
,
438 fprintf(fp
, " add %s ", strxf_time(cur
->add_time
));
439 fprintf(fp
, "use %s%s", strxf_time(cur
->use_time
), _SL_
);
443 void xfrm_selector_print(struct xfrm_selector
*sel
, __u16 family
,
444 FILE *fp
, const char *prefix
)
453 f
= preferred_family
;
458 memset(abuf
, '\0', sizeof(abuf
));
459 fprintf(fp
, "src %s/%u ", rt_addr_n2a(f
, sizeof(sel
->saddr
),
460 &sel
->saddr
, abuf
, sizeof(abuf
)),
463 memset(abuf
, '\0', sizeof(abuf
));
464 fprintf(fp
, "dst %s/%u ", rt_addr_n2a(f
, sizeof(sel
->daddr
),
465 &sel
->daddr
, abuf
, sizeof(abuf
)),
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 __u16 family
, 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 %d/0x%x\n", m
->v
, m
->m
);
698 if (tb
[XFRMA_ALG_AUTH
] && !tb
[XFRMA_ALG_AUTH_TRUNC
]) {
699 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH
];
700 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
701 XFRMA_ALG_AUTH
, RTA_PAYLOAD(rta
), fp
, prefix
);
704 if (tb
[XFRMA_ALG_AUTH_TRUNC
]) {
705 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH_TRUNC
];
706 xfrm_auth_trunc_print((struct xfrm_algo_auth
*) RTA_DATA(rta
),
707 RTA_PAYLOAD(rta
), fp
, prefix
);
710 if (tb
[XFRMA_ALG_AEAD
]) {
711 struct rtattr
*rta
= tb
[XFRMA_ALG_AEAD
];
712 xfrm_aead_print((struct xfrm_algo_aead
*)RTA_DATA(rta
),
713 RTA_PAYLOAD(rta
), fp
, prefix
);
716 if (tb
[XFRMA_ALG_CRYPT
]) {
717 struct rtattr
*rta
= tb
[XFRMA_ALG_CRYPT
];
718 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
719 XFRMA_ALG_CRYPT
, RTA_PAYLOAD(rta
), fp
, prefix
);
722 if (tb
[XFRMA_ALG_COMP
]) {
723 struct rtattr
*rta
= tb
[XFRMA_ALG_COMP
];
724 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
725 XFRMA_ALG_COMP
, RTA_PAYLOAD(rta
), fp
, prefix
);
728 if (tb
[XFRMA_ENCAP
]) {
729 struct xfrm_encap_tmpl
*e
;
734 fprintf(fp
, "encap ");
736 if (RTA_PAYLOAD(tb
[XFRMA_ENCAP
]) < sizeof(*e
)) {
737 fprintf(fp
, "(ERROR truncated)");
738 fprintf(fp
, "%s", _SL_
);
741 e
= (struct xfrm_encap_tmpl
*) RTA_DATA(tb
[XFRMA_ENCAP
]);
743 fprintf(fp
, "type ");
744 switch (e
->encap_type
) {
746 fprintf(fp
, "espinudp-nonike ");
749 fprintf(fp
, "espinudp ");
752 fprintf(fp
, "%u ", e
->encap_type
);
755 fprintf(fp
, "sport %u ", ntohs(e
->encap_sport
));
756 fprintf(fp
, "dport %u ", ntohs(e
->encap_dport
));
758 memset(abuf
, '\0', sizeof(abuf
));
759 fprintf(fp
, "addr %s",
760 rt_addr_n2a(family
, sizeof(e
->encap_oa
),
761 &e
->encap_oa
, abuf
, sizeof(abuf
)));
762 fprintf(fp
, "%s", _SL_
);
765 if (tb
[XFRMA_TMPL
]) {
766 struct rtattr
*rta
= tb
[XFRMA_TMPL
];
767 xfrm_tmpl_print((struct xfrm_user_tmpl
*) RTA_DATA(rta
),
768 RTA_PAYLOAD(rta
), family
, fp
, prefix
);
771 if (tb
[XFRMA_COADDR
]) {
779 coa
= (xfrm_address_t
*)RTA_DATA(tb
[XFRMA_COADDR
]);
781 if (RTA_PAYLOAD(tb
[XFRMA_COADDR
]) < sizeof(*coa
)) {
782 fprintf(fp
, "(ERROR truncated)");
783 fprintf(fp
, "%s", _SL_
);
787 memset(abuf
, '\0', sizeof(abuf
));
789 rt_addr_n2a(family
, sizeof(*coa
), coa
,
790 abuf
, sizeof(abuf
)));
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_
);
815 static int xfrm_selector_iszero(struct xfrm_selector
*s
)
817 struct xfrm_selector s0
;
819 memset(&s0
, 0, sizeof(s0
));
821 return (memcmp(&s0
, s
, sizeof(s0
)) == 0);
824 void xfrm_state_info_print(struct xfrm_usersa_info
*xsinfo
,
825 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
828 char buf
[STRBUF_SIZE
];
829 int force_spi
= xfrm_xfrmproto_is_ipsec(xsinfo
->id
.proto
);
831 memset(buf
, '\0', sizeof(buf
));
833 xfrm_id_info_print(&xsinfo
->saddr
, &xsinfo
->id
, xsinfo
->mode
,
834 xsinfo
->reqid
, xsinfo
->family
, force_spi
, fp
,
838 STRBUF_CAT(buf
, prefix
);
839 STRBUF_CAT(buf
, "\t");
842 fprintf(fp
, "replay-window %u ", xsinfo
->replay_window
);
844 fprintf(fp
, "seq 0x%08u ", xsinfo
->seq
);
845 if (show_stats
> 0 || xsinfo
->flags
) {
846 __u8 flags
= xsinfo
->flags
;
848 fprintf(fp
, "flag ");
849 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOECN
, "noecn");
850 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_DECAP_DSCP
, "decap-dscp");
851 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOPMTUDISC
, "nopmtudisc");
852 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_WILDRECV
, "wildrecv");
853 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ICMP
, "icmp");
854 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_AF_UNSPEC
, "af-unspec");
855 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ALIGN4
, "align4");
857 fprintf(fp
, "%x", flags
);
859 if (show_stats
> 0 || tb
[XFRMA_SA_EXTRA_FLAGS
]) {
860 __u32 extra_flags
= *(__u32
*)RTA_DATA(tb
[XFRMA_SA_EXTRA_FLAGS
]);
862 fprintf(fp
, "extra_flag ");
863 XFRM_FLAG_PRINT(fp
, extra_flags
,
864 XFRM_SA_XFLAG_DONT_ENCAP_DSCP
,
867 fprintf(fp
, "%x", extra_flags
);
870 fprintf(fp
, " (0x%s)", strxf_mask8(xsinfo
->flags
));
871 fprintf(fp
, "%s", _SL_
);
873 xfrm_xfrma_print(tb
, xsinfo
->family
, fp
, buf
);
875 if (!xfrm_selector_iszero(&xsinfo
->sel
)) {
876 char sbuf
[STRBUF_SIZE
];
878 memcpy(sbuf
, buf
, sizeof(sbuf
));
879 STRBUF_CAT(sbuf
, "sel ");
881 xfrm_selector_print(&xsinfo
->sel
, xsinfo
->family
, fp
, sbuf
);
884 if (show_stats
> 0) {
885 xfrm_lifetime_print(&xsinfo
->lft
, &xsinfo
->curlft
, fp
, buf
);
886 xfrm_stats_print(&xsinfo
->stats
, fp
, buf
);
889 if (tb
[XFRMA_SEC_CTX
]) {
890 struct xfrm_user_sec_ctx
*sctx
;
892 fprintf(fp
, "\tsecurity context ");
894 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
895 fprintf(fp
, "(ERROR truncated)");
897 sctx
= (struct xfrm_user_sec_ctx
*)RTA_DATA(tb
[XFRMA_SEC_CTX
]);
899 fprintf(fp
, "%s %s", (char *)(sctx
+ 1), _SL_
);
904 void xfrm_policy_info_print(struct xfrm_userpolicy_info
*xpinfo
,
905 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
908 char buf
[STRBUF_SIZE
];
910 memset(buf
, '\0', sizeof(buf
));
912 xfrm_selector_print(&xpinfo
->sel
, preferred_family
, fp
, title
);
914 if (tb
[XFRMA_SEC_CTX
]) {
915 struct xfrm_user_sec_ctx
*sctx
;
917 fprintf(fp
, "\tsecurity context ");
919 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
920 fprintf(fp
, "(ERROR truncated)");
922 sctx
= (struct xfrm_user_sec_ctx
*)RTA_DATA(tb
[XFRMA_SEC_CTX
]);
924 fprintf(fp
, "%s ", (char *)(sctx
+ 1));
925 fprintf(fp
, "%s", _SL_
);
929 STRBUF_CAT(buf
, prefix
);
930 STRBUF_CAT(buf
, "\t");
933 if (xpinfo
->dir
>= XFRM_POLICY_MAX
) {
934 xpinfo
->dir
-= XFRM_POLICY_MAX
;
935 fprintf(fp
, "socket ");
939 switch (xpinfo
->dir
) {
943 case XFRM_POLICY_OUT
:
946 case XFRM_POLICY_FWD
:
950 fprintf(fp
, "%u", xpinfo
->dir
);
955 switch (xpinfo
->action
) {
956 case XFRM_POLICY_ALLOW
:
958 fprintf(fp
, "action allow ");
960 case XFRM_POLICY_BLOCK
:
961 fprintf(fp
, "action block ");
964 fprintf(fp
, "action %u ", xpinfo
->action
);
969 fprintf(fp
, "index %u ", xpinfo
->index
);
970 fprintf(fp
, "priority %u ", xpinfo
->priority
);
972 if (tb
[XFRMA_POLICY_TYPE
]) {
973 struct xfrm_userpolicy_type
*upt
;
975 fprintf(fp
, "ptype ");
977 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
))
978 fprintf(fp
, "(ERROR truncated)");
980 upt
= (struct xfrm_userpolicy_type
*)RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
981 fprintf(fp
, "%s ", strxf_ptype(upt
->type
));
985 fprintf(fp
, "share %s ", strxf_share(xpinfo
->share
));
987 if (show_stats
> 0 || xpinfo
->flags
) {
988 __u8 flags
= xpinfo
->flags
;
990 fprintf(fp
, "flag ");
991 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_LOCALOK
, "localok");
992 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_ICMP
, "icmp");
994 fprintf(fp
, "%x", flags
);
997 fprintf(fp
, " (0x%s)", strxf_mask8(xpinfo
->flags
));
998 fprintf(fp
, "%s", _SL_
);
1001 xfrm_lifetime_print(&xpinfo
->lft
, &xpinfo
->curlft
, fp
, buf
);
1003 xfrm_xfrma_print(tb
, xpinfo
->sel
.family
, fp
, buf
);
1006 int xfrm_id_parse(xfrm_address_t
*saddr
, struct xfrm_id
*id
, __u16
*family
,
1007 int loose
, int *argcp
, char ***argvp
)
1010 char **argv
= *argvp
;
1014 memset(&dst
, 0, sizeof(dst
));
1015 memset(&src
, 0, sizeof(src
));
1018 if (strcmp(*argv
, "src") == 0) {
1021 get_prefix(&src
, *argv
, preferred_family
);
1022 if (src
.family
== AF_UNSPEC
)
1023 invarg("value after \"src\" has an unrecognized address family", *argv
);
1025 *family
= src
.family
;
1027 memcpy(saddr
, &src
.data
, sizeof(*saddr
));
1029 filter
.id_src_mask
= src
.bitlen
;
1031 } else if (strcmp(*argv
, "dst") == 0) {
1034 get_prefix(&dst
, *argv
, preferred_family
);
1035 if (dst
.family
== AF_UNSPEC
)
1036 invarg("value after \"dst\" has an unrecognized address family", *argv
);
1038 *family
= dst
.family
;
1040 memcpy(&id
->daddr
, &dst
.data
, sizeof(id
->daddr
));
1042 filter
.id_dst_mask
= dst
.bitlen
;
1044 } else if (strcmp(*argv
, "proto") == 0) {
1049 ret
= xfrm_xfrmproto_getbyname(*argv
);
1051 invarg("XFRM-PROTO value is invalid", *argv
);
1053 id
->proto
= (__u8
)ret
;
1055 filter
.id_proto_mask
= XFRM_FILTER_MASK_FULL
;
1057 } else if (strcmp(*argv
, "spi") == 0) {
1061 if (get_u32(&spi
, *argv
, 0))
1062 invarg("SPI value is invalid", *argv
);
1067 filter
.id_spi_mask
= XFRM_FILTER_MASK_FULL
;
1070 PREV_ARG(); /* back track */
1079 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1080 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv
);
1082 if (id
->spi
&& id
->proto
) {
1083 if (xfrm_xfrmproto_is_ro(id
->proto
)) {
1084 fprintf(stderr
, "\"spi\" is invalid with XFRM-PROTO value \"%s\"\n",
1085 strxf_xfrmproto(id
->proto
));
1087 } else if (id
->proto
== IPPROTO_COMP
&& ntohl(id
->spi
) >= 0x10000) {
1088 fprintf(stderr
, "SPI value is too large with XFRM-PROTO value \"%s\"\n",
1089 strxf_xfrmproto(id
->proto
));
1094 if (loose
== 0 && id
->proto
== 0)
1095 missarg("XFRM-PROTO");
1105 int xfrm_mode_parse(__u8
*mode
, int *argcp
, char ***argvp
)
1108 char **argv
= *argvp
;
1110 if (matches(*argv
, "transport") == 0)
1111 *mode
= XFRM_MODE_TRANSPORT
;
1112 else if (matches(*argv
, "tunnel") == 0)
1113 *mode
= XFRM_MODE_TUNNEL
;
1114 else if (matches(*argv
, "ro") == 0)
1115 *mode
= XFRM_MODE_ROUTEOPTIMIZATION
;
1116 else if (matches(*argv
, "in_trigger") == 0)
1117 *mode
= XFRM_MODE_IN_TRIGGER
;
1118 else if (matches(*argv
, "beet") == 0)
1119 *mode
= XFRM_MODE_BEET
;
1121 invarg("MODE value is invalid", *argv
);
1129 int xfrm_encap_type_parse(__u16
*type
, int *argcp
, char ***argvp
)
1132 char **argv
= *argvp
;
1134 if (strcmp(*argv
, "espinudp-nonike") == 0)
1136 else if (strcmp(*argv
, "espinudp") == 0)
1139 invarg("ENCAP-TYPE value is invalid", *argv
);
1147 /* NOTE: reqid is used by host-byte order */
1148 int xfrm_reqid_parse(__u32
*reqid
, int *argcp
, char ***argvp
)
1151 char **argv
= *argvp
;
1153 if (get_u32(reqid
, *argv
, 0))
1154 invarg("REQID value is invalid", *argv
);
1162 static int xfrm_selector_upspec_parse(struct xfrm_selector
*sel
,
1163 int *argcp
, char ***argvp
)
1166 char **argv
= *argvp
;
1167 char *sportp
= NULL
;
1168 char *dportp
= NULL
;
1171 char *grekey
= NULL
;
1174 if (strcmp(*argv
, "proto") == 0) {
1179 if (strcmp(*argv
, "any") == 0)
1182 struct protoent
*pp
;
1183 pp
= getprotobyname(*argv
);
1185 upspec
= pp
->p_proto
;
1187 if (get_u8(&upspec
, *argv
, 0))
1188 invarg("PROTO value is invalid", *argv
);
1191 sel
->proto
= upspec
;
1193 filter
.upspec_proto_mask
= XFRM_FILTER_MASK_FULL
;
1195 } else if (strcmp(*argv
, "sport") == 0) {
1200 if (get_u16(&sel
->sport
, *argv
, 0))
1201 invarg("value after \"sport\" is invalid", *argv
);
1202 sel
->sport
= htons(sel
->sport
);
1204 sel
->sport_mask
= ~((__u16
)0);
1206 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1208 } else if (strcmp(*argv
, "dport") == 0) {
1213 if (get_u16(&sel
->dport
, *argv
, 0))
1214 invarg("value after \"dport\" is invalid", *argv
);
1215 sel
->dport
= htons(sel
->dport
);
1217 sel
->dport_mask
= ~((__u16
)0);
1219 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1221 } else if (strcmp(*argv
, "type") == 0) {
1226 if (get_u16(&sel
->sport
, *argv
, 0) ||
1227 (sel
->sport
& ~((__u16
)0xff)))
1228 invarg("value after \"type\" is invalid", *argv
);
1229 sel
->sport
= htons(sel
->sport
);
1230 sel
->sport_mask
= ~((__u16
)0);
1232 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1235 } else if (strcmp(*argv
, "code") == 0) {
1240 if (get_u16(&sel
->dport
, *argv
, 0) ||
1241 (sel
->dport
& ~((__u16
)0xff)))
1242 invarg("value after \"code\" is invalid", *argv
);
1243 sel
->dport
= htons(sel
->dport
);
1244 sel
->dport_mask
= ~((__u16
)0);
1246 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1248 } else if (strcmp(*argv
, "key") == 0) {
1255 if (strchr(*argv
, '.'))
1256 uval
= htonl(get_addr32(*argv
));
1258 if (get_unsigned(&uval
, *argv
, 0)<0) {
1259 fprintf(stderr
, "value after \"key\" is invalid\n");
1264 sel
->sport
= htons(uval
>> 16);
1265 sel
->dport
= htons(uval
& 0xffff);
1266 sel
->sport_mask
= ~((__u16
)0);
1267 sel
->dport_mask
= ~((__u16
)0);
1269 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1272 PREV_ARG(); /* back track */
1282 if (sportp
|| dportp
) {
1283 switch (sel
->proto
) {
1290 fprintf(stderr
, "\"sport\" and \"dport\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1294 if (typep
|| codep
) {
1295 switch (sel
->proto
) {
1297 case IPPROTO_ICMPV6
:
1301 fprintf(stderr
, "\"type\" and \"code\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1306 switch (sel
->proto
) {
1310 fprintf(stderr
, "\"key\" is invalid with PROTO value \"%s\"\n", strxf_proto(sel
->proto
));
1321 int xfrm_selector_parse(struct xfrm_selector
*sel
, int *argcp
, char ***argvp
)
1324 char **argv
= *argvp
;
1327 char *upspecp
= NULL
;
1329 memset(&dst
, 0, sizeof(dst
));
1330 memset(&src
, 0, sizeof(src
));
1333 if (strcmp(*argv
, "src") == 0) {
1336 get_prefix(&src
, *argv
, preferred_family
);
1337 if (src
.family
== AF_UNSPEC
)
1338 invarg("value after \"src\" has an unrecognized address family", *argv
);
1339 sel
->family
= src
.family
;
1341 memcpy(&sel
->saddr
, &src
.data
, sizeof(sel
->saddr
));
1342 sel
->prefixlen_s
= src
.bitlen
;
1344 filter
.sel_src_mask
= src
.bitlen
;
1346 } else if (strcmp(*argv
, "dst") == 0) {
1349 get_prefix(&dst
, *argv
, preferred_family
);
1350 if (dst
.family
== AF_UNSPEC
)
1351 invarg("value after \"dst\" has an unrecognized address family", *argv
);
1352 sel
->family
= dst
.family
;
1354 memcpy(&sel
->daddr
, &dst
.data
, sizeof(sel
->daddr
));
1355 sel
->prefixlen_d
= dst
.bitlen
;
1357 filter
.sel_dst_mask
= dst
.bitlen
;
1359 } else if (strcmp(*argv
, "dev") == 0) {
1364 if (strcmp(*argv
, "none") == 0)
1367 ifindex
= ll_name_to_index(*argv
);
1369 invarg("DEV value is invalid", *argv
);
1371 sel
->ifindex
= ifindex
;
1373 filter
.sel_dev_mask
= XFRM_FILTER_MASK_FULL
;
1377 PREV_ARG(); /* back track */
1381 xfrm_selector_upspec_parse(sel
, &argc
, &argv
);
1391 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1392 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv
);
1395 missarg("SELECTOR");
1403 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg
*lft
,
1404 int *argcp
, char ***argvp
)
1407 char **argv
= *argvp
;
1410 if (strcmp(*argv
, "time-soft") == 0) {
1412 ret
= get_u64(&lft
->soft_add_expires_seconds
, *argv
, 0);
1414 invarg("value after \"time-soft\" is invalid", *argv
);
1415 } else if (strcmp(*argv
, "time-hard") == 0) {
1417 ret
= get_u64(&lft
->hard_add_expires_seconds
, *argv
, 0);
1419 invarg("value after \"time-hard\" is invalid", *argv
);
1420 } else if (strcmp(*argv
, "time-use-soft") == 0) {
1422 ret
= get_u64(&lft
->soft_use_expires_seconds
, *argv
, 0);
1424 invarg("value after \"time-use-soft\" is invalid", *argv
);
1425 } else if (strcmp(*argv
, "time-use-hard") == 0) {
1427 ret
= get_u64(&lft
->hard_use_expires_seconds
, *argv
, 0);
1429 invarg("value after \"time-use-hard\" is invalid", *argv
);
1430 } else if (strcmp(*argv
, "byte-soft") == 0) {
1432 ret
= get_u64(&lft
->soft_byte_limit
, *argv
, 0);
1434 invarg("value after \"byte-soft\" is invalid", *argv
);
1435 } else if (strcmp(*argv
, "byte-hard") == 0) {
1437 ret
= get_u64(&lft
->hard_byte_limit
, *argv
, 0);
1439 invarg("value after \"byte-hard\" is invalid", *argv
);
1440 } else if (strcmp(*argv
, "packet-soft") == 0) {
1442 ret
= get_u64(&lft
->soft_packet_limit
, *argv
, 0);
1444 invarg("value after \"packet-soft\" is invalid", *argv
);
1445 } else if (strcmp(*argv
, "packet-hard") == 0) {
1447 ret
= get_u64(&lft
->hard_packet_limit
, *argv
, 0);
1449 invarg("value after \"packet-hard\" is invalid", *argv
);
1451 invarg("LIMIT value is invalid", *argv
);
1459 int do_xfrm(int argc
, char **argv
)
1461 memset(&filter
, 0, sizeof(filter
));
1466 if (matches(*argv
, "state") == 0 ||
1467 matches(*argv
, "sa") == 0)
1468 return do_xfrm_state(argc
-1, argv
+1);
1469 else if (matches(*argv
, "policy") == 0)
1470 return do_xfrm_policy(argc
-1, argv
+1);
1471 else if (matches(*argv
, "monitor") == 0)
1472 return do_xfrm_monitor(argc
-1, argv
+1);
1473 else if (matches(*argv
, "help") == 0) {
1475 fprintf(stderr
, "xfrm Object \"%s\" is unknown.\n", *argv
);