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]
31 "renderer" => "networkd",
38 node[:networking][:interfaces].each do |name, interface|
39 if interface[:interface]
40 network_packages |= ["vlan"] if interface[:interface] =~ /\.\d+$/
41 network_packages |= ["ifenslave"] if interface[:bond]
43 if interface[:role] && (role = node[:networking][:roles][interface[:role]])
44 if role[interface[:family]]
45 node.normal[:networking][:interfaces][name][:prefix] = role[interface[:family]][:prefix]
46 node.normal[:networking][:interfaces][name][:gateway] = role[interface[:family]][:gateway]
49 node.normal[:networking][:interfaces][name][:metric] = role[:metric]
50 node.normal[:networking][:interfaces][name][:zone] = role[:zone]
53 prefix = node[:networking][:interfaces][name][:prefix]
55 node.normal[:networking][:interfaces][name][:netmask] = (~IPAddr.new(interface[:address]).mask(0)).mask(prefix)
56 node.normal[:networking][:interfaces][name][:network] = IPAddr.new(interface[:address]).mask(prefix)
58 if node[:networking][:netplan]
59 deviceplan = if interface[:interface] =~ /^(.*)\.(\d+)$/
60 netplan["network"]["vlans"][interface[:interface]] ||= {
61 "id" => Regexp.last_match(2).to_i,
62 "link" => Regexp.last_match(1),
67 elsif interface[:interface] =~ /^bond\d+$/
68 netplan["network"]["bonds"][interface[:interface]] ||= {
74 netplan["network"]["ethernets"][interface[:interface]] ||= {
81 deviceplan["addresses"].push("#{interface[:address]}/#{prefix}")
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],
129 node.rm(:networking, :interfaces, name)
133 if node[:networking][:netplan]
136 file "/etc/netplan/01-netcfg.yaml" do
140 netplan["network"]["bonds"].each_value do |bond|
141 bond["interfaces"].each do |interface|
142 netplan["network"]["ethernets"][interface] ||= { "accept-ra" => false }
146 netplan["network"]["vlans"].each_value do |vlan|
147 unless vlan["link"] =~ /^bond\d+$/
148 netplan["network"]["ethernets"][vlan["link"]] ||= { "accept-ra" => false }
152 file "/etc/netplan/99-chef.yaml" do
156 content YAML.dump(netplan)
159 service "networking" do
163 file "/etc/network/interfaces" do
167 package "ifupdown" do
171 package network_packages
173 template "/etc/network/interfaces" do
174 source "interfaces.erb"
181 execute "hostname" do
183 command "/bin/hostname -F /etc/hostname"
186 template "/etc/hostname" do
187 source "hostname.erb"
191 notifies :run, "execute[hostname]"
194 template "/etc/hosts" do
201 unless node[:networking][:nameservers].empty?
202 link "/etc/resolv.conf" do
205 to "/run/resolvconf/resolv.conf"
206 only_if { File.symlink?("/etc/resolv.conf") }
209 template "/etc/resolv.conf" do
210 source "resolv.conf.erb"
217 node.interfaces(:role => :internal) do |interface|
218 if interface[:gateway] && interface[:gateway] != interface[:address]
219 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
220 next unless gateway[:openvpn]
222 gateway[:openvpn][:tunnels].each_value do |tunnel|
223 if tunnel[:peer][:address] # ~FC023
224 route tunnel[:peer][:address] do
225 netmask "255.255.255.255"
226 gateway interface[:gateway]
227 device interface[:interface]
231 next unless tunnel[:peer][:networks]
233 tunnel[:peer][:networks].each do |network|
234 route network[:address] do
235 netmask network[:netmask]
236 gateway interface[:gateway]
237 device interface[:interface]
247 search(:node, "networking:interfaces").collect do |n|
248 next if n[:fqdn] == node[:fqdn]
250 n.interfaces.each do |interface|
251 next unless interface[:role] == "external" && interface[:zone]
253 zones[interface[:zone]] ||= {}
254 zones[interface[:zone]][interface[:family]] ||= []
255 zones[interface[:zone]][interface[:family]] << interface[:address]
261 template "/etc/default/shorewall" do
262 source "shorewall-default.erb"
266 notifies :restart, "service[shorewall]"
269 template "/etc/shorewall/shorewall.conf" do
270 source "shorewall.conf.erb"
274 notifies :restart, "service[shorewall]"
277 template "/etc/shorewall/zones" do
278 source "shorewall-zones.erb"
282 variables :type => "ipv4"
283 notifies :restart, "service[shorewall]"
286 template "/etc/shorewall/interfaces" do
287 source "shorewall-interfaces.erb"
291 notifies :restart, "service[shorewall]"
294 template "/etc/shorewall/hosts" do
295 source "shorewall-hosts.erb"
299 variables :zones => zones
300 notifies :restart, "service[shorewall]"
303 template "/etc/shorewall/conntrack" do
304 source "shorewall-conntrack.erb"
308 notifies :restart, "service[shorewall]"
309 only_if { node[:networking][:firewall][:raw] }
312 template "/etc/shorewall/policy" do
313 source "shorewall-policy.erb"
317 notifies :restart, "service[shorewall]"
320 template "/etc/shorewall/rules" do
321 source "shorewall-rules.erb"
325 variables :family => "inet"
326 notifies :restart, "service[shorewall]"
329 service "shorewall" do
330 action [:enable, :start]
331 supports :restart => true
332 status_command "shorewall status"
335 template "/etc/logrotate.d/shorewall" do
336 source "logrotate.shorewall.erb"
340 variables :name => "shorewall"
343 firewall_rule "limit-icmp-echo" do
349 dest_ports "echo-request"
350 rate_limit "s:1/sec:5"
353 %w[ucl ams bm].each do |zone|
354 firewall_rule "accept-openvpn-#{zone}" do
359 dest_ports "1194:1197"
360 source_ports "1194:1197"
364 if node[:roles].include?("gateway")
365 template "/etc/shorewall/masq" do
366 source "shorewall-masq.erb"
370 notifies :restart, "service[shorewall]"
373 file "/etc/shorewall/masq" do
375 notifies :restart, "service[shorewall]"
379 unless node.interfaces(:family => :inet6).empty?
382 template "/etc/default/shorewall6" do
383 source "shorewall-default.erb"
387 notifies :restart, "service[shorewall6]"
390 template "/etc/shorewall6/shorewall6.conf" do
391 source "shorewall6.conf.erb"
395 notifies :restart, "service[shorewall6]"
398 template "/etc/shorewall6/zones" do
399 source "shorewall-zones.erb"
403 variables :type => "ipv6"
404 notifies :restart, "service[shorewall6]"
407 template "/etc/shorewall6/interfaces" do
408 source "shorewall6-interfaces.erb"
412 notifies :restart, "service[shorewall6]"
415 template "/etc/shorewall6/hosts" do
416 source "shorewall6-hosts.erb"
420 variables :zones => zones
421 notifies :restart, "service[shorewall6]"
424 template "/etc/shorewall6/conntrack" do
425 source "shorewall-conntrack.erb"
429 notifies :restart, "service[shorewall6]"
430 only_if { node[:networking][:firewall][:raw] }
433 template "/etc/shorewall6/policy" do
434 source "shorewall-policy.erb"
438 notifies :restart, "service[shorewall6]"
441 template "/etc/shorewall6/rules" do
442 source "shorewall-rules.erb"
446 variables :family => "inet6"
447 notifies :restart, "service[shorewall6]"
450 service "shorewall6" do
451 action [:enable, :start]
452 supports :restart => true
453 status_command "shorewall6 status"
456 template "/etc/logrotate.d/shorewall6" do
457 source "logrotate.shorewall.erb"
461 variables :name => "shorewall6"
464 firewall_rule "limit-icmp6-echo" do
470 dest_ports "echo-request"
471 rate_limit "s:1/sec:5"
475 firewall_rule "accept-http" do
481 rate_limit node[:networking][:firewall][:http_rate_limit]
482 connection_limit node[:networking][:firewall][:http_connection_limit]
485 firewall_rule "accept-https" do
491 rate_limit node[:networking][:firewall][:http_rate_limit]
492 connection_limit node[:networking][:firewall][:http_connection_limit]