nf_nat_sip.c 8.72 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/* SIP extension for UDP NAT alteration.
 *
 * (C) 2005 by Christian Hentschel <chentschel@arnet.com.ar>
 * based on RR's ip_nat_ftp.c and other modules.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
14
#include <net/ip.h>
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <linux/udp.h>

#include <net/netfilter/nf_nat.h>
#include <net/netfilter/nf_nat_helper.h>
#include <net/netfilter/nf_nat_rule.h>
#include <net/netfilter/nf_conntrack_helper.h>
#include <net/netfilter/nf_conntrack_expect.h>
#include <linux/netfilter/nf_conntrack_sip.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
MODULE_DESCRIPTION("SIP NAT helper");
MODULE_ALIAS("ip_nat_sip");

struct addr_map {
	struct {
		char		src[sizeof("nnn.nnn.nnn.nnn:nnnnn")];
		char		dst[sizeof("nnn.nnn.nnn.nnn:nnnnn")];
		unsigned int	srclen, srciplen;
		unsigned int	dstlen, dstiplen;
	} addr[IP_CT_DIR_MAX];
};

static void addr_map_init(struct nf_conn *ct, struct addr_map *map)
{
	struct nf_conntrack_tuple *t;
	enum ip_conntrack_dir dir;
	unsigned int n;

	for (dir = 0; dir < IP_CT_DIR_MAX; dir++) {
		t = &ct->tuplehash[dir].tuple;

		n = sprintf(map->addr[dir].src, "%u.%u.%u.%u",
			    NIPQUAD(t->src.u3.ip));
		map->addr[dir].srciplen = n;
		n += sprintf(map->addr[dir].src + n, ":%u",
			     ntohs(t->src.u.udp.port));
		map->addr[dir].srclen = n;

		n = sprintf(map->addr[dir].dst, "%u.%u.%u.%u",
			    NIPQUAD(t->dst.u3.ip));
		map->addr[dir].dstiplen = n;
		n += sprintf(map->addr[dir].dst + n, ":%u",
			     ntohs(t->dst.u.udp.port));
		map->addr[dir].dstlen = n;
	}
}

63
static int map_sip_addr(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
			struct nf_conn *ct, const char **dptr, size_t dlen,
			enum sip_header_pos pos, struct addr_map *map)
{
	enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
	unsigned int matchlen, matchoff, addrlen;
	char *addr;

	if (ct_sip_get_info(ct, *dptr, dlen, &matchoff, &matchlen, pos) <= 0)
		return 1;

	if ((matchlen == map->addr[dir].srciplen ||
	     matchlen == map->addr[dir].srclen) &&
	    memcmp(*dptr + matchoff, map->addr[dir].src, matchlen) == 0) {
		addr    = map->addr[!dir].dst;
		addrlen = map->addr[!dir].dstlen;
	} else if ((matchlen == map->addr[dir].dstiplen ||
		    matchlen == map->addr[dir].dstlen) &&
		   memcmp(*dptr + matchoff, map->addr[dir].dst, matchlen) == 0) {
		addr    = map->addr[!dir].src;
		addrlen = map->addr[!dir].srclen;
	} else
		return 1;

87
	if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo,
88
				      matchoff, matchlen, addr, addrlen))
89
		return 0;
90
	*dptr = skb->data + ip_hdrlen(skb) + sizeof(struct udphdr);
91
92
93
94
	return 1;

}

95
static unsigned int ip_nat_sip(struct sk_buff *skb,
96
97
98
99
100
101
102
103
			       enum ip_conntrack_info ctinfo,
			       struct nf_conn *ct,
			       const char **dptr)
{
	enum sip_header_pos pos;
	struct addr_map map;
	int dataoff, datalen;

104
105
	dataoff = ip_hdrlen(skb) + sizeof(struct udphdr);
	datalen = skb->len - dataoff;
106
	if (datalen < sizeof("SIP/2.0") - 1)
107
		return NF_ACCEPT;
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

	addr_map_init(ct, &map);

	/* Basic rules: requests and responses. */
	if (strncmp(*dptr, "SIP/2.0", sizeof("SIP/2.0") - 1) != 0) {
		/* 10.2: Constructing the REGISTER Request:
		 *
		 * The "userinfo" and "@" components of the SIP URI MUST NOT
		 * be present.
		 */
		if (datalen >= sizeof("REGISTER") - 1 &&
		    strncmp(*dptr, "REGISTER", sizeof("REGISTER") - 1) == 0)
			pos = POS_REG_REQ_URI;
		else
			pos = POS_REQ_URI;

124
		if (!map_sip_addr(skb, ctinfo, ct, dptr, datalen, pos, &map))
125
126
127
			return NF_DROP;
	}

