tayga-proto.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/sh
  2. # tayga.sh - TAYGA proto
  3. # Copyright (c) 2014 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_tayga_setup() {
  11. local cfg="$1"
  12. local iface="$2"
  13. local link="tayga-$cfg"
  14. local ipv4_addr ipv6_addr prefix dynamic_pool ipaddr ip6addr noroutes
  15. json_get_vars ipv4_addr ipv6_addr prefix dynamic_pool ipaddr ip6addr noroutes
  16. [ -z "$ipv4_addr" -o -z "$prefix" ] && {
  17. proto_notify_error "$cfg" "REQUIRED_PARAMETERS_MISSING"
  18. proto_block_restart "$cfg"
  19. return
  20. }
  21. local tmpconf="/var/etc/tayga-$cfg.conf"
  22. mkdir -p /var/etc
  23. mkdir -p /var/run/tayga/$cfg
  24. echo "tun-device $link" >$tmpconf
  25. echo "ipv4-addr $ipv4_addr" >>$tmpconf
  26. [ -n "$ipv6_addr" ] &&
  27. echo "ipv6-addr $ipv6_addr" >>$tmpconf
  28. [ -n "$prefix" ] &&
  29. echo "prefix $prefix" >>$tmpconf
  30. [ -n "$dynamic_pool" ] &&
  31. echo "dynamic-pool $dynamic_pool" >>$tmpconf
  32. echo "data-dir /var/run/tayga/$cfg" >>$tmpconf
  33. #TODO: Support static mapping of IPv4 <-> IPv6
  34. # here we create TUN device and check configuration
  35. tayga -c $tmpconf --mktun
  36. [ "$?" -ne 0 ] && {
  37. proto_notify_error "$cfg" "TAYGA_FAILED"
  38. proto_block_restart "$cfg"
  39. return
  40. }
  41. proto_init_update "$link" 1
  42. [ -n "$ipaddr" ] && proto_add_ipv4_address "$ipaddr" "255.255.255.255"
  43. [ -n "$ip6addr" ] && proto_add_ipv6_address "$ip6addr" "128"
  44. [ "$noroutes" != 1 ] && {
  45. [ -n "$ipv6_addr" ] && proto_add_ipv6_route "$ipv6_addr" "128"
  46. [ -n "$dynamic_pool" ] && {
  47. local pool="${dynamic_pool%%/*}"
  48. local mask="${dynamic_pool##*/}"
  49. proto_add_ipv4_route "$pool" "$mask"
  50. }
  51. [ -n "$prefix" ] && {
  52. local prefix6="${prefix%%/*}"
  53. local mask6="${prefix##*/}"
  54. proto_add_ipv6_route "$prefix6" "$mask6"
  55. }
  56. }
  57. proto_send_update "$cfg"
  58. proto_run_command "$cfg" tayga -n -c $tmpconf \
  59. -p /var/run/$link.pid
  60. }
  61. proto_tayga_teardown() {
  62. local cfg="$1"
  63. local tmpconf="/var/etc/tayga-$cfg.conf"
  64. proto_kill_command "$cfg"
  65. sleep 1
  66. tayga -c $tmpconf --rmtun
  67. }
  68. proto_tayga_init_config() {
  69. no_device=1
  70. available=1
  71. proto_config_add_string "ipv4_addr"
  72. proto_config_add_string "ipv6_addr"
  73. proto_config_add_string "prefix"
  74. proto_config_add_string "dynamic_pool"
  75. proto_config_add_string "ipaddr"
  76. proto_config_add_string "ip6addr:ip6addr"
  77. proto_config_add_boolean "noroutes"
  78. }
  79. [ -n "$INCLUDE_ONLY" ] || {
  80. add_protocol tayga
  81. }