Shorewall and Multiple Internet Connections

Tom Eastep

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover, and with no Back-Cover Texts. A copy of the license is included in the section entitled “GNU Free Documentation License”.


Table of Contents

Multiple Internet Connection Support
/etc/shorewall/providers File
What an entry in the Providers File Does
/etc/shorewall/masq and Multi-ISP
Routing a Particular Application Through a Specific Interface
Port Forwarding
More than 2 Providers
Applications running on the Firewall
Routing Rules
Columns in the route_rules file
Multi-ISP and VPN
Gateway Monitoring and Failover
Link Status Monitor (LSM)
Two Providers Sharing an Interface
A Complete Working Example


This document describes the Multi-ISP facility in Shorewall 4.3.5 and later. If you are running an earlier release, please see the documentation for that release.


Reading just Shorewall documentation is probably not going to give you enough background to use this material. Shorewall may make iptables easy but the Shorewall team doesn't have the resources to be able to spoon-feed Linux policy routing to you (please remember that the user's manual for a tractor doesn't teach you to grow corn either). You will likely need to refer to the following additional information:

Multiple Internet Connection Support

Shorewall includes limited support for multiple Internet connections. Limitations of this support are as follows:

  • It utilizes static routing configuration. If there is a change in the routing topopogy, Shorewall must be restarted.

  • The routing changes are made and the route cache is purged when Shorewall is started and when Shorewall is restarted (unless you specify the "-n" option to shorewall restart). Ideally, restarting the packet filter should have no effect on routing.

  • For most routing applications, Quagga is a better solution although it requires that your ISPs offer routing protocol support.


Let's assume that a firewall is connected via two separate Ethernet interfaces to two different ISPs as in the following diagram.

  • eth0 connects to ISP1. The IP address of eth0 is and the ISP's gateway router has IP address

  • eth1 connects to ISP 2. The IP address of eth1 is and the ISP's gateway router has IP address

  • eth2 connects to the local LAN. Its IP configuration is not relevant to this discussion.

Each of these providers is described in an entry in the file /etc/shorewall/providers.

Entries in /etc/shorewall/providers can specify that outgoing connections are to be load-balanced between the two ISPs. Entries in /etc/shorewall/tcrules and /etc/shorewall/route_rules can be used to direct particular outgoing connections to one ISP or the other. Use of /etc/shorewall/tcrules is not required for /etc/shorewall/providers to work, but in most cases, you must select a unique MARK value for each provider so Shorewall can set up the correct marking rules for you.

When you use the track option in /etc/shorewall/providers, connections from the Internet are automatically routed back out of the correct interface and through the correct ISP gateway. This works whether the connection is handled by the firewall itself or if it is routed or port-forwarded to a system behind the firewall.

Shorewall will set up the routing and will update the /etc/iproute2/rt_tables to include the table names and numbers of the tables that it adds.


This feature uses packet marking to control the routing. As a consequence, there are some restrictions concerning entries in /etc/shorewall/tcrules:

  • Packet marking for traffic control purposes may not be done in the PREROUTING table for connections involving providers with 'track' specified (see below).

  • You may not use the SAVE or RESTORE options unless you also set HIGH_ROUTE_MARKS=Yes in /etc/shorewall/shorewall.conf.

  • You may not use connection marking unless you also set HIGH_ROUTE_MARKS=Yes in /etc/shorewall/shorewall.conf.

The /etc/shorewall/providers file can also be used in other routing scenarios. See the Squid documentation for an example.

/etc/shorewall/providers File

Entries in this file have the following columns. As in all Shorewall configuration files, enter "-" in a column if you don't want to enter any value.


The provider name. Must begin with a letter and consist of letters and digits. The provider name becomes the name of the generated routing table for this provider.


A number between 1 and 252. This becomes the routing table number for the generated table for this provider.


A mark value used in your /etc/shorewall/tcrules file to direct packets to this provider. Shorewall will also mark connections that have seen input from this provider with this value and will restore the packet mark in the PREROUTING CHAIN. Mark values must be in the range 1-255.

Alternatively, you may set HIGH_ROUTE_MARKS=Yes in /etc/shorewall/shorewall.conf. This allows you to:

  • Use connection marks for traffic shaping, provided that you assign those marks in the FORWARD chain.

  • Use mark values > 255 for provider marks in this column. These mark values must be a multiple of 256 in the range 256-65280 (hex equivalent 0x100 - 0xFF00 with the low-order 8 bits being zero).

This column may be omitted if you don´t use packet marking to direct connections to a particular provider and you don´t specify track in the OPTIONS column.


Gives the name or number of a routing table to duplicate. May be 'main' or the name or number of a previously declared provider. For most applications, you want to specify 'main' here. This field should be be specified as '-' when USE_DEFAULT_RT=Yes in shorewall.conf


