Create devices instead of raw elements
[connman] / plugins / rtnllink.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 <stdio.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <linux/if_arp.h>
33 #include <linux/wireless.h>
34
35 #include <connman/plugin.h>
36 #include <connman/device.h>
37 #include <connman/rtnl.h>
38 #include <connman/log.h>
39
40 #include "inet.h"
41
42 static GSList *device_list = NULL;
43
44 static struct connman_device *find_device(int index)
45 {
46         GSList *list;
47
48         for (list = device_list; list; list = list->next) {
49                 struct connman_device *device = list->data;
50
51                 if (connman_device_get_index(device) == index)
52                         return device;
53         }
54
55         return NULL;
56 }
57
58 static void rtnllink_newlink(unsigned short type, int index,
59                                         unsigned flags, unsigned change)
60 {
61         enum connman_device_type devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
62         struct connman_device *device;
63         gchar *name, *devname;
64
65         DBG("index %d", index);
66
67         device = find_device(index);
68         if (device != NULL)
69                 return;
70
71         devname = inet_index2name(index);
72
73         if (type == ARPHRD_ETHER) {
74                 char bridge_path[PATH_MAX], wimax_path[PATH_MAX];
75                 struct stat st;
76                 struct iwreq iwr;
77                 int sk;
78
79                 snprintf(bridge_path, PATH_MAX,
80                                         "/sys/class/net/%s/bridge", devname);
81                 snprintf(wimax_path, PATH_MAX,
82                                         "/sys/class/net/%s/wimax", devname);
83
84                 memset(&iwr, 0, sizeof(iwr));
85                 strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
86
87                 sk = socket(PF_INET, SOCK_DGRAM, 0);
88
89                 if (g_str_has_prefix(devname, "bnep") == TRUE)
90                         devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
91                 else if (stat(bridge_path, &st) == 0 && (st.st_mode & S_IFDIR))
92                         devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
93                 else if (stat(wimax_path, &st) == 0 && (st.st_mode & S_IFDIR))
94                         devtype = CONNMAN_DEVICE_TYPE_WIMAX;
95                 else if (ioctl(sk, SIOCGIWNAME, &iwr) == 0)
96                         devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
97                 else
98                         devtype = CONNMAN_DEVICE_TYPE_ETHERNET;
99
100                 close(sk);
101         }
102
103         if (devtype == CONNMAN_DEVICE_TYPE_UNKNOWN) {
104                 g_free(devname);
105                 return;
106         }
107
108         name = inet_index2ident(index, "dev_");
109
110         device = connman_device_create(name, devtype);
111         if (device == NULL) {
112                 g_free(devname);
113                 g_free(name);
114                 return;
115         }
116
117         connman_device_set_index(device, index);
118         connman_device_set_interface(device, devname);
119
120         g_free(devname);
121         g_free(name);
122
123         if (connman_device_register(device) < 0) {
124                 connman_device_unref(device);
125                 return;
126         }
127
128         device_list = g_slist_append(device_list, device);
129 }
130
131 static void rtnllink_dellink(unsigned short type, int index,
132                                         unsigned flags, unsigned change)
133 {
134         struct connman_device *device;
135
136         DBG("index %d", index);
137
138         device = find_device(index);
139         if (device == NULL)
140                 return;
141
142         device_list = g_slist_remove(device_list, device);
143
144         connman_device_unregister(device);
145         connman_device_unref(device);
146 }
147
148 static struct connman_rtnl rtnllink_rtnl = {
149         .name           = "rtnllink",
150         .newlink        = rtnllink_newlink,
151         .dellink        = rtnllink_dellink,
152 };
153
154 static int rtnllink_init(void)
155 {
156         int err;
157
158         err = connman_rtnl_register(&rtnllink_rtnl);
159         if (err < 0)
160                 return err;
161
162         connman_rtnl_send_getlink();
163
164         return 0;
165 }
166
167 static void rtnllink_exit(void)
168 {
169         GSList *list;
170
171         connman_rtnl_unregister(&rtnllink_rtnl);
172
173         for (list = device_list; list; list = list->next) {
174                 struct connman_device *device = list->data;
175
176                 connman_device_unregister(device);
177                 connman_device_unref(device);
178         }
179
180         g_slist_free(device_list);
181         device_list = NULL;
182 }
183
184 CONNMAN_PLUGIN_DEFINE(rtnllink, "RTNL link detection plugin", VERSION,
185                                                 rtnllink_init, rtnllink_exit)