]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipxfrm.c
SA and SP in IPSec BEET mode.
[mirror_iproute2.git] / ip / ipxfrm.c
1 /* $USAGI: $ */
2
3 /*
4 * Copyright (C)2004 USAGI/WIDE Project
5 *
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.
10 *
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.
15 *
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
19 */
20 /*
21 * based on ip.c, iproute.c
22 */
23 /*
24 * Authors:
25 * Masahide NAKAMURA @USAGI
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <time.h>
34 #include <netdb.h>
35 #include <net/if.h>
36 #include <linux/netlink.h>
37 #include <linux/rtnetlink.h>
38 #include <linux/xfrm.h>
39
40 #include "utils.h"
41 #include "xfrm.h"
42
43 #define STRBUF_SIZE (128)
44 #define STRBUF_CAT(buf, str) \
45 do { \
46 int rest = sizeof(buf) - 1 - strlen(buf); \
47 if (rest > 0) { \
48 int len = strlen(str); \
49 if (len > rest) \
50 len = rest; \
51 strncat(buf, str, len); \
52 buf[sizeof(buf) - 1] = '\0'; \
53 } \
54 } while(0);
55
56 struct xfrm_filter filter;
57
58 static void usage(void) __attribute__((noreturn));
59
60 static void usage(void)
61 {
62 fprintf(stderr,
63 "Usage: ip xfrm XFRM_OBJECT { COMMAND | help }\n"
64 "where XFRM_OBJECT := { state | policy | monitor }\n");
65 exit(-1);
66 }
67
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)
70 {
71 __u32 *a1 = (__u32 *)x1;
72 __u32 *a2 = (__u32 *)x2;
73 int words = bits >> 0x05;
74
75 bits &= 0x1f;
76
77 if (words)
78 if (memcmp(a1, a2, words << 2))
79 return -1;
80
81 if (bits) {
82 __u32 w1, w2;
83 __u32 mask;
84
85 w1 = a1[words];
86 w2 = a2[words];
87
88 mask = htonl((0xffffffff) << (0x20 - bits));
89
90 if ((w1 ^ w2) & mask)
91 return 1;
92 }
93
94 return 0;
95 }
96
97 struct typeent {
98 const char *t_name;
99 int t_type;
100 };
101
102 static const struct typeent xfrmproto_types[]= {
103 { "esp", IPPROTO_ESP }, { "ah", IPPROTO_AH }, { "comp", IPPROTO_COMP },
104 { NULL, -1 }
105 };
106
107 int xfrm_xfrmproto_getbyname(char *name)
108 {
109 int i;
110
111 for (i = 0; ; i++) {
112 const struct typeent *t = &xfrmproto_types[i];
113 if (!t->t_name || t->t_type == -1)
114 break;
115
116 if (strcmp(t->t_name, name) == 0)
117 return t->t_type;
118 }
119
120 return -1;
121 }
122
123 const char *strxf_xfrmproto(__u8 proto)
124 {
125 int i;
126
127 for (i = 0; ; i++) {
128 const struct typeent *t = &xfrmproto_types[i];
129 if (!t->t_name || t->t_type == -1)
130 break;
131
132 if (t->t_type == proto)
133 return t->t_name;
134 }
135
136 return NULL;
137 }
138
139 static const struct typeent algo_types[]= {
140 { "enc", XFRMA_ALG_CRYPT }, { "auth", XFRMA_ALG_AUTH },
141 { "comp", XFRMA_ALG_COMP }, { NULL, -1 }
142 };
143
144 int xfrm_algotype_getbyname(char *name)
145 {
146 int i;
147
148 for (i = 0; ; i++) {
149 const struct typeent *t = &algo_types[i];
150 if (!t->t_name || t->t_type == -1)
151 break;
152
153 if (strcmp(t->t_name, name) == 0)
154 return t->t_type;
155 }
156
157 return -1;
158 }
159
160 const char *strxf_algotype(int type)
161 {
162 int i;
163
164 for (i = 0; ; i++) {
165 const struct typeent *t = &algo_types[i];
166 if (!t->t_name || t->t_type == -1)
167 break;
168
169 if (t->t_type == type)
170 return t->t_name;
171 }
172
173 return NULL;
174 }
175
176 const char *strxf_mask8(__u8 mask)
177 {
178 static char str[16];
179 const int sn = sizeof(mask) * 8 - 1;
180 __u8 b;
181 int i = 0;
182
183 for (b = (1 << sn); b > 0; b >>= 1)
184 str[i++] = ((b & mask) ? '1' : '0');
185 str[i] = '\0';
186
187 return str;
188 }
189
190 const char *strxf_mask32(__u32 mask)
191 {
192 static char str[16];
193
194 sprintf(str, "%.8x", mask);
195
196 return str;
197 }
198
199 const char *strxf_share(__u8 share)
200 {
201 static char str[32];
202
203 switch (share) {
204 case XFRM_SHARE_ANY:
205 strcpy(str, "any");
206 break;
207 case XFRM_SHARE_SESSION:
208 strcpy(str, "session");
209 break;
210 case XFRM_SHARE_USER:
211 strcpy(str, "user");
212 break;
213 case XFRM_SHARE_UNIQUE:
214 strcpy(str, "unique");
215 break;
216 default:
217 sprintf(str, "%u", share);
218 break;
219 }
220
221 return str;
222 }
223
224 const char *strxf_proto(__u8 proto)
225 {
226 static char buf[32];
227 struct protoent *pp;
228 const char *p;
229
230 pp = getprotobynumber(proto);
231 if (pp)
232 p = pp->p_name;
233 else {
234 sprintf(buf, "%u", proto);
235 p = buf;
236 }
237
238 return p;
239 }
240
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)
244 {
245 char abuf[256];
246
247 if (title)
248 fprintf(fp, title);
249
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_);
257
258 if (prefix)
259 fprintf(fp, prefix);
260 fprintf(fp, "\t");
261
262 fprintf(fp, "proto %s ", strxf_xfrmproto(id->proto));
263
264 if (show_stats > 0 || force_spi || id->spi) {
265 __u32 spi = ntohl(id->spi);
266 fprintf(fp, "spi 0x%08x", spi);
267 if (show_stats > 0)
268 fprintf(fp, "(%u)", spi);
269 fprintf(fp, " ");
270 }
271
272 fprintf(fp, "reqid %u", reqid);
273 if (show_stats > 0)
274 fprintf(fp, "(0x%08x)", reqid);
275 fprintf(fp, " ");
276
277 fprintf(fp, "mode ");
278 switch (mode) {
279 case 0:
280 fprintf(fp, "transport");
281 break;
282 case 1:
283 fprintf(fp, "tunnel");
284 break;
285 case 4:
286 fprintf(fp, "beet");
287 break;
288 default:
289 fprintf(fp, "%u", mode);
290 break;
291 }
292 fprintf(fp, "%s", _SL_);
293 }
294
295 static const char *strxf_limit(__u64 limit)
296 {
297 static char str[32];
298 if (limit == XFRM_INF)
299 strcpy(str, "(INF)");
300 else
301 sprintf(str, "%llu", (unsigned long long) limit);
302
303 return str;
304 }
305
306 void xfrm_stats_print(struct xfrm_stats *s, FILE *fp, const char *prefix)
307 {
308 if (prefix)
309 fprintf(fp, prefix);
310 fprintf(fp, "stats:");
311 fprintf(fp, "%s", _SL_);
312
313 if (prefix)
314 fprintf(fp, prefix);
315 fprintf(fp, " ");
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_);
320 }
321
322 static const char *strxf_time(__u64 time)
323 {
324 static char str[32];
325
326 if (time == 0)
327 strcpy(str, "-");
328 else {
329 time_t t;
330 struct tm *tp;
331
332 /* XXX: treat time in the same manner of kernel's
333 * net/xfrm/xfrm_{user,state}.c
334 */
335 t = (long)time;
336 tp = localtime(&t);
337
338 strftime(str, sizeof(str), "%Y-%m-%d %T", tp);
339 }
340
341 return str;
342 }
343
344 void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg,
345 struct xfrm_lifetime_cur *cur,
346 FILE *fp, const char *prefix)
347 {
348 if (cfg) {
349 if (prefix)
350 fprintf(fp, prefix);
351 fprintf(fp, "lifetime config:");
352 fprintf(fp, "%s", _SL_);
353
354 if (prefix)
355 fprintf(fp, prefix);
356 fprintf(fp, " ");
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_);
364
365 if (prefix)
366 fprintf(fp, prefix);
367 fprintf(fp, " ");
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_);
375
376 if (prefix)
377 fprintf(fp, prefix);
378 fprintf(fp, " ");
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_);
386
387 if (prefix)
388 fprintf(fp, prefix);
389 fprintf(fp, " ");
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_);
397 }
398 if (cur) {
399 if (prefix)
400 fprintf(fp, prefix);
401 fprintf(fp, "lifetime current:");
402 fprintf(fp, "%s", _SL_);
403
404 if (prefix)
405 fprintf(fp, prefix);
406 fprintf(fp, " ");
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_);
410
411 if (prefix)
412 fprintf(fp, prefix);
413 fprintf(fp, " ");
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_);
417 }
418 }
419
420 void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
421 FILE *fp, const char *prefix)
422 {
423 char abuf[256];
424 __u16 f;
425
426 f = sel->family;
427 if (f == AF_UNSPEC)
428 f = family;
429 if (f == AF_UNSPEC)
430 f = preferred_family;
431
432 if (prefix)
433 fprintf(fp, prefix);
434
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)),
438 sel->prefixlen_s);
439
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)),
443 sel->prefixlen_d);
444
445 if (sel->proto)
446 fprintf(fp, "proto %s ", strxf_proto(sel->proto));
447 switch (sel->proto) {
448 case IPPROTO_TCP:
449 case IPPROTO_UDP:
450 case IPPROTO_SCTP:
451 case IPPROTO_DCCP:
452 default: /* XXX */
453 if (sel->sport_mask)
454 fprintf(fp, "sport %u ", ntohs(sel->sport));
455 if (sel->dport_mask)
456 fprintf(fp, "dport %u ", ntohs(sel->dport));
457 break;
458 case IPPROTO_ICMP:
459 case IPPROTO_ICMPV6:
460 /* type/code is stored at sport/dport in selector */
461 if (sel->sport_mask)
462 fprintf(fp, "type %u ", ntohs(sel->sport));
463 if (sel->dport_mask)
464 fprintf(fp, "code %u ", ntohs(sel->dport));
465 break;
466 }
467
468 if (sel->ifindex > 0) {
469 char buf[IFNAMSIZ];
470
471 memset(buf, '\0', sizeof(buf));
472 if_indextoname(sel->ifindex, buf);
473 fprintf(fp, "dev %s ", buf);
474 }
475
476 if (show_stats > 0)
477 fprintf(fp, "uid %u", sel->user);
478
479 fprintf(fp, "%s", _SL_);
480 }
481
482 static void xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
483 FILE *fp, const char *prefix)
484 {
485 int keylen;
486 int i;
487
488 if (prefix)
489 fprintf(fp, prefix);
490
491 fprintf(fp, "%s ", strxf_algotype(type));
492
493 if (len < sizeof(*algo)) {
494 fprintf(fp, "(ERROR truncated)");
495 goto fin;
496 }
497 len -= sizeof(*algo);
498
499 fprintf(fp, "%s ", algo->alg_name);
500
501 keylen = algo->alg_key_len / 8;
502 if (len < keylen) {
503 fprintf(fp, "(ERROR truncated)");
504 goto fin;
505 }
506
507 fprintf(fp, "0x");
508 for (i = 0; i < keylen; i ++)
509 fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]);
510
511 if (show_stats > 0)
512 fprintf(fp, " (%d bits)", algo->alg_key_len);
513
514 fin:
515 fprintf(fp, "%s", _SL_);
516 }
517
518 static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int len,
519 __u16 family, FILE *fp, const char *prefix)
520 {
521 int ntmpls = len / sizeof(struct xfrm_user_tmpl);
522 int i;
523
524 if (ntmpls <= 0) {
525 if (prefix)
526 fprintf(fp, prefix);
527 fprintf(fp, "(ERROR \"tmpl\" truncated)");
528 fprintf(fp, "%s", _SL_);
529 return;
530 }
531
532 for (i = 0; i < ntmpls; i++) {
533 struct xfrm_user_tmpl *tmpl = &tmpls[i];
534
535 if (prefix)
536 fprintf(fp, prefix);
537
538 xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode,
539 tmpl->reqid, family, 0, fp, prefix, "tmpl ");
540
541 if (show_stats > 0 || tmpl->optional) {
542 if (prefix)
543 fprintf(fp, prefix);
544 fprintf(fp, "\t");
545 switch (tmpl->optional) {
546 case 0:
547 if (show_stats > 0)
548 fprintf(fp, "level required ");
549 break;
550 case 1:
551 fprintf(fp, "level use ");
552 break;
553 default:
554 fprintf(fp, "level %u ", tmpl->optional);
555 break;
556 }
557
558 if (show_stats > 0)
559 fprintf(fp, "share %s ", strxf_share(tmpl->share));
560
561 fprintf(fp, "%s", _SL_);
562 }
563
564 if (show_stats > 0) {
565 if (prefix)
566 fprintf(fp, prefix);
567 fprintf(fp, "\t");
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));
577
578 fprintf(fp, "%s", _SL_);
579 }
580 }
581 }
582
583 void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
584 FILE *fp, const char *prefix)
585 {
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);
590 }
591
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);
596 }
597
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);
602 }
603
604 if (tb[XFRMA_ENCAP]) {
605 struct xfrm_encap_tmpl *e;
606 char abuf[256];
607
608 if (prefix)
609 fprintf(fp, prefix);
610 fprintf(fp, "encap ");
611
612 if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) {
613 fprintf(fp, "(ERROR truncated)");
614 fprintf(fp, "%s", _SL_);
615 return;
616 }
617 e = (struct xfrm_encap_tmpl *) RTA_DATA(tb[XFRMA_ENCAP]);
618
619 fprintf(fp, "type ");
620 switch (e->encap_type) {
621 case 1:
622 fprintf(fp, "espinudp-nonike ");
623 break;
624 case 2:
625 fprintf(fp, "espinudp ");
626 break;
627 default:
628 fprintf(fp, "%u ", e->encap_type);
629 break;
630 }
631 fprintf(fp, "sport %u ", ntohs(e->encap_sport));
632 fprintf(fp, "dport %u ", ntohs(e->encap_dport));
633
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_);
639 }
640
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);
645 }
646 }
647
648 static int xfrm_selector_iszero(struct xfrm_selector *s)
649 {
650 struct xfrm_selector s0;
651
652 memset(&s0, 0, sizeof(s0));
653
654 return (memcmp(&s0, s, sizeof(s0)) == 0);
655 }
656
657 void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo,
658 struct rtattr *tb[], FILE *fp, const char *prefix,
659 const char *title)
660 {
661 char buf[STRBUF_SIZE];
662
663 memset(buf, '\0', sizeof(buf));
664
665 xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode,
666 xsinfo->reqid, xsinfo->family, 1, fp, prefix,
667 title);
668
669 if (prefix)
670 STRBUF_CAT(buf, prefix);
671 STRBUF_CAT(buf, "\t");
672
673 fprintf(fp, buf);
674 fprintf(fp, "replay-window %u ", xsinfo->replay_window);
675 if (show_stats > 0)
676 fprintf(fp, "seq 0x%08u ", xsinfo->seq);
677 if (show_stats > 0 || xsinfo->flags) {
678 __u8 flags = xsinfo->flags;
679
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");
683 if (flags)
684 fprintf(fp, "%x", flags);
685 if (show_stats > 0)
686 fprintf(fp, " (0x%s)", strxf_mask8(flags));
687 }
688 fprintf(fp, "%s", _SL_);
689
690 xfrm_xfrma_print(tb, xsinfo->family, fp, buf);
691
692 if (!xfrm_selector_iszero(&xsinfo->sel)) {
693 char sbuf[STRBUF_SIZE];
694
695 memcpy(sbuf, buf, sizeof(sbuf));
696 STRBUF_CAT(sbuf, "sel ");
697
698 xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, sbuf);
699 }
700
701 if (show_stats > 0) {
702 xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, buf);
703 xfrm_stats_print(&xsinfo->stats, fp, buf);
704 }
705 }
706
707 void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
708 struct rtattr *tb[], FILE *fp, const char *prefix,
709 const char *title)
710 {
711 char buf[STRBUF_SIZE];
712
713 memset(buf, '\0', sizeof(buf));
714
715 xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title);
716
717 if (prefix)
718 STRBUF_CAT(buf, prefix);
719 STRBUF_CAT(buf, "\t");
720
721 fprintf(fp, buf);
722 fprintf(fp, "dir ");
723 switch (xpinfo->dir) {
724 case XFRM_POLICY_IN:
725 fprintf(fp, "in");
726 break;
727 case XFRM_POLICY_OUT:
728 fprintf(fp, "out");
729 break;
730 case XFRM_POLICY_FWD:
731 fprintf(fp, "fwd");
732 break;
733 default:
734 fprintf(fp, "%u", xpinfo->dir);
735 break;
736 }
737 fprintf(fp, " ");
738
739 switch (xpinfo->action) {
740 case XFRM_POLICY_ALLOW:
741 if (show_stats > 0)
742 fprintf(fp, "action allow ");
743 break;
744 case XFRM_POLICY_BLOCK:
745 fprintf(fp, "action block ");
746 break;
747 default:
748 fprintf(fp, "action %u ", xpinfo->action);
749 break;
750 }
751
752 if (show_stats)
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));
758 }
759 fprintf(fp, "%s", _SL_);
760
761 if (show_stats > 0)
762 xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf);
763
764 xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf);
765 }
766
767 int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
768 int loose, int *argcp, char ***argvp)
769 {
770 int argc = *argcp;
771 char **argv = *argvp;
772 inet_prefix dst;
773 inet_prefix src;
774
775 memset(&dst, 0, sizeof(dst));
776 memset(&src, 0, sizeof(src));
777
778 while (1) {
779 if (strcmp(*argv, "src") == 0) {
780 NEXT_ARG();
781
782 get_prefix(&src, *argv, preferred_family);
783 if (src.family == AF_UNSPEC)
784 invarg("\"src\" address family is AF_UNSPEC", *argv);
785 if (family)
786 *family = src.family;
787
788 memcpy(saddr, &src.data, sizeof(*saddr));
789
790 filter.id_src_mask = src.bitlen;
791
792 } else if (strcmp(*argv, "dst") == 0) {
793 NEXT_ARG();
794
795 get_prefix(&dst, *argv, preferred_family);
796 if (dst.family == AF_UNSPEC)
797 invarg("\"dst\" address family is AF_UNSPEC", *argv);
798 if (family)
799 *family = dst.family;
800
801 memcpy(&id->daddr, &dst.data, sizeof(id->daddr));
802
803 filter.id_dst_mask = dst.bitlen;
804
805 } else if (strcmp(*argv, "proto") == 0) {
806 int ret;
807
808 NEXT_ARG();
809
810 ret = xfrm_xfrmproto_getbyname(*argv);
811 if (ret < 0)
812 invarg("\"XFRM_PROTO\" is invalid", *argv);
813
814 id->proto = (__u8)ret;
815
816 filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
817
818 } else if (strcmp(*argv, "spi") == 0) {
819 __u32 spi;
820
821 NEXT_ARG();
822 if (get_u32(&spi, *argv, 0))
823 invarg("\"SPI\" is invalid", *argv);
824
825 spi = htonl(spi);
826 id->spi = spi;
827
828 filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
829
830 } else {
831 PREV_ARG(); /* back track */
832 break;
833 }
834
835 if (!NEXT_ARG_OK())
836 break;
837 NEXT_ARG();
838 }
839
840 if (src.family && dst.family && (src.family != dst.family))
841 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
842
843 if (loose == 0 && id->proto == 0)
844 missarg("XFRM_PROTO");
845 if (argc == *argcp)
846 missarg("ID");
847
848 *argcp = argc;
849 *argvp = argv;
850
851 return 0;
852 }
853
854 int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp)
855 {
856 int argc = *argcp;
857 char **argv = *argvp;
858
859 if (matches(*argv, "transport") == 0)
860 *mode = 0;
861 else if (matches(*argv, "tunnel") == 0)
862 *mode = 1;
863 else if (matches(*argv, "beet") == 0)
864 *mode = 4;
865 else
866 invarg("\"MODE\" is invalid", *argv);
867
868 *argcp = argc;
869 *argvp = argv;
870
871 return 0;
872 }
873
874 int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp)
875 {
876 int argc = *argcp;
877 char **argv = *argvp;
878
879 if (strcmp(*argv, "espinudp-nonike") == 0)
880 *type = 1;
881 else if (strcmp(*argv, "espinudp") == 0)
882 *type = 2;
883 else
884 invarg("\"ENCAP-TYPE\" is invalid", *argv);
885
886 *argcp = argc;
887 *argvp = argv;
888
889 return 0;
890 }
891
892 /* NOTE: reqid is used by host-byte order */
893 int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp)
894 {
895 int argc = *argcp;
896 char **argv = *argvp;
897
898 if (get_u32(reqid, *argv, 0))
899 invarg("\"REQID\" is invalid", *argv);
900
901 *argcp = argc;
902 *argvp = argv;
903
904 return 0;
905 }
906
907 static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
908 int *argcp, char ***argvp)
909 {
910 int argc = *argcp;
911 char **argv = *argvp;
912 char *sportp = NULL;
913 char *dportp = NULL;
914 char *typep = NULL;
915 char *codep = NULL;
916
917 while (1) {
918 if (strcmp(*argv, "proto") == 0) {
919 __u8 upspec;
920
921 NEXT_ARG();
922
923 if (strcmp(*argv, "any") == 0)
924 upspec = 0;
925 else {
926 struct protoent *pp;
927 pp = getprotobyname(*argv);
928 if (pp)
929 upspec = pp->p_proto;
930 else {
931 if (get_u8(&upspec, *argv, 0))
932 invarg("\"PROTO\" is invalid", *argv);
933 }
934 }
935 sel->proto = upspec;
936
937 filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL;
938
939 } else if (strcmp(*argv, "sport") == 0) {
940 sportp = *argv;
941
942 NEXT_ARG();
943
944 if (get_u16(&sel->sport, *argv, 0))
945 invarg("\"PORT\" is invalid", *argv);
946 sel->sport = htons(sel->sport);
947 if (sel->sport)
948 sel->sport_mask = ~((__u16)0);
949
950 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
951
952 } else if (strcmp(*argv, "dport") == 0) {
953 dportp = *argv;
954
955 NEXT_ARG();
956
957 if (get_u16(&sel->dport, *argv, 0))
958 invarg("\"PORT\" is invalid", *argv);
959 sel->dport = htons(sel->dport);
960 if (sel->dport)
961 sel->dport_mask = ~((__u16)0);
962
963 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
964
965 } else if (strcmp(*argv, "type") == 0) {
966 typep = *argv;
967
968 NEXT_ARG();
969
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);
975
976 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
977
978
979 } else if (strcmp(*argv, "code") == 0) {
980 codep = *argv;
981
982 NEXT_ARG();
983
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);
989
990 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
991
992 } else {
993 PREV_ARG(); /* back track */
994 break;
995 }
996
997 if (!NEXT_ARG_OK())
998 break;
999 NEXT_ARG();
1000 }
1001 if (argc == *argcp)
1002 missarg("UPSPEC");
1003 if (sportp || dportp) {
1004 switch (sel->proto) {
1005 case IPPROTO_TCP:
1006 case IPPROTO_UDP:
1007 case IPPROTO_SCTP:
1008 case IPPROTO_DCCP:
1009 break;
1010 default:
1011 fprintf(stderr, "\"sport\" and \"dport\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1012 exit(1);
1013 }
1014 }
1015 if (typep || codep) {
1016 switch (sel->proto) {
1017 case IPPROTO_ICMP:
1018 case IPPROTO_ICMPV6:
1019 break;
1020 default:
1021 fprintf(stderr, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1022 exit(1);
1023 }
1024 }
1025
1026 *argcp = argc;
1027 *argvp = argv;
1028
1029 return 0;
1030 }
1031
1032 int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
1033 {
1034 int argc = *argcp;
1035 char **argv = *argvp;
1036 inet_prefix dst;
1037 inet_prefix src;
1038 char *upspecp = NULL;
1039
1040 memset(&dst, 0, sizeof(dst));
1041 memset(&src, 0, sizeof(src));
1042
1043 while (1) {
1044 if (strcmp(*argv, "src") == 0) {
1045 NEXT_ARG();
1046
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;
1051
1052 memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
1053 sel->prefixlen_s = src.bitlen;
1054
1055 filter.sel_src_mask = src.bitlen;
1056
1057 } else if (strcmp(*argv, "dst") == 0) {
1058 NEXT_ARG();
1059
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;
1064
1065 memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
1066 sel->prefixlen_d = dst.bitlen;
1067
1068 filter.sel_dst_mask = dst.bitlen;
1069
1070 } else if (strcmp(*argv, "dev") == 0) {
1071 int ifindex;
1072
1073 NEXT_ARG();
1074
1075 if (strcmp(*argv, "none") == 0)
1076 ifindex = 0;
1077 else {
1078 ifindex = if_nametoindex(*argv);
1079 if (ifindex <= 0)
1080 invarg("\"DEV\" is invalid", *argv);
1081 }
1082 sel->ifindex = ifindex;
1083
1084 filter.sel_dev_mask = XFRM_FILTER_MASK_FULL;
1085
1086 } else {
1087 if (upspecp) {
1088 PREV_ARG(); /* back track */
1089 break;
1090 } else {
1091 upspecp = *argv;
1092 xfrm_selector_upspec_parse(sel, &argc, &argv);
1093 }
1094 }
1095
1096 if (!NEXT_ARG_OK())
1097 break;
1098
1099 NEXT_ARG();
1100 }
1101
1102 if (src.family && dst.family && (src.family != dst.family))
1103 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
1104
1105 if (argc == *argcp)
1106 missarg("SELECTOR");
1107
1108 *argcp = argc;
1109 *argvp = argv;
1110
1111 return 0;
1112 }
1113
1114 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft,
1115 int *argcp, char ***argvp)
1116 {
1117 int argc = *argcp;
1118 char **argv = *argvp;
1119 int ret;
1120
1121 if (strcmp(*argv, "time-soft") == 0) {
1122 NEXT_ARG();
1123 ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
1124 if (ret)
1125 invarg("\"time-soft\" value is invalid", *argv);
1126 } else if (strcmp(*argv, "time-hard") == 0) {
1127 NEXT_ARG();
1128 ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
1129 if (ret)
1130 invarg("\"time-hard\" value is invalid", *argv);
1131 } else if (strcmp(*argv, "time-use-soft") == 0) {
1132 NEXT_ARG();
1133 ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
1134 if (ret)
1135 invarg("\"time-use-soft\" value is invalid", *argv);
1136 } else if (strcmp(*argv, "time-use-hard") == 0) {
1137 NEXT_ARG();
1138 ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
1139 if (ret)
1140 invarg("\"time-use-hard\" value is invalid", *argv);
1141 } else if (strcmp(*argv, "byte-soft") == 0) {
1142 NEXT_ARG();
1143 ret = get_u64(&lft->soft_byte_limit, *argv, 0);
1144 if (ret)
1145 invarg("\"byte-soft\" value is invalid", *argv);
1146 } else if (strcmp(*argv, "byte-hard") == 0) {
1147 NEXT_ARG();
1148 ret = get_u64(&lft->hard_byte_limit, *argv, 0);
1149 if (ret)
1150 invarg("\"byte-hard\" value is invalid", *argv);
1151 } else if (strcmp(*argv, "packet-soft") == 0) {
1152 NEXT_ARG();
1153 ret = get_u64(&lft->soft_packet_limit, *argv, 0);
1154 if (ret)
1155 invarg("\"packet-soft\" value is invalid", *argv);
1156 } else if (strcmp(*argv, "packet-hard") == 0) {
1157 NEXT_ARG();
1158 ret = get_u64(&lft->hard_packet_limit, *argv, 0);
1159 if (ret)
1160 invarg("\"packet-hard\" value is invalid", *argv);
1161 } else
1162 invarg("\"LIMIT\" is invalid", *argv);
1163
1164 *argcp = argc;
1165 *argvp = argv;
1166
1167 return 0;
1168 }
1169
1170 int do_xfrm(int argc, char **argv)
1171 {
1172 memset(&filter, 0, sizeof(filter));
1173
1174 if (argc < 1)
1175 usage();
1176
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) {
1185 usage();
1186 fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv);
1187 exit(-1);
1188 }
1189 usage();
1190 }