]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipxfrm.c
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
31 #include <sys/types.h>
32 #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
));
102 static const struct typeent xfrmproto_types
[]= {
103 { "esp", IPPROTO_ESP
}, { "ah", IPPROTO_AH
}, { "comp", IPPROTO_COMP
},
107 int xfrm_xfrmproto_getbyname(char *name
)
112 const struct typeent
*t
= &xfrmproto_types
[i
];
113 if (!t
->t_name
|| t
->t_type
== -1)
116 if (strcmp(t
->t_name
, name
) == 0)
123 const char *strxf_xfrmproto(__u8 proto
)
128 const struct typeent
*t
= &xfrmproto_types
[i
];
129 if (!t
->t_name
|| t
->t_type
== -1)
132 if (t
->t_type
== proto
)
139 static const struct typeent algo_types
[]= {
140 { "enc", XFRMA_ALG_CRYPT
}, { "auth", XFRMA_ALG_AUTH
},
141 { "comp", XFRMA_ALG_COMP
}, { NULL
, -1 }
144 int xfrm_algotype_getbyname(char *name
)
149 const struct typeent
*t
= &algo_types
[i
];
150 if (!t
->t_name
|| t
->t_type
== -1)
153 if (strcmp(t
->t_name
, name
) == 0)
160 const char *strxf_algotype(int type
)
165 const struct typeent
*t
= &algo_types
[i
];
166 if (!t
->t_name
|| t
->t_type
== -1)
169 if (t
->t_type
== type
)
176 const char *strxf_mask8(__u8 mask
)
179 const int sn
= sizeof(mask
) * 8 - 1;
183 for (b
= (1 << sn
); b
> 0; b
>>= 1)
184 str
[i
++] = ((b
& mask
) ? '1' : '0');
190 const char *strxf_mask32(__u32 mask
)
194 sprintf(str
, "%.8x", mask
);
199 const char *strxf_share(__u8 share
)
207 case XFRM_SHARE_SESSION
:
208 strcpy(str
, "session");
210 case XFRM_SHARE_USER
:
213 case XFRM_SHARE_UNIQUE
:
214 strcpy(str
, "unique");
217 sprintf(str
, "%u", share
);
224 const char *strxf_proto(__u8 proto
)
230 pp
= getprotobynumber(proto
);
234 sprintf(buf
, "%u", proto
);
241 void xfrm_id_info_print(xfrm_address_t
*saddr
, struct xfrm_id
*id
,
242 __u8 mode
, __u32 reqid
, __u16 family
, int force_spi
,
243 FILE *fp
, const char *prefix
, const char *title
)
250 memset(abuf
, '\0', sizeof(abuf
));
251 fprintf(fp
, "src %s ", rt_addr_n2a(family
, sizeof(*saddr
),
252 saddr
, abuf
, sizeof(abuf
)));
253 memset(abuf
, '\0', sizeof(abuf
));
254 fprintf(fp
, "dst %s", rt_addr_n2a(family
, sizeof(id
->daddr
),
255 &id
->daddr
, abuf
, sizeof(abuf
)));
256 fprintf(fp
, "%s", _SL_
);
262 fprintf(fp
, "proto %s ", strxf_xfrmproto(id
->proto
));
264 if (show_stats
> 0 || force_spi
|| id
->spi
) {
265 __u32 spi
= ntohl(id
->spi
);
266 fprintf(fp
, "spi 0x%08x", spi
);
268 fprintf(fp
, "(%u)", spi
);
272 fprintf(fp
, "reqid %u", reqid
);
274 fprintf(fp
, "(0x%08x)", reqid
);
277 fprintf(fp
, "mode ");
280 fprintf(fp
, "transport");
283 fprintf(fp
, "tunnel");
289 fprintf(fp
, "%u", mode
);
292 fprintf(fp
, "%s", _SL_
);
295 static const char *strxf_limit(__u64 limit
)
298 if (limit
== XFRM_INF
)
299 strcpy(str
, "(INF)");
301 sprintf(str
, "%llu", (unsigned long long) limit
);
306 void xfrm_stats_print(struct xfrm_stats
*s
, FILE *fp
, const char *prefix
)
310 fprintf(fp
, "stats:");
311 fprintf(fp
, "%s", _SL_
);
316 fprintf(fp
, "replay-window %u ", s
->replay_window
);
317 fprintf(fp
, "replay %u ", s
->replay
);
318 fprintf(fp
, "failed %u", s
->integrity_failed
);
319 fprintf(fp
, "%s", _SL_
);
322 static const char *strxf_time(__u64 time
)
332 /* XXX: treat time in the same manner of kernel's
333 * net/xfrm/xfrm_{user,state}.c
338 strftime(str
, sizeof(str
), "%Y-%m-%d %T", tp
);
344 void xfrm_lifetime_print(struct xfrm_lifetime_cfg
*cfg
,
345 struct xfrm_lifetime_cur
*cur
,
346 FILE *fp
, const char *prefix
)
351 fprintf(fp
, "lifetime config:");
352 fprintf(fp
, "%s", _SL_
);
357 fprintf(fp
, "limit: ");
358 fprintf(fp
, "soft ");
359 fprintf(fp
, strxf_limit(cfg
->soft_byte_limit
));
360 fprintf(fp
, "(bytes), hard ");
361 fprintf(fp
, strxf_limit(cfg
->hard_byte_limit
));
362 fprintf(fp
, "(bytes)");
363 fprintf(fp
, "%s", _SL_
);
368 fprintf(fp
, "limit: ");
369 fprintf(fp
, "soft ");
370 fprintf(fp
, strxf_limit(cfg
->soft_packet_limit
));
371 fprintf(fp
, "(packets), hard ");
372 fprintf(fp
, strxf_limit(cfg
->hard_packet_limit
));
373 fprintf(fp
, "(packets)");
374 fprintf(fp
, "%s", _SL_
);
379 fprintf(fp
, "expire add: ");
380 fprintf(fp
, "soft ");
381 fprintf(fp
, "%llu", (unsigned long long) cfg
->soft_add_expires_seconds
);
382 fprintf(fp
, "(sec), hard ");
383 fprintf(fp
, "%llu", (unsigned long long) cfg
->hard_add_expires_seconds
);
384 fprintf(fp
, "(sec)");
385 fprintf(fp
, "%s", _SL_
);
390 fprintf(fp
, "expire use: ");
391 fprintf(fp
, "soft ");
392 fprintf(fp
, "%llu", (unsigned long long) cfg
->soft_use_expires_seconds
);
393 fprintf(fp
, "(sec), hard ");
394 fprintf(fp
, "%llu", (unsigned long long) cfg
->hard_use_expires_seconds
);
395 fprintf(fp
, "(sec)");
396 fprintf(fp
, "%s", _SL_
);
401 fprintf(fp
, "lifetime current:");
402 fprintf(fp
, "%s", _SL_
);
407 fprintf(fp
, "%llu(bytes), ", (unsigned long long) cur
->bytes
);
408 fprintf(fp
, "%llu(packets)", (unsigned long long) cur
->packets
);
409 fprintf(fp
, "%s", _SL_
);
414 fprintf(fp
, "add %s ", strxf_time(cur
->add_time
));
415 fprintf(fp
, "use %s", strxf_time(cur
->use_time
));
416 fprintf(fp
, "%s", _SL_
);
420 void xfrm_selector_print(struct xfrm_selector
*sel
, __u16 family
,
421 FILE *fp
, const char *prefix
)
430 f
= preferred_family
;
435 memset(abuf
, '\0', sizeof(abuf
));
436 fprintf(fp
, "src %s/%u ", rt_addr_n2a(f
, sizeof(sel
->saddr
),
437 &sel
->saddr
, abuf
, sizeof(abuf
)),
440 memset(abuf
, '\0', sizeof(abuf
));
441 fprintf(fp
, "dst %s/%u ", rt_addr_n2a(f
, sizeof(sel
->daddr
),
442 &sel
->daddr
, abuf
, sizeof(abuf
)),
446 fprintf(fp
, "proto %s ", strxf_proto(sel
->proto
));
447 switch (sel
->proto
) {
454 fprintf(fp
, "sport %u ", ntohs(sel
->sport
));
456 fprintf(fp
, "dport %u ", ntohs(sel
->dport
));
460 /* type/code is stored at sport/dport in selector */
462 fprintf(fp
, "type %u ", ntohs(sel
->sport
));
464 fprintf(fp
, "code %u ", ntohs(sel
->dport
));
468 if (sel
->ifindex
> 0) {
471 memset(buf
, '\0', sizeof(buf
));
472 if_indextoname(sel
->ifindex
, buf
);
473 fprintf(fp
, "dev %s ", buf
);
477 fprintf(fp
, "uid %u", sel
->user
);
479 fprintf(fp
, "%s", _SL_
);
482 static void xfrm_algo_print(struct xfrm_algo
*algo
, int type
, int len
,
483 FILE *fp
, const char *prefix
)
491 fprintf(fp
, "%s ", strxf_algotype(type
));
493 if (len
< sizeof(*algo
)) {
494 fprintf(fp
, "(ERROR truncated)");
497 len
-= sizeof(*algo
);
499 fprintf(fp
, "%s ", algo
->alg_name
);
501 keylen
= algo
->alg_key_len
/ 8;
503 fprintf(fp
, "(ERROR truncated)");
508 for (i
= 0; i
< keylen
; i
++)
509 fprintf(fp
, "%.2x", (unsigned char)algo
->alg_key
[i
]);
512 fprintf(fp
, " (%d bits)", algo
->alg_key_len
);
515 fprintf(fp
, "%s", _SL_
);
518 static void xfrm_tmpl_print(struct xfrm_user_tmpl
*tmpls
, int len
,
519 __u16 family
, FILE *fp
, const char *prefix
)
521 int ntmpls
= len
/ sizeof(struct xfrm_user_tmpl
);
527 fprintf(fp
, "(ERROR \"tmpl\" truncated)");
528 fprintf(fp
, "%s", _SL_
);
532 for (i
= 0; i
< ntmpls
; i
++) {
533 struct xfrm_user_tmpl
*tmpl
= &tmpls
[i
];
538 xfrm_id_info_print(&tmpl
->saddr
, &tmpl
->id
, tmpl
->mode
,
539 tmpl
->reqid
, family
, 0, fp
, prefix
, "tmpl ");
541 if (show_stats
> 0 || tmpl
->optional
) {
545 switch (tmpl
->optional
) {
548 fprintf(fp
, "level required ");
551 fprintf(fp
, "level use ");
554 fprintf(fp
, "level %u ", tmpl
->optional
);
559 fprintf(fp
, "share %s ", strxf_share(tmpl
->share
));
561 fprintf(fp
, "%s", _SL_
);
564 if (show_stats
> 0) {
568 fprintf(fp
, "%s-mask %s ",
569 strxf_algotype(XFRMA_ALG_CRYPT
),
570 strxf_mask32(tmpl
->ealgos
));
571 fprintf(fp
, "%s-mask %s ",
572 strxf_algotype(XFRMA_ALG_AUTH
),
573 strxf_mask32(tmpl
->aalgos
));
574 fprintf(fp
, "%s-mask %s",
575 strxf_algotype(XFRMA_ALG_COMP
),
576 strxf_mask32(tmpl
->calgos
));
578 fprintf(fp
, "%s", _SL_
);
583 void xfrm_xfrma_print(struct rtattr
*tb
[], __u16 family
,
584 FILE *fp
, const char *prefix
)
586 if (tb
[XFRMA_ALG_AUTH
]) {
587 struct rtattr
*rta
= tb
[XFRMA_ALG_AUTH
];
588 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
589 XFRMA_ALG_AUTH
, RTA_PAYLOAD(rta
), fp
, prefix
);
592 if (tb
[XFRMA_ALG_CRYPT
]) {
593 struct rtattr
*rta
= tb
[XFRMA_ALG_CRYPT
];
594 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
595 XFRMA_ALG_CRYPT
, RTA_PAYLOAD(rta
), fp
, prefix
);
598 if (tb
[XFRMA_ALG_COMP
]) {
599 struct rtattr
*rta
= tb
[XFRMA_ALG_COMP
];
600 xfrm_algo_print((struct xfrm_algo
*) RTA_DATA(rta
),
601 XFRMA_ALG_COMP
, RTA_PAYLOAD(rta
), fp
, prefix
);
604 if (tb
[XFRMA_ENCAP
]) {
605 struct xfrm_encap_tmpl
*e
;
610 fprintf(fp
, "encap ");
612 if (RTA_PAYLOAD(tb
[XFRMA_ENCAP
]) < sizeof(*e
)) {
613 fprintf(fp
, "(ERROR truncated)");
614 fprintf(fp
, "%s", _SL_
);
617 e
= (struct xfrm_encap_tmpl
*) RTA_DATA(tb
[XFRMA_ENCAP
]);
619 fprintf(fp
, "type ");
620 switch (e
->encap_type
) {
622 fprintf(fp
, "espinudp-nonike ");
625 fprintf(fp
, "espinudp ");
628 fprintf(fp
, "%u ", e
->encap_type
);
631 fprintf(fp
, "sport %u ", ntohs(e
->encap_sport
));
632 fprintf(fp
, "dport %u ", ntohs(e
->encap_dport
));
634 memset(abuf
, '\0', sizeof(abuf
));
635 fprintf(fp
, "addr %s",
636 rt_addr_n2a(family
, sizeof(e
->encap_oa
),
637 &e
->encap_oa
, abuf
, sizeof(abuf
)));
638 fprintf(fp
, "%s", _SL_
);
641 if (tb
[XFRMA_TMPL
]) {
642 struct rtattr
*rta
= tb
[XFRMA_TMPL
];
643 xfrm_tmpl_print((struct xfrm_user_tmpl
*) RTA_DATA(rta
),
644 RTA_PAYLOAD(rta
), family
, fp
, prefix
);
648 static int xfrm_selector_iszero(struct xfrm_selector
*s
)
650 struct xfrm_selector s0
;
652 memset(&s0
, 0, sizeof(s0
));
654 return (memcmp(&s0
, s
, sizeof(s0
)) == 0);
657 void xfrm_state_info_print(struct xfrm_usersa_info
*xsinfo
,
658 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
661 char buf
[STRBUF_SIZE
];
663 memset(buf
, '\0', sizeof(buf
));
665 xfrm_id_info_print(&xsinfo
->saddr
, &xsinfo
->id
, xsinfo
->mode
,
666 xsinfo
->reqid
, xsinfo
->family
, 1, fp
, prefix
,
670 STRBUF_CAT(buf
, prefix
);
671 STRBUF_CAT(buf
, "\t");
674 fprintf(fp
, "replay-window %u ", xsinfo
->replay_window
);
676 fprintf(fp
, "seq 0x%08u ", xsinfo
->seq
);
677 if (show_stats
> 0 || xsinfo
->flags
) {
678 __u8 flags
= xsinfo
->flags
;
680 fprintf(fp
, "flag ");
681 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_NOECN
, "noecn");
682 XFRM_FLAG_PRINT(fp
, flags
, XFRM_STATE_DECAP_DSCP
, "decap-dscp");
684 fprintf(fp
, "%x", flags
);
686 fprintf(fp
, " (0x%s)", strxf_mask8(flags
));
688 fprintf(fp
, "%s", _SL_
);
690 xfrm_xfrma_print(tb
, xsinfo
->family
, fp
, buf
);
692 if (!xfrm_selector_iszero(&xsinfo
->sel
)) {
693 char sbuf
[STRBUF_SIZE
];
695 memcpy(sbuf
, buf
, sizeof(sbuf
));
696 STRBUF_CAT(sbuf
, "sel ");
698 xfrm_selector_print(&xsinfo
->sel
, xsinfo
->family
, fp
, sbuf
);
701 if (show_stats
> 0) {
702 xfrm_lifetime_print(&xsinfo
->lft
, &xsinfo
->curlft
, fp
, buf
);
703 xfrm_stats_print(&xsinfo
->stats
, fp
, buf
);
707 void xfrm_policy_info_print(struct xfrm_userpolicy_info
*xpinfo
,
708 struct rtattr
*tb
[], FILE *fp
, const char *prefix
,
711 char buf
[STRBUF_SIZE
];
713 memset(buf
, '\0', sizeof(buf
));
715 xfrm_selector_print(&xpinfo
->sel
, preferred_family
, fp
, title
);
718 STRBUF_CAT(buf
, prefix
);
719 STRBUF_CAT(buf
, "\t");
723 switch (xpinfo
->dir
) {
727 case XFRM_POLICY_OUT
:
730 case XFRM_POLICY_FWD
:
734 fprintf(fp
, "%u", xpinfo
->dir
);
739 switch (xpinfo
->action
) {
740 case XFRM_POLICY_ALLOW
:
742 fprintf(fp
, "action allow ");
744 case XFRM_POLICY_BLOCK
:
745 fprintf(fp
, "action block ");
748 fprintf(fp
, "action %u ", xpinfo
->action
);
753 fprintf(fp
, "index %u ", xpinfo
->index
);
754 fprintf(fp
, "priority %u ", xpinfo
->priority
);
755 if (show_stats
> 0) {
756 fprintf(fp
, "share %s ", strxf_share(xpinfo
->share
));
757 fprintf(fp
, "flag 0x%s", strxf_mask8(xpinfo
->flags
));
759 fprintf(fp
, "%s", _SL_
);
762 xfrm_lifetime_print(&xpinfo
->lft
, &xpinfo
->curlft
, fp
, buf
);
764 xfrm_xfrma_print(tb
, xpinfo
->sel
.family
, fp
, buf
);
767 int xfrm_id_parse(xfrm_address_t
*saddr
, struct xfrm_id
*id
, __u16
*family
,
768 int loose
, int *argcp
, char ***argvp
)
771 char **argv
= *argvp
;
775 memset(&dst
, 0, sizeof(dst
));
776 memset(&src
, 0, sizeof(src
));
779 if (strcmp(*argv
, "src") == 0) {
782 get_prefix(&src
, *argv
, preferred_family
);
783 if (src
.family
== AF_UNSPEC
)
784 invarg("\"src\" address family is AF_UNSPEC", *argv
);
786 *family
= src
.family
;
788 memcpy(saddr
, &src
.data
, sizeof(*saddr
));
790 filter
.id_src_mask
= src
.bitlen
;
792 } else if (strcmp(*argv
, "dst") == 0) {
795 get_prefix(&dst
, *argv
, preferred_family
);
796 if (dst
.family
== AF_UNSPEC
)
797 invarg("\"dst\" address family is AF_UNSPEC", *argv
);
799 *family
= dst
.family
;
801 memcpy(&id
->daddr
, &dst
.data
, sizeof(id
->daddr
));
803 filter
.id_dst_mask
= dst
.bitlen
;
805 } else if (strcmp(*argv
, "proto") == 0) {
810 ret
= xfrm_xfrmproto_getbyname(*argv
);
812 invarg("\"XFRM_PROTO\" is invalid", *argv
);
814 id
->proto
= (__u8
)ret
;
816 filter
.id_proto_mask
= XFRM_FILTER_MASK_FULL
;
818 } else if (strcmp(*argv
, "spi") == 0) {
822 if (get_u32(&spi
, *argv
, 0))
823 invarg("\"SPI\" is invalid", *argv
);
828 filter
.id_spi_mask
= XFRM_FILTER_MASK_FULL
;
831 PREV_ARG(); /* back track */
840 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
841 invarg("the same address family is required between \"src\" and \"dst\"", *argv
);
843 if (loose
== 0 && id
->proto
== 0)
844 missarg("XFRM_PROTO");
854 int xfrm_mode_parse(__u8
*mode
, int *argcp
, char ***argvp
)
857 char **argv
= *argvp
;
859 if (matches(*argv
, "transport") == 0)
861 else if (matches(*argv
, "tunnel") == 0)
863 else if (matches(*argv
, "beet") == 0)
866 invarg("\"MODE\" is invalid", *argv
);
874 int xfrm_encap_type_parse(__u16
*type
, int *argcp
, char ***argvp
)
877 char **argv
= *argvp
;
879 if (strcmp(*argv
, "espinudp-nonike") == 0)
881 else if (strcmp(*argv
, "espinudp") == 0)
884 invarg("\"ENCAP-TYPE\" is invalid", *argv
);
892 /* NOTE: reqid is used by host-byte order */
893 int xfrm_reqid_parse(__u32
*reqid
, int *argcp
, char ***argvp
)
896 char **argv
= *argvp
;
898 if (get_u32(reqid
, *argv
, 0))
899 invarg("\"REQID\" is invalid", *argv
);
907 static int xfrm_selector_upspec_parse(struct xfrm_selector
*sel
,
908 int *argcp
, char ***argvp
)
911 char **argv
= *argvp
;
918 if (strcmp(*argv
, "proto") == 0) {
923 if (strcmp(*argv
, "any") == 0)
927 pp
= getprotobyname(*argv
);
929 upspec
= pp
->p_proto
;
931 if (get_u8(&upspec
, *argv
, 0))
932 invarg("\"PROTO\" is invalid", *argv
);
937 filter
.upspec_proto_mask
= XFRM_FILTER_MASK_FULL
;
939 } else if (strcmp(*argv
, "sport") == 0) {
944 if (get_u16(&sel
->sport
, *argv
, 0))
945 invarg("\"PORT\" is invalid", *argv
);
946 sel
->sport
= htons(sel
->sport
);
948 sel
->sport_mask
= ~((__u16
)0);
950 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
952 } else if (strcmp(*argv
, "dport") == 0) {
957 if (get_u16(&sel
->dport
, *argv
, 0))
958 invarg("\"PORT\" is invalid", *argv
);
959 sel
->dport
= htons(sel
->dport
);
961 sel
->dport_mask
= ~((__u16
)0);
963 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
965 } else if (strcmp(*argv
, "type") == 0) {
970 if (get_u16(&sel
->sport
, *argv
, 0) ||
971 (sel
->sport
& ~((__u16
)0xff)))
972 invarg("\"type\" value is invalid", *argv
);
973 sel
->sport
= htons(sel
->sport
);
974 sel
->sport_mask
= ~((__u16
)0);
976 filter
.upspec_sport_mask
= XFRM_FILTER_MASK_FULL
;
979 } else if (strcmp(*argv
, "code") == 0) {
984 if (get_u16(&sel
->dport
, *argv
, 0) ||
985 (sel
->dport
& ~((__u16
)0xff)))
986 invarg("\"code\" value is invalid", *argv
);
987 sel
->dport
= htons(sel
->dport
);
988 sel
->dport_mask
= ~((__u16
)0);
990 filter
.upspec_dport_mask
= XFRM_FILTER_MASK_FULL
;
993 PREV_ARG(); /* back track */
1003 if (sportp
|| dportp
) {
1004 switch (sel
->proto
) {
1011 fprintf(stderr
, "\"sport\" and \"dport\" are invalid with proto=%s\n", strxf_proto(sel
->proto
));
1015 if (typep
|| codep
) {
1016 switch (sel
->proto
) {
1018 case IPPROTO_ICMPV6
:
1021 fprintf(stderr
, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel
->proto
));
1032 int xfrm_selector_parse(struct xfrm_selector
*sel
, int *argcp
, char ***argvp
)
1035 char **argv
= *argvp
;
1038 char *upspecp
= NULL
;
1040 memset(&dst
, 0, sizeof(dst
));
1041 memset(&src
, 0, sizeof(src
));
1044 if (strcmp(*argv
, "src") == 0) {
1047 get_prefix(&src
, *argv
, preferred_family
);
1048 if (src
.family
== AF_UNSPEC
)
1049 invarg("\"src\" address family is AF_UNSPEC", *argv
);
1050 sel
->family
= src
.family
;
1052 memcpy(&sel
->saddr
, &src
.data
, sizeof(sel
->saddr
));
1053 sel
->prefixlen_s
= src
.bitlen
;
1055 filter
.sel_src_mask
= src
.bitlen
;
1057 } else if (strcmp(*argv
, "dst") == 0) {
1060 get_prefix(&dst
, *argv
, preferred_family
);
1061 if (dst
.family
== AF_UNSPEC
)
1062 invarg("\"dst\" address family is AF_UNSPEC", *argv
);
1063 sel
->family
= dst
.family
;
1065 memcpy(&sel
->daddr
, &dst
.data
, sizeof(sel
->daddr
));
1066 sel
->prefixlen_d
= dst
.bitlen
;
1068 filter
.sel_dst_mask
= dst
.bitlen
;
1070 } else if (strcmp(*argv
, "dev") == 0) {
1075 if (strcmp(*argv
, "none") == 0)
1078 ifindex
= if_nametoindex(*argv
);
1080 invarg("\"DEV\" is invalid", *argv
);
1082 sel
->ifindex
= ifindex
;
1084 filter
.sel_dev_mask
= XFRM_FILTER_MASK_FULL
;
1088 PREV_ARG(); /* back track */
1092 xfrm_selector_upspec_parse(sel
, &argc
, &argv
);
1102 if (src
.family
&& dst
.family
&& (src
.family
!= dst
.family
))
1103 invarg("the same address family is required between \"src\" and \"dst\"", *argv
);
1106 missarg("SELECTOR");
1114 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg
*lft
,
1115 int *argcp
, char ***argvp
)
1118 char **argv
= *argvp
;
1121 if (strcmp(*argv
, "time-soft") == 0) {
1123 ret
= get_u64(&lft
->soft_add_expires_seconds
, *argv
, 0);
1125 invarg("\"time-soft\" value is invalid", *argv
);
1126 } else if (strcmp(*argv
, "time-hard") == 0) {
1128 ret
= get_u64(&lft
->hard_add_expires_seconds
, *argv
, 0);
1130 invarg("\"time-hard\" value is invalid", *argv
);
1131 } else if (strcmp(*argv
, "time-use-soft") == 0) {
1133 ret
= get_u64(&lft
->soft_use_expires_seconds
, *argv
, 0);
1135 invarg("\"time-use-soft\" value is invalid", *argv
);
1136 } else if (strcmp(*argv
, "time-use-hard") == 0) {
1138 ret
= get_u64(&lft
->hard_use_expires_seconds
, *argv
, 0);
1140 invarg("\"time-use-hard\" value is invalid", *argv
);
1141 } else if (strcmp(*argv
, "byte-soft") == 0) {
1143 ret
= get_u64(&lft
->soft_byte_limit
, *argv
, 0);
1145 invarg("\"byte-soft\" value is invalid", *argv
);
1146 } else if (strcmp(*argv
, "byte-hard") == 0) {
1148 ret
= get_u64(&lft
->hard_byte_limit
, *argv
, 0);
1150 invarg("\"byte-hard\" value is invalid", *argv
);
1151 } else if (strcmp(*argv
, "packet-soft") == 0) {
1153 ret
= get_u64(&lft
->soft_packet_limit
, *argv
, 0);
1155 invarg("\"packet-soft\" value is invalid", *argv
);
1156 } else if (strcmp(*argv
, "packet-hard") == 0) {
1158 ret
= get_u64(&lft
->hard_packet_limit
, *argv
, 0);
1160 invarg("\"packet-hard\" value is invalid", *argv
);
1162 invarg("\"LIMIT\" is invalid", *argv
);
1170 int do_xfrm(int argc
, char **argv
)
1172 memset(&filter
, 0, sizeof(filter
));
1177 if (matches(*argv
, "state") == 0 ||
1178 matches(*argv
, "sa") == 0)
1179 return do_xfrm_state(argc
-1, argv
+1);
1180 else if (matches(*argv
, "policy") == 0)
1181 return do_xfrm_policy(argc
-1, argv
+1);
1182 else if (matches(*argv
, "monitor") == 0)
1183 return do_xfrm_monitor(argc
-1, argv
+1);
1184 else if (matches(*argv
, "help") == 0) {
1186 fprintf(stderr
, "xfrm Object \"%s\" is unknown.\n", *argv
);