-
Notifications
You must be signed in to change notification settings - Fork 7
/
brmon.c
215 lines (181 loc) · 5.03 KB
/
brmon.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
/*
* brmon.c RTnetlink listener.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Authors: Stephen Hemminger <[email protected]>
* Modified by Srinivas Aji <[email protected]>
* for use in RSTP daemon. - 2006-09-01
* Modified by Vitalii Demianets <[email protected]>
* for use in MSTP daemon. - 2011-07-18
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <linux/if_bridge.h>
#include "log.h"
#include "libnetlink.h"
#include "bridge_ctl.h"
#include "netif_utils.h"
#include "epoll_loop.h"
/* RFC 2863 operational status */
enum
{
IF_OPER_UNKNOWN,
IF_OPER_NOTPRESENT,
IF_OPER_DOWN,
IF_OPER_LOWERLAYERDOWN,
IF_OPER_TESTING,
IF_OPER_DORMANT,
IF_OPER_UP,
};
/* link modes */
enum
{
IF_LINK_MODE_DEFAULT,
IF_LINK_MODE_DORMANT, /* limit upward transition to dormant */
};
static const char *port_states[] =
{
[BR_STATE_DISABLED] = "disabled",
[BR_STATE_LISTENING] = "listening",
[BR_STATE_LEARNING] = "learning",
[BR_STATE_FORWARDING] = "forwarding",
[BR_STATE_BLOCKING] = "blocking",
};
static struct rtnl_handle rth;
static struct epoll_event_handler br_handler;
struct rtnl_handle rth_state;
static int dump_msg(const struct sockaddr_nl *who, struct nlmsghdr *n,
void *arg)
{
struct ifinfomsg *ifi = NLMSG_DATA(n);
struct rtattr * tb[IFLA_MAX + 1];
int len = n->nlmsg_len;
char b1[IFNAMSIZ];
int af_family = ifi->ifi_family;
bool newlink;
int br_index;
if(n->nlmsg_type == NLMSG_DONE)
return 0;
len -= NLMSG_LENGTH(sizeof(*ifi));
if(len < 0)
{
return -1;
}
if(af_family != AF_BRIDGE && af_family != AF_UNSPEC)
return 0;
if(n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
return 0;
parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
/* Check if we got this from bonding */
if(tb[IFLA_MASTER] && af_family != AF_BRIDGE)
return 0;
if(tb[IFLA_IFNAME] == NULL)
{
ERROR("BUG: nil ifname\n");
return -1;
}
if(n->nlmsg_type == RTM_DELLINK)
LOG("Deleted ");
LOG("%d: %s ", ifi->ifi_index, (char*)RTA_DATA(tb[IFLA_IFNAME]));
if(tb[IFLA_OPERSTATE])
{
int state = *(int*)RTA_DATA(tb[IFLA_OPERSTATE]);
switch (state)
{
case IF_OPER_UNKNOWN:
LOG("Unknown ");
break;
case IF_OPER_NOTPRESENT:
LOG("Not Present ");
break;
case IF_OPER_DOWN:
LOG("Down ");
break;
case IF_OPER_LOWERLAYERDOWN:
LOG("Lowerlayerdown ");
break;
case IF_OPER_TESTING:
LOG("Testing ");
break;
case IF_OPER_DORMANT:
LOG("Dormant ");
break;
case IF_OPER_UP:
LOG("Up ");
break;
default:
LOG("State(%d) ", state);
}
}
if(tb[IFLA_MTU])
LOG("mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
if(tb[IFLA_MASTER])
{
LOG("master %s ",
if_indextoname(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
}
if(tb[IFLA_PROTINFO])
{
uint8_t state = *(uint8_t *)RTA_DATA(tb[IFLA_PROTINFO]);
if(state <= BR_STATE_BLOCKING)
LOG("state %s", port_states[state]);
else
LOG("state (%d)", state);
}
newlink = (n->nlmsg_type == RTM_NEWLINK);
if(tb[IFLA_MASTER])
br_index = *(int*)RTA_DATA(tb[IFLA_MASTER]);
else if(is_bridge((char*)RTA_DATA(tb[IFLA_IFNAME])))
br_index = ifi->ifi_index;
else
br_index = -1;
bridge_notify(br_index, ifi->ifi_index, newlink, ifi->ifi_flags);
return 0;
}
static inline void br_ev_handler(uint32_t events, struct epoll_event_handler *h)
{
if(rtnl_listen(&rth, dump_msg, stdout) < 0)
{
ERROR("Error on bridge monitoring socket\n");
}
}
int init_bridge_ops(void)
{
if(rtnl_open(&rth, RTMGRP_LINK) < 0)
{
ERROR("Couldn't open rtnl socket for monitoring\n");
return -1;
}
if(rtnl_open(&rth_state, 0) < 0)
{
ERROR("Couldn't open rtnl socket for setting state\n");
return -1;
}
if(rtnl_wilddump_request(&rth, PF_BRIDGE, RTM_GETLINK) < 0)
{
ERROR("Cannot send dump request: %m\n");
return -1;
}
if(rtnl_dump_filter(&rth, dump_msg, stdout, NULL, NULL) < 0)
{
ERROR("Dump terminated\n");
return -1;
}
if(fcntl(rth.fd, F_SETFL, O_NONBLOCK) < 0)
{
ERROR("Error setting O_NONBLOCK: %m\n");
return -1;
}
br_handler.fd = rth.fd;
br_handler.arg = NULL;
br_handler.handler = br_ev_handler;
if(add_epoll(&br_handler) < 0)
return -1;
return 0;
}