Skip to content

Commit

Permalink
Add support for B.A.T.M.A.N. Advanced
Browse files Browse the repository at this point in the history
This adds support for the layer 2 mesh routing protocol
B.A.T.M.A.N. Advanced. "batadv" can be used to filter on batman-adv
packets. It also allows later filters to look at frames inside the
tunnel when both "version" and "type" are specified.

Documentation for the batman-adv protocol can be found at the following
locations:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/batman-adv.rst
https://www.open-mesh.org/

Signed-off-by: Linus Lüssing <[email protected]>
  • Loading branch information
T-X authored and fxlb committed Dec 4, 2022
1 parent 350244e commit 3d8d268
Show file tree
Hide file tree
Showing 11 changed files with 453 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ PUBHDR = \
HDR = $(PUBHDR) \
arcnet.h \
atmuni31.h \
batadv_legacy_packet.h \
batadv_packet.h \
diag-control.h \
ethertype.h \
extract.h \
Expand Down
77 changes: 77 additions & 0 deletions batadv_legacy_packet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* SPDX-License-Identifier: BSD-3 */
/* Copyright (C) 2020 Linus Lüssing */

#ifndef _BATADV_LEGACY_PACKET_H_
#define _BATADV_LEGACY_PACKET_H_

enum batadv_legacy_packettype {
BATADV_LEGACY_IV_OGM = 0x01,
BATADV_LEGACY_ICMP = 0x02,
BATADV_LEGACY_UNICAST = 0x03,
BATADV_LEGACY_BCAST = 0x04,
BATADV_LEGACY_VIS = 0x05,
BATADV_LEGACY_UNICAST_FRAG = 0x06,
BATADV_LEGACY_TT_QUERY = 0x07,
BATADV_LEGACY_ROAM_ADV = 0x08,
BATADV_LEGACY_UNICAST_4ADDR = 0x09,
BATADV_LEGACY_CODED = 0x0a,
};

#define ETH_ALEN 6

struct batadv_legacy_unicast_packet {
uint8_t packet_type;
uint8_t version;
uint8_t ttl;
uint8_t ttvn;
uint8_t dest[ETH_ALEN];
};

struct batadv_legacy_unicast_4addr_packet {
uint8_t packet_type;
uint8_t version;
uint8_t ttl;
uint8_t src[ETH_ALEN];
uint8_t subtype;
uint8_t reserved;
};

struct batadv_legacy_unicast_frag_packet {
uint8_t packet_type;
uint8_t version;
uint8_t ttl;
uint8_t ttvn;
uint8_t dest[ETH_ALEN];
uint8_t flags;
uint8_t align;
uint8_t orig[ETH_ALEN];
uint8_t seqno[2]; /* 2-byte integral value */
};

struct batadv_legacy_bcast_packet {
uint8_t packet_type;
uint8_t version;
uint8_t ttl;
uint8_t reserved;
uint8_t seqno[4]; /* 4-byte integral value */
uint8_t orig[ETH_ALEN];
};

struct batadv_legacy_coded_packet {
uint8_t packet_type;
uint8_t version;
uint8_t ttl;
uint8_t first_ttvn;
uint8_t first_source[ETH_ALEN];
uint8_t first_orig_dest[ETH_ALEN];
uint8_t first_crc[4]; /* 4-byte integral value */
uint8_t second_ttl;
uint8_t second_ttvn;
uint8_t second_dest[ETH_ALEN];
uint8_t second_source[ETH_ALEN];
uint8_t second_orig_dest[ETH_ALEN];
uint8_t second_crc[4]; /* 4-byte integral value */
uint8_t coded_len[2]; /* 2-byte integral value */
};

#endif /* _BATADV_LEGACY_PACKET_H_ */
78 changes: 78 additions & 0 deletions batadv_packet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* SPDX-License-Identifier: BSD-3 */
/* Copyright (C) 2020 Linus Lüssing */

#ifndef _BATADV_PACKET_H_
#define _BATADV_PACKET_H_

/* For the definitive and most recent packet format definition,
* see the batadv_packet.h in the Linux kernel.
*/

