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.normal[:networking][:interfaces][name][:prefix] = role[interface[:family]][:prefix]
43 node.normal[:networking][:interfaces][name][:gateway] = role[interface[:family]][:gateway]
44 node.normal[:networking][:interfaces][name][:routes] = role[interface[:family]][:routes]
47 node.normal[:networking][:interfaces][name][:metric] = role[:metric]
48 node.normal[:networking][:interfaces][name][:zone] = role[:zone]
51 prefix = node[:networking][:interfaces][name][:prefix]
53 node.normal[:networking][:interfaces][name][:netmask] = (~IPAddr.new(interface[:address]).mask(0)).mask(prefix)
54 node.normal[:networking][:interfaces][name][:network] = IPAddr.new(interface[:address]).mask(prefix)
56 interface = node[:networking][:interfaces][name]
58 deviceplan = if interface[:interface] =~ /^(.*)\.(\d+)$/
59 netplan["network"]["vlans"][interface[:interface]] ||= {
60 "id" => Regexp.last_match(2).to_i,
61 "link" => Regexp.last_match(1),
66 elsif interface[:interface] =~ /^bond\d+$/
67 netplan["network"]["bonds"][interface[:interface]] ||= {
73 netplan["network"]["ethernets"][interface[:interface]] ||= {
80 deviceplan["addresses"].push("#{interface[:address]}/#{prefix}")
83 deviceplan["mtu"] = interface[:mtu]
87 deviceplan["interfaces"] = interface[:bond][:slaves].to_a
89 deviceplan["parameters"] = {
90 "mode" => interface[:bond][:mode] || "active-backup",
91 "primary" => interface[:bond][:slaves].first,
92 "mii-monitor-interval" => interface[:bond][:miimon] || 100,
93 "down-delay" => interface[:bond][:downdelay] || 200,
94 "up-delay" => interface[:bond][:updelay] || 200
97 deviceplan["parameters"]["transmit-hash-policy"] = interface[:bond][:xmithashpolicy] if interface[:bond][:xmithashpolicy]
98 deviceplan["parameters"]["lacp-rate"] = interface[:bond][:lacprate] if interface[:bond][:lacprate]
101 if interface[:gateway]
102 if interface[:family] == "inet"
103 default_route = "0.0.0.0/0"
104 elsif interface[:family] == "inet6"
105 default_route = "::/0"
108 deviceplan["routes"].push(
109 "to" => default_route,
110 "via" => interface[:gateway],
111 "metric" => interface[:metric],
115 # This ordering relies on systemd-networkd adding routes
116 # in reverse order and will need moving before the previous
117 # route once that is fixed:
119 # https://github.com/systemd/systemd/issues/5430
120 # https://github.com/systemd/systemd/pull/10938
121 if interface[:family] == "inet6" &&
122 !interface[:network].include?(interface[:gateway]) &&
123 !IPAddr.new("fe80::/64").include?(interface[:gateway])
124 deviceplan["routes"].push(
125 "to" => interface[:gateway],
131 if interface[:routes]
132 interface[:routes].each do |to, parameters|
137 route["type"] = parameters[:type] if parameters[:type]
138 route["via"] = parameters[:via] if parameters[:via]
139 route["metric"] = parameters[:metric] if parameters[:metric]
141 deviceplan["routes"].push(route)
145 node.rm(:networking, :interfaces, name)
149 netplan["network"]["bonds"].each_value do |bond|
150 bond["interfaces"].each do |interface|
151 netplan["network"]["ethernets"][interface] ||= { "accept-ra" => false }
155 netplan["network"]["vlans"].each_value do |vlan|
156 unless vlan["link"] =~ /^bond\d+$/
157 netplan["network"]["ethernets"][vlan["link"]] ||= { "accept-ra" => false }
161 file "/etc/netplan/01-netcfg.yaml" do
165 file "/etc/netplan/50-cloud-init.yaml" do
169 file "/etc/netplan/99-chef.yaml" do
173 content YAML.dump(netplan)
176 package "cloud-init" do
180 ohai "reload-hostname" do
185 execute "hostnamectl-set-static" do
186 command "hostnamectl set-static #{node[:networking][:hostname]}"
187 notifies :reload, "ohai[reload-hostname]"
188 not_if { node[:hostnamectl][:static_hostname] == node[:networking][:hostname] }
191 template "/etc/hosts" do
196 not_if { ENV["TEST_KITCHEN"] }
199 service "systemd-resolved" do
200 action [:enable, :start]
203 directory "/etc/systemd/resolved.conf.d" do
209 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
210 source "resolved.conf.erb"
214 notifies :restart, "service[systemd-resolved]", :immediately
217 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
218 mount "/etc/resolv.conf" do
220 device node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"][:devices].first
224 link "/etc/resolv.conf" do
225 to "../run/systemd/resolve/stub-resolv.conf"
228 if node[:networking][:tcp_fastopen_key]
229 fastopen_keys = data_bag_item("networking", "fastopen")
231 node.normal[:sysctl][:tcp_fastopen] = {
232 :comment => "Set shared key for TCP fast open",
234 "net.ipv4.tcp_fastopen_key" => fastopen_keys[node[:networking][:tcp_fastopen_key]]
239 node.interfaces(:role => :internal) do |interface|
240 if interface[:gateway] && interface[:gateway] != interface[:address]
241 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
242 next unless gateway[:openvpn]
244 gateway[:openvpn][:tunnels].each_value do |tunnel|
245 if tunnel[:peer][:address]
246 route tunnel[:peer][:address] do
247 netmask "255.255.255.255"
248 gateway interface[:gateway]
249 device interface[:interface]
253 next unless tunnel[:peer][:networks]
255 tunnel[:peer][:networks].each do |network|
256 route network[:address] do
257 netmask network[:netmask]
258 gateway interface[:gateway]
259 device interface[:interface]
269 search(:node, "networking:interfaces").collect do |n|
270 next if n[:fqdn] == node[:fqdn]
272 n.interfaces.each do |interface|
273 next unless interface[:role] == "external" && interface[:zone]
275 zones[interface[:zone]] ||= {}
276 zones[interface[:zone]][interface[:family]] ||= []
277 zones[interface[:zone]][interface[:family]] << interface[:address]
283 template "/etc/default/shorewall" do
284 source "shorewall-default.erb"
288 notifies :restart, "service[shorewall]"
291 template "/etc/shorewall/shorewall.conf" do
292 source "shorewall.conf.erb"
296 notifies :restart, "service[shorewall]"
299 template "/etc/shorewall/zones" do
300 source "shorewall-zones.erb"
304 variables :type => "ipv4"
305 notifies :restart, "service[shorewall]"
308 template "/etc/shorewall/interfaces" do
309 source "shorewall-interfaces.erb"
313 notifies :restart, "service[shorewall]"
316 template "/etc/shorewall/hosts" do
317 source "shorewall-hosts.erb"
321 variables :zones => zones
322 notifies :restart, "service[shorewall]"
325 template "/etc/shorewall/conntrack" do
326 source "shorewall-conntrack.erb"
330 notifies :restart, "service[shorewall]"
331 only_if { node[:networking][:firewall][:raw] }
334 template "/etc/shorewall/policy" do
335 source "shorewall-policy.erb"
339 notifies :restart, "service[shorewall]"
342 template "/etc/shorewall/rules" do
343 source "shorewall-rules.erb"
347 variables :family => "inet"
348 notifies :restart, "service[shorewall]"
351 service "shorewall" do
352 action [:enable, :start]
353 supports :restart => true
354 status_command "shorewall status"
357 template "/etc/logrotate.d/shorewall" do
358 source "logrotate.shorewall.erb"
362 variables :name => "shorewall"
365 firewall_rule "limit-icmp-echo" do
371 dest_ports "echo-request"
372 rate_limit "s:1/sec:5"
375 %w[ucl ams bm].each do |zone|
376 firewall_rule "accept-openvpn-#{zone}" do
381 dest_ports "1194:1197"
382 source_ports "1194:1197"
386 if node[:roles].include?("gateway")
387 template "/etc/shorewall/masq" do
388 source "shorewall-masq.erb"
392 notifies :restart, "service[shorewall]"
395 file "/etc/shorewall/masq" do
397 notifies :restart, "service[shorewall]"
401 unless node.interfaces(:family => :inet6).empty?
404 template "/etc/default/shorewall6" do
405 source "shorewall-default.erb"
409 notifies :restart, "service[shorewall6]"
412 template "/etc/shorewall6/shorewall6.conf" do
413 source "shorewall6.conf.erb"
417 notifies :restart, "service[shorewall6]"
420 template "/etc/shorewall6/zones" do
421 source "shorewall-zones.erb"
425 variables :type => "ipv6"
426 notifies :restart, "service[shorewall6]"
429 template "/etc/shorewall6/interfaces" do
430 source "shorewall6-interfaces.erb"
434 notifies :restart, "service[shorewall6]"
437 template "/etc/shorewall6/hosts" do
438 source "shorewall6-hosts.erb"
442 variables :zones => zones
443 notifies :restart, "service[shorewall6]"
446 template "/etc/shorewall6/conntrack" do
447 source "shorewall-conntrack.erb"
451 notifies :restart, "service[shorewall6]"
452 only_if { node[:networking][:firewall][:raw] }
455 template "/etc/shorewall6/policy" do
456 source "shorewall-policy.erb"
460 notifies :restart, "service[shorewall6]"
463 template "/etc/shorewall6/rules" do
464 source "shorewall-rules.erb"
468 variables :family => "inet6"
469 notifies :restart, "service[shorewall6]"
472 service "shorewall6" do
473 action [:enable, :start]
474 supports :restart => true
475 status_command "shorewall6 status"
478 template "/etc/logrotate.d/shorewall6" do
479 source "logrotate.shorewall.erb"
483 variables :name => "shorewall6"
486 firewall_rule "limit-icmp6-echo" do
492 dest_ports "echo-request"
493 rate_limit "s:1/sec:5"
497 firewall_rule "accept-http" do
503 rate_limit node[:networking][:firewall][:http_rate_limit]
504 connection_limit node[:networking][:firewall][:http_connection_limit]
507 firewall_rule "accept-https" do
513 rate_limit node[:networking][:firewall][:http_rate_limit]
514 connection_limit node[:networking][:firewall][:http_connection_limit]