viriptables.c 36.5 KB
Newer Older
1
/*
2 3
 * viriptables.c: helper APIs for managing iptables
 *
4
 * Copyright (C) 2007-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20
 */

21
#include <config.h>
22 23 24 25 26 27

#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
E
Eric Blake 已提交
28
#include <sys/wait.h>
29

30
#include "internal.h"
31
#include "viriptables.h"
32
#include "vircommand.h"
33
#include "viralloc.h"
34
#include "virerror.h"
35
#include "virfile.h"
36
#include "virlog.h"
37
#include "virthread.h"
38 39
#include "virstring.h"
#include "virutil.h"
40
#include "virhash.h"
41

42 43
VIR_LOG_INIT("util.iptables");

44
#define VIR_FROM_THIS VIR_FROM_NONE
45

46 47 48 49 50
enum {
    ADD = 0,
    REMOVE
};

51
static bool deletePrivate = true;
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
typedef struct {
    const char *parent;
    const char *child;
} iptablesGlobalChain;

typedef struct {
    virFirewallLayer layer;
    const char *table;
    iptablesGlobalChain *chains;
    size_t nchains;
    bool *changed;
} iptablesGlobalChainData;


static int
iptablesPrivateChainCreate(virFirewallPtr fw,
                           virFirewallLayer layer,
                           const char *const *lines,
                           void *opaque)
{
    iptablesGlobalChainData *data = opaque;
    virHashTablePtr chains = NULL;
    virHashTablePtr links = NULL;
    const char *const *tmp;
    int ret = -1;
    size_t i;

    if (!(chains = virHashCreate(50, NULL)))
        goto cleanup;
    if (!(links = virHashCreate(50, NULL)))
        goto cleanup;

    tmp = lines;
    while (tmp && *tmp) {
        if (STRPREFIX(*tmp, "-N ")) { /* eg "-N LIBVIRT_INP" */
            if (virHashUpdateEntry(chains, *tmp + 3, (void *)0x1) < 0)
                goto cleanup;
        } else if (STRPREFIX(*tmp, "-A ")) { /* eg "-A INPUT -j LIBVIRT_INP" */
            char *sep = strchr(*tmp + 3, ' ');
            if (sep) {
                *sep = '\0';
                if (STRPREFIX(sep + 1, "-j ")) {
                    if (virHashUpdateEntry(links, sep + 4,
                                           (char *)*tmp + 3) < 0)
                        goto cleanup;
                }
            }
        }
        tmp++;
    }

    for (i = 0; i < data->nchains; i++) {
        const char *from;
        if (!virHashLookup(chains, data->chains[i].child)) {
            virFirewallAddRule(fw, layer,
                               "--table", data->table,
                               "--new-chain", data->chains[i].child, NULL);
            *data->changed = true;
        }

        from = virHashLookup(links, data->chains[i].child);
        if (!from || STRNEQ(from, data->chains[i].parent))
            virFirewallAddRule(fw, layer,
                               "--table", data->table,
                               "--insert", data->chains[i].parent,
                               "--jump", data->chains[i].child, NULL);
    }

    ret = 0;
 cleanup:
    virHashFree(chains);
    virHashFree(links);
    return ret;
}


int
iptablesSetupPrivateChains(void)
{
    virFirewallPtr fw = NULL;
    int ret = -1;
    iptablesGlobalChain filter_chains[] = {
        {"INPUT", "LIBVIRT_INP"},
        {"OUTPUT", "LIBVIRT_OUT"},
        {"FORWARD", "LIBVIRT_FWO"},
        {"FORWARD", "LIBVIRT_FWI"},
        {"FORWARD", "LIBVIRT_FWX"},
    };
    iptablesGlobalChain natmangle_chains[] = {
        {"POSTROUTING",  "LIBVIRT_PRT"},
    };
    bool changed = false;
    iptablesGlobalChainData data[] = {
        { VIR_FIREWALL_LAYER_IPV4, "filter",
          filter_chains, ARRAY_CARDINALITY(filter_chains), &changed },
        { VIR_FIREWALL_LAYER_IPV4, "nat",
          natmangle_chains, ARRAY_CARDINALITY(natmangle_chains), &changed },
        { VIR_FIREWALL_LAYER_IPV4, "mangle",
          natmangle_chains, ARRAY_CARDINALITY(natmangle_chains), &changed },
        { VIR_FIREWALL_LAYER_IPV6, "filter",
          filter_chains, ARRAY_CARDINALITY(filter_chains), &changed },
        { VIR_FIREWALL_LAYER_IPV6, "nat",
          natmangle_chains, ARRAY_CARDINALITY(natmangle_chains), &changed },
        { VIR_FIREWALL_LAYER_IPV6, "mangle",
          natmangle_chains, ARRAY_CARDINALITY(natmangle_chains), &changed },
    };
    size_t i;

    fw = virFirewallNew();

    virFirewallStartTransaction(fw, 0);

    for (i = 0; i < ARRAY_CARDINALITY(data); i++)
        virFirewallAddRuleFull(fw, data[i].layer,
                               false, iptablesPrivateChainCreate,
                               &(data[i]), "--table", data[i].table,
                               "--list-rules", NULL);

    if (virFirewallApply(fw) < 0)
        goto cleanup;

    ret = changed ? 1 : 0;

 cleanup:

    virFirewallFree(fw);
    return ret;
}


