viriptables.c 30.3 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 "virlog.h"
48
#include "virthread.h"
49 50 51 52 53 54 55 56 57

#if HAVE_FIREWALLD
static char *firewall_cmd_path = NULL;

static int
virIpTablesOnceInit(void)
{
    firewall_cmd_path = virFindFileInPath("firewall-cmd");
    if (!firewall_cmd_path) {
58
        VIR_INFO("firewall-cmd not found on system. "
59 60 61 62 63 64 65
                 "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) {
66
            VIR_INFO("firewall-cmd found but disabled for iptables");
67 68 69
            VIR_FREE(firewall_cmd_path);
            firewall_cmd_path = NULL;
        } else {
70
            VIR_INFO("using firewalld for iptables commands");
71 72 73 74 75 76 77 78 79
        }
        virCommandFree(cmd);
    }
    return 0;
}

VIR_ONCE_GLOBAL_INIT(virIpTables)

#endif
80

81
#define VIR_FROM_THIS VIR_FROM_NONE
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
enum {
    ADD = 0,
    REMOVE
};

typedef struct
{
    char  *table;
    char  *chain;
} iptRules;

struct _iptablesContext
{
    iptRules *input_filter;
    iptRules *forward_filter;
    iptRules *nat_postrouting;
99
    iptRules *mangle_postrouting;
100 101 102 103 104
};

static void
iptRulesFree(iptRules *rules)
{
105 106
    VIR_FREE(rules->table);
    VIR_FREE(rules->chain);
107
    VIR_FREE(rules);
108 109 110 111 112 113 114 115
}

static iptRules *
iptRulesNew(const char *table,
            const char *chain)
{
    iptRules *rules;

116
    if (VIR_ALLOC(rules) < 0)
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        return NULL;

    if (!(rules->table = strdup(table)))
        goto error;

    if (!(rules->chain = strdup(chain)))
        goto error;

    return rules;

 error:
    iptRulesFree(rules);
    return NULL;
}

132 133
static virCommandPtr
iptablesCommandNew(iptRules *rules, int family, int action)
134
{
135 136 137 138 139 140 141 142 143 144 145 146
    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)
147
                        ? IP6TABLES_PATH : IPTABLES_PATH);
148
    }
149

150 151
    virCommandAddArgList(cmd, "--table", rules->table,
                         action == ADD ? "--insert" : "--delete",
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
                         rules->chain, NULL);
    return cmd;
}

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

static int ATTRIBUTE_SENTINEL
iptablesAddRemoveRule(iptRules *rules, int family, int action,
                      const char *arg, ...)
{
    va_list args;
    virCommandPtr cmd = NULL;
    const char *s;

    cmd = iptablesCommandNew(rules, family, action);
    virCommandAddArg(cmd, arg);
175 176

    va_start(args, arg);
177 178
    while ((s = va_arg(args, const char *)))
        virCommandAddArg(cmd, s);
179 180
    va_end(args);

181
    return iptablesCommandRunAndFree(cmd);
182 183
}

184 185 186 187 188 189 190
/**
 * iptablesContextNew:
 *
 * Create a new IPtable context
 *
 * Returns a pointer to the new structure or NULL in case of error
 */
191 192 193 194 195
iptablesContext *
iptablesContextNew(void)
{
    iptablesContext *ctx;

196
    if (VIR_ALLOC(ctx) < 0)
197 198
        return NULL;

199
    if (!(ctx->input_filter = iptRulesNew("filter", "INPUT")))
200 201
        goto error;

202
    if (!(ctx->forward_filter = iptRulesNew("filter", "FORWARD")))
203 204
        goto error;

205
    if (!(ctx->nat_postrouting = iptRulesNew("nat", "POSTROUTING")))
206 207
        goto error;

208 209 210
    if (!(ctx->mangle_postrouting = iptRulesNew("mangle", "POSTROUTING")))
        goto error;

211 212 213 214 215 216 217
    return ctx;

 error:
    iptablesContextFree(ctx);
    return NULL;
}

