gre.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #!/bin/sh
  2. [ -n "$INCLUDE_ONLY" ] || {
  3. . /lib/functions.sh
  4. . /lib/functions/network.sh
  5. . ../netifd-proto.sh
  6. init_proto "$@"
  7. }
  8. gre_generic_setup() {
  9. local cfg="$1"
  10. local mode="$2"
  11. local local="$3"
  12. local remote="$4"
  13. local link="$5"
  14. local mtu ttl tos zone ikey okey icsum ocsum iseqno oseqno multicast
  15. json_get_vars mtu ttl tos zone ikey okey icsum ocsum iseqno oseqno multicast
  16. [ -z "$zone" ] && zone="wan"
  17. [ -z "$multicast" ] && multicast=1
  18. proto_init_update "$link" 1
  19. proto_add_tunnel
  20. json_add_string mode "$mode"
  21. json_add_int mtu "${mtu:-1280}"
  22. [ -n "$df" ] && json_add_boolean df "$df"
  23. [ -n "$ttl" ] && json_add_int ttl "$ttl"
  24. [ -n "$tos" ] && json_add_string tos "$tos"
  25. json_add_boolean multicast "$multicast"
  26. json_add_string local "$local"
  27. json_add_string remote "$remote"
  28. [ -n "$tunlink" ] && json_add_string link "$tunlink"
  29. json_add_object 'data'
  30. [ -n "$ikey" ] && json_add_int ikey "$ikey"
  31. [ -n "$okey" ] && json_add_int okey "$okey"
  32. [ -n "$icsum" ] && json_add_boolean icsum "$icsum"
  33. [ -n "$ocsum" ] && json_add_boolean ocsum "$ocsum"
  34. [ -n "$iseqno" ] && json_add_boolean iseqno "$iseqno"
  35. [ -n "$oseqno" ] && json_add_boolean oseqno "$oseqno"
  36. [ -n "$encaplimit" ] && json_add_string encaplimit "$encaplimit"
  37. json_close_object
  38. proto_close_tunnel
  39. proto_add_data
  40. [ -n "$zone" ] && json_add_string zone "$zone"
  41. proto_close_data
  42. proto_send_update "$cfg"
  43. }
  44. gre_setup() {
  45. local cfg="$1"
  46. local mode="$2"
  47. local remoteip
  48. local ipaddr peeraddr
  49. json_get_vars df ipaddr peeraddr tunlink
  50. [ -z "$peeraddr" ] && {
  51. proto_notify_error "$cfg" "MISSING_PEER_ADDRESS"
  52. proto_block_restart "$cfg"
  53. exit
  54. }
  55. remoteip=$(resolveip -t 10 -4 "$peeraddr")
  56. if [ -z "$remoteip" ]; then
  57. proto_notify_error "$cfg" "PEER_RESOLVE_FAIL"
  58. exit
  59. fi
  60. for ip in $remoteip; do
  61. peeraddr=$ip
  62. break
  63. done
  64. ( proto_add_host_dependency "$cfg" "$peeraddr" "$tunlink" )
  65. [ -z "$ipaddr" ] && {
  66. local wanif="$tunlink"
  67. if [ -z $wanif ] && ! network_find_wan wanif; then
  68. proto_notify_error "$cfg" "NO_WAN_LINK"
  69. exit
  70. fi
  71. if ! network_get_ipaddr ipaddr "$wanif"; then
  72. proto_notify_error "$cfg" "NO_WAN_LINK"
  73. exit
  74. fi
  75. }
  76. [ -z "$df" ] && df="1"
  77. case "$mode" in
  78. gretapip)
  79. gre_generic_setup $cfg $mode $ipaddr $peeraddr "gre4t-$cfg"
  80. ;;
  81. *)
  82. gre_generic_setup $cfg $mode $ipaddr $peeraddr "gre4-$cfg"
  83. ;;
  84. esac
  85. }
  86. proto_gre_setup() {
  87. local cfg="$1"
  88. gre_setup $cfg "greip"
  89. }
  90. proto_gretap_setup() {
  91. local cfg="$1"
  92. local network
  93. json_get_vars network
  94. gre_setup $cfg "gretapip"
  95. json_init
  96. json_add_string name "gre4t-$cfg"
  97. json_add_boolean link-ext 0
  98. json_close_object
  99. for i in $network; do
  100. ubus call network.interface."$i" add_device "$(json_dump)"
  101. done
  102. }
  103. grev6_setup() {
  104. local cfg="$1"
  105. local mode="$2"
  106. local remoteip6
  107. local ip6addr peer6addr weakif
  108. json_get_vars ip6addr peer6addr tunlink weakif encaplimit
  109. [ -z "$peer6addr" ] && {
  110. proto_notify_error "$cfg" "MISSING_PEER_ADDRESS"
  111. proto_block_restart "$cfg"
  112. exit
  113. }
  114. remoteip6=$(resolveip -t 10 -6 "$peer6addr")
  115. if [ -z "$remoteip6" ]; then
  116. proto_notify_error "$cfg" "PEER_RESOLVE_FAIL"
  117. exit
  118. fi
  119. for ip6 in $remoteip6; do
  120. peer6addr=$ip6
  121. break
  122. done
  123. ( proto_add_host_dependency "$cfg" "$peer6addr" "$tunlink" )
  124. [ -z "$ip6addr" ] && {
  125. local wanif="$tunlink"
  126. if [ -z $wanif ] && ! network_find_wan6 wanif; then
  127. proto_notify_error "$cfg" "NO_WAN_LINK"
  128. exit
  129. fi
  130. if ! network_get_ipaddr6 ip6addr "$wanif"; then
  131. [ -z "$weakif" ] && weakif="lan"
  132. if ! network_get_ipaddr6 ip6addr "$weakif"; then
  133. proto_notify_error "$cfg" "NO_WAN_LINK"
  134. exit
  135. fi
  136. fi
  137. }
  138. case "$mode" in
  139. gretapip6)
  140. gre_generic_setup $cfg $mode $ip6addr $peer6addr "gre6t-$cfg"
  141. ;;
  142. *)
  143. gre_generic_setup $cfg $mode $ip6addr $peer6addr "gre6-$cfg"
  144. ;;
  145. esac
  146. }
  147. proto_grev6_setup() {
  148. local cfg="$1"
  149. grev6_setup $cfg "greip6"
  150. }
  151. proto_grev6tap_setup() {
  152. local cfg="$1"
  153. local network
  154. json_get_vars network
  155. grev6_setup $cfg "gretapip6"
  156. json_init
  157. json_add_string name "gre6t-$cfg"
  158. json_add_boolean link-ext 0
  159. json_close_object
  160. for i in $network; do
  161. ubus call network.interface."$i" add_device "$(json_dump)"
  162. done
  163. }
  164. gretap_generic_teardown() {
  165. local network
  166. json_get_vars network
  167. json_init
  168. json_add_string name "$1"
  169. json_add_boolean link-ext 0
  170. json_close_object
  171. for i in $network; do
  172. ubus call network.interface."$i" remove_device "$(json_dump)"
  173. done
  174. }
  175. proto_gre_teardown() {
  176. local cfg="$1"
  177. }
  178. proto_gretap_teardown() {
  179. local cfg="$1"
  180. gretap_generic_teardown "gre4t-$cfg"
  181. }
  182. proto_grev6_teardown() {
  183. local cfg="$1"
  184. }
  185. proto_grev6tap_teardown() {
  186. local cfg="$1"
  187. gretap_generic_teardown "gre6t-$cfg"
  188. }
  189. gre_generic_init_config() {
  190. no_device=1
  191. available=1
  192. proto_config_add_int "mtu"
  193. proto_config_add_int "ttl"
  194. proto_config_add_string "tos"
  195. proto_config_add_string "tunlink"
  196. proto_config_add_string "zone"
  197. proto_config_add_int "ikey"
  198. proto_config_add_int "okey"
  199. proto_config_add_boolean "icsum"
  200. proto_config_add_boolean "ocsum"
  201. proto_config_add_boolean "iseqno"
  202. proto_config_add_boolean "oseqno"
  203. proto_config_add_boolean "multicast"
  204. }
  205. proto_gre_init_config() {
  206. gre_generic_init_config
  207. proto_config_add_string "ipaddr"
  208. proto_config_add_string "peeraddr"
  209. proto_config_add_boolean "df"
  210. }
  211. proto_gretap_init_config() {
  212. proto_gre_init_config
  213. proto_config_add_string "network"
  214. }
  215. proto_grev6_init_config() {
  216. gre_generic_init_config
  217. proto_config_add_string "ip6addr"
  218. proto_config_add_string "peer6addr"
  219. proto_config_add_string "weakif"
  220. proto_config_add_string "encaplimit"
  221. }
  222. proto_grev6tap_init_config() {
  223. proto_grev6_init_config
  224. proto_config_add_string "network"
  225. }
  226. [ -n "$INCLUDE_ONLY" ] || {
  227. [ -f /lib/modules/$(uname -r)/gre.ko ] && add_protocol gre
  228. [ -f /lib/modules/$(uname -r)/gre.ko ] && add_protocol gretap
  229. [ -f /lib/modules/$(uname -r)/ip6_gre.ko ] && add_protocol grev6
  230. [ -f /lib/modules/$(uname -r)/ip6_gre.ko ] && add_protocol grev6tap
  231. }