summaryrefslogtreecommitdiffstats
path: root/lib/libipsec/pfkey_dump.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libipsec/pfkey_dump.c')
-rw-r--r--lib/libipsec/pfkey_dump.c185
1 files changed, 124 insertions, 61 deletions
diff --git a/lib/libipsec/pfkey_dump.c b/lib/libipsec/pfkey_dump.c
index b7def24..6408651 100644
--- a/lib/libipsec/pfkey_dump.c
+++ b/lib/libipsec/pfkey_dump.c
@@ -1,3 +1,6 @@
+/* $FreeBSD$ */
+/* $KAME: pfkey_dump.c,v 1.19 2000/06/10 06:47:11 sakane Exp $ */
+
/*
* Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
* All rights reserved.
@@ -25,8 +28,6 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
- * $FreeBSD$
*/
#include <sys/types.h>
@@ -39,9 +40,6 @@
#include <netinet/in.h>
#include <netinet6/ipsec.h>
-#ifdef INET6
-#include <netinet6/in6.h>
-#endif
#include <arpa/inet.h>
#include <stdlib.h>
@@ -49,11 +47,13 @@
#include <stdio.h>
#include <string.h>
#include <time.h>
+#include <netdb.h>
#include "ipsec_strerror.h"
+#include "libpfkey.h"
-#define GETMSGSTR(str, num) \
-{ \
+#define GETMSGSTR(str, num) \
+do { \
if (sizeof((str)[0]) == 0 \
|| num >= sizeof(str)/sizeof((str)[0])) \
printf("%d ", (num)); \
@@ -61,15 +61,12 @@
printf("%d ", (num)); \
else \
printf("%s ", (str)[(num)]); \
-}
-
-#define GETAF(p) \
- (((struct sockaddr *)(p))->sa_family)
+} while (0)
-static char *_str_ipaddr __P((u_int family, caddr_t addr));
-static char *_str_prefport __P((u_int family, u_int pref, u_int port));
-static char *_str_time __P((time_t t));
-static void _str_lifetime_byte __P((struct sadb_lifetime *x, char *str));
+static char *str_ipaddr __P((struct sockaddr *));
+static char *str_prefport __P((u_int, u_int, u_int));
+static char *str_time __P((time_t));
+static void str_lifetime_byte __P((struct sadb_lifetime *, char *));
/*
* Must to be re-written about following strings.
@@ -151,6 +148,7 @@ pfkey_sadump(m)
{
caddr_t mhp[SADB_EXT_MAX + 1];
struct sadb_sa *m_sa;
+ struct sadb_x_sa2 *m_sa2;
struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts;
struct sadb_address *m_saddr, *m_daddr, *m_paddr;
struct sadb_key *m_auth, *m_enc;
@@ -168,6 +166,7 @@ pfkey_sadump(m)
}
m_sa = (struct sadb_sa *)mhp[SADB_EXT_SA];
+ m_sa2 = (struct sadb_x_sa2 *)mhp[SADB_X_EXT_SA2];
m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
m_lfts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT];
@@ -177,7 +176,7 @@ pfkey_sadump(m)
m_auth = (struct sadb_key *)mhp[SADB_EXT_KEY_AUTH];
m_enc = (struct sadb_key *)mhp[SADB_EXT_KEY_ENCRYPT];
m_sid = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_SRC];
- m_did = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_SRC];
+ m_did = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_DST];
m_sens = (struct sadb_sens *)mhp[SADB_EXT_SENSITIVITY];
/* source address */
@@ -185,34 +184,36 @@ pfkey_sadump(m)
printf("no ADDRESS_SRC extension.\n");
return;
}
- printf("%s ",
- _str_ipaddr(GETAF(m_saddr + 1), _INADDRBYSA(m_saddr + 1)));
+ printf("%s ", str_ipaddr((struct sockaddr *)(m_saddr + 1)));
/* destination address */
if (m_daddr == NULL) {
printf("no ADDRESS_DST extension.\n");
return;
}
- printf("%s ",
- _str_ipaddr(GETAF(m_daddr + 1), _INADDRBYSA(m_daddr + 1)));
+ printf("%s ", str_ipaddr((struct sockaddr *)(m_daddr + 1)));
/* SA type */
if (m_sa == NULL) {
printf("no SA extension.\n");
return;
}
+ if (m_sa2 == NULL) {
+ printf("no SA2 extension.\n");
+ return;
+ }
printf("\n\t");
GETMSGSTR(_str_satype, m->sadb_msg_satype);
printf("mode=");
- GETMSGSTR(_str_mode, m->sadb_msg_mode);
+ GETMSGSTR(_str_mode, m_sa2->sadb_x_sa2_mode);
- printf("spi=%u(0x%08x) replay=%u flags=0x%08x\n",
+ printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n",
(u_int32_t)ntohl(m_sa->sadb_sa_spi),
(u_int32_t)ntohl(m_sa->sadb_sa_spi),
- m_sa->sadb_sa_replay,
- m_sa->sadb_sa_flags);
+ (u_int32_t)m_sa2->sadb_x_sa2_reqid,
+ (u_int32_t)m_sa2->sadb_x_sa2_reqid);
/* encryption key */
if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
@@ -237,8 +238,13 @@ pfkey_sadump(m)
printf("\n");
}
+ /* replay windoe size & flags */
+ printf("\treplay=%u flags=0x%08x ",
+ m_sa->sadb_sa_replay,
+ m_sa->sadb_sa_flags);
+
/* state */
- printf("\tstate=");
+ printf("state=");
GETMSGSTR(_str_state, m_sa->sadb_sa_state);
printf("seq=%lu pid=%lu\n",
@@ -250,8 +256,8 @@ pfkey_sadump(m)
time_t tmp_time = time(0);
printf("\tcreated: %s",
- _str_time(m_lftc->sadb_lifetime_addtime));
- printf("\tcurrent: %s\n", _str_time(tmp_time));
+ str_time(m_lftc->sadb_lifetime_addtime));
+ printf("\tcurrent: %s\n", str_time(tmp_time));
printf("\tdiff: %lu(s)",
(u_long)(m_lftc->sadb_lifetime_addtime == 0 ?
0 : (tmp_time - m_lftc->sadb_lifetime_addtime)));
@@ -264,7 +270,7 @@ pfkey_sadump(m)
0 : m_lfts->sadb_lifetime_addtime));
printf("\tlast: %s",
- _str_time(m_lftc->sadb_lifetime_usetime));
+ str_time(m_lftc->sadb_lifetime_usetime));
printf("\thard: %lu(s)",
(u_long)(m_lfth == NULL ?
0 : m_lfth->sadb_lifetime_usetime));
@@ -272,9 +278,9 @@ pfkey_sadump(m)
(u_long)(m_lfts == NULL ?
0 : m_lfts->sadb_lifetime_usetime));
- _str_lifetime_byte(m_lftc, "current");
- _str_lifetime_byte(m_lfth, "hard");
- _str_lifetime_byte(m_lfts, "soft");
+ str_lifetime_byte(m_lftc, "current");
+ str_lifetime_byte(m_lfth, "hard");
+ str_lifetime_byte(m_lfts, "soft");
printf("\n");
printf("\tallocated: %lu",
@@ -288,7 +294,7 @@ pfkey_sadump(m)
}
/* XXX DEBUG */
- printf("\trefcnt=%d\n", m->sadb_msg_reserved);
+ printf("\trefcnt=%u\n", m->sadb_msg_reserved);
return;
}
@@ -297,9 +303,12 @@ void
pfkey_spdump(m)
struct sadb_msg *m;
{
+ char pbuf[NI_MAXSERV];
caddr_t mhp[SADB_EXT_MAX + 1];
struct sadb_address *m_saddr, *m_daddr;
struct sadb_x_policy *m_xpl;
+ struct sockaddr *sa;
+ u_int16_t port;
/* check pfkey message. */
if (pfkey_align(m, mhp)) {
@@ -320,25 +329,49 @@ pfkey_spdump(m)
printf("no ADDRESS_SRC extension.\n");
return;
}
- printf("%s%s ",
- _str_ipaddr(GETAF(m_saddr + 1), _INADDRBYSA(m_saddr + 1)),
- _str_prefport(GETAF(m_saddr + 1),
- m_saddr->sadb_address_prefixlen,
- _INPORTBYSA(m_saddr + 1)));
+ sa = (struct sockaddr *)(m_saddr + 1);
+ switch (sa->sa_family) {
+ case AF_INET:
+ case AF_INET6:
+ if (getnameinfo(sa, sa->sa_len, NULL, 0, pbuf, sizeof(pbuf),
+ NI_NUMERICSERV) != 0)
+ port = 0; /*XXX*/
+ else
+ port = atoi(pbuf);
+ printf("%s%s ", str_ipaddr(sa),
+ str_prefport(sa->sa_family,
+ m_saddr->sadb_address_prefixlen, port));
+ break;
+ default:
+ printf("unknown-af ");
+ break;
+ }
/* destination address */
if (m_daddr == NULL) {
printf("no ADDRESS_DST extension.\n");
return;
}
- printf("%s%s ",
- _str_ipaddr(GETAF(m_daddr + 1), _INADDRBYSA(m_daddr + 1)),
- _str_prefport(GETAF(m_daddr + 1),
- m_daddr->sadb_address_prefixlen,
- _INPORTBYSA(m_daddr + 1)));
+ sa = (struct sockaddr *)(m_daddr + 1);
+ switch (sa->sa_family) {
+ case AF_INET:
+ case AF_INET6:
+ if (getnameinfo(sa, sa->sa_len, NULL, 0, pbuf, sizeof(pbuf),
+ NI_NUMERICSERV) != 0)
+ port = 0; /*XXX*/
+ else
+ port = atoi(pbuf);
+ printf("%s%s ", str_ipaddr(sa),
+ str_prefport(sa->sa_family,
+ m_daddr->sadb_address_prefixlen, port));
+ break;
+ default:
+ printf("unknown-af ");
+ break;
+ }
/* upper layer protocol */
- if (m_saddr->sadb_address_proto != m_saddr->sadb_address_proto) {
+ if (m_saddr->sadb_address_proto != m_daddr->sadb_address_proto) {
printf("upper layer protocol mismatched.\n");
return;
}
@@ -362,12 +395,13 @@ pfkey_spdump(m)
free(d_xpl);
}
- printf("\tseq=%ld pid=%ld\n",
+ printf("\tspid=%ld seq=%ld pid=%ld\n",
+ (u_long)m_xpl->sadb_x_policy_id,
(u_long)m->sadb_msg_seq,
(u_long)m->sadb_msg_pid);
/* XXX TEST */
- printf("\trefcnt=%d\n", m->sadb_msg_reserved);
+ printf("\trefcnt=%u\n", m->sadb_msg_reserved);
return;
}
@@ -376,35 +410,48 @@ pfkey_spdump(m)
* set "ipaddress" to buffer.
*/
static char *
-_str_ipaddr(family, addr)
- u_int family;
- caddr_t addr;
+str_ipaddr(sa)
+ struct sockaddr *sa;
{
- static char buf[128];
- char addrbuf[128];
+ static char buf[NI_MAXHOST];
+#ifdef NI_WITHSCOPEID
+ const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
+#else
+ const int niflag = NI_NUMERICHOST;
+#endif
- if (addr == NULL)
+ if (sa == NULL)
return "";
- inet_ntop(family, addr, addrbuf, sizeof(addrbuf));
-
- snprintf(buf, sizeof(buf), "%s", addrbuf);
-
- return buf;
+ if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0, niflag) == 0)
+ return buf;
+ return NULL;
}
/*
* set "/prefix[port number]" to buffer.
*/
static char *
-_str_prefport(family, pref, port)
+str_prefport(family, pref, port)
u_int family, pref, port;
{
static char buf[128];
char prefbuf[10];
char portbuf[10];
+ int plen;
+
+ switch (family) {
+ case AF_INET:
+ plen = sizeof(struct in_addr) << 3;
+ break;
+ case AF_INET6:
+ plen = sizeof(struct in6_addr) << 3;
+ break;
+ default:
+ return "?";
+ }
- if (pref == (_INALENBYAF(family) << 3))
+ if (pref == plen)
prefbuf[0] = '\0';
else
snprintf(prefbuf, sizeof(prefbuf), "/%u", pref);
@@ -412,7 +459,7 @@ _str_prefport(family, pref, port)
if (port == IPSEC_PORT_ANY)
snprintf(portbuf, sizeof(portbuf), "[%s]", "any");
else
- snprintf(portbuf, sizeof(portbuf), "[%u]", ntohs(port));
+ snprintf(portbuf, sizeof(portbuf), "[%u]", port);
snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf);
@@ -423,7 +470,7 @@ _str_prefport(family, pref, port)
* set "Mon Day Time Year" to buffer
*/
static char *
-_str_time(t)
+str_time(t)
time_t t;
{
static char buf[128];
@@ -443,7 +490,7 @@ _str_time(t)
}
static void
-_str_lifetime_byte(x, str)
+str_lifetime_byte(x, str)
struct sadb_lifetime *x;
char *str;
{
@@ -456,8 +503,24 @@ _str_lifetime_byte(x, str)
return;
}
+#if 0
+ if ((x->sadb_lifetime_bytes) / 1024 / 1024) {
+ y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024;
+ unit = "M";
+ w = 1;
+ } else if ((x->sadb_lifetime_bytes) / 1024) {
+ y = (x->sadb_lifetime_bytes) * 1.0 / 1024;
+ unit = "K";
+ w = 1;
+ } else {
+ y = (x->sadb_lifetime_bytes) * 1.0;
+ unit = "";
+ w = 0;
+ }
+#else
y = (x->sadb_lifetime_bytes) * 1.0;
unit = "";
w = 0;
+#endif
printf("\t%s: %.*f(%sbytes)", str, w, y, unit);
}
OpenPOWER on IntegriCloud