-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlldp_linux_framer.c
344 lines (270 loc) · 9.67 KB
/
lldp_linux_framer.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
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <malloc.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <linux/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <time.h>
#include "lldp_linux_framer.h"
#include "lldp_port.h"
#include "lldp_debug.h"
#include "common_func.h"
#include "lldp_dunchong.h"
#define XENOSOCK 0
ssize_t lldp_write(struct lldp_port *lldp_port)
{
return sendto(lldp_port->socket, lldp_port->tx.frame, lldp_port->tx.sendsize, 0, NULL, 0);
}
ssize_t lldp_read(struct lldp_port *lldp_port) {
lldp_port->rx.recvsize = recvfrom(lldp_port->socket, lldp_port->rx.frame, lldp_port->mtu, 0, 0, 0);
return (lldp_port->rx.recvsize);
}
#define IS_2G(wifimode) (wifimode & 0x01)
#define IS_5G(wifimode) (wifimode & 0x02)
/* This wifi working-mode is 2G or 5G , error -1 */
static int32_t _get_wifi_working_mode(struct lldp_port* wifi_port)
{
#if 0
FILE *fp;
int8_t *p;
int8_t cmd[128] = {0};
int8_t buf[16] = {0};
int32_t wifimode;
sprintf(cmd, "iwprive %s get_mode", wifi_port->if_name);
if ((fp = popen(cmd, "r")) == NULL)
return -1;
p = fgets(cmd, sizeof(buf), fp);
if (p == NULL)
return -1;
printf("buf %s", buf);
pclose(fp);
wifimode = atoi(buf);
if (IS_2G(wifimode))
wifimode = LLDP_DUNCHONG_WIFI_2G;
if (IS_5G(wifimode))
wifimode = LLDP_DUNCHONG_WIFI_5G;
wifi_port->wifimode = wifimode;
return wifimode;
#endif
wifi_port->wifimode = LLDP_DUNCHONG_WIFI_2G;
return LLDP_DUNCHONG_WIFI_2G;
}
/* get the MAC address of an interface */
static int _getmac(struct lldp_port *lldp_port)
{
uint8_t *p;
struct ifreq *ifr = calloc(1, sizeof(struct ifreq));
int retval = 0;
ifr->ifr_ifindex = lldp_port->if_index;
strncpy(ifr->ifr_name, &lldp_port->if_name[0], strlen(lldp_port->if_name));
retval = ioctl(lldp_port->socket, SIOCGIFHWADDR, ifr);
if (retval < 0) {
free(ifr);
return retval;
}
memcpy(&lldp_port->source_mac[0], &ifr->ifr_hwaddr.sa_data[0], 6);
p = lldp_port->source_mac;
lldp_printf(MSG_INFO, "[%s %d] [INFO] interface %s's MAC address %02x:%02x:%02x:%02x:%02x:%02x\n",
__FUNCTION__, __LINE__, lldp_port->if_name, p[0], p[1], p[2], p[3], p[4], p[5]);
free(ifr);
return 0;
}
/* get the IP address of an interface */
static int _getip(struct lldp_port *lldp_port)
{
struct ifreq *ifr = calloc(1, sizeof(struct ifreq));
int retval = 0;
strncpy(ifr->ifr_name, &lldp_port->if_name[0], strlen(lldp_port->if_name));
retval = ioctl(lldp_port->socket, SIOCGIFADDR, ifr);
if (retval < 0) {
/* set source ip address to 0.0.0.0 on error */
memset(&lldp_port->source_ipaddr[0], 0x00, 4);
free(ifr);
return retval;
} else {
memcpy(&lldp_port->source_ipaddr[0], &ifr->ifr_addr.sa_data[2], 4);
}
free(ifr);
return retval;
}
extern struct lldp_port *wifi_ports;
void lldp_refresh_if_data(struct lldp_port *lldp_port)
{
_getmac(lldp_port);
_getip(lldp_port);
}
/* get wifi interface module */
int get_wifi_interface()
{
int if_index = 0;
char if_name[32];
struct lldp_port *lldp_port, *tmpport;
int retval = 0;
struct ifreq *ifr = calloc(1, sizeof(struct ifreq));
struct sockaddr_ll *sll = calloc(1, sizeof(struct sockaddr_ll));
for (if_index = MIN_INTERFACES; if_index < MAX_INTERFACES; if_index++) {
if (if_indextoname(if_index, if_name) == NULL)
continue;
/* if is not wifi interface, skip */
if (strncmp(if_name, "wifi", 4))
continue;
/* create new interface struct */
lldp_port = malloc(sizeof(struct lldp_port));
memset(lldp_port, 0x0, sizeof(struct lldp_port));
/* add it to the global list */
lldp_port->next = wifi_ports;
lldp_printf(MSG_DEBUG, "[%s %d] add this interface %s to the global port list \n", __FUNCTION__, __LINE__, if_name);
lldp_port->if_index = if_index;
lldp_port->if_name = malloc(32);
if (lldp_port->if_name == NULL) {
free(lldp_port);
lldp_port = NULL;
continue;
}
memcpy(lldp_port->if_name, if_name, 32);
lldp_port->socket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_DUNCHONG));
if (lldp_port->socket < 0) {
lldp_printf(MSG_ERROR, "[%s %d]Couldn't initialize raw socket for interface %s!\n", __FUNCTION__, __LINE__, lldp_port->if_name);
return -1;
}
/* Set up the interface for binding */
sll->sll_family = PF_PACKET;
sll->sll_ifindex = lldp_port->if_index;
sll->sll_protocol = htons(ETH_P_DUNCHONG);
retval = bind(lldp_port->socket, (struct sockaddr*)sll, sizeof(struct sockaddr_ll));
if (retval < 0) {
lldp_printf(MSG_ERROR, "[%s %d] [ERROR] binding raw socket to interface %s failed\n", __FUNCTION__, __LINE__, lldp_port->if_name);
return -1;
}
strncpy(ifr->ifr_name, &lldp_port->if_name[0], strlen(lldp_port->if_name));
if (strlen(ifr->ifr_name) == 0) {
lldp_printf(MSG_ERROR, "[%s %d] [ERROR]Invalid interface name\n", __FUNCTION__, __LINE__);
return -1;
}
_getmac(lldp_port);
_get_wifi_working_mode(lldp_port);
lldp_printf(MSG_INFO, "[%s %d]wifi INC %02x:%02x:%02x:%02x:%02x:%02x, wifimode %dG\n",
__FUNCTION__, __LINE__,
lldp_port->source_mac[0], lldp_port->source_mac[1], lldp_port->source_mac[2],
lldp_port->source_mac[3], lldp_port->source_mac[4], lldp_port->source_mac[5],
lldp_port->wifimode);
wifi_ports = lldp_port;
}
free(ifr);
free(sll);
}
int lldp_init_socket(struct lldp_port *lldp_port)
{
struct ifreq *ifr = calloc(1, sizeof(struct ifreq));
struct sockaddr_ll *sll = calloc(1, sizeof(struct sockaddr_ll));
int retval = 0;
if (lldp_port->if_name == NULL) {
lldp_printf(MSG_ERROR, "[%s %d] [ERROR] Got NULL interface\n", __FUNCTION__, __LINE__);
return XENOSOCK;
}
if (lldp_port->if_index <= 0) {
lldp_printf(MSG_ERROR, "[%s %d] [ERROR] if_index <= 0, does not appear b to be a valid interface\n", __FUNCTION__, __LINE__);
return XENOSOCK;
}
lldp_printf(MSG_INFO, "[%s %d]'%s' is index %d\n",
__FUNCTION__, __LINE__,
lldp_port->if_name, lldp_port->if_index);
/* Set up the raw socket for the LLDP ethertype */
lldp_port->socket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_DUNCHONG));
if (lldp_port->socket < 0) {
lldp_printf(MSG_ERROR, "[%s %d]Couldn't initialize raw socket for interface %s!\n", __FUNCTION__, __LINE__, lldp_port->if_name);
return XENOSOCK;
}
/* Set up the interface for binding */
sll->sll_family = PF_PACKET;
sll->sll_ifindex = lldp_port->if_index;
sll->sll_protocol = htons(ETH_P_DUNCHONG);
retval = bind(lldp_port->socket, (struct sockaddr*)sll, sizeof(struct sockaddr_ll));
if (retval < 0) {
lldp_printf(MSG_ERROR, "[%s %d] [ERROR] binding raw socket to interface %s failed\n", __FUNCTION__, __LINE__, lldp_port->if_name);
return XENOSOCK;
}
ifr->ifr_ifindex = lldp_port->if_index;
strncpy(ifr->ifr_name, &lldp_port->if_name[0], strlen(lldp_port->if_name));
if (strlen(ifr->ifr_name) == 0) {
lldp_printf(MSG_ERROR, "[%s %d] [ERROR]Invalid interface name\n", __FUNCTION__, __LINE__);
return XENOSOCK;
}
lldp_refresh_if_data(lldp_port);
retval = ioctl(lldp_port->socket, SIOCGIFFLAGS, ifr);
if (retval == -1)
lldp_printf(MSG_WARNING, "[%s %d] [WARNING] can not get flags for interface %s\n", __FUNCTION__, __LINE__, lldp_port->if_name);
if ((ifr->ifr_flags & IFF_UP) == 0) {
lldp_printf(MSG_WARNING, "[%s %d] [WARNING] Interface %s is DOWN,portEnabled = 0\n", lldp_port->if_name, __FUNCTION__, __LINE__);
lldp_port->portEnabled = 0;
}
/* set allmulti on interface
* need to devise a grateful way to turn off allmulti otherwise
* it is left on for the interface when problem is terminated.
*
* ifr.ifr_flags &= ~IFF_ALLMULTI; allmulti off
*/
retval = ioctl(lldp_port->socket, SIOCGIFFLAGS, ifr);
if (retval == -1)
lldp_printf(MSG_WARNING, "[%s %d] [WARNING] Can not get flags for interface %s\n",
__FUNCTION__, __LINE__, lldp_port->if_name);
/* allmulti on (verified via ifconfig) */
ifr->ifr_flags |= IFF_ALLMULTI;
retval = ioctl(lldp_port->socket, SIOCSIFFLAGS, ifr);
if (retval == -1) {
lldp_printf(MSG_ERROR, "[%s %d]Can't set flags for interface %s\n", __FUNCTION__, __LINE__, lldp_port->if_name);
}
/* discover MTU for this interface */
retval = ioctl(lldp_port->socket, SIOCGIFMTU, ifr);
if (retval < 0) {
lldp_printf(MSG_ERROR, "[%s %d] [ERROR] Can't determin MTU for interface %s\n", __FUNCTION__, __LINE__, lldp_port->if_name);
return retval;
}
lldp_port->mtu = ifr->ifr_ifru.ifru_mtu;
lldp_printf(MSG_INFO, "[%s %d][INFO] Interface %s's MTU is %d\n",
__FUNCTION__, __LINE__, lldp_port->if_name, lldp_port->mtu);
lldp_port->rx.frame = calloc(1, lldp_port->mtu - 4);
lldp_port->tx.frame = calloc(1, lldp_port->mtu - 2);
if (!lldp_port->tx.frame)
lldp_printf(MSG_ERROR, "[%s %d] [ERROR] Unable to malloc buffer for tx.frame\n", __FUNCTION__, __LINE__);
if (!lldp_port->rx.frame)
lldp_printf(MSG_ERROR, "[%s %d] [ERROR] Unable to malloc buffer for rx.frame\n", __FUNCTION__, __LINE__);
free(ifr);
free(sll);
return 0;
}
void socketCleanupLLDP(struct lldp_port *lldp_port)
{
if (!lldp_port) {
/* lldp_port was NULL ! */
return;
}
if (lldp_port->if_name != NULL) {
free(lldp_port->rx.frame);
lldp_port->rx.frame = NULL;
} else {
/*lldp_port->rx.frame was NULL ! */
}
if (lldp_port->tx.frame != NULL) {
free(lldp_port->tx.frame);
lldp_port->tx.frame = NULL;
} else {
/*lldp_port->tx.frame was NULL ! */
}
if (lldp_port->if_name != NULL) {
free(lldp_port->if_name);
lldp_port->if_name = NULL;
} else {
/* Error, lldp_port->if_name was NULL */
}
}