5 * Copyright (C) 2007-2013 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
29 #include <sys/ioctl.h>
30 #include <sys/types.h>
34 #include <linux/if_vlan.h>
35 #include <linux/sockios.h>
36 #include <linux/ethtool.h>
39 #define IFF_LOWER_UP 0x10000
44 #define CONNMAN_API_SUBJECT_TO_CHANGE
45 #include <connman/technology.h>
46 #include <connman/plugin.h>
47 #include <connman/device.h>
48 #include <connman/inet.h>
49 #include <connman/rtnl.h>
50 #include <connman/log.h>
51 #include <connman/setting.h>
53 static bool eth_tethering = false;
55 struct ethernet_data {
59 struct connman_network *network;
63 static int get_vlan_vid(const char *ifname)
65 struct vlan_ioctl_args vifr;
69 memset(&vifr, 0, sizeof(vifr));
71 sk = socket(AF_INET, SOCK_STREAM, 0);
75 vifr.cmd = GET_VLAN_VID_CMD;
76 strncpy(vifr.device1, ifname, sizeof(vifr.device1));
78 if(ioctl(sk, SIOCSIFVLAN, &vifr) >= 0)
88 static int get_dsa_port(const char *ifname)
93 struct ethtool_cmd cmd;
94 struct ethtool_drvinfo drvinfocmd;
95 struct vlan_ioctl_args vifr;
97 sk = socket(AF_INET, SOCK_STREAM, 0);
101 memset(&ifr, 0, sizeof(ifr));
102 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
104 /* check if it is a vlan and get physical interface name*/
105 vifr.cmd = GET_VLAN_REALDEV_NAME_CMD;
106 strncpy(vifr.device1, ifname, sizeof(vifr.device1));
108 if(ioctl(sk, SIOCSIFVLAN, &vifr) >= 0)
109 strncpy(ifr.ifr_name, vifr.u.device2, sizeof(ifr.ifr_name));
111 /* get driver info */
112 drvinfocmd.cmd = ETHTOOL_GDRVINFO;
113 ifr.ifr_data = (caddr_t)&drvinfocmd;
115 if (!ioctl(sk, SIOCETHTOOL, &ifr)) {
116 if(!strcmp(drvinfocmd.driver, "dsa")) {
118 cmd.cmd = ETHTOOL_GSET;
119 ifr.ifr_data = (caddr_t)&cmd;
121 if (!ioctl(sk, SIOCETHTOOL, &ifr))
122 dsaport = cmd.phy_address;
130 static int eth_network_probe(struct connman_network *network)
132 DBG("network %p", network);
137 static void eth_network_remove(struct connman_network *network)
139 DBG("network %p", network);
142 static int eth_network_connect(struct connman_network *network)
144 DBG("network %p", network);
146 connman_network_set_connected(network, true);
151 static int eth_network_disconnect(struct connman_network *network)
153 DBG("network %p", network);
155 connman_network_set_connected(network, false);
160 static struct connman_network_driver eth_network_driver = {
162 .type = CONNMAN_NETWORK_TYPE_ETHERNET,
163 .probe = eth_network_probe,
164 .remove = eth_network_remove,
165 .connect = eth_network_connect,
166 .disconnect = eth_network_disconnect,
169 static void add_network(struct connman_device *device,
170 struct ethernet_data *ethernet)
172 struct connman_network *network;
176 network = connman_network_create("carrier",
177 CONNMAN_NETWORK_TYPE_ETHERNET);
181 index = connman_device_get_index(device);
182 connman_network_set_index(network, index);
183 ifname = connman_inet_ifname(index);
187 connman_network_set_name(network, "Wired");
189 if (connman_device_add_network(device, network) < 0) {
190 connman_network_unref(network);
195 if (!eth_tethering) {
196 char group[25] = "cable";
199 vid = get_vlan_vid(ifname);
200 dsaport = get_dsa_port(ifname);
203 * Prevent service from starting the reconnect
204 * procedure as we do not want the DHCP client
205 * to run when tethering.
207 if((vid >= 0) && (dsaport >= 0))
208 snprintf(group, sizeof(group), "p%02x_%03x_cable", dsaport, vid);
210 snprintf(group, sizeof(group), "%03x_cable", vid);
211 else if (dsaport >= 0)
212 snprintf(group, sizeof(group), "p%02x_cable", dsaport);
214 connman_network_set_group(network, group);
217 ethernet->network = network;
221 static void remove_network(struct connman_device *device,
222 struct ethernet_data *ethernet)
224 if (!ethernet->network)
227 connman_device_remove_network(device, ethernet->network);
228 connman_network_unref(ethernet->network);
230 ethernet->network = NULL;
233 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
235 struct connman_device *device = user_data;
236 struct ethernet_data *ethernet = connman_device_get_data(device);
238 DBG("index %d flags %d change %d", ethernet->index, flags, change);
240 if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
241 if (flags & IFF_UP) {
243 connman_device_set_powered(device, true);
246 connman_device_set_powered(device, false);
250 if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
251 if (flags & IFF_LOWER_UP) {
253 add_network(device, ethernet);
256 remove_network(device, ethernet);
260 ethernet->flags = flags;
263 static int eth_dev_probe(struct connman_device *device)
265 struct ethernet_data *ethernet;
267 DBG("device %p", device);
269 ethernet = g_try_new0(struct ethernet_data, 1);
273 connman_device_set_data(device, ethernet);
275 ethernet->index = connman_device_get_index(device);
278 ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
279 ethernet_newlink, device);
284 static void eth_dev_remove(struct connman_device *device)
286 struct ethernet_data *ethernet = connman_device_get_data(device);
288 DBG("device %p", device);
290 connman_device_set_data(device, NULL);
292 connman_rtnl_remove_watch(ethernet->watch);
294 remove_network(device, ethernet);
299 static int eth_dev_enable(struct connman_device *device)
301 struct ethernet_data *ethernet = connman_device_get_data(device);
303 DBG("device %p", device);
305 return connman_inet_ifup(ethernet->index);
308 static int eth_dev_disable(struct connman_device *device)
310 struct ethernet_data *ethernet = connman_device_get_data(device);
312 DBG("device %p", device);
314 return connman_inet_ifdown(ethernet->index);
317 static struct connman_device_driver eth_dev_driver = {
319 .type = CONNMAN_DEVICE_TYPE_ETHERNET,
320 .probe = eth_dev_probe,
321 .remove = eth_dev_remove,
322 .enable = eth_dev_enable,
323 .disable = eth_dev_disable,
326 static int eth_tech_probe(struct connman_technology *technology)
331 static void eth_tech_remove(struct connman_technology *technology)
336 static GList *eth_interface_list = NULL;
338 static void eth_tech_add_interface(struct connman_technology *technology,
339 int index, const char *name, const char *ident)
341 DBG("index %d name %s ident %s", index, name, ident);
343 if (g_list_find(eth_interface_list, GINT_TO_POINTER((int)index)))
346 eth_interface_list = g_list_prepend(eth_interface_list,
347 (GINT_TO_POINTER((int) index)));
350 static void eth_tech_remove_interface(struct connman_technology *technology,
353 DBG("index %d", index);
355 eth_interface_list = g_list_remove(eth_interface_list,
356 GINT_TO_POINTER((int) index));
359 static void eth_tech_enable_tethering(struct connman_technology *technology,
363 struct ethernet_data *ethernet;
365 for (list = eth_interface_list; list; list = list->next) {
366 int index = GPOINTER_TO_INT(list->data);
367 struct connman_device *device =
368 connman_device_find_by_index(index);
371 ethernet = connman_device_get_data(device);
373 remove_network(device, ethernet);
376 connman_technology_tethering_notify(technology, true);
378 connman_inet_ifup(index);
380 connman_inet_add_to_bridge(index, bridge);
382 eth_tethering = true;
386 static void eth_tech_disable_tethering(struct connman_technology *technology,
391 for (list = eth_interface_list; list; list = list->next) {
392 int index = GPOINTER_TO_INT(list->data);
393 struct connman_device *device =
394 connman_device_find_by_index(index);
396 connman_inet_remove_from_bridge(index, bridge);
398 connman_technology_tethering_notify(technology, false);
401 connman_device_reconnect_service(device);
403 eth_tethering = false;
407 static int eth_tech_set_tethering(struct connman_technology *technology,
408 const char *identifier, const char *passphrase,
409 const char *bridge, bool enabled)
411 if (!connman_technology_is_tethering_allowed(
412 CONNMAN_SERVICE_TYPE_ETHERNET))
415 DBG("bridge %s enabled %d", bridge, enabled);
418 eth_tech_enable_tethering(technology, bridge);
420 eth_tech_disable_tethering(technology, bridge);
425 static struct connman_technology_driver eth_tech_driver = {
427 .type = CONNMAN_SERVICE_TYPE_ETHERNET,
428 .probe = eth_tech_probe,
429 .remove = eth_tech_remove,
430 .add_interface = eth_tech_add_interface,
431 .remove_interface = eth_tech_remove_interface,
432 .set_tethering = eth_tech_set_tethering,
435 static int ethernet_init(void)
439 err = connman_technology_driver_register(ð_tech_driver);
443 err = connman_network_driver_register(ð_network_driver);
447 err = connman_device_driver_register(ð_dev_driver);
449 connman_network_driver_unregister(ð_network_driver);
456 static void ethernet_exit(void)
458 connman_technology_driver_unregister(ð_tech_driver);
460 connman_network_driver_unregister(ð_network_driver);
462 connman_device_driver_unregister(ð_dev_driver);
465 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
466 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)