viriptables.c 27.1 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 55 56 57 58 59 60

#if HAVE_FIREWALLD
static char *firewall_cmd_path = NULL;

static int
virIpTablesOnceInit(void)
{
    firewall_cmd_path = virFindFileInPath("firewall-cmd");
    if (!firewall_cmd_path) {
61
        VIR_INFO("firewall-cmd not found on system. "
62 63 64 65 66 67 68
                 "firewalld support disabled for iptables.");
    } else {
        virCommandPtr cmd = virCommandNew(firewall_cmd_path);
        int status;

        virCommandAddArgList(cmd, "--state", NULL);
        if (virCommandRun(cmd, &status) < 0 || status != 0) {
69
            VIR_INFO("firewall-cmd found but disabled for iptables");
70 71 72
            VIR_FREE(firewall_cmd_path);
            firewall_cmd_path = NULL;
        } else {
73
            VIR_INFO("using firewalld for iptables commands");
74 75 76 77 78 79 80 81 82
        }
        virCommandFree(cmd);
    }
    return 0;
}

VIR_ONCE_GLOBAL_INIT(virIpTables)

#endif
83

84
#define VIR_FROM_THIS VIR_FROM_NONE
85

86 87 88 89 90
enum {
    ADD = 0,
    REMOVE
};

91
static virCommandPtr
R
Roman Bogorodskiy 已提交
92
iptablesCommandNew(const char *table, const char *chain, int family, int action)
93
{
94 95 96 97 98 99 100 101 102 103 104 105
    virCommandPtr cmd = NULL;
#if HAVE_FIREWALLD
    virIpTablesInitialize();
    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)
106
                        ? IP6TABLES_PATH : IPTABLES_PATH);
107
    }
108

R
Roman Bogorodskiy 已提交
109
    virCommandAddArgList(cmd, "--table", table,
110
                         action == ADD ? "--insert" : "--delete",
R
Roman Bogorodskiy 已提交
111
                         chain, NULL);
112 113 114 115 116 117 118 119 120 121 122 123 124
    return cmd;
}

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

static int ATTRIBUTE_SENTINEL
R
Roman Bogorodskiy 已提交
125
iptablesAddRemoveRule(const char *table, const char *chain, int family, int action,
126 127 128 129 130 131
                      const char *arg, ...)
{
    va_list args;
    virCommandPtr cmd = NULL;
    const char *s;

R
Roman Bogorodskiy 已提交
132
    cmd = iptablesCommandNew(table, chain, family, action);
133
    virCommandAddArg(cmd, arg);
134 135

    va_start(args, arg);
136 137
    while ((s = va_arg(args, const char *)))
        virCommandAddArg(cmd, s);
138 139
    va_end(args);

140
    return iptablesCommandRunAndFree(cmd);
141 142 143
}

static int
R
Roman Bogorodskiy 已提交
144
iptablesInput(int family,
145 146 147 148 149 150 151 152 153 154
              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 已提交
155
    return iptablesAddRemoveRule("filter", "INPUT",
156
                                 family,
157 158 159 160 161 162
                                 action,
                                 "--in-interface", iface,
                                 "--protocol", tcp ? "tcp" : "udp",
                                 "--destination-port", portstr,
                                 "--jump", "ACCEPT",
                                 NULL);
163 164
}

165 166 167 168 169 170 171 172 173 174 175 176
/**
 * 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
 */

177
int
R
Roman Bogorodskiy 已提交
178
iptablesAddTcpInput(int family,
179 180 181
                    const char *iface,
                    int port)
{
R
Roman Bogorodskiy 已提交
182
    return iptablesInput(family, iface, port, ADD, 1);
183 184
}

185 186 187 188 189 190
/**
 * iptablesRemoveTcpInput:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the TCP port to remove
 *
R
Richard W.M. Jones 已提交
191
 * Removes an input from the IP table, hence forbidding access to the given
192 193 194 195
 * @port on the given @iface interface for TCP packets
 *
 * Returns 0 in case of success or an error code in case of error
 */
