viriptables.c 31.3 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 22 23
 *
 * Authors:
 *     Mark McLoughlin <markmc@redhat.com>
 */

24
#include <config.h>
25 26 27 28 29 30 31 32 33 34 35

#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>
E
Eric Blake 已提交
36
#include <sys/wait.h>
37

38
#include "internal.h"
39
#include "viriptables.h"
40
#include "vircommand.h"
41
#include "viralloc.h"
42
#include "virerror.h"
43
#include "virfile.h"
44
#include "virlog.h"
45
#include "virthread.h"
46 47
#include "virstring.h"
#include "virutil.h"
48

49 50
VIR_LOG_INIT("util.iptables");

51
#define VIR_FROM_THIS VIR_FROM_NONE
52

53 54 55 56 57
enum {
    ADD = 0,
    REMOVE
};

58

59 60 61
static void
iptablesInput(virFirewallPtr fw,
              virFirewallLayer layer,
62 63 64 65 66 67 68 69 70 71
              const char *iface,
              int port,
              int action,
              int tcp)
{
    char portstr[32];

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

72 73 74 75 76 77 78 79
    virFirewallAddRule(fw, layer,
                       "--table", "filter",
                       action == ADD ? "--insert" : "--delete", "INPUT",
                       "--in-interface", iface,
                       "--protocol", tcp ? "tcp" : "udp",
                       "--destination-port", portstr,
                       "--jump", "ACCEPT",
                       NULL);
80 81
}

82 83 84
static void
iptablesOutput(virFirewallPtr fw,
               virFirewallLayer layer,
85 86 87 88 89 90 91 92 93 94
               const char *iface,
               int port,
               int action,
               int tcp)
{
    char portstr[32];

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

95 96 97 98 99 100 101 102
    virFirewallAddRule(fw, layer,
                       "--table", "filter",
                       action == ADD ? "--insert" : "--delete", "OUTPUT",
                       "--out-interface", iface,
                       "--protocol", tcp ? "tcp" : "udp",
                       "--destination-port", portstr,
                       "--jump", "ACCEPT",
                       NULL);
103 104
}

105 106 107 108 109 110 111 112 113
/**
 * 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
 */
114 115 116
void
iptablesAddTcpInput(virFirewallPtr fw,
                    virFirewallLayer layer,
117 118 119
                    const char *iface,
                    int port)
{
120
    iptablesInput(fw, layer, iface, port, ADD, 1);
121 122
}

123 124 125 126 127 128
/**
 * iptablesRemoveTcpInput:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the TCP port to remove
 *
R
Richard W.M. Jones 已提交
129
 * Removes an input from the IP table, hence forbidding access to the given
130 131
 * @port on the given @iface interface for TCP packets
 */
132 133 134
void
iptablesRemoveTcpInput(virFirewallPtr fw,
                       virFirewallLayer layer,
135 136 137
                       const char *iface,
                       int port)
{
138
    iptablesInput(fw, layer, iface, port, REMOVE, 1);
139 140
}

141 142 143 144 145 146 147 148 149
/**
 * 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
 */
150 151 152
void
iptablesAddUdpInput(virFirewallPtr fw,
                    virFirewallLayer layer,
153 154 155
                    const char *iface,
                    int port)
{
156
    iptablesInput(fw, layer, iface, port, ADD, 0);
157 158
}

159 160 161 162 163 164
/**
 * iptablesRemoveUdpInput:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the UDP port to remove
 *
R
Richard W.M. Jones 已提交
165
 * Removes an input from the IP table, hence forbidding access to the given
166 167
 * @port on the given @iface interface for UDP packets
 */
168 169 170
void
iptablesRemoveUdpInput(virFirewallPtr fw,
                       virFirewallLayer layer,
171 172 173
                       const char *iface,
                       int port)
{
174
    return iptablesInput(fw, layer, iface, port, REMOVE, 0);
175 176
}

177 178 179 180 181 182 183 184 185
/**
 * 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
 */