enum batadv_packettype {
BATADV_IV_OGM = 0x00,
BATADV_BCAST = 0x01,
BATADV_CODED = 0x02,
BATADV_ELP = 0x03,
BATADV_OGM2 = 0x04,
BATADV_UNICAST = 0x40,
BATADV_UNICAST_FRAG = 0x41,
BATADV_UNICAST_4ADDR = 0x42,
BATADV_ICMP = 0x43,
BATADV_UNICAST_TVLV = 0x44,
};

#define ETH_ALEN 6

struct batadv_unicast_packet {
uint8_t packet_type;
uint8_t version;
uint8_t ttl;
uint8_t ttvn;
uint8_t dest[ETH_ALEN];
};

struct batadv_unicast_4addr_packet {
struct batadv_unicast_packet u;
uint8_t src[ETH_ALEN];
uint8_t subtype;
uint8_t reserved;
};

struct batadv_frag_packet {
uint8_t packet_type;
uint8_t version;
uint8_t ttl;
uint8_t num_pri; /* number and priority */
uint8_t dest[ETH_ALEN];
uint8_t orig[ETH_ALEN];
uint8_t seqno[2]; /* 2-byte integral value */
uint8_t total_size[2]; /* 2-byte integral value */
};

struct batadv_bcast_packet {
uint8_t packet_type;
uint8_t version;
uint8_t ttl;
uint8_t reserved;
uint8_t seqno[4]; /* 4-byte integral value */
uint8_t orig[ETH_ALEN];
};

struct batadv_coded_packet {
uint8_t packet_type;
uint8_t version;
uint8_t ttl;
uint8_t first_ttvn;
uint8_t first_source[ETH_ALEN];
uint8_t first_orig_dest[ETH_ALEN];
uint8_t first_crc[4]; /* 4-byte integral value */
uint8_t second_ttl;
uint8_t second_ttvn;
uint8_t second_dest[ETH_ALEN];
uint8_t second_source[ETH_ALEN];
uint8_t second_orig_dest[ETH_ALEN];
uint8_t second_crc[4]; /* 4-byte integral value */
uint8_t coded_len[2]; /* 2-byte integral value */
};

#endif /* _BATADV_PACKET_H_ */
3 changes: 3 additions & 0 deletions ethertype.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
#ifndef ETHERTYPE_TRAIL
#define ETHERTYPE_TRAIL 0x1000
#endif
#ifndef ETHERTYPE_BATMAN
#define ETHERTYPE_BATMAN 0x4305 /* B.A.T.M.A.N. Advanced */
#endif
#ifndef ETHERTYPE_MOPDL
#define ETHERTYPE_MOPDL 0x6001
#endif
Expand Down
164 changes: 164 additions & 0 deletions gencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
#include "sunatmpos.h"
#include "pflog.h"
#include "ppp.h"
#include "batadv_packet.h"
#include "batadv_legacy_packet.h"
#include "pcap/sll.h"
#include "pcap/ipnet.h"
#include "arcnet.h"
Expand Down Expand Up @@ -9436,6 +9438,168 @@ gen_geneve(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
return b1;
}

static struct block *
gen_batadv_check_version(compiler_state_t *cstate, struct block *b0, bpf_u_int32 version)
{
struct block *b1;

if (version > UINT8_MAX)
bpf_error(cstate,
"batman-adv compatibility version number %u unsupported",
version);

b1 = gen_cmp(cstate, OR_LINKPL, 1, BPF_B, version);
gen_and(b0, b1);

return b1;
}

static struct block *
gen_batadv_check_type(compiler_state_t *cstate, struct block *b0,
bpf_u_int32 version, bpf_u_int32 type)
{
struct block *b1;

switch (version) {
case 14:
case 15:
if (type > UINT8_MAX)
bpf_error(cstate,
"batman-adv packet type %u unsupported for compatibility version %u",
type, version);

b1 = gen_cmp(cstate, OR_LINKPL, 0, BPF_B, type);
gen_and(b0, b1);
b0 = b1;

break;
default:
bpf_error(cstate,
"batman-adv compatibility version number %u unsupported",
version);
}

return b0;
}


