network.sh 8.5 KB

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