2 # Cookbook:: 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 if interface[:role] && (role = node[:networking][:roles][interface[:role]])
41 if role[interface[:family]]
42 node.default[:networking][:interfaces][name][:prefix] = role[interface[:family]][:prefix]
43 node.default[:networking][:interfaces][name][:gateway] = role[interface[:family]][:gateway]
44 node.default[:networking][:interfaces][name][:routes] = role[interface[:family]][:routes]
47 node.default[:networking][:interfaces][name][:metric] = role[:metric]
48 node.default[:networking][:interfaces][name][:zone] = role[:zone]
51 if interface[:address]
52 prefix = node[:networking][:interfaces][name][:prefix]
54 node.default[:networking][:interfaces][name][:netmask] = (~IPAddr.new(interface[:address]).mask(0)).mask(prefix)
55 node.default[:networking][:interfaces][name][:network] = IPAddr.new(interface[:address]).mask(prefix)
58 interface = node[:networking][:interfaces][name]
60 deviceplan = if interface[:interface] =~ /^(.*)\.(\d+)$/
61 netplan["network"]["vlans"][interface[:interface]] ||= {
62 "id" => Regexp.last_match(2).to_i,
63 "link" => Regexp.last_match(1),
68 elsif interface[:interface] =~ /^bond\d+$/
69 netplan["network"]["bonds"][interface[:interface]] ||= {
75 netplan["network"]["ethernets"][interface[:interface]] ||= {
82 if interface[:address]
83 deviceplan["addresses"].push("#{interface[:address]}/#{prefix}")
87 deviceplan["mtu"] = interface[:mtu]
91 deviceplan["interfaces"] = interface[:bond][:slaves].to_a
93 deviceplan["parameters"] = {
94 "mode" => interface[:bond][:mode] || "active-backup",
95 "primary" => interface[:bond][:slaves].first,
96 "mii-monitor-interval" => interface[:bond][:miimon] || 100,
97 "down-delay" => interface[:bond][:downdelay] || 200,
98 "up-delay" => interface[:bond][:updelay] || 200
101 deviceplan["parameters"]["transmit-hash-policy"] = interface[:bond][:xmithashpolicy] if interface[:bond][:xmithashpolicy]
102 deviceplan["parameters"]["lacp-rate"] = interface[:bond][:lacprate] if interface[:bond][:lacprate]
105 if interface[:gateway]
106 if interface[:family] == "inet"
107 default_route = "0.0.0.0/0"
108 elsif interface[:family] == "inet6"
109 default_route = "::/0"
112 deviceplan["routes"].push(
113 "to" => default_route,
114 "via" => interface[:gateway],
115 "metric" => interface[:metric],
119 # This ordering relies on systemd-networkd adding routes
120 # in reverse order and will need moving before the previous
121 # route once that is fixed:
123 # https://github.com/systemd/systemd/issues/5430
124 # https://github.com/systemd/systemd/pull/10938
125 if interface[:family] == "inet6" &&
126 !interface[:network].include?(interface[:gateway]) &&
127 !IPAddr.new("fe80::/64").include?(interface[:gateway])
128 deviceplan["routes"].push(
129 "to" => interface[:gateway],
135 if interface[:routes]
136 interface[:routes].each do |to, parameters|
141 route["type"] = parameters[:type] if parameters[:type]
142 route["via"] = parameters[:via] if parameters[:via]
143 route["metric"] = parameters[:metric] if parameters[:metric]
145 deviceplan["routes"].push(route)
149 node.rm(:networking, :interfaces, name)
153 netplan["network"]["bonds"].each_value do |bond|
154 bond["interfaces"].each do |interface|
155 netplan["network"]["ethernets"][interface] ||= { "accept-ra" => false }
159 netplan["network"]["vlans"].each_value do |vlan|
160 unless vlan["link"] =~ /^bond\d+$/
161 netplan["network"]["ethernets"][vlan["link"]] ||= { "accept-ra" => false }
165 file "/etc/netplan/01-netcfg.yaml" do
169 file "/etc/netplan/50-cloud-init.yaml" do
173 file "/etc/netplan/99-chef.yaml" do
177 content YAML.dump(netplan)
180 package "cloud-init" do
184 ohai "reload-hostname" do
189 execute "hostnamectl-set-hostname" do
190 command "hostnamectl set-hostname #{node[:networking][:hostname]}"
191 notifies :reload, "ohai[reload-hostname]"
192 not_if { ENV.key?("TEST_KITCHEN") || node[:hostnamectl][:static_hostname] == node[:networking][:hostname] }
195 template "/etc/hosts" do
200 not_if { ENV["TEST_KITCHEN"] }
203 service "systemd-resolved" do
204 action [:enable, :start]
207 directory "/etc/systemd/resolved.conf.d" do
213 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
214 source "resolved.conf.erb"
218 notifies :restart, "service[systemd-resolved]", :immediately
221 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
222 mount "/etc/resolv.conf" do
224 device node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"][:devices].first
228 link "/etc/resolv.conf" do
229 to "../run/systemd/resolve/stub-resolv.conf"
232 node.interfaces(:role => :internal) do |interface|
233 if interface[:gateway] && interface[:gateway] != interface[:address]
234 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
235 next unless gateway[:openvpn]
237 gateway[:openvpn][:tunnels].each_value do |tunnel|
238 if tunnel[:peer][:address]
239 route tunnel[:peer][:address] do
240 netmask "255.255.255.255"
241 gateway interface[:gateway]
242 device interface[:interface]
246 next unless tunnel[:peer][:networks]
248 tunnel[:peer][:networks].each do |network|
249 route network[:address] do
250 netmask network[:netmask]
251 gateway interface[:gateway]
252 device interface[:interface]
262 search(:node, "networking:interfaces").collect do |n|
263 next if n[:fqdn] == node[:fqdn]
265 n.interfaces.each do |interface|
266 next unless interface[:role] == "external" && interface[:zone]
268 zones[interface[:zone]] ||= {}
269 zones[interface[:zone]][interface[:family]] ||= []
270 zones[interface[:zone]][interface[:family]] << interface[:address]
276 template "/etc/default/shorewall" do
277 source "shorewall-default.erb"
281 notifies :restart, "service[shorewall]"
284 template "/etc/shorewall/shorewall.conf" do
285 source "shorewall.conf.erb"
289 notifies :restart, "service[shorewall]"
292 template "/etc/shorewall/zones" do
293 source "shorewall-zones.erb"
297 variables :type => "ipv4"
298 notifies :restart, "service[shorewall]"
301 template "/etc/shorewall/interfaces" do
302 source "shorewall-interfaces.erb"
306 notifies :restart, "service[shorewall]"
309 template "/etc/shorewall/hosts" do
310 source "shorewall-hosts.erb"
314 variables :zones => zones
315 notifies :restart, "service[shorewall]"
318 template "/etc/shorewall/conntrack" do
319 source "shorewall-conntrack.erb"
323 notifies :restart, "service[shorewall]"
324 only_if { node[:networking][:firewall][:raw] }
327 template "/etc/shorewall/policy" do
328 source "shorewall-policy.erb"
332 notifies :restart, "service[shorewall]"
335 template "/etc/shorewall/rules" do
336 source "shorewall-rules.erb"
340 variables :family => "inet"
341 notifies :restart, "service[shorewall]"
344 service "shorewall" do
345 action [:enable, :start]
346 supports :restart => true
347 status_command "shorewall status"
350 template "/etc/logrotate.d/shorewall" do
351 source "logrotate.shorewall.erb"
355 variables :name => "shorewall"
358 firewall_rule "limit-icmp-echo" do
364 dest_ports "echo-request"
365 rate_limit "s:1/sec:5"
368 %w[ucl ams bm].each do |zone|
369 firewall_rule "accept-openvpn-#{zone}" do
374 dest_ports "1194:1197"
375 source_ports "1194:1197"
379 if node[:roles].include?("gateway")
380 template "/etc/shorewall/masq" do
381 source "shorewall-masq.erb"
385 notifies :restart, "service[shorewall]"
388 file "/etc/shorewall/masq" do
390 notifies :restart, "service[shorewall]"
394 unless node.interfaces(:family => :inet6).empty?
397 template "/etc/default/shorewall6" do
398 source "shorewall-default.erb"
402 notifies :restart, "service[shorewall6]"
405 template "/etc/shorewall6/shorewall6.conf" do
406 source "shorewall6.conf.erb"
410 notifies :restart, "service[shorewall6]"
413 template "/etc/shorewall6/zones" do
414 source "shorewall-zones.erb"
418 variables :type => "ipv6"
419 notifies :restart, "service[shorewall6]"
422 template "/etc/shorewall6/interfaces" do
423 source "shorewall6-interfaces.erb"
427 notifies :restart, "service[shorewall6]"
430 template "/etc/shorewall6/hosts" do
431 source "shorewall6-hosts.erb"
435 variables :zones => zones
436 notifies :restart, "service[shorewall6]"
439 template "/etc/shorewall6/conntrack" do
440 source "shorewall-conntrack.erb"
444 notifies :restart, "service[shorewall6]"
445 only_if { node[:networking][:firewall][:raw] }
448 template "/etc/shorewall6/policy" do
449 source "shorewall-policy.erb"
453 notifies :restart, "service[shorewall6]"
456 template "/etc/shorewall6/rules" do
457 source "shorewall-rules.erb"
461 variables :family => "inet6"
462 notifies :restart, "service[shorewall6]"
465 service "shorewall6" do
466 action [:enable, :start]
467 supports :restart => true
468 status_command "shorewall6 status"
471 template "/etc/logrotate.d/shorewall6" do
472 source "logrotate.shorewall.erb"
476 variables :name => "shorewall6"
479 firewall_rule "limit-icmp6-echo" do
485 dest_ports "echo-request"
486 rate_limit "s:1/sec:5"
490 firewall_rule "accept-http" do
496 rate_limit node[:networking][:firewall][:http_rate_limit]
497 connection_limit node[:networking][:firewall][:http_connection_limit]
500 firewall_rule "accept-https" do
506 rate_limit node[:networking][:firewall][:http_rate_limit]
507 connection_limit node[:networking][:firewall][:http_connection_limit]