Skip to content

Invalid Decoding JoinRequest on mipsel #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
devleesch001 opened this issue Feb 26, 2024 · 7 comments
Closed

Invalid Decoding JoinRequest on mipsel #52

devleesch001 opened this issue Feb 26, 2024 · 7 comments

Comments

@devleesch001
Copy link
Contributor

devleesch001 commented Feb 26, 2024

Hello,

I have an importante issue.

What happened?

When the MQTT Forwarder deserialize JoinRequest Frame from Semtech UDP GWMP Frame, the MIC code is badly decoded.

For exemple the Semtech UDP GWMP JoinRequest have this MIC code [1 192 29 90] but after decoding by MQTT Forwarder have this [1 192 28 90].

All my JoinRequest with MQTT Packet Forwarder are in MIC Error.

I am sur the problem appears during json deserialization, especially when decoding the payload in base64.

How I found?

I added in code a debug print (only for me), before and After this line https://github.com/chirpstack/chirpstack-mqtt-forwarder/blob/8b2ed3d2a2bc1aab8d11a6a78a2d09929be983b9/src/backend/semtech_udp/mod.rs#L342C5-L342C51

Like this.

debug!("--- RAW UPLINK --- {:?}", data);

let pl = structs::PushData::from_slice(data)?;

debug!("--- DECODED PAYLOAD --- {:?}", pl.payload);

And I obtained this :

RAW UDP GWMP Frame :

7b 22 72 78 70 6b 22 3a 5b 7b 22 6a 76 65 72 22 3a 31 2c 22 74 6d 73 74 22 3a 38 36 33 34 32 32 31 33 2c 22 63 68 61 6e 22 3a 37 2c 22 72 66 63 68 22 3a 31 2c 22 66 72 65 71 22 3a 38 36 38 2e 35 30 30 30 30 30 2c 22 6d 69 64 22 3a 20 30 2c 22 73 74 61 74 22 3a 31 2c 22 6d 6f 64 75 22 3a 22 4c 4f 52 41 22 2c 22 64 61 74 72 22 3a 22 53 46 31 32 42 57 31 32 35 22 2c 22 63 6f 64 72 22 3a 22 34 2f 35 22 2c 22 72 73 73 69 73 22 3a 2d 35 32 2c 22 6c 73 6e 72 22 3a 36 2e 32 2c 22 66 6f 66 66 22 3a 2d 37 34 37 36 2c 22 72 73 73 69 22 3a 2d 35 31 2c 22 73 69 7a 65 22 3a 32 33 2c 22 64 61 74 61 22 3a 22 41 41 49 41 41 41 44 77 37 4a 41 41 2b 52 30 78 46 67 48 73 6b 41 41 6c 4c 77 48 41 48 56 6f 3d 22 7d 5d 7d

Manual decoding I optain this Payload in JSON

{"rxpk":[{"jver":1,"tmst":86342213,"chan":7,"rfch":1,"freq":868.500000,"mid": 0,"stat":1,"modu":"LORA","datr":"SF12BW125","codr":"4/5","rssis":-52,"lsnr":6.2,"foff":-7476,"rssi":-51,"size":23,"data":"AAIAAADw7JAA+R0xFgHskAAlLwHAHVo="}]}

This LoRaWAN DATA

AAIAAADw7JAA+R0xFgHskAAlLwHAHVo=
00 02 00 00 00 f0 ec 90 00 f9 1d 31 16 01 ec 90 00 25 2f 01 c0 1d 5a

Contain this MIC

MIC : [1 192 29 90], 01c01d5a

But after let pl = structs::PushData::from_slice(data)?;

When MQTT Forwarder deserialize the RAW UDP GWMP and decode the DATA of Payload Frame I obtaine this LoRaWAN Frame

00 02 00 00 00 f0 ec 90 00 f9 1d 31 16 01 ec 90 00 25 2f 01 c0 1c 5a
MIC : [1 192 28 90], 01c01c5a

The mic code does not correspond to the UDP frame.

GOOD 01c01d5a, BAD 01c01c5a

Material

