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