Skip to content

Commit

Permalink
linux: set handlep->vlan_offset if the linktype is changed.
Browse files Browse the repository at this point in the history
The change to the linktype might change the offset at which to insert
VLAN tags (or change it to -1, meaning "don't insert VLAN tags").

This should fix issue #1105.
  • Loading branch information
guyharris committed Apr 9, 2022
1 parent ee9097a commit 4bfca36
Showing 1 changed file with 41 additions and 23 deletions.
64 changes: 41 additions & 23 deletions pcap-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,39 @@ static void pcap_breakloop_linux(pcap_t *handle)
(void)write(handlep->poll_breakloop_fd, &value, sizeof(value));
}

/*
* Set the offset at which to insert VLAN tags.
* That should be the offset of the type field.
*/
static void
set_vlan_offset(pcap_t *handle)
{
struct pcap_linux *handlep = handle->priv;

switch (handle->linktype) {

case DLT_EN10MB:
/*
* The type field is after the destination and source
* MAC address.
*/
handlep->vlan_offset = 2 * ETH_ALEN;
break;

case DLT_LINUX_SLL:
/*
* The type field is in the last 2 bytes of the
* DLT_LINUX_SLL header.
*/
handlep->vlan_offset = SLL_HDR_LEN - 2;
break;

default:
handlep->vlan_offset = -1; /* unknown */
break;
}
}

/*
* Get a handle for a live capture from the given device. You can
* pass NULL as device to get all packages (without link level
Expand Down Expand Up @@ -1113,6 +1146,13 @@ static int
pcap_set_datalink_linux(pcap_t *handle, int dlt)
{
handle->linktype = dlt;

/*
* Update the offset at which to insert VLAN tags for the
* new link-layer type.
*/
set_vlan_offset(handle);

return 0;
}

Expand Down Expand Up @@ -2584,30 +2624,8 @@ activate_pf_packet(pcap_t *handle, int is_any_device)

/*
* Set the offset at which to insert VLAN tags.
* That should be the offset of the type field.
*/
switch (handle->linktype) {

case DLT_EN10MB:
/*
* The type field is after the destination and source
* MAC address.
*/
handlep->vlan_offset = 2 * ETH_ALEN;
break;

case DLT_LINUX_SLL:
/*
* The type field is in the last 2 bytes of the
* DLT_LINUX_SLL header.
*/
handlep->vlan_offset = SLL_HDR_LEN - 2;
break;

default:
handlep->vlan_offset = -1; /* unknown */
break;
}
set_vlan_offset(handle);

if (handle->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO) {
int nsec_tstamps = 1;
Expand Down

0 comments on commit 4bfca36

Please sign in to comment.