The name of the interface to the provider. Where multiple providers share the same interface (which is not recommended), you must follow the name of the interface by a colon (":") and the IP address assigned by this provider (e.g., eth0: See below for additional considerations.

The interface must have been previously defined in shorewall-interfaces (5). In general, that interface should not have the proxyarp option specified unless loose is given in the OPTIONS column of this entry.


The IP address of the provider's Gateway router.

You can enter detect here and Shorewall will attempt to automatically determine the gateway IP address.

Hint: "detect" is appropriate for use in cases where the interface named in the INTERFACE column is dynamically configured via DHCP etc. Be sure, however, that you don't have stale dhcp client state files in /var/lib/dhcpcd or /var/lib/dhclient-*.lease because Shorewall may try to use those stale files to determine the gateway address.

The GATEWAY may be omitted (enter '-') for point-to-point links.


A comma-separated list from the following:


If specified, connections FROM this interface are to be tracked so that responses may be routed back out this same interface.

You want to specify 'track' if Internet hosts will be connecting to local servers through this provider. Any time that you specify 'track', you will normally want to also specify 'balance' (see below). 'track' will also ensure that outgoing connections remain stay anchored to a single provider and don't try to switch providers when route cache entries expire.

Use of this feature requires that your kernel and iptables include CONNMARK target and connmark match support (Warning: Until recently, standard Debian™ and Ubuntu™ kernels lacked that support. Both Lenny and Jaunty do have the proper support).


If you are using /etc/shorewall/providers because you have multiple Internet connections, we recommend that you specify track even if you don't need it. It helps maintain long-term connections in which there are significant periods with no traffic.


The providers that have balance specified will get outbound traffic load-balanced among them. Balancing will not be perfect, as it is route based, and routes are cached. This means that routes to often-used sites will always be over the same provider.

By default, each provider is given the same weight (1) . You can change the weight of a given provider by following balance with "=" and the desired weight (e.g., balance=2). The weights reflect the relative bandwidth of the providers connections and should be small numbers since the kernel actually creates additional default routes for each weight increment.


If you are using /etc/shorewall/providers because you have multiple Internet connections, we recommend that you specify balance even if you don't need it. You can still use entries in /etc/shorewall/tcrules and /etc/shorewall/route_rules to force all traffic to one provider or another.


If you don't heed this advice then please read and follow the advice in FAQ 57 and FAQ 58.


If you specify balance and still find that all traffic is going out through only one provider, you may need to install a kernel built with CONFIG_IP_ROUTE_MULTIPATH_CACHED=n. Several users have reported that this change has corrected similar problems.

The SuSE 10.0 kernel is subject to this problem, and a kernel oops may result in this circumstance. SUSE 10.1 and SLES 10 have CONFIG_IP_ROUTE_MULTIPATH_CACHED=n set by default. The source of the problem seems to be an incompatibility between the LARTC patches and CONFIG_IP_ROUTE_MULTIPATH_CACHED.


Do not include routing rules that force traffic whose source IP is an address of the INTERFACE to be routed to this provider. Useful for defining providers that are to be used only when the appropriate packet mark is applied.



This option is deprecated in favor of the optional interface option. That option performs the same function.

Shorewall will determine if this interface is up and has a configured IP address. If it is not, a warning is issued and this provider is not configured.


optional is designed to detect interface states that will cause shorewall start or shorewall restart to fail; just because an interface is in a state that Shorewall can [re]start without error doesn't mean that traffic can actually be sent through the interface.

You can supply an 'isusable' extension script to extend Shorewall's interface state detection. See also the Gateway Monitoring and Failover section below.


Specifies the source address to use when routing to this provider and none is known (the local client has bound to the 0 address). May not be specified when an address is given in the INTERFACE column. If this option is not used, Shorewall substitutes the primary IP address on the interface named in the INTERFACE column.


Specifies the MTU when forwarding through this provider. If not given, the MTU of the interface named in the INTERFACE column is assumed.


Indicates that a default route through the provider should be added to the default routing table (table 253). If a weight is given, a balanced route is added with the weight of this provider equal to the specified weight. If the option is given without a weight, a separate default route is added through the provider's gateway; the route has a metric equal to the provider's NUMBER. The option is ignored with a warning message if USE_DEFAULT_RT=Yes in shorewall.conf.

For those of you who are confused between track and balance:

  • track governs incoming connections (but is also useful for binding long-running connections to the same interface).

  • balance governs outgoing connections.


A comma-separated list of interface names. Wildcards specified using an asterisk ("*") are permitted (e.g., tun* ).

When you specify an existing table in the DUPLICATE column, Shorewall copies all routes through the interface specified in the INTERFACE column plus the interfaces listed in this column. Normally, you will list all interfaces on your firewall in this column except those Internet interfaces specified in the INTERFACE column of entries in this file.

What an entry in the Providers File Does

Adding another entry in the providers file simply creates an alternate routing table for you (see the LARTC Howto). The table will usually contain two routes:

  1. A host route to the specified GATEWAY through the specified INTERFACE.

  2. A default route through the GATEWAY.

Note that the first route is omitted if "-" is specified as the GATEWAY; in that case, the default route does not specify a gateway (point-to-point link).

If the DUPLICATE column is non-empty, then routes from the table named in that column are copied into the new table. By default, all routes (except default routes) are copied. The set of routes copied can be restricted using the COPY column which lists the interfaces whose routes you want copied. You will generally want to include all local interfaces in this list. You should exclude the loopback interface (lo) and any interfaces that do not have an IP configuration. You should also omit interfaces like tun interfaces that are created dynamically. Traffic to networks handled by those interfaces should be routed through the main table using entries in /etc/shorewall/route_rules (see Example 2 below) or by using USE_DEFAULT_RT=Yes.

In addition:

  1. Unless loose is specified, an ip rule is generated for each IP address on the INTERFACE that routes traffic from that address through the associated routing table.

  2. If you specify track, then connections which have had at least one packet arrive on the interface listed in the INTERFACE column have their connection mark set to the value in the MARK column. In the PREROUTING chain, packets with a connection mark have their packet mark set to the value of the associated connection mark; packets marked in this way bypass any prerouting rules that you create in /etc/shorewall/tcrules. This ensures that packets associated with connections from outside are always routed out of the correct interface.

  3. If you specify balance, then Shorewall will replace the 'default' route with weight 100 in the 'main' routing table with a load-balancing route among those gateways where balance was specified. So if you configure default routes, be sure that their weight is less than 100 or the route added by Shorewall will not be used.

That's all that these entries do. You still have to follow the principle stated in the Shorewall Routing documentation:

  1. Routing determines where packets are to be sent.

  2. Once routing determines where the packet is to go, the firewall (Shorewall) determines if the packet is allowed to go there.

The bottom line is that if you want traffic to go out through a particular provider then you must mark that traffic with the provider's MARK value in /etc/shorewall/tcrules and you must do that marking in the PREROUTING chain; or, you must provide the appropriate rules in /etc/shorewall/route_rules.

/etc/shorewall/masq and Multi-ISP

If you masquerade a local network, you will need to add masquerade rules for both external interfaces. Referring to the diagram above, if each of the interfaces has only a single IP address and you have no systems with public IP addresses behind your firewall, then I suggest the following simple entries:


If you have a public subnet (for example behind your firewall, then use exclusion:

#INTERFACE       SOURCE               ADDRESS
eth0             !

Note that exclusion is only used on the interface corresponding to internal subnetwork.

If you have multiple IP addresses on one of your interfaces, you can use a similar technique -- simple exclude the smallest network that contains all of those addresses from being masqueraded.


Entries in /etc/shorewall/masq have no effect on which ISP a particular connection will be sent through. That is rather the purpose of entries in /etc/shorewall/tcrules and /etc/shorewall/route_rules.


One problem that often arises with Multi-ISP configuration is 'Martians'. If your Internet interfaces are configured with the routefilter option in /etc/shorewall/interfaces (remember that if you set that option, you should also select logmartians), then things may not work correctly and you will see messages like this:

Feb  9 17:23:45 gw.ilinx kernel: martian source from, on dev eth1 
Feb  9 17:23:45 gw.ilinx kernel: ll header: 00:a0:24:2a:1f:72:00:13:5f:07:97:05:08:00

The above message is somewhat awkwardly phrased. The source IP in this incoming packet was and the destination IP address was Another gotcha is that the incoming packet has already had the destination IP address changed for DNAT or because the original outgoing connection was altered by an entry in /etc/shorewall/masq (SNAT or Masquerade). So the destination IP address ( may not have been the destination IP address in the packet as it was initially received.

There a couple of common causes for these problems:

  1. You have connected both of your external interfaces to the same hub/switch. Connecting multiple firewall interfaces to a common hub or switch is always a bad idea that will result in hard-to-diagnose problems.

  2. You are specifying both the loose and balance options on your provider(s). This can cause individual connections to ping-pong back and forth between the interfaces which is almost guaranteed to cause problems.

  3. You are redirecting traffic from the firewall system out of one interface or the other using packet marking in your /etc/shorewall/tcrules file. A better approach is to configure the application to use the appropriate local IP address (the IP address of the interface that you want the application to use). See below.

If all else fails, remove the routefilter option from your external interfaces. If you do this, you may wish to add rules to log and drop packets from the Internet that have source addresses in your local networks. For example, if the local LAN in the above diagram is, then you would add this rule:

#ACTION          SOURCE                     DEST
DROP:info        net:         all

Be sure the above rule is added before any other rules with net in the SOURCE column.


The configuration in the figure at the top of this section would be specified in /etc/shorewall/providers as follows.

ISP1    1       1       main            eth0   track,balance    eth2
ISP2    2       2       main            eth1    track,balance    eth2

Other configuration files go something like this:


net      eth0         detect          …          
net      eth1         detect          …


net        net            DROP



Routing a Particular Application Through a Specific Interface

This continues the example in the preceding section.

Now suppose that you want to route all outgoing SMTP traffic from your local network through ISP 2. You would make this entry in /etc/shorewall/tcrules (and if you are running a version of Shorewall earlier than 3.0.0, you would set TC_ENABLED=Yes in /etc/shorewall/shorewall.conf).

#MARK           SOURCE          DEST            PROTO   PORT(S) CLIENT  USER    TEST
#                                                               PORT(S)
2:P             <local network>       tcp     25

Note that traffic from the firewall itself must be handled in a different rule:

#MARK           SOURCE          DEST            PROTO   PORT(S) CLIENT  USER    TEST
#                                                               PORT(S)
2               $FW          tcp     25

Port Forwarding

Shorewall provides considerable flexibility for port forwarding in a multi-ISP environment.

Normal port forwarding rules such as the following will forward from both providers.


#ACTION        SOURCE             DEST              PROTO     DEST PORT(S)     SOURCE       ORIGINAL
#                                                                              PORTS(S)     DEST
DNAT           net                loc:   tcp       25

Continuing the above example, to forward only connection requests from ISP 1, you can either:

  1. Qualify the SOURCE by ISP 1's interface:

    #ACTION        SOURCE             DEST              PROTO     DEST PORT(S)     SOURCE       ORIGINAL
    #                                                                              PORTS(S)     DEST
    DNAT           net:eth0           loc:   tcp       25


  2. Specify the IP address of ISP 1 in the ORIGINAL DEST column:

    #ACTION        SOURCE             DEST              PROTO     DEST PORT(S)     SOURCE       ORIGINAL
    #                                                                              PORTS(S)     DEST
    DNAT           net                loc:   tcp       25               -  

More than 2 Providers

When there are more than two providers, you need to extend the two-provider case in the expected way:

  1. For each external address, you need an entry in /etc/shorewall/masq to handle the case where a connection using that address as the SOURCE is sent out of the interfaces other than the one that the address is configured on.

  2. For each external interface, you need to add an entry to /etc/shorewall/masq.

If we extend the above example to add eth3 with IP address with gateway, then:


ISP1    1       1       main            eth0   track,balance    eth2
ISP2    2       2       main            eth1    track,balance    eth2
ISP3    3       3       main            eth3     track,balance    eth2



Applications running on the Firewall

As noted above, separate entries in /etc/shorewall/tcrules are required for traffic originating from the firewall.

Experience has shown that in some cases, problems occur with applications running on the firewall itself. This is especially true when you have specified routefilter on your external interfaces in /etc/shorewall/interfaces (see above). When this happens, it is suggested that you have the application use specific local IP addresses rather than 0.


  • Squid: In squid.conf, set tcp_outgoing_address to the IP address of the interface that you want Squid to use.

  • In OpenVPN, set local (--local on the command line) to the IP address that you want the server to receive connections on.

Note that some traffic originating on the firewall doesn't have a SOURCE IP address before routing. At least one Shorewall user reports that an entry in /etc/shorewall/route_rules with 'lo' in the SOURCE column seems to be the most reliable way to direct such traffic to a particular ISP.


The route_rules file allows assigning certain traffic to a particular provider just as entries in the tcrules file. The difference between the two files is that entries in route_rules are independent of Netfilter.

Routing Rules

Routing rules are maintained by the Linux kernel and can be displayed using the ip rule ls command. When routing a packet, the rules are processed in turn until the packet is successfully routed.

gateway:~ # ip rule ls
0:      from all lookup local                <=== Local (to the firewall) IP addresses
10001:  from all fwmark 0x1 lookup Blarg     <=== This and the next rule are generated by the
10002:  from all fwmark 0x2 lookup Comcast        'MARK' values in /etc/shorewall/providers.
20000:  from lookup Blarg    <=== This and the next rule are generated unless
20256:  from lookup Comcast           'loose' is specified; based in the output of 'ip addr ls'
32766:  from all lookup main                 <=== This is the routing table shown by 'iproute -n'
32767:  from all lookup default              <=== This table is usually empty
gateway:~ #

In the above example, there are two providers: Blarg and Comcast with MARK 1 going to Blarg and mark 2 going to Comcast.

Columns in the route_rules file

Columns in the file are:

SOURCE (Optional)

An ip address (network or host) that matches the source IP address in a packet. May also be specified as an interface name optionally followed by ":" and an address. If the device 'lo' is specified, the packet must originate from the firewall itself.

DEST (Optional)

An ip address (network or host) that matches the destination IP address in a packet.

If you choose to omit either SOURCE or DEST, place "-" in that column. Note that you may not omit both SOURCE and DEST.


The provider to route the traffic through. May be expressed either as the provider name or the provider number.


The rule's priority which determines the order in which the rules are processed.

1000-1999 Before Shorewall-generated 'MARK' rules

11000- 11999 After 'MARK' rules but before Shorewall-generated rules for ISP interfaces.

26000-26999 After ISP interface rules but before 'default' rule.

Rules with equal priority are applied in the order in which they appear in the file.

Multi-ISP and VPN

For those VPN types that use routing to direct traffic to remote VPN clients (including but not limited to OpenVPN in routed mode and PPTP), the VPN software adds a host route to the main table for each VPN client. The best approach is to use USE_DEFAULT_RT=Yes as described below. If that isn't possible, you must add a routing rule in the 1000-1999 range to specify the main table for traffic addressed to those clients. See Example 2 below.

If you have an IPSEC gateway on your firewall, be sure to arrange for ESP packets to be routed out of the same interface that you have configured your keying daemon to use.


Example 1: You want all traffic entering the firewall on eth1 to be routed through Comcast.

#SOURCE            DEST      PROVIDER        PRIORITY
eth1               -         Comcast         1000

With this entry, the output of ip rule ls would be as follows.

gateway:~ # ip rule ls
0:      from all lookup local
1000:   from all iif eth1 lookup Comcast
10001:  from all fwmark 0x1 lookup Blarg
10002:  from all fwmark 0x2 lookup Comcast
20000:  from lookup Blarg
20256:  from lookup Comcast
32766:  from all lookup main
32767:  from all lookup default
gateway:~ #

Note that because we used a priority of 1000, the test for eth1 is inserted before the fwmark tests.

Example 2: You use OpenVPN (routed setup w/tunX) in combination with multiple providers. In this case you have to set up a rule to ensure that the OpenVPN traffic is routed back through the tunX interface(s) rather than through any of the providers. is the subnet chosen in your OpenVPN configuration (server

#SOURCE                 DEST            PROVIDER        PRIORITY
-                  main            1000


USE_DEFAULT_RT is an option in shorewall.conf (5).

One of the drawbacks of the Multi-ISP support as described in the preceding sections is that changes to the main table made by applications are not added to the individual provider tables. This makes route rules such as described in one of the examples above necessary.

USE_DEFAULT_RT=Yes works around that problem by passing packets through the main table first rather than last. This has a number of implications:

  1. Both the DUPLICATE and the COPY columns in the providers file must remain empty or contain "-". The individual provider routing tables generated when USE_DEFAULT_RT=Yes contain only a host route to the gateway and a default route via the gateway.

  2. The balance option is assumed for all interfaces that do not have the loose option. When you want both balance and loose, both must be specified.

  3. The default route generated by Shorewall is added to the default routing table (253) rather than to the main routing table (254).

  4. Packets are sent through the main routing table by a routing rule with priority 999. In ), the priority range 1-998 may be used for inserting rules that bypass the main table.

  5. You should disable all default route management outside of Shorewall. If a default route is inadvertently added to the main table while Shorewall is started, then all policy routing will stop working except for those routing rules in the priority range 1-998.

  6. For ppp interfaces, the GATEWAY may remain unspecified ("-"). For those interfaces managed by dhcpcd or dhclient, you may specify 'detect' in the GATEWAY column; Shorewall will use the dhcp client's database to determine the gateway IP address. All other interfaces must have a GATEWAY specified explicitly.

Although 'balance' is automatically assumed when USE_DEFAULT_RT=Yes, you can easily cause all traffic to use one provider except when you explicitly direct it to use the other provider via shorewall-route_rules (5) or shorewall-tcrules (5).

Example (send all traffic through the 'shorewall' provider unless otherwise directed).


linksys       1    1    -        wlan0    track,balance=1,optional
shorewall     2    2    -        eth0 track,balance=2,optional


-           -         shorewall       11999

Gateway Monitoring and Failover

There are a couple of options available for monitoring the status of provider links and taking action when a failure occurs.

You specify the optional option in /etc/shorewall/interfaces:

net      eth0         detect          optional         
net      eth1         detect          optional


Shorewall includes a sample monitoring script swping. The swping file is available in the main directory contained in the Shorewall-common tarball and is included in the Shorewall-common documentation directory in the Shorewall-common RPM. The script is inspired by Angsuman Chakraborty's gwping script.


These samples are offered as is — they work for me but I don't make any claim that they will work for anyone else. But if you have a need for automated link monitoring, they offer you a place to start.

The script should be copied to a directory on root's PATH such as /usr/local/sbin/.

The script works by sending pings to target IP addresses through each external interface. These targets must not depend on any routes other than those that are present in the main routing table. That ensures that a route is available to the target even when the target's interface is not working and Shorewall has omitted it from the routing configuration. An interface is assumed to be up when a specified number (UP_COUNT) of consecutive ping operations succeed. Similarly, an interface is assumed to be down when a specified number (DOWN_COUNT) of consecutive ping operations fail. You can specify the interval between pings (PING_INTERVAL).

The script monitors two interfaces but it is a trivial exercise to extend it to more than two. At the top are a number of variables to set:

# IP family -- 4 or 6
# The commands to run when the status of a line changes. Multiple commands may be specified 
# when separated by semicolons (";")

# Interfaces to monitor -- you may use shell variables from your params file
# Sites to Ping. Must depend only on routes in the 'main' routing table. If not specified,
# the interface is assumed to be managed by dhcpcd and the script uses the gateway address
# from /var/lib/dhcpcd/dhcpcd-${IFx}.info
# How often to ping
# Value for ping's -W option
# This many successive pings must succeed for the interface to be marked up when it is down
# This many successive pings must fail for the interface to be marked down when it is up

If you leave COMMAND empty, the script sets its value automatically depending on whether Shorewall-lite is installed.

When the status of an interface changes:

  • For each interface, a file is placed in ${VARDIR} (normally /var/lib/shorewall) to record the status of the interface: either 0 (UP) or 1 (DOWN). The name of the file is interface.status where interface is the interface (e.g., eth0.status).

  • A shorewall -f restart command is executed (shorewall-lite restart, if Shorewall-lite is installed).

  • The contents of the main routing table are displayed.

The .status files are intended to be used with the following /etc/shorewall/isusable script.

local status=0

[ -f ${VARDIR}/${1}.status ] && status=$(cat ${VARDIR}/${1}.status)

return $status

The above script is installed in /etc/shorewall, beginning with Shorewall 4.3.11.

Also included is a sample init script (swping.init) to start the monitoring daemon. Copy it to /etc/init.d/swping and use your distribution's SysV init tools to cause it to be run at boot. It works on OpenSuSE™ 11.0 -- YMMV. Modify the PROG and STATEDIR variables as needed.

As an alternative to using the init script, you can add the following to /etc/shorewall/started:

if [ "$COMMAND" = start ]; then
   killall -9 swping 2> /dev/null #be sure that there are none left running
   /usr/local/sbin/swping &

and add this to /etc/shorewall/stopped.

if [ "$COMMAND" = stop -o "$COMMAND" = clear ]; then
   killall -9 swping 2> /dev/null

This simple script has a number of limitations:

  1. It only works on IPv4 or IPv6 but not both at once. So if you want to monitor both IPv4 and IPv6, you need to clone the script are run two copies; one for IPv4 and one for IPv6.

  2. It can only detect the gateway for interfaces managed by dhcpcd.

  3. It's method of determining whether an interface is up or down is crude. You will normally specify the default gateway for each provider as the sites to ping and being able to ping the default gateway is not a surefire indication that the provider is usable. The method of determining whether a site is up or down is also crude.

  4. Because of the crudeness of the algorithm, hysteresis may occur.

  5. It is tricky to configure a system such that the system works correctly when one of its providers is down unless you largely don't care which interface is used.

Link Status Monitor (LSM)

Link Status Monitor was written by Mika Ilmaranta <ilmis at> and performs more sophisticated monitoring than the simple swping script described in the preceding section.

Like many Open Source products, LSM is poorly documented. It's main configuration file is normally kept in /etc/lsm/lsm.conf, but the file's name is passed as an argument to the lsm program so you can name it anything you want.

The sample lsm.conf included with the product shows some of the possibilities for configuration. One feature that is not mentioned in the sample is that an "include" directive is supported. This allows additional files to be sourced in from the main configuration file.

I personally use LSM here at (configuration is described below). I have set things up so that Shorewall [re]starts lsm during processing of the start and restore commands. I don't have Shorewall restart lsm during Shorewall restart because I restart Shorewall much more often than the average user is likely to do. I have Shorewall start lsm because I have a dynamic IP address from one of my providers (Comcast); Shorewall detects the default gateway to that provider and creates a secondary configuration file (/etc/lsm/shorewall.conf) that contains the link configurations. That file is included by /etc/lsm/lsm.conf.B

Below are my relevant configuration files.


These files only work with Shorewall-perl 4.4 Beta 2 and later.


Note that /etc/lsm/script writes a ${VARDIR}/xxx.status file when the status of an interface changes.

local status=0

[ -f ${VARDIR}/${1}.status ] && status=$(cat ${VARDIR}/${1}.status)

return $status


# Create /etc/lsm/shorewall.conf
# Remove the current interface status files
# Start lsm
start_lsm() {
   killall lsm 2> /dev/null
   cat <<EOF > /etc/lsm/shorewall.conf
connection {

connection {
   rm -f /etc/shorewall/*.status
   /usr/sbin/lsm /etc/lsm/lsm.conf >> /var/log/lsm

eth3 has a dynamic IP address so I need to use the Shorewall-detected gateway address ($ETH3_GATEWAY). I supply a default value in the event that detection fails.


# [re]start lsm if this is a 'start' command or if lsm isn't running
if [ "$COMMAND" = start -o -z "$(ps ax | grep 'lsm ' | grep -v 'grep ' )" ]; then


# Start lsm if it isn't running
if [ -z "$(ps ax | grep 'lsm ' | grep -v 'grep ' )" ]; then


# Defaults for the connection entries
defaults {

include /etc/lsm/shorewall.conf


# (C) 2009 Mika Ilmaranta <>
# (C) 2009 Tom Eastep <>
# License: GPLv2


if [ -f /usr/share/shorewall-lite/lib.base ]; then

[ -f ${STATEDIR}/vardir ] && . ${STATEDIR}/vardir

cat <<EOM | mail -s "${NAME} ${STATE}, DEV ${DEVICE}" ${WARN_EMAIL}


Connection ${NAME} is now ${STATE}.

Following parameters were passed:
newstate     = ${STATE}
name         = ${NAME}
checkip      = ${CHECKIP}
device       = ${DEVICE}
warn_email   = ${WARN_EMAIL}

Packet counters:
replied      = ${REPLIED} packets replied
waiting      = ${WAITING} packets waiting for reply
timeout      = ${TIMEOUT} packets that have timed out (= packet loss)
reply_late   = ${REPLY_LATE} packets that received a reply after timeout
cons_rcvd    = ${CONS_RCVD} consecutively received replies in sequence
cons_wait    = ${CONS_WAIT} consecutive packets waiting for reply
cons_miss    = ${CONS_MISS} consecutive packets that have timed out
avg_rtt      = ${AVG_RTT} average rtt, notice that waiting and timed out packets have rtt = 0 when calculating this

Your LSM Daemon


[ ${STATE} = up ] && state=0 || state=1

echo $state > ${VARDIR}/${DEVICE}.status

/sbin/shorewall -f restart >> /var/log/lsm 2>&1

/sbin/shorewall show routing >> /var/log/lsm

exit 0;



Two Providers Sharing an Interface

Shared interface support has the following characteristics:

  1. Only Ethernet (or Ethernet-like) interfaces can be used. For inbound traffic, the MAC addresses of the gateway routers are used to determine which provider a packet was received through. Note that only routed traffic can be categorized using this technique.

  2. You must specify the address on the interface that corresponds to a particular provider in the INTERFACE column by following the interface name with a colon (":") and the address.

  3. Entries in /etc/shorewall/masq must be qualified by the provider name (or number).

  4. This feature requires Realm Match support in your kernel and iptables.

  5. You must add route_rules entries for networks that are accessed through a particular provider.

  6. If you have additional IP addresses through either provider, you must add route_rules to direct traffic FROM each of those addresses through the appropriate provider.

  7. You must manually add MARK rules for traffic known to come from each provider.

  8. You must specify a gateway IP address in the GATEWAY column of /etc/shorewall/providers; detect is not permitted.

Taken together, b. and h. effectively preclude using this technique with dynamic IP addresses.


This is our home network circa fall 2008. We have two Internet providers:

  1. Comcast -- Cable modem with one dynamic IP address.

  2. Avvanta -- ADSL with 5 static IP addresses.

Because the old CompaqPresario™ that I use for a firewall only has three PCI slots and no onboard Ethernet, it doesn't have enough Ethernet controllers to support both providers. So I use a Linksys WRT300n pre-N router as a gateway to Comcast. Note that because the Comcast IP address is dynamic, I could not share a single firewall interface between the two providers directly.

On my personal laptop (ursa), I have 9 virtual machines running various Linux distributions. It is the Shorewall configuration on ursa that I will describe here.

Below is a diagram of our network:

The local wired network in my office is connected to both gateways and uses the private (RFC 1918) network The Comcast gateway has local IP address while the Avvanta gateway has local IP address Ursa's eth0 interface has a single IP address (

This configuration uses USE_DEFAULT_RT=Yes in shorewall.conf (see above).

Here is the providers file:

#NAME          NUMBER   MARK DUPLICATE INTERFACE            GATEWAY      OPTIONS                            COPY
comcast        1        1    -         eth0:   track,loose,balance,optional
avvanta        2        2    -         eth0: track,optional,loose
wireless       3        3    -         wlan0         track,optional

Several things to note:

  1. is specified as the eth0 IP address for both providers.

  2. Both wired providers have the loose option. This prevents Shorewall from automatically generating routing rules based on the source IP address.

  3. Only comcast has the balance option. With USE_DEFAULT_RT=yes, that means that comcast will be the default provider. While balance is the default, with USE_DEFAULT_RT=Yes, it must be specified explicitly when loose is also specified.

  4. I always disable the wireless interface when the laptop is connected to the wired network.

  5. I use a different Shorewall configuration when I take the laptop on the road.

Here is the route_rules file:

#SOURCE                 DEST                    PROVIDER        PRIORITY
-                   avvanta         1000
-                   avvanta         1000
-                   avvanta         1000

Those rules direct traffic to the five static Avvanta IP addresses (only two are currently used) through the avvanta provider.

Here is the tcrules file (MARK_IN_FORWARD_CHAIN=No in shorewall.conf):

#MARK           SOURCE          DEST            PROTO   PORT(S)         CLIENT  USER    TEST    LENGTH  TOS     CONNBYTES       HELPER
#                                                                       PORT(S) 
2               $FW          tcp     21
2               $FW          tcp     -               -       -       -       -       -       -               ftp
2               $FW          tcp     119

These rules:

  • Use avvanta for FTP.

  • Use avvanta for NTTP

The remaining files are for a rather standard two-interface config with a bridge as the local interface.


#ZONE   IPSEC   OPTIONS                 IN                      OUT
#       ONLY                            OPTIONS                 OPTIONS
fw      firewall
net     ipv4
kvm     ipv4


net             net             NONE
fw              net             ACCEPT
fw              kvm             ACCEPT
kvm             all             ACCEPT
net             all             DROP            info
all             all             REJECT          info


net     eth0            detect          dhcp,tcpflags,routefilter,blacklist,logmartians,optional,arp_ignore
net     wlan0           detect          dhcp,tcpflags,routefilter,blacklist,logmartians,optional
kvm     br0             detect          routeback       #Virtual Machines


wlan0 is the wireless adapter in the notebook. Used when the laptop is in our home but not connected to the wired network.


#INTERFACE              SUBNET          ADDRESS         PROTO   PORT(S) IPSEC


Because the firewall has only a single external IP address, I don't need to specify the providers in the masq rules.

A Complete Working Example

This section describes the network at early in 2009. The configuration is as follows:

  • Two providers:

    • Avvanta -- A slow (1.5mb/384kb) DSL service with 5 static IP addresses.

    • Comcast -- A fast (20mb/10mb) Cable circuit with a single dynamic address.

  • A local network consisting of wired and wireless client systems. A Linksys WRT300N wireless router is used as an access point for the wireless hosts.

  • A DMZ hosting a single server ( aka,,etc.)

The network is pictured in the following diagram:

Because of the speed of the cable provider, all traffic uses that provider unless there is a specific need for the traffic to use the DSL line.

  • Responses to connections from the Internet to one of the DSL IP addresses -- the track option takes care of that.

  • Connections initiated by the server and connections requested by clients on the firewall that have bound their local socket to one of the DSL IP addresses. Two entries in /etc/shorewall/route_rules take care of that traffic.

As a consequence, I have disabled all route filtering on the firewall and only use the balance option in /etc/shorewall/providers on the Comcast provider whose default route in the main table is established by DHCP. By specifying the fallback option on Avvanta, I ensure that there is still a default route if Comcast is down. lsm is used to monitor the links.


net.ipv4.conf.all.rp_filter = 0



RESTORE_DEFAULT_ROUTE=No causes the default route in the main table to be deleted when the Comcast link is unavailable. That way, the default route in the default table will be used until Comcast is available again.


Avvanta     1 0x100 main      eth0 track,loose,fallback    eth2,eth4,tun*
Comcast     2 0x200 main      eth3      detect          track,balance           eth2,eth4,tun*

The loose option on Avvanta results in fewer routing rules. The first two routing rules below insure that all traffic from Avvanta-assigned IP addresses is sent via the Avvanta provider. The 'tun*' included in the COPY column is there because I run a routed OpenVPN server on the firewall.


-          main     1000    # Addresses assigned by routed OpenVPN server -              Avvanta  26000    -              Avvanta  26000
-           Avvanta  26000   # Avvanta NNTP Server -- verifies source IP address

The /etc/shorewall/route_rules entries provide all of the provider selection necessary so my /etc/shorewall/tcrules file is used exclusively for traffic shaping of the Avvanta line. Note that I still need to provide values in the MARK colum of /etc/shorewall/providers because I specify track on both providers.

Here is the output of shorewall show routing:

Routing Rules

0:     from all lookup local
1000:  from all to lookup main 
10000: from all fwmark 0x100 lookup Avvanta 
10001: from all fwmark 0x200 lookup Comcast 
20256: from lookup Comcast 
26000: from lookup Avvanta 
26000: from lookup Avvanta 
26000: from all to lookup Avvanta 
32766: from all lookup main 
32767: from all lookup default 

Table Avvanta: dev eth0  scope link  src dev eth4  scope link dev eth2  proto kernel  scope link  src dev eth0  proto kernel  scope link  src dev eth0  scope link 
default via dev eth0  src 

Table Comcast: dev eth4  scope link dev eth3  scope link  src dev eth2  proto kernel  scope link  src dev eth3  proto kernel  scope link  src 
default via dev eth3  src 

Table default:

default via dev eth0 metric 1

Table local:

broadcast dev lo  proto kernel  scope link  src 
broadcast dev eth2  proto kernel  scope link  src 
broadcast dev eth0  proto kernel  scope link  src 
local dev eth0  proto kernel  scope host  src 
local dev eth0  proto kernel  scope host  src 
local dev eth0  proto kernel  scope host  src 
local dev eth4  proto kernel  scope host  src 
broadcast dev eth3  proto kernel  scope link  src 
broadcast dev eth3  proto kernel  scope link  src 
local dev eth2  proto kernel  scope host  src 
local dev lo  proto kernel  scope host  src 
broadcast dev eth2  proto kernel  scope link  src 
local dev eth3  proto kernel  scope host  src 
broadcast dev eth0  proto kernel  scope link  src 
broadcast dev lo  proto kernel  scope link  src 
local dev eth0  proto kernel  scope host  src 
local dev lo  proto kernel  scope host  src 
local dev lo  proto kernel  scope host  src 

Table main: dev eth4  scope link dev eth2  proto kernel  scope link  src dev eth0  proto kernel  scope link  src dev eth3  proto kernel  scope link  src dev eth0  scope link dev lo  scope link 
default via dev eth3 


loc      eth2              detect              dhcp,routeback
dmz      eth4              detect
net      eth0              detect              dhcp,blacklist,tcpflags,optional
net      eth3              detect              dhcp,blacklist,tcpflags,optional



COMMENT Masquerade Local Network
eth0        !


All traffic leaving eth3 must use the dynamic IP address assigned to that interface as the SOURCE address. All traffic leaving eth0 that does not have a SOURCE address falling within the Avvanta subnet ( must have its SOURCE address changed to