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