Stop dhcpcd before starting if it was running
[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 126 elif [ "${TYPE}" == "DHCP" ]; then
7e0cd11d
AF
127
128 if [ -e $LEASEINFO ]; then
129 boot_mesg -n "Stopping dhcpcd on the ${DEVICE} interface..."
130 . $LEASEINFO
131 if [ "$LEASETIME" = "4294967295" ]; then
132 # do nothing, just echo ok
133 echo ""
134 echo_ok
135 else
136 if [ -n "$DHCP_STOP" ]; then
137 /sbin/dhcpcd ${DEVICE} $DHCP_STOP &> /dev/null
138 RET="$?"
139 if [ "$RET" -eq 0 ]; then
140 echo ""
141 echo_ok
142 elif [ "$RET" -eq 1 ]; then
143 boot_mesg "dhcpcd not running!" ${WARNING}
144 echo_warning
145 else
146 echo ""
147 echo_failure
148 fi
149 else
150 echo ""
151 killproc dhcpcd
152 fi
153 fi
154 fi
155
d1e90efc
MT
156 boot_mesg -n "Starting dhcpcd on the ${DEVICE} interface..."
157 echo -n "${DEVICE}" > /var/ipfire/red/iface
158
941e123e
AF
159 ## Create & Enable vnstat
160 /usr/bin/vnstat -u -i ${DEVICE} -r --enable --force > /dev/null 2>&1
161
d1e90efc
MT
162 # Test to see if there is a stale pid file
163 if [ -f "$PIDFILE" ]; then
164 ps `cat "$PIDFILE"` | grep dhcpcd > /dev/null
165 if [ $? != 0 ]; then
166 rm -f /var/run/dhcpcd-${DEVICE}.pid > /dev/null
167 else
168 boot_mesg "dhcpcd already running!" ${WARNING}
169 echo_warning
170 exit 2
171 fi
172 fi
173
174 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i ${DEVICE} -j ACCEPT
175 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i ${DEVICE} -j ACCEPT
176
7f5fdd04
MT
177 if [ -n "${RED_DHCP_HOSTNAME}" ]; then
178 DHCP_START+="-h ${RED_DHCP_HOSTNAME} "
d1e90efc
MT
179 fi
180
181 /sbin/dhcpcd ${DEVICE} ${DHCP_START} >/dev/null 2>&1
182 RET="$?"
183
184 if [ "$RET" = "0" ]; then
185 . /var/ipfire/dhcpc/dhcpcd-${DEVICE}.info
186 echo ""
187 echo_ok
188 boot_mesg " DHCP Assigned Settings for ${DEVICE}:"
189 boot_mesg_flush
190 boot_mesg " IP Address: $IPADDR"
191 boot_mesg_flush
7f5fdd04
MT
192 if [ -n "${RED_DHCP_HOSTNAME}" ]; then
193 boot_mesg " Hostname: $RED_DHCP_HOSTNAME"
d1e90efc
MT
194 boot_mesg_flush
195 fi
196 boot_mesg " Subnet Mask: $NETMASK"
197 boot_mesg_flush
198 boot_mesg " Default Gateway: $GATEWAY"
199 boot_mesg_flush
200 boot_mesg " DNS Server: $DNS"
201 boot_mesg_flush
202
203 echo -n `/etc/rc.d/helper/getdnsfromdhcpc.pl 1` > /var/ipfire/red/dns1
204 echo -n `/etc/rc.d/helper/getdnsfromdhcpc.pl 2` > /var/ipfire/red/dns2
205
206 . /var/ipfire/dhcpc/dhcpcd-${DEVICE}.info
207 echo "$IPADDR" > /var/ipfire/red/local-ipaddress
a332b303
CS
208 grep -v "gateway" /etc/hosts > /tmp/hosts
209 echo "$GATEWAY gateway" >> /tmp/hosts
210 mv /tmp/hosts /etc/hosts
d1e90efc 211 echo "$GATEWAY" > /var/ipfire/red/remote-ipaddress
e288abb4 212 touch /var/ipfire/red/active
d1e90efc
MT
213 else
214 echo ""
215 $(exit "$RET")
216 evaluate_retval
217 fi
218
219 elif [ "$TYPE" == "PPPOE" ]; then
a89770fa
MT
220
221 if ( ps ax | grep -q [p]ppd ); then
0dde24fa
MT
222 boot_mesg "pppd is still running." ${FAILURE}
223 echo_failure
224 exit 1
a89770fa 225 fi
0dde24fa 226
d1e90efc
MT
227 eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
228
905fbf3e 229 [ -c "/dev/ppp" ] || mknod /dev/ppp c 108 0
d1e90efc 230
a89770fa 231 PPP_NIC=${DEVICE}
40049855
AF
232
233 if [ "$TYPE" == "vdsl" ]; then
234 boot_mesg "Createing VLAN Interface ${DEVICE}.7 ..."
235 modprobe 8021q
236 vconfig add ${DEVICE} 7
237 PPP_NIC=${DEVICE}.7
238 sleep 0.2
239 ip link set ${PPP_NIC} up
240 TYPE="pppoe"
241 fi
242
5aae218d 243 if [ "$TYPE" == "pppoeatm" ] || [ "$TYPE" == "pptpatm" ]; then
a89770fa 244 PPP_NIC=nas0
0dde24fa 245 boot_mesg "Createing ATM-Bridge as $PPP_NIC ..."
a89770fa
MT
246 br2684ctl -c0 -e${ENCAP} -a0.${VPI}.${VCI} >/dev/null 2>&1 &
247 sleep 1
5aae218d
AF
248 if [ "$TYPE" == "pppoeatm" ]; then
249 TYPE="pppoe"
250 fi
251 if [ "$TYPE" == "pptpatm" ]; then
252 TYPE="pptp"
253 fi
a89770fa 254 fi
5aae218d 255 if [ "$TYPE" == "pppoe" ] || [ "$TYPE" == "pptp" ]; then
4cd4876a 256 if [ "$PPP_NIC" == "" ]; then
0dde24fa
MT
257 boot_mesg "No device for red interface given. Check netsetup or dialprofile!" ${FAILURE}
258 echo_failure
cb1fb691 259 exit 0
0dde24fa 260 fi
5aae218d 261 boot_mesg "Bringing up the $TYPE interface on $PPP_NIC ..."
58e9b9dc 262 ip addr flush dev $PPP_NIC >/dev/null 2>&1
5aae218d
AF
263 if [ "$TYPE" == "pptp" ]; then
264 ip addr add $PPTP_NICCFG dev $PPP_NIC
265 fi
58e9b9dc 266 ip link set ${PPP_NIC} up
27b8cc24
MT
267 else
268 boot_mesg "Bringing up the PPP via ${TYPE} on ${COMPORT}..."
269 fi
0dde24fa 270
905fbf3e
MT
271 ### ###
272 ### Configuring the pppd ###
273 ### ###
274
275 ### Plugin Options
276 #
5aae218d
AF
277 if [ "$TYPE" == "pppoe" ]; then
278 [ "${METHOD}" == "PPPOE_PLUGIN" ] && \
279 PLUGOPTS="plugin /usr/lib/pppd/2.4.4/rp-pppoe.so"
280 fi
d1e90efc 281
905fbf3e
MT
282 ### Synchronous Mode
283 #
284 #PPPOE_SYNC=-s
a89770fa
MT
285 #PPPD_SYNC=sync
286
287 ### Access Concentrator Name
288 #
905fbf3e
MT
289 if [ -n "${CONCENTRATORNAME}" ]; then
290 ACNAME="-C ${CONCENTRATORNAME}"
d1e90efc 291 fi
905fbf3e
MT
292
293 ### Service Name
294 #
295 if [ -n "${SERVICENAME}" ]; then
296 if [ "${METHOD}" == "PPPOE_PLUGIN" ]; then
297 PLUGOPTS+=" rp_pppoe_service ${SERVICENAME}"
298 else
299 SERVICENAME="-S ${SERVICENAME}"
300 fi
d1e90efc 301 fi
a89770fa 302
905fbf3e
MT
303 ### Authentication Types
304 #
d1e90efc 305 if [ "${AUTH}" == "pap" ]; then
905fbf3e 306 AUTH="-chap"
d1e90efc 307 elif [ "${AUTH}" == "chap" ]; then
905fbf3e 308 AUTH="-pap"
57cb9775
CS
309 else
310 AUTH=""
d1e90efc 311 fi
a89770fa
MT
312
313 ### DNS Config
314 #
315 if [ "${DNS}" == "Automatic" ]; then
316 DNS="usepeerdns"
317 else
318 DNS=""
319 echo nameserver=$DNS1 > /etc/ppp/resolv.conf
320 echo nameserver=$DNS2 >> /etc/ppp/resolv.conf
a89770fa
MT
321 fi
322
905fbf3e
MT
323 ### Dial On Demand
324 #
d1e90efc
MT
325 if [ "${RECONNECTION}" != "persistent" ]; then
326 if [ "${TIMEOUT}" != "0" ] && [ "${TIMEOUT}" != "" ]; then
327 SECONDS=$[${TIMEOUT} * 60]
905fbf3e
MT
328 else
329 SECONDS=300
d1e90efc
MT
330 fi
331 if [ "${RECONNECTION}" == "dialondemand" ]; then
332 touch /var/ipfire/red/dial-on-demand
905fbf3e 333 DEMAND="demand persist idle ${SECONDS} 10.112.112.112:10.112.112.113"
a89770fa 334 DEMAND+=" ipcp-accept-remote ipcp-accept-local noipdefault ktune"
d1e90efc 335 fi
905fbf3e
MT
336 fi
337
5aae218d
AF
338 if [ "$TYPE" == "pppoe" ]; then
339 ### When using pppoe-plugin the device has to be the last option
340 #
341 [ "${METHOD}" == "PPPOE_PLUGIN" ] && PLUGOPTS+=" $PPP_NIC"
342 fi
27b8cc24
MT
343
344 if [ "$TYPE" == "modem" ]; then
a89770fa 345 PLUGOPTS=" /dev/${COMPORT} ${DTERATE} connect /etc/ppp/dialer lock modem crtscts"
7c653e4b 346 METHOD="PPPOE_PLUGIN"
27b8cc24 347 elif [ "$TYPE" == "serial" ]; then
a89770fa 348 PLUGOPTS=" /dev/${COMPORT} ${DTERATE} connect /bin/true lock modem crtscts"
7c653e4b 349 METHOD="PPPOE_PLUGIN"
27b8cc24 350 fi
905fbf3e
MT
351
352 ### Standard PPP options we always use
353 #
a89770fa 354 PPP_STD_OPTIONS="$PLUGOPTS $DNS defaultroute noipdefault noauth"
905fbf3e
MT
355 PPP_STD_OPTIONS+=" default-asyncmap hide-password nodetach mtu ${MTU}"
356 PPP_STD_OPTIONS+=" mru ${MTU} noaccomp nodeflate nopcomp novj novjccomp"
357 PPP_STD_OPTIONS+=" nobsdcomp user ${USERNAME} lcp-echo-interval 20"
358 PPP_STD_OPTIONS+=" lcp-echo-failure 3 ${AUTH}"
a89770fa 359
905fbf3e
MT
360 ### Debugging
361 #
362 if [ "${DEBUG}" == "on" ]; then
363 DEBUG="debug"
d1e90efc 364 else
905fbf3e 365 DEBUG=""
d1e90efc
MT
366 fi
367
905fbf3e
MT
368 ### PPPoE invocation
369 #
a89770fa
MT
370 if [ "$TYPE" == "pppoe" ]; then
371 PPPOE_CMD="/usr/sbin/pppoe -p /var/run/ppp-ipfire.pid.pppoe -I $PPP_NIC"
27b8cc24
MT
372 PPPOE_CMD+=" -T 80 -U $PPPOE_SYNC $ACNAME $SERVICENAMEOPT"
373 fi
5aae218d
AF
374
375 ### PPTP ###
376 #
377 if [ "$TYPE" == "pptp" ]; then
378 PPPOE_CMD="pptp $PPTP_PEER --nolaunchpppd"
7c653e4b 379 METHOD=""
5aae218d 380 fi
d1e90efc 381
905fbf3e
MT
382 ### Run everything
383 #
7c653e4b 384 if [ "$METHOD" == "PPPOE_PLUGIN" ]; then
905fbf3e
MT
385 /usr/sbin/pppd $PPP_STD_OPTIONS $DEBUG $DEMAND >/dev/null 2>&1 &
386 evaluate_retval
a89770fa 387 # echo PLUGIN: /usr/sbin/pppd $PPP_STD_OPTIONS $DEBUG $DEMAND
905fbf3e
MT
388 else
389 /usr/sbin/pppd pty "$PPPOE_CMD" $PPP_STD_OPTIONS $DEBUG $DEMAND $PPPD_SYNC >/dev/null 2>&1 &
390 evaluate_retval
a89770fa 391 # echo PPP: /usr/sbin/pppd pty "$PPPOE_CMD" $PPP_STD_OPTIONS $DEBUG $DEMAND $PPPD_SYNC
905fbf3e 392 fi
1e4656cd
AF
393 ## Create & Enable vnstat
394 /usr/bin/vnstat -u -i ppp0 -r --enable --force > /dev/null 2>&1
905fbf3e 395 /etc/rc.d/init.d/connectd start
1f2ff0fa 396 # Add a NaN value to ppp0 rrd to supress spikes at reconnect
c772568a 397 rrdtool update $RRDLOG/collectd/localhost/interface/if_octets-ppp0.rrd \
49ab1173 398 $(date +%s):: > /dev/null 2>&1
28ec28bc 399 exit 0
d1e90efc 400 fi
d1e90efc
MT
401 ;;
402
403 stop)
404 if [ "$TYPE" == "STATIC" ]; then
0dde24fa
MT
405 boot_mesg "Stopping default gateway ${GATEWAY}..."
406 ip route del default via ${GATEWAY} >/dev/null 2>&1
407 echo_ok
408 if [ "$DEVICE" != "${GREEN_DEV}" ]; then
70631572
AF
409 boot_mesg "Removing IPv4 addresses from the ${DEVICE} interface..."
410 ip addr flush dev ${DEVICE}
0dde24fa
MT
411 evaluate_retval
412 fi
0e42072a 413 run_subdir ${rc_base}/init.d/networking/red.down/
f8841352 414
d1e90efc
MT
415 elif [ "$TYPE" == "DHCP" ]; then
416 boot_mesg -n "Stopping dhcpcd on the ${DEVICE} interface..."
417 if [ -e $LEASEINFO ]; then
418 . $LEASEINFO
419 if [ "$LEASETIME" = "4294967295" ]; then
420 # do nothing, just echo ok
421 echo ""
422 echo_ok
423 else
424 if [ -n "$DHCP_STOP" ]; then
425 /sbin/dhcpcd ${DEVICE} $DHCP_STOP &> /dev/null
426 RET="$?"
427 if [ "$RET" -eq 0 ]; then
428 echo ""
429 echo_ok
430 elif [ "$RET" -eq 1 ]; then
431 boot_mesg "dhcpcd not running!" ${WARNING}
432 echo_warning
433 else
434 echo ""
435 echo_failure
436 fi
437 else
438 echo ""
439 killproc dhcpcd
440 fi
441 fi
442 else
443 boot_mesg -n "LEASEINFO Test failed! - " ${WARNING}
444 boot_mesg "dhcpcd is not running!" ${WARNING}
445 echo_warning
d1e90efc 446 fi
941e123e 447
5aae218d 448 elif [ "$TYPE" == "PPPOE" ]; then
a89770fa 449 boot_mesg "Bringing down the PPP interface ..."
1e4656cd
AF
450 ## Disable vnstat collection
451 /usr/bin/vnstat -u -i ppp0 -r --disable > /dev/null 2>&1
93b34528 452 rm -f /var/ipfire/red/keepconnected
a89770fa 453 killall -w -s TERM /usr/sbin/pppd 2>/dev/null
352e626f 454 evaluate_retval
1f2ff0fa 455 # Add a NaN value to ppp0 rrd to supress spikes at reconnect
c772568a 456 rrdtool update $RRDLOG/collectd/localhost/interface/if_octets-ppp0.rrd \
49ab1173 457 $(date +%s):: > /dev/null 2>&1
d1e90efc 458 fi
0dde24fa
MT
459
460 if [ "$DEVICE" != "${GREEN_DEV}" ] && [ "$DEVICE" != "" ]; then
40049855
AF
461 link_status=`ip link show $DEVICE.7 2> /dev/null`
462 if [ -n "${link_status}" ]; then
463 if echo "${link_status}" | grep -q UP; then
464 boot_mesg "Bringing down the ${DEVICE}.7 interface..."
465 ip link set ${DEVICE}.7 down
466 vconfig del ${DEVICE} 7
467 evaluate_retval
468 fi
469 fi
0dde24fa
MT
470 link_status=`ip link show $DEVICE 2> /dev/null`
471 if [ -n "${link_status}" ]; then
472 if echo "${link_status}" | grep -q UP; then
473 boot_mesg "Bringing down the ${DEVICE} interface..."
474 ip link set ${DEVICE} down
475 evaluate_retval
476 fi
d1e90efc
MT
477 fi
478 fi
cb1fb691 479 killall -w -s KILL /usr/sbin/pppd >/dev/null 2>&1
5aae218d 480 killall -w -s KILL pptp >/dev/null 2>&1
cb1fb691 481 killall -w -s KILL br2684ctl >/dev/null 2>&1
6c33dc5c 482
1e4656cd
AF
483 ## Disable vnstat collection
484 /usr/bin/vnstat -u -i ${DEVICE} -r --disable > /dev/null 2>&1
ed35052a
AF
485
486 rm -f /var/ipfire/red/{active,device,dial-on-demand,dns1,dns2,local-ipaddress,remote-ipaddress,resolv.conf}
6c33dc5c 487 exit 0;
d1e90efc 488 ;;
d1e90efc
MT
489esac
490
491# End