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 <gsupplicant/gsupplicant.h>
58 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
65 static bool eth_tethering = false;
67 struct ethernet_data {
71 struct connman_network *network;
72 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
73 GSupplicantInterface *interface;
74 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
78 static int get_vlan_vid(const char *ifname)
80 struct vlan_ioctl_args vifr;
84 memset(&vifr, 0, sizeof(vifr));
86 sk = socket(AF_INET, SOCK_STREAM, 0);
90 vifr.cmd = GET_VLAN_VID_CMD;
91 stpncpy(vifr.device1, ifname, sizeof(vifr.device1) - 1);
93 if(ioctl(sk, SIOCSIFVLAN, &vifr) >= 0)
103 static int get_dsa_port(const char *ifname)
108 struct ethtool_cmd cmd;
109 struct ethtool_drvinfo drvinfocmd;
110 struct vlan_ioctl_args vifr;
112 sk = socket(AF_INET, SOCK_STREAM, 0);
116 memset(&ifr, 0, sizeof(ifr));
117 stpncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
119 /* check if it is a vlan and get physical interface name*/
120 vifr.cmd = GET_VLAN_REALDEV_NAME_CMD;
121 stpncpy(vifr.device1, ifname, sizeof(vifr.device1) - 1);
123 if(ioctl(sk, SIOCSIFVLAN, &vifr) >= 0) {
124 stpncpy(ifr.ifr_name, vifr.u.device2, sizeof(ifr.ifr_name) - 1);
125 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
128 /* get driver info */
129 drvinfocmd.cmd = ETHTOOL_GDRVINFO;
130 ifr.ifr_data = (caddr_t)&drvinfocmd;
132 if (!ioctl(sk, SIOCETHTOOL, &ifr)) {
133 if(!strcmp(drvinfocmd.driver, "dsa")) {
135 cmd.cmd = ETHTOOL_GSET;
136 ifr.ifr_data = (caddr_t)&cmd;
138 if (!ioctl(sk, SIOCETHTOOL, &ifr))
139 dsaport = cmd.phy_address;
147 static int eth_network_probe(struct connman_network *network)
149 DBG("network %p", network);
154 static void eth_network_remove(struct connman_network *network)
156 DBG("network %p", network);
159 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
160 #define NETCONFIG_SERVICE "net.netconfig"
161 #define NETCONFIG_ETHERNET_INTERFACE NETCONFIG_SERVICE ".ethernet"
162 #define NETCONFIG_ETHERNET_PATH "/net/netconfig/ethernet"
164 struct eapol_method_call_data {
165 DBusConnection *connection;
166 struct connman_network *network;
169 static struct eapol_method_call_data enable_eapol_data;
171 void handle_eap_signal(GSupplicantInterface *interface, bool status)
173 DBG("captured EAP signal");
175 if (!enable_eapol_data.network)
178 if (g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
181 if (!connman_network_check_validity(enable_eapol_data.network))
184 DBG("network is valid");
186 g_supplicant_unregister_eap_callback();
189 // Should we mark service as non favorite or make autoconnect as false?
191 struct ethernet_data *ethernet = g_supplicant_interface_get_data(interface);
192 if (ethernet && ethernet->interface) {
193 g_supplicant_interface_remove(ethernet->interface, NULL, NULL);
194 ethernet->interface = NULL;
197 connman_network_set_error(enable_eapol_data.network, CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
198 enable_eapol_data.network = NULL;
202 connman_network_set_connected(enable_eapol_data.network, status);
203 enable_eapol_data.network = NULL;
206 static void interface_create_callback(int result,
207 GSupplicantInterface *interface, void *user_data)
209 struct ethernet_data *ethernet = user_data;
211 if (result < 0 || !interface || !ethernet)
214 DBG("result %d ifname %s, ethernet %p", result,
215 g_supplicant_interface_get_ifname(interface),
218 ethernet->interface = interface;
219 g_supplicant_interface_set_data(interface, ethernet);
222 static int eapol_interface_create(void)
224 struct connman_network *network = enable_eapol_data.network;
225 struct connman_service *service = connman_service_lookup_from_network(network);
228 DBG("service not found");
232 struct connman_device *device = connman_network_get_device(network);
233 struct ethernet_data *ethernet = connman_device_get_data(device);
234 const char *driver = "wired";
235 int index = connman_network_get_index(network);
236 char *ifname = connman_inet_ifname(index);;
237 char *config_file = NULL;
239 g_supplicant_register_eap_callback(handle_eap_signal);
241 if (asprintf(&config_file, "/var/lib/connman/%s-eapol.conf", ifname) < 0) {
246 DBG("config_file %s", config_file);
248 g_supplicant_replace_config_file(ifname, config_file);
252 * TODO: RemoveInterface if already present because
253 * already created interface will not start EAP handshake.
255 return g_supplicant_interface_create(ifname, driver, NULL,
256 0, 0, 60, interface_create_callback, ethernet);
259 static void enable_eapol_reply(DBusPendingCall *call, void *user_data)
266 reply = dbus_pending_call_steal_reply(call);
268 dbus_error_init(&error);
269 if (dbus_set_error_from_message(&error, reply)) {
270 DBG("enable_eapol_request() %s %s", error.name, error.message);
271 dbus_error_free(&error);
272 dbus_message_unref(reply);
273 dbus_pending_call_unref(call);
274 dbus_connection_unref(enable_eapol_data.connection);
276 enable_eapol_data.connection = NULL;
280 if (eapol_interface_create() < 0)
281 DBG("Failed to create eapol interface");
284 static int eth_network_enable_eapol(struct connman_service *service, struct connman_network *network)
286 DBusMessage *msg = NULL;
287 DBusPendingCall *call;
289 DBusConnection *connection = connman_dbus_get_connection();
291 DBG("dbus connection does not exist");
295 msg = dbus_message_new_method_call(NETCONFIG_SERVICE, NETCONFIG_ETHERNET_PATH,
296 NETCONFIG_ETHERNET_INTERFACE, "EnableEap");
298 dbus_connection_unref(connection);
302 const char *path = __connman_service_get_path(service);
303 dbus_bool_t enable = true;
305 dbus_message_append_args(msg, DBUS_TYPE_STRING, &path,
307 dbus_message_append_args(msg, DBUS_TYPE_BOOLEAN, &enable,
310 if (!dbus_connection_send_with_reply(connection, msg,
311 &call, DBUS_TIMEOUT_USE_DEFAULT)) {
312 dbus_message_unref(msg);
313 dbus_connection_unref(connection);
318 dbus_message_unref(msg);
319 dbus_connection_unref(connection);
323 enable_eapol_data.connection = connection;
324 enable_eapol_data.network = network;
326 dbus_pending_call_set_notify(call, enable_eapol_reply, NULL, NULL);
327 dbus_message_unref(msg);
332 static int eth_network_connect(struct connman_network *network)
334 DBG("network %p", network);
337 struct connman_service *service = connman_service_lookup_from_network(network);
339 if (service && __connman_service_get_use_eapol(service)) {
340 /** Enable eapol on device reboot **/
341 if (__connman_service_get_connect_reason(service) != CONNMAN_SERVICE_CONNECT_REASON_USER) {
342 err = eth_network_enable_eapol(service, network);
344 DBG("Failed to enable eapol");
348 err = eapol_interface_create();
350 DBG("Failed to create eapol interface");
358 connman_network_set_connected(network, true);
363 static int eth_network_disconnect(struct connman_network *network)
365 DBG("network %p", network);
367 struct connman_service *service = connman_service_lookup_from_network(network);
369 if (service && __connman_service_get_use_eapol(service)) {
370 struct connman_device *device = connman_network_get_device(network);
371 struct ethernet_data *ethernet = connman_device_get_data(device);
373 enable_eapol_data.network = NULL;
374 g_supplicant_unregister_eap_callback();
375 if (ethernet && ethernet->interface) {
376 g_supplicant_interface_remove(ethernet->interface, NULL, NULL);
377 ethernet->interface = NULL;
379 connman_network_set_associating(network, false);
380 connman_network_set_connected(network, false);
385 connman_network_set_connected(network, false);
390 #else /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
392 static int eth_network_connect(struct connman_network *network)
394 DBG("network %p", network);
396 connman_network_set_connected(network, true);
401 static int eth_network_disconnect(struct connman_network *network)
403 DBG("network %p", network);
405 connman_network_set_connected(network, false);
410 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
412 static struct connman_network_driver eth_network_driver = {
414 .type = CONNMAN_NETWORK_TYPE_ETHERNET,
415 .probe = eth_network_probe,
416 .remove = eth_network_remove,
417 .connect = eth_network_connect,
418 .disconnect = eth_network_disconnect,
421 static void add_network(struct connman_device *device,
422 struct ethernet_data *ethernet)
424 struct connman_network *network;
428 network = connman_network_create("carrier",
429 CONNMAN_NETWORK_TYPE_ETHERNET);
433 index = connman_device_get_index(device);
434 connman_network_set_index(network, index);
435 ifname = connman_inet_ifname(index);
439 connman_network_set_name(network, "Wired");
441 if (connman_device_add_network(device, network) < 0) {
442 connman_network_unref(network);
447 if (!eth_tethering) {
448 char group[25] = "cable";
451 vid = get_vlan_vid(ifname);
452 dsaport = get_dsa_port(ifname);
455 * Prevent service from starting the reconnect
456 * procedure as we do not want the DHCP client
457 * to run when tethering.
459 if((vid >= 0) && (dsaport >= 0))
460 snprintf(group, sizeof(group), "p%02x_%03x_cable", dsaport, vid);
462 snprintf(group, sizeof(group), "%03x_cable", vid);
463 else if (dsaport >= 0)
464 snprintf(group, sizeof(group), "p%02x_cable", dsaport);
466 connman_network_set_group(network, group);
469 ethernet->network = network;
473 static void remove_network(struct connman_device *device,
474 struct ethernet_data *ethernet)
476 if (!ethernet->network)
479 connman_device_remove_network(device, ethernet->network);
480 connman_network_unref(ethernet->network);
482 ethernet->network = NULL;
485 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
487 struct connman_device *device = user_data;
488 struct ethernet_data *ethernet = connman_device_get_data(device);
490 DBG("index %d flags %d change %d", ethernet->index, flags, change);
492 if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
493 if (flags & IFF_UP) {
495 connman_device_set_powered(device, true);
498 connman_device_set_powered(device, false);
502 if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
503 if (flags & IFF_LOWER_UP) {
505 add_network(device, ethernet);
508 remove_network(device, ethernet);
509 #if defined TIZEN_EXT_WIFI_MESH
510 /* Remove ethernet from mesh bridge */
511 __connman_mesh_remove_ethernet_from_bridge();
516 ethernet->flags = flags;
519 static int eth_dev_probe(struct connman_device *device)
521 struct ethernet_data *ethernet;
523 DBG("device %p", device);
525 ethernet = g_try_new0(struct ethernet_data, 1);
529 connman_device_set_data(device, ethernet);
531 ethernet->index = connman_device_get_index(device);
533 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
534 ethernet->interface = NULL;
535 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
537 ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
538 ethernet_newlink, device);
543 static void eth_dev_remove(struct connman_device *device)
545 struct ethernet_data *ethernet = connman_device_get_data(device);
547 DBG("device %p", device);
549 connman_device_set_data(device, NULL);
551 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
555 if (ethernet->interface) {
556 g_supplicant_interface_remove(ethernet->interface, NULL, NULL);
557 ethernet->interface = NULL;
559 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
561 connman_rtnl_remove_watch(ethernet->watch);
563 remove_network(device, ethernet);
568 static int eth_dev_enable(struct connman_device *device)
570 struct ethernet_data *ethernet = connman_device_get_data(device);
572 DBG("device %p", device);
574 return connman_inet_ifup(ethernet->index);
577 static int eth_dev_disable(struct connman_device *device)
579 struct ethernet_data *ethernet = connman_device_get_data(device);
581 DBG("device %p", device);
583 return connman_inet_ifdown(ethernet->index);
586 static struct connman_device_driver eth_dev_driver = {
588 .type = CONNMAN_DEVICE_TYPE_ETHERNET,
589 .probe = eth_dev_probe,
590 .remove = eth_dev_remove,
591 .enable = eth_dev_enable,
592 .disable = eth_dev_disable,
595 static int eth_tech_probe(struct connman_technology *technology)
600 static void eth_tech_remove(struct connman_technology *technology)
605 static GList *eth_interface_list = NULL;
607 static void eth_tech_add_interface(struct connman_technology *technology,
608 int index, const char *name, const char *ident)
610 DBG("index %d name %s ident %s", index, name, ident);
612 if (g_list_find(eth_interface_list, GINT_TO_POINTER((int)index)))
615 eth_interface_list = g_list_prepend(eth_interface_list,
616 (GINT_TO_POINTER((int) index)));
619 static void eth_tech_remove_interface(struct connman_technology *technology,
622 DBG("index %d", index);
624 eth_interface_list = g_list_remove(eth_interface_list,
625 GINT_TO_POINTER((int) index));
628 static void eth_tech_enable_tethering(struct connman_technology *technology,
632 struct ethernet_data *ethernet;
634 for (list = eth_interface_list; list; list = list->next) {
635 int index = GPOINTER_TO_INT(list->data);
636 struct connman_device *device =
637 connman_device_find_by_index(index);
640 ethernet = connman_device_get_data(device);
642 remove_network(device, ethernet);
645 connman_technology_tethering_notify(technology, true);
647 connman_inet_ifup(index);
649 connman_inet_add_to_bridge(index, bridge);
651 eth_tethering = true;
655 static void eth_tech_disable_tethering(struct connman_technology *technology,
660 for (list = eth_interface_list; list; list = list->next) {
661 int index = GPOINTER_TO_INT(list->data);
662 struct connman_device *device =
663 connman_device_find_by_index(index);
665 connman_inet_remove_from_bridge(index, bridge);
667 connman_technology_tethering_notify(technology, false);
670 connman_device_reconnect_service(device);
672 eth_tethering = false;
676 static int eth_tech_set_tethering(struct connman_technology *technology,
677 const char *bridge, bool enabled)
679 if (!connman_technology_is_tethering_allowed(
680 CONNMAN_SERVICE_TYPE_ETHERNET))
683 DBG("bridge %s enabled %d", bridge, enabled);
686 eth_tech_enable_tethering(technology, bridge);
688 eth_tech_disable_tethering(technology, bridge);
693 static struct connman_technology_driver eth_tech_driver = {
695 .type = CONNMAN_SERVICE_TYPE_ETHERNET,
696 .probe = eth_tech_probe,
697 .remove = eth_tech_remove,
698 .add_interface = eth_tech_add_interface,
699 .remove_interface = eth_tech_remove_interface,
700 .set_tethering = eth_tech_set_tethering,
703 #if defined TIZEN_EXT_WIFI_MESH
704 static int eth_mesh_add_to_bridge(const char *bridge)
707 struct ethernet_data *ethernet;
709 DBG("Add ethernet to bridge %s", bridge);
711 for (list = eth_interface_list; list; list = list->next) {
712 int index = GPOINTER_TO_INT(list->data);
713 struct connman_device *device =
714 connman_device_find_by_index(index);
717 ethernet = connman_device_get_data(device);
719 remove_network(device, ethernet);
722 connman_inet_ifup(index);
724 connman_inet_add_to_bridge(index, bridge);
730 static int eth_mesh_remove_from_bridge(const char *bridge)
734 DBG("Remove ethernet from bridge %s", bridge);
736 for (list = eth_interface_list; list; list = list->next) {
737 int index = GPOINTER_TO_INT(list->data);
739 connman_inet_remove_from_bridge(index, bridge);
745 static struct connman_mesh_eth_driver eth_mesh_driver = {
746 .add_to_bridge = eth_mesh_add_to_bridge,
747 .remove_from_bridge = eth_mesh_remove_from_bridge,
751 static int ethernet_init(void)
755 err = connman_technology_driver_register(ð_tech_driver);
759 #if defined TIZEN_EXT_WIFI_MESH
760 err = connman_mesh_eth_driver_register(ð_mesh_driver);
765 err = connman_network_driver_register(ð_network_driver);
769 err = connman_device_driver_register(ð_dev_driver);
771 connman_network_driver_unregister(ð_network_driver);
778 static void ethernet_exit(void)
780 connman_technology_driver_unregister(ð_tech_driver);
782 #if defined TIZEN_EXT_WIFI_MESH
783 connman_mesh_eth_driver_unregister(ð_mesh_driver);
786 connman_network_driver_unregister(ð_network_driver);
788 connman_device_driver_unregister(ð_dev_driver);
791 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
792 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)