186 187 188
void
iptablesAddUdpOutput(virFirewallPtr fw,
                     virFirewallLayer layer,
189 190 191
                     const char *iface,
                     int port)
{
192
    iptablesOutput(fw, layer, iface, port, ADD, 0);
193 194 195 196 197 198 199 200 201 202 203
}

/**
 * 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
 */
204 205 206
void
iptablesRemoveUdpOutput(virFirewallPtr fw,
                        virFirewallLayer layer,
207 208 209
                        const char *iface,
                        int port)
{
210
    iptablesOutput(fw, layer, iface, port, REMOVE, 0);
211 212
}

213

214
static char *iptablesFormatNetwork(virSocketAddr *netaddr,
215
                                   unsigned int prefix)
216 217 218 219 220
{
    virSocketAddr network;
    char *netstr;
    char *ret;

221 222
    if (!(VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET) ||
          VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET6))) {
223 224
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Only IPv4 or IPv6 addresses can be used with iptables"));
225 226 227
        return NULL;
    }

228
    if (virSocketAddrMaskByPrefix(netaddr, prefix, &network) < 0) {
229 230
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Failure to mask address"));
231 232
        return NULL;
    }
233

234
    netstr = virSocketAddrFormat(&network);
235 236 237 238

    if (!netstr)
        return NULL;

239
    ignore_value(virAsprintf(&ret, "%s/%d", netstr, prefix));
240 241 242 243 244 245

    VIR_FREE(netstr);
    return ret;
}


246 247 248
/* Allow all traffic coming from the bridge, with a valid network address
 * to proceed to WAN
 */
249
static int
250 251
iptablesForwardAllowOut(virFirewallPtr fw,
                        virSocketAddr *netaddr,
252
                        unsigned int prefix,
253 254 255
                        const char *iface,
                        const char *physdev,
                        int action)
256
{
257
    char *networkstr;
258 259
    virFirewallLayer layer = VIR_SOCKET_ADDR_FAMILY(netaddr) == AF_INET ?
        VIR_FIREWALL_LAYER_IPV4 : VIR_FIREWALL_LAYER_IPV6;
260

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

264
    if (physdev && physdev[0])
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
        virFirewallAddRule(fw, layer,
                           "--table", "filter",
                           action == ADD ? "--insert" : "--delete", "FORWARD",
                           "--source", networkstr,
                           "--in-interface", iface,
                           "--out-interface", physdev,
                           "--jump", "ACCEPT",
                           NULL);
    else
        virFirewallAddRule(fw, layer,
                           "--table", "filter",
                           action == ADD ? "--insert" : "--delete", "FORWARD",
                           "--source", networkstr,
                           "--in-interface", iface,
                           "--jump", "ACCEPT",
                           NULL);
281

282
    VIR_FREE(networkstr);
283
    return 0;
284 285
}

286 287 288 289 290 291
/**
 * iptablesAddForwardAllowOut:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @iface: the source interface name
 * @physdev: the physical output device
292
 *
293 294 295 296 297 298
 * 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
 */
299
int
300 301
iptablesAddForwardAllowOut(virFirewallPtr fw,
                           virSocketAddr *netaddr,
302
                           unsigned int prefix,
303 304
                           const char *iface,
                           const char *physdev)
305
{
306
    return iptablesForwardAllowOut(fw, netaddr, prefix, iface, physdev, ADD);
307 308
}

309 310 311 312 313 314
/**
 * iptablesRemoveForwardAllowOut:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @iface: the source interface name
 * @physdev: the physical output device
315
 *
316 317 318 319 320 321
 * 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
 */
322
int
323 324
iptablesRemoveForwardAllowOut(virFirewallPtr fw,
                              virSocketAddr *netaddr,
325
                              unsigned int prefix,
326 327
                              const char *iface,
                              const char *physdev)
328
{
329
    return iptablesForwardAllowOut(fw, netaddr, prefix, iface, physdev, REMOVE);
330 331
}

332 333 334 335

/* Allow all traffic destined to the bridge, with a valid network address
 * and associated with an existing connection
 */
336
static int
337 338
iptablesForwardAllowRelatedIn(virFirewallPtr fw,
                              virSocketAddr *netaddr,
339
                              unsigned int prefix,
340 341 342
                              const char *iface,
                              const char *physdev,
                              int action)
