summaryrefslogtreecommitdiffstats
path: root/contrib/tcpdump/print-esp.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/tcpdump/print-esp.c')
-rw-r--r--contrib/tcpdump/print-esp.c109
1 files changed, 55 insertions, 54 deletions
diff --git a/contrib/tcpdump/print-esp.c b/contrib/tcpdump/print-esp.c
index 0ca0cfa..bf125eb 100644
--- a/contrib/tcpdump/print-esp.c
+++ b/contrib/tcpdump/print-esp.c
@@ -23,7 +23,7 @@
#ifndef lint
static const char rcsid[] _U_ =
- "@(#) $Header: /tcpdump/master/tcpdump/print-esp.c,v 1.44.2.4 2003/11/19 05:36:40 guy Exp $ (LBL)";
+ "@(#) $Header: /tcpdump/master/tcpdump/print-esp.c,v 1.55 2004/07/21 22:00:11 guy Exp $ (LBL)";
#endif
#ifdef HAVE_CONFIG_H
@@ -50,11 +50,7 @@ static const char rcsid[] _U_ =
#include "ip6.h"
#endif
-#if defined(__MINGW32__) || defined(__WATCOMC__)
-extern char *strsep(char **stringp, const char *delim); /* Missing/strsep.c */
-#endif
-
-#include "interface.h"
+#include "netdissect.h"
#include "addrtoname.h"
#include "extract.h"
@@ -83,10 +79,8 @@ struct sa_list {
int secretlen;
};
-static struct sa_list *sa_list_head = NULL;
-static struct sa_list *sa_default = NULL;
-
-static void esp_print_addsa(struct sa_list *sa, int sa_def)
+static void esp_print_addsa(netdissect_options *ndo,
+ struct sa_list *sa, int sa_def)
{
/* copy the "sa" */
@@ -94,19 +88,19 @@ static void esp_print_addsa(struct sa_list *sa, int sa_def)
nsa = (struct sa_list *)malloc(sizeof(struct sa_list));
if (nsa == NULL)
- error("ran out of memory to allocate sa structure");
+ (*ndo->ndo_error)(ndo, "ran out of memory to allocate sa structure");
*nsa = *sa;
if (sa_def)
- sa_default = nsa;
+ ndo->ndo_sa_default = nsa;
- nsa->next = sa_list_head;
- sa_list_head = nsa;
+ nsa->next = ndo->ndo_sa_list_head;
+ ndo->ndo_sa_list_head = nsa;
}
-static int hexdigit(char hex)
+static int hexdigit(netdissect_options *ndo, char hex)
{
if (hex >= '0' && hex <= '9')
return (hex - '0');
@@ -115,16 +109,16 @@ static int hexdigit(char hex)
else if (hex >= 'a' && hex <= 'f')
return (hex - 'a' + 10);
else {
- printf("invalid hex digit %c in espsecret\n", hex);
+ (*ndo->ndo_error)(ndo, "invalid hex digit %c in espsecret\n", hex);
return 0;
}
}
-static int hex2byte(char *hexstring)
+static int hex2byte(netdissect_options *ndo, char *hexstring)
{
int byte;
- byte = (hexdigit(hexstring[0]) << 4) + hexdigit(hexstring[1]);
+ byte = (hexdigit(ndo, hexstring[0]) << 4) + hexdigit(ndo, hexstring[1]);
return byte;
}
@@ -135,7 +129,7 @@ static int hex2byte(char *hexstring)
* causes us to go read from this file instead.
*
*/
-static void esp_print_decode_onesecret(char *line)
+static void esp_print_decode_onesecret(netdissect_options *ndo, char *line)
{
struct sa_list sa1;
int sa_def;
@@ -177,7 +171,7 @@ static void esp_print_decode_onesecret(char *line)
if (fileline[0] == '#') continue;
if (fileline[0] == '\0') continue;
- esp_print_decode_onesecret(fileline);
+ esp_print_decode_onesecret(ndo, fileline);
}
fclose(secretfile);
@@ -196,7 +190,7 @@ static void esp_print_decode_onesecret(char *line)
spino = strtoul(spistr, &foo, 0);
if (spistr == foo || !spikey) {
- printf("print_esp: failed to decode spi# %s\n", foo);
+ (*ndo->ndo_warning)(ndo, "print_esp: failed to decode spi# %s\n", foo);
return;
}
@@ -218,7 +212,7 @@ static void esp_print_decode_onesecret(char *line)
#endif
sin->sin_family = AF_INET;
} else {
- printf("print_esp: can not decode IP# %s\n", spikey);
+ (*ndo->ndo_warning)(ndo, "print_esp: can not decode IP# %s\n", spikey);
return;
}
}
@@ -229,7 +223,6 @@ static void esp_print_decode_onesecret(char *line)
int len;
size_t i;
const EVP_CIPHER *evp;
- int ivlen = 8;
int authlen = 0;
/* skip any blank spaces */
@@ -238,7 +231,7 @@ static void esp_print_decode_onesecret(char *line)
colon = strchr(decode, ':');
if (colon == NULL) {
- printf("failed to decode espsecret: %s\n", decode);
+ (*ndo->ndo_warning)(ndo, "failed to decode espsecret: %s\n", decode);
return;
}
*colon = '\0';
@@ -258,7 +251,7 @@ static void esp_print_decode_onesecret(char *line)
}
evp = EVP_get_cipherbyname(decode);
if (!evp) {
- printf("failed to find cipher algo %s\n", decode);
+ (*ndo->ndo_warning)(ndo, "failed to find cipher algo %s\n", decode);
sa1.evp = NULL;
sa1.authlen = 0;
sa1.ivlen = 0;
@@ -267,7 +260,7 @@ static void esp_print_decode_onesecret(char *line)
sa1.evp = evp;
sa1.authlen = authlen;
- sa1.ivlen = ivlen;
+ sa1.ivlen = EVP_CIPHER_iv_length(evp);
colon++;
if (colon[0] == '0' && colon[1] == 'x') {
@@ -276,13 +269,13 @@ static void esp_print_decode_onesecret(char *line)
len = strlen(colon) / 2;
if (len > 256) {
- printf("secret is too big: %d\n", len);
+ (*ndo->ndo_warning)(ndo, "secret is too big: %d\n", len);
return;
}
i = 0;
while (colon[0] != '\0' && colon[1]!='\0') {
- espsecret_key[i] = hex2byte(colon);
+ espsecret_key[i] = hex2byte(ndo, colon);
colon += 2;
i++;
}
@@ -302,28 +295,28 @@ static void esp_print_decode_onesecret(char *line)
}
}
- esp_print_addsa(&sa1, sa_def);
+ esp_print_addsa(ndo, &sa1, sa_def);
}
-static void esp_print_decodesecret(void)
+static void esp_print_decodesecret(netdissect_options *ndo)
{
char *line;
char *p;
- p = espsecret;
+ p = ndo->ndo_espsecret;
- while (espsecret && espsecret[0] != '\0') {
+ while (ndo->ndo_espsecret && ndo->ndo_espsecret[0] != '\0') {
/* pick out the first line or first thing until a comma */
- if ((line = strsep(&espsecret, "\n,")) == NULL) {
- line = espsecret;
- espsecret = NULL;
+ if ((line = strsep(&ndo->ndo_espsecret, "\n,")) == NULL) {
+ line = ndo->ndo_espsecret;
+ ndo->ndo_espsecret = NULL;
}
- esp_print_decode_onesecret(line);
+ esp_print_decode_onesecret(ndo, line);
}
}
-static void esp_init(void)
+static void esp_init(netdissect_options *ndo _U_)
{
OpenSSL_add_all_algorithms();
@@ -332,7 +325,8 @@ static void esp_init(void)
#endif
int
-esp_print(const u_char *bp, const u_char *bp2
+esp_print(netdissect_options *ndo,
+ const u_char *bp, const int length, const u_char *bp2
#ifndef HAVE_LIBCRYPTO
_U_
#endif
@@ -362,7 +356,7 @@ esp_print(const u_char *bp, const u_char *bp2
char *secret;
int ivlen = 0;
u_char *ivoff;
- const u_char *p;
+ u_char *p;
EVP_CIPHER_CTX ctx;
int blocksz;
static int initialized = 0;
@@ -375,7 +369,7 @@ esp_print(const u_char *bp, const u_char *bp2
advance = 0;
if (!initialized) {
- esp_init();
+ esp_init(ndo);
initialized = 1;
}
#endif
@@ -386,28 +380,28 @@ esp_print(const u_char *bp, const u_char *bp2
#endif
/* 'ep' points to the end of available data. */
- ep = snapend;
+ ep = ndo->ndo_snapend;
if ((u_char *)(esp + 1) >= ep) {
fputs("[|ESP]", stdout);
goto fail;
}
- printf("ESP(spi=0x%08x", EXTRACT_32BITS(&esp->esp_spi));
- printf(",seq=0x%x", EXTRACT_32BITS(&esp->esp_seq));
- printf(")");
+ (*ndo->ndo_printf)(ndo, "ESP(spi=0x%08x", EXTRACT_32BITS(&esp->esp_spi));
+ (*ndo->ndo_printf)(ndo, ",seq=0x%x)", EXTRACT_32BITS(&esp->esp_seq));
+ (*ndo->ndo_printf)(ndo, ", length %u", length);
#ifndef HAVE_LIBCRYPTO
goto fail;
#else
/* initiailize SAs */
- if (sa_list_head == NULL) {
- if (!espsecret)
+ if (ndo->ndo_sa_list_head == NULL) {
+ if (!ndo->ndo_espsecret)
goto fail;
- esp_print_decodesecret();
+ esp_print_decodesecret(ndo);
}
- if (sa_list_head == NULL)
+ if (ndo->ndo_sa_list_head == NULL)
goto fail;
ip = (struct ip *)bp2;
@@ -422,7 +416,7 @@ esp_print(const u_char *bp, const u_char *bp2
len = sizeof(struct ip6_hdr) + EXTRACT_16BITS(&ip6->ip6_plen);
/* see if we can find the SA, and if so, decode it */
- for (sa = sa_list_head; sa != NULL; sa = sa->next) {
+ for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa->daddr;
if (sa->spi == ntohl(esp->esp_spi) &&
sin6->sin6_family == AF_INET6 &&
@@ -440,7 +434,7 @@ esp_print(const u_char *bp, const u_char *bp2
len = EXTRACT_16BITS(&ip->ip_len);
/* see if we can find the SA, and if so, decode it */
- for (sa = sa_list_head; sa != NULL; sa = sa->next) {
+ for (sa = ndo->ndo_sa_list_head; sa != NULL; sa = sa->next) {
struct sockaddr_in *sin = (struct sockaddr_in *)&sa->daddr;
if (sa->spi == ntohl(esp->esp_spi) &&
sin->sin_family == AF_INET &&
@@ -457,7 +451,7 @@ esp_print(const u_char *bp, const u_char *bp2
* an unspecified one.
*/
if (sa == NULL)
- sa = sa_default;
+ sa = ndo->ndo_sa_default;
/* if not found fail */
if (sa == NULL)
@@ -475,11 +469,12 @@ esp_print(const u_char *bp, const u_char *bp2
ivlen = sa->ivlen;
secret = sa->secret;
espsecret_keylen = sa->secretlen;
+ ep = ep - sa->authlen;
if (sa->evp) {
memset(&ctx, 0, sizeof(ctx));
if (EVP_CipherInit(&ctx, sa->evp, secret, NULL, 0) < 0)
- printf("espkey init failed");
+ (*ndo->ndo_warning)(ndo, "espkey init failed");
blocksz = EVP_CIPHER_CTX_block_size(&ctx);
@@ -490,7 +485,6 @@ esp_print(const u_char *bp, const u_char *bp2
} else
advance = sizeof(struct newesp);
- ep = ep - sa->authlen;
/* sanity check for pad length */
if (ep - bp < *(ep - 2))
goto fail;
@@ -501,10 +495,17 @@ esp_print(const u_char *bp, const u_char *bp2
if (nhdr)
*nhdr = *(ep - 1);
- printf(": ");
+ (ndo->ndo_printf)(ndo, ": ");
return advance;
#endif
fail:
return -1;
}
+
+/*
+ * Local Variables:
+ * c-style: whitesmith
+ * c-basic-offset: 8
+ * End:
+ */
OpenPOWER on IntegriCloud