]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - config/udev/network-hotplug-bridges
Core 152: the script "network-hotplug-bridges" now reads the variable ${ZONE}_STP...
[people/pmueller/ipfire-2.x.git] / config / udev / network-hotplug-bridges
CommitLineData
7b616db4
JS
1#!/bin/bash
2############################################################################
3# #
4# This file is part of the IPFire Firewall. #
5# #
6# IPFire is free software; you can redistribute it and/or modify #
7# it under the terms of the GNU General Public License as published by #
8# the Free Software Foundation; either version 2 of the License, or #
9# (at your option) any later version. #
10# #
11# IPFire is distributed in the hope that it will be useful, #
12# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14# GNU General Public License for more details. #
15# #
16# You should have received a copy of the GNU General Public License #
17# along with IPFire; if not, write to the Free Software #
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
19# #
20# Copyright (C) 2016 IPFire Team <info@ipfire.org> #
21# #
22############################################################################
23
24[ -n "${INTERFACE}" ] || exit 2
25
4aef53d5 26eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
7b616db4 27
4aef53d5 28detect_zone() {
593de24f
JS
29 local intf="${INTERFACE%?}"
30 intf="${intf%phys}"
4aef53d5
JS
31 intf="${intf^^}"
32
33 local zone
34 for zone in GREEN BLUE ORANGE RED; do
35 # Try to find if INTERFACE is the *phys version of a zone
36 if [ "${intf}" = "${zone}" ]; then
37 echo "${zone}"
38 return 0
39 fi
40
41 # Try to find out if this INTERFACE is a slave of a zone
42 local slave
43 for slave in $(get_value "${zone}_SLAVES"); do
c2336f6b
JS
44
45 #Compare if the mac address matches or if the name matches
46 if ([ "$(</sys/class/net/${INTERFACE}/address)" = "${slave}" ] || [ "${INTERFACE}" = "${slave}" ]); then
4aef53d5
JS
47 echo "${zone}"
48 return 0
49 fi
50 done
51 done
52
53 return 1
54}
55
56get_value() {
57 echo "${!1}"
58}
59
60random_mac_address() {
61 local address="02"
62
63 for i in $(seq 5); do
64 printf -v address "${address}:%02x" "$(( RANDOM % 256 ))"
65 done
66
67 echo "${address}"
68}
69
70# Try to detect which zone we are operating on
71ZONE=$(detect_zone)
72
73# Cannot proceed if we could not find a zone
74if [ -z "${ZONE}" ]; then
943eab66 75 logger "Could not find a bridged zone for ${INTERFACE}"
4aef53d5
JS
76 exit 0
77fi
78
79# Determine the mode of this zone
80MODE="$(get_value "${ZONE}_MODE")"
81
82# The name of the virtual bridge
83BRIDGE="$(get_value "${ZONE}_DEV")"
f8bf19c9 84STP="$(get_value "${ZONE}_STP")"
4aef53d5
JS
85
86case "${MODE}" in
87 bridge)
88 ADDRESS="$(get_value "${ZONE}_MACADDR")"
89 [ -n "${ADDRESS}" ] || ADDRESS="$(random_mac_address)"
90
91 # We need to create the bridge if it doesn't exist, yet
92 if [ ! -d "/sys/class/net/${BRIDGE}" ]; then
f8bf19c9
DW
93 ip link add "${BRIDGE}" address "${ADDRESS}" type bridge \
94 $([ "${STP}" = "on" ] && echo "stp_state 1")
4aef53d5
JS
95 #ip link set "${BRIDGE}" up
96 fi
97
b53d8ae9
MT
98 if grep -q "INTERFACE=${INTERFACE}" "/var/ipfire/wlanap/settings" 2>/dev/null; then
99 iw dev "${INTERFACE}" set type __ap
100 fi
101
4aef53d5 102 # Attach the physical device
943eab66 103 logger "Attach ${INTERFACE} to ${BRIDGE}"
4aef53d5
JS
104 ip link set dev "${INTERFACE}" master "${BRIDGE}"
105 ip link set dev "${INTERFACE}" up
7b616db4 106 ;;
4aef53d5
JS
107
108 macvtap)
109 ADDRESS="$(</sys/class/net/${INTERFACE}/address)"
110 GENERATED_ADDRESS=$(random_mac_address)
111
112 ip link add link "${INTERFACE}" "${BRIDGE}" address "${ADDRESS}" type macvlan mode bridge
113 ip link set "${INTERFACE}" address "${GENERATED_ADDRESS}"
114 ip link set "${INTERFACE}" up
7b616db4 115 ;;
7b616db4 116
4aef53d5
JS
117 "")
118 exit 0
119 ;;
7b616db4 120
4aef53d5
JS
121 *)
122 logger -t "network" "Unhandled mode '${MODE}' for '${ZONE}' (${INTERFACE})"
123 exit 1
124 ;;
125esac