Search code examples
socketsmulticastethernetraw-sockets

Raw socket multicasting


I have a raw socket I have bound to eth2.

#define DEVICE_NAME "eth2"

// open a socket
int Socket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

if (Socket < 0)
{
    perror("socket() error");
    return -1;
}

// create a interface request structure
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));     

// set the interface name
strncpy(ifr.ifr_name, DEVICE_NAME, IFNAMSIZ); 

// get interface index
ioctl(Socket, SIOCGIFINDEX, &ifr);
int Socket_Index = ifr.ifr_ifindex;

// bind the socket to the interface
struct sockaddr_ll Socket_Addr;
Socket_Addr.sll_family = AF_PACKET;
Socket_Addr.sll_protocol = htons(ETH_P_ALL);
Socket_Addr.sll_ifindex = Socket_Index;
bind(Socket, (struct sockaddr *)&Socket_Addr, sizeof(Socket_Addr)); 

// add multicast addresses to the socket, based on Unit Number

struct packet_mreq mreq;
mreq.mr_ifindex = Socket_Index;
mreq.mr_type = PACKET_MR_MULTICAST;
mreq.mr_alen = ETH_ALEN; 

memcpy(mreq.mr_address, Addresses[UNITS_1_2], ETH_ALEN);
setsockopt(Socket, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq));

memcpy(mreq.mr_address, Addresses[UNIT_3], ETH_ALEN);
setsockopt(Socket, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq));

Where Addresses[UNITS_1_2] resolves to 03:00:00:01:04:00 and Addresses[UNIT_3] resolves to 02:00:00:01:04:01.

The socket is only receiving the multicast packets, and not the unicast ones. While debugging I started tcpdump and low-and-behold going to promiscuous mode did the trick.

My question is: Can I receive both multicast and unicast packets on the same raw socket without promiscuous mode? I have tried adding 02:00:00:01:04:01 to eth0s mac addresses using maddr, with no luck.


Solution

  • Sneaking from gabhijit: Try adding

    Socket_Addr.sll_pkttype = PACKET_HOST | PACKET_MULTICAST;