model.lua 15.7 KB
Newer Older
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
1 2
--[[
Base data model for Alpine Wall
3
Copyright (C) 2012-2016 Kaarle Ritvanen
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
4
See LICENSE file for license details
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
5 6 7
]]--


8
local M = {}
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
9 10


11 12 13 14
local loadclass = require('awall').loadclass
M.class = require('awall.class')
local resolve = require('awall.host')
local builtin = require('awall.iptables').builtin
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
15

16 17
local optfrag = require('awall.optfrag')
local combinations = optfrag.combinations
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
18

19
local raise = require('awall.uerror').raise
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
20

21 22 23 24
local util = require('awall.util')
local contains = util.contains
local extend = util.extend
local filter = util.filter
25
local join = util.join
26 27
local listpairs = util.listpairs
local maplist = util.maplist
28
local setdefault = util.setdefault
29

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
30

31 32 33 34 35 36
local startswith = require('stringy').startswith


M.ConfigObject = M.class()

function M.ConfigObject:init(context, location)
37 38
   if context then
      self.context = context
39
      self.root = context.objects
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
40
   end
41
   self.location = location
42 43

   self.extraobjs = {}
44
   self.uniqueids = {}
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
45 46
end

47 48 49 50 51 52 53 54
function M.ConfigObject:create(cls, params, label, index)
   local key
   if label then
      key = label..(index or '')
      local obj = self.extraobjs[key]
      if obj then return obj end
   end

55 56
   if type(cls) == 'string' then
      local name = cls
57
      cls = loadclass(cls)
58 59 60 61
      if not cls then
	 self:error('Support for '..name..' objects not installed')
      end
   end
62

63
   if type(params) ~= 'table' then params = {params} end
64
   params.label = join(self.label, '-', label)
65

66 67 68
   local obj = cls.morph(params, self.context, self.location)
   if key then self.extraobjs[key] = obj end
   return obj
69 70
end

71
function M.ConfigObject:uniqueid(key)
72
   if not key then key = '' end
73 74
   if self.uniqueids[key] then return self.uniqueids[key] end

75
   local lastid = setdefault(self.context, 'lastid', {})
76
   local res = join(key, '-', self.label)
77
   lastid[res] = setdefault(lastid, res, -1) + 1
78 79 80 81 82 83
   res = res..'-'..lastid[res]

   self.uniqueids[key] = res
   return res
end

84
function M.ConfigObject:error(msg) raise(self.location..': '..msg) end
85

86
function M.ConfigObject:warning(msg)
87
   util.printmsg(self.location..': '..msg)
88 89
end

90
function M.ConfigObject:trules() return {} end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
91

92
function M.ConfigObject:info()
93 94
   local res = {}
   for i, trule in ipairs(self:trules()) do
95
      table.insert(res, {'  '..optfrag.location(trule), optfrag.command(trule)})
96 97 98 99
   end
   return res
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
100

101
M.Zone = M.class(M.ConfigObject)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
102

103
function M.Zone:optfrags(dir)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
104 105 106 107 108 109 110
   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
111 112 113
   local aopts = nil
   if self.addr then
      aopts = {}
114 115
      for i, hostdef in listpairs(self.addr) do
	 for i, addr in ipairs(resolve(hostdef, self)) do
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
116 117 118 119 120
	    table.insert(aopts,
			 {family=addr[1],
			  [aprop]=addr[2],
			  opts='-'..aopt..' '..addr[2]})
	 end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
121 122 123
      end
   end

124 125 126 127 128 129 130
   return combinations(
      maplist(
	 self.iface,
	 function(x) return {[iprop]=x, opts='-'..iopt..' '..x} end
      ),
      aopts
   )
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
131 132 133
end


134
M.fwzone = M.Zone()
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
135 136


137
local IPSet = M.class(M.ConfigObject)
138 139

function IPSet:init(...)
140
   IPSet.super(self):init(...)
