-
Notifications
You must be signed in to change notification settings - Fork 7
/
monitor.c
212 lines (173 loc) · 4.95 KB
/
monitor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (C) 2020 embedd.ch
* Copyright (C) 2020 Felix Fietkau <[email protected]>
* Copyright (C) 2020 John Crispin <[email protected]>
*/
#include <netinet/udp.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <pcap/pcap.h>
#include <libubox/blobmsg_json.h>
#include "usteer.h"
#include "remote.h"
static pcap_t *pcap;
static int pkt_offset;
/* IP header */
struct ip_header {
uint8_t ip_vhl; /* version << 4 | header length >> 2 */
uint8_t ip_tos; /* type of service */
uint16_t ip_len; /* total length */
uint16_t ip_id; /* identification */
uint16_t ip_off; /* fragment offset field */
#define IP_RF 0x8000 /* reserved fragment flag */
#define IP_DF 0x4000 /* dont fragment flag */
#define IP_MF 0x2000 /* more fragments flag */
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
uint8_t ip_ttl; /* time to live */
uint8_t ip_p; /* protocol */
uint16_t ip_sum; /* checksum */
struct in_addr ip_src, ip_dst; /* source and dest address */
};
#define IP_HL(ip) (((ip)->ip_vhl) & 0x0f)
#define IP_V(ip) (((ip)->ip_vhl) >> 4)
struct udp_header {
uint16_t uh_sport; /* source port */
uint16_t uh_dport; /* destination port */
uint16_t uh_ulen; /* udp length */
uint16_t uh_sum; /* udp checksum */
};
static void
decode_sta(struct blob_attr *data)
{
struct apmsg_sta msg;
if (!parse_apmsg_sta(&msg, data))
return;
fprintf(stderr, "\t\tSta "MAC_ADDR_FMT" signal=%d connected=%d timeout=%d\n",
MAC_ADDR_DATA(msg.addr), msg.signal, msg.connected, msg.timeout);
}
static void
decode_node(struct blob_attr *data)
{
struct apmsg_node msg;
struct blob_attr *cur;
int rem;
if (!parse_apmsg_node(&msg, data))
return;
fprintf(stderr, "\tNode %s, freq=%d, n_assoc=%d, noise=%d load=%d max_assoc=%d\n",
msg.name, msg.freq, msg.n_assoc, msg.noise, msg.load, msg.max_assoc);
if (msg.rrm_nr) {
fprintf(stderr, "\t\tRRM:");
blobmsg_for_each_attr(cur, msg.rrm_nr, rem) {
if (!blobmsg_check_attr(cur, false))
continue;
if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
continue;
fprintf(stderr, " %s", blobmsg_get_string(cur));
}
fprintf(stderr, "\n");
}
if (msg.node_info) {
char *data = blobmsg_format_json(msg.node_info, true);
fprintf(stderr, "\t\tNode info: %s\n", data);
free(data);
}
blob_for_each_attr(cur, msg.stations, rem)
decode_sta(cur);
}
static void
decode_packet(struct blob_attr *data)
{
struct apmsg msg;
struct blob_attr *cur;
int rem;
if (!parse_apmsg(&msg, data)) {
fprintf(stderr, "missing fields\n");
return;
}
fprintf(stderr, "id=%08x, seq=%d\n", msg.id, msg.seq);
if (msg.host_info) {
char *data = blobmsg_format_json(msg.host_info, true);
fprintf(stderr, "\tHost info: %s\n", data);
free(data);
}
blob_for_each_attr(cur, msg.nodes, rem)
decode_node(cur);
}
static void
recv_packet(unsigned char *user, const struct pcap_pkthdr *hdr,
const unsigned char *packet)
{
char addr[INET_ADDRSTRLEN];
struct ip_header *ip;
struct udp_header *uh;
struct blob_attr *data;
int len = hdr->caplen;
int hdrlen;
len -= pkt_offset;
packet += pkt_offset;
ip = (void *) packet;
hdrlen = IP_HL(ip) * 4;
if (hdrlen < 20 || hdrlen >= len)
return;
len -= hdrlen;
packet += hdrlen;
inet_ntop(AF_INET, &ip->ip_src, addr, sizeof(addr));
hdrlen = sizeof(*uh);
if (len <= hdrlen)
return;
uh = (void *) packet;
packet += hdrlen;
len -= hdrlen;
if (uh->uh_dport != htons(APMGR_PORT))
return;
data = (void *) packet;
fprintf(stderr, "[%s]: len=%d ", addr, len);
if (len != blob_pad_len(data)) {
fprintf(stderr, "invalid data\n");
return;
}
decode_packet(data);
}
int main(int argc, char **argv)
{
static char errbuf[PCAP_ERRBUF_SIZE];
struct bpf_program fp;
if (argc != 2) {
fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
return 1;
}
pcap = pcap_open_live(argv[1], APMGR_BUFLEN, 1, 1000, errbuf);
if (!pcap) {
fprintf(stderr, "Failed to open interface %s: %s\n", argv[1], errbuf);
return 1;
}
pcap_compile(pcap, &fp, "port "APMGR_PORT_STR, 1, PCAP_NETMASK_UNKNOWN);
pcap_setfilter(pcap, &fp);
switch (pcap_datalink(pcap)) {
case DLT_EN10MB:
pkt_offset = 14;
break;
case DLT_RAW:
pkt_offset = 0;
break;
default:
fprintf(stderr, "Invalid link type\n");
return -1;
}
pcap_loop(pcap, 0, recv_packet, NULL);
pcap_close(pcap);
return 0;
}