{"id":184,"date":"2018-04-28T22:23:13","date_gmt":"2018-04-28T21:23:13","guid":{"rendered":"https:\/\/www.fukr.org.uk\/?p=184"},"modified":"2018-06-20T15:31:35","modified_gmt":"2018-06-20T14:31:35","slug":"using-a-raspberry-pi-2-as-a-router-on-a-stick-starring-netbsd","status":"publish","type":"post","link":"https:\/\/www.fukr.org.uk\/?p=184","title":{"rendered":"Using a Raspberry Pi 2 as a Router on a Stick Starring NetBSD"},"content":{"rendered":"<p>A few weeks ago I set about upgrading my feeble networking skills by playing around with a Cisco 2970 switch. I set up a couple of VLANs and found the urge to set up a router to route between them. The 2970 isn&#8217;t a modern layer 3 switch so what am I to do?<\/p>\n<p>Why not make use of the Raspberry Pi 2 that I&#8217;ve never used and put it to some good use as a &#8216;router on a stick&#8217;.<\/p>\n<p>I could install a Linux based OS as I am quite familiar with it but where&#8217;s the fun in that? In my home lab I use SmartOS which by the way is a shit hot hypervisor but as far as I know there aren&#8217;t any Illumos distributions for the Raspberry Pi. On the desktop I use Solus OS which is by far the slickest Linux based OS that I&#8217;ve had the pleasure to use but Solus&#8217; focus is purely desktop. It&#8217;s looking like BSD then!<\/p>\n<p>I believe FreeBSD is renowned for it&#8217;s top notch networking stack and so I wrote to the BSDNow show on Jupiter Broadcasting for some help but it seems that the FreeBSD chaps from the show are off on a jolly to some BSD conference or another(love the show by the way).<\/p>\n<p>It looks like me and the luvverly NetBSD are on a date this Saturday. I&#8217;ve always had a secret love for NetBSD. She&#8217;s a beautiful, charming and promiscuous lover(looking at the supported architectures) and I just can&#8217;t stop going back to her despite her misgivings(ahem, zfs). Just my type of grrrl!<\/p>\n<p>Let&#8217;s crack on.<\/p>\n<h2>Download the image and install NetBSD on Micro SD card<\/h2>\n<p>On the OS of your choosing do something similar to the following<\/p>\n<p><code>wget http:\/\/cdn.netbsd.org\/pub\/NetBSD\/NetBSD-7.1.2\/evbarm-earmv7hf\/binary\/gzimg\/armv7.img.gz<br \/>\ngunzip armv7.img.gz<br \/>\ndd if=armv7.img of=\/dev\/&lt;yourSDcard&gt;<br \/>\nsync<\/code><\/p>\n<h2>Boot up<\/h2>\n<p>Put the micro SD card in your RPI2 and power on, set a root password.<br \/>\nMake \/etc\/ssh\/sshd_config editable by issuing:<\/p>\n<p><code>armv7# chmod +w \/etc\/ssh\/sshd_config<\/code><\/p>\n<p>Open <code>\/etc\/ssh\/sshd_config<\/code> for editing, uncomment and set<br \/>\n<code>PermitRootLogin yes<\/code><\/p>\n<p>Lets try to create a VLAN interface<\/p>\n<p><code>armv7# ifconfig vlan101 create<br \/>\nifconfig: clone_command: Invalid argument<br \/>\nifconfig: exec_matches: Invalid argument<\/code><\/p>\n<p>Hmmm, I suspect VLANs are not enabled in the kernel, so we need to build a kernel with VLANs enabled<\/p>\n<h2>Not Using RPI to build,\u00a0 too slow<\/h2>\n<p>Spin up a NetBSD VM in Virtualbox<\/p>\n<h2>Download Kernel Source<\/h2>\n<p>Follow instructions from the NetBSD Guide:<br \/>\n<a href=\"https:\/\/www.netbsd.org\/docs\/guide\/en\/chap-fetch.html#chap-fetch-tarball-netbsd-release\">30.3.1. Downloading sources for a NetBSD release<\/a><\/p>\n<p>I Dont think we needed xsrc.tgz but the others seem necessary for build.sh to work<\/p>\n<h2>On with the build prep<\/h2>\n<p><code>build# mkdir \/usr\/obj<\/code><\/p>\n<p>The file \/usr\/src\/sys\/arch\/evbarm\/conf\/RPI2 includes code from RPI so we only need to edit that.<\/p>\n<p><code>build# vi \/usr\/src\/sys\/arch\/evbarm\/conf\/RPI<\/code><\/p>\n<p>Uncomment:<\/p>\n<p><code>#pseudo-device vlan<\/code><\/p>\n<p>Optionally uncomment:<\/p>\n<p><code>#pseudo-device pf<br \/>\n#pseudo-device pflog<\/code><\/p>\n<h2>Lets build<\/h2>\n<p><code>build# cd \/usr\/src<br \/>\nbuild# .\/build.sh -m evbarm -a earmv7hf tools<br \/>\nbuild# .\/build.sh -m evbarm -a earmv7hf kernel=RPI2<\/code><\/p>\n<h2>Upon a successful build you should see something like<\/h2>\n<p><code>===&gt; Kernels built from RPI2:<br \/>\n\/usr\/src\/sys\/arch\/evbarm\/compile\/obj\/RPI2\/netbsd<br \/>\n===&gt; build.sh ended: Sat Apr 28 19:21:38 BST 2018<br \/>\n===&gt; Summary of results:<br \/>\nbuild.sh command: .\/build.sh -m evbarm -a earmv7hf kernel=RPI2<br \/>\nbuild.sh started: Sat Apr 28 19:14:29 BST 2018<br \/>\nNetBSD version: 7.1.2<br \/>\nMACHINE: evbarm<br \/>\nMACHINE_ARCH: earmv7hf<br \/>\nBuild platform: NetBSD 7.1.2 amd64<br \/>\nHOST_SH: \/bin\/sh<br \/>\nMAKECONF file: \/etc\/mk.conf (File not found)<br \/>\nTOOLDIR path: \/usr\/src\/obj\/tooldir.NetBSD-7.1.2-amd64<br \/>\nDESTDIR path: \/usr\/src\/obj\/destdir.evbarm<br \/>\nRELEASEDIR path: \/usr\/src\/obj\/releasedir<br \/>\nUpdated makewrapper: \/usr\/src\/obj\/tooldir.NetBSD-7.1.2-amd64\/bin\/nbmake-evbarm<br \/>\nBuilding kernel without building new tools<br \/>\nBuilding kernel: RPI2<br \/>\nBuild directory: \/usr\/src\/sys\/arch\/evbarm\/compile\/obj\/RPI2<br \/>\nKernels built from RPI2:<br \/>\n\/usr\/src\/sys\/arch\/evbarm\/compile\/obj\/RPI2\/netbsd<br \/>\nbuild.sh ended: Sat Apr 28 19:21:38 BST 2018<br \/>\n===&gt; .<\/code><\/p>\n<h2>Woohooo,we have a kernel<\/h2>\n<p>Now on our RPI2, we just rename the old kernel (kernel7.img) to something else and scp <code>\/usr\/src\/sys\/arch\/evbarm\/compile\/obj\/RPI2\/netbsd.bin<\/code> from our build system to <code>kernel7.img<\/code> in <code>\/boot<\/code> on the RPI2.<br \/>\nReboot<\/p>\n<h2>Test for vlan-ability<\/h2>\n<p>Try to create a VLAN Interface<\/p>\n<p><code>armv7# ifconfig vlan101 create # no errors? looking good!<br \/>\narmv7# ifconfig vlan101 vlan 101 vlanif usmsc0 # no errors? looking even better! lets check ifconfig<br \/>\narmv7# ifconfig vlan101<br \/>\nvlan101: flags=8843&lt;UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST&gt; mtu 1500<br \/>\nvlan: 101 parent: usmsc0<br \/>\naddress: b8:27:eb:43:4e:3f<br \/>\ninet6 fe80::ba27:ebff:fe43:4e3f%vlan101 prefixlen 64 scopeid 0x3<\/code><\/p>\n<p>Lets create another VLAN interface<\/p>\n<p><code>armv7# ifconfig vlan102 create<br \/>\narmv7# ifconfig vlan102 vlan 102 vlanif usmsc0<br \/>\narmv7# ifconfig vlan102<br \/>\nvlan102: flags=8843&lt;UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST&gt; mtu 1500<br \/>\nvlan: 102 parent: usmsc0<br \/>\naddress: b8:27:eb:43:4e:3f<br \/>\ninet6 fe80::ba27:ebff:fe43:4e3f%vlan102 prefixlen 64 scopeid 0x4<\/code><\/p>\n<h2>Lets give the new interfaces IP addresses<\/h2>\n<p><code>armv7# ifconfig vlan101 inet 192.168.101.1\/24 up<br \/>\narmv7# ifconfig vlan102 inet 192.168.102.1\/24 up<br \/>\narmv7# ifconfig vlan101<br \/>\nvlan101: flags=8843&lt;UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST&gt; mtu 1500<br \/>\nvlan: 101 parent: usmsc0<br \/>\naddress: b8:27:eb:43:4e:3f<br \/>\ninet6 fe80::ba27:ebff:fe43:4e3f%vlan101 prefixlen 64 scopeid 0x3<br \/>\ninet 192.168.101.1 netmask 0xffffff00 broadcast 192.168.101.255<br \/>\narmv7# ifconfig vlan102<br \/>\nvlan102: flags=8843&lt;UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST&gt; mtu 1500<br \/>\nvlan: 102 parent: usmsc0<br \/>\naddress: b8:27:eb:43:4e:3f<br \/>\ninet6 fe80::ba27:ebff:fe43:4e3f%vlan102 prefixlen 64 scopeid 0x4<br \/>\ninet 192.168.102.1 netmask 0xffffff00 broadcast 192.168.102.255<\/code><\/p>\n<p>Of course this wont persist a reboot<\/p>\n<p>Create <code>\/etc\/ifconfig.vlan101<\/code> with the contents<\/p>\n<p><code>create<br \/>\nvlan 101 vlanif usmsc0<br \/>\ninet 192.168.101.1\/24 up<\/code><\/p>\n<p>Create <code>\/etc\/ifconfig.vlan102<\/code> with the contents<\/p>\n<p><code>create<br \/>\nvlan 102 vlanif usmsc0<br \/>\ninet 192.168.102.1\/24 up<\/code><\/p>\n<h2>Set up packet forwarding<\/h2>\n<p>Check whether packet forwarding is enabled by issuing:<\/p>\n<p><code>armv7# sysctl net.inet.ip.forwarding<br \/>\nnet.inet.ip.forwarding = 0<\/code><\/p>\n<p>Hmmm, it isn&#8217;t<\/p>\n<p><code>armv7# sysctl -w net.inet.ip.forwarding=1<br \/>\nnet.inet.ip.forwarding: 0 -&gt; 1<\/code><\/p>\n<p>Now it is but that&#8217;s fine for testing, lets make it permanent by adding the line:<\/p>\n<p><code>net.inet.ip.forwarding=1<\/code><\/p>\n<p>to <code>\/etc\/sysctl.conf<\/code><\/p>\n<p>Reboot and you should have vlan101, vlan 102 created with IP addresses and packet forwarding enabled. This is probably a good time to remove the remote access for root.<\/p>\n<p>And thanks to Andy Ruhl on the netbsd-users mailing list for the proof reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few weeks ago I set about upgrading my feeble networking skills by playing around with a Cisco 2970 switch. I set up a couple of VLANs and found the urge to set up a router to route between them. The 2970 isn&#8217;t a modern layer 3 switch so what am I to do? Why [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[6],"class_list":["post-184","post","type-post","status-publish","format-standard","hentry","category-netbsd","tag-networking"],"_links":{"self":[{"href":"https:\/\/www.fukr.org.uk\/index.php?rest_route=\/wp\/v2\/posts\/184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fukr.org.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fukr.org.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fukr.org.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fukr.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=184"}],"version-history":[{"count":31,"href":"https:\/\/www.fukr.org.uk\/index.php?rest_route=\/wp\/v2\/posts\/184\/revisions"}],"predecessor-version":[{"id":215,"href":"https:\/\/www.fukr.org.uk\/index.php?rest_route=\/wp\/v2\/posts\/184\/revisions\/215"}],"wp:attachment":[{"href":"https:\/\/www.fukr.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fukr.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fukr.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}