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 stpncpy(vifr.device1, ifname, sizeof(vifr.device1) - 1);
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 stpncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
104 /* check if it is a vlan and get physical interface name*/
105 vifr.cmd = GET_VLAN_REALDEV_NAME_CMD;
106 stpncpy(vifr.device1, ifname, sizeof(vifr.device1) - 1);
108 if(ioctl(sk, SIOCSIFVLAN, &vifr) >= 0) {
109 stpncpy(ifr.ifr_name, vifr.u.device2, sizeof(ifr.ifr_name) - 1);
110 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
113 /* get driver info */
114 drvinfocmd.cmd = ETHTOOL_GDRVINFO;
115 ifr.ifr_data = (caddr_t)&drvinfocmd;
117 if (!ioctl(sk, SIOCETHTOOL, &ifr)) {
118 if(!strcmp(drvinfocmd.driver, "dsa")) {
120 cmd.cmd = ETHTOOL_GSET;
121 ifr.ifr_data = (caddr_t)&cmd;
123 if (!ioctl(sk, SIOCETHTOOL, &ifr))
124 dsaport = cmd.phy_address;
132 static int eth_network_probe(struct connman_network *network)
134 DBG("network %p", network);
139 static void eth_network_remove(struct connman_network *network)
141 DBG("network %p", network);
144 static int eth_network_connect(struct connman_network *network)
146 DBG("network %p", network);
148 connman_network_set_connected(network, true);
153 static int eth_network_disconnect(struct connman_network *network)
155 DBG("network %p", network);
157 connman_network_set_connected(network, false);
162 static struct connman_network_driver eth_network_driver = {
164 .type = CONNMAN_NETWORK_TYPE_ETHERNET,
165 .probe = eth_network_probe,
166 .remove = eth_network_remove,
167 .connect = eth_network_connect,
168 .disconnect = eth_network_disconnect,
171 static void add_network(struct connman_device *device,
172 struct ethernet_data *ethernet)
174 struct connman_network *network;
178 network = connman_network_create("carrier",
179 CONNMAN_NETWORK_TYPE_ETHERNET);
183 index = connman_device_get_index(device);
184 connman_network_set_index(network, index);
185 ifname = connman_inet_ifname(index);
189 connman_network_set_name(network, "Wired");
191 if (connman_device_add_network(device, network) < 0) {
192 connman_network_unref(network);
197 if (!eth_tethering) {
198 char group[25] = "cable";
201 vid = get_vlan_vid(ifname);
202 dsaport = get_dsa_port(ifname);
205 * Prevent service from starting the reconnect
206 * procedure as we do not want the DHCP client
207 * to run when tethering.
209 if((vid >= 0) && (dsaport >= 0))
210 snprintf(group, sizeof(group), "p%02x_%03x_cable", dsaport, vid);
212 snprintf(group, sizeof(group), "%03x_cable", vid);
213 else if (dsaport >= 0)
214 snprintf(group, sizeof(group), "p%02x_cable", dsaport);
216 connman_network_set_group(network, group);
219 ethernet->network = network;
223 static void remove_network(struct connman_device *device,
224 struct ethernet_data *ethernet)
226 if (!ethernet->network)
229 connman_device_remove_network(device, ethernet->network);
230 connman_network_unref(ethernet->network);
232 ethernet->network = NULL;
235 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
237 struct connman_device *device = user_data;
238 struct ethernet_data *ethernet = connman_device_get_data(device);
240 DBG("index %d flags %d change %d", ethernet->index, flags, change);
242 if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
243 if (flags & IFF_UP) {
245 connman_device_set_powered(device, true);
248 connman_device_set_powered(device, false);
252 if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
253 if (flags & IFF_LOWER_UP) {
255 add_network(device, ethernet);
258 remove_network(device, ethernet);
262 ethernet->flags = flags;
265 static int eth_dev_probe(struct connman_device *device)
267 struct ethernet_data *ethernet;
269 DBG("device %p", device);
271 ethernet = g_try_new0(struct ethernet_data, 1);
275 connman_device_set_data(device, ethernet);
277 ethernet->index = connman_device_get_index(device);
280 ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
281 ethernet_newlink, device);
286 static void eth_dev_remove(struct connman_device *device)
288 struct ethernet_data *ethernet = connman_device_get_data(device);
290 DBG("device %p", device);
292 connman_device_set_data(device, NULL);
294 connman_rtnl_remove_watch(ethernet->watch);
296 remove_network(device, ethernet);
301 static int eth_dev_enable(struct connman_device *device)
303 struct ethernet_data *ethernet = connman_device_get_data(device);
305 DBG("device %p", device);
307 return connman_inet_ifup(ethernet->index);
310 static int eth_dev_disable(struct connman_device *device)
312 struct ethernet_data *ethernet = connman_device_get_data(device);
314 DBG("device %p", device);
316 return connman_inet_ifdown(ethernet->index);
319 static struct connman_device_driver eth_dev_driver = {
321 .type = CONNMAN_DEVICE_TYPE_ETHERNET,
322 .probe = eth_dev_probe,
323 .remove = eth_dev_remove,
324 .enable = eth_dev_enable,
325 .disable = eth_dev_disable,
328 static int eth_tech_probe(struct connman_technology *technology)
333 static void eth_tech_remove(struct connman_technology *technology)
338 static GList *eth_interface_list = NULL;
340 static void eth_tech_add_interface(struct connman_technology *technology,
341 int index, const char *name, const char *ident)
343 DBG("index %d name %s ident %s", index, name, ident);
345 if (g_list_find(eth_interface_list, GINT_TO_POINTER((int)index)))
348 eth_interface_list = g_list_prepend(eth_interface_list,
349 (GINT_TO_POINTER((int) index)));
352 static void eth_tech_remove_interface(struct connman_technology *technology,
355 DBG("index %d", index);
357 eth_interface_list = g_list_remove(eth_interface_list,
358 GINT_TO_POINTER((int) index));
361 static void eth_tech_enable_tethering(struct connman_technology *technology,
365 struct ethernet_data *ethernet;
367 for (list = eth_interface_list; list; list = list->next) {
368 int index = GPOINTER_TO_INT(list->data);
369 struct connman_device *device =
370 connman_device_find_by_index(index);
373 ethernet = connman_device_get_data(device);
375 remove_network(device, ethernet);
378 connman_technology_tethering_notify(technology, true);
380 connman_inet_ifup(index);
382 connman_inet_add_to_bridge(index, bridge);
384 eth_tethering = true;
388 static void eth_tech_disable_tethering(struct connman_technology *technology,
393 for (list = eth_interface_list; list; list = list->next) {
394 int index = GPOINTER_TO_INT(list->data);
395 struct connman_device *device =
396 connman_device_find_by_index(index);
398 connman_inet_remove_from_bridge(index, bridge);
400 connman_technology_tethering_notify(technology, false);
403 connman_device_reconnect_service(device);
405 eth_tethering = false;
409 static int eth_tech_set_tethering(struct connman_technology *technology,
410 const char *identifier, const char *passphrase,
411 const char *bridge, bool enabled)
413 if (!connman_technology_is_tethering_allowed(
414 CONNMAN_SERVICE_TYPE_ETHERNET))
417 DBG("bridge %s enabled %d", bridge, enabled);
420 eth_tech_enable_tethering(technology, bridge);
422 eth_tech_disable_tethering(technology, bridge);
427 static struct connman_technology_driver eth_tech_driver = {
429 .type = CONNMAN_SERVICE_TYPE_ETHERNET,
430 .probe = eth_tech_probe,
431 .remove = eth_tech_remove,
432 .add_interface = eth_tech_add_interface,
433 .remove_interface = eth_tech_remove_interface,
434 .set_tethering = eth_tech_set_tethering,
437 static int ethernet_init(void)
441 err = connman_technology_driver_register(ð_tech_driver);
445 err = connman_network_driver_register(ð_network_driver);
449 err = connman_device_driver_register(ð_dev_driver);
451 connman_network_driver_unregister(ð_network_driver);
458 static void ethernet_exit(void)
460 connman_technology_driver_unregister(ð_tech_driver);
462 connman_network_driver_unregister(ð_network_driver);
464 connman_device_driver_unregister(ð_dev_driver);
467 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
468 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)