343
{
344 345
    virFirewallLayer layer = VIR_SOCKET_ADDR_FAMILY(netaddr) == AF_INET ?
        VIR_FIREWALL_LAYER_IPV4 : VIR_FIREWALL_LAYER_IPV6;
346 347
    char *networkstr;

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

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    if (physdev && physdev[0])
        virFirewallAddRule(fw, layer,
                           "--table", "filter",
                           action == ADD ? "--insert" : "--delete", "FORWARD",
                           "--destination", networkstr,
                           "--in-interface", physdev,
                           "--out-interface", iface,
                           "--match", "conntrack",
                           "--ctstate", "ESTABLISHED,RELATED",
                           "--jump", "ACCEPT",
                           NULL);
    else
        virFirewallAddRule(fw, layer,
                           "--table", "filter",
                           action == ADD ? "--insert" : "--delete", "FORWARD",
                           "--destination", networkstr,
                           "--out-interface", iface,
                           "--match", "conntrack",
                           "--ctstate", "ESTABLISHED,RELATED",
                           "--jump", "ACCEPT",
                           NULL);

373
    VIR_FREE(networkstr);
374
    return 0;
375 376
}

377 378 379 380 381 382 383 384 385 386 387 388 389 390
/**
 * 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
391 392
iptablesAddForwardAllowRelatedIn(virFirewallPtr fw,
                                 virSocketAddr *netaddr,
393
                                 unsigned int prefix,
394 395
                                 const char *iface,
                                 const char *physdev)
396
{
397
    return iptablesForwardAllowRelatedIn(fw, netaddr, prefix, iface, physdev, ADD);
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
}

/**
 * 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
414 415
iptablesRemoveForwardAllowRelatedIn(virFirewallPtr fw,
                                    virSocketAddr *netaddr,
416
                                    unsigned int prefix,
417 418
                                    const char *iface,
                                    const char *physdev)
419
{
420
    return iptablesForwardAllowRelatedIn(fw, netaddr, prefix, iface, physdev, REMOVE);
421 422 423 424 425
}

/* Allow all traffic destined to the bridge, with a valid network address
 */
static int
426 427
iptablesForwardAllowIn(virFirewallPtr fw,
                       virSocketAddr *netaddr,
428
                       unsigned int prefix,
429 430 431 432
                       const char *iface,
                       const char *physdev,
                       int action)
{
433 434
    virFirewallLayer layer = VIR_SOCKET_ADDR_FAMILY(netaddr) == AF_INET ?
        VIR_FIREWALL_LAYER_IPV4 : VIR_FIREWALL_LAYER_IPV6;
435 436
    char *networkstr;

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

440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
    if (physdev && physdev[0])
        virFirewallAddRule(fw, layer,
                           "--table", "filter",
                           action == ADD ? "--insert" : "--delete", "FORWARD",
                           "--destination", networkstr,
                           "--in-interface", physdev,
                           "--out-interface", iface,
                           "--jump", "ACCEPT",
                           NULL);
    else
        virFirewallAddRule(fw, layer,
                           "--table", "filter",
                           action == ADD ? "--insert" : "--delete", "FORWARD",
                           "--destination", networkstr,
                           "--out-interface", iface,
                           "--jump", "ACCEPT",
                           NULL);
457
    VIR_FREE(networkstr);
458
    return 0;
459 460
}

461 462 463 464 465 466
/**
 * 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
467
 *
468 469 470 471 472 473
 * 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
 */
474
int
475 476
iptablesAddForwardAllowIn(virFirewallPtr fw,
                          virSocketAddr *netaddr,
477
                          unsigned int prefix,
478 479 480
                          const char *iface,
                          const char *physdev)
{
481
    return iptablesForwardAllowIn(fw, netaddr, prefix, iface, physdev, ADD);
482 483
}

484 485 486 487 488 489
/**
 * 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
490
 *
491 492 493 494 495 496
 * 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
 */
