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 stpncpy(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 stpncpy(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 stpncpy(vifr.device1, ifname, sizeof(vifr.device1));
119 if(ioctl(sk, SIOCSIFVLAN, &vifr) >= 0)
120 stpncpy(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 #define NETCONFIG_SERVICE "net.netconfig"
155 #define NETCONFIG_ETHERNET_INTERFACE NETCONFIG_SERVICE ".ethernet"
156 #define NETCONFIG_ETHERNET_PATH "/net/netconfig/ethernet"
158 struct eapol_method_call_data {
159 DBusConnection *connection;
160 struct connman_network *network;
163 static struct eapol_method_call_data enable_eapol_data;
165 void handle_eap_signal(GSupplicantInterface *interface, bool status)
167 DBG("captured EAP signal");
169 if (!enable_eapol_data.network)
172 if (g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
175 if (!connman_network_check_validity(enable_eapol_data.network))
178 DBG("network is valid");
180 g_supplicant_unregister_eap_callback();
183 // Should we mark service as non favorite or make autoconnect as false?
185 struct ethernet_data *ethernet = g_supplicant_interface_get_data(interface);
186 if (ethernet && ethernet->interface) {
187 g_supplicant_interface_remove(ethernet->interface, NULL, NULL);
188 ethernet->interface = NULL;
191 connman_network_set_error(enable_eapol_data.network, CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
192 enable_eapol_data.network = NULL;
196 connman_network_set_connected(enable_eapol_data.network, status);
197 enable_eapol_data.network = NULL;
200 static void interface_create_callback(int result,
201 GSupplicantInterface *interface, void *user_data)
203 struct ethernet_data *ethernet = user_data;
205 if (result < 0 || !interface || !ethernet)
208 DBG("result %d ifname %s, ethernet %p", result,
209 g_supplicant_interface_get_ifname(interface),
212 ethernet->interface = interface;
213 g_supplicant_interface_set_data(interface, ethernet);
216 static int eapol_interface_create(void)
218 struct connman_network *network = enable_eapol_data.network;
219 struct connman_service *service = connman_service_lookup_from_network(network);
222 DBG("service not found");
226 struct connman_device *device = connman_network_get_device(network);
227 struct ethernet_data *ethernet = connman_device_get_data(device);
228 const char *driver = "wired";
229 int index = connman_network_get_index(network);
230 char *ifname = connman_inet_ifname(index);;
231 char *config_file = NULL;
233 g_supplicant_register_eap_callback(handle_eap_signal);
235 if (asprintf(&config_file, "/var/lib/connman/%s-eapol.conf", ifname) < 0) {
240 DBG("config_file %s", config_file);
242 g_supplicant_replace_config_file(ifname, config_file);
246 * TODO: RemoveInterface if already present because
247 * already created interface will not start EAP handshake.
249 return g_supplicant_interface_create(ifname, driver, NULL,
250 0, 0, 60, interface_create_callback, ethernet);
253 static void enable_eapol_reply(DBusPendingCall *call, void *user_data)
257 DBusMessageIter args;
261 reply = dbus_pending_call_steal_reply(call);
263 dbus_error_init(&error);
264 if (dbus_set_error_from_message(&error, reply)) {
265 DBG("enable_eapol_request() %s %s", error.name, error.message);
266 dbus_error_free(&error);
267 dbus_message_unref(reply);
268 dbus_pending_call_unref(call);
269 dbus_connection_unref(enable_eapol_data.connection);
271 enable_eapol_data.connection = NULL;
275 if (eapol_interface_create() < 0)
276 DBG("Failed to create eapol interface");
279 static int eth_network_enable_eapol(struct connman_service *service, struct connman_network *network)
281 DBusMessage *msg = NULL;
282 DBusPendingCall *call;
284 DBusConnection *connection = connman_dbus_get_connection();
286 DBG("dbus connection does not exist");
290 msg = dbus_message_new_method_call(NETCONFIG_SERVICE, NETCONFIG_ETHERNET_PATH,
291 NETCONFIG_ETHERNET_INTERFACE, "EnableEap");
293 dbus_connection_unref(connection);
297 const char *path = __connman_service_get_path(service);
298 dbus_bool_t enable = true;
300 dbus_message_append_args(msg, DBUS_TYPE_STRING, &path,
302 dbus_message_append_args(msg, DBUS_TYPE_BOOLEAN, &enable,
305 if (!dbus_connection_send_with_reply(connection, msg,
306 &call, DBUS_TIMEOUT_USE_DEFAULT)) {
307 dbus_message_unref(msg);
308 dbus_connection_unref(connection);
313 dbus_message_unref(msg);
314 dbus_connection_unref(connection);
318 enable_eapol_data.connection = connection;
319 enable_eapol_data.network = network;
321 dbus_pending_call_set_notify(call, enable_eapol_reply, NULL, NULL);
322 dbus_message_unref(msg);
327 static int eth_network_connect(struct connman_network *network)
329 DBG("network %p", network);
332 struct connman_service *service = connman_service_lookup_from_network(network);
334 if (service && __connman_service_get_use_eapol(service)) {
335 /** Enable eapol on device reboot **/
336 if (__connman_service_get_connect_reason(service) != CONNMAN_SERVICE_CONNECT_REASON_USER) {
337 err = eth_network_enable_eapol(service, network);
339 DBG("Failed to enable eapol");
343 err = eapol_interface_create();
345 DBG("Failed to create eapol interface");
353 connman_network_set_connected(network, true);
358 static int eth_network_disconnect(struct connman_network *network)
360 DBG("network %p", network);
362 struct connman_service *service = connman_service_lookup_from_network(network);
364 if (service && __connman_service_get_use_eapol(service)) {
365 struct connman_device *device = connman_network_get_device(network);
366 struct ethernet_data *ethernet = connman_device_get_data(device);
368 enable_eapol_data.network = NULL;
369 g_supplicant_unregister_eap_callback();
370 if (ethernet && ethernet->interface) {
371 g_supplicant_interface_remove(ethernet->interface, NULL, NULL);
372 ethernet->interface = NULL;
374 connman_network_set_associating(network, false);
375 connman_network_set_connected(network, false);
380 connman_network_set_connected(network, false);
385 #else /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
387 static int eth_network_connect(struct connman_network *network)
389 DBG("network %p", network);
391 connman_network_set_connected(network, true);
396 static int eth_network_disconnect(struct connman_network *network)
398 DBG("network %p", network);
400 connman_network_set_connected(network, false);
405 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
407 static struct connman_network_driver eth_network_driver = {
409 .type = CONNMAN_NETWORK_TYPE_ETHERNET,
410 .probe = eth_network_probe,
411 .remove = eth_network_remove,
412 .connect = eth_network_connect,
413 .disconnect = eth_network_disconnect,
416 static void add_network(struct connman_device *device,
417 struct ethernet_data *ethernet)
419 struct connman_network *network;
423 network = connman_network_create("carrier",
424 CONNMAN_NETWORK_TYPE_ETHERNET);
428 index = connman_device_get_index(device);
429 connman_network_set_index(network, index);
430 ifname = connman_inet_ifname(index);
434 connman_network_set_name(network, "Wired");
436 if (connman_device_add_network(device, network) < 0) {
437 connman_network_unref(network);
442 if (!eth_tethering) {
443 char group[25] = "cable";
446 vid = get_vlan_vid(ifname);
447 dsaport = get_dsa_port(ifname);
450 * Prevent service from starting the reconnect
451 * procedure as we do not want the DHCP client
452 * to run when tethering.
454 if((vid >= 0) && (dsaport >= 0))
455 snprintf(group, sizeof(group), "p%02x_%03x_cable", dsaport, vid);
457 snprintf(group, sizeof(group), "%03x_cable", vid);
458 else if (dsaport >= 0)
459 snprintf(group, sizeof(group), "p%02x_cable", dsaport);
461 connman_network_set_group(network, group);
464 ethernet->network = network;
468 static void remove_network(struct connman_device *device,
469 struct ethernet_data *ethernet)
471 if (!ethernet->network)
474 connman_device_remove_network(device, ethernet->network);
475 connman_network_unref(ethernet->network);
477 ethernet->network = NULL;
480 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
482 struct connman_device *device = user_data;
483 struct ethernet_data *ethernet = connman_device_get_data(device);
485 DBG("index %d flags %d change %d", ethernet->index, flags, change);
487 if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
488 if (flags & IFF_UP) {
490 connman_device_set_powered(device, true);
493 connman_device_set_powered(device, false);
497 if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
498 if (flags & IFF_LOWER_UP) {
500 add_network(device, ethernet);
503 remove_network(device, ethernet);
504 #if defined TIZEN_EXT_WIFI_MESH
505 /* Remove ethernet from mesh bridge */
506 __connman_mesh_remove_ethernet_from_bridge();
511 ethernet->flags = flags;
514 static int eth_dev_probe(struct connman_device *device)
516 struct ethernet_data *ethernet;
518 DBG("device %p", device);
520 ethernet = g_try_new0(struct ethernet_data, 1);
524 connman_device_set_data(device, ethernet);
526 ethernet->index = connman_device_get_index(device);
528 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
529 ethernet->interface = NULL;
530 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
532 ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
533 ethernet_newlink, device);
538 static void eth_dev_remove(struct connman_device *device)
540 struct ethernet_data *ethernet = connman_device_get_data(device);
542 DBG("device %p", device);
544 connman_device_set_data(device, NULL);
546 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
550 if (ethernet->interface) {
551 g_supplicant_interface_remove(ethernet->interface, NULL, NULL);
552 ethernet->interface = NULL;
554 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
556 connman_rtnl_remove_watch(ethernet->watch);
558 remove_network(device, ethernet);
563 static int eth_dev_enable(struct connman_device *device)
565 struct ethernet_data *ethernet = connman_device_get_data(device);
567 DBG("device %p", device);
569 return connman_inet_ifup(ethernet->index);
572 static int eth_dev_disable(struct connman_device *device)
574 struct ethernet_data *ethernet = connman_device_get_data(device);
576 DBG("device %p", device);
578 return connman_inet_ifdown(ethernet->index);
581 static struct connman_device_driver eth_dev_driver = {
583 .type = CONNMAN_DEVICE_TYPE_ETHERNET,
584 .probe = eth_dev_probe,
585 .remove = eth_dev_remove,
586 .enable = eth_dev_enable,
587 .disable = eth_dev_disable,
590 static int eth_tech_probe(struct connman_technology *technology)
595 static void eth_tech_remove(struct connman_technology *technology)
600 static GList *eth_interface_list = NULL;
602 static void eth_tech_add_interface(struct connman_technology *technology,
603 int index, const char *name, const char *ident)
605 DBG("index %d name %s ident %s", index, name, ident);
607 if (g_list_find(eth_interface_list, GINT_TO_POINTER((int)index)))
610 eth_interface_list = g_list_prepend(eth_interface_list,
611 (GINT_TO_POINTER((int) index)));
614 static void eth_tech_remove_interface(struct connman_technology *technology,
617 DBG("index %d", index);
619 eth_interface_list = g_list_remove(eth_interface_list,
620 GINT_TO_POINTER((int) index));
623 static void eth_tech_enable_tethering(struct connman_technology *technology,
627 struct ethernet_data *ethernet;
629 for (list = eth_interface_list; list; list = list->next) {
630 int index = GPOINTER_TO_INT(list->data);
631 struct connman_device *device =
632 connman_device_find_by_index(index);
635 ethernet = connman_device_get_data(device);
637 remove_network(device, ethernet);
640 connman_technology_tethering_notify(technology, true);
642 connman_inet_ifup(index);
644 connman_inet_add_to_bridge(index, bridge);
646 eth_tethering = true;
650 static void eth_tech_disable_tethering(struct connman_technology *technology,
655 for (list = eth_interface_list; list; list = list->next) {
656 int index = GPOINTER_TO_INT(list->data);
657 struct connman_device *device =
658 connman_device_find_by_index(index);
660 connman_inet_remove_from_bridge(index, bridge);
662 connman_technology_tethering_notify(technology, false);
665 connman_device_reconnect_service(device);
667 eth_tethering = false;
671 static int eth_tech_set_tethering(struct connman_technology *technology,
672 const char *identifier, const char *passphrase,
673 const char *bridge, bool enabled)
675 if (!connman_technology_is_tethering_allowed(
676 CONNMAN_SERVICE_TYPE_ETHERNET))
679 DBG("bridge %s enabled %d", bridge, enabled);
682 eth_tech_enable_tethering(technology, bridge);
684 eth_tech_disable_tethering(technology, bridge);
689 static struct connman_technology_driver eth_tech_driver = {
691 .type = CONNMAN_SERVICE_TYPE_ETHERNET,
692 .probe = eth_tech_probe,
693 .remove = eth_tech_remove,
694 .add_interface = eth_tech_add_interface,
695 .remove_interface = eth_tech_remove_interface,
696 .set_tethering = eth_tech_set_tethering,
699 #if defined TIZEN_EXT_WIFI_MESH
700 static int eth_mesh_add_to_bridge(const char *bridge)
703 struct ethernet_data *ethernet;
705 DBG("Add ethernet to bridge %s", bridge);
707 for (list = eth_interface_list; list; list = list->next) {
708 int index = GPOINTER_TO_INT(list->data);
709 struct connman_device *device =
710 connman_device_find_by_index(index);
713 ethernet = connman_device_get_data(device);
715 remove_network(device, ethernet);
718 connman_inet_ifup(index);
720 connman_inet_add_to_bridge(index, bridge);
726 static int eth_mesh_remove_from_bridge(const char *bridge)
730 DBG("Remove ethernet from bridge %s", bridge);
732 for (list = eth_interface_list; list; list = list->next) {
733 int index = GPOINTER_TO_INT(list->data);
735 connman_inet_remove_from_bridge(index, bridge);
741 static struct connman_mesh_eth_driver eth_mesh_driver = {
742 .add_to_bridge = eth_mesh_add_to_bridge,
743 .remove_from_bridge = eth_mesh_remove_from_bridge,
747 static int ethernet_init(void)
751 err = connman_technology_driver_register(ð_tech_driver);
755 #if defined TIZEN_EXT_WIFI_MESH
756 err = connman_mesh_eth_driver_register(ð_mesh_driver);
761 err = connman_network_driver_register(ð_network_driver);
765 err = connman_device_driver_register(ð_dev_driver);
767 connman_network_driver_unregister(ð_network_driver);
774 static void ethernet_exit(void)
776 connman_technology_driver_unregister(ð_tech_driver);
778 #if defined TIZEN_EXT_WIFI_MESH
779 connman_mesh_eth_driver_unregister(ð_mesh_driver);
782 connman_network_driver_unregister(ð_network_driver);
784 connman_device_driver_unregister(ð_dev_driver);
787 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
788 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)