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