|
ebtables - sourceforge page - downloads - browse cvs - bridge - netfilter Free firewall software distributed under GNU General Public License |
||||||||||||
|
For more information about the various commands and options used in these examples, we refer to the manual page. Feel free to add examples and applications of ebtables into network situations. Your contributions are appreciated. You can mail me directly or send a message to the mailing list. Last updated: Wednesday, 05-Apr-2006 09:35:24 PDT. When needing a network protocol for an example, we usually use the IP protocol, as it is most common and is what I am familiar with. When appropriate, these examples can be reformulated using a different network protocol (like Appletalk). Unless otherwise stated, we assume a bridge setup consisting of one bridge device br0 with two bridge ports eth0 and eth1.
ebtables -P FORWARD DROP ebtables -A FORWARD -p IPv4 -j ACCEPT ebtables -A FORWARD -p ARP -j ACCEPT ebtables -A FORWARD -p LENGTH -j ACCEPT ebtables -A FORWARD --log-level info --log-ip --log-prefix EBFW ebtables -P INPUT DROP ebtables -A INPUT -p IPv4 -j ACCEPT ebtables -A INPUT -p ARP -j ACCEPT ebtables -A INPUT -p LENGTH -j ACCEPT ebtables -A INPUT --log-level info --log-ip --log-prefix EBFW ebtables -P OUTPUT DROP ebtables -A OUTPUT -p IPv4 -j ACCEPT ebtables -A OUTPUT -p ARP -j ACCEPT ebtables -A OUTPUT -p LENGTH -j ACCEPT ebtables -A OUTPUT --log-level info --log-ip --log-arp --log-prefix EBFW -j DROP This is a basic filter configuration which will only let frames made by the protocols IP version 4 and ARP through. Also, the network has some old machines that use the protocol field of the Ethernet frame as a length field (they use the Ethernet 802.2 or 802.3 protocol). There was no reason not to let those machines through, more precisely: there was a reason to let them through ;-). So, those frames, with protocol LENGTH denoting that it's really a length field, are accepted. Of course one could filter on the MAC addresses of those old machines so no other machine can use the old Ethernet 802.2 or 802.3 protocol. All other frames get logged and dropped. This logging consists of the protocol number, the MAC addresses, the ip/arp info (if it's an IP/ARP packet of course) and the in and out interfaces.
Important note: ebtables -A FORWARD -p IPv4 --ip-src 172.16.1.4 -s ! 00:11:22:33:44:55 -j DROP
This is an anti-spoofing filter rule. It says that the computer using IP
address 172.16.1.4 has to be the one that uses ethernet card
00:11:22:33:44:55 to send this traffic. iptables -A FORWARD -s 172.16.1.4 -m mac --mac-source ! 00:11:22:33:44:55 -j DROP The difference is that the frame will be dropped earlier if the ebtables rule is used, because ebtables inspects the frame before iptables does. Also note the subtle difference in what is considered the default type for a source address: an IP address in iptables, a MAC address in ebtables.
If you have many such rules, you can also use the ebtables -N MATCHING-MAC-IP-PAIR ebtables -A FORWARD -p IPv4 --among-dst 00:11:22:33:44:55=172.16.1.4,00:11:33:44:22:55=172.16.1.5 -j MATCHING-MAC-IP-PAIR
We first make a new user-defined chain See the real-life examples section for a usage. ebtables -t nat -A PREROUTING -d 00:11:22:33:44:55 -i eth0 -j dnat --to-destination 54:44:33:22:11:00
This will make all frames destined to 00:11:22:33:44:55 that arrived on
interface eth0 be transferred to 54:44:33:22:11:00 instead. As this change of
destination MAC address is done in the
This situation was described by someone: ebtables -A FORWARD -s 00:11:22:33:44:55 -p IPV4 -j ACCEPT ebtables -A FORWARD -s 00:11:22:33:44:55 -j DROP Here is an example setup for a brouter with the following situation: br0 with ports eth0 and eth1. ifconfig br0 0.0.0.0 ifconfig eth0 172.16.1.1 netmask 255.255.255.0 ifconfig eth1 172.16.2.1 netmask 255.255.255.0 ebtables -t broute -A BROUTING -p ipv4 -i eth0 --ip-dst 172.16.1.1 -j DROP ebtables -t broute -A BROUTING -p ipv4 -i eth1 --ip-dst 172.16.2.1 -j DROP ebtables -t broute -A BROUTING -p arp -i eth0 -d $MAC_OF_ETH0 -j DROP ebtables -t broute -A BROUTING -p arp -i eth1 -d $MAC_OF_ETH1 -j DROP
As mentioned in the man pages, the Here is a simple example that will make all IP traffic entering a (forwarding) bridge port be routed instead of bridged (suppose eth0 is a port of the bridge br0): ebtables -t broute -A BROUTING -i eth0 -p ipv4 -j redirect --redirect-target DROP
As mentioned in the man pages, the Using the following rule has a similar effect: ebtables -t nat -A PREROUTING --logical-in br0 -p ipv4 -j redirect --redirect-target ACCEPT The difference is that in the second case the IP code and routing code will think the IP packet entered through the br0 device. In the first case the IP code and routing code will think the IP packet entered through the eth0 device. It depends on the situation in which chain to use the redirect target. F.e., if your routing table only uses br0, then the redirect belongs in the PREROUTING chain.
Why do we want to be able to atomically load a complete table with any
predefined contents into the kernel? Because then the data is given
to the kernel in one step, saving a lot of context switching and kernel
time. The most obvious time to do this is when the box is booted. Here
is a brief description how to do this. The examples will use the ebtables --atomic nat_table -t nat --atomic-save Then we (optionally) zero the counters of the rules in the file: ebtables -t nat --atomic-file nat_table -Z At bootup we use the following command to get everything into the kernel table at once: ebtables --atomic-file nat_table -t nat --atomic-commit
We can also build up the complete table in the file. We can use
the environment variable export EBTABLES_ATOMIC_FILE=nat_table
Then we initialize the file with the default table, which has
empty chains and policy ebtables -t nat --atomic-init We then add our rules, user defined chains, change policies: ebtables -t nat -A PREROUTING -j DROP We can check the contents of our table with: ebtables -t nat -L --Lc --Ln We then use the following command to get everything into the kernel table at once: ebtables -t nat --atomic-commit Don't forget to unset the environment variable: unset EBTABLES_ATOMIC_FILE
Now all ebtables commands will execute onto the actual kernel
table again, instead of on the file
If you really need filtering on an interface and can't
use a standard way of doing it (i.e. there is no standard
filtering tool for the protocol), there is a solution if
you only need basic filtering. brctl addbr br0 Then (perhaps) disable the spanning tree protocol on that bridge: brctl stp br0 off Then add the physical device eth0 to the logical bridge device: brctl addif br0 eth0 give the IP address of eth0 to the bridge device and remove it from eth0: ifconfig br0 172.16.1.10 netmask 255.255.255.0 ifconfig eth0 0.0.0.0 The routing table must be corrected too, f.e.: route del -net 172.16.1.0 netmask 255.255.255.0 dev eth0 route add -net 172.16.1.0 netmask 255.255.255.0 dev br0 route del default gateway $DEFAULT_GW dev eth0 route add default gateway $DEFAULT_GW dev br0
So, now all IP traffic that originally went through eth0 will go
through br0. Note that this is kind of a hack: using a bridge
with only one enslaved device. However, now ebtables will see all
the traffic that passes eth0, because eth0 is now a port of the
bridge device br0.
In some situations the bridge not only serves as a bridge box, but
also talks to other hosts. Packets that arrive on a bridge port
and that are destinated to the bridge box itself will by default
enter the iptables
The way to let locally destinated packets be queued only once is
by brouting them in the ebtables -t broute -A BROUTING -d $MAC_OF_BR0 -p ipv4 -j redirect --redirect-target DROP The replies from the bridge will be sent out through the br0 device (assuming your routing table is correct and sends all traffic through br0), so everything keeps working neatly, without the performance loss caused by the packet being queued twice. The redirect target is needed because the MAC address of the bridge port is not necessarily equal to the MAC address of the bridge device. The packets destinated to the bridge box will have a destination MAC address equal to that of the bridge br0, so that destination address must be changed to that of the bridge port.
Say there are 3 types of traffic you want to mark. The best mark
values are powers of 2, because these translate to setting one bit
in the ebtables -A FORWARD -p ipv4 -i eth0 -j mark --set-mark 2 --mark-target CONTINUE Suppose we want to do something to all frames that are marked with the first mark value: ebtables -A FORWARD --mark 1/1 Suppose we want to do something to all frames that are marked with either the first, either the third mark value: ebtables -A FORWARD --mark /5
1 + 4 = 5. We only specified the
Note that iptables uses the same
The ebtables -t nat -A PREROUTING -p arp --arp-opcode Request -j arpreply --arpreply-mac 10:11:12:13:14:15 --arpreply-target ACCEPT This is not possible in a standard way, but it is possible with some tricks. Suppose you want to direct traffic to 192.168.0.255, then this should work: # suppose there is no route to 192.168.0.0 yet route add -net 192.168.0.0 netmask 255.255.255.0 dev br0 ifconfig br0 0.0.0.0 arp -s 192.168.0.255 ff:ff:ff:ff:ff:ff iptables -t nat -A PREROUTING -j DNAT --to-destination 192.168.0.255 The bridge device should not have an address in the range of 192.168.0.0/24, because if it does, the routing code won't decide to send the packet out through the bridge device.
The ebtables -A FORWARD --ulog-nlgroup 5 -j DROP
To read the packets sent to userspace, a program needs to be written. Under These example setups were given by ebtables users, and they are very much appreciated. If you have a configuration involving ebtables which you would like to share, please write a little story about it so it can be added here.
|
Last modified: Saturday, 10-Dec-2005 02:16:39 PST.