viriptables.c 32.1 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
bool iptables_supports_xlock = false;

55 56
#if HAVE_FIREWALLD
static char *firewall_cmd_path = NULL;
57
#endif
58 59 60 61

static int
virIpTablesOnceInit(void)
{
62 63 64
    virCommandPtr cmd;

#if HAVE_FIREWALLD
65 66
    firewall_cmd_path = virFindFileInPath("firewall-cmd");
    if (!firewall_cmd_path) {
67
        VIR_INFO("firewall-cmd not found on system. "
68 69
                 "firewalld support disabled for iptables.");
    } else {
70
        cmd = virCommandNew(firewall_cmd_path);
71 72

        virCommandAddArgList(cmd, "--state", NULL);
73
        if (virCommandRun(cmd, NULL) < 0) {
74
            VIR_INFO("firewall-cmd found but disabled for iptables");
75 76 77
            VIR_FREE(firewall_cmd_path);
            firewall_cmd_path = NULL;
        } else {
78
            VIR_INFO("using firewalld for iptables commands");
79 80 81
        }
        virCommandFree(cmd);
    }
82 83 84 85 86 87 88 89

    if (firewall_cmd_path)
        return 0;

#endif

    cmd = virCommandNew(IPTABLES_PATH);
    virCommandAddArgList(cmd, "-w", "-L", "-n", NULL);
90
    if (virCommandRun(cmd, NULL) < 0) {
91 92 93 94 95 96
        VIR_INFO("xtables locking not supported by your iptables");
    } else {
        VIR_INFO("using xtables locking for iptables");
        iptables_supports_xlock = true;
    }
    virCommandFree(cmd);
97 98 99 100 101
    return 0;
}

VIR_ONCE_GLOBAL_INIT(virIpTables)

102
#define VIR_FROM_THIS VIR_FROM_NONE
103

104 105 106 107 108
enum {
    ADD = 0,
    REMOVE
};

109
static virCommandPtr
R
Roman Bogorodskiy 已提交
110
iptablesCommandNew(const char *table, const char *chain, int family, int action)
111
{
112 113
    virCommandPtr cmd = NULL;
    virIpTablesInitialize();
114
#if HAVE_FIREWALLD
115 116 117 118 119 120 121 122 123
    if (firewall_cmd_path) {
        cmd = virCommandNew(firewall_cmd_path);
        virCommandAddArgList(cmd, "--direct", "--passthrough",
                             (family == AF_INET6) ? "ipv6" : "ipv4", NULL);
    }
#endif

    if (cmd == NULL) {
        cmd = virCommandNew((family == AF_INET6)
124
                        ? IP6TABLES_PATH : IPTABLES_PATH);
125 126 127

        if (iptables_supports_xlock)
            virCommandAddArgList(cmd, "-w", NULL);
128
    }
129

R
Roman Bogorodskiy 已提交
130
    virCommandAddArgList(cmd, "--table", table,
131
                         action == ADD ? "--insert" : "--delete",
R
Roman Bogorodskiy 已提交
132
                         chain, NULL);
133 134 135 136 137 138 139 140 141 142 143 144 145
    return cmd;
}

static int
iptablesCommandRunAndFree(virCommandPtr cmd)
{
    int ret;
    ret = virCommandRun(cmd, NULL);
    virCommandFree(cmd);
    return ret;
}

static int ATTRIBUTE_SENTINEL
R
Roman Bogorodskiy 已提交
146
iptablesAddRemoveRule(const char *table, const char *chain, int family, int action,
147 148 149 150 151 152
                      const char *arg, ...)
{
    va_list args;
    virCommandPtr cmd = NULL;
    const char *s;

R
Roman Bogorodskiy 已提交
153
    cmd = iptablesCommandNew(table, chain, family, action);
154
    virCommandAddArg(cmd, arg);
155 156

    va_start(args, arg);
157 158
    while ((s = va_arg(args, const char *)))
        virCommandAddArg(cmd, s);
159 160
    va_end(args);

161
    return iptablesCommandRunAndFree(cmd);
162 163 164
}

