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>
15 #include <linux/udp.h>
17 #include <net/netfilter/nf_nat.h>
18 #include <net/netfilter/nf_nat_helper.h>
19 #include <net/netfilter/nf_nat_rule.h>
20 #include <net/netfilter/nf_conntrack_helper.h>
21 #include <net/netfilter/nf_conntrack_expect.h>
22 #include <linux/netfilter/nf_conntrack_sip.h>
24 MODULE_LICENSE("GPL");
25 MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
26 MODULE_DESCRIPTION("SIP NAT helper");
27 MODULE_ALIAS("ip_nat_sip");
32 #define DEBUGP(format, args...)
37 char src[sizeof("nnn.nnn.nnn.nnn:nnnnn")];
38 char dst[sizeof("nnn.nnn.nnn.nnn:nnnnn")];
39 unsigned int srclen, srciplen;
40 unsigned int dstlen, dstiplen;
41 } addr[IP_CT_DIR_MAX];
44 static void addr_map_init(struct nf_conn *ct, struct addr_map *map)
46 struct nf_conntrack_tuple *t;
47 enum ip_conntrack_dir dir;
50 for (dir = 0; dir < IP_CT_DIR_MAX; dir++) {
51 t = &ct->tuplehash[dir].tuple;
53 n = sprintf(map->addr[dir].src, "%u.%u.%u.%u",
54 NIPQUAD(t->src.u3.ip));
55 map->addr[dir].srciplen = n;
56 n += sprintf(map->addr[dir].src + n, ":%u",
57 ntohs(t->src.u.udp.port));
58 map->addr[dir].srclen = n;
60 n = sprintf(map->addr[dir].dst, "%u.%u.%u.%u",
61 NIPQUAD(t->dst.u3.ip));
62 map->addr[dir].dstiplen = n;
63 n += sprintf(map->addr[dir].dst + n, ":%u",
64 ntohs(t->dst.u.udp.port));
65 map->addr[dir].dstlen = n;
69 static int map_sip_addr(struct sk_buff **pskb, enum ip_conntrack_info ctinfo,
70 struct nf_conn *ct, const char **dptr, size_t dlen,
71 enum sip_header_pos pos, struct addr_map *map)
73 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
74 unsigned int matchlen, matchoff, addrlen;
77 if (ct_sip_get_info(ct, *dptr, dlen, &matchoff, &matchlen, pos) <= 0)
80 if ((matchlen == map->addr[dir].srciplen ||
81 matchlen == map->addr[dir].srclen) &&
82 memcmp(*dptr + matchoff, map->addr[dir].src, matchlen) == 0) {
83 addr = map->addr[!dir].dst;
84 addrlen = map->addr[!dir].dstlen;
85 } else if ((matchlen == map->addr[dir].dstiplen ||
86 matchlen == map->addr[dir].dstlen) &&
87 memcmp(*dptr + matchoff, map->addr[dir].dst, matchlen) == 0) {
88 addr = map->addr[!dir].src;
89 addrlen = map->addr[!dir].srclen;
93 if (!nf_nat_mangle_udp_packet(pskb, ct, ctinfo,
94 matchoff, matchlen, addr, addrlen))
96 *dptr = (*pskb)->data + ip_hdrlen(*pskb) + sizeof(struct udphdr);
101 static unsigned int ip_nat_sip(struct sk_buff **pskb,
102 enum ip_conntrack_info ctinfo,
106 enum sip_header_pos pos;
108 int dataoff, datalen;
110 dataoff = ip_hdrlen(*pskb) + sizeof(struct udphdr);
111 datalen = (*pskb)->len - dataoff;
112 if (datalen < sizeof("SIP/2.0") - 1)
115 addr_map_init(ct, &map);
117 /* Basic rules: requests and responses. */
118 if (strncmp(*dptr, "SIP/2.0", sizeof("SIP/2.0") - 1) != 0) {
119 /* 10.2: Constructing the REGISTER Request:
121 * The "userinfo" and "@" components of the SIP URI MUST NOT
124 if (datalen >= sizeof("REGISTER") - 1 &&
125 strncmp(*dptr, "REGISTER", sizeof("REGISTER") - 1) == 0)
126 pos = POS_REG_REQ_URI;
130 if (!map_sip_addr(pskb, ctinfo, ct, dptr, datalen, pos, &map))
134 if (!map_sip_addr(pskb, ctinfo, ct, dptr, datalen, POS_FROM, &map) ||
135 !map_sip_addr(pskb, ctinfo, ct, dptr, datalen, POS_TO, &map) ||
136 !map_sip_addr(pskb, ctinfo, ct, dptr, datalen, POS_VIA, &map) ||
137 !map_sip_addr(pskb, ctinfo, ct, dptr, datalen, POS_CONTACT, &map))
142 static unsigned int mangle_sip_packet(struct sk_buff **pskb,
143 enum ip_conntrack_info ctinfo,
145 const char **dptr, size_t dlen,
146 char *buffer, int bufflen,
147 enum sip_header_pos pos)
149 unsigned int matchlen, matchoff;
151 if (ct_sip_get_info(ct, *dptr, dlen, &matchoff, &matchlen, pos) <= 0)
154 if (!nf_nat_mangle_udp_packet(pskb, ct, ctinfo,
155 matchoff, matchlen, buffer, bufflen))
158 /* We need to reload this. Thanks Patrick. */
159 *dptr = (*pskb)->data + ip_hdrlen(*pskb) + sizeof(struct udphdr);
163 static int mangle_content_len(struct sk_buff **pskb,
164 enum ip_conntrack_info ctinfo,
168 unsigned int dataoff, matchoff, matchlen;
169 char buffer[sizeof("65536")];
172 dataoff = ip_hdrlen(*pskb) + sizeof(struct udphdr);
174 /* Get actual SDP lenght */
175 if (ct_sip_get_info(ct, dptr, (*pskb)->len - dataoff, &matchoff,
176 &matchlen, POS_SDP_HEADER) > 0) {
178 /* since ct_sip_get_info() give us a pointer passing 'v='
179 we need to add 2 bytes in this count. */
180 int c_len = (*pskb)->len - dataoff - matchoff + 2;
182 /* Now, update SDP length */
183 if (ct_sip_get_info(ct, dptr, (*pskb)->len - dataoff, &matchoff,
184 &matchlen, POS_CONTENT) > 0) {
186 bufflen = sprintf(buffer, "%u", c_len);
187 return nf_nat_mangle_udp_packet(pskb, ct, ctinfo,
195 static unsigned int mangle_sdp(struct sk_buff **pskb,
196 enum ip_conntrack_info ctinfo,
198 __be32 newip, u_int16_t port,
201 char buffer[sizeof("nnn.nnn.nnn.nnn")];
202 unsigned int dataoff, bufflen;
204 dataoff = ip_hdrlen(*pskb) + sizeof(struct udphdr);
206 /* Mangle owner and contact info. */
207 bufflen = sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(newip));
208 if (!mangle_sip_packet(pskb, ctinfo, ct, &dptr, (*pskb)->len - dataoff,
209 buffer, bufflen, POS_OWNER_IP4))
212 if (!mangle_sip_packet(pskb, ctinfo, ct, &dptr, (*pskb)->len - dataoff,
213 buffer, bufflen, POS_CONNECTION_IP4))
216 /* Mangle media port. */
217 bufflen = sprintf(buffer, "%u", port);
218 if (!mangle_sip_packet(pskb, ctinfo, ct, &dptr, (*pskb)->len - dataoff,
219 buffer, bufflen, POS_MEDIA))
222 return mangle_content_len(pskb, ctinfo, ct, dptr);
225 static void ip_nat_sdp_expect(struct nf_conn *ct,
226 struct nf_conntrack_expect *exp)
228 struct nf_nat_range range;
230 /* This must be a fresh one. */
231 BUG_ON(ct->status & IPS_NAT_DONE_MASK);
233 /* Change src to where master sends to */
234 range.flags = IP_NAT_RANGE_MAP_IPS;
235 range.min_ip = range.max_ip
236 = ct->master->tuplehash[!exp->dir].tuple.dst.u3.ip;
237 /* hook doesn't matter, but it has to do source manip */
238 nf_nat_setup_info(ct, &range, NF_IP_POST_ROUTING);
240 /* For DST manip, map port here to where it's expected. */
241 range.flags = (IP_NAT_RANGE_MAP_IPS | IP_NAT_RANGE_PROTO_SPECIFIED);
242 range.min = range.max = exp->saved_proto;
243 range.min_ip = range.max_ip = exp->saved_ip;
244 /* hook doesn't matter, but it has to do destination manip */
245 nf_nat_setup_info(ct, &range, NF_IP_PRE_ROUTING);
248 /* So, this packet has hit the connection tracking matching code.
249 Mangle it, and change the expectation to match the new version. */
250 static unsigned int ip_nat_sdp(struct sk_buff **pskb,
251 enum ip_conntrack_info ctinfo,
252 struct nf_conntrack_expect *exp,
255 struct nf_conn *ct = exp->master;
256 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
260 DEBUGP("ip_nat_sdp():\n");
262 /* Connection will come from reply */
263 newip = ct->tuplehash[!dir].tuple.dst.u3.ip;
265 exp->saved_ip = exp->tuple.dst.u3.ip;
266 exp->tuple.dst.u3.ip = newip;
267 exp->saved_proto.udp.port = exp->tuple.dst.u.udp.port;
270 /* When you see the packet, we need to NAT it the same as the
272 exp->expectfn = ip_nat_sdp_expect;
274 /* Try to get same port: if not, try to change it. */
275 for (port = ntohs(exp->saved_proto.udp.port); port != 0; port++) {
276 exp->tuple.dst.u.udp.port = htons(port);
277 if (nf_conntrack_expect_related(exp) == 0)
284 if (!mangle_sdp(pskb, ctinfo, ct, newip, port, dptr)) {
285 nf_conntrack_unexpect_related(exp);
291 static void __exit nf_nat_sip_fini(void)
293 rcu_assign_pointer(nf_nat_sip_hook, NULL);
294 rcu_assign_pointer(nf_nat_sdp_hook, NULL);
298 static int __init nf_nat_sip_init(void)
300 BUG_ON(rcu_dereference(nf_nat_sip_hook));
301 BUG_ON(rcu_dereference(nf_nat_sdp_hook));
302 rcu_assign_pointer(nf_nat_sip_hook, ip_nat_sip);
303 rcu_assign_pointer(nf_nat_sdp_hook, ip_nat_sdp);
307 module_init(nf_nat_sip_init);
308 module_exit(nf_nat_sip_fini);