fucking the white bunny rabbit

this blog is a feature

Simple & Easy NAT between wifi and eth

with 6 comments

Sometimes happen the same situation: one ethernet port, one ethernet cable, no hub/switch and many PCs with wireless.
A good solution is to connect a pc with ethernet and create a WLAN (ad hoc or infrastructure) to connect other PCs to internet.
Too many time I had this problem and the solution is always the same or similar, so I wrote this useful and reusable bash script to configure wifi card (chipset Atheros with madwifi driver) and create a NAT to connect AdHoc WLAN.


#!/bin/bash
# inizializing ethernet (I suppose outer network have address 193.205.22.12)
# I suggest to disable NetworkManager and kill dhclient
ifconfig eth0 193.205.22.12

# Now I need to unload and reload module with option "autocreate=adhoc". This
# simplify the creation of virttual interfaces athX (See how madwifi work for more informations)
/sbin/rmmod ath_pci
modprobe ath_pci autocreate=adhoc


# Configuring essid (In this case I use essid "spongepowa"
iwconfig ath0 essid spongepowa
# Configuring WLAN address (I suppose my network is 192.168.1.0/24)
ifconfig ath0 192.168.1.1

# Enabling forwarding
/bin/echo "1" > /proc/sys/net/ipv4/ip_forward

# Inizializing iptables
iptables --flush
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P PREROUTING ACCEPT

# Making rules:
# - 192.168.1.0/24 is local WLAN addresses
# - eth0 is the output interface (ethernet interface), ath0 is the input interface (wifi interface)
iptables -A FORWARD -s 192.168.1.0/24 -d 0/0 -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -d 192.168.1.0/24 -j ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -s 192.168.1.0/24 -j MASQUERADE

I hope this is correct. If you have any question ask me by comment.

If my english is full of mistakes, tell me (AYBABTU is not allowed without motivation :þ)…with my mistakes, of course.

Written by whitenoise

September 27, 2007 at 12:37 am

6 Responses

Subscribe to comments with RSS.

  1. I didn’t know this trick. I have an Intel IPW2200 wireless card on my laptop, but I guess it shouldn’t be difficult to adjust your howto to fit my needs.. Have you got some link to draw from?

    Stefano Costa

    September 27, 2007 at 10:10 am

  2. uhm, I don’t have link (try with google if you need ;-) ), but the operation to do are:
    1) configure ethernet ip address
    2) switch wifi to ad-hoc mode (in my case I have to unload and reload module, but you can do with “iwconfig $wifi_interface mode adhoc”
    3) create adHoc essid and configure ip address
    4) copy and paste my last part and change wifi interface and LAN ip address with data of your net….

    if you copy and past my script and try to adapt to your configuration i will be more simpe…trust me ;-)

    whitenoise

    September 27, 2007 at 10:41 am

  3. Howdy,…

    I adjested your script a bit (to make it work with bcm43xx also) and put it on my blog at :
    http://bsh83.blogspot.com/search/label/Ad%20-%20Hoc

    Jabka

    November 21, 2007 at 1:14 pm

  4. thanks :)

    I’m happy it is useful.. :-)

    I guess if you link my post, blogspot send a pingback on my blog as comment ;-)

    thanks again :)

    whitenoise

    November 21, 2007 at 1:25 pm

  5. np .

    the problem is that im not so good with perl and awk ( i wish to adjust it that so it can dinf the ip of the eth0/ath0.

    Jabka

    November 21, 2007 at 6:31 pm

  6. You can get an ip of interface in the next way :
    ip route show|grep ethX|awk ‘{ print $1}’

    jabka

    December 4, 2007 at 7:59 pm


Leave a Reply