Asterisk 13 w. PJSIP, wrong SDP IP (no NAT, two interfaces)

Hi,

I have a problem on a new Asterisk 13. It has two interfaces (primary one only for Maintenance, second one for VOIP). It is at a datacenter with public IPs.
SIP/PJSIP works fine after setting bindaddr and so on. But RTP does not and I found the reason to be the SDP o=/c= field. It is set to the primary IP instead of the secondary although the secondary is set as external_media_address.

primary IP: 1.2.3.41
secondary IP: 1.2.3.85
IP of SIP phones: 4.5.6.13

PJSIP config looks like this:

[code]svvoip1*CLI> pjsip show transports

Transport: <TransportId…> <BindAddress…>

Transport: 1.2.3.85-udp udp 0 0 1.2.3.85:5060

svvoip1*CLI> pjsip show transport 1.2.3.85-udp

Transport: <TransportId…> <BindAddress…

Transport: 1.2.3.85-udp udp 0 0 1.2.3.85:5060

ParameterName : ParameterValue

async_operations : 1
bind : 1.2.3.85:5060
ca_list_file :
ca_list_path :
cert_file :
cipher :
cos : 0
domain :
external_media_address : 1.2.3.85
external_signaling_address : 1.2.3.85
external_signaling_port : 0
local_net :
method : unspecified
password :
priv_key_file :
protocol : udp
require_client_cert : No
tos : 0
verify_client : No
verify_server : No
websocket_write_timeout : 100
[/code]

But this is the SIP dialog I dumped.
Packets 1/4 is call init from the sip phone 5001. SDP IPs correct in there
Packet 7 is call init from Asterisk to sip phone 5000. Goes out the correct IP (.85) but has the wrong IP in o= and c= (.41).
I guess o= would be okay, but c= is the problem.

[root@svvoip1 asterisk]# tcpdump -nni any port 5060 -v
tcpdump: listening on any, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes
17:58:50.986135 IP (tos 0xc0, ttl 253, id 469, offset 0, flags [none], proto UDP (17), length 881)
    4.5.6.13.5062 > 1.2.3.85.5060: SIP, length: 853
        INVITE sip:5000@1.2.3.85;user=phone SIP/2.0
        Via: SIP/2.0/UDP 4.5.6.13:5062;branch=z9hG4bK38078f27299378c6
        From: "5001" <sip:5001@1.2.3.85;user=phone>;tag=c0803e70f96d156d
        To: <sip:5000@1.2.3.85;user=phone>
        Contact: <sip:5001@4.5.6.13:5062;transport=udp;user=phone>
        Supported: replaces, timer, path
        X-Grandstream-PBX: true
        P-Early-Media: Supported
        Call-ID: 32809f33e7332289@192.168.3.30
        CSeq: 29421 INVITE
        User-Agent: Grandstream GXP2020 1.2.5.3
        Max-Forwards: 70
        Allow: INVITE,ACK,CANCEL,BYE,NOTIFY,REFER,OPTIONS,INFO,SUBSCRIBE,UPDATE,PRACK,MESSAGE
        Content-Type: application/sdp
        Content-Length: 211

        v=0
        o=5001 8000 8000 IN IP4 4.5.6.13
        s=SIP Call
        c=IN IP4 4.5.6.13
        t=0 0
        m=audio 5042 RTP/AVP 8 101
        a=sendrecv
        a=rtpmap:8 PCMA/8000
        a=ptime:20
        a=rtpmap:101 telephone-event/8000
        a=fmtp:101 0-11

17:58:50.987082 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 558)
    1.2.3.85.5060 > 4.5.6.13.5062: SIP, length: 530
        SIP/2.0 401 Unauthorized
        Via: SIP/2.0/UDP 4.5.6.13:5062;rport=5062;received=4.5.6.13;branch=z9hG4bK38078f27299378c6
        Call-ID: 32809f33e7332289@192.168.3.30
        From: "5001" <sip:5001@1.2.3.85;user=phone>;tag=c0803e70f96d156d
        To: <sip:5000@1.2.3.85;user=phone>;tag=z9hG4bK38078f27299378c6
        CSeq: 29421 INVITE
        WWW-Authenticate: Digest  realm="asterisk",nonce="1424797130/22fd48b2cf2c919e5c295772c49d65f9",opaque="7b0445136f9ab0bb",algorithm=md5,qop="auth"
        Server: FPBX-12.0.38(13.1.0)
        Content-Length:  0


