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["mtu"] = interface[:mtu]
84 deviceplan["interfaces"] = interface[:bond][:slaves].to_a
86 deviceplan["parameters"] = {
87 "mode" => interface[:bond][:mode] || "active-backup",
88 "primary" => interface[:bond][:slaves].first,
89 "mii-monitor-interval" => interface[:bond][:miimon] || 100,
90 "down-delay" => interface[:bond][:downdelay] || 200,
91 "up-delay" => interface[:bond][:updelay] || 200
94 deviceplan["parameters"]["transmit-hash-policy"] = interface[:bond][:xmithashpolicy] if interface[:bond][:xmithashpolicy]
95 deviceplan["parameters"]["lacp-rate"] = interface[:bond][:lacprate] if interface[:bond][:lacprate]
98 if interface[:gateway]
99 if interface[:family] == "inet"
100 default_route = "0.0.0.0/0"
101 elsif interface[:family] == "inet6"
102 default_route = "::/0"
105 deviceplan["routes"].push(
106 "to" => default_route,
107 "via" => interface[:gateway],
108 "metric" => interface[:metric],
112 # This ordering relies on systemd-networkd adding routes
113 # in reverse order and will need moving before the previous
114 # route once that is fixed:
116 # https://github.com/systemd/systemd/issues/5430
117 # https://github.com/systemd/systemd/pull/10938
118 if interface[:family] == "inet6" &&
119 !interface[:network].include?(interface[:gateway]) &&
120 !IPAddr.new("fe80::/64").include?(interface[:gateway])
121 deviceplan["routes"].push(
122 "to" => interface[:gateway],
128 node.rm(:networking, :interfaces, name)
132 netplan["network"]["bonds"].each_value do |bond|
133 bond["interfaces"].each do |interface|
134 netplan["network"]["ethernets"][interface] ||= { "accept-ra" => false }
138 netplan["network"]["vlans"].each_value do |vlan|
139 unless vlan["link"] =~ /^bond\d+$/
140 netplan["network"]["ethernets"][vlan["link"]] ||= { "accept-ra" => false }
144 file "/etc/netplan/01-netcfg.yaml" do
148 file "/etc/netplan/50-cloud-init.yaml" do
152 file "/etc/netplan/99-chef.yaml" do
156 content YAML.dump(netplan)
159 package "cloud-init" do
163 execute "hostname" do
165 command "/bin/hostname -F /etc/hostname"
168 template "/etc/hostname" do
169 source "hostname.erb"
173 notifies :run, "execute[hostname]"
176 template "/etc/hosts" do
183 service "systemd-resolved" do
184 action [:enable, :start]
187 directory "/etc/systemd/resolved.conf.d" do
193 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
194 source "resolved.conf.erb"
198 notifies :restart, "service[systemd-resolved]"
201 node.interfaces(:role => :internal) do |interface|
202 if interface[:gateway] && interface[:gateway] != interface[:address]
203 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
204 next unless gateway[:openvpn]
206 gateway[:openvpn][:tunnels].each_value do |tunnel|
207 if tunnel[:peer][:address] # ~FC023
208 route tunnel[:peer][:address] do
209 netmask "255.255.255.255"
210 gateway interface[:gateway]
211 device interface[:interface]
215 next unless tunnel[:peer][:networks]
217 tunnel[:peer][:networks].each do |network|
218 route network[:address] do
219 netmask network[:netmask]
220 gateway interface[:gateway]
221 device interface[:interface]
231 search(:node, "networking:interfaces").collect do |n|
232 next if n[:fqdn] == node[:fqdn]
234 n.interfaces.each do |interface|
235 next unless interface[:role] == "external" && interface[:zone]
237 zones[interface[:zone]] ||= {}
238 zones[interface[:zone]][interface[:family]] ||= []
239 zones[interface[:zone]][interface[:family]] << interface[:address]
245 template "/etc/default/shorewall" do
246 source "shorewall-default.erb"
250 notifies :restart, "service[shorewall]"
253 template "/etc/shorewall/shorewall.conf" do
254 source "shorewall.conf.erb"
258 notifies :restart, "service[shorewall]"
261 template "/etc/shorewall/zones" do
262 source "shorewall-zones.erb"
266 variables :type => "ipv4"
267 notifies :restart, "service[shorewall]"
270 template "/etc/shorewall/interfaces" do
271 source "shorewall-interfaces.erb"
275 notifies :restart, "service[shorewall]"
278 template "/etc/shorewall/hosts" do
279 source "shorewall-hosts.erb"
283 variables :zones => zones
284 notifies :restart, "service[shorewall]"
287 template "/etc/shorewall/conntrack" do
288 source "shorewall-conntrack.erb"
292 notifies :restart, "service[shorewall]"
293 only_if { node[:networking][:firewall][:raw] }
296 template "/etc/shorewall/policy" do
297 source "shorewall-policy.erb"
301 notifies :restart, "service[shorewall]"
304 template "/etc/shorewall/rules" do
305 source "shorewall-rules.erb"
309 variables :family => "inet"
310 notifies :restart, "service[shorewall]"
313 service "shorewall" do
314 action [:enable, :start]
315 supports :restart => true
316 status_command "shorewall status"
319 template "/etc/logrotate.d/shorewall" do
320 source "logrotate.shorewall.erb"
324 variables :name => "shorewall"
327 firewall_rule "limit-icmp-echo" do
333 dest_ports "echo-request"
334 rate_limit "s:1/sec:5"
337 %w[ucl ams bm].each do |zone|
338 firewall_rule "accept-openvpn-#{zone}" do
343 dest_ports "1194:1197"
344 source_ports "1194:1197"
348 if node[:roles].include?("gateway")
349 template "/etc/shorewall/masq" do
350 source "shorewall-masq.erb"
354 notifies :restart, "service[shorewall]"
357 file "/etc/shorewall/masq" do
359 notifies :restart, "service[shorewall]"
363 unless node.interfaces(:family => :inet6).empty?
366 template "/etc/default/shorewall6" do
367 source "shorewall-default.erb"
371 notifies :restart, "service[shorewall6]"
374 template "/etc/shorewall6/shorewall6.conf" do
375 source "shorewall6.conf.erb"
379 notifies :restart, "service[shorewall6]"
382 template "/etc/shorewall6/zones" do
383 source "shorewall-zones.erb"
387 variables :type => "ipv6"
388 notifies :restart, "service[shorewall6]"
391 template "/etc/shorewall6/interfaces" do
392 source "shorewall6-interfaces.erb"
396 notifies :restart, "service[shorewall6]"
399 template "/etc/shorewall6/hosts" do
400 source "shorewall6-hosts.erb"
404 variables :zones => zones
405 notifies :restart, "service[shorewall6]"
408 template "/etc/shorewall6/conntrack" do
409 source "shorewall-conntrack.erb"
413 notifies :restart, "service[shorewall6]"
414 only_if { node[:networking][:firewall][:raw] }
417 template "/etc/shorewall6/policy" do
418 source "shorewall-policy.erb"
422 notifies :restart, "service[shorewall6]"
425 template "/etc/shorewall6/rules" do
426 source "shorewall-rules.erb"
430 variables :family => "inet6"
431 notifies :restart, "service[shorewall6]"
434 service "shorewall6" do
435 action [:enable, :start]
436 supports :restart => true
437 status_command "shorewall6 status"
440 template "/etc/logrotate.d/shorewall6" do
441 source "logrotate.shorewall.erb"
445 variables :name => "shorewall6"
448 firewall_rule "limit-icmp6-echo" do
454 dest_ports "echo-request"
455 rate_limit "s:1/sec:5"
459 firewall_rule "accept-http" do
465 rate_limit node[:networking][:firewall][:http_rate_limit]
466 connection_limit node[:networking][:firewall][:http_connection_limit]
469 firewall_rule "accept-https" do
475 rate_limit node[:networking][:firewall][:http_rate_limit]
476 connection_limit node[:networking][:firewall][:http_connection_limit]