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.

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