-
Notifications
You must be signed in to change notification settings - Fork 10
/
genl_hub.go
executable file
·360 lines (328 loc) · 8.52 KB
/
genl_hub.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
// +build linux
package nlgo
import (
"fmt"
"log"
"sync"
"syscall"
"unsafe"
)
type GenlMessage struct {
syscall.NetlinkMessage
Family GenlFamily
}
func (self GenlMessage) Genl() GenlMsghdr {
return *(*GenlMsghdr)(unsafe.Pointer(&self.Data[0]))
}
func (self GenlMessage) FamilyHeader() []byte {
return self.Data[GENL_HDRLEN : GENL_HDRLEN+NLMSG_ALIGN(int(self.Family.Hdrsize))]
}
func (self GenlMessage) Body() []byte {
return self.Data[GENL_HDRLEN+NLMSG_ALIGN(int(self.Family.Hdrsize)):]
}
type GenlListener interface {
GenlListen(GenlMessage)
}
type groupKey struct {
Family string
Name string
}
// GenlHub is generic netlink version of RtHub.
type GenlHub struct {
sock *NlSock
lock *sync.Mutex
familyIds map[uint16]GenlFamily
groupIds map[uint32]GenlGroup
membership []uint32
unilock *sync.Mutex
uniseq uint32
unicast GenlListener
multicast map[groupKey][]GenlListener
}
// sync group registeration
func (self *GenlHub) sync() error {
var active []uint32
for gkey, _ := range self.multicast {
for _, ginfo := range self.groupIds {
if ginfo.Family == gkey.Family && ginfo.Name == gkey.Name {
active = append(active, ginfo.Id)
}
}
}
for _, old := range self.membership {
// should not happen because CTRL_CMD_DELMCAST_GRP event have already cleaned up.
drop := true
for _, new := range active {
if new == old {
drop = false
}
}
if drop {
if err := NlSocketDropMembership(self.sock, int(old)); err != nil {
return err
}
}
}
for _, new := range active {
join := true
for _, old := range self.membership {
if new == old {
join = false
}
}
if join {
if err := NlSocketAddMembership(self.sock, int(new)); err != nil {
return err
}
}
}
self.membership = active
return nil
}
func NewGenlHub() (*GenlHub, error) {
self := &GenlHub{
sock: NlSocketAlloc(),
lock: &sync.Mutex{},
familyIds: map[uint16]GenlFamily{
GENL_ID_CTRL: GenlFamilyCtrl,
},
groupIds: map[uint32]GenlGroup{
GENL_ID_CTRL: GenlGroup{
Id: GENL_ID_CTRL,
Family: "nlctrl",
Name: "notify",
},
},
unilock: &sync.Mutex{},
multicast: make(map[groupKey][]GenlListener),
}
if err := NlConnect(self.sock, syscall.NETLINK_GENERIC); err != nil {
self.Close()
return nil, err
}
if err := self.Add("nlctrl", "notify", self); err != nil {
self.Close()
return nil, err
}
if err := self.Async(GenlFamilyCtrl.DumpRequest(CTRL_CMD_GETFAMILY), self); err != nil {
self.Close()
return nil, err
}
go func() {
for {
buf := make([]byte, syscall.Getpagesize())
if bufN, _, err := syscall.Recvfrom(self.sock.Fd, buf, syscall.MSG_TRUNC); err != nil {
if e, ok := err.(syscall.Errno); ok && e.Temporary() {
continue
}
break
} else if msgs, err := syscall.ParseNetlinkMessage(buf[:bufN]); err != nil {
break
} else {
for _, msg := range msgs {
self.lock.Lock()
family := self.familyIds[msg.Header.Type]
var multi []GenlListener
for gkey, s := range self.multicast {
if family.Name == gkey.Family {
for _, n := range s {
if uniq := func() bool {
for _, m := range multi {
if m == n {
return false
}
}
return true
}(); uniq {
multi = append(multi, n)
}
}
}
}
self.lock.Unlock()
gmsg := GenlMessage{
NetlinkMessage: msg,
Family: family,
}
if msg.Header.Seq == self.uniseq {
if self.unicast != nil {
self.unicast.GenlListen(gmsg)
}
switch msg.Header.Type {
case syscall.NLMSG_DONE, syscall.NLMSG_ERROR:
self.unilock.Unlock()
}
}
if msg.Header.Seq == 0 {
for _, proc := range multi {
proc.GenlListen(gmsg)
}
}
}
}
}
log.Print("genl hub loop exit")
}()
return self, nil
}
func (self GenlHub) Close() {
NlSocketFree(self.sock)
}
// Async() submits request with callback. Note that this locks sending request.
// Calling Async() in GenlListen() may create dead lock.
func (self *GenlHub) Async(msg GenlMessage, listener GenlListener) error {
self.unilock.Lock()
self.lock.Lock()
defer self.lock.Unlock()
self.unicast = listener
self.uniseq = self.sock.SeqNext
hdr := msg.Header
if err := NlSendSimple(self.sock, hdr.Type, hdr.Flags, msg.Data); err != nil {
self.unilock.Unlock()
return err
}
return nil
}
type genlHubCapture struct {
Msgs []GenlMessage
}
func (self *genlHubCapture) GenlListen(msg GenlMessage) {
self.Msgs = append(self.Msgs, msg)
}
// Sync() is synchronous version of Async().
// Calling Sync() in GenlListen() may create dead lock.
func (self *GenlHub) Sync(msg GenlMessage) ([]GenlMessage, error) {
cap := &genlHubCapture{}
if err := self.Async(msg, cap); err != nil {
return nil, err
} else {
self.unilock.Lock() // wait for unicast processing
defer self.unilock.Unlock()
return cap.Msgs, nil
}
}
func (self GenlHub) GenlListen(msg GenlMessage) {
if msg.Header.Type != GENL_ID_CTRL {
return
}
if family, groups, err := GenlCtrl(GenlFamilyCtrl).Parse(msg); err != nil {
log.Printf("genl ctrl msg parse err %v", err)
} else {
self.lock.Lock()
defer self.lock.Unlock()
switch msg.Genl().Cmd {
case CTRL_CMD_NEWFAMILY:
self.familyIds[family.Id] = family
fallthrough
case CTRL_CMD_NEWMCAST_GRP:
for _, grp := range groups {
if _, exists := self.groupIds[grp.Id]; !exists {
self.groupIds[grp.Id] = grp
}
}
case CTRL_CMD_DELFAMILY:
delete(self.familyIds, family.Id)
fallthrough
case CTRL_CMD_DELMCAST_GRP:
for _, grp := range groups {
if _, exists := self.groupIds[grp.Id]; exists {
delete(self.groupIds, grp.Id)
}
}
}
self.sync()
}
}
// Add adds a GenlListener to GenlHub.
// listeners will recieve all of the same family events, regardless of their group registration.
// If you want to limited group multicast, create separate GenlHub for each.
func (self GenlHub) Add(family, group string, listener GenlListener) error {
self.lock.Lock()
defer self.lock.Unlock()
key := groupKey{Family: family, Name: group}
self.multicast[key] = append(self.multicast[key], listener)
return self.sync()
}
func (self GenlHub) Remove(family, group string, listener GenlListener) error {
self.lock.Lock()
defer self.lock.Unlock()
key := groupKey{Family: family, Name: group}
var active []GenlListener
for _, li := range self.multicast[key] {
if li != listener {
active = append(active, li)
}
}
self.multicast[key] = active
return self.sync()
}
type familyListener struct {
name string
done GenlFamily
}
func (self *familyListener) GenlListen(msg GenlMessage) {
switch msg.Header.Type {
case GENL_ID_CTRL:
if family, _, err := GenlCtrl(GenlFamilyCtrl).Parse(msg); err != nil {
log.Printf("genl ctrl %v", err)
} else if family.Name == self.name {
self.done = family
}
}
}
// Family waits for genl family loading. Make sure loading that module by modprobe or by other means.
func (self *GenlHub) Family(name string) GenlFamily {
cap := &familyListener{name: name}
self.Add("nlctrl", "notify", cap)
defer self.Remove("nlctrl", "notify", cap)
self.lock.Lock()
for _, f := range self.familyIds {
if f.Name == name {
self.lock.Unlock()
return f
}
}
self.lock.Unlock()
if err := self.Async(GenlFamilyCtrl.DumpRequest(CTRL_CMD_GETFAMILY), cap); err != nil {
log.Print(err)
} else {
self.unilock.Lock() // wait for unicast processing done.
defer self.unilock.Unlock()
}
return cap.done
}
type GenlCtrl GenlFamily
func (self GenlCtrl) Parse(msg GenlMessage) (GenlFamily, []GenlGroup, error) {
var ret GenlFamily
if attrs, err := CtrlPolicy.Parse(msg.Body()); err != nil {
return ret, nil, err
} else if amap, ok := attrs.(AttrMap); !ok {
return ret, nil, fmt.Errorf("genl ctrl policy parse error")
} else {
if t := amap.Get(CTRL_ATTR_FAMILY_ID); t != nil {
ret.Id = uint16(t.(U16))
}
if t := amap.Get(CTRL_ATTR_FAMILY_NAME); t != nil {
ret.Name = string(t.(NulString))
}
if t := amap.Get(CTRL_ATTR_VERSION); t != nil {
ret.Version = uint8(uint32(t.(U32)))
}
if t := amap.Get(CTRL_ATTR_HDRSIZE); t != nil {
ret.Hdrsize = uint32(t.(U32))
}
var groups []GenlGroup
if grps := amap.Get(CTRL_ATTR_MCAST_GROUPS); grps != nil {
for _, grp := range grps.(AttrSlice).Slice() {
gattr := grp.Value.(AttrMap)
key := uint32(gattr.Get(CTRL_ATTR_MCAST_GRP_ID).(U32))
groups = append(groups, GenlGroup{
Id: key,
Family: ret.Name,
Name: string(gattr.Get(CTRL_ATTR_MCAST_GRP_NAME).(NulString)),
})
}
}
return ret, groups, nil
}
}