Enable Debugging on Raspberry Pi with Buildroot


This is a quick post that shows how to enable gdb and/or gdbserver on the Raspberry Pi when using Buildroot.

Enable option Build packages with debugging symbols, under Build Options

debugging symbols

Enable the following options under Toolchain

  • Build cross gdb for the host – to facilitate remote debugging
  • Thread library debugging – to enable adding gdb and gdbserver packages to the device’s root file system

cross gdb on host

Thread library debugging

Select gdb and gdbserver (useful for remote debugging), under Target packages, “Debugging, profiling and benchmark”

gdb and gdbserver

Wireless Router with Buildroot and Raspberry Pi


This post shows how to build a basic wireless router, using Raspberry Pi. It is driven by a custom Linux system built with Buildroot 2014.08, that boots up in no time at all.

I refer you to the following posts

Add all appropriate Kernel modules and Buildroot packages mentioned in these posts before proceeding.

Kernel Configuration

I’m using a different USB Wi-Fi adapter here. It has the RTL8188CUS chipset that supports access point mode. The Kernel driver is Realtek 8192C USB WiFi, available under Device Drivers, Network device support, Wireless LAN

Realtek 8192C USB WiFi

Package Configuration

Invoke make menuconfig within the buildroot folder from a command prompt. Under Target Packages, select option Show packages that are also provided by busybox

BR2_PACKAGE_BUSYBOX_SHOW_OTHERS

Under Target packages, Networking application, select dhcp and its sub-packages

BR2_PACKAGE_DHCP

If you’re using a different wireless adapter, select hostapd and its sub-packages

BR2_PACKAGE_HOSTAPD

Perform build by invoking make, copy the newly minted system to an SD card, and use it to boot up your Raspberry Pi.

Custom hostapd

The hostapd built by Buildroot does not work with the chosen Wi-Fi adapter. We need to build the hostapd module provided by Realtek for RTL8188CUS.

Download the driver package from Realtek. It contains the GPL source code for hostapd. In my case, I expanded RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911.zip, then expanded wpa_supplicant_hostapd-0.8_rtw_r7475.20130812.tar.gz under folder wpa_supplicant_hostapd.

Proceed to folder hostapd from the command line, and use the toolchain built by Buildroot to build hostapd

export PATH=/home/parallels/buildroot-2014.08/output/host/usr/bin:$PATH
make CC=arm-buildroot-linux-uclibcgnueabi-gcc

Copy hostapd binary to SD card.

Router Setup

Bring up wireless interface with a static IP address

ifconfig wlan0 up 192.168.2.1 netmask 255.255.255.0

Or, when using iproute2

ip addr add 192.168.2.1/24 dev wlan0
ip link set wlan0 up

Configure and bring up dhcpd. Edit /etc/dhcp/dhcpd.conf. Edit following lines so that they are commented (as shown)

#option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;

Edit following line so that it is uncommented (as shown)

authoritative;

Add following lines at the end

subnet 192.168.2.0 netmask 255.255.255.0 {
  range 192.168.2.10 192.168.2.50;
  option broadcast-address 192.168.2.255;
  option routers 192.168.2.1;
  default-lease-time 600;
  max-lease-time 7200;
  option domain-name "local";
  option domain-name-servers 8.8.8.8, 8.8.4.4;
}

Create leases file

touch /var/lib/dhcp/dhcpd.leases

Instantiate dhcpd

dhcpd -cf /etc/dhcp/dhcpd.conf

Configure and bring up hostapd. Create file /etc/hostapd/hostapd.conf with

interface=wlan0
driver=rtl871xdrv
ssid=pi
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=raspberry
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Run hostapd

hostapd -B /etc/hostapd/hostapd.conf

Try driver=nl80211 if you’re using a new netlink interface compatible driver. If you get the following error when starting hostapd, you probably need a different driver

