loopback: Return a More Descriptive Error Code
[framework/connectivity/connman.git] / plugins / ethernet.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <net/if.h>
28
29 #ifndef IFF_LOWER_UP
30 #define IFF_LOWER_UP    0x10000
31 #endif
32
33 #include <glib.h>
34
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
43 struct ethernet_data {
44         int index;
45         unsigned flags;
46         unsigned int watch;
47         struct connman_network *network;
48 };
49
50 static int cable_probe(struct connman_network *network)
51 {
52         DBG("network %p", network);
53
54         return 0;
55 }
56
57 static void cable_remove(struct connman_network *network)
58 {
59         DBG("network %p", network);
60 }
61
62 static int cable_connect(struct connman_network *network)
63 {
64         DBG("network %p", network);
65
66         return 0;
67 }
68
69 static int cable_disconnect(struct connman_network *network)
70 {
71         DBG("network %p", network);
72
73         return 0;
74 }
75
76 static struct connman_network_driver cable_driver = {
77         .name           = "cable",
78         .type           = CONNMAN_NETWORK_TYPE_ETHERNET,
79         .probe          = cable_probe,
80         .remove         = cable_remove,
81         .connect        = cable_connect,
82         .disconnect     = cable_disconnect,
83 };
84
85 static void add_network(struct connman_device *device)
86 {
87         struct connman_network *network;
88         int index;
89
90         network = connman_network_create("carrier",
91                                         CONNMAN_NETWORK_TYPE_ETHERNET);
92         if (network == NULL)
93                 return;
94
95         connman_network_register(network);
96
97         index = connman_device_get_index(device);
98         connman_network_set_index(network, index);
99
100         connman_network_set_name(network, "Wired");
101
102         if (connman_device_add_network(device, network) < 0) {
103                 connman_network_unregister(network);
104                 connman_network_unref(network);
105                 return;
106         }
107
108         connman_network_set_available(network, TRUE);
109
110         connman_network_set_group(network, "cable");
111
112         connman_network_set_connected(network, TRUE);
113 }
114
115 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
116 {
117         struct connman_device *device = user_data;
118         struct ethernet_data *ethernet = connman_device_get_data(device);
119
120         DBG("index %d flags %d change %d", ethernet->index, flags, change);
121
122         if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
123                 if (flags & IFF_UP) {
124                         DBG("power on");
125                         connman_device_set_powered(device, TRUE);
126                 } else {
127                         DBG("power off");
128                         connman_device_set_powered(device, FALSE);
129                 }
130         }
131
132         if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
133                 if (flags & IFF_LOWER_UP) {
134                         DBG("carrier on");
135                         add_network(device);
136                 } else {
137                         DBG("carrier off");
138                         connman_device_remove_all_networks(device);
139                 }
140         }
141
142         ethernet->flags = flags;
143 }
144
145 static int ethernet_probe(struct connman_device *device)
146 {
147         struct ethernet_data *ethernet;
148
149         DBG("device %p", device);
150
151         ethernet = g_try_new0(struct ethernet_data, 1);
152         if (ethernet == NULL)
153                 return -ENOMEM;
154
155         connman_device_set_data(device, ethernet);
156
157         ethernet->index = connman_device_get_index(device);
158         ethernet->flags = 0;
159
160         ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
161                                                 ethernet_newlink, device);
162
163         return 0;
164 }
165
166 static void ethernet_remove(struct connman_device *device)
167 {
168         struct ethernet_data *ethernet = connman_device_get_data(device);
169
170         DBG("device %p", device);
171
172         connman_device_set_data(device, NULL);
173
174         connman_rtnl_remove_watch(ethernet->watch);
175
176         connman_device_remove_all_networks(device);
177
178         g_free(ethernet);
179 }
180
181 static int ethernet_enable(struct connman_device *device)
182 {
183         struct ethernet_data *ethernet = connman_device_get_data(device);
184
185         DBG("device %p", device);
186
187         return connman_inet_ifup(ethernet->index);
188 }
189
190 static int ethernet_disable(struct connman_device *device)
191 {
192         struct ethernet_data *ethernet = connman_device_get_data(device);
193
194         DBG("device %p", device);
195
196         return connman_inet_ifdown(ethernet->index);
197 }
198
199 static struct connman_device_driver ethernet_driver = {
200         .name           = "ethernet",
201         .type           = CONNMAN_DEVICE_TYPE_ETHERNET,
202         .probe          = ethernet_probe,
203         .remove         = ethernet_remove,
204         .enable         = ethernet_enable,
205         .disable        = ethernet_disable,
206 };
207
208 static GList *cdc_interface_list = NULL;
209
210 static void tech_add_interface(struct connman_technology *technology,
211                         int index, const char *name, const char *ident)
212 {
213         DBG("index %d name %s ident %s", index, name, ident);
214
215         if (g_list_find(cdc_interface_list,
216                         GINT_TO_POINTER((int) index)) != NULL)
217                 return;
218
219         cdc_interface_list = g_list_prepend(cdc_interface_list,
220                                         (GINT_TO_POINTER((int) index)));
221 }
222
223 static void tech_remove_interface(struct connman_technology *technology,
224                                                                 int index)
225 {
226         DBG("index %d", index);
227
228         cdc_interface_list = g_list_remove(cdc_interface_list,
229                                         GINT_TO_POINTER((int) index));
230 }
231
232 static void enable_tethering(struct connman_technology *technology,
233                                                 const char *bridge)
234 {
235         GList *list;
236
237         for (list = cdc_interface_list; list; list = list->next) {
238                 int index = GPOINTER_TO_INT(list->data);
239
240                 connman_technology_tethering_notify(technology, TRUE);
241
242                 connman_inet_ifup(index);
243
244                 connman_inet_add_to_bridge(index, bridge);
245         }
246 }
247
248 static void disable_tethering(struct connman_technology *technology,
249                                                 const char *bridge)
250 {
251         GList *list;
252
253         for (list = cdc_interface_list; list; list = list->next) {
254                 int index = GPOINTER_TO_INT(list->data);
255
256                 connman_inet_remove_from_bridge(index, bridge);
257
258                 connman_inet_ifdown(index);
259
260                 connman_technology_tethering_notify(technology, FALSE);
261         }
262 }
263
264 static int tech_set_tethering(struct connman_technology *technology,
265                                 const char *identifier, const char *passphrase,
266                                 const char *bridge, connman_bool_t enabled)
267 {
268         DBG("bridge %s enabled %d", bridge, enabled);
269
270         if (enabled)
271                 enable_tethering(technology, bridge);
272         else
273                 disable_tethering(technology, bridge);
274
275         return 0;
276 }
277
278 static int tech_probe(struct connman_technology *technology)
279 {
280         return 0;
281 }
282
283 static void tech_remove(struct connman_technology *technology)
284 {
285         g_list_free(cdc_interface_list);
286
287         cdc_interface_list = NULL;
288 }
289
290 static struct connman_technology_driver tech_driver = {
291         .name                   = "cdc_ethernet",
292         .type                   = CONNMAN_SERVICE_TYPE_GADGET,
293         .probe                  = tech_probe,
294         .remove                 = tech_remove,
295         .add_interface          = tech_add_interface,
296         .remove_interface       = tech_remove_interface,
297         .set_tethering          = tech_set_tethering,
298 };
299
300 static int ethernet_init(void)
301 {
302         int err;
303
304         err = connman_network_driver_register(&cable_driver);
305         if (err < 0)
306                 return err;
307
308         err = connman_device_driver_register(&ethernet_driver);
309         if (err < 0) {
310                 connman_network_driver_unregister(&cable_driver);
311                 return err;
312         }
313
314         err = connman_technology_driver_register(&tech_driver);
315         if (err < 0) {
316                 connman_device_driver_unregister(&ethernet_driver);
317                 connman_network_driver_unregister(&cable_driver);
318                 return err;
319         }
320
321         return 0;
322 }
323
324 static void ethernet_exit(void)
325 {
326         connman_technology_driver_unregister(&tech_driver);
327
328         connman_network_driver_unregister(&cable_driver);
329
330         connman_device_driver_unregister(&ethernet_driver);
331 }
332
333 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
334                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)