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