Skip to content

Commit 74c76dc

Browse files
authored
Merge pull request #463 from tiiuae/upgrade-cbma-to-v0.1.7
2 parents 03097d8 + 8b60649 commit 74c76dc

File tree

11 files changed

+74
-33
lines changed

11 files changed

+74
-33
lines changed

modules/sc-mesh-secure-deployment/src/nats/cbma/README.md

+11-10
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@ ip link set wlp1s0 up
1919
# Connect wlp1s0 to the mesh if it isn't - Need a working wpa_supplicant_11s.conf
2020
wpa_supplicant -i wlp1s0 -c wpa_supplicant_11s.conf -D nl80211 -B
2121

22-
# Prepare lower-batman interface (using wlp1s0 locally administered MAC)
23-
ip link del bat0 2>/dev/null
24-
ip link add name bat0 type batadv
25-
ip link set bat0 address $(read a < /sys/class/net/wlp1s0/address && printf "%02x${a:2}\n" $(( 0x${a:0:2} ^ 0x2 )))
26-
ip link set bat0 up
27-
28-
# Create upper-batman inteface
29-
ip link del bat1 2>/dev/null
30-
ip link add name bat1 type batadv
31-
3222
# Install Python dependencies - Recommended to create + activate a venv before
3323
$ python3 -m pip install -r requirements.txt
3424

