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