5 * Copyright (C) 2007 Intel Corporation. All rights reserved.
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.
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.
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
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
35 #include <net/route.h>
37 #include <linux/netlink.h>
38 #include <linux/rtnetlink.h>
43 #include <hal/libhal.h>
47 static GSList *drivers = NULL;
49 int connman_iface_register(struct connman_iface_driver *driver)
51 DBG("driver %p", driver);
53 drivers = g_slist_append(drivers, driver);
58 void connman_iface_unregister(struct connman_iface_driver *driver)
60 DBG("driver %p", driver);
62 drivers = g_slist_remove(drivers, driver);
65 static GSList *interfaces = NULL;
67 struct connman_iface *__connman_iface_find(int index)
71 for (list = interfaces; list; list = list->next) {
72 struct connman_iface *iface = list->data;
74 if (iface->index == index)
81 void __connman_iface_list(DBusMessageIter *iter)
87 for (list = interfaces; list; list = list->next) {
88 struct connman_iface *iface = list->data;
90 dbus_message_iter_append_basic(iter,
91 DBUS_TYPE_OBJECT_PATH, &iface->path);
95 int connman_iface_update(struct connman_iface *iface,
96 enum connman_iface_state state)
99 case CONNMAN_IFACE_STATE_ACTIVE:
100 if (iface->type == CONNMAN_IFACE_TYPE_80211) {
101 if (iface->driver->connect)
102 iface->driver->connect(iface, NULL);
106 case CONNMAN_IFACE_STATE_CONNECTED:
107 __connman_dhcp_request(iface);
114 iface->state = state;
119 void connman_iface_indicate_carrier(struct connman_iface *iface, int carrier)
121 DBG("iface %p carrier %d", iface, carrier);
124 int connman_iface_get_ipv4(struct connman_iface *iface,
125 struct connman_ipv4 *ipv4)
132 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
135 DBG("iface %p ipv4 %p", iface, ipv4);
137 memset(&req, 0, sizeof(req));
138 req.hdr.nlmsg_len = sizeof(req.hdr) + sizeof(req.msg);
139 req.hdr.nlmsg_type = RTM_GETADDR;
140 req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
141 req.hdr.nlmsg_pid = 0;
142 req.hdr.nlmsg_seq = 4711;
143 req.msg.rtgen_family = AF_INET;
145 __connman_rtnl_send(&req, sizeof(req));
150 int connman_iface_set_ipv4(struct connman_iface *iface,
151 struct connman_ipv4 *ipv4)
155 struct sockaddr_in *addr;
158 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
161 DBG("iface %p ipv4 %p", iface, ipv4);
163 sk = socket(PF_INET, SOCK_DGRAM, 0);
167 memset(&ifr, 0, sizeof(ifr));
168 ifr.ifr_ifindex = iface->index;
170 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
175 DBG("ifname %s", ifr.ifr_name);
177 addr = (struct sockaddr_in *) &ifr.ifr_addr;
178 addr->sin_family = AF_INET;
179 addr->sin_addr = ipv4->address;
181 err = ioctl(sk, SIOCSIFADDR, &ifr);
184 DBG("address setting failed (%s)", strerror(errno));
186 addr = (struct sockaddr_in *) &ifr.ifr_netmask;
187 addr->sin_family = AF_INET;
188 addr->sin_addr = ipv4->netmask;
190 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
193 DBG("netmask setting failed (%s)", strerror(errno));
195 addr = (struct sockaddr_in *) &ifr.ifr_broadaddr;
196 addr->sin_family = AF_INET;
197 addr->sin_addr = ipv4->broadcast;
199 err = ioctl(sk, SIOCSIFBRDADDR, &ifr);
202 DBG("broadcast setting failed (%s)", strerror(errno));
204 memset(&rt, 0, sizeof(rt));
205 rt.rt_flags = RTF_UP | RTF_GATEWAY;
207 addr = (struct sockaddr_in *) &rt.rt_dst;
208 addr->sin_family = AF_INET;
209 addr->sin_addr.s_addr = INADDR_ANY;
211 addr = (struct sockaddr_in *) &rt.rt_gateway;
212 addr->sin_family = AF_INET;
213 addr->sin_addr = ipv4->gateway;
215 addr = (struct sockaddr_in *) &rt.rt_genmask;
216 addr->sin_family = AF_INET;
217 addr->sin_addr.s_addr = INADDR_ANY;
219 err = ioctl(sk, SIOCADDRT, &rt);
224 DBG("default route failed (%s)", strerror(errno));
231 int connman_iface_clear_ipv4(struct connman_iface *iface)
234 struct sockaddr_in *addr;
237 if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
240 DBG("iface %p", iface);
242 sk = socket(PF_INET, SOCK_DGRAM, 0);
246 memset(&ifr, 0, sizeof(ifr));
247 ifr.ifr_ifindex = iface->index;
249 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
254 DBG("ifname %s", ifr.ifr_name);
256 addr = (struct sockaddr_in *) &ifr.ifr_addr;
257 addr->sin_family = AF_INET;
258 addr->sin_addr.s_addr = INADDR_ANY;
260 //err = ioctl(sk, SIOCDIFADDR, &ifr);
261 err = ioctl(sk, SIOCSIFADDR, &ifr);
265 if (err < 0 && errno != EADDRNOTAVAIL) {
266 DBG("address removal failed (%s)", strerror(errno));
273 static DBusMessage *enable_iface(DBusConnection *conn,
274 DBusMessage *msg, void *data)
276 struct connman_iface *iface = data;
277 struct connman_iface_driver *driver = iface->driver;
280 DBG("conn %p", conn);
282 reply = dbus_message_new_method_return(msg);
286 if (driver->activate)
287 driver->activate(iface);
289 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
294 static DBusMessage *scan_iface(DBusConnection *conn,
295 DBusMessage *msg, void *data)
297 struct connman_iface *iface = data;
298 struct connman_iface_driver *driver = iface->driver;
301 DBG("conn %p", conn);
303 reply = dbus_message_new_method_return(msg);
310 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
315 static GDBusMethodTable iface_methods[] = {
316 { "Enable", "", "", enable_iface },
317 { "Scan", "", "", scan_iface },
321 static dbus_bool_t get_type(DBusConnection *conn,
322 DBusMessageIter *iter, void *data)
324 struct connman_iface *iface = data;
327 DBG("iface %p", iface);
329 switch (iface->type) {
330 case CONNMAN_IFACE_TYPE_80203:
333 case CONNMAN_IFACE_TYPE_80211:
336 case CONNMAN_IFACE_TYPE_WIMAX:
339 case CONNMAN_IFACE_TYPE_BLUETOOTH:
347 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &type);
352 static GDBusPropertyTable iface_properties[] = {
353 { "Type", "s", get_type },
357 static void device_free(void *data)
359 struct connman_iface *iface = data;
361 DBG("iface %p", iface);
363 connman_iface_clear_ipv4(iface);
365 if (iface->driver && iface->driver->remove)
366 iface->driver->remove(iface);
370 g_free(iface->sysfs);
374 static int probe_device(LibHalContext *ctx,
375 struct connman_iface_driver *driver, const char *udi)
377 DBusConnection *conn;
378 struct connman_iface *iface;
382 DBG("ctx %p driver %p udi %s", ctx, driver, udi);
387 iface = g_try_new0(struct connman_iface, 1);
391 temp = g_path_get_basename(udi);
392 iface->path = g_strdup_printf("%s/%s", CONNMAN_IFACE_BASEPATH, temp);
395 iface->udi = g_strdup(udi);
397 DBG("path %s", iface->path);
399 sysfs = libhal_device_get_property_string(ctx, udi,
400 "linux.sysfs_path", NULL);
402 iface->sysfs = g_strdup(sysfs);
406 if (g_str_has_prefix(driver->capability, "net") == TRUE)
407 iface->index = libhal_device_get_property_int(ctx, udi,
408 "net.linux.ifindex", NULL);
410 iface->type = CONNMAN_IFACE_TYPE_UNKNOWN;
412 iface->state = CONNMAN_IFACE_STATE_UNKNOWN;
414 DBG("iface %p", iface);
416 err = driver->probe(iface);
422 iface->driver = driver;
424 conn = libhal_ctx_get_dbus_connection(ctx);
426 g_dbus_register_object(conn, iface->path, iface, device_free);
428 interfaces = g_slist_append(interfaces, iface);
430 if (iface->flags & CONNMAN_IFACE_FLAG_IPV4) {
431 if (driver->get_ipv4)
432 driver->get_ipv4(iface, &iface->ipv4);
434 connman_iface_get_ipv4(iface, &iface->ipv4);
436 DBG("address %s", inet_ntoa(iface->ipv4.address));
439 g_dbus_register_interface(conn, iface->path,
440 CONNMAN_IFACE_INTERFACE,
441 iface_methods, NULL, iface_properties);
443 g_dbus_emit_signal(conn, CONNMAN_MANAGER_PATH,
444 CONNMAN_MANAGER_INTERFACE,
446 DBUS_TYPE_OBJECT_PATH, &iface->path,
452 static void device_added(LibHalContext *ctx, const char *udi)
456 DBG("ctx %p udi %s", ctx, udi);
458 for (list = drivers; list; list = list->next) {
459 struct connman_iface_driver *driver = list->data;
461 if (driver->capability == NULL)
464 if (libhal_device_query_capability(ctx, udi,
465 driver->capability, NULL) == TRUE) {
466 if (probe_device(ctx, driver, udi) == 0)
472 static void device_removed(LibHalContext *ctx, const char *udi)
474 DBusConnection *conn;
477 DBG("ctx %p udi %s", ctx, udi);
479 conn = libhal_ctx_get_dbus_connection(ctx);
481 for (list = interfaces; list; list = list->next) {
482 struct connman_iface *iface = list->data;
484 if (strcmp(udi, iface->udi) == 0) {
485 g_dbus_emit_signal(conn, CONNMAN_MANAGER_PATH,
486 CONNMAN_MANAGER_INTERFACE,
488 DBUS_TYPE_OBJECT_PATH, &iface->path,
490 interfaces = g_slist_remove(interfaces, iface);
491 g_dbus_unregister_interface(conn, iface->path,
492 CONNMAN_IFACE_INTERFACE);
493 g_dbus_unregister_object(conn, iface->path);
499 static void probe_driver(LibHalContext *ctx,
500 struct connman_iface_driver *driver)
505 DBG("ctx %p driver %p", ctx, driver);
507 list = libhal_find_device_by_capability(ctx,
508 driver->capability, &num, NULL);
513 probe_device(ctx, driver, *tmp);
517 libhal_free_string_array(list);
521 static void find_devices(LibHalContext *ctx)
527 for (list = drivers; list; list = list->next) {
528 struct connman_iface_driver *driver = list->data;
530 DBG("driver %p", driver);
532 if (driver->capability == NULL)
535 probe_driver(ctx, driver);
539 static LibHalContext *hal_ctx = NULL;
541 static void hal_init(void *data)
543 DBusConnection *conn = data;
545 DBG("conn %p", conn);
550 hal_ctx = libhal_ctx_new();
554 if (libhal_ctx_set_dbus_connection(hal_ctx, conn) == FALSE) {
555 libhal_ctx_free(hal_ctx);
559 if (libhal_ctx_init(hal_ctx, NULL) == FALSE) {
560 libhal_ctx_free(hal_ctx);
564 libhal_ctx_set_device_added(hal_ctx, device_added);
565 libhal_ctx_set_device_removed(hal_ctx, device_removed);
567 //libhal_ctx_set_device_new_capability(hal_ctx, new_capability);
568 //libhal_ctx_set_device_lost_capability(hal_ctx, lost_capability);
570 find_devices(hal_ctx);
573 static void hal_cleanup(void *data)
575 DBusConnection *conn = data;
578 DBG("conn %p", conn);
583 for (list = interfaces; list; list = list->next) {
584 struct connman_iface *iface = list->data;
586 DBG("path %s", iface->path);
588 g_dbus_emit_signal(conn, CONNMAN_MANAGER_PATH,
589 CONNMAN_MANAGER_INTERFACE,
591 DBUS_TYPE_OBJECT_PATH, &iface->path,
594 g_dbus_unregister_interface(conn, iface->path,
595 CONNMAN_IFACE_INTERFACE);
597 g_dbus_unregister_object(conn, iface->path);
600 g_slist_free(interfaces);
604 libhal_ctx_shutdown(hal_ctx, NULL);
606 libhal_ctx_free(hal_ctx);
611 static DBusConnection *connection = NULL;
612 static guint hal_watch = 0;
614 int __connman_iface_init(DBusConnection *conn)
616 DBG("conn %p", conn);
618 connection = dbus_connection_ref(conn);
619 if (connection == NULL)
622 hal_init(connection);
624 hal_watch = g_dbus_add_watch(connection, "org.freedesktop.Hal",
625 hal_init, hal_cleanup, connection, NULL);
630 void __connman_iface_cleanup(void)
632 DBG("conn %p", connection);
634 g_dbus_remove_watch(connection, hal_watch);
636 hal_cleanup(connection);
638 dbus_connection_unref(connection);