Tethering: Add hidden access point support in technology
[platform/upstream/connman.git] / plugins / ethernet.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  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 bool 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 eth_network_probe(struct connman_network *network)
54 {
55         DBG("network %p", network);
56
57         return 0;
58 }
59
60 static void eth_network_remove(struct connman_network *network)
61 {
62         DBG("network %p", network);
63 }
64
65 static int eth_network_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 eth_network_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 eth_network_driver = {
84         .name           = "cable",
85         .type           = CONNMAN_NETWORK_TYPE_ETHERNET,
86         .probe          = eth_network_probe,
87         .remove         = eth_network_remove,
88         .connect        = eth_network_connect,
89         .disconnect     = eth_network_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)
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)
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)
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 eth_dev_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)
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 eth_dev_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 eth_dev_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 eth_dev_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 eth_dev_driver = {
221         .name           = "ethernet",
222         .type           = CONNMAN_DEVICE_TYPE_ETHERNET,
223         .probe          = eth_dev_probe,
224         .remove         = eth_dev_remove,
225         .enable         = eth_dev_enable,
226         .disable        = eth_dev_disable,
227 };
228
229 static int eth_tech_probe(struct connman_technology *technology)
230 {
231         return 0;
232 }
233
234 static void eth_tech_remove(struct connman_technology *technology)
235 {
236         DBG("");
237 }
238
239 static GList *eth_interface_list = NULL;
240
241 static void eth_tech_add_interface(struct connman_technology *technology,
242                         int index, const char *name, const char *ident)
243 {
244         DBG("index %d name %s ident %s", index, name, ident);
245
246         if (g_list_find(eth_interface_list, GINT_TO_POINTER((int)index)))
247                 return;
248
249         eth_interface_list = g_list_prepend(eth_interface_list,
250                                         (GINT_TO_POINTER((int) index)));
251 }
252
253 static void eth_tech_remove_interface(struct connman_technology *technology,
254                                                                 int index)
255 {
256         DBG("index %d", index);
257
258         eth_interface_list = g_list_remove(eth_interface_list,
259                                         GINT_TO_POINTER((int) index));
260 }
261
262 static void eth_tech_enable_tethering(struct connman_technology *technology,
263                                                 const char *bridge)
264 {
265         GList *list;
266         struct ethernet_data *ethernet;
267
268         for (list = eth_interface_list; list; list = list->next) {
269                 int index = GPOINTER_TO_INT(list->data);
270                 struct connman_device *device =
271                         connman_device_find_by_index(index);
272
273                 if (device) {
274                         ethernet = connman_device_get_data(device);
275                         if (ethernet)
276                                 remove_network(device, ethernet);
277                 }
278
279                 connman_technology_tethering_notify(technology, true);
280
281                 connman_inet_ifup(index);
282
283                 connman_inet_add_to_bridge(index, bridge);
284
285                 eth_tethering = true;
286         }
287 }
288
289 static void eth_tech_disable_tethering(struct connman_technology *technology,
290                                                 const char *bridge)
291 {
292         GList *list;
293
294         for (list = eth_interface_list; list; list = list->next) {
295                 int index = GPOINTER_TO_INT(list->data);
296                 struct connman_device *device =
297                         connman_device_find_by_index(index);
298
299                 connman_inet_remove_from_bridge(index, bridge);
300
301                 connman_technology_tethering_notify(technology, false);
302
303                 if (device)
304                         connman_device_reconnect_service(device);
305
306                 eth_tethering = false;
307         }
308 }
309
310 static int eth_tech_set_tethering(struct connman_technology *technology,
311                                 const char *identifier, const char *passphrase,
312                                 const char *bridge, bool enabled, bool hidden)
313 {
314         if (!connman_technology_is_tethering_allowed(
315                         CONNMAN_SERVICE_TYPE_ETHERNET))
316                 return 0;
317
318         DBG("bridge %s enabled %d", bridge, enabled);
319
320         if (enabled)
321                 eth_tech_enable_tethering(technology, bridge);
322         else
323                 eth_tech_disable_tethering(technology, bridge);
324
325         return 0;
326 }
327
328 static struct connman_technology_driver eth_tech_driver = {
329         .name                   = "ethernet",
330         .type                   = CONNMAN_SERVICE_TYPE_ETHERNET,
331         .probe                  = eth_tech_probe,
332         .remove                 = eth_tech_remove,
333         .add_interface          = eth_tech_add_interface,
334         .remove_interface       = eth_tech_remove_interface,
335         .set_tethering          = eth_tech_set_tethering,
336 };
337
338 static int ethernet_init(void)
339 {
340         int err;
341
342         err = connman_technology_driver_register(&eth_tech_driver);
343         if (err < 0)
344                 return err;
345
346         err = connman_network_driver_register(&eth_network_driver);
347         if (err < 0)
348                 return err;
349
350         err = connman_device_driver_register(&eth_dev_driver);
351         if (err < 0) {
352                 connman_network_driver_unregister(&eth_network_driver);
353                 return err;
354         }
355
356         return 0;
357 }
358
359 static void ethernet_exit(void)
360 {
361         connman_technology_driver_unregister(&eth_tech_driver);
362
363         connman_network_driver_unregister(&eth_network_driver);
364
365         connman_device_driver_unregister(&eth_dev_driver);
366 }
367
368 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
369                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)