1 // SPDX-License-Identifier: GPL-2.0-only
3 * H.323 connection tracking helper
5 * Copyright (c) 2006 Jing Min Zhao <zhaojingmin@users.sourceforge.net>
6 * Copyright (c) 2006-2012 Patrick McHardy <kaber@trash.net>
8 * Based on the 'brute force' H.323 connection tracking module by
9 * Jozsef Kadlecsik <kadlec@netfilter.org>
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>
26 #include <linux/netfilter_ipv6.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 static int get_tpkt_data(struct sk_buff
*skb
, unsigned int protoff
,
119 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
,
120 unsigned char **data
, int *datalen
, int *dataoff
)
122 struct nf_ct_h323_master
*info
= nfct_help_data(ct
);
123 int dir
= CTINFO2DIR(ctinfo
);
124 const struct tcphdr
*th
;
133 th
= skb_header_pointer(skb
, protoff
, sizeof(_tcph
), &_tcph
);
137 /* Get TCP data offset */
138 tcpdataoff
= protoff
+ th
->doff
* 4;
140 /* Get TCP data length */
141 tcpdatalen
= skb
->len
- tcpdataoff
;
142 if (tcpdatalen
<= 0) /* No TCP data */
145 if (*data
== NULL
) { /* first TPKT */
146 /* Get first TPKT pointer */
147 tpkt
= skb_header_pointer(skb
, tcpdataoff
, tcpdatalen
,
149 BUG_ON(tpkt
== NULL
);
151 /* Validate TPKT identifier */
152 if (tcpdatalen
< 4 || tpkt
[0] != 0x03 || tpkt
[1] != 0) {
153 /* Netmeeting sends TPKT header and data separately */
154 if (info
->tpkt_len
[dir
] > 0) {
155 pr_debug("nf_ct_h323: previous packet "
156 "indicated separate TPKT data of %hu "
157 "bytes\n", info
->tpkt_len
[dir
]);
158 if (info
->tpkt_len
[dir
] <= tcpdatalen
) {
159 /* Yes, there was a TPKT header
162 *datalen
= info
->tpkt_len
[dir
];
167 /* Fragmented TPKT */
168 pr_debug("nf_ct_h323: fragmented TPKT\n");
172 /* It is not even a TPKT */
176 } else { /* Next TPKT */
177 tpktoff
= *dataoff
+ *datalen
;
178 tcpdatalen
-= tpktoff
;
179 if (tcpdatalen
<= 4) /* No more TPKT */
181 tpkt
= *data
+ *datalen
;
183 /* Validate TPKT identifier */
184 if (tpkt
[0] != 0x03 || tpkt
[1] != 0)
188 /* Validate TPKT length */
189 tpktlen
= tpkt
[2] * 256 + tpkt
[3];
192 if (tpktlen
> tcpdatalen
) {
193 if (tcpdatalen
== 4) { /* Separate TPKT header */
194 /* Netmeeting sends TPKT header and data separately */
195 pr_debug("nf_ct_h323: separate TPKT header indicates "
196 "there will be TPKT data of %hu bytes\n",
198 info
->tpkt_len
[dir
] = tpktlen
- 4;
202 pr_debug("nf_ct_h323: incomplete TPKT (fragmented?)\n");
206 /* This is the encapsulated data */
208 *datalen
= tpktlen
- 4;
209 *dataoff
= tpktoff
+ 4;
212 /* Clear TPKT length */
213 info
->tpkt_len
[dir
] = 0;
217 info
->tpkt_len
[dir
] = 0;
221 static int get_h245_addr(struct nf_conn
*ct
, const unsigned char *data
,
222 H245_TransportAddress
*taddr
,
223 union nf_inet_addr
*addr
, __be16
*port
)
225 const unsigned char *p
;
228 if (taddr
->choice
!= eH245_TransportAddress_unicastAddress
)
231 switch (taddr
->unicastAddress
.choice
) {
232 case eUnicastAddress_iPAddress
:
233 if (nf_ct_l3num(ct
) != AF_INET
)
235 p
= data
+ taddr
->unicastAddress
.iPAddress
.network
;
238 case eUnicastAddress_iP6Address
:
239 if (nf_ct_l3num(ct
) != AF_INET6
)
241 p
= data
+ taddr
->unicastAddress
.iP6Address
.network
;
248 memcpy(addr
, p
, len
);
249 memset((void *)addr
+ len
, 0, sizeof(*addr
) - len
);
250 memcpy(port
, p
+ len
, sizeof(__be16
));
255 static int expect_rtp_rtcp(struct sk_buff
*skb
, struct nf_conn
*ct
,
256 enum ip_conntrack_info ctinfo
,
257 unsigned int protoff
,
258 unsigned char **data
, int dataoff
,
259 H245_TransportAddress
*taddr
)
261 int dir
= CTINFO2DIR(ctinfo
);
264 __be16 rtp_port
, rtcp_port
;
265 union nf_inet_addr addr
;
266 struct nf_conntrack_expect
*rtp_exp
;
267 struct nf_conntrack_expect
*rtcp_exp
;
268 typeof(nat_rtp_rtcp_hook
) nat_rtp_rtcp
;
270 /* Read RTP or RTCP address */
271 if (!get_h245_addr(ct
, *data
, taddr
, &addr
, &port
) ||
272 memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) ||
276 /* RTP port is even */
277 rtp_port
= port
& ~htons(1);
278 rtcp_port
= port
| htons(1);
280 /* Create expect for RTP */
281 if ((rtp_exp
= nf_ct_expect_alloc(ct
)) == NULL
)
283 nf_ct_expect_init(rtp_exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
284 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
285 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
286 IPPROTO_UDP
, NULL
, &rtp_port
);
288 /* Create expect for RTCP */
289 if ((rtcp_exp
= nf_ct_expect_alloc(ct
)) == NULL
) {
290 nf_ct_expect_put(rtp_exp
);
293 nf_ct_expect_init(rtcp_exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
294 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
295 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
296 IPPROTO_UDP
, NULL
, &rtcp_port
);
298 if (memcmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
,
299 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
300 sizeof(ct
->tuplehash
[dir
].tuple
.src
.u3
)) &&
301 (nat_rtp_rtcp
= rcu_dereference(nat_rtp_rtcp_hook
)) &&
302 nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
303 ct
->status
& IPS_NAT_MASK
) {
305 ret
= nat_rtp_rtcp(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
306 taddr
, port
, rtp_port
, rtp_exp
, rtcp_exp
);
307 } else { /* Conntrack only */
308 if (nf_ct_expect_related(rtp_exp
, 0) == 0) {
309 if (nf_ct_expect_related(rtcp_exp
, 0) == 0) {
310 pr_debug("nf_ct_h323: expect RTP ");
311 nf_ct_dump_tuple(&rtp_exp
->tuple
);
312 pr_debug("nf_ct_h323: expect RTCP ");
313 nf_ct_dump_tuple(&rtcp_exp
->tuple
);
315 nf_ct_unexpect_related(rtp_exp
);
322 nf_ct_expect_put(rtp_exp
);
323 nf_ct_expect_put(rtcp_exp
);
328 static int expect_t120(struct sk_buff
*skb
,
330 enum ip_conntrack_info ctinfo
,
331 unsigned int protoff
,
332 unsigned char **data
, int dataoff
,
333 H245_TransportAddress
*taddr
)
335 int dir
= CTINFO2DIR(ctinfo
);
338 union nf_inet_addr addr
;
339 struct nf_conntrack_expect
*exp
;
340 typeof(nat_t120_hook
) nat_t120
;
342 /* Read T.120 address */
343 if (!get_h245_addr(ct
, *data
, taddr
, &addr
, &port
) ||
344 memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) ||
348 /* Create expect for T.120 connections */
349 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
351 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
352 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
353 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
354 IPPROTO_TCP
, NULL
, &port
);
355 exp
->flags
= NF_CT_EXPECT_PERMANENT
; /* Accept multiple channels */
357 if (memcmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
,
358 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
359 sizeof(ct
->tuplehash
[dir
].tuple
.src
.u3
)) &&
360 (nat_t120
= rcu_dereference(nat_t120_hook
)) &&
361 nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
362 ct
->status
& IPS_NAT_MASK
) {
364 ret
= nat_t120(skb
, ct
, ctinfo
, protoff
, data
, dataoff
, taddr
,
366 } else { /* Conntrack only */
367 if (nf_ct_expect_related(exp
, 0) == 0) {
368 pr_debug("nf_ct_h323: expect T.120 ");
369 nf_ct_dump_tuple(&exp
->tuple
);
374 nf_ct_expect_put(exp
);
379 static int process_h245_channel(struct sk_buff
*skb
,
381 enum ip_conntrack_info ctinfo
,
382 unsigned int protoff
,
383 unsigned char **data
, int dataoff
,
384 H2250LogicalChannelParameters
*channel
)
388 if (channel
->options
& eH2250LogicalChannelParameters_mediaChannel
) {
390 ret
= expect_rtp_rtcp(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
391 &channel
->mediaChannel
);
397 options
& eH2250LogicalChannelParameters_mediaControlChannel
) {
399 ret
= expect_rtp_rtcp(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
400 &channel
->mediaControlChannel
);
408 static int process_olc(struct sk_buff
*skb
, struct nf_conn
*ct
,
409 enum ip_conntrack_info ctinfo
,
410 unsigned int protoff
,
411 unsigned char **data
, int dataoff
,
412 OpenLogicalChannel
*olc
)
416 pr_debug("nf_ct_h323: OpenLogicalChannel\n");
418 if (olc
->forwardLogicalChannelParameters
.multiplexParameters
.choice
==
419 eOpenLogicalChannel_forwardLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters
)
421 ret
= process_h245_channel(skb
, ct
, ctinfo
,
422 protoff
, data
, dataoff
,
424 forwardLogicalChannelParameters
.
426 h2250LogicalChannelParameters
);
432 eOpenLogicalChannel_reverseLogicalChannelParameters
) &&
433 (olc
->reverseLogicalChannelParameters
.options
&
434 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters
)
435 && (olc
->reverseLogicalChannelParameters
.multiplexParameters
.
437 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters
))
440 process_h245_channel(skb
, ct
, ctinfo
,
441 protoff
, data
, dataoff
,
443 reverseLogicalChannelParameters
.
445 h2250LogicalChannelParameters
);
450 if ((olc
->options
& eOpenLogicalChannel_separateStack
) &&
451 olc
->forwardLogicalChannelParameters
.dataType
.choice
==
453 olc
->forwardLogicalChannelParameters
.dataType
.data
.application
.
454 choice
== eDataApplicationCapability_application_t120
&&
455 olc
->forwardLogicalChannelParameters
.dataType
.data
.application
.
456 t120
.choice
== eDataProtocolCapability_separateLANStack
&&
457 olc
->separateStack
.networkAddress
.choice
==
458 eNetworkAccessParameters_networkAddress_localAreaAddress
) {
459 ret
= expect_t120(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
460 &olc
->separateStack
.networkAddress
.
469 static int process_olca(struct sk_buff
*skb
, struct nf_conn
*ct
,
470 enum ip_conntrack_info ctinfo
,
471 unsigned int protoff
, unsigned char **data
, int dataoff
,
472 OpenLogicalChannelAck
*olca
)
474 H2250LogicalChannelAckParameters
*ack
;
477 pr_debug("nf_ct_h323: OpenLogicalChannelAck\n");
480 eOpenLogicalChannelAck_reverseLogicalChannelParameters
) &&
481 (olca
->reverseLogicalChannelParameters
.options
&
482 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters
)
483 && (olca
->reverseLogicalChannelParameters
.multiplexParameters
.
485 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters
))
487 ret
= process_h245_channel(skb
, ct
, ctinfo
,
488 protoff
, data
, dataoff
,
490 reverseLogicalChannelParameters
.
492 h2250LogicalChannelParameters
);
498 eOpenLogicalChannelAck_forwardMultiplexAckParameters
) &&
499 (olca
->forwardMultiplexAckParameters
.choice
==
500 eOpenLogicalChannelAck_forwardMultiplexAckParameters_h2250LogicalChannelAckParameters
))
502 ack
= &olca
->forwardMultiplexAckParameters
.
503 h2250LogicalChannelAckParameters
;
505 eH2250LogicalChannelAckParameters_mediaChannel
) {
507 ret
= expect_rtp_rtcp(skb
, ct
, ctinfo
,
508 protoff
, data
, dataoff
,
515 eH2250LogicalChannelAckParameters_mediaControlChannel
) {
517 ret
= expect_rtp_rtcp(skb
, ct
, ctinfo
,
518 protoff
, data
, dataoff
,
519 &ack
->mediaControlChannel
);
525 if ((olca
->options
& eOpenLogicalChannelAck_separateStack
) &&
526 olca
->separateStack
.networkAddress
.choice
==
527 eNetworkAccessParameters_networkAddress_localAreaAddress
) {
528 ret
= expect_t120(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
529 &olca
->separateStack
.networkAddress
.
538 static int process_h245(struct sk_buff
*skb
, struct nf_conn
*ct
,
539 enum ip_conntrack_info ctinfo
,
540 unsigned int protoff
, unsigned char **data
, int dataoff
,
541 MultimediaSystemControlMessage
*mscm
)
543 switch (mscm
->choice
) {
544 case eMultimediaSystemControlMessage_request
:
545 if (mscm
->request
.choice
==
546 eRequestMessage_openLogicalChannel
) {
547 return process_olc(skb
, ct
, ctinfo
,
548 protoff
, data
, dataoff
,
549 &mscm
->request
.openLogicalChannel
);
551 pr_debug("nf_ct_h323: H.245 Request %d\n",
552 mscm
->request
.choice
);
554 case eMultimediaSystemControlMessage_response
:
555 if (mscm
->response
.choice
==
556 eResponseMessage_openLogicalChannelAck
) {
557 return process_olca(skb
, ct
, ctinfo
,
558 protoff
, data
, dataoff
,
560 openLogicalChannelAck
);
562 pr_debug("nf_ct_h323: H.245 Response %d\n",
563 mscm
->response
.choice
);
566 pr_debug("nf_ct_h323: H.245 signal %d\n", mscm
->choice
);
573 static int h245_help(struct sk_buff
*skb
, unsigned int protoff
,
574 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
576 static MultimediaSystemControlMessage mscm
;
577 unsigned char *data
= NULL
;
582 /* Until there's been traffic both ways, don't look in packets. */
583 if (ctinfo
!= IP_CT_ESTABLISHED
&& ctinfo
!= IP_CT_ESTABLISHED_REPLY
)
586 pr_debug("nf_ct_h245: skblen = %u\n", skb
->len
);
588 spin_lock_bh(&nf_h323_lock
);
590 /* Process each TPKT */
591 while (get_tpkt_data(skb
, protoff
, ct
, ctinfo
,
592 &data
, &datalen
, &dataoff
)) {
593 pr_debug("nf_ct_h245: TPKT len=%d ", datalen
);
594 nf_ct_dump_tuple(&ct
->tuplehash
[CTINFO2DIR(ctinfo
)].tuple
);
596 /* Decode H.245 signal */
597 ret
= DecodeMultimediaSystemControlMessage(data
, datalen
,
600 pr_debug("nf_ct_h245: decoding error: %s\n",
601 ret
== H323_ERROR_BOUND
?
602 "out of bound" : "out of range");
603 /* We don't drop when decoding error */
607 /* Process H.245 signal */
608 if (process_h245(skb
, ct
, ctinfo
, protoff
,
609 &data
, dataoff
, &mscm
) < 0)
613 spin_unlock_bh(&nf_h323_lock
);
617 spin_unlock_bh(&nf_h323_lock
);
618 nf_ct_helper_log(skb
, ct
, "cannot process H.245 message");
622 static const struct nf_conntrack_expect_policy h245_exp_policy
= {
623 .max_expected
= H323_RTP_CHANNEL_MAX
* 4 + 2 /* T.120 */,
627 static struct nf_conntrack_helper nf_conntrack_helper_h245 __read_mostly
= {
630 .tuple
.src
.l3num
= AF_UNSPEC
,
631 .tuple
.dst
.protonum
= IPPROTO_UDP
,
633 .expect_policy
= &h245_exp_policy
,
636 int get_h225_addr(struct nf_conn
*ct
, unsigned char *data
,
637 TransportAddress
*taddr
,
638 union nf_inet_addr
*addr
, __be16
*port
)
640 const unsigned char *p
;
643 switch (taddr
->choice
) {
644 case eTransportAddress_ipAddress
:
645 if (nf_ct_l3num(ct
) != AF_INET
)
647 p
= data
+ taddr
->ipAddress
.ip
;
650 case eTransportAddress_ip6Address
:
651 if (nf_ct_l3num(ct
) != AF_INET6
)
653 p
= data
+ taddr
->ip6Address
.ip
;
660 memcpy(addr
, p
, len
);
661 memset((void *)addr
+ len
, 0, sizeof(*addr
) - len
);
662 memcpy(port
, p
+ len
, sizeof(__be16
));
667 static int expect_h245(struct sk_buff
*skb
, struct nf_conn
*ct
,
668 enum ip_conntrack_info ctinfo
,
669 unsigned int protoff
, unsigned char **data
, int dataoff
,
670 TransportAddress
*taddr
)
672 int dir
= CTINFO2DIR(ctinfo
);
675 union nf_inet_addr addr
;
676 struct nf_conntrack_expect
*exp
;
677 typeof(nat_h245_hook
) nat_h245
;
679 /* Read h245Address */
680 if (!get_h225_addr(ct
, *data
, taddr
, &addr
, &port
) ||
681 memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) ||
685 /* Create expect for h245 connection */
686 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
688 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
689 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
690 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
691 IPPROTO_TCP
, NULL
, &port
);
692 exp
->helper
= &nf_conntrack_helper_h245
;
694 if (memcmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
,
695 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
696 sizeof(ct
->tuplehash
[dir
].tuple
.src
.u3
)) &&
697 (nat_h245
= rcu_dereference(nat_h245_hook
)) &&
698 nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
699 ct
->status
& IPS_NAT_MASK
) {
701 ret
= nat_h245(skb
, ct
, ctinfo
, protoff
, data
, dataoff
, taddr
,
703 } else { /* Conntrack only */
704 if (nf_ct_expect_related(exp
, 0) == 0) {
705 pr_debug("nf_ct_q931: expect H.245 ");
706 nf_ct_dump_tuple(&exp
->tuple
);
711 nf_ct_expect_put(exp
);
716 /* If the calling party is on the same side of the forward-to party,
717 * we don't need to track the second call
719 static int callforward_do_filter(struct net
*net
,
720 const union nf_inet_addr
*src
,
721 const union nf_inet_addr
*dst
,
728 struct flowi4 fl1
, fl2
;
729 struct rtable
*rt1
, *rt2
;
731 memset(&fl1
, 0, sizeof(fl1
));
734 memset(&fl2
, 0, sizeof(fl2
));
736 if (!nf_ip_route(net
, (struct dst_entry
**)&rt1
,
737 flowi4_to_flowi(&fl1
), false)) {
738 if (!nf_ip_route(net
, (struct dst_entry
**)&rt2
,
739 flowi4_to_flowi(&fl2
), false)) {
740 if (rt_nexthop(rt1
, fl1
.daddr
) ==
741 rt_nexthop(rt2
, fl2
.daddr
) &&
742 rt1
->dst
.dev
== rt2
->dst
.dev
)
744 dst_release(&rt2
->dst
);
746 dst_release(&rt1
->dst
);
750 #if IS_ENABLED(CONFIG_IPV6)
752 struct rt6_info
*rt1
, *rt2
;
753 struct flowi6 fl1
, fl2
;
755 memset(&fl1
, 0, sizeof(fl1
));
756 fl1
.daddr
= src
->in6
;
758 memset(&fl2
, 0, sizeof(fl2
));
759 fl2
.daddr
= dst
->in6
;
760 if (!nf_ip6_route(net
, (struct dst_entry
**)&rt1
,
761 flowi6_to_flowi(&fl1
), false)) {
762 if (!nf_ip6_route(net
, (struct dst_entry
**)&rt2
,
763 flowi6_to_flowi(&fl2
), false)) {
764 if (ipv6_addr_equal(rt6_nexthop(rt1
, &fl1
.daddr
),
765 rt6_nexthop(rt2
, &fl2
.daddr
)) &&
766 rt1
->dst
.dev
== rt2
->dst
.dev
)
768 dst_release(&rt2
->dst
);
770 dst_release(&rt1
->dst
);
780 static int expect_callforwarding(struct sk_buff
*skb
,
782 enum ip_conntrack_info ctinfo
,
783 unsigned int protoff
,
784 unsigned char **data
, int dataoff
,
785 TransportAddress
*taddr
)
787 int dir
= CTINFO2DIR(ctinfo
);
790 union nf_inet_addr addr
;
791 struct nf_conntrack_expect
*exp
;
792 struct net
*net
= nf_ct_net(ct
);
793 typeof(nat_callforwarding_hook
) nat_callforwarding
;
795 /* Read alternativeAddress */
796 if (!get_h225_addr(ct
, *data
, taddr
, &addr
, &port
) || port
== 0)
799 /* If the calling party is on the same side of the forward-to party,
800 * we don't need to track the second call
802 if (callforward_filter
&&
803 callforward_do_filter(net
, &addr
, &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
805 pr_debug("nf_ct_q931: Call Forwarding not tracked\n");
809 /* Create expect for the second call leg */
810 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
812 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
813 &ct
->tuplehash
[!dir
].tuple
.src
.u3
, &addr
,
814 IPPROTO_TCP
, NULL
, &port
);
815 exp
->helper
= nf_conntrack_helper_q931
;
817 if (memcmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
,
818 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
819 sizeof(ct
->tuplehash
[dir
].tuple
.src
.u3
)) &&
820 (nat_callforwarding
= rcu_dereference(nat_callforwarding_hook
)) &&
821 nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
822 ct
->status
& IPS_NAT_MASK
) {
824 ret
= nat_callforwarding(skb
, ct
, ctinfo
,
825 protoff
, data
, dataoff
,
827 } else { /* Conntrack only */
828 if (nf_ct_expect_related(exp
, 0) == 0) {
829 pr_debug("nf_ct_q931: expect Call Forwarding ");
830 nf_ct_dump_tuple(&exp
->tuple
);
835 nf_ct_expect_put(exp
);
840 static int process_setup(struct sk_buff
*skb
, struct nf_conn
*ct
,
841 enum ip_conntrack_info ctinfo
,
842 unsigned int protoff
,
843 unsigned char **data
, int dataoff
,
846 int dir
= CTINFO2DIR(ctinfo
);
850 union nf_inet_addr addr
;
851 typeof(set_h225_addr_hook
) set_h225_addr
;
853 pr_debug("nf_ct_q931: Setup\n");
855 if (setup
->options
& eSetup_UUIE_h245Address
) {
856 ret
= expect_h245(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
857 &setup
->h245Address
);
862 set_h225_addr
= rcu_dereference(set_h225_addr_hook
);
863 if ((setup
->options
& eSetup_UUIE_destCallSignalAddress
) &&
864 (set_h225_addr
) && nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
865 ct
->status
& IPS_NAT_MASK
&&
866 get_h225_addr(ct
, *data
, &setup
->destCallSignalAddress
,
868 memcmp(&addr
, &ct
->tuplehash
[!dir
].tuple
.src
.u3
, sizeof(addr
))) {
869 pr_debug("nf_ct_q931: set destCallSignalAddress %pI6:%hu->%pI6:%hu\n",
870 &addr
, ntohs(port
), &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
871 ntohs(ct
->tuplehash
[!dir
].tuple
.src
.u
.tcp
.port
));
872 ret
= set_h225_addr(skb
, protoff
, data
, dataoff
,
873 &setup
->destCallSignalAddress
,
874 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
875 ct
->tuplehash
[!dir
].tuple
.src
.u
.tcp
.port
);
880 if ((setup
->options
& eSetup_UUIE_sourceCallSignalAddress
) &&
881 (set_h225_addr
) && nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
882 ct
->status
& IPS_NAT_MASK
&&
883 get_h225_addr(ct
, *data
, &setup
->sourceCallSignalAddress
,
885 memcmp(&addr
, &ct
->tuplehash
[!dir
].tuple
.dst
.u3
, sizeof(addr
))) {
886 pr_debug("nf_ct_q931: set sourceCallSignalAddress %pI6:%hu->%pI6:%hu\n",
887 &addr
, ntohs(port
), &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
888 ntohs(ct
->tuplehash
[!dir
].tuple
.dst
.u
.tcp
.port
));
889 ret
= set_h225_addr(skb
, protoff
, data
, dataoff
,
890 &setup
->sourceCallSignalAddress
,
891 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
892 ct
->tuplehash
[!dir
].tuple
.dst
.u
.tcp
.port
);
897 if (setup
->options
& eSetup_UUIE_fastStart
) {
898 for (i
= 0; i
< setup
->fastStart
.count
; i
++) {
899 ret
= process_olc(skb
, ct
, ctinfo
,
900 protoff
, data
, dataoff
,
901 &setup
->fastStart
.item
[i
]);
910 static int process_callproceeding(struct sk_buff
*skb
,
912 enum ip_conntrack_info ctinfo
,
913 unsigned int protoff
,
914 unsigned char **data
, int dataoff
,
915 CallProceeding_UUIE
*callproc
)
920 pr_debug("nf_ct_q931: CallProceeding\n");
922 if (callproc
->options
& eCallProceeding_UUIE_h245Address
) {
923 ret
= expect_h245(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
924 &callproc
->h245Address
);
929 if (callproc
->options
& eCallProceeding_UUIE_fastStart
) {
930 for (i
= 0; i
< callproc
->fastStart
.count
; i
++) {
931 ret
= process_olc(skb
, ct
, ctinfo
,
932 protoff
, data
, dataoff
,
933 &callproc
->fastStart
.item
[i
]);
942 static int process_connect(struct sk_buff
*skb
, struct nf_conn
*ct
,
943 enum ip_conntrack_info ctinfo
,
944 unsigned int protoff
,
945 unsigned char **data
, int dataoff
,
946 Connect_UUIE
*connect
)
951 pr_debug("nf_ct_q931: Connect\n");
953 if (connect
->options
& eConnect_UUIE_h245Address
) {
954 ret
= expect_h245(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
955 &connect
->h245Address
);
960 if (connect
->options
& eConnect_UUIE_fastStart
) {
961 for (i
= 0; i
< connect
->fastStart
.count
; i
++) {
962 ret
= process_olc(skb
, ct
, ctinfo
,
963 protoff
, data
, dataoff
,
964 &connect
->fastStart
.item
[i
]);
973 static int process_alerting(struct sk_buff
*skb
, struct nf_conn
*ct
,
974 enum ip_conntrack_info ctinfo
,
975 unsigned int protoff
,
976 unsigned char **data
, int dataoff
,
977 Alerting_UUIE
*alert
)
982 pr_debug("nf_ct_q931: Alerting\n");
984 if (alert
->options
& eAlerting_UUIE_h245Address
) {
985 ret
= expect_h245(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
986 &alert
->h245Address
);
991 if (alert
->options
& eAlerting_UUIE_fastStart
) {
992 for (i
= 0; i
< alert
->fastStart
.count
; i
++) {
993 ret
= process_olc(skb
, ct
, ctinfo
,
994 protoff
, data
, dataoff
,
995 &alert
->fastStart
.item
[i
]);
1004 static int process_facility(struct sk_buff
*skb
, struct nf_conn
*ct
,
1005 enum ip_conntrack_info ctinfo
,
1006 unsigned int protoff
,
1007 unsigned char **data
, int dataoff
,
1008 Facility_UUIE
*facility
)
1013 pr_debug("nf_ct_q931: Facility\n");
1015 if (facility
->reason
.choice
== eFacilityReason_callForwarded
) {
1016 if (facility
->options
& eFacility_UUIE_alternativeAddress
)
1017 return expect_callforwarding(skb
, ct
, ctinfo
,
1018 protoff
, data
, dataoff
,
1020 alternativeAddress
);
1024 if (facility
->options
& eFacility_UUIE_h245Address
) {
1025 ret
= expect_h245(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
1026 &facility
->h245Address
);
1031 if (facility
->options
& eFacility_UUIE_fastStart
) {
1032 for (i
= 0; i
< facility
->fastStart
.count
; i
++) {
1033 ret
= process_olc(skb
, ct
, ctinfo
,
1034 protoff
, data
, dataoff
,
1035 &facility
->fastStart
.item
[i
]);
1044 static int process_progress(struct sk_buff
*skb
, struct nf_conn
*ct
,
1045 enum ip_conntrack_info ctinfo
,
1046 unsigned int protoff
,
1047 unsigned char **data
, int dataoff
,
1048 Progress_UUIE
*progress
)
1053 pr_debug("nf_ct_q931: Progress\n");
1055 if (progress
->options
& eProgress_UUIE_h245Address
) {
1056 ret
= expect_h245(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
1057 &progress
->h245Address
);
1062 if (progress
->options
& eProgress_UUIE_fastStart
) {
1063 for (i
= 0; i
< progress
->fastStart
.count
; i
++) {
1064 ret
= process_olc(skb
, ct
, ctinfo
,
1065 protoff
, data
, dataoff
,
1066 &progress
->fastStart
.item
[i
]);
1075 static int process_q931(struct sk_buff
*skb
, struct nf_conn
*ct
,
1076 enum ip_conntrack_info ctinfo
,
1077 unsigned int protoff
, unsigned char **data
, int dataoff
,
1080 H323_UU_PDU
*pdu
= &q931
->UUIE
.h323_uu_pdu
;
1084 switch (pdu
->h323_message_body
.choice
) {
1085 case eH323_UU_PDU_h323_message_body_setup
:
1086 ret
= process_setup(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
1087 &pdu
->h323_message_body
.setup
);
1089 case eH323_UU_PDU_h323_message_body_callProceeding
:
1090 ret
= process_callproceeding(skb
, ct
, ctinfo
,
1091 protoff
, data
, dataoff
,
1092 &pdu
->h323_message_body
.
1095 case eH323_UU_PDU_h323_message_body_connect
:
1096 ret
= process_connect(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
1097 &pdu
->h323_message_body
.connect
);
1099 case eH323_UU_PDU_h323_message_body_alerting
:
1100 ret
= process_alerting(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
1101 &pdu
->h323_message_body
.alerting
);
1103 case eH323_UU_PDU_h323_message_body_facility
:
1104 ret
= process_facility(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
1105 &pdu
->h323_message_body
.facility
);
1107 case eH323_UU_PDU_h323_message_body_progress
:
1108 ret
= process_progress(skb
, ct
, ctinfo
, protoff
, data
, dataoff
,
1109 &pdu
->h323_message_body
.progress
);
1112 pr_debug("nf_ct_q931: Q.931 signal %d\n",
1113 pdu
->h323_message_body
.choice
);
1120 if (pdu
->options
& eH323_UU_PDU_h245Control
) {
1121 for (i
= 0; i
< pdu
->h245Control
.count
; i
++) {
1122 ret
= process_h245(skb
, ct
, ctinfo
,
1123 protoff
, data
, dataoff
,
1124 &pdu
->h245Control
.item
[i
]);
1133 static int q931_help(struct sk_buff
*skb
, unsigned int protoff
,
1134 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
1137 unsigned char *data
= NULL
;
1142 /* Until there's been traffic both ways, don't look in packets. */
1143 if (ctinfo
!= IP_CT_ESTABLISHED
&& ctinfo
!= IP_CT_ESTABLISHED_REPLY
)
1146 pr_debug("nf_ct_q931: skblen = %u\n", skb
->len
);
1148 spin_lock_bh(&nf_h323_lock
);
1150 /* Process each TPKT */
1151 while (get_tpkt_data(skb
, protoff
, ct
, ctinfo
,
1152 &data
, &datalen
, &dataoff
)) {
1153 pr_debug("nf_ct_q931: TPKT len=%d ", datalen
);
1154 nf_ct_dump_tuple(&ct
->tuplehash
[CTINFO2DIR(ctinfo
)].tuple
);
1156 /* Decode Q.931 signal */
1157 ret
= DecodeQ931(data
, datalen
, &q931
);
1159 pr_debug("nf_ct_q931: decoding error: %s\n",
1160 ret
== H323_ERROR_BOUND
?
1161 "out of bound" : "out of range");
1162 /* We don't drop when decoding error */
1166 /* Process Q.931 signal */
1167 if (process_q931(skb
, ct
, ctinfo
, protoff
,
1168 &data
, dataoff
, &q931
) < 0)
1172 spin_unlock_bh(&nf_h323_lock
);
1176 spin_unlock_bh(&nf_h323_lock
);
1177 nf_ct_helper_log(skb
, ct
, "cannot process Q.931 message");
1181 static const struct nf_conntrack_expect_policy q931_exp_policy
= {
1182 /* T.120 and H.245 */
1183 .max_expected
= H323_RTP_CHANNEL_MAX
* 4 + 4,
1187 static struct nf_conntrack_helper nf_conntrack_helper_q931
[] __read_mostly
= {
1191 .tuple
.src
.l3num
= AF_INET
,
1192 .tuple
.src
.u
.tcp
.port
= cpu_to_be16(Q931_PORT
),
1193 .tuple
.dst
.protonum
= IPPROTO_TCP
,
1195 .expect_policy
= &q931_exp_policy
,
1200 .tuple
.src
.l3num
= AF_INET6
,
1201 .tuple
.src
.u
.tcp
.port
= cpu_to_be16(Q931_PORT
),
1202 .tuple
.dst
.protonum
= IPPROTO_TCP
,
1204 .expect_policy
= &q931_exp_policy
,
1208 static unsigned char *get_udp_data(struct sk_buff
*skb
, unsigned int protoff
,
1211 const struct udphdr
*uh
;
1215 uh
= skb_header_pointer(skb
, protoff
, sizeof(_uh
), &_uh
);
1218 dataoff
= protoff
+ sizeof(_uh
);
1219 if (dataoff
>= skb
->len
)
1221 *datalen
= skb
->len
- dataoff
;
1222 return skb_header_pointer(skb
, dataoff
, *datalen
, h323_buffer
);
1225 static struct nf_conntrack_expect
*find_expect(struct nf_conn
*ct
,
1226 union nf_inet_addr
*addr
,
1229 struct net
*net
= nf_ct_net(ct
);
1230 struct nf_conntrack_expect
*exp
;
1231 struct nf_conntrack_tuple tuple
;
1233 memset(&tuple
.src
.u3
, 0, sizeof(tuple
.src
.u3
));
1234 tuple
.src
.u
.tcp
.port
= 0;
1235 memcpy(&tuple
.dst
.u3
, addr
, sizeof(tuple
.dst
.u3
));
1236 tuple
.dst
.u
.tcp
.port
= port
;
1237 tuple
.dst
.protonum
= IPPROTO_TCP
;
1239 exp
= __nf_ct_expect_find(net
, nf_ct_zone(ct
), &tuple
);
1240 if (exp
&& exp
->master
== ct
)
1245 static int expect_q931(struct sk_buff
*skb
, struct nf_conn
*ct
,
1246 enum ip_conntrack_info ctinfo
,
1247 unsigned int protoff
, unsigned char **data
,
1248 TransportAddress
*taddr
, int count
)
1250 struct nf_ct_h323_master
*info
= nfct_help_data(ct
);
1251 int dir
= CTINFO2DIR(ctinfo
);
1255 union nf_inet_addr addr
;
1256 struct nf_conntrack_expect
*exp
;
1257 typeof(nat_q931_hook
) nat_q931
;
1259 /* Look for the first related address */
1260 for (i
= 0; i
< count
; i
++) {
1261 if (get_h225_addr(ct
, *data
, &taddr
[i
], &addr
, &port
) &&
1262 memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
,
1263 sizeof(addr
)) == 0 && port
!= 0)
1267 if (i
>= count
) /* Not found */
1270 /* Create expect for Q.931 */
1271 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
1273 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
1274 gkrouted_only
? /* only accept calls from GK? */
1275 &ct
->tuplehash
[!dir
].tuple
.src
.u3
: NULL
,
1276 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
1277 IPPROTO_TCP
, NULL
, &port
);
1278 exp
->helper
= nf_conntrack_helper_q931
;
1279 exp
->flags
= NF_CT_EXPECT_PERMANENT
; /* Accept multiple calls */
1281 nat_q931
= rcu_dereference(nat_q931_hook
);
1282 if (nat_q931
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1283 ct
->status
& IPS_NAT_MASK
) { /* Need NAT */
1284 ret
= nat_q931(skb
, ct
, ctinfo
, protoff
, data
,
1285 taddr
, i
, port
, exp
);
1286 } else { /* Conntrack only */
1287 if (nf_ct_expect_related(exp
, 0) == 0) {
1288 pr_debug("nf_ct_ras: expect Q.931 ");
1289 nf_ct_dump_tuple(&exp
->tuple
);
1291 /* Save port for looking up expect in processing RCF */
1292 info
->sig_port
[dir
] = port
;
1297 nf_ct_expect_put(exp
);
1302 static int process_grq(struct sk_buff
*skb
, struct nf_conn
*ct
,
1303 enum ip_conntrack_info ctinfo
,
1304 unsigned int protoff
,
1305 unsigned char **data
, GatekeeperRequest
*grq
)
1307 typeof(set_ras_addr_hook
) set_ras_addr
;
1309 pr_debug("nf_ct_ras: GRQ\n");
1311 set_ras_addr
= rcu_dereference(set_ras_addr_hook
);
1312 if (set_ras_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1313 ct
->status
& IPS_NAT_MASK
) /* NATed */
1314 return set_ras_addr(skb
, ct
, ctinfo
, protoff
, data
,
1315 &grq
->rasAddress
, 1);
1319 static int process_gcf(struct sk_buff
*skb
, struct nf_conn
*ct
,
1320 enum ip_conntrack_info ctinfo
,
1321 unsigned int protoff
,
1322 unsigned char **data
, GatekeeperConfirm
*gcf
)
1324 int dir
= CTINFO2DIR(ctinfo
);
1327 union nf_inet_addr addr
;
1328 struct nf_conntrack_expect
*exp
;
1330 pr_debug("nf_ct_ras: GCF\n");
1332 if (!get_h225_addr(ct
, *data
, &gcf
->rasAddress
, &addr
, &port
))
1335 /* Registration port is the same as discovery port */
1336 if (!memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) &&
1337 port
== ct
->tuplehash
[dir
].tuple
.src
.u
.udp
.port
)
1340 /* Avoid RAS expectation loops. A GCF is never expected. */
1341 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
1344 /* Need new expect */
1345 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
1347 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
1348 &ct
->tuplehash
[!dir
].tuple
.src
.u3
, &addr
,
1349 IPPROTO_UDP
, NULL
, &port
);
1350 exp
->helper
= nf_conntrack_helper_ras
;
1352 if (nf_ct_expect_related(exp
, 0) == 0) {
1353 pr_debug("nf_ct_ras: expect RAS ");
1354 nf_ct_dump_tuple(&exp
->tuple
);
1358 nf_ct_expect_put(exp
);
1363 static int process_rrq(struct sk_buff
*skb
, struct nf_conn
*ct
,
1364 enum ip_conntrack_info ctinfo
,
1365 unsigned int protoff
,
1366 unsigned char **data
, RegistrationRequest
*rrq
)
1368 struct nf_ct_h323_master
*info
= nfct_help_data(ct
);
1370 typeof(set_ras_addr_hook
) set_ras_addr
;
1372 pr_debug("nf_ct_ras: RRQ\n");
1374 ret
= expect_q931(skb
, ct
, ctinfo
, protoff
, data
,
1375 rrq
->callSignalAddress
.item
,
1376 rrq
->callSignalAddress
.count
);
1380 set_ras_addr
= rcu_dereference(set_ras_addr_hook
);
1381 if (set_ras_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1382 ct
->status
& IPS_NAT_MASK
) {
1383 ret
= set_ras_addr(skb
, ct
, ctinfo
, protoff
, data
,
1384 rrq
->rasAddress
.item
,
1385 rrq
->rasAddress
.count
);
1390 if (rrq
->options
& eRegistrationRequest_timeToLive
) {
1391 pr_debug("nf_ct_ras: RRQ TTL = %u seconds\n", rrq
->timeToLive
);
1392 info
->timeout
= rrq
->timeToLive
;
1394 info
->timeout
= default_rrq_ttl
;
1399 static int process_rcf(struct sk_buff
*skb
, struct nf_conn
*ct
,
1400 enum ip_conntrack_info ctinfo
,
1401 unsigned int protoff
,
1402 unsigned char **data
, RegistrationConfirm
*rcf
)
1404 struct nf_ct_h323_master
*info
= nfct_help_data(ct
);
1405 int dir
= CTINFO2DIR(ctinfo
);
1407 struct nf_conntrack_expect
*exp
;
1408 typeof(set_sig_addr_hook
) set_sig_addr
;
1410 pr_debug("nf_ct_ras: RCF\n");
1412 set_sig_addr
= rcu_dereference(set_sig_addr_hook
);
1413 if (set_sig_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1414 ct
->status
& IPS_NAT_MASK
) {
1415 ret
= set_sig_addr(skb
, ct
, ctinfo
, protoff
, data
,
1416 rcf
->callSignalAddress
.item
,
1417 rcf
->callSignalAddress
.count
);
1422 if (rcf
->options
& eRegistrationConfirm_timeToLive
) {
1423 pr_debug("nf_ct_ras: RCF TTL = %u seconds\n", rcf
->timeToLive
);
1424 info
->timeout
= rcf
->timeToLive
;
1427 if (info
->timeout
> 0) {
1428 pr_debug("nf_ct_ras: set RAS connection timeout to "
1429 "%u seconds\n", info
->timeout
);
1430 nf_ct_refresh(ct
, skb
, info
->timeout
* HZ
);
1432 /* Set expect timeout */
1433 spin_lock_bh(&nf_conntrack_expect_lock
);
1434 exp
= find_expect(ct
, &ct
->tuplehash
[dir
].tuple
.dst
.u3
,
1435 info
->sig_port
[!dir
]);
1437 pr_debug("nf_ct_ras: set Q.931 expect "
1438 "timeout to %u seconds for",
1440 nf_ct_dump_tuple(&exp
->tuple
);
1441 mod_timer_pending(&exp
->timeout
,
1442 jiffies
+ info
->timeout
* HZ
);
1444 spin_unlock_bh(&nf_conntrack_expect_lock
);
1450 static int process_urq(struct sk_buff
*skb
, struct nf_conn
*ct
,
1451 enum ip_conntrack_info ctinfo
,
1452 unsigned int protoff
,
1453 unsigned char **data
, UnregistrationRequest
*urq
)
1455 struct nf_ct_h323_master
*info
= nfct_help_data(ct
);
1456 int dir
= CTINFO2DIR(ctinfo
);
1458 typeof(set_sig_addr_hook
) set_sig_addr
;
1460 pr_debug("nf_ct_ras: URQ\n");
1462 set_sig_addr
= rcu_dereference(set_sig_addr_hook
);
1463 if (set_sig_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1464 ct
->status
& IPS_NAT_MASK
) {
1465 ret
= set_sig_addr(skb
, ct
, ctinfo
, protoff
, data
,
1466 urq
->callSignalAddress
.item
,
1467 urq
->callSignalAddress
.count
);
1472 /* Clear old expect */
1473 nf_ct_remove_expectations(ct
);
1474 info
->sig_port
[dir
] = 0;
1475 info
->sig_port
[!dir
] = 0;
1477 /* Give it 30 seconds for UCF or URJ */
1478 nf_ct_refresh(ct
, skb
, 30 * HZ
);
1483 static int process_arq(struct sk_buff
*skb
, struct nf_conn
*ct
,
1484 enum ip_conntrack_info ctinfo
,
1485 unsigned int protoff
,
1486 unsigned char **data
, AdmissionRequest
*arq
)
1488 const struct nf_ct_h323_master
*info
= nfct_help_data(ct
);
1489 int dir
= CTINFO2DIR(ctinfo
);
1491 union nf_inet_addr addr
;
1492 typeof(set_h225_addr_hook
) set_h225_addr
;
1494 pr_debug("nf_ct_ras: ARQ\n");
1496 set_h225_addr
= rcu_dereference(set_h225_addr_hook
);
1497 if ((arq
->options
& eAdmissionRequest_destCallSignalAddress
) &&
1498 get_h225_addr(ct
, *data
, &arq
->destCallSignalAddress
,
1500 !memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) &&
1501 port
== info
->sig_port
[dir
] &&
1502 nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1503 set_h225_addr
&& ct
->status
& IPS_NAT_MASK
) {
1505 return set_h225_addr(skb
, protoff
, data
, 0,
1506 &arq
->destCallSignalAddress
,
1507 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
1508 info
->sig_port
[!dir
]);
1511 if ((arq
->options
& eAdmissionRequest_srcCallSignalAddress
) &&
1512 get_h225_addr(ct
, *data
, &arq
->srcCallSignalAddress
,
1514 !memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
, sizeof(addr
)) &&
1515 set_h225_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1516 ct
->status
& IPS_NAT_MASK
) {
1518 return set_h225_addr(skb
, protoff
, data
, 0,
1519 &arq
->srcCallSignalAddress
,
1520 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
1527 static int process_acf(struct sk_buff
*skb
, struct nf_conn
*ct
,
1528 enum ip_conntrack_info ctinfo
,
1529 unsigned int protoff
,
1530 unsigned char **data
, AdmissionConfirm
*acf
)
1532 int dir
= CTINFO2DIR(ctinfo
);
1535 union nf_inet_addr addr
;
1536 struct nf_conntrack_expect
*exp
;
1537 typeof(set_sig_addr_hook
) set_sig_addr
;
1539 pr_debug("nf_ct_ras: ACF\n");
1541 if (!get_h225_addr(ct
, *data
, &acf
->destCallSignalAddress
,
1545 if (!memcmp(&addr
, &ct
->tuplehash
[dir
].tuple
.dst
.u3
, sizeof(addr
))) {
1547 set_sig_addr
= rcu_dereference(set_sig_addr_hook
);
1548 if (set_sig_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1549 ct
->status
& IPS_NAT_MASK
)
1550 return set_sig_addr(skb
, ct
, ctinfo
, protoff
, data
,
1551 &acf
->destCallSignalAddress
, 1);
1555 /* Need new expect */
1556 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
1558 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
1559 &ct
->tuplehash
[!dir
].tuple
.src
.u3
, &addr
,
1560 IPPROTO_TCP
, NULL
, &port
);
1561 exp
->flags
= NF_CT_EXPECT_PERMANENT
;
1562 exp
->helper
= nf_conntrack_helper_q931
;
1564 if (nf_ct_expect_related(exp
, 0) == 0) {
1565 pr_debug("nf_ct_ras: expect Q.931 ");
1566 nf_ct_dump_tuple(&exp
->tuple
);
1570 nf_ct_expect_put(exp
);
1575 static int process_lrq(struct sk_buff
*skb
, struct nf_conn
*ct
,
1576 enum ip_conntrack_info ctinfo
,
1577 unsigned int protoff
,
1578 unsigned char **data
, LocationRequest
*lrq
)
1580 typeof(set_ras_addr_hook
) set_ras_addr
;
1582 pr_debug("nf_ct_ras: LRQ\n");
1584 set_ras_addr
= rcu_dereference(set_ras_addr_hook
);
1585 if (set_ras_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1586 ct
->status
& IPS_NAT_MASK
)
1587 return set_ras_addr(skb
, ct
, ctinfo
, protoff
, data
,
1588 &lrq
->replyAddress
, 1);
1592 static int process_lcf(struct sk_buff
*skb
, struct nf_conn
*ct
,
1593 enum ip_conntrack_info ctinfo
,
1594 unsigned int protoff
,
1595 unsigned char **data
, LocationConfirm
*lcf
)
1597 int dir
= CTINFO2DIR(ctinfo
);
1600 union nf_inet_addr addr
;
1601 struct nf_conntrack_expect
*exp
;
1603 pr_debug("nf_ct_ras: LCF\n");
1605 if (!get_h225_addr(ct
, *data
, &lcf
->callSignalAddress
,
1609 /* Need new expect for call signal */
1610 if ((exp
= nf_ct_expect_alloc(ct
)) == NULL
)
1612 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
1613 &ct
->tuplehash
[!dir
].tuple
.src
.u3
, &addr
,
1614 IPPROTO_TCP
, NULL
, &port
);
1615 exp
->flags
= NF_CT_EXPECT_PERMANENT
;
1616 exp
->helper
= nf_conntrack_helper_q931
;
1618 if (nf_ct_expect_related(exp
, 0) == 0) {
1619 pr_debug("nf_ct_ras: expect Q.931 ");
1620 nf_ct_dump_tuple(&exp
->tuple
);
1624 nf_ct_expect_put(exp
);
1626 /* Ignore rasAddress */
1631 static int process_irr(struct sk_buff
*skb
, struct nf_conn
*ct
,
1632 enum ip_conntrack_info ctinfo
,
1633 unsigned int protoff
,
1634 unsigned char **data
, InfoRequestResponse
*irr
)
1637 typeof(set_ras_addr_hook
) set_ras_addr
;
1638 typeof(set_sig_addr_hook
) set_sig_addr
;
1640 pr_debug("nf_ct_ras: IRR\n");
1642 set_ras_addr
= rcu_dereference(set_ras_addr_hook
);
1643 if (set_ras_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1644 ct
->status
& IPS_NAT_MASK
) {
1645 ret
= set_ras_addr(skb
, ct
, ctinfo
, protoff
, data
,
1646 &irr
->rasAddress
, 1);
1651 set_sig_addr
= rcu_dereference(set_sig_addr_hook
);
1652 if (set_sig_addr
&& nf_ct_l3num(ct
) == NFPROTO_IPV4
&&
1653 ct
->status
& IPS_NAT_MASK
) {
1654 ret
= set_sig_addr(skb
, ct
, ctinfo
, protoff
, data
,
1655 irr
->callSignalAddress
.item
,
1656 irr
->callSignalAddress
.count
);
1664 static int process_ras(struct sk_buff
*skb
, struct nf_conn
*ct
,
1665 enum ip_conntrack_info ctinfo
,
1666 unsigned int protoff
,
1667 unsigned char **data
, RasMessage
*ras
)
1669 switch (ras
->choice
) {
1670 case eRasMessage_gatekeeperRequest
:
1671 return process_grq(skb
, ct
, ctinfo
, protoff
, data
,
1672 &ras
->gatekeeperRequest
);
1673 case eRasMessage_gatekeeperConfirm
:
1674 return process_gcf(skb
, ct
, ctinfo
, protoff
, data
,
1675 &ras
->gatekeeperConfirm
);
1676 case eRasMessage_registrationRequest
:
1677 return process_rrq(skb
, ct
, ctinfo
, protoff
, data
,
1678 &ras
->registrationRequest
);
1679 case eRasMessage_registrationConfirm
:
1680 return process_rcf(skb
, ct
, ctinfo
, protoff
, data
,
1681 &ras
->registrationConfirm
);
1682 case eRasMessage_unregistrationRequest
:
1683 return process_urq(skb
, ct
, ctinfo
, protoff
, data
,
1684 &ras
->unregistrationRequest
);
1685 case eRasMessage_admissionRequest
:
1686 return process_arq(skb
, ct
, ctinfo
, protoff
, data
,
1687 &ras
->admissionRequest
);
1688 case eRasMessage_admissionConfirm
:
1689 return process_acf(skb
, ct
, ctinfo
, protoff
, data
,
1690 &ras
->admissionConfirm
);
1691 case eRasMessage_locationRequest
:
1692 return process_lrq(skb
, ct
, ctinfo
, protoff
, data
,
1693 &ras
->locationRequest
);
1694 case eRasMessage_locationConfirm
:
1695 return process_lcf(skb
, ct
, ctinfo
, protoff
, data
,
1696 &ras
->locationConfirm
);
1697 case eRasMessage_infoRequestResponse
:
1698 return process_irr(skb
, ct
, ctinfo
, protoff
, data
,
1699 &ras
->infoRequestResponse
);
1701 pr_debug("nf_ct_ras: RAS message %d\n", ras
->choice
);
1708 static int ras_help(struct sk_buff
*skb
, unsigned int protoff
,
1709 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
1711 static RasMessage ras
;
1712 unsigned char *data
;
1716 pr_debug("nf_ct_ras: skblen = %u\n", skb
->len
);
1718 spin_lock_bh(&nf_h323_lock
);
1721 data
= get_udp_data(skb
, protoff
, &datalen
);
1724 pr_debug("nf_ct_ras: RAS message len=%d ", datalen
);
1725 nf_ct_dump_tuple(&ct
->tuplehash
[CTINFO2DIR(ctinfo
)].tuple
);
1727 /* Decode RAS message */
1728 ret
= DecodeRasMessage(data
, datalen
, &ras
);
1730 pr_debug("nf_ct_ras: decoding error: %s\n",
1731 ret
== H323_ERROR_BOUND
?
1732 "out of bound" : "out of range");
1736 /* Process RAS message */
1737 if (process_ras(skb
, ct
, ctinfo
, protoff
, &data
, &ras
) < 0)
1741 spin_unlock_bh(&nf_h323_lock
);
1745 spin_unlock_bh(&nf_h323_lock
);
1746 nf_ct_helper_log(skb
, ct
, "cannot process RAS message");
1750 static const struct nf_conntrack_expect_policy ras_exp_policy
= {
1755 static struct nf_conntrack_helper nf_conntrack_helper_ras
[] __read_mostly
= {
1759 .tuple
.src
.l3num
= AF_INET
,
1760 .tuple
.src
.u
.udp
.port
= cpu_to_be16(RAS_PORT
),
1761 .tuple
.dst
.protonum
= IPPROTO_UDP
,
1763 .expect_policy
= &ras_exp_policy
,
1768 .tuple
.src
.l3num
= AF_INET6
,
1769 .tuple
.src
.u
.udp
.port
= cpu_to_be16(RAS_PORT
),
1770 .tuple
.dst
.protonum
= IPPROTO_UDP
,
1772 .expect_policy
= &ras_exp_policy
,
1776 static int __init
h323_helper_init(void)
1780 ret
= nf_conntrack_helper_register(&nf_conntrack_helper_h245
);
1783 ret
= nf_conntrack_helpers_register(nf_conntrack_helper_q931
,
1784 ARRAY_SIZE(nf_conntrack_helper_q931
));
1787 ret
= nf_conntrack_helpers_register(nf_conntrack_helper_ras
,
1788 ARRAY_SIZE(nf_conntrack_helper_ras
));
1794 nf_conntrack_helpers_unregister(nf_conntrack_helper_q931
,
1795 ARRAY_SIZE(nf_conntrack_helper_q931
));
1797 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245
);
1801 static void __exit
h323_helper_exit(void)
1803 nf_conntrack_helpers_unregister(nf_conntrack_helper_ras
,
1804 ARRAY_SIZE(nf_conntrack_helper_ras
));
1805 nf_conntrack_helpers_unregister(nf_conntrack_helper_q931
,
1806 ARRAY_SIZE(nf_conntrack_helper_q931
));
1807 nf_conntrack_helper_unregister(&nf_conntrack_helper_h245
);
1810 static void __exit
nf_conntrack_h323_fini(void)
1814 pr_debug("nf_ct_h323: fini\n");
1817 static int __init
nf_conntrack_h323_init(void)
1821 NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_h323_master
));
1823 h323_buffer
= kmalloc(65536, GFP_KERNEL
);
1826 ret
= h323_helper_init();
1829 pr_debug("nf_ct_h323: init success\n");
1836 module_init(nf_conntrack_h323_init
);
1837 module_exit(nf_conntrack_h323_fini
);
1839 EXPORT_SYMBOL_GPL(get_h225_addr
);
1840 EXPORT_SYMBOL_GPL(set_h245_addr_hook
);
1841 EXPORT_SYMBOL_GPL(set_h225_addr_hook
);
1842 EXPORT_SYMBOL_GPL(set_sig_addr_hook
);
1843 EXPORT_SYMBOL_GPL(set_ras_addr_hook
);
1844 EXPORT_SYMBOL_GPL(nat_rtp_rtcp_hook
);
1845 EXPORT_SYMBOL_GPL(nat_t120_hook
);
1846 EXPORT_SYMBOL_GPL(nat_h245_hook
);
1847 EXPORT_SYMBOL_GPL(nat_callforwarding_hook
);
1848 EXPORT_SYMBOL_GPL(nat_q931_hook
);
1850 MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>");
1851 MODULE_DESCRIPTION("H.323 connection tracking helper");
1852 MODULE_LICENSE("GPL");
1853 MODULE_ALIAS("ip_conntrack_h323");
1854 MODULE_ALIAS_NFCT_HELPER("RAS");
1855 MODULE_ALIAS_NFCT_HELPER("Q.931");
1856 MODULE_ALIAS_NFCT_HELPER("H.245");