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
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 bool eth_tethering = false;
46 struct ethernet_data {
50 struct connman_network *network;
53 static int eth_network_probe(struct connman_network *network)
55 DBG("network %p", network);
60 static void eth_network_remove(struct connman_network *network)
62 DBG("network %p", network);
65 static int eth_network_connect(struct connman_network *network)
67 DBG("network %p", network);
69 connman_network_set_connected(network, true);
74 static int eth_network_disconnect(struct connman_network *network)
76 DBG("network %p", network);
78 connman_network_set_connected(network, false);
83 static struct connman_network_driver eth_network_driver = {
85 .type = CONNMAN_NETWORK_TYPE_ETHERNET,
86 .probe = eth_network_probe,
87 .remove = eth_network_remove,
88 .connect = eth_network_connect,
89 .disconnect = eth_network_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);
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)
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 eth_dev_probe(struct connman_device *device)
168 struct ethernet_data *ethernet;
170 DBG("device %p", device);
172 ethernet = g_try_new0(struct ethernet_data, 1);
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 eth_dev_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 eth_dev_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 eth_dev_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 eth_dev_driver = {
222 .type = CONNMAN_DEVICE_TYPE_ETHERNET,
223 .probe = eth_dev_probe,
224 .remove = eth_dev_remove,
225 .enable = eth_dev_enable,
226 .disable = eth_dev_disable,
229 static int eth_tech_probe(struct connman_technology *technology)
234 static void eth_tech_remove(struct connman_technology *technology)
239 static GList *eth_interface_list = NULL;
241 static void eth_tech_add_interface(struct connman_technology *technology,
242 int index, const char *name, const char *ident)
244 DBG("index %d name %s ident %s", index, name, ident);
246 if (g_list_find(eth_interface_list, GINT_TO_POINTER((int)index)))
249 eth_interface_list = g_list_prepend(eth_interface_list,
250 (GINT_TO_POINTER((int) index)));
253 static void eth_tech_remove_interface(struct connman_technology *technology,
256 DBG("index %d", index);
258 eth_interface_list = g_list_remove(eth_interface_list,
259 GINT_TO_POINTER((int) index));
262 static void eth_tech_enable_tethering(struct connman_technology *technology,
266 struct ethernet_data *ethernet;
268 for (list = eth_interface_list; list; list = list->next) {
269 int index = GPOINTER_TO_INT(list->data);
270 struct connman_device *device =
271 connman_device_find_by_index(index);
274 ethernet = connman_device_get_data(device);
276 remove_network(device, ethernet);
279 connman_technology_tethering_notify(technology, true);
281 connman_inet_ifup(index);
283 connman_inet_add_to_bridge(index, bridge);
285 eth_tethering = true;
289 static void eth_tech_disable_tethering(struct connman_technology *technology,
294 for (list = eth_interface_list; list; list = list->next) {
295 int index = GPOINTER_TO_INT(list->data);
296 struct connman_device *device =
297 connman_device_find_by_index(index);
299 connman_inet_remove_from_bridge(index, bridge);
301 connman_technology_tethering_notify(technology, false);
304 connman_device_reconnect_service(device);
306 eth_tethering = false;
310 static int eth_tech_set_tethering(struct connman_technology *technology,
311 const char *identifier, const char *passphrase,
312 const char *bridge, bool enabled)
314 if (!connman_technology_is_tethering_allowed(
315 CONNMAN_SERVICE_TYPE_ETHERNET))
318 DBG("bridge %s enabled %d", bridge, enabled);
321 eth_tech_enable_tethering(technology, bridge);
323 eth_tech_disable_tethering(technology, bridge);
328 static struct connman_technology_driver eth_tech_driver = {
330 .type = CONNMAN_SERVICE_TYPE_ETHERNET,
331 .probe = eth_tech_probe,
332 .remove = eth_tech_remove,
333 .add_interface = eth_tech_add_interface,
334 .remove_interface = eth_tech_remove_interface,
335 .set_tethering = eth_tech_set_tethering,
338 static int ethernet_init(void)
342 err = connman_technology_driver_register(ð_tech_driver);
346 err = connman_network_driver_register(ð_network_driver);
350 err = connman_device_driver_register(ð_dev_driver);
352 connman_network_driver_unregister(ð_network_driver);
359 static void ethernet_exit(void)
361 connman_technology_driver_unregister(ð_tech_driver);
363 connman_network_driver_unregister(ð_network_driver);
365 connman_device_driver_unregister(ð_dev_driver);
368 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
369 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)