#!/bin/bash # Dumb AP Mode Block set_apmode() { touch /root/.madeap # AP_MODE=$(uci -q get easymesh.config.ap_mode) # if [ "$AP_MODE" = 1 ]; then # Set a flag to indicate that we are in AP mode # When we go to disable easymesh, we will check this flag and if it was set we restore settings uci set easymesh.config.ap_mode_enabled=1 # Backup our configs rm /etc/config/*.meshbak cp /etc/config/wireless /etc/config/wireless.meshbak cp /etc/config/network /etc/config/network.meshbak cp /etc/config/dhcp /etc/config/dhcp.meshbak # Disabling and stopping services not needed for service in firewall dnsmasq odhcpd; do if /etc/init.d/$service enabled; then echo "Disabling and stopping $service..." /etc/init.d/$service disable /etc/init.d/$service stop else echo "$service is not enabled, skipping..." fi done if [ $(uci -q get easymesh.config.ipmode) == "static" ]; then # Set static IP uci set network.lan.proto='static' uci set network.lan.ipaddr=$(uci -q get easymesh.config.ipaddr) uci set network.lan.netmask=$(uci -q get easymesh.config.netmask) uci set network.lan.gateway=$(uci -q get easymesh.config.gatewayIP) uci set network.lan.dns='1.1.1.1' else # Set LAN interface to DHCP client uci del network.lan.ipaddr uci del network.lan.netmask uci set network.lan.proto='dhcp' fi # Delete wan interfaces uci del network.wan uci del network.wan6 # Set firewall disabled uci del firewall.lan.network uci del firewall.wan.network # Just in case, set lan to be ignored by dhcp uci set dhcp.lan.ignore='1' uci del dhcp.wan # Fix this for proper variable name HOSTNAME=$(uci -q get easymesh.config.hostname) # Set netmask and gateway (assuming $netmask and $dns didn't break more stuff) uci set system.@system[0].hostname=$HOSTNAME # Retrieve the list of ports for network.@device[0] LAN_PORTS=$(uci get network.@device[0].ports) # Check if 'wan' is already in the list of ports if echo "$LAN_PORTS" | grep -q -w 'wan'; then echo "'wan' is already in the list of ports for lan." else echo "'wan' is not in the list. Adding it to lan ports..." uci add_list network.@device[0].ports='wan' fi # Get the radio to be used for mesh from the config AP_RADIO=$(uci -q get easymesh.config.apRadio) # Our config set mesh_id that we are looking for MESH_ID=$(uci -q get easymesh.config.mesh_id) # Check if MESH_NAME and AP_RADIO are set, if so find set our network if [ ! -z "$AP_RADIO" ] || [ ! -z "$MESH_ID" ]; then # Get the output from uci show wireless uci_output=$(uci show wireless) # Find the wireless network with the matching ssid and delete it ssid=$(echo "$uci_output" | grep -o "wireless\.wifinet[0-9]*\.ssid='$MESH_ID'") if [ ! -z "$ssid" ]; then # Loop through radios and delete the wireless networks for radio in $AP_RADIO; do # Get the number from the radio name radio_num="${radio#radio}" # Delete the network #uci del wireless.wifinet$radio_num echo "Found wireless network with ssid '$MESH_NAME' on radio$radio_num" done fi fi # # Loop through the selected radios # for CURRENT_RADIO in $AP_RADIO; do # echo "Multiple Radio Setup, Current Radio: $CURRENT_RADIO" # # Extact the radio number from the radio name # wifi_num="${cur_radio#CURRENT_RADIO}" # uci set wireless.wifinet${wifi_num}.network="lan private_router_batman" # done uci commit # Tell openwrt to reload the configs reload_config /etc/init.d/network reload # else # # Set a flag to indicate that we are in AP mode # uci set easymesh.config.ap_mode_enabled=0 # # Restore our configs # [ -f /etc/config/wireless.meshbak ] && { # mv /etc/config/wireless /etc/config/wireless.dumbap # cp /etc/config/wireless.meshbak /etc/config/wireless # } # [ -f /etc/config/network.meshbak ] && { # mv /etc/config/network /etc/config/network.dumbap # cp /etc/config/network.meshbak /etc/config/network # } # [ -f /etc/config/dhcp.meshbak ] && { # mv /etc/config/dhcp /etc/config/dhcp.dumbap # cp /etc/config/dhcp.meshbak /etc/config/dhcp # } # # Enable and start services not needed # for service in firewall dnsmasq odhcpd; do # if /etc/init.d/$service disabled; then # echo "Enabling and starting $service..." # /etc/init.d/$service enable # /etc/init.d/$service start # else # echo "$service is not disabled, skipping..." # fi # done # reload_config # /etc/init.d/network reload # fi } clear_mesh_radio() { # Get the radio to be used for mesh from the config AP_RADIO=$(uci -q get easymesh.config.apRadio) # Get the mesh name from the UCI configuration OLD_MESH_NAME=$(uci -q get easymesh.config.old_mesh_id) # Check if MESH_NAME is not empty if [ -z "$OLD_MESH_NAME" ]; then echo "Old mesh name is not set. None to clear." return; fi # Get the output from uci show wireless uci_output=$(uci show wireless) # Find the mesh network with the matching mesh_id and delete it mesh_id=$(echo "$uci_output" | grep -o "wireless\.mesh_radio[0-9]*\.mesh_id='$OLD_MESH_NAME'") if [ ! -z "$mesh_id" ]; then # Extract the number from the interface name mesh_radio=$(echo "$mesh_id" | grep -o "radio[0-9]*") # Loop through radios and delete the mesh networks for radio in $AP_RADIO; do # Delete the network uci del wireless.mesh_$radio echo "Deleted mesh network with mesh_id '$OLD_MESH_NAME' on $radio" done fi # Find the wireless network with the matching ssid and delete it ssid=$(echo "$uci_output" | grep -o "wireless\.wifinet[0-9]*\.ssid='$OLD_MESH_NAME'") if [ ! -z "$ssid" ]; then # Loop through radios and delete the wireless networks for radio in $AP_RADIO; do # Get the number from the radio name radio_num="${radio#radio}" # Delete the network uci del wireless.wifinet$radio_num echo "Deleted wireless network with ssid '$OLD_MESH_NAME' on radio$radio_num" done fi # Commit changes to make sure the wireless configuration is updated uci commit wireless # Restart wireless to apply changes wifi up echo "Wireless interfaces reloaded." } create_batman_network() { # Check if bat0 already exists if uci -q get network.bat0 >/dev/null; then echo "bat0 interface already exists." else uci set network.bat0=interface uci set network.bat0.proto='batadv' uci set network.bat0.routing_algo='BATMAN_IV' uci set network.bat0.aggregated_ogms='1' uci set network.bat0.ap_isolation='0' uci set network.bat0.bonding='1' uci set network.bat0.bridge_loop_avoidance='1' uci set network.bat0.distributed_arp_table='1' uci set network.bat0.fragmentation='1' uci set network.bat0.hop_penalty='30' uci set network.bat0.isolation_mark='0x00000000/0x00000000' uci set network.bat0.log_level='0' uci set network.bat0.multicast_fanout='16' uci set network.bat0.multicast_mode='1' uci set network.bat0.network_coding='0' uci set network.bat0.orig_interval='1000' echo "bat0 interface has been created." # Set the bat0 role BAT_ROLE=$(uci -q get easymesh.config.role) if [ "${BAT_ROLE}" == "server" ]; then uci set network.bat0.gw_mode='server' elif [ "${BAT_ROLE}" == "client" ]; then uci set network.bat0.gw_mode='client' else uci set network.bat0.gw_mode='off' fi # Get the current list of ports for the 'br-lan' interface LAN_PORTS=$(uci -q get network.@device[0].ports) # Check if 'bat0' is already in the list of ports for 'br-lan' if echo "$LAN_PORTS" | grep -q 'bat0'; then uci del_list network.@device[0].ports='bat0' echo "Old bat0 interface deleted from lan network." fi uci commit network fi # Check if network.private_router_batman already exists if uci -q get network.private_router_batman >/dev/null; then echo "network.private_router_batman interface already exists." else # Get the LAN bridge name LAN_NAME=$(uci -q get network.@device[0].name) # Setup our Private Router Batman Interface uci set network.private_router_batman=interface uci set network.private_router_batman.proto='batadv_hardif' uci set network.private_router_batman.master='bat0' uci set network.private_router_batman.device="$LAN_NAME" uci set network.private_router_batman.mtu='1536' uci commit network fi } process_radios() { # Get the radio to be used for mesh from the config AP_RADIO=$(uci -q get easymesh.config.apRadio) # Check if AP_RADIO is empty, if so exit if [ -z "$AP_RADIO" ]; then echo "No radio specified in the config, exiting." exit 1 fi # Loop through the selected radios for CURRENT_RADIO in $AP_RADIO; do echo "Setting up mesh networks for: $CURRENT_RADIO" setup_mesh_radio $CURRENT_RADIO done } # This is called from the process_radios function and is passed the radio to be used for mesh setup_mesh_radio() { CURRENT_RADIO=$1 echo "Setting up radio: $CURRENT_RADIO" # Get the mesh name from UCI config MESH_NAME=$(uci -q get easymesh.config.mesh_id) # Commit changes to wireless uci commit wireless uci set wireless.mesh_$CURRENT_RADIO='wifi-iface' uci set wireless.mesh_$CURRENT_RADIO.device="$CURRENT_RADIO" uci set wireless.mesh_$CURRENT_RADIO.ifname="mesh_$CURRENT_RADIO" uci set wireless.mesh_$CURRENT_RADIO.network='private_router_batman' uci set wireless.mesh_$CURRENT_RADIO.mode='mesh' uci set wireless.mesh_$CURRENT_RADIO.mesh_id="$MESH_NAME" uci set wireless.mesh_$CURRENT_RADIO.mesh_fwding='0' uci set wireless.mesh_$CURRENT_RADIO.mesh_rssi_threshold='0' uci set wireless.mesh_$CURRENT_RADIO.mesh_ttl='1' uci set wireless.mesh_$CURRENT_RADIO.mcast_rate='24000' uci set wireless.mesh_$CURRENT_RADIO.disabled='0' uci commit wireless # Extact the radio number from the radio name RADIO_NUM="${CURRENT_RADIO#radio}" # Get the mobility domain from the config MOBILITY_DOMAIN=$(uci -q get easymesh.config.mobility_domain) # If mobility domain is empty, set it to a random 4 digit hex number, then set it in the config if [ -z "$MOBILITY_DOMAIN" ]; then MOBILITY_DOMAIN=$(hexdump -n 2 -e '2/1 "%02x"' /dev/urandom) uci set easymesh.config.mobility_domain=$MOBILITY_DOMAIN uci commit easymesh fi # Get the encryption enabled from the config ENCRYPTION_ENABLED=$(uci -q get easymesh.config.encryption) # Setup the interface for the mesh network uci set wireless.mesh_$CURRENT_RADIO.disabled='0' uci set wireless.wifinet$RADIO_NUM=wifi-iface uci set wireless.wifinet$RADIO_NUM.device=$CURRENT_RADIO uci set wireless.wifinet$RADIO_NUM.mode='ap' uci set wireless.wifinet$RADIO_NUM.ssid=$MESH_NAME uci set wireless.wifinet$RADIO_NUM.ieee80211r='1' uci set wireless.wifinet$RADIO_NUM.mobility_domain=$MOBILITY_DOMAIN uci set wireless.wifinet$RADIO_NUM.ft_over_ds='0' uci set wireless.wifinet$RADIO_NUM.ft_psk_generate_local='0' uci set wireless.wifinet$RADIO_NUM.network='lan' uci set wireless.wifinet$RADIO_NUM.disabled=0 # Get the encryption key from the config NETWORK_KEY=$(uci -q get easymesh.config.key) # Check if encryption is enabled and if the key is not empty if [ "$ENCRYPTION_ENABLED" = 1 ] && [ ! -z $NETWORK_KEY ]; then uci set wireless.mesh_$CURRENT_RADIO.encryption='sae' uci set wireless.mesh_$CURRENT_RADIO.key=$NETWORK_KEY uci set wireless.wifinet$RADIO_NUM.encryption='sae-mixed' uci set wireless.wifinet$RADIO_NUM.key=$NETWORK_KEY else uci set wireless.mesh_$CURRENT_RADIO.encryption='none' uci set wireless.wifinet$RADIO_NUM.encryption='none' [ -z $NETWORK_KEY ] && echo "Encryption key is empty, so encryption was disabled." fi # Commit our block of changes to wireless uci commit wireless # Check if we need to setup kvr ENABLE_KVR=$(uci -q get easymesh.config.kvr) if [ "$ENABLE_KVR" = 1 ]; then uci set wireless.wifinet$RADIO_NUM.ieee80211k='1' uci set wireless.wifinet$RADIO_NUM.rrm_neighbor_report='1' uci set wireless.wifinet$RADIO_NUM.rrm_beacon_report='1' uci set wireless.wifinet$RADIO_NUM.ieee80211v='1' uci set wireless.wifinet$RADIO_NUM.bss_transition='1' uci set wireless.wifinet$RADIO_NUM.ieee80211r='1' uci set wireless.wifinet$RADIO_NUM.ft_over_ds='1' uci set wireless.wifinet$RADIO_NUM.ft_psk_generate_local='1' uci commit wireless else uci -q delete wireless.wifinet$RADIO_NUM.ieee80211k uci -q delete wireless.wifinet$RADIO_NUM.ieee80211v uci -q delete wireless.wifinet$RADIO_NUM.ieee80211r uci commit wireless fi # If KVR is enabled, setup DAWN if [ "$ENABLE_KVR" = 1 ]; then RSSI=$(uci -q get easymesh.config.rssi_val) LOW_RSSI=$(uci -q get easymesh.config.low_rssi_val) uci set dawn.@metric[0].rssi_val=$RSSI uci set dawn.@metric[0].low_rssi_val=$LOW_RSSI uci commit dawn /etc/init.d/dawn enable && /etc/init.d/dawn start else /etc/init.d/dawn stop && /etc/init.d/dawn disable fi # Commit our block of changes to the configs uci commit } restart_and_reload() { # Get the radio to be used for mesh from the config AP_RADIO=$(uci -q get easymesh.config.apRadio) # Check if AP_RADIO is empty, if so exit if [ -z "$AP_RADIO" ]; then echo "No radio specified in the config, exiting." exit 1 fi # Enable radios for radio in $AP_RADIO; do echo "Enabling $radio..." uci set wireless.$radio.disabled=0 done uci commit wireless # Reload wifi to apply changes without restarting all interfaces wifi up echo "Wireless interfaces reloaded." # Apply network configuration changes reload_config echo "Network configuration reloaded." /etc/init.d/network reload } disable_batman_interfaces() { # Delete the bat0 interface if [ "$(uci -q get network.bat0)" = "interface" ]; then uci del network.bat0 fi # Delete the private_router_batman network interface if [ "$(uci -q get network.private_router_batman)" = "interface" ]; then uci del network.private_router_batman fi uci commit network reload_config /etc/init.d/network reload echo "Network configuration reloaded." } # Enable easymesh enable_easymesh() { # Clear old radios then set "old values" clear_mesh_radio uci set easymesh.config.old_mesh_id="$(uci -q get easymesh.config.mesh_id)" create_batman_network process_radios restart_and_reload # Set at end to be sure it worked uci set easymesh.config.running=1 } # Disable easymesh disable_easymesh() { # Clear old radios then clear "old values" clear_mesh_radio uci del easymesh.config.old_mesh_id disable_batman_interfaces # Set at end to be sure it worked uci del easymesh.config.running } sleep 5 if [ "$1" = "enable" ]; then enable_easymesh exit 0 elif [ "$1" = "disable" ]; then disable_easymesh exit 0 elif [ "$1" = "dumbap" ]; then set_apmode exit 0 else echo "Invalid argument. Please use 'start', 'stop' or 'dumbap'" exit 1 fi