Use RTNL newlink watch to reduce code complexity
[platform/upstream/connman.git] / plugins / ethernet.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 #define CONNMAN_API_SUBJECT_TO_CHANGE
38 #include <connman/plugin.h>
39 #include <connman/device.h>
40 #include <connman/rtnl.h>
41 #include <connman/log.h>
42
43 #include "inet.h"
44
45 struct ethernet_data {
46         int index;
47         unsigned flags;
48         unsigned int watch;
49 };
50
51 static void ethernet_newlink(unsigned flags, unsigned change, void *user_data)
52 {
53         struct connman_device *device = user_data;
54         struct ethernet_data *ethernet = connman_device_get_data(device);
55
56         DBG("index %d flags %ld change %ld", ethernet->index, flags, change);
57
58         if ((ethernet->flags & IFF_UP) != (flags & IFF_UP)) {
59                 if (flags & IFF_UP) {
60                         DBG("power on");
61                         connman_device_set_powered(device, TRUE);
62                 } else {
63                         DBG("power off");
64                         connman_device_set_powered(device, FALSE);
65                 }
66         }
67
68         if ((ethernet->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
69                 if (flags & IFF_LOWER_UP) {
70                         DBG("carrier on");
71                         connman_device_set_carrier(device, TRUE);
72                 } else {
73                         DBG("carrier off");
74                         connman_device_set_carrier(device, FALSE);
75                 }
76         }
77
78         ethernet->flags = flags;
79 }
80
81 static int ethernet_probe(struct connman_device *device)
82 {
83         struct ethernet_data *ethernet;
84
85         DBG("device %p", device);
86
87         ethernet = g_try_new0(struct ethernet_data, 1);
88         if (ethernet == NULL)
89                 return -ENOMEM;
90
91         connman_device_set_data(device, ethernet);
92
93         ethernet->index = connman_device_get_index(device);
94         ethernet->flags = 0;
95
96         ethernet->watch = connman_rtnl_add_newlink_watch(ethernet->index,
97                                                 ethernet_newlink, device);
98
99         connman_rtnl_send_getlink();
100
101         return 0;
102 }
103
104 static void ethernet_remove(struct connman_device *device)
105 {
106         struct ethernet_data *ethernet = connman_device_get_data(device);
107
108         DBG("device %p", device);
109
110         connman_device_set_data(device, NULL);
111
112         connman_rtnl_remove_watch(ethernet->watch);
113
114         g_free(ethernet);
115 }
116
117 static int ethernet_enable(struct connman_device *device)
118 {
119         struct ethernet_data *ethernet = connman_device_get_data(device);
120
121         DBG("device %p", device);
122
123         return inet_ifup(ethernet->index);
124 }
125
126 static int ethernet_disable(struct connman_device *device)
127 {
128         struct ethernet_data *ethernet = connman_device_get_data(device);
129
130         DBG("device %p", device);
131
132         return inet_ifdown(ethernet->index);
133 }
134
135 static struct connman_device_driver ethernet_driver = {
136         .name           = "ethernet",
137         .type           = CONNMAN_DEVICE_TYPE_ETHERNET,
138         .probe          = ethernet_probe,
139         .remove         = ethernet_remove,
140         .enable         = ethernet_enable,
141         .disable        = ethernet_disable,
142 };
143
144 static int ethernet_init(void)
145 {
146         return connman_device_driver_register(&ethernet_driver);
147 }
148
149 static void ethernet_exit(void)
150 {
151         connman_device_driver_unregister(&ethernet_driver);
152 }
153
154 CONNMAN_PLUGIN_DEFINE(ethernet, "Ethernet interface plugin", VERSION,
155                                                 ethernet_init, ethernet_exit)