<h3>Description</h3>
<p>Kamailio is using the wrong source port when relaying INVITE packets with t_relay(). Another interesting thing is that while doing this, Kamailio reports to Homer via HEPv2 that it sent the packet from port 4242, but a tcpdump on the server shows this to be false so maybe homer was just looking at the R-URI port?<br>
There is a somewhat random element to this, so it is very difficult to reproduce. I will provide the scenario which led to this problem.</p>
<h4>Reproduction</h4>
<p>This is the code block which is responsible for relaying INVITEs to phone from our PBX servers, it is also the only instance of port 5062 being inserted into a SIP message.</p>
<pre><code>if(is_method("INVITE"))
{
        if(lookup("location", "sip:$rU@location"))
        {
                remove_hf("Contact");
                append_hf("Contact: <sip:$sel(cfg_get.global.publicIP):5062>\r\n"); //A vendor's SIPIS server doesn't work on port 4242, does changing this port break SIP compliance?
                rtpproxy_manage("cow");
                t_relay();
                exit;
        }
}
</code></pre>
<ul>
<li>Kamailio listens for UDP+TCP on ports 5060,5062, and 4242.</li>
<li>Phone A registers to tcp:KamailioIP:4242 from tcp:CustomerIP:10042</li>
<li>Phone B registers to tcp:KamailioIP:4242 from tcp:CustomerIP:11042</li>
<li>These phones have been re-registering from and to the same ports for a long time without problems with the same REGISTER callID (same transaction so nothing about the connection has changed).</li>
<li>At this point we have TCP connections <code>tcp:CustomerIP:10042 -> tcp:KamailioIP:4242</code> and <code>tcp:CustomerIP:11042 -> tcp:KamailioIP:4242</code></li>
<li>Kamailio sends INVITEs from tcp:KamailioIP:4242 to the address of the registered phone with a contact header that tells the phones to send responses to tcp:KamailioIP:5062 (as seen in the above code block). This causes a new TCP connection to be made from the customer's router to Kamailio, just for this transaction.</li>
<li>All is well until phone A sends a response and the customer's router, by pure chance, decides to use port 11042 for the new TCP connection. <code>tcp:CustomerIP:11042 -> tcp:KamailioIP:5062</code></li>
<li>From this point on Kamailio begins to route INVITEs, that were meant for phone B, to phone A using this TCP connection <code>tcp:CustomerIP:11042 -> tcp:KamailioIP:5062</code> (source port 5062!) rather than the connection that phone B is actually registered to <code>tcp:CustomerIP:11042 -> tcp:KamailioIP:4242</code>.</li>
<li>The customer's router sends packets from this TCP socket to phone A because the connection identified by the Kamailio source port had been created by phone A.</li>
<li>Kamailio is still routing other types of packets to phone B from port 4242 like NOTIFY in response to a subscription and OPTIONs keep alives, but INVITEs (which are an entirely new SIP transaction) are being sent from the wrong source port.</li>
</ul>
<h3>Possible <del>Solutions</del> Problems</h3>
<p>I have the default tcp_accept_aliases=0, so ;alias in a Via header is not a problem. We are also not using force_send_socket.<br>
I've tried to figure out how Kamailio works, and I am wondering if this function might be adding the new TCP connection from phone A to port 5062 as an alias of the already existing TCP connection from phone B to port 4242.<br>
<a href="https://github.com/kamailio/kamailio/blob/cb7810c939da9c8f4385b530539487528ac8705d/src/core/tcp_main.c#L1287-L1300">https://github.com/kamailio/kamailio/blob/cb7810c939da9c8f4385b530539487528ac8705d/src/core/tcp_main.c#L1287-L1300</a></p>
<p>I also found these comments interesting.<br>
<a href="https://github.com/kamailio/kamailio/blob/52111974b4571e0562e8e731df80f48dbc504915/src/core/forward.c#L288-L290">https://github.com/kamailio/kamailio/blob/52111974b4571e0562e8e731df80f48dbc504915/src/core/forward.c#L288-L290</a><br>
<a href="https://github.com/kamailio/kamailio/blob/52111974b4571e0562e8e731df80f48dbc504915/src/core/forward.c#L76-L81">https://github.com/kamailio/kamailio/blob/52111974b4571e0562e8e731df80f48dbc504915/src/core/forward.c#L76-L81</a></p>
<h3>Additional Information</h3>
<ul>
<li><strong>Kamailio Version</strong> - output of <code>kamailio -v</code></li>
</ul>
<pre><code>version: kamailio 5.0.5 (x86_64/linux) 
flags: STATS: Off, USE_TCP, USE_TLS, USE_SCTP, TLS_HOOKS, DISABLE_NAGLE, USE_MCAST, DNS_IP_HACK, SHM_MEM, SHM_MMAP, PKG_MALLOC, Q_MALLOC, F_MALLOC, TLSF_MALLOC, DBG_SR_MEMORY, USE_FUTEX, FAST_LOCK-ADAPTIVE_WAIT, USE_DNS_CACHE, USE_DNS_FAILOVER, USE_NAPTR, USE_DST_BLACKLIST, HAVE_RESOLV_RES
ADAPTIVE_WAIT_LOOPS=1024, MAX_RECV_BUFFER_SIZE 262144, MAX_LISTEN 16, MAX_URI_SIZE 1024, BUF_SIZE 65535, DEFAULT PKG_SIZE 8MB
poll method support: poll, epoll_lt, epoll_et, sigio_rt, select.
id: unknown 
compiled on 04:38:13 Feb  2 2018 with gcc 4.8.5
</code></pre>
<ul>
<li><strong>Operating System</strong>:</li>
</ul>
<pre><code>Linux dallas-sip1-int 3.10.0-514.16.1.el7.x86_64 #1 SMP Wed Apr 12 15:04:24 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
</code></pre>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/kamailio/kamailio/issues/1532">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AF36ZQWrkOdYEE4r0S7NYtWoYOMQ1oTnks5ty0_zgaJpZM4UAV3q">mute the thread</a>.<img src="https://github.com/notifications/beacon/AF36ZXqowEJkW5Rsv_idCBSBTU4Sl_MUks5ty0_zgaJpZM4UAV3q.gif" height="1" width="1" alt="" /></p>
<div itemscope itemtype="http://schema.org/EmailMessage">
<div itemprop="action" itemscope itemtype="http://schema.org/ViewAction">
  <link itemprop="url" href="https://github.com/kamailio/kamailio/issues/1532"></link>
  <meta itemprop="name" content="View Issue"></meta>
