Don't use configure it's a waste of time
[l2tpns.git] / arp.c
1 #include <string.h>
2 #include <unistd.h>
3 #include <net/ethernet.h>
4 #include <net/if_arp.h>
5 #include <linux/if_packet.h>
6
7 #include "l2tpns.h"
8
9 /* Most of this code is based on keepalived:vrrp_arp.c */
10
11 struct arp_buf {
12 struct ether_header eth;
13 struct arphdr arp;
14
15 /* Data bit - variably sized, so not present in |struct arphdr| */
16 unsigned char ar_sha[ETH_ALEN]; /* Sender hardware address */
17 ipt ar_sip; /* Sender IP address. */
18 unsigned char ar_tha[ETH_ALEN]; /* Target hardware address */
19 ipt ar_tip; /* Target ip */
20 } __attribute__((packed));
21
22 void sendarp(int ifr_idx, const unsigned char* mac, ipt ip)
23 {
24 int fd;
25 struct sockaddr_ll sll;
26 struct arp_buf buf;
27
28 /* Ethernet */
29 memset(buf.eth.ether_dhost, 0xFF, ETH_ALEN);
30 memcpy(buf.eth.ether_shost, mac, ETH_ALEN);
31 buf.eth.ether_type = htons(ETHERTYPE_ARP);
32
33 /* ARP */
34 buf.arp.ar_hrd = htons(ARPHRD_ETHER);
35 buf.arp.ar_pro = htons(ETHERTYPE_IP);
36 buf.arp.ar_hln = ETH_ALEN;
37 buf.arp.ar_pln = 4; //IPPROTO_ADDR_LEN;
38 buf.arp.ar_op = htons(ARPOP_REQUEST);
39
40 /* Data */
41 memcpy(buf.ar_sha, mac, ETH_ALEN);
42 memcpy(&buf.ar_sip, &ip, sizeof(ip));
43 memcpy(buf.ar_tha, mac, ETH_ALEN);
44 memcpy(&buf.ar_tip, &ip, sizeof(ip));
45
46 /* Now actually send the thing */
47 fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_RARP));
48
49 memset(&sll, 0, sizeof(sll));
50 sll.sll_family = AF_PACKET;
51 strncpy(sll.sll_addr, mac, sizeof(sll.sll_addr) - 1);
52 sll.sll_halen = ETH_ALEN;
53 sll.sll_ifindex = ifr_idx;
54
55 sendto(fd, &buf, sizeof(buf), 0, (struct sockaddr*)&sll, sizeof(sll));
56 close(fd);
57 }