218 219 220 221
/**
 * iptablesContextFree:
 * @ctx: pointer to the IP table context
 *
R
Richard W.M. Jones 已提交
222
 * Free the resources associated with an IP table context
223
 */
224 225 226
void
iptablesContextFree(iptablesContext *ctx)
{
227 228 229 230 231 232
    if (ctx->input_filter)
        iptRulesFree(ctx->input_filter);
    if (ctx->forward_filter)
        iptRulesFree(ctx->forward_filter);
    if (ctx->nat_postrouting)
        iptRulesFree(ctx->nat_postrouting);
233 234
    if (ctx->mangle_postrouting)
        iptRulesFree(ctx->mangle_postrouting);
235
    VIR_FREE(ctx);
236 237 238 239
}

static int
iptablesInput(iptablesContext *ctx,
240
              int family,
241 242 243 244 245 246 247 248 249 250
              const char *iface,
              int port,
              int action,
              int tcp)
{
    char portstr[32];

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

251
    return iptablesAddRemoveRule(ctx->input_filter,
252
                                 family,
253 254 255 256 257 258
                                 action,
                                 "--in-interface", iface,
                                 "--protocol", tcp ? "tcp" : "udp",
                                 "--destination-port", portstr,
                                 "--jump", "ACCEPT",
                                 NULL);
259 260
}

261 262 263 264 265 266 267 268 269 270 271 272
/**
 * 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
 */

273 274
int
iptablesAddTcpInput(iptablesContext *ctx,
275
                    int family,
276 277 278
                    const char *iface,
                    int port)
{
279
    return iptablesInput(ctx, family, iface, port, ADD, 1);
280 281
}

282 283 284 285 286 287
/**
 * iptablesRemoveTcpInput:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the TCP port to remove
 *
R
Richard W.M. Jones 已提交
288
 * Removes an input from the IP table, hence forbidding access to the given
289 290 291 292
 * @port on the given @iface interface for TCP packets
 *
 * Returns 0 in case of success or an error code in case of error
 */
293 294
int
iptablesRemoveTcpInput(iptablesContext *ctx,
295
                       int family,
296 297 298
                       const char *iface,
                       int port)
{
299
    return iptablesInput(ctx, family, iface, port, REMOVE, 1);
300 301
}

302 303 304 305 306 307 308 309 310 311 312 313
/**
 * 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
 */

314 315
int
iptablesAddUdpInput(iptablesContext *ctx,
316
                    int family,
317 318 319
                    const char *iface,
                    int port)
{
320
    return iptablesInput(ctx, family, iface, port, ADD, 0);
321 322
}

323 324 325 326 327 328
/**
 * iptablesRemoveUdpInput:
 * @ctx: pointer to the IP table context
 * @iface: the interface name
 * @port: the UDP port to remove
 *
R
Richard W.M. Jones 已提交
329
 * Removes an input from the IP table, hence forbidding access to the given
330 331 332 333
 * @port on the given @iface interface for UDP packets
 *
 * Returns 0 in case of success or an error code in case of error
 */
334 335
int
iptablesRemoveUdpInput(iptablesContext *ctx,
336
                       int family,
337 338 339
                       const char *iface,
                       int port)
{
340
    return iptablesInput(ctx, family, iface, port, REMOVE, 0);
341 342 343
}


344
static char *iptablesFormatNetwork(virSocketAddr *netaddr,
345
                                   unsigned int prefix)
346 347 348 349 350
{
    virSocketAddr network;
    char *netstr;
    char *ret;

351 352
    if (!(VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET) ||
          VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET6))) {
353 354
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Only IPv4 or IPv6 addresses can be used with iptables"));
355 356 357
        return NULL;
    }

358
    if (virSocketAddrMaskByPrefix(netaddr, prefix, &network) < 0) {
359 360
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Failure to mask address"));
361 362
        return NULL;
    }
