1
0

l2tp.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. teardown_on_l3_link_down=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 keepalive username password pppd_options mtu
  53. json_get_vars ipv6 keepalive username password pppd_options mtu
  54. [ "$ipv6" = 1 ] || ipv6=""
  55. local interval="${keepalive##*[, ]}"
  56. [ "$interval" != "$keepalive" ] || interval=5
  57. keepalive="${keepalive:+lcp-echo-interval $interval lcp-echo-failure ${keepalive%%[, ]*}}"
  58. username="${username:+user \"$username\" password \"$password\"}"
  59. ipv6="${ipv6:++ipv6}"
  60. mtu="${mtu:+mtu $mtu mru $mtu}"
  61. mkdir -p /tmp/l2tp
  62. cat <<EOF >"$optfile"
  63. usepeerdns
  64. nodefaultroute
  65. ipparam "$interface"
  66. ifname "l2tp-$interface"
  67. ip-up-script /lib/netifd/ppp-up
  68. ipv6-up-script /lib/netifd/ppp-up
  69. ip-down-script /lib/netifd/ppp-down
  70. ipv6-down-script /lib/netifd/ppp-down
  71. # Don't wait for LCP term responses; exit immediately when killed.
  72. lcp-max-terminate 0
  73. $keepalive
  74. $username
  75. $ipv6
  76. $mtu
  77. $pppd_options
  78. EOF
  79. xl2tpd-control add l2tp-${interface} pppoptfile=${optfile} lns=${server} || {
  80. echo "xl2tpd-control: Add l2tp-$interface failed" >&2
  81. proto_setup_failed "$interface"
  82. exit 1
  83. }
  84. xl2tpd-control connect l2tp-${interface} || {
  85. echo "xl2tpd-control: Connect l2tp-$interface failed" >&2
  86. proto_setup_failed "$interface"
  87. exit 1
  88. }
  89. }
  90. proto_l2tp_teardown() {
  91. local interface="$1"
  92. local optfile="/tmp/l2tp/options.${interface}"
  93. rm -f ${optfile}
  94. if [ -p /var/run/xl2tpd/l2tp-control ]; then
  95. xl2tpd-control remove l2tp-${interface} || {
  96. echo "xl2tpd-control: Remove l2tp-$interface failed" >&2
  97. }
  98. fi
  99. # Wait for interface to go down
  100. while [ -d /sys/class/net/l2tp-${interface} ]; do
  101. sleep 1
  102. done
  103. }
  104. [ -n "$INCLUDE_ONLY" ] || {
  105. add_protocol l2tp
  106. }