<p>laforge has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/c/libosmo-sccp/+/19001">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">WIP: Fix race during fast re-establishment of inbound M3UA connections<br><br>When a client closes and instantaneously re-opens a SCTP socket for an<br>M3UA connection, there is a chance that both the "shutdwon event" (old<br>connection socket becomes readable for sctp event) and the "init event"<br>(listen-fd becomes readable) happen during the same scheduler interval /<br>select() cycle.  As there is no guaranteed order by which we call our<br>file descriptor callbacks, it means that we may end up processing<br>then new connection (accept) before we get the notification that the<br>old one is dead.<br><br>The fact that the fd number of the accept-fd is mostly lower than the fd<br>number of the individual per-client connection actually makes it likely<br>that the order is exactly the opposite of what would feel "logical".<br><br>As the ASP is identified by the tuple of (src-port, src-ip, dst-port, dst-ip),<br>both the old connection and the new connection map to the same ASP<br>object.  So we need to handle this situation gracefully:  If we get a<br>new connection for a tuple that we already [think we still] have one,<br>close the old one and use the new.<br><br>Change-Id: I9b3ae6dfcf6efeabb7fb6c33503d1d7924fec2fa<br>Closes: OS#4625<br>---<br>M src/osmo_ss7.c<br>1 file changed, 12 insertions(+), 1 deletion(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/libosmo-sccp refs/changes/01/19001/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/src/osmo_ss7.c b/src/osmo_ss7.c</span><br><span>index b7d69cb..9aeed9b 100644</span><br><span>--- a/src/osmo_ss7.c</span><br><span>+++ b/src/osmo_ss7.c</span><br><span>@@ -1832,6 +1832,17 @@</span><br><span>     if (asp) {</span><br><span>           LOGP(DLSS7, LOGL_INFO, "%s: matched connection to ASP %s\n",</span><br><span>                       sock_name, asp->cfg.name);</span><br><span style="color: hsl(120, 100%, 40%);">+         /* we need to check if we already have a socket associated, and close it.  Otherwise it might</span><br><span style="color: hsl(120, 100%, 40%);">+          * happen that both the listen-fd for this accept() and the old socket are marked 'readable'</span><br><span style="color: hsl(120, 100%, 40%);">+           * during the same scheduling interval, and we're processing them in the "wrong" order, i.e.</span><br><span style="color: hsl(120, 100%, 40%);">+             * we first see the accept of the new fd before we see the close on the old fd */</span><br><span style="color: hsl(120, 100%, 40%);">+             if (asp->server) {</span><br><span style="color: hsl(120, 100%, 40%);">+                 LOGPASP(asp, DLSS7, LOGL_FATAL, "accept of new connection from %s before old was closed "</span><br><span style="color: hsl(120, 100%, 40%);">+                           "-> close old one\n", sock_name);</span><br><span style="color: hsl(120, 100%, 40%);">+                        osmo_stream_srv_set_data(asp->server, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+                       osmo_stream_srv_destroy(asp->server);</span><br><span style="color: hsl(120, 100%, 40%);">+                      asp->server = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+                }</span><br><span>    } else {</span><br><span>             if (!oxs->cfg.accept_dyn_reg) {</span><br><span>                   LOGP(DLSS7, LOGL_NOTICE, "%s: %s connection without matching "</span><br><span>@@ -1870,13 +1881,13 @@</span><br><span>                   talloc_free(sock_name);</span><br><span>                      return -1;</span><br><span>           }</span><br><span style="color: hsl(120, 100%, 40%);">+             llist_add_tail(&asp->siblings, &oxs->asp_list);</span><br><span>        }</span><br><span> </span><br><span>        /* update the ASP reference back to the server over which the</span><br><span>         * connection came in */</span><br><span>     asp->server = srv;</span><br><span>        asp->xua_server = oxs;</span><br><span style="color: hsl(0, 100%, 40%);">-       llist_add_tail(&asp->siblings, &oxs->asp_list);</span><br><span>        /* update the ASP socket name */</span><br><span>     if (asp->sock_name)</span><br><span>               talloc_free(asp->sock_name);</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/libosmo-sccp/+/19001">change 19001</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/c/libosmo-sccp/+/19001"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: libosmo-sccp </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I9b3ae6dfcf6efeabb7fb6c33503d1d7924fec2fa </div>
<div style="display:none"> Gerrit-Change-Number: 19001 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>