From 810c743583c9d4a1f6580bcbcad38e7846978dee Mon Sep 17 00:00:00 2001 From: nick Date: Tue, 15 Apr 2014 13:49:18 +0100 Subject: [PATCH 1/5] Make the test helper path absolute --- test/t_route.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/t_route.rb b/test/t_route.rb index 6212794..d41bcb3 100644 --- a/test/t_route.rb +++ b/test/t_route.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), 'test_helper') +require File.expand_path( File.join(File.dirname(__FILE__), 'test_helper') ) require 'linux/netlink/route' # Note: multiple sockets bound to the same PID seem to cause timeout problems. From 28cda0ee6221bd8a39c41e4828d20da0bcb5dc2e Mon Sep 17 00:00:00 2001 From: nick Date: Tue, 15 Apr 2014 14:12:06 +0100 Subject: [PATCH 2/5] Add the IFA_FLAGS RTAttr and a couple of new flags --- lib/linux/constants.rb | 37 +++++++++++++------------ lib/linux/netlink/route/addr_handler.rb | 4 +++ 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/linux/constants.rb b/lib/linux/constants.rb index 8c28a54..7c432f6 100644 --- a/lib/linux/constants.rb +++ b/lib/linux/constants.rb @@ -310,24 +310,27 @@ module Linux VLAN_FLAG_MVRP = 0x8 # from linux/if_addr.h - IFA_UNSPEC = 0 - IFA_ADDRESS = 1 - IFA_LOCAL = 2 - IFA_LABEL = 3 - IFA_BROADCAST = 4 - IFA_ANYCAST = 5 - IFA_CACHEINFO = 6 - IFA_MULTICAST = 7 + IFA_UNSPEC = 0 + IFA_ADDRESS = 1 + IFA_LOCAL = 2 + IFA_LABEL = 3 + IFA_BROADCAST = 4 + IFA_ANYCAST = 5 + IFA_CACHEINFO = 6 + IFA_MULTICAST = 7 + IFA_FLAGS = 8 - IFA_F_SECONDARY = 0x01 - IFA_F_TEMPORARY = IFA_F_SECONDARY - IFA_F_NODAD = 0x02 - IFA_F_OPTIMISTIC = 0x04 - IFA_F_DADFAILED = 0x08 - IFA_F_HOMEADDRESS = 0x10 - IFA_F_DEPRECATED = 0x20 - IFA_F_TENTATIVE = 0x40 - IFA_F_PERMANENT = 0x80 + IFA_F_SECONDARY = 0x01 + IFA_F_TEMPORARY = IFA_F_SECONDARY + IFA_F_NODAD = 0x02 + IFA_F_OPTIMISTIC = 0x04 + IFA_F_DADFAILED = 0x08 + IFA_F_HOMEADDRESS = 0x10 + IFA_F_DEPRECATED = 0x20 + IFA_F_TENTATIVE = 0x40 + IFA_F_PERMANENT = 0x80 + IFA_F_MANAGETEMPADDR = 0x100 + IFA_F_NOPREFIXROUTE = 0x200 # from linux/if_arp.h - selected subset ARPHRD_NETROM = 0 diff --git a/lib/linux/netlink/route/addr_handler.rb b/lib/linux/netlink/route/addr_handler.rb index 1e4c393..b3bb422 100644 --- a/lib/linux/netlink/route/addr_handler.rb +++ b/lib/linux/netlink/route/addr_handler.rb @@ -24,6 +24,10 @@ module Netlink :pack => lambda { |val,obj| val.to_a.pack("L*") }, :unpack => lambda { |str,obj| IFACacheInfo.new(*(str.unpack("L*"))) } rtattr :multicast, IFA_MULTICAST, :l3addr + + # TODO: is there any difference between flags and ifa_flags? The latter only + # shows up on newer kernels + rtattr :ifa_flags, IFA_FLAGS, :uint end module Route From cac6e7698b7beea11b807384dd00aaa0bed3c5b4 Mon Sep 17 00:00:00 2001 From: nick Date: Tue, 15 Apr 2014 15:10:39 +0100 Subject: [PATCH 3/5] Fix a multiple-sockets-in-one-process issue. "pid" is not "process id" in netlink, but rather, "port id". If you bind to a sockaddr == 0 then Linux automatically assigns your socket a port id - which happens to be the same as the process ID for the first one concurrently open. For the second and subsequent concurrently-open sockets, binding 0 (as most users of this library will do) gets you back a random high-numbered port id. This change preserves the existing use case (one port open in the process, binding to 0) while fixing multiple-ports-open-in-the-same-process, socket-is- passed-in-and-pid-is-not-specified, and specific-pid-is-requested-but-could- not-bind-to-it. We're probably still not thread-safe - the seq handling looks dodgy - but at least now we can use multiple sockets in separate threads and have them all work. Using the same socket from multiple threads is a slightly niche use case, and it's tempting to say "don't do this" instead... --- lib/linux/netlink/nlsocket.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/linux/netlink/nlsocket.rb b/lib/linux/netlink/nlsocket.rb index 5f6ba45..8cdf9ea 100644 --- a/lib/linux/netlink/nlsocket.rb +++ b/lib/linux/netlink/nlsocket.rb @@ -58,14 +58,16 @@ module Netlink # :timeout => N (seconds, default to DEFAULT_TIMEOUT. Pass nil for no timeout) # :junk_handler => lambda { ... } for unexpected packets def initialize(opt) - @socket ||= opt[:socket] || ::Socket.new( + @socket = opt[:socket] || ::Socket.new( Socket::AF_NETLINK, Socket::SOCK_DGRAM, opt[:protocol] || (raise "Missing :protocol") ) @socket.bind(NLSocket.sockaddr(opt)) unless opt[:socket] @seq = opt[:seq] || Time.now.to_i - @pid = opt[:pid] || $$ + + @pid = @socket.getsockname.unpack(SOCKADDR_PACK)[2] + @timeout = opt.has_key?(:timeout) ? opt[:timeout] : DEFAULT_TIMEOUT if opt.has_key?(:junk_handler) @junk_handler = opt[:junk_handler] @@ -75,7 +77,7 @@ module Netlink } end end - + # Close the Netlink socket def close @socket.close @@ -85,7 +87,7 @@ module Netlink def next_seq @seq = (@seq + 1) & 0xffffffff end - + # Add a header and send a single message over the socket. # type:: the message type code # msg:: the message to send (without header) @@ -203,7 +205,7 @@ module Netlink end end end - + # Receive one datagram from kernel. Validates the sender, and returns # the raw binary message. Raises an exception on timeout or if the # kernel closes the socket. From a8ad8c1e09ea93d2b27cc08728aa375081a4d41a Mon Sep 17 00:00:00 2001 From: nick Date: Tue, 15 Apr 2014 15:17:09 +0100 Subject: [PATCH 4/5] Fix the tests now that you can have multiple sockets open at once --- test/t_route.rb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/test/t_route.rb b/test/t_route.rb index d41bcb3..a9ab25c 100644 --- a/test/t_route.rb +++ b/test/t_route.rb @@ -1,10 +1,6 @@ require File.expand_path( File.join(File.dirname(__FILE__), 'test_helper') ) require 'linux/netlink/route' -# Note: multiple sockets bound to the same PID seem to cause timeout problems. -# (Should we use different algorithm for generating the PID? PID + seq?) -$ip ||= Linux::Netlink::Route::Socket.new - # # Ruby 1.8.7 appears to lack the KeyError constant. # @@ -15,7 +11,7 @@ end class TestAddr < Test::Unit::TestCase context "With netlink route socket" do setup do - @ip = $ip + @ip = Linux::Netlink::Route::Socket.new @ifname = nil end @@ -25,12 +21,25 @@ class TestAddr < Test::Unit::TestCase rescue KeyError, IndexError # Do nothing end + + @ip.close end test "Read link type" do assert_equal Linux::ARPHRD_LOOPBACK, @ip.link["lo"].type end + test "Both sockets work if two are open at the same time" do + begin + @ip2 = Linux::Netlink::Route::Socket.new + + assert_kind_of Enumerable, @ip.route.list + assert_kind_of Enumerable, @ip2.route.list + ensure + @ip2.close + end + end + def create_test_interface(ifname = "test_#{$$}") begin @ip.link.add( From 617129b7072b6578b1cd672600eabeef6714bea1 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Tue, 26 Aug 2014 16:31:34 +0100 Subject: [PATCH 5/5] Don't whinge about missing attributes --- lib/linux/netlink/message.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linux/netlink/message.rb b/lib/linux/netlink/message.rb index e2214b9..4bafb11 100644 --- a/lib/linux/netlink/message.rb +++ b/lib/linux/netlink/message.rb @@ -187,7 +187,7 @@ module Netlink warn "Duplicate attribute #{name} (#{code}): #{attrs[name].inspect} -> #{val.inspect}" if attrs[name] attrs[name] = val else - warn "Unknown attribute #{code}, in class #{self}, value #{val.inspect}" + warn "Unknown attribute #{code}, in class #{self}, value #{val.inspect}" if $DEBUG attrs[code] = val end end