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