viriptables.c 31.4 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

#ifdef HAVE_PATHS_H
39
# include <paths.h>
40
#endif
41

42
#include "internal.h"
43
#include "viriptables.h"
44
#include "vircommand.h"
45
#include "viralloc.h"
46
#include "virerror.h"
47
#include "virfile.h"
48
#include "virlog.h"
49
#include "virthread.h"
50 51
#include "virstring.h"
#include "virutil.h"
52

53 54
VIR_LOG_INIT("util.iptables");

55 56
bool iptables_supports_xlock = false;

57
#define VIR_FROM_THIS VIR_FROM_NONE
58

59 60 61 62 63
enum {
    ADD = 0,
    REMOVE
};

64

65 66 67
static void
iptablesInput(virFirewallPtr fw,
              virFirewallLayer layer,
68 69 70 71 72 73 74 75 76 77
              const char *iface,
              int port,
              int action,
              int tcp)
{
    char portstr[32];

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

78 79 80 81 82 83 84 85
    virFirewallAddRule(fw, layer,
                       "--table", "filter",
                       action == ADD ? "--insert" : "--delete", "INPUT",
                       "--in-interface", iface,
                       "--protocol", tcp ? "tcp" : "udp",
                       "--destination-port", portstr,
                       "--jump", "ACCEPT",
                       NULL);
86 87
}

88 89 90
static void
iptablesOutput(virFirewallPtr fw,
               virFirewallLayer layer,
91 92 93 94 95 96 97 98 99 100
               const char *iface,
               int port,
               int action,
               int tcp)
{
    char portstr[32];

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

101 102 103 104 105 106 107 108
    virFirewallAddRule(fw, layer,
                       "--table", "filter",
                       action == ADD ? "--insert" : "--delete", "OUTPUT",
                       "--out-interface", iface,
                       "--protocol", tcp ? "tcp" : "udp",
                       "--destination-port", portstr,
                       "--jump", "ACCEPT",
                       NULL);
109 110
}

111 112 113 114 115 116 117 118 119
/**
 * 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
 */
120 121 122
void
iptablesAddTcpInput(virFirewallPtr fw,
                    virFirewallLayer layer,
123 124 125
                    const char *iface,
                    int port)
{
126
    iptablesInput(fw, layer, iface, port, ADD, 1);
127 128
}

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

147 148 149 150 151 152 153 154 155
/**
 * 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
 */
156 157 158
void
iptablesAddUdpInput(virFirewallPtr fw,
                    virFirewallLayer layer,
159 160 161
                    const char *iface,
                    int port)
{
162
    iptablesInput(fw, layer, iface, port, ADD, 0);
163 164
}

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

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

/**
 * 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
 */
210 211 212
void
iptablesRemoveUdpOutput(virFirewallPtr fw,
                        virFirewallLayer layer,
213 214 215
                        const char *iface,
                        int port)
{
216
    iptablesOutput(fw, layer, iface, port, REMOVE, 0);
217 218
}

219

220
static char *iptablesFormatNetwork(virSocketAddr *netaddr,
221
                                   unsigned int prefix)
222 223 224 225 226
{
    virSocketAddr network;
    char *netstr;
    char *ret;

227 228
    if (!(VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET) ||
          VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET6))) {
229 230
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Only IPv4 or IPv6 addresses can be used with iptables"));
231 232 233
        return NULL;
    }

234
    if (virSocketAddrMaskByPrefix(netaddr, prefix, &network) < 0) {
235 236
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Failure to mask address"));
237 238
        return NULL;
    }
239

240
    netstr = virSocketAddrFormat(&network);
241 242 243 244

    if (!netstr)
        return NULL;

245
    ignore_value(virAsprintf(&ret, "%s/%d", netstr, prefix));
246 247 248 249 250 251

    VIR_FREE(netstr);
    return ret;
}


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

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

270
    if (physdev && physdev[0])
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
        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);
287

288
    VIR_FREE(networkstr);
289
    return 0;
290 291
}

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

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

338 339 340 341

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

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

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    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);

379
    VIR_FREE(networkstr);
380
    return 0;
381 382
}

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

/**
 * 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
420 421
iptablesRemoveForwardAllowRelatedIn(virFirewallPtr fw,
                                    virSocketAddr *netaddr,
422
                                    unsigned int prefix,
423 424
                                    const char *iface,
                                    const char *physdev)
425
{
426
    return iptablesForwardAllowRelatedIn(fw, netaddr, prefix, iface, physdev, REMOVE);
427 428 429 430 431
}

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

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

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    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);
463
    VIR_FREE(networkstr);
464
    return 0;
465 466
}

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

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

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

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

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

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


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

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

656 657 658 659

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

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

681
    if (!VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET)) {
682
        /* Higher level code *should* guaranteee it's impossible to get here. */
