-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathconf.c
539 lines (447 loc) · 12.8 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/* 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 join_mgroup(int lineno, char *ifname, char *source, char *group)
{
int rc = 0;
if (!ifname || !group) {
errno = EINVAL;
return 1;
}
if (strchr(group, ':')) {
#if !defined(HAVE_IPV6_MULTICAST_HOST) || !defined(HAVE_IPV6_MULTICAST_ROUTING)
WARN("Ignoring join %s on %s, IPv6 disabled.", group, ifname);
#else
inet_addr_t src, grp;
int len = 0;
memset(&src, 0, sizeof(src));
memset(&grp, 0, sizeof(grp));
if (source) {
len = is_range(source);
if (len != 0)
WARN("join: ignoring source perfix len: %d", len);
if (inet_str2addr(source, &src)) {
WARN("join: Invalid IPv6 multicast source: %s", source);
return 1;
}
} else
inet_anyaddr(AF_INET6, &src);
if (group) {
len = is_range(group);
if (len < 0 || len > 128) {
WARN("Invalid IPv6 group prefix length (0-128): %d", len);
return 1;
}
if (!len)
len = 128;
if (inet_str2addr(group, &grp) || !is_multicast(&grp)) {
WARN("join: Invalid IPv6 multicast group: %s", group);
return 1;
}
} else {
WARN("join: missing IPv6 multicast group");
return 1;
}
/* XXX: Add support for GROUP/LEN to IPv6 */
rc = mcgroup_action('j', ifname, &src, &grp, len);
#endif
} else {
inet_addr_t src, grp;
int len = 0;
memset(&src, 0, sizeof(src));
memset(&grp, 0, sizeof(grp));
if (source) {
if ((len = is_range(source)) > 0)
WARN("join: ignoring source perfix len: %d", len);
if (inet_str2addr(source, &src)) {
WARN("join: Invalid IPv4 multicast source: %s", source);
return 1;
}
} else
inet_anyaddr(AF_INET, &src);
if (group) {
len = is_range(group);
if (len < 0 || len > 32) {
WARN("join: Invalid IPv4 group s prefix length (0-32): %d", len);
return 1;
}
if (!len)
len = 32;
if (inet_str2addr(group, &grp) || !is_multicast(&grp)) {
WARN("join: Invalid IPv4 multicast group: %s", group);
return 1;
}
}
rc = mcgroup_action('j', ifname, &src, &grp, len);
}
return rc;
}
static int add_mroute(int lineno, char *ifname, char *group, char *source, char *outbound[], int num)
{
struct ifmatch state_in, state_out;
struct iface *iface;
int rc = 0;
if (!ifname || !group || !outbound || !num) {
errno = EINVAL;
return 1;
}
if (strchr(group, ':')) {
#if !defined(HAVE_IPV6_MULTICAST_HOST) || !defined(HAVE_IPV6_MULTICAST_ROUTING)
WARN("Ignoring mroute for %s from %s, IPv6 disabled.", group, ifname);
return 0;
#else
struct mroute mroute;
int mif;
iface_match_init(&state_in);
while ((mif = iface_match_mif_by_name(ifname, &state_in, NULL)) != NO_VIF) {
int i, total;
memset(&mroute, 0, sizeof(mroute));
mroute.inbound = mif;
if (!source) {
inet_anyaddr(AF_INET6, &mroute.source);
mroute.src_len = 0;
} else {
int len;
if (inet_str2addr(source, &mroute.source)) {
WARN("mroute: Invalid source IPv6 address: %s", source);
return 1;
}
len = is_range(source);
if (len != 0)
WARN("mroute: Unsupported source prefix length: %d", len);
mroute.src_len = 128;
}
if (!group) {
WARN("mroute: Invalid IPv6 multicast group: %s", group);
return 1;
}
mroute.len = is_range(group);
if (mroute.len < 0 || mroute.len > 128) {
WARN("mroute: Invalid IPv6 multicast group prefix length: %d", mroute.len);
return 1;
}
if (mroute.len != 0) {
/* TODO: see mroute.c:is_match6() */
WARN("mroute: Unsupported IPv6 multicast group prefix length: %d", mroute.len);
mroute.len = 128;
} else {
/* Assume missing prefix for specified address as 128 */
mroute.len = 128;
}
if (inet_str2addr(group, &mroute.group) || !is_multicast(&mroute.group)) {
WARN("mroute: Invalid IPv6 multicast group: %s", group);
return 1;
}
total = 0;
for (i = 0; i < num; i++) {
iface_match_init(&state_out);
while ((mif = iface_match_mif_by_name(outbound[i], &state_out, &iface)) != NO_VIF) {
if (mif == 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 IPv6 interface (%s) as inbound (%s) may cause routing loops.", outbound[i], ifname);
}
/* Use a TTL threshold to indicate the list of outbound interfaces. */
mroute.ttl[mif] = iface->threshold;
total++;
}
if (!state_out.match_count)
WARN("mroute: Invalid outbound IPv6 interface, skipping %s", outbound[i]);
}
if (!total) {
WARN("mroute: No valid outbound interfaces, skipping multicast route.");
rc += 1;
} else {
DEBUG("mroute: Adding IPv6 route ...");
rc += mroute6_add(&mroute);
}
}
if (!state_in.match_count) {
WARN("mroute: Invalid inbound IPv6 interface: %s", ifname);
return 1;
}
return rc;
#endif
} else {
struct mroute mroute;
int vif;
iface_match_init(&state_in);
DEBUG("mroute: checking for input iface %s ...", ifname);
while ((vif = iface_match_vif_by_name(ifname, &state_in, NULL)) != NO_VIF) {
int i, total;
DEBUG("mroute: input iface %s has vif %d", ifname, vif);
memset(&mroute, 0, sizeof(mroute));
mroute.inbound = vif;
if (!source) {
inet_anyaddr(AF_INET, &mroute.source);
mroute.src_len = 0;
} else {
mroute.src_len = is_range(source);
if (mroute.src_len < 0 || mroute.src_len > 32) {
WARN("mroute: invalid prefix length: %d", mroute.src_len);
return 1;
}
if (inet_str2addr(source, (inet_addr_t *)&mroute.source)) {
WARN("mroute: Invalid source IPv4 address: %s", source);
return 1;
}
}
if (!group) {
WARN("mroute: Invalid IPv4 multicast group: %s", group);
return 1;
}
mroute.len = is_range(group);
if (mroute.len < 0 || mroute.len > 32) {
WARN("mroute: Invalid IPv4 multicast group prefix length, %d", mroute.len);
return 1;
}
if (inet_str2addr(group, (inet_addr_t *)&mroute.group) || !is_multicast((inet_addr_t *)&mroute.group)) {
char str[INET_ADDRSTR_LEN];
inet_addr2str((inet_addr_t *)&mroute.group, str, sizeof(str));
WARN("mroute: Invalid IPv4 multicast group: %s", str);
return 1;
}
total = 0;
for (i = 0; i < num; i++) {
iface_match_init(&state_out);
DEBUG("mroute: checking for %s ...", outbound[i]);
while ((vif = iface_match_vif_by_name(outbound[i], &state_out, &iface)) != NO_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 IPv4 interface (%s) as inbound (%s) may cause routing loops.", outbound[i], ifname);
}
/* Use a TTL threshold to indicate the list of outbound interfaces. */
mroute.ttl[vif] = iface->threshold;
total++;
}
if (!state_out.match_count)
WARN("mroute: Invalid outbound IPv4 interface, skipping %s", outbound[i]);
}
if (!total) {
WARN("mroute: No valid outbound IPv4 interfaces, skipping multicast route.");
rc += 1;
} else {
rc += mroute4_add(&mroute);
}
}
if (!state_in.match_count) {
WARN("mroute: Invalid inbound IPv4 interface: %s", ifname);
return 1;
}
}
return rc;
}
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;
}
if (op == MGROUP) {
rc += join_mgroup(lineno, ifname, source, group);
} else if (op == MROUTE) {
rc += add_mroute(lineno, ifname, group, source, dest, num);
} else if (op == PHYINT) {
if (enable)
rc += mroute_add_vif(ifname, mrdisc, threshold);
else
rc += mroute_del_vif(ifname);
}
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:
*/