summaryrefslogtreecommitdiff
path: root/src/rtnl.c
blob: 33031ffc5f209f9da688c330e0591d5ab3a2fe89 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007  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/socket.h>
#include <arpa/inet.h>

#include <linux/if.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>

#include <glib.h>

#include "connman.h"

static inline void print_char(struct rtattr *attr, const char *name)
{
	printf("  attr %s (len %d) %s\n", name, RTA_PAYLOAD(attr),
						(char *) RTA_DATA(attr));
}

static inline void print_byte(struct rtattr *attr, const char *name)
{
	printf("  attr %s (len %d) 0x%02x\n", name, RTA_PAYLOAD(attr),
					*((unsigned char *) RTA_DATA(attr)));
}

static inline void print_attr(struct rtattr *attr, const char *name)
{
	if (name)
		printf("  attr %s (len %d)\n", name, RTA_PAYLOAD(attr));
	else
		printf("  attr %d (len %d)\n",
					attr->rta_type, RTA_PAYLOAD(attr));
}

static void rtnl_link(struct nlmsghdr *hdr)
{
	struct connman_iface *iface;
	struct ifinfomsg *msg;
	struct rtattr *attr;
	int bytes;

	msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
	bytes = IFLA_PAYLOAD(hdr);

	DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);

	iface = __connman_iface_find(msg->ifi_index);
	if (iface == NULL)
		return;

	if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
		return;

	if (iface->carrier != ((msg->ifi_flags & IFF_RUNNING) != 0)) {
		iface->carrier = ((msg->ifi_flags & IFF_RUNNING) != 0);
		if (iface->driver->rtnl_carrier)
			iface->driver->rtnl_carrier(iface, iface->carrier);
		else
			connman_iface_indicate_carrier(iface, iface->carrier);
	}

	for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
					attr = RTA_NEXT(attr, bytes)) {
		switch (attr->rta_type) {
		case IFLA_ADDRESS:
			print_attr(attr, "address");
			break;
		case IFLA_BROADCAST:
			print_attr(attr, "broadcast");
			break;
		case IFLA_IFNAME:
			print_char(attr, "ifname");
			break;
		case IFLA_MTU:
			print_attr(attr, "mtu");
			break;
		case IFLA_LINK:
			print_attr(attr, "link");
			break;
		case IFLA_QDISC:
			print_attr(attr, "qdisc");
			break;
		case IFLA_STATS:
			print_attr(attr, "stats");
			break;
		case IFLA_COST:
			print_attr(attr, "cost");
			break;
		case IFLA_PRIORITY:
			print_attr(attr, "priority");
			break;
		case IFLA_MASTER:
			print_attr(attr, "master");
			break;
		case IFLA_WIRELESS:
			if (iface->driver->rtnl_wireless)
				iface->driver->rtnl_wireless(iface,
					RTA_DATA(attr), RTA_PAYLOAD(attr));
			break;
		case IFLA_PROTINFO:
			print_attr(attr, "protinfo");
			break;
		case IFLA_TXQLEN:
			print_attr(attr, "txqlen");
			break;
		case IFLA_MAP:
			print_attr(attr, "map");
			break;
		case IFLA_WEIGHT:
			print_attr(attr, "weight");
			break;
		case IFLA_OPERSTATE:
			print_byte(attr, "operstate");
			break;
		case IFLA_LINKMODE:
			print_byte(attr, "linkmode");
			break;
		default:
			print_attr(attr, NULL);
			break;
		}
	}
}

static void rtnl_addr(struct nlmsghdr *hdr)
{
	struct connman_iface *iface;
	struct ifaddrmsg *msg;
	struct rtattr *attr;
	int bytes;

	msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
	bytes = IFA_PAYLOAD(hdr);

	DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);

	iface = __connman_iface_find(msg->ifa_index);
	if (iface == NULL)
		return;

	if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
		return;

	for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
					attr = RTA_NEXT(attr, bytes)) {
		switch (attr->rta_type) {
		case IFA_ADDRESS:
			print_attr(attr, "address");
			if (msg->ifa_family == AF_INET) {
				struct in_addr addr;
				addr = *((struct in_addr *) RTA_DATA(attr));
				DBG("    address %s", inet_ntoa(addr));
			}
			break;
		case IFA_LOCAL:
			print_attr(attr, "local");
			if (msg->ifa_family == AF_INET) {
				struct in_addr addr;
				addr = *((struct in_addr *) RTA_DATA(attr));
				DBG("    address %s", inet_ntoa(addr));
			}
			break;
		case IFA_LABEL:
			print_char(attr, "label");
			break;
		case IFA_BROADCAST:
			print_attr(attr, "broadcast");
			if (msg->ifa_family == AF_INET) {
				struct in_addr addr;
				addr = *((struct in_addr *) RTA_DATA(attr));
				DBG("    address %s", inet_ntoa(addr));
			}
			break;
		case IFA_ANYCAST:
			print_attr(attr, "anycast");
			break;
		case IFA_CACHEINFO:
			print_attr(attr, "cacheinfo");
			break;
		case IFA_MULTICAST:
			print_attr(attr, "multicast");
			break;
		default:
			print_attr(attr, NULL);
			break;
		}
	}
}

static void rtnl_route(struct nlmsghdr *hdr)
{
	struct rtmsg *msg;
	struct rtattr *attr;
	int bytes;

	msg = (struct rtmsg *) NLMSG_DATA(hdr);
	bytes = RTM_PAYLOAD(hdr);

	DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);

	for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
					attr = RTA_NEXT(attr, bytes)) {
		switch (attr->rta_type) {
		case RTA_DST:
			print_attr(attr, "dst");
			if (msg->rtm_family == AF_INET) {
				struct in_addr addr;
				addr = *((struct in_addr *) RTA_DATA(attr));
				DBG("    address %s", inet_ntoa(addr));
			}
			break;
		case RTA_SRC:
			print_attr(attr, "src");
			if (msg->rtm_family == AF_INET) {
				struct in_addr addr;
				addr = *((struct in_addr *) RTA_DATA(attr));
				DBG("    address %s", inet_ntoa(addr));
			}
			break;
		case RTA_IIF:
			print_char(attr, "iif");
			break;
		case RTA_OIF:
			print_attr(attr, "oif");
			break;
		case RTA_GATEWAY:
			print_attr(attr, "gateway");
			if (msg->rtm_family == AF_INET) {
				struct in_addr addr;
				addr = *((struct in_addr *) RTA_DATA(attr));
				DBG("    address %s", inet_ntoa(addr));
			}
			break;
		case RTA_PRIORITY:
			print_attr(attr, "priority");
			break;
		case RTA_PREFSRC:
			print_attr(attr, "prefsrc");
			if (msg->rtm_family == AF_INET) {
				struct in_addr addr;
				addr = *((struct in_addr *) RTA_DATA(attr));
				DBG("    address %s", inet_ntoa(addr));
			}
			break;
		case RTA_METRICS:
			print_attr(attr, "metrics");
			break;
		case RTA_TABLE:
			print_attr(attr, "table");
			break;
		default:
			print_attr(attr, NULL);
			break;
		}
	}
}

static void rtnl_message(unsigned char *buf, size_t size)
{
	struct nlmsghdr *hdr = (void *) buf;

	if (!NLMSG_OK(hdr, size))
		return;

	switch (hdr->nlmsg_type) {
	case NLMSG_DONE:
		DBG("done");
		return;
	case NLMSG_NOOP:
		DBG("noop");
		return;
	case NLMSG_OVERRUN:
		DBG("overrun");
		return;
	case NLMSG_ERROR:
		DBG("error");
		return;
	case RTM_NEWLINK:
		rtnl_link(hdr);
		break;
	case RTM_DELLINK:
		rtnl_link(hdr);
		break;
	case RTM_NEWADDR:
		rtnl_addr(hdr);
		break;
	case RTM_DELADDR:
		rtnl_addr(hdr);
		break;
	case RTM_NEWROUTE:
		rtnl_route(hdr);
		break;
	case RTM_DELROUTE:
		rtnl_route(hdr);
		break;
	default:
		DBG("type %d", hdr->nlmsg_type);
		break;
	}
}

static gboolean netlink_event(GIOChannel *chan,
				GIOCondition cond, gpointer data)
{
	unsigned char buf[256];
	gsize len;
	GIOError err;

	if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
		g_io_channel_unref(chan);
		return FALSE;
	}

	memset(buf, 0, sizeof(buf));

	err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
	if (err) {
		if (err == G_IO_ERROR_AGAIN)
			return TRUE;
		g_io_channel_unref(chan);
		return FALSE;
	}

	rtnl_message(buf, len);

	return TRUE;
}

static GIOChannel *channel = NULL;

int __connman_rtnl_init(void)
{
	struct sockaddr_nl addr;
	int sk;

	DBG("");

	sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
	if (sk < 0)
		return -1;

	memset(&addr, 0, sizeof(addr));
	addr.nl_family = AF_NETLINK;
	addr.nl_groups = RTMGRP_LINK;
	//addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
	addr.nl_pid = getpid();

	if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		close(sk);
		return -1;
	}

	channel = g_io_channel_unix_new(sk);
	g_io_channel_set_close_on_unref(channel, TRUE);

	g_io_add_watch(channel,
			G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
						netlink_event, NULL);

	g_io_channel_unref(channel);

	return 0;
}

void __connman_rtnl_cleanup(void)
{
	DBG("");

	g_io_channel_unref(channel);

	channel = NULL;
}