6rd.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/sh
  2. # 6rd.sh - IPv6-in-IPv4 tunnel backend
  3. # Copyright (c) 2010-2012 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_6rd_setup() {
  11. local cfg="$1"
  12. local iface="$2"
  13. local link="6rd-$cfg"
  14. local mtu df ttl tos ipaddr peeraddr ip6prefix ip6prefixlen ip4prefixlen tunlink zone
  15. json_get_vars mtu df ttl tos ipaddr peeraddr ip6prefix ip6prefixlen ip4prefixlen tunlink zone
  16. [ -z "$ip6prefix" -o -z "$peeraddr" ] && {
  17. proto_notify_error "$cfg" "MISSING_ADDRESS"
  18. proto_block_restart "$cfg"
  19. return
  20. }
  21. ( proto_add_host_dependency "$cfg" "$peeraddr" "$tunlink" )
  22. [ -z "$ipaddr" ] && {
  23. local wanif="$tunlink"
  24. if [ -z $wanif ] && ! network_find_wan wanif; then
  25. proto_notify_error "$cfg" "NO_WAN_LINK"
  26. return
  27. fi
  28. if ! network_get_ipaddr ipaddr "$wanif"; then
  29. proto_notify_error "$cfg" "NO_WAN_LINK"
  30. return
  31. fi
  32. }
  33. # Determine the relay prefix.
  34. local ip4prefixlen="${ip4prefixlen:-0}"
  35. local ip4prefix=$(ipcalc.sh "$ipaddr/$ip4prefixlen" | grep NETWORK)
  36. ip4prefix="${ip4prefix#NETWORK=}"
  37. # Determine our IPv6 address.
  38. local ip6subnet=$(6rdcalc "$ip6prefix/$ip6prefixlen" "$ipaddr/$ip4prefixlen")
  39. local ip6addr="${ip6subnet%%::*}::1"
  40. # Determine the IPv6 prefix
  41. local ip6lanprefix="$ip6subnet/$(($ip6prefixlen + 32 - $ip4prefixlen))"
  42. proto_init_update "$link" 1
  43. proto_add_ipv6_address "$ip6addr" "$ip6prefixlen"
  44. proto_add_ipv6_prefix "$ip6lanprefix"
  45. proto_add_ipv6_route "::" 0 "::$peeraddr" 4096 "" "$ip6addr/$ip6prefixlen"
  46. proto_add_ipv6_route "::" 0 "::$peeraddr" 4096 "" "$ip6lanprefix"
  47. proto_add_tunnel
  48. json_add_string mode sit
  49. json_add_int mtu "${mtu:-1280}"
  50. json_add_boolean df "${df:-1}"
  51. json_add_int ttl "${ttl:-64}"
  52. [ -n "$tos" ] && json_add_string tos "$tos"
  53. json_add_string local "$ipaddr"
  54. json_add_string 6rd-prefix "$ip6prefix/$ip6prefixlen"
  55. json_add_string 6rd-relay-prefix "$ip4prefix/$ip4prefixlen"
  56. [ -n "$tunlink" ] && json_add_string link "$tunlink"
  57. proto_close_tunnel
  58. proto_add_data
  59. [ -n "$zone" ] && json_add_string zone "$zone"
  60. proto_close_data
  61. proto_send_update "$cfg"
  62. }
  63. proto_6rd_teardown() {
  64. local cfg="$1"
  65. }
  66. proto_6rd_init_config() {
  67. no_device=1
  68. available=1
  69. proto_config_add_int "mtu"
  70. proto_config_add_boolean "df"
  71. proto_config_add_int "ttl"
  72. proto_config_add_string "tos"
  73. proto_config_add_string "ipaddr"
  74. proto_config_add_string "peeraddr"
  75. proto_config_add_string "ip6prefix"
  76. proto_config_add_string "ip6prefixlen"
  77. proto_config_add_string "ip4prefixlen"
  78. proto_config_add_string "tunlink"
  79. proto_config_add_string "zone"
  80. }
  81. [ -n "$INCLUDE_ONLY" ] || {
  82. add_protocol 6rd
  83. }