Configuration file: /etc/hostapd/hostapd.conf
Line 2: invalid/unknown driver 'nl80211'
1 errors found in configuration file '/etc/hostapd/hostapd.conf'

If you get the following error using the Wi-Fi adapter mentioned earlier, you need to use the Realtek hostapd compiled above, with driver=rtl871xdrv in hostapd.conf

rfkill: Cannot open RFKILL control device
nl80211: Could not configure driver mode
nl80211 driver initialization failed.
hostapd_free_hapd_data: Interface wlan0 wasn't started

Join the pi network from any other device using password raspberry, and you’re good to go. Configure NAT routing, if you want to access internet over the wired ethernet interface like so

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE
iptables -A FORWARD -i usb0 -o wlan0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -i wlan0 -o usb0 -j ACCEPT

Change usb0 to eth0 if that is where your ISP link is at.

Check where your default route is configured for with

netstat -nr

Or

route 

If default route is set for usb0 (or eth0) add route through wlan0 for subnet 192.168.2.0 thus

route add -net 192.168.2.0/24 dev wlan0

Bluetooth on Raspberry Pi with Buildroot


This post shows how to build Bluetooth support with Buildroot for a Raspberry Pi. You’ll need a Bluetooth adapter such as the Bluetooth 4.0 USB Module from Adafruit. That adapter is also Bluetooth Smart (LE) ready.

I recommend using Buildroot 2014.08 because bluez-tools such as bt-adapter and bt-agent work with its version of Bluez.

Kernel Configuration

Enable Bluetooth subsystem support under Networking support

Bluetooth subsystem support

Enable RFCOMM  protocol support under Bluetooth subsystem support, if you want Bluetooth SPP support. Enable BNEP protocol support, if you want support for networking over Bluetooth. Enable HIDP protocol support, if you want support for Bluetooth enabled keyboard and mice.

RFCOMM and BNEP support

Since we’re using a USB adapter, enable HCI USB driver under Bluetooth subsystem support, Bluetooth device drivers

HCI USB driver

Buildroot Packages

The Kernel Bluetooth stack is called BlueZ. BlueZ also provides a set of utilities that can be used from the command line. To add those, select bluez-utils under Target packages, Networking applications

bluez-utils

If using Buildroot 2014.08, select bluez-utils 5.x package instead

bluez5_utils

make the Linux system and copy to SD card.

Useful commands

Look for interfaces

hciconfig -a

Bring up HCI interface

hciconfig hci0 up

Make the device discoverable

hciconfig hci0 piscan

Scan available devices

hcitool scan

Ping a device

l2ping C8:3E:99:C6:1B:F8

Browse capabilities of a device

sdptool browse C8:3E:99:C6:1B:F8

Run bluetooth daemon

bluetoothd

If using Buildroot 2014.08

/usr/libexec/bluetooth/bluetoothd &

Browse local capabilities

sdptool browse local

Another means to discover available devices, using bluez-tools

bt-adapter -d

Connect and pair with a device listed by the command above

bt-device -c 5C:0E:8B:03:6E:4E

Create a network connection and obtain IP address

bt-network -c C8:3E:99:C6:1B:F8 nap
sudo dhclient bnep0

Create a serial port

sdptool add --channel=7 SP
rfcomm connect /dev/rfcomm0 C8:3E:99:C6:1B:F8 7

Use screen with the above serial port

screen /dev/rfcomm0 115200

Bluetooth LE commands

Scan for Bluetooth LE devices

hcitool lescan

Access services and characteristics provided by an LE peripheral

gatttool -b 7F:AE:48:2B:00:0C -t random -I

gatttool prompt supports a set of commands to interact with the peripheral

connect
primary
char-desc
char-read-hnd handle
char-write-req handle data

Enable advertising as an iBeacon

hciconfig hci0 leadv
hciconfig hci0 noscan
hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 00

Bluetooth is big. Go ahead and do something with it.

Wireless on Raspberry Pi with Buildroot


