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
29if ! which ifup >/dev/null 2>/dev/null
30then
31  preiftransfer()
32  {
33    true
34  }
35  ifup()
36  {
37    false
38  }
39  ifdown()
40  {
41    false
42  }
43else
44  preiftransfer()
45  {
46    true
47  }
48fi
49
50
51first_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
65find_dhcpd_conf_file()
66{
67  first_file -f /etc/dhcp3/dhcpd.conf /etc/dhcpd.conf
68}
69
70
71find_dhcpd_init_file()
72{
73  first_file -x /etc/init.d/{dhcp3-server,dhcp,dhcpd}
74}
75
76find_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
101setup_physical_bridge_port() {
102    _setup_bridge_port $1 0
103}
104setup_virtual_bridge_port() {
105    _setup_bridge_port $1 1
106}
107
108# Usage: create_bridge bridge
109create_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	brctl addbr ${bridge}
115	brctl stp ${bridge} off
116	brctl setfd ${bridge} 0
117    fi
118}
119
120# Usage: add_to_bridge bridge dev
121add_to_bridge () {
122    local bridge=$1
123    local dev=$2
124
125    # Don't add $dev to $bridge if it's already on a bridge.
126    if [ -e "/sys/class/net/${bridge}/brif/${dev}" ]; then
127	ip link set dev ${dev} up || true
128	return
129    fi
130    brctl addif ${bridge} ${dev}
131    ip link set dev ${dev} up
132}
133
134# Usage: set_mtu bridge dev
135set_mtu () {
136    local bridge=$1
137    local dev=$2
138    mtu="`ip link show dev ${bridge}| awk '/mtu/ { print $5 }'`"
139    if [ -n "$mtu" ] && [ "$mtu" -gt 0 ]
140    then
141            ip link set dev ${dev} mtu $mtu || :
142    fi
143}
144