Files

361 lines
17 KiB
Markdown

# Forwarder
A forwarder joins two MeshCore networks that run different radio settings, for example two neighbouring
countries on different frequencies. It is one node with two LoRa radios: it listens on both, and a packet heard
on one radio can be sent out on the other, if your rules allow it.
It does the job of [meshcore-forwarder](https://github.com/recrof/meshcore-forwarder) (a Linux host with two
KISS modems and a JavaScript filter) on a single board, with no host computer, and it can be administered over
the mesh like a repeater.
The design decision is [ADR 0015](adr/0015-forwarder-role.md); every command is in the
[CLI reference](CLI_commands.md#forwarder-zephcore-only-config_zephcore_role_forwarder).
## Forwarder, repeater and the alternatives
A forwarder **forwards, it never repeats**. A packet that arrives on radio a may go out on radio b, never back
out on radio a. So it is not a repeater for either network.
| | Repeater | Forwarder |
|---|---|---|
| Radios | One | Two, each with its own frequency, bandwidth, SF, CR and TX power |
| Retransmits on the radio it heard the packet on | Yes, that is its job | Never |
| Carries packets between the two networks | No | Yes, by the `fwd` rules |
| What carries by default | Everything its checks allow | Nothing (`fwd default deny`) until you add rules |
| Seen on the mesh as | A repeater | A repeater on each side, with a name per side |
| Hops a packet gains | One | One per crossing |
| Configured over | USB CLI, remote admin | The same, with `.a` / `.b` / `.peer` on per-radio settings |
Two consequences follow from "never repeats":
- **Replacing a repeater with a forwarder removes that repeater.** Nodes that could only reach the network
through it are cut off. Put the forwarder where both networks already have repeaters, or keep a repeater
next to it.
- **A forwarder does not help coverage inside either network.** It only joins them.
Compared with the alternatives:
- **meshcore-forwarder (Linux + two KISS modems).** It forwards raw packets and adds no hop, so return paths cannot
point at it and every direct packet must be carried blindly. The ZephCore forwarder appends its own hash like a
repeater does, so a crossing is one hop, replies route back through it, and it carries only the direct packets
that are addressed through it. Its rule set is a close match for meshcore-forwarder's `filter()`
([below](#emulating-meshcore-forwarder-czsk)).
- **MeshCore's "repeater bridge".** That repeats on both sides. A forwarder deliberately does not.
## How it works
### One node, two sides
The two radios are the two **sides**: side **a** is the `lora0` radio, side **b** the `lora1` radio. The names are
fixed to the hardware; renaming a side does not swap them.
Shared by both sides (one node):
- the identity (public key), so both sides are the same node to the mesh;
- the table of packets already seen, so a packet that comes back from the far side is not carried again;
- logins (ACL), the admin and guest passwords and the wrong-password limit (4 per 180 s for the node, not per radio), the limit on packets that fail their check code, regions and transport keys, the flood blacklist (`filter`),
the twin guard (`twin.guard`, damaged copies of forwarded floods), mesh time sync and the neighbour list (each neighbour is tagged with the side it was heard on).
Separate per side:
- name, radio settings (`radio`, `freq`), TX power, duty cycle, the TX/RX delay settings, CAD and
listen-before-talk, RX gain and RX duty cycle, advert intervals and path hash mode;
- the send queue, the contention estimate and the radio and packet statistics.
Each side sends its own adverts on its own radio, under its own name, as a repeater. Adverts are not a special
node type, so stock MeshCore nodes see an ordinary repeater.
### What happens to a packet
```mermaid
flowchart TD
A[Packet received on side X] --> B{Repeater checks:<br/>hop limits, region map,<br/>loop.detect, filter}
B -- refused --> Z[Not carried]
B -- passed --> C{Direct packet whose next hop<br/>is a neighbour heard on side X<br/>and not on side Y?}
C -- yes --> Z
C -- no --> D{fwd rules, first match wins,<br/>else the default}
D -- deny --> Z
D -- allow --> E[Forwarder appends its hash,<br/>queues it on side Y]
E --> F[Side Y waits a slot from its own<br/>listen-before-talk window, then transmits]
```
1. **The repeater's own checks run first, unchanged**: flood hop limits, the region map, `loop.detect`, the
flood blacklist and the twin guard. A packet these refuse never reaches the `fwd` rules, and is not counted in `fwd stats`.
A packet the next two steps refuse is not counted against `req.limit` and is not remembered by the twin guard.
2. **Direct packets need the forwarder as their next hop.** A direct packet reaches the rules only when its path
names the forwarder as the next hop. If the hop after the forwarder is a neighbour heard on the radio the
packet arrived on, and no neighbour with that hash was heard on the other radio, the packet is not carried: it
matched the forwarder's short hash by coincidence and belongs to someone else.
3. **The `fwd` rules decide**: first match wins; a packet no rule matches gets the default.
4. **The other side transmits it.** The packet is never sent on the side it came from. The transmitting side picks
its delay from its own listen-before-talk window, sized to its own SF and bandwidth and to how busy its
channel is.
5. **Hold while the other radio is receiving.** A side holds its transmit while the other radio is in the
middle of receiving a packet, for at most one maximum-length packet's airtime. It cannot protect a reception
that starts while it is already transmitting.
Acknowledgements for direct messages cross the same way, under the same rules (`type=ack`, `route=direct`).
### Remote admin and the CLI
You can log in to the forwarder from either network, as to any repeater. Settings that exist once per radio take a
suffix:
| Where the command comes from | `set freq 869.4` (no suffix) | `set freq.peer 869.4` | `set freq.a` / `set freq.b` |
|---|---|---|---|
| Remote admin over LoRa | the side you are logged in through | the other side | that side |
| USB serial | refused: `Err - freq is per radio, use freq.a or freq.b` | refused | that side |
Settings shared by both sides take no suffix (`Err - <key> is shared by both radios, no suffix`). The full list
of per-radio settings is in the [CLI reference](CLI_commands.md#forwarder-zephcore-only-config_zephcore_role_forwarder).
Over USB, `advert` without a suffix advertises on both radios, and `neighbors` shows each neighbour with `:a`
or `:b`. `get role` answers `forwarder`.
## Setting one up
### Hardware and firmware
You need a board with two LoRa radios described in its devicetree as `lora0` and `lora1`. The supported build
today is a **RAK4631 with a RAK13300 in the WisBlock IO slot**: side a is the RAK4631's own SX1262, side b the
RAK13300. Each radio needs its own antenna.
The forwarder is not in the release downloads yet. Build it ([BUILDING.md](BUILDING.md)):
```sh
west build -b rak4631 zephcore --pristine -- \
-DEXTRA_CONF_FILE="boards/common/forwarder.conf;boards/nrf52840/rak4631/rak13300.conf"
```
and copy `build/zephyr/zephyr.uf2` to the board's UF2 drive. A node that already runs ZephCore can be put into
the UF2 bootloader with `start dfu`.
The forwarder has no Bluetooth: configure it over USB serial (115200 baud, any terminal) or by remote admin.
On the RAK13300 build the user button is not available: its pin is the RAK13300's BUSY line.
### Moving from a repeater
Reflashing a repeater (or room server) as a forwarder keeps its identity, logins and regions. Its settings become
side a. Side b starts as a copy of side a named `<name>-B`, so **both radios start on side a's frequency** until
you set side b. No rules exist yet, so nothing crosses.
### Step by step
The example joins a network on 869.432 MHz with one on 869.618 MHz, both 62.5 kHz, SF7, CR5. Run these over USB.
1. **Name the sides** (optional, but it makes neighbour lists and logins clearer):
```
set name.a Border-CZ
set name.b Border-SK
```
2. **Set each radio** (frequency, bandwidth, SF, CR) and its TX power:
```
set radio.a 869.432,62.5,7,5
set radio.b 869.618,62.5,7,5
set tx.a 22
set tx.b 22
```
3. **Set the admin password** (shared by both sides): `password <new-password>`.
4. **Add the regions you want to carry to the region map**. A region-scoped flood is only forwarded when its
region is in the map with floods allowed, exactly as on a repeater, and this check comes before the `fwd`
rules:
```
region put #cs
region save
```
5. **Add the rules** ([next section](#forwarding-rules)), for example:
```
fwd add both allow route=direct
fwd add both allow region=#cs
fwd add both allow type=grp_txt channel=#cs
```
6. **Reboot** to apply the radio settings: `reboot`. The `set radio` and `set freq` replies say
`OK - reboot to apply`.
7. **Check it**:
```
get radio.a
get radio.b
stats-packets.a received / sent on side a
stats-packets.b
neighbors each neighbour ends in :a or :b
fwd stats packets carried each way, and hits per rule
```
If `stats-packets` grows on a side, that radio is receiving. `fwd stats` shows whether the rules are reached at
all (see [Troubleshooting](#troubleshooting)).
## Forwarding rules
### How they are evaluated
- The rules are an ordered list, at most 16. **The first rule that matches decides**, allow or deny.
- A packet no rule matches gets the **default**, `deny` on a new forwarder.
- Rules only see packets that passed the repeater checks and, for direct packets, are addressed through the
forwarder (see [What happens to a packet](#what-happens-to-a-packet)).
- Rules are saved at once (in `/lfs/repeater/fwd`) and apply immediately; no reboot.
### Commands
| Command | What it does |
|---|---|
| `fwd` or `fwd list [n]` | Show the rules, numbered, and the default. `n` starts the listing at rule `n`, for replies too long for one radio packet |
| `fwd add <dir> <allow\|deny> [field ...]` | Append a rule |
| `fwd ins <n> <dir> <allow\|deny> [field ...]` | Insert a rule at position `n` (it becomes rule `n`) |
| `fwd del <n>` | Delete rule `n` |
| `fwd default allow\|deny` | What a packet no rule matches gets |
| `fwd clear` | Delete every rule; the default stays as it is |
| `fwd stats [n]` | `a>b:` and `b>a:` packets carried each way, then how many packets each rule and the default decided, since boot |
| `fwd help` | Syntax summary |
### A rule
```
<direction> <allow|deny> [field ...]
```
`<direction>` is `a>b` (packets heard on side a, going to side b), `b>a`, or `both`. Every field you give must
match; a rule with no fields matches every packet in its direction.
| Field | Matches | Example |
|---|---|---|
| `route=` | How the packet travels: `flood` (both kinds), `uflood` (unscoped flood), `tflood` (region-scoped flood), `direct`. A comma list matches any of them | `route=uflood,direct` |
| `type=` | Payload type: `req`, `resp`, `txt`, `ack`, `advert`, `grp_txt`, `grp_data`, `anon_req`, `path`, `trace`, `multipart`, `control`, `raw`. A comma list matches any | `type=txt,ack` |
| `region=` | The flood is scoped to this region. `#name` (or a bare `name`) is the public hashtag region; `$name` is a private region from the region map | `region=#cs` |
| `channel=` | A `grp_txt` or `grp_data` packet of this channel: `#name` for a hashtag channel, or the channel's 32-hex-character key. The packet must verify with the key, not just share its 1-byte channel hash | `channel=#hamradio` |
| `hops<=` | The packet's hop count as received, before the forwarder adds itself (0 to 63) | `hops<=3` |
A region-scoped flood only reaches a `region=` rule if its region is in the forwarder's region map (`region put`).
### Examples
#### Emulating meshcore-forwarder (CZ/SK)
This meshcore-forwarder filter:
```js
filter(packet, constants) {
if (!packet.isFlood) return true;
return packet.isInRegion('#cs') || (packet.type === constants.GRP_TXT &&
(packet.isFromChannel('#cs') || packet.isFromChannel('#hamradio')));
}
```
is, on the forwarder:
```
region put #cs
region save
fwd add both allow route=direct
fwd add both allow region=#cs
fwd add both allow type=grp_txt channel=#cs
fwd add both allow type=grp_txt channel=#hamradio
fwd default deny
```
What crosses, and what does not:
| Packet | Result | Why |
|---|---|---|
| Direct message, path through the forwarder | Carried | rule 1 |
| Its acknowledgement | Carried | rule 1 |
| Direct message between two nodes on the same side | Not carried | not addressed through the forwarder |
| Any flood scoped to `#cs` (messages, adverts, path discovery) | Carried | rule 2 |
| Unscoped message on channel `#cs` or `#hamradio` | Carried | rules 3 and 4 |
| Unscoped message on Public | Not carried | default |
| Unscoped advert | Not carried | default |
Unscoped adverts do not cross, so the two networks do not learn each other's nodes from ordinary adverts. Nodes
that should be reachable across the border set their **default flood scope to `#cs`** (in the MeshCore app, or
`set default_scope #cs` in meshcore-cli). Their adverts, channel messages and first messages to a contact are
then scoped to `#cs` and cross under rule 2. Once a direct path has been learned through the forwarder, later
messages cross as direct traffic under rule 1.
#### Carry everything except unscoped adverts
```
fwd add both deny route=uflood type=advert
fwd default allow
```
The forwarder still never carries a packet twice, and the repeater checks (hop limits, `loop.detect`) still apply.
Carrying everything doubles the flood load of both networks; start narrow.
#### One way only
Let channel `#emergency` from side a reach side b, but not the other way:
```
fwd add a>b allow type=grp_txt channel=#emergency
```
#### Order matters: an exception before the general rule
Carry `#cs`-scoped floods, except adverts that have already travelled more than 3 hops:
```
fwd add both allow region=#cs type=advert hops<=3
fwd add both deny region=#cs type=advert
fwd add both allow region=#cs
```
To put an exception in front of existing rules, insert it: `fwd ins 1 both deny type=grp_txt channel=#noisy`.
#### A channel by its key
The Public channel has a well-known key, so it can be named directly:
```
fwd add both deny type=grp_txt channel=8b3387e9c5cdea6ac9e5edbaa115cd72
```
#### Direct traffic only
Join two networks for direct messages and replies only, with no floods at all:
```
fwd add both allow route=direct
```
Contacts must then be learned some other way (sharing contact cards), and the first message to a contact, which
is a flood, does not cross. In practice add a scoped region as in the CZ/SK example.
## Troubleshooting
| Symptom | Likely cause |
|---|---|
| Nothing crosses, `fwd stats` shows `default:` growing | No rule matches; the default is `deny`. Check `fwd` |
| Region-scoped floods do not cross, and **no** counter in `fwd stats` moves | The region is not in the region map, so the repeater checks drop the flood before the rules. `region put #name`, then `region save` |
| Adverts from the other network never arrive | Unscoped adverts are not carried by your rules. Scope them on the sending nodes (default flood scope) or add a rule for them |
| `stats-packets` on one side stays at 0 | That radio is not receiving: check its antenna, `get radio.<side>`, and that you rebooted after changing it |
| A node on side b hears a node on side a directly, although they are on different frequencies | Strong signals close together can still be decoded off-channel. On a bench at a few centimetres this was seen with two channels exactly one bandwidth apart (62.5 kHz): use frequencies that are not a whole number of bandwidths apart, or more distance |
| Two contacts for the same node | Each side advertises under its own name, and a node that hears both networks sees both |
Two more things to know:
- **Same-band radios in one box desense each other.** While one side transmits, the other side can miss packets.
Keep the antennas apart and the TX power no higher than needed.
- **`fwd stats` rule counters include some duplicates.** For direct and TRACE packets the rules are consulted
before duplicates are discarded, so a retransmitted copy is counted again. `a>b:` / `b>a:` count packets
actually queued on the other radio.
## Where it stores things
| File | Contents |
|---|---|
| `/lfs/repeater/prefs.json` | Side a and the shared settings, the same file and format as a repeater |
| `/lfs/repeater/prefs_b.json` | Side b, the same format |
| `/lfs/repeater/fwd` | The rules and the default |
The region map, logins and identity are stored as on a repeater.