http://downloads.netgear.com/files/GPL/GPL_Source_V361j_DM111PSP_series_consumer_rele...
[bcm963xx.git] / userapps / opensource / ipsec-tools / src / 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 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #ifdef HAVE_NETINET6_IPSEC
40 #  include <netinet6/ipsec.h>
41 #else
42 #  include <netinet/ipsec.h>
43 #endif
44 #include <net/pfkeyv2.h>
45
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <time.h>
54 #include <netdb.h>
55
56 #include "ipsec_strerror.h"
57 #include "libpfkey.h"
58
59 /* cope with old kame headers - ugly */
60 #ifndef SADB_X_AALG_MD5
61 #define SADB_X_AALG_MD5         SADB_AALG_MD5   
62 #endif
63 #ifndef SADB_X_AALG_SHA
64 #define SADB_X_AALG_SHA         SADB_AALG_SHA
65 #endif
66 #ifndef SADB_X_AALG_NULL
67 #define SADB_X_AALG_NULL        SADB_AALG_NULL
68 #endif
69
70 #ifndef SADB_X_EALG_BLOWFISHCBC
71 #define SADB_X_EALG_BLOWFISHCBC SADB_EALG_BLOWFISHCBC
72 #endif
73 #ifndef SADB_X_EALG_CAST128CBC
74 #define SADB_X_EALG_CAST128CBC  SADB_EALG_CAST128CBC
75 #endif
76 #ifndef SADB_X_EALG_RC5CBC
77 #ifdef SADB_EALG_RC5CBC
78 #define SADB_X_EALG_RC5CBC      SADB_EALG_RC5CBC
79 #endif
80 #endif
81
82 #define GETMSGSTR(str, num) \
83 do { \
84         if (sizeof((str)[0]) == 0 \
85          || num >= sizeof(str)/sizeof((str)[0])) \
86                 printf("%u ", (num)); \
87         else if (strlen((str)[(num)]) == 0) \
88                 printf("%u ", (num)); \
89         else \
90                 printf("%s ", (str)[(num)]); \
91 } while (0)
92
93 #define GETMSGV2S(v2s, num) \
94 do { \
95         struct val2str *p;  \
96         for (p = (v2s); p && p->str; p++) { \
97                 if (p->val == (num)) \
98                         break; \
99         } \
100         if (p && p->str) \
101                 printf("%s ", p->str); \
102         else \
103                 printf("%u ", (num)); \
104 } while (0)
105
106 static char *str_ipaddr __P((struct sockaddr *));
107 static char *str_prefport __P((u_int, u_int, u_int, u_int));
108 static void str_upperspec __P((u_int, u_int, u_int));
109 static char *str_time __P((time_t));
110 static void str_lifetime_byte __P((struct sadb_lifetime *, char *));
111
112 struct val2str {
113         int val;
114         const char *str;
115 };
116
117 /*
118  * Must to be re-written about following strings.
119  */
120 static char *str_satype[] = {
121         "unspec",
122         "unknown",
123         "ah",
124         "esp",
125         "unknown",
126         "rsvp",
127         "ospfv2",
128         "ripv2",
129         "mip",
130         "ipcomp",
131 };
132
133 static char *str_mode[] = {
134         "any",
135         "transport",
136         "tunnel",
137 };
138
139 static char *str_state[] = {
140         "larval",
141         "mature",
142         "dying",
143         "dead",
144 };
145
146 static struct val2str str_alg_auth[] = {
147         { SADB_AALG_NONE, "none", },
148         { SADB_AALG_MD5HMAC, "hmac-md5", },
149         { SADB_AALG_SHA1HMAC, "hmac-sha1", },
150         { SADB_X_AALG_MD5, "md5", },
151         { SADB_X_AALG_SHA, "sha", },
152         { SADB_X_AALG_NULL, "null", },
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_AESCBC
182         { SADB_X_EALG_AESCBC, "aes-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         { -1, NULL, },
191 };
192
193 static struct val2str str_alg_comp[] = {
194         { SADB_X_CALG_NONE, "none", },
195         { SADB_X_CALG_OUI, "oui", },
196         { SADB_X_CALG_DEFLATE, "deflate", },
197         { SADB_X_CALG_LZS, "lzs", },
198         { -1, NULL, },
199 };
200
201 /*
202  * dump SADB_MSG formated.  For debugging, you should use kdebug_sadb().
203  */
204 void
205 pfkey_sadump(m)
206         struct sadb_msg *m;
207 {
208         caddr_t mhp[SADB_EXT_MAX + 1];
209         struct sadb_sa *m_sa;
210         struct sadb_x_sa2 *m_sa2;
211         struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts;
212         struct sadb_address *m_saddr, *m_daddr, *m_paddr;
213         struct sadb_key *m_auth, *m_enc;
214         struct sadb_ident *m_sid, *m_did;
215         struct sadb_sens *m_sens;
216 #ifdef SADB_X_EXT_NAT_T_TYPE
217         struct sadb_x_nat_t_type *natt_type;
218         struct sadb_x_nat_t_port *natt_sport, *natt_dport;
219         struct sadb_address *natt_oa;
220
221         int use_natt = 0;
222 #endif
223
224         /* check pfkey message. */
225         if (pfkey_align(m, mhp)) {
226                 printf("%s\n", ipsec_strerror());
227                 return;
228         }
229         if (pfkey_check(mhp)) {
230                 printf("%s\n", ipsec_strerror());
231                 return;
232         }
233
234         m_sa = (struct sadb_sa *)mhp[SADB_EXT_SA];
235         m_sa2 = (struct sadb_x_sa2 *)mhp[SADB_X_EXT_SA2];
236         m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
237         m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
238         m_lfts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT];
239         m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
240         m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
241         m_paddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_PROXY];
242         m_auth = (struct sadb_key *)mhp[SADB_EXT_KEY_AUTH];
243         m_enc = (struct sadb_key *)mhp[SADB_EXT_KEY_ENCRYPT];
244         m_sid = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_SRC];
245         m_did = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_DST];
246         m_sens = (struct sadb_sens *)mhp[SADB_EXT_SENSITIVITY];
247 #ifdef SADB_X_EXT_NAT_T_TYPE
248         natt_type = (struct sadb_x_nat_t_type *)mhp[SADB_X_EXT_NAT_T_TYPE];
249         natt_sport = (struct sadb_x_nat_t_port *)mhp[SADB_X_EXT_NAT_T_SPORT];
250         natt_dport = (struct sadb_x_nat_t_port *)mhp[SADB_X_EXT_NAT_T_DPORT];
251         natt_oa = (struct sadb_address *)mhp[SADB_X_EXT_NAT_T_OA];
252
253         if (natt_type && natt_type->sadb_x_nat_t_type_type)
254                 use_natt = 1;
255 #endif
256
257         /* source address */
258         if (m_saddr == NULL) {
259                 printf("no ADDRESS_SRC extension.\n");
260                 return;
261         }
262         printf("%s", str_ipaddr((struct sockaddr *)(m_saddr + 1)));
263 #ifdef SADB_X_EXT_NAT_T_TYPE
264         if (use_natt && natt_sport)
265                 printf("[%u]", ntohs(natt_sport->sadb_x_nat_t_port_port));
266 #endif
267         printf(" ");
268
269         /* destination address */
270         if (m_daddr == NULL) {
271                 printf(" no ADDRESS_DST extension.\n");
272                 return;
273         }
274         printf("%s", str_ipaddr((struct sockaddr *)(m_daddr + 1)));
275 #ifdef SADB_X_EXT_NAT_T_TYPE
276         if (use_natt && natt_dport)
277                 printf("[%u]", ntohs(natt_dport->sadb_x_nat_t_port_port));
278 #endif
279         printf(" ");
280
281         /* SA type */
282         if (m_sa == NULL) {
283                 printf("no SA extension.\n");
284                 return;
285         }
286         if (m_sa2 == NULL) {
287                 printf("no SA2 extension.\n");
288                 return;
289         }
290         printf("\n\t");
291
292 #ifdef SADB_X_EXT_NAT_T_TYPE
293         if (use_natt && m->sadb_msg_satype == SADB_SATYPE_ESP)
294                 printf("esp-udp ");
295         else if (use_natt)
296                 printf("natt+");
297
298         if (!use_natt || m->sadb_msg_satype != SADB_SATYPE_ESP)
299 #endif
300         GETMSGSTR(str_satype, m->sadb_msg_satype);
301
302         printf("mode=");
303         GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode);
304
305         printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n",
306                 (u_int32_t)ntohl(m_sa->sadb_sa_spi),
307                 (u_int32_t)ntohl(m_sa->sadb_sa_spi),
308                 (u_int32_t)m_sa2->sadb_x_sa2_reqid,
309                 (u_int32_t)m_sa2->sadb_x_sa2_reqid);
310
311 #ifdef SADB_X_EXT_NAT_T_TYPE
312         /* other NAT-T information */
313         if (use_natt && natt_oa)
314                 printf("\tNAT OA=%s\n",
315                        str_ipaddr((struct sockaddr *)(natt_oa + 1)));
316 #endif
317
318         /* encryption key */
319         if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
320                 printf("\tC: ");
321                 GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt);
322         } else if (m->sadb_msg_satype == SADB_SATYPE_ESP) {
323                 if (m_enc != NULL) {
324                         printf("\tE: ");
325                         GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt);
326                         ipsec_hexdump((caddr_t)m_enc + sizeof(*m_enc),
327                                       m_enc->sadb_key_bits / 8);
328                         printf("\n");
329                 }
330         }
331
332         /* authentication key */
333         if (m_auth != NULL) {
334                 printf("\tA: ");
335                 GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth);
336                 ipsec_hexdump((caddr_t)m_auth + sizeof(*m_auth),
337                               m_auth->sadb_key_bits / 8);
338                 printf("\n");
339         }
340
341         /* replay windoe size & flags */
342         printf("\tseq=0x%08x replay=%u flags=0x%08x ",
343                 m_sa2->sadb_x_sa2_sequence,
344                 m_sa->sadb_sa_replay,
345                 m_sa->sadb_sa_flags);
346
347         /* state */
348         printf("state=");
349         GETMSGSTR(str_state, m_sa->sadb_sa_state);
350         printf("\n");
351
352         /* lifetime */
353         if (m_lftc != NULL) {
354                 time_t tmp_time = time(0);
355
356                 printf("\tcreated: %s",
357                         str_time(m_lftc->sadb_lifetime_addtime));
358                 printf("\tcurrent: %s\n", str_time(tmp_time));
359                 printf("\tdiff: %lu(s)",
360                         (u_long)(m_lftc->sadb_lifetime_addtime == 0 ?
361                         0 : (tmp_time - m_lftc->sadb_lifetime_addtime)));
362
363                 printf("\thard: %lu(s)",
364                         (u_long)(m_lfth == NULL ?
365                         0 : m_lfth->sadb_lifetime_addtime));
366                 printf("\tsoft: %lu(s)\n",
367                         (u_long)(m_lfts == NULL ?
368                         0 : m_lfts->sadb_lifetime_addtime));
369
370                 printf("\tlast: %s",
371                         str_time(m_lftc->sadb_lifetime_usetime));
372                 printf("\thard: %lu(s)",
373                         (u_long)(m_lfth == NULL ?
374                         0 : m_lfth->sadb_lifetime_usetime));
375                 printf("\tsoft: %lu(s)\n",
376                         (u_long)(m_lfts == NULL ?
377                         0 : m_lfts->sadb_lifetime_usetime));
378
379                 str_lifetime_byte(m_lftc, "current");
380                 str_lifetime_byte(m_lfth, "hard");
381                 str_lifetime_byte(m_lfts, "soft");
382                 printf("\n");
383
384                 printf("\tallocated: %lu",
385                         (unsigned long)m_lftc->sadb_lifetime_allocations);
386                 printf("\thard: %lu",
387                         (u_long)(m_lfth == NULL ?
388                         0 : m_lfth->sadb_lifetime_allocations));
389                 printf("\tsoft: %lu\n",
390                         (u_long)(m_lfts == NULL ?
391                         0 : m_lfts->sadb_lifetime_allocations));
392         }
393
394         printf("\tsadb_seq=%lu pid=%lu ",
395                 (u_long)m->sadb_msg_seq,
396                 (u_long)m->sadb_msg_pid);
397
398         /* XXX DEBUG */
399         printf("refcnt=%u\n", m->sadb_msg_reserved);
400
401         return;
402 }
403
404 void
405 pfkey_spdump(m)
406         struct sadb_msg *m;
407 {
408         char pbuf[NI_MAXSERV];
409         caddr_t mhp[SADB_EXT_MAX + 1];
410         struct sadb_address *m_saddr, *m_daddr;
411 #ifdef SADB_X_EXT_TAG
412         struct sadb_x_tag *m_tag;
413 #endif
414         struct sadb_x_policy *m_xpl;
415         struct sadb_lifetime *m_lftc = NULL, *m_lfth = NULL;
416         struct sockaddr *sa;
417         u_int16_t sport = 0, dport = 0;
418
419         /* check pfkey message. */
420         if (pfkey_align(m, mhp)) {
421                 printf("%s\n", ipsec_strerror());
422                 return;
423         }
424         if (pfkey_check(mhp)) {
425                 printf("%s\n", ipsec_strerror());
426                 return;
427         }
428
429         m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
430         m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
431 #ifdef SADB_X_EXT_TAG
432         m_tag = (struct sadb_x_tag *)mhp[SADB_X_EXT_TAG];
433 #endif
434         m_xpl = (struct sadb_x_policy *)mhp[SADB_X_EXT_POLICY];
435         m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
436         m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
437
438 #ifdef __linux__
439         /* *bsd indicates per-socket policies by omiting src and dst 
440          * extensions. Linux always includes them, but we can catch it
441          * by checkin for policy id.
442          */
443         if (m_xpl->sadb_x_policy_id % 8 >= 3) {
444                 printf("(per-socket policy) ");
445         } else
446 #endif
447         if (m_saddr && m_daddr) {
448                 /* source address */
449                 sa = (struct sockaddr *)(m_saddr + 1);
450                 switch (sa->sa_family) {
451                 case AF_INET:
452                 case AF_INET6:
453                         if (getnameinfo(sa, sysdep_sa_len(sa), NULL, 0,
454                             pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0)
455                                 sport = 0;      /*XXX*/
456                         else
457                                 sport = atoi(pbuf);
458                         printf("%s%s ", str_ipaddr(sa),
459                                 str_prefport(sa->sa_family,
460                                     m_saddr->sadb_address_prefixlen, sport,
461                                     m_saddr->sadb_address_proto));
462                         break;
463                 default:
464                         printf("unknown-af ");
465                         break;
466                 }
467
468                 /* destination address */
469                 sa = (struct sockaddr *)(m_daddr + 1);
470                 switch (sa->sa_family) {
471                 case AF_INET:
472                 case AF_INET6:
473                         if (getnameinfo(sa, sysdep_sa_len(sa), NULL, 0,
474                             pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0)
475                                 dport = 0;      /*XXX*/
476                         else
477                                 dport = atoi(pbuf);
478                         printf("%s%s ", str_ipaddr(sa),
479                                 str_prefport(sa->sa_family,
480                                     m_daddr->sadb_address_prefixlen, dport,
481                                     m_saddr->sadb_address_proto));
482                         break;
483                 default:
484                         printf("unknown-af ");
485                         break;
486                 }
487
488                 /* upper layer protocol */
489                 if (m_saddr->sadb_address_proto !=
490                     m_daddr->sadb_address_proto) {
491                         printf("upper layer protocol mismatched.\n");
492                         return;
493                 }
494                 str_upperspec(m_saddr->sadb_address_proto, sport, dport);
495         }
496 #ifdef SADB_X_EXT_TAG
497         else if (m_tag)
498                 printf("tagged \"%s\" ", m_tag->sadb_x_tag_name);
499 #endif
500         else
501                 printf("(no selector, probably per-socket policy) ");
502
503         /* policy */
504     {
505         char *d_xpl;
506
507         if (m_xpl == NULL) {
508                 printf("no X_POLICY extension.\n");
509                 return;
510         }
511         d_xpl = ipsec_dump_policy((char *)m_xpl, "\n\t");
512         if (!d_xpl)
513                 printf("\n\tPolicy:[%s]\n", ipsec_strerror());
514         else {
515                 /* dump SPD */
516                 printf("\n\t%s\n", d_xpl);
517                 free(d_xpl);
518         }
519     }
520
521         /* lifetime */
522         if (m_lftc) {
523                 printf("\tcreated: %s  ",
524                         str_time(m_lftc->sadb_lifetime_addtime));
525                 printf("lastused: %s\n",
526                         str_time(m_lftc->sadb_lifetime_usetime));
527         }
528         if (m_lfth) {
529                 printf("\tlifetime: %lu(s) ",
530                         (u_long)m_lfth->sadb_lifetime_addtime);
531                 printf("validtime: %lu(s)\n",
532                         (u_long)m_lfth->sadb_lifetime_usetime);
533         }
534
535
536         printf("\tspid=%ld seq=%ld pid=%ld\n",
537                 (u_long)m_xpl->sadb_x_policy_id,
538                 (u_long)m->sadb_msg_seq,
539                 (u_long)m->sadb_msg_pid);
540
541         /* XXX TEST */
542         printf("\trefcnt=%u\n", m->sadb_msg_reserved);
543
544         return;
545 }
546
547 /*
548  * set "ipaddress" to buffer.
549  */
550 static char *
551 str_ipaddr(sa)
552         struct sockaddr *sa;
553 {
554         static char buf[NI_MAXHOST];
555         const int niflag = NI_NUMERICHOST;
556
557         if (sa == NULL)
558                 return "";
559
560         if (getnameinfo(sa, sysdep_sa_len(sa), buf, sizeof(buf), NULL, 0, niflag) == 0)
561                 return buf;
562         return NULL;
563 }
564
565 /*
566  * set "/prefix[port number]" to buffer.
567  */
568 static char *
569 str_prefport(family, pref, port, ulp)
570         u_int family, pref, port, ulp;
571 {
572         static char buf[128];
573         char prefbuf[128];
574         char portbuf[128];
575         int plen;
576
577         switch (family) {
578         case AF_INET:
579                 plen = sizeof(struct in_addr) << 3;
580                 break;
581         case AF_INET6:
582                 plen = sizeof(struct in6_addr) << 3;
583                 break;
584         default:
585                 return "?";
586         }
587
588         if (pref == plen)
589                 prefbuf[0] = '\0';
590         else
591                 snprintf(prefbuf, sizeof(prefbuf), "/%u", pref);
592
593         if (ulp == IPPROTO_ICMPV6)
594                 memset(portbuf, 0, sizeof(portbuf));
595         else {
596                 if (port == IPSEC_PORT_ANY)
597                         snprintf(portbuf, sizeof(portbuf), "[%s]", "any");
598                 else
599                         snprintf(portbuf, sizeof(portbuf), "[%u]", port);
600         }
601
602         snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf);
603
604         return buf;
605 }
606
607 static void
608 str_upperspec(ulp, p1, p2)
609         u_int ulp, p1, p2;
610 {
611         if (ulp == IPSEC_ULPROTO_ANY)
612                 printf("any");
613         else if (ulp == IPPROTO_ICMPV6) {
614                 printf("icmp6");
615                 if (!(p1 == IPSEC_PORT_ANY && p2 == IPSEC_PORT_ANY))
616                         printf(" %u,%u", p1, p2);
617         } else {
618                 struct protoent *ent;
619
620                 switch (ulp) {
621                 case IPPROTO_IPV4:
622                         printf("ip4");
623                         break;
624                 default:
625                         ent = getprotobynumber(ulp);
626                         if (ent)
627                                 printf("%s", ent->p_name);
628                         else
629                                 printf("%u", ulp);
630
631                         endprotoent();
632                         break;
633                 }
634         }
635 }
636
637 /*
638  * set "Mon Day Time Year" to buffer
639  */
640 static char *
641 str_time(t)
642         time_t t;
643 {
644         static char buf[128];
645
646         if (t == 0) {
647                 int i = 0;
648                 for (;i < 20;) buf[i++] = ' ';
649         } else {
650                 char *t0;
651                 t0 = ctime(&t);
652                 memcpy(buf, t0 + 4, 20);
653         }
654
655         buf[20] = '\0';
656
657         return(buf);
658 }
659
660 static void
661 str_lifetime_byte(x, str)
662         struct sadb_lifetime *x;
663         char *str;
664 {
665         double y;
666         char *unit;
667         int w;
668
669         if (x == NULL) {
670                 printf("\t%s: 0(bytes)", str);
671                 return;
672         }
673
674 #if 0
675         if ((x->sadb_lifetime_bytes) / 1024 / 1024) {
676                 y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024;
677                 unit = "M";
678                 w = 1;
679         } else if ((x->sadb_lifetime_bytes) / 1024) {
680                 y = (x->sadb_lifetime_bytes) * 1.0 / 1024;
681                 unit = "K";
682                 w = 1;
683         } else {
684                 y = (x->sadb_lifetime_bytes) * 1.0;
685                 unit = "";
686                 w = 0;
687         }
688 #else
689         y = (x->sadb_lifetime_bytes) * 1.0;
690         unit = "";
691         w = 0;
692 #endif
693         printf("\t%s: %.*f(%sbytes)", str, w, y, unit);
694 }