]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-esp.c
When checking for pcap_if_t, add $V_INCLS to CFLAGS, so we look at the
[tcpdump] / print-esp.c
index 7894fd4816e0bab952aaf27c0fa98c74ccd14b9a..25303e30d4457aa71fd851f821e0238da0859a8b 100644 (file)
  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
+#ifndef lint
+static const char rcsid[] _U_ =
+    "@(#) $Header: /tcpdump/master/tcpdump/print-esp.c,v 1.51 2004-04-10 08:41:22 guy Exp $ (LBL)";
+#endif
 
 #ifdef HAVE_CONFIG_H
 #include "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"
 
-#ifndef lint
-static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/tcpdump/print-esp.c,v 1.45 2003-11-15 00:39:22 guy Exp $ (LBL)";
-#endif
 #ifndef HAVE_SOCKADDR_STORAGE
 #ifdef INET6
 struct sockaddr_storage {
@@ -83,10 +83,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 +92,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 +113,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 +133,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 +175,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 +194,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 +216,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 +227,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 +235,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 +255,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 +264,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 +273,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 +299,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,12 +329,18 @@ 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 u_char *bp2
 #ifndef HAVE_LIBCRYPTO
        _U_
 #endif
        ,
-       int *nhdr, int *padlen
+       int *nhdr
+#ifndef HAVE_LIBCRYPTO
+       _U_
+#endif
+       ,
+       int *padlen
 #ifndef HAVE_LIBCRYPTO
        _U_
 #endif
@@ -357,7 +360,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;
@@ -370,7 +373,7 @@ esp_print(const u_char *bp, const u_char *bp2
        advance = 0;
 
        if (!initialized) {
-               esp_init();
+               esp_init(ndo);
                initialized = 1;
        }
 #endif
@@ -381,28 +384,27 @@ 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));
 
 #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;
@@ -417,7 +419,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 &&
@@ -435,7 +437,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 &&
@@ -452,7 +454,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)
@@ -470,11 +472,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);
 
@@ -485,7 +488,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;
@@ -496,12 +498,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:
-       if (nhdr)
-               *nhdr = -1;
-       return 65536;
+       return -1;
 }
+
+/*
+ * Local Variables:
+ * c-style: whitesmith
+ * c-basic-offset: 8
+ * End:
+ */