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>
51 #if defined TIZEN_EXT_WIFI_MESH
52 #include <connman/mesh.h>
55 static bool eth_tethering = false;
57 struct ethernet_data {
61 struct connman_network *network;
65 static int get_vlan_vid(const char *ifname)
67 struct vlan_ioctl_args vifr;
71 memset(&vifr, 0, sizeof(vifr));
73 sk = socket(AF_INET, SOCK_STREAM, 0);
77 vifr.cmd = GET_VLAN_VID_CMD;
78 strncpy(vifr.device1, ifname, sizeof(vifr.device1));
80 if(ioctl(sk, SIOCSIFVLAN, &vifr) >= 0)
90 static int get_dsa_port(const char *ifname)
95 struct ethtool_cmd cmd;
96 struct ethtool_drvinfo drvinfocmd;
97 struct vlan_ioctl_args vifr;
99 sk = socket(AF_INET, SOCK_STREAM, 0);
103 memset(&ifr, 0, sizeof(ifr));
104 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
106 /* check if it is a vlan and get physical interface name*/
107 vifr.cmd = GET_VLAN_REALDEV_NAME_CMD;
108 strncpy(vifr.device1, ifname, sizeof(vifr.device1));
110 if(ioctl(sk, SIOCSIFVLAN, &vifr) >= 0)
111 strncpy(ifr.ifr_name, vifr.u.device2, sizeof(ifr.ifr_name));
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);
196 if (!eth_tethering) {
197 char group[16] = "cable";
200 vid = get_vlan_vid(ifname);
201 dsaport = get_dsa_port(ifname);
204 * Prevent service from starting the reconnect
205 * procedure as we do not want the DHCP client
206 * to run when tethering.
208 if((vid >= 0) && (dsaport >= 0))
209 snprintf(group, sizeof(group), "p%02x_%03x_cable", dsaport, vid);
211 snprintf(group, sizeof(group), "%03x_cable", vid);
212 else if (dsaport >= 0)
213 snprintf(group, sizeof(group), "p%02x_cable", dsaport);
215 connman_network_set_group(network, group);
218 ethernet->network = network;
222 static void remove_network(struct connman_device *device,
223 struct ethernet_data *ethernet)
225 if (!ethernet->network)
228 connman_device_remove_network(device, ethernet->network);
229 connman_network_unref(ethernet->network);
231 ethernet->network = NULL;
234 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
236 struct connman_device *device = user_data;
237 struct ethernet_data *ethernet = connman_device_get_data(device);
239 DBG("index %d flags %d change %d", ethernet->index, flags, change);
241 if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
242 if (flags & IFF_UP) {
244 connman_device_set_powered(device, true);
247 connman_device_set_powered(device, false);
251 if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
252 if (flags & IFF_LOWER_UP) {
254 add_network(device, ethernet);
257 remove_network(device, ethernet);
258 #if defined TIZEN_EXT_WIFI_MESH
259 /* Remove ethernet from mesh bridge */
260 __connman_mesh_remove_ethernet_from_bridge();
265 ethernet->flags = flags;
268 static int eth_dev_probe(struct connman_device *device)
270 struct ethernet_data *ethernet;
272 DBG("device %p", device);
274 ethernet = g_try_new0(struct ethernet_data, 1);
278 connman_device_set_data(device, ethernet);
280 ethernet->index = connman_device_get_index(device);
283 ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
284 ethernet_newlink, device);
289 static void eth_dev_remove(struct connman_device *device)
291 struct ethernet_data *ethernet = connman_device_get_data(device);
293 DBG("device %p", device);
295 connman_device_set_data(device, NULL);
297 connman_rtnl_remove_watch(ethernet->watch);
299 remove_network(device, ethernet);
304 static int eth_dev_enable(struct connman_device *device)
306 struct ethernet_data *ethernet = connman_device_get_data(device);
308 DBG("device %p", device);
310 return connman_inet_ifup(ethernet->index);
313 static int eth_dev_disable(struct connman_device *device)
315 struct ethernet_data *ethernet = connman_device_get_data(device);
317 DBG("device %p", device);
319 return connman_inet_ifdown(ethernet->index);
322 static struct connman_device_driver eth_dev_driver = {
324 .type = CONNMAN_DEVICE_TYPE_ETHERNET,
325 .probe = eth_dev_probe,
326 .remove = eth_dev_remove,
327 .enable = eth_dev_enable,
328 .disable = eth_dev_disable,
331 static int eth_tech_probe(struct connman_technology *technology)
336 static void eth_tech_remove(struct connman_technology *technology)
341 static GList *eth_interface_list = NULL;
343 static void eth_tech_add_interface(struct connman_technology *technology,
344 int index, const char *name, const char *ident)
346 DBG("index %d name %s ident %s", index, name, ident);
348 if (g_list_find(eth_interface_list, GINT_TO_POINTER((int)index)))
351 eth_interface_list = g_list_prepend(eth_interface_list,
352 (GINT_TO_POINTER((int) index)));
355 static void eth_tech_remove_interface(struct connman_technology *technology,
358 DBG("index %d", index);
360 eth_interface_list = g_list_remove(eth_interface_list,
361 GINT_TO_POINTER((int) index));
364 static void eth_tech_enable_tethering(struct connman_technology *technology,
368 struct ethernet_data *ethernet;
370 for (list = eth_interface_list; list; list = list->next) {
371 int index = GPOINTER_TO_INT(list->data);
372 struct connman_device *device =
373 connman_device_find_by_index(index);
376 ethernet = connman_device_get_data(device);
378 remove_network(device, ethernet);
381 connman_technology_tethering_notify(technology, true);
383 connman_inet_ifup(index);
385 connman_inet_add_to_bridge(index, bridge);
387 eth_tethering = true;
391 static void eth_tech_disable_tethering(struct connman_technology *technology,
396 for (list = eth_interface_list; list; list = list->next) {
397 int index = GPOINTER_TO_INT(list->data);
398 struct connman_device *device =
399 connman_device_find_by_index(index);
401 connman_inet_remove_from_bridge(index, bridge);
403 connman_technology_tethering_notify(technology, false);
406 connman_device_reconnect_service(device);
408 eth_tethering = false;
412 static int eth_tech_set_tethering(struct connman_technology *technology,
413 const char *identifier, const char *passphrase,
414 const char *bridge, bool enabled)
416 if (!connman_technology_is_tethering_allowed(
417 CONNMAN_SERVICE_TYPE_ETHERNET))
420 DBG("bridge %s enabled %d", bridge, enabled);
423 eth_tech_enable_tethering(technology, bridge);
425 eth_tech_disable_tethering(technology, bridge);
430 static struct connman_technology_driver eth_tech_driver = {
432 .type = CONNMAN_SERVICE_TYPE_ETHERNET,
433 .probe = eth_tech_probe,
434 .remove = eth_tech_remove,
435 .add_interface = eth_tech_add_interface,
436 .remove_interface = eth_tech_remove_interface,
437 .set_tethering = eth_tech_set_tethering,
440 #if defined TIZEN_EXT_WIFI_MESH
441 static int eth_mesh_add_to_bridge(const char *bridge)
444 struct ethernet_data *ethernet;
446 DBG("Add ethernet to bridge %s", bridge);
448 for (list = eth_interface_list; list; list = list->next) {
449 int index = GPOINTER_TO_INT(list->data);
450 struct connman_device *device =
451 connman_device_find_by_index(index);
454 ethernet = connman_device_get_data(device);
456 remove_network(device, ethernet);
459 connman_inet_ifup(index);
461 connman_inet_add_to_bridge(index, bridge);
467 static int eth_mesh_remove_from_bridge(const char *bridge)
471 DBG("Remove ethernet from bridge %s", bridge);
473 for (list = eth_interface_list; list; list = list->next) {
474 int index = GPOINTER_TO_INT(list->data);
476 connman_inet_remove_from_bridge(index, bridge);
482 static struct connman_mesh_eth_driver eth_mesh_driver = {
483 .add_to_bridge = eth_mesh_add_to_bridge,
484 .remove_from_bridge = eth_mesh_remove_from_bridge,
488 static int ethernet_init(void)
492 err = connman_technology_driver_register(ð_tech_driver);
496 #if defined TIZEN_EXT_WIFI_MESH
497 err = connman_mesh_eth_driver_register(ð_mesh_driver);
502 err = connman_network_driver_register(ð_network_driver);
506 err = connman_device_driver_register(ð_dev_driver);
508 connman_network_driver_unregister(ð_network_driver);
515 static void ethernet_exit(void)
517 connman_technology_driver_unregister(ð_tech_driver);
519 #if defined TIZEN_EXT_WIFI_MESH
520 connman_mesh_eth_driver_unregister(ð_mesh_driver);
523 connman_network_driver_unregister(ð_network_driver);
525 connman_device_driver_unregister(ð_dev_driver);
528 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
529 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)