-
Notifications
You must be signed in to change notification settings - Fork 2
/
batadv_query.c
297 lines (257 loc) · 6.34 KB
/
batadv_query.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Copyright (C) 2012-2015 B.A.T.M.A.N. contributors:
*
* Simon Wunderlich
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*
*/
#include "batadv_query.h"
#include <errno.h>
#include <net/ethernet.h>
#include <netinet/ether.h>
#include <netinet/in.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "debugfs.h"
#define DEBUG_BATIF_PATH_FMT "%s/batman_adv/%s"
#define DEBUG_TRANSTABLE_GLOBAL "transtable_global"
#define DEBUG_ORIGINATORS "originators"
int mac_to_ipv6(const struct ether_addr *mac, struct in6_addr *addr)
{
memset(addr, 0, sizeof(*addr));
addr->s6_addr[0] = 0xfe;
addr->s6_addr[1] = 0x80;
addr->s6_addr[8] = mac->ether_addr_octet[0] ^ 0x02;
addr->s6_addr[9] = mac->ether_addr_octet[1];
addr->s6_addr[10] = mac->ether_addr_octet[2];
addr->s6_addr[11] = 0xff;
addr->s6_addr[12] = 0xfe;
addr->s6_addr[13] = mac->ether_addr_octet[3];
addr->s6_addr[14] = mac->ether_addr_octet[4];
addr->s6_addr[15] = mac->ether_addr_octet[5];
return 0;
}
int is_ipv6_eui64(const struct in6_addr *addr)
{
size_t i;
for (i = 2; i < 8; i++) {
if (addr->s6_addr[i] != 0x0)
return 0;
}
if (addr->s6_addr[0] != 0xfe ||
addr->s6_addr[1] != 0x80 ||
addr->s6_addr[11] != 0xff ||
addr->s6_addr[12] != 0xfe)
return 0;
return 1;
}
int ipv6_to_mac(const struct in6_addr *addr, struct ether_addr *mac)
{
if (!is_ipv6_eui64(addr))
return -EINVAL;
mac->ether_addr_octet[0] = addr->s6_addr[8] ^ 0x02;
mac->ether_addr_octet[1] = addr->s6_addr[9];
mac->ether_addr_octet[2] = addr->s6_addr[10];
mac->ether_addr_octet[3] = addr->s6_addr[13];
mac->ether_addr_octet[4] = addr->s6_addr[14];
mac->ether_addr_octet[5] = addr->s6_addr[15];
return 0;
}
int batadv_interface_check(const char *mesh_iface)
{
char *debugfs_mnt;
char full_path[MAX_PATH + 1];
FILE *f;
debugfs_mnt = debugfs_mount(NULL);
if (!debugfs_mnt) {
fprintf(stderr, "Could not find debugfs path\n");
return -1;
}
debugfs_make_path(DEBUG_BATIF_PATH_FMT "/" DEBUG_TRANSTABLE_GLOBAL,
mesh_iface, full_path, sizeof(full_path));
f = fopen(full_path, "r");
if (!f) {
fprintf(stderr,
"Could not find %s for interface %s. Make sure it is a valid batman-adv soft-interface\n",
DEBUG_TRANSTABLE_GLOBAL, mesh_iface);
return -1;
}
fclose(f);
debugfs_make_path(DEBUG_BATIF_PATH_FMT "/" DEBUG_ORIGINATORS,
mesh_iface, full_path, sizeof(full_path));
f = fopen(full_path, "r");
if (!f) {
fprintf(stderr,
"Could not find %s for interface %s. Make sure it is a valid batman-adv soft-interface\n",
DEBUG_ORIGINATORS, mesh_iface);
return -1;
}
fclose(f);
return 0;
}
struct ether_addr *translate_mac(const char *mesh_iface, struct ether_addr *mac)
{
enum {
tg_start,
tg_mac,
tg_via,
tg_originator,
} pos;
char full_path[MAX_PATH + 1];
char *debugfs_mnt;
static struct ether_addr in_mac;
struct ether_addr *mac_result, *mac_tmp;
FILE *f = NULL;
size_t len = 0;
char *line = NULL;
char *input, *saveptr, *token;
int line_invalid;
memcpy(&in_mac, mac, sizeof(in_mac));
mac_result = &in_mac;
debugfs_mnt = debugfs_mount(NULL);
if (!debugfs_mnt)
goto out;
debugfs_make_path(DEBUG_BATIF_PATH_FMT "/" DEBUG_TRANSTABLE_GLOBAL,
mesh_iface, full_path, sizeof(full_path));
f = fopen(full_path, "r");
if (!f)
goto out;
while (getline(&line, &len, f) != -1) {
line_invalid = 0;
pos = tg_start;
input = line;
while ((token = strtok_r(input, " \t", &saveptr))) {
input = NULL;
switch (pos) {
case tg_start:
if (strcmp(token, "*") != 0)
line_invalid = 1;
else
pos = tg_mac;
break;
case tg_mac:
mac_tmp = ether_aton(token);
if (!mac_tmp || memcmp(mac_tmp, &in_mac,
sizeof(in_mac)) != 0)
line_invalid = 1;
else
pos = tg_via;
break;
case tg_via:
if (strcmp(token, "via") == 0)
pos = tg_originator;
break;
case tg_originator:
mac_tmp = ether_aton(token);
if (!mac_tmp) {
line_invalid = 1;
} else {
mac_result = mac_tmp;
goto out;
}
break;
}
if (line_invalid)
break;
}
}
out:
if (f)
fclose(f);
free(line);
return mac_result;
}
uint8_t get_tq(const char *mesh_iface, struct ether_addr *mac)
{
enum {
orig_mac,
orig_lastseen,
orig_tqstart,
orig_tqvalue,
} pos;
char full_path[MAX_PATH + 1];
char *debugfs_mnt;
static struct ether_addr in_mac;
struct ether_addr *mac_tmp;
FILE *f = NULL;
size_t len = 0;
char *line = NULL;
char *input, *saveptr, *token;
int line_invalid;
uint8_t tq = 0;
memcpy(&in_mac, mac, sizeof(in_mac));
debugfs_mnt = debugfs_mount(NULL);
if (!debugfs_mnt)
goto out;
debugfs_make_path(DEBUG_BATIF_PATH_FMT "/" DEBUG_ORIGINATORS,
mesh_iface, full_path, sizeof(full_path));
f = fopen(full_path, "r");
if (!f)
goto out;
while (getline(&line, &len, f) != -1) {
line_invalid = 0;
pos = orig_mac;
input = line;
while ((token = strtok_r(input, " \t", &saveptr))) {
input = NULL;
switch (pos) {
case orig_mac:
mac_tmp = ether_aton(token);
if (!mac_tmp || memcmp(mac_tmp, &in_mac,
sizeof(in_mac)) != 0)
line_invalid = 1;
else
pos = orig_lastseen;
break;
case orig_lastseen:
pos = orig_tqstart;
break;
case orig_tqstart:
if (strlen(token) == 0) {
line_invalid = 1;
break;
} else if (token[0] != '(') {
line_invalid = 1;
break;
} else if (strlen(token) == 1) {
pos = orig_tqvalue;
break;
}
token++;
/* fall through */
case orig_tqvalue:
if (token[strlen(token) - 1] != ')') {
line_invalid = 1;
} else {
token[strlen(token) - 1] = '\0';
tq = strtol(token, NULL, 10);
goto out;
}
break;
}
if (line_invalid)
break;
}
}
out:
if (f)
fclose(f);
free(line);
return tq;
}