183 184 185 186 187 188 189
void
iptablesSetDeletePrivate(bool pvt)
{
    deletePrivate = pvt;
}


190 191 192
static void
iptablesInput(virFirewallPtr fw,
              virFirewallLayer layer,
193
              bool pvt,
194 195 196 197 198 199 200 201 202 203
              const char *iface,
              int port,
              int action,
              int tcp)
{
    char portstr[32];

    snprintf(portstr, sizeof(portstr), "%d", port);
    portstr[sizeof(portstr) - 1] = '\0';

204 205
    virFirewallAddRule(fw, layer,
                       "--table", "filter",
206 207
                       action == ADD ? "--insert" : "--delete",
                       pvt ? "LIBVIRT_INP" : "INPUT",
208 209 210 211 212
                       "--in-interface", iface,
                       "--protocol", tcp ? "tcp" : "udp",
                       "--destination-port", portstr,
                       "--jump", "ACCEPT",
                       NULL);
213 214
}

215 216 217
static void
iptablesOutput(virFirewallPtr fw,
               virFirewallLayer layer,
218
               bool pvt,
219 220 221 222 223 224 225 226 227 228
               const char *iface,
               int port,
               int action,
               int tcp)
{
    char portstr[32];

    snprintf(portstr, sizeof(portstr), "%d", port);
    portstr[sizeof(portstr) - 1] = '\0';

229 230
    virFirewallAddRule(fw, layer,
                       "--table", "filter",
231 232
                       action == ADD ? "--insert" : "--delete",
                       pvt ? "LIBVIRT_OUT" : "OUTPUT",
233 234 235 236 237
                       "--out-interface", iface,
                       "--protocol", tcp ? "tcp" : "udp",
                       "--destination-port", portstr,
                       "--jump", "ACCEPT",
                       NULL);
238 239
}

240 241 242 243 244 245 246 247 248
/**
 * iptablesAddTcpInput:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the TCP port to add
 *
 * Add an input to the IP table allowing access to the given @port on
 * the given @iface interface for TCP packets
 */
249 250 251
void
iptablesAddTcpInput(virFirewallPtr fw,
                    virFirewallLayer layer,
252 253 254
                    const char *iface,
                    int port)
{
255
    iptablesInput(fw, layer, true, iface, port, ADD, 1);
256 257
}

258 259 260 261 262 263
/**
 * iptablesRemoveTcpInput:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the TCP port to remove
 *
R
Richard W.M. Jones 已提交
264
 * Removes an input from the IP table, hence forbidding access to the given
265 266
 * @port on the given @iface interface for TCP packets
 */
267 268 269
void
iptablesRemoveTcpInput(virFirewallPtr fw,
                       virFirewallLayer layer,
270 271 272
                       const char *iface,
                       int port)
{
273
    iptablesInput(fw, layer, deletePrivate, iface, port, REMOVE, 1);
274 275
}

276 277 278 279 280 281 282 283 284
/**
 * iptablesAddUdpInput:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the UDP port to add
 *
 * Add an input to the IP table allowing access to the given @port on
 * the given @iface interface for UDP packets
 */
285 286 287
void
iptablesAddUdpInput(virFirewallPtr fw,
                    virFirewallLayer layer,
288 289 290
                    const char *iface,
                    int port)
{
291
    iptablesInput(fw, layer, true, iface, port, ADD, 0);
292 293
}

294 295 296 297 298 299
/**
 * iptablesRemoveUdpInput:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the UDP port to remove
 *
R
Richard W.M. Jones 已提交
300
 * Removes an input from the IP table, hence forbidding access to the given
301 302
 * @port on the given @iface interface for UDP packets
 */
303 304 305
void
iptablesRemoveUdpInput(virFirewallPtr fw,
                       virFirewallLayer layer,
306 307 308
                       const char *iface,
                       int port)
{
309
    iptablesInput(fw, layer, deletePrivate, iface, port, REMOVE, 0);
310 311
}

312 313 314 315 316 317 318 319 320
/**
 * iptablesAddUdpOutput:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the UDP port to add
 *
 * Add an output to the IP table allowing access to the given @port from
 * the given @iface interface for UDP packets
 */
