-
Notifications
You must be signed in to change notification settings - Fork 84
/
WTPNL80211Netlink.c
441 lines (358 loc) · 10.4 KB
/
WTPNL80211Netlink.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* Elena Agostini - elena.ago@gmail.com
*
* Netlink functions with libnl
*/
#include "CWWTP.h"
static uint32_t port_bitmap[32] = { 0 };
/* ************* NETLINK LIBNL *************** */
int nl80211_init_socket(struct nl80211SocketUnit *nlSockUnit)
{
int err;
nlSockUnit->nl_sock = nl_socket_alloc();
if (!nlSockUnit->nl_sock) {
fprintf(stderr, "Failed to allocate netlink socket.\n");
return -ENOMEM;
}
nl_socket_set_buffer_size(nlSockUnit->nl_sock, 8192, 8192);
if (genl_connect(nlSockUnit->nl_sock)) {
fprintf(stderr, "Failed to connect to generic netlink.\n");
err = -ENOLINK;
goto out_handle_destroy;
}
nlSockUnit->nl80211_id = genl_ctrl_resolve(nlSockUnit->nl_sock, "nl80211");
if (nlSockUnit->nl80211_id < 0) {
fprintf(stderr, "nl80211 not found.\n");
err = -ENOENT;
goto out_handle_destroy;
}
nlSockUnit->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
if (!nlSockUnit->nl_cb)
return -1;
/*
nl_cb_set(nlSockUnit->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
nl_cb_set(nlSockUnit->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_bss_event, bss);
*/
return 0;
out_handle_destroy:
nl_socket_free(nlSockUnit->nl_sock);
return err;
}
//Chiude netlink libnl
void nl80211_cleanup_socket(struct nl80211SocketUnit *nlSockUnit)
{
nl_socket_free(nlSockUnit->nl_sock);
}
//Crea handler
struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
{
struct nl_handle *handle;
handle = nl80211_handle_alloc(cb);
if (handle == NULL) {
CWLog("nl80211: Failed to allocate netlink callbacks (%s)", dbg);
return NULL;
}
if (genl_connect(handle)) {
CWLog("nl80211: Failed to connect to generic netlink (%s)", dbg);
nl80211_handle_destroy(handle);
return NULL;
}
return handle;
}
/*
struct nl_handle *nl80211_handle_alloc(void *cb)
{
struct nl_handle *handle;
uint32_t pid = getpid() & 0x3FFFFF;
int i;
handle = nl_handle_alloc_cb(cb);
for (i = 0; i < 1024; i++) {
if (port_bitmap[i / 32] & (1 << (i % 32)))
continue;
port_bitmap[i / 32] |= 1 << (i % 32);
pid += i << 22;
break;
}
nl_socket_set_local_port(handle, pid);
return handle;
}
void nl80211_handle_destroy(struct nl_handle *handle)
{
uint32_t port = nl_socket_get_local_port(handle);
port >>= 22;
port_bitmap[port / 32] &= ~(1 << (port % 32));
nl_handle_destroy(handle);
}
* */
void nl_destroy_handles(struct nl_handle **handle)
{
if (*handle == NULL)
return;
nl80211_handle_destroy(*handle);
*handle = NULL;
}
int no_seq_check(struct nl_msg *msg, void *arg)
{
return NL_OK;
}
//Alloca nuova callback
int CW80211InitNlCb(WTPBSSInfo * WTPBSSInfoPtr) //WTPInterfaceInfo * interfaceInfo)
{
WTPBSSInfoPtr->interfaceInfo->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
if (!WTPBSSInfoPtr->interfaceInfo->nl_cb) {
CWLog("nl80211: Failed to alloc cb struct");
return -1;
}
if(nl_cb_set(WTPBSSInfoPtr->interfaceInfo->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL) != 0)
{
CWLog("nl80211: Errore nl_cb_set no_seq_check");
return -1;
}
if(nl_cb_set(WTPBSSInfoPtr->interfaceInfo->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, CW80211CheckTypeEvent, WTPBSSInfoPtr) != 0)
{
CWLog("nl80211: Errore nl_cb_set CW80211CheckTypeEvent");
return -1;
}
return 0;
}
int CW80211CheckTypeEvent(struct nl_msg *msg, void *arg)
{
WTPBSSInfo * WTPBSSInfoPtr = arg;
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
char * frameBuffer = nlmsg_data(nlmsg_hdr(msg));
struct nlattr *tb[NL80211_ATTR_MAX + 1];
int ifidx = -1;
nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL);
if (tb[NL80211_ATTR_IFINDEX]) {
ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
if (ifidx == -1 || ifidx == WTPBSSInfoPtr->interfaceInfo->realWlanID) {
CW80211EventProcess(WTPBSSInfoPtr, gnlh->cmd, tb, frameBuffer);
return NL_SKIP;
}
CWLog("nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)", gnlh->cmd, ifidx);
} else if (tb[NL80211_ATTR_WDEV]) {
u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
CWLog("nl80211: Process event on P2P device");
/*for (bss = drv->first_bss; bss; bss = bss->next) {
if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
CW80211EventProcess(bss, gnlh->cmd, tb);
return NL_SKIP;
}
}
*/
CWLog("nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)", gnlh->cmd, (long long unsigned int) wdev_id);
}
return NL_SKIP;
}
//Alloca nuovo messaggio
struct nl_msg * nl80211_message_alloc()
{
struct nl_msg *msg;
msg = nlmsg_alloc();
if (!msg) {
fprintf(stderr, "failed to allocate netlink message\n");
return NULL;
}
return msg;
}
int nl80211_send_recv_cb_input(struct nl80211SocketUnit *nlSockUnit,
struct nl_msg *msg,
int (*valid_handler)(struct nl_msg *, void *),
void *valid_data)
{
struct nl_cb *cb;
int err = -ENOMEM;
//Prepara handler del netlink
cb = nl_cb_alloc(NL_CB_DEFAULT);
if (!cb) {
fprintf(stderr, "failed to allocate netlink callbacks\n");
err = 2;
return err;
}
err = nl_send_auto(nlSockUnit->nl_sock, msg);
if (err < 0)
goto out;
err = 1;
nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
if (valid_handler)
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, valid_handler, valid_data);
while (err > 0) {
int res = nl_recvmsgs(nlSockUnit->nl_sock, cb);
if (res < 0) {
CWLog("nl80211: %s->nl_recvmsgs failed: %d", __func__, res);
}
}
out:
nl_cb_put(cb);
nlmsg_free(msg);
return err;
}
int send_and_recv(struct nl80211SocketUnit *global,
struct nl_handle *nl_handle, struct nl_msg *msg,
int (*valid_handler)(struct nl_msg *, void *),
void *valid_data)
{
struct nl_cb *cb;
int err = -ENOMEM;
cb = nl_cb_clone(global->nl_cb);
if (!cb)
goto out;
err = nl_send_auto(nl_handle, msg);
//err = nl_send_auto_complete(nl_handle, msg);
if (err < 0)
goto out;
err = 1;
nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
if (valid_handler)
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
valid_handler, valid_data);
while (err > 0) {
int res = nl_recvmsgs(nl_handle, cb);
if (res < 0) {
CWLog("nl80211: %s->nl_recvmsgs failed: %d", __func__, res);
}
}
out:
nl_cb_put(cb);
nlmsg_free(msg);
return err;
}
/* ************* NETLINK SOCKET *************** */
int netlink_create_socket(struct nl80211SocketUnit *nlSockUnit)
{
struct sockaddr_nl local;
nlSockUnit->sockNetlink = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (nlSockUnit->sockNetlink < 0) {
CWLog("netlink: Failed to open netlink socket: %s", strerror(errno));
// netlink_deinit(netlink);
return -1;
}
os_memset(&local, 0, sizeof(local));
local.nl_family = AF_NETLINK;
local.nl_groups = RTMGRP_LINK;
if (bind(nlSockUnit->sockNetlink, (struct sockaddr *) &local, sizeof(local)) < 0)
{
CWLog("netlink: Failed to bind netlink socket: %s", strerror(errno));
// netlink_deinit(netlink);
return -1;
}
return 0;
}
CWBool netlink_send_oper_ifla(int sock, int ifindex, int linkmode, int operstate)
{
struct {
struct nlmsghdr hdr;
struct ifinfomsg ifinfo;
char opts[16];
} req;
struct rtattr *rta;
static int nl_seq;
ssize_t ret;
os_memset(&req, 0, sizeof(req));
req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
req.hdr.nlmsg_type = RTM_SETLINK;
req.hdr.nlmsg_flags = NLM_F_REQUEST;
req.hdr.nlmsg_seq = ++nl_seq;
req.hdr.nlmsg_pid = 0;
req.ifinfo.ifi_family = AF_UNSPEC;
req.ifinfo.ifi_type = 0;
req.ifinfo.ifi_index = ifindex;
req.ifinfo.ifi_flags = 0;
req.ifinfo.ifi_change = 0;
if (linkmode != -1) {
rta = aliasing_hide_typecast(
((char *) &req + NLMSG_ALIGN(req.hdr.nlmsg_len)),
struct rtattr);
rta->rta_type = IFLA_LINKMODE;
rta->rta_len = RTA_LENGTH(sizeof(char));
*((char *) RTA_DATA(rta)) = linkmode;
req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
RTA_LENGTH(sizeof(char));
}
if (operstate != -1) {
rta = aliasing_hide_typecast(
((char *) &req + NLMSG_ALIGN(req.hdr.nlmsg_len)),
struct rtattr);
rta->rta_type = IFLA_OPERSTATE;
rta->rta_len = RTA_LENGTH(sizeof(char));
*((char *) RTA_DATA(rta)) = operstate;
req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
RTA_LENGTH(sizeof(char));
}
ret = send(sock, &req, req.hdr.nlmsg_len, 0);
if (ret < 0) {
CWLog("netlink: Sending operstate IFLA failed: %s (assume operstate is not supported)", strerror(errno));
}
CWLog("netlink: Operstate: ifindex=%d linkmode=%d, operstate=%d", ifindex, linkmode, operstate);
return ret < 0 ? CW_FALSE : CW_TRUE;
}
#ifndef SIOCBRADDBR
#define SIOCBRADDBR 0x89a0
#endif
#ifndef SIOCBRDELBR
#define SIOCBRDELBR 0x89a1
#endif
#ifndef SIOCBRADDIF
#define SIOCBRADDIF 0x89a2
#endif
#ifndef SIOCBRDELIF
#define SIOCBRDELIF 0x89a3
#endif
CWBool CWSetNewBridge(int sock, char * bridgeName) {
//Set up bridge
if (ioctl(sock, SIOCBRADDBR, bridgeName) < 0) {
CWLog("Could not add bridge %s: %s", bridgeName, strerror(errno));
//return CW_FALSE;
}
return CW_TRUE;
}
CWBool CWAddNewBridgeInterface(int sock, char * bridgeName, int wlanID) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, bridgeName, IFNAMSIZ);
ifr.ifr_ifindex = wlanID;
CWLog("Aggiungo a %s ifface %d", bridgeName, wlanID);
if (ioctl(sock, SIOCBRADDIF, &ifr) < 0) {
CWLog("Could not add interface %d into bridge %s: %s", wlanID, bridgeName, strerror(errno));
return CW_FALSE;
}
return CW_TRUE;
}
CWBool CWDelBridge(int sock, char * bridgeName) {
if (ioctl(sock, SIOCBRDELBR, bridgeName) < 0) {
CWLog("Could not remove bridge %s: %s", bridgeName, strerror(errno));
return CW_FALSE;
}
return CW_TRUE;
}
CWBool CWDelBridgeInterface(int sock, char * bridgeName, int wlanID) {
struct ifreq ifr;
os_memset(&ifr, 0, sizeof(ifr));
os_strlcpy(ifr.ifr_name, bridgeName, IFNAMSIZ);
ifr.ifr_ifindex = wlanID;
if (ioctl(sock, SIOCBRDELIF, &ifr) < 0) {
CWLog("Could not remove interface %d from bridge %s: %s", wlanID, bridgeName, strerror(errno));
return CW_FALSE;
}
return CW_TRUE;
}
int CWGetBridge(char *brname, const char *ifname)
{
char path[128], brlink[128], *pos;
ssize_t res;
os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/bridge", ifname);
res = readlink(path, brlink, sizeof(brlink));
if (res < 0 || (size_t) res >= sizeof(brlink))
return CW_FALSE;
brlink[res] = '\0';
pos = os_strrchr(brlink, '/');
if (pos == NULL)
return CW_FALSE;
pos++;
os_strlcpy(brname, pos, IFNAMSIZ);
return CW_TRUE;
}