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
198 not_if { ENV["TEST_KITCHEN"] }
201 service "systemd-resolved" do
202 action [:enable, :start]
205 directory "/etc/systemd/resolved.conf.d" do
211 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
212 source "resolved.conf.erb"
216 notifies :restart, "service[systemd-resolved]", :immediately
219 if node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"]
220 mount "/etc/resolv.conf" do
222 device node[:filesystem][:by_mountpoint][:"/etc/resolv.conf"][:devices].first
226 link "/etc/resolv.conf" do
227 to "../run/systemd/resolve/stub-resolv.conf"
230 if node[:networking][:tcp_fastopen_key]
231 fastopen_keys = data_bag_item("networking", "fastopen")
233 node.normal[:sysctl][:tcp_fastopen] = {
234 :comment => "Set shared key for TCP fast open",
236 "net.ipv4.tcp_fastopen_key" => fastopen_keys[node[:networking][:tcp_fastopen_key]]
241 node.interfaces(:role => :internal) do |interface|
242 if interface[:gateway] && interface[:gateway] != interface[:address]
243 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
244 next unless gateway[:openvpn]
246 gateway[:openvpn][:tunnels].each_value do |tunnel|
247 if tunnel[:peer][:address]
248 route tunnel[:peer][:address] do
249 netmask "255.255.255.255"
250 gateway interface[:gateway]
251 device interface[:interface]
255 next unless tunnel[:peer][:networks]
257 tunnel[:peer][:networks].each do |network|
258 route network[:address] do
259 netmask network[:netmask]
260 gateway interface[:gateway]
261 device interface[:interface]
271 search(:node, "networking:interfaces").collect do |n|
272 next if n[:fqdn] == node[:fqdn]
274 n.interfaces.each do |interface|
275 next unless interface[:role] == "external" && interface[:zone]
277 zones[interface[:zone]] ||= {}
278 zones[interface[:zone]][interface[:family]] ||= []
279 zones[interface[:zone]][interface[:family]] << interface[:address]
285 template "/etc/default/shorewall" do
286 source "shorewall-default.erb"
290 notifies :restart, "service[shorewall]"
293 template "/etc/shorewall/shorewall.conf" do
294 source "shorewall.conf.erb"
298 notifies :restart, "service[shorewall]"
301 template "/etc/shorewall/zones" do
302 source "shorewall-zones.erb"
306 variables :type => "ipv4"
307 notifies :restart, "service[shorewall]"
310 template "/etc/shorewall/interfaces" do
311 source "shorewall-interfaces.erb"
315 notifies :restart, "service[shorewall]"
318 template "/etc/shorewall/hosts" do
319 source "shorewall-hosts.erb"
323 variables :zones => zones
324 notifies :restart, "service[shorewall]"
327 template "/etc/shorewall/conntrack" do
328 source "shorewall-conntrack.erb"
332 notifies :restart, "service[shorewall]"
333 only_if { node[:networking][:firewall][:raw] }
336 template "/etc/shorewall/policy" do
337 source "shorewall-policy.erb"
341 notifies :restart, "service[shorewall]"
344 template "/etc/shorewall/rules" do
345 source "shorewall-rules.erb"
349 variables :family => "inet"
350 notifies :restart, "service[shorewall]"
353 service "shorewall" do
354 action [:enable, :start]
355 supports :restart => true
356 status_command "shorewall status"
359 template "/etc/logrotate.d/shorewall" do
360 source "logrotate.shorewall.erb"
364 variables :name => "shorewall"
367 firewall_rule "limit-icmp-echo" do
373 dest_ports "echo-request"
374 rate_limit "s:1/sec:5"
377 %w[ucl ams bm].each do |zone|
378 firewall_rule "accept-openvpn-#{zone}" do
383 dest_ports "1194:1197"
384 source_ports "1194:1197"
388 if node[:roles].include?("gateway")
389 template "/etc/shorewall/masq" do
390 source "shorewall-masq.erb"
394 notifies :restart, "service[shorewall]"
397 file "/etc/shorewall/masq" do
399 notifies :restart, "service[shorewall]"
403 unless node.interfaces(:family => :inet6).empty?
406 template "/etc/default/shorewall6" do
407 source "shorewall-default.erb"
411 notifies :restart, "service[shorewall6]"
414 template "/etc/shorewall6/shorewall6.conf" do
415 source "shorewall6.conf.erb"
419 notifies :restart, "service[shorewall6]"
422 template "/etc/shorewall6/zones" do
423 source "shorewall-zones.erb"
427 variables :type => "ipv6"
428 notifies :restart, "service[shorewall6]"
431 template "/etc/shorewall6/interfaces" do
432 source "shorewall6-interfaces.erb"
436 notifies :restart, "service[shorewall6]"
439 template "/etc/shorewall6/hosts" do
440 source "shorewall6-hosts.erb"
444 variables :zones => zones
445 notifies :restart, "service[shorewall6]"
448 template "/etc/shorewall6/conntrack" do
449 source "shorewall-conntrack.erb"
453 notifies :restart, "service[shorewall6]"
454 only_if { node[:networking][:firewall][:raw] }
457 template "/etc/shorewall6/policy" do
458 source "shorewall-policy.erb"
462 notifies :restart, "service[shorewall6]"
465 template "/etc/shorewall6/rules" do
466 source "shorewall-rules.erb"
470 variables :family => "inet6"
471 notifies :restart, "service[shorewall6]"
474 service "shorewall6" do
475 action [:enable, :start]
476 supports :restart => true
477 status_command "shorewall6 status"
480 template "/etc/logrotate.d/shorewall6" do
481 source "logrotate.shorewall.erb"
485 variables :name => "shorewall6"
488 firewall_rule "limit-icmp6-echo" do
494 dest_ports "echo-request"
495 rate_limit "s:1/sec:5"
499 firewall_rule "accept-http" do
505 rate_limit node[:networking][:firewall][:http_rate_limit]
506 connection_limit node[:networking][:firewall][:http_connection_limit]
509 firewall_rule "accept-https" do
515 rate_limit node[:networking][:firewall][:http_rate_limit]
516 connection_limit node[:networking][:firewall][:http_connection_limit]