nf_nat_sip.c 8.84 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
#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];
};

38
static void addr_map_init(const struct nf_conn *ct, struct addr_map *map)
39
{
40
	const struct nf_conntrack_tuple *t;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
	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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
static unsigned int mangle_packet(struct sk_buff *skb,
				  const char **dptr, unsigned int *datalen,
				  unsigned int matchoff, unsigned int matchlen,
				  const char *buffer, unsigned int buflen)
{
	enum ip_conntrack_info ctinfo;
	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);

	if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo, matchoff, matchlen,
				      buffer, buflen))
		return 0;

	/* Reload data pointer and adjust datalen value */
	*dptr = skb->data + ip_hdrlen(skb) + sizeof(struct udphdr);
	*datalen += buflen - matchlen;
	return 1;
}

81
82
83
84
static int map_addr(struct sk_buff *skb,
		    const char **dptr, unsigned int *datalen,
		    unsigned int matchoff, unsigned int matchlen,
		    struct addr_map *map)
85
{
86
	enum ip_conntrack_info ctinfo;
87
	struct nf_conn *ct __maybe_unused = nf_ct_get(skb, &ctinfo);
88
	enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
89
	unsigned int addrlen;
90
91
92
93
	char *addr;

	if ((matchlen == map->addr[dir].srciplen ||
	     matchlen == map->addr[dir].srclen) &&
94
	    strncmp(*dptr + matchoff, map->addr[dir].src, matchlen) == 0) {
95
96
97
98
		addr    = map->addr[!dir].dst;
		addrlen = map->addr[!dir].dstlen;
	} else if ((matchlen == map->addr[dir].dstiplen ||
		    matchlen == map->addr[dir].dstlen) &&
99
		   strncmp(*dptr + matchoff, map->addr[dir].dst, matchlen) == 0) {
100
101
102
103
104
		addr    = map->addr[!dir].src;
		addrlen = map->addr[!dir].srclen;
	} else
		return 1;

105
106
	return mangle_packet(skb, dptr, datalen, matchoff, matchlen,
			     addr, addrlen);
107
108
}

109
110
static int map_sip_addr(struct sk_buff *skb,
			const char **dptr, unsigned int *datalen,
111
			enum sip_header_types type, struct addr_map *map)
112
113
114
115
116
{
	enum ip_conntrack_info ctinfo;
	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
	unsigned int matchlen, matchoff;

117
118
	if (ct_sip_get_header(ct, *dptr, 0, *datalen, type,
			      &matchoff, &matchlen) <= 0)
119
120
121
122
		return 1;
	return map_addr(skb, dptr, datalen, matchoff, matchlen, map);
}

123
static unsigned int ip_nat_sip(struct sk_buff *skb,
124
			       const char **dptr, unsigned int *datalen)
125
{
126
127
	enum ip_conntrack_info ctinfo;
	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
128
	struct addr_map map;
129
	unsigned int matchoff, matchlen;
130

131
	if (*datalen < strlen("SIP/2.0"))
132
		return NF_ACCEPT;
133
134
135
136

	addr_map_init(ct, &map);

	/* Basic rules: requests and responses. */
137
	if (strnicmp(*dptr, "SIP/2.0", strlen("SIP/2.0")) != 0) {
138
139
140
		if (ct_sip_parse_request(ct, *dptr, *datalen,
					 &matchoff, &matchlen) > 0 &&
		    !map_addr(skb, dptr, datalen, matchoff, matchlen, &map))
141
142
143
			return NF_DROP;
	}

144
145
146
147
	if (!map_sip_addr(skb, dptr, datalen, SIP_HDR_FROM, &map) ||
	    !map_sip_addr(skb, dptr, datalen, SIP_HDR_TO, &map) ||
	    !map_sip_addr(skb, dptr, datalen, SIP_HDR_VIA, &map) ||
	    !map_sip_addr(skb, dptr, datalen, SIP_HDR_CONTACT, &map))
148
149
150
151
		return NF_DROP;
	return NF_ACCEPT;
}

