2 # Cookbook Name:: 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]
29 "renderer" => "networkd",
36 node[:networking][:interfaces].each do |name, interface|
37 if interface[:interface]
38 if interface[:role] && (role = node[:networking][:roles][interface[:role]])
39 if role[interface[:family]]
40 node.normal[:networking][:interfaces][name][:prefix] = role[interface[:family]][:prefix]
41 node.normal[:networking][:interfaces][name][:gateway] = role[interface[:family]][:gateway]
44 node.normal[:networking][:interfaces][name][:metric] = role[:metric]
45 node.normal[:networking][:interfaces][name][:zone] = role[:zone]
48 prefix = node[:networking][:interfaces][name][:prefix]
50 node.normal[:networking][:interfaces][name][:netmask] = (~IPAddr.new(interface[:address]).mask(0)).mask(prefix)
51 node.normal[:networking][:interfaces][name][:network] = IPAddr.new(interface[:address]).mask(prefix)
53 interface = node[:networking][:interfaces][name]
55 deviceplan = if interface[:interface] =~ /^(.*)\.(\d+)$/
56 netplan["network"]["vlans"][interface[:interface]] ||= {
57 "id" => Regexp.last_match(2).to_i,
58 "link" => Regexp.last_match(1),
63 elsif interface[:interface] =~ /^bond\d+$/
64 netplan["network"]["bonds"][interface[:interface]] ||= {
70 netplan["network"]["ethernets"][interface[:interface]] ||= {
77 deviceplan["addresses"].push("#{interface[:address]}/#{prefix}")
80 deviceplan["interfaces"] = interface[:bond][:slaves].to_a
82 deviceplan["parameters"] = {
83 "mode" => interface[:bond][:mode] || "active-backup",
84 "primary" => interface[:bond][:slaves].first,
85 "mii-monitor-interval" => interface[:bond][:miimon] || 100,
86 "down-delay" => interface[:bond][:downdelay] || 200,
87 "up-delay" => interface[:bond][:updelay] || 200
90 deviceplan["parameters"]["transmit-hash-policy"] = interface[:bond][:xmithashpolicy] if interface[:bond][:xmithashpolicy]
91 deviceplan["parameters"]["lacp-rate"] = interface[:bond][:lacprate] if interface[:bond][:lacprate]
94 if interface[:gateway]
95 if interface[:family] == "inet"
96 default_route = "0.0.0.0/0"
97 elsif interface[:family] == "inet6"
98 default_route = "::/0"
101 deviceplan["routes"].push(
102 "to" => default_route,
103 "via" => interface[:gateway],
104 "metric" => interface[:metric],
108 # This ordering relies on systemd-networkd adding routes
109 # in reverse order and will need moving before the previous
110 # route once that is fixed:
112 # https://github.com/systemd/systemd/issues/5430
113 # https://github.com/systemd/systemd/pull/10938
114 if interface[:family] == "inet6" &&
115 !interface[:network].include?(interface[:gateway]) &&
116 !IPAddr.new("fe80::/64").include?(interface[:gateway])
117 deviceplan["routes"].push(
118 "to" => interface[:gateway],
124 node.rm(:networking, :interfaces, name)
128 netplan["network"]["bonds"].each_value do |bond|
129 bond["interfaces"].each do |interface|
130 netplan["network"]["ethernets"][interface] ||= { "accept-ra" => false }
134 netplan["network"]["vlans"].each_value do |vlan|
135 unless vlan["link"] =~ /^bond\d+$/
136 netplan["network"]["ethernets"][vlan["link"]] ||= { "accept-ra" => false }
140 file "/etc/netplan/01-netcfg.yaml" do
144 file "/etc/netplan/50-cloud-init.yaml" do
148 file "/etc/netplan/99-chef.yaml" do
152 content YAML.dump(netplan)
155 package "cloud-init" do
159 execute "hostname" do
161 command "/bin/hostname -F /etc/hostname"
164 template "/etc/hostname" do
165 source "hostname.erb"
169 notifies :run, "execute[hostname]"
172 template "/etc/hosts" do
179 service "systemd-resolved" do
180 action [:enable, :start]
183 directory "/etc/systemd/resolved.conf.d" do
189 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
190 source "resolved.conf.erb"
194 notifies :restart, "service[systemd-resolved]"
197 node.interfaces(:role => :internal) do |interface|
198 if interface[:gateway] && interface[:gateway] != interface[:address]
199 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
200 next unless gateway[:openvpn]
202 gateway[:openvpn][:tunnels].each_value do |tunnel|
203 if tunnel[:peer][:address] # ~FC023
204 route tunnel[:peer][:address] do
205 netmask "255.255.255.255"
206 gateway interface[:gateway]
207 device interface[:interface]
211 next unless tunnel[:peer][:networks]
213 tunnel[:peer][:networks].each do |network|
214 route network[:address] do
215 netmask network[:netmask]
216 gateway interface[:gateway]
217 device interface[:interface]
227 search(:node, "networking:interfaces").collect do |n|
228 next if n[:fqdn] == node[:fqdn]
230 n.interfaces.each do |interface|
231 next unless interface[:role] == "external" && interface[:zone]
233 zones[interface[:zone]] ||= {}
234 zones[interface[:zone]][interface[:family]] ||= []
235 zones[interface[:zone]][interface[:family]] << interface[:address]
241 template "/etc/default/shorewall" do
242 source "shorewall-default.erb"
246 notifies :restart, "service[shorewall]"
249 template "/etc/shorewall/shorewall.conf" do
250 source "shorewall.conf.erb"
254 notifies :restart, "service[shorewall]"
257 template "/etc/shorewall/zones" do
258 source "shorewall-zones.erb"
262 variables :type => "ipv4"
263 notifies :restart, "service[shorewall]"
266 template "/etc/shorewall/interfaces" do
267 source "shorewall-interfaces.erb"
271 notifies :restart, "service[shorewall]"
274 template "/etc/shorewall/hosts" do
275 source "shorewall-hosts.erb"
279 variables :zones => zones
280 notifies :restart, "service[shorewall]"
283 template "/etc/shorewall/conntrack" do
284 source "shorewall-conntrack.erb"
288 notifies :restart, "service[shorewall]"
289 only_if { node[:networking][:firewall][:raw] }
292 template "/etc/shorewall/policy" do
293 source "shorewall-policy.erb"
297 notifies :restart, "service[shorewall]"
300 template "/etc/shorewall/rules" do
301 source "shorewall-rules.erb"
305 variables :family => "inet"
306 notifies :restart, "service[shorewall]"
309 service "shorewall" do
310 action [:enable, :start]
311 supports :restart => true
312 status_command "shorewall status"
315 template "/etc/logrotate.d/shorewall" do
316 source "logrotate.shorewall.erb"
320 variables :name => "shorewall"
323 firewall_rule "limit-icmp-echo" do
329 dest_ports "echo-request"
330 rate_limit "s:1/sec:5"
333 %w[ucl ams bm].each do |zone|
334 firewall_rule "accept-openvpn-#{zone}" do
339 dest_ports "1194:1197"
340 source_ports "1194:1197"
344 if node[:roles].include?("gateway")
345 template "/etc/shorewall/masq" do
346 source "shorewall-masq.erb"
350 notifies :restart, "service[shorewall]"
353 file "/etc/shorewall/masq" do
355 notifies :restart, "service[shorewall]"
359 unless node.interfaces(:family => :inet6).empty?
362 template "/etc/default/shorewall6" do
363 source "shorewall-default.erb"
367 notifies :restart, "service[shorewall6]"
370 template "/etc/shorewall6/shorewall6.conf" do
371 source "shorewall6.conf.erb"
375 notifies :restart, "service[shorewall6]"
378 template "/etc/shorewall6/zones" do
379 source "shorewall-zones.erb"
383 variables :type => "ipv6"
384 notifies :restart, "service[shorewall6]"
387 template "/etc/shorewall6/interfaces" do
388 source "shorewall6-interfaces.erb"
392 notifies :restart, "service[shorewall6]"
395 template "/etc/shorewall6/hosts" do
396 source "shorewall6-hosts.erb"
400 variables :zones => zones
401 notifies :restart, "service[shorewall6]"
404 template "/etc/shorewall6/conntrack" do
405 source "shorewall-conntrack.erb"
409 notifies :restart, "service[shorewall6]"
410 only_if { node[:networking][:firewall][:raw] }
413 template "/etc/shorewall6/policy" do
414 source "shorewall-policy.erb"
418 notifies :restart, "service[shorewall6]"
421 template "/etc/shorewall6/rules" do
422 source "shorewall-rules.erb"
426 variables :family => "inet6"
427 notifies :restart, "service[shorewall6]"
430 service "shorewall6" do
431 action [:enable, :start]
432 supports :restart => true
433 status_command "shorewall6 status"
436 template "/etc/logrotate.d/shorewall6" do
437 source "logrotate.shorewall.erb"
441 variables :name => "shorewall6"
444 firewall_rule "limit-icmp6-echo" do
450 dest_ports "echo-request"
451 rate_limit "s:1/sec:5"
455 firewall_rule "accept-http" do
461 rate_limit node[:networking][:firewall][:http_rate_limit]
462 connection_limit node[:networking][:firewall][:http_connection_limit]
465 firewall_rule "accept-https" do
471 rate_limit node[:networking][:firewall][:http_rate_limit]
472 connection_limit node[:networking][:firewall][:http_connection_limit]