viriptables.c 32.2 KB
Newer Older
1
/*
2 3
 * viriptables.c: helper APIs for managing iptables
 *
4
 * Copyright (C) 2007-2013 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 65
    virCommandPtr cmd;
    int status;

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

        virCommandAddArgList(cmd, "--state", NULL);
        if (virCommandRun(cmd, &status) < 0 || status != 0) {
75
            VIR_INFO("firewall-cmd found but disabled for iptables");
76 77 78
            VIR_FREE(firewall_cmd_path);
            firewall_cmd_path = NULL;
        } else {
79
            VIR_INFO("using firewalld for iptables commands");
80 81 82
        }
        virCommandFree(cmd);
    }
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

    if (firewall_cmd_path)
        return 0;

#endif

    cmd = virCommandNew(IPTABLES_PATH);
    virCommandAddArgList(cmd, "-w", "-L", "-n", NULL);
    if (virCommandRun(cmd, &status) < 0 || status != 0) {
        VIR_INFO("xtables locking not supported by your iptables");
    } else {
        VIR_INFO("using xtables locking for iptables");
        iptables_supports_xlock = true;
    }
    virCommandFree(cmd);
98 99 100 101 102
    return 0;
}

VIR_ONCE_GLOBAL_INIT(virIpTables)

103
#define VIR_FROM_THIS VIR_FROM_NONE
104

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

110
static virCommandPtr
R
Roman Bogorodskiy 已提交
111
iptablesCommandNew(const char *table, const char *chain, int family, int action)
112
{
113 114
    virCommandPtr cmd = NULL;
    virIpTablesInitialize();
115
#if HAVE_FIREWALLD
116 117 118 119 120 121 122 123 124
    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)
125
                        ? IP6TABLES_PATH : IPTABLES_PATH);
126 127 128

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

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

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

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

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

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

162
    return iptablesCommandRunAndFree(cmd);
163 164 165
}

static int
R
Roman Bogorodskiy 已提交
166
iptablesInput(int family,
167 168 169 170 171 172 173 174 175 176
              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 已提交
177
    return iptablesAddRemoveRule("filter", "INPUT",
178
                                 family,
179 180 181 182 183 184
                                 action,
                                 "--in-interface", iface,
                                 "--protocol", tcp ? "tcp" : "udp",
                                 "--destination-port", portstr,
                                 "--jump", "ACCEPT",
                                 NULL);
185 186
}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
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);
}

209 210 211 212 213 214 215 216 217 218 219 220
/**
 * 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
 */

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

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

248 249 250 251 252 253 254 255 256 257 258 259
/**
 * 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
 */

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

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

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
/**
 * 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);
}

326

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

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

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

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

    if (!netstr)
        return NULL;

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

    VIR_FREE(netstr);
    return ret;
}


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

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

R
Roman Bogorodskiy 已提交
376
    cmd = iptablesCommandNew("filter", "FORWARD",
377 378 379 380 381 382 383 384 385 386 387 388
                             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);
389 390
    VIR_FREE(networkstr);
    return ret;
391 392
}

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

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

437 438 439 440

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

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

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

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

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

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

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

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

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

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


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

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

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


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

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

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




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

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

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

763 764 765 766

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

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

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

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

R
Roman Bogorodskiy 已提交
804
    cmd = iptablesCommandNew("nat", "POSTROUTING", AF_INET, action);
805 806 807 808 809 810 811 812 813 814
    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);

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

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

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

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

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

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

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

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

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

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


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

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

R
Roman Bogorodskiy 已提交
1016
    return iptablesAddRemoveRule("mangle", "POSTROUTING",
1017
                                 AF_INET,
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
                                 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 已提交
1032
 * Add a rule to the mangle table's POSTROUTING chain that fixes up the
1033 1034 1035 1036 1037 1038 1039 1040 1041
 * 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 已提交
1042
iptablesAddOutputFixUdpChecksum(const char *iface,
1043 1044
                                int port)
{
R
Roman Bogorodskiy 已提交
1045
    return iptablesOutputFixUdpChecksum(iface, port, ADD);
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
}

/**
 * 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 已提交
1062
iptablesRemoveOutputFixUdpChecksum(const char *iface,
1063 1064
                                   int port)
{
R
Roman Bogorodskiy 已提交
1065
    return iptablesOutputFixUdpChecksum(iface, port, REMOVE);
1066
}