This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
/
uplink.go
280 lines (244 loc) · 8.64 KB
/
uplink.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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package broker
import (
"crypto/md5"
"encoding/hex"
"fmt"
"sort"
"time"
pb "github.com/TheThingsNetwork/api/broker"
pb_discovery "github.com/TheThingsNetwork/api/discovery"
"github.com/TheThingsNetwork/api/logfields"
"github.com/TheThingsNetwork/api/networkserver"
pb_lorawan "github.com/TheThingsNetwork/api/protocol/lorawan"
"github.com/TheThingsNetwork/api/trace"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/ttn/core/types"
"github.com/TheThingsNetwork/ttn/utils/errors"
"github.com/TheThingsNetwork/ttn/utils/fcnt"
"github.com/brocaar/lorawan"
)
const maxFCntGap = 16384
func (b *broker) HandleUplink(uplink *pb.UplinkMessage) (err error) {
ctx := b.Ctx.WithFields(logfields.ForMessage(uplink))
start := time.Now()
deduplicatedUplink := new(pb.DeduplicatedUplinkMessage)
deduplicatedUplink.ServerTime = start.UnixNano()
b.RegisterReceived(uplink)
defer func() {
if err != nil {
if deduplicatedUplink != nil {
deduplicatedUplink.Trace = deduplicatedUplink.Trace.WithEvent(trace.DropEvent, "reason", err)
}
ctx.WithError(err).Warn("Could not handle uplink")
} else {
b.RegisterHandled(uplink)
ctx.WithField("Duration", time.Now().Sub(start)).Info("Handled uplink")
}
if deduplicatedUplink != nil && b.monitorStream != nil {
b.monitorStream.Send(deduplicatedUplink)
}
}()
b.status.uplink.Mark(1)
uplink.Trace = uplink.Trace.WithEvent(trace.ReceiveEvent)
// De-duplicate uplink messages
duplicates := b.deduplicateUplink(uplink)
if len(duplicates) == 0 {
return nil
}
ctx = ctx.WithField("Duplicates", len(duplicates))
b.status.uplinkUnique.Mark(1)
deduplicatedUplink.Payload = duplicates[0].Payload
deduplicatedUplink.ProtocolMetadata = duplicates[0].ProtocolMetadata
deduplicatedUplink.Trace = deduplicatedUplink.Trace.WithEvent(trace.DeduplicateEvent,
"duplicates", len(duplicates),
)
for _, duplicate := range duplicates {
if duplicate.Trace != nil {
deduplicatedUplink.Trace.Parents = append(deduplicatedUplink.Trace.Parents, duplicate.Trace)
}
}
if deduplicatedUplink.ProtocolMetadata.GetLoRaWAN() == nil {
return errors.NewErrInvalidArgument("Uplink", "does not contain LoRaWAN metadata")
}
// LoRaWAN: Unmarshal
var phyPayload lorawan.PHYPayload
err = phyPayload.UnmarshalBinary(deduplicatedUplink.Payload)
if err != nil {
return err
}
macPayload, ok := phyPayload.MACPayload.(*lorawan.MACPayload)
if !ok {
return errors.NewErrInvalidArgument("Uplink", "does not contain a MAC payload")
}
// Request devices from NS
devAddr := types.DevAddr(macPayload.FHDR.DevAddr)
ctx = ctx.WithFields(ttnlog.Fields{
"DevAddr": devAddr,
"FCnt": macPayload.FHDR.FCnt,
})
var getDevicesResp *networkserver.DevicesResponse
getDevicesResp, err = b.ns.GetDevices(b.Component.GetContext(b.nsToken), &networkserver.DevicesRequest{
DevAddr: devAddr,
FCnt: macPayload.FHDR.FCnt,
})
if err != nil {
return errors.Wrap(errors.FromGRPCError(err), "NetworkServer did not return devices")
}
b.status.deduplication.Update(int64(len(getDevicesResp.Results)))
duplicatesHistogram.Observe(float64(len(getDevicesResp.Results)))
if len(getDevicesResp.Results) == 0 {
return errors.NewErrNotFound(fmt.Sprintf("Device with DevAddr %s and FCnt <= %d", devAddr, macPayload.FHDR.FCnt))
}
ctx = ctx.WithField("DevAddrResults", len(getDevicesResp.Results))
deduplicatedUplink.Trace = deduplicatedUplink.Trace.WithEvent("got devices from networkserver",
"devices", len(getDevicesResp.Results),
)
// Sort by FCntUp to optimize the number of MIC checks
sort.Sort(ByFCntUp(getDevicesResp.Results))
// Find AppEUI/DevEUI through MIC check
var device *pb_lorawan.Device
var micChecks int
originalFCnt := macPayload.FHDR.FCnt
for _, candidate := range getDevicesResp.Results {
nwkSKey := lorawan.AES128Key(*candidate.NwkSKey)
// First check with the 16 bit counter
micChecks++
ok, err = phyPayload.ValidateMIC(nwkSKey)
if err != nil {
return err
}
if ok {
device = candidate
break
}
if fullFCnt := fcnt.GetFull(candidate.FCntUp, uint16(originalFCnt)); fullFCnt != originalFCnt && candidate.Uses32BitFCnt {
macPayload.FHDR.FCnt = fullFCnt
// Then check again with the 32 bit counter
if macPayload.FHDR.FCnt != originalFCnt {
micChecks++
ok, err = phyPayload.ValidateMIC(nwkSKey)
if err != nil {
return err
}
if ok {
device = candidate
break
}
}
macPayload.FHDR.FCnt = originalFCnt
}
}
if device == nil {
return errors.NewErrNotFound("device that validates MIC")
}
micChecksHistogram.Observe(float64(micChecks))
ctx = ctx.WithFields(ttnlog.Fields{
"MICChecks": micChecks,
"DevEUI": device.DevEUI,
"AppEUI": device.AppEUI,
"AppID": device.AppID,
"DevID": device.DevID,
"FCnt": originalFCnt,
})
deduplicatedUplink.DevEUI = &device.DevEUI
deduplicatedUplink.AppEUI = &device.AppEUI
deduplicatedUplink.AppID = device.AppID
deduplicatedUplink.DevID = device.DevID
deduplicatedUplink.Trace = deduplicatedUplink.Trace.WithEvent(trace.CheckMICEvent, "mic checks", micChecks)
if macPayload.FHDR.FCnt != originalFCnt {
ctx = ctx.WithField("RealFCnt", macPayload.FHDR.FCnt)
}
switch {
case macPayload.FHDR.FCnt > device.FCntUp && macPayload.FHDR.FCnt-device.FCntUp <= maxFCntGap:
// FCnt higher than latest and within max FCnt gap (normal case)
case device.DisableFCntCheck:
// FCnt Check disabled. Rely on MIC check only
case device.FCntUp == 0:
// FCntUp is reset. We don't know where the device will start sending.
case macPayload.FHDR.FCnt == device.FCntUp:
if phyPayload.MHDR.MType == lorawan.ConfirmedDataUp {
// Retry of confirmed uplink
break
}
fallthrough
case macPayload.FHDR.FCnt <= device.FCntUp:
return errors.NewErrInvalidArgument("FCnt", "not high enough")
case macPayload.FHDR.FCnt-device.FCntUp > maxFCntGap:
return errors.NewErrInvalidArgument("FCnt", "too high")
default:
return errors.NewErrInternal("FCnt check failed")
}
// Add FCnt to Metadata (because it's not marshaled in lorawan payload)
deduplicatedUplink.ProtocolMetadata.GetLoRaWAN().FCnt = macPayload.FHDR.FCnt
// Collect GatewayMetadata and DownlinkOptions
var downlinkOptions []*pb.DownlinkOption
for _, duplicate := range duplicates {
deduplicatedUplink.GatewayMetadata = append(deduplicatedUplink.GatewayMetadata, &duplicate.GatewayMetadata)
downlinkOptions = append(downlinkOptions, duplicate.DownlinkOptions...)
}
// Select best DownlinkOption
if len(downlinkOptions) > 0 {
deduplicatedUplink.ResponseTemplate = &pb.DownlinkMessage{
DevEUI: device.DevEUI,
AppEUI: device.AppEUI,
AppID: device.AppID,
DevID: device.DevID,
DownlinkOption: selectBestDownlink(downlinkOptions),
}
}
// Pass Uplink through NS
deduplicatedUplink, err = b.ns.Uplink(b.Component.GetContext(b.nsToken), deduplicatedUplink)
if err != nil {
return errors.Wrap(errors.FromGRPCError(err), "NetworkServer did not handle uplink")
}
var announcements []*pb_discovery.Announcement
announcements, err = b.Discovery.GetAllHandlersForAppID(device.AppID)
if err != nil {
return err
}
if len(announcements) == 0 {
return errors.NewErrNotFound(fmt.Sprintf("Handler for AppID %s", device.AppID))
}
if len(announcements) > 1 {
return errors.NewErrInternal(fmt.Sprintf("Multiple Handlers for AppID %s", device.AppID))
}
var handler chan<- *pb.DeduplicatedUplinkMessage
handler, err = b.getHandlerUplink(announcements[0].ID)
if err != nil {
return err
}
deduplicatedUplink.Trace = deduplicatedUplink.Trace.WithEvent(trace.ForwardEvent,
"handler", announcements[0].ID,
)
handler <- deduplicatedUplink
return nil
}
func (b *broker) deduplicateUplink(duplicate *pb.UplinkMessage) (uplinks []*pb.UplinkMessage) {
sum := md5.Sum(duplicate.Payload)
key := hex.EncodeToString(sum[:])
list := b.uplinkDeduplicator.Deduplicate(key, duplicate)
if len(list) == 0 {
return
}
for _, duplicate := range list {
uplinks = append(uplinks, duplicate.(*pb.UplinkMessage))
}
return
}
func selectBestDownlink(options []*pb.DownlinkOption) *pb.DownlinkOption {
sort.Sort(ByScore(options))
return options[0]
}
// ByFCntUp implements sort.Interface for []*pb_lorawan.Device based on FCnt
type ByFCntUp []*pb_lorawan.Device
func (a ByFCntUp) Len() int { return len(a) }
func (a ByFCntUp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByFCntUp) Less(i, j int) bool {
// Devices that disable the FCnt check have low priority
if a[i].DisableFCntCheck {
return 2*int(a[i].FCntUp)+100 < int(a[j].FCntUp)
}
return int(a[i].FCntUp) < int(a[j].FCntUp)
}