netmode: deprecate config based mode switching

This commit is contained in:
Sukru Senli 2020-04-23 09:59:21 +02:00
parent de03c6149c
commit df93ed2d3c
26 changed files with 234 additions and 1804 deletions

View file

@ -1,9 +1,8 @@
config mode 'setup'
config netmoded 'netmoded'
option enabled '0'
config setup 'setup'
option enabled '0'
option dir '/etc/netmodes'
option detail ''
option curmode 'routed'
config tools 'tools'
option wificontrol 0
option arp_discovery 1
# option mode 'router'

View file

@ -1,50 +0,0 @@
#!/bin/sh
[ "$INTERFACE" != "wan" ] && exit
automode="$(uci -q get netmode.setup.automode)"
[ "$automode" == "0" ] && exit
. /lib/functions.sh
. /lib/functions/network.sh
. /lib/functions/netmode.sh
network_flush_cache
network_get_ipaddr ipaddr $INTERFACE
[ -z "$ipaddr" ] && exit
repeaterready="$(uci -q get netmode.setup.repeaterready)"
curmode="$(uci -q get netmode.setup.curmode)"
if [ "$(netmode_get_ip_type $ipaddr)" == "private" ]; then
if [ "$repeaterready" == "1" -o "${curmode:0:8}" == "repeater" ]; then
# flush the ip on br-lan; br-lan will anyhow be deleted.
# this is needed if the ip received on the wan is also from the same net
logger -s -p user.info -t $0: [netmode] ip addr flush dev br-lan >/dev/console
ip addr flush dev br-lan 2>/dev/null
# flush iptables in case the MASTER
# tries to configure us before
# firewall is disabled the proper way
logger -s -p user.info -t $0: [netmode] iptables -F >/dev/console
iptables -F
disable_firewall 1
fi
else
disable_firewall 0
fi
case "$curmode" in
repeater*)
;;
*)
[ "$repeaterready" == "1" ] || return
if [ "$(netmode_get_ip_type $ipaddr)" == "private" ]; then
wificontrol_takes_over || switch_netmode repeater
else
switch_netmode routed
fi
;;
esac

View file

@ -1,27 +0,0 @@
#!/bin/sh
. /lib/functions.sh
. /lib/functions/netmode.sh
ps | grep hotplug | grep button && exit
[ -z "$INTERFACE" ] && exit
defroute=$(ip route | grep default | awk '{print$3}')
case "$(uci get netmode.setup.curmode)" in
repeater*)
;;
*)
ping -c 1 -w 5 $defroute >/dev/null 2>&1 || {
logger -s -t "80-repeater" "ping to $defroute failed, sending USR1 to udhcpc" >/dev/console
killall -USR1 udhcpc
}
exit
;;
esac
case "$ACTION" in
add|register|remove|unregister) correct_uplink $INTERFACE;;
esac

View file

@ -24,18 +24,12 @@ start_service() {
populate_netmodes
start_netmoded
start_netmode_tools
}
reload_service() {
switch_netmode
start_netmoded
start_netmode_tools
}
stop_service() {
stop_netmode_tools
}
service_triggers()

View file

@ -0,0 +1,9 @@
{
"description": [
{ "en" : "Bridge Mode" }
],
"explanation": [
{ "en" : "NAT is disabled." }
],
"reboot" : 0
}

View file

@ -0,0 +1,92 @@
#!/bin/sh
. /usr/share/libubox/jshn.sh
. /lib/functions.sh
BRIDGEPORTS=""
get_vlan() {
local device="$1"
for section in $(uci show network | grep "=device" | cut -d '=' -f1); do
if [ "$(uci -q get $section.ifname)" == "$device" ]; then
uci -q get $section.name
break
fi
done
}
add_wifi_devs()
{
add_wdev()
{
local cfg=$1
# local disabled
# config_get_bool disabled $cfg disabled 0
# config_get_bool ifname $cfg ifname
# if [ $disabled -eq 0 -a -n "$ifname" ]; then
# BRIDGEPORTS="$ifname"
# fi
uci -q set wireless.$cfg.network="wan"
}
config_load wireless
config_foreach add_wdev "wifi-iface"
uci -q commit wireless
}
add_xtm_devs() {
local section device vlan
for section in $(uci show dsl | grep "=.*tm-device" | cut -d'=' -f1); do
if [ -n "$(uci -q get $section.device)" ]; then
vlan="$(get_vlan $device)"
[ -n "$vlan" ] && device="$vlan"
if [ -n "$device" ]; then
BRIDGEPORTS="$BRIDGEPORTS $device"
fi
fi
done
}
add_eth_ports() {
add_port()
{
local cfg=$1
local ifname vlan
config_get ifname $cfg ifname
vlan="$(get_vlan $ifname)"
[ -n "$vlan" ] && ifname="$vlan"
if [ -n "$ifname" ]; then
BRIDGEPORTS="$BRIDGEPORTS $ifname"
fi
}
config_load ports
config_foreach add_port "ethport"
}
add_xtm_devs
add_eth_ports
add_wifi_devs
BRIDGEPORTS="$(echo $BRIDGEPORTS | sed -e 's/[[:space:]]*$//')"
uci -q set network.wan.type="bridge"
uci -q set network.wan.ifname="$BRIDGEPORTS"
uci -q set network.wan6.ifname="@wan"
uci -q delete network.lan.ifname
ubus call uci commit '{"config":"network"}'

View file

@ -1,19 +0,0 @@
{
"description": [
{ "en" : "Repeater 5▴▾2.4&5" },
{ "sv" : "Repeater 5▴▾2.4&5" }
],
"explanation": [
{ "en" : "Your router is going to act as a wired extender or wireless repeater to the selected Wireless Access Point. Uplink 5G & Downlink 2.4G + 5G" },
{ "sv" : "Routern kommer att fungera som en trådbunden extender eller trådlös repeater till den valda trådlösa åtkomstpunkten. Uplink 5G & Downlink 2.4G + 5G" }
],
"excluded_boards" : [
"*G*",
"F*",
"NORRLAND"
],
"uplink_band" : 'a',
"downlink_band" : 'a b',
"credentials" : 1,
"reboot" : 0
}

View file

@ -1,47 +0,0 @@
config interface 'loopback'
option ifname 'lo'
option proto 'static'
option ipaddr '127.0.0.1'
option netmask '255.0.0.0'
config device 'lan_dev'
option name 'eth0.1'
# Unused?
#option macaddr '$MACLAN'
option vid 1
config interface 'wan'
option type 'bridge'
option ifname 'eth0.2 eth0.1'
option proto 'dhcp'
#option hostname AUTO-CREATED AT FIRST BOOT
option reqopts '66 67 128 224'
option igmp_snooping '0'
# Unused?
#option macaddr '$MACWAN'
config device 'wan_dev'
option name 'eth0.2'
# Unused?
#option macaddr '$MACWAN'
option vid 2
config interface 'wan6'
option ifname '@wan'
option proto 'dhcpv6'
config switch
option name 'switch0'
option reset '1'
option enable_vlan '1'
config switch_vlan
option device 'switch0'
option vlan '1'
option ports '1 6t'
config switch_vlan
option device 'switch0'
option vlan '2'
option ports '0 6t'

View file

@ -1,38 +0,0 @@
#if wet in wireless config and SSID nad KEY was saved for it, apply to config
#!/bin/sh
. /usr/share/libubox/jshn.sh
. /lib/functions.sh
parse_wet_json()
{
local interface=$1
local mode key encryption ssid connect_bssid
config_get mode $interface mode
[ "$mode" = "wet" ] || return
json_load "$(cat /tmp/netmodecfg)" 2> /dev/null
json_get_var ssid ssid
json_get_var key key
json_get_var encryption encryption
json_get_var connect_bssid connect_bssid
uci -q set wireless.$1.key="$key"
uci -q set wireless.$1.encryption="$encryption"
uci -q set wireless.$1.ssid="$ssid"
uci -q set wireless.$1.connect_bssid="$connect_bssid"
uci commit wireless
}
apply_wet_cfg()
{
[ -f /tmp/netmodecfg ] || return
config_load wireless
config_foreach parse_wet_json "wifi-iface"
rm /tmp/netmodecfg 2> /dev/null
}
apply_wet_cfg

View file

@ -1,2 +0,0 @@
uci -q set owsd.ubusproxy.enable="0"
uci -q commit owsd

View file

@ -1,38 +0,0 @@
#if mode wet in wireless config has SSID Key, save them
#!/bin/sh
. /usr/share/libubox/jshn.sh
. /lib/functions.sh
parse_wet_cfg()
{
local interface=$1
local mode key encryption ssid connect_bssid
config_get mode $interface mode
[ "$mode" = "wet" ] || return
config_get ssid $interface ssid
config_get key $interface key
config_get encryption $interface encryption
config_get connect_bssid $interface connect_bssid
[ -n "$key" ] || return
[ -n "$encryption" ] || return
json_init
json_add_string "ssid" "$ssid"
json_add_string "key" "$key"
json_add_string "encryption" "$encryption"
json_add_string "connect_bssid" "$connect_bssid"
echo "`json_dump`" > /tmp/netmodecfg
}
save_wet_cfg()
{
config_load wireless
config_foreach parse_wet_cfg "wifi-iface"
}
save_wet_cfg

View file

@ -1,55 +0,0 @@
config wifi-status 'status'
option wlan '1'
option sched_status '0'
option schedule '0'
config bandsteering 'bandsteering'
option enabled '0'
option policy '0'
config wifi-device ra0
option band b
option channel auto
option bandwidth '20'
option hwmode auto
option htmode HT20
option country 'DE'
config wifi-iface
option device ra0
option network wan
option mode ap
#option ssid AUTO-CREATED AT FIRST BOOT
option encryption psk2
#option key AUTO-CREATED AT FIRST BOOT
option ifname ra0
option wps 1
config wifi-device rai0
option band a
option channel auto
option bandwidth '80'
option hwmode 11ac
option htmode VHT80
option country 'DE'
option beamforming 1
config wifi-iface
option device rai0
option network wan
option mode ap
#option ssid AUTO-CREATED AT FIRST BOOT
option encryption psk2
#option key AUTO-CREATED AT FIRST BOOT
option ifname rai0
option wps 1
config wifi-iface
option device rai0
option network wan
option mode wet
#option ssid AUTO-CREATED AT FIRST BOOT
option encryption psk2
#option key AUTO-CREATED AT FIRST BOOT
option ifname apclii0

View file

@ -1,18 +0,0 @@
{
"description": [
{ "en" : "Fully Routed (NAT)" },
{ "sv" : "Fullt Omdirigerad (NAT)" }
],
"explanation": [
{ "en" : "" },
{ "sv" : "" }
],
"excluded_boards" : [
"*G*",
"F*",
"NORRLAND"
],
"reboot" : 0,
"credentials" : 0
}

View file

@ -1,22 +0,0 @@
config dnsmasq
option domainneeded 1
option boguspriv 1
option filterwin2k 0
option localise_queries 1
option rebind_protection 0
option rebind_localhost 1
option local '/lan/'
option domain 'lan'
option expandhosts 1
option nonegcache 0
option authoritative 1
option readethers 1
option leasefile '/tmp/dhcp.leases'
option resolvfile '/tmp/resolv.conf.auto'
config dhcp lan
option interface lan
option start 100
option limit 150
option leasetime 12h

View file

@ -1,153 +0,0 @@
config settings 'settings'
option disabled '0'
config defaults
option syn_flood 1
option input ACCEPT
option output ACCEPT
option forward REJECT
# Uncomment this line to disable ipv6 rules
# option disable_ipv6 1
config zone
option name lan
list network 'lan'
option input ACCEPT
option output ACCEPT
option forward ACCEPT
config zone
option name wan
list network 'wan'
list network 'wan6'
option input REJECT
option output ACCEPT
option forward REJECT
option masq 1
option mtu_fix 1
config forwarding
option src lan
option dest wan
# We need to accept udp packets on port 68,
# see https://dev.openwrt.org/ticket/4108
config rule
option name Allow-DHCP-Renew
option src wan
option proto udp
option dest_port 68
option target ACCEPT
option family ipv4
# Allow IPv4 ping
config rule
option name Allow-Ping
option src wan
option proto icmp
option icmp_type echo-request
option family ipv4
option target ACCEPT
config rule
option name Allow-IGMP
option src wan
option proto igmp
option family ipv4
option target ACCEPT
# Allow DHCPv6 replies
# see https://dev.openwrt.org/ticket/10381
config rule
option name Allow-DHCPv6
option src wan
option proto udp
option src_ip fe80::/10
option src_port 547
option dest_ip fe80::/10
option dest_port 546
option family ipv6
option target ACCEPT
config rule
option name Allow-MLD
option src wan
option proto icmp
option src_ip fe80::/10
list icmp_type '130/0'
list icmp_type '131/0'
list icmp_type '132/0'
list icmp_type '143/0'
option family ipv6
option target ACCEPT
# Allow essential incoming IPv6 ICMP traffic
config rule
option name Allow-ICMPv6-Input
option src wan
option proto icmp
list icmp_type echo-request
list icmp_type echo-reply
list icmp_type destination-unreachable
list icmp_type packet-too-big
list icmp_type time-exceeded
list icmp_type bad-header
list icmp_type unknown-header-type
list icmp_type router-solicitation
list icmp_type neighbour-solicitation
list icmp_type router-advertisement
list icmp_type neighbour-advertisement
option limit 1000/sec
option family ipv6
option target ACCEPT
# Allow essential forwarded IPv6 ICMP traffic
config rule
option name Allow-ICMPv6-Forward
option src wan
option dest *
option proto icmp
list icmp_type echo-request
list icmp_type echo-reply
list icmp_type destination-unreachable
list icmp_type packet-too-big
list icmp_type time-exceeded
list icmp_type bad-header
list icmp_type unknown-header-type
option limit 1000/sec
option family ipv6
option target ACCEPT
# allow IPsec/ESP and ISAKMP passthrough
config rule
option src wan
option dest lan
option proto esp
option target ACCEPT
config rule
option src wan
option dest lan
option dest_port 500
option proto udp
option target ACCEPT
config dmz dmz
option enabled '0'
option exclude_ports '5060 7547'
# include a file with users custom iptables rules
config include
option path /etc/firewall.user
option reload 1
# include a file with rules for prioritizing some specific packets
config include
option path /etc/firewall.qos
option reload 1
# include a file with rules for DMZ Host
config include dmzhost
option path /etc/firewall.dmz
option reload 1

View file

@ -1,53 +0,0 @@
config interface 'loopback'
option ifname 'lo'
option proto 'static'
option ipaddr '127.0.0.1'
option netmask '255.0.0.0'
config interface 'lan'
option type 'bridge'
option proto 'static'
option is_lan '1'
option ipaddr '192.168.1.1'
option netmask '255.255.255.0'
option ip6assign '60'
option ifname 'eth0.1'
config device 'lan_dev'
option name 'eth0.1'
# Unused?
#option macaddr '$MACLAN'
option vid 1
config interface 'wan'
option ifname 'eth0.2'
option proto 'dhcp'
#option hostname AUTO-CREATED AT FIRST BOOT
#option vendorid AUTO-CREATED AT FIRST BOOT
option reqopts '66 67 128 224'
config device 'wan_dev'
option name 'eth0.2'
# Unused?
#option macaddr '$MACWAN'
option vid 2
config interface 'wan6'
option ifname '@wan'
option proto 'dhcpv6'
config switch
option name 'switch0'
option reset '1'
option enable_vlan '1'
config switch_vlan
option device 'switch0'
option vlan '1'
option ports '1 6t'
config switch_vlan
option device 'switch0'
option vlan '2'
option ports '0 6t'

View file

@ -1,48 +0,0 @@
config wifi-status 'status'
option wlan '1'
option sched_status '0'
option schedule '0'
config bandsteering 'bandsteering'
option enabled '0'
option policy '0'
config wifi-device ra0
option band b
option channel auto
option bandwidth '20'
option hwmode auto
option htmode HT20
option country 'DE'
option scantimer '60'
config wifi-iface
option device ra0
option network lan
option mode ap
option ssid iopsys-$BSSID4
option encryption psk2
option key $WPAKEY
option wps 1
option ifname ra0
config wifi-device rai0
option band a
option channel auto
option bandwidth '80'
option hwmode 11ac
option htmode VHT80
option country 'DE'
option beamforming 1
option scantimer '60'
config wifi-iface
option device rai0
option network lan
option mode ap
option ssid iopsys-$BSSID4
option encryption psk2
option key $WPAKEY
option wps 1
option ifname rai0

View file

@ -0,0 +1,9 @@
{
"description": [
{ "en" : "Router Mode" }
],
"explanation": [
{ "en" : "NAT is enabled." }
],
"reboot" : 0
}

View file

@ -0,0 +1,94 @@
#!/bin/sh
. /usr/share/libubox/jshn.sh
. /lib/functions.sh
WANPORTS=""
LANPORTS=""
get_vlan() {
local device="$1"
for section in $(uci show network | grep "=device" | cut -d '=' -f1); do
if [ "$(uci -q get $section.ifname)" == "$device" ]; then
uci -q get $section.name
break
fi
done
}
add_wifi_devs()
{
add_wdev()
{
local cfg=$1
config_get mode $cfg mode "ap"
if [ "$mode" == "ap" ]; then
uci -q set wireless.$cfg.network="lan"
fi
}
config_load wireless
config_foreach add_wdev "wifi-iface"
uci -q commit wireless
}
add_xtm_devs() {
local section device vlan
for section in $(uci show dsl | grep "=.*tm-device" | cut -d'=' -f1); do
device="$(uci -q get $section.device)"
if [ -n "$device" ]; then
vlan="$(get_vlan $device)"
[ -n "$vlan" ] && device="$vlan"
if [ -n "$device" ]; then
WANPORTS="$WANPORTS $device"
fi
fi
done
}
add_eth_ports() {
add_port()
{
local cfg=$1
local uplink ifname vlan
config_get ifname $cfg ifname
config_get_bool uplink $cfg uplink 0
vlan="$(get_vlan $ifname)"
[ -n "$vlan" ] && ifname="$vlan"
if [ $uplink -eq 1 ]; then
WANPORTS="$WANPORTS $ifname"
else
LANPORTS="$LANPORTS $ifname"
fi
}
config_load ports
config_foreach add_port "ethport"
}
add_xtm_devs
add_eth_ports
add_wifi_devs
WANPORTS="$(echo $WANPORTS | sed -e 's/[[:space:]]*$//')"
LANPORTS="$(echo $LANPORTS | sed -e 's/[[:space:]]*$//')"
uci -q set network.wan.type="anywan"
uci -q set network.wan.ifname="$WANPORTS"
uci -q set network.wan6.ifname="@wan"
uci -q set network.lan.type="bridge"
uci -q set network.lan.ifname="$LANPORTS"
ubus call uci commit '{"config":"network"}'

View file

@ -3,120 +3,10 @@
. /lib/functions.sh
. /usr/share/libubox/jshn.sh
NMTMPDIR=/var/netmodes
OLD_MODE_FILE=/var/netmodes/old_mode
CONF_BACKUP_DIR=/var/netmodes/backup/
SWITCHMODELOCK="/tmp/switching_mode"
MODEDIR=$(uci -q get netmode.setup.dir)
MTK=0
opkg list-installed | grep -q kmod-mt.*mtk && MTK=1
[ -n "$MODEDIR" ] || MODEDIR="/etc/netmodes"
toggle_firewall() {
local section=$1
local disable=$2
config_get name "$1" name
if [ "$name" == "wan" ]; then
uci -q set firewall.settings.disabled=$disable
if [ "$disable" == "1" ]; then
uci -q set firewall.$section.input="ACCEPT"
else
uci -q set firewall.$section.input="REJECT"
fi
uci -q commit firewall
fi
}
disable_firewall() {
config_load firewall
config_foreach toggle_firewall zone $1
/etc/init.d/firewall reload
}
is_known_macaddr()
{
macaddr=$1
echo $macaddr | grep -i -e "^00:22:07" \
-e "^02:22:07" \
-e "^44:D4:37" \
-e "^00:0C:07" \
-e "^02:0C:07" \
-e "^06:0C:07" \
-e "^00:0C:43" \
-e "^02:0C:43" \
-e "^06:0C:43" \
&& return
false
}
get_wifi_wet_interface() {
local ifname=""
handle_interface() {
[ -n "$ifname" ] && return
config_get mode "$1" mode
if [ "$mode" == "sta" -o "$mode" == "wet" ]; then
config_get ifname "$1" ifname
fi
}
config_load wireless
config_foreach handle_interface wifi-iface
echo "$ifname"
}
get_wifi_iface_cfgstr() {
get_cfgno() {
config_get ifname "$1" ifname
[ "$ifname" == "$2" ] && echo "wireless.$1"
}
config_load wireless
config_foreach get_cfgno wifi-iface $1
}
correct_uplink() {
local IFACE="$1"
local WANDEV="$(db -q get hw.board.ethernetWanPort)"
local WETIF="$(get_wifi_wet_interface)"
local link wetcfg wetnet wetmac
[ $MTK -eq 1 ] || WANDEV="$WANDEV.1"
[ -n "$IFACE" -a "$IFACE" != "$WANDEV" -a "$IFACE" != "$WETIF" ] && return
link=$(cat /sys/class/net/${WANDEV:0:4}/operstate)
[ $MTK -eq 1 ] && link=$(swconfig dev switch0 port 0 get link | awk '{print$2}' | cut -d':' -f2)
if [ ! -f /tmp/netmodes/uplink-macaddr-corrected ]; then
wetcfg="$(get_wifi_iface_cfgstr $WETIF)"
wetnet="$(uci -q get $wetcfg.network)"
wetmac="$(ifconfig $WETIF | grep HWaddr | awk '{print$NF}')"
ethwanmac="$(ifconfig $WANDEV | grep HWaddr | awk '{print$NF}')"
if [ -d /sys/class/net/br-$wetnet ]; then
if [ "$link" == "up" ]; then
ifconfig br-$wetnet hw ether $ethwanmac
else
ifconfig br-$wetnet hw ether $wetmac
fi
#touch -f /tmp/netmodes/uplink-macaddr-corrected
fi
fi
if [ "$link" == "up" ]; then
ubus call network.device set_state "{\"name\":\"$WETIF\", \"defer\":true}"
ifconfig $WETIF down
echo ethernet > /tmp/netmodes/uplink_type
ubus call network.device set_state "{\"name\":\"$WANDEV\", \"defer\":false}"
else
ubus call network.device set_state "{\"name\":\"$WETIF\", \"defer\":false}"
ifconfig $WETIF up
echo wireless > /tmp/netmodes/uplink_type
ubus call network.device set_state "{\"name\":\"$WANDEV\", \"defer\":true}"
ubus call led.internet set '{"state" : "notice"}'
fi
}
run_netmode_scripts() {
local mode=$1
local when=$2
@ -135,220 +25,42 @@ run_netmode_scripts() {
}
switch_netmode() {
local newmode="$1"
[ -f /etc/config/netmode -a -d $MODEDIR ] || return
[ -n "$newmode" ] && uci -q set netmode.setup.curmode="$newmode"
local curmode conf old_mode
# NETMODE CONFIG #
config_load netmode
config_get curmode setup curmode
uci -q set netmode.setup.repeaterready="0"
# set default JUCI page to overview
uci -q set juci.juci.homepage="overview"
uci commit juci
local enabled
config_get_bool enabled setup enabled '0'
[ $enabled -eq 0 ] && return
if [ "$curmode" == "repeater" ]; then
if [ $MTK -eq 1 ]; then
curmode="repeater_mtk_5g_up_dual_down"
else
curmode="repeater_brcm_2g_up_dual_down"
fi
uci set netmode.setup.curmode="$curmode"
fi
if [ "$curmode" == "routed" ]; then
if [ $MTK -eq 1 ]; then
curmode="routed_mtk"
else
curmode="routed_brcm"
fi
uci set netmode.setup.curmode="$curmode"
fi
uci commit netmode
# end of NETMODE CONFIG #
local mode
config_get mode setup mode
old_mode="$(cat $OLD_MODE_FILE 2>/dev/null)"
[ -d "/etc/netmodes/$mode" ] || return
# if curmode has not changed do not copy configs
if [ "$curmode" == "$old_mode" ]; then
/etc/init.d/environment reload
return
fi
logger -s -p user.info -t "netmode" "Switching to $mode Mode" >/dev/console
echo $curmode >$OLD_MODE_FILE
run_netmode_scripts $mode "pre"
[ -d "/etc/netmodes/$curmode" ] || return
local reboot=$(uci -q get netmode.$mode.reboot)
run_netmode_scripts $curmode "pre"
# make backup of current config before switching
rm -rf $CONF_BACKUP_DIR
mkdir -p $CONF_BACKUP_DIR
cp -af /etc/config/* $CONF_BACKUP_DIR
logger -s -p user.info -t "netmode" "Copying /etc/netmodes/$curmode in /etc/config" >/dev/console
for file in $(ls /etc/netmodes/$curmode/); do
case "$file" in
DETAILS|scripts) continue ;;
esac
cp /etc/netmodes/$curmode/$file /etc/config/
done
sync
#commit owsd to reload all init.d scripts dependent on its config
ubus call uci commit '{"config":"owsd"}'
local reboot=$(uci -q get netmode.$curmode.reboot)
if [ "$reboot" != "0" ]; then
run_netmode_scripts $curmode "post"
if [ "$reboot" == "1" ]; then
#run_netmode_scripts $mode "post"
reboot &
exit
fi
/etc/init.d/environment reload
case "$curmode" in
repeater*)
touch $SWITCHMODELOCK
logger -s -p user.info -t "netmode" "Switching to $curmode mode" > /dev/console
ubus call leds set '{"state" : "allflash"}'
[ -f /etc/init.d/layer2 ] && /etc/init.d/layer2 reload
[ -f /etc/init.d/macoffset ] && /etc/init.d/macoffset reload
ubus call network reload
wifi reload nodat
ubus call router.network reload
rm -f /tmp/netmodes/uplink-macaddr-corrected
correct_uplink
ubus call leds set '{"state" : "normal"}'
rm -f $SWITCHMODELOCK
;;
*)
[ -f /etc/init.d/layer2 ] && /etc/init.d/layer2 reload
[ -f /etc/init.d/macoffset ] && /etc/init.d/macoffset reload
ubus call uci commit '{"config":"network"}'
;;
esac
run_netmode_scripts $curmode "post"
}
revert_netmode() {
local from="$1"
local to="$2"
local rready="$3"
ubus call leds set '{"state" : "allflash"}'
logger -s -p user.info -t "netmode" "Could not switch to '$from' mode; going back to '$to' mode" > /dev/console
cp -af $CONF_BACKUP_DIR/* /etc/config/
sync
uci -q set netmode.setup.curmode="$to"
uci -q set netmode.setup.repeaterready="$rready"
uci commit netmode
rm -rf $CONF_BACKUP_DIR
rm -rf $OLD_MODE_FILE
logger -s -p user.info -t "netmode" "Restarting network services" > /dev/console
/etc/init.d/network restart
ubus call router.network reload
ubus call leds set '{"state" : "normal"}'
}
wificontrol_takes_over() {
local ret
[ -f /sbin/wificontrol ] || return
ubus call leds set '{"state" : "allflash"}'
if pidof wificontrol >/dev/null; then
ret=0
# let netmode-conf up to 20 seconds before switching mode
for tm in 2 4 6 8; do
if [ -f /tmp/wificontrol.txt ]; then
ret=1
break
fi
sleep $tm
done
# let netmode-conf take over
[ $ret -eq 1 ] && return 0
fi
return 1
}
wait_for_netmode_handler() {
for tm in 2 4 6 8; do
if [ ! -f $SWITCHMODELOCK ]; then
break
fi
sleep $tm
done
}
netmode_get_ip_type() {
[ -n "$(echo $1 | grep -E '^(192\.168|10\.|172\.1[6789]\.|172\.2[0-9]\.|172\.3[01]\.)')" ] && {
logger -t $0 "netmode_get_ip_type: ip $1 is private"
echo "private"
} || {
logger -t $0 "netmode_get_ip_type: ip $1 is public"
echo "public"
}
}
get_device_of() {
local PORT_NAMES=$(db get hw.board.ethernetPortNames)
local PORT_ORDER=$(db get hw.board.ethernetPortOrder)
local cnt=1
local idx=0
local pnum=$(echo $PORT_NAMES | wc -w)
if [ $pnum -le 2 ]; then
PORT_NAMES=$(echo $PORT_NAMES | sed 's/LAN/LAN1/g')
fi
# get index of interface name
for i in $PORT_NAMES; do
if [ "$i" == "$1" ]; then
idx=$cnt
fi
cnt=$((cnt+1))
done
# get port name from index
cnt=1
for i in $PORT_ORDER; do
if [ "$cnt" == "$idx" ]; then
echo $i
fi
cnt=$((cnt+1))
done
#run_netmode_scripts $mode "post"
}
populate_netmodes() {
[ -f /etc/config/netmode -a -d $MODEDIR ] || return
local curmode
config_load netmode
config_get curmode setup curmode
mkdir -p $NMTMPDIR
if [ "$curmode" == "routed" ]; then
if [ $MTK -eq 1 ]; then
curmode="routed_mtk"
else
curmode="routed_brcm"
fi
fi
echo $curmode > $OLD_MODE_FILE
local enabled
config_get_bool enabled setup enabled '0'
[ $enabled -eq 0 ] && return
delete_netmode() {
uci delete netmode.$1
@ -357,41 +69,9 @@ populate_netmodes() {
config_foreach delete_netmode netmode
uci commit netmode
wan=$(get_device_of WAN)
lan1=$(get_device_of LAN1)
lan2=$(get_device_of LAN2)
lan3=$(get_device_of LAN3)
lan4=$(get_device_of LAN4)
lan5=$(get_device_of LAN5)
for file in $(find $MODEDIR -type f); do
conf="$(echo $file | cut -d'/' -f5)"
if [ "$conf" == "layer2_interface_ethernet" ]; then
grep -q "\$WAN" $file && sed -i "s/\$WAN/$wan/g" $file
fi
if [ "$conf" == "network" ]; then
grep -q "\$WAN" $file && sed -i "s/\$WAN/$wan/g" $file
grep -q "\$LAN1" $file && sed -i "s/\$LAN1/$lan1/g" $file
grep -q "\$LAN2" $file && sed -i "s/\$LAN2/$lan2/g" $file
grep -q "\$LAN3" $file && sed -i "s/\$LAN3/$lan3/g" $file
grep -q "\$LAN4" $file && sed -i "s/\$LAN4/$lan4/g" $file
ifname="$(uci -q get $file.wan.ifname | sed 's/[ \t]*$//')"
uci -q set $file.wan.ifname="$ifname"
uci -q commit $file
fi
done
local hardware=$(db get hw.board.model_name)
local hardware=$(db -q get hw.board.model_name)
local keys lang desc exp exclude support
for mode in $(ls $MODEDIR); do
case "$mode" in
repeater*)
wlctl -i wl1 ap >/dev/null 2>&1 || ifconfig rai0 2>/dev/null | grep -q rai0 || continue
;;
esac
lang=""
desc=""
exp=""
@ -478,43 +158,5 @@ populate_netmodes() {
uci -q set netmode.$mode.reboot="$reboot"
done
config_get curmode setup curmode
[ -d /etc/netmodes/$curmode ] || {
[ $MTK -eq 1 ] && uci -q set netmode.setup.curmode="routed_mtk" || uci -q set netmode.setup.curmode="routed_brcm"
}
uci commit netmode
}
start_netmode_tools() {
local curmode repeaterready wificontrol discover
killall -9 wificontrol >/dev/null 2>&1
killall -9 netmode-discover >/dev/null 2>&1
config_load netmode
config_get_bool repeaterready setup repeaterready 0
config_get_bool wificontrol tools wificontrol 1
config_get_bool arp_discovery tools arp_discovery 1
[ $repeaterready -eq 1 ] && {
[ $arp_discovery -eq 1 ] && /sbin/netmode-discover &
[ $wificontrol -eq 1 ] && /sbin/wificontrol --repeater &
return
}
config_get curmode setup curmode
case "$curmode" in
repeater*)
[ $arp_discovery -eq 1 ] && /sbin/netmode-discover &
[ $wificontrol -eq 1 ] && /sbin/wificontrol --repeater &
;;
esac
}
stop_netmode_tools() {
killall -9 netmode-discover >/dev/null 2>&1
killall -9 wificontrol >/dev/null 2>&1
}

View file

@ -1,59 +0,0 @@
#!/bin/sh
# receive new client events
# and trigger wificontrol in --router mode for that client
. /usr/share/libubox/jshn.sh
action=
ipaddr=
macaddr=
network=
timed_check() {
while true; do
network=${network:-lan}
ubus call repeater get_creds '{"network":"'$network'","file":"/tmp/wificontrol.txt"}'
wificontrol --router
sleep $1
done
}
is_known_macaddr()
{
macaddr=$1
echo $macaddr | grep -i -e "^00:22:07" \
-e "^02:22:07" \
-e "^44:D4:37" \
-e "^00:0C:07" \
-e "^02:0C:07" \
-e "^06:0C:07" \
-e "^00:0C:43" \
-e "^02:0C:43" \
-e "^06:0C:43" \
&& return
false
}
timed_check 60 &
while true ; do
ubus listen client | \
while read event ; do
#echo "netmode-client-detect got event: $event" >/dev/console
json_load "$event"
json_select client
json_get_var action action
[ "$action" == "connect" ] || continue
json_get_var macaddr macaddr
json_get_var ipaddr ipaddr
json_get_var network network "lan"
if is_known_macaddr $macaddr; then
logger -s -p user.info -t $0 "netmode-client-detect: a new known device detected on '$network' network (MACAddr:$macaddr IPAddr:$ipaddr)" >/dev/console
ubus call repeater get_creds '{"network":"'$network'","file":"/tmp/wificontrol.txt"}'
/sbin/wificontrol --router --destination $ipaddr
fi
done
done

View file

@ -1,308 +0,0 @@
#!/bin/sh
. /usr/share/libubox/jshn.sh
. /lib/functions.sh
. /lib/functions/netmode.sh
state="exit"
TMPPATH="/tmp/netmode_config_backup"
CURMODE=""
SLEEPTIME=300
pid_file=/tmp/netmode-conf.pid
echo "$$" >$pid_file
cleanup(){
rm -rf $pid_file
# we need to trigger a reload of icwmp but ONLY if ip or device has changed
[ -f "/etc/hotplug.d/iface/90-icwmp" ] && INTERFACE=wan ACTION=ifup sh /etc/hotplug.d/iface/90-icwmp
exit
}
trap cleanup SIGHUP SIGINT SIGTERM SIGSTOP
set_wireless_values() {
local iface_num="$1"
local ssid="$2"
local key="$3"
local encryption="$4"
local device="$5"
local old_ssid old_key old_encryption
if ! uci -q get wireless.@wifi-iface[$iface_num] >/dev/null 2>&1; then
state="reload"
uci add wireless wifi-iface >/dev/null 2>&1
uci set wireless.@wifi-iface[$iface_num].device="$device"
fi
local network="$(uci -q get wireless.@wifi-iface[$iface_num].network)"
old_ssid="$(uci -q get wireless.@wifi-iface[$iface_num].ssid)"
old_key="$(uci -q get wireless.@wifi-iface[$iface_num].key)"
old_encryption="$(uci -q get wireless.@wifi-iface[$iface_num].encryption)"
if [ "$old_ssid" != "$ssid" -o "$old_encryption" != "$encryption" -o "$old_key" != "$key" -o -z "$network" ]; then
#TODO: get network dynamicaly
[ "$state" == "exit" ] && state="apply"
[ -z "$network" ] && network="wan"
uci set wireless.@wifi-iface[$iface_num].network="$network"
uci set wireless.@wifi-iface[$iface_num].ssid="$ssid"
uci set wireless.@wifi-iface[$iface_num].key="$key"
uci set wireless.@wifi-iface[$iface_num].encryption="$encryption"
fi
}
get_wifi_device_from_band(){
local section="$1"
local band="$2"
local __ret="$3"
local b
config_get b $section "band"
if [ "$band" == "$b" ]; then
eval "export -- \"$__ret=$section\""
fi
}
get_device(){
local band="$1"
config_foreach get_wifi_device_from_band "wifi-device" "$band" "$2"
}
get_iface_num() {
local device="$1"
local prev="$2"
local __save="$3"
local i=0
local dev
while true; do
[ $i -gt 100 ] && break ## just a safty messure
if ! uci -q get wireless.@wifi-iface[$i] >/dev/null; then
break
fi
mode="$(uci -q get wireless.@wifi-iface[$i].mode)"
if [ "$mode" == "wet" -o "$mode" == "sta" ]; then
i=$((i+1))
continue
fi
dev="$(uci -q get wireless.@wifi-iface[$i].device)"
if [ "$dev" != "$device" ]; then
[ -z "$dev" ] && break
i=$((i+1))
elif [ $prev -ne 0 ]; then
i=$((i+1))
prev=$((prev-1))
else
break
fi
done
eval "export -- \"$__save=$i\""
}
restore() {
if [ "$1" == "back" ]; then
uci set juci.juci.homepage='netmode-wizard'
uci commit juci
cp $TMPPATH/* /etc/config/
sync
uci set netmode.setup.curmode=$CURMODE
uci commit netmode
ubus call network reload
wifi reload
ubus call router.network reload
fi
rm -r $TMPPATH
ubus call leds set '{"state":"normal"}'
}
backup() {
mkdir -p $TMPPATH
cp /etc/config/* $TMPPATH/
CURMODE="$(uci -q get netmode.setup.curmode)"
ubus call leds set '{"state":"allflash"}'
}
###### START HERE #######
file="$1"
from_gui="$2"
[ -f "$file" ] || cleanup
# let netmode-handler up to 20 seconds to finish switching mode
wait_for_netmode_handler
[ "$from_gui" == "true" ] && backup
json_load "`cat $file`"
json_select "wifi_ifaces"
curmode=""
repmode=""
config_load netmode
config_get curmode setup curmode
case $curmode in
*repeater*)
;;
*)
uci set netmode.setup.curmode="repeater"
uci commit netmode
/etc/init.d/netmode reload
curmode="$(uci -q get netmode.setup.curmode)"
;;
esac
apcliband="a"
case $curmode in
*_2g_*) apcliband="b";;
esac
i=1
dummy=
band=
ssid=
key=
encryption=
device=
iface_num=
while json_get_var dummy $i; do
json_select $i
json_get_var band band
[ "$band" == "" -o "$apcliband" == "$band" ] && break
json_select ..
i=$((i+1))
done
[ "$apcliband" == "$band" -o "$band" == "" ] || {
[ "$from_gui" == "true" ] && restore "back"
cleanup
}
repeater_iface_num=$(uci -q show wireless | grep -e ".mode='wet'" -e ".mode='sta'" | sed 's/.*\[\([0-9]\)\].*/\1/')
[ -z "$repeater_iface_num" ] && {
[ "$from_gui" == "true" ] && restore "back"
cleanup
}
json_get_var ssid ssid
json_get_var key key
json_get_var encryption encryption
set_wireless_values "$repeater_iface_num" "$ssid" "$key" "$encryption"
json_load "`cat $file`"
json_select "wifi_ifaces"
config_load wireless
b_num=0
a_num=0
i=1
while json_get_var dummy $i; do
json_select $i
json_get_var band band
json_get_var ssid ssid
json_get_var encryption encryption
json_get_var key key
if [ "$band" == "" ]; then
get_device "a" device
get_iface_num "$device" "$a_num" iface_num
a_num=$((a_num+1))
[ -z $iface_num ] && {
[ "$from_gui" == "true" ] && restore "back"
cleanup
}
set_wireless_values "$iface_num" "$ssid" "$key" "$encryption" "$device"
get_device "b" device
get_iface_num "$device" "$b_num" iface_num
b_num=$((b_num+1))
[ -z $iface_num ] && {
[ "$from_gui" == "true" ] && restore "back"
cleanup
}
set_wireless_values "$iface_num" "$ssid" "$key" "$encryption" "$device"
else
get_device "$band" device
case $band in
a)
get_iface_num "$device" "$a_num" iface_num
a_num=$((a_num+1))
;;
b)
get_iface_num "$device" "$b_num" iface_num
b_num=$((b_num+1))
;;
esac
[ -z $iface_num ] && {
[ "$from_gui" == "true" ] && restore "back"
cleanup
}
set_wireless_values "$iface_num" "$ssid" "$key" "$encryption" "$device"
fi
i=$((i+1))
json_select ..
done
i=$((b_num + a_num + 1))
if [ $repeater_iface_num -gt $i ]; then
i=$((i-1))
fi
while uci -q get wireless.@wifi-iface[$i] >/dev/null; do
# if repeater_iface_num is greater than the number of
# configured downlink we need to make sure its not deleted
local mode="$(uci -q get wireless.@wifi-iface[$i].mode)"
if [ "$mode" == "wet" -o "$mode" == "sta" ]; then
i=$((i+1))
continue
fi
state="reload"
uci -q delete wireless.@wifi-iface[$i] >/dev/null
# do not increment i. The next interface will now
# have the same index as the deleted interface
done
uci commit wireless
if [ "$from_gui" == "true" ]; then
# check for connectivity
wifi reload
[ -f /etc/init.d/layer2 ] && /etc/init.d/layer2 reload
correct_uplink
i=$SLEEPTIME
while [ $i -gt 0 ]; do
ip=`route -n | awk '/^0.0.0.0/{print $2}'`
if [ "$ip" == "" ]; then
i=$((i-10))
sleep 10
continue
fi
ping -w1 $ip
if [ $? -eq 0 ]; then
restore
cleanup
else
i=$((i-10))
sleep 9
fi
done
restore "back"
else
if [ "$state" == "exit" ]; then
cleanup
fi
ubus call leds set '{"state":"allflash"}'
if [ "$state" == "apply" ]; then
# wifi apply
wifi reload
else
# wifi reload
wifi reload
fi
[ -f /etc/init.d/layer2 ] && /etc/init.d/layer2 reload
correct_uplink
ubus call leds set '{"state":"normal"}'
fi
cleanup