static void gen_batadv_push_offset(compiler_state_t *cstate, u_int offset)
{
PUSH_LINKHDR(cstate, DLT_EN10MB, cstate->off_linkpl.is_variable,
cstate->off_linkpl.constant_part + cstate->off_nl + offset,
cstate->off_linkpl.reg);

cstate->off_linktype.constant_part += cstate->off_linkhdr.constant_part;
cstate->off_linkpl.constant_part += cstate->off_linkhdr.constant_part;

cstate->off_nl = 0;
cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
}

static void
gen_batadv_offsets_v14(compiler_state_t *cstate, bpf_u_int32 type)
{
size_t offset;

switch (type) {
case BATADV_LEGACY_UNICAST: /* 0x03 */
offset = sizeof(struct batadv_legacy_unicast_packet);
break;
case BATADV_LEGACY_BCAST: /* 0x04 */
offset = sizeof(struct batadv_legacy_bcast_packet);
break;
case BATADV_LEGACY_UNICAST_FRAG: /* 0x06 */
offset = sizeof(struct batadv_legacy_unicast_frag_packet);
break;
case BATADV_LEGACY_UNICAST_4ADDR: /* 0x09 */
offset = sizeof(struct batadv_legacy_unicast_4addr_packet);
break;
case BATADV_LEGACY_CODED: /* 0x0a */
offset = sizeof(struct batadv_legacy_coded_packet);
break;
default:
offset = 0;
}

if (offset)
gen_batadv_push_offset(cstate, (u_int)offset);
}

static void
gen_batadv_offsets_v15(compiler_state_t *cstate, bpf_u_int32 type)
{
size_t offset;

switch (type) {
case BATADV_BCAST: /* 0x01 */
offset = sizeof(struct batadv_bcast_packet);
break;
case BATADV_CODED: /* 0x02 */
offset = sizeof(struct batadv_coded_packet);
break;
case BATADV_UNICAST: /* 0x40 */
offset = sizeof(struct batadv_unicast_packet);
break;
case BATADV_UNICAST_FRAG: /* 0x41 */
offset = sizeof(struct batadv_frag_packet);
break;
case BATADV_UNICAST_4ADDR: /* 0x42 */
offset = sizeof(struct batadv_unicast_4addr_packet);
break;
case BATADV_UNICAST_TVLV:
/* unsupported for now, needs variable offset to
* take tvlv_len into account
*/
/* fall through */
default:
offset = 0;
}

if (offset)
gen_batadv_push_offset(cstate, (u_int)offset);
}

static void
gen_batadv_offsets(compiler_state_t *cstate, bpf_u_int32 version, bpf_u_int32 type)
{
switch (version) {
case 14:
gen_batadv_offsets_v14(cstate, type);
break;
case 15:
gen_batadv_offsets_v15(cstate, type);
break;
default:
break;
}
}

struct block *
gen_batadv(compiler_state_t *cstate, bpf_u_int32 version, int has_version,
bpf_u_int32 type, int has_type)
{
struct block *b0;

/*
* Catch errors reported by us and routines below us, and return NULL
* on an error.
*/
if (setjmp(cstate->top_ctx))
return (NULL);

b0 = gen_linktype(cstate, ETHERTYPE_BATMAN);

if (has_version)
b0 = gen_batadv_check_version(cstate, b0, version);

if (has_type) {
b0 = gen_batadv_check_type(cstate, b0, version, type);
gen_batadv_offsets(cstate, version, type);
}

return b0;
}

/* Check that the encapsulated frame has a link layer header
* for Ethernet filters. */
static struct block *
Expand Down
3 changes: 3 additions & 0 deletions gencode.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ struct block *gen_pppoes(compiler_state_t *, bpf_u_int32, int);

struct block *gen_geneve(compiler_state_t *, bpf_u_int32, int);

struct block *gen_batadv(compiler_state_t *, bpf_u_int32, int,
bpf_u_int32, int);

struct block *gen_atmfield_code(compiler_state_t *, int, bpf_u_int32,
int, int);
struct block *gen_atmtype_abbrev(compiler_state_t *, int);
Expand Down
Loading

0 comments on commit 3d8d268

Please sign in to comment.