ia64/xen-unstable

view tools/examples/network @ 3513:4df7d8e48278

bitkeeper revision 1.1159.226.3 (41f55c8er11pRwXCJ66we1vDzrEZ9Q)

With ipv6, every network interface always has a link local
address. This fools the network script into thinking that
xen-br0 already has a network address, and it refuses to
transfer over eth0's IP address to xen-br0.

By grepping for 'inet ' instead of just 'inet', the network
script is no longer fooled and does the right thing. This
patch makes Xen networking work again on my Fedora system.

Signed-off-by: Rik van Riel <riel@redhat.com>
Signed-off-by: ian.pratt@cl.cam.ac.uk
author iap10@labyrinth.cl.cam.ac.uk
date Mon Jan 24 20:37:34 2005 +0000 (2005-01-24)
parents 03bd2991dbd4
children e2f1686415aa
line source
1 #!/bin/sh
2 #============================================================================
3 # Default Xen network start/stop script.
4 # Xend calls a network script when it starts.
5 # The script name to use is defined in /etc/xen/xend-config.sxp
6 # in the network-script field.
7 #
8 # This script creates a bridge (default xen-br0), adds a device
9 # (default eth0) to it, copies the IP addresses from the device
10 # to the bridge and adjusts the routes accordingly.
11 #
12 # If all goes well, this should ensure that networking stays up.
13 # However, some configurations are upset by this, especially
14 # NFS roots. If the bridged setup does not meet your needs,
15 # configure a different script, for example using routing instead.
16 #
17 # Usage:
18 #
19 # network (start|stop|status) {VAR=VAL}*
20 #
21 # Vars:
22 #
23 # bridge The bridge to use (default xen-br0).
24 # netdev The interface to add to the bridge (default eth0).
25 # antispoof Whether to use iptables to prevent spoofing (default yes).
26 #
27 # start:
28 # Creates the bridge and enslaves netdev to it.
29 # Copies the IP addresses from netdev to the bridge.
30 # Deletes the routes to netdev and adds them on bridge.
31 #
32 # stop:
33 # Removes netdev from the bridge.
34 # Deletes the routes to bridge and adds them to netdev.
35 #
36 # status:
37 # Print ifconfig for netdev and bridge.
38 # Print routes.
39 #
40 #============================================================================
42 # Exit if anything goes wrong.
43 set -e
45 # First arg is the operation.
46 OP=$1
47 shift
49 # Pull variables in args in to environment.
50 for arg ; do export "${arg}" ; done
52 bridge=${bridge:-xen-br0}
53 netdev=${netdev:-eth0}
54 antispoof=${antispoof:-yes}
56 echo "network $OP bridge=$bridge netdev=$netdev antispoof=$antispoof"
58 # Usage: transfer_addrs src dst
59 # Copy all IP addresses (including aliases) from device $src to device $dst.
60 transfer_addrs () {
61 local src=$1
62 local dst=$2
63 # Don't bother if $dst already has IP addresses.
64 if ip addr show dev ${dst} | egrep -q '^ *inet ' ; then
65 return
66 fi
67 # Address lines start with 'inet' and have the device in them.
68 # Replace 'inet' with 'ip addr add' and change the device name $src
69 # to 'dev $src'. Remove netmask as we'll add routes later.
70 ip addr show dev ${src} | egrep '^ *inet ' | sed -e "
71 s/inet/ip addr add/
72 s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)/[0-9]\+@\1@
73 s/${src}/dev ${dst}/
74 " | sh -e
75 }
77 # Usage: transfer_routes src dst
78 # Get all IP routes to device $src, delete them, and
79 # add the same routes to device $dst.
80 # The original routes have to be deleted, otherwise adding them
81 # for $dst fails (duplicate routes).
82 transfer_routes () {
83 local src=$1
84 local dst=$2
85 # List all routes and grep the ones with $src in.
86 # Stick 'ip route del' on the front to delete.
87 # Change $src to $dst and use 'ip route add' to add.
88 ip route list | grep ${src} | sed -e "
89 h
90 s/^/ip route del /
91 P
92 g
93 s/${src}/${dst}/
94 s/^/ip route add /
95 P
96 d
97 " | sh -e
98 }
100 # Usage: create_bridge dev bridge
101 # Create bridge $bridge and add device $dev to it.
102 create_bridge () {
103 local dev=$1
104 local bridge=$2
106 # Don't create the bridge if it already exists.
107 if ! brctl show | grep -q ${bridge} ; then
108 brctl addbr ${bridge}
109 brctl stp ${bridge} off
110 brctl setfd ${bridge} 0
111 fi
112 ifconfig ${bridge} up
113 }
115 # Usage: antispoofing dev bridge
116 # Set the default forwarding policy for $dev to drop.
117 # Allow forwarding to the bridge.
118 antispoofing () {
119 local dev=$1
120 local bridge=$2
122 iptables -P FORWARD DROP
123 iptables -A FORWARD -m physdev --physdev-in ${dev} -j ACCEPT
124 }
126 # Usage: show_status dev bridge
127 # Print ifconfig and routes.
128 show_status () {
129 local dev=$1
130 local bridge=$2
132 echo '============================================================'
133 ifconfig ${dev}
134 ifconfig ${bridge}
135 echo ' '
136 ip route list
137 echo ' '
138 route -n
139 echo '============================================================'
140 }
142 op_start () {
143 if [ "${bridge}" == "null" ] ; then
144 return
145 fi
146 # Create the bridge and give it the interface IP addresses.
147 # Move the interface routes onto the bridge.
148 create_bridge ${netdev} ${bridge}
149 transfer_addrs ${netdev} ${bridge}
150 transfer_routes ${netdev} ${bridge}
151 # Don't add $dev to $bridge if it's already on a bridge.
152 if ! brctl show | grep -q ${netdev} ; then
153 brctl addif ${bridge} ${netdev}
154 fi
156 if [ ${antispoof} == 'yes' ] ; then
157 antispoofing ${netdev} ${bridge}
158 fi
159 }
161 op_stop () {
162 if [ "${bridge}" == "null" ] ; then
163 return
164 fi
165 # Remove the interface from the bridge.
166 # Move the routes back to the interface.
167 brctl delif ${bridge} ${netdev}
168 transfer_routes ${bridge} ${netdev}
170 # It's not our place to be enabling forwarding...
171 }
173 case ${OP} in
174 start)
175 op_start
176 ;;
178 stop)
179 op_stop
180 ;;
182 status)
183 show_status ${netdev} ${bridge}
184 ;;
186 *)
187 echo 'Unknown command: ' ${OP}
188 echo 'Valid commands are: start, stop, status'
189 exit 1
190 esac