5 * based on IPv4 Local Link library with GLib integration,
6 * Copyright (C) 2009-2010 Aldebaran Robotics. All rights reserved.
8 * Copyright (C) 2018 Commend International. All rights reserved.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <netpacket/packet.h>
29 #include <net/ethernet.h>
30 #include <netinet/if_ether.h>
31 #include <net/if_arp.h>
33 #include <arpa/inet.h>
35 #include "src/shared/arp.h"
36 #include "src/connman.h"
38 int arp_send_packet(uint8_t* source_eth, uint32_t source_ip,
39 uint32_t target_ip, int ifindex)
41 struct sockaddr_ll dest;
47 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
51 memset(&dest, 0, sizeof(dest));
52 memset(&p, 0, sizeof(p));
54 dest.sll_family = AF_PACKET;
55 dest.sll_protocol = htons(ETH_P_ARP);
56 dest.sll_ifindex = ifindex;
57 dest.sll_halen = ETH_ALEN;
58 memset(dest.sll_addr, 0xFF, ETH_ALEN);
59 if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
65 ip_source = htonl(source_ip);
66 ip_target = htonl(target_ip);
67 p.arp_hrd = htons(ARPHRD_ETHER);
68 p.arp_pro = htons(ETHERTYPE_IP);
71 p.arp_op = htons(ARPOP_REQUEST);
73 memcpy(&p.arp_sha, source_eth, ETH_ALEN);
74 memcpy(&p.arp_spa, &ip_source, sizeof(p.arp_spa));
75 memcpy(&p.arp_tpa, &ip_target, sizeof(p.arp_tpa));
77 n = sendto(fd, &p, sizeof(p), 0,
78 (struct sockaddr*) &dest, sizeof(dest));
87 int arp_socket(int ifindex)
90 struct sockaddr_ll sock;
92 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
96 memset(&sock, 0, sizeof(sock));
98 sock.sll_family = AF_PACKET;
99 sock.sll_protocol = htons(ETH_P_ARP);
100 sock.sll_ifindex = ifindex;
102 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
112 * Return a random link local IP (in host byte order)
114 uint32_t arp_random_ip(void)
120 __connman_util_get_random(&rand);
122 tmp = tmp & IN_CLASSB_HOST;
123 } while (tmp > (IN_CLASSB_HOST - 0x0200));
125 return (LINKLOCAL_ADDR + 0x0100) + tmp;