321 322 323
void
iptablesAddUdpOutput(virFirewallPtr fw,
                     virFirewallLayer layer,
324 325 326
                     const char *iface,
                     int port)
{
327
    iptablesOutput(fw, layer, true, iface, port, ADD, 0);
328 329 330 331 332 333 334 335 336 337 338
}

/**
 * iptablesRemoveUdpOutput:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the UDP port to remove
 *
 * Removes an output from the IP table, hence forbidding access to the given
 * @port from the given @iface interface for UDP packets
 */
339 340 341
void
iptablesRemoveUdpOutput(virFirewallPtr fw,
                        virFirewallLayer layer,
342 343 344
                        const char *iface,
                        int port)
{
345
    iptablesOutput(fw, layer, deletePrivate, iface, port, REMOVE, 0);
346 347
}

348

349
static char *iptablesFormatNetwork(virSocketAddr *netaddr,
350
                                   unsigned int prefix)
351 352
{
    virSocketAddr network;
353
    VIR_AUTOFREE(char *) netstr = NULL;
354 355
    char *ret;

356 357
    if (!(VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET) ||
          VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET6))) {
358 359
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Only IPv4 or IPv6 addresses can be used with iptables"));
360 361 362
        return NULL;
    }

363
    if (virSocketAddrMaskByPrefix(netaddr, prefix, &network) < 0) {
364 365
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Failure to mask address"));
366 367
        return NULL;
    }
368

369
    netstr = virSocketAddrFormat(&network);
370 371 372 373

    if (!netstr)
        return NULL;

374
    ignore_value(virAsprintf(&ret, "%s/%d", netstr, prefix));
375 376 377 378 379

    return ret;
}


380 381 382
/* Allow all traffic coming from the bridge, with a valid network address
 * to proceed to WAN
 */
383
static int
384
iptablesForwardAllowOut(virFirewallPtr fw,
385
                        bool pvt,
386
                        virSocketAddr *netaddr,
387
                        unsigned int prefix,
388 389 390
                        const char *iface,
                        const char *physdev,
                        int action)
391
{
392
    VIR_AUTOFREE(char *) networkstr = NULL;
393 394
    virFirewallLayer layer = VIR_SOCKET_ADDR_FAMILY(netaddr) == AF_INET ?
        VIR_FIREWALL_LAYER_IPV4 : VIR_FIREWALL_LAYER_IPV6;
395

396
    if (!(networkstr = iptablesFormatNetwork(netaddr, prefix)))
397 398
        return -1;

399
    if (physdev && physdev[0])
400 401
        virFirewallAddRule(fw, layer,
                           "--table", "filter",
402 403
                           action == ADD ? "--insert" : "--delete",
                           pvt ? "LIBVIRT_FWO" : "FORWARD",
404 405 406 407 408 409 410 411
                           "--source", networkstr,
                           "--in-interface", iface,
                           "--out-interface", physdev,
                           "--jump", "ACCEPT",
                           NULL);
    else
        virFirewallAddRule(fw, layer,
                           "--table", "filter",
412 413
                           action == ADD ? "--insert" : "--delete",
                           pvt ? "LIBVIRT_FWO" : "FORWARD",
414 415 416 417
                           "--source", networkstr,
                           "--in-interface", iface,
                           "--jump", "ACCEPT",
                           NULL);
418

419
    return 0;
420 421
}

422 423 424 425 426 427
/**
 * iptablesAddForwardAllowOut:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @iface: the source interface name
 * @physdev: the physical output device
428
 *
429 430 431 432 433 434
 * Add a rule to the IP table context to allow the traffic for the
 * network @network via interface @iface to be forwarded to
 * @physdev device. This allow the outbound traffic on a bridge.
 *
 * Returns 0 in case of success or an error code otherwise
 */
435
int
436 437
iptablesAddForwardAllowOut(virFirewallPtr fw,
                           virSocketAddr *netaddr,
438
                           unsigned int prefix,
439 440
                           const char *iface,
                           const char *physdev)
441
{
442
    return iptablesForwardAllowOut(fw, true, netaddr, prefix, iface, physdev, ADD);
443 444
}

445 446 447 448 449 450
/**
 * iptablesRemoveForwardAllowOut:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @iface: the source interface name
 * @physdev: the physical output device
451
 *
452 453 454 455 456 457
 * Remove a rule from the IP table context hence forbidding forwarding
 * of the traffic for the network @network via interface @iface
 * to the @physdev device output. This stops the outbound traffic on a bridge.
 *
 * Returns 0 in case of success or an error code otherwise
 */
458
int
459 460
iptablesRemoveForwardAllowOut(virFirewallPtr fw,
                              virSocketAddr *netaddr,
461
                              unsigned int prefix,
462 463
                              const char *iface,
                              const char *physdev)
464
{
465
    return iptablesForwardAllowOut(fw, deletePrivate, netaddr, prefix, iface, physdev, REMOVE);
466 467
}

