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.

107 lines
2.8 KiB

  1. #!/bin/sh
  2. [ -x /usr/sbin/xl2tpd ] || exit 0
  3. [ -n "$INCLUDE_ONLY" ] || {
  4. . /lib/functions.sh
  5. . ../netifd-proto.sh
  6. init_proto "$@"
  7. }
  8. proto_l2tp_init_config() {
  9. proto_config_add_string "username"
  10. proto_config_add_string "password"
  11. proto_config_add_string "keepalive"
  12. proto_config_add_string "pppd_options"
  13. proto_config_add_boolean "ipv6"
  14. proto_config_add_int "mtu"
  15. proto_config_add_string "server"
  16. available=1
  17. no_device=1
  18. }
  19. proto_l2tp_setup() {
  20. local config="$1"
  21. local iface="$2"
  22. local optfile="/tmp/l2tp/options.${config}"
  23. local ip serv_addr server
  24. json_get_var server server && {
  25. for ip in $(resolveip -t 5 "$server"); do
  26. ( proto_add_host_dependency "$config" "$ip" )
  27. serv_addr=1
  28. done
  29. }
  30. [ -n "$serv_addr" ] || {
  31. echo "Could not resolve server address"
  32. sleep 5
  33. proto_setup_failed "$config"
  34. exit 1
  35. }
  36. if [ ! -p /var/run/xl2tpd/l2tp-control ]; then
  37. /etc/init.d/xl2tpd start
  38. fi
  39. json_get_vars ipv6 demand keepalive username password pppd_options
  40. [ "$ipv6" = 1 ] || ipv6=""
  41. if [ "${demand:-0}" -gt 0 ]; then
  42. demand="precompiled-active-filter /etc/ppp/filter demand idle $demand"
  43. else
  44. demand="persist"
  45. fi
  46. [ -n "$mtu" ] || json_get_var mtu mtu
  47. local interval="${keepalive##*[, ]}"
  48. [ "$interval" != "$keepalive" ] || interval=5
  49. mkdir -p /tmp/l2tp
  50. echo "${keepalive:+lcp-echo-interval $interval lcp-echo-failure ${keepalive%%[, ]*}}" > "${optfile}"
  51. echo "usepeerdns" >> "${optfile}"
  52. echo "nodefaultroute" >> "${optfile}"
  53. echo "${username:+user \"$username\" password \"$password\"}" >> "${optfile}"
  54. echo "ipparam \"$config\"" >> "${optfile}"
  55. echo "ifname \"l2tp-$config\"" >> "${optfile}"
  56. echo "ip-up-script /lib/netifd/ppp-up" >> "${optfile}"
  57. echo "ipv6-up-script /lib/netifd/ppp-up" >> "${optfile}"
  58. echo "ip-down-script /lib/netifd/ppp-down" >> "${optfile}"
  59. echo "ipv6-down-script /lib/netifd/ppp-down" >> "${optfile}"
  60. # Don't wait for LCP term responses; exit immediately when killed.
  61. echo "lcp-max-terminate 0" >> "${optfile}"
  62. echo "${ipv6:++ipv6} ${pppd_options}" >> "${optfile}"
  63. echo "${mtu:+mtu $mtu mru $mtu}" >> "${optfile}"
  64. xl2tpd-control add l2tp-${config} pppoptfile=${optfile} lns=${server} redial=yes redial timeout=20
  65. xl2tpd-control connect l2tp-${config}
  66. }
  67. proto_l2tp_teardown() {
  68. local interface="$1"
  69. local optfile="/tmp/l2tp/options.${interface}"
  70. case "$ERROR" in
  71. 11|19)
  72. proto_notify_error "$interface" AUTH_FAILED
  73. proto_block_restart "$interface"
  74. ;;
  75. 2)
  76. proto_notify_error "$interface" INVALID_OPTIONS
  77. proto_block_restart "$interface"
  78. ;;
  79. esac
  80. xl2tpd-control disconnect l2tp-${interface}
  81. # Wait for interface to go down
  82. while [ -d /sys/class/net/l2tp-${interface} ]; do
  83. sleep 1
  84. done
  85. xl2tpd-control remove l2tp-${interface}
  86. rm -f ${optfile}
  87. }
  88. [ -n "$INCLUDE_ONLY" ] || {
  89. add_protocol l2tp
  90. }