1 # l2tp.sh - L2TPv3 tunnel backend
2 # Copyright (c) 2010 OpenWrt.org
4 l2tp_next_tunnel_id
() {
9 l2tpv3tun show tunnel |
while read l
; do
11 Tunnel
*,*encap
*) l
="${l#Tunnel }"; echo "${l%%,*}";;
15 [ "$val" -gt "$max" ] && max
="$val"
20 l2tp_next_session_id
() {
26 l2tpv3tun show session
${tunnel:+ tunnel_id "$tunnel"} |
while read l
; do
28 Session
*in*) l
="${l#Session }"; echo "${l%% *}";;
32 [ "$val" -gt "$max" ] && max
="$val"
37 l2tp_tunnel_exists
() {
38 test -n "$(l2tpv3tun show tunnel tunnel_id "$1" 2>/dev/null)"
41 l2tp_session_exists
() {
42 test -n "$(l2tpv3tun show session tunnel_id "$1" session_id "$2" 2>/dev/null)"
46 l2tpv3tun show session tunnel_id
"$1" session_id
"$2" 2>/dev
/null | \
47 sed -ne 's/^.*interface name: //p'
51 lock
/var
/lock
/l2tp-setup
55 lock
-u /var
/lock
/l2tp-setup
59 logger
-t "ifup-l2tp" "$@"
63 # Hook into scan_interfaces() to synthesize a .device option
64 # This is needed for /sbin/ifup to properly dispatch control
65 # to setup_interface_l2tp() even if no .ifname is set in
69 config_get dev
"$1" device
70 config_set
"$1" device
"${dev:+$dev }l2tp-$1"
73 coldplug_interface_l2tp
() {
74 setup_interface_l2tp
"l2tp-$1" "$1"
77 setup_interface_l2tp
() {
80 local link
="l2tp-$cfg"
85 local up
="$(uci_get_state network "$cfg" up 0)"
92 config_get tunnel_id
"$cfg" tunnel_id
93 [ -n "$tunnel_id" ] ||
{
94 tunnel_id
="$(l2tp_next_tunnel_id)"
95 uci_set_state network
"$cfg" tunnel_id
"$tunnel_id"
96 l2tp_log
"No tunnel ID specified, assuming $tunnel_id"
100 config_get peer_tunnel_id
"$cfg" peer_tunnel_id
101 [ -n "$peer_tunnel_id" ] ||
{
102 peer_tunnel_id
="$tunnel_id"
103 uci_set_state network
"$cfg" peer_tunnel_id
"$peer_tunnel_id"
104 l2tp_log
"No peer tunnel ID specified, assuming $peer_tunnel_id"
108 config_get encap
"$cfg" encap udp
111 [ "$encap" = udp
] && {
112 config_get sport
"$cfg" sport
1701
113 config_get dport
"$cfg" dport
1701
117 config_get peeraddr
"$cfg" peeraddr
118 [ -z "$peeraddr" ] && config_get peeraddr
"$cfg" peer6addr
122 *:*) config_get localaddr
"$cfg" local6addr
;;
123 *) config_get localaddr
"$cfg" localaddr
;;
126 [ -n "$localaddr" -a -n "$peeraddr" ] ||
{
127 l2tp_log
"Missing local or peer address for tunnel $cfg - skipping"
132 while ! l2tp_tunnel_exists
"$tunnel_id"; do
133 [ -n "$sport" ] && l2tpv3tun show tunnel
2>/dev
/null |
grep -q "ports: $sport/" && {
134 l2tp_log
"There already is a tunnel with src port $sport - skipping"
139 l2tpv3tun add tunnel tunnel_id
"$tunnel_id" peer_tunnel_id
"$peer_tunnel_id" \
140 encap
"$encap" local "$localaddr" remote
"$peeraddr" \
141 ${sport:+udp_sport "$sport"} ${dport:+udp_dport "$dport"}
149 config_get session_id
"$cfg" session_id
150 [ -n "$session_id" ] ||
{
151 session_id
="$(l2tp_next_session_id "$tunnel_id")"
152 uci_set_state network
"$cfg" session_id
"$session_id"
153 l2tp_log
"No session ID specified, assuming $session_id"
156 local peer_session_id
157 config_get peer_session_id
"$cfg" peer_session_id
158 [ -n "$peer_session_id" ] ||
{
159 peer_session_id
="$session_id"
160 uci_set_state network
"$cfg" peer_session_id
"$peer_session_id"
161 l2tp_log
"No peer session ID specified, assuming $peer_session_id"
165 while ! l2tp_session_exists
"$tunnel_id" "$session_id"; do
166 l2tpv3tun add session ifname
"$link" tunnel_id
"$tunnel_id" \
167 session_id
"$session_id" peer_session_id
"$peer_session_id"
175 config_get dev
"$cfg" device
178 config_get ifn
"$cfg" ifname
180 uci_set_state network
"$cfg" ifname
"${ifn:-$dev}"
181 uci_set_state network
"$cfg" device
"$dev"
184 config_get mtu
"$cfg" mtu
1462
187 config_get ttl
"$cfg" ttl
189 ip link
set mtu
"$mtu" ${ttl:+ ttl "$ttl"} dev
"$link"
191 # IP setup inherited from proto static
192 prepare_interface
"$link" "$cfg"
193 setup_interface_static
"${ifn:-$dev}" "$cfg"
195 ip link
set up dev
"$link"
197 uci_set_state network
"$cfg" up
1
202 stop_interface_l2tp
() {
204 local link
="l2tp-$cfg"
206 local tunnel
=$
(uci_get_state network
"$cfg" tunnel_id
)
207 local session
=$
(uci_get_state network
"$cfg" session_id
)
209 [ -n "$tunnel" ] && [ -n "$session" ] && {
210 l2tpv3tun del session tunnel_id
"$tunnel" session_id
"$session"
211 l2tpv3tun del tunnel tunnel_id
"$tunnel"