363

364
    netstr = virSocketAddrFormat(&network);
365 366 367 368 369 370 371 372 373 374 375 376

    if (!netstr)
        return NULL;

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

    VIR_FREE(netstr);
    return ret;
}


377 378 379
/* Allow all traffic coming from the bridge, with a valid network address
 * to proceed to WAN
 */
380
static int
381
iptablesForwardAllowOut(iptablesContext *ctx,
382
                        virSocketAddr *netaddr,
383
                        unsigned int prefix,
384 385 386
                        const char *iface,
                        const char *physdev,
                        int action)
387
{
388 389
    int ret;
    char *networkstr;
390
    virCommandPtr cmd = NULL;
391

392
    if (!(networkstr = iptablesFormatNetwork(netaddr, prefix)))
393 394
        return -1;

395 396 397 398 399 400 401 402 403 404 405 406 407
    cmd = iptablesCommandNew(ctx->forward_filter,
                             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);
408 409
    VIR_FREE(networkstr);
    return ret;
410 411
}

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

435 436 437 438 439 440
/**
 * iptablesRemoveForwardAllowOut:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @iface: the source interface name
 * @physdev: the physical output device
441
 *
442 443 444 445 446 447
 * 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
 */
448
int
449
iptablesRemoveForwardAllowOut(iptablesContext *ctx,
450
                              virSocketAddr *netaddr,
451
                              unsigned int prefix,
452 453
                              const char *iface,
                              const char *physdev)
454
{
455
    return iptablesForwardAllowOut(ctx, netaddr, prefix, iface, physdev, REMOVE);
456 457
}

458 459 460 461

/* Allow all traffic destined to the bridge, with a valid network address
 * and associated with an existing connection
 */
462
static int
463
iptablesForwardAllowRelatedIn(iptablesContext *ctx,
464
                              virSocketAddr *netaddr,
465
                              unsigned int prefix,
466 467 468
                              const char *iface,
                              const char *physdev,
                              int action)
469
{
470 471 472
    int ret;
    char *networkstr;

473
    if (!(networkstr = iptablesFormatNetwork(netaddr, prefix)))
474 475
        return -1;

476
    if (physdev && physdev[0]) {
477
        ret = iptablesAddRemoveRule(ctx->forward_filter,
478
                                    VIR_SOCKET_ADDR_FAMILY(netaddr),
479 480 481 482
                                    action,
                                    "--destination", networkstr,
                                    "--in-interface", physdev,
                                    "--out-interface", iface,
S
Stefan Seyfried 已提交
483 484
                                    "--match", "conntrack",
                                    "--ctstate", "ESTABLISHED,RELATED",
485 486
                                    "--jump", "ACCEPT",
                                    NULL);
487
    } else {
488
        ret = iptablesAddRemoveRule(ctx->forward_filter,
489
                                    VIR_SOCKET_ADDR_FAMILY(netaddr),
490 491 492
                                    action,
                                    "--destination", networkstr,
                                    "--out-interface", iface,
S
Stefan Seyfried 已提交
493 494
                                    "--match", "conntrack",
                                    "--ctstate", "ESTABLISHED,RELATED",
495 496
                                    "--jump", "ACCEPT",
                                    NULL);
497
    }
498 499
    VIR_FREE(networkstr);
    return ret;
500 501
}

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
/**
 * 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
iptablesAddForwardAllowRelatedIn(iptablesContext *ctx,
517
                                 virSocketAddr *netaddr,
518
                                 unsigned int prefix,
519 520
                                 const char *iface,
                                 const char *physdev)
521
{
522
    return iptablesForwardAllowRelatedIn(ctx, netaddr, prefix, iface, physdev, ADD);
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
}

/**
 * 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
iptablesRemoveForwardAllowRelatedIn(iptablesContext *ctx,
540
                                    virSocketAddr *netaddr,
541
                                    unsigned int prefix,
542 543
                                    const char *iface,
                                    const char *physdev)
544
{
545
    return iptablesForwardAllowRelatedIn(ctx, netaddr, prefix, iface, physdev, REMOVE);
546 547 548 549 550 551
}

/* Allow all traffic destined to the bridge, with a valid network address
 */
