]> git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/networking/red
networking red: add delay to wait for carrier
[ipfire-2.x.git] / src / initscripts / 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 . /etc/sysconfig/rc
19 . ${rc_functions}
20 . /etc/init.d/networking/functions.network
21
22 #Define some defaults
23 INET_VLAN=7
24 IPTV_VLAN=8
25 ATM_DEV=0
26
27 eval $(/usr/local/bin/readhash /var/ipfire/main/settings)
28 if [ "$RRDLOG" == "" ]; then
29 RRDLOG=/var/log/rrd
30 fi
31
32 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
33 eval $(/usr/local/bin/readhash /var/ipfire/dns/settings)
34 eval $(/usr/local/bin/readhash /var/ipfire/mac/settings)
35
36 MAC=$(tr - : <<<$MAC)
37 MAC1=$(tr - : <<<$MAC1)
38 MAC2=$(tr - : <<<$MAC2)
39
40 TYPE="${RED_TYPE}"
41 DEVICE="${RED_DEV}"
42
43 if [ "$TYPE" == "STATIC" ] || [ "$TYPE" == "DHCP" ]; then
44 if [ "$DEVICE" == "" ]; then
45 boot_mesg "No device for red network. Please run setup." ${FAILURE}
46 echo_failure
47 [ "${1}" == "start" ] && exit 0
48 fi
49 fi
50
51 if [ "${TYPE}" == "STATIC" ]; then
52 if [ "${DEVICE}" != "${GREEN_DEV}" ]; then
53 ADDRESS="${RED_ADDRESS}"
54 BROADCAST="${RED_BROADCAST}"
55 NETADDRESS="${RED_NETADDRESS}"
56 NETMASK="${RED_NETMASK}"
57 else
58 ADDRESS="${GREEN_ADDRESS}"
59 BROADCAST="${GREEN_BROADCAST}"
60 NETADDRESS="${GREEN_NETADDRESS}"
61 NETMASK="${GREEN_NETMASK}"
62 fi
63 GATEWAY="${DEFAULT_GATEWAY}"
64 # DNS1
65 # DNS2
66
67 if [ -z "${BROADCAST}" ]; then
68 boot_mesg "BROADCAST variable missing, cannot continue." ${FAILURE}
69 echo_failure
70 exit 1
71 fi
72 if [ -n "${ADDRESS}" -a -n "${NETMASK}" ]; then
73 PREFIX=`whatmask ${NETMASK} | grep -e ^CIDR | awk -F': ' '{ print $2 }' | cut -c 2-`
74 args="${args} ${ADDRESS}/${PREFIX} broadcast ${BROADCAST}"
75 else
76 boot_mesg "ADDRESS and/or NETMASK variable missing from input, cannot continue." ${FAILURE}
77 echo_failure
78 exit 1
79 fi
80 fi
81
82 case "${1}" in
83 start)
84 if [ "${DEVICE}" != "${GREEN_DEV}" ] && [ "${DEVICE}" != "" ]; then
85 boot_mesg "Bringing up the ${DEVICE} interface..."
86 boot_mesg_flush
87 # Check if an interface is there...
88 if ip link show ${DEVICE} > /dev/null 2>&1; then
89 link_status=`ip link show ${DEVICE} 2> /dev/null`
90 if [ -n "${link_status}" ]; then
91 if ! echo "${link_status}" | grep -q UP; then
92 if [ -n "$MAC" ]; then
93 boot_mesg "Setting mac address on ${DEVICE} to ${MAC}"
94 ip link set dev ${DEVICE} address ${MAC}
95 evaluate_retval
96 fi
97 ip link set ${DEVICE} up
98 fi
99 fi
100 else
101 boot_mesg "Interface ${DEVICE} doesn't exist." ${FAILURE}
102 echo_failure
103 exit 1
104 fi
105 fi
106
107 ## Create & Enable vnstat
108 /usr/bin/vnstat -u -i ${DEVICE} -r --enable --force > /dev/null 2>&1
109
110 if [ "${TYPE}" == "STATIC" ]; then
111
112 if [ "$DEVICE" != "${GREEN_DEV}" ]; then
113 boot_mesg "Adding IPv4 address ${ADDRESS} to the ${DEVICE} interface..."
114 ip addr add ${args} dev ${DEVICE}
115 evaluate_retval
116 fi
117 echo -n "${DEVICE}" > /var/ipfire/red/iface
118 echo -n "${ADDRESS}" > /var/ipfire/red/local-ipaddress
119 echo -n "${GATEWAY}" > /var/ipfire/red/remote-ipaddress
120 grep -v -E "\<gateway\>" /etc/hosts > /tmp/hosts
121 echo "$GATEWAY gateway" >> /tmp/hosts
122 mv /tmp/hosts /etc/hosts
123 echo -n "${DNS1}" > /var/ipfire/red/dns1
124 echo -n "${DNS2}" > /var/ipfire/red/dns2
125 touch /var/ipfire/red/active
126
127 # Create route to default gateway
128 ip route add ${GATEWAY} dev ${DEVICE}
129
130 boot_mesg "Setting up default gateway ${GATEWAY}..."
131 ip route add default via ${GATEWAY} dev ${DEVICE}
132 evaluate_retval
133
134 # has carrier ?
135 if [ ! "$(</sys/class/net/${DEVICE}/carrier)" = "1" ]; then
136 boot_mesg -n "Wait for carrier on ${DEVICE} "
137 for (( i=30; i>1; i-- )) do
138 if [ "$(</sys/class/net/${DEVICE}/carrier)" = "1" ]; then
139 break;
140 fi
141 boot_mesg -n "."
142 sleep 2
143 done
144 boot_mesg ""
145 if [ ! "$(</sys/class/net/${DEVICE}/carrier)" = "1" ]; then
146 echo_failure
147 else
148 echo_ok
149 fi
150 fi
151
152 run_subdir ${rc_base}/init.d/networking/red.up/
153
154 # Configure aliases only if red static
155 /usr/local/bin/setaliases
156
157 elif [ "${TYPE}" == "DHCP" ]; then
158 # Add firewall rules to allow comunication with the dhcp server on red.
159 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i ${DEVICE} -j ACCEPT
160 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i ${DEVICE} -j ACCEPT
161
162 echo -n "${DEVICE}" > /var/ipfire/red/iface
163
164 # Check if the wlan-client is used on red.
165 # To determine this we check if a wpa_supplicant is running.
166 pid="$(pidof wpa_supplicant)"
167
168 if [ -z "${pid}" ]; then
169 # No wpa_supplicant is running. So it's save to start dhcpcd.
170 dhcpcd_start "${DEVICE}"
171 fi
172
173 ## Create & Enable vnstat
174 /usr/bin/vnstat -u -i ${DEVICE} -r --enable --force > /dev/null 2>&1
175
176 elif [ "$TYPE" == "PPPOE" ]; then
177
178 if ( ps ax | grep -q [p]ppd ); then
179 boot_mesg "pppd is still running." ${FAILURE}
180 echo_failure
181 exit 1
182 fi
183
184 eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
185
186 [ -c "/dev/ppp" ] || mknod /dev/ppp c 108 0
187
188 # We force the plugin method, anyway.
189 METHOD="PPPOE_PLUGIN"
190
191 PPP_NIC=${DEVICE}
192
193
194 if [ "$TYPE" == "pppoeatm" ] || [ "$TYPE" == "pptpatm" ]; then
195 PPP_NIC=nas${ATM_DEV}
196 DEVICE=nas${ATM_DEV}
197 boot_mesg "Creating ATM-Bridge as $PPP_NIC ..."
198 br2684ctl -c${ATM_DEV} -e${ENCAP} -a${ATM_DEV}.${VPI}.${VCI} >/dev/null 2>&1 &
199 sleep 1
200
201 # use user-defined or green mac address for nas0
202 if [ -n "$MAC" ]; then
203 ip link set dev nas${ATM_DEV} address ${MAC}
204 else
205 ip link set dev nas${ATM_DEV} address $(cat /sys/class/net/green0/address)
206 fi
207
208 if [ "$TYPE" == "pppoeatm" ]; then
209 TYPE="pppoe"
210 fi
211 if [ "$TYPE" == "pptpatm" ]; then
212 TYPE="pptp"
213 fi
214 fi
215
216 if [ "$TYPE" == "vdsl" ]; then
217 boot_mesg "Creating VLAN Interface ${DEVICE}.${INET_VLAN} ..."
218 modprobe 8021q
219 vconfig add ${DEVICE} ${INET_VLAN}
220 if [ -n "$MAC1" ]; then
221 boot_mesg "Setting mac address on ${DEVICE}.${INET_VLAN} to ${MAC1}"
222 ip link set dev ${DEVICE}.${INET_VLAN} address ${MAC1}
223 evaluate_retval
224 fi
225 PPP_NIC=${DEVICE}.${INET_VLAN}
226 sleep 0.2
227 ip link set ${PPP_NIC} up
228 TYPE="pppoe"
229 fi
230 if [ "${IPTV}" == "enable" ]; then
231 PIDFILE="/var/run/dhcpcd-${DEVICE}.${IPTV_VLAN}.pid"
232 LEASEINFO="/var/ipfire/dhcpc/dhcpcd-${DEVICE}.${IPTV_VLAN}.info"
233 # Test to see if there is a stale pid file
234 if [ -f "$PIDFILE" ]; then
235 ps `cat "$PIDFILE"` | grep dhcpcd > /dev/null
236 if [ $? != 0 ]; then
237 rm -f /var/run/dhcpcd-${DEVICE}.${IPTV_VLAN}.pid > /dev/null
238 fi
239 fi
240
241 if [ ! -f "$PIDFILE" ]; then
242 boot_mesg "Creating VLAN Interface ${DEVICE}.${IPTV_VLAN} ..."
243 modprobe 8021q
244 vconfig add ${DEVICE} ${IPTV_VLAN}
245 if [ -n "$MAC2" ]; then
246 boot_mesg "Setting mac address on ${DEVICE}.${IPTV_VLAN} to ${MAC2}"
247 ip link set dev ${DEVICE}.${IPTV_VLAN} address ${MAC2}
248 evaluate_retval
249 fi
250 boot_mesg -n "Starting dhcpcd on the ${DEVICE}.${IPTV_VLAN} interface..."
251 /sbin/dhcpcd ${DEVICE}.${IPTV_VLAN} ${DHCP_START} >/dev/null 2>&1
252 RET="$?"
253
254 if [ "$RET" = "0" ]; then
255 . /var/ipfire/dhcpc/dhcpcd-${DEVICE}.${IPTV_VLAN}.info
256 echo ""
257 echo_ok
258 boot_mesg " DHCP Assigned Settings for ${DEVICE}.${IPTV_VLAN}:"
259 boot_mesg_flush
260 boot_mesg " IP Address: $ip_address"
261 boot_mesg_flush
262 boot_mesg " Hostname: $RED_DHCP_HOSTNAME"
263 boot_mesg_flush
264 boot_mesg " Subnet Mask: $subnet_mask"
265 boot_mesg_flush
266 boot_mesg " Default Gateway: $routers"
267 boot_mesg_flush
268 boot_mesg " DNS Server: $domain_name_servers"
269 boot_mesg_flush
270
271 else
272 echo ""
273 $(exit "$RET")
274 evaluate_retval
275 fi
276 fi
277 fi
278 if [ "$TYPE" == "pppoe" ] || [ "$TYPE" == "pptp" ]; then
279 if [ "$PPP_NIC" == "" ]; then
280 boot_mesg "No device for red interface given. Check netsetup or dialprofile!" ${FAILURE}
281 echo_failure
282 exit 0
283 fi
284 boot_mesg "Bringing up the $TYPE interface on $PPP_NIC ..."
285 ip addr flush dev $PPP_NIC >/dev/null 2>&1
286 if [ "$TYPE" == "pptp" ]; then
287 if [ "$PPTP_NICCFG" == "dhcp" ]; then
288 # Test to see if there is a stale pid file
289 if [ -f "$PIDFILE" ]; then
290 ps `cat "$PIDFILE"` | grep dhcpcd > /dev/null
291 if [ $? != 0 ]; then
292 rm -f /var/run/dhcpcd-${DEVICE}.pid > /dev/null
293 fi
294 fi
295
296 if [ ! -f "$PIDFILE" ]; then
297 boot_mesg -n "Starting dhcpcd on the ${DEVICE} interface..."
298 /sbin/dhcpcd ${DEVICE} ${DHCP_START} >/dev/null 2>&1
299 RET="$?"
300
301 if [ "$RET" = "0" ]; then
302 . /var/ipfire/dhcpc/dhcpcd-${DEVICE}.info
303 echo ""
304 echo_ok
305 boot_mesg " DHCP Assigned Settings for ${DEVICE}:"
306 boot_mesg_flush
307 boot_mesg " IP Address: $ip_address"
308 boot_mesg_flush
309 boot_mesg " Hostname: $RED_DHCP_HOSTNAME"
310 boot_mesg_flush
311 boot_mesg " Subnet Mask: $subnet_mask"
312 boot_mesg_flush
313 boot_mesg " Default Gateway: $routers"
314 boot_mesg_flush
315 boot_mesg " DNS Server: $domain_name_servers"
316 boot_mesg_flush
317 /sbin/route add $PPTP_PEER gw $routers $PPP_NIC
318 else
319 echo ""
320 $(exit "$RET")
321 evaluate_retval
322 fi
323 fi
324 else
325 ip addr add $PPTP_NICCFG dev $PPP_NIC
326 fi
327 fi
328 ip link set ${PPP_NIC} up
329 if [ -n "${PPTP_ROUTE}" ]; then
330 boot_mesg "Set route ${PPTP_ROUTE} to pptp server..."
331 route add ${PPTP_ROUTE}
332 fi
333 else
334 boot_mesg "Bringing up the PPP via ${TYPE} on ${COMPORT}..."
335 fi
336
337 ### ###
338 ### Configuring the pppd ###
339 ### ###
340
341 ### Plugin Options
342 #
343 if [ "$TYPE" == "pppoe" ]; then
344 [ "${METHOD}" == "PPPOE_PLUGIN" ] && \
345 PLUGOPTS="plugin rp-pppoe.so"
346 fi
347
348 ### Synchronous Mode
349 #
350 #PPPOE_SYNC=-s
351 #PPPD_SYNC=sync
352
353 ### Access Concentrator Name
354 #
355 if [ -n "${CONCENTRATORNAME}" ]; then
356 ACNAME="-C ${CONCENTRATORNAME}"
357 fi
358
359 ### Service Name
360 #
361 if [ -n "${SERVICENAME}" ]; then
362 if [ "${METHOD}" == "PPPOE_PLUGIN" ]; then
363 PLUGOPTS+=" rp_pppoe_service ${SERVICENAME}"
364 else
365 SERVICENAME="-S ${SERVICENAME}"
366 fi
367 fi
368
369 ### Authentication Types
370 #
371 if [ "${AUTH}" == "pap" ]; then
372 AUTH="-chap"
373 elif [ "${AUTH}" == "chap" ]; then
374 AUTH="-pap"
375 else
376 AUTH=""
377 fi
378
379 ### DNS Config
380 #
381 if [ "${DNS}" == "Automatic" ]; then
382 DNS="usepeerdns"
383 else
384 DNS=""
385 echo nameserver=$DNS1 > /etc/ppp/resolv.conf
386 echo nameserver=$DNS2 >> /etc/ppp/resolv.conf
387 fi
388
389 ### Dial On Demand
390 #
391 if [ "${RECONNECTION}" != "persistent" ]; then
392 if [ "${TIMEOUT}" != "0" ] && [ "${TIMEOUT}" != "" ]; then
393 SECONDS=$[${TIMEOUT} * 60]
394 else
395 SECONDS=300
396 fi
397 if [ "${RECONNECTION}" == "dialondemand" ]; then
398 touch /var/ipfire/red/dial-on-demand
399 DEMAND="demand persist idle ${SECONDS} 10.112.112.112:10.112.112.113"
400 DEMAND+=" ipcp-accept-remote ipcp-accept-local noipdefault ktune"
401 fi
402 fi
403
404 if [ "$TYPE" == "pppoe" ]; then
405 ### When using pppoe-plugin the device has to be the last option
406 #
407 [ "${METHOD}" == "PPPOE_PLUGIN" ] && PLUGOPTS+=" $PPP_NIC"
408 fi
409
410 if [ "$TYPE" == "modem" ]; then
411 PLUGOPTS=" /dev/${COMPORT} ${DTERATE} connect /etc/ppp/dialer lock modem crtscts"
412 METHOD="PPPOE_PLUGIN"
413 elif [ "$TYPE" == "serial" ]; then
414 PLUGOPTS=" /dev/${COMPORT} ${DTERATE} connect /bin/true lock modem crtscts"
415 METHOD="PPPOE_PLUGIN"
416 fi
417
418 ### Standard PPP options we always use
419 #
420 PPP_STD_OPTIONS="$PLUGOPTS $DNS defaultroute noipdefault noauth"
421 PPP_STD_OPTIONS+=" default-asyncmap hide-password nodetach"
422 PPP_STD_OPTIONS+=" noaccomp nodeflate nopcomp novj novjccomp"
423 PPP_STD_OPTIONS+=" nobsdcomp user ${USERNAME} lcp-echo-interval 20"
424 PPP_STD_OPTIONS+=" lcp-echo-failure 5 ${AUTH}"
425
426 if [ -n "${MTU}" ]; then
427 PPP_STD_OPTIONS="${PPP_STD_OPTIONS} mtu ${MTU}"
428 fi
429
430 if [ -n "${MRU}" ]; then
431 PPP_STD_OPTIONS="${PPP_STD_OPTIONS} mru ${MRU}"
432 fi
433
434 ### Debugging
435 #
436 if [ "${DEBUG}" == "on" ]; then
437 DEBUG="debug"
438 else
439 DEBUG=""
440 fi
441
442 ### PPPoE invocation
443 #
444 if [ "$TYPE" == "pppoe" ]; then
445 PPPOE_CMD="/usr/sbin/pppoe -p /var/run/ppp-ipfire.pid.pppoe -I $PPP_NIC"
446 PPPOE_CMD+=" -T 80 -U $PPPOE_SYNC $ACNAME $SERVICENAMEOPT"
447 fi
448
449 ### PPTP ###
450 #
451 if [ "$TYPE" == "pptp" ]; then
452 PPPOE_CMD="pptp $PPTP_PEER --nolaunchpppd"
453 METHOD=""
454 fi
455
456 ### Run everything
457 #
458 if [ "$METHOD" == "PPPOE_PLUGIN" ]; then
459 /usr/sbin/pppd $PPP_STD_OPTIONS $DEBUG $DEMAND >/dev/null 2>&1 &
460 evaluate_retval
461 # echo PLUGIN: /usr/sbin/pppd $PPP_STD_OPTIONS $DEBUG $DEMAND
462 else
463 /usr/sbin/pppd pty "$PPPOE_CMD" $PPP_STD_OPTIONS $DEBUG $DEMAND $PPPD_SYNC >/dev/null 2>&1 &
464 evaluate_retval
465 # echo PPP: /usr/sbin/pppd pty "$PPPOE_CMD" $PPP_STD_OPTIONS $DEBUG $DEMAND $PPPD_SYNC
466 fi
467 ## Create & Enable vnstat
468 /usr/bin/vnstat -u -i ppp0 -r --enable --force > /dev/null 2>&1
469 /etc/rc.d/init.d/connectd start
470 # Add a NaN value to ppp0 rrd to supress spikes at reconnect
471 rrdtool update $RRDLOG/collectd/localhost/interface/if_octets-ppp0.rrd \
472 $(date +%s):: > /dev/null 2>&1
473 exit 0
474 fi
475 ;;
476
477 stop)
478 rm -f /var/ipfire/red/{active,device,dial-on-demand,dns1,dns2,local-ipaddress,remote-ipaddress,resolv.conf}
479
480 if [ "$TYPE" == "STATIC" ]; then
481 boot_mesg "Stopping default gateway ${GATEWAY}..."
482 ip route del default via ${GATEWAY} >/dev/null 2>&1
483 echo_ok
484 if [ "$DEVICE" != "${GREEN_DEV}" ]; then
485 boot_mesg "Removing IPv4 addresses from the ${DEVICE} interface..."
486 ip addr flush dev ${DEVICE}
487 evaluate_retval
488 fi
489 run_subdir ${rc_base}/init.d/networking/red.down/
490
491 elif [ "$TYPE" == "PPPOE" ]; then
492 boot_mesg "Bringing down the PPP interface ..."
493 ## Disable vnstat collection
494 /usr/bin/vnstat -u -i ppp0 -r --disable > /dev/null 2>&1
495 rm -f /var/ipfire/red/keepconnected
496 killall -w -s TERM /usr/sbin/pppd 2>/dev/null
497 evaluate_retval
498 # Add a NaN value to ppp0 rrd to supress spikes at reconnect
499 rrdtool update $RRDLOG/collectd/localhost/interface/if_octets-ppp0.rrd \
500 $(date +%s):: > /dev/null 2>&1
501
502 elif [ "$TYPE" == "DHCP" ]; then
503 # Check if the wlan-client is used on red.
504 # To determine this we check if a wpa_supplicant is running.
505 pid="$(pidof wpa_supplicant)"
506
507 if [ -z "${pid}" ]; then
508 # Stop dhcpcd.
509 dhcpcd_stop "${DEVICE}"
510 fi
511 fi
512
513 if [ -n "${PPTP_ROUTE}" ]; then
514 route del ${PPTP_ROUTE}
515 fi
516
517 if [ "$DEVICE" != "${GREEN_DEV}" ] && [ "$DEVICE" != "" ]; then
518 link_status=`ip link show $DEVICE.${INET_VLAN} 2> /dev/null`
519 if [ -n "${link_status}" ]; then
520 if echo "${link_status}" | grep -q UP; then
521 boot_mesg "Bringing down the ${DEVICE}.${INET_VLAN} interface..."
522 ip link set ${DEVICE}.${INET_VLAN} down
523 vconfig rem ${DEVICE}.${INET_VLAN}
524 evaluate_retval
525 fi
526 else
527 link_status=`ip link show $DEVICE 2> /dev/null`
528 if [ -n "${link_status}" ]; then
529 if echo "${link_status}" | grep -q UP; then
530 boot_mesg "Bringing down the ${DEVICE} interface..."
531 ip link set ${DEVICE} down
532 evaluate_retval
533 fi
534 fi
535 fi
536 fi
537 killall -w -s KILL /usr/sbin/pppd >/dev/null 2>&1
538 killall -w -s KILL pptp >/dev/null 2>&1
539 killall -w -s KILL br2684ctl >/dev/null 2>&1
540
541 ## Disable vnstat collection
542 /usr/bin/vnstat -u -i ${DEVICE} -r --disable > /dev/null 2>&1
543
544 exit 0;
545 ;;
546 esac
547
548 # End