Class: Puffy::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/puffy/rule.rb

Overview

Abstract firewall rule.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Rule

Instanciate a firewall Puffy::Rule.

options is a Hash of the Puffy::Rule class attributes

Rule.new({ action: :accept, dir: :in, proto: :tcp, to: { port: 80 } })


64
65
66
67
68
69
70
71
# File 'lib/puffy/rule.rb', line 64

def initialize(options = {})
  send_options(options)

  @af = detect_af unless af

  raise "unsupported action `#{options[:action]}'" unless valid_action?
  raise 'if from_port or to_port is specified, the protocol must also be given' if port_without_protocol?
end

Instance Attribute Details

#actionSymbol

The action to perform when the rule apply (:accept or :block).

Returns:

  • (Symbol)

    Action



57
58
59
# File 'lib/puffy/rule.rb', line 57

def action
  @action
end

#afSymbol

The address family of the rule (:inet6 or :inet)

Returns:

  • (Symbol)

    Address family



57
# File 'lib/puffy/rule.rb', line 57

attr_accessor :action, :return, :dir, :proto, :af, :on, :in, :out, :from, :to, :nat_to, :rdr_to, :no_quick

#dirSymbol

The direction of the rule (:in or :out).

Returns:

  • (Symbol)

    Direction



57
# File 'lib/puffy/rule.rb', line 57

attr_accessor :action, :return, :dir, :proto, :af, :on, :in, :out, :from, :to, :nat_to, :rdr_to, :no_quick

#fromHash

The packet source as a Hash for the rule to apply.

:host

address of the source host or network the rule apply to

:port

source port the rule apply to

Returns:



57
# File 'lib/puffy/rule.rb', line 57

attr_accessor :action, :return, :dir, :proto, :af, :on, :in, :out, :from, :to, :nat_to, :rdr_to, :no_quick

#inString

The interface packets must arrive on for the rule to apply in a forwarding context.

Returns:

  • (String)

    Interface



57
# File 'lib/puffy/rule.rb', line 57

attr_accessor :action, :return, :dir, :proto, :af, :on, :in, :out, :from, :to, :nat_to, :rdr_to, :no_quick

#nat_toIPAddr

The packet destination when peforming NAT.

Returns:

  • (IPAddr)

    IP Adress



57
# File 'lib/puffy/rule.rb', line 57

attr_accessor :action, :return, :dir, :proto, :af, :on, :in, :out, :from, :to, :nat_to, :rdr_to, :no_quick

#no_quickBoolean

Prevent the rule from being a quick one.

Returns:

  • (Boolean)

    Quick flag



57
# File 'lib/puffy/rule.rb', line 57

attr_accessor :action, :return, :dir, :proto, :af, :on, :in, :out, :from, :to, :nat_to, :rdr_to, :no_quick

#onString

The interface the rule applies to.

Returns:

  • (String)

    Interface



57
# File 'lib/puffy/rule.rb', line 57

attr_accessor :action, :return, :dir, :proto, :af, :on, :in, :out, :from, :to, :nat_to, :rdr_to, :no_quick

#outString

The interface packets must be sent to for the rule to apply in a forwarding context.

Returns:

  • (String)

    Interface



57
# File 'lib/puffy/rule.rb', line 57

attr_accessor :action, :return, :dir, :proto, :af, :on, :in, :out, :from, :to, :nat_to, :rdr_to, :no_quick

#protoSymbol

The protocol the Puffy::Rule applies to (:tcp, :udp, etc).

Returns:

  • (Symbol)

    Protocol



57
# File 'lib/puffy/rule.rb', line 57

attr_accessor :action, :return, :dir, :proto, :af, :on, :in, :out, :from, :to, :nat_to, :rdr_to, :no_quick

#rdr_toHash

The destination as a Hash for redirections.

:host

address of the destination host or network the rule apply to

:port

destination port the rule apply to

Returns:

  • (Hash)

    Destination



57
# File 'lib/puffy/rule.rb', line 57

attr_accessor :action, :return, :dir, :proto, :af, :on, :in, :out, :from, :to, :nat_to, :rdr_to, :no_quick

#returnBoolean

Whether blocked packets must be returned to sender instead of being silently dropped.

Returns:

  • (Boolean)

    Return flag



57
# File 'lib/puffy/rule.rb', line 57

attr_accessor :action, :return, :dir, :proto, :af, :on, :in, :out, :from, :to, :nat_to, :rdr_to, :no_quick

#toHash

The packet destination as a Hash for the rule to apply.

:host

address of the destination host or network the rule apply to

:port

destination port the rule apply to

Returns:

  • (Hash)

    Destination



57
# File 'lib/puffy/rule.rb', line 57

attr_accessor :action, :return, :dir, :proto, :af, :on, :in, :out, :from, :to, :nat_to, :rdr_to, :no_quick

Class Method Details

.fwd_rule(rule) ⇒ Puffy::Rule

Instanciate a forward Puffy::Rule.

Parameters:

Returns:



78
79
80
81
82
83
84
85
# File 'lib/puffy/rule.rb', line 78

