2014-06-19 16:06:44 +00:00
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
|
|
|
# 3: path
|
|
|
|
# 4: separator
|
|
|
|
# 5: limit
|
|
|
|
__network_ifstatus() {
|
2012-12-16 20:17:18 +00:00
|
|
|
local __tmp
|
|
|
|
|
2014-06-19 16:06:44 +00:00
|
|
|
[ -z "$__NETWORK_CACHE" ] && \
|
|
|
|
export __NETWORK_CACHE="$(ubus call network.interface dump)"
|
2012-12-16 20:17:18 +00:00
|
|
|
|
2014-06-19 16:06:44 +00:00
|
|
|
__tmp="$(jsonfilter ${4:+-F "$4"} ${5:+-l "$5"} -s "$__NETWORK_CACHE" -e "$1=@.interface${2:+[@.interface='$2']}$3")"
|
2012-12-16 20:17:18 +00:00
|
|
|
|
2014-06-19 16:06:44 +00:00
|
|
|
[ -z "$__tmp" ] && \
|
|
|
|
unset "$1" && \
|
|
|
|
return 1
|
2012-12-16 20:17:18 +00:00
|
|
|
|
2014-06-19 16:06:44 +00:00
|
|
|
eval "$__tmp"
|
2012-05-28 03:15:02 +00:00
|
|
|
}
|
|
|
|
|
2013-06-17 11:33:36 +00:00
|
|
|
# determine first IPv4 address of given logical interface
|
2012-12-16 18:22:02 +00:00
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
2014-06-19 16:06:44 +00:00
|
|
|
network_get_ipaddr() {
|
|
|
|
__network_ifstatus "$1" "$2" "['ipv4-address'][0].address";
|
|
|
|
}
|
2012-12-16 18:22:02 +00:00
|
|
|
|
2013-06-17 11:33:36 +00:00
|
|
|
# determine first IPv6 address of given logical interface
|
2012-12-16 18:22:02 +00:00
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
2014-06-19 16:06:44 +00:00
|
|
|
network_get_ipaddr6() {
|
2017-06-08 10:02:36 +00:00
|
|
|
__network_ifstatus "$1" "$2" "['ipv6-address'][0].address" || \
|
|
|
|
__network_ifstatus "$1" "$2" "['ipv6-prefix-assignment'][0]['local-address'].address" || \
|
|
|
|
return 1
|
2014-06-19 16:06:44 +00:00
|
|
|
}
|
2012-05-28 03:15:02 +00:00
|
|
|
|
2013-06-17 11:33:36 +00:00
|
|
|
# determine first IPv4 subnet of given logical interface
|
2012-12-16 18:22:02 +00:00
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
2014-06-19 16:06:44 +00:00
|
|
|
network_get_subnet() {
|
|
|
|
__network_ifstatus "$1" "$2" "['ipv4-address'][0]['address','mask']" "/"
|
|
|
|
}
|
2012-12-16 18:22:02 +00:00
|
|
|
|
2013-06-17 11:33:36 +00:00
|
|
|
# determine first IPv6 subnet of given logical interface
|
2012-12-16 18:22:02 +00:00
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
2014-06-19 16:06:44 +00:00
|
|
|
network_get_subnet6() {
|
2017-06-08 17:27:46 +00:00
|
|
|
local __nets __addr
|
|
|
|
|
|
|
|
if network_get_subnets6 __nets "$2"; then
|
|
|
|
# Attempt to return first non-fe80::/10, non-fc::/7 range
|
|
|
|
for __addr in $__nets; do
|
|
|
|
case "$__addr" in fe[8ab]?:*|f[cd]??:*)
|
|
|
|
continue
|
|
|
|
esac
|
|
|
|
export "$1=$__addr"
|
|
|
|
return 0
|
|
|
|
done
|
|
|
|
|
|
|
|
# Attempt to return first non-fe80::/10 range
|
|
|
|
for __addr in $__nets; do
|
|
|
|
case "$__addr" in fe[8ab]?:*)
|
|
|
|
continue
|
|
|
|
esac
|
|
|
|
export "$1=$__addr"
|
|
|
|
return 0
|
|
|
|
done
|
|
|
|
|
|
|
|
# Return first item
|
|
|
|
for __addr in $__nets; do
|
|
|
|
export "$1=$__addr"
|
|
|
|
return 0
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
unset "$1"
|
|
|
|
return 1
|
2014-06-19 16:06:44 +00:00
|
|
|
}
|
2012-05-28 03:24:56 +00:00
|
|
|
|
2013-06-17 11:33:36 +00:00
|
|
|
# determine first IPv6 prefix of given logical interface
|
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
2014-06-19 16:06:44 +00:00
|
|
|
network_get_prefix6() {
|
|
|
|
__network_ifstatus "$1" "$2" "['ipv6-prefix'][0]['address','mask']" "/"
|
|
|
|
}
|
2013-01-15 13:07:51 +00:00
|
|
|
|
2013-06-17 11:33:36 +00:00
|
|
|
# determine all IPv4 addresses of given logical interface
|
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
2014-06-19 16:06:44 +00:00
|
|
|
network_get_ipaddrs() {
|
|
|
|
__network_ifstatus "$1" "$2" "['ipv4-address'][*].address"
|
|
|
|
}
|
2013-06-17 11:33:36 +00:00
|
|
|
|
|
|
|
# determine all IPv6 addresses of given logical interface
|
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
2014-06-19 16:06:44 +00:00
|
|
|
network_get_ipaddrs6() {
|
|
|
|
local __addr
|
|
|
|
local __list=""
|
|
|
|
|
2017-06-08 10:02:36 +00:00
|
|
|
if __network_ifstatus "__addr" "$2" "['ipv6-address'][*].address"; then
|
2014-06-19 16:06:44 +00:00
|
|
|
for __addr in $__addr; do
|
2017-06-08 10:02:36 +00:00
|
|
|
__list="${__list:+$__list }${__addr}"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
if __network_ifstatus "__addr" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address"; then
|
|
|
|
for __addr in $__addr; do
|
|
|
|
__list="${__list:+$__list }${__addr}"
|
2014-06-19 16:06:44 +00:00
|
|
|
done
|
2017-06-08 10:02:36 +00:00
|
|
|
fi
|
2014-06-19 16:06:44 +00:00
|
|
|
|
2017-06-08 10:02:36 +00:00
|
|
|
if [ -n "$__list" ]; then
|
2014-06-19 16:06:44 +00:00
|
|
|
export "$1=$__list"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
unset "$1"
|
|
|
|
return 1
|
|
|
|
}
|
2013-06-17 11:33:36 +00:00
|
|
|
|
2014-10-09 07:16:29 +00:00
|
|
|
# determine all IP addresses of given logical interface
|
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
|
|
|
network_get_ipaddrs_all() {
|
2017-06-08 17:27:46 +00:00
|
|
|
local __addr __addr6
|
2014-10-09 07:16:29 +00:00
|
|
|
|
2017-06-08 17:27:46 +00:00
|
|
|
network_get_ipaddrs __addr "$2"
|
|
|
|
network_get_ipaddrs6 __addr6 "$2"
|
2014-10-09 07:16:29 +00:00
|
|
|
|
2017-06-08 17:27:46 +00:00
|
|
|
if [ -n "$__addr" -o -n "$__addr6" ]; then
|
|
|
|
export "$1=${__addr:+$__addr }$__addr6"
|
2014-10-09 07:16:29 +00:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
unset "$1"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2013-06-17 11:33:36 +00:00
|
|
|
# determine all IPv4 subnets of given logical interface
|
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
2014-06-19 16:06:44 +00:00
|
|
|
network_get_subnets() {
|
|
|
|
__network_ifstatus "$1" "$2" "['ipv4-address'][*]['address','mask']" "/ "
|
|
|
|
}
|
2013-06-17 11:33:36 +00:00
|
|
|
|
|
|
|
# determine all IPv6 subnets of given logical interface
|
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
2014-06-19 16:06:44 +00:00
|
|
|
network_get_subnets6() {
|
2017-06-08 17:27:46 +00:00
|
|
|
local __addr __mask
|
2014-06-19 16:06:44 +00:00
|
|
|
local __list=""
|
2012-05-28 03:24:56 +00:00
|
|
|
|
2017-06-08 17:27:46 +00:00
|
|
|
if __network_ifstatus "__addr" "$2" "['ipv6-address'][*]['address','mask']" "/ "; then
|
2014-06-19 16:06:44 +00:00
|
|
|
for __addr in $__addr; do
|
2017-06-08 17:27:46 +00:00
|
|
|
__list="${__list:+$__list }${__addr}"
|
2014-06-19 16:06:44 +00:00
|
|
|
done
|
2017-06-08 17:27:46 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if __network_ifstatus "__addr" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address" && \
|
|
|
|
__network_ifstatus "__mask" "$2" "['ipv6-prefix-assignment'][*].mask"; then
|
|
|
|
for __addr in $__addr; do
|
|
|
|
__list="${__list:+$__list }${__addr}/${__mask%% *}"
|
|
|
|
__mask="${__mask#* }"
|
|
|
|
done
|
|
|
|
fi
|
2012-12-16 18:22:02 +00:00
|
|
|
|
2017-06-08 17:27:46 +00:00
|
|
|
if [ -n "$__list" ]; then
|
2014-06-19 16:06:44 +00:00
|
|
|
export "$1=$__list"
|
|
|
|
return 0
|
2012-12-16 20:17:18 +00:00
|
|
|
fi
|
2012-06-17 11:50:50 +00:00
|
|
|
|
2014-06-19 16:06:44 +00:00
|
|
|
unset "$1"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# determine all IPv6 prefixes of given logical interface
|
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
|
|
|
network_get_prefixes6() {
|
|
|
|
__network_ifstatus "$1" "$2" "['ipv6-prefix'][*]['address','mask']" "/ "
|
2012-06-17 11:50:50 +00:00
|
|
|
}
|
|
|
|
|
2012-12-16 18:22:02 +00:00
|
|
|
# determine IPv4 gateway of given logical interface
|
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
|
|
|
# 3: consider inactive gateway if "true" (optional)
|
2014-06-19 16:06:44 +00:00
|
|
|
network_get_gateway() {
|
|
|
|
__network_ifstatus "$1" "$2" ".route[@.target='0.0.0.0' && !@.table].nexthop" "" 1 && \
|
|
|
|
return 0
|
|
|
|
|
|
|
|
[ "$3" = 1 -o "$3" = "true" ] && \
|
|
|
|
__network_ifstatus "$1" "$2" ".inactive.route[@.target='0.0.0.0' && !@.table].nexthop" "" 1
|
|
|
|
}
|
2012-12-16 18:22:02 +00:00
|
|
|
|
2013-12-09 11:30:14 +00:00
|
|
|
# determine IPv6 gateway of given logical interface
|
2012-12-16 18:22:02 +00:00
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
|
|
|
# 3: consider inactive gateway if "true" (optional)
|
2014-06-19 16:06:44 +00:00
|
|
|
network_get_gateway6() {
|
|
|
|
__network_ifstatus "$1" "$2" ".route[@.target='::' && !@.table].nexthop" "" 1 && \
|
|
|
|
return 0
|
2012-12-16 20:17:18 +00:00
|
|
|
|
2014-06-19 16:06:44 +00:00
|
|
|
[ "$3" = 1 -o "$3" = "true" ] && \
|
|
|
|
__network_ifstatus "$1" "$2" ".inactive.route[@.target='::' && !@.table].nexthop" "" 1
|
2012-06-29 19:04:11 +00:00
|
|
|
}
|
|
|
|
|
2012-12-16 18:22:02 +00:00
|
|
|
# determine the DNS servers of the given logical interface
|
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
|
|
|
# 3: consider inactive servers if "true" (optional)
|
2014-06-19 16:06:44 +00:00
|
|
|
network_get_dnsserver() {
|
|
|
|
__network_ifstatus "$1" "$2" "['dns-server'][*]" && return 0
|
|
|
|
|
|
|
|
[ "$3" = 1 -o "$3" = "true" ] && \
|
|
|
|
__network_ifstatus "$1" "$2" ".inactive['dns-server'][*]"
|
|
|
|
}
|
2012-12-16 18:22:02 +00:00
|
|
|
|
|
|
|
# determine the domains of the given logical interface
|
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
2012-12-16 20:17:18 +00:00
|
|
|
# 3: consider inactive domains if "true" (optional)
|
2014-06-19 16:06:44 +00:00
|
|
|
network_get_dnssearch() {
|
|
|
|
__network_ifstatus "$1" "$2" "['dns-search'][*]" && return 0
|
2012-06-29 19:04:11 +00:00
|
|
|
|
2014-06-19 16:06:44 +00:00
|
|
|
[ "$3" = 1 -o "$3" = "true" ] && \
|
|
|
|
__network_ifstatus "$1" "$2" ".inactive['dns-search'][*]"
|
|
|
|
}
|
2012-06-29 19:04:11 +00:00
|
|
|
|
2014-06-19 16:06:44 +00:00
|
|
|
|
|
|
|
# 1: destination variable
|
|
|
|
# 2: addr
|
|
|
|
# 3: inactive
|
2012-12-16 20:17:18 +00:00
|
|
|
__network_wan()
|
|
|
|
{
|
2014-06-19 16:06:44 +00:00
|
|
|
__network_ifstatus "$1" "" \
|
|
|
|
"[@.route[@.target='$2' && !@.table]].interface" "" 1 && \
|
|
|
|
return 0
|
|
|
|
|
|
|
|
[ "$3" = 1 -o "$3" = "true" ] && \
|
|
|
|
__network_ifstatus "$1" "" \
|
|
|
|
"[@.inactive.route[@.target='$2' && !@.table]].interface" "" 1
|
2012-06-17 11:50:50 +00:00
|
|
|
}
|
|
|
|
|
2012-12-16 18:22:02 +00:00
|
|
|
# find the logical interface which holds the current IPv4 default route
|
|
|
|
# 1: destination variable
|
2012-12-16 20:17:18 +00:00
|
|
|
# 2: consider inactive default routes if "true" (optional)
|
2014-06-19 16:06:44 +00:00
|
|
|
network_find_wan() { __network_wan "$1" "0.0.0.0" "$2"; }
|
2012-12-16 18:22:02 +00:00
|
|
|
|
|
|
|
# find the logical interface which holds the current IPv6 default route
|
|
|
|
# 1: destination variable
|
2012-12-16 20:17:18 +00:00
|
|
|
# 2: consider inactive dafault routes if "true" (optional)
|
2014-06-19 16:06:44 +00:00
|
|
|
network_find_wan6() { __network_wan "$1" "::" "$2"; }
|
2012-05-29 00:20:16 +00:00
|
|
|
|
2012-12-16 18:22:02 +00:00
|
|
|
# test whether the given logical interface is running
|
|
|
|
# 1: interface
|
2012-05-29 02:03:25 +00:00
|
|
|
network_is_up()
|
|
|
|
{
|
|
|
|
local __up
|
2014-06-19 16:06:44 +00:00
|
|
|
__network_ifstatus "__up" "$1" ".up" && [ "$__up" = 1 ]
|
2012-05-29 02:03:25 +00:00
|
|
|
}
|
|
|
|
|
2013-12-16 16:28:45 +00:00
|
|
|
# determine the protocol of the given logical interface
|
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
2014-06-19 16:06:44 +00:00
|
|
|
network_get_protocol() { __network_ifstatus "$1" "$2" ".proto"; }
|
2013-12-16 16:28:45 +00:00
|
|
|
|
2012-12-16 18:22:02 +00:00
|
|
|
# determine the layer 3 linux network device of the given logical interface
|
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
2014-06-19 16:06:44 +00:00
|
|
|
network_get_device() { __network_ifstatus "$1" "$2" ".l3_device"; }
|
2012-12-16 18:22:02 +00:00
|
|
|
|
|
|
|
# determine the layer 2 linux network device of the given logical interface
|
|
|
|
# 1: destination variable
|
|
|
|
# 2: interface
|
2014-06-19 16:06:44 +00:00
|
|
|
network_get_physdev() { __network_ifstatus "$1" "$2" ".device"; }
|
2012-06-07 14:55:26 +00:00
|
|
|
|
2012-12-16 18:22:02 +00:00
|
|
|
# defer netifd actions on the given linux network device
|
|
|
|
# 1: device name
|
2014-06-19 16:06:44 +00:00
|
|
|
network_defer_device()
|
|
|
|
{
|
|
|
|
ubus call network.device set_state \
|
|
|
|
"$(printf '{ "name": "%s", "defer": true }' "$1")" 2>/dev/null
|
|
|
|
}
|
2012-12-16 18:22:02 +00:00
|
|
|
|
|
|
|
# continue netifd actions on the given linux network device
|
|
|
|
# 1: device name
|
2014-06-19 16:06:44 +00:00
|
|
|
network_ready_device()
|
2012-12-16 18:22:02 +00:00
|
|
|
{
|
2014-06-19 16:06:44 +00:00
|
|
|
ubus call network.device set_state \
|
|
|
|
"$(printf '{ "name": "%s", "defer": false }' "$1")" 2>/dev/null
|
2012-12-16 18:22:02 +00:00
|
|
|
}
|
2014-06-19 16:06:44 +00:00
|
|
|
|
|
|
|
# flush the internal value cache to force re-reading values from ubus
|
|
|
|
network_flush_cache() { unset __NETWORK_CACHE; }
|