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