summaryrefslogtreecommitdiff
path: root/plugins/rtnllink.c
blob: 14bb34374823448eb2675049dfbc2d9950415ce3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if_arp.h>
#include <linux/wireless.h>

#include <connman/plugin.h>
#include <connman/element.h>
#include <connman/rtnl.h>
#include <connman/log.h>

#include "inet.h"

static GStaticMutex device_mutex = G_STATIC_MUTEX_INIT;
static GSList *device_list = NULL;

static void rtnllink_newlink(unsigned short type, int index,
					unsigned flags, unsigned change)
{
	enum connman_element_subtype subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
	struct connman_element *device;
	GSList *list;
	gboolean exists = FALSE;
	gchar *name;

	DBG("index %d", index);

	g_static_mutex_lock(&device_mutex);

	for (list = device_list; list; list = list->next) {
		struct connman_element *device = list->data;

		if (device->index == index) {
			exists = TRUE;
			break;
		}
	}

	g_static_mutex_unlock(&device_mutex);

	if (exists == TRUE)
		return;

	name = inet_index2name(index);

	if (type == ARPHRD_ETHER) {
		char bridge_path[PATH_MAX], wimax_path[PATH_MAX];
		struct stat st;
		struct iwreq iwr;
		int sk;

		snprintf(bridge_path, PATH_MAX,
					"/sys/class/net/%s/bridge", name);
		snprintf(wimax_path, PATH_MAX,
					"/sys/class/net/%s/wimax", name);

		memset(&iwr, 0, sizeof(iwr));
		strncpy(iwr.ifr_ifrn.ifrn_name, name, IFNAMSIZ);

		sk = socket(PF_INET, SOCK_DGRAM, 0);

		if (g_str_has_prefix(name, "bnep") == TRUE)
			subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
		else if (stat(bridge_path, &st) == 0 && (st.st_mode & S_IFDIR))
			subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
		else if (stat(wimax_path, &st) == 0 && (st.st_mode & S_IFDIR))
			subtype = CONNMAN_ELEMENT_SUBTYPE_WIMAX;
		else if (ioctl(sk, SIOCGIWNAME, &iwr) == 0)
			subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
		else
			subtype = CONNMAN_ELEMENT_SUBTYPE_ETHERNET;

		close(sk);
	}

	if (subtype == CONNMAN_ELEMENT_SUBTYPE_UNKNOWN) {
		g_free(name);
		return;
	}

	device = connman_element_create(NULL);
	device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
	device->subtype = subtype;

	device->index = index;
	device->name = name;

	g_static_mutex_lock(&device_mutex);

	connman_element_register(device, NULL);
	device_list = g_slist_append(device_list, device);

	g_static_mutex_unlock(&device_mutex);
}

static void rtnllink_dellink(unsigned short type, int index,
					unsigned flags, unsigned change)
{
	GSList *list;

	DBG("index %d", index);

	g_static_mutex_lock(&device_mutex);

	for (list = device_list; list; list = list->next) {
		struct connman_element *device = list->data;

		if (device->index == index) {
			device_list = g_slist_remove(device_list, device);
			connman_element_unregister(device);
			connman_element_unref(device);
			break;
		}
	}

	g_static_mutex_unlock(&device_mutex);
}

static struct connman_rtnl rtnllink_rtnl = {
	.name		= "rtnllink",
	.newlink	= rtnllink_newlink,
	.dellink	= rtnllink_dellink,
};

static int rtnllink_init(void)
{
	int err;

	err = connman_rtnl_register(&rtnllink_rtnl);
	if (err < 0)
		return err;

	connman_rtnl_send_getlink();

	return 0;
}

static void rtnllink_exit(void)
{
	GSList *list;

	connman_rtnl_unregister(&rtnllink_rtnl);

	g_static_mutex_lock(&device_mutex);

	for (list = device_list; list; list = list->next) {
		struct connman_element *device = list->data;

		connman_element_unregister(device);
		connman_element_unref(device);
	}

	g_slist_free(device_list);
	device_list = NULL;

	g_static_mutex_unlock(&device_mutex);
}

CONNMAN_PLUGIN_DEFINE("rtnllink", "RTNL link detection plugin", VERSION,
						rtnllink_init, rtnllink_exit)