]> git.ipfire.org Git - ipfire-2.x.git/blame - config/udev/network-hotplug-bridges
suricata: Change midstream policy to "pass-flow"
[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
58d471a4
MT
44 # Compare if the mac address matches or if the name matches
45 if [ -r "/sys/class/net/${INTERFACE}/address" -a "$(</sys/class/net/${INTERFACE}/address)" = "${slave}" ] || [ "${INTERFACE}" = "${slave}" ]; then
4aef53d5
JS
46 echo "${zone}"
47 return 0
48 fi
49 done
50 done
51
52 return 1
53}
54
55get_value() {
56 echo "${!1}"
57}
58
59random_mac_address() {
60 local address="02"
61
62 for i in $(seq 5); do
63 printf -v address "${address}:%02x" "$(( RANDOM % 256 ))"
64 done
65
66 echo "${address}"
67}
68
69# Try to detect which zone we are operating on
70ZONE=$(detect_zone)
71
72# Cannot proceed if we could not find a zone
73if [ -z "${ZONE}" ]; then
943eab66 74 logger "Could not find a bridged zone for ${INTERFACE}"
4aef53d5
JS
75 exit 0
76fi
77
78# Determine the mode of this zone
79MODE="$(get_value "${ZONE}_MODE")"
80
81# The name of the virtual bridge
82BRIDGE="$(get_value "${ZONE}_DEV")"
90c988a6 83MTU="$(get_value "${ZONE}_MTU")"
f8bf19c9 84STP="$(get_value "${ZONE}_STP")"
82973fae 85STP_PRIORITY="$(get_value "${ZONE}_STP_PRIORITY")"
4aef53d5
JS
86
87case "${MODE}" in
88 bridge)
90c988a6
MT
89 # Set default MTU if nothing is set
90 if [ -z "${MTU}" ]; then
91 MTU=1500
92 fi
93
82973fae 94 # We need to check if $STP_PRIORITY has a valid value if not set it
502f6c63
MT
95 if [ -z "${STP_PRIORITY}" ]; then
96 STP_PRIORITY=16384
82973fae
DW
97 fi
98
4aef53d5
JS
99 ADDRESS="$(get_value "${ZONE}_MACADDR")"
100 [ -n "${ADDRESS}" ] || ADDRESS="$(random_mac_address)"
101
102 # We need to create the bridge if it doesn't exist, yet
103 if [ ! -d "/sys/class/net/${BRIDGE}" ]; then
90c988a6 104 ip link add "${BRIDGE}" address "${ADDRESS}" mtu "${MTU}" type bridge \
82973fae 105 $([ "${STP}" = "on" ] && echo "stp_state 1 priority ${STP_PRIORITY}" )
4aef53d5
JS
106 #ip link set "${BRIDGE}" up
107 fi
108
fa4905ad
DW
109 # Try setting wireless interfaces into master mode
110 if [ -d "/sys/class/net/${INTERFACE}/phy80211" ]; then
b53d8ae9
MT
111 iw dev "${INTERFACE}" set type __ap
112 fi
113
90c988a6
MT
114 # Attempt to set the MTU
115 ip link set dev "${INTERFACE}" mtu "${MTU}"
116
4aef53d5 117 # Attach the physical device
943eab66 118 logger "Attach ${INTERFACE} to ${BRIDGE}"
4aef53d5
JS
119 ip link set dev "${INTERFACE}" master "${BRIDGE}"
120 ip link set dev "${INTERFACE}" up
7b616db4 121 ;;
4aef53d5 122
4aef53d5
JS
123 "")
124 exit 0
125 ;;
7b616db4 126
4aef53d5
JS
127 *)
128 logger -t "network" "Unhandled mode '${MODE}' for '${ZONE}' (${INTERFACE})"
129 exit 1
130 ;;
131esac