]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - src/initscripts/init.d/networking/red
465d528cc84ab634f2a02d9129791d428f22bb90
[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" ]; then
26 ADDRESS="${RED_ADDRESS}"
27 BROADCAST="${RED_BROADCAST}"
28 NETADDRESS="${RED_NETADDRESS}"
29 NETMASK="${RED_NETMASK}"
30 GATEWAY="${DEFAULT_GATEWAY}"
31 # DNS1
32 # DNS2
33
34 if [ -z "${BROADCAST}" ]; then
35 boot_mesg "BROADCAST variable missing from input, cannot continue." ${FAILURE}
36 echo_failure
37 exit 1
38 fi
39 if [ -n "${ADDRESS}" -a -n "${NETMASK}" ]; then
40 PREFIX=`whatmask ${NETMASK} | grep -e ^CIDR | awk -F': ' '{ print $2 }' | cut -c 2-`
41 args="${args} ${ADDRESS}/${PREFIX} broadcast ${BROADCAST}"
42 else
43 boot_mesg "ADDRESS and/or NETMASK variable missing from input, cannot continue." ${FAILURE}
44 echo_failure
45 exit 1
46 fi
47
48 elif [ "${TYPE}" == "DHCP" ]; then
49
50 PIDFILE="/var/run/dhcpcd-${DEVICE}.pid"
51 LEASEINFO="/var/ipfire/dhcpc/dhcpcd-${DEVICE}.info"
52 DHCP_START="-N -R -L /var/ipfire/dhcpc -c /var/ipfire/dhcpc/dhcpcd.exe "
53 DHCP_STOP="-k -c /var/ipfire/dhcpc/dhcpcd.exe "
54
55 fi
56
57 case "${1}" in
58 start)
59 boot_mesg "Bringing up the ${DEVICE} interface..."
60 boot_mesg_flush
61
62 # Check if an interface is there...
63 if ip link show ${DEVICE} > /dev/null 2>&1; then
64 link_status=`ip link show ${DEVICE} 2> /dev/null`
65 if [ -n "${link_status}" ]; then
66 if ! echo "${link_status}" | grep -q UP; then
67 ip link set ${DEVICE} up
68 fi
69 fi
70 else
71 boot_mesg "Interface ${DEVICE} doesn't exist." ${FAILURE}
72 echo_failure
73 exit 1
74 fi
75
76 if [ "${TYPE}" == "STATIC" ]; then
77 boot_mesg "Adding IPv4 address ${ADDRESS} to the ${DEVICE} interface..."
78 ip addr add ${args} dev ${DEVICE}
79 evaluate_retval
80 echo -n "${DEVICE}" > /var/ipfire/red/iface
81 echo -n "${ADDRESS}" > /var/ipfire/red/local-ipaddress
82 echo -n "${GATEWAY}" > /var/ipfire/red/remote-ipaddress
83 echo -n "${DNS1}" > /var/ipfire/red/dns1
84 echo -n "${DNS2}" > /var/ipfire/red/dns2
85
86 elif [ "${TYPE}" == "DHCP" ]; then
87 boot_mesg -n "Starting dhcpcd on the ${DEVICE} interface..."
88 echo -n "${DEVICE}" > /var/ipfire/red/iface
89
90 # Test to see if there is a stale pid file
91 if [ -f "$PIDFILE" ]; then
92 ps `cat "$PIDFILE"` | grep dhcpcd > /dev/null
93 if [ $? != 0 ]; then
94 rm -f /var/run/dhcpcd-${DEVICE}.pid > /dev/null
95 else
96 boot_mesg "dhcpcd already running!" ${WARNING}
97 echo_warning
98 exit 2
99 fi
100 fi
101
102 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i ${DEVICE} -j ACCEPT
103 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i ${DEVICE} -j ACCEPT
104
105 if [ -n "${DHCP_HOSTNAME}" ]; then
106 DHCP_START+="-h ${DHCP_HOSTNAME} "
107 fi
108
109 /sbin/dhcpcd ${DEVICE} ${DHCP_START} >/dev/null 2>&1
110 RET="$?"
111
112 if [ "$RET" = "0" ]; then
113 . /var/ipfire/dhcpc/dhcpcd-${DEVICE}.info
114 echo ""
115 echo_ok
116 boot_mesg " DHCP Assigned Settings for ${DEVICE}:"
117 boot_mesg_flush
118 boot_mesg " IP Address: $IPADDR"
119 boot_mesg_flush
120 if [ -n "${DHCP_HOSTNAME}" ]; then
121 boot_mesg " Hostname: $DHCP_HOSTNAME"
122 boot_mesg_flush
123 fi
124 boot_mesg " Subnet Mask: $NETMASK"
125 boot_mesg_flush
126 boot_mesg " Default Gateway: $GATEWAY"
127 boot_mesg_flush
128 boot_mesg " DNS Server: $DNS"
129 boot_mesg_flush
130
131 echo -n `/etc/rc.d/helper/getdnsfromdhcpc.pl 1` > /var/ipfire/red/dns1
132 echo -n `/etc/rc.d/helper/getdnsfromdhcpc.pl 2` > /var/ipfire/red/dns2
133
134 . /var/ipfire/dhcpc/dhcpcd-${DEVICE}.info
135 echo "$IPADDR" > /var/ipfire/red/local-ipaddress
136 echo "$GATEWAY" > /var/ipfire/red/remote-ipaddress
137 else
138 echo ""
139 $(exit "$RET")
140 evaluate_retval
141 fi
142
143 elif [ "$TYPE" == "PPPOE" ]; then
144 eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
145
146 boot_mesg "Bringing up the PPPoE interface on ${DEVICE}..."
147 ip addr add 1.1.1.1/24 broadcast 1.1.1.255 dev ${DEVICE}
148
149 PPPOECONF=/etc/ppp/pppoe.conf
150 echo > $PPPOECONF #clear
151
152 if [ "${METHOD}" != "PPPOE_PLUGIN" ]; then
153 echo "LINUX_PLUGIN=" >> $PPPOECONF
154 else
155 echo "LINUX_PLUGIN=/usr/lib/pppd/2.4.4/rp-pppoe.so" >> $PPPOECONF
156 fi
157
158 echo "ETH=${DEVICE}" >> $PPPOECONF
159 echo "USER=${USERNAME}" >> $PPPOECONF
160
161 echo "PPPOE_TIMEOUT=80" >> $PPPOECONF
162
163 #echo "PPPD_EXTRA=\"-U\"" >> $PPPOECONF
164 #echo "PPPOE_EXTRA=\"-m ${MTU}\"" >> $PPPOECONF
165
166 if [ -n ${SERVICENAME} ]; then
167 echo "SERVICENAME=${SERVICENAME}" >> $PPPOECONF
168 fi
169 if [ -n ${CONCENTRATORNAME} ]; then
170 echo "ACNAME=${CONCENTRATORNAME}" >> $PPPOECONF
171 fi
172
173 if [ "${DNS}" == "Automatic" ]; then
174 echo "PEERDNS=yes" >> $PPPOECONF
175 echo "DNSTYPE=NOCHANGE" >> $PPPOECONF
176 fi
177
178 if [ "${AUTH}" == "pap" ]; then
179 PPPD_EXTRA+="-chap "
180 elif [ "${AUTH}" == "chap" ]; then
181 PPPD_EXTRA+="-pap "
182 fi
183
184 echo "FIREWALL=NONE" >> $PPPOECONF
185
186 if [ "${RECONNECTION}" != "persistent" ]; then
187 if [ "${TIMEOUT}" != "0" ] && [ "${TIMEOUT}" != "" ]; then
188 SECONDS=$[${TIMEOUT} * 60]
189 fi
190 if [ "${RECONNECTION}" == "dialondemand" ]; then
191 touch /var/ipfire/red/dial-on-demand
192 echo "DEMAND=${SECONDS}" >> $PPPOECONF
193 fi
194 else
195 echo "DEMAND=no" >> $PPPOECONF
196 fi
197
198 echo "LCP_INTERVAL=20" >> $PPPOECONF
199 echo "LCP_FAILURE=3" >> $PPPOECONF
200
201 echo "PIDFILE=/var/run/ppp-ipfire.pid" >> $PPPOECONF
202
203 #ARGS+=" maxfail ${MAXRETRIES}"
204
205 [ "${METHOD}" != "PPPOE_PLUGIN" ] && [ "${DEBUG}" == "on" ] && export DEBUG=1
206 /usr/sbin/pppoe-start
207 evaluate_retval
208 fi
209
210 for i in $( ls -v ${rc_base}/init.d/networking/red.up/* 2> /dev/null); do
211 check_script_status
212 OUT=$(echo $(basename ${i}) | awk -F- '{ print $2 }')
213 case "$OUT" in
214 S) ${i} start ;;
215 K) ${i} stop ;;
216 RS) ${i} restart ;;
217 RL) ${i} reload ;;
218 *) ${i} ;;
219 esac
220 done
221 ;;
222
223 stop)
224 if [ "$TYPE" == "STATIC" ]; then
225 boot_mesg "Removing IPv4 address ${ADDRESS} from the ${DEVICE} interface..."
226 ip addr del ${args} dev ${DEVICE}
227 evaluate_retval
228
229 elif [ "$TYPE" == "DHCP" ]; then
230 boot_mesg -n "Stopping dhcpcd on the ${DEVICE} interface..."
231 if [ -e $LEASEINFO ]; then
232 . $LEASEINFO
233 if [ "$LEASETIME" = "4294967295" ]; then
234 # do nothing, just echo ok
235 echo ""
236 echo_ok
237 else
238 if [ -n "$DHCP_STOP" ]; then
239 /sbin/dhcpcd ${DEVICE} $DHCP_STOP &> /dev/null
240 RET="$?"
241 if [ "$RET" -eq 0 ]; then
242 echo ""
243 echo_ok
244 elif [ "$RET" -eq 1 ]; then
245 boot_mesg "dhcpcd not running!" ${WARNING}
246 echo_warning
247 else
248 echo ""
249 echo_failure
250 fi
251 else
252 echo ""
253 killproc dhcpcd
254 fi
255 fi
256 else
257 boot_mesg -n "LEASEINFO Test failed! - " ${WARNING}
258 boot_mesg "dhcpcd is not running!" ${WARNING}
259 echo_warning
260 exit 1
261 fi
262
263 elif [ "$TYPE" == "PPPOE" ]; then
264 boot_mesg "Bringing down the PPPoE interface on ${DEVICE}..."
265
266 /usr/sbin/pppoe-stop
267 evaluate_retval
268 ip addr del 1.1.1.1/24 broadcast 1.1.1.255 dev ${DEVICE}
269
270 fi
271
272 link_status=`ip link show $DEVICE 2> /dev/null`
273 if [ -n "${link_status}" ]; then
274 if echo "${link_status}" | grep -q UP; then
275 boot_mesg "Bringing down the ${DEVICE} interface..."
276 ip link set ${DEVICE} down
277 evaluate_retval
278 fi
279 fi
280
281 rm -f /var/ipfire/red/{active,device,dial-on-demand,dns1,dns2,local-ipaddress,remote-ipaddress,resolv.conf}
282 for i in $( ls -v ${rc_base}/init.d/networking/red.down/* 2> /dev/null); do
283 check_script_status
284 OUT=$(echo $(basename ${i}) | awk -F- '{ print $2 }')
285 case "$OUT" in
286 S) ${i} start ;;
287 K) ${i} stop ;;
288 RS) ${i} restart ;;
289 RL) ${i} reload ;;
290 *) ${i} ;;
291 esac
292 done
293 ;;
294
295 esac
296
297 # End