gre.sh 5.5 KB

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