1 #
2 # Copyright (c) 2005 XenSource Ltd.
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of version 2.1 of the GNU Lesser General Public
6 # License as published by the Free Software Foundation.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; If not, see <http://www.gnu.org/licenses/>.
15 #
16 
17 
18 # Gentoo doesn't have ifup/ifdown, so we define appropriate alternatives.
19 
20 # Other platforms just use ifup / ifdown directly.
21 
22 ##
23 # preiftransfer
24 #
25 # @param $1 The current name for the physical device, which is also the name
26 #           that the virtual device will take once the physical device has
27 #           been renamed.
28 
29 if ! which ifup >/dev/null 2>/dev/null
30 then
31   preiftransfer()
32   {
33     true
34   }
35   ifup()
36   {
37     false
38   }
39   ifdown()
40   {
41     false
42   }
43 else
44   preiftransfer()
45   {
46     true
47   }
48 fi
49 
50 
51 first_file()
52 {
53   t="$1"
54   shift
55   for file in $@
56   do
57     if [ "$t" "$file" ]
58     then
59       echo "$file"
60       return
61     fi
62   done
63 }
64 
65 find_dhcpd_conf_file()
66 {
67   first_file -f /etc/dhcp3/dhcpd.conf /etc/dhcpd.conf
68 }
69 
70 
71 find_dhcpd_init_file()
72 {
73   first_file -x /etc/init.d/{dhcp3-server,dhcp,dhcpd}
74 }
75 
76 find_dhcpd_arg_file()
77 {
78   first_file -f /etc/sysconfig/dhcpd /etc/defaults/dhcp /etc/default/dhcp3-server
79 }
80 
81 # configure interfaces which act as pure bridge ports:
82 _setup_bridge_port() {
83     local dev="$1"
84     local virtual="$2"
85 
86     # take interface down ...
87     ip link set dev ${dev} down
88 
89     if [ $virtual -ne 0 ] ; then
90         # Initialise a dummy MAC address. We choose the numerically
91         # largest non-broadcast address to prevent the address getting
92         # stolen by an Ethernet bridge for STP purposes.
93         # (FE:FF:FF:FF:FF:FF)
94         ip link set dev ${dev} address fe:ff:ff:ff:ff:ff || true
95     fi
96 
97     # ... and configure it
98     ip address flush dev ${dev}
99 }
100 
101 setup_physical_bridge_port() {
102     _setup_bridge_port $1 0
103 }
104 setup_virtual_bridge_port() {
105     _setup_bridge_port $1 1
106 }
107 
108 # Usage: create_bridge bridge
109 create_bridge () {
110     local bridge=$1
111 
112     # Don't create the bridge if it already exists.
113     if [ ! -e "/sys/class/net/${bridge}/bridge" ]; then
114         if which brctl >&/dev/null; then
115             brctl addbr ${bridge}
116             brctl stp ${bridge} off
117             brctl setfd ${bridge} 0
118         else
119             ip link add name ${bridge} type bridge stp_state 0 forward_delay 0
120         fi
121     fi
122 }
123 
124 # Usage: add_to_bridge bridge dev
125 add_to_bridge () {
126     local bridge=$1
127     local dev=$2
128 
129     # Don't add $dev to $bridge if it's already on a bridge.
130     if [ -e "/sys/class/net/${bridge}/brif/${dev}" ]; then
131 	ip link set dev ${dev} up || true
132 	return
133     fi
134     if which brctl >&/dev/null; then
135         brctl addif ${bridge} ${dev}
136     else
137         ip link set ${dev} master ${bridge}
138     fi
139     ip link set dev ${dev} up
140 }
141 
142 # Usage: set_mtu bridge dev
143 set_mtu () {
144     local bridge=$1
145     local dev=$2
146     mtu="`ip link show dev ${bridge}| awk '/mtu/ { print $5 }'`"
147     if [ -n "$mtu" ] && [ "$mtu" -gt 0 ]
148     then
149             ip link set dev ${dev} mtu $mtu || :
150     fi
151 }
152