gre.sh 5.8 KB

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