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 deviceplan = if interface[:interface] =~ /^(.*)\.(\d+)$/
54 netplan["network"]["vlans"][interface[:interface]] ||= {
55 "id" => Regexp.last_match(2).to_i,
56 "link" => Regexp.last_match(1),
61 elsif interface[:interface] =~ /^bond\d+$/
62 netplan["network"]["bonds"][interface[:interface]] ||= {
68 netplan["network"]["ethernets"][interface[:interface]] ||= {
75 deviceplan["addresses"].push("#{interface[:address]}/#{prefix}")
78 deviceplan["interfaces"] = interface[:bond][:slaves].to_a
80 deviceplan["parameters"] = {
81 "mode" => interface[:bond][:mode] || "active-backup",
82 "primary" => interface[:bond][:slaves].first,
83 "mii-monitor-interval" => interface[:bond][:miimon] || 100,
84 "down-delay" => interface[:bond][:downdelay] || 200,
85 "up-delay" => interface[:bond][:updelay] || 200
88 deviceplan["parameters"]["transmit-hash-policy"] = interface[:bond][:xmithashpolicy] if interface[:bond][:xmithashpolicy]
89 deviceplan["parameters"]["lacp-rate"] = interface[:bond][:lacprate] if interface[:bond][:lacprate]
92 if interface[:gateway]
93 if interface[:family] == "inet"
94 default_route = "0.0.0.0/0"
95 elsif interface[:family] == "inet6"
96 default_route = "::/0"
99 deviceplan["routes"].push(
100 "to" => default_route,
101 "via" => interface[:gateway],
102 "metric" => interface[:metric],
106 # This ordering relies on systemd-networkd adding routes
107 # in reverse order and will need moving before the previous
108 # route once that is fixed:
110 # https://github.com/systemd/systemd/issues/5430
111 # https://github.com/systemd/systemd/pull/10938
112 if interface[:family] == "inet6" &&
113 !interface[:network].include?(interface[:gateway]) &&
114 !IPAddr.new("fe80::/64").include?(interface[:gateway])
115 deviceplan["routes"].push(
116 "to" => interface[:gateway],
122 node.rm(:networking, :interfaces, name)
126 netplan["network"]["bonds"].each_value do |bond|
127 bond["interfaces"].each do |interface|
128 netplan["network"]["ethernets"][interface] ||= { "accept-ra" => false }
132 netplan["network"]["vlans"].each_value do |vlan|
133 unless vlan["link"] =~ /^bond\d+$/
134 netplan["network"]["ethernets"][vlan["link"]] ||= { "accept-ra" => false }
138 file "/etc/netplan/01-netcfg.yaml" do
142 file "/etc/netplan/50-cloud-init.yaml" do
146 file "/etc/netplan/99-chef.yaml" do
150 content YAML.dump(netplan)
153 package "cloud-init" do
157 execute "hostname" do
159 command "/bin/hostname -F /etc/hostname"
162 template "/etc/hostname" do
163 source "hostname.erb"
167 notifies :run, "execute[hostname]"
170 template "/etc/hosts" do
177 if node[:networking][:resolved]
178 service "systemd-resolved" do
179 action [:enable, :start]
182 directory "/etc/systemd/resolved.conf.d" do
188 template "/etc/systemd/resolved.conf.d/99-chef.conf" do
189 source "resolved.conf.erb"
193 notifies :restart, "service[systemd-resolved]"
196 file "/etc/resolv.conf" do
198 not_if { ::File.symlink?("/etc/resolv.conf") }
201 link "/etc/resolv.conf" do
202 to "../run/systemd/resolve/stub-resolv.conf"
205 package "resolvconf" do
209 unless node[:networking][:nameservers].empty?
210 link "/etc/resolv.conf" do
213 to "/run/resolvconf/resolv.conf"
214 only_if { File.symlink?("/etc/resolv.conf") }
217 template "/etc/resolv.conf" do
218 source "resolv.conf.erb"
226 node.interfaces(:role => :internal) do |interface|
227 if interface[:gateway] && interface[:gateway] != interface[:address]
228 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
229 next unless gateway[:openvpn]
231 gateway[:openvpn][:tunnels].each_value do |tunnel|
232 if tunnel[:peer][:address] # ~FC023
233 route tunnel[:peer][:address] do
234 netmask "255.255.255.255"
235 gateway interface[:gateway]
236 device interface[:interface]
240 next unless tunnel[:peer][:networks]
242 tunnel[:peer][:networks].each do |network|
243 route network[:address] do
244 netmask network[:netmask]
245 gateway interface[:gateway]
246 device interface[:interface]
256 search(:node, "networking:interfaces").collect do |n|
257 next if n[:fqdn] == node[:fqdn]
259 n.interfaces.each do |interface|
260 next unless interface[:role] == "external" && interface[:zone]
262 zones[interface[:zone]] ||= {}
263 zones[interface[:zone]][interface[:family]] ||= []
264 zones[interface[:zone]][interface[:family]] << interface[:address]
270 template "/etc/default/shorewall" do
271 source "shorewall-default.erb"
275 notifies :restart, "service[shorewall]"
278 template "/etc/shorewall/shorewall.conf" do
279 source "shorewall.conf.erb"
283 notifies :restart, "service[shorewall]"
286 template "/etc/shorewall/zones" do
287 source "shorewall-zones.erb"
291 variables :type => "ipv4"
292 notifies :restart, "service[shorewall]"
295 template "/etc/shorewall/interfaces" do
296 source "shorewall-interfaces.erb"
300 notifies :restart, "service[shorewall]"
303 template "/etc/shorewall/hosts" do
304 source "shorewall-hosts.erb"
308 variables :zones => zones
309 notifies :restart, "service[shorewall]"
312 template "/etc/shorewall/conntrack" do
313 source "shorewall-conntrack.erb"
317 notifies :restart, "service[shorewall]"
318 only_if { node[:networking][:firewall][:raw] }
321 template "/etc/shorewall/policy" do
322 source "shorewall-policy.erb"
326 notifies :restart, "service[shorewall]"
329 template "/etc/shorewall/rules" do
330 source "shorewall-rules.erb"
334 variables :family => "inet"
335 notifies :restart, "service[shorewall]"
338 service "shorewall" do
339 action [:enable, :start]
340 supports :restart => true
341 status_command "shorewall status"
344 template "/etc/logrotate.d/shorewall" do
345 source "logrotate.shorewall.erb"
349 variables :name => "shorewall"
352 firewall_rule "limit-icmp-echo" do
358 dest_ports "echo-request"
359 rate_limit "s:1/sec:5"
362 %w[ucl ams bm].each do |zone|
363 firewall_rule "accept-openvpn-#{zone}" do
368 dest_ports "1194:1197"
369 source_ports "1194:1197"
373 if node[:roles].include?("gateway")
374 template "/etc/shorewall/masq" do
375 source "shorewall-masq.erb"
379 notifies :restart, "service[shorewall]"
382 file "/etc/shorewall/masq" do
384 notifies :restart, "service[shorewall]"
388 unless node.interfaces(:family => :inet6).empty?
391 template "/etc/default/shorewall6" do
392 source "shorewall-default.erb"
396 notifies :restart, "service[shorewall6]"
399 template "/etc/shorewall6/shorewall6.conf" do
400 source "shorewall6.conf.erb"
404 notifies :restart, "service[shorewall6]"
407 template "/etc/shorewall6/zones" do
408 source "shorewall-zones.erb"
412 variables :type => "ipv6"
413 notifies :restart, "service[shorewall6]"
416 template "/etc/shorewall6/interfaces" do
417 source "shorewall6-interfaces.erb"
421 notifies :restart, "service[shorewall6]"
424 template "/etc/shorewall6/hosts" do
425 source "shorewall6-hosts.erb"
429 variables :zones => zones
430 notifies :restart, "service[shorewall6]"
433 template "/etc/shorewall6/conntrack" do
434 source "shorewall-conntrack.erb"
438 notifies :restart, "service[shorewall6]"
439 only_if { node[:networking][:firewall][:raw] }
442 template "/etc/shorewall6/policy" do
443 source "shorewall-policy.erb"
447 notifies :restart, "service[shorewall6]"
450 template "/etc/shorewall6/rules" do
451 source "shorewall-rules.erb"
455 variables :family => "inet6"
456 notifies :restart, "service[shorewall6]"
459 service "shorewall6" do
460 action [:enable, :start]
461 supports :restart => true
462 status_command "shorewall6 status"
465 template "/etc/logrotate.d/shorewall6" do
466 source "logrotate.shorewall.erb"
470 variables :name => "shorewall6"
473 firewall_rule "limit-icmp6-echo" do
479 dest_ports "echo-request"
480 rate_limit "s:1/sec:5"
484 firewall_rule "accept-http" do
490 rate_limit node[:networking][:firewall][:http_rate_limit]
491 connection_limit node[:networking][:firewall][:http_connection_limit]
494 firewall_rule "accept-https" do
500 rate_limit node[:networking][:firewall][:http_rate_limit]
501 connection_limit node[:networking][:firewall][:http_connection_limit]