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.

126 lines
3.0 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 "demand"
  15. proto_config_add_int "mtu"
  16. proto_config_add_int "checkup_interval"
  17. proto_config_add_string "server"
  18. available=1
  19. no_device=1
  20. no_proto_task=1
  21. }
  22. proto_l2tp_setup() {
  23. local interface="$1"
  24. local optfile="/tmp/l2tp/options.${interface}"
  25. local ip serv_addr server host
  26. json_get_var server server
  27. host="${server%:*}"
  28. for ip in $(resolveip -t 5 "$host"); do
  29. ( proto_add_host_dependency "$interface" "$ip" )
  30. serv_addr=1
  31. done
  32. [ -n "$serv_addr" ] || {
  33. echo "Could not resolve server address" >&2
  34. sleep 5
  35. proto_setup_failed "$interface"
  36. exit 1
  37. }
  38. # Start and wait for xl2tpd
  39. if [ ! -p /var/run/xl2tpd/l2tp-control -o -z "$(pidof xl2tpd)" ]; then
  40. /etc/init.d/xl2tpd restart
  41. local wait_timeout=0
  42. while [ ! -p /var/run/xl2tpd/l2tp-control ]; do
  43. wait_timeout=$(($wait_timeout + 1))
  44. [ "$wait_timeout" -gt 5 ] && {
  45. echo "Cannot find xl2tpd control file." >&2
  46. proto_setup_failed "$interface"
  47. exit 1
  48. }
  49. sleep 1
  50. done
  51. fi
  52. local ipv6 demand keepalive username password pppd_options mtu
  53. json_get_vars ipv6 demand keepalive username password pppd_options mtu
  54. [ "$ipv6" = 1 ] || ipv6=""
  55. if [ "${demand:-0}" -gt 0 ]; then
  56. demand="precompiled-active-filter /etc/ppp/filter demand idle $demand"
  57. else
  58. demand="persist"
  59. fi
  60. local interval="${keepalive##*[, ]}"
  61. [ "$interval" != "$keepalive" ] || interval=5
  62. keepalive="${keepalive:+lcp-echo-interval $interval lcp-echo-failure ${keepalive%%[, ]*}}"
  63. username="${username:+user \"$username\" password \"$password\"}"
  64. ipv6="${ipv6:++ipv6}"
  65. mtu="${mtu:+mtu $mtu mru $mtu}"
  66. mkdir -p /tmp/l2tp
  67. cat <<EOF >"$optfile"
  68. usepeerdns
  69. nodefaultroute
  70. ipparam "$interface"
  71. ifname "l2tp-$interface"
  72. ip-up-script /lib/netifd/ppp-up
  73. ipv6-up-script /lib/netifd/ppp-up
  74. ip-down-script /lib/netifd/ppp-down
  75. ipv6-down-script /lib/netifd/ppp-down
  76. # Don't wait for LCP term responses; exit immediately when killed.
  77. lcp-max-terminate 0
  78. $keepalive
  79. $username
  80. $ipv6
  81. $mtu
  82. $pppd_options
  83. EOF
  84. xl2tpd-control add l2tp-${interface} pppoptfile=${optfile} lns=${server} || {
  85. echo "xl2tpd-control: Add l2tp-$interface failed" >&2
  86. proto_setup_failed "$interface"
  87. exit 1
  88. }
  89. xl2tpd-control connect l2tp-${interface} || {
  90. echo "xl2tpd-control: Connect l2tp-$interface failed" >&2
  91. proto_setup_failed "$interface"
  92. exit 1
  93. }
  94. }
  95. proto_l2tp_teardown() {
  96. local interface="$1"
  97. local optfile="/tmp/l2tp/options.${interface}"
  98. rm -f ${optfile}
  99. if [ -p /var/run/xl2tpd/l2tp-control ]; then
  100. xl2tpd-control remove l2tp-${interface} || {
  101. echo "xl2tpd-control: Remove l2tp-$interface failed" >&2
  102. }
  103. fi
  104. # Wait for interface to go down
  105. while [ -d /sys/class/net/l2tp-${interface} ]; do
  106. sleep 1
  107. done
  108. }
  109. [ -n "$INCLUDE_ONLY" ] || {
  110. add_protocol l2tp
  111. }