2019-07-03 19:20:25 +00:00
|
|
|
#include <arpa/inet.h>
|
2019-07-03 20:05:29 +00:00
|
|
|
#include <assert.h>
|
2019-07-03 19:20:25 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <linux/if_packet.h>
|
|
|
|
#include <net/ethernet.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2019-07-03 20:09:14 +00:00
|
|
|
#include <stdlib.h>
|
2019-07-03 19:20:25 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-07-03 20:05:29 +00:00
|
|
|
char hexify(int nybble) {
|
|
|
|
assert(0 <= nybble && nybble <= 16);
|
|
|
|
return "0123456789abcdef"[nybble];
|
|
|
|
}
|
|
|
|
|
|
|
|
void format_mac(const unsigned char binary_address[6], char formatted[18]) {
|
|
|
|
for (size_t i = 0; i < 6; i++) {
|
|
|
|
unsigned char byte = binary_address[i];
|
|
|
|
formatted[3*i] = hexify(byte >> 4);
|
|
|
|
formatted[3*i + 1] = hexify(byte & 0xf);
|
|
|
|
formatted[3*i + 2] = ':';
|
|
|
|
}
|
|
|
|
formatted[17] = '\0';
|
|
|
|
}
|
|
|
|
|
2019-07-03 20:09:14 +00:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
if (argc != 2) {
|
|
|
|
fprintf(stderr, "Usage: %s interface\n", argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-07-03 19:20:25 +00:00
|
|
|
// Create a packet socket
|
|
|
|
errno = 0;
|
|
|
|
int packet_socket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
|
|
|
|
if (packet_socket == -1) {
|
|
|
|
err(1, "socket");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the index of the network interface
|
|
|
|
struct ifreq ifr;
|
2019-07-03 20:09:14 +00:00
|
|
|
strncpy(ifr.ifr_name, argv[1], IFNAMSIZ);
|
2019-07-03 19:20:25 +00:00
|
|
|
errno = 0;
|
|
|
|
if (ioctl(packet_socket, SIOCGIFINDEX, &ifr) == -1) {
|
|
|
|
err(1, "ioctl");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bind to the network interface
|
|
|
|
struct sockaddr_ll sll;
|
|
|
|
sll.sll_family = AF_PACKET;
|
|
|
|
sll.sll_protocol = htons(ETH_P_ALL);
|
|
|
|
sll.sll_ifindex = ifr.ifr_ifindex;
|
|
|
|
errno = 0;
|
|
|
|
if (bind(packet_socket, (const struct sockaddr*)&sll, sizeof(sll)) == -1) {
|
|
|
|
err(1, "bind");
|
|
|
|
}
|
|
|
|
|
|
|
|
for(;;) {
|
2019-07-03 19:36:20 +00:00
|
|
|
unsigned char frame[1522]; // 1522 is the largest a 802.3 frame can be
|
2019-07-03 19:20:25 +00:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
ssize_t frame_length = recv(packet_socket, frame, sizeof(frame), MSG_TRUNC);
|
|
|
|
if (frame_length == -1) {
|
|
|
|
err(1, "recv");
|
|
|
|
}
|
|
|
|
|
2019-07-03 20:05:29 +00:00
|
|
|
if (frame_length < 64) {
|
|
|
|
errno = 0;
|
|
|
|
if (printf("Runt frame (%zdB)\n", frame_length) == -1) {
|
|
|
|
err(1, "printf");
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the MACs
|
|
|
|
// 012345 012345
|
|
|
|
// dest source
|
|
|
|
char destination_mac[18], source_mac[18];
|
|
|
|
format_mac(&frame[0], destination_mac);
|
|
|
|
format_mac(&frame[6], source_mac);
|
|
|
|
|
2019-07-03 19:20:25 +00:00
|
|
|
// Extract EtherType / length field
|
|
|
|
// 012345 012345 01
|
|
|
|
// dest source ^^
|
|
|
|
// It is stored in the network byte order, that is, high byte 1st
|
2019-07-03 19:43:55 +00:00
|
|
|
uint16_t ethertype = (frame[12] << 8) | frame[13];
|
|
|
|
|
|
|
|
const char *ethertype_meaning = NULL;
|
|
|
|
if (ethertype <= 1500) {
|
|
|
|
ethertype_meaning = "packet length";
|
|
|
|
} else if (ethertype < 0x0600) {
|
|
|
|
ethertype_meaning = "undefined";
|
|
|
|
} else if (ethertype == 0x0800) {
|
|
|
|
ethertype_meaning = "IPv4";
|
|
|
|
} else if (ethertype == 0x0806) {
|
|
|
|
ethertype_meaning = "ARP";
|
|
|
|
} else if (ethertype == 0x86dd) {
|
|
|
|
ethertype_meaning = "IPv6";
|
|
|
|
}
|
2019-07-03 19:20:25 +00:00
|
|
|
|
2019-07-03 20:05:29 +00:00
|
|
|
errno = 0;
|
|
|
|
if(printf("%s -> %s ethertype: %02x%02x", source_mac, destination_mac, ethertype >> 8, ethertype & 0xff) == -1) {
|
|
|
|
err(1, "printf");
|
|
|
|
}
|
|
|
|
|
2019-07-03 19:20:25 +00:00
|
|
|
errno = 0;
|
2019-07-03 19:43:55 +00:00
|
|
|
if (ethertype_meaning == NULL) {
|
2019-07-03 20:05:29 +00:00
|
|
|
if (printf(", length: %zd\n", frame_length) == -1) {
|
2019-07-03 19:43:55 +00:00
|
|
|
err(1, "printf");
|
|
|
|
}
|
|
|
|
} else {
|
2019-07-03 20:05:29 +00:00
|
|
|
if (printf("(%s), length: %zd\n", ethertype_meaning, frame_length) == -1) {
|
2019-07-03 19:43:55 +00:00
|
|
|
err(1, "printf");
|
|
|
|
}
|
2019-07-03 19:20:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|