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