128
129
130
131
	if (!map_sip_addr(skb, ctinfo, ct, dptr, datalen, POS_FROM, &map) ||
	    !map_sip_addr(skb, ctinfo, ct, dptr, datalen, POS_TO, &map) ||
	    !map_sip_addr(skb, ctinfo, ct, dptr, datalen, POS_VIA, &map) ||
	    !map_sip_addr(skb, ctinfo, ct, dptr, datalen, POS_CONTACT, &map))
132
133
134
135
		return NF_DROP;
	return NF_ACCEPT;
}

136
static unsigned int mangle_sip_packet(struct sk_buff *skb,
137
138
139
140
141
142
143
144
145
146
147
				      enum ip_conntrack_info ctinfo,
				      struct nf_conn *ct,
				      const char **dptr, size_t dlen,
				      char *buffer, int bufflen,
				      enum sip_header_pos pos)
{
	unsigned int matchlen, matchoff;

	if (ct_sip_get_info(ct, *dptr, dlen, &matchoff, &matchlen, pos) <= 0)
		return 0;

148
	if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo,
149
				      matchoff, matchlen, buffer, bufflen))
150
151
152
		return 0;

	/* We need to reload this. Thanks Patrick. */
153
	*dptr = skb->data + ip_hdrlen(skb) + sizeof(struct udphdr);
154
155
156
	return 1;
}

157
static int mangle_content_len(struct sk_buff *skb,
158
159
160
161
162
163
164
165
			      enum ip_conntrack_info ctinfo,
			      struct nf_conn *ct,
			      const char *dptr)
{
	unsigned int dataoff, matchoff, matchlen;
	char buffer[sizeof("65536")];
	int bufflen;

166
	dataoff = ip_hdrlen(skb) + sizeof(struct udphdr);
167
168

	/* Get actual SDP lenght */
169
	if (ct_sip_get_info(ct, dptr, skb->len - dataoff, &matchoff,
170
			    &matchlen, POS_SDP_HEADER) > 0) {
171
172
173

		/* since ct_sip_get_info() give us a pointer passing 'v='
		   we need to add 2 bytes in this count. */
174
		int c_len = skb->len - dataoff - matchoff + 2;
175
176

		/* Now, update SDP length */
177
		if (ct_sip_get_info(ct, dptr, skb->len - dataoff, &matchoff,
178
				    &matchlen, POS_CONTENT) > 0) {
179
180

			bufflen = sprintf(buffer, "%u", c_len);
181
			return nf_nat_mangle_udp_packet(skb, ct, ctinfo,
182
183
184
185
186
187
188
							matchoff, matchlen,
							buffer, bufflen);
		}
	}
	return 0;
}

189
static unsigned int mangle_sdp(struct sk_buff *skb,
190
191
192
193
194
195
196
197
			       enum ip_conntrack_info ctinfo,
			       struct nf_conn *ct,
			       __be32 newip, u_int16_t port,
			       const char *dptr)
{
	char buffer[sizeof("nnn.nnn.nnn.nnn")];
	unsigned int dataoff, bufflen;

198
	dataoff = ip_hdrlen(skb) + sizeof(struct udphdr);
199
200
201

	/* Mangle owner and contact info. */
	bufflen = sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(newip));
202
	if (!mangle_sip_packet(skb, ctinfo, ct, &dptr, skb->len - dataoff,
203
			       buffer, bufflen, POS_OWNER_IP4))
204
205
		return 0;

