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