#ifndef lint
static const char rcsid[] _U_ =
- "@(#) $Header: /tcpdump/master/tcpdump/print-esp.c,v 1.48 2003-11-19 05:37:18 guy Exp $ (LBL)";
+ "@(#) $Header: /tcpdump/master/tcpdump/print-esp.c,v 1.51 2004-04-10 08:41:22 guy Exp $ (LBL)";
#endif
#ifdef HAVE_CONFIG_H
extern char *strsep(char **stringp, const char *delim); /* Missing/strsep.c */
#endif
-#include "interface.h"
+#include "netdissect.h"
#include "addrtoname.h"
#include "extract.h"
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" */
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');
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;
}
* 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;
if (fileline[0] == '#') continue;
if (fileline[0] == '\0') continue;
- esp_print_decode_onesecret(fileline);
+ esp_print_decode_onesecret(ndo, fileline);
}
fclose(secretfile);
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;
}
#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;
}
}
int len;
size_t i;
const EVP_CIPHER *evp;
- int ivlen = 8;
int authlen = 0;
/* skip any blank spaces */
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';
}
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;
sa1.evp = evp;
sa1.authlen = authlen;
- sa1.ivlen = ivlen;
+ sa1.ivlen = EVP_CIPHER_iv_length(evp);
colon++;
if (colon[0] == '0' && colon[1] == 'x') {
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++;
}
}
}
- 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();
#endif
int
-esp_print(const u_char *bp, const u_char *bp2
+esp_print(netdissect_options *ndo,
+ const u_char *bp, const u_char *bp2
#ifndef HAVE_LIBCRYPTO
_U_
#endif
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;
advance = 0;
if (!initialized) {
- esp_init();
+ esp_init(ndo);
initialized = 1;
}
#endif
#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));
#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;
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 &&
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 &&
* an unspecified one.
*/
if (sa == NULL)
- sa = sa_default;
+ sa = ndo->ndo_sa_default;
/* if not found fail */
if (sa == NULL)
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);
} else
advance = sizeof(struct newesp);
- ep = ep - sa->authlen;
/* sanity check for pad length */
if (ep - bp < *(ep - 2))
goto fail;
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:
+ */