2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
15 * Copyright (C) 2020 embedd.ch
16 * Copyright (C) 2020 Felix Fietkau <nbd@nbd.name>
17 * Copyright (C) 2020 John Crispin <john@phrozen.org>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
24 #include <arpa/inet.h>
28 #include <libubox/vlist.h>
29 #include <libubox/avl-cmp.h>
30 #include <libubox/usock.h>
35 static uint32_t local_id
;
36 static struct uloop_fd remote_fd
;
37 static struct uloop_timeout remote_timer
;
38 static struct uloop_timeout reload_timer
;
40 static struct blob_buf buf
;
41 static uint32_t msg_seq
;
44 struct vlist_node node
;
49 interfaces_update_cb(struct vlist_tree
*tree
,
50 struct vlist_node
*node_new
,
51 struct vlist_node
*node_old
);
53 static int remote_host_cmp(const void *k1
, const void *k2
, void *ptr
)
55 unsigned long v1
= (unsigned long) k1
;
56 unsigned long v2
= (unsigned long) k2
;
61 static VLIST_TREE(interfaces
, avl_strcmp
, interfaces_update_cb
, true, true);
62 LIST_HEAD(remote_nodes
);
63 AVL_TREE(remote_hosts
, remote_host_cmp
, false, NULL
);
66 interface_name(struct interface
*iface
)
68 return iface
->node
.avl
.key
;
72 interface_check(struct interface
*iface
)
74 iface
->ifindex
= if_nametoindex(interface_name(iface
));
75 uloop_timeout_set(&reload_timer
, 1);
79 interface_init(struct interface
*iface
)
81 interface_check(iface
);
85 interface_free(struct interface
*iface
)
87 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
92 interfaces_update_cb(struct vlist_tree
*tree
,
93 struct vlist_node
*node_new
,
94 struct vlist_node
*node_old
)
96 struct interface
*iface
;
98 if (node_new
&& node_old
) {
99 iface
= container_of(node_new
, struct interface
, node
);
101 iface
= container_of(node_old
, struct interface
, node
);
102 interface_check(iface
);
103 } else if (node_old
) {
104 iface
= container_of(node_old
, struct interface
, node
);
105 interface_free(iface
);
107 iface
= container_of(node_new
, struct interface
, node
);
108 interface_init(iface
);
112 void usteer_interface_add(const char *name
)
114 struct interface
*iface
;
117 iface
= calloc_a(sizeof(*iface
), &name_buf
, strlen(name
) + 1);
118 strcpy(name_buf
, name
);
119 vlist_add(&interfaces
, &iface
->node
, name_buf
);
122 void config_set_interfaces(struct blob_attr
*data
)
124 struct blob_attr
*cur
;
130 if (!blobmsg_check_attr_list(data
, BLOBMSG_TYPE_STRING
))
133 vlist_update(&interfaces
);
134 blobmsg_for_each_attr(cur
, data
, rem
) {
135 usteer_interface_add(blobmsg_data(cur
));
137 vlist_flush(&interfaces
);
140 void config_get_interfaces(struct blob_buf
*buf
)
142 struct interface
*iface
;
145 c
= blobmsg_open_array(buf
, "interfaces");
146 vlist_for_each_element(&interfaces
, iface
, node
) {
147 blobmsg_add_string(buf
, NULL
, interface_name(iface
));
149 blobmsg_close_array(buf
, c
);
153 interface_add_station(struct usteer_remote_node
*node
, struct blob_attr
*data
)
157 struct apmsg_sta msg
;
160 if (!parse_apmsg_sta(&msg
, data
)) {
161 MSG(DEBUG
, "Cannot parse station in message\n");
165 if (msg
.timeout
<= 0) {
166 MSG(DEBUG
, "Refuse to add an already expired station entry\n");
170 sta
= usteer_sta_get(msg
.addr
, true);
174 si
= usteer_sta_info_get(sta
, &node
->node
, &create
);
178 si
->connected
= msg
.connected
;
179 si
->signal
= msg
.signal
;
180 si
->seen
= current_time
- msg
.seen
;
181 usteer_sta_info_update_timeout(si
, msg
.timeout
);
185 remote_node_free(struct usteer_remote_node
*node
)
187 struct usteer_remote_host
*host
= node
->host
;
189 list_del(&node
->list
);
190 list_del(&node
->host_list
);
191 usteer_sta_node_cleanup(&node
->node
);
194 if (!list_empty(&host
->nodes
))
197 avl_delete(&remote_hosts
, &host
->avl
);
201 static struct usteer_remote_host
*
202 interface_get_host(const char *addr
, unsigned long id
)
204 struct usteer_remote_host
*host
;
206 host
= avl_find_element(&remote_hosts
, (void *)id
, host
, avl
);
210 host
= calloc(1, sizeof(*host
));
211 host
->avl
.key
= (void *)id
;
212 INIT_LIST_HEAD(&host
->nodes
);
213 avl_insert(&remote_hosts
, &host
->avl
);
216 if (host
->addr
&& strcmp(host
->addr
, addr
) != 0)
220 host
->addr
= strdup(addr
);
225 static struct usteer_remote_node
*
226 interface_get_node(struct usteer_remote_host
*host
, const char *name
)
228 struct usteer_remote_node
*node
;
229 int addr_len
= strlen(host
->addr
);
232 list_for_each_entry(node
, &host
->nodes
, host_list
)
233 if (!strcmp(node
->name
, name
))
236 node
= calloc_a(sizeof(*node
), &buf
, addr_len
+ 1 + strlen(name
) + 1);
237 node
->node
.type
= NODE_TYPE_REMOTE
;
239 sprintf(buf
, "%s#%s", host
->addr
, name
);
240 node
->node
.avl
.key
= buf
;
241 node
->name
= buf
+ addr_len
+ 1;
243 INIT_LIST_HEAD(&node
->node
.sta_info
);
245 list_add_tail(&node
->list
, &remote_nodes
);
246 list_add_tail(&node
->host_list
, &host
->nodes
);
252 interface_add_node(struct usteer_remote_host
*host
, struct blob_attr
*data
)
254 struct usteer_remote_node
*node
;
255 struct apmsg_node msg
;
256 struct blob_attr
*cur
;
259 if (!parse_apmsg_node(&msg
, data
)) {
260 MSG(DEBUG
, "Cannot parse node in message\n");
264 node
= interface_get_node(host
, msg
.name
);
266 node
->node
.freq
= msg
.freq
;
267 node
->node
.n_assoc
= msg
.n_assoc
;
268 node
->node
.max_assoc
= msg
.max_assoc
;
269 node
->node
.noise
= msg
.noise
;
270 node
->node
.load
= msg
.load
;
271 snprintf(node
->node
.ssid
, sizeof(node
->node
.ssid
), "%s", msg
.ssid
);
272 usteer_node_set_blob(&node
->node
.rrm_nr
, msg
.rrm_nr
);
273 usteer_node_set_blob(&node
->node
.node_info
, msg
.node_info
);
275 blob_for_each_attr(cur
, msg
.stations
, rem
)
276 interface_add_station(node
, cur
);
280 interface_recv_msg(struct interface
*iface
, struct in_addr
*addr
, void *buf
, int len
)
282 struct usteer_remote_host
*host
;
283 char addr_str
[INET_ADDRSTRLEN
];
284 struct blob_attr
*data
= buf
;
286 struct blob_attr
*cur
;
289 if (blob_pad_len(data
) != len
) {
290 MSG(DEBUG
, "Invalid message length (header: %d, real: %d)\n", blob_pad_len(data
), len
);
294 if (!parse_apmsg(&msg
, data
)) {
295 MSG(DEBUG
, "Missing fields in message\n");
299 if (msg
.id
== local_id
)
302 MSG(NETWORK
, "Received message on %s (id=%08x->%08x seq=%d len=%d)\n",
303 interface_name(iface
), msg
.id
, local_id
, msg
.seq
, len
);
305 inet_ntop(AF_INET
, addr
, addr_str
, sizeof(addr_str
));
307 host
= interface_get_host(addr_str
, msg
.id
);
309 blob_for_each_attr(cur
, msg
.nodes
, rem
)
310 interface_add_node(host
, cur
);
313 static struct interface
*
314 interface_find_by_ifindex(int index
)
316 struct interface
*iface
;
318 vlist_for_each_element(&interfaces
, iface
, node
) {
319 if (iface
->ifindex
== index
)
327 interface_recv(struct uloop_fd
*u
, unsigned int events
)
329 static char buf
[APMGR_BUFLEN
];
330 static char cmsg_buf
[( CMSG_SPACE(sizeof(struct in_pktinfo
)) + sizeof(int)) + 1];
331 static struct sockaddr_in sin
;
332 static struct iovec iov
= {
334 .iov_len
= sizeof(buf
)
336 static struct msghdr msg
= {
338 .msg_namelen
= sizeof(sin
),
341 .msg_control
= cmsg_buf
,
342 .msg_controllen
= sizeof(cmsg_buf
),
344 struct cmsghdr
*cmsg
;
348 struct in_pktinfo
*pkti
= NULL
;
349 struct interface
*iface
;
351 len
= recvmsg(u
->fd
, &msg
, 0);
365 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
366 if (cmsg
->cmsg_type
!= IP_PKTINFO
)
369 pkti
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
373 MSG(DEBUG
, "Received packet without ifindex\n");
377 iface
= interface_find_by_ifindex(pkti
->ipi_ifindex
);
379 MSG(DEBUG
, "Received packet from unconfigured interface %d\n", pkti
->ipi_ifindex
);
383 interface_recv_msg(iface
, &sin
.sin_addr
, buf
, len
);
387 static void interface_send_msg(struct interface
*iface
, struct blob_attr
*data
)
389 static size_t cmsg_data
[( CMSG_SPACE(sizeof(struct in_pktinfo
)) / sizeof(size_t)) + 1];
390 static struct sockaddr_in a
;
391 static struct iovec iov
;
392 static struct msghdr m
= {
393 .msg_name
= (struct sockaddr
*) &a
,
394 .msg_namelen
= sizeof(a
),
397 .msg_control
= cmsg_data
,
398 .msg_controllen
= CMSG_LEN(sizeof(struct in_pktinfo
)),
400 struct in_pktinfo
*pkti
;
401 struct cmsghdr
*cmsg
;
403 a
.sin_family
= AF_INET
;
404 a
.sin_port
= htons(16720);
405 a
.sin_addr
.s_addr
= ~0;
407 memset(cmsg_data
, 0, sizeof(cmsg_data
));
408 cmsg
= CMSG_FIRSTHDR(&m
);
409 cmsg
->cmsg_len
= m
.msg_controllen
;
410 cmsg
->cmsg_level
= IPPROTO_IP
;
411 cmsg
->cmsg_type
= IP_PKTINFO
;
413 pkti
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
414 pkti
->ipi_ifindex
= iface
->ifindex
;
417 iov
.iov_len
= blob_pad_len(data
);
419 if (sendmsg(remote_fd
.fd
, &m
, 0) < 0)
423 static void usteer_send_sta_info(struct sta_info
*sta
)
425 int seen
= current_time
- sta
->seen
;
428 c
= blob_nest_start(&buf
, 0);
429 blob_put(&buf
, APMSG_STA_ADDR
, sta
->sta
->addr
, 6);
430 blob_put_int8(&buf
, APMSG_STA_CONNECTED
, !!sta
->connected
);
431 blob_put_int32(&buf
, APMSG_STA_SIGNAL
, sta
->signal
);
432 blob_put_int32(&buf
, APMSG_STA_SEEN
, seen
);
433 blob_put_int32(&buf
, APMSG_STA_TIMEOUT
, config
.local_sta_timeout
- seen
);
434 blob_nest_end(&buf
, c
);
437 static void usteer_send_node(struct usteer_node
*node
, struct sta_info
*sta
)
441 c
= blob_nest_start(&buf
, 0);
443 blob_put_string(&buf
, APMSG_NODE_NAME
, usteer_node_name(node
));
444 blob_put_string(&buf
, APMSG_NODE_SSID
, node
->ssid
);
445 blob_put_int32(&buf
, APMSG_NODE_FREQ
, node
->freq
);
446 blob_put_int32(&buf
, APMSG_NODE_NOISE
, node
->noise
);
447 blob_put_int32(&buf
, APMSG_NODE_LOAD
, node
->load
);
448 blob_put_int32(&buf
, APMSG_NODE_N_ASSOC
, node
->n_assoc
);
449 blob_put_int32(&buf
, APMSG_NODE_MAX_ASSOC
, node
->max_assoc
);
451 r
= blob_nest_start(&buf
, APMSG_NODE_RRM_NR
);
452 blobmsg_add_field(&buf
, BLOBMSG_TYPE_ARRAY
, "",
453 blobmsg_data(node
->rrm_nr
),
454 blobmsg_data_len(node
->rrm_nr
));
455 blob_nest_end(&buf
, r
);
459 blob_put(&buf
, APMSG_NODE_NODE_INFO
,
460 blob_data(node
->node_info
),
461 blob_len(node
->node_info
));
463 s
= blob_nest_start(&buf
, APMSG_NODE_STATIONS
);
466 usteer_send_sta_info(sta
);
468 list_for_each_entry(sta
, &node
->sta_info
, node_list
)
469 usteer_send_sta_info(sta
);
472 blob_nest_end(&buf
, s
);
474 blob_nest_end(&buf
, c
);
478 usteer_check_timeout(void)
480 struct usteer_remote_node
*node
, *tmp
;
481 int timeout
= config
.remote_node_timeout
/ config
.remote_update_interval
;
483 list_for_each_entry_safe(node
, tmp
, &remote_nodes
, list
) {
484 if (node
->check
++ > timeout
)
485 remote_node_free(node
);
490 usteer_update_init(void)
492 blob_buf_init(&buf
, 0);
493 blob_put_int32(&buf
, APMSG_ID
, local_id
);
494 blob_put_int32(&buf
, APMSG_SEQ
, ++msg_seq
);
496 return blob_nest_start(&buf
, APMSG_NODES
);
500 usteer_update_send(void *c
)
502 struct interface
*iface
;
504 blob_nest_end(&buf
, c
);
506 vlist_for_each_element(&interfaces
, iface
, node
)
507 interface_send_msg(iface
, buf
.head
);
511 usteer_send_sta_update(struct sta_info
*si
)
513 void *c
= usteer_update_init();
514 usteer_send_node(si
->node
, si
);
515 usteer_update_send(c
);
519 usteer_send_update_timer(struct uloop_timeout
*t
)
521 struct usteer_node
*node
;
524 usteer_update_time();
525 uloop_timeout_set(t
, config
.remote_update_interval
);
527 c
= usteer_update_init();
528 for_each_local_node(node
)
529 usteer_send_node(node
, NULL
);
531 usteer_update_send(c
);
532 usteer_check_timeout();
536 usteer_init_local_id(void)
540 f
= fopen("/dev/urandom", "r");
542 perror("fopen(/dev/urandom)");
546 if (fread(&local_id
, sizeof(local_id
), 1, f
) < 1)
554 usteer_reload_timer(struct uloop_timeout
*t
)
559 if (remote_fd
.registered
) {
560 uloop_fd_delete(&remote_fd
);
564 fd
= usock(USOCK_UDP
| USOCK_SERVER
| USOCK_NONBLOCK
|
565 USOCK_NUMERIC
| USOCK_IPV4ONLY
,
566 "0.0.0.0", APMGR_PORT_STR
);
572 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &yes
, sizeof(yes
)) < 0)
573 perror("setsockopt(IP_PKTINFO)");
575 if (setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, &yes
, sizeof(yes
)) < 0)
576 perror("setsockopt(SO_BROADCAST)");
579 remote_fd
.cb
= interface_recv
;
580 uloop_fd_add(&remote_fd
, ULOOP_READ
);
583 int usteer_interface_init(void)
585 if (usteer_init_local_id())
588 remote_timer
.cb
= usteer_send_update_timer
;
589 remote_timer
.cb(&remote_timer
);
591 reload_timer
.cb
= usteer_reload_timer
;
592 reload_timer
.cb(&reload_timer
);