2 * H.323 connection tracking helper
4 * Copyright (c) 2006 Jing Min Zhao <zhaojingmin@users.sourceforge.net>
5 * Copyright (c) 2006-2012 Patrick McHardy <kaber@trash.net>
7 * This source code is licensed under General Public License version 2.
9 * Based on the 'brute force' H.323 connection tracking module by
10 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 * For more information, please see http://nath323.sourceforge.net/
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/ctype.h>
18 #include <linux/inet.h>
21 #include <linux/slab.h>
22 #include <linux/udp.h>
23 #include <linux/tcp.h>
24 #include <linux/skbuff.h>
25 #include <net/route.h>
26 #include <net/ip6_route.h>
28 #include <net/netfilter/nf_conntrack.h>
29 #include <net/netfilter/nf_conntrack_core.h>
30 #include <net/netfilter/nf_conntrack_tuple.h>
31 #include <net/netfilter/nf_conntrack_expect.h>
32 #include <net/netfilter/nf_conntrack_ecache.h>
33 #include <net/netfilter/nf_conntrack_helper.h>
34 #include <net/netfilter/nf_conntrack_zones.h>
35 #include <linux/netfilter/nf_conntrack_h323.h>
38 static unsigned int default_rrq_ttl __read_mostly
= 300;
39 module_param(default_rrq_ttl
, uint
, 0600);
40 MODULE_PARM_DESC(default_rrq_ttl
, "use this TTL if it's missing in RRQ");
42 static int gkrouted_only __read_mostly
= 1;
43 module_param(gkrouted_only
, int, 0600);
44 MODULE_PARM_DESC(gkrouted_only
, "only accept calls from gatekeeper");
46 static bool callforward_filter __read_mostly
= true;
47 module_param(callforward_filter
, bool, 0600);
48 MODULE_PARM_DESC(callforward_filter
, "only create call forwarding expectations "
49 "if both endpoints are on different sides "
50 "(determined by routing information)");
53 int (*set_h245_addr_hook
) (struct sk_buff
*skb
, unsigned int protoff
,
54 unsigned char **data
, int dataoff
,
55 H245_TransportAddress
*taddr
,
56 union nf_inet_addr
*addr
, __be16 port
)
58 int (*set_h225_addr_hook
) (struct sk_buff
*skb
, unsigned int protoff
,
59 unsigned char **data
, int dataoff
,
60 TransportAddress
*taddr
,
61 union nf_inet_addr
*addr
, __be16 port
)
63 int (*set_sig_addr_hook
) (struct sk_buff
*skb
,
65 enum ip_conntrack_info ctinfo
,
66 unsigned int protoff
, unsigned char **data
,
67 TransportAddress
*taddr
, int count
) __read_mostly
;
68 int (*set_ras_addr_hook
) (struct sk_buff
*skb
,
70 enum ip_conntrack_info ctinfo
,
71 unsigned int protoff
, unsigned char **data
,
72 TransportAddress
*taddr
, int count
) __read_mostly
;
73 int (*nat_rtp_rtcp_hook
) (struct sk_buff
*skb
,
75 enum ip_conntrack_info ctinfo
,
77 unsigned char **data
, int dataoff
,
78 H245_TransportAddress
*taddr
,
79 __be16 port
, __be16 rtp_port
,
80 struct nf_conntrack_expect
*rtp_exp
,
81 struct nf_conntrack_expect
*rtcp_exp
) __read_mostly
;
82 int (*nat_t120_hook
) (struct sk_buff
*skb
,
84 enum ip_conntrack_info ctinfo
,
86 unsigned char **data
, int dataoff
,
87 H245_TransportAddress
*taddr
, __be16 port
,
88 struct nf_conntrack_expect
*exp
) __read_mostly
;
89 int (*nat_h245_hook
) (struct sk_buff
*skb
,
91 enum ip_conntrack_info ctinfo
,
93 unsigned char **data
, int dataoff
,
94 TransportAddress
*taddr
, __be16 port
,
95 struct nf_conntrack_expect
*exp
) __read_mostly
;
96 int (*nat_callforwarding_hook
) (struct sk_buff
*skb
,
98 enum ip_conntrack_info ctinfo
,
100 unsigned char **data
, int dataoff
,
101 TransportAddress
*taddr
, __be16 port
,
102 struct nf_conntrack_expect
*exp
) __read_mostly
;
103 int (*nat_q931_hook
) (struct sk_buff
*skb
,
105 enum ip_conntrack_info ctinfo
,
106 unsigned int protoff
,
107 unsigned char **data
, TransportAddress
*taddr
, int idx
,
108 __be16 port
, struct nf_conntrack_expect
*exp
)
111 static DEFINE_SPINLOCK(nf_h323_lock
);
112 static char *h323_buffer
;
114 static struct nf_conntrack_helper nf_conntrack_helper_h245
;
115 static struct nf_conntrack_helper nf_conntrack_helper_q931
[];
116 static struct nf_conntrack_helper nf_conntrack_helper_ras
[];
118 /****************************************************************************/
119 static int get_tpkt_data(struct sk_buff
*skb
, unsigned int protoff
,
120 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
,
121 unsigned char **data
, int *datalen
, int *dataoff
)
123 struct nf_ct_h323_master
*info
= nfct_help_data(ct
);
124 int dir
= CTINFO2DIR(ctinfo
);
125 const struct tcphdr
*th
;
134 th
= skb_header_pointer(skb
, protoff
, sizeof(_tcph
), &_tcph
);
138 /* Get TCP data offset */
139 tcpdataoff
= protoff
+ th
->doff
* 4;
141 /* Get TCP data length */
142 tcpdatalen
= skb
->len
- tcpdataoff
;
143 if (tcpdatalen
<= 0) /* No TCP data */
146 if (*data
== NULL
) { /* first TPKT */
147 /* Get first TPKT pointer */
148 tpkt
= skb_header_pointer(skb
, tcpdataoff
, tcpdatalen
,
150 BUG_ON(tpkt
== NULL
);
152 /* Validate TPKT identifier */
153 if (tcpdatalen
< 4 || tpkt
[0] != 0x03 || tpkt
[1] != 0) {
154 /* Netmeeting sends TPKT header and data separately */
155 if (info
->tpkt_len
[dir
] > 0) {
156 pr_debug("nf_ct_h323: previous packet "
157 "indicated separate TPKT data of %hu "
158 "bytes\n", info
->tpkt_len
[dir
]);
159 if (info
->tpkt_len
[dir
] <= tcpdatalen
) {
160 /* Yes, there was a TPKT header
163 *datalen
= info
->tpkt_len
[dir
];
168 /* Fragmented TPKT */
169 pr_debug("nf_ct_h323: fragmented TPKT\n");
173 /* It is not even a TPKT */
177 } else { /* Next TPKT */
178 tpktoff
= *dataoff
+ *datalen
;
179 tcpdatalen
-= tpktoff
;
180 if (tcpdatalen
<= 4) /* No more TPKT */
182 tpkt
= *data
+ *datalen
;
184 /* Validate TPKT identifier */
185 if (tpkt
[0] != 0x03 || tpkt
[1] != 0)
189 /* Validate TPKT length */
190 tpktlen
= tpkt
[2] * 256 + tpkt
[3];
193 if (tpktlen
> tcpdatalen
) {
194 if (tcpdatalen
== 4) { /* Separate TPKT header */
195 /* Netmeeting sends TPKT header and data separately */
196 pr_debug("nf_ct_h323: separate TPKT header indicates "
197 "there will be TPKT data of %hu bytes\n",
199 info
->tpkt_len
[dir
] = tpktlen
- 4;
203 pr_debug("nf_ct_h323: incomplete TPKT (fragmented?)\n");
207 /* This is the encapsulated data */
209 *datalen
= tpktlen
- 4;
210 *dataoff
= tpktoff
+ 4;
213 /* Clear TPKT length */
214 info
->tpkt_len
[dir
] = 0;
218 info
->tpkt_len
[dir
] = 0;
222 /****************************************************************************/
223 static int get_h245_addr(struct nf_conn
*ct
, const unsigned char *data
,
224 H245_TransportAddress
*taddr
,
225 union nf_inet_addr
*addr
, __be16
*port
)
227 const unsigned char *p
;
230 if (taddr
->choice
!= eH245_TransportAddress_unicastAddress
)
233 switch (taddr
->unicastAddress
.choice
) {
234 case eUnicastAddress_iPAddress
:
235 if (nf_ct_l3num(ct
) != AF_INET
)
237 p
= data
+ taddr
->unicastAddress
.iPAddress
.network
;
240 case eUnicastAddress_iP6Address
:
241 if (nf_ct_l3num(ct
) != AF_INET6
)
243 p
= data
+ taddr
->unicastAddress
.iP6Address
.network
;
250 memcpy(addr
, p
, len
);
251 memset((void *)addr
+ len
, 0, sizeof(*addr
) - len
);
252 memcpy(port
, p
+ len
, sizeof(__be16
));
257 /****************************************************************************/
258 static int expect_rtp_rtcp(struct sk_buff
*skb
, struct nf_conn
*ct
,
259 enum ip_conntrack_info ctinfo
,
260 unsigned int protoff
,
261 unsigned char **data
, int dataoff
,
262 H245_TransportAddress
*taddr
)
264 int dir
= CTINFO2DIR(ctinfo
);
267 __be16 rtp_port
, rtcp_port
;
268 union nf_inet_addr addr
;
269 struct nf_conntrack_expect
*rtp_exp
;
270 struct nf_conntrack_expect
*rtcp_exp
;
271 typeof(nat_rtp_rtcp_hook
) nat_rtp_rtcp
;
273 /* Read RTP or RTCP address */
274 if (!get_h245_addr(ct
, *data
, taddr
, &addr
, &port
) ||
275 memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) ||
279 /* RTP port is even */
280 rtp_port
= port
& ~htons(1);
281 rtcp_port
= port
| htons(1);
283 /* Create expect for RTP */
284 if ((rtp_exp
= nf_ct_expect_alloc(ct
)) == NULL
)
286 nf_ct_expect_init(rtp_exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
287 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
288 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
289 IPPROTO_UDP
, NULL
, &rtp_port
);
291 /* Create expect for RTCP */
292 if ((rtcp_exp
= nf_ct_expect_alloc(ct
)) == NULL
) {
293 nf_ct_expect_put(rtp_exp
);
296 nf_ct_expect_init(rtcp_exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
297 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
298 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
299 IPPROTO_UDP
, NULL
, &rtcp_port
);
301 if (memcmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
,
302 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
303 sizeof(ct
->tuplehash
[dir
].tuple
.src
.u3
)) &&
304 (nat_rtp_rtcp
= rcu_dereference(nat_rtp_rtcp_hook
)) &&
305 nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
306 ct
->status
& IPS_NAT_MASK
) {
308 ret
= nat_rtp_rtcp(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
309 taddr
, port
, rtp_port
, rtp_exp
, rtcp_exp
);
310 } else { /* Conntrack only */
311 if (nf_ct_expect_related(rtp_exp
) == 0) {
312 if (nf_ct_expect_related(rtcp_exp
) == 0) {
313 pr_debug("nf_ct_h323: expect RTP ");
314 nf_ct_dump_tuple(&rtp_exp
->tuple
);
315 pr_debug("nf_ct_h323: expect RTCP ");
316 nf_ct_dump_tuple(&rtcp_exp
->tuple
);
318 nf_ct_unexpect_related(rtp_exp
);
325 nf_ct_expect_put(rtp_exp
);
326 nf_ct_expect_put(rtcp_exp
);
331 /****************************************************************************/
332 static int expect_t120(struct sk_buff
*skb
,
334 enum ip_conntrack_info ctinfo
,
335 unsigned int protoff
,
336 unsigned char **data
, int dataoff
,
337 H245_TransportAddress
*taddr
)
339 int dir
= CTINFO2DIR(ctinfo
);
342 union nf_inet_addr addr
;
343 struct nf_conntrack_expect
*exp
;
344 typeof(nat_t120_hook
) nat_t120
;
346 /* Read T.120 address */
347 if (!get_h245_addr(ct
, *data
, taddr
, &addr
, &port
) ||
348 memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) ||
352 /* Create expect for T.120 connections */
353 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
355 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
356 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
357 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
358 IPPROTO_TCP
, NULL
, &port
);
359 exp
->flags
= NF_CT_EXPECT_PERMANENT
; /* Accept multiple channels */
361 if (memcmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
,
362 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
363 sizeof(ct
->tuplehash
[dir
].tuple
.src
.u3
)) &&
364 (nat_t120
= rcu_dereference(nat_t120_hook
)) &&
365 nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
366 ct
->status
& IPS_NAT_MASK
) {
368 ret
= nat_t120(skb
, ct
, ctinfo
, protoff
, data
, dataoff
, taddr
,
370 } else { /* Conntrack only */
371 if (nf_ct_expect_related(exp
) == 0) {
372 pr_debug("nf_ct_h323: expect T.120 ");
373 nf_ct_dump_tuple(&exp
->tuple
);
378 nf_ct_expect_put(exp
);
383 /****************************************************************************/
384 static int process_h245_channel(struct sk_buff
*skb
,
386 enum ip_conntrack_info ctinfo
,
387 unsigned int protoff
,
388 unsigned char **data
, int dataoff
,
389 H2250LogicalChannelParameters
*channel
)
393 if (channel
->options
& eH2250LogicalChannelParameters_mediaChannel
) {
395 ret
= expect_rtp_rtcp(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
396 &channel
->mediaChannel
);
402 options
& eH2250LogicalChannelParameters_mediaControlChannel
) {
404 ret
= expect_rtp_rtcp(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
405 &channel
->mediaControlChannel
);
413 /****************************************************************************/
414 static int process_olc(struct sk_buff
*skb
, struct nf_conn
*ct
,
415 enum ip_conntrack_info ctinfo
,
416 unsigned int protoff
,
417 unsigned char **data
, int dataoff
,
418 OpenLogicalChannel
*olc
)
422 pr_debug("nf_ct_h323: OpenLogicalChannel\n");
424 if (olc
->forwardLogicalChannelParameters
.multiplexParameters
.choice
==
425 eOpenLogicalChannel_forwardLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters
)
427 ret
= process_h245_channel(skb
, ct
, ctinfo
,
428 protoff
, data
, dataoff
,
430 forwardLogicalChannelParameters
.
432 h2250LogicalChannelParameters
);
438 eOpenLogicalChannel_reverseLogicalChannelParameters
) &&
439 (olc
->reverseLogicalChannelParameters
.options
&
440 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters
)
441 && (olc
->reverseLogicalChannelParameters
.multiplexParameters
.
443 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters
))
446 process_h245_channel(skb
, ct
, ctinfo
,
447 protoff
, data
, dataoff
,
449 reverseLogicalChannelParameters
.
451 h2250LogicalChannelParameters
);
456 if ((olc
->options
& eOpenLogicalChannel_separateStack
) &&
457 olc
->forwardLogicalChannelParameters
.dataType
.choice
==
459 olc
->forwardLogicalChannelParameters
.dataType
.data
.application
.
460 choice
== eDataApplicationCapability_application_t120
&&
461 olc
->forwardLogicalChannelParameters
.dataType
.data
.application
.
462 t120
.choice
== eDataProtocolCapability_separateLANStack
&&
463 olc
->separateStack
.networkAddress
.choice
==
464 eNetworkAccessParameters_networkAddress_localAreaAddress
) {
465 ret
= expect_t120(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
466 &olc
->separateStack
.networkAddress
.
475 /****************************************************************************/
476 static int process_olca(struct sk_buff
*skb
, struct nf_conn
*ct
,
477 enum ip_conntrack_info ctinfo
,
478 unsigned int protoff
, unsigned char **data
, int dataoff
,
479 OpenLogicalChannelAck
*olca
)
481 H2250LogicalChannelAckParameters
*ack
;
484 pr_debug("nf_ct_h323: OpenLogicalChannelAck\n");
487 eOpenLogicalChannelAck_reverseLogicalChannelParameters
) &&
488 (olca
->reverseLogicalChannelParameters
.options
&
489 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters
)
490 && (olca
->reverseLogicalChannelParameters
.multiplexParameters
.
492 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters
))
494 ret
= process_h245_channel(skb
, ct
, ctinfo
,
495 protoff
, data
, dataoff
,
497 reverseLogicalChannelParameters
.
499 h2250LogicalChannelParameters
);
505 eOpenLogicalChannelAck_forwardMultiplexAckParameters
) &&
506 (olca
->forwardMultiplexAckParameters
.choice
==
507 eOpenLogicalChannelAck_forwardMultiplexAckParameters_h2250LogicalChannelAckParameters
))
509 ack
= &olca
->forwardMultiplexAckParameters
.
510 h2250LogicalChannelAckParameters
;
512 eH2250LogicalChannelAckParameters_mediaChannel
) {
514 ret
= expect_rtp_rtcp(skb
, ct
, ctinfo
,
515 protoff
, data
, dataoff
,
522 eH2250LogicalChannelAckParameters_mediaControlChannel
) {
524 ret
= expect_rtp_rtcp(skb
, ct
, ctinfo
,
525 protoff
, data
, dataoff
,
526 &ack
->mediaControlChannel
);
532 if ((olca
->options
& eOpenLogicalChannelAck_separateStack
) &&
533 olca
->separateStack
.networkAddress
.choice
==
534 eNetworkAccessParameters_networkAddress_localAreaAddress
) {
535 ret
= expect_t120(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
536 &olca
->separateStack
.networkAddress
.
545 /****************************************************************************/
546 static int process_h245(struct sk_buff
*skb
, struct nf_conn
*ct
,
547 enum ip_conntrack_info ctinfo
,
548 unsigned int protoff
, unsigned char **data
, int dataoff
,
549 MultimediaSystemControlMessage
*mscm
)
551 switch (mscm
->choice
) {
552 case eMultimediaSystemControlMessage_request
:
553 if (mscm
->request
.choice
==
554 eRequestMessage_openLogicalChannel
) {
555 return process_olc(skb
, ct
, ctinfo
,
556 protoff
, data
, dataoff
,
557 &mscm
->request
.openLogicalChannel
);
559 pr_debug("nf_ct_h323: H.245 Request %d\n",
560 mscm
->request
.choice
);
562 case eMultimediaSystemControlMessage_response
:
563 if (mscm
->response
.choice
==
564 eResponseMessage_openLogicalChannelAck
) {
565 return process_olca(skb
, ct
, ctinfo
,
566 protoff
, data
, dataoff
,
568 openLogicalChannelAck
);
570 pr_debug("nf_ct_h323: H.245 Response %d\n",
571 mscm
->response
.choice
);
574 pr_debug("nf_ct_h323: H.245 signal %d\n", mscm
->choice
);
581 /****************************************************************************/
582 static int h245_help(struct sk_buff
*skb
, unsigned int protoff
,
583 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
585 static MultimediaSystemControlMessage mscm
;
586 unsigned char *data
= NULL
;
591 /* Until there's been traffic both ways, don't look in packets. */
592 if (ctinfo
!= IP_CT_ESTABLISHED
&& ctinfo
!= IP_CT_ESTABLISHED_REPLY
)
595 pr_debug("nf_ct_h245: skblen = %u\n", skb
->len
);
597 spin_lock_bh(&nf_h323_lock
);
599 /* Process each TPKT */
600 while (get_tpkt_data(skb
, protoff
, ct
, ctinfo
,
601 &data
, &datalen
, &dataoff
)) {
602 pr_debug("nf_ct_h245: TPKT len=%d ", datalen
);
603 nf_ct_dump_tuple(&ct
->tuplehash
[CTINFO2DIR(ctinfo
)].tuple
);
605 /* Decode H.245 signal */
606 ret
= DecodeMultimediaSystemControlMessage(data
, datalen
,
609 pr_debug("nf_ct_h245: decoding error: %s\n",
610 ret
== H323_ERROR_BOUND
?
611 "out of bound" : "out of range");
612 /* We don't drop when decoding error */
616 /* Process H.245 signal */
617 if (process_h245(skb
, ct
, ctinfo
, protoff
,
618 &data
, dataoff
, &mscm
) < 0)
622 spin_unlock_bh(&nf_h323_lock
);
626 spin_unlock_bh(&nf_h323_lock
);
627 nf_ct_helper_log(skb
, ct
, "cannot process H.245 message");
631 /****************************************************************************/
632 static const struct nf_conntrack_expect_policy h245_exp_policy
= {
633 .max_expected
= H323_RTP_CHANNEL_MAX
* 4 + 2 /* T.120 */,
637 static struct nf_conntrack_helper nf_conntrack_helper_h245 __read_mostly
= {
640 .data_len
= sizeof(struct nf_ct_h323_master
),
641 .tuple
.src
.l3num
= AF_UNSPEC
,
642 .tuple
.dst
.protonum
= IPPROTO_UDP
,
644 .expect_policy
= &h245_exp_policy
,
647 /****************************************************************************/
648 int get_h225_addr(struct nf_conn
*ct
, unsigned char *data
,
649 TransportAddress
*taddr
,
650 union nf_inet_addr
*addr
, __be16
*port
)
652 const unsigned char *p
;
655 switch (taddr
->choice
) {
656 case eTransportAddress_ipAddress
:
657 if (nf_ct_l3num(ct
) != AF_INET
)
659 p
= data
+ taddr
->ipAddress
.ip
;
662 case eTransportAddress_ip6Address
:
663 if (nf_ct_l3num(ct
) != AF_INET6
)
665 p
= data
+ taddr
->ip6Address
.ip
;
672 memcpy(addr
, p
, len
);
673 memset((void *)addr
+ len
, 0, sizeof(*addr
) - len
);
674 memcpy(port
, p
+ len
, sizeof(__be16
));
679 /****************************************************************************/
680 static int expect_h245(struct sk_buff
*skb
, struct nf_conn
*ct
,
681 enum ip_conntrack_info ctinfo
,
682 unsigned int protoff
, unsigned char **data
, int dataoff
,
683 TransportAddress
*taddr
)
685 int dir
= CTINFO2DIR(ctinfo
);
688 union nf_inet_addr addr
;
689 struct nf_conntrack_expect
*exp
;
690 typeof(nat_h245_hook
) nat_h245
;
692 /* Read h245Address */
693 if (!get_h225_addr(ct
, *data
, taddr
, &addr
, &port
) ||
694 memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) ||
698 /* Create expect for h245 connection */
699 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
701 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
702 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
703 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
704 IPPROTO_TCP
, NULL
, &port
);
705 exp
->helper
= &nf_conntrack_helper_h245
;
707 if (memcmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
,
708 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
709 sizeof(ct
->tuplehash
[dir
].tuple
.src
.u3
)) &&
710 (nat_h245
= rcu_dereference(nat_h245_hook
)) &&
711 nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
712 ct
->status
& IPS_NAT_MASK
) {
714 ret
= nat_h245(skb
, ct
, ctinfo
, protoff
, data
, dataoff
, taddr
,
716 } else { /* Conntrack only */
717 if (nf_ct_expect_related(exp
) == 0) {
718 pr_debug("nf_ct_q931: expect H.245 ");
719 nf_ct_dump_tuple(&exp
->tuple
);
724 nf_ct_expect_put(exp
);
729 /* If the calling party is on the same side of the forward-to party,
730 * we don't need to track the second call */
731 static int callforward_do_filter(const union nf_inet_addr
*src
,
732 const union nf_inet_addr
*dst
,
735 const struct nf_afinfo
*afinfo
;
738 /* rcu_read_lock()ed by nf_hook_slow() */
739 afinfo
= nf_get_afinfo(family
);
745 struct flowi4 fl1
, fl2
;
746 struct rtable
*rt1
, *rt2
;
748 memset(&fl1
, 0, sizeof(fl1
));
751 memset(&fl2
, 0, sizeof(fl2
));
753 if (!afinfo
->route(&init_net
, (struct dst_entry
**)&rt1
,
754 flowi4_to_flowi(&fl1
), false)) {
755 if (!afinfo
->route(&init_net
, (struct dst_entry
**)&rt2
,
756 flowi4_to_flowi(&fl2
), false)) {
757 if (rt_nexthop(rt1
, fl1
.daddr
) ==
758 rt_nexthop(rt2
, fl2
.daddr
) &&
759 rt1
->dst
.dev
== rt2
->dst
.dev
)
761 dst_release(&rt2
->dst
);
763 dst_release(&rt1
->dst
);
767 #if IS_ENABLED(CONFIG_NF_CONNTRACK_IPV6)
769 struct flowi6 fl1
, fl2
;
770 struct rt6_info
*rt1
, *rt2
;
772 memset(&fl1
, 0, sizeof(fl1
));
773 fl1
.daddr
= src
->in6
;
775 memset(&fl2
, 0, sizeof(fl2
));
776 fl2
.daddr
= dst
->in6
;
777 if (!afinfo
->route(&init_net
, (struct dst_entry
**)&rt1
,
778 flowi6_to_flowi(&fl1
), false)) {
779 if (!afinfo
->route(&init_net
, (struct dst_entry
**)&rt2
,
780 flowi6_to_flowi(&fl2
), false)) {
781 if (ipv6_addr_equal(rt6_nexthop(rt1
),
783 rt1
->dst
.dev
== rt2
->dst
.dev
)
785 dst_release(&rt2
->dst
);
787 dst_release(&rt1
->dst
);
797 /****************************************************************************/
798 static int expect_callforwarding(struct sk_buff
*skb
,
800 enum ip_conntrack_info ctinfo
,
801 unsigned int protoff
,
802 unsigned char **data
, int dataoff
,
803 TransportAddress
*taddr
)
805 int dir
= CTINFO2DIR(ctinfo
);
808 union nf_inet_addr addr
;
809 struct nf_conntrack_expect
*exp
;
810 typeof(nat_callforwarding_hook
) nat_callforwarding
;
812 /* Read alternativeAddress */
813 if (!get_h225_addr(ct
, *data
, taddr
, &addr
, &port
) || port
== 0)
816 /* If the calling party is on the same side of the forward-to party,
817 * we don't need to track the second call */
818 if (callforward_filter
&&
819 callforward_do_filter(&addr
, &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
821 pr_debug("nf_ct_q931: Call Forwarding not tracked\n");
825 /* Create expect for the second call leg */
826 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
828 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
829 &ct
->tuplehash
[!dir
].tuple
.src
.u3
, &addr
,
830 IPPROTO_TCP
, NULL
, &port
);
831 exp
->helper
= nf_conntrack_helper_q931
;
833 if (memcmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
,
834 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
835 sizeof(ct
->tuplehash
[dir
].tuple
.src
.u3
)) &&
836 (nat_callforwarding
= rcu_dereference(nat_callforwarding_hook
)) &&
837 nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
838 ct
->status
& IPS_NAT_MASK
) {
840 ret
= nat_callforwarding(skb
, ct
, ctinfo
,
841 protoff
, data
, dataoff
,
843 } else { /* Conntrack only */
844 if (nf_ct_expect_related(exp
) == 0) {
845 pr_debug("nf_ct_q931: expect Call Forwarding ");
846 nf_ct_dump_tuple(&exp
->tuple
);
851 nf_ct_expect_put(exp
);
856 /****************************************************************************/
857 static int process_setup(struct sk_buff
*skb
, struct nf_conn
*ct
,
858 enum ip_conntrack_info ctinfo
,
859 unsigned int protoff
,
860 unsigned char **data
, int dataoff
,
863 int dir
= CTINFO2DIR(ctinfo
);
867 union nf_inet_addr addr
;
868 typeof(set_h225_addr_hook
) set_h225_addr
;
870 pr_debug("nf_ct_q931: Setup\n");
872 if (setup
->options
& eSetup_UUIE_h245Address
) {
873 ret
= expect_h245(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
874 &setup
->h245Address
);
879 set_h225_addr
= rcu_dereference(set_h225_addr_hook
);
880 if ((setup
->options
& eSetup_UUIE_destCallSignalAddress
) &&
881 (set_h225_addr
) && nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
882 ct
->status
& IPS_NAT_MASK
&&
883 get_h225_addr(ct
, *data
, &setup
->destCallSignalAddress
,
885 memcmp(&addr
, &ct
->tuplehash
[!dir
].tuple
.src
.u3
, sizeof(addr
))) {
886 pr_debug("nf_ct_q931: set destCallSignalAddress %pI6:%hu->%pI6:%hu\n",
887 &addr
, ntohs(port
), &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
888 ntohs(ct
->tuplehash
[!dir
].tuple
.src
.u
.tcp
.port
));
889 ret
= set_h225_addr(skb
, protoff
, data
, dataoff
,
890 &setup
->destCallSignalAddress
,
891 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
892 ct
->tuplehash
[!dir
].tuple
.src
.u
.tcp
.port
);
897 if ((setup
->options
& eSetup_UUIE_sourceCallSignalAddress
) &&
898 (set_h225_addr
) && nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
899 ct
->status
& IPS_NAT_MASK
&&
900 get_h225_addr(ct
, *data
, &setup
->sourceCallSignalAddress
,
902 memcmp(&addr
, &ct
->tuplehash
[!dir
].tuple
.dst
.u3
, sizeof(addr
))) {
903 pr_debug("nf_ct_q931: set sourceCallSignalAddress %pI6:%hu->%pI6:%hu\n",
904 &addr
, ntohs(port
), &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
905 ntohs(ct
->tuplehash
[!dir
].tuple
.dst
.u
.tcp
.port
));
906 ret
= set_h225_addr(skb
, protoff
, data
, dataoff
,
907 &setup
->sourceCallSignalAddress
,
908 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
909 ct
->tuplehash
[!dir
].tuple
.dst
.u
.tcp
.port
);
914 if (setup
->options
& eSetup_UUIE_fastStart
) {
915 for (i
= 0; i
< setup
->fastStart
.count
; i
++) {
916 ret
= process_olc(skb
, ct
, ctinfo
,
917 protoff
, data
, dataoff
,
918 &setup
->fastStart
.item
[i
]);
927 /****************************************************************************/
928 static int process_callproceeding(struct sk_buff
*skb
,
930 enum ip_conntrack_info ctinfo
,
931 unsigned int protoff
,
932 unsigned char **data
, int dataoff
,
933 CallProceeding_UUIE
*callproc
)
938 pr_debug("nf_ct_q931: CallProceeding\n");
940 if (callproc
->options
& eCallProceeding_UUIE_h245Address
) {
941 ret
= expect_h245(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
942 &callproc
->h245Address
);
947 if (callproc
->options
& eCallProceeding_UUIE_fastStart
) {
948 for (i
= 0; i
< callproc
->fastStart
.count
; i
++) {
949 ret
= process_olc(skb
, ct
, ctinfo
,
950 protoff
, data
, dataoff
,
951 &callproc
->fastStart
.item
[i
]);
960 /****************************************************************************/
961 static int process_connect(struct sk_buff
*skb
, struct nf_conn
*ct
,
962 enum ip_conntrack_info ctinfo
,
963 unsigned int protoff
,
964 unsigned char **data
, int dataoff
,
965 Connect_UUIE
*connect
)
970 pr_debug("nf_ct_q931: Connect\n");
972 if (connect
->options
& eConnect_UUIE_h245Address
) {
973 ret
= expect_h245(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
974 &connect
->h245Address
);
979 if (connect
->options
& eConnect_UUIE_fastStart
) {
980 for (i
= 0; i
< connect
->fastStart
.count
; i
++) {
981 ret
= process_olc(skb
, ct
, ctinfo
,
982 protoff
, data
, dataoff
,
983 &connect
->fastStart
.item
[i
]);
992 /****************************************************************************/
993 static int process_alerting(struct sk_buff
*skb
, struct nf_conn
*ct
,
994 enum ip_conntrack_info ctinfo
,
995 unsigned int protoff
,
996 unsigned char **data
, int dataoff
,
997 Alerting_UUIE
*alert
)
1002 pr_debug("nf_ct_q931: Alerting\n");
1004 if (alert
->options
& eAlerting_UUIE_h245Address
) {
1005 ret
= expect_h245(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
1006 &alert
->h245Address
);
1011 if (alert
->options
& eAlerting_UUIE_fastStart
) {
1012 for (i
= 0; i
< alert
->fastStart
.count
; i
++) {
1013 ret
= process_olc(skb
, ct
, ctinfo
,
1014 protoff
, data
, dataoff
,
1015 &alert
->fastStart
.item
[i
]);
1024 /****************************************************************************/
1025 static int process_facility(struct sk_buff
*skb
, struct nf_conn
*ct
,
1026 enum ip_conntrack_info ctinfo
,
1027 unsigned int protoff
,
1028 unsigned char **data
, int dataoff
,
1029 Facility_UUIE
*facility
)
1034 pr_debug("nf_ct_q931: Facility\n");
1036 if (facility
->reason
.choice
== eFacilityReason_callForwarded
) {
1037 if (facility
->options
& eFacility_UUIE_alternativeAddress
)
1038 return expect_callforwarding(skb
, ct
, ctinfo
,
1039 protoff
, data
, dataoff
,
1041 alternativeAddress
);
1045 if (facility
->options
& eFacility_UUIE_h245Address
) {
1046 ret
= expect_h245(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
1047 &facility
->h245Address
);
1052 if (facility
->options
& eFacility_UUIE_fastStart
) {
1053 for (i
= 0; i
< facility
->fastStart
.count
; i
++) {
1054 ret
= process_olc(skb
, ct
, ctinfo
,
1055 protoff
, data
, dataoff
,
1056 &facility
->fastStart
.item
[i
]);
1065 /****************************************************************************/
1066 static int process_progress(struct sk_buff
*skb
, struct nf_conn
*ct
,
1067 enum ip_conntrack_info ctinfo
,
1068 unsigned int protoff
,
1069 unsigned char **data
, int dataoff
,
1070 Progress_UUIE
*progress
)
1075 pr_debug("nf_ct_q931: Progress\n");
1077 if (progress
->options
& eProgress_UUIE_h245Address
) {
1078 ret
= expect_h245(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
1079 &progress
->h245Address
);
1084 if (progress
->options
& eProgress_UUIE_fastStart
) {
1085 for (i
= 0; i
< progress
->fastStart
.count
; i
++) {
1086 ret
= process_olc(skb
, ct
, ctinfo
,
1087 protoff
, data
, dataoff
,
1088 &progress
->fastStart
.item
[i
]);
1097 /****************************************************************************/
1098 static int process_q931(struct sk_buff
*skb
, struct nf_conn
*ct
,
1099 enum ip_conntrack_info ctinfo
,
1100 unsigned int protoff
, unsigned char **data
, int dataoff
,
1103 H323_UU_PDU
*pdu
= &q931
->UUIE
.h323_uu_pdu
;
1107 switch (pdu
->h323_message_body
.choice
) {
1108 case eH323_UU_PDU_h323_message_body_setup
:
1109 ret
= process_setup(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
1110 &pdu
->h323_message_body
.setup
);
1112 case eH323_UU_PDU_h323_message_body_callProceeding
:
1113 ret
= process_callproceeding(skb
, ct
, ctinfo
,
1114 protoff
, data
, dataoff
,
1115 &pdu
->h323_message_body
.
1118 case eH323_UU_PDU_h323_message_body_connect
:
1119 ret
= process_connect(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
1120 &pdu
->h323_message_body
.connect
);
1122 case eH323_UU_PDU_h323_message_body_alerting
:
1123 ret
= process_alerting(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
1124 &pdu
->h323_message_body
.alerting
);
1126 case eH323_UU_PDU_h323_message_body_facility
:
1127 ret
= process_facility(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
1128 &pdu
->h323_message_body
.facility
);
1130 case eH323_UU_PDU_h323_message_body_progress
:
1131 ret
= process_progress(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
1132 &pdu
->h323_message_body
.progress
);
1135 pr_debug("nf_ct_q931: Q.931 signal %d\n",
1136 pdu
->h323_message_body
.choice
);
1143 if (pdu
->options
& eH323_UU_PDU_h245Control
) {
1144 for (i
= 0; i
< pdu
->h245Control
.count
; i
++) {
1145 ret
= process_h245(skb
, ct
, ctinfo
,
1146 protoff
, data
, dataoff
,
1147 &pdu
->h245Control
.item
[i
]);
1156 /****************************************************************************/
1157 static int q931_help(struct sk_buff
*skb
, unsigned int protoff
,
1158 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
1161 unsigned char *data
= NULL
;
1166 /* Until there's been traffic both ways, don't look in packets. */
1167 if (ctinfo
!= IP_CT_ESTABLISHED
&& ctinfo
!= IP_CT_ESTABLISHED_REPLY
)
1170 pr_debug("nf_ct_q931: skblen = %u\n", skb
->len
);
1172 spin_lock_bh(&nf_h323_lock
);
1174 /* Process each TPKT */
1175 while (get_tpkt_data(skb
, protoff
, ct
, ctinfo
,
1176 &data
, &datalen
, &dataoff
)) {
1177 pr_debug("nf_ct_q931: TPKT len=%d ", datalen
);
1178 nf_ct_dump_tuple(&ct
->tuplehash
[CTINFO2DIR(ctinfo
)].tuple
);
1180 /* Decode Q.931 signal */
1181 ret
= DecodeQ931(data
, datalen
, &q931
);
1183 pr_debug("nf_ct_q931: decoding error: %s\n",
1184 ret
== H323_ERROR_BOUND
?
1185 "out of bound" : "out of range");
1186 /* We don't drop when decoding error */
1190 /* Process Q.931 signal */
1191 if (process_q931(skb
, ct
, ctinfo
, protoff
,
1192 &data
, dataoff
, &q931
) < 0)
1196 spin_unlock_bh(&nf_h323_lock
);
1200 spin_unlock_bh(&nf_h323_lock
);
1201 nf_ct_helper_log(skb
, ct
, "cannot process Q.931 message");
1205 /****************************************************************************/
1206 static const struct nf_conntrack_expect_policy q931_exp_policy
= {
1207 /* T.120 and H.245 */
1208 .max_expected
= H323_RTP_CHANNEL_MAX
* 4 + 4,
1212 static struct nf_conntrack_helper nf_conntrack_helper_q931
[] __read_mostly
= {
1216 .data_len
= sizeof(struct nf_ct_h323_master
),
1217 .tuple
.src
.l3num
= AF_INET
,
1218 .tuple
.src
.u
.tcp
.port
= cpu_to_be16(Q931_PORT
),
1219 .tuple
.dst
.protonum
= IPPROTO_TCP
,
1221 .expect_policy
= &q931_exp_policy
,
1226 .tuple
.src
.l3num
= AF_INET6
,
1227 .tuple
.src
.u
.tcp
.port
= cpu_to_be16(Q931_PORT
),
1228 .tuple
.dst
.protonum
= IPPROTO_TCP
,
1230 .expect_policy
= &q931_exp_policy
,
1234 /****************************************************************************/
1235 static unsigned char *get_udp_data(struct sk_buff
*skb
, unsigned int protoff
,
1238 const struct udphdr
*uh
;
1242 uh
= skb_header_pointer(skb
, protoff
, sizeof(_uh
), &_uh
);
1245 dataoff
= protoff
+ sizeof(_uh
);
1246 if (dataoff
>= skb
->len
)
1248 *datalen
= skb
->len
- dataoff
;
1249 return skb_header_pointer(skb
, dataoff
, *datalen
, h323_buffer
);
1252 /****************************************************************************/
1253 static struct nf_conntrack_expect
*find_expect(struct nf_conn
*ct
,
1254 union nf_inet_addr
*addr
,
1257 struct net
*net
= nf_ct_net(ct
);
1258 struct nf_conntrack_expect
*exp
;
1259 struct nf_conntrack_tuple tuple
;
1261 memset(&tuple
.src
.u3
, 0, sizeof(tuple
.src
.u3
));
1262 tuple
.src
.u
.tcp
.port
= 0;
1263 memcpy(&tuple
.dst
.u3
, addr
, sizeof(tuple
.dst
.u3
));
1264 tuple
.dst
.u
.tcp
.port
= port
;
1265 tuple
.dst
.protonum
= IPPROTO_TCP
;
1267 exp
= __nf_ct_expect_find(net
, nf_ct_zone(ct
), &tuple
);
1268 if (exp
&& exp
->master
== ct
)
1273 /****************************************************************************/
1274 static int set_expect_timeout(struct nf_conntrack_expect
*exp
,
1275 unsigned int timeout
)
1277 if (!exp
|| !del_timer(&exp
->timeout
))
1280 exp
->timeout
.expires
= jiffies
+ timeout
* HZ
;
1281 add_timer(&exp
->timeout
);
1286 /****************************************************************************/
1287 static int expect_q931(struct sk_buff
*skb
, struct nf_conn
*ct
,
1288 enum ip_conntrack_info ctinfo
,
1289 unsigned int protoff
, unsigned char **data
,
1290 TransportAddress
*taddr
, int count
)
1292 struct nf_ct_h323_master
*info
= nfct_help_data(ct
);
1293 int dir
= CTINFO2DIR(ctinfo
);
1297 union nf_inet_addr addr
;
1298 struct nf_conntrack_expect
*exp
;
1299 typeof(nat_q931_hook
) nat_q931
;
1301 /* Look for the first related address */
1302 for (i
= 0; i
< count
; i
++) {
1303 if (get_h225_addr(ct
, *data
, &taddr
[i
], &addr
, &port
) &&
1304 memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
,
1305 sizeof(addr
)) == 0 && port
!= 0)
1309 if (i
>= count
) /* Not found */
1312 /* Create expect for Q.931 */
1313 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
1315 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
1316 gkrouted_only
? /* only accept calls from GK? */
1317 &ct
->tuplehash
[!dir
].tuple
.src
.u3
: NULL
,
1318 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
1319 IPPROTO_TCP
, NULL
, &port
);
1320 exp
->helper
= nf_conntrack_helper_q931
;
1321 exp
->flags
= NF_CT_EXPECT_PERMANENT
; /* Accept multiple calls */
1323 nat_q931
= rcu_dereference(nat_q931_hook
);
1324 if (nat_q931
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1325 ct
->status
& IPS_NAT_MASK
) { /* Need NAT */
1326 ret
= nat_q931(skb
, ct
, ctinfo
, protoff
, data
,
1327 taddr
, i
, port
, exp
);
1328 } else { /* Conntrack only */
1329 if (nf_ct_expect_related(exp
) == 0) {
1330 pr_debug("nf_ct_ras: expect Q.931 ");
1331 nf_ct_dump_tuple(&exp
->tuple
);
1333 /* Save port for looking up expect in processing RCF */
1334 info
->sig_port
[dir
] = port
;
1339 nf_ct_expect_put(exp
);
1344 /****************************************************************************/
1345 static int process_grq(struct sk_buff
*skb
, struct nf_conn
*ct
,
1346 enum ip_conntrack_info ctinfo
,
1347 unsigned int protoff
,
1348 unsigned char **data
, GatekeeperRequest
*grq
)
1350 typeof(set_ras_addr_hook
) set_ras_addr
;
1352 pr_debug("nf_ct_ras: GRQ\n");
1354 set_ras_addr
= rcu_dereference(set_ras_addr_hook
);
1355 if (set_ras_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1356 ct
->status
& IPS_NAT_MASK
) /* NATed */
1357 return set_ras_addr(skb
, ct
, ctinfo
, protoff
, data
,
1358 &grq
->rasAddress
, 1);
1362 /****************************************************************************/
1363 static int process_gcf(struct sk_buff
*skb
, struct nf_conn
*ct
,
1364 enum ip_conntrack_info ctinfo
,
1365 unsigned int protoff
,
1366 unsigned char **data
, GatekeeperConfirm
*gcf
)
1368 int dir
= CTINFO2DIR(ctinfo
);
1371 union nf_inet_addr addr
;
1372 struct nf_conntrack_expect
*exp
;
1374 pr_debug("nf_ct_ras: GCF\n");
1376 if (!get_h225_addr(ct
, *data
, &gcf
->rasAddress
, &addr
, &port
))
1379 /* Registration port is the same as discovery port */
1380 if (!memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) &&
1381 port
== ct
->tuplehash
[dir
].tuple
.src
.u
.udp
.port
)
1384 /* Avoid RAS expectation loops. A GCF is never expected. */
1385 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1388 /* Need new expect */
1389 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
1391 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
1392 &ct
->tuplehash
[!dir
].tuple
.src
.u3
, &addr
,
1393 IPPROTO_UDP
, NULL
, &port
);
1394 exp
->helper
= nf_conntrack_helper_ras
;
1396 if (nf_ct_expect_related(exp
) == 0) {
1397 pr_debug("nf_ct_ras: expect RAS ");
1398 nf_ct_dump_tuple(&exp
->tuple
);
1402 nf_ct_expect_put(exp
);
1407 /****************************************************************************/
1408 static int process_rrq(struct sk_buff
*skb
, struct nf_conn
*ct
,
1409 enum ip_conntrack_info ctinfo
,
1410 unsigned int protoff
,
1411 unsigned char **data
, RegistrationRequest
*rrq
)
1413 struct nf_ct_h323_master
*info
= nfct_help_data(ct
);
1415 typeof(set_ras_addr_hook
) set_ras_addr
;
1417 pr_debug("nf_ct_ras: RRQ\n");
1419 ret
= expect_q931(skb
, ct
, ctinfo
, protoff
, data
,
1420 rrq
->callSignalAddress
.item
,
1421 rrq
->callSignalAddress
.count
);
1425 set_ras_addr
= rcu_dereference(set_ras_addr_hook
);
1426 if (set_ras_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1427 ct
->status
& IPS_NAT_MASK
) {
1428 ret
= set_ras_addr(skb
, ct
, ctinfo
, protoff
, data
,
1429 rrq
->rasAddress
.item
,
1430 rrq
->rasAddress
.count
);
1435 if (rrq
->options
& eRegistrationRequest_timeToLive
) {
1436 pr_debug("nf_ct_ras: RRQ TTL = %u seconds\n", rrq
->timeToLive
);
1437 info
->timeout
= rrq
->timeToLive
;
1439 info
->timeout
= default_rrq_ttl
;
1444 /****************************************************************************/
1445 static int process_rcf(struct sk_buff
*skb
, struct nf_conn
*ct
,
1446 enum ip_conntrack_info ctinfo
,
1447 unsigned int protoff
,
1448 unsigned char **data
, RegistrationConfirm
*rcf
)
1450 struct nf_ct_h323_master
*info
= nfct_help_data(ct
);
1451 int dir
= CTINFO2DIR(ctinfo
);
1453 struct nf_conntrack_expect
*exp
;
1454 typeof(set_sig_addr_hook
) set_sig_addr
;
1456 pr_debug("nf_ct_ras: RCF\n");
1458 set_sig_addr
= rcu_dereference(set_sig_addr_hook
);
1459 if (set_sig_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1460 ct
->status
& IPS_NAT_MASK
) {
1461 ret
= set_sig_addr(skb
, ct
, ctinfo
, protoff
, data
,
1462 rcf
->callSignalAddress
.item
,
1463 rcf
->callSignalAddress
.count
);
1468 if (rcf
->options
& eRegistrationConfirm_timeToLive
) {
1469 pr_debug("nf_ct_ras: RCF TTL = %u seconds\n", rcf
->timeToLive
);
1470 info
->timeout
= rcf
->timeToLive
;
1473 if (info
->timeout
> 0) {
1474 pr_debug("nf_ct_ras: set RAS connection timeout to "
1475 "%u seconds\n", info
->timeout
);
1476 nf_ct_refresh(ct
, skb
, info
->timeout
* HZ
);
1478 /* Set expect timeout */
1479 spin_lock_bh(&nf_conntrack_lock
);
1480 exp
= find_expect(ct
, &ct
->tuplehash
[dir
].tuple
.dst
.u3
,
1481 info
->sig_port
[!dir
]);
1483 pr_debug("nf_ct_ras: set Q.931 expect "
1484 "timeout to %u seconds for",
1486 nf_ct_dump_tuple(&exp
->tuple
);
1487 set_expect_timeout(exp
, info
->timeout
);
1489 spin_unlock_bh(&nf_conntrack_lock
);
1495 /****************************************************************************/
1496 static int process_urq(struct sk_buff
*skb
, struct nf_conn
*ct
,
1497 enum ip_conntrack_info ctinfo
,
1498 unsigned int protoff
,
1499 unsigned char **data
, UnregistrationRequest
*urq
)
1501 struct nf_ct_h323_master
*info
= nfct_help_data(ct
);
1502 int dir
= CTINFO2DIR(ctinfo
);
1504 typeof(set_sig_addr_hook
) set_sig_addr
;
1506 pr_debug("nf_ct_ras: URQ\n");
1508 set_sig_addr
= rcu_dereference(set_sig_addr_hook
);
1509 if (set_sig_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1510 ct
->status
& IPS_NAT_MASK
) {
1511 ret
= set_sig_addr(skb
, ct
, ctinfo
, protoff
, data
,
1512 urq
->callSignalAddress
.item
,
1513 urq
->callSignalAddress
.count
);
1518 /* Clear old expect */
1519 nf_ct_remove_expectations(ct
);
1520 info
->sig_port
[dir
] = 0;
1521 info
->sig_port
[!dir
] = 0;
1523 /* Give it 30 seconds for UCF or URJ */
1524 nf_ct_refresh(ct
, skb
, 30 * HZ
);
1529 /****************************************************************************/
1530 static int process_arq(struct sk_buff
*skb
, struct nf_conn
*ct
,
1531 enum ip_conntrack_info ctinfo
,
1532 unsigned int protoff
,
1533 unsigned char **data
, AdmissionRequest
*arq
)
1535 const struct nf_ct_h323_master
*info
= nfct_help_data(ct
);
1536 int dir
= CTINFO2DIR(ctinfo
);
1538 union nf_inet_addr addr
;
1539 typeof(set_h225_addr_hook
) set_h225_addr
;
1541 pr_debug("nf_ct_ras: ARQ\n");
1543 set_h225_addr
= rcu_dereference(set_h225_addr_hook
);
1544 if ((arq
->options
& eAdmissionRequest_destCallSignalAddress
) &&
1545 get_h225_addr(ct
, *data
, &arq
->destCallSignalAddress
,
1547 !memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) &&
1548 port
== info
->sig_port
[dir
] &&
1549 nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1550 set_h225_addr
&& ct
->status
& IPS_NAT_MASK
) {
1552 return set_h225_addr(skb
, protoff
, data
, 0,
1553 &arq
->destCallSignalAddress
,
1554 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
1555 info
->sig_port
[!dir
]);
1558 if ((arq
->options
& eAdmissionRequest_srcCallSignalAddress
) &&
1559 get_h225_addr(ct
, *data
, &arq
->srcCallSignalAddress
,
1561 !memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) &&
1562 set_h225_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1563 ct
->status
& IPS_NAT_MASK
) {
1565 return set_h225_addr(skb
, protoff
, data
, 0,
1566 &arq
->srcCallSignalAddress
,
1567 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
1574 /****************************************************************************/
1575 static int process_acf(struct sk_buff
*skb
, struct nf_conn
*ct
,
1576 enum ip_conntrack_info ctinfo
,
1577 unsigned int protoff
,
1578 unsigned char **data
, AdmissionConfirm
*acf
)
1580 int dir
= CTINFO2DIR(ctinfo
);
1583 union nf_inet_addr addr
;
1584 struct nf_conntrack_expect
*exp
;
1585 typeof(set_sig_addr_hook
) set_sig_addr
;
1587 pr_debug("nf_ct_ras: ACF\n");
1589 if (!get_h225_addr(ct
, *data
, &acf
->destCallSignalAddress
,
1593 if (!memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.dst
.u3
, sizeof(addr
))) {
1595 set_sig_addr
= rcu_dereference(set_sig_addr_hook
);
1596 if (set_sig_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1597 ct
->status
& IPS_NAT_MASK
)
1598 return set_sig_addr(skb
, ct
, ctinfo
, protoff
, data
,
1599 &acf
->destCallSignalAddress
, 1);
1603 /* Need new expect */
1604 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
1606 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
1607 &ct
->tuplehash
[!dir
].tuple
.src
.u3
, &addr
,
1608 IPPROTO_TCP
, NULL
, &port
);
1609 exp
->flags
= NF_CT_EXPECT_PERMANENT
;
1610 exp
->helper
= nf_conntrack_helper_q931
;
1612 if (nf_ct_expect_related(exp
) == 0) {
1613 pr_debug("nf_ct_ras: expect Q.931 ");
1614 nf_ct_dump_tuple(&exp
->tuple
);
1618 nf_ct_expect_put(exp
);
1623 /****************************************************************************/
1624 static int process_lrq(struct sk_buff
*skb
, struct nf_conn
*ct
,
1625 enum ip_conntrack_info ctinfo
,
1626 unsigned int protoff
,
1627 unsigned char **data
, LocationRequest
*lrq
)
1629 typeof(set_ras_addr_hook
) set_ras_addr
;
1631 pr_debug("nf_ct_ras: LRQ\n");
1633 set_ras_addr
= rcu_dereference(set_ras_addr_hook
);
1634 if (set_ras_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1635 ct
->status
& IPS_NAT_MASK
)
1636 return set_ras_addr(skb
, ct
, ctinfo
, protoff
, data
,
1637 &lrq
->replyAddress
, 1);
1641 /****************************************************************************/
1642 static int process_lcf(struct sk_buff
*skb
, struct nf_conn
*ct
,
1643 enum ip_conntrack_info ctinfo
,
1644 unsigned int protoff
,
1645 unsigned char **data
, LocationConfirm
*lcf
)
1647 int dir
= CTINFO2DIR(ctinfo
);
1650 union nf_inet_addr addr
;
1651 struct nf_conntrack_expect
*exp
;
1653 pr_debug("nf_ct_ras: LCF\n");
1655 if (!get_h225_addr(ct
, *data
, &lcf
->callSignalAddress
,
1659 /* Need new expect for call signal */
1660 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
1662 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
1663 &ct
->tuplehash
[!dir
].tuple
.src
.u3
, &addr
,
1664 IPPROTO_TCP
, NULL
, &port
);
1665 exp
->flags
= NF_CT_EXPECT_PERMANENT
;
1666 exp
->helper
= nf_conntrack_helper_q931
;
1668 if (nf_ct_expect_related(exp
) == 0) {
1669 pr_debug("nf_ct_ras: expect Q.931 ");
1670 nf_ct_dump_tuple(&exp
->tuple
);
1674 nf_ct_expect_put(exp
);
1676 /* Ignore rasAddress */
1681 /****************************************************************************/
1682 static int process_irr(struct sk_buff
*skb
, struct nf_conn
*ct
,
1683 enum ip_conntrack_info ctinfo
,
1684 unsigned int protoff
,
1685 unsigned char **data
, InfoRequestResponse
*irr
)
1688 typeof(set_ras_addr_hook
) set_ras_addr
;
1689 typeof(set_sig_addr_hook
) set_sig_addr
;
1691 pr_debug("nf_ct_ras: IRR\n");
1693 set_ras_addr
= rcu_dereference(set_ras_addr_hook
);
1694 if (set_ras_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1695 ct
->status
& IPS_NAT_MASK
) {
1696 ret
= set_ras_addr(skb
, ct
, ctinfo
, protoff
, data
,
1697 &irr
->rasAddress
, 1);
1702 set_sig_addr
= rcu_dereference(set_sig_addr_hook
);
1703 if (set_sig_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1704 ct
->status
& IPS_NAT_MASK
) {
1705 ret
= set_sig_addr(skb
, ct
, ctinfo
, protoff
, data
,
1706 irr
->callSignalAddress
.item
,
1707 irr
->callSignalAddress
.count
);
1715 /****************************************************************************/
1716 static int process_ras(struct sk_buff
*skb
, struct nf_conn
*ct
,
1717 enum ip_conntrack_info ctinfo
,
1718 unsigned int protoff
,
1719 unsigned char **data
, RasMessage
*ras
)
1721 switch (ras
->choice
) {
1722 case eRasMessage_gatekeeperRequest
:
1723 return process_grq(skb
, ct
, ctinfo
, protoff
, data
,
1724 &ras
->gatekeeperRequest
);
1725 case eRasMessage_gatekeeperConfirm
:
1726 return process_gcf(skb
, ct
, ctinfo
, protoff
, data
,
1727 &ras
->gatekeeperConfirm
);
1728 case eRasMessage_registrationRequest
:
1729 return process_rrq(skb
, ct
, ctinfo
, protoff
, data
,
1730 &ras
->registrationRequest
);
1731 case eRasMessage_registrationConfirm
:
1732 return process_rcf(skb
, ct
, ctinfo
, protoff
, data
,
1733 &ras
->registrationConfirm
);
1734 case eRasMessage_unregistrationRequest
:
1735 return process_urq(skb
, ct
, ctinfo
, protoff
, data
,
1736 &ras
->unregistrationRequest
);
1737 case eRasMessage_admissionRequest
:
1738 return process_arq(skb
, ct
, ctinfo
, protoff
, data
,
1739 &ras
->admissionRequest
);
1740 case eRasMessage_admissionConfirm
:
1741 return process_acf(skb
, ct
, ctinfo
, protoff
, data
,
1742 &ras
->admissionConfirm
);
1743 case eRasMessage_locationRequest
:
1744 return process_lrq(skb
, ct
, ctinfo
, protoff
, data
,
1745 &ras
->locationRequest
);
1746 case eRasMessage_locationConfirm
:
1747 return process_lcf(skb
, ct
, ctinfo
, protoff
, data
,
1748 &ras
->locationConfirm
);
1749 case eRasMessage_infoRequestResponse
:
1750 return process_irr(skb
, ct
, ctinfo
, protoff
, data
,
1751 &ras
->infoRequestResponse
);
1753 pr_debug("nf_ct_ras: RAS message %d\n", ras
->choice
);
1760 /****************************************************************************/
1761 static int ras_help(struct sk_buff
*skb
, unsigned int protoff
,
1762 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
1764 static RasMessage ras
;
1765 unsigned char *data
;
1769 pr_debug("nf_ct_ras: skblen = %u\n", skb
->len
);
1771 spin_lock_bh(&nf_h323_lock
);
1774 data
= get_udp_data(skb
, protoff
, &datalen
);
1777 pr_debug("nf_ct_ras: RAS message len=%d ", datalen
);
1778 nf_ct_dump_tuple(&ct
->tuplehash
[CTINFO2DIR(ctinfo
)].tuple
);
1780 /* Decode RAS message */
1781 ret
= DecodeRasMessage(data
, datalen
, &ras
);
1783 pr_debug("nf_ct_ras: decoding error: %s\n",
1784 ret
== H323_ERROR_BOUND
?
1785 "out of bound" : "out of range");
1789 /* Process RAS message */
1790 if (process_ras(skb
, ct
, ctinfo
, protoff
, &data
, &ras
) < 0)
1794 spin_unlock_bh(&nf_h323_lock
);
1798 spin_unlock_bh(&nf_h323_lock
);
1799 nf_ct_helper_log(skb
, ct
, "cannot process RAS message");
1803 /****************************************************************************/
1804 static const struct nf_conntrack_expect_policy ras_exp_policy
= {
1809 static struct nf_conntrack_helper nf_conntrack_helper_ras
[] __read_mostly
= {
1813 .data_len
= sizeof(struct nf_ct_h323_master
),
1814 .tuple
.src
.l3num
= AF_INET
,
1815 .tuple
.src
.u
.udp
.port
= cpu_to_be16(RAS_PORT
),
1816 .tuple
.dst
.protonum
= IPPROTO_UDP
,
1818 .expect_policy
= &ras_exp_policy
,
1823 .data_len
= sizeof(struct nf_ct_h323_master
),
1824 .tuple
.src
.l3num
= AF_INET6
,
1825 .tuple
.src
.u
.udp
.port
= cpu_to_be16(RAS_PORT
),
1826 .tuple
.dst
.protonum
= IPPROTO_UDP
,
1828 .expect_policy
= &ras_exp_policy
,
1832 /****************************************************************************/
1833 static void __exit
nf_conntrack_h323_fini(void)
1835 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras
[1]);
1836 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras
[0]);
1837 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931
[1]);
1838 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931
[0]);
1839 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245
);
1841 pr_debug("nf_ct_h323: fini\n");
1844 /****************************************************************************/
1845 static int __init
nf_conntrack_h323_init(void)
1849 h323_buffer
= kmalloc(65536, GFP_KERNEL
);
1852 ret
= nf_conntrack_helper_register(&nf_conntrack_helper_h245
);
1855 ret
= nf_conntrack_helper_register(&nf_conntrack_helper_q931
[0]);
1858 ret
= nf_conntrack_helper_register(&nf_conntrack_helper_q931
[1]);
1861 ret
= nf_conntrack_helper_register(&nf_conntrack_helper_ras
[0]);
1864 ret
= nf_conntrack_helper_register(&nf_conntrack_helper_ras
[1]);
1867 pr_debug("nf_ct_h323: init success\n");
1871 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras
[0]);
1873 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931
[1]);
1875 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931
[0]);
1877 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245
);
1883 /****************************************************************************/
1884 module_init(nf_conntrack_h323_init
);
1885 module_exit(nf_conntrack_h323_fini
);
1887 EXPORT_SYMBOL_GPL(get_h225_addr
);
1888 EXPORT_SYMBOL_GPL(set_h245_addr_hook
);
1889 EXPORT_SYMBOL_GPL(set_h225_addr_hook
);
1890 EXPORT_SYMBOL_GPL(set_sig_addr_hook
);
1891 EXPORT_SYMBOL_GPL(set_ras_addr_hook
);
1892 EXPORT_SYMBOL_GPL(nat_rtp_rtcp_hook
);
1893 EXPORT_SYMBOL_GPL(nat_t120_hook
);
1894 EXPORT_SYMBOL_GPL(nat_h245_hook
);
1895 EXPORT_SYMBOL_GPL(nat_callforwarding_hook
);
1896 EXPORT_SYMBOL_GPL(nat_q931_hook
);
1898 MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>");
1899 MODULE_DESCRIPTION("H.323 connection tracking helper");
1900 MODULE_LICENSE("GPL");
1901 MODULE_ALIAS("ip_conntrack_h323");
1902 MODULE_ALIAS_NFCT_HELPER("RAS");
1903 MODULE_ALIAS_NFCT_HELPER("Q.931");
1904 MODULE_ALIAS_NFCT_HELPER("H.245");