Raspberry Pi ad-hoc networking - linux

I want to try some networking projects with Raspberry Pis, and I need to just send packets between a pair of pis. I would be happy as a first step just being able to ping between to Raspberry Pis in ad-hoc mode. I have not successfully done this despite looking at several tutorials and examples online.
I have 2x Raspberry Pis with the Debian Wheezy OS installed. I am using the following USB adapter which I installed firmware for on both pis and tested that they work by connected them in managed mode to a router:
Bus 001 Device 004: ID 050d:945a Belkin Components F7D1101 v1 Basic Wireless Adapter [Realtek RTL8188SU]
Here are is some printouts about the networking information:
/etc/network/interfaces at each pi:
pi1#raspberrypi ~ $ cat /etc/network/interfaces
auto lo
iface lo inet loopback
iface eth0 inet dhcp
auto wlan0
iface wlan0 inet manual
address 192.168.2.1
netmask 255.255.255.0
wireless-channel 4
wireless-essid pi-ad-hoc
wireless-mode ad-hoc
pi2#raspberrypi ~ $ cat /etc/network/interfaces
auto lo
iface lo inet loopback
iface eth0 inet dhcp
auto wlan0
iface wlan0 inet manual
address 192.168.2.2
netmask 255.255.255.0
wireless-channel 4
wireless-essid pi-ad-hoc
wireless-mode ad-hoc
ifconfig at each pi:
pi1#raspberrypi ~ $ ifconfig wlan0
wlan0 Link encap:Ethernet HWaddr ec:1a:59:46:8e:5a
inet addr:192.168.2.1 Bcast:192.168.2.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:26 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
pi2#raspberrypi ~ $ ifconfig wlan0
wlan0 Link encap:Ethernet HWaddr ec:1a:59:46:59:0a
inet addr:192.168.2.2 Bcast:192.168.2.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:6 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
iwconfig at each pi:
pi1#raspberrypi ~ $ iwconfig wlan0
wlan0 IEEE 802.11bg ESSID:"pi-ad-hoc" Nickname:"rtl_wifi"
Mode:Ad-Hoc Cell: 02:11:87:FA:4A:02 Bit Rate:54 Mb/s
Sensitivity:0/0
Retry:off RTS thr:off Fragment thr:off
Power Management:off
Link Quality:0 Signal level:0 Noise level:0
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0
pi2#raspberrypi ~ $ iwconfig wlan0
wlan0 IEEE 802.11bg ESSID:"pi-ad-hoc" Nickname:"rtl_wifi"
Mode:Ad-Hoc Cell: 02:11:87:C4:F2:01 Bit Rate:54 Mb/s
Sensitivity:0/0
Retry:off RTS thr:off Fragment thr:off
Power Management:off
Link Quality:0 Signal level:0 Noise level:0
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0
route at one pi (identical on other pi):
pi1#raspberrypi ~ $ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
192.168.2.0 * 255.255.255.0 U 0 0 0 wlan0
iwlist scan at each pi:
pi1#raspberrypi ~ $ sudo iwlist wlan0 scan
wlan0 Scan completed :
Cell 01 - Address: 02:11:87:FA:4A:02
ESSID:"pi-ad-hoc"
Protocol:IEEE 802.11bg
Mode:Ad-Hoc
Frequency:2.427 GHz (Channel 4)
Encryption key:off
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
48 Mb/s; 54 Mb/s
Quality:0 Signal level:0 Noise level:0
Cell 04 - Address: 02:11:87:C4:F2:01
ESSID:"pi-ad-hoc"
Protocol:IEEE 802.11bg
Mode:Ad-Hoc
Frequency:2.427 GHz (Channel 4)
Encryption key:off
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
48 Mb/s; 54 Mb/s
Signal level=100/100
pi2#raspberrypi ~ $ sudo iwlist wlan0 scan
wlan0 Scan completed :
Cell 01 - Address: 02:11:87:C4:F2:01
ESSID:"pi-ad-hoc"
Protocol:IEEE 802.11bg
Mode:Ad-Hoc
Frequency:2.427 GHz (Channel 4)
Encryption key:off
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
48 Mb/s; 54 Mb/s
Quality:0 Signal level:0 Noise level:0
Cell 02 - Address: 02:11:87:FA:4A:02
ESSID:"pi-ad-hoc"
Protocol:IEEE 802.11bg
Mode:Ad-Hoc
Frequency:2.427 GHz (Channel 4)
Encryption key:off
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
48 Mb/s; 54 Mb/s
Signal level=100/100
Ping does not work, and no networking seems to work between them. From iwconfig, you can see that they each have a different "Cell" address (not the same Cell as in the iwlist scan) which is the pseudo-base station ID that is used to define an ad-hoc network (my best understanding from what I've read). Also, from the iwlist, each pi can see their own plus the other pi's ad-hoc network. I assume they need to select the same Cell id to communicate, and I'm unsure how to get them to do this automatically. I tried statically forcing these to be the same with the following command at each pi which did not change the cell id and therefore did not work:
sudo iwconfig wlan0 ap (some address)
I also tried a solution which uses ap_scan=2 in the wpa_supplicant config which did not seem to help.
Anyone have any idea what I've done wrong?
Thanks,
Andy.

After some searching I found that the Belkin USB adapter I was using apparently didn't have ad-hoc mode support with the linux drivers. I bought some other wireless USB adapaters that worked great "Edimax EW-7811Un 150 Mbps Wireless 11n Nano Size USB Adapter". They are cheaper, smaller, and they worked in ad-hoc mode without even needing to worry drivers. The details I put for troubleshooting can be used as a guide if you are also wanting to do ad-hoc raspberry PI projects.

This works for me in /etc/network/interfaces:
auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
wireless-essid "MYPINET"
wireless-channel 3
wireless-mode ad-hoc
wireless-ap 11:5F:02:38:5C:45
address 192.168.10.1
netmask 255.255.255.0
The essid, channel and ap can be any valid value (same on all your Pi's). Make sure to assign different addresses on the same subnet to your different Pi's and you should be fine. The ap defines the cell ID that was mentioned above. FWIW I am using a TP-link WN725N. It has the RTL8188CUS chipset and works fine out of the box even though dmesg indicates the Pi is treating it as an RTL8192.

I was unsuccessful with any adapter using the RTL8188CUS chipset. Luckily, I had a number of Ralink RT5370 dongles (from this kit) that support the nl80211 interface and ad-hoc mode.
My solution involves using wpa_supplicant and is configured with 2 files. Ensure that the nl80211 driver is installed:
sudo apt-get install libnl1
Next, create the following wpa_supplicant configuration file called /etc/wpa_supplicant-adhoc.conf on each Pi:
ctrl_interface=DIR=/run/wpa_supplicant GROUP=netdev
update_config=1
ap_scan=2
network={
ssid="pihoc_wpa"
mode=1
frequency=2462
proto=WPA
key_mgmt=WPA-NONE
pairwise=NONE
group=TKIP
psk="password"
}
where you can choose the ssid, frequency (look here for valid values), and psk. Make sure that you are part of the user group net-dev using the command
getent group netdev
and if not, you can add yourself using
sudo usermod -a -G netdev userName
Next, add the following block to the /etc/network/interfaces file on each Pi:
auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 10.10.2.1
netmask 255.255.255.0
pre-up wpa_supplicant -B -D nl80211 -i wlan0 -c /etc/wpa_supplicant-adhoc.conf
where each Pi has a different address field beginning with 10.10.2.. Also, if your RT5370 adapter is using an interface other than wlan0 (e.g. wlan1, wlan2, etc.), be sure to use that interface name instead.
At this point, the Pis should automatically join the network upon being rebooted. Test the connection by pinging or using ssh, for example run the following from the agent with IP address 10.10.2.1:
ssh 10.10.2.2
to access the agent with IP address 10.10.2.2.
The steps listed here are adapted from this Arch Linux wiki article and this Raspberry Pi forum discussion.

Related

Set eth0 as DHCP in raspberry pi

I want to set eth0 of RPI (Raspbian Stretch) to be DHCP, my goal is that when I will connect any device that communicates using TCP/IP protocol, that device will receive an IP address.
I have found many guides that all lead to making eth0 ip address static, which is not my intention.
Currently a device is connected through eth0 , ifconfig says it has some IP but pinging the hostname of the device gets no reply. wlan0 is connected via wifi.
Here is some info :
pi#raspberrypi:~ $ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 169.254.31.197 netmask 255.255.0.0 broadcast 169.254.255.255
inet6 fe80::ad5a:8219:4c27:b59b prefixlen 64 scopeid 0x20<link>
ether b8:27:eb:f3:f2:87 txqueuelen 1000 (Ethernet)
RX packets 81 bytes 26568 (25.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 46 bytes 10544 (10.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 31 bytes 3472 (3.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 31 bytes 3472 (3.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.71 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fd01::60ff:8818:5965:dc58 prefixlen 64 scopeid 0x0<global>
inet6 fe80::473d:110e:5474:8000 prefixlen 64 scopeid 0x20<link>
ether b8:27:eb:a6:a7:d2 txqueuelen 1000 (Ethernet)
RX packets 955 bytes 74427 (72.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 184 bytes 22096 (21.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
pi#raspberrypi:~ $ cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
Any advice to how to achieve my goal will be happily received.
Have you tried this: https://wiki.debian.org/NetworkConfiguration#Using_DHCP_to_automatically_configure_the_interface
i.e. just add this to /etc/network/interfaces:
auto eth0
allow-hotplug eth0
iface eth0 inet dhcp
if you're really pedantic you could create a file like /etc/network/interfaces.d/eth0-dhcp and paste these lines into it - the end result will be the same.
If you simply want to connect two devices (without routing/forwarding/etc), i.e. for a standalone test network, this should work:
Figure out what IP range you want to use, and what IP you want your
DHCP server to have (let's call it IP_DHCP)
Install isc-dhcp server
Set static IP address for eth0 (e.g. set it to the IP_DHCP you
chose earlier)
Configure /etc/dhcp/dhcpd.conf for the desired network range (man
dhcpd has a decent reference), make it authoritative (unless there
are other DHCP servers).
Run service isc-dhcpd-server start
Example of a configuration:
sudo apt-get install isc-dhcp-server
sudo nano /etc/conf/dhcpd.conf
Uncomment #authoritative to make it authoritative
Add along the following lines(customize based on what you decided in step 1), where routers is IP_DHCP:
subnet 10.0.0.0 netmask 255.255.255.0 {
range 10.0.0.1 10.0.0.100;
option subnet-mask 255.255.255.0;
option broadcast-address 10.0.0.255;
option routers 10.0.0.1;
}
Save buffer and close
sudo service isc-dhcpd-server start
Good luck!
P.S. if you are getting connect:network not found issues, then you likely have an issue in your routing tables (route) setup.
Something along the lines of
sudo route add default gw 10.0.0.1
where the IP address is your eth0 will likely allow communication.
NetWork Manager (nmcli) might be running on the device and has been configured to work with a static IP.
The following commands can help you check if your device is managed by nmcli:
nmcli d
nmcli con show
Run the following command and replace 'Wired connection 1' with the name of your connection.
nmcli con show 'Wired connection 1' | grep 'ipv4.method'
ipv4.method will show "auto" if set to DHCP or "manual" if set to static.
http://www.intellamech.com/RaspberryPi-projects/rpi_nmcli.html

No DHCP response when connecting to WEP wifi

I am unable to connect to WEP wifi on Linux (Mint): I get no DHCP offer.
My bash script (that I launch as root) does the following:
pkill dhclient
pkill wpa_supplicant #just in case
ip link set dev wlan0 down
ip addr flush dev wlan0
iwconfig wlan0 essid "MyWifi"
iwconfig wlan0 enc off
ip link set dev wlan0 up
dhclient -v wlan0
When I launch it, I get no DHCP offer:
Internet Systems Consortium DHCP Client 4.2.4
Copyright 2004-2012 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/wlan0/e8:4e:06:24:b2:9e
Sending on LPF/wlan0/e8:4e:06:24:b2:9e
Sending on Socket/fallback
DHCPREQUEST of 192.168.100.101 on wlan0 to 255.255.255.255 port 67 (xid=0x19efd6f2)
DHCPREQUEST of 192.168.100.101 on wlan0 to 255.255.255.255 port 67 (xid=0x19efd6f2)
DHCPREQUEST of 192.168.100.101 on wlan0 to 255.255.255.255 port 67 (xid=0x19efd6f2)
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 3 (xid=0x17d23b2f)
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 3 (xid=0x17d23b2f)
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 5 (xid=0x17d23b2f)
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 11 (xid=0x17d23b2f)
...
Important:
I do not have NetworkManager (not running and removed - I saw that was a common issue) and I prefer to use command line.
When I scan for my wifi, I see it no problem:
sudo iwlist wlan0 scan
wlan0 Scan completed :
Cell 01 - Address: 20:C9
[..]
Cell 02 - Address: 02:26:C6:2F:46:7E
Channel:9
Frequency:2.452 GHz (Channel 9)
Quality=54/70 Signal level=-56 dBm
Encryption key:off
ESSID:"MyWifi"
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 48 Mb/s
54 Mb/s; 6 Mb/s; 9 Mb/s
Bit Rates:12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
Mode:Ad-Hoc
Extra:tsf=0000000001ed4e21
Extra: Last beacon: 32ms ago
IE: Unknown: 000B6950686F6E65204D795769
IE: Unknown: 010882848B96606C0C12
IE: Unknown: 030109
IE: Unknown: 06020000
IE: Unknown: 2A0100
IE: Unknown: 320418243048
I know that wifi works as I have other equipments connect to it
I know my wifi dongle works too as I manage to connect to other wifi (WPA).
Ok I found the solution. I was using mode 'managed' by default. But in the iwlist scan, it says 'ad-hoc':
Mode:Ad-Hoc
So, the correct script to connect to my Wifi is:
#!/bin/bash
pkill dhclient
pkill wpa_supplicant #just in case
ip link set dev wlan0 down
ip addr flush dev wlan0
iwconfig wlan0 essid "MyWifi"
iwconfig wlan0 enc off
iwconfig wlan0 key off
iwconfig wlan0 mode Ad-Hoc
ip link set dev wlan0 up
dhclient -v wlan0
and that works :)

zolertia z1 connection with tunslip for webserver

I want to install a webserver on my zolertia z1 sensor. I followed step here : http://wismote.org/doku.php?id=development:sample_code
When i run tunslip program like this :
"sudo ./tunslip -B 115200 -s /dev/ttyUSB0 192.168.1.1 255.255.255.0 "
Results are :
slip started on ``/dev/ttyUSB0''
opened device ``/dev/tun0''
ifconfig tun0 inet `hostname` up
route add -net 192.168.1.0 netmask 255.255.255.0 dev tun0
ifconfig tun0
tun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:127.0.1.1 P-t-P:127.0.1.1 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:500
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
The route on tun0 is opened but he doesn't detect my sensor connected with serial line. There no "route add -net 192.168.1.2 netmask 255.255.255.255 dev tun0" at end and i don't know why. I don't know if i must change the flag for a TAP or TUN device ?!
if i try to login on my sensor with "make login". it works fine. the program is
correctly installed on this.
I tried this on a virtual image with contiki and on Ubuntu 12.04.4 LTS x86_64. I have the same result on both OS.
Maybe you've to change your BaudRate which it's used to be arround 38.400 bauds on motes z1 Zolertia.
sudo ./tunslip -B 38400 -s /dev/ttyUSB0 192.168.1.1 255.255.255.0

why i can not disable multicast request

I use the following command to disable the eth0 interface's multicast mode , but i not works :
sudo ifconfig eth0 -multicast
when do this, the eth0's configure is so:
ifconfig -v eth0
eth0 Link encap:Ethernet HWaddr 00:16:3E:E8:43:01
inet addr:10.232.67.1 Bcast:10.232.67.255 Mask:255.255.255.0
UP BROADCAST RUNNING MTU:1500 Metric:1
RX packets:46728751 errors:0 dropped:0 overruns:0 frame:0
TX packets:15981372 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:8005709841 (7.4 GiB) TX bytes:3372203819 (3.1 GiB)
then, i do icmp echo_request in host 10.232.67.2:
ping 224.0.0.1
and tcpdump the package on host 10.232.67.1:
tcpdump -i eth0 host 224.0.0.1 -n
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
21:11:03.182813 IP 10.232.67.2 > 224.0.0.1: ICMP echo request, id 3639, seq 324, length 64
21:11:04.184667 IP 10.232.67.2 > 224.0.0.1: ICMP echo request, id 3639, seq 325, length 64
21:11:05.186781 IP 10.232.67.2 > 224.0.0.1: ICMP echo request, id 3639, seq 326, length 64
so, how to disable the multicast mode ?
by the way , when i disable the broadcast:
sudo ifconfig eth0 -broadcast
error message is :
Warning: Interface eth0 still in BROADCAST mode.
so, why can't stop broad cast mode ?
To disable multicast on an interface (you had it right):
ifconfig eth0 -multicast
tcpdump without the -p flag puts the interface in PROMISCUOUS mode, so it captures any traffic on the wire. If you're on a hub, you can see traffic sent to/from everyone else. When an interface isn't in promiscuous mode, it ignores all traffic not sent to it. If you try the tcpdump and ping without promiscuous mode, and multicast disabled, you shouldn't see the traffic:
tcpdump -p -i eth0 host 224.0.0.1 -n
You don't want to disable BROADCAST mode. It just designates the broadcast address for your subnet.

Linux/RHEL5: UDP on IPv6 does not work on the same pc

I try to setup a netcat server/client with UDP and IPv6 on the same pc.
Here are my interfaces on my pc:
[root#rh55hp360g7ss7 trunk_dir]# ifconfig
eth0 Link encap:Ethernet HWaddr xxx
inet addr:192.168.255.166 Bcast:192.168.255.255 Mask:255.255.255.0
inet6 addr: fe80::1ec1:deff:fef3:4870/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:21948499 errors:0 dropped:0 overruns:0 frame:0
TX packets:24300265 errors:0 dropped:0 overruns:0 carrier:0
collisions:360733 txqueuelen:1000
RX bytes:3645218404 (3.3 GiB) TX bytes:1672728274 (1.5 GiB)
Interrupt:162 Memory:f4000000-f4012800
Then I start the netcat server:
nc -6ul fe80::1ec1:deff:fef3:4870%eth0 5678
And the netcat client (still on the same pc)
nc -6u fe80::1ec1:deff:fef3:4870%eth0 5678
But then, when I type something into the NetCat Client, nothing is transfered to the server.
The same example is working if
I start the netcat client on another pc
I'm using TCP instead of UDP (i.e. when I remove the -u option)
When I'm using IPv4 instead of IPv6 (i.e. when I remove the -6 option and if I take the IPv4 address).
Any Ideas?
TSohr.
Here is the routing table, in case it might help:
[root#rh55hp360g7ss7 trunk_dir]# route -A inet6
Kernel IPv6 routing table
Destination Next Hop Flags Metric Ref Use Iface
fe80::/64 * U 256 0 0 eth0
::1/128 * U 0 265 5 lo
fe80::1ec1:deff:fef3:4870/128 * U 0 10551 1 lo
ff00::/8 * U 256 0 0 eth0
[root#rh55hp360g7ss7 trunk_dir]#
## Added 2012-03-13
With ::1, it is working.
I have the same problem when trying to run a SIP stack on the pc.
It's only a problem with Red Hat and with link-local scope. When using an address with global scope, its working.
I tried out with Ubuntu 10.4, here it is working also with link-local adddresses.
This is my Red Hat Distribution:
[root#BETESIP02 sipp]# uname -a
Linux BETESIP02 2.6.18-194.el5PAE #1 SMP Tue Mar 16 22:00:21 EDT 2010 i686 i686 i386 GNU/Linux

Resources