You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.7 KiB

  1. # tayga.sh - NAT64 backend
  2. find_tayga_wanif4() {
  3. local if=$(ip -4 r l e 0.0.0.0/0); if="${if#default* dev }"; if="${if%% *}"
  4. [ -n "$if" ] && grep -qs "^ *$if:" /proc/net/dev && echo "$if"
  5. }
  6. find_tayga_wanip4() {
  7. local ip=$(ip -4 a s dev "$1"); ip="${ip#*inet }"
  8. echo "${ip%%[^0-9.]*}"
  9. }
  10. find_tayga_wanif6() {
  11. local if=$(ip -6 r l e ::/0); if="${if#default* dev }"; if="${if%% *}"
  12. [ -n "$if" ] && grep -qs "^ *$if:" /proc/net/dev && echo "$if"
  13. }
  14. find_tayga_wanip6() {
  15. local ip=$(ip -6 a s dev "$1"); ip="${ip#*inet6 }"
  16. echo "${ip%%[^0-9A-Fa-f:]*}"
  17. }
  18. # Hook into scan_interfaces() to synthesize a .device option
  19. # This is needed for /sbin/ifup to properly dispatch control
  20. # to setup_interface_tayga() even if no .ifname is set in
  21. # the configuration.
  22. scan_tayga() {
  23. config_set "$1" device "tayga-$1"
  24. }
  25. coldplug_interface_tayga() {
  26. setup_interface_tayga "tayga-$1" "$1"
  27. }
  28. tayga_add_static_mappings() {
  29. local tmpconf="$1"
  30. (
  31. . /lib/functions.sh
  32. config_load firewall
  33. tayga_map_rule_add() {
  34. local cfg="$1"
  35. local tmpconf="$2"
  36. local ipv4_addr ipv6_addr
  37. config_get ipv4_addr "$cfg" ipv4_addr ""
  38. config_get ipv6_addr "$cfg" ipv6_addr ""
  39. [ -n "$ipv4_addr" ] && [ -n "$ipv6_addr" ] &&
  40. echo "map $ipv4_addr $ipv6_addr" >>$tmpconf
  41. }
  42. config_foreach tayga_map_rule_add nat64 "$tmpconf"
  43. )
  44. }
  45. setup_interface_tayga() {
  46. local iface="$1"
  47. local cfg="$2"
  48. local link="tayga-$cfg"
  49. local ipv4_addr ipv6_addr prefix dynamic_pool
  50. config_get ipv4_addr "$cfg" ipv4_addr
  51. config_get ipv6_addr "$cfg" ipv6_addr
  52. config_get prefix "$cfg" prefix
  53. config_get dynamic_pool "$cfg" dynamic_pool
  54. local args
  55. include /lib/network
  56. scan_interfaces
  57. local wanip4=$(uci_get network "$cfg" ipv4addr)
  58. local wanip6=$(uci_get network "$cfg" ipv6addr)
  59. local wanif4=$(find_tayga_wanif4)
  60. local wanif6=$(find_tayga_wanif6)
  61. [ -z "$wanip4" ] && {
  62. [ -n "$wanif4" ] && {
  63. wanip4=$(find_tayga_wanip4 "$wanif4")
  64. uci_set_state network "$cfg" wan4_device "$wanif4"
  65. }
  66. }
  67. [ -z "$wanip6" ] && {
  68. [ -n "$wanif6" ] && {
  69. wanip6=$(find_tayga_wanip6 "$wanif6")
  70. uci_set_state network "$cfg" wan6_device "$wanif6"
  71. }
  72. }
  73. [ -n "$wanip4" ] && [ -n "$wanip6" ] || {
  74. echo "Cannot determine local IPv4 and IPv6 addressed for tayga NAT64 $cfg - skipping"
  75. return 1
  76. }
  77. local tmpconf="/var/etc/tayga-$cfg.conf"
  78. args="-c $tmpconf"
  79. mkdir -p /var/etc
  80. mkdir -p /var/run/tayga/$cfg
  81. echo "tun-device $link" >$tmpconf
  82. echo "ipv4-addr $ipv4_addr" >>$tmpconf
  83. [ -n "$ipv6_addr" ] &&
  84. echo "ipv6-addr $ipv6_addr" >>$tmpconf
  85. [ -n "$prefix" ] &&
  86. echo "prefix $prefix" >>$tmpconf
  87. tayga_add_static_mappings "$tmpconf"
  88. [ -n "$dynamic_pool" ] &&
  89. echo "dynamic-pool $dynamic_pool" >>$tmpconf
  90. echo "data-dir /var/run/tayga/$cfg" >>$tmpconf
  91. # creating the tunnel below will trigger a net subsystem event
  92. # prevent it from touching or iface by disabling .auto here
  93. uci_set_state network "$cfg" ifname $link
  94. uci_set_state network "$cfg" auto 0
  95. # here we create TUN device and check configuration
  96. tayga $args --mktun || return 1
  97. ip link set "$link" up
  98. ip addr add "$wanip4" dev "$link"
  99. ip addr add "$wanip6" dev "$link"
  100. [ -n "$dynamic_pool" ] &&
  101. ip -4 route add "$dynamic_pool" dev "$link"
  102. [ -n "$prefix" ] &&
  103. ip -6 route add "$prefix" dev "$link"
  104. start-stop-daemon -S -x tayga -- $args -p /var/run/$link.pid
  105. env -i ACTION="ifup" DEVICE="$link" INTERFACE="$cfg" PROTO="tayga" \
  106. /sbin/hotplug-call iface
  107. }
  108. stop_interface_tayga() {
  109. local cfg="$1"
  110. local link="tayga-$cfg"
  111. env -i ACTION="ifdown" DEVICE="$link" INTERFACE="$cfg" PROTO="tayga" \
  112. /sbin/hotplug-call iface
  113. service_kill tayga "/var/run/$link.pid"
  114. ip link set "$link" down
  115. ip addr flush dev "$link"
  116. ip route flush dev "$link"
  117. }