468 469 470 471

/* Allow all traffic destined to the bridge, with a valid network address
 * and associated with an existing connection
 */
472
static int
473
iptablesForwardAllowRelatedIn(virFirewallPtr fw,
474
                              bool pvt,
475
                              virSocketAddr *netaddr,
476
                              unsigned int prefix,
477 478 479
                              const char *iface,
                              const char *physdev,
                              int action)
480
{
481 482
    virFirewallLayer layer = VIR_SOCKET_ADDR_FAMILY(netaddr) == AF_INET ?
        VIR_FIREWALL_LAYER_IPV4 : VIR_FIREWALL_LAYER_IPV6;
483
    VIR_AUTOFREE(char *) networkstr = NULL;
484

485
    if (!(networkstr = iptablesFormatNetwork(netaddr, prefix)))
486 487
        return -1;

488 489 490
    if (physdev && physdev[0])
        virFirewallAddRule(fw, layer,
                           "--table", "filter",
491 492
                           action == ADD ? "--insert" : "--delete",
                           pvt ? "LIBVIRT_FWI" : "FORWARD",
493 494 495 496 497 498 499 500 501 502
                           "--destination", networkstr,
                           "--in-interface", physdev,
                           "--out-interface", iface,
                           "--match", "conntrack",
                           "--ctstate", "ESTABLISHED,RELATED",
                           "--jump", "ACCEPT",
                           NULL);
    else
        virFirewallAddRule(fw, layer,
                           "--table", "filter",
503 504
                           action == ADD ? "--insert" : "--delete",
                           pvt ? "LIBVIRT_FWI" : "FORWARD",
505 506 507 508 509 510 511 512
                           "--destination", networkstr,
                           "--out-interface", iface,
                           "--match", "conntrack",
                           "--ctstate", "ESTABLISHED,RELATED",
                           "--jump", "ACCEPT",
                           NULL);

    return 0;
513 514
}

515 516 517 518 519 520 521 522 523 524 525 526 527 528
/**
 * iptablesAddForwardAllowRelatedIn:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @iface: the output interface name
 * @physdev: the physical input device or NULL
 *
 * Add rules to the IP table context to allow the traffic for the
 * network @network on @physdev device to be forwarded to
 * interface @iface, if it is part of an existing connection.
 *
 * Returns 0 in case of success or an error code otherwise
 */
int
529 530
iptablesAddForwardAllowRelatedIn(virFirewallPtr fw,
                                 virSocketAddr *netaddr,
531
                                 unsigned int prefix,
532 533
                                 const char *iface,
                                 const char *physdev)
534
{
535
    return iptablesForwardAllowRelatedIn(fw, true, netaddr, prefix, iface, physdev, ADD);
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
}

/**
 * iptablesRemoveForwardAllowRelatedIn:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @iface: the output interface name
 * @physdev: the physical input device or NULL
 *
 * Remove rules from the IP table context hence forbidding the traffic for
 * network @network on @physdev device to be forwarded to
 * interface @iface, if it is part of an existing connection.
 *
 * Returns 0 in case of success or an error code otherwise
 */
int
552 553
iptablesRemoveForwardAllowRelatedIn(virFirewallPtr fw,
                                    virSocketAddr *netaddr,
554
                                    unsigned int prefix,
555 556
                                    const char *iface,
                                    const char *physdev)
557
{
558
    return iptablesForwardAllowRelatedIn(fw, deletePrivate, netaddr, prefix, iface, physdev, REMOVE);
559 560 561 562 563
}

/* Allow all traffic destined to the bridge, with a valid network address
 */
static int
564
iptablesForwardAllowIn(virFirewallPtr fw,
565
                       bool pvt,
566
                       virSocketAddr *netaddr,
567
                       unsigned int prefix,
568 569 570 571
                       const char *iface,
                       const char *physdev,
                       int action)
{
572 573
    virFirewallLayer layer = VIR_SOCKET_ADDR_FAMILY(netaddr) == AF_INET ?
        VIR_FIREWALL_LAYER_IPV4 : VIR_FIREWALL_LAYER_IPV6;
574
    VIR_AUTOFREE(char *) networkstr = NULL;
575

576
    if (!(networkstr = iptablesFormatNetwork(netaddr, prefix)))
577 578
        return -1;

579 580 581
    if (physdev && physdev[0])
        virFirewallAddRule(fw, layer,
                           "--table", "filter",
582 583
                           action == ADD ? "--insert" : "--delete",
                           pvt ? "LIBVIRT_FWI" : "FORWARD",
584 585 586 587 588 589 590 591
                           "--destination", networkstr,
                           "--in-interface", physdev,
                           "--out-interface", iface,
                           "--jump", "ACCEPT",
                           NULL);
    else
        virFirewallAddRule(fw, layer,
                           "--table", "filter",
592 593
                           action == ADD ? "--insert" : "--delete",
                           pvt ? "LIBVIRT_FWI" : "FORWARD",
594 595 596 597 598
                           "--destination", networkstr,
                           "--out-interface", iface,
                           "--jump", "ACCEPT",
                           NULL);
    return 0;
599 600
}

