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