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


module(..., package.seeall)

require 'awall'
require 'awall.host'
require 'awall.util'
13
require 'awall.object'
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
14 15 16 17 18
require 'awall.optfrag'

local util = awall.util
local combinations = awall.optfrag.combinations

19
class = awall.object.class
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
20 21


22
ConfigObject = class(awall.object.Object)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
23

24 25 26 27
function ConfigObject:init(context)
   if context then
      self.context = context
      self.root = context.input
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
28 29 30
   end
end

31
function ConfigObject:trules() return {} end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
32 33


34
Zone = class(ConfigObject)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

function Zone:optfrags(dir)
   local iopt, aopt, iprop, aprop
   if dir == 'in' then
      iopt, aopt, iprop, aprop = 'i', 's', 'in', 'src'
   elseif dir == 'out' then
      iopt, aopt, iprop, aprop = 'o', 'd', 'out', 'dest'
   else assert(false) end

   local aopts = {}
   for i, hostdef in util.listpairs(self.addr) do
      for i, addr in ipairs(awall.host.resolve(hostdef)) do
	 table.insert(aopts,
		      {family=addr[1],
		       [aprop]=addr[2],
		       opts='-'..aopt..' '..addr[2]})
      end
   end
   if not aopts[1] then aopts = nil end

   return combinations(util.maplist(self.iface,
				    function(x)
				       return {[iprop]=x,
					       opts='-'..iopt..' '..x}
				    end),
		       aopts)
end


fwzone = Zone.new()


67 68
Rule = class(ConfigObject)

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
69

70 71
function Rule:init(context)
   ConfigObject.init(self, context)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
72 73 74 75

   for i, prop in ipairs({'in', 'out'}) do
      self[prop] = self[prop] and util.maplist(self[prop],
					       function(z)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
76
						  if type(z) ~= 'string' then return z end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
77
						  return z == '_fw' and fwzone or
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
78
						     self.root.zone[z] or
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
79 80 81
						     error('Invalid zone: '..z)
					       end) or self:defaultzones()
   end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
82

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
83
   if self.service then
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
84
      if type(self.service) == 'string' then self.label = self.service end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
85 86
      self.service = util.maplist(self.service,
				  function(s)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
87
				     if type(s) ~= 'string' then return s end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
88
				     return self.root.service[s] or error('Invalid service: '..s)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
89 90 91 92 93 94 95 96 97 98 99 100
				  end)
   end
end

function Rule:defaultzones() return {nil, fwzone} end


function Rule:checkzoneoptfrag(ofrag) end


function Rule:zoneoptfrags()

101
   local function zonepair(zin, zout)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
102 103
      assert(zin ~= zout or not zin)

104
      local function zofs(zone, dir)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	 if not zone then return zone end
	 local ofrags = zone:optfrags(dir)
	 util.map(ofrags, function(x) self:checkzoneoptfrag(x) end)
	 return ofrags
      end

      local chain, ofrags

      if zin == fwzone or zout == fwzone then
	 local dir, z = 'in', zin
	 if zin == fwzone then dir, z = 'out', zout end
	 chain = string.upper(dir)..'PUT'
	 ofrags = zofs(z, dir)

      else
	 chain = 'FORWARD'
	 ofrags = combinations(zofs(zin, 'in'),
			       zofs(zout, 'out'))
      end

      if not ofrags then ofrags = {{}} end

      for i, ofrag in ipairs(ofrags) do ofrag.fchain = chain end

      return ofrags
   end

   local res = {}

   for i = 1,math.max(1, table.maxn(self['in'])) do
      izone = self['in'][i]
      for i = 1,math.max(1, table.maxn(self.out)) do
	 ozone = self.out[i]
	 if izone ~= ozone or not izone then
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
139
	    util.extend(res, zonepair(izone, ozone))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
140 141 142 143 144 145 146 147 148 149 150 151
	 end
      end
   end

   return res
end


function Rule:servoptfrags()

   if not self.service then return end

152
   local function containskey(tbl, key)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
      for k, v in pairs(tbl) do if k == key then return true end end
      return false
   end

   local ports = {}
   local res = {}

   for i, serv in ipairs(self.service) do
      for i, sdef in util.listpairs(serv) do
	 if not sdef.proto then error('Protocol not defined') end

	 if util.contains({6, 'tcp', 17, 'udp'}, sdef.proto) then
	    local new = not containskey(ports, sdef.proto)
	    if new then ports[sdef.proto] = {} end

	    if new or ports[sdef.proto][1] then
	       if sdef.port then
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
170
		  util.extend(ports[sdef.proto], sdef.port)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
171 172 173 174 175 176 177 178 179 180 181 182
	       else ports[sdef.proto] = {} end
	    end

	 else

	    local opts = '-p '..sdef.proto
	    local family = nil

	    if sdef.type then
	       -- TODO multiple ICMP types per rule
	       local oname
	       if util.contains({1, 'icmp'}, sdef.proto) then
183
		  family = 'inet'
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
184 185
		  oname = 'icmp-type'
	       elseif util.contains({58, 'ipv6-icmp', 'icmpv6'}, sdef.proto) then
