-
Notifications
You must be signed in to change notification settings - Fork 0
/
join.go
362 lines (301 loc) · 10.6 KB
/
join.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
package join
import (
"crypto/rand"
"encoding/binary"
"fmt"
"strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/brocaar/loraserver/internal/config"
joindown "github.com/brocaar/loraserver/internal/downlink/join"
"github.com/brocaar/loraserver/internal/framelog"
"github.com/brocaar/loraserver/internal/helpers"
"github.com/brocaar/loraserver/internal/models"
"github.com/brocaar/loraserver/internal/storage"
"github.com/brocaar/lorawan"
"github.com/brocaar/lorawan/backend"
"github.com/brocaar/lorawan/band"
)
var tasks = []func(*context) error{
setContextFromJoinRequestPHYPayload,
logJoinRequestFramesCollected,
getDeviceAndDeviceProfile,
validateNonce,
getRandomDevAddr,
getJoinAcceptFromAS,
flushDeviceQueue,
createDeviceSession,
createDeviceActivation,
sendJoinAcceptDownlink,
}
type context struct {
RXPacket models.RXPacket
JoinRequestPayload *lorawan.JoinRequestPayload
Device storage.Device
ServiceProfile storage.ServiceProfile
DeviceProfile storage.DeviceProfile
DevAddr lorawan.DevAddr
CFList []uint32
JoinAnsPayload backend.JoinAnsPayload
DeviceSession storage.DeviceSession
}
// Handle handles a join-request
func Handle(rxPacket models.RXPacket) error {
ctx := context{
RXPacket: rxPacket,
}
for _, t := range tasks {
if err := t(&ctx); err != nil {
return err
}
}
return nil
}
func setContextFromJoinRequestPHYPayload(ctx *context) error {
jrPL, ok := ctx.RXPacket.PHYPayload.MACPayload.(*lorawan.JoinRequestPayload)
if !ok {
return fmt.Errorf("expected *lorawan.JoinRequestPayload, got: %T", ctx.RXPacket.PHYPayload.MACPayload)
}
ctx.JoinRequestPayload = jrPL
return nil
}
func logJoinRequestFramesCollected(ctx *context) error {
var gatewayIDs []string
for _, p := range ctx.RXPacket.RXInfoSet {
gatewayIDs = append(gatewayIDs, helpers.GetGatewayID(p).String())
}
uplinkFrameSet, err := framelog.CreateUplinkFrameSet(ctx.RXPacket)
if err != nil {
return errors.Wrap(err, "create uplink frame-set error")
}
if err := framelog.LogUplinkFrameForDevEUI(config.C.Redis.Pool, ctx.JoinRequestPayload.DevEUI, uplinkFrameSet); err != nil {
log.WithError(err).Error("log uplink frame for device error")
}
log.WithFields(log.Fields{
"dev_eui": ctx.JoinRequestPayload.DevEUI,
"gw_count": len(gatewayIDs),
"gw_ids": strings.Join(gatewayIDs, ", "),
"mtype": ctx.RXPacket.PHYPayload.MHDR.MType,
}).Info("packet(s) collected")
return nil
}
func getDeviceAndDeviceProfile(ctx *context) error {
var err error
ctx.Device, err = storage.GetDevice(config.C.PostgreSQL.DB, ctx.JoinRequestPayload.DevEUI)
if err != nil {
return errors.Wrap(err, "get device error")
}
ctx.DeviceProfile, err = storage.GetDeviceProfile(config.C.PostgreSQL.DB, ctx.Device.DeviceProfileID)
if err != nil {
return errors.Wrap(err, "get device-profile error")
}
ctx.ServiceProfile, err = storage.GetServiceProfile(config.C.PostgreSQL.DB, ctx.Device.ServiceProfileID)
if err != nil {
return errors.Wrap(err, "get service-profile error")
}
if !ctx.DeviceProfile.SupportsJoin {
return errors.New("device does not support join")
}
return nil
}
func validateNonce(ctx *context) error {
// validate that the nonce has not been used yet
err := storage.ValidateDevNonce(config.C.PostgreSQL.DB, ctx.JoinRequestPayload.JoinEUI, ctx.JoinRequestPayload.DevEUI, ctx.JoinRequestPayload.DevNonce, lorawan.JoinRequestType)
if err != nil {
return errors.Wrap(err, "validate dev-nonce error")
}
return nil
}
func getRandomDevAddr(ctx *context) error {
devAddr, err := storage.GetRandomDevAddr(config.C.Redis.Pool, config.C.NetworkServer.NetID)
if err != nil {
return errors.Wrap(err, "get random DevAddr error")
}
ctx.DevAddr = devAddr
return nil
}
func getJoinAcceptFromAS(ctx *context) error {
b, err := ctx.RXPacket.PHYPayload.MarshalBinary()
if err != nil {
return errors.Wrap(err, "PHYPayload marshal binary error")
}
randomBytes := make([]byte, 4)
_, err = rand.Read(randomBytes)
if err != nil {
return errors.Wrap(err, "read random bytes error")
}
transactionID := binary.LittleEndian.Uint32(randomBytes)
var cFListB []byte
cFList := config.C.NetworkServer.Band.Band.GetCFList(ctx.DeviceProfile.MACVersion)
if cFList != nil {
cFListB, err = cFList.MarshalBinary()
if err != nil {
return errors.Wrap(err, "marshal cflist error")
}
}
// note about the OptNeg field:
// it must only be set to true for devices != 1.0.x as it will indicate to
// the join-server and device how to derrive the session-keys and how to
// sign the join-accept message
joinReqPL := backend.JoinReqPayload{
BasePayload: backend.BasePayload{
ProtocolVersion: backend.ProtocolVersion1_0,
SenderID: config.C.NetworkServer.NetID.String(),
ReceiverID: ctx.JoinRequestPayload.JoinEUI.String(),
TransactionID: transactionID,
MessageType: backend.JoinReq,
},
MACVersion: ctx.DeviceProfile.MACVersion,
PHYPayload: backend.HEXBytes(b),
DevEUI: ctx.JoinRequestPayload.DevEUI,
DevAddr: ctx.DevAddr,
DLSettings: lorawan.DLSettings{
OptNeg: !strings.HasPrefix(ctx.DeviceProfile.MACVersion, "1.0"), // must be set to true for != "1.0" devices
RX2DataRate: uint8(config.C.NetworkServer.NetworkSettings.RX2DR),
RX1DROffset: uint8(config.C.NetworkServer.NetworkSettings.RX1DROffset),
},
RxDelay: config.C.NetworkServer.NetworkSettings.RX1Delay,
CFList: backend.HEXBytes(cFListB),
}
jsClient, err := config.C.JoinServer.Pool.Get(ctx.JoinRequestPayload.JoinEUI)
if err != nil {
return errors.Wrap(err, "get join-server client error")
}
ctx.JoinAnsPayload, err = jsClient.JoinReq(joinReqPL)
if err != nil {
return errors.Wrap(err, "join-request to join-server error")
}
return nil
}
func flushDeviceQueue(ctx *context) error {
if err := storage.FlushDeviceQueueForDevEUI(config.C.PostgreSQL.DB, ctx.Device.DevEUI); err != nil {
return errors.Wrap(err, "flush device-queue error")
}
return nil
}
func createDeviceSession(ctx *context) error {
ds := storage.DeviceSession{
DeviceProfileID: ctx.Device.DeviceProfileID,
ServiceProfileID: ctx.Device.ServiceProfileID,
RoutingProfileID: ctx.Device.RoutingProfileID,
MACVersion: ctx.DeviceProfile.MACVersion,
DevAddr: ctx.DevAddr,
JoinEUI: ctx.JoinRequestPayload.JoinEUI,
DevEUI: ctx.JoinRequestPayload.DevEUI,
RXWindow: storage.RX1,
RXDelay: uint8(config.C.NetworkServer.NetworkSettings.RX1Delay),
RX1DROffset: uint8(config.C.NetworkServer.NetworkSettings.RX1DROffset),
RX2DR: uint8(config.C.NetworkServer.NetworkSettings.RX2DR),
RX2Frequency: config.C.NetworkServer.Band.Band.GetDefaults().RX2Frequency,
EnabledUplinkChannels: config.C.NetworkServer.Band.Band.GetStandardUplinkChannelIndices(),
ExtraUplinkChannels: make(map[int]band.Channel),
UplinkGatewayHistory: map[lorawan.EUI64]storage.UplinkGatewayHistory{},
MaxSupportedDR: ctx.ServiceProfile.DRMax,
SkipFCntValidation: ctx.Device.SkipFCntCheck,
PingSlotDR: ctx.DeviceProfile.PingSlotDR,
PingSlotFrequency: int(ctx.DeviceProfile.PingSlotFreq),
NbTrans: 1,
ReferenceAltitude: ctx.Device.ReferenceAltitude,
}
if ctx.JoinAnsPayload.AppSKey != nil {
ds.AppSKeyEvelope = &storage.KeyEnvelope{
KEKLabel: ctx.JoinAnsPayload.AppSKey.KEKLabel,
AESKey: ctx.JoinAnsPayload.AppSKey.AESKey,
}
}
if ctx.JoinAnsPayload.NwkSKey != nil {
key, err := unwrapNSKeyEnvelope(ctx.JoinAnsPayload.NwkSKey)
if err != nil {
return err
}
ds.SNwkSIntKey = key
ds.FNwkSIntKey = key
ds.NwkSEncKey = key
}
if ctx.JoinAnsPayload.SNwkSIntKey != nil {
key, err := unwrapNSKeyEnvelope(ctx.JoinAnsPayload.SNwkSIntKey)
if err != nil {
return err
}
ds.SNwkSIntKey = key
}
if ctx.JoinAnsPayload.FNwkSIntKey != nil {
key, err := unwrapNSKeyEnvelope(ctx.JoinAnsPayload.FNwkSIntKey)
if err != nil {
return err
}
ds.FNwkSIntKey = key
}
if ctx.JoinAnsPayload.NwkSEncKey != nil {
key, err := unwrapNSKeyEnvelope(ctx.JoinAnsPayload.NwkSEncKey)
if err != nil {
return err
}
ds.NwkSEncKey = key
}
if cfList := config.C.NetworkServer.Band.Band.GetCFList(ctx.DeviceProfile.MACVersion); cfList != nil && cfList.CFListType == lorawan.CFListChannel {
channelPL, ok := cfList.Payload.(*lorawan.CFListChannelPayload)
if !ok {
return fmt.Errorf("expected *lorawan.CFListChannelPayload, got %T", cfList.Payload)
}
for _, f := range channelPL.Channels {
if f == 0 {
continue
}
i, err := config.C.NetworkServer.Band.Band.GetUplinkChannelIndex(int(f), false)
if err != nil {
// if this happens, something is really wrong
log.WithError(err).WithFields(log.Fields{
"frequency": f,
}).Error("unknown fclist frequency")
continue
}
// add extra channel to enabled channels
ds.EnabledUplinkChannels = append(ds.EnabledUplinkChannels, i)
// add extra channel to extra uplink channels, so that we can
// keep track on frequency and data-rate changes
c, err := config.C.NetworkServer.Band.Band.GetUplinkChannel(i)
if err != nil {
return errors.Wrap(err, "get uplink channel error")
}
ds.ExtraUplinkChannels[i] = c
}
}
if ctx.DeviceProfile.PingSlotPeriod != 0 {
ds.PingSlotNb = (1 << 12) / ctx.DeviceProfile.PingSlotPeriod
}
ctx.DeviceSession = ds
if err := storage.SaveDeviceSession(config.C.Redis.Pool, ctx.DeviceSession); err != nil {
return errors.Wrap(err, "save node-session error")
}
if err := storage.FlushMACCommandQueue(config.C.Redis.Pool, ctx.DeviceSession.DevEUI); err != nil {
return fmt.Errorf("flush mac-command queue error: %s", err)
}
return nil
}
func createDeviceActivation(ctx *context) error {
da := storage.DeviceActivation{
DevEUI: ctx.DeviceSession.DevEUI,
JoinEUI: ctx.DeviceSession.JoinEUI,
DevAddr: ctx.DeviceSession.DevAddr,
SNwkSIntKey: ctx.DeviceSession.SNwkSIntKey,
FNwkSIntKey: ctx.DeviceSession.FNwkSIntKey,
NwkSEncKey: ctx.DeviceSession.NwkSEncKey,
DevNonce: ctx.JoinRequestPayload.DevNonce,
JoinReqType: lorawan.JoinRequestType,
}
if err := storage.CreateDeviceActivation(config.C.PostgreSQL.DB, &da); err != nil {
return errors.Wrap(err, "create device-activation error")
}
return nil
}
func sendJoinAcceptDownlink(ctx *context) error {
var phy lorawan.PHYPayload
if err := phy.UnmarshalBinary(ctx.JoinAnsPayload.PHYPayload[:]); err != nil {
return errors.Wrap(err, "unmarshal downlink phypayload error")
}
if err := joindown.Handle(ctx.DeviceSession, ctx.RXPacket, phy); err != nil {
return errors.Wrap(err, "run join-response flow error")
}
return nil
}