model.lua 10.9 KB
Newer Older
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
1 2 3 4 5 6 7 8 9 10 11
--[[
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'
12
require 'awall.iptables'
13
require 'awall.object'
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
14
require 'awall.optfrag'
15
require 'awall.util'
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
16 17 18 19

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

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


23
ConfigObject = class()
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
24

25
function ConfigObject:init(context, location)
26 27
   if context then
      self.context = context
28
      self.root = context.objects
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
29
   end
30
   self.location = location
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
31 32
end

33
function ConfigObject:create(cls, params)
34 35 36 37 38 39 40
   if type(cls) == 'string' then
      local name = cls
      cls = awall.classmap[cls]
      if not cls then
	 self:error('Support for '..name..' objects not installed')
      end
   end
41 42 43 44 45
   return cls.morph(params, self.context, self.location)
end

function ConfigObject:error(msg) error(self.location..': '..msg) end

46 47 48 49
function ConfigObject:warning(msg)
   io.stderr:write(self.location..': '..msg..'\n')
end

50
function ConfigObject:trules() return {} end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
51

52 53 54 55
function ConfigObject:info()
   local res = {}
   for i, trule in ipairs(self:trules()) do
      table.insert(res,
56 57
		   {'  '..awall.optfrag.location(trule),
		    (trule.opts and trule.opts..' ' or '')..'-j '..trule.target})
58 59 60 61
   end
   return res
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
62

63
Zone = class(ConfigObject)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
64 65 66 67 68 69 70 71 72

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

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
73 74 75 76
   local aopts = nil
   if self.addr then
      aopts = {}
      for i, hostdef in util.listpairs(self.addr) do
77
	 for i, addr in ipairs(awall.host.resolve(hostdef, self)) do
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
78 79 80 81 82
	    table.insert(aopts,
			 {family=addr[1],
			  [aprop]=addr[2],
			  opts='-'..aopt..' '..addr[2]})
	 end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      end
   end

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


fwzone = Zone.new()


98 99
Rule = class(ConfigObject)

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
100

101 102
function Rule:init(...)
   ConfigObject.init(self, unpack(arg))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
103

104 105
   self.newchains = {}

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
106 107 108
   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
109
						  if type(z) ~= 'string' then return z end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
110
						  return z == '_fw' and fwzone or
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
111
						     self.root.zone[z] or
112
						     self:error('Invalid zone: '..z)
113
					       end)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
114
   end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
115

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
116
   if self.service then
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
117
      if type(self.service) == 'string' then self.label = self.service end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
118 119
      self.service = util.maplist(self.service,
				  function(s)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
120
				     if type(s) ~= 'string' then return s end
121
				     return self.root.service[s] or self:error('Invalid service: '..s)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
122 123 124 125 126
				  end)
   end
end


127 128 129 130 131 132 133
function Rule:direction(dir)
   if dir == 'in' then return self.reverse and 'out' or 'in' end
   if dir == 'out' then return self.reverse and 'in' or 'out' end
   self:error('Invalid direction: '..dir)
end


Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
134 135
function Rule:zoneoptfrags()

136
   local function zonepair(zin, zout)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
137

138
      local function zofs(zone, dir)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
139
	 if not zone then return zone end
140
	 return zone:optfrags(dir)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
141 142 143 144 145
      end

      local chain, ofrags

      if zin == fwzone or zout == fwzone then
146
	 if zin == zout then return {} end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
147 148 149 150 151
	 local dir, z = 'in', zin
	 if zin == fwzone then dir, z = 'out', zout end
	 chain = string.upper(dir)..'PUT'
	 ofrags = zofs(z, dir)

152 153 154 155 156 157 158 159 160 161 162
      elseif not zin or not zout then

	 if zin then
	    chain = 'PREROUTING'
	    ofrags = zofs(zin, 'in')

	 elseif zout then
	    chain = 'POSTROUTING'
	    ofrags = zofs(zout, 'out')
	 end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
163 164
      else
	 chain = 'FORWARD'
165 166
	 ofrags = combinations(zofs(zin, 'in'), zofs(zout, 'out'))

167
	 if ofrags and not zout['route-back'] then
168 169 170 171 172 173
	    ofrags = util.filter(ofrags,
				 function(of)
				    return not (of['in'] and of.out and
						of['in'] == of.out)
				 end)
	 end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
174 175
      end

176 177 178
      return combinations(ofrags,
			  chain and {{chain=chain}} or {{chain='PREROUTING'},
							{chain='OUTPUT'}})
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
179 180 181
   end

   local res = {}
182 183
   local izones = self[self:direction('in')] or {}
   local ozones = self[self:direction('out')] or {}
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
184

185 186 187
   for i = 1,math.max(1, table.maxn(izones)) do
      for j = 1,math.max(1, table.maxn(ozones)) do
	 util.extend(res, zonepair(izones[i], ozones[j]))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
188 189 190 191 192 193 194 195 196 197 198
      end
   end

   return res
end


function Rule:servoptfrags()

   if not self.service then return end

199
   local function containskey(tbl, key)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
200 201 202 203 204 205 206 207 208
      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
209
	 if not sdef.proto then self:error('Protocol not defined') end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
210 211 212 213 214 215 216

	 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
217 218 219
		  util.extend(ports[sdef.proto],
			      util.maplist(sdef.port,
					   function(p) return string.gsub(p, '-', ':') end))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
220 221 222 223 224 225 226 227
	       else ports[sdef.proto] = {} end
	    end

	 else

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

228 229 230 231 232 233 234 235 236
	    -- TODO multiple ICMP types per rule
	    local oname
	    if util.contains({1, 'icmp'}, sdef.proto) then
	       family = 'inet'
	       oname = 'icmp-type'
	    elseif util.contains({58, 'ipv6-icmp', 'icmpv6'}, sdef.proto) then
	       family = 'inet6'
	       oname = 'icmpv6-type'
	    elseif sdef.type then
237
	       self:error('Type specification not valid with '..sdef.proto)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
238
	    end
239
	    if sdef.type then opts = opts..' --'..oname..' '..sdef.type end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
240 241 242 243 244 245

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

246
   local popt = ' --'..(self.reverse and 's' or 'd')..'port'
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
247 248 249 250 251
   for proto, plist in pairs(ports) do
      local opts = '-p '..proto
      local len = table.maxn(plist)

      if len == 1 then
252
	 opts = opts..popt..' '..plist[1]
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
253
      elseif len > 1 then
254
	 opts = opts..' -m multiport'..popt..'s '..table.concat(plist, ',')
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
255 256 257 258 259 260 261 262
      end

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

   return res
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
263
function Rule:destoptfrags()
264
   return self:create(Zone, {addr=self.dest}):optfrags(self:direction('out'))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
265 266
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
267 268 269 270 271
function Rule:table() return 'filter' end

function Rule:position() return 'append' end

function Rule:target()
272
   if not self.action then self:error('Action not defined') end
273 274 275 276
   if util.contains({'accept', 'drop', 'reject'}, self.action) then
      return string.upper(self.action)
   end
   return self.action
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
277 278 279 280 281
end


function Rule:trules()

282
   local function tag(ofrags, tag, value)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
283 284 285 286 287 288 289 290
      for i, ofrag in ipairs(ofrags) do
	 assert(not ofrag[tag])
	 ofrag[tag] = value
      end
   end

   local families

291
   local function setfamilies(ofrags)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
292 293 294 295 296 297 298 299 300 301 302 303
      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

304
   local function ffilter(ofrags)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
305
      if not ofrags or not ofrags[1] or not families then return ofrags end
306 307 308 309 310
      return util.filter(ofrags,
			 function(of)
			    return not of.family or util.contains(families,
								  of.family)
			 end)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
311 312 313 314
   end

   local res = self:zoneoptfrags()

315
   if self.ipset then
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
316 317
      local ipsetofrags = {}
      for i, ipset in util.listpairs(self.ipset) do
318
	 if not ipset.name then self:error('Set name not defined') end
319

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
320
	 local setdef = self.root.ipset and self.root.ipset[ipset.name]
321
	 if not setdef then self:error('Invalid set name') end
322

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
323
	 if not ipset.args then
324
	    self:error('Set direction arguments not defined')
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
325
	 end
326

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
327
	 local setopts = '-m set --match-set '..ipset.name..' '
328 329
	 setopts = setopts..table.concat(util.map(util.list(ipset.args),
						  function(a)
330 331 332 333
						     if self:direction(a) == 'in' then
							return 'src'
						     end
						     return 'dst'
334 335
						  end),
					 ',')
336
	 table.insert(ipsetofrags, {family=setdef.family, opts=setopts})
337
      end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
338
      res = combinations(res, ipsetofrags)
339 340
   end

341
   if self.ipsec then
342 343
      res = combinations(res,
			 {{opts='-m policy --pol ipsec --dir '..self:direction(self.ipsec)}})
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
344 345 346 347 348 349
   end

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

   setfamilies(res)

350 351
   local addrofrags = combinations(self:create(Zone,
					       {addr=self.src}):optfrags(self:direction('in')),
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
352
				   self:destoptfrags())
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367

   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
368
      target = self:newchain('address')
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
369 370
   else
      target = self:target()
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
371
      res = combinations(res, addrofrags)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
372 373 374 375
   end

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

376
   res = combinations(res, {{target=target}})
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
377 378 379 380

   if addrchain then
      for i, ofrag in ipairs(addrofrags) do
	 ofrag.chain = target
381
	 ofrag.target = self:target()
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
382 383 384 385
	 table.insert(res, ofrag)
      end      
   end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
386
   util.extend(res, ffilter(self:extraoptfrags()))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
387

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
   local tbl = self:table()

   local function convertchains(ofrags)
      local res = {}

      for i, ofrag in ipairs(ofrags) do

	 if util.contains(awall.iptables.builtin[tbl], ofrag.chain) then
	    table.insert(res, ofrag)

	 else
	    local chains
	    if ofrag.chain == 'PREROUTING' then chains = {'FORWARD', 'INPUT'}
	    elseif ofrag.chain == 'POSTROUTING' then
	       chains = {'FORWARD', 'OUTPUT'}
	    elseif util.contains({'INPUT', 'FORWARD'}, ofrag.chain) then
	       chains = {'PREROUTING'}
	    end

	    if chains then
	       ofrag.chain = nil
	       util.extend(res,
			   convertchains(combinations({ofrag},
						      util.map(chains,
							       function(c)
								  return {chain=c}
							       end))))
	    else table.insert(res, ofrag) end
	 end
      end

      return res
   end

   res = convertchains(res)
   tag(res, 'table', tbl, false)
424 425 426 427 428 429 430 431 432 433 434

   local function checkzof(ofrag, dir, chains)
      if ofrag[dir] and util.contains(chains, ofrag.chain) then
	 self:error('Cannot specify '..dir..'bound interface ('..ofrag[dir]..')')
      end
   end

   for i, ofrag in ipairs(res) do
      checkzof(ofrag, 'in', {'OUTPUT', 'POSTROUTING'})
      checkzof(ofrag, 'out', {'INPUT', 'PREROUTING'})
   end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
435
   
436
   return combinations(res, ffilter({{family='inet'}, {family='inet6'}}))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
437 438 439 440
end

function Rule:extraoptfrags() return {} end

441 442 443
function Rule:newchain(key)
   if self.newchains[key] then return self.newchains[key] end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
444 445 446
   if not self.context.lastid then self.context.lastid = {} end
   local lastid = self.context.lastid

447 448 449 450 451 452 453 454
   local res = key
   if self.label then res = res..'-'..self.label end
   if not lastid[res] then lastid[res] = -1 end
   lastid[res] = lastid[res] + 1
   res = res..'-'..lastid[res]

   self.newchains[key] = res
   return res
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
455 456
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
457

458
classes = {{'zone', Zone}}
459