]> git.ipfire.org Git - ipfire-2.x.git/blame - src/initscripts/init.d/networking/red
Merge branch 'master' of git://git.ipfire.org/ipfire-2.x
[ipfire-2.x.git] / src / initscripts / init.d / networking / red
CommitLineData
d1e90efc
MT
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}
20eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
21
22TYPE="${RED_TYPE}"
23DEVICE="${RED_DEV}"
24
0dde24fa
MT
25if [ "$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
31fi
32
33if [ "${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
d1e90efc 45 GATEWAY="${DEFAULT_GATEWAY}"
0db33b56
MT
46 # DNS1
47 # DNS2
d1e90efc
MT
48
49 if [ -z "${BROADCAST}" ]; then
0dde24fa 50 boot_mesg "BROADCAST variable missing, cannot continue." ${FAILURE}
d1e90efc
MT
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
63elif [ "${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
70fi
71
72case "${1}" in
73 start)
0dde24fa
MT
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
d1e90efc 84 fi
0dde24fa
MT
85 else
86 boot_mesg "Interface ${DEVICE} doesn't exist." ${FAILURE}
87 echo_failure
88 exit 1
d1e90efc 89 fi
d1e90efc 90 fi
6c33dc5c 91
1e4656cd
AF
92 ## Create & Enable vnstat
93 /usr/bin/vnstat -u -i ${DEVICE} -r --enable --force > /dev/null 2>&1
d1e90efc
MT
94
95 if [ "${TYPE}" == "STATIC" ]; then
0dde24fa
MT
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
0db33b56
MT
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
b2f872eb
AF
105 grep -v "gateway" /etc/hosts > /tmp/hosts
106 echo "$GATEWAY gateway" >> /tmp/hosts
107 mv /tmp/hosts /etc/hosts
0db33b56
MT
108 echo -n "${DNS1}" > /var/ipfire/red/dns1
109 echo -n "${DNS2}" > /var/ipfire/red/dns2
bcdde652 110 touch /var/ipfire/red/active
d1e90efc 111
040e5040
MT
112 boot_mesg "Setting up default gateway ${GATEWAY}..."
113 ip route add default via ${GATEWAY} dev ${DEVICE}
114 evaluate_retval
115
0e42072a
MT
116 run_subdir ${rc_base}/init.d/networking/red.up/
117
d1e90efc
MT
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 # Test to see if there is a stale pid file
123 if [ -f "$PIDFILE" ]; then
124 ps `cat "$PIDFILE"` | grep dhcpcd > /dev/null
125 if [ $? != 0 ]; then
126 rm -f /var/run/dhcpcd-${DEVICE}.pid > /dev/null
127 else
128 boot_mesg "dhcpcd already running!" ${WARNING}
129 echo_warning
130 exit 2
131 fi
132 fi
133
134 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i ${DEVICE} -j ACCEPT
135 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i ${DEVICE} -j ACCEPT
136
7f5fdd04
MT
137 if [ -n "${RED_DHCP_HOSTNAME}" ]; then
138 DHCP_START+="-h ${RED_DHCP_HOSTNAME} "
d1e90efc
MT
139 fi
140
141 /sbin/dhcpcd ${DEVICE} ${DHCP_START} >/dev/null 2>&1
142 RET="$?"
143
144 if [ "$RET" = "0" ]; then
145 . /var/ipfire/dhcpc/dhcpcd-${DEVICE}.info
146 echo ""
147 echo_ok
148 boot_mesg " DHCP Assigned Settings for ${DEVICE}:"
149 boot_mesg_flush
150 boot_mesg " IP Address: $IPADDR"
151 boot_mesg_flush
7f5fdd04
MT
152 if [ -n "${RED_DHCP_HOSTNAME}" ]; then
153 boot_mesg " Hostname: $RED_DHCP_HOSTNAME"
d1e90efc
MT
154 boot_mesg_flush
155 fi
156 boot_mesg " Subnet Mask: $NETMASK"
157 boot_mesg_flush
158 boot_mesg " Default Gateway: $GATEWAY"
159 boot_mesg_flush
160 boot_mesg " DNS Server: $DNS"
161 boot_mesg_flush
162
163 echo -n `/etc/rc.d/helper/getdnsfromdhcpc.pl 1` > /var/ipfire/red/dns1
164 echo -n `/etc/rc.d/helper/getdnsfromdhcpc.pl 2` > /var/ipfire/red/dns2
165
166 . /var/ipfire/dhcpc/dhcpcd-${DEVICE}.info
167 echo "$IPADDR" > /var/ipfire/red/local-ipaddress
a332b303
CS
168 grep -v "gateway" /etc/hosts > /tmp/hosts
169 echo "$GATEWAY gateway" >> /tmp/hosts
170 mv /tmp/hosts /etc/hosts
d1e90efc
MT
171 echo "$GATEWAY" > /var/ipfire/red/remote-ipaddress
172 else
173 echo ""
174 $(exit "$RET")
175 evaluate_retval
176 fi
177
178 elif [ "$TYPE" == "PPPOE" ]; then
a89770fa
MT
179
180 if ( ps ax | grep -q [p]ppd ); then
0dde24fa
MT
181 boot_mesg "pppd is still running." ${FAILURE}
182 echo_failure
183 exit 1
a89770fa 184 fi
0dde24fa 185
d1e90efc
MT
186 eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
187
905fbf3e 188 [ -c "/dev/ppp" ] || mknod /dev/ppp c 108 0
d1e90efc 189
a89770fa
MT
190 PPP_NIC=${DEVICE}
191
192 if [ "$TYPE" == "pppoeatm" ]; then
193 PPP_NIC=nas0
0dde24fa 194 boot_mesg "Createing ATM-Bridge as $PPP_NIC ..."
a89770fa
MT
195 br2684ctl -c0 -e${ENCAP} -a0.${VPI}.${VCI} >/dev/null 2>&1 &
196 sleep 1
70631572 197 ip link set ${PPP_NIC} up
a89770fa
MT
198 TYPE="pppoe"
199 fi
200 if [ "$TYPE" == "pppoe" ]; then
4cd4876a 201 if [ "$PPP_NIC" == "" ]; then
0dde24fa
MT
202 boot_mesg "No device for red interface given. Check netsetup or dialprofile!" ${FAILURE}
203 echo_failure
cb1fb691 204 exit 0
0dde24fa 205 fi
a89770fa
MT
206 boot_mesg "Bringing up the PPPoE interface on $PPP_NIC ..."
207 ip addr add 1.1.1.1/24 broadcast 1.1.1.255 dev $PPP_NIC
27b8cc24
MT
208 else
209 boot_mesg "Bringing up the PPP via ${TYPE} on ${COMPORT}..."
210 fi
0dde24fa 211
905fbf3e
MT
212 ### ###
213 ### Configuring the pppd ###
214 ### ###
215
216 ### Plugin Options
217 #
218 [ "${METHOD}" == "PPPOE_PLUGIN" ] && \
a89770fa
MT
219 PLUGOPTS="plugin /usr/lib/pppd/2.4.4/rp-pppoe.so"
220
221 # PLUGOPTS="plugin /usr/lib/pppd/2.4.4/rp-pppoe.so nic-$PPP_NIC"
d1e90efc 222
905fbf3e
MT
223 ### Synchronous Mode
224 #
225 #PPPOE_SYNC=-s
a89770fa
MT
226 #PPPD_SYNC=sync
227
228 ### Access Concentrator Name
229 #
905fbf3e
MT
230 if [ -n "${CONCENTRATORNAME}" ]; then
231 ACNAME="-C ${CONCENTRATORNAME}"
d1e90efc 232 fi
905fbf3e
MT
233
234 ### Service Name
235 #
236 if [ -n "${SERVICENAME}" ]; then
237 if [ "${METHOD}" == "PPPOE_PLUGIN" ]; then
238 PLUGOPTS+=" rp_pppoe_service ${SERVICENAME}"
239 else
240 SERVICENAME="-S ${SERVICENAME}"
241 fi
d1e90efc 242 fi
a89770fa 243
905fbf3e
MT
244 ### Authentication Types
245 #
d1e90efc 246 if [ "${AUTH}" == "pap" ]; then
905fbf3e 247 AUTH="-chap"
d1e90efc 248 elif [ "${AUTH}" == "chap" ]; then
905fbf3e 249 AUTH="-pap"
57cb9775
CS
250 else
251 AUTH=""
d1e90efc 252 fi
a89770fa
MT
253
254 ### DNS Config
255 #
256 if [ "${DNS}" == "Automatic" ]; then
257 DNS="usepeerdns"
258 else
259 DNS=""
260 echo nameserver=$DNS1 > /etc/ppp/resolv.conf
261 echo nameserver=$DNS2 >> /etc/ppp/resolv.conf
262
263 fi
264
905fbf3e
MT
265 ### Dial On Demand
266 #
d1e90efc
MT
267 if [ "${RECONNECTION}" != "persistent" ]; then
268 if [ "${TIMEOUT}" != "0" ] && [ "${TIMEOUT}" != "" ]; then
269 SECONDS=$[${TIMEOUT} * 60]
905fbf3e
MT
270 else
271 SECONDS=300
d1e90efc
MT
272 fi
273 if [ "${RECONNECTION}" == "dialondemand" ]; then
274 touch /var/ipfire/red/dial-on-demand
905fbf3e 275 DEMAND="demand persist idle ${SECONDS} 10.112.112.112:10.112.112.113"
a89770fa 276 DEMAND+=" ipcp-accept-remote ipcp-accept-local noipdefault ktune"
d1e90efc 277 fi
905fbf3e
MT
278 fi
279
27b8cc24 280 ### When using pppoe-plugin the device has to be the last option
905fbf3e 281 #
a89770fa 282 [ "${METHOD}" == "PPPOE_PLUGIN" ] && PLUGOPTS+=" $PPP_NIC"
27b8cc24
MT
283
284 if [ "$TYPE" == "modem" ]; then
a89770fa 285 PLUGOPTS=" /dev/${COMPORT} ${DTERATE} connect /etc/ppp/dialer lock modem crtscts"
27b8cc24 286 elif [ "$TYPE" == "serial" ]; then
a89770fa 287 PLUGOPTS=" /dev/${COMPORT} ${DTERATE} connect /bin/true lock modem crtscts"
27b8cc24 288 fi
905fbf3e
MT
289
290 ### Standard PPP options we always use
291 #
a89770fa 292 PPP_STD_OPTIONS="$PLUGOPTS $DNS defaultroute noipdefault noauth"
905fbf3e
MT
293 PPP_STD_OPTIONS+=" default-asyncmap hide-password nodetach mtu ${MTU}"
294 PPP_STD_OPTIONS+=" mru ${MTU} noaccomp nodeflate nopcomp novj novjccomp"
295 PPP_STD_OPTIONS+=" nobsdcomp user ${USERNAME} lcp-echo-interval 20"
296 PPP_STD_OPTIONS+=" lcp-echo-failure 3 ${AUTH}"
a89770fa 297
905fbf3e
MT
298 ### Debugging
299 #
300 if [ "${DEBUG}" == "on" ]; then
301 DEBUG="debug"
d1e90efc 302 else
905fbf3e 303 DEBUG=""
d1e90efc
MT
304 fi
305
905fbf3e
MT
306 ### PPPoE invocation
307 #
a89770fa
MT
308 if [ "$TYPE" == "pppoe" ]; then
309 PPPOE_CMD="/usr/sbin/pppoe -p /var/run/ppp-ipfire.pid.pppoe -I $PPP_NIC"
27b8cc24
MT
310 PPPOE_CMD+=" -T 80 -U $PPPOE_SYNC $ACNAME $SERVICENAMEOPT"
311 fi
d1e90efc 312
905fbf3e
MT
313 ### Run everything
314 #
315 if [ "${METHOD}" == "PPPOE_PLUGIN" ]; then
316 /usr/sbin/pppd $PPP_STD_OPTIONS $DEBUG $DEMAND >/dev/null 2>&1 &
317 evaluate_retval
a89770fa 318 # echo PLUGIN: /usr/sbin/pppd $PPP_STD_OPTIONS $DEBUG $DEMAND
905fbf3e
MT
319 else
320 /usr/sbin/pppd pty "$PPPOE_CMD" $PPP_STD_OPTIONS $DEBUG $DEMAND $PPPD_SYNC >/dev/null 2>&1 &
321 evaluate_retval
a89770fa 322 # echo PPP: /usr/sbin/pppd pty "$PPPOE_CMD" $PPP_STD_OPTIONS $DEBUG $DEMAND $PPPD_SYNC
905fbf3e 323 fi
1e4656cd
AF
324 ## Create & Enable vnstat
325 /usr/bin/vnstat -u -i ppp0 -r --enable --force > /dev/null 2>&1
905fbf3e
MT
326 /etc/rc.d/init.d/connectd start
327
d1e90efc 328 fi
d1e90efc
MT
329 ;;
330
331 stop)
332 if [ "$TYPE" == "STATIC" ]; then
0dde24fa
MT
333 boot_mesg "Stopping default gateway ${GATEWAY}..."
334 ip route del default via ${GATEWAY} >/dev/null 2>&1
335 echo_ok
336 if [ "$DEVICE" != "${GREEN_DEV}" ]; then
70631572
AF
337 boot_mesg "Removing IPv4 addresses from the ${DEVICE} interface..."
338 ip addr flush dev ${DEVICE}
0dde24fa
MT
339 evaluate_retval
340 fi
0e42072a
MT
341 run_subdir ${rc_base}/init.d/networking/red.down/
342
d1e90efc
MT
343 elif [ "$TYPE" == "DHCP" ]; then
344 boot_mesg -n "Stopping dhcpcd on the ${DEVICE} interface..."
345 if [ -e $LEASEINFO ]; then
346 . $LEASEINFO
347 if [ "$LEASETIME" = "4294967295" ]; then
348 # do nothing, just echo ok
349 echo ""
350 echo_ok
351 else
352 if [ -n "$DHCP_STOP" ]; then
353 /sbin/dhcpcd ${DEVICE} $DHCP_STOP &> /dev/null
354 RET="$?"
355 if [ "$RET" -eq 0 ]; then
356 echo ""
357 echo_ok
358 elif [ "$RET" -eq 1 ]; then
359 boot_mesg "dhcpcd not running!" ${WARNING}
360 echo_warning
361 else
362 echo ""
363 echo_failure
364 fi
365 else
366 echo ""
367 killproc dhcpcd
368 fi
369 fi
370 else
371 boot_mesg -n "LEASEINFO Test failed! - " ${WARNING}
372 boot_mesg "dhcpcd is not running!" ${WARNING}
373 echo_warning
374 exit 1
375 fi
376
377 elif [ "$TYPE" == "PPPOE" ]; then
a89770fa 378 boot_mesg "Bringing down the PPP interface ..."
1e4656cd
AF
379 ## Disable vnstat collection
380 /usr/bin/vnstat -u -i ppp0 -r --disable > /dev/null 2>&1
93b34528 381 rm -f /var/ipfire/red/keepconnected
a89770fa 382 killall -w -s TERM /usr/sbin/pppd 2>/dev/null
352e626f 383 evaluate_retval
0dde24fa 384 ip addr del 1.1.1.1/24 broadcast 1.1.1.255 dev ${DEVICE} >/dev/null 2>&1
d1e90efc 385 fi
0dde24fa
MT
386
387 if [ "$DEVICE" != "${GREEN_DEV}" ] && [ "$DEVICE" != "" ]; then
388 link_status=`ip link show $DEVICE 2> /dev/null`
389 if [ -n "${link_status}" ]; then
390 if echo "${link_status}" | grep -q UP; then
391 boot_mesg "Bringing down the ${DEVICE} interface..."
392 ip link set ${DEVICE} down
393 evaluate_retval
394 fi
d1e90efc
MT
395 fi
396 fi
cb1fb691
MT
397 killall -w -s KILL /usr/sbin/pppd >/dev/null 2>&1
398 killall -w -s KILL br2684ctl >/dev/null 2>&1
f480386f 399 rm -f /var/ipfire/red/{active,device,dial-on-demand,dns1,dns2,local-ipaddress,remote-ipaddress,resolv.conf}
6c33dc5c 400
1e4656cd
AF
401 ## Disable vnstat collection
402 /usr/bin/vnstat -u -i ${DEVICE} -r --disable > /dev/null 2>&1
6c33dc5c 403 exit 0;
d1e90efc 404 ;;
d1e90efc
MT
405esac
406
407# End