static int
R
Roman Bogorodskiy 已提交
165
iptablesInput(int family,
166 167 168 169 170 171 172 173 174 175
              const char *iface,
              int port,
              int action,
              int tcp)
{
    char portstr[32];

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

R
Roman Bogorodskiy 已提交
176
    return iptablesAddRemoveRule("filter", "INPUT",
177
                                 family,
178 179 180 181 182 183
                                 action,
                                 "--in-interface", iface,
                                 "--protocol", tcp ? "tcp" : "udp",
                                 "--destination-port", portstr,
                                 "--jump", "ACCEPT",
                                 NULL);
184 185
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
static int
iptablesOutput(int family,
               const char *iface,
               int port,
               int action,
               int tcp)
{
    char portstr[32];

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

    return iptablesAddRemoveRule("filter", "OUTPUT",
                                 family,
                                 action,
                                 "--out-interface", iface,
                                 "--protocol", tcp ? "tcp" : "udp",
                                 "--destination-port", portstr,
                                 "--jump", "ACCEPT",
                                 NULL);
}

208 209 210 211 212 213 214 215 216 217 218 219
/**
 * 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
 *
 * Returns 0 in case of success or an error code in case of error
 */

220
int
R
Roman Bogorodskiy 已提交
221
iptablesAddTcpInput(int family,
222 223 224
                    const char *iface,
                    int port)
{
R
Roman Bogorodskiy 已提交
225
    return iptablesInput(family, iface, port, ADD, 1);
226 227
}

228 229 230 231 232 233
/**
 * iptablesRemoveTcpInput:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the TCP port to remove
 *
R
Richard W.M. Jones 已提交
234
 * Removes an input from the IP table, hence forbidding access to the given
235 236 237 238
 * @port on the given @iface interface for TCP packets
 *
 * Returns 0 in case of success or an error code in case of error
 */
239
int
R
Roman Bogorodskiy 已提交
240
iptablesRemoveTcpInput(int family,
241 242 243
                       const char *iface,
                       int port)
{
R
Roman Bogorodskiy 已提交
244
    return iptablesInput(family, iface, port, REMOVE, 1);
245 246
}

247 248 249 250 251 252 253 254 255 256 257 258
/**
 * 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
 *
 * Returns 0 in case of success or an error code in case of error
 */

259
int
R
Roman Bogorodskiy 已提交
260
iptablesAddUdpInput(int family,
261 262 263
                    const char *iface,
                    int port)
{
R
Roman Bogorodskiy 已提交
264
    return iptablesInput(family, iface, port, ADD, 0);
265 266
}

267 268 269 270 271 272
/**
 * iptablesRemoveUdpInput:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the UDP port to remove
 *
R
Richard W.M. Jones 已提交
273
 * Removes an input from the IP table, hence forbidding access to the given
274 275 276 277
 * @port on the given @iface interface for UDP packets
 *
 * Returns 0 in case of success or an error code in case of error
 */
278
int
R
Roman Bogorodskiy 已提交
279
iptablesRemoveUdpInput(int family,
280 281 282
                       const char *iface,
                       int port)
{
R
Roman Bogorodskiy 已提交
283
    return iptablesInput(family, iface, port, REMOVE, 0);
284 285
}

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
/**
 * 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
 *
 * Returns 0 in case of success or an error code in case of error
 */

int
iptablesAddUdpOutput(int family,
                     const char *iface,
                     int port)
{
    return iptablesOutput(family, iface, port, ADD, 0);
}

/**
 * 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
 *
 * Returns 0 in case of success or an error code in case of error
 */
int
iptablesRemoveUdpOutput(int family,
                        const char *iface,
                        int port)
{
    return iptablesOutput(family, iface, port, REMOVE, 0);
}

325

326
static char *iptablesFormatNetwork(virSocketAddr *netaddr,
327
                                   unsigned int prefix)
328 329 330 331 332
{
    virSocketAddr network;
    char *netstr;
    char *ret;

333 334
    if (!(VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET) ||
          VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET6))) {
335 336
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Only IPv4 or IPv6 addresses can be used with iptables"));
337 338 339
        return NULL;
    }

