]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipxfrm.c
man: dcb-ets: Remove an unnecessary empty line
[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, see <http://www.gnu.org/licenses>.
18 */
19 /*
20 * based on ip.c, iproute.c
21 */
22 /*
23 * Authors:
24 * Masahide NAKAMURA @USAGI
25 */
26
27 #include <alloca.h>
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 <linux/netlink.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/udp.h>
38
39 #include "utils.h"
40 #include "xfrm.h"
41 #include "ip_common.h"
42
43 #define STRBUF_SIZE (128)
44
45 struct xfrm_filter filter;
46
47 static void usage(void) __attribute__((noreturn));
48
49 static void usage(void)
50 {
51 fprintf(stderr,
52 "Usage: ip xfrm XFRM-OBJECT { COMMAND | help }\n"
53 "where XFRM-OBJECT := state | policy | monitor\n");
54 exit(-1);
55 }
56
57 /* This is based on utils.c(inet_addr_match) */
58 int xfrm_addr_match(xfrm_address_t *x1, xfrm_address_t *x2, int bits)
59 {
60 __u32 *a1 = (__u32 *)x1;
61 __u32 *a2 = (__u32 *)x2;
62 int words = bits >> 0x05;
63
64 bits &= 0x1f;
65
66 if (words)
67 if (memcmp(a1, a2, words << 2))
68 return -1;
69
70 if (bits) {
71 __u32 w1, w2;
72 __u32 mask;
73
74 w1 = a1[words];
75 w2 = a2[words];
76
77 mask = htonl((0xffffffff) << (0x20 - bits));
78
79 if ((w1 ^ w2) & mask)
80 return 1;
81 }
82
83 return 0;
84 }
85
86 int xfrm_xfrmproto_is_ipsec(__u8 proto)
87 {
88 return (proto == IPPROTO_ESP ||
89 proto == IPPROTO_AH ||
90 proto == IPPROTO_COMP);
91 }
92
93 int xfrm_xfrmproto_is_ro(__u8 proto)
94 {
95 return (proto == IPPROTO_ROUTING ||
96 proto == IPPROTO_DSTOPTS);
97 }
98
99 struct typeent {
100 const char *t_name;
101 int t_type;
102 };
103
104 static const struct typeent xfrmproto_types[] = {
105 { "esp", IPPROTO_ESP }, { "ah", IPPROTO_AH }, { "comp", IPPROTO_COMP },
106 { "route2", IPPROTO_ROUTING }, { "hao", IPPROTO_DSTOPTS },
107 { "ipsec-any", IPSEC_PROTO_ANY },
108 { NULL, -1 }
109 };
110
111 int xfrm_xfrmproto_getbyname(char *name)
112 {
113 int i;
114
115 for (i = 0; ; i++) {
116 const struct typeent *t = &xfrmproto_types[i];
117
118 if (!t->t_name || t->t_type == -1)
119 break;
120
121 if (strcmp(t->t_name, name) == 0)
122 return t->t_type;
123 }
124
125 return -1;
126 }
127
128 const char *strxf_xfrmproto(__u8 proto)
129 {
130 static char str[16];
131 int i;
132
133 for (i = 0; ; i++) {
134 const struct typeent *t = &xfrmproto_types[i];
135
136 if (!t->t_name || t->t_type == -1)
137 break;
138
139 if (t->t_type == proto)
140 return t->t_name;
141 }
142
143 sprintf(str, "%u", proto);
144 return str;
145 }
146
147 static const struct typeent algo_types[] = {
148 { "enc", XFRMA_ALG_CRYPT }, { "auth", XFRMA_ALG_AUTH },
149 { "comp", XFRMA_ALG_COMP }, { "aead", XFRMA_ALG_AEAD },
150 { "auth-trunc", XFRMA_ALG_AUTH_TRUNC },
151 { NULL, -1 }
152 };
153
154 int xfrm_algotype_getbyname(char *name)
155 {
156 int i;
157
158 for (i = 0; ; i++) {
159 const struct typeent *t = &algo_types[i];
160
161 if (!t->t_name || t->t_type == -1)
162 break;
163
164 if (strcmp(t->t_name, name) == 0)
165 return t->t_type;
166 }
167
168 return -1;
169 }
170
171 const char *strxf_algotype(int type)
172 {
173 static char str[32];
174 int i;
175
176 for (i = 0; ; i++) {
177 const struct typeent *t = &algo_types[i];
178
179 if (!t->t_name || t->t_type == -1)
180 break;
181
182 if (t->t_type == type)
183 return t->t_name;
184 }
185
186 sprintf(str, "%d", type);
187 return str;
188 }
189
190 static const char *strxf_mask8(__u8 mask)
191 {
192 static char str[16];
193 const int sn = sizeof(mask) * 8 - 1;
194 __u8 b;
195 int i = 0;
196
197 for (b = (1 << sn); b > 0; b >>= 1)
198 str[i++] = ((b & mask) ? '1' : '0');
199 str[i] = '\0';
200
201 return str;
202 }
203
204 const char *strxf_mask32(__u32 mask)
205 {
206 static char str[16];
207
208 sprintf(str, "%.8x", mask);
209
210 return str;
211 }
212
213 static const char *strxf_share(__u8 share)
214 {
215 static char str[32];
216
217 switch (share) {
218 case XFRM_SHARE_ANY:
219 strcpy(str, "any");
220 break;
221 case XFRM_SHARE_SESSION:
222 strcpy(str, "session");
223 break;
224 case XFRM_SHARE_USER:
225 strcpy(str, "user");
226 break;
227 case XFRM_SHARE_UNIQUE:
228 strcpy(str, "unique");
229 break;
230 default:
231 sprintf(str, "%u", share);
232 break;
233 }
234
235 return str;
236 }
237
238 const char *strxf_proto(__u8 proto)
239 {
240 static char buf[32];
241 struct protoent *pp;
242 const char *p;
243
244 pp = getprotobynumber(proto);
245 if (pp)
246 p = pp->p_name;
247 else {
248 sprintf(buf, "%u", proto);
249 p = buf;
250 }
251
252 return p;
253 }
254
255 const char *strxf_ptype(__u8 ptype)
256 {
257 static char str[16];
258
259 switch (ptype) {
260 case XFRM_POLICY_TYPE_MAIN:
261 strcpy(str, "main");
262 break;
263 case XFRM_POLICY_TYPE_SUB:
264 strcpy(str, "sub");
265 break;
266 default:
267 sprintf(str, "%u", ptype);
268 break;
269 }
270
271 return str;
272 }
273
274 static void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id,
275 __u8 mode, __u32 reqid, __u16 family, int force_spi,
276 FILE *fp, const char *prefix, const char *title)
277 {
278 if (title)
279 fputs(title, fp);
280
281 fprintf(fp, "src %s ", rt_addr_n2a(family, sizeof(*saddr), saddr));
282 fprintf(fp, "dst %s", rt_addr_n2a(family, sizeof(id->daddr), &id->daddr));
283 fprintf(fp, "%s", _SL_);
284
285 if (prefix)
286 fputs(prefix, fp);
287 fprintf(fp, "\t");
288
289 fprintf(fp, "proto %s ", strxf_xfrmproto(id->proto));
290
291 if (show_stats > 0 || force_spi || id->spi) {
292 __u32 spi = ntohl(id->spi);
293
294 fprintf(fp, "spi 0x%08x", spi);
295 if (show_stats > 0)
296 fprintf(fp, "(%u)", spi);
297 fprintf(fp, " ");
298 }
299
300 fprintf(fp, "reqid %u", reqid);
301 if (show_stats > 0)
302 fprintf(fp, "(0x%08x)", reqid);
303 fprintf(fp, " ");
304
305 fprintf(fp, "mode ");
306 switch (mode) {
307 case XFRM_MODE_TRANSPORT:
308 fprintf(fp, "transport");
309 break;
310 case XFRM_MODE_TUNNEL:
311 fprintf(fp, "tunnel");
312 break;
313 case XFRM_MODE_ROUTEOPTIMIZATION:
314 fprintf(fp, "ro");
315 break;
316 case XFRM_MODE_IN_TRIGGER:
317 fprintf(fp, "in_trigger");
318 break;
319 case XFRM_MODE_BEET:
320 fprintf(fp, "beet");
321 break;
322 default:
323 fprintf(fp, "%u", mode);
324 break;
325 }
326 fprintf(fp, "%s", _SL_);
327 }
328
329 static const char *strxf_limit(__u64 limit)
330 {
331 static char str[32];
332
333 if (limit == XFRM_INF)
334 strcpy(str, "(INF)");
335 else
336 sprintf(str, "%llu", (unsigned long long) limit);
337
338 return str;
339 }
340
341 static void xfrm_stats_print(struct xfrm_stats *s, FILE *fp,
342 const char *prefix)
343 {
344 if (prefix)
345 fputs(prefix, fp);
346 fprintf(fp, "stats:%s", _SL_);
347
348 if (prefix)
349 fputs(prefix, fp);
350 fprintf(fp, " replay-window %u replay %u failed %u%s",
351 s->replay_window, s->replay, s->integrity_failed, _SL_);
352 }
353
354 static const char *strxf_time(__u64 time)
355 {
356 static char str[32];
357
358 if (time == 0)
359 strcpy(str, "-");
360 else {
361 time_t t;
362 struct tm *tp;
363
364 /* XXX: treat time in the same manner of kernel's
365 * net/xfrm/xfrm_{user,state}.c
366 */
367 t = (long)time;
368 tp = localtime(&t);
369
370 strftime(str, sizeof(str), "%Y-%m-%d %T", tp);
371 }
372
373 return str;
374 }
375
376 static void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg,
377 struct xfrm_lifetime_cur *cur,
378 FILE *fp, const char *prefix)
379 {
380 if (cfg) {
381 if (prefix)
382 fputs(prefix, fp);
383 fprintf(fp, "lifetime config:%s", _SL_);
384
385 if (prefix)
386 fputs(prefix, fp);
387 fprintf(fp, " limit: soft %s(bytes),",
388 strxf_limit(cfg->soft_byte_limit));
389 fprintf(fp, " hard %s(bytes)%s",
390 strxf_limit(cfg->hard_byte_limit), _SL_);
391
392 if (prefix)
393 fputs(prefix, fp);
394 fprintf(fp, " limit: soft %s(packets),",
395 strxf_limit(cfg->soft_packet_limit));
396 fprintf(fp, " hard %s(packets)%s",
397 strxf_limit(cfg->hard_packet_limit), _SL_);
398
399 if (prefix)
400 fputs(prefix, fp);
401 fprintf(fp, " expire add: soft %llu(sec), hard %llu(sec)%s",
402 (unsigned long long) cfg->soft_add_expires_seconds,
403 (unsigned long long) cfg->hard_add_expires_seconds,
404 _SL_);
405
406 if (prefix)
407 fputs(prefix, fp);
408 fprintf(fp, " expire use: soft %llu(sec), hard %llu(sec)%s",
409 (unsigned long long) cfg->soft_use_expires_seconds,
410 (unsigned long long) cfg->hard_use_expires_seconds,
411 _SL_);
412 }
413 if (cur) {
414 if (prefix)
415 fputs(prefix, fp);
416 fprintf(fp, "lifetime current:%s", _SL_);
417
418 if (prefix)
419 fputs(prefix, fp);
420 fprintf(fp, " %llu(bytes), %llu(packets)%s",
421 (unsigned long long) cur->bytes,
422 (unsigned long long) cur->packets,
423 _SL_);
424
425 if (prefix)
426 fputs(prefix, fp);
427 fprintf(fp, " add %s ", strxf_time(cur->add_time));
428 fprintf(fp, "use %s%s", strxf_time(cur->use_time), _SL_);
429 }
430 }
431
432 void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
433 FILE *fp, const char *prefix)
434 {
435 __u16 f;
436
437 f = sel->family;
438 if (f == AF_UNSPEC)
439 f = family;
440 if (f == AF_UNSPEC)
441 f = preferred_family;
442
443 if (prefix)
444 fputs(prefix, fp);
445
446 fprintf(fp, "src %s/%u ",
447 rt_addr_n2a(f, sizeof(sel->saddr), &sel->saddr),
448 sel->prefixlen_s);
449
450 fprintf(fp, "dst %s/%u ",
451 rt_addr_n2a(f, sizeof(sel->daddr), &sel->daddr),
452 sel->prefixlen_d);
453
454 if (sel->proto)
455 fprintf(fp, "proto %s ", strxf_proto(sel->proto));
456 switch (sel->proto) {
457 case IPPROTO_TCP:
458 case IPPROTO_UDP:
459 case IPPROTO_SCTP:
460 case IPPROTO_DCCP:
461 default: /* XXX */
462 if (sel->sport_mask)
463 fprintf(fp, "sport %u ", ntohs(sel->sport));
464 if (sel->dport_mask)
465 fprintf(fp, "dport %u ", ntohs(sel->dport));
466 break;
467 case IPPROTO_ICMP:
468 case IPPROTO_ICMPV6:
469 /* type/code is stored at sport/dport in selector */
470 if (sel->sport_mask)
471 fprintf(fp, "type %u ", ntohs(sel->sport));
472 if (sel->dport_mask)
473 fprintf(fp, "code %u ", ntohs(sel->dport));
474 break;
475 case IPPROTO_GRE:
476 if (sel->sport_mask || sel->dport_mask)
477 fprintf(fp, "key %u ",
478 (((__u32)ntohs(sel->sport)) << 16) +
479 ntohs(sel->dport));
480 break;
481 case IPPROTO_MH:
482 if (sel->sport_mask)
483 fprintf(fp, "type %u ", ntohs(sel->sport));
484 if (sel->dport_mask) {
485 if (show_stats > 0)
486 fprintf(fp, "(dport) 0x%.4x ", sel->dport);
487 }
488 break;
489 }
490
491 if (sel->ifindex > 0)
492 fprintf(fp, "dev %s ", ll_index_to_name(sel->ifindex));
493
494 if (show_stats > 0)
495 fprintf(fp, "uid %u", sel->user);
496
497 fprintf(fp, "%s", _SL_);
498 }
499
500 static void __xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
501 FILE *fp, const char *prefix, int newline,
502 bool nokeys)
503 {
504 int keylen;
505 int i;
506
507 if (prefix)
508 fputs(prefix, fp);
509
510 fprintf(fp, "%s ", strxf_algotype(type));
511
512 if (len < sizeof(*algo)) {
513 fprintf(fp, "(ERROR truncated)");
514 goto fin;
515 }
516 len -= sizeof(*algo);
517
518 fprintf(fp, "%s ", algo->alg_name);
519
520 keylen = algo->alg_key_len / 8;
521 if (len < keylen) {
522 fprintf(fp, "(ERROR truncated)");
523 goto fin;
524 }
525
526 if (nokeys)
527 fprintf(fp, "<<Keys hidden>>");
528 else if (keylen > 0) {
529 fprintf(fp, "0x");
530 for (i = 0; i < keylen; i++)
531 fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]);
532
533 if (show_stats > 0)
534 fprintf(fp, " (%d bits)", algo->alg_key_len);
535 }
536
537 fin:
538 if (newline)
539 fprintf(fp, "%s", _SL_);
540 }
541
542 static inline void xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
543 FILE *fp, const char *prefix, bool nokeys)
544 {
545 return __xfrm_algo_print(algo, type, len, fp, prefix, 1, nokeys);
546 }
547
548 static void xfrm_aead_print(struct xfrm_algo_aead *algo, int len,
549 FILE *fp, const char *prefix, bool nokeys)
550 {
551 struct xfrm_algo *base_algo = alloca(sizeof(*base_algo) + algo->alg_key_len / 8);
552
553 memcpy(base_algo->alg_name, algo->alg_name, sizeof(base_algo->alg_name));
554 base_algo->alg_key_len = algo->alg_key_len;
555 memcpy(base_algo->alg_key, algo->alg_key, algo->alg_key_len / 8);
556
557 __xfrm_algo_print(base_algo, XFRMA_ALG_AEAD, len, fp, prefix, 0,
558 nokeys);
559
560 fprintf(fp, " %d", algo->alg_icv_len);
561
562 fprintf(fp, "%s", _SL_);
563 }
564
565 static void xfrm_auth_trunc_print(struct xfrm_algo_auth *algo, int len,
566 FILE *fp, const char *prefix, bool nokeys)
567 {
568 struct xfrm_algo *base_algo = alloca(sizeof(*base_algo) + algo->alg_key_len / 8);
569
570 memcpy(base_algo->alg_name, algo->alg_name, sizeof(base_algo->alg_name));
571 base_algo->alg_key_len = algo->alg_key_len;
572 memcpy(base_algo->alg_key, algo->alg_key, algo->alg_key_len / 8);
573
574 __xfrm_algo_print(base_algo, XFRMA_ALG_AUTH_TRUNC, len, fp, prefix, 0,
575 nokeys);
576
577 fprintf(fp, " %d", algo->alg_trunc_len);
578
579 fprintf(fp, "%s", _SL_);
580 }
581
582 static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int len,
583 FILE *fp, const char *prefix)
584 {
585 int ntmpls = len / sizeof(struct xfrm_user_tmpl);
586 int i;
587
588 if (ntmpls <= 0) {
589 if (prefix)
590 fputs(prefix, fp);
591 fprintf(fp, "(ERROR \"tmpl\" truncated)");
592 fprintf(fp, "%s", _SL_);
593 return;
594 }
595
596 for (i = 0; i < ntmpls; i++) {
597 struct xfrm_user_tmpl *tmpl = &tmpls[i];
598
599 if (prefix)
600 fputs(prefix, fp);
601
602 xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode,
603 tmpl->reqid, tmpl->family, 0, fp, prefix, "tmpl ");
604
605 if (show_stats > 0 || tmpl->optional) {
606 if (prefix)
607 fputs(prefix, fp);
608 fprintf(fp, "\t");
609 switch (tmpl->optional) {
610 case 0:
611 if (show_stats > 0)
612 fprintf(fp, "level required ");
613 break;
614 case 1:
615 fprintf(fp, "level use ");
616 break;
617 default:
618 fprintf(fp, "level %u ", tmpl->optional);
619 break;
620 }
621
622 if (show_stats > 0)
623 fprintf(fp, "share %s ", strxf_share(tmpl->share));
624
625 fprintf(fp, "%s", _SL_);
626 }
627
628 if (show_stats > 0) {
629 if (prefix)
630 fputs(prefix, fp);
631 fprintf(fp, "\t");
632 fprintf(fp, "%s-mask %s ",
633 strxf_algotype(XFRMA_ALG_CRYPT),
634 strxf_mask32(tmpl->ealgos));
635 fprintf(fp, "%s-mask %s ",
636 strxf_algotype(XFRMA_ALG_AUTH),
637 strxf_mask32(tmpl->aalgos));
638 fprintf(fp, "%s-mask %s",
639 strxf_algotype(XFRMA_ALG_COMP),
640 strxf_mask32(tmpl->calgos));
641
642 fprintf(fp, "%s", _SL_);
643 }
644 }
645 }
646
647 static void xfrm_output_mark_print(struct rtattr *tb[], FILE *fp)
648 {
649 __u32 output_mark = rta_getattr_u32(tb[XFRMA_OUTPUT_MARK]);
650
651 fprintf(fp, "output-mark 0x%x", output_mark);
652 if (tb[XFRMA_SET_MARK_MASK]) {
653 __u32 mask = rta_getattr_u32(tb[XFRMA_SET_MARK_MASK]);
654 fprintf(fp, "/0x%x", mask);
655 }
656 }
657
658 int xfrm_parse_mark(struct xfrm_mark *mark, int *argcp, char ***argvp)
659 {
660 int argc = *argcp;
661 char **argv = *argvp;
662
663 NEXT_ARG();
664 if (get_u32(&mark->v, *argv, 0)) {
665 invarg("MARK value is invalid\n", *argv);
666 }
667 if (argc > 1)
668 NEXT_ARG();
669 else { /* last entry on parse line */
670 mark->m = 0xffffffff;
671 goto done;
672 }
673
674 if (strcmp(*argv, "mask") == 0) {
675 NEXT_ARG();
676 if (get_u32(&mark->m, *argv, 0)) {
677 invarg("MASK value is invalid\n", *argv);
678 }
679 } else {
680 mark->m = 0xffffffff;
681 PREV_ARG();
682 }
683
684 done:
685 *argcp = argc;
686 *argvp = argv;
687
688 return 0;
689 }
690
691 void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
692 FILE *fp, const char *prefix, bool nokeys)
693 {
694 if (tb[XFRMA_MARK]) {
695 struct rtattr *rta = tb[XFRMA_MARK];
696 struct xfrm_mark *m = RTA_DATA(rta);
697
698 fprintf(fp, "\tmark %#x/%#x ", m->v, m->m);
699
700 if (tb[XFRMA_OUTPUT_MARK])
701 xfrm_output_mark_print(tb, fp);
702 fprintf(fp, "%s", _SL_);
703 } else if (tb[XFRMA_OUTPUT_MARK]) {
704 fprintf(fp, "\t");
705
706 xfrm_output_mark_print(tb, fp);
707 fprintf(fp, "%s", _SL_);
708 }
709
710 if (tb[XFRMA_ALG_AUTH] && !tb[XFRMA_ALG_AUTH_TRUNC]) {
711 struct rtattr *rta = tb[XFRMA_ALG_AUTH];
712
713 xfrm_algo_print(RTA_DATA(rta), XFRMA_ALG_AUTH, RTA_PAYLOAD(rta),
714 fp, prefix, nokeys);
715 }
716
717 if (tb[XFRMA_ALG_AUTH_TRUNC]) {
718 struct rtattr *rta = tb[XFRMA_ALG_AUTH_TRUNC];
719
720 xfrm_auth_trunc_print(RTA_DATA(rta), RTA_PAYLOAD(rta), fp,
721 prefix, nokeys);
722 }
723
724 if (tb[XFRMA_ALG_AEAD]) {
725 struct rtattr *rta = tb[XFRMA_ALG_AEAD];
726
727 xfrm_aead_print(RTA_DATA(rta), RTA_PAYLOAD(rta), fp, prefix,
728 nokeys);
729 }
730
731 if (tb[XFRMA_ALG_CRYPT]) {
732 struct rtattr *rta = tb[XFRMA_ALG_CRYPT];
733
734 xfrm_algo_print(RTA_DATA(rta), XFRMA_ALG_CRYPT,
735 RTA_PAYLOAD(rta), fp, prefix, nokeys);
736 }
737
738 if (tb[XFRMA_ALG_COMP]) {
739 struct rtattr *rta = tb[XFRMA_ALG_COMP];
740
741 xfrm_algo_print(RTA_DATA(rta), XFRMA_ALG_COMP, RTA_PAYLOAD(rta),
742 fp, prefix, nokeys);
743 }
744
745 if (tb[XFRMA_ENCAP]) {
746 struct xfrm_encap_tmpl *e;
747
748 if (prefix)
749 fputs(prefix, fp);
750 fprintf(fp, "encap ");
751
752 if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) {
753 fprintf(fp, "(ERROR truncated)");
754 fprintf(fp, "%s", _SL_);
755 return;
756 }
757 e = RTA_DATA(tb[XFRMA_ENCAP]);
758
759 fprintf(fp, "type ");
760 switch (e->encap_type) {
761 case UDP_ENCAP_ESPINUDP_NON_IKE:
762 fprintf(fp, "espinudp-nonike ");
763 break;
764 case UDP_ENCAP_ESPINUDP:
765 fprintf(fp, "espinudp ");
766 break;
767 case TCP_ENCAP_ESPINTCP:
768 fprintf(fp, "espintcp ");
769 break;
770 default:
771 fprintf(fp, "%u ", e->encap_type);
772 break;
773 }
774 fprintf(fp, "sport %u ", ntohs(e->encap_sport));
775 fprintf(fp, "dport %u ", ntohs(e->encap_dport));
776
777 fprintf(fp, "addr %s",
778 rt_addr_n2a(family, sizeof(e->encap_oa), &e->encap_oa));
779 fprintf(fp, "%s", _SL_);
780 }
781
782 if (tb[XFRMA_TMPL]) {
783 struct rtattr *rta = tb[XFRMA_TMPL];
784
785 xfrm_tmpl_print(RTA_DATA(rta),
786 RTA_PAYLOAD(rta), fp, prefix);
787 }
788
789 if (tb[XFRMA_COADDR]) {
790 const xfrm_address_t *coa;
791
792 if (prefix)
793 fputs(prefix, fp);
794 fprintf(fp, "coa ");
795
796 coa = RTA_DATA(tb[XFRMA_COADDR]);
797 if (RTA_PAYLOAD(tb[XFRMA_COADDR]) < sizeof(*coa)) {
798 fprintf(fp, "(ERROR truncated)");
799 fprintf(fp, "%s", _SL_);
800 return;
801 }
802
803 fprintf(fp, "%s",
804 rt_addr_n2a(family, sizeof(*coa), coa));
805 fprintf(fp, "%s", _SL_);
806 }
807
808 if (tb[XFRMA_LASTUSED]) {
809 __u64 lastused;
810
811 if (prefix)
812 fputs(prefix, fp);
813 fprintf(fp, "lastused ");
814
815 if (RTA_PAYLOAD(tb[XFRMA_LASTUSED]) < sizeof(lastused)) {
816 fprintf(fp, "(ERROR truncated)");
817 fprintf(fp, "%s", _SL_);
818 return;
819 }
820
821 lastused = rta_getattr_u64(tb[XFRMA_LASTUSED]);
822
823 fprintf(fp, "%s", strxf_time(lastused));
824 fprintf(fp, "%s", _SL_);
825 }
826
827 if (tb[XFRMA_REPLAY_VAL]) {
828 struct xfrm_replay_state *replay;
829
830 if (prefix)
831 fputs(prefix, fp);
832 fprintf(fp, "anti-replay context: ");
833
834 if (RTA_PAYLOAD(tb[XFRMA_REPLAY_VAL]) < sizeof(*replay)) {
835 fprintf(fp, "(ERROR truncated)");
836 fprintf(fp, "%s", _SL_);
837 return;
838 }
839
840 replay = RTA_DATA(tb[XFRMA_REPLAY_VAL]);
841 fprintf(fp, "seq 0x%x, oseq 0x%x, bitmap 0x%08x",
842 replay->seq, replay->oseq, replay->bitmap);
843 fprintf(fp, "%s", _SL_);
844 }
845
846 if (tb[XFRMA_REPLAY_ESN_VAL]) {
847 struct xfrm_replay_state_esn *replay;
848 unsigned int i, j;
849
850 if (prefix)
851 fputs(prefix, fp);
852 fprintf(fp, "anti-replay esn context:");
853
854 if (RTA_PAYLOAD(tb[XFRMA_REPLAY_ESN_VAL]) < sizeof(*replay)) {
855 fprintf(fp, "(ERROR truncated)");
856 fprintf(fp, "%s", _SL_);
857 return;
858 }
859 fprintf(fp, "%s", _SL_);
860
861 replay = RTA_DATA(tb[XFRMA_REPLAY_ESN_VAL]);
862 if (prefix)
863 fputs(prefix, fp);
864 fprintf(fp, " seq-hi 0x%x, seq 0x%x, oseq-hi 0x%0x, oseq 0x%0x",
865 replay->seq_hi, replay->seq, replay->oseq_hi,
866 replay->oseq);
867 fprintf(fp, "%s", _SL_);
868 if (prefix)
869 fputs(prefix, fp);
870 fprintf(fp, " replay_window %u, bitmap-length %u",
871 replay->replay_window, replay->bmp_len);
872 for (i = replay->bmp_len, j = 0; i; i--) {
873 if (j++ % 8 == 0) {
874 fprintf(fp, "%s", _SL_);
875 if (prefix)
876 fputs(prefix, fp);
877 fprintf(fp, " ");
878 }
879 fprintf(fp, "%08x ", replay->bmp[i - 1]);
880 }
881 fprintf(fp, "%s", _SL_);
882 }
883 if (tb[XFRMA_OFFLOAD_DEV]) {
884 struct xfrm_user_offload *xuo;
885
886 if (prefix)
887 fputs(prefix, fp);
888 fprintf(fp, "crypto offload parameters: ");
889
890 if (RTA_PAYLOAD(tb[XFRMA_OFFLOAD_DEV]) < sizeof(*xuo)) {
891 fprintf(fp, "(ERROR truncated)");
892 fprintf(fp, "%s", _SL_);
893 return;
894 }
895
896 xuo = (struct xfrm_user_offload *)
897 RTA_DATA(tb[XFRMA_OFFLOAD_DEV]);
898 fprintf(fp, "dev %s dir %s", ll_index_to_name(xuo->ifindex),
899 (xuo->flags & XFRM_OFFLOAD_INBOUND) ? "in" : "out");
900 fprintf(fp, "%s", _SL_);
901 }
902 if (tb[XFRMA_IF_ID]) {
903 __u32 if_id = rta_getattr_u32(tb[XFRMA_IF_ID]);
904
905 if (prefix)
906 fputs(prefix, fp);
907 fprintf(fp, "if_id %#x", if_id);
908 fprintf(fp, "%s", _SL_);
909 }
910 }
911
912 static int xfrm_selector_iszero(struct xfrm_selector *s)
913 {
914 struct xfrm_selector s0 = {};
915
916 return (memcmp(&s0, s, sizeof(s0)) == 0);
917 }
918
919 void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo,
920 struct rtattr *tb[], FILE *fp, const char *prefix,
921 const char *title, bool nokeys)
922 {
923 char buf[STRBUF_SIZE] = {};
924 int force_spi = xfrm_xfrmproto_is_ipsec(xsinfo->id.proto);
925
926 xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode,
927 xsinfo->reqid, xsinfo->family, force_spi, fp,
928 prefix, title);
929
930 if (prefix)
931 strlcat(buf, prefix, sizeof(buf));
932 strlcat(buf, "\t", sizeof(buf));
933
934 fputs(buf, fp);
935 fprintf(fp, "replay-window %u ", xsinfo->replay_window);
936 if (show_stats > 0)
937 fprintf(fp, "seq 0x%08u ", xsinfo->seq);
938 if (show_stats > 0 || xsinfo->flags) {
939 __u8 flags = xsinfo->flags;
940
941 fprintf(fp, "flag ");
942 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOECN, "noecn");
943 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp");
944 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOPMTUDISC, "nopmtudisc");
945 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_WILDRECV, "wildrecv");
946 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ICMP, "icmp");
947 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_AF_UNSPEC, "af-unspec");
948 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ALIGN4, "align4");
949 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ESN, "esn");
950 if (flags)
951 fprintf(fp, "%x", flags);
952 }
953 if (show_stats > 0 && tb[XFRMA_SA_EXTRA_FLAGS]) {
954 __u32 extra_flags = rta_getattr_u32(tb[XFRMA_SA_EXTRA_FLAGS]);
955
956 fprintf(fp, "extra_flag ");
957 XFRM_FLAG_PRINT(fp, extra_flags,
958 XFRM_SA_XFLAG_DONT_ENCAP_DSCP,
959 "dont-encap-dscp");
960 XFRM_FLAG_PRINT(fp, extra_flags,
961 XFRM_SA_XFLAG_OSEQ_MAY_WRAP,
962 "oseq-may-wrap");
963 if (extra_flags)
964 fprintf(fp, "%x", extra_flags);
965 }
966 if (show_stats > 0)
967 fprintf(fp, " (0x%s)", strxf_mask8(xsinfo->flags));
968 fprintf(fp, "%s", _SL_);
969
970 xfrm_xfrma_print(tb, xsinfo->family, fp, buf, nokeys);
971
972 if (!xfrm_selector_iszero(&xsinfo->sel)) {
973 char sbuf[STRBUF_SIZE];
974
975 memcpy(sbuf, buf, sizeof(sbuf));
976 strlcat(sbuf, "sel ", sizeof(sbuf));
977
978 xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, sbuf);
979 }
980
981 if (show_stats > 0) {
982 xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, buf);
983 xfrm_stats_print(&xsinfo->stats, fp, buf);
984 }
985
986 if (tb[XFRMA_SEC_CTX]) {
987 struct xfrm_user_sec_ctx *sctx;
988
989 fprintf(fp, "\tsecurity context ");
990
991 if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
992 fprintf(fp, "(ERROR truncated)");
993
994 sctx = RTA_DATA(tb[XFRMA_SEC_CTX]);
995
996 fprintf(fp, "%s %s", (char *)(sctx + 1), _SL_);
997 }
998
999 }
1000
1001 void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
1002 struct rtattr *tb[], FILE *fp, const char *prefix,
1003 const char *title)
1004 {
1005 char buf[STRBUF_SIZE] = {};
1006
1007 xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title);
1008
1009 if (tb[XFRMA_SEC_CTX]) {
1010 struct xfrm_user_sec_ctx *sctx;
1011
1012 fprintf(fp, "\tsecurity context ");
1013
1014 if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
1015 fprintf(fp, "(ERROR truncated)");
1016
1017 sctx = RTA_DATA(tb[XFRMA_SEC_CTX]);
1018
1019 fprintf(fp, "%s ", (char *)(sctx + 1));
1020 fprintf(fp, "%s", _SL_);
1021 }
1022
1023 if (prefix)
1024 strlcat(buf, prefix, sizeof(buf));
1025 strlcat(buf, "\t", sizeof(buf));
1026
1027 fputs(buf, fp);
1028 if (xpinfo->dir >= XFRM_POLICY_MAX) {
1029 xpinfo->dir -= XFRM_POLICY_MAX;
1030 fprintf(fp, "socket ");
1031 } else
1032 fprintf(fp, "dir ");
1033
1034 switch (xpinfo->dir) {
1035 case XFRM_POLICY_IN:
1036 fprintf(fp, "in");
1037 break;
1038 case XFRM_POLICY_OUT:
1039 fprintf(fp, "out");
1040 break;
1041 case XFRM_POLICY_FWD:
1042 fprintf(fp, "fwd");
1043 break;
1044 default:
1045 fprintf(fp, "%u", xpinfo->dir);
1046 break;
1047 }
1048 fprintf(fp, " ");
1049
1050 switch (xpinfo->action) {
1051 case XFRM_POLICY_ALLOW:
1052 if (show_stats > 0)
1053 fprintf(fp, "action allow ");
1054 break;
1055 case XFRM_POLICY_BLOCK:
1056 fprintf(fp, "action block ");
1057 break;
1058 default:
1059 fprintf(fp, "action %u ", xpinfo->action);
1060 break;
1061 }
1062
1063 if (show_stats)
1064 fprintf(fp, "index %u ", xpinfo->index);
1065 fprintf(fp, "priority %u ", xpinfo->priority);
1066
1067 if (tb[XFRMA_POLICY_TYPE]) {
1068 struct xfrm_userpolicy_type *upt;
1069
1070 fprintf(fp, "ptype ");
1071
1072 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
1073 fprintf(fp, "(ERROR truncated)");
1074
1075 upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]);
1076 fprintf(fp, "%s ", strxf_ptype(upt->type));
1077 }
1078
1079 if (show_stats > 0)
1080 fprintf(fp, "share %s ", strxf_share(xpinfo->share));
1081
1082 if (show_stats > 0 || xpinfo->flags) {
1083 __u8 flags = xpinfo->flags;
1084
1085 fprintf(fp, "flag ");
1086 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_LOCALOK, "localok");
1087 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_ICMP, "icmp");
1088 if (flags)
1089 fprintf(fp, "%x", flags);
1090 }
1091 if (show_stats > 0)
1092 fprintf(fp, " (0x%s)", strxf_mask8(xpinfo->flags));
1093 fprintf(fp, "%s", _SL_);
1094
1095 if (show_stats > 0)
1096 xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf);
1097
1098 xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf, false);
1099 }
1100
1101 int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
1102 int loose, int *argcp, char ***argvp)
1103 {
1104 int argc = *argcp;
1105 char **argv = *argvp;
1106 inet_prefix dst = {};
1107 inet_prefix src = {};
1108
1109 while (1) {
1110 if (strcmp(*argv, "src") == 0) {
1111 NEXT_ARG();
1112
1113 get_prefix(&src, *argv, preferred_family);
1114 if (src.family == AF_UNSPEC)
1115 invarg("value after \"src\" has an unrecognized address family", *argv);
1116 if (family)
1117 *family = src.family;
1118
1119 memcpy(saddr, &src.data, sizeof(*saddr));
1120
1121 filter.id_src_mask = src.bitlen;
1122
1123 } else if (strcmp(*argv, "dst") == 0) {
1124 NEXT_ARG();
1125
1126 get_prefix(&dst, *argv, preferred_family);
1127 if (dst.family == AF_UNSPEC)
1128 invarg("value after \"dst\" has an unrecognized address family", *argv);
1129 if (family)
1130 *family = dst.family;
1131
1132 memcpy(&id->daddr, &dst.data, sizeof(id->daddr));
1133
1134 filter.id_dst_mask = dst.bitlen;
1135
1136 } else if (strcmp(*argv, "proto") == 0) {
1137 int ret;
1138
1139 NEXT_ARG();
1140
1141 ret = xfrm_xfrmproto_getbyname(*argv);
1142 if (ret < 0)
1143 invarg("XFRM-PROTO value is invalid", *argv);
1144
1145 id->proto = (__u8)ret;
1146
1147 filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
1148
1149 } else if (strcmp(*argv, "spi") == 0) {
1150 NEXT_ARG();
1151 if (get_be32(&id->spi, *argv, 0))
1152 invarg("SPI value is invalid", *argv);
1153
1154 filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
1155
1156 } else {
1157 PREV_ARG(); /* back track */
1158 break;
1159 }
1160
1161 if (!NEXT_ARG_OK())
1162 break;
1163 NEXT_ARG();
1164 }
1165
1166 if (src.family && dst.family && (src.family != dst.family))
1167 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv);
1168
1169 if (id->spi && id->proto) {
1170 if (xfrm_xfrmproto_is_ro(id->proto)) {
1171 fprintf(stderr, "\"spi\" is invalid with XFRM-PROTO value \"%s\"\n",
1172 strxf_xfrmproto(id->proto));
1173 exit(1);
1174 } else if (id->proto == IPPROTO_COMP && ntohl(id->spi) >= 0x10000) {
1175 fprintf(stderr, "SPI value is too large with XFRM-PROTO value \"%s\"\n",
1176 strxf_xfrmproto(id->proto));
1177 exit(1);
1178 }
1179 }
1180
1181 if (loose == 0 && id->proto == 0)
1182 missarg("XFRM-PROTO");
1183 if (argc == *argcp)
1184 missarg("ID");
1185
1186 *argcp = argc;
1187 *argvp = argv;
1188
1189 return 0;
1190 }
1191
1192 int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp)
1193 {
1194 int argc = *argcp;
1195 char **argv = *argvp;
1196
1197 if (matches(*argv, "transport") == 0)
1198 *mode = XFRM_MODE_TRANSPORT;
1199 else if (matches(*argv, "tunnel") == 0)
1200 *mode = XFRM_MODE_TUNNEL;
1201 else if (matches(*argv, "ro") == 0)
1202 *mode = XFRM_MODE_ROUTEOPTIMIZATION;
1203 else if (matches(*argv, "in_trigger") == 0)
1204 *mode = XFRM_MODE_IN_TRIGGER;
1205 else if (matches(*argv, "beet") == 0)
1206 *mode = XFRM_MODE_BEET;
1207 else
1208 invarg("MODE value is invalid", *argv);
1209
1210 *argcp = argc;
1211 *argvp = argv;
1212
1213 return 0;
1214 }
1215
1216 int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp)
1217 {
1218 int argc = *argcp;
1219 char **argv = *argvp;
1220
1221 if (strcmp(*argv, "espinudp-nonike") == 0)
1222 *type = UDP_ENCAP_ESPINUDP_NON_IKE;
1223 else if (strcmp(*argv, "espinudp") == 0)
1224 *type = UDP_ENCAP_ESPINUDP;
1225 else if (strcmp(*argv, "espintcp") == 0)
1226 *type = TCP_ENCAP_ESPINTCP;
1227 else
1228 invarg("ENCAP-TYPE value is invalid", *argv);
1229
1230 *argcp = argc;
1231 *argvp = argv;
1232
1233 return 0;
1234 }
1235
1236 /* NOTE: reqid is used by host-byte order */
1237 int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp)
1238 {
1239 int argc = *argcp;
1240 char **argv = *argvp;
1241
1242 if (get_u32(reqid, *argv, 0))
1243 invarg("REQID value is invalid", *argv);
1244
1245 *argcp = argc;
1246 *argvp = argv;
1247
1248 return 0;
1249 }
1250
1251 static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
1252 int *argcp, char ***argvp)
1253 {
1254 int argc = *argcp;
1255 char **argv = *argvp;
1256 char *sportp = NULL;
1257 char *dportp = NULL;
1258 char *typep = NULL;
1259 char *codep = NULL;
1260 char *grekey = NULL;
1261
1262 while (1) {
1263 if (strcmp(*argv, "proto") == 0) {
1264 __u8 upspec;
1265
1266 NEXT_ARG();
1267
1268 if (strcmp(*argv, "any") == 0)
1269 upspec = 0;
1270 else {
1271 struct protoent *pp;
1272
1273 pp = getprotobyname(*argv);
1274 if (pp)
1275 upspec = pp->p_proto;
1276 else {
1277 if (get_u8(&upspec, *argv, 0))
1278 invarg("PROTO value is invalid", *argv);
1279 }
1280 }
1281 sel->proto = upspec;
1282
1283 filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL;
1284
1285 } else if (strcmp(*argv, "sport") == 0) {
1286 sportp = *argv;
1287
1288 NEXT_ARG();
1289
1290 if (get_be16(&sel->sport, *argv, 0))
1291 invarg("value after \"sport\" is invalid", *argv);
1292 if (sel->sport)
1293 sel->sport_mask = ~((__u16)0);
1294
1295 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1296
1297 } else if (strcmp(*argv, "dport") == 0) {
1298 dportp = *argv;
1299
1300 NEXT_ARG();
1301
1302 if (get_be16(&sel->dport, *argv, 0))
1303 invarg("value after \"dport\" is invalid", *argv);
1304 if (sel->dport)
1305 sel->dport_mask = ~((__u16)0);
1306
1307 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1308
1309 } else if (strcmp(*argv, "type") == 0) {
1310 typep = *argv;
1311
1312 NEXT_ARG();
1313
1314 if (get_u16(&sel->sport, *argv, 0) ||
1315 (sel->sport & ~((__u16)0xff)))
1316 invarg("value after \"type\" is invalid", *argv);
1317 sel->sport = htons(sel->sport);
1318 sel->sport_mask = ~((__u16)0);
1319
1320 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1321
1322
1323 } else if (strcmp(*argv, "code") == 0) {
1324 codep = *argv;
1325
1326 NEXT_ARG();
1327
1328 if (get_u16(&sel->dport, *argv, 0) ||
1329 (sel->dport & ~((__u16)0xff)))
1330 invarg("value after \"code\" is invalid", *argv);
1331 sel->dport = htons(sel->dport);
1332 sel->dport_mask = ~((__u16)0);
1333
1334 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1335
1336 } else if (strcmp(*argv, "key") == 0) {
1337 unsigned int uval;
1338
1339 grekey = *argv;
1340
1341 NEXT_ARG();
1342
1343 if (strchr(*argv, '.'))
1344 uval = htonl(get_addr32(*argv));
1345 else {
1346 if (get_unsigned(&uval, *argv, 0) < 0) {
1347 fprintf(stderr, "value after \"key\" is invalid\n");
1348 exit(-1);
1349 }
1350 }
1351
1352 sel->sport = htons(uval >> 16);
1353 sel->dport = htons(uval & 0xffff);
1354 sel->sport_mask = ~((__u16)0);
1355 sel->dport_mask = ~((__u16)0);
1356
1357 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1358
1359 } else {
1360 PREV_ARG(); /* back track */
1361 break;
1362 }
1363
1364 if (!NEXT_ARG_OK())
1365 break;
1366 NEXT_ARG();
1367 }
1368 if (argc == *argcp)
1369 missarg("UPSPEC");
1370 if (sportp || dportp) {
1371 switch (sel->proto) {
1372 case IPPROTO_TCP:
1373 case IPPROTO_UDP:
1374 case IPPROTO_SCTP:
1375 case IPPROTO_DCCP:
1376 case IPPROTO_IP: /* to allow shared SA for different protocols */
1377 break;
1378 default:
1379 fprintf(stderr, "\"sport\" and \"dport\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto));
1380 exit(1);
1381 }
1382 }
1383 if (typep || codep) {
1384 switch (sel->proto) {
1385 case IPPROTO_ICMP:
1386 case IPPROTO_ICMPV6:
1387 case IPPROTO_MH:
1388 break;
1389 default:
1390 fprintf(stderr, "\"type\" and \"code\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto));
1391 exit(1);
1392 }
1393 }
1394 if (grekey) {
1395 switch (sel->proto) {
1396 case IPPROTO_GRE:
1397 break;
1398 default:
1399 fprintf(stderr, "\"key\" is invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto));
1400 exit(1);
1401 }
1402 }
1403
1404 *argcp = argc;
1405 *argvp = argv;
1406
1407 return 0;
1408 }
1409
1410 int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
1411 {
1412 int argc = *argcp;
1413 char **argv = *argvp;
1414 inet_prefix dst = {};
1415 inet_prefix src = {};
1416 char *upspecp = NULL;
1417
1418 while (1) {
1419 if (strcmp(*argv, "src") == 0) {
1420 NEXT_ARG();
1421
1422 get_prefix(&src, *argv, preferred_family);
1423 if (src.family == AF_UNSPEC)
1424 invarg("value after \"src\" has an unrecognized address family", *argv);
1425 sel->family = src.family;
1426
1427 memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
1428 sel->prefixlen_s = src.bitlen;
1429
1430 filter.sel_src_mask = src.bitlen;
1431
1432 } else if (strcmp(*argv, "dst") == 0) {
1433 NEXT_ARG();
1434
1435 get_prefix(&dst, *argv, preferred_family);
1436 if (dst.family == AF_UNSPEC)
1437 invarg("value after \"dst\" has an unrecognized address family", *argv);
1438 sel->family = dst.family;
1439
1440 memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
1441 sel->prefixlen_d = dst.bitlen;
1442
1443 filter.sel_dst_mask = dst.bitlen;
1444
1445 } else if (strcmp(*argv, "dev") == 0) {
1446 int ifindex;
1447
1448 NEXT_ARG();
1449
1450 if (strcmp(*argv, "none") == 0)
1451 ifindex = 0;
1452 else {
1453 ifindex = ll_name_to_index(*argv);
1454 if (ifindex <= 0)
1455 invarg("DEV value is invalid", *argv);
1456 }
1457 sel->ifindex = ifindex;
1458
1459 filter.sel_dev_mask = XFRM_FILTER_MASK_FULL;
1460
1461 } else {
1462 if (upspecp) {
1463 PREV_ARG(); /* back track */
1464 break;
1465 } else {
1466 upspecp = *argv;
1467 xfrm_selector_upspec_parse(sel, &argc, &argv);
1468 }
1469 }
1470
1471 if (!NEXT_ARG_OK())
1472 break;
1473
1474 NEXT_ARG();
1475 }
1476
1477 if (src.family && dst.family && (src.family != dst.family))
1478 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv);
1479
1480 if (argc == *argcp)
1481 missarg("SELECTOR");
1482
1483 *argcp = argc;
1484 *argvp = argv;
1485
1486 return 0;
1487 }
1488
1489 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft,
1490 int *argcp, char ***argvp)
1491 {
1492 int argc = *argcp;
1493 char **argv = *argvp;
1494 int ret;
1495
1496 if (strcmp(*argv, "time-soft") == 0) {
1497 NEXT_ARG();
1498 ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
1499 if (ret)
1500 invarg("value after \"time-soft\" is invalid", *argv);
1501 } else if (strcmp(*argv, "time-hard") == 0) {
1502 NEXT_ARG();
1503 ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
1504 if (ret)
1505 invarg("value after \"time-hard\" is invalid", *argv);
1506 } else if (strcmp(*argv, "time-use-soft") == 0) {
1507 NEXT_ARG();
1508 ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
1509 if (ret)
1510 invarg("value after \"time-use-soft\" is invalid", *argv);
1511 } else if (strcmp(*argv, "time-use-hard") == 0) {
1512 NEXT_ARG();
1513 ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
1514 if (ret)
1515 invarg("value after \"time-use-hard\" is invalid", *argv);
1516 } else if (strcmp(*argv, "byte-soft") == 0) {
1517 NEXT_ARG();
1518 ret = get_u64(&lft->soft_byte_limit, *argv, 0);
1519 if (ret)
1520 invarg("value after \"byte-soft\" is invalid", *argv);
1521 } else if (strcmp(*argv, "byte-hard") == 0) {
1522 NEXT_ARG();
1523 ret = get_u64(&lft->hard_byte_limit, *argv, 0);
1524 if (ret)
1525 invarg("value after \"byte-hard\" is invalid", *argv);
1526 } else if (strcmp(*argv, "packet-soft") == 0) {
1527 NEXT_ARG();
1528 ret = get_u64(&lft->soft_packet_limit, *argv, 0);
1529 if (ret)
1530 invarg("value after \"packet-soft\" is invalid", *argv);
1531 } else if (strcmp(*argv, "packet-hard") == 0) {
1532 NEXT_ARG();
1533 ret = get_u64(&lft->hard_packet_limit, *argv, 0);
1534 if (ret)
1535 invarg("value after \"packet-hard\" is invalid", *argv);
1536 } else
1537 invarg("LIMIT value is invalid", *argv);
1538
1539 *argcp = argc;
1540 *argvp = argv;
1541
1542 return 0;
1543 }
1544
1545 int do_xfrm(int argc, char **argv)
1546 {
1547 memset(&filter, 0, sizeof(filter));
1548
1549 if (argc < 1)
1550 usage();
1551
1552 if (matches(*argv, "state") == 0 ||
1553 matches(*argv, "sa") == 0)
1554 return do_xfrm_state(argc-1, argv+1);
1555 else if (matches(*argv, "policy") == 0)
1556 return do_xfrm_policy(argc-1, argv+1);
1557 else if (matches(*argv, "monitor") == 0)
1558 return do_xfrm_monitor(argc-1, argv+1);
1559 else if (matches(*argv, "help") == 0) {
1560 usage();
1561 fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv);
1562 exit(-1);
1563 }
1564 usage();
1565 }