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