]>
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 MT |
26 | |
27 | function is_mac() { | |
ae69ea7e | 28 | 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 | 29 | } |
63ef8328 MT |
30 | |
31 | function get_device_by_mac() { | |
ae69ea7e MT |
32 | local mac |
33 | local i | |
34 | ||
35 | mac=$1 | |
36 | ||
63ef8328 MT |
37 | for i in /sys/class/net/*; do |
38 | if [ "$(cat $i/address)" = "$mac" ]; then | |
ae69ea7e MT |
39 | grep -q "^${i##*/}" /proc/net/vlan/config 2>/dev/null && continue |
40 | echo ${i##*/} | |
41 | break | |
63ef8328 MT |
42 | fi |
43 | done | |
44 | } | |
45 | ||
ae69ea7e MT |
46 | function get_device_by_mac_and_vid() { |
47 | local mac | |
48 | local vid | |
49 | ||
50 | mac=$1 | |
51 | vid=$2 | |
52 | ||
53 | local i | |
54 | local VID | |
55 | local DEVICE | |
56 | for i in $(cat /proc/net/vlan/config 2>/dev/null); do | |
57 | awk -F'|' '{ print $2 $3 }' | read DEVICE VID PARENT | |
58 | if [ -n "${VID}" ] || [ -n "${DEVICE}" ]; then | |
59 | continue | |
60 | fi | |
61 | if [ "${vid}" = "${VID}" ] && [ "$(macify ${PARENT})" = "${mac}" ]; then | |
62 | echo "${DEVICE}" | |
63 | return 0 | |
64 | fi | |
65 | done | |
66 | return 1 | |
67 | } | |
68 | ||
63ef8328 | 69 | function get_mac_by_device() { |
ae69ea7e MT |
70 | local device |
71 | device=$1 | |
63ef8328 MT |
72 | if [ -d "/sys/class/net/$device" ]; then |
73 | cat /sys/class/net/$device/address | |
74 | return 0 | |
75 | fi | |
76 | return 1 | |
77 | } | |
b5238f57 | 78 | |
ae69ea7e MT |
79 | function devicify() { |
80 | local device | |
81 | local mac | |
82 | ||
83 | device=$1 | |
84 | ||
85 | if is_mac ${device}; then | |
86 | mac=${device} | |
87 | device=$(get_device_by_mac ${device}) | |
88 | fi | |
89 | echo ${device} | |
90 | } | |
91 | ||
92 | function macify() { | |
93 | local input | |
94 | local mac | |
95 | ||
96 | input=$1 | |
97 | ||
98 | if is_mac ${input}; then | |
99 | mac=${input} | |
100 | else | |
101 | mac=$(get_mac_by_device ${input}) | |
102 | fi | |
103 | echo ${mac} | |
104 | } | |
105 | ||
b5238f57 | 106 | function device_exists() { |
ae69ea7e MT |
107 | ip link show $(devicify ${1}) &>/dev/null |
108 | } | |
109 | ||
110 | function rename_device() { | |
111 | local source | |
112 | local destination | |
113 | ||
114 | source=$1 | |
115 | destination=$2 | |
116 | ||
117 | # Check if devices exist | |
118 | if ! device_exists ${source} || device_exists ${destination}; then | |
119 | return 4 | |
120 | fi | |
121 | ||
122 | ip link set ${source} name ${destination} | |
123 | return $? | |
b5238f57 MT |
124 | } |
125 | ||
126 | function zone_exists() { | |
127 | [ -e "$CONFIG_ZONES/$1" ] #|| device_exists $@ | |
128 | } | |
129 | ||
130 | function bridge_devices() { | |
ae69ea7e MT |
131 | local bridge |
132 | bridge=$1 | |
133 | [ -z "${bridge}" ] && return 2 | |
134 | brctl show | grep "^${bridge}" | awk '{ print $NF }' | grep -v "^interfaces$" | |
135 | } | |
136 | ||
137 | function zone_add_port() { | |
138 | brctl addif ${1} ${2} | |
139 | } | |
140 | ||
141 | function all_zones() { | |
142 | local zone | |
143 | for zone in ${CONFIG_ZONES}/*; do | |
144 | [ -d "${zone}" ] && echo ${zone} | |
145 | done | |
b5238f57 | 146 | } |