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 hostname node[:networking][:hostname]
182 template "/etc/hosts" do
187 not_if { ENV["TEST_KITCHEN"] }
190 service "systemd-resolved" do
191 action [:enable, :start]
194 directory "/etc/systemd/resolved.conf.d" do
200 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
201 source "resolved.conf.erb"
205 notifies :restart, "service[systemd-resolved]", :immediately
208 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
209 mount "/etc/resolv.conf" do
211 device node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"][:devices].first
215 link "/etc/resolv.conf" do
216 to "../run/systemd/resolve/stub-resolv.conf"
219 if node[:networking][:tcp_fastopen_key]
220 fastopen_keys = data_bag_item("networking", "fastopen")
222 node.normal[:sysctl][:tcp_fastopen] = {
223 :comment => "Set shared key for TCP fast open",
225 "net.ipv4.tcp_fastopen_key" => fastopen_keys[node[:networking][:tcp_fastopen_key]]
230 node.interfaces(:role => :internal) do |interface|
231 if interface[:gateway] && interface[:gateway] != interface[:address]
232 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
233 next unless gateway[:openvpn]
235 gateway[:openvpn][:tunnels].each_value do |tunnel|
236 if tunnel[:peer][:address]
237 route tunnel[:peer][:address] do
238 netmask "255.255.255.255"
239 gateway interface[:gateway]
240 device interface[:interface]
244 next unless tunnel[:peer][:networks]
246 tunnel[:peer][:networks].each do |network|
247 route network[:address] do
248 netmask network[:netmask]
249 gateway interface[:gateway]
250 device interface[:interface]
260 search(:node, "networking:interfaces").collect do |n|
261 next if n[:fqdn] == node[:fqdn]
263 n.interfaces.each do |interface|
264 next unless interface[:role] == "external" && interface[:zone]
266 zones[interface[:zone]] ||= {}
267 zones[interface[:zone]][interface[:family]] ||= []
268 zones[interface[:zone]][interface[:family]] << interface[:address]
274 template "/etc/default/shorewall" do
275 source "shorewall-default.erb"
279 notifies :restart, "service[shorewall]"
282 template "/etc/shorewall/shorewall.conf" do
283 source "shorewall.conf.erb"
287 notifies :restart, "service[shorewall]"
290 template "/etc/shorewall/zones" do
291 source "shorewall-zones.erb"
295 variables :type => "ipv4"
296 notifies :restart, "service[shorewall]"
299 template "/etc/shorewall/interfaces" do
300 source "shorewall-interfaces.erb"
304 notifies :restart, "service[shorewall]"
307 template "/etc/shorewall/hosts" do
308 source "shorewall-hosts.erb"
312 variables :zones => zones
313 notifies :restart, "service[shorewall]"
316 template "/etc/shorewall/conntrack" do
317 source "shorewall-conntrack.erb"
321 notifies :restart, "service[shorewall]"
322 only_if { node[:networking][:firewall][:raw] }
325 template "/etc/shorewall/policy" do
326 source "shorewall-policy.erb"
330 notifies :restart, "service[shorewall]"
333 template "/etc/shorewall/rules" do
334 source "shorewall-rules.erb"
338 variables :family => "inet"
339 notifies :restart, "service[shorewall]"
342 service "shorewall" do
343 action [:enable, :start]
344 supports :restart => true
345 status_command "shorewall status"
348 template "/etc/logrotate.d/shorewall" do
349 source "logrotate.shorewall.erb"
353 variables :name => "shorewall"
356 firewall_rule "limit-icmp-echo" do
362 dest_ports "echo-request"
363 rate_limit "s:1/sec:5"
366 %w[ucl ams bm].each do |zone|
367 firewall_rule "accept-openvpn-#{zone}" do
372 dest_ports "1194:1197"
373 source_ports "1194:1197"
377 if node[:roles].include?("gateway")
378 template "/etc/shorewall/masq" do
379 source "shorewall-masq.erb"
383 notifies :restart, "service[shorewall]"
386 file "/etc/shorewall/masq" do
388 notifies :restart, "service[shorewall]"
392 unless node.interfaces(:family => :inet6).empty?
395 template "/etc/default/shorewall6" do
396 source "shorewall-default.erb"
400 notifies :restart, "service[shorewall6]"
403 template "/etc/shorewall6/shorewall6.conf" do
404 source "shorewall6.conf.erb"
408 notifies :restart, "service[shorewall6]"
411 template "/etc/shorewall6/zones" do
412 source "shorewall-zones.erb"
416 variables :type => "ipv6"
417 notifies :restart, "service[shorewall6]"
420 template "/etc/shorewall6/interfaces" do
421 source "shorewall6-interfaces.erb"
425 notifies :restart, "service[shorewall6]"
428 template "/etc/shorewall6/hosts" do
429 source "shorewall6-hosts.erb"
433 variables :zones => zones
434 notifies :restart, "service[shorewall6]"
437 template "/etc/shorewall6/conntrack" do
438 source "shorewall-conntrack.erb"
442 notifies :restart, "service[shorewall6]"
443 only_if { node[:networking][:firewall][:raw] }
446 template "/etc/shorewall6/policy" do
447 source "shorewall-policy.erb"
451 notifies :restart, "service[shorewall6]"
454 template "/etc/shorewall6/rules" do
455 source "shorewall-rules.erb"
459 variables :family => "inet6"
460 notifies :restart, "service[shorewall6]"
463 service "shorewall6" do
464 action [:enable, :start]
465 supports :restart => true
466 status_command "shorewall6 status"
469 template "/etc/logrotate.d/shorewall6" do
470 source "logrotate.shorewall.erb"
474 variables :name => "shorewall6"
477 firewall_rule "limit-icmp6-echo" do
483 dest_ports "echo-request"
484 rate_limit "s:1/sec:5"
488 firewall_rule "accept-http" do
494 rate_limit node[:networking][:firewall][:http_rate_limit]
495 connection_limit node[:networking][:firewall][:http_connection_limit]
498 firewall_rule "accept-https" do
504 rate_limit node[:networking][:firewall][:http_rate_limit]
505 connection_limit node[:networking][:firewall][:http_connection_limit]