Invalid mic issues

Hi all,

I am currently playing around with a single channel gateway and chirpstack. In the future I’ll probably switch over to a full multichannel concentrator and use chirpstack-concentratord or one of the other gateway packages. But RFM96W’s is all I have on the device side right now, and it’s hard to get anything else in the current situation. And it gives me a good opportunity to understand the protocol and different libraries too.
On the device side I am using a teensy with MCCI Arduino LMIC, using ABP. As a packet forwarder I am using Congduc Pham’s LowCostLoraGw because, though it leaves a lot to be desired in terms of quality, it is the only single channel gateway that seems to have implemented downlink messages and OTAA.

I have disabled frame counter validation on the gateway, and use symmetric network and application session keys so the byte order doesn’t matter (just for testing). I am now consistently getting MIC errors
form chirpstack-networks-server error="get device-session error: invalid MIC" for all frames except frame 0 and 4. Every time I restart the device, and reactivate it in the gateway, frames 0 and 4 have correct mics, but the rest has wrong mics. All the other frames do show up on the gateway live lorawan frames, and I see them in logs too. Just not on the device since the MIC is wrong.

So I guess my question is 2-fold:

  1. What could cause the MIC to be correctly calculated for frames 0 and 4, but incorrect on all others.
  2. How should the mic be calculated, so I can verify if Arduino LMIC and Chirpstack-networks-server calculate it the same way.

Here are 2 examples, the top one a correct LoraWAN frame with correct MIC, and the second one with incorrect MIC, from the same device

I would recommend to refer to the LoRaWAN Specification: https://lora-alliance.org/resource-hub.

You could also take a look at how this calculation is made within ChirpStack: https://github.com/brocaar/lorawan/blob/master/phypayload.go#L171.

Thanks a lot. It turned out to be a memory issue with the Teensy and the arduino_lmic library.