-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathconf.c
434 lines (363 loc) · 9.6 KB
/
conf.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* Simple .conf file parser for smcroute
*
* Copyright (c) 2011-2020 Joachim Wiberg <troglobit@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "log.h"
#include "conf.h"
#include "iface.h"
#include "script.h"
#include "mcgroup.h"
#include "util.h"
#define MAX_LINE_LEN 512
#define DEBUG(fmt, args...) \
smclog(LOG_DEBUG, "%s:%02d: " fmt, conf, lineno, ##args)
#define INFO(fmt, args...) \
smclog(LOG_INFO, "%s:%02d: " fmt, conf, lineno, ##args)
#define WARN(fmt, args...) { \
smclog(LOG_WARNING, "%s:%02d: " fmt, conf, lineno, ##args); \
if (conf_vrfy) \
rc++; \
}
/* Tokens */
#define MGROUP 1
#define MROUTE 2
#define PHYINT 3
static const char *conf = NULL;
static char *pop_token(char **line)
{
char *end, *token;
if (!line)
return NULL;
token = *line;
if (!token)
return NULL;
/* Find start of token, skip whitespace. */
while (*token && isspace((int)*token))
token++;
/* Find end of token. */
end = token;
while (*end && !isspace((int)*end))
end++;
if (end == token) {
*line = NULL;
return NULL;
}
*end = 0; /* Terminate token. */
*line = end + 1;
return token;
}
static int match(char *keyword, char *token)
{
size_t len;
if (!keyword || !token)
return 0;
len = strlen(keyword);
return !strncmp(keyword, token, len);
}
static int conf_mgroup(int lineno, char *ifname, char *source, char *group)
{
inet_addr_t src = { 0 }, grp = { 0 };
int grp_len = 0;
int len_max;
int rc = 0;
if (!ifname || !group) {
errno = EINVAL;
return 1;
}
grp_len = is_range(group);
if (inet_str2addr(group, &grp) || !is_multicast(&grp)) {
WARN("join: Invalid multicast group: %s", group);
goto done;
}
#ifdef HAVE_IPV6_MULTICAST_HOST
if (grp.ss_family == AF_INET6)
len_max = 128;
else
#endif
len_max = 32;
if (grp_len < 0 || grp_len > len_max) {
WARN("join: Invalid group prefix length (0-%d): %d", len_max, grp_len);
goto done;
}
if (!grp_len)
grp_len = len_max;
if (source) {
int src_len = is_range(source);
if (src_len > 0)
WARN("join: ignoring source prefix len: %d", src_len);
if (inet_str2addr(source, &src)) {
WARN("join: Invalid multicast source: %s", source);
goto done;
}
} else
inet_anyaddr(grp.ss_family, &src);
rc += mcgroup_action(1, ifname, &src, &grp, grp_len);
done:
return rc;
}
static int conf_mroute(int lineno, char *ifname, char *group, char *source, char *outbound[], int num)
{
struct ifmatch state_in, state_out;
struct mroute mroute = { 0 };
struct iface *iface;
int len_max;
int rc = 0;
int vif;
if (!ifname || !group || !outbound || !num) {
errno = EINVAL;
return 1;
}
mroute.len = is_range(group);
if (inet_str2addr(group, &mroute.group) || !is_multicast(&mroute.group)) {
WARN("mroute: Invalid multicast group: %s", group);
goto done;
}
#ifdef HAVE_IPV6_MULTICAST_HOST
if (mroute.group.ss_family == AF_INET6)
len_max = 128;
else
#endif
len_max = 32;
if (mroute.len < 0 || mroute.len > len_max) {
WARN("mroute: Invalid multicast group prefix length, %d", mroute.len);
goto done;
}
if (source) {
mroute.src_len = is_range(source);
if (mroute.src_len < 0 || mroute.src_len > len_max) {
WARN("mroute: invalid prefix length: %d", mroute.src_len);
goto done;
}
if (inet_str2addr(source, &mroute.source)) {
WARN("mroute: Invalid source address: %s", source);
goto done;
}
} else {
inet_anyaddr(mroute.group.ss_family, &mroute.source);
mroute.src_len = 0;
}
iface_match_init(&state_in);
DEBUG("mroute: checking for input iface %s ...", ifname);
while (iface_match_vif_by_name(ifname, &state_in, &iface) != NO_VIF) {
int i, total;
#ifdef HAVE_IPV6_MULTICAST_HOST
if (mroute.group.ss_family == AF_INET6)
vif = iface->mif;
else
#endif
vif = iface->vif;
DEBUG("mroute: input iface %s has vif %d", ifname, vif);
mroute.inbound = vif;
total = 0;
for (i = 0; i < num; i++) {
iface_match_init(&state_out);
DEBUG("mroute: checking for %s ...", outbound[i]);
while (iface_match_vif_by_name(outbound[i], &state_out, &iface) != NO_VIF) {
#ifdef HAVE_IPV6_MULTICAST_HOST
if (mroute.group.ss_family == AF_INET6)
vif = iface->mif;
else
#endif
vif = iface->vif;
if (vif == mroute.inbound) {
/* In case of wildcard match in==out is normal, so don't complain */
if (!ifname_is_wildcard(ifname) && !ifname_is_wildcard(outbound[i]))
INFO("mroute: Same outbound interface (%s) as inbound (%s) may cause routing loops.",
outbound[i], ifname);
}
/* Use configured TTL threshold for the output phyint */
mroute.ttl[vif] = iface->threshold;
total++;
}
if (!state_out.match_count)
WARN("mroute: Invalid outbound interface, skipping %s", outbound[i]);
}
if (!total) {
WARN("mroute: No valid outbound interfaces, skipping multicast route.");
rc += 1;
} else {
#ifdef HAVE_IPV6_MULTICAST_HOST
if (mroute.group.ss_family == AF_INET6)
rc += mroute6_add(&mroute);
else
#endif
rc += mroute4_add(&mroute);
}
}
if (!state_in.match_count) {
WARN("mroute: Invalid inbound interface: %s", ifname);
rc++;
}
done:
return rc;
}
static int conf_phyint(int enable, char *ifname, int mrdisc, int threshold)
{
if (enable)
return mroute_add_vif(ifname, mrdisc, threshold);
return mroute_del_vif(ifname);
}
static char *chomp(char *str)
{
char *p;
if (!str || strlen(str) < 1) {
errno = EINVAL;
return NULL;
}
p = str + strlen(str) - 1;
while (p >= str && *p == '\n')
*p-- = 0;
return str;
}
/*
* This function parses the given configuration file according to the
* below format rules. Joins multicast groups and creates multicast
* routes accordingly in the kernel. Whitespace is ignored.
*
* Format:
* phyint IFNAME <enable|disable> [threshold <1-255>]
* mgroup from IFNAME [source ADDRESS] group MCGROUP
* mroute from IFNAME source ADDRESS group MCGROUP to IFNAME [IFNAME ...]
*/
static int conf_parse(const char *file, int do_vifs)
{
char *linebuf, *line;
int lineno = 1;
int rc = 0;
FILE *fp;
fp = fopen(file, "r");
if (!fp)
return 1;
linebuf = malloc(MAX_LINE_LEN * sizeof(char));
if (!linebuf) {
int tmp = errno;
fclose(fp);
errno = tmp;
return 1;
}
conf = file;
while ((line = fgets(linebuf, MAX_LINE_LEN, fp))) {
int op = 0, num = 0, enable = do_vifs;
int mrdisc = 0, threshold = DEFAULT_THRESHOLD;
char *token;
char *ifname = NULL;
char *source = NULL;
char *group = NULL;
char *dest[32];
/* Strip any line end character(s) */
chomp(line);
DEBUG("%s", line);
while ((token = pop_token(&line))) {
/* Strip comments. */
if (match("#", token))
break;
if (!op) {
if (match("mgroup", token)) {
op = MGROUP;
} else if (match("mroute", token)) {
op = MROUTE;
} else if (match("phyint", token)) {
op = PHYINT;
ifname = pop_token(&line);
if (!ifname)
op = 0;
} else if (match("ssmgroup", token)) {
op = MGROUP; /* Compat */
} else {
WARN("Unknown command %s, skipping.", token);
continue;
}
}
if (match("from", token)) {
ifname = pop_token(&line);
} else if (match("source", token)) {
source = pop_token(&line);
} else if (match("group", token)) {
group = pop_token(&line);
} else if (match("to", token)) {
while ((dest[num] = pop_token(&line)))
num++;
} else if (match("enable", token)) {
enable = 1;
} else if (match("disable", token)) {
enable = 0;
} else if (match("mrdisc", token)) {
mrdisc = 1;
} else if (match("ttl-threshold", token)) {
token = pop_token(&line);
if (token) {
int ttl = atoi(token);
if (ttl >= 1 && ttl <= 255)
threshold = ttl;
}
}
}
if (ifname && !iface_exist(ifname)) {
WARN("Interface %s matches no valid system interfaces, skipping.", ifname);
continue;
}
switch (op) {
case MGROUP:
rc += conf_mgroup(lineno, ifname, source, group);
break;
case MROUTE:
rc += conf_mroute(lineno, ifname, group, source, dest, num);
break;
case PHYINT:
rc += conf_phyint(enable, ifname, mrdisc, threshold);
break;
default:
WARN("Unknown token %d", op);
break;
}
lineno++;
}
free(linebuf);
fclose(fp);
return rc;
}
/* Parse .conf file and setup routes */
int conf_read(char *file, int do_vifs)
{
int rc;
if (access(file, R_OK)) {
if (errno == ENOENT)
smclog(LOG_NOTICE, "Configuration file %s does not exist", file);
else
smclog(LOG_WARNING, "Unexpected error when accessing %s: %s", file, strerror(errno));
if (!conf_vrfy)
smclog(LOG_NOTICE, "Continuing anyway, waiting for client to connect.");
return 1;
}
rc = conf_parse(file, do_vifs);
if (rc)
smclog(LOG_WARNING, "Failed reading %s: %s.", file, errno ? strerror(errno): "parse error");
else
script_exec(NULL);
return rc;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/