Newer
Older
* Copyright (C) 2020 IOPSYS Software Solutions AB. All rights reserved.
*
* Author: anjan.chanda@iopsys.eu
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <json-c/json.h>
#include <libubox/blobmsg.h>
#include <libubox/blobmsg_json.h>
#include <libubox/uloop.h>
#include <libubox/ustream.h>
#include <libubox/utils.h>
#include <libubus.h>
#include <easy/easy.h>
#include <wifi.h> // FIXME: should not be included
#include <map1905/map2.h>
#include <map1905/maputils.h>
#include "config.h"
#include "cntlr.h"
#include "comm.h"
#include "hlist.h"
#include "allsta.h"
#include "cntlr_ubus.h"
#include "cntlr_cmdu_generator.h"
static int update_fronthaul_bsslist(struct node *n, struct netif_fhbss *fh);
static void node_getbssinfo(void *cntlr, void *resp, int len, void *cookie);
static int enumerate_topology_indirect(struct controller *c);
void stop_cntlr(struct controller *c)
{
if (!c) {
warn("%s: (cntlr = NULL)\n", __func__);
exit(0);
}
stop_test_logging();
ubus_unregister_event_handler(c->ubus_ctx, &c->ieee1905_evh);
ubus_unregister_event_handler(c->ubus_ctx, &c->ubusx_ev);
cntlr_remove_object(c);
uloop_done();
cntlr_config_clean(&c->cfg);
comm_destroy(c->comm);
ubus_free(c->ubus_ctx);
free(c);
}
static void cntlr_terminate(struct controller *c)
{
dbg("%s: called.\n", __func__);
stop_cntlr(c);
exit_alloctrace();
stop_logging();
//unlink(pidfile);
exit(0);
}
static struct node *find_node_by_mac(struct controller *c,
const unsigned char *mac)
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
{
struct node *p;
list_for_each_entry(p, &c->nodelist, list) {
if (!memcmp(p->hwaddr, mac, 6))
return p;
}
return NULL;
}
/* find node by ip address */
static struct node *find_node_by_ip(struct controller *c, const char *ip)
{
struct node *p;
struct in_addr ipn;
if (ip && strlen(ip) && !inet_aton(ip, &ipn)) {
warn("Invalid ipaddr: %s\n", ip);
return NULL;
}
list_for_each_entry(p, &c->nodelist, list) {
if (!memcmp(&p->ipaddr, &ipn, sizeof(struct in_addr)))
return p;
}
return NULL;
}
/* find node by any of its fh-bssid */
struct node *get_node_by_bssid(struct controller *c, unsigned char *bssid)
{
struct node *n;
struct netif_fhbss *p;
list_for_each_entry(n, &c->nodelist, list) {
list_for_each_entry(p, &n->fronthaul_iflist, list) {
if (memcmp(bssid, p->bssid, 6))
continue;
return n;
}
}
return NULL;
}
static void cntlr_log_nodes(struct controller *c)
{
struct node *n;
int i = 0;
list_for_each_entry(n, &c->nodelist, list) {
cntlr_dbg(" %d | & = %p , agent = %p, prefix = '%s')\n",
i++, n, n->uobj[4],
inet_ntoa(n->ipaddr));
}
}
static int forall_node_update_neighbors(struct controller *c)
{
struct node *n, *p;
if (c->num_nodes == 0)
return 0;
cntlr_dbg("num nodes = %d\n", c->num_nodes);
/* For single node, there will be no neighbors. So, delete
* neighbors from it, if any.
* A broadcast neighbor address passed to CMD_DEL_NEIGHBOR
* command will delete all neighbors from a node.
*/
if (c->num_nodes == 1) {
char nbrstr[12 + 1] = {0};
unsigned char nbrmac[6] = "\xff\xff\xff\xff\xff\xff";
n = list_first_entry(&c->nodelist, struct node, list);
if (!n)
return -1;
btostr((unsigned char *)nbrmac, sizeof(nbrmac), nbrstr);
cntlr_dbg("Sending DEL_NEIGHBOR Async to node (%p:%p)---->\n",
CMD(c->comm, c, (void *)(uintptr_t)n->uobj[4],
CMD_DEL_NEIGHBOR,
nbrstr, sizeof(nbrstr),
NULL, NULL);
return 0;
}
list_for_each_entry(n, &c->nodelist, list) {
list_for_each_entry(p, &c->nodelist, list) {
struct netif_fhbss *fh;
if (n == p)
continue;
list_for_each_entry(fh, &p->fronthaul_iflist, list) {
struct nbr nbr;
char nbrstr[2*sizeof(struct nbr) + 1] = {0};
memset(&nbr, 0, sizeof(nbr));
memcpy(nbr.bssid, fh->bssid, 6);
nbr.bssid_info = htonl(fh->bssid_info);
nbr.channel = fh->channel;
nbr.reg = fh->reg;
nbr.phy = fh->phy;
btostr((unsigned char *)&nbr, sizeof(nbr), nbrstr);
cntlr_dbg("Sending ADD_NEIGHBOR to node " \
" (%p:%p)---->\n",
n, n->uobj[4]);
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
CMD(c->comm, c, (void *)n->uobj[4],
CMD_ADD_NEIGHBOR,
&nbrstr, 2 * sizeof(nbr) + 1,
NULL, NULL);
}
}
}
return 0;
}
static void forall_but_node_restrict_sta(struct controller *c, struct node *ex,
uint8_t *sta)
{
#define sc_sz (sizeof(struct cmd_assoc_control))
unsigned char sccmd[sc_sz] = {0};
struct cmd_assoc_control *asc = (struct cmd_assoc_control *)sccmd;
char sccmd_str[2 * sc_sz + 1] = {0};
struct node *n;
asc->len = htonl(sizeof(asc));
asc->enable = htonl(1);
asc->duration = htonl(0); /* use fh-iface's assoc_ctrl_time */
asc->num = htonl(1);
memcpy(asc->stas, sta, 6);
btostr(sccmd, sc_sz, sccmd_str);
list_for_each_entry(n, &c->nodelist, list) {
if (n == ex)
continue;
cntlr_dbg("Send ASSOC_CONTROL to node (%p:%p)---->\n",
n, n->uobj[4]);
CMD(c->comm, c, (void *)n->uobj[4],
CMD_ASSOC_CONTROL,
&sccmd_str,
sizeof(sccmd_str),
NULL, NULL);
}
}
static int cntlr_monitor_sta(struct controller *c, uint8_t *sta,
struct node *sta_node)
{
char sta_macstr[12 + 1] = {0};
struct node *n;
btostr((unsigned char *)sta, 6, sta_macstr);
list_for_each_entry(n, &c->nodelist, list) {
/* skip monitoring on sta's current node */
if (n == sta_node)
continue;
if (list_empty(&n->fronthaul_iflist))
continue;
cntlr_dbg("Send MONITOR_STA to node (%p:%p)---->\n",
n, n->uobj[4]);
CMD(c->comm, c, (void *)n->uobj[4],
CMD_MONITOR_STA,
&sta_macstr, sizeof(sta_macstr),
NULL, NULL);
}
return 0;
}
static void node_get_monitor_sta(void *cntlr, void *resp, int len, void *cookie)
{
struct controller *c = (struct controller *)cntlr;
/* struct node *n = (struct node *)cookie; */
struct cmd_monitor_sta *info;
#define SZ sizeof(struct cmd_monitor_sta)
unsigned char rbytes[SZ] = {0};
struct active_sta *entry;
int nbr_rssi = 0;
cntlr_dbg("%s(): received: %s\n", __func__, (char *)resp);
strtob(resp, len, rbytes);
info = (struct cmd_monitor_sta *)rbytes;
/* TODO: info->rssi[0..3] */
nbr_rssi = (info->rssi[0] > 0) ? info->rssi[0] - 256 : info->rssi[0];
info->seen = ntohl(info->seen);
if (hwaddr_is_zero(info->addr) || nbr_rssi == 0) {
cntlr_dbg("Invalid GET_MONITOR resp!!!\n");
return;
}
entry = as_lookup(c->as_table, info->addr);
if (!entry)
return;
cntlr_dbg("STA " MACFMT " old best nbr-rssi = %d nbr-rssi = %d " \
"seen = %d secs ago\n",
MAC2STR(entry->hwaddr), entry->best_nbr_rssi,
nbr_rssi, info->seen);
if (entry->best_nbr_rssi < nbr_rssi &&
(info->seen >= 0 && info->seen < 30)) {
entry->best_nbr_rssi = nbr_rssi;
memcpy(entry->best_nbr, info->bss, 6);
cntlr_dbg("STA " MACFMT " better neighbor is " MACFMT \
" with rssi %d\n",
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
MAC2STR(info->addr),
MAC2STR(entry->best_nbr),
entry->best_nbr_rssi);
}
}
static int cntlr_handle_sta_rssi_low(struct controller *c, struct node *sta_n,
uint8_t *sta, int lowrssi)
{
struct node *n;
char sta_macstr[12 + 1] = {0};
struct active_sta *s_entry;
if (hwaddr_is_zero(sta))
return 0;
s_entry = as_lookup(c->as_table, sta);
if (!s_entry)
return -1;
/* reset lowrssi and best_nbr */
s_entry->lowrssi = lowrssi;
s_entry->best_nbr_rssi = -127;
memset(s_entry->best_nbr, 0, 6);
/* get this sta's monitored data from other nodes,
* if any available.
*/
btostr((unsigned char *)sta, 6, sta_macstr);
list_for_each_entry(n, &c->nodelist, list) {
if (n == sta_n)
continue; /* skip sta's currently assoc'd node */
cntlr_dbg("Send GET_MONITOR_STA to node (%p:%p)---->\n",
n, n->uobj[4]);
CMD(c->comm, c, (void *)n->uobj[4],
CMD_GET_MONITOR_STA,
&sta_macstr, sizeof(sta_macstr),
node_get_monitor_sta, n);
}
cntlr_dbg("%s: After doing get monitor sta .......\n", __func__);
if (hwaddr_is_zero(s_entry->best_nbr))
return 0;
#define lowrssi_thresh -65
#define diffrssi 9
if (lowrssi < lowrssi_thresh &&
(s_entry->best_nbr_rssi != -127 &&
s_entry->best_nbr_rssi - lowrssi >= diffrssi)) {
/* found a better neighbor for this lowrssi sta */
#define ss_sz (sizeof(struct cmd_steer_sta) + 6)
unsigned char sscmd[ss_sz] = {0};
struct cmd_steer_sta *ss = (struct cmd_steer_sta *)sscmd;
char sscmd_str[2 * ss_sz + 1] = {0};
struct node *best_nbr_node;
/* 1. assoc_control STA on all nodes but the best_nbr node */
best_nbr_node = get_node_by_bssid(c, s_entry->best_nbr);
if (best_nbr_node)
forall_but_node_restrict_sta(c, best_nbr_node, sta);
/* 2. send steer mandate */
memcpy(ss->sta, sta, 6);
ss->num = htonl(1);
memcpy(ss->nbrs, s_entry->best_nbr, 6);
btostr(sscmd, ss_sz, sscmd_str);
cntlr_dbg("CMD_STEER in agent with best nbr +++++++++++>\n");
cntlr_dbg("Send STEER to node (%p:%p)---->\n",
sta_n, sta_n->uobj[4]);
CMD(c->comm, c, (void *)sta_n->uobj[4],
CMD_STEER, &sscmd_str, sizeof(sscmd_str),
NULL, NULL);
}
return 0;
}
static bool endpoint_hwaddr(struct controller *c, unsigned char *sta)
{
struct node *n;
list_for_each_entry(n, &c->nodelist, list) {
if (!memcmp(n->hwaddr, sta, 6))
return false;
}
return true;
}
static void cntlr_handle_assoclist_resp(void *cntlr,
void *resp,
int len,
void *cookie)
{
struct controller *c = (struct controller *)cntlr;
struct netif_fhbss *p, *fh = (struct netif_fhbss *)cookie;
unsigned char rbytes[sizeof(struct cmd_get_assoclist)] = {0};
unsigned char *bssid = fh->bssid;
struct cmd_get_assoclist *info;
bool found = false;
int i, num_sta;
struct node *n;
/* cntlr_dbg("%s(): received: %s\n", __func__, (char *)resp); */
strtob(resp, len, rbytes);
info = (struct cmd_get_assoclist *)rbytes;
num_sta = ntohl(info->num);
/* TODO: include node pointer in netif_fhbss, so that
* we don't have to parse for node from fh.
*/
list_for_each_entry(n, &c->nodelist, list) {
list_for_each_entry(p, &n->fronthaul_iflist, list) {
if (p == fh) {
found = true;
break;
}
}
}
if (!found)
n = NULL;
for (i = 0; i < num_sta; i++) {
unsigned char *macaddr = &info->sta[6 * i];
if (!endpoint_hwaddr(c, macaddr))
continue;
as_insert(c->as_table, macaddr, bssid);
if (n) {
cntlr_dbg("Start Monitoring for STA " MACFMT " ....\n",
MAC2STR(macaddr));
cntlr_monitor_sta(c, macaddr, n);
}
}
}
/* XXX: now doing for only the first fh-iface of a node */
static void forall_node_get_assoclist(struct controller *c)
{
struct node *n;
int ret;
list_for_each_entry(n, &c->nodelist, list) {
struct netif_fhbss *p;
struct cmd_get_assoclist req;
char reqbuf[2 * sizeof(struct cmd_get_assoclist) + 1] = {0};
if (list_empty(&n->fronthaul_iflist))
continue;
p = list_first_entry(&n->fronthaul_iflist,
struct netif_fhbss, list);
if (!p)
continue;
memset(&req, 0, sizeof(req));
strncpy(req.ifname, p->ifname, 16);
btostr((unsigned char *)&req, sizeof(req), reqbuf);
ret = CMD(c->comm, c, (void *)n->uobj[4],
CMD_GET_ASSOCLIST,
reqbuf, sizeof(reqbuf),
cntlr_handle_assoclist_resp,
p);
if (ret)
cntlr_dbg("Failed to get assoclist (ret = %d)\n", ret);
}
}
int invoke_disconnect_sta(struct node *n, struct netif_fhbss *p,
unsigned char *hwaddr)
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
{
struct cmd_disconnect_sta ds = {0};
char s[2*sizeof(struct cmd_disconnect_sta) + 1] = {0};
int ret;
memcpy(ds.hwaddr, hwaddr, sizeof(ds.hwaddr));
strncpy(ds.iface, p->ifname, 16);
btostr((unsigned char *)&ds, sizeof(struct cmd_disconnect_sta), s);
ret = CMD(n->cntlr->comm, n->cntlr, (void *)n->uobj[4],
CMD_DISCONNECT_STA, s, sizeof(s), NULL, NULL);
return ret;
}
int invoke_disconnect_sta_by_bssid(struct controller *c, unsigned char *bssid,
unsigned char *hwaddr)
{
int ret = 0;
struct node *n;
struct netif_fhbss *p;
n = get_node_by_bssid(c, bssid);
if (!n)
return -1;
list_for_each_entry(p, &n->fronthaul_iflist, list) {
if (memcmp(bssid, p->bssid, 6))
continue;
invoke_disconnect_sta(n, p, hwaddr);
break;
}
return ret;
}
static void disconnect_repeated_macs(struct controller *c, struct node *n,
unsigned char *sta)
{
struct active_sta *as;
int hidx = as_hash(sta);
hlist_for_each_entry(as, &c->as_table[hidx], hlist) {
if (as->hwaddr[3] ^ sta[3] ||
as->hwaddr[4] ^ sta[4] ||
as->hwaddr[5] ^ sta[5])
continue;
if (as->hwaddr[1] != n->mobility_domain[0] ||
as->hwaddr[2] != n->mobility_domain[1])
continue;
if (!match_oui0((unsigned char *)n->oui0, as->hwaddr, n->ouis))
continue;
if (memcmp(sta, as->hwaddr, 6) == 0)
continue;
dbg("Disconnecting redundant repeated client " MACFMT "\n",
MAC2STR(as->hwaddr));
invoke_disconnect_sta_by_bssid(c, as->bssid, as->hwaddr);
invoke_disconnect_sta_by_bssid(c, as->old_bssid, as->hwaddr);
}
}
static int parse_sta_event(struct controller *c, const char *evtype,
struct blob_attr *msg)
{
struct netif_fhbss *p;
char macaddr[18] = {0}, action[64] = {0}, vif[16] = {0};
unsigned char sta_macaddr[6], bssid[6] = {0};
bool found = false;
struct active_sta *as;
uint32_t lowrssi = 0;
int rv;
enum {
MACADDR,
ACTION,
VIF,
LOWRSSI,
__POLICY_MAX
};
const struct blobmsg_policy sta_policy[__POLICY_MAX] = {
[MACADDR] = {.name = "macaddr", .type = BLOBMSG_TYPE_STRING},
[ACTION] = {.name = "action", .type = BLOBMSG_TYPE_STRING},
[VIF] = {.name = "vif", .type = BLOBMSG_TYPE_STRING},
[LOWRSSI] = {.name = "lowrssi", .type = BLOBMSG_TYPE_INT32},
};
struct blob_attr *tb[__POLICY_MAX];
char sta_node[128] = {0};
char *sta_p;
struct in_addr ipn;
struct node *n;
sta_p = strstr(evtype, "wifi.sta");
snprintf(sta_node, sta_p - evtype, "%s", evtype);
if (strlen(sta_node) && !inet_aton(sta_node, &ipn)) {
warn("Invalid 'wifi.sta' prefix: %s\n", sta_node);
return -1;
}
if (strlen(sta_node))
n = find_node_by_ip(c, sta_node);
else
n = find_node_by_ip(c, "127.0.0.1");
rv = blobmsg_parse(sta_policy, __POLICY_MAX, tb,
blobmsg_data(msg), blobmsg_len(msg));
if (rv < 0) {
cntlr_dbg("Error parsing wifi.sta events!\n");
goto fail;
}
if (!tb[MACADDR] || !tb[VIF] || !tb[ACTION])
goto fail;
strncpy(macaddr, blobmsg_get_string(tb[MACADDR]), 17);
if (hwaddr_aton(macaddr, sta_macaddr) == NULL)
goto fail;
strncpy(vif, blobmsg_get_string(tb[VIF]), 15);
if (tb[LOWRSSI]) {
lowrssi = blobmsg_get_u32(tb[LOWRSSI]);
if (lowrssi != 0) {
cntlr_dbg("Received wifi.sta 'lowrssi' (%d) event " \
"+++++++++++++>\n", lowrssi);
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
return cntlr_handle_sta_rssi_low(c, n, sta_macaddr, lowrssi);
}
}
strncpy(action, blobmsg_get_string(tb[ACTION]), 63);
/* At this instant when the associated STA is checked to see if it is
* backhaul wifi-iface of a node, the nodelist does not include this
* STA.
* We check again for pure STA from wifi.agent objects' appear/disappear
* event handler.
*/
if (!endpoint_hwaddr(c, sta_macaddr)) {
cntlr_dbg("STA %s is NOT a pure STA!\n", macaddr);
goto fail;
}
/* TODO: wifi.sta events should contain bssid too */
list_for_each_entry(p, &n->fronthaul_iflist, list) {
if (strncmp(p->ifname, vif, 16) != 0)
continue;
memcpy(bssid, p->bssid, 6);
found = true;
break;
}
if (!found)
goto fail;
if (strncmp(action, "disassociated", strlen("disassociated") + 1) == 0) {
as = as_lookup(c->as_table, sta_macaddr);
/* if disassociate event came from most recently recorded bssid,
* we must have missed the disassociate event from previous one
*/
if (as && memcmp(as->bssid, bssid, 6) == 0)
invoke_disconnect_sta_by_bssid(n->cntlr,
as->old_bssid,
as->hwaddr);
as_clean_entry(c->as_table, sta_macaddr, bssid);
} else if (strncmp(action, "associated", strlen("associated") + 1) == 0) {
as = as_lookup(c->as_table, sta_macaddr);
if (as) {
if (memcmp(as->bssid, bssid, 6) != 0) {
struct node *prev_ap;
char hwaddrstr[12 + 1] = {0};
/* if entry already exists, it must not have
* receieved dissassoc event;
* issue disconnect_sta to previous bssid.
*/
invoke_disconnect_sta_by_bssid(n->cntlr,
as->bssid,
as->hwaddr);
/* flush from arp table of previous bssid */
prev_ap = get_node_by_bssid(c, as->bssid);
if (!prev_ap)
return -1;
btostr(as->hwaddr, sizeof(as->hwaddr),
hwaddrstr);
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
CMD(prev_ap->cntlr->comm, prev_ap->cntlr,
(void *)prev_ap->uobj[4],
CMD_FLUSH_ARP,
hwaddrstr, sizeof(hwaddrstr),
NULL, NULL);
}
}
/* disconnect redundant repeated addresses if real mac is seen
* locally, or a repeated mac is seen
*/
/* TODO TODO: check this logic.. again */
if ((sta_macaddr[1] != n->mobility_domain[0] ||
sta_macaddr[2] != n->mobility_domain[1]) ||
(sta_macaddr[1] == n->mobility_domain[0] &&
sta_macaddr[2] == n->mobility_domain[1] &&
match_oui0(n->oui0, sta_macaddr, n->ouis))) {
disconnect_repeated_macs(c, n, sta_macaddr);
}
as = as_insert(c->as_table, sta_macaddr, bssid);
/* instruct agents to start monitoring this sta */
cntlr_dbg("Start Monitoring for STA " MACFMT " ....\n",
MAC2STR(sta_macaddr));
cntlr_monitor_sta(c, sta_macaddr, n);
}
as_print(c->as_table);
return 0;
fail:
return -1;
}
static void node_event_handler(struct ubus_context *ctx,
struct ubus_event_handler *ev,
const char *type,
struct blob_attr *msg)
{
struct node *n = container_of(ev, struct node, evh);
char *str;
str = blobmsg_format_json(msg, true);
if (!str)
return;
cntlr_dbg("[ &node = %p ] Received event = '%s': %s\n", n, type, str);
if (strstr(type, "wifi.sta"))
parse_sta_event(n->cntlr, type, msg);
/* handle other events as needed */
free(str);
}
static void system_info_resp_handler(struct ubus_request *req, int type,
struct blob_attr *msg)
{
struct node *n = (struct node *)req->priv;
struct json_object *json_msg;
char *json_str;
loud("Response from peer: 0x%x\n", req->peer);
json_str = blobmsg_format_json(msg, true);
if (!json_str)
return;
json_msg = json_tokener_parse(json_str);
if (!json_msg)
goto out_str;
if (!json_object_is_type(json_msg, json_type_object))
goto out_json;
if (n->uobj[0] == req->peer) {
/* handle resp for system info */
json_object_object_foreach(json_msg, key, val) {
if (!strncmp(key, "system", 6)) {
struct json_object *uptime_obj;
json_object_object_get_ex(val, "uptime", &uptime_obj);
if (!uptime_obj &&
!json_object_is_type(uptime_obj,
json_type_int)) {
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
continue;
}
/* TODO - in node data struct */
/* sys.attr.uptime = json_object_get_int(uptime_obj); */
} else if (!strncmp(key, "memoryKB", 8)) {
struct json_object *mtotal_obj, *mfree_obj;
int mtotal = 0, mfree = 0;
json_object_object_get_ex(val, "total", &mtotal_obj);
json_object_object_get_ex(val, "free", &mfree_obj);
mtotal = json_object_get_int(mtotal_obj);
mfree = json_object_get_int(mfree_obj);
/* sys.attr.memfree = mfree * 100 / mtotal; */
}
}
} else if (n->uobj[1] == req->peer) {
/* handle resp for system load */
json_object_object_foreach(json_msg, key, val) {
if (!strncmp(key, "load", 4)) {
struct json_object *l1_obj, *l5_obj, *l15_obj;
json_object_object_get_ex(val, "min_1", &l1_obj);
json_object_object_get_ex(val, "min_5", &l5_obj);
json_object_object_get_ex(val, "min_15", &l15_obj);
/* sys.attr.avgcpu_1 = json_object_get_int(l1_obj); */
/* sys.attr.avgcpu_5 = json_object_get_int(l5_obj); */
/* sys.attr.avgcpu_15 = json_object_get_int(l15_obj); */
}
}
}
out_json:
json_object_put(json_msg);
out_str:
free(json_str);
}
static int get_node_system_info(struct controller *c, struct node *n)
{
struct blob_buf bb = {};
int ret;
blob_buf_init(&bb, 0);
loud("Request to peer: 0x%x\n", n->uobj[0]);
ret = ubus_invoke(c->ubus_ctx, n->uobj[0], "info", bb.head,
system_info_resp_handler, n, 2 * 1000);
if (ret)
cntlr_dbg("Failed to get node's sys data1 (ret = %d)\n", ret);
blob_buf_free(&bb);
blob_buf_init(&bb, 0);
loud("Request to peer: 0x%x\n", n->uobj[1]);
ret = ubus_invoke(c->ubus_ctx, n->uobj[1], "load", bb.head,
system_info_resp_handler, n, 2 * 1000);
if (ret)
cntlr_dbg("Failed to get node's sys data2 (ret = %d)\n", ret);
blob_buf_free(&bb);
return ret;
}
static void node_getoui(void *cntlr, void *resp, int len, void *cookie)
{
unsigned char rbytes[sizeof(struct cmd_get_oui)] = {0};
struct node *n = (struct node *)cookie;
struct cmd_get_oui *oui;
int i;
strtob(resp, len, rbytes);
oui = (struct cmd_get_oui *) rbytes;
memcpy(n->oui0, oui->oui0, sizeof(n->oui0));
n->ouis = oui->ouis;
if (n->ouis)
dbg("Received %d different oui0s\n", n->ouis);
for (i = 0; i < n->ouis; i++)
dbg("oui0[%d] = %d\n", i, n->oui0[i]);
}
static void node_getmobid(void *cntlr, void *resp, int len, void *cookie)
{
struct node *n = (struct node *)cookie;
strtob(resp, len, n->mobility_domain);
dbg("Received mobility domain = %02x%02x\n",
n->mobility_domain[0], n->mobility_domain[1]);
}
static int get_node_oui(struct controller *c, struct in_addr *ipn)
{
struct node *n;
list_for_each_entry(n, &c->nodelist, list) {
if (memcmp(ipn, &n->ipaddr, sizeof(struct in_addr)))
continue;
CMD(c->comm, c, (void *)n->uobj[4], CMD_GET_OUI, NULL, 0,
node_getoui, n);
}
return 0;
}
static int get_node_mobid(struct controller *c, struct in_addr *ipn)
{
struct node *n;
list_for_each_entry(n, &c->nodelist, list) {
if (memcmp(ipn, &n->ipaddr, sizeof(struct in_addr)))
continue;
CMD(c->comm, c, (void *)n->uobj[4], CMD_GET_MOBID, NULL, 0,
node_getmobid, n);
}
return 0;
}
static void forall_node_get_repeater_hwmasks(struct controller *c)
{
struct node *n;
list_for_each_entry(n, &c->nodelist, list) {
CMD_ASYNC(c->comm, c, (void *)n->uobj[4], CMD_GET_MOBID, NULL,
0, node_getmobid, n);
CMD_ASYNC(c->comm, c, (void *)n->uobj[4], CMD_GET_OUI, NULL, 0,
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
}
}
static void forall_node_get_fhinfo(struct controller *c)
{
struct node *n;
list_for_each_entry(n, &c->nodelist, list) {
cntlr_dbg("CMD GET_BSSINFO to agent @ (%p) " MACFMT "\n",
(void *)n->uobj[4], MAC2STR(n->hwaddr));
CMD_ASYNC(c->comm, c, (void *)n->uobj[4], CMD_GET_BSS_INFO,
NULL, 0, node_getbssinfo, n);
}
}
#if 0
static void refresh_node_info(struct uloop_timeout *t)
{
struct node *n = container_of(t, struct node, refresh_timer);
struct controller *c = n->cntlr;
/* get_node_system_info(c, n); */
/* get_node_wifi_info(c, n); */
CMD(c->comm, c, (void *)n->uobj[4], CMD_GET_BSS_INFO, NULL, 0,
node_getbssinfo, n);
uloop_timeout_set(&n->refresh_timer, NODE_STATS_INTERVAL);
}
#endif
static int update_node(struct controller *c, struct node *n)
{
char *uobj[] = {
"router.system", /* info */
"router.graph", /* load */
"router.network", /* clients */
"wifix", /* radios, status, assoclist, stas, list_neighbor */
"wifi.agent", /* cmd, ... */
NULL };
int j;
const char *ipstr;
ipstr = inet_ntoa(n->ipaddr);
/* update refs to node's (ubus-x) objects */
for (j = 0; j < MAX_UOBJECTS && uobj[j]; j++) {
char uobjpath[128] = {0};
int ret;
snprintf(uobjpath, 128, "%s%s%s", ipstr,
strlen(ipstr) ? "/" : "", uobj[j]);
ret = ubus_lookup_id(c->ubus_ctx, uobjpath, &n->uobj[j]);
if (ret != UBUS_STATUS_OK) {
n->uobj[j] = OBJECT_INVALID;
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
cntlr_dbg("object '%s' not present!\n", uobjpath);
continue;
}
}
return 0;
}
static struct node *alloc_node_init(struct controller *c, char *ipstr)
{
struct node *n;
char *uobj[] = {
"router.system", /* info */
"router.graph", /* load */
"router.network", /* clients */
"wifix", /* radios, status, assoclist, stas, list_neighbor */
"wifi.agent", /* cmd, ... */
NULL };
char *evmask[] = { "wifi*", "client*", NULL };
char ev[128] = {0};
int j;
n = calloc(1, sizeof(struct node));
if (!n) {
warn("OOM: node malloc failed!\n");
return NULL;
}
n->cntlr = c;
/* n->refresh_timer.cb = refresh_node_info; */
INIT_LIST_HEAD(&n->stalist);
INIT_LIST_HEAD(&n->fronthaul_iflist);
n->depth = -1;
if (ipstr && strlen(ipstr))
inet_pton(AF_INET, ipstr, &n->ipaddr);
else
inet_pton(AF_INET, "127.0.0.1", &n->ipaddr);
n->scan_supported = true;
/* get refs to node (ubus-x) objects we are interested in */
for (j = 0; j < MAX_UOBJECTS && uobj[j]; j++) {
char uobjpath[128] = {0};
int ret;
snprintf(uobjpath, 128, "%s%s%s", ipstr,
strlen(ipstr) ? "/" : "", uobj[j]);
ret = ubus_lookup_id(c->ubus_ctx, uobjpath, &n->uobj[j]);
if (ret != UBUS_STATUS_OK) {
n->uobj[j] = OBJECT_INVALID;
cntlr_dbg("object '%s' not present!\n", uobjpath);
continue;