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