]> git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/init.d/networking/red
Fix online-flagfile creation on red dhcp
[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 elif [ "${TYPE}" == "DHCP" ]; then
127 boot_mesg -n "Starting dhcpcd on the ${DEVICE} interface..."
128 echo -n "${DEVICE}" > /var/ipfire/red/iface
129
130 ## Create & Enable vnstat
131 /usr/bin/vnstat -u -i ${DEVICE} -r --enable --force > /dev/null 2>&1
132
133 # Test to see if there is a stale pid file
134 if [ -f "$PIDFILE" ]; then
135 ps `cat "$PIDFILE"` | grep dhcpcd > /dev/null
136 if [ $? != 0 ]; then
137 rm -f /var/run/dhcpcd-${DEVICE}.pid > /dev/null
138 else
139 boot_mesg "dhcpcd already running!" ${WARNING}
140 echo_warning
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 touch /var/ipfire/red/active
184 else
185 echo ""
186 $(exit "$RET")
187 evaluate_retval
188 fi
189
190 elif [ "$TYPE" == "PPPOE" ]; then
191
192 if ( ps ax | grep -q [p]ppd ); then
193 boot_mesg "pppd is still running." ${FAILURE}
194 echo_failure
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 exit 0
231 fi
232 boot_mesg "Bringing up the $TYPE interface on $PPP_NIC ..."
233 ip addr flush dev $PPP_NIC >/dev/null 2>&1
234 if [ "$TYPE" == "pptp" ]; then
235 ip addr add $PPTP_NICCFG dev $PPP_NIC
236 fi
237 ip link set ${PPP_NIC} up
238 else
239 boot_mesg "Bringing up the PPP via ${TYPE} on ${COMPORT}..."
240 fi
241
242 ### ###
243 ### Configuring the pppd ###
244 ### ###
245
246 ### Plugin Options
247 #
248 if [ "$TYPE" == "pppoe" ]; then
249 [ "${METHOD}" == "PPPOE_PLUGIN" ] && \
250 PLUGOPTS="plugin /usr/lib/pppd/2.4.4/rp-pppoe.so"
251 fi
252
253 ### Synchronous Mode
254 #
255 #PPPOE_SYNC=-s
256 #PPPD_SYNC=sync
257
258 ### Access Concentrator Name
259 #
260 if [ -n "${CONCENTRATORNAME}" ]; then
261 ACNAME="-C ${CONCENTRATORNAME}"
262 fi
263
264 ### Service Name
265 #
266 if [ -n "${SERVICENAME}" ]; then
267 if [ "${METHOD}" == "PPPOE_PLUGIN" ]; then
268 PLUGOPTS+=" rp_pppoe_service ${SERVICENAME}"
269 else
270 SERVICENAME="-S ${SERVICENAME}"
271 fi
272 fi
273
274 ### Authentication Types
275 #
276 if [ "${AUTH}" == "pap" ]; then
277 AUTH="-chap"
278 elif [ "${AUTH}" == "chap" ]; then
279 AUTH="-pap"
280 else
281 AUTH=""
282 fi
283
284 ### DNS Config
285 #
286 if [ "${DNS}" == "Automatic" ]; then
287 DNS="usepeerdns"
288 else
289 DNS=""
290 echo nameserver=$DNS1 > /etc/ppp/resolv.conf
291 echo nameserver=$DNS2 >> /etc/ppp/resolv.conf
292 fi
293
294 ### Dial On Demand
295 #
296 if [ "${RECONNECTION}" != "persistent" ]; then
297 if [ "${TIMEOUT}" != "0" ] && [ "${TIMEOUT}" != "" ]; then
298 SECONDS=$[${TIMEOUT} * 60]
299 else
300 SECONDS=300
301 fi
302 if [ "${RECONNECTION}" == "dialondemand" ]; then
303 touch /var/ipfire/red/dial-on-demand
304 DEMAND="demand persist idle ${SECONDS} 10.112.112.112:10.112.112.113"
305 DEMAND+=" ipcp-accept-remote ipcp-accept-local noipdefault ktune"
306 fi
307 fi
308
309 if [ "$TYPE" == "pppoe" ]; then
310 ### When using pppoe-plugin the device has to be the last option
311 #
312 [ "${METHOD}" == "PPPOE_PLUGIN" ] && PLUGOPTS+=" $PPP_NIC"
313 fi
314
315 if [ "$TYPE" == "modem" ]; then
316 PLUGOPTS=" /dev/${COMPORT} ${DTERATE} connect /etc/ppp/dialer lock modem crtscts"
317 METHOD="PPPOE_PLUGIN"
318 elif [ "$TYPE" == "serial" ]; then
319 PLUGOPTS=" /dev/${COMPORT} ${DTERATE} connect /bin/true lock modem crtscts"
320 METHOD="PPPOE_PLUGIN"
321 fi
322
323 ### Standard PPP options we always use
324 #
325 PPP_STD_OPTIONS="$PLUGOPTS $DNS defaultroute noipdefault noauth"
326 PPP_STD_OPTIONS+=" default-asyncmap hide-password nodetach mtu ${MTU}"
327 PPP_STD_OPTIONS+=" mru ${MTU} noaccomp nodeflate nopcomp novj novjccomp"
328 PPP_STD_OPTIONS+=" nobsdcomp user ${USERNAME} lcp-echo-interval 20"
329 PPP_STD_OPTIONS+=" lcp-echo-failure 3 ${AUTH}"
330
331 ### Debugging
332 #
333 if [ "${DEBUG}" == "on" ]; then
334 DEBUG="debug"
335 else
336 DEBUG=""
337 fi
338
339 ### PPPoE invocation
340 #
341 if [ "$TYPE" == "pppoe" ]; then
342 PPPOE_CMD="/usr/sbin/pppoe -p /var/run/ppp-ipfire.pid.pppoe -I $PPP_NIC"
343 PPPOE_CMD+=" -T 80 -U $PPPOE_SYNC $ACNAME $SERVICENAMEOPT"
344 fi
345
346 ### PPTP ###
347 #
348 if [ "$TYPE" == "pptp" ]; then
349 PPPOE_CMD="pptp $PPTP_PEER --nolaunchpppd"
350 METHOD=""
351 fi
352
353 ### Run everything
354 #
355 if [ "$METHOD" == "PPPOE_PLUGIN" ]; then
356 /usr/sbin/pppd $PPP_STD_OPTIONS $DEBUG $DEMAND >/dev/null 2>&1 &
357 evaluate_retval
358 # echo PLUGIN: /usr/sbin/pppd $PPP_STD_OPTIONS $DEBUG $DEMAND
359 else
360 /usr/sbin/pppd pty "$PPPOE_CMD" $PPP_STD_OPTIONS $DEBUG $DEMAND $PPPD_SYNC >/dev/null 2>&1 &
361 evaluate_retval
362 # echo PPP: /usr/sbin/pppd pty "$PPPOE_CMD" $PPP_STD_OPTIONS $DEBUG $DEMAND $PPPD_SYNC
363 fi
364 ## Create & Enable vnstat
365 /usr/bin/vnstat -u -i ppp0 -r --enable --force > /dev/null 2>&1
366 /etc/rc.d/init.d/connectd start
367 # Add a NaN value to ppp0 rrd to supress spikes at reconnect
368 rrdtool update $RRDLOG/collectd/localhost/interface/if_octets-ppp0.rrd \
369 $(date +%s):: > /dev/null 2>&1
370 exit 0
371 fi
372 ;;
373
374 stop)
375 if [ "$TYPE" == "STATIC" ]; then
376 boot_mesg "Stopping default gateway ${GATEWAY}..."
377 ip route del default via ${GATEWAY} >/dev/null 2>&1
378 echo_ok
379 if [ "$DEVICE" != "${GREEN_DEV}" ]; then
380 boot_mesg "Removing IPv4 addresses from the ${DEVICE} interface..."
381 ip addr flush dev ${DEVICE}
382 evaluate_retval
383 fi
384 run_subdir ${rc_base}/init.d/networking/red.down/
385
386 elif [ "$TYPE" == "DHCP" ]; then
387 boot_mesg -n "Stopping dhcpcd on the ${DEVICE} interface..."
388 if [ -e $LEASEINFO ]; then
389 . $LEASEINFO
390 if [ "$LEASETIME" = "4294967295" ]; then
391 # do nothing, just echo ok
392 echo ""
393 echo_ok
394 else
395 if [ -n "$DHCP_STOP" ]; then
396 /sbin/dhcpcd ${DEVICE} $DHCP_STOP &> /dev/null
397 RET="$?"
398 if [ "$RET" -eq 0 ]; then
399 echo ""
400 echo_ok
401 elif [ "$RET" -eq 1 ]; then
402 boot_mesg "dhcpcd not running!" ${WARNING}
403 echo_warning
404 else
405 echo ""
406 echo_failure
407 fi
408 else
409 echo ""
410 killproc dhcpcd
411 fi
412 fi
413 else
414 boot_mesg -n "LEASEINFO Test failed! - " ${WARNING}
415 boot_mesg "dhcpcd is not running!" ${WARNING}
416 echo_warning
417 fi
418
419 elif [ "$TYPE" == "PPPOE" ]; then
420 boot_mesg "Bringing down the PPP interface ..."
421 ## Disable vnstat collection
422 /usr/bin/vnstat -u -i ppp0 -r --disable > /dev/null 2>&1
423 rm -f /var/ipfire/red/keepconnected
424 killall -w -s TERM /usr/sbin/pppd 2>/dev/null
425 evaluate_retval
426 # Add a NaN value to ppp0 rrd to supress spikes at reconnect
427 rrdtool update $RRDLOG/collectd/localhost/interface/if_octets-ppp0.rrd \
428 $(date +%s):: > /dev/null 2>&1
429 fi
430
431 if [ "$DEVICE" != "${GREEN_DEV}" ] && [ "$DEVICE" != "" ]; then
432 link_status=`ip link show $DEVICE.7 2> /dev/null`
433 if [ -n "${link_status}" ]; then
434 if echo "${link_status}" | grep -q UP; then
435 boot_mesg "Bringing down the ${DEVICE}.7 interface..."
436 ip link set ${DEVICE}.7 down
437 vconfig del ${DEVICE} 7
438 evaluate_retval
439 fi
440 fi
441 link_status=`ip link show $DEVICE 2> /dev/null`
442 if [ -n "${link_status}" ]; then
443 if echo "${link_status}" | grep -q UP; then
444 boot_mesg "Bringing down the ${DEVICE} interface..."
445 ip link set ${DEVICE} down
446 evaluate_retval
447 fi
448 fi
449 fi
450 killall -w -s KILL /usr/sbin/pppd >/dev/null 2>&1
451 killall -w -s KILL pptp >/dev/null 2>&1
452 killall -w -s KILL br2684ctl >/dev/null 2>&1
453
454 ## Disable vnstat collection
455 /usr/bin/vnstat -u -i ${DEVICE} -r --disable > /dev/null 2>&1
456
457 rm -f /var/ipfire/red/{active,device,dial-on-demand,dns1,dns2,local-ipaddress,remote-ipaddress,resolv.conf}
458 exit 0;
459 ;;
460 esac
461
462 # End