2
0

network.sh 6.9 KB

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