]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - ip/ipxfrm.c
iproute2: allow to specify truncation bits on auth algo
[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 = *(__u64 *)RTA_DATA(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                 if (flags)
858                         fprintf(fp, "%x", flags);
859         }
860         if (show_stats > 0)
861                 fprintf(fp, " (0x%s)", strxf_mask8(xsinfo->flags));
862         fprintf(fp, "%s", _SL_);
863
864         xfrm_xfrma_print(tb, xsinfo->family, fp, buf);
865
866         if (!xfrm_selector_iszero(&xsinfo->sel)) {
867                 char sbuf[STRBUF_SIZE];
868
869                 memcpy(sbuf, buf, sizeof(sbuf));
870                 STRBUF_CAT(sbuf, "sel ");
871
872                 xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, sbuf);
873         }
874
875         if (show_stats > 0) {
876                 xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, buf);
877                 xfrm_stats_print(&xsinfo->stats, fp, buf);
878         }
879
880         if (tb[XFRMA_SEC_CTX]) {
881                 struct xfrm_user_sec_ctx *sctx;
882
883                 fprintf(fp, "\tsecurity context ");
884
885                 if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
886                         fprintf(fp, "(ERROR truncated)");
887
888                 sctx = (struct xfrm_user_sec_ctx *)RTA_DATA(tb[XFRMA_SEC_CTX]);
889
890                 fprintf(fp, "%s %s", (char *)(sctx + 1), _SL_);
891         }
892
893 }
894
895 void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
896                             struct rtattr *tb[], FILE *fp, const char *prefix,
897                             const char *title)
898 {
899         char buf[STRBUF_SIZE];
900
901         memset(buf, '\0', sizeof(buf));
902
903         xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title);
904
905         if (tb[XFRMA_SEC_CTX]) {
906                 struct xfrm_user_sec_ctx *sctx;
907
908                 fprintf(fp, "\tsecurity context ");
909
910                 if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
911                         fprintf(fp, "(ERROR truncated)");
912
913                 sctx = (struct xfrm_user_sec_ctx *)RTA_DATA(tb[XFRMA_SEC_CTX]);
914
915                 fprintf(fp, "%s ", (char *)(sctx + 1));
916                 fprintf(fp, "%s", _SL_);
917         }
918
919         if (prefix)
920                 STRBUF_CAT(buf, prefix);
921         STRBUF_CAT(buf, "\t");
922
923         fputs(buf, fp);
924         if (xpinfo->dir >= XFRM_POLICY_MAX) {
925                 xpinfo->dir -= XFRM_POLICY_MAX;
926                 fprintf(fp, "socket ");
927         } else
928                 fprintf(fp, "dir ");
929
930         switch (xpinfo->dir) {
931         case XFRM_POLICY_IN:
932                 fprintf(fp, "in");
933                 break;
934         case XFRM_POLICY_OUT:
935                 fprintf(fp, "out");
936                 break;
937         case XFRM_POLICY_FWD:
938                 fprintf(fp, "fwd");
939                 break;
940         default:
941                 fprintf(fp, "%u", xpinfo->dir);
942                 break;
943         }
944         fprintf(fp, " ");
945
946         switch (xpinfo->action) {
947         case XFRM_POLICY_ALLOW:
948                 if (show_stats > 0)
949                         fprintf(fp, "action allow ");
950                 break;
951         case XFRM_POLICY_BLOCK:
952                 fprintf(fp, "action block ");
953                 break;
954         default:
955                 fprintf(fp, "action %u ", xpinfo->action);
956                 break;
957         }
958
959         if (show_stats)
960                 fprintf(fp, "index %u ", xpinfo->index);
961         fprintf(fp, "priority %u ", xpinfo->priority);
962
963         if (tb[XFRMA_POLICY_TYPE]) {
964                 struct xfrm_userpolicy_type *upt;
965
966                 fprintf(fp, "ptype ");
967
968                 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
969                         fprintf(fp, "(ERROR truncated)");
970
971                 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
972                 fprintf(fp, "%s ", strxf_ptype(upt->type));
973         }
974
975         if (show_stats > 0)
976                 fprintf(fp, "share %s ", strxf_share(xpinfo->share));
977
978         if (show_stats > 0 || xpinfo->flags) {
979                 __u8 flags = xpinfo->flags;
980
981                 fprintf(fp, "flag ");
982                 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_LOCALOK, "localok");
983                 if (flags)
984                         fprintf(fp, "%x", flags);
985         }
986         if (show_stats > 0)
987                 fprintf(fp, " (0x%s)", strxf_mask8(xpinfo->flags));
988         fprintf(fp, "%s", _SL_);
989
990         if (show_stats > 0)
991                 xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf);
992
993         xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf);
994 }
995
996 int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
997                   int loose, int *argcp, char ***argvp)
998 {
999         int argc = *argcp;
1000         char **argv = *argvp;
1001         inet_prefix dst;
1002         inet_prefix src;
1003
1004         memset(&dst, 0, sizeof(dst));
1005         memset(&src, 0, sizeof(src));
1006
1007         while (1) {
1008                 if (strcmp(*argv, "src") == 0) {
1009                         NEXT_ARG();
1010
1011                         get_prefix(&src, *argv, preferred_family);
1012                         if (src.family == AF_UNSPEC)
1013                                 invarg("\"src\" address family is AF_UNSPEC", *argv);
1014                         if (family)
1015                                 *family = src.family;
1016
1017                         memcpy(saddr, &src.data, sizeof(*saddr));
1018
1019                         filter.id_src_mask = src.bitlen;
1020
1021                 } else if (strcmp(*argv, "dst") == 0) {
1022                         NEXT_ARG();
1023
1024                         get_prefix(&dst, *argv, preferred_family);
1025                         if (dst.family == AF_UNSPEC)
1026                                 invarg("\"dst\" address family is AF_UNSPEC", *argv);
1027                         if (family)
1028                                 *family = dst.family;
1029
1030                         memcpy(&id->daddr, &dst.data, sizeof(id->daddr));
1031
1032                         filter.id_dst_mask = dst.bitlen;
1033
1034                 } else if (strcmp(*argv, "proto") == 0) {
1035                         int ret;
1036
1037                         NEXT_ARG();
1038
1039                         ret = xfrm_xfrmproto_getbyname(*argv);
1040                         if (ret < 0)
1041                                 invarg("\"XFRM_PROTO\" is invalid", *argv);
1042
1043                         id->proto = (__u8)ret;
1044
1045                         filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
1046
1047                 } else if (strcmp(*argv, "spi") == 0) {
1048                         __u32 spi;
1049
1050                         NEXT_ARG();
1051                         if (get_u32(&spi, *argv, 0))
1052                                 invarg("\"SPI\" is invalid", *argv);
1053
1054                         spi = htonl(spi);
1055                         id->spi = spi;
1056
1057                         filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
1058
1059                 } else {
1060                         PREV_ARG(); /* back track */
1061                         break;
1062                 }
1063
1064                 if (!NEXT_ARG_OK())
1065                         break;
1066                 NEXT_ARG();
1067         }
1068
1069         if (src.family && dst.family && (src.family != dst.family))
1070                 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
1071
1072         if (loose == 0 && id->proto == 0)
1073                 missarg("XFRM_PROTO");
1074         if (argc == *argcp)
1075                 missarg("ID");
1076
1077         *argcp = argc;
1078         *argvp = argv;
1079
1080         return 0;
1081 }
1082
1083 int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp)
1084 {
1085         int argc = *argcp;
1086         char **argv = *argvp;
1087
1088         if (matches(*argv, "transport") == 0)
1089                 *mode = XFRM_MODE_TRANSPORT;
1090         else if (matches(*argv, "tunnel") == 0)
1091                 *mode = XFRM_MODE_TUNNEL;
1092         else if (matches(*argv, "ro") == 0)
1093                 *mode = XFRM_MODE_ROUTEOPTIMIZATION;
1094         else if (matches(*argv, "in_trigger") == 0)
1095                 *mode = XFRM_MODE_IN_TRIGGER;
1096         else if (matches(*argv, "beet") == 0)
1097                 *mode = XFRM_MODE_BEET;
1098         else
1099                 invarg("\"MODE\" is invalid", *argv);
1100
1101         *argcp = argc;
1102         *argvp = argv;
1103
1104         return 0;
1105 }
1106
1107 int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp)
1108 {
1109         int argc = *argcp;
1110         char **argv = *argvp;
1111
1112         if (strcmp(*argv, "espinudp-nonike") == 0)
1113                 *type = 1;
1114         else if (strcmp(*argv, "espinudp") == 0)
1115                 *type = 2;
1116         else
1117                 invarg("\"ENCAP-TYPE\" is invalid", *argv);
1118
1119         *argcp = argc;
1120         *argvp = argv;
1121
1122         return 0;
1123 }
1124
1125 /* NOTE: reqid is used by host-byte order */
1126 int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp)
1127 {
1128         int argc = *argcp;
1129         char **argv = *argvp;
1130
1131         if (get_u32(reqid, *argv, 0))
1132                 invarg("\"REQID\" is invalid", *argv);
1133
1134         *argcp = argc;
1135         *argvp = argv;
1136
1137         return 0;
1138 }
1139
1140 static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
1141                                       int *argcp, char ***argvp)
1142 {
1143         int argc = *argcp;
1144         char **argv = *argvp;
1145         char *sportp = NULL;
1146         char *dportp = NULL;
1147         char *typep = NULL;
1148         char *codep = NULL;
1149         char *grekey = NULL;
1150
1151         while (1) {
1152                 if (strcmp(*argv, "proto") == 0) {
1153                         __u8 upspec;
1154
1155                         NEXT_ARG();
1156
1157                         if (strcmp(*argv, "any") == 0)
1158                                 upspec = 0;
1159                         else {
1160                                 struct protoent *pp;
1161                                 pp = getprotobyname(*argv);
1162                                 if (pp)
1163                                         upspec = pp->p_proto;
1164                                 else {
1165                                         if (get_u8(&upspec, *argv, 0))
1166                                                 invarg("\"PROTO\" is invalid", *argv);
1167                                 }
1168                         }
1169                         sel->proto = upspec;
1170
1171                         filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL;
1172
1173                 } else if (strcmp(*argv, "sport") == 0) {
1174                         sportp = *argv;
1175
1176                         NEXT_ARG();
1177
1178                         if (get_u16(&sel->sport, *argv, 0))
1179                                 invarg("\"PORT\" is invalid", *argv);
1180                         sel->sport = htons(sel->sport);
1181                         if (sel->sport)
1182                                 sel->sport_mask = ~((__u16)0);
1183
1184                         filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1185
1186                 } else if (strcmp(*argv, "dport") == 0) {
1187                         dportp = *argv;
1188
1189                         NEXT_ARG();
1190
1191                         if (get_u16(&sel->dport, *argv, 0))
1192                                 invarg("\"PORT\" is invalid", *argv);
1193                         sel->dport = htons(sel->dport);
1194                         if (sel->dport)
1195                                 sel->dport_mask = ~((__u16)0);
1196
1197                         filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1198
1199                 } else if (strcmp(*argv, "type") == 0) {
1200                         typep = *argv;
1201
1202                         NEXT_ARG();
1203
1204                         if (get_u16(&sel->sport, *argv, 0) ||
1205                             (sel->sport & ~((__u16)0xff)))
1206                                 invarg("\"type\" value is invalid", *argv);
1207                         sel->sport = htons(sel->sport);
1208                         sel->sport_mask = ~((__u16)0);
1209
1210                         filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1211
1212
1213                 } else if (strcmp(*argv, "code") == 0) {
1214                         codep = *argv;
1215
1216                         NEXT_ARG();
1217
1218                         if (get_u16(&sel->dport, *argv, 0) ||
1219                             (sel->dport & ~((__u16)0xff)))
1220                                 invarg("\"code\" value is invalid", *argv);
1221                         sel->dport = htons(sel->dport);
1222                         sel->dport_mask = ~((__u16)0);
1223
1224                         filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1225
1226                 } else if (strcmp(*argv, "key") == 0) {
1227                         unsigned uval;
1228
1229                         grekey = *argv;
1230
1231                         NEXT_ARG();
1232
1233                         if (strchr(*argv, '.'))
1234                                 uval = htonl(get_addr32(*argv));
1235                         else {
1236                                 if (get_unsigned(&uval, *argv, 0)<0) {
1237                                         fprintf(stderr, "invalid value of \"key\"\n");
1238                                         exit(-1);
1239                                 }
1240                         }
1241
1242                         sel->sport = htons(uval >> 16);
1243                         sel->dport = htons(uval & 0xffff);
1244                         sel->sport_mask = ~((__u16)0);
1245                         sel->dport_mask = ~((__u16)0);
1246
1247                         filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1248
1249                 } else {
1250                         PREV_ARG(); /* back track */
1251                         break;
1252                 }
1253
1254                 if (!NEXT_ARG_OK())
1255                         break;
1256                 NEXT_ARG();
1257         }
1258         if (argc == *argcp)
1259                 missarg("UPSPEC");
1260         if (sportp || dportp) {
1261                 switch (sel->proto) {
1262                 case IPPROTO_TCP:
1263                 case IPPROTO_UDP:
1264                 case IPPROTO_SCTP:
1265                 case IPPROTO_DCCP:
1266                         break;
1267                 default:
1268                         fprintf(stderr, "\"sport\" and \"dport\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1269                         exit(1);
1270                 }
1271         }
1272         if (typep || codep) {
1273                 switch (sel->proto) {
1274                 case IPPROTO_ICMP:
1275                 case IPPROTO_ICMPV6:
1276                 case IPPROTO_MH:
1277                         break;
1278                 default:
1279                         fprintf(stderr, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1280                         exit(1);
1281                 }
1282         }
1283         if (grekey) {
1284                 switch (sel->proto) {
1285                 case IPPROTO_GRE:
1286                         break;
1287                 default:
1288                         fprintf(stderr, "\"key\" is invalid with proto=%s\n", strxf_proto(sel->proto));
1289                         exit(1);
1290                 }
1291         }
1292
1293         *argcp = argc;
1294         *argvp = argv;
1295
1296         return 0;
1297 }
1298
1299 int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
1300 {
1301         int argc = *argcp;
1302         char **argv = *argvp;
1303         inet_prefix dst;
1304         inet_prefix src;
1305         char *upspecp = NULL;
1306
1307         memset(&dst, 0, sizeof(dst));
1308         memset(&src, 0, sizeof(src));
1309
1310         while (1) {
1311                 if (strcmp(*argv, "src") == 0) {
1312                         NEXT_ARG();
1313
1314                         get_prefix(&src, *argv, preferred_family);
1315                         if (src.family == AF_UNSPEC)
1316                                 invarg("\"src\" address family is AF_UNSPEC", *argv);
1317                         sel->family = src.family;
1318
1319                         memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
1320                         sel->prefixlen_s = src.bitlen;
1321
1322                         filter.sel_src_mask = src.bitlen;
1323
1324                 } else if (strcmp(*argv, "dst") == 0) {
1325                         NEXT_ARG();
1326
1327                         get_prefix(&dst, *argv, preferred_family);
1328                         if (dst.family == AF_UNSPEC)
1329                                 invarg("\"dst\" address family is AF_UNSPEC", *argv);
1330                         sel->family = dst.family;
1331
1332                         memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
1333                         sel->prefixlen_d = dst.bitlen;
1334
1335                         filter.sel_dst_mask = dst.bitlen;
1336
1337                 } else if (strcmp(*argv, "dev") == 0) {
1338                         int ifindex;
1339
1340                         NEXT_ARG();
1341
1342                         if (strcmp(*argv, "none") == 0)
1343                                 ifindex = 0;
1344                         else {
1345                                 ifindex = ll_name_to_index(*argv);
1346                                 if (ifindex <= 0)
1347                                         invarg("\"DEV\" is invalid", *argv);
1348                         }
1349                         sel->ifindex = ifindex;
1350
1351                         filter.sel_dev_mask = XFRM_FILTER_MASK_FULL;
1352
1353                 } else {
1354                         if (upspecp) {
1355                                 PREV_ARG(); /* back track */
1356                                 break;
1357                         } else {
1358                                 upspecp = *argv;
1359                                 xfrm_selector_upspec_parse(sel, &argc, &argv);
1360                         }
1361                 }
1362
1363                 if (!NEXT_ARG_OK())
1364                         break;
1365
1366                 NEXT_ARG();
1367         }
1368
1369         if (src.family && dst.family && (src.family != dst.family))
1370                 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
1371
1372         if (argc == *argcp)
1373                 missarg("SELECTOR");
1374
1375         *argcp = argc;
1376         *argvp = argv;
1377
1378         return 0;
1379 }
1380
1381 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft,
1382                             int *argcp, char ***argvp)
1383 {
1384         int argc = *argcp;
1385         char **argv = *argvp;
1386         int ret;
1387
1388         if (strcmp(*argv, "time-soft") == 0) {
1389                 NEXT_ARG();
1390                 ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
1391                 if (ret)
1392                         invarg("\"time-soft\" value is invalid", *argv);
1393         } else if (strcmp(*argv, "time-hard") == 0) {
1394                 NEXT_ARG();
1395                 ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
1396                 if (ret)
1397                         invarg("\"time-hard\" value is invalid", *argv);
1398         } else if (strcmp(*argv, "time-use-soft") == 0) {
1399                 NEXT_ARG();
1400                 ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
1401                 if (ret)
1402                         invarg("\"time-use-soft\" value is invalid", *argv);
1403         } else if (strcmp(*argv, "time-use-hard") == 0) {
1404                 NEXT_ARG();
1405                 ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
1406                 if (ret)
1407                         invarg("\"time-use-hard\" value is invalid", *argv);
1408         } else if (strcmp(*argv, "byte-soft") == 0) {
1409                 NEXT_ARG();
1410                 ret = get_u64(&lft->soft_byte_limit, *argv, 0);
1411                 if (ret)
1412                         invarg("\"byte-soft\" value is invalid", *argv);
1413         } else if (strcmp(*argv, "byte-hard") == 0) {
1414                 NEXT_ARG();
1415                 ret = get_u64(&lft->hard_byte_limit, *argv, 0);
1416                 if (ret)
1417                         invarg("\"byte-hard\" value is invalid", *argv);
1418         } else if (strcmp(*argv, "packet-soft") == 0) {
1419                 NEXT_ARG();
1420                 ret = get_u64(&lft->soft_packet_limit, *argv, 0);
1421                 if (ret)
1422                         invarg("\"packet-soft\" value is invalid", *argv);
1423         } else if (strcmp(*argv, "packet-hard") == 0) {
1424                 NEXT_ARG();
1425                 ret = get_u64(&lft->hard_packet_limit, *argv, 0);
1426                 if (ret)
1427                         invarg("\"packet-hard\" value is invalid", *argv);
1428         } else
1429                 invarg("\"LIMIT\" is invalid", *argv);
1430
1431         *argcp = argc;
1432         *argvp = argv;
1433
1434         return 0;
1435 }
1436
1437 int do_xfrm(int argc, char **argv)
1438 {
1439         memset(&filter, 0, sizeof(filter));
1440
1441         if (argc < 1)
1442                 usage();
1443
1444         if (matches(*argv, "state") == 0 ||
1445             matches(*argv, "sa") == 0)
1446                 return do_xfrm_state(argc-1, argv+1);
1447         else if (matches(*argv, "policy") == 0)
1448                 return do_xfrm_policy(argc-1, argv+1);
1449         else if (matches(*argv, "monitor") == 0)
1450                 return do_xfrm_monitor(argc-1, argv+1);
1451         else if (matches(*argv, "help") == 0) {
1452                 usage();
1453                 fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv);
1454                 exit(-1);
1455         }
1456         usage();
1457 }