17:58:51.048416 IP (tos 0xc0, ttl 253, id 470, offset 0, flags [none], proto UDP (17), length 616)
    4.5.6.13.5062 > 1.2.3.85.5060: SIP, length: 588
        ACK sip:5000@1.2.3.85;user=phone SIP/2.0
        Via: SIP/2.0/UDP 4.5.6.13:5062;branch=z9hG4bK38078f27299378c6
        From: "5001" <sip:5001@1.2.3.85;user=phone>;tag=c0803e70f96d156d
        To: <sip:5000@1.2.3.85;user=phone>;tag=z9hG4bK38078f27299378c6
        Contact: <sip:5001@4.5.6.13:5062;transport=udp;user=phone>
        Supported: path
        X-Grandstream-PBX: true
        Call-ID: 32809f33e7332289@192.168.3.30
        CSeq: 29421 ACK
        User-Agent: Grandstream GXP2020 1.2.5.3
        Max-Forwards: 70
        Allow: INVITE,ACK,CANCEL,BYE,NOTIFY,REFER,OPTIONS,INFO,SUBSCRIBE,UPDATE,PRACK,MESSAGE
        Content-Length: 0


17:58:51.071818 IP (tos 0xc0, ttl 253, id 471, offset 0, flags [none], proto UDP (17), length 1168)
    4.5.6.13.5062 > 1.2.3.85.5060: SIP, length: 1140
        INVITE sip:5000@1.2.3.85;user=phone SIP/2.0
        Via: SIP/2.0/UDP 4.5.6.13:5062;branch=z9hG4bK43475627c28735c3
        From: "5001" <sip:5001@1.2.3.85;user=phone>;tag=c0803e70f96d156d
        To: <sip:5000@1.2.3.85;user=phone>
        Contact: <sip:5001@4.5.6.13:5062;transport=udp;user=phone>
        Supported: replaces, timer, path
        X-Grandstream-PBX: true
        P-Early-Media: Supported
        Authorization: Digest username="5001", realm="asterisk", algorithm=MD5, uri="sip:5000@1.2.3.85;user=phone", qop=auth, nc=00000001, cnonce="afb038c088f3d0bd", opaque="7b0445136f9ab0bb", nonce="1424797130/22fd48b2cf2c919e5c295772c49d65f9", response="5f339e05fe792c5f071faed58853a8d0"
        Call-ID: 32809f33e7332289@192.168.3.30
        CSeq: 29422 INVITE
        User-Agent: Grandstream GXP2020 1.2.5.3
        Max-Forwards: 70
        Allow: INVITE,ACK,CANCEL,BYE,NOTIFY,REFER,OPTIONS,INFO,SUBSCRIBE,UPDATE,PRACK,MESSAGE
        Content-Type: application/sdp
        Content-Length: 211

        v=0
        o=5001 8000 8001 IN IP4 4.5.6.13
        s=SIP Call
        c=IN IP4 4.5.6.13
        t=0 0
        m=audio 5042 RTP/AVP 8 101
        a=sendrecv
        a=rtpmap:8 PCMA/8000
        a=ptime:20
        a=rtpmap:101 telephone-event/8000
        a=fmtp:101 0-11

17:58:51.074517 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 377)
    1.2.3.85.5060 > 4.5.6.13.5062: SIP, length: 349
        SIP/2.0 100 Trying
        Via: SIP/2.0/UDP 4.5.6.13:5062;rport=5062;received=4.5.6.13;branch=z9hG4bK43475627c28735c3
        Call-ID: 32809f33e7332289@192.168.3.30
        From: "5001" <sip:5001@1.2.3.85;user=phone>;tag=c0803e70f96d156d
        To: <sip:5000@1.2.3.85;user=phone>
        CSeq: 29422 INVITE
        Server: FPBX-12.0.38(13.1.0)
        Content-Length:  0


17:58:51.192991 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 632)
    1.2.3.85.5060 > 4.5.6.13.5062: SIP, length: 604
        SIP/2.0 180 Ringing
        Via: SIP/2.0/UDP 4.5.6.13:5062;rport=5062;received=4.5.6.13;branch=z9hG4bK43475627c28735c3
        Call-ID: 32809f33e7332289@192.168.3.30
        From: "5001" <sip:5001@1.2.3.85;user=phone>;tag=c0803e70f96d156d
        To: <sip:5000@1.2.3.85;user=phone>;tag=FvKP4lAsvnYRzebcCJdkyf3k9TYE.iwg
        CSeq: 29422 INVITE
        Server: FPBX-12.0.38(13.1.0)
        Contact: <sip:1.2.3.85:5060>
        Allow: OPTIONS, SUBSCRIBE, NOTIFY, PUBLISH, INVITE, ACK, BYE, CANCEL, UPDATE, PRACK, REFER, REGISTER, MESSAGE
        P-Asserted-Identity: "5000" <sip:5000@1.2.3.85;user=phone>
        Content-Length:  0


