Skip to content

Commit a2473a1

Browse files
authored
Merge pull request #91 from CESNET/feature_pcap_sll2
pcap: add support for DLT_LINUX_SLL2 datalink layer
2 parents 33cfbd3 + cf2e895 commit a2473a1

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

input/parser.cpp

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@
4747
#include <iostream>
4848
#include <sys/types.h>
4949

50-
#ifdef WITH_PCAP
51-
#include <pcap/sll.h>
52-
#endif /* WITH_PCAP */
53-
5450
#include "parser.hpp"
5551
#include "headers.hpp"
5652
#include <ipfixprobe/packet.hpp>
@@ -178,6 +174,39 @@ inline uint16_t parse_sll(const u_char *data_ptr, uint16_t data_len, Packet *pkt
178174
pkt->ethertype = ntohs(sll->sll_protocol);
179175
return sizeof(struct sll_header);
180176
}
177+
178+
# ifdef DLT_LINUX_SLL2
179+
inline uint16_t parse_sll2(const u_char *data_ptr, uint16_t data_len, Packet *pkt)
180+
{
181+
struct sll2_header *sll = (struct sll2_header *) data_ptr;
182+
if (sizeof(struct sll2_header) > data_len) {
183+
throw "Parser detected malformed packet";
184+
}
185+
186+
DEBUG_MSG("SLL2 header:\n");
187+
DEBUG_MSG("\tPacket type:\t%u\n", ntohs(sll->sll2_pkttype));
188+
DEBUG_MSG("\tHA type:\t%u\n", ntohs(sll->sll2_hatype));
189+
DEBUG_MSG("\tHA len:\t\t%u\n", ntohs(sll->sll2_halen));
190+
DEBUG_MSG("\tinterface index:\t\t%u\n", ntohl(sll->sll2_if_index));
191+
DEBUG_CODE(
192+
DEBUG_MSG("\tAddress:\t");
193+
for (int i = 0; i < SLL_ADDRLEN; i++) {
194+
DEBUG_MSG("%02x ", sll->sll2_addr[i]);
195+
}
196+
DEBUG_MSG("\n");
197+
);
198+
DEBUG_MSG("\tProtocol:\t%u\n", ntohs(sll->sll2_protocol));
199+
200+
if (ntohs(sll->sll2_hatype) == ARPHRD_ETHER) {
201+
memcpy(pkt->src_mac, sll->sll2_addr, 6);
202+
} else {
203+
memset(pkt->src_mac, 0, sizeof(pkt->src_mac));
204+
}
205+
memset(pkt->dst_mac, 0, sizeof(pkt->dst_mac));
206+
pkt->ethertype = ntohs(sll->sll2_protocol);
207+
return sizeof(struct sll2_header);
208+
}
209+
# endif /* DLT_LINUX_SLL2 */
181210
#endif /* WITH_PCAP */
182211

183212

@@ -619,6 +648,10 @@ void parse_packet(parser_opt_t *opt, struct timeval ts, const uint8_t *data, uin
619648
data_offset = parse_eth_hdr(data, caplen, pkt);
620649
} else if (opt->datalink == DLT_LINUX_SLL) {
621650
data_offset = parse_sll(data, caplen, pkt);
651+
# ifdef DLT_LINUX_SLL2
652+
} else if (opt->datalink == DLT_LINUX_SLL2) {
653+
data_offset = parse_sll2(data, caplen, pkt);
654+
# endif /* DLT_LINUX_SLL2 */
622655
} else if (opt->datalink == DLT_RAW) {
623656
if ((data[0] & 0xF0) == 0x40) {
624657
pkt->ethertype = ETH_P_IP;

input/parser.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646

4747
#include <ipfixprobe/packet.hpp>
4848

49+
#ifdef WITH_PCAP
50+
#include <pcap/pcap.h>
51+
#include <pcap/sll.h>
52+
#endif /* WITH_PCAP */
53+
4954
#ifndef DLT_EN10MB
5055
#define DLT_EN10MB 1
5156
#endif

input/pcap.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ void PcapReader::open_ifc(const std::string &ifc)
186186
void PcapReader::check_datalink(int datalink)
187187
{
188188
if (m_datalink != DLT_EN10MB && m_datalink != DLT_LINUX_SLL && m_datalink != DLT_RAW) {
189+
#ifdef DLT_LINUX_SLL2
190+
if (m_datalink == DLT_LINUX_SLL2) {
191+
// DLT_LINUX_SLL2 is also supported
192+
return;
193+
} else {
194+
close();
195+
throw PluginError("unsupported link type detected, supported types are: DLT_EN10MB, DLT_LINUX_SLL, DLT_LINUX_SLL2, and DLT_RAW");
196+
}
197+
#endif
189198
close();
190199
throw PluginError("unsupported link type detected, supported types are DLT_EN10MB and DLT_LINUX_SLL and DLT_RAW");
191200
}

0 commit comments

Comments
 (0)