]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipxfrm.c
32f560933a47767ef87d51fe970f025f303af299
[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 bool nokeys)
502 {
503 int keylen;
504 int i;
505
506 if (prefix)
507 fputs(prefix, fp);
508
509 fprintf(fp, "%s ", strxf_algotype(type));
510
511 if (len < sizeof(*algo)) {
512 fprintf(fp, "(ERROR truncated)");
513 goto fin;
514 }
515 len -= sizeof(*algo);
516
517 fprintf(fp, "%s ", algo->alg_name);
518
519 keylen = algo->alg_key_len / 8;
520 if (len < keylen) {
521 fprintf(fp, "(ERROR truncated)");
522 goto fin;
523 }
524
525 if (nokeys)
526 fprintf(fp, "<<Keys hidden>>");
527 else if (keylen > 0) {
528 fprintf(fp, "0x");
529 for (i = 0; i < keylen; i++)
530 fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]);
531
532 if (show_stats > 0)
533 fprintf(fp, " (%d bits)", algo->alg_key_len);
534 }
535
536 fin:
537 if (newline)
538 fprintf(fp, "%s", _SL_);
539 }
540
541 static inline void xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
542 FILE *fp, const char *prefix, bool nokeys)
543 {
544 return __xfrm_algo_print(algo, type, len, fp, prefix, 1, nokeys);
545 }
546
547 static void xfrm_aead_print(struct xfrm_algo_aead *algo, int len,
548 FILE *fp, const char *prefix, bool nokeys)
549 {
550 struct xfrm_algo *base_algo = alloca(sizeof(*base_algo) + algo->alg_key_len / 8);
551
552 memcpy(base_algo->alg_name, algo->alg_name, sizeof(base_algo->alg_name));
553 base_algo->alg_key_len = algo->alg_key_len;
554 memcpy(base_algo->alg_key, algo->alg_key, algo->alg_key_len / 8);
555
556 __xfrm_algo_print(base_algo, XFRMA_ALG_AEAD, len, fp, prefix, 0,
557 nokeys);
558
559 fprintf(fp, " %d", algo->alg_icv_len);
560
561 fprintf(fp, "%s", _SL_);
562 }
563
564 static void xfrm_auth_trunc_print(struct xfrm_algo_auth *algo, int len,
565 FILE *fp, const char *prefix, bool nokeys)
566 {
567 struct xfrm_algo *base_algo = alloca(sizeof(*base_algo) + algo->alg_key_len / 8);
568
569 memcpy(base_algo->alg_name, algo->alg_name, sizeof(base_algo->alg_name));
570 base_algo->alg_key_len = algo->alg_key_len;
571 memcpy(base_algo->alg_key, algo->alg_key, algo->alg_key_len / 8);
572
573 __xfrm_algo_print(base_algo, XFRMA_ALG_AUTH_TRUNC, len, fp, prefix, 0,
574 nokeys);
575
576 fprintf(fp, " %d", algo->alg_trunc_len);
577
578 fprintf(fp, "%s", _SL_);
579 }
580
581 static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int len,
582 FILE *fp, const char *prefix)
583 {
584 int ntmpls = len / sizeof(struct xfrm_user_tmpl);
585 int i;
586
587 if (ntmpls <= 0) {
588 if (prefix)
589 fputs(prefix, fp);
590 fprintf(fp, "(ERROR \"tmpl\" truncated)");
591 fprintf(fp, "%s", _SL_);
592 return;
593 }
594
595 for (i = 0; i < ntmpls; i++) {
596 struct xfrm_user_tmpl *tmpl = &tmpls[i];
597
598 if (prefix)
599 fputs(prefix, fp);
600
601 xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode,
602 tmpl->reqid, tmpl->family, 0, fp, prefix, "tmpl ");
603
604 if (show_stats > 0 || tmpl->optional) {
605 if (prefix)
606 fputs(prefix, fp);
607 fprintf(fp, "\t");
608 switch (tmpl->optional) {
609 case 0:
610 if (show_stats > 0)
611 fprintf(fp, "level required ");
612 break;
613 case 1:
614 fprintf(fp, "level use ");
615 break;
616 default:
617 fprintf(fp, "level %u ", tmpl->optional);
618 break;
619 }
620
621 if (show_stats > 0)
622 fprintf(fp, "share %s ", strxf_share(tmpl->share));
623
624 fprintf(fp, "%s", _SL_);
625 }
626
627 if (show_stats > 0) {
628 if (prefix)
629 fputs(prefix, fp);
630 fprintf(fp, "\t");
631 fprintf(fp, "%s-mask %s ",
632 strxf_algotype(XFRMA_ALG_CRYPT),
633 strxf_mask32(tmpl->ealgos));
634 fprintf(fp, "%s-mask %s ",
635 strxf_algotype(XFRMA_ALG_AUTH),
636 strxf_mask32(tmpl->aalgos));
637 fprintf(fp, "%s-mask %s",
638 strxf_algotype(XFRMA_ALG_COMP),
639 strxf_mask32(tmpl->calgos));
640
641 fprintf(fp, "%s", _SL_);
642 }
643 }
644 }
645
646 static void xfrm_output_mark_print(struct rtattr *tb[], FILE *fp)
647 {
648 __u32 output_mark = rta_getattr_u32(tb[XFRMA_OUTPUT_MARK]);
649
650 fprintf(fp, "output-mark 0x%x", output_mark);
651 }
652
653 int xfrm_parse_mark(struct xfrm_mark *mark, int *argcp, char ***argvp)
654 {
655 int argc = *argcp;
656 char **argv = *argvp;
657
658 NEXT_ARG();
659 if (get_u32(&mark->v, *argv, 0)) {
660 invarg("MARK value is invalid\n", *argv);
661 }
662 if (argc > 1)
663 NEXT_ARG();
664 else { /* last entry on parse line */
665 mark->m = 0xffffffff;
666 goto done;
667 }
668
669 if (strcmp(*argv, "mask") == 0) {
670 NEXT_ARG();
671 if (get_u32(&mark->m, *argv, 0)) {
672 invarg("MASK value is invalid\n", *argv);
673 }
674 } else {
675 mark->m = 0xffffffff;
676 PREV_ARG();
677 }
678
679 done:
680 *argcp = argc;
681 *argvp = argv;
682
683 return 0;
684 }
685
686 void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
687 FILE *fp, const char *prefix, bool nokeys)
688 {
689 if (tb[XFRMA_MARK]) {
690 struct rtattr *rta = tb[XFRMA_MARK];
691 struct xfrm_mark *m = RTA_DATA(rta);
692
693 fprintf(fp, "\tmark %#x/%#x ", m->v, m->m);
694
695 if (tb[XFRMA_OUTPUT_MARK])
696 xfrm_output_mark_print(tb, fp);
697 fprintf(fp, "%s", _SL_);
698 } else if (tb[XFRMA_OUTPUT_MARK]) {
699 fprintf(fp, "\t");
700
701 xfrm_output_mark_print(tb, fp);
702 fprintf(fp, "%s", _SL_);
703 }
704
705 if (tb[XFRMA_ALG_AUTH] && !tb[XFRMA_ALG_AUTH_TRUNC]) {
706 struct rtattr *rta = tb[XFRMA_ALG_AUTH];
707
708 xfrm_algo_print(RTA_DATA(rta), XFRMA_ALG_AUTH, RTA_PAYLOAD(rta),
709 fp, prefix, nokeys);
710 }
711
712 if (tb[XFRMA_ALG_AUTH_TRUNC]) {
713 struct rtattr *rta = tb[XFRMA_ALG_AUTH_TRUNC];
714
715 xfrm_auth_trunc_print(RTA_DATA(rta), RTA_PAYLOAD(rta), fp,
716 prefix, nokeys);
717 }
718
719 if (tb[XFRMA_ALG_AEAD]) {
720 struct rtattr *rta = tb[XFRMA_ALG_AEAD];
721
722 xfrm_aead_print(RTA_DATA(rta), RTA_PAYLOAD(rta), fp, prefix,
723 nokeys);
724 }
725
726 if (tb[XFRMA_ALG_CRYPT]) {
727 struct rtattr *rta = tb[XFRMA_ALG_CRYPT];
728
729 xfrm_algo_print(RTA_DATA(rta), XFRMA_ALG_CRYPT,
730 RTA_PAYLOAD(rta), fp, prefix, nokeys);
731 }
732
733 if (tb[XFRMA_ALG_COMP]) {
734 struct rtattr *rta = tb[XFRMA_ALG_COMP];
735
736 xfrm_algo_print(RTA_DATA(rta), XFRMA_ALG_COMP, RTA_PAYLOAD(rta),
737 fp, prefix, nokeys);
738 }
739
740 if (tb[XFRMA_ENCAP]) {
741 struct xfrm_encap_tmpl *e;
742
743 if (prefix)
744 fputs(prefix, fp);
745 fprintf(fp, "encap ");
746
747 if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) {
748 fprintf(fp, "(ERROR truncated)");
749 fprintf(fp, "%s", _SL_);
750 return;
751 }
752 e = RTA_DATA(tb[XFRMA_ENCAP]);
753
754 fprintf(fp, "type ");
755 switch (e->encap_type) {
756 case 1:
757 fprintf(fp, "espinudp-nonike ");
758 break;
759 case 2:
760 fprintf(fp, "espinudp ");
761 break;
762 default:
763 fprintf(fp, "%u ", e->encap_type);
764 break;
765 }
766 fprintf(fp, "sport %u ", ntohs(e->encap_sport));
767 fprintf(fp, "dport %u ", ntohs(e->encap_dport));
768
769 fprintf(fp, "addr %s",
770 rt_addr_n2a(family, sizeof(e->encap_oa), &e->encap_oa));
771 fprintf(fp, "%s", _SL_);
772 }
773
774 if (tb[XFRMA_TMPL]) {
775 struct rtattr *rta = tb[XFRMA_TMPL];
776
777 xfrm_tmpl_print(RTA_DATA(rta),
778 RTA_PAYLOAD(rta), fp, prefix);
779 }
780
781 if (tb[XFRMA_COADDR]) {
782 const xfrm_address_t *coa;
783
784 if (prefix)
785 fputs(prefix, fp);
786 fprintf(fp, "coa ");
787
788 coa = RTA_DATA(tb[XFRMA_COADDR]);
789 if (RTA_PAYLOAD(tb[XFRMA_COADDR]) < sizeof(*coa)) {
790 fprintf(fp, "(ERROR truncated)");
791 fprintf(fp, "%s", _SL_);
792 return;
793 }
794
795 fprintf(fp, "%s",
796 rt_addr_n2a(family, sizeof(*coa), coa));
797 fprintf(fp, "%s", _SL_);
798 }
799
800 if (tb[XFRMA_LASTUSED]) {
801 __u64 lastused;
802
803 if (prefix)
804 fputs(prefix, fp);
805 fprintf(fp, "lastused ");
806
807 if (RTA_PAYLOAD(tb[XFRMA_LASTUSED]) < sizeof(lastused)) {
808 fprintf(fp, "(ERROR truncated)");
809 fprintf(fp, "%s", _SL_);
810 return;
811 }
812
813 lastused = rta_getattr_u64(tb[XFRMA_LASTUSED]);
814
815 fprintf(fp, "%s", strxf_time(lastused));
816 fprintf(fp, "%s", _SL_);
817 }
818
819 if (tb[XFRMA_REPLAY_VAL]) {
820 struct xfrm_replay_state *replay;
821
822 if (prefix)
823 fputs(prefix, fp);
824 fprintf(fp, "anti-replay context: ");
825
826 if (RTA_PAYLOAD(tb[XFRMA_REPLAY_VAL]) < sizeof(*replay)) {
827 fprintf(fp, "(ERROR truncated)");
828 fprintf(fp, "%s", _SL_);
829 return;
830 }
831
832 replay = RTA_DATA(tb[XFRMA_REPLAY_VAL]);
833 fprintf(fp, "seq 0x%x, oseq 0x%x, bitmap 0x%08x",
834 replay->seq, replay->oseq, replay->bitmap);
835 fprintf(fp, "%s", _SL_);
836 }
837
838 if (tb[XFRMA_REPLAY_ESN_VAL]) {
839 struct xfrm_replay_state_esn *replay;
840 unsigned int i, j;
841
842 if (prefix)
843 fputs(prefix, fp);
844 fprintf(fp, "anti-replay esn context:");
845
846 if (RTA_PAYLOAD(tb[XFRMA_REPLAY_ESN_VAL]) < sizeof(*replay)) {
847 fprintf(fp, "(ERROR truncated)");
848 fprintf(fp, "%s", _SL_);
849 return;
850 }
851 fprintf(fp, "%s", _SL_);
852
853 replay = RTA_DATA(tb[XFRMA_REPLAY_ESN_VAL]);
854 if (prefix)
855 fputs(prefix, fp);
856 fprintf(fp, " seq-hi 0x%x, seq 0x%x, oseq-hi 0x%0x, oseq 0x%0x",
857 replay->seq_hi, replay->seq, replay->oseq_hi,
858 replay->oseq);
859 fprintf(fp, "%s", _SL_);
860 if (prefix)
861 fputs(prefix, fp);
862 fprintf(fp, " replay_window %u, bitmap-length %u",
863 replay->replay_window, replay->bmp_len);
864 for (i = replay->bmp_len, j = 0; i; i--) {
865 if (j++ % 8 == 0) {
866 fprintf(fp, "%s", _SL_);
867 if (prefix)
868 fputs(prefix, fp);
869 fprintf(fp, " ");
870 }
871 fprintf(fp, "%08x ", replay->bmp[i - 1]);
872 }
873 fprintf(fp, "%s", _SL_);
874 }
875 if (tb[XFRMA_OFFLOAD_DEV]) {
876 struct xfrm_user_offload *xuo;
877
878 if (prefix)
879 fputs(prefix, fp);
880 fprintf(fp, "crypto offload parameters: ");
881
882 if (RTA_PAYLOAD(tb[XFRMA_OFFLOAD_DEV]) < sizeof(*xuo)) {
883 fprintf(fp, "(ERROR truncated)");
884 fprintf(fp, "%s", _SL_);
885 return;
886 }
887
888 xuo = (struct xfrm_user_offload *)
889 RTA_DATA(tb[XFRMA_OFFLOAD_DEV]);
890 fprintf(fp, "dev %s dir %s", ll_index_to_name(xuo->ifindex),
891 (xuo->flags & XFRM_OFFLOAD_INBOUND) ? "in" : "out");
892 fprintf(fp, "%s", _SL_);
893 }
894 if (tb[XFRMA_IF_ID]) {
895 __u32 if_id = rta_getattr_u32(tb[XFRMA_IF_ID]);
896
897 if (prefix)
898 fputs(prefix, fp);
899 fprintf(fp, "if_id %#x", if_id);
900 fprintf(fp, "%s", _SL_);
901 }
902 }
903
904 static int xfrm_selector_iszero(struct xfrm_selector *s)
905 {
906 struct xfrm_selector s0 = {};
907
908 return (memcmp(&s0, s, sizeof(s0)) == 0);
909 }
910
911 void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo,
912 struct rtattr *tb[], FILE *fp, const char *prefix,
913 const char *title, bool nokeys)
914 {
915 char buf[STRBUF_SIZE] = {};
916 int force_spi = xfrm_xfrmproto_is_ipsec(xsinfo->id.proto);
917
918 xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode,
919 xsinfo->reqid, xsinfo->family, force_spi, fp,
920 prefix, title);
921
922 if (prefix)
923 strlcat(buf, prefix, sizeof(buf));
924 strlcat(buf, "\t", sizeof(buf));
925
926 fputs(buf, fp);
927 fprintf(fp, "replay-window %u ", xsinfo->replay_window);
928 if (show_stats > 0)
929 fprintf(fp, "seq 0x%08u ", xsinfo->seq);
930 if (show_stats > 0 || xsinfo->flags) {
931 __u8 flags = xsinfo->flags;
932
933 fprintf(fp, "flag ");
934 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOECN, "noecn");
935 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp");
936 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOPMTUDISC, "nopmtudisc");
937 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_WILDRECV, "wildrecv");
938 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ICMP, "icmp");
939 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_AF_UNSPEC, "af-unspec");
940 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ALIGN4, "align4");
941 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ESN, "esn");
942 if (flags)
943 fprintf(fp, "%x", flags);
944 }
945 if (show_stats > 0 && tb[XFRMA_SA_EXTRA_FLAGS]) {
946 __u32 extra_flags = rta_getattr_u32(tb[XFRMA_SA_EXTRA_FLAGS]);
947
948 fprintf(fp, "extra_flag ");
949 XFRM_FLAG_PRINT(fp, extra_flags,
950 XFRM_SA_XFLAG_DONT_ENCAP_DSCP,
951 "dont-encap-dscp");
952 if (extra_flags)
953 fprintf(fp, "%x", extra_flags);
954 }
955 if (show_stats > 0)
956 fprintf(fp, " (0x%s)", strxf_mask8(xsinfo->flags));
957 fprintf(fp, "%s", _SL_);
958
959 xfrm_xfrma_print(tb, xsinfo->family, fp, buf, nokeys);
960
961 if (!xfrm_selector_iszero(&xsinfo->sel)) {
962 char sbuf[STRBUF_SIZE];
963
964 memcpy(sbuf, buf, sizeof(sbuf));
965 strlcat(sbuf, "sel ", sizeof(sbuf));
966
967 xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, sbuf);
968 }
969
970 if (show_stats > 0) {
971 xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, buf);
972 xfrm_stats_print(&xsinfo->stats, fp, buf);
973 }
974
975 if (tb[XFRMA_SEC_CTX]) {
976 struct xfrm_user_sec_ctx *sctx;
977
978 fprintf(fp, "\tsecurity context ");
979
980 if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
981 fprintf(fp, "(ERROR truncated)");
982
983 sctx = RTA_DATA(tb[XFRMA_SEC_CTX]);
984
985 fprintf(fp, "%s %s", (char *)(sctx + 1), _SL_);
986 }
987
988 }
989
990 void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
991 struct rtattr *tb[], FILE *fp, const char *prefix,
992 const char *title)
993 {
994 char buf[STRBUF_SIZE] = {};
995
996 xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title);
997
998 if (tb[XFRMA_SEC_CTX]) {
999 struct xfrm_user_sec_ctx *sctx;
1000
1001 fprintf(fp, "\tsecurity context ");
1002
1003 if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
1004 fprintf(fp, "(ERROR truncated)");
1005
1006 sctx = RTA_DATA(tb[XFRMA_SEC_CTX]);
1007
1008 fprintf(fp, "%s ", (char *)(sctx + 1));
1009 fprintf(fp, "%s", _SL_);
1010 }
1011
1012 if (prefix)
1013 strlcat(buf, prefix, sizeof(buf));
1014 strlcat(buf, "\t", sizeof(buf));
1015
1016 fputs(buf, fp);
1017 if (xpinfo->dir >= XFRM_POLICY_MAX) {
1018 xpinfo->dir -= XFRM_POLICY_MAX;
1019 fprintf(fp, "socket ");
1020 } else
1021 fprintf(fp, "dir ");
1022
1023 switch (xpinfo->dir) {
1024 case XFRM_POLICY_IN:
1025 fprintf(fp, "in");
1026 break;
1027 case XFRM_POLICY_OUT:
1028 fprintf(fp, "out");
1029 break;
1030 case XFRM_POLICY_FWD:
1031 fprintf(fp, "fwd");
1032 break;
1033 default:
1034 fprintf(fp, "%u", xpinfo->dir);
1035 break;
1036 }
1037 fprintf(fp, " ");
1038
1039 switch (xpinfo->action) {
1040 case XFRM_POLICY_ALLOW:
1041 if (show_stats > 0)
1042 fprintf(fp, "action allow ");
1043 break;
1044 case XFRM_POLICY_BLOCK:
1045 fprintf(fp, "action block ");
1046 break;
1047 default:
1048 fprintf(fp, "action %u ", xpinfo->action);
1049 break;
1050 }
1051
1052 if (show_stats)
1053 fprintf(fp, "index %u ", xpinfo->index);
1054 fprintf(fp, "priority %u ", xpinfo->priority);
1055
1056 if (tb[XFRMA_POLICY_TYPE]) {
1057 struct xfrm_userpolicy_type *upt;
1058
1059 fprintf(fp, "ptype ");
1060
1061 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
1062 fprintf(fp, "(ERROR truncated)");
1063
1064 upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]);
1065 fprintf(fp, "%s ", strxf_ptype(upt->type));
1066 }
1067
1068 if (show_stats > 0)
1069 fprintf(fp, "share %s ", strxf_share(xpinfo->share));
1070
1071 if (show_stats > 0 || xpinfo->flags) {
1072 __u8 flags = xpinfo->flags;
1073
1074 fprintf(fp, "flag ");
1075 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_LOCALOK, "localok");
1076 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_ICMP, "icmp");
1077 if (flags)
1078 fprintf(fp, "%x", flags);
1079 }
1080 if (show_stats > 0)
1081 fprintf(fp, " (0x%s)", strxf_mask8(xpinfo->flags));
1082 fprintf(fp, "%s", _SL_);
1083
1084 if (show_stats > 0)
1085 xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf);
1086
1087 xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf, false);
1088 }
1089
1090 int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
1091 int loose, int *argcp, char ***argvp)
1092 {
1093 int argc = *argcp;
1094 char **argv = *argvp;
1095 inet_prefix dst = {};
1096 inet_prefix src = {};
1097
1098 while (1) {
1099 if (strcmp(*argv, "src") == 0) {
1100 NEXT_ARG();
1101
1102 get_prefix(&src, *argv, preferred_family);
1103 if (src.family == AF_UNSPEC)
1104 invarg("value after \"src\" has an unrecognized address family", *argv);
1105 if (family)
1106 *family = src.family;
1107
1108 memcpy(saddr, &src.data, sizeof(*saddr));
1109
1110 filter.id_src_mask = src.bitlen;
1111
1112 } else if (strcmp(*argv, "dst") == 0) {
1113 NEXT_ARG();
1114
1115 get_prefix(&dst, *argv, preferred_family);
1116 if (dst.family == AF_UNSPEC)
1117 invarg("value after \"dst\" has an unrecognized address family", *argv);
1118 if (family)
1119 *family = dst.family;
1120
1121 memcpy(&id->daddr, &dst.data, sizeof(id->daddr));
1122
1123 filter.id_dst_mask = dst.bitlen;
1124
1125 } else if (strcmp(*argv, "proto") == 0) {
1126 int ret;
1127
1128 NEXT_ARG();
1129
1130 ret = xfrm_xfrmproto_getbyname(*argv);
1131 if (ret < 0)
1132 invarg("XFRM-PROTO value is invalid", *argv);
1133
1134 id->proto = (__u8)ret;
1135
1136 filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
1137
1138 } else if (strcmp(*argv, "spi") == 0) {
1139 NEXT_ARG();
1140 if (get_be32(&id->spi, *argv, 0))
1141 invarg("SPI value is invalid", *argv);
1142
1143 filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
1144
1145 } else {
1146 PREV_ARG(); /* back track */
1147 break;
1148 }
1149
1150 if (!NEXT_ARG_OK())
1151 break;
1152 NEXT_ARG();
1153 }
1154
1155 if (src.family && dst.family && (src.family != dst.family))
1156 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv);
1157
1158 if (id->spi && id->proto) {
1159 if (xfrm_xfrmproto_is_ro(id->proto)) {
1160 fprintf(stderr, "\"spi\" is invalid with XFRM-PROTO value \"%s\"\n",
1161 strxf_xfrmproto(id->proto));
1162 exit(1);
1163 } else if (id->proto == IPPROTO_COMP && ntohl(id->spi) >= 0x10000) {
1164 fprintf(stderr, "SPI value is too large with XFRM-PROTO value \"%s\"\n",
1165 strxf_xfrmproto(id->proto));
1166 exit(1);
1167 }
1168 }
1169
1170 if (loose == 0 && id->proto == 0)
1171 missarg("XFRM-PROTO");
1172 if (argc == *argcp)
1173 missarg("ID");
1174
1175 *argcp = argc;
1176 *argvp = argv;
1177
1178 return 0;
1179 }
1180
1181 int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp)
1182 {
1183 int argc = *argcp;
1184 char **argv = *argvp;
1185
1186 if (matches(*argv, "transport") == 0)
1187 *mode = XFRM_MODE_TRANSPORT;
1188 else if (matches(*argv, "tunnel") == 0)
1189 *mode = XFRM_MODE_TUNNEL;
1190 else if (matches(*argv, "ro") == 0)
1191 *mode = XFRM_MODE_ROUTEOPTIMIZATION;
1192 else if (matches(*argv, "in_trigger") == 0)
1193 *mode = XFRM_MODE_IN_TRIGGER;
1194 else if (matches(*argv, "beet") == 0)
1195 *mode = XFRM_MODE_BEET;
1196 else
1197 invarg("MODE value is invalid", *argv);
1198
1199 *argcp = argc;
1200 *argvp = argv;
1201
1202 return 0;
1203 }
1204
1205 int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp)
1206 {
1207 int argc = *argcp;
1208 char **argv = *argvp;
1209
1210 if (strcmp(*argv, "espinudp-nonike") == 0)
1211 *type = 1;
1212 else if (strcmp(*argv, "espinudp") == 0)
1213 *type = 2;
1214 else
1215 invarg("ENCAP-TYPE value is invalid", *argv);
1216
1217 *argcp = argc;
1218 *argvp = argv;
1219
1220 return 0;
1221 }
1222
1223 /* NOTE: reqid is used by host-byte order */
1224 int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp)
1225 {
1226 int argc = *argcp;
1227 char **argv = *argvp;
1228
1229 if (get_u32(reqid, *argv, 0))
1230 invarg("REQID value is invalid", *argv);
1231
1232 *argcp = argc;
1233 *argvp = argv;
1234
1235 return 0;
1236 }
1237
1238 static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
1239 int *argcp, char ***argvp)
1240 {
1241 int argc = *argcp;
1242 char **argv = *argvp;
1243 char *sportp = NULL;
1244 char *dportp = NULL;
1245 char *typep = NULL;
1246 char *codep = NULL;
1247 char *grekey = NULL;
1248
1249 while (1) {
1250 if (strcmp(*argv, "proto") == 0) {
1251 __u8 upspec;
1252
1253 NEXT_ARG();
1254
1255 if (strcmp(*argv, "any") == 0)
1256 upspec = 0;
1257 else {
1258 struct protoent *pp;
1259
1260 pp = getprotobyname(*argv);
1261 if (pp)
1262 upspec = pp->p_proto;
1263 else {
1264 if (get_u8(&upspec, *argv, 0))
1265 invarg("PROTO value is invalid", *argv);
1266 }
1267 }
1268 sel->proto = upspec;
1269
1270 filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL;
1271
1272 } else if (strcmp(*argv, "sport") == 0) {
1273 sportp = *argv;
1274
1275 NEXT_ARG();
1276
1277 if (get_be16(&sel->sport, *argv, 0))
1278 invarg("value after \"sport\" is invalid", *argv);
1279 if (sel->sport)
1280 sel->sport_mask = ~((__u16)0);
1281
1282 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1283
1284 } else if (strcmp(*argv, "dport") == 0) {
1285 dportp = *argv;
1286
1287 NEXT_ARG();
1288
1289 if (get_be16(&sel->dport, *argv, 0))
1290 invarg("value after \"dport\" is invalid", *argv);
1291 if (sel->dport)
1292 sel->dport_mask = ~((__u16)0);
1293
1294 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1295
1296 } else if (strcmp(*argv, "type") == 0) {
1297 typep = *argv;
1298
1299 NEXT_ARG();
1300
1301 if (get_u16(&sel->sport, *argv, 0) ||
1302 (sel->sport & ~((__u16)0xff)))
1303 invarg("value after \"type\" is invalid", *argv);
1304 sel->sport = htons(sel->sport);
1305 sel->sport_mask = ~((__u16)0);
1306
1307 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1308
1309
1310 } else if (strcmp(*argv, "code") == 0) {
1311 codep = *argv;
1312
1313 NEXT_ARG();
1314
1315 if (get_u16(&sel->dport, *argv, 0) ||
1316 (sel->dport & ~((__u16)0xff)))
1317 invarg("value after \"code\" is invalid", *argv);
1318 sel->dport = htons(sel->dport);
1319 sel->dport_mask = ~((__u16)0);
1320
1321 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1322
1323 } else if (strcmp(*argv, "key") == 0) {
1324 unsigned int uval;
1325
1326 grekey = *argv;
1327
1328 NEXT_ARG();
1329
1330 if (strchr(*argv, '.'))
1331 uval = htonl(get_addr32(*argv));
1332 else {
1333 if (get_unsigned(&uval, *argv, 0) < 0) {
1334 fprintf(stderr, "value after \"key\" is invalid\n");
1335 exit(-1);
1336 }
1337 }
1338
1339 sel->sport = htons(uval >> 16);
1340 sel->dport = htons(uval & 0xffff);
1341 sel->sport_mask = ~((__u16)0);
1342 sel->dport_mask = ~((__u16)0);
1343
1344 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1345
1346 } else {
1347 PREV_ARG(); /* back track */
1348 break;
1349 }
1350
1351 if (!NEXT_ARG_OK())
1352 break;
1353 NEXT_ARG();
1354 }
1355 if (argc == *argcp)
1356 missarg("UPSPEC");
1357 if (sportp || dportp) {
1358 switch (sel->proto) {
1359 case IPPROTO_TCP:
1360 case IPPROTO_UDP:
1361 case IPPROTO_SCTP:
1362 case IPPROTO_DCCP:
1363 case IPPROTO_IP: /* to allow shared SA for different protocols */
1364 break;
1365 default:
1366 fprintf(stderr, "\"sport\" and \"dport\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto));
1367 exit(1);
1368 }
1369 }
1370 if (typep || codep) {
1371 switch (sel->proto) {
1372 case IPPROTO_ICMP:
1373 case IPPROTO_ICMPV6:
1374 case IPPROTO_MH:
1375 break;
1376 default:
1377 fprintf(stderr, "\"type\" and \"code\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto));
1378 exit(1);
1379 }
1380 }
1381 if (grekey) {
1382 switch (sel->proto) {
1383 case IPPROTO_GRE:
1384 break;
1385 default:
1386 fprintf(stderr, "\"key\" is invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto));
1387 exit(1);
1388 }
1389 }
1390
1391 *argcp = argc;
1392 *argvp = argv;
1393
1394 return 0;
1395 }
1396
1397 int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
1398 {
1399 int argc = *argcp;
1400 char **argv = *argvp;
1401 inet_prefix dst = {};
1402 inet_prefix src = {};
1403 char *upspecp = NULL;
1404
1405 while (1) {
1406 if (strcmp(*argv, "src") == 0) {
1407 NEXT_ARG();
1408
1409 get_prefix(&src, *argv, preferred_family);
1410 if (src.family == AF_UNSPEC)
1411 invarg("value after \"src\" has an unrecognized address family", *argv);
1412 sel->family = src.family;
1413
1414 memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
1415 sel->prefixlen_s = src.bitlen;
1416
1417 filter.sel_src_mask = src.bitlen;
1418
1419 } else if (strcmp(*argv, "dst") == 0) {
1420 NEXT_ARG();
1421
1422 get_prefix(&dst, *argv, preferred_family);
1423 if (dst.family == AF_UNSPEC)
1424 invarg("value after \"dst\" has an unrecognized address family", *argv);
1425 sel->family = dst.family;
1426
1427 memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
1428 sel->prefixlen_d = dst.bitlen;
1429
1430 filter.sel_dst_mask = dst.bitlen;
1431
1432 } else if (strcmp(*argv, "dev") == 0) {
1433 int ifindex;
1434
1435 NEXT_ARG();
1436
1437 if (strcmp(*argv, "none") == 0)
1438 ifindex = 0;
1439 else {
1440 ifindex = ll_name_to_index(*argv);
1441 if (ifindex <= 0)
1442 invarg("DEV value is invalid", *argv);
1443 }
1444 sel->ifindex = ifindex;
1445
1446 filter.sel_dev_mask = XFRM_FILTER_MASK_FULL;
1447
1448 } else {
1449 if (upspecp) {
1450 PREV_ARG(); /* back track */
1451 break;
1452 } else {
1453 upspecp = *argv;
1454 xfrm_selector_upspec_parse(sel, &argc, &argv);
1455 }
1456 }
1457
1458 if (!NEXT_ARG_OK())
1459 break;
1460
1461 NEXT_ARG();
1462 }
1463
1464 if (src.family && dst.family && (src.family != dst.family))
1465 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv);
1466
1467 if (argc == *argcp)
1468 missarg("SELECTOR");
1469
1470 *argcp = argc;
1471 *argvp = argv;
1472
1473 return 0;
1474 }
1475
1476 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft,
1477 int *argcp, char ***argvp)
1478 {
1479 int argc = *argcp;
1480 char **argv = *argvp;
1481 int ret;
1482
1483 if (strcmp(*argv, "time-soft") == 0) {
1484 NEXT_ARG();
1485 ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
1486 if (ret)
1487 invarg("value after \"time-soft\" is invalid", *argv);
1488 } else if (strcmp(*argv, "time-hard") == 0) {
1489 NEXT_ARG();
1490 ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
1491 if (ret)
1492 invarg("value after \"time-hard\" is invalid", *argv);
1493 } else if (strcmp(*argv, "time-use-soft") == 0) {
1494 NEXT_ARG();
1495 ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
1496 if (ret)
1497 invarg("value after \"time-use-soft\" is invalid", *argv);
1498 } else if (strcmp(*argv, "time-use-hard") == 0) {
1499 NEXT_ARG();
1500 ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
1501 if (ret)
1502 invarg("value after \"time-use-hard\" is invalid", *argv);
1503 } else if (strcmp(*argv, "byte-soft") == 0) {
1504 NEXT_ARG();
1505 ret = get_u64(&lft->soft_byte_limit, *argv, 0);
1506 if (ret)
1507 invarg("value after \"byte-soft\" is invalid", *argv);
1508 } else if (strcmp(*argv, "byte-hard") == 0) {
1509 NEXT_ARG();
1510 ret = get_u64(&lft->hard_byte_limit, *argv, 0);
1511 if (ret)
1512 invarg("value after \"byte-hard\" is invalid", *argv);
1513 } else if (strcmp(*argv, "packet-soft") == 0) {
1514 NEXT_ARG();
1515 ret = get_u64(&lft->soft_packet_limit, *argv, 0);
1516 if (ret)
1517 invarg("value after \"packet-soft\" is invalid", *argv);
1518 } else if (strcmp(*argv, "packet-hard") == 0) {
1519 NEXT_ARG();
1520 ret = get_u64(&lft->hard_packet_limit, *argv, 0);
1521 if (ret)
1522 invarg("value after \"packet-hard\" is invalid", *argv);
1523 } else
1524 invarg("LIMIT value is invalid", *argv);
1525
1526 *argcp = argc;
1527 *argvp = argv;
1528
1529 return 0;
1530 }
1531
1532 int do_xfrm(int argc, char **argv)
1533 {
1534 memset(&filter, 0, sizeof(filter));
1535
1536 if (argc < 1)
1537 usage();
1538
1539 if (matches(*argv, "state") == 0 ||
1540 matches(*argv, "sa") == 0)
1541 return do_xfrm_state(argc-1, argv+1);
1542 else if (matches(*argv, "policy") == 0)
1543 return do_xfrm_policy(argc-1, argv+1);
1544 else if (matches(*argv, "monitor") == 0)
1545 return do_xfrm_monitor(argc-1, argv+1);
1546 else if (matches(*argv, "help") == 0) {
1547 usage();
1548 fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv);
1549 exit(-1);
1550 }
1551 usage();
1552 }