<p>Yikes, I don't have pcaps of this issue anymore and it is difficult to reproduce because it involves a random collision of port numbers on the customer's network with NAT involved.<br>
On top of that I seem to be having trouble getting my issue across.</p>
<p>In this case I have two connections created by phones with the same destination but different sources, then a third which has a the same source but different destination.<br>
Device A: tcp:CustomerIP:10042 -> <code>tcp:KamailioIP:4242</code><br>
Device B: <code>tcp:CustomerIP:11042</code> -> <code>tcp:KamailioIP:4242</code><br>
Device A new connection for a 200 OK response: <code>tcp:CustomerIP:11042</code> -> tcp:KamailioIP:5062</p>
<p>Kamailio sees this as:<br>
Device A: <code>tcp:KamailioIP:4242</code> -> tcp:CustomerIP:10042<br>
Device B: <code>tcp:KamailioIP:4242</code> -> <code>tcp:CustomerIP:11042</code><br>
Device A receives a SIP message with a contact telling them to respond to Kamailio on 5062, which causes it to create a new TCP connection which in turn confuses Kamailio.<br>
Device A tcp:KamailioIP:5062 -> <code>tcp:CustomerIP:11042</code></p>
<blockquote>
<p>Kamailio is reusing the tcp connection whenever the destination address is matching an active one.<br>
As seen above, Kamailio thinks Device A has two connections with the same destination.<br>
This invite is not in response to any packet, it is being relayed from my PBX as the first packet of a new transaction and I am using lookup() and then t_relay() to set the Request-URI and then relay the packet to that destination.<br>
If t_relay uses the R-URI destination to find the TCP socket it should send from, how does it choose between these two sockets?<br>
The problem I am seeing is that it incorrectly chooses to send the INVITE from the `socket' that has a source port of 5062 because the newest connection it has become aware of for that destination is the socket with 5062.</p>
</blockquote>
<p>I hope I've been able to clearly describe what, I think, is happening here. If not, we currently have a mitigation strategy to avoid sending a packet with a contact that makes phones want to create another connection to Kamailio.</p>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you commented.<br />Reply to this email directly, <a href="https://github.com/kamailio/kamailio/issues/1532#issuecomment-399254671">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AF36Za3cfPsfuytD7YMfyZ_cAUKWPOFyks5t_BUXgaJpZM4UAV3q">mute the thread</a>.<img src="https://github.com/notifications/beacon/AF36ZVzHgpBN1DVLKBudLavy6GPJQJO7ks5t_BUXgaJpZM4UAV3q.gif" height="1" width="1" alt="" /></p>
<script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","potentialAction":{"@type":"ViewAction","target":"https://github.com/kamailio/kamailio/issues/1532#issuecomment-399254671","url":"https://github.com/kamailio/kamailio/issues/1532#issuecomment-399254671","name":"View Issue"},"description":"View this Issue on GitHub","publisher":{"@type":"Organization","name":"GitHub","url":"https://github.com"}}</script>
<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://assets-cdn.github.com/images/email/message_cards/header.png","avatar_image_url":"https://assets-cdn.github.com/images/email/message_cards/avatar.png","action":{"name":"Open in GitHub","url":"https://github.com/kamailio/kamailio"}},"updates":{"snippets":[{"icon":"PERSON","message":"@Forty-Tw0 in #1532: Yikes, I don't have pcaps of this issue anymore and it is difficult to reproduce because it involves a random collision of port numbers on the customer's network with NAT involved.\r\nOn top of that I seem to be having trouble getting my issue across.\r\n\r\nIn this case I have two connections created by phones with the same destination but different sources, then a third which has a the same source but different destination.\r\nDevice A: tcp:CustomerIP:10042 -\u003e `tcp:KamailioIP:4242`\r\nDevice B: `tcp:CustomerIP:11042` -\u003e `tcp:KamailioIP:4242`\r\nDevice A new connection for a 200 OK response: `tcp:CustomerIP:11042` -\u003e tcp:KamailioIP:5062\r\n\r\nKamailio sees this as:\r\nDevice A: `tcp:KamailioIP:4242` -\u003e tcp:CustomerIP:10042\r\nDevice B: `tcp:KamailioIP:4242` -\u003e `tcp:CustomerIP:11042`\r\nDevice A receives a SIP message with a contact telling them to respond to Kamailio on 5062, which causes it to create a new TCP connection which in turn confuses Kamailio.\r\nDevice A tcp:KamailioIP:5062 -\u003e `tcp:CustomerIP:11042`\r\n\r\n\u003eKamailio is reusing the tcp connection whenever the destination address is matching an active one.\r\nAs seen above, Kamailio thinks Device A has two connections with the same destination.\r\nThis invite is not in response to any packet, it is being relayed from my PBX as the first packet of a new transaction and I am using lookup() and then t_relay() to set the Request-URI and then relay the packet to that destination.\r\nIf t_relay uses the R-URI destination to find the TCP socket it should send from, how does it choose between these two sockets?\r\nThe problem I am seeing is that it incorrectly chooses to send the INVITE from the `socket' that has a source port of 5062 because the newest connection it has become aware of for that destination is the socket with 5062.\r\n\r\nI hope I've been able to clearly describe what, I think, is happening here. If not, we currently have a mitigation strategy to avoid sending a packet with a contact that makes phones want to create another connection to Kamailio."}],"action":{"name":"View Issue","url":"https://github.com/kamailio/kamailio/issues/1532#issuecomment-399254671"}}}</script>
<script type="application/ld+json">{
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"hideOriginalBody": "false",
"originator": "AF6C5A86-E920-430C-9C59-A73278B5EFEB",
"title": "Re: [kamailio/kamailio] t_relay() sending packets from the wrong TCP socket (#1532)",
"sections": [
{
"text": "",
"activityTitle": "**Jared Hull**",
"activityImage": "https://assets-cdn.github.com/images/email/message_cards/avatar.png",
"activitySubtitle": "@Forty-Tw0",
"facts": [

]
}
],
"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": "{\n\"commandName\": \"IssueComment\",\n\"repositoryFullName\": \"kamailio/kamailio\",\n\"issueId\": 1532,\n\"IssueComment\": \"{{IssueComment.value}}\"\n}"
}
]
},
{
"targets": [
{
"os": "default",
"uri": "https://github.com/kamailio/kamailio/issues/1532#issuecomment-399254671"
}
],
"@type": "OpenUri",
"name": "View on GitHub"
},
{
"name": "Unsubscribe",
"@type": "HttpPOST",
"target": "https://api.github.com",
"body": "{\n\"commandName\": \"MuteNotification\",\n\"threadId\": 335633898\n}"
}
],
"themeColor": "26292E"
}</script>