601 602 603 604 605 606
/**
 * iptablesAddForwardAllowIn:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @iface: the output interface name
 * @physdev: the physical input device or NULL
607
 *
608 609 610 611 612 613
 * Add rules to the IP table context to allow the traffic for the
 * network @network on @physdev device to be forwarded to
 * interface @iface. This allow the inbound traffic on a bridge.
 *
 * Returns 0 in case of success or an error code otherwise
 */
614
int
615 616
iptablesAddForwardAllowIn(virFirewallPtr fw,
                          virSocketAddr *netaddr,
617
                          unsigned int prefix,
618 619 620
                          const char *iface,
                          const char *physdev)
{
621
    return iptablesForwardAllowIn(fw, true, netaddr, prefix, iface, physdev, ADD);
622 623
}

624 625 626 627 628 629
/**
 * iptablesRemoveForwardAllowIn:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @iface: the output interface name
 * @physdev: the physical input device or NULL
630
 *
631 632 633 634 635 636
 * Remove rules from the IP table context hence forbidding the traffic for
 * network @network on @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
 */
637
int
638 639
iptablesRemoveForwardAllowIn(virFirewallPtr fw,
                             virSocketAddr *netaddr,
640
                             unsigned int prefix,
641 642 643
                             const char *iface,
                             const char *physdev)
{
644
    return iptablesForwardAllowIn(fw, deletePrivate, netaddr, prefix, iface, physdev, REMOVE);
645 646
}

647 648 649
static void
iptablesForwardAllowCross(virFirewallPtr fw,
                          virFirewallLayer layer,
650
                          bool pvt,
651 652 653 654 655
                          const char *iface,
                          int action)
{
    virFirewallAddRule(fw, layer,
                       "--table", "filter",
656 657
                       action == ADD ? "--insert" : "--delete",
                       pvt ? "LIBVIRT_FWX" : "FORWARD",
658 659 660 661 662 663
                       "--in-interface", iface,
                       "--out-interface", iface,
                       "--jump", "ACCEPT",
                       NULL);
}

664 665 666 667 668 669 670 671 672 673 674
/**
 * iptablesAddForwardAllowCross:
 * @ctx: pointer to the IP table context
 * @iface: the input/output interface name
 *
 * Add rules to the IP table context to allow traffic to cross that
 * interface. It allows all traffic between guests on the same bridge
 * represented by that interface.
 *
 * Returns 0 in case of success or an error code otherwise
 */
675 676 677
void
iptablesAddForwardAllowCross(virFirewallPtr fw,
                             virFirewallLayer layer,
678 679
                             const char *iface)
{
680
    iptablesForwardAllowCross(fw, layer, true, iface, ADD);
681 682
}

683 684 685 686 687 688 689 690 691 692 693
/**
 * iptablesRemoveForwardAllowCross:
 * @ctx: pointer to the IP table context
 * @iface: the input/output interface name
 *
 * Remove rules to the IP table context to block traffic to cross that
 * interface. It forbids traffic between guests on the same bridge
 * represented by that interface.
 *
 * Returns 0 in case of success or an error code otherwise
 */
694 695 696
void
iptablesRemoveForwardAllowCross(virFirewallPtr fw,
                                virFirewallLayer layer,
697
                                const char *iface)
698
{
699
    iptablesForwardAllowCross(fw, layer, deletePrivate, iface, REMOVE);
700 701 702 703 704
}

static void
iptablesForwardRejectOut(virFirewallPtr fw,
                         virFirewallLayer layer,
705
                         bool pvt,
706 707
                         const char *iface,
                         int action)
708
{
709 710
    virFirewallAddRule(fw, layer,
                       "--table", "filter",
711 712
                       action == ADD ? "--insert" : "--delete",
                       pvt ? "LIBVIRT_FWO" : "FORWARD",
713
                       "--in-interface", iface,
714
                       "--jump", "REJECT",
715
                       NULL);
716 717
}

718 719 720 721 722 723 724 725 726 727
/**
 * iptablesAddForwardRejectOut:
 * @ctx: pointer to the IP table context
 * @iface: the output interface name
 *
 * Add rules to the IP table context to forbid all traffic to that
 * interface. It forbids forwarding from the bridge to that interface.
 *
 * Returns 0 in case of success or an error code otherwise
 */
728 729 730
void
iptablesAddForwardRejectOut(virFirewallPtr fw,
                            virFirewallLayer layer,
731 732
                            const char *iface)
{
733
    iptablesForwardRejectOut(fw, layer, true, iface, ADD);
734 735
}