196
int
R
Roman Bogorodskiy 已提交
197
iptablesRemoveTcpInput(int family,
198 199 200
                       const char *iface,
                       int port)
{
R
Roman Bogorodskiy 已提交
201
    return iptablesInput(family, iface, port, REMOVE, 1);
202 203
}

204 205 206 207 208 209 210 211 212 213 214 215
/**
 * 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
 */

216
int
R
Roman Bogorodskiy 已提交
217
iptablesAddUdpInput(int family,
218 219 220
                    const char *iface,
                    int port)
{
R
Roman Bogorodskiy 已提交
221
    return iptablesInput(family, iface, port, ADD, 0);
222 223
}

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


244
static char *iptablesFormatNetwork(virSocketAddr *netaddr,
245
                                   unsigned int prefix)
246 247 248 249 250
{
    virSocketAddr network;
    char *netstr;
    char *ret;

251 252
    if (!(VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET) ||
          VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET6))) {
253 254
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Only IPv4 or IPv6 addresses can be used with iptables"));
255 256 257
        return NULL;
    }

258
    if (virSocketAddrMaskByPrefix(netaddr, prefix, &network) < 0) {
259 260
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Failure to mask address"));
261 262
        return NULL;
    }
263

264
    netstr = virSocketAddrFormat(&network);
265 266 267 268 269 270 271 272 273 274 275 276

    if (!netstr)
        return NULL;

    if (virAsprintf(&ret, "%s/%d", netstr, prefix) < 0)
        virReportOOMError();

    VIR_FREE(netstr);
    return ret;
}


277 278 279
/* Allow all traffic coming from the bridge, with a valid network address
 * to proceed to WAN
 */
280
static int
R
Roman Bogorodskiy 已提交
281
iptablesForwardAllowOut(virSocketAddr *netaddr,
282
                        unsigned int prefix,
283 284 285
                        const char *iface,
                        const char *physdev,
                        int action)
286
{
287 288
    int ret;
    char *networkstr;
289
    virCommandPtr cmd = NULL;
290

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

R
Roman Bogorodskiy 已提交
294
    cmd = iptablesCommandNew("filter", "FORWARD",
295 296 297 298 299 300 301 302 303 304 305 306
                             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);
307 308
    VIR_FREE(networkstr);
    return ret;
309 310
}

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

333 334 335 336 337 338
/**
 * iptablesRemoveForwardAllowOut:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @iface: the source interface name
 * @physdev: the physical output device
339
 *
340 341 342 343 344 345
 * 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
 */
346
int
R
Roman Bogorodskiy 已提交
347
iptablesRemoveForwardAllowOut(virSocketAddr *netaddr,
348
                              unsigned int prefix,
349 350
                              const char *iface,
                              const char *physdev)
351
{
R
Roman Bogorodskiy 已提交
352
    return iptablesForwardAllowOut(netaddr, prefix, iface, physdev, REMOVE);
353 354
}

355 356 357 358

/* Allow all traffic destined to the bridge, with a valid network address
 * and associated with an existing connection
 */
359
static int
R
Roman Bogorodskiy 已提交
360
iptablesForwardAllowRelatedIn(virSocketAddr *netaddr,
361
                              unsigned int prefix,
362 363 364
                              const char *iface,
                              const char *physdev,
                              int action)
365
{
366 367 368
    int ret;
    char *networkstr;

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

372
    if (physdev && physdev[0]) {
R
Roman Bogorodskiy 已提交
373
        ret = iptablesAddRemoveRule("filter", "FORWARD",
374
                                    VIR_SOCKET_ADDR_FAMILY(netaddr),
375 376 377 378
                                    action,
                                    "--destination", networkstr,
                                    "--in-interface", physdev,
                                    "--out-interface", iface,
S
Stefan Seyfried 已提交
379 380
                                    "--match", "conntrack",
                                    "--ctstate", "ESTABLISHED,RELATED",
381 382
                                    "--jump", "ACCEPT",
                                    NULL);
383
    } else {
R
Roman Bogorodskiy 已提交
384
        ret = iptablesAddRemoveRule("filter", "FORWARD",
385
                                    VIR_SOCKET_ADDR_FAMILY(netaddr),
386 387 388
                                    action,
                                    "--destination", networkstr,
                                    "--out-interface", iface,
S
Stefan Seyfried 已提交
389 390
                                    "--match", "conntrack",
                                    "--ctstate", "ESTABLISHED,RELATED",
391 392
                                    "--jump", "ACCEPT",
                                    NULL);
393
    }
394 395
    VIR_FREE(networkstr);
    return ret;
396 397
}