206
	if (!mangle_sip_packet(skb, ctinfo, ct, &dptr, skb->len - dataoff,
207
			       buffer, bufflen, POS_CONNECTION_IP4))
208
209
210
211
		return 0;

	/* Mangle media port. */
	bufflen = sprintf(buffer, "%u", port);
212
	if (!mangle_sip_packet(skb, ctinfo, ct, &dptr, skb->len - dataoff,
213
			       buffer, bufflen, POS_MEDIA))
214
215
		return 0;

216
	return mangle_content_len(skb, ctinfo, ct, dptr);
217
218
}

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
static void ip_nat_sdp_expect(struct nf_conn *ct,
			      struct nf_conntrack_expect *exp)
{
	struct nf_nat_range range;

	/* This must be a fresh one. */
	BUG_ON(ct->status & IPS_NAT_DONE_MASK);

	/* Change src to where master sends to */
	range.flags = IP_NAT_RANGE_MAP_IPS;
	range.min_ip = range.max_ip
		= ct->master->tuplehash[!exp->dir].tuple.dst.u3.ip;
	/* hook doesn't matter, but it has to do source manip */
	nf_nat_setup_info(ct, &range, NF_IP_POST_ROUTING);

	/* For DST manip, map port here to where it's expected. */
	range.flags = (IP_NAT_RANGE_MAP_IPS | IP_NAT_RANGE_PROTO_SPECIFIED);
	range.min = range.max = exp->saved_proto;
	range.min_ip = range.max_ip = exp->saved_ip;
	/* hook doesn't matter, but it has to do destination manip */
	nf_nat_setup_info(ct, &range, NF_IP_PRE_ROUTING);
}

242
243
/* So, this packet has hit the connection tracking matching code.
   Mangle it, and change the expectation to match the new version. */
244
static unsigned int ip_nat_sdp(struct sk_buff *skb,
245
246
247
248
249
250
251
252
253
254
			       enum ip_conntrack_info ctinfo,
			       struct nf_conntrack_expect *exp,
			       const char *dptr)
{
	struct nf_conn *ct = exp->master;
	enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
	__be32 newip;
	u_int16_t port;

	/* Connection will come from reply */
255
256
257
258
259
	if (ct->tuplehash[dir].tuple.src.u3.ip ==
	    ct->tuplehash[!dir].tuple.dst.u3.ip)
		newip = exp->tuple.dst.u3.ip;
	else
		newip = ct->tuplehash[!dir].tuple.dst.u3.ip;
260

261
	exp->saved_ip = exp->tuple.dst.u3.ip;
262
263
264
265
266
267
	exp->tuple.dst.u3.ip = newip;
	exp->saved_proto.udp.port = exp->tuple.dst.u.udp.port;
	exp->dir = !dir;

	/* When you see the packet, we need to NAT it the same as the
	   this one. */
268
	exp->expectfn = ip_nat_sdp_expect;
269
270
271
272

	/* Try to get same port: if not, try to change it. */
	for (port = ntohs(exp->saved_proto.udp.port); port != 0; port++) {
		exp->tuple.dst.u.udp.port = htons(port);
273
		if (nf_ct_expect_related(exp) == 0)
274
275
276
277
278
279
			break;
	}

	if (port == 0)
		return NF_DROP;

280
	if (!mangle_sdp(skb, ctinfo, ct, newip, port, dptr)) {
281
		nf_ct_unexpect_related(exp);
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
		return NF_DROP;
	}
	return NF_ACCEPT;
}

static void __exit nf_nat_sip_fini(void)
{
	rcu_assign_pointer(nf_nat_sip_hook, NULL);
	rcu_assign_pointer(nf_nat_sdp_hook, NULL);
	synchronize_rcu();
}

static int __init nf_nat_sip_init(void)
{
	BUG_ON(rcu_dereference(nf_nat_sip_hook));
	BUG_ON(rcu_dereference(nf_nat_sdp_hook));
	rcu_assign_pointer(nf_nat_sip_hook, ip_nat_sip);
	rcu_assign_pointer(nf_nat_sdp_hook, ip_nat_sdp);
	return 0;
}

module_init(nf_nat_sip_init);
module_exit(nf_nat_sip_fini);