Export interface name via connection
[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
209         DBG("conn %p", conn);
210
211         reply = dbus_message_new_method_return(msg);
212         if (reply == NULL)
213                 return NULL;
214
215         dbus_message_iter_init_append(reply, &array);
216
217         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
218                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
219                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
220                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
221
222         if (element->devname != NULL)
223                 connman_dbus_dict_append_variant(&dict, "Interface",
224                                         DBUS_TYPE_STRING, &element->devname);
225
226         dbus_message_iter_close_container(&array, &dict);
227
228         return reply;
229 }
230
231 static DBusMessage *set_property(DBusConnection *conn,
232                                         DBusMessage *msg, void *data)
233 {
234         DBusMessageIter iter, value;
235         const char *name;
236
237         DBG("conn %p", conn);
238
239         if (dbus_message_iter_init(msg, &iter) == FALSE)
240                 return __connman_error_invalid_arguments(msg);
241
242         dbus_message_iter_get_basic(&iter, &name);
243         dbus_message_iter_next(&iter);
244         dbus_message_iter_recurse(&iter, &value);
245
246         if (__connman_security_check_privileges(msg) < 0)
247                 return __connman_error_permission_denied(msg);
248
249         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
250 }
251
252 static GDBusMethodTable connection_methods[] = {
253         { "GetProperties", "",   "a{sv}", get_properties },
254         { "SetProperty",   "sv", "",      set_property   },
255         { },
256 };
257
258 static GDBusSignalTable connection_signals[] = {
259         { "PropertyChanged", "sv" },
260         { },
261 };
262
263 static DBusConnection *connection;
264
265 static void emit_connections_signal(void)
266 {
267 }
268
269 static int register_interface(struct connman_element *element)
270 {
271         DBG("element %p name %s", element, element->name);
272
273         if (g_dbus_register_interface(connection, element->path,
274                                         CONNMAN_CONNECTION_INTERFACE,
275                                         connection_methods, connection_signals,
276                                         NULL, element, NULL) == FALSE) {
277                 connman_error("Failed to register %s connection", element->path);
278                 return -EIO;
279         }
280
281         emit_connections_signal();
282
283         return 0;
284 }
285
286 static void unregister_interface(struct connman_element *element)
287 {
288         DBG("element %p name %s", element, element->name);
289
290         emit_connections_signal();
291
292         g_dbus_unregister_interface(connection, element->path,
293                                                 CONNMAN_CONNECTION_INTERFACE);
294 }
295
296 static int connection_probe(struct connman_element *element)
297 {
298         const char *gateway = NULL;
299
300         DBG("element %p name %s", element, element->name);
301
302         if (element->parent == NULL)
303                 return -ENODEV;
304
305         if (element->parent->type != CONNMAN_ELEMENT_TYPE_IPV4)
306                 return -ENODEV;
307
308         connman_element_get_value(element,
309                                 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
310
311         DBG("gateway %s", gateway);
312
313         if (register_interface(element) < 0)
314                 return -ENODEV;
315
316         if (gateway == NULL)
317                 return 0;
318
319         if (g_slist_length(gateway_list) > 0) {
320                 DBG("default gateway already present");
321                 return 0;
322         }
323
324         set_route(element, gateway);
325
326         connman_element_set_enabled(element, TRUE);
327
328         return 0;
329 }
330
331 static void connection_remove(struct connman_element *element)
332 {
333         const char *gateway = NULL;
334
335         DBG("element %p name %s", element, element->name);
336
337         unregister_interface(element);
338
339         connman_element_get_value(element,
340                                 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
341
342         DBG("gateway %s", gateway);
343
344         if (gateway == NULL)
345                 return;
346
347         del_route(element, gateway);
348 }
349
350 static struct connman_driver connection_driver = {
351         .name           = "connection",
352         .type           = CONNMAN_ELEMENT_TYPE_CONNECTION,
353         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
354         .probe          = connection_probe,
355         .remove         = connection_remove,
356 };
357
358 int __connman_connection_init(void)
359 {
360         DBG("");
361
362         connection = connman_dbus_get_connection();
363
364         if (connman_rtnl_register(&connection_rtnl) < 0)
365                 connman_error("Failed to setup RTNL gateway driver");
366
367         connman_rtnl_send_getroute();
368
369         return connman_driver_register(&connection_driver);
370 }
371
372 void __connman_connection_cleanup(void)
373 {
374         GSList *list;
375
376         DBG("");
377
378         connman_driver_unregister(&connection_driver);
379
380         connman_rtnl_unregister(&connection_rtnl);
381
382         for (list = gateway_list; list; list = list->next) {
383                 struct gateway_data *data = list->data;
384
385                 DBG("index %d gateway %s", data->index, data->gateway);
386
387                 g_free(data->gateway);
388                 g_free(data);
389                 list->data = NULL;
390         }
391
392         g_slist_free(gateway_list);
393         gateway_list = NULL;
394
395         dbus_connection_unref(connection);
396 }