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 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_device *device = list->data;
55 struct ethernet_data *ethernet;
57 ethernet = connman_device_get_data(device);
61 if (ethernet->index != index)
64 if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
67 connman_device_set_powered(device, TRUE);
70 connman_device_set_powered(device, FALSE);
74 if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
75 if (flags & IFF_LOWER_UP) {
77 connman_device_set_carrier(device, TRUE);
80 connman_device_set_carrier(device, FALSE);
84 ethernet->flags = flags;
88 static struct connman_rtnl ethernet_rtnl = {
90 .newlink = ethernet_newlink,
93 static int iface_up(struct ethernet_data *ethernet)
98 DBG("index %d flags %d", ethernet->index, ethernet->flags);
100 sk = socket(PF_INET, SOCK_DGRAM, 0);
104 memset(&ifr, 0, sizeof(ifr));
105 ifr.ifr_ifindex = ethernet->index;
107 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
112 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
117 if (ifr.ifr_flags & IFF_UP) {
122 ifr.ifr_flags |= IFF_UP;
124 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
137 static int iface_down(struct ethernet_data *ethernet)
142 DBG("index %d flags %d", ethernet->index, ethernet->flags);
144 sk = socket(PF_INET, SOCK_DGRAM, 0);
148 memset(&ifr, 0, sizeof(ifr));
149 ifr.ifr_ifindex = ethernet->index;
151 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
156 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
161 if (!(ifr.ifr_flags & IFF_UP)) {
166 ifr.ifr_flags &= ~IFF_UP;
168 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
179 static int ethernet_probe(struct connman_device *device)
181 struct ethernet_data *ethernet;
183 DBG("device %p", device);
185 ethernet = g_try_new0(struct ethernet_data, 1);
186 if (ethernet == NULL)
189 ethernet_list = g_slist_append(ethernet_list, device);
191 connman_device_set_data(device, ethernet);
193 ethernet->index = connman_device_get_index(device);
195 connman_rtnl_send_getlink();
200 static void ethernet_remove(struct connman_device *device)
202 struct ethernet_data *ethernet = connman_device_get_data(device);
204 DBG("device %p", device);
206 connman_device_set_data(device, NULL);
208 ethernet_list = g_slist_remove(ethernet_list, device);
213 static int ethernet_enable(struct connman_device *device)
215 struct ethernet_data *ethernet = connman_device_get_data(device);
217 DBG("device %p", device);
219 return iface_up(ethernet);
222 static int ethernet_disable(struct connman_device *device)
224 struct ethernet_data *ethernet = connman_device_get_data(device);
226 DBG("device %p", device);
228 return iface_down(ethernet);
231 static struct connman_device_driver ethernet_driver = {
233 .type = CONNMAN_DEVICE_TYPE_ETHERNET,
234 .probe = ethernet_probe,
235 .remove = ethernet_remove,
236 .enable = ethernet_enable,
237 .disable = ethernet_disable,
240 static int ethernet_init(void)
244 err = connman_rtnl_register(ðernet_rtnl);
248 err = connman_device_driver_register(ðernet_driver);
250 connman_rtnl_unregister(ðernet_rtnl);
257 static void ethernet_exit(void)
259 connman_device_driver_unregister(ðernet_driver);
261 connman_rtnl_unregister(ðernet_rtnl);
264 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
265 ethernet_init, ethernet_exit)