Adding connman-test subpackage
[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 #if defined TIZEN_EXT
86 static void set_proxy(struct connman_network *network)
87 {
88         const char http_proxy[] = "http_proxy";
89         char *proxy = NULL;
90
91         proxy = getenv(http_proxy);
92         DBG("Get system proxy: %s", proxy);
93
94         if(proxy != NULL && strlen(proxy) > 8)
95                 connman_network_set_proxy(network, proxy);
96 }
97 #endif
98
99 static void add_network(struct connman_device *device,
100                         struct ethernet_data *ethernet)
101 {
102         struct connman_network *network;
103         int index;
104
105         network = connman_network_create("carrier",
106                                         CONNMAN_NETWORK_TYPE_ETHERNET);
107         if (network == NULL)
108                 return;
109
110         index = connman_device_get_index(device);
111         connman_network_set_index(network, index);
112
113         connman_network_set_name(network, "Wired");
114
115         if (connman_device_add_network(device, network) < 0) {
116                 connman_network_unref(network);
117                 return;
118         }
119
120         connman_network_set_available(network, TRUE);
121
122         connman_network_set_group(network, "cable");
123
124         connman_network_set_connected(network, TRUE);
125
126 #if defined TIZEN_EXT
127         set_proxy(network);
128 #endif
129
130         ethernet->network = network;
131 }
132
133 static void remove_network(struct connman_device *device,
134                                 struct ethernet_data *ethernet)
135 {
136         if (ethernet->network == NULL)
137                 return;
138
139         connman_device_remove_network(device, ethernet->network);
140         connman_network_unref(ethernet->network);
141
142         ethernet->network = NULL;
143 }
144
145 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
146 {
147         struct connman_device *device = user_data;
148         struct ethernet_data *ethernet = connman_device_get_data(device);
149
150         DBG("index %d flags %d change %d", ethernet->index, flags, change);
151
152         if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
153                 if (flags & IFF_UP) {
154                         DBG("power on");
155                         connman_device_set_powered(device, TRUE);
156                 } else {
157                         DBG("power off");
158                         connman_device_set_powered(device, FALSE);
159                 }
160         }
161
162         if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
163                 if (flags & IFF_LOWER_UP) {
164                         DBG("carrier on");
165                         add_network(device, ethernet);
166                 } else {
167                         DBG("carrier off");
168                         remove_network(device, ethernet);
169                 }
170         }
171
172         ethernet->flags = flags;
173 }
174
175 static int ethernet_probe(struct connman_device *device)
176 {
177         struct ethernet_data *ethernet;
178
179         DBG("device %p", device);
180
181         ethernet = g_try_new0(struct ethernet_data, 1);
182         if (ethernet == NULL)
183                 return -ENOMEM;
184
185         connman_device_set_data(device, ethernet);
186
187         ethernet->index = connman_device_get_index(device);
188         ethernet->flags = 0;
189
190         ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
191                                                 ethernet_newlink, device);
192
193         return 0;
194 }
195
196 static void ethernet_remove(struct connman_device *device)
197 {
198         struct ethernet_data *ethernet = connman_device_get_data(device);
199
200         DBG("device %p", device);
201
202         connman_device_set_data(device, NULL);
203
204         connman_rtnl_remove_watch(ethernet->watch);
205
206         remove_network(device, ethernet);
207
208         g_free(ethernet);
209 }
210
211 static int ethernet_enable(struct connman_device *device)
212 {
213         struct ethernet_data *ethernet = connman_device_get_data(device);
214
215         DBG("device %p", device);
216
217         return connman_inet_ifup(ethernet->index);
218 }
219
220 static int ethernet_disable(struct connman_device *device)
221 {
222         struct ethernet_data *ethernet = connman_device_get_data(device);
223
224         DBG("device %p", device);
225
226         return connman_inet_ifdown(ethernet->index);
227 }
228
229 static struct connman_device_driver ethernet_driver = {
230         .name           = "ethernet",
231         .type           = CONNMAN_DEVICE_TYPE_ETHERNET,
232         .probe          = ethernet_probe,
233         .remove         = ethernet_remove,
234         .enable         = ethernet_enable,
235         .disable        = ethernet_disable,
236 };
237
238 static GList *cdc_interface_list = NULL;
239
240 static void tech_add_interface(struct connman_technology *technology,
241                         int index, const char *name, const char *ident)
242 {
243         DBG("index %d name %s ident %s", index, name, ident);
244
245         if (g_list_find(cdc_interface_list,
246                         GINT_TO_POINTER((int) index)) != NULL)
247                 return;
248
249         cdc_interface_list = g_list_prepend(cdc_interface_list,
250                                         (GINT_TO_POINTER((int) index)));
251 }
252
253 static void tech_remove_interface(struct connman_technology *technology,
254                                                                 int index)
255 {
256         DBG("index %d", index);
257
258         cdc_interface_list = g_list_remove(cdc_interface_list,
259                                         GINT_TO_POINTER((int) index));
260 }
261
262 static void enable_tethering(struct connman_technology *technology,
263                                                 const char *bridge)
264 {
265         GList *list;
266
267         for (list = cdc_interface_list; list; list = list->next) {
268                 int index = GPOINTER_TO_INT(list->data);
269
270                 connman_technology_tethering_notify(technology, TRUE);
271
272                 connman_inet_ifup(index);
273
274                 connman_inet_add_to_bridge(index, bridge);
275         }
276 }
277
278 static void disable_tethering(struct connman_technology *technology,
279                                                 const char *bridge)
280 {
281         GList *list;
282
283         for (list = cdc_interface_list; list; list = list->next) {
284                 int index = GPOINTER_TO_INT(list->data);
285
286                 connman_inet_remove_from_bridge(index, bridge);
287
288                 connman_inet_ifdown(index);
289
290                 connman_technology_tethering_notify(technology, FALSE);
291         }
292 }
293
294 static int tech_set_tethering(struct connman_technology *technology,
295                                 const char *identifier, const char *passphrase,
296                                 const char *bridge, connman_bool_t enabled)
297 {
298         DBG("bridge %s enabled %d", bridge, enabled);
299
300         if (enabled)
301                 enable_tethering(technology, bridge);
302         else
303                 disable_tethering(technology, bridge);
304
305         return 0;
306 }
307
308 static int tech_probe(struct connman_technology *technology)
309 {
310         return 0;
311 }
312
313 static void tech_remove(struct connman_technology *technology)
314 {
315         g_list_free(cdc_interface_list);
316
317         cdc_interface_list = NULL;
318 }
319
320 static struct connman_technology_driver tech_driver = {
321         .name                   = "cdc_ethernet",
322         .type                   = CONNMAN_SERVICE_TYPE_GADGET,
323         .probe                  = tech_probe,
324         .remove                 = tech_remove,
325         .add_interface          = tech_add_interface,
326         .remove_interface       = tech_remove_interface,
327         .set_tethering          = tech_set_tethering,
328 };
329
330 #if defined TIZEN_EXT
331 static int eth_probe(struct connman_technology *technology)
332 {
333         return 0;
334 }
335
336 static void eth_remove(struct connman_technology *technology)
337 {
338         DBG("");
339 }
340
341 static struct connman_technology_driver eth_driver = {
342         .name                   = "ethernet",
343         .type                   = CONNMAN_SERVICE_TYPE_ETHERNET,
344         .probe                  = eth_probe,
345         .remove                 = eth_remove,
346 };
347 #endif
348
349 static int ethernet_init(void)
350 {
351         int err;
352
353 #if defined TIZEN_EXT
354         err = connman_technology_driver_register(&eth_driver);
355         if (err < 0)
356                 return err;
357 #endif
358
359         err = connman_network_driver_register(&cable_driver);
360         if (err < 0)
361                 return err;
362
363         err = connman_device_driver_register(&ethernet_driver);
364         if (err < 0) {
365                 connman_network_driver_unregister(&cable_driver);
366                 return err;
367         }
368
369         err = connman_technology_driver_register(&tech_driver);
370         if (err < 0) {
371                 connman_device_driver_unregister(&ethernet_driver);
372                 connman_network_driver_unregister(&cable_driver);
373                 return err;
374         }
375
376         return 0;
377 }
378
379 static void ethernet_exit(void)
380 {
381 #if defined TIZEN_EXT
382         connman_technology_driver_unregister(&eth_driver);
383 #endif
384
385         connman_technology_driver_unregister(&tech_driver);
386
387         connman_network_driver_unregister(&cable_driver);
388
389         connman_device_driver_unregister(&ethernet_driver);
390 }
391
392 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
393                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)