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
);
202 static struct usteer_remote_host
*
203 interface_get_host(const char *addr
, unsigned long id
)
205 struct usteer_remote_host
*host
;
207 host
= avl_find_element(&remote_hosts
, (void *)id
, host
, avl
);
211 host
= calloc(1, sizeof(*host
));
212 host
->avl
.key
= (void *)id
;
213 INIT_LIST_HEAD(&host
->nodes
);
214 avl_insert(&remote_hosts
, &host
->avl
);
217 if (host
->addr
&& !strcmp(host
->addr
, addr
))
221 host
->addr
= strdup(addr
);
226 static struct usteer_remote_node
*
227 interface_get_node(struct usteer_remote_host
*host
, const char *name
)
229 struct usteer_remote_node
*node
;
230 int addr_len
= strlen(host
->addr
);
233 list_for_each_entry(node
, &host
->nodes
, host_list
)
234 if (!strcmp(node
->name
, name
))
237 node
= calloc_a(sizeof(*node
), &buf
, addr_len
+ 1 + strlen(name
) + 1);
238 node
->node
.type
= NODE_TYPE_REMOTE
;
240 sprintf(buf
, "%s#%s", host
->addr
, name
);
241 node
->node
.avl
.key
= buf
;
242 node
->name
= buf
+ addr_len
+ 1;
244 INIT_LIST_HEAD(&node
->node
.sta_info
);
246 list_add_tail(&node
->list
, &remote_nodes
);
247 list_add_tail(&node
->host_list
, &host
->nodes
);
253 interface_add_node(struct usteer_remote_host
*host
, struct blob_attr
*data
)
255 struct usteer_remote_node
*node
;
256 struct apmsg_node msg
;
257 struct blob_attr
*cur
;
260 if (!parse_apmsg_node(&msg
, data
)) {
261 MSG(DEBUG
, "Cannot parse node in message\n");
265 node
= interface_get_node(host
, msg
.name
);
267 node
->node
.freq
= msg
.freq
;
268 node
->node
.n_assoc
= msg
.n_assoc
;
269 node
->node
.max_assoc
= msg
.max_assoc
;
270 node
->node
.noise
= msg
.noise
;
271 node
->node
.load
= msg
.load
;
273 memcpy(node
->node
.bssid
, msg
.bssid
, sizeof(node
->node
.bssid
));
275 snprintf(node
->node
.ssid
, sizeof(node
->node
.ssid
), "%s", msg
.ssid
);
276 usteer_node_set_blob(&node
->node
.rrm_nr
, msg
.rrm_nr
);
277 usteer_node_set_blob(&node
->node
.node_info
, msg
.node_info
);
279 blob_for_each_attr(cur
, msg
.stations
, rem
)
280 interface_add_station(node
, cur
);
284 interface_recv_msg(struct interface
*iface
, char *addr_str
, void *buf
, int len
)
286 struct usteer_remote_host
*host
;
287 struct blob_attr
*data
= buf
;
289 struct blob_attr
*cur
;
292 if (blob_pad_len(data
) != len
) {
293 MSG(DEBUG
, "Invalid message length (header: %d, real: %d)\n", blob_pad_len(data
), len
);
297 if (!parse_apmsg(&msg
, data
)) {
298 MSG(DEBUG
, "Missing fields in message\n");
302 if (msg
.id
== local_id
)
305 MSG(NETWORK
, "Received message on %s (id=%08x->%08x seq=%d len=%d)\n",
306 interface_name(iface
), msg
.id
, local_id
, msg
.seq
, len
);
308 host
= interface_get_host(addr_str
, msg
.id
);
309 usteer_node_set_blob(&host
->host_info
, msg
.host_info
);
311 blob_for_each_attr(cur
, msg
.nodes
, rem
)
312 interface_add_node(host
, cur
);
315 static struct interface
*
316 interface_find_by_ifindex(int index
)
318 struct interface
*iface
;
320 vlist_for_each_element(&interfaces
, iface
, node
) {
321 if (iface
->ifindex
== index
)
329 interface_recv_v4(struct uloop_fd
*u
, unsigned int events
)
331 static char buf
[APMGR_BUFLEN
];
332 static char cmsg_buf
[( CMSG_SPACE(sizeof(struct in_pktinfo
)) + sizeof(int)) + 1];
333 static struct sockaddr_in sin
;
334 char addr_str
[INET_ADDRSTRLEN
];
335 static struct iovec iov
= {
337 .iov_len
= sizeof(buf
)
339 static struct msghdr msg
= {
341 .msg_namelen
= sizeof(sin
),
344 .msg_control
= cmsg_buf
,
345 .msg_controllen
= sizeof(cmsg_buf
),
347 struct cmsghdr
*cmsg
;
351 struct in_pktinfo
*pkti
= NULL
;
352 struct interface
*iface
;
354 len
= recvmsg(u
->fd
, &msg
, 0);
368 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
369 if (cmsg
->cmsg_type
!= IP_PKTINFO
)
372 pkti
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
376 MSG(DEBUG
, "Received packet without ifindex\n");
380 iface
= interface_find_by_ifindex(pkti
->ipi_ifindex
);
382 MSG(DEBUG
, "Received packet from unconfigured interface %d\n", pkti
->ipi_ifindex
);
386 inet_ntop(AF_INET
, &sin
.sin_addr
, addr_str
, sizeof(addr_str
));
388 interface_recv_msg(iface
, addr_str
, buf
, len
);
393 static void interface_recv_v6(struct uloop_fd
*u
, unsigned int events
){
394 static char buf
[APMGR_BUFLEN
];
395 static char cmsg_buf
[( CMSG_SPACE(sizeof(struct in6_pktinfo
)) + sizeof(int)) + 1];
396 static struct sockaddr_in6 sin
;
397 static struct iovec iov
= {
399 .iov_len
= sizeof(buf
)
401 static struct msghdr msg
= {
403 .msg_namelen
= sizeof(sin
),
406 .msg_control
= cmsg_buf
,
407 .msg_controllen
= sizeof(cmsg_buf
),
409 struct cmsghdr
*cmsg
;
410 char addr_str
[INET6_ADDRSTRLEN
];
414 struct in6_pktinfo
*pkti
= NULL
;
415 struct interface
*iface
;
417 len
= recvmsg(u
->fd
, &msg
, 0);
431 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
432 if (cmsg
->cmsg_type
!= IPV6_PKTINFO
)
435 pkti
= (struct in6_pktinfo
*) CMSG_DATA(cmsg
);
439 MSG(DEBUG
, "Received packet without ifindex\n");
443 iface
= interface_find_by_ifindex(pkti
->ipi6_ifindex
);
445 MSG(DEBUG
, "Received packet from unconfigured interface %d\n", pkti
->ipi6_ifindex
);
449 inet_ntop(AF_INET6
, &sin
.sin6_addr
, addr_str
, sizeof(addr_str
));
450 if (sin
.sin6_addr
.s6_addr
[0] == 0) {
451 /* IPv4 mapped address. Ignore. */
455 interface_recv_msg(iface
, addr_str
, buf
, len
);
459 static void interface_send_msg_v4(struct interface
*iface
, struct blob_attr
*data
)
461 static size_t cmsg_data
[( CMSG_SPACE(sizeof(struct in_pktinfo
)) / sizeof(size_t)) + 1];
462 static struct sockaddr_in a
;
463 static struct iovec iov
;
464 static struct msghdr m
= {
465 .msg_name
= (struct sockaddr
*) &a
,
466 .msg_namelen
= sizeof(a
),
469 .msg_control
= cmsg_data
,
470 .msg_controllen
= CMSG_LEN(sizeof(struct in_pktinfo
)),
472 struct in_pktinfo
*pkti
;
473 struct cmsghdr
*cmsg
;
475 a
.sin_family
= AF_INET
;
476 a
.sin_port
= htons(APMGR_PORT
);
477 a
.sin_addr
.s_addr
= ~0;
479 memset(cmsg_data
, 0, sizeof(cmsg_data
));
480 cmsg
= CMSG_FIRSTHDR(&m
);
481 cmsg
->cmsg_len
= m
.msg_controllen
;
482 cmsg
->cmsg_level
= IPPROTO_IP
;
483 cmsg
->cmsg_type
= IP_PKTINFO
;
485 pkti
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
486 pkti
->ipi_ifindex
= iface
->ifindex
;
489 iov
.iov_len
= blob_pad_len(data
);
491 if (sendmsg(remote_fd
.fd
, &m
, 0) < 0)
496 static void interface_send_msg_v6(struct interface
*iface
, struct blob_attr
*data
) {
497 static struct sockaddr_in6 groupSock
= {};
499 groupSock
.sin6_family
= AF_INET6
;
500 inet_pton(AF_INET6
, APMGR_V6_MCAST_GROUP
, &groupSock
.sin6_addr
);
501 groupSock
.sin6_port
= htons(APMGR_PORT
);
503 setsockopt(remote_fd
.fd
, IPPROTO_IPV6
, IPV6_MULTICAST_IF
, &iface
->ifindex
, sizeof(iface
->ifindex
));
505 if (sendto(remote_fd
.fd
, data
, blob_pad_len(data
), 0, (const struct sockaddr
*)&groupSock
, sizeof(groupSock
)) < 0)
509 static void interface_send_msg(struct interface
*iface
, struct blob_attr
*data
){
511 interface_send_msg_v6(iface
, data
);
513 interface_send_msg_v4(iface
, data
);
517 static void usteer_send_sta_info(struct sta_info
*sta
)
519 int seen
= current_time
- sta
->seen
;
522 c
= blob_nest_start(&buf
, 0);
523 blob_put(&buf
, APMSG_STA_ADDR
, sta
->sta
->addr
, 6);
524 blob_put_int8(&buf
, APMSG_STA_CONNECTED
, !!sta
->connected
);
525 blob_put_int32(&buf
, APMSG_STA_SIGNAL
, sta
->signal
);
526 blob_put_int32(&buf
, APMSG_STA_SEEN
, seen
);
527 blob_put_int32(&buf
, APMSG_STA_TIMEOUT
, config
.local_sta_timeout
- seen
);
528 blob_nest_end(&buf
, c
);
531 static void usteer_send_node(struct usteer_node
*node
, struct sta_info
*sta
)
535 c
= blob_nest_start(&buf
, 0);
537 blob_put_string(&buf
, APMSG_NODE_NAME
, usteer_node_name(node
));
538 blob_put_string(&buf
, APMSG_NODE_SSID
, node
->ssid
);
539 blob_put_int32(&buf
, APMSG_NODE_FREQ
, node
->freq
);
540 blob_put_int32(&buf
, APMSG_NODE_NOISE
, node
->noise
);
541 blob_put_int32(&buf
, APMSG_NODE_LOAD
, node
->load
);
542 blob_put_int32(&buf
, APMSG_NODE_N_ASSOC
, node
->n_assoc
);
543 blob_put_int32(&buf
, APMSG_NODE_MAX_ASSOC
, node
->max_assoc
);
544 blob_put(&buf
, APMSG_NODE_BSSID
, node
->bssid
, sizeof(node
->bssid
));
546 r
= blob_nest_start(&buf
, APMSG_NODE_RRM_NR
);
547 blobmsg_add_field(&buf
, BLOBMSG_TYPE_ARRAY
, "",
548 blobmsg_data(node
->rrm_nr
),
549 blobmsg_data_len(node
->rrm_nr
));
550 blob_nest_end(&buf
, r
);
554 blob_put(&buf
, APMSG_NODE_NODE_INFO
,
555 blob_data(node
->node_info
),
556 blob_len(node
->node_info
));
558 s
= blob_nest_start(&buf
, APMSG_NODE_STATIONS
);
561 usteer_send_sta_info(sta
);
563 list_for_each_entry(sta
, &node
->sta_info
, node_list
)
564 usteer_send_sta_info(sta
);
567 blob_nest_end(&buf
, s
);
569 blob_nest_end(&buf
, c
);
573 usteer_check_timeout(void)
575 struct usteer_remote_node
*node
, *tmp
;
576 int timeout
= config
.remote_node_timeout
;
578 list_for_each_entry_safe(node
, tmp
, &remote_nodes
, list
) {
579 if (node
->check
++ > timeout
)
580 remote_node_free(node
);
585 usteer_update_init(void)
587 blob_buf_init(&buf
, 0);
588 blob_put_int32(&buf
, APMSG_ID
, local_id
);
589 blob_put_int32(&buf
, APMSG_SEQ
, ++msg_seq
);
591 blob_put(&buf
, APMSG_HOST_INFO
,
592 blob_data(host_info_blob
),
593 blob_len(host_info_blob
));
595 return blob_nest_start(&buf
, APMSG_NODES
);
599 usteer_update_send(void *c
)
601 struct interface
*iface
;
603 blob_nest_end(&buf
, c
);
605 vlist_for_each_element(&interfaces
, iface
, node
)
606 interface_send_msg(iface
, buf
.head
);
610 usteer_send_sta_update(struct sta_info
*si
)
612 void *c
= usteer_update_init();
613 usteer_send_node(si
->node
, si
);
614 usteer_update_send(c
);
618 usteer_send_update_timer(struct uloop_timeout
*t
)
620 struct usteer_node
*node
;
623 if (avl_is_empty(&local_nodes
) && !host_info_blob
)
626 usteer_update_time();
627 uloop_timeout_set(t
, config
.remote_update_interval
);
629 c
= usteer_update_init();
630 for_each_local_node(node
)
631 usteer_send_node(node
, NULL
);
633 usteer_update_send(c
);
634 usteer_check_timeout();
638 usteer_init_local_id(void)
642 f
= fopen("/dev/urandom", "r");
644 perror("fopen(/dev/urandom)");
648 if (fread(&local_id
, sizeof(local_id
), 1, f
) < 1)
655 static int usteer_create_v4_socket() {
659 fd
= usock(USOCK_UDP
| USOCK_SERVER
| USOCK_NONBLOCK
|
660 USOCK_NUMERIC
| USOCK_IPV4ONLY
,
661 "0.0.0.0", APMGR_PORT_STR
);
667 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &yes
, sizeof(yes
)) < 0)
668 perror("setsockopt(IP_PKTINFO)");
670 if (setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, &yes
, sizeof(yes
)) < 0)
671 perror("setsockopt(SO_BROADCAST)");
677 static int usteer_create_v6_socket() {
678 struct interface
*iface
;
679 struct ipv6_mreq group
;
683 fd
= usock(USOCK_UDP
| USOCK_SERVER
| USOCK_NONBLOCK
|
684 USOCK_NUMERIC
| USOCK_IPV6ONLY
,
685 "::", APMGR_PORT_STR
);
691 if (!inet_pton(AF_INET6
, APMGR_V6_MCAST_GROUP
, &group
.ipv6mr_multiaddr
.s6_addr
))
692 perror("inet_pton(AF_INET6)");
694 /* Membership has to be added for every interface we listen on. */
695 vlist_for_each_element(&interfaces
, iface
, node
) {
696 group
.ipv6mr_interface
= iface
->ifindex
;
697 if(setsockopt(fd
, IPPROTO_IPV6
, IPV6_ADD_MEMBERSHIP
, (char *)&group
, sizeof group
) < 0)
698 perror("setsockopt(IPV6_ADD_MEMBERSHIP)");
701 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &yes
, sizeof(yes
)) < 0)
702 perror("setsockopt(IPV6_RECVPKTINFO)");
704 if (setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, &yes
, sizeof(yes
)) < 0)
705 perror("setsockopt(SO_BROADCAST)");
710 static void usteer_reload_timer(struct uloop_timeout
*t
) {
711 /* Remove uloop descriptor */
712 if (remote_fd
.fd
&& remote_fd
.registered
) {
713 uloop_fd_delete(&remote_fd
);
718 remote_fd
.fd
= usteer_create_v6_socket();
719 remote_fd
.cb
= interface_recv_v6
;
721 remote_fd
.fd
= usteer_create_v4_socket();
722 remote_fd
.cb
= interface_recv_v4
;
725 if (remote_fd
.fd
< 0)
728 uloop_fd_add(&remote_fd
, ULOOP_READ
);
731 int usteer_interface_init(void)
733 if (usteer_init_local_id())
736 remote_timer
.cb
= usteer_send_update_timer
;
737 remote_timer
.cb(&remote_timer
);
739 reload_timer
.cb
= usteer_reload_timer
;
740 reload_timer
.cb(&reload_timer
);