398 399 400 401 402 403 404 405 406 407 408 409 410 411
/**
 * 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 已提交
412
iptablesAddForwardAllowRelatedIn(virSocketAddr *netaddr,
413
                                 unsigned int prefix,
414 415
                                 const char *iface,
                                 const char *physdev)
416
{
R
Roman Bogorodskiy 已提交
417
    return iptablesForwardAllowRelatedIn(netaddr, prefix, iface, physdev, ADD);
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
}

/**
 * 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 已提交
434
iptablesRemoveForwardAllowRelatedIn(virSocketAddr *netaddr,
435
                                    unsigned int prefix,
436 437
                                    const char *iface,
                                    const char *physdev)
438
{
R
Roman Bogorodskiy 已提交
439
    return iptablesForwardAllowRelatedIn(netaddr, prefix, iface, physdev, REMOVE);
440 441 442 443 444
}

/* Allow all traffic destined to the bridge, with a valid network address
 */
static int
R
Roman Bogorodskiy 已提交
445
iptablesForwardAllowIn(virSocketAddr *netaddr,
446
                       unsigned int prefix,
447 448 449 450
                       const char *iface,
                       const char *physdev,
                       int action)
{
451 452 453
    int ret;
    char *networkstr;

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

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

479 480 481 482 483 484
/**
 * 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
485
 *
486 487 488 489 490 491
 * 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
 */
492
int
R
Roman Bogorodskiy 已提交
493
iptablesAddForwardAllowIn(virSocketAddr *netaddr,
494
                          unsigned int prefix,
495 496 497
                          const char *iface,
                          const char *physdev)
{
R
Roman Bogorodskiy 已提交
498
    return iptablesForwardAllowIn(netaddr, prefix, iface, physdev, ADD);
499 500
}

501 502 503 504 505 506
/**
 * 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
507
 *
508 509 510 511 512 513
 * 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
 */
514
int
R
Roman Bogorodskiy 已提交
515
iptablesRemoveForwardAllowIn(virSocketAddr *netaddr,
516
                             unsigned int prefix,
517 518 519
                             const char *iface,
                             const char *physdev)
{
R
Roman Bogorodskiy 已提交
520
    return iptablesForwardAllowIn(netaddr, prefix, iface, physdev, REMOVE);
521 522 523 524 525 526 527
}


/* Allow all traffic between guests on the same bridge,
 * with a valid network address
 */
static int
R
Roman Bogorodskiy 已提交
528
iptablesForwardAllowCross(int family,
529 530 531
                          const char *iface,
                          int action)
{
R
Roman Bogorodskiy 已提交
532
    return iptablesAddRemoveRule("filter", "FORWARD",
533
                                 family,
534 535 536 537 538 539 540
                                 action,
                                 "--in-interface", iface,
                                 "--out-interface", iface,
                                 "--jump", "ACCEPT",
                                 NULL);
}

541 542 543 544 545 546 547 548 549 550 551
/**
 * 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
 */
552
int
R
Roman Bogorodskiy 已提交
553
iptablesAddForwardAllowCross(int family,
554 555
                             const char *iface)
{
R
Roman Bogorodskiy 已提交
556
    return iptablesForwardAllowCross(family, iface, ADD);
557 558
}

559 560 561 562 563 564 565 566 567 568 569
/**
 * 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
 */
570
int
R
Roman Bogorodskiy 已提交
571
iptablesRemoveForwardAllowCross(int family,
572 573
                                const char *iface)
{
R
Roman Bogorodskiy 已提交
574
    return iptablesForwardAllowCross(family, iface, REMOVE);
575 576 577 578 579 580 581
}


/* Drop all traffic trying to forward from the bridge.
 * ie the bridge is the in interface
 */
static int
R
Roman Bogorodskiy 已提交
582
iptablesForwardRejectOut(int family,
583 584 585
                         const char *iface,
                         int action)
{
R
Roman Bogorodskiy 已提交
586
    return iptablesAddRemoveRule("filter", "FORWARD",
587 588 589 590 591
                                 family,
                                 action,
                                 "--in-interface", iface,
                                 "--jump", "REJECT",
                                 NULL);
592 593
}

594 595 596 597 598 599 600 601 602 603
/**
 * 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
 */
604
int
R
Roman Bogorodskiy 已提交
605
iptablesAddForwardRejectOut(int family,
606 607
                            const char *iface)
{
R
Roman Bogorodskiy 已提交
608
    return iptablesForwardRejectOut(family, iface, ADD);
609 610
}

611 612 613 614 615 616 617 618 619 620
/**
 * 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
 */
621
int
R
Roman Bogorodskiy 已提交
622
iptablesRemoveForwardRejectOut(int family,
623 624
                               const char *iface)
{
R
Roman Bogorodskiy 已提交
625
    return iptablesForwardRejectOut(family, iface, REMOVE);
626 627 628 629 630 631 632 633 634
}




/* Drop all traffic trying to forward to the bridge.
 * ie the bridge is the out interface
 */
static int
R
Roman Bogorodskiy 已提交
635
iptablesForwardRejectIn(int family,
636
                        const char *iface,
637 638
                        int action)
{
R
Roman Bogorodskiy 已提交
639
    return iptablesAddRemoveRule("filter", "FORWARD",
640
                                 family,
641 642 643 644 645 646
                                 action,
                                 "--out-interface", iface,
                                 "--jump", "REJECT",
                                 NULL);
}

647 648 649 650 651 652 653 654 655 656
/**
 * 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
 */
657
int
R
Roman Bogorodskiy 已提交
658
iptablesAddForwardRejectIn(int family,
659
                           const char *iface)
660
{
R
Roman Bogorodskiy 已提交
661
    return iptablesForwardRejectIn(family, iface, ADD);
662 663
}

664 665 666 667 668 669 670 671 672 673
/**
 * 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
 */
674
int
R
Roman Bogorodskiy 已提交
675
iptablesRemoveForwardRejectIn(int family,
676
                              const char *iface)
677
{
R
Roman Bogorodskiy 已提交
678
    return iptablesForwardRejectIn(family, iface, REMOVE);
679 680
}

681 682 683 684

/* Masquerade all traffic coming from the network associated
 * with the bridge
 */
685
static int
R
Roman Bogorodskiy 已提交
686
iptablesForwardMasquerade(virSocketAddr *netaddr,
687
                          unsigned int prefix,
688
                          const char *physdev,
689 690
                          virSocketAddrRangePtr addr,
                          virPortRangePtr port,
691 692
                          const char *protocol,
                          int action)
693
{
694 695 696 697
    int ret = -1;
    char *networkstr = NULL;
    char *addrStartStr = NULL;
    char *addrEndStr = NULL;
698
    char *portRangeStr = NULL;
699
    char *natRangeStr = NULL;
700
    virCommandPtr cmd = NULL;
701

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

705
    if (!VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET)) {
706
        /* Higher level code *should* guaranteee it's impossible to get here. */
707 708 709
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Attempted to NAT '%s'. NAT is only supported for IPv4."),
                       networkstr);
710 711 712
        goto cleanup;
    }

