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]
31 "renderer" => "networkd",
38 node[:networking][:interfaces].each do |name, interface|
39 if interface[:interface]
40 network_packages |= ["vlan"] if interface[:interface] =~ /\.\d+$/
41 network_packages |= ["ifenslave"] if interface[:bond]
43 if interface[:role] && (role = node[:networking][:roles][interface[:role]])
44 if role[interface[:family]]
45 node.normal[:networking][:interfaces][name][:prefix] = role[interface[:family]][:prefix]
46 node.normal[:networking][:interfaces][name][:gateway] = role[interface[:family]][:gateway]
49 node.normal[:networking][:interfaces][name][:metric] = role[:metric]
50 node.normal[:networking][:interfaces][name][:zone] = role[:zone]
53 prefix = node[:networking][:interfaces][name][:prefix]
55 node.normal[:networking][:interfaces][name][:netmask] = (~IPAddr.new(interface[:address]).mask(0)).mask(prefix)
56 node.normal[:networking][:interfaces][name][:network] = IPAddr.new(interface[:address]).mask(prefix)
58 if node[:networking][:netplan]
59 if interface[:interface] =~ /^(.*)\.(\d+)$/
60 deviceplan = netplan["network"]["vlans"][interface[:interface]] = {
61 "id" => Regexp.last_match(2).to_i,
62 "link" => Regexp.last_match(1),
67 elsif interface[:bond]
68 deviceplan = netplan["network"]["bonds"][interface[:interface]] = {
72 "interfaces" => interface[:bond][:slaves].to_a,
74 "mode" => interface[:bond][:mode] || "active-backup",
75 "primary" => interface[:bond][:slaves].first,
76 "mii-monitor-interval" => interface[:bond][:miimon] || 100,
77 "down-delay" => interface[:bond][:downdelay] || 200,
78 "up-delay" => interface[:bond][:updelay] || 200
82 deviceplan["parameters"]["transmit-hash-policy"] = interface[:bond][:xmithashpolicy] if interface[:bond][:xmithashpolicy]
83 deviceplan["parameters"]["lacp-rate"] = interface[:bond][:lacprate] if interface[:bond][:lacprate]
85 deviceplan = netplan["network"]["ethernets"][interface[:interface]] = {
92 deviceplan["addresses"].push("#{interface[:address]}/#{prefix}")
94 if interface[:gateway]
95 if interface[:family] == "inet"
96 default_route = "0.0.0.0/0"
97 elsif interface[:family] == "inet6"
98 default_route = "::/0"
101 deviceplan["routes"].push(
102 "to" => default_route,
103 "via" => interface[:gateway],
104 "metric" => interface[:metric],
110 node.rm(:networking, :interfaces, name)
114 if node[:networking][:netplan]
117 file "/etc/netplan/01-netcfg.yaml" do
121 netplan["network"]["bonds"].each_value do |bond|
122 bond["interfaces"].each do |interface|
123 netplan["network"]["ethernets"][interface] ||= { "accept-ra" => false }
127 netplan["network"]["vlans"].each_value do |vlan|
128 unless vlan["link"] =~ /^bond\d+$/
129 netplan["network"]["ethernets"][vlan["link"]] ||= { "accept-ra" => false }
133 file "/etc/netplan/99-chef.yaml" do
137 content YAML.dump(netplan)
140 service "networking" do
144 file "/etc/network/interfaces" do
148 package "ifupdown" do
152 package network_packages
154 template "/etc/network/interfaces" do
155 source "interfaces.erb"
162 execute "hostname" do
164 command "/bin/hostname -F /etc/hostname"
167 template "/etc/hostname" do
168 source "hostname.erb"
172 notifies :run, "execute[hostname]"
175 template "/etc/hosts" do
182 unless node[:networking][:nameservers].empty?
183 link "/etc/resolv.conf" do
186 to "/run/resolvconf/resolv.conf"
187 only_if { File.symlink?("/etc/resolv.conf") }
190 template "/etc/resolv.conf" do
191 source "resolv.conf.erb"
198 node.interfaces(:role => :internal) do |interface|
199 if interface[:gateway] && interface[:gateway] != interface[:address]
200 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
201 next unless gateway[:openvpn]
203 gateway[:openvpn][:tunnels].each_value do |tunnel|
204 if tunnel[:peer][:address] # ~FC023
205 route tunnel[:peer][:address] do
206 netmask "255.255.255.255"
207 gateway interface[:gateway]
208 device interface[:interface]
212 next unless tunnel[:peer][:networks]
214 tunnel[:peer][:networks].each do |network|
215 route network[:address] do
216 netmask network[:netmask]
217 gateway interface[:gateway]
218 device interface[:interface]
228 search(:node, "networking:interfaces").collect do |n|
229 next if n[:fqdn] == node[:fqdn]
231 n.interfaces.each do |interface|
232 next unless interface[:role] == "external" && interface[:zone]
234 zones[interface[:zone]] ||= {}
235 zones[interface[:zone]][interface[:family]] ||= []
236 zones[interface[:zone]][interface[:family]] << interface[:address]
242 template "/etc/default/shorewall" do
243 source "shorewall-default.erb"
247 notifies :restart, "service[shorewall]"
250 template "/etc/shorewall/shorewall.conf" do
251 source "shorewall.conf.erb"
255 notifies :restart, "service[shorewall]"
258 template "/etc/shorewall/zones" do
259 source "shorewall-zones.erb"
263 variables :type => "ipv4"
264 notifies :restart, "service[shorewall]"
267 template "/etc/shorewall/interfaces" do
268 source "shorewall-interfaces.erb"
272 notifies :restart, "service[shorewall]"
275 template "/etc/shorewall/hosts" do
276 source "shorewall-hosts.erb"
280 variables :zones => zones
281 notifies :restart, "service[shorewall]"
284 template "/etc/shorewall/conntrack" do
285 source "shorewall-conntrack.erb"
289 notifies :restart, "service[shorewall]"
290 only_if { node[:networking][:firewall][:raw] }
293 template "/etc/shorewall/policy" do
294 source "shorewall-policy.erb"
298 notifies :restart, "service[shorewall]"
301 template "/etc/shorewall/rules" do
302 source "shorewall-rules.erb"
306 variables :family => "inet"
307 notifies :restart, "service[shorewall]"
310 service "shorewall" do
311 action [:enable, :start]
312 supports :restart => true
313 status_command "shorewall status"
316 template "/etc/logrotate.d/shorewall" do
317 source "logrotate.shorewall.erb"
321 variables :name => "shorewall"
324 firewall_rule "limit-icmp-echo" do
330 dest_ports "echo-request"
331 rate_limit "s:1/sec:5"
334 %w[ucl ams bm].each do |zone|
335 firewall_rule "accept-openvpn-#{zone}" do
340 dest_ports "1194:1197"
341 source_ports "1194:1197"
345 if node[:roles].include?("gateway")
346 template "/etc/shorewall/masq" do
347 source "shorewall-masq.erb"
351 notifies :restart, "service[shorewall]"
354 file "/etc/shorewall/masq" do
356 notifies :restart, "service[shorewall]"
360 unless node.interfaces(:family => :inet6).empty?
363 template "/etc/default/shorewall6" do
364 source "shorewall-default.erb"
368 notifies :restart, "service[shorewall6]"
371 template "/etc/shorewall6/shorewall6.conf" do
372 source "shorewall6.conf.erb"
376 notifies :restart, "service[shorewall6]"
379 template "/etc/shorewall6/zones" do
380 source "shorewall-zones.erb"
384 variables :type => "ipv6"
385 notifies :restart, "service[shorewall6]"
388 template "/etc/shorewall6/interfaces" do
389 source "shorewall6-interfaces.erb"
393 notifies :restart, "service[shorewall6]"
396 template "/etc/shorewall6/hosts" do
397 source "shorewall6-hosts.erb"
401 variables :zones => zones
402 notifies :restart, "service[shorewall6]"
405 template "/etc/shorewall6/conntrack" do
406 source "shorewall-conntrack.erb"
410 notifies :restart, "service[shorewall6]"
411 only_if { node[:networking][:firewall][:raw] }
414 template "/etc/shorewall6/policy" do
415 source "shorewall-policy.erb"
419 notifies :restart, "service[shorewall6]"
422 template "/etc/shorewall6/rules" do
423 source "shorewall-rules.erb"
427 variables :family => "inet6"
428 notifies :restart, "service[shorewall6]"
431 service "shorewall6" do
432 action [:enable, :start]
433 supports :restart => true
434 status_command "shorewall6 status"
437 template "/etc/logrotate.d/shorewall6" do
438 source "logrotate.shorewall.erb"
442 variables :name => "shorewall6"
445 firewall_rule "limit-icmp6-echo" do
451 dest_ports "echo-request"
452 rate_limit "s:1/sec:5"
456 firewall_rule "accept-http" do
462 rate_limit node[:networking][:firewall][:http_rate_limit]
463 connection_limit node[:networking][:firewall][:http_connection_limit]
466 firewall_rule "accept-https" do
472 rate_limit node[:networking][:firewall][:http_rate_limit]
473 connection_limit node[:networking][:firewall][:http_connection_limit]