blob: 2eb179661d9273d2f08e3005560653dce796f881 [file] [log] [blame]
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +00001/* $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 <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 <net/if.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
43struct xfrm_filter filter;
44
45static void usage(void) __attribute__((noreturn));
46
47static void usage(void)
48{
49 fprintf(stderr,
50 "Usage: ip xfrm XFRM_OBJECT { COMMAND | help }\n"
51 "where XFRM_OBJECT := { state | policy }\n");
52 exit(-1);
53}
54
55const char *strxf_flags(__u8 flags)
56{
57 static char str[16];
58 const int sn = sizeof(flags) * 8 - 1;
59 __u8 b;
60 int i = 0;
61
62 for (b = (1 << sn); b > 0; b >>= 1)
63 str[i++] = ((b & flags) ? '1' : '0');
64 str[i] = '\0';
65
66 return str;
67}
68
69const char *strxf_share(__u8 share)
70{
71 static char str[32];
72
73 switch (share) {
74 case XFRM_SHARE_ANY:
75 strcpy(str, "any");
76 break;
77 case XFRM_SHARE_SESSION:
78 strcpy(str, "session");
79 break;
80 case XFRM_SHARE_USER:
81 strcpy(str, "user");
82 break;
83 case XFRM_SHARE_UNIQUE:
84 strcpy(str, "unique");
85 break;
86 default:
87 sprintf(str, "unknown-share(%d)", share);
88 break;
89 }
90
91 return str;
92}
93
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +000094const char *strxf_proto(__u8 proto)
95{
96 static char buf[32];
97 struct protoent *pp;
98 const char *p;
99
100 pp = getprotobynumber(proto);
101 if (pp)
102 p = pp->p_name;
103 else {
104 sprintf(buf, "%d", proto);
105 p = buf;
106 }
107
108 return p;
109}
110
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000111void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id,
112 __u8 mode, __u32 reqid, __u16 family, FILE *fp,
113 const char *prefix)
114{
115 char abuf[256];
116 __u32 spi;
117 struct protoent *pp;
118 char pbuf[32];
119 char *p;
120
121 if (prefix)
122 fprintf(fp, prefix);
123
124 memset(abuf, '\0', sizeof(abuf));
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000125 fprintf(fp, "src %s ", rt_addr_n2a(family, sizeof(*saddr),
126 saddr, abuf, sizeof(abuf)));
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000127 memset(abuf, '\0', sizeof(abuf));
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000128 fprintf(fp, "dst %s\n", rt_addr_n2a(family, sizeof(id->daddr),
129 &id->daddr, abuf, sizeof(abuf)));
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000130
131 if (prefix)
132 fprintf(fp, prefix);
133 fprintf(fp, "\t");
134
135 pp = getprotobynumber(id->proto);
136 if (pp)
137 p = pp->p_name;
138 else {
139 sprintf(pbuf, "%d", id->proto);
140 p = pbuf;
141 }
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000142 fprintf(fp, "proto %s ", p);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000143
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000144 spi = ntohl(id->spi);
145 fprintf(fp, "spi %u", spi);
146 if (show_stats > 0)
147 fprintf(fp, "(0x%08x)", spi);
148 fprintf(fp, " ");
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000149
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000150 fprintf(fp, "reqid %u", reqid);
151 if (show_stats > 0)
152 fprintf(fp, "(0x%08x)", reqid);
153 fprintf(fp, " ");
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000154
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000155 fprintf(fp, "mode %s\n", (mode ? "tunnel" : "transport"));
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000156}
157
158static const char *strxf_limit(__u64 limit)
159{
160 static char str[32];
161 if (limit == XFRM_INF)
162 strcpy(str, "(INF)");
163 else
164 sprintf(str, "%llu", limit);
165
166 return str;
167}
168
169void xfrm_stats_print(struct xfrm_stats *s, FILE *fp, const char *prefix)
170{
171 if (prefix)
172 fprintf(fp, prefix);
173 fprintf(fp, "stats:\n");
174
175 if (prefix)
176 fprintf(fp, prefix);
177 fprintf(fp, " ");
178 fprintf(fp, "replay-window %d ", s->replay_window);
179 fprintf(fp, "replay %d ", s->replay);
180 fprintf(fp, "failed %d", s->integrity_failed);
181 fprintf(fp, "\n");
182}
183
184static const char *strxf_time(__u64 time)
185{
186 static char str[32];
187 struct tm *tp;
188 time_t t;
189
190 if (time == 0) {
191 strcpy(str, "(undefined)");
192 } else {
193 /* XXX: treat time in the same manner of xfrm_{user,state}.c */
194 t = (long)time;
195 tp = localtime(&t);
196
197 sprintf(str, "%04d/%02d/%02d %02d:%02d:%02d",
198 tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday,
199 tp->tm_hour, tp->tm_min, tp->tm_sec);
200 }
201
202 return str;
203}
204
205void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg,
206 struct xfrm_lifetime_cur *cur,
207 FILE *fp, const char *prefix)
208{
209 if (cfg) {
210 if (prefix)
211 fprintf(fp, prefix);
212 fprintf(fp, "lifetime config:\n");
213
214 if (prefix)
215 fprintf(fp, prefix);
216 fprintf(fp, " ");
217 fprintf(fp, "limit: ");
218 fprintf(fp, "soft ");
219 fprintf(fp, strxf_limit(cfg->soft_byte_limit));
220 fprintf(fp, "(bytes), hard ");
221 fprintf(fp, strxf_limit(cfg->hard_byte_limit));
222 fprintf(fp, "(bytes)\n");
223
224 if (prefix)
225 fprintf(fp, prefix);
226 fprintf(fp, " ");
227 fprintf(fp, "limit: ");
228 fprintf(fp, "soft ");
229 fprintf(fp, strxf_limit(cfg->soft_packet_limit));
230 fprintf(fp, "(packets), hard ");
231 fprintf(fp, strxf_limit(cfg->hard_packet_limit));
232 fprintf(fp, "(packets)\n");
233
234 if (prefix)
235 fprintf(fp, prefix);
236 fprintf(fp, " ");
237 fprintf(fp, "expire add: ");
238 fprintf(fp, "soft ");
239 fprintf(fp, "%llu", cfg->soft_add_expires_seconds);
240 fprintf(fp, "(sec), hard ");
241 fprintf(fp, "%llu", cfg->hard_add_expires_seconds);
242 fprintf(fp, "(sec)\n");
243
244 if (prefix)
245 fprintf(fp, prefix);
246 fprintf(fp, " ");
247 fprintf(fp, "expire use: ");
248 fprintf(fp, "soft ");
249 fprintf(fp, "%llu", cfg->soft_use_expires_seconds);
250 fprintf(fp, "(sec), hard ");
251 fprintf(fp, "%llu", cfg->hard_use_expires_seconds);
252 fprintf(fp, "(sec)\n");
253 }
254 if (cur) {
255 if (prefix)
256 fprintf(fp, prefix);
257 fprintf(fp, "lifetime current:\n");
258
259 if (prefix)
260 fprintf(fp, prefix);
261 fprintf(fp, " ");
262 fprintf(fp, "%llu(bytes), ", cur->bytes);
263 fprintf(fp, "%llu(packets)\n", cur->packets);
264 if (prefix)
265 fprintf(fp, prefix);
266 fprintf(fp, " ");
267 fprintf(fp, "add %s ", strxf_time(cur->add_time));
268 fprintf(fp, "use %s", strxf_time(cur->use_time));
269 fprintf(fp, "\n");
270 }
271}
272
273void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
274 FILE *fp, const char *prefix)
275{
276 char abuf[256];
277 __u16 f;
278
279 f = sel->family;
280 if (f == AF_UNSPEC)
281 f = family;
282 if (f == AF_UNSPEC)
283 f = preferred_family;
284
285 if (prefix)
286 fprintf(fp, prefix);
287
288 memset(abuf, '\0', sizeof(abuf));
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000289 fprintf(fp, "src %s/%d ", rt_addr_n2a(f, sizeof(sel->saddr),
290 &sel->saddr, abuf, sizeof(abuf)),
291 sel->prefixlen_s);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000292
293 memset(abuf, '\0', sizeof(abuf));
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000294 fprintf(fp, "dst %s/%d", rt_addr_n2a(f, sizeof(sel->daddr),
295 &sel->daddr, abuf, sizeof(abuf)),
296 sel->prefixlen_d);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000297
298 fprintf(fp, "\n");
299
300 if (prefix)
301 fprintf(fp, prefix);
302 fprintf(fp, "\t");
303
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000304 fprintf(fp, "upspec proto %u ", sel->proto);
305 fprintf(fp, "sport %u dport %u ", sel->sport, sel->dport);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000306
307 if (sel->ifindex > 0) {
308 char buf[IF_NAMESIZE];
309
310 memset(buf, '\0', sizeof(buf));
311 if_indextoname(sel->ifindex, buf);
312 fprintf(fp, "dev %s ", buf);
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000313 }
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000314
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000315 if (show_stats > 0)
316 fprintf(fp, "uid %u", sel->user);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000317 fprintf(fp, "\n");
318}
319
320static void xfrm_algo_print(struct xfrm_algo *algo, FILE *fp,
321 const char *prefix)
322{
323 int len;
324 int i;
325
326 if (prefix)
327 fprintf(fp, prefix);
328
329 fprintf(fp, "%s", algo->alg_name);
330
331 len = algo->alg_key_len / 8;
332 for (i = 0; i < len; i ++) {
333 if (i % 4 == 0)
334 fprintf(fp, " ");
335 fprintf(fp, "%x", algo->alg_key[i]);
336 }
337
338 fprintf(fp, "\n");
339}
340
341static const char *strxf_mask(__u32 mask)
342{
343 static char str[128];
344 const int sn = sizeof(mask) * 8 - 1;
345 __u32 b;
346 int finish = 0;
347 int broken = 0;
348 int i = 0;
349
350 for (b = (1 << sn); b > 0; b >>= 1) {
351 if ((b & mask) == 0) {
352 if (!finish)
353 finish = 1;
354 } else {
355 if (!finish)
356 i ++;
357 else {
358 broken = 1;
359 break;
360 }
361 }
362 }
363
364 if (!broken)
365 sprintf(str, "%u", i);
366 else
367 sprintf(str, "broken(%u)", mask);
368
369 return str;
370}
371
372static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int ntmpls,
373 __u16 family, FILE *fp, const char *prefix)
374{
375 char buf[32];
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000376 int i;
377
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000378 for (i = 0; i < ntmpls; i++) {
379 struct xfrm_user_tmpl *tmpl = &tmpls[i];
380
381 if (prefix)
382 fprintf(fp, prefix);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000383
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000384 fprintf(fp, "tmpl");
385 xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode,
386 tmpl->reqid, family, fp, prefix);
387
388 fprintf(fp, prefix);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000389 fprintf(fp, "\t");
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000390 fprintf(fp, "level ");
391 switch (tmpl->optional) {
392 case 0:
393 fprintf(fp, "required");
394 break;
395 case 1:
396 fprintf(fp, "use");
397 break;
398 default:
399 fprintf(fp, "%d", tmpl->optional);
400 break;
401 }
402 fprintf(fp, " ");
403
404 if (show_stats > 0) {
405 fprintf(fp, "share %s ", strxf_share(tmpl->share));
406 fprintf(fp, "algo-mask:");
407 fprintf(fp, "E=%s, ", strxf_mask(tmpl->ealgos));
408 fprintf(fp, "A=%s, ", strxf_mask(tmpl->aalgos));
409 fprintf(fp, "C=%s", strxf_mask(tmpl->calgos));
410 }
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000411 fprintf(fp, "\n");
412 }
413}
414
415void xfrm_xfrma_print(struct rtattr *tb[], int ntb, __u16 family,
416 FILE *fp, const char *prefix)
417{
418 int i;
419
420 for (i = 0; i < ntb; i++) {
421 __u16 type = tb[i]->rta_type;
422 void *data = RTA_DATA(tb[i]);
423
424 switch (type) {
425 case XFRMA_ALG_CRYPT:
426 if (prefix)
427 fprintf(fp, prefix);
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000428 xfrm_algo_print((struct xfrm_algo *)data, fp, "algo E ");
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000429 break;
430 case XFRMA_ALG_AUTH:
431 if (prefix)
432 fprintf(fp, prefix);
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000433 xfrm_algo_print((struct xfrm_algo *)data, fp, "algo A ");
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000434 break;
435 case XFRMA_ALG_COMP:
436 if (prefix)
437 fprintf(fp, prefix);
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000438 xfrm_algo_print((struct xfrm_algo *)data, fp, "algo C ");
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000439 break;
440 case XFRMA_ENCAP:
441 if (prefix)
442 fprintf(fp, prefix);
443 /* XXX */
444 fprintf(fp, "encap: (not implemented yet!)\n");
445 break;
446 case XFRMA_TMPL:
447 {
448 int len = tb[i]->rta_len;
449 int ntmpls = len / sizeof(struct xfrm_user_tmpl);
450
451 xfrm_tmpl_print((struct xfrm_user_tmpl *)data,
452 ntmpls, family, fp, prefix);
453 break;
454 }
455 default:
456 if (prefix)
457 fprintf(fp, prefix);
458 fprintf(fp, "unknown rta_type: %u\n", type);
459 break;
460 }
461 }
462}
463
464int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
465 int *argcp, char ***argvp)
466{
467 int argc = *argcp;
468 char **argv = *argvp;
469 inet_prefix dst;
470 inet_prefix src;
471 __u8 proto = 0;
472
473 memset(&dst, 0, sizeof(dst));
474 memset(&src, 0, sizeof(src));
475
476 while (1) {
477 if (strcmp(*argv, "src") == 0) {
478 NEXT_ARG();
479
480 get_prefix(&src, *argv, preferred_family);
481 if (src.family == AF_UNSPEC)
482 invarg("\"SADDR\" address family is AF_UNSPEC", *argv);
483 if (family)
484 *family = src.family;
485
486 memcpy(saddr, &src.data, sizeof(*saddr));
487
488 filter.id_src_mask = src.bitlen;
489
490 } else if (strcmp(*argv, "dst") == 0) {
491 NEXT_ARG();
492
493 get_prefix(&dst, *argv, preferred_family);
494 if (dst.family == AF_UNSPEC)
495 invarg("\"DADDR\" address family is AF_UNSPEC", *argv);
496 if (family)
497 *family = dst.family;
498
499 memcpy(&id->daddr, &dst.data, sizeof(id->daddr));
500
501 filter.id_dst_mask = dst.bitlen;
502
503 } else if (strcmp(*argv, "proto") == 0) {
504 struct protoent *pp;
505
506 NEXT_ARG();
507
508 pp = getprotobyname(*argv);
509 if (pp)
510 proto = pp->p_proto;
511 else {
512 if (get_u8(&proto, *argv, 0))
513 invarg("\"PROTO\" is invalid", *argv);
514 }
515
516 switch (proto) {
517 case IPPROTO_ESP:
518 case IPPROTO_AH:
519 case IPPROTO_COMP:
520 id->proto = proto;
521 break;
522 default:
523 invarg("\"PROTO\" is unsuppored proto", *argv);
524 }
525
526 filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
527
528 } else if (strcmp(*argv, "spi") == 0) {
529 __u32 spi;
530
531 NEXT_ARG();
532 if (get_u32(&spi, *argv, 0))
533 invarg("\"SPI\" is invalid", *argv);
534
535 spi = htonl(spi);
536 id->spi = spi;
537
538 filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
539
540 } else {
541 PREV_ARG(); /* back track */
542 break;
543 }
544
545 if (!NEXT_ARG_OK())
546 break;
547 NEXT_ARG();
548 }
549
550 if (src.family && dst.family && (src.family != dst.family))
551 invarg("the same address family is required between \"SADDR\" and \"DADDR\"", *argv);
552 if (proto == 0)
553 missarg("PROTO");
554
555 if (argc == *argcp)
556 missarg("ID");
557
558 *argcp = argc;
559 *argvp = argv;
560
561 return 0;
562}
563
564int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp)
565{
566 int argc = *argcp;
567 char **argv = *argvp;
568
569 if (matches(*argv, "transport") == 0)
570 *mode = 0;
571 else if (matches(*argv, "tunnel") == 0)
572 *mode = 1;
573 else
574 invarg("\"MODE\" is invalid", *argv);
575
576 *argcp = argc;
577 *argvp = argv;
578
579 return 0;
580}
581
582/* NOTE: reqid is used by host-byte order */
583int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp)
584{
585 int argc = *argcp;
586 char **argv = *argvp;
587
588 if (get_u32(reqid, *argv, 0))
589 invarg("\"REQID\" is invalid", *argv);
590
591 *argcp = argc;
592 *argvp = argv;
593
594 return 0;
595}
596
597static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
598 int *argcp, char ***argvp)
599{
600 int argc = *argcp;
601 char **argv = *argvp;
602 __u8 upspec;
603
604 while (1) {
605 if (strcmp(*argv, "proto") == 0) {
606 NEXT_ARG();
607
608 if (strcmp(*argv, "any") == 0)
609 upspec = 0;
610 else {
611 struct protoent *pp;
612 pp = getprotobyname(*argv);
613 if (pp)
614 upspec = pp->p_proto;
615 else {
616 if (get_u8(&upspec, *argv, 0))
617 invarg("\"UPSPEC\" is invalid", *argv);
618 }
619 }
620 sel->proto = upspec;
621
622 filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL;
623
624 } else if (strcmp(*argv, "sport") == 0) {
625 NEXT_ARG();
626
627 if (get_u16(&sel->sport, *argv, 0))
628 invarg("\"PORT\" is invalid", *argv);
629 sel->sport = htons(sel->sport);
630 if (sel->sport)
631 sel->sport_mask = ~((__u16)0);
632
633 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
634
635 } else if (strcmp(*argv, "dport") == 0) {
636 NEXT_ARG();
637
638 if (get_u16(&sel->dport, *argv, 0))
639 invarg("\"PORT\" is invalid", *argv);
640 sel->dport = htons(sel->dport);
641 if (sel->dport)
642 sel->dport_mask = ~((__u16)0);
643
644 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
645
646 } else {
647 PREV_ARG(); /* back track */
648 break;
649 }
650
651 if (!NEXT_ARG_OK())
652 break;
653 NEXT_ARG();
654 }
655 if (argc == *argcp)
656 missarg("UPSPEC");
657
658 *argcp = argc;
659 *argvp = argv;
660
661 return 0;
662}
663
664int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
665{
666 int argc = *argcp;
667 char **argv = *argvp;
668 inet_prefix dst;
669 inet_prefix src;
670
671 memset(&dst, 0, sizeof(dst));
672 memset(&src, 0, sizeof(src));
673
674 while (1) {
675 if (strcmp(*argv, "src") == 0) {
676 NEXT_ARG();
677
678 get_prefix(&src, *argv, preferred_family);
679 if (src.family == AF_UNSPEC)
680 invarg("\"SADDR\" address family is AF_UNSPEC", *argv);
681 sel->family = src.family;
682
683 memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
684 sel->prefixlen_s = src.bitlen;
685
686 filter.sel_src_mask = src.bitlen;
687
688 } else if (strcmp(*argv, "dst") == 0) {
689 NEXT_ARG();
690
691 get_prefix(&dst, *argv, preferred_family);
692 if (dst.family == AF_UNSPEC)
693 invarg("\"DADDR\" address family is AF_UNSPEC", *argv);
694 sel->family = dst.family;
695
696 memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
697 sel->prefixlen_d = dst.bitlen;
698
699 filter.sel_dst_mask = dst.bitlen;
700
701 } else if (strcmp(*argv, "upspec") == 0) {
702 NEXT_ARG();
703
704 xfrm_selector_upspec_parse(sel, &argc, &argv);
705
706 } else if (strcmp(*argv, "dev") == 0) {
707 int ifindex;
708
709 NEXT_ARG();
710
711 if (strcmp(*argv, "none") == 0)
712 ifindex = 0;
713 else {
714 ifindex = if_nametoindex(*argv);
715 if (ifindex <= 0)
716 invarg("\"DEV\" is invalid", *argv);
717 }
718 sel->ifindex = ifindex;
719
720 filter.sel_dev_mask = XFRM_FILTER_MASK_FULL;
721
722 } else {
723 PREV_ARG(); /* back track */
724 break;
725 }
726
727 if (!NEXT_ARG_OK())
728 break;
729
730 NEXT_ARG();
731 }
732
733 if (src.family && dst.family && (src.family != dst.family))
734 invarg("the same address family is required between \"SADDR\" and \"DADDR\"", *argv);
735
736 if (argc == *argcp)
737 missarg("SELECTOR");
738
739 *argcp = argc;
740 *argvp = argv;
741
742 return 0;
743}
744
745int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft,
746 int *argcp, char ***argvp)
747{
748 int argc = *argcp;
749 char **argv = *argvp;
750 int ret;
751
752 if (strcmp(*argv, "time-soft") == 0) {
753 NEXT_ARG();
754 ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
755 if (ret)
756 invarg("\"time-soft\" value is invalid", *argv);
757 } else if (strcmp(*argv, "time-hard") == 0) {
758 NEXT_ARG();
759 ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
760 if (ret)
761 invarg("\"time-hard\" value is invalid", *argv);
762 } else if (strcmp(*argv, "time-use-soft") == 0) {
763 NEXT_ARG();
764 ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
765 if (ret)
766 invarg("\"time-use-soft\" value is invalid", *argv);
767 } else if (strcmp(*argv, "time-use-hard") == 0) {
768 NEXT_ARG();
769 ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
770 if (ret)
771 invarg("\"time-use-hard\" value is invalid", *argv);
772 } else if (strcmp(*argv, "byte-soft") == 0) {
773 NEXT_ARG();
774 ret = get_u64(&lft->soft_byte_limit, *argv, 0);
775 if (ret)
776 invarg("\"byte-soft\" value is invalid", *argv);
777 } else if (strcmp(*argv, "byte-hard") == 0) {
778 NEXT_ARG();
779 ret = get_u64(&lft->hard_byte_limit, *argv, 0);
780 if (ret)
781 invarg("\"byte-hard\" value is invalid", *argv);
782 } else if (strcmp(*argv, "packet-soft") == 0) {
783 NEXT_ARG();
784 ret = get_u64(&lft->soft_packet_limit, *argv, 0);
785 if (ret)
786 invarg("\"packet-soft\" value is invalid", *argv);
787 } else if (strcmp(*argv, "packet-hard") == 0) {
788 NEXT_ARG();
789 ret = get_u64(&lft->hard_packet_limit, *argv, 0);
790 if (ret)
791 invarg("\"packet-hard\" value is invalid", *argv);
792 } else
793 invarg("\"LIMIT\" is invalid", *argv);
794
795 *argcp = argc;
796 *argvp = argv;
797
798 return 0;
799}
800
801int do_xfrm(int argc, char **argv)
802{
803 memset(&filter, 0, sizeof(filter));
804
805 if (argc < 1)
806 usage();
807
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000808 if (matches(*argv, "state") == 0 ||
809 matches(*argv, "sa") == 0) {
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000810 return do_xfrm_state(argc-1, argv+1);
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000811 } else if (matches(*argv, "policy") == 0)
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000812 return do_xfrm_policy(argc-1, argv+1);
net[shemminger]!shemmingerad273962004-07-30 20:26:15 +0000813 else if (matches(*argv, "help") == 0) {
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000814 usage();
815 fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv);
816 exit(-1);
817 }
818 usage();
819}