340
    if (virSocketAddrMaskByPrefix(netaddr, prefix, &network) < 0) {
341 342
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Failure to mask address"));
343 344
        return NULL;
    }
345

346
    netstr = virSocketAddrFormat(&network);
347 348 349 350

    if (!netstr)
        return NULL;

351
    ignore_value(virAsprintf(&ret, "%s/%d", netstr, prefix));
352 353 354 355 356 357

    VIR_FREE(netstr);
    return ret;
}


358 359 360
/* Allow all traffic coming from the bridge, with a valid network address
 * to proceed to WAN
 */
361
static int
R
Roman Bogorodskiy 已提交
362
iptablesForwardAllowOut(virSocketAddr *netaddr,
363
                        unsigned int prefix,
364 365 366
                        const char *iface,
                        const char *physdev,
                        int action)
367
{
368 369
    int ret;
    char *networkstr;
370
    virCommandPtr cmd = NULL;
371

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

R
Roman Bogorodskiy 已提交
375
    cmd = iptablesCommandNew("filter", "FORWARD",
376 377 378 379 380 381 382 383 384 385 386 387
                             VIR_SOCKET_ADDR_FAMILY(netaddr),
                             action);
    virCommandAddArgList(cmd,
                         "--source", networkstr,
                         "--in-interface", iface, NULL);

    if (physdev && physdev[0])
        virCommandAddArgList(cmd, "--out-interface", physdev, NULL);

    virCommandAddArgList(cmd, "--jump", "ACCEPT", NULL);

    ret = iptablesCommandRunAndFree(cmd);
388 389
    VIR_FREE(networkstr);
    return ret;
390 391
}

392 393 394 395 396 397
/**
 * iptablesAddForwardAllowOut:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @iface: the source interface name
 * @physdev: the physical output device
398
 *
399 400 401 402 403 404
 * 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
 */
405
int
R
Roman Bogorodskiy 已提交
406
iptablesAddForwardAllowOut(virSocketAddr *netaddr,
407
                           unsigned int prefix,
408 409
                           const char *iface,
                           const char *physdev)
410
{
R
Roman Bogorodskiy 已提交
411
    return iptablesForwardAllowOut(netaddr, prefix, iface, physdev, ADD);
412 413
}

414 415 416 417 418 419
/**
 * iptablesRemoveForwardAllowOut:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @iface: the source interface name
 * @physdev: the physical output device
420
 *
421 422 423 424 425 426
 * 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
 */
427
int
R
Roman Bogorodskiy 已提交
428
iptablesRemoveForwardAllowOut(virSocketAddr *netaddr,
429
                              unsigned int prefix,
430 431
                              const char *iface,
                              const char *physdev)
432
{
R
Roman Bogorodskiy 已提交
433
    return iptablesForwardAllowOut(netaddr, prefix, iface, physdev, REMOVE);
434 435
}

436 437 438 439

/* Allow all traffic destined to the bridge, with a valid network address
 * and associated with an existing connection
 */
440
static int
R
Roman Bogorodskiy 已提交
441
iptablesForwardAllowRelatedIn(virSocketAddr *netaddr,
442
                              unsigned int prefix,
443 444 445
                              const char *iface,
                              const char *physdev,
                              int action)
446
{
447 448 449
    int ret;
    char *networkstr;

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

453
    if (physdev && physdev[0]) {
R
Roman Bogorodskiy 已提交
454
        ret = iptablesAddRemoveRule("filter", "FORWARD",
455
                                    VIR_SOCKET_ADDR_FAMILY(netaddr),
456 457 458 459
                                    action,
                                    "--destination", networkstr,
                                    "--in-interface", physdev,
                                    "--out-interface", iface,
S
Stefan Seyfried 已提交
460 461
                                    "--match", "conntrack",
                                    "--ctstate", "ESTABLISHED,RELATED",
462 463
                                    "--jump", "ACCEPT",
                                    NULL);
464
    } else {
R
Roman Bogorodskiy 已提交
465
        ret = iptablesAddRemoveRule("filter", "FORWARD",
466
                                    VIR_SOCKET_ADDR_FAMILY(netaddr),
467 468 469
                                    action,
                                    "--destination", networkstr,
                                    "--out-interface", iface,
S
Stefan Seyfried 已提交
470 471
                                    "--match", "conntrack",
                                    "--ctstate", "ESTABLISHED,RELATED",
472 473
                                    "--jump", "ACCEPT",
                                    NULL);
474
    }
475 476
    VIR_FREE(networkstr);
    return ret;
477 478
}

