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 if interface[:interface] =~ /^(.*)\.(\d+)$/
60 deviceplan = netplan["network"]["vlans"][interface[:interface]] = {
61 "id" => Regexp.last_match(2).to_i,
62 "link" => Regexp.last_match(1),
67 elsif interface[:bond]
68 deviceplan = netplan["network"]["bonds"][interface[:interface]] = {
72 "interfaces" => interface[:bond][:slaves].to_a,
73 "mode" => interface[:bond][:mode] || "active-backup",
74 "primary" => interface[:bond][:slaves].first,
75 "mii-monitor-interval" => interface[:bond][:miimon] || 100,
76 "down-delay" => interface[:bond][:downdelay] || 200,
77 "up-delay" => interface[:bond][:updelay] || 200
80 deviceplan["transmit-hash-policy"] = interface[:bond][:xmithashpolicy] if interface[:bond][:xmithashpolicy]
81 deviceplan["lacp-rate"] = interface[:bond][:lacprate] if interface[:bond][:lacprate]
83 deviceplan = netplan["network"]["ethernets"][interface[:interface]] = {
90 deviceplan["addresses"].push("#{interface[:address]}/#{prefix}")
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],
108 node.rm(:networking, :interfaces, name)
112 if node[:networking][:netplan]
115 file "/etc/netplan/01-netcfg.yaml" do
119 netplan["network"]["bonds"].each_value do |bond|
120 bond["interfaces"].each do |interface|
121 netplan["network"]["ethernets"][interface] ||= { "accept-ra" => false }
125 netplan["network"]["vlans"].each_value do |vlan|
126 netplan["network"]["ethernets"][vlan["link"]] ||= { "accept-ra" => false }
129 file "/etc/netplan/99-chef.yaml" do
133 content YAML.dump(netplan)
136 service "networking" do
140 file "/etc/network/interfaces" do
144 package "ifupdown" do
148 package network_packages
150 template "/etc/network/interfaces" do
151 source "interfaces.erb"
158 execute "hostname" do
160 command "/bin/hostname -F /etc/hostname"
163 template "/etc/hostname" do
164 source "hostname.erb"
168 notifies :run, "execute[hostname]"
171 template "/etc/hosts" do
178 unless node[:networking][:nameservers].empty?
179 link "/etc/resolv.conf" do
182 to "/run/resolvconf/resolv.conf"
183 only_if { File.symlink?("/etc/resolv.conf") }
186 template "/etc/resolv.conf" do
187 source "resolv.conf.erb"
194 node.interfaces(:role => :internal) do |interface|
195 if interface[:gateway] && interface[:gateway] != interface[:address]
196 search(:node, "networking_interfaces*address:#{interface[:gateway]}") do |gateway|
197 next unless gateway[:openvpn]
199 gateway[:openvpn][:tunnels].each_value do |tunnel|
200 if tunnel[:peer][:address] # ~FC023
201 route tunnel[:peer][:address] do
202 netmask "255.255.255.255"
203 gateway interface[:gateway]
204 device interface[:interface]
208 next unless tunnel[:peer][:networks]
210 tunnel[:peer][:networks].each do |network|
211 route network[:address] do
212 netmask network[:netmask]
213 gateway interface[:gateway]
214 device interface[:interface]
224 search(:node, "networking:interfaces").collect do |n|
225 next if n[:fqdn] == node[:fqdn]
227 n.interfaces.each do |interface|
228 next unless interface[:role] == "external" && interface[:zone]
230 zones[interface[:zone]] ||= {}
231 zones[interface[:zone]][interface[:family]] ||= []
232 zones[interface[:zone]][interface[:family]] << interface[:address]
238 template "/etc/default/shorewall" do
239 source "shorewall-default.erb"
243 notifies :restart, "service[shorewall]"
246 template "/etc/shorewall/shorewall.conf" do
247 source "shorewall.conf.erb"
251 notifies :restart, "service[shorewall]"
254 template "/etc/shorewall/zones" do
255 source "shorewall-zones.erb"
259 variables :type => "ipv4"
260 notifies :restart, "service[shorewall]"
263 template "/etc/shorewall/interfaces" do
264 source "shorewall-interfaces.erb"
268 notifies :restart, "service[shorewall]"
271 template "/etc/shorewall/hosts" do
272 source "shorewall-hosts.erb"
276 variables :zones => zones
277 notifies :restart, "service[shorewall]"
280 template "/etc/shorewall/conntrack" do
281 source "shorewall-conntrack.erb"
285 notifies :restart, "service[shorewall]"
286 only_if { node[:networking][:firewall][:raw] }
289 template "/etc/shorewall/policy" do
290 source "shorewall-policy.erb"
294 notifies :restart, "service[shorewall]"
297 template "/etc/shorewall/rules" do
298 source "shorewall-rules.erb"
302 variables :family => "inet"
303 notifies :restart, "service[shorewall]"
306 service "shorewall" do
307 action [:enable, :start]
308 supports :restart => true
309 status_command "shorewall status"
312 template "/etc/logrotate.d/shorewall" do
313 source "logrotate.shorewall.erb"
317 variables :name => "shorewall"
320 firewall_rule "limit-icmp-echo" do
326 dest_ports "echo-request"
327 rate_limit "s:1/sec:5"
330 %w[ucl ams bm].each do |zone|
331 firewall_rule "accept-openvpn-#{zone}" do
336 dest_ports "1194:1197"
337 source_ports "1194:1197"
341 if node[:roles].include?("gateway")
342 template "/etc/shorewall/masq" do
343 source "shorewall-masq.erb"
347 notifies :restart, "service[shorewall]"
350 file "/etc/shorewall/masq" do
352 notifies :restart, "service[shorewall]"
356 unless node.interfaces(:family => :inet6).empty?
359 template "/etc/default/shorewall6" do
360 source "shorewall-default.erb"
364 notifies :restart, "service[shorewall6]"
367 template "/etc/shorewall6/shorewall6.conf" do
368 source "shorewall6.conf.erb"
372 notifies :restart, "service[shorewall6]"
375 template "/etc/shorewall6/zones" do
376 source "shorewall-zones.erb"
380 variables :type => "ipv6"
381 notifies :restart, "service[shorewall6]"
384 template "/etc/shorewall6/interfaces" do
385 source "shorewall6-interfaces.erb"
389 notifies :restart, "service[shorewall6]"
392 template "/etc/shorewall6/hosts" do
393 source "shorewall6-hosts.erb"
397 variables :zones => zones
398 notifies :restart, "service[shorewall6]"
401 template "/etc/shorewall6/conntrack" do
402 source "shorewall-conntrack.erb"
406 notifies :restart, "service[shorewall6]"
407 only_if { node[:networking][:firewall][:raw] }
410 template "/etc/shorewall6/policy" do
411 source "shorewall-policy.erb"
415 notifies :restart, "service[shorewall6]"
418 template "/etc/shorewall6/rules" do
419 source "shorewall-rules.erb"
423 variables :family => "inet6"
424 notifies :restart, "service[shorewall6]"
427 service "shorewall6" do
428 action [:enable, :start]
429 supports :restart => true
430 status_command "shorewall6 status"
433 template "/etc/logrotate.d/shorewall6" do
434 source "logrotate.shorewall.erb"
438 variables :name => "shorewall6"
441 firewall_rule "limit-icmp6-echo" do
447 dest_ports "echo-request"
448 rate_limit "s:1/sec:5"
452 firewall_rule "accept-http" do
458 rate_limit node[:networking][:firewall][:http_rate_limit]
459 connection_limit node[:networking][:firewall][:http_connection_limit]
462 firewall_rule "accept-https" do
468 rate_limit node[:networking][:firewall][:http_rate_limit]
469 connection_limit node[:networking][:firewall][:http_connection_limit]