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]", :immediately
218 if node[:filesystem][:by_mountpoint]["/etc/resolv.conf"]
219 mount "/etc/resolv.conf" do
221 device node[:filesystem][:by_mountpoint]["/etc/resolv.conf"][:devices].first
225 link "/etc/resolv.conf" do
226 to "../run/systemd/resolve/stub-resolv.conf"
229 if node[:networking][:tcp_fastopen_key]
230 fastopen_keys = data_bag_item("networking", "fastopen")
232 node.normal[:sysctl][:tcp_fastopen] = {
233 :comment => "Set shared key for TCP fast open",
235 "net.ipv4.tcp_fastopen_key" => fastopen_keys[node[:networking][:tcp_fastopen_key]]
240 node.interfaces(:role => :internal) do |interface|
241 if interface[:gateway] && interface[:gateway] != interface[:address]
242 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
243 next unless gateway[:openvpn]
245 gateway[:openvpn][:tunnels].each_value do |tunnel|
246 if tunnel[:peer][:address]
247 route tunnel[:peer][:address] do
248 netmask "255.255.255.255"
249 gateway interface[:gateway]
250 device interface[:interface]
254 next unless tunnel[:peer][:networks]
256 tunnel[:peer][:networks].each do |network|
257 route network[:address] do
258 netmask network[:netmask]
259 gateway interface[:gateway]
260 device interface[:interface]
270 search(:node, "networking:interfaces").collect do |n|
271 next if n[:fqdn] == node[:fqdn]
273 n.interfaces.each do |interface|
274 next unless interface[:role] == "external" && interface[:zone]
276 zones[interface[:zone]] ||= {}
277 zones[interface[:zone]][interface[:family]] ||= []
278 zones[interface[:zone]][interface[:family]] << interface[:address]
284 template "/etc/default/shorewall" do
285 source "shorewall-default.erb"
289 notifies :restart, "service[shorewall]"
292 template "/etc/shorewall/shorewall.conf" do
293 source "shorewall.conf.erb"
297 notifies :restart, "service[shorewall]"
300 template "/etc/shorewall/zones" do
301 source "shorewall-zones.erb"
305 variables :type => "ipv4"
306 notifies :restart, "service[shorewall]"
309 template "/etc/shorewall/interfaces" do
310 source "shorewall-interfaces.erb"
314 notifies :restart, "service[shorewall]"
317 template "/etc/shorewall/hosts" do
318 source "shorewall-hosts.erb"
322 variables :zones => zones
323 notifies :restart, "service[shorewall]"
326 template "/etc/shorewall/conntrack" do
327 source "shorewall-conntrack.erb"
331 notifies :restart, "service[shorewall]"
332 only_if { node[:networking][:firewall][:raw] }
335 template "/etc/shorewall/policy" do
336 source "shorewall-policy.erb"
340 notifies :restart, "service[shorewall]"
343 template "/etc/shorewall/rules" do
344 source "shorewall-rules.erb"
348 variables :family => "inet"
349 notifies :restart, "service[shorewall]"
352 service "shorewall" do
353 action [:enable, :start]
354 supports :restart => true
355 status_command "shorewall status"
358 template "/etc/logrotate.d/shorewall" do
359 source "logrotate.shorewall.erb"
363 variables :name => "shorewall"
366 firewall_rule "limit-icmp-echo" do
372 dest_ports "echo-request"
373 rate_limit "s:1/sec:5"
376 %w[ucl ams bm].each do |zone|
377 firewall_rule "accept-openvpn-#{zone}" do
382 dest_ports "1194:1197"
383 source_ports "1194:1197"
387 if node[:roles].include?("gateway")
388 template "/etc/shorewall/masq" do
389 source "shorewall-masq.erb"
393 notifies :restart, "service[shorewall]"
396 file "/etc/shorewall/masq" do
398 notifies :restart, "service[shorewall]"
402 unless node.interfaces(:family => :inet6).empty?
405 template "/etc/default/shorewall6" do
406 source "shorewall-default.erb"
410 notifies :restart, "service[shorewall6]"
413 template "/etc/shorewall6/shorewall6.conf" do
414 source "shorewall6.conf.erb"
418 notifies :restart, "service[shorewall6]"
421 template "/etc/shorewall6/zones" do
422 source "shorewall-zones.erb"
426 variables :type => "ipv6"
427 notifies :restart, "service[shorewall6]"
430 template "/etc/shorewall6/interfaces" do
431 source "shorewall6-interfaces.erb"
435 notifies :restart, "service[shorewall6]"
438 template "/etc/shorewall6/hosts" do
439 source "shorewall6-hosts.erb"
443 variables :zones => zones
444 notifies :restart, "service[shorewall6]"
447 template "/etc/shorewall6/conntrack" do
448 source "shorewall-conntrack.erb"
452 notifies :restart, "service[shorewall6]"
453 only_if { node[:networking][:firewall][:raw] }
456 template "/etc/shorewall6/policy" do
457 source "shorewall-policy.erb"
461 notifies :restart, "service[shorewall6]"
464 template "/etc/shorewall6/rules" do
465 source "shorewall-rules.erb"
469 variables :family => "inet6"
470 notifies :restart, "service[shorewall6]"
473 service "shorewall6" do
474 action [:enable, :start]
475 supports :restart => true
476 status_command "shorewall6 status"
479 template "/etc/logrotate.d/shorewall6" do
480 source "logrotate.shorewall.erb"
484 variables :name => "shorewall6"
487 firewall_rule "limit-icmp6-echo" do
493 dest_ports "echo-request"
494 rate_limit "s:1/sec:5"
498 firewall_rule "accept-http" do
504 rate_limit node[:networking][:firewall][:http_rate_limit]
505 connection_limit node[:networking][:firewall][:http_connection_limit]
508 firewall_rule "accept-https" do
514 rate_limit node[:networking][:firewall][:http_rate_limit]
515 connection_limit node[:networking][:firewall][:http_connection_limit]