]>
Commit | Line | Data |
---|---|---|
63ef8328 MT |
1 | #!/bin/sh |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2009 Michael Tremer & Christian Schmidt # | |
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 | ||
ae69ea7e MT |
22 | CONFIG_DIR=/etc/sysconfig/networking |
23 | ||
24 | CONFIG_ZONES=${CONFIG_DIR}/zones | |
25 | CONFIG_PORTS=${CONFIG_DIR}/ports | |
b5238f57 | 26 | |
cd1bc684 MT |
27 | COMMON_DEVICE=black+ |
28 | ||
b5238f57 | 29 | function is_mac() { |
ae69ea7e | 30 | egrep -q "^[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]$" <<<$1 |
b5238f57 | 31 | } |
63ef8328 MT |
32 | |
33 | function get_device_by_mac() { | |
ae69ea7e | 34 | local mac |
4bded844 | 35 | local device |
ae69ea7e MT |
36 | |
37 | mac=$1 | |
38 | ||
4bded844 MT |
39 | for device in /sys/class/net/*; do |
40 | if [ "$(cat $device/address)" = "$mac" ]; then | |
41 | device=${device##*/} | |
42 | # Skip virtual devices | |
43 | if [ -e "/proc/net/vlan/$device" ]; then | |
44 | continue | |
45 | fi | |
46 | # Skip zones | |
47 | if zone_exists ${device}; then | |
48 | continue | |
49 | fi | |
50 | echo ${device} | |
51 | return 0 | |
63ef8328 MT |
52 | fi |
53 | done | |
4bded844 | 54 | return 1 |
63ef8328 MT |
55 | } |
56 | ||
ae69ea7e MT |
57 | function get_device_by_mac_and_vid() { |
58 | local mac | |
59 | local vid | |
60 | ||
61 | mac=$1 | |
62 | vid=$2 | |
63 | ||
64 | local i | |
65 | local VID | |
66 | local DEVICE | |
4bded844 MT |
67 | if [ -e "/proc/net/vlan/config" ]; then |
68 | grep '|' /proc/net/vlan/config | sed "s/|//g" | \ | |
69 | while read DEVICE VID PARENT; do | |
70 | if [ "${vid}" = "${VID}" ] && [ "$(macify ${PARENT})" = "${mac}" ]; then | |
71 | echo "${DEVICE}" | |
72 | return 0 | |
73 | fi | |
74 | done | |
75 | fi | |
ae69ea7e MT |
76 | return 1 |
77 | } | |
78 | ||
90af6f24 MT |
79 | function get_device() { |
80 | if [ ${#@} -gt 1 ]; then | |
81 | get_device_by_mac_and_vid $@ | |
82 | else | |
83 | get_device_by_mac $@ | |
84 | fi | |
85 | } | |
86 | ||
63ef8328 | 87 | function get_mac_by_device() { |
ae69ea7e MT |
88 | local device |
89 | device=$1 | |
63ef8328 MT |
90 | if [ -d "/sys/class/net/$device" ]; then |
91 | cat /sys/class/net/$device/address | |
92 | return 0 | |
93 | fi | |
94 | return 1 | |
95 | } | |
b5238f57 | 96 | |
90af6f24 MT |
97 | function get_mac() { |
98 | get_mac_by_device $@ | |
99 | } | |
100 | ||
ae69ea7e MT |
101 | function devicify() { |
102 | local device | |
103 | local mac | |
104 | ||
105 | device=$1 | |
106 | ||
107 | if is_mac ${device}; then | |
108 | mac=${device} | |
109 | device=$(get_device_by_mac ${device}) | |
110 | fi | |
4bded844 MT |
111 | if [ -n "${device}" ]; then |
112 | echo ${device} | |
113 | return 0 | |
114 | else | |
115 | echo "devicify: Could not find device of $@" >&2 | |
116 | return 1 | |
117 | fi | |
ae69ea7e MT |
118 | } |
119 | ||
120 | function macify() { | |
121 | local input | |
122 | local mac | |
123 | ||
124 | input=$1 | |
125 | ||
126 | if is_mac ${input}; then | |
127 | mac=${input} | |
128 | else | |
129 | mac=$(get_mac_by_device ${input}) | |
130 | fi | |
131 | echo ${mac} | |
132 | } | |
133 | ||
b5238f57 | 134 | function device_exists() { |
ae69ea7e MT |
135 | ip link show $(devicify ${1}) &>/dev/null |
136 | } | |
137 | ||
cd1bc684 MT |
138 | function device_is_up() { |
139 | ip link show $(devicify ${1}) 2>/dev/null | grep -qE "<.*UP.*>" | |
140 | } | |
141 | ||
ae69ea7e MT |
142 | function rename_device() { |
143 | local source | |
144 | local destination | |
145 | ||
146 | source=$1 | |
147 | destination=$2 | |
148 | ||
4bded844 MT |
149 | # Replace + by a valid number |
150 | if grep -q "+$" <<<${destination}; then | |
151 | local number | |
152 | destination=$(sed -e "s/+//" <<<$destination) | |
153 | number=0 | |
154 | while :; do | |
155 | if ! device_exists "${destination}${number}"; then | |
156 | destination="${destination}${number}" | |
157 | break | |
158 | fi | |
159 | number=$(($number + 1)) | |
160 | done | |
161 | fi | |
162 | ||
ae69ea7e MT |
163 | # Check if devices exist |
164 | if ! device_exists ${source} || device_exists ${destination}; then | |
165 | return 4 | |
166 | fi | |
167 | ||
4bded844 | 168 | ip link set ${source} down |
ae69ea7e | 169 | ip link set ${source} name ${destination} |
4bded844 | 170 | ip link set ${destination} up |
ae69ea7e | 171 | return $? |
b5238f57 MT |
172 | } |
173 | ||
174 | function zone_exists() { | |
175 | [ -e "$CONFIG_ZONES/$1" ] #|| device_exists $@ | |
176 | } | |
177 | ||
cd1bc684 MT |
178 | function port_is_up() { |
179 | device_is_up $@ | |
180 | } | |
181 | ||
182 | function zone_is_up() { | |
183 | zone_exists $@ && device_is_up $@ | |
184 | } | |
185 | ||
b5238f57 | 186 | function bridge_devices() { |
ae69ea7e MT |
187 | local bridge |
188 | bridge=$1 | |
189 | [ -z "${bridge}" ] && return 2 | |
190 | brctl show | grep "^${bridge}" | awk '{ print $NF }' | grep -v "^interfaces$" | |
191 | } | |
192 | ||
193 | function zone_add_port() { | |
90af6f24 MT |
194 | local zone |
195 | local port | |
196 | ||
197 | zone=${1} | |
198 | port=${2} | |
199 | ||
200 | brctl addif ${zone} ${port} | |
201 | } | |
202 | ||
203 | function zone_del_port() { | |
204 | local zone | |
205 | local port | |
206 | ||
207 | zone=${1} | |
208 | port=${2} | |
209 | ||
210 | brctl delif ${zone} ${port} | |
ae69ea7e MT |
211 | } |
212 | ||
213 | function all_zones() { | |
214 | local zone | |
215 | for zone in ${CONFIG_ZONES}/*; do | |
216 | [ -d "${zone}" ] && echo ${zone} | |
217 | done | |
b5238f57 | 218 | } |