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 GSList *ethernet_list = NULL;
46 static void ethernet_newlink(unsigned short type, int index,
47 unsigned flags, unsigned change)
51 DBG("index %d flags %ld change %ld", index, flags, change);
53 for (list = ethernet_list; list; list = list->next) {
54 struct connman_element *element = list->data;
55 struct connman_element *netdev;
56 struct ethernet_data *ethernet;
58 ethernet = connman_element_get_data(element);
62 if (ethernet->index != index)
65 if ((ethernet->flags & IFF_RUNNING) == (flags & IFF_RUNNING))
68 ethernet->flags = flags;
70 if (ethernet->flags & IFF_RUNNING) {
73 netdev = connman_element_create(NULL);
75 netdev->type = CONNMAN_ELEMENT_TYPE_DEVICE;
76 netdev->subtype = CONNMAN_ELEMENT_SUBTYPE_NETWORK;
77 netdev->index = element->index;
79 connman_element_register(netdev, element);
84 connman_element_unregister_children(element);
89 static struct connman_rtnl ethernet_rtnl = {
91 .newlink = ethernet_newlink,
94 static int iface_up(struct ethernet_data *ethernet)
99 DBG("index %d flags %d", ethernet->index, ethernet->flags);
101 sk = socket(PF_INET, SOCK_DGRAM, 0);
105 memset(&ifr, 0, sizeof(ifr));
106 ifr.ifr_ifindex = ethernet->index;
108 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
113 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
118 if (ifr.ifr_flags & IFF_UP) {
123 ifr.ifr_flags |= IFF_UP;
125 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
138 static int iface_down(struct ethernet_data *ethernet)
143 DBG("index %d flags %d", ethernet->index, ethernet->flags);
145 sk = socket(PF_INET, SOCK_DGRAM, 0);
149 memset(&ifr, 0, sizeof(ifr));
150 ifr.ifr_ifindex = ethernet->index;
152 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
157 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
162 if (!(ifr.ifr_flags & IFF_UP)) {
167 ifr.ifr_flags &= ~IFF_UP;
169 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
180 static int ethernet_probe(struct connman_element *element)
182 struct ethernet_data *ethernet;
184 DBG("element %p name %s", element, element->name);
186 ethernet = g_try_new0(struct ethernet_data, 1);
187 if (ethernet == NULL)
190 ethernet_list = g_slist_append(ethernet_list, element);
192 connman_element_set_data(element, ethernet);
194 ethernet->index = element->index;
198 connman_rtnl_send_getlink();
203 static void ethernet_remove(struct connman_element *element)
205 struct ethernet_data *ethernet = connman_element_get_data(element);
207 DBG("element %p name %s", element, element->name);
209 connman_element_set_data(element, NULL);
211 iface_down(ethernet);
213 ethernet_list = g_slist_remove(ethernet_list, element);
218 static struct connman_driver ethernet_driver = {
220 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
221 .subtype = CONNMAN_ELEMENT_SUBTYPE_ETHERNET,
222 .probe = ethernet_probe,
223 .remove = ethernet_remove,
226 static int ethernet_init(void)
230 err = connman_rtnl_register(ðernet_rtnl);
234 err = connman_driver_register(ðernet_driver);
236 connman_rtnl_unregister(ðernet_rtnl);
243 static void ethernet_exit(void)
245 connman_driver_unregister(ðernet_driver);
247 connman_rtnl_unregister(ðernet_rtnl);
250 CONNMAN_PLUGIN_DEFINE("ethernet", "Ethernet interface plugin", VERSION,
251 ethernet_init, ethernet_exit)