162583Sitojun/* $FreeBSD$ */ 2122108Sume/* $KAME: setkey.c,v 1.28 2003/06/27 07:15:45 itojun Exp $ */ 362583Sitojun 455505Sshin/* 555505Sshin * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. 655505Sshin * All rights reserved. 762583Sitojun * 855505Sshin * Redistribution and use in source and binary forms, with or without 955505Sshin * modification, are permitted provided that the following conditions 1055505Sshin * are met: 1155505Sshin * 1. Redistributions of source code must retain the above copyright 1255505Sshin * notice, this list of conditions and the following disclaimer. 1355505Sshin * 2. Redistributions in binary form must reproduce the above copyright 1455505Sshin * notice, this list of conditions and the following disclaimer in the 1555505Sshin * documentation and/or other materials provided with the distribution. 1655505Sshin * 3. Neither the name of the project nor the names of its contributors 1755505Sshin * may be used to endorse or promote products derived from this software 1855505Sshin * without specific prior written permission. 1962583Sitojun * 2055505Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2155505Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2255505Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2355505Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2455505Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2555505Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2655505Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2755505Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2855505Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2955505Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3055505Sshin * SUCH DAMAGE. 3155505Sshin */ 3255505Sshin 3355505Sshin#include <sys/types.h> 3455505Sshin#include <sys/param.h> 3555505Sshin#include <sys/socket.h> 3655505Sshin#include <sys/time.h> 3755505Sshin#include <err.h> 3855505Sshin#include <net/route.h> 3955505Sshin#include <netinet/in.h> 4055505Sshin#include <net/pfkeyv2.h> 41171135Sgnn#include <netipsec/keydb.h> 42171135Sgnn#include <netipsec/key_debug.h> 43171135Sgnn#include <netipsec/ipsec.h> 4455505Sshin 4555505Sshin#include <stdio.h> 4655505Sshin#include <stdlib.h> 4755505Sshin#include <limits.h> 4855505Sshin#include <string.h> 4955505Sshin#include <ctype.h> 5055505Sshin#include <unistd.h> 5155505Sshin#include <errno.h> 5255505Sshin#include <netdb.h> 5355505Sshin 5462583Sitojun#include "libpfkey.h" 5555505Sshin 56173412Skevlovoid usage(void); 57173412Skevloint main(int, char **); 58173412Skevloint get_supported(void); 59173412Skevlovoid sendkeyshort(u_int); 60173412Skevlovoid promisc(void); 61173412Skevloint sendkeymsg(char *, size_t); 62173412Skevloint postproc(struct sadb_msg *, int); 63173412Skevloconst char *numstr(int); 64173412Skevlovoid shortdump_hdr(void); 65173412Skevlovoid shortdump(struct sadb_msg *); 66173412Skevlostatic void printdate(void); 67173412Skevlostatic int32_t gmt2local(time_t); 6862583Sitojun 6955505Sshin#define MODE_SCRIPT 1 7055505Sshin#define MODE_CMDDUMP 2 7155505Sshin#define MODE_CMDFLUSH 3 7262583Sitojun#define MODE_PROMISC 4 7355505Sshin 7455505Sshinint so; 7555505Sshin 7662583Sitojunint f_forever = 0; 7762583Sitojunint f_all = 0; 7862583Sitojunint f_verbose = 0; 7962583Sitojunint f_mode = 0; 8062583Sitojunint f_cmddump = 0; 8162583Sitojunint f_policy = 0; 8262583Sitojunint f_hexdump = 0; 8378064Sumeint f_tflag = 0; 8478064Sumestatic time_t thiszone; 8578064Sume 8662583Sitojunextern int lineno; 8755505Sshin 88173412Skevloextern int parse(FILE **); 8955505Sshin 9055505Sshinvoid 91121155Sumeusage() 9255505Sshin{ 93122108Sume 94122108Sume printf("usage: setkey [-v] -c\n"); 95122108Sume printf(" setkey [-v] -f filename\n"); 96122108Sume printf(" setkey [-Palv] -D\n"); 97122108Sume printf(" setkey [-Pv] -F\n"); 98122108Sume printf(" setkey [-h] -x\n"); 9962583Sitojun exit(1); 10055505Sshin} 10155505Sshin 10255505Sshinint 10355505Sshinmain(ac, av) 10455505Sshin int ac; 10555505Sshin char **av; 10655505Sshin{ 10755505Sshin FILE *fp = stdin; 10855505Sshin int c; 10955505Sshin 110121155Sume if (ac == 1) { 111121155Sume usage(); 112121155Sume /* NOTREACHED */ 113121155Sume } 11455505Sshin 11578064Sume thiszone = gmt2local(0); 11678064Sume 11778064Sume while ((c = getopt(ac, av, "acdf:hlvxDFP")) != -1) { 11855505Sshin switch (c) { 11955505Sshin case 'c': 12055505Sshin f_mode = MODE_SCRIPT; 12155505Sshin fp = stdin; 12255505Sshin break; 12355505Sshin case 'f': 12455505Sshin f_mode = MODE_SCRIPT; 12555505Sshin if ((fp = fopen(optarg, "r")) == NULL) { 12655505Sshin err(-1, "fopen"); 12755505Sshin /*NOTREACHED*/ 12855505Sshin } 12955505Sshin break; 13055505Sshin case 'D': 13155505Sshin f_mode = MODE_CMDDUMP; 13255505Sshin break; 13355505Sshin case 'F': 13455505Sshin f_mode = MODE_CMDFLUSH; 13555505Sshin break; 13655505Sshin case 'a': 13755505Sshin f_all = 1; 13855505Sshin break; 13955505Sshin case 'l': 14055505Sshin f_forever = 1; 14155505Sshin break; 14255505Sshin case 'h': 14355505Sshin f_hexdump = 1; 14455505Sshin break; 14555505Sshin case 'x': 14662583Sitojun f_mode = MODE_PROMISC; 14778064Sume f_tflag++; 14862583Sitojun break; 14955505Sshin case 'P': 15055505Sshin f_policy = 1; 15155505Sshin break; 15255505Sshin case 'v': 15355505Sshin f_verbose = 1; 15455505Sshin break; 15555505Sshin default: 156121155Sume usage(); 15755505Sshin /*NOTREACHED*/ 15855505Sshin } 15955505Sshin } 16055505Sshin 161122108Sume so = pfkey_open(); 162122108Sume if (so < 0) { 163122108Sume perror("pfkey_open"); 164122108Sume exit(1); 165122108Sume } 166122108Sume 16755505Sshin switch (f_mode) { 16855505Sshin case MODE_CMDDUMP: 16955505Sshin sendkeyshort(f_policy ? SADB_X_SPDDUMP: SADB_DUMP); 17055505Sshin break; 17155505Sshin case MODE_CMDFLUSH: 17255505Sshin sendkeyshort(f_policy ? SADB_X_SPDFLUSH: SADB_FLUSH); 17355505Sshin break; 17455505Sshin case MODE_SCRIPT: 17555505Sshin if (get_supported() < 0) { 17655505Sshin errx(-1, "%s", ipsec_strerror()); 17755505Sshin /*NOTREACHED*/ 17855505Sshin } 17962583Sitojun if (parse(&fp)) 18062583Sitojun exit (1); 18155505Sshin break; 18262583Sitojun case MODE_PROMISC: 18362583Sitojun promisc(); 18462583Sitojun /*NOTREACHED*/ 18555505Sshin default: 186121155Sume usage(); 18762583Sitojun /*NOTREACHED*/ 18855505Sshin } 18955505Sshin 19055505Sshin exit(0); 19155505Sshin} 19255505Sshin 19355505Sshinint 19455505Sshinget_supported() 19555505Sshin{ 19655505Sshin 19778064Sume if (pfkey_send_register(so, SADB_SATYPE_UNSPEC) < 0) 19855505Sshin return -1; 19955505Sshin 20055505Sshin if (pfkey_recv_register(so) < 0) 20155505Sshin return -1; 20255505Sshin 20355505Sshin return 0; 20455505Sshin} 20555505Sshin 20655505Sshinvoid 20755505Sshinsendkeyshort(type) 20855505Sshin u_int type; 20955505Sshin{ 210122108Sume struct sadb_msg msg; 21155505Sshin 212122108Sume msg.sadb_msg_version = PF_KEY_V2; 213122108Sume msg.sadb_msg_type = type; 214122108Sume msg.sadb_msg_errno = 0; 215122108Sume msg.sadb_msg_satype = SADB_SATYPE_UNSPEC; 216122108Sume msg.sadb_msg_len = PFKEY_UNIT64(sizeof(msg)); 217122108Sume msg.sadb_msg_reserved = 0; 218122108Sume msg.sadb_msg_seq = 0; 219122108Sume msg.sadb_msg_pid = getpid(); 22055505Sshin 221122108Sume sendkeymsg((char *)&msg, sizeof(msg)); 22255505Sshin 22355505Sshin return; 22455505Sshin} 22555505Sshin 22655505Sshinvoid 22755505Sshinpromisc() 22855505Sshin{ 229122108Sume struct sadb_msg msg; 23055505Sshin u_char rbuf[1024 * 32]; /* XXX: Enough ? Should I do MSG_PEEK ? */ 231122108Sume ssize_t l; 23255505Sshin 233122108Sume msg.sadb_msg_version = PF_KEY_V2; 234122108Sume msg.sadb_msg_type = SADB_X_PROMISC; 235122108Sume msg.sadb_msg_errno = 0; 236122108Sume msg.sadb_msg_satype = 1; 237122108Sume msg.sadb_msg_len = PFKEY_UNIT64(sizeof(msg)); 238122108Sume msg.sadb_msg_reserved = 0; 239122108Sume msg.sadb_msg_seq = 0; 240122108Sume msg.sadb_msg_pid = getpid(); 24155505Sshin 242122108Sume if ((l = send(so, &msg, sizeof(msg), 0)) < 0) { 24355505Sshin err(1, "send"); 24455505Sshin /*NOTREACHED*/ 24555505Sshin } 24655505Sshin 24755505Sshin while (1) { 24855505Sshin struct sadb_msg *base; 24955505Sshin 250121155Sume if ((l = recv(so, rbuf, sizeof(*base), MSG_PEEK)) < 0) { 25155505Sshin err(1, "recv"); 25255505Sshin /*NOTREACHED*/ 25355505Sshin } 25455505Sshin 255121155Sume if (l != sizeof(*base)) 25655505Sshin continue; 25755505Sshin 25855505Sshin base = (struct sadb_msg *)rbuf; 259121155Sume if ((l = recv(so, rbuf, PFKEY_UNUNIT64(base->sadb_msg_len), 26055505Sshin 0)) < 0) { 26155505Sshin err(1, "recv"); 26255505Sshin /*NOTREACHED*/ 26355505Sshin } 26478064Sume printdate(); 26555505Sshin if (f_hexdump) { 26655505Sshin int i; 267121155Sume for (i = 0; i < l; i++) { 26855505Sshin if (i % 16 == 0) 26955505Sshin printf("%08x: ", i); 27055505Sshin printf("%02x ", rbuf[i] & 0xff); 27155505Sshin if (i % 16 == 15) 27255505Sshin printf("\n"); 27355505Sshin } 274121155Sume if (l % 16) 27555505Sshin printf("\n"); 27655505Sshin } 27755505Sshin /* adjust base pointer for promisc mode */ 27855505Sshin if (base->sadb_msg_type == SADB_X_PROMISC) { 279122108Sume if ((ssize_t)sizeof(*base) < l) 28055505Sshin base++; 28155505Sshin else 28255505Sshin base = NULL; 28355505Sshin } 28455505Sshin if (base) { 28555505Sshin kdebug_sadb(base); 28655505Sshin printf("\n"); 28755505Sshin fflush(stdout); 28855505Sshin } 28955505Sshin } 29055505Sshin} 29155505Sshin 29255505Sshinint 293122108Sumesendkeymsg(buf, len) 294122108Sume char *buf; 295122108Sume size_t len; 29655505Sshin{ 29755505Sshin u_char rbuf[1024 * 32]; /* XXX: Enough ? Should I do MSG_PEEK ? */ 298122108Sume ssize_t l; 29955505Sshin struct sadb_msg *msg; 30055505Sshin 30155505Sshin { 30255505Sshin struct timeval tv; 30355505Sshin tv.tv_sec = 1; 30455505Sshin tv.tv_usec = 0; 30555505Sshin if (setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) { 30655505Sshin perror("setsockopt"); 30755505Sshin goto end; 30855505Sshin } 30955505Sshin } 31055505Sshin 31155505Sshin if (f_forever) 31255505Sshin shortdump_hdr(); 31355505Sshinagain: 31462583Sitojun if (f_verbose) { 315122108Sume kdebug_sadb((struct sadb_msg *)buf); 31662583Sitojun printf("\n"); 31762583Sitojun } 318122108Sume if (f_hexdump) { 319122108Sume int i; 320122108Sume for (i = 0; i < len; i++) { 321122108Sume if (i % 16 == 0) 322122108Sume printf("%08x: ", i); 323122108Sume printf("%02x ", buf[i] & 0xff); 324122108Sume if (i % 16 == 15) 325122108Sume printf("\n"); 326122108Sume } 327122108Sume if (len % 16) 328122108Sume printf("\n"); 329122108Sume } 33055505Sshin 331122108Sume if ((l = send(so, buf, len, 0)) < 0) { 33255505Sshin perror("send"); 33355505Sshin goto end; 33455505Sshin } 33555505Sshin 33655505Sshin msg = (struct sadb_msg *)rbuf; 33755505Sshin do { 338121155Sume if ((l = recv(so, rbuf, sizeof(rbuf), 0)) < 0) { 33955505Sshin perror("recv"); 34055505Sshin goto end; 34155505Sshin } 34255505Sshin 343121155Sume if (PFKEY_UNUNIT64(msg->sadb_msg_len) != l) { 34455505Sshin warnx("invalid keymsg length"); 34555505Sshin break; 34655505Sshin } 34755505Sshin 34862583Sitojun if (f_verbose) { 34955505Sshin kdebug_sadb((struct sadb_msg *)rbuf); 35062583Sitojun printf("\n"); 35162583Sitojun } 352121155Sume if (postproc(msg, l) < 0) 35355505Sshin break; 35455505Sshin } while (msg->sadb_msg_errno || msg->sadb_msg_seq); 35555505Sshin 35655505Sshin if (f_forever) { 35755505Sshin fflush(stdout); 35855505Sshin sleep(1); 35955505Sshin goto again; 36055505Sshin } 36155505Sshin 36255505Sshinend: 36355505Sshin return(0); 36455505Sshin} 36555505Sshin 36655505Sshinint 36755505Sshinpostproc(msg, len) 36855505Sshin struct sadb_msg *msg; 36955505Sshin int len; 37055505Sshin{ 37155505Sshin 37255505Sshin if (msg->sadb_msg_errno != 0) { 37355505Sshin char inf[80]; 374121155Sume const char *errmsg = NULL; 37555505Sshin 37655505Sshin if (f_mode == MODE_SCRIPT) 37755505Sshin snprintf(inf, sizeof(inf), "The result of line %d: ", lineno); 37855505Sshin else 37955505Sshin inf[0] = '\0'; 38055505Sshin 38155505Sshin switch (msg->sadb_msg_errno) { 38255505Sshin case ENOENT: 38355505Sshin switch (msg->sadb_msg_type) { 38455505Sshin case SADB_DELETE: 38555505Sshin case SADB_GET: 38655505Sshin case SADB_X_SPDDELETE: 38755505Sshin errmsg = "No entry"; 38855505Sshin break; 38955505Sshin case SADB_DUMP: 39055505Sshin errmsg = "No SAD entries"; 39155505Sshin break; 39255505Sshin case SADB_X_SPDDUMP: 39355505Sshin errmsg = "No SPD entries"; 39455505Sshin break; 39555505Sshin } 39655505Sshin break; 39755505Sshin default: 39855505Sshin errmsg = strerror(msg->sadb_msg_errno); 39955505Sshin } 40055505Sshin printf("%s%s.\n", inf, errmsg); 40155505Sshin return(-1); 40255505Sshin } 40355505Sshin 40455505Sshin switch (msg->sadb_msg_type) { 40555505Sshin case SADB_GET: 40655505Sshin pfkey_sadump(msg); 40755505Sshin break; 40855505Sshin 40955505Sshin case SADB_DUMP: 41055505Sshin /* filter out DEAD SAs */ 41155505Sshin if (!f_all) { 41255505Sshin caddr_t mhp[SADB_EXT_MAX + 1]; 41355505Sshin struct sadb_sa *sa; 41455505Sshin pfkey_align(msg, mhp); 41555505Sshin pfkey_check(mhp); 41655505Sshin if ((sa = (struct sadb_sa *)mhp[SADB_EXT_SA]) != NULL) { 41755505Sshin if (sa->sadb_sa_state == SADB_SASTATE_DEAD) 41855505Sshin break; 41955505Sshin } 42055505Sshin } 42155505Sshin if (f_forever) 42255505Sshin shortdump(msg); 42355505Sshin else 42455505Sshin pfkey_sadump(msg); 42555505Sshin msg = (struct sadb_msg *)((caddr_t)msg + 42655505Sshin PFKEY_UNUNIT64(msg->sadb_msg_len)); 42762583Sitojun if (f_verbose) { 42855505Sshin kdebug_sadb((struct sadb_msg *)msg); 42962583Sitojun printf("\n"); 43062583Sitojun } 43155505Sshin break; 43255505Sshin 43355505Sshin case SADB_X_SPDDUMP: 43455505Sshin pfkey_spdump(msg); 43555505Sshin if (msg->sadb_msg_seq == 0) break; 43655505Sshin msg = (struct sadb_msg *)((caddr_t)msg + 43755505Sshin PFKEY_UNUNIT64(msg->sadb_msg_len)); 43862583Sitojun if (f_verbose) { 43955505Sshin kdebug_sadb((struct sadb_msg *)msg); 44062583Sitojun printf("\n"); 44162583Sitojun } 44255505Sshin break; 44355505Sshin } 44455505Sshin 44555505Sshin return(0); 44655505Sshin} 44755505Sshin 44855505Sshin/*------------------------------------------------------------*/ 449121155Sumestatic const char *satype[] = { 45055505Sshin NULL, NULL, "ah", "esp" 45155505Sshin}; 452121155Sumestatic const char *sastate[] = { 45355505Sshin "L", "M", "D", "d" 45455505Sshin}; 455121155Sumestatic const char *ipproto[] = { 45655505Sshin/*0*/ "ip", "icmp", "igmp", "ggp", "ip4", 45755505Sshin NULL, "tcp", NULL, "egp", NULL, 45855505Sshin/*10*/ NULL, NULL, NULL, NULL, NULL, 45955505Sshin NULL, NULL, "udp", NULL, NULL, 46055505Sshin/*20*/ NULL, NULL, "idp", NULL, NULL, 46155505Sshin NULL, NULL, NULL, NULL, "tp", 46255505Sshin/*30*/ NULL, NULL, NULL, NULL, NULL, 46355505Sshin NULL, NULL, NULL, NULL, NULL, 46455505Sshin/*40*/ NULL, "ip6", NULL, "rt6", "frag6", 46555505Sshin NULL, "rsvp", "gre", NULL, NULL, 46655505Sshin/*50*/ "esp", "ah", NULL, NULL, NULL, 46755505Sshin NULL, NULL, NULL, "icmp6", "none", 46855505Sshin/*60*/ "dst6", 46955505Sshin}; 47055505Sshin 47155505Sshin#define STR_OR_ID(x, tab) \ 47255505Sshin (((x) < sizeof(tab)/sizeof(tab[0]) && tab[(x)]) ? tab[(x)] : numstr(x)) 47355505Sshin 47455505Sshinconst char * 47555505Sshinnumstr(x) 47655505Sshin int x; 47755505Sshin{ 47855505Sshin static char buf[20]; 47955505Sshin snprintf(buf, sizeof(buf), "#%d", x); 48055505Sshin return buf; 48155505Sshin} 48255505Sshin 48355505Sshinvoid 48455505Sshinshortdump_hdr() 48555505Sshin{ 48655505Sshin printf("%-4s %-3s %-1s %-8s %-7s %s -> %s\n", 48755505Sshin "time", "p", "s", "spi", "ltime", "src", "dst"); 48855505Sshin} 48955505Sshin 49055505Sshinvoid 49155505Sshinshortdump(msg) 49255505Sshin struct sadb_msg *msg; 49355505Sshin{ 49455505Sshin caddr_t mhp[SADB_EXT_MAX + 1]; 495113552Ssumikawa char buf[NI_MAXHOST], pbuf[NI_MAXSERV]; 49655505Sshin struct sadb_sa *sa; 49755505Sshin struct sadb_address *saddr; 49855505Sshin struct sadb_lifetime *lts, *lth, *ltc; 49955505Sshin struct sockaddr *s; 50055505Sshin u_int t; 50155505Sshin time_t cur = time(0); 50255505Sshin 50355505Sshin pfkey_align(msg, mhp); 50455505Sshin pfkey_check(mhp); 50555505Sshin 50655505Sshin printf("%02lu%02lu", (u_long)(cur % 3600) / 60, (u_long)(cur % 60)); 50755505Sshin 50855505Sshin printf(" %-3s", STR_OR_ID(msg->sadb_msg_satype, satype)); 50955505Sshin 51055505Sshin if ((sa = (struct sadb_sa *)mhp[SADB_EXT_SA]) != NULL) { 51155505Sshin printf(" %-1s", STR_OR_ID(sa->sadb_sa_state, sastate)); 51255505Sshin printf(" %08x", (u_int32_t)ntohl(sa->sadb_sa_spi)); 51355505Sshin } else 51455505Sshin printf("%-1s %-8s", "?", "?"); 51555505Sshin 51655505Sshin lts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT]; 51755505Sshin lth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD]; 51855505Sshin ltc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT]; 51955505Sshin if (lts && lth && ltc) { 52055505Sshin if (ltc->sadb_lifetime_addtime == 0) 52155505Sshin t = (u_long)0; 52255505Sshin else 52355505Sshin t = (u_long)(cur - ltc->sadb_lifetime_addtime); 52455505Sshin if (t >= 1000) 525121155Sume strlcpy(buf, " big/", sizeof(buf)); 52655505Sshin else 52755505Sshin snprintf(buf, sizeof(buf), " %3lu/", (u_long)t); 52855505Sshin printf("%s", buf); 52955505Sshin 53055505Sshin t = (u_long)lth->sadb_lifetime_addtime; 53155505Sshin if (t >= 1000) 532121155Sume strlcpy(buf, "big", sizeof(buf)); 53355505Sshin else 53455505Sshin snprintf(buf, sizeof(buf), "%-3lu", (u_long)t); 53555505Sshin printf("%s", buf); 53655505Sshin } else 53778064Sume printf(" ??\?/???"); /* backslash to avoid trigraph ??/ */ 53855505Sshin 53955505Sshin printf(" "); 54055505Sshin 54155505Sshin if ((saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]) != NULL) { 54255505Sshin if (saddr->sadb_address_proto) 54355505Sshin printf("%s ", STR_OR_ID(saddr->sadb_address_proto, ipproto)); 54455505Sshin s = (struct sockaddr *)(saddr + 1); 54555505Sshin getnameinfo(s, s->sa_len, buf, sizeof(buf), 54655505Sshin pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV); 54755505Sshin if (strcmp(pbuf, "0") != 0) 54855505Sshin printf("%s[%s]", buf, pbuf); 54955505Sshin else 55055505Sshin printf("%s", buf); 55155505Sshin } else 55255505Sshin printf("?"); 55355505Sshin 55455505Sshin printf(" -> "); 55555505Sshin 55655505Sshin if ((saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]) != NULL) { 55755505Sshin if (saddr->sadb_address_proto) 55855505Sshin printf("%s ", STR_OR_ID(saddr->sadb_address_proto, ipproto)); 55955505Sshin 56055505Sshin s = (struct sockaddr *)(saddr + 1); 56155505Sshin getnameinfo(s, s->sa_len, buf, sizeof(buf), 56255505Sshin pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV); 56355505Sshin if (strcmp(pbuf, "0") != 0) 56455505Sshin printf("%s[%s]", buf, pbuf); 56555505Sshin else 56655505Sshin printf("%s", buf); 56755505Sshin } else 56855505Sshin printf("?"); 56955505Sshin 57055505Sshin printf("\n"); 57155505Sshin} 57278064Sume 57378064Sume/* From: tcpdump(1):gmt2local.c and util.c */ 57478064Sume/* 57578064Sume * Print the timestamp 57678064Sume */ 57778064Sumestatic void 57878064Sumeprintdate() 57978064Sume{ 58078064Sume struct timeval tp; 58178064Sume int s; 58278064Sume 58378064Sume if (gettimeofday(&tp, NULL) == -1) { 58478064Sume perror("gettimeofday"); 58578064Sume return; 58678064Sume } 58778064Sume 58878064Sume if (f_tflag == 1) { 58978064Sume /* Default */ 59078064Sume s = (tp.tv_sec + thiszone ) % 86400; 59178064Sume (void)printf("%02d:%02d:%02d.%06u ", 59278064Sume s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tp.tv_usec); 59378064Sume } else if (f_tflag > 1) { 59478064Sume /* Unix timeval style */ 59578064Sume (void)printf("%u.%06u ", 59678064Sume (u_int32_t)tp.tv_sec, (u_int32_t)tp.tv_usec); 59778064Sume } 59878064Sume 59978064Sume printf("\n"); 60078064Sume} 60178064Sume 60278064Sume/* 60378064Sume * Returns the difference between gmt and local time in seconds. 60478064Sume * Use gmtime() and localtime() to keep things simple. 60578064Sume */ 60678064Sumeint32_t 60778064Sumegmt2local(time_t t) 60878064Sume{ 60978064Sume register int dt, dir; 61078064Sume register struct tm *gmt, *loc; 61178064Sume struct tm sgmt; 61278064Sume 61378064Sume if (t == 0) 61478064Sume t = time(NULL); 61578064Sume gmt = &sgmt; 61678064Sume *gmt = *gmtime(&t); 61778064Sume loc = localtime(&t); 61878064Sume dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 + 61978064Sume (loc->tm_min - gmt->tm_min) * 60; 62078064Sume 62178064Sume /* 62278064Sume * If the year or julian day is different, we span 00:00 GMT 62378064Sume * and must add or subtract a day. Check the year first to 62478064Sume * avoid problems when the julian day wraps. 62578064Sume */ 62678064Sume dir = loc->tm_year - gmt->tm_year; 62778064Sume if (dir == 0) 62878064Sume dir = loc->tm_yday - gmt->tm_yday; 62978064Sume dt += dir * 24 * 60 * 60; 63078064Sume 63178064Sume return (dt); 63278064Sume} 633