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