Use device helpers to set power and carrier states
[connman] / plugins / ethernet.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  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 <unistd.h>
27 #include <string.h>
28 #include <sys/ioctl.h>
29 #include <sys/socket.h>
30 #include <linux/if.h>
31 #include <linux/netlink.h>
32 #include <linux/rtnetlink.h>
33
34 #include <connman/plugin.h>
35 #include <connman/device.h>
36 #include <connman/rtnl.h>
37 #include <connman/log.h>
38
39 struct ethernet_data {
40         int index;
41         unsigned flags;
42 };
43
44 static GSList *ethernet_list = NULL;
45
46 static void ethernet_newlink(unsigned short type, int index,
47                                         unsigned flags, unsigned change)
48 {
49         GSList *list;
50
51         DBG("index %d flags %ld change %ld", index, flags, change);
52
53         for (list = ethernet_list; list; list = list->next) {
54                 struct connman_device *device = list->data;
55                 struct ethernet_data *ethernet;
56
57                 ethernet = connman_device_get_data(device);
58                 if (ethernet == NULL)
59                         continue;
60
61                 if (ethernet->index != index)
62                         continue;
63
64                 if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
65                         if (flags & IFF_UP) {
66                                 DBG("power on");
67                                 connman_device_set_powered(device, TRUE);
68                         } else {
69                                 DBG("power off");
70                                 connman_device_set_powered(device, FALSE);
71                         }
72                 }
73
74                 if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
75                         if (flags & IFF_LOWER_UP) {
76                                 DBG("carrier on");
77                                 connman_device_set_carrier(device, TRUE);
78                         } else {
79                                 DBG("carrier off");
80                                 connman_device_set_carrier(device, FALSE);
81                         }
82                 }
83
84                 ethernet->flags = flags;
85         }
86 }
87
88 static struct connman_rtnl ethernet_rtnl = {
89         .name           = "ethernet",
90         .newlink        = ethernet_newlink,
91 };
92
93 static int iface_up(struct ethernet_data *ethernet)
94 {
95         struct ifreq ifr;
96         int sk, err;
97
98         DBG("index %d flags %d", ethernet->index, ethernet->flags);
99
100         sk = socket(PF_INET, SOCK_DGRAM, 0);
101         if (sk < 0)
102                 return -errno;
103
104         memset(&ifr, 0, sizeof(ifr));
105         ifr.ifr_ifindex = ethernet->index;
106
107         if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
108                 err = -errno;
109                 goto done;
110         }
111
112         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
113                 err = -errno;
114                 goto done;
115         }
116
117         if (ifr.ifr_flags & IFF_UP) {
118                 err = -EALREADY;
119                 goto done;
120         }
121
122         ifr.ifr_flags |= IFF_UP;
123
124         if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
125                 err = -errno;
126                 goto done;
127         }
128
129         err = 0;
130
131 done:
132         close(sk);
133
134         return err;
135 }
136
137 static int iface_down(struct ethernet_data *ethernet)
138 {
139         struct ifreq ifr;
140         int sk, err;
141
142         DBG("index %d flags %d", ethernet->index, ethernet->flags);
143
144         sk = socket(PF_INET, SOCK_DGRAM, 0);
145         if (sk < 0)
146                 return -errno;
147
148         memset(&ifr, 0, sizeof(ifr));
149         ifr.ifr_ifindex = ethernet->index;
150
151         if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
152                 err = -errno;
153                 goto done;
154         }
155
156         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
157                 err = -errno;
158                 goto done;
159         }
160
161         if (!(ifr.ifr_flags & IFF_UP)) {
162                 err = -EALREADY;
163                 goto done;
164         }
165
166         ifr.ifr_flags &= ~IFF_UP;
167
168         if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
169                 err = -errno;
170         else
171                 err = 0;
172
173 done:
174         close(sk);
175
176         return err;
177 }
178
179 static int ethernet_probe(struct connman_device *device)
180 {
181         struct ethernet_data *ethernet;
182
183         DBG("device %p", device);
184
185         ethernet = g_try_new0(struct ethernet_data, 1);
186         if (ethernet == NULL)
187                 return -ENOMEM;
188
189         ethernet_list = g_slist_append(ethernet_list, device);
190
191         connman_device_set_data(device, ethernet);
192
193         ethernet->index = connman_device_get_index(device);
194
195         connman_rtnl_send_getlink();
196
197         return 0;
198 }
199
200 static void ethernet_remove(struct connman_device *device)
201 {
202         struct ethernet_data *ethernet = connman_device_get_data(device);
203
204         DBG("device %p", device);
205
206         connman_device_set_data(device, NULL);
207
208         ethernet_list = g_slist_remove(ethernet_list, device);
209
210         g_free(ethernet);
211 }
212
213 static int ethernet_enable(struct connman_device *device)
214 {
215         struct ethernet_data *ethernet = connman_device_get_data(device);
216
217         DBG("device %p", device);
218
219         return iface_up(ethernet);
220 }
221
222 static int ethernet_disable(struct connman_device *device)
223 {
224         struct ethernet_data *ethernet = connman_device_get_data(device);
225
226         DBG("device %p", device);
227
228         return iface_down(ethernet);
229 }
230
231 static struct connman_device_driver ethernet_driver = {
232         .name           = "ethernet",
233         .type           = CONNMAN_DEVICE_TYPE_ETHERNET,
234         .probe          = ethernet_probe,
235         .remove         = ethernet_remove,
236         .enable         = ethernet_enable,
237         .disable        = ethernet_disable,
238 };
239
240 static int ethernet_init(void)
241 {
242         int err;
243
244         err = connman_rtnl_register(&ethernet_rtnl);
245         if (err < 0)
246                 return err;
247
248         err = connman_device_driver_register(&ethernet_driver);
249         if (err < 0) {
250                 connman_rtnl_unregister(&ethernet_rtnl);
251                 return err;
252         }
253
254         return 0;
255 }
256
257 static void ethernet_exit(void)
258 {
259         connman_device_driver_unregister(&ethernet_driver);
260
261         connman_rtnl_unregister(&ethernet_rtnl);
262 }
263
264 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
265                                                 ethernet_init, ethernet_exit)