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