Imported Upstream version 1.37
[platform/upstream/connman.git] / src / shared / arp.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  based on IPv4 Local Link library with GLib integration,
6  *          Copyright (C) 2009-2010  Aldebaran Robotics. All rights reserved.
7  *
8  *  Copyright (C) 2018  Commend International. All rights reserved.
9  *
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.
13  *
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.
18  *
19  */
20 #include <string.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <unistd.h>
24
25 #include <sys/time.h>
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>
32
33 #include <arpa/inet.h>
34
35 #include "src/shared/arp.h"
36 #include "src/connman.h"
37
38 int arp_send_packet(uint8_t* source_eth, uint32_t source_ip,
39                     uint32_t target_ip, int ifindex)
40 {
41         struct sockaddr_ll dest;
42         struct ether_arp p;
43         uint32_t ip_source;
44         uint32_t ip_target;
45         int fd, n;
46
47         fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
48         if (fd < 0)
49                 return -errno;
50
51         memset(&dest, 0, sizeof(dest));
52         memset(&p, 0, sizeof(p));
53
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) {
60                 int err = errno;
61                 close(fd);
62                 return -err;
63         }
64
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);
69         p.arp_hln = ETH_ALEN;
70         p.arp_pln = 4;
71         p.arp_op = htons(ARPOP_REQUEST);
72
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));
76
77         n = sendto(fd, &p, sizeof(p), 0,
78                (struct sockaddr*) &dest, sizeof(dest));
79         if (n < 0)
80                 n = -errno;
81
82         close(fd);
83
84         return n;
85 }
86
87 int arp_socket(int ifindex)
88 {
89         int fd;
90         struct sockaddr_ll sock;
91
92         fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
93         if (fd < 0)
94                 return fd;
95
96         memset(&sock, 0, sizeof(sock));
97
98         sock.sll_family = AF_PACKET;
99         sock.sll_protocol = htons(ETH_P_ARP);
100         sock.sll_ifindex = ifindex;
101
102         if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
103                 int err = errno;
104                 close(fd);
105                 return -err;
106         }
107
108         return fd;
109 }
110
111 /**
112  * Return a random link local IP (in host byte order)
113  */
114 uint32_t arp_random_ip(void)
115 {
116         unsigned tmp;
117
118         do {
119                 uint64_t rand;
120                 __connman_util_get_random(&rand);
121                 tmp = rand;
122                 tmp = tmp & IN_CLASSB_HOST;
123         } while (tmp > (IN_CLASSB_HOST - 0x0200));
124
125         return (LINKLOCAL_ADDR + 0x0100) + tmp;
126 }