summaryrefslogtreecommitdiff
blob: 5496c36b2377933eb118ebc6ae106a9409b34c64 (plain)
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
/*
 * Copyright (C) 2007-2012 Red Hat, Inc.
 * Copyright (C) 2009 IBM Corp.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * based on iptables.c
 * Authors:
 *     Gerhard Stenzel <gerhard.stenzel@de.ibm.com>
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>

#ifdef HAVE_PATHS_H
# include <paths.h>
#endif

#include "internal.h"
#include "ebtables.h"
#include "command.h"
#include "memory.h"
#include "virterror_internal.h"
#include "logging.h"
#include "threads.h"

#if HAVE_FIREWALLD
static char *firewall_cmd_path = NULL;

static int
virEbTablesOnceInit(void)
{
    firewall_cmd_path = virFindFileInPath("firewall-cmd");
    if (!firewall_cmd_path) {
        VIR_INFO("firewall-cmd not found on system. "
                 "firewalld support disabled for ebtables.");
    } else {
        virCommandPtr cmd = virCommandNew(firewall_cmd_path);
        int status;

        virCommandAddArgList(cmd, "--state", NULL);
        if (virCommandRun(cmd, &status) < 0 || status != 0) {
            VIR_INFO("firewall-cmd found but disabled for ebtables");
            VIR_FREE(firewall_cmd_path);
            firewall_cmd_path = NULL;
        } else {
            VIR_INFO("using firewalld for ebtables commands");
        }
        virCommandFree(cmd);
    }
    return 0;
}

VIR_ONCE_GLOBAL_INIT(virEbTables)

#endif

struct _ebtablesContext
{
    ebtRules *input_filter;
    ebtRules *forward_filter;
    ebtRules *nat_postrouting;
};

enum {
    ADD = 0,
    REMOVE,
    CREATE,
    POLICY,
    INSERT
};

static void
ebtRuleFree(ebtRule *rule)
{
    VIR_FREE(rule->rule);

    if (rule->argv) {
        int i = 0;
        while (rule->argv[i])
            VIR_FREE(rule->argv[i++]);
        VIR_FREE(rule->argv);
    }
}

static int
ebtRulesAppend(ebtRules *rules,
               char *rule,
               const char **argv,
               int command_idx)
{
    if (VIR_REALLOC_N(rules->rules, rules->nrules+1) < 0) {
        int i = 0;
        while (argv[i])
            VIR_FREE(argv[i++]);
        VIR_FREE(argv);
        return ENOMEM;
    }

    rules->rules[rules->nrules].rule        = rule;
    rules->rules[rules->nrules].argv        = argv;
    rules->rules[rules->nrules].command_idx = command_idx;

    rules->nrules++;

    return 0;
}

static int
ebtRulesRemove(ebtRules *rules,
               char *rule)
{
    int i;

    for (i = 0; i < rules->nrules; i++)
        if (STREQ(rules->rules[i].rule, rule))
            break;

    if (i >= rules->nrules)
        return EINVAL;

    ebtRuleFree(&rules->rules[i]);

    memmove(&rules->rules[i],
            &rules->rules[i+1],
            (rules->nrules - i - 1) * sizeof(ebtRule));

    rules->nrules--;

    return 0;
}

static void
ebtRulesFree(ebtRules *rules)
{
    int i;

    VIR_FREE(rules->table);
    VIR_FREE(rules->chain);

    if (rules->rules) {
        for (i = 0; i < rules->nrules; i++)
            ebtRuleFree(&rules->rules[i]);

        VIR_FREE(rules->rules);

        rules->nrules = 0;
    }

    VIR_FREE(rules);
}

static ebtRules *
ebtRulesNew(const char *table,
            const char *chain)
{
    ebtRules *rules;

    if (VIR_ALLOC(rules) < 0)
        return NULL;

    if (!(rules->table = strdup(table)))
        goto error;

    if (!(rules->chain = strdup(chain)))
        goto error;

    rules->rules = NULL;
    rules->nrules = 0;

    return rules;

 error:
    ebtRulesFree(rules);
    return NULL;
}

static int ATTRIBUTE_SENTINEL
ebtablesAddRemoveRule(ebtRules *rules, int action, const char *arg, ...)
{
    va_list args;
    int retval = ENOMEM;
    const char **argv;
    char *rule = NULL;
    const char *s;
    int n, command_idx;

    n = 1 + /* /sbin/ebtables  */
        2 + /*   --table foo   */
        2 + /*   --insert bar  */
        1;  /*   arg           */

