# [workaround] broadcasting through router

## bugmenot

hi

i connect to the internet using my phone (/etc/init.d/net.usb0)

i want to pipe this connection from my computer into a router and broadcast it

how would i go about doing this? 

```

eth0      Link encap:Ethernet  HWaddr e0:cb:4e:44:f3:83 

          inet addr:192.168.1.100  Bcast:192.168.1.255  Mask:255.255.255.0

          inet6 addr: fe80::e2cb:4eff:fe44:f383/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:1219 errors:0 dropped:0 overruns:0 frame:0

          TX packets:1140 errors:0 dropped:0 overruns:0 carrier:23

          collisions:0 txqueuelen:1000

          RX bytes:818864 (799.6 KiB)  TX bytes:105753 (103.2 KiB)

          Interrupt:19

lo        Link encap:Local Loopback 

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:16436  Metric:1

          RX packets:425 errors:0 dropped:0 overruns:0 frame:0

          TX packets:425 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:33368 (32.5 KiB)  TX bytes:33368 (32.5 KiB)

usb0      Link encap:Ethernet  HWaddr fa:f5:7a:37:17:9a 

          inet addr:172.20.23.XXX  Bcast:172.20.23.255  Mask:255.255.255.252

          inet6 addr: fe80::f8f5:7aff:fe37:179a/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:3056 errors:30 dropped:0 overruns:0 frame:30

          TX packets:3988 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:1983935 (1.8 MiB)  TX bytes:675978 (660.1 KiB)

```

i also have an ath9k wireless cardLast edited by bugmenot on Wed Sep 15, 2010 8:32 pm; edited 1 time in total

----------

## BradN

