-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipset.go
556 lines (529 loc) · 16 KB
/
ipset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package ipset
import (
"fmt"
"net"
"strconv"
"sync"
"github.com/chenchun/ipset/log"
"github.com/vishvananda/netlink/nl"
"golang.org/x/sys/unix"
)
var (
revisionLock sync.RWMutex
setRevisionMap = map[SetType][]uint8{} // check ipset/lib/ipset_hash_ip.c ...
)
// Handle provides a specific ipset handle to program ipset rules.
type Handle struct {
l log.LOG
protolcol uint8
}
func New(l log.LOG) (*Handle, error) {
h := &Handle{l: l}
if proto, err := h.protocol(); err != nil {
return nil, fmt.Errorf("failed to get kernel supported ipset protocol version: %v", err)
} else {
h.protolcol = proto
}
return h, nil
}
func (h *Handle) Create(set *IPSet, opts ...Opt) error {
if set.Name == "" {
return fmt.Errorf("Invalid create command: missing setname")
}
if string(set.SetType) == "" {
return fmt.Errorf("Invalid create command: missing settype")
}
if set.Family == "" {
// family must be set as empty for HashMac
if set.SetType != HashMac {
set.Family = "inet"
}
}
req, err := h.newRequest(IPSET_CMD_CREATE)
if err != nil {
return err
}
req.AddData(nl.NewRtAttr(IPSET_ATTR_SETNAME, nl.ZeroTerminated(set.Name)))
req.AddData(nl.NewRtAttr(IPSET_ATTR_TYPENAME, nl.ZeroTerminated(string(set.SetType))))
if err := h.fillRevision(req, set.SetType, set.SetRevison); err != nil {
return err
}
fillFamily(req, set.Family)
h.l.Debugf("create %v", req.Serialize())
_, err = req.Execute(unix.NETLINK_NETFILTER, 0)
return err
}
func (h *Handle) Destroy(setName string, opts ...Opt) error {
if setName == "" {
return fmt.Errorf("invalid destroy command: missing setname")
}
req, err := h.newRequest(IPSET_CMD_DESTROY)
if err != nil {
return err
}
req.AddData(nl.NewRtAttr(IPSET_ATTR_SETNAME, nl.ZeroTerminated(setName)))
_, err = req.Execute(unix.NETLINK_NETFILTER, 0)
return err
}
func (h *Handle) fillRevision(req *nl.NetlinkRequest, setType SetType, revision *uint8) error {
var revisions []uint8
revisionLock.RLock()
cached, ok := setRevisionMap[setType]
revisionLock.RUnlock()
if ok {
revisions = cached
} else {
max, min, err := h.getRevision(setType)
if err != nil {
return err
}
revisions = []uint8{min, max}
revisionLock.Lock()
setRevisionMap[setType] = revisions
revisionLock.Unlock()
}
if revision != nil {
if *revision < revisions[0] {
return fmt.Errorf("revision %d is smaller than min supported %d", *revision, revisions[0])
}
if *revision > revisions[1] {
return fmt.Errorf("revision %d is larger than max supported %d", *revision, revisions[1])
}
req.AddData(nl.NewRtAttr(IPSET_ATTR_REVISION, nl.Uint8Attr(uint8(*revision))))
} else {
req.AddData(nl.NewRtAttr(IPSET_ATTR_REVISION, nl.Uint8Attr(uint8(revisions[1]))))
}
return nil
}
func fillFamily(req *nl.NetlinkRequest, hashFamily string) {
switch hashFamily {
case "inet6":
req.AddData(nl.NewRtAttr(IPSET_ATTR_FAMILY, nl.Uint8Attr(uint8(NFPROTO_IPV6))))
case "inet":
req.AddData(nl.NewRtAttr(IPSET_ATTR_FAMILY, nl.Uint8Attr(uint8(NFPROTO_IPV4))))
default:
req.AddData(nl.NewRtAttr(IPSET_ATTR_FAMILY, nl.Uint8Attr(uint8(NFPROTO_UNSPEC))))
}
}
//buffers: 28 0 0 0 1 6 1 0 124 248 115 92 0 0 0 0 2 0 0 0 5 0 1 0 7 0 0 0
//Message header: sent cmd PROTOCOL (1)
//len 28
//flag EXIST
//seq 1551104124
//Command attributes:
//PROTOCOL: 7
//buffers: 28 0 0 0 1 6 0 0 124 248 115 92 57 10 0 0 2 0 0 0 5 0 1 0 6 0 0 0
//Message header: received cmd PROTOCOL (1)
//len 28
//flag EXIST
//seq 1551104124
//Command attributes:
//PROTOCOL: 6
func (h *Handle) protocol() (uint8, error) {
req, err := h.newRequest(IPSET_CMD_PROTOCOL)
if err != nil {
return 0, err
}
msgs, err := req.Execute(unix.NETLINK_NETFILTER, 0)
if err != nil {
return 0, err
}
var min, max uint8
for i := range msgs {
if len(msgs[i]) < SizeofNFGenMsg {
return 0, fmt.Errorf("possible corrupt msg %v", msgs[i])
}
//nlGenlMsg := DeserializeNFGenlMsg(msgs[i])
attrs, err := nl.ParseRouteAttr(msgs[i][SizeofNFGenMsg:])
if err != nil {
return 0, fmt.Errorf("possible corrupt msg %v", msgs[i])
}
for i := range attrs {
switch attrs[i].Attr.Type {
case IPSET_ATTR_PROTOCOL:
if attrs[i].Attr.Len != unix.SizeofRtAttr+1 {
return 0, fmt.Errorf("possible corrupt msg %v", msgs[i])
}
max = uint8(attrs[i].Value[0])
if min == 0 {
min = max
}
case IPSET_ATTR_PROTOCOL_MIN:
min = uint8(attrs[i].Value[0])
}
}
break
}
h.l.Debugf("supported protocol %d, min supported %d", max, min)
return max, nil
}
func (h *Handle) List(setName string, opts ...Opt) ([]ListItem, error) {
//req: msg: IPSET_CMD_LIST|SAVE
//attr: IPSET_ATTR_PROTOCOL
// IPSET_ATTR_SETNAME (optional)
//
//resp: attr: IPSET_ATTR_SETNAME
// IPSET_ATTR_TYPENAME
// IPSET_ATTR_REVISION
// IPSET_ATTR_FAMILY
// IPSET_ATTR_DATA
// create-specific-data
// IPSET_ATTR_ADT
// IPSET_ATTR_DATA
// adt-specific-data
// IPSET_ATTR_ADT
// IPSET_ATTR_DATA
// adt-specific-data
// ...
req, err := h.newRequest(IPSET_CMD_LIST)
if err != nil {
return nil, err
}
if setName != "" {
req.AddData(nl.NewRtAttr(IPSET_ATTR_SETNAME, nl.ZeroTerminated(setName)))
}
req.AddData(nl.NewRtAttr(IPSET_ATTR_FLAGS, nl.Uint32Attr(IPSET_FLAG_LIST_SETNAME|IPSET_FLAG_LIST_HEADER)))
msgs, err := req.Execute(unix.NETLINK_NETFILTER, 0)
if err != nil {
return nil, err
}
var sets []ListItem
for k := range msgs {
h.l.Debugf("receive msgs[%d]=%v", k, msgs[k])
var ipset ListItem
if len(msgs[k]) < SizeofNFGenMsg {
return nil, fmt.Errorf("possible corrupt msg %v", msgs[k])
}
//nlGenlMsg := DeserializeNFGenlMsg(msgs[k])
attrs, err := nl.ParseRouteAttr(msgs[k][SizeofNFGenMsg:])
if err != nil {
return nil, fmt.Errorf("possible corrupt msg %v", msgs[k])
}
for i := range attrs {
switch attrs[i].Attr.Type {
case IPSET_ATTR_PROTOCOL:
if attrs[i].Attr.Len != unix.SizeofRtAttr+1 {
return nil, fmt.Errorf("possible corrupt msg %v", msgs[k])
}
//protocol := uint8(attrs[i].Value[0])
case IPSET_ATTR_SETNAME:
ipset.Name = string(attrs[i].Value[:len(attrs[i].Value)-1])
case IPSET_ATTR_TYPENAME:
ipset.SetType = SetType(attrs[i].Value[:len(attrs[i].Value)-1])
case IPSET_ATTR_REVISION:
if attrs[i].Attr.Len != unix.SizeofRtAttr+1 {
return nil, fmt.Errorf("possible corrupt msg %v", msgs[k])
}
ipset.SetRevison = &attrs[i].Value[0]
case IPSET_ATTR_FAMILY:
if attrs[i].Attr.Len != unix.SizeofRtAttr+1 {
return nil, fmt.Errorf("possible corrupt msg %v", msgs[k])
}
switch attrs[i].Value[0] {
case NFPROTO_IPV4:
ipset.Family = "inet"
case NFPROTO_IPV6:
ipset.Family = "inet6"
}
case IPSET_ATTR_DATA | unix.NLA_F_NESTED:
nestAttrs, err := nl.ParseRouteAttr(attrs[i].Value)
if err != nil {
return nil, fmt.Errorf("possible corrupt msg %v", msgs[k])
}
for j := range nestAttrs {
switch nestAttrs[j].Attr.Type {
default:
// TODO Parse create attr
//HASHSIZE: 1024
//MAXELEM: 65536
//REFERENCES: 0
//MEMSIZE: 176
//log.Infof("unknown attr %v", nestAttrs[j].Attr.Type)
}
}
case IPSET_ATTR_ADT | unix.NLA_F_NESTED:
entries, err := parseAdtAttr(attrs[i].Value)
if err != nil {
return nil, err
}
ipset.Entries = append(ipset.Entries, entries...)
}
}
sets = append(sets, ipset)
}
return sets, nil
}
func parseAdtAttr(data []byte) ([]Entry, error) {
nestAttrs, err := nl.ParseRouteAttr(data)
if err != nil {
return nil, err
}
var entries []Entry
for j := range nestAttrs {
switch nestAttrs[j].Attr.Type {
case IPSET_ATTR_DATA | unix.NLA_F_NESTED:
var entry Entry
nestGrandAttrs, err := nl.ParseRouteAttr(nestAttrs[j].Value)
if err != nil {
return nil, err
}
for k := range nestGrandAttrs {
switch nestGrandAttrs[k].Attr.Type {
case IPSET_ATTR_IP | unix.NLA_F_NESTED:
fallthrough
case IPSET_ATTR_IP2 | unix.NLA_F_NESTED:
ip, err := parseIP(nestGrandAttrs[k].Value)
if err != nil {
return nil, err
}
if nestGrandAttrs[k].Attr.Type == IPSET_ATTR_IP2|unix.NLA_F_NESTED {
entry.IP2 = ip.String()
} else {
entry.IP = ip.String()
}
case IPSET_ATTR_CIDR:
fallthrough
case IPSET_ATTR_CIDR2:
if nestGrandAttrs[k].Attr.Len != unix.SizeofRtAttr+1 {
return nil, fmt.Errorf("possible corrupt cidr msg %v", nestGrandAttrs)
}
cidr := uint8(nestGrandAttrs[k].Value[0])
if nestGrandAttrs[k].Attr.Type == IPSET_ATTR_CIDR2 {
entry.CIDR2 = &cidr
} else {
entry.CIDR = &cidr
}
case IPSET_ATTR_ETHER:
if nestGrandAttrs[k].Attr.Len != unix.SizeofRtAttr+6 {
return nil, fmt.Errorf("possible corrupt mac msg %v", nestGrandAttrs)
}
entry.Mac = net.HardwareAddr(nestGrandAttrs[k].Value)
case IPSET_ATTR_PORT | unix.NLA_F_NET_BYTEORDER:
if nestGrandAttrs[k].Attr.Len != unix.SizeofRtAttr+2 {
return nil, fmt.Errorf("possible corrupt port msg %v", nestGrandAttrs)
}
port := ntohs(nestGrandAttrs[k].Value)
entry.Port = uint16(port)
case IPSET_ATTR_PROTO:
if nestGrandAttrs[k].Attr.Len != unix.SizeofRtAttr+1 {
return nil, fmt.Errorf("possible corrupt port msg %v", nestGrandAttrs)
}
entry.Proto = uint8(nestGrandAttrs[k].Value[0])
default:
return nil, fmt.Errorf("unknown attr %v", nestGrandAttrs[k].Attr.Type)
}
}
entries = append(entries, entry)
default:
return nil, fmt.Errorf("unknown attr %v, expect only IPSET_ATTR_DATA attr", nestAttrs[j].Attr.Type)
}
}
return entries, nil
}
func parseIP(ipData []byte) (net.IP, error) {
nestAttrs, err := nl.ParseRouteAttr(ipData)
if err != nil {
return nil, fmt.Errorf("possible corrupt ip msg %v", ipData)
}
for i := range nestAttrs {
switch nestAttrs[i].Attr.Type {
case IPSET_ATTR_IPADDR_IPV4:
if nestAttrs[i].Attr.Len != unix.SizeofRtAttr+4 {
return nil, fmt.Errorf("possible corrupt ip msg %v", ipData)
}
return net.IP(nestAttrs[i].Value), nil
//TODO ipv6
}
}
return nil, fmt.Errorf("possible corrupt ip msg %v, nestAttrs %v", ipData, nestAttrs)
}
func (h *Handle) Add(set *IPSet, entry *Entry, opts ...Opt) error {
return h.addOrDel(IPSET_CMD_ADD, set, entry, opts...)
}
func (h *Handle) Del(set *IPSet, entry *Entry, opts ...Opt) error {
return h.addOrDel(IPSET_CMD_DEL, set, entry, opts...)
}
func (h *Handle) addOrDel(command int, set *IPSet, entry *Entry, opts ...Opt) error {
if set.Name == "" {
return fmt.Errorf("invalid add command: missing setname")
}
req, err := h.newRequest(command)
if err != nil {
return err
}
req.AddData(nl.NewRtAttr(IPSET_ATTR_SETNAME, nl.ZeroTerminated(set.Name)))
dataAttr := nl.NewRtAttr(IPSET_ATTR_DATA|unix.NLA_F_NESTED, nil)
if err := fillEntries(dataAttr, set, entry); err != nil {
return err
}
req.AddData(dataAttr)
h.l.Debugf("addOrDel %v", req.Serialize())
_, err = req.Execute(unix.NETLINK_NETFILTER, 0)
return err
}
type fillAddAttr func(parent *nl.RtAttr, entry *Entry) error
var setTypeFillFuncMap = map[SetType][]fillAddAttr{
HashIP: {fillIP},
HashMac: {fillMac},
HashIPMac: {fillIP, fillMac},
HashNet: {fillIP},
HashNetNet: {fillIP, fillIP2},
HashIPPort: {fillIP, fillPort},
HashNetPort: {fillIP, fillPort},
HashIPPortIP: {fillIP, fillPort, fillIP2},
HashIPPortNet: {fillIP, fillPort, fillIP2},
HashNetPortNet: {fillIP, fillPort, fillIP2},
}
func fillEntries(parent *nl.RtAttr, set *IPSet, entry *Entry) error {
if funcs, exist := setTypeFillFuncMap[set.SetType]; !exist {
return fmt.Errorf("adding entries for setType %s not supported now", set.SetType)
} else {
for i := range funcs {
if err := funcs[i](parent, entry); err != nil {
return err
}
}
}
fillLineno(parent)
return nil
}
func fillLineno(parent *nl.RtAttr) {
parent.AddRtAttr(IPSET_ATTR_LINENO|unix.NLA_F_NET_BYTEORDER, nl.Uint32Attr(0))
}
func fillIP(parent *nl.RtAttr, entry *Entry) error {
ip := net.ParseIP(entry.IP)
if ip == nil {
return fmt.Errorf("invalid add command: bad ip: %s", entry.IP)
}
ipAttr := nl.NewRtAttr(IPSET_ATTR_IP|unix.NLA_F_NESTED, nil)
if ip4 := ip.To4(); ip4 != nil {
ipAttr.AddRtAttr(IPSET_ATTR_IPADDR_IPV4|unix.NLA_F_NET_BYTEORDER, []byte(ip4))
} else if ip6 := ip.To16(); ip6 != nil {
// TODO ip6
}
parent.AddChild(ipAttr)
if entry.CIDR != nil {
parent.AddRtAttr(IPSET_ATTR_CIDR, nl.Uint8Attr(*entry.CIDR))
}
return nil
}
func fillPort(parent *nl.RtAttr, entry *Entry) error {
parent.AddRtAttr(IPSET_ATTR_PORT|unix.NLA_F_NET_BYTEORDER, htons(entry.Port))
if entry.PortTo != 0 {
parent.AddRtAttr(IPSET_ATTR_PORT_TO|unix.NLA_F_NET_BYTEORDER, htons(entry.PortTo))
}
if entry.Proto == 0 {
parent.AddRtAttr(IPSET_ATTR_PROTO, nl.Uint8Attr(unix.IPPROTO_TCP))
} else {
parent.AddRtAttr(IPSET_ATTR_PROTO, nl.Uint8Attr(entry.Proto))
}
return nil
}
func fillIP2(parent *nl.RtAttr, entry *Entry) error {
ip := net.ParseIP(entry.IP2)
if ip == nil {
return fmt.Errorf("invalid add command: bad ip: %s", entry.IP2)
}
ipAttr := nl.NewRtAttr(IPSET_ATTR_IP2|unix.NLA_F_NESTED, nil)
if ip4 := ip.To4(); ip4 != nil {
ipAttr.AddRtAttr(IPSET_ATTR_IPADDR_IPV4|unix.NLA_F_NET_BYTEORDER, []byte(ip4))
} else if ip6 := ip.To16(); ip6 != nil {
// TODO ip6
}
parent.AddChild(ipAttr)
if entry.CIDR2 != nil {
parent.AddRtAttr(IPSET_ATTR_CIDR2, nl.Uint8Attr(*entry.CIDR2))
}
return nil
}
func fillMac(parent *nl.RtAttr, entry *Entry) error {
if len(entry.Mac) == 0 {
return fmt.Errorf("invalid add command: bad mac: %v", entry.Mac)
}
parent.AddRtAttr(IPSET_ATTR_ETHER, []byte(entry.Mac))
return nil
}
func (h *Handle) newRequest(cmd int) (*nl.NetlinkRequest, error) {
if cmd <= IPSET_CMD_NONE || cmd >= IPSET_MSG_MAX {
return nil, fmt.Errorf("cmd should between IPSET_CMD_NONE and IPSET_MSG_MAX")
}
req := nl.NewNetlinkRequest(cmd|(NFNL_SUBSYS_IPSET<<8), IPSetCmdflags[cmd-1])
req.AddData(&nfgenmsg{family: unix.AF_INET, version: NFNETLINK_V0, resid: 0})
req.AddData(nl.NewRtAttr(IPSET_ATTR_PROTOCOL, nl.Uint8Attr(h.protolcol)))
return req, nil
}
// TryConvertErrno tries to convert input err to a IPSETErrno
// Return the IPSetErrno pointer if it succeeds, otherwise nil
func TryConvertErrno(err error) *int32 {
if len(err.Error()) < len("errno ") {
return nil
}
no, err := strconv.Atoi(err.Error()[len("errno "):])
if err != nil {
return nil
}
ipsetNo := int32(no)
if ipsetNo >= IPSET_ERR_PRIVATE && ipsetNo <= IPSET_ERR_SKBINFO {
return &ipsetNo
}
return nil
}
//buffers: 48 0 0 0 13 6 1 0 125 248 115 92 0 0 0 0 2 0 0 0 5 0 1 0 6 0 0 0 12 0 3 0 104 97 115 104 58 105 112 0 5 0 5 0 2 0 0 0
//Message header: sent cmd TYPE (13)
//len 48
//flag EXIST
//seq 1551104125
//Command attributes:
//PROTOCOL: 6
//TYPENAME: hash:ip
//FAMILY: 2
//buffers: 64 0 0 0 13 6 0 0 125 248 115 92 57 10 0 0 2 0 0 0 5 0 1 0 6 0 0 0 12 0 3 0 104 97 115 104 58 105 112 0 5 0 5 0 2 0 0 0 5 0 4 0 4 0 0 0 5 0 10 0 0 0 0 0
//Message header: received cmd TYPE (13)
//len 64
//flag EXIST
//seq 1551104125
//Command attributes:
//PROTOCOL: 6
//TYPENAME: hash:ip
//REVISION: 4
//FAMILY: 2
//PROTO_MIN: 0
func (h *Handle) getRevision(setType SetType) (uint8, uint8, error) {
req, err := h.newRequest(IPSET_CMD_TYPE)
if err != nil {
return 0, 0, err
}
h.l.Debugf("type %v", req.Serialize())
req.AddData(nl.NewRtAttr(IPSET_ATTR_TYPENAME, nl.ZeroTerminated(string(setType))))
fillFamily(req, "inet")
msgs, err := req.Execute(unix.NETLINK_NETFILTER, 0)
if err != nil {
return 0, 0, err
}
var min, max uint8
for k := range msgs {
h.l.Debugf("receive msgs[%d]=%v", k, msgs[k])
if len(msgs[k]) < SizeofNFGenMsg {
return 0, 0, fmt.Errorf("possible corrupt msg %v", msgs[k])
}
//nlGenlMsg := DeserializeNFGenlMsg(msgs[i])
attrs, err := nl.ParseRouteAttr(msgs[k][SizeofNFGenMsg:])
if err != nil {
return 0, 0, fmt.Errorf("possible corrupt msg %v", msgs[k])
}
for i := range attrs {
switch attrs[i].Attr.Type {
case IPSET_ATTR_REVISION:
if attrs[i].Attr.Len != unix.SizeofRtAttr+1 {
return 0, 0, fmt.Errorf("possible corrupt msg %v", msgs[i])
}
max = uint8(attrs[i].Value[0])
case IPSET_ATTR_REVISION_MIN:
min = uint8(attrs[i].Value[0])
}
}
break
}
h.l.Debugf("supported revision of %v is %d, min supported %d", setType, max, min)
return max, min, nil
}