-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroute-monitor.c
422 lines (351 loc) · 8.41 KB
/
route-monitor.c
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
#include <stdio.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/netlink.h>
#include <linux/wireless.h>
#include <netlink/netlink.h>
#include <netlink/msg.h>
#include "nl-monitor.h"
#include "rt-label.h"
static void show_arp_type (unsigned type)
{
printf (" link/");
#define SHOW(type, name) case ARPHRD_##type: printf (name); break;
switch (type) {
SHOW (ETHER, "ether")
SHOW (INFINIBAND, "infiniband")
SHOW (PPP, "ppp")
SHOW (HDLC, "hdlc")
SHOW (TUNNEL, "tunnel")
SHOW (TUNNEL6, "tunnel6")
SHOW (LOOPBACK, "loopback")
SHOW (SIT, "sip")
SHOW (IPGRE, "ipgre")
SHOW (NONE, "none")
default:
printf ("%04x", type);
}
#undef SHOW
}
static unsigned show_link_flag (unsigned flags,
unsigned flag, const char *name)
{
if ((flags & flag) != 0) {
printf ("%s", name);
if ((flags &= ~flag) != 0)
printf (",");
}
return flags;
}
static void show_link_flags (unsigned flags)
{
printf (" <");
#define SHOW(name) flags = show_link_flag (flags, IFF_##name, #name)
SHOW (UP);
SHOW (BROADCAST);
SHOW (DEBUG);
SHOW (LOOPBACK);
SHOW (POINTOPOINT);
SHOW (RUNNING);
SHOW (NOARP);
SHOW (PROMISC);
SHOW (MASTER);
SHOW (SLAVE);
SHOW (MULTICAST);
SHOW (AUTOMEDIA);
SHOW (DYNAMIC);
SHOW (LOWER_UP); /* L1 is up */
#undef SHOW
if (flags != 0)
printf ("%04x", flags);
printf (">");
}
static void show_proto (unsigned char index)
{
const char *label = rt_proto (index);
if (index == RTPROT_KERNEL)
return;
if (label != NULL)
printf (" proto %s", label);
else
printf (" proto %u", index);
}
static void show_scope (unsigned char index)
{
const char *label = rt_scope (index);
if (index == RT_SCOPE_UNIVERSE)
return;
if (label != NULL)
printf (" scope %s", label);
else
printf (" scope %u", index);
}
static void show_table (unsigned index)
{
const char *label = rt_table (index);
if (index == RT_TABLE_MAIN)
return;
if (label != NULL)
printf (" table %s", label);
else
printf (" table %u", index);
}
static void show_route_type (unsigned type)
{
#define SHOW(type, name) case RTN_##type: printf (" %s", name); break;
switch (type) {
case RTN_UNICAST:
break;
SHOW (LOCAL, "local")
SHOW (BROADCAST, "broadcast")
SHOW (ANYCAST, "anycast")
SHOW (MULTICAST, "multicast")
SHOW (BLACKHOLE, "blackhole")
SHOW (UNREACHABLE, "unreachable")
SHOW (PROHIBIT, "prohibit")
SHOW (THROW, "throw")
SHOW (NAT, "nat")
default:
printf (" route-type %02x", type);
}
#undef SHOW
}
static void show_dump (const char *prefix, const void *data, size_t size)
{
const unsigned char *p;
printf ("%s", prefix);
for (p = data; size > 0; ++p) {
printf ("%02x", *p);
if (--size != 0)
printf (":");
}
}
static void link_show_rta (struct ifinfomsg *o, struct rtattr *rta)
{
struct iw_event *iw;
switch (rta->rta_type) {
case IFLA_ADDRESS:
show_dump (" address ", RTA_DATA (rta), RTA_PAYLOAD (rta));
break;
case IFLA_BROADCAST:
show_dump (" broadcast ", RTA_DATA (rta), RTA_PAYLOAD (rta));
break;
case IFLA_IFNAME:
printf (" name %s", (char *) RTA_DATA (rta));
break;
case IFLA_MTU:
printf (" mtu %u", *(unsigned *) RTA_DATA (rta));
break;
case IFLA_LINK:
printf (" link-type %i", *(int *) RTA_DATA (rta));
break;
case IFLA_TXQLEN:
printf (" qlen %u", *(unsigned *) RTA_DATA (rta));
break;
case IFLA_WIRELESS:
iw = (struct iw_event *) RTA_DATA (rta);
printf (" wireless %04x", iw->cmd);
break;
case IFLA_QDISC:
case IFLA_STATS:
case IFLA_MAP:
case IFLA_OPERSTATE:
case IFLA_LINKMODE:
case IFLA_LINKINFO:
case IFLA_STATS64:
case IFLA_AF_SPEC:
case IFLA_GROUP:
case IFLA_PROMISCUITY:
case IFLA_NUM_TX_QUEUES:
case IFLA_NUM_RX_QUEUES:
case IFLA_CARRIER:
case IFLA_CARRIER_CHANGES:
case IFLA_PROTO_DOWN:
case IFLA_GSO_MAX_SEGS:
case IFLA_GSO_MAX_SIZE:
/* ignore it */
break;
default:
printf (" type %d", rta->rta_type);
show_dump (" ", RTA_DATA (rta), RTA_PAYLOAD (rta));
break;
}
}
static int process_link (struct nlmsghdr *h, void *ctx)
{
struct ifinfomsg *o = NLMSG_DATA (h);
struct rtattr *rta;
int len;
printf ("link %s", h->nlmsg_type == RTM_NEWLINK ? "add" : "del");
printf (" dev %d", o->ifi_index);
show_arp_type (o->ifi_type);
for (
rta = IFLA_RTA (o), len = IFLA_PAYLOAD (h);
RTA_OK (rta, len);
rta = RTA_NEXT (rta, len)
)
link_show_rta (o, rta);
show_link_flags (o->ifi_flags);
printf ("\n");
return 0;
}
static void addr_show_rta (struct ifaddrmsg *ifa, struct rtattr *rta,
struct rtattr *addr)
{
char buf[INET6_ADDRSTRLEN];
const char *type = "", *p;
switch (rta->rta_type) {
case IFA_LOCAL: type = "local"; break;
case IFA_BROADCAST: type = "broadcast"; break;
case IFA_ANYCAST: type = "anycast"; break;
case IFA_MULTICAST: type = "multicast"; break;
}
switch (rta->rta_type) {
case IFA_LABEL:
printf (" label %s", (const char *) RTA_DATA (rta));
break;
case IFA_ADDRESS:
p = inet_ntop (ifa->ifa_family, RTA_DATA (rta),
buf, sizeof (buf));
printf (" address %s/%d", p, ifa->ifa_prefixlen);
break;
case IFA_LOCAL:
if (addr != NULL && addr->rta_len == rta->rta_len &&
memcmp (RTA_DATA (addr), RTA_DATA (rta),
RTA_PAYLOAD (rta)) == 0)
break;
case IFA_BROADCAST:
case IFA_ANYCAST:
case IFA_MULTICAST:
p = inet_ntop (ifa->ifa_family, RTA_DATA (rta),
buf, sizeof (buf));
printf (" %s %s", type, p);
break;
case IFA_CACHEINFO:
/* ignore it */
break;
default:
printf (" type %d", rta->rta_type);
break;
}
}
static int process_addr (struct nlmsghdr *h, void *ctx)
{
struct ifaddrmsg *ifa = NLMSG_DATA (h);
struct rtattr *rta, *addr = NULL;
int len;
if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6)
return 0;
printf ("address %s", h->nlmsg_type == RTM_NEWADDR ? "add" : "del");
for (
rta = IFA_RTA (ifa), len = IFA_PAYLOAD (h);
RTA_OK (rta, len);
rta = RTA_NEXT (rta, len)
) {
if (rta->rta_type == IFA_ADDRESS)
addr = rta;
addr_show_rta (ifa, rta, addr);
}
printf (" dev %d", ifa->ifa_index);
show_scope (ifa->ifa_scope);
printf ("\n");
return 0;
}
static void route_show_rta (struct rtmsg *rtm, struct rtattr *rta)
{
char buf[INET6_ADDRSTRLEN];
const char *p;
switch (rta->rta_type) {
case RTA_DST:
p = inet_ntop (rtm->rtm_family, RTA_DATA (rta),
buf, sizeof (buf));
printf (" dst %s/%d", p, rtm->rtm_dst_len);
break;
case RTA_GATEWAY:
p = inet_ntop (rtm->rtm_family, RTA_DATA (rta),
buf, sizeof (buf));
printf (" via %s", p);
break;
case RTA_OIF:
printf (" dev %d", *(int *) RTA_DATA (rta));
break;
case RTA_PREFSRC:
p = inet_ntop (rtm->rtm_family, RTA_DATA (rta),
buf, sizeof (buf));
printf (" src %s", p);
break;
case RTA_PRIORITY:
printf (" metric %u", *(unsigned *) RTA_DATA (rta));
break;
case RTA_CACHEINFO:
/* ignore it */
break;
case RTA_TABLE:
if (rtm->rtm_table == RT_TABLE_UNSPEC)
show_table (*(unsigned *) RTA_DATA (rta));
break;
case RTA_MARK:
printf (" mark 0x%x", *(unsigned *) RTA_DATA (rta));
break;
default:
printf (" type %d", rta->rta_type);
break;
}
}
static int process_route (struct nlmsghdr *h, void *ctx)
{
struct rtmsg *rtm = NLMSG_DATA (h);
struct rtattr *rta;
int len;
if (rtm->rtm_family != AF_INET && rtm->rtm_family != AF_INET6)
return 0;
if (rtm->rtm_table == RT_TABLE_LOCAL)
return 0;
printf ("route %s", h->nlmsg_type == RTM_NEWROUTE ? "add" : "del");
show_route_type (rtm->rtm_type);
for (
rta = RTM_RTA (rtm), len = RTM_PAYLOAD (h);
RTA_OK (rta, len);
rta = RTA_NEXT (rta, len)
)
route_show_rta (rtm, rta);
if (rtm->rtm_table != RT_TABLE_UNSPEC)
show_table (rtm->rtm_table);
show_proto (rtm->rtm_protocol);
show_scope (rtm->rtm_scope);
printf ("\n");
return 0;
}
static int cb (struct nl_msg *m, void *ctx)
{
struct nlmsghdr *h = nlmsg_hdr (m);
switch (h->nlmsg_type) {
case RTM_NEWLINK:
case RTM_DELLINK:
return process_link (h, ctx);
case RTM_NEWADDR:
case RTM_DELADDR:
return process_addr (h, ctx);
case RTM_NEWROUTE:
case RTM_DELROUTE:
return process_route (h, ctx);
}
return 0;
}
int main (void)
{
int ret;
if ((ret = nl_execute (cb, NETLINK_ROUTE, RTM_GETLINK)) < 0 ||
(ret = nl_execute (cb, NETLINK_ROUTE, RTM_GETADDR)) < 0 ||
(ret = nl_execute (cb, NETLINK_ROUTE, RTM_GETROUTE)) < 0 ||
(ret = nl_monitor (cb, NETLINK_ROUTE, RTNLGRP_LINK,
RTNLGRP_IPV4_ROUTE,
RTNLGRP_IPV6_ROUTE,
RTNLGRP_IPV4_IFADDR,
RTNLGRP_IPV6_IFADDR, 0)) < 0) {
nl_perror (ret, "netlink monitor");
return 1;
}
return 0;
}