713 714
    if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->start, AF_INET)) {
        if (!(addrStartStr = virSocketAddrFormat(&addr->start)))
715
            goto cleanup;
716 717
        if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->end, AF_INET)) {
            if (!(addrEndStr = virSocketAddrFormat(&addr->end)))
718 719
                goto cleanup;
        }
720 721
    }

R
Roman Bogorodskiy 已提交
722
    cmd = iptablesCommandNew("nat", "POSTROUTING", AF_INET, action);
723 724 725 726 727 728 729 730 731 732
    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);

733
    if (protocol && protocol[0]) {
734 735 736
        if (port->start == 0 && port->end == 0) {
            port->start = 1024;
            port->end = 65535;
737 738
        }

739 740 741
        if (port->start < port->end && port->end < 65536) {
            if (virAsprintf(&portRangeStr, ":%u-%u",
                            port->start, port->end) < 0) {
742 743 744 745 746 747
                virReportOOMError();
                goto cleanup;
            }
        } else {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid port range '%u-%u'."),
748
                           port->start, port->end);
749 750 751
        }
    }

752 753 754
    /* Use --jump SNAT if public addr is specified */
    if (addrStartStr && addrStartStr[0]) {
        int r = 0;
755

756 757
        if (addrEndStr && addrEndStr[0]) {
            r = virAsprintf(&natRangeStr, "%s-%s%s", addrStartStr, addrEndStr,
758
                            portRangeStr ? portRangeStr : "");
759
        } else {
760 761
            r = virAsprintf(&natRangeStr, "%s%s", addrStartStr,
                            portRangeStr ? portRangeStr : "");
762 763 764 765 766 767 768 769 770 771 772 773
        }

        if (r < 0) {
            virReportOOMError();
            goto cleanup;
        }

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

774 775
         if (portRangeStr && portRangeStr[0])
             virCommandAddArgList(cmd, "--to-ports", &portRangeStr[1], NULL);
776 777 778 779 780
     }

    ret = virCommandRun(cmd, NULL);
