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