forked from tailscale/wf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firewall.go
851 lines (726 loc) · 22.7 KB
/
firewall.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
// Copyright (c) 2021 The Inet.Af AUTHORS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package wf
import (
"errors"
"fmt"
"net/netip"
"reflect"
"time"
"unsafe"
"golang.org/x/sys/windows"
)
type fieldTypes map[FieldID]reflect.Type
type layerTypes map[LayerID]fieldTypes
// Session is a connection to the WFP API.
type Session struct {
handle windows.Handle
// layerTypes is a map of layer ID -> field ID -> Go type for that field.
layerTypes layerTypes
// indicates if we are currently in a transaction
status TransactionStatus
}
// Options configures a Session.
type Options struct {
// Name is a short name for the session, shown in Windows
// administrative tools.
Name string
// Description is a short description for the session, shown in
// Windows administrative tools.
Description string
// Dynamic, if true, indicates that all objects created during the
// session should be removed when the session is closed or the
// current process terminates. Dynamic sessions are meant for
// adding firewall configuration that should not outlast your
// program's execution.
Dynamic bool
// TransactionStartTimeout is how long the session is willing to
// wait to acquire the global transaction lock. If zero, WFP's
// default timeout (15 seconds) is used.
TransactionStartTimeout time.Duration
// StartTransaction indicates if we want to immediately start
// a transaction when the session begins
StartTransaction bool
// TransactionFlags indicate if we want read-only or read/write
// access
TransactionFlags TransactionFlag
}
// Util enum to track different states
type TransactionState uint32
const (
NoTransaction TransactionState = iota
BeganTransaction
AbortedTransaction
CommittedTransaction
)
var transactionStateMap map[TransactionState]string = map[TransactionState]string{
NoTransaction: "No transaction requested",
BeganTransaction: "Transaction was started",
AbortedTransaction: "Transaction was aborted",
CommittedTransaction: "Transaction was committed",
}
func (ts TransactionState) String() string {
return transactionStateMap[ts]
}
// TransactionStatus tracks the last state our transaction was in. If an error
// occurs when calling a transaction function, this value is preserved along
// with the error of transaction operatoin we were trying to perform.
type TransactionStatus struct {
State TransactionState
Err error
}
// New connects to the WFP API.
func New(opts *Options) (*Session, error) {
if opts == nil {
opts = &Options{}
}
var a arena
defer a.Dispose()
s0 := toSession0(&a, opts)
var handle windows.Handle
err := fwpmEngineOpen0(nil, authnServiceWinNT, nil, s0, &handle)
if err != nil {
return nil, err
}
ret := &Session{
handle: handle,
layerTypes: layerTypes{},
}
if opts.StartTransaction {
// Don't worry if we return an error here. We're not going to fail
// the creation of the session if we can't start a transaction.
ret.BeginTransaction(opts.TransactionFlags)
}
// Populate the layer type cache.
layers, err := ret.Layers()
if err != nil {
ret.Close()
return nil, err
}
for _, layer := range layers {
fields := fieldTypes{}
for _, field := range layer.Fields {
fields[field.ID] = field.Type
}
ret.layerTypes[layer.ID] = fields
}
return ret, nil
}
// Close implements io.Closer.
func (s *Session) Close() error {
if s.handle == 0 {
return nil
}
// if we have a transaction in progress that was not commited, abort it now
if s.status.State == BeganTransaction {
s.AbortTransaction()
}
err := fwpmEngineClose0(s.handle)
s.handle = 0
return err
}
// LayerID identifies a WFP layer.
type LayerID windows.GUID
func (id LayerID) String() string {
if s := guidNames[windows.GUID(id)]; s != "" {
return s
}
return windows.GUID(id).String()
}
// IsZero reports whether id is nil or the zero GUID.
func (id *LayerID) IsZero() bool {
return id == nil || *id == LayerID{}
}
// ConditionFlag represents special conditions that can be tested.
type ConditionFlag uint32 // do not change type, used in C calls
const (
ConditionFlagIsLoopback ConditionFlag = 0x00000001
ConditionFlagIsIPSecSecured ConditionFlag = 0x00000002
ConditionFlagIsReauthorize ConditionFlag = 0x00000004
ConditionFlagIsWildcardBind ConditionFlag = 0x00000008
ConditionFlagIsRawEndpoint ConditionFlag = 0x00000010
ConditionFlagIsFragmant ConditionFlag = 0x00000020
ConditionFlagIsFragmantGroup ConditionFlag = 0x00000040
ConditionFlagIsIPSecNATTReclassify ConditionFlag = 0x00000080
ConditionFlagIsRequiresALEClassify ConditionFlag = 0x00000100
ConditionFlagIsImplicitBind ConditionFlag = 0x00000200
ConditionFlagIsReassembled ConditionFlag = 0x00000400
ConditionFlagIsNameAppSpecified ConditionFlag = 0x00004000
ConditionFlagIsPromiscuous ConditionFlag = 0x00008000
ConditionFlagIsAuthFW ConditionFlag = 0x00010000
ConditionFlagIsReclassify ConditionFlag = 0x00020000
ConditionFlagIsOutboundPassThru ConditionFlag = 0x00040000
ConditionFlagIsInboundPassThru ConditionFlag = 0x00080000
ConditionFlagIsConnectionRedirected ConditionFlag = 0x00100000
)
// IPProto represents the protocol being used.
type IPProto uint8 // do not change type, used in C calls
// From: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-socket
const (
IPProtoICMP IPProto = 1
IPProtoICMPV6 IPProto = 58
IPProtoTCP IPProto = 6
IPProtoUDP IPProto = 17
)
// AppID returns the application ID associated with the provided file.
func AppID(file string) (string, error) {
var a arena
defer a.Dispose()
fileBytes, _ := toBytesFromString(&a, file)
var appID *fwpByteBlob
if err := fwpmGetAppIdFromFileName0(fileBytes, &appID); err != nil {
return "", err
}
defer fwpmFreeMemory0((*struct{})(unsafe.Pointer(&appID)))
return fromByteBlobToString(appID)
}
// Layer is a point in the packet processing path where filter rules
// can be applied.
type Layer struct {
// ID is the unique identifier for this layer.
ID LayerID
// KernelID is the internal kernel ID for this layer.
KernelID uint16
// Name is a short descriptive name.
Name string
// Description is a longer description of the layer's function.
Description string
// DefaultSublayer is the ID for the default sublayer into which
// filter rules are added.
DefaultSublayer SublayerID
// Fields describes the fields that are available in this layer to
// be matched against.
Fields []*Field
}
// FieldID identifies a WFP layer field.
type FieldID windows.GUID
func (id FieldID) String() string {
if s := guidNames[windows.GUID(id)]; s != "" {
return s
}
return windows.GUID(id).String()
}
// IsZero reports whether id is nil or the zero GUID.
func (id *FieldID) IsZero() bool {
return id == nil || *id == FieldID{}
}
// Field is a piece of information that a layer makes available to
// filter rules for matching.
type Field struct {
// ID is the unique identifier for the field.
ID FieldID
// Type is the type of the field.
Type reflect.Type
}
// TokenAccessInformation represents all the information in a token
// that is necessary to perform an access check.
// This type is only present in Layer fields, and cannot be used
// directly as a value in firewall rules.
type TokenAccessInformation struct{}
type Range struct {
From, To interface{}
}
// TokenInformation defines a set of security identifiers.
// This type is only present in Layer fields, and cannot be used
// directly as a value in firewall rules.
type TokenInformation struct{}
// Layers returns information on available WFP layers.
func (s *Session) Layers() ([]*Layer, error) {
var enum windows.Handle
if err := fwpmLayerCreateEnumHandle0(s.handle, nil, &enum); err != nil {
return nil, err
}
defer fwpmLayerDestroyEnumHandle0(s.handle, enum)
var ret []*Layer
for {
layers, err := s.getLayerPage(enum)
if err != nil {
return nil, err
}
if len(layers) == 0 {
return ret, nil
}
ret = append(ret, layers...)
}
}
func (s *Session) getLayerPage(enum windows.Handle) ([]*Layer, error) {
const pageSize = 100
var (
array **fwpmLayer0
num uint32
)
if err := fwpmLayerEnum0(s.handle, enum, pageSize, &array, &num); err != nil {
return nil, err
}
if num == 0 {
return nil, nil
}
defer fwpmFreeMemory0((*struct{})(unsafe.Pointer(&array)))
return fromLayer0(array, num)
}
// SublayerID identifies a WFP sublayer.
type SublayerID windows.GUID
func (id SublayerID) String() string {
if s := guidNames[windows.GUID(id)]; s != "" {
return s
}
return windows.GUID(id).String()
}
// IsZero reports whether id is nil or the zero GUID.
func (id *SublayerID) IsZero() bool {
return id == nil || *id == SublayerID{}
}
// A Sublayer is a container for filtering rules.
type Sublayer struct {
// ID is the unique identifier for this sublayer.
ID SublayerID
// Name is a short descriptive name.
Name string
// Description is a longer description of the Sublayer.
Description string
// Persistent indicates whether the sublayer is preserved across
// restarts of the filtering engine.
Persistent bool
// Provider optionally identifies the Provider that manages this
// sublayer.
Provider ProviderID
// ProviderData is optional opaque data that can be held on behalf
// of the Provider.
ProviderData []byte
// Weight specifies the priority of this sublayer relative to
// other sublayers. Higher-weighted sublayers are invoked first.
Weight uint16
}
// Sublayers returns available Sublayers. If providers are given,
// returns only sublayers registered to those providers.
func (s *Session) Sublayers(providers ...ProviderID) ([]*Sublayer, error) {
if len(providers) == 0 {
// Do one lookup with a zero provider, which returns all
// sublayers.
providers = []ProviderID{ProviderID{}}
}
var ret []*Sublayer
for _, provider := range providers {
sls, err := s.getOneProvider(provider)
if err != nil {
return nil, err
}
ret = append(ret, sls...)
}
return ret, nil
}
func (s *Session) getOneProvider(provider ProviderID) ([]*Sublayer, error) {
var a arena
defer a.Dispose()
tpl := toSublayerEnumTemplate0(&a, provider)
var enum windows.Handle
if err := fwpmSubLayerCreateEnumHandle0(s.handle, tpl, &enum); err != nil {
return nil, err
}
defer fwpmSubLayerDestroyEnumHandle0(s.handle, enum)
var ret []*Sublayer
for {
sublayers, err := s.getSublayerPage(enum)
if err != nil {
return nil, err
}
if len(sublayers) == 0 {
return ret, nil
}
ret = append(ret, sublayers...)
}
}
func (s *Session) getSublayerPage(enum windows.Handle) ([]*Sublayer, error) {
const pageSize = 100
var (
array **fwpmSublayer0
num uint32
)
if err := fwpmSubLayerEnum0(s.handle, enum, pageSize, &array, &num); err != nil {
return nil, err
}
if num == 0 {
return nil, nil
}
defer fwpmFreeMemory0((*struct{})(unsafe.Pointer(&array)))
return fromSublayer0(array, num), nil
}
// AddSublayer creates a new Sublayer.
func (s *Session) AddSublayer(sl *Sublayer) error {
// the WFP API accepts zero GUIDs and interprets it as "give me a
// random GUID". However, we can't get that GUID back out, so it
// would be pointless to make such a request. Stop it here.
if sl.ID.IsZero() {
return errors.New("Sublayer.ID cannot be zero")
}
var a arena
defer a.Dispose()
sl0 := toSublayer0(&a, sl)
return fwpmSubLayerAdd0(s.handle, sl0, nil) // TODO: security descriptor
}
// DeleteSublayer deletes the Sublayer whose GUID is id.
func (s *Session) DeleteSublayer(id SublayerID) error {
if id.IsZero() {
return errors.New("GUID cannot be zero")
}
return fwpmSubLayerDeleteByKey0(s.handle, &id)
}
// ProviderID identifies a WFP provider.
type ProviderID windows.GUID
func (id ProviderID) String() string {
if s := guidNames[windows.GUID(id)]; s != "" {
return s
}
return windows.GUID(id).String()
}
// IsZero reports whether id is nil or the zero GUID.
func (id *ProviderID) IsZero() bool {
return id == nil || *id == ProviderID{}
}
// A Provider is an entity that owns sublayers and filtering rules.
type Provider struct {
// ID is the unique identifier for this provider.
ID ProviderID
// Name is a short descriptive name.
Name string
// Description is a longer description of the provider.
Description string
// Persistent indicates whether the provider is preserved across
// restarts of the filtering engine.
Persistent bool
// Data is optional opaque data that can be held on behalf of the
// Provider.
Data []byte
// ServiceName is an optional Windows service name. If present,
// the rules owned by this Provider are only activated when the
// service is active.
ServiceName string
// Disabled indicates whether the rules owned by this Provider are
// disabled due to its associated service being
// disabled. Read-only, ignored on Provider creation.
Disabled bool
}
func (s *Session) Providers() ([]*Provider, error) {
var enum windows.Handle
if err := fwpmProviderCreateEnumHandle0(s.handle, nil, &enum); err != nil {
return nil, err
}
defer fwpmProviderDestroyEnumHandle0(s.handle, enum)
var ret []*Provider
for {
providers, err := s.getProviderPage(enum)
if err != nil {
return nil, err
}
if len(providers) == 0 {
return ret, nil
}
ret = append(ret, providers...)
}
}
func (s *Session) getProviderPage(enum windows.Handle) ([]*Provider, error) {
const pageSize = 100
var (
array **fwpmProvider0
num uint32
)
if err := fwpmProviderEnum0(s.handle, enum, pageSize, &array, &num); err != nil {
return nil, err
}
if num == 0 {
return nil, nil
}
defer fwpmFreeMemory0((*struct{})(unsafe.Pointer(&array)))
return fromProvider0(array, num), nil
}
// AddProvider creates a new provider.
func (s *Session) AddProvider(p *Provider) error {
if p.ID.IsZero() {
return errors.New("Provider.ID cannot be zero")
}
var a arena
defer a.Dispose()
p0 := toProvider0(&a, p)
return fwpmProviderAdd0(s.handle, p0, nil)
}
// DeleteProvider deletes the Provider whose GUID is id. A provider
// can only be deleted once all the resources it owns have been
// deleted.
func (s *Session) DeleteProvider(id ProviderID) error {
if id.IsZero() {
return errors.New("GUID cannot be zero")
}
return fwpmProviderDeleteByKey0(s.handle, &id)
}
// MatchType is the operator to use when testing a field in a Match.
type MatchType uint32 // do not change type, used in C calls
const (
MatchTypeEqual MatchType = iota
MatchTypeGreater
MatchTypeLess
MatchTypeGreaterOrEqual
MatchTypeLessOrEqual
MatchTypeRange // true if the field value is within the Range.
MatchTypeFlagsAllSet
MatchTypeFlagsAnySet
MatchTypeFlagsNoneSet
MatchTypeEqualCaseInsensitive // only valid on strings, no string fields exist
MatchTypeNotEqual
MatchTypePrefix // TODO: not well documented. Is this prefix.Contains(ip) ?
MatchTypeNotPrefix // TODO: see above.
)
var mtStr = map[MatchType]string{
MatchTypeEqual: "==",
MatchTypeGreater: ">",
MatchTypeLess: "<",
MatchTypeGreaterOrEqual: ">=",
MatchTypeLessOrEqual: "<=",
MatchTypeRange: "in",
MatchTypeFlagsAllSet: "F[all]",
MatchTypeFlagsAnySet: "F[any]",
MatchTypeFlagsNoneSet: "F[none]",
MatchTypeEqualCaseInsensitive: "i==",
MatchTypeNotEqual: "!=",
MatchTypePrefix: "pfx",
MatchTypeNotPrefix: "!pfx",
}
func (m MatchType) String() string {
return mtStr[m]
}
// Match is a matching test that gets run against a layer's field.
type Match struct {
Field FieldID
Op MatchType
Value interface{}
}
func (m Match) String() string {
return fmt.Sprintf("%s %s %v (%T)", m.Field, m.Op, m.Value, m.Value)
}
// Action is an action the filtering engine can execute.
type Action uint32
// Flags which are applied to actions and can be filtered
type ActionFlag uint32
const (
// Action Flags can be used in enumerations to restrict the type of rules to return
ActionFlagTerminating ActionFlag = 0x1000 // only rules which must terminate evaluation
ActionFlagNonTerminating ActionFlag = 0x2000 // only rules which cannot terminate evaluation
ActionFlagCallout ActionFlag = 0x4000 // any rules with a callout
ActionFlagIgnore ActionFlag = 0xFFFFFFFF // match any rule action
// pre-defined actions with appropriate action flags
ActionBlock = (0x01 | Action(ActionFlagTerminating)) // blocks a packet or session
ActionPermit = (0x02 | Action(ActionFlagTerminating)) // permits a packet or session
ActionCalloutTerminating = (0x03 | Action(ActionFlagCallout|ActionFlagTerminating)) // invokes a callout for permit/block
ActionCalloutInspection = (0x04 | Action(ActionFlagCallout|ActionFlagNonTerminating)) // invokes a callout for inspection only
ActionCalloutUnknown = (0x05 | Action(ActionFlagCallout)) // invokes a callout which may return permit or block
)
type TransactionFlag uint32
const (
TransactionReadWrite TransactionFlag = iota
TransactionReadOnly
)
// RuleID identifies a WFP filtering rule.
type RuleID windows.GUID
func (id RuleID) String() string {
if s := guidNames[windows.GUID(id)]; s != "" {
return s
}
return windows.GUID(id).String()
}
// IsZero reports whether id is nil or the zero GUID.
func (id *RuleID) IsZero() bool {
return id == nil || *id == RuleID{}
}
// CalloutID identifies a WFP callout function.
type CalloutID windows.GUID
func (id CalloutID) String() string {
if s := guidNames[windows.GUID(id)]; s != "" {
return s
}
return windows.GUID(id).String()
}
// IsZero reports whether id is nil or the zero GUID.
func (id *CalloutID) IsZero() bool {
return id == nil || *id == CalloutID{}
}
// A Rule is an action to take on packets that match a set of
// conditions.
type Rule struct {
// ID is the unique identifier for this rule.
ID RuleID
// KernelID is the kernel ID for this rule.
KernelID uint64
// Name is a short descriptive name.
Name string
// Description is a longer description of the rule.
Description string
// Layer is the ID of the layer in which the rule runs.
Layer LayerID
// Sublayer is the ID of the sublayer in which the rule runs.
Sublayer SublayerID
// Weight is the priority of the rule relative to other rules in
// its sublayer.
Weight uint64
// Conditions are the tests which must pass for this rule to apply
// to a packet.
Conditions []*Match
// Action is the action to take on matching packets.
Action Action
// Callout is the ID of the callout to invoke. Only valid if
// Action is ActionCalloutTerminating, ActionCalloutInspection, or
// ActionCalloutUnknown.
Callout CalloutID
// PermitIfMissing, if set, indicates that a callout action to a
// callout ID that isn't registered should be translated into an
// ActionPermit, rather than an ActionBlock. Only relevant if
// Action is ActionCalloutTerminating or ActionCalloutUnknown.
PermitIfMissing bool
// HardAction, if set, indicates that the action type is hard and cannot
// be overridden except by a Veto.
HardAction bool
// Persistent indicates whether the rule is preserved across
// restarts of the filtering engine.
Persistent bool
// BootTime indicates that this rule applies only during early
// boot, before the filtering engine fully starts and hands off to
// the normal runtime rules.
BootTime bool
// Provider optionally identifies the Provider that manages this
// rule.
Provider ProviderID
// ProviderData is optional opaque data that can be held on behalf
// of the Provider.
ProviderData []byte
// Disabled indicates whether the rule is currently disabled due
// to its provider being associated with an inactive Windows
// service. See Provider.ServiceName for details.
Disabled bool
}
// TODO: figure out what currently unexposed flags do: Indexed
// TODO: figure out what ProviderContextKey is about. MSDN doesn't explain what contexts are.
func (s *Session) Rules() ([]*Rule, error) { // TODO: support filter settings
var enum windows.Handle
if err := fwpmFilterCreateEnumHandle0(s.handle, nil, &enum); err != nil {
return nil, err
}
defer fwpmFilterDestroyEnumHandle0(s.handle, enum)
var ret []*Rule
for {
rules, err := s.getRulePage(enum)
if err != nil {
return nil, err
}
if len(rules) == 0 {
return ret, nil
}
ret = append(ret, rules...)
}
}
func (s *Session) getRulePage(enum windows.Handle) ([]*Rule, error) {
const pageSize = 100
var (
array **fwpmFilter0
num uint32
)
if err := fwpmFilterEnum0(s.handle, enum, pageSize, &array, &num); err != nil {
return nil, err
}
if num == 0 {
return nil, nil
}
defer fwpmFreeMemory0((*struct{})(unsafe.Pointer(&array)))
return fromFilter0(array, num, s.layerTypes)
}
func (s *Session) AddRule(r *Rule) error {
if r.ID.IsZero() {
return errors.New("Provider.ID cannot be zero")
}
var a arena
defer a.Dispose()
f, err := toFilter0(&a, r, s.layerTypes)
if err != nil {
return err
}
if err := fwpmFilterAdd0(s.handle, f, nil, &f.FilterID); err != nil {
return err
}
return nil
}
func (s *Session) DeleteRule(id RuleID) error {
if id.IsZero() {
return errors.New("GUID cannot be zero")
}
return fwpmFilterDeleteByKey0(s.handle, &id)
}
type DropEvent struct {
Timestamp time.Time
IPProtocol uint8
LocalAddr netip.AddrPort
RemoteAddr netip.AddrPort
AppID string
LayerID uint16
FilterID uint64
}
func (s *Session) DropEvents() ([]*DropEvent, error) {
var enum windows.Handle
if err := fwpmNetEventCreateEnumHandle0(s.handle, nil, &enum); err != nil {
return nil, err
}
defer fwpmNetEventDestroyEnumHandle0(s.handle, enum)
var ret []*DropEvent
for {
events, err := s.getEventPage(enum)
if err != nil {
return nil, err
}
if len(events) == 0 {
return ret, nil
}
ret = append(ret, events...)
}
}
func (s *Session) getEventPage(enum windows.Handle) ([]*DropEvent, error) {
const pageSize = 100
var (
array **fwpmNetEvent1
num uint32
)
if err := fwpmNetEventEnum1(s.handle, enum, pageSize, &array, &num); err != nil {
return nil, err
}
if num == 0 {
return nil, nil
}
defer fwpmFreeMemory0((*struct{})(unsafe.Pointer(&array)))
return fromNetEvent1(array, num)
}
func (s *Session) BeginTransaction(p TransactionFlag) {
err := fwpmTransactionBegin0(s.handle, uint32(p))
if err == nil {
s.status.State = BeganTransaction
return
}
s.status.Err = err
}
func (s *Session) AbortTransaction() {
err := fwpmTransactionAbort0(s.handle)
if err == nil {
s.status.State = AbortedTransaction
return
}
s.status.Err = err
}
func (s *Session) CommitTransaction() {
err := fwpmTransactionCommit0(s.handle)
if err == nil {
s.status.State = CommittedTransaction
return
}
s.status.Err = err
}
// Returns the transaction status of the sesssion
// May be called after Close() to get the fineal status
// on the Close() call.
func (s *Session) TransactionStatus() *TransactionStatus {
return &s.status
}