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

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">gprs_gmm: introduce a GMM Attach Request FSM<br><br>The old GMM Attach Request handling used a recursive function<br>which can not handle certain states and is quite complex and hard to<br>extend.<br><br>The new FSM handles such request in a FSM and can be called multiple<br>times.<br><br>Change-Id: I58b9c17be9776a03bb2a5b21e99135cfefc8c912<br>---<br>M include/osmocom/sgsn/Makefile.am<br>M include/osmocom/sgsn/gprs_gmm.h<br>A include/osmocom/sgsn/gprs_gmm_attach.h<br>M include/osmocom/sgsn/gprs_sgsn.h<br>M src/gprs/Makefile.am<br>M src/gprs/gprs_gmm.c<br>A src/gprs/gprs_gmm_attach.c<br>M src/gprs/gprs_sgsn.c<br>M tests/sgsn/Makefile.am<br>9 files changed, 494 insertions(+), 9 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/osmocom/sgsn/Makefile.am b/include/osmocom/sgsn/Makefile.am</span><br><span>index 269cebc..3b563c4 100644</span><br><span>--- a/include/osmocom/sgsn/Makefile.am</span><br><span>+++ b/include/osmocom/sgsn/Makefile.am</span><br><span>@@ -5,6 +5,7 @@</span><br><span>    gb_proxy.h \</span><br><span>         gprs_gb_parse.h \</span><br><span>    gprs_gmm.h \</span><br><span style="color: hsl(120, 100%, 40%);">+  gprs_gmm_attach.h \</span><br><span>  gprs_llc.h \</span><br><span>         gprs_llc_xid.h \</span><br><span>     gprs_sgsn.h \</span><br><span>diff --git a/include/osmocom/sgsn/gprs_gmm.h b/include/osmocom/sgsn/gprs_gmm.h</span><br><span>index d12eaf9..ffcebd3 100644</span><br><span>--- a/include/osmocom/sgsn/gprs_gmm.h</span><br><span>+++ b/include/osmocom/sgsn/gprs_gmm.h</span><br><span>@@ -40,6 +40,8 @@</span><br><span>                           uint8_t gmm_cause);</span><br><span> int gsm48_tx_gmm_att_ack(struct sgsn_mm_ctx *mm);</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+int gprs_gmm_attach_req_ies(struct msgb *a, struct msgb *b);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /* TODO: move extract_subscr_* when gsm48_gmm_authorize() got removed */</span><br><span> void extract_subscr_msisdn(struct sgsn_mm_ctx *ctx);</span><br><span> void extract_subscr_hlr(struct sgsn_mm_ctx *ctx);</span><br><span>diff --git a/include/osmocom/sgsn/gprs_gmm_attach.h b/include/osmocom/sgsn/gprs_gmm_attach.h</span><br><span>new file mode 100644</span><br><span>index 0000000..22fbd6f</span><br><span>--- /dev/null</span><br><span>+++ b/include/osmocom/sgsn/gprs_gmm_attach.h</span><br><span>@@ -0,0 +1,37 @@</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef GPRS_GMM_ATTACH_H</span><br><span style="color: hsl(120, 100%, 40%);">+#define GPRS_GMM_ATTACH_H</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/core/fsm.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct sgsn_mm_ctx;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+enum gmm_attach_req_fsm_states {</span><br><span style="color: hsl(120, 100%, 40%);">+        ST_INIT,</span><br><span style="color: hsl(120, 100%, 40%);">+      ST_IDENTIY,</span><br><span style="color: hsl(120, 100%, 40%);">+   ST_RETRIEVE_AUTH,</span><br><span style="color: hsl(120, 100%, 40%);">+     ST_AUTH,</span><br><span style="color: hsl(120, 100%, 40%);">+      ST_ASK_VLR,</span><br><span style="color: hsl(120, 100%, 40%);">+   ST_ACCEPT,</span><br><span style="color: hsl(120, 100%, 40%);">+    ST_REJECT</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%);">+enum gmm_attach_req_fsm_events {</span><br><span style="color: hsl(120, 100%, 40%);">+     E_ATTACH_REQ_RECV,</span><br><span style="color: hsl(120, 100%, 40%);">+    E_IDEN_RESP_RECV,</span><br><span style="color: hsl(120, 100%, 40%);">+     E_AUTH_RESP_RECV_SUCCESS,</span><br><span style="color: hsl(120, 100%, 40%);">+     E_AUTH_RESP_RECV_RESYNC,</span><br><span style="color: hsl(120, 100%, 40%);">+      E_ATTACH_ACCEPTED,</span><br><span style="color: hsl(120, 100%, 40%);">+    E_ATTACH_ACCEPT_SENT,</span><br><span style="color: hsl(120, 100%, 40%);">+ E_ATTACH_COMPLETE_RECV,</span><br><span style="color: hsl(120, 100%, 40%);">+       E_REJECT,</span><br><span style="color: hsl(120, 100%, 40%);">+     E_VLR_ANSWERED,</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%);">+#define GMM_DISCARD_MS_WITHOUT_REJECT -1</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+extern const struct value_string gmm_attach_req_fsm_event_names[];</span><br><span style="color: hsl(120, 100%, 40%);">+extern struct osmo_fsm gmm_attach_req_fsm;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+void gmm_att_req_free(struct sgsn_mm_ctx *mm);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#endif // GPRS_GMM_ATTACH_H</span><br><span>diff --git a/include/osmocom/sgsn/gprs_sgsn.h b/include/osmocom/sgsn/gprs_sgsn.h</span><br><span>index 6f16dc7..a5ca959 100644</span><br><span>--- a/include/osmocom/sgsn/gprs_sgsn.h</span><br><span>+++ b/include/osmocom/sgsn/gprs_sgsn.h</span><br><span>@@ -4,6 +4,7 @@</span><br><span> #include <stdint.h></span><br><span> #include <netinet/in.h></span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/core/fsm.h></span><br><span> #include <osmocom/core/timer.h></span><br><span> </span><br><span> #include <osmocom/gsm/gsm48.h></span><br><span>@@ -168,6 +169,15 @@</span><br><span>                 struct ranap_ue_conn_ctx        *ue_ctx;</span><br><span>             struct service_info     service;</span><br><span>     } iu;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct {</span><br><span style="color: hsl(120, 100%, 40%);">+              struct osmo_fsm_inst *fsm;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+          /* when a second attach req arrives while in this procedure,</span><br><span style="color: hsl(120, 100%, 40%);">+           * the fsm needs to compare it against old to decide what to do */</span><br><span style="color: hsl(120, 100%, 40%);">+            struct msgb *attach_req;</span><br><span style="color: hsl(120, 100%, 40%);">+              uint32_t id_type;</span><br><span style="color: hsl(120, 100%, 40%);">+             bool auth_reattempt;</span><br><span style="color: hsl(120, 100%, 40%);">+  } gmm_att_req;</span><br><span>       /* VLR number */</span><br><span>     uint32_t                new_sgsn_addr;</span><br><span>       /* Authentication Triplet */</span><br><span>diff --git a/src/gprs/Makefile.am b/src/gprs/Makefile.am</span><br><span>index 46d94d4..0f7c5fa 100644</span><br><span>--- a/src/gprs/Makefile.am</span><br><span>+++ b/src/gprs/Makefile.am</span><br><span>@@ -59,6 +59,7 @@</span><br><span>        $(NULL)</span><br><span> </span><br><span> osmo_sgsn_SOURCES = \</span><br><span style="color: hsl(120, 100%, 40%);">+  gprs_gmm_attach.c \</span><br><span>  gprs_gmm.c \</span><br><span>         gprs_sgsn.c \</span><br><span>        gprs_sndcp.c \</span><br><span>diff --git a/src/gprs/gprs_gmm.c b/src/gprs/gprs_gmm.c</span><br><span>index bea63dc..a363c70 100644</span><br><span>--- a/src/gprs/gprs_gmm.c</span><br><span>+++ b/src/gprs/gprs_gmm.c</span><br><span>@@ -59,6 +59,7 @@</span><br><span> #include <osmocom/sgsn/gprs_utils.h></span><br><span> #include <osmocom/sgsn/gprs_subscriber.h></span><br><span> #include <osmocom/sgsn/sgsn.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/sgsn/gprs_gmm_attach.h></span><br><span> #include <osmocom/sgsn/signal.h></span><br><span> #include <osmocom/sgsn/gprs_sndcp.h></span><br><span> </span><br><span>@@ -791,7 +792,7 @@</span><br><span>      /* FIXME: enable LLC cipheirng */</span><br><span> </span><br><span>        /* Check if we can let the mobile station enter */</span><br><span style="color: hsl(0, 100%, 40%);">-      return gsm48_gmm_authorize(ctx);</span><br><span style="color: hsl(120, 100%, 40%);">+      return osmo_fsm_inst_dispatch(ctx->gmm_att_req.fsm, E_AUTH_RESP_RECV_SUCCESS, NULL);</span><br><span> }</span><br><span> </span><br><span> /* Section 9.4.10: Authentication and Ciphering Failure */</span><br><span>@@ -836,7 +837,7 @@</span><br><span>                 rc = gprs_subscr_request_auth_info(ctx, auts,</span><br><span>                                                   ctx->auth_triplet.vec.rand);</span><br><span>           if (!rc)</span><br><span style="color: hsl(0, 100%, 40%);">-                        return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+                     return osmo_fsm_inst_dispatch(ctx->gmm_att_req.fsm, E_AUTH_RESP_RECV_RESYNC, NULL);</span><br><span>               /* on error, fall through to send a reject */</span><br><span>                LOGMMCTXP(LOGL_ERROR, ctx,</span><br><span>                     "Sending AUTS to HLR failed (rc = %d)\n", rc);</span><br><span>@@ -1107,7 +1108,10 @@</span><br><span> {</span><br><span>     ctx->sec_ctx = OSMO_AUTH_TYPE_NONE;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-      gsm48_gmm_authorize(ctx);</span><br><span style="color: hsl(120, 100%, 40%);">+     if (ctx->gmm_att_req.fsm->state != ST_INIT)</span><br><span style="color: hsl(120, 100%, 40%);">+             osmo_fsm_inst_dispatch(ctx->gmm_att_req.fsm, E_VLR_ANSWERED, (void *) 0);</span><br><span style="color: hsl(120, 100%, 40%);">+  else</span><br><span style="color: hsl(120, 100%, 40%);">+          gsm48_gmm_authorize(ctx);</span><br><span> }</span><br><span> </span><br><span> void gsm0408_gprs_access_granted(struct sgsn_mm_ctx *ctx)</span><br><span>@@ -1118,7 +1122,8 @@</span><br><span>                   "Authorized, continuing procedure, IMSI=%s\n",</span><br><span>                     ctx->imsi);</span><br><span>          /* Continue with the authorization */</span><br><span style="color: hsl(0, 100%, 40%);">-           gsm48_gmm_authorize(ctx);</span><br><span style="color: hsl(120, 100%, 40%);">+             if (ctx->gmm_att_req.fsm->state != ST_INIT)</span><br><span style="color: hsl(120, 100%, 40%);">+                     osmo_fsm_inst_dispatch(ctx->gmm_att_req.fsm, E_VLR_ANSWERED, (void *) 0);</span><br><span>                 break;</span><br><span>       default:</span><br><span>             LOGMMCTXP(LOGL_INFO, ctx,</span><br><span>@@ -1139,8 +1144,8 @@</span><br><span>                      "with cause '%s' (%d)\n",</span><br><span>                          get_value_string(gsm48_gmm_cause_names, gmm_cause),</span><br><span>                          gmm_cause);</span><br><span style="color: hsl(0, 100%, 40%);">-           gsm48_tx_gmm_att_rej(ctx, gmm_cause);</span><br><span style="color: hsl(0, 100%, 40%);">-           mm_ctx_cleanup_free(ctx, "GPRS ATTACH REJECT");</span><br><span style="color: hsl(120, 100%, 40%);">+             if (ctx->gmm_att_req.fsm->state != ST_INIT)</span><br><span style="color: hsl(120, 100%, 40%);">+                     osmo_fsm_inst_dispatch(ctx->gmm_att_req.fsm, E_REJECT, (void *) (long) gmm_cause);</span><br><span>                break;</span><br><span>       case GMM_REGISTERED_NORMAL:</span><br><span>  case GMM_REGISTERED_SUSPENDED:</span><br><span>@@ -1183,6 +1188,7 @@</span><br><span> {</span><br><span>  struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);</span><br><span>  uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;</span><br><span style="color: hsl(120, 100%, 40%);">+      long mi_typel = mi_type;</span><br><span>     char mi_string[GSM48_MI_SIZE];</span><br><span> </span><br><span>   gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);</span><br><span>@@ -1235,7 +1241,7 @@</span><br><span>       }</span><br><span> </span><br><span>        /* Check if we can let the mobile station enter */</span><br><span style="color: hsl(0, 100%, 40%);">-      return gsm48_gmm_authorize(ctx);</span><br><span style="color: hsl(120, 100%, 40%);">+      return osmo_fsm_inst_dispatch(ctx->gmm_att_req.fsm, E_IDEN_RESP_RECV, (void *)mi_typel);</span><br><span> }</span><br><span> </span><br><span> /* Allocate a new P-TMSI and change context state */</span><br><span>@@ -1425,8 +1431,8 @@</span><br><span>                 gprs_llgmm_assign(ctx->gb.llme, ctx->gb.tlli, ctx->gb.tlli_new);</span><br><span>    }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   ctx->pending_req = GSM48_MT_GMM_ATTACH_REQ;</span><br><span style="color: hsl(0, 100%, 40%);">-  return gsm48_gmm_authorize(ctx);</span><br><span style="color: hsl(120, 100%, 40%);">+      osmo_fsm_inst_dispatch(ctx->gmm_att_req.fsm, E_ATTACH_REQ_RECV, msg);</span><br><span style="color: hsl(120, 100%, 40%);">+      return 0;</span><br><span> </span><br><span> err_inval:</span><br><span>  LOGPC(DMM, LOGL_INFO, "\n");</span><br><span>@@ -1447,6 +1453,28 @@</span><br><span> </span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* Checks if two attach request contain the IEs and IE values</span><br><span style="color: hsl(120, 100%, 40%);">+ * return 0 if equal</span><br><span style="color: hsl(120, 100%, 40%);">+ * return -1 if error</span><br><span style="color: hsl(120, 100%, 40%);">+ * return 1 if unequal</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Only do a simple memcmp for now.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+int gprs_gmm_attach_req_ies(struct msgb *a, struct msgb *b)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      struct gsm48_hdr *gh_a = (struct gsm48_hdr *) msgb_gmmh(a);</span><br><span style="color: hsl(120, 100%, 40%);">+   struct gsm48_hdr *gh_b = (struct gsm48_hdr *) msgb_gmmh(b);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define GMM_ATTACH_REQ_LEN 26</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        /* there is the LLC FCS behind */</span><br><span style="color: hsl(120, 100%, 40%);">+     if (msgb_l3len(a) < GMM_ATTACH_REQ_LEN || msgb_l3len(b) < GMM_ATTACH_REQ_LEN)</span><br><span style="color: hsl(120, 100%, 40%);">+           return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  return !!memcmp(gh_a, gh_b, GMM_ATTACH_REQ_LEN);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /* Section 4.7.4.1 / 9.4.5.2 MO Detach request */</span><br><span> static int gsm48_rx_gmm_det_req(struct sgsn_mm_ctx *ctx, struct msgb *msg)</span><br><span> {</span><br><span>@@ -2023,6 +2051,7 @@</span><br><span>                mmctx_set_mm_state(mmctx, MM_READY);</span><br><span>                 rc = 0;</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+           osmo_fsm_inst_dispatch(mmctx->gmm_att_req.fsm, E_ATTACH_COMPLETE_RECV, 0);</span><br><span>                memset(&sig_data, 0, sizeof(sig_data));</span><br><span>          sig_data.mm = mmctx;</span><br><span>                 osmo_signal_dispatch(SS_SGSN, S_SGSN_ATTACH, &sig_data);</span><br><span>diff --git a/src/gprs/gprs_gmm_attach.c b/src/gprs/gprs_gmm_attach.c</span><br><span>new file mode 100644</span><br><span>index 0000000..272fec7</span><br><span>--- /dev/null</span><br><span>+++ b/src/gprs/gprs_gmm_attach.c</span><br><span>@@ -0,0 +1,398 @@</span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/sgsn/gprs_gmm_attach.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/gsm/protocol/gsm_04_08_gprs.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/sgsn/debug.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/sgsn/gprs_gmm.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/sgsn/sgsn.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define X(s) (1 << (s))</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int require_identity_imei = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+static int require_auth = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void st_init(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  struct sgsn_mm_ctx *ctx = fi->priv;</span><br><span style="color: hsl(120, 100%, 40%);">+        struct msgb *attach_req = data;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     /* we can run st_init multiple times */</span><br><span style="color: hsl(120, 100%, 40%);">+       if (ctx->gmm_att_req.attach_req)</span><br><span style="color: hsl(120, 100%, 40%);">+           msgb_free(ctx->gmm_att_req.attach_req);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  ctx->gmm_att_req.attach_req = msgb_copy(attach_req, "Attach Request");</span><br><span style="color: hsl(120, 100%, 40%);">+   ctx->auth_state = SGSN_AUTH_UNKNOWN;</span><br><span style="color: hsl(120, 100%, 40%);">+       ctx->gmm_att_req.auth_reattempt = 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%);">+     * TODO: remove pending_req as soon the sgsn_auth code doesn't depend</span><br><span style="color: hsl(120, 100%, 40%);">+      * on it.</span><br><span style="color: hsl(120, 100%, 40%);">+      * pending_req must be set, even this fsm doesn't use it, because</span><br><span style="color: hsl(120, 100%, 40%);">+  * the sgsn_auth code is using this too</span><br><span style="color: hsl(120, 100%, 40%);">+        */</span><br><span style="color: hsl(120, 100%, 40%);">+   ctx->pending_req = GSM48_MT_GMM_ATTACH_REQ;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      if (require_identity_imei) {</span><br><span style="color: hsl(120, 100%, 40%);">+          ctx->gmm_att_req.id_type = GSM_MI_TYPE_IMEI;</span><br><span style="color: hsl(120, 100%, 40%);">+               osmo_fsm_inst_state_chg(fi, ST_IDENTIY, sgsn->cfg.timers.T3370, 3370);</span><br><span style="color: hsl(120, 100%, 40%);">+     } else if (!strlen(ctx->imsi)) {</span><br><span style="color: hsl(120, 100%, 40%);">+           ctx->gmm_att_req.id_type = GSM_MI_TYPE_IMSI;</span><br><span style="color: hsl(120, 100%, 40%);">+               osmo_fsm_inst_state_chg(fi, ST_IDENTIY, sgsn->cfg.timers.T3370, 3370);</span><br><span style="color: hsl(120, 100%, 40%);">+     } else if (require_auth)</span><br><span style="color: hsl(120, 100%, 40%);">+              osmo_fsm_inst_state_chg(fi, ST_AUTH, sgsn->cfg.timers.T3360, 3360);</span><br><span style="color: hsl(120, 100%, 40%);">+        else</span><br><span style="color: hsl(120, 100%, 40%);">+          osmo_fsm_inst_state_chg(fi, ST_ACCEPT, sgsn->cfg.timers.T3350, 3350);</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 void st_identity_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     struct sgsn_mm_ctx *ctx = fi->priv;</span><br><span style="color: hsl(120, 100%, 40%);">+        int ret = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        ctx->num_T_exp = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      switch (ctx->gmm_att_req.id_type) {</span><br><span style="color: hsl(120, 100%, 40%);">+        case GSM_MI_TYPE_IMEI:</span><br><span style="color: hsl(120, 100%, 40%);">+        case GSM_MI_TYPE_IMSI:</span><br><span style="color: hsl(120, 100%, 40%);">+                break;</span><br><span style="color: hsl(120, 100%, 40%);">+        default:</span><br><span style="color: hsl(120, 100%, 40%);">+              /* TODO logging */</span><br><span style="color: hsl(120, 100%, 40%);">+            osmo_fsm_inst_dispatch(fi, E_REJECT, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+           return;</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%);">+   ctx->t3370_id_type = ctx->gmm_att_req.id_type;</span><br><span style="color: hsl(120, 100%, 40%);">+  ret = gsm48_tx_gmm_id_req(ctx, ctx->gmm_att_req.id_type);</span><br><span style="color: hsl(120, 100%, 40%);">+  if (ret < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+             LOGPFSM(fi, "Can not send tx_gmm_id %d.\n", ret);</span><br><span style="color: hsl(120, 100%, 40%);">+           osmo_fsm_inst_dispatch(fi, E_REJECT, NULL);</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%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void st_identity(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      struct sgsn_mm_ctx *ctx = fi->priv;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      OSMO_ASSERT(event == E_IDEN_RESP_RECV);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     /* check if we received a identity response */</span><br><span style="color: hsl(120, 100%, 40%);">+        long type = (long) data;</span><br><span style="color: hsl(120, 100%, 40%);">+      switch (type) {</span><br><span style="color: hsl(120, 100%, 40%);">+       case GSM_MI_TYPE_IMEI:</span><br><span style="color: hsl(120, 100%, 40%);">+        case GSM_MI_TYPE_IMSI:</span><br><span style="color: hsl(120, 100%, 40%);">+                break;</span><br><span style="color: hsl(120, 100%, 40%);">+        default:</span><br><span style="color: hsl(120, 100%, 40%);">+              LOGMMCTXP(LOGL_ERROR, ctx, "Unknown mi type: 0x%lx, rejecting MS.\n", type);</span><br><span style="color: hsl(120, 100%, 40%);">+                osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_CAUSE_NET_FAIL);</span><br><span style="color: hsl(120, 100%, 40%);">+            return;</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%);">+   if (type != ctx->gmm_att_req.id_type) {</span><br><span style="color: hsl(120, 100%, 40%);">+            /* ignore wrong package */</span><br><span style="color: hsl(120, 100%, 40%);">+            /* TODO logging */</span><br><span style="color: hsl(120, 100%, 40%);">+            return;</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%);">+   if (type == GSM_MI_TYPE_IMEI && !strlen(ctx->imsi)) {</span><br><span style="color: hsl(120, 100%, 40%);">+              ctx->gmm_att_req.id_type = GSM_MI_TYPE_IMSI;</span><br><span style="color: hsl(120, 100%, 40%);">+               osmo_fsm_inst_state_chg(fi, ST_IDENTIY, sgsn->cfg.timers.T3370, 3370);</span><br><span style="color: hsl(120, 100%, 40%);">+     } else if (require_auth)</span><br><span style="color: hsl(120, 100%, 40%);">+              osmo_fsm_inst_state_chg(fi, ST_AUTH, sgsn->cfg.timers.T3360, 3360);</span><br><span style="color: hsl(120, 100%, 40%);">+        else</span><br><span style="color: hsl(120, 100%, 40%);">+          osmo_fsm_inst_state_chg(fi, ST_ACCEPT, sgsn->cfg.timers.T3350, 3350);</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 void st_auth_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sgsn_mm_ctx *ctx = fi->priv;</span><br><span style="color: hsl(120, 100%, 40%);">+        enum sgsn_auth_state auth_state;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    ctx->num_T_exp = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /* TODO: remove this layer violation. Don't parse any auth_policy here</span><br><span style="color: hsl(120, 100%, 40%);">+     * The correct way would be to ask the SGSN is this mmctx has to be auth</span><br><span style="color: hsl(120, 100%, 40%);">+       * regardless of the state.</span><br><span style="color: hsl(120, 100%, 40%);">+    * Otherwise someone else could steal the TLLI and just use it without further</span><br><span style="color: hsl(120, 100%, 40%);">+         * auth.</span><br><span style="color: hsl(120, 100%, 40%);">+       */</span><br><span style="color: hsl(120, 100%, 40%);">+   if (sgsn->cfg.auth_policy != SGSN_AUTH_POLICY_REMOTE) {</span><br><span style="color: hsl(120, 100%, 40%);">+            /* we can "trust" sgsn_auth_state as long it's not remote */</span><br><span style="color: hsl(120, 100%, 40%);">+            auth_state = sgsn_auth_state(ctx);</span><br><span style="color: hsl(120, 100%, 40%);">+    } else {</span><br><span style="color: hsl(120, 100%, 40%);">+              auth_state = ctx->auth_state;</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%);">+   switch(auth_state) {</span><br><span style="color: hsl(120, 100%, 40%);">+  case SGSN_AUTH_UMTS_RESYNC: /* ask the vlr for a new vector to match the simcards seq */</span><br><span style="color: hsl(120, 100%, 40%);">+      case SGSN_AUTH_UNKNOWN: /* the SGSN doesn know this MS */</span><br><span style="color: hsl(120, 100%, 40%);">+             osmo_fsm_inst_state_chg(fi, ST_ASK_VLR, sgsn->cfg.timers.T3350, 3350);</span><br><span style="color: hsl(120, 100%, 40%);">+             break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case SGSN_AUTH_REJECTED:</span><br><span style="color: hsl(120, 100%, 40%);">+              /* TODO: correct GMM cause */</span><br><span style="color: hsl(120, 100%, 40%);">+         osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_CAUSE_GPRS_NOTALLOWED);</span><br><span style="color: hsl(120, 100%, 40%);">+             break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case SGSN_AUTH_ACCEPTED:</span><br><span style="color: hsl(120, 100%, 40%);">+              osmo_fsm_inst_state_chg(fi, ST_ACCEPT, sgsn->cfg.timers.T3350, 3350);</span><br><span style="color: hsl(120, 100%, 40%);">+              break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case SGSN_AUTH_AUTHENTICATE:</span><br><span style="color: hsl(120, 100%, 40%);">+          if (ctx->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) {</span><br><span style="color: hsl(120, 100%, 40%);">+                      /* invalid key material */</span><br><span style="color: hsl(120, 100%, 40%);">+                    osmo_fsm_inst_state_chg(fi, ST_ASK_VLR, sgsn->cfg.timers.T3350, 3350);</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%);">+           struct gsm_auth_tuple *at = &ctx->auth_triplet;</span><br><span style="color: hsl(120, 100%, 40%);">+                if (gsm48_tx_gmm_auth_ciph_req(ctx, &at->vec, at->key_seq,</span><br><span style="color: hsl(120, 100%, 40%);">+                                         false) < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       /* network failure */</span><br><span style="color: hsl(120, 100%, 40%);">+                 osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_CAUSE_NET_FAIL);</span><br><span style="color: hsl(120, 100%, 40%);">+            }</span><br><span style="color: hsl(120, 100%, 40%);">+             ctx->gmm_att_req.auth_reattempt++;</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%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void st_auth(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  struct sgsn_mm_ctx *ctx = fi->priv;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case E_AUTH_RESP_RECV_SUCCESS:</span><br><span style="color: hsl(120, 100%, 40%);">+                sgsn_auth_request(ctx);</span><br><span style="color: hsl(120, 100%, 40%);">+               osmo_fsm_inst_state_chg(fi, ST_ACCEPT, sgsn->cfg.timers.T3350, 3350);</span><br><span style="color: hsl(120, 100%, 40%);">+              break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case E_AUTH_RESP_RECV_RESYNC:</span><br><span style="color: hsl(120, 100%, 40%);">+         if (ctx->gmm_att_req.auth_reattempt <= 1)</span><br><span style="color: hsl(120, 100%, 40%);">+                       osmo_fsm_inst_state_chg(fi, ST_ASK_VLR, sgsn->cfg.timers.T3350, 3350);</span><br><span style="color: hsl(120, 100%, 40%);">+             else</span><br><span style="color: hsl(120, 100%, 40%);">+                  osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_CAUSE_SYNC_FAIL);</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%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void st_accept_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      struct sgsn_mm_ctx *ctx = fi->priv;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      ctx->num_T_exp = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /* TODO: remove pending_req as soon the sgsn_auth code doesn't depend on it */</span><br><span style="color: hsl(120, 100%, 40%);">+    ctx->pending_req = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+      gsm48_tx_gmm_att_ack(ctx);</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 void st_accept(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       struct sgsn_mm_ctx *ctx = fi->priv;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      switch(event) {</span><br><span style="color: hsl(120, 100%, 40%);">+       case E_ATTACH_COMPLETE_RECV:</span><br><span style="color: hsl(120, 100%, 40%);">+          /* TODO: #ifdef ! PTMSI_ALLOC is not supported */</span><br><span style="color: hsl(120, 100%, 40%);">+             extract_subscr_msisdn(ctx);</span><br><span style="color: hsl(120, 100%, 40%);">+           extract_subscr_hlr(ctx);</span><br><span style="color: hsl(120, 100%, 40%);">+              osmo_fsm_inst_state_chg(fi, ST_INIT, 0, 0);</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%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void st_reject(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        struct sgsn_mm_ctx *ctx = fi->priv;</span><br><span style="color: hsl(120, 100%, 40%);">+        long reject_cause = (long) data;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (reject_cause != GMM_DISCARD_MS_WITHOUT_REJECT)</span><br><span style="color: hsl(120, 100%, 40%);">+            gsm48_tx_gmm_att_rej(ctx, (uint8_t) reject_cause);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  sgsn_mm_ctx_cleanup_free(ctx);</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 void st_ask_vlr_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        struct sgsn_mm_ctx *ctx = fi->priv;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /* FIXME: remove this layer violation.</span><br><span style="color: hsl(120, 100%, 40%);">+         * The VLR should send the message to the HLR and not the rx function</span><br><span style="color: hsl(120, 100%, 40%);">+  * gsm48_rx_gmm_auth_ciph_fail. Because gmm_auth_ciph_fail already send a</span><br><span style="color: hsl(120, 100%, 40%);">+      * message to the HLR, we don't send here a request. */</span><br><span style="color: hsl(120, 100%, 40%);">+   if (ctx->auth_state == SGSN_AUTH_UMTS_RESYNC)</span><br><span style="color: hsl(120, 100%, 40%);">+              return;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     /* ask the auth layer for more data */</span><br><span style="color: hsl(120, 100%, 40%);">+        sgsn_auth_request(ctx);</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 void st_ask_vlr(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ switch(event) {</span><br><span style="color: hsl(120, 100%, 40%);">+       case E_VLR_ANSWERED:</span><br><span style="color: hsl(120, 100%, 40%);">+          osmo_fsm_inst_state_chg(fi, ST_AUTH, sgsn->cfg.timers.T3360, 3360);</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%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct osmo_fsm_state gmm_attach_req_fsm_states[] = {</span><br><span style="color: hsl(120, 100%, 40%);">+  /* default state for non-DTX and DTX when SPEECH is in progress */</span><br><span style="color: hsl(120, 100%, 40%);">+    [ST_INIT] = {</span><br><span style="color: hsl(120, 100%, 40%);">+         .in_event_mask = X(E_ATTACH_REQ_RECV),</span><br><span style="color: hsl(120, 100%, 40%);">+                .out_state_mask = X(ST_INIT) | X(ST_IDENTIY) | X(ST_AUTH) | X(ST_ACCEPT),</span><br><span style="color: hsl(120, 100%, 40%);">+             .name = "Init",</span><br><span style="color: hsl(120, 100%, 40%);">+             .action = st_init,</span><br><span style="color: hsl(120, 100%, 40%);">+    },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ST_ASK_VLR] = {</span><br><span style="color: hsl(120, 100%, 40%);">+              .in_event_mask = X(E_VLR_ANSWERED),</span><br><span style="color: hsl(120, 100%, 40%);">+           .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_REJECT),</span><br><span style="color: hsl(120, 100%, 40%);">+              .name = "AskVLR",</span><br><span style="color: hsl(120, 100%, 40%);">+           .onenter = st_ask_vlr_on_enter,</span><br><span style="color: hsl(120, 100%, 40%);">+               .action = st_ask_vlr,</span><br><span style="color: hsl(120, 100%, 40%);">+ },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ST_IDENTIY] = {</span><br><span style="color: hsl(120, 100%, 40%);">+              .in_event_mask = X(E_IDEN_RESP_RECV),</span><br><span style="color: hsl(120, 100%, 40%);">+         .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_IDENTIY) | X(ST_REJECT),</span><br><span style="color: hsl(120, 100%, 40%);">+              .onenter = st_identity_on_enter,</span><br><span style="color: hsl(120, 100%, 40%);">+              .name = "CheckIdentity",</span><br><span style="color: hsl(120, 100%, 40%);">+            .action = st_identity,</span><br><span style="color: hsl(120, 100%, 40%);">+        },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ST_AUTH] = {</span><br><span style="color: hsl(120, 100%, 40%);">+         .in_event_mask = X(E_AUTH_RESP_RECV_SUCCESS) | X(E_AUTH_RESP_RECV_RESYNC),</span><br><span style="color: hsl(120, 100%, 40%);">+            .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_ASK_VLR) | X(ST_REJECT),</span><br><span style="color: hsl(120, 100%, 40%);">+              .name = "Authenticate",</span><br><span style="color: hsl(120, 100%, 40%);">+             .onenter = st_auth_on_enter,</span><br><span style="color: hsl(120, 100%, 40%);">+          .action = st_auth,</span><br><span style="color: hsl(120, 100%, 40%);">+    },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ST_ACCEPT] = {</span><br><span style="color: hsl(120, 100%, 40%);">+               .in_event_mask = X(E_ATTACH_COMPLETE_RECV),</span><br><span style="color: hsl(120, 100%, 40%);">+           .out_state_mask = X(ST_INIT) | X(ST_REJECT),</span><br><span style="color: hsl(120, 100%, 40%);">+          .name = "WaitAttachComplete",</span><br><span style="color: hsl(120, 100%, 40%);">+               .onenter = st_accept_on_enter,</span><br><span style="color: hsl(120, 100%, 40%);">+                .action = st_accept,</span><br><span style="color: hsl(120, 100%, 40%);">+  },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ST_REJECT] = {</span><br><span style="color: hsl(120, 100%, 40%);">+               .in_event_mask = X(E_REJECT),</span><br><span style="color: hsl(120, 100%, 40%);">+         .out_state_mask = X(ST_INIT),</span><br><span style="color: hsl(120, 100%, 40%);">+         .name = "Reject",</span><br><span style="color: hsl(120, 100%, 40%);">+           .action = st_reject,</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%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+const struct value_string gmm_attach_req_fsm_event_names[] = {</span><br><span style="color: hsl(120, 100%, 40%);">+      { E_ATTACH_REQ_RECV,            "Received an attach request" },</span><br><span style="color: hsl(120, 100%, 40%);">+     { E_IDEN_RESP_RECV,             "Identity Request received" },</span><br><span style="color: hsl(120, 100%, 40%);">+      { E_AUTH_RESP_RECV_SUCCESS,     "Authentication Response received" },</span><br><span style="color: hsl(120, 100%, 40%);">+       { E_AUTH_RESP_RECV_RESYNC,      "Authentication Failure with resync received" },</span><br><span style="color: hsl(120, 100%, 40%);">+    { E_ATTACH_ACCEPTED,            "Attach accepted" },</span><br><span style="color: hsl(120, 100%, 40%);">+        { E_ATTACH_ACCEPT_SENT,         "Attach accept sent" },</span><br><span style="color: hsl(120, 100%, 40%);">+     { E_ATTACH_COMPLETE_RECV,       "Attach complete received." },</span><br><span style="color: hsl(120, 100%, 40%);">+      { E_REJECT,                     "Reject the MS"},</span><br><span style="color: hsl(120, 100%, 40%);">+   { E_VLR_ANSWERED,               "VLR answered"},</span><br><span style="color: hsl(120, 100%, 40%);">+    { 0,                            NULL }</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%);">+void gmm_attach_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct sgsn_mm_ctx *ctx = fi->priv;</span><br><span style="color: hsl(120, 100%, 40%);">+        struct msgb *new_attach_req = data;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case E_ATTACH_REQ_RECV:</span><br><span style="color: hsl(120, 100%, 40%);">+               switch (fi->state) {</span><br><span style="color: hsl(120, 100%, 40%);">+               case ST_INIT:</span><br><span style="color: hsl(120, 100%, 40%);">+         case ST_REJECT:</span><br><span style="color: hsl(120, 100%, 40%);">+                       st_init(fi, event, data);</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%);">+              case ST_ACCEPT:</span><br><span style="color: hsl(120, 100%, 40%);">+                       /* TODO: drop all state (e.g. PDP Ctx) and do this procedure */</span><br><span style="color: hsl(120, 100%, 40%);">+                       osmo_fsm_inst_state_chg(fi, ST_INIT, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+                   st_init(fi, event, data);</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%);">+              case ST_ASK_VLR:</span><br><span style="color: hsl(120, 100%, 40%);">+              case ST_AUTH:</span><br><span style="color: hsl(120, 100%, 40%);">+         case ST_IDENTIY:</span><br><span style="color: hsl(120, 100%, 40%);">+              case ST_RETRIEVE_AUTH:</span><br><span style="color: hsl(120, 100%, 40%);">+                        /* 04.08 4.7.3.1.6 d) Abnormal Case</span><br><span style="color: hsl(120, 100%, 40%);">+                    * Only do action if Req IEs differs. */</span><br><span style="color: hsl(120, 100%, 40%);">+                      if (ctx->gmm_att_req.attach_req &&</span><br><span style="color: hsl(120, 100%, 40%);">+                                 gprs_gmm_attach_req_ies(new_attach_req, ctx->gmm_att_req.attach_req)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                            osmo_fsm_inst_state_chg(fi, ST_INIT, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+                           st_init(fi, event, data);</span><br><span style="color: hsl(120, 100%, 40%);">+                     }</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%);">+             break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case E_REJECT:</span><br><span style="color: hsl(120, 100%, 40%);">+                if (fi->state != ST_REJECT)</span><br><span style="color: hsl(120, 100%, 40%);">+                        osmo_fsm_inst_state_chg(fi, ST_REJECT, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+         st_reject(fi, event, data);</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%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int gmm_attach_timer_cb(struct osmo_fsm_inst *fi)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  struct sgsn_mm_ctx *ctx = fi->priv;</span><br><span style="color: hsl(120, 100%, 40%);">+        struct gsm_auth_tuple *at = &ctx->auth_triplet;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      ctx->num_T_exp++;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        switch(fi->state) {</span><br><span style="color: hsl(120, 100%, 40%);">+        case ST_ASK_VLR:</span><br><span style="color: hsl(120, 100%, 40%);">+              /* TODO: replace T3350 by a better timer or it's own</span><br><span style="color: hsl(120, 100%, 40%);">+               * re-use T3350 - not defined by standard */</span><br><span style="color: hsl(120, 100%, 40%);">+          LOGMMCTXP(LOGL_ERROR, ctx, "HLR did not answer in time. Rejecting.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+             osmo_fsm_inst_dispatch(fi, E_REJECT,</span><br><span style="color: hsl(120, 100%, 40%);">+                                 (void *) GMM_CAUSE_NET_FAIL);</span><br><span style="color: hsl(120, 100%, 40%);">+          break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case ST_IDENTIY:</span><br><span style="color: hsl(120, 100%, 40%);">+              /* T3370 */</span><br><span style="color: hsl(120, 100%, 40%);">+           if (ctx->num_T_exp >= 5) {</span><br><span style="color: hsl(120, 100%, 40%);">+                      osmo_fsm_inst_dispatch(fi, E_REJECT,</span><br><span style="color: hsl(120, 100%, 40%);">+                                         (void *) GMM_CAUSE_MS_ID_NOT_DERIVED);</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%);">+             gsm48_tx_gmm_id_req(ctx, ctx->gmm_att_req.id_type);</span><br><span style="color: hsl(120, 100%, 40%);">+                osmo_timer_schedule(&fi->timer, sgsn->cfg.timers.T3370, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+         break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case ST_AUTH:</span><br><span style="color: hsl(120, 100%, 40%);">+         /* T3360 */</span><br><span style="color: hsl(120, 100%, 40%);">+           if (ctx->num_T_exp >= 5) {</span><br><span style="color: hsl(120, 100%, 40%);">+                      osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_DISCARD_MS_WITHOUT_REJECT);</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%);">+             gsm48_tx_gmm_auth_ciph_req(ctx, &at->vec, at->key_seq, false);</span><br><span style="color: hsl(120, 100%, 40%);">+              osmo_timer_schedule(&fi->timer, sgsn->cfg.timers.T3360, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+         break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case ST_ACCEPT:</span><br><span style="color: hsl(120, 100%, 40%);">+               /* T3350 */</span><br><span style="color: hsl(120, 100%, 40%);">+           if (ctx->num_T_exp >= 5) {</span><br><span style="color: hsl(120, 100%, 40%);">+                      osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_DISCARD_MS_WITHOUT_REJECT);</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%);">+             gsm48_tx_gmm_att_ack(ctx);</span><br><span style="color: hsl(120, 100%, 40%);">+            osmo_timer_schedule(&fi->timer, sgsn->cfg.timers.T3350, 0);</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%);">+</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%);">+struct osmo_fsm gmm_attach_req_fsm = {</span><br><span style="color: hsl(120, 100%, 40%);">+        .name = "GMM_ATTACH_REQ_FSM",</span><br><span style="color: hsl(120, 100%, 40%);">+       .states = gmm_attach_req_fsm_states,</span><br><span style="color: hsl(120, 100%, 40%);">+  .num_states = ARRAY_SIZE(gmm_attach_req_fsm_states),</span><br><span style="color: hsl(120, 100%, 40%);">+  .event_names = gmm_attach_req_fsm_event_names,</span><br><span style="color: hsl(120, 100%, 40%);">+        .allstate_event_mask = X(E_REJECT) | X(E_ATTACH_REQ_RECV),</span><br><span style="color: hsl(120, 100%, 40%);">+    .allstate_action = gmm_attach_allstate_action,</span><br><span style="color: hsl(120, 100%, 40%);">+        .log_subsys = DMM,</span><br><span style="color: hsl(120, 100%, 40%);">+    .timer_cb = gmm_attach_timer_cb,</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 __attribute__((constructor)) void gprs_gmm_fsm_init(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   osmo_fsm_register(&gmm_attach_req_fsm);</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%);">+void gmm_att_req_free(struct sgsn_mm_ctx *mm) {</span><br><span style="color: hsl(120, 100%, 40%);">+     if (mm->gmm_att_req.fsm)</span><br><span style="color: hsl(120, 100%, 40%);">+           osmo_fsm_inst_free(mm->gmm_att_req.fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (mm->gmm_att_req.attach_req)</span><br><span style="color: hsl(120, 100%, 40%);">+            msgb_free(mm->gmm_att_req.attach_req);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span>diff --git a/src/gprs/gprs_sgsn.c b/src/gprs/gprs_sgsn.c</span><br><span>index d8bca85..977ae48 100644</span><br><span>--- a/src/gprs/gprs_sgsn.c</span><br><span>+++ b/src/gprs/gprs_sgsn.c</span><br><span>@@ -40,6 +40,7 @@</span><br><span> #include <osmocom/sgsn/gprs_gmm.h></span><br><span> #include <osmocom/sgsn/gprs_utils.h></span><br><span> #include <osmocom/sgsn/signal.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/sgsn/gprs_gmm_attach.h></span><br><span> #include <osmocom/sgsn/gprs_llc.h></span><br><span> </span><br><span> #include <pdp.h></span><br><span>@@ -241,6 +242,7 @@</span><br><span>           talloc_free(ctx);</span><br><span>            return NULL;</span><br><span>         }</span><br><span style="color: hsl(120, 100%, 40%);">+     ctx->gmm_att_req.fsm = osmo_fsm_inst_alloc(&gmm_attach_req_fsm, ctx, ctx, LOGL_DEBUG, "gb_gmm_req");</span><br><span>        INIT_LLIST_HEAD(&ctx->pdp_list);</span><br><span> </span><br><span>  llist_add(&ctx->list, &sgsn_mm_ctxts);</span><br><span>@@ -273,6 +275,7 @@</span><br><span>              talloc_free(ctx);</span><br><span>            return NULL;</span><br><span>         }</span><br><span style="color: hsl(120, 100%, 40%);">+     ctx->gmm_att_req.fsm = osmo_fsm_inst_alloc(&gmm_attach_req_fsm, ctx, ctx, LOGL_DEBUG, "gb_gmm_req");</span><br><span> </span><br><span>    /* Need to get RAID from IU conn */</span><br><span>  ctx->ra = ctx->iu.ue_ctx->ra_id;</span><br><span>@@ -350,6 +353,9 @@</span><br><span>              gprs_subscr_put(subscr);</span><br><span>     }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ if (mm->gmm_att_req.fsm)</span><br><span style="color: hsl(120, 100%, 40%);">+           gmm_att_req_free(mm);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>      sgsn_mm_ctx_free(mm);</span><br><span>        mm = NULL;</span><br><span> </span><br><span>diff --git a/tests/sgsn/Makefile.am b/tests/sgsn/Makefile.am</span><br><span>index abcc357..0eb2100 100644</span><br><span>--- a/tests/sgsn/Makefile.am</span><br><span>+++ b/tests/sgsn/Makefile.am</span><br><span>@@ -45,6 +45,7 @@</span><br><span>      $(top_builddir)/src/gprs/gprs_llc.o \</span><br><span>        $(top_builddir)/src/gprs/crc24.o \</span><br><span>   $(top_builddir)/src/gprs/gprs_sndcp.o \</span><br><span style="color: hsl(120, 100%, 40%);">+       $(top_builddir)/src/gprs/gprs_gmm_attach.o \</span><br><span>         $(top_builddir)/src/gprs/gprs_gmm.o \</span><br><span>        $(top_builddir)/src/gprs/gprs_sgsn.o \</span><br><span>       $(top_builddir)/src/gprs/sgsn_vty.o \</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/9257">change 9257</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/9257"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: osmo-sgsn </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: I58b9c17be9776a03bb2a5b21e99135cfefc8c912 </div>
<div style="display:none"> Gerrit-Change-Number: 9257 </div>
<div style="display:none"> Gerrit-PatchSet: 17 </div>
<div style="display:none"> Gerrit-Owner: lynxis lazus <lynxis@fe80.eu> </div>
<div style="display:none"> Gerrit-Reviewer: Harald Welte <laforge@gnumonks.org> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins Builder (1000002) </div>
<div style="display:none"> Gerrit-Reviewer: lynxis lazus <lynxis@fe80.eu> </div>