aboutsummaryrefslogtreecommitdiff
blob: 1b14feddf777ed45e29570ce220e50feddcac68e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Copyright (c) 2005-2023 Gentoo Authors
# Copyright (c) 2007-2008 Roy Marples <roy@marples.name>
# Released under the 2-clause BSD license.
# shellcheck shell=sh disable=SC1008

pppd_depend()
{
	program /usr/sbin/pppd
	after interface
	before dhcp
	provide ppp
}

is_ppp()
{
	[ -e /run/ppp-"${IFACE}".pid ]
}

requote()
{
	printf "'%s' " "$@"
}

pppd_version_int() {
	# 002004008 is v2.4.8
	printf '%03d' $(/usr/sbin/pppd --version | awk '/pppd version/ {print $3}' | tr '.' ' ')
}

pppd_is_ge_248()
{
	[ $(pppd_version_int) -ge 002004008 ]
}

pppd_is_ge_250()
{
	[ $(pppd_version_int) -ge 002005000 ]
}

pppd_pidfile()
{
	if pppd_is_ge_250; then
		echo "/run/pppd/ppp-${IFACE}.pid"
	else
		echo "/run/ppp-${IFACE}.pid"
	fi
}

pppd_pre_start()
{
	# Interface has to be called ppp
	[ "${IFACE%%[0-9]*}" = "ppp" ] || return 0

	# Set our base metric
	metric=4000

	if yesno ${IN_BACKGROUND}; then
		local config=
		eval config=\$config_${IFVAR}
		# If no config for ppp then don't default to DHCP
		if [ -z "${config}" ]; then
			eval config_${IFVAR}=null
		fi
		return 0
	fi

	local link= i= unit="${IFACE#ppp}" opts= routemetric=defaultroute-metric

	# https://github.com/paulusmack/ppp/commit/35e5a569c988b1ff865b02a24d9a727a00db4da9
	pppd_is_ge_248 || routemetric=defaultmetric

	# 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
		/*)
			if [ ! -e "${link}" ]; then
				eerror "${link} does not exist"
				eerror "Please verify hardware or kernel module (driver)"
				return 1
			fi
			;;
	esac

	if [ -z "${unit}" ]; then
		eerror "PPP requires a unit - use net.ppp[0-9] instead of net.ppp"
		return 1
	fi

	# We need to flatten the useless array
	set -- $(_get_array "pppd_${IFVAR}")
	opts="$@"

	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
			;;
			defaultmetric) hasdefaultmetric=true;;
			defaultroute-metric) 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}
	eval passwordset=\$\{password_${IFVAR}-x\}
	if [ -n "${username}" ] \
	&& [ -n "${password}" -o -z "${passwordset}" ]; then
		opts="plugin passwordfd.so ${opts} passwordfd 0"
	fi

	if ! ${hasdefaultmetric}; then
		local m=
		eval m=\$metric_${IFVAR}
		[ -z "${m}" ] && : $(( m = metric + $(_ifindex) ))
		opts="${opts} ${routemetric} ${m}"
	fi
	if [ -n "${mtu}" ]; then
		${hasmtu} || opts="${opts} mtu ${mtu}"
		${hasmru} || opts="${opts} mru ${mtu}"
	fi
	${hasmaxfail} || opts="${opts} maxfail 0"
	${haspersist} || opts="${opts} persist"

	# Set linkname because we need /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
	if [ -n "${username}" ]; then
		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
	local chatprog="/usr/sbin/chat -e -E -v" phone=
	eval phone=\$phone_number_${IFVAR}
	set -- ${phone}
	[ -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;;
			pppoa) shift; set -- "pppoatm" "$@";;
			capi) shift; set -- "capiplugin" "$@";;
		esac
		case "$1" in
			pppoatm)        haspppoa=true;;
			pppoe|rp-pppoe) haspppoe=true;;
		esac
		if [ "$1" = "pppoe" ] || [ "$1" = "rp-pppoe" ] || [ "$1" = "pppoatm" -a "${link}" != "/dev/null" ]; then
			opts="${opts} connect true"
			set -- "$@" "${link}"
		fi
		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
	if ${haspppoe}; then
		if [ ! -e /proc/net/pppoe ]; then
			# Load the PPPoE kernel module
			if ! modprobe pppoe; then
				eerror "kernel does not support PPPoE"
				return 1
			fi
		fi

		# Ensure that the link exists and is up
		( IFACE="${link}"; _exists true && _up ) || return 1
		insert_link_in_opts=0
	fi

	if ${haspppoa}; then
		if [ ! -d /proc/net/atm ]; then
			# Load the PPPoA kernel module
			if ! modprobe pppoatm; then
				eerror "kernel does not support PPPoATM"
				return 1
			fi
		fi

		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}"

	local pidfile="$(pppd_pidfile)"

	ebegin "Starting pppd in ${IFACE}"
	mark_service_inactive
	if [ -n "${username}" ] \
	&& [ -n "${password}" -o -z "${passwordset}" ]; then
		printf "%s" "${password}" | \
		eval start-stop-daemon --start --exec /usr/sbin/pppd \
			--pidfile "${pidfile}" -- "${opts}" >/dev/null
	else
		eval start-stop-daemon --start --exec /usr/sbin/pppd \
			--pidfile "${pidfile}" -- "${opts}" >/dev/null
	fi

	if ! eend $? "Failed to start PPP"; then
		mark_service_stopped
		return 1
	fi

	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()
{
	yesno ${IN_BACKGROUND} && return 0

	local pidfile="$(pppd_pidfile)"
	if [ ! -s "${pidfile}" ]; then
		# Try the old path.
		pidfile="/run/ppp-${IFACE}.pid"
		[ -s "${pidfile}" ] || return 0
	fi

	# 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 $?
}