]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/initscripts/networking/red
f927ddbfb42c617a6e99a2e375856f17c6d63163
[people/pmueller/ipfire-2.x.git] / src / initscripts / networking / red
1 #!/bin/sh
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 . /etc/sysconfig/rc
23 . ${rc_functions}
24 . /etc/init.d/networking/functions.network
25
26 #Define some defaults
27 INET_VLAN=7
28 IPTV_VLAN=8
29 ATM_DEV=0
30
31 eval $(/usr/local/bin/readhash /var/ipfire/main/settings)
32 if [ "$RRDLOG" == "" ]; then
33 RRDLOG=/var/log/rrd
34 fi
35
36 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
37 eval $(/usr/local/bin/readhash /var/ipfire/dns/settings)
38 eval $(/usr/local/bin/readhash /var/ipfire/mac/settings)
39
40 MAC=$(tr - : <<<$MAC)
41 MAC1=$(tr - : <<<$MAC1)
42 MAC2=$(tr - : <<<$MAC2)
43
44 TYPE="${RED_TYPE}"
45 DEVICE="${RED_DEV}"
46
47 if [ "$TYPE" == "STATIC" ] || [ "$TYPE" == "DHCP" ]; then
48 if [ "$DEVICE" == "" ]; then
49 boot_mesg "No device for red network. Please run setup." ${FAILURE}
50 echo_failure
51 [ "${1}" == "start" ] && exit 0
52 fi
53 fi
54
55 if [ "${TYPE}" == "STATIC" ]; then
56 if [ "${DEVICE}" != "${GREEN_DEV}" ]; then
57 ADDRESS="${RED_ADDRESS}"
58 NETADDRESS="${RED_NETADDRESS}"
59 NETMASK="${RED_NETMASK}"
60 MTU="${RED_MTU}"
61 else
62 ADDRESS="${GREEN_ADDRESS}"
63 NETADDRESS="${GREEN_NETADDRESS}"
64 NETMASK="${GREEN_NETMASK}"
65 MTU="${GREEN_MTU}"
66 fi
67 GATEWAY="${DEFAULT_GATEWAY}"
68 # DNS1
69 # DNS2
70
71 if [ -n "${ADDRESS}" -a -n "${NETMASK}" ]; then
72 PREFIX=`whatmask ${NETMASK} | grep -e ^CIDR | awk -F': ' '{ print $2 }' | cut -c 2-`
73 args="${args} ${ADDRESS}/${PREFIX}"
74 else
75 boot_mesg "ADDRESS and/or NETMASK variable missing from input, cannot continue." ${FAILURE}
76 echo_failure
77 exit 1
78 fi
79 fi
80
81 case "${1}" in
82 start)
83 if [ "${DEVICE}" != "${GREEN_DEV}" ] && [ "${DEVICE}" != "" ]; then
84 boot_mesg "Bringing up the ${DEVICE} interface..."
85 boot_mesg_flush
86 # Check if an interface is there...
87 if ip link show ${DEVICE} > /dev/null 2>&1; then
88 link_status=`ip link show ${DEVICE} 2> /dev/null`
89 if [ -n "${link_status}" ]; then
90 if ! echo "${link_status}" | grep -q UP; then
91 if [ -n "$MAC" ]; then
92 boot_mesg "Setting mac address on ${DEVICE} to ${MAC}"
93 ip link set dev ${DEVICE} address ${MAC}
94 evaluate_retval
95 fi
96 ip link set ${DEVICE} up
97 fi
98 fi
99 else
100 boot_mesg "Interface ${DEVICE} doesn't exist." ${FAILURE}
101 echo_failure
102 exit 1
103 fi
104 fi
105
106 if [ "${TYPE}" == "STATIC" ]; then
107 # Set the MTU
108 if [ -n "${MTU}" ]; then
109 if ! ip link set dev "${DEVICE}" mtu "${MTU}" &>/dev/null; then
110 boot_mesg "Could not set MTU of ${MTU} to ${DEVICE}..."
111 echo_warning
112 fi
113 fi
114
115 if [ "$DEVICE" != "${GREEN_DEV}" ]; then
116 boot_mesg "Adding IPv4 address ${ADDRESS} to the ${DEVICE} interface..."
117 ip addr add ${args} dev ${DEVICE}
118 evaluate_retval
119 fi
120 echo -n "${DEVICE}" > /var/ipfire/red/iface
121 echo -n "${ADDRESS}" > /var/ipfire/red/local-ipaddress
122 echo -n "${GATEWAY}" > /var/ipfire/red/remote-ipaddress
123 grep -v -E "\<gateway\>" /etc/hosts > /tmp/hosts
124 echo "$GATEWAY gateway" >> /tmp/hosts
125 mv /tmp/hosts /etc/hosts
126 touch /var/ipfire/red/active
127
128 # Create route to default gateway
129 ip route add ${GATEWAY} dev ${DEVICE}
130
131 boot_mesg "Setting up default gateway ${GATEWAY}..."
132 ip route add default via ${GATEWAY} dev ${DEVICE}
133 evaluate_retval
134
135 if [ -d "/sys/class/net/${DEVICE}" ]; then
136 # has carrier ?
137 if [ ! "$(</sys/class/net/${DEVICE}/carrier)" = "1" ]; then
138 boot_mesg -n "Wait for carrier on ${DEVICE} "
139 for (( i=30; i>1; i-- )) do
140 if [ "$(</sys/class/net/${DEVICE}/carrier)" = "1" ]; then
141 break;
142 fi
143 boot_mesg -n "."
144 sleep 2
145 done
146 boot_mesg ""
147 if [ ! "$(</sys/class/net/${DEVICE}/carrier)" = "1" ]; then
148 echo_failure
149 else
150 echo_ok
151 fi
152 fi
153 fi
154
155 run_subdir ${rc_base}/init.d/networking/red.up/
156
157 # Configure aliases only if red static
158 /usr/local/bin/setaliases
159
160 elif [ "${TYPE}" == "DHCP" ]; then
161 # Add firewall rules to allow comunication with the dhcp server on red.
162 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i ${DEVICE} -j ACCEPT
163 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i ${DEVICE} -j ACCEPT
164
165 echo -n "${DEVICE}" > /var/ipfire/red/iface
166
167 # Check if the wlan-client is used on red.
168 # To determine this we check if a wpa_supplicant is running.
169 pid="$(pidof wpa_supplicant)"
170
171 if [ -z "${pid}" ]; then
172 # No wpa_supplicant is running. So it's save to start dhcpcd.
173 dhcpcd_start "${DEVICE}"
174 fi
175
176 elif [ "$TYPE" == "PPPOE" ]; then
177
178 if ( ps ax | grep -q [p]ppd ); then
179 boot_mesg "pppd is still running." ${FAILURE}
180 echo_failure
181 exit 1
182 fi
183
184 eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
185
186 [ -c "/dev/ppp" ] || mknod /dev/ppp c 108 0
187
188 # We force the plugin method, anyway.
189 METHOD="PPPOE_PLUGIN"
190
191 PPP_NIC=${DEVICE}
192
193 if [ "$TYPE" == "pppoeatm" ] || [ "$TYPE" == "pptpatm" ]; then
194 PPP_NIC=nas${ATM_DEV}
195 DEVICE=nas${ATM_DEV}
196 boot_mesg "Creating ATM-Bridge as $PPP_NIC ..."
197 br2684ctl -c${ATM_DEV} -e${ENCAP} -a${ATM_DEV}.${VPI}.${VCI} >/dev/null 2>&1 &
198 sleep 1
199
200 # use user-defined or green mac address for nas0
201 if [ -n "$MAC" ]; then
202 ip link set dev nas${ATM_DEV} address ${MAC}
203 else
204 ip link set dev nas${ATM_DEV} address $(cat /sys/class/net/green0/address)
205 fi
206
207 if [ "$TYPE" == "pppoeatm" ]; then
208 TYPE="pppoe"
209 fi
210 if [ "$TYPE" == "pptpatm" ]; then
211 TYPE="pptp"
212 fi
213
214 # QMI
215 elif [ "$TYPE" = "qmi" ]; then
216 DEVICE="$(qmi_find_device "${RED_DEV}")"
217
218 boot_mesg "Bringing up QMI on ${RED_DEV} (${DEVICE})..."
219
220 # Enable RAW-IP mode
221 qmi_enable_rawip_mode "${RED_DEV}"
222
223 # Configure APN
224 qmi_configure_apn "${DEVICE}" "${APN}" "${AUTH}" "${USERNAME}" "${PASSWORD}"
225
226 # Set up the interface
227 ip link set "${RED_DEV}" up &>/dev/null
228
229 # Start the DHCP client
230 dhcpcd_start "${RED_DEV}" --dhcp
231
232 # Done
233 exit 0
234 fi
235
236 if [ "$TYPE" == "vdsl" ]; then
237 boot_mesg "Creating VLAN Interface ${DEVICE}.${INET_VLAN} ..."
238 modprobe 8021q
239 vconfig add ${DEVICE} ${INET_VLAN}
240 if [ -n "$MAC1" ]; then
241 boot_mesg "Setting mac address on ${DEVICE}.${INET_VLAN} to ${MAC1}"
242 ip link set dev ${DEVICE}.${INET_VLAN} address ${MAC1}
243 evaluate_retval
244 fi
245 PPP_NIC=${DEVICE}.${INET_VLAN}
246 sleep 0.2
247 ip link set ${PPP_NIC} up
248 TYPE="pppoe"
249 fi
250 if [ "${IPTV}" == "enable" ]; then
251 PIDFILE="/var/run/dhcpcd/${DEVICE}.${IPTV_VLAN}.pid"
252 LEASEINFO="/var/ipfire/dhcpc/dhcpcd-${DEVICE}.${IPTV_VLAN}.info"
253 # Test to see if there is a stale pid file
254 if [ -f "$PIDFILE" ]; then
255 ps `cat "$PIDFILE"` | grep dhcpcd > /dev/null
256 if [ $? != 0 ]; then
257 rm -f /var/run/dhcpcd/${DEVICE}.${IPTV_VLAN}.pid > /dev/null
258 fi
259 fi
260
261 if [ ! -f "$PIDFILE" ]; then
262 boot_mesg "Creating VLAN Interface ${DEVICE}.${IPTV_VLAN} ..."
263 modprobe 8021q
264 vconfig add ${DEVICE} ${IPTV_VLAN}
265 if [ -n "$MAC2" ]; then
266 boot_mesg "Setting mac address on ${DEVICE}.${IPTV_VLAN} to ${MAC2}"
267 ip link set dev ${DEVICE}.${IPTV_VLAN} address ${MAC2}
268 evaluate_retval
269 fi
270 boot_mesg -n "Starting dhcpcd on the ${DEVICE}.${IPTV_VLAN} interface..."
271 /sbin/dhcpcd ${DEVICE}.${IPTV_VLAN} ${DHCP_START} >/dev/null 2>&1
272 RET="$?"
273
274 if [ "$RET" = "0" ]; then
275 . /var/ipfire/dhcpc/dhcpcd-${DEVICE}.${IPTV_VLAN}.info
276 echo ""
277 echo_ok
278 boot_mesg " DHCP Assigned Settings for ${DEVICE}.${IPTV_VLAN}:"
279 boot_mesg_flush
280 boot_mesg " IP Address: $ip_address"
281 boot_mesg_flush
282 boot_mesg " Hostname: $RED_DHCP_HOSTNAME"
283 boot_mesg_flush
284 boot_mesg " Subnet Mask: $subnet_mask"
285 boot_mesg_flush
286 boot_mesg " Default Gateway: $routers"
287 boot_mesg_flush
288 boot_mesg " DNS Server: $domain_name_servers"
289 boot_mesg_flush
290
291 else
292 echo ""
293 $(exit "$RET")
294 evaluate_retval
295 fi
296 fi
297 fi
298 if [ "$TYPE" == "pppoe" ] || [ "$TYPE" == "pptp" ]; then
299 if [ "$PPP_NIC" == "" ]; then
300 boot_mesg "No device for red interface given. Check netsetup or dialprofile!" ${FAILURE}
301 echo_failure
302 exit 0
303 fi
304 boot_mesg "Bringing up the $TYPE interface on $PPP_NIC ..."
305 ip addr flush dev $PPP_NIC >/dev/null 2>&1
306 if [ "$TYPE" == "pptp" ]; then
307 if [ "$PPTP_NICCFG" == "dhcp" ]; then
308 # Test to see if there is a stale pid file
309 if [ -f "$PIDFILE" ]; then
310 ps `cat "$PIDFILE"` | grep dhcpcd > /dev/null
311 if [ $? != 0 ]; then
312 rm -f /var/run/dhcpcd/${DEVICE}.pid > /dev/null
313 fi
314 fi
315
316 if [ ! -f "$PIDFILE" ]; then
317 boot_mesg -n "Starting dhcpcd on the ${DEVICE} interface..."
318 /sbin/dhcpcd ${DEVICE} ${DHCP_START} >/dev/null 2>&1
319 RET="$?"
320
321 if [ "$RET" = "0" ]; then
322 . /var/ipfire/dhcpc/dhcpcd-${DEVICE}.info
323 echo ""
324 echo_ok
325 boot_mesg " DHCP Assigned Settings for ${DEVICE}:"
326 boot_mesg_flush
327 boot_mesg " IP Address: $ip_address"
328 boot_mesg_flush
329 boot_mesg " Hostname: $RED_DHCP_HOSTNAME"
330 boot_mesg_flush
331 boot_mesg " Subnet Mask: $subnet_mask"
332 boot_mesg_flush
333 boot_mesg " Default Gateway: $routers"
334 boot_mesg_flush
335 boot_mesg " DNS Server: $domain_name_servers"
336 boot_mesg_flush
337 /sbin/route add $PPTP_PEER gw $routers $PPP_NIC
338 else
339 echo ""
340 $(exit "$RET")
341 evaluate_retval
342 fi
343 fi
344 else
345 ip addr add $PPTP_NICCFG dev $PPP_NIC
346 fi
347 fi
348 ip link set ${PPP_NIC} up
349 if [ -n "${PPTP_ROUTE}" ]; then
350 boot_mesg "Set route ${PPTP_ROUTE} to pptp server..."
351 route add ${PPTP_ROUTE}
352 fi
353 else
354 boot_mesg "Bringing up the PPP via ${TYPE} on ${COMPORT}..."
355 fi
356
357 ### ###
358 ### Configuring the pppd ###
359 ### ###
360
361 ### Plugin Options
362 #
363 if [ "$TYPE" == "pppoe" ]; then
364 [ "${METHOD}" == "PPPOE_PLUGIN" ] && \
365 PLUGOPTS="plugin rp-pppoe.so"
366 fi
367
368 ### Synchronous Mode
369 #
370 #PPPOE_SYNC=-s
371 #PPPD_SYNC=sync
372
373 ### Access Concentrator Name
374 #
375 if [ -n "${CONCENTRATORNAME}" ]; then
376 ACNAME="-C ${CONCENTRATORNAME}"
377 fi
378
379 ### Service Name
380 #
381 if [ -n "${SERVICENAME}" ]; then
382 if [ "${METHOD}" == "PPPOE_PLUGIN" ]; then
383 PLUGOPTS+=" rp_pppoe_service ${SERVICENAME}"
384 else
385 SERVICENAME="-S ${SERVICENAME}"
386 fi
387 fi
388
389 ### Authentication Types
390 #
391 if [ "${AUTH}" == "pap" ]; then
392 AUTH="-chap"
393 elif [ "${AUTH}" == "chap" ]; then
394 AUTH="-pap"
395 else
396 AUTH=""
397 fi
398
399 ### Dial On Demand
400 #
401 if [ "${RECONNECTION}" != "persistent" ]; then
402 if [ "${TIMEOUT}" != "0" ] && [ "${TIMEOUT}" != "" ]; then
403 SECONDS=$[${TIMEOUT} * 60]
404 else
405 SECONDS=300
406 fi
407 if [ "${RECONNECTION}" == "dialondemand" ]; then
408 touch /var/ipfire/red/dial-on-demand
409 DEMAND="demand persist idle ${SECONDS} 10.112.112.112:10.112.112.113"
410 DEMAND+=" ipcp-accept-remote ipcp-accept-local noipdefault ktune"
411 fi
412 fi
413
414 if [ "$TYPE" == "pppoe" ]; then
415 ### When using pppoe-plugin the device has to be the last option
416 #
417 [ "${METHOD}" == "PPPOE_PLUGIN" ] && PLUGOPTS+=" $PPP_NIC"
418 fi
419
420 if [ "$TYPE" == "modem" ]; then
421 PLUGOPTS=" /dev/${COMPORT} ${DTERATE} connect /etc/ppp/dialer lock modem crtscts"
422 METHOD="PPPOE_PLUGIN"
423 elif [ "$TYPE" == "serial" ]; then
424 PLUGOPTS=" /dev/${COMPORT} ${DTERATE} connect /bin/true lock modem crtscts"
425 METHOD="PPPOE_PLUGIN"
426 fi
427
428 ### Standard PPP options we always use
429 #
430 PPP_STD_OPTIONS="$PLUGOPTS usepeerdns defaultroute noipdefault noauth"
431 PPP_STD_OPTIONS+=" default-asyncmap hide-password nodetach noipv6"
432 PPP_STD_OPTIONS+=" noaccomp nodeflate nopcomp novj novjccomp"
433 PPP_STD_OPTIONS+=" nobsdcomp user ${USERNAME} lcp-echo-interval 20"
434 PPP_STD_OPTIONS+=" lcp-echo-failure 5 ${AUTH}"
435
436 if [ -n "${MTU}" ]; then
437 PPP_STD_OPTIONS="${PPP_STD_OPTIONS} mtu ${MTU}"
438 fi
439
440 if [ -n "${MRU}" ]; then
441 PPP_STD_OPTIONS="${PPP_STD_OPTIONS} mru ${MRU}"
442 fi
443
444 ### Debugging
445 #
446 if [ "${DEBUG}" == "on" ]; then
447 DEBUG="debug"
448 else
449 DEBUG=""
450 fi
451
452 ### PPPoE invocation
453 #
454 if [ "$TYPE" == "pppoe" ]; then
455 PPPOE_CMD="/usr/sbin/pppoe -p /var/run/ppp-ipfire.pid.pppoe -I $PPP_NIC"
456 PPPOE_CMD+=" -T 80 -U $PPPOE_SYNC $ACNAME $SERVICENAMEOPT"
457 fi
458
459 ### PPTP ###
460 #
461 if [ "$TYPE" == "pptp" ]; then
462 PPPOE_CMD="pptp $PPTP_PEER --nolaunchpppd"
463 METHOD=""
464 fi
465
466 ### Run everything
467 #
468 if [ "$METHOD" == "PPPOE_PLUGIN" ]; then
469 /usr/sbin/pppd $PPP_STD_OPTIONS $DEBUG $DEMAND >/dev/null 2>&1 &
470 evaluate_retval
471 # echo PLUGIN: /usr/sbin/pppd $PPP_STD_OPTIONS $DEBUG $DEMAND
472 else
473 /usr/sbin/pppd pty "$PPPOE_CMD" $PPP_STD_OPTIONS $DEBUG $DEMAND $PPPD_SYNC >/dev/null 2>&1 &
474 evaluate_retval
475 # echo PPP: /usr/sbin/pppd pty "$PPPOE_CMD" $PPP_STD_OPTIONS $DEBUG $DEMAND $PPPD_SYNC
476 fi
477
478 /etc/rc.d/init.d/connectd start
479 # Add a NaN value to ppp0 rrd to supress spikes at reconnect
480 rrdtool update $RRDLOG/collectd/localhost/interface/if_octets-ppp0.rrd \
481 $(date +%s):: > /dev/null 2>&1
482 exit 0
483 fi
484 ;;
485
486 stop)
487 rm -f /var/ipfire/red/{active,device,dial-on-demand,dns1,dns2,local-ipaddress,remote-ipaddress,resolv.conf}
488
489 if [ "$TYPE" == "STATIC" ]; then
490 boot_mesg "Stopping default gateway ${GATEWAY}..."
491 ip route del default via ${GATEWAY} >/dev/null 2>&1
492 echo_ok
493 if [ "$DEVICE" != "${GREEN_DEV}" ]; then
494 boot_mesg "Removing IPv4 addresses from the ${DEVICE} interface..."
495 ip addr flush dev ${DEVICE}
496 evaluate_retval
497 fi
498 run_subdir ${rc_base}/init.d/networking/red.down/
499
500 elif [ "$TYPE" == "PPPOE" ]; then
501 eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
502
503 if [ "${TYPE}" = "qmi" ]; then
504 boot_mesg "Bringing down the QMI interface ${RED_DEV}..."
505 DEVICE="$(qmi_find_device "${RED_DEV}")"
506
507 # Stop the DHCP client on RED
508 dhcpcd_stop "${RED_DEV}"
509
510 # Reset any QMI settings
511 qmi_reset "${DEVICE}"
512
513 exit 0
514 fi
515
516 boot_mesg "Bringing down the PPP interface ..."
517 rm -f /var/ipfire/red/keepconnected
518 killall -w -s TERM /usr/sbin/pppd 2>/dev/null
519 evaluate_retval
520 # Add a NaN value to ppp0 rrd to supress spikes at reconnect
521 rrdtool update $RRDLOG/collectd/localhost/interface/if_octets-ppp0.rrd \
522 $(date +%s):: > /dev/null 2>&1
523
524 elif [ "$TYPE" == "DHCP" ]; then
525 # Check if the wlan-client is used on red.
526 # To determine this we check if a wpa_supplicant is running.
527 pid="$(pidof wpa_supplicant)"
528
529 if [ -z "${pid}" ]; then
530 # Stop dhcpcd.
531 dhcpcd_stop "${DEVICE}"
532 fi
533 fi
534
535 if [ -n "${PPTP_ROUTE}" ]; then
536 route del ${PPTP_ROUTE}
537 fi
538
539 if [ "$DEVICE" != "${GREEN_DEV}" ] && [ "$DEVICE" != "" ]; then
540 link_status=`ip link show $DEVICE.${INET_VLAN} 2> /dev/null`
541 if [ -n "${link_status}" ]; then
542 if echo "${link_status}" | grep -q UP; then
543 boot_mesg "Bringing down the ${DEVICE}.${INET_VLAN} interface..."
544 ip link set ${DEVICE}.${INET_VLAN} down
545 vconfig rem ${DEVICE}.${INET_VLAN}
546 evaluate_retval
547 fi
548 else
549 link_status=`ip link show $DEVICE 2> /dev/null`
550 if [ -n "${link_status}" ]; then
551 if echo "${link_status}" | grep -q UP; then
552 boot_mesg "Bringing down the ${DEVICE} interface..."
553 ip link set ${DEVICE} down
554 evaluate_retval
555 fi
556 fi
557 fi
558 fi
559 killall -w -s KILL /usr/sbin/pppd >/dev/null 2>&1
560 killall -w -s KILL pptp >/dev/null 2>&1
561 killall -w -s KILL br2684ctl >/dev/null 2>&1
562
563 exit 0;
564 ;;
565 esac