497
int
498 499
iptablesRemoveForwardAllowIn(virFirewallPtr fw,
                             virSocketAddr *netaddr,
500
                             unsigned int prefix,
501 502 503
                             const char *iface,
                             const char *physdev)
{
504
    return iptablesForwardAllowIn(fw, netaddr, prefix, iface, physdev, REMOVE);
505 506
}

507 508 509 510 511 512 513 514 515 516 517
/**
 * 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
 */
518 519 520
void
iptablesAddForwardAllowCross(virFirewallPtr fw,
                             virFirewallLayer layer,
521 522
                             const char *iface)
{
523 524 525 526 527 528 529
    virFirewallAddRule(fw, layer,
                       "--table", "filter",
                       "--insert", "FORWARD",
                       "--in-interface", iface,
                       "--out-interface", iface,
                       "--jump", "ACCEPT",
                       NULL);
530 531
}

532 533 534 535 536 537 538 539 540 541 542
/**
 * 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
 */
543 544 545
void
iptablesRemoveForwardAllowCross(virFirewallPtr fw,
                                virFirewallLayer layer,
546 547
                                const char *iface)
{
548 549 550 551 552 553 554
    virFirewallAddRule(fw, layer,
                       "--table", "filter",
                       "--delete", "FORWARD",
                       "--in-interface", iface,
                       "--out-interface", iface,
                       "--jump", "ACCEPT",
                       NULL);
555 556
}

557 558 559 560 561 562 563 564 565 566
/**
 * 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
 */
567 568 569
void
iptablesAddForwardRejectOut(virFirewallPtr fw,
                            virFirewallLayer layer,
570 571
                            const char *iface)
{
572 573 574 575 576 577
    virFirewallAddRule(fw, layer,
                       "--table", "filter",
                       "--insert", "FORWARD",
                       "--in-interface", iface,
                       "--jump", "REJECT",
                       NULL);
578 579
}

580 581 582 583 584 585 586 587 588 589
/**
 * 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
 */
590 591 592
void
iptablesRemoveForwardRejectOut(virFirewallPtr fw,
                               virFirewallLayer layer,
593 594
                               const char *iface)
{
595 596 597 598 599 600
    virFirewallAddRule(fw, layer,
                       "--table", "filter",
                       "--delete", "FORWARD",
                       "--in-interface", iface,
                       "--jump", "REJECT",
                       NULL);
601 602 603
}


604 605 606 607 608 609 610 611 612 613
/**
 * 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
 */
614 615 616
void
iptablesAddForwardRejectIn(virFirewallPtr fw,
                           virFirewallLayer layer,
617
                           const char *iface)
618
{
619 620 621 622 623 624
    virFirewallAddRule(fw, layer,
                       "--table", "filter",
                       "--insert", "FORWARD",
                       "--out-interface", iface,
                       "--jump", "REJECT",
                       NULL);
625 626
}

627 628 629 630 631 632 633 634 635 636
/**
 * 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
 */
637 638 639
void
iptablesRemoveForwardRejectIn(virFirewallPtr fw,
                              virFirewallLayer layer,
640
                              const char *iface)
641
{
642 643 644 645 646 647
    virFirewallAddRule(fw, layer,
                       "--table", "filter",
                       "--delete", "FORWARD",
                       "--out-interface", iface,
                       "--jump", "REJECT",
                       NULL);
648 649
}

650 651 652 653

/* Masquerade all traffic coming from the network associated
 * with the bridge
 */
654
static int
655 656
iptablesForwardMasquerade(virFirewallPtr fw,
                          virSocketAddr *netaddr,
657
                          unsigned int prefix,
658
                          const char *physdev,
659 660
                          virSocketAddrRangePtr addr,
                          virPortRangePtr port,
661 662
                          const char *protocol,
                          int action)
663
{
664 665 666 667
    int ret = -1;
    char *networkstr = NULL;
    char *addrStartStr = NULL;
    char *addrEndStr = NULL;
668
    char *portRangeStr = NULL;
669
    char *natRangeStr = NULL;
670
    virFirewallRulePtr rule;
671

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

675
    if (!VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET)) {
676
        /* Higher level code *should* guaranteee it's impossible to get here. */
677 678 679
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Attempted to NAT '%s'. NAT is only supported for IPv4."),
                       networkstr);
