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
))
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
, char *addr_str
, void *buf
, int len
)
282 struct usteer_remote_host
*host
;
283 struct blob_attr
*data
= buf
;
285 struct blob_attr
*cur
;
288 if (blob_pad_len(data
) != len
) {
289 MSG(DEBUG
, "Invalid message length (header: %d, real: %d)\n", blob_pad_len(data
), len
);
293 if (!parse_apmsg(&msg
, data
)) {
294 MSG(DEBUG
, "Missing fields in message\n");
298 if (msg
.id
== local_id
)
301 MSG(NETWORK
, "Received message on %s (id=%08x->%08x seq=%d len=%d)\n",
302 interface_name(iface
), msg
.id
, local_id
, msg
.seq
, len
);
304 host
= interface_get_host(addr_str
, msg
.id
);
305 usteer_node_set_blob(&host
->host_info
, msg
.host_info
);
307 blob_for_each_attr(cur
, msg
.nodes
, rem
)
308 interface_add_node(host
, cur
);
311 static struct interface
*
312 interface_find_by_ifindex(int index
)
314 struct interface
*iface
;
316 vlist_for_each_element(&interfaces
, iface
, node
) {
317 if (iface
->ifindex
== index
)
325 interface_recv_v4(struct uloop_fd
*u
, unsigned int events
)
327 static char buf
[APMGR_BUFLEN
];
328 static char cmsg_buf
[( CMSG_SPACE(sizeof(struct in_pktinfo
)) + sizeof(int)) + 1];
329 static struct sockaddr_in sin
;
330 char addr_str
[INET_ADDRSTRLEN
];
331 static struct iovec iov
= {
333 .iov_len
= sizeof(buf
)
335 static struct msghdr msg
= {
337 .msg_namelen
= sizeof(sin
),
340 .msg_control
= cmsg_buf
,
341 .msg_controllen
= sizeof(cmsg_buf
),
343 struct cmsghdr
*cmsg
;
347 struct in_pktinfo
*pkti
= NULL
;
348 struct interface
*iface
;
350 len
= recvmsg(u
->fd
, &msg
, 0);
364 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
365 if (cmsg
->cmsg_type
!= IP_PKTINFO
)
368 pkti
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
372 MSG(DEBUG
, "Received packet without ifindex\n");
376 iface
= interface_find_by_ifindex(pkti
->ipi_ifindex
);
378 MSG(DEBUG
, "Received packet from unconfigured interface %d\n", pkti
->ipi_ifindex
);
382 inet_ntop(AF_INET
, &sin
.sin_addr
, addr_str
, sizeof(addr_str
));
384 interface_recv_msg(iface
, addr_str
, buf
, len
);
389 static void interface_recv_v6(struct uloop_fd
*u
, unsigned int events
){
390 static char buf
[APMGR_BUFLEN
];
391 static char cmsg_buf
[( CMSG_SPACE(sizeof(struct in6_pktinfo
)) + sizeof(int)) + 1];
392 static struct sockaddr_in6 sin
;
393 static struct iovec iov
= {
395 .iov_len
= sizeof(buf
)
397 static struct msghdr msg
= {
399 .msg_namelen
= sizeof(sin
),
402 .msg_control
= cmsg_buf
,
403 .msg_controllen
= sizeof(cmsg_buf
),
405 struct cmsghdr
*cmsg
;
406 char addr_str
[INET6_ADDRSTRLEN
];
410 struct in6_pktinfo
*pkti
= NULL
;
411 struct interface
*iface
;
413 len
= recvmsg(u
->fd
, &msg
, 0);
427 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
428 if (cmsg
->cmsg_type
!= IPV6_PKTINFO
)
431 pkti
= (struct in6_pktinfo
*) CMSG_DATA(cmsg
);
435 MSG(DEBUG
, "Received packet without ifindex\n");
439 iface
= interface_find_by_ifindex(pkti
->ipi6_ifindex
);
441 MSG(DEBUG
, "Received packet from unconfigured interface %d\n", pkti
->ipi6_ifindex
);
445 inet_ntop(AF_INET6
, &sin
.sin6_addr
, addr_str
, sizeof(addr_str
));
446 if (sin
.sin6_addr
.s6_addr
[0] == 0) {
447 /* IPv4 mapped address. Ignore. */
451 interface_recv_msg(iface
, addr_str
, buf
, len
);
455 static void interface_send_msg_v4(struct interface
*iface
, struct blob_attr
*data
)
457 static size_t cmsg_data
[( CMSG_SPACE(sizeof(struct in_pktinfo
)) / sizeof(size_t)) + 1];
458 static struct sockaddr_in a
;
459 static struct iovec iov
;
460 static struct msghdr m
= {
461 .msg_name
= (struct sockaddr
*) &a
,
462 .msg_namelen
= sizeof(a
),
465 .msg_control
= cmsg_data
,
466 .msg_controllen
= CMSG_LEN(sizeof(struct in_pktinfo
)),
468 struct in_pktinfo
*pkti
;
469 struct cmsghdr
*cmsg
;
471 a
.sin_family
= AF_INET
;
472 a
.sin_port
= htons(APMGR_PORT
);
473 a
.sin_addr
.s_addr
= ~0;
475 memset(cmsg_data
, 0, sizeof(cmsg_data
));
476 cmsg
= CMSG_FIRSTHDR(&m
);
477 cmsg
->cmsg_len
= m
.msg_controllen
;
478 cmsg
->cmsg_level
= IPPROTO_IP
;
479 cmsg
->cmsg_type
= IP_PKTINFO
;
481 pkti
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
482 pkti
->ipi_ifindex
= iface
->ifindex
;
485 iov
.iov_len
= blob_pad_len(data
);
487 if (sendmsg(remote_fd
.fd
, &m
, 0) < 0)
492 static void interface_send_msg_v6(struct interface
*iface
, struct blob_attr
*data
) {
493 static struct sockaddr_in6 groupSock
= {};
495 groupSock
.sin6_family
= AF_INET6
;
496 inet_pton(AF_INET6
, APMGR_V6_MCAST_GROUP
, &groupSock
.sin6_addr
);
497 groupSock
.sin6_port
= htons(APMGR_PORT
);
499 setsockopt(remote_fd
.fd
, IPPROTO_IPV6
, IPV6_MULTICAST_IF
, &iface
->ifindex
, sizeof(iface
->ifindex
));
501 if (sendto(remote_fd
.fd
, data
, blob_pad_len(data
), 0, (const struct sockaddr
*)&groupSock
, sizeof(groupSock
)) < 0)
505 static void interface_send_msg(struct interface
*iface
, struct blob_attr
*data
){
507 interface_send_msg_v6(iface
, data
);
509 interface_send_msg_v4(iface
, data
);
513 static void usteer_send_sta_info(struct sta_info
*sta
)
515 int seen
= current_time
- sta
->seen
;
518 c
= blob_nest_start(&buf
, 0);
519 blob_put(&buf
, APMSG_STA_ADDR
, sta
->sta
->addr
, 6);
520 blob_put_int8(&buf
, APMSG_STA_CONNECTED
, !!sta
->connected
);
521 blob_put_int32(&buf
, APMSG_STA_SIGNAL
, sta
->signal
);
522 blob_put_int32(&buf
, APMSG_STA_SEEN
, seen
);
523 blob_put_int32(&buf
, APMSG_STA_TIMEOUT
, config
.local_sta_timeout
- seen
);
524 blob_nest_end(&buf
, c
);
527 static void usteer_send_node(struct usteer_node
*node
, struct sta_info
*sta
)
531 c
= blob_nest_start(&buf
, 0);
533 blob_put_string(&buf
, APMSG_NODE_NAME
, usteer_node_name(node
));
534 blob_put_string(&buf
, APMSG_NODE_SSID
, node
->ssid
);
535 blob_put_int32(&buf
, APMSG_NODE_FREQ
, node
->freq
);
536 blob_put_int32(&buf
, APMSG_NODE_NOISE
, node
->noise
);
537 blob_put_int32(&buf
, APMSG_NODE_LOAD
, node
->load
);
538 blob_put_int32(&buf
, APMSG_NODE_N_ASSOC
, node
->n_assoc
);
539 blob_put_int32(&buf
, APMSG_NODE_MAX_ASSOC
, node
->max_assoc
);
541 r
= blob_nest_start(&buf
, APMSG_NODE_RRM_NR
);
542 blobmsg_add_field(&buf
, BLOBMSG_TYPE_ARRAY
, "",
543 blobmsg_data(node
->rrm_nr
),
544 blobmsg_data_len(node
->rrm_nr
));
545 blob_nest_end(&buf
, r
);
549 blob_put(&buf
, APMSG_NODE_NODE_INFO
,
550 blob_data(node
->node_info
),
551 blob_len(node
->node_info
));
553 s
= blob_nest_start(&buf
, APMSG_NODE_STATIONS
);
556 usteer_send_sta_info(sta
);
558 list_for_each_entry(sta
, &node
->sta_info
, node_list
)
559 usteer_send_sta_info(sta
);
562 blob_nest_end(&buf
, s
);
564 blob_nest_end(&buf
, c
);
568 usteer_check_timeout(void)
570 struct usteer_remote_node
*node
, *tmp
;
571 int timeout
= config
.remote_node_timeout
;
573 list_for_each_entry_safe(node
, tmp
, &remote_nodes
, list
) {
574 if (node
->check
++ > timeout
)
575 remote_node_free(node
);
580 usteer_update_init(void)
582 blob_buf_init(&buf
, 0);
583 blob_put_int32(&buf
, APMSG_ID
, local_id
);
584 blob_put_int32(&buf
, APMSG_SEQ
, ++msg_seq
);
586 blob_put(&buf
, APMSG_HOST_INFO
,
587 blob_data(host_info_blob
),
588 blob_len(host_info_blob
));
590 return blob_nest_start(&buf
, APMSG_NODES
);
594 usteer_update_send(void *c
)
596 struct interface
*iface
;
598 blob_nest_end(&buf
, c
);
600 vlist_for_each_element(&interfaces
, iface
, node
)
601 interface_send_msg(iface
, buf
.head
);
605 usteer_send_sta_update(struct sta_info
*si
)
607 void *c
= usteer_update_init();
608 usteer_send_node(si
->node
, si
);
609 usteer_update_send(c
);
613 usteer_send_update_timer(struct uloop_timeout
*t
)
615 struct usteer_node
*node
;
618 if (avl_is_empty(&local_nodes
) && !host_info_blob
)
621 usteer_update_time();
622 uloop_timeout_set(t
, config
.remote_update_interval
);
624 c
= usteer_update_init();
625 for_each_local_node(node
)
626 usteer_send_node(node
, NULL
);
628 usteer_update_send(c
);
629 usteer_check_timeout();
633 usteer_init_local_id(void)
637 f
= fopen("/dev/urandom", "r");
639 perror("fopen(/dev/urandom)");
643 if (fread(&local_id
, sizeof(local_id
), 1, f
) < 1)
650 static int usteer_create_v4_socket() {
654 fd
= usock(USOCK_UDP
| USOCK_SERVER
| USOCK_NONBLOCK
|
655 USOCK_NUMERIC
| USOCK_IPV4ONLY
,
656 "0.0.0.0", APMGR_PORT_STR
);
662 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &yes
, sizeof(yes
)) < 0)
663 perror("setsockopt(IP_PKTINFO)");
665 if (setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, &yes
, sizeof(yes
)) < 0)
666 perror("setsockopt(SO_BROADCAST)");
672 static int usteer_create_v6_socket() {
673 struct interface
*iface
;
674 struct ipv6_mreq group
;
678 fd
= usock(USOCK_UDP
| USOCK_SERVER
| USOCK_NONBLOCK
|
679 USOCK_NUMERIC
| USOCK_IPV6ONLY
,
680 "::", APMGR_PORT_STR
);
686 if (!inet_pton(AF_INET6
, APMGR_V6_MCAST_GROUP
, &group
.ipv6mr_multiaddr
.s6_addr
))
687 perror("inet_pton(AF_INET6)");
689 /* Membership has to be added for every interface we listen on. */
690 vlist_for_each_element(&interfaces
, iface
, node
) {
691 group
.ipv6mr_interface
= iface
->ifindex
;
692 if(setsockopt(fd
, IPPROTO_IPV6
, IPV6_ADD_MEMBERSHIP
, (char *)&group
, sizeof group
) < 0)
693 perror("setsockopt(IPV6_ADD_MEMBERSHIP)");
696 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &yes
, sizeof(yes
)) < 0)
697 perror("setsockopt(IPV6_RECVPKTINFO)");
699 if (setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, &yes
, sizeof(yes
)) < 0)
700 perror("setsockopt(SO_BROADCAST)");
705 static void usteer_reload_timer(struct uloop_timeout
*t
) {
706 /* Remove uloop descriptor */
707 if (remote_fd
.fd
&& remote_fd
.registered
) {
708 uloop_fd_delete(&remote_fd
);
713 remote_fd
.fd
= usteer_create_v6_socket();
714 remote_fd
.cb
= interface_recv_v6
;
716 remote_fd
.fd
= usteer_create_v4_socket();
717 remote_fd
.cb
= interface_recv_v4
;
720 if (remote_fd
.fd
< 0)
723 uloop_fd_add(&remote_fd
, ULOOP_READ
);
726 int usteer_interface_init(void)
728 if (usteer_init_local_id())
731 remote_timer
.cb
= usteer_send_update_timer
;
732 remote_timer
.cb(&remote_timer
);
734 reload_timer
.cb
= usteer_reload_timer
;
735 reload_timer
.cb(&reload_timer
);