]> git.ipfire.org Git - people/amarx/ipfire-3.x.git/blame - src/initscripts/networking/functions
Worked on networking stuff.
[people/amarx/ipfire-3.x.git] / src / initscripts / networking / functions
CommitLineData
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
22CONFIG_DIR=/etc/sysconfig/networking
23
24CONFIG_ZONES=${CONFIG_DIR}/zones
25CONFIG_PORTS=${CONFIG_DIR}/ports
b5238f57
MT
26
27function 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
31function 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
46function 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
90af6f24
MT
56 grep '|' < /proc/net/vlan/config 2>/dev/null | sed "s/|//g" | \
57 while read DEVICE VID PARENT; do
58 if [ "${vid}" = "${VID}" ] && [ "$(macify ${PARENT})" = "${mac}" ]; then
59 echo "${DEVICE}"
60 return 0
61 fi
62 done
ae69ea7e
MT
63 return 1
64}
65
90af6f24
MT
66function get_device() {
67 if [ ${#@} -gt 1 ]; then
68 get_device_by_mac_and_vid $@
69 else
70 get_device_by_mac $@
71 fi
72}
73
63ef8328 74function get_mac_by_device() {
ae69ea7e
MT
75 local device
76 device=$1
63ef8328
MT
77 if [ -d "/sys/class/net/$device" ]; then
78 cat /sys/class/net/$device/address
79 return 0
80 fi
81 return 1
82}
b5238f57 83
90af6f24
MT
84function get_mac() {
85 get_mac_by_device $@
86}
87
ae69ea7e
MT
88function devicify() {
89 local device
90 local mac
91
92 device=$1
93
94 if is_mac ${device}; then
95 mac=${device}
96 device=$(get_device_by_mac ${device})
97 fi
98 echo ${device}
99}
100
101function macify() {
102 local input
103 local mac
104
105 input=$1
106
107 if is_mac ${input}; then
108 mac=${input}
109 else
110 mac=$(get_mac_by_device ${input})
111 fi
112 echo ${mac}
113}
114
b5238f57 115function device_exists() {
ae69ea7e
MT
116 ip link show $(devicify ${1}) &>/dev/null
117}
118
119function rename_device() {
120 local source
121 local destination
122
123 source=$1
124 destination=$2
125
126 # Check if devices exist
127 if ! device_exists ${source} || device_exists ${destination}; then
128 return 4
129 fi
130
131 ip link set ${source} name ${destination}
132 return $?
b5238f57
MT
133}
134
135function zone_exists() {
136 [ -e "$CONFIG_ZONES/$1" ] #|| device_exists $@
137}
138
139function bridge_devices() {
ae69ea7e
MT
140 local bridge
141 bridge=$1
142 [ -z "${bridge}" ] && return 2
143 brctl show | grep "^${bridge}" | awk '{ print $NF }' | grep -v "^interfaces$"
144}
145
146function zone_add_port() {
90af6f24
MT
147 local zone
148 local port
149
150 zone=${1}
151 port=${2}
152
153 brctl addif ${zone} ${port}
154}
155
156function zone_del_port() {
157 local zone
158 local port
159
160 zone=${1}
161 port=${2}
162
163 brctl delif ${zone} ${port}
ae69ea7e
MT
164}
165
166function all_zones() {
167 local zone
168 for zone in ${CONFIG_ZONES}/*; do
169 [ -d "${zone}" ] && echo ${zone}
170 done
b5238f57 171}