-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcfm_server.c
executable file
·197 lines (151 loc) · 4.94 KB
/
cfm_server.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
// Copyright (c) 2020 Microchip Technology Inc. and its subsidiaries.
// SPDX-License-Identifier: (GPL-2.0)
#include <stdint.h>
#include <unistd.h>
#include <linux/types.h>
#include <linux/if_bridge.h>
#include <errno.h>
#include "list.h"
#include <stdio.h>
#include <stdbool.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <ev.h>
#include <fcntl.h>
#include <getopt.h>
#include <net/if.h>
#include "cfm_netlink.h"
#include "libnetlink.h"
volatile bool quit = false;
static void handle_signal(int sig)
{
ev_break(EV_DEFAULT, EVBREAK_ALL);;
}
int signal_init(void)
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handle_signal;
sa.sa_flags = 0;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, NULL);
return 0;
}
static struct rtnl_handle rth;
static ev_io netlink_watcher;
char *rta_getattr_mac(const struct rtattr *rta)
{
static char buf_ret[100];
unsigned char mac[6];
memcpy(&mac, RTA_DATA(rta), 6);
snprintf(buf_ret, sizeof(buf_ret), "%02X-%02X-%02X-%02X-%02X-%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return buf_ret;
}
static int netlink_listen(struct rtnl_ctrl_data *who, struct nlmsghdr *n,
void *arg)
{
struct rtattr *aftb[IFLA_BRIDGE_MAX + 1];
struct rtattr *info_peer[IFLA_BRIDGE_CFM_CC_PEER_EVENT_MAX + 1];
struct rtattr *info_mip[IFLA_BRIDGE_CFM_MIP_EVENT_MAX + 1];
struct ifinfomsg *ifi = NLMSG_DATA(n);
struct rtattr *tb[IFLA_MAX + 1];
int len = n->nlmsg_len;
struct rtattr *i, *list;
int rem;
uint32_t instance, request, sub_code, status;
if (n->nlmsg_type == NLMSG_DONE)
return 0;
len -= NLMSG_LENGTH(sizeof(*ifi));
if (len < 0) {
fprintf(stderr, "Message too short!\n");
return -1;
}
if (ifi->ifi_family != AF_BRIDGE)
return 0;
if (n->nlmsg_type != RTM_NEWLINK)
return 0;
parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len, NLA_F_NESTED);
if (tb[IFLA_IFNAME] == NULL) {
printf("No IFLA_IFNAME\n");
return -1;
}
if (!tb[IFLA_AF_SPEC])
return 0;
parse_rtattr_flags(aftb, IFLA_BRIDGE_MAX, RTA_DATA(tb[IFLA_AF_SPEC]), RTA_PAYLOAD(tb[IFLA_AF_SPEC]), NLA_F_NESTED);
if (!aftb[IFLA_BRIDGE_CFM])
return 0;
list = aftb[IFLA_BRIDGE_CFM];
rem = RTA_PAYLOAD(list);
printf("EVENT CFM CC peer status:\n");
instance = 0xFFFFFFFF;
for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
if (i->rta_type != (IFLA_BRIDGE_CFM_CC_PEER_EVENT_INFO | NLA_F_NESTED))
continue;
parse_rtattr_flags(info_peer, IFLA_BRIDGE_CFM_CC_PEER_EVENT_MAX, RTA_DATA(i), RTA_PAYLOAD(i), NLA_F_NESTED);
if (!info_peer[IFLA_BRIDGE_CFM_CC_PEER_EVENT_INSTANCE])
continue;
if (instance != rta_getattr_u32(info_peer[IFLA_BRIDGE_CFM_CC_PEER_EVENT_INSTANCE])) {
instance = rta_getattr_u32(info_peer[IFLA_BRIDGE_CFM_CC_PEER_EVENT_INSTANCE]);
printf("Instance %u\n", rta_getattr_u32(info_peer[IFLA_BRIDGE_CFM_CC_PEER_EVENT_INSTANCE]));
}
printf(" Peer-mep %u\n", rta_getattr_u32(info_peer[IFLA_BRIDGE_CFM_CC_PEER_EVENT_PEER_MEPID]));
printf(" CCM defect %u\n", rta_getattr_u32(info_peer[IFLA_BRIDGE_CFM_CC_PEER_EVENT_CCM_DEFECT]));
printf("\n");
}
printf("EVENT CFM MIP RAPS info:\n");
instance = 0xFFFFFFFF;
for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
if (i->rta_type != (IFLA_BRIDGE_CFM_MIP_EVENT_INFO | NLA_F_NESTED))
continue;
parse_rtattr_flags(info_mip, IFLA_BRIDGE_CFM_MIP_EVENT_MAX, RTA_DATA(i), RTA_PAYLOAD(i), NLA_F_NESTED);
if (!info_mip[IFLA_BRIDGE_CFM_MIP_EVENT_INSTANCE])
continue;
if (instance != rta_getattr_u32(info_mip[IFLA_BRIDGE_CFM_MIP_EVENT_INSTANCE])) {
instance = rta_getattr_u32(info_mip[IFLA_BRIDGE_CFM_MIP_EVENT_INSTANCE]);
printf("Instance %u\n", rta_getattr_u32(info_mip[IFLA_BRIDGE_CFM_MIP_EVENT_INSTANCE]));
}
request = (rta_getattr_u32(info_mip[IFLA_BRIDGE_CFM_MIP_EVENT_RAPS_REQUEST_SUBCODE]) & 0xF0) >> 4;
sub_code = rta_getattr_u32(info_mip[IFLA_BRIDGE_CFM_MIP_EVENT_RAPS_REQUEST_SUBCODE]) & 0x0F;
status = rta_getattr_u32(info_mip[IFLA_BRIDGE_CFM_MIP_EVENT_RAPS_STATUS]);
printf(" request %u\n", request);
printf(" sub_code %u\n", sub_code);
printf(" status %u\n", status);
printf(" Node-id %s\n", rta_getattr_mac(info_mip[IFLA_BRIDGE_CFM_MIP_EVENT_RAPS_NODE_ID]));
printf("\n");
}
return 0;
}
static void netlink_rcv(EV_P_ ev_io *w, int revents)
{
rtnl_listen(&rth, netlink_listen, stdout);
}
static int netlink_init(void)
{
int err;
err = rtnl_open(&rth, RTMGRP_LINK);
if (err)
return err;
fcntl(rth.fd, F_SETFL, O_NONBLOCK);
ev_io_init(&netlink_watcher, netlink_rcv, rth.fd, EV_READ);
ev_io_start(EV_DEFAULT, &netlink_watcher);
return 0;
}
static void netlink_uninit(void)
{
ev_io_stop(EV_DEFAULT, &netlink_watcher);
rtnl_close(&rth);
}
int main (void)
{
if (netlink_init()) {
printf("netlink init failed!\n");
return -1;
}
ev_run(EV_DEFAULT, 0);
netlink_uninit();
return 0;
}