{"id":243,"date":"2012-12-23T21:05:09","date_gmt":"2012-12-23T21:05:09","guid":{"rendered":"http:\/\/www.timokorthals.de\/?p=243"},"modified":"2016-12-28T00:17:14","modified_gmt":"2016-12-27T23:17:14","slug":"building-an-wireless-router-for-a-wireless-network-with-a-raspberry-pi","status":"publish","type":"post","link":"http:\/\/www.timokorthals.de\/?p=243","title":{"rendered":"Building a wireless router for a wireless network with a Raspberry Pi"},"content":{"rendered":"<p>Hi there,<\/p>\n<p>there is a lack of detailed information about to building a system, that shares it&#8217;s wireless connection which has internet (like eduroam or any other network) via an own wireless AP with it&#8217;s own setup configuration. Because of this, I&#8217;ll publish the manual how to do so with an RPI (or any other Debian system). To have a system like this can be really helpfull if you have an old system which only supports WEP, to connect with an AP which only allows devices wia WPA or certain cerificates to connect.<\/p>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\"><span style=\"color: #ff0000;\"><strong>So what I literally want to do is the following<\/strong><\/span><\/span><\/p>\n<pre class=\"font:monospace lang:default highlight:0 decode:true\">             Raspberry Pi acting as wireless router\r\n             between AP1 and devices via AP2. Security, SSID etc.,\r\n             of AP2 are independent of AP1.\r\n                           _____\r\n\\|\/               \\|\/     | RPI |     \\|\/        \\|\/ |Devices which\r\n |  AP1(e.g.       |      |Linux|      | AP2      |  |use AP2 (NB,\r\n |  eduroam)       |_wlan0|_____|wlan1_|          |__|PDA, etc.<\/pre>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\" data-mce-mark=\"1\"><span style=\"color: #ff0000;\" data-mce-mark=\"1\"><strong>The setup<\/strong><\/span><\/span><\/p>\n<ul>\n<li>Raspberry Pi (256 MB SDRAM) with &#8220;2012-12-16-wheezy-raspbian&#8221;<\/li>\n<li>2GB Kingston microSD card with Kingston microSD-to-SD adapter<\/li>\n<li>DeLOCK powered USB 2.0 HUB (B\/N61393)<\/li>\n<li>2x LogiLink W-LAN USB with a\u00a0Ralink RT5370 chipset<\/li>\n<\/ul>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\"><span style=\"color: #ff0000;\"><strong>The Manual<\/strong><\/span><\/span><\/p>\n<p>I will devide the manual into three parts, while in the first part I will describe how to <!--more-->connect via the first W-LAN stick and the\u00a0<strong>wpa_supplicant<\/strong> to an\u00a0access point (AP)\u00a0which offers an internet connection. The second part describes, how to setup the other W-LAN stick as an AP via <strong>hostapd<\/strong> and <strong>dnsmasq<\/strong>, so that you can connect to your RPI via W-LAN from your notebook (NB). In the last part I&#8217;ll setup the routing between the two W-LAN sticks via <strong>iptables<\/strong>, so that you can use the internet connection of the first W-LAN stick trough the second.<\/p>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline; color: #ff0000;\"><strong>Part I (Connecting to any network)<\/strong><\/span><\/p>\n<p>If you have <strong>wpa_supplicant<\/strong> already installed on your system, everything is fine, otherwise install it via:<\/p>\n<pre>$ sudo apt-get install wpasupplicant<\/pre>\n<p>Now create a new config file, so that the supplicant knows, how to connect to the AP with the internet connection. Create it with the following command:<\/p>\n<pre>$ sudo touch \/etc\/wpa.conf<\/pre>\n<p>Now you can edit the file with your favorite editor (nano, etc.):<\/p>\n<pre># Content of \/etc\/wpa.conf\r\nctrl_interface=\/var\/run\/wpa_supplicant\r\n\r\n# Conenctioninfo for e.g. university of paderborn network\r\n\r\nnetwork={\u00a0\r\n\u00a0 \u00a0 \u00a0 \u00a0 ssid=\"eduroam\"\r\n\u00a0 \u00a0 \u00a0 \u00a0 key_mgmt=WPA-EAP\r\n\u00a0 \u00a0 \u00a0 \u00a0 identity=\"benutzername@uni-paderborn.de\"\r\n\u00a0 \u00a0 \u00a0 \u00a0 private_key=\"\/Verzeichnis\/Network_Certificate.p12\"\r\n\u00a0 \u00a0 \u00a0 \u00a0 private_key_passwd=\"Zertfikiatspasswort\"\r\n\u00a0 \u00a0 \u00a0 \u00a0 priority=0\r\n\u00a0 \u00a0 \u00a0 \u00a0 }<\/pre>\n<p>Search via any search engine you like, to setup the connection information to any other network. You can also try <em>man wpa_supplicant.conf<\/em> in the cli for more information.<\/p>\n<p>For connecting with this network at startup of the system you have to add some lines to the\u00a0<em>\/etc\/network\/interfaces<\/em>\u00a0file. <strong>Make sure that the device you want to connect with is <em>wlan0<\/em> (Or replace <em>wlan0<\/em> with the name of your device e.g. <em>wlan1<\/em>). Be also sure, that there is no other line wich configures your device (Delete or comment every other line, which has <em>wlan0<\/em> in it)<\/strong>:<\/p>\n<pre>## Additional lines in \/etc\/network\/interfaces for wlan0 device\r\n# Start the device at system startup\r\nauto wlan0\r\n# Configure the device via dhcp\r\niface wlan0 inet dhcp\r\n# Use the given file for connecting to the internet\r\nwpa-conf \/etc\/wpa.conf<\/pre>\n<p>Now you can reboot your system and try to ping any website. If it works, be happy.<\/p>\n<p><strong>If it&#8217;s not working<\/strong>, try the following two commands, which kills the actual wpa_supplicant and runs it in debug-mode.<\/p>\n<pre>$ sudo killall wpa_supplicant\r\n$ sudo wpa_supplicant -iwlan0 -c\/etc\/wpa.conf -dd<\/pre>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\"><span style=\"color: #ff0000;\"><strong>Part II (Setup an AP for accessing the Pi via W-LAN)<\/strong><\/span><\/span><\/p>\n<p>Install <strong>hostapd<\/strong> so that the Pi can act as an AP, and <strong>dnsmasq<\/strong> for beeing an dhcp server:<\/p>\n<pre>$ sudo apt-get install hostapd\r\n$ sudo apt-get install dnsmasq<\/pre>\n<p>After installing, you need to setup your W-LAN stick, with an static IP, so that hostapd can work properly. Add the following lines to your <em>\/etc\/network\/interfaces<\/em>:<\/p>\n<pre>## Additional lines in \/etc\/network\/interfaces for wlan1 device\r\n# Setup the device with an static IP\r\niface wlan1 inet static\r\n# Static IP-Address\r\naddress 10.0.0.1\r\n# Networkmask\r\nnetmask 255.255.255.0<\/pre>\n<p>Now, the <strong>hostapd<\/strong> needs to be configured. To do so, you need to tell him, where the configuration file is. Eddit the file<em> \/etc\/default\/hostapd<\/em>\u00a0in the following way:<\/p>\n<pre>DAEMON_CONF=\"\/etc\/hostapd\/hostapd.conf\"<\/pre>\n<p>Now edit the config file, with the wieless setup configuration you like:<\/p>\n<pre class=\"\"># Content of \/etc\/hostapd\/hostapd.conf\r\n# 1. The Device which will act as AP\r\ninterface=wlan1\r\ndriver=nl80211\r\n\r\n# 2. Parameters so that the daemon runs\r\nctrl_interface=\/var\/run\/hostapd\r\nctrl_interface_group=0\r\n\r\n# 3. The Wifi configuration\r\nssid=pi\r\nchannel=6\r\nhw_mode=g\r\nieee80211n=1\r\n\r\n# 4. Security of the Wifi connection\r\nwpa=2\r\nwpa_passphrase=VERYSECRET\r\nwpa_key_mgmt=WPA-PSK\r\nwpa_pairwise=CCMP\r\nrsn_pairwise=CCMP\r\n\r\n# 5. Other settings\r\nbeacon_int=100\r\nauth_algs=3\r\nwmm_enabled=1<\/pre>\n<p>Now you need to configure the <strong>dnsmasq<\/strong>, so that your NB or PDA gets a IP address when it&#8217;s connecting to your Pi. To do so edit the<em>\u00a0\/etc\/dnsmasq.conf<\/em> file in the following way:<\/p>\n<pre>interface=wlan1\r\ndhcp-range=10.0.0.2,10.0.0.128,255.255.255.0,12h<\/pre>\n<p>The above setup means, that <strong>dnsmasq<\/strong> listens only on interface <em>wlan1<\/em> and the second line gives every device which connects to it, an IP address between 10.0.0.2 and 10.0.0.128 for 12 hours.<\/p>\n<p>Now you can reboot the Pi and try to connect to it with the security setup you have choosen.<\/p>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\"><span style=\"color: #ff0000;\"><strong>Part III (Connecting wlan0 and wlan1, so that NB can connect to the inet)<\/strong><\/span><\/span><\/p>\n<p>Now l turn the Raspberry Pi into a router.\u00a0First thing you need to do is to enable packet forwarding.\u00a0In the file <em>\/etc\/sysctl.conf<\/em>, we need to uncomment the following line (should be line 28).<\/p>\n<pre>net.ipv4.ip_forward=1<\/pre>\n<p>After changing that, run this command to re-read the <em>sysctl.conf<\/em> file<\/p>\n<pre>$ sysctl -p<\/pre>\n<p>Now install <strong>iptables<\/strong>, so that the packeges from<strong> wlan1<\/strong> can go through <strong>wlan0<\/strong> and vice versa.<\/p>\n<pre>$ sudo apt-get install iptables<\/pre>\n<p>Now you have to define certain rules, so that the IP packages can be handed over. To do so, create a file and give it rights to be executed on every startup of the system (Update 2013-06-03: Add router.sh script to rc.local. Otherwise it wont run at startup.):<\/p>\n<pre>$ sudo touch \/etc\/network\/if-up.d\/router.sh\r\n$ sudo chmod +x \/etc\/network\/if-up.d\/router.sh\r\n$ sudo su -c \"echo '\/etc\/network\/if-up.d\/router.sh' &gt;&gt; \/etc\/rc.local\"<\/pre>\n<p>Now edit the rules in \/etc\/network\/if-up.d\/router.sh in the following way:<\/p>\n<pre>iptables --table nat --append POSTROUTING --out-interface wlan0 -j MASQUERADE\r\niptables --append FORWARD --in-interface wlan1 -j ACCEPT<\/pre>\n<p>The last step is, to let dnsmasq tell the connected devices, that itself is an gateway to the internet. To do so, edit the file \/etc\/dnsmasq.conf in the folowing way:<\/p>\n<pre>dhcp-option=3,10.0.0.1<\/pre>\n<p>Now, after a reboot, the system should act as mentioned.<\/p>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\"><span style=\"color: #ff0000;\"><strong>Troubleshoot<\/strong><\/span><\/span><\/p>\n<ul>\n<li>Is every W-LAN device is running properly? To check, try <strong>ifconfig <\/strong>in the cli. There should be one <strong>wlan0<\/strong> which has an IP from the AP with the inet connection, and a <strong>wlan1<\/strong> with the static IP 10.0.0.1<\/li>\n<li>Make sure that the rules for <strong>iptables <\/strong>have been executed from <strong>router.sh<\/strong> at startup. For checking, try <strong>iptables &#8211;list<\/strong>, then execute the<strong> router.sh<\/strong> and run <strong>iptables &#8211;list<\/strong> again. If nothing changes, the rules have been setup at startup. If there was a change, then add the rules to<strong> \/etc\/rc.local<\/strong> to run them explicitly on every startup.<\/li>\n<li>Run <strong>wpa_supplicant<\/strong> or <strong>hostapd<\/strong> in debug-mode for more information.<\/li>\n<li>Maybe your your wired ethernet adapter (eth0) is still up. This can cause to some forwarding problems. Just make sure, that eth0 won&#8217;t come up in <em>\/etc\/network\/interfaces<\/em><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hi there, there is a lack of detailed information about to building a system, that shares it&#8217;s wireless connection which has internet (like eduroam or any other network) via an own wireless AP with it&#8217;s own setup configuration. Because of this, I&#8217;ll publish the manual how to do so with an RPI (or any other &hellip; <a href=\"http:\/\/www.timokorthals.de\/?p=243\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Building a wireless router for a wireless network with a Raspberry Pi<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,3,4,5,6],"tags":[],"_links":{"self":[{"href":"http:\/\/www.timokorthals.de\/index.php?rest_route=\/wp\/v2\/posts\/243"}],"collection":[{"href":"http:\/\/www.timokorthals.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.timokorthals.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.timokorthals.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.timokorthals.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=243"}],"version-history":[{"count":1,"href":"http:\/\/www.timokorthals.de\/index.php?rest_route=\/wp\/v2\/posts\/243\/revisions"}],"predecessor-version":[{"id":406,"href":"http:\/\/www.timokorthals.de\/index.php?rest_route=\/wp\/v2\/posts\/243\/revisions\/406"}],"wp:attachment":[{"href":"http:\/\/www.timokorthals.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.timokorthals.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.timokorthals.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}