479 480 481 482 483 484 485 486 487 488 489 490 491 492
/**
 * 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
R
Roman Bogorodskiy 已提交
493
iptablesAddForwardAllowRelatedIn(virSocketAddr *netaddr,
494
                                 unsigned int prefix,
495 496
                                 const char *iface,
                                 const char *physdev)
497
{
R
Roman Bogorodskiy 已提交
498
    return iptablesForwardAllowRelatedIn(netaddr, prefix, iface, physdev, ADD);
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
}

/**
 * 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
R
Roman Bogorodskiy 已提交
515
iptablesRemoveForwardAllowRelatedIn(virSocketAddr *netaddr,
516
                                    unsigned int prefix,
517 518
                                    const char *iface,
                                    const char *physdev)
519
{
R
Roman Bogorodskiy 已提交
520
    return iptablesForwardAllowRelatedIn(netaddr, prefix, iface, physdev, REMOVE);
521 522 523 524 525
}

/* Allow all traffic destined to the bridge, with a valid network address
 */
static int
R
Roman Bogorodskiy 已提交
526
iptablesForwardAllowIn(virSocketAddr *netaddr,
527
                       unsigned int prefix,
528 529 530 531
                       const char *iface,
                       const char *physdev,
                       int action)
{
532 533 534
    int ret;
    char *networkstr;

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

538
    if (physdev && physdev[0]) {
R
Roman Bogorodskiy 已提交
539
        ret = iptablesAddRemoveRule("filter", "FORWARD",
540
                                    VIR_SOCKET_ADDR_FAMILY(netaddr),
541 542 543 544 545 546
                                    action,
                                    "--destination", networkstr,
                                    "--in-interface", physdev,
                                    "--out-interface", iface,
                                    "--jump", "ACCEPT",
                                    NULL);
547
    } else {
R
Roman Bogorodskiy 已提交
548
        ret = iptablesAddRemoveRule("filter", "FORWARD",
549
                                    VIR_SOCKET_ADDR_FAMILY(netaddr),
550 551 552 553 554
                                    action,
                                    "--destination", networkstr,
                                    "--out-interface", iface,
                                    "--jump", "ACCEPT",
                                    NULL);
555
    }
556 557
    VIR_FREE(networkstr);
    return ret;
558 559
}

560 561 562 563 564 565
/**
 * 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
566
 *
567 568 569 570 571 572
 * 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
 */
573
int
R
Roman Bogorodskiy 已提交
574
iptablesAddForwardAllowIn(virSocketAddr *netaddr,
575
                          unsigned int prefix,
576 577 578
                          const char *iface,
                          const char *physdev)
{
R
Roman Bogorodskiy 已提交
579
    return iptablesForwardAllowIn(netaddr, prefix, iface, physdev, ADD);
580 581
}

582 583 584 585 586 587
/**
 * 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
588
 *
589 590 591 592 593 594
 * 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
 */
595
int
R
Roman Bogorodskiy 已提交
596
iptablesRemoveForwardAllowIn(virSocketAddr *netaddr,
597
                             unsigned int prefix,
598 599 600
                             const char *iface,
                             const char *physdev)
{
R
Roman Bogorodskiy 已提交
601
    return iptablesForwardAllowIn(netaddr, prefix, iface, physdev, REMOVE);
602 603 604 605 606 607 608
}


/* Allow all traffic between guests on the same bridge,
 * with a valid network address
 */
