Sophie

Sophie

distrib > Mandriva > mes5 > x86_64 > by-pkgid > 45723c51178a73df679c2a8284d8eeff > files > 35

shorewall-doc-4.0.15-0.2mdvmes5.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Packet Marking using /etc/shorewall/tcrules</title><link rel="stylesheet" href="html.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /></head><body><div class="article" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="id257527"></a>Packet Marking using /etc/shorewall/tcrules</h2></div><div><div class="authorgroup"><div class="author"><h3 class="author"><span class="firstname">Tom</span> <span class="surname">Eastep</span></h3></div></div></div><div><p class="copyright">Copyright © 2006 Thomas M. Eastep</p></div><div><div class="legalnotice"><a id="id292634"></a><p>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
      “<span class="quote"><a class="ulink" href="GnuCopyright.htm" target="_self">GNU Free Documentation
      License</a></span>”.</p></div></div><div><p class="pubdate">2008/12/15</p></div></div><hr /></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#Marks">Packet and Connection Marks</a></span></dt><dt><span class="section"><a href="#Programs">Packet Marking "Programs"</a></span></dt><dt><span class="section"><a href="#Values">Mark and Mask Values</a></span></dt><dt><span class="section"><a href="#Shorewall">Shorewall-defined Chains in the Mangle Table</a></span></dt><dt><span class="section"><a href="#Examples">An Example</a></span></dt><dt><span class="section"><a href="#Show">Examining the Marking Programs on a Running System</a></span></dt></dl></div><div class="caution" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Caution</h3><p>This article includes information that applies to Shorewall version
    3.2.5 and later. Not all features described here will be available in
    earlier releases.</p></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="Marks"></a>Packet and Connection Marks</h2></div></div></div><p>Perhaps no aspect of Shorewall causes more confusion than packet
    marking. This article will attempt to clear up some of that
    confusion.</p><p>Each packet has a mark whose value is initially 0. Mark values are
    stored in the <span class="emphasis"><em>skb</em></span> (socket buffer) structure used by
    the Linux kernel to track packets; the mark value is not part of the
    packet itself and cannot be seen with <span class="command"><strong>tcpdump</strong></span>,
    <span class="command"><strong>ethereal</strong></span> or any other packet sniffing program.</p><p>Each active connection (even those that are not yet in ESTABLISHED
    state) has a mark value that is distinct from the packet marks. Connection
    mark values can be seen using the <span class="command"><strong>shorewall show
    connections</strong></span> command. The default connection mark value is
    0.</p><p>Example (output has been folded for display ):</p><pre class="programlisting"><span class="command"><strong>shorewall show connections</strong></span>
Shorewall 3.3.2 Connections at gateway - Mon Oct  2 09:08:18 PDT 2006

tcp      6 19 TIME_WAIT src=206.124.146.176 dst=192.136.34.98 sport=58597 dport=80
         packets=23 bytes=4623 src=192.136.34.98 dst=206.124.146.176 sport=80 dport=58597
         packets=23 bytes=22532 [ASSURED] <span class="bold"><strong>mark=256</strong></span> use=1
