[PATCH] umbim: fix invalid mbim message string encoding

Daniel Danzberger daniel at dd-wrt.com
Tue May 10 02:55:53 PDT 2022


Strings in mbim messages have to follow these formatting rules:
 - 4 byte alignment, padded if not.
 - utf-16 little endian.

Fixes:
 - mbim connect fails with more than 1 string parameter (apn/user/pass)
   when they are not 4 byte aligned.

Signed-off-by: Daniel Danzberger <daniel at dd-wrt.com>
---
 mbim-msg.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/mbim-msg.c b/mbim-msg.c
index 5ec04f4..8465091 100644
--- a/mbim-msg.c
+++ b/mbim-msg.c
@@ -53,8 +53,10 @@ mbim_add_payload(uint8_t len)
 int
 mbim_encode_string(struct mbim_string *str, char *in)
 {
-	int l = strlen(in);
-	int s = mbim_add_payload(l * 2);
+	const int l = strlen(in);
+	const int utf16_len = l * 2;
+	const int pad_len = utf16_len % 4;
+	const int s = mbim_add_payload(utf16_len + pad_len);
 	uint8_t *p = &payload_buffer[s];
 	int i;
 
@@ -62,14 +64,15 @@ mbim_encode_string(struct mbim_string *str, char *in)
 		return -1;
 
 	str->offset = htole32(s);
-	str->length = htole32(l * 2);
+	str->length = htole32(utf16_len);
+
+	/* convert to utf-16 little endian */
 	for (i = 0; i < l; i++)
-		p[i * 2] = in[i];
+		p[i * 2] = htole16(in[i]);
 
 	return 0;
 }
 
-
 char *
 mbim_get_string(struct mbim_string *str, char *in)
 {
-- 
2.35.1




More information about the openwrt-devel mailing list