2 * H.323 connection tracking helper
4 * Copyright (c) 2006 Jing Min Zhao <zhaojingmin@users.sourceforge.net>
6 * This source code is licensed under General Public License version 2.
8 * Based on the 'brute force' H.323 connection tracking module by
9 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
11 * For more information, please see http://nath323.sourceforge.net/
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/ctype.h>
17 #include <linux/inet.h>
20 #include <linux/slab.h>
21 #include <linux/udp.h>
22 #include <linux/tcp.h>
23 #include <linux/skbuff.h>
24 #include <net/route.h>
25 #include <net/ip6_route.h>
27 #include <net/netfilter/nf_conntrack.h>
28 #include <net/netfilter/nf_conntrack_core.h>
29 #include <net/netfilter/nf_conntrack_tuple.h>
30 #include <net/netfilter/nf_conntrack_expect.h>
31 #include <net/netfilter/nf_conntrack_ecache.h>
32 #include <net/netfilter/nf_conntrack_helper.h>
33 #include <net/netfilter/nf_conntrack_zones.h>
34 #include <linux/netfilter/nf_conntrack_h323.h>
37 static unsigned int default_rrq_ttl __read_mostly
= 300;
38 module_param(default_rrq_ttl
, uint
, 0600);
39 MODULE_PARM_DESC(default_rrq_ttl
, "use this TTL if it's missing in RRQ");
41 static int gkrouted_only __read_mostly
= 1;
42 module_param(gkrouted_only
, int, 0600);
43 MODULE_PARM_DESC(gkrouted_only
, "only accept calls from gatekeeper");
45 static bool callforward_filter __read_mostly
= true;
46 module_param(callforward_filter
, bool, 0600);
47 MODULE_PARM_DESC(callforward_filter
, "only create call forwarding expectations "
48 "if both endpoints are on different sides "
49 "(determined by routing information)");
52 int (*set_h245_addr_hook
) (struct sk_buff
*skb
,
53 unsigned char **data
, int dataoff
,
54 H245_TransportAddress
*taddr
,
55 union nf_inet_addr
*addr
, __be16 port
)
57 int (*set_h225_addr_hook
) (struct sk_buff
*skb
,
58 unsigned char **data
, int dataoff
,
59 TransportAddress
*taddr
,
60 union nf_inet_addr
*addr
, __be16 port
)
62 int (*set_sig_addr_hook
) (struct sk_buff
*skb
,
64 enum ip_conntrack_info ctinfo
,
66 TransportAddress
*taddr
, int count
) __read_mostly
;
67 int (*set_ras_addr_hook
) (struct sk_buff
*skb
,
69 enum ip_conntrack_info ctinfo
,
71 TransportAddress
*taddr
, int count
) __read_mostly
;
72 int (*nat_rtp_rtcp_hook
) (struct sk_buff
*skb
,
74 enum ip_conntrack_info ctinfo
,
75 unsigned char **data
, int dataoff
,
76 H245_TransportAddress
*taddr
,
77 __be16 port
, __be16 rtp_port
,
78 struct nf_conntrack_expect
*rtp_exp
,
79 struct nf_conntrack_expect
*rtcp_exp
) __read_mostly
;
80 int (*nat_t120_hook
) (struct sk_buff
*skb
,
82 enum ip_conntrack_info ctinfo
,
83 unsigned char **data
, int dataoff
,
84 H245_TransportAddress
*taddr
, __be16 port
,
85 struct nf_conntrack_expect
*exp
) __read_mostly
;
86 int (*nat_h245_hook
) (struct sk_buff
*skb
,
88 enum ip_conntrack_info ctinfo
,
89 unsigned char **data
, int dataoff
,
90 TransportAddress
*taddr
, __be16 port
,
91 struct nf_conntrack_expect
*exp
) __read_mostly
;
92 int (*nat_callforwarding_hook
) (struct sk_buff
*skb
,
94 enum ip_conntrack_info ctinfo
,
95 unsigned char **data
, int dataoff
,
96 TransportAddress
*taddr
, __be16 port
,
97 struct nf_conntrack_expect
*exp
) __read_mostly
;
98 int (*nat_q931_hook
) (struct sk_buff
*skb
,
100 enum ip_conntrack_info ctinfo
,
101 unsigned char **data
, TransportAddress
*taddr
, int idx
,
102 __be16 port
, struct nf_conntrack_expect
*exp
)
105 static DEFINE_SPINLOCK(nf_h323_lock
);
106 static char *h323_buffer
;
108 static struct nf_conntrack_helper nf_conntrack_helper_h245
;
109 static struct nf_conntrack_helper nf_conntrack_helper_q931
[];
110 static struct nf_conntrack_helper nf_conntrack_helper_ras
[];
112 /****************************************************************************/
113 static int get_tpkt_data(struct sk_buff
*skb
, unsigned int protoff
,
114 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
,
115 unsigned char **data
, int *datalen
, int *dataoff
)
117 struct nf_ct_h323_master
*info
= &nfct_help(ct
)->help
.ct_h323_info
;
118 int dir
= CTINFO2DIR(ctinfo
);
119 const struct tcphdr
*th
;
128 th
= skb_header_pointer(skb
, protoff
, sizeof(_tcph
), &_tcph
);
132 /* Get TCP data offset */
133 tcpdataoff
= protoff
+ th
->doff
* 4;
135 /* Get TCP data length */
136 tcpdatalen
= skb
->len
- tcpdataoff
;
137 if (tcpdatalen
<= 0) /* No TCP data */
140 if (*data
== NULL
) { /* first TPKT */
141 /* Get first TPKT pointer */
142 tpkt
= skb_header_pointer(skb
, tcpdataoff
, tcpdatalen
,
144 BUG_ON(tpkt
== NULL
);
146 /* Validate TPKT identifier */
147 if (tcpdatalen
< 4 || tpkt
[0] != 0x03 || tpkt
[1] != 0) {
148 /* Netmeeting sends TPKT header and data separately */
149 if (info
->tpkt_len
[dir
] > 0) {
150 pr_debug("nf_ct_h323: previous packet "
151 "indicated separate TPKT data of %hu "
152 "bytes\n", info
->tpkt_len
[dir
]);
153 if (info
->tpkt_len
[dir
] <= tcpdatalen
) {
154 /* Yes, there was a TPKT header
157 *datalen
= info
->tpkt_len
[dir
];
162 /* Fragmented TPKT */
163 pr_debug("nf_ct_h323: fragmented TPKT\n");
167 /* It is not even a TPKT */
171 } else { /* Next TPKT */
172 tpktoff
= *dataoff
+ *datalen
;
173 tcpdatalen
-= tpktoff
;
174 if (tcpdatalen
<= 4) /* No more TPKT */
176 tpkt
= *data
+ *datalen
;
178 /* Validate TPKT identifier */
179 if (tpkt
[0] != 0x03 || tpkt
[1] != 0)
183 /* Validate TPKT length */
184 tpktlen
= tpkt
[2] * 256 + tpkt
[3];
187 if (tpktlen
> tcpdatalen
) {
188 if (tcpdatalen
== 4) { /* Separate TPKT header */
189 /* Netmeeting sends TPKT header and data separately */
190 pr_debug("nf_ct_h323: separate TPKT header indicates "
191 "there will be TPKT data of %hu bytes\n",
193 info
->tpkt_len
[dir
] = tpktlen
- 4;
197 pr_debug("nf_ct_h323: incomplete TPKT (fragmented?)\n");
201 /* This is the encapsulated data */
203 *datalen
= tpktlen
- 4;
204 *dataoff
= tpktoff
+ 4;
207 /* Clear TPKT length */
208 info
->tpkt_len
[dir
] = 0;
212 info
->tpkt_len
[dir
] = 0;
216 /****************************************************************************/
217 static int get_h245_addr(struct nf_conn
*ct
, const unsigned char *data
,
218 H245_TransportAddress
*taddr
,
219 union nf_inet_addr
*addr
, __be16
*port
)
221 const unsigned char *p
;
224 if (taddr
->choice
!= eH245_TransportAddress_unicastAddress
)
227 switch (taddr
->unicastAddress
.choice
) {
228 case eUnicastAddress_iPAddress
:
229 if (nf_ct_l3num(ct
) != AF_INET
)
231 p
= data
+ taddr
->unicastAddress
.iPAddress
.network
;
234 case eUnicastAddress_iP6Address
:
235 if (nf_ct_l3num(ct
) != AF_INET6
)
237 p
= data
+ taddr
->unicastAddress
.iP6Address
.network
;
244 memcpy(addr
, p
, len
);
245 memset((void *)addr
+ len
, 0, sizeof(*addr
) - len
);
246 memcpy(port
, p
+ len
, sizeof(__be16
));
251 /****************************************************************************/
252 static int expect_rtp_rtcp(struct sk_buff
*skb
, struct nf_conn
*ct
,
253 enum ip_conntrack_info ctinfo
,
254 unsigned char **data
, int dataoff
,
255 H245_TransportAddress
*taddr
)
257 int dir
= CTINFO2DIR(ctinfo
);
260 __be16 rtp_port
, rtcp_port
;
261 union nf_inet_addr addr
;
262 struct nf_conntrack_expect
*rtp_exp
;
263 struct nf_conntrack_expect
*rtcp_exp
;
264 typeof(nat_rtp_rtcp_hook
) nat_rtp_rtcp
;
266 /* Read RTP or RTCP address */
267 if (!get_h245_addr(ct
, *data
, taddr
, &addr
, &port
) ||
268 memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) ||
272 /* RTP port is even */
273 rtp_port
= port
& ~htons(1);
274 rtcp_port
= port
| htons(1);
276 /* Create expect for RTP */
277 if ((rtp_exp
= nf_ct_expect_alloc(ct
)) == NULL
)
279 nf_ct_expect_init(rtp_exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
280 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
281 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
282 IPPROTO_UDP
, NULL
, &rtp_port
);
284 /* Create expect for RTCP */
285 if ((rtcp_exp
= nf_ct_expect_alloc(ct
)) == NULL
) {
286 nf_ct_expect_put(rtp_exp
);
289 nf_ct_expect_init(rtcp_exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
290 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
291 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
292 IPPROTO_UDP
, NULL
, &rtcp_port
);
294 if (memcmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
,
295 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
296 sizeof(ct
->tuplehash
[dir
].tuple
.src
.u3
)) &&
297 (nat_rtp_rtcp
= rcu_dereference(nat_rtp_rtcp_hook
)) &&
298 ct
->status
& IPS_NAT_MASK
) {
300 ret
= nat_rtp_rtcp(skb
, ct
, ctinfo
, data
, dataoff
,
301 taddr
, port
, rtp_port
, rtp_exp
, rtcp_exp
);
302 } else { /* Conntrack only */
303 if (nf_ct_expect_related(rtp_exp
) == 0) {
304 if (nf_ct_expect_related(rtcp_exp
) == 0) {
305 pr_debug("nf_ct_h323: expect RTP ");
306 nf_ct_dump_tuple(&rtp_exp
->tuple
);
307 pr_debug("nf_ct_h323: expect RTCP ");
308 nf_ct_dump_tuple(&rtcp_exp
->tuple
);
310 nf_ct_unexpect_related(rtp_exp
);
317 nf_ct_expect_put(rtp_exp
);
318 nf_ct_expect_put(rtcp_exp
);
323 /****************************************************************************/
324 static int expect_t120(struct sk_buff
*skb
,
326 enum ip_conntrack_info ctinfo
,
327 unsigned char **data
, int dataoff
,
328 H245_TransportAddress
*taddr
)
330 int dir
= CTINFO2DIR(ctinfo
);
333 union nf_inet_addr addr
;
334 struct nf_conntrack_expect
*exp
;
335 typeof(nat_t120_hook
) nat_t120
;
337 /* Read T.120 address */
338 if (!get_h245_addr(ct
, *data
, taddr
, &addr
, &port
) ||
339 memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) ||
343 /* Create expect for T.120 connections */
344 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
346 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
347 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
348 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
349 IPPROTO_TCP
, NULL
, &port
);
350 exp
->flags
= NF_CT_EXPECT_PERMANENT
; /* Accept multiple channels */
352 if (memcmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
,
353 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
354 sizeof(ct
->tuplehash
[dir
].tuple
.src
.u3
)) &&
355 (nat_t120
= rcu_dereference(nat_t120_hook
)) &&
356 ct
->status
& IPS_NAT_MASK
) {
358 ret
= nat_t120(skb
, ct
, ctinfo
, data
, dataoff
, taddr
,
360 } else { /* Conntrack only */
361 if (nf_ct_expect_related(exp
) == 0) {
362 pr_debug("nf_ct_h323: expect T.120 ");
363 nf_ct_dump_tuple(&exp
->tuple
);
368 nf_ct_expect_put(exp
);
373 /****************************************************************************/
374 static int process_h245_channel(struct sk_buff
*skb
,
376 enum ip_conntrack_info ctinfo
,
377 unsigned char **data
, int dataoff
,
378 H2250LogicalChannelParameters
*channel
)
382 if (channel
->options
& eH2250LogicalChannelParameters_mediaChannel
) {
384 ret
= expect_rtp_rtcp(skb
, ct
, ctinfo
, data
, dataoff
,
385 &channel
->mediaChannel
);
391 options
& eH2250LogicalChannelParameters_mediaControlChannel
) {
393 ret
= expect_rtp_rtcp(skb
, ct
, ctinfo
, data
, dataoff
,
394 &channel
->mediaControlChannel
);
402 /****************************************************************************/
403 static int process_olc(struct sk_buff
*skb
, struct nf_conn
*ct
,
404 enum ip_conntrack_info ctinfo
,
405 unsigned char **data
, int dataoff
,
406 OpenLogicalChannel
*olc
)
410 pr_debug("nf_ct_h323: OpenLogicalChannel\n");
412 if (olc
->forwardLogicalChannelParameters
.multiplexParameters
.choice
==
413 eOpenLogicalChannel_forwardLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters
)
415 ret
= process_h245_channel(skb
, ct
, ctinfo
, data
, dataoff
,
417 forwardLogicalChannelParameters
.
419 h2250LogicalChannelParameters
);
425 eOpenLogicalChannel_reverseLogicalChannelParameters
) &&
426 (olc
->reverseLogicalChannelParameters
.options
&
427 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters
)
428 && (olc
->reverseLogicalChannelParameters
.multiplexParameters
.
430 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters
))
433 process_h245_channel(skb
, ct
, ctinfo
, data
, dataoff
,
435 reverseLogicalChannelParameters
.
437 h2250LogicalChannelParameters
);
442 if ((olc
->options
& eOpenLogicalChannel_separateStack
) &&
443 olc
->forwardLogicalChannelParameters
.dataType
.choice
==
445 olc
->forwardLogicalChannelParameters
.dataType
.data
.application
.
446 choice
== eDataApplicationCapability_application_t120
&&
447 olc
->forwardLogicalChannelParameters
.dataType
.data
.application
.
448 t120
.choice
== eDataProtocolCapability_separateLANStack
&&
449 olc
->separateStack
.networkAddress
.choice
==
450 eNetworkAccessParameters_networkAddress_localAreaAddress
) {
451 ret
= expect_t120(skb
, ct
, ctinfo
, data
, dataoff
,
452 &olc
->separateStack
.networkAddress
.
461 /****************************************************************************/
462 static int process_olca(struct sk_buff
*skb
, struct nf_conn
*ct
,
463 enum ip_conntrack_info ctinfo
,
464 unsigned char **data
, int dataoff
,
465 OpenLogicalChannelAck
*olca
)
467 H2250LogicalChannelAckParameters
*ack
;
470 pr_debug("nf_ct_h323: OpenLogicalChannelAck\n");
473 eOpenLogicalChannelAck_reverseLogicalChannelParameters
) &&
474 (olca
->reverseLogicalChannelParameters
.options
&
475 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters
)
476 && (olca
->reverseLogicalChannelParameters
.multiplexParameters
.
478 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters
))
480 ret
= process_h245_channel(skb
, ct
, ctinfo
, data
, dataoff
,
482 reverseLogicalChannelParameters
.
484 h2250LogicalChannelParameters
);
490 eOpenLogicalChannelAck_forwardMultiplexAckParameters
) &&
491 (olca
->forwardMultiplexAckParameters
.choice
==
492 eOpenLogicalChannelAck_forwardMultiplexAckParameters_h2250LogicalChannelAckParameters
))
494 ack
= &olca
->forwardMultiplexAckParameters
.
495 h2250LogicalChannelAckParameters
;
497 eH2250LogicalChannelAckParameters_mediaChannel
) {
499 ret
= expect_rtp_rtcp(skb
, ct
, ctinfo
, data
, dataoff
,
506 eH2250LogicalChannelAckParameters_mediaControlChannel
) {
508 ret
= expect_rtp_rtcp(skb
, ct
, ctinfo
, data
, dataoff
,
509 &ack
->mediaControlChannel
);
515 if ((olca
->options
& eOpenLogicalChannelAck_separateStack
) &&
516 olca
->separateStack
.networkAddress
.choice
==
517 eNetworkAccessParameters_networkAddress_localAreaAddress
) {
518 ret
= expect_t120(skb
, ct
, ctinfo
, data
, dataoff
,
519 &olca
->separateStack
.networkAddress
.
528 /****************************************************************************/
529 static int process_h245(struct sk_buff
*skb
, struct nf_conn
*ct
,
530 enum ip_conntrack_info ctinfo
,
531 unsigned char **data
, int dataoff
,
532 MultimediaSystemControlMessage
*mscm
)
534 switch (mscm
->choice
) {
535 case eMultimediaSystemControlMessage_request
:
536 if (mscm
->request
.choice
==
537 eRequestMessage_openLogicalChannel
) {
538 return process_olc(skb
, ct
, ctinfo
, data
, dataoff
,
539 &mscm
->request
.openLogicalChannel
);
541 pr_debug("nf_ct_h323: H.245 Request %d\n",
542 mscm
->request
.choice
);
544 case eMultimediaSystemControlMessage_response
:
545 if (mscm
->response
.choice
==
546 eResponseMessage_openLogicalChannelAck
) {
547 return process_olca(skb
, ct
, ctinfo
, data
, dataoff
,
549 openLogicalChannelAck
);
551 pr_debug("nf_ct_h323: H.245 Response %d\n",
552 mscm
->response
.choice
);
555 pr_debug("nf_ct_h323: H.245 signal %d\n", mscm
->choice
);
562 /****************************************************************************/
563 static int h245_help(struct sk_buff
*skb
, unsigned int protoff
,
564 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
566 static MultimediaSystemControlMessage mscm
;
567 unsigned char *data
= NULL
;
572 /* Until there's been traffic both ways, don't look in packets. */
573 if (ctinfo
!= IP_CT_ESTABLISHED
&& ctinfo
!= IP_CT_ESTABLISHED_REPLY
)
576 pr_debug("nf_ct_h245: skblen = %u\n", skb
->len
);
578 spin_lock_bh(&nf_h323_lock
);
580 /* Process each TPKT */
581 while (get_tpkt_data(skb
, protoff
, ct
, ctinfo
,
582 &data
, &datalen
, &dataoff
)) {
583 pr_debug("nf_ct_h245: TPKT len=%d ", datalen
);
584 nf_ct_dump_tuple(&ct
->tuplehash
[CTINFO2DIR(ctinfo
)].tuple
);
586 /* Decode H.245 signal */
587 ret
= DecodeMultimediaSystemControlMessage(data
, datalen
,
590 pr_debug("nf_ct_h245: decoding error: %s\n",
591 ret
== H323_ERROR_BOUND
?
592 "out of bound" : "out of range");
593 /* We don't drop when decoding error */
597 /* Process H.245 signal */
598 if (process_h245(skb
, ct
, ctinfo
, &data
, dataoff
, &mscm
) < 0)
602 spin_unlock_bh(&nf_h323_lock
);
606 spin_unlock_bh(&nf_h323_lock
);
607 net_info_ratelimited("nf_ct_h245: packet dropped\n");
611 /****************************************************************************/
612 static const struct nf_conntrack_expect_policy h245_exp_policy
= {
613 .max_expected
= H323_RTP_CHANNEL_MAX
* 4 + 2 /* T.120 */,
617 static struct nf_conntrack_helper nf_conntrack_helper_h245 __read_mostly
= {
620 .tuple
.src
.l3num
= AF_UNSPEC
,
621 .tuple
.dst
.protonum
= IPPROTO_UDP
,
623 .expect_policy
= &h245_exp_policy
,
626 /****************************************************************************/
627 int get_h225_addr(struct nf_conn
*ct
, unsigned char *data
,
628 TransportAddress
*taddr
,
629 union nf_inet_addr
*addr
, __be16
*port
)
631 const unsigned char *p
;
634 switch (taddr
->choice
) {
635 case eTransportAddress_ipAddress
:
636 if (nf_ct_l3num(ct
) != AF_INET
)
638 p
= data
+ taddr
->ipAddress
.ip
;
641 case eTransportAddress_ip6Address
:
642 if (nf_ct_l3num(ct
) != AF_INET6
)
644 p
= data
+ taddr
->ip6Address
.ip
;
651 memcpy(addr
, p
, len
);
652 memset((void *)addr
+ len
, 0, sizeof(*addr
) - len
);
653 memcpy(port
, p
+ len
, sizeof(__be16
));
658 /****************************************************************************/
659 static int expect_h245(struct sk_buff
*skb
, struct nf_conn
*ct
,
660 enum ip_conntrack_info ctinfo
,
661 unsigned char **data
, int dataoff
,
662 TransportAddress
*taddr
)
664 int dir
= CTINFO2DIR(ctinfo
);
667 union nf_inet_addr addr
;
668 struct nf_conntrack_expect
*exp
;
669 typeof(nat_h245_hook
) nat_h245
;
671 /* Read h245Address */
672 if (!get_h225_addr(ct
, *data
, taddr
, &addr
, &port
) ||
673 memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) ||
677 /* Create expect for h245 connection */
678 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
680 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
681 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
682 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
683 IPPROTO_TCP
, NULL
, &port
);
684 exp
->helper
= &nf_conntrack_helper_h245
;
686 if (memcmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
,
687 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
688 sizeof(ct
->tuplehash
[dir
].tuple
.src
.u3
)) &&
689 (nat_h245
= rcu_dereference(nat_h245_hook
)) &&
690 ct
->status
& IPS_NAT_MASK
) {
692 ret
= nat_h245(skb
, ct
, ctinfo
, data
, dataoff
, taddr
,
694 } else { /* Conntrack only */
695 if (nf_ct_expect_related(exp
) == 0) {
696 pr_debug("nf_ct_q931: expect H.245 ");
697 nf_ct_dump_tuple(&exp
->tuple
);
702 nf_ct_expect_put(exp
);
707 /* If the calling party is on the same side of the forward-to party,
708 * we don't need to track the second call */
709 static int callforward_do_filter(const union nf_inet_addr
*src
,
710 const union nf_inet_addr
*dst
,
713 const struct nf_afinfo
*afinfo
;
716 /* rcu_read_lock()ed by nf_hook_slow() */
717 afinfo
= nf_get_afinfo(family
);
723 struct flowi4 fl1
, fl2
;
724 struct rtable
*rt1
, *rt2
;
726 memset(&fl1
, 0, sizeof(fl1
));
729 memset(&fl2
, 0, sizeof(fl2
));
731 if (!afinfo
->route(&init_net
, (struct dst_entry
**)&rt1
,
732 flowi4_to_flowi(&fl1
), false)) {
733 if (!afinfo
->route(&init_net
, (struct dst_entry
**)&rt2
,
734 flowi4_to_flowi(&fl2
), false)) {
735 if (rt1
->rt_gateway
== rt2
->rt_gateway
&&
736 rt1
->dst
.dev
== rt2
->dst
.dev
)
738 dst_release(&rt2
->dst
);
740 dst_release(&rt1
->dst
);
744 #if IS_ENABLED(CONFIG_NF_CONNTRACK_IPV6)
746 struct flowi6 fl1
, fl2
;
747 struct rt6_info
*rt1
, *rt2
;
749 memset(&fl1
, 0, sizeof(fl1
));
750 fl1
.daddr
= src
->in6
;
752 memset(&fl2
, 0, sizeof(fl2
));
753 fl2
.daddr
= dst
->in6
;
754 if (!afinfo
->route(&init_net
, (struct dst_entry
**)&rt1
,
755 flowi6_to_flowi(&fl1
), false)) {
756 if (!afinfo
->route(&init_net
, (struct dst_entry
**)&rt2
,
757 flowi6_to_flowi(&fl2
), false)) {
758 if (!memcmp(&rt1
->rt6i_gateway
, &rt2
->rt6i_gateway
,
759 sizeof(rt1
->rt6i_gateway
)) &&
760 rt1
->dst
.dev
== rt2
->dst
.dev
)
762 dst_release(&rt2
->dst
);
764 dst_release(&rt1
->dst
);
774 /****************************************************************************/
775 static int expect_callforwarding(struct sk_buff
*skb
,
777 enum ip_conntrack_info ctinfo
,
778 unsigned char **data
, int dataoff
,
779 TransportAddress
*taddr
)
781 int dir
= CTINFO2DIR(ctinfo
);
784 union nf_inet_addr addr
;
785 struct nf_conntrack_expect
*exp
;
786 typeof(nat_callforwarding_hook
) nat_callforwarding
;
788 /* Read alternativeAddress */
789 if (!get_h225_addr(ct
, *data
, taddr
, &addr
, &port
) || port
== 0)
792 /* If the calling party is on the same side of the forward-to party,
793 * we don't need to track the second call */
794 if (callforward_filter
&&
795 callforward_do_filter(&addr
, &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
797 pr_debug("nf_ct_q931: Call Forwarding not tracked\n");
801 /* Create expect for the second call leg */
802 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
804 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
805 &ct
->tuplehash
[!dir
].tuple
.src
.u3
, &addr
,
806 IPPROTO_TCP
, NULL
, &port
);
807 exp
->helper
= nf_conntrack_helper_q931
;
809 if (memcmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
,
810 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
811 sizeof(ct
->tuplehash
[dir
].tuple
.src
.u3
)) &&
812 (nat_callforwarding
= rcu_dereference(nat_callforwarding_hook
)) &&
813 ct
->status
& IPS_NAT_MASK
) {
815 ret
= nat_callforwarding(skb
, ct
, ctinfo
, data
, dataoff
,
817 } else { /* Conntrack only */
818 if (nf_ct_expect_related(exp
) == 0) {
819 pr_debug("nf_ct_q931: expect Call Forwarding ");
820 nf_ct_dump_tuple(&exp
->tuple
);
825 nf_ct_expect_put(exp
);
830 /****************************************************************************/
831 static int process_setup(struct sk_buff
*skb
, struct nf_conn
*ct
,
832 enum ip_conntrack_info ctinfo
,
833 unsigned char **data
, int dataoff
,
836 int dir
= CTINFO2DIR(ctinfo
);
840 union nf_inet_addr addr
;
841 typeof(set_h225_addr_hook
) set_h225_addr
;
843 pr_debug("nf_ct_q931: Setup\n");
845 if (setup
->options
& eSetup_UUIE_h245Address
) {
846 ret
= expect_h245(skb
, ct
, ctinfo
, data
, dataoff
,
847 &setup
->h245Address
);
852 set_h225_addr
= rcu_dereference(set_h225_addr_hook
);
853 if ((setup
->options
& eSetup_UUIE_destCallSignalAddress
) &&
854 (set_h225_addr
) && ct
->status
& IPS_NAT_MASK
&&
855 get_h225_addr(ct
, *data
, &setup
->destCallSignalAddress
,
857 memcmp(&addr
, &ct
->tuplehash
[!dir
].tuple
.src
.u3
, sizeof(addr
))) {
858 pr_debug("nf_ct_q931: set destCallSignalAddress %pI6:%hu->%pI6:%hu\n",
859 &addr
, ntohs(port
), &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
860 ntohs(ct
->tuplehash
[!dir
].tuple
.src
.u
.tcp
.port
));
861 ret
= set_h225_addr(skb
, data
, dataoff
,
862 &setup
->destCallSignalAddress
,
863 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
864 ct
->tuplehash
[!dir
].tuple
.src
.u
.tcp
.port
);
869 if ((setup
->options
& eSetup_UUIE_sourceCallSignalAddress
) &&
870 (set_h225_addr
) && ct
->status
& IPS_NAT_MASK
&&
871 get_h225_addr(ct
, *data
, &setup
->sourceCallSignalAddress
,
873 memcmp(&addr
, &ct
->tuplehash
[!dir
].tuple
.dst
.u3
, sizeof(addr
))) {
874 pr_debug("nf_ct_q931: set sourceCallSignalAddress %pI6:%hu->%pI6:%hu\n",
875 &addr
, ntohs(port
), &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
876 ntohs(ct
->tuplehash
[!dir
].tuple
.dst
.u
.tcp
.port
));
877 ret
= set_h225_addr(skb
, data
, dataoff
,
878 &setup
->sourceCallSignalAddress
,
879 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
880 ct
->tuplehash
[!dir
].tuple
.dst
.u
.tcp
.port
);
885 if (setup
->options
& eSetup_UUIE_fastStart
) {
886 for (i
= 0; i
< setup
->fastStart
.count
; i
++) {
887 ret
= process_olc(skb
, ct
, ctinfo
, data
, dataoff
,
888 &setup
->fastStart
.item
[i
]);
897 /****************************************************************************/
898 static int process_callproceeding(struct sk_buff
*skb
,
900 enum ip_conntrack_info ctinfo
,
901 unsigned char **data
, int dataoff
,
902 CallProceeding_UUIE
*callproc
)
907 pr_debug("nf_ct_q931: CallProceeding\n");
909 if (callproc
->options
& eCallProceeding_UUIE_h245Address
) {
910 ret
= expect_h245(skb
, ct
, ctinfo
, data
, dataoff
,
911 &callproc
->h245Address
);
916 if (callproc
->options
& eCallProceeding_UUIE_fastStart
) {
917 for (i
= 0; i
< callproc
->fastStart
.count
; i
++) {
918 ret
= process_olc(skb
, ct
, ctinfo
, data
, dataoff
,
919 &callproc
->fastStart
.item
[i
]);
928 /****************************************************************************/
929 static int process_connect(struct sk_buff
*skb
, struct nf_conn
*ct
,
930 enum ip_conntrack_info ctinfo
,
931 unsigned char **data
, int dataoff
,
932 Connect_UUIE
*connect
)
937 pr_debug("nf_ct_q931: Connect\n");
939 if (connect
->options
& eConnect_UUIE_h245Address
) {
940 ret
= expect_h245(skb
, ct
, ctinfo
, data
, dataoff
,
941 &connect
->h245Address
);
946 if (connect
->options
& eConnect_UUIE_fastStart
) {
947 for (i
= 0; i
< connect
->fastStart
.count
; i
++) {
948 ret
= process_olc(skb
, ct
, ctinfo
, data
, dataoff
,
949 &connect
->fastStart
.item
[i
]);
958 /****************************************************************************/
959 static int process_alerting(struct sk_buff
*skb
, struct nf_conn
*ct
,
960 enum ip_conntrack_info ctinfo
,
961 unsigned char **data
, int dataoff
,
962 Alerting_UUIE
*alert
)
967 pr_debug("nf_ct_q931: Alerting\n");
969 if (alert
->options
& eAlerting_UUIE_h245Address
) {
970 ret
= expect_h245(skb
, ct
, ctinfo
, data
, dataoff
,
971 &alert
->h245Address
);
976 if (alert
->options
& eAlerting_UUIE_fastStart
) {
977 for (i
= 0; i
< alert
->fastStart
.count
; i
++) {
978 ret
= process_olc(skb
, ct
, ctinfo
, data
, dataoff
,
979 &alert
->fastStart
.item
[i
]);
988 /****************************************************************************/
989 static int process_facility(struct sk_buff
*skb
, struct nf_conn
*ct
,
990 enum ip_conntrack_info ctinfo
,
991 unsigned char **data
, int dataoff
,
992 Facility_UUIE
*facility
)
997 pr_debug("nf_ct_q931: Facility\n");
999 if (facility
->reason
.choice
== eFacilityReason_callForwarded
) {
1000 if (facility
->options
& eFacility_UUIE_alternativeAddress
)
1001 return expect_callforwarding(skb
, ct
, ctinfo
, data
,
1004 alternativeAddress
);
1008 if (facility
->options
& eFacility_UUIE_h245Address
) {
1009 ret
= expect_h245(skb
, ct
, ctinfo
, data
, dataoff
,
1010 &facility
->h245Address
);
1015 if (facility
->options
& eFacility_UUIE_fastStart
) {
1016 for (i
= 0; i
< facility
->fastStart
.count
; i
++) {
1017 ret
= process_olc(skb
, ct
, ctinfo
, data
, dataoff
,
1018 &facility
->fastStart
.item
[i
]);
1027 /****************************************************************************/
1028 static int process_progress(struct sk_buff
*skb
, struct nf_conn
*ct
,
1029 enum ip_conntrack_info ctinfo
,
1030 unsigned char **data
, int dataoff
,
1031 Progress_UUIE
*progress
)
1036 pr_debug("nf_ct_q931: Progress\n");
1038 if (progress
->options
& eProgress_UUIE_h245Address
) {
1039 ret
= expect_h245(skb
, ct
, ctinfo
, data
, dataoff
,
1040 &progress
->h245Address
);
1045 if (progress
->options
& eProgress_UUIE_fastStart
) {
1046 for (i
= 0; i
< progress
->fastStart
.count
; i
++) {
1047 ret
= process_olc(skb
, ct
, ctinfo
, data
, dataoff
,
1048 &progress
->fastStart
.item
[i
]);
1057 /****************************************************************************/
1058 static int process_q931(struct sk_buff
*skb
, struct nf_conn
*ct
,
1059 enum ip_conntrack_info ctinfo
,
1060 unsigned char **data
, int dataoff
, Q931
*q931
)
1062 H323_UU_PDU
*pdu
= &q931
->UUIE
.h323_uu_pdu
;
1066 switch (pdu
->h323_message_body
.choice
) {
1067 case eH323_UU_PDU_h323_message_body_setup
:
1068 ret
= process_setup(skb
, ct
, ctinfo
, data
, dataoff
,
1069 &pdu
->h323_message_body
.setup
);
1071 case eH323_UU_PDU_h323_message_body_callProceeding
:
1072 ret
= process_callproceeding(skb
, ct
, ctinfo
, data
, dataoff
,
1073 &pdu
->h323_message_body
.
1076 case eH323_UU_PDU_h323_message_body_connect
:
1077 ret
= process_connect(skb
, ct
, ctinfo
, data
, dataoff
,
1078 &pdu
->h323_message_body
.connect
);
1080 case eH323_UU_PDU_h323_message_body_alerting
:
1081 ret
= process_alerting(skb
, ct
, ctinfo
, data
, dataoff
,
1082 &pdu
->h323_message_body
.alerting
);
1084 case eH323_UU_PDU_h323_message_body_facility
:
1085 ret
= process_facility(skb
, ct
, ctinfo
, data
, dataoff
,
1086 &pdu
->h323_message_body
.facility
);
1088 case eH323_UU_PDU_h323_message_body_progress
:
1089 ret
= process_progress(skb
, ct
, ctinfo
, data
, dataoff
,
1090 &pdu
->h323_message_body
.progress
);
1093 pr_debug("nf_ct_q931: Q.931 signal %d\n",
1094 pdu
->h323_message_body
.choice
);
1101 if (pdu
->options
& eH323_UU_PDU_h245Control
) {
1102 for (i
= 0; i
< pdu
->h245Control
.count
; i
++) {
1103 ret
= process_h245(skb
, ct
, ctinfo
, data
, dataoff
,
1104 &pdu
->h245Control
.item
[i
]);
1113 /****************************************************************************/
1114 static int q931_help(struct sk_buff
*skb
, unsigned int protoff
,
1115 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
1118 unsigned char *data
= NULL
;
1123 /* Until there's been traffic both ways, don't look in packets. */
1124 if (ctinfo
!= IP_CT_ESTABLISHED
&& ctinfo
!= IP_CT_ESTABLISHED_REPLY
)
1127 pr_debug("nf_ct_q931: skblen = %u\n", skb
->len
);
1129 spin_lock_bh(&nf_h323_lock
);
1131 /* Process each TPKT */
1132 while (get_tpkt_data(skb
, protoff
, ct
, ctinfo
,
1133 &data
, &datalen
, &dataoff
)) {
1134 pr_debug("nf_ct_q931: TPKT len=%d ", datalen
);
1135 nf_ct_dump_tuple(&ct
->tuplehash
[CTINFO2DIR(ctinfo
)].tuple
);
1137 /* Decode Q.931 signal */
1138 ret
= DecodeQ931(data
, datalen
, &q931
);
1140 pr_debug("nf_ct_q931: decoding error: %s\n",
1141 ret
== H323_ERROR_BOUND
?
1142 "out of bound" : "out of range");
1143 /* We don't drop when decoding error */
1147 /* Process Q.931 signal */
1148 if (process_q931(skb
, ct
, ctinfo
, &data
, dataoff
, &q931
) < 0)
1152 spin_unlock_bh(&nf_h323_lock
);
1156 spin_unlock_bh(&nf_h323_lock
);
1157 net_info_ratelimited("nf_ct_q931: packet dropped\n");
1161 /****************************************************************************/
1162 static const struct nf_conntrack_expect_policy q931_exp_policy
= {
1163 /* T.120 and H.245 */
1164 .max_expected
= H323_RTP_CHANNEL_MAX
* 4 + 4,
1168 static struct nf_conntrack_helper nf_conntrack_helper_q931
[] __read_mostly
= {
1172 .tuple
.src
.l3num
= AF_INET
,
1173 .tuple
.src
.u
.tcp
.port
= cpu_to_be16(Q931_PORT
),
1174 .tuple
.dst
.protonum
= IPPROTO_TCP
,
1176 .expect_policy
= &q931_exp_policy
,
1181 .tuple
.src
.l3num
= AF_INET6
,
1182 .tuple
.src
.u
.tcp
.port
= cpu_to_be16(Q931_PORT
),
1183 .tuple
.dst
.protonum
= IPPROTO_TCP
,
1185 .expect_policy
= &q931_exp_policy
,
1189 /****************************************************************************/
1190 static unsigned char *get_udp_data(struct sk_buff
*skb
, unsigned int protoff
,
1193 const struct udphdr
*uh
;
1197 uh
= skb_header_pointer(skb
, protoff
, sizeof(_uh
), &_uh
);
1200 dataoff
= protoff
+ sizeof(_uh
);
1201 if (dataoff
>= skb
->len
)
1203 *datalen
= skb
->len
- dataoff
;
1204 return skb_header_pointer(skb
, dataoff
, *datalen
, h323_buffer
);
1207 /****************************************************************************/
1208 static struct nf_conntrack_expect
*find_expect(struct nf_conn
*ct
,
1209 union nf_inet_addr
*addr
,
1212 struct net
*net
= nf_ct_net(ct
);
1213 struct nf_conntrack_expect
*exp
;
1214 struct nf_conntrack_tuple tuple
;
1216 memset(&tuple
.src
.u3
, 0, sizeof(tuple
.src
.u3
));
1217 tuple
.src
.u
.tcp
.port
= 0;
1218 memcpy(&tuple
.dst
.u3
, addr
, sizeof(tuple
.dst
.u3
));
1219 tuple
.dst
.u
.tcp
.port
= port
;
1220 tuple
.dst
.protonum
= IPPROTO_TCP
;
1222 exp
= __nf_ct_expect_find(net
, nf_ct_zone(ct
), &tuple
);
1223 if (exp
&& exp
->master
== ct
)
1228 /****************************************************************************/
1229 static int set_expect_timeout(struct nf_conntrack_expect
*exp
,
1230 unsigned int timeout
)
1232 if (!exp
|| !del_timer(&exp
->timeout
))
1235 exp
->timeout
.expires
= jiffies
+ timeout
* HZ
;
1236 add_timer(&exp
->timeout
);
1241 /****************************************************************************/
1242 static int expect_q931(struct sk_buff
*skb
, struct nf_conn
*ct
,
1243 enum ip_conntrack_info ctinfo
,
1244 unsigned char **data
,
1245 TransportAddress
*taddr
, int count
)
1247 struct nf_ct_h323_master
*info
= &nfct_help(ct
)->help
.ct_h323_info
;
1248 int dir
= CTINFO2DIR(ctinfo
);
1252 union nf_inet_addr addr
;
1253 struct nf_conntrack_expect
*exp
;
1254 typeof(nat_q931_hook
) nat_q931
;
1256 /* Look for the first related address */
1257 for (i
= 0; i
< count
; i
++) {
1258 if (get_h225_addr(ct
, *data
, &taddr
[i
], &addr
, &port
) &&
1259 memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
,
1260 sizeof(addr
)) == 0 && port
!= 0)
1264 if (i
>= count
) /* Not found */
1267 /* Create expect for Q.931 */
1268 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
1270 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
1271 gkrouted_only
? /* only accept calls from GK? */
1272 &ct
->tuplehash
[!dir
].tuple
.src
.u3
: NULL
,
1273 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
1274 IPPROTO_TCP
, NULL
, &port
);
1275 exp
->helper
= nf_conntrack_helper_q931
;
1276 exp
->flags
= NF_CT_EXPECT_PERMANENT
; /* Accept multiple calls */
1278 nat_q931
= rcu_dereference(nat_q931_hook
);
1279 if (nat_q931
&& ct
->status
& IPS_NAT_MASK
) { /* Need NAT */
1280 ret
= nat_q931(skb
, ct
, ctinfo
, data
, taddr
, i
, port
, exp
);
1281 } else { /* Conntrack only */
1282 if (nf_ct_expect_related(exp
) == 0) {
1283 pr_debug("nf_ct_ras: expect Q.931 ");
1284 nf_ct_dump_tuple(&exp
->tuple
);
1286 /* Save port for looking up expect in processing RCF */
1287 info
->sig_port
[dir
] = port
;
1292 nf_ct_expect_put(exp
);
1297 /****************************************************************************/
1298 static int process_grq(struct sk_buff
*skb
, struct nf_conn
*ct
,
1299 enum ip_conntrack_info ctinfo
,
1300 unsigned char **data
, GatekeeperRequest
*grq
)
1302 typeof(set_ras_addr_hook
) set_ras_addr
;
1304 pr_debug("nf_ct_ras: GRQ\n");
1306 set_ras_addr
= rcu_dereference(set_ras_addr_hook
);
1307 if (set_ras_addr
&& ct
->status
& IPS_NAT_MASK
) /* NATed */
1308 return set_ras_addr(skb
, ct
, ctinfo
, data
,
1309 &grq
->rasAddress
, 1);
1313 /****************************************************************************/
1314 static int process_gcf(struct sk_buff
*skb
, struct nf_conn
*ct
,
1315 enum ip_conntrack_info ctinfo
,
1316 unsigned char **data
, GatekeeperConfirm
*gcf
)
1318 int dir
= CTINFO2DIR(ctinfo
);
1321 union nf_inet_addr addr
;
1322 struct nf_conntrack_expect
*exp
;
1324 pr_debug("nf_ct_ras: GCF\n");
1326 if (!get_h225_addr(ct
, *data
, &gcf
->rasAddress
, &addr
, &port
))
1329 /* Registration port is the same as discovery port */
1330 if (!memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) &&
1331 port
== ct
->tuplehash
[dir
].tuple
.src
.u
.udp
.port
)
1334 /* Avoid RAS expectation loops. A GCF is never expected. */
1335 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1338 /* Need new expect */
1339 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
1341 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
1342 &ct
->tuplehash
[!dir
].tuple
.src
.u3
, &addr
,
1343 IPPROTO_UDP
, NULL
, &port
);
1344 exp
->helper
= nf_conntrack_helper_ras
;
1346 if (nf_ct_expect_related(exp
) == 0) {
1347 pr_debug("nf_ct_ras: expect RAS ");
1348 nf_ct_dump_tuple(&exp
->tuple
);
1352 nf_ct_expect_put(exp
);
1357 /****************************************************************************/
1358 static int process_rrq(struct sk_buff
*skb
, struct nf_conn
*ct
,
1359 enum ip_conntrack_info ctinfo
,
1360 unsigned char **data
, RegistrationRequest
*rrq
)
1362 struct nf_ct_h323_master
*info
= &nfct_help(ct
)->help
.ct_h323_info
;
1364 typeof(set_ras_addr_hook
) set_ras_addr
;
1366 pr_debug("nf_ct_ras: RRQ\n");
1368 ret
= expect_q931(skb
, ct
, ctinfo
, data
,
1369 rrq
->callSignalAddress
.item
,
1370 rrq
->callSignalAddress
.count
);
1374 set_ras_addr
= rcu_dereference(set_ras_addr_hook
);
1375 if (set_ras_addr
&& ct
->status
& IPS_NAT_MASK
) {
1376 ret
= set_ras_addr(skb
, ct
, ctinfo
, data
,
1377 rrq
->rasAddress
.item
,
1378 rrq
->rasAddress
.count
);
1383 if (rrq
->options
& eRegistrationRequest_timeToLive
) {
1384 pr_debug("nf_ct_ras: RRQ TTL = %u seconds\n", rrq
->timeToLive
);
1385 info
->timeout
= rrq
->timeToLive
;
1387 info
->timeout
= default_rrq_ttl
;
1392 /****************************************************************************/
1393 static int process_rcf(struct sk_buff
*skb
, struct nf_conn
*ct
,
1394 enum ip_conntrack_info ctinfo
,
1395 unsigned char **data
, RegistrationConfirm
*rcf
)
1397 struct nf_ct_h323_master
*info
= &nfct_help(ct
)->help
.ct_h323_info
;
1398 int dir
= CTINFO2DIR(ctinfo
);
1400 struct nf_conntrack_expect
*exp
;
1401 typeof(set_sig_addr_hook
) set_sig_addr
;
1403 pr_debug("nf_ct_ras: RCF\n");
1405 set_sig_addr
= rcu_dereference(set_sig_addr_hook
);
1406 if (set_sig_addr
&& ct
->status
& IPS_NAT_MASK
) {
1407 ret
= set_sig_addr(skb
, ct
, ctinfo
, data
,
1408 rcf
->callSignalAddress
.item
,
1409 rcf
->callSignalAddress
.count
);
1414 if (rcf
->options
& eRegistrationConfirm_timeToLive
) {
1415 pr_debug("nf_ct_ras: RCF TTL = %u seconds\n", rcf
->timeToLive
);
1416 info
->timeout
= rcf
->timeToLive
;
1419 if (info
->timeout
> 0) {
1420 pr_debug("nf_ct_ras: set RAS connection timeout to "
1421 "%u seconds\n", info
->timeout
);
1422 nf_ct_refresh(ct
, skb
, info
->timeout
* HZ
);
1424 /* Set expect timeout */
1425 spin_lock_bh(&nf_conntrack_lock
);
1426 exp
= find_expect(ct
, &ct
->tuplehash
[dir
].tuple
.dst
.u3
,
1427 info
->sig_port
[!dir
]);
1429 pr_debug("nf_ct_ras: set Q.931 expect "
1430 "timeout to %u seconds for",
1432 nf_ct_dump_tuple(&exp
->tuple
);
1433 set_expect_timeout(exp
, info
->timeout
);
1435 spin_unlock_bh(&nf_conntrack_lock
);
1441 /****************************************************************************/
1442 static int process_urq(struct sk_buff
*skb
, struct nf_conn
*ct
,
1443 enum ip_conntrack_info ctinfo
,
1444 unsigned char **data
, UnregistrationRequest
*urq
)
1446 struct nf_ct_h323_master
*info
= &nfct_help(ct
)->help
.ct_h323_info
;
1447 int dir
= CTINFO2DIR(ctinfo
);
1449 typeof(set_sig_addr_hook
) set_sig_addr
;
1451 pr_debug("nf_ct_ras: URQ\n");
1453 set_sig_addr
= rcu_dereference(set_sig_addr_hook
);
1454 if (set_sig_addr
&& ct
->status
& IPS_NAT_MASK
) {
1455 ret
= set_sig_addr(skb
, ct
, ctinfo
, data
,
1456 urq
->callSignalAddress
.item
,
1457 urq
->callSignalAddress
.count
);
1462 /* Clear old expect */
1463 nf_ct_remove_expectations(ct
);
1464 info
->sig_port
[dir
] = 0;
1465 info
->sig_port
[!dir
] = 0;
1467 /* Give it 30 seconds for UCF or URJ */
1468 nf_ct_refresh(ct
, skb
, 30 * HZ
);
1473 /****************************************************************************/
1474 static int process_arq(struct sk_buff
*skb
, struct nf_conn
*ct
,
1475 enum ip_conntrack_info ctinfo
,
1476 unsigned char **data
, AdmissionRequest
*arq
)
1478 const struct nf_ct_h323_master
*info
= &nfct_help(ct
)->help
.ct_h323_info
;
1479 int dir
= CTINFO2DIR(ctinfo
);
1481 union nf_inet_addr addr
;
1482 typeof(set_h225_addr_hook
) set_h225_addr
;
1484 pr_debug("nf_ct_ras: ARQ\n");
1486 set_h225_addr
= rcu_dereference(set_h225_addr_hook
);
1487 if ((arq
->options
& eAdmissionRequest_destCallSignalAddress
) &&
1488 get_h225_addr(ct
, *data
, &arq
->destCallSignalAddress
,
1490 !memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) &&
1491 port
== info
->sig_port
[dir
] &&
1492 set_h225_addr
&& ct
->status
& IPS_NAT_MASK
) {
1494 return set_h225_addr(skb
, data
, 0,
1495 &arq
->destCallSignalAddress
,
1496 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
1497 info
->sig_port
[!dir
]);
1500 if ((arq
->options
& eAdmissionRequest_srcCallSignalAddress
) &&
1501 get_h225_addr(ct
, *data
, &arq
->srcCallSignalAddress
,
1503 !memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) &&
1504 set_h225_addr
&& ct
->status
& IPS_NAT_MASK
) {
1506 return set_h225_addr(skb
, data
, 0,
1507 &arq
->srcCallSignalAddress
,
1508 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
1515 /****************************************************************************/
1516 static int process_acf(struct sk_buff
*skb
, struct nf_conn
*ct
,
1517 enum ip_conntrack_info ctinfo
,
1518 unsigned char **data
, AdmissionConfirm
*acf
)
1520 int dir
= CTINFO2DIR(ctinfo
);
1523 union nf_inet_addr addr
;
1524 struct nf_conntrack_expect
*exp
;
1525 typeof(set_sig_addr_hook
) set_sig_addr
;
1527 pr_debug("nf_ct_ras: ACF\n");
1529 if (!get_h225_addr(ct
, *data
, &acf
->destCallSignalAddress
,
1533 if (!memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.dst
.u3
, sizeof(addr
))) {
1535 set_sig_addr
= rcu_dereference(set_sig_addr_hook
);
1536 if (set_sig_addr
&& ct
->status
& IPS_NAT_MASK
)
1537 return set_sig_addr(skb
, ct
, ctinfo
, data
,
1538 &acf
->destCallSignalAddress
, 1);
1542 /* Need new expect */
1543 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
1545 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
1546 &ct
->tuplehash
[!dir
].tuple
.src
.u3
, &addr
,
1547 IPPROTO_TCP
, NULL
, &port
);
1548 exp
->flags
= NF_CT_EXPECT_PERMANENT
;
1549 exp
->helper
= nf_conntrack_helper_q931
;
1551 if (nf_ct_expect_related(exp
) == 0) {
1552 pr_debug("nf_ct_ras: expect Q.931 ");
1553 nf_ct_dump_tuple(&exp
->tuple
);
1557 nf_ct_expect_put(exp
);
1562 /****************************************************************************/
1563 static int process_lrq(struct sk_buff
*skb
, struct nf_conn
*ct
,
1564 enum ip_conntrack_info ctinfo
,
1565 unsigned char **data
, LocationRequest
*lrq
)
1567 typeof(set_ras_addr_hook
) set_ras_addr
;
1569 pr_debug("nf_ct_ras: LRQ\n");
1571 set_ras_addr
= rcu_dereference(set_ras_addr_hook
);
1572 if (set_ras_addr
&& ct
->status
& IPS_NAT_MASK
)
1573 return set_ras_addr(skb
, ct
, ctinfo
, data
,
1574 &lrq
->replyAddress
, 1);
1578 /****************************************************************************/
1579 static int process_lcf(struct sk_buff
*skb
, struct nf_conn
*ct
,
1580 enum ip_conntrack_info ctinfo
,
1581 unsigned char **data
, LocationConfirm
*lcf
)
1583 int dir
= CTINFO2DIR(ctinfo
);
1586 union nf_inet_addr addr
;
1587 struct nf_conntrack_expect
*exp
;
1589 pr_debug("nf_ct_ras: LCF\n");
1591 if (!get_h225_addr(ct
, *data
, &lcf
->callSignalAddress
,
1595 /* Need new expect for call signal */
1596 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
1598 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
1599 &ct
->tuplehash
[!dir
].tuple
.src
.u3
, &addr
,
1600 IPPROTO_TCP
, NULL
, &port
);
1601 exp
->flags
= NF_CT_EXPECT_PERMANENT
;
1602 exp
->helper
= nf_conntrack_helper_q931
;
1604 if (nf_ct_expect_related(exp
) == 0) {
1605 pr_debug("nf_ct_ras: expect Q.931 ");
1606 nf_ct_dump_tuple(&exp
->tuple
);
1610 nf_ct_expect_put(exp
);
1612 /* Ignore rasAddress */
1617 /****************************************************************************/
1618 static int process_irr(struct sk_buff
*skb
, struct nf_conn
*ct
,
1619 enum ip_conntrack_info ctinfo
,
1620 unsigned char **data
, InfoRequestResponse
*irr
)
1623 typeof(set_ras_addr_hook
) set_ras_addr
;
1624 typeof(set_sig_addr_hook
) set_sig_addr
;
1626 pr_debug("nf_ct_ras: IRR\n");
1628 set_ras_addr
= rcu_dereference(set_ras_addr_hook
);
1629 if (set_ras_addr
&& ct
->status
& IPS_NAT_MASK
) {
1630 ret
= set_ras_addr(skb
, ct
, ctinfo
, data
,
1631 &irr
->rasAddress
, 1);
1636 set_sig_addr
= rcu_dereference(set_sig_addr_hook
);
1637 if (set_sig_addr
&& ct
->status
& IPS_NAT_MASK
) {
1638 ret
= set_sig_addr(skb
, ct
, ctinfo
, data
,
1639 irr
->callSignalAddress
.item
,
1640 irr
->callSignalAddress
.count
);
1648 /****************************************************************************/
1649 static int process_ras(struct sk_buff
*skb
, struct nf_conn
*ct
,
1650 enum ip_conntrack_info ctinfo
,
1651 unsigned char **data
, RasMessage
*ras
)
1653 switch (ras
->choice
) {
1654 case eRasMessage_gatekeeperRequest
:
1655 return process_grq(skb
, ct
, ctinfo
, data
,
1656 &ras
->gatekeeperRequest
);
1657 case eRasMessage_gatekeeperConfirm
:
1658 return process_gcf(skb
, ct
, ctinfo
, data
,
1659 &ras
->gatekeeperConfirm
);
1660 case eRasMessage_registrationRequest
:
1661 return process_rrq(skb
, ct
, ctinfo
, data
,
1662 &ras
->registrationRequest
);
1663 case eRasMessage_registrationConfirm
:
1664 return process_rcf(skb
, ct
, ctinfo
, data
,
1665 &ras
->registrationConfirm
);
1666 case eRasMessage_unregistrationRequest
:
1667 return process_urq(skb
, ct
, ctinfo
, data
,
1668 &ras
->unregistrationRequest
);
1669 case eRasMessage_admissionRequest
:
1670 return process_arq(skb
, ct
, ctinfo
, data
,
1671 &ras
->admissionRequest
);
1672 case eRasMessage_admissionConfirm
:
1673 return process_acf(skb
, ct
, ctinfo
, data
,
1674 &ras
->admissionConfirm
);
1675 case eRasMessage_locationRequest
:
1676 return process_lrq(skb
, ct
, ctinfo
, data
,
1677 &ras
->locationRequest
);
1678 case eRasMessage_locationConfirm
:
1679 return process_lcf(skb
, ct
, ctinfo
, data
,
1680 &ras
->locationConfirm
);
1681 case eRasMessage_infoRequestResponse
:
1682 return process_irr(skb
, ct
, ctinfo
, data
,
1683 &ras
->infoRequestResponse
);
1685 pr_debug("nf_ct_ras: RAS message %d\n", ras
->choice
);
1692 /****************************************************************************/
1693 static int ras_help(struct sk_buff
*skb
, unsigned int protoff
,
1694 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
1696 static RasMessage ras
;
1697 unsigned char *data
;
1701 pr_debug("nf_ct_ras: skblen = %u\n", skb
->len
);
1703 spin_lock_bh(&nf_h323_lock
);
1706 data
= get_udp_data(skb
, protoff
, &datalen
);
1709 pr_debug("nf_ct_ras: RAS message len=%d ", datalen
);
1710 nf_ct_dump_tuple(&ct
->tuplehash
[CTINFO2DIR(ctinfo
)].tuple
);
1712 /* Decode RAS message */
1713 ret
= DecodeRasMessage(data
, datalen
, &ras
);
1715 pr_debug("nf_ct_ras: decoding error: %s\n",
1716 ret
== H323_ERROR_BOUND
?
1717 "out of bound" : "out of range");
1721 /* Process RAS message */
1722 if (process_ras(skb
, ct
, ctinfo
, &data
, &ras
) < 0)
1726 spin_unlock_bh(&nf_h323_lock
);
1730 spin_unlock_bh(&nf_h323_lock
);
1731 net_info_ratelimited("nf_ct_ras: packet dropped\n");
1735 /****************************************************************************/
1736 static const struct nf_conntrack_expect_policy ras_exp_policy
= {
1741 static struct nf_conntrack_helper nf_conntrack_helper_ras
[] __read_mostly
= {
1745 .tuple
.src
.l3num
= AF_INET
,
1746 .tuple
.src
.u
.udp
.port
= cpu_to_be16(RAS_PORT
),
1747 .tuple
.dst
.protonum
= IPPROTO_UDP
,
1749 .expect_policy
= &ras_exp_policy
,
1754 .tuple
.src
.l3num
= AF_INET6
,
1755 .tuple
.src
.u
.udp
.port
= cpu_to_be16(RAS_PORT
),
1756 .tuple
.dst
.protonum
= IPPROTO_UDP
,
1758 .expect_policy
= &ras_exp_policy
,
1762 /****************************************************************************/
1763 static void __exit
nf_conntrack_h323_fini(void)
1765 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras
[1]);
1766 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras
[0]);
1767 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931
[1]);
1768 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931
[0]);
1769 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245
);
1771 pr_debug("nf_ct_h323: fini\n");
1774 /****************************************************************************/
1775 static int __init
nf_conntrack_h323_init(void)
1779 h323_buffer
= kmalloc(65536, GFP_KERNEL
);
1782 ret
= nf_conntrack_helper_register(&nf_conntrack_helper_h245
);
1785 ret
= nf_conntrack_helper_register(&nf_conntrack_helper_q931
[0]);
1788 ret
= nf_conntrack_helper_register(&nf_conntrack_helper_q931
[1]);
1791 ret
= nf_conntrack_helper_register(&nf_conntrack_helper_ras
[0]);
1794 ret
= nf_conntrack_helper_register(&nf_conntrack_helper_ras
[1]);
1797 pr_debug("nf_ct_h323: init success\n");
1801 nf_conntrack_helper_unregister(&nf_conntrack_helper_ras
[0]);
1803 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931
[1]);
1805 nf_conntrack_helper_unregister(&nf_conntrack_helper_q931
[0]);
1807 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245
);
1813 /****************************************************************************/
1814 module_init(nf_conntrack_h323_init
);
1815 module_exit(nf_conntrack_h323_fini
);
1817 EXPORT_SYMBOL_GPL(get_h225_addr
);
1818 EXPORT_SYMBOL_GPL(set_h245_addr_hook
);
1819 EXPORT_SYMBOL_GPL(set_h225_addr_hook
);
1820 EXPORT_SYMBOL_GPL(set_sig_addr_hook
);
1821 EXPORT_SYMBOL_GPL(set_ras_addr_hook
);
1822 EXPORT_SYMBOL_GPL(nat_rtp_rtcp_hook
);
1823 EXPORT_SYMBOL_GPL(nat_t120_hook
);
1824 EXPORT_SYMBOL_GPL(nat_h245_hook
);
1825 EXPORT_SYMBOL_GPL(nat_callforwarding_hook
);
1826 EXPORT_SYMBOL_GPL(nat_q931_hook
);
1828 MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>");
1829 MODULE_DESCRIPTION("H.323 connection tracking helper");
1830 MODULE_LICENSE("GPL");
1831 MODULE_ALIAS("ip_conntrack_h323");
1832 MODULE_ALIAS_NFCT_HELPER("RAS");
1833 MODULE_ALIAS_NFCT_HELPER("Q.931");
1834 MODULE_ALIAS_NFCT_HELPER("H.245");