openrc/net/pppd.sh

244 lines
5.8 KiB
Bash
Raw Normal View History

2009-05-01 19:41:40 +05:30
# Copyright (c) 2005-2007 Gentoo Foundation
# Copyright (c) 2007-2008 Roy Marples <roy@marples.name>
# Released under the 2-clause BSD license.
pppd_depend()
{
program /usr/sbin/pppd
after interface
before dhcp
provide ppp
}
is_ppp()
{
[ -e /var/run/ppp-"${IFACE}".pid ]
}
requote()
{
printf "'%s' " "$@"
}
pppd_pre_start()
{
# Interface has to be called ppp
[ "${IFACE%%[0-9]*}" = "ppp" ] || return 0
2007-08-08 02:31:16 +05:30
# Set our base metric
metric=4000
2007-11-22 18:58:14 +05:30
if yesno ${IN_BACKGROUND}; then
2007-04-19 18:30:59 +05:30
local config=
eval config=\$config_${IFVAR}
# If no config for ppp then don't default to DHCP
2007-11-28 21:15:03 +05:30
if [ -z "${config}" ]; then
2007-08-08 01:53:38 +05:30
eval config_${IFVAR}=null
2007-04-19 18:30:59 +05:30
fi
return 0
fi
2007-11-02 16:32:07 +05:30
local link= i= unit="${IFACE#ppp}" opts=
# PPP requires a link to communicate over - normally a serial port
# PPPoE communicates over Ethernet
# PPPoA communicates over ATM
# In all cases, the link needs to be available before we start PPP
eval link=\$link_${IFVAR}
[ -n "${link}" ] || return 0
case "${link}" in
/*)
2007-11-28 21:15:03 +05:30
if [ ! -e "${link}" ]; then
eerror "${link} does not exist"
eerror "Please verify hardware or kernel module (driver)"
return 1
fi
;;
esac
2007-11-28 21:15:03 +05:30
if [ -z "${unit}" ]; then
eerror "PPP requires a unit - use net.ppp[0-9] instead of net.ppp"
return 1
fi
2007-11-02 16:32:07 +05:30
# We need to flatten the useless array
set -- $(_get_array "pppd_${IFVAR}")
opts="$@"
2007-08-08 01:53:38 +05:30
local mtu= hasmtu=false hasmru=false hasmaxfail=false haspersist=false
local hasupdetach=false hasdefaultmetric=false
for i in ${opts}; do
case "${i}" in
unit|nodetach|linkname)
eerror "The option \"${i}\" is not allowed in pppd_${IFVAR}"
return 1
;;
2007-11-28 21:15:03 +05:30
defaultmetric) hasdefaultmetric=true;;
mtu) hasmtu=true;;
mru) hasmru=true;;
maxfail) hasmaxfail=true;;
persist) haspersist=true;;
updetach) hasupdetach=true;;
esac
done
# Might be set in conf.d/net
local username= password= passwordset=
eval username=\$username_${IFVAR}
eval password=\$password_${IFVAR}
2007-08-08 01:53:38 +05:30
eval passwordset=\$\{password_${IFVAR}-x\}
if [ -n "${username}" ] \
2007-11-28 21:15:03 +05:30
&& [ -n "${password}" -o -z "${passwordset}" ]; then
2009-11-05 01:10:52 +05:30
opts="plugin passwordfd.so ${opts} passwordfd 0"
fi
2007-11-28 21:15:03 +05:30
if ! ${hasdefaultmetric}; then
2007-08-21 13:51:40 +05:30
local m=
eval m=\$metric_${IFVAR}
[ -z "${m}" ] && : $(( m = metric + $(_ifindex) ))
2007-08-08 02:31:16 +05:30
opts="${opts} defaultmetric ${m}"
fi
2007-11-28 21:15:03 +05:30
if [ -n "${mtu}" ]; then
2007-04-18 20:12:40 +05:30
${hasmtu} || opts="${opts} mtu ${mtu}"
${hasmru} || opts="${opts} mru ${mtu}"
fi
2007-08-08 01:53:38 +05:30
${hasmaxfail} || opts="${opts} maxfail 0"
2007-04-18 20:12:40 +05:30
${haspersist} || opts="${opts} persist"
# Set linkname because we need /var/run/ppp-${linkname}.pid
# This pidfile has the advantage of being there,
# even if ${IFACE} interface was never started
opts="linkname ${IFACE} ${opts}"
# Setup auth info
2007-11-28 21:15:03 +05:30
if [ -n "${username}" ]; then
2007-04-18 20:12:40 +05:30
opts="user '${username}' remotename ${IFACE} ${opts}"
fi
# Load a custom interface configuration file if it exists
[ -f "/etc/ppp/options.${IFACE}" ] \
&& opts="${opts} file '/etc/ppp/options.${IFACE}'"
# Set unit
opts="unit ${unit} ${opts}"
# Setup connect script
2007-11-02 16:32:07 +05:30
local chatprog="/usr/sbin/chat -e -E -v" phone=
eval phone=\$phone_number_${IFVAR}
set -- ${phone}
2007-11-02 16:32:07 +05:30
[ -n "$1" ] && chatprog="${chatprog} -T '$1'"
[ -n "$2" ] && chatprog="${chatprog} -U '$2'"
# We need to flatten the useless array
set -- $(_get_array "chat_${IFVAR}")
if [ $# != 0 ]; then
opts="${opts} connect '$(echo ${chatprog} $@ | sed -e "s:':'\\\\'':g")'"
fi
# Add plugins
local haspppoa=false haspppoe=false plugins="$(_get_array "plugins_${IFVAR}")"
local IFS="$__IFS"
for i in ${plugins}; do
unset IFS
set -- ${i}
case "$1" in
passwordfd) continue;;
2007-11-28 21:15:03 +05:30
pppoa) shift; set -- "pppoatm" "$@";;
pppoe) shift; set -- "rp-pppoe" "$@";;
capi) shift; set -- "capiplugin" "$@";;
esac
case "$1" in
2007-11-28 21:15:03 +05:30
rp-pppoe) haspppoe=true;;
pppoatm) haspppoa=true;;
esac
2007-11-28 21:15:03 +05:30
if [ "$1" = "rp-pppoe" ] || [ "$1" = "pppoatm" -a "${link}" != "/dev/null" ]; then
opts="${opts} connect true"
set -- "$@" "${link}"
fi
2009-11-05 01:10:52 +05:30
opts="plugin $1.so ${opts}"
shift
opts="${opts} $@"
done
unset IFS
#Specialized stuff. Insert here actions particular to connection type (pppoe,pppoa,capi)
local insert_link_in_opts=1
2007-11-28 21:15:03 +05:30
if ${haspppoe}; then
if [ ! -e /proc/net/pppoe ]; then
# Load the PPPoE kernel module
2007-11-28 21:15:03 +05:30
if ! modprobe pppoe; then
eerror "kernel does not support PPPoE"
return 1
fi
fi
# Ensure that the link exists and is up
2007-11-28 21:15:03 +05:30
( IFACE="${link}"; _exists true && _up ) || return 1
insert_link_in_opts=0
fi
2007-11-28 21:15:03 +05:30
if ${haspppoa}; then
if [ ! -d /proc/net/atm ]; then
# Load the PPPoA kernel module
2007-11-28 21:15:03 +05:30
if ! modprobe pppoatm; then
eerror "kernel does not support PPPoATM"
return 1
fi
fi
2007-11-28 21:15:03 +05:30
if [ "${link}" != "/dev/null" ]; then
insert_link_in_opts=0
else
ewarn "WARNING: An [itf.]vpi.vci ATM address was expected in link_${IFVAR}"
fi
fi
[ "${insert_link_in_opts}" = "0" ] || opts="${link} ${opts}"
ebegin "Starting pppd in ${IFACE}"
2007-12-18 23:29:29 +05:30
mark_service_inactive
if [ -n "${username}" ] \
2007-11-28 21:15:03 +05:30
&& [ -n "${password}" -o -z "${passwordset}" ]; then
2007-04-19 17:39:19 +05:30
printf "%s" "${password}" | \
eval start-stop-daemon --start --exec /usr/sbin/pppd \
--pidfile "/var/run/ppp-${IFACE}.pid" -- "${opts}" >/dev/null
else
eval start-stop-daemon --start --exec /usr/sbin/pppd \
--pidfile "/var/run/ppp-${IFACE}.pid" -- "${opts}" >/dev/null
fi
2007-11-28 21:15:03 +05:30
if ! eend $? "Failed to start PPP"; then
2007-12-18 23:29:29 +05:30
mark_service_stopped
return 1
fi
2007-11-28 21:15:03 +05:30
if ${hasupdetach}; then
_show_address
else
einfo "Backgrounding ..."
fi
# pppd will re-call us when we bring the interface up
exit 0
}
# Dummy function for users that still have config_ppp0="ppp"
pppd_start()
{
return 0
}
pppd_stop()
{
2007-11-22 18:58:14 +05:30
yesno ${IN_BACKGROUND} && return 0
local pidfile="/var/run/ppp-${IFACE}.pid"
[ ! -s "${pidfile}" ] && return 0
# Give pppd at least 30 seconds do die, #147490
einfo "Stopping pppd on ${IFACE}"
start-stop-daemon --stop --quiet --exec /usr/sbin/pppd \
--pidfile "${pidfile}" --retry 30
eend $?
}