1 /* SIP extension for UDP NAT alteration.
3 * (C) 2005 by Christian Hentschel <chentschel@arnet.com.ar>
4 * based on RR's ip_nat_ftp.c and other modules.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
14 #include <linux/udp.h>
16 #include <net/netfilter/nf_nat.h>
17 #include <net/netfilter/nf_nat_helper.h>
18 #include <net/netfilter/nf_nat_rule.h>
19 #include <net/netfilter/nf_conntrack_helper.h>
20 #include <net/netfilter/nf_conntrack_expect.h>
21 #include <linux/netfilter/nf_conntrack_sip.h>
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
25 MODULE_DESCRIPTION("SIP NAT helper");
26 MODULE_ALIAS("ip_nat_sip");
31 #define DEBUGP(format, args...)
36 char src
[sizeof("nnn.nnn.nnn.nnn:nnnnn")];
37 char dst
[sizeof("nnn.nnn.nnn.nnn:nnnnn")];
38 unsigned int srclen
, srciplen
;
39 unsigned int dstlen
, dstiplen
;
40 } addr
[IP_CT_DIR_MAX
];
43 static void addr_map_init(struct nf_conn
*ct
, struct addr_map
*map
)
45 struct nf_conntrack_tuple
*t
;
46 enum ip_conntrack_dir dir
;
49 for (dir
= 0; dir
< IP_CT_DIR_MAX
; dir
++) {
50 t
= &ct
->tuplehash
[dir
].tuple
;
52 n
= sprintf(map
->addr
[dir
].src
, "%u.%u.%u.%u",
53 NIPQUAD(t
->src
.u3
.ip
));
54 map
->addr
[dir
].srciplen
= n
;
55 n
+= sprintf(map
->addr
[dir
].src
+ n
, ":%u",
56 ntohs(t
->src
.u
.udp
.port
));
57 map
->addr
[dir
].srclen
= n
;
59 n
= sprintf(map
->addr
[dir
].dst
, "%u.%u.%u.%u",
60 NIPQUAD(t
->dst
.u3
.ip
));
61 map
->addr
[dir
].dstiplen
= n
;
62 n
+= sprintf(map
->addr
[dir
].dst
+ n
, ":%u",
63 ntohs(t
->dst
.u
.udp
.port
));
64 map
->addr
[dir
].dstlen
= n
;
68 static int map_sip_addr(struct sk_buff
**pskb
, enum ip_conntrack_info ctinfo
,
69 struct nf_conn
*ct
, const char **dptr
, size_t dlen
,
70 enum sip_header_pos pos
, struct addr_map
*map
)
72 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
73 unsigned int matchlen
, matchoff
, addrlen
;
76 if (ct_sip_get_info(ct
, *dptr
, dlen
, &matchoff
, &matchlen
, pos
) <= 0)
79 if ((matchlen
== map
->addr
[dir
].srciplen
||
80 matchlen
== map
->addr
[dir
].srclen
) &&
81 memcmp(*dptr
+ matchoff
, map
->addr
[dir
].src
, matchlen
) == 0) {
82 addr
= map
->addr
[!dir
].dst
;
83 addrlen
= map
->addr
[!dir
].dstlen
;
84 } else if ((matchlen
== map
->addr
[dir
].dstiplen
||
85 matchlen
== map
->addr
[dir
].dstlen
) &&
86 memcmp(*dptr
+ matchoff
, map
->addr
[dir
].dst
, matchlen
) == 0) {
87 addr
= map
->addr
[!dir
].src
;
88 addrlen
= map
->addr
[!dir
].srclen
;
92 if (!nf_nat_mangle_udp_packet(pskb
, ct
, ctinfo
,
93 matchoff
, matchlen
, addr
, addrlen
))
95 *dptr
= (*pskb
)->data
+ (*pskb
)->nh
.iph
->ihl
*4 + sizeof(struct udphdr
);
100 static unsigned int ip_nat_sip(struct sk_buff
**pskb
,
101 enum ip_conntrack_info ctinfo
,
105 enum sip_header_pos pos
;
107 int dataoff
, datalen
;
109 dataoff
= (*pskb
)->nh
.iph
->ihl
*4 + sizeof(struct udphdr
);
110 datalen
= (*pskb
)->len
- dataoff
;
111 if (datalen
< sizeof("SIP/2.0") - 1)
114 addr_map_init(ct
, &map
);
116 /* Basic rules: requests and responses. */
117 if (strncmp(*dptr
, "SIP/2.0", sizeof("SIP/2.0") - 1) != 0) {
118 /* 10.2: Constructing the REGISTER Request:
120 * The "userinfo" and "@" components of the SIP URI MUST NOT
123 if (datalen
>= sizeof("REGISTER") - 1 &&
124 strncmp(*dptr
, "REGISTER", sizeof("REGISTER") - 1) == 0)
125 pos
= POS_REG_REQ_URI
;
129 if (!map_sip_addr(pskb
, ctinfo
, ct
, dptr
, datalen
, pos
, &map
))
133 if (!map_sip_addr(pskb
, ctinfo
, ct
, dptr
, datalen
, POS_FROM
, &map
) ||
134 !map_sip_addr(pskb
, ctinfo
, ct
, dptr
, datalen
, POS_TO
, &map
) ||
135 !map_sip_addr(pskb
, ctinfo
, ct
, dptr
, datalen
, POS_VIA
, &map
) ||
136 !map_sip_addr(pskb
, ctinfo
, ct
, dptr
, datalen
, POS_CONTACT
, &map
))
141 static unsigned int mangle_sip_packet(struct sk_buff
**pskb
,
142 enum ip_conntrack_info ctinfo
,
144 const char **dptr
, size_t dlen
,
145 char *buffer
, int bufflen
,
146 enum sip_header_pos pos
)
148 unsigned int matchlen
, matchoff
;
150 if (ct_sip_get_info(ct
, *dptr
, dlen
, &matchoff
, &matchlen
, pos
) <= 0)
153 if (!nf_nat_mangle_udp_packet(pskb
, ct
, ctinfo
,
154 matchoff
, matchlen
, buffer
, bufflen
))
157 /* We need to reload this. Thanks Patrick. */
158 *dptr
= (*pskb
)->data
+ (*pskb
)->nh
.iph
->ihl
*4 + sizeof(struct udphdr
);
162 static int mangle_content_len(struct sk_buff
**pskb
,
163 enum ip_conntrack_info ctinfo
,
167 unsigned int dataoff
, matchoff
, matchlen
;
168 char buffer
[sizeof("65536")];
171 dataoff
= (*pskb
)->nh
.iph
->ihl
*4 + sizeof(struct udphdr
);
173 /* Get actual SDP lenght */
174 if (ct_sip_get_info(ct
, dptr
, (*pskb
)->len
- dataoff
, &matchoff
,
175 &matchlen
, POS_SDP_HEADER
) > 0) {
177 /* since ct_sip_get_info() give us a pointer passing 'v='
178 we need to add 2 bytes in this count. */
179 int c_len
= (*pskb
)->len
- dataoff
- matchoff
+ 2;
181 /* Now, update SDP length */
182 if (ct_sip_get_info(ct
, dptr
, (*pskb
)->len
- dataoff
, &matchoff
,
183 &matchlen
, POS_CONTENT
) > 0) {
185 bufflen
= sprintf(buffer
, "%u", c_len
);
186 return nf_nat_mangle_udp_packet(pskb
, ct
, ctinfo
,
194 static unsigned int mangle_sdp(struct sk_buff
**pskb
,
195 enum ip_conntrack_info ctinfo
,
197 __be32 newip
, u_int16_t port
,
200 char buffer
[sizeof("nnn.nnn.nnn.nnn")];
201 unsigned int dataoff
, bufflen
;
203 dataoff
= (*pskb
)->nh
.iph
->ihl
*4 + sizeof(struct udphdr
);
205 /* Mangle owner and contact info. */
206 bufflen
= sprintf(buffer
, "%u.%u.%u.%u", NIPQUAD(newip
));
207 if (!mangle_sip_packet(pskb
, ctinfo
, ct
, &dptr
, (*pskb
)->len
- dataoff
,
208 buffer
, bufflen
, POS_OWNER_IP4
))
211 if (!mangle_sip_packet(pskb
, ctinfo
, ct
, &dptr
, (*pskb
)->len
- dataoff
,
212 buffer
, bufflen
, POS_CONNECTION_IP4
))
215 /* Mangle media port. */
216 bufflen
= sprintf(buffer
, "%u", port
);
217 if (!mangle_sip_packet(pskb
, ctinfo
, ct
, &dptr
, (*pskb
)->len
- dataoff
,
218 buffer
, bufflen
, POS_MEDIA
))
221 return mangle_content_len(pskb
, ctinfo
, ct
, dptr
);
224 /* So, this packet has hit the connection tracking matching code.
225 Mangle it, and change the expectation to match the new version. */
226 static unsigned int ip_nat_sdp(struct sk_buff
**pskb
,
227 enum ip_conntrack_info ctinfo
,
228 struct nf_conntrack_expect
*exp
,
231 struct nf_conn
*ct
= exp
->master
;
232 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
236 DEBUGP("ip_nat_sdp():\n");
238 /* Connection will come from reply */
239 newip
= ct
->tuplehash
[!dir
].tuple
.dst
.u3
.ip
;
241 exp
->tuple
.dst
.u3
.ip
= newip
;
242 exp
->saved_proto
.udp
.port
= exp
->tuple
.dst
.u
.udp
.port
;
245 /* When you see the packet, we need to NAT it the same as the
247 exp
->expectfn
= nf_nat_follow_master
;
249 /* Try to get same port: if not, try to change it. */
250 for (port
= ntohs(exp
->saved_proto
.udp
.port
); port
!= 0; port
++) {
251 exp
->tuple
.dst
.u
.udp
.port
= htons(port
);
252 if (nf_conntrack_expect_related(exp
) == 0)
259 if (!mangle_sdp(pskb
, ctinfo
, ct
, newip
, port
, dptr
)) {
260 nf_conntrack_unexpect_related(exp
);
266 static void __exit
nf_nat_sip_fini(void)
268 rcu_assign_pointer(nf_nat_sip_hook
, NULL
);
269 rcu_assign_pointer(nf_nat_sdp_hook
, NULL
);
273 static int __init
nf_nat_sip_init(void)
275 BUG_ON(rcu_dereference(nf_nat_sip_hook
));
276 BUG_ON(rcu_dereference(nf_nat_sdp_hook
));
277 rcu_assign_pointer(nf_nat_sip_hook
, ip_nat_sip
);
278 rcu_assign_pointer(nf_nat_sdp_hook
, ip_nat_sdp
);
282 module_init(nf_nat_sip_init
);
283 module_exit(nf_nat_sip_fini
);