17:58:51.202055 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 1156)
    1.2.3.85.5060 > 4.5.6.13.1025: SIP, length: 1128
        INVITE sip:5000@4.5.6.13:1025;user=phone SIP/2.0
        Via: SIP/2.0/UDP 1.2.3.85:5060;rport;branch=z9hG4bKPjGJgVumNW3X9OVXSTGRuppyD657CiS5n3
        From: "5001" <sip:5001@1.2.3.85>;tag=.GmLAfujeN6WLOFnt4ipJEUvWy7mLTe6
        To: <sip:5000@4.5.6.13;user=phone>
        Contact: <sip:7672ed6e-50bc-4089-a082-7e5b04f79608@1.2.3.85:5060>
        Call-ID: LO1ISvkQthIOA.kVcTggmuv.pngCR2z4
        CSeq: 4918 INVITE
        Allow: OPTIONS, SUBSCRIBE, NOTIFY, PUBLISH, INVITE, ACK, BYE, CANCEL, UPDATE, PRACK, REFER, REGISTER, MESSAGE
        Supported: 100rel, timer, replaces, norefersub
        Session-Expires: 1800
        Min-SE: 90
        P-Asserted-Identity: "5001" <sip:5001@1.2.3.85>
        Max-Forwards: 70
        User-Agent: FPBX-12.0.38(13.1.0)
        Content-Type: application/sdp
        Content-Length:   364

        v=0
        o=- 439413208 439413208 IN IP4 1.2.3.41
        s=Asterisk
        c=IN IP4 1.2.3.41
        t=0 0
        m=audio 16156 RTP/AVP 18 97 0 8 3 111 101
        a=rtpmap:18 G729/8000
        a=rtpmap:97 iLBC/8000
        a=rtpmap:0 PCMU/8000
        a=rtpmap:8 PCMA/8000
        a=rtpmap:3 GSM/8000
        a=rtpmap:111 G726-32/8000
        a=rtpmap:101 telephone-event/8000
        a=fmtp:101 0-16
        a=ptime:20
        a=maxptime:30
        a=sendrecv

17:58:51.204172 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 632)
    1.2.3.85.5060 > 4.5.6.13.5062: SIP, length: 604
        SIP/2.0 180 Ringing
        Via: SIP/2.0/UDP 4.5.6.13:5062;rport=5062;received=4.5.6.13;branch=z9hG4bK43475627c28735c3
        Call-ID: 32809f33e7332289@192.168.3.30
        From: "5001" <sip:5001@1.2.3.85;user=phone>;tag=c0803e70f96d156d
        To: <sip:5000@1.2.3.85;user=phone>;tag=FvKP4lAsvnYRzebcCJdkyf3k9TYE.iwg
        CSeq: 29422 INVITE
        Server: FPBX-12.0.38(13.1.0)
        Allow: OPTIONS, SUBSCRIBE, NOTIFY, PUBLISH, INVITE, ACK, BYE, CANCEL, UPDATE, PRACK, REFER, REGISTER, MESSAGE
        P-Asserted-Identity: "5000" <sip:5000@1.2.3.85;user=phone>
        Contact: <sip:1.2.3.85:5060>
        Content-Length:  0


17:58:51.260111 IP (tos 0xc0, ttl 253, id 787, offset 0, flags [none], proto UDP (17), length 387)
    4.5.6.13.1025 > 1.2.3.85.5060: SIP, length: 359
        SIP/2.0 100 Trying
        Via: SIP/2.0/UDP 1.2.3.85:5060;rport;branch=z9hG4bKPjGJgVumNW3X9OVXSTGRuppyD657CiS5n3
        From: "5001" <sip:5001@1.2.3.85>;tag=.GmLAfujeN6WLOFnt4ipJEUvWy7mLTe6
        To: <sip:5000@4.5.6.13;user=phone>
        Call-ID: LO1ISvkQthIOA.kVcTggmuv.pngCR2z4
        CSeq: 4918 INVITE
        User-Agent: Grandstream GXP2020 1.1.6.16
        Content-Length: 0


17:58:51.269612 IP (tos 0xc0, ttl 253, id 788, offset 0, flags [none], proto UDP (17), length 560)
    4.5.6.13.1025 > 1.2.3.85.5060: SIP, length: 532
        SIP/2.0 180 Ringing
        Via: SIP/2.0/UDP 1.2.3.85:5060;rport;branch=z9hG4bKPjGJgVumNW3X9OVXSTGRuppyD657CiS5n3
        From: "5001" <sip:5001@1.2.3.85>;tag=.GmLAfujeN6WLOFnt4ipJEUvWy7mLTe6
        To: <sip:5000@4.5.6.13;user=phone>;tag=6213a41368c55398
        Call-ID: LO1ISvkQthIOA.kVcTggmuv.pngCR2z4
        CSeq: 4918 INVITE
        User-Agent: Grandstream GXP2020 1.1.6.16
        Contact: <sip:5000@4.5.6.13:1025;transport=udp;user=phone>
        Allow: INVITE,ACK,CANCEL,BYE,NOTIFY,REFER,OPTIONS,INFO,SUBSCRIBE,UPDATE,PRACK,MESSAGE
        Content-Length: 0


