gre.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. json_add_int ttl "${ttl:-64}"
  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 ipaddr peeraddr
  40. json_get_vars df ipaddr peeraddr tunlink
  41. [ -z "$peeraddr" ] && {
  42. proto_notify_error "$cfg" "MISSING_ADDRESS"
  43. proto_block_restart "$cfg"
  44. exit
  45. }
  46. ( proto_add_host_dependency "$cfg" "$peeraddr" "$tunlink" )
  47. [ -z "$ipaddr" ] && {
  48. local wanif="$tunlink"
  49. if [ -z $wanif ] && ! network_find_wan wanif; then
  50. proto_notify_error "$cfg" "NO_WAN_LINK"
  51. exit
  52. fi
  53. if ! network_get_ipaddr ipaddr "$wanif"; then
  54. proto_notify_error "$cfg" "NO_WAN_LINK"
  55. exit
  56. fi
  57. }
  58. [ -z "$df" ] && df="1"
  59. gre_generic_setup $cfg $mode $ipaddr $peeraddr "gre-$cfg"
  60. }
  61. proto_gre_setup() {
  62. local cfg="$1"
  63. gre_setup $cfg "greip"
  64. }
  65. proto_gretap_setup() {
  66. local cfg="$1"
  67. local network
  68. json_get_vars network
  69. gre_setup $cfg "gretapip"
  70. json_init
  71. json_add_string name "gre-$cfg"
  72. json_add_boolean link-ext 0
  73. json_close_object
  74. for i in $network; do
  75. ubus call network.interface."$i" add_device "$(json_dump)"
  76. done
  77. }
  78. grev6_setup() {
  79. local cfg="$1"
  80. local mode="$2"
  81. local ip6addr peer6addr weakif
  82. json_get_vars ip6addr peer6addr tunlink weakif
  83. [ -z "$peer6addr" ] && {
  84. proto_notify_error "$cfg" "MISSING_ADDRESS"
  85. proto_block_restart "$cfg"
  86. exit
  87. }
  88. ( proto_add_host_dependency "$cfg" "$peer6addr" "$tunlink" )
  89. [ -z "$ip6addr" ] && {
  90. local wanif="$tunlink"
  91. if [ -z $wanif ] && ! network_find_wan6 wanif; then
  92. proto_notify_error "$cfg" "NO_WAN_LINK"
  93. exit
  94. fi
  95. if ! network_get_ipaddr6 ip6addr "$wanif"; then
  96. [ -z "$weakif" ] && weakif="lan"
  97. if ! network_get_ipaddr6 ip6addr "$weakif"; then
  98. proto_notify_error "$cfg" "NO_WAN_LINK"
  99. exit
  100. fi
  101. fi
  102. }
  103. gre_generic_setup $cfg $mode $ip6addr $peer6addr "grev6-$cfg"
  104. }
  105. proto_grev6_setup() {
  106. local cfg="$1"
  107. grev6_setup $cfg "greip6"
  108. }
  109. proto_grev6tap_setup() {
  110. local cfg="$1"
  111. local network
  112. json_get_vars network
  113. grev6_setup $cfg "gretapip6"
  114. json_init
  115. json_add_string name "grev6-$cfg"
  116. json_add_boolean link-ext 0
  117. json_close_object
  118. for i in $network; do
  119. ubus call network.interface."$i" add_device "$(json_dump)"
  120. done
  121. }
  122. gretap_generic_teardown() {
  123. local network
  124. json_get_vars network
  125. json_init
  126. json_add_string name "$1"
  127. json_add_boolean link-ext 0
  128. json_close_object
  129. for i in $network; do
  130. ubus call network.interface."$i" remove_device "$(json_dump)"
  131. done
  132. }
  133. proto_gre_teardown() {
  134. local cfg="$1"
  135. }
  136. proto_gretap_teardown() {
  137. local cfg="$1"
  138. gretap_generic_teardown "gre-$cfg"
  139. }
  140. proto_grev6_teardown() {
  141. local cfg="$1"
  142. }
  143. proto_grev6tap_teardown() {
  144. local cfg="$1"
  145. gretap_generic_teardown "grev6-$cfg"
  146. }
  147. gre_generic_init_config() {
  148. no_device=1
  149. available=1
  150. proto_config_add_int "mtu"
  151. proto_config_add_int "ttl"
  152. proto_config_add_string "tos"
  153. proto_config_add_string "tunlink"
  154. proto_config_add_string "zone"
  155. proto_config_add_int "ikey"
  156. proto_config_add_int "okey"
  157. proto_config_add_boolean "icsum"
  158. proto_config_add_boolean "ocsum"
  159. proto_config_add_boolean "iseqno"
  160. proto_config_add_boolean "oseqno"
  161. proto_config_add_boolean "multicast"
  162. }
  163. proto_gre_init_config() {
  164. gre_generic_init_config
  165. proto_config_add_string "ipaddr"
  166. proto_config_add_string "peeraddr"
  167. proto_config_add_boolean "df"
  168. }
  169. proto_gretap_init_config() {
  170. proto_gre_init_config
  171. proto_config_add_string "network"
  172. }
  173. proto_grev6_init_config() {
  174. gre_generic_init_config
  175. proto_config_add_string "ip6addr"
  176. proto_config_add_string "peer6addr"
  177. proto_config_add_string "weakif"
  178. }
  179. proto_grev6tap_init_config() {
  180. proto_grev6_init_config
  181. proto_config_add_string "network"
  182. }
  183. [ -n "$INCLUDE_ONLY" ] || {
  184. [ -f /lib/modules/$(uname -r)/gre.ko ] && add_protocol gre
  185. [ -f /lib/modules/$(uname -r)/gre.ko ] && add_protocol gretap
  186. [ -f /lib/modules/$(uname -r)/ip6_gre.ko ] && add_protocol grev6
  187. [ -f /lib/modules/$(uname -r)/ip6_gre.ko ] && add_protocol grev6tap
  188. }