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]
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 if node[:networking][:tcp_fastopen_key]
202 fastopen_keys = data_bag_item("networking", "fastopen")
204 node.normal[:sysctl][:tcp_fastopen] = {
205 :comment => "Set shared key for TCP fast open",
207 "net.ipv4.tcp_fastopen_key" => fastopen_keys[node[:networking][:tcp_fastopen_key]]
212 node.interfaces(:role => :internal) do |interface|
213 if interface[:gateway] && interface[:gateway] != interface[:address]
214 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
215 next unless gateway[:openvpn]
217 gateway[:openvpn][:tunnels].each_value do |tunnel|
218 if tunnel[:peer][:address]
219 route tunnel[:peer][:address] do
220 netmask "255.255.255.255"
221 gateway interface[:gateway]
222 device interface[:interface]
226 next unless tunnel[:peer][:networks]
228 tunnel[:peer][:networks].each do |network|
229 route network[:address] do
230 netmask network[:netmask]
231 gateway interface[:gateway]
232 device interface[:interface]
242 search(:node, "networking:interfaces").collect do |n|
243 next if n[:fqdn] == node[:fqdn]
245 n.interfaces.each do |interface|
246 next unless interface[:role] == "external" && interface[:zone]
248 zones[interface[:zone]] ||= {}
249 zones[interface[:zone]][interface[:family]] ||= []
250 zones[interface[:zone]][interface[:family]] << interface[:address]
256 template "/etc/default/shorewall" do
257 source "shorewall-default.erb"
261 notifies :restart, "service[shorewall]"
264 template "/etc/shorewall/shorewall.conf" do
265 source "shorewall.conf.erb"
269 notifies :restart, "service[shorewall]"
272 template "/etc/shorewall/zones" do
273 source "shorewall-zones.erb"
277 variables :type => "ipv4"
278 notifies :restart, "service[shorewall]"
281 template "/etc/shorewall/interfaces" do
282 source "shorewall-interfaces.erb"
286 notifies :restart, "service[shorewall]"
289 template "/etc/shorewall/hosts" do
290 source "shorewall-hosts.erb"
294 variables :zones => zones
295 notifies :restart, "service[shorewall]"
298 template "/etc/shorewall/conntrack" do
299 source "shorewall-conntrack.erb"
303 notifies :restart, "service[shorewall]"
304 only_if { node[:networking][:firewall][:raw] }
307 template "/etc/shorewall/policy" do
308 source "shorewall-policy.erb"
312 notifies :restart, "service[shorewall]"
315 template "/etc/shorewall/rules" do
316 source "shorewall-rules.erb"
320 variables :family => "inet"
321 notifies :restart, "service[shorewall]"
324 service "shorewall" do
325 action [:enable, :start]
326 supports :restart => true
327 status_command "shorewall status"
330 template "/etc/logrotate.d/shorewall" do
331 source "logrotate.shorewall.erb"
335 variables :name => "shorewall"
338 firewall_rule "limit-icmp-echo" do
344 dest_ports "echo-request"
345 rate_limit "s:1/sec:5"
348 %w[ucl ams bm].each do |zone|
349 firewall_rule "accept-openvpn-#{zone}" do
354 dest_ports "1194:1197"
355 source_ports "1194:1197"
359 if node[:roles].include?("gateway")
360 template "/etc/shorewall/masq" do
361 source "shorewall-masq.erb"
365 notifies :restart, "service[shorewall]"
368 file "/etc/shorewall/masq" do
370 notifies :restart, "service[shorewall]"
374 unless node.interfaces(:family => :inet6).empty?
377 template "/etc/default/shorewall6" do
378 source "shorewall-default.erb"
382 notifies :restart, "service[shorewall6]"
385 template "/etc/shorewall6/shorewall6.conf" do
386 source "shorewall6.conf.erb"
390 notifies :restart, "service[shorewall6]"
393 template "/etc/shorewall6/zones" do
394 source "shorewall-zones.erb"
398 variables :type => "ipv6"
399 notifies :restart, "service[shorewall6]"
402 template "/etc/shorewall6/interfaces" do
403 source "shorewall6-interfaces.erb"
407 notifies :restart, "service[shorewall6]"
410 template "/etc/shorewall6/hosts" do
411 source "shorewall6-hosts.erb"
415 variables :zones => zones
416 notifies :restart, "service[shorewall6]"
419 template "/etc/shorewall6/conntrack" do
420 source "shorewall-conntrack.erb"
424 notifies :restart, "service[shorewall6]"
425 only_if { node[:networking][:firewall][:raw] }
428 template "/etc/shorewall6/policy" do
429 source "shorewall-policy.erb"
433 notifies :restart, "service[shorewall6]"
436 template "/etc/shorewall6/rules" do
437 source "shorewall-rules.erb"
441 variables :family => "inet6"
442 notifies :restart, "service[shorewall6]"
445 service "shorewall6" do
446 action [:enable, :start]
447 supports :restart => true
448 status_command "shorewall6 status"
451 template "/etc/logrotate.d/shorewall6" do
452 source "logrotate.shorewall.erb"
456 variables :name => "shorewall6"
459 firewall_rule "limit-icmp6-echo" do
465 dest_ports "echo-request"
466 rate_limit "s:1/sec:5"
470 firewall_rule "accept-http" do
476 rate_limit node[:networking][:firewall][:http_rate_limit]
477 connection_limit node[:networking][:firewall][:http_connection_limit]
480 firewall_rule "accept-https" do
486 rate_limit node[:networking][:firewall][:http_rate_limit]
487 connection_limit node[:networking][:firewall][:http_connection_limit]