1
0

network.sh 7.4 KB

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