Skip to content

Commit 7266507

Browse files
cernekeeummakynes
authored andcommitted
netfilter: nf_ct_sip: support Cisco 7941/7945 IP phones
Most SIP devices use a source port of 5060/udp on SIP requests, so the response automatically comes back to port 5060: phone_ip:5060 -> proxy_ip:5060 REGISTER proxy_ip:5060 -> phone_ip:5060 100 Trying The newer Cisco IP phones, however, use a randomly chosen high source port for the SIP request but expect the response on port 5060: phone_ip:49173 -> proxy_ip:5060 REGISTER proxy_ip:5060 -> phone_ip:5060 100 Trying Standard Linux NAT, with or without nf_nat_sip, will send the reply back to port 49173, not 5060: phone_ip:49173 -> proxy_ip:5060 REGISTER proxy_ip:5060 -> phone_ip:49173 100 Trying But the phone is not listening on 49173, so it will never see the reply. This patch modifies nf_*_sip to work around this quirk by extracting the SIP response port from the Via: header, iff the source IP in the packet header matches the source IP in the SIP request. Signed-off-by: Kevin Cernekee <cernekee@gmail.com> Acked-by: Eric Dumazet <eric.dumazet@gmail.com> Cc: Patrick McHardy <kaber@trash.net> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 247fa82 commit 7266507

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎include/linux/netfilter/nf_conntrack_sip.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
#include <net/netfilter/nf_conntrack_expect.h>
66

7+
#include <linux/types.h>
8+
79
#define SIP_PORT 5060
810
#define SIP_TIMEOUT 3600
911

1012
struct nf_ct_sip_master {
1113
unsigned int register_cseq;
1214
unsigned int invite_cseq;
15+
__be16 forced_dport;
1316
};
1417

1518
enum sip_expectation_classes {

‎net/netfilter/nf_conntrack_sip.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,8 +1440,25 @@ static int process_sip_request(struct sk_buff *skb, unsigned int protoff,
14401440
{
14411441
enum ip_conntrack_info ctinfo;
14421442
struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1443+
struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1444+
enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
14431445
unsigned int matchoff, matchlen;
14441446
unsigned int cseq, i;
1447+
union nf_inet_addr addr;
1448+
__be16 port;
1449+
1450+
/* Many Cisco IP phones use a high source port for SIP requests, but
1451+
* listen for the response on port 5060. If we are the local
1452+
* router for one of these phones, save the port number from the
1453+
* Via: header so that nf_nat_sip can redirect the responses to
1454+
* the correct port.
1455+
*/
1456+
if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
1457+
SIP_HDR_VIA_UDP, NULL, &matchoff,
1458+
&matchlen, &addr, &port) > 0 &&
1459+
port != ct->tuplehash[dir].tuple.src.u.udp.port &&
1460+
nf_inet_addr_cmp(&addr, &ct->tuplehash[dir].tuple.src.u3))
1461+
ct_sip_info->forced_dport = port;
14451462

14461463
for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
14471464
const struct sip_handler *handler;

‎net/netfilter/nf_nat_sip.c‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ static int map_addr(struct sk_buff *skb, unsigned int protoff,
9595
enum ip_conntrack_info ctinfo;
9696
struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
9797
enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
98+
struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
9899
char buffer[INET6_ADDRSTRLEN + sizeof("[]:nnnnn")];
99100
unsigned int buflen;
100101
union nf_inet_addr newaddr;
@@ -107,7 +108,8 @@ static int map_addr(struct sk_buff *skb, unsigned int protoff,
107108
} else if (nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, addr) &&
108109
ct->tuplehash[dir].tuple.dst.u.udp.port == port) {
109110
newaddr = ct->tuplehash[!dir].tuple.src.u3;
110-
newport = ct->tuplehash[!dir].tuple.src.u.udp.port;
111+
newport = ct_sip_info->forced_dport ? :
112+
ct->tuplehash[!dir].tuple.src.u.udp.port;
111113
} else
112114
return 1;
113115

@@ -144,6 +146,7 @@ static unsigned int nf_nat_sip(struct sk_buff *skb, unsigned int protoff,
144146
enum ip_conntrack_info ctinfo;
145147
struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
146148
enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
149+
struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
147150
unsigned int coff, matchoff, matchlen;
148151
enum sip_header_types hdr;
149152
union nf_inet_addr addr;
@@ -258,6 +261,21 @@ static unsigned int nf_nat_sip(struct sk_buff *skb, unsigned int protoff,
258261
!map_sip_addr(skb, protoff, dataoff, dptr, datalen, SIP_HDR_TO))
259262
return NF_DROP;
260263

264+
/* Mangle destination port for Cisco phones, then fix up checksums */
265+
if (dir == IP_CT_DIR_REPLY && ct_sip_info->forced_dport) {
266+
struct udphdr *uh;
267+
268+
if (!skb_make_writable(skb, skb->len))
269+
return NF_DROP;
270+
271+
uh = (void *)skb->data + protoff;
272+
uh->dest = ct_sip_info->forced_dport;
273+
274+
if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo, protoff,
275+
0, 0, NULL, 0))
276+
return NF_DROP;
277+
}
278+
261279
return NF_ACCEPT;
262280
}
263281

@@ -311,8 +329,10 @@ static unsigned int nf_nat_sip_expect(struct sk_buff *skb, unsigned int protoff,
311329
enum ip_conntrack_info ctinfo;
312330
struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
313331
enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
332+
struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
314333
union nf_inet_addr newaddr;
315334
u_int16_t port;
335+
__be16 srcport;
316336
char buffer[INET6_ADDRSTRLEN + sizeof("[]:nnnnn")];
317337
unsigned int buflen;
318338

@@ -326,8 +346,9 @@ static unsigned int nf_nat_sip_expect(struct sk_buff *skb, unsigned int protoff,
326346
/* If the signalling port matches the connection's source port in the
327347
* original direction, try to use the destination port in the opposite
328348
* direction. */
329-
if (exp->tuple.dst.u.udp.port ==
330-
ct->tuplehash[dir].tuple.src.u.udp.port)
349+
srcport = ct_sip_info->forced_dport ? :
350+
ct->tuplehash[dir].tuple.src.u.udp.port;
351+
if (exp->tuple.dst.u.udp.port == srcport)
331352
port = ntohs(ct->tuplehash[!dir].tuple.dst.u.udp.port);
332353
else
333354
port = ntohs(exp->tuple.dst.u.udp.port);

0 commit comments

Comments
 (0)