Raspberry Pi does not have Wi-Fi on board. You’ll need to use a Wi-Fi dongle and discover the driver to build into the kernel. One way to find the driver is to plug the dongle into a Raspberry Pi running Raspbian, and perform an lsmod to see which module gets loaded. The following text assumes you’re building a custom embedded Linux system using Buildroot.

Kernel Configuration

Invoke the Kernel configuration utility using make linux-menuconfig.

Enable Wireless Networking support under Networking support

Wireless

Enable cfg80211 – wireless configuration API, and Generic IEEE 802.11 Networking Stack (mac80211), under Networking Support, Wireless

cfg80211 and mac80211

Enable EEPROM 93CX6 support under Device Drivers, Misc devices, EEPROM support. Required for RTL8187 module in my Wi-Fi dongle, may not be needed for your particular adapter

EEPROM 93CX6 support

Enable Wireless LAN driver support under Device Drivers, Network device support

Wireless LAN

Enable Realtek 8187 and 8187B USB support under Device Drivers, Network device support, Wireless LAN. Select the driver appropriate for your adapter

Realtek 8187 and 8187B USB support

Package Configuration

Invoke make menuconfig within the buildroot folder from a command prompt.

Select package iw required to configure wireless networking, under Target packages, Networking applications. Enable iproute2 if you want to use the ip utility instead of ifconfig.

iw

Select package wpa_supplicant and its sub-packages for WPA/WPA2 support

wpa_supplicant

Perform the build by invoking make, copy the newly minted system to an SD card, and use it to boot up your Raspberry Pi.

Configure Wireless Networking

List your wireless interfaces

iw list

If you’ve selected the iproute2 package above, the following should list all network interfaces

ip link

Bring up the wlan0 interface

ip link set wlan0 up

Or

ifconfig wlan0 up

Find the access point you want to connect to

iw dev wlan0 scan

Assuming you’re using WPA/WPA2, invoke wpa_passphrase to create config file, and wpa_supplicant to connect to network

wpa_passphrase your_SSID your_passphrase > your_SSID.conf
wpa_supplicant -B -i wlan0 -c your_SSID.conf

Instead, if you want to connect to an open network

iw dev wlan0 connect your_SSID

Request IP address using DHCP

dhcpcd wlan0

Check link status

iw dev wlan0 link

Add nameserver entries to /etc/resolv.conf e.g.

nameserver 8.8.8.8
nameserver 8.8.4.4

Test internet connectivity using ping

ping www.google.com

Linux NAT routing on Raspberry Pi with Buildroot


This post documents the kernel modules and other packages required to build an embedded Linux NAT router using buildroot, for Raspberry Pi.

Modify Linux kernel configuration

Invoke make linux-menuconfig in buildroot folder to initiate kernel configuration utility. Select modules shown in the figures below.

Networking Options

Network packet filtering framework

Ensure that the following is enabled under Networking Options

  • Network packet filtering framework (Netfilter)

Core Netfilter Configuration

Ensure that the following are enabled under Core Netfilter Configuration

  • Netfilter connection tracking support
  • Netfilter Xtables support (required for ip_tables)
  • “conntrack” connection tracking match support
  • “state” match support

IP: Netfilter Configuration

Ensure following is enabled under Network packet filtering framework (Netfilter)

  • Advanced netfilter configuration

IPv4 packet filtering and NAT

Ensure the following modules are selected under IP: Netfilter Configuration

  • IPv4 connection tracking support (required for NAT)
  • IP tables support (required for filtering/masq/NAT)
  • Packet filtering
  • IPv4 NAT
    • MASQUERADE target support
    • NETMAP target support
    • REDIRECT target support

Enable other targets if you want to do sophisticated filtering.

Modify Buildroot Configuration

Include iptables utility package shown in the figure below, using the configuration utility invoked by executing make menuconfig. Include tcpdump if you want to sniff network data.

Buildroot iptables