Gateway : RAK7268CV2

uname : Linux RAK7268CV2 5.4.154 #0 Fri Nov 24 07:30:08 2023 mips GNU/Linux

cpuinfo :

system type		: MediaTek MT7628AN ver:1 eco:2
machine			: Rakwireless RAK636
processor		: 0
cpu model		: MIPS 24KEc V5.5
BogoMIPS		: 385.02
wait instruction	: yes
microsecond timers	: yes
tlb_entries		: 32
extra interrupt vector	: yes
hardware watchpoint	: yes, count: 4, address/irw mask: [0x0ffc, 0x0ffc, 0x0ffb, 0x0ffb]
isa			: mips1 mips2 mips32r1 mips32r2
ASEs implemented	: mips16 dsp
Options implemented	: tlb 4kex 4k_cache prefetch mcheck ejtag llsc pindexed_dcache userlocal vint perf_cntr_intr_bit perf
shadow register sets	: 1
kscratch registers	: 0
package			: 0
core			: 0
VCED exceptions		: not available
VCEI exceptions		: not available

OS : WisGateOS_2.2.1_RAK based on OpenWRT 21.2
Toolchain : make dist-mipsel-unknown-linux-musl

@devleesch001
Copy link
Contributor Author

devleesch001 commented Feb 26, 2024

The problem seems to come from the base64 library, i made little poc to simple decode base64 string, follwoing oficial documention I obtain this

use base64::prelude::*;

fn main() {
    assert_eq!(BASE64_STANDARD.decode(b"+uwgVQA=").expect("panic"), b"\xFA\xEC\x20\x55\0");
    assert_eq!(BASE64_STANDARD.encode(b"\xFF\xEC\x20\x55\0"), "/+wgVQA=");
}

on my pc x86_64-unknown-linux-musl :

./base64_test
echo $?
0

All it's ok

But on gateway with mipsel-unknown-linux-musl :

./base64_test
thread 'main' panicked at src/main.rs:5:5:
assertion `left == right` failed
  left: [250, 236, 32, 84, 0]
 right: [250, 236, 32, 85, 0]

echo $?
101

This is exactly the same probleme.

The problem could be come cross-rs, but I don't know.

@devleesch001 devleesch001 changed the title Invalid Decoding JoinRequest Invalid Decoding JoinRequest on Ramips Feb 26, 2024
@devleesch001 devleesch001 changed the title Invalid Decoding JoinRequest on Ramips Invalid Decoding JoinRequest on mipsel Feb 26, 2024
@devleesch001
Copy link
Contributor Author

Maybe the compiler.

rust-lang/compiler-team#648

Maybe we should develop our custom base64 decoding for this architecture

@brocaar
Copy link
Contributor

brocaar commented Feb 27, 2024

Thanks for reporting. This sound very similar to #50.

@brocaar
Copy link
Contributor

brocaar commented Feb 27, 2024

I can confirm that I was able to reproduce this issue. After compiling using the latest Rust toolchain, everything works as expected. I think this was a toolchain related issue. As mips has been moved to a tier 3 target, I have made the following change in the Makefile: a9d9ebb

I'm creating a new release right now, which should be available shortly.

@brocaar
Copy link
Contributor

brocaar commented Feb 27, 2024

This was released as v4.2.0.

@brocaar brocaar closed this as completed Feb 27, 2024
@devleesch001
Copy link
Contributor Author

I just compiled it and it works on my gateway.
Thanks you very much

@brocaar
Copy link
Contributor

brocaar commented Mar 11, 2024

Sorry, v4.2.1 re-introduces this issue because I downgraded the Rust toolchain because of nightly build issues (I forgot about that this might cause this issue). I will release a v4.2.2 shortly.

brocaar added a commit that referenced this issue Mar 11, 2024
MIPS has moved to tier 3, we need the nightly version for
cross-compilation to this target. To avoid this, I reverted back to
1.74.1 (latest version with MIPS toolchain pre-compiled), however this
re-introduces #52. Currently the latest nightly version returns an error
during build:

 undefined reference to `pthread_getname_np'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants