config_generate 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #!/bin/sh
  2. CFG=/etc/board.json
  3. . /usr/share/libubox/jshn.sh
  4. [ -f $CFG ] || exit 1
  5. generate_static_network() {
  6. uci -q batch <<-EOF
  7. delete network.loopback
  8. set network.loopback='interface'
  9. set network.loopback.ifname='lo'
  10. set network.loopback.proto='static'
  11. set network.loopback.ipaddr='127.0.0.1'
  12. set network.loopback.netmask='255.0.0.0'
  13. delete network.globals
  14. set network.globals='globals'
  15. set network.globals.ula_prefix='auto'
  16. EOF
  17. if json_is_a dsl object; then
  18. json_select dsl
  19. if json_is_a atmbridge object; then
  20. json_select atmbridge
  21. local vpi vci encaps payload
  22. json_get_vars vpi vci encaps payload
  23. uci -q batch <<-EOF
  24. delete network.atm
  25. set network.atm='atm-bridge'
  26. set network.atm.vpi='$vpi'
  27. set network.atm.vci='$vci'
  28. set network.atm.encaps='$encaps'
  29. set network.atm.payload='$payload'
  30. EOF
  31. json_select ..
  32. fi
  33. if json_is_a modem object; then
  34. json_select modem
  35. local type annex firmware tone xfer_mode
  36. json_get_vars type annex firmware tone xfer_mode
  37. uci -q batch <<-EOF
  38. delete network.dsl
  39. set network.dsl='dsl'
  40. set network.dsl.annex='$annex'
  41. set network.dsl.firmware='$firmware'
  42. set network.dsl.tone='$tone'
  43. set network.dsl.xfer_mode='$xfer_mode'
  44. EOF
  45. json_select ..
  46. fi
  47. json_select ..
  48. fi
  49. }
  50. addr_offset=2
  51. generate_network() {
  52. local ifname macaddr protocol type ipaddr netmask
  53. json_select network
  54. json_select "$1"
  55. json_get_vars ifname macaddr protocol ipaddr netmask
  56. json_select ..
  57. json_select ..
  58. [ -n "$ifname" ] || return
  59. # force bridge for multi-interface devices (and lan)
  60. case "$1:$ifname" in
  61. *\ * | lan:*) type="bridge" ;;
  62. esac
  63. uci -q batch <<-EOF
  64. delete network.$1
  65. set network.$1='interface'
  66. set network.$1.type='$type'
  67. set network.$1.ifname='$ifname'
  68. set network.$1.proto='none'
  69. EOF
  70. [ -n "$macaddr" ] && uci -q batch <<-EOF
  71. delete network.$1_dev
  72. set network.$1_dev='device'
  73. set network.$1_dev.name='$ifname'
  74. set network.$1_dev.macaddr='$macaddr'
  75. EOF
  76. case "$protocol" in
  77. static)
  78. local ipad
  79. case "$1" in
  80. lan) ipad=${ipaddr:-"192.168.1.1"} ;;
  81. *) ipad=${ipaddr:-"192.168.$((addr_offset++)).1"} ;;
  82. esac
  83. netm=${netmask:-"255.255.255.0"}
  84. uci -q batch <<-EOF
  85. set network.$1.proto='static'
  86. set network.$1.ipaddr='$ipad'
  87. set network.$1.netmask='$netm'
  88. set network.$1.ip6assign='60'
  89. EOF
  90. ;;
  91. dhcp)
  92. # fixup IPv6 slave interface if parent is a bridge
  93. [ "$type" = "bridge" ] && ifname="br-$1"
  94. uci -q batch <<-EOF
  95. set network.$1.proto='dhcp'
  96. delete network.${1}6
  97. set network.${1}6='interface'
  98. set network.${1}6.ifname='$ifname'
  99. set network.${1}6.proto='dhcpv6'
  100. EOF
  101. ;;
  102. pppoe)
  103. # fixup IPv6 slave interface
  104. ifname="pppoe-$1"
  105. uci -q batch <<-EOF
  106. set network.$1.proto='pppoe'
  107. set network.$1.username='username'
  108. set network.$1.password='password'
  109. set network.$1.ipv6='auto'
  110. delete network.${1}6
  111. set network.${1}6='interface'
  112. set network.${1}6.ifname='$ifname'
  113. set network.${1}6.proto='dhcpv6'
  114. EOF
  115. ;;
  116. esac
  117. }
  118. generate_switch_vlans_ports() {
  119. local switch="$1"
  120. local port ports role roles num attr val
  121. #
  122. # autogenerate vlans
  123. #
  124. json_get_keys roles roles
  125. json_select roles
  126. for role in $roles; do
  127. json_select "$role"
  128. json_get_vars ports
  129. json_select ..
  130. uci -q batch <<-EOF
  131. add network switch_vlan
  132. set network.@switch_vlan[-1].device='$switch'
  133. set network.@switch_vlan[-1].vlan='$role'
  134. set network.@switch_vlan[-1].ports='$ports'
  135. EOF
  136. done
  137. json_select ..
  138. #
  139. # write port specific settings
  140. #
  141. json_get_keys ports ports
  142. json_select ports
  143. for port in $ports; do
  144. json_select "$port"
  145. json_get_vars num
  146. if json_is_a attr object; then
  147. json_get_keys attr attr
  148. json_select attr
  149. uci -q batch <<-EOF
  150. add network switch_port
  151. set network.@switch_port[-1].device='$switch'
  152. set network.@switch_port[-1].port=$num
  153. EOF
  154. for attr in $attr; do
  155. json_get_var val "$attr"
  156. uci -q set network.@switch_port[-1].$attr="$val"
  157. done
  158. json_select ..
  159. fi
  160. json_select ..
  161. done
  162. json_select ..
  163. }
  164. generate_switch() {
  165. local key="$1"
  166. local vlans
  167. json_select switch
  168. json_select "$key"
  169. json_get_vars enable reset blinkrate cpu_port
  170. uci -q batch <<-EOF
  171. add network switch
  172. set network.@switch[-1].name='$key'
  173. set network.@switch[-1].reset='$reset'
  174. set network.@switch[-1].enable_vlan='$enable'
  175. set network.@switch[-1].blinkrate='$blinkrate'
  176. EOF
  177. generate_switch_vlans_ports "$1"
  178. json_select ..
  179. json_select ..
  180. }
  181. generate_rssimon() {
  182. local key="$1"
  183. local cfg="rssid_$key"
  184. local refresh threshold
  185. json_select rssimon
  186. json_select "$key"
  187. json_get_vars refresh threshold
  188. json_select ..
  189. json_select ..
  190. uci -q batch <<-EOF
  191. delete system.$cfg
  192. set system.$cfg='rssid'
  193. set system.$cfg.dev='$key'
  194. set system.$cfg.refresh='$refresh'
  195. set system.$cfg.threshold='$threshold'
  196. EOF
  197. }
  198. generate_led() {
  199. local key="$1"
  200. local cfg="led_$key"
  201. json_select led
  202. json_select "$key"
  203. json_get_vars name sysfs type trigger default
  204. uci -q batch <<-EOF
  205. delete system.$cfg
  206. set system.$cfg='led'
  207. set system.$cfg.name='$name'
  208. set system.$cfg.sysfs='$sysfs'
  209. set system.$cfg.trigger='$trigger'
  210. set system.$cfg.default='$default'
  211. EOF
  212. case "$type" in
  213. gpio)
  214. local gpio inverted
  215. json_get_vars gpio inverted
  216. uci -q batch <<-EOF
  217. set system.$cfg.trigger='gpio'
  218. set system.$cfg.gpio='$gpio'
  219. set system.$cfg.inverted='$inverted'
  220. EOF
  221. ;;
  222. netdev)
  223. local device mode
  224. json_get_vars device mode
  225. uci -q batch <<-EOF
  226. set system.$cfg.trigger='netdev'
  227. set system.$cfg.mode='$mode'
  228. set system.$cfg.dev='$device'
  229. EOF
  230. ;;
  231. usb)
  232. local device
  233. json_get_vars device
  234. uci -q batch <<-EOF
  235. set system.$cfg.trigger='usbdev'
  236. set system.$cfg.interval='50'
  237. set system.$cfg.dev='$device'
  238. EOF
  239. ;;
  240. rssi)
  241. local iface minq maxq offset factor
  242. json_get_vars iface minq maxq offset factor
  243. uci -q batch <<-EOF
  244. set system.$cfg.trigger='rssi'
  245. set system.$cfg.iface='rssid_$iface'
  246. set system.$cfg.minq='$minq'
  247. set system.$cfg.maxq='$maxq'
  248. set system.$cfg.offset='$offset'
  249. set system.$cfg.factor='$factor'
  250. EOF
  251. ;;
  252. switch)
  253. local port_mask speed_mask
  254. json_get_vars port_mask speed_mask
  255. uci -q batch <<-EOF
  256. set system.$cfg.port_mask='$port_mask'
  257. set system.$cfg.speed_mask='$speed_mask'
  258. EOF
  259. ;;
  260. timer|oneshot)
  261. local delayon delayoff
  262. json_get_vars delayon delayoff
  263. uci -q batch <<-EOF
  264. set system.$cfg.trigger='$type'
  265. set system.$cfg.delayon='$delayon'
  266. set system.$cfg.delayoff='$delayoff'
  267. EOF
  268. ;;
  269. esac
  270. json_select ..
  271. json_select ..
  272. }
  273. generate_gpioswitch() {
  274. local cfg="$1"
  275. json_select gpioswitch
  276. json_select "$cfg"
  277. local name pin default
  278. json_get_vars name pin default
  279. uci -q batch <<-EOF
  280. delete system.$cfg
  281. set system.$cfg='gpio_switch'
  282. set system.$cfg.name='$name'
  283. set system.$cfg.gpio_pin='$pin'
  284. set system.$cfg.default='$default'
  285. EOF
  286. json_select ..
  287. json_select ..
  288. }
  289. json_init
  290. json_load "$(cat ${CFG})"
  291. generate_static_network
  292. json_get_keys keys network
  293. for key in $keys; do generate_network $key; done
  294. json_get_keys keys switch
  295. for key in $keys; do generate_switch $key; done
  296. json_get_keys keys rssimon
  297. for key in $keys; do generate_rssimon $key; done
  298. json_get_keys keys gpioswitch
  299. for key in $keys; do generate_gpioswitch $key; done
  300. json_get_keys keys led
  301. for key in $keys; do generate_led $key; done
  302. uci commit