680 681 682
        goto cleanup;
    }

683 684
    if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->start, AF_INET)) {
        if (!(addrStartStr = virSocketAddrFormat(&addr->start)))
685
            goto cleanup;
686 687
        if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->end, AF_INET)) {
            if (!(addrEndStr = virSocketAddrFormat(&addr->end)))
688 689
                goto cleanup;
        }
690 691
    }

692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
    if (protocol && protocol[0]) {
        rule = virFirewallAddRule(fw, VIR_FIREWALL_LAYER_IPV4,
                                  "--table", "nat",
                                  action == ADD ? "--insert" : "--delete", "POSTROUTING",
                                  "--source", networkstr,
                                  "-p", protocol,
                                  "!", "--destination", networkstr,
                                  NULL);
    } else {
        rule = virFirewallAddRule(fw, VIR_FIREWALL_LAYER_IPV4,
                                  "--table", "nat",
                                  action == ADD ? "--insert" : "--delete", "POSTROUTING",
                                  "--source", networkstr,
                                  "!", "--destination", networkstr,
                                  NULL);
    }
708 709

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

712
    if (protocol && protocol[0]) {
713 714 715
        if (port->start == 0 && port->end == 0) {
            port->start = 1024;
            port->end = 65535;
716 717
        }

718 719
        if (port->start < port->end && port->end < 65536) {
            if (virAsprintf(&portRangeStr, ":%u-%u",
720
                            port->start, port->end) < 0)
721 722 723 724
                goto cleanup;
        } else {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid port range '%u-%u'."),
725
                           port->start, port->end);
726 727 728
        }
    }

729 730 731
    /* Use --jump SNAT if public addr is specified */
    if (addrStartStr && addrStartStr[0]) {
        int r = 0;
732

733 734
        if (addrEndStr && addrEndStr[0]) {
            r = virAsprintf(&natRangeStr, "%s-%s%s", addrStartStr, addrEndStr,
735
                            portRangeStr ? portRangeStr : "");
736
        } else {
737 738
            r = virAsprintf(&natRangeStr, "%s%s", addrStartStr,
                            portRangeStr ? portRangeStr : "");
739 740
        }

741
        if (r < 0)
742 743
            goto cleanup;

744 745
        virFirewallRuleAddArgList(fw, rule,
                                  "--jump", "SNAT",
746
                                  "--to-source", natRangeStr, NULL);
747 748 749
    } else {
        virFirewallRuleAddArgList(fw, rule,
                                  "--jump", "MASQUERADE", NULL);
750

751 752 753 754
        if (portRangeStr && portRangeStr[0])
            virFirewallRuleAddArgList(fw, rule,
                                      "--to-ports", &portRangeStr[1], NULL);
    }
755

756
    ret = 0;
757
 cleanup:
758
    VIR_FREE(networkstr);
759 760
    VIR_FREE(addrStartStr);
    VIR_FREE(addrEndStr);
761
    VIR_FREE(portRangeStr);
762
    VIR_FREE(natRangeStr);
763
    return ret;
764 765
}

766 767 768 769 770
/**
 * iptablesAddForwardMasquerade:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @physdev: the physical input device or NULL
771
 * @protocol: the network protocol or NULL
772
 *
773 774 775 776 777 778
 * 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
 */
779
int
780 781
iptablesAddForwardMasquerade(virFirewallPtr fw,
                             virSocketAddr *netaddr,
782
                             unsigned int prefix,
783
                             const char *physdev,
784 785
                             virSocketAddrRangePtr addr,
                             virPortRangePtr port,
786
                             const char *protocol)
787
{
788
    return iptablesForwardMasquerade(fw, netaddr, prefix, physdev, addr, port,
789
                                     protocol, ADD);
790 791
}

792 793 794 795 796
/**
 * iptablesRemoveForwardMasquerade:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @physdev: the physical input device or NULL
797
 * @protocol: the network protocol or NULL
798
 *
799 800 801 802 803 804
 * 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
 */
