-
-
Save vthanki/fdb19abd19c002195204ae96fb54cea1 to your computer and use it in GitHub Desktop.
Send a raw Ethernet frame in Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 MY_DEST_MAC0 0x00 | |
#define MY_DEST_MAC1 0x00 | |
#define MY_DEST_MAC2 0x00 | |
#define MY_DEST_MAC3 0x00 | |
#define MY_DEST_MAC4 0x00 | |
#define MY_DEST_MAC5 0x00 | |
#define DEFAULT_IF "eth0" | |
#define BUF_SIZ 1024 | |
int main(int argc, char *argv[]) | |
{ | |
int sockfd; | |
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]; | |
unsigned char dst_mac[6] = {0}; | |
int user_dst_mac = 0; | |
char octet[3] = {0}; | |
int i; | |
/* Get interface name */ | |
if (argc > 1) { | |
strcpy(ifName, argv[1]); | |
if (argc == 3) { | |
/* User supplied MAC address in form of | |
* xx:xx:xx:xx:xx:xx | |
*/ | |
if (strlen(argv[2]) != 17) { | |
printf("Invalid mac address len:%d\n", strlen(argv[2])); | |
return -1; | |
} | |
for (i = 0; i < 6; i++) { | |
octet[0] = argv[2][3*i+0]; | |
octet[1] = argv[2][3*i+1]; | |
dst_mac[i] = (unsigned char) strtol(octet, NULL, 16); | |
printf("MAC[%d] = %x\n", i, dst_mac[i] & 0xff); | |
user_dst_mac = 1; | |
} | |
} | |
} | |
else | |
strcpy(ifName, DEFAULT_IF); | |
/* 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 */ | |
for (i = 0; i < 6; i++) { | |
eh->ether_shost[i] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[i]; | |
if (user_dst_mac) | |
eh->ether_dhost[i] = dst_mac[i]; | |
} | |
/* 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; | |
memset(&socket_address, 0x0, sizeof(socket_address)); | |
/* Index of the network device */ | |
socket_address.sll_ifindex = if_idx.ifr_ifindex; | |
/* Address length*/ | |
socket_address.sll_halen = ETH_ALEN; | |
/* Destination MAC */ | |
if (user_dst_mac) | |
for (i = 0; i < 6; i++) | |
socket_address.sll_addr[i] = dst_mac[i]; | |
/* Send packet */ | |
if (sendto(sockfd, sendbuf, tx_len, 0, (struct | |
sockaddr*)&socket_address, sizeof(struct sockaddr_ll)) < 0) | |
printf("Send failed\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment