2014-08-18 13:09:17 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
CFG=/etc/board.json
|
|
|
|
|
|
|
|
. /usr/share/libubox/jshn.sh
|
|
|
|
|
|
|
|
[ -f $CFG ] || exit 1
|
|
|
|
|
|
|
|
generate_static_network() {
|
2015-11-26 12:04:32 +00:00
|
|
|
uci -q batch <<-EOF
|
|
|
|
delete network.loopback
|
|
|
|
set network.loopback='interface'
|
|
|
|
set network.loopback.ifname='lo'
|
|
|
|
set network.loopback.proto='static'
|
|
|
|
set network.loopback.ipaddr='127.0.0.1'
|
|
|
|
set network.loopback.netmask='255.0.0.0'
|
|
|
|
delete network.globals
|
|
|
|
set network.globals='globals'
|
|
|
|
set network.globals.ula_prefix='auto'
|
|
|
|
EOF
|
2014-08-18 13:09:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
next_vlan=3
|
|
|
|
generate_network() {
|
|
|
|
local vlan
|
|
|
|
|
|
|
|
json_select network
|
2015-11-26 12:04:32 +00:00
|
|
|
json_select "$1"
|
2014-08-18 13:09:17 +00:00
|
|
|
json_get_vars ifname create_vlan macaddr
|
|
|
|
json_select ..
|
|
|
|
json_select ..
|
|
|
|
|
|
|
|
[ -n "$ifname" ] || return
|
2015-11-26 12:04:32 +00:00
|
|
|
|
|
|
|
if [ "${create_vlan:-0}" -eq 1 ]; then
|
|
|
|
case "$1" in
|
|
|
|
lan) vlan=1;;
|
|
|
|
wan) vlan=2;;
|
|
|
|
*)
|
|
|
|
vlan=$next_vlan
|
|
|
|
next_vlan=$((next_vlan + 1))
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
|
2014-08-18 13:09:17 +00:00
|
|
|
[ -n "$vlan" ] && ifname=${ifname}.${vlan}
|
2015-11-26 12:04:32 +00:00
|
|
|
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
delete network.$1
|
|
|
|
set network.$1='interface'
|
|
|
|
set network.$1.ifname='$ifname'
|
|
|
|
set network.$1.proto='none'
|
|
|
|
set network.$1.macaddr='$macaddr'
|
|
|
|
EOF
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
lan)
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
set network.$1.type='bridge'
|
|
|
|
set network.$1.proto='static'
|
|
|
|
set network.$1.ipaddr='192.168.1.1'
|
|
|
|
set network.$1.netmask='255.255.255.0'
|
|
|
|
set network.$1.ip6assign='60'
|
|
|
|
EOF
|
2014-08-18 13:09:17 +00:00
|
|
|
;;
|
2015-11-30 10:55:43 +00:00
|
|
|
|
2015-11-26 12:04:32 +00:00
|
|
|
wan)
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
set network.$1.proto='dhcp'
|
|
|
|
delete network.wan6
|
|
|
|
set network.wan6='interface'
|
|
|
|
set network.wan6.ifname='$ifname'
|
|
|
|
set network.wan6.proto='dhcpv6'
|
|
|
|
EOF
|
2014-08-18 13:09:17 +00:00
|
|
|
;;
|
2015-11-30 10:55:43 +00:00
|
|
|
|
|
|
|
*)
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
set network.$1.force_link=1
|
|
|
|
EOF
|
|
|
|
;;
|
2014-08-18 13:09:17 +00:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
generate_switch_vlan() {
|
2015-11-26 12:04:32 +00:00
|
|
|
local device="$1"
|
|
|
|
local vlan="$2"
|
|
|
|
local cpu_port="$3"
|
|
|
|
|
|
|
|
case "$vlan" in
|
|
|
|
lan) vlan=1;;
|
|
|
|
wan) vlan=2;;
|
|
|
|
*) vlan="${vlan##vlan}";;
|
2014-08-18 13:09:17 +00:00
|
|
|
esac
|
|
|
|
|
|
|
|
json_select vlans
|
2015-11-26 12:04:32 +00:00
|
|
|
json_select "$2"
|
2014-08-18 13:09:17 +00:00
|
|
|
json_get_values ports
|
|
|
|
json_select ..
|
|
|
|
json_select ..
|
|
|
|
|
2015-11-26 12:04:32 +00:00
|
|
|
uci -q batch <<-EOF
|
|
|
|
add network switch_vlan
|
|
|
|
set network.@switch_vlan[-1].device='$device'
|
|
|
|
set network.@switch_vlan[-1].vlan='$vlan'
|
|
|
|
set network.@switch_vlan[-1].ports='$ports ${cpu_port}t'
|
|
|
|
EOF
|
2014-08-18 13:09:17 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 21:16:38 +00:00
|
|
|
calculate_switch_vlans() {
|
2015-11-26 12:04:32 +00:00
|
|
|
local switch="$1"
|
2015-11-25 15:43:50 +00:00
|
|
|
local ports port attr val
|
2015-11-24 21:16:38 +00:00
|
|
|
local prev_role
|
|
|
|
local num device role index
|
|
|
|
local n_cpu=0 cpu0 cpu1 cpu2 cpu3
|
|
|
|
local dev0 dev1 dev2 dev3 role0 role1 role2 role3
|
|
|
|
local n_vlan=0 vlan_off=-1
|
|
|
|
local vlan_ports cpu_port
|
|
|
|
|
|
|
|
json_get_keys ports ports
|
|
|
|
|
|
|
|
json_select ports
|
|
|
|
|
|
|
|
# gather all cpu ports and count vlans
|
|
|
|
for port in $ports; do
|
|
|
|
json_select "$port"
|
|
|
|
json_get_vars num device role
|
|
|
|
|
2015-11-25 15:43:50 +00:00
|
|
|
if json_is_a attr object; then
|
|
|
|
json_get_keys attr attr
|
|
|
|
json_select attr
|
|
|
|
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
add network switch_port
|
|
|
|
set network.@switch_port[-1].device='$switch'
|
|
|
|
set network.@switch_port[-1].port=$num
|
|
|
|
EOF
|
|
|
|
|
|
|
|
for attr in $attr; do
|
|
|
|
json_get_var val "$attr"
|
|
|
|
uci -q set network.@switch_port[-1].$attr="$val"
|
|
|
|
done
|
|
|
|
|
|
|
|
json_select ..
|
|
|
|
fi
|
|
|
|
|
|
|
|
json_select ..
|
|
|
|
|
2015-11-24 21:16:38 +00:00
|
|
|
if [ -n "$num" ] && [ -n "$device" ]; then
|
|
|
|
export "cpu$n_cpu=$num"
|
|
|
|
export "dev$n_cpu=$device"
|
|
|
|
n_cpu=$((n_cpu + 1))
|
|
|
|
elif [ -n "$num" ] && [ -n "$role" ] && [ "$role" != "$prev_role" ]; then
|
|
|
|
export "role$n_vlan=$role"
|
|
|
|
n_vlan=$((n_vlan + 1))
|
|
|
|
prev_role="$role"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
unset prev_role
|
|
|
|
|
|
|
|
# autogenerate vlans
|
|
|
|
for port in $ports ""; do
|
|
|
|
if [ -n "$port" ]; then
|
2015-11-25 15:43:50 +00:00
|
|
|
json_select "$port"
|
2015-11-24 21:16:38 +00:00
|
|
|
json_get_vars num device role
|
2015-11-25 15:43:50 +00:00
|
|
|
json_select ..
|
2015-11-24 21:16:38 +00:00
|
|
|
else
|
|
|
|
num="-"; role="-"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$num" ] && [ -n "$role" ]; then
|
|
|
|
if [ "$role" != "$prev_role" ]; then
|
|
|
|
if [ -n "$vlan_ports" ]; then
|
|
|
|
let cpu_port="cpu$((vlan_off % n_cpu))"
|
2015-11-30 12:32:12 +00:00
|
|
|
let need_tag="tag$((vlan_off % n_cpu))"
|
|
|
|
[ $n_vlan -gt $n_cpu -o $need_tag -eq 1 ] && cpu_port="${cpu_port}t"
|
2015-11-24 21:16:38 +00:00
|
|
|
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
add network switch_vlan
|
|
|
|
set network.@switch_vlan[-1].device='$switch'
|
|
|
|
set network.@switch_vlan[-1].vlan='$((vlan_off + 1))'
|
|
|
|
set network.@switch_vlan[-1].ports='$vlan_ports $cpu_port'
|
|
|
|
EOF
|
|
|
|
fi
|
|
|
|
|
|
|
|
vlan_off=$((vlan_off + 1))
|
|
|
|
vlan_ports="$num"
|
|
|
|
prev_role="$role"
|
|
|
|
else
|
|
|
|
vlan_ports="$vlan_ports $num"
|
|
|
|
fi
|
|
|
|
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
json_select ..
|
|
|
|
|
|
|
|
# autogenerate interfaces
|
|
|
|
vlan_off=0; while [ $vlan_off -lt $n_vlan ]; do
|
|
|
|
eval role="\$role$((vlan_off))"
|
2015-11-30 12:32:12 +00:00
|
|
|
eval device="\$dev$((vlan_off % n_cpu))"
|
|
|
|
let need_tag="tag$((vlan_off++ % n_cpu))"
|
|
|
|
[ $n_vlan -gt $n_cpu -o $need_tag -eq 1 ] && device="$device.$vlan_off"
|
2015-11-24 21:16:38 +00:00
|
|
|
|
2015-11-25 15:43:33 +00:00
|
|
|
# quirk: append ifnames for subsequent switches
|
|
|
|
case "$switch" in switch[1-9])
|
|
|
|
local prev_devs="$(uci -q get "network.$role.ifname")"
|
|
|
|
if echo "$prev_devs" | grep -wq "$device"; then
|
|
|
|
device="$prev_devs"
|
|
|
|
else
|
|
|
|
device="$prev_devs $device"
|
|
|
|
fi
|
|
|
|
;; esac
|
|
|
|
|
2015-11-24 21:16:38 +00:00
|
|
|
uci -q batch <<-EOF
|
|
|
|
set network.$role='interface'
|
|
|
|
set network.$role.ifname='$device'
|
|
|
|
EOF
|
|
|
|
|
|
|
|
case $role in
|
2015-11-26 12:04:32 +00:00
|
|
|
lan)
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
set network.lan.type='bridge'
|
|
|
|
set network.lan.proto='static'
|
|
|
|
set network.lan.ipaddr='192.168.1.1'
|
|
|
|
set network.lan.netmask='255.255.255.0'
|
|
|
|
set network.lan.ip6assign='60'
|
|
|
|
EOF
|
2015-11-24 21:16:38 +00:00
|
|
|
;;
|
2015-11-26 12:04:32 +00:00
|
|
|
|
|
|
|
wan)
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
set network.wan.proto='dhcp'
|
|
|
|
set network.wan6='interface'
|
|
|
|
set network.wan6.ifname='$device'
|
|
|
|
set network.wan6.proto='dhcpv6'
|
|
|
|
EOF
|
2015-11-24 21:16:38 +00:00
|
|
|
;;
|
2015-11-26 12:04:32 +00:00
|
|
|
|
|
|
|
*)
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
set network.$role.force_link='1'
|
|
|
|
set network.$role.proto='none'
|
|
|
|
EOF
|
2015-11-24 21:16:38 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2014-08-18 13:09:17 +00:00
|
|
|
generate_switch() {
|
2015-11-26 12:04:32 +00:00
|
|
|
local key="$1"
|
2014-08-18 13:09:17 +00:00
|
|
|
local vlans
|
|
|
|
|
|
|
|
json_select switch
|
2015-11-26 12:04:32 +00:00
|
|
|
json_select "$key"
|
2014-08-18 13:09:17 +00:00
|
|
|
json_get_vars enable reset blinkrate cpu_port
|
|
|
|
|
2015-11-26 12:04:32 +00:00
|
|
|
uci -q batch <<-EOF
|
|
|
|
add network switch
|
|
|
|
set network.@switch[-1].name='$key'
|
|
|
|
set network.@switch[-1].reset='$reset'
|
|
|
|
set network.@switch[-1].enable_vlan='$enable'
|
|
|
|
set network.@switch[-1].blinkrate='$blinkrate'
|
|
|
|
EOF
|
2015-11-24 21:16:38 +00:00
|
|
|
|
|
|
|
if [ -n "$cpu_port" ]; then
|
2014-10-27 15:51:17 +00:00
|
|
|
json_get_keys vlans vlans
|
|
|
|
for vlan in $vlans; do generate_switch_vlan $1 $vlan $cpu_port; done
|
2015-11-24 21:16:38 +00:00
|
|
|
elif json_is_a ports array; then
|
|
|
|
calculate_switch_vlans $1
|
|
|
|
fi
|
|
|
|
|
2014-08-18 13:09:17 +00:00
|
|
|
json_select ..
|
|
|
|
json_select ..
|
|
|
|
}
|
|
|
|
|
2015-11-26 12:04:43 +00:00
|
|
|
generate_rssimon() {
|
|
|
|
local key="$1"
|
|
|
|
local cfg="rssid_$key"
|
|
|
|
local refresh threshold
|
|
|
|
|
|
|
|
json_select rssimon
|
|
|
|
json_select "$key"
|
|
|
|
json_get_vars refresh threshold
|
|
|
|
json_select ..
|
|
|
|
json_select ..
|
|
|
|
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
delete system.$cfg
|
|
|
|
set system.$cfg='rssid'
|
|
|
|
set system.$cfg.dev='$key'
|
|
|
|
set system.$cfg.refresh='$refresh'
|
|
|
|
set system.$cfg.threshold='$threshold'
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2014-08-18 13:09:17 +00:00
|
|
|
generate_led() {
|
2015-11-26 12:04:32 +00:00
|
|
|
local key="$1"
|
2014-08-18 13:09:17 +00:00
|
|
|
local cfg="led_$key"
|
|
|
|
|
|
|
|
json_select led
|
2015-11-26 12:04:32 +00:00
|
|
|
json_select "$key"
|
2015-11-26 13:53:02 +00:00
|
|
|
json_get_vars name sysfs type trigger default
|
2014-08-18 13:09:17 +00:00
|
|
|
|
2015-11-26 12:04:32 +00:00
|
|
|
uci -q batch <<-EOF
|
|
|
|
delete system.$cfg
|
|
|
|
set system.$cfg='led'
|
|
|
|
set system.$cfg.name='$name'
|
|
|
|
set system.$cfg.sysfs='$sysfs'
|
|
|
|
set system.$cfg.trigger='$trigger'
|
|
|
|
set system.$cfg.default='$default'
|
|
|
|
EOF
|
|
|
|
|
|
|
|
case "$type" in
|
2015-11-26 13:53:06 +00:00
|
|
|
gpio)
|
|
|
|
local gpio inverted
|
|
|
|
json_get_vars gpio inverted
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
set system.$cfg.trigger='gpio'
|
|
|
|
set system.$cfg.gpio='$gpio'
|
|
|
|
set system.$cfg.inverted='$inverted'
|
|
|
|
EOF
|
|
|
|
;;
|
|
|
|
|
2015-11-26 12:04:32 +00:00
|
|
|
netdev)
|
2015-11-26 13:53:02 +00:00
|
|
|
local device
|
|
|
|
json_get_vars device
|
2015-11-26 12:04:32 +00:00
|
|
|
uci -q batch <<-EOF
|
|
|
|
set system.$cfg.trigger='netdev'
|
|
|
|
set system.$cfg.mode='link tx rx'
|
2015-11-26 13:53:02 +00:00
|
|
|
set system.$cfg.dev='$device'
|
2015-11-26 12:04:32 +00:00
|
|
|
EOF
|
|
|
|
;;
|
2014-08-18 13:09:17 +00:00
|
|
|
|
2015-11-26 12:04:32 +00:00
|
|
|
usb)
|
2015-11-26 13:53:02 +00:00
|
|
|
local device
|
|
|
|
json_get_vars device
|
2015-11-26 12:04:32 +00:00
|
|
|
uci -q batch <<-EOF
|
|
|
|
set system.$cfg.trigger='usbdev'
|
|
|
|
set system.$cfg.interval='50'
|
2015-11-26 13:53:02 +00:00
|
|
|
set system.$cfg.dev='$device'
|
2015-11-26 12:04:32 +00:00
|
|
|
EOF
|
|
|
|
;;
|
2015-11-26 12:04:43 +00:00
|
|
|
|
|
|
|
rssi)
|
2015-11-26 13:53:02 +00:00
|
|
|
local iface minq maxq offset factor
|
2015-11-26 12:04:43 +00:00
|
|
|
json_get_vars iface minq maxq offset factor
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
set system.$cfg.trigger='rssi'
|
|
|
|
set system.$cfg.iface='rssid_$iface'
|
|
|
|
set system.$cfg.minq='$minq'
|
|
|
|
set system.$cfg.maxq='$maxq'
|
|
|
|
set system.$cfg.offset='$offset'
|
|
|
|
set system.$cfg.factor='$factor'
|
|
|
|
EOF
|
|
|
|
;;
|
2015-11-26 13:53:02 +00:00
|
|
|
|
|
|
|
switch)
|
|
|
|
local port_mask
|
|
|
|
json_get_vars port_mask
|
|
|
|
uci -q batch <<-EOF
|
|
|
|
set system.$cfg.port_mask='$port_mask'
|
|
|
|
EOF
|
|
|
|
;;
|
2014-08-18 13:09:17 +00:00
|
|
|
esac
|
2015-11-26 12:04:43 +00:00
|
|
|
|
|
|
|
json_select ..
|
|
|
|
json_select ..
|
2014-08-18 13:09:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
json_init
|
|
|
|
json_load "$(cat ${CFG})"
|
|
|
|
|
|
|
|
generate_static_network
|
|
|
|
|
|
|
|
json_get_keys keys network
|
|
|
|
for key in $keys; do generate_network $key; done
|
|
|
|
|
|
|
|
json_get_keys keys switch
|
|
|
|
for key in $keys; do generate_switch $key; done
|
|
|
|
|
2015-11-26 12:04:43 +00:00
|
|
|
json_get_keys keys rssimon
|
|
|
|
for key in $keys; do generate_rssimon $key; done
|
|
|
|
|
2014-08-18 13:09:17 +00:00
|
|
|
json_get_keys keys led
|
|
|
|
for key in $keys; do generate_led $key; done
|
|
|
|
|
|
|
|
uci commit
|