source: rtems-libbsd/freebsd/lib/libipsec/pfkey_dump.c @ d48955b

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since d48955b was d48955b, checked in by Sebastian Huber <sebastian.huber@…>, on 11/06/13 at 08:02:16

Add and use <machine/rtems-bsd-user-space.h>

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