static int
iptablesForwardAllowIn(iptablesContext *ctx,
552
                       virSocketAddr *netaddr,
553
                       unsigned int prefix,
554 555 556 557
                       const char *iface,
                       const char *physdev,
                       int action)
{
558 559 560
    int ret;
    char *networkstr;

561
    if (!(networkstr = iptablesFormatNetwork(netaddr, prefix)))
562 563
        return -1;

564
    if (physdev && physdev[0]) {
565
        ret = iptablesAddRemoveRule(ctx->forward_filter,
566
                                    VIR_SOCKET_ADDR_FAMILY(netaddr),
567 568 569 570 571 572
                                    action,
                                    "--destination", networkstr,
                                    "--in-interface", physdev,
                                    "--out-interface", iface,
                                    "--jump", "ACCEPT",
                                    NULL);
573
    } else {
574
        ret = iptablesAddRemoveRule(ctx->forward_filter,
575
                                    VIR_SOCKET_ADDR_FAMILY(netaddr),
576 577 578 579 580
                                    action,
                                    "--destination", networkstr,
                                    "--out-interface", iface,
                                    "--jump", "ACCEPT",
                                    NULL);
581
    }
582 583
    VIR_FREE(networkstr);
    return ret;
584 585
}

586 587 588 589 590 591
/**
 * 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
592
 *
593 594 595 596 597 598
 * 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
 */
599
int
600
iptablesAddForwardAllowIn(iptablesContext *ctx,
601
                          virSocketAddr *netaddr,
602
                          unsigned int prefix,
603 604 605
                          const char *iface,
                          const char *physdev)
{
606
    return iptablesForwardAllowIn(ctx, netaddr, prefix, iface, physdev, ADD);
607 608
}

609 610 611 612 613 614
/**
 * 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
615
 *
616 617 618 619 620 621
 * 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
 */
622 623
int
iptablesRemoveForwardAllowIn(iptablesContext *ctx,
624
                             virSocketAddr *netaddr,
625
                             unsigned int prefix,
626 627 628
                             const char *iface,
                             const char *physdev)
{
629
    return iptablesForwardAllowIn(ctx, netaddr, prefix, iface, physdev, REMOVE);
630 631 632 633 634 635 636 637
}


/* Allow all traffic between guests on the same bridge,
 * with a valid network address
 */
static int
iptablesForwardAllowCross(iptablesContext *ctx,
638
                          int family,
639 640 641 642
                          const char *iface,
                          int action)
{
    return iptablesAddRemoveRule(ctx->forward_filter,
643
                                 family,
644 645 646 647 648 649 650
                                 action,
                                 "--in-interface", iface,
                                 "--out-interface", iface,
                                 "--jump", "ACCEPT",
                                 NULL);
}

651 652 653 654 655 656 657 658 659 660 661
/**
 * 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
 */
662 663
int
iptablesAddForwardAllowCross(iptablesContext *ctx,
664 665 666 667
                             int family,
                             const char *iface)
{
    return iptablesForwardAllowCross(ctx, family, iface, ADD);
668 669
}

670 671 672 673 674 675 676 677 678 679 680
/**
 * 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
 */
681 682
int
iptablesRemoveForwardAllowCross(iptablesContext *ctx,
683 684 685 686
                                int family,
                                const char *iface)
{
    return iptablesForwardAllowCross(ctx, family, iface, REMOVE);
687 688 689 690 691 692 693 694
}


/* Drop all traffic trying to forward from the bridge.
 * ie the bridge is the in interface
 */
