Fix bridge and WiMAX device detection
[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/element.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 void rtnllink_newlink(unsigned short type, int index,
45                                         unsigned flags, unsigned change)
46 {
47         enum connman_element_subtype subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
48         struct connman_element *device;
49         GSList *list;
50         gboolean exists = FALSE;
51         gchar *name, *devname;
52
53         DBG("index %d", index);
54
55         for (list = device_list; list; list = list->next) {
56                 struct connman_element *device = list->data;
57
58                 if (device->index == index) {
59                         exists = TRUE;
60                         break;
61                 }
62         }
63
64         if (exists == TRUE)
65                 return;
66
67         name = inet_index2ident(index, "dev_");
68         devname = inet_index2name(index);
69
70         if (type == ARPHRD_ETHER) {
71                 char bridge_path[PATH_MAX], wimax_path[PATH_MAX];
72                 struct stat st;
73                 struct iwreq iwr;
74                 int sk;
75
76                 snprintf(bridge_path, PATH_MAX,
77                                         "/sys/class/net/%s/bridge", devname);
78                 snprintf(wimax_path, PATH_MAX,
79                                         "/sys/class/net/%s/wimax", devname);
80
81                 memset(&iwr, 0, sizeof(iwr));
82                 strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
83
84                 sk = socket(PF_INET, SOCK_DGRAM, 0);
85
86                 if (g_str_has_prefix(name, "bnep") == TRUE)
87                         subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
88                 else if (stat(bridge_path, &st) == 0 && (st.st_mode & S_IFDIR))
89                         subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
90                 else if (stat(wimax_path, &st) == 0 && (st.st_mode & S_IFDIR))
91                         subtype = CONNMAN_ELEMENT_SUBTYPE_WIMAX;
92                 else if (ioctl(sk, SIOCGIWNAME, &iwr) == 0)
93                         subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
94                 else
95                         subtype = CONNMAN_ELEMENT_SUBTYPE_ETHERNET;
96
97                 close(sk);
98         }
99
100         if (subtype == CONNMAN_ELEMENT_SUBTYPE_UNKNOWN) {
101                 g_free(name);
102                 return;
103         }
104
105         device = connman_element_create(NULL);
106         device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
107         device->subtype = subtype;
108
109         device->index = index;
110         device->name = name;
111         device->devname = devname;
112
113         connman_element_register(device, NULL);
114         device_list = g_slist_append(device_list, device);
115 }
116
117 static void rtnllink_dellink(unsigned short type, int index,
118                                         unsigned flags, unsigned change)
119 {
120         GSList *list;
121
122         DBG("index %d", index);
123
124         for (list = device_list; list; list = list->next) {
125                 struct connman_element *device = list->data;
126
127                 if (device->index == index) {
128                         device_list = g_slist_remove(device_list, device);
129                         connman_element_unregister(device);
130                         connman_element_unref(device);
131                         break;
132                 }
133         }
134 }
135
136 static struct connman_rtnl rtnllink_rtnl = {
137         .name           = "rtnllink",
138         .newlink        = rtnllink_newlink,
139         .dellink        = rtnllink_dellink,
140 };
141
142 static int rtnllink_init(void)
143 {
144         int err;
145
146         err = connman_rtnl_register(&rtnllink_rtnl);
147         if (err < 0)
148                 return err;
149
150         connman_rtnl_send_getlink();
151
152         return 0;
153 }
154
155 static void rtnllink_exit(void)
156 {
157         GSList *list;
158
159         connman_rtnl_unregister(&rtnllink_rtnl);
160
161         for (list = device_list; list; list = list->next) {
162                 struct connman_element *device = list->data;
163
164                 connman_element_unregister(device);
165                 connman_element_unref(device);
166         }
167
168         g_slist_free(device_list);
169         device_list = NULL;
170 }
171
172 CONNMAN_PLUGIN_DEFINE(rtnllink, "RTNL link detection plugin", VERSION,
173                                                 rtnllink_init, rtnllink_exit)