#if HAVE_FIREWALLD
    virEbTablesInitialize();
    if (firewall_cmd_path)
        n += 3; /* --direct --passthrough eb */
#endif

    va_start(args, arg);
    while (va_arg(args, const char *))
        n++;

    va_end(args);

    if (VIR_ALLOC_N(argv, n + 1) < 0)
        goto error;

    n = 0;

#if HAVE_FIREWALLD
    if (firewall_cmd_path) {
        if (!(argv[n++] = strdup(firewall_cmd_path)))
            goto error;
        if (!(argv[n++] = strdup("--direct")))
            goto error;
        if (!(argv[n++] = strdup("--passthrough")))
            goto error;
        if (!(argv[n++] = strdup("eb")))
            goto error;
    } else
#endif
    if (!(argv[n++] = strdup(EBTABLES_PATH)))
        goto error;

    command_idx = n;

    if(action == ADD || action == REMOVE) {
        if (!(argv[n++] = strdup("--insert")))
            goto error;

        if (!(argv[n++] = strdup(rules->chain)))
            goto error;
    }

    if (!(argv[n++] = strdup(arg)))
        goto error;

    va_start(args, arg);

    while ((s = va_arg(args, const char *))) {
        if (!(argv[n++] = strdup(s))) {
            va_end(args);
            goto error;
        }
    }

    va_end(args);

    if (!(rule = virArgvToString(&argv[command_idx])))
        goto error;

    if (action == REMOVE) {
        VIR_FREE(argv[command_idx]);
        if (!(argv[command_idx] = strdup("--delete")))
            goto error;
    }

    if (virRun(argv, NULL) < 0) {
        retval = errno;
        goto error;
    }

    if (action == ADD || action == CREATE || action == POLICY ||
        action == INSERT) {
        retval = ebtRulesAppend(rules, rule, argv, command_idx);
        rule = NULL;
        argv = NULL;
    } else {
        retval = ebtRulesRemove(rules, rule);
    }

 error:
    VIR_FREE(rule);

    if (argv) {
        n = 0;
        while (argv[n])
            VIR_FREE(argv[n++]);
        VIR_FREE(argv);
    }

    return retval;
}


/**
 * ebtablesContextNew:
 *
 * Create a new ebtable context
 *
 * Returns a pointer to the new structure or NULL in case of error
 */
ebtablesContext *
ebtablesContextNew(const char *driver)
{
    bool success = false;
    ebtablesContext *ctx = NULL;
    char *input_chain = NULL;
    char *forward_chain = NULL;
    char *nat_chain = NULL;

    if (VIR_ALLOC(ctx) < 0)
        return NULL;

    if (virAsprintf(&input_chain, "libvirt_%s_INPUT", driver) < 0 ||
        virAsprintf(&forward_chain, "libvirt_%s_FORWARD", driver) < 0 ||
        virAsprintf(&nat_chain, "libvirt_%s_POSTROUTING", driver) < 0) {
        goto cleanup;
    }

    if (!(ctx->input_filter = ebtRulesNew("filter", input_chain)))
        goto cleanup;

    if (!(ctx->forward_filter = ebtRulesNew("filter", forward_chain)))
        goto cleanup;

    if (!(ctx->nat_postrouting = ebtRulesNew("nat", nat_chain)))
        goto cleanup;

    success = true;

cleanup:
    VIR_FREE(input_chain);
    VIR_FREE(forward_chain);
    VIR_FREE(nat_chain);

    if (!success) {
        ebtablesContextFree(ctx);
        ctx = NULL;
    }

    return ctx;
}

