Saturday, November 15, 2008

FreeBSD 101: Creating a Bridge using FreeBSD

The reason why I came across with this application is that I want to sniff and examine packets lurking around the network. I could not use my switch since I only have an unmanageable switch so I cannot configure it to do port mirroring. I tried to look for hubs but could not find either. But I do have a machine that has multiple Network Interface Cards in it so I look for ways to configure these ports to broadcast frames/packets that it received from one port to all of the other ports so I can sniff any of the packets that enter in any of the ports.

I have done this before with the help of a friend but I cannot remember the exact details on how to do it. I'm not that type of guy who takes down notes for every new procedures/configurations I have done. So I spent some time googling around and finally found this link. I am going to summarize the procedures by posting a simple shell script that will create a bridge interface in FreeBSD.

#!/bin/sh
# Filename: createbridge.sh

# load the kernel loadable module
/sbin/kldload /boot/kernel/if_bridge.ko

# create a bridge interface
/sbin/ifconfig bridge0 create

# add the network interfaces you want as member of the bridge
/sbin/ifconfig bridge0 addm em0 addm em1 addm em2

# bring these interfaces up
/sbin/ifconfig em0 up
/sbin/ifconfig em1 up
/sbin/ifconfig em2 up

# then you can assign an ip address to your bridge interface if you want
/sbin/ifconfig bridge0 inet 192.168.1.1/24



Save this script at /usr/local/etc/rc.d so that it will get executed during boot-up process. Then make the script executable and execute it:

# chmod 755 /usr/local/etc/rc.d/createbridge.sh
# /usr/local/etc/rc.d/createbridge.sh


Just as there are many ways to kill a cat, there are also other ways to create a bridge interface in FreeBSD. One of which is to do it using rc.conf. But I think you still need to add "device if_bridge" into your kernel configuration /usr/src/sys/{arch}/conf/GENERIC so that it will be dynamically loaded by the kernel everytime you create a bridge interface.

No comments: