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 void create_element(struct connman_element *parent,
42 enum connman_element_type type)
44 struct connman_element *element;
46 DBG("parent %p name %s", parent, parent->name);
48 element = connman_element_create();
51 element->netdev.index = parent->netdev.index;
52 element->netdev.name = g_strdup(parent->netdev.name);
54 connman_element_register(element, parent);
57 static void rtnl_link(struct nlmsghdr *hdr, const char *type)
60 struct ifinfomsg *msg;
63 msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
64 bytes = IFLA_PAYLOAD(hdr);
66 DBG("%s ifi_index %d ifi_flags 0x%04x",
67 type, msg->ifi_index, msg->ifi_flags);
69 g_static_mutex_lock(ðernet_mutex);
71 for (list = ethernet_list; list; list = list->next) {
72 struct connman_element *element = list->data;
74 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
77 if (element->netdev.index != msg->ifi_index)
80 if ((element->netdev.flags & IFF_RUNNING) ==
81 (msg->ifi_flags & IFF_RUNNING))
84 element->netdev.flags = msg->ifi_flags;
86 if (msg->ifi_flags & IFF_RUNNING) {
89 create_element(element, CONNMAN_ELEMENT_TYPE_DHCP);
90 create_element(element, CONNMAN_ELEMENT_TYPE_ZEROCONF);
94 connman_element_unregister_children(element);
98 g_static_mutex_unlock(ðernet_mutex);
101 static gboolean rtnl_event(GIOChannel *chan, GIOCondition cond, gpointer data)
103 unsigned char buf[1024];
108 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
111 memset(buf, 0, sizeof(buf));
113 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
115 if (err == G_IO_ERROR_AGAIN)
120 DBG("buf %p len %zd", buf, len);
123 struct nlmsghdr *hdr = ptr;
124 struct nlmsgerr *err;
126 if (!NLMSG_OK(hdr, len))
129 DBG("len %d type %d flags 0x%04x seq %d",
130 hdr->nlmsg_len, hdr->nlmsg_type,
131 hdr->nlmsg_flags, hdr->nlmsg_seq);
133 switch (hdr->nlmsg_type) {
135 err = NLMSG_DATA(hdr);
136 DBG("ERROR %d (%s)", -err->error,
137 strerror(-err->error));
141 rtnl_link(hdr, "NEWLINK");
145 rtnl_link(hdr, "DELLINK");
149 len -= hdr->nlmsg_len;
150 ptr += hdr->nlmsg_len;
156 static GIOChannel *channel = NULL;
158 static int rtnl_request(void)
165 struct sockaddr_nl addr;
170 memset(&req, 0, sizeof(req));
171 req.hdr.nlmsg_len = sizeof(req.hdr) + sizeof(req.msg);
172 req.hdr.nlmsg_type = RTM_GETLINK;
173 req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
174 req.hdr.nlmsg_pid = 0;
175 req.hdr.nlmsg_seq = 42;
176 req.msg.rtgen_family = AF_INET;
178 sk = g_io_channel_unix_get_fd(channel);
180 memset(&addr, 0, sizeof(addr));
181 addr.nl_family = AF_NETLINK;
183 return sendto(sk, &req, sizeof(req), 0,
184 (struct sockaddr *) &addr, sizeof(addr));
187 static int iface_up(struct connman_element *element)
192 DBG("element %p", element);
194 sk = socket(PF_INET, SOCK_DGRAM, 0);
198 memset(&ifr, 0, sizeof(ifr));
199 ifr.ifr_ifindex = element->netdev.index;
201 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
206 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
211 if (ifr.ifr_flags & IFF_UP) {
216 ifr.ifr_flags |= IFF_UP;
218 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
231 static int iface_down(struct connman_element *element)
236 DBG("element %p", element);
238 sk = socket(PF_INET, SOCK_DGRAM, 0);
242 memset(&ifr, 0, sizeof(ifr));
243 ifr.ifr_ifindex = element->netdev.index;
245 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
250 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
255 if (!(ifr.ifr_flags & IFF_UP)) {
260 ifr.ifr_flags &= ~IFF_UP;
262 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
273 static int ethernet_probe(struct connman_element *element)
275 DBG("element %p name %s", element, element->name);
277 g_static_mutex_lock(ðernet_mutex);
278 ethernet_list = g_slist_append(ethernet_list, element);
279 g_static_mutex_unlock(ðernet_mutex);
288 static void ethernet_remove(struct connman_element *element)
290 DBG("element %p name %s", element, element->name);
294 g_static_mutex_lock(ðernet_mutex);
295 ethernet_list = g_slist_remove(ethernet_list, element);
296 g_static_mutex_unlock(ðernet_mutex);
299 static struct connman_driver ethernet_driver = {
301 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
302 .subtype = CONNMAN_ELEMENT_SUBTYPE_ETHERNET,
303 .probe = ethernet_probe,
304 .remove = ethernet_remove,
307 static int rtnl_init(void)
309 struct sockaddr_nl addr;
314 sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
318 memset(&addr, 0, sizeof(addr));
319 addr.nl_family = AF_NETLINK;
320 addr.nl_groups = RTMGRP_LINK;
322 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
328 channel = g_io_channel_unix_new(sk);
329 g_io_channel_set_close_on_unref(channel, TRUE);
331 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
337 static void rtnl_cleanup(void)
341 g_io_channel_shutdown(channel, TRUE, NULL);
342 g_io_channel_unref(channel);
347 static int ethernet_init(void)
355 err = connman_driver_register(ðernet_driver);
364 static void ethernet_exit(void)
366 connman_driver_unregister(ðernet_driver);
371 CONNMAN_PLUGIN_DEFINE("ethernet", "Ethernet interface plugin", VERSION,
372 ethernet_init, ethernet_exit)