</div>
<meta itemprop="description" content="View this Issue on GitHub"></meta>
</div>

<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/kamailio/kamailio","title":"kamailio/kamailio","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/kamailio/kamailio"}},"updates":{"snippets":[{"icon":"DESCRIPTION","message":"t_relay() sending packets from the wrong TCP socket (#1532)"}],"action":{"name":"View Issue","url":"https://github.com/kamailio/kamailio/issues/1532"}}}</script>
<script type="application/ld+json">{"@type":"MessageCard","@context":"http://schema.org/extensions","hideOriginalBody":"false","originator":"37567f93-e2a7-4e2a-ad37-a9160fc62647","title":"t_relay() sending packets from the wrong TCP socket (#1532)","sections":[{"text":"","activityTitle":"**Jared Hull**","activityImage":"https://avatars3.githubusercontent.com/u/5343171?s=160\u0026v=4","activitySubtitle":"@Forty-Tw0","facts":[{"name":"Repository: ","value":"kamailio/kamailio"},{"name":"Issue #: ","value":1532}]}],"potentialAction":[{"name":"Add a comment","@type":"ActionCard","inputs":[{"isMultiLine":true,"@type":"TextInput","id":"IssueComment","isRequired":false}],"actions":[{"name":"Comment","@type":"HttpPOST","target":"https://api.github.com","body":"{\"commandName\":\"IssueComment\",\"repositoryFullName\":\"kamailio/kamailio\",\"issueId\":1532,\"IssueComment\":\"{{IssueComment.value}}\"}"}]},{"name":"Close issue","@type":"HttpPOST","target":"https://api.github.com","body":"{\"commandName\":\"IssueClose\",\"repositoryFullName\":\"kamailio/kamailio\",\"issueId\":1532}"},{"targets":[{"os":"default","uri":"https://github.com/kamailio/kamailio/issues/1532"}],"@type":"OpenUri","name":"View on GitHub"},{"name":"Unsubscribe","@type":"HttpPOST","target":"https://api.github.com","body":"{\"commandName\":\"MuteNotification\",\"threadId\":335633898}"}],"themeColor":"26292E"}</script>