Skip to content

Commit e3698e7

Browse files
committed
Add support for B.A.T.M.A.N. Advanced
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 <linus.luessing@c0d3.blue>
1 parent 2aaaef7 commit e3698e7

10 files changed

+453
-3
lines changed

batadv_legacy_packet.h

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* SPDX-License-Identifier: BSD-3 */
2+
/* Copyright (C) 2020 Linus Lüssing */
3+
4+
#ifndef _BATADV_LEGACY_PACKET_H_
5+
#define _BATADV_LEGACY_PACKET_H_
6+
7+
enum batadv_legacy_packettype {
8+
BATADV_LEGACY_IV_OGM = 0x01,
9+
BATADV_LEGACY_ICMP = 0x02,
10+
BATADV_LEGACY_UNICAST = 0x03,
11+
BATADV_LEGACY_BCAST = 0x04,
12+
BATADV_LEGACY_VIS = 0x05,
13+
BATADV_LEGACY_UNICAST_FRAG = 0x06,
14+
BATADV_LEGACY_TT_QUERY = 0x07,
15+
BATADV_LEGACY_ROAM_ADV = 0x08,
16+
BATADV_LEGACY_UNICAST_4ADDR = 0x09,
17+
BATADV_LEGACY_CODED = 0x0a,
18+
};
19+
20+
#define ETH_ALEN 6
21+
22+
struct batadv_legacy_unicast_packet {
23+
uint8_t packet_type;
24+
uint8_t version;
25+
uint8_t ttl;
26+
uint8_t ttvn;
27+
uint8_t dest[ETH_ALEN];
28+
};
29+
30+
struct batadv_legacy_unicast_4addr_packet {
31+
uint8_t packet_type;
32+
uint8_t version;
33+
uint8_t ttl;
34+
uint8_t src[ETH_ALEN];
35+
uint8_t subtype;
36+
uint8_t reserved;
37+
};
38+
39+
struct batadv_legacy_unicast_frag_packet {
40+
uint8_t packet_type;
41+
uint8_t version;
42+
uint8_t ttl;
43+
uint8_t ttvn;
44+
uint8_t dest[ETH_ALEN];
45+
uint8_t flags;
46+
uint8_t align;
47+
uint8_t orig[ETH_ALEN];
48+
uint8_t seqno[2]; /* 2-byte integral value */
49+
};
50+
51+
struct batadv_legacy_bcast_packet {
52+
uint8_t packet_type;
53+
uint8_t version;
54+
uint8_t ttl;
55+
uint8_t reserved;
56+
uint8_t seqno[4]; /* 4-byte integral value */
57+
uint8_t orig[ETH_ALEN];
58+
};
59+
60+
struct batadv_legacy_coded_packet {
61+
uint8_t packet_type;
62+
uint8_t version;
63+
uint8_t ttl;
64+
uint8_t first_ttvn;
65+
uint8_t first_source[ETH_ALEN];
66+
uint8_t first_orig_dest[ETH_ALEN];
67+
uint8_t first_crc[4]; /* 4-byte integral value */
68+
uint8_t second_ttl;
69+
uint8_t second_ttvn;
70+
uint8_t second_dest[ETH_ALEN];
71+
uint8_t second_source[ETH_ALEN];
72+
uint8_t second_orig_dest[ETH_ALEN];
73+
uint8_t second_crc[4]; /* 4-byte integral value */
74+
uint8_t coded_len[2]; /* 2-byte integral value */
75+
};
76+
77+
#endif /* _BATADV_LEGACY_PACKET_H_ */