141 142 143

   if not self.type then self:error('Type not defined') end

144
   if startswith(self.type, 'bitmap:') then
145 146 147 148
      if not self.range then self:error('Range not defined') end
      self.options = {self.type, 'range', self.range}
      self.family = 'inet'

149
   elseif startswith(self.type, 'hash:') then
150 151 152 153 154 155 156 157 158
      if not self.family then self:error('Family not defined') end
      self.options = {self.type, 'family', self.family}

   elseif self.type == 'list:set' then self.options = {self.type}

   else self:error('Invalid type: '..self.type) end
end


159
M.Rule = M.class(M.ConfigObject)
160

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
161

162 163
function M.Rule:init(...)
   M.Rule.super(self):init(...)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
164 165

   for i, prop in ipairs({'in', 'out'}) do
166 167 168 169 170 171 172 173 174
      self[prop] = self[prop] and maplist(
	 self[prop],
	 function(z)
	    if type(z) ~= 'string' then return z end
	    return z == '_fw' and M.fwzone or
	       self.root.zone[z] or
	       self:error('Invalid zone: '..z)
	 end
      )
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
175
   end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
176

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
177
   if self.service then
178 179 180 181
      if not self.label and type(self.service) == 'string' then
	 self.label = self.service
      end

182 183 184 185 186 187
      self.service = util.list(self.service)

      for i, serv in ipairs(self.service) do
	 if type(serv) == 'string' then
	    self.service[i] = self.root.service[serv] or
	       self:error('Invalid service: '..serv)
188
	 end
189 190 191 192 193 194 195
	 for i, sdef in listpairs(self.service[i]) do
	    if not sdef.proto then self:error('Protocol not defined') end
	    sdef.proto = (
	       {[1]='icmp', [6]='tcp', [17]='udp', [58]='ipv6-icmp'}
            )[sdef.proto] or sdef.proto
	 end
      end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
196 197 198 199
   end
end


200
function M.Rule:direction(dir)
201 202 203 204 205 206
   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


207
function M.Rule:zoneoptfrags()
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
208

209
   local function zonepair(zin, zout)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
210

211
      local function zofs(zone, dir)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
212
	 if not zone then return zone end
213
	 return zone:optfrags(dir)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
214 215 216 217
      end

      local chain, ofrags

218
      if zin == M.fwzone or zout == M.fwzone then
219
	 if zin == zout then return {} end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
220
	 local dir, z = 'in', zin
221
	 if zin == M.fwzone then dir, z = 'out', zout end
222
	 chain = dir:upper()..'PUT'
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
223 224
	 ofrags = zofs(z, dir)

225 226 227 228 229 230 231 232 233 234 235
      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
236 237
      else
	 chain = 'FORWARD'
238 239
	 ofrags = combinations(zofs(zin, 'in'), zofs(zout, 'out'))

240
	 if ofrags and not zout['route-back'] then
241 242 243 244 245 246
	    ofrags = filter(
	       ofrags,
	       function(of)
		  return not (of['in'] and of.out and of['in'] == of.out)
	       end
	    )
247
	 end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
248 249
      end

250 251 252
      return combinations(ofrags,
			  chain and {{chain=chain}} or {{chain='PREROUTING'},
							{chain='OUTPUT'}})
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
253 254 255
   end

   local res = {}
256 257
   local izones = self[self:direction('in')] or {}
   local ozones = self[self:direction('out')] or {}
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
258

259 260
   for i = 1,math.max(1, table.maxn(izones)) do
      for j = 1,math.max(1, table.maxn(ozones)) do
261
	 extend(res, zonepair(izones[i], ozones[j]))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
262 263 264 265 266 267 268
      end
   end

   return res
end


269
function M.Rule:servoptfrags()
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
270 271 272

   if not self.service then return end

273
   local fports = {inet={}, inet6={}}
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
274 275 276
   local res = {}

   for i, serv in ipairs(self.service) do
