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/log.h>
38 struct ethernet_data {
43 static GStaticMutex ethernet_mutex = G_STATIC_MUTEX_INIT;
44 static GSList *ethernet_list = NULL;
46 static void ethernet_link_flags(int index, short flags)
50 g_static_mutex_lock(ðernet_mutex);
52 for (list = ethernet_list; list; list = list->next) {
53 struct connman_element *element = list->data;
54 struct connman_element *netdev;
55 struct ethernet_data *ethernet;
57 ethernet = connman_element_get_data(element);
61 if (ethernet->index != index)
64 if ((ethernet->flags & IFF_RUNNING) == (flags & IFF_RUNNING))
67 ethernet->flags = flags;
69 if (ethernet->flags & IFF_RUNNING) {
72 netdev = connman_element_create(NULL);
74 netdev->type = CONNMAN_ELEMENT_TYPE_DEVICE;
75 netdev->subtype = CONNMAN_ELEMENT_SUBTYPE_NETWORK;
76 netdev->index = element->index;
78 connman_element_register(netdev, element);
83 connman_element_unregister_children(element);
87 g_static_mutex_unlock(ðernet_mutex);
90 static struct connman_rtnl ethernet_rtnl = {
92 .link_flags = ethernet_link_flags,
95 static int iface_up(struct ethernet_data *ethernet)
100 DBG("index %d flags %d", ethernet->index, ethernet->flags);
102 sk = socket(PF_INET, SOCK_DGRAM, 0);
106 memset(&ifr, 0, sizeof(ifr));
107 ifr.ifr_ifindex = ethernet->index;
109 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
114 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
119 if (ifr.ifr_flags & IFF_UP) {
124 ifr.ifr_flags |= IFF_UP;
126 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
139 static int iface_down(struct ethernet_data *ethernet)
144 DBG("index %d flags %d", ethernet->index, ethernet->flags);
146 sk = socket(PF_INET, SOCK_DGRAM, 0);
150 memset(&ifr, 0, sizeof(ifr));
151 ifr.ifr_ifindex = ethernet->index;
153 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
158 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
163 if (!(ifr.ifr_flags & IFF_UP)) {
168 ifr.ifr_flags &= ~IFF_UP;
170 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
181 static int ethernet_probe(struct connman_element *element)
183 struct ethernet_data *ethernet;
185 DBG("element %p name %s", element, element->name);
187 ethernet = g_try_new0(struct ethernet_data, 1);
188 if (ethernet == NULL)
191 g_static_mutex_lock(ðernet_mutex);
192 ethernet_list = g_slist_append(ethernet_list, element);
193 g_static_mutex_unlock(ðernet_mutex);
195 connman_element_set_data(element, ethernet);
197 ethernet->index = element->index;
201 connman_rtnl_send_getlink();
206 static void ethernet_remove(struct connman_element *element)
208 struct ethernet_data *ethernet = connman_element_get_data(element);
210 DBG("element %p name %s", element, element->name);
212 connman_element_set_data(element, NULL);
214 iface_down(ethernet);
216 g_static_mutex_lock(ðernet_mutex);
217 ethernet_list = g_slist_remove(ethernet_list, element);
218 g_static_mutex_unlock(ðernet_mutex);
223 static struct connman_driver ethernet_driver = {
225 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
226 .subtype = CONNMAN_ELEMENT_SUBTYPE_ETHERNET,
227 .probe = ethernet_probe,
228 .remove = ethernet_remove,
231 static int ethernet_init(void)
235 err = connman_rtnl_register(ðernet_rtnl);
239 err = connman_driver_register(ðernet_driver);
241 connman_rtnl_unregister(ðernet_rtnl):
248 static void ethernet_exit(void)
250 connman_driver_unregister(ðernet_driver);
252 connman_rtnl_unregister(ðernet_rtnl);
255 CONNMAN_PLUGIN_DEFINE("ethernet", "Ethernet interface plugin", VERSION,
256 ethernet_init, ethernet_exit)