View file

@ -1,47 +0,0 @@
#!/bin/sh
mask_to_cidr() {
nbits=0
IFS=.
for dec in $1 ; do
case $dec in
255) let nbits+=8;;
254) let nbits+=7;;
252) let nbits+=6;;
248) let nbits+=5;;
240) let nbits+=4;;
224) let nbits+=3;;
192) let nbits+=2;;
128) let nbits+=1;;
0);;
*) echo "Error: $dec is not recognised"; exit 1
esac
done
echo "$nbits"
}
while true; do
[ -e /sys/class/net/br-wan ] || {
sleep 5
continue
}
unlisted=0
ps | grep wifi | grep -q reload || {
for mac in $(brctl showmacs br-wan 2>/dev/null | tail -n +2 | grep no | awk '{print$2}'); do
grep -q "$mac" /proc/net/arp || unlisted=$((unlisted+1))
done
ipaddr=$(ifconfig br-wan | grep -v inet6 | grep inet | tr ':' ' ' | awk '{print$3}')
netmask=$(ifconfig br-wan | grep -v inet6 | grep inet | tr ':' ' ' | awk '{print$7}')
mask=$(mask_to_cidr $netmask)
}
if [ $unlisted -gt 0 -a -n "$ipaddr" -a -n "$netmask" -a $mask -ge 24 ]; then
fping -c 1 -t 1000 -g $ipaddr/$mask -q >/dev/null 2>&1
fi
sleep 30
done

