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