6to4.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/bin/sh
  2. # 6to4.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. find_6to4_prefix() {
  11. local ip4="$1"
  12. local oIFS="$IFS"; IFS="."; set -- $ip4; IFS="$oIFS"
  13. printf "2002:%02x%02x:%02x%02x\n" $1 $2 $3 $4
  14. }
  15. test_6to4_rfc1918()
  16. {
  17. local oIFS="$IFS"; IFS="."; set -- $1; IFS="$oIFS"
  18. [ $1 -eq 10 ] && return 0
  19. [ $1 -eq 192 ] && [ $2 -eq 168 ] && return 0
  20. [ $1 -eq 172 ] && [ $2 -ge 16 ] && [ $2 -le 31 ] && return 0
  21. # RFC 6598
  22. [ $1 -eq 100 ] && [ $2 -ge 64 ] && [ $2 -le 127 ] && return 0
  23. return 1
  24. }
  25. proto_6to4_setup() {
  26. local cfg="$1"
  27. local iface="$2"
  28. local link="6to4-$cfg"
  29. local mtu ttl tos ipaddr
  30. json_get_vars mtu ttl tos ipaddr
  31. ( proto_add_host_dependency "$cfg" 0.0.0.0 )
  32. local wanif
  33. if ! network_find_wan wanif; then
  34. proto_notify_error "$cfg" "NO_WAN_LINK"
  35. return
  36. fi
  37. [ -z "$ipaddr" ] && {
  38. if ! network_get_ipaddr ipaddr "$wanif"; then
  39. proto_notify_error "$cfg" "NO_WAN_ADDRESS"
  40. return
  41. fi
  42. }
  43. test_6to4_rfc1918 "$ipaddr" && {
  44. proto_notify_error "$cfg" "INVALID_LOCAL_ADDRESS"
  45. return
  46. }
  47. # find our local prefix
  48. local prefix6=$(find_6to4_prefix "$ipaddr")
  49. local local6="$prefix6::1"
  50. proto_init_update "$link" 1
  51. proto_add_ipv6_address "$local6" 16
  52. proto_add_ipv6_prefix "$prefix6::/48"
  53. proto_add_ipv6_route "::" 0 "::192.88.99.1" "" "" "$local6/16"
  54. proto_add_ipv6_route "::" 0 "::192.88.99.1" "" "" "$prefix6::/48"
  55. proto_add_tunnel
  56. json_add_string mode sit
  57. json_add_int mtu "${mtu:-1280}"
  58. json_add_int ttl "${ttl:-64}"
  59. [ -n "$tos" ] && json_add_string tos "$tos"
  60. json_add_string local "$ipaddr"
  61. proto_close_tunnel
  62. proto_send_update "$cfg"
  63. }
  64. proto_6to4_teardown() {
  65. local cfg="$1"
  66. }
  67. proto_6to4_init_config() {
  68. no_device=1
  69. available=1
  70. proto_config_add_string "ipaddr"
  71. proto_config_add_int "mtu"
  72. proto_config_add_int "ttl"
  73. proto_config_add_string "tos"
  74. }
  75. [ -n "$INCLUDE_ONLY" ] || {
  76. add_protocol 6to4
  77. }