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