277
      for i, sdef in listpairs(serv) do
278
	 if contains({'tcp', 'udp'}, sdef.proto) then
279 280 281 282 283 284 285 286
	    for family, ports in pairs(fports) do
	       if not sdef.family or family == sdef.family then

		  local new = not ports[sdef.proto]
		  if new then ports[sdef.proto] = {} end

		  if new or ports[sdef.proto][1] then
		     if sdef.port then
287
			extend(
288
			   ports[sdef.proto],
289
			   maplist(
290
			      sdef.port,
291
			      function(p) return tostring(p):gsub('-', ':') end
292 293 294 295 296
			   )
			)
		     else ports[sdef.proto] = {} end
		  end
	       end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
297 298 299 300 301 302 303
	    end

	 else

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

304 305
	    -- TODO multiple ICMP types per rule
	    local oname
306
	    if sdef.proto == 'icmp' then
307 308
	       family = 'inet'
	       oname = 'icmp-type'
309
	    elseif contains({'ipv6-icmp', 'icmpv6'}, sdef.proto) then
310 311
	       family = 'inet6'
	       oname = 'icmpv6-type'
312
	    elseif sdef.type or sdef['reply-type'] then
313
	       self:error('Type specification not valid with '..sdef.proto)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
314 315
	    end

316 317 318 319 320 321 322 323 324
	    if sdef.family then
	       if not family then family = sdef.family
	       elseif family ~= sdef.family then
		  self:error(
		     'Protocol '..sdef.proto..' is incompatible with '..sdef.family
		  )
	       end
	    end

325 326
	    if sdef.type then
	       opts = opts..' --'..oname..' '..(
327
		  self.reverse and sdef['reply-type'] or sdef.type
328 329
	       )
	    end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
330 331 332 333 334
	    table.insert(res, {family=family, opts=opts})
	 end
      end
   end

335
   local popt = ' --'..(self.reverse and 's' or 'd')..'port'
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
   for family, pports in pairs(fports) do
      local ofrags = {}

      for proto, ports in pairs(pports) do
	 local propt = '-p '..proto

	 if ports[1] then
	    local len = #ports
	    repeat
	       local opts

	       if len == 1 then
		  opts = propt..popt..' '..ports[1]
		  len = 0

	       else
		  opts = propt..' -m multiport'..popt..'s '
		  local pc = 0
		  repeat
		     local sep = pc == 0 and '' or ','
		     local port = ports[1]
		     
358
		     pc = pc + (port:find(':') and 2 or 1)
359 360 361 362 363 364 365 366 367 368 369 370 371 372
		     if pc > 15 then break end
		     
		     opts = opts..sep..port
		     
		     table.remove(ports, 1)
		     len = len - 1
		  until len == 0
	       end

	       table.insert(ofrags, {opts=opts})
	    until len == 0

	 else table.insert(ofrags, {opts=propt}) end
      end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
373

374
      extend(res, combinations(ofrags, {{family=family}}))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
375 376 377 378 379
   end

   return res
end

380 381
function M.Rule:destoptfrags()
   return self:create(M.Zone, {addr=self.dest}):optfrags(self:direction('out'))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
382 383
end

384
function M.Rule:table() return 'filter' end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
385

386
function M.Rule:position() return 'append' end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
387

388
function M.Rule:target()
389 390 391 392 393 394 395 396 397 398
   -- alpine v2.7 compatibility
   if self.action == 'accept' then
      self:warning("'accept' action deprecated in favor of 'exclude'")
      self.action = 'exclude'
   end

   if self.action == 'exclude' then return 'ACCEPT' end
   if self.action and self.action ~= 'include' then
      self:error('Invalid action: '..self.action)
   end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
399 400 401
end


402
function M.Rule:trules()
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
403

404
   local function tag(ofrags, tag, value)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
405 406 407 408 409 410 411 412
      for i, ofrag in ipairs(ofrags) do
	 assert(not ofrag[tag])
	 ofrag[tag] = value
      end
   end

   local families

413
   local function setfamilies(ofrags)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
414 415 416 417 418 419 420 421 422 423 424 425
      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

426
   local function ffilter(ofrags)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
427
      if not ofrags or not ofrags[1] or not families then return ofrags end
428 429 430 431 432 433
      return filter(
	 ofrags,
	 function(of)
	    return not of.family or contains(families, of.family)
	 end
      )
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
434 435 436 437
   end

   local res = self:zoneoptfrags()

438
   if self.ipset then
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
439
      local ipsetofrags = {}
440
      for i, ipset in listpairs(self.ipset) do
441
	 if not ipset.name then self:error('Set name not defined') end
442

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

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
446
	 if not ipset.args then
447
	    self:error('Set direction arguments not defined')
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
448
	 end
449

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
450
	 local setopts = '-m set --match-set '..ipset.name..' '
451 452
	 setopts = setopts..table.concat(util.map(util.list(ipset.args),
						  function(a)
453 454 455 456
						     if self:direction(a) == 'in' then
							return 'src'
						     end
						     return 'dst'
457 458
						  end),
					 ',')
459
	 table.insert(ipsetofrags, {family=setdef.family, opts=setopts})
460
      end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
461
      res = combinations(res, ipsetofrags)
462 463
   end

464
   if self.ipsec then
465 466
      res = combinations(res,
			 {{opts='-m policy --pol ipsec --dir '..self:direction(self.ipsec)}})
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
467 468 469 470 471 472
   end

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

   setfamilies(res)

473 474 475 476
   local addrofrags = combinations(
      self:create(M.Zone, {addr=self.src}):optfrags(self:direction('in')),
      self:destoptfrags()
   )
477
   local combined = res
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
478 479 480 481 482 483

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

484 485
      combined = {}
      for i, ofrag in ipairs(res) do
486 487 488
	 local aofs = combinations(addrofrags, {{family=ofrag.family}})
	 local cc = combinations({ofrag}, aofs)
	 if #cc < #aofs then
489 490 491
	    combined = nil
	    break
	 end
492
	 extend(combined, cc)
493
      end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
494 495 496
   end

   local target
497
   if combined then
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
498
      target = self:target()
499
      res = combined
500
   else target = self:uniqueid('address') end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
501 502 503

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

504
   res = combinations(res, {{target=target}})
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
505

506
   if not combined then
507
      extend(
508 509 510
	 res,
	 combinations(addrofrags, {{chain=target, target=self:target()}})
      )
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
511 512
   end

513
   extend(res, self:extraoptfrags())
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
514

515 516 517 518 519 520 521
   local tbl = self:table()

   local function convertchains(ofrags)
      local res = {}

      for i, ofrag in ipairs(ofrags) do

522
	 if contains(builtin[tbl], ofrag.chain) then table.insert(res, ofrag)
523
	 else
524 525 526
	    local ofs, recursive
	    if ofrag.chain == 'PREROUTING' then
	       ofs = {{chain='FORWARD'}, {chain='INPUT'}}
527
	    elseif ofrag.chain == 'POSTROUTING' then
528 529 530 531 532 533 534 535
	       ofs = {{chain='FORWARD'}, {chain='OUTPUT'}}
	       recursive = true
	    elseif ofrag.chain == 'INPUT' then
	       ofs = {{opts='-m addrtype --dst-type LOCAL', chain='PREROUTING'}}
	    elseif ofrag.chain == 'FORWARD' then
	       ofs = {
		  {opts='-m addrtype ! --dst-type LOCAL', chain='PREROUTING'}
	       }
536 537
	    end

538
	    if ofs then
539
	       ofrag.chain = nil
540 541
	       ofs = combinations(ofs, {ofrag})
	       if recursive then ofs = convertchains(ofs) end