static int
R
Roman Bogorodskiy 已提交
609
iptablesForwardAllowCross(int family,
610 611 612
                          const char *iface,
                          int action)
{
R
Roman Bogorodskiy 已提交
613
    return iptablesAddRemoveRule("filter", "FORWARD",
614
                                 family,
615 616 617 618 619 620 621
                                 action,
                                 "--in-interface", iface,
                                 "--out-interface", iface,
                                 "--jump", "ACCEPT",
                                 NULL);
}

622 623 624 625 626 627 628 629 630 631 632
/**
 * 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
 */
633
int
R
Roman Bogorodskiy 已提交
634
iptablesAddForwardAllowCross(int family,
635 636
                             const char *iface)
{
R
Roman Bogorodskiy 已提交
637
    return iptablesForwardAllowCross(family, iface, ADD);
638 639
}

640 641 642 643 644 645 646 647 648 649 650
/**
 * 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
 */
651
int
R
Roman Bogorodskiy 已提交
652
iptablesRemoveForwardAllowCross(int family,
653 654
                                const char *iface)
{
R
Roman Bogorodskiy 已提交
655
    return iptablesForwardAllowCross(family, iface, REMOVE);
656 657 658 659 660 661 662
}


/* Drop all traffic trying to forward from the bridge.
 * ie the bridge is the in interface
 */
static int
R
Roman Bogorodskiy 已提交
663
iptablesForwardRejectOut(int family,
664 665 666
                         const char *iface,
                         int action)
{
R
Roman Bogorodskiy 已提交
667
    return iptablesAddRemoveRule("filter", "FORWARD",
668 669 670 671 672
                                 family,
                                 action,
                                 "--in-interface", iface,
                                 "--jump", "REJECT",
                                 NULL);
673 674
}

675 676 677 678 679 680 681 682 683 684
/**
 * 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
 */
685
int
R
Roman Bogorodskiy 已提交
686
iptablesAddForwardRejectOut(int family,
687 688
                            const char *iface)
{
R
Roman Bogorodskiy 已提交
689
    return iptablesForwardRejectOut(family, iface, ADD);
690 691
}

692 693 694 695 696 697 698 699 700 701
/**
 * 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
 */
702
int
R
Roman Bogorodskiy 已提交
703
iptablesRemoveForwardRejectOut(int family,
704 705
                               const char *iface)
{
R
Roman Bogorodskiy 已提交
706
    return iptablesForwardRejectOut(family, iface, REMOVE);
707 708 709 710 711 712 713 714 715
}




/* Drop all traffic trying to forward to the bridge.
 * ie the bridge is the out interface
 */
static int
R
Roman Bogorodskiy 已提交
716
iptablesForwardRejectIn(int family,
717
                        const char *iface,
718 719
                        int action)
{
R
Roman Bogorodskiy 已提交
720
    return iptablesAddRemoveRule("filter", "FORWARD",
721
                                 family,
722 723 724 725 726 727
                                 action,
                                 "--out-interface", iface,
                                 "--jump", "REJECT",
                                 NULL);
}

728 729 730 731 732 733 734 735 736 737
/**
 * 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
 */
738
int
R
Roman Bogorodskiy 已提交
739
iptablesAddForwardRejectIn(int family,
740
                           const char *iface)
741
{
R
Roman Bogorodskiy 已提交
742
    return iptablesForwardRejectIn(family, iface, ADD);
743 744
}

745 746 747 748 749 750 751 752 753 754
/**
 * 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
 */
755
int
R
Roman Bogorodskiy 已提交
756
iptablesRemoveForwardRejectIn(int family,
757
                              const char *iface)
758
{
R
Roman Bogorodskiy 已提交
759
    return iptablesForwardRejectIn(family, iface, REMOVE);
760 761
}

762 763 764 765

/* Masquerade all traffic coming from the network associated
 * with the bridge
 */
766
static int
R
Roman Bogorodskiy 已提交
767
iptablesForwardMasquerade(virSocketAddr *netaddr,
768
                          unsigned int prefix,
769
                          const char *physdev,
770 771
                          virSocketAddrRangePtr addr,
                          virPortRangePtr port,
772 773
                          const char *protocol,
                          int action)