View file

@ -1,3 +0,0 @@
#!/bin/sh
netmode-conf "$1" "$2" &

View file

@ -1,157 +1,14 @@
#!/bin/sh
. /lib/functions.sh
. /lib/network/utils.sh
NETCON=0
TVCON=0
WANIP=0
LASTSTATUS=""
CURSTATUS=""
ETHWAN="$(db -q get hw.board.ethernetWanPort)"
SLEEP_TIME=30
enableCounter=0
AccessPolicy=0
get_wifi_iface_cfgstr() {
get_cfgno() {
config_get ifname "$1" ifname
[ "$ifname" == "$2" ] && echo "wireless.$1"
}
config_load wireless
config_foreach get_cfgno wifi-iface $1
}
ping_uplink()
{
local hasip hasdev hasmode
local ipaddr=""
local device=""
local rready=""
local wetif=""
local assoclist=""
local rv=0
[ -f /tmp/wps_client_mode_active ] && {
ubus call led.internet set '{"state" : "off"}'
return
}
ipaddr="$(ip r | grep default | awk '{print$3}')"
[ -n "$ipaddr" ] && hasip=1 || hasip=0
device="$(ip r | grep default | awk '{print$5}')"
[ -n "$device" ] && hasdev=1 || hasdev=0
rready="$(uci -q get netmode.setup.repeaterready)"
[ "$rready" == "1" ] && hasmode=0 || hasmode=1
if [ "$hasmode" == "1" ] && [ "$hasip" == "0" -o "$hasdev" == "0" ]; then
rv=0
elif [ "$hasmode" == "1" ]; then
arping -f -q -c1 -w3 -I $device $ipaddr
[ $? -eq 1 ] && rv=0 || rv=1
if [ "$rv" == "0" ]; then
ping -c2 -w5 $ipaddr >/dev/null
[ $? -eq 1 ] && rv=0 || rv=1
fi
else
SLEEP_TIME=5
ubus call led.internet set '{"state" : "off"}'
return
fi
if [ $rv -eq 0 ]; then
echo "{ \"online\" : false }" > /tmp/internet_connection_status
SLEEP_TIME=10
wetif="$(uci -q get wireless.$(uci show wireless | grep 'mode=.*wet.*' | cut -d'.' -f2 | head -1).ifname)"
# If there is an uplink IP address but no connection for 5 minutes, reboot
if [ $enableCounter -gt 32 ]; then
logger -s -p user.info -t "testnet" "No uplink connection; reboot" >/dev/console
ubus call led.internet set '{"state" : "error"}'
reboot &
elif [ $((enableCounter % 8)) -eq 0 -a $enableCounter -ge 8 -o $enableCounter -eq 0 ]; then
ps | grep -v grep | grep -q 'wifi reload\|wifi import\|wifi.credentials\|iwpriv' && return
logger -s -p user.info -t "testnet" "No uplink connection; try to re-connect" >/dev/console
if [ -f /usr/sbin/wlctl ]; then
assoclist="$(wlctl -i $wetif assoclist | head -1 | awk '{print$2}')"
wlctl -i $wetif reassoc $assoclsit
elif [ -f /usr/sbin/iwpriv ]; then
wetcfg="$(get_wifi_iface_cfgstr $wetif)"
wetssid="$(uci -q get $wetcfg.ssid)"
wetkey="$(uci -q get $wetcfg.key)"
if [ -n "$wetkey" ]; then
wetauth="WPA2PSK"
wetenc="AES"
else
wetauth="OPEN"
wetenc="NONE"
fi
iwpriv $wetif set ApCliEnable=0
iwpriv $wetif set ApCliSsid="$wetssid"
iwpriv $wetif set ApCliAuthMode="$wetauth"
iwpriv $wetif set ApCliEncrypType="$wetenc"
iwpriv $wetif set ApCliWPAPSK="$wetkey"
iwpriv $wetif set ApCliEnable=1
iwpriv $wetif set ApCliAutoConnect=1
fi
ubus call led.internet set '{"state" : "notice"}'
# touch /tmp/netmodes/repeater_trying_to_connect
# else
# if [ -f /tmp/netmodes/repeater_trying_to_connect ]; then
# rm -f /tmp/netmodes/repeater_trying_to_connect
# else
# ubus call led.internet set '{"state" : "error"}'
# fi
fi
enableCounter=$((enableCounter+1))
if [ $AccessPolicy -eq 1 ]; then
return
fi
AccessPolicy=1
if [ -f /usr/sbin/iwpriv ]; then
grep -q up /sys/class/net/$ETHWAN/operstate 2>/dev/null || {
logger -s -p user.info -t "testnet" "Disconnect all stations and disable WiFi access on downlink wireless interfaces" >/dev/console
# Do not allow clients to connect on 2.4GHz radio
iwpriv ra0 set AccessPolicy=$AccessPolicy
# Do not allow clients to connect on 5GHz radio
iwpriv rai0 set AccessPolicy=$AccessPolicy
# Disconnect clients on 2.4GHz radio
iwpriv ra0 set DisConnectAllSta=2
# Disconnect clients on 5GHz radio
iwpriv rai0 set DisConnectAllSta=2
}
fi
else
grep -q "true" /tmp/internet_connection_status 2>/dev/null || logger -s -p user.info -t "testnet" "Uplink connection is gained" >/dev/console
echo "{ \"online\" : true }" > /tmp/internet_connection_status
leden="$(uci -q get leds.internet.enable)"
[ "$leden" == "1" ] && ubus call led.internet set '{"state" : "eok"}'
SLEEP_TIME=5
enableCounter=0
if [ $AccessPolicy -eq 0 ]; then
return
fi
# Uplink working, allow clients to connect
AccessPolicy=0
if [ -f /usr/sbin/iwpriv ]; then
logger -s -p user.info -t "testnet" "Enable WiFi access on downlink wireless interfaces" >/dev/console
iwpriv ra0 set AccessPolicy=$AccessPolicy
iwpriv rai0 set AccessPolicy=$AccessPolicy
fi
fi
}
test_connection() {
local addr="$1"
@ -166,31 +23,25 @@ test_connection() {
for nmsrv in $(grep nameserver /var/resolv.conf.auto | awk '{print$2}'); do
ping -q -w 5 -c 1 $nmsrv >/dev/null 2>&1 && return 0
done
elif [ -n "$def6route" ] && [ -n "$ping6dev" ]; then
elif [ -n "$def6route" -a -n "$ping6dev" ]; then
ndisc6 -w 5 -1 $def6route $ping6dev >/dev/null 2>&1 && return 0
fi
return 1
}
wan_have_ip() {
[ -d /sys/class/net/$ETHWAN ] || return
ubus list led.wan >/dev/null 2>&1 || return
local addr=$(ip a show dev $ETHWAN | grep "inet "| awk '{print $2}')
if [ -n "$addr" ]; then
WANIP=1
uplink_test() {
uplink_phy_connected
if [ $? -eq 0 ]; then
ubus call led.wan set '{"state" : "ok"}'
else
WANIP=0
ubus call led.wan set '{"state" : "off"}'
fi
# don't set wan here status is set from hotplug, only set when ip is detected.
}
internet_test() {
local link dest
dest="$(uci -q get system.@system[0].netping_addr)"
dest="$(uci -q get diagnostics.@connectivity[0].destination)"
test_connection $dest
@ -205,41 +56,8 @@ internet_test() {
fi
}
iptv_test() {
local dest="$(uci -q get system.@system[0].tvping_addr)"
[ -n "$dest" ] || return
ubus list led.ext >/dev/null 2>&1 || return
test_connection $dest
if [ "$?" -eq 0 ]; then
TVCON=1
ubus call led.ext set '{"state" : "ok"}'
else
TVCON=0
ubus call led.ext set '{"state" : "off"}'
fi
}
netmode_check() {
local curmode="$(uci -q get netmode.setup.curmode)"
local uplinkband="$(uci -q get netmode.$curmode.uplink_band)"
local rready="$(uci -q get netmode.setup.repeaterready)"
[ "$rready" == "1" ] && uplinkband="a"
echo "$uplinkband"
}
connectivity_test() {
if [ "$(netmode_check)" != "" ]; then
ping_uplink
return
fi
SLEEP_TIME=30
uplink_test
internet_test

View file

@ -1,240 +0,0 @@
#!/bin/sh
. /usr/share/libubox/jshn.sh
. /lib/functions.sh
WIFISTA=0
STACONF="/etc/Wireless/iNIC/iNIC_ap.dat"
[ -f $STACONF ] && WIFISTA=1
get_if_creds() {
local section=$1
local network=$2
local net dev ssid key encryption band
config_get net $section "network" "lan"
if [ "$net" == "$network" ]; then
config_get dev $section "device"
band="$(uci -q get wireless.$dev.band)"
config_get ssid $section ssid
config_get key $section key
config_get encryption $section encryption
config_get disabled $section disabled 0
[ $disabled -eq 1 ] && return
[ "$ssid" == "" -o "$band" == "" ] && return
json_add_object
json_add_string "ssid" "$ssid"
json_add_string "band" "$band"
json_add_string "key" "$key"
json_add_string "encryption" "$encryption"
json_close_object
fi
}
validate_file() {
local file="$1"
local real="$(readlink -f `dirname $file`)"
real="${real}/`basename $file`"
case "$real" in
/tmp/*)
touch $real && return 0 || return 1
;;
*)
return 1
;;
esac
}
write_error(){
json_init
json_add_string "status" "error: $1"
json_dump
exit 1
}
duplicate_if_single_radio() {
json_select "wifi-ifaces"
local i=1
local dummy band dup_band num_radios
num_radios="$(uci -q show wireless | grep wifi-device | wc -l)"
[ "$num_radios" == "2" ] && return
band="$(uci -q get wireless.@wifi-device[0].band)"
[ "$band" == "a" ] && dup_band="b"
[ "$band" == "b" ] && dup_band="a"
[ -z "$dup_band" ] && return
while json_get_var dummy $i; do
i=$((i+1))
done
i=$((i-1))
local ssid key encryption
while [ $i -gt 0 ]; do
json_select $i
json_get_var ssid ssid
json_get_var key key
json_get_var encryption encryption
json_select ..
json_add_object
json_add_string "ssid" "$ssid"
json_add_string "band" "$dup_band"
json_add_string "key" "$key"
json_add_string "encryption" "$encryption"
json_close_object
i=$((i-1))
done
}
get_wifi_iface_cfgstr() {
get_cfgno() {
config_get ifname "$1" ifname
[ "$ifname" == "$2" ] && echo "wireless.$1"
}
config_load wireless
config_foreach get_cfgno wifi-iface $1
}
case "$1" in
list)
if [ $WIFISTA -eq 1 ]; then
echo '{ "get_creds": { "network": "str", "file": "str" }, "set_creds": { "file": "str", "from_gui": "str" }, "connect": { "ssid": "str", "key": "str" }, "forget_network": {}, "wps_enroll": {}, "wps_stop": {} }'
else
echo '{ "get_creds": { "network": "str", "file": "str" }, "set_creds": { "file": "str", "from_gui": "str" } }'
fi
;;
call)
case "$2" in
get_creds)
#TODO: if not routed exit
local curmode="$(uci -q get netmode.setup.curmode)"
case $curmode in
*repeater*) write_error "not in repeater mode";;
esac
local network file input
read input
json_load $input
json_get_var network network
json_get_var file file
json_init
json_add_array "wifi-ifaces"
config_load wireless
config_foreach get_if_creds "wifi-iface" "$network"
json_close_array
duplicate_if_single_radio
if [ "$file" == "" ]; then
json_dump
else
validate_file $file && json_dump -i >$file || write_error "invalid filename (\"$file\"), file must be in /tmp/ and in an existing directory"
fi
json_init
json_add_string "status" success
json_dump
;;
set_creds)
local file from_gui
read input
json_load "$input"
json_get_var file file
json_get_var from_gui from_gui
validate_file "$file" || write_error "invalid filename (\"$file\"), file must be in /tmp and exist"
json_load "`cat $file`" || write_error "invalid file content"
json_select "wifi_ifaces" || write_error "invalid file content"
start-netmode-conf "$file" "$from_gui" >/dev/null 2>/dev/null
json_init
json_add_string "status" success
json_dump
;;
connect)
local ssid key enc auth wetif wetcfg wetssid wetkey code
read input
json_load "$input"
json_get_var ssid ssid
json_get_var key key
code=1
[ $WIFISTA -eq 1 ] && wetif="$(uci -q get wireless.$(uci show wireless | grep 'mode=.*wet.*' | cut -d'.' -f2 | head -1).ifname)"
if [ -s /sys/class/net/$wetif/operstate ]; then
wetcfg="$(get_wifi_iface_cfgstr $wetif)"
wetssid="$(uci -q get $wetcfg.ssid)"
wetkey="$(uci -q get $wetcfg.key)"
ssid="${ssid:-$wetssid}"
key="${key:-$wetkey}"
if [ -n "$key" ]; then
auth="WPA2PSK"
enc="AES"
else
auth="OPEN"
enc="NONE"
fi
iwpriv $wetif set ApCliEnable=0
iwpriv $wetif set ApCliSsid="$ssid"
iwpriv $wetif set ApCliAuthMode="$auth"
iwpriv $wetif set ApCliEncrypType="$enc"
iwpriv $wetif set ApCliWPAPSK="$key"
iwpriv $wetif set ApCliEnable=1
iwpriv $wetif set ApCliAutoConnect=1
code=0
fi
json_init
json_add_int "code" $code
json_dump
;;
forget_network)
local wetif wetcfg code
code=1
[ $WIFISTA -eq 1 ] && wetif="$(uci -q get wireless.$(uci show wireless | grep 'mode=.*wet.*' | cut -d'.' -f2 | head -1).ifname)"
if [ -s /sys/class/net/$wetif/operstate ]; then
wetcfg="$(get_wifi_iface_cfgstr $wetif)"
uci -q set $wetcfg.ssid=""
uci -q set $wetcfg.key=""
uci -q commit wireless
sed -i "s/ApCliSsid=.*/ApCliSsid=/g" $STACONF
sed -i "s/ApCliAuthMode=.*/ApCliAuthMode=/g" $STACONF
sed -i "s/ApCliEncrypType=.*/ApCliEncrypType=/g" $STACONF
sed -i "s/ApCliWPAPSK=.*/ApCliWPAPSK=/g" $STACONF
ifconfig $wetif down
ifconfig $wetif up
code=0
fi
json_init
json_add_int "code" $code
json_dump
;;
wps_enroll)
local wetif
[ $WIFISTA -eq 1 ] && wetif="$(uci -q get wireless.$(uci show wireless | grep 'mode=.*wet.*' | cut -d'.' -f2 | head -1).ifname)"
if [ -n "$wetif" ]; then
iwpriv $wetif set ApCliEnable=0
iwpriv $wetif set WscConfMode=1
iwpriv $wetif set WscMode=2
iwpriv $wetif set ApCliEnable=1
iwpriv $wetif set WscGetConf=1
fi
json_init
json_add_string "ifname" $wetif
json_dump
;;
wps_stop)
local wetif
[ $WIFISTA -eq 1 ] && wetif="$(uci -q get wireless.$(uci show wireless | grep 'mode=.*wet.*' | cut -d'.' -f2 | head -1).ifname)"
if [ -n "$wetif" ]; then
iwpriv $wetif set WscStop=1
fi
json_init
json_add_string "ifname" $wetif
json_dump
;;
esac
;;
esac