2 # Cookbook Name:: networking
5 # Copyright 2010, OpenStreetMap Foundation.
6 # Copyright 2009, Opscode, Inc.
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
12 # https://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
21 # * node[:networking][:nameservers]
27 node[:networking][:interfaces].each do |name, interface|
28 if interface[:interface]
29 network_packages |= ["vlan"] if interface[:interface] =~ /\.\d+$/
30 network_packages |= ["ifenslave"] if interface[:bond]
32 if interface[:role] && (role = node[:networking][:roles][interface[:role]])
33 if role[interface[:family]]
34 node.normal[:networking][:interfaces][name][:prefix] = role[interface[:family]][:prefix]
35 node.normal[:networking][:interfaces][name][:gateway] = role[interface[:family]][:gateway]
38 node.normal[:networking][:interfaces][name][:metric] = role[:metric]
39 node.normal[:networking][:interfaces][name][:zone] = role[:zone]
42 prefix = node[:networking][:interfaces][name][:prefix]
44 node.normal[:networking][:interfaces][name][:netmask] = (~IPAddr.new(interface[:address]).mask(0)).mask(prefix)
45 node.normal[:networking][:interfaces][name][:network] = IPAddr.new(interface[:address]).mask(prefix)
47 node.rm(:networking, :interfaces, name)
51 package network_packages
53 template "/etc/network/interfaces" do
54 source "interfaces.erb"
62 command "/bin/hostname -F /etc/hostname"
65 template "/etc/hostname" do
70 notifies :run, "execute[hostname]"
73 template "/etc/hosts" do
80 unless node[:networking][:nameservers].empty?
81 link "/etc/resolv.conf" do
84 to "/run/resolvconf/resolv.conf"
85 only_if { File.symlink?("/etc/resolv.conf") }
88 template "/etc/resolv.conf" do
89 source "resolv.conf.erb"
96 node.interfaces(:role => :internal) do |interface|
97 if interface[:gateway] && interface[:gateway] != interface[:address]
98 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
99 next unless gateway[:openvpn]
101 gateway[:openvpn][:tunnels].each_value do |tunnel|
102 if tunnel[:peer][:address] # ~FC023
103 route tunnel[:peer][:address] do
104 netmask "255.255.255.255"
105 gateway interface[:gateway]
106 device interface[:interface]
110 next unless tunnel[:peer][:networks]
112 tunnel[:peer][:networks].each do |network|
113 route network[:address] do
114 netmask network[:netmask]
115 gateway interface[:gateway]
116 device interface[:interface]
126 search(:node, "networking:interfaces").collect do |n|
127 next if n[:fqdn] == node[:fqdn]
129 n.interfaces.each do |interface|
130 next unless interface[:role] == "external" && interface[:zone]
132 zones[interface[:zone]] ||= {}
133 zones[interface[:zone]][interface[:family]] ||= []
134 zones[interface[:zone]][interface[:family]] << interface[:address]
140 template "/etc/default/shorewall" do
141 source "shorewall-default.erb"
145 notifies :restart, "service[shorewall]"
148 template "/etc/shorewall/shorewall.conf" do
149 source "shorewall.conf.erb"
153 notifies :restart, "service[shorewall]"
156 template "/etc/shorewall/zones" do
157 source "shorewall-zones.erb"
161 variables :type => "ipv4"
162 notifies :restart, "service[shorewall]"
165 template "/etc/shorewall/interfaces" do
166 source "shorewall-interfaces.erb"
170 notifies :restart, "service[shorewall]"
173 template "/etc/shorewall/hosts" do
174 source "shorewall-hosts.erb"
178 variables :zones => zones
179 notifies :restart, "service[shorewall]"
182 template "/etc/shorewall/conntrack" do
183 source "shorewall-conntrack.erb"
187 notifies :restart, "service[shorewall]"
188 only_if { node[:networking][:firewall][:raw] }
191 template "/etc/shorewall/policy" do
192 source "shorewall-policy.erb"
196 notifies :restart, "service[shorewall]"
199 template "/etc/shorewall/rules" do
200 source "shorewall-rules.erb"
204 variables :family => "inet"
205 notifies :restart, "service[shorewall]"
208 service "shorewall" do
209 action [:enable, :start]
210 supports :restart => true
211 status_command "shorewall status"
214 template "/etc/logrotate.d/shorewall" do
215 source "logrotate.shorewall.erb"
219 variables :name => "shorewall"
222 firewall_rule "limit-icmp-echo" do
228 dest_ports "echo-request"
229 rate_limit "s:1/sec:5"
232 %w[ucl ams bm].each do |zone|
233 firewall_rule "accept-openvpn-#{zone}" do
238 dest_ports "1194:1197"
239 source_ports "1194:1197"
243 if node[:roles].include?("gateway")
244 template "/etc/shorewall/masq" do
245 source "shorewall-masq.erb"
249 notifies :restart, "service[shorewall]"
252 file "/etc/shorewall/masq" do
254 notifies :restart, "service[shorewall]"
258 unless node.interfaces(:family => :inet6).empty?
261 template "/etc/default/shorewall6" do
262 source "shorewall-default.erb"
266 notifies :restart, "service[shorewall6]"
269 template "/etc/shorewall6/shorewall6.conf" do
270 source "shorewall6.conf.erb"
274 notifies :restart, "service[shorewall6]"
277 template "/etc/shorewall6/zones" do
278 source "shorewall-zones.erb"
282 variables :type => "ipv6"
283 notifies :restart, "service[shorewall6]"
286 template "/etc/shorewall6/interfaces" do
287 source "shorewall6-interfaces.erb"
291 notifies :restart, "service[shorewall6]"
294 template "/etc/shorewall6/hosts" do
295 source "shorewall6-hosts.erb"
299 variables :zones => zones
300 notifies :restart, "service[shorewall6]"
303 template "/etc/shorewall6/conntrack" do
304 source "shorewall-conntrack.erb"
308 notifies :restart, "service[shorewall6]"
309 only_if { node[:networking][:firewall][:raw] }
312 template "/etc/shorewall6/policy" do
313 source "shorewall-policy.erb"
317 notifies :restart, "service[shorewall6]"
320 template "/etc/shorewall6/rules" do
321 source "shorewall-rules.erb"
325 variables :family => "inet6"
326 notifies :restart, "service[shorewall6]"
329 service "shorewall6" do
330 action [:enable, :start]
331 supports :restart => true
332 status_command "shorewall6 status"
335 template "/etc/logrotate.d/shorewall6" do
336 source "logrotate.shorewall.erb"
340 variables :name => "shorewall6"
343 firewall_rule "limit-icmp6-echo" do
349 dest_ports "echo-request"
350 rate_limit "s:1/sec:5"
354 firewall_rule "accept-http" do
360 rate_limit node[:networking][:firewall][:http_rate_limit]
361 connection_limit node[:networking][:firewall][:http_connection_limit]
364 firewall_rule "accept-https" do
370 rate_limit node[:networking][:firewall][:http_rate_limit]
371 connection_limit node[:networking][:firewall][:http_connection_limit]