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.

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