46
46
'network device for vlans')
47
47
flags.DEFINE_string('dhcpbridge', _bin_file('nova-dhcpbridge'),
48
48
'location of nova-dhcpbridge')
49
flags.DEFINE_string('cc_host', utils.get_my_ip(), 'ip of api server')
50
flags.DEFINE_integer('cc_port', 8773, 'cloud controller port')
51
flags.DEFINE_string('routing_source_ip', '127.0.0.1',
49
flags.DEFINE_string('routing_source_ip', '$my_ip',
52
50
'Public IP of network host')
53
51
flags.DEFINE_bool('use_nova_chains', False,
54
52
'use the nova_ routing chains instead of default')
56
DEFAULT_PORTS = [("tcp", 80), ("tcp", 22), ("udp", 1194), ("tcp", 443)]
54
flags.DEFINE_string('dns_server', None,
55
'if set, uses specific dns server for dnsmasq')
56
flags.DEFINE_string('dmz_cidr', '10.128.0.0/24',
57
'dmz range that should be accepted')
59
60
def metadata_forward():
60
61
"""Create forwarding rule for metadata"""
61
62
_confirm_rule("PREROUTING", "-t nat -s 0.0.0.0/0 "
62
63
"-d 169.254.169.254/32 -p tcp -m tcp --dport 80 -j DNAT "
63
"--to-destination %s:%s" % (FLAGS.cc_host, FLAGS.cc_port))
64
"--to-destination %s:%s" % (FLAGS.ec2_dmz_host, FLAGS.ec2_port))
67
68
"""Basic networking setup goes here"""
70
if FLAGS.use_nova_chains:
71
_execute("sudo iptables -N nova_input", check_exit_code=False)
72
_execute("sudo iptables -D %s -j nova_input" % FLAGS.input_chain,
73
check_exit_code=False)
74
_execute("sudo iptables -A %s -j nova_input" % FLAGS.input_chain)
76
_execute("sudo iptables -N nova_forward", check_exit_code=False)
77
_execute("sudo iptables -D FORWARD -j nova_forward",
78
check_exit_code=False)
79
_execute("sudo iptables -A FORWARD -j nova_forward")
81
_execute("sudo iptables -N nova_output", check_exit_code=False)
82
_execute("sudo iptables -D OUTPUT -j nova_output",
83
check_exit_code=False)
84
_execute("sudo iptables -A OUTPUT -j nova_output")
86
_execute("sudo iptables -t nat -N nova_prerouting",
87
check_exit_code=False)
88
_execute("sudo iptables -t nat -D PREROUTING -j nova_prerouting",
89
check_exit_code=False)
90
_execute("sudo iptables -t nat -A PREROUTING -j nova_prerouting")
92
_execute("sudo iptables -t nat -N nova_postrouting",
93
check_exit_code=False)
94
_execute("sudo iptables -t nat -D POSTROUTING -j nova_postrouting",
95
check_exit_code=False)
96
_execute("sudo iptables -t nat -A POSTROUTING -j nova_postrouting")
98
_execute("sudo iptables -t nat -N nova_snatting",
99
check_exit_code=False)
100
_execute("sudo iptables -t nat -D POSTROUTING -j nova_snatting",
101
check_exit_code=False)
102
_execute("sudo iptables -t nat -A POSTROUTING -j nova_snatting")
104
_execute("sudo iptables -t nat -N nova_output", check_exit_code=False)
105
_execute("sudo iptables -t nat -D OUTPUT -j nova_output",
106
check_exit_code=False)
107
_execute("sudo iptables -t nat -A OUTPUT -j nova_output")
109
# NOTE(vish): This makes it easy to ensure snatting rules always
110
# come after the accept rules in the postrouting chain
111
_execute("sudo iptables -t nat -N SNATTING",
112
check_exit_code=False)
113
_execute("sudo iptables -t nat -D POSTROUTING -j SNATTING",
114
check_exit_code=False)
115
_execute("sudo iptables -t nat -A POSTROUTING -j SNATTING")
68
117
# NOTE(devcamcar): Cloud public SNAT entries and the default
69
118
# SNAT rule for outbound traffic.
70
_confirm_rule("POSTROUTING", "-t nat -s %s "
119
_confirm_rule("SNATTING", "-t nat -s %s "
71
120
"-j SNAT --to-source %s"
72
% (FLAGS.fixed_range, FLAGS.routing_source_ip))
121
% (FLAGS.fixed_range, FLAGS.routing_source_ip), append=True)
74
_confirm_rule("POSTROUTING", "-t nat -s %s -j MASQUERADE" %
123
_confirm_rule("POSTROUTING", "-t nat -s %s -d %s -j ACCEPT" %
124
(FLAGS.fixed_range, FLAGS.dmz_cidr))
76
125
_confirm_rule("POSTROUTING", "-t nat -s %(range)s -d %(range)s -j ACCEPT" %
77
126
{'range': FLAGS.fixed_range})
80
def bind_floating_ip(floating_ip):
129
def bind_floating_ip(floating_ip, check_exit_code=True):
81
130
"""Bind ip to public interface"""
82
131
_execute("sudo ip addr add %s dev %s" % (floating_ip,
83
FLAGS.public_interface))
132
FLAGS.public_interface),
133
check_exit_code=check_exit_code)
86
136
def unbind_floating_ip(floating_ip):
102
152
"""Ensure floating ip forwarding rule"""
103
153
_confirm_rule("PREROUTING", "-t nat -d %s -j DNAT --to %s"
104
154
% (floating_ip, fixed_ip))
105
_confirm_rule("POSTROUTING", "-t nat -s %s -j SNAT --to %s"
155
_confirm_rule("SNATTING", "-t nat -s %s -j SNAT --to %s"
106
156
% (fixed_ip, floating_ip))
107
# TODO(joshua): Get these from the secgroup datastore entries
108
_confirm_rule("FORWARD", "-d %s -p icmp -j ACCEPT"
110
for (protocol, port) in DEFAULT_PORTS:
111
_confirm_rule("FORWARD", "-d %s -p %s --dport %s -j ACCEPT"
112
% (fixed_ip, protocol, port))
115
159
def remove_floating_forward(floating_ip, fixed_ip):
116
160
"""Remove forwarding for floating ip"""
117
161
_remove_rule("PREROUTING", "-t nat -d %s -j DNAT --to %s"
118
162
% (floating_ip, fixed_ip))
119
_remove_rule("POSTROUTING", "-t nat -s %s -j SNAT --to %s"
163
_remove_rule("SNATTING", "-t nat -s %s -j SNAT --to %s"
120
164
% (fixed_ip, floating_ip))
121
_remove_rule("FORWARD", "-d %s -p icmp -j ACCEPT"
123
for (protocol, port) in DEFAULT_PORTS:
124
_remove_rule("FORWARD", "-d %s -p %s --dport %s -j ACCEPT"
125
% (fixed_ip, protocol, port))
128
167
def ensure_vlan_bridge(vlan_num, bridge, net_attrs=None):
158
197
net_attrs['gateway'],
159
198
net_attrs['broadcast'],
160
199
net_attrs['netmask']))
201
_execute("sudo ifconfig %s add %s up" % \
203
net_attrs['cidr_v6']))
162
205
_execute("sudo ifconfig %s up" % bridge)
206
if FLAGS.use_nova_chains:
207
(out, err) = _execute("sudo iptables -N nova_forward",
208
check_exit_code=False)
209
if err != 'iptables: Chain already exists.\n':
210
# NOTE(vish): chain didn't exist link chain
211
_execute("sudo iptables -D FORWARD -j nova_forward",
212
check_exit_code=False)
213
_execute("sudo iptables -A FORWARD -j nova_forward")
163
215
_confirm_rule("FORWARD", "--in-interface %s -j ACCEPT" % bridge)
164
216
_confirm_rule("FORWARD", "--out-interface %s -j ACCEPT" % bridge)
217
_execute("sudo iptables -N nova-local", check_exit_code=False)
218
_confirm_rule("FORWARD", "-j nova-local")
167
221
def get_dhcp_hosts(context, network_id):
213
267
_execute(command, addl_env=env)
270
def update_ra(context, network_id):
271
network_ref = db.network_get(context, network_id)
273
conffile = _ra_file(network_ref['bridge'], 'conf')
274
with open(conffile, 'w') as f:
280
MaxRtrAdvInterval 10;
287
""" % (network_ref['bridge'], network_ref['cidr_v6'])
290
# Make sure radvd can actually read it (it setuid()s to "nobody")
291
os.chmod(conffile, 0644)
293
pid = _ra_pid_for(network_ref['bridge'])
295
# if radvd is already running, then tell it to reload
297
out, _err = _execute('cat /proc/%d/cmdline'
298
% pid, check_exit_code=False)
301
_execute('sudo kill -HUP %d' % pid)
303
except Exception as exc: # pylint: disable-msg=W0703
304
LOG.debug(_("Hupping radvd threw %s"), exc)
306
LOG.debug(_("Pid %d is stale, relaunching radvd"), pid)
307
command = _ra_cmd(network_ref)
309
db.network_update(context, network_id,
311
utils.get_my_linklocal(network_ref['bridge'])})
216
314
def _host_dhcp(fixed_ip_ref):
217
315
"""Return a host string for an address"""
218
316
instance_ref = fixed_ip_ref['instance']