cleanup:
    virCommandFree(cmd);
781
    VIR_FREE(networkstr);
782 783
    VIR_FREE(addrStartStr);
    VIR_FREE(addrEndStr);
784
    VIR_FREE(portRangeStr);
785
    VIR_FREE(natRangeStr);
786
    return ret;
787 788
}

789 790 791 792 793
/**
 * iptablesAddForwardMasquerade:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @physdev: the physical input device or NULL
794
 * @protocol: the network protocol or NULL
795
 *
796 797 798 799 800 801
 * 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
 */
802
int
R
Roman Bogorodskiy 已提交
803
iptablesAddForwardMasquerade(virSocketAddr *netaddr,
804
                             unsigned int prefix,
805
                             const char *physdev,
806 807
                             virSocketAddrRangePtr addr,
                             virPortRangePtr port,
808
                             const char *protocol)
809
{
R
Roman Bogorodskiy 已提交
810
    return iptablesForwardMasquerade(netaddr, prefix, physdev, addr, port,
811
                                     protocol, ADD);
812 813
}

814 815 816 817 818
/**
 * iptablesRemoveForwardMasquerade:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @physdev: the physical input device or NULL
819
 * @protocol: the network protocol or NULL
820
 *
821 822 823 824 825 826
 * 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
 */
827
int
R
Roman Bogorodskiy 已提交
828
iptablesRemoveForwardMasquerade(virSocketAddr *netaddr,
829
                                unsigned int prefix,
830
                                const char *physdev,
831 832
                                virSocketAddrRangePtr addr,
                                virPortRangePtr port,
833
                                const char *protocol)
834
{
R
Roman Bogorodskiy 已提交
835
    return iptablesForwardMasquerade(netaddr, prefix, physdev, addr, port,
836
                                     protocol, REMOVE);
837
}
838 839 840


static int
R
Roman Bogorodskiy 已提交
841
iptablesOutputFixUdpChecksum(const char *iface,
842 843 844 845 846 847 848 849
                             int port,
                             int action)
{
    char portstr[32];

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

R
Roman Bogorodskiy 已提交
850
    return iptablesAddRemoveRule("mangle", "POSTROUTING",
851
                                 AF_INET,
852 853 854 855 856 857 858 859 860 861 862 863 864 865
                                 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 已提交
866
 * Add a rule to the mangle table's POSTROUTING chain that fixes up the
867 868 869 870 871 872 873 874 875
 * 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 已提交
876
iptablesAddOutputFixUdpChecksum(const char *iface,
877 878
                                int port)
{
R
Roman Bogorodskiy 已提交
879
    return iptablesOutputFixUdpChecksum(iface, port, ADD);
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
}

/**
 * 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 已提交
896
iptablesRemoveOutputFixUdpChecksum(const char *iface,
897 898
                                   int port)
{
R
Roman Bogorodskiy 已提交
899
    return iptablesOutputFixUdpChecksum(iface, port, REMOVE);
900
}