Reputation: 21
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <linux/if_ether.h>
#include <net/if.h>
#include <netpacket/packet.h>
struct ethernet {
unsigned char dest[6];
unsigned char source[6];
uint16_t eth_type;
};
struct arp {
uint16_t htype;
uint16_t ptype;
unsigned char hlen;
unsigned char plen;
uint16_t oper;
/* addresses */
unsigned char sender_ha[6];
unsigned char sender_pa[4];
unsigned char target_ha[6];
unsigned char target_pa[4];
};
#define ETH_HDR_LEN 14
#define BUFF_SIZE 2048
#define ARP_PROTO 0x0806
static void dump_arp(struct arp *arp_hdr);
int main(void)
{
int sock, err;
void *buffer = NULL;
ssize_t recvd_size;
struct sockaddr_ll s_ll;
struct ethernet *eth_hdr = NULL;
struct arp *arp_hdr = NULL;
if( (sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1)
// if( (sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1)
{
perror("socket(): ");
exit(-1);
}
s_ll.sll_family = AF_PACKET;
s_ll.sll_protocol = htons(ETH_P_ARP);
//s_ll.sll_protocol = htons(ETH_P_ARP);
s_ll.sll_ifindex = 0; // all ifaces
//s_ll.sll_ifindex = 2 // eth0
if( (err = bind(sock, (struct sockaddr *)&s_ll, sizeof(s_ll))) == -1)
{
perror("bind(): ");
exit(-1);
}
buffer = malloc(BUFF_SIZE);
while(1)
{
if( (recvd_size = recv(sock, buffer, BUFF_SIZE, 0)) == -1)
{
perror("recv(): ");
free(buffer);
close(sock);
exit(-1);
}
if(recvd_size <= (sizeof(struct ethernet) + sizeof(struct arp)))
{
printf("Short packet. Packet len: %d\n", recvd_size);
continue;
}
eth_hdr = (struct ethernet *)buffer;
if(ntohs(eth_hdr->eth_type) != ARP_PROTO)
continue;
arp_hdr = (struct arp *)(buffer+ETH_HDR_LEN);
dump_arp(arp_hdr);
}
free(buffer);
close(sock);
}
static void
dump_arp(struct arp *arp_hdr)
{
uint16_t htype = ntohs(arp_hdr->htype);
uint16_t ptype = ntohs(arp_hdr->ptype);
uint16_t oper = ntohs(arp_hdr->oper);
switch(htype)
{
case 0x0001:
printf("ARP HTYPE: Ethernet(0x%04X)\n", htype);
break;
default:
printf("ARP HYPE: 0x%04X\n", htype);
break;
}
switch(ptype)
{
case 0x0800:
printf("ARP PTYPE: IPv4(0x%04X)\n", ptype);
break;
default:
printf("ARP PTYPE: 0x%04X\n", ptype);
break;
}
printf("ARP HLEN: %d\n", arp_hdr->hlen);
printf("ARP PLEN: %d\n", arp_hdr->plen);
switch(oper)
{
case 0x0001:
printf("ARP OPER: Request(0x%04X)\n", oper);
break;
case 0x0002:
printf("ARP OPER: Response(0x%04X)\n", oper);
break;
default:
printf("ARP OPER: 0x%04X\n", oper);
break;
}
printf("ARP Sender HA: %02X:%02X:%02X:%02X:%02X:%02X\n",
arp_hdr->sender_ha[0],arp_hdr->sender_ha[1],arp_hdr->sender_ha[2],
arp_hdr->sender_ha[3], arp_hdr->sender_ha[4], arp_hdr->sender_ha[5]);
printf("ARP Sender PA: %d.%d.%d.%d\n", arp_hdr->sender_pa[0],
arp_hdr->sender_pa[1], arp_hdr->sender_pa[2], arp_hdr->sender_pa[3]);
printf("ARP Target HA: %02X:%02X:%02X:%02X:%02X:%02X\n",
arp_hdr->target_ha[0],arp_hdr->target_ha[1],arp_hdr->target_ha[2],
arp_hdr->target_ha[3], arp_hdr->target_ha[4], arp_hdr->target_ha[5]);
printf("ARP Target PA: %d.%d.%d.%d\n", arp_hdr->target_pa[0],
arp_hdr->target_pa[1], arp_hdr->target_pa[2], arp_hdr->target_pa[3]);
printf("ARP DONE =====================\n");
}
I create socket() with AF_PACKET, SOCK_RAW, ETH_P_ARP args. And bind() it on all interfaces(it does not matter) with args( AF_PACKET, ETH_P_ARP). So, all ARP packet's must flow through this socket.
My host: 192.168.1.2 remote host: 192.168.1.7, host don't contain ARP record of 192.167.1.7.
Program output, when i make ping 192.168.1.7:
... ARP OPER: Response(0x0002) ARP Sender HA: 50:67:F0:94:70:F5 ARP Sender PA: 192.168.1.7 ARP Target HA: 00:22:15:A2:D0:C5 ARP Target PA: 192.168.1.2 ARP DONE ===================== ... ARP OPER: Request(0x0001) ARP Sender HA: 50:67:F0:94:70:F5 ARP Sender PA: 192.168.1.7 ARP Target HA: 00:00:00:00:00:00 ARP Target PA: 192.168.1.2 ARP DONE =====================
My socket got only 2 packet's of 4(my_host request and my_host response is missed). tcpdump -n -p -i eth0 arp show all 4 packet's.
If i change ETH_P_ARP to ETH_P_ALL in socket() and bind() then all 4 packet's goes to socket(with IP and others).
Why? How to fix that?
PS. Please tell me address of some mailing list, where i can ask about this behavior.
Upvotes: 2
Views: 5452
Reputation: 2020
Kind of a late answer, but I experimented with this one for fun. Maybe some googler/duckduckgoer can benefit.
My suggestion is to use ETH_P_ALL to receive all packets, but then filter them with a Linux socket filter, so that the application only receives the requested ARP packets.
Here is my code. The larges changes are marked with a CHANGE comment
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <linux/if_ether.h>
#include <net/if.h>
#include <netpacket/packet.h>
#include <linux/filter.h> // CHANGE: include lsf
struct ethernet {
unsigned char dest[6];
unsigned char source[6];
uint16_t eth_type;
};
struct arp {
uint16_t htype;
uint16_t ptype;
unsigned char hlen;
unsigned char plen;
uint16_t oper;
/* addresses */
unsigned char sender_ha[6];
unsigned char sender_pa[4];
unsigned char target_ha[6];
unsigned char target_pa[4];
};
#define ETH_HDR_LEN 14
#define BUFF_SIZE 2048
/* CHANGE
Linux socket filters use the Berkeley packet filter syntax.
This was adapted from BSDs "man 4 bpf" example for RARP.
*/
struct sock_filter arpfilter[] = {
BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12), /* Skip 12 bytes */
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETH_P_ARP, 0, 1), /* if eth type != ARP
skip next instr. */
BPF_STMT(BPF_RET+BPF_K, sizeof(struct arp) +
sizeof(struct ethernet)),
BPF_STMT(BPF_RET+BPF_K, 0), /* Return, either the ARP packet or nil */
};
static void dump_arp(struct arp *arp_hdr);
int main(void)
{
int sock;
void *buffer = NULL;
ssize_t recvd_size;
struct ethernet *eth_hdr = NULL;
struct arp *arp_hdr = NULL;
struct sock_filter *filter;
struct sock_fprog fprog;
if( (sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1)
{
perror("socket(): ");
exit(-1);
}
/* CHANGE prepare linux packet filter */
if ((filter = malloc(sizeof(arpfilter))) == NULL) {
perror("malloc");
close(sock);
exit(1);
}
memcpy(filter, &arpfilter, sizeof(arpfilter));
fprog.filter = filter;
fprog.len = sizeof(arpfilter)/sizeof(struct sock_filter);
/* CHANGE add filter */
if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)) == -1) {
perror("setsockopt");
close(sock);
exit(1);
}
buffer = malloc(BUFF_SIZE);
while(1)
{
if( (recvd_size = recv(sock, buffer, BUFF_SIZE, 0)) < 0)
{
perror("recv(): ");
free(buffer);
close(sock);
exit(-1);
}
if((size_t)recvd_size < (sizeof(struct ethernet) + sizeof(struct arp)))
{
printf("Short packet. Packet len: %ld\n", recvd_size);
continue;
}
eth_hdr = (struct ethernet *)buffer;
if(ntohs(eth_hdr->eth_type) != ETH_P_ARP) {
printf("Received wrong ethernet type: %X\n", eth_hdr->eth_type);
exit(1);
}
arp_hdr = (struct arp *)(buffer+ETH_HDR_LEN);
dump_arp(arp_hdr);
}
free(buffer);
close(sock);
}
static void
dump_arp(struct arp *arp_hdr)
{
uint16_t htype = ntohs(arp_hdr->htype);
uint16_t ptype = ntohs(arp_hdr->ptype);
uint16_t oper = ntohs(arp_hdr->oper);
switch(htype)
{
case 0x0001:
printf("ARP HTYPE: Ethernet(0x%04X)\n", htype);
break;
default:
printf("ARP HYPE: 0x%04X\n", htype);
break;
}
switch(ptype)
{
case 0x0800:
printf("ARP PTYPE: IPv4(0x%04X)\n", ptype);
break;
default:
printf("ARP PTYPE: 0x%04X\n", ptype);
break;
}
printf("ARP HLEN: %d\n", arp_hdr->hlen);
printf("ARP PLEN: %d\n", arp_hdr->plen);
switch(oper)
{
case 0x0001:
printf("ARP OPER: Request(0x%04X)\n", oper);
break;
case 0x0002:
printf("ARP OPER: Response(0x%04X)\n", oper);
break;
default:
printf("ARP OPER: 0x%04X\n", oper);
break;
}
printf("ARP Sender HA: %02X:%02X:%02X:%02X:%02X:%02X\n",
arp_hdr->sender_ha[0],arp_hdr->sender_ha[1],arp_hdr->sender_ha[2],
arp_hdr->sender_ha[3], arp_hdr->sender_ha[4], arp_hdr->sender_ha[5]);
printf("ARP Sender PA: %d.%d.%d.%d\n", arp_hdr->sender_pa[0],
arp_hdr->sender_pa[1], arp_hdr->sender_pa[2], arp_hdr->sender_pa[3]);
printf("ARP Target HA: %02X:%02X:%02X:%02X:%02X:%02X\n",
arp_hdr->target_ha[0],arp_hdr->target_ha[1],arp_hdr->target_ha[2],
arp_hdr->target_ha[3], arp_hdr->target_ha[4], arp_hdr->target_ha[5]);
printf("ARP Target PA: %d.%d.%d.%d\n", arp_hdr->target_pa[0],
arp_hdr->target_pa[1], arp_hdr->target_pa[2], arp_hdr->target_pa[3]);
printf("ARP DONE =====================\n");
}
I also removed the bind()
as unnecessary and corrected an off by one in the comparison of the captured packet size. The comparison was <= sizeof(struct ethernet) + sizeof(struct arp)
when it should be <
Short of reading the packet socket kernel source, which Im not up for now, I did not find a good explanation for why the example code only receives packets addressed to the hosts IP. As the OP and many examples around the Internet confirm, when the level is ETH_P_ALL, also the outgoing packets are received by the socket. I guess it is just an implementation choise. It might be the preferrable behaviour for most applications, e.g. those implementing a protocol, instead of snooping on an existing one.
NOTE Speaking of the kernel source, I am not at all sure why the lsf/bpf filter works when I give it the two byte ETH_P_ARP without byteorder modifications. I think it might be because of these lines in the kernel:
case BPF_S_ANC_PROTOCOL:
A = ntohs(skb->protocol);
from http://lxr.linux.no/#linux+v3.11/net/core/filter.c#L317
Upvotes: 4