7b5ca41ace32208a9535cc6c64cb56ccec8d2797
[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 <errno.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <linux/if.h>
32 #include <linux/netlink.h>
33 #include <linux/rtnetlink.h>
34
35 #include <glib.h>
36
37 #include <connman/plugin.h>
38 #include <connman/device.h>
39 #include <connman/rtnl.h>
40 #include <connman/log.h>
41
42 #include "inet.h"
43
44 struct ethernet_data {
45         int index;
46         unsigned flags;
47 };
48
49 static GSList *ethernet_list = NULL;
50
51 static void ethernet_newlink(unsigned short type, int index,
52                                         unsigned flags, unsigned change)
53 {
54         GSList *list;
55
56         DBG("index %d flags %ld change %ld", index, flags, change);
57
58         for (list = ethernet_list; list; list = list->next) {
59                 struct connman_device *device = list->data;
60                 struct ethernet_data *ethernet;
61
62                 ethernet = connman_device_get_data(device);
63                 if (ethernet == NULL)
64                         continue;
65
66                 if (ethernet->index != index)
67                         continue;
68
69                 if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
70                         if (flags & IFF_UP) {
71                                 DBG("power on");
72                                 connman_device_set_powered(device, TRUE);
73                         } else {
74                                 DBG("power off");
75                                 connman_device_set_powered(device, FALSE);
76                         }
77                 }
78
79                 if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
80                         if (flags & IFF_LOWER_UP) {
81                                 DBG("carrier on");
82                                 connman_device_set_carrier(device, TRUE);
83                         } else {
84                                 DBG("carrier off");
85                                 connman_device_set_carrier(device, FALSE);
86                         }
87                 }
88
89                 ethernet->flags = flags;
90         }
91 }
92
93 static struct connman_rtnl ethernet_rtnl = {
94         .name           = "ethernet",
95         .newlink        = ethernet_newlink,
96 };
97
98 static int ethernet_probe(struct connman_device *device)
99 {
100         struct ethernet_data *ethernet;
101
102         DBG("device %p", device);
103
104         ethernet = g_try_new0(struct ethernet_data, 1);
105         if (ethernet == NULL)
106                 return -ENOMEM;
107
108         ethernet_list = g_slist_append(ethernet_list, device);
109
110         connman_device_set_data(device, ethernet);
111
112         ethernet->index = connman_device_get_index(device);
113
114         connman_rtnl_send_getlink();
115
116         return 0;
117 }
118
119 static void ethernet_remove(struct connman_device *device)
120 {
121         struct ethernet_data *ethernet = connman_device_get_data(device);
122
123         DBG("device %p", device);
124
125         connman_device_set_data(device, NULL);
126
127         ethernet_list = g_slist_remove(ethernet_list, device);
128
129         g_free(ethernet);
130 }
131
132 static int ethernet_enable(struct connman_device *device)
133 {
134         struct ethernet_data *ethernet = connman_device_get_data(device);
135
136         DBG("device %p", device);
137
138         return inet_ifup(ethernet->index);
139 }
140
141 static int ethernet_disable(struct connman_device *device)
142 {
143         struct ethernet_data *ethernet = connman_device_get_data(device);
144
145         DBG("device %p", device);
146
147         return inet_ifdown(ethernet->index);
148 }
149
150 static struct connman_device_driver ethernet_driver = {
151         .name           = "ethernet",
152         .type           = CONNMAN_DEVICE_TYPE_ETHERNET,
153         .probe          = ethernet_probe,
154         .remove         = ethernet_remove,
155         .enable         = ethernet_enable,
156         .disable        = ethernet_disable,
157 };
158
159 static int ethernet_init(void)
160 {
161         int err;
162
163         err = connman_rtnl_register(&ethernet_rtnl);
164         if (err < 0)
165                 return err;
166
167         err = connman_device_driver_register(&ethernet_driver);
168         if (err < 0) {
169                 connman_rtnl_unregister(&ethernet_rtnl);
170                 return err;
171         }
172
173         return 0;
174 }
175
176 static void ethernet_exit(void)
177 {
178         connman_device_driver_unregister(&ethernet_driver);
179
180         connman_rtnl_unregister(&ethernet_rtnl);
181 }
182
183 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
184                                                 ethernet_init, ethernet_exit)