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.default[:networking][:interfaces][name][:prefix] = role[interface[:family]][:prefix]
43 node.default[:networking][:interfaces][name][:gateway] = role[interface[:family]][:gateway]
44 node.default[:networking][:interfaces][name][:routes] = role[interface[:family]][:routes]
47 node.default[:networking][:interfaces][name][:metric] = role[:metric]
48 node.default[:networking][:interfaces][name][:zone] = role[:zone]
51 if interface[:address]
52 prefix = node[:networking][:interfaces][name][:prefix]
54 node.default[:networking][:interfaces][name][:netmask] = (~IPAddr.new(interface[:address]).mask(0)).mask(prefix)
55 node.default[:networking][:interfaces][name][:network] = IPAddr.new(interface[:address]).mask(prefix)
58 interface = node[:networking][:interfaces][name]
60 deviceplan = if interface[:interface] =~ /^(.*)\.(\d+)$/
61 netplan["network"]["vlans"][interface[:interface]] ||= {
62 "id" => Regexp.last_match(2).to_i,
63 "link" => Regexp.last_match(1),
68 elsif interface[:interface] =~ /^bond\d+$/
69 netplan["network"]["bonds"][interface[:interface]] ||= {
75 netplan["network"]["ethernets"][interface[:interface]] ||= {
82 if interface[:address]
83 deviceplan["addresses"].push("#{interface[:address]}/#{prefix}")
87 deviceplan["mtu"] = interface[:mtu]
91 deviceplan["interfaces"] = interface[:bond][:slaves].to_a
93 deviceplan["parameters"] = {
94 "mode" => interface[:bond][:mode] || "active-backup",
95 "primary" => interface[:bond][:slaves].first,
96 "mii-monitor-interval" => interface[:bond][:miimon] || 100,
97 "down-delay" => interface[:bond][:downdelay] || 200,
98 "up-delay" => interface[:bond][:updelay] || 200
101 deviceplan["parameters"]["transmit-hash-policy"] = interface[:bond][:xmithashpolicy] if interface[:bond][:xmithashpolicy]
102 deviceplan["parameters"]["lacp-rate"] = interface[:bond][:lacprate] if interface[:bond][:lacprate]
105 if interface[:gateway]
106 if interface[:family] == "inet"
107 default_route = "0.0.0.0/0"
108 elsif interface[:family] == "inet6"
109 default_route = "::/0"
112 deviceplan["routes"].push(
113 "to" => default_route,
114 "via" => interface[:gateway],
115 "metric" => interface[:metric],
119 # This ordering relies on systemd-networkd adding routes
120 # in reverse order and will need moving before the previous
121 # route once that is fixed:
123 # https://github.com/systemd/systemd/issues/5430
124 # https://github.com/systemd/systemd/pull/10938
125 if interface[:family] == "inet6" &&
126 !interface[:network].include?(interface[:gateway]) &&
127 !IPAddr.new("fe80::/64").include?(interface[:gateway])
128 deviceplan["routes"].push(
129 "to" => interface[:gateway],
134 if interface[:role] == "internal" && interface[:gateway] != interface[:address]
135 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
136 next unless gateway[:openvpn]
138 gateway[:openvpn][:tunnels].each_value do |tunnel|
139 if tunnel[:peer][:address]
140 deviceplan["routes"].push(
141 "to" => "#{tunnel[:peer][:address]}/32",
142 "via" => interface[:gateway]
145 route tunnel[:peer][:address] do
146 netmask "255.255.255.255"
147 gateway interface[:gateway]
148 device interface[:interface]
152 next unless tunnel[:peer][:networks]
154 tunnel[:peer][:networks].each do |network|
155 prefix = IPAddr.new("#{network[:address]}/#{network[:netmask]}").prefix
157 deviceplan["routes"].push(
158 "to" => "#{network[:address]}/#{prefix}",
159 "via" => interface[:gateway]
162 route network[:address] do
163 netmask network[:netmask]
164 gateway interface[:gateway]
165 device interface[:interface]
173 if interface[:routes]
174 interface[:routes].each do |to, parameters|
179 route["type"] = parameters[:type] if parameters[:type]
180 route["via"] = parameters[:via] if parameters[:via]
181 route["metric"] = parameters[:metric] if parameters[:metric]
183 deviceplan["routes"].push(route)
187 node.rm(:networking, :interfaces, name)
191 netplan["network"]["bonds"].each_value do |bond|
192 bond["interfaces"].each do |interface|
193 netplan["network"]["ethernets"][interface] ||= { "accept-ra" => false }
197 netplan["network"]["vlans"].each_value do |vlan|
198 unless vlan["link"] =~ /^bond\d+$/
199 netplan["network"]["ethernets"][vlan["link"]] ||= { "accept-ra" => false }
203 file "/etc/netplan/01-netcfg.yaml" do
207 file "/etc/netplan/50-cloud-init.yaml" do
211 file "/etc/netplan/99-chef.yaml" do
215 content YAML.dump(netplan)
218 package "cloud-init" do
222 ohai "reload-hostname" do
227 execute "hostnamectl-set-hostname" do
228 command "hostnamectl set-hostname #{node[:networking][:hostname]}"
229 notifies :reload, "ohai[reload-hostname]"
230 not_if { ENV.key?("TEST_KITCHEN") || node[:hostnamectl][:static_hostname] == node[:networking][:hostname] }
233 template "/etc/hosts" do
238 not_if { ENV["TEST_KITCHEN"] }
241 service "systemd-resolved" do
242 action [:enable, :start]
245 directory "/etc/systemd/resolved.conf.d" do
251 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
252 source "resolved.conf.erb"
256 notifies :restart, "service[systemd-resolved]", :immediately
259 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
260 mount "/etc/resolv.conf" do
262 device node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"][:devices].first
266 link "/etc/resolv.conf" do
267 to "../run/systemd/resolve/stub-resolv.conf"
272 search(:node, "networking:interfaces").collect do |n|
273 next if n[:fqdn] == node[:fqdn]
275 n.interfaces.each do |interface|
276 next unless interface[:role] == "external" && interface[:zone]
278 zones[interface[:zone]] ||= {}
279 zones[interface[:zone]][interface[:family]] ||= []
280 zones[interface[:zone]][interface[:family]] << interface[:address]
286 template "/etc/default/shorewall" do
287 source "shorewall-default.erb"
291 notifies :restart, "service[shorewall]"
294 template "/etc/shorewall/shorewall.conf" do
295 source "shorewall.conf.erb"
299 notifies :restart, "service[shorewall]"
302 template "/etc/shorewall/zones" do
303 source "shorewall-zones.erb"
307 variables :type => "ipv4"
308 notifies :restart, "service[shorewall]"
311 template "/etc/shorewall/interfaces" do
312 source "shorewall-interfaces.erb"
316 notifies :restart, "service[shorewall]"
319 template "/etc/shorewall/hosts" do
320 source "shorewall-hosts.erb"
324 variables :zones => zones
325 notifies :restart, "service[shorewall]"
328 template "/etc/shorewall/conntrack" do
329 source "shorewall-conntrack.erb"
333 notifies :restart, "service[shorewall]"
334 only_if { node[:networking][:firewall][:raw] }
337 template "/etc/shorewall/policy" do
338 source "shorewall-policy.erb"
342 notifies :restart, "service[shorewall]"
345 template "/etc/shorewall/rules" do
346 source "shorewall-rules.erb"
350 variables :family => "inet"
351 notifies :restart, "service[shorewall]"
354 service "shorewall" do
355 action [:enable, :start]
356 supports :restart => true
357 status_command "shorewall status"
360 template "/etc/logrotate.d/shorewall" do
361 source "logrotate.shorewall.erb"
365 variables :name => "shorewall"
368 firewall_rule "limit-icmp-echo" do
374 dest_ports "echo-request"
375 rate_limit "s:1/sec:5"
378 %w[ucl ams bm].each do |zone|
379 firewall_rule "accept-openvpn-#{zone}" do
384 dest_ports "1194:1197"
385 source_ports "1194:1197"
389 if node[:roles].include?("gateway")
390 template "/etc/shorewall/masq" do
391 source "shorewall-masq.erb"
395 notifies :restart, "service[shorewall]"
398 file "/etc/shorewall/masq" do
400 notifies :restart, "service[shorewall]"
404 unless node.interfaces(:family => :inet6).empty?
407 template "/etc/default/shorewall6" do
408 source "shorewall-default.erb"
412 notifies :restart, "service[shorewall6]"
415 template "/etc/shorewall6/shorewall6.conf" do
416 source "shorewall6.conf.erb"
420 notifies :restart, "service[shorewall6]"
423 template "/etc/shorewall6/zones" do
424 source "shorewall-zones.erb"
428 variables :type => "ipv6"
429 notifies :restart, "service[shorewall6]"
432 template "/etc/shorewall6/interfaces" do
433 source "shorewall6-interfaces.erb"
437 notifies :restart, "service[shorewall6]"
440 template "/etc/shorewall6/hosts" do
441 source "shorewall6-hosts.erb"
445 variables :zones => zones
446 notifies :restart, "service[shorewall6]"
449 template "/etc/shorewall6/conntrack" do
450 source "shorewall-conntrack.erb"
454 notifies :restart, "service[shorewall6]"
455 only_if { node[:networking][:firewall][:raw] }
458 template "/etc/shorewall6/policy" do
459 source "shorewall-policy.erb"
463 notifies :restart, "service[shorewall6]"
466 template "/etc/shorewall6/rules" do
467 source "shorewall-rules.erb"
471 variables :family => "inet6"
472 notifies :restart, "service[shorewall6]"
475 service "shorewall6" do
476 action [:enable, :start]
477 supports :restart => true
478 status_command "shorewall6 status"
481 template "/etc/logrotate.d/shorewall6" do
482 source "logrotate.shorewall.erb"
486 variables :name => "shorewall6"
489 firewall_rule "limit-icmp6-echo" do
495 dest_ports "echo-request"
496 rate_limit "s:1/sec:5"
500 firewall_rule "accept-http" do
506 rate_limit node[:networking][:firewall][:http_rate_limit]
507 connection_limit node[:networking][:firewall][:http_connection_limit]
510 firewall_rule "accept-https" do
516 rate_limit node[:networking][:firewall][:http_rate_limit]
517 connection_limit node[:networking][:firewall][:http_connection_limit]