dslite.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/sh
  2. # dslite.sh - IPv4-in-IPv6 tunnel backend
  3. # Copyright (c) 2013 OpenWrt.org
  4. [ -n "$INCLUDE_ONLY" ] || {
  5. . /lib/functions.sh
  6. . /lib/functions/network.sh
  7. . ../netifd-proto.sh
  8. init_proto "$@"
  9. }
  10. proto_dslite_setup() {
  11. local cfg="$1"
  12. local iface="$2"
  13. local link="ds-$cfg"
  14. local remoteip6
  15. local mtu ttl peeraddr ip6addr tunlink zone weakif encaplimit
  16. json_get_vars mtu ttl peeraddr ip6addr tunlink zone weakif encaplimit
  17. [ -z "$peeraddr" ] && {
  18. proto_notify_error "$cfg" "MISSING_ADDRESS"
  19. proto_block_restart "$cfg"
  20. return
  21. }
  22. ( proto_add_host_dependency "$cfg" "::" "$tunlink" )
  23. remoteip6=$(resolveip -6 "$peeraddr")
  24. if [ -z "$remoteip6" ]; then
  25. sleep 3
  26. remoteip6=$(resolveip -6 "$peeraddr")
  27. if [ -z "$remoteip6" ]; then
  28. proto_notify_error "$cfg" "AFTR_DNS_FAIL"
  29. return
  30. fi
  31. fi
  32. for ip6 in $remoteip6; do
  33. peeraddr=$ip6
  34. break
  35. done
  36. [ -z "$ip6addr" ] && {
  37. local wanif="$tunlink"
  38. if [ -z "$wanif" ] && ! network_find_wan6 wanif; then
  39. proto_notify_error "$cfg" "NO_WAN_LINK"
  40. return
  41. fi
  42. if ! network_get_ipaddr6 ip6addr "$wanif"; then
  43. [ -z "$weakif" ] && weakif="lan"
  44. if ! network_get_ipaddr6 ip6addr "$weakif"; then
  45. proto_notify_error "$cfg" "NO_WAN_LINK"
  46. return
  47. fi
  48. fi
  49. }
  50. proto_init_update "$link" 1
  51. proto_add_ipv4_route "0.0.0.0" 0
  52. proto_add_ipv4_address "192.0.0.2" "" "" "192.0.0.1"
  53. proto_add_tunnel
  54. json_add_string mode ipip6
  55. json_add_int mtu "${mtu:-1280}"
  56. json_add_int ttl "${ttl:-64}"
  57. json_add_string local "$ip6addr"
  58. json_add_string remote "$peeraddr"
  59. [ -n "$tunlink" ] && json_add_string link "$tunlink"
  60. json_add_object "data"
  61. [ -n "$encaplimit" ] && json_add_string encaplimit "$encaplimit"
  62. json_close_object
  63. proto_close_tunnel
  64. proto_add_data
  65. [ -n "$zone" ] && json_add_string zone "$zone"
  66. json_add_array firewall
  67. json_add_object ""
  68. json_add_string type nat
  69. json_add_string target ACCEPT
  70. json_close_object
  71. json_close_array
  72. proto_close_data
  73. proto_send_update "$cfg"
  74. }
  75. proto_dslite_teardown() {
  76. local cfg="$1"
  77. }
  78. proto_dslite_init_config() {
  79. no_device=1
  80. available=1
  81. proto_config_add_string "ip6addr"
  82. proto_config_add_string "peeraddr"
  83. proto_config_add_string "tunlink"
  84. proto_config_add_int "mtu"
  85. proto_config_add_int "ttl"
  86. proto_config_add_string "encaplimit"
  87. proto_config_add_string "zone"
  88. proto_config_add_string "weakif"
  89. }
  90. [ -n "$INCLUDE_ONLY" ] || {
  91. add_protocol dslite
  92. }