6rd.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 ttl ipaddr peeraddr ip6prefix ip6prefixlen ip4prefixlen
  15. json_get_vars mtu ttl ipaddr peeraddr ip6prefix ip6prefixlen ip4prefixlen
  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" 0.0.0.0 )
  22. [ -z "$ipaddr" ] && {
  23. local wanif
  24. if ! network_find_wan wanif || ! network_get_ipaddr ipaddr "$wanif"; then
  25. proto_notify_error "$cfg" "NO_WAN_LINK"
  26. return
  27. fi
  28. }
  29. # Determine the relay prefix.
  30. local ip4prefixlen="${ip4prefixlen:-0}"
  31. local ip4prefix=$(ipcalc.sh "$ipaddr/$ip4prefixlen" | grep NETWORK)
  32. ip4prefix="${ip4prefix#NETWORK=}"
  33. # Determine our IPv6 address.
  34. local ip6subnet=$(6rdcalc "$ip6prefix/$ip6prefixlen" "$ipaddr/$ip4prefixlen")
  35. local ip6addr="${ip6subnet%%::*}::1"
  36. # Determine the IPv6 prefix
  37. local ip6lanprefix="$ip6subnet/$(($ip6prefixlen + 32 - $ip4prefixlen))"
  38. proto_init_update "$link" 1
  39. proto_add_ipv6_address "$ip6addr" "$ip6prefixlen"
  40. proto_add_ipv6_prefix "$ip6lanprefix"
  41. proto_add_ipv6_route "::" 0 "::$peeraddr" 4096
  42. proto_add_tunnel
  43. json_add_string mode sit
  44. json_add_int mtu "${mtu:-1280}"
  45. json_add_int ttl "${ttl:-64}"
  46. json_add_string local "$ipaddr"
  47. json_add_string 6rd-prefix "$ip6prefix/$ip6prefixlen"
  48. json_add_string 6rd-relay-prefix "$ip4prefix/$ip4prefixlen"
  49. proto_close_tunnel
  50. proto_send_update "$cfg"
  51. }
  52. proto_6rd_teardown() {
  53. local cfg="$1"
  54. }
  55. proto_6rd_init_config() {
  56. no_device=1
  57. available=1
  58. proto_config_add_int "mtu"
  59. proto_config_add_int "ttl"
  60. proto_config_add_string "ipaddr"
  61. proto_config_add_string "peeraddr"
  62. proto_config_add_string "ip6prefix"
  63. proto_config_add_string "ip6prefixlen"
  64. proto_config_add_string "ip4prefixlen"
  65. }
  66. [ -n "$INCLUDE_ONLY" ] || {
  67. add_protocol 6rd
  68. }