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

    if (!netstr)
        return NULL;

269
    ignore_value(virAsprintf(&ret, "%s/%d", netstr, prefix));
270 271 272 273 274 275

    VIR_FREE(netstr);
    return ret;
}


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

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

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

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

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

354 355 356 357

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

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

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

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

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

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

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

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

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

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


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

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

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


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

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

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




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

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

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

680 681 682 683

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

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

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

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

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

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

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

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

753 754
        if (addrEndStr && addrEndStr[0]) {
            r = virAsprintf(&natRangeStr, "%s-%s%s", addrStartStr, addrEndStr,
755
                            portRangeStr ? portRangeStr : "");
756
        } else {
757 758
            r = virAsprintf(&natRangeStr, "%s%s", addrStartStr,
                            portRangeStr ? portRangeStr : "");
759 760
        }

761
        if (r < 0)
762 763 764 765 766 767 768
            goto cleanup;

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

769 770
         if (portRangeStr && portRangeStr[0])
             virCommandAddArgList(cmd, "--to-ports", &portRangeStr[1], NULL);
771 772 773 774 775
     }

    ret = virCommandRun(cmd, NULL);
cleanup:
    virCommandFree(cmd);
776
    VIR_FREE(networkstr);
777 778
    VIR_FREE(addrStartStr);
    VIR_FREE(addrEndStr);
779
    VIR_FREE(portRangeStr);
780
    VIR_FREE(natRangeStr);
781
    return ret;
782 783
}

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

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


835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
/* 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);
}


923
static int
R
Roman Bogorodskiy 已提交
924
iptablesOutputFixUdpChecksum(const char *iface,
925 926 927 928 929 930 931 932
                             int port,
                             int action)
{
    char portstr[32];

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

R
Roman Bogorodskiy 已提交
933
    return iptablesAddRemoveRule("mangle", "POSTROUTING",
934
                                 AF_INET,
935 936 937 938 939 940 941 942 943 944 945 946 947 948
                                 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 已提交
949
 * Add a rule to the mangle table's POSTROUTING chain that fixes up the
950 951 952 953 954 955 956 957 958
 * 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 已提交
959
iptablesAddOutputFixUdpChecksum(const char *iface,
960 961
                                int port)
{
R
Roman Bogorodskiy 已提交
962
    return iptablesOutputFixUdpChecksum(iface, port, ADD);
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
}

/**
 * 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 已提交
979
iptablesRemoveOutputFixUdpChecksum(const char *iface,
980 981
                                   int port)
{
R
Roman Bogorodskiy 已提交
982
    return iptablesOutputFixUdpChecksum(iface, port, REMOVE);
983
}