Useful tips found the hard way.

Friday, October 16, 2009

Mac OSX as a router (IP forwarding with NAT)

OSX has a built-in "share internet" feature which enables IP forwarding and NAT under the covers between to NICs. Unfortunate it also enables a DHCP server on the NATed side. I had a situation where I needed to manually assign network addresses on the NATed side but still wanted IP forwarding to share my internet connection. I needed to do this for a trade show, so just wanted to set up the routing temporarily.

My setup:

en1: my wireless NIC, connected to an internet provider
en0: my wired NIC, which I need to serve a static 10.0.0/24 network

First, enable IP forwarding in the kernel:

sudo sysctl -w net.inet.ip.forwarding=1


Next, run NAT:

sudo natd -interface en1 -v yes

The "-v yes" option will run it in the foreground, which is useful for debugging.

Then, have the firewall forward traffic from the NICs through NAT:

sudo ipfw flush
sudo ipfw add divert natd all from any to any via en1
sudo ipfw add divert natd all from any to any via en0
sudo ipfw add 65535 allow all from any to any

1 comment:

  1. Awesome You are the Great
    MAN
    thankyou so much you saved my toooooo much time
    thanks

    ReplyDelete