@@ -45,6 +35,17 @@ $ python3 standalone.py -i wlp1s0 # Runs lower-CBMA by default
4535
$ python3 standalone.py -i bat0 -b bat1 # Add -u if lower-CBMA wasn't established beforehand
4636
# NOTE: if bat0 doesn't have the same MAC as any of its attached interfaces (like LA wlp1s0 one)
4737
# you will have to generate certificates for it as a workaround
38+
39+
# Optional - Now done automatically by standalone.py
40+
# Prepare lower-batman interface (using wlp1s0 locally administered MAC)
41+
ip link del bat0 2>/dev/null
42+
ip link add name bat0 type batadv
43+
ip link set bat0 address $(read a < /sys/class/net/wlp1s0/address && printf "%02x${a:2}\n" $(( 0x${a:0:2} ^ 0x2 )))
44+
ip link set bat0 up
45+
46+
# Create upper-batman inteface
47+
ip link del bat1 2>/dev/null
48+
ip link add name bat1 type batadv
4849
```
4950

5051

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
GIT_VERSION=v0.0.0-old_requirements_fixes
2-
GIT_SHA=261e1b95dbe8a8a99af7cb56ad7abac90fd26884
3-
EPOCH_TIMESTAMP=1714545575
4-
PRECISE_DATE_TIMESTAMP="2024-05-01 - 06:39:35.164044574"
1+
GIT_VERSION=v0.1.7-old_requirements_fixes
2+
GIT_SHA=2b333ce8b4308663fd7901d87add93b731314f56
3+
EPOCH_TIMESTAMP=1717686203
4+
PRECISE_DATE_TIMESTAMP="2024-06-06 - 15:03:23.866091572"

modules/sc-mesh-secure-deployment/src/nats/cbma/scripts/mess/cleanup_bridge.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33

4-
export SCN='/sys/class/net'
4+
source "${BASH_SOURCE%/*}/constants.rc"
55

66
cleanup_bridge_if_needed()
77
{

modules/sc-mesh-secure-deployment/src/nats/cbma/scripts/mess/cleanup_mess.sh

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
#! /bin/bash
1+
#!/bin/bash
22

33

4-
5-
export SCN='/sys/class/net'
6-
4+
source "${BASH_SOURCE%/*}/constants.rc"
75

86
cleanup_macscbpad_interface()
97
{
@@ -33,6 +31,7 @@ cleanup_macsec_interface()
3331

3432
cleanup_macvlan_interface()
3533
{
34+
[ -e "$SCN/$MACVLAN_NAME" ] || return 0
3635
cleanup_macscbub_interface
3736
cleanup_macsec_interface
3837
ip link delete "$MACVLAN_NAME"

modules/sc-mesh-secure-deployment/src/nats/cbma/scripts/mess/constants.rc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
export MACSEC_OVERHEAD=16
33

44
# needs more testing, an overhead of 32 might even occur if batman-adv uses 4 address mode
5-
export BATMAN_OVERHEAD=24
5+
export BATMAN_OVERHEAD=48
66

77
export HOPEFULLY1500=1500
88
export SCN='/sys/class/net'

modules/sc-mesh-secure-deployment/src/nats/cbma/scripts/mess/create_mess.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ random_mac_address()
178178

179179
create_macvlan_interface()
180180
{
181-
if ! ip link add link "$BASE_INTERFACE_NAME" name "$MACVLAN_NAME" address "$LOCAL_MAC" mtu "$MACVLAN_MTU" type macvlan mode source bcqueuelen 0 \
181+
if ! ip link add link "$BASE_INTERFACE_NAME" name "$MACVLAN_NAME" address "$LOCAL_MAC" mtu "$MACVLAN_MTU" type macvlan mode source nodst bcqueuelen 0 \
182182
|| ! ip link set link dev "$MACVLAN_NAME" type macvlan macaddr add "$REMOTE_MAC"; then
183183
return `false`
184184
fi
@@ -216,12 +216,12 @@ create_macvlan_interface()
216216
create_bridge_if_needed()
217217
{
218218
if [ ! -e "$SCN/$MACBR_NAME/bridge" ]; then
219-
>&2 echo "Error: creation has been moved to create_bridge.sh, $MACBR_NAME should exist"
220-
return `false`
219+
>&2 echo "WARN: $MACBR_NAME doesn't exist when it should, recreating it but something is misbehaving"
220+
"${BASH_SOURCE%/*}/create_bridge.sh" "$L_OR_U" "$BASE_INTERFACE_NAME" || return $?
221221
fi
222222
create_macvlan_interface || return `false`
223223
REMOTE_EUI64=`mac_to_eui64 "$REMOTE_MAC"`
224-
REMOTE_LLA="fe80::$REMOTE_EUI64"
224+
REMOTE_LLA="fe80::$REMOTE_EUI64"
225225
ip neigh replace "$REMOTE_LLA" lladdr "$REMOTE_MAC" dev "$MACBR_NAME"
226226
}
227227

modules/sc-mesh-secure-deployment/src/nats/cbma/scripts/run_simulation.sh

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ KEYPAIR_TYPE="rsa" # Can be ecdsa, eddsa, or rsa
77

88
DEFAULT_LOG_LEVEL="INFO"
99

10-
BASE_MTU="1400"
10+
BASE_MTU="1500"
1111

1212
CBMA_DEBUG=0
1313
BAT_DEBUG=0
@@ -104,8 +104,7 @@ setup_wlan() {
104104
# ip netns exec "$I" ip link set dev "$WLAN" name "wlp1s${I}"
105105
# ip netns exec "$I" iw dev "wlp1s${I}" set type mesh
106106

107-
# NOTE - No need to set MTU for now
108-
# ip netns exec "$I" ip link set dev "wlp1s${I}" mtu $((BASE_MTU + 80))
107+
ip netns exec "$I" ip link set dev "wlp1s${I}" mtu $((BASE_MTU + 80))
109108

110109
ip netns exec "$I" ip link set dev "wlp1s${I}" address "00:20:91:0${I}:0${I}:0${I}"
111110
ip netns exec "$I" ip link set dev "wlp1s${I}" up
@@ -126,7 +125,7 @@ setup_eth() {
126125
ip link add "$ETH" type veth peer name "eth${I}" netns "$I"
127126

128127
# NOTE - No need to set MTU for now
129-
# ip netns exec "$I" ip link set dev "eth${I}" mtu $((BASE_MTU + 108))
128+
ip netns exec "$I" ip link set dev "eth${I}" mtu $((BASE_MTU + 108))
130129
ip netns exec "$I" ip link set dev "eth${I}" address "00:20:91:${I}0:${I}0:${I}0"
131130

132131
ip link set "$ETH" up

modules/sc-mesh-secure-deployment/src/nats/cbma/scripts/upgrade_cbma.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ cleanup_mdm_agent_files() {
4747
command ssh ${ID_RSA:+-i "$ID_RSA"} $SSH_OPTIONS "root@$IP" bash <<-EOF || exit 6
4848
rm -f /opt/certs_uploaded
4949
rm -rf /opt/certs || :
50+
rm -rf /opt/mdm || :
5051
EOF
5152
}
5253

@@ -122,6 +123,6 @@ fi
122123
stop_mdm_agent "$IP" "$ID_RSA"
123124
cleanup_logs "$IP" "$ID_RSA"
124125
cleanup_mdm_agent_files "$IP" "$ID_RSA"
125-
upgrade_cbma "$CBMA" "$IP" "$ID_RSA"
126+
# upgrade_cbma "$CBMA" "$IP" "$ID_RSA"
126127
# upgrade_birthcerts "$IP" "$ID_RSA"
127128
start_mdm_agent "$IP" "$ID_RSA"

modules/sc-mesh-secure-deployment/src/nats/cbma/standalone.py

+41-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import sys
23
import argparse
34

@@ -14,8 +15,9 @@
1415

1516

1617
BATMAN_ROUTING_ALG = 'BATMAN_V'
18+
CBMA_ROOT = os.path.normpath(os.path.dirname(__file__))
1719

18-
def get_interface_locally_administed_mac(interface: str) -> str:
20+
def get_interface_locally_administered_mac(interface: str) -> str:
1921
mac = get_interface_mac_address(interface)
2022
mac_bytes = bytearray.fromhex(mac.replace(':', ''))
2123
mac_bytes[0] ^= 0x2 # Locally administered bit
@@ -35,6 +37,32 @@ def destroy_batman(batman: str) -> None:
3537
destroy_batman_str = f"ip link del {batman}"
3638
run_command_retcode(destroy_batman_str.split())
3739

40+
def get_mtu_from_constants_rc(exclude: list[str] = []) -> int:
41+
mtu = 0
42+
constants_rc = f"{CBMA_ROOT}/scripts/mess/constants.rc"
43+
with open(constants_rc, 'r') as f:
44+
for line in f.readlines():
45+
if line.startswith('#') or not '=' in line \
46+
or not ('OVERHEAD' in line or 'HOPEFULLY' in line):
47+
continue
48+
for e in exclude:
49+
if e in line:
50+
break
51+
else:
52+
try:
53+
mtu += int(line.split('=')[-1].strip())
54+
except ValueError:
55+
logger.warning(f"Ignoring '{line.strip()}' for MTU calculation")
56+
if not mtu:
57+
logger.error(f"Unable to get MTU value from {constants_rc}")
58+
sys.exit(255)
59+
60+
return mtu
61+
62+
def set_interface_mtu(interface: str, mtu: int) -> bool:
63+
cmd_str = f"ip link set {interface} mtu {mtu}"
64+
return not run_command_retcode(cmd_str.split())
65+
3866

3967
if __name__ == '__main__':
4068
parser = argparse.ArgumentParser(description='CBMA standalone parameters')
@@ -101,6 +129,17 @@ def destroy_batman(batman: str) -> None:
101129
certificates = CBMACertificates(cert_dir, key, chain, ca)
102130

103131
is_upper = args.upper or any('bat' in i and glob(f"/sys/class/net/*/upper_{i}") for i in interfaces)
132+
133+
mtu_base = get_mtu_from_constants_rc(exclude=['OVERHEAD'])
134+
mtu_overhead = get_mtu_from_constants_rc(exclude=['HOPEFULLY'])
135+
if not is_upper:
136+
mtu_overhead *= 2
137+
mtu = mtu_base + mtu_overhead
138+
139+
for i in interfaces:
140+
if not set_interface_mtu(i, mtu):
141+
sys.exit(255)
142+
104143
enable_macsec_encryption = is_upper
105144
try:
106145
controller = CBMAController(args.port,
@@ -113,7 +152,7 @@ def destroy_batman(batman: str) -> None:
113152
sys.exit(255)
114153

115154
if not (existing_batman := f"/sys/class/net/{args.batman}" in glob("/sys/class/net/*")):
116-
mac = get_interface_locally_administed_mac(interfaces[0])
155+
mac = get_interface_locally_administered_mac(interfaces[0])
117156
create_batman(args.batman, mac)
118157
try:
119158
logger.info(f"Adding {interfaces} to the CBMAController")

modules/sc-mesh-secure-deployment/src/nats/cbma/unittests/test_secure_socket.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import os
2+
import sys
23

34
from OpenSSL import SSL
45

6+
sys.path.insert(0, os.path.normpath(os.path.join(os.path.dirname(__file__), os.pardir)))
7+
58
from secure_socket.secure_socket import FileBasedSecureSocket
69
from models.certificates import CBMACertificates
710

modules/sc-mesh-secure-deployment/src/nats/cbma/utils/networking.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from struct import Struct
22
from ipaddress import IPv6Address
33

4-
from . import logging, common
4+
from . import common
55

66

77
LLA_PREFIX = 'fe80'
88

99
pack_ipv6 = Struct('!16s').pack
10-
logger = logging.get_logger()
1110

1211

1312
def get_interface_mac_address(interface: str) -> str:

0 commit comments

Comments
 (0)