network.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. # 1: destination variable
  2. # 2: interface
  3. # 3: path
  4. # 4: separator
  5. # 5: limit
  6. __network_ifstatus() {
  7. local __tmp
  8. [ -z "$__NETWORK_CACHE" ] && {
  9. __tmp="$(ubus call network.interface dump 2>&1)"
  10. case "$?" in
  11. 4) : ;;
  12. 0) export __NETWORK_CACHE="$__tmp" ;;
  13. *) echo "$__tmp" >&2 ;;
  14. esac
  15. }
  16. __tmp="$(jsonfilter ${4:+-F "$4"} ${5:+-l "$5"} -s "${__NETWORK_CACHE:-{}}" -e "$1=@.interface${2:+[@.interface='$2']}$3")"
  17. [ -z "$__tmp" ] && \
  18. unset "$1" && \
  19. return 1
  20. eval "$__tmp"
  21. }
  22. # determine first IPv4 address of given logical interface
  23. # 1: destination variable
  24. # 2: interface
  25. network_get_ipaddr() {
  26. __network_ifstatus "$1" "$2" "['ipv4-address'][0].address";
  27. }
  28. # determine first IPv6 address of given logical interface
  29. # 1: destination variable
  30. # 2: interface
  31. network_get_ipaddr6() {
  32. __network_ifstatus "$1" "$2" "['ipv6-address'][0].address" || \
  33. __network_ifstatus "$1" "$2" "['ipv6-prefix-assignment'][0]['local-address'].address" || \
  34. return 1
  35. }
  36. # determine first IPv4 subnet of given logical interface
  37. # 1: destination variable
  38. # 2: interface
  39. network_get_subnet() {
  40. __network_ifstatus "$1" "$2" "['ipv4-address'][0]['address','mask']" "/"
  41. }
  42. # determine first IPv6 subnet of given logical interface
  43. # 1: destination variable
  44. # 2: interface
  45. network_get_subnet6() {
  46. local __nets __addr
  47. if network_get_subnets6 __nets "$2"; then
  48. # Attempt to return first non-fe80::/10, non-fc::/7 range
  49. for __addr in $__nets; do
  50. case "$__addr" in fe[8ab]?:*|f[cd]??:*)
  51. continue
  52. esac
  53. export "$1=$__addr"
  54. return 0
  55. done
  56. # Attempt to return first non-fe80::/10 range
  57. for __addr in $__nets; do
  58. case "$__addr" in fe[8ab]?:*)
  59. continue
  60. esac
  61. export "$1=$__addr"
  62. return 0
  63. done
  64. # Return first item
  65. for __addr in $__nets; do
  66. export "$1=$__addr"
  67. return 0
  68. done
  69. fi
  70. unset "$1"
  71. return 1
  72. }
  73. # determine first IPv6 prefix of given logical interface
  74. # 1: destination variable
  75. # 2: interface
  76. network_get_prefix6() {
  77. __network_ifstatus "$1" "$2" "['ipv6-prefix'][0]['address','mask']" "/"
  78. }
  79. # determine all IPv4 addresses of given logical interface
  80. # 1: destination variable
  81. # 2: interface
  82. network_get_ipaddrs() {
  83. __network_ifstatus "$1" "$2" "['ipv4-address'][*].address"
  84. }
  85. # determine all IPv6 addresses of given logical interface
  86. # 1: destination variable
  87. # 2: interface
  88. network_get_ipaddrs6() {
  89. local __addr
  90. local __list=""
  91. if __network_ifstatus "__addr" "$2" "['ipv6-address'][*].address"; then
  92. for __addr in $__addr; do
  93. __list="${__list:+$__list }${__addr}"
  94. done
  95. fi
  96. if __network_ifstatus "__addr" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address"; then
  97. for __addr in $__addr; do
  98. __list="${__list:+$__list }${__addr}"
  99. done
  100. fi
  101. if [ -n "$__list" ]; then
  102. export "$1=$__list"
  103. return 0
  104. fi
  105. unset "$1"
  106. return 1
  107. }
  108. # determine all IP addresses of given logical interface
  109. # 1: destination variable
  110. # 2: interface
  111. network_get_ipaddrs_all() {
  112. local __addr __addr6
  113. network_get_ipaddrs __addr "$2"
  114. network_get_ipaddrs6 __addr6 "$2"
  115. if [ -n "$__addr" -o -n "$__addr6" ]; then
  116. export "$1=${__addr:+$__addr }$__addr6"
  117. return 0
  118. fi
  119. unset "$1"
  120. return 1
  121. }
  122. # determine all IPv4 subnets of given logical interface
  123. # 1: destination variable
  124. # 2: interface
  125. network_get_subnets() {
  126. __network_ifstatus "$1" "$2" "['ipv4-address'][*]['address','mask']" "/ "
  127. }
  128. # determine all IPv6 subnets of given logical interface
  129. # 1: destination variable
  130. # 2: interface
  131. network_get_subnets6() {
  132. local __addr __mask
  133. local __list=""
  134. if __network_ifstatus "__addr" "$2" "['ipv6-address'][*]['address','mask']" "/ "; then
  135. for __addr in $__addr; do
  136. __list="${__list:+$__list }${__addr}"
  137. done
  138. fi
  139. if __network_ifstatus "__addr" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address" && \
  140. __network_ifstatus "__mask" "$2" "['ipv6-prefix-assignment'][*].mask"; then
  141. for __addr in $__addr; do
  142. __list="${__list:+$__list }${__addr}/${__mask%% *}"
  143. __mask="${__mask#* }"
  144. done
  145. fi
  146. if [ -n "$__list" ]; then
  147. export "$1=$__list"
  148. return 0
  149. fi
  150. unset "$1"
  151. return 1
  152. }
  153. # determine all IPv6 prefixes of given logical interface
  154. # 1: destination variable
  155. # 2: interface
  156. network_get_prefixes6() {
  157. __network_ifstatus "$1" "$2" "['ipv6-prefix'][*]['address','mask']" "/ "
  158. }
  159. # determine IPv4 gateway of given logical interface
  160. # 1: destination variable
  161. # 2: interface
  162. # 3: consider inactive gateway if "true" (optional)
  163. network_get_gateway() {
  164. __network_ifstatus "$1" "$2" ".route[@.target='0.0.0.0' && !@.table].nexthop" "" 1 && \
  165. return 0
  166. [ "$3" = 1 -o "$3" = "true" ] && \
  167. __network_ifstatus "$1" "$2" ".inactive.route[@.target='0.0.0.0' && !@.table].nexthop" "" 1
  168. }
  169. # determine IPv6 gateway of given logical interface
  170. # 1: destination variable
  171. # 2: interface
  172. # 3: consider inactive gateway if "true" (optional)
  173. network_get_gateway6() {
  174. __network_ifstatus "$1" "$2" ".route[@.target='::' && !@.table].nexthop" "" 1 && \
  175. return 0
  176. [ "$3" = 1 -o "$3" = "true" ] && \
  177. __network_ifstatus "$1" "$2" ".inactive.route[@.target='::' && !@.table].nexthop" "" 1
  178. }
  179. # determine the DNS servers of the given logical interface
  180. # 1: destination variable
  181. # 2: interface
  182. # 3: consider inactive servers if "true" (optional)
  183. network_get_dnsserver() {
  184. __network_ifstatus "$1" "$2" "['dns-server'][*]" && return 0
  185. [ "$3" = 1 -o "$3" = "true" ] && \
  186. __network_ifstatus "$1" "$2" ".inactive['dns-server'][*]"
  187. }
  188. # determine the domains of the given logical interface
  189. # 1: destination variable
  190. # 2: interface
  191. # 3: consider inactive domains if "true" (optional)
  192. network_get_dnssearch() {
  193. __network_ifstatus "$1" "$2" "['dns-search'][*]" && return 0
  194. [ "$3" = 1 -o "$3" = "true" ] && \
  195. __network_ifstatus "$1" "$2" ".inactive['dns-search'][*]"
  196. }
  197. # 1: destination variable
  198. # 2: addr
  199. # 3: inactive
  200. __network_wan()
  201. {
  202. __network_ifstatus "$1" "" \
  203. "[@.route[@.target='$2' && !@.table]].interface" "" 1 && \
  204. return 0
  205. [ "$3" = 1 -o "$3" = "true" ] && \
  206. __network_ifstatus "$1" "" \
  207. "[@.inactive.route[@.target='$2' && !@.table]].interface" "" 1
  208. }
  209. # find the logical interface which holds the current IPv4 default route
  210. # 1: destination variable
  211. # 2: consider inactive default routes if "true" (optional)
  212. network_find_wan() { __network_wan "$1" "0.0.0.0" "$2"; }
  213. # find the logical interface which holds the current IPv6 default route
  214. # 1: destination variable
  215. # 2: consider inactive dafault routes if "true" (optional)
  216. network_find_wan6() { __network_wan "$1" "::" "$2"; }
  217. # test whether the given logical interface is running
  218. # 1: interface
  219. network_is_up()
  220. {
  221. local __up
  222. __network_ifstatus "__up" "$1" ".up" && [ "$__up" = 1 ]
  223. }
  224. # determine the protocol of the given logical interface
  225. # 1: destination variable
  226. # 2: interface
  227. network_get_protocol() { __network_ifstatus "$1" "$2" ".proto"; }
  228. # determine the uptime of the given logical interface
  229. # 1: destination variable
  230. # 2: interface
  231. network_get_uptime() { __network_ifstatus "$1" "$2" ".uptime"; }
  232. # determine the metric of the given logical interface
  233. # 1: destination variable
  234. # 2: interface
  235. network_get_metric() { __network_ifstatus "$1" "$2" ".metric"; }
  236. # determine the layer 3 linux network device of the given logical interface
  237. # 1: destination variable
  238. # 2: interface
  239. network_get_device() { __network_ifstatus "$1" "$2" ".l3_device"; }
  240. # determine the layer 2 linux network device of the given logical interface
  241. # 1: destination variable
  242. # 2: interface
  243. network_get_physdev() { __network_ifstatus "$1" "$2" ".device"; }
  244. # defer netifd actions on the given linux network device
  245. # 1: device name
  246. network_defer_device()
  247. {
  248. ubus call network.device set_state \
  249. "$(printf '{ "name": "%s", "defer": true }' "$1")" 2>/dev/null
  250. }
  251. # continue netifd actions on the given linux network device
  252. # 1: device name
  253. network_ready_device()
  254. {
  255. ubus call network.device set_state \
  256. "$(printf '{ "name": "%s", "defer": false }' "$1")" 2>/dev/null
  257. }
  258. # flush the internal value cache to force re-reading values from ubus
  259. network_flush_cache() { unset __NETWORK_CACHE; }