nat.lua 1.4 KB
Newer Older
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
--[[
NAT module for Alpine Wall
Copyright (C) 2012 Kaarle Ritvanen
Licensed under the terms of GPL2
]]--


module(..., package.seeall)

require 'awall.model'

local model = awall.model


15
local NATRule = model.class(model.ForwardOnlyRule)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
16 17 18

function NATRule:trules()
   local res = {}
19
   for i, ofrags in ipairs(model.ForwardOnlyRule.trules(self)) do
20
      if ofrags.family == 'inet' then table.insert(res, ofrags) end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
21 22 23 24 25 26 27 28 29
   end
   return res
end

function NATRule:table() return 'nat' end

function NATRule:chain() return self.params.chain end

function NATRule:target()
30
   if self.action then return model.ForwardOnlyRule.target(self) end
31 32 33 34 35 36

   local target
   if self['ip-range'] then
      target = self.params.target..' --to-'..self.params.subject..' '..self['ip-range']
   else target = self.params.deftarget end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
37 38 39 40 41 42 43
   if self['port-range'] then target = target..':'..self['port-range'] end
   return target
end


local DNATRule = model.class(NATRule)

44 45
function DNATRule:init(...)
   NATRule.init(self, unpack(arg))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
46
   self.params = {forbidif='out', subject='destination',
47
		  chain='PREROUTING', target='DNAT', deftarget='REDIRECT'}
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
48 49 50 51 52
end


local SNATRule = model.class(NATRule)

53 54
function SNATRule:init(...)
   NATRule.init(self, unpack(arg))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
55
   self.params = {forbidif='in', subject='source',
56
		  chain='POSTROUTING', target='SNAT', deftarget='MASQUERADE'}
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
57 58 59
end


60 61
classes = {{'dnat', DNATRule},
	   {'snat', SNATRule}}
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
62

63
defrules = {}