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.

92 lines
2.2 KiB

  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
  15. json_get_vars ipv4_addr ipv6_addr prefix dynamic_pool ipaddr ip6addr
  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. [ -n "$ipv6_addr" ] && proto_add_ipv6_route "$ipv6_addr" "128"
  45. [ -n "$dynamic_pool" ] && {
  46. local pool="${dynamic_pool%%/*}"
  47. local mask="${dynamic_pool##*/}"
  48. proto_add_ipv4_route "$pool" "$mask"
  49. }
  50. [ -n "$prefix" ] && {
  51. local prefix6="${prefix%%/*}"
  52. local mask6="${prefix##*/}"
  53. proto_add_ipv6_route "$prefix6" "$mask6"
  54. }
  55. proto_send_update "$cfg"
  56. proto_run_command "$cfg" tayga -n -c $tmpconf \
  57. -p /var/run/$link.pid
  58. }
  59. proto_tayga_teardown() {
  60. local cfg="$1"
  61. local tmpconf="/var/etc/tayga-$cfg.conf"
  62. proto_kill_command "$cfg"
  63. sleep 1
  64. tayga -c $tmpconf --rmtun
  65. }
  66. proto_tayga_init_config() {
  67. no_device=1
  68. available=1
  69. proto_config_add_string "ipv4_addr"
  70. proto_config_add_string "ipv6_addr"
  71. proto_config_add_string "prefix"
  72. proto_config_add_string "dynamic_pool"
  73. proto_config_add_string "ipaddr"
  74. proto_config_add_string "ip6addr:ip6addr"
  75. }
  76. [ -n "$INCLUDE_ONLY" ] || {
  77. add_protocol tayga
  78. }