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_node_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 AVL_TREE(remote_nodes
, remote_node_cmp
, true, NULL
);
65 interface_name(struct interface
*iface
)
67 return iface
->node
.avl
.key
;
71 interface_check(struct interface
*iface
)
73 iface
->ifindex
= if_nametoindex(interface_name(iface
));
74 uloop_timeout_set(&reload_timer
, 1);
78 interface_init(struct interface
*iface
)
80 interface_check(iface
);
84 interface_free(struct interface
*iface
)
86 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
91 interfaces_update_cb(struct vlist_tree
*tree
,
92 struct vlist_node
*node_new
,
93 struct vlist_node
*node_old
)
95 struct interface
*iface
;
97 if (node_new
&& node_old
) {
98 iface
= container_of(node_new
, struct interface
, node
);
100 iface
= container_of(node_old
, struct interface
, node
);
101 interface_check(iface
);
102 } else if (node_old
) {
103 iface
= container_of(node_old
, struct interface
, node
);
104 interface_free(iface
);
106 iface
= container_of(node_new
, struct interface
, node
);
107 interface_init(iface
);
111 void usteer_interface_add(const char *name
)
113 struct interface
*iface
;
116 iface
= calloc_a(sizeof(*iface
), &name_buf
, strlen(name
) + 1);
117 strcpy(name_buf
, name
);
118 vlist_add(&interfaces
, &iface
->node
, name_buf
);
121 void config_set_interfaces(struct blob_attr
*data
)
123 struct blob_attr
*cur
;
129 if (!blobmsg_check_attr_list(data
, BLOBMSG_TYPE_STRING
))
132 vlist_update(&interfaces
);
133 blobmsg_for_each_attr(cur
, data
, rem
) {
134 usteer_interface_add(blobmsg_data(cur
));
136 vlist_flush(&interfaces
);
139 void config_get_interfaces(struct blob_buf
*buf
)
141 struct interface
*iface
;
144 c
= blobmsg_open_array(buf
, "interfaces");
145 vlist_for_each_element(&interfaces
, iface
, node
) {
146 blobmsg_add_string(buf
, NULL
, interface_name(iface
));
148 blobmsg_close_array(buf
, c
);
152 interface_add_station(struct usteer_remote_node
*node
, struct blob_attr
*data
)
156 struct apmsg_sta msg
;
159 if (!parse_apmsg_sta(&msg
, data
)) {
160 MSG(DEBUG
, "Cannot parse station in message\n");
164 if (msg
.timeout
<= 0) {
165 MSG(DEBUG
, "Refuse to add an already expired station entry\n");
169 sta
= usteer_sta_get(msg
.addr
, true);
173 si
= usteer_sta_info_get(sta
, &node
->node
, &create
);
177 si
->connected
= msg
.connected
;
178 si
->signal
= msg
.signal
;
179 si
->seen
= current_time
- msg
.seen
;
180 usteer_sta_info_update_timeout(si
, msg
.timeout
);
184 remote_node_free(struct usteer_remote_node
*node
)
186 avl_delete(&remote_nodes
, &node
->avl
);
187 usteer_sta_node_cleanup(&node
->node
);
191 static struct usteer_remote_node
*
192 interface_get_node(const char *addr
, unsigned long id
, const char *name
)
194 struct usteer_remote_node
*node
;
195 int addr_len
= strlen(addr
);
198 node
= avl_find_element(&remote_nodes
, (void *) id
, node
, avl
);
199 while (node
&& node
->avl
.key
== (void *) id
) {
200 if (!strcmp(node
->name
, name
))
203 node
= avl_next_element(node
, avl
);
206 node
= calloc_a(sizeof(*node
), &buf
, addr_len
+ 1 + strlen(name
) + 1);
207 node
->avl
.key
= (void *) id
;
208 node
->node
.type
= NODE_TYPE_REMOTE
;
210 sprintf(buf
, "%s#%s", addr
, name
);
211 node
->node
.avl
.key
= buf
;
212 node
->name
= buf
+ addr_len
+ 1;
213 INIT_LIST_HEAD(&node
->node
.sta_info
);
215 avl_insert(&remote_nodes
, &node
->avl
);
221 interface_add_node(struct interface
*iface
, const char *addr
, unsigned long id
, struct blob_attr
*data
)
223 struct usteer_remote_node
*node
;
224 struct apmsg_node msg
;
225 struct blob_attr
*cur
;
228 if (!parse_apmsg_node(&msg
, data
)) {
229 MSG(DEBUG
, "Cannot parse node in message\n");
233 node
= interface_get_node(addr
, id
, msg
.name
);
235 node
->node
.freq
= msg
.freq
;
236 node
->node
.n_assoc
= msg
.n_assoc
;
237 node
->node
.max_assoc
= msg
.max_assoc
;
238 node
->node
.noise
= msg
.noise
;
239 node
->node
.load
= msg
.load
;
241 snprintf(node
->node
.ssid
, sizeof(node
->node
.ssid
), "%s", msg
.ssid
);
242 usteer_node_set_blob(&node
->node
.rrm_nr
, msg
.rrm_nr
);
243 usteer_node_set_blob(&node
->node
.script_data
, msg
.script_data
);
245 blob_for_each_attr(cur
, msg
.stations
, rem
)
246 interface_add_station(node
, cur
);
250 interface_recv_msg(struct interface
*iface
, struct in_addr
*addr
, void *buf
, int len
)
252 char addr_str
[INET_ADDRSTRLEN
];
253 struct blob_attr
*data
= buf
;
255 struct blob_attr
*cur
;
258 if (blob_pad_len(data
) != len
) {
259 MSG(DEBUG
, "Invalid message length (header: %d, real: %d)\n", blob_pad_len(data
), len
);
263 if (!parse_apmsg(&msg
, data
)) {
264 MSG(DEBUG
, "Missing fields in message\n");
268 if (msg
.id
== local_id
)
271 MSG(NETWORK
, "Received message on %s (id=%08x->%08x seq=%d len=%d)\n",
272 interface_name(iface
), msg
.id
, local_id
, msg
.seq
, len
);
274 inet_ntop(AF_INET
, addr
, addr_str
, sizeof(addr_str
));
276 blob_for_each_attr(cur
, msg
.nodes
, rem
)
277 interface_add_node(iface
, addr_str
, msg
.id
, cur
);
280 static struct interface
*
281 interface_find_by_ifindex(int index
)
283 struct interface
*iface
;
285 vlist_for_each_element(&interfaces
, iface
, node
) {
286 if (iface
->ifindex
== index
)
294 interface_recv(struct uloop_fd
*u
, unsigned int events
)
296 static char buf
[APMGR_BUFLEN
];
297 static char cmsg_buf
[( CMSG_SPACE(sizeof(struct in_pktinfo
)) + sizeof(int)) + 1];
298 static struct sockaddr_in sin
;
299 static struct iovec iov
= {
301 .iov_len
= sizeof(buf
)
303 static struct msghdr msg
= {
305 .msg_namelen
= sizeof(sin
),
308 .msg_control
= cmsg_buf
,
309 .msg_controllen
= sizeof(cmsg_buf
),
311 struct cmsghdr
*cmsg
;
315 struct in_pktinfo
*pkti
= NULL
;
316 struct interface
*iface
;
318 len
= recvmsg(u
->fd
, &msg
, 0);
332 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
333 if (cmsg
->cmsg_type
!= IP_PKTINFO
)
336 pkti
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
340 MSG(DEBUG
, "Received packet without ifindex\n");
344 iface
= interface_find_by_ifindex(pkti
->ipi_ifindex
);
346 MSG(DEBUG
, "Received packet from unconfigured interface %d\n", pkti
->ipi_ifindex
);
350 interface_recv_msg(iface
, &sin
.sin_addr
, buf
, len
);
354 static void interface_send_msg(struct interface
*iface
, struct blob_attr
*data
)
356 static size_t cmsg_data
[( CMSG_SPACE(sizeof(struct in_pktinfo
)) / sizeof(size_t)) + 1];
357 static struct sockaddr_in a
;
358 static struct iovec iov
;
359 static struct msghdr m
= {
360 .msg_name
= (struct sockaddr
*) &a
,
361 .msg_namelen
= sizeof(a
),
364 .msg_control
= cmsg_data
,
365 .msg_controllen
= CMSG_LEN(sizeof(struct in_pktinfo
)),
367 struct in_pktinfo
*pkti
;
368 struct cmsghdr
*cmsg
;
370 a
.sin_family
= AF_INET
;
371 a
.sin_port
= htons(16720);
372 a
.sin_addr
.s_addr
= ~0;
374 memset(cmsg_data
, 0, sizeof(cmsg_data
));
375 cmsg
= CMSG_FIRSTHDR(&m
);
376 cmsg
->cmsg_len
= m
.msg_controllen
;
377 cmsg
->cmsg_level
= IPPROTO_IP
;
378 cmsg
->cmsg_type
= IP_PKTINFO
;
380 pkti
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
381 pkti
->ipi_ifindex
= iface
->ifindex
;
384 iov
.iov_len
= blob_pad_len(data
);
386 if (sendmsg(remote_fd
.fd
, &m
, 0) < 0)
390 static void usteer_send_sta_info(struct sta_info
*sta
)
392 int seen
= current_time
- sta
->seen
;
395 c
= blob_nest_start(&buf
, 0);
396 blob_put(&buf
, APMSG_STA_ADDR
, sta
->sta
->addr
, 6);
397 blob_put_int8(&buf
, APMSG_STA_CONNECTED
, !!sta
->connected
);
398 blob_put_int32(&buf
, APMSG_STA_SIGNAL
, sta
->signal
);
399 blob_put_int32(&buf
, APMSG_STA_SEEN
, seen
);
400 blob_put_int32(&buf
, APMSG_STA_TIMEOUT
, config
.local_sta_timeout
- seen
);
401 blob_nest_end(&buf
, c
);
404 static void usteer_send_node(struct usteer_node
*node
, struct sta_info
*sta
)
408 c
= blob_nest_start(&buf
, 0);
410 blob_put_string(&buf
, APMSG_NODE_NAME
, usteer_node_name(node
));
411 blob_put_string(&buf
, APMSG_NODE_SSID
, node
->ssid
);
412 blob_put_int32(&buf
, APMSG_NODE_FREQ
, node
->freq
);
413 blob_put_int32(&buf
, APMSG_NODE_NOISE
, node
->noise
);
414 blob_put_int32(&buf
, APMSG_NODE_LOAD
, node
->load
);
415 blob_put_int32(&buf
, APMSG_NODE_N_ASSOC
, node
->n_assoc
);
416 blob_put_int32(&buf
, APMSG_NODE_MAX_ASSOC
, node
->max_assoc
);
418 r
= blob_nest_start(&buf
, APMSG_NODE_RRM_NR
);
419 blobmsg_add_field(&buf
, BLOBMSG_TYPE_ARRAY
, "",
420 blobmsg_data(node
->rrm_nr
),
421 blobmsg_data_len(node
->rrm_nr
));
422 blob_nest_end(&buf
, r
);
425 if (node
->script_data
)
426 blob_put(&buf
, APMSG_NODE_SCRIPT_DATA
,
427 blob_data(node
->script_data
),
428 blob_len(node
->script_data
));
430 s
= blob_nest_start(&buf
, APMSG_NODE_STATIONS
);
433 usteer_send_sta_info(sta
);
435 list_for_each_entry(sta
, &node
->sta_info
, node_list
)
436 usteer_send_sta_info(sta
);
439 blob_nest_end(&buf
, s
);
441 blob_nest_end(&buf
, c
);
445 usteer_check_timeout(void)
447 struct usteer_remote_node
*node
, *tmp
;
448 int timeout
= config
.remote_node_timeout
/ config
.remote_update_interval
;
450 avl_for_each_element_safe(&remote_nodes
, node
, avl
, tmp
) {
451 if (node
->check
++ > timeout
)
452 remote_node_free(node
);
457 usteer_update_init(void)
459 blob_buf_init(&buf
, 0);
460 blob_put_int32(&buf
, APMSG_ID
, local_id
);
461 blob_put_int32(&buf
, APMSG_SEQ
, ++msg_seq
);
463 return blob_nest_start(&buf
, APMSG_NODES
);
467 usteer_update_send(void *c
)
469 struct interface
*iface
;
471 blob_nest_end(&buf
, c
);
473 vlist_for_each_element(&interfaces
, iface
, node
)
474 interface_send_msg(iface
, buf
.head
);
478 usteer_send_sta_update(struct sta_info
*si
)
480 void *c
= usteer_update_init();
481 usteer_send_node(si
->node
, si
);
482 usteer_update_send(c
);
486 usteer_send_update_timer(struct uloop_timeout
*t
)
488 struct usteer_node
*node
;
491 usteer_update_time();
492 uloop_timeout_set(t
, config
.remote_update_interval
);
494 c
= usteer_update_init();
495 for_each_local_node(node
)
496 usteer_send_node(node
, NULL
);
498 usteer_update_send(c
);
499 usteer_check_timeout();
503 usteer_init_local_id(void)
507 f
= fopen("/dev/urandom", "r");
509 perror("fopen(/dev/urandom)");
513 if (fread(&local_id
, sizeof(local_id
), 1, f
) < 1)
521 usteer_reload_timer(struct uloop_timeout
*t
)
526 if (remote_fd
.registered
) {
527 uloop_fd_delete(&remote_fd
);
531 fd
= usock(USOCK_UDP
| USOCK_SERVER
| USOCK_NONBLOCK
|
532 USOCK_NUMERIC
| USOCK_IPV4ONLY
,
533 "0.0.0.0", APMGR_PORT_STR
);
539 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &yes
, sizeof(yes
)) < 0)
540 perror("setsockopt(IP_PKTINFO)");
542 if (setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, &yes
, sizeof(yes
)) < 0)
543 perror("setsockopt(SO_BROADCAST)");
546 remote_fd
.cb
= interface_recv
;
547 uloop_fd_add(&remote_fd
, ULOOP_READ
);
550 int usteer_interface_init(void)
552 if (usteer_init_local_id())
555 remote_timer
.cb
= usteer_send_update_timer
;
556 remote_timer
.cb(&remote_timer
);
558 reload_timer
.cb
= usteer_reload_timer
;
559 reload_timer
.cb(&reload_timer
);