TheThingsArchive / ttn

The Things Network Stack V2
https://www.thethingsnetwork.org
MIT License
461 stars 276 forks source link

when port is nil will cause a “Invalid MIC” #668

Closed RongAtRabbit closed 6 years ago

RongAtRabbit commented 7 years ago

This is a MIC VALID bug report for the backend

What do you want to do? I want to receive a ack message from lora module What steps did you take? receiving the ack What went wrong or what is missing? The Message with port==nil and len(frmpayload) == 0. The backend report "Invalid MIC"

hdl.1 | WARN Could not handle uplink AppID=3AC7CB39D7CF0A57 DevID=0000000050000007 error=Uplink not valid: Invalid MIC

Do you have Screenshots? no What kind of OS/Browser/Gateway are you using? Which version? nil What are the IDs and EUIs of your Device/Gateway? (if applicable) nil What do your configuration files look like?

What do your log files look like?

Can you fix this yourself and submit a pull request?

RongAtRabbit commented 7 years ago

I think the reason is in

 MACPayloadFromPayload;
        if payload.FPort != nil { ==== >when payload.FPort== nil,it will init the mac.FPort = 0
            mac.FPort = int32(*payload.FPort)
        }

but in function :

// Payload converts the MACPayload to a lorawan.Payload
func (msg *Message_MacPayload) Payload() lorawan.Payload {
    if m.FPort >= 0 { ====>this step will give port a value 0
        fPort := uint8(m.FPort)
        mac.FPort = &fPort
    }

above change the port nil to value 0 MIC is calculated by port nil. but will validate by port value 0. so it cause the "Invalid MIC"

htdvisser commented 7 years ago

MACPayloadFromPayload will indeed output an FPort of 0 for both situations (FPort=0 and FPort=nil). However, the Payload() will only set the FPort if it is greater than 0 or set it to 0 if there is a FRMPayload. In other cases it will remain nil:

if m.FPort > 0 {
    fPort := uint8(m.FPort)
    mac.FPort = &fPort
}
if len(m.FRMPayload) != 0 {
    mac.FRMPayload = []lorawan.Payload{&lorawan.DataPayload{Bytes: m.FRMPayload}}
    if mac.FPort == nil {
        fPort := uint8(0)
        mac.FPort = &fPort
    }
}

Could you give a little more information about the device you are using for this? It would be very helpful to have the