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