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 execute "hostname" do
182 command "/bin/hostname -F /etc/hostname"
185 template "/etc/hostname" do
186 source "hostname.erb"
190 notifies :run, "execute[hostname]"
193 template "/etc/hosts" do
200 service "systemd-resolved" do
201 action [:enable, :start]
204 directory "/etc/systemd/resolved.conf.d" do
210 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
211 source "resolved.conf.erb"
215 notifies :restart, "service[systemd-resolved]"
218 if node[:networking][:tcp_fastopen_key]
219 fastopen_keys = data_bag_item("networking", "fastopen")
221 node.normal[:sysctl][:tcp_fastopen] = {
222 :comment => "Set shared key for TCP fast open",
224 "net.ipv4.tcp_fastopen_key" => fastopen_keys[node[:networking][:tcp_fastopen_key]]
229 node.interfaces(:role => :internal) do |interface|
230 if interface[:gateway] && interface[:gateway] != interface[:address]
231 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
232 next unless gateway[:openvpn]
234 gateway[:openvpn][:tunnels].each_value do |tunnel|
235 if tunnel[:peer][:address]
236 route tunnel[:peer][:address] do
237 netmask "255.255.255.255"
238 gateway interface[:gateway]
239 device interface[:interface]
243 next unless tunnel[:peer][:networks]
245 tunnel[:peer][:networks].each do |network|
246 route network[:address] do
247 netmask network[:netmask]
248 gateway interface[:gateway]
249 device interface[:interface]
259 search(:node, "networking:interfaces").collect do |n|
260 next if n[:fqdn] == node[:fqdn]
262 n.interfaces.each do |interface|
263 next unless interface[:role] == "external" && interface[:zone]
265 zones[interface[:zone]] ||= {}
266 zones[interface[:zone]][interface[:family]] ||= []
267 zones[interface[:zone]][interface[:family]] << interface[:address]
273 template "/etc/default/shorewall" do
274 source "shorewall-default.erb"
278 notifies :restart, "service[shorewall]"
281 template "/etc/shorewall/shorewall.conf" do
282 source "shorewall.conf.erb"
286 notifies :restart, "service[shorewall]"
289 template "/etc/shorewall/zones" do
290 source "shorewall-zones.erb"
294 variables :type => "ipv4"
295 notifies :restart, "service[shorewall]"
298 template "/etc/shorewall/interfaces" do
299 source "shorewall-interfaces.erb"
303 notifies :restart, "service[shorewall]"
306 template "/etc/shorewall/hosts" do
307 source "shorewall-hosts.erb"
311 variables :zones => zones
312 notifies :restart, "service[shorewall]"
315 template "/etc/shorewall/conntrack" do
316 source "shorewall-conntrack.erb"
320 notifies :restart, "service[shorewall]"
321 only_if { node[:networking][:firewall][:raw] }
324 template "/etc/shorewall/policy" do
325 source "shorewall-policy.erb"
329 notifies :restart, "service[shorewall]"
332 template "/etc/shorewall/rules" do
333 source "shorewall-rules.erb"
337 variables :family => "inet"
338 notifies :restart, "service[shorewall]"
341 service "shorewall" do
342 action [:enable, :start]
343 supports :restart => true
344 status_command "shorewall status"
347 template "/etc/logrotate.d/shorewall" do
348 source "logrotate.shorewall.erb"
352 variables :name => "shorewall"
355 firewall_rule "limit-icmp-echo" do
361 dest_ports "echo-request"
362 rate_limit "s:1/sec:5"
365 %w[ucl ams bm].each do |zone|
366 firewall_rule "accept-openvpn-#{zone}" do
371 dest_ports "1194:1197"
372 source_ports "1194:1197"
376 if node[:roles].include?("gateway")
377 template "/etc/shorewall/masq" do
378 source "shorewall-masq.erb"
382 notifies :restart, "service[shorewall]"
385 file "/etc/shorewall/masq" do
387 notifies :restart, "service[shorewall]"
391 unless node.interfaces(:family => :inet6).empty?
394 template "/etc/default/shorewall6" do
395 source "shorewall-default.erb"
399 notifies :restart, "service[shorewall6]"
402 template "/etc/shorewall6/shorewall6.conf" do
403 source "shorewall6.conf.erb"
407 notifies :restart, "service[shorewall6]"
410 template "/etc/shorewall6/zones" do
411 source "shorewall-zones.erb"
415 variables :type => "ipv6"
416 notifies :restart, "service[shorewall6]"
419 template "/etc/shorewall6/interfaces" do
420 source "shorewall6-interfaces.erb"
424 notifies :restart, "service[shorewall6]"
427 template "/etc/shorewall6/hosts" do
428 source "shorewall6-hosts.erb"
432 variables :zones => zones
433 notifies :restart, "service[shorewall6]"
436 template "/etc/shorewall6/conntrack" do
437 source "shorewall-conntrack.erb"
441 notifies :restart, "service[shorewall6]"
442 only_if { node[:networking][:firewall][:raw] }
445 template "/etc/shorewall6/policy" do
446 source "shorewall-policy.erb"
450 notifies :restart, "service[shorewall6]"
453 template "/etc/shorewall6/rules" do
454 source "shorewall-rules.erb"
458 variables :family => "inet6"
459 notifies :restart, "service[shorewall6]"
462 service "shorewall6" do
463 action [:enable, :start]
464 supports :restart => true
465 status_command "shorewall6 status"
468 template "/etc/logrotate.d/shorewall6" do
469 source "logrotate.shorewall.erb"
473 variables :name => "shorewall6"
476 firewall_rule "limit-icmp6-echo" do
482 dest_ports "echo-request"
483 rate_limit "s:1/sec:5"
487 firewall_rule "accept-http" do
493 rate_limit node[:networking][:firewall][:http_rate_limit]
494 connection_limit node[:networking][:firewall][:http_connection_limit]
497 firewall_rule "accept-https" do
503 rate_limit node[:networking][:firewall][:http_rate_limit]
504 connection_limit node[:networking][:firewall][:http_connection_limit]