@@ -19,21 +19,27 @@ var arpTable = make(map[string]string)
19
19
const reloadInterval = 5 * time .Minute
20
20
21
21
var startOnce sync.Once
22
+
22
23
func init () {
23
24
}
24
25
25
- type SrcMAC struct {
26
+ type MacAddr struct {
26
27
* Base
27
- mac string
28
- adapter string
28
+ mac string
29
+ adapter string
30
+ isSourceIP bool
29
31
}
30
32
31
- func (d * SrcMAC ) RuleType () C.RuleType {
32
- return C .SrcMAC
33
+ func (d * MacAddr ) RuleType () C.RuleType {
34
+ if d .isSourceIP {
35
+ return C .SrcMAC
36
+ } else {
37
+ return C .DstMAC
38
+ }
33
39
}
34
40
35
41
func getLoadArpTableFunc () func () (string , error ) {
36
- const ipv6Error = "can't load ipv6 arp table, SRC-MAC rule can't match src ipv6 address"
42
+ const ipv6Error = "can't load ipv6 arp table, SRC-MAC/DST-MAC rule can't match src ipv6 address"
37
43
38
44
getIpv4Only := func () (string , error ) {
39
45
return cmd .ExecCmd ("arp -a" )
@@ -95,39 +101,45 @@ func getLoadArpTableFunc() func() (string, error) {
95
101
}
96
102
}
97
103
98
- func (d * SrcMAC ) Match (metadata * C.Metadata ) (bool , string ) {
104
+ func (d * MacAddr ) Match (metadata * C.Metadata ) (bool , string ) {
99
105
table := getArpTable ()
100
- srcIP := metadata .SrcIP .String ()
101
- mac , exists := table [srcIP ]
106
+ var ip string
107
+ if d .isSourceIP {
108
+ ip = metadata .SrcIP .String ()
109
+ } else {
110
+ ip = metadata .DstIP .String ()
111
+ }
112
+ mac , exists := table [ip ]
102
113
if exists {
103
114
if mac == d .mac {
104
115
return true , d .adapter
105
116
}
106
117
} else {
107
- log .Warnln ("can't find the IP address in arp table: %s" , srcIP )
118
+ log .Infoln ("can't find the IP address in arp table: %s" , ip )
108
119
}
109
120
return false , d .adapter
110
121
}
111
122
112
- func (d * SrcMAC ) Adapter () string {
123
+ func (d * MacAddr ) Adapter () string {
113
124
return d .adapter
114
125
}
115
126
116
- func (d * SrcMAC ) Payload () string {
127
+ func (d * MacAddr ) Payload () string {
117
128
return d .mac
118
129
}
119
130
120
131
var macRegex = regexp .MustCompile (`^([0-9a-f]{2}:){5}[0-9a-f]{2}$` )
121
132
122
- func NewMAC (mac string , adapter string ) (* SrcMAC , error ) {
133
+ func NewMAC (mac string , adapter string , isSrc bool ) (* MacAddr , error ) {
123
134
macAddr := strings .ReplaceAll (strings .ToLower (mac ), "-" , ":" )
124
135
if ! macRegex .MatchString (macAddr ) {
125
136
return nil , errors .New ("mac address format error: " + mac )
126
137
}
127
- return & SrcMAC {
128
- Base : & Base {},
129
- mac : macAddr ,
130
- adapter : adapter ,
138
+ return & MacAddr {
139
+ Base : & Base {},
140
+ mac : macAddr ,
141
+ adapter : adapter ,
142
+ isSourceIP : isSrc ,
131
143
}, nil
132
144
}
133
145
0 commit comments