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