network.sh 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 "[email protected]${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 first IPv6 prefix assignment of given logical interface
  80. # 1: destination variable
  81. # 2: interface
  82. network_get_prefix_assignment6() {
  83. __network_ifstatus "$1" "$2" "['ipv6-prefix-assignment'][0]['address','mask']" "/"
  84. }
  85. # determine all IPv4 addresses of given logical interface
  86. # 1: destination variable
  87. # 2: interface
  88. network_get_ipaddrs() {
  89. __network_ifstatus "$1" "$2" "['ipv4-address'][*].address"
  90. }
  91. # determine all IPv6 addresses of given logical interface
  92. # 1: destination variable
  93. # 2: interface
  94. network_get_ipaddrs6() {
  95. local __addr
  96. local __list=""
  97. if __network_ifstatus "__addr" "$2" "['ipv6-address'][*].address"; then
  98. for __addr in $__addr; do
  99. __list="${__list:+$__list }${__addr}"
  100. done
  101. fi
  102. if __network_ifstatus "__addr" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address"; then
  103. for __addr in $__addr; do
  104. __list="${__list:+$__list }${__addr}"
  105. done
  106. fi
  107. if [ -n "$__list" ]; then
  108. export "$1=$__list"
  109. return 0
  110. fi
  111. unset "$1"
  112. return 1
  113. }
  114. # determine all IP addresses of given logical interface
  115. # 1: destination variable
  116. # 2: interface
  117. network_get_ipaddrs_all() {
  118. local __addr __addr6
  119. network_get_ipaddrs __addr "$2"
  120. network_get_ipaddrs6 __addr6 "$2"
  121. if [ -n "$__addr" -o -n "$__addr6" ]; then
  122. export "$1=${__addr:+$__addr }$__addr6"
  123. return 0
  124. fi
  125. unset "$1"
  126. return 1
  127. }
  128. # determine all IPv4 subnets of given logical interface
  129. # 1: destination variable
  130. # 2: interface
  131. network_get_subnets() {
  132. __network_ifstatus "$1" "$2" "['ipv4-address'][*]['address','mask']" "/ "
  133. }
  134. # determine all IPv6 subnets of given logical interface
  135. # 1: destination variable
  136. # 2: interface
  137. network_get_subnets6() {
  138. local __addr __mask
  139. local __list=""
  140. if __network_ifstatus "__addr" "$2" "['ipv6-address'][*]['address','mask']" "/ "; then
  141. for __addr in $__addr; do
  142. __list="${__list:+$__list }${__addr}"
  143. done
  144. fi
  145. if __network_ifstatus "__addr" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address" && \
  146. __network_ifstatus "__mask" "$2" "['ipv6-prefix-assignment'][*].mask"; then
  147. for __addr in $__addr; do
  148. __list="${__list:+$__list }${__addr}/${__mask%% *}"
  149. __mask="${__mask#* }"
  150. done
  151. fi
  152. if [ -n "$__list" ]; then
  153. export "$1=$__list"
  154. return 0
  155. fi
  156. unset "$1"
  157. return 1
  158. }
  159. # determine all IPv6 prefixes of given logical interface
  160. # 1: destination variable
  161. # 2: interface
  162. network_get_prefixes6() {
  163. __network_ifstatus "$1" "$2" "['ipv6-prefix'][*]['address','mask']" "/ "
  164. }
  165. # determine all IPv6 prefix assignments of given logical interface
  166. # 1: destination variable
  167. # 2: interface
  168. network_get_prefix_assignments6() {
  169. __network_ifstatus "$1" "$2" "['ipv6-prefix-assignment'][*]['address','mask']" "/ "
  170. }
  171. # determine IPv4 gateway of given logical interface
  172. # 1: destination variable
  173. # 2: interface
  174. # 3: consider inactive gateway if "true" (optional)
  175. network_get_gateway() {
  176. __network_ifstatus "$1" "$2" ".route[@.target='0.0.0.0' && [email protected]].nexthop" "" 1 && \
  177. return 0
  178. [ "$3" = 1 -o "$3" = "true" ] && \
  179. __network_ifstatus "$1" "$2" ".inactive.route[@.target='0.0.0.0' && [email protected]].nexthop" "" 1
  180. }
  181. # determine IPv6 gateway of given logical interface
  182. # 1: destination variable
  183. # 2: interface
  184. # 3: consider inactive gateway if "true" (optional)
  185. network_get_gateway6() {
  186. __network_ifstatus "$1" "$2" ".route[@.target='::' && [email protected]].nexthop" "" 1 && \
  187. return 0
  188. [ "$3" = 1 -o "$3" = "true" ] && \
  189. __network_ifstatus "$1" "$2" ".inactive.route[@.target='::' && [email protected]].nexthop" "" 1
  190. }
  191. # determine the DNS servers of the given logical interface
  192. # 1: destination variable
  193. # 2: interface
  194. # 3: consider inactive servers if "true" (optional)
  195. network_get_dnsserver() {
  196. __network_ifstatus "$1" "$2" "['dns-server'][*]" && return 0
  197. [ "$3" = 1 -o "$3" = "true" ] && \
  198. __network_ifstatus "$1" "$2" ".inactive['dns-server'][*]"
  199. }
  200. # determine the domains of the given logical interface
  201. # 1: destination variable
  202. # 2: interface
  203. # 3: consider inactive domains if "true" (optional)
  204. network_get_dnssearch() {
  205. __network_ifstatus "$1" "$2" "['dns-search'][*]" && return 0
  206. [ "$3" = 1 -o "$3" = "true" ] && \
  207. __network_ifstatus "$1" "$2" ".inactive['dns-search'][*]"
  208. }
  209. # 1: destination variable
  210. # 2: addr
  211. # 3: inactive
  212. __network_wan()
  213. {
  214. __network_ifstatus "$1" "" \
  215. "[@.route[@.target='$2' && [email protected]]].interface" "" 1 && \
  216. return 0
  217. [ "$3" = 1 -o "$3" = "true" ] && \
  218. __network_ifstatus "$1" "" \
  219. "[@.inactive.route[@.target='$2' && [email protected]]].interface" "" 1
  220. }
  221. # find the logical interface which holds the current IPv4 default route
  222. # 1: destination variable
  223. # 2: consider inactive default routes if "true" (optional)
  224. network_find_wan() { __network_wan "$1" "0.0.0.0" "$2"; }
  225. # find the logical interface which holds the current IPv6 default route
  226. # 1: destination variable
  227. # 2: consider inactive default routes if "true" (optional)
  228. network_find_wan6() { __network_wan "$1" "::" "$2"; }
  229. # test whether the given logical interface is running
  230. # 1: interface
  231. network_is_up()
  232. {
  233. local __up
  234. __network_ifstatus "__up" "$1" ".up" && [ "$__up" = 1 ]
  235. }
  236. # determine the protocol of the given logical interface
  237. # 1: destination variable
  238. # 2: interface
  239. network_get_protocol() { __network_ifstatus "$1" "$2" ".proto"; }
  240. # determine the uptime of the given logical interface
  241. # 1: destination variable
  242. # 2: interface
  243. network_get_uptime() { __network_ifstatus "$1" "$2" ".uptime"; }
  244. # determine the metric of the given logical interface
  245. # 1: destination variable
  246. # 2: interface
  247. network_get_metric() { __network_ifstatus "$1" "$2" ".metric"; }
  248. # determine the layer 3 linux network device of the given logical interface
  249. # 1: destination variable
  250. # 2: interface
  251. network_get_device() { __network_ifstatus "$1" "$2" ".l3_device"; }
  252. # determine the layer 2 linux network device of the given logical interface
  253. # 1: destination variable
  254. # 2: interface
  255. network_get_physdev() { __network_ifstatus "$1" "$2" ".device"; }
  256. # defer netifd actions on the given linux network device
  257. # 1: device name
  258. network_defer_device()
  259. {
  260. ubus call network.device set_state \
  261. "$(printf '{ "name": "%s", "defer": true }' "$1")" 2>/dev/null
  262. }
  263. # continue netifd actions on the given linux network device
  264. # 1: device name
  265. network_ready_device()
  266. {
  267. ubus call network.device set_state \
  268. "$(printf '{ "name": "%s", "defer": false }' "$1")" 2>/dev/null
  269. }
  270. # flush the internal value cache to force re-reading values from ubus
  271. network_flush_cache() { unset __NETWORK_CACHE; }