Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Handle retries for confirmed uplinks
Browse files Browse the repository at this point in the history
Resolves #272
  • Loading branch information
htdvisser committed Feb 1, 2017
1 parent 82d93a0 commit b00b987
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
26 changes: 19 additions & 7 deletions core/broker/uplink.go
Expand Up @@ -167,13 +167,25 @@ func (b *broker) HandleUplink(uplink *pb.UplinkMessage) (err error) {
ctx = ctx.WithField("RealFCnt", macPayload.FHDR.FCnt)
}

if device.DisableFCntCheck {
// TODO: Add warning to message?
} else if device.FCntUp == 0 {

} else if macPayload.FHDR.FCnt <= device.FCntUp || macPayload.FHDR.FCnt-device.FCntUp > maxFCntGap {
// Replay attack or FCnt gap too big
return errors.NewErrNotFound("device with matching 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)
Expand Down
2 changes: 1 addition & 1 deletion core/broker/uplink_test.go
Expand Up @@ -112,7 +112,7 @@ func TestHandleUplink(t *testing.T) {
GatewayMetadata: &gateway.RxMetadata{Snr: 1.2, GatewayId: gtwID},
ProtocolMetadata: &protocol.RxMetadata{Protocol: &protocol.RxMetadata_Lorawan{Lorawan: &pb_lorawan.Metadata{}}},
})
a.So(err, ShouldHaveSameTypeAs, &errors.ErrNotFound{})
a.So(err, ShouldHaveSameTypeAs, &errors.ErrInvalidArgument{})

// Disable FCnt Check
b.uplinkDeduplicator = NewDeduplicator(10 * time.Millisecond)
Expand Down

0 comments on commit b00b987

Please sign in to comment.