736 737 738 739 740 741 742 743 744 745
/**
 * iptablesRemoveForwardRejectOut:
 * @ctx: pointer to the IP table context
 * @iface: the output interface name
 *
 * Remove rules from the IP table context forbidding all traffic to that
 * interface. It reallow forwarding from the bridge to that interface.
 *
 * Returns 0 in case of success or an error code otherwise
 */
746 747 748
void
iptablesRemoveForwardRejectOut(virFirewallPtr fw,
                               virFirewallLayer layer,
749
                               const char *iface)
750
{
751
    iptablesForwardRejectOut(fw, layer, deletePrivate, iface, REMOVE);
752 753 754 755 756 757
}


static void
iptablesForwardRejectIn(virFirewallPtr fw,
                        virFirewallLayer layer,
758
                        bool pvt,
759 760
                        const char *iface,
                        int action)
761
{
762 763
    virFirewallAddRule(fw, layer,
                       "--table", "filter",
764 765
                       action == ADD ? "--insert" : "--delete",
                       pvt ? "LIBVIRT_FWI" : "FORWARD",
766
                       "--out-interface", iface,
767 768
                       "--jump", "REJECT",
                       NULL);
769 770
}

771 772 773 774 775 776 777 778 779 780
/**
 * iptablesAddForwardRejectIn:
 * @ctx: pointer to the IP table context
 * @iface: the input interface name
 *
 * Add rules to the IP table context to forbid all traffic from that
 * interface. It forbids forwarding from that interface to the bridge.
 *
 * Returns 0 in case of success or an error code otherwise
 */
781 782 783
void
iptablesAddForwardRejectIn(virFirewallPtr fw,
                           virFirewallLayer layer,
784
                           const char *iface)
785
{
786
    iptablesForwardRejectIn(fw, layer, true, iface, ADD);
787 788
}

789 790 791 792 793 794 795 796 797 798
/**
 * iptablesRemoveForwardRejectIn:
 * @ctx: pointer to the IP table context
 * @iface: the input interface name
 *
 * Remove rules from the IP table context forbidding all traffic from that
 * interface. It allows forwarding from that interface to the bridge.
 *
 * Returns 0 in case of success or an error code otherwise
 */
799 800 801
void
iptablesRemoveForwardRejectIn(virFirewallPtr fw,
                              virFirewallLayer layer,
802
                              const char *iface)
803
{
804
    iptablesForwardRejectIn(fw, layer, deletePrivate, iface, REMOVE);
805 806
}

807 808 809 810

/* Masquerade all traffic coming from the network associated
 * with the bridge
 */
811
static int
812
iptablesForwardMasquerade(virFirewallPtr fw,
813
                          bool pvt,
814
                          virSocketAddr *netaddr,
815
                          unsigned int prefix,
816
                          const char *physdev,
817 818
                          virSocketAddrRangePtr addr,
                          virPortRangePtr port,
819 820
                          const char *protocol,
                          int action)
821
{
822 823 824 825 826
    VIR_AUTOFREE(char *) networkstr = NULL;
    VIR_AUTOFREE(char *) addrStartStr = NULL;
    VIR_AUTOFREE(char *) addrEndStr = NULL;
    VIR_AUTOFREE(char *) portRangeStr = NULL;
    VIR_AUTOFREE(char *) natRangeStr = NULL;
827
    virFirewallRulePtr rule;
828

829
    if (!(networkstr = iptablesFormatNetwork(netaddr, prefix)))
830 831
        return -1;

832
    if (!VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET)) {
833
        /* Higher level code *should* guaranteee it's impossible to get here. */
834 835 836
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Attempted to NAT '%s'. NAT is only supported for IPv4."),
                       networkstr);
837
        return -1;
838 839
    }

840 841
    if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->start, AF_INET)) {
        if (!(addrStartStr = virSocketAddrFormat(&addr->start)))
842
            return -1;
843 844
        if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->end, AF_INET)) {
            if (!(addrEndStr = virSocketAddrFormat(&addr->end)))
845
                return -1;
846
        }
847 848
    }

849 850 851
    if (protocol && protocol[0]) {
        rule = virFirewallAddRule(fw, VIR_FIREWALL_LAYER_IPV4,
                                  "--table", "nat",
852 853
                                  action == ADD ? "--insert" : "--delete",
                                  pvt ? "LIBVIRT_PRT" : "POSTROUTING",
854 855 856 857 858 859 860
                                  "--source", networkstr,
                                  "-p", protocol,
                                  "!", "--destination", networkstr,
                                  NULL);
    } else {
        rule = virFirewallAddRule(fw, VIR_FIREWALL_LAYER_IPV4,
                                  "--table", "nat",
861 862
                                  action == ADD ? "--insert" : "--delete",
                                  pvt ? "LIBVIRT_PRT" : "POSTROUTING",
863 864 865 866
                                  "--source", networkstr,
                                  "!", "--destination", networkstr,
                                  NULL);
    }
