<p>Harald Welte <strong>merged</strong> this change.</p><p><a href="https://gerrit.osmocom.org/9528">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Jenkins Builder: Verified
  Harald Welte: Looks good to me, approved

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">gsm/gsm0480.c: introduce gsm0480_extract_ie_by_tag()<br><br>In some cases, there is no need to parse the whole message,<br>e.g. during the conversion from DTAP to GSUP/MAP. This<br>function can be used to extract given IE from a message.<br><br>Change-Id: I3989d061903352473305f80712f1a1560d05df3d<br>---<br>M include/osmocom/gsm/gsm0480.h<br>M src/gsm/gsm0480.c<br>M src/gsm/libosmogsm.map<br>M tests/ussd/ussd_test.c<br>M tests/ussd/ussd_test.ok<br>5 files changed, 172 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/osmocom/gsm/gsm0480.h b/include/osmocom/gsm/gsm0480.h</span><br><span>index b0b6aa2..b31f8a7 100644</span><br><span>--- a/include/osmocom/gsm/gsm0480.h</span><br><span>+++ b/include/osmocom/gsm/gsm0480.h</span><br><span>@@ -91,6 +91,8 @@</span><br><span>    uint8_t invoke_id;</span><br><span> };</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+int gsm0480_extract_ie_by_tag(const struct gsm48_hdr *hdr, uint16_t msg_len,</span><br><span style="color: hsl(120, 100%, 40%);">+                          uint8_t **ie, uint16_t *ie_len, uint8_t ie_tag);</span><br><span> int gsm0480_decode_ss_request(const struct gsm48_hdr *hdr, uint16_t len,</span><br><span>                           struct ss_request *request);</span><br><span> </span><br><span>diff --git a/src/gsm/gsm0480.c b/src/gsm/gsm0480.c</span><br><span>index 5c73e5b..dfd9877 100644</span><br><span>--- a/src/gsm/gsm0480.c</span><br><span>+++ b/src/gsm/gsm0480.c</span><br><span>@@ -33,6 +33,7 @@</span><br><span> #include <osmocom/gsm/protocol/gsm_04_80.h></span><br><span> </span><br><span> #include <string.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <errno.h></span><br><span> </span><br><span> static inline unsigned char *msgb_wrap_with_TL(struct msgb *msgb, uint8_t tag)</span><br><span> {</span><br><span>@@ -214,6 +215,93 @@</span><br><span>                                   uint16_t length,</span><br><span>                                     struct ss_request *req);</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! Get pointer to the IE of a given type</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in]  hdr      Pointer to the message starting from header</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in]  msg_len  Length of the whole message + header</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[out] ie       External pointer to be set</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[out] ie_len   External IE length variable</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in]  ie_tag   Tag value of the required IE</span><br><span style="color: hsl(120, 100%, 40%);">+ * \returns 0 in case of success, otherwise -ERRNO</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This function iterates over existing IEs within a given</span><br><span style="color: hsl(120, 100%, 40%);">+ * message (depending on its type), and looks for the one with</span><br><span style="color: hsl(120, 100%, 40%);">+ * given \ref ie_tag value. If the IE is found, the external</span><br><span style="color: hsl(120, 100%, 40%);">+ * pointer pointed by \ref ie will be set to its value part</span><br><span style="color: hsl(120, 100%, 40%);">+ * (omitting TL), and \ref ie_len will be set to the length.</span><br><span style="color: hsl(120, 100%, 40%);">+ * Otherwise, e.g. in case of parsing error, both \ref ie</span><br><span style="color: hsl(120, 100%, 40%);">+ * and \ref ie_len are set to NULL and 0 respectively.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+int gsm0480_extract_ie_by_tag(const struct gsm48_hdr *hdr, uint16_t msg_len,</span><br><span style="color: hsl(120, 100%, 40%);">+                           uint8_t **ie, uint16_t *ie_len, uint8_t ie_tag)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      uint8_t pdisc, msg_type;</span><br><span style="color: hsl(120, 100%, 40%);">+      uint8_t *tlv, len;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  /* Init external variables */</span><br><span style="color: hsl(120, 100%, 40%);">+ *ie_len = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+  *ie = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Drop incomplete / corrupted messages */</span><br><span style="color: hsl(120, 100%, 40%);">+    if (msg_len < sizeof(*hdr))</span><br><span style="color: hsl(120, 100%, 40%);">+                return -EINVAL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     pdisc = gsm48_hdr_pdisc(hdr);</span><br><span style="color: hsl(120, 100%, 40%);">+ msg_type = gsm48_hdr_msg_type(hdr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Drop non-SS related messages */</span><br><span style="color: hsl(120, 100%, 40%);">+    if (pdisc != GSM48_PDISC_NC_SS)</span><br><span style="color: hsl(120, 100%, 40%);">+               return -EINVAL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     len = msg_len - sizeof(*hdr);</span><br><span style="color: hsl(120, 100%, 40%);">+ tlv = (uint8_t *) hdr->data;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     /* Parse a message depending on its type */</span><br><span style="color: hsl(120, 100%, 40%);">+   switch (msg_type) {</span><br><span style="color: hsl(120, 100%, 40%);">+   /* See table 2.5: RELEASE COMPLETE message content */</span><br><span style="color: hsl(120, 100%, 40%);">+ case GSM0480_MTYPE_RELEASE_COMPLETE:</span><br><span style="color: hsl(120, 100%, 40%);">+  /* See tables 2.3 and 2.4: REGISTER message content */</span><br><span style="color: hsl(120, 100%, 40%);">+        case GSM0480_MTYPE_REGISTER:</span><br><span style="color: hsl(120, 100%, 40%);">+          /* Iterate over TLV-based IEs */</span><br><span style="color: hsl(120, 100%, 40%);">+              while (len > 2) {</span><br><span style="color: hsl(120, 100%, 40%);">+                  if (tlv[0] == ie_tag) {</span><br><span style="color: hsl(120, 100%, 40%);">+                               *ie_len = tlv[1];</span><br><span style="color: hsl(120, 100%, 40%);">+                             *ie = tlv + 2;</span><br><span style="color: hsl(120, 100%, 40%);">+                                return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+                     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                   len -= tlv[1] + 2;</span><br><span style="color: hsl(120, 100%, 40%);">+                    tlv += tlv[1] + 2;</span><br><span style="color: hsl(120, 100%, 40%);">+                    continue;</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           /* The Facility IE is mandatory for REGISTER */</span><br><span style="color: hsl(120, 100%, 40%);">+               if (msg_type == GSM0480_MTYPE_REGISTER)</span><br><span style="color: hsl(120, 100%, 40%);">+                       if (ie_tag == GSM0480_IE_FACILITY)</span><br><span style="color: hsl(120, 100%, 40%);">+                            return -EINVAL;</span><br><span style="color: hsl(120, 100%, 40%);">+               break;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /* See table 2.2: FACILITY message content */</span><br><span style="color: hsl(120, 100%, 40%);">+ case GSM0480_MTYPE_FACILITY:</span><br><span style="color: hsl(120, 100%, 40%);">+          /* There is no other IEs */</span><br><span style="color: hsl(120, 100%, 40%);">+           if (ie_tag != GSM0480_IE_FACILITY)</span><br><span style="color: hsl(120, 100%, 40%);">+                    break;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+              /* Mandatory LV-based Facility IE */</span><br><span style="color: hsl(120, 100%, 40%);">+          if (len < 2)</span><br><span style="color: hsl(120, 100%, 40%);">+                       return -EINVAL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+             *ie_len = tlv[0];</span><br><span style="color: hsl(120, 100%, 40%);">+             *ie = tlv + 1;</span><br><span style="color: hsl(120, 100%, 40%);">+                return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   default:</span><br><span style="color: hsl(120, 100%, 40%);">+              /* Wrong message type, out of specs */</span><br><span style="color: hsl(120, 100%, 40%);">+                return -EINVAL;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /* Decode a mobile-originated USSD-request message */</span><br><span> int gsm0480_decode_ussd_request(const struct gsm48_hdr *hdr, uint16_t len,</span><br><span>                                struct ussd_request *req)</span><br><span>diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map</span><br><span>index 117cecf..4aaed46 100644</span><br><span>--- a/src/gsm/libosmogsm.map</span><br><span>+++ b/src/gsm/libosmogsm.map</span><br><span>@@ -89,6 +89,7 @@</span><br><span> gsm0480_create_ussd_resp;</span><br><span> gsm0480_create_ussd_notify;</span><br><span> gsm0480_create_ussd_release_complete;</span><br><span style="color: hsl(120, 100%, 40%);">+gsm0480_extract_ie_by_tag;</span><br><span> gsm0480_decode_ussd_request;</span><br><span> gsm0480_decode_ss_request;</span><br><span> gsm0480_wrap_facility;</span><br><span>diff --git a/tests/ussd/ussd_test.c b/tests/ussd/ussd_test.c</span><br><span>index 1f79063..23fd739 100644</span><br><span>--- a/tests/ussd/ussd_test.c</span><br><span>+++ b/tests/ussd/ussd_test.c</span><br><span>@@ -34,6 +34,17 @@</span><br><span>        0x01, 0x7f, 0x01, 0x00</span><br><span> };</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static const uint8_t ussd_facility[] = {</span><br><span style="color: hsl(120, 100%, 40%);">+    0x1b, 0x3a, 0x12, 0xa2, 0x10, 0x02, 0x01, 0x01,</span><br><span style="color: hsl(120, 100%, 40%);">+       0x30, 0x0b, 0x02, 0x01, 0x3c, 0x30, 0x06, 0x04,</span><br><span style="color: hsl(120, 100%, 40%);">+       0x01, 0x0f, 0x04, 0x01, 0x32</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static const uint8_t ussd_release[] = {</span><br><span style="color: hsl(120, 100%, 40%);">+   0x8b, 0x2a, 0x1c, 0x08, 0xa3, 0x06, 0x02, 0x01,</span><br><span style="color: hsl(120, 100%, 40%);">+       0x05, 0x02, 0x01, 0x24</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static const uint8_t interrogate_ss[] = {</span><br><span>    0x0b, 0x7b, 0x1c, 0x0d, 0xa1, 0x0b, 0x02, 0x01,</span><br><span>      0x03, 0x02, 0x01, 0x0e, 0x30, 0x03, 0x04, 0x01,</span><br><span>@@ -116,6 +127,67 @@</span><br><span>       }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static void test_extract_ie_by_tag(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       uint16_t ie_len;</span><br><span style="color: hsl(120, 100%, 40%);">+      uint8_t *ie;</span><br><span style="color: hsl(120, 100%, 40%);">+  int rc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     printf("[i] Testing gsm0480_extract_ie_by_tag()\n");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /* REGISTER message with Facility IE */</span><br><span style="color: hsl(120, 100%, 40%);">+       rc = gsm0480_extract_ie_by_tag((struct gsm48_hdr *) ussd_request,</span><br><span style="color: hsl(120, 100%, 40%);">+             sizeof(ussd_request), &ie, &ie_len, GSM0480_IE_FACILITY);</span><br><span style="color: hsl(120, 100%, 40%);">+     OSMO_ASSERT(rc == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ OSMO_ASSERT(ie != NULL && ie_len > 0);</span><br><span style="color: hsl(120, 100%, 40%);">+     printf("[?] REGISTER message with Facility IE "</span><br><span style="color: hsl(120, 100%, 40%);">+             "(len=%u): %s\n", ie_len, osmo_hexdump(ie, ie_len));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /* REGISTER message with SS version IE */</span><br><span style="color: hsl(120, 100%, 40%);">+     rc = gsm0480_extract_ie_by_tag((struct gsm48_hdr *) ussd_request,</span><br><span style="color: hsl(120, 100%, 40%);">+             sizeof(ussd_request), &ie, &ie_len, GSM0480_IE_SS_VERSION);</span><br><span style="color: hsl(120, 100%, 40%);">+   OSMO_ASSERT(rc == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ OSMO_ASSERT(ie != NULL && ie_len > 0);</span><br><span style="color: hsl(120, 100%, 40%);">+     printf("[?] REGISTER message with SS version IE "</span><br><span style="color: hsl(120, 100%, 40%);">+           "(len=%u): %s\n", ie_len, osmo_hexdump(ie, ie_len));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /* REGISTER message with unknown IE */</span><br><span style="color: hsl(120, 100%, 40%);">+        rc = gsm0480_extract_ie_by_tag((struct gsm48_hdr *) ussd_request,</span><br><span style="color: hsl(120, 100%, 40%);">+             sizeof(ussd_request), &ie, &ie_len, 0xff);</span><br><span style="color: hsl(120, 100%, 40%);">+    OSMO_ASSERT(rc == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ OSMO_ASSERT(ie == NULL && ie_len == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     /* FACILITY message with Facility IE */</span><br><span style="color: hsl(120, 100%, 40%);">+       rc = gsm0480_extract_ie_by_tag((struct gsm48_hdr *) ussd_facility,</span><br><span style="color: hsl(120, 100%, 40%);">+            sizeof(ussd_facility), &ie, &ie_len, GSM0480_IE_FACILITY);</span><br><span style="color: hsl(120, 100%, 40%);">+    OSMO_ASSERT(rc == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ OSMO_ASSERT(ie != NULL && ie_len > 0);</span><br><span style="color: hsl(120, 100%, 40%);">+     printf("[?] FACILITY message with Facility IE "</span><br><span style="color: hsl(120, 100%, 40%);">+             "(len=%u): %s\n", ie_len, osmo_hexdump(ie, ie_len));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /* FACILITY message with unknown IE */</span><br><span style="color: hsl(120, 100%, 40%);">+        rc = gsm0480_extract_ie_by_tag((struct gsm48_hdr *) ussd_facility,</span><br><span style="color: hsl(120, 100%, 40%);">+            sizeof(ussd_facility), &ie, &ie_len, 0xff);</span><br><span style="color: hsl(120, 100%, 40%);">+   OSMO_ASSERT(rc == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ OSMO_ASSERT(ie == NULL && ie_len == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     /* RELEASE COMPLETE message with Facility IE */</span><br><span style="color: hsl(120, 100%, 40%);">+       rc = gsm0480_extract_ie_by_tag((struct gsm48_hdr *) ussd_release,</span><br><span style="color: hsl(120, 100%, 40%);">+             sizeof(ussd_release), &ie, &ie_len, GSM0480_IE_FACILITY);</span><br><span style="color: hsl(120, 100%, 40%);">+     OSMO_ASSERT(rc == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ OSMO_ASSERT(ie != NULL && ie_len > 0);</span><br><span style="color: hsl(120, 100%, 40%);">+     printf("[?] RELEASE COMPLETE message with Facility IE "</span><br><span style="color: hsl(120, 100%, 40%);">+             "(len=%u): %s\n", ie_len, osmo_hexdump(ie, ie_len));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /* RELEASE COMPLETE message without Facility IE */</span><br><span style="color: hsl(120, 100%, 40%);">+    rc = gsm0480_extract_ie_by_tag((struct gsm48_hdr *) ussd_release,</span><br><span style="color: hsl(120, 100%, 40%);">+             sizeof(struct gsm48_hdr), &ie, &ie_len, GSM0480_IE_FACILITY);</span><br><span style="color: hsl(120, 100%, 40%);">+ OSMO_ASSERT(rc == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ OSMO_ASSERT(ie == NULL && ie_len == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     printf("\n");</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> int main(int argc, char **argv)</span><br><span> {</span><br><span>         struct ss_request req;</span><br><span>@@ -126,6 +198,9 @@</span><br><span> </span><br><span>     osmo_init_logging2(ctx, &info);</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+       /* Test gsm0480_extract_ie_by_tag() */</span><br><span style="color: hsl(120, 100%, 40%);">+        test_extract_ie_by_tag();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>  memset(&req, 0, sizeof(req));</span><br><span>    gsm0480_decode_ss_request((struct gsm48_hdr *) ussd_request,</span><br><span>                 sizeof(ussd_request), &req);</span><br><span>diff --git a/tests/ussd/ussd_test.ok b/tests/ussd/ussd_test.ok</span><br><span>index aff383e..8fa4348 100644</span><br><span>--- a/tests/ussd/ussd_test.ok</span><br><span>+++ b/tests/ussd/ussd_test.ok</span><br><span>@@ -1,3 +1,9 @@</span><br><span style="color: hsl(120, 100%, 40%);">+[i] Testing gsm0480_extract_ie_by_tag()</span><br><span style="color: hsl(120, 100%, 40%);">+[?] REGISTER message with Facility IE (len=21): a1 13 02 01 03 02 01 3b 30 0b 04 01 0f 04 06 2a d5 4c 16 1b 01 </span><br><span style="color: hsl(120, 100%, 40%);">+[?] REGISTER message with SS version IE (len=1): 00 </span><br><span style="color: hsl(120, 100%, 40%);">+[?] FACILITY message with Facility IE (len=18): a2 10 02 01 01 30 0b 02 01 3c 30 06 04 01 0f 04 01 32 </span><br><span style="color: hsl(120, 100%, 40%);">+[?] RELEASE COMPLETE message with Facility IE (len=8): a3 06 02 01 05 02 01 24 </span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> Tested if it still works. Text was: **321#</span><br><span> interrogateSS CFU text..'' code 33</span><br><span> Testing parsing a USSD request and truncated versions</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/9528">change 9528</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/9528"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: libosmocore </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: I3989d061903352473305f80712f1a1560d05df3d </div>
<div style="display:none"> Gerrit-Change-Number: 9528 </div>
<div style="display:none"> Gerrit-PatchSet: 3 </div>
<div style="display:none"> Gerrit-Owner: Vadim Yanitskiy <axilirator@gmail.com> </div>
<div style="display:none"> Gerrit-Reviewer: Harald Welte <laforge@gnumonks.org> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins Builder </div>