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)
227 DBG("config_file %s", config_file);
229 g_supplicant_replace_config_file(ifname, config_file);
233 * TODO: RemoveInterface if already present because
234 * already created interface will not start EAP handshake.
236 g_supplicant_interface_create(ifname, driver, NULL,
237 interface_create_callback, ethernet);
244 connman_network_set_connected(network, true);
249 static int eth_network_disconnect(struct connman_network *network)
251 DBG("network %p", network);
253 struct connman_service *service = connman_service_lookup_from_network(network);
255 if (service && __connman_service_get_use_eapol(service)) {
256 struct connman_device *device = connman_network_get_device(network);
257 struct ethernet_data *ethernet = connman_device_get_data(device);
260 g_supplicant_unregister_eap_callback();
261 if (ethernet && ethernet->interface) {
262 g_supplicant_interface_remove(ethernet->interface, NULL, NULL);
263 ethernet->interface = NULL;
265 connman_network_set_associating(network, false);
266 connman_network_set_connected(network, false);
271 connman_network_set_connected(network, false);
276 #else /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
278 static int eth_network_connect(struct connman_network *network)
280 DBG("network %p", network);
282 connman_network_set_connected(network, true);
287 static int eth_network_disconnect(struct connman_network *network)
289 DBG("network %p", network);
291 connman_network_set_connected(network, false);
296 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
298 static struct connman_network_driver eth_network_driver = {
300 .type = CONNMAN_NETWORK_TYPE_ETHERNET,
301 .probe = eth_network_probe,
302 .remove = eth_network_remove,
303 .connect = eth_network_connect,
304 .disconnect = eth_network_disconnect,
307 static void add_network(struct connman_device *device,
308 struct ethernet_data *ethernet)
310 struct connman_network *network;
314 network = connman_network_create("carrier",
315 CONNMAN_NETWORK_TYPE_ETHERNET);
319 index = connman_device_get_index(device);
320 connman_network_set_index(network, index);
321 ifname = connman_inet_ifname(index);
325 connman_network_set_name(network, "Wired");
327 if (connman_device_add_network(device, network) < 0) {
328 connman_network_unref(network);
333 if (!eth_tethering) {
334 char group[25] = "cable";
337 vid = get_vlan_vid(ifname);
338 dsaport = get_dsa_port(ifname);
341 * Prevent service from starting the reconnect
342 * procedure as we do not want the DHCP client
343 * to run when tethering.
345 if((vid >= 0) && (dsaport >= 0))
346 snprintf(group, sizeof(group), "p%02x_%03x_cable", dsaport, vid);
348 snprintf(group, sizeof(group), "%03x_cable", vid);
349 else if (dsaport >= 0)
350 snprintf(group, sizeof(group), "p%02x_cable", dsaport);
352 connman_network_set_group(network, group);
355 ethernet->network = network;
359 static void remove_network(struct connman_device *device,
360 struct ethernet_data *ethernet)
362 if (!ethernet->network)
365 connman_device_remove_network(device, ethernet->network);
366 connman_network_unref(ethernet->network);
368 ethernet->network = NULL;
371 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
373 struct connman_device *device = user_data;
374 struct ethernet_data *ethernet = connman_device_get_data(device);
376 DBG("index %d flags %d change %d", ethernet->index, flags, change);
378 if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
379 if (flags & IFF_UP) {
381 connman_device_set_powered(device, true);
384 connman_device_set_powered(device, false);
388 if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
389 if (flags & IFF_LOWER_UP) {
391 add_network(device, ethernet);
394 remove_network(device, ethernet);
395 #if defined TIZEN_EXT_WIFI_MESH
396 /* Remove ethernet from mesh bridge */
397 __connman_mesh_remove_ethernet_from_bridge();
402 ethernet->flags = flags;
405 static int eth_dev_probe(struct connman_device *device)
407 struct ethernet_data *ethernet;
409 DBG("device %p", device);
411 ethernet = g_try_new0(struct ethernet_data, 1);
415 connman_device_set_data(device, ethernet);
417 ethernet->index = connman_device_get_index(device);
419 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
420 ethernet->interface = NULL;
421 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
423 ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
424 ethernet_newlink, device);
429 static void eth_dev_remove(struct connman_device *device)
431 struct ethernet_data *ethernet = connman_device_get_data(device);
433 DBG("device %p", device);
435 connman_device_set_data(device, NULL);
437 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
438 if (ethernet && ethernet->interface) {
439 g_supplicant_interface_remove(ethernet->interface, NULL, NULL);
440 ethernet->interface = NULL;
442 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
444 connman_rtnl_remove_watch(ethernet->watch);
446 remove_network(device, ethernet);
451 static int eth_dev_enable(struct connman_device *device)
453 struct ethernet_data *ethernet = connman_device_get_data(device);
455 DBG("device %p", device);
457 return connman_inet_ifup(ethernet->index);
460 static int eth_dev_disable(struct connman_device *device)
462 struct ethernet_data *ethernet = connman_device_get_data(device);
464 DBG("device %p", device);
466 return connman_inet_ifdown(ethernet->index);
469 static struct connman_device_driver eth_dev_driver = {
471 .type = CONNMAN_DEVICE_TYPE_ETHERNET,
472 .probe = eth_dev_probe,
473 .remove = eth_dev_remove,
474 .enable = eth_dev_enable,
475 .disable = eth_dev_disable,
478 static int eth_tech_probe(struct connman_technology *technology)
483 static void eth_tech_remove(struct connman_technology *technology)
488 static GList *eth_interface_list = NULL;
490 static void eth_tech_add_interface(struct connman_technology *technology,
491 int index, const char *name, const char *ident)
493 DBG("index %d name %s ident %s", index, name, ident);
495 if (g_list_find(eth_interface_list, GINT_TO_POINTER((int)index)))
498 eth_interface_list = g_list_prepend(eth_interface_list,
499 (GINT_TO_POINTER((int) index)));
502 static void eth_tech_remove_interface(struct connman_technology *technology,
505 DBG("index %d", index);
507 eth_interface_list = g_list_remove(eth_interface_list,
508 GINT_TO_POINTER((int) index));
511 static void eth_tech_enable_tethering(struct connman_technology *technology,
515 struct ethernet_data *ethernet;
517 for (list = eth_interface_list; list; list = list->next) {
518 int index = GPOINTER_TO_INT(list->data);
519 struct connman_device *device =
520 connman_device_find_by_index(index);
523 ethernet = connman_device_get_data(device);
525 remove_network(device, ethernet);
528 connman_technology_tethering_notify(technology, true);
530 connman_inet_ifup(index);
532 connman_inet_add_to_bridge(index, bridge);
534 eth_tethering = true;
538 static void eth_tech_disable_tethering(struct connman_technology *technology,
543 for (list = eth_interface_list; list; list = list->next) {
544 int index = GPOINTER_TO_INT(list->data);
545 struct connman_device *device =
546 connman_device_find_by_index(index);
548 connman_inet_remove_from_bridge(index, bridge);
550 connman_technology_tethering_notify(technology, false);
553 connman_device_reconnect_service(device);
555 eth_tethering = false;
559 static int eth_tech_set_tethering(struct connman_technology *technology,
560 const char *identifier, const char *passphrase,
561 const char *bridge, bool enabled)
563 if (!connman_technology_is_tethering_allowed(
564 CONNMAN_SERVICE_TYPE_ETHERNET))
567 DBG("bridge %s enabled %d", bridge, enabled);
570 eth_tech_enable_tethering(technology, bridge);
572 eth_tech_disable_tethering(technology, bridge);
577 static struct connman_technology_driver eth_tech_driver = {
579 .type = CONNMAN_SERVICE_TYPE_ETHERNET,
580 .probe = eth_tech_probe,
581 .remove = eth_tech_remove,
582 .add_interface = eth_tech_add_interface,
583 .remove_interface = eth_tech_remove_interface,
584 .set_tethering = eth_tech_set_tethering,
587 #if defined TIZEN_EXT_WIFI_MESH
588 static int eth_mesh_add_to_bridge(const char *bridge)
591 struct ethernet_data *ethernet;
593 DBG("Add ethernet to bridge %s", bridge);
595 for (list = eth_interface_list; list; list = list->next) {
596 int index = GPOINTER_TO_INT(list->data);
597 struct connman_device *device =
598 connman_device_find_by_index(index);
601 ethernet = connman_device_get_data(device);
603 remove_network(device, ethernet);
606 connman_inet_ifup(index);
608 connman_inet_add_to_bridge(index, bridge);
614 static int eth_mesh_remove_from_bridge(const char *bridge)
618 DBG("Remove ethernet from bridge %s", bridge);
620 for (list = eth_interface_list; list; list = list->next) {
621 int index = GPOINTER_TO_INT(list->data);
623 connman_inet_remove_from_bridge(index, bridge);
629 static struct connman_mesh_eth_driver eth_mesh_driver = {
630 .add_to_bridge = eth_mesh_add_to_bridge,
631 .remove_from_bridge = eth_mesh_remove_from_bridge,
635 static int ethernet_init(void)
639 err = connman_technology_driver_register(ð_tech_driver);
643 #if defined TIZEN_EXT_WIFI_MESH
644 err = connman_mesh_eth_driver_register(ð_mesh_driver);
649 err = connman_network_driver_register(ð_network_driver);
653 err = connman_device_driver_register(ð_dev_driver);
655 connman_network_driver_unregister(ð_network_driver);
662 static void ethernet_exit(void)
664 connman_technology_driver_unregister(ð_tech_driver);
666 #if defined TIZEN_EXT_WIFI_MESH
667 connman_mesh_eth_driver_unregister(ð_mesh_driver);
670 connman_network_driver_unregister(ð_network_driver);
672 connman_device_driver_unregister(ð_dev_driver);
675 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
676 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)