1
0

6rd.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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
  36. eval "$(ipcalc.sh "$ipaddr/$ip4prefixlen")";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. [ -n "$tunlink" ] && json_add_string link "$tunlink"
  55. json_add_object 'data'
  56. json_add_string prefix "$ip6prefix/$ip6prefixlen"
  57. json_add_string relay-prefix "$ip4prefix/$ip4prefixlen"
  58. json_close_object
  59. proto_close_tunnel
  60. proto_add_data
  61. [ -n "$zone" ] && json_add_string zone "$zone"
  62. proto_close_data
  63. proto_send_update "$cfg"
  64. }
  65. proto_6rd_teardown() {
  66. local cfg="$1"
  67. }
  68. proto_6rd_init_config() {
  69. no_device=1
  70. available=1
  71. proto_config_add_int "mtu"
  72. proto_config_add_boolean "df"
  73. proto_config_add_int "ttl"
  74. proto_config_add_string "tos"
  75. proto_config_add_string "ipaddr"
  76. proto_config_add_string "peeraddr"
  77. proto_config_add_string "ip6prefix"
  78. proto_config_add_string "ip6prefixlen"
  79. proto_config_add_string "ip4prefixlen"
  80. proto_config_add_string "tunlink"
  81. proto_config_add_string "zone"
  82. }
  83. [ -n "$INCLUDE_ONLY" ] || {
  84. add_protocol 6rd
  85. }