Imported Upstream version 1.29
[platform/upstream/connman.git] / gdhcp / ipv4ll.c
1 /*
2  *
3  *  IPV4 Local Link library with GLib integration
4  *
5  *  Copyright (C) 2009-2010  Aldebaran Robotics. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21 #include <string.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <unistd.h>
25
26 #include <sys/time.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
29 #include <netpacket/packet.h>
30 #include <net/ethernet.h>
31 #include <netinet/if_ether.h>
32
33 #include <arpa/inet.h>
34
35 #include <glib.h>
36 #include "ipv4ll.h"
37 #include "common.h"
38
39 /**
40  * Return a random link local IP (in host byte order)
41  */
42 uint32_t ipv4ll_random_ip(void)
43 {
44         unsigned tmp;
45         uint64_t rand;
46
47         do {
48                 dhcp_get_random(&rand);
49                 tmp = rand;
50                 tmp = tmp & IN_CLASSB_HOST;
51         } while (tmp > (IN_CLASSB_HOST - 0x0200));
52         return ((LINKLOCAL_ADDR + 0x0100) + tmp);
53 }
54
55 /**
56  * Return a random delay in range of zero to secs*1000
57  */
58 guint ipv4ll_random_delay_ms(guint secs)
59 {
60         uint64_t rand;
61
62         dhcp_get_random(&rand);
63         return rand % (secs * 1000);
64 }
65
66 int ipv4ll_send_arp_packet(uint8_t* source_eth, uint32_t source_ip,
67                     uint32_t target_ip, int ifindex)
68 {
69         struct sockaddr_ll dest;
70         struct ether_arp p;
71         uint32_t ip_source;
72         uint32_t ip_target;
73         int fd, n;
74
75         fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_ARP));
76         if (fd < 0)
77                 return -errno;
78
79         memset(&dest, 0, sizeof(dest));
80         memset(&p, 0, sizeof(p));
81
82         dest.sll_family = AF_PACKET;
83         dest.sll_protocol = htons(ETH_P_ARP);
84         dest.sll_ifindex = ifindex;
85         dest.sll_halen = ETH_ALEN;
86         memset(dest.sll_addr, 0xFF, ETH_ALEN);
87         if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
88                 close(fd);
89                 return -errno;
90         }
91
92         ip_source = htonl(source_ip);
93         ip_target = htonl(target_ip);
94         p.arp_hrd = htons(ARPHRD_ETHER);
95         p.arp_pro = htons(ETHERTYPE_IP);
96         p.arp_hln = ETH_ALEN;
97         p.arp_pln = 4;
98         p.arp_op = htons(ARPOP_REQUEST);
99
100         memcpy(&p.arp_sha, source_eth, ETH_ALEN);
101         memcpy(&p.arp_spa, &ip_source, sizeof(p.arp_spa));
102         memcpy(&p.arp_tpa, &ip_target, sizeof(p.arp_tpa));
103
104         n = sendto(fd, &p, sizeof(p), 0,
105                (struct sockaddr*) &dest, sizeof(dest));
106         if (n < 0)
107                 n = -errno;
108
109         close(fd);
110
111         return n;
112 }
113
114 int ipv4ll_arp_socket(int ifindex)
115 {
116         int fd;
117         struct sockaddr_ll sock;
118
119         fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_ARP));
120         if (fd < 0)
121                 return fd;
122
123         memset(&sock, 0, sizeof(sock));
124
125         sock.sll_family = AF_PACKET;
126         sock.sll_protocol = htons(ETH_P_ARP);
127         sock.sll_ifindex = ifindex;
128
129         if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
130                 close(fd);
131                 return -errno;
132         }
133
134         return fd;
135 }