867 868

    if (physdev && physdev[0])
869
        virFirewallRuleAddArgList(fw, rule, "--out-interface", physdev, NULL);
870

871
    if (protocol && protocol[0]) {
872 873 874
        if (port->start == 0 && port->end == 0) {
            port->start = 1024;
            port->end = 65535;
875 876
        }

877 878
        if (port->start < port->end && port->end < 65536) {
            if (virAsprintf(&portRangeStr, ":%u-%u",
879
                            port->start, port->end) < 0)
880
                return -1;
881 882 883
        } else {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid port range '%u-%u'."),
884
                           port->start, port->end);
885 886 887
        }
    }

888 889 890
    /* Use --jump SNAT if public addr is specified */
    if (addrStartStr && addrStartStr[0]) {
        int r = 0;
891

892 893
        if (addrEndStr && addrEndStr[0]) {
            r = virAsprintf(&natRangeStr, "%s-%s%s", addrStartStr, addrEndStr,
894
                            portRangeStr ? portRangeStr : "");
895
        } else {
896 897
            r = virAsprintf(&natRangeStr, "%s%s", addrStartStr,
                            portRangeStr ? portRangeStr : "");
898 899
        }

900
        if (r < 0)
901
            return -1;
902

903 904
        virFirewallRuleAddArgList(fw, rule,
                                  "--jump", "SNAT",
905
                                  "--to-source", natRangeStr, NULL);
906 907 908
    } else {
        virFirewallRuleAddArgList(fw, rule,
                                  "--jump", "MASQUERADE", NULL);
909

910 911 912 913
        if (portRangeStr && portRangeStr[0])
            virFirewallRuleAddArgList(fw, rule,
                                      "--to-ports", &portRangeStr[1], NULL);
    }
914

915
    return 0;
916 917
}

918 919 920 921 922
/**
 * iptablesAddForwardMasquerade:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @physdev: the physical input device or NULL
923
 * @protocol: the network protocol or NULL
924
 *
925 926 927 928 929 930
 * Add rules to the IP table context to allow masquerading
 * network @network on @physdev. This allow the bridge to
 * masquerade for that network (on @physdev).
 *
 * Returns 0 in case of success or an error code otherwise
 */
931
int
932 933
iptablesAddForwardMasquerade(virFirewallPtr fw,
                             virSocketAddr *netaddr,
934
                             unsigned int prefix,
935
                             const char *physdev,
936 937
                             virSocketAddrRangePtr addr,
                             virPortRangePtr port,
938
                             const char *protocol)
939
{
940 941
    return iptablesForwardMasquerade(fw, true, netaddr, prefix,
                                     physdev, addr, port, protocol, ADD);
942 943
}

944 945 946 947 948
/**
 * iptablesRemoveForwardMasquerade:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @physdev: the physical input device or NULL
949
 * @protocol: the network protocol or NULL
950
 *
951 952 953 954 955 956
 * Remove rules from the IP table context to stop masquerading
 * network @network on @physdev. This stops the bridge from
 * masquerading for that network (on @physdev).
 *
 * Returns 0 in case of success or an error code otherwise
 */
957
int
958 959
iptablesRemoveForwardMasquerade(virFirewallPtr fw,
                                virSocketAddr *netaddr,
960
                                unsigned int prefix,
961
                                const char *physdev,
962 963
                                virSocketAddrRangePtr addr,
                                virPortRangePtr port,
964
                                const char *protocol)
965
{
966 967
    return iptablesForwardMasquerade(fw, deletePrivate, netaddr, prefix,
                                     physdev, addr, port, protocol, REMOVE);
968
}
969 970


971 972 973 974
/* Don't masquerade traffic coming from the network associated with the bridge
 * if said traffic targets @destaddr.
 */
static int
975
iptablesForwardDontMasquerade(virFirewallPtr fw,
976
                              bool pvt,
977
                              virSocketAddr *netaddr,
978 979 980 981 982
                              unsigned int prefix,
                              const char *physdev,
                              const char *destaddr,
                              int action)
{
983
    VIR_AUTOFREE(char *) networkstr = NULL;
984 985 986 987 988 989 990 991 992

    if (!(networkstr = iptablesFormatNetwork(netaddr, prefix)))
        return -1;

    if (!VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET)) {
        /* Higher level code *should* guaranteee it's impossible to get here. */
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Attempted to NAT '%s'. NAT is only supported for IPv4."),
                       networkstr);
993
        return -1;
994 995 996
    }

    if (physdev && physdev[0])
