The ipfwadm, ipchains, and iptables commands differ in how accounting data is handled, so we will treat them independently.
Listing Accounting Data with ipfwadm
The most basic means of listing our accounting data with the ipfwadm command is to use it like this:
# ipfwadm -A -l
IP accounting rules
pkts bytes dir prot source destination ports
9833 2345K i/o all 172.16.3.0/24 anywhere n/a
56527 33M i/o all 172.16.4.0/24 anywhere n/a
This will tell us the number of packets sent in each direction. If we use the extended output format with the -e option (not shown here because the output is too wide for the page), we are also supplied the options and applicable interface names. Most of the fields in the output will be self-explanatory, but the following may not:
dir
The direction in which the rule applies. Expected values here are in, out, or i/o, meaning both ways.
prot
The protocols to which the rule applies.
opt
A coded form of the options we use when invoking ipfwadm.
ifname
The name of the interface to which the rule applies.
ifaddress
The address of the interface to which the rule applies.
By default, ipfwadm displays the packet and byte counts in a shortened form, rounded to the nearest thousand (K) or million (M). We can ask it to display the collected data in exact units by using the expanded option as follows:
# ipfwadm -A -l -e -x
Listing Accounting Data with ipchains
The ipchains command will not display our accounting data (packet and byte counters) unless we supply it the -v argument. The simplest means of listing our accounting data with the ipchains is to use it like this:
# ipchains -L -v
Again, just as with ipfwadm, we can display the packet and byte counters in units by using the expanded output mode. The ipchains uses the -x argument for this:
# ipchains -L -v -x
Listing Accounting Data with iptables
The iptables command behaves very similarly to the ipchains command. Again, we must use the -v when listing tour rules to see the accounting counters. To list our accounting data, we would use:
# iptables -L -v
Just as for the ipchains command, you can use the -x argument to show the output in expanded format with unit figures.
The IP accounting counters will overflow if you leave them long enough. If they overflow, you will have difficulty determining the value they actually represent. To avoid this problem, you should read the accounting data periodically, record it, and then reset the counters back to zero to begin collecting accounting information for the next accounting interval.
The ipfwadm and ipchains commands provide you with a means of doing this quite simply:
# ipfwadm -A -z
or:
# ipchains -Z
or:
# iptables -Z
You can even combine the list and zeroing actions together to ensure that no accounting data is lost in between:
# ipfwadm -A -l -z
or:
# ipchains -L -Z
or:
# iptables -L -Z -v
These commands will first list the accounting data and then immediately zero the counters and begin counting again. If you are interested in collecting and using this information regularly, you would probably want to put this command into a script that recorded the output and stored it somewhere, and execute the script periodically using the cron command.
One last command that might be useful allows you to flush all the IP accounting rules you have configured. This is most useful when you want to radically alter your ruleset without rebooting the machine.
The -f argument in combination with the ipfwadm command will flush all of the rules of the type you specify. ipchains supports the -F argument, which does the same:
# ipfwadm -A -f
or:
# ipchains -F
or:
# iptables -F
This flushes all of your configured IP accounting rules, removing them all and saving you having to remove each of them individually. Note that flushing the rules with ipchains does not cause any user-defined chains to be removed, only the rules within them.
Passive Collection of Accounting Data
One last trick you might like to consider: if your Linux machine is connected to an Ethernet, you can apply accounting rules to all of the data from the segment, not only that which it is transmitted by or destined for it. Your machine will passively listen to all of the data on the segment and count it.
You should first turn IP forwarding off on your Linux machine so that it doesn't try to route the datagrams it receives. [65] This isn't a good thing to do if your Linux machine serves as a router. If you disable IP forwarding, it will cease to route! Do this only on a machine with a single physical network interface.
In the 2.0.36 and 2.2 kernels, this is a matter of:
# echo 0 ›/proc/sys/net/ipv4/ip_forward
You should then enable promiscuous mode on your Ethernet interface using the ifconfig command. Now you can establish accounting rules that allow you to collect information about the datagrams flowing across your Ethernet without involving your Linux in the route at all.
Chapter 11. IP Masquerade and Network Address Translation
You don't have to have a good memory to remember a time when only large organizations could afford to have a number of computers networked together by a LAN. Today network technology has dropped so much in price that two things have happened. First, LANs are now commonplace, even in many household environments. Certainly many Linux users will have two or more computers connected by some Ethernet. Second, network resources, particularly IP addresses, are now a scarce resource and while they used to be free, they are now being bought and sold.
Most people with a LAN will probably also want an Internet connection that every computer on the LAN can use. The IP routing rules are quite strict in how they deal with this situation. Traditional solutions to this problem would have involved requesting an IP network address, perhaps a class C address for small sites, assigning each host on the LAN an address from this network and using a router to connect the LAN to the Internet.
In a commercialized Internet environment, this is quite an expensive proposition. First, you'd be required to pay for the network address that is assigned to you. Second, you'd probably have to pay your Internet Service Provider for the privilege of having a suitable route to your network put in place so that the rest of the Internet knows how to reach you. This might still be practical for companies, but domestic installations don't usually justify the cost.
Fortunately, Linux provides an answer to this dilemma. This answer involves a component of a group of advanced networking features called Network Address Translation (NAT). NAT describes the process of modifying the network addresses contained with datagram headers while they are in transit. This might sound odd at first, but we'll show that it is ideal for solving the problem we've just described and many have encountered. IP masquerade is the name given to one type of network address translation that allows all of the hosts on a private network to use the Internet at the price of a single IP address.
Читать дальше