1
0

464xlat.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/bin/sh
  2. # 464xlat.sh - 464xlat CLAT
  3. #
  4. # Copyright (c) 2015 Steven Barth <cyrus@openwrt.org>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License version 2
  8. # as published by the Free Software Foundation
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. [ -n "$INCLUDE_ONLY" ] || {
  15. . /lib/functions.sh
  16. . /lib/functions/network.sh
  17. . ../netifd-proto.sh
  18. init_proto "$@"
  19. }
  20. proto_464xlat_setup() {
  21. local cfg="$1"
  22. local iface="$2"
  23. local link="464-$cfg"
  24. local ip6addr ip6prefix tunlink zone
  25. json_get_vars ip6addr ip6prefix tunlink zone
  26. [ "$zone" = "-" ] && zone=""
  27. ( proto_add_host_dependency "$cfg" "::" "$tunlink" )
  28. if [ -z "$tunlink" ] && ! network_find_wan6 tunlink; then
  29. proto_notify_error "$cfg" "NO_WAN_LINK"
  30. return
  31. fi
  32. network_get_device tundev "$tunlink"
  33. ip6addr=$(464xlatcfg "$link" "$tundev" "$ip6prefix" 192.0.0.1 $ip6addr)
  34. if [ -z "$ip6addr" ]; then
  35. proto_notify_error "$cfg" "CLAT_CONFIG_FAILED"
  36. return
  37. fi
  38. ip -6 rule del from all lookup local
  39. ip -6 rule add from all lookup local pref 1
  40. ip -6 rule add to $ip6addr lookup prelocal pref 0
  41. echo "$ip6addr" > /tmp/464-$cfg-anycast
  42. proto_init_update "$link" 1
  43. proto_add_ipv4_route "0.0.0.0" 0 "" "" 2048
  44. proto_add_ipv6_route $ip6addr 128 "" "" "" "" 128
  45. proto_add_data
  46. [ -n "$zone" ] && json_add_string zone "$zone"
  47. json_add_array firewall
  48. [ -z "$zone" ] && zone=$(fw3 -q network $iface 2>/dev/null)
  49. json_add_object ""
  50. json_add_string type nat
  51. json_add_string target SNAT
  52. json_add_string family inet
  53. json_add_string snat_ip 192.0.0.1
  54. json_close_object
  55. [ -n "$zone" ] && {
  56. json_add_object ""
  57. json_add_string type rule
  58. json_add_string family inet6
  59. json_add_string proto all
  60. json_add_string direction in
  61. json_add_string dest "$zone"
  62. json_add_string src "$zone"
  63. json_add_string src_ip $ip6addr
  64. json_add_string target ACCEPT
  65. json_close_object
  66. }
  67. json_close_array
  68. proto_close_data
  69. proto_send_update "$cfg"
  70. }
  71. proto_464xlat_teardown() {
  72. local cfg="$1"
  73. local link="464-$cfg"
  74. [ -f /tmp/464-$cfg-anycast ] || return
  75. local ip6addr=$(cat /tmp/464-$cfg-anycast)
  76. 464xlatcfg "$link"
  77. rm -rf /tmp/464-$cfg-anycast
  78. [ -n "$ip6addr" ] && ip -6 rule del to $ip6addr lookup prelocal
  79. if [ -z "$(ls /tmp/464-*-anycast 2>&-)" ]; then
  80. ip -6 rule del from all lookup local
  81. ip -6 rule add from all lookup local pref 0
  82. fi
  83. # Kill conntracks SNATed to 192.0.0.1
  84. echo 192.0.0.1 > /proc/net/nf_conntrack
  85. }
  86. proto_464xlat_init_config() {
  87. no_device=1
  88. available=1
  89. proto_config_add_string "ip6prefix"
  90. proto_config_add_string "ip6addr"
  91. proto_config_add_string "tunlink"
  92. proto_config_add_string "zone"
  93. }
  94. [ -n "$INCLUDE_ONLY" ] || {
  95. add_protocol 464xlat
  96. }