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>
52 #if defined TIZEN_EXT_WIFI_MESH
53 #include <connman/mesh.h>
56 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
57 #include <connman/option.h>
58 #include <gsupplicant/gsupplicant.h>
59 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
61 static bool eth_tethering = false;
63 struct ethernet_data {
67 struct connman_network *network;
68 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
69 GSupplicantInterface *interface;
70 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
74 static int get_vlan_vid(const char *ifname)
76 struct vlan_ioctl_args vifr;
80 memset(&vifr, 0, sizeof(vifr));
82 sk = socket(AF_INET, SOCK_STREAM, 0);
86 vifr.cmd = GET_VLAN_VID_CMD;
87 strncpy(vifr.device1, ifname, sizeof(vifr.device1));
89 if(ioctl(sk, SIOCSIFVLAN, &vifr) >= 0)
99 static int get_dsa_port(const char *ifname)
104 struct ethtool_cmd cmd;
105 struct ethtool_drvinfo drvinfocmd;
106 struct vlan_ioctl_args vifr;
108 sk = socket(AF_INET, SOCK_STREAM, 0);
112 memset(&ifr, 0, sizeof(ifr));
113 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
115 /* check if it is a vlan and get physical interface name*/
116 vifr.cmd = GET_VLAN_REALDEV_NAME_CMD;
117 strncpy(vifr.device1, ifname, sizeof(vifr.device1));
119 if(ioctl(sk, SIOCSIFVLAN, &vifr) >= 0)
120 strncpy(ifr.ifr_name, vifr.u.device2, sizeof(ifr.ifr_name));
122 /* get driver info */
123 drvinfocmd.cmd = ETHTOOL_GDRVINFO;
124 ifr.ifr_data = (caddr_t)&drvinfocmd;
126 if (!ioctl(sk, SIOCETHTOOL, &ifr)) {
127 if(!strcmp(drvinfocmd.driver, "dsa")) {
129 cmd.cmd = ETHTOOL_GSET;
130 ifr.ifr_data = (caddr_t)&cmd;
132 if (!ioctl(sk, SIOCETHTOOL, &ifr))
133 dsaport = cmd.phy_address;
141 static int eth_network_probe(struct connman_network *network)
143 DBG("network %p", network);
148 static void eth_network_remove(struct connman_network *network)
150 DBG("network %p", network);
153 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
154 static struct connman_network *g_network = NULL;
156 void handle_eap_signal(GSupplicantInterface *interface, bool status)
158 DBG("captured EAP signal");
163 if (g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
166 if (!connman_network_check_validity(g_network))
169 DBG("network is valid");
171 g_supplicant_unregister_eap_callback();
174 // Should we mark service as non favorite or make autoconnect as false?
176 struct ethernet_data *ethernet = g_supplicant_interface_get_data(interface);
177 if (ethernet && ethernet->interface) {
178 g_supplicant_interface_remove(ethernet->interface, NULL, NULL);
179 ethernet->interface = NULL;
182 connman_network_set_error(g_network, CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
187 connman_network_set_connected(g_network, status);
191 static void interface_create_callback(int result,
192 GSupplicantInterface *interface, void *user_data)
194 struct ethernet_data *ethernet = user_data;
196 if (result < 0 || !interface || !ethernet)
199 DBG("result %d ifname %s, ethernet %p", result,
200 g_supplicant_interface_get_ifname(interface),
203 ethernet->interface = interface;
204 g_supplicant_interface_set_data(interface, ethernet);
207 static int eth_network_connect(struct connman_network *network)
209 DBG("network %p", network);
211 struct connman_service *service = connman_service_lookup_from_network(network);
213 if (service && __connman_service_get_use_eapol(service)) {
214 struct connman_device *device = connman_network_get_device(network);
215 struct ethernet_data *ethernet = connman_device_get_data(device);
216 const char *driver = "wired";
217 int index = connman_network_get_index(network);
218 char *ifname = connman_inet_ifname(index);;
219 char *config_file = NULL;
221 g_supplicant_register_eap_callback(handle_eap_signal);
224 if (asprintf(&config_file, "/opt/usr/data/network/%s-eapol.conf", ifname) < 0) {
229 DBG("config_file %s", config_file);
231 g_supplicant_replace_config_file(ifname, config_file);
235 * TODO: RemoveInterface if already present because
236 * already created interface will not start EAP handshake.
238 g_supplicant_interface_create(ifname, driver, NULL,
239 interface_create_callback, ethernet);
246 connman_network_set_connected(network, true);
251 static int eth_network_disconnect(struct connman_network *network)
253 DBG("network %p", network);
255 struct connman_service *service = connman_service_lookup_from_network(network);
257 if (service && __connman_service_get_use_eapol(service)) {
258 struct connman_device *device = connman_network_get_device(network);
259 struct ethernet_data *ethernet = connman_device_get_data(device);
262 g_supplicant_unregister_eap_callback();
263 if (ethernet && ethernet->interface) {
264 g_supplicant_interface_remove(ethernet->interface, NULL, NULL);
265 ethernet->interface = NULL;
267 connman_network_set_associating(network, false);
268 connman_network_set_connected(network, false);
273 connman_network_set_connected(network, false);
278 #else /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
280 static int eth_network_connect(struct connman_network *network)
282 DBG("network %p", network);
284 connman_network_set_connected(network, true);
289 static int eth_network_disconnect(struct connman_network *network)
291 DBG("network %p", network);
293 connman_network_set_connected(network, false);
298 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
300 static struct connman_network_driver eth_network_driver = {
302 .type = CONNMAN_NETWORK_TYPE_ETHERNET,
303 .probe = eth_network_probe,
304 .remove = eth_network_remove,
305 .connect = eth_network_connect,
306 .disconnect = eth_network_disconnect,
309 static void add_network(struct connman_device *device,
310 struct ethernet_data *ethernet)
312 struct connman_network *network;
316 network = connman_network_create("carrier",
317 CONNMAN_NETWORK_TYPE_ETHERNET);
321 index = connman_device_get_index(device);
322 connman_network_set_index(network, index);
323 ifname = connman_inet_ifname(index);
327 connman_network_set_name(network, "Wired");
329 if (connman_device_add_network(device, network) < 0) {
330 connman_network_unref(network);
335 if (!eth_tethering) {
336 char group[25] = "cable";
339 vid = get_vlan_vid(ifname);
340 dsaport = get_dsa_port(ifname);
343 * Prevent service from starting the reconnect
344 * procedure as we do not want the DHCP client
345 * to run when tethering.
347 if((vid >= 0) && (dsaport >= 0))
348 snprintf(group, sizeof(group), "p%02x_%03x_cable", dsaport, vid);
350 snprintf(group, sizeof(group), "%03x_cable", vid);
351 else if (dsaport >= 0)
352 snprintf(group, sizeof(group), "p%02x_cable", dsaport);
354 connman_network_set_group(network, group);
357 ethernet->network = network;
361 static void remove_network(struct connman_device *device,
362 struct ethernet_data *ethernet)
364 if (!ethernet->network)
367 connman_device_remove_network(device, ethernet->network);
368 connman_network_unref(ethernet->network);
370 ethernet->network = NULL;
373 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
375 struct connman_device *device = user_data;
376 struct ethernet_data *ethernet = connman_device_get_data(device);
378 DBG("index %d flags %d change %d", ethernet->index, flags, change);
380 if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
381 if (flags & IFF_UP) {
383 connman_device_set_powered(device, true);
386 connman_device_set_powered(device, false);
390 if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
391 if (flags & IFF_LOWER_UP) {
393 add_network(device, ethernet);
396 remove_network(device, ethernet);
397 #if defined TIZEN_EXT_WIFI_MESH
398 /* Remove ethernet from mesh bridge */
399 __connman_mesh_remove_ethernet_from_bridge();
404 ethernet->flags = flags;
407 static int eth_dev_probe(struct connman_device *device)
409 struct ethernet_data *ethernet;
411 DBG("device %p", device);
413 ethernet = g_try_new0(struct ethernet_data, 1);
417 connman_device_set_data(device, ethernet);
419 ethernet->index = connman_device_get_index(device);
421 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
422 ethernet->interface = NULL;
423 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
425 ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
426 ethernet_newlink, device);
431 static void eth_dev_remove(struct connman_device *device)
433 struct ethernet_data *ethernet = connman_device_get_data(device);
435 DBG("device %p", device);
437 connman_device_set_data(device, NULL);
439 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
440 if (ethernet && ethernet->interface) {
441 g_supplicant_interface_remove(ethernet->interface, NULL, NULL);
442 ethernet->interface = NULL;
444 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
446 connman_rtnl_remove_watch(ethernet->watch);
448 remove_network(device, ethernet);
453 static int eth_dev_enable(struct connman_device *device)
455 struct ethernet_data *ethernet = connman_device_get_data(device);
457 DBG("device %p", device);
459 return connman_inet_ifup(ethernet->index);
462 static int eth_dev_disable(struct connman_device *device)
464 struct ethernet_data *ethernet = connman_device_get_data(device);
466 DBG("device %p", device);
468 return connman_inet_ifdown(ethernet->index);
471 static struct connman_device_driver eth_dev_driver = {
473 .type = CONNMAN_DEVICE_TYPE_ETHERNET,
474 .probe = eth_dev_probe,
475 .remove = eth_dev_remove,
476 .enable = eth_dev_enable,
477 .disable = eth_dev_disable,
480 static int eth_tech_probe(struct connman_technology *technology)
485 static void eth_tech_remove(struct connman_technology *technology)
490 static GList *eth_interface_list = NULL;
492 static void eth_tech_add_interface(struct connman_technology *technology,
493 int index, const char *name, const char *ident)
495 DBG("index %d name %s ident %s", index, name, ident);
497 if (g_list_find(eth_interface_list, GINT_TO_POINTER((int)index)))
500 eth_interface_list = g_list_prepend(eth_interface_list,
501 (GINT_TO_POINTER((int) index)));
504 static void eth_tech_remove_interface(struct connman_technology *technology,
507 DBG("index %d", index);
509 eth_interface_list = g_list_remove(eth_interface_list,
510 GINT_TO_POINTER((int) index));
513 static void eth_tech_enable_tethering(struct connman_technology *technology,
517 struct ethernet_data *ethernet;
519 for (list = eth_interface_list; list; list = list->next) {
520 int index = GPOINTER_TO_INT(list->data);
521 struct connman_device *device =
522 connman_device_find_by_index(index);
525 ethernet = connman_device_get_data(device);
527 remove_network(device, ethernet);
530 connman_technology_tethering_notify(technology, true);
532 connman_inet_ifup(index);
534 connman_inet_add_to_bridge(index, bridge);
536 eth_tethering = true;
540 static void eth_tech_disable_tethering(struct connman_technology *technology,
545 for (list = eth_interface_list; list; list = list->next) {
546 int index = GPOINTER_TO_INT(list->data);
547 struct connman_device *device =
548 connman_device_find_by_index(index);
550 connman_inet_remove_from_bridge(index, bridge);
552 connman_technology_tethering_notify(technology, false);
555 connman_device_reconnect_service(device);
557 eth_tethering = false;
561 static int eth_tech_set_tethering(struct connman_technology *technology,
562 const char *identifier, const char *passphrase,
563 const char *bridge, bool enabled)
565 if (!connman_technology_is_tethering_allowed(
566 CONNMAN_SERVICE_TYPE_ETHERNET))
569 DBG("bridge %s enabled %d", bridge, enabled);
572 eth_tech_enable_tethering(technology, bridge);
574 eth_tech_disable_tethering(technology, bridge);
579 static struct connman_technology_driver eth_tech_driver = {
581 .type = CONNMAN_SERVICE_TYPE_ETHERNET,
582 .probe = eth_tech_probe,
583 .remove = eth_tech_remove,
584 .add_interface = eth_tech_add_interface,
585 .remove_interface = eth_tech_remove_interface,
586 .set_tethering = eth_tech_set_tethering,
589 #if defined TIZEN_EXT_WIFI_MESH
590 static int eth_mesh_add_to_bridge(const char *bridge)
593 struct ethernet_data *ethernet;
595 DBG("Add ethernet to bridge %s", bridge);
597 for (list = eth_interface_list; list; list = list->next) {
598 int index = GPOINTER_TO_INT(list->data);
599 struct connman_device *device =
600 connman_device_find_by_index(index);
603 ethernet = connman_device_get_data(device);
605 remove_network(device, ethernet);
608 connman_inet_ifup(index);
610 connman_inet_add_to_bridge(index, bridge);
616 static int eth_mesh_remove_from_bridge(const char *bridge)
620 DBG("Remove ethernet from bridge %s", bridge);
622 for (list = eth_interface_list; list; list = list->next) {
623 int index = GPOINTER_TO_INT(list->data);
625 connman_inet_remove_from_bridge(index, bridge);
631 static struct connman_mesh_eth_driver eth_mesh_driver = {
632 .add_to_bridge = eth_mesh_add_to_bridge,
633 .remove_from_bridge = eth_mesh_remove_from_bridge,
637 static int ethernet_init(void)
641 err = connman_technology_driver_register(ð_tech_driver);
645 #if defined TIZEN_EXT_WIFI_MESH
646 err = connman_mesh_eth_driver_register(ð_mesh_driver);
651 err = connman_network_driver_register(ð_network_driver);
655 err = connman_device_driver_register(ð_dev_driver);
657 connman_network_driver_unregister(ð_network_driver);
664 static void ethernet_exit(void)
666 connman_technology_driver_unregister(ð_tech_driver);
668 #if defined TIZEN_EXT_WIFI_MESH
669 connman_mesh_eth_driver_unregister(ð_mesh_driver);
672 connman_network_driver_unregister(ð_network_driver);
674 connman_device_driver_unregister(ð_dev_driver);
677 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
678 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)