Skip to content

Commit

Permalink
Support IEEE 802.1Q, VLAN
Browse files Browse the repository at this point in the history
  • Loading branch information
chenxm committed May 12, 2016
1 parent d32dcea commit 9641564
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
20 changes: 16 additions & 4 deletions pkt2flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include <getopt.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h>
Expand Down Expand Up @@ -222,7 +221,7 @@ static int pcap_handle_layer4(struct af_6tuple *af_6tuple, const u_char *bytes,
#endif

#ifdef darwin
if (tcphdr->th_flags == TH_SYN)
if (tcphdr->th_flags == TH_SYN)
#else
if (tcphdr->syn)
#endif
Expand Down Expand Up @@ -347,8 +346,21 @@ static int pcap_handle_ethernet(struct af_6tuple *af_6tuple,
len -= sizeof(*ethhdr);
bytes += sizeof(*ethhdr);

if (ntohs(ethhdr->ether_type) != ETHERTYPE_IP &&
ntohs(ethhdr->ether_type) != ETHERTYPE_IPV6)
struct vlan_header *vlanhdr;
uint16_t etype = ntohs(ethhdr->ether_type);

/* VLAN header, IEEE 802.1Q */
if (etype == ETHERTYPE_VLAN) {
vlanhdr = (struct vlan_header *)bytes;
etype = ntohs(vlanhdr->tpid);
bytes += sizeof(*vlanhdr);
len -= sizeof(*vlanhdr);
af_6tuple->is_vlan = 1;
} else {
af_6tuple->is_vlan = 0;
}

if (etype != ETHERTYPE_IP && etype != ETHERTYPE_IPV6)
return -1;

return pcap_handle_ip(af_6tuple, bytes, len);
Expand Down
7 changes: 7 additions & 0 deletions pkt2flow.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ struct pkt_dump_file {
unsigned long start_time;
};

/* VLAN header, IEEE 802.1Q */
struct vlan_header {
uint16_t tci; /* Priority 3bits, CFI 1bit, ID 12bits */
uint16_t tpid;
};

union ip_address {
struct in_addr v4;
struct in6_addr v6;
Expand All @@ -78,6 +84,7 @@ struct af_6tuple {
int protocol;
union ip_address ip1, ip2;
uint16_t port1, port2;
uint8_t is_vlan;
};

struct ip_pair {
Expand Down
12 changes: 11 additions & 1 deletion utilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,19 @@ char *new_file_name(struct af_6tuple af_6tuple, unsigned long timestamp)
break;
}

ret = asprintf(&fname, "%s_%"PRIu16"_%s_%"PRIu16"_%lu.pcap",
switch (af_6tuple.is_vlan) {
case 0:
ret = asprintf(&fname, "%s_%"PRIu16"_%s_%"PRIu16"_%lu.pcap",
src_ip_str, af_6tuple.port1, dst_ip_str, af_6tuple.port2,
timestamp);
break;
case 1:
ret = asprintf(&fname, "%s_%"PRIu16"_%s_%"PRIu16"_%lu_vlan.pcap",
src_ip_str, af_6tuple.port1, dst_ip_str, af_6tuple.port2,
timestamp);
break;
}

if (ret < 0)
fname = NULL;

Expand Down

0 comments on commit 9641564

Please sign in to comment.