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] && interface[:gateway] != interface[:address]
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],
137 if interface[:routes]
138 interface[:routes].each do |to, parameters|
139 next if parameters[:via] == interface[:address]
145 route["type"] = parameters[:type] if parameters[:type]
146 route["via"] = parameters[:via] if parameters[:via]
147 route["metric"] = parameters[:metric] if parameters[:metric]
149 deviceplan["routes"].push(route)
153 node.rm(:networking, :interfaces, name)
157 netplan["network"]["bonds"].each_value do |bond|
158 bond["interfaces"].each do |interface|
159 netplan["network"]["ethernets"][interface] ||= { "accept-ra" => false, "optional" => true }
163 netplan["network"]["vlans"].each_value do |vlan|
164 unless vlan["link"] =~ /^bond\d+$/
165 netplan["network"]["ethernets"][vlan["link"]] ||= { "accept-ra" => false }
169 file "/etc/netplan/00-installer-config.yaml" do
173 file "/etc/netplan/01-netcfg.yaml" do
177 file "/etc/netplan/50-cloud-init.yaml" do
181 file "/etc/netplan/99-chef.yaml" do
185 content YAML.dump(netplan)
188 package "cloud-init" do
192 if node[:networking][:wireguard][:enabled]
193 wireguard_id = persistent_token("networking", "wireguard")
195 node.default[:networking][:wireguard][:address] = "fd43:e709:ea6d:1:#{wireguard_id[0, 4]}:#{wireguard_id[4, 4]}:#{wireguard_id[8, 4]}:#{wireguard_id[12, 4]}"
197 package "wireguard-tools" do
201 directory "/var/lib/systemd/wireguard" do
203 group "systemd-network"
208 file "/var/lib/systemd/wireguard/private.key" do
209 action :create_if_missing
211 group "systemd-network"
213 content %x(wg genkey)
217 node.default[:networking][:wireguard][:public_key] = %x(wg pubkey < /var/lib/systemd/wireguard/private.key).chomp
219 file "/var/lib/systemd/wireguard/preshared.key" do
220 action :create_if_missing
222 group "systemd-network"
224 content keys["wireguard"]
227 if node[:roles].include?("gateway")
228 search(:node, "roles:gateway") do |gateway|
229 next if gateway.name == node.name
230 next unless gateway[:networking][:wireguard] && gateway[:networking][:wireguard][:enabled]
232 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
233 "#{interface[:network]}/#{interface[:prefix]}"
236 node.default[:networking][:wireguard][:peers] << {
237 :public_key => gateway[:networking][:wireguard][:public_key],
238 :allowed_ips => allowed_ips,
239 :endpoint => "#{gateway.name}:51820"
243 search(:node, "roles:shenron OR roles:prometheus") do |server|
244 allowed_ips = server.interfaces(:role => :internal).map do |interface|
245 "#{interface[:network]}/#{interface[:prefix]}"
248 if server[:networking][:private_address]
249 allowed_ips << "#{server[:networking][:private_address]}/32"
252 node.default[:networking][:wireguard][:peers] << {
253 :public_key => server[:networking][:wireguard][:public_key],
254 :allowed_ips => allowed_ips,
255 :endpoint => "#{server.name}:51820"
259 node.default[:networking][:wireguard][:peers] << {
260 :public_key => "7Oj9ufNlgidyH/xDc+aHQKMjJPqTmD/ab13agMh6AxA=",
261 :allowed_ips => "10.0.16.1/32",
262 :endpoint => "gate.compton.nu:51820"
266 node.default[:networking][:wireguard][:peers] << {
267 :public_key => "RofATnvlWxP3mt87+QKRXFE5MVxtoCcTsJ+yftZYEE4=",
268 :allowed_ips => "10.89.122.1/32",
269 :endpoint => "gate.firefishy.com:51820"
273 node.default[:networking][:wireguard][:peers] << {
274 :public_key => "YbUkREE9TAmomqgL/4Fh2e5u2Hh7drN/2o5qg3ndRxg=",
275 :allowed_ips => "10.89.123.1/32",
276 :endpoint => "roaming.firefishy.com:51820"
278 elsif node[:roles].include?("shenron")
279 search(:node, "roles:gateway") do |gateway|
280 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
281 "#{interface[:network]}/#{interface[:prefix]}"
284 node.default[:networking][:wireguard][:peers] << {
285 :public_key => gateway[:networking][:wireguard][:public_key],
286 :allowed_ips => allowed_ips,
287 :endpoint => "#{gateway.name}:51820"
292 template "/etc/systemd/network/wireguard.netdev" do
293 source "wireguard.netdev.erb"
295 group "systemd-network"
299 template "/etc/systemd/network/wireguard.network" do
300 source "wireguard.network.erb"
306 if node[:lsb][:release].to_f < 20.04
307 execute "ip-link-delete-wg0" do
309 command "ip link delete wg0"
310 subscribes :run, "template[/etc/systemd/network/wireguard.netdev]"
311 only_if { ::File.exist?("/sys/class/net/wg0") }
314 service "systemd-networkd" do
316 subscribes :restart, "template[/etc/systemd/network/wireguard.netdev]"
317 subscribes :restart, "template[/etc/systemd/network/wireguard.network]"
321 execute "networkctl-delete-wg0" do
323 command "networkctl delete wg0"
324 subscribes :run, "template[/etc/systemd/network/wireguard.netdev]"
325 only_if { ::File.exist?("/sys/class/net/wg0") }
328 execute "networkctl-reload" do
330 command "networkctl reload"
331 subscribes :run, "template[/etc/systemd/network/wireguard.netdev]"
332 subscribes :run, "template[/etc/systemd/network/wireguard.network]"
338 ohai "reload-hostname" do
343 execute "hostnamectl-set-hostname" do
344 command "hostnamectl set-hostname #{node[:networking][:hostname]}"
345 notifies :reload, "ohai[reload-hostname]"
346 not_if { kitchen? || node[:hostnamectl][:static_hostname] == node[:networking][:hostname] }
349 template "/etc/hosts" do
357 service "systemd-resolved" do
358 action [:enable, :start]
361 directory "/etc/systemd/resolved.conf.d" do
367 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
368 source "resolved.conf.erb"
372 notifies :restart, "service[systemd-resolved]", :immediately
375 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
376 execute "umount-resolve-conf" do
377 command "umount -c /etc/resolv.conf"
381 link "/etc/resolv.conf" do
382 to "../run/systemd/resolve/stub-resolv.conf"
387 search(:node, "networking:interfaces").collect do |n|
388 next if n[:fqdn] == node[:fqdn]
390 n.interfaces.each do |interface|
391 next unless interface[:role] == "external" && interface[:zone]
393 zones[interface[:zone]] ||= {}
394 zones[interface[:zone]][interface[:family]] ||= []
395 zones[interface[:zone]][interface[:family]] << interface[:address]
401 systemd_service "shorewall-docker" do
404 exec_stop "/sbin/shorewall $OPTIONS stop"
405 notifies :restart, "service[shorewall]"
408 template "/etc/default/shorewall" do
409 source "shorewall-default.erb"
413 notifies :restart, "service[shorewall]"
416 template "/etc/shorewall/shorewall.conf" do
417 source "shorewall.conf.erb"
421 notifies :restart, "service[shorewall]"
424 template "/etc/shorewall/zones" do
425 source "shorewall-zones.erb"
429 variables :type => "ipv4"
430 notifies :restart, "service[shorewall]"
433 template "/etc/shorewall/interfaces" do
434 source "shorewall-interfaces.erb"
438 notifies :restart, "service[shorewall]"
441 template "/etc/shorewall/hosts" do
442 source "shorewall-hosts.erb"
446 variables :zones => zones
447 notifies :restart, "service[shorewall]"
450 template "/etc/shorewall/conntrack" do
451 source "shorewall-conntrack.erb"
455 notifies :restart, "service[shorewall]"
456 only_if { node[:networking][:firewall][:raw] }
459 template "/etc/shorewall/policy" do
460 source "shorewall-policy.erb"
464 notifies :restart, "service[shorewall]"
467 template "/etc/shorewall/rules" do
468 source "shorewall-rules.erb"
472 variables :family => "inet"
473 notifies :restart, "service[shorewall]"
476 template "/etc/shorewall/stoppedrules" do
477 source "shorewall-stoppedrules.erb"
481 notifies :restart, "service[shorewall]"
484 if node[:networking][:firewall][:enabled]
485 service "shorewall" do
486 action [:enable, :start]
487 supports :restart => true
488 status_command "shorewall status"
492 service "shorewall" do
493 action [:disable, :stop]
494 supports :restart => true
495 status_command "shorewall status"
500 template "/etc/logrotate.d/shorewall" do
501 source "logrotate.shorewall.erb"
505 variables :name => "shorewall"
508 firewall_rule "limit-icmp-echo" do
514 dest_ports "echo-request"
515 rate_limit "s:1/sec:5"
518 if node[:networking][:wireguard][:enabled]
519 wireguard_source = if node[:roles].include?("gateway")
525 firewall_rule "accept-wireguard" do
527 source wireguard_source
535 file "/etc/shorewall/masq" do
539 file "/etc/shorewall/masq.bak" do
543 if node[:roles].include?("gateway")
544 template "/etc/shorewall/snat" do
545 source "shorewall-snat.erb"
549 notifies :restart, "service[shorewall]"
552 file "/etc/shorewall/snat" do
554 notifies :restart, "service[shorewall]"
558 unless node.interfaces(:family => :inet6).empty?
561 template "/etc/default/shorewall6" do
562 source "shorewall-default.erb"
566 notifies :restart, "service[shorewall6]"
569 template "/etc/shorewall6/shorewall6.conf" do
570 source "shorewall6.conf.erb"
574 notifies :restart, "service[shorewall6]"
577 template "/etc/shorewall6/zones" do
578 source "shorewall-zones.erb"
582 variables :type => "ipv6"
583 notifies :restart, "service[shorewall6]"
586 template "/etc/shorewall6/interfaces" do
587 source "shorewall6-interfaces.erb"
591 notifies :restart, "service[shorewall6]"
594 template "/etc/shorewall6/hosts" do
595 source "shorewall6-hosts.erb"
599 variables :zones => zones
600 notifies :restart, "service[shorewall6]"
603 template "/etc/shorewall6/conntrack" do
604 source "shorewall-conntrack.erb"
608 notifies :restart, "service[shorewall6]"
609 only_if { node[:networking][:firewall][:raw] }
612 template "/etc/shorewall6/policy" do
613 source "shorewall-policy.erb"
617 notifies :restart, "service[shorewall6]"
620 template "/etc/shorewall6/rules" do
621 source "shorewall-rules.erb"
625 variables :family => "inet6"
626 notifies :restart, "service[shorewall6]"
629 if node[:networking][:firewall][:enabled]
630 service "shorewall6" do
631 action [:enable, :start]
632 supports :restart => true
633 status_command "shorewall6 status"
637 service "shorewall6" do
638 action [:disable, :stop]
639 supports :restart => true
640 status_command "shorewall6 status"
645 template "/etc/logrotate.d/shorewall6" do
646 source "logrotate.shorewall.erb"
650 variables :name => "shorewall6"
653 firewall_rule "limit-icmp6-echo" do
659 dest_ports "echo-request"
660 rate_limit "s:1/sec:5"
664 firewall_rule "accept-http" do
670 rate_limit node[:networking][:firewall][:http_rate_limit]
671 connection_limit node[:networking][:firewall][:http_connection_limit]
674 firewall_rule "accept-https" do
680 rate_limit node[:networking][:firewall][:http_rate_limit]
681 connection_limit node[:networking][:firewall][:http_connection_limit]