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>
33 #include <linux/if_vlan.h>
34 #include <linux/sockios.h>
35 #include <linux/ethtool.h>
38 #define IFF_LOWER_UP 0x10000
43 #define CONNMAN_API_SUBJECT_TO_CHANGE
44 #include <connman/technology.h>
45 #include <connman/plugin.h>
46 #include <connman/device.h>
47 #include <connman/inet.h>
48 #include <connman/rtnl.h>
49 #include <connman/log.h>
50 #include <connman/setting.h>
52 static bool eth_tethering = false;
54 struct ethernet_data {
58 struct connman_network *network;
62 static int get_vlan_vid(const char *ifname)
64 struct vlan_ioctl_args vifr;
68 memset(&vifr, 0, sizeof(vifr));
70 sk = socket(AF_INET, SOCK_STREAM, 0);
74 vifr.cmd = GET_VLAN_VID_CMD;
75 strncpy(vifr.device1, ifname, sizeof(vifr.device1));
77 if(ioctl(sk, SIOCSIFVLAN, &vifr) >= 0)
87 static int get_dsa_port(const char *ifname)
92 struct ethtool_cmd cmd;
93 struct ethtool_drvinfo drvinfocmd;
94 struct vlan_ioctl_args vifr;
96 sk = socket(AF_INET, SOCK_STREAM, 0);
100 memset(&ifr, 0, sizeof(ifr));
101 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
103 /* check if it is a vlan and get physical interface name*/
104 vifr.cmd = GET_VLAN_REALDEV_NAME_CMD;
105 strncpy(vifr.device1, ifname, sizeof(vifr.device1));
107 if(ioctl(sk, SIOCSIFVLAN, &vifr) >= 0)
108 strncpy(ifr.ifr_name, vifr.u.device2, sizeof(ifr.ifr_name));
110 /* get driver info */
111 drvinfocmd.cmd = ETHTOOL_GDRVINFO;
112 ifr.ifr_data = (caddr_t)&drvinfocmd;
114 if (!ioctl(sk, SIOCETHTOOL, &ifr)) {
115 if(!strcmp(drvinfocmd.driver, "dsa")) {
117 cmd.cmd = ETHTOOL_GSET;
118 ifr.ifr_data = (caddr_t)&cmd;
120 if (!ioctl(sk, SIOCETHTOOL, &ifr))
121 dsaport = cmd.phy_address;
129 static int eth_network_probe(struct connman_network *network)
131 DBG("network %p", network);
136 static void eth_network_remove(struct connman_network *network)
138 DBG("network %p", network);
141 static int eth_network_connect(struct connman_network *network)
143 DBG("network %p", network);
145 connman_network_set_connected(network, true);
150 static int eth_network_disconnect(struct connman_network *network)
152 DBG("network %p", network);
154 connman_network_set_connected(network, false);
159 static struct connman_network_driver eth_network_driver = {
161 .type = CONNMAN_NETWORK_TYPE_ETHERNET,
162 .probe = eth_network_probe,
163 .remove = eth_network_remove,
164 .connect = eth_network_connect,
165 .disconnect = eth_network_disconnect,
168 static void add_network(struct connman_device *device,
169 struct ethernet_data *ethernet)
171 struct connman_network *network;
175 network = connman_network_create("carrier",
176 CONNMAN_NETWORK_TYPE_ETHERNET);
180 index = connman_device_get_index(device);
181 connman_network_set_index(network, index);
182 ifname = connman_inet_ifname(index);
186 connman_network_set_name(network, "Wired");
188 if (connman_device_add_network(device, network) < 0) {
189 connman_network_unref(network);
193 if (!eth_tethering) {
194 char group[16] = "cable";
197 vid = get_vlan_vid(ifname);
198 dsaport = get_dsa_port(ifname);
201 * Prevent service from starting the reconnect
202 * procedure as we do not want the DHCP client
203 * to run when tethering.
205 if((vid >= 0) && (dsaport >= 0))
206 snprintf(group, sizeof(group), "p%02x_%03x_cable", dsaport, vid);
208 snprintf(group, sizeof(group), "%03x_cable", vid);
209 else if (dsaport >= 0)
210 snprintf(group, sizeof(group), "p%02x_cable", dsaport);
212 connman_network_set_group(network, group);
215 ethernet->network = network;
219 static void remove_network(struct connman_device *device,
220 struct ethernet_data *ethernet)
222 if (!ethernet->network)
225 connman_device_remove_network(device, ethernet->network);
226 connman_network_unref(ethernet->network);
228 ethernet->network = NULL;
231 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
233 struct connman_device *device = user_data;
234 struct ethernet_data *ethernet = connman_device_get_data(device);
236 DBG("index %d flags %d change %d", ethernet->index, flags, change);
238 if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
239 if (flags & IFF_UP) {
241 connman_device_set_powered(device, true);
244 connman_device_set_powered(device, false);
248 if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
249 if (flags & IFF_LOWER_UP) {
251 add_network(device, ethernet);
254 remove_network(device, ethernet);
258 ethernet->flags = flags;
261 static int eth_dev_probe(struct connman_device *device)
263 struct ethernet_data *ethernet;
265 DBG("device %p", device);
267 ethernet = g_try_new0(struct ethernet_data, 1);
271 connman_device_set_data(device, ethernet);
273 ethernet->index = connman_device_get_index(device);
276 ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
277 ethernet_newlink, device);
282 static void eth_dev_remove(struct connman_device *device)
284 struct ethernet_data *ethernet = connman_device_get_data(device);
286 DBG("device %p", device);
288 connman_device_set_data(device, NULL);
290 connman_rtnl_remove_watch(ethernet->watch);
292 remove_network(device, ethernet);
297 static int eth_dev_enable(struct connman_device *device)
299 struct ethernet_data *ethernet = connman_device_get_data(device);
301 DBG("device %p", device);
303 return connman_inet_ifup(ethernet->index);
306 static int eth_dev_disable(struct connman_device *device)
308 struct ethernet_data *ethernet = connman_device_get_data(device);
310 DBG("device %p", device);
312 return connman_inet_ifdown(ethernet->index);
315 static struct connman_device_driver eth_dev_driver = {
317 .type = CONNMAN_DEVICE_TYPE_ETHERNET,
318 .probe = eth_dev_probe,
319 .remove = eth_dev_remove,
320 .enable = eth_dev_enable,
321 .disable = eth_dev_disable,
324 static int eth_tech_probe(struct connman_technology *technology)
329 static void eth_tech_remove(struct connman_technology *technology)
334 static GList *eth_interface_list = NULL;
336 static void eth_tech_add_interface(struct connman_technology *technology,
337 int index, const char *name, const char *ident)
339 DBG("index %d name %s ident %s", index, name, ident);
341 if (g_list_find(eth_interface_list, GINT_TO_POINTER((int)index)))
344 eth_interface_list = g_list_prepend(eth_interface_list,
345 (GINT_TO_POINTER((int) index)));
348 static void eth_tech_remove_interface(struct connman_technology *technology,
351 DBG("index %d", index);
353 eth_interface_list = g_list_remove(eth_interface_list,
354 GINT_TO_POINTER((int) index));
357 static void eth_tech_enable_tethering(struct connman_technology *technology,
361 struct ethernet_data *ethernet;
363 for (list = eth_interface_list; list; list = list->next) {
364 int index = GPOINTER_TO_INT(list->data);
365 struct connman_device *device =
366 connman_device_find_by_index(index);
369 ethernet = connman_device_get_data(device);
371 remove_network(device, ethernet);
374 connman_technology_tethering_notify(technology, true);
376 connman_inet_ifup(index);
378 connman_inet_add_to_bridge(index, bridge);
380 eth_tethering = true;
384 static void eth_tech_disable_tethering(struct connman_technology *technology,
389 for (list = eth_interface_list; list; list = list->next) {
390 int index = GPOINTER_TO_INT(list->data);
391 struct connman_device *device =
392 connman_device_find_by_index(index);
394 connman_inet_remove_from_bridge(index, bridge);
396 connman_technology_tethering_notify(technology, false);
399 connman_device_reconnect_service(device);
401 eth_tethering = false;
405 static int eth_tech_set_tethering(struct connman_technology *technology,
406 const char *identifier, const char *passphrase,
407 const char *bridge, bool enabled)
409 if (!connman_technology_is_tethering_allowed(
410 CONNMAN_SERVICE_TYPE_ETHERNET))
413 DBG("bridge %s enabled %d", bridge, enabled);
416 eth_tech_enable_tethering(technology, bridge);
418 eth_tech_disable_tethering(technology, bridge);
423 static struct connman_technology_driver eth_tech_driver = {
425 .type = CONNMAN_SERVICE_TYPE_ETHERNET,
426 .probe = eth_tech_probe,
427 .remove = eth_tech_remove,
428 .add_interface = eth_tech_add_interface,
429 .remove_interface = eth_tech_remove_interface,
430 .set_tethering = eth_tech_set_tethering,
433 static int ethernet_init(void)
437 err = connman_technology_driver_register(ð_tech_driver);
441 err = connman_network_driver_register(ð_network_driver);
445 err = connman_device_driver_register(ð_dev_driver);
447 connman_network_driver_unregister(ð_network_driver);
454 static void ethernet_exit(void)
456 connman_technology_driver_unregister(ð_tech_driver);
458 connman_network_driver_unregister(ð_network_driver);
460 connman_device_driver_unregister(ð_dev_driver);
463 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
464 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)