683 684 685
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Attempted to NAT '%s'. NAT is only supported for IPv4."),
                       networkstr);
686 687 688
        goto cleanup;
    }

689 690
    if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->start, AF_INET)) {
        if (!(addrStartStr = virSocketAddrFormat(&addr->start)))
691
            goto cleanup;
692 693
        if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->end, AF_INET)) {
            if (!(addrEndStr = virSocketAddrFormat(&addr->end)))
694 695
                goto cleanup;
        }
696 697
    }

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
    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);
    }
714 715

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

718
    if (protocol && protocol[0]) {
719 720 721
        if (port->start == 0 && port->end == 0) {
            port->start = 1024;
            port->end = 65535;
722 723
        }

724 725
        if (port->start < port->end && port->end < 65536) {
            if (virAsprintf(&portRangeStr, ":%u-%u",
726
                            port->start, port->end) < 0)
727 728 729 730
                goto cleanup;
        } else {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid port range '%u-%u'."),
731
                           port->start, port->end);
732 733 734
        }
    }

735 736 737
    /* Use --jump SNAT if public addr is specified */
    if (addrStartStr && addrStartStr[0]) {
        int r = 0;
738

739 740
        if (addrEndStr && addrEndStr[0]) {
            r = virAsprintf(&natRangeStr, "%s-%s%s", addrStartStr, addrEndStr,
741
                            portRangeStr ? portRangeStr : "");
742
        } else {
743 744
            r = virAsprintf(&natRangeStr, "%s%s", addrStartStr,
                            portRangeStr ? portRangeStr : "");
745 746
        }

747
        if (r < 0)
748 749
            goto cleanup;

750 751
        virFirewallRuleAddArgList(fw, rule,
                                  "--jump", "SNAT",
752
                                  "--to-source", natRangeStr, NULL);
753 754 755
    } else {
        virFirewallRuleAddArgList(fw, rule,
                                  "--jump", "MASQUERADE", NULL);
756

757 758 759 760
        if (portRangeStr && portRangeStr[0])
            virFirewallRuleAddArgList(fw, rule,
                                      "--to-ports", &portRangeStr[1], NULL);
    }
761

762
    ret = 0;
763
 cleanup:
764
    VIR_FREE(networkstr);
765 766
    VIR_FREE(addrStartStr);
    VIR_FREE(addrEndStr);
767
    VIR_FREE(portRangeStr);
768
    VIR_FREE(natRangeStr);
769
    return ret;
770 771
}

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

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


825 826 827 828
/* Don't masquerade traffic coming from the network associated with the bridge
 * if said traffic targets @destaddr.
 */
static int
829 830
iptablesForwardDontMasquerade(virFirewallPtr fw,
                              virSocketAddr *netaddr,
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
                              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])
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
        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;
869
 cleanup:
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
    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
889 890
iptablesAddDontMasquerade(virFirewallPtr fw,
                          virSocketAddr *netaddr,
891 892 893 894
                          unsigned int prefix,
                          const char *physdev,
                          const char *destaddr)
{
895
    return iptablesForwardDontMasquerade(fw, netaddr, prefix, physdev, destaddr,
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
                                         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
914 915
iptablesRemoveDontMasquerade(virFirewallPtr fw,
                             virSocketAddr *netaddr,
916 917 918 919
                             unsigned int prefix,
                             const char *physdev,
                             const char *destaddr)
{
920
    return iptablesForwardDontMasquerade(fw, netaddr, prefix, physdev, destaddr,
921 922 923 924
                                         REMOVE);
}


925 926 927
static void
iptablesOutputFixUdpChecksum(virFirewallPtr fw,
                             const char *iface,
928 929 930 931 932 933 934 935
                             int port,
                             int action)
{
    char portstr[32];

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

936 937 938 939 940 941 942 943
    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);
944 945 946 947 948 949 950 951
}

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

/**
 * 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.
 */
974 975 976
void
iptablesRemoveOutputFixUdpChecksum(virFirewallPtr fw,
                                   const char *iface,
977 978
                                   int port)
{
979
    iptablesOutputFixUdpChecksum(fw, iface, port, REMOVE);
980
}