gre.sh 4.7 KB

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