152
153
static int mangle_content_len(struct sk_buff *skb,
			      const char **dptr, unsigned int *datalen)
154
{
155
156
	enum ip_conntrack_info ctinfo;
	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
157
158
159
	unsigned int matchoff, matchlen;
	char buffer[sizeof("65536")];
	int buflen, c_len;
160

161
162
163
164
165
166
167
168
	/* Get actual SDP length */
	if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
				  SDP_HDR_VERSION, SDP_HDR_UNSPEC,
				  &matchoff, &matchlen) <= 0)
		return 0;
	c_len = *datalen - matchoff + strlen("v=");

	/* Now, update SDP length */
169
170
	if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CONTENT_LENGTH,
			      &matchoff, &matchlen) <= 0)
171
172
		return 0;

173
	buflen = sprintf(buffer, "%u", c_len);
174
	return mangle_packet(skb, dptr, datalen, matchoff, matchlen,
175
			     buffer, buflen);
176
177
}

178
179
180
181
static unsigned mangle_sdp_packet(struct sk_buff *skb,
				  const char **dptr, unsigned int *datalen,
				  enum sdp_header_types type,
				  char *buffer, int buflen)
182
{
183
184
	enum ip_conntrack_info ctinfo;
	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
185
	unsigned int matchlen, matchoff;
186

187
188
189
190
191
	if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen, type, SDP_HDR_UNSPEC,
				  &matchoff, &matchlen) <= 0)
		return 0;
	return mangle_packet(skb, dptr, datalen, matchoff, matchlen,
			     buffer, buflen);
192
193
}

194
static unsigned int mangle_sdp(struct sk_buff *skb,
195
196
197
			       enum ip_conntrack_info ctinfo,
			       struct nf_conn *ct,
			       __be32 newip, u_int16_t port,
198
			       const char **dptr, unsigned int *datalen)
199
200
{
	char buffer[sizeof("nnn.nnn.nnn.nnn")];
201
	unsigned int bufflen;
202
203
204

	/* Mangle owner and contact info. */
	bufflen = sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(newip));
205
206
	if (!mangle_sdp_packet(skb, dptr, datalen, SDP_HDR_OWNER_IP4,
			       buffer, bufflen))
207
208
		return 0;

209
210
	if (!mangle_sdp_packet(skb, dptr, datalen, SDP_HDR_CONNECTION_IP4,
			       buffer, bufflen))
211
212
213
214
		return 0;

	/* Mangle media port. */
	bufflen = sprintf(buffer, "%u", port);
215
216
	if (!mangle_sdp_packet(skb, dptr, datalen, SDP_HDR_MEDIA,
			       buffer, bufflen))
217
218
		return 0;

219
	return mangle_content_len(skb, dptr, datalen);
220
221
}

222
223
224
225
226
227
228
229
230
231
232
233
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);

	/* 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;
234
	nf_nat_setup_info(ct, &range, IP_NAT_MANIP_DST);
235
236
237
238
239
240

	/* 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;
	nf_nat_setup_info(ct, &range, IP_NAT_MANIP_SRC);
241
242
}

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

	/* Connection will come from reply */
256
257
258
259
260
	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;
261

262
	exp->saved_ip = exp->tuple.dst.u3.ip;
263
264
265
266
267
268
	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. */
269
	exp->expectfn = ip_nat_sdp_expect;
270
271
272
273

	/* 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);
274
		if (nf_ct_expect_related(exp) == 0)
275
276
277
278
279
280
			break;
	}

	if (port == 0)
		return NF_DROP;

281
	if (!mangle_sdp(skb, ctinfo, ct, newip, port, dptr, datalen)) {
282
		nf_ct_unexpect_related(exp);
283
284
285
286
287
288
289
290
291
292
293
294
295
296
		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)
{
297
298
	BUG_ON(nf_nat_sip_hook != NULL);
	BUG_ON(nf_nat_sdp_hook != NULL);
299
300
301
302
303
304
305
	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);