]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.ppp
pppoe: Allow hotplugging the port and better handle pppd errors
[people/stevee/network.git] / src / functions / functions.ppp
CommitLineData
5b20e43a
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
1848564d 5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
5b20e43a
MT
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
97cb552e
MT
22PPP_SUPPORTED_AUTH_METHODS="chap pap"
23
ea5f6354
MT
24EXIT_PPPD_ERROR=${EXIT_ERROR}
25EXIT_PPPD_ERROR_FATAL=$(( ${EXIT_ERROR} + 1 ))
97cb552e 26
ea5f6354
MT
27# This function monitors the pppd activity.
28
29function pppd_angel() {
30 local device="${1}"
31 assert isset device
32
33 local config_file="${2}"
34 assert isset config_file
35 shift 2
36
37 local holdoff_time
38
39 while [ $# -gt 0 ]; do
40 case "${1}" in
41 --holdoff-time=*)
42 holdoff_time="$(cli_get_val "${1}")"
43 ;;
44 *)
45 warning "Unrecognized argument: ${1}"
46 return ${EXIT_ERROR}
47 ;;
48 esac
49 shift
50 done
51
ea5f6354
MT
52 while :; do
53 # Execute ppp daemon.
54 pppd_exec "${device}" "${config_file}"
0994996d 55 local ret=${?}
acd07453 56
ea5f6354
MT
57 case "${ret}" in
58 ${EXIT_OK})
59 # pppd terminated gracefully. Propagating...
60 return ${EXIT_OK}
61 ;;
62 ${EXIT_PPPD_ERROR})
63 # pppd has a (non-fatal) error, in which case we
64 # restart it instantly, so it will try to re-establish
65 # the connection.
66 ;;
67 ${EXIT_PPPD_ERROR_FATAL})
68 # pppd has a fatal error. We cannot go on from here
69 # because there is either no chance to establish a connection
70 # without any user interaction, or we will damage the system.
acd07453 71 log ERROR "Fatal error. Not going to restart pppd."
0994996d 72 return ${EXIT_ERROR}
ea5f6354
MT
73 ;;
74 *)
75 log ERROR "Invalid return code: ${ret}"
76 return ${EXIT_ERROR}
77 ;;
78 esac
79
80 isset holdoff_time || continue
81
82 # When we got here, we need to wait a little bit and restart the
83 # ppp daemon soon.
acd07453 84 log INFO "Restarting pppd in ${holdoff_time}s"
ea5f6354
MT
85 sleep ${holdoff_time}
86 done
87}
81d0c0b9 88
ea5f6354
MT
89function pppd_exec() {
90 local device="${1}"
91 assert isset device
92
93 local config_file="${2}"
94 assert isset config_file
95 shift 2
96
97 # Execute pppd.
98 cmd "pppd file ${config_file} $@"
99 local ret=$?
100
0994996d
MT
101 # Get the hook of the zone
102 local hook
103 if zone_exists "${device}"; then
104 hook="$(zone_get_hook "${zone}")"
105 fi
106
107 log DEBUG "pppd exited with code: ${ret}"
108
ea5f6354
MT
109 # Evaluate return code.
110 local error_code
0994996d
MT
111 case "${ret},${hook}" in
112 0,*)
ea5f6354
MT
113 # Pppd has detached, or otherwise the connection was successfully
114 # established and terminated at the peer's request.
115 log DEBUG "pppd exited gracefully"
0994996d 116 return ${EXIT_OK}
81d0c0b9 117 ;;
0994996d 118 1,*)
ea5f6354
MT
119 # An immediately fatal error of some kind occurred, such as an
120 # essential system call failing, or running out of virtual memory.
121 log ERROR "pppd crashed for an unknown reason"
0994996d 122 return ${EXIT_PPPD_ERROR_FATAL}
81d0c0b9 123 ;;
0994996d 124 2,*)
ea5f6354
MT
125 # An error was detected in processing the options given, such as two
126 # mutually exclusive options being used.
127 log ERROR "pppd: Configuration error"
0994996d 128 return ${EXIT_PPPD_ERROR_FATAL}
ea5f6354 129 ;;
0994996d 130 3,*)
ea5f6354
MT
131 # Pppd is not setuid-root and the invoking user is not root.
132 log ERROR "pppd: Launched with insufficient privileges"
0994996d 133 return ${EXIT_PPPD_ERROR_FATAL}
ea5f6354 134 ;;
0994996d 135 4,*)
ea5f6354
MT
136 # The kernel does not support PPP, for example, the PPP kernel driver is
137 # not included or cannot be loaded.
138 log ERROR "pppd: Kernel does not support PPP"
0994996d 139 return ${EXIT_PPPD_ERROR_FATAL}
81d0c0b9 140 ;;
0994996d 141 5,*)
ea5f6354
MT
142 # Pppd terminated because it was sent a SIGINT, SIGTERM or SIGHUP signal.
143 log ERROR "pppd: Received SIGINT, SIGTERM or SIGHUP signal"
0994996d 144 return ${EXIT_PPPD_ERROR}
ea5f6354 145 ;;
0994996d 146 6,*)
ea5f6354
MT
147 # The serial port could not be locked.
148 log ERROR "pppd: Serial port could not be locked"
0994996d 149 return ${EXIT_PPPD_ERROR}
ea5f6354 150 ;;
0994996d 151 7,*)
ea5f6354
MT
152 # The serial port could not be opened.
153 log ERROR "pppd: Serial port could not be opened"
0994996d
MT
154 return ${EXIT_PPPD_ERROR}
155 ;;
156 8,pppoe)
157 # For PPPoE this tells us that we were not able to contact
158 # the DSLAM (i.e. timeout waiting for PADO packets)
159 log ERROR "pppd: Unable to contact the DSLAM"
160 return ${EXIT_PPPD_ERROR}
ea5f6354 161 ;;
0994996d 162 8,*)
ea5f6354
MT
163 # The connect script failed (returned a non-zero exit status).
164 log ERROR "pppd: Connect script failed"
0994996d 165 return ${EXIT_PPPD_ERROR_FATAL}
ea5f6354 166 ;;
0994996d 167 9,*)
ea5f6354
MT
168 # The command specified as the argument to the pty option could not be run.
169 log ERROR "pppd: Could not run pty command"
0994996d 170 return ${EXIT_PPPD_ERROR_FATAL}
ea5f6354 171 ;;
0994996d 172 10,*)
ea5f6354
MT
173 # The PPP negotiation failed, that is, it didn't reach the point where at
174 # least one network protocol (e.g. IP) was running.
175 log ERROR "pppd: Protocol negotiation failed"
0994996d 176 return ${EXIT_PPPD_ERROR}
ea5f6354 177 ;;
0994996d 178 11,*)
ea5f6354
MT
179 # The peer system failed (or refused) to authenticate itself.
180 log ERROR "pppd: peer system failed (or refused) to authenticate itself"
0994996d 181 return ${EXIT_PPPD_ERROR}
ea5f6354 182 ;;
0994996d 183 12,*)
ea5f6354
MT
184 # The link was established successfully and terminated because it was idle.
185 log ERROR "pppd: Terminated because of idleness"
0994996d 186 return ${EXIT_PPPD_ERROR}
ea5f6354 187 ;;
0994996d 188 13,*)
ea5f6354
MT
189 # The link was established successfully and terminated because the connect time
190 # limit was reached.
191 log ERROR "pppd: connect time limit was reached"
0994996d 192 return ${EXIT_PPPD_ERROR}
ea5f6354 193 ;;
0994996d 194 14,*)
ea5f6354
MT
195 # Callback was negotiated and an incoming call should arrive shortly.
196 # We should not be using this, so make it fatal that nobody is able to
197 # abuse the feature.
0994996d 198 return ${EXIT_PPPD_ERROR_FATAL}
ea5f6354 199 ;;
0994996d 200 15,*)
ea5f6354
MT
201 # The link was terminated because the peer is not responding to echo requests.
202 log ERROR "pppd: Peer is not responding to echo requests"
0994996d 203 return ${EXIT_PPPD_ERROR}
81d0c0b9 204 ;;
0994996d 205 16,*)
ea5f6354
MT
206 # The link was terminated by the modem hanging up.
207 log ERROR "pppd: Modem hung up"
0994996d 208 return ${EXIT_PPPD_ERROR}
ea5f6354 209 ;;
0994996d 210 17,*)
ea5f6354
MT
211 # The PPP negotiation failed because serial loopback was detected.
212 log ERROR "pppd: Serial loopback detected"
0994996d 213 return ${EXIT_PPPD_ERROR_FATAL}
ea5f6354 214 ;;
0994996d 215 18,*)
ea5f6354
MT
216 # The init script failed (returned a non-zero exit status).
217 log ERROR "pppd: Init script failed"
0994996d 218 return ${EXIT_PPPD_ERROR_FATAL}
6c74a64c 219 ;;
0994996d 220 19,*)
ea5f6354
MT
221 # We failed to authenticate ourselves to the peer.
222 log ERROR "pppd: Authentication failed"
0994996d 223 return ${EXIT_PPPD_ERROR_FATAL}
81d0c0b9
MT
224 ;;
225 *)
ea5f6354 226 log ERROR "pppd: Unhandled exit code: ${ret}"
0994996d 227 return ${EXIT_PPPD_ERROR_FATAL}
81d0c0b9
MT
228 ;;
229 esac
ea5f6354
MT
230}
231
232function pppd_start() {
233 local device="${1}"
234 assert isset device
235
236 # This will block until the connection has been established or
237 # pppd exited.
238 service_start "pppd@${device}.service"
97cb552e
MT
239}
240
241function pppd_stop() {
ea5f6354
MT
242 local device="${1}"
243 assert isset device
97cb552e 244
ea5f6354 245 service_stop "pppd@${device}.service"
97cb552e
MT
246}
247
248function pppd_status() {
ea5f6354
MT
249 local device="${1}"
250 assert isset device
97cb552e 251
ea5f6354 252 service_status "pppd@${device}.service"
97cb552e
MT
253}
254
c7ad7801
MT
255function ppp_common_ip_pre_up() {
256 local zone=${1}
257 shift
258
259 if ! zone_exists ${zone}; then
260 error "Zone '${zone}' does not exist."
261 return ${EXIT_ERROR}
262 fi
263
2c973348 264 routing_db_from_ppp ${zone} ipv4
ff8ec5ef 265
c7ad7801
MT
266 return ${EXIT_OK}
267}
268
2181765d 269function ppp_common_ipv4_up() {
c7ad7801
MT
270 local zone=${1}
271 shift
272
273 if ! zone_exists ${zone}; then
274 error "Zone '${zone}' does not exist."
275 return ${EXIT_ERROR}
276 fi
277
2c973348
MT
278 routing_db_set ${zone} ipv4 active 1
279 routing_update ${zone} ipv4
f5a771cf 280 routing_default_update
ff8ec5ef 281
c7ad7801
MT
282 return ${EXIT_OK}
283}
284
2181765d 285function ppp_common_ipv4_down() {
c7ad7801
MT
286 local zone=${1}
287 shift
288
289 if ! zone_exists ${zone}; then
290 error "Zone '${zone}' does not exist."
291 return ${EXIT_ERROR}
292 fi
293
201b7dff
MT
294 # Remove the information about this zone from the routing database
295 # and update the routing table.
296 routing_db_remove ${zone} ipv4
297 routing_update ${zone} ipv4
f5a771cf 298 routing_default_update
201b7dff
MT
299
300 # Save accounting information
301 ppp_accounting ${zone}
302
201b7dff
MT
303 return ${EXIT_OK}
304}
305
306function ppp_common_ipv6_up() {
307 local zone=${1}
308 shift
309
310 if ! zone_exists ${zone}; then
311 error "Zone '${zone}' does not exist."
312 return ${EXIT_ERROR}
313 fi
314
315 # Add information about this zone to the routing database.
316 routing_db_from_ppp ${zone} ipv6
317
318 routing_db_set ${zone} ipv6 active 1
319 routing_update ${zone} ipv6
f5a771cf 320 routing_default_update
201b7dff 321
201b7dff
MT
322 return ${EXIT_OK}
323}
324
325function ppp_common_ipv6_down() {
326 local zone=${1}
327 shift
328
329 if ! zone_exists ${zone}; then
330 error "Zone '${zone}' does not exist."
331 return ${EXIT_ERROR}
332 fi
333
334 # Remove the information about this zone from the routing database
335 # and update the routing table.
336 routing_db_remove ${zone} ipv6
337 routing_update ${zone} ipv6
f5a771cf 338 routing_default_update
201b7dff 339
059469a8
MT
340 # Save accounting information
341 ppp_accounting ${zone}
342
c7ad7801
MT
343 return ${EXIT_OK}
344}
345
5b20e43a
MT
346function ppp_secret() {
347 local USER=${1}
348 local SECRET=${2}
349 local a
350 local secret
351 local user
352
353 # Updateing secret file
354 > ${PPP_SECRETS}.tmp
355 while read user a secret; do
356 if [ "'${USER}'" != "${user}" ]; then
357 echo "${user} ${a} ${secret}" >> ${PPP_SECRETS}.tmp
358 fi
359 done < ${PPP_SECRETS}
360 echo "'${USER}' * '${SECRET}'" >> ${PPP_SECRETS}.tmp
361 cat ${PPP_SECRETS}.tmp > ${PPP_SECRETS}
362 rm -f ${PPP_SECRETS}.tmp
363}
364
059469a8
MT
365function ppp_accounting() {
366 local zone=${1}
367 shift
5b20e43a 368
059469a8
MT
369 db_ppp_update ${zone} --duration="${CONNECT_TIME}" \
370 --rcvd="${BYTES_RCVD}" --sent="${BYTES_SENT}"
5b20e43a 371}
711ffac1 372
97cb552e
MT
373function pppd_write_config() {
374 local file=${1}; shift
375 assert isset file
376
377 local auth
6c74a64c
MT
378 local baudrate
379 local connect_cmd
45d5539c 380 local default_asyncmap="true"
97cb552e 381 local interface
aadb3cd0 382 local ipv6="true"
45d5539c
MT
383 local lcp_echo_failure=3
384 local lcp_echo_interval=20
97cb552e
MT
385 local linkname
386 local mtu mru
6c74a64c 387 local password
97cb552e 388 local plugin plugin_options
7649cf73
SS
389 local pty
390 local refuses
6c74a64c
MT
391 local serial="false"
392 local username
45d5539c 393 local value
97cb552e
MT
394
395 while [ $# -gt 0 ]; do
396 case "${1}" in
397 --auth=*)
398 auth=$(cli_get_val ${1})
399 ;;
6c74a64c
MT
400 --baudrate=*)
401 baudrate=$(cli_get_val ${1})
402 assert isoneof baudrate ${SERIAL_BAUDRATES}
403 ;;
404 --connect-command=*)
405 connect_cmd=$(cli_get_val ${1})
406 ;;
45d5539c
MT
407 # Enable or disable the use of the default asyncmap.
408 --default-asyncmap=*)
409 value=$(cli_get_val ${1})
410 if enabled value; then
411 default_asyncmap="true"
412 else
413 default_asyncmap="false"
414 fi
415 ;;
97cb552e
MT
416 # The name of the created ppp interface.
417 --interface=*)
418 interface=$(cli_get_val ${1})
419 ;;
aadb3cd0
MT
420 # IPv6
421 --ipv6=*)
422 ipv6="$(cli_get_val ${1})"
423 ;;
45d5539c
MT
424 # LCP echo failure.
425 --lcr-echo-failure=*)
426 lcr_echo_failure=$(cli_get_val ${1})
427
428 if ! isinteger ${lcr_echo_failure}; then
429 error "--lcr-echo-failure= requires a number"
430 return ${EXIT_ERROR}
431 fi
432 ;;
433 # LCP echo interval.
434 --lcr-echo-interval=*)
435 lcr_echo_interval=$(cli_get_val ${1})
436
437 if ! isinteger ${lcr_echo_failure}; then
438 error "--lcr-echo-interval= requires a number"
439 return ${EXIT_ERROR}
440 fi
441 ;;
97cb552e
MT
442 # Maximum Transmission Unit
443 --mtu=*)
444 mtu=$(cli_get_val ${1})
445 ;;
446 # Maximum Receive Unit
447 --mru=*)
448 mru=$(cli_get_val ${1})
449 ;;
6c74a64c
MT
450 --password=*)
451 password=$(cli_get_val ${1})
452 ;;
97cb552e
MT
453 --plugin=*)
454 plugin=$(cli_get_val ${1})
455 ;;
456 --plugin-options=*)
457 plugin_options=$(cli_get_val ${1})
458 ;;
7649cf73
SS
459 --pty=*)
460 pty=$(cli_get_val ${1})
461 ;;
462 # Refused authentication methods
463 --refuse=*)
464 list_append refuses "$(cli_get_val "${1}")"
465 error_log "REFUSES $refuses $1"
466 ;;
6c74a64c
MT
467 # Sets if the modem is a serial device.
468 --serial=*)
469 serial=$(cli_get_val ${1})
470 ;;
471 --serial-device=*)
472 serial_device=$(cli_get_val ${1})
473 ;;
474 --username=*)
475 username=$(cli_get_val ${1})
97cb552e
MT
476 ;;
477 *)
478 log WARNING "Unhandled argument: ${1}"
479 ;;
480 esac
481 shift
482 done
483
484 if [ -z "${interface}" ]; then
485 log ERROR "You need to set the interface name: ${interface}"
486 return ${EXIT_ERROR}
487 fi
45d5539c 488 linkname="${interface}"
97cb552e
MT
489
490 if isset auth; then
491 if ! isoneof ${auth} ${PPP_SUPPORTED_AUTH_METHODS}; then
492 log ERROR "Unsupported auth method: ${auth}"
493 return ${EXIT_ERROR}
494 fi
495 fi
496
6c74a64c
MT
497 if enabled serial; then
498 assert isset serial_device
499 assert [ -c "${serial_device}" ]
500 fi
501
502 # Set the user credentials.
503 ppp_secret "${username}" "${password}"
504
97cb552e
MT
505 # Write the configuration header.
506 mkdir -p $(dirname ${file}) 2>/dev/null
507 config_header "PPP daemon configuration file" > ${file}
508
509 # At first, set the name of the link.
45d5539c 510 print "linkname ${linkname}\n" >> ${file}
97cb552e 511
6c74a64c
MT
512 # Configure the interface/zone name.
513 (
514 print "# Interface name"
515 print "ifname ${interface}"
516 print
517 ) >> ${file}
97cb552e
MT
518
519 # Plugin settings
520 if isset plugin; then
521 (
522 print "# Plugin settings"
523 print "plugin ${plugin} ${plugin_options}"
524 print
525 ) >> ${file}
526 fi
527
7649cf73
SS
528 # pty settings
529 if isset pty; then
530 (
531 print "# pty settings"
532 print "pty \"${pty}\""
533 print
534 ) >> ${file}
535 fi
536
97cb552e 537 # User authentication
6c74a64c 538 if isset username; then
97cb552e
MT
539 (
540 print "# User authentication"
6c74a64c 541 print "user ${username}"
97cb552e
MT
542
543 print "noauth"
544 if isset auth; then
545 print "require-${auth}"
546 fi
7649cf73
SS
547
548 # Refused authentication methods
549 for refuse in ${refuses}; do
550 print "refuse-${refuse}"
551 done
97cb552e
MT
552 print
553 ) >> ${file}
554 fi
555
aadb3cd0
MT
556 # IPv6
557 if enabled ipv6; then
558 (
559 print "# IPv6 support"
560 print "+ipv6"
561 print
562 ) >> ${file}
563 fi
564
97cb552e
MT
565 # MTU/MRU settings
566 if isset mtu; then
567 isset mru || mru=${mtu}
568
569 (
570 print "# MTU/MRU settings"
571 print "mtu ${mtu}"
572 print "mru ${mru}"
573 print
574 ) >> ${file}
575 fi
576
6c74a64c
MT
577 if enabled serial; then
578 (
579 print "# Serial modem settings"
580 print "${serial_device} ${baudrate}"
581 print "crtscts"
582 print "lock"
583 print "modem"
584 print
585 ) >> ${file}
586
587 # Connect command
588 if isset connect_cmd; then
589 (
590 print "# Connect command"
591 print "connect \"${connect_cmd}\""
592 print
593 ) >> ${file}
594 fi
595 fi
596
45d5539c
MT
597 # Default asyncmap.
598 if enabled default_asyncmap; then
599 (
600 print "# Use the default asyncmap."
601 print "default-asyncmap"
602 print
603 ) >> ${file}
604 fi
605
606 # LCP settings.
607 (
608 print "# LCP settings"
609 print "lcp-echo-failure ${lcp_echo_failure}"
610 print "lcp-echo-interval ${lcp_echo_interval}"
611 print
612 ) >> ${file}
613
97cb552e
MT
614 # Add the default settings.
615 (
616 print "# Disable the compression"
617 print "noccp noaccomp nodeflate nopcomp novj novjccomp nobsdcomp nomppe"
618
ea5f6354 619 print "noipdefault nodetach debug"
97cb552e
MT
620 ) >> ${file}
621
622 return ${EXIT_OK}
623}