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 static GStaticMutex ethernet_mutex = G_STATIC_MUTEX_INIT;
39 static GSList *ethernet_list = NULL;
41 static GStaticMutex element_mutex = G_STATIC_MUTEX_INIT;
42 static GSList *element_list = NULL;
44 static void create_element(struct connman_element *parent,
45 enum connman_element_type type)
47 struct connman_element *element;
49 DBG("parent %p name %s", parent, parent->name);
51 element = connman_element_create();
54 element->netdev.index = parent->netdev.index;
55 element->netdev.name = g_strdup(parent->netdev.name);
57 g_static_mutex_lock(&element_mutex);
58 element_list = g_slist_append(element_list, element);
59 g_static_mutex_unlock(&element_mutex);
61 connman_element_register(element, parent);
64 static void remove_elements(struct connman_element *parent)
66 GSList *list = element_list;
68 DBG("parent %p name %s", parent, parent->name);
70 g_static_mutex_lock(&element_mutex);
73 GSList *next = list->next;
74 struct connman_element *element = list->data;
76 if (element->netdev.index != parent->netdev.index) {
81 element_list = g_slist_delete_link(element_list, list);
83 connman_element_unregister(element);
85 connman_element_unref(element);
90 g_static_mutex_unlock(&element_mutex);
93 static void rtnl_link(struct nlmsghdr *hdr, const char *type)
96 struct ifinfomsg *msg;
99 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
100 bytes = IFLA_PAYLOAD(hdr);
102 DBG("%s ifi_index %d ifi_flags 0x%04x",
103 type, msg->ifi_index, msg->ifi_flags);
105 g_static_mutex_lock(ðernet_mutex);
107 for (list = ethernet_list; list; list = list->next) {
108 struct connman_element *element = list->data;
110 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
113 if (element->netdev.index != msg->ifi_index)
116 if ((element->netdev.flags & IFF_RUNNING) ==
117 (msg->ifi_flags & IFF_RUNNING))
120 element->netdev.flags = msg->ifi_flags;
122 if (msg->ifi_flags & IFF_RUNNING) {
125 create_element(element, CONNMAN_ELEMENT_TYPE_DHCP);
126 create_element(element, CONNMAN_ELEMENT_TYPE_ZEROCONF);
130 remove_elements(element);
134 g_static_mutex_unlock(ðernet_mutex);
137 static gboolean rtnl_event(GIOChannel *chan, GIOCondition cond, gpointer data)
139 unsigned char buf[1024];
144 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
147 memset(buf, 0, sizeof(buf));
149 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
151 if (err == G_IO_ERROR_AGAIN)
156 DBG("buf %p len %zd", buf, len);
159 struct nlmsghdr *hdr = ptr;
160 struct nlmsgerr *err;
162 if (!NLMSG_OK(hdr, len))
165 DBG("len %d type %d flags 0x%04x seq %d",
166 hdr->nlmsg_len, hdr->nlmsg_type,
167 hdr->nlmsg_flags, hdr->nlmsg_seq);
169 switch (hdr->nlmsg_type) {
171 err = NLMSG_DATA(hdr);
172 DBG("ERROR %d (%s)", -err->error,
173 strerror(-err->error));
177 rtnl_link(hdr, "NEWLINK");
181 rtnl_link(hdr, "DELLINK");
185 len -= hdr->nlmsg_len;
186 ptr += hdr->nlmsg_len;
192 static GIOChannel *channel = NULL;
194 static int rtnl_request(void)
201 struct sockaddr_nl addr;
206 memset(&req, 0, sizeof(req));
207 req.hdr.nlmsg_len = sizeof(req.hdr) + sizeof(req.msg);
208 req.hdr.nlmsg_type = RTM_GETLINK;
209 req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
210 req.hdr.nlmsg_pid = 0;
211 req.hdr.nlmsg_seq = 42;
212 req.msg.rtgen_family = AF_INET;
214 sk = g_io_channel_unix_get_fd(channel);
216 memset(&addr, 0, sizeof(addr));
217 addr.nl_family = AF_NETLINK;
219 return sendto(sk, &req, sizeof(req), 0,
220 (struct sockaddr *) &addr, sizeof(addr));
223 static int iface_up(struct connman_element *element)
228 DBG("element %p", element);
230 sk = socket(PF_INET, SOCK_DGRAM, 0);
234 memset(&ifr, 0, sizeof(ifr));
235 ifr.ifr_ifindex = element->netdev.index;
237 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
242 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
247 if (ifr.ifr_flags & IFF_UP) {
252 ifr.ifr_flags |= IFF_UP;
254 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
267 static int iface_down(struct connman_element *element)
272 DBG("element %p", element);
274 sk = socket(PF_INET, SOCK_DGRAM, 0);
278 memset(&ifr, 0, sizeof(ifr));
279 ifr.ifr_ifindex = element->netdev.index;
281 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
286 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
291 if (!(ifr.ifr_flags & IFF_UP)) {
296 ifr.ifr_flags &= ~IFF_UP;
298 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
309 static int ethernet_probe(struct connman_element *element)
311 DBG("element %p name %s", element, element->name);
313 g_static_mutex_lock(ðernet_mutex);
314 ethernet_list = g_slist_append(ethernet_list, element);
315 g_static_mutex_unlock(ðernet_mutex);
324 static void ethernet_remove(struct connman_element *element)
326 DBG("element %p name %s", element, element->name);
330 remove_elements(element);
332 g_static_mutex_lock(ðernet_mutex);
333 ethernet_list = g_slist_remove(ethernet_list, element);
334 g_static_mutex_unlock(ðernet_mutex);
337 static struct connman_driver ethernet_driver = {
339 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
340 .subtype = CONNMAN_ELEMENT_SUBTYPE_ETHERNET,
341 .probe = ethernet_probe,
342 .remove = ethernet_remove,
345 static int rtnl_init(void)
347 struct sockaddr_nl addr;
352 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
356 memset(&addr, 0, sizeof(addr));
357 addr.nl_family = AF_NETLINK;
358 addr.nl_groups = RTMGRP_LINK;
360 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
366 channel = g_io_channel_unix_new(sk);
367 g_io_channel_set_close_on_unref(channel, TRUE);
369 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
375 static void rtnl_cleanup(void)
379 g_io_channel_shutdown(channel, TRUE, NULL);
380 g_io_channel_unref(channel);
385 static int ethernet_init(void)
393 err = connman_driver_register(ðernet_driver);
402 static void ethernet_exit(void)
404 connman_driver_unregister(ðernet_driver);
409 CONNMAN_PLUGIN_DEFINE("ethernet", "Ethernet interface plugin", VERSION,
410 ethernet_init, ethernet_exit)