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]
29 "renderer" => "networkd",
36 node[:networking][:interfaces].each do |name, interface|
37 if interface[:interface]
38 if interface[:role] && (role = node[:networking][:roles][interface[:role]])
39 if role[interface[:family]]
40 node.normal[:networking][:interfaces][name][:prefix] = role[interface[:family]][:prefix]
41 node.normal[:networking][:interfaces][name][:gateway] = role[interface[:family]][:gateway]
42 node.normal[:networking][:interfaces][name][:routes] = role[interface[:family]][:routes]
45 node.normal[:networking][:interfaces][name][:metric] = role[:metric]
46 node.normal[:networking][:interfaces][name][:zone] = role[:zone]
49 prefix = node[:networking][:interfaces][name][:prefix]
51 node.normal[:networking][:interfaces][name][:netmask] = (~IPAddr.new(interface[:address]).mask(0)).mask(prefix)
52 node.normal[:networking][:interfaces][name][:network] = IPAddr.new(interface[:address]).mask(prefix)
54 interface = node[:networking][:interfaces][name]
56 deviceplan = if interface[:interface] =~ /^(.*)\.(\d+)$/
57 netplan["network"]["vlans"][interface[:interface]] ||= {
58 "id" => Regexp.last_match(2).to_i,
59 "link" => Regexp.last_match(1),
64 elsif interface[:interface] =~ /^bond\d+$/
65 netplan["network"]["bonds"][interface[:interface]] ||= {
71 netplan["network"]["ethernets"][interface[:interface]] ||= {
78 deviceplan["addresses"].push("#{interface[:address]}/#{prefix}")
81 deviceplan["mtu"] = interface[:mtu]
85 deviceplan["interfaces"] = interface[:bond][:slaves].to_a
87 deviceplan["parameters"] = {
88 "mode" => interface[:bond][:mode] || "active-backup",
89 "primary" => interface[:bond][:slaves].first,
90 "mii-monitor-interval" => interface[:bond][:miimon] || 100,
91 "down-delay" => interface[:bond][:downdelay] || 200,
92 "up-delay" => interface[:bond][:updelay] || 200
95 deviceplan["parameters"]["transmit-hash-policy"] = interface[:bond][:xmithashpolicy] if interface[:bond][:xmithashpolicy]
96 deviceplan["parameters"]["lacp-rate"] = interface[:bond][:lacprate] if interface[:bond][:lacprate]
99 if interface[:gateway]
100 if interface[:family] == "inet"
101 default_route = "0.0.0.0/0"
102 elsif interface[:family] == "inet6"
103 default_route = "::/0"
106 deviceplan["routes"].push(
107 "to" => default_route,
108 "via" => interface[:gateway],
109 "metric" => interface[:metric],
113 # This ordering relies on systemd-networkd adding routes
114 # in reverse order and will need moving before the previous
115 # route once that is fixed:
117 # https://github.com/systemd/systemd/issues/5430
118 # https://github.com/systemd/systemd/pull/10938
119 if interface[:family] == "inet6" &&
120 !interface[:network].include?(interface[:gateway]) &&
121 !IPAddr.new("fe80::/64").include?(interface[:gateway])
122 deviceplan["routes"].push(
123 "to" => interface[:gateway],
129 if interface[:routes]
130 interface[:routes].each do |to, parameters|
135 route["type"] = parameters[:type] if parameters[:type]
136 route["via"] = parameters[:via] if parameters[:via]
137 route["metric"] = parameters[:metric] if parameters[:metric]
139 deviceplan["routes"].push(route)
143 node.rm(:networking, :interfaces, name)
147 netplan["network"]["bonds"].each_value do |bond|
148 bond["interfaces"].each do |interface|
149 netplan["network"]["ethernets"][interface] ||= { "accept-ra" => false }
153 netplan["network"]["vlans"].each_value do |vlan|
154 unless vlan["link"] =~ /^bond\d+$/
155 netplan["network"]["ethernets"][vlan["link"]] ||= { "accept-ra" => false }
159 file "/etc/netplan/01-netcfg.yaml" do
163 file "/etc/netplan/50-cloud-init.yaml" do
167 file "/etc/netplan/99-chef.yaml" do
171 content YAML.dump(netplan)
174 package "cloud-init" do
178 execute "hostname" do
180 command "/bin/hostname -F /etc/hostname"
183 template "/etc/hostname" do
184 source "hostname.erb"
188 notifies :run, "execute[hostname]"
191 template "/etc/hosts" do
198 service "systemd-resolved" do
199 action [:enable, :start]
202 directory "/etc/systemd/resolved.conf.d" do
208 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
209 source "resolved.conf.erb"
213 notifies :restart, "service[systemd-resolved]"
216 if node[:networking][:tcp_fastopen_key]
217 fastopen_keys = data_bag_item("networking", "fastopen")
219 node.normal[:sysctl][:tcp_fastopen] = {
220 :comment => "Set shared key for TCP fast open",
222 "net.ipv4.tcp_fastopen_key" => fastopen_keys[node[:networking][:tcp_fastopen_key]]
227 node.interfaces(:role => :internal) do |interface|
228 if interface[:gateway] && interface[:gateway] != interface[:address]
229 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
230 next unless gateway[:openvpn]
232 gateway[:openvpn][:tunnels].each_value do |tunnel|
233 if tunnel[:peer][:address]
234 route tunnel[:peer][:address] do
235 netmask "255.255.255.255"
236 gateway interface[:gateway]
237 device interface[:interface]
241 next unless tunnel[:peer][:networks]
243 tunnel[:peer][:networks].each do |network|
244 route network[:address] do
245 netmask network[:netmask]
246 gateway interface[:gateway]
247 device interface[:interface]
257 search(:node, "networking:interfaces").collect do |n|
258 next if n[:fqdn] == node[:fqdn]
260 n.interfaces.each do |interface|
261 next unless interface[:role] == "external" && interface[:zone]
263 zones[interface[:zone]] ||= {}
264 zones[interface[:zone]][interface[:family]] ||= []
265 zones[interface[:zone]][interface[:family]] << interface[:address]
271 template "/etc/default/shorewall" do
272 source "shorewall-default.erb"
276 notifies :restart, "service[shorewall]"
279 template "/etc/shorewall/shorewall.conf" do
280 source "shorewall.conf.erb"
284 notifies :restart, "service[shorewall]"
287 template "/etc/shorewall/zones" do
288 source "shorewall-zones.erb"
292 variables :type => "ipv4"
293 notifies :restart, "service[shorewall]"
296 template "/etc/shorewall/interfaces" do
297 source "shorewall-interfaces.erb"
301 notifies :restart, "service[shorewall]"
304 template "/etc/shorewall/hosts" do
305 source "shorewall-hosts.erb"
309 variables :zones => zones
310 notifies :restart, "service[shorewall]"
313 template "/etc/shorewall/conntrack" do
314 source "shorewall-conntrack.erb"
318 notifies :restart, "service[shorewall]"
319 only_if { node[:networking][:firewall][:raw] }
322 template "/etc/shorewall/policy" do
323 source "shorewall-policy.erb"
327 notifies :restart, "service[shorewall]"
330 template "/etc/shorewall/rules" do
331 source "shorewall-rules.erb"
335 variables :family => "inet"
336 notifies :restart, "service[shorewall]"
339 service "shorewall" do
340 action [:enable, :start]
341 supports :restart => true
342 status_command "shorewall status"
345 template "/etc/logrotate.d/shorewall" do
346 source "logrotate.shorewall.erb"
350 variables :name => "shorewall"
353 firewall_rule "limit-icmp-echo" do
359 dest_ports "echo-request"
360 rate_limit "s:1/sec:5"
363 %w[ucl ams bm].each do |zone|
364 firewall_rule "accept-openvpn-#{zone}" do
369 dest_ports "1194:1197"
370 source_ports "1194:1197"
374 if node[:roles].include?("gateway")
375 template "/etc/shorewall/masq" do
376 source "shorewall-masq.erb"
380 notifies :restart, "service[shorewall]"
383 file "/etc/shorewall/masq" do
385 notifies :restart, "service[shorewall]"
389 unless node.interfaces(:family => :inet6).empty?
392 template "/etc/default/shorewall6" do
393 source "shorewall-default.erb"
397 notifies :restart, "service[shorewall6]"
400 template "/etc/shorewall6/shorewall6.conf" do
401 source "shorewall6.conf.erb"
405 notifies :restart, "service[shorewall6]"
408 template "/etc/shorewall6/zones" do
409 source "shorewall-zones.erb"
413 variables :type => "ipv6"
414 notifies :restart, "service[shorewall6]"
417 template "/etc/shorewall6/interfaces" do
418 source "shorewall6-interfaces.erb"
422 notifies :restart, "service[shorewall6]"
425 template "/etc/shorewall6/hosts" do
426 source "shorewall6-hosts.erb"
430 variables :zones => zones
431 notifies :restart, "service[shorewall6]"
434 template "/etc/shorewall6/conntrack" do
435 source "shorewall-conntrack.erb"
439 notifies :restart, "service[shorewall6]"
440 only_if { node[:networking][:firewall][:raw] }
443 template "/etc/shorewall6/policy" do
444 source "shorewall-policy.erb"
448 notifies :restart, "service[shorewall6]"
451 template "/etc/shorewall6/rules" do
452 source "shorewall-rules.erb"
456 variables :family => "inet6"
457 notifies :restart, "service[shorewall6]"
460 service "shorewall6" do
461 action [:enable, :start]
462 supports :restart => true
463 status_command "shorewall6 status"
466 template "/etc/logrotate.d/shorewall6" do
467 source "logrotate.shorewall.erb"
471 variables :name => "shorewall6"
474 firewall_rule "limit-icmp6-echo" do
480 dest_ports "echo-request"
481 rate_limit "s:1/sec:5"
485 firewall_rule "accept-http" do
491 rate_limit node[:networking][:firewall][:http_rate_limit]
492 connection_limit node[:networking][:firewall][:http_connection_limit]
495 firewall_rule "accept-https" do
501 rate_limit node[:networking][:firewall][:http_rate_limit]
502 connection_limit node[:networking][:firewall][:http_connection_limit]