Remove error message if no ppp0.rrd exist yet.
[people/pmueller/ipfire-2.x.git] / src / initscripts / init.d / networking / red
CommitLineData
d1e90efc
MT
1#!/bin/sh
2########################################################################
3# Begin
4#
5# Description : RED Device Script
6#
7# Authors : Michael Tremer - mitch@ipfire.org
8# Maniacikarus - maniacikarus@ipfire.org
9# Inspired by : Nathan Coulson - nathan@linuxfromscratch.org
10# Kevin P. Fleming - kpfleming@linuxfromscratch.org
11#
12# Version : 01.00
13#
14# Notes :
15#
16########################################################################
17
40049855
AF
18
19
d1e90efc
MT
20. /etc/sysconfig/rc
21. ${rc_functions}
1f2ff0fa
AF
22
23eval $(/usr/local/bin/readhash /var/ipfire/main/settings)
24if [ "$RRDLOG" == "" ]; then
25 RRDLOG=/var/log/rrd
26fi
27
d1e90efc
MT
28eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
29
30TYPE="${RED_TYPE}"
31DEVICE="${RED_DEV}"
32
0dde24fa
MT
33if [ "$TYPE" == "STATIC" ] || [ "$TYPE" == "DHCP" ]; then
34 if [ "$DEVICE" == "" ]; then
35 boot_mesg "No device for red network. Please run setup." ${FAILURE}
36 echo_failure
37 [ "${1}" == "start" ] && exit 0
38 fi
39fi
40
41if [ "${TYPE}" == "STATIC" ]; then
42 if [ "${DEVICE}" != "${GREEN_DEV}" ]; then
43 ADDRESS="${RED_ADDRESS}"
44 BROADCAST="${RED_BROADCAST}"
45 NETADDRESS="${RED_NETADDRESS}"
46 NETMASK="${RED_NETMASK}"
47 else
48 ADDRESS="${GREEN_ADDRESS}"
49 BROADCAST="${GREEN_BROADCAST}"
50 NETADDRESS="${GREEN_NETADDRESS}"
51 NETMASK="${GREEN_NETMASK}"
52 fi
d1e90efc 53 GATEWAY="${DEFAULT_GATEWAY}"
0db33b56
MT
54 # DNS1
55 # DNS2
d1e90efc
MT
56
57 if [ -z "${BROADCAST}" ]; then
0dde24fa 58 boot_mesg "BROADCAST variable missing, cannot continue." ${FAILURE}
d1e90efc
MT
59 echo_failure
60 exit 1
61 fi
62 if [ -n "${ADDRESS}" -a -n "${NETMASK}" ]; then
63 PREFIX=`whatmask ${NETMASK} | grep -e ^CIDR | awk -F': ' '{ print $2 }' | cut -c 2-`
64 args="${args} ${ADDRESS}/${PREFIX} broadcast ${BROADCAST}"
65 else
66 boot_mesg "ADDRESS and/or NETMASK variable missing from input, cannot continue." ${FAILURE}
67 echo_failure
68 exit 1
69 fi
70
71elif [ "${TYPE}" == "DHCP" ]; then
72
73 PIDFILE="/var/run/dhcpcd-${DEVICE}.pid"
74 LEASEINFO="/var/ipfire/dhcpc/dhcpcd-${DEVICE}.info"
75 DHCP_START="-N -R -L /var/ipfire/dhcpc -c /var/ipfire/dhcpc/dhcpcd.exe "
76 DHCP_STOP="-k -c /var/ipfire/dhcpc/dhcpcd.exe "
77
78fi
79
80case "${1}" in
81 start)
0dde24fa
MT
82 if [ "${DEVICE}" != "${GREEN_DEV}" ] && [ "${DEVICE}" != "" ]; then
83 boot_mesg "Bringing up the ${DEVICE} interface..."
84 boot_mesg_flush
85 # Check if an interface is there...
86 if ip link show ${DEVICE} > /dev/null 2>&1; then
87 link_status=`ip link show ${DEVICE} 2> /dev/null`
88 if [ -n "${link_status}" ]; then
89 if ! echo "${link_status}" | grep -q UP; then
90 ip link set ${DEVICE} up
91 fi
d1e90efc 92 fi
0dde24fa
MT
93 else
94 boot_mesg "Interface ${DEVICE} doesn't exist." ${FAILURE}
95 echo_failure
96 exit 1
d1e90efc 97 fi
d1e90efc 98 fi
6c33dc5c 99
1e4656cd
AF
100 ## Create & Enable vnstat
101 /usr/bin/vnstat -u -i ${DEVICE} -r --enable --force > /dev/null 2>&1
d1e90efc
MT
102
103 if [ "${TYPE}" == "STATIC" ]; then
0dde24fa
MT
104
105 if [ "$DEVICE" != "${GREEN_DEV}" ]; then
106 boot_mesg "Adding IPv4 address ${ADDRESS} to the ${DEVICE} interface..."
107 ip addr add ${args} dev ${DEVICE}
108 evaluate_retval
109 fi
0db33b56
MT
110 echo -n "${DEVICE}" > /var/ipfire/red/iface
111 echo -n "${ADDRESS}" > /var/ipfire/red/local-ipaddress
112 echo -n "${GATEWAY}" > /var/ipfire/red/remote-ipaddress
b2f872eb
AF
113 grep -v "gateway" /etc/hosts > /tmp/hosts
114 echo "$GATEWAY gateway" >> /tmp/hosts
115 mv /tmp/hosts /etc/hosts
0db33b56
MT
116 echo -n "${DNS1}" > /var/ipfire/red/dns1
117 echo -n "${DNS2}" > /var/ipfire/red/dns2
bcdde652 118 touch /var/ipfire/red/active
d1e90efc 119
040e5040
MT
120 boot_mesg "Setting up default gateway ${GATEWAY}..."
121 ip route add default via ${GATEWAY} dev ${DEVICE}
122 evaluate_retval
123
0e42072a
MT
124 run_subdir ${rc_base}/init.d/networking/red.up/
125
d1e90efc
MT
126 elif [ "${TYPE}" == "DHCP" ]; then
127 boot_mesg -n "Starting dhcpcd on the ${DEVICE} interface..."
128 echo -n "${DEVICE}" > /var/ipfire/red/iface
129
941e123e
AF
130 ## Create & Enable vnstat
131 /usr/bin/vnstat -u -i ${DEVICE} -r --enable --force > /dev/null 2>&1
132
d1e90efc
MT
133 # Test to see if there is a stale pid file
134 if [ -f "$PIDFILE" ]; then
135 ps `cat "$PIDFILE"` | grep dhcpcd > /dev/null
136 if [ $? != 0 ]; then
137 rm -f /var/run/dhcpcd-${DEVICE}.pid > /dev/null
138 else
139 boot_mesg "dhcpcd already running!" ${WARNING}
140 echo_warning
141 exit 2
142 fi
143 fi
144
145 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i ${DEVICE} -j ACCEPT
146 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i ${DEVICE} -j ACCEPT
147
7f5fdd04
MT
148 if [ -n "${RED_DHCP_HOSTNAME}" ]; then
149 DHCP_START+="-h ${RED_DHCP_HOSTNAME} "
d1e90efc
MT
150 fi
151
152 /sbin/dhcpcd ${DEVICE} ${DHCP_START} >/dev/null 2>&1
153 RET="$?"
154
155 if [ "$RET" = "0" ]; then
156 . /var/ipfire/dhcpc/dhcpcd-${DEVICE}.info
157 echo ""
158 echo_ok
159 boot_mesg " DHCP Assigned Settings for ${DEVICE}:"
160 boot_mesg_flush
161 boot_mesg " IP Address: $IPADDR"
162 boot_mesg_flush
7f5fdd04
MT
163 if [ -n "${RED_DHCP_HOSTNAME}" ]; then
164 boot_mesg " Hostname: $RED_DHCP_HOSTNAME"
d1e90efc
MT
165 boot_mesg_flush
166 fi
167 boot_mesg " Subnet Mask: $NETMASK"
168 boot_mesg_flush
169 boot_mesg " Default Gateway: $GATEWAY"
170 boot_mesg_flush
171 boot_mesg " DNS Server: $DNS"
172 boot_mesg_flush
173
174 echo -n `/etc/rc.d/helper/getdnsfromdhcpc.pl 1` > /var/ipfire/red/dns1
175 echo -n `/etc/rc.d/helper/getdnsfromdhcpc.pl 2` > /var/ipfire/red/dns2
176
177 . /var/ipfire/dhcpc/dhcpcd-${DEVICE}.info
178 echo "$IPADDR" > /var/ipfire/red/local-ipaddress
a332b303
CS
179 grep -v "gateway" /etc/hosts > /tmp/hosts
180 echo "$GATEWAY gateway" >> /tmp/hosts
181 mv /tmp/hosts /etc/hosts
d1e90efc
MT
182 echo "$GATEWAY" > /var/ipfire/red/remote-ipaddress
183 else
184 echo ""
185 $(exit "$RET")
186 evaluate_retval
187 fi
188
189 elif [ "$TYPE" == "PPPOE" ]; then
a89770fa
MT
190
191 if ( ps ax | grep -q [p]ppd ); then
0dde24fa
MT
192 boot_mesg "pppd is still running." ${FAILURE}
193 echo_failure
194 exit 1
a89770fa 195 fi
0dde24fa 196
d1e90efc
MT
197 eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
198
905fbf3e 199 [ -c "/dev/ppp" ] || mknod /dev/ppp c 108 0
d1e90efc 200
a89770fa 201 PPP_NIC=${DEVICE}
40049855
AF
202
203 if [ "$TYPE" == "vdsl" ]; then
204 boot_mesg "Createing VLAN Interface ${DEVICE}.7 ..."
205 modprobe 8021q
206 vconfig add ${DEVICE} 7
207 PPP_NIC=${DEVICE}.7
208 sleep 0.2
209 ip link set ${PPP_NIC} up
210 TYPE="pppoe"
211 fi
212
5aae218d 213 if [ "$TYPE" == "pppoeatm" ] || [ "$TYPE" == "pptpatm" ]; then
a89770fa 214 PPP_NIC=nas0
0dde24fa 215 boot_mesg "Createing ATM-Bridge as $PPP_NIC ..."
a89770fa
MT
216 br2684ctl -c0 -e${ENCAP} -a0.${VPI}.${VCI} >/dev/null 2>&1 &
217 sleep 1
5aae218d
AF
218 if [ "$TYPE" == "pppoeatm" ]; then
219 TYPE="pppoe"
220 fi
221 if [ "$TYPE" == "pptpatm" ]; then
222 TYPE="pptp"
223 fi
a89770fa 224 fi
5aae218d 225 if [ "$TYPE" == "pppoe" ] || [ "$TYPE" == "pptp" ]; then
4cd4876a 226 if [ "$PPP_NIC" == "" ]; then
0dde24fa
MT
227 boot_mesg "No device for red interface given. Check netsetup or dialprofile!" ${FAILURE}
228 echo_failure
cb1fb691 229 exit 0
0dde24fa 230 fi
5aae218d 231 boot_mesg "Bringing up the $TYPE interface on $PPP_NIC ..."
58e9b9dc 232 ip addr flush dev $PPP_NIC >/dev/null 2>&1
5aae218d
AF
233 if [ "$TYPE" == "pptp" ]; then
234 ip addr add $PPTP_NICCFG dev $PPP_NIC
235 fi
58e9b9dc 236 ip link set ${PPP_NIC} up
27b8cc24
MT
237 else
238 boot_mesg "Bringing up the PPP via ${TYPE} on ${COMPORT}..."
239 fi
0dde24fa 240
905fbf3e
MT
241 ### ###
242 ### Configuring the pppd ###
243 ### ###
244
245 ### Plugin Options
246 #
5aae218d
AF
247 if [ "$TYPE" == "pppoe" ]; then
248 [ "${METHOD}" == "PPPOE_PLUGIN" ] && \
249 PLUGOPTS="plugin /usr/lib/pppd/2.4.4/rp-pppoe.so"
250 fi
d1e90efc 251
905fbf3e
MT
252 ### Synchronous Mode
253 #
254 #PPPOE_SYNC=-s
a89770fa
MT
255 #PPPD_SYNC=sync
256
257 ### Access Concentrator Name
258 #
905fbf3e
MT
259 if [ -n "${CONCENTRATORNAME}" ]; then
260 ACNAME="-C ${CONCENTRATORNAME}"
d1e90efc 261 fi
905fbf3e
MT
262
263 ### Service Name
264 #
265 if [ -n "${SERVICENAME}" ]; then
266 if [ "${METHOD}" == "PPPOE_PLUGIN" ]; then
267 PLUGOPTS+=" rp_pppoe_service ${SERVICENAME}"
268 else
269 SERVICENAME="-S ${SERVICENAME}"
270 fi
d1e90efc 271 fi
a89770fa 272
905fbf3e
MT
273 ### Authentication Types
274 #
d1e90efc 275 if [ "${AUTH}" == "pap" ]; then
905fbf3e 276 AUTH="-chap"
d1e90efc 277 elif [ "${AUTH}" == "chap" ]; then
905fbf3e 278 AUTH="-pap"
57cb9775
CS
279 else
280 AUTH=""
d1e90efc 281 fi
a89770fa
MT
282
283 ### DNS Config
284 #
285 if [ "${DNS}" == "Automatic" ]; then
286 DNS="usepeerdns"
287 else
288 DNS=""
289 echo nameserver=$DNS1 > /etc/ppp/resolv.conf
290 echo nameserver=$DNS2 >> /etc/ppp/resolv.conf
a89770fa
MT
291 fi
292
905fbf3e
MT
293 ### Dial On Demand
294 #
d1e90efc
MT
295 if [ "${RECONNECTION}" != "persistent" ]; then
296 if [ "${TIMEOUT}" != "0" ] && [ "${TIMEOUT}" != "" ]; then
297 SECONDS=$[${TIMEOUT} * 60]
905fbf3e
MT
298 else
299 SECONDS=300
d1e90efc
MT
300 fi
301 if [ "${RECONNECTION}" == "dialondemand" ]; then
302 touch /var/ipfire/red/dial-on-demand
905fbf3e 303 DEMAND="demand persist idle ${SECONDS} 10.112.112.112:10.112.112.113"
a89770fa 304 DEMAND+=" ipcp-accept-remote ipcp-accept-local noipdefault ktune"
d1e90efc 305 fi
905fbf3e
MT
306 fi
307
5aae218d
AF
308 if [ "$TYPE" == "pppoe" ]; then
309 ### When using pppoe-plugin the device has to be the last option
310 #
311 [ "${METHOD}" == "PPPOE_PLUGIN" ] && PLUGOPTS+=" $PPP_NIC"
312 fi
27b8cc24
MT
313
314 if [ "$TYPE" == "modem" ]; then
a89770fa 315 PLUGOPTS=" /dev/${COMPORT} ${DTERATE} connect /etc/ppp/dialer lock modem crtscts"
7c653e4b 316 METHOD="PPPOE_PLUGIN"
27b8cc24 317 elif [ "$TYPE" == "serial" ]; then
a89770fa 318 PLUGOPTS=" /dev/${COMPORT} ${DTERATE} connect /bin/true lock modem crtscts"
7c653e4b 319 METHOD="PPPOE_PLUGIN"
27b8cc24 320 fi
905fbf3e
MT
321
322 ### Standard PPP options we always use
323 #
a89770fa 324 PPP_STD_OPTIONS="$PLUGOPTS $DNS defaultroute noipdefault noauth"
905fbf3e
MT
325 PPP_STD_OPTIONS+=" default-asyncmap hide-password nodetach mtu ${MTU}"
326 PPP_STD_OPTIONS+=" mru ${MTU} noaccomp nodeflate nopcomp novj novjccomp"
327 PPP_STD_OPTIONS+=" nobsdcomp user ${USERNAME} lcp-echo-interval 20"
328 PPP_STD_OPTIONS+=" lcp-echo-failure 3 ${AUTH}"
a89770fa 329
905fbf3e
MT
330 ### Debugging
331 #
332 if [ "${DEBUG}" == "on" ]; then
333 DEBUG="debug"
d1e90efc 334 else
905fbf3e 335 DEBUG=""
d1e90efc
MT
336 fi
337
905fbf3e
MT
338 ### PPPoE invocation
339 #
a89770fa
MT
340 if [ "$TYPE" == "pppoe" ]; then
341 PPPOE_CMD="/usr/sbin/pppoe -p /var/run/ppp-ipfire.pid.pppoe -I $PPP_NIC"
27b8cc24
MT
342 PPPOE_CMD+=" -T 80 -U $PPPOE_SYNC $ACNAME $SERVICENAMEOPT"
343 fi
5aae218d
AF
344
345 ### PPTP ###
346 #
347 if [ "$TYPE" == "pptp" ]; then
348 PPPOE_CMD="pptp $PPTP_PEER --nolaunchpppd"
7c653e4b 349 METHOD=""
5aae218d 350 fi
d1e90efc 351
905fbf3e
MT
352 ### Run everything
353 #
7c653e4b 354 if [ "$METHOD" == "PPPOE_PLUGIN" ]; then
905fbf3e
MT
355 /usr/sbin/pppd $PPP_STD_OPTIONS $DEBUG $DEMAND >/dev/null 2>&1 &
356 evaluate_retval
a89770fa 357 # echo PLUGIN: /usr/sbin/pppd $PPP_STD_OPTIONS $DEBUG $DEMAND
905fbf3e
MT
358 else
359 /usr/sbin/pppd pty "$PPPOE_CMD" $PPP_STD_OPTIONS $DEBUG $DEMAND $PPPD_SYNC >/dev/null 2>&1 &
360 evaluate_retval
a89770fa 361 # echo PPP: /usr/sbin/pppd pty "$PPPOE_CMD" $PPP_STD_OPTIONS $DEBUG $DEMAND $PPPD_SYNC
905fbf3e 362 fi
1e4656cd
AF
363 ## Create & Enable vnstat
364 /usr/bin/vnstat -u -i ppp0 -r --enable --force > /dev/null 2>&1
905fbf3e 365 /etc/rc.d/init.d/connectd start
1f2ff0fa 366 # Add a NaN value to ppp0 rrd to supress spikes at reconnect
c772568a 367 rrdtool update $RRDLOG/collectd/localhost/interface/if_octets-ppp0.rrd \
49ab1173 368 $(date +%s):: > /dev/null 2>&1
28ec28bc 369 exit 0
d1e90efc 370 fi
d1e90efc
MT
371 ;;
372
373 stop)
374 if [ "$TYPE" == "STATIC" ]; then
0dde24fa
MT
375 boot_mesg "Stopping default gateway ${GATEWAY}..."
376 ip route del default via ${GATEWAY} >/dev/null 2>&1
377 echo_ok
378 if [ "$DEVICE" != "${GREEN_DEV}" ]; then
70631572
AF
379 boot_mesg "Removing IPv4 addresses from the ${DEVICE} interface..."
380 ip addr flush dev ${DEVICE}
0dde24fa
MT
381 evaluate_retval
382 fi
0e42072a 383 run_subdir ${rc_base}/init.d/networking/red.down/
f8841352 384
d1e90efc
MT
385 elif [ "$TYPE" == "DHCP" ]; then
386 boot_mesg -n "Stopping dhcpcd on the ${DEVICE} interface..."
387 if [ -e $LEASEINFO ]; then
388 . $LEASEINFO
389 if [ "$LEASETIME" = "4294967295" ]; then
390 # do nothing, just echo ok
391 echo ""
392 echo_ok
393 else
394 if [ -n "$DHCP_STOP" ]; then
395 /sbin/dhcpcd ${DEVICE} $DHCP_STOP &> /dev/null
396 RET="$?"
397 if [ "$RET" -eq 0 ]; then
398 echo ""
399 echo_ok
400 elif [ "$RET" -eq 1 ]; then
401 boot_mesg "dhcpcd not running!" ${WARNING}
402 echo_warning
403 else
404 echo ""
405 echo_failure
406 fi
407 else
408 echo ""
409 killproc dhcpcd
410 fi
411 fi
412 else
413 boot_mesg -n "LEASEINFO Test failed! - " ${WARNING}
414 boot_mesg "dhcpcd is not running!" ${WARNING}
415 echo_warning
d1e90efc 416 fi
941e123e 417
5aae218d 418 elif [ "$TYPE" == "PPPOE" ]; then
a89770fa 419 boot_mesg "Bringing down the PPP interface ..."
1e4656cd
AF
420 ## Disable vnstat collection
421 /usr/bin/vnstat -u -i ppp0 -r --disable > /dev/null 2>&1
93b34528 422 rm -f /var/ipfire/red/keepconnected
a89770fa 423 killall -w -s TERM /usr/sbin/pppd 2>/dev/null
352e626f 424 evaluate_retval
1f2ff0fa 425 # Add a NaN value to ppp0 rrd to supress spikes at reconnect
c772568a 426 rrdtool update $RRDLOG/collectd/localhost/interface/if_octets-ppp0.rrd \
49ab1173 427 $(date +%s):: > /dev/null 2>&1
d1e90efc 428 fi
0dde24fa
MT
429
430 if [ "$DEVICE" != "${GREEN_DEV}" ] && [ "$DEVICE" != "" ]; then
40049855
AF
431 link_status=`ip link show $DEVICE.7 2> /dev/null`
432 if [ -n "${link_status}" ]; then
433 if echo "${link_status}" | grep -q UP; then
434 boot_mesg "Bringing down the ${DEVICE}.7 interface..."
435 ip link set ${DEVICE}.7 down
436 vconfig del ${DEVICE} 7
437 evaluate_retval
438 fi
439 fi
0dde24fa
MT
440 link_status=`ip link show $DEVICE 2> /dev/null`
441 if [ -n "${link_status}" ]; then
442 if echo "${link_status}" | grep -q UP; then
443 boot_mesg "Bringing down the ${DEVICE} interface..."
444 ip link set ${DEVICE} down
445 evaluate_retval
446 fi
d1e90efc
MT
447 fi
448 fi
cb1fb691 449 killall -w -s KILL /usr/sbin/pppd >/dev/null 2>&1
5aae218d 450 killall -w -s KILL pptp >/dev/null 2>&1
cb1fb691 451 killall -w -s KILL br2684ctl >/dev/null 2>&1
6c33dc5c 452
1e4656cd
AF
453 ## Disable vnstat collection
454 /usr/bin/vnstat -u -i ${DEVICE} -r --disable > /dev/null 2>&1
ed35052a
AF
455
456 rm -f /var/ipfire/red/{active,device,dial-on-demand,dns1,dns2,local-ipaddress,remote-ipaddress,resolv.conf}
6c33dc5c 457 exit 0;
d1e90efc 458 ;;
d1e90efc
MT
459esac
460
461# End