def self.fwd_rule(rule)
  res = rule.dup
  res.on_to_in_out!
  res.to.merge!(res.rdr_to.compact)
  res.rdr_to = nil
  res.dir = :fwd
  res
end

Instance Method Details

#filter?Boolean

Return true if the rule is a filter rule.

Returns:

  • (Boolean)


108
109
110
# File 'lib/puffy/rule.rb', line 108

def filter?
  !nat? && !rdr?
end

#from_hostObject

Returns the source host of the Puffy::Rule.



149
150
151
152
153
154
155
156
# File 'lib/puffy/rule.rb', line 149

%i[from to rdr_to].each do |destination|
  %i[host port].each do |param|
    define_method("#{destination}_#{param}") do
      res = public_send(destination)
      res && res[param]
    end
  end
end

#from_portObject

Returns the source port of the Puffy::Rule.



149
150
151
152
153
154
155
156
# File 'lib/puffy/rule.rb', line 149

%i[from to rdr_to].each do |destination|
  %i[host port].each do |param|
    define_method("#{destination}_#{param}") do
      res = public_send(destination)
      res && res[param]
    end
  end
end

#fwd?Boolean

Returns whether the rule performs forwarding.

Returns:

  • (Boolean)


133
134
135
# File 'lib/puffy/rule.rb', line 133

def fwd?
  dir == :fwd
end

#implicit_ipv4?Boolean

Return true if the rule has an IPv4 source or destination.

Returns:

  • (Boolean)


93
94
95
# File 'lib/puffy/rule.rb', line 93

def implicit_ipv4?
  from_ipv4? || to_ipv4? || rdr_to_ipv4? || (rdr_to && af == :inet)
end

#implicit_ipv6?Boolean

Return true if the rule has an IPv6 source or destination.

Returns:

  • (Boolean)


103
104
105
# File 'lib/puffy/rule.rb', line 103

def implicit_ipv6?
  from_ipv6? || to_ipv6? || rdr_to_ipv6? || (rdr_to && af == :inet6)
end

#in?Boolean

Returns whether the rule applies to incomming packets.

Returns:

  • (Boolean)


113
114
115
# File 'lib/puffy/rule.rb', line 113

def in?
  dir.nil? || dir == :in
end

#ipv4?Boolean

Return true if the rule is valid in an IPv4 context.

Returns:

  • (Boolean)


88
89
90
# File 'lib/puffy/rule.rb', line 88

def ipv4?
  af.nil? || af == :inet
end

#ipv6?Boolean

Return true if the rule is valid in an IPv6 context.

Returns:

  • (Boolean)


98
99
100
# File 'lib/puffy/rule.rb', line 98

def ipv6?
  af.nil? || af == :inet6
end

#nat?Boolean

Returns whether the rule performs Network Address Translation.

Returns:

  • (Boolean)


123
124
125
# File 'lib/puffy/rule.rb', line 123

def nat?
  nat_to
end

#on_to_in_out!void

This method returns an undefined value.

Setsthe #in / #out to #on depending on #dir.



161
162
163
164
165
166
167
168
# File 'lib/puffy/rule.rb', line 161

def on_to_in_out!
  if dir == :in
    self.in ||= on
  else
    self.out ||= on
  end
  self.on = nil
end

#out?Boolean

Returns whether the rule applies to outgoing packets.

Returns:

  • (Boolean)


118
119
120
# File 'lib/puffy/rule.rb', line 118

def out?
  dir.nil? || dir == :out
end

#rdr?Boolean

Returns whether the rule is a redirection.

Returns:

  • (Boolean)


128
129
130
# File 'lib/puffy/rule.rb', line 128

def rdr?
  rdr_to_host || rdr_to_port
end

#rdr_to_hostObject

Returns the redirect destination host of the Puffy::Rule.



149
150
151
152
153
154
155
156
# File 'lib/puffy/rule.rb', line 149

%i[from to rdr_to].each do |destination|
  %i[host port].each do |param|
    define_method("#{destination}_#{param}") do
      res = public_send(destination)
      res && res[param]
    end
  end
end

#rdr_to_portObject

Returns the redirect destination port of the Puffy::Rule.



149
150
151
152
153
154
155
156
# File 'lib/puffy/rule.rb', line 149

%i[from to rdr_to].each do |destination|
  %i[host port].each do |param|
    define_method("#{destination}_#{param}") do
      res = public_send(destination)
      res && res[param]
    end
  end
end

#to_hostObject

Returns the destination host of the Puffy::Rule.



149
150
151
152
153
154
155
156
# File 'lib/puffy/rule.rb', line 149

%i[from to rdr_to].each do |destination|
  %i[host port].each do |param|
    define_method("#{destination}_#{param}") do
      res = public_send(destination)
      res && res[param]
    end
  end
end

#to_portObject

Returns the destination port of the Puffy::Rule.



149
150
151
152
153
154
155
156
# File 'lib/puffy/rule.rb', line 149

%i[from to rdr_to].each do |destination|
  %i[host port].each do |param|
    define_method("#{destination}_#{param}") do
      res = public_send(destination)
      res && res[param]
    end
  end
end