Книга: Linux Network Administrator Guide, Second Edition
A na?ve example
A na?ve example
Let's suppose that we have a network in our organization and that we are using a Linux-based firewall machine to connect our network to the Internet. Additionally, let's suppose that we wish the users of that network to be able to access web servers on the Internet, but to allow no other traffic to be passed.
We will put in place a forwarding rule to allow datagrams with a source address on our network and a destination socket of port 80 to be forwarded out, and for the corresponding reply datagrams to be forwarded back via the firewall.
Assume our network has a 24-bit network mask (Class C) and an address of 172.16.1.0. The rules we might use are: The second rule sets our default forwarding policy. We tell the kernel to deny or disallow forwarding of IP datagrams. It is very important to set the default policy, because this describes what will happen to any datagrams that are not specifically handled by any other rule. In most firewall configurations, you will want to set your default policy to "deny," as shown, to be sure that only the traffic you specifically allow past your firewall is forwarded.
# ipfwadm -F -f
# ipfwadm -F -p deny
# ipfwadm -F -a accept -P tcp -S 172.16.1.0/24 -D 0/0 80
# ipfwadm -F -a accept -P tcp -S 0/0 80 -D 172.16.1.0/24
The -F command-line argument tells ipfwadm that this is a forwarding rule. The first command instructs ipfwadm to "flush" all of the forwarding rules. This ensures we are working from a known state before we begin adding specific rules.
The second rule sets our default forwarding policy. We tell the kernel to deny or disallow forwarding of IP datagrams. It is very important to set the default policy, because this describes what will happen to any datagrams that are not specifically handled by any other rule. In most firewall configurations, you will want to set your default policy to "deny," as shown, to be sure that only the traffic you specifically allow past your firewall is forwarded.
The third and fourth rules are the ones that implement our requirement. The third command allows our datagrams out, and the fourth rule allows the responses back.
Let's review each of the arguments:
- F
This is a Forwarding rule.
- a accept
Append this rule with the policy set to "accept," meaning we will forward any datagrams that match this rule.
- P tcp
This rule applies to tcp datagrams (as opposed to UDP or ICMP).
- S 172.16.1.0/24
The Source address must have the first 24 bits matching those of the network address 172.16.1.0.
- D 0/0 80
The destination address must have zero bits matching the address 0.0.0.0. This is really a shorthand notation for "anything." The 80 is the destination port, in this case WWW. You may also use any entry that appears in the /etc/services file to describe the port, so -D 0/0 www would have worked just as well.
ipfwadm accepts network masks in a form with which you may not be familiar. The /nn notation is a means of describing how many bits of the supplied address are significant, or the size of the mask. The bits are always counted from left to right; some common examples are listed in Table 9.1.
Table 9.1: Common Netmask Bit Values
Netmask | Bits |
---|---|
255.0.0.0 | 8 |
255.255.0.0 | 16 |
255.255.255.0 | 24 |
255.255.255.128 | 25 |
255.255.255.192 | 26 |
255.255.255.224 | 27 |
255.255.255.240 | 28 |
255.255.255.248 | 29 |
255.255.255.252 | 30 |
We mentioned earlier that ipfwadm implements a small trick that makes adding these sorts of rules easier. This trick is an option called -b, which makes the command a bidirectional rule.
The bidirectional flag allows us to collapse our two rules into one as follows:
#
ipfwadm -F -a accept -P tcp -S 172.16.1.0/24 -D 0/0 80 -b
- Dedications
- About the author
- How to read
- Conventions used in this document
- Chapter 5. Preparations
- Chapter 10. Iptables matches
- Chapter 12. Debugging your scripts
- Chapter 13. rc.firewall file
- Chapter 14. Example scripts
- Appendix A. Detailed explanations of special commands
- Appendix D. TCP options
- Appendix E. Other resources and links