6rd.sh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. proto_init_update "$link" 1
  37. proto_add_ipv6_address "$ip6addr" "$ip6prefixlen"
  38. proto_add_ipv6_route "::" 0 "::$peeraddr"
  39. proto_add_tunnel
  40. json_add_string mode sit
  41. json_add_int mtu "${mtu:-1280}"
  42. json_add_int ttl "${ttl:-64}"
  43. json_add_string local "$ipaddr"
  44. json_add_string 6rd-prefix "$ip6prefix/$ip6prefixlen"
  45. json_add_string 6rd-relay-prefix "$ip4prefix/$ip4prefixlen"
  46. proto_close_tunnel
  47. proto_send_update "$cfg"
  48. }
  49. proto_6rd_teardown() {
  50. local cfg="$1"
  51. }
  52. proto_6rd_init_config() {
  53. no_device=1
  54. available=1
  55. proto_config_add_int "mtu"
  56. proto_config_add_int "ttl"
  57. proto_config_add_string "ipaddr"
  58. proto_config_add_string "peeraddr"
  59. proto_config_add_string "ip6prefix"
  60. proto_config_add_string "ip6prefixlen"
  61. proto_config_add_string "ip4prefixlen"
  62. }
  63. [ -n "$INCLUDE_ONLY" ] || {
  64. add_protocol 6rd
  65. }