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