network: Remove connman_network_register/unregister()
[platform/upstream/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         index = connman_device_get_index(device);
96         connman_network_set_index(network, index);
97
98         connman_network_set_name(network, "Wired");
99
100         if (connman_device_add_network(device, network) < 0) {
101                 connman_network_unref(network);
102                 return;
103         }
104
105         connman_network_set_available(network, TRUE);
106
107         connman_network_set_group(network, "cable");
108
109         connman_network_set_connected(network, TRUE);
110 }
111
112 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
113 {
114         struct connman_device *device = user_data;
115         struct ethernet_data *ethernet = connman_device_get_data(device);
116
117         DBG("index %d flags %d change %d", ethernet->index, flags, change);
118
119         if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
120                 if (flags & IFF_UP) {
121                         DBG("power on");
122                         connman_device_set_powered(device, TRUE);
123                 } else {
124                         DBG("power off");
125                         connman_device_set_powered(device, FALSE);
126                 }
127         }
128
129         if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
130                 if (flags & IFF_LOWER_UP) {
131                         DBG("carrier on");
132                         add_network(device);
133                 } else {
134                         DBG("carrier off");
135                         connman_device_remove_all_networks(device);
136                 }
137         }
138
139         ethernet->flags = flags;
140 }
141
142 static int ethernet_probe(struct connman_device *device)
143 {
144         struct ethernet_data *ethernet;
145
146         DBG("device %p", device);
147
148         ethernet = g_try_new0(struct ethernet_data, 1);
149         if (ethernet == NULL)
150                 return -ENOMEM;
151
152         connman_device_set_data(device, ethernet);
153
154         ethernet->index = connman_device_get_index(device);
155         ethernet->flags = 0;
156
157         ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
158                                                 ethernet_newlink, device);
159
160         return 0;
161 }
162
163 static void ethernet_remove(struct connman_device *device)
164 {
165         struct ethernet_data *ethernet = connman_device_get_data(device);
166
167         DBG("device %p", device);
168
169         connman_device_set_data(device, NULL);
170
171         connman_rtnl_remove_watch(ethernet->watch);
172
173         connman_device_remove_all_networks(device);
174
175         g_free(ethernet);
176 }
177
178 static int ethernet_enable(struct connman_device *device)
179 {
180         struct ethernet_data *ethernet = connman_device_get_data(device);
181
182         DBG("device %p", device);
183
184         return connman_inet_ifup(ethernet->index);
185 }
186
187 static int ethernet_disable(struct connman_device *device)
188 {
189         struct ethernet_data *ethernet = connman_device_get_data(device);
190
191         DBG("device %p", device);
192
193         return connman_inet_ifdown(ethernet->index);
194 }
195
196 static struct connman_device_driver ethernet_driver = {
197         .name           = "ethernet",
198         .type           = CONNMAN_DEVICE_TYPE_ETHERNET,
199         .probe          = ethernet_probe,
200         .remove         = ethernet_remove,
201         .enable         = ethernet_enable,
202         .disable        = ethernet_disable,
203 };
204
205 static GList *cdc_interface_list = NULL;
206
207 static void tech_add_interface(struct connman_technology *technology,
208                         int index, const char *name, const char *ident)
209 {
210         DBG("index %d name %s ident %s", index, name, ident);
211
212         if (g_list_find(cdc_interface_list,
213                         GINT_TO_POINTER((int) index)) != NULL)
214                 return;
215
216         cdc_interface_list = g_list_prepend(cdc_interface_list,
217                                         (GINT_TO_POINTER((int) index)));
218 }
219
220 static void tech_remove_interface(struct connman_technology *technology,
221                                                                 int index)
222 {
223         DBG("index %d", index);
224
225         cdc_interface_list = g_list_remove(cdc_interface_list,
226                                         GINT_TO_POINTER((int) index));
227 }
228
229 static void enable_tethering(struct connman_technology *technology,
230                                                 const char *bridge)
231 {
232         GList *list;
233
234         for (list = cdc_interface_list; list; list = list->next) {
235                 int index = GPOINTER_TO_INT(list->data);
236
237                 connman_technology_tethering_notify(technology, TRUE);
238
239                 connman_inet_ifup(index);
240
241                 connman_inet_add_to_bridge(index, bridge);
242         }
243 }
244
245 static void disable_tethering(struct connman_technology *technology,
246                                                 const char *bridge)
247 {
248         GList *list;
249
250         for (list = cdc_interface_list; list; list = list->next) {
251                 int index = GPOINTER_TO_INT(list->data);
252
253                 connman_inet_remove_from_bridge(index, bridge);
254
255                 connman_inet_ifdown(index);
256
257                 connman_technology_tethering_notify(technology, FALSE);
258         }
259 }
260
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)
264 {
265         DBG("bridge %s enabled %d", bridge, enabled);
266
267         if (enabled)
268                 enable_tethering(technology, bridge);
269         else
270                 disable_tethering(technology, bridge);
271
272         return 0;
273 }
274
275 static int tech_probe(struct connman_technology *technology)
276 {
277         return 0;
278 }
279
280 static void tech_remove(struct connman_technology *technology)
281 {
282         g_list_free(cdc_interface_list);
283
284         cdc_interface_list = NULL;
285 }
286
287 static struct connman_technology_driver tech_driver = {
288         .name                   = "cdc_ethernet",
289         .type                   = CONNMAN_SERVICE_TYPE_GADGET,
290         .probe                  = tech_probe,
291         .remove                 = tech_remove,
292         .add_interface          = tech_add_interface,
293         .remove_interface       = tech_remove_interface,
294         .set_tethering          = tech_set_tethering,
295 };
296
297 static int ethernet_init(void)
298 {
299         int err;
300
301         err = connman_network_driver_register(&cable_driver);
302         if (err < 0)
303                 return err;
304
305         err = connman_device_driver_register(&ethernet_driver);
306         if (err < 0) {
307                 connman_network_driver_unregister(&cable_driver);
308                 return err;
309         }
310
311         err = connman_technology_driver_register(&tech_driver);
312         if (err < 0) {
313                 connman_device_driver_unregister(&ethernet_driver);
314                 connman_network_driver_unregister(&cable_driver);
315                 return err;
316         }
317
318         return 0;
319 }
320
321 static void ethernet_exit(void)
322 {
323         connman_technology_driver_unregister(&tech_driver);
324
325         connman_network_driver_unregister(&cable_driver);
326
327         connman_device_driver_unregister(&ethernet_driver);
328 }
329
330 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
331                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)