186
		  family = 'inet6'
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
		  oname = 'icmpv6-type'
	       else error('Type specification not valid with '..sdef.proto) end
	       opts = opts..' --'..oname..' '..sdef.type
	    end

	    table.insert(res, {family=family, opts=opts})
	 end
      end
   end

   for proto, plist in pairs(ports) do
      local opts = '-p '..proto
      local len = table.maxn(plist)

      if len == 1 then
	 opts = opts..' --dport '..plist[1]
      elseif len > 1 then
	 opts = opts..' -m multiport --dports '
	 for i, port in ipairs(plist) do
	    if i > 1 then opts = opts..',' end
	    opts = opts..port
	 end
      end

      table.insert(res, {opts=opts})
   end

   return res
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
217 218 219 220
function Rule:destoptfrags()
   return Zone.morph({addr=self.dest}):optfrags('out')
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234
function Rule:table() return 'filter' end

function Rule:chain() return nil end

function Rule:position() return 'append' end

function Rule:target()
   if not self.action then error('Action not defined') end
   return string.upper(self.action)
end


function Rule:trules()

235
   local function tag(ofrags, tag, value)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
236 237 238 239 240 241 242 243
      for i, ofrag in ipairs(ofrags) do
	 assert(not ofrag[tag])
	 ofrag[tag] = value
      end
   end

   local families

244
   local function setfamilies(ofrags)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
245 246 247 248 249 250 251 252 253 254 255 256
      if ofrags then
	 families = {}
	 for i, ofrag in ipairs(ofrags) do
	    if not ofrag.family then
	       families = nil
	       return
	    end
	    table.insert(families, ofrag.family)
	 end
      else families = nil end
   end

257
   local function ffilter(ofrags)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
258 259 260 261 262 263 264 265 266 267
      if not ofrags or not ofrags[1] or not families then return ofrags end
      local res = {}
      for i, ofrag in util.listpairs(ofrags) do
	 if not ofrag.family or util.contains(families, ofrag.family) then
	    table.insert(res, ofrag)
	 end
      end
      return res
   end

268
   local function appendtarget(ofrag, target)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
269 270 271 272 273
      ofrag.opts = (ofrag.opts and ofrag.opts..' ' or '')..'-j '..target
   end

   local res = self:zoneoptfrags()

274
   if self.ipset then
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
275 276 277
      local ipsetofrags = {}
      for i, ipset in util.listpairs(self.ipset) do
	 if not ipset.name then error('Set name not defined') end
278

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
279
	 local setdef = self.root.ipset and self.root.ipset[ipset.name]
280 281
	 if not setdef then error('Invalid set name') end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
282 283 284
	 if not ipset.args then
	    error('Set direction arguments not defined')
	 end
285

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
286 287 288 289 290 291 292
	 local setopts = '-m set --match-set '..ipset.name..' '
	 for i, arg in util.listpairs(ipset.args) do
	    if i > 1 then setopts = setopts..',' end
	    if arg == 'in' then setopts = setopts..'src'
	    elseif arg == 'out' then setopts = setopts..'dst'
	    else error('Invalid set direction argument') end
	 end
293
	 table.insert(ipsetofrags, {family=setdef.family, opts=setopts})
294
      end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
295
      res = combinations(res, ipsetofrags)
296 297
   end

298 299
   if self.ipsec then
      res = combinations(res, {{opts='-m policy --pol ipsec --dir '..self.ipsec}})
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
300 301 302 303 304 305 306
   end

   res = combinations(res, self:servoptfrags())

   setfamilies(res)
   tag(res, 'chain', self:chain())

307
   local addrofrags = combinations(Zone.morph({addr=self.src}):optfrags('in'),
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
308
				   self:destoptfrags())
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

   if addrofrags then
      addrofrags = ffilter(addrofrags)
      setfamilies(addrofrags)
      res = ffilter(res)
   end

   local addrchain = false
   for i, ofrag in ipairs(res) do
      if not ofrag.chain then ofrag.chain = ofrag.fchain end
      addrchain = addrchain or (self.src and ofrag.src) or (self.dest and ofrag.dest)
   end

   local target
   if addrchain then
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
324
      target = self:newchain('address')
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
   else
      target = self:target()
      if addrofrags then res = combinations(res, addrofrags) end
   end

   tag(res, 'position', self:position())

   for i, ofrag in ipairs(res) do appendtarget(ofrag, target) end

   if addrchain then
      for i, ofrag in ipairs(addrofrags) do
	 ofrag.chain = target
	 appendtarget(ofrag, self:target())
	 table.insert(res, ofrag)
      end      
   end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
342
   util.extend(res, ffilter(self:extraoptfrags()))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
343 344 345

   tag(res, 'table', self:table(), false)
   
346
   return combinations(res, ffilter({{family='inet'}, {family='inet6'}}))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
347 348 349 350
end

function Rule:extraoptfrags() return {} end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
351
function Rule:newchain(base)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
352 353 354
   if not self.context.lastid then self.context.lastid = {} end
   local lastid = self.context.lastid

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
355 356 357 358 359 360
   if self.label then base = base..'-'..self.label end
   if not lastid[base] then lastid[base] = -1 end
   lastid[base] = lastid[base] + 1
   return base..'-'..lastid[base]
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
361

362
classes = {{'zone', Zone}}
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
363
defrules = {}