static int
iptablesForwardRejectOut(iptablesContext *ctx,
695
                         int family,
696 697 698 699
                         const char *iface,
                         int action)
{
    return iptablesAddRemoveRule(ctx->forward_filter,
700 701 702 703 704
                                 family,
                                 action,
                                 "--in-interface", iface,
                                 "--jump", "REJECT",
                                 NULL);
705 706
}

707 708 709 710 711 712 713 714 715 716
/**
 * 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
 */
717 718
int
iptablesAddForwardRejectOut(iptablesContext *ctx,
719
                            int family,
720 721
                            const char *iface)
{
722
    return iptablesForwardRejectOut(ctx, family, iface, ADD);
723 724
}

725 726 727 728 729 730 731 732 733 734
/**
 * 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
 */
735 736
int
iptablesRemoveForwardRejectOut(iptablesContext *ctx,
737
                               int family,
738 739
                               const char *iface)
{
740
    return iptablesForwardRejectOut(ctx, family, iface, REMOVE);
741 742 743 744 745 746 747 748 749 750
}




/* Drop all traffic trying to forward to the bridge.
 * ie the bridge is the out interface
 */
static int
iptablesForwardRejectIn(iptablesContext *ctx,
751
                        int family,
752
                        const char *iface,
753 754 755
                        int action)
{
    return iptablesAddRemoveRule(ctx->forward_filter,
756
                                 family,
757 758 759 760 761 762
                                 action,
                                 "--out-interface", iface,
                                 "--jump", "REJECT",
                                 NULL);
}

763 764 765 766 767 768 769 770 771 772
/**
 * 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
 */
773 774
int
iptablesAddForwardRejectIn(iptablesContext *ctx,
775
                           int family,
776
                           const char *iface)
777
{
778
    return iptablesForwardRejectIn(ctx, family, iface, ADD);
779 780
}

781 782 783 784 785 786 787 788 789 790
/**
 * 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
 */
791
int
792
iptablesRemoveForwardRejectIn(iptablesContext *ctx,
793
                              int family,
794
                              const char *iface)
795
{
796
    return iptablesForwardRejectIn(ctx, family, iface, REMOVE);
797 798
}

799 800 801 802

/* Masquerade all traffic coming from the network associated
 * with the bridge
 */
803
static int
804
iptablesForwardMasquerade(iptablesContext *ctx,
805
                          virSocketAddr *netaddr,
806
                          unsigned int prefix,
807
                          const char *physdev,
808 809
                          virSocketAddrRangePtr addr,
                          virPortRangePtr port,
810 811
                          const char *protocol,
                          int action)
812
{
813 814 815 816
    int ret = -1;
    char *networkstr = NULL;
    char *addrStartStr = NULL;
    char *addrEndStr = NULL;
817
    char *portRangeStr = NULL;
818
    char *natRangeStr = NULL;
819
    virCommandPtr cmd = NULL;
820

821
    if (!(networkstr = iptablesFormatNetwork(netaddr, prefix)))
822 823
        return -1;

824
    if (!VIR_SOCKET_ADDR_IS_FAMILY(netaddr, AF_INET)) {
825
        /* Higher level code *should* guaranteee it's impossible to get here. */
826 827 828
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Attempted to NAT '%s'. NAT is only supported for IPv4."),
                       networkstr);
829 830 831
        goto cleanup;
    }

832 833
    if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->start, AF_INET)) {
        if (!(addrStartStr = virSocketAddrFormat(&addr->start)))
834
            goto cleanup;
835 836
        if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->end, AF_INET)) {
            if (!(addrEndStr = virSocketAddrFormat(&addr->end)))
837 838
                goto cleanup;
        }
839 840
    }

841 842 843 844 845 846 847 848 849 850 851
    cmd = iptablesCommandNew(ctx->nat_postrouting, AF_INET, action);
    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);