Look up generic instructions for internet connection sharing (NAT routing) in linux - this will apply to your situation as well.  What it ends up being, is you have a couple iptables rules that enable NAT routing, and a sysctl entry that turns on IP forwarding in the kernel (as by default, the kernel won't route packets for other machines).

Some parts may depend on your router, depending which services (dhcp, dns) you want to run on the router vs the computer, and whether the router supports redirecting to another gateway.  Worst case, you may have to disable dhcp and dns on the router and configure these on the NAT machine.

----------

## bugmenot

i'm trying to follow this guide: http://www.gentoo.org/doc/en/home-router-howto.xml

but i'm unsure how to apply it to my situation

for iptables, it says

# export LAN=eth0

# export WAN=eth1

should i substitute eth1 with usb0?

also; what settings should i use on my router?

i'm assuming a static ip

Internet IP Address: 192.168.0.1?

Subnet Mask: 255.255.255.0?

Default Gateway: ???

DNS 1: ???

Local IP Address: ???

----------

## BradN

should i substitute eth1 with usb0?

Yes.

For your router, the ideal settings are as follows:

No internet connection (WAN port disabled), or just unplug the WAN port, or maybe Internet IP Address 0.0.0.0.

DHCP using normal settings, _except_ its gateway should point at your NAT machine (192.168.1.100 as per your current settings, but change this to a static IP, like 192.168.1.2 or so, as in, your NAT machine should not be using DHCP to get its address for the local network).

DNS - either point this at your ISP's dns server, or point it at OpenDNS (208.67.222.222)

Local IP address: 192.168.1.1 is fine

Gateway: See above under DHCP

If your router is capable of running dd-wrt, I strongly recommend using it as it allows almost any customization like this you can think of.  You can even switch the WAN port into a 5th LAN port.

If you can't properly set the DHCP on your router (especially gateway and DNS settings), you need to turn that part off and set up something like dnsmasq on your NAT machine.  If you can't set them correctly or turn them off, your router might not be suitable.

----------

## bugmenot

using these iptables settings:

iptables -F

iptables -t nat -F

iptables -P INPUT ACCEPT

iptables -P OUTPUT ACCEPT

iptables -P FORWARD DROP

export LAN=eth0

export WAN=usb0

iptables -I INPUT 1 -i ${LAN} -j ACCEPT

iptables -I INPUT 1 -i lo -j ACCEPT

iptables -A INPUT -p UDP --dport bootps ! -i ${LAN} -j REJECT

iptables -A INPUT -p UDP --dport domain ! -i ${LAN} -j REJECT

iptables -A INPUT -p TCP --dport ssh -i ${WAN} -j ACCEPT

iptables -A INPUT -p TCP ! -i ${LAN} -d 0/0 --dport 0:1023 -j DROP

iptables -A INPUT -p UDP ! -i ${LAN} -d 0/0 --dport 0:1023 -j DROP

iptables -I FORWARD -i ${LAN} -d 192.168.0.0/255.255.0.0 -j DROP

iptables -A FORWARD -i ${LAN} -s 192.168.0.0/255.255.0.0 -j ACCEPT

iptables -A FORWARD -i ${WAN} -d 192.168.0.0/255.255.0.0 -j ACCEPT

iptables -t nat -A POSTROUTING -o ${WAN} -j MASQUERADE

ip address and default gateway have to be in the same subnet mask for the router to accept it

using these settings:

Internet IP Address: 	192.168.1.1

Subnet Mask: 	255.255.255.0

Default Gateway: 	192.168.1.100 (for now)

DNS 1: 	opendns

local ip:   192.168.1.1

what should my conf.d/net settings be?

i'm using eth0=( "dhcp" ) and usb0=( "dhcp" )

also, do i plug the Ethernet cable from my computer into the "internet" slot on the router or one of the four Ethernet slots?

----------

## BradN

First, where you have 192.168.0.*, change it to 192.168.1.*

The subnet masks should be the same as your router for simplicity reasons (but 255.255.0.0 will work fine here as well but really should be 255.255.255.0)

eth0=( "192.168.1.2/24" )

This sets your eth0 statically to 192.168.1.2 with netmask 255.255.255.0 (24 1's -> 3 bytes of 255)

Plug into one of the LAN ports on your router, leave WAN disconnected unless you can configure it to act as an extra LAN port.

----------

## bugmenot

Thank you for your informative replies.

I think I'm getting closer   :Very Happy: 

Here is everything I've tried:

/etc/conf.d/net:

```
config_usb0=( "dhcp" )

config_eth0=( "192.168.1.2/24" )
```

iptables:

```
iptables -F

iptables -t nat -F

iptables -P INPUT ACCEPT

iptables -P OUTPUT ACCEPT

iptables -P FORWARD DROP

export LAN=eth0

export WAN=usb0

iptables -I INPUT 1 -i ${LAN} -j ACCEPT

iptables -I INPUT 1 -i lo -j ACCEPT

iptables -A INPUT -p UDP --dport bootps ! -i ${LAN} -j REJECT

iptables -A INPUT -p UDP --dport domain ! -i ${LAN} -j REJECT

iptables -A INPUT -p TCP --dport ssh -i ${WAN} -j ACCEPT

iptables -A INPUT -p TCP ! -i ${LAN} -d 0/0 --dport 0:1023 -j DROP

iptables -A INPUT -p UDP ! -i ${LAN} -d 0/0 --dport 0:1023 -j DROP

iptables -I FORWARD -i ${LAN} -d 192.168.1.0/255.255.255.0 -j DROP

iptables -A FORWARD -i ${LAN} -s 192.168.1.0/255.255.255.0 -j ACCEPT

iptables -A FORWARD -i ${WAN} -d 192.168.1.0/255.255.255.0 -j ACCEPT

iptables -t nat -A POSTROUTING -o ${WAN} -j MASQUERADE 

/etc/init.d/iptables save 
```

router (linksys wrt120n):

```
Internet Connection Type

[] DHCP

[x] Static IP

[] PPPoE

[] PPPTP

[] L2TP

[] Telstra Cable

Internet IP Address: 192.168.1.1

Subnet Mask: 255.255.255.0

Default Gateway: 192.168.1.2

DNS: 208.67.222.222

Local IP Address: 192.168.1.1

DHCP Server [x] enabled

Start Address: 192.168.1.100

Max 50 Users

Range: 192.168.1.100 - 149

Routing Table:

Dest IP              Subnet Mask        Gateway      Interface

0.0.0.0              0.0.0.0                 192.168.1.2  WAN(Internet)

208.67.222.222  255.255.255.255   192.168.1.2  WAN(Internet)

192.168.1.2       255.255.255.255   192.168.1.1  WAN(Internet)

192.168.1.0       255.255.255.0       192.168.1.1 LAN & Wireless

127.0.0.1          255.255.255.255   127.0.0.1      Loopback
```

starting services:

```
/etc/init.d/iptables start

/etc/init.d/net.usb0 start

/etc/init.d/net.eth0 start
```

still, computer B can't access the internet and the "web" light on the router is not lit

this leads me to believe i'm not using iptables correctly (or incorrect router settings)

i'm pretty sure the router is capable of this, since i've used it as a network switch in the past (internet -> router a -> router b -> LAN)

ifconfig:

```
eth0      Link encap:Ethernet  HWaddr e0:cb:4e:44:f3:83  

          inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0

          inet6 addr: fe80::e2cb:4eff:fe44:f383/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:385 errors:0 dropped:0 overruns:0 frame:0

          TX packets:369 errors:0 dropped:0 overruns:0 carrier:13

          collisions:0 txqueuelen:1000 

          RX bytes:227749 (222.4 KiB)  TX bytes:34588 (33.7 KiB)

          Interrupt:19 

lo        Link encap:Local Loopback  

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:16436  Metric:1

          RX packets:72 errors:0 dropped:0 overruns:0 frame:0

          TX packets:72 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0 

          RX bytes:4500 (4.3 KiB)  TX bytes:4500 (4.3 KiB)

usb0      Link encap:Ethernet  HWaddr fa:f5:7a:37:17:9a  

          inet addr:172.20.23.xxx  Bcast:172.20.23.255  Mask:255.255.255.252

          inet6 addr: fe80::f8f5:7aff:fe37:179a/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:2591 errors:13 dropped:0 overruns:0 frame:13

          TX packets:3315 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:1740041 (1.6 MiB)  TX bytes:575461 (561.9 KiB)
```

emerge --info:

```
Portage 2.1.8.3 (default/linux/x86/10.0/desktop, gcc-4.4.3, glibc-2.11.2-r0, 2.6.34-gentoo-r1 i686)

=================================================================

System uname: Linux-2.6.34-gentoo-r1-i686-Intel-R-_Atom-TM-_CPU_N270_@_1.60GHz-with-gentoo-1.12.13

Timestamp of tree: Sun, 12 Sep 2010 15:30:01 +0000

app-shells/bash:     4.1_p7

dev-java/java-config: 2.1.11

dev-lang/python:     2.6.5-r3, 3.1.2-r4

dev-util/cmake:      2.8.1-r2

sys-apps/baselayout: 1.12.13

sys-apps/sandbox:    1.6-r2

sys-devel/autoconf:  2.13, 2.65

sys-devel/automake:  1.4_p6-r1, 1.8.5-r4, 1.9.6-r3, 1.10.3, 1.11.1

sys-devel/binutils:  2.20.1-r1

sys-devel/gcc:       4.4.3-r2

sys-devel/gcc-config: 1.4.1

sys-devel/libtool:   2.2.6b

sys-devel/make:      3.81-r2

virtual/os-headers:  2.6.30-r1

ACCEPT_KEYWORDS="x86"

ACCEPT_LICENSE="* -@EULA"

CBUILD="i686-pc-linux-gnu"

CFLAGS="-O2 -march=core2 -mtune=generic -fomit-frame-pointer -pipe"

CHOST="i686-pc-linux-gnu"

CONFIG_PROTECT="/etc /usr/share/X11/xkb /var/lib/hsqldb"

CONFIG_PROTECT_MASK="/etc/ca-certificates.conf /etc/env.d /etc/env.d/java/ /etc/fonts/fonts.conf /etc/gconf /etc/revdep-rebuild /etc/sandbox.d /etc/terminfo"

CXXFLAGS="-O2 -march=core2 -mtune=generic -fomit-frame-pointer -pipe"

DISTDIR="/usr/portage/distfiles"

FEATURES="assume-digests distlocks fixpackages news parallel-fetch protect-owned sandbox sfperms strict unmerge-logs unmerge-orphans userfetch"

GENTOO_MIRRORS="http://distfiles.gentoo.org"

LDFLAGS="-Wl,-O1 -Wl,--sort-common -Wl,--warn-once"

MAKEOPTS="-j2"

PKGDIR="/usr/portage/packages"

PORTAGE_CONFIGROOT="/"

PORTAGE_RSYNC_OPTS="--recursive --links --safe-links --perms --times --compress --force --whole-file --delete --stats --timeout=180 --exclude=/distfiles --exclude=/local --exclude=/packages"

PORTAGE_TMPDIR="/var/tmp"

PORTDIR="/usr/portage"

SYNC="rsync://rsync.gentoo.org/gentoo-portage"

USE="X a52 aac acl acpi alsa berkdb bittorrent bluetooth branding bzip2 cairo cdr cli consolekit cracklib crypt cups cxx dbus dri dts dvd dvdr emboss encode exif extras fam firefox flac fortran gdbm gif gnutls gpm hal iconv ipv6 java jpeg lcms ldap libnotify mad mikmod mng modules mp3 mp4 mpeg mudflap ncurses nls nptl nptlonly nsplugin ogg opengl openmp pam pango pcre pdf perl png ppds pppd python readline reflection sdl session spell ssl startup-notification svg sysfs tcpd thunar tiff truetype unicode usb vorbis x264 x86 xcb xml xorg xulrunner xv xvid zlib" ALSA_CARDS="ali5451 als4000 atiixp atiixp-modem bt87x ca0106 cmipci emu10k1 emu10k1x ens1370 ens1371 es1938 es1968 fm801 hda-intel intel8x0 intel8x0m maestro3 trident usb-audio via82xx via82xx-modem ymfpci" ALSA_PCM_PLUGINS="adpcm alaw asym copy dmix dshare dsnoop empty extplug file hooks iec958 ioplug ladspa lfloat linear meter mmap_emul mulaw multi null plug rate route share shm softvol" APACHE2_MODULES="actions alias auth_basic authn_alias authn_anon authn_dbm authn_default authn_file authz_dbm authz_default authz_groupfile authz_host authz_owner authz_user autoindex cache cgi cgid dav dav_fs dav_lock deflate dir disk_cache env expires ext_filter file_cache filter headers include info log_config logio mem_cache mime mime_magic negotiation rewrite setenvif speling status unique_id userdir usertrack vhost_alias" ELIBC="glibc" INPUT_DEVICES="evdev synaptics" KERNEL="linux" LCD_DEVICES="bayrad cfontz cfontz633 glk hd44780 lb216 lcdm001 mtxorb ncurses text" RUBY_TARGETS="ruby18" USERLAND="GNU" VIDEO_CARDS="intel" XTABLES_ADDONS="quota2 psd pknock lscan length2 ipv4options ipset ipp2p iface geoip fuzzy condition tee tarpit sysrq steal rawnat logmark ipmark dhcpmac delude chaos account" 

Unset:  CPPFLAGS, CTARGET, EMERGE_DEFAULT_OPTS, FFLAGS, INSTALL_MASK, LANG, LC_ALL, LINGUAS, PORTAGE_COMPRESS, PORTAGE_COMPRESS_FLAGS, PORTAGE_RSYNC_EXTRA_OPTS, PORTDIR_OVERLAY
```

----------

## BradN

Internet IP Address: 192.168.1.1 

This part isn't right, try to set this to 0.0.0.0 netmask 255.255.255.255 if it lets you.  If it doesn't let you, set it to dhcp and leave the WAN port disconnected so it doesn't obtain an address on it.

Here is a more concise and direct script that should start up plain NAT forwarding (the ip_forward line is critical and you either need it in a startup script or you need to configure that option I think in /etc/conf.d/sysctl or some file like that in /etc somewhere):

```
iptables -F 

iptables -t nat -F

iptables-save #make sure this outputs an empty iptables config with ACCEPT by default so we know it's clean of old settings 

iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE 

/etc/init.d/iptables save

echo 1 > /proc/sys/net/ipv4/ip_forward
```

Also, please check "ifconfig; route -n; cat /etc/resolv.conf" output on a connected client, or ipconfig /all in windows so that the settings DHCP is giving out can be verified.

----------

## bugmenot

had to set to dhcp

"ip address and gateway must be in same subnet mask"

i wish it was dd-wrt compatible

anyway, here's the output on computer b

lo        Link encap:Local Loopback  

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:16436  Metric:1

          RX packets:148 errors:0 dropped:0 overruns:0 frame:0

          TX packets:148 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0 

          RX bytes:13122 (12.8 KiB)  TX bytes:13122 (12.8 KiB)

wlan0     Link encap:Ethernet  HWaddr 00:24:23:04:ae:e1  

          inet addr:192.168.1.100  Bcast:192.168.1.255  Mask:255.255.255.0

          inet6 addr: fe80::224:23ff:fe04:aee1/64 Scope:Link

          UP BROADCAST MULTICAST  MTU:1500  Metric:1

          RX packets:7136 errors:0 dropped:0 overruns:0 frame:0

          TX packets:4895 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:4364544 (4.1 MiB)  TX bytes:515940 (503.8 KiB)

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.1.0     0.0.0.0         255.255.255.0   U     2000   0        0 wlan0

127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo

0.0.0.0         192.168.1.1     0.0.0.0         UG    2000   0        0 wlan0

# Generated by dhcpcd from wlan0

# /etc/resolv.conf.head can replace this line

nameserver 192.168.1.1

# /etc/resolv.conf.tail can replace this line

not sure how to set the gateway with dhcp. it makes the option disappear.

when i used it as a switch, i had it on static ip, if that makes any difference.

----------

## BradN

It looks like you might have to use dhcpd or dnsmasq instead of the router's DHCP server, unless you can find a way to change the gateway and DNS it's reporting to the clients.

Or, you could statically assign addresses to the clients.

----------

## bugmenot

when i used it as a network switch, i had the router reserve an ip for the other router (dhcp reservation) and simply set the other router's local ip address as the gateway and set the internet ip address and local ip address of the switch-router to something outside the range of the other router's dhcp server. i can add to the router's routing table but i can't edit it.

i've tried disabling a few firewalls and such in the router but i still can't get through

maybe there's another way to do this? with my wireless card or something?

or should i be using SNAT instead of iptables?

i don't understand much about networking   :Sad: 

route -n:

Kernel IP routing table

```
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

172.20.23.252   0.0.0.0         255.255.255.252 U     0      0        0 usb0

192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0

127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo

0.0.0.0         172.20.23.254   0.0.0.0         UG    0      0        0 usb0

0.0.0.0         192.168.1.1     0.0.0.0         UG    1      0        0 eth0
```

----------

## bugmenot

I was able to use my wireless card in ad-hoc mode to achieve the same end and broadcast my phone's 3g connection directly from my computer, cutting the router out of the equation all together  :Very Happy: 

Thanks for your help Brad.

Excuse me while I cancel my DSL plan with my ISP.   :Cool: 

----------

## BradN

Ohh, if I had known your phone was acting as a DHCP server and could handle multiple clients and all that, I could have just told you to use bridging, but bridging has its own issues when done over wireless...  I was assuming the phone could only handle one client.

----------

