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