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]
26 keys = data_bag_item("networking", "keys")
33 "renderer" => "networkd",
40 node[:networking][:interfaces].each do |name, interface|
41 if interface[:interface]
42 if interface[:role] && (role = node[:networking][:roles][interface[:role]])
43 if role[interface[:family]]
44 node.default[:networking][:interfaces][name][:prefix] = role[interface[:family]][:prefix]
45 node.default[:networking][:interfaces][name][:gateway] = role[interface[:family]][:gateway]
46 node.default[:networking][:interfaces][name][:routes] = role[interface[:family]][:routes]
49 node.default[:networking][:interfaces][name][:metric] = role[:metric]
50 node.default[:networking][:interfaces][name][:zone] = role[:zone]
53 if interface[:address]
54 prefix = node[:networking][:interfaces][name][:prefix]
56 node.default[:networking][:interfaces][name][:netmask] = (~IPAddr.new(interface[:address]).mask(0)).mask(prefix)
57 node.default[:networking][:interfaces][name][:network] = IPAddr.new(interface[:address]).mask(prefix)
60 interface = node[:networking][:interfaces][name]
62 deviceplan = if interface[:interface] =~ /^(.*)\.(\d+)$/
63 netplan["network"]["vlans"][interface[:interface]] ||= {
64 "id" => Regexp.last_match(2).to_i,
65 "link" => Regexp.last_match(1),
70 elsif interface[:interface] =~ /^bond\d+$/
71 netplan["network"]["bonds"][interface[:interface]] ||= {
77 netplan["network"]["ethernets"][interface[:interface]] ||= {
84 if interface[:address]
85 deviceplan["addresses"].push("#{interface[:address]}/#{prefix}")
89 deviceplan["mtu"] = interface[:mtu]
93 deviceplan["interfaces"] = interface[:bond][:slaves].to_a
95 deviceplan["parameters"] = {
96 "mode" => interface[:bond][:mode] || "active-backup",
97 "primary" => interface[:bond][:slaves].first,
98 "mii-monitor-interval" => interface[:bond][:miimon] || 100,
99 "down-delay" => interface[:bond][:downdelay] || 200,
100 "up-delay" => interface[:bond][:updelay] || 200
103 deviceplan["parameters"]["transmit-hash-policy"] = interface[:bond][:xmithashpolicy] if interface[:bond][:xmithashpolicy]
104 deviceplan["parameters"]["lacp-rate"] = interface[:bond][:lacprate] if interface[:bond][:lacprate]
107 if interface[:gateway]
108 if interface[:family] == "inet"
109 default_route = "0.0.0.0/0"
110 elsif interface[:family] == "inet6"
111 default_route = "::/0"
114 deviceplan["routes"].push(
115 "to" => default_route,
116 "via" => interface[:gateway],
117 "metric" => interface[:metric],
121 # This ordering relies on systemd-networkd adding routes
122 # in reverse order and will need moving before the previous
123 # route once that is fixed:
125 # https://github.com/systemd/systemd/issues/5430
126 # https://github.com/systemd/systemd/pull/10938
127 if interface[:family] == "inet6" &&
128 !interface[:network].include?(interface[:gateway]) &&
129 !IPAddr.new("fe80::/64").include?(interface[:gateway])
130 deviceplan["routes"].push(
131 "to" => interface[:gateway],
136 if interface[:role] == "internal" && interface[:gateway] != interface[:address]
137 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
138 next unless gateway[:openvpn]
140 gateway[:openvpn][:tunnels].each_value do |tunnel|
141 if tunnel[:peer][:address]
142 deviceplan["routes"].push(
143 "to" => "#{tunnel[:peer][:address]}/32",
144 "via" => interface[:gateway]
147 route tunnel[:peer][:address] do
148 netmask "255.255.255.255"
149 gateway interface[:gateway]
150 device interface[:interface]
154 next unless tunnel[:peer][:networks]
156 tunnel[:peer][:networks].each do |network|
157 prefix = IPAddr.new("#{network[:address]}/#{network[:netmask]}").prefix
159 deviceplan["routes"].push(
160 "to" => "#{network[:address]}/#{prefix}",
161 "via" => interface[:gateway]
164 route network[:address] do
165 netmask network[:netmask]
166 gateway interface[:gateway]
167 device interface[:interface]
175 if interface[:routes]
176 interface[:routes].each do |to, parameters|
181 route["type"] = parameters[:type] if parameters[:type]
182 route["via"] = parameters[:via] if parameters[:via]
183 route["metric"] = parameters[:metric] if parameters[:metric]
185 deviceplan["routes"].push(route)
189 node.rm(:networking, :interfaces, name)
193 netplan["network"]["bonds"].each_value do |bond|
194 bond["interfaces"].each do |interface|
195 netplan["network"]["ethernets"][interface] ||= { "accept-ra" => false }
199 netplan["network"]["vlans"].each_value do |vlan|
200 unless vlan["link"] =~ /^bond\d+$/
201 netplan["network"]["ethernets"][vlan["link"]] ||= { "accept-ra" => false }
205 file "/etc/netplan/01-netcfg.yaml" do
209 file "/etc/netplan/50-cloud-init.yaml" do
213 file "/etc/netplan/99-chef.yaml" do
217 content YAML.dump(netplan)
220 package "cloud-init" do
224 if node[:networking][:wireguard][:enabled]
225 package "wireguard-tools" do
229 directory "/var/lib/systemd/wireguard" do
231 group "systemd-network"
236 file "/var/lib/systemd/wireguard/private.key" do
237 action :create_if_missing
239 group "systemd-network"
241 content %x{wg genkey}
245 node.default[:networking][:wireguard][:public_key] = %x{wg pubkey < /var/lib/systemd/wireguard/private.key}
247 file "/var/lib/systemd/wireguard/preshared.key" do
248 action :create_if_missing
250 group "systemd-network"
252 content keys["wireguard"]
255 template "/etc/systemd/network/wireguard.netdev" do
256 source "wireguard.netdev.erb"
262 template "/etc/systemd/network/wireguard.network" do
263 source "wireguard.network.erb"
269 execute "ip-link-delete-wg0" do
271 command "ip link delete wg0"
272 subscribes :run, "template[/etc/systemd/network/wireguard.netdev]"
273 only_if { ::File.exist?("/sys/class/net/wg0") }
276 execute "networkctl-reload" do
278 command "networkctl reload"
279 subscribes :run, "template[/etc/systemd/network/wireguard.netdev]"
280 subscribes :run, "template[/etc/systemd/network/wireguard.network]"
281 not_if { ENV.key?("TEST_KITCHEN") }
285 ohai "reload-hostname" do
290 execute "hostnamectl-set-hostname" do
291 command "hostnamectl set-hostname #{node[:networking][:hostname]}"
292 notifies :reload, "ohai[reload-hostname]"
293 not_if { ENV.key?("TEST_KITCHEN") || node[:hostnamectl][:static_hostname] == node[:networking][:hostname] }
296 template "/etc/hosts" do
301 not_if { ENV["TEST_KITCHEN"] }
304 service "systemd-resolved" do
305 action [:enable, :start]
308 directory "/etc/systemd/resolved.conf.d" do
314 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
315 source "resolved.conf.erb"
319 notifies :restart, "service[systemd-resolved]", :immediately
322 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
323 mount "/etc/resolv.conf" do
325 device node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"][:devices].first
329 link "/etc/resolv.conf" do
330 to "../run/systemd/resolve/stub-resolv.conf"
335 search(:node, "networking:interfaces").collect do |n|
336 next if n[:fqdn] == node[:fqdn]
338 n.interfaces.each do |interface|
339 next unless interface[:role] == "external" && interface[:zone]
341 zones[interface[:zone]] ||= {}
342 zones[interface[:zone]][interface[:family]] ||= []
343 zones[interface[:zone]][interface[:family]] << interface[:address]
349 template "/etc/default/shorewall" do
350 source "shorewall-default.erb"
354 notifies :restart, "service[shorewall]"
357 template "/etc/shorewall/shorewall.conf" do
358 source "shorewall.conf.erb"
362 notifies :restart, "service[shorewall]"
365 template "/etc/shorewall/zones" do
366 source "shorewall-zones.erb"
370 variables :type => "ipv4"
371 notifies :restart, "service[shorewall]"
374 template "/etc/shorewall/interfaces" do
375 source "shorewall-interfaces.erb"
379 notifies :restart, "service[shorewall]"
382 template "/etc/shorewall/hosts" do
383 source "shorewall-hosts.erb"
387 variables :zones => zones
388 notifies :restart, "service[shorewall]"
391 template "/etc/shorewall/conntrack" do
392 source "shorewall-conntrack.erb"
396 notifies :restart, "service[shorewall]"
397 only_if { node[:networking][:firewall][:raw] }
400 template "/etc/shorewall/policy" do
401 source "shorewall-policy.erb"
405 notifies :restart, "service[shorewall]"
408 template "/etc/shorewall/rules" do
410 source "shorewall-rules.erb"
414 variables :family => "inet"
415 notifies :restart, "service[shorewall]"
418 notify_group "shorewall-rules" do
420 notifies :create, "template[/etc/shorewall/rules]"
423 if node[:networking][:firewall][:enabled]
424 service "shorewall" do
425 action [:enable, :start]
426 supports :restart => true
427 status_command "shorewall status"
430 service "shorewall" do
431 action [:disable, :stop]
432 supports :restart => true
433 status_command "shorewall status"
437 template "/etc/logrotate.d/shorewall" do
438 source "logrotate.shorewall.erb"
442 variables :name => "shorewall"
445 firewall_rule "limit-icmp-echo" do
451 dest_ports "echo-request"
452 rate_limit "s:1/sec:5"
455 %w[ucl ams bm].each do |zone|
456 firewall_rule "accept-openvpn-#{zone}" do
461 dest_ports "1194:1197"
462 source_ports "1194:1197"
466 if node[:networking][:wireguard][:enabled]
467 firewall_rule "accept-wireguard" do
477 if node[:roles].include?("gateway")
478 template "/etc/shorewall/masq" do
479 source "shorewall-masq.erb"
483 notifies :restart, "service[shorewall]"
486 file "/etc/shorewall/masq" do
488 notifies :restart, "service[shorewall]"
492 unless node.interfaces(:family => :inet6).empty?
495 template "/etc/default/shorewall6" do
496 source "shorewall-default.erb"
500 notifies :restart, "service[shorewall6]"
503 template "/etc/shorewall6/shorewall6.conf" do
504 source "shorewall6.conf.erb"
508 notifies :restart, "service[shorewall6]"
511 template "/etc/shorewall6/zones" do
512 source "shorewall-zones.erb"
516 variables :type => "ipv6"
517 notifies :restart, "service[shorewall6]"
520 template "/etc/shorewall6/interfaces" do
521 source "shorewall6-interfaces.erb"
525 notifies :restart, "service[shorewall6]"
528 template "/etc/shorewall6/hosts" do
529 source "shorewall6-hosts.erb"
533 variables :zones => zones
534 notifies :restart, "service[shorewall6]"
537 template "/etc/shorewall6/conntrack" do
538 source "shorewall-conntrack.erb"
542 notifies :restart, "service[shorewall6]"
543 only_if { node[:networking][:firewall][:raw] }
546 template "/etc/shorewall6/policy" do
547 source "shorewall-policy.erb"
551 notifies :restart, "service[shorewall6]"
554 template "/etc/shorewall6/rules" do
556 source "shorewall-rules.erb"
560 variables :family => "inet6"
561 notifies :restart, "service[shorewall6]"
564 notify_group "shorewall6-rules" do
566 notifies :create, "template[/etc/shorewall6/rules]"
569 if node[:networking][:firewall][:enabled]
570 service "shorewall6" do
571 action [:enable, :start]
572 supports :restart => true
573 status_command "shorewall6 status"
576 service "shorewall6" do
577 action [:disable, :stop]
578 supports :restart => true
579 status_command "shorewall6 status"
583 template "/etc/logrotate.d/shorewall6" do
584 source "logrotate.shorewall.erb"
588 variables :name => "shorewall6"
591 firewall_rule "limit-icmp6-echo" do
597 dest_ports "echo-request"
598 rate_limit "s:1/sec:5"
602 firewall_rule "accept-http" do
608 rate_limit node[:networking][:firewall][:http_rate_limit]
609 connection_limit node[:networking][:firewall][:http_connection_limit]
612 firewall_rule "accept-https" do
618 rate_limit node[:networking][:firewall][:http_rate_limit]
619 connection_limit node[:networking][:firewall][:http_connection_limit]