home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / doc / HOWTO / mini / Bridge+Firewall < prev    next >
Text File  |  1997-10-13  |  20KB  |  511 lines

  1.   Linux Bridge+Firewall Mini-HOWTO version 1.1.1
  2.   Peter Breuer (ptb@it.uc3m.es)
  3.   Dec. 10 1996, revised Jul. 10 1997
  4.  
  5.   1.  Introduction
  6.  
  7.   You should look at the original Bridging mini-HOWTO
  8.   <ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/mini/Bridge> by Chris Cole
  9.   for a different perspective on this.  He is chris@polymer.uakron.edu.
  10.   The version of his HOWTO that I have based this document on
  11.   (alternatively, ripped off) is 1.03 dated Aug 23 1996.
  12.  
  13.   2.  What and Why (and How?)
  14.  
  15.   2.1.  What
  16.  
  17.   A bridge is an intelligent connecting wire betwen two network cards.
  18.   A firewall is an intelligent insulator.
  19.  
  20.   2.2.  Why
  21.  
  22.   You might want a bridge if you have several computers:
  23.  
  24.   1. to save the price of a new hub when you just happen to have an
  25.      extra ethernet card available.
  26.  
  27.   2. to save the bother of learning how to do IP-forwarding and other
  28.      tricks when you _have_ two cards in your computer.
  29.  
  30.   3. to avoid maintenance work in the future when things change around!
  31.  
  32.   ``Several computers'' might be as few as three if those are routing or
  33.   bridging or just moving around the room from time to time! You also
  34.   might want a bridge just for the fun of finding out what it does.
  35.   ``2'' was what I wanted a bridge for.
  36.  
  37.   If you are really interested in ``1'', you have to be one of the very
  38.   few.  Check the NET-2-HOWTO
  39.   <ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/NET-2-HOWTO> and the
  40.   Serial-HOWTO <ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/Serial-HOWTO>
  41.   for better tricks.
  42.  
  43.   You want a firewall if
  44.  
  45.   1. you are trying to protect your network from external accesses, or
  46.  
  47.   2. you are trying to deny access to the world outside from your
  48.      network.
  49.  
  50.   Curiously, I needed ``2'' here too.  Policy at my university presently
  51.   is that we should not act as internet service providers to
  52.   undergraduates.
  53.  
  54.   2.3.  How?
  55.  
  56.   I started out bridging the two network cards in a firewalling machine
  57.   and ended up firewalling without having cut the bridge. It seems to
  58.   work and is more flexible than either configuration alone. I can take
  59.   down the firewall and keep bridging or take down the bridge when I
  60.   want to be more circumspect.
  61.  
  62.   I would guess that the bridge code lives just above the physical
  63.   device layer and the firewalling code lives one layer higher up, so
  64.   that the bridging and firewalling configurations effectively act as
  65.   though they are running connected together ``in sequence'' and not
  66.   ``in parallel'' (ouch!). Diagram:
  67.  
  68.        -> Bridge-in -> Firewall-in -> Kernel -> Firewall-out -> Bridge-out ->
  69.  
  70.   There is no other way to explain how one machine can be a
  71.   ``conductor'' and an ``insulator'' at the same time. Anyway, it all
  72.   seems to work together nicely. Here is what you do ...
  73.  
  74.   3.  BRIDGING
  75.  
  76.   3.1.  Software
  77.  
  78.   Get the bridge configuration utility
  79.   <ftp://shadow.cabi.net/pub/Linux/BRCFG.tgz> from Alan Cox's home
  80.   pages. This is the same reference as in Chris' document. I just didn't
  81.   realize that it was an ftp and not an http URL ...
  82.  
  83.   3.2.  Prior Reading.
  84.  
  85.   Read the Multiple Ethernet HOWTO
  86.   <ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/mini/Multiple-Ethernet>
  87.   for some advice on getting more than one network card recognized and
  88.   configured.
  89.  
  90.   Yet more details of the kind of boot magic that you may need are in
  91.   the Boot Prompt HOWTO
  92.   <ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/BootPrompt-HOWTO>.
  93.  
  94.   You may be able to get away without the NET-2 HOWTO
  95.   <ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/NET-2-HOWTO>. It is a good
  96.   long read and you will have to pick from it the details you need.
  97.  
  98.   3.3.  Boot configuration
  99.  
  100.   The reading material above will tell you that you need to prepare the
  101.   kernel to recognize a second ethernet device at boot up by adding this
  102.   to your /etc/lilo.conf, and then re-run lilo:
  103.  
  104.   append = 'ether=0,0,eth1'
  105.  
  106.   Note the 'eth1'. 'eth0' is the first card. 'eth1' is the second card.
  107.   You can always add the boot parameters in your response to the line
  108.   that lilo offers you. This is for three cards:
  109.  
  110.        linux ether=0,0,eth1 ether=0,0,eth2
  111.  
  112.   I use loadlin to boot my kernel from DOS:
  113.  
  114.        loadlin.exe c:\vmlinuz root=/dev/hda3 ro ether=0,0,eth1 ether=0,0,eth2
  115.  
  116.   Note that this trick makes the kernel probe at bootup. That will not
  117.   happen if you load the ethernet drivers as modules (for safety since
  118.   the probe order can't be determined) so if you use modules you will
  119.   have to add the appropriate IRQ and port parameters for the driver in
  120.   your /etc/conf.modules.  I have at least
  121.  
  122.        alias eth0 3c509
  123.        alias eth1 de620
  124.        options 3c509 irq=5 io=0x210
  125.        options de620 irq=7 bnc=1
  126.  
  127.   You can tell if you use modules by using ``ps -aux'' to see if kerneld
  128.   is running and checking that there are .o files in a subdirectory of
  129.   your /lib/modules directory. You want the directory named with what
  130.   uname -r tells you. If you have kerneld and/or you have a foo.o then
  131.   edit /etc/conf.modules and read the man page for depmod carefully.
  132.  
  133.   Note also that until recently (kernel 2.0.25) the 3c509 driver could
  134.   not be used for more than one card if used as a module. I have seen a
  135.   patch floating around that fixes the oversight. It may be in the
  136.   kernel when you read this.
  137.  
  138.   3.4.  Kernel configuration
  139.  
  140.   Recompile the kernel with bridging enabled.
  141.  
  142.   CONFIG_BRIDGE=y
  143.  
  144.   I also compiled with firewalling and IP-forwarding and -masquerading
  145.   and the rest enabled. Only if you want firewalling too ...
  146.  
  147.        CONFIG_FIREWALL=y
  148.        CONFIG_NET_ALIAS=y
  149.        CONFIG_INET=y
  150.        CONFIG_IP_FORWARD=y
  151.        CONFIG_IP_MULTICAST=y
  152.        CONFIG_IP_FIREWALL=y
  153.        CONFIG_IP_FIREWALL_VERBOSE=y
  154.        CONFIG_IP_MASQUERADE=y
  155.  
  156.   You don't need all of this. What you do need apart from this is the
  157.   standard net configuration:
  158.  
  159.        CONFIG_NET=y
  160.  
  161.   and I do not think you need worry about any of the other networking
  162.   options. I have any options that I did not actually compile into the
  163.   kernel available through kernel modules that I can add in later.
  164.  
  165.   Install the new kernel in place, rerun lilo and reboot with the new
  166.   kernel. Nothing should have changed at this point!
  167.  
  168.   Note added in formatting version 1.1.1 of this HOWTO: I have had
  169.   several messages from people with one (I don't know which) of the
  170.   2.0.30 kernel versions complaining that they can't stop bridging when
  171.   firewalling. I believe them, but some things are definitely wrong in
  172.   2.0.30 that may account for this. As far as I can gather, switching
  173.   the router to a different card and placing nets on each card sometimes
  174.   does the trick even on 2.0.30. I am still at 2.0.25 and staying there!
  175.   (and it works for me :-). PTB
  176.  
  177.   3.5.  Network addresses
  178.  
  179.   Chris says that a bridge should not have an IP address but that is not
  180.   the setup to be described here.
  181.  
  182.   You are going to want to use the machine for connecting to the net so
  183.   you need an address and you need to make sure that you have the
  184.   loopback device configured in the normal way so that your software can
  185.   talk to the places they expect to be able to talk to. If loopback is
  186.   down the name resolver or other net sevices might fail. See the
  187.   NET-2-HOWTO, but your standard configuration should already have done
  188.   this bit:
  189.  
  190.        ifconfig lo 127.0.0.1 route add -net 127.0.0.0
  191.  
  192.   You will have to give addresses to both your network cards. I altered
  193.   the /etc/rc.d/rc.inet1 file in my slackware (3.x) to setup my two
  194.   cards and you should also essentially just look for your net
  195.   configuration file and double the number of instructions in it.
  196.   Suppose that you already have an address at
  197.  
  198.        192.168.2.100
  199.  
  200.   (that is the private net reserved address space, but never mind - it
  201.   won't hurt anybody if you use this address by mistake) then you
  202.   probably already have a line like
  203.  
  204.        ifconfig eth0 192.168.2.100 netmask 255.255.255.0 metric 1
  205.  
  206.   in your configuration. The first thing you are going to probably want
  207.   to do is cut the address space reached by this card in half so that
  208.   you can eventually bridge the two halves. So add a line which reduces
  209.   the mask to address a smaller number of machines:
  210.  
  211.        ifconfig eth0 netmask 255.255.255.128
  212.  
  213.   Try it too. That restricts the card to at most the address space
  214.   between .0 and .127.
  215.  
  216.   Now you can set your second card up in the other half of the local
  217.   address space. Make sure that nobody already has the address. For
  218.   symmetry I set it at 228=128+100 here. Any address will do so long as
  219.   it is not in the other card's mask, and even then, well, maybe. Avoid
  220.   special addresses like .0, .1, .128 etc. unless you really know what
  221.   you are doing.
  222.  
  223.        ifconfig eth1 192.168.2.228 netmask 255.255.255.128 metric 1
  224.  
  225.   That restricts the second card to addresses between .128 and .255.
  226.  
  227.   3.6.  Network routing
  228.  
  229.   The above might be enough address configuration for a working bridge,
  230.   but I will be doing firewalling too and I want to control the physical
  231.   destination of some packets. Even then one might have to take
  232.   precautions against address spoofing.
  233.  
  234.   I have the small net of machines attached to a hub hanging off eth0,
  235.   so I configure a net there:
  236.  
  237.        route add -net 192.168.2.128 netmask 255.255.255.128 dev eth0
  238.  
  239.   The 128 would be 0 if I had a full class C network there. The 'dev
  240.   eth0' is not necessary here because the cards address falls within
  241.   this net, but it may be necessary for you.
  242.  
  243.   On the other card I have a line going straight through to a big router
  244.   that I trust.
  245.  
  246.                                                client 129
  247.            __                                        |    __
  248.   client 1   \    .0                    .128         |   /   net 1
  249.   client 2 --- Hub - eth0 - Kernel - eth1 - Hub - Router --- net 2
  250.   client 3 __/       .100            .228         .2 |   \__ net 3
  251.                                                      |
  252.                                                client 254
  253.  
  254.   I attach the address of the router to that card as a fixed ('static')
  255.   route because it would otherwise fall within the first cards netmask
  256.   and the kernel would be thinking wrongly about how to send packets to
  257.   the big router.
  258.  
  259.        route add 192.168.2.2 dev eth1
  260.  
  261.   I don't need it, since I don't have any more machines in that half of
  262.   the address space, but I declare a net also on the second card
  263.  
  264.        route add -net 192.168.2.128 netmask 255.255.255.128 dev eth1
  265.  
  266.   I also need to send all non-local packets out to the world so I tell
  267.   the kernel to send them to the big router
  268.  
  269.        route add default gw 192.168.2.2
  270.  
  271.   3.7.  Card configuration
  272.  
  273.   So much was standard networking setup, but we are bridging so we also
  274.   have to listen on both (?) cards for packets that are not aimed at us.
  275.   The following should go into the network configuration file.
  276.  
  277.        ifconfig promisc eth0 ifconfig promisc eth1
  278.  
  279.   The man page says allmulti=promisc, but it didn't work for me.
  280.  
  281.   3.8.  Additional routing
  282.  
  283.   One thing that I noticed was that I had to put at least the second
  284.   card into a mode where it would respond to the big router's questions
  285.   about which machines I was hiding in my local net.
  286.  
  287.        ifconfig arp eth1
  288.  
  289.   For good measure I did this to the other card too.
  290.  
  291.        ifconfig arp eth0.
  292.  
  293.   3.9.  Bridge Configuration
  294.  
  295.   Put bridging enabling on and into your configuration file:
  296.  
  297.        brcfg -enable
  298.  
  299.   You should have been trying this out in real time all along, of
  300.   course!  The bridge configure will bring up some numbers. You can
  301.   experiment with turning on and off the ports one at a time
  302.  
  303.        brcfg -port 0 -disable/-enable
  304.         brcfg -port 1 -disable/-enable
  305.  
  306.   You get status reports anytime by just running
  307.  
  308.        brcfg
  309.  
  310.   without any parameters. You will see that the bridge listens,learns,
  311.   and then does forwarding. (I don't understand why the code repeats the
  312.   same hardware addresses for both my cards, but never mind .. Chris'
  313.   howto say that is OK)
  314.  
  315.   3.10.  Try it out
  316.  
  317.   If you are still up and running as things are, try out your
  318.   configuration script for real by taking down both cards and then
  319.   executing it:
  320.  
  321.        ifconfig eth0 down ifconfig eth1 down /etc/rc.d/rc.inet1
  322.  
  323.   With any luck the various subsystems (nfs, ypbind, etc.)  won't
  324.   notice. Do not try this unless you are sitting at the keyboard!
  325.  
  326.   If you want to be more careful than this, you should take down as many
  327.   daemons as possible beforehand, and unmount nfs directories. The worst
  328.   that can happen is that you have to reboot in single-user mode (the
  329.   'single' parameter to lilo or loadlin), and take out your changes
  330.   before rebooting with things the way they were before you started.
  331.  
  332.   3.11.  Checks
  333.  
  334.   Verify that there is different traffic on each interface:
  335.  
  336.        tcpdump -i eth0
  337.  
  338.        tcpdump -i eth1
  339.  
  340.   You should get used to using tcpdump to look for things that should
  341.   not be happening or that are happening and should not.
  342.  
  343.   For instance look for packets that have gone through the bridge to the
  344.   second card from the internal net. Here I am looking for packets from
  345.   the machine with address .22:
  346.  
  347.        tcpdump -i eth1 -e host 192.168.2.22
  348.  
  349.   Then send a ping from the .22 host to the router. You should see the
  350.   packet reported by tcpdump.
  351.  
  352.   At this stage you should have a fully working bridge that also has two
  353.   network addreses. Test that you can ping them from outside and inside
  354.   your local net, and that you can telnet and ftp around between inside
  355.   and outside too.
  356.  
  357.   4.  FIREWALLING
  358.  
  359.   4.1.  Software and reading
  360.  
  361.   You should read the Firewall-HOWTO
  362.   <ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/Firewall-HOWTO>.
  363.  
  364.   That will tell you where to get ipfwadm if you don't already have it.
  365.   There are other tools you can get but I made no progress until I tried
  366.   ipfwadm. It is nice and low level! You can see exactly what it is
  367.   doing.
  368.  
  369.   4.2.  Preliminary checks
  370.  
  371.   You have compiled IP-forwarding and masquerading into the kernel so
  372.   you will want to check that the firewall is in its default (accepting)
  373.   state with
  374.  
  375.        ipfwadm -I -l ipfwadm -O -l ipfwadm -F -l
  376.  
  377.   That is respectively, 'display the rules affecting the ..'  incoming
  378.   or outgoing or forwarding (masquerading) '.. sides of the firewall'.
  379.   The '-l' means 'list'.
  380.  
  381.   You might have compiled in accounting too:
  382.  
  383.        ipfwadm -A -l
  384.  
  385.   You should see that there are no rules defined and that the default is
  386.   to accept every packet You can get back to this working state anytime
  387.   with
  388.  
  389.        ipfwadm -I -f
  390.        ipfwadm -O -f
  391.        ipfwadm -F -f
  392.  
  393.   The '-f' means 'flush'. You may need to use that.
  394.  
  395.   4.3.  Default rule
  396.  
  397.   I want to cut the world off from my internal net and do nothing else,
  398.   so I will want to give as a last (default) rule that the firewall
  399.   should ignore any packets coming in from the internal net and directed
  400.   to outside.  I put all the rules (in this order) into
  401.   /etc/rc.d/rc.firewall and execute it from /etc/rc.d/rc.local at
  402.   bootup.
  403.  
  404.        ipfwadm -I -a reject -S 192.168.2.0/255.255.255.128 -D 0.0.0.0/0.0.0.0
  405.  
  406.   The '-S' is the source address/mask. The '-D' is the destination
  407.   address/mask.
  408.  
  409.   This format to is rather long-winded. Ipfwadm is intelligent about
  410.   network names and some common abbreviations. Check the man pages.
  411.  
  412.   It is possibly more convenient to put some or all of these rules on
  413.   the outgoing half of the firewall by using '-O' instead of '-I', but
  414.   I'll state the rules here all formulated for the incoming half.
  415.  
  416.   4.4.  Holes per address
  417.  
  418.   Before that default rule, I have to place some rules that serve as
  419.   exceptions to this general denial of external services to internal
  420.   clients.
  421.  
  422.   I want to treat the firewall machines address on the internal net
  423.   specially.  I will stop people logging in to the firewall machine
  424.   unless they have special permission, but once they are there they
  425.   should be allowed to talk to the world.
  426.  
  427.        ipfwadm -I -i accept -S 192.168.2.100/255.255.255.255 \
  428.         -D 0.0.0.0/0.0.0.0
  429.  
  430.   I also want the internal clients to be able to talk to the firewalling
  431.   machine. Maybe they can persuade it to let them get out!
  432.  
  433.        ipfwadm -I -i accept -S 192.168.2.0/255.255.255.128 \
  434.         -D 192.168.2.100/255.255.255.255
  435.  
  436.   Check at this point that you can get in to the clients from outside
  437.   the firewall via telnet, but that you cannot get out. That should mean
  438.   that you can just about make first contact, but the clients cannot
  439.   send you any prompts. You should be able to get all the way in if you
  440.   use the firewall machine as a staging post. Try rlogin and ping too,
  441.   with tcpdump running on one card or the other. You should be able to
  442.   make sense of what you see.
  443.  
  444.   4.5.  Holes per protocol
  445.  
  446.   I went on to relax the rules protocol by protocol. I want to allow
  447.   pings from the outside to the inside to get an echo back, for
  448.   instance, so I inserted the rule:
  449.  
  450.        ipfwadm -I -i accept -P icmp -S 192.168.2.0/255.255.255.128 \
  451.         -D 0.0.0.0/0.0.0.0
  452.  
  453.   The '-P icmp' works the protocol-specific magic.
  454.  
  455.   Until I get hold of an ftp proxy I am also allowing ftp calls out with
  456.   port-specific relaxations. This targets ports 20 21 and 115 on outside
  457.   machines.
  458.  
  459.        ipfwadm -I -i accept -P tcp -S 192.168.2.0/255.255.255.128 \
  460.         -D 0.0.0.0/0.0.0.0 20 21 115
  461.  
  462.   I could not make sendmail between the local clients work without a
  463.   nameserver. Rather than set up a nameserver right then on the
  464.   firewall, I just lifted the firewall for tcp domain service queries
  465.   precisely aimed at the nearest existing nameserver and put its address
  466.   in the clients /etc/resolv.conf ('nameserver 123.456.789.31' on a
  467.   separate line).
  468.  
  469.        ipfwadm -I -i accept -P tcp -S 192.168.2.0/255.255.255.128 \
  470.         -D 123.456.789.31/255.255.255.255 54
  471.  
  472.   You can find which port number and protocol a service requires with
  473.   tcpdump. Trigger the service with a an ftp or a telnet or whatever to
  474.   or from the internal machine and then watch for it on the input and
  475.   output ports of the firewall with tcpdump:
  476.  
  477.        tcpdump -i eth1 -e host client04
  478.  
  479.   for example. The /etc/services file is another important source of
  480.   clues. To let telnet and ftp IN to the firewall from outside, you have
  481.   to allow the local clients to call OUT on a specific port. I
  482.   understand why this is necessary for ftp - it's the server that
  483.   establishes the data stream in the end - but I am not sure why telnet
  484.   also needs this.
  485.  
  486.        ipfwadm -I -i accept -P tcp -S 192.168.2.0/255.255.255.128 ftp telnet \
  487.         -D 0.0.0.0/0.0.0.0
  488.  
  489.   There is a particular problem with some daemons that look up the
  490.   hostname of the firewalling machine in order to decide what is their
  491.   networking address. Rpc.yppasswdd is the one I had trouble with. It
  492.   insists on broadcasting information that says it is outside the
  493.   firewall (on the second card). That means the clients inside can't
  494.   contact it.
  495.  
  496.   Rather than start IP aliasing or change the daemon code, I mapped the
  497.   name to the inside card address on the clients in their /etc/hosts.
  498.  
  499.   4.6.  Checks
  500.  
  501.   You want to test that you can still telnet, rlogin and ping from the
  502.   outside. From the inside you should be able to ping out. You should
  503.   also be able to telnet to the firewall machine from the inside and the
  504.   latter should be able to do anything.
  505.  
  506.   That is it. At this point you probably want to learn about rpc/Yellow
  507.   Pages and the interaction with the password file. The firewalled
  508.   network wants to run without its unprivileged users being able to log
  509.   on to the firewall - and thus get out. Some other HOWTO!
  510.  
  511.