572b700dac662f8b751b95655e075e3d6be1eb1c
[dragonfly.git] / lib / libipsec / pfkey_dump.c
1 /*      $FreeBSD: src/lib/libipsec/pfkey_dump.c,v 1.1.2.4 2003/04/27 00:03:36 sumikawa Exp $    */
2 /*      $KAME: pfkey_dump.c,v 1.28 2001/06/27 10:46:51 sakane Exp $     */
3
4 /*
5  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <netinet6/ipsec.h>
37 #include <net/pfkeyv2.h>
38 #include <netkey/key_var.h>
39 #include <netkey/key_debug.h>
40
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <time.h>
49 #include <netdb.h>
50
51 #include "ipsec_strerror.h"
52 #include "libpfkey.h"
53
54 /* cope with old kame headers - ugly */
55 #ifndef SADB_X_AALG_MD5
56 #define SADB_X_AALG_MD5         SADB_AALG_MD5   
57 #endif
58 #ifndef SADB_X_AALG_SHA
59 #define SADB_X_AALG_SHA         SADB_AALG_SHA
60 #endif
61 #ifndef SADB_X_AALG_NULL
62 #define SADB_X_AALG_NULL        SADB_AALG_NULL
63 #endif
64
65 #ifndef SADB_X_EALG_BLOWFISHCBC
66 #define SADB_X_EALG_BLOWFISHCBC SADB_EALG_BLOWFISHCBC
67 #endif
68 #ifndef SADB_X_EALG_CAST128CBC
69 #define SADB_X_EALG_CAST128CBC  SADB_EALG_CAST128CBC
70 #endif
71 #ifndef SADB_X_EALG_RC5CBC
72 #ifdef SADB_EALG_RC5CBC
73 #define SADB_X_EALG_RC5CBC      SADB_EALG_RC5CBC
74 #endif
75 #endif
76
77 #define GETMSGSTR(str, num) \
78 do { \
79         if (sizeof((str)[0]) == 0 \
80          || num >= sizeof(str)/sizeof((str)[0])) \
81                 printf("%d ", (num)); \
82         else if (strlen((str)[(num)]) == 0) \
83                 printf("%d ", (num)); \
84         else \
85                 printf("%s ", (str)[(num)]); \
86 } while (0)
87
88 #define GETMSGV2S(v2s, num) \
89 do { \
90         struct val2str *p;  \
91         for (p = (v2s); p && p->str; p++) { \
92                 if (p->val == (num)) \
93                         break; \
94         } \
95         if (p && p->str) \
96                 printf("%s ", p->str); \
97         else \
98                 printf("%d ", (num)); \
99 } while (0)
100
101 static char *str_ipaddr (struct sockaddr *);
102 static char *str_prefport (u_int, u_int, u_int);
103 static char *str_time (time_t);
104 static void str_lifetime_byte (struct sadb_lifetime *, char *);
105
106 struct val2str {
107         int val;
108         const char *str;
109 };
110
111 /*
112  * Must to be re-written about following strings.
113  */
114 static char *str_satype[] = {
115         "unspec",
116         "unknown",
117         "ah",
118         "esp",
119         "unknown",
120         "rsvp",
121         "ospfv2",
122         "ripv2",
123         "mip",
124         "ipcomp",
125         "policy",
126         "tcp",
127 };
128
129 static char *str_mode[] = {
130         "any",
131         "transport",
132         "tunnel",
133 };
134
135 static char *str_upper[] = {
136 /*0*/   "ip", "icmp", "igmp", "ggp", "ip4",
137         "", "tcp", "", "egp", "",
138 /*10*/  "", "", "", "", "",
139         "", "", "udp", "", "",
140 /*20*/  "", "", "idp", "", "",
141         "", "", "", "", "tp",
142 /*30*/  "", "", "", "", "",
143         "", "", "", "", "",
144 /*40*/  "", "ip6", "", "rt6", "frag6",
145         "", "rsvp", "gre", "", "",
146 /*50*/  "esp", "ah", "", "", "",
147         "", "", "", "icmp6", "none",
148 /*60*/  "dst6",
149 };
150
151 static char *str_state[] = {
152         "larval",
153         "mature",
154         "dying",
155         "dead",
156 };
157
158 static struct val2str str_alg_auth[] = {
159         { SADB_AALG_NONE, "none", },
160         { SADB_AALG_MD5HMAC, "hmac-md5", },
161         { SADB_AALG_SHA1HMAC, "hmac-sha1", },
162         { SADB_X_AALG_MD5, "md5", },
163         { SADB_X_AALG_SHA, "sha", },
164         { SADB_X_AALG_NULL, "null", },
165         { SADB_X_AALG_TCP_MD5, "tcp-md5", },
166 #ifdef SADB_X_AALG_SHA2_256
167         { SADB_X_AALG_SHA2_256, "hmac-sha2-256", },
168 #endif
169 #ifdef SADB_X_AALG_SHA2_384
170         { SADB_X_AALG_SHA2_384, "hmac-sha2-384", },
171 #endif
172 #ifdef SADB_X_AALG_SHA2_512
173         { SADB_X_AALG_SHA2_512, "hmac-sha2-512", },
174 #endif
175         { -1, NULL, },
176 };
177
178 static struct val2str str_alg_enc[] = {
179         { SADB_EALG_NONE, "none", },
180         { SADB_EALG_DESCBC, "des-cbc", },
181         { SADB_EALG_3DESCBC, "3des-cbc", },
182         { SADB_EALG_NULL, "null", },
183 #ifdef SADB_X_EALG_RC5CBC
184         { SADB_X_EALG_RC5CBC, "rc5-cbc", },
185 #endif
186         { SADB_X_EALG_CAST128CBC, "cast128-cbc", },
187         { SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", },
188 #ifdef SADB_X_EALG_RIJNDAELCBC
189         { SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", },
190 #endif
191 #ifdef SADB_X_EALG_TWOFISHCBC
192         { SADB_X_EALG_TWOFISHCBC, "twofish-cbc", },
193 #endif
194         { -1, NULL, },
195 };
196
197 static struct val2str str_alg_comp[] = {
198         { SADB_X_CALG_NONE, "none", },
199         { SADB_X_CALG_OUI, "oui", },
200         { SADB_X_CALG_DEFLATE, "deflate", },
201         { SADB_X_CALG_LZS, "lzs", },
202         { -1, NULL, },
203 };
204
205 /*
206  * dump SADB_MSG formated.  For debugging, you should use kdebug_sadb().
207  */
208 void
209 pfkey_sadump(m)
210         struct sadb_msg *m;
211 {
212         caddr_t mhp[SADB_EXT_MAX + 1];
213         struct sadb_sa *m_sa;
214         struct sadb_x_sa2 *m_sa2;
215         struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts;
216         struct sadb_address *m_saddr, *m_daddr, *m_paddr;
217         struct sadb_key *m_auth, *m_enc;
218         struct sadb_ident *m_sid, *m_did;
219         struct sadb_sens *m_sens;
220
221         /* check pfkey message. */
222         if (pfkey_align(m, mhp)) {
223                 printf("%s\n", ipsec_strerror());
224                 return;
225         }
226         if (pfkey_check(mhp)) {
227                 printf("%s\n", ipsec_strerror());
228                 return;
229         }
230
231         m_sa = (struct sadb_sa *)mhp[SADB_EXT_SA];
232         m_sa2 = (struct sadb_x_sa2 *)mhp[SADB_X_EXT_SA2];
233         m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
234         m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
235         m_lfts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT];
236         m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
237         m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
238         m_paddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_PROXY];
239         m_auth = (struct sadb_key *)mhp[SADB_EXT_KEY_AUTH];
240         m_enc = (struct sadb_key *)mhp[SADB_EXT_KEY_ENCRYPT];
241         m_sid = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_SRC];
242         m_did = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_DST];
243         m_sens = (struct sadb_sens *)mhp[SADB_EXT_SENSITIVITY];
244
245         /* source address */
246         if (m_saddr == NULL) {
247                 printf("no ADDRESS_SRC extension.\n");
248                 return;
249         }
250         printf("%s ", str_ipaddr((struct sockaddr *)(m_saddr + 1)));
251
252         /* destination address */
253         if (m_daddr == NULL) {
254                 printf("no ADDRESS_DST extension.\n");
255                 return;
256         }
257         printf("%s ", str_ipaddr((struct sockaddr *)(m_daddr + 1)));
258
259         /* SA type */
260         if (m_sa == NULL) {
261                 printf("no SA extension.\n");
262                 return;
263         }
264         if (m_sa2 == NULL) {
265                 printf("no SA2 extension.\n");
266                 return;
267         }
268         printf("\n\t");
269
270         GETMSGSTR(str_satype, m->sadb_msg_satype);
271
272         printf("mode=");
273         GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode);
274
275         printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n",
276                 (u_int32_t)ntohl(m_sa->sadb_sa_spi),
277                 (u_int32_t)ntohl(m_sa->sadb_sa_spi),
278                 (u_int32_t)m_sa2->sadb_x_sa2_reqid,
279                 (u_int32_t)m_sa2->sadb_x_sa2_reqid);
280
281         /* encryption key */
282         if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
283                 printf("\tC: ");
284                 GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt);
285         } else if (m->sadb_msg_satype == SADB_SATYPE_ESP) {
286                 if (m_enc != NULL) {
287                         printf("\tE: ");
288                         GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt);
289                         ipsec_hexdump((caddr_t)m_enc + sizeof(*m_enc),
290                                       m_enc->sadb_key_bits / 8);
291                         printf("\n");
292                 }
293         }
294
295         /* authentication key */
296         if (m_auth != NULL) {
297                 printf("\tA: ");
298                 GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth);
299                 ipsec_hexdump((caddr_t)m_auth + sizeof(*m_auth),
300                               m_auth->sadb_key_bits / 8);
301                 printf("\n");
302         }
303
304         /* replay windoe size & flags */
305         printf("\tseq=0x%08x replay=%u flags=0x%08x ",
306                 m_sa2->sadb_x_sa2_sequence,
307                 m_sa->sadb_sa_replay,
308                 m_sa->sadb_sa_flags);
309
310         /* state */
311         printf("state=");
312         GETMSGSTR(str_state, m_sa->sadb_sa_state);
313         printf("\n");
314
315         /* lifetime */
316         if (m_lftc != NULL) {
317                 time_t tmp_time = time(0);
318
319                 printf("\tcreated: %s",
320                         str_time(m_lftc->sadb_lifetime_addtime));
321                 printf("\tcurrent: %s\n", str_time(tmp_time));
322                 printf("\tdiff: %lu(s)",
323                         (u_long)(m_lftc->sadb_lifetime_addtime == 0 ?
324                         0 : (tmp_time - m_lftc->sadb_lifetime_addtime)));
325
326                 printf("\thard: %lu(s)",
327                         (u_long)(m_lfth == NULL ?
328                         0 : m_lfth->sadb_lifetime_addtime));
329                 printf("\tsoft: %lu(s)\n",
330                         (u_long)(m_lfts == NULL ?
331                         0 : m_lfts->sadb_lifetime_addtime));
332
333                 printf("\tlast: %s",
334                         str_time(m_lftc->sadb_lifetime_usetime));
335                 printf("\thard: %lu(s)",
336                         (u_long)(m_lfth == NULL ?
337                         0 : m_lfth->sadb_lifetime_usetime));
338                 printf("\tsoft: %lu(s)\n",
339                         (u_long)(m_lfts == NULL ?
340                         0 : m_lfts->sadb_lifetime_usetime));
341
342                 str_lifetime_byte(m_lftc, "current");
343                 str_lifetime_byte(m_lfth, "hard");
344                 str_lifetime_byte(m_lfts, "soft");
345                 printf("\n");
346
347                 printf("\tallocated: %lu",
348                         (unsigned long)m_lftc->sadb_lifetime_allocations);
349                 printf("\thard: %lu",
350                         (u_long)(m_lfth == NULL ?
351                         0 : m_lfth->sadb_lifetime_allocations));
352                 printf("\tsoft: %lu\n",
353                         (u_long)(m_lfts == NULL ?
354                         0 : m_lfts->sadb_lifetime_allocations));
355         }
356
357         printf("\tsadb_seq=%lu pid=%lu ",
358                 (u_long)m->sadb_msg_seq,
359                 (u_long)m->sadb_msg_pid);
360
361         /* XXX DEBUG */
362         printf("refcnt=%u\n", m->sadb_msg_reserved);
363
364         return;
365 }
366
367 void
368 pfkey_spdump(m)
369         struct sadb_msg *m;
370 {
371         char pbuf[NI_MAXSERV];
372         caddr_t mhp[SADB_EXT_MAX + 1];
373         struct sadb_address *m_saddr, *m_daddr;
374         struct sadb_x_policy *m_xpl;
375         struct sadb_lifetime *m_lft = NULL;
376         struct sockaddr *sa;
377         u_int16_t port;
378
379         /* check pfkey message. */
380         if (pfkey_align(m, mhp)) {
381                 printf("%s\n", ipsec_strerror());
382                 return;
383         }
384         if (pfkey_check(mhp)) {
385                 printf("%s\n", ipsec_strerror());
386                 return;
387         }
388
389         m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
390         m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
391         m_xpl = (struct sadb_x_policy *)mhp[SADB_X_EXT_POLICY];
392         m_lft = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
393
394         /* source address */
395         if (m_saddr == NULL) {
396                 printf("no ADDRESS_SRC extension.\n");
397                 return;
398         }
399         sa = (struct sockaddr *)(m_saddr + 1);
400         switch (sa->sa_family) {
401         case AF_INET:
402         case AF_INET6:
403                 if (getnameinfo(sa, sa->sa_len, NULL, 0, pbuf, sizeof(pbuf),
404                     NI_NUMERICSERV) != 0)
405                         port = 0;       /*XXX*/
406                 else
407                         port = atoi(pbuf);
408                 printf("%s%s ", str_ipaddr(sa),
409                         str_prefport(sa->sa_family,
410                             m_saddr->sadb_address_prefixlen, port));
411                 break;
412         default:
413                 printf("unknown-af ");
414                 break;
415         }
416
417         /* destination address */
418         if (m_daddr == NULL) {
419                 printf("no ADDRESS_DST extension.\n");
420                 return;
421         }
422         sa = (struct sockaddr *)(m_daddr + 1);
423         switch (sa->sa_family) {
424         case AF_INET:
425         case AF_INET6:
426                 if (getnameinfo(sa, sa->sa_len, NULL, 0, pbuf, sizeof(pbuf),
427                     NI_NUMERICSERV) != 0)
428                         port = 0;       /*XXX*/
429                 else
430                         port = atoi(pbuf);
431                 printf("%s%s ", str_ipaddr(sa),
432                         str_prefport(sa->sa_family,
433                             m_daddr->sadb_address_prefixlen, port));
434                 break;
435         default:
436                 printf("unknown-af ");
437                 break;
438         }
439
440         /* upper layer protocol */
441         if (m_saddr->sadb_address_proto != m_daddr->sadb_address_proto) {
442                 printf("upper layer protocol mismatched.\n");
443                 return;
444         }
445         if (m_saddr->sadb_address_proto == IPSEC_ULPROTO_ANY)
446                 printf("any");
447         else
448                 GETMSGSTR(str_upper, m_saddr->sadb_address_proto);
449
450         /* policy */
451     {
452         char *d_xpl;
453
454         if (m_xpl == NULL) {
455                 printf("no X_POLICY extension.\n");
456                 return;
457         }
458         d_xpl = ipsec_dump_policy((char *)m_xpl, "\n\t");
459
460         /* dump SPD */
461         printf("\n\t%s\n", d_xpl);
462         free(d_xpl);
463     }
464
465         /* lifetime */
466         if (m_lft) {
467                 printf("\tlifetime:%lu validtime:%lu\n",
468                         (u_long)m_lft->sadb_lifetime_addtime,
469                         (u_long)m_lft->sadb_lifetime_usetime);
470         }
471
472         printf("\tspid=%ld seq=%ld pid=%ld\n",
473                 (u_long)m_xpl->sadb_x_policy_id,
474                 (u_long)m->sadb_msg_seq,
475                 (u_long)m->sadb_msg_pid);
476
477         /* XXX TEST */
478         printf("\trefcnt=%u\n", m->sadb_msg_reserved);
479
480         return;
481 }
482
483 /*
484  * set "ipaddress" to buffer.
485  */
486 static char *
487 str_ipaddr(sa)
488         struct sockaddr *sa;
489 {
490         static char buf[NI_MAXHOST];
491 #ifdef NI_WITHSCOPEID
492         const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
493 #else
494         const int niflag = NI_NUMERICHOST;
495 #endif
496
497         if (sa == NULL)
498                 return "";
499
500         if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0, niflag) == 0)
501                 return buf;
502         return NULL;
503 }
504
505 /*
506  * set "/prefix[port number]" to buffer.
507  */
508 static char *
509 str_prefport(family, pref, port)
510         u_int family, pref, port;
511 {
512         static char buf[128];
513         char prefbuf[128];
514         char portbuf[128];
515         int plen;
516
517         switch (family) {
518         case AF_INET:
519                 plen = sizeof(struct in_addr) << 3;
520                 break;
521         case AF_INET6:
522                 plen = sizeof(struct in6_addr) << 3;
523                 break;
524         default:
525                 return "?";
526         }
527
528         if (pref == plen)
529                 prefbuf[0] = '\0';
530         else
531                 snprintf(prefbuf, sizeof(prefbuf), "/%u", pref);
532
533         if (port == IPSEC_PORT_ANY)
534                 snprintf(portbuf, sizeof(portbuf), "[%s]", "any");
535         else
536                 snprintf(portbuf, sizeof(portbuf), "[%u]", port);
537
538         snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf);
539
540         return buf;
541 }
542
543 /*
544  * set "Mon Day Time Year" to buffer
545  */
546 static char *
547 str_time(t)
548         time_t t;
549 {
550         static char buf[128];
551
552         if (t == 0) {
553                 int i = 0;
554                 for (;i < 20;) buf[i++] = ' ';
555         } else {
556                 char *t0;
557                 t0 = ctime(&t);
558                 memcpy(buf, t0 + 4, 20);
559         }
560
561         buf[20] = '\0';
562
563         return(buf);
564 }
565
566 static void
567 str_lifetime_byte(x, str)
568         struct sadb_lifetime *x;
569         char *str;
570 {
571         double y;
572         char *unit;
573         int w;
574
575         if (x == NULL) {
576                 printf("\t%s: 0(bytes)", str);
577                 return;
578         }
579
580 #if 0
581         if ((x->sadb_lifetime_bytes) / 1024 / 1024) {
582                 y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024;
583                 unit = "M";
584                 w = 1;
585         } else if ((x->sadb_lifetime_bytes) / 1024) {
586                 y = (x->sadb_lifetime_bytes) * 1.0 / 1024;
587                 unit = "K";
588                 w = 1;
589         } else {
590                 y = (x->sadb_lifetime_bytes) * 1.0;
591                 unit = "";
592                 w = 0;
593         }
594 #else
595         y = (x->sadb_lifetime_bytes) * 1.0;
596         unit = "";
597         w = 0;
598 #endif
599         printf("\t%s: %.*f(%sbytes)", str, w, y, unit);
600 }