997 998
        virFirewallAddRule(fw, VIR_FIREWALL_LAYER_IPV4,
                           "--table", "nat",
999 1000
                           action == ADD ? "--insert" : "--delete",
                           pvt ? "LIBVIRT_PRT" : "POSTROUTING",
1001 1002 1003 1004 1005 1006 1007 1008
                           "--out-interface", physdev,
                           "--source", networkstr,
                           "--destination", destaddr,
                           "--jump", "RETURN",
                           NULL);
    else
        virFirewallAddRule(fw, VIR_FIREWALL_LAYER_IPV4,
                           "--table", "nat",
1009 1010
                           action == ADD ? "--insert" : "--delete",
                           pvt ? "LIBVIRT_PRT" : "POSTROUTING",
1011 1012 1013 1014 1015
                           "--source", networkstr,
                           "--destination", destaddr,
                           "--jump", "RETURN",
                           NULL);

1016
    return 0;
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
}

/**
 * iptablesAddDontMasquerade:
 * @netaddr: the source network name
 * @prefix: prefix (# of 1 bits) of netmask to apply to @netaddr
 * @physdev: the physical output device or NULL
 * @destaddr: the destination network not to masquerade for
 *
 * Add rules to the IP table context to avoid masquerading from
 * @netaddr/@prefix to @destaddr on @physdev. @destaddr must be in a format
 * directly consumable by iptables, it must not depend on user input or
 * configuration.
 *
 * Returns 0 in case of success or an error code otherwise.
 */
int
1034 1035
iptablesAddDontMasquerade(virFirewallPtr fw,
                          virSocketAddr *netaddr,
1036 1037 1038 1039
                          unsigned int prefix,
                          const char *physdev,
                          const char *destaddr)
{
1040 1041
    return iptablesForwardDontMasquerade(fw, true, netaddr, prefix,
                                         physdev, destaddr, ADD);
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
}

/**
 * iptablesRemoveDontMasquerade:
 * @netaddr: the source network name
 * @prefix: prefix (# of 1 bits) of netmask to apply to @netaddr
 * @physdev: the physical output device or NULL
 * @destaddr: the destination network not to masquerade for
 *
 * Remove rules from the IP table context that prevent masquerading from
 * @netaddr/@prefix to @destaddr on @physdev. @destaddr must be in a format
 * directly consumable by iptables, it must not depend on user input or
 * configuration.
 *
 * Returns 0 in case of success or an error code otherwise.
 */
int
1059 1060
iptablesRemoveDontMasquerade(virFirewallPtr fw,
                             virSocketAddr *netaddr,
1061 1062 1063 1064
                             unsigned int prefix,
                             const char *physdev,
                             const char *destaddr)
{
1065 1066
    return iptablesForwardDontMasquerade(fw, deletePrivate, netaddr, prefix,
                                         physdev, destaddr, REMOVE);
1067 1068 1069
}


1070 1071
static void
iptablesOutputFixUdpChecksum(virFirewallPtr fw,
1072
                             bool pvt,
1073
                             const char *iface,
1074 1075 1076 1077 1078 1079 1080 1081
                             int port,
                             int action)
{
    char portstr[32];

    snprintf(portstr, sizeof(portstr), "%d", port);
    portstr[sizeof(portstr) - 1] = '\0';

1082 1083
    virFirewallAddRule(fw, VIR_FIREWALL_LAYER_IPV4,
                       "--table", "mangle",
1084 1085
                       action == ADD ? "--insert" : "--delete",
                       pvt ? "LIBVIRT_PRT" : "POSTROUTING",
1086 1087 1088 1089 1090
                       "--out-interface", iface,
                       "--protocol", "udp",
                       "--destination-port", portstr,
                       "--jump", "CHECKSUM", "--checksum-fill",
                       NULL);
1091 1092 1093 1094 1095 1096 1097 1098
}

/**
 * iptablesAddOutputFixUdpChecksum:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the UDP port to match
 *
E
Eric Blake 已提交
1099
 * Add a rule to the mangle table's POSTROUTING chain that fixes up the
1100 1101 1102 1103
 * checksum of packets with the given destination @port.
 * the given @iface interface for TCP packets.
 *
 */
1104 1105 1106
void
iptablesAddOutputFixUdpChecksum(virFirewallPtr fw,
                                const char *iface,
1107 1108
                                int port)
{
1109
    iptablesOutputFixUdpChecksum(fw, true, iface, port, ADD);
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
}

/**
 * iptablesRemoveOutputFixUdpChecksum:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the UDP port of the rule to remove
 *
 * Removes the checksum fixup rule that was previous added with
 * iptablesAddOutputFixUdpChecksum.
 */
1121 1122 1123
void
iptablesRemoveOutputFixUdpChecksum(virFirewallPtr fw,
                                   const char *iface,
1124 1125
                                   int port)
{
1126
    iptablesOutputFixUdpChecksum(fw, deletePrivate, iface, port, REMOVE);
1127
}