805
int
806 807
iptablesRemoveForwardMasquerade(virFirewallPtr fw,
                                virSocketAddr *netaddr,
808
                                unsigned int prefix,
809
                                const char *physdev,
810 811
                                virSocketAddrRangePtr addr,
                                virPortRangePtr port,
812
                                const char *protocol)
813
{
814
    return iptablesForwardMasquerade(fw, netaddr, prefix, physdev, addr, port,
815
                                     protocol, REMOVE);
816
}
817 818


819 820 821 822
/* Don't masquerade traffic coming from the network associated with the bridge
 * if said traffic targets @destaddr.
 */
static int
823 824
iptablesForwardDontMasquerade(virFirewallPtr fw,
                              virSocketAddr *netaddr,
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
                              unsigned int prefix,
                              const char *physdev,
                              const char *destaddr,
                              int action)
{
    int ret = -1;
    char *networkstr = NULL;

    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);
        goto cleanup;
    }

    if (physdev && physdev[0])
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
        virFirewallAddRule(fw, VIR_FIREWALL_LAYER_IPV4,
                           "--table", "nat",
                           action == ADD ? "--insert" : "--delete", "POSTROUTING",
                           "--out-interface", physdev,
                           "--source", networkstr,
                           "--destination", destaddr,
                           "--jump", "RETURN",
                           NULL);
    else
        virFirewallAddRule(fw, VIR_FIREWALL_LAYER_IPV4,
                           "--table", "nat",
                           action == ADD ? "--insert" : "--delete", "POSTROUTING",
                           "--source", networkstr,
                           "--destination", destaddr,
                           "--jump", "RETURN",
                           NULL);

    ret = 0;
863
 cleanup:
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
    VIR_FREE(networkstr);
    return ret;
}

/**
 * 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
883 884
iptablesAddDontMasquerade(virFirewallPtr fw,
                          virSocketAddr *netaddr,
885 886 887 888
                          unsigned int prefix,
                          const char *physdev,
                          const char *destaddr)
{
889
    return iptablesForwardDontMasquerade(fw, netaddr, prefix, physdev, destaddr,
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
                                         ADD);
}

/**
 * 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
908 909
iptablesRemoveDontMasquerade(virFirewallPtr fw,
                             virSocketAddr *netaddr,
910 911 912 913
                             unsigned int prefix,
                             const char *physdev,
                             const char *destaddr)
{
914
    return iptablesForwardDontMasquerade(fw, netaddr, prefix, physdev, destaddr,
915 916 917 918
                                         REMOVE);
}


919 920 921
static void
iptablesOutputFixUdpChecksum(virFirewallPtr fw,
                             const char *iface,
922 923 924 925 926 927 928 929
                             int port,
                             int action)
{
    char portstr[32];

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

930 931 932 933 934 935 936 937
    virFirewallAddRule(fw, VIR_FIREWALL_LAYER_IPV4,
                       "--table", "mangle",
                       action == ADD ? "--insert" : "--delete", "POSTROUTING",
                       "--out-interface", iface,
                       "--protocol", "udp",
                       "--destination-port", portstr,
                       "--jump", "CHECKSUM", "--checksum-fill",
                       NULL);
938 939 940 941 942 943 944 945
}

/**
 * iptablesAddOutputFixUdpChecksum:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the UDP port to match
 *
E
Eric Blake 已提交
946
 * Add a rule to the mangle table's POSTROUTING chain that fixes up the
947 948 949 950
 * checksum of packets with the given destination @port.
 * the given @iface interface for TCP packets.
 *
 */
951 952 953
void
iptablesAddOutputFixUdpChecksum(virFirewallPtr fw,
                                const char *iface,
954 955
                                int port)
{
956
    iptablesOutputFixUdpChecksum(fw, iface, port, ADD);
957 958 959 960 961 962 963 964 965 966 967
}

/**
 * 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.
 */
968 969 970
void
iptablesRemoveOutputFixUdpChecksum(virFirewallPtr fw,
                                   const char *iface,
971 972
                                   int port)
{
973
    iptablesOutputFixUdpChecksum(fw, iface, port, REMOVE);
974
}