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 struct xfrm_filter filter;
45 static void usage(void) __attribute__((noreturn));
47 static void usage(void)
50 "Usage: ip xfrm XFRM_OBJECT { COMMAND | help }\n"
51 "where XFRM_OBJECT := { state | policy }\n");
55 /* This is based on utils.c(inet_addr_match) */
56 int xfrm_addr_match(xfrm_address_t *x1, xfrm_address_t *x2, int bits)
58 __u32 *a1 = (__u32 *)x1;
59 __u32 *a2 = (__u32 *)x2;
60 int words = bits >> 0x05;
65 if (memcmp(a1, a2, words << 2))
75 mask = htonl((0xffffffff) << (0x20 - bits));
89 static const struct typeent xfrmproto_types[]= {
90 { "esp", IPPROTO_ESP }, { "ah", IPPROTO_AH }, { "comp", IPPROTO_COMP },
94 int xfrm_xfrmproto_getbyname(char *name)
99 const struct typeent *t = &xfrmproto_types[i];
100 if (!t->t_name || t->t_type == -1)
103 if (strcmp(t->t_name, name) == 0)
110 const char *strxf_xfrmproto(__u8 proto)
115 const struct typeent *t = &xfrmproto_types[i];
116 if (!t->t_name || t->t_type == -1)
119 if (t->t_type == proto)
126 static const struct typeent algo_types[]= {
127 { "enc", XFRMA_ALG_CRYPT }, { "auth", XFRMA_ALG_AUTH },
128 { "comp", XFRMA_ALG_COMP }, { NULL, -1 }
131 int xfrm_algotype_getbyname(char *name)
136 const struct typeent *t = &algo_types[i];
137 if (!t->t_name || t->t_type == -1)
140 if (strcmp(t->t_name, name) == 0)
147 const char *strxf_algotype(int type)
152 const struct typeent *t = &algo_types[i];
153 if (!t->t_name || t->t_type == -1)
156 if (t->t_type == type)
163 const char *strxf_mask8(__u8 mask)
166 const int sn = sizeof(mask) * 8 - 1;
170 for (b = (1 << sn); b > 0; b >>= 1)
171 str[i++] = ((b & mask) ? '1' : '0');
177 const char *strxf_mask32(__u32 mask)
181 sprintf(str, "%.8x", mask);
186 const char *strxf_share(__u8 share)
194 case XFRM_SHARE_SESSION:
195 strcpy(str, "session");
197 case XFRM_SHARE_USER:
200 case XFRM_SHARE_UNIQUE:
201 strcpy(str, "unique");
204 sprintf(str, "%u", share);
211 const char *strxf_proto(__u8 proto)
217 pp = getprotobynumber(proto);
221 sprintf(buf, "%u", proto);
228 void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id,
229 __u8 mode, __u32 reqid, __u16 family, int force_spi,
230 FILE *fp, const char *prefix)
237 memset(abuf, '\0', sizeof(abuf));
238 fprintf(fp, "src %s ", rt_addr_n2a(family, sizeof(*saddr),
239 saddr, abuf, sizeof(abuf)));
240 memset(abuf, '\0', sizeof(abuf));
241 fprintf(fp, "dst %s", rt_addr_n2a(family, sizeof(id->daddr),
242 &id->daddr, abuf, sizeof(abuf)));
243 fprintf(fp, "%s", _SL_);
249 fprintf(fp, "proto %s ", strxf_xfrmproto(id->proto));
252 if (show_stats > 0 || force_spi || id->spi) {
253 __u32 spi = ntohl(id->spi);
254 fprintf(fp, "spi 0x%08x", spi);
256 fprintf(fp, "(%u)", spi);
260 fprintf(fp, "reqid %u", reqid);
262 fprintf(fp, "(0x%08x)", reqid);
265 fprintf(fp, "mode ");
268 fprintf(fp, "transport");
271 fprintf(fp, "tunnel");
274 fprintf(fp, "%u", mode);
277 fprintf(fp, "%s", _SL_);
280 static const char *strxf_limit(__u64 limit)
283 if (limit == XFRM_INF)
284 strcpy(str, "(INF)");
286 sprintf(str, "%llu", (unsigned long long) limit);
291 void xfrm_stats_print(struct xfrm_stats *s, FILE *fp, const char *prefix)
295 fprintf(fp, "stats:");
296 fprintf(fp, "%s", _SL_);
301 fprintf(fp, "replay-window %u ", s->replay_window);
302 fprintf(fp, "replay %u ", s->replay);
303 fprintf(fp, "failed %u", s->integrity_failed);
304 fprintf(fp, "%s", _SL_);
307 static const char *strxf_time(__u64 time)
317 /* XXX: treat time in the same manner of kernel's
318 * net/xfrm/xfrm_{user,state}.c
323 strftime(str, sizeof(str), "%Y-%m-%d %T", tp);
329 void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg,
330 struct xfrm_lifetime_cur *cur,
331 FILE *fp, const char *prefix)
336 fprintf(fp, "lifetime config:");
337 fprintf(fp, "%s", _SL_);
342 fprintf(fp, "limit: ");
343 fprintf(fp, "soft ");
344 fprintf(fp, strxf_limit(cfg->soft_byte_limit));
345 fprintf(fp, "(bytes), hard ");
346 fprintf(fp, strxf_limit(cfg->hard_byte_limit));
347 fprintf(fp, "(bytes)");
348 fprintf(fp, "%s", _SL_);
353 fprintf(fp, "limit: ");
354 fprintf(fp, "soft ");
355 fprintf(fp, strxf_limit(cfg->soft_packet_limit));
356 fprintf(fp, "(packets), hard ");
357 fprintf(fp, strxf_limit(cfg->hard_packet_limit));
358 fprintf(fp, "(packets)");
359 fprintf(fp, "%s", _SL_);
364 fprintf(fp, "expire add: ");
365 fprintf(fp, "soft ");
366 fprintf(fp, "%llu", (unsigned long long) cfg->soft_add_expires_seconds);
367 fprintf(fp, "(sec), hard ");
368 fprintf(fp, "%llu", (unsigned long long) cfg->hard_add_expires_seconds);
369 fprintf(fp, "(sec)");
370 fprintf(fp, "%s", _SL_);
375 fprintf(fp, "expire use: ");
376 fprintf(fp, "soft ");
377 fprintf(fp, "%llu", (unsigned long long) cfg->soft_use_expires_seconds);
378 fprintf(fp, "(sec), hard ");
379 fprintf(fp, "%llu", (unsigned long long) cfg->hard_use_expires_seconds);
380 fprintf(fp, "(sec)");
381 fprintf(fp, "%s", _SL_);
386 fprintf(fp, "lifetime current:");
387 fprintf(fp, "%s", _SL_);
392 fprintf(fp, "%llu(bytes), ", (unsigned long long) cur->bytes);
393 fprintf(fp, "%llu(packets)", (unsigned long long) cur->packets);
394 fprintf(fp, "%s", _SL_);
399 fprintf(fp, "add %s ", strxf_time(cur->add_time));
400 fprintf(fp, "use %s", strxf_time(cur->use_time));
401 fprintf(fp, "%s", _SL_);
405 void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
406 FILE *fp, const char *prefix)
415 f = preferred_family;
420 memset(abuf, '\0', sizeof(abuf));
421 fprintf(fp, "src %s/%u ", rt_addr_n2a(f, sizeof(sel->saddr),
422 &sel->saddr, abuf, sizeof(abuf)),
425 memset(abuf, '\0', sizeof(abuf));
426 fprintf(fp, "dst %s/%u ", rt_addr_n2a(f, sizeof(sel->daddr),
427 &sel->daddr, abuf, sizeof(abuf)),
431 fprintf(fp, "proto %s ", strxf_proto(sel->proto));
432 switch (sel->proto) {
438 fprintf(fp, "sport %u ", ntohs(sel->sport));
440 fprintf(fp, "dport %u ", ntohs(sel->dport));
444 /* type/code is stored at sport/dport in selector */
446 fprintf(fp, "type %u ", ntohs(sel->sport));
448 fprintf(fp, "code %u ", ntohs(sel->dport));
452 if (sel->ifindex > 0) {
455 memset(buf, '\0', sizeof(buf));
456 if_indextoname(sel->ifindex, buf);
457 fprintf(fp, "dev %s ", buf);
461 fprintf(fp, "uid %u", sel->user);
463 fprintf(fp, "%s", _SL_);
466 static void xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
467 FILE *fp, const char *prefix)
475 fprintf(fp, "%s ", strxf_algotype(type));
477 if (len < sizeof(*algo)) {
478 fprintf(fp, "(ERROR truncated)");
481 len -= sizeof(*algo);
483 fprintf(fp, "%s ", algo->alg_name);
485 keylen = algo->alg_key_len / 8;
487 fprintf(fp, "(ERROR truncated)");
492 for (i = 0; i < keylen; i ++)
493 fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]);
496 fprintf(fp, " (%d bits)", algo->alg_key_len);
499 fprintf(fp, "%s", _SL_);
502 static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int len,
503 __u16 family, FILE *fp, const char *prefix)
505 int ntmpls = len / sizeof(struct xfrm_user_tmpl);
511 fprintf(fp, "(ERROR \"tmpl\" truncated)");
512 fprintf(fp, "%s", _SL_);
516 for (i = 0; i < ntmpls; i++) {
517 struct xfrm_user_tmpl *tmpl = &tmpls[i];
523 xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode,
524 tmpl->reqid, family, 0, fp, prefix);
526 if (show_stats > 0 || tmpl->optional) {
530 switch (tmpl->optional) {
533 fprintf(fp, "level required ");
536 fprintf(fp, "level use ");
539 fprintf(fp, "level %u ", tmpl->optional);
544 fprintf(fp, "share %s ", strxf_share(tmpl->share));
546 fprintf(fp, "%s", _SL_);
549 if (show_stats > 0) {
553 fprintf(fp, "%s-mask %s ",
554 strxf_algotype(XFRMA_ALG_CRYPT),
555 strxf_mask32(tmpl->ealgos));
556 fprintf(fp, "%s-mask %s ",
557 strxf_algotype(XFRMA_ALG_AUTH),
558 strxf_mask32(tmpl->aalgos));
559 fprintf(fp, "%s-mask %s",
560 strxf_algotype(XFRMA_ALG_COMP),
561 strxf_mask32(tmpl->calgos));
563 fprintf(fp, "%s", _SL_);
568 void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
569 FILE *fp, const char *prefix)
571 if (tb[XFRMA_ALG_AUTH]) {
572 struct rtattr *rta = tb[XFRMA_ALG_AUTH];
573 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
574 XFRMA_ALG_AUTH, RTA_PAYLOAD(rta), fp, prefix);
577 if (tb[XFRMA_ALG_CRYPT]) {
578 struct rtattr *rta = tb[XFRMA_ALG_CRYPT];
579 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
580 XFRMA_ALG_CRYPT, RTA_PAYLOAD(rta), fp, prefix);
583 if (tb[XFRMA_ALG_COMP]) {
584 struct rtattr *rta = tb[XFRMA_ALG_COMP];
585 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
586 XFRMA_ALG_COMP, RTA_PAYLOAD(rta), fp, prefix);
589 if (tb[XFRMA_ENCAP]) {
590 struct xfrm_encap_tmpl *e;
595 fprintf(fp, "encap ");
597 if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) {
598 fprintf(fp, "(ERROR truncated)");
599 fprintf(fp, "%s", _SL_);
602 e = (struct xfrm_encap_tmpl *) RTA_DATA(tb[XFRMA_ENCAP]);
604 fprintf(fp, "type ");
605 switch (e->encap_type) {
607 fprintf(fp, "espinudp-nonike ");
610 fprintf(fp, "espinudp ");
613 fprintf(fp, "%u ", e->encap_type);
616 fprintf(fp, "sport %u ", ntohs(e->encap_sport));
617 fprintf(fp, "dport %u ", ntohs(e->encap_dport));
619 memset(abuf, '\0', sizeof(abuf));
620 fprintf(fp, "addr %s",
621 rt_addr_n2a(family, sizeof(e->encap_oa),
622 &e->encap_oa, abuf, sizeof(abuf)));
623 fprintf(fp, "%s", _SL_);
626 if (tb[XFRMA_TMPL]) {
627 struct rtattr *rta = tb[XFRMA_TMPL];
628 xfrm_tmpl_print((struct xfrm_user_tmpl *) RTA_DATA(rta),
629 RTA_PAYLOAD(rta), family, fp, prefix);
633 int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
634 int loose, int *argcp, char ***argvp)
637 char **argv = *argvp;
641 memset(&dst, 0, sizeof(dst));
642 memset(&src, 0, sizeof(src));
645 if (strcmp(*argv, "src") == 0) {
648 get_prefix(&src, *argv, preferred_family);
649 if (src.family == AF_UNSPEC)
650 invarg("\"src\" address family is AF_UNSPEC", *argv);
652 *family = src.family;
654 memcpy(saddr, &src.data, sizeof(*saddr));
656 filter.id_src_mask = src.bitlen;
658 } else if (strcmp(*argv, "dst") == 0) {
661 get_prefix(&dst, *argv, preferred_family);
662 if (dst.family == AF_UNSPEC)
663 invarg("\"dst\" address family is AF_UNSPEC", *argv);
665 *family = dst.family;
667 memcpy(&id->daddr, &dst.data, sizeof(id->daddr));
669 filter.id_dst_mask = dst.bitlen;
671 } else if (strcmp(*argv, "proto") == 0) {
676 ret = xfrm_xfrmproto_getbyname(*argv);
678 invarg("\"XFRM_PROTO\" is invalid", *argv);
680 id->proto = (__u8)ret;
682 filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
684 } else if (strcmp(*argv, "spi") == 0) {
688 if (get_u32(&spi, *argv, 0))
689 invarg("\"SPI\" is invalid", *argv);
694 filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
697 PREV_ARG(); /* back track */
706 if (src.family && dst.family && (src.family != dst.family))
707 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
709 if (loose == 0 && id->proto == 0)
710 missarg("XFRM_PROTO");
720 int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp)
723 char **argv = *argvp;
725 if (matches(*argv, "transport") == 0)
727 else if (matches(*argv, "tunnel") == 0)
730 invarg("\"MODE\" is invalid", *argv);
738 int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp)
741 char **argv = *argvp;
743 if (strcmp(*argv, "espinudp-nonike") == 0)
745 else if (strcmp(*argv, "espinudp") == 0)
748 invarg("\"ENCAP-TYPE\" is invalid", *argv);
756 /* NOTE: reqid is used by host-byte order */
757 int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp)
760 char **argv = *argvp;
762 if (get_u32(reqid, *argv, 0))
763 invarg("\"REQID\" is invalid", *argv);
771 static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
772 int *argcp, char ***argvp)
775 char **argv = *argvp;
782 if (strcmp(*argv, "proto") == 0) {
787 if (strcmp(*argv, "any") == 0)
791 pp = getprotobyname(*argv);
793 upspec = pp->p_proto;
795 if (get_u8(&upspec, *argv, 0))
796 invarg("\"PROTO\" is invalid", *argv);
801 filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL;
803 } else if (strcmp(*argv, "sport") == 0) {
808 if (get_u16(&sel->sport, *argv, 0))
809 invarg("\"PORT\" is invalid", *argv);
810 sel->sport = htons(sel->sport);
812 sel->sport_mask = ~((__u16)0);
814 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
816 } else if (strcmp(*argv, "dport") == 0) {
821 if (get_u16(&sel->dport, *argv, 0))
822 invarg("\"PORT\" is invalid", *argv);
823 sel->dport = htons(sel->dport);
825 sel->dport_mask = ~((__u16)0);
827 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
829 } else if (strcmp(*argv, "type") == 0) {
834 if (get_u16(&sel->sport, *argv, 0) ||
835 (sel->sport & ~((__u16)0xff)))
836 invarg("\"type\" value is invalid", *argv);
837 sel->sport = htons(sel->sport);
838 sel->sport_mask = ~((__u16)0);
840 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
843 } else if (strcmp(*argv, "code") == 0) {
848 if (get_u16(&sel->dport, *argv, 0) ||
849 (sel->dport & ~((__u16)0xff)))
850 invarg("\"code\" value is invalid", *argv);
851 sel->dport = htons(sel->dport);
852 sel->dport_mask = ~((__u16)0);
854 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
857 PREV_ARG(); /* back track */
867 if (sportp || dportp) {
868 switch (sel->proto) {
874 fprintf(stderr, "\"sport\" and \"dport\" are invalid with proto=%s\n", strxf_proto(sel->proto));
878 if (typep || codep) {
879 switch (sel->proto) {
884 fprintf(stderr, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel->proto));
895 int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
898 char **argv = *argvp;
901 char *upspecp = NULL;
903 memset(&dst, 0, sizeof(dst));
904 memset(&src, 0, sizeof(src));
907 if (strcmp(*argv, "src") == 0) {
910 get_prefix(&src, *argv, preferred_family);
911 if (src.family == AF_UNSPEC)
912 invarg("\"src\" address family is AF_UNSPEC", *argv);
913 sel->family = src.family;
915 memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
916 sel->prefixlen_s = src.bitlen;
918 filter.sel_src_mask = src.bitlen;
920 } else if (strcmp(*argv, "dst") == 0) {
923 get_prefix(&dst, *argv, preferred_family);
924 if (dst.family == AF_UNSPEC)
925 invarg("\"dst\" address family is AF_UNSPEC", *argv);
926 sel->family = dst.family;
928 memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
929 sel->prefixlen_d = dst.bitlen;
931 filter.sel_dst_mask = dst.bitlen;
933 } else if (strcmp(*argv, "dev") == 0) {
938 if (strcmp(*argv, "none") == 0)
941 ifindex = if_nametoindex(*argv);
943 invarg("\"DEV\" is invalid", *argv);
945 sel->ifindex = ifindex;
947 filter.sel_dev_mask = XFRM_FILTER_MASK_FULL;
951 PREV_ARG(); /* back track */
955 xfrm_selector_upspec_parse(sel, &argc, &argv);
965 if (src.family && dst.family && (src.family != dst.family))
966 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
977 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft,
978 int *argcp, char ***argvp)
981 char **argv = *argvp;
984 if (strcmp(*argv, "time-soft") == 0) {
986 ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
988 invarg("\"time-soft\" value is invalid", *argv);
989 } else if (strcmp(*argv, "time-hard") == 0) {
991 ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
993 invarg("\"time-hard\" value is invalid", *argv);
994 } else if (strcmp(*argv, "time-use-soft") == 0) {
996 ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
998 invarg("\"time-use-soft\" value is invalid", *argv);
999 } else if (strcmp(*argv, "time-use-hard") == 0) {
1001 ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
1003 invarg("\"time-use-hard\" value is invalid", *argv);
1004 } else if (strcmp(*argv, "byte-soft") == 0) {
1006 ret = get_u64(&lft->soft_byte_limit, *argv, 0);
1008 invarg("\"byte-soft\" value is invalid", *argv);
1009 } else if (strcmp(*argv, "byte-hard") == 0) {
1011 ret = get_u64(&lft->hard_byte_limit, *argv, 0);
1013 invarg("\"byte-hard\" value is invalid", *argv);
1014 } else if (strcmp(*argv, "packet-soft") == 0) {
1016 ret = get_u64(&lft->soft_packet_limit, *argv, 0);
1018 invarg("\"packet-soft\" value is invalid", *argv);
1019 } else if (strcmp(*argv, "packet-hard") == 0) {
1021 ret = get_u64(&lft->hard_packet_limit, *argv, 0);
1023 invarg("\"packet-hard\" value is invalid", *argv);
1025 invarg("\"LIMIT\" is invalid", *argv);
1033 int do_xfrm(int argc, char **argv)
1035 memset(&filter, 0, sizeof(filter));
1040 if (matches(*argv, "state") == 0 ||
1041 matches(*argv, "sa") == 0) {
1042 return do_xfrm_state(argc-1, argv+1);
1043 } else if (matches(*argv, "policy") == 0)
1044 return do_xfrm_policy(argc-1, argv+1);
1045 else if (matches(*argv, "help") == 0) {
1047 fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv);