gre.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 ipv6 ttl tos zone ikey okey icsum ocsum iseqno oseqno multicast
  15. json_get_vars mtu ipv6 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. json_add_boolean ipv6 "${ipv6:-1}"
  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 nohostroute
  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. if [ "${nohostroute}" != "1" ]; then
  65. ( proto_add_host_dependency "$cfg" "$peeraddr" "$tunlink" )
  66. fi
  67. [ -z "$ipaddr" ] && {
  68. local wanif="$tunlink"
  69. if [ -z $wanif ] && ! network_find_wan wanif; then
  70. proto_notify_error "$cfg" "NO_WAN_LINK"
  71. exit
  72. fi
  73. if ! network_get_ipaddr ipaddr "$wanif"; then
  74. proto_notify_error "$cfg" "NO_WAN_LINK"
  75. exit
  76. fi
  77. }
  78. [ -z "$df" ] && df="1"
  79. case "$mode" in
  80. gretapip)
  81. gre_generic_setup $cfg $mode $ipaddr $peeraddr "gre4t-$cfg"
  82. ;;
  83. *)
  84. gre_generic_setup $cfg $mode $ipaddr $peeraddr "gre4-$cfg"
  85. ;;
  86. esac
  87. }
  88. proto_gre_setup() {
  89. local cfg="$1"
  90. gre_setup $cfg "greip"
  91. }
  92. proto_gretap_setup() {
  93. local cfg="$1"
  94. local network
  95. json_get_vars network
  96. gre_setup $cfg "gretapip"
  97. json_init
  98. json_add_string name "gre4t-$cfg"
  99. json_add_boolean link-ext 0
  100. json_close_object
  101. for i in $network; do
  102. ubus call network.interface."$i" add_device "$(json_dump)"
  103. done
  104. }
  105. grev6_setup() {
  106. local cfg="$1"
  107. local mode="$2"
  108. local remoteip6
  109. local ip6addr peer6addr weakif
  110. json_get_vars ip6addr peer6addr tunlink weakif encaplimit nohostroute
  111. [ -z "$peer6addr" ] && {
  112. proto_notify_error "$cfg" "MISSING_PEER_ADDRESS"
  113. proto_block_restart "$cfg"
  114. exit
  115. }
  116. remoteip6=$(resolveip -t 10 -6 "$peer6addr")
  117. if [ -z "$remoteip6" ]; then
  118. proto_notify_error "$cfg" "PEER_RESOLVE_FAIL"
  119. exit
  120. fi
  121. for ip6 in $remoteip6; do
  122. peer6addr=$ip6
  123. break
  124. done
  125. if [ "${nohostroute}" != "1" ]; then
  126. ( proto_add_host_dependency "$cfg" "$peer6addr" "$tunlink" )
  127. fi
  128. [ -z "$ip6addr" ] && {
  129. local wanif="$tunlink"
  130. if [ -z $wanif ] && ! network_find_wan6 wanif; then
  131. proto_notify_error "$cfg" "NO_WAN_LINK"
  132. exit
  133. fi
  134. if ! network_get_ipaddr6 ip6addr "$wanif"; then
  135. [ -z "$weakif" ] && weakif="lan"
  136. if ! network_get_ipaddr6 ip6addr "$weakif"; then
  137. proto_notify_error "$cfg" "NO_WAN_LINK"
  138. exit
  139. fi
  140. fi
  141. }
  142. case "$mode" in
  143. gretapip6)
  144. gre_generic_setup $cfg $mode $ip6addr $peer6addr "gre6t-$cfg"
  145. ;;
  146. *)
  147. gre_generic_setup $cfg $mode $ip6addr $peer6addr "gre6-$cfg"
  148. ;;
  149. esac
  150. }
  151. proto_grev6_setup() {
  152. local cfg="$1"
  153. grev6_setup $cfg "greip6"
  154. }
  155. proto_grev6tap_setup() {
  156. local cfg="$1"
  157. local network
  158. json_get_vars network
  159. grev6_setup $cfg "gretapip6"
  160. json_init
  161. json_add_string name "gre6t-$cfg"
  162. json_add_boolean link-ext 0
  163. json_close_object
  164. for i in $network; do
  165. ubus call network.interface."$i" add_device "$(json_dump)"
  166. done
  167. }
  168. gretap_generic_teardown() {
  169. local network
  170. json_get_vars network
  171. json_init
  172. json_add_string name "$1"
  173. json_add_boolean link-ext 0
  174. json_close_object
  175. for i in $network; do
  176. ubus call network.interface."$i" remove_device "$(json_dump)"
  177. done
  178. }
  179. proto_gre_teardown() {
  180. local cfg="$1"
  181. }
  182. proto_gretap_teardown() {
  183. local cfg="$1"
  184. gretap_generic_teardown "gre4t-$cfg"
  185. }
  186. proto_grev6_teardown() {
  187. local cfg="$1"
  188. }
  189. proto_grev6tap_teardown() {
  190. local cfg="$1"
  191. gretap_generic_teardown "gre6t-$cfg"
  192. }
  193. gre_generic_init_config() {
  194. no_device=1
  195. available=1
  196. proto_config_add_int "mtu"
  197. proto_config_add_boolean "ipv6"
  198. proto_config_add_int "ttl"
  199. proto_config_add_string "tos"
  200. proto_config_add_string "tunlink"
  201. proto_config_add_string "zone"
  202. proto_config_add_int "ikey"
  203. proto_config_add_int "okey"
  204. proto_config_add_boolean "icsum"
  205. proto_config_add_boolean "ocsum"
  206. proto_config_add_boolean "iseqno"
  207. proto_config_add_boolean "oseqno"
  208. proto_config_add_boolean "multicast"
  209. }
  210. proto_gre_init_config() {
  211. gre_generic_init_config
  212. proto_config_add_string "ipaddr"
  213. proto_config_add_string "peeraddr"
  214. proto_config_add_boolean "df"
  215. proto_config_add_boolean "nohostroute"
  216. }
  217. proto_gretap_init_config() {
  218. proto_gre_init_config
  219. proto_config_add_string "network"
  220. }
  221. proto_grev6_init_config() {
  222. gre_generic_init_config
  223. proto_config_add_string "ip6addr"
  224. proto_config_add_string "peer6addr"
  225. proto_config_add_string "weakif"
  226. proto_config_add_string "encaplimit"
  227. proto_config_add_boolean "nohostroute"
  228. }
  229. proto_grev6tap_init_config() {
  230. proto_grev6_init_config
  231. proto_config_add_string "network"
  232. }
  233. [ -n "$INCLUDE_ONLY" ] || {
  234. [ -d /sys/module/ip_gre ] && { add_protocol gre; add_protocol gretap; }
  235. [ -d /sys/module/ip6_gre ] && { add_protocol grev6; add_protocol grev6tap; }
  236. }