batadv_packet.h

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* SPDX-License-Identifier: BSD-3 */
2+
/* Copyright (C) 2020 Linus Lüssing */
3+
4+
#ifndef _BATADV_PACKET_H_
5+
#define _BATADV_PACKET_H_
6+
7+
/* For the definitive and most recent packet format definition,
8+
* see the batadv_packet.h in the Linux kernel.
9+
*/
10+
11+
enum batadv_packettype {
12+
BATADV_IV_OGM = 0x00,
13+
BATADV_BCAST = 0x01,
14+
BATADV_CODED = 0x02,
15+
BATADV_ELP = 0x03,
16+
BATADV_OGM2 = 0x04,
17+
BATADV_UNICAST = 0x40,
18+
BATADV_UNICAST_FRAG = 0x41,
19+
BATADV_UNICAST_4ADDR = 0x42,
20+
BATADV_ICMP = 0x43,
21+
BATADV_UNICAST_TVLV = 0x44,
22+
};
23+
24+
#define ETH_ALEN 6
25+
26+
struct batadv_unicast_packet {
27+
uint8_t packet_type;
28+
uint8_t version;
29+
uint8_t ttl;
30+
uint8_t ttvn;
31+
uint8_t dest[ETH_ALEN];
32+
};
33+
34+
struct batadv_unicast_4addr_packet {
35+
struct batadv_unicast_packet u;
36+
uint8_t src[ETH_ALEN];
37+
uint8_t subtype;
38+
uint8_t reserved;
39+
};
40+
41+
struct batadv_frag_packet {
42+
uint8_t packet_type;
43+
uint8_t version;
44+
uint8_t ttl;
45+
uint8_t num_pri; /* number and priority */
46+
uint8_t dest[ETH_ALEN];
47+
uint8_t orig[ETH_ALEN];
48+
uint8_t seqno[2]; /* 2-byte integral value */
49+
uint8_t total_size[2]; /* 2-byte integral value */
50+
};
51+
52+
struct batadv_bcast_packet {
53+
uint8_t packet_type;
54+
uint8_t version;
55+
uint8_t ttl;
56+
uint8_t reserved;
57+
uint8_t seqno[4]; /* 4-byte integral value */
58+
uint8_t orig[ETH_ALEN];
59+
};
60+
61+
struct batadv_coded_packet {
62+
uint8_t packet_type;
63+
uint8_t version;
64+
uint8_t ttl;
65+
uint8_t first_ttvn;
66+
uint8_t first_source[ETH_ALEN];
67+
uint8_t first_orig_dest[ETH_ALEN];
68+
uint8_t first_crc[4]; /* 4-byte integral value */
69+
uint8_t second_ttl;
70+
uint8_t second_ttvn;
71+
uint8_t second_dest[ETH_ALEN];
72+
uint8_t second_source[ETH_ALEN];
73+
uint8_t second_orig_dest[ETH_ALEN];
74+
uint8_t second_crc[4]; /* 4-byte integral value */
75+
uint8_t coded_len[2]; /* 2-byte integral value */
76+
};
77+
78+
#endif /* _BATADV_PACKET_H_ */

ethertype.h

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
#ifndef ETHERTYPE_TRAIL
5050
#define ETHERTYPE_TRAIL 0x1000
5151
#endif
52+
#ifndef ETHERTYPE_BATMAN
53+
#define ETHERTYPE_BATMAN 0x4305 /* B.A.T.M.A.N. Advanced */
54+
#endif
5255
#ifndef ETHERTYPE_MOPDL
5356
#define ETHERTYPE_MOPDL 0x6001
5457
#endif

gencode.c

+164
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
#include "atmuni31.h"
7575
#include "sunatmpos.h"
7676
#include "ppp.h"
77+
#include "batadv_packet.h"
78+
#include "batadv_legacy_packet.h"
7779
#include "pcap/sll.h"
7880
#include "pcap/ipnet.h"
7981
#include "arcnet.h"
@@ -9505,6 +9507,168 @@ gen_geneve(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
95059507
return b1;
95069508
}
95079509

