dryfire117

How to: Make a Raspberry Pi Powered Wifi Repeater

May 12th, 2015
14,479
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 1. Either start with a fresh install of Rasbian, or make a backup image of your current install
  2.  
  3. 2. Make a backup of the interfaces file with sudo mv /etc/network/interfaces /etc/network/interfaces.BAK
  4.  
  5. 2. Open /etc/network/interfaces with a text editor and paste in what is below (assuming you have the same interfaces):
  6.  
  7. # The loopback network interface
  8. auto lo
  9. iface lo inet loopback
  10.  
  11. # the internal (wired) network interface
  12. allow-hotplug eth0
  13. iface eth0 inet static
  14. address 192.168.2.1
  15. network 192.168.2.0
  16. netmask 255.255.255.0
  17. broadcast 192.168.2.255
  18. gateway 192.168.2.1
  19.  
  20. # the wifi interface communicating with the main router
  21. allow-hotplug wlan0
  22. iface wlan0 inet static
  23. address 192.168.1.98
  24. netmask 255.255.255.0
  25. broadcast 192.168.1.255
  26. gateway 192.168.1.1
  27. # add the wifi network name and password that you are connecting to
  28. wpa-ssid "*********"
  29. wpa-psk "**********"
  30. # if the wifi refuses to connect, try changing TKIP to AES
  31. wpa-group TKIP CCMP
  32. wpa-key-mgmt WPA-PSK
  33.  
  34. # the wifi interface acting as an AP for the repeater
  35. allow-hotplug wlan0
  36. iface wlan1 inet static
  37. address 192.168.3.1
  38. network 192.168.3.0
  39. netmask 255.255.255.0
  40. broadcast 192.168.3.255
  41. gateway 192.168.3.1
  42.  
  43. pre-up iptables-restore < /etc/network/iptables
  44.  
  45. 3. Reboot the pi with sudo shutdown -r now to let the interface settings take effect
  46.  
  47. *wonder why you don't have a pi 2*
  48.  
  49. 4. Open a script called resetiptables.sh and fill it with the following:
  50.  
  51. #!/bin/sh
  52. IPTABLES="$(which iptables)"
  53. # RESET DEFAULT POLICIES
  54. $IPTABLES -P INPUT ACCEPT
  55. $IPTABLES -P FORWARD ACCEPT
  56. $IPTABLES -P OUTPUT ACCEPT
  57. $IPTABLES -t nat -P PREROUTING ACCEPT
  58. $IPTABLES -t nat -P POSTROUTING ACCEPT
  59. $IPTABLES -t nat -P OUTPUT ACCEPT
  60. $IPTABLES -t mangle -P PREROUTING ACCEPT
  61. $IPTABLES -t mangle -P OUTPUT ACCEPT
  62. # FLUSH ALL RULES, ERASE NON-DEFAULT CHAINS
  63. $IPTABLES -F
  64. $IPTABLES -X
  65. $IPTABLES -t nat -F
  66. $IPTABLES -t nat -X
  67. $IPTABLES -t mangle -F
  68. $IPTABLES -t mangle -X
  69.  
  70. 5. chmod 755 resetiptables
  71.  
  72. 6. sudo ./resetiptables. This removes any table rules you currently have.
  73.  
  74. 7. This is where the magic happens. Open a script called setupiptables.sh and fill it with the following:
  75.  
  76. #!/bin/sh
  77. IPT=/sbin/iptables
  78. LOCAL_IFACE=eth0
  79. INET_IFACE=wlan0
  80. AP_IFACE=wlan1
  81. INET_ADDRESS=192.168.1.98
  82. # Flush the tables
  83. $IPT -F INPUT
  84. $IPT -F OUTPUT
  85. $IPT -F FORWARD
  86. $IPT -t nat -P PREROUTING ACCEPT
  87. $IPT -t nat -P POSTROUTING ACCEPT
  88. $IPT -t nat -P OUTPUT ACCEPT
  89. # Allow forwarding packets:
  90. $IPT -A FORWARD -p ALL -i $LOCAL_IFACE -o $INET_IFACE -j ACCEPT
  91. $IPT -A FORWARD -p ALL -i $AP_IFACE -o $INET_IFACE -j ACCEPT
  92. $IPT -A FORWARD -i $INET_IFACE -o $LOCAL_IFACE -m state --state ESTABLISHED,RELATED -j ACCEPT
  93. $IPT -A FORWARD -i $INET_IFACE -o $AP_IFACE -m state --state ESTABLISHED,RELATED -j ACCEPT
  94. # Packet masquerading
  95. #$IPT -t nat -A POSTROUTING -o $LOCAL_IFACE -j MASQUERADE
  96. # I'm pretty sure this is unneeded but I added it because I'm mirroring the original script
  97. #$IPT -t nat -A POSTROUTING -o $AP_IFACE -j MASQUERADE
  98. $IPT -t nat -A POSTROUTING -o $INET_IFACE -j SNAT --to-source $INET_ADDRESS
  99.  
  100. 8. chmod 755 setupiptables
  101.  
  102. 9. sudo ./setupiptables
  103.  
  104. 10. sudo iptables-save > dump.txt
  105.  
  106. 11. sudo cp dump.txt /etc/network/iptables
  107.  
  108. 12. sudo nano /etc/sysctl.conf and uncomment the line net.ipv4.ip_forward=1
  109.  
  110. 13. sudo sysctl --system
  111.  
  112. 14. Open a script called setuproutes and fill it with the following:
  113.  
  114. #!/bin/sh
  115. #/etc/init.d/setuproutes
  116. #
  117. ### BEGIN INIT INFO
  118. # Provides: default routes for wlan0
  119. # Required-Start: $syslog $network
  120. # Required-Stop: $syslog
  121. # Should-Start:
  122. # Should-Stop:
  123. # X-Start-Before:
  124. # X-Stop-After:
  125. # Default-Start: 2 3 4 5
  126. # Default-Stop: 0 1 6
  127. # X-Interactive: false
  128. # Short-Description: adjust default routes
  129. # Description: removes default route 192.168.2.1 adds default route 192.168.1.1
  130. ### END INIT INFO
  131. sudo route del default gw 192.168.2.1 eth0
  132. sudo route del default gw 192.168.3.1 wlan1
  133. sudo route add default gw 192.168.1.1 wlan0
  134. exit 0
  135.  
  136. 15. chmod 755 setuproutes
  137.  
  138. 16. sudo cp setuproutes /etc/init.d
  139.  
  140. 17. sudo update-rc.d setuproutes defaults
  141.  
  142. 18. sudo shutdown -r now
  143.  
  144. *crack a beer*
  145.  
  146. 19. sudo nano sudo nano /etc/init.d/wlan-restart and fill it with the following:
  147.  
  148. #!/bin/sh
  149. #
  150. #
  151.  
  152. ### BEGIN INIT INFO
  153. # Provides: wlan-restart
  154. # Required-Start: $network
  155. # Required-Stop: $network
  156. # Should-Start:
  157. # Should-Stop:
  158. # Default-Start: 2 3 4 5
  159. # Default-Stop: 0 1 6
  160. # Short-Description: Restarts wlan interfaces
  161. # Description: Restarts wlan interfaces to fix problem with static address in WiFi AP
  162. # X-Start-Before: hostapd isc-dhcp-server
  163. ### END INIT INFO
  164.  
  165. PATH=/sbin:/usr/sbin:$PATH
  166.  
  167. do_start() {
  168. ifdown wlan0
  169. ifup wlan0
  170. ifdown wlan1
  171. ifup wlan1
  172. }
  173.  
  174. case "$1" in
  175. start)
  176. do_start
  177. ;;
  178. restart|reload|force-reload)
  179. echo "Error: argument '$1' not supported" >&2
  180. exit 3
  181. ;;
  182. stop)
  183. ;;
  184. status)
  185. ;;
  186. *)
  187. echo "Usage: $0 start|stop" >&2
  188. exit 3
  189. ;;
  190. esac
  191. exit 0
  192.  
  193. 20. sudo chmod 755 /etc/init.d/wlan-restart
  194.  
  195. 21. sudo update-rc.d wlan-restart defaults
  196.  
  197.  
  198. 22. sudo apt-get update
  199.  
  200. *take a quick nap*
  201.  
  202. 23. sudo apt-get install isc-dhcp-server
  203.  
  204. *strum fingers impatiently*
  205.  
  206. 24. sudo mv /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.orig
  207.  
  208. 25. sudo nano /etc/dhcp/dhcpd.conf and fill it with the following:
  209.  
  210. #
  211. # Configuration file for ISC dhcpd for Debian
  212. #
  213. #
  214. ddns-update-style none;
  215. option domain-name "rbn.local";
  216. option domain-name-servers 192.168.1.1;
  217. default-lease-time 600;
  218. max-lease-time 7200;
  219. authoritative;
  220. log-facility local7;
  221. #let the server know about network 192.168.1.0 but offer no service here
  222. subnet 192.168.1.0 netmask 255.255.255.0{
  223. }
  224. #configure service for network 192.168.2.0 (the ethernet port)
  225. subnet 192.168.2.0 netmask 255.255.255.0 {
  226. range 192.168.2.10 192.168.2.50;
  227. option routers 192.168.2.1;
  228. option ip-forwarding off;
  229. }
  230. #configure service for nework 192.168.3.0 (the wireless AP)
  231. subment 192.168.3.0 netmask 255.255.255.0 {
  232. range 192.168.3.10 192.168.3.50;
  233. option routers 192.168.3.1;
  234. option ip-forwarding off;
  235. }
  236.  
  237. 26. sudo nano /etc/default/isc-dhcp-server and change INTERFACES="" to INTERFACES="eth0 wlan1"
  238.  
  239. 27. sudo service isc-dhcp-server restart
  240.  
  241. Almost there! time to start making the new wireless network
  242.  
  243. 28. sudo apt-get install hostapd
  244.  
  245. Now, this wasn't covered very well in either tutorial, which is why I made this tutorial. In short, we have to compile the lastest hostapd files and use them to replace the ones we downloaded from apt-get.
  246.  
  247. 29. git clone git://w1.fi/srv/git/hostap.git
  248.  
  249. 30. cd hostap/hostapd
  250.  
  251. 31. cp defconfig .config
  252.  
  253. 32. nano .config
  254.  
  255. 33. uncomment this line #CONFIG_DRIVER_NL80211=y
  256.  
  257. 34. uncomment this line #CONFIG_IEEE80211N=y
  258.  
  259. 35. sudo apt-get install libnl-dev
  260.  
  261. The next command will compile the hostapd files, I admit, there are two dependencies that need to be solved, and I forget what they are off the top of my head :( you can solve them with apt-get though
  262.  
  263. 36. make
  264.  
  265. *browse reddit because this is going to take forever*
  266.  
  267. 36. sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.ORIG
  268.  
  269. 37. sudo mv hostapd /usr/sbin
  270.  
  271. 38. sudo chmod 755 /usr/sbin/hostapd
  272.  
  273. Woot! So that updated hostapd to the latest version and installed the nl80211 driver which may come in handy later
  274.  
  275. 39. sudo nano /etc/hostapd/hostapd.conf and fill it with the following:
  276.  
  277. interface=wlan1
  278. #if you get an error, try uncommenting rt1871xdrv and commenting nl80111
  279. #driver=rtl871xdrv
  280. driver=nl80211
  281. # change this to what you want
  282. ssid=Pi_AP
  283. hw_mode=g
  284. channel=6
  285. macaddr_acl=0
  286. auth_algs=1
  287. ignore_broadcast_ssid=0
  288. wpa=2
  289. # I'd change your password to something a bit less obvious
  290. wpa_passphrase=Raspberry
  291. wpa_key_mgmt=WPA-PSK
  292. wpa_pairwise=TKIP
  293. rsn_pairwise=CCMP
  294.  
  295. 40. sudo nano /etc/default/hostapd
  296.  
  297. 41. Find #DAEMON_CONF="" and uncomment it plus change to DAEMON_CONF="/etc/hostapd/hostapd.conf"
  298.  
  299. 42. sudo service hostapd start
  300.  
  301. 43. sudo update-rc.d hostapd enable
  302.  
  303. 44. And finally..... sudo shutdown -r now.
  304.  
  305. When you reboot, you should be able to connect to the wifi network being broadcast from the pi, and have internet access!!
RAW Paste Data