l2tp.sh 3.1 KB

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