Commit ba9c1815 authored by Paul Cammish's avatar Paul Cammish
Browse files

Merge branch 'foldin_ruby-netlink' into 'stretch'

Folded in ruby-netlink libraries from https://github.com/BytemarkHosting/netlinkrb

See merge request sympl.host/sympl!9
parents f2809260 166c2006
......@@ -10,7 +10,7 @@ XS-Ruby-Versions: all
Package: symbiosis-common
Architecture: all
XB-Ruby-Versions: ${ruby:Versions}
Depends: ruby | ruby-interpreter, ruby-acme-client (>= 0.3.5), ruby-linux-netlink, ruby-password, ruby-diffy, ruby-erubis, ruby-mocha, ruby-webmock, ruby-test-unit, gnutls-bin, openssl, sudo, adduser, ssl-cert, ${misc:Depends}
Depends: ruby | ruby-interpreter, ruby-acme-client (>= 0.3.5), ruby-password, ruby-diffy, ruby-erubis, ruby-mocha, ruby-webmock, ruby-test-unit, gnutls-bin, openssl, sudo, adduser, ssl-cert, ${misc:Depends}
Replaces: symbiosis-firewall (<< 2011:1214), symbiosis-range, symbiosis-test, bytemark-vhost-range, bytemark-vhost-test, symbiosis-crack
Breaks: symbiosis-firewall (<< 2011:1214), symbiosis-email (<< 2012:0215)
Conflicts: symbiosis-range, symbiosis-test, symbiosis-crack, bytemark-vhost-range, bytemark-vhost-test, symbiosis-email (<< 2012:0215)
......
module Linux
# This class allows defining of C-style structures, and converting
# object instances to and from a packed binary representation.
#
# A new structure is created by subclassing CStruct, and then using the
# 'field' metaprogramming macro to define each field:
#
# class Foo < Linux::CStruct
# field :bar, :char
# field :baz, :long
#
# # custom packing
# field :qux, :pattern => "Z16", :default => EMPTY_STRING
#
# # user-defined types
# define_type :str16, :pattern => "Z16", :default => EMPTY_STRING
# field :qux2, :str16
# field :qux3, :str16
# end
#
# You can then instantiate the structure by calling 'new'. You may pass in
# a hash of values to initialize the structure.
#
# msg = Foo.new(:bar => 123)
# msg.bar = 456 # accessor methods
# str = msg.to_str # convert to binary
# msg2 = Foo.parse(str) # convert from binary
# msg2 = Foo.new(msg) # copy an existing object
class CStruct
EMPTY_STRING = "".freeze #:nodoc:
EMPTY_ARRAY = [].freeze #:nodoc:
TYPE_INFO = {} #:nodoc
# The size of the structure (in bytes)
def self.bytesize
@bytesize
end
# Define a new type for use with 'field'. You supply the
# symbolic name for the type, and a set of options.
# :pattern => "str" # format string for Array#pack / String#unpack
# :default => val # default (if not 0)
# :size => 16 # size of this entry
# :align => 4 # align to 4-byte boundary (must be power-of-two)
# :align => true # align to [size]-byte boundary
#
# If you do not specify :size then it is calculated by packing an
# instance of the default value.
def self.define_type(name, opt)
TYPE_INFO[name] = opt
end
# Return a type info hash given a type id. Raises IndexError if not found.
def self.find_type(type)
case type
when nil, Hash
type
else
TYPE_INFO.fetch(type)
end
end
define_type :uchar, :pattern => "C"
define_type :uint16, :pattern => "S", :align => true
define_type :uint32, :pattern => "L", :align => true
define_type :uint64, :pattern => "Q", :align => true
define_type :char, :pattern => "c"
define_type :int16, :pattern => "s", :align => true
define_type :int32, :pattern => "l", :align => true
define_type :int64, :pattern => "q", :align => true
define_type :ushort, :pattern => "S_", :align => true
define_type :uint, :pattern => "I", :align => true
define_type :ulong, :pattern => "L_", :align => true
define_type :short, :pattern => "s_", :align => true
define_type :int, :pattern => "i", :align => true
define_type :long, :pattern => "l_", :align => true
define_type :ns, :pattern => "n", :align => true
define_type :nl, :pattern => "N", :align => true
begin
require 'linux/c_struct_sizeof_size_t.rb'
rescue LoadError
warn "Falling back to gcc to determine sizeof size_t." if $VERBOSE
SIZEOF_SIZE_T = Integer(`echo __SIZEOF_SIZE_T__ | gcc -E -P -`) rescue 1.size
end
define_type :size_t,
case SIZEOF_SIZE_T
when 8
{:pattern => "Q", :align => true}
when 4
{:pattern => "L", :align => true}
else
raise "Bad size_t (#{SIZEOF_SIZE_T.inspect})"
end
# these can be used at end of structure only
define_type :binary, :pattern => "a*", :default => EMPTY_STRING
# cstring has \x00 terminator when sent over wire
define_type :cstring, :pattern => "Z*", :default => EMPTY_STRING
def initialize(h=nil)
if h.instance_of?(self.class)
@attrs = h.to_hash.dup
else
@attrs = {}
h.each { |k,v| self[k] = v } if h
end
end
# This hook is called after unpacking from binary, and can be used
# for fixing up the data
def after_parse
end
def to_hash
@attrs
end
def each(&blk)
@attrs.each(&blk)
end
# Set a field by name. Currently can use either symbol or string as key.
def []=(k,v)
send "#{k}=", v
end
# Retrieve a field by name. Must use symbol as key.
def [](k)
@attrs[k]
end
def self.inherited(subclass) #:nodoc:
subclass.const_set(:FIELDS, [])
subclass.const_set(:FORMAT, "")
subclass.const_set(:DEFAULTS, {})
subclass.instance_variable_set(:@bytesize, 0)
end
# Define a field for this message, which creates accessor methods and
# sets up data required to pack and unpack the structure.
# field :foo, :uchar
# field :foo, :uchar, :default=>0xff # use this default value
def self.field(name, type, opt={})
info = find_type(type)
pattern = info[:pattern]
default = opt.fetch(:default) { info.fetch(:default, 0) }
# Apply padding for structure alignment if necessary
size = info[:size] || [default].pack(pattern).bytesize
if align = (opt[:align] || info[:align])
align = size if align == true
field_pad alignto(@bytesize, align) - @bytesize
end
@bytesize += size
self::FIELDS << name
self::FORMAT << pattern
self::DEFAULTS[name] = default
define_method name do
@attrs[name]
end
define_method "#{name}=" do |val|
@attrs.store name, val
end
end
# Skip pad byte(s) - default 1
def self.field_pad(count=1)
if count > 0
self::FORMAT << "x#{count}"
@bytesize += count
end
end
# Returns the packed binary representation of this structure
def to_str
self.class::FIELDS.map { |key| self[key] || self.class::DEFAULTS[key] }.pack(self.class::FORMAT)
end
def inspect
"#<#{self.class} #{@attrs.inspect}>"
end
# Convert a binary representation of this structure into an object instance.
# If a block is given, the object is yielded to that block. Finally the
# after_parse hook is called.
def self.parse(data, obj=new)
data.unpack(self::FORMAT).zip(self::FIELDS).each do |val, key|
obj[key] = val
end
yield obj if block_given?
obj.after_parse
obj
end
# Round up a number to multiple of m, where m is a power of two
def self.alignto(val, m)
(val + (m-1)) & ~(m-1)
end
end
end # module Linux
require 'socket'
class Socket
# From bits/socket.h
PF_NETLINK = 16 unless defined? Socket::PF_NETLINK
AF_NETLINK = PF_NETLINK unless defined? Socket::AF_NETLINK
# From in.h
IPPROTO_IPV6 = 41 unless defined? Socket::IPPROTO_IPV6
end
module Linux
# From linux/netlink.h
NETLINK_ROUTE = 0
NETLINK_UNUSED = 1
NETLINK_USERSOCK = 2
NETLINK_FIREWALL = 3
NETLINK_INET_DIAG = 4
NETLINK_NFLOG = 5
NETLINK_XFRM = 6
NETLINK_SELINUX = 7
NETLINK_ISCSI = 8
NETLINK_AUDIT = 9
NETLINK_FIB_LOOKUP = 10
NETLINK_CONNECTOR = 11
NETLINK_NETFILTER = 12
NETLINK_IP6_FW = 13
NETLINK_DNRTMSG = 14
NETLINK_KOBJECT_UEVENT = 15
NETLINK_GENERIC = 16
NETLINK_SCSITRANSPORT = 18
NETLINK_ECRYPTFS = 19
NETLINK_RDMA = 20
NETLINK_CRYPTO = 21
NLM_F_REQUEST = 1
NLM_F_MULTI = 2
NLM_F_ACK = 4
NLM_F_ECHO = 8
NLM_F_DUMP_INTR = 16
NLM_F_DUMP_FILTERED = 32
NLM_F_ROOT = 0x100
NLM_F_MATCH = 0x200
NLM_F_ATOMIC = 0x400
NLM_F_DUMP = (NLM_F_ROOT|NLM_F_MATCH)
NLM_F_REPLACE = 0x100
NLM_F_EXCL = 0x200
NLM_F_CREATE = 0x400
NLM_F_APPEND = 0x800
NLMSG_ALIGNTO = 4 # same on i686 and x86_64
NLMSG_NOOP = 0x1
NLMSG_ERROR = 0x2
NLMSG_DONE = 0x3
NLMSG_OVERRUN = 0x4
NLMSG_MIN_TYPE = 0x10
NETLINK_ADD_MEMBERSHIP = 1
NETLINK_DROP_MEMBERSHIP = 2
NETLINK_PKTINFO = 3
NETLINK_BROADCAST_ERROR = 4
NETLINK_NO_ENOBUFS = 5
NETLINK_RX_RING = 6
NETLINK_TX_RING = 7
NETLINK_LISTEN_ALL_NSID = 8
NETLINK_LIST_MEMBERSHIPS = 9
NETLINK_CAP_ACK = 10
NETLINK_UNCONNECTED = 0
NETLINK_CONNECTED = 1
NLA_F_NESTED = (1 << 15)
NLA_F_NET_BYTEORDER = (1 << 14)
NLA_TYPE_MASK = ~(NLA_F_NESTED | NLA_F_NET_BYTEORDER)
NLA_ALIGNTO = 4 # same on i686 and x86_64
# from linux/rtnetlink.h.
RTM_BASE = 16
RTM_NEWLINK = 16
RTM_DELLINK = 17
RTM_GETLINK = 18
RTM_SETLINK = 19
RTM_NEWADDR = 20
RTM_DELADDR = 21
RTM_GETADDR = 22
RTM_NEWROUTE = 24
RTM_DELROUTE = 25
RTM_GETROUTE = 26
RTM_NEWNEIGH = 28
RTM_DELNEIGH = 29
RTM_GETNEIGH = 30
RTM_NEWRULE = 32
RTM_DELRULE = 33
RTM_GETRULE = 34
RTM_NEWQDISC = 36
RTM_DELQDISC = 37
RTM_GETQDISC = 38
RTM_NEWTCLASS = 40
RTM_DELTCLASS = 41
RTM_GETTCLASS = 42
RTM_NEWTFILTER = 44
RTM_DELTFILTER = 45
RTM_GETTFILTER = 46
RTM_NEWACTION = 48
RTM_DELACTION = 49
RTM_GETACTION = 50
RTM_NEWPREFIX = 52
RTM_GETMULTICAST = 58
RTM_GETANYCAST = 62
RTM_NEWNEIGHTBL = 64
RTM_GETNEIGHTBL = 66
RTM_SETNEIGHTBL = 67
RTM_NEWNDUSEROPT = 68
RTM_NEWADDRLABEL = 72
RTM_DELADDRLABEL = 73
RTM_GETADDRLABEL = 74
RTM_GETDCB = 78
RTM_SETDCB = 79
RTM_NEWNETCONF = 80
RTM_GETNETCONF = 82
RTM_NEWMDB = 84
RTM_DELMDB = 85
RTM_GETMDB = 86
RTM_NEWNSID = 88
RTM_DELNSID = 89
RTM_GETNSID = 90
RTM_NEWSTATS = 92
RTM_GETSTATS = 94
# RT#type
RTN_UNSPEC = 0
RTN_UNICAST = 1
RTN_LOCAL = 2
RTN_BROADCAST = 3
RTN_ANYCAST = 4
RTN_MULTICAST = 5
RTN_BLACKHOLE = 6
RTN_UNREACHABLE = 7
RTN_PROHIBIT = 8
RTN_THROW = 9
RTN_NAT = 10
RTN_XRESOLVE = 11
# RT#protocol
RTPROT_UNSPEC = 0
RTPROT_REDIRECT = 1
RTPROT_KERNEL = 2
RTPROT_BOOT = 3
RTPROT_STATIC = 4
RTPROT_GATED = 8
RTPROT_RA = 9
RTPROT_MRT = 10
RTPROT_ZEBRA = 11
RTPROT_BIRD = 12
RTPROT_DNROUTED = 13
RTPROT_XORP = 14
RTPROT_NTK = 15
RTPROT_DHCP = 16
RTPROT_MROUTED = 17
RTPROT_BABEL = 42
# RT#scope, IFAddr#scope
RT_SCOPE_UNIVERSE = 0
RT_SCOPE_SITE = 200
RT_SCOPE_LINK = 253
RT_SCOPE_HOST = 254
RT_SCOPE_NOWHERE = 255
# RT#flags
RTM_F_NOTIFY = 0x100
RTM_F_CLONED = 0x200
RTM_F_EQUALIZE = 0x400
RTM_F_PREFIX = 0x800
RTM_F_LOOKUP_TABLE = 0x1000
# RT#table (reserved values)
RT_TABLE_UNSPEC = 0
RT_TABLE_COMPAT = 252
RT_TABLE_DEFAULT = 253
RT_TABLE_MAIN = 254
RT_TABLE_LOCAL = 255
# routing message attributes
RTA_UNSPEC = 0
RTA_DST = 1
RTA_SRC = 2
RTA_IIF = 3
RTA_OIF = 4
RTA_GATEWAY = 5
RTA_PRIORITY = 6
RTA_PREFSRC = 7
RTA_METRICS = 8
RTA_MULTIPATH = 9
RTA_FLOW = 11
RTA_CACHEINFO = 12
RTA_TABLE = 15
RTA_MARK = 16
RTA_MFC_STATS = 17
RTA_VIA = 18
RTA_NEWDST = 19
RTA_PREF = 20
RTA_ENCAP_TYPE = 21
RTA_ENCAP = 22
RTA_EXPIRES = 23
RTA_PAD = 24
# Keys for RT#nh_flags
RTNH_F_DEAD = 1 # Nexthop is dead (used by multipath)
RTNH_F_PERVASIVE = 2 # Do recursive gateway lookup
RTNH_F_ONLINK = 4 # Gateway is forced on link
RTNH_F_OFFLOAD = 8 # offloaded route
RTNH_F_LINKDOWN = 16 # carrier-down on nexthop
# Keys for RT#metrics
RTAX_UNSPEC = 0
RTAX_LOCK = 1
RTAX_MTU = 2
RTAX_WINDOW = 3
RTAX_RTT = 4
RTAX_RTTVAR = 5
RTAX_SSTHRESH = 6
RTAX_CWND = 7
RTAX_ADVMSS = 8
RTAX_REORDERING = 9
RTAX_HOPLIMIT = 10
RTAX_INITCWND = 11
RTAX_FEATURES = 12
RTAX_RTO_MIN = 13
RTAX_INITRWND = 14
RTAX_QUICKACK = 15
RTAX_CC_ALGO = 16
# from linux/if_link.h
IFLA_UNSPEC = 0
IFLA_ADDRESS = 1
IFLA_BROADCAST = 2
IFLA_IFNAME = 3
IFLA_MTU = 4
IFLA_LINK = 5
IFLA_QDISC = 6
IFLA_STATS = 7
IFLA_COST = 8
IFLA_PRIORITY = 9
IFLA_MASTER = 10
IFLA_WIRELESS = 11
IFLA_PROTINFO = 12
IFLA_TXQLEN = 13
IFLA_MAP = 14
IFLA_WEIGHT = 15
IFLA_OPERSTATE = 16
IFLA_LINKMODE = 17
IFLA_LINKINFO = 18 # Nested IFLA_INFO_*
IFLA_NET_NS_PID = 19
IFLA_IFALIAS = 20
IFLA_NUM_VF = 21
IFLA_VFINFO_LIST = 22
IFLA_STATS64 = 23
IFLA_VF_PORTS = 24
IFLA_PORT_SELF = 25
IFLA_AF_SPEC = 26
IFLA_GROUP = 27
IFLA_NET_NS_FD = 28
IFLA_EXT_MASK = 29
IFLA_PROMISCUITY = 30
IFLA_NUM_TX_QUEUES = 31
IFLA_NUM_RX_QUEUES = 32
IFLA_CARRIER = 33
IFLA_PHYS_PORT_ID = 34
IFLA_CARRIER_CHANGES = 35
IFLA_PHYS_SWITCH_ID = 36
IFLA_LINK_NETNSID = 37
IFLA_PHYS_PORT_NAME = 38
IFLA_PROTO_DOWN = 39
IFLA_GSO_MAX_SEGS = 40
IFLA_GSO_MAX_SIZE = 41
IFLA_PAD = 42
BRIDGE_MODE_UNSPEC = 0
BRIDGE_MODE_HAIRPIN = 1
IFLA_BRPORT_UNSPEC = 0
IFLA_BRPORT_STATE = 1 # Spanning tree state
IFLA_BRPORT_PRIORITY = 2 # " priority
IFLA_BRPORT_COST = 3 # " cost
IFLA_BRPORT_MODE = 4 # mode (hairpin)
IFLA_BRPORT_GUARD = 5 # bpdu guard
IFLA_BRPORT_PROTECT = 6 # root port protection
IFLA_BRPORT_FAST_LEAVE = 7 # multicast fast leave
IFLA_BRPORT_LEARNING = 8 # mac learning
IFLA_BRPORT_UNICAST_FLOOD = 9 # flood unicast traffic
IFLA_BRPORT_PROXYARP = 10 # proxy ARP
IFLA_BRPORT_LEARNING_SYNC = 11 # mac learning sync from device
IFLA_BRPORT_PROXYARP_WIFI = 12 # proxy ARP for Wi-Fi
IFLA_BRPORT_ROOT_ID = 13 # designated root
IFLA_BRPORT_BRIDGE_ID = 14 # designated bridge
IFLA_BRPORT_DESIGNATED_PORT = 15
IFLA_BRPORT_DESIGNATED_COST = 16
IFLA_BRPORT_ID = 17
IFLA_BRPORT_NO = 18
IFLA_BRPORT_TOPOLOGY_CHANGE_ACK = 19
IFLA_BRPORT_CONFIG_PENDING = 20
IFLA_BRPORT_MESSAGE_AGE_TIMER = 21
IFLA_BRPORT_FORWARD_DELAY_TIMER = 22
IFLA_BRPORT_HOLD_TIMER = 23
IFLA_BRPORT_FLUSH = 24
IFLA_BRPORT_MULTICAST_ROUTER = 25
IFLA_BRPORT_PAD = 26
IFLA_INFO_UNSPEC = 0
IFLA_INFO_KIND = 1 # "vlan", "gre" etc
IFLA_INFO_DATA = 2 # packed rtattrs specific to type, e.g. vlan
IFLA_INFO_XSTATS = 3
# INFO_DATA for INFO_KIND == "vlan"
IFLA_VLAN_UNSPEC = 0
IFLA_VLAN_ID = 1 # ushort
IFLA_VLAN_FLAGS = 2 # struct ifla_vlan_flags
IFLA_VLAN_EGRESS_QOS = 3 # followed by instance of IFLA_VLAN_QOS_*
IFLA_VLAN_INGRESS_QOS = 4 # followed by instance of IFLA_VLAN_QOS_*
IFLA_VLAN_PROTOCOL = 5
IFLA_VLAN_QOS_UNSPEC = 0
IFLA_VLAN_QOS_MAPPING = 1
IFLA_MACVLAN_UNSPEC = 0
IFLA_MACVLAN_MODE = 1
IFLA_MACVLAN_FLAGS = 2
IFLA_MACVLAN_MACADDR_MODE = 3
IFLA_MACVLAN_MACADDR = 4
IFLA_MACVLAN_MACADDR_DATA = 5
IFLA_MACVLAN_MACADDR_COUNT = 6
MACVLAN_MODE_PRIVATE = 1 # don't talk to other macvlans
MACVLAN_MODE_VEPA = 2 # talk to other ports through ext bridge
MACVLAN_MODE_BRIDGE = 4 # talk to bridge ports directly
MACVLAN_MODE_PASSTHRU = 8 # take over the underlying device
MACVLAN_MODE_SOURCE = 16 # use source MAC address list to assign
# TODO: VXLAN and VF stuff is missing here
# linux/if_vlan.h
VLAN_FLAG_REORDER_HDR = 0x1
VLAN_FLAG_GVRP = 0x2
VLAN_FLAG_LOOSE_BINDING = 0x4
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_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_MANAGETEMPADDR = 0x100
IFA_F_NOPREFIXROUTE = 0x200
IFA_F_MCAUTOJOIN = 0x400
IFA_F_STABLE_PRIVACY = 0x800
# from linux/if_arp.h - selected subset
ARPHRD_NETROM = 0
ARPHRD_ETHER = 1
ARPHRD_EETHER = 2
ARPHRD_AX25 = 3
ARPHRD_PRONET = 4
ARPHRD_CHAOS = 5
ARPHRD_IEEE802 = 6
ARPHRD_ARCNET = 7
ARPHRD_APPLETLK = 8
ARPHRD_DLCI = 15
ARPHRD_ATM = 19
ARPHRD_METRICOM = 23