pcap-namedb.h \
pcap/bpf.h \
pcap/bluetooth.h \
+ pcap/can_socketcan.h \
pcap/dlt.h \
pcap/export-defs.h \
pcap/ipnet.h \
bpf_error(cstate, "Bluetooth link-layer type filtering not implemented");
case DLT_CAN20B:
- case DLT_CAN_SOCKETCAN:
+ case DLT_CAN_SOCKETCAN_BIGENDIAN:
+ case DLT_CAN_SOCKETCAN_HOSTENDIAN:
bpf_error(cstate, "CAN link-layer type filtering not implemented");
case DLT_IEEE802_15_4:
/* Initialize some components of the pcap structure. */
handle->bufsize = CAN_CONTROL_SIZE + 16;
- handle->linktype = DLT_CAN_SOCKETCAN;
+ handle->linktype = DLT_CAN_SOCKETCAN_BIGENDIAN;
handle->read_op = can_read_linux;
handle->inject_op = can_inject_linux;
handle->setfilter_op = can_setfilter_linux;
/* Initialize some components of the pcap structure. */
handle->bufsize = 32;
handle->offset = 8;
- handle->linktype = DLT_CAN_SOCKETCAN;
+ handle->linktype = DLT_CAN_SOCKETCAN_HOSTENDIAN;
handle->set_datalink_op = NULL;
serial = handle->opt.device + strlen(CANUSB_IFACE);
#include "pcap-int.h"
#include "pcap/usb.h"
#include "pcap/nflog.h"
+#include "pcap/can_socketcan.h"
#include "pcap-common.h"
/*
* CAN (Controller Area Network) frames, with a pseudo-header as supplied
- * by Linux SocketCAN. See Documentation/networking/can.txt in the Linux
- * source.
+ * by Linux SocketCAN, and with multi-byte numerical fields in that header
+ * in big-endian byte order.
+ *
+ * See Documentation/networking/can.txt in the Linux source.
*
*/
-#define LINKTYPE_CAN_SOCKETCAN 227
+#define LINKTYPE_CAN_SOCKETCAN_BIGENDIAN 227
/*
* Raw IPv4/IPv6; different from DLT_RAW in that the DLT_ value specifies
*/
#define LINKTYPE_ISO_14443 264
-#define LINKTYPE_MATCHING_MAX 264 /* highest value in the "matching" range */
+/*
+ * CAN (Controller Area Network) frames, with a pseudo-header as supplied
+ * by Linux SocketCAN, and with multi-byte numerical fields in that header
+ * in host byte order.
+ *
+ * See Documentation/networking/can.txt in the Linux source.
+ */
+#define LINKTYPE_CAN_SOCKETCAN_HOSTENDIAN 265
+
+#define LINKTYPE_MATCHING_MAX 265 /* highest value in the "matching" range */
static struct linktype_map {
int dlt;
u_int length = hdr->len;
u_int16_t size;
- if (caplen < (int) sizeof(nflog_hdr_t) || length < (int) sizeof(nflog_hdr_t)) {
+ if (caplen < (u_int) sizeof(nflog_hdr_t) ||
+ length < (u_int) sizeof(nflog_hdr_t)) {
/* Not enough data to have any TLVs. */
return;
}
}
}
+/*
+ * The DLT_CAN_SOCKETCAN_HOSTENDIAN header is in host byte order when
+ * capturing (it's filled in by the kernel and provided on a PF_PACKET
+ * socket).
+ *
+ * When reading a DLT_CAN_SOCKETCAN_HOSTENDIAN capture file, we need to
+ * convert it from the byte order of the host that wrote the file to
+ * this host's byte order.
+ */
+static void
+swap_can_socketcan_header(const struct pcap_pkthdr *hdr, u_char *buf)
+{
+ u_int caplen = hdr->caplen;
+ u_int length = hdr->len;
+ pcap_can_socketcan_hdr *chdr = (pcap_can_socketcan_hdr *)buf;
+
+ if (caplen < (u_int) sizeof(chdr->can_id) ||
+ length < (u_int) sizeof(chdr->can_id)) {
+ /* Not enough data to have the ID */
+ return;
+ }
+
+ chdr->can_id = SWAPLONG(chdr->can_id);
+}
+
void
swap_pseudo_headers(int linktype, struct pcap_pkthdr *hdr, u_char *data)
{
case DLT_NFLOG:
swap_nflog_header(hdr, data);
break;
+
+ case DLT_CAN_SOCKETCAN_HOSTENDIAN:
+ swap_can_socketcan_header(hdr, data);
+ break;
}
}
#define ARPHRD_CAN 280
#endif
case ARPHRD_CAN:
- handle->linktype = DLT_CAN_SOCKETCAN;
+ /*
+ * DLT_CAN_SOCKETCAN_BIGENDIAN is defined to have the
+ * can_id field of the pseudo-header in big-endian
+ * (network) byte order.
+ *
+ * The packets delivered to sockets have that field
+ * in host byte order.
+ *
+ * The code that implements it in packet-can-linux.c
+ * passes that field to htonl() to put it into network
+ * byte order.
+ *
+ * The code that reads from a PF_PACKET socket doesn't
+ * change the byte order of that field, so we define
+ * a new DLT_CAN_SOCKETCAN_HOSTENDIAN, where the can_id
+ * is in host byte order.
+ */
+ handle->linktype = DLT_CAN_SOCKETCAN_HOSTENDIAN;
break;
#ifndef ARPHRD_IEEE802_TR
DLT_CHOICE(DLT_FC_2, "Fibre Channel FC-2"),
DLT_CHOICE(DLT_FC_2_WITH_FRAME_DELIMS, "Fibre Channel FC-2 with frame delimiters"),
DLT_CHOICE(DLT_IPNET, "Solaris ipnet"),
- DLT_CHOICE(DLT_CAN_SOCKETCAN, "CAN-bus with SocketCAN headers"),
+ DLT_CHOICE(DLT_CAN_SOCKETCAN_BIGENDIAN, "CAN-bus with big-endian SocketCAN headers"),
DLT_CHOICE(DLT_IPV4, "Raw IPv4"),
DLT_CHOICE(DLT_IPV6, "Raw IPv6"),
DLT_CHOICE(DLT_IEEE802_15_4_NOFCS, "IEEE 802.15.4 without FCS"),
DLT_CHOICE(DLT_PROFIBUS_DL, "PROFIBUS data link layer"),
DLT_CHOICE(DLT_PKTAP, "Apple DLT_PKTAP"),
DLT_CHOICE(DLT_EPON, "Ethernet with 802.3 Clause 65 EPON preamble"),
+ DLT_CHOICE(DLT_CAN_SOCKETCAN_HOSTENDIAN, "CAN-bus with host-endian SocketCAN headers"),
DLT_CHOICE_SENTINEL
};
--- /dev/null
+/*-
+ * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from the Stanford/CMU enet packet filter,
+ * (net/enet.c) distributed as part of 4.3BSD, and code contributed
+ * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
+ * Berkeley Laboratory.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lib_pcap_can_socketcan_h
+#define lib_pcap_can_socketcan_h
+
+/*
+ * SocketCAN header, as per Documentation/networking/can.txt in the
+ * Linux source.
+ */
+typedef struct {
+ u_int32_t can_id;
+ u_int8_t payload_length;
+ u_int8_t pad;
+ u_int8_t reserved1;
+ u_int8_t reserved2;
+} pcap_can_socketcan_hdr;
+
+#endif
/*
* CAN (Controller Area Network) frames, with a pseudo-header as supplied
- * by Linux SocketCAN. See Documentation/networking/can.txt in the Linux
- * source.
+ * by Linux SocketCAN, and with multi-byte numerical fields in that header
+ * in big-endian byte order.
+ *
+ * See Documentation/networking/can.txt in the Linux source.
*
*/
+#define DLT_CAN_SOCKETCAN_BIGENDIAN 227
#define DLT_CAN_SOCKETCAN 227
/*
*/
#define DLT_ISO_14443 264
+/*
+ * CAN (Controller Area Network) frames, with a pseudo-header as supplied
+ * by Linux SocketCAN, and with multi-byte numerical fields in that header
+ * in host byte order.
+ *
+ * See Documentation/networking/can.txt in the Linux source.
+ */
+#define DLT_CAN_SOCKETCAN_HOSTENDIAN 265
+
/*
* In case the code that includes this file (directly or indirectly)
* has also included OS files that happen to define DLT_MATCHING_MAX,
#ifdef DLT_MATCHING_MAX
#undef DLT_MATCHING_MAX
#endif
-#define DLT_MATCHING_MAX 264 /* highest value in the "matching" range */
+#define DLT_MATCHING_MAX 265 /* highest value in the "matching" range */
/*
* DLT and savefile link type values are split into a class and