Now, just execute make to build the system, and copy the kernel image and root file system to SD card.

Perform NAT routing

I use the following commands to bring up the network interfaces and setup NAT forwarding. Any packets received on interface eth0 are forwarded to usb0. Only packets for connections already established are forwarded back to eth0. usb0 is a USB CDC ethernet interface of the kind seen in modems.

ifconfig usb0 up
ifconfig eth0 up
dhcpcd
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE
iptables -A FORWARD -i usb0 -o eth0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -i eth0 -o usb0 -j ACCEPT

If you get any of the following error messages with iptables, you are probably missing one of the kernel modules mentioned above

iptables v1.4.21: can't initialize iptables table `nat': Table does not exist (do you need to insmod?)
Perhaps iptables or your kernel needs to be upgraded.
iptables v1.4.21: can't initialize iptables table `filter': Table does not exist (do you need to insmod?)
Perhaps iptables or your kernel needs to be upgraded.
iptables: No chain/target/match by that name.

If the NAT router is not the default gateway on downstream hosts and routers, you’ll have to setup static routes to relay packets upstream. This can be done using a command such as the following on Mac OS X

sudo route -n add -net 10.0.0.0/8 192.168.2.1

Or, on Linux

route add -net 10.0.0.0/8 gw 192.168.2.1

Finally, a couple of useful commands to troubleshoot IP routing and forwarding.

List all rules in iptables

iptables -L -v

Flush all rules in iptables

iptables --flush

A basic packet sniffer

tcpdump -A -i usb0

List all routes

netstat -nr

Network Connection Bridging on Raspberry Pi with Buildroot


This post shows how I add network connection bridging to my custom embedded Linux system for Raspberry Pi. This allows me to experiment with bridging network connections for internet sharing, robustness testing, packet capture, and so on.

Modify Linux kernel configuration

Execute the following in the buildroot folder to get Linux Kernel configuration menu

make linux-menuconfig

Select the 802.1d Ethernet Bridging module shown in the following screenshot

Ethernet Bridging

Modify Buildroot configuration

Execute the following in the buildroot folder to get the configuration menu

make menuconfig

Select the bridge-utils package, shown in the screenshot below. This package contains the brctl utility required to configure bridging.

bridge-utils

Then, just execute make to build the system. Once that is done, copy the new kernel image and root file system over to the SD card.

Perform bridging

These are the sequence of commands I typically use to bring up the bridge manually. I use a regular ethernet interface and a USB CDC ethernet interface for testing.

ifconfig eth0 0.0.0.0 promisc up
ifconfig usb0 0.0.0.0 promisc up
brctl addbr br0
brctl addif br0 eth0 usb0
ifconfig br0 up

If you need the bridge interface to have an IP address, you can assign one manually, or by invoking the DHCP client daemon as shown below. This is useful if you need to have access to your Pi over the network.

dhcpcd br0

To wrap it up, here’s how you can tear everything down.

ifconfig eth0 down
ifconfig usb0 down
ifconfig br0 down
brctl delbr br0

Node.js on Raspberry Pi with Buildroot


I successfully built an embedded Linux system for Raspberry Pi using Buildroot recently, and decided it was time to add some useful packages to it. The first that came to mind is Node.js. Having that on the Linux system will allow me to serve an HTML5 UI to any computer with a browser, on the same network.

Invoke

make menuconfig 

in buildroot folder to obtain the configuration UI. Node.js requires IPv6 support so I enabled that in the Toolchain menu thus

buildroot_ipv6

Then I enabled Node.js and npm from the Target packages menu thus

buildroot_nodejs

buildroot_npm

I followed that up by issuing a full build thus

make clean
make

Buildroot stores all downloads in the dl folder, so it will only download whatever isn’t already there. It will build the host cross-compilation toolchain from scratch though, since that is available under output, and make clean removes everything there.

I’ve now got Node.js 0.10.12 working successfully on the Linux system.