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