netfilter: nft_reject: introduce icmp code abstraction for inet and bridge
[deliverable/linux.git] / net / ipv4 / netfilter / nf_nat_h323.c
CommitLineData
f587de0e
PM
1/*
2 * H.323 extension for NAT alteration.
3 *
4 * Copyright (c) 2006 Jing Min Zhao <zhaojingmin@users.sourceforge.net>
f229f6ce 5 * Copyright (c) 2006-2012 Patrick McHardy <kaber@trash.net>
f587de0e
PM
6 *
7 * This source code is licensed under General Public License version 2.
8 *
9 * Based on the 'brute force' H.323 NAT module by
10 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
11 */
12
13#include <linux/module.h>
f587de0e
PM
14#include <linux/tcp.h>
15#include <net/tcp.h>
16
17#include <net/netfilter/nf_nat.h>
18#include <net/netfilter/nf_nat_helper.h>
f587de0e
PM
19#include <net/netfilter/nf_conntrack_helper.h>
20#include <net/netfilter/nf_conntrack_expect.h>
21#include <linux/netfilter/nf_conntrack_h323.h>
22
f587de0e 23/****************************************************************************/
051966c0 24static int set_addr(struct sk_buff *skb, unsigned int protoff,
f587de0e
PM
25 unsigned char **data, int dataoff,
26 unsigned int addroff, __be32 ip, __be16 port)
27{
28 enum ip_conntrack_info ctinfo;
3db05fea 29 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
f587de0e
PM
30 struct {
31 __be32 ip;
32 __be16 port;
33 } __attribute__ ((__packed__)) buf;
905e3e8e
JE
34 const struct tcphdr *th;
35 struct tcphdr _tcph;
f587de0e
PM
36
37 buf.ip = ip;
38 buf.port = port;
39 addroff += dataoff;
40
3db05fea
HX
41 if (ip_hdr(skb)->protocol == IPPROTO_TCP) {
42 if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
051966c0 43 protoff, addroff, sizeof(buf),
f587de0e 44 (char *) &buf, sizeof(buf))) {
e87cc472 45 net_notice_ratelimited("nf_nat_h323: nf_nat_mangle_tcp_packet error\n");
f587de0e
PM
46 return -1;
47 }
48
49 /* Relocate data pointer */
3db05fea 50 th = skb_header_pointer(skb, ip_hdrlen(skb),
f587de0e
PM
51 sizeof(_tcph), &_tcph);
52 if (th == NULL)
53 return -1;
3db05fea 54 *data = skb->data + ip_hdrlen(skb) + th->doff * 4 + dataoff;
f587de0e 55 } else {
3db05fea 56 if (!nf_nat_mangle_udp_packet(skb, ct, ctinfo,
051966c0 57 protoff, addroff, sizeof(buf),
f587de0e 58 (char *) &buf, sizeof(buf))) {
e87cc472 59 net_notice_ratelimited("nf_nat_h323: nf_nat_mangle_udp_packet error\n");
f587de0e
PM
60 return -1;
61 }
62 /* nf_nat_mangle_udp_packet uses skb_make_writable() to copy
63 * or pull everything in a linear buffer, so we can safely
64 * use the skb pointers now */
3db05fea 65 *data = skb->data + ip_hdrlen(skb) + sizeof(struct udphdr);
f587de0e
PM
66 }
67
68 return 0;
69}
70
71/****************************************************************************/
051966c0 72static int set_h225_addr(struct sk_buff *skb, unsigned int protoff,
f587de0e
PM
73 unsigned char **data, int dataoff,
74 TransportAddress *taddr,
643a2c15 75 union nf_inet_addr *addr, __be16 port)
f587de0e 76{
051966c0 77 return set_addr(skb, protoff, data, dataoff, taddr->ipAddress.ip,
f587de0e
PM
78 addr->ip, port);
79}
80
81/****************************************************************************/
051966c0 82static int set_h245_addr(struct sk_buff *skb, unsigned protoff,
f587de0e
PM
83 unsigned char **data, int dataoff,
84 H245_TransportAddress *taddr,
643a2c15 85 union nf_inet_addr *addr, __be16 port)
f587de0e 86{
051966c0 87 return set_addr(skb, protoff, data, dataoff,
f587de0e
PM
88 taddr->unicastAddress.iPAddress.network,
89 addr->ip, port);
90}
91
92/****************************************************************************/
3db05fea 93static int set_sig_addr(struct sk_buff *skb, struct nf_conn *ct,
f587de0e 94 enum ip_conntrack_info ctinfo,
051966c0 95 unsigned int protoff, unsigned char **data,
f587de0e
PM
96 TransportAddress *taddr, int count)
97{
1afc5679 98 const struct nf_ct_h323_master *info = nfct_help_data(ct);
f587de0e
PM
99 int dir = CTINFO2DIR(ctinfo);
100 int i;
101 __be16 port;
643a2c15 102 union nf_inet_addr addr;
f587de0e
PM
103
104 for (i = 0; i < count; i++) {
105 if (get_h225_addr(ct, *data, &taddr[i], &addr, &port)) {
106 if (addr.ip == ct->tuplehash[dir].tuple.src.u3.ip &&
107 port == info->sig_port[dir]) {
108 /* GW->GK */
109
110 /* Fix for Gnomemeeting */
111 if (i > 0 &&
112 get_h225_addr(ct, *data, &taddr[0],
113 &addr, &port) &&
114 (ntohl(addr.ip) & 0xff000000) == 0x7f000000)
115 i = 0;
116
cffee385
HH
117 pr_debug("nf_nat_ras: set signal address %pI4:%hu->%pI4:%hu\n",
118 &addr.ip, port,
119 &ct->tuplehash[!dir].tuple.dst.u3.ip,
0d53778e 120 info->sig_port[!dir]);
051966c0
PM
121 return set_h225_addr(skb, protoff, data, 0,
122 &taddr[i],
f587de0e
PM
123 &ct->tuplehash[!dir].
124 tuple.dst.u3,
125 info->sig_port[!dir]);
126 } else if (addr.ip == ct->tuplehash[dir].tuple.dst.u3.ip &&
127 port == info->sig_port[dir]) {
128 /* GK->GW */
cffee385
HH
129 pr_debug("nf_nat_ras: set signal address %pI4:%hu->%pI4:%hu\n",
130 &addr.ip, port,
131 &ct->tuplehash[!dir].tuple.src.u3.ip,
0d53778e 132 info->sig_port[!dir]);
051966c0
PM
133 return set_h225_addr(skb, protoff, data, 0,
134 &taddr[i],
f587de0e
PM
135 &ct->tuplehash[!dir].
136 tuple.src.u3,
137 info->sig_port[!dir]);
138 }
139 }
140 }
141
142 return 0;
143}
144
145/****************************************************************************/
3db05fea 146static int set_ras_addr(struct sk_buff *skb, struct nf_conn *ct,
f587de0e 147 enum ip_conntrack_info ctinfo,
051966c0 148 unsigned int protoff, unsigned char **data,
f587de0e
PM
149 TransportAddress *taddr, int count)
150{
151 int dir = CTINFO2DIR(ctinfo);
152 int i;
153 __be16 port;
643a2c15 154 union nf_inet_addr addr;
f587de0e
PM
155
156 for (i = 0; i < count; i++) {
157 if (get_h225_addr(ct, *data, &taddr[i], &addr, &port) &&
158 addr.ip == ct->tuplehash[dir].tuple.src.u3.ip &&
159 port == ct->tuplehash[dir].tuple.src.u.udp.port) {
cffee385
HH
160 pr_debug("nf_nat_ras: set rasAddress %pI4:%hu->%pI4:%hu\n",
161 &addr.ip, ntohs(port),
162 &ct->tuplehash[!dir].tuple.dst.u3.ip,
0d53778e 163 ntohs(ct->tuplehash[!dir].tuple.dst.u.udp.port));
051966c0 164 return set_h225_addr(skb, protoff, data, 0, &taddr[i],
f587de0e
PM
165 &ct->tuplehash[!dir].tuple.dst.u3,
166 ct->tuplehash[!dir].tuple.
167 dst.u.udp.port);
168 }
169 }
170
171 return 0;
172}
173
174/****************************************************************************/
3db05fea 175static int nat_rtp_rtcp(struct sk_buff *skb, struct nf_conn *ct,
f587de0e 176 enum ip_conntrack_info ctinfo,
051966c0 177 unsigned int protoff, unsigned char **data, int dataoff,
f587de0e
PM
178 H245_TransportAddress *taddr,
179 __be16 port, __be16 rtp_port,
180 struct nf_conntrack_expect *rtp_exp,
181 struct nf_conntrack_expect *rtcp_exp)
182{
1afc5679 183 struct nf_ct_h323_master *info = nfct_help_data(ct);
f587de0e
PM
184 int dir = CTINFO2DIR(ctinfo);
185 int i;
186 u_int16_t nated_port;
187
188 /* Set expectations for NAT */
189 rtp_exp->saved_proto.udp.port = rtp_exp->tuple.dst.u.udp.port;
190 rtp_exp->expectfn = nf_nat_follow_master;
191 rtp_exp->dir = !dir;
192 rtcp_exp->saved_proto.udp.port = rtcp_exp->tuple.dst.u.udp.port;
193 rtcp_exp->expectfn = nf_nat_follow_master;
194 rtcp_exp->dir = !dir;
195
196 /* Lookup existing expects */
197 for (i = 0; i < H323_RTP_CHANNEL_MAX; i++) {
198 if (info->rtp_port[i][dir] == rtp_port) {
199 /* Expected */
200
201 /* Use allocated ports first. This will refresh
202 * the expects */
203 rtp_exp->tuple.dst.u.udp.port = info->rtp_port[i][dir];
204 rtcp_exp->tuple.dst.u.udp.port =
205 htons(ntohs(info->rtp_port[i][dir]) + 1);
206 break;
207 } else if (info->rtp_port[i][dir] == 0) {
208 /* Not expected */
209 break;
210 }
211 }
212
213 /* Run out of expectations */
214 if (i >= H323_RTP_CHANNEL_MAX) {
e87cc472 215 net_notice_ratelimited("nf_nat_h323: out of expectations\n");
f587de0e
PM
216 return 0;
217 }
218
219 /* Try to get a pair of ports. */
220 for (nated_port = ntohs(rtp_exp->tuple.dst.u.udp.port);
221 nated_port != 0; nated_port += 2) {
5b92b61f
PNA
222 int ret;
223
f587de0e 224 rtp_exp->tuple.dst.u.udp.port = htons(nated_port);
5b92b61f
PNA
225 ret = nf_ct_expect_related(rtp_exp);
226 if (ret == 0) {
f587de0e
PM
227 rtcp_exp->tuple.dst.u.udp.port =
228 htons(nated_port + 1);
5b92b61f
PNA
229 ret = nf_ct_expect_related(rtcp_exp);
230 if (ret == 0)
231 break;
829d9315
AD
232 else if (ret == -EBUSY) {
233 nf_ct_unexpect_related(rtp_exp);
234 continue;
235 } else if (ret < 0) {
5b92b61f
PNA
236 nf_ct_unexpect_related(rtp_exp);
237 nated_port = 0;
f587de0e 238 break;
5b92b61f
PNA
239 }
240 } else if (ret != -EBUSY) {
241 nated_port = 0;
242 break;
f587de0e
PM
243 }
244 }
245
246 if (nated_port == 0) { /* No port available */
e87cc472 247 net_notice_ratelimited("nf_nat_h323: out of RTP ports\n");
f587de0e
PM
248 return 0;
249 }
250
251 /* Modify signal */
051966c0 252 if (set_h245_addr(skb, protoff, data, dataoff, taddr,
f587de0e
PM
253 &ct->tuplehash[!dir].tuple.dst.u3,
254 htons((port & htons(1)) ? nated_port + 1 :
e905a9ed 255 nated_port)) == 0) {
f587de0e
PM
256 /* Save ports */
257 info->rtp_port[i][dir] = rtp_port;
258 info->rtp_port[i][!dir] = htons(nated_port);
259 } else {
6823645d
PM
260 nf_ct_unexpect_related(rtp_exp);
261 nf_ct_unexpect_related(rtcp_exp);
f587de0e
PM
262 return -1;
263 }
264
265 /* Success */
cffee385
HH
266 pr_debug("nf_nat_h323: expect RTP %pI4:%hu->%pI4:%hu\n",
267 &rtp_exp->tuple.src.u3.ip,
0d53778e 268 ntohs(rtp_exp->tuple.src.u.udp.port),
cffee385 269 &rtp_exp->tuple.dst.u3.ip,
0d53778e 270 ntohs(rtp_exp->tuple.dst.u.udp.port));
cffee385
HH
271 pr_debug("nf_nat_h323: expect RTCP %pI4:%hu->%pI4:%hu\n",
272 &rtcp_exp->tuple.src.u3.ip,
0d53778e 273 ntohs(rtcp_exp->tuple.src.u.udp.port),
cffee385 274 &rtcp_exp->tuple.dst.u3.ip,
0d53778e 275 ntohs(rtcp_exp->tuple.dst.u.udp.port));
f587de0e
PM
276
277 return 0;
278}
279
280/****************************************************************************/
3db05fea 281static int nat_t120(struct sk_buff *skb, struct nf_conn *ct,
f587de0e 282 enum ip_conntrack_info ctinfo,
051966c0 283 unsigned int protoff, unsigned char **data, int dataoff,
f587de0e
PM
284 H245_TransportAddress *taddr, __be16 port,
285 struct nf_conntrack_expect *exp)
286{
287 int dir = CTINFO2DIR(ctinfo);
288 u_int16_t nated_port = ntohs(port);
289
290 /* Set expectations for NAT */
291 exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
292 exp->expectfn = nf_nat_follow_master;
293 exp->dir = !dir;
294
295 /* Try to get same port: if not, try to change it. */
296 for (; nated_port != 0; nated_port++) {
5b92b61f
PNA
297 int ret;
298
f587de0e 299 exp->tuple.dst.u.tcp.port = htons(nated_port);
5b92b61f
PNA
300 ret = nf_ct_expect_related(exp);
301 if (ret == 0)
302 break;
303 else if (ret != -EBUSY) {
304 nated_port = 0;
f587de0e 305 break;
5b92b61f 306 }
f587de0e
PM
307 }
308
309 if (nated_port == 0) { /* No port available */
e87cc472 310 net_notice_ratelimited("nf_nat_h323: out of TCP ports\n");
f587de0e
PM
311 return 0;
312 }
313
314 /* Modify signal */
051966c0 315 if (set_h245_addr(skb, protoff, data, dataoff, taddr,
f587de0e
PM
316 &ct->tuplehash[!dir].tuple.dst.u3,
317 htons(nated_port)) < 0) {
6823645d 318 nf_ct_unexpect_related(exp);
f587de0e
PM
319 return -1;
320 }
321
cffee385
HH
322 pr_debug("nf_nat_h323: expect T.120 %pI4:%hu->%pI4:%hu\n",
323 &exp->tuple.src.u3.ip,
0d53778e 324 ntohs(exp->tuple.src.u.tcp.port),
cffee385 325 &exp->tuple.dst.u3.ip,
0d53778e 326 ntohs(exp->tuple.dst.u.tcp.port));
f587de0e
PM
327
328 return 0;
329}
330
331/****************************************************************************/
3db05fea 332static int nat_h245(struct sk_buff *skb, struct nf_conn *ct,
f587de0e 333 enum ip_conntrack_info ctinfo,
051966c0 334 unsigned int protoff, unsigned char **data, int dataoff,
f587de0e
PM
335 TransportAddress *taddr, __be16 port,
336 struct nf_conntrack_expect *exp)
337{
1afc5679 338 struct nf_ct_h323_master *info = nfct_help_data(ct);
f587de0e
PM
339 int dir = CTINFO2DIR(ctinfo);
340 u_int16_t nated_port = ntohs(port);
341
342 /* Set expectations for NAT */
343 exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
344 exp->expectfn = nf_nat_follow_master;
345 exp->dir = !dir;
346
347 /* Check existing expects */
348 if (info->sig_port[dir] == port)
349 nated_port = ntohs(info->sig_port[!dir]);
350
351 /* Try to get same port: if not, try to change it. */
352 for (; nated_port != 0; nated_port++) {
5b92b61f
PNA
353 int ret;
354
f587de0e 355 exp->tuple.dst.u.tcp.port = htons(nated_port);
5b92b61f
PNA
356 ret = nf_ct_expect_related(exp);
357 if (ret == 0)
f587de0e 358 break;
5b92b61f
PNA
359 else if (ret != -EBUSY) {
360 nated_port = 0;
361 break;
362 }
f587de0e
PM
363 }
364
365 if (nated_port == 0) { /* No port available */
e87cc472 366 net_notice_ratelimited("nf_nat_q931: out of TCP ports\n");
f587de0e
PM
367 return 0;
368 }
369
370 /* Modify signal */
051966c0 371 if (set_h225_addr(skb, protoff, data, dataoff, taddr,
f587de0e
PM
372 &ct->tuplehash[!dir].tuple.dst.u3,
373 htons(nated_port)) == 0) {
374 /* Save ports */
375 info->sig_port[dir] = port;
376 info->sig_port[!dir] = htons(nated_port);
377 } else {
6823645d 378 nf_ct_unexpect_related(exp);
f587de0e
PM
379 return -1;
380 }
381
cffee385
HH
382 pr_debug("nf_nat_q931: expect H.245 %pI4:%hu->%pI4:%hu\n",
383 &exp->tuple.src.u3.ip,
0d53778e 384 ntohs(exp->tuple.src.u.tcp.port),
cffee385 385 &exp->tuple.dst.u3.ip,
0d53778e 386 ntohs(exp->tuple.dst.u.tcp.port));
f587de0e
PM
387
388 return 0;
389}
390
391/****************************************************************************
392 * This conntrack expect function replaces nf_conntrack_q931_expect()
393 * which was set by nf_conntrack_h323.c.
394 ****************************************************************************/
395static void ip_nat_q931_expect(struct nf_conn *new,
396 struct nf_conntrack_expect *this)
397{
c7232c99 398 struct nf_nat_range range;
f587de0e
PM
399
400 if (this->tuple.src.u3.ip != 0) { /* Only accept calls from GK */
401 nf_nat_follow_master(new, this);
402 return;
403 }
404
405 /* This must be a fresh one. */
406 BUG_ON(new->status & IPS_NAT_DONE_MASK);
407
408 /* Change src to where master sends to */
cbc9f2f4 409 range.flags = NF_NAT_RANGE_MAP_IPS;
c7232c99
PM
410 range.min_addr = range.max_addr =
411 new->tuplehash[!this->dir].tuple.src.u3;
cbc9f2f4 412 nf_nat_setup_info(new, &range, NF_NAT_MANIP_SRC);
f587de0e
PM
413
414 /* For DST manip, map port here to where it's expected. */
cbc9f2f4 415 range.flags = (NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED);
c7232c99
PM
416 range.min_proto = range.max_proto = this->saved_proto;
417 range.min_addr = range.max_addr =
418 new->master->tuplehash[!this->dir].tuple.src.u3;
cbc9f2f4 419 nf_nat_setup_info(new, &range, NF_NAT_MANIP_DST);
f587de0e
PM
420}
421
422/****************************************************************************/
3db05fea 423static int nat_q931(struct sk_buff *skb, struct nf_conn *ct,
f587de0e 424 enum ip_conntrack_info ctinfo,
051966c0
PM
425 unsigned int protoff, unsigned char **data,
426 TransportAddress *taddr, int idx,
f587de0e
PM
427 __be16 port, struct nf_conntrack_expect *exp)
428{
1afc5679 429 struct nf_ct_h323_master *info = nfct_help_data(ct);
f587de0e
PM
430 int dir = CTINFO2DIR(ctinfo);
431 u_int16_t nated_port = ntohs(port);
643a2c15 432 union nf_inet_addr addr;
f587de0e
PM
433
434 /* Set expectations for NAT */
435 exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
436 exp->expectfn = ip_nat_q931_expect;
437 exp->dir = !dir;
438
439 /* Check existing expects */
440 if (info->sig_port[dir] == port)
441 nated_port = ntohs(info->sig_port[!dir]);
442
443 /* Try to get same port: if not, try to change it. */
444 for (; nated_port != 0; nated_port++) {
5b92b61f
PNA
445 int ret;
446
f587de0e 447 exp->tuple.dst.u.tcp.port = htons(nated_port);
5b92b61f
PNA
448 ret = nf_ct_expect_related(exp);
449 if (ret == 0)
450 break;
451 else if (ret != -EBUSY) {
452 nated_port = 0;
f587de0e 453 break;
5b92b61f 454 }
f587de0e
PM
455 }
456
457 if (nated_port == 0) { /* No port available */
e87cc472 458 net_notice_ratelimited("nf_nat_ras: out of TCP ports\n");
f587de0e
PM
459 return 0;
460 }
461
462 /* Modify signal */
051966c0 463 if (set_h225_addr(skb, protoff, data, 0, &taddr[idx],
f587de0e
PM
464 &ct->tuplehash[!dir].tuple.dst.u3,
465 htons(nated_port)) == 0) {
466 /* Save ports */
467 info->sig_port[dir] = port;
468 info->sig_port[!dir] = htons(nated_port);
469
470 /* Fix for Gnomemeeting */
471 if (idx > 0 &&
472 get_h225_addr(ct, *data, &taddr[0], &addr, &port) &&
473 (ntohl(addr.ip) & 0xff000000) == 0x7f000000) {
051966c0 474 set_h225_addr(skb, protoff, data, 0, &taddr[0],
1ff75ed2
JMZ
475 &ct->tuplehash[!dir].tuple.dst.u3,
476 info->sig_port[!dir]);
f587de0e
PM
477 }
478 } else {
6823645d 479 nf_ct_unexpect_related(exp);
f587de0e
PM
480 return -1;
481 }
482
483 /* Success */
cffee385
HH
484 pr_debug("nf_nat_ras: expect Q.931 %pI4:%hu->%pI4:%hu\n",
485 &exp->tuple.src.u3.ip,
0d53778e 486 ntohs(exp->tuple.src.u.tcp.port),
cffee385 487 &exp->tuple.dst.u3.ip,
0d53778e 488 ntohs(exp->tuple.dst.u.tcp.port));
f587de0e
PM
489
490 return 0;
491}
492
493/****************************************************************************/
494static void ip_nat_callforwarding_expect(struct nf_conn *new,
495 struct nf_conntrack_expect *this)
496{
c7232c99 497 struct nf_nat_range range;
f587de0e
PM
498
499 /* This must be a fresh one. */
500 BUG_ON(new->status & IPS_NAT_DONE_MASK);
501
502 /* Change src to where master sends to */
cbc9f2f4 503 range.flags = NF_NAT_RANGE_MAP_IPS;
c7232c99
PM
504 range.min_addr = range.max_addr =
505 new->tuplehash[!this->dir].tuple.src.u3;
cbc9f2f4 506 nf_nat_setup_info(new, &range, NF_NAT_MANIP_SRC);
f587de0e
PM
507
508 /* For DST manip, map port here to where it's expected. */
cbc9f2f4 509 range.flags = (NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED);
c7232c99
PM
510 range.min_proto = range.max_proto = this->saved_proto;
511 range.min_addr = range.max_addr = this->saved_addr;
cbc9f2f4 512 nf_nat_setup_info(new, &range, NF_NAT_MANIP_DST);
f587de0e
PM
513}
514
515/****************************************************************************/
3db05fea 516static int nat_callforwarding(struct sk_buff *skb, struct nf_conn *ct,
f587de0e 517 enum ip_conntrack_info ctinfo,
051966c0 518 unsigned int protoff,
f587de0e
PM
519 unsigned char **data, int dataoff,
520 TransportAddress *taddr, __be16 port,
521 struct nf_conntrack_expect *exp)
522{
523 int dir = CTINFO2DIR(ctinfo);
524 u_int16_t nated_port;
525
526 /* Set expectations for NAT */
c7232c99 527 exp->saved_addr = exp->tuple.dst.u3;
f587de0e
PM
528 exp->tuple.dst.u3.ip = ct->tuplehash[!dir].tuple.dst.u3.ip;
529 exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
530 exp->expectfn = ip_nat_callforwarding_expect;
531 exp->dir = !dir;
532
533 /* Try to get same port: if not, try to change it. */
534 for (nated_port = ntohs(port); nated_port != 0; nated_port++) {
5b92b61f
PNA
535 int ret;
536
f587de0e 537 exp->tuple.dst.u.tcp.port = htons(nated_port);
5b92b61f
PNA
538 ret = nf_ct_expect_related(exp);
539 if (ret == 0)
f587de0e 540 break;
5b92b61f
PNA
541 else if (ret != -EBUSY) {
542 nated_port = 0;
543 break;
544 }
f587de0e
PM
545 }
546
547 if (nated_port == 0) { /* No port available */
e87cc472 548 net_notice_ratelimited("nf_nat_q931: out of TCP ports\n");
f587de0e
PM
549 return 0;
550 }
551
552 /* Modify signal */
051966c0 553 if (!set_h225_addr(skb, protoff, data, dataoff, taddr,
f587de0e
PM
554 &ct->tuplehash[!dir].tuple.dst.u3,
555 htons(nated_port)) == 0) {
6823645d 556 nf_ct_unexpect_related(exp);
f587de0e
PM
557 return -1;
558 }
559
560 /* Success */
cffee385
HH
561 pr_debug("nf_nat_q931: expect Call Forwarding %pI4:%hu->%pI4:%hu\n",
562 &exp->tuple.src.u3.ip,
0d53778e 563 ntohs(exp->tuple.src.u.tcp.port),
cffee385 564 &exp->tuple.dst.u3.ip,
0d53778e 565 ntohs(exp->tuple.dst.u.tcp.port));
f587de0e
PM
566
567 return 0;
568}
569
544d5c7d
PNA
570static struct nf_ct_helper_expectfn q931_nat = {
571 .name = "Q.931",
572 .expectfn = ip_nat_q931_expect,
573};
574
575static struct nf_ct_helper_expectfn callforwarding_nat = {
576 .name = "callforwarding",
577 .expectfn = ip_nat_callforwarding_expect,
578};
579
f587de0e
PM
580/****************************************************************************/
581static int __init init(void)
582{
d1332e0a
PM
583 BUG_ON(set_h245_addr_hook != NULL);
584 BUG_ON(set_h225_addr_hook != NULL);
585 BUG_ON(set_sig_addr_hook != NULL);
586 BUG_ON(set_ras_addr_hook != NULL);
587 BUG_ON(nat_rtp_rtcp_hook != NULL);
588 BUG_ON(nat_t120_hook != NULL);
589 BUG_ON(nat_h245_hook != NULL);
590 BUG_ON(nat_callforwarding_hook != NULL);
591 BUG_ON(nat_q931_hook != NULL);
f587de0e 592
a9b3cd7f
SH
593 RCU_INIT_POINTER(set_h245_addr_hook, set_h245_addr);
594 RCU_INIT_POINTER(set_h225_addr_hook, set_h225_addr);
595 RCU_INIT_POINTER(set_sig_addr_hook, set_sig_addr);
596 RCU_INIT_POINTER(set_ras_addr_hook, set_ras_addr);
597 RCU_INIT_POINTER(nat_rtp_rtcp_hook, nat_rtp_rtcp);
598 RCU_INIT_POINTER(nat_t120_hook, nat_t120);
599 RCU_INIT_POINTER(nat_h245_hook, nat_h245);
600 RCU_INIT_POINTER(nat_callforwarding_hook, nat_callforwarding);
601 RCU_INIT_POINTER(nat_q931_hook, nat_q931);
544d5c7d
PNA
602 nf_ct_helper_expectfn_register(&q931_nat);
603 nf_ct_helper_expectfn_register(&callforwarding_nat);
f587de0e
PM
604 return 0;
605}
606
607/****************************************************************************/
608static void __exit fini(void)
609{
a9b3cd7f
SH
610 RCU_INIT_POINTER(set_h245_addr_hook, NULL);
611 RCU_INIT_POINTER(set_h225_addr_hook, NULL);
612 RCU_INIT_POINTER(set_sig_addr_hook, NULL);
613 RCU_INIT_POINTER(set_ras_addr_hook, NULL);
614 RCU_INIT_POINTER(nat_rtp_rtcp_hook, NULL);
615 RCU_INIT_POINTER(nat_t120_hook, NULL);
616 RCU_INIT_POINTER(nat_h245_hook, NULL);
617 RCU_INIT_POINTER(nat_callforwarding_hook, NULL);
618 RCU_INIT_POINTER(nat_q931_hook, NULL);
544d5c7d
PNA
619 nf_ct_helper_expectfn_unregister(&q931_nat);
620 nf_ct_helper_expectfn_unregister(&callforwarding_nat);
f587de0e
PM
621 synchronize_rcu();
622}
623
624/****************************************************************************/
625module_init(init);
626module_exit(fini);
627
628MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>");
629MODULE_DESCRIPTION("H.323 NAT helper");
630MODULE_LICENSE("GPL");
631MODULE_ALIAS("ip_nat_h323");
This page took 0.620737 seconds and 5 git commands to generate.