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