9510+
static struct block *
9511+
gen_batadv_check_version(compiler_state_t *cstate, struct block *b0, bpf_u_int32 version)
9512+
{
9513+
struct block *b1;
9514+
9515+
if (version > UINT8_MAX)
9516+
bpf_error(cstate,
9517+
"batman-adv compatibility version number %u unsupported",
9518+
version);
9519+
9520+
b1 = gen_cmp(cstate, OR_LINKPL, 1, BPF_B, version);
9521+
gen_and(b0, b1);
9522+
9523+
return b1;
9524+
}
9525+
9526+
static struct block *
9527+
gen_batadv_check_type(compiler_state_t *cstate, struct block *b0,
9528+
bpf_u_int32 version, bpf_u_int32 type)
9529+
{
9530+
struct block *b1;
9531+
9532+
switch (version) {
9533+
case 14:
9534+
case 15:
9535+
if (type > UINT8_MAX)
9536+
bpf_error(cstate,
9537+
"batman-adv packet type %u unsupported for compatibility version %u",
9538+
type, version);
9539+
9540+
b1 = gen_cmp(cstate, OR_LINKPL, 0, BPF_B, type);
9541+
gen_and(b0, b1);
9542+
b0 = b1;
9543+
9544+
break;
9545+
default:
9546+
bpf_error(cstate,
9547+
"batman-adv compatibility version number %u unsupported",
9548+
version);
9549+
}
9550+
9551+
return b0;
9552+
}
9553+
9554+
9555+
static void gen_batadv_push_offset(compiler_state_t *cstate, u_int offset)
9556+
{
9557+
PUSH_LINKHDR(cstate, DLT_EN10MB, cstate->off_linkpl.is_variable,
9558+
cstate->off_linkpl.constant_part + cstate->off_nl + offset,
9559+
cstate->off_linkpl.reg);
9560+
9561+
cstate->off_linktype.constant_part += cstate->off_linkhdr.constant_part;
9562+
cstate->off_linkpl.constant_part += cstate->off_linkhdr.constant_part;
9563+
9564+
cstate->off_nl = 0;
9565+
cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
9566+
}
9567+
9568+
static void
9569+
gen_batadv_offsets_v14(compiler_state_t *cstate, bpf_u_int32 type)
9570+
{
9571+
size_t offset;
9572+
9573+
switch (type) {
9574+
case BATADV_LEGACY_UNICAST: /* 0x03 */
9575+
offset = sizeof(struct batadv_legacy_unicast_packet);
9576+
break;
9577+
case BATADV_LEGACY_BCAST: /* 0x04 */
9578+
offset = sizeof(struct batadv_legacy_bcast_packet);
9579+
break;
9580+
case BATADV_LEGACY_UNICAST_FRAG: /* 0x06 */
9581+
offset = sizeof(struct batadv_legacy_unicast_frag_packet);
9582+
break;
9583+
case BATADV_LEGACY_UNICAST_4ADDR: /* 0x09 */
9584+
offset = sizeof(struct batadv_legacy_unicast_4addr_packet);
9585+
break;
9586+
case BATADV_LEGACY_CODED: /* 0x0a */
9587+
offset = sizeof(struct batadv_legacy_coded_packet);
9588+
break;
9589+
default:
9590+
offset = 0;
9591+
}
9592+
9593+
if (offset)
9594+
gen_batadv_push_offset(cstate, (u_int)offset);
9595+
}
9596+
9597+
static void
9598+
gen_batadv_offsets_v15(compiler_state_t *cstate, bpf_u_int32 type)
9599+
{
9600+
size_t offset;
9601+
9602+
switch (type) {
9603+
case BATADV_BCAST: /* 0x01 */
9604+
offset = sizeof(struct batadv_bcast_packet);
9605+
break;
9606+
case BATADV_CODED: /* 0x02 */
9607+
offset = sizeof(struct batadv_coded_packet);
9608+
break;
9609+
case BATADV_UNICAST: /* 0x40 */
9610+
offset = sizeof(struct batadv_unicast_packet);
9611+
break;
9612+
case BATADV_UNICAST_FRAG: /* 0x41 */
9613+
offset = sizeof(struct batadv_frag_packet);
9614+
break;
9615+
case BATADV_UNICAST_4ADDR: /* 0x42 */
9616+
offset = sizeof(struct batadv_unicast_4addr_packet);
9617+
break;
9618+
case BATADV_UNICAST_TVLV:
9619+
/* unsupported for now, needs variable offset to
9620+
* take tvlv_len into account
9621+
*/
9622+
/* fall through */
9623+
default:
9624+
offset = 0;
9625+
}
9626+
9627+
if (offset)
9628+
gen_batadv_push_offset(cstate, (u_int)offset);
9629+
}
9630+
9631+
static void
9632+
gen_batadv_offsets(compiler_state_t *cstate, bpf_u_int32 version, bpf_u_int32 type)
9633+
{
9634+
switch (version) {
9635+
case 14:
9636+
gen_batadv_offsets_v14(cstate, type);
9637+
break;
9638+
case 15:
9639+
gen_batadv_offsets_v15(cstate, type);
9640+
break;
9641+
default:
9642+
break;
9643+
}
9644+
}
9645+
9646+
struct block *
9647+
gen_batadv(compiler_state_t *cstate, bpf_u_int32 version, int has_version,
9648+
bpf_u_int32 type, int has_type)
9649+
{
9650+
struct block *b0;
9651+
9652+
/*
9653+
* Catch errors reported by us and routines below us, and return NULL
9654+
* on an error.
9655+
*/
9656+
if (setjmp(cstate->top_ctx))
9657+
return (NULL);
9658+
9659+
b0 = gen_linktype(cstate, ETHERTYPE_BATMAN);
9660+
9661+
if (has_version)
9662+
b0 = gen_batadv_check_version(cstate, b0, version);
9663+
9664+
if (has_type) {
9665+
b0 = gen_batadv_check_type(cstate, b0, version, type);
9666+
gen_batadv_offsets(cstate, version, type);
9667+
}
9668+
9669+
return b0;
9670+
}
9671+
95089672
/* Check that the encapsulated frame has a link layer header
95099673
* for Ethernet filters. */
95109674
static struct block *

gencode.h

+3
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ struct block *gen_pppoes(compiler_state_t *, bpf_u_int32, int);
350350

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

353+
struct block *gen_batadv(compiler_state_t *, bpf_u_int32, int,
354+
bpf_u_int32, int);
355+
353356
struct block *gen_atmfield_code(compiler_state_t *, int, bpf_u_int32,
354357
int, int);
355358
struct block *gen_atmtype_abbrev(compiler_state_t *, int);

0 commit comments

Comments
 (0)