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/device.h>
36 #include <connman/rtnl.h>
37 #include <connman/log.h>
39 struct ethernet_data {
44 static GSList *ethernet_list = NULL;
46 static void update_power(struct connman_device *device, unsigned flags)
51 connman_device_set_powered(device, TRUE);
55 connman_device_set_powered(device, FALSE);
59 static void update_carrier(struct connman_device *device, unsigned flags)
61 struct connman_element *netdev;
63 if (flags & IFF_LOWER_UP) {
66 netdev = connman_element_create(NULL);
68 netdev->type = CONNMAN_ELEMENT_TYPE_DEVICE;
69 netdev->subtype = CONNMAN_ELEMENT_SUBTYPE_NETWORK;
70 netdev->index = device->element->index;
72 if (connman_element_register(netdev,
74 connman_element_unref(netdev);
79 connman_element_unregister_children(device->element);
83 static void ethernet_newlink(unsigned short type, int index,
84 unsigned flags, unsigned change)
88 DBG("index %d flags %ld change %ld", index, flags, change);
90 for (list = ethernet_list; list; list = list->next) {
91 struct connman_device *device = list->data;
92 struct ethernet_data *ethernet;
94 ethernet = connman_device_get_data(device);
98 if (ethernet->index != index)
101 if ((ethernet->flags & IFF_UP) != (flags & IFF_UP))
102 update_power(device, flags);
104 if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP))
105 update_carrier(device, flags);
107 ethernet->flags = flags;
111 static struct connman_rtnl ethernet_rtnl = {
113 .newlink = ethernet_newlink,
116 static int iface_up(struct ethernet_data *ethernet)
121 DBG("index %d flags %d", ethernet->index, ethernet->flags);
123 sk = socket(PF_INET, SOCK_DGRAM, 0);
127 memset(&ifr, 0, sizeof(ifr));
128 ifr.ifr_ifindex = ethernet->index;
130 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
135 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
140 if (ifr.ifr_flags & IFF_UP) {
145 ifr.ifr_flags |= IFF_UP;
147 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
160 static int iface_down(struct ethernet_data *ethernet)
165 DBG("index %d flags %d", ethernet->index, ethernet->flags);
167 sk = socket(PF_INET, SOCK_DGRAM, 0);
171 memset(&ifr, 0, sizeof(ifr));
172 ifr.ifr_ifindex = ethernet->index;
174 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
179 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
184 if (!(ifr.ifr_flags & IFF_UP)) {
189 ifr.ifr_flags &= ~IFF_UP;
191 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
202 static int ethernet_probe(struct connman_device *device)
204 struct ethernet_data *ethernet;
206 DBG("device %p", device);
208 ethernet = g_try_new0(struct ethernet_data, 1);
209 if (ethernet == NULL)
212 ethernet_list = g_slist_append(ethernet_list, device);
214 connman_device_set_data(device, ethernet);
216 ethernet->index = device->element->index;
218 connman_rtnl_send_getlink();
223 static void ethernet_remove(struct connman_device *device)
225 struct ethernet_data *ethernet = connman_device_get_data(device);
227 DBG("device %p", device);
229 connman_device_set_data(device, NULL);
231 ethernet_list = g_slist_remove(ethernet_list, device);
236 static int ethernet_enable(struct connman_device *device)
238 struct ethernet_data *ethernet = connman_device_get_data(device);
240 DBG("device %p", device);
242 return iface_up(ethernet);
245 static int ethernet_disable(struct connman_device *device)
247 struct ethernet_data *ethernet = connman_device_get_data(device);
249 DBG("device %p", device);
251 return iface_down(ethernet);
254 static struct connman_device_driver ethernet_driver = {
256 .type = CONNMAN_DEVICE_TYPE_ETHERNET,
257 .probe = ethernet_probe,
258 .remove = ethernet_remove,
259 .enable = ethernet_enable,
260 .disable = ethernet_disable,
263 static int ethernet_init(void)
267 err = connman_rtnl_register(ðernet_rtnl);
271 err = connman_device_driver_register(ðernet_driver);
273 connman_rtnl_unregister(ðernet_rtnl);
280 static void ethernet_exit(void)
282 connman_device_driver_unregister(ðernet_driver);
284 connman_rtnl_unregister(ðernet_rtnl);
287 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
288 ethernet_init, ethernet_exit)