aiccu.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/bin/sh
  2. # aiccu.sh - AICCU 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_aiccu_setup() {
  11. local cfg="$1"
  12. local iface="$2"
  13. local link="aiccu-$cfg"
  14. local username password protocol server ip6prefix tunnelid requiretls defaultroute nat heartbeat verbose sourcerouting ip6addr ntpsynctimeout
  15. json_get_vars username password protocol server ip6prefix tunnelid requiretls defaultroute nat heartbeat verbose sourcerouting ip6addr ntpsynctimeout
  16. [ -z "$username" -o -z "$password" ] && {
  17. proto_notify_error "$cfg" "MISSING_USERNAME_OR_PASSWORD"
  18. proto_block_restart "$cfg"
  19. return
  20. }
  21. ( proto_add_host_dependency "$cfg" 0.0.0.0 )
  22. CFGFILE="/var/etc/${link}.conf"
  23. PIDFILE="/var/run/${link}.pid"
  24. NTPSTRATUMFILE="/var/run/aiccu_ntp_stratum"
  25. mkdir -p /var/run /var/etc
  26. echo "username $username" > "$CFGFILE"
  27. echo "password $password" >> "$CFGFILE"
  28. echo "ipv6_interface $link" >> "$CFGFILE"
  29. [ -n "$server" ] && echo "server $server" >> "$CFGFILE"
  30. [ -n "$protocol" ] && echo "protocol $protocol" >> "$CFGFILE"
  31. [ -n "$tunnelid" ] && echo "tunnel_id $tunnelid" >> "$CFGFILE"
  32. [ "$requiretls" == 1 ] && echo "requiretls true" >> "$CFGFILE"
  33. [ "$nat" == 1 ] && echo "behindnat true" >> "$CFGFILE"
  34. [ "$heartbeat" == 1 ] && echo "makebeats true" >> "$CFGFILE"
  35. [ "$verbose" == 1 ] && echo "verbose true" >> "$CFGFILE"
  36. echo "defaultroute false" >> "$CFGFILE"
  37. echo "daemonize true" >> "$CFGFILE"
  38. echo "pidfile $PIDFILE" >> "$CFGFILE"
  39. # By default, wait at most 90 seconds for NTP sync
  40. [ -z "$ntpsynctimeout" ] && ntpsynctimeout=90
  41. for i in $(seq 1 $ntpsynctimeout); do
  42. [ -f "$NTPSTRATUMFILE" ] && \
  43. [ "$(cat $NTPSTRATUMFILE)" -lt 16 ] && \
  44. echo "NTP synced, stratum $(cat $NTPSTRATUMFILE)" && break
  45. [ "$(( $i % 10 ))" -eq 0 ] && echo "Waiting ${i} secs for NTP sync..."
  46. sleep 1
  47. done
  48. aiccu start "$CFGFILE"
  49. [ "$?" -ne 0 ] && {
  50. proto_notify_error "$cfg" "AICCU_FAILED_SEE_LOG"
  51. proto_block_restart "$cfg"
  52. return
  53. }
  54. proto_init_update "$link" 1
  55. local source=""
  56. [ "$sourcerouting" != "0" ] && source="::/128"
  57. [ "$defaultroute" != "0" ] && proto_add_ipv6_route "::" 0 "" "" "" "$source"
  58. [ -n "$ip6addr" ] && {
  59. local local6="${ip6addr%%/*}"
  60. local mask6="${ip6addr##*/}"
  61. [[ "$local6" = "$mask6" ]] && mask6=
  62. proto_add_ipv6_address "$local6" "$mask6"
  63. [ "$defaultroute" != "0" -a "$sourcerouting" != "0" ] && proto_add_ipv6_route "::" 0 "" "" "" "$local6/$mask6"
  64. }
  65. [ -n "$ip6prefix" ] && {
  66. proto_add_ipv6_prefix "$ip6prefix"
  67. [ "$defaultroute" != "0" -a "$sourcerouting" != "0" ] && proto_add_ipv6_route "::" 0 "" "" "" "$ip6prefix"
  68. }
  69. proto_send_update "$cfg"
  70. }
  71. proto_aiccu_teardown() {
  72. local cfg="$1"
  73. local link="aiccu-$cfg"
  74. CFGFILE="/var/etc/${link}.conf"
  75. PIDFILE="/var/run/${link}.pid"
  76. [ -f "$CFGFILE" -a -f "$PIDFILE" ] && {
  77. local pid="$(cat "$PIDFILE")"
  78. [ -d /proc/$pid -a $(cat /proc/$pid/comm) = "aiccu" ] && \
  79. aiccu stop "$CFGFILE"
  80. }
  81. }
  82. proto_aiccu_init_config() {
  83. no_device=1
  84. available=1
  85. proto_config_add_string "username"
  86. proto_config_add_string "password"
  87. proto_config_add_string "protocol"
  88. proto_config_add_string "server"
  89. proto_config_add_string "ip6addr:ip6addr"
  90. proto_config_add_string "ip6prefix:ip6addr"
  91. proto_config_add_string "tunnelid"
  92. proto_config_add_boolean "requiretls"
  93. proto_config_add_boolean "defaultroute"
  94. proto_config_add_boolean "sourcerouting"
  95. proto_config_add_boolean "nat"
  96. proto_config_add_boolean "heartbeat"
  97. proto_config_add_boolean "verbose"
  98. proto_config_add_int "ntpsynctimeout"
  99. }
  100. [ -n "$INCLUDE_ONLY" ] || {
  101. add_protocol aiccu
  102. }