/**
 * ebtablesContextFree:
 * @ctx: pointer to the EB table context
 *
 * Free the resources associated with an EB table context
 */
void
ebtablesContextFree(ebtablesContext *ctx)
{
    if (!ctx)
        return;
    if (ctx->input_filter)
        ebtRulesFree(ctx->input_filter);
    if (ctx->forward_filter)
        ebtRulesFree(ctx->forward_filter);
    if (ctx->nat_postrouting)
        ebtRulesFree(ctx->nat_postrouting);
    VIR_FREE(ctx);
}

int
ebtablesAddForwardPolicyReject(ebtablesContext *ctx)
{
    return ebtablesForwardPolicyReject(ctx, ADD);
}


int
ebtablesRemoveForwardPolicyReject(ebtablesContext *ctx)
{
    return ebtablesForwardPolicyReject(ctx, REMOVE);
}

int
ebtablesForwardPolicyReject(ebtablesContext *ctx,
                            int action)
{
    /* create it, if it does not exist */
    if (action == ADD) {
        ebtablesAddRemoveRule(ctx->forward_filter,
                              CREATE,
                              "--new-chain", ctx->forward_filter->chain, NULL,
                              NULL);
        ebtablesAddRemoveRule(ctx->forward_filter,
                              INSERT,
                              "--insert", "FORWARD", "--jump",
                              ctx->forward_filter->chain, NULL);
    }

    return ebtablesAddRemoveRule(ctx->forward_filter,
                                 POLICY,
                                 "-P", ctx->forward_filter->chain, "DROP",
                                 NULL);
}

/*
 * Allow all traffic destined to the bridge, with a valid network address
 */
static int
ebtablesForwardAllowIn(ebtablesContext *ctx,
                       const char *iface,
                       const char *macaddr,
                       int action)
{
    return ebtablesAddRemoveRule(ctx->forward_filter,
                                     action,
                                     "--in-interface", iface,
                                     "--source", macaddr,
                                     "--jump", "ACCEPT",
                                     NULL);
}

/**
 * ebtablesAddForwardAllowIn:
 * @ctx: pointer to the EB table context
 * @iface: the output interface name
 * @physdev: the physical input device or NULL
 *
 * Add rules to the EB table context to allow the traffic on
 * @physdev device to be forwarded to interface @iface. This allows
 * the inbound traffic on a bridge.
 *
 * Returns 0 in case of success or an error code otherwise
 */
int
ebtablesAddForwardAllowIn(ebtablesContext *ctx,
                          const char *iface,
                          const virMacAddrPtr mac)
{
    char *macaddr;

    if (virAsprintf(&macaddr,
                    "%02x:%02x:%02x:%02x:%02x:%02x",
                    mac->addr[0], mac->addr[1],
                    mac->addr[2], mac->addr[3],
                    mac->addr[4], mac->addr[5]) < 0) {
        return -1;
    }
    return ebtablesForwardAllowIn(ctx, iface, macaddr, ADD);
}

/**
 * ebtablesRemoveForwardAllowIn:
 * @ctx: pointer to the EB table context
 * @iface: the output interface name
 * @physdev: the physical input device or NULL
 *
 * Remove rules from the EB table context hence forbidding the traffic
 * on the @physdev device to be forwarded to interface @iface. This
 * stops the inbound traffic on a bridge.
 *
 * Returns 0 in case of success or an error code otherwise
 */
int
ebtablesRemoveForwardAllowIn(ebtablesContext *ctx,
                             const char *iface,
                             const virMacAddrPtr mac)
{
    char *macaddr;

    if (virAsprintf(&macaddr,
                    "%02x:%02x:%02x:%02x:%02x:%02x",
                    mac->addr[0], mac->addr[1],
                    mac->addr[2], mac->addr[3],
                    mac->addr[4], mac->addr[5]) < 0) {
       return -1;
    }
    return ebtablesForwardAllowIn(ctx, iface, macaddr, REMOVE);
}