f2a4d551e8834b4d8b15e9a066197a4732391168
[openwrt/svn-archive/archive.git] / net / xl2tpd / files / l2tp.sh
1 find_route() {
2 ip route get $1 | sed -e 's/ /\n/g' | \
3 sed -ne '1p;/via/{N;p};/dev/{N;p};/src/{N;p};/mtu/{N;p}'
4 }
5
6 scan_l2tp() {
7 config_set "$1" device "l2tp-$1"
8 }
9
10 stop_interface_l2tp() {
11 local config="$1"
12 local lock="/var/lock/l2tp-${config}"
13 local optfile="/tmp/l2tp/options.${config}"
14 local l2tpcontrol=/var/run/xl2tpd/l2tp-control
15
16 lock "$lock"
17
18 [ -p ${l2tpcontrol} ] && echo "r l2tp-${config}" > ${l2tpcontrol}
19 rm -f ${optfile}
20
21 for ip in $(uci_get_state network "$1" serv_addrs); do
22 ip route del "$ip" 2>/dev/null
23 done
24
25 lock -u "$lock"
26 }
27
28 setup_interface_l2tp() {
29 local config="$2"
30 local lock="/var/lock/l2tp-${config}"
31 local optfile="/tmp/l2tp/options.${config}"
32
33 lock "$lock"
34
35 if [ ! -p /var/run/xl2tpd/l2tp-control ]; then
36 /etc/init.d/xl2tpd start
37 fi
38
39 local device
40 config_get device "$config" device "l2tp-$config"
41
42 local server
43 config_get server "$config" server
44
45 local username
46 config_get username "$config" username
47
48 local password
49 config_get password "$config" password
50
51 local keepalive
52 config_get keepalive "$config" keepalive
53
54 local pppd_options
55 config_get pppd_options "$config" pppd_options
56
57 local defaultroute
58 config_get_bool defaultroute "$config" defaultroute 1
59 [ "$defaultroute" -eq 1 ] && \
60 defaultroute="defaultroute replacedefaultroute" || defaultroute="nodefaultroute"
61
62 local interval="${keepalive##*[, ]}"
63 [ "$interval" != "$keepalive" ] || interval=5
64
65 local dns
66 config_get dns "$config" dns
67
68 local has_dns=0
69 local peer_default=1
70 [ -n "$dns" ] && {
71 has_dns=1
72 peer_default=0
73 }
74
75 local peerdns
76 config_get_bool peerdns "$config" peerdns $peer_default
77
78 [ "$peerdns" -eq 1 ] && {
79 peerdns="usepeerdns"
80 } || {
81 peerdns=""
82 add_dns "$config" $dns
83 }
84
85 local ipv6
86 config_get ipv6 "$config" ipv6 1
87 [ "$ipv6" -eq 1 ] && ipv6="+ipv6" || ipv6=""
88
89 local serv_addrs=""
90 for ip in $(resolveip -t 3 "$server"); do
91 append serv_addrs "$ip"
92 ip route replace $(find_route $ip)
93 done
94 uci_toggle_state network "$config" serv_addrs "$serv_addrs"
95
96 # fix up the netmask
97 config_get netmask "$config" netmask
98 [ -z "$netmask" -o -z "$device" ] || ifconfig $device netmask $netmask
99
100 config_get mtu "$config" mtu
101
102 mkdir -p /tmp/l2tp
103
104 echo ${keepalive:+lcp-echo-interval $interval lcp-echo-failure ${keepalive%%[, ]*}} > "${optfile}"
105 echo "$peerdns" >> "${optfile}"
106 echo "$defaultroute" >> "${optfile}"
107 echo "${username:+user \"$username\" password \"$password\"}" >> "${optfile}"
108 echo "ipparam \"$config\"" >> "${optfile}"
109 echo "ifname \"l2tp-$config\"" >> "${optfile}"
110 # Don't wait for LCP term responses; exit immediately when killed.
111 echo "lcp-max-terminate 0" >> "${optfile}"
112 echo "${ipv6} ${pppd_options}" >> "${optfile}"
113
114 xl2tpd-control remove l2tp-${config}
115 # Wait and ensure pppd has died.
116 while [ -d /sys/class/net/l2tp-${config} ]; do
117 sleep 1
118 done
119
120 xl2tpd-control add l2tp-${config} pppoptfile=${optfile} lns=${server} redial=yes redial timeout=20
121 xl2tpd-control connect l2tp-${config}
122
123 lock -u "${lock}"
124 }