774
{
775 776 777 778
    int ret = -1;
    char *networkstr = NULL;
    char *addrStartStr = NULL;
    char *addrEndStr = NULL;
779
    char *portRangeStr = NULL;
780
    char *natRangeStr = NULL;
781
    virCommandPtr cmd = NULL;
782

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

786
    if (!VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET)) {
787
        /* Higher level code *should* guaranteee it's impossible to get here. */
788 789 790
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Attempted to NAT '%s'. NAT is only supported for IPv4."),
                       networkstr);
791 792 793
        goto cleanup;
    }

794 795
    if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->start, AF_INET)) {
        if (!(addrStartStr = virSocketAddrFormat(&addr->start)))
796
            goto cleanup;
797 798
        if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->end, AF_INET)) {
            if (!(addrEndStr = virSocketAddrFormat(&addr->end)))
799 800
                goto cleanup;
        }
801 802
    }

R
Roman Bogorodskiy 已提交
803
    cmd = iptablesCommandNew("nat", "POSTROUTING", AF_INET, action);
804 805 806 807 808 809 810 811 812 813
    virCommandAddArgList(cmd, "--source", networkstr, NULL);

    if (protocol && protocol[0])
        virCommandAddArgList(cmd, "-p", protocol, NULL);

    virCommandAddArgList(cmd, "!", "--destination", networkstr, NULL);

    if (physdev && physdev[0])
        virCommandAddArgList(cmd, "--out-interface", physdev, NULL);

814
    if (protocol && protocol[0]) {
815 816 817
        if (port->start == 0 && port->end == 0) {
            port->start = 1024;
            port->end = 65535;
818 819
        }

820 821
        if (port->start < port->end && port->end < 65536) {
            if (virAsprintf(&portRangeStr, ":%u-%u",
822
                            port->start, port->end) < 0)
823 824 825 826
                goto cleanup;
        } else {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid port range '%u-%u'."),
827
                           port->start, port->end);
828 829 830
        }
    }

831 832 833
    /* Use --jump SNAT if public addr is specified */
    if (addrStartStr && addrStartStr[0]) {
        int r = 0;
834

835 836
        if (addrEndStr && addrEndStr[0]) {
            r = virAsprintf(&natRangeStr, "%s-%s%s", addrStartStr, addrEndStr,
837
                            portRangeStr ? portRangeStr : "");
838
        } else {
839 840
            r = virAsprintf(&natRangeStr, "%s%s", addrStartStr,
                            portRangeStr ? portRangeStr : "");
841 842
        }

843
        if (r < 0)
844 845 846 847 848 849 850
            goto cleanup;

        virCommandAddArgList(cmd, "--jump", "SNAT",
                                  "--to-source", natRangeStr, NULL);
     } else {
         virCommandAddArgList(cmd, "--jump", "MASQUERADE", NULL);

851 852
         if (portRangeStr && portRangeStr[0])
             virCommandAddArgList(cmd, "--to-ports", &portRangeStr[1], NULL);
853 854 855 856 857
     }

    ret = virCommandRun(cmd, NULL);
cleanup:
    virCommandFree(cmd);
858
    VIR_FREE(networkstr);
859 860
    VIR_FREE(addrStartStr);
    VIR_FREE(addrEndStr);
861
    VIR_FREE(portRangeStr);
862
    VIR_FREE(natRangeStr);
863
    return ret;
864 865
}

866 867 868 869 870
/**
 * iptablesAddForwardMasquerade:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @physdev: the physical input device or NULL
871
 * @protocol: the network protocol or NULL
872
 *
873 874 875 876 877 878
 * 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
 */
879
int
R
Roman Bogorodskiy 已提交
880
iptablesAddForwardMasquerade(virSocketAddr *netaddr,
881
                             unsigned int prefix,
882
                             const char *physdev,
883 884
                             virSocketAddrRangePtr addr,
                             virPortRangePtr port,
885
                             const char *protocol)
886
{
R
Roman Bogorodskiy 已提交
887
    return iptablesForwardMasquerade(netaddr, prefix, physdev, addr, port,
888
                                     protocol, ADD);
889 890
}

