Stop dhcpcd before starting if it was running
[people/pmueller/ipfire-2.x.git] / src / initscripts / init.d / networking / red
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
18
19
20 . /etc/sysconfig/rc
21 . ${rc_functions}
22
23 eval $(/usr/local/bin/readhash /var/ipfire/main/settings)
24 if [ "$RRDLOG" == "" ]; then
25 RRDLOG=/var/log/rrd
26 fi
27
28 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
29
30 TYPE="${RED_TYPE}"
31 DEVICE="${RED_DEV}"
32
33 if [ "$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
39 fi
40
41 if [ "${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
53 GATEWAY="${DEFAULT_GATEWAY}"
54 # DNS1
55 # DNS2
56
57 if [ -z "${BROADCAST}" ]; then
58 boot_mesg "BROADCAST variable missing, cannot continue." ${FAILURE}
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
71 elif [ "${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
78 fi
79
80 case "${1}" in
81 start)
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
92 fi
93 else
94 boot_mesg "Interface ${DEVICE} doesn't exist." ${FAILURE}
95 echo_failure
96 exit 1
97 fi
98 fi
99
100 ## Create & Enable vnstat
101 /usr/bin/vnstat -u -i ${DEVICE} -r --enable --force > /dev/null 2>&1
102
103 if [ "${TYPE}" == "STATIC" ]; then
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
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
113 grep -v "gateway" /etc/hosts > /tmp/hosts
114 echo "$GATEWAY gateway" >> /tmp/hosts
115 mv /tmp/hosts /etc/hosts
116 echo -n "${DNS1}" > /var/ipfire/red/dns1
117 echo -n "${DNS2}" > /var/ipfire/red/dns2
118 touch /var/ipfire/red/active
119
120 boot_mesg "Setting up default gateway ${GATEWAY}..."
121 ip route add default via ${GATEWAY} dev ${DEVICE}
122 evaluate_retval
123
124 run_subdir ${rc_base}/init.d/networking/red.up/
125
126 elif [ "${TYPE}" == "DHCP" ]; then
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
156 boot_mesg -n "Starting dhcpcd on the ${DEVICE} interface..."
157 echo -n "${DEVICE}" > /var/ipfire/red/iface
158
159 ## Create & Enable vnstat
160 /usr/bin/vnstat -u -i ${DEVICE} -r --enable --force > /dev/null 2>&1
161
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
177 if [ -n "${RED_DHCP_HOSTNAME}" ]; then
178 DHCP_START+="-h ${RED_DHCP_HOSTNAME} "
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
192 if [ -n "${RED_DHCP_HOSTNAME}" ]; then
193 boot_mesg " Hostname: $RED_DHCP_HOSTNAME"
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
208 grep -v "gateway" /etc/hosts > /tmp/hosts
209 echo "$GATEWAY gateway" >> /tmp/hosts
210 mv /tmp/hosts /etc/hosts
211 echo "$GATEWAY" > /var/ipfire/red/remote-ipaddress
212 touch /var/ipfire/red/active
213 else
214 echo ""
215 $(exit "$RET")
216 evaluate_retval
217 fi
218
219 elif [ "$TYPE" == "PPPOE" ]; then
220
221 if ( ps ax | grep -q [p]ppd ); then
222 boot_mesg "pppd is still running." ${FAILURE}
223 echo_failure
224 exit 1
225 fi
226
227 eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
228
229 [ -c "/dev/ppp" ] || mknod /dev/ppp c 108 0
230
231 PPP_NIC=${DEVICE}
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
243 if [ "$TYPE" == "pppoeatm" ] || [ "$TYPE" == "pptpatm" ]; then
244 PPP_NIC=nas0
245 boot_mesg "Createing ATM-Bridge as $PPP_NIC ..."
246 br2684ctl -c0 -e${ENCAP} -a0.${VPI}.${VCI} >/dev/null 2>&1 &
247 sleep 1
248 if [ "$TYPE" == "pppoeatm" ]; then
249 TYPE="pppoe"
250 fi
251 if [ "$TYPE" == "pptpatm" ]; then
252 TYPE="pptp"
253 fi
254 fi
255 if [ "$TYPE" == "pppoe" ] || [ "$TYPE" == "pptp" ]; then
256 if [ "$PPP_NIC" == "" ]; then
257 boot_mesg "No device for red interface given. Check netsetup or dialprofile!" ${FAILURE}
258 echo_failure
259 exit 0
260 fi
261 boot_mesg "Bringing up the $TYPE interface on $PPP_NIC ..."
262 ip addr flush dev $PPP_NIC >/dev/null 2>&1
263 if [ "$TYPE" == "pptp" ]; then
264 ip addr add $PPTP_NICCFG dev $PPP_NIC
265 fi
266 ip link set ${PPP_NIC} up
267 else
268 boot_mesg "Bringing up the PPP via ${TYPE} on ${COMPORT}..."
269 fi
270
271 ### ###
272 ### Configuring the pppd ###
273 ### ###
274
275 ### Plugin Options
276 #
277 if [ "$TYPE" == "pppoe" ]; then
278 [ "${METHOD}" == "PPPOE_PLUGIN" ] && \
279 PLUGOPTS="plugin /usr/lib/pppd/2.4.4/rp-pppoe.so"
280 fi
281
282 ### Synchronous Mode
283 #
284 #PPPOE_SYNC=-s
285 #PPPD_SYNC=sync
286
287 ### Access Concentrator Name
288 #
289 if [ -n "${CONCENTRATORNAME}" ]; then
290 ACNAME="-C ${CONCENTRATORNAME}"
291 fi
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
301 fi
302
303 ### Authentication Types
304 #
305 if [ "${AUTH}" == "pap" ]; then
306 AUTH="-chap"
307 elif [ "${AUTH}" == "chap" ]; then
308 AUTH="-pap"
309 else
310 AUTH=""
311 fi
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
321 fi
322
323 ### Dial On Demand
324 #
325 if [ "${RECONNECTION}" != "persistent" ]; then
326 if [ "${TIMEOUT}" != "0" ] && [ "${TIMEOUT}" != "" ]; then
327 SECONDS=$[${TIMEOUT} * 60]
328 else
329 SECONDS=300
330 fi
331 if [ "${RECONNECTION}" == "dialondemand" ]; then
332 touch /var/ipfire/red/dial-on-demand
333 DEMAND="demand persist idle ${SECONDS} 10.112.112.112:10.112.112.113"
334 DEMAND+=" ipcp-accept-remote ipcp-accept-local noipdefault ktune"
335 fi
336 fi
337
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
343
344 if [ "$TYPE" == "modem" ]; then
345 PLUGOPTS=" /dev/${COMPORT} ${DTERATE} connect /etc/ppp/dialer lock modem crtscts"
346 METHOD="PPPOE_PLUGIN"
347 elif [ "$TYPE" == "serial" ]; then
348 PLUGOPTS=" /dev/${COMPORT} ${DTERATE} connect /bin/true lock modem crtscts"
349 METHOD="PPPOE_PLUGIN"
350 fi
351
352 ### Standard PPP options we always use
353 #
354 PPP_STD_OPTIONS="$PLUGOPTS $DNS defaultroute noipdefault noauth"
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}"
359
360 ### Debugging
361 #
362 if [ "${DEBUG}" == "on" ]; then
363 DEBUG="debug"
364 else
365 DEBUG=""
366 fi
367
368 ### PPPoE invocation
369 #
370 if [ "$TYPE" == "pppoe" ]; then
371 PPPOE_CMD="/usr/sbin/pppoe -p /var/run/ppp-ipfire.pid.pppoe -I $PPP_NIC"
372 PPPOE_CMD+=" -T 80 -U $PPPOE_SYNC $ACNAME $SERVICENAMEOPT"
373 fi
374
375 ### PPTP ###
376 #
377 if [ "$TYPE" == "pptp" ]; then
378 PPPOE_CMD="pptp $PPTP_PEER --nolaunchpppd"
379 METHOD=""
380 fi
381
382 ### Run everything
383 #
384 if [ "$METHOD" == "PPPOE_PLUGIN" ]; then
385 /usr/sbin/pppd $PPP_STD_OPTIONS $DEBUG $DEMAND >/dev/null 2>&1 &
386 evaluate_retval
387 # echo PLUGIN: /usr/sbin/pppd $PPP_STD_OPTIONS $DEBUG $DEMAND
388 else
389 /usr/sbin/pppd pty "$PPPOE_CMD" $PPP_STD_OPTIONS $DEBUG $DEMAND $PPPD_SYNC >/dev/null 2>&1 &
390 evaluate_retval
391 # echo PPP: /usr/sbin/pppd pty "$PPPOE_CMD" $PPP_STD_OPTIONS $DEBUG $DEMAND $PPPD_SYNC
392 fi
393 ## Create & Enable vnstat
394 /usr/bin/vnstat -u -i ppp0 -r --enable --force > /dev/null 2>&1
395 /etc/rc.d/init.d/connectd start
396 # Add a NaN value to ppp0 rrd to supress spikes at reconnect
397 rrdtool update $RRDLOG/collectd/localhost/interface/if_octets-ppp0.rrd \
398 $(date +%s):: > /dev/null 2>&1
399 exit 0
400 fi
401 ;;
402
403 stop)
404 if [ "$TYPE" == "STATIC" ]; then
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
409 boot_mesg "Removing IPv4 addresses from the ${DEVICE} interface..."
410 ip addr flush dev ${DEVICE}
411 evaluate_retval
412 fi
413 run_subdir ${rc_base}/init.d/networking/red.down/
414
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
446 fi
447
448 elif [ "$TYPE" == "PPPOE" ]; then
449 boot_mesg "Bringing down the PPP interface ..."
450 ## Disable vnstat collection
451 /usr/bin/vnstat -u -i ppp0 -r --disable > /dev/null 2>&1
452 rm -f /var/ipfire/red/keepconnected
453 killall -w -s TERM /usr/sbin/pppd 2>/dev/null
454 evaluate_retval
455 # Add a NaN value to ppp0 rrd to supress spikes at reconnect
456 rrdtool update $RRDLOG/collectd/localhost/interface/if_octets-ppp0.rrd \
457 $(date +%s):: > /dev/null 2>&1
458 fi
459
460 if [ "$DEVICE" != "${GREEN_DEV}" ] && [ "$DEVICE" != "" ]; then
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
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
477 fi
478 fi
479 killall -w -s KILL /usr/sbin/pppd >/dev/null 2>&1
480 killall -w -s KILL pptp >/dev/null 2>&1
481 killall -w -s KILL br2684ctl >/dev/null 2>&1
482
483 ## Disable vnstat collection
484 /usr/bin/vnstat -u -i ${DEVICE} -r --disable > /dev/null 2>&1
485
486 rm -f /var/ipfire/red/{active,device,dial-on-demand,dns1,dns2,local-ipaddress,remote-ipaddress,resolv.conf}
487 exit 0;
488 ;;
489 esac
490
491 # End