summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authortpowell-progress <104777878+tpowell-progress@users.noreply.github.com>2022-05-31 14:44:17 -0500
committerGitHub <noreply@github.com>2022-05-31 14:44:17 -0500
commit06fc3473963d182f83e4b55622e10e0faab54f98 (patch)
tree5ff3b8f5f328839daf7e4447f4db34f799f3facf /lib
parent062d2b42bc3c235335956ee2ff62d1f520714637 (diff)
parenteab8a763263295dff15d1893f257312b1851749c (diff)
downloadohai-06fc3473963d182f83e4b55622e10e0faab54f98.tar.gz
Merge pull request #1741 from mattp-/main
networking/linux: map src only default routes accordingly
Diffstat (limited to 'lib')
-rw-r--r--lib/ohai/plugins/linux/network.rb21
1 files changed, 18 insertions, 3 deletions
diff --git a/lib/ohai/plugins/linux/network.rb b/lib/ohai/plugins/linux/network.rb
index 4a5ddb3c..eb83843c 100644
--- a/lib/ohai/plugins/linux/network.rb
+++ b/lib/ohai/plugins/linux/network.rb
@@ -103,9 +103,24 @@ Ohai.plugin(:Network) do
next
end
route_endings.each do |route_ending|
+ route_entry = Mash.new(destination: route_dest,
+ family: family[:name])
+ route_int = nil
if route_ending =~ /\bdev\s+([^\s]+)\b/
route_int = $1
- else
+ end
+ # does any known interface own the src address?
+ # we try to infer the interface/device from its address if it isn't specified
+ # we want to override the interface set via nexthop but only if possible
+ if line =~ /\bsrc\s+([^\s]+)\b/ && (!route_int || line.include?("nexthop"))
+ # only clobber previously set route_int if we find a match
+ if (match = iface.select { |name, intf| intf.fetch("addresses", {}).any? { |addr, _| addr == $1 } }.keys.first)
+ route_int = match
+ route_entry[:inferred] = true
+ end
+ end
+
+ unless route_int
logger.trace("Plugin Network: Skipping route entry without a device: '#{line}'")
next
end
@@ -116,8 +131,6 @@ Ohai.plugin(:Network) do
next
end
- route_entry = Mash.new(destination: route_dest,
- family: family[:name])
%w{via scope metric proto src}.each do |k|
# http://rubular.com/r/pwTNp65VFf
route_entry[k] = $1 if route_ending =~ /\b#{k}\s+([^\s]+)/
@@ -645,10 +658,12 @@ Ohai.plugin(:Network) do
# sorting the selected routes:
# - getting default routes first
# - then sort by metric
+ # - then sort by if the device was inferred or not (preferring explicit to inferred)
# - then by prefixlen
[
r[:destination] == "default" ? 0 : 1,
r[:metric].nil? ? 0 : r[:metric].to_i,
+ r[:inferred] ? 1 : 0,
# for some reason IPAddress doesn't accept "::/0", it doesn't like prefix==0
# just a quick workaround: use 0 if IPAddress fails
begin