XBee frames are smaller than typical Ethernet or TCP frames. XBee frames, in fact, are typically limited to about 255 bytes on the SX series; other devices may have different limits. Therefore, xbnet supports fragmentation and reassembly. It will split a frame to be transmitted into the size supported by XBee, and reassemble on the other end.
XBee, of course, cannot guarantee that all frames will be received, and therefore xbnet can't make that guarantee either. However, the protocols you may run atop it -- from UUCP to ZModem to TCP/IP -- should handle this.
When running in **xbnet tap** mode, it is simulating an Ethernet interface. Every Ethernet packet has a source and destination MAC address. xbnet will maintain a cache of the Ethernet MAC addresses it has seen and what XBee MAC address they came from. Therefore, when it sees a request to transmit to a certain Ethernet MAC, it will reuse what it knows from its cache and direct the packet to the appropriate XBee destination. Ethernet broadcasts are converted into XBee broadcasts.
The **xbnet tun** mode operates in a similar fashion; it keeps a cache of seen IP addresses and their corresponding XBee MAC addresses, and directs packets appropriately.
This program requires API mode from the board. It will perform that initialization automatically. Additional configurations may be added by you using the **--initfile** option.
This is the marquee feature of xbnet. It provides a full TCP/IP stack across the XBee links, supporting both IPv4 and IPv6. You can do anything you wish with the participating nodes in your mesh: ping, ssh, route the Internet across them, etc. Up to you! A Raspberry Pi with wifi and xbnet could provide an Internet gateway for an entire XBee mesh, if you so desire.
This works by creating a virtual network device in Linux, called a "tun" device. Traffic going out that device will be routed onto XBee, and traffic coming in will be routed to the computer.
To make this work, you will first bring up the interface with xbnet. Then, give it an IP address with ifconfig or ipaddr. Do the same on the remote end, and boom, you can ping!
Note that for this mode, xbnet must be run as root (or granted `CAP_NET_ADMIN`).
Here's an example. Start on machine A:
```
sudo xbnet /dev/ttyUSB3 tun
```
Wait until it tells you what interface it created. By default, this will be **xbnet0**. Now run:
```
sudo ip addr add 192.168.3.3/24 dev xbnet0
sudo ip link set dev xbnet0 up
```
If you don't have the **ip** program, you can use the older-style **ifconfig** instead. This one command does the same as the two newer-style ones above:
Now, on machine B, start xbnet the same as on machine A. Give it a different IP
```
sudo ip addr add 192.168.3.4/24 dev xbnet0
sudo ip link set dev xbnet0 up
```
Now you can ping from A to B:
```
ping 192.168.3.4
PING 192.168.3.4 (192.168.3.4) 56(84) bytes of data.
64 bytes from 192.168.3.4: icmp_seq=1 ttl=64 time=130 ms
64 bytes from 192.168.3.4: icmp_seq=2 ttl=64 time=89.1 ms
64 bytes from 192.168.3.4: icmp_seq=3 ttl=64 time=81.6 ms
```
For more details, see the tun command below.
## ETHERNET MODE WITH TAP
The tap mode is similar to the tun mode, except it simulates a full Ethernet connection. You might want this if you need to run a non-IP protocol, or if you want to do something like bridge two Ethernet segments. The configuration is very similar.
Be aware that a lot of programs generate broadcasts across an Ethernet interface, and bridging will do even more. It would be easy to overwhelm your XBee network with this kind of cruft, so the tun mode is recommended unless you have a specific need for tap.
XBee systems have a "transparent mode" in which you can configure a particular destination and use them as a raw serial port. You should definitely consider if this meets your needs for serial-based protocols; it would eliminate xbnet from the path entirely.
However, you may still wish to use xbnet; perhaps for its debugging. Also there are some scenarios (such at TCP/IP with multiple destinations) that really cannot be done in transparent mode -- and that is what xbnet is for, and where it shines.
ZModem makes a good fit for the higher bitrate XBee modules. For the slower settings, consider YModem; its 128-byte block size may be more suitable for very slow links than ZModem's 1K.
PPP is the fastest way to run TCP/IP over XBee with **xbnet** if you only need to have two nodes talk to each other. PPP can work in transparent mode without xbnet as well. It
By default, the XBee system requests an acknowledgment from the remote node. The XBee firmware will automatically attempt retransmits if they don't get an ACK in the expected timeframe. Although higher-level protocols also will do ACK and retransmit, they don't have the XBee level of knowledge of the link layer timing and so XBee may be able to detect and correct for a missing packet much quicker.
However, sometimes all these ACKs can cause significant degredation in performance. Whether or not they do for you will depend on your network topology and usage patterns; you probably should just try it both ways. Use **disable-xbee-acks** to disable the XBee level ACKs on messages sent from a given node and see what it does.
## PROTOCOL SELECTION
If all you really need is point-to-point, then consider using PPP rather than tun. PPP supports header compression which may reduce the TCP/IP overhead significantly.
Bear in mind the underlying packet size. For low-overhead protocols, you might want to use a packet size less than the XBee packet size. For high-overhead protocols such as TCP, you may find that using large packet sizes and letting **xbnet** do fragmentation gives much better performance on clean links, especially at the lower XBee bitrates.
## SERIAL COMMUNICATION SPEED
By defualt, XBee modules communicate at 9600bps. You should change this and write the updated setting to the module, and give it to xbnet with **--serial-speed**.
# TROUBLESHOOTING
## BROADCAST ISSUES
# SECURITY
xbnet is a low-level tool and should not be considered secure on its own. The **xbnet pipe** command, for instance, will display information from any node on your mesh. Here are some tips:
Of course, begin by securing things at the XBee layer. Enable encryption and passwords for remote AT commands in XBee.
If you are running a network protocol across XBee, enable firewalls at every node on the network. Remember, joining a node to a networked mesh is like giving it a port on your switch! Consider how nodes can talk to each other.
Use encryption and authentication at the application layer as well. ssh or gpg would be a fantastic choice here.
For nodes that are using xbnet to access the Internet, consider not giving them direct Internet access, but rather requiring them to access via something like OpenVPN or SSH forwarding.
: Disable the XBee protocol-level acknowledgments of transmitted packets. This may improve, or hurt, performance; see the conversation under the PERFORMANCE TUNING section.
: A file listing commands to send to the radio to initialize it. Each command must yield an `OK` result from the radio. After running these commands, **xbnet** will issue additional commands to ensure the radio is in the operating mode required by **xbnet**. Enable **--debug** to see all initialization activity.
: The XBee firmware can return back a report about the success or failure of a transmission. **xbnet** has no use for these reports, though they are displayed for you if **--debug** is given. By default, **xbnet** suppresses the generation of these reports. If you give this option and **--debug**, then you can see them.
**--serial-speed** *SPEED*
: Communicate with the XBee module at the given serial speed, given in bits per second (baud rate). If not given, defaults to 9600, which is the Digi default for the XBee modules. You can change this default with XBee commands and save the new default persistently to the board. It is strongly recommended that you do so, because many XBee modules can communicate much faster than 9600bps.
The **pipe** subcommand permits piping data between radios. It requires a **--dest** parameter, which gives the hex MAC address of the recipient of data sent to xbnet's stdin. pipe is described extensively above.
Note that **--dest** will not restrict the devices that xbnet will receive data from.
These commands run a network stack across XBee and are described extensively above. They have several optional parameters:
**--broadcast-everything** (tun and tap)
: Normally, **xbnet** will use unicast (directed) transmissions to remotes where it knows their XBee MAC address. This is more efficient on the XBee network. However, in some cases you may simply want it to use broadcast packets for all transmissions, and this accomplishes that.
: Normally, **xbnet** will drop Ethernet frames destined for MAC addresses that it hasn't seen. (Broadcast packets still go out.) This is suitable for most situations. However, you can also have it broadcast all packets do unknown MAC addresses. This can be useful in some obscure situations such as multicast.
**--disable-ipv4** and **disable-ipv6** (tun only)
: Disable all relaying of either IPv4 or IPv6 packets. This is not valid in tap mode because tap doesn't operate at this protocol level. It is recommended you disable protocols you don't use.
**--iface-name** *NAME* (tun and tap)
: Request a specific name for the tun or tap interface. By default, this requests **xbnet%d**. The kernel replaces **%d** with an integer starting at 0, finding an unused interface. It can be useful to specify an explicit interface here for use in scripts.
**--max-ip-cache** *SECONDS* (tun only)
: Specifies how long it caches the XBee MAC address for a given IP address. After this many seconds without receiving a packet from the given IP address, **xbnet** will send the next packet to the IP as a broadcast and then cache the result. The only reason to expire IPs from the cache is if you re-provision them on other devices. The tap mode doesn't have a timed cache, since the OS will re-ARP (generating a broadcast anyhow) if it fails to communicate with a given IP.