Export IPv4 PAC
[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, *pac = 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         connman_element_get_value(element,
189                         CONNMAN_PROPERTY_ID_IPV4_PAC, &pac);
190
191         DBG("address %s", address);
192         DBG("netmask %s", netmask);
193         DBG("broadcast %s", broadcast);
194
195         if (address == NULL || netmask == NULL)
196                 return -EINVAL;
197
198         memset(&ipv4, 0, sizeof(ipv4));
199         ipv4.address.s_addr = inet_addr(address);
200         ipv4.netmask.s_addr = inet_addr(netmask);
201         if (broadcast)
202                 ipv4.broadcast.s_addr = inet_addr(broadcast);
203         else
204                 ipv4.broadcast.s_addr = ipv4.address.s_addr |
205                                                 ~ipv4.netmask.s_addr;
206
207         set_ipv4(element, &ipv4);
208
209         service = __connman_element_get_service(element);
210
211         if (pac != NULL)
212                 __connman_service_set_proxy_autoconfig(service, pac);
213
214         if (nameserver != NULL)
215                 __connman_service_append_nameserver(service, nameserver);
216
217         connman_timeserver_append(timeserver);
218
219         connection = connman_element_create(NULL);
220
221         connection->type    = CONNMAN_ELEMENT_TYPE_CONNECTION;
222         connection->index   = element->index;
223         connection->devname = index2name(element->index);
224
225         ipconfig = __connman_service_get_ipconfig(service);
226         if (ipconfig != NULL)
227                 __connman_ipconfig_set_element_ipv6_gateway(
228                                                 ipconfig, connection);
229
230         if (connman_element_register(connection, element) < 0)
231                 connman_element_unref(connection);
232
233         return 0;
234 }
235
236 static void ipv4_remove(struct connman_element *element)
237 {
238         const char *nameserver = NULL;
239         char *timeserver = NULL;
240
241         DBG("element %p name %s", element, element->name);
242
243         connman_element_get_value(element,
244                         CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver);
245         connman_element_get_value(element,
246                         CONNMAN_PROPERTY_ID_IPV4_TIMESERVER, &timeserver);
247
248         connman_timeserver_remove(timeserver);
249
250         if (nameserver != NULL) {
251                 struct connman_service *service;
252
253                 service = __connman_element_get_service(element);
254                 __connman_service_remove_nameserver(service, nameserver);
255         }
256
257         clear_ipv4(element);
258 }
259
260 static struct connman_driver ipv4_driver = {
261         .name           = "ipv4",
262         .type           = CONNMAN_ELEMENT_TYPE_IPV4,
263         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
264         .probe          = ipv4_probe,
265         .remove         = ipv4_remove,
266 };
267
268 int __connman_ipv4_init(void)
269 {
270         return connman_driver_register(&ipv4_driver);
271 }
272
273 void __connman_ipv4_cleanup(void)
274 {
275         connman_driver_unregister(&ipv4_driver);
276 }