5 * Copyright (C) 2007-2010 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>
43 struct ethernet_data {
47 struct connman_network *network;
50 static int cable_probe(struct connman_network *network)
52 DBG("network %p", network);
57 static void cable_remove(struct connman_network *network)
59 DBG("network %p", network);
62 static int cable_connect(struct connman_network *network)
64 DBG("network %p", network);
69 static int cable_disconnect(struct connman_network *network)
71 DBG("network %p", network);
76 static struct connman_network_driver cable_driver = {
78 .type = CONNMAN_NETWORK_TYPE_ETHERNET,
80 .remove = cable_remove,
81 .connect = cable_connect,
82 .disconnect = cable_disconnect,
85 static void add_network(struct connman_device *device)
87 struct connman_network *network;
90 network = connman_network_create("carrier",
91 CONNMAN_NETWORK_TYPE_ETHERNET);
95 index = connman_device_get_index(device);
96 connman_network_set_index(network, index);
98 connman_network_set_name(network, "Wired");
100 if (connman_device_add_network(device, network) < 0) {
101 connman_network_unref(network);
105 connman_network_set_available(network, TRUE);
107 connman_network_set_group(network, "cable");
109 connman_network_set_connected(network, TRUE);
112 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
114 struct connman_device *device = user_data;
115 struct ethernet_data *ethernet = connman_device_get_data(device);
117 DBG("index %d flags %d change %d", ethernet->index, flags, change);
119 if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
120 if (flags & IFF_UP) {
122 connman_device_set_powered(device, TRUE);
125 connman_device_set_powered(device, FALSE);
129 if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
130 if (flags & IFF_LOWER_UP) {
135 connman_device_remove_all_networks(device);
139 ethernet->flags = flags;
142 static int ethernet_probe(struct connman_device *device)
144 struct ethernet_data *ethernet;
146 DBG("device %p", device);
148 ethernet = g_try_new0(struct ethernet_data, 1);
149 if (ethernet == NULL)
152 connman_device_set_data(device, ethernet);
154 ethernet->index = connman_device_get_index(device);
157 ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
158 ethernet_newlink, device);
163 static void ethernet_remove(struct connman_device *device)
165 struct ethernet_data *ethernet = connman_device_get_data(device);
167 DBG("device %p", device);
169 connman_device_set_data(device, NULL);
171 connman_rtnl_remove_watch(ethernet->watch);
173 connman_device_remove_all_networks(device);
178 static int ethernet_enable(struct connman_device *device)
180 struct ethernet_data *ethernet = connman_device_get_data(device);
182 DBG("device %p", device);
184 return connman_inet_ifup(ethernet->index);
187 static int ethernet_disable(struct connman_device *device)
189 struct ethernet_data *ethernet = connman_device_get_data(device);
191 DBG("device %p", device);
193 return connman_inet_ifdown(ethernet->index);
196 static struct connman_device_driver ethernet_driver = {
198 .type = CONNMAN_DEVICE_TYPE_ETHERNET,
199 .probe = ethernet_probe,
200 .remove = ethernet_remove,
201 .enable = ethernet_enable,
202 .disable = ethernet_disable,
205 static GList *cdc_interface_list = NULL;
207 static void tech_add_interface(struct connman_technology *technology,
208 int index, const char *name, const char *ident)
210 DBG("index %d name %s ident %s", index, name, ident);
212 if (g_list_find(cdc_interface_list,
213 GINT_TO_POINTER((int) index)) != NULL)
216 cdc_interface_list = g_list_prepend(cdc_interface_list,
217 (GINT_TO_POINTER((int) index)));
220 static void tech_remove_interface(struct connman_technology *technology,
223 DBG("index %d", index);
225 cdc_interface_list = g_list_remove(cdc_interface_list,
226 GINT_TO_POINTER((int) index));
229 static void enable_tethering(struct connman_technology *technology,
234 for (list = cdc_interface_list; list; list = list->next) {
235 int index = GPOINTER_TO_INT(list->data);
237 connman_technology_tethering_notify(technology, TRUE);
239 connman_inet_ifup(index);
241 connman_inet_add_to_bridge(index, bridge);
245 static void disable_tethering(struct connman_technology *technology,
250 for (list = cdc_interface_list; list; list = list->next) {
251 int index = GPOINTER_TO_INT(list->data);
253 connman_inet_remove_from_bridge(index, bridge);
255 connman_inet_ifdown(index);
257 connman_technology_tethering_notify(technology, FALSE);
261 static int tech_set_tethering(struct connman_technology *technology,
262 const char *identifier, const char *passphrase,
263 const char *bridge, connman_bool_t enabled)
265 DBG("bridge %s enabled %d", bridge, enabled);
268 enable_tethering(technology, bridge);
270 disable_tethering(technology, bridge);
275 static int tech_probe(struct connman_technology *technology)
280 static void tech_remove(struct connman_technology *technology)
282 g_list_free(cdc_interface_list);
284 cdc_interface_list = NULL;
287 static struct connman_technology_driver tech_driver = {
288 .name = "cdc_ethernet",
289 .type = CONNMAN_SERVICE_TYPE_GADGET,
291 .remove = tech_remove,
292 .add_interface = tech_add_interface,
293 .remove_interface = tech_remove_interface,
294 .set_tethering = tech_set_tethering,
297 static int ethernet_init(void)
301 err = connman_network_driver_register(&cable_driver);
305 err = connman_device_driver_register(ðernet_driver);
307 connman_network_driver_unregister(&cable_driver);
311 err = connman_technology_driver_register(&tech_driver);
313 connman_device_driver_unregister(ðernet_driver);
314 connman_network_driver_unregister(&cable_driver);
321 static void ethernet_exit(void)
323 connman_technology_driver_unregister(&tech_driver);
325 connman_network_driver_unregister(&cable_driver);
327 connman_device_driver_unregister(ðernet_driver);
330 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
331 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)