Track and report VPN providers states
[platform/upstream/connman.git] / src / ipv4.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. 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
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <string.h>
28 #include <sys/ioctl.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <net/if.h>
33
34 #include "connman.h"
35
36 struct connman_ipv4 {
37         enum connman_ipconfig_method method;
38         struct in_addr address;
39         struct in_addr netmask;
40         struct in_addr broadcast;
41 };
42
43 static int set_ipv4(struct connman_element *element, struct connman_ipv4 *ipv4)
44 {
45         struct ifreq ifr;
46         struct sockaddr_in addr;
47         int sk, err;
48
49         DBG("element %p ipv4 %p", element, ipv4);
50
51         sk = socket(PF_INET, SOCK_DGRAM, 0);
52         if (sk < 0)
53                 return -1;
54
55         memset(&ifr, 0, sizeof(ifr));
56         ifr.ifr_ifindex = element->index;
57
58         if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
59                 close(sk);
60                 return -1;
61         }
62
63         DBG("ifname %s", ifr.ifr_name);
64
65         memset(&addr, 0, sizeof(addr));
66         addr.sin_family = AF_INET;
67         addr.sin_addr = ipv4->address;
68         memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
69
70         err = ioctl(sk, SIOCSIFADDR, &ifr);
71
72         if (err < 0)
73                 DBG("address setting failed (%s)", strerror(errno));
74
75         memset(&addr, 0, sizeof(addr));
76         addr.sin_family = AF_INET;
77         addr.sin_addr = ipv4->netmask;
78         memcpy(&ifr.ifr_netmask, &addr, sizeof(ifr.ifr_netmask));
79
80         err = ioctl(sk, SIOCSIFNETMASK, &ifr);
81
82         if (err < 0)
83                 DBG("netmask setting failed (%s)", strerror(errno));
84
85         memset(&addr, 0, sizeof(addr));
86         addr.sin_family = AF_INET;
87         addr.sin_addr = ipv4->broadcast;
88         memcpy(&ifr.ifr_broadaddr, &addr, sizeof(ifr.ifr_broadaddr));
89
90         err = ioctl(sk, SIOCSIFBRDADDR, &ifr);
91
92         if (err < 0)
93                 DBG("broadcast setting failed (%s)", strerror(errno));
94
95         close(sk);
96
97         return 0;
98 }
99
100 static int clear_ipv4(struct connman_element *element)
101 {
102         struct ifreq ifr;
103         struct sockaddr_in addr;
104         int sk, err;
105
106         DBG("element %p", element);
107
108         sk = socket(PF_INET, SOCK_DGRAM, 0);
109         if (sk < 0)
110                 return -1;
111
112         memset(&ifr, 0, sizeof(ifr));
113         ifr.ifr_ifindex = element->index;
114
115         if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
116                 close(sk);
117                 return -1;
118         }
119
120         DBG("ifname %s", ifr.ifr_name);
121
122         memset(&addr, 0, sizeof(addr));
123         addr.sin_family = AF_INET;
124         addr.sin_addr.s_addr = INADDR_ANY;
125         memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
126
127         //err = ioctl(sk, SIOCDIFADDR, &ifr);
128         err = ioctl(sk, SIOCSIFADDR, &ifr);
129
130         close(sk);
131
132         if (err < 0 && errno != EADDRNOTAVAIL) {
133                 DBG("address removal failed (%s)", strerror(errno));
134                 return -1;
135         }
136
137         return 0;
138 }
139
140 static char *index2name(int index)
141 {
142         struct ifreq ifr;
143         int sk, err;
144
145         if (index < 0)
146                 return NULL;
147
148         sk = socket(PF_INET, SOCK_DGRAM, 0);
149         if (sk < 0)
150                 return NULL;
151
152         memset(&ifr, 0, sizeof(ifr));
153         ifr.ifr_ifindex = index;
154
155         err = ioctl(sk, SIOCGIFNAME, &ifr);
156
157         close(sk);
158
159         if (err < 0)
160                 return NULL;
161
162         return strdup(ifr.ifr_name);
163 }
164
165 static int ipv4_probe(struct connman_element *element)
166 {
167         struct connman_service *service;
168         struct connman_ipconfig *ipconfig;
169         struct connman_element *connection;
170         struct connman_ipv4 ipv4;
171         const char *address = NULL, *netmask = NULL, *broadcast = NULL;
172         const char *nameserver = NULL;
173         char *timeserver = NULL;
174
175         DBG("element %p name %s", element, element->name);
176
177         connman_element_get_value(element,
178                                 CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &address);
179         connman_element_get_value(element,
180                                 CONNMAN_PROPERTY_ID_IPV4_NETMASK, &netmask);
181         connman_element_get_value(element,
182                                 CONNMAN_PROPERTY_ID_IPV4_BROADCAST, &broadcast);
183
184         connman_element_get_value(element,
185                         CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver);
186         connman_element_get_value(element,
187                         CONNMAN_PROPERTY_ID_IPV4_TIMESERVER, &timeserver);
188
189         DBG("address %s", address);
190         DBG("netmask %s", netmask);
191         DBG("broadcast %s", broadcast);
192
193         if (address == NULL || netmask == NULL)
194                 return -EINVAL;
195
196         memset(&ipv4, 0, sizeof(ipv4));
197         ipv4.address.s_addr = inet_addr(address);
198         ipv4.netmask.s_addr = inet_addr(netmask);
199         if (broadcast)
200                 ipv4.broadcast.s_addr = inet_addr(broadcast);
201         else
202                 ipv4.broadcast.s_addr = ipv4.address.s_addr |
203                                                 ~ipv4.netmask.s_addr;
204
205         set_ipv4(element, &ipv4);
206
207         service = __connman_element_get_service(element);
208
209         if (nameserver != NULL)
210                 __connman_service_append_nameserver(service, nameserver);
211
212         connman_timeserver_append(timeserver);
213
214         connection = connman_element_create(NULL);
215
216         connection->type    = CONNMAN_ELEMENT_TYPE_CONNECTION;
217         connection->index   = element->index;
218         connection->devname = index2name(element->index);
219
220         ipconfig = __connman_service_get_ipconfig(service);
221         if (ipconfig != NULL)
222                 __connman_ipconfig_set_element_ipv6_gateway(
223                                                 ipconfig, connection);
224
225         if (connman_element_register(connection, element) < 0)
226                 connman_element_unref(connection);
227
228         return 0;
229 }
230
231 static void ipv4_remove(struct connman_element *element)
232 {
233         const char *nameserver = NULL;
234         char *timeserver = NULL;
235
236         DBG("element %p name %s", element, element->name);
237
238         connman_element_get_value(element,
239                         CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver);
240         connman_element_get_value(element,
241                         CONNMAN_PROPERTY_ID_IPV4_TIMESERVER, &timeserver);
242
243         connman_timeserver_remove(timeserver);
244
245         if (nameserver != NULL) {
246                 struct connman_service *service;
247
248                 service = __connman_element_get_service(element);
249                 __connman_service_remove_nameserver(service, nameserver);
250         }
251
252         clear_ipv4(element);
253 }
254
255 static struct connman_driver ipv4_driver = {
256         .name           = "ipv4",
257         .type           = CONNMAN_ELEMENT_TYPE_IPV4,
258         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
259         .probe          = ipv4_probe,
260         .remove         = ipv4_remove,
261 };
262
263 int __connman_ipv4_init(void)
264 {
265         return connman_driver_register(&ipv4_driver);
266 }
267
268 void __connman_ipv4_cleanup(void)
269 {
270         connman_driver_unregister(&ipv4_driver);
271 }