Make use of the RTNL framework
[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/driver.h>
36 #include <connman/log.h>
37
38 struct ethernet_data {
39         int index;
40         short flags;
41 };
42
43 static GStaticMutex ethernet_mutex = G_STATIC_MUTEX_INIT;
44 static GSList *ethernet_list = NULL;
45
46 static void ethernet_link_flags(int index, short flags)
47 {
48         GSList *list;
49
50         g_static_mutex_lock(&ethernet_mutex);
51
52         for (list = ethernet_list; list; list = list->next) {
53                 struct connman_element *element = list->data;
54                 struct connman_element *netdev;
55                 struct ethernet_data *ethernet;
56
57                 ethernet = connman_element_get_data(element);
58                 if (ethernet == NULL)
59                         continue;
60
61                 if (ethernet->index != index)
62                         continue;
63
64                 if ((ethernet->flags & IFF_RUNNING) == (flags & IFF_RUNNING))
65                         continue;
66
67                 ethernet->flags = flags;
68
69                 if (ethernet->flags & IFF_RUNNING) {
70                         DBG("carrier on");
71
72                         netdev = connman_element_create(NULL);
73                         if (netdev != NULL) {
74                                 netdev->type    = CONNMAN_ELEMENT_TYPE_DEVICE;
75                                 netdev->subtype = CONNMAN_ELEMENT_SUBTYPE_NETWORK;
76                                 netdev->index   = element->index;
77
78                                 connman_element_register(netdev, element);
79                         }
80                 } else {
81                         DBG("carrier off");
82
83                         connman_element_unregister_children(element);
84                 }
85         }
86
87         g_static_mutex_unlock(&ethernet_mutex);
88 }
89
90 static struct connman_rtnl ethernet_rtnl = {
91         .name           = "ethernet",
92         .link_flags     = ethernet_link_flags,
93 };
94
95 static int iface_up(struct ethernet_data *ethernet)
96 {
97         struct ifreq ifr;
98         int sk, err;
99
100         DBG("index %d flags %d", ethernet->index, ethernet->flags);
101
102         sk = socket(PF_INET, SOCK_DGRAM, 0);
103         if (sk < 0)
104                 return -errno;
105
106         memset(&ifr, 0, sizeof(ifr));
107         ifr.ifr_ifindex = ethernet->index;
108
109         if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
110                 err = -errno;
111                 goto done;
112         }
113
114         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
115                 err = -errno;
116                 goto done;
117         }
118
119         if (ifr.ifr_flags & IFF_UP) {
120                 err = -EALREADY;
121                 goto done;
122         }
123
124         ifr.ifr_flags |= IFF_UP;
125
126         if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
127                 err = -errno;
128                 goto done;
129         }
130
131         err = 0;
132
133 done:
134         close(sk);
135
136         return err;
137 }
138
139 static int iface_down(struct ethernet_data *ethernet)
140 {
141         struct ifreq ifr;
142         int sk, err;
143
144         DBG("index %d flags %d", ethernet->index, ethernet->flags);
145
146         sk = socket(PF_INET, SOCK_DGRAM, 0);
147         if (sk < 0)
148                 return -errno;
149
150         memset(&ifr, 0, sizeof(ifr));
151         ifr.ifr_ifindex = ethernet->index;
152
153         if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
154                 err = -errno;
155                 goto done;
156         }
157
158         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
159                 err = -errno;
160                 goto done;
161         }
162
163         if (!(ifr.ifr_flags & IFF_UP)) {
164                 err = -EALREADY;
165                 goto done;
166         }
167
168         ifr.ifr_flags &= ~IFF_UP;
169
170         if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
171                 err = -errno;
172         else
173                 err = 0;
174
175 done:
176         close(sk);
177
178         return err;
179 }
180
181 static int ethernet_probe(struct connman_element *element)
182 {
183         struct ethernet_data *ethernet;
184
185         DBG("element %p name %s", element, element->name);
186
187         ethernet = g_try_new0(struct ethernet_data, 1);
188         if (ethernet == NULL)
189                 return -ENOMEM;
190
191         g_static_mutex_lock(&ethernet_mutex);
192         ethernet_list = g_slist_append(ethernet_list, element);
193         g_static_mutex_unlock(&ethernet_mutex);
194
195         connman_element_set_data(element, ethernet);
196
197         ethernet->index = element->index;
198
199         iface_up(ethernet);
200
201         connman_rtnl_send_getlink();
202
203         return 0;
204 }
205
206 static void ethernet_remove(struct connman_element *element)
207 {
208         struct ethernet_data *ethernet = connman_element_get_data(element);
209
210         DBG("element %p name %s", element, element->name);
211
212         connman_element_set_data(element, NULL);
213
214         iface_down(ethernet);
215
216         g_static_mutex_lock(&ethernet_mutex);
217         ethernet_list = g_slist_remove(ethernet_list, element);
218         g_static_mutex_unlock(&ethernet_mutex);
219
220         g_free(ethernet);
221 }
222
223 static struct connman_driver ethernet_driver = {
224         .name           = "ethernet",
225         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
226         .subtype        = CONNMAN_ELEMENT_SUBTYPE_ETHERNET,
227         .probe          = ethernet_probe,
228         .remove         = ethernet_remove,
229 };
230
231 static int ethernet_init(void)
232 {
233         int err;
234
235         err = connman_rtnl_register(&ethernet_rtnl);
236         if (err < 0)
237                 return err;
238
239         err = connman_driver_register(&ethernet_driver);
240         if (err < 0) {
241                 connman_rtnl_unregister(&ethernet_rtnl):
242                 return err;
243         }
244
245         return 0;
246 }
247
248 static void ethernet_exit(void)
249 {
250         connman_driver_unregister(&ethernet_driver);
251
252         connman_rtnl_unregister(&ethernet_rtnl);
253 }
254
255 CONNMAN_PLUGIN_DEFINE("ethernet", "Ethernet interface plugin", VERSION,
256                                                 ethernet_init, ethernet_exit)