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