Add support for IPv4 details in connection interface
[framework/connectivity/connman.git] / src / connection.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 <errno.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <sys/ioctl.h>
30 #include <arpa/inet.h>
31 #include <net/if.h>
32 #include <net/route.h>
33
34 #include <gdbus.h>
35
36 #include "connman.h"
37
38 struct gateway_data {
39         int index;
40         char *gateway;
41 };
42
43 static GSList *gateway_list = NULL;
44
45 static struct gateway_data *find_gateway(int index, const char *gateway)
46 {
47         GSList *list;
48
49         if (gateway == NULL)
50                 return NULL;
51
52         for (list = gateway_list; list; list = list->next) {
53                 struct gateway_data *data = list->data;
54
55                 if (data->gateway == NULL)
56                         continue;
57
58                 if (data->index == index &&
59                                 g_str_equal(data->gateway, gateway) == TRUE)
60                         return data;
61         }
62
63         return NULL;
64 }
65
66 static void connection_newgateway(int index, const char *gateway)
67 {
68         struct gateway_data *data;
69
70         DBG("index %d gateway %s", index, gateway);
71
72         data = find_gateway(index, gateway);
73         if (data != NULL)
74                 return;
75
76         data = g_try_new0(struct gateway_data, 1);
77         if (data == NULL)
78                 return;
79
80         data->index = index;
81         data->gateway = g_strdup(gateway);
82
83         gateway_list = g_slist_append(gateway_list, data);
84 }
85
86 static void connection_delgateway(int index, const char *gateway)
87 {
88         struct gateway_data *data;
89
90         DBG("index %d gateway %s", index, gateway);
91
92         data = find_gateway(index, gateway);
93         if (data == NULL)
94                 return;
95
96         gateway_list = g_slist_remove(gateway_list, data);
97
98         g_free(data->gateway);
99         g_free(data);
100 }
101
102 static struct connman_rtnl connection_rtnl = {
103         .name           = "connection",
104         .newgateway     = connection_newgateway,
105         .delgateway     = connection_delgateway,
106 };
107
108 static int set_route(struct connman_element *element, const char *gateway)
109 {
110         struct ifreq ifr;
111         struct rtentry rt;
112         struct sockaddr_in *addr;
113         int sk, err;
114
115         DBG("element %p", element);
116
117         sk = socket(PF_INET, SOCK_DGRAM, 0);
118         if (sk < 0)
119                 return -1;
120
121         memset(&ifr, 0, sizeof(ifr));
122         ifr.ifr_ifindex = element->index;
123
124         if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
125                 close(sk);
126                 return -1;
127         }
128
129         DBG("ifname %s", ifr.ifr_name);
130
131         memset(&rt, 0, sizeof(rt));
132         rt.rt_flags = RTF_UP | RTF_GATEWAY;
133
134         addr = (struct sockaddr_in *) &rt.rt_dst;
135         addr->sin_family = AF_INET;
136         addr->sin_addr.s_addr = INADDR_ANY;
137
138         addr = (struct sockaddr_in *) &rt.rt_gateway;
139         addr->sin_family = AF_INET;
140         addr->sin_addr.s_addr = inet_addr(gateway);
141
142         addr = (struct sockaddr_in *) &rt.rt_genmask;
143         addr->sin_family = AF_INET;
144         addr->sin_addr.s_addr = INADDR_ANY;
145
146         err = ioctl(sk, SIOCADDRT, &rt);
147         if (err < 0)
148                 DBG("default route setting failed (%s)", strerror(errno));
149
150         close(sk);
151
152         return err;
153 }
154
155 static int del_route(struct connman_element *element, const char *gateway)
156 {
157         struct ifreq ifr;
158         struct rtentry rt;
159         struct sockaddr_in *addr;
160         int sk, err;
161
162         DBG("element %p", element);
163
164         sk = socket(PF_INET, SOCK_DGRAM, 0);
165         if (sk < 0)
166                 return -1;
167
168         memset(&ifr, 0, sizeof(ifr));
169         ifr.ifr_ifindex = element->index;
170
171         if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
172                 close(sk);
173                 return -1;
174         }
175
176         DBG("ifname %s", ifr.ifr_name);
177
178         memset(&rt, 0, sizeof(rt));
179         rt.rt_flags = RTF_UP | RTF_GATEWAY;
180
181         addr = (struct sockaddr_in *) &rt.rt_dst;
182         addr->sin_family = AF_INET;
183         addr->sin_addr.s_addr = INADDR_ANY;
184
185         addr = (struct sockaddr_in *) &rt.rt_gateway;
186         addr->sin_family = AF_INET;
187         addr->sin_addr.s_addr = inet_addr(gateway);
188
189         addr = (struct sockaddr_in *) &rt.rt_genmask;
190         addr->sin_family = AF_INET;
191         addr->sin_addr.s_addr = INADDR_ANY;
192
193         err = ioctl(sk, SIOCDELRT, &rt);
194         if (err < 0)
195                 DBG("default route removal failed (%s)", strerror(errno));
196
197         close(sk);
198
199         return err;
200 }
201
202 static DBusMessage *get_properties(DBusConnection *conn,
203                                         DBusMessage *msg, void *data)
204 {
205         struct connman_element *element = data;
206         DBusMessage *reply;
207         DBusMessageIter array, dict;
208         const char *method = NULL;
209         const char *address = NULL, *netmask = NULL, *gateway = NULL;
210
211         DBG("conn %p", conn);
212
213         reply = dbus_message_new_method_return(msg);
214         if (reply == NULL)
215                 return NULL;
216
217         dbus_message_iter_init_append(reply, &array);
218
219         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
220                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
221                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
222                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
223
224         if (element->devname != NULL)
225                 connman_dbus_dict_append_variant(&dict, "Interface",
226                                         DBUS_TYPE_STRING, &element->devname);
227
228         connman_dbus_dict_append_variant(&dict, "Default",
229                                         DBUS_TYPE_BOOLEAN, &element->enabled);
230
231         connman_element_get_value(element,
232                                 CONNMAN_PROPERTY_ID_IPV4_METHOD, &method);
233
234         connman_element_get_value(element,
235                                 CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &address);
236         connman_element_get_value(element,
237                                 CONNMAN_PROPERTY_ID_IPV4_NETMASK, &netmask);
238         connman_element_get_value(element,
239                                 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
240
241         if (method != NULL)
242                 connman_dbus_dict_append_variant(&dict, "IPv4.Method",
243                                                 DBUS_TYPE_STRING, &method);
244
245         if (address != NULL)
246                 connman_dbus_dict_append_variant(&dict, "IPv4.Address",
247                                                 DBUS_TYPE_STRING, &address);
248
249         if (netmask != NULL)
250                 connman_dbus_dict_append_variant(&dict, "IPv4.Netmask",
251                                                 DBUS_TYPE_STRING, &netmask);
252
253         if (gateway != NULL)
254                 connman_dbus_dict_append_variant(&dict, "IPv4.Gateway",
255                                                 DBUS_TYPE_STRING, &gateway);
256
257         dbus_message_iter_close_container(&array, &dict);
258
259         return reply;
260 }
261
262 static DBusMessage *set_property(DBusConnection *conn,
263                                         DBusMessage *msg, void *data)
264 {
265         DBusMessageIter iter, value;
266         const char *name;
267
268         DBG("conn %p", conn);
269
270         if (dbus_message_iter_init(msg, &iter) == FALSE)
271                 return __connman_error_invalid_arguments(msg);
272
273         dbus_message_iter_get_basic(&iter, &name);
274         dbus_message_iter_next(&iter);
275         dbus_message_iter_recurse(&iter, &value);
276
277         if (__connman_security_check_privileges(msg) < 0)
278                 return __connman_error_permission_denied(msg);
279
280         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
281 }
282
283 static GDBusMethodTable connection_methods[] = {
284         { "GetProperties", "",   "a{sv}", get_properties },
285         { "SetProperty",   "sv", "",      set_property   },
286         { },
287 };
288
289 static GDBusSignalTable connection_signals[] = {
290         { "PropertyChanged", "sv" },
291         { },
292 };
293
294 static DBusConnection *connection;
295
296 static void emit_connections_signal(void)
297 {
298 }
299
300 static int register_interface(struct connman_element *element)
301 {
302         DBG("element %p name %s", element, element->name);
303
304         if (g_dbus_register_interface(connection, element->path,
305                                         CONNMAN_CONNECTION_INTERFACE,
306                                         connection_methods, connection_signals,
307                                         NULL, element, NULL) == FALSE) {
308                 connman_error("Failed to register %s connection", element->path);
309                 return -EIO;
310         }
311
312         emit_connections_signal();
313
314         return 0;
315 }
316
317 static void unregister_interface(struct connman_element *element)
318 {
319         DBG("element %p name %s", element, element->name);
320
321         emit_connections_signal();
322
323         g_dbus_unregister_interface(connection, element->path,
324                                                 CONNMAN_CONNECTION_INTERFACE);
325 }
326
327 static int connection_probe(struct connman_element *element)
328 {
329         const char *gateway = NULL;
330
331         DBG("element %p name %s", element, element->name);
332
333         if (element->parent == NULL)
334                 return -ENODEV;
335
336         if (element->parent->type != CONNMAN_ELEMENT_TYPE_IPV4)
337                 return -ENODEV;
338
339         connman_element_get_value(element,
340                                 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
341
342         DBG("gateway %s", gateway);
343
344         if (register_interface(element) < 0)
345                 return -ENODEV;
346
347         if (gateway == NULL)
348                 return 0;
349
350         if (g_slist_length(gateway_list) > 0) {
351                 DBG("default gateway already present");
352                 return 0;
353         }
354
355         set_route(element, gateway);
356
357         connman_element_set_enabled(element, TRUE);
358
359         return 0;
360 }
361
362 static void connection_remove(struct connman_element *element)
363 {
364         const char *gateway = NULL;
365
366         DBG("element %p name %s", element, element->name);
367
368         unregister_interface(element);
369
370         connman_element_get_value(element,
371                                 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
372
373         DBG("gateway %s", gateway);
374
375         if (gateway == NULL)
376                 return;
377
378         del_route(element, gateway);
379 }
380
381 static struct connman_driver connection_driver = {
382         .name           = "connection",
383         .type           = CONNMAN_ELEMENT_TYPE_CONNECTION,
384         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
385         .probe          = connection_probe,
386         .remove         = connection_remove,
387 };
388
389 int __connman_connection_init(void)
390 {
391         DBG("");
392
393         connection = connman_dbus_get_connection();
394
395         if (connman_rtnl_register(&connection_rtnl) < 0)
396                 connman_error("Failed to setup RTNL gateway driver");
397
398         connman_rtnl_send_getroute();
399
400         return connman_driver_register(&connection_driver);
401 }
402
403 void __connman_connection_cleanup(void)
404 {
405         GSList *list;
406
407         DBG("");
408
409         connman_driver_unregister(&connection_driver);
410
411         connman_rtnl_unregister(&connection_rtnl);
412
413         for (list = gateway_list; list; list = list->next) {
414                 struct gateway_data *data = list->data;
415
416                 DBG("index %d gateway %s", data->index, data->gateway);
417
418                 g_free(data->gateway);
419                 g_free(data);
420                 list->data = NULL;
421         }
422
423         g_slist_free(gateway_list);
424         gateway_list = NULL;
425
426         dbus_connection_unref(connection);
427 }