#!/bin/bash # ---- Configuration ---- source /opt/scripts/mesh/freq.sh source /opt/scripts/mesh/node_addresses.sh PHYNAME=$1 DEVNAME=$2 CHANNEL=${chan[$3]} POWER=$4 NOISEFLOOR=$5 phy_number="${1//[!0-9]/}" dev_number="${2//[!0-9]/}" devPath="/sys/class/net/" phyPath="/sys/kernel/debug/ieee80211/" SSID="adhocnet" BSSID="02:11:87:3A:4C:55" # ---- Host validation ---- nodename="$(uname -n)" host_valid=0 ALLNODES=${neighbors["all"]} while [[ host_valid -eq 0 ]]; do for i in $ALLNODES; do if [[ $nodename == $i ]]; then host_valid=1 echo "Hostname $nodename is valid" break fi done if [[ host_valid -eq 0 ]]; then sleep 1 nodename="$(uname -n)" fi done echo "Configuring $nodename $DEVNAME on $PHYNAME" # ---- Kernel/Network tuning ---- sysctl -w net.core.rmem_max=8388608 sysctl -w net.core.wmem_max=8388608 sysctl -w net.core.rmem_default=8388608 sysctl -w net.core.wmem_default=8388608 sysctl -w net.core.optmem_max=8388608 sysctl -w net.ipv4.tcp_mem='8388608 8388608 8388608' sysctl -w net.ipv4.tcp_rmem='4096 87380 8388608' sysctl -w net.ipv4.tcp_wmem='4096 65536 8388608' sysctl -w net.ipv4.udp_mem='8388608 8388608 8388608' sysctl -w net.ipv4.udp_rmem_min=4096 sysctl -w net.ipv4.udp_wmem_min=4096 sysctl -w net.core.netdev_max_backlog=5000 sysctl -w net.ipv4.tcp_max_syn_backlog=5000 sysctl -w net.ipv4.tcp_syn_retries=9 sysctl -w net.ipv4.tcp_synack_retries=9 sysctl -w net.ipv4.tcp_no_metrics_save=1 sysctl -w net.ipv4.route.flush=1 echo 0 > /sys/module/tcp_cubic/parameters/hystart echo 0 > /sys/module/tcp_cubic/parameters/hystart_detect sysctl -w net.ipv4.neigh.default.base_reachable_time_ms=1200000 sysctl -w net.ipv4.neigh.default.mcast_solicit=25 sysctl -w net.ipv4.neigh.default.ucast_solicit=25 # ---- Check/clean interface ---- if [ ! -e "$phyPath$PHYNAME" ]; then echo "$PHYNAME not found in $phyPath, aborting." exit 1 fi systemctl stop systemd-timesyncd.service if [ -e "$devPath$DEVNAME" ]; then number=$(iw dev "$DEVNAME" info | awk '$1 == "wiphy" {print $2}') echo "Removing existing $DEVNAME from phy$number" ip link set $DEVNAME down iw dev $DEVNAME del fi # Delete existing interfaces on this phy current_dev_on_phy=$(iw dev | awk -v phy="$phy_number" '$0 ~ "phy#"phy {getline; while ($1 ~ /^Interface/) {print $2; getline}}') if [ ! -z "$current_dev_on_phy" ]; then echo "Removing $current_dev_on_phy from phy$phy_number" ip link set $current_dev_on_phy down iw dev $current_dev_on_phy del fi # ---- Create IBSS interface ---- iw phy ${PHYNAME} interface add ${DEVNAME} type ibss ip link set ${DEVNAME} down ip link set ${DEVNAME} up iw dev ${DEVNAME} ibss join $SSID ${CHANNEL} HT40+ fixed-freq $BSSID # ---- Set tx power and bitrate ---- iw dev ${DEVNAME} set txpower fixed $POWER if [[ $3 -lt 15 ]]; then iw dev ${DEVNAME} set bitrates ht-mcs-2.4 6 sgi-2.4 else iw dev ${DEVNAME} set bitrates ht-mcs-5 6 sgi-5 fi # ---- Set IP address ---- ip addr flush dev ${DEVNAME} if [[ "$dev_number" -eq 0 ]]; then NODE_MESH_IP=${phy0IP["$nodename"]} BROADCAST_IP=${phy0IP["broadcast"]} else NODE_MESH_IP=${phy1IP["$nodename"]} BROADCAST_IP=${phy1IP["broadcast"]} fi echo "Assigning IP: $NODE_MESH_IP" ip addr add ${NODE_MESH_IP}/24 broadcast ${BROADCAST_IP} dev ${DEVNAME} # ---- Done ---- echo "$DEVNAME is now in IBSS mode with IP $NODE_MESH_IP"