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>
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 ", rt_addr_n2a(f
, sizeof(sel
->saddr
),
459 &sel
->saddr
, abuf
, sizeof(abuf
)),
462 memset(abuf
, '\0', sizeof(abuf
));
463 fprintf(fp
, "dst %s/%u ", rt_addr_n2a(f
, sizeof(sel
->daddr
),
464 &sel
->daddr
, abuf
, sizeof(abuf
)),
468 fprintf(fp
, "proto %s ", strxf_proto(sel
->proto
));
469 switch (sel
->proto
) {
476 fprintf(fp
, "sport %u ", ntohs(sel
->sport
));
478 fprintf(fp
, "dport %u ", ntohs(sel
->dport
));
482 /* type/code is stored at sport/dport in selector */
484 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
486 fprintf(fp
, "code %u ", ntohs(sel
->dport
));
489 if (sel
->sport_mask
|| sel
->dport_mask
)
490 fprintf(fp
, "key %u ",
491 (((__u32
)ntohs(sel
->sport
)) << 16) +
496 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
497 if (sel
->dport_mask
) {
499 fprintf(fp
, "(dport) 0x%.4x ", sel
->dport
);
504 if (sel
->ifindex
> 0)
505 fprintf(fp
, "dev %s ", ll_index_to_name(sel
->ifindex
));
508 fprintf(fp
, "uid %u", sel
->user
);
510 fprintf(fp
, "%s", _SL_
);
513 static void __xfrm_algo_print(struct xfrm_algo
*algo
, int type
, int len
,
514 FILE *fp
, const char *prefix
, int newline
)
522 fprintf(fp
, "%s ", strxf_algotype(type
));
524 if (len
< sizeof(*algo
)) {
525 fprintf(fp
, "(ERROR truncated)");
528 len
-= sizeof(*algo
);
530 fprintf(fp
, "%s ", algo
->alg_name
);
532 keylen
= algo
->alg_key_len
/ 8;
534 fprintf(fp
, "(ERROR truncated)");
539 for (i
= 0; i
< keylen
; i
++)
540 fprintf(fp
, "%.2x", (unsigned char)algo
->alg_key
[i
]);
543 fprintf(fp
, " (%d bits)", algo
->alg_key_len
);
547 fprintf(fp
, "%s", _SL_
);
550 static inline void xfrm_algo_print(struct xfrm_algo
*algo
, int type
, int len
,
551 FILE *fp
, const char *prefix
)
553 return __xfrm_algo_print(algo
, type
, len
, fp
, prefix
, 1);
556 static void xfrm_aead_print(struct xfrm_algo_aead
*algo
, int len
,
557 FILE *fp
, const char *prefix
)
559 struct xfrm_algo
*base_algo
= alloca(sizeof(*base_algo
) + algo
->alg_key_len
/ 8);
561 memcpy(base_algo
->alg_name
, algo
->alg_name
, sizeof(base_algo
->alg_name
));
562 base_algo
->alg_key_len
= algo
->alg_key_len
;
563 memcpy(base_algo
->alg_key
, algo
->alg_key
, algo
->alg_key_len
/ 8);
565 __xfrm_algo_print(base_algo
, XFRMA_ALG_AEAD
, len
, fp
, prefix
, 0);
567 fprintf(fp
, " %d", algo
->alg_icv_len
);
569 fprintf(fp
, "%s", _SL_
);
572 static void xfrm_auth_trunc_print(struct xfrm_algo_auth
*algo
, int len
,
573 FILE *fp
, const char *prefix
)
575 struct xfrm_algo
*base_algo
= alloca(sizeof(*base_algo
) + algo
->alg_key_len
/ 8);
577 memcpy(base_algo
->alg_name
, algo
->alg_name
, sizeof(base_algo
->alg_name
));
578 base_algo
->alg_key_len
= algo
->alg_key_len
;
579 memcpy(base_algo
->alg_key
, algo
->alg_key
, algo
->alg_key_len
/ 8);
581 __xfrm_algo_print(base_algo
, XFRMA_ALG_AUTH_TRUNC
, len
, fp
, prefix
, 0);
583 fprintf(fp
, " %d", algo
->alg_trunc_len
);
585 fprintf(fp
, "%s", _SL_
);
588 static void xfrm_tmpl_print(struct xfrm_user_tmpl
*tmpls
, int len
,
589 __u16 family
, FILE *fp
, const char *prefix
)
591 int ntmpls
= len
/ sizeof(struct xfrm_user_tmpl
);
597 fprintf(fp
, "(ERROR \"tmpl\" truncated)");
598 fprintf(fp
, "%s", _SL_
);
602 for (i
= 0; i
< ntmpls
; i
++) {
603 struct xfrm_user_tmpl
*tmpl
= &tmpls
[i
];
608 xfrm_id_info_print(&tmpl
->saddr
, &tmpl
->id
, tmpl
->mode
,
609 tmpl
->reqid
, tmpl
->family
, 0, fp
, prefix
, "tmpl ");
611 if (show_stats
> 0 || tmpl
->optional
) {
615 switch (tmpl
->optional
) {
618 fprintf(fp
, "level required ");
621 fprintf(fp
, "level use ");
624 fprintf(fp
, "level %u ", tmpl
->optional
);
629 fprintf(fp
, "share %s ", strxf_share(tmpl
->share
));
631 fprintf(fp
, "%s", _SL_
);
634 if (show_stats
> 0) {
638 fprintf(fp
, "%s-mask %s ",
639 strxf_algotype(XFRMA_ALG_CRYPT
),
640 strxf_mask32(tmpl
->ealgos
));
641 fprintf(fp
, "%s-mask %s ",
642 strxf_algotype(XFRMA_ALG_AUTH
),
643 strxf_mask32(tmpl
->aalgos
));
644 fprintf(fp
, "%s-mask %s",
645 strxf_algotype(XFRMA_ALG_COMP
),
646 strxf_mask32(tmpl
->calgos
));
648 fprintf(fp
, "%s", _SL_
);
653 int xfrm_parse_mark(struct xfrm_mark
*mark
, int *argcp
, char ***argvp
)
656 char **argv
= *argvp
;
659 if (get_u32(&mark
->v
, *argv
, 0)) {
660 invarg("Illegal \"mark\" value\n", *argv
);
664 else { /* last entry on parse line */
665 mark
->m
= 0xffffffff;
669 if (strcmp(*argv
, "mask") == 0) {
671 if (get_u32(&mark
->m
, *argv
, 0)) {
672 invarg("Illegal \"mark\" mask\n", *argv
);
675 mark
->m
= 0xffffffff;
686 void xfrm_xfrma_print(struct rtattr
*tb
[], __u16 family
,
687 FILE *fp
, const char *prefix
)
689 if (tb
[XFRMA_MARK
]) {
690 struct rtattr
*rta
= tb
[XFRMA_MARK
];
691 struct xfrm_mark
*m
= (struct xfrm_mark
*) RTA_DATA(rta
);
692 fprintf(fp
, "\tmark %d/0x%x\n", m
->v
, m
->m
);
695 if (tb
[XFRMA_ALG_AUTH
] && !tb
[XFRMA_ALG_AUTH_TRUNC
]) {
696 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH
];
697 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
698 XFRMA_ALG_AUTH
, RTA_PAYLOAD(rta
), fp
, prefix
);
701 if (tb
[XFRMA_ALG_AUTH_TRUNC
]) {
702 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH_TRUNC
];
703 xfrm_auth_trunc_print((struct xfrm_algo_auth
*) RTA_DATA(rta
),
704 RTA_PAYLOAD(rta
), fp
, prefix
);
707 if (tb
[XFRMA_ALG_AEAD
]) {
708 struct rtattr
*rta
= tb
[XFRMA_ALG_AEAD
];
709 xfrm_aead_print((struct xfrm_algo_aead
*)RTA_DATA(rta
),
710 RTA_PAYLOAD(rta
), fp
, prefix
);
713 if (tb
[XFRMA_ALG_CRYPT
]) {
714 struct rtattr
*rta
= tb
[XFRMA_ALG_CRYPT
];
715 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
716 XFRMA_ALG_CRYPT
, RTA_PAYLOAD(rta
), fp
, prefix
);
719 if (tb
[XFRMA_ALG_COMP
]) {
720 struct rtattr
*rta
= tb
[XFRMA_ALG_COMP
];
721 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
722 XFRMA_ALG_COMP
, RTA_PAYLOAD(rta
), fp
, prefix
);
725 if (tb
[XFRMA_ENCAP
]) {
726 struct xfrm_encap_tmpl
*e
;
731 fprintf(fp
, "encap ");
733 if (RTA_PAYLOAD(tb
[XFRMA_ENCAP
]) < sizeof(*e
)) {
734 fprintf(fp
, "(ERROR truncated)");
735 fprintf(fp
, "%s", _SL_
);
738 e
= (struct xfrm_encap_tmpl
*) RTA_DATA(tb
[XFRMA_ENCAP
]);
740 fprintf(fp
, "type ");
741 switch (e
->encap_type
) {
743 fprintf(fp
, "espinudp-nonike ");
746 fprintf(fp
, "espinudp ");
749 fprintf(fp
, "%u ", e
->encap_type
);
752 fprintf(fp
, "sport %u ", ntohs(e
->encap_sport
));
753 fprintf(fp
, "dport %u ", ntohs(e
->encap_dport
));
755 memset(abuf
, '\0', sizeof(abuf
));
756 fprintf(fp
, "addr %s",
757 rt_addr_n2a(family
, sizeof(e
->encap_oa
),
758 &e
->encap_oa
, abuf
, sizeof(abuf
)));
759 fprintf(fp
, "%s", _SL_
);
762 if (tb
[XFRMA_TMPL
]) {
763 struct rtattr
*rta
= tb
[XFRMA_TMPL
];
764 xfrm_tmpl_print((struct xfrm_user_tmpl
*) RTA_DATA(rta
),
765 RTA_PAYLOAD(rta
), family
, fp
, prefix
);
768 if (tb
[XFRMA_COADDR
]) {
776 coa
= (xfrm_address_t
*)RTA_DATA(tb
[XFRMA_COADDR
]);
778 if (RTA_PAYLOAD(tb
[XFRMA_COADDR
]) < sizeof(*coa
)) {
779 fprintf(fp
, "(ERROR truncated)");
780 fprintf(fp
, "%s", _SL_
);
784 memset(abuf
, '\0', sizeof(abuf
));
786 rt_addr_n2a(family
, sizeof(*coa
), coa
,
787 abuf
, sizeof(abuf
)));
788 fprintf(fp
, "%s", _SL_
);
791 if (tb
[XFRMA_LASTUSED
]) {
796 fprintf(fp
, "lastused ");
798 if (RTA_PAYLOAD(tb
[XFRMA_LASTUSED
]) < sizeof(lastused
)) {
799 fprintf(fp
, "(ERROR truncated)");
800 fprintf(fp
, "%s", _SL_
);
804 lastused
= rta_getattr_u64(tb
[XFRMA_LASTUSED
]);
806 fprintf(fp
, "%s", strxf_time(lastused
));
807 fprintf(fp
, "%s", _SL_
);
812 static int xfrm_selector_iszero(struct xfrm_selector
*s
)
814 struct xfrm_selector s0
;
816 memset(&s0
, 0, sizeof(s0
));
818 return (memcmp(&s0
, s
, sizeof(s0
)) == 0);
821 void xfrm_state_info_print(struct xfrm_usersa_info
*xsinfo
,
822 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
825 char buf
[STRBUF_SIZE
];
826 int force_spi
= xfrm_xfrmproto_is_ipsec(xsinfo
->id
.proto
);
828 memset(buf
, '\0', sizeof(buf
));
830 xfrm_id_info_print(&xsinfo
->saddr
, &xsinfo
->id
, xsinfo
->mode
,
831 xsinfo
->reqid
, xsinfo
->family
, force_spi
, fp
,
835 STRBUF_CAT(buf
, prefix
);
836 STRBUF_CAT(buf
, "\t");
839 fprintf(fp
, "replay-window %u ", xsinfo
->replay_window
);
841 fprintf(fp
, "seq 0x%08u ", xsinfo
->seq
);
842 if (show_stats
> 0 || xsinfo
->flags
) {
843 __u8 flags
= xsinfo
->flags
;
845 fprintf(fp
, "flag ");
846 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOECN
, "noecn");
847 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_DECAP_DSCP
, "decap-dscp");
848 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOPMTUDISC
, "nopmtudisc");
849 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_WILDRECV
, "wildrecv");
850 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ICMP
, "icmp");
851 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_AF_UNSPEC
, "af-unspec");
852 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_ALIGN4
, "align4");
854 fprintf(fp
, "%x", flags
);
857 fprintf(fp
, " (0x%s)", strxf_mask8(xsinfo
->flags
));
858 fprintf(fp
, "%s", _SL_
);
860 xfrm_xfrma_print(tb
, xsinfo
->family
, fp
, buf
);
862 if (!xfrm_selector_iszero(&xsinfo
->sel
)) {
863 char sbuf
[STRBUF_SIZE
];
865 memcpy(sbuf
, buf
, sizeof(sbuf
));
866 STRBUF_CAT(sbuf
, "sel ");
868 xfrm_selector_print(&xsinfo
->sel
, xsinfo
->family
, fp
, sbuf
);
871 if (show_stats
> 0) {
872 xfrm_lifetime_print(&xsinfo
->lft
, &xsinfo
->curlft
, fp
, buf
);
873 xfrm_stats_print(&xsinfo
->stats
, fp
, buf
);
876 if (tb
[XFRMA_SEC_CTX
]) {
877 struct xfrm_user_sec_ctx
*sctx
;
879 fprintf(fp
, "\tsecurity context ");
881 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
882 fprintf(fp
, "(ERROR truncated)");
884 sctx
= (struct xfrm_user_sec_ctx
*)RTA_DATA(tb
[XFRMA_SEC_CTX
]);
886 fprintf(fp
, "%s %s", (char *)(sctx
+ 1), _SL_
);
891 void xfrm_policy_info_print(struct xfrm_userpolicy_info
*xpinfo
,
892 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
895 char buf
[STRBUF_SIZE
];
897 memset(buf
, '\0', sizeof(buf
));
899 xfrm_selector_print(&xpinfo
->sel
, preferred_family
, fp
, title
);
901 if (tb
[XFRMA_SEC_CTX
]) {
902 struct xfrm_user_sec_ctx
*sctx
;
904 fprintf(fp
, "\tsecurity context ");
906 if (RTA_PAYLOAD(tb
[XFRMA_SEC_CTX
]) < sizeof(*sctx
))
907 fprintf(fp
, "(ERROR truncated)");
909 sctx
= (struct xfrm_user_sec_ctx
*)RTA_DATA(tb
[XFRMA_SEC_CTX
]);
911 fprintf(fp
, "%s ", (char *)(sctx
+ 1));
912 fprintf(fp
, "%s", _SL_
);
916 STRBUF_CAT(buf
, prefix
);
917 STRBUF_CAT(buf
, "\t");
920 if (xpinfo
->dir
>= XFRM_POLICY_MAX
) {
921 xpinfo
->dir
-= XFRM_POLICY_MAX
;
922 fprintf(fp
, "socket ");
926 switch (xpinfo
->dir
) {
930 case XFRM_POLICY_OUT
:
933 case XFRM_POLICY_FWD
:
937 fprintf(fp
, "%u", xpinfo
->dir
);
942 switch (xpinfo
->action
) {
943 case XFRM_POLICY_ALLOW
:
945 fprintf(fp
, "action allow ");
947 case XFRM_POLICY_BLOCK
:
948 fprintf(fp
, "action block ");
951 fprintf(fp
, "action %u ", xpinfo
->action
);
956 fprintf(fp
, "index %u ", xpinfo
->index
);
957 fprintf(fp
, "priority %u ", xpinfo
->priority
);
959 if (tb
[XFRMA_POLICY_TYPE
]) {
960 struct xfrm_userpolicy_type
*upt
;
962 fprintf(fp
, "ptype ");
964 if (RTA_PAYLOAD(tb
[XFRMA_POLICY_TYPE
]) < sizeof(*upt
))
965 fprintf(fp
, "(ERROR truncated)");
967 upt
= (struct xfrm_userpolicy_type
*)RTA_DATA(tb
[XFRMA_POLICY_TYPE
]);
968 fprintf(fp
, "%s ", strxf_ptype(upt
->type
));
972 fprintf(fp
, "share %s ", strxf_share(xpinfo
->share
));
974 if (show_stats
> 0 || xpinfo
->flags
) {
975 __u8 flags
= xpinfo
->flags
;
977 fprintf(fp
, "flag ");
978 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_LOCALOK
, "localok");
979 XFRM_FLAG_PRINT(fp
, flags
, XFRM_POLICY_ICMP
, "icmp");
981 fprintf(fp
, "%x", flags
);
984 fprintf(fp
, " (0x%s)", strxf_mask8(xpinfo
->flags
));
985 fprintf(fp
, "%s", _SL_
);
988 xfrm_lifetime_print(&xpinfo
->lft
, &xpinfo
->curlft
, fp
, buf
);
990 xfrm_xfrma_print(tb
, xpinfo
->sel
.family
, fp
, buf
);
993 int xfrm_id_parse(xfrm_address_t
*saddr
, struct xfrm_id
*id
, __u16
*family
,
994 int loose
, int *argcp
, char ***argvp
)
997 char **argv
= *argvp
;
1001 memset(&dst
, 0, sizeof(dst
));
1002 memset(&src
, 0, sizeof(src
));
1005 if (strcmp(*argv
, "src") == 0) {
1008 get_prefix(&src
, *argv
, preferred_family
);
1009 if (src
.family
== AF_UNSPEC
)
1010 invarg("\"src\" address family is AF_UNSPEC", *argv
);
1012 *family
= src
.family
;
1014 memcpy(saddr
, &src
.data
, sizeof(*saddr
));
1016 filter
.id_src_mask
= src
.bitlen
;
1018 } else if (strcmp(*argv
, "dst") == 0) {
1021 get_prefix(&dst
, *argv
, preferred_family
);
1022 if (dst
.family
== AF_UNSPEC
)
1023 invarg("\"dst\" address family is AF_UNSPEC", *argv
);
1025 *family
= dst
.family
;
1027 memcpy(&id
->daddr
, &dst
.data
, sizeof(id
->daddr
));
1029 filter
.id_dst_mask
= dst
.bitlen
;
1031 } else if (strcmp(*argv
, "proto") == 0) {
1036 ret
= xfrm_xfrmproto_getbyname(*argv
);
1038 invarg("\"XFRM-PROTO\" is invalid", *argv
);
1040 id
->proto
= (__u8
)ret
;
1042 filter
.id_proto_mask
= XFRM_FILTER_MASK_FULL
;
1044 } else if (strcmp(*argv
, "spi") == 0) {
1048 if (get_u32(&spi
, *argv
, 0))
1049 invarg("\"SPI\" is invalid", *argv
);
1054 filter
.id_spi_mask
= XFRM_FILTER_MASK_FULL
;
1057 PREV_ARG(); /* back track */
1066 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1067 invarg("the same address family is required between \"src\" and \"dst\"", *argv
);
1069 if (loose
== 0 && id
->proto
== 0)
1070 missarg("XFRM-PROTO");
1080 int xfrm_mode_parse(__u8
*mode
, int *argcp
, char ***argvp
)
1083 char **argv
= *argvp
;
1085 if (matches(*argv
, "transport") == 0)
1086 *mode
= XFRM_MODE_TRANSPORT
;
1087 else if (matches(*argv
, "tunnel") == 0)
1088 *mode
= XFRM_MODE_TUNNEL
;
1089 else if (matches(*argv
, "ro") == 0)
1090 *mode
= XFRM_MODE_ROUTEOPTIMIZATION
;
1091 else if (matches(*argv
, "in_trigger") == 0)
1092 *mode
= XFRM_MODE_IN_TRIGGER
;
1093 else if (matches(*argv
, "beet") == 0)
1094 *mode
= XFRM_MODE_BEET
;
1096 invarg("\"MODE\" is invalid", *argv
);
1104 int xfrm_encap_type_parse(__u16
*type
, int *argcp
, char ***argvp
)
1107 char **argv
= *argvp
;
1109 if (strcmp(*argv
, "espinudp-nonike") == 0)
1111 else if (strcmp(*argv
, "espinudp") == 0)
1114 invarg("\"ENCAP-TYPE\" is invalid", *argv
);
1122 /* NOTE: reqid is used by host-byte order */
1123 int xfrm_reqid_parse(__u32
*reqid
, int *argcp
, char ***argvp
)
1126 char **argv
= *argvp
;
1128 if (get_u32(reqid
, *argv
, 0))
1129 invarg("\"REQID\" is invalid", *argv
);
1137 static int xfrm_selector_upspec_parse(struct xfrm_selector
*sel
,
1138 int *argcp
, char ***argvp
)
1141 char **argv
= *argvp
;
1142 char *sportp
= NULL
;
1143 char *dportp
= NULL
;
1146 char *grekey
= NULL
;
1149 if (strcmp(*argv
, "proto") == 0) {
1154 if (strcmp(*argv
, "any") == 0)
1157 struct protoent
*pp
;
1158 pp
= getprotobyname(*argv
);
1160 upspec
= pp
->p_proto
;
1162 if (get_u8(&upspec
, *argv
, 0))
1163 invarg("\"PROTO\" is invalid", *argv
);
1166 sel
->proto
= upspec
;
1168 filter
.upspec_proto_mask
= XFRM_FILTER_MASK_FULL
;
1170 } else if (strcmp(*argv
, "sport") == 0) {
1175 if (get_u16(&sel
->sport
, *argv
, 0))
1176 invarg("\"PORT\" is invalid", *argv
);
1177 sel
->sport
= htons(sel
->sport
);
1179 sel
->sport_mask
= ~((__u16
)0);
1181 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1183 } else if (strcmp(*argv
, "dport") == 0) {
1188 if (get_u16(&sel
->dport
, *argv
, 0))
1189 invarg("\"PORT\" is invalid", *argv
);
1190 sel
->dport
= htons(sel
->dport
);
1192 sel
->dport_mask
= ~((__u16
)0);
1194 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1196 } else if (strcmp(*argv
, "type") == 0) {
1201 if (get_u16(&sel
->sport
, *argv
, 0) ||
1202 (sel
->sport
& ~((__u16
)0xff)))
1203 invarg("\"type\" value is invalid", *argv
);
1204 sel
->sport
= htons(sel
->sport
);
1205 sel
->sport_mask
= ~((__u16
)0);
1207 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
1210 } else if (strcmp(*argv
, "code") == 0) {
1215 if (get_u16(&sel
->dport
, *argv
, 0) ||
1216 (sel
->dport
& ~((__u16
)0xff)))
1217 invarg("\"code\" value is invalid", *argv
);
1218 sel
->dport
= htons(sel
->dport
);
1219 sel
->dport_mask
= ~((__u16
)0);
1221 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1223 } else if (strcmp(*argv
, "key") == 0) {
1230 if (strchr(*argv
, '.'))
1231 uval
= htonl(get_addr32(*argv
));
1233 if (get_unsigned(&uval
, *argv
, 0)<0) {
1234 fprintf(stderr
, "invalid value for \"key\"; it should be an unsigned integer\n");
1239 sel
->sport
= htons(uval
>> 16);
1240 sel
->dport
= htons(uval
& 0xffff);
1241 sel
->sport_mask
= ~((__u16
)0);
1242 sel
->dport_mask
= ~((__u16
)0);
1244 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
1247 PREV_ARG(); /* back track */
1257 if (sportp
|| dportp
) {
1258 switch (sel
->proto
) {
1265 fprintf(stderr
, "\"sport\" and \"dport\" are invalid with proto=%s\n", strxf_proto(sel
->proto
));
1269 if (typep
|| codep
) {
1270 switch (sel
->proto
) {
1272 case IPPROTO_ICMPV6
:
1276 fprintf(stderr
, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel
->proto
));
1281 switch (sel
->proto
) {
1285 fprintf(stderr
, "\"key\" is invalid with proto=%s\n", strxf_proto(sel
->proto
));
1296 int xfrm_selector_parse(struct xfrm_selector
*sel
, int *argcp
, char ***argvp
)
1299 char **argv
= *argvp
;
1302 char *upspecp
= NULL
;
1304 memset(&dst
, 0, sizeof(dst
));
1305 memset(&src
, 0, sizeof(src
));
1308 if (strcmp(*argv
, "src") == 0) {
1311 get_prefix(&src
, *argv
, preferred_family
);
1312 if (src
.family
== AF_UNSPEC
)
1313 invarg("\"src\" address family is AF_UNSPEC", *argv
);
1314 sel
->family
= src
.family
;
1316 memcpy(&sel
->saddr
, &src
.data
, sizeof(sel
->saddr
));
1317 sel
->prefixlen_s
= src
.bitlen
;
1319 filter
.sel_src_mask
= src
.bitlen
;
1321 } else if (strcmp(*argv
, "dst") == 0) {
1324 get_prefix(&dst
, *argv
, preferred_family
);
1325 if (dst
.family
== AF_UNSPEC
)
1326 invarg("\"dst\" address family is AF_UNSPEC", *argv
);
1327 sel
->family
= dst
.family
;
1329 memcpy(&sel
->daddr
, &dst
.data
, sizeof(sel
->daddr
));
1330 sel
->prefixlen_d
= dst
.bitlen
;
1332 filter
.sel_dst_mask
= dst
.bitlen
;
1334 } else if (strcmp(*argv
, "dev") == 0) {
1339 if (strcmp(*argv
, "none") == 0)
1342 ifindex
= ll_name_to_index(*argv
);
1344 invarg("\"DEV\" is invalid", *argv
);
1346 sel
->ifindex
= ifindex
;
1348 filter
.sel_dev_mask
= XFRM_FILTER_MASK_FULL
;
1352 PREV_ARG(); /* back track */
1356 xfrm_selector_upspec_parse(sel
, &argc
, &argv
);
1366 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1367 invarg("the same address family is required between \"src\" and \"dst\"", *argv
);
1370 missarg("SELECTOR");
1378 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg
*lft
,
1379 int *argcp
, char ***argvp
)
1382 char **argv
= *argvp
;
1385 if (strcmp(*argv
, "time-soft") == 0) {
1387 ret
= get_u64(&lft
->soft_add_expires_seconds
, *argv
, 0);
1389 invarg("\"time-soft\" value is invalid", *argv
);
1390 } else if (strcmp(*argv
, "time-hard") == 0) {
1392 ret
= get_u64(&lft
->hard_add_expires_seconds
, *argv
, 0);
1394 invarg("\"time-hard\" value is invalid", *argv
);
1395 } else if (strcmp(*argv
, "time-use-soft") == 0) {
1397 ret
= get_u64(&lft
->soft_use_expires_seconds
, *argv
, 0);
1399 invarg("\"time-use-soft\" value is invalid", *argv
);
1400 } else if (strcmp(*argv
, "time-use-hard") == 0) {
1402 ret
= get_u64(&lft
->hard_use_expires_seconds
, *argv
, 0);
1404 invarg("\"time-use-hard\" value is invalid", *argv
);
1405 } else if (strcmp(*argv
, "byte-soft") == 0) {
1407 ret
= get_u64(&lft
->soft_byte_limit
, *argv
, 0);
1409 invarg("\"byte-soft\" value is invalid", *argv
);
1410 } else if (strcmp(*argv
, "byte-hard") == 0) {
1412 ret
= get_u64(&lft
->hard_byte_limit
, *argv
, 0);
1414 invarg("\"byte-hard\" value is invalid", *argv
);
1415 } else if (strcmp(*argv
, "packet-soft") == 0) {
1417 ret
= get_u64(&lft
->soft_packet_limit
, *argv
, 0);
1419 invarg("\"packet-soft\" value is invalid", *argv
);
1420 } else if (strcmp(*argv
, "packet-hard") == 0) {
1422 ret
= get_u64(&lft
->hard_packet_limit
, *argv
, 0);
1424 invarg("\"packet-hard\" value is invalid", *argv
);
1426 invarg("\"LIMIT\" is invalid", *argv
);
1434 int do_xfrm(int argc
, char **argv
)
1436 memset(&filter
, 0, sizeof(filter
));
1441 if (matches(*argv
, "state") == 0 ||
1442 matches(*argv
, "sa") == 0)
1443 return do_xfrm_state(argc
-1, argv
+1);
1444 else if (matches(*argv
, "policy") == 0)
1445 return do_xfrm_policy(argc
-1, argv
+1);
1446 else if (matches(*argv
, "monitor") == 0)
1447 return do_xfrm_monitor(argc
-1, argv
+1);
1448 else if (matches(*argv
, "help") == 0) {
1450 fprintf(stderr
, "xfrm Object \"%s\" is unknown.\n", *argv
);