bafc75dddd2f2a920bf859d7f4b1205062592e3a
[platform/upstream/connman.git] / plugins / ethernet.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 #include <connman/setting.h>
43
44 static connman_bool_t eth_tethering = FALSE;
45
46 struct ethernet_data {
47         int index;
48         unsigned flags;
49         unsigned int watch;
50         struct connman_network *network;
51 };
52
53 static int cable_probe(struct connman_network *network)
54 {
55         DBG("network %p", network);
56
57         return 0;
58 }
59
60 static void cable_remove(struct connman_network *network)
61 {
62         DBG("network %p", network);
63 }
64
65 static int cable_connect(struct connman_network *network)
66 {
67         DBG("network %p", network);
68
69         connman_network_set_connected(network, TRUE);
70
71         return 0;
72 }
73
74 static int cable_disconnect(struct connman_network *network)
75 {
76         DBG("network %p", network);
77
78         connman_network_set_connected(network, FALSE);
79
80         return 0;
81 }
82
83 static struct connman_network_driver cable_driver = {
84         .name           = "cable",
85         .type           = CONNMAN_NETWORK_TYPE_ETHERNET,
86         .probe          = cable_probe,
87         .remove         = cable_remove,
88         .connect        = cable_connect,
89         .disconnect     = cable_disconnect,
90 };
91
92 static void add_network(struct connman_device *device,
93                         struct ethernet_data *ethernet)
94 {
95         struct connman_network *network;
96         int index;
97
98         network = connman_network_create("carrier",
99                                         CONNMAN_NETWORK_TYPE_ETHERNET);
100         if (network == NULL)
101                 return;
102
103         index = connman_device_get_index(device);
104         connman_network_set_index(network, index);
105
106         connman_network_set_name(network, "Wired");
107
108         if (connman_device_add_network(device, network) < 0) {
109                 connman_network_unref(network);
110                 return;
111         }
112
113         if (eth_tethering == FALSE)
114                 /*
115                  * Prevent service from starting the reconnect
116                  * procedure as we do not want the DHCP client
117                  * to run when tethering.
118                  */
119                 connman_network_set_group(network, "cable");
120
121         ethernet->network = network;
122 }
123
124 static void remove_network(struct connman_device *device,
125                                 struct ethernet_data *ethernet)
126 {
127         if (ethernet->network == NULL)
128                 return;
129
130         connman_device_remove_network(device, ethernet->network);
131         connman_network_unref(ethernet->network);
132
133         ethernet->network = NULL;
134 }
135
136 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
137 {
138         struct connman_device *device = user_data;
139         struct ethernet_data *ethernet = connman_device_get_data(device);
140
141         DBG("index %d flags %d change %d", ethernet->index, flags, change);
142
143         if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
144                 if (flags & IFF_UP) {
145                         DBG("power on");
146                         connman_device_set_powered(device, TRUE);
147                 } else {
148                         DBG("power off");
149                         connman_device_set_powered(device, FALSE);
150                 }
151         }
152
153         if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
154                 if (flags & IFF_LOWER_UP) {
155                         DBG("carrier on");
156                         add_network(device, ethernet);
157                 } else {
158                         DBG("carrier off");
159                         remove_network(device, ethernet);
160                 }
161         }
162
163         ethernet->flags = flags;
164 }
165
166 static int ethernet_probe(struct connman_device *device)
167 {
168         struct ethernet_data *ethernet;
169
170         DBG("device %p", device);
171
172         ethernet = g_try_new0(struct ethernet_data, 1);
173         if (ethernet == NULL)
174                 return -ENOMEM;
175
176         connman_device_set_data(device, ethernet);
177
178         ethernet->index = connman_device_get_index(device);
179         ethernet->flags = 0;
180
181         ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
182                                                 ethernet_newlink, device);
183
184         return 0;
185 }
186
187 static void ethernet_remove(struct connman_device *device)
188 {
189         struct ethernet_data *ethernet = connman_device_get_data(device);
190
191         DBG("device %p", device);
192
193         connman_device_set_data(device, NULL);
194
195         connman_rtnl_remove_watch(ethernet->watch);
196
197         remove_network(device, ethernet);
198
199         g_free(ethernet);
200 }
201
202 static int ethernet_enable(struct connman_device *device)
203 {
204         struct ethernet_data *ethernet = connman_device_get_data(device);
205
206         DBG("device %p", device);
207
208         return connman_inet_ifup(ethernet->index);
209 }
210
211 static int ethernet_disable(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_ifdown(ethernet->index);
218 }
219
220 static struct connman_device_driver ethernet_driver = {
221         .name           = "ethernet",
222         .type           = CONNMAN_DEVICE_TYPE_ETHERNET,
223         .probe          = ethernet_probe,
224         .remove         = ethernet_remove,
225         .enable         = ethernet_enable,
226         .disable        = ethernet_disable,
227 };
228
229 static GList *cdc_interface_list = NULL;
230
231 static void tech_add_interface(struct connman_technology *technology,
232                         int index, const char *name, const char *ident)
233 {
234         DBG("index %d name %s ident %s", index, name, ident);
235
236         if (g_list_find(cdc_interface_list,
237                         GINT_TO_POINTER((int) index)) != NULL)
238                 return;
239
240         cdc_interface_list = g_list_prepend(cdc_interface_list,
241                                         (GINT_TO_POINTER((int) index)));
242 }
243
244 static void tech_remove_interface(struct connman_technology *technology,
245                                                                 int index)
246 {
247         DBG("index %d", index);
248
249         cdc_interface_list = g_list_remove(cdc_interface_list,
250                                         GINT_TO_POINTER((int) index));
251 }
252
253 static void enable_tethering(struct connman_technology *technology,
254                                                 const char *bridge)
255 {
256         GList *list;
257
258         for (list = cdc_interface_list; list; list = list->next) {
259                 int index = GPOINTER_TO_INT(list->data);
260
261                 connman_technology_tethering_notify(technology, TRUE);
262
263                 connman_inet_ifup(index);
264
265                 connman_inet_add_to_bridge(index, bridge);
266         }
267 }
268
269 static void disable_tethering(struct connman_technology *technology,
270                                                 const char *bridge)
271 {
272         GList *list;
273
274         for (list = cdc_interface_list; list; list = list->next) {
275                 int index = GPOINTER_TO_INT(list->data);
276
277                 connman_inet_remove_from_bridge(index, bridge);
278
279                 connman_inet_ifdown(index);
280
281                 connman_technology_tethering_notify(technology, FALSE);
282         }
283 }
284
285 static int tech_set_tethering(struct connman_technology *technology,
286                                 const char *identifier, const char *passphrase,
287                                 const char *bridge, connman_bool_t enabled)
288 {
289         DBG("bridge %s enabled %d", bridge, enabled);
290
291         if (enabled)
292                 enable_tethering(technology, bridge);
293         else
294                 disable_tethering(technology, bridge);
295
296         return 0;
297 }
298
299 static int tech_probe(struct connman_technology *technology)
300 {
301         return 0;
302 }
303
304 static void tech_remove(struct connman_technology *technology)
305 {
306         g_list_free(cdc_interface_list);
307
308         cdc_interface_list = NULL;
309 }
310
311 static struct connman_technology_driver tech_driver = {
312         .name                   = "cdc_ethernet",
313         .type                   = CONNMAN_SERVICE_TYPE_GADGET,
314         .probe                  = tech_probe,
315         .remove                 = tech_remove,
316         .add_interface          = tech_add_interface,
317         .remove_interface       = tech_remove_interface,
318         .set_tethering          = tech_set_tethering,
319 };
320
321 static int eth_probe(struct connman_technology *technology)
322 {
323         return 0;
324 }
325
326 static void eth_remove(struct connman_technology *technology)
327 {
328         DBG("");
329 }
330
331 static GList *eth_interface_list = NULL;
332
333 static void eth_add_interface(struct connman_technology *technology,
334                         int index, const char *name, const char *ident)
335 {
336         DBG("index %d name %s ident %s", index, name, ident);
337
338         if (g_list_find(eth_interface_list,
339                         GINT_TO_POINTER((int) index)) != NULL)
340                 return;
341
342         eth_interface_list = g_list_prepend(eth_interface_list,
343                                         (GINT_TO_POINTER((int) index)));
344 }
345
346 static void eth_remove_interface(struct connman_technology *technology,
347                                                                 int index)
348 {
349         DBG("index %d", index);
350
351         eth_interface_list = g_list_remove(eth_interface_list,
352                                         GINT_TO_POINTER((int) index));
353 }
354
355 static void eth_enable_tethering(struct connman_technology *technology,
356                                                 const char *bridge)
357 {
358         GList *list;
359
360         for (list = eth_interface_list; list; list = list->next) {
361                 int index = GPOINTER_TO_INT(list->data);
362                 struct connman_device *device =
363                         connman_device_find_by_index(index);
364
365                 if (device != NULL)
366                         connman_device_disconnect_service(device);
367
368                 connman_technology_tethering_notify(technology, TRUE);
369
370                 connman_inet_ifup(index);
371
372                 connman_inet_add_to_bridge(index, bridge);
373
374                 eth_tethering = TRUE;
375         }
376 }
377
378 static void eth_disable_tethering(struct connman_technology *technology,
379                                                 const char *bridge)
380 {
381         GList *list;
382
383         for (list = eth_interface_list; list; list = list->next) {
384                 int index = GPOINTER_TO_INT(list->data);
385                 struct connman_device *device =
386                         connman_device_find_by_index(index);
387
388                 connman_inet_remove_from_bridge(index, bridge);
389
390                 connman_inet_ifdown(index);
391
392                 connman_technology_tethering_notify(technology, FALSE);
393
394                 if (device != NULL)
395                         connman_device_reconnect_service(device);
396
397                 eth_tethering = FALSE;
398         }
399 }
400
401 static int eth_set_tethering(struct connman_technology *technology,
402                                 const char *identifier, const char *passphrase,
403                                 const char *bridge, connman_bool_t enabled)
404 {
405         if (connman_technology_is_tethering_allowed(
406                                 CONNMAN_SERVICE_TYPE_ETHERNET) == FALSE)
407                 return 0;
408
409         DBG("bridge %s enabled %d", bridge, enabled);
410
411         if (enabled)
412                 eth_enable_tethering(technology, bridge);
413         else
414                 eth_disable_tethering(technology, bridge);
415
416         return 0;
417 }
418
419 static struct connman_technology_driver eth_driver = {
420         .name                   = "ethernet",
421         .type                   = CONNMAN_SERVICE_TYPE_ETHERNET,
422         .probe                  = eth_probe,
423         .remove                 = eth_remove,
424         .add_interface          = eth_add_interface,
425         .remove_interface       = eth_remove_interface,
426         .set_tethering          = eth_set_tethering,
427 };
428
429 static int ethernet_init(void)
430 {
431         int err;
432
433         err = connman_technology_driver_register(&eth_driver);
434         if (err < 0)
435                 return err;
436
437         err = connman_network_driver_register(&cable_driver);
438         if (err < 0)
439                 return err;
440
441         err = connman_device_driver_register(&ethernet_driver);
442         if (err < 0) {
443                 connman_network_driver_unregister(&cable_driver);
444                 return err;
445         }
446
447         err = connman_technology_driver_register(&tech_driver);
448         if (err < 0) {
449                 connman_device_driver_unregister(&ethernet_driver);
450                 connman_network_driver_unregister(&cable_driver);
451                 return err;
452         }
453
454         return 0;
455 }
456
457 static void ethernet_exit(void)
458 {
459         connman_technology_driver_unregister(&eth_driver);
460
461         connman_technology_driver_unregister(&tech_driver);
462
463         connman_network_driver_unregister(&cable_driver);
464
465         connman_device_driver_unregister(&ethernet_driver);
466 }
467
468 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
469                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)