Add some ipconfig helpers
[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/plugin.h>
37 #include <connman/device.h>
38 #include <connman/inet.h>
39 #include <connman/rtnl.h>
40 #include <connman/log.h>
41
42 struct ethernet_data {
43         int index;
44         unsigned flags;
45         unsigned int watch;
46         struct connman_network *network;
47 };
48
49 static int cable_probe(struct connman_network *network)
50 {
51         DBG("network %p", network);
52
53         return 0;
54 }
55
56 static void cable_remove(struct connman_network *network)
57 {
58         DBG("network %p", network);
59 }
60
61 static int cable_connect(struct connman_network *network)
62 {
63         DBG("network %p", network);
64
65         return 0;
66 }
67
68 static int cable_disconnect(struct connman_network *network)
69 {
70         DBG("network %p", network);
71
72         return 0;
73 }
74
75 static struct connman_network_driver cable_driver = {
76         .name           = "cable",
77         .type           = CONNMAN_NETWORK_TYPE_ETHERNET,
78         .probe          = cable_probe,
79         .remove         = cable_remove,
80         .connect        = cable_connect,
81         .disconnect     = cable_disconnect,
82 };
83
84 static void add_network(struct connman_device *device)
85 {
86         struct connman_network *network;
87         int index;
88
89         network = connman_network_create("carrier",
90                                         CONNMAN_NETWORK_TYPE_ETHERNET);
91         if (network == NULL)
92                 return;
93
94         index = connman_device_get_index(device);
95         connman_network_set_index(network, index);
96
97         connman_network_set_name(network, "Wired");
98         connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
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_method(network, CONNMAN_IPCONFIG_METHOD_DHCP);
110
111         connman_network_set_connected(network, TRUE);
112 }
113
114 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
115 {
116         struct connman_device *device = user_data;
117         struct ethernet_data *ethernet = connman_device_get_data(device);
118
119         DBG("index %d flags %d change %d", ethernet->index, flags, change);
120
121         if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
122                 if (flags & IFF_UP) {
123                         DBG("power on");
124                         connman_device_set_powered(device, TRUE);
125                 } else {
126                         DBG("power off");
127                         connman_device_set_powered(device, FALSE);
128                 }
129         }
130
131         if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
132                 if (flags & IFF_LOWER_UP) {
133                         DBG("carrier on");
134                         add_network(device);
135                 } else {
136                         DBG("carrier off");
137                         connman_device_remove_all_networks(device);
138                 }
139         }
140
141         ethernet->flags = flags;
142 }
143
144 static int ethernet_probe(struct connman_device *device)
145 {
146         struct ethernet_data *ethernet;
147
148         DBG("device %p", device);
149
150         ethernet = g_try_new0(struct ethernet_data, 1);
151         if (ethernet == NULL)
152                 return -ENOMEM;
153
154         connman_device_set_data(device, ethernet);
155
156         ethernet->index = connman_device_get_index(device);
157         ethernet->flags = 0;
158
159         ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
160                                                 ethernet_newlink, device);
161
162         return 0;
163 }
164
165 static void ethernet_remove(struct connman_device *device)
166 {
167         struct ethernet_data *ethernet = connman_device_get_data(device);
168
169         DBG("device %p", device);
170
171         connman_device_set_data(device, NULL);
172
173         connman_rtnl_remove_watch(ethernet->watch);
174
175         connman_device_remove_all_networks(device);
176
177         g_free(ethernet);
178 }
179
180 static int ethernet_enable(struct connman_device *device)
181 {
182         struct ethernet_data *ethernet = connman_device_get_data(device);
183
184         DBG("device %p", device);
185
186         return connman_inet_ifup(ethernet->index);
187 }
188
189 static int ethernet_disable(struct connman_device *device)
190 {
191         struct ethernet_data *ethernet = connman_device_get_data(device);
192
193         DBG("device %p", device);
194
195         return connman_inet_ifdown(ethernet->index);
196 }
197
198 static struct connman_device_driver ethernet_driver = {
199         .name           = "ethernet",
200         .type           = CONNMAN_DEVICE_TYPE_ETHERNET,
201         .probe          = ethernet_probe,
202         .remove         = ethernet_remove,
203         .enable         = ethernet_enable,
204         .disable        = ethernet_disable,
205 };
206
207 static int ethernet_init(void)
208 {
209         int err;
210
211         err = connman_network_driver_register(&cable_driver);
212         if (err < 0)
213                 return err;
214
215         err = connman_device_driver_register(&ethernet_driver);
216         if (err < 0) {
217                 connman_network_driver_unregister(&cable_driver);
218                 return err;
219         }
220
221         return 0;
222 }
223
224 static void ethernet_exit(void)
225 {
226         connman_network_driver_unregister(&cable_driver);
227
228         connman_device_driver_unregister(&ethernet_driver);
229 }
230
231 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
232                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ethernet_init, ethernet_exit)