891 892 893 894 895
/**
 * iptablesRemoveForwardMasquerade:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @physdev: the physical input device or NULL
896
 * @protocol: the network protocol or NULL
897
 *
898 899 900 901 902 903
 * 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
 */
904
int
R
Roman Bogorodskiy 已提交
905
iptablesRemoveForwardMasquerade(virSocketAddr *netaddr,
906
                                unsigned int prefix,
907
                                const char *physdev,
908 909
                                virSocketAddrRangePtr addr,
                                virPortRangePtr port,
910
                                const char *protocol)
911
{
R
Roman Bogorodskiy 已提交
912
    return iptablesForwardMasquerade(netaddr, prefix, physdev, addr, port,
913
                                     protocol, REMOVE);
914
}
915 916


917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
/* Don't masquerade traffic coming from the network associated with the bridge
 * if said traffic targets @destaddr.
 */
static int
iptablesForwardDontMasquerade(virSocketAddr *netaddr,
                              unsigned int prefix,
                              const char *physdev,
                              const char *destaddr,
                              int action)
{
    int ret = -1;
    char *networkstr = NULL;
    virCommandPtr cmd = 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;
    }

    cmd = iptablesCommandNew("nat", "POSTROUTING", AF_INET, action);

    if (physdev && physdev[0])
        virCommandAddArgList(cmd, "--out-interface", physdev, NULL);

    virCommandAddArgList(cmd, "--source", networkstr,
                         "--destination", destaddr, "--jump", "RETURN", NULL);
    ret = virCommandRun(cmd, NULL);
cleanup:
    virCommandFree(cmd);
    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
iptablesAddDontMasquerade(virSocketAddr *netaddr,
                          unsigned int prefix,
                          const char *physdev,
                          const char *destaddr)
{
    return iptablesForwardDontMasquerade(netaddr, prefix, physdev, destaddr,
                                         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
iptablesRemoveDontMasquerade(virSocketAddr *netaddr,
                             unsigned int prefix,
                             const char *physdev,
                             const char *destaddr)
{
    return iptablesForwardDontMasquerade(netaddr, prefix, physdev, destaddr,
                                         REMOVE);
}


1005
static int
R
Roman Bogorodskiy 已提交
1006
iptablesOutputFixUdpChecksum(const char *iface,
1007 1008 1009 1010 1011 1012 1013 1014
                             int port,
                             int action)
{
    char portstr[32];

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

R
Roman Bogorodskiy 已提交
1015
    return iptablesAddRemoveRule("mangle", "POSTROUTING",
1016
                                 AF_INET,
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
                                 action,
                                 "--out-interface", iface,
                                 "--protocol", "udp",
                                 "--destination-port", portstr,
                                 "--jump", "CHECKSUM", "--checksum-fill",
                                 NULL);
}

/**
 * iptablesAddOutputFixUdpChecksum:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the UDP port to match
 *
E
Eric Blake 已提交
1031
 * Add a rule to the mangle table's POSTROUTING chain that fixes up the
1032 1033 1034 1035 1036 1037 1038 1039 1040
 * checksum of packets with the given destination @port.
 * the given @iface interface for TCP packets.
 *
 * Returns 0 in case of success or an error code in case of error.
 * (NB: if the system's iptables does not support checksum mangling,
 * this will return an error, which should be ignored.)
 */

int
R
Roman Bogorodskiy 已提交
1041
iptablesAddOutputFixUdpChecksum(const char *iface,
1042 1043
                                int port)
{
R
Roman Bogorodskiy 已提交
1044
    return iptablesOutputFixUdpChecksum(iface, port, ADD);
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
}

/**
 * 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.
 *
 * Returns 0 in case of success or an error code in case of error
 * (again, if iptables doesn't support checksum fixup, this will
 * return an error, which should be ignored)
 */
int
R
Roman Bogorodskiy 已提交
1061
iptablesRemoveOutputFixUdpChecksum(const char *iface,
1062 1063
                                   int port)
{
R
Roman Bogorodskiy 已提交
1064
    return iptablesOutputFixUdpChecksum(iface, port, REMOVE);
1065
}