852
    if (protocol && protocol[0]) {
853 854 855
        if (port->start == 0 && port->end == 0) {
            port->start = 1024;
            port->end = 65535;
856 857
        }

858 859 860
        if (port->start < port->end && port->end < 65536) {
            if (virAsprintf(&portRangeStr, ":%u-%u",
                            port->start, port->end) < 0) {
861 862 863 864 865 866
                virReportOOMError();
                goto cleanup;
            }
        } else {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid port range '%u-%u'."),
867
                           port->start, port->end);
868 869 870
        }
    }

871 872 873
    /* Use --jump SNAT if public addr is specified */
    if (addrStartStr && addrStartStr[0]) {
        int r = 0;
874

875 876
        if (addrEndStr && addrEndStr[0]) {
            r = virAsprintf(&natRangeStr, "%s-%s%s", addrStartStr, addrEndStr,
877
                            portRangeStr ? portRangeStr : "");
878
        } else {
879 880
            r = virAsprintf(&natRangeStr, "%s%s", addrStartStr,
                            portRangeStr ? portRangeStr : "");
881 882 883 884 885 886 887 888 889 890 891 892
        }

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

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

893 894
         if (portRangeStr && portRangeStr[0])
             virCommandAddArgList(cmd, "--to-ports", &portRangeStr[1], NULL);
895 896 897 898 899
     }

    ret = virCommandRun(cmd, NULL);
cleanup:
    virCommandFree(cmd);
900
    VIR_FREE(networkstr);
901 902
    VIR_FREE(addrStartStr);
    VIR_FREE(addrEndStr);
903
    VIR_FREE(portRangeStr);
904
    VIR_FREE(natRangeStr);
905
    return ret;
906 907
}

908 909 910 911 912
/**
 * iptablesAddForwardMasquerade:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @physdev: the physical input device or NULL
913
 * @protocol: the network protocol or NULL
914
 *
915 916 917 918 919 920
 * 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
 */
921
int
922
iptablesAddForwardMasquerade(iptablesContext *ctx,
923
                             virSocketAddr *netaddr,
924
                             unsigned int prefix,
925
                             const char *physdev,
926 927
                             virSocketAddrRangePtr addr,
                             virPortRangePtr port,
928
                             const char *protocol)
929
{
930
    return iptablesForwardMasquerade(ctx, netaddr, prefix, physdev, addr, port,
931
                                     protocol, ADD);
932 933
}

934 935 936 937 938
/**
 * iptablesRemoveForwardMasquerade:
 * @ctx: pointer to the IP table context
 * @network: the source network name
 * @physdev: the physical input device or NULL
939
 * @protocol: the network protocol or NULL
940
 *
941 942 943 944 945 946
 * 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
 */
947
int
948
iptablesRemoveForwardMasquerade(iptablesContext *ctx,
949
                                virSocketAddr *netaddr,
950
                                unsigned int prefix,
951
                                const char *physdev,
952 953
                                virSocketAddrRangePtr addr,
                                virPortRangePtr port,
954
                                const char *protocol)
955
{
956
    return iptablesForwardMasquerade(ctx, netaddr, prefix, physdev, addr, port,
957
                                     protocol, REMOVE);
958
}
959 960 961 962 963 964 965 966 967 968 969 970 971 972


static int
iptablesOutputFixUdpChecksum(iptablesContext *ctx,
                             const char *iface,
                             int port,
                             int action)
{
    char portstr[32];

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

    return iptablesAddRemoveRule(ctx->mangle_postrouting,
973
                                 AF_INET,
974 975 976 977 978 979 980 981 982 983 984 985 986 987
                                 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 已提交
988
 * Add a rule to the mangle table's POSTROUTING chain that fixes up the
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
 * 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
iptablesAddOutputFixUdpChecksum(iptablesContext *ctx,
                                const char *iface,
                                int port)
{
    return iptablesOutputFixUdpChecksum(ctx, iface, port, ADD);
}

/**
 * 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
iptablesRemoveOutputFixUdpChecksum(iptablesContext *ctx,
                                   const char *iface,
                                   int port)
{
    return iptablesOutputFixUdpChecksum(ctx, iface, port, REMOVE);
}