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