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