summaryrefslogtreecommitdiff
path: root/print-ether.c
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>2010-02-21 00:27:00 -0800
committerGuy Harris <guy@alum.mit.edu>2010-02-21 00:27:00 -0800
commite8b523758959c1854689d71c7a4686c631e5501c (patch)
tree67d50632c8f8f0410d2a8d227a78709a7aca1ca2 /print-ether.c
parentb00042a8ae3656f01e022bf6df1eb761e6323d3d (diff)
downloadtcpdump-e8b523758959c1854689d71c7a4686c631e5501c.tar.gz
Don't directly fetch multi-byte integers from packets.
Use the EXTRACT_ macros to extract multi-byte integral values from packets, rather than just dereferencing pointers into the packet; there is no guarantee that the packet data will be aligned on the right boundary, and there is no guarantee that, if they're not, a direct access will work correctly.
Diffstat (limited to 'print-ether.c')
-rw-r--r--print-ether.c33
1 files changed, 20 insertions, 13 deletions
diff --git a/print-ether.c b/print-ether.c
index 20a2a65f..581d6882 100644
--- a/print-ether.c
+++ b/print-ether.c
@@ -33,6 +33,7 @@ static const char rcsid[] _U_ =
#include <pcap.h>
#include "interface.h"
+#include "extract.h"
#include "addrtoname.h"
#include "ethertype.h"
@@ -86,24 +87,27 @@ static inline void
ether_hdr_print(register const u_char *bp, u_int length)
{
register const struct ether_header *ep;
+ u_int16_t ether_type;
+
ep = (const struct ether_header *)bp;
(void)printf("%s > %s",
etheraddr_string(ESRC(ep)),
etheraddr_string(EDST(ep)));
+ ether_type = EXTRACT_16BITS(&ep->ether_type);
if (!qflag) {
- if (ntohs(ep->ether_type) <= ETHERMTU)
+ if (ether_type <= ETHERMTU)
(void)printf(", 802.3");
else
(void)printf(", ethertype %s (0x%04x)",
- tok2str(ethertype_values,"Unknown", ntohs(ep->ether_type)),
- ntohs(ep->ether_type));
+ tok2str(ethertype_values,"Unknown", ether_type),
+ ether_type);
} else {
- if (ntohs(ep->ether_type) <= ETHERMTU)
+ if (ether_type <= ETHERMTU)
(void)printf(", 802.3");
else
- (void)printf(", %s", tok2str(ethertype_values,"Unknown Ethertype (0x%04x)", ntohs(ep->ether_type)));
+ (void)printf(", %s", tok2str(ethertype_values,"Unknown Ethertype (0x%04x)", ether_type));
}
(void)printf(", length %u: ", length);
@@ -129,7 +133,7 @@ ether_print(const u_char *p, u_int length, u_int caplen)
ep = (struct ether_header *)p;
p += ETHER_HDRLEN;
- ether_type = ntohs(ep->ether_type);
+ ether_type = EXTRACT_16BITS(&ep->ether_type);
/*
* Is it (gag) an 802.3 encapsulation?
@@ -226,13 +230,16 @@ ether_encap_print(u_short ether_type, const u_char *p,
return (1);
case ETHERTYPE_8021Q:
- if (eflag)
- printf("vlan %u, p %u%s, ",
- ntohs(*(u_int16_t *)p) & 0xfff,
- ntohs(*(u_int16_t *)p) >> 13,
- (ntohs(*(u_int16_t *)p) & 0x1000) ? ", CFI" : "");
+ if (eflag) {
+ u_int16_t tag = EXTRACT_16BITS(p);
+
+ printf("vlan %u, p %u%s, ",
+ tag & 0xfff,
+ tag >> 13,
+ (tag & 0x1000) ? ", CFI" : "");
+ }
- ether_type = ntohs(*(u_int16_t *)(p + 2));
+ ether_type = EXTRACT_16BITS(p + 2);
p += 4;
length -= 4;
caplen -= 4;
@@ -259,7 +266,7 @@ ether_encap_print(u_short ether_type, const u_char *p,
return (1);
case ETHERTYPE_JUMBO:
- ether_type = ntohs(*(u_int16_t *)(p));
+ ether_type = EXTRACT_16BITS(p);
p += 2;
length -= 2;
caplen -= 2;