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