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>
33 #include <linux/if_vlan.h>
34 #include <linux/sockios.h>
37 #define IFF_LOWER_UP 0x10000
42 #define CONNMAN_API_SUBJECT_TO_CHANGE
43 #include <connman/technology.h>
44 #include <connman/plugin.h>
45 #include <connman/device.h>
46 #include <connman/inet.h>
47 #include <connman/rtnl.h>
48 #include <connman/log.h>
49 #include <connman/setting.h>
51 static bool eth_tethering = false;
53 struct ethernet_data {
57 struct connman_network *network;
61 static int get_vlan_vid(const char *ifname)
63 struct vlan_ioctl_args vifr;
67 memset(&vifr, 0, sizeof(vifr));
69 sk = socket(AF_INET, SOCK_STREAM, 0);
73 vifr.cmd = GET_VLAN_VID_CMD;
74 strncpy(vifr.device1, ifname, sizeof(vifr.device1));
76 if(ioctl(sk, SIOCSIFVLAN, &vifr) >= 0)
86 static int eth_network_probe(struct connman_network *network)
88 DBG("network %p", network);
93 static void eth_network_remove(struct connman_network *network)
95 DBG("network %p", network);
98 static int eth_network_connect(struct connman_network *network)
100 DBG("network %p", network);
102 connman_network_set_connected(network, true);
107 static int eth_network_disconnect(struct connman_network *network)
109 DBG("network %p", network);
111 connman_network_set_connected(network, false);
116 static struct connman_network_driver eth_network_driver = {
118 .type = CONNMAN_NETWORK_TYPE_ETHERNET,
119 .probe = eth_network_probe,
120 .remove = eth_network_remove,
121 .connect = eth_network_connect,
122 .disconnect = eth_network_disconnect,
125 static void add_network(struct connman_device *device,
126 struct ethernet_data *ethernet)
128 struct connman_network *network;
132 network = connman_network_create("carrier",
133 CONNMAN_NETWORK_TYPE_ETHERNET);
137 index = connman_device_get_index(device);
138 connman_network_set_index(network, index);
139 ifname = connman_inet_ifname(index);
142 vid = get_vlan_vid(ifname);
144 connman_network_set_name(network, "Wired");
146 if (connman_device_add_network(device, network) < 0) {
147 connman_network_unref(network);
151 if (!eth_tethering) {
152 char group[10] = "cable";
154 * Prevent service from starting the reconnect
155 * procedure as we do not want the DHCP client
156 * to run when tethering.
159 snprintf(group, sizeof(group), "%03x_cable", vid);
161 connman_network_set_group(network, group);
164 ethernet->network = network;
168 static void remove_network(struct connman_device *device,
169 struct ethernet_data *ethernet)
171 if (!ethernet->network)
174 connman_device_remove_network(device, ethernet->network);
175 connman_network_unref(ethernet->network);
177 ethernet->network = NULL;
180 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
182 struct connman_device *device = user_data;
183 struct ethernet_data *ethernet = connman_device_get_data(device);
185 DBG("index %d flags %d change %d", ethernet->index, flags, change);
187 if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
188 if (flags & IFF_UP) {
190 connman_device_set_powered(device, true);
193 connman_device_set_powered(device, false);
197 if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
198 if (flags & IFF_LOWER_UP) {
200 add_network(device, ethernet);
203 remove_network(device, ethernet);
207 ethernet->flags = flags;
210 static int eth_dev_probe(struct connman_device *device)
212 struct ethernet_data *ethernet;
214 DBG("device %p", device);
216 ethernet = g_try_new0(struct ethernet_data, 1);
220 connman_device_set_data(device, ethernet);
222 ethernet->index = connman_device_get_index(device);
225 ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
226 ethernet_newlink, device);
231 static void eth_dev_remove(struct connman_device *device)
233 struct ethernet_data *ethernet = connman_device_get_data(device);
235 DBG("device %p", device);
237 connman_device_set_data(device, NULL);
239 connman_rtnl_remove_watch(ethernet->watch);
241 remove_network(device, ethernet);
246 static int eth_dev_enable(struct connman_device *device)
248 struct ethernet_data *ethernet = connman_device_get_data(device);
250 DBG("device %p", device);
252 return connman_inet_ifup(ethernet->index);
255 static int eth_dev_disable(struct connman_device *device)
257 struct ethernet_data *ethernet = connman_device_get_data(device);
259 DBG("device %p", device);
261 return connman_inet_ifdown(ethernet->index);
264 static struct connman_device_driver eth_dev_driver = {
266 .type = CONNMAN_DEVICE_TYPE_ETHERNET,
267 .probe = eth_dev_probe,
268 .remove = eth_dev_remove,
269 .enable = eth_dev_enable,
270 .disable = eth_dev_disable,
273 static int eth_tech_probe(struct connman_technology *technology)
278 static void eth_tech_remove(struct connman_technology *technology)
283 static GList *eth_interface_list = NULL;
285 static void eth_tech_add_interface(struct connman_technology *technology,
286 int index, const char *name, const char *ident)
288 DBG("index %d name %s ident %s", index, name, ident);
290 if (g_list_find(eth_interface_list, GINT_TO_POINTER((int)index)))
293 eth_interface_list = g_list_prepend(eth_interface_list,
294 (GINT_TO_POINTER((int) index)));
297 static void eth_tech_remove_interface(struct connman_technology *technology,
300 DBG("index %d", index);
302 eth_interface_list = g_list_remove(eth_interface_list,
303 GINT_TO_POINTER((int) index));
306 static void eth_tech_enable_tethering(struct connman_technology *technology,
310 struct ethernet_data *ethernet;
312 for (list = eth_interface_list; list; list = list->next) {
313 int index = GPOINTER_TO_INT(list->data);
314 struct connman_device *device =
315 connman_device_find_by_index(index);
318 ethernet = connman_device_get_data(device);
320 remove_network(device, ethernet);
323 connman_technology_tethering_notify(technology, true);
325 connman_inet_ifup(index);
327 connman_inet_add_to_bridge(index, bridge);
329 eth_tethering = true;
333 static void eth_tech_disable_tethering(struct connman_technology *technology,
338 for (list = eth_interface_list; list; list = list->next) {
339 int index = GPOINTER_TO_INT(list->data);
340 struct connman_device *device =
341 connman_device_find_by_index(index);
343 connman_inet_remove_from_bridge(index, bridge);
345 connman_technology_tethering_notify(technology, false);
348 connman_device_reconnect_service(device);
350 eth_tethering = false;
354 static int eth_tech_set_tethering(struct connman_technology *technology,
355 const char *identifier, const char *passphrase,
356 const char *bridge, bool enabled, bool hidden)
358 if (!connman_technology_is_tethering_allowed(
359 CONNMAN_SERVICE_TYPE_ETHERNET))
362 DBG("bridge %s enabled %d", bridge, enabled);
365 eth_tech_enable_tethering(technology, bridge);
367 eth_tech_disable_tethering(technology, bridge);
372 static struct connman_technology_driver eth_tech_driver = {
374 .type = CONNMAN_SERVICE_TYPE_ETHERNET,
375 .probe = eth_tech_probe,
376 .remove = eth_tech_remove,
377 .add_interface = eth_tech_add_interface,
378 .remove_interface = eth_tech_remove_interface,
379 .set_tethering = eth_tech_set_tethering,
382 static int ethernet_init(void)
386 err = connman_technology_driver_register(ð_tech_driver);
390 err = connman_network_driver_register(ð_network_driver);
394 err = connman_device_driver_register(ð_dev_driver);
396 connman_network_driver_unregister(ð_network_driver);
403 static void ethernet_exit(void)
405 connman_technology_driver_unregister(ð_tech_driver);
407 connman_network_driver_unregister(ð_network_driver);
409 connman_device_driver_unregister(ð_dev_driver);
412 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
413 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)