From: "Mario" Date: Wed, 24 Mar 2004 17:45:01 +0000 Subject: [LARTC] IP Masquerade issues Message-Id: <20040324174714.1BF79400E@outpost.ds9a.nl> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable To: lartc@vger.kernel.org Okay here is my setup:=20 Gentoo Box running 2.6.4 w/ 4 NICs=20 1 NIC is for internal network=20 3 NICs are for external network=20 The machine has a static address assigned to the internal network nic. This nic runs dhcp and dns forwarding. The other 3 nics have external dynamic IP addresses. All will have the same gateway. There are 3 NICs because this is a very large pipe, that will only allocate a certain ammount to each client ( NIC ).=20 The way i set this up is so that internal network clients will be routed through the 3 nics with their packets sent out in a round robin sort of fashion. I found a few sites explaining a bit about how to do this, and the current setup i found that somewhat works is this:=20 A virtual device teql0 is created:=20 This virtual device teql0 is an imaginary device using QoS Trivial link equalizer (TeQL). It has a static ip address assigned to it. If you attempt to ping it from another machine, you wont necessarily be pinging the same network card every time.=20 Code: =A0tc qdisc add dev eth0 root teql0=20 =A0tc qdisc add dev eth1 root teql0=20 =A0tc qdisc add dev eth3 root teql0=20 =A0ip link set dev teql0 up=20 =A0ip addr add dev teql0 64.113.86.126 IP Masquerading is Setup=20 A file name rc.firewall.2.6 is executed at every startup. This file sets up a few routing things and the masquerading setup.=20 Code: #!/bin/sh=20 #=20 # rc.firewall-2.6=20 #FWVER=3D0.75=20 #=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Initial SIMPLE IP Masquerade test for 2.4.x= kernels=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0using IPTABLES.=A0=20 #=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Once IP Masquerading has been tested, with = this simple=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ruleset, it is highly recommended to use a = stronger=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0IPTABLES ruleset either given later in this= HOWTO or=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0from another reputable resource.=20 #=20 #=20 #=20 # Log:=20 #=A0 =A0 =A0 =A00.75 - Added more kernel modules to the comments section=20 #=A0 =A0 =A0 =A00.74 - the ruleset now uses modprobe vs. insmod=20 #=A0 =A0 =A0 =A00.73 - REJECT is not a legal policy yet; back to DROP=20 #=A0 =A0 =A0 =A00.72 - Changed the default block behavior to REJECT not DRO= P=20 #=A0 =A0 =A0 =A00.71 - Added clarification that PPPoE users need to use=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 "ppp0" instead of "eth0" for their external in= terface=20 #=A0 =A0 =A0 =A00.70 - Added commented option for IRC nat module=20 #=A0 =A0 =A0 =A0 =A0 =A0 - Added additional use of environment variables=20 #=A0 =A0 =A0 =A0 =A0 =A0 - Added additional formatting=20 #=A0 =A0 =A0 =A00.63 - Added support for the IRC IPTABLES module=20 #=A0 =A0 =A0 =A00.62 - Fixed a typo on the MASQ enable line that used eth0 = #=A0 =A0 =A0 =A0 =A0 =A0 =A0 instead of $EXTIF=20 #=A0 =A0 =A0 =A00.61 - Changed the firewall to use variables for the intern= al=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 and external interfaces.=20 #=A0 =A0 =A0 =A00.60 - 0.50 had a mistake where the ruleset had a rule to D= ROP=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 all forwarded packets but it didn't have a rul= e to ACCEPT=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 any packets to be forwarded either=20 #=A0 =A0 =A0 =A0 =A0 =A0 - Load the ip_nat_ftp and ip_conntrack_ftp modules= by default=20 #=A0 =A0 =A0 =A00.50 - Initial draft=20 #=20 echo -e "\n\nLoading simple rc.firewall version $FWVER..\n"=20 # The location of the iptables and kernel module programs=20 #=20 #=A0 =A0If your Linux distribution came with a copy of iptables,=20 #=A0 =A0most likely all the programs will be located in /sbin.=A0 If=20 #=A0 =A0you manually compiled iptables, the default location will=20 #=A0 =A0be in /usr/local/sbin=20 #=20 # ** Please use the "whereis iptables" command to figure out=20 # ** where your copy is and change the path below to reflect=20 # ** your setup=20 #=20 IPTABLES=3D/sbin/iptables=20 DEPMOD=3D/sbin/depmod=20 MODPROBE=3D/sbin/modprobe=20 #Setting the EXTERNAL and INTERNAL interfaces for the network=20 #=20 #=A0 Each IP Masquerade network needs to have at least one=20 #=A0 external and one internal network.=A0 The external network=20 #=A0 is where the natting will occur and the internal network=20 #=A0 should preferably be addressed with a RFC1918 private address=20 #=A0 scheme.=20 #=20 #=A0 For this example, "eth0" is external and "eth1" is internal"=20 #=20 #=20 #=A0 NOTE:=A0 If this doesnt EXACTLY fit your configuration, you must=20 #=A0 =A0 =A0 =A0 =A0change the EXTIF or INTIF variables above. For example:= =20 #=20 #=A0 =A0 =A0 =A0 =A0 =A0 If you are a PPPoE or analog modem user:=20 #=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0EXTIF=3D"ppp0"=20 #=20 #=20 TEQLIF=3D"teql0"=20 EXTIF=3D"eth0"=20 EXTIF2=3D"eth1"=20 EXTIF3=3D"eth3"=20 INTIF=3D"eth2"=20 echo "=A0 =A0TEQL Interface: $TEQLIF"=20 echo "=A0 =A0External Interface:=A0 $EXTIF"=20 echo "=A0 =A0External Interface:=A0 $EXTIF2"=20 echo "=A0 =A0External Interface:=A0 $EXTIF3"=20 echo "=A0 =A0Internal Interface:=A0 $INTIF"=20 #=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=20 #=3D No editing beyond this line is required for initial MASQ testing =3D=20 echo -en "=A0 =A0loading modules: "=20 # Need to verify that all modules have all required dependencies=20 #=20 echo "=A0 - Verifying that all kernel modules are ok"=20 $DEPMOD -a=20 # With the new IPTABLES code, the core MASQ functionality is now either=20 # modular or compiled into the kernel.=A0 This HOWTO shows ALL IPTABLES=20 # options as MODULES.=A0 If your kernel is compiled correctly, there is=20 # NO need to load the kernel modules manually.=A0=20 #=20 #=A0 NOTE: The following items are listed ONLY for informational reasons.=20 #=A0 =A0 =A0 =A0 There is no reason to manual load these modules unless you= r=20 #=A0 =A0 =A0 =A0 kernel is either mis-configured or you intentionally disab= led=20 #=A0 =A0 =A0 =A0 the kernel module autoloader.=20 #=20 # Upon the commands of starting up IP Masq on the server, the=20 # following kernel modules will be automatically loaded:=20 #=20 # NOTE:=A0 Only load the IP MASQ modules you need.=A0 All current IP MASQ=20 #=A0 =A0 =A0 =A0 modules are shown below but are commented out from loading= .=20 # =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=20 echo "----------------------------------------------------------------------"=20 #Load the main body of the IPTABLES module - "iptable"=20 #=A0 - Loaded automatically when the "iptables" command is invoked=20 #=20 #=A0 - Loaded manually to clean up kernel auto-loading timing issues=20 #=20 echo -en "ip_tables, "=20 $MODPROBE ip_tables=20 #Load the IPTABLES filtering module - "iptable_filter"=20 #=A0 - Loaded automatically when filter policies are activated=20 #Load the stateful connection tracking framework - "ip_conntrack"=20 #=20 # The conntrack=A0 module in itself does nothing without other specific=20 # conntrack modules being loaded afterwards such as the "ip_conntrack_ftp" = # module=20 #=20 #=A0 - This module is loaded automatically when MASQ functionality is=20 #=A0 =A0 enabled=20 #=20 #=A0 - Loaded manually to clean up kernel auto-loading timing issues=20 #=20 echo -en "ip_conntrack, "=20 $MODPROBE ip_conntrack=20 #Load the FTP tracking mechanism for full FTP tracking=20 #=20 # Enabled by default -- insert a "#" on the next line to deactivate=20 #=20 echo -en "ip_conntrack_ftp, "=20 $MODPROBE ip_conntrack_ftp=20 #Load the IRC tracking mechanism for full IRC tracking=20 #=20 # Enabled by default -- insert a "#" on the next line to deactivate=20 #=20 echo -en "ip_conntrack_irc, "=20 $MODPROBE ip_conntrack_irc=20 #Load the general IPTABLES NAT code - "iptable_nat"=20 #=A0 - Loaded automatically when MASQ functionality is turned on=20 #=20 #=A0 - Loaded manually to clean up kernel auto-loading timing issues=20 #=20 echo -en "iptable_nat, "=20 $MODPROBE iptable_nat=20 #Loads the FTP NAT functionality into the core IPTABLES code=20 # Required to support non-PASV FTP.=20 #=20 # Enabled by default -- insert a "#" on the next line to deactivate=20 #=20 echo -en "ip_nat_ftp, "=20 $MODPROBE ip_nat_ftp=20 #Loads the IRC NAT functionality into the core IPTABLES code=20 # Required to support NAT of IRC DCC requests=20 #=20 # Disabled by default -- remove the "#" on the next line to activate=20 #=20 echo -e "ip_nat_irc"=20 $MODPROBE ip_nat_irc=20 echo "----------------------------------------------------------------------"=20 # Just to be complete, here is a partial list of some of the other=A0=20 # IPTABLES kernel modules and their function.=A0 Please note that most=20 # of these modules (the ipt ones) are automatically loaded by the=20 # master kernel module for proper operation and don't need to be=20 # manually loaded.=20 # --------------------------------------------------------------------=20 #=20 #=A0 =A0 ip_nat_snmp_basic - this module allows for proper NATing of some=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 SNMP traffic=20 #=20 #=A0 =A0 iptable_mangle=A0 =A0 - this target allows for packets to be=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 manipulated for things lik= e the TCPMSS=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 option, etc.=20 #=20 # --=20 #=20 #=A0 =A0 ipt_mark=A0 =A0 =A0 =A0- this target marks a given packet for futu= re action.=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0This automatically loads the ip= t_MARK module=20 #=20 #=A0 =A0 ipt_tcpmss=A0 =A0 =A0- this target allows to manipulate the TCP MS= S=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0option for braindead remote fir= ewalls.=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0This automatically loads the ip= t_TCPMSS module=20 #=20 #=A0 =A0 ipt_limit=A0 =A0 =A0 - this target allows for packets to be limite= d to=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0to many hits per sec/min/hr=20 #=20 #=A0 =A0 ipt_multiport=A0 - this match allows for targets within a range=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0of port numbers vs. listing eac= h port individually=20 #=20 #=A0 =A0 ipt_state=A0 =A0 =A0 - this match allows to catch packets with var= ious=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0IP and TCP flags set/unset=20 #=20 #=A0 =A0 ipt_unclean=A0 =A0 - this match allows to catch packets that have = invalid=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0IP/TCP flags set=20 #=20 #=A0 =A0 iptable_filter - this module allows for packets to be DROPped,=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0REJECTed, or LOGged.=A0 This mo= dule automatically=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0loads the following modules:=20 #=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ipt_LOG - this target allows fo= r packets to be=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0logged=20 #=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ipt_REJECT - this target DROPs = the packet and returns=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 a conf= igurable ICMP packet back to the=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 sender= .=20 #=20 echo -e "=A0 =A0Done loading modules.\n"=20 #CRITICAL:=A0 Enable IP forwarding since it is disabled by default since=20 #=20 #=A0 =A0 =A0 =A0 =A0 =A0Redhat Users:=A0 you may try changing the options i= n=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 /etc/sysconfig/network= from:=20 #=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0FORWARD_IPV4=FAlse=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0to=20 #=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0FORWARD_IPV4=3Dtrue=20 #=20 echo "=A0 =A0Enabling forwarding.."=20 echo 1 > /proc/sys/net/ipv4/ip_forward=20 # Dynamic IP users:=20 #=20 #=A0 =A0If you get your IP address dynamically from SLIP, PPP, or DHCP,=20 #=A0 =A0enable this following option.=A0 This enables dynamic-address hacki= ng=20 #=A0 =A0which makes the life with Diald and similar programs much easier.=20 #=20 echo "=A0 =A0Enabling DynamicAddr.."=20 echo "1" > /proc/sys/net/ipv4/ip_dynaddr=20 # Enable simple IP forwarding and Masquerading=20 #=20 #=A0 NOTE:=A0 In IPTABLES speak, IP Masquerading is a form of SourceNAT or = SNAT. #=20 #=A0 NOTE #2:=A0 The following is an example for an internal LAN address in= the=20 #=A0 =A0 =A0 =A0 =A0 =A0 192.168.0.x network with a 255.255.255.0 or a "24"= bit subnet mask=20 #=A0 =A0 =A0 =A0 =A0 =A0 connecting to the Internet on external interface "= eth0".=A0 This=20 #=A0 =A0 =A0 =A0 =A0 =A0 example will MASQ internal traffic out to the Inte= rnet but not=20 #=A0 =A0 =A0 =A0 =A0 =A0 allow non-initiated traffic into your internal net= work.=20 #=20 #=A0 =A0 =A0 =A0 =A0 =A0=20 #=A0 =A0 =A0 =A0 =A0** Please change the above network numbers, subnet mask= , and your=20 #=A0 =A0 =A0 =A0 =A0*** Internet connection interface name to match your se= tup=20 #=A0 =A0 =A0 =A0 =A0=20 #Clearing any previous configuration=20 #=20 #=A0 Unless specified, the defaults for INPUT and OUTPUT is ACCEPT=20 #=A0 =A0 The default for FORWARD is DROP (REJECT is not a valid policy)=20 #=20 echo "=A0 =A0Clearing any existing rules and setting default policy.."=20 $IPTABLES -P INPUT ACCEPT=20 $IPTABLES -F INPUT=20 $IPTABLES -P OUTPUT ACCEPT=20 $IPTABLES -F OUTPUT=20 $IPTABLES -P FORWARD DROP=20 $IPTABLES -F FORWARD=20 $IPTABLES -t nat -F=20 echo "=A0 =A0FWD: Allow all connections OUT and only existing and related o= nes IN"=20 $IPTABLES -A FORWARD -i $TEQLIF -o $INTIF -m state --state ESTABLISHED,RELATED -j ACCEPT=20 $IPTABLES -A FORWARD -i $EXTIF -o $INTIF -m state --state ESTABLISHED,RELATED -j ACCEPT=20 $IPTABLES -A FORWARD -i $EXTIF2 -o $INTIF -m state --state ESTABLISHED,RELATED -j ACCEPT=20 $IPTABLES -A FORWARD -i $EXTIF3 -o $INTIF -m state --state ESTABLISHED,RELATED -j ACCEPT=20 $IPTABLES -A FORWARD -i $INTIF -o $TEQLIF -j ACCEPT=20 $IPTABLES -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT=20 $IPTABLES -A FORWARD -i $INTIF -o $EXTIF3 -j ACCEPT=20 $IPTABLES -A FORWARD -i $INTIF -o $EXTIF2 -j ACCEPT=20 $IPTABLES -A FORWARD -j LOG=20 echo "=A0 =A0Enabling SNAT (MASQUERADE) functionality on $TEQLIF"=20 $IPTABLES -t nat -A POSTROUTING -o $TEQLIF -j MASQUERADE=20 $IPTABLES -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE=20 $IPTABLES -t nat -A POSTROUTING -o $EXTIF2 -j MASQUERADE=20 $IPTABLES -t nat -A POSTROUTING -o $EXTIF3 -j MASQUERADE=20 iptables -I FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu=20 echo -e "\nrc.firewall-2.6 v$FWVER done.\n"=20 Now this works well in most cases, but for some reasons when a computer on the internal lan wants to visit a site, it may take about 20-30 seconds longer than it should for the site to come up. If you go back to this routing machine, and pull up a dmesg, a whole slew of this comes up:=20 Quote: MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 printk: 1 messages suppressed.=20 MASQUERADE: Route sent us somewhere else.=20 printk: 2 messages suppressed.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 MASQUERADE: Route sent us somewhere else.=20 Now adding the extra lines for the interfaces other than TEQL0 (ex ETH0,ETH1, ETH3) seemed to lessen how many of these messages were actually shown and how badly the performance originally was. I can't seem to figure out why it wants to route through any other device than TEQL0 however, with those extra lines for the physcial interfaces or without.=20 $IPTABLES -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE=20 $IPTABLES -t nat -A POSTROUTING -o $EXTIF2 -j MASQUERADE=20 $IPTABLES -t nat -A POSTROUTING -o $EXTIF3 -j MASQUERADE=A0=20 Anyone have any clue what to do? _______________________________________________ LARTC mailing list / LARTC@mailman.ds9a.nl http://mailman.ds9a.nl/mailman/listinfo/lartc HOWTO: http://lartc.org/