17:58:51.271389 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 632)
    1.2.3.85.5060 > 4.5.6.13.5062: SIP, length: 604
        SIP/2.0 180 Ringing
        Via: SIP/2.0/UDP 4.5.6.13:5062;rport=5062;received=4.5.6.13;branch=z9hG4bK43475627c28735c3
        Call-ID: 32809f33e7332289@192.168.3.30
        From: "5001" <sip:5001@1.2.3.85;user=phone>;tag=c0803e70f96d156d
        To: <sip:5000@1.2.3.85;user=phone>;tag=FvKP4lAsvnYRzebcCJdkyf3k9TYE.iwg
        CSeq: 29422 INVITE
        Server: FPBX-12.0.38(13.1.0)
        Allow: OPTIONS, SUBSCRIBE, NOTIFY, PUBLISH, INVITE, ACK, BYE, CANCEL, UPDATE, PRACK, REFER, REGISTER, MESSAGE
        P-Asserted-Identity: "5000" <sip:5000@1.2.3.85;user=phone>
        Contact: <sip:1.2.3.85:5060>
        Content-Length:  0

Does anyone have a hint on this issue? Seems as external_media_address would not be correctly used for the SDP generation.

Regards
Martin

One more addition - asterisk also sends the RTP stream out with the wrong IP:

[root@svvoip1 asterisk]# tcpdump -nni any not host 127.0.0.1 and not port 22 and not port 5060 | head -n 100
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes
18:14:57.410561 IP 4.5.6.13.5004 > 1.2.3.85.17532: UDP, length 172
18:14:57.411036 IP 1.2.3.41.11128 > 4.5.6.13.5052: UDP, length 172
18:14:57.419494 IP 4.5.6.13.5004 > 1.2.3.85.17532: UDP, length 172
18:14:57.419666 IP 1.2.3.41.11128 > 4.5.6.13.5052: UDP, length 172
18:14:57.439262 IP 4.5.6.13.5004 > 1.2.3.85.17532: UDP, length 172
18:14:57.439433 IP 1.2.3.41.11128 > 4.5.6.13.5052: UDP, length 172
18:14:57.440009 IP 4.5.6.13.5052 > 1.2.3.41.11128: UDP, length 172
18:14:57.440230 IP 1.2.3.41.17532 > 4.5.6.13.5004: UDP, length 172
18:14:57.448811 IP 4.5.6.13.5052 > 1.2.3.41.11128: UDP, length 172
18:14:57.448985 IP 1.2.3.41.17532 > 4.5.6.13.5004: UDP, length 172
18:14:57.459509 IP 4.5.6.13.5004 > 1.2.3.85.17532: UDP, length 172
18:14:57.459675 IP 1.2.3.41.11128 > 4.5.6.13.5052: UDP, length 172
18:14:57.468476 IP 4.5.6.13.5052 > 1.2.3.41.11128: UDP, length 172
18:14:57.468621 IP 1.2.3.41.17532 > 4.5.6.13.5004: UDP, length 172

Time for an update:
I am not sure what´s the reason but it could been an update of asterisk that I did yesterday evening.
Now the correct IP is set in the c= field. o= field keeps the primary IP.
So Phones are sending RTP to the correct IP. But asterisk keeps sending the RTP packets form the primary (wrong) IP.

I don´t understand why there are so much problems on PJSIP using a server with more than one IP. I don´t think I am the only one with this kind of infrastructure.
On another server with two IPs, Asterisk 12 and chan_sip there are no problems and I guess it is becauce of PJSIP.

If I view to netstat on the Asterisk 12 server during a call it listens for UDP packets only on the second IP like this:
udp 1.2.3.86:12340 asterisk
On the problematic one it listens on all IPs and looks like this:
udp 0.0.0.0:12342 asterisk

Edit: I have found in some other threads that asterisk tries to determine the IP to be used by routes of the OS. Can someone please explain? My default gateway is obviously the primary interface.
What route do I need to convince asterisk to use always the second one?
Referring these threads:
viewtopic.php?f=1&t=86725&p=187440
viewtopic.php?f=1&t=82802&p=173797