5 * Copyright (C) 2007-2008 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
28 #include <sys/ioctl.h>
29 #include <sys/socket.h>
31 #include <linux/netlink.h>
32 #include <linux/rtnetlink.h>
34 #include <connman/plugin.h>
35 #include <connman/driver.h>
36 #include <connman/rtnl.h>
37 #include <connman/log.h>
39 struct ethernet_data {
44 static GStaticMutex ethernet_mutex = G_STATIC_MUTEX_INIT;
45 static GSList *ethernet_list = NULL;
47 static void ethernet_newlink(unsigned short type, int index,
48 unsigned flags, unsigned change)
52 g_static_mutex_lock(ðernet_mutex);
54 for (list = ethernet_list; list; list = list->next) {
55 struct connman_element *element = list->data;
56 struct connman_element *netdev;
57 struct ethernet_data *ethernet;
59 ethernet = connman_element_get_data(element);
63 if (ethernet->index != index)
66 if ((ethernet->flags & IFF_RUNNING) == (flags & IFF_RUNNING))
69 ethernet->flags = flags;
71 if (ethernet->flags & IFF_RUNNING) {
74 netdev = connman_element_create(NULL);
76 netdev->type = CONNMAN_ELEMENT_TYPE_DEVICE;
77 netdev->subtype = CONNMAN_ELEMENT_SUBTYPE_NETWORK;
78 netdev->index = element->index;
80 connman_element_register(netdev, element);
85 connman_element_unregister_children(element);
89 g_static_mutex_unlock(ðernet_mutex);
92 static struct connman_rtnl ethernet_rtnl = {
94 .newlink = ethernet_newlink,
97 static int iface_up(struct ethernet_data *ethernet)
102 DBG("index %d flags %d", ethernet->index, ethernet->flags);
104 sk = socket(PF_INET, SOCK_DGRAM, 0);
108 memset(&ifr, 0, sizeof(ifr));
109 ifr.ifr_ifindex = ethernet->index;
111 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
116 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
121 if (ifr.ifr_flags & IFF_UP) {
126 ifr.ifr_flags |= IFF_UP;
128 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
141 static int iface_down(struct ethernet_data *ethernet)
146 DBG("index %d flags %d", ethernet->index, ethernet->flags);
148 sk = socket(PF_INET, SOCK_DGRAM, 0);
152 memset(&ifr, 0, sizeof(ifr));
153 ifr.ifr_ifindex = ethernet->index;
155 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
160 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
165 if (!(ifr.ifr_flags & IFF_UP)) {
170 ifr.ifr_flags &= ~IFF_UP;
172 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
183 static int ethernet_probe(struct connman_element *element)
185 struct ethernet_data *ethernet;
187 DBG("element %p name %s", element, element->name);
189 ethernet = g_try_new0(struct ethernet_data, 1);
190 if (ethernet == NULL)
193 g_static_mutex_lock(ðernet_mutex);
194 ethernet_list = g_slist_append(ethernet_list, element);
195 g_static_mutex_unlock(ðernet_mutex);
197 connman_element_set_data(element, ethernet);
199 ethernet->index = element->index;
203 connman_rtnl_send_getlink();
208 static void ethernet_remove(struct connman_element *element)
210 struct ethernet_data *ethernet = connman_element_get_data(element);
212 DBG("element %p name %s", element, element->name);
214 connman_element_set_data(element, NULL);
216 iface_down(ethernet);
218 g_static_mutex_lock(ðernet_mutex);
219 ethernet_list = g_slist_remove(ethernet_list, element);
220 g_static_mutex_unlock(ðernet_mutex);
225 static struct connman_driver ethernet_driver = {
227 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
228 .subtype = CONNMAN_ELEMENT_SUBTYPE_ETHERNET,
229 .probe = ethernet_probe,
230 .remove = ethernet_remove,
233 static int ethernet_init(void)
237 err = connman_rtnl_register(ðernet_rtnl);
241 err = connman_driver_register(ðernet_driver);
243 connman_rtnl_unregister(ðernet_rtnl);
250 static void ethernet_exit(void)
252 connman_driver_unregister(ðernet_driver);
254 connman_rtnl_unregister(ðernet_rtnl);
257 CONNMAN_PLUGIN_DEFINE("ethernet", "Ethernet interface plugin", VERSION,
258 ethernet_init, ethernet_exit)