542
	       extend(res, ofs)
543

544 545 546 547 548 549 550
	    else table.insert(res, ofrag) end
	 end
      end

      return res
   end

551
   res = convertchains(ffilter(res))
552
   tag(res, 'table', tbl, false)
553 554

   local function checkzof(ofrag, dir, chains)
555
      if ofrag[dir] and contains(chains, ofrag.chain) then
556 557 558 559 560 561 562 563
	 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
564
   
565
   return combinations(res, ffilter({{family='inet'}, {family='inet6'}}))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
566 567
end

568
function M.Rule:extraoptfrags() return {} end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
569

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
function M.Rule:extrarules(label, cls, options)
   local params = {}

   for _, attr in ipairs(
      extend(
	 {'in', 'out', 'src', 'dest', 'ipset', 'ipsec', 'service'},
	 options.attrs
      )
   ) do
      params[attr] = (options.src or self)[attr]
   end

   util.update(params, options.update)
   if options.discard then params[options.discard] = nil end

   return self:create(cls, params, label, options.index):trules()
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
588

589 590 591 592 593 594 595 596 597 598 599 600
M.Limit = M.class(M.ConfigObject)

function M.Limit:init(...)
   M.Limit.super(self):init(...)

   if not self.count then
      if not self[1] then
	 self:error('Packet count not defined for limit')
      end
      self.count = self[1]
   end

601
   setdefault(self, 'interval', 1)
602 603 604 605

   if type(setdefault(self, 'mask', {})) == 'number' then
      self.mask = {src=self.mask}
   end
606
   for _, family in ipairs{'inet', 'inet6'} do
607
      setdefault(self.mask, family, util.copy(self.mask))
608 609 610 611 612 613
      for _, attr in ipairs{'src', 'dest'} do
	 local mask = setdefault(
	    self.mask[family],
	    attr,
	    ({src=({inet=32, inet6=128})[family], dest=0})[attr]
	 )
614 615 616 617 618 619
	 if mask > 0 then
	    self.mask[family].mode =
	       self.mask[family].mode and true or {attr, mask}
	 end
      end
   end
620 621
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
622 623 624
function M.Limit:rate() return self.count / self.interval end

function M.Limit:intrate() return math.ceil(self:rate()) end
625

626
function M.Limit:limitofrags(name)
627
   local rate = self:rate()
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
628 629 630 631 632 633 634 635 636 637
   local unit
   for _, quantum in ipairs{
      {1, 'second'}, {60, 'minute'}, {60, 'hour'}, {24, 'day'}
   } do
      rate = rate * quantum[1]
      unit = quantum[2]
      if rate >= 1 then break end
   end
   rate = math.ceil(rate)..'/'..unit

638 639 640 641 642
   local ofrags = {}

   for _, family in ipairs{'inet', 'inet6'} do
      local keys = {}
      local maskopts = ''
643
      for _, attr in ipairs{'src', 'dest'} do
644 645
	 local mask = self.mask[family][attr]
	 if mask > 0 then
646
	    local opt = ({src='src', dest='dst'})[attr]
647 648 649 650 651 652 653 654 655 656
	    table.insert(keys, opt..'ip')
	    maskopts = maskopts..' --hashlimit-'..opt..'mask '..mask
	 end
      end

      table.insert(
	 ofrags,
	 {
	    family=family,
	    opts=keys[1] and
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
657 658 659 660
	       '-m hashlimit --hashlimit-upto '..rate..' --hashlimit-burst '..
	       self:intrate()..' --hashlimit-mode '..table.concat(keys, ',')..
	       maskopts..' --hashlimit-name '..(name or self:uniqueid()) or
	       '-m limit --limit '..rate
661 662 663 664 665
	 }
      )
   end

   return ofrags
666 667 668
end


669 670 671
M.export = {zone={class=M.Zone}, ipset={class=IPSet, before='%modules'}}

return M