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");
60 static const struct typeent algo_types[]= {
61 { "enc", XFRMA_ALG_CRYPT }, { "auth", XFRMA_ALG_AUTH },
62 { "comp", XFRMA_ALG_COMP }, { NULL, -1 }
65 int xfrm_algotype_getbyname(char *name)
70 const struct typeent *t = &algo_types[i];
71 if (!t->t_name || t->t_type == -1)
74 if (strcmp(t->t_name, name) == 0)
81 const char *strxf_algotype(int type)
86 const struct typeent *t = &algo_types[i];
87 if (!t->t_name || t->t_type == -1)
90 if (t->t_type == type)
97 const char *strxf_flags(__u8 flags)
100 const int sn = sizeof(flags) * 8 - 1;
104 for (b = (1 << sn); b > 0; b >>= 1)
105 str[i++] = ((b & flags) ? '1' : '0');
111 const char *strxf_share(__u8 share)
119 case XFRM_SHARE_SESSION:
120 strcpy(str, "session");
122 case XFRM_SHARE_USER:
125 case XFRM_SHARE_UNIQUE:
126 strcpy(str, "unique");
129 sprintf(str, "%d", share);
136 const char *strxf_proto(__u8 proto)
142 pp = getprotobynumber(proto);
146 sprintf(buf, "%d", proto);
153 void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id,
154 __u8 mode, __u32 reqid, __u16 family, FILE *fp,
163 memset(abuf, '\0', sizeof(abuf));
164 fprintf(fp, "src %s ", rt_addr_n2a(family, sizeof(*saddr),
165 saddr, abuf, sizeof(abuf)));
166 memset(abuf, '\0', sizeof(abuf));
167 fprintf(fp, "dst %s", rt_addr_n2a(family, sizeof(id->daddr),
168 &id->daddr, abuf, sizeof(abuf)));
169 fprintf(fp, "%s", _SL_);
175 fprintf(fp, "proto %s ", strxf_proto(id->proto));
177 spi = ntohl(id->spi);
178 fprintf(fp, "spi 0x%08x", spi);
180 fprintf(fp, "(%u)", spi);
183 fprintf(fp, "reqid %u", reqid);
185 fprintf(fp, "(0x%08x)", reqid);
188 fprintf(fp, "mode ");
191 fprintf(fp, "transport");
194 fprintf(fp, "tunnel");
197 fprintf(fp, "%u", mode);
200 fprintf(fp, "%s", _SL_);
203 static const char *strxf_limit(__u64 limit)
206 if (limit == XFRM_INF)
207 strcpy(str, "(INF)");
209 sprintf(str, "%llu", limit);
214 void xfrm_stats_print(struct xfrm_stats *s, FILE *fp, const char *prefix)
218 fprintf(fp, "stats:");
219 fprintf(fp, "%s", _SL_);
224 fprintf(fp, "replay-window %d ", s->replay_window);
225 fprintf(fp, "replay %d ", s->replay);
226 fprintf(fp, "failed %d", s->integrity_failed);
227 fprintf(fp, "%s", _SL_);
230 static const char *strxf_time(__u64 time)
240 /* XXX: treat time in the same manner of kernel's
241 * net/xfrm/xfrm_{user,state}.c
246 strftime(str, sizeof(str), "%F %T", tp);
252 void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg,
253 struct xfrm_lifetime_cur *cur,
254 FILE *fp, const char *prefix)
259 fprintf(fp, "lifetime config:");
260 fprintf(fp, "%s", _SL_);
265 fprintf(fp, "limit: ");
266 fprintf(fp, "soft ");
267 fprintf(fp, strxf_limit(cfg->soft_byte_limit));
268 fprintf(fp, "(bytes), hard ");
269 fprintf(fp, strxf_limit(cfg->hard_byte_limit));
270 fprintf(fp, "(bytes)");
271 fprintf(fp, "%s", _SL_);
276 fprintf(fp, "limit: ");
277 fprintf(fp, "soft ");
278 fprintf(fp, strxf_limit(cfg->soft_packet_limit));
279 fprintf(fp, "(packets), hard ");
280 fprintf(fp, strxf_limit(cfg->hard_packet_limit));
281 fprintf(fp, "(packets)");
282 fprintf(fp, "%s", _SL_);
287 fprintf(fp, "expire add: ");
288 fprintf(fp, "soft ");
289 fprintf(fp, "%llu", cfg->soft_add_expires_seconds);
290 fprintf(fp, "(sec), hard ");
291 fprintf(fp, "%llu", cfg->hard_add_expires_seconds);
292 fprintf(fp, "(sec)");
293 fprintf(fp, "%s", _SL_);
298 fprintf(fp, "expire use: ");
299 fprintf(fp, "soft ");
300 fprintf(fp, "%llu", cfg->soft_use_expires_seconds);
301 fprintf(fp, "(sec), hard ");
302 fprintf(fp, "%llu", cfg->hard_use_expires_seconds);
303 fprintf(fp, "(sec)");
304 fprintf(fp, "%s", _SL_);
309 fprintf(fp, "lifetime current:");
310 fprintf(fp, "%s", _SL_);
315 fprintf(fp, "%llu(bytes), ", cur->bytes);
316 fprintf(fp, "%llu(packets)", cur->packets);
317 fprintf(fp, "%s", _SL_);
322 fprintf(fp, "add %s ", strxf_time(cur->add_time));
323 fprintf(fp, "use %s", strxf_time(cur->use_time));
324 fprintf(fp, "%s", _SL_);
328 void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
329 FILE *fp, const char *prefix)
338 f = preferred_family;
343 memset(abuf, '\0', sizeof(abuf));
344 fprintf(fp, "src %s/%d ", rt_addr_n2a(f, sizeof(sel->saddr),
345 &sel->saddr, abuf, sizeof(abuf)),
348 memset(abuf, '\0', sizeof(abuf));
349 fprintf(fp, "dst %s/%d ", rt_addr_n2a(f, sizeof(sel->daddr),
350 &sel->daddr, abuf, sizeof(abuf)),
354 fprintf(fp, "proto %s ", strxf_proto(sel->proto));
356 fprintf(fp, "sport %u ", ntohs(sel->sport));
358 fprintf(fp, "dport %u ", ntohs(sel->dport));
360 if (sel->ifindex > 0) {
361 char buf[IF_NAMESIZE];
363 memset(buf, '\0', sizeof(buf));
364 if_indextoname(sel->ifindex, buf);
365 fprintf(fp, "dev %s ", buf);
369 fprintf(fp, "uid %u", sel->user);
371 fprintf(fp, "%s", _SL_);
374 static void xfrm_algo_print(struct xfrm_algo *algo, int type, FILE *fp,
383 fprintf(fp, "%s ", strxf_algotype(type));
384 fprintf(fp, "%s ", algo->alg_name);
387 len = algo->alg_key_len / 8;
388 for (i = 0; i < len; i ++)
389 fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]);
392 fprintf(fp, " (%d bits)", algo->alg_key_len);
394 fprintf(fp, "%s", _SL_);
397 static const char *strxf_mask(__u32 mask)
399 static char str[128];
400 const int sn = sizeof(mask) * 8 - 1;
406 for (b = (1 << sn); b > 0; b >>= 1) {
407 if ((b & mask) == 0) {
421 sprintf(str, "%u", i);
423 sprintf(str, "broken(%u)", mask);
428 static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int ntmpls,
429 __u16 family, FILE *fp, const char *prefix)
433 for (i = 0; i < ntmpls; i++) {
434 struct xfrm_user_tmpl *tmpl = &tmpls[i];
440 xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode,
441 tmpl->reqid, family, fp, prefix);
446 switch (tmpl->optional) {
449 fprintf(fp, "level required ");
452 fprintf(fp, "level use ");
455 fprintf(fp, "level %d ", tmpl->optional);
459 if (show_stats > 0) {
460 fprintf(fp, "share %s ", strxf_share(tmpl->share));
461 fprintf(fp, "algo-mask:");
462 fprintf(fp, "%s=%s, ",
463 strxf_algotype(XFRMA_ALG_CRYPT),
464 strxf_mask(tmpl->ealgos));
465 fprintf(fp, "%s=%s, ",
466 strxf_algotype(XFRMA_ALG_AUTH),
467 strxf_mask(tmpl->aalgos));
469 strxf_algotype(XFRMA_ALG_COMP),
470 strxf_mask(tmpl->calgos));
472 fprintf(fp, "%s", _SL_);
476 void xfrm_xfrma_print(struct rtattr *tb[], int ntb, __u16 family,
477 FILE *fp, const char *prefix)
481 for (i = 0; i < ntb; i++) {
482 __u16 type = tb[i]->rta_type;
483 void *data = RTA_DATA(tb[i]);
486 case XFRMA_ALG_CRYPT:
489 xfrm_algo_print((struct xfrm_algo *)data, type, fp,
496 fprintf(fp, "encap (not implemented yet!)");
497 fprintf(fp, "%s", _SL_);
501 int len = tb[i]->rta_len;
502 int ntmpls = len / sizeof(struct xfrm_user_tmpl);
504 xfrm_tmpl_print((struct xfrm_user_tmpl *)data,
505 ntmpls, family, fp, prefix);
511 fprintf(fp, "%u (unknown rta_type)", type);
512 fprintf(fp, "%s", _SL_);
518 int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
519 int loose, int *argcp, char ***argvp)
522 char **argv = *argvp;
527 memset(&dst, 0, sizeof(dst));
528 memset(&src, 0, sizeof(src));
531 if (strcmp(*argv, "src") == 0) {
534 get_prefix(&src, *argv, preferred_family);
535 if (src.family == AF_UNSPEC)
536 invarg("\"SADDR\" address family is AF_UNSPEC", *argv);
538 *family = src.family;
540 memcpy(saddr, &src.data, sizeof(*saddr));
542 filter.id_src_mask = src.bitlen;
544 } else if (strcmp(*argv, "dst") == 0) {
547 get_prefix(&dst, *argv, preferred_family);
548 if (dst.family == AF_UNSPEC)
549 invarg("\"DADDR\" address family is AF_UNSPEC", *argv);
551 *family = dst.family;
553 memcpy(&id->daddr, &dst.data, sizeof(id->daddr));
555 filter.id_dst_mask = dst.bitlen;
557 } else if (strcmp(*argv, "proto") == 0) {
562 pp = getprotobyname(*argv);
566 if (get_u8(&proto, *argv, 0))
567 invarg("\"XFRM_PROTO\" is invalid", *argv);
577 invarg("\"XFRM_PROTO\" is unsuppored proto", *argv);
580 filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
582 } else if (strcmp(*argv, "spi") == 0) {
586 if (get_u32(&spi, *argv, 0))
587 invarg("\"SPI\" is invalid", *argv);
592 filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
595 PREV_ARG(); /* back track */
604 if (src.family && dst.family && (src.family != dst.family))
605 invarg("the same address family is required between \"SADDR\" and \"DADDR\"", *argv);
607 if (loose == 0 && proto == 0)
618 int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp)
621 char **argv = *argvp;
623 if (matches(*argv, "transport") == 0)
625 else if (matches(*argv, "tunnel") == 0)
628 invarg("\"MODE\" is invalid", *argv);
636 /* NOTE: reqid is used by host-byte order */
637 int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp)
640 char **argv = *argvp;
642 if (get_u32(reqid, *argv, 0))
643 invarg("\"REQID\" is invalid", *argv);
651 static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
652 int *argcp, char ***argvp)
655 char **argv = *argvp;
658 if (strcmp(*argv, "proto") == 0) {
663 if (strcmp(*argv, "any") == 0)
667 pp = getprotobyname(*argv);
669 upspec = pp->p_proto;
671 if (get_u8(&upspec, *argv, 0))
672 invarg("\"PROTO\" is invalid", *argv);
677 filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL;
679 } else if (strcmp(*argv, "sport") == 0) {
682 if (get_u16(&sel->sport, *argv, 0))
683 invarg("\"PORT\" is invalid", *argv);
684 sel->sport = htons(sel->sport);
686 sel->sport_mask = ~((__u16)0);
688 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
690 } else if (strcmp(*argv, "dport") == 0) {
693 if (get_u16(&sel->dport, *argv, 0))
694 invarg("\"PORT\" is invalid", *argv);
695 sel->dport = htons(sel->dport);
697 sel->dport_mask = ~((__u16)0);
699 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
702 PREV_ARG(); /* back track */
719 int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
722 char **argv = *argvp;
725 char *upspecp = NULL;
727 memset(&dst, 0, sizeof(dst));
728 memset(&src, 0, sizeof(src));
731 if (strcmp(*argv, "src") == 0) {
734 get_prefix(&src, *argv, preferred_family);
735 if (src.family == AF_UNSPEC)
736 invarg("\"SADDR\" address family is AF_UNSPEC", *argv);
737 sel->family = src.family;
739 memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
740 sel->prefixlen_s = src.bitlen;
742 filter.sel_src_mask = src.bitlen;
744 } else if (strcmp(*argv, "dst") == 0) {
747 get_prefix(&dst, *argv, preferred_family);
748 if (dst.family == AF_UNSPEC)
749 invarg("\"DADDR\" address family is AF_UNSPEC", *argv);
750 sel->family = dst.family;
752 memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
753 sel->prefixlen_d = dst.bitlen;
755 filter.sel_dst_mask = dst.bitlen;
757 } else if (strcmp(*argv, "dev") == 0) {
762 if (strcmp(*argv, "none") == 0)
765 ifindex = if_nametoindex(*argv);
767 invarg("\"DEV\" is invalid", *argv);
769 sel->ifindex = ifindex;
771 filter.sel_dev_mask = XFRM_FILTER_MASK_FULL;
775 PREV_ARG(); /* back track */
779 xfrm_selector_upspec_parse(sel, &argc, &argv);
789 if (src.family && dst.family && (src.family != dst.family))
790 invarg("the same address family is required between \"SADDR\" and \"DADDR\"", *argv);
801 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft,
802 int *argcp, char ***argvp)
805 char **argv = *argvp;
808 if (strcmp(*argv, "time-soft") == 0) {
810 ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
812 invarg("\"time-soft\" value is invalid", *argv);
813 } else if (strcmp(*argv, "time-hard") == 0) {
815 ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
817 invarg("\"time-hard\" value is invalid", *argv);
818 } else if (strcmp(*argv, "time-use-soft") == 0) {
820 ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
822 invarg("\"time-use-soft\" value is invalid", *argv);
823 } else if (strcmp(*argv, "time-use-hard") == 0) {
825 ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
827 invarg("\"time-use-hard\" value is invalid", *argv);
828 } else if (strcmp(*argv, "byte-soft") == 0) {
830 ret = get_u64(&lft->soft_byte_limit, *argv, 0);
832 invarg("\"byte-soft\" value is invalid", *argv);
833 } else if (strcmp(*argv, "byte-hard") == 0) {
835 ret = get_u64(&lft->hard_byte_limit, *argv, 0);
837 invarg("\"byte-hard\" value is invalid", *argv);
838 } else if (strcmp(*argv, "packet-soft") == 0) {
840 ret = get_u64(&lft->soft_packet_limit, *argv, 0);
842 invarg("\"packet-soft\" value is invalid", *argv);
843 } else if (strcmp(*argv, "packet-hard") == 0) {
845 ret = get_u64(&lft->hard_packet_limit, *argv, 0);
847 invarg("\"packet-hard\" value is invalid", *argv);
849 invarg("\"LIMIT\" is invalid", *argv);
857 int do_xfrm(int argc, char **argv)
859 memset(&filter, 0, sizeof(filter));
864 if (matches(*argv, "state") == 0 ||
865 matches(*argv, "sa") == 0) {
866 return do_xfrm_state(argc-1, argv+1);
867 } else if (matches(*argv, "policy") == 0)
868 return do_xfrm_policy(argc-1, argv+1);
869 else if (matches(*argv, "help") == 0) {
871 fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv);