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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
/*
*
* 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 <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 GStaticRWLock rtnl_lock = G_STATIC_RW_LOCK_INIT;
static GSList *rtnl_list = NULL;
static gint compare_priority(gconstpointer a, gconstpointer b)
{
const struct connman_rtnl *rtnl1 = a;
const struct connman_rtnl *rtnl2 = b;
return rtnl2->priority - rtnl1->priority;
}
/**
* connman_rtnl_register:
* @rtnl: RTNL module
*
* Register a new RTNL module
*
* Returns: %0 on success
*/
int connman_rtnl_register(struct connman_rtnl *rtnl)
{
DBG("rtnl %p name %s", rtnl, rtnl->name);
g_static_rw_lock_writer_lock(&rtnl_lock);
rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
compare_priority);
g_static_rw_lock_writer_unlock(&rtnl_lock);
return 0;
}
/**
* connman_rtnl_unregister:
* @rtnl: RTNL module
*
* Remove a previously registered RTNL module
*/
void connman_rtnl_unregister(struct connman_rtnl *rtnl)
{
DBG("rtnl %p name %s", rtnl, rtnl->name);
g_static_rw_lock_writer_lock(&rtnl_lock);
rtnl_list = g_slist_remove(rtnl_list, rtnl);
g_static_rw_lock_writer_unlock(&rtnl_lock);
}
static void process_link_flags(int flags)
{
GSList *list;
DBG("idex %d", index);
g_static_rw_lock_reader_lock(&rtnl_lock);
for (list = rtnl_list; list; list = list->next) {
struct connman_rtnl *rtnl = list->data;
if (rtnl->link_flags)
rtnl->link_flags(index, flags);
}
g_static_rw_lock_reader_unlock(&rtnl_lock);
}
static inline void print_inet(struct rtattr *attr, const char *name, int family)
{
if (family == AF_INET) {
struct in_addr addr;
addr = *((struct in_addr *) RTA_DATA(attr));
DBG(" attr %s (len %jd) %s\n",
name, RTA_PAYLOAD(attr), inet_ntoa(addr));
} else
DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
}
static inline void print_char(struct rtattr *attr, const char *name)
{
DBG(" attr %s (len %jd) %s\n", name, RTA_PAYLOAD(attr),
(char *) RTA_DATA(attr));
}
static inline void print_byte(struct rtattr *attr, const char *name)
{
DBG(" attr %s (len %jd) 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)
DBG(" attr %s (len %jd)\n", name, RTA_PAYLOAD(attr));
else
DBG(" attr %d (len %jd)\n",
attr->rta_type, RTA_PAYLOAD(attr));
}
static void rtnl_link(struct nlmsghdr *hdr)
{
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);
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:
print_attr(attr, "wireless");
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;
}
}
process_link_flags(msg->ifi_flags);
}
static void rtnl_addr(struct nlmsghdr *hdr)
{
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);
for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
attr = RTA_NEXT(attr, bytes)) {
switch (attr->rta_type) {
case IFA_ADDRESS:
print_inet(attr, "address", msg->ifa_family);
break;
case IFA_LOCAL:
print_inet(attr, "local", msg->ifa_family);
break;
case IFA_LABEL:
print_char(attr, "label");
break;
case IFA_BROADCAST:
print_inet(attr, "broadcast", msg->ifa_family);
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_inet(attr, "dst", msg->rtm_family);
break;
case RTA_SRC:
print_inet(attr, "src", msg->rtm_family);
break;
case RTA_IIF:
print_char(attr, "iif");
break;
case RTA_OIF:
print_attr(attr, "oif");
break;
case RTA_GATEWAY:
print_inet(attr, "gateway", msg->rtm_family);
break;
case RTA_PRIORITY:
print_attr(attr, "priority");
break;
case RTA_PREFSRC:
print_inet(attr, "prefsrc", msg->rtm_family);
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(void *buf, size_t len)
{
DBG("buf %p len %zd", buf, len);
while (len > 0) {
struct nlmsghdr *hdr = buf;
struct nlmsgerr *err;
if (!NLMSG_OK(hdr, len))
break;
DBG("len %d type %d flags 0x%04x seq %d",
hdr->nlmsg_len, hdr->nlmsg_type,
hdr->nlmsg_flags, hdr->nlmsg_seq);
switch (hdr->nlmsg_type) {
case NLMSG_NOOP:
DBG("NOOP");
return;
case NLMSG_ERROR:
err = NLMSG_DATA(hdr);
DBG("ERROR %d (%s)", -err->error,
strerror(-err->error));
return;
case NLMSG_DONE:
DBG("DONE");
return;
case NLMSG_OVERRUN:
DBG("OVERRUN");
return;
case RTM_NEWLINK:
DBG("NEWLINK");
rtnl_link(hdr);
break;
case RTM_DELLINK:
DBG("DELLINK");
rtnl_link(hdr);
break;
case RTM_NEWADDR:
DBG("NEWADDR");
rtnl_addr(hdr);
break;
case RTM_DELADDR:
DBG("DELADDR");
rtnl_addr(hdr);
break;
case RTM_NEWROUTE:
DBG("NEWROUTE");
rtnl_route(hdr);
break;
case RTM_DELROUTE:
DBG("DELROUTE");
rtnl_route(hdr);
break;
default:
DBG("type %d", hdr->nlmsg_type);
break;
}
len -= hdr->nlmsg_len;
buf += hdr->nlmsg_len;
}
}
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))
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;
return FALSE;
}
rtnl_message(buf, len);
return TRUE;
}
static GIOChannel *channel = NULL;
int __connman_rtnl_send(const void *buf, size_t len)
{
struct sockaddr_nl addr;
int sk;
DBG("buf %p len %zd", buf, len);
sk = g_io_channel_unix_get_fd(channel);
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
return sendto(sk, buf, len, 0,
(struct sockaddr *) &addr, sizeof(addr));
}
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;
//addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
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);
return 0;
}
void __connman_rtnl_cleanup(void)
{
DBG("");
g_io_channel_shutdown(channel, TRUE, NULL);
g_io_channel_unref(channel);
channel = NULL;
}
|