# Shorewall-perl

Tom
Eastep
2007
Thomas M. 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.

## Shorewall-perl - What is it?

Shorewall-perl is a companion product to Shorewall. It requires Shorewall 3.4.2 or later.

Shorewall-perl contains a re-implementation of the Shorewall compiler written in Perl. The advantages of using Shorewall-perl are over Shorewall-shell (the shell-based compiler included in earlier Shorewall 3.x releases) are:

- The Shorewall-perl compiler is much faster.

- The script generated by the compiler uses `iptables-restore` to instantiate the Netfilter configuration. So it runs much faster than the script generated by the Shorewall-shell compiler.

- The Shorewall-perl compiler does more thorough checking of the configuration than the Shorewall-shell compiler does.

- The error messages produced by the compiler are better, more consistent and always include the file name and line number where the error was detected.

- Going forward, the Shorewall-perl compiler will get all enhancements; the Shorewall-shell compiler will only get those enhancements that are easy to retrofit.

## Shorewall-perl - The down side

While there are advantages to using Shorewall-perl, there are also disadvantages:

- There are a number of incompatibilities between the Shorewall-perl compiler and the earlier one.

  1. The Perl-based compiler requires the following capabilities in your kernel and iptables.

     - addrtype match (may be relaxed later)

     - multiport match (will not be relaxed)

     These capabilities are in current distributions.

  2. Now that Netfilter has features to deal reasonably with port lists, I see no reason to duplicate those features in Shorewall. The Shorewall-shell compiler goes to great pain (in some cases) to break very long port lists ( > 15 where port ranges in lists count as two ports) into individual rules. In the new compiler, I'm avoiding the ugliness required to do that. The new compiler just generates an error if your list is too long. It will also produce an error if you insert a port range into a port list and you don't have extended multiport support.

  3. BRIDGING=Yes is not supported. The kernel code necessary to support this option was removed in Linux kernel 2.6.20.

  4. The BROADCAST column in the interfaces file is essentially unused; if you enter anything in this column but '-' or 'detect', you will receive a warning. This will be relaxed if and when the addrtype match requirement is relaxed.

  5. Because the compiler is now written in Perl, your compile-time extension scripts from earlier versions will no longer work. For now, if you want to use extension scripts, you will need to read the Perl code to see how the compiler operates internally. I will produce documentation before the first official release. Compile-time extension scripts are executed using the Perl 'do FILE' mechanism.

  6. The 'refresh' command is now synonymous with 'restart'.

  7. Because the compiler is now written in Perl, your compile-time extension scripts from earlier versions will no longer work. Compile-time extension scripts are executed using the Perl 'eval `cat <file>`' mechanism. Be sure that each script returns a 'true' value; otherwise, the compiler will assume that the script failed and will abort the compilation.

     When a script is invoked, the $chainref scalar variable will hold a reference to a chain table entry.

     - $chainref->{name} contains the name of the chain
     - $chainref->{table} holds the table name

     To add a rule to the chain:

     - add_rule $chainref, <the rule>

     Where

     - <the rule> is a scalar argument holding the rule text. Do not include "-A <chain name>"

     Example:

     - add_rule $chainref, '-j ACCEPT';

     To insert a rule into the chain:

     - insert_rule $chainref, <rulenum>, <the rule>

     The log_rule_limit function works like it does in the shell compiler with two exceptions:

     - You pass the chain reference rather than the name of the chain.

     - The commands are 'add' and 'insert' rather than '-A' and '-I'.

     - There is only a single "pass as-is to iptables" argument (so you must quote that par

     Example:

             log_rule_limit
                       'info' ,
                       $chainref ,
                       $chainref->{name},
                       'DROP' ,
                       '',    #Limit
                       '' ,   #Log tag
                       'add';

  8. The `/etc/shorewall/tos` file now has zone-independent SOURCE and DEST columns as do all other files except the rules and policy files.

     The SOURCE column may be one of the following:

     - [`all`:]<address>[,...]
     - [`all`:]<interface>[:<address>[,...]]
     - `$FW`[:<address>[,...]]

     The DEST column may be one of the following:

     - [`all`:]<address>[,...]
     - [`all`:]<interface>[:<address>[,...]]

     This is a permanent change. The old zone-based rules have never worked right and this is a good time to replace them. I've tried to make the new syntax cover the most common cases without requiring change to existing files. In particular, it will handle the tos file released with Shorewall 1.4 and earlier.

  9. Currently, support for ipsets is untested. That will change with future pre-releases but one thing is certain -- Shorewall is now out of the ipset load/reload business. With scripts generated by the Perl-based Compiler, the Netfilter ruleset is never cleared. That means that there is no opportunity for Shorewall to load/reload your ipsets since that cannot be done while there are any current rules using ipsets.

     So:

     1. Your ipsets must be loaded before Shorewall starts. You are free to try to do that with the following code in `/etc/shorewall/start`:

            if [ "$COMMAND" = start ]; then
                ipset -U :all: :all:
                ipset -F
                ipset -X
                ipset -R < /etc/shorewall/ipsets
            fi

        The file `/etc/shorewall/ipsets` will normally be produced using the `ipset -S` command.

        The above will work most of the time but will fail in a `shorewall stop` - `shorewall start` sequence if you use ipsets in your routestopped file (see below).

     2. Your ipsets may not be reloaded until Shorewall is stopped or cleared.

     3. If you specify ipsets in your routestopped file then Shorewall must be cleared in order to reload your ipsets.

     As a consequence, scripts generated by the Perl-based compiler will ignore `/etc/shorewall/ipsets` and will issue a warning if you set SAVE_IPSETS=Yes in `shorewall.conf`.

  10. Because the configuration files (with the exception of `/etc/shorewall/params`) are now processed by the Shorewall-perl compiler rather than by the shell, only the basic forms of Shell expansion ($variable and ${variable}) are supported. The more exotic forms such as ${variable:=default} are not supported. Both variables defined in /etc/shorewall/params and environmental variables (exported by the shell) can be used in configuration files.

  11. USE_ACTIONS=No is not supported. That option is intended to minimize Shorewall's footprint in embedded applications. As a consequence, Default Macros are not supported.

  12. DELAYBLACKLISTLOAD=Yes is not supported. The entire ruleset is atomically loaded with one execution of `iptables-restore`.

  13. MAPOLDACTIONS=Yes is not supported. People should have converted to using macros by now.

  14. The pre Shorewall-3.0 format of the zones file is not supported; neither is the `/etc/shorewall/ipsec` file.

  15. BLACKLISTNEWONLY=No is not permitted with FASTACCEPT=Yes. This combination doesn't work in previous versions of Shorewall so the Perl-based compiler simply rejects it.

  16. Shorewall-perl has a single rule generator that is used for all rule-oriented files. So it is important that the syntax is consistent between files.

      With shorewall-shell, there is a special syntax in the SOURCE column of /etc/shorewall/masq to designate "all traffic entering the firewall on this interface except...".

      Example:

          #INTERFACE       SOURCE                  ADDRESSES
          eth0             eth1!192.168.4.9        ...

      Shorewall-perl uses syntax that is consistent with the rest of Shorewall:

          #INTERFACE       SOURCE                  ADDRESSES
          eth0             eth1:!192.168.4.9       ...

  17. The 'allowoutUPnP' built-in action is no longer supported. In kernel 2.6.14, the Netfilter team have removed support for '-m owner --owner-cmd' which that action depended on.

## Shorewall-perl - Prerequisites

In addition to Shorewall-3.4.2 or later, you need:

- Perl (I use Perl 5.8.8 but other versions should work fine)

- Perl Cwd Module

- Perl File::Basename Module

- Perl File::Temp Module

## Shorewall-perl - Installation

Shorewall-perl is still part of the current development release. Use it at your own risk.

Either

    tar -jxf shorewall-perl-3.9.x.tar.bz2
    cd shorewall-perl-3.9.x
    ./install.sh

or

    rpm -ivh shorewall-pl-3.9.x-1.noarch.rpm

Note that you can also install the Shorewall 3.9.x version of Shorewall. If you do that and still want access to the legacy shell-based compiler, you must also install the Shorewall-shell package.

## Using Shorewall-perl

### Using Shorewall-perl under Shorewall 3.4.x

By default, the Shorewall-shell compiler will be used.

To use the Shorewall-perl compiler, add this to `shorewall.conf`:

SHOREWALL_COMPILER=perl

If you add this setting to `/etc/shorewall/shorewall.conf` then by default, the new compiler will be used on the system. If you add it to `shorewall.conf` in a separate directory (such as a Shorewall-lite export directory) then the new compiler will only be used when you compile from that directory.

Regardless of the setting of SHOREWALL_COMPILER, there is one change in Shorewall operation that is triggered simply by installing Shorewall-perl. Your params file will be processed with the shell's '-a' option which causes any variables that you set or create in that file to be automatically exported. Since the params file is processed before `shorewall.conf`, using -a insures that the settings of your params variables are available to the new compiler should it's use be specified in `shorewall.conf`.

### Using Shorewall-perl under Shorewall 3.9.x

If you only install one compiler, then that compiler will be used.

If you install both compilers, then the compiler actually used depends on the SHOREWALL_COMPILER setting in `shorewall.conf`.

The value of this new option can be either 'perl' or 'shell'.

If you add 'SHOREWALL_COMPILER=perl' to `/etc/shorewall/shorewall.conf` then by default, the new compiler will be used on the system. If you add it to `shorewall.conf` in a separate directory (such as a Shorewall-lite export directory) then the new compiler will only be used when you compile from that directory.

If you only install one compiler, it is suggested that you do not set SHOREWALL_COMPILER.

If you install Shorewall-perl under Shorewall 3.9.2 or later, you can select the compiler to use on the command line using the 'C option:

- '-C shell' means use the shell compiler
- '-C perl' means use the perl compiler

The -C option overrides the setting in shorewall.conf.

Example:

    shorewall restart -C perl

Regardless of the setting of SHOREWALL_COMPILER, there is one change in Shorewall operation that is triggered simply by installing shorewall-perl. Your params file will be processed during compilation with the shell's '-a' option which causes any variables that you set or create in that file to be automatically exported. Since the params file is processed before shorewall.conf, using -a insures that the settings of your params variables are available to the new compiler should its use be specified in shorewall.conf.