5 * Copyright (C) 2007-2012 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
30 #define IFF_LOWER_UP 0x10000
35 #define CONNMAN_API_SUBJECT_TO_CHANGE
36 #include <connman/technology.h>
37 #include <connman/plugin.h>
38 #include <connman/device.h>
39 #include <connman/inet.h>
40 #include <connman/rtnl.h>
41 #include <connman/log.h>
42 #include <connman/setting.h>
44 static connman_bool_t eth_tethering = FALSE;
46 struct ethernet_data {
50 struct connman_network *network;
53 static int cable_probe(struct connman_network *network)
55 DBG("network %p", network);
60 static void cable_remove(struct connman_network *network)
62 DBG("network %p", network);
65 static int cable_connect(struct connman_network *network)
67 DBG("network %p", network);
69 connman_network_set_connected(network, TRUE);
74 static int cable_disconnect(struct connman_network *network)
76 DBG("network %p", network);
78 connman_network_set_connected(network, FALSE);
83 static struct connman_network_driver cable_driver = {
85 .type = CONNMAN_NETWORK_TYPE_ETHERNET,
87 .remove = cable_remove,
88 .connect = cable_connect,
89 .disconnect = cable_disconnect,
92 static void add_network(struct connman_device *device,
93 struct ethernet_data *ethernet)
95 struct connman_network *network;
98 network = connman_network_create("carrier",
99 CONNMAN_NETWORK_TYPE_ETHERNET);
103 index = connman_device_get_index(device);
104 connman_network_set_index(network, index);
106 connman_network_set_name(network, "Wired");
108 if (connman_device_add_network(device, network) < 0) {
109 connman_network_unref(network);
113 if (eth_tethering == FALSE)
115 * Prevent service from starting the reconnect
116 * procedure as we do not want the DHCP client
117 * to run when tethering.
119 connman_network_set_group(network, "cable");
121 ethernet->network = network;
124 static void remove_network(struct connman_device *device,
125 struct ethernet_data *ethernet)
127 if (ethernet->network == NULL)
130 connman_device_remove_network(device, ethernet->network);
131 connman_network_unref(ethernet->network);
133 ethernet->network = NULL;
136 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
138 struct connman_device *device = user_data;
139 struct ethernet_data *ethernet = connman_device_get_data(device);
141 DBG("index %d flags %d change %d", ethernet->index, flags, change);
143 if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
144 if (flags & IFF_UP) {
146 connman_device_set_powered(device, TRUE);
149 connman_device_set_powered(device, FALSE);
153 if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
154 if (flags & IFF_LOWER_UP) {
156 add_network(device, ethernet);
159 remove_network(device, ethernet);
163 ethernet->flags = flags;
166 static int ethernet_probe(struct connman_device *device)
168 struct ethernet_data *ethernet;
170 DBG("device %p", device);
172 ethernet = g_try_new0(struct ethernet_data, 1);
173 if (ethernet == NULL)
176 connman_device_set_data(device, ethernet);
178 ethernet->index = connman_device_get_index(device);
181 ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
182 ethernet_newlink, device);
187 static void ethernet_remove(struct connman_device *device)
189 struct ethernet_data *ethernet = connman_device_get_data(device);
191 DBG("device %p", device);
193 connman_device_set_data(device, NULL);
195 connman_rtnl_remove_watch(ethernet->watch);
197 remove_network(device, ethernet);
202 static int ethernet_enable(struct connman_device *device)
204 struct ethernet_data *ethernet = connman_device_get_data(device);
206 DBG("device %p", device);
208 return connman_inet_ifup(ethernet->index);
211 static int ethernet_disable(struct connman_device *device)
213 struct ethernet_data *ethernet = connman_device_get_data(device);
215 DBG("device %p", device);
217 return connman_inet_ifdown(ethernet->index);
220 static struct connman_device_driver ethernet_driver = {
222 .type = CONNMAN_DEVICE_TYPE_ETHERNET,
223 .probe = ethernet_probe,
224 .remove = ethernet_remove,
225 .enable = ethernet_enable,
226 .disable = ethernet_disable,
229 static GList *cdc_interface_list = NULL;
231 static void tech_add_interface(struct connman_technology *technology,
232 int index, const char *name, const char *ident)
234 DBG("index %d name %s ident %s", index, name, ident);
236 if (g_list_find(cdc_interface_list,
237 GINT_TO_POINTER((int) index)) != NULL)
240 cdc_interface_list = g_list_prepend(cdc_interface_list,
241 (GINT_TO_POINTER((int) index)));
244 static void tech_remove_interface(struct connman_technology *technology,
247 DBG("index %d", index);
249 cdc_interface_list = g_list_remove(cdc_interface_list,
250 GINT_TO_POINTER((int) index));
253 static void enable_tethering(struct connman_technology *technology,
258 for (list = cdc_interface_list; list; list = list->next) {
259 int index = GPOINTER_TO_INT(list->data);
261 connman_technology_tethering_notify(technology, TRUE);
263 connman_inet_ifup(index);
265 connman_inet_add_to_bridge(index, bridge);
269 static void disable_tethering(struct connman_technology *technology,
274 for (list = cdc_interface_list; list; list = list->next) {
275 int index = GPOINTER_TO_INT(list->data);
277 connman_inet_remove_from_bridge(index, bridge);
279 connman_inet_ifdown(index);
281 connman_technology_tethering_notify(technology, FALSE);
285 static int tech_set_tethering(struct connman_technology *technology,
286 const char *identifier, const char *passphrase,
287 const char *bridge, connman_bool_t enabled)
289 DBG("bridge %s enabled %d", bridge, enabled);
292 enable_tethering(technology, bridge);
294 disable_tethering(technology, bridge);
299 static int tech_probe(struct connman_technology *technology)
304 static void tech_remove(struct connman_technology *technology)
306 g_list_free(cdc_interface_list);
308 cdc_interface_list = NULL;
311 static struct connman_technology_driver tech_driver = {
312 .name = "cdc_ethernet",
313 .type = CONNMAN_SERVICE_TYPE_GADGET,
315 .remove = tech_remove,
316 .add_interface = tech_add_interface,
317 .remove_interface = tech_remove_interface,
318 .set_tethering = tech_set_tethering,
321 static int eth_probe(struct connman_technology *technology)
326 static void eth_remove(struct connman_technology *technology)
331 static GList *eth_interface_list = NULL;
333 static void eth_add_interface(struct connman_technology *technology,
334 int index, const char *name, const char *ident)
336 DBG("index %d name %s ident %s", index, name, ident);
338 if (g_list_find(eth_interface_list,
339 GINT_TO_POINTER((int) index)) != NULL)
342 eth_interface_list = g_list_prepend(eth_interface_list,
343 (GINT_TO_POINTER((int) index)));
346 static void eth_remove_interface(struct connman_technology *technology,
349 DBG("index %d", index);
351 eth_interface_list = g_list_remove(eth_interface_list,
352 GINT_TO_POINTER((int) index));
355 static void eth_enable_tethering(struct connman_technology *technology,
360 for (list = eth_interface_list; list; list = list->next) {
361 int index = GPOINTER_TO_INT(list->data);
362 struct connman_device *device =
363 connman_device_find_by_index(index);
366 connman_device_disconnect_service(device);
368 connman_technology_tethering_notify(technology, TRUE);
370 connman_inet_ifup(index);
372 connman_inet_add_to_bridge(index, bridge);
374 eth_tethering = TRUE;
378 static void eth_disable_tethering(struct connman_technology *technology,
383 for (list = eth_interface_list; list; list = list->next) {
384 int index = GPOINTER_TO_INT(list->data);
385 struct connman_device *device =
386 connman_device_find_by_index(index);
388 connman_inet_remove_from_bridge(index, bridge);
390 connman_inet_ifdown(index);
392 connman_technology_tethering_notify(technology, FALSE);
395 connman_device_reconnect_service(device);
397 eth_tethering = FALSE;
401 static int eth_set_tethering(struct connman_technology *technology,
402 const char *identifier, const char *passphrase,
403 const char *bridge, connman_bool_t enabled)
405 if (connman_technology_is_tethering_allowed(
406 CONNMAN_SERVICE_TYPE_ETHERNET) == FALSE)
409 DBG("bridge %s enabled %d", bridge, enabled);
412 eth_enable_tethering(technology, bridge);
414 eth_disable_tethering(technology, bridge);
419 static struct connman_technology_driver eth_driver = {
421 .type = CONNMAN_SERVICE_TYPE_ETHERNET,
423 .remove = eth_remove,
424 .add_interface = eth_add_interface,
425 .remove_interface = eth_remove_interface,
426 .set_tethering = eth_set_tethering,
429 static int ethernet_init(void)
433 err = connman_technology_driver_register(ð_driver);
437 err = connman_network_driver_register(&cable_driver);
441 err = connman_device_driver_register(ðernet_driver);
443 connman_network_driver_unregister(&cable_driver);
447 err = connman_technology_driver_register(&tech_driver);
449 connman_device_driver_unregister(ðernet_driver);
450 connman_network_driver_unregister(&cable_driver);
457 static void ethernet_exit(void)
459 connman_technology_driver_unregister(ð_driver);
461 connman_technology_driver_unregister(&tech_driver);
463 connman_network_driver_unregister(&cable_driver);
465 connman_device_driver_unregister(ðernet_driver);
468 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
469 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)