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 "mii-monitor-interval" => interface[:bond][:miimon] || 100,
98 "down-delay" => interface[:bond][:downdelay] || 200,
99 "up-delay" => interface[:bond][:updelay] || 200
102 deviceplan["parameters"]["primary"] = interface[:bond][:slaves].first if deviceplan["parameters"]["mode"] == "active-backup"
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
199 options "--no-install-recommends"
202 directory "/var/lib/systemd/wireguard" do
204 group "systemd-network"
209 file "/var/lib/systemd/wireguard/private.key" do
210 action :create_if_missing
212 group "systemd-network"
214 content %x(wg genkey)
218 node.default[:networking][:wireguard][:public_key] = %x(wg pubkey < /var/lib/systemd/wireguard/private.key).chomp
220 file "/var/lib/systemd/wireguard/preshared.key" do
221 action :create_if_missing
223 group "systemd-network"
225 content keys["wireguard"]
228 if node[:roles].include?("gateway")
229 search(:node, "roles:gateway") do |gateway|
230 next if gateway.name == node.name
231 next unless gateway[:networking][:wireguard] && gateway[:networking][:wireguard][:enabled]
233 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
234 "#{interface[:network]}/#{interface[:prefix]}"
237 node.default[:networking][:wireguard][:peers] << {
238 :public_key => gateway[:networking][:wireguard][:public_key],
239 :allowed_ips => allowed_ips,
240 :endpoint => "#{gateway.name}:51820"
244 search(:node, "roles:prometheus") do |server|
245 allowed_ips = server.interfaces(:role => :internal).map do |interface|
246 "#{interface[:network]}/#{interface[:prefix]}"
249 if server[:networking][:private_address]
250 allowed_ips << "#{server[:networking][:private_address]}/32"
253 node.default[:networking][:wireguard][:peers] << {
254 :public_key => server[:networking][:wireguard][:public_key],
255 :allowed_ips => allowed_ips,
256 :endpoint => "#{server.name}:51820"
260 node.default[:networking][:wireguard][:peers] << {
261 :public_key => "7Oj9ufNlgidyH/xDc+aHQKMjJPqTmD/ab13agMh6AxA=",
262 :allowed_ips => "10.0.16.1/32",
263 :endpoint => "gate.compton.nu:51820"
267 node.default[:networking][:wireguard][:peers] << {
268 :public_key => "RofATnvlWxP3mt87+QKRXFE5MVxtoCcTsJ+yftZYEE4=",
269 :allowed_ips => "10.89.122.1/32",
270 :endpoint => "gate.firefishy.com:51820"
274 node.default[:networking][:wireguard][:peers] << {
275 :public_key => "YbUkREE9TAmomqgL/4Fh2e5u2Hh7drN/2o5qg3ndRxg=",
276 :allowed_ips => "10.89.123.1/32",
277 :endpoint => "roaming.firefishy.com:51820"
279 elsif node[:roles].include?("shenron")
280 search(:node, "roles:gateway") do |gateway|
281 allowed_ips = gateway.interfaces(:role => :internal).map do |interface|
282 "#{interface[:network]}/#{interface[:prefix]}"
285 node.default[:networking][:wireguard][:peers] << {
286 :public_key => gateway[:networking][:wireguard][:public_key],
287 :allowed_ips => allowed_ips,
288 :endpoint => "#{gateway.name}:51820"
293 template "/etc/systemd/network/wireguard.netdev" do
294 source "wireguard.netdev.erb"
296 group "systemd-network"
300 template "/etc/systemd/network/wireguard.network" do
301 source "wireguard.network.erb"
307 if node[:lsb][:release].to_f < 20.04
308 execute "ip-link-delete-wg0" do
310 command "ip link delete wg0"
311 subscribes :run, "template[/etc/systemd/network/wireguard.netdev]"
312 only_if { ::File.exist?("/sys/class/net/wg0") }
315 service "systemd-networkd" do
317 subscribes :restart, "template[/etc/systemd/network/wireguard.netdev]"
318 subscribes :restart, "template[/etc/systemd/network/wireguard.network]"
322 execute "networkctl-delete-wg0" do
324 command "networkctl delete wg0"
325 subscribes :run, "template[/etc/systemd/network/wireguard.netdev]"
326 only_if { ::File.exist?("/sys/class/net/wg0") }
329 execute "networkctl-reload" do
331 command "networkctl reload"
332 subscribes :run, "template[/etc/systemd/network/wireguard.netdev]"
333 subscribes :run, "template[/etc/systemd/network/wireguard.network]"
339 ohai "reload-hostname" do
344 execute "hostnamectl-set-hostname" do
345 command "hostnamectl set-hostname #{node[:networking][:hostname]}"
346 notifies :reload, "ohai[reload-hostname]"
347 not_if { kitchen? || node[:hostnamectl][:static_hostname] == node[:networking][:hostname] }
350 template "/etc/hosts" do
358 service "systemd-resolved" do
359 action [:enable, :start]
362 directory "/etc/systemd/resolved.conf.d" do
368 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
369 source "resolved.conf.erb"
373 notifies :restart, "service[systemd-resolved]", :immediately
376 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
377 execute "umount-resolve-conf" do
378 command "umount -c /etc/resolv.conf"
382 link "/etc/resolv.conf" do
383 to "../run/systemd/resolve/stub-resolv.conf"
386 hosts = { "inet" => [], "inet6" => [] }
388 search(:node, "networking:interfaces").collect do |n|
389 next if n[:fqdn] == node[:fqdn]
391 n.interfaces.each do |interface|
392 next unless interface[:role] == "external"
394 hosts[interface[:family]] << interface[:address]
402 node.interfaces(:role => :external).each do |interface|
403 interfaces << interface[:interface]
406 template "/etc/nftables.conf" do
407 source "nftables.conf.erb"
411 variables :interfaces => interfaces, :hosts => hosts
412 notifies :restart, "service[nftables]"
416 "-/usr/sbin/nft delete table inet filter",
417 "-/usr/sbin/nft delete table inet chef-filter"
420 stop_commands << "-/usr/sbin/nft delete table ip nat" if node[:roles].include?("gateway")
421 stop_commands << "-/usr/sbin/nft delete table ip chef-nat" if node[:roles].include?("gateway")
423 systemd_service "nftables-stop" do
427 exec_stop stop_commands
430 if node[:networking][:firewall][:enabled]
431 service "nftables" do
432 action [:enable, :start]
435 service "nftables" do
436 action [:disable, :stop]
440 if node[:networking][:wireguard][:enabled]
441 firewall_rule "accept-wireguard" do
445 source :osm unless node[:roles].include?("gateway")
451 firewall_rule "accept-http" do
455 dest_ports %w[http https]
456 rate_limit node[:networking][:firewall][:http_rate_limit]
457 connection_limit node[:networking][:firewall][:http_connection_limit]