vti.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. vti_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 zone ikey
  15. json_get_vars mtu zone ikey okey
  16. proto_init_update "$link" 1
  17. proto_add_tunnel
  18. json_add_string mode "$mode"
  19. json_add_int mtu "${mtu:-1280}"
  20. json_add_string local "$local"
  21. json_add_string remote "$remote"
  22. [ -n "$tunlink" ] && json_add_string link "$tunlink"
  23. json_add_object 'data'
  24. [ -n "$ikey" ] && json_add_int ikey "$ikey"
  25. [ -n "$okey" ] && json_add_int okey "$okey"
  26. json_close_object
  27. proto_close_tunnel
  28. proto_add_data
  29. [ -n "$zone" ] && json_add_string zone "$zone"
  30. proto_close_data
  31. proto_send_update "$cfg"
  32. }
  33. vti_setup() {
  34. local cfg="$1"
  35. local mode="$2"
  36. local ipaddr peeraddr
  37. json_get_vars df ipaddr peeraddr tunlink
  38. [ -z "$peeraddr" ] && {
  39. proto_notify_error "$cfg" "MISSING_ADDRESS"
  40. proto_block_restart "$cfg"
  41. exit
  42. }
  43. ( proto_add_host_dependency "$cfg" "$peeraddr" "$tunlink" )
  44. [ -z "$ipaddr" ] && {
  45. local wanif="$tunlink"
  46. if [ -z $wanif ] && ! network_find_wan wanif; then
  47. proto_notify_error "$cfg" "NO_WAN_LINK"
  48. exit
  49. fi
  50. if ! network_get_ipaddr ipaddr "$wanif"; then
  51. proto_notify_error "$cfg" "NO_WAN_LINK"
  52. exit
  53. fi
  54. }
  55. vti_generic_setup $cfg $mode $ipaddr $peeraddr "vti-$cfg"
  56. }
  57. proto_vti_setup() {
  58. local cfg="$1"
  59. vti_setup $cfg "vtiip"
  60. }
  61. vti6_setup() {
  62. local cfg="$1"
  63. local mode="$2"
  64. local ip6addr peer6addr weakif
  65. json_get_vars ip6addr peer6addr tunlink weakif
  66. [ -z "$peer6addr" ] && {
  67. proto_notify_error "$cfg" "MISSING_ADDRESS"
  68. proto_block_restart "$cfg"
  69. exit
  70. }
  71. ( proto_add_host_dependency "$cfg" "$peer6addr" "$tunlink" )
  72. [ -z "$ip6addr" ] && {
  73. local wanif="$tunlink"
  74. if [ -z $wanif ] && ! network_find_wan6 wanif; then
  75. proto_notify_error "$cfg" "NO_WAN_LINK"
  76. exit
  77. fi
  78. if ! network_get_ipaddr6 ip6addr "$wanif"; then
  79. [ -z "$weakif" ] && weakif="lan"
  80. if ! network_get_ipaddr6 ip6addr "$weakif"; then
  81. proto_notify_error "$cfg" "NO_WAN_LINK"
  82. exit
  83. fi
  84. fi
  85. }
  86. vti_generic_setup $cfg $mode $ip6addr $peer6addr "vti6-$cfg"
  87. }
  88. proto_vti6_setup() {
  89. local cfg="$1"
  90. vti6_setup $cfg "vtiip6"
  91. }
  92. proto_vti_teardown() {
  93. local cfg="$1"
  94. }
  95. proto_vti6_teardown() {
  96. local cfg="$1"
  97. }
  98. vti_generic_init_config() {
  99. no_device=1
  100. available=1
  101. proto_config_add_int "mtu"
  102. proto_config_add_string "tunlink"
  103. proto_config_add_string "zone"
  104. proto_config_add_int "ikey"
  105. proto_config_add_int "okey"
  106. }
  107. proto_vti_init_config() {
  108. vti_generic_init_config
  109. proto_config_add_string "ipaddr"
  110. proto_config_add_string "peeraddr"
  111. }
  112. proto_vti6_init_config() {
  113. vti_generic_init_config
  114. proto_config_add_string "ip6addr"
  115. proto_config_add_string "peer6addr"
  116. proto_config_add_string "weakif"
  117. }
  118. [ -n "$INCLUDE_ONLY" ] || {
  119. [ -f /lib/modules/$(uname -r)/ip_vti.ko ] && add_protocol vti
  120. [ -f /lib/modules/$(uname -r)/ip6_vti.ko ] && add_protocol vti6
  121. }