]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/initscripts/init.d/networking/red
Updated git rootfile.
[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}
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)
f950f258
AF
76 # Stop collectd if running
77 if [ "$(ps -A | grep " collectd$")" != "" ]; then
78 /etc/rc.d/init.d/collectd stop
79 fi
0dde24fa
MT
80 if [ "${DEVICE}" != "${GREEN_DEV}" ] && [ "${DEVICE}" != "" ]; then
81 boot_mesg "Bringing up the ${DEVICE} interface..."
82 boot_mesg_flush
83 # Check if an interface is there...
84 if ip link show ${DEVICE} > /dev/null 2>&1; then
85 link_status=`ip link show ${DEVICE} 2> /dev/null`
86 if [ -n "${link_status}" ]; then
87 if ! echo "${link_status}" | grep -q UP; then
88 ip link set ${DEVICE} up
89 fi
d1e90efc 90 fi
0dde24fa
MT
91 else
92 boot_mesg "Interface ${DEVICE} doesn't exist." ${FAILURE}
93 echo_failure
f950f258 94 /etc/rc.d/init.d/collectd start
0dde24fa 95 exit 1
d1e90efc 96 fi
d1e90efc 97 fi
6c33dc5c 98
1e4656cd
AF
99 ## Create & Enable vnstat
100 /usr/bin/vnstat -u -i ${DEVICE} -r --enable --force > /dev/null 2>&1
d1e90efc
MT
101
102 if [ "${TYPE}" == "STATIC" ]; then
0dde24fa
MT
103
104 if [ "$DEVICE" != "${GREEN_DEV}" ]; then
105 boot_mesg "Adding IPv4 address ${ADDRESS} to the ${DEVICE} interface..."
106 ip addr add ${args} dev ${DEVICE}
107 evaluate_retval
108 fi
0db33b56
MT
109 echo -n "${DEVICE}" > /var/ipfire/red/iface
110 echo -n "${ADDRESS}" > /var/ipfire/red/local-ipaddress
111 echo -n "${GATEWAY}" > /var/ipfire/red/remote-ipaddress
b2f872eb
AF
112 grep -v "gateway" /etc/hosts > /tmp/hosts
113 echo "$GATEWAY gateway" >> /tmp/hosts
114 mv /tmp/hosts /etc/hosts
0db33b56
MT
115 echo -n "${DNS1}" > /var/ipfire/red/dns1
116 echo -n "${DNS2}" > /var/ipfire/red/dns2
bcdde652 117 touch /var/ipfire/red/active
d1e90efc 118
040e5040
MT
119 boot_mesg "Setting up default gateway ${GATEWAY}..."
120 ip route add default via ${GATEWAY} dev ${DEVICE}
121 evaluate_retval
122
0e42072a
MT
123 run_subdir ${rc_base}/init.d/networking/red.up/
124
d1e90efc
MT
125 elif [ "${TYPE}" == "DHCP" ]; then
126 boot_mesg -n "Starting dhcpcd on the ${DEVICE} interface..."
127 echo -n "${DEVICE}" > /var/ipfire/red/iface
128
941e123e
AF
129 ## Create & Enable vnstat
130 /usr/bin/vnstat -u -i ${DEVICE} -r --enable --force > /dev/null 2>&1
131
d1e90efc
MT
132 # Test to see if there is a stale pid file
133 if [ -f "$PIDFILE" ]; then
134 ps `cat "$PIDFILE"` | grep dhcpcd > /dev/null
135 if [ $? != 0 ]; then
136 rm -f /var/run/dhcpcd-${DEVICE}.pid > /dev/null
137 else
138 boot_mesg "dhcpcd already running!" ${WARNING}
139 echo_warning
f950f258 140 /etc/rc.d/init.d/collectd start
d1e90efc
MT
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
f950f258 194 /etc/init.d/collectd start
0dde24fa 195 exit 1
a89770fa 196 fi
0dde24fa 197
d1e90efc
MT
198 eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
199
905fbf3e 200 [ -c "/dev/ppp" ] || mknod /dev/ppp c 108 0
d1e90efc 201
a89770fa 202 PPP_NIC=${DEVICE}
40049855
AF
203
204 if [ "$TYPE" == "vdsl" ]; then
205 boot_mesg "Createing VLAN Interface ${DEVICE}.7 ..."
206 modprobe 8021q
207 vconfig add ${DEVICE} 7
208 PPP_NIC=${DEVICE}.7
209 sleep 0.2
210 ip link set ${PPP_NIC} up
211 TYPE="pppoe"
212 fi
213
5aae218d 214 if [ "$TYPE" == "pppoeatm" ] || [ "$TYPE" == "pptpatm" ]; then
a89770fa 215 PPP_NIC=nas0
0dde24fa 216 boot_mesg "Createing ATM-Bridge as $PPP_NIC ..."
a89770fa
MT
217 br2684ctl -c0 -e${ENCAP} -a0.${VPI}.${VCI} >/dev/null 2>&1 &
218 sleep 1
5aae218d
AF
219 if [ "$TYPE" == "pppoeatm" ]; then
220 TYPE="pppoe"
221 fi
222 if [ "$TYPE" == "pptpatm" ]; then
223 TYPE="pptp"
224 fi
a89770fa 225 fi
5aae218d 226 if [ "$TYPE" == "pppoe" ] || [ "$TYPE" == "pptp" ]; then
4cd4876a 227 if [ "$PPP_NIC" == "" ]; then
0dde24fa
MT
228 boot_mesg "No device for red interface given. Check netsetup or dialprofile!" ${FAILURE}
229 echo_failure
f950f258 230 /etc/rc.d/init.d/collectd start
cb1fb691 231 exit 0
0dde24fa 232 fi
5aae218d 233 boot_mesg "Bringing up the $TYPE interface on $PPP_NIC ..."
58e9b9dc 234 ip addr flush dev $PPP_NIC >/dev/null 2>&1
5aae218d
AF
235 if [ "$TYPE" == "pptp" ]; then
236 ip addr add $PPTP_NICCFG dev $PPP_NIC
237 fi
58e9b9dc 238 ip link set ${PPP_NIC} up
27b8cc24
MT
239 else
240 boot_mesg "Bringing up the PPP via ${TYPE} on ${COMPORT}..."
241 fi
0dde24fa 242
905fbf3e
MT
243 ### ###
244 ### Configuring the pppd ###
245 ### ###
246
247 ### Plugin Options
248 #
5aae218d
AF
249 if [ "$TYPE" == "pppoe" ]; then
250 [ "${METHOD}" == "PPPOE_PLUGIN" ] && \
251 PLUGOPTS="plugin /usr/lib/pppd/2.4.4/rp-pppoe.so"
252 fi
d1e90efc 253
905fbf3e
MT
254 ### Synchronous Mode
255 #
256 #PPPOE_SYNC=-s
a89770fa
MT
257 #PPPD_SYNC=sync
258
259 ### Access Concentrator Name
260 #
905fbf3e
MT
261 if [ -n "${CONCENTRATORNAME}" ]; then
262 ACNAME="-C ${CONCENTRATORNAME}"
d1e90efc 263 fi
905fbf3e
MT
264
265 ### Service Name
266 #
267 if [ -n "${SERVICENAME}" ]; then
268 if [ "${METHOD}" == "PPPOE_PLUGIN" ]; then
269 PLUGOPTS+=" rp_pppoe_service ${SERVICENAME}"
270 else
271 SERVICENAME="-S ${SERVICENAME}"
272 fi
d1e90efc 273 fi
a89770fa 274
905fbf3e
MT
275 ### Authentication Types
276 #
d1e90efc 277 if [ "${AUTH}" == "pap" ]; then
905fbf3e 278 AUTH="-chap"
d1e90efc 279 elif [ "${AUTH}" == "chap" ]; then
905fbf3e 280 AUTH="-pap"
57cb9775
CS
281 else
282 AUTH=""
d1e90efc 283 fi
a89770fa
MT
284
285 ### DNS Config
286 #
287 if [ "${DNS}" == "Automatic" ]; then
288 DNS="usepeerdns"
289 else
290 DNS=""
291 echo nameserver=$DNS1 > /etc/ppp/resolv.conf
292 echo nameserver=$DNS2 >> /etc/ppp/resolv.conf
a89770fa
MT
293 fi
294
905fbf3e
MT
295 ### Dial On Demand
296 #
d1e90efc
MT
297 if [ "${RECONNECTION}" != "persistent" ]; then
298 if [ "${TIMEOUT}" != "0" ] && [ "${TIMEOUT}" != "" ]; then
299 SECONDS=$[${TIMEOUT} * 60]
905fbf3e
MT
300 else
301 SECONDS=300
d1e90efc
MT
302 fi
303 if [ "${RECONNECTION}" == "dialondemand" ]; then
304 touch /var/ipfire/red/dial-on-demand
905fbf3e 305 DEMAND="demand persist idle ${SECONDS} 10.112.112.112:10.112.112.113"
a89770fa 306 DEMAND+=" ipcp-accept-remote ipcp-accept-local noipdefault ktune"
d1e90efc 307 fi
905fbf3e
MT
308 fi
309
5aae218d
AF
310 if [ "$TYPE" == "pppoe" ]; then
311 ### When using pppoe-plugin the device has to be the last option
312 #
313 [ "${METHOD}" == "PPPOE_PLUGIN" ] && PLUGOPTS+=" $PPP_NIC"
314 fi
27b8cc24
MT
315
316 if [ "$TYPE" == "modem" ]; then
a89770fa 317 PLUGOPTS=" /dev/${COMPORT} ${DTERATE} connect /etc/ppp/dialer lock modem crtscts"
7c653e4b 318 METHOD="PPPOE_PLUGIN"
27b8cc24 319 elif [ "$TYPE" == "serial" ]; then
a89770fa 320 PLUGOPTS=" /dev/${COMPORT} ${DTERATE} connect /bin/true lock modem crtscts"
7c653e4b 321 METHOD="PPPOE_PLUGIN"
27b8cc24 322 fi
905fbf3e
MT
323
324 ### Standard PPP options we always use
325 #
a89770fa 326 PPP_STD_OPTIONS="$PLUGOPTS $DNS defaultroute noipdefault noauth"
905fbf3e
MT
327 PPP_STD_OPTIONS+=" default-asyncmap hide-password nodetach mtu ${MTU}"
328 PPP_STD_OPTIONS+=" mru ${MTU} noaccomp nodeflate nopcomp novj novjccomp"
329 PPP_STD_OPTIONS+=" nobsdcomp user ${USERNAME} lcp-echo-interval 20"
330 PPP_STD_OPTIONS+=" lcp-echo-failure 3 ${AUTH}"
a89770fa 331
905fbf3e
MT
332 ### Debugging
333 #
334 if [ "${DEBUG}" == "on" ]; then
335 DEBUG="debug"
d1e90efc 336 else
905fbf3e 337 DEBUG=""
d1e90efc
MT
338 fi
339
905fbf3e
MT
340 ### PPPoE invocation
341 #
a89770fa
MT
342 if [ "$TYPE" == "pppoe" ]; then
343 PPPOE_CMD="/usr/sbin/pppoe -p /var/run/ppp-ipfire.pid.pppoe -I $PPP_NIC"
27b8cc24
MT
344 PPPOE_CMD+=" -T 80 -U $PPPOE_SYNC $ACNAME $SERVICENAMEOPT"
345 fi
5aae218d
AF
346
347 ### PPTP ###
348 #
349 if [ "$TYPE" == "pptp" ]; then
350 PPPOE_CMD="pptp $PPTP_PEER --nolaunchpppd"
7c653e4b 351 METHOD=""
5aae218d 352 fi
d1e90efc 353
905fbf3e
MT
354 ### Run everything
355 #
7c653e4b 356 if [ "$METHOD" == "PPPOE_PLUGIN" ]; then
905fbf3e
MT
357 /usr/sbin/pppd $PPP_STD_OPTIONS $DEBUG $DEMAND >/dev/null 2>&1 &
358 evaluate_retval
a89770fa 359 # echo PLUGIN: /usr/sbin/pppd $PPP_STD_OPTIONS $DEBUG $DEMAND
905fbf3e
MT
360 else
361 /usr/sbin/pppd pty "$PPPOE_CMD" $PPP_STD_OPTIONS $DEBUG $DEMAND $PPPD_SYNC >/dev/null 2>&1 &
362 evaluate_retval
a89770fa 363 # echo PPP: /usr/sbin/pppd pty "$PPPOE_CMD" $PPP_STD_OPTIONS $DEBUG $DEMAND $PPPD_SYNC
905fbf3e 364 fi
1e4656cd
AF
365 ## Create & Enable vnstat
366 /usr/bin/vnstat -u -i ppp0 -r --enable --force > /dev/null 2>&1
905fbf3e 367 /etc/rc.d/init.d/connectd start
f950f258 368 /etc/rc.d/init.d/collectd start
d1e90efc 369 fi
d1e90efc
MT
370 ;;
371
372 stop)
373 if [ "$TYPE" == "STATIC" ]; then
0dde24fa
MT
374 boot_mesg "Stopping default gateway ${GATEWAY}..."
375 ip route del default via ${GATEWAY} >/dev/null 2>&1
376 echo_ok
377 if [ "$DEVICE" != "${GREEN_DEV}" ]; then
70631572
AF
378 boot_mesg "Removing IPv4 addresses from the ${DEVICE} interface..."
379 ip addr flush dev ${DEVICE}
0dde24fa
MT
380 evaluate_retval
381 fi
0e42072a 382 run_subdir ${rc_base}/init.d/networking/red.down/
f8841352 383
d1e90efc
MT
384 elif [ "$TYPE" == "DHCP" ]; then
385 boot_mesg -n "Stopping dhcpcd on the ${DEVICE} interface..."
386 if [ -e $LEASEINFO ]; then
387 . $LEASEINFO
388 if [ "$LEASETIME" = "4294967295" ]; then
389 # do nothing, just echo ok
390 echo ""
391 echo_ok
392 else
393 if [ -n "$DHCP_STOP" ]; then
394 /sbin/dhcpcd ${DEVICE} $DHCP_STOP &> /dev/null
395 RET="$?"
396 if [ "$RET" -eq 0 ]; then
397 echo ""
398 echo_ok
399 elif [ "$RET" -eq 1 ]; then
400 boot_mesg "dhcpcd not running!" ${WARNING}
401 echo_warning
402 else
403 echo ""
404 echo_failure
405 fi
406 else
407 echo ""
408 killproc dhcpcd
409 fi
410 fi
411 else
412 boot_mesg -n "LEASEINFO Test failed! - " ${WARNING}
413 boot_mesg "dhcpcd is not running!" ${WARNING}
414 echo_warning
d1e90efc 415 fi
941e123e 416
5aae218d 417 elif [ "$TYPE" == "PPPOE" ]; then
a89770fa 418 boot_mesg "Bringing down the PPP interface ..."
1e4656cd
AF
419 ## Disable vnstat collection
420 /usr/bin/vnstat -u -i ppp0 -r --disable > /dev/null 2>&1
93b34528 421 rm -f /var/ipfire/red/keepconnected
a89770fa 422 killall -w -s TERM /usr/sbin/pppd 2>/dev/null
352e626f 423 evaluate_retval
d1e90efc 424 fi
0dde24fa
MT
425
426 if [ "$DEVICE" != "${GREEN_DEV}" ] && [ "$DEVICE" != "" ]; then
40049855
AF
427 link_status=`ip link show $DEVICE.7 2> /dev/null`
428 if [ -n "${link_status}" ]; then
429 if echo "${link_status}" | grep -q UP; then
430 boot_mesg "Bringing down the ${DEVICE}.7 interface..."
431 ip link set ${DEVICE}.7 down
432 vconfig del ${DEVICE} 7
433 evaluate_retval
434 fi
435 fi
0dde24fa
MT
436 link_status=`ip link show $DEVICE 2> /dev/null`
437 if [ -n "${link_status}" ]; then
438 if echo "${link_status}" | grep -q UP; then
439 boot_mesg "Bringing down the ${DEVICE} interface..."
440 ip link set ${DEVICE} down
441 evaluate_retval
442 fi
d1e90efc
MT
443 fi
444 fi
cb1fb691 445 killall -w -s KILL /usr/sbin/pppd >/dev/null 2>&1
5aae218d 446 killall -w -s KILL pptp >/dev/null 2>&1
cb1fb691 447 killall -w -s KILL br2684ctl >/dev/null 2>&1
6c33dc5c 448
1e4656cd
AF
449 ## Disable vnstat collection
450 /usr/bin/vnstat -u -i ${DEVICE} -r --disable > /dev/null 2>&1
ed35052a
AF
451
452 rm -f /var/ipfire/red/{active,device,dial-on-demand,dns1,dns2,local-ipaddress,remote-ipaddress,resolv.conf}
6c33dc5c 453 exit 0;
d1e90efc 454 ;;
d1e90efc
MT
455esac
456
457# End