1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
"""Bridge control utilities.
"""
import os
import os.path
import re
import sys
os.defpath = os.defpath + ':/sbin:/usr/sbin:/usr/local/sbin'
CMD_IFCONFIG = 'ifconfig'
CMD_ROUTE = 'route'
CMD_BRCTL = 'brctl'
CMD_IPTABLES = "iptables"
opts = None
class Opts:
def __init__(self, defaults):
for (k, v) in defaults.items():
setattr(self, k, v)
pass
def cmd(p, s):
"""Print and execute command 'p' with args 's'.
"""
global opts
c = p + ' ' + s
if opts.verbose: print c
if not opts.dryrun:
os.system(c)
def vif_bridge_add(params):
"""Add the network interface for vif on dom to a bridge.
"""
cmd(CMD_BRCTL, 'addif %(bridge)s %(vif)s' % params)
def vif_bridge_rem(params):
"""Remove the network interface for vif on dom from a bridge.
"""
cmd(CMD_BRCTL, 'delif %(bridge)s %(vif)s' % params)
def vif_restrict_addr(vif, addr, delete=0):
d = { 'vif': vif, 'addr': addr}
if delete:
d['flag'] = '-D'
else:
d['flag'] = '-A'
cmd(CMD_IPTABLES, '-P FORWARD DROP')
cmd(CMD_IPTABLES, '%(flag)s FORWARD -m physdev --physdev-in %(vif)s -s %(addr)s -j ACCEPT' % d)
cmd(CMD_IPTABLES, '%(flag)s FORWARD -m physdev --physdev-out %(vif)s -d %(addr)s -j ACCEPT' % d)
def bridge_create(bridge, **kwd):
"""Create a bridge.
Defaults hello time to 0, forward delay to 0 and stp off.
"""
cmd(CMD_BRCTL, 'addbr %s' % bridge)
if kwd.get('hello', None) is None:
kwd['hello'] = 0
if kwd.get('fd', None) is None:
kwd['fd'] = 0
if kwd.get('stp', None) is None:
kwd['stp'] = 'off'
bridge_set(bridge, **kwd)
def bridge_set(bridge, hello=None, fd=None, stp=None):
"""Set bridge parameters.
"""
if hello is not None:
cmd(CMD_BRCTL, 'sethello %s %d' % (bridge, hello))
if fd is not None:
cmd(CMD_BRCTL, 'setfd %s %d' % (bridge, fd))
if stp is not None:
cmd(CMD_BRCTL, 'stp %s %s' % (bridge, stp))
def bridge_del(bridge):
"""Delete a bridge.
"""
cmd(CMD_BRCTL, 'delbr %s' % bridge)
def routes():
"""Return a list of the routes.
"""
fin = os.popen(CMD_ROUTE + ' -n', 'r')
routes = []
for x in fin:
if x.startswith('Kernel'): continue
if x.startswith('Destination'): continue
x = x.strip()
y = x.split()
z = { 'destination': y[0],
'gateway' : y[1],
'mask' : y[2],
'flags' : y[3],
'metric' : y[4],
'ref' : y[5],
'use' : y[6],
'interface' : y[7] }
routes.append(z)
return routes
def ifconfig(interface):
"""Return the ip config for an interface,
"""
fin = os.popen(CMD_IFCONFIG + ' %s' % interface, 'r')
inetre = re.compile('\s*inet\s*addr:(?P<address>\S*)\s*Bcast:(?P<broadcast>\S*)\s*Mask:(?P<mask>\S*)')
info = None
for x in fin:
m = inetre.match(x)
if not m: continue
info = m.groupdict()
info['interface'] = interface
break
return info
def reconfigure(interface, bridge):
"""Reconfigure an interface to be attached to a bridge, and give the bridge
the IP address etc. from interface. Move the default route to the interface
to the bridge.
"""
global opts
intf_info = ifconfig(interface)
if not intf_info:
print >>sys.stderr, 'Interface not found:', interface
return
#bridge_info = ifconfig(bridge)
#if not bridge_info:
# print >>sys.stderr, 'Bridge not found:', bridge
# return
route_info = routes()
intf_info['bridge'] = bridge
intf_info['gateway'] = None
for r in route_info:
if (r['destination'] == '0.0.0.0' and
'G' in r['flags'] and
r['interface'] == interface):
intf_info['gateway'] = r['gateway']
if not intf_info['gateway']:
print >>sys.stderr, 'Gateway not found: ', interface
return
cmd(CMD_IFCONFIG,
'%(bridge)s %(address)s netmask %(mask)s broadcast %(broadcast)s up'
% intf_info)
cmd(CMD_ROUTE,
'add default gateway %(gateway)s dev %(bridge)s'
% intf_info)
cmd(CMD_BRCTL, 'addif %(bridge)s %(interface)s' % intf_info)
cmd(CMD_IFCONFIG, '%(interface)s 0.0.0.0' % intf_info)
defaults = {
'verbose' : 1,
'dryrun' : 0,
}
opts = Opts(defaults)
def set_opts(val):
global opts
opts = val
return opts
|