…</pre><p>Packet marks are valid only while the packet is being processed by
    the firewall. Once the packet has been given to a local process or sent on
    to another system, the packet's mark value is no longer available.
    Connection mark values, on the other hand, persist for the life of the
    connection.</p><div class="important" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Important</h3><p>Other parts of the system such as <a class="ulink" href="traffic_shaping.htm" target="_self">Traffic Shaping</a> and <a class="ulink" href="MultiISP.html" target="_self">Policy Routing</a> cannot use connection marks —
      they can only use packet marks.</p></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="Programs"></a>Packet Marking "Programs"</h2></div></div></div><p>Packet marking occurs in Netfilter's <span class="emphasis"><em>mangle</em></span>
    table. See the <a class="ulink" href="NetfilterOverview.html" target="_self">Netfilter
    Overview</a> article.</p><p>You can think of entries in the tcrules file like instructions in a
    program coded in a crude assembly language. The program gets executed for
    each packet.</p><p>That is another way of saying that <span class="bold"><strong>if you
    don't program, you may have difficulty making full use of
    Netfilter/Shorewall's Packet Marking</strong></span>.</p><p>Actually, the tcrules define several programs. Each program
    corresponds to one of the built-in chains in the mangle table.</p><div class="itemizedlist"><ul type="disc"><li><p>PREROUTING program — If MARK_IN_FORWARD_CHAIN=No in
        <code class="filename">shorewall.conf</code>, then by default entries in
        <code class="filename">/etc/shorewall/tcrules</code> are part of the PREROUTING
        program. Entries specifying the ":P" suffix in the MARK column are
        also part of the PREROUTING program. The PREROUTING program gets
        executed for each packet entering the firewall.</p></li><li><p>FORWARD program — If MARK_IN_FORWARD_CHAIN=Yes in
        <code class="filename">shorewall.conf</code>, then by default entries in
        <code class="filename">/etc/shorewall/tcrules</code> are part of the FORWARD
        program. Entries specifying the ":F" suffix in the MARK column are
        also part of the FORWARD program. The FORWARD program gets executed
        for each packet forwarded by the firewall.</p></li><li><p>OUTPUT program — Entries with $FW in the SOURCE column are part
        of the OUTPUT program. The OUTPUT program is executed for each packet
        originating on the firewall itself.</p></li><li><p>POSTROUTING program — Entries with a class-id in the MARK column
        (and that don't specify $FW in the SOURCE column) are part of the
        POSTROUTING program. These rules are executed for each packet leaving
        the firewall. Entries specifying the ":T" suffix in the MARK column
        are also part of the POSTROUTING program (Shorewall version 3.4.0 and
        later).</p></li><li><p>INPUT program — No entries in tcrules will add entries to this
        program. It is executed for each packet that is targeted to the
        firewall itself.</p></li></ul></div><p>Note that a packet being forwarded by your firewall actually gets
    processed by three different programs: PREROUTING, FORWARD and
    POSTROUTING. Similarly, packets addressed to the firewall itself are
    processed by two programs (PREROUTING and INPUT) while packets originating
    on the firewall are likewise processed by two programs (OUTPUT and
    POSTROUTING).</p><p>Rules in each program are <span class="emphasis"><em>executed</em></span> as
    follows:</p><div class="itemizedlist"><ul type="disc"><li><p>Rules are conditionally executed based on whether the current
        packet matches the contents of the SOURCE, DEST, PROTO, PORT(S),
        CLIENT PORT(S_, USER, TEST, LENGTH and TOS columns.</p></li><li><p>When a rule is executed, either:</p><div class="orderedlist"><ol type="1"><li><p>the current packet receives a new mark value; or</p></li><li><p>the connection to which the current packet belongs receives
            a new mark value (":C", ":CF" or ":CP" suffix in the MARK column);
            or</p></li><li><p>the packet is classified for traffic shaping (class-id in
            the MARK column); or</p></li><li><p>the packet mark in the current packet is moved to the
            connection mark for the connection that the current packet is part
            of ("SAVE" in the MARK column); or</p></li><li><p>the connection mark value for the connection that the
            current packet is part of is moved to the current packet's mark
            ("RESTORE" in the MARK column); or</p></li><li><p>jump to a subroutine (another chain in the mangle table).
            These jumps are generated by Shorewall; or</p></li><li><p>exit the current subroutine ("CONTINUE" in the MARK
            column).</p></li></ol></div></li><li><p>Unless the subroutine is exited using CONTINUE, <span class="bold"><strong>the current packet is always passed to the next tcrule in
        the subroutine</strong></span>.</p></li></ul></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="Values"></a>Mark and Mask Values</h2></div></div></div><p>The mark value is held in a 32-bit field. Because packet marking is
    the Netfilter <span class="emphasis"><em>kludge of last resort</em></span> for solving many
    hard technical problems, Shorewall reserves half of this field (16 bits)
    for future use. The remainder is split into two 8-bit values:</p><div class="itemizedlist"><ul type="disc"><li><p>The low-order eight bits are used for traffic shaping marks.
        These eight bits are also used for selecting among multiple providers
        when HIGH_ROUTE_MARKS=No in <code class="filename">shorewall.conf</code>. Some
        rules that deal with only these bits use a mask value of 0xff.</p></li><li><p>The next 8 bits are used for selecting among multiple providers
        when HIGH_ROUTE_MARKS=Yes in <code class="filename">shorewall.conf</code>.
        These bits are manipulated using a mask value of 0xff00.</p></li></ul></div><p>As hinted above, marking rules can specify both a mark value and a
    mask. The mask determines the subset of the 32 bits in the mark to be used
    in the operation — only those bits that are on in the mask are manipulated
    when the rule is executed. For entries in tcrules, Shorewall-generated
    rules use a mask value that depends on which program the rule is part of,
    what the rule does, and the setting of HIGH_ROUTE_MARKS.</p><p>For entries in tcrules, the default mask value is 0xffff except in
    these cases:</p><div class="itemizedlist"><ul type="disc"><li><p>RESTORE rules use a default mask value of 0xff.</p></li><li><p>SAVE rules use a default mask value of 0xff.</p></li><li><p>Connection marking rules use a mask value of 0xff.</p></li></ul></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="Shorewall"></a>Shorewall-defined Chains in the Mangle Table</h2></div></div></div><p>Shorewall creates a set of chains in the mangle table to hold rules
    defined in your <em class="firstterm">/etc/shorewall/tcrules</em> file. As
    mentioned above, chains are like subroutines in the packet marking
    programming language. By placing all of your rules in subroutines,
    CONTINUE (which generates a Netfilter RETURN rule) can be used to stop
    processing your rules while still allowing following Shorewall-generated
    rules to be executed.</p><div class="variablelist"><dl><dt><span class="term">tcpre</span></dt><dd><p>PREROUTING rules.</p></dd><dt><span class="term">tcfor</span></dt><dd><p>FORWARD rules.</p></dd><dt><span class="term">tcout</span></dt><dd><p>OUTPUT rules.</p></dd><dt><span class="term">tcpost</span></dt><dd><p>POSTROUTING rules.</p></dd></dl></div><p>Shorewall generates jumps to these chains from the built-in chains
    (PREROUTING, FORWARD, etc.).</p></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="Examples"></a>An Example</h2></div></div></div><p>Here's the example (slightly expanded) from the comments at the top
    of the <code class="filename">/etc/shorewall/tcrules</code> file.</p><pre class="programlisting">#MARK    SOURCE          DEST            PROTO   PORT(S) CLIENT  USER    TEST    LENGTH  TOS
#                                                        PORT(S)
1        0.0.0.0/0       0.0.0.0/0       icmp    echo-request                 #Rule 1
1        0.0.0.0/0       0.0.0.0/0       icmp    echo-reply                   #Rule 2
1        $FW             0.0.0.0/0       icmp    echo-request                 #Rule 3
1        $FW             0.0.0.0/0       icmp    echo-reply                   #Rule 4

RESTORE  0.0.0.0/0       0.0.0.0/0       all     -       -       -       0    #Rule 5
CONTINUE 0.0.0.0/0       0.0.0.0/0       all     -       -       -       !0   #Rule 6
4        0.0.0.0/0       0.0.0.0/0       ipp2p:all                            #Rule 7
SAVE     0.0.0.0/0       0.0.0.0/0       all     -       -       -       !0   #Rule 8
##LAST LINE -- ADD YOUR ENTRIES BEFORE THIS ONE -- DO NOT REMOVE</pre><p>Let's take a look at each rule:</p><div class="orderedlist"><ol type="1"><li><p>This straight-forward rule simply marks all 'ping' requests
        passing through the firewall with mark value 1. Note that it does not
        mark pings that originate on the firewall itself.</p></li><li><p>Similarly, this rule marks 'ping' replies.</p></li><li><p>This rule marks 'ping' requests that originate on the firewall.
        This rule and the next ones are part of the OUTPUT program.</p></li><li><p>Similarly, this rule marks 'ping' replies from the firewall
        itself.</p></li><li><p>Remember that even though 'ping' packets were marked in one of
        the first two rules, they are still passed on to rule 5 (note that
        packets marked by rules 3 and 4 are not processed by this rule since
        it is in a different program). That rule moves the connection mark to
        the packet mark, <span class="emphasis"><em>if the packet mark is still zero</em></span>
        (note the '0' in the TEST column). Without the '0' in the TEST column,
        this rule would overwrite the marks assigned in the first two
        rules.</p></li><li><p>If the packet mark is non-zero (note the '!0' in the TEST
        column), then exit — The remaining rules will not be executed in this
        case. The packet mark will be non-zero if this is a 'ping' packet, or
        if the connection mark restored in rule 5 was non-zero.</p></li><li><p>The packet mark is still zero. This rule checks to see if this
        is a P2P packet and if it is, the packet mark is set to 4.</p></li><li><p>If the packet mark is non-zero (meaning that it was set to 4 in
        rule 7), then save the value (4) in the connection. The next time that
        a packet from this same connection comes through this program, rule 6
        will be executed and the P2P check will be avoided.</p></li></ol></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="Show"></a>Examining the Marking Programs on a Running System</h2></div></div></div><p>You can see the tcrules in action using the <span class="command"><strong>shorewall show
    mangle</strong></span> command.</p><p>The sample output from that command shown below has the following in
    <code class="filename">/etc/shorewall/providers</code>:</p><pre class="programlisting">#NAME   NUMBER  MARK    DUPLICATE       INTERFACE       GATEWAY         OPTIONS         COPY
Blarg   1       0x100   main            eth3            206.124.146.254 track,balance   br0,eth1
#LAST LINE -- ADD YOUR ENTRIES ABOVE THIS LINE -- DO NOT REMOVE
</pre><p>Here is <code class="filename">/etc/shorewall/tcrules</code>:</p><pre class="programlisting">#MARK   SOURCE          DEST            PROTO   PORT(S) CLIENT  USER    TEST
#                                                       PORT(S)
1:110   192.168.0.0/22  eth3                                            #Our internal nets get priority
                                                                        #over the server
1:130   206.124.146.177 eth3            tcp     -       873
#LAST LINE -- ADD YOUR ENTRIES BEFORE THIS ONE -- DO NOT REMOVE
</pre><p>And here is <code class="filename">/etc/shorewall/tcdevices</code> and
    <code class="filename">/etc/shorewall/tcclasses</code>:</p><pre class="programlisting">#INTERFACE      IN-BANDWITH     OUT-BANDWIDTH
eth3            1.3mbit         384kbit
#LAST LINE -- ADD YOUR ENTRIES BEFORE THIS ONE -- DO NOT REMOVE

#INTERFACE      MARK    RATE            CEIL            PRIORITY        OPTIONS
eth3            10      full            full            1               tcp-ack,tos-minimize-delay
eth3            20      9*full/10       9*full/10       2               default
eth3            30      6*full/10       6*full/10       3
#LAST LINE -- ADD YOUR ENTRIES BEFORE THIS ONE -- DO NOT REMOVE
</pre><p>I've annotated the following output with comments beginning with
    "&lt;&lt;&lt;&lt;" and ending with "&gt;&gt;&gt;&gt;". This example uses
    HIGH_ROUTE_MARKS=Yes and TC_EXPERT=No in
    <code class="filename">shorewall.conf</code>.</p><pre class="programlisting">gateway:~ # <span class="command"><strong>shorewall show mangle</strong></span>
Shorewall 3.3.2 Mangle Table at gateway - Mon Oct  2 15:07:32 PDT 2006

Counters reset Mon Oct  2 07:49:52 PDT 2006

&lt;&lt;&lt;&lt; The PREROUTING Program &gt;&gt;&gt;&gt;

Chain PREROUTING (policy ACCEPT 409K packets, 122M bytes)
 pkts bytes target     prot opt in     out     source               destination

&lt;&lt;&lt;&lt; Restore the provider mark from the connection, if any &gt;&gt;&gt;&gt;

 185K   77M CONNMARK   all  --  *      *       0.0.0.0/0            0.0.0.0/0           CONNMARK match !0x0/0xff00 CONNMARK restore mask 0xff00

&lt;&lt;&lt;&lt; If there is no mark in the connection and the packet came in on eth3, then jump to the <span class="bold"><strong>routemark</strong></span> chain 
     This rule is generated as a result of 'track' being specified in the providers file entry for eth3 &gt;&gt;&gt;&gt;

 8804 1396K routemark  all  --  eth3   *       0.0.0.0/0            0.0.0.0/0           MARK match 0x0/0xff00

&lt;&lt;&lt;&lt; If the packet came in on eth3, jump the the <span class="bold"><strong>tcpre</strong></span> chain -- packets entering on a 'track'ed interface can have their mark set to zero there &gt;&gt;&gt;&gt;

 102K   52M tcpre      all  --  eth3   *       0.0.0.0/0            0.0.0.0/0

&lt;&lt;&lt;&lt; Otherwise, jump to the tcpre chain if there is no current provider mark -- 
     if we would have had TC_EXPERT=Yes, this jump would have been unconditional&gt;&gt;&gt;&gt;

 215K   44M tcpre      all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x0/0xff00

&lt;&lt;&lt;&lt; End of PREROUTING program &gt;&gt;&gt;&gt;

&lt;&lt;&lt;&lt; INPUT Program -- Shorewall generates the single rule here which turns off the provider mark in the packet after routing
                      The rule does that by logically ANDing the mark value with 0xff which will turn off all but the low-order 8 bits &gt;&gt;&gt;&gt;

Chain INPUT (policy ACCEPT 98238 packets, 16M bytes)
 pkts bytes target     prot opt in     out     source               destination
98234   16M MARK       all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK and 0xff

&lt;&lt;&lt;&lt; End of INPUT program &gt;&gt;&gt;&gt;

&lt;&lt;&lt;&lt; FORWARD Program -- Shorewall generates the first rule here which turns off the provider mark in the packet after routing &gt;&gt;&gt;&gt;

Chain FORWARD (policy ACCEPT 312K packets, 106M bytes)
 pkts bytes target     prot opt in     out     source               destination
 312K  106M MARK       all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK and 0xff

&lt;&lt;&lt;&lt; Jump unconditionally to the <span class="bold"><strong>tcfor</strong></span> chain &gt;&gt;&gt;&gt;

 312K  106M tcfor      all  --  *      *       0.0.0.0/0            0.0.0.0/0

&lt;&lt;&lt;&lt; End of FORWARD program &gt;&gt;&gt;&gt;

&lt;&lt;&lt;&lt; OUTPUT Program &gt;&gt;&gt;&gt;

Chain OUTPUT (policy ACCEPT 1462K packets, 396M bytes)
 pkts bytes target     prot opt in     out     source               destination

&lt;&lt;&lt;&lt; Restore the provider mark from the connection -- this rule was generated by Shorewall because of the 'track' option &gt;&gt;&gt;&gt;

 3339  615K CONNMARK   all  --  *      *       0.0.0.0/0            0.0.0.0/0           CONNMARK match !0x0/0xff00 CONNMARK restore mask 0xff00

&lt;&lt;&lt;&lt; If there is no provider mark, then jump to the <span class="bold"><strong>tcout</strong></span> chain -- 
     if we would have had TC_EXPERT=Yes, this jump would have been unconditional &gt;&gt;&gt;&gt;

92747   28M tcout      all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x0/0xff00

&lt;&lt;&lt;&lt; End of FORWARD program &gt;&gt;&gt;&gt;

&lt;&lt;&lt;&lt; POSTROUTING Program -- Unconditionally jump to the <span class="bold"><strong>tcpost</strong></span> chain &gt;&gt;&gt;&gt;

Chain POSTROUTING (policy ACCEPT 407K packets, 135M bytes)
 pkts bytes target     prot opt in     out     source               destination
 407K  135M tcpost     all  --  *      *       0.0.0.0/0            0.0.0.0/0

&lt;&lt;&lt;&lt; End of FORWARD program &gt;&gt;&gt;&gt;

Chain <span class="bold"><strong>routemark</strong></span> (1 references)
 pkts bytes target     prot opt in     out     source               destination

&lt;&lt;&lt;&lt; Set connection 'track' mark for packets coming in on eth3 &gt;&gt;&gt;&gt;

 8804 1396K MARK       all  --  eth3   *       0.0.0.0/0            0.0.0.0/0           MARK or 0x100

&lt;&lt;&lt;&lt; Save any mark added above in the connection mark &gt;&gt;&gt;&gt;

 8804 1396K CONNMARK   all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match !0x0/0xff00 CONNMARK save mask 0xff00

Chain <span class="bold"><strong>tcfor</strong></span> (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain <span class="bold"><strong>tcout</strong></span> (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain <span class="bold"><strong>tcpost</strong></span> (1 references)
 pkts bytes target     prot opt in     out     source               destination

&lt;&lt;&lt;&lt; The next two rules are the entries in the /etc/shorewall/tcrules file &gt;&gt;&gt;&gt;

65061   11M CLASSIFY   all  --  *      eth3    192.168.0.0/22       0.0.0.0/0           CLASSIFY set 1:110
 2224 2272K CLASSIFY   tcp  --  *      eth3    206.124.146.177      0.0.0.0/0           tcp spt:873 CLASSIFY set 1:130

&lt;&lt;&lt;&lt; The following rules are generated by Shorewall and classify the traffic according to the marks in /etc/shorewall/classes &gt;&gt;&gt;&gt;

    0     0 CLASSIFY   all  --  *      eth3    0.0.0.0/0            0.0.0.0/0           MARK match 0xa/0xff CLASSIFY set 1:110
    0     0 CLASSIFY   all  --  *      eth3    0.0.0.0/0            0.0.0.0/0           MARK match 0x14/0xff CLASSIFY set 1:120
    0     0 CLASSIFY   all  --  *      eth3    0.0.0.0/0            0.0.0.0/0           MARK match 0x1e/0xff CLASSIFY set 1:130

Chain <span class="bold"><strong>tcpre</strong></span> (2 references)
 pkts bytes target     prot opt in     out     source               destination
gateway:~ #</pre></div></div></body></html>