Skip to content

Instantly share code, notes, and snippets.

@abrodkin
Forked from austinmarton/recvRawEth.c
Last active April 7, 2021 05:11
Show Gist options
  • Save abrodkin/8854874f3b34e92f71653a7fa35c9900 to your computer and use it in GitHub Desktop.
Save abrodkin/8854874f3b34e92f71653a7fa35c9900 to your computer and use it in GitHub Desktop.
Receive raw Ethernet frames in Linux
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/ether.h>
#define DEFAULT_IF "eth0"
#define BUF_SIZ 1024
uint8_t bytes[6] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
int values[6];
int main(int argc, char *argv[])
{
int sockfd, i;
struct ifreq if_idx;
struct ifreq if_mac;
int tx_len = 0;
char sendbuf[BUF_SIZ];
struct ether_header *eh = (struct ether_header *) sendbuf;
struct iphdr *iph = (struct iphdr *) (sendbuf + sizeof(struct ether_header));
struct sockaddr_ll socket_address;
char ifName[IFNAMSIZ];
/* Get interface name */
if (argc > 1)
strcpy(ifName, argv[1]);
else
strcpy(ifName, DEFAULT_IF);
if (argc > 2)
if (6 == sscanf(argv[2], "%x:%x:%x:%x:%x:%x%c",
&values[0], &values[1], &values[2],
&values[3], &values[4], &values[5])) {
/* convert to uint8_t */
for(i = 0; i < 6; ++i)
bytes[i] = (uint8_t)values[i];
}
printf("Sending to MAC %02x:%02x:%02x:%02x:%02x:%02x via %s interface\n",
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5],
ifName);
/* Open RAW socket to send on */
if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) {
perror("socket");
}
/* Get the index of the interface to send on */
memset(&if_idx, 0, sizeof(struct ifreq));
strncpy(if_idx.ifr_name, ifName, IFNAMSIZ-1);
if (ioctl(sockfd, SIOCGIFINDEX, &if_idx) < 0)
perror("SIOCGIFINDEX");
/* Get the MAC address of the interface to send on */
memset(&if_mac, 0, sizeof(struct ifreq));
strncpy(if_mac.ifr_name, ifName, IFNAMSIZ-1);
if (ioctl(sockfd, SIOCGIFHWADDR, &if_mac) < 0)
perror("SIOCGIFHWADDR");
/* Construct the Ethernet header */
memset(sendbuf, 0, BUF_SIZ);
/* Ethernet header */
eh->ether_shost[0] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[0];
eh->ether_shost[1] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[1];
eh->ether_shost[2] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[2];
eh->ether_shost[3] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[3];
eh->ether_shost[4] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[4];
eh->ether_shost[5] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[5];
eh->ether_dhost[0] = bytes[0];
eh->ether_dhost[1] = bytes[1];
eh->ether_dhost[2] = bytes[2];
eh->ether_dhost[3] = bytes[3];
eh->ether_dhost[4] = bytes[4];
eh->ether_dhost[5] = bytes[5];
/* Ethertype field */
eh->ether_type = htons(ETH_P_IP);
tx_len += sizeof(struct ether_header);
/* Packet data */
sendbuf[tx_len++] = 0xde;
sendbuf[tx_len++] = 0xad;
sendbuf[tx_len++] = 0xbe;
sendbuf[tx_len++] = 0xef;
/* Index of the network device */
socket_address.sll_ifindex = if_idx.ifr_ifindex;
/* Address length*/
socket_address.sll_halen = ETH_ALEN;
/* Destination MAC */
socket_address.sll_addr[0] = bytes[0];
socket_address.sll_addr[1] = bytes[1];
socket_address.sll_addr[2] = bytes[2];
socket_address.sll_addr[3] = bytes[3];
socket_address.sll_addr[4] = bytes[4];
socket_address.sll_addr[5] = bytes[5];
/* Send packet */
if (sendto(sockfd, sendbuf, tx_len, 0, (struct sockaddr*)&socket_address, sizeof(struct sockaddr_ll)) < 0)
printf("Send failed\n");
return 0;
}
@chrischiu71
Copy link

The content looks like it's sender not receiver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment