[media] rtl28xxu: 0ccd:00d7 TerraTec Cinergy T Stick+
[cascardo/linux.git] / net / netfilter / nf_conntrack_h323_main.c
1 /*
2  * H.323 connection tracking helper
3  *
4  * Copyright (c) 2006 Jing Min Zhao <zhaojingmin@users.sourceforge.net>
5  *
6  * This source code is licensed under General Public License version 2.
7  *
8  * Based on the 'brute force' H.323 connection tracking module by
9  * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
10  *
11  * For more information, please see http://nath323.sourceforge.net/
12  */
13
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/ctype.h>
17 #include <linux/inet.h>
18 #include <linux/in.h>
19 #include <linux/ip.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
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>
35
36 /* Parameters */
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");
40
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");
44
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)");
50
51 /* Hooks for NAT */
52 int (*set_h245_addr_hook) (struct sk_buff *skb, unsigned int protoff,
53                            unsigned char **data, int dataoff,
54                            H245_TransportAddress *taddr,
55                            union nf_inet_addr *addr, __be16 port)
56                            __read_mostly;
57 int (*set_h225_addr_hook) (struct sk_buff *skb, unsigned int protoff,
58                            unsigned char **data, int dataoff,
59                            TransportAddress *taddr,
60                            union nf_inet_addr *addr, __be16 port)
61                            __read_mostly;
62 int (*set_sig_addr_hook) (struct sk_buff *skb,
63                           struct nf_conn *ct,
64                           enum ip_conntrack_info ctinfo,
65                           unsigned int protoff, unsigned char **data,
66                           TransportAddress *taddr, int count) __read_mostly;
67 int (*set_ras_addr_hook) (struct sk_buff *skb,
68                           struct nf_conn *ct,
69                           enum ip_conntrack_info ctinfo,
70                           unsigned int protoff, unsigned char **data,
71                           TransportAddress *taddr, int count) __read_mostly;
72 int (*nat_rtp_rtcp_hook) (struct sk_buff *skb,
73                           struct nf_conn *ct,
74                           enum ip_conntrack_info ctinfo,
75                           unsigned int protoff,
76                           unsigned char **data, int dataoff,
77                           H245_TransportAddress *taddr,
78                           __be16 port, __be16 rtp_port,
79                           struct nf_conntrack_expect *rtp_exp,
80                           struct nf_conntrack_expect *rtcp_exp) __read_mostly;
81 int (*nat_t120_hook) (struct sk_buff *skb,
82                       struct nf_conn *ct,
83                       enum ip_conntrack_info ctinfo,
84                       unsigned int protoff,
85                       unsigned char **data, int dataoff,
86                       H245_TransportAddress *taddr, __be16 port,
87                       struct nf_conntrack_expect *exp) __read_mostly;
88 int (*nat_h245_hook) (struct sk_buff *skb,
89                       struct nf_conn *ct,
90                       enum ip_conntrack_info ctinfo,
91                       unsigned int protoff,
92                       unsigned char **data, int dataoff,
93                       TransportAddress *taddr, __be16 port,
94                       struct nf_conntrack_expect *exp) __read_mostly;
95 int (*nat_callforwarding_hook) (struct sk_buff *skb,
96                                 struct nf_conn *ct,
97                                 enum ip_conntrack_info ctinfo,
98                                 unsigned int protoff,
99                                 unsigned char **data, int dataoff,
100                                 TransportAddress *taddr, __be16 port,
101                                 struct nf_conntrack_expect *exp) __read_mostly;
102 int (*nat_q931_hook) (struct sk_buff *skb,
103                       struct nf_conn *ct,
104                       enum ip_conntrack_info ctinfo,
105                       unsigned int protoff,
106                       unsigned char **data, TransportAddress *taddr, int idx,
107                       __be16 port, struct nf_conntrack_expect *exp)
108                       __read_mostly;
109
110 static DEFINE_SPINLOCK(nf_h323_lock);
111 static char *h323_buffer;
112
113 static struct nf_conntrack_helper nf_conntrack_helper_h245;
114 static struct nf_conntrack_helper nf_conntrack_helper_q931[];
115 static struct nf_conntrack_helper nf_conntrack_helper_ras[];
116
117 /****************************************************************************/
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)
121 {
122         struct nf_ct_h323_master *info = nfct_help_data(ct);
123         int dir = CTINFO2DIR(ctinfo);
124         const struct tcphdr *th;
125         struct tcphdr _tcph;
126         int tcpdatalen;
127         int tcpdataoff;
128         unsigned char *tpkt;
129         int tpktlen;
130         int tpktoff;
131
132         /* Get TCP header */
133         th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
134         if (th == NULL)
135                 return 0;
136
137         /* Get TCP data offset */
138         tcpdataoff = protoff + th->doff * 4;
139
140         /* Get TCP data length */
141         tcpdatalen = skb->len - tcpdataoff;
142         if (tcpdatalen <= 0)    /* No TCP data */
143                 goto clear_out;
144
145         if (*data == NULL) {    /* first TPKT */
146                 /* Get first TPKT pointer */
147                 tpkt = skb_header_pointer(skb, tcpdataoff, tcpdatalen,
148                                           h323_buffer);
149                 BUG_ON(tpkt == NULL);
150
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
160                                          * received */
161                                         *data = tpkt;
162                                         *datalen = info->tpkt_len[dir];
163                                         *dataoff = 0;
164                                         goto out;
165                                 }
166
167                                 /* Fragmented TPKT */
168                                 pr_debug("nf_ct_h323: fragmented TPKT\n");
169                                 goto clear_out;
170                         }
171
172                         /* It is not even a TPKT */
173                         return 0;
174                 }
175                 tpktoff = 0;
176         } else {                /* Next TPKT */
177                 tpktoff = *dataoff + *datalen;
178                 tcpdatalen -= tpktoff;
179                 if (tcpdatalen <= 4)    /* No more TPKT */
180                         goto clear_out;
181                 tpkt = *data + *datalen;
182
183                 /* Validate TPKT identifier */
184                 if (tpkt[0] != 0x03 || tpkt[1] != 0)
185                         goto clear_out;
186         }
187
188         /* Validate TPKT length */
189         tpktlen = tpkt[2] * 256 + tpkt[3];
190         if (tpktlen < 4)
191                 goto clear_out;
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",
197                                  tpktlen - 4);
198                         info->tpkt_len[dir] = tpktlen - 4;
199                         return 0;
200                 }
201
202                 pr_debug("nf_ct_h323: incomplete TPKT (fragmented?)\n");
203                 goto clear_out;
204         }
205
206         /* This is the encapsulated data */
207         *data = tpkt + 4;
208         *datalen = tpktlen - 4;
209         *dataoff = tpktoff + 4;
210
211       out:
212         /* Clear TPKT length */
213         info->tpkt_len[dir] = 0;
214         return 1;
215
216       clear_out:
217         info->tpkt_len[dir] = 0;
218         return 0;
219 }
220
221 /****************************************************************************/
222 static int get_h245_addr(struct nf_conn *ct, const unsigned char *data,
223                          H245_TransportAddress *taddr,
224                          union nf_inet_addr *addr, __be16 *port)
225 {
226         const unsigned char *p;
227         int len;
228
229         if (taddr->choice != eH245_TransportAddress_unicastAddress)
230                 return 0;
231
232         switch (taddr->unicastAddress.choice) {
233         case eUnicastAddress_iPAddress:
234                 if (nf_ct_l3num(ct) != AF_INET)
235                         return 0;
236                 p = data + taddr->unicastAddress.iPAddress.network;
237                 len = 4;
238                 break;
239         case eUnicastAddress_iP6Address:
240                 if (nf_ct_l3num(ct) != AF_INET6)
241                         return 0;
242                 p = data + taddr->unicastAddress.iP6Address.network;
243                 len = 16;
244                 break;
245         default:
246                 return 0;
247         }
248
249         memcpy(addr, p, len);
250         memset((void *)addr + len, 0, sizeof(*addr) - len);
251         memcpy(port, p + len, sizeof(__be16));
252
253         return 1;
254 }
255
256 /****************************************************************************/
257 static int expect_rtp_rtcp(struct sk_buff *skb, struct nf_conn *ct,
258                            enum ip_conntrack_info ctinfo,
259                            unsigned int protoff,
260                            unsigned char **data, int dataoff,
261                            H245_TransportAddress *taddr)
262 {
263         int dir = CTINFO2DIR(ctinfo);
264         int ret = 0;
265         __be16 port;
266         __be16 rtp_port, rtcp_port;
267         union nf_inet_addr addr;
268         struct nf_conntrack_expect *rtp_exp;
269         struct nf_conntrack_expect *rtcp_exp;
270         typeof(nat_rtp_rtcp_hook) nat_rtp_rtcp;
271
272         /* Read RTP or RTCP address */
273         if (!get_h245_addr(ct, *data, taddr, &addr, &port) ||
274             memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
275             port == 0)
276                 return 0;
277
278         /* RTP port is even */
279         rtp_port = port & ~htons(1);
280         rtcp_port = port | htons(1);
281
282         /* Create expect for RTP */
283         if ((rtp_exp = nf_ct_expect_alloc(ct)) == NULL)
284                 return -1;
285         nf_ct_expect_init(rtp_exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
286                           &ct->tuplehash[!dir].tuple.src.u3,
287                           &ct->tuplehash[!dir].tuple.dst.u3,
288                           IPPROTO_UDP, NULL, &rtp_port);
289
290         /* Create expect for RTCP */
291         if ((rtcp_exp = nf_ct_expect_alloc(ct)) == NULL) {
292                 nf_ct_expect_put(rtp_exp);
293                 return -1;
294         }
295         nf_ct_expect_init(rtcp_exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
296                           &ct->tuplehash[!dir].tuple.src.u3,
297                           &ct->tuplehash[!dir].tuple.dst.u3,
298                           IPPROTO_UDP, NULL, &rtcp_port);
299
300         if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
301                    &ct->tuplehash[!dir].tuple.dst.u3,
302                    sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
303                    (nat_rtp_rtcp = rcu_dereference(nat_rtp_rtcp_hook)) &&
304                    nf_ct_l3num(ct) == NFPROTO_IPV4 &&
305                    ct->status & IPS_NAT_MASK) {
306                 /* NAT needed */
307                 ret = nat_rtp_rtcp(skb, ct, ctinfo, protoff, data, dataoff,
308                                    taddr, port, rtp_port, rtp_exp, rtcp_exp);
309         } else {                /* Conntrack only */
310                 if (nf_ct_expect_related(rtp_exp) == 0) {
311                         if (nf_ct_expect_related(rtcp_exp) == 0) {
312                                 pr_debug("nf_ct_h323: expect RTP ");
313                                 nf_ct_dump_tuple(&rtp_exp->tuple);
314                                 pr_debug("nf_ct_h323: expect RTCP ");
315                                 nf_ct_dump_tuple(&rtcp_exp->tuple);
316                         } else {
317                                 nf_ct_unexpect_related(rtp_exp);
318                                 ret = -1;
319                         }
320                 } else
321                         ret = -1;
322         }
323
324         nf_ct_expect_put(rtp_exp);
325         nf_ct_expect_put(rtcp_exp);
326
327         return ret;
328 }
329
330 /****************************************************************************/
331 static int expect_t120(struct sk_buff *skb,
332                        struct nf_conn *ct,
333                        enum ip_conntrack_info ctinfo,
334                        unsigned int protoff,
335                        unsigned char **data, int dataoff,
336                        H245_TransportAddress *taddr)
337 {
338         int dir = CTINFO2DIR(ctinfo);
339         int ret = 0;
340         __be16 port;
341         union nf_inet_addr addr;
342         struct nf_conntrack_expect *exp;
343         typeof(nat_t120_hook) nat_t120;
344
345         /* Read T.120 address */
346         if (!get_h245_addr(ct, *data, taddr, &addr, &port) ||
347             memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
348             port == 0)
349                 return 0;
350
351         /* Create expect for T.120 connections */
352         if ((exp = nf_ct_expect_alloc(ct)) == NULL)
353                 return -1;
354         nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
355                           &ct->tuplehash[!dir].tuple.src.u3,
356                           &ct->tuplehash[!dir].tuple.dst.u3,
357                           IPPROTO_TCP, NULL, &port);
358         exp->flags = NF_CT_EXPECT_PERMANENT;    /* Accept multiple channels */
359
360         if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
361                    &ct->tuplehash[!dir].tuple.dst.u3,
362                    sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
363             (nat_t120 = rcu_dereference(nat_t120_hook)) &&
364             nf_ct_l3num(ct) == NFPROTO_IPV4 &&
365             ct->status & IPS_NAT_MASK) {
366                 /* NAT needed */
367                 ret = nat_t120(skb, ct, ctinfo, protoff, data, dataoff, taddr,
368                                port, exp);
369         } else {                /* Conntrack only */
370                 if (nf_ct_expect_related(exp) == 0) {
371                         pr_debug("nf_ct_h323: expect T.120 ");
372                         nf_ct_dump_tuple(&exp->tuple);
373                 } else
374                         ret = -1;
375         }
376
377         nf_ct_expect_put(exp);
378
379         return ret;
380 }
381
382 /****************************************************************************/
383 static int process_h245_channel(struct sk_buff *skb,
384                                 struct nf_conn *ct,
385                                 enum ip_conntrack_info ctinfo,
386                                 unsigned int protoff,
387                                 unsigned char **data, int dataoff,
388                                 H2250LogicalChannelParameters *channel)
389 {
390         int ret;
391
392         if (channel->options & eH2250LogicalChannelParameters_mediaChannel) {
393                 /* RTP */
394                 ret = expect_rtp_rtcp(skb, ct, ctinfo, protoff, data, dataoff,
395                                       &channel->mediaChannel);
396                 if (ret < 0)
397                         return -1;
398         }
399
400         if (channel->
401             options & eH2250LogicalChannelParameters_mediaControlChannel) {
402                 /* RTCP */
403                 ret = expect_rtp_rtcp(skb, ct, ctinfo, protoff, data, dataoff,
404                                       &channel->mediaControlChannel);
405                 if (ret < 0)
406                         return -1;
407         }
408
409         return 0;
410 }
411
412 /****************************************************************************/
413 static int process_olc(struct sk_buff *skb, struct nf_conn *ct,
414                        enum ip_conntrack_info ctinfo,
415                        unsigned int protoff,
416                        unsigned char **data, int dataoff,
417                        OpenLogicalChannel *olc)
418 {
419         int ret;
420
421         pr_debug("nf_ct_h323: OpenLogicalChannel\n");
422
423         if (olc->forwardLogicalChannelParameters.multiplexParameters.choice ==
424             eOpenLogicalChannel_forwardLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters)
425         {
426                 ret = process_h245_channel(skb, ct, ctinfo,
427                                            protoff, data, dataoff,
428                                            &olc->
429                                            forwardLogicalChannelParameters.
430                                            multiplexParameters.
431                                            h2250LogicalChannelParameters);
432                 if (ret < 0)
433                         return -1;
434         }
435
436         if ((olc->options &
437              eOpenLogicalChannel_reverseLogicalChannelParameters) &&
438             (olc->reverseLogicalChannelParameters.options &
439              eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters)
440             && (olc->reverseLogicalChannelParameters.multiplexParameters.
441                 choice ==
442                 eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
443         {
444                 ret =
445                     process_h245_channel(skb, ct, ctinfo,
446                                          protoff, data, dataoff,
447                                          &olc->
448                                          reverseLogicalChannelParameters.
449                                          multiplexParameters.
450                                          h2250LogicalChannelParameters);
451                 if (ret < 0)
452                         return -1;
453         }
454
455         if ((olc->options & eOpenLogicalChannel_separateStack) &&
456             olc->forwardLogicalChannelParameters.dataType.choice ==
457             eDataType_data &&
458             olc->forwardLogicalChannelParameters.dataType.data.application.
459             choice == eDataApplicationCapability_application_t120 &&
460             olc->forwardLogicalChannelParameters.dataType.data.application.
461             t120.choice == eDataProtocolCapability_separateLANStack &&
462             olc->separateStack.networkAddress.choice ==
463             eNetworkAccessParameters_networkAddress_localAreaAddress) {
464                 ret = expect_t120(skb, ct, ctinfo, protoff, data, dataoff,
465                                   &olc->separateStack.networkAddress.
466                                   localAreaAddress);
467                 if (ret < 0)
468                         return -1;
469         }
470
471         return 0;
472 }
473
474 /****************************************************************************/
475 static int process_olca(struct sk_buff *skb, struct nf_conn *ct,
476                         enum ip_conntrack_info ctinfo,
477                         unsigned int protoff, unsigned char **data, int dataoff,
478                         OpenLogicalChannelAck *olca)
479 {
480         H2250LogicalChannelAckParameters *ack;
481         int ret;
482
483         pr_debug("nf_ct_h323: OpenLogicalChannelAck\n");
484
485         if ((olca->options &
486              eOpenLogicalChannelAck_reverseLogicalChannelParameters) &&
487             (olca->reverseLogicalChannelParameters.options &
488              eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters)
489             && (olca->reverseLogicalChannelParameters.multiplexParameters.
490                 choice ==
491                 eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters))
492         {
493                 ret = process_h245_channel(skb, ct, ctinfo,
494                                            protoff, data, dataoff,
495                                            &olca->
496                                            reverseLogicalChannelParameters.
497                                            multiplexParameters.
498                                            h2250LogicalChannelParameters);
499                 if (ret < 0)
500                         return -1;
501         }
502
503         if ((olca->options &
504              eOpenLogicalChannelAck_forwardMultiplexAckParameters) &&
505             (olca->forwardMultiplexAckParameters.choice ==
506              eOpenLogicalChannelAck_forwardMultiplexAckParameters_h2250LogicalChannelAckParameters))
507         {
508                 ack = &olca->forwardMultiplexAckParameters.
509                     h2250LogicalChannelAckParameters;
510                 if (ack->options &
511                     eH2250LogicalChannelAckParameters_mediaChannel) {
512                         /* RTP */
513                         ret = expect_rtp_rtcp(skb, ct, ctinfo,
514                                               protoff, data, dataoff,
515                                               &ack->mediaChannel);
516                         if (ret < 0)
517                                 return -1;
518                 }
519
520                 if (ack->options &
521                     eH2250LogicalChannelAckParameters_mediaControlChannel) {
522                         /* RTCP */
523                         ret = expect_rtp_rtcp(skb, ct, ctinfo,
524                                               protoff, data, dataoff,
525                                               &ack->mediaControlChannel);
526                         if (ret < 0)
527                                 return -1;
528                 }
529         }
530
531         if ((olca->options & eOpenLogicalChannelAck_separateStack) &&
532                 olca->separateStack.networkAddress.choice ==
533                 eNetworkAccessParameters_networkAddress_localAreaAddress) {
534                 ret = expect_t120(skb, ct, ctinfo, protoff, data, dataoff,
535                                   &olca->separateStack.networkAddress.
536                                   localAreaAddress);
537                 if (ret < 0)
538                         return -1;
539         }
540
541         return 0;
542 }
543
544 /****************************************************************************/
545 static int process_h245(struct sk_buff *skb, struct nf_conn *ct,
546                         enum ip_conntrack_info ctinfo,
547                         unsigned int protoff, unsigned char **data, int dataoff,
548                         MultimediaSystemControlMessage *mscm)
549 {
550         switch (mscm->choice) {
551         case eMultimediaSystemControlMessage_request:
552                 if (mscm->request.choice ==
553                     eRequestMessage_openLogicalChannel) {
554                         return process_olc(skb, ct, ctinfo,
555                                            protoff, data, dataoff,
556                                            &mscm->request.openLogicalChannel);
557                 }
558                 pr_debug("nf_ct_h323: H.245 Request %d\n",
559                          mscm->request.choice);
560                 break;
561         case eMultimediaSystemControlMessage_response:
562                 if (mscm->response.choice ==
563                     eResponseMessage_openLogicalChannelAck) {
564                         return process_olca(skb, ct, ctinfo,
565                                             protoff, data, dataoff,
566                                             &mscm->response.
567                                             openLogicalChannelAck);
568                 }
569                 pr_debug("nf_ct_h323: H.245 Response %d\n",
570                          mscm->response.choice);
571                 break;
572         default:
573                 pr_debug("nf_ct_h323: H.245 signal %d\n", mscm->choice);
574                 break;
575         }
576
577         return 0;
578 }
579
580 /****************************************************************************/
581 static int h245_help(struct sk_buff *skb, unsigned int protoff,
582                      struct nf_conn *ct, enum ip_conntrack_info ctinfo)
583 {
584         static MultimediaSystemControlMessage mscm;
585         unsigned char *data = NULL;
586         int datalen;
587         int dataoff;
588         int ret;
589
590         /* Until there's been traffic both ways, don't look in packets. */
591         if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
592                 return NF_ACCEPT;
593
594         pr_debug("nf_ct_h245: skblen = %u\n", skb->len);
595
596         spin_lock_bh(&nf_h323_lock);
597
598         /* Process each TPKT */
599         while (get_tpkt_data(skb, protoff, ct, ctinfo,
600                              &data, &datalen, &dataoff)) {
601                 pr_debug("nf_ct_h245: TPKT len=%d ", datalen);
602                 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
603
604                 /* Decode H.245 signal */
605                 ret = DecodeMultimediaSystemControlMessage(data, datalen,
606                                                            &mscm);
607                 if (ret < 0) {
608                         pr_debug("nf_ct_h245: decoding error: %s\n",
609                                  ret == H323_ERROR_BOUND ?
610                                  "out of bound" : "out of range");
611                         /* We don't drop when decoding error */
612                         break;
613                 }
614
615                 /* Process H.245 signal */
616                 if (process_h245(skb, ct, ctinfo, protoff,
617                                  &data, dataoff, &mscm) < 0)
618                         goto drop;
619         }
620
621         spin_unlock_bh(&nf_h323_lock);
622         return NF_ACCEPT;
623
624       drop:
625         spin_unlock_bh(&nf_h323_lock);
626         net_info_ratelimited("nf_ct_h245: packet dropped\n");
627         return NF_DROP;
628 }
629
630 /****************************************************************************/
631 static const struct nf_conntrack_expect_policy h245_exp_policy = {
632         .max_expected   = H323_RTP_CHANNEL_MAX * 4 + 2 /* T.120 */,
633         .timeout        = 240,
634 };
635
636 static struct nf_conntrack_helper nf_conntrack_helper_h245 __read_mostly = {
637         .name                   = "H.245",
638         .me                     = THIS_MODULE,
639         .data_len               = sizeof(struct nf_ct_h323_master),
640         .tuple.src.l3num        = AF_UNSPEC,
641         .tuple.dst.protonum     = IPPROTO_UDP,
642         .help                   = h245_help,
643         .expect_policy          = &h245_exp_policy,
644 };
645
646 /****************************************************************************/
647 int get_h225_addr(struct nf_conn *ct, unsigned char *data,
648                   TransportAddress *taddr,
649                   union nf_inet_addr *addr, __be16 *port)
650 {
651         const unsigned char *p;
652         int len;
653
654         switch (taddr->choice) {
655         case eTransportAddress_ipAddress:
656                 if (nf_ct_l3num(ct) != AF_INET)
657                         return 0;
658                 p = data + taddr->ipAddress.ip;
659                 len = 4;
660                 break;
661         case eTransportAddress_ip6Address:
662                 if (nf_ct_l3num(ct) != AF_INET6)
663                         return 0;
664                 p = data + taddr->ip6Address.ip;
665                 len = 16;
666                 break;
667         default:
668                 return 0;
669         }
670
671         memcpy(addr, p, len);
672         memset((void *)addr + len, 0, sizeof(*addr) - len);
673         memcpy(port, p + len, sizeof(__be16));
674
675         return 1;
676 }
677
678 /****************************************************************************/
679 static int expect_h245(struct sk_buff *skb, struct nf_conn *ct,
680                        enum ip_conntrack_info ctinfo,
681                        unsigned int protoff, unsigned char **data, int dataoff,
682                        TransportAddress *taddr)
683 {
684         int dir = CTINFO2DIR(ctinfo);
685         int ret = 0;
686         __be16 port;
687         union nf_inet_addr addr;
688         struct nf_conntrack_expect *exp;
689         typeof(nat_h245_hook) nat_h245;
690
691         /* Read h245Address */
692         if (!get_h225_addr(ct, *data, taddr, &addr, &port) ||
693             memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) ||
694             port == 0)
695                 return 0;
696
697         /* Create expect for h245 connection */
698         if ((exp = nf_ct_expect_alloc(ct)) == NULL)
699                 return -1;
700         nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
701                           &ct->tuplehash[!dir].tuple.src.u3,
702                           &ct->tuplehash[!dir].tuple.dst.u3,
703                           IPPROTO_TCP, NULL, &port);
704         exp->helper = &nf_conntrack_helper_h245;
705
706         if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
707                    &ct->tuplehash[!dir].tuple.dst.u3,
708                    sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
709             (nat_h245 = rcu_dereference(nat_h245_hook)) &&
710             nf_ct_l3num(ct) == NFPROTO_IPV4 &&
711             ct->status & IPS_NAT_MASK) {
712                 /* NAT needed */
713                 ret = nat_h245(skb, ct, ctinfo, protoff, data, dataoff, taddr,
714                                port, exp);
715         } else {                /* Conntrack only */
716                 if (nf_ct_expect_related(exp) == 0) {
717                         pr_debug("nf_ct_q931: expect H.245 ");
718                         nf_ct_dump_tuple(&exp->tuple);
719                 } else
720                         ret = -1;
721         }
722
723         nf_ct_expect_put(exp);
724
725         return ret;
726 }
727
728 /* If the calling party is on the same side of the forward-to party,
729  * we don't need to track the second call */
730 static int callforward_do_filter(const union nf_inet_addr *src,
731                                  const union nf_inet_addr *dst,
732                                  u_int8_t family)
733 {
734         const struct nf_afinfo *afinfo;
735         int ret = 0;
736
737         /* rcu_read_lock()ed by nf_hook_slow() */
738         afinfo = nf_get_afinfo(family);
739         if (!afinfo)
740                 return 0;
741
742         switch (family) {
743         case AF_INET: {
744                 struct flowi4 fl1, fl2;
745                 struct rtable *rt1, *rt2;
746
747                 memset(&fl1, 0, sizeof(fl1));
748                 fl1.daddr = src->ip;
749
750                 memset(&fl2, 0, sizeof(fl2));
751                 fl2.daddr = dst->ip;
752                 if (!afinfo->route(&init_net, (struct dst_entry **)&rt1,
753                                    flowi4_to_flowi(&fl1), false)) {
754                         if (!afinfo->route(&init_net, (struct dst_entry **)&rt2,
755                                            flowi4_to_flowi(&fl2), false)) {
756                                 if (rt1->rt_gateway == rt2->rt_gateway &&
757                                     rt1->dst.dev  == rt2->dst.dev)
758                                         ret = 1;
759                                 dst_release(&rt2->dst);
760                         }
761                         dst_release(&rt1->dst);
762                 }
763                 break;
764         }
765 #if IS_ENABLED(CONFIG_NF_CONNTRACK_IPV6)
766         case AF_INET6: {
767                 struct flowi6 fl1, fl2;
768                 struct rt6_info *rt1, *rt2;
769
770                 memset(&fl1, 0, sizeof(fl1));
771                 fl1.daddr = src->in6;
772
773                 memset(&fl2, 0, sizeof(fl2));
774                 fl2.daddr = dst->in6;
775                 if (!afinfo->route(&init_net, (struct dst_entry **)&rt1,
776                                    flowi6_to_flowi(&fl1), false)) {
777                         if (!afinfo->route(&init_net, (struct dst_entry **)&rt2,
778                                            flowi6_to_flowi(&fl2), false)) {
779                                 if (!memcmp(&rt1->rt6i_gateway, &rt2->rt6i_gateway,
780                                             sizeof(rt1->rt6i_gateway)) &&
781                                     rt1->dst.dev == rt2->dst.dev)
782                                         ret = 1;
783                                 dst_release(&rt2->dst);
784                         }
785                         dst_release(&rt1->dst);
786                 }
787                 break;
788         }
789 #endif
790         }
791         return ret;
792
793 }
794
795 /****************************************************************************/
796 static int expect_callforwarding(struct sk_buff *skb,
797                                  struct nf_conn *ct,
798                                  enum ip_conntrack_info ctinfo,
799                                  unsigned int protoff,
800                                  unsigned char **data, int dataoff,
801                                  TransportAddress *taddr)
802 {
803         int dir = CTINFO2DIR(ctinfo);
804         int ret = 0;
805         __be16 port;
806         union nf_inet_addr addr;
807         struct nf_conntrack_expect *exp;
808         typeof(nat_callforwarding_hook) nat_callforwarding;
809
810         /* Read alternativeAddress */
811         if (!get_h225_addr(ct, *data, taddr, &addr, &port) || port == 0)
812                 return 0;
813
814         /* If the calling party is on the same side of the forward-to party,
815          * we don't need to track the second call */
816         if (callforward_filter &&
817             callforward_do_filter(&addr, &ct->tuplehash[!dir].tuple.src.u3,
818                                   nf_ct_l3num(ct))) {
819                 pr_debug("nf_ct_q931: Call Forwarding not tracked\n");
820                 return 0;
821         }
822
823         /* Create expect for the second call leg */
824         if ((exp = nf_ct_expect_alloc(ct)) == NULL)
825                 return -1;
826         nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
827                           &ct->tuplehash[!dir].tuple.src.u3, &addr,
828                           IPPROTO_TCP, NULL, &port);
829         exp->helper = nf_conntrack_helper_q931;
830
831         if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
832                    &ct->tuplehash[!dir].tuple.dst.u3,
833                    sizeof(ct->tuplehash[dir].tuple.src.u3)) &&
834             (nat_callforwarding = rcu_dereference(nat_callforwarding_hook)) &&
835             nf_ct_l3num(ct) == NFPROTO_IPV4 &&
836             ct->status & IPS_NAT_MASK) {
837                 /* Need NAT */
838                 ret = nat_callforwarding(skb, ct, ctinfo,
839                                          protoff, data, dataoff,
840                                          taddr, port, exp);
841         } else {                /* Conntrack only */
842                 if (nf_ct_expect_related(exp) == 0) {
843                         pr_debug("nf_ct_q931: expect Call Forwarding ");
844                         nf_ct_dump_tuple(&exp->tuple);
845                 } else
846                         ret = -1;
847         }
848
849         nf_ct_expect_put(exp);
850
851         return ret;
852 }
853
854 /****************************************************************************/
855 static int process_setup(struct sk_buff *skb, struct nf_conn *ct,
856                          enum ip_conntrack_info ctinfo,
857                          unsigned int protoff,
858                          unsigned char **data, int dataoff,
859                          Setup_UUIE *setup)
860 {
861         int dir = CTINFO2DIR(ctinfo);
862         int ret;
863         int i;
864         __be16 port;
865         union nf_inet_addr addr;
866         typeof(set_h225_addr_hook) set_h225_addr;
867
868         pr_debug("nf_ct_q931: Setup\n");
869
870         if (setup->options & eSetup_UUIE_h245Address) {
871                 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
872                                   &setup->h245Address);
873                 if (ret < 0)
874                         return -1;
875         }
876
877         set_h225_addr = rcu_dereference(set_h225_addr_hook);
878         if ((setup->options & eSetup_UUIE_destCallSignalAddress) &&
879             (set_h225_addr) && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
880             ct->status & IPS_NAT_MASK &&
881             get_h225_addr(ct, *data, &setup->destCallSignalAddress,
882                           &addr, &port) &&
883             memcmp(&addr, &ct->tuplehash[!dir].tuple.src.u3, sizeof(addr))) {
884                 pr_debug("nf_ct_q931: set destCallSignalAddress %pI6:%hu->%pI6:%hu\n",
885                          &addr, ntohs(port), &ct->tuplehash[!dir].tuple.src.u3,
886                          ntohs(ct->tuplehash[!dir].tuple.src.u.tcp.port));
887                 ret = set_h225_addr(skb, protoff, data, dataoff,
888                                     &setup->destCallSignalAddress,
889                                     &ct->tuplehash[!dir].tuple.src.u3,
890                                     ct->tuplehash[!dir].tuple.src.u.tcp.port);
891                 if (ret < 0)
892                         return -1;
893         }
894
895         if ((setup->options & eSetup_UUIE_sourceCallSignalAddress) &&
896             (set_h225_addr) && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
897             ct->status & IPS_NAT_MASK &&
898             get_h225_addr(ct, *data, &setup->sourceCallSignalAddress,
899                           &addr, &port) &&
900             memcmp(&addr, &ct->tuplehash[!dir].tuple.dst.u3, sizeof(addr))) {
901                 pr_debug("nf_ct_q931: set sourceCallSignalAddress %pI6:%hu->%pI6:%hu\n",
902                          &addr, ntohs(port), &ct->tuplehash[!dir].tuple.dst.u3,
903                          ntohs(ct->tuplehash[!dir].tuple.dst.u.tcp.port));
904                 ret = set_h225_addr(skb, protoff, data, dataoff,
905                                     &setup->sourceCallSignalAddress,
906                                     &ct->tuplehash[!dir].tuple.dst.u3,
907                                     ct->tuplehash[!dir].tuple.dst.u.tcp.port);
908                 if (ret < 0)
909                         return -1;
910         }
911
912         if (setup->options & eSetup_UUIE_fastStart) {
913                 for (i = 0; i < setup->fastStart.count; i++) {
914                         ret = process_olc(skb, ct, ctinfo,
915                                           protoff, data, dataoff,
916                                           &setup->fastStart.item[i]);
917                         if (ret < 0)
918                                 return -1;
919                 }
920         }
921
922         return 0;
923 }
924
925 /****************************************************************************/
926 static int process_callproceeding(struct sk_buff *skb,
927                                   struct nf_conn *ct,
928                                   enum ip_conntrack_info ctinfo,
929                                   unsigned int protoff,
930                                   unsigned char **data, int dataoff,
931                                   CallProceeding_UUIE *callproc)
932 {
933         int ret;
934         int i;
935
936         pr_debug("nf_ct_q931: CallProceeding\n");
937
938         if (callproc->options & eCallProceeding_UUIE_h245Address) {
939                 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
940                                   &callproc->h245Address);
941                 if (ret < 0)
942                         return -1;
943         }
944
945         if (callproc->options & eCallProceeding_UUIE_fastStart) {
946                 for (i = 0; i < callproc->fastStart.count; i++) {
947                         ret = process_olc(skb, ct, ctinfo,
948                                           protoff, data, dataoff,
949                                           &callproc->fastStart.item[i]);
950                         if (ret < 0)
951                                 return -1;
952                 }
953         }
954
955         return 0;
956 }
957
958 /****************************************************************************/
959 static int process_connect(struct sk_buff *skb, struct nf_conn *ct,
960                            enum ip_conntrack_info ctinfo,
961                            unsigned int protoff,
962                            unsigned char **data, int dataoff,
963                            Connect_UUIE *connect)
964 {
965         int ret;
966         int i;
967
968         pr_debug("nf_ct_q931: Connect\n");
969
970         if (connect->options & eConnect_UUIE_h245Address) {
971                 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
972                                   &connect->h245Address);
973                 if (ret < 0)
974                         return -1;
975         }
976
977         if (connect->options & eConnect_UUIE_fastStart) {
978                 for (i = 0; i < connect->fastStart.count; i++) {
979                         ret = process_olc(skb, ct, ctinfo,
980                                           protoff, data, dataoff,
981                                           &connect->fastStart.item[i]);
982                         if (ret < 0)
983                                 return -1;
984                 }
985         }
986
987         return 0;
988 }
989
990 /****************************************************************************/
991 static int process_alerting(struct sk_buff *skb, struct nf_conn *ct,
992                             enum ip_conntrack_info ctinfo,
993                             unsigned int protoff,
994                             unsigned char **data, int dataoff,
995                             Alerting_UUIE *alert)
996 {
997         int ret;
998         int i;
999
1000         pr_debug("nf_ct_q931: Alerting\n");
1001
1002         if (alert->options & eAlerting_UUIE_h245Address) {
1003                 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
1004                                   &alert->h245Address);
1005                 if (ret < 0)
1006                         return -1;
1007         }
1008
1009         if (alert->options & eAlerting_UUIE_fastStart) {
1010                 for (i = 0; i < alert->fastStart.count; i++) {
1011                         ret = process_olc(skb, ct, ctinfo,
1012                                           protoff, data, dataoff,
1013                                           &alert->fastStart.item[i]);
1014                         if (ret < 0)
1015                                 return -1;
1016                 }
1017         }
1018
1019         return 0;
1020 }
1021
1022 /****************************************************************************/
1023 static int process_facility(struct sk_buff *skb, struct nf_conn *ct,
1024                             enum ip_conntrack_info ctinfo,
1025                             unsigned int protoff,
1026                             unsigned char **data, int dataoff,
1027                             Facility_UUIE *facility)
1028 {
1029         int ret;
1030         int i;
1031
1032         pr_debug("nf_ct_q931: Facility\n");
1033
1034         if (facility->reason.choice == eFacilityReason_callForwarded) {
1035                 if (facility->options & eFacility_UUIE_alternativeAddress)
1036                         return expect_callforwarding(skb, ct, ctinfo,
1037                                                      protoff, data, dataoff,
1038                                                      &facility->
1039                                                      alternativeAddress);
1040                 return 0;
1041         }
1042
1043         if (facility->options & eFacility_UUIE_h245Address) {
1044                 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
1045                                   &facility->h245Address);
1046                 if (ret < 0)
1047                         return -1;
1048         }
1049
1050         if (facility->options & eFacility_UUIE_fastStart) {
1051                 for (i = 0; i < facility->fastStart.count; i++) {
1052                         ret = process_olc(skb, ct, ctinfo,
1053                                           protoff, data, dataoff,
1054                                           &facility->fastStart.item[i]);
1055                         if (ret < 0)
1056                                 return -1;
1057                 }
1058         }
1059
1060         return 0;
1061 }
1062
1063 /****************************************************************************/
1064 static int process_progress(struct sk_buff *skb, struct nf_conn *ct,
1065                             enum ip_conntrack_info ctinfo,
1066                             unsigned int protoff,
1067                             unsigned char **data, int dataoff,
1068                             Progress_UUIE *progress)
1069 {
1070         int ret;
1071         int i;
1072
1073         pr_debug("nf_ct_q931: Progress\n");
1074
1075         if (progress->options & eProgress_UUIE_h245Address) {
1076                 ret = expect_h245(skb, ct, ctinfo, protoff, data, dataoff,
1077                                   &progress->h245Address);
1078                 if (ret < 0)
1079                         return -1;
1080         }
1081
1082         if (progress->options & eProgress_UUIE_fastStart) {
1083                 for (i = 0; i < progress->fastStart.count; i++) {
1084                         ret = process_olc(skb, ct, ctinfo,
1085                                           protoff, data, dataoff,
1086                                           &progress->fastStart.item[i]);
1087                         if (ret < 0)
1088                                 return -1;
1089                 }
1090         }
1091
1092         return 0;
1093 }
1094
1095 /****************************************************************************/
1096 static int process_q931(struct sk_buff *skb, struct nf_conn *ct,
1097                         enum ip_conntrack_info ctinfo,
1098                         unsigned int protoff, unsigned char **data, int dataoff,
1099                         Q931 *q931)
1100 {
1101         H323_UU_PDU *pdu = &q931->UUIE.h323_uu_pdu;
1102         int i;
1103         int ret = 0;
1104
1105         switch (pdu->h323_message_body.choice) {
1106         case eH323_UU_PDU_h323_message_body_setup:
1107                 ret = process_setup(skb, ct, ctinfo, protoff, data, dataoff,
1108                                     &pdu->h323_message_body.setup);
1109                 break;
1110         case eH323_UU_PDU_h323_message_body_callProceeding:
1111                 ret = process_callproceeding(skb, ct, ctinfo,
1112                                              protoff, data, dataoff,
1113                                              &pdu->h323_message_body.
1114                                              callProceeding);
1115                 break;
1116         case eH323_UU_PDU_h323_message_body_connect:
1117                 ret = process_connect(skb, ct, ctinfo, protoff, data, dataoff,
1118                                       &pdu->h323_message_body.connect);
1119                 break;
1120         case eH323_UU_PDU_h323_message_body_alerting:
1121                 ret = process_alerting(skb, ct, ctinfo, protoff, data, dataoff,
1122                                        &pdu->h323_message_body.alerting);
1123                 break;
1124         case eH323_UU_PDU_h323_message_body_facility:
1125                 ret = process_facility(skb, ct, ctinfo, protoff, data, dataoff,
1126                                        &pdu->h323_message_body.facility);
1127                 break;
1128         case eH323_UU_PDU_h323_message_body_progress:
1129                 ret = process_progress(skb, ct, ctinfo, protoff, data, dataoff,
1130                                        &pdu->h323_message_body.progress);
1131                 break;
1132         default:
1133                 pr_debug("nf_ct_q931: Q.931 signal %d\n",
1134                          pdu->h323_message_body.choice);
1135                 break;
1136         }
1137
1138         if (ret < 0)
1139                 return -1;
1140
1141         if (pdu->options & eH323_UU_PDU_h245Control) {
1142                 for (i = 0; i < pdu->h245Control.count; i++) {
1143                         ret = process_h245(skb, ct, ctinfo,
1144                                            protoff, data, dataoff,
1145                                            &pdu->h245Control.item[i]);
1146                         if (ret < 0)
1147                                 return -1;
1148                 }
1149         }
1150
1151         return 0;
1152 }
1153
1154 /****************************************************************************/
1155 static int q931_help(struct sk_buff *skb, unsigned int protoff,
1156                      struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1157 {
1158         static Q931 q931;
1159         unsigned char *data = NULL;
1160         int datalen;
1161         int dataoff;
1162         int ret;
1163
1164         /* Until there's been traffic both ways, don't look in packets. */
1165         if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
1166                 return NF_ACCEPT;
1167
1168         pr_debug("nf_ct_q931: skblen = %u\n", skb->len);
1169
1170         spin_lock_bh(&nf_h323_lock);
1171
1172         /* Process each TPKT */
1173         while (get_tpkt_data(skb, protoff, ct, ctinfo,
1174                              &data, &datalen, &dataoff)) {
1175                 pr_debug("nf_ct_q931: TPKT len=%d ", datalen);
1176                 nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1177
1178                 /* Decode Q.931 signal */
1179                 ret = DecodeQ931(data, datalen, &q931);
1180                 if (ret < 0) {
1181                         pr_debug("nf_ct_q931: decoding error: %s\n",
1182                                  ret == H323_ERROR_BOUND ?
1183                                  "out of bound" : "out of range");
1184                         /* We don't drop when decoding error */
1185                         break;
1186                 }
1187
1188                 /* Process Q.931 signal */
1189                 if (process_q931(skb, ct, ctinfo, protoff,
1190                                  &data, dataoff, &q931) < 0)
1191                         goto drop;
1192         }
1193
1194         spin_unlock_bh(&nf_h323_lock);
1195         return NF_ACCEPT;
1196
1197       drop:
1198         spin_unlock_bh(&nf_h323_lock);
1199         net_info_ratelimited("nf_ct_q931: packet dropped\n");
1200         return NF_DROP;
1201 }
1202
1203 /****************************************************************************/
1204 static const struct nf_conntrack_expect_policy q931_exp_policy = {
1205         /* T.120 and H.245 */
1206         .max_expected           = H323_RTP_CHANNEL_MAX * 4 + 4,
1207         .timeout                = 240,
1208 };
1209
1210 static struct nf_conntrack_helper nf_conntrack_helper_q931[] __read_mostly = {
1211         {
1212                 .name                   = "Q.931",
1213                 .me                     = THIS_MODULE,
1214                 .data_len               = sizeof(struct nf_ct_h323_master),
1215                 .tuple.src.l3num        = AF_INET,
1216                 .tuple.src.u.tcp.port   = cpu_to_be16(Q931_PORT),
1217                 .tuple.dst.protonum     = IPPROTO_TCP,
1218                 .help                   = q931_help,
1219                 .expect_policy          = &q931_exp_policy,
1220         },
1221         {
1222                 .name                   = "Q.931",
1223                 .me                     = THIS_MODULE,
1224                 .tuple.src.l3num        = AF_INET6,
1225                 .tuple.src.u.tcp.port   = cpu_to_be16(Q931_PORT),
1226                 .tuple.dst.protonum     = IPPROTO_TCP,
1227                 .help                   = q931_help,
1228                 .expect_policy          = &q931_exp_policy,
1229         },
1230 };
1231
1232 /****************************************************************************/
1233 static unsigned char *get_udp_data(struct sk_buff *skb, unsigned int protoff,
1234                                    int *datalen)
1235 {
1236         const struct udphdr *uh;
1237         struct udphdr _uh;
1238         int dataoff;
1239
1240         uh = skb_header_pointer(skb, protoff, sizeof(_uh), &_uh);
1241         if (uh == NULL)
1242                 return NULL;
1243         dataoff = protoff + sizeof(_uh);
1244         if (dataoff >= skb->len)
1245                 return NULL;
1246         *datalen = skb->len - dataoff;
1247         return skb_header_pointer(skb, dataoff, *datalen, h323_buffer);
1248 }
1249
1250 /****************************************************************************/
1251 static struct nf_conntrack_expect *find_expect(struct nf_conn *ct,
1252                                                union nf_inet_addr *addr,
1253                                                __be16 port)
1254 {
1255         struct net *net = nf_ct_net(ct);
1256         struct nf_conntrack_expect *exp;
1257         struct nf_conntrack_tuple tuple;
1258
1259         memset(&tuple.src.u3, 0, sizeof(tuple.src.u3));
1260         tuple.src.u.tcp.port = 0;
1261         memcpy(&tuple.dst.u3, addr, sizeof(tuple.dst.u3));
1262         tuple.dst.u.tcp.port = port;
1263         tuple.dst.protonum = IPPROTO_TCP;
1264
1265         exp = __nf_ct_expect_find(net, nf_ct_zone(ct), &tuple);
1266         if (exp && exp->master == ct)
1267                 return exp;
1268         return NULL;
1269 }
1270
1271 /****************************************************************************/
1272 static int set_expect_timeout(struct nf_conntrack_expect *exp,
1273                               unsigned int timeout)
1274 {
1275         if (!exp || !del_timer(&exp->timeout))
1276                 return 0;
1277
1278         exp->timeout.expires = jiffies + timeout * HZ;
1279         add_timer(&exp->timeout);
1280
1281         return 1;
1282 }
1283
1284 /****************************************************************************/
1285 static int expect_q931(struct sk_buff *skb, struct nf_conn *ct,
1286                        enum ip_conntrack_info ctinfo,
1287                        unsigned int protoff, unsigned char **data,
1288                        TransportAddress *taddr, int count)
1289 {
1290         struct nf_ct_h323_master *info = nfct_help_data(ct);
1291         int dir = CTINFO2DIR(ctinfo);
1292         int ret = 0;
1293         int i;
1294         __be16 port;
1295         union nf_inet_addr addr;
1296         struct nf_conntrack_expect *exp;
1297         typeof(nat_q931_hook) nat_q931;
1298
1299         /* Look for the first related address */
1300         for (i = 0; i < count; i++) {
1301                 if (get_h225_addr(ct, *data, &taddr[i], &addr, &port) &&
1302                     memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3,
1303                            sizeof(addr)) == 0 && port != 0)
1304                         break;
1305         }
1306
1307         if (i >= count)         /* Not found */
1308                 return 0;
1309
1310         /* Create expect for Q.931 */
1311         if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1312                 return -1;
1313         nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1314                           gkrouted_only ? /* only accept calls from GK? */
1315                                 &ct->tuplehash[!dir].tuple.src.u3 : NULL,
1316                           &ct->tuplehash[!dir].tuple.dst.u3,
1317                           IPPROTO_TCP, NULL, &port);
1318         exp->helper = nf_conntrack_helper_q931;
1319         exp->flags = NF_CT_EXPECT_PERMANENT;    /* Accept multiple calls */
1320
1321         nat_q931 = rcu_dereference(nat_q931_hook);
1322         if (nat_q931 && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1323             ct->status & IPS_NAT_MASK) {        /* Need NAT */
1324                 ret = nat_q931(skb, ct, ctinfo, protoff, data,
1325                                taddr, i, port, exp);
1326         } else {                /* Conntrack only */
1327                 if (nf_ct_expect_related(exp) == 0) {
1328                         pr_debug("nf_ct_ras: expect Q.931 ");
1329                         nf_ct_dump_tuple(&exp->tuple);
1330
1331                         /* Save port for looking up expect in processing RCF */
1332                         info->sig_port[dir] = port;
1333                 } else
1334                         ret = -1;
1335         }
1336
1337         nf_ct_expect_put(exp);
1338
1339         return ret;
1340 }
1341
1342 /****************************************************************************/
1343 static int process_grq(struct sk_buff *skb, struct nf_conn *ct,
1344                        enum ip_conntrack_info ctinfo,
1345                        unsigned int protoff,
1346                        unsigned char **data, GatekeeperRequest *grq)
1347 {
1348         typeof(set_ras_addr_hook) set_ras_addr;
1349
1350         pr_debug("nf_ct_ras: GRQ\n");
1351
1352         set_ras_addr = rcu_dereference(set_ras_addr_hook);
1353         if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1354             ct->status & IPS_NAT_MASK)  /* NATed */
1355                 return set_ras_addr(skb, ct, ctinfo, protoff, data,
1356                                     &grq->rasAddress, 1);
1357         return 0;
1358 }
1359
1360 /****************************************************************************/
1361 static int process_gcf(struct sk_buff *skb, struct nf_conn *ct,
1362                        enum ip_conntrack_info ctinfo,
1363                        unsigned int protoff,
1364                        unsigned char **data, GatekeeperConfirm *gcf)
1365 {
1366         int dir = CTINFO2DIR(ctinfo);
1367         int ret = 0;
1368         __be16 port;
1369         union nf_inet_addr addr;
1370         struct nf_conntrack_expect *exp;
1371
1372         pr_debug("nf_ct_ras: GCF\n");
1373
1374         if (!get_h225_addr(ct, *data, &gcf->rasAddress, &addr, &port))
1375                 return 0;
1376
1377         /* Registration port is the same as discovery port */
1378         if (!memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1379             port == ct->tuplehash[dir].tuple.src.u.udp.port)
1380                 return 0;
1381
1382         /* Avoid RAS expectation loops. A GCF is never expected. */
1383         if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1384                 return 0;
1385
1386         /* Need new expect */
1387         if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1388                 return -1;
1389         nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1390                           &ct->tuplehash[!dir].tuple.src.u3, &addr,
1391                           IPPROTO_UDP, NULL, &port);
1392         exp->helper = nf_conntrack_helper_ras;
1393
1394         if (nf_ct_expect_related(exp) == 0) {
1395                 pr_debug("nf_ct_ras: expect RAS ");
1396                 nf_ct_dump_tuple(&exp->tuple);
1397         } else
1398                 ret = -1;
1399
1400         nf_ct_expect_put(exp);
1401
1402         return ret;
1403 }
1404
1405 /****************************************************************************/
1406 static int process_rrq(struct sk_buff *skb, struct nf_conn *ct,
1407                        enum ip_conntrack_info ctinfo,
1408                        unsigned int protoff,
1409                        unsigned char **data, RegistrationRequest *rrq)
1410 {
1411         struct nf_ct_h323_master *info = nfct_help_data(ct);
1412         int ret;
1413         typeof(set_ras_addr_hook) set_ras_addr;
1414
1415         pr_debug("nf_ct_ras: RRQ\n");
1416
1417         ret = expect_q931(skb, ct, ctinfo, protoff, data,
1418                           rrq->callSignalAddress.item,
1419                           rrq->callSignalAddress.count);
1420         if (ret < 0)
1421                 return -1;
1422
1423         set_ras_addr = rcu_dereference(set_ras_addr_hook);
1424         if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1425             ct->status & IPS_NAT_MASK) {
1426                 ret = set_ras_addr(skb, ct, ctinfo, protoff, data,
1427                                    rrq->rasAddress.item,
1428                                    rrq->rasAddress.count);
1429                 if (ret < 0)
1430                         return -1;
1431         }
1432
1433         if (rrq->options & eRegistrationRequest_timeToLive) {
1434                 pr_debug("nf_ct_ras: RRQ TTL = %u seconds\n", rrq->timeToLive);
1435                 info->timeout = rrq->timeToLive;
1436         } else
1437                 info->timeout = default_rrq_ttl;
1438
1439         return 0;
1440 }
1441
1442 /****************************************************************************/
1443 static int process_rcf(struct sk_buff *skb, struct nf_conn *ct,
1444                        enum ip_conntrack_info ctinfo,
1445                        unsigned int protoff,
1446                        unsigned char **data, RegistrationConfirm *rcf)
1447 {
1448         struct nf_ct_h323_master *info = nfct_help_data(ct);
1449         int dir = CTINFO2DIR(ctinfo);
1450         int ret;
1451         struct nf_conntrack_expect *exp;
1452         typeof(set_sig_addr_hook) set_sig_addr;
1453
1454         pr_debug("nf_ct_ras: RCF\n");
1455
1456         set_sig_addr = rcu_dereference(set_sig_addr_hook);
1457         if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1458             ct->status & IPS_NAT_MASK) {
1459                 ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1460                                         rcf->callSignalAddress.item,
1461                                         rcf->callSignalAddress.count);
1462                 if (ret < 0)
1463                         return -1;
1464         }
1465
1466         if (rcf->options & eRegistrationConfirm_timeToLive) {
1467                 pr_debug("nf_ct_ras: RCF TTL = %u seconds\n", rcf->timeToLive);
1468                 info->timeout = rcf->timeToLive;
1469         }
1470
1471         if (info->timeout > 0) {
1472                 pr_debug("nf_ct_ras: set RAS connection timeout to "
1473                          "%u seconds\n", info->timeout);
1474                 nf_ct_refresh(ct, skb, info->timeout * HZ);
1475
1476                 /* Set expect timeout */
1477                 spin_lock_bh(&nf_conntrack_lock);
1478                 exp = find_expect(ct, &ct->tuplehash[dir].tuple.dst.u3,
1479                                   info->sig_port[!dir]);
1480                 if (exp) {
1481                         pr_debug("nf_ct_ras: set Q.931 expect "
1482                                  "timeout to %u seconds for",
1483                                  info->timeout);
1484                         nf_ct_dump_tuple(&exp->tuple);
1485                         set_expect_timeout(exp, info->timeout);
1486                 }
1487                 spin_unlock_bh(&nf_conntrack_lock);
1488         }
1489
1490         return 0;
1491 }
1492
1493 /****************************************************************************/
1494 static int process_urq(struct sk_buff *skb, struct nf_conn *ct,
1495                        enum ip_conntrack_info ctinfo,
1496                        unsigned int protoff,
1497                        unsigned char **data, UnregistrationRequest *urq)
1498 {
1499         struct nf_ct_h323_master *info = nfct_help_data(ct);
1500         int dir = CTINFO2DIR(ctinfo);
1501         int ret;
1502         typeof(set_sig_addr_hook) set_sig_addr;
1503
1504         pr_debug("nf_ct_ras: URQ\n");
1505
1506         set_sig_addr = rcu_dereference(set_sig_addr_hook);
1507         if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1508             ct->status & IPS_NAT_MASK) {
1509                 ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1510                                    urq->callSignalAddress.item,
1511                                    urq->callSignalAddress.count);
1512                 if (ret < 0)
1513                         return -1;
1514         }
1515
1516         /* Clear old expect */
1517         nf_ct_remove_expectations(ct);
1518         info->sig_port[dir] = 0;
1519         info->sig_port[!dir] = 0;
1520
1521         /* Give it 30 seconds for UCF or URJ */
1522         nf_ct_refresh(ct, skb, 30 * HZ);
1523
1524         return 0;
1525 }
1526
1527 /****************************************************************************/
1528 static int process_arq(struct sk_buff *skb, struct nf_conn *ct,
1529                        enum ip_conntrack_info ctinfo,
1530                        unsigned int protoff,
1531                        unsigned char **data, AdmissionRequest *arq)
1532 {
1533         const struct nf_ct_h323_master *info = nfct_help_data(ct);
1534         int dir = CTINFO2DIR(ctinfo);
1535         __be16 port;
1536         union nf_inet_addr addr;
1537         typeof(set_h225_addr_hook) set_h225_addr;
1538
1539         pr_debug("nf_ct_ras: ARQ\n");
1540
1541         set_h225_addr = rcu_dereference(set_h225_addr_hook);
1542         if ((arq->options & eAdmissionRequest_destCallSignalAddress) &&
1543             get_h225_addr(ct, *data, &arq->destCallSignalAddress,
1544                           &addr, &port) &&
1545             !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1546             port == info->sig_port[dir] &&
1547             nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1548             set_h225_addr && ct->status & IPS_NAT_MASK) {
1549                 /* Answering ARQ */
1550                 return set_h225_addr(skb, protoff, data, 0,
1551                                      &arq->destCallSignalAddress,
1552                                      &ct->tuplehash[!dir].tuple.dst.u3,
1553                                      info->sig_port[!dir]);
1554         }
1555
1556         if ((arq->options & eAdmissionRequest_srcCallSignalAddress) &&
1557             get_h225_addr(ct, *data, &arq->srcCallSignalAddress,
1558                           &addr, &port) &&
1559             !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) &&
1560             set_h225_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1561             ct->status & IPS_NAT_MASK) {
1562                 /* Calling ARQ */
1563                 return set_h225_addr(skb, protoff, data, 0,
1564                                      &arq->srcCallSignalAddress,
1565                                      &ct->tuplehash[!dir].tuple.dst.u3,
1566                                      port);
1567         }
1568
1569         return 0;
1570 }
1571
1572 /****************************************************************************/
1573 static int process_acf(struct sk_buff *skb, struct nf_conn *ct,
1574                        enum ip_conntrack_info ctinfo,
1575                        unsigned int protoff,
1576                        unsigned char **data, AdmissionConfirm *acf)
1577 {
1578         int dir = CTINFO2DIR(ctinfo);
1579         int ret = 0;
1580         __be16 port;
1581         union nf_inet_addr addr;
1582         struct nf_conntrack_expect *exp;
1583         typeof(set_sig_addr_hook) set_sig_addr;
1584
1585         pr_debug("nf_ct_ras: ACF\n");
1586
1587         if (!get_h225_addr(ct, *data, &acf->destCallSignalAddress,
1588                            &addr, &port))
1589                 return 0;
1590
1591         if (!memcmp(&addr, &ct->tuplehash[dir].tuple.dst.u3, sizeof(addr))) {
1592                 /* Answering ACF */
1593                 set_sig_addr = rcu_dereference(set_sig_addr_hook);
1594                 if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1595                     ct->status & IPS_NAT_MASK)
1596                         return set_sig_addr(skb, ct, ctinfo, protoff, data,
1597                                             &acf->destCallSignalAddress, 1);
1598                 return 0;
1599         }
1600
1601         /* Need new expect */
1602         if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1603                 return -1;
1604         nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1605                           &ct->tuplehash[!dir].tuple.src.u3, &addr,
1606                           IPPROTO_TCP, NULL, &port);
1607         exp->flags = NF_CT_EXPECT_PERMANENT;
1608         exp->helper = nf_conntrack_helper_q931;
1609
1610         if (nf_ct_expect_related(exp) == 0) {
1611                 pr_debug("nf_ct_ras: expect Q.931 ");
1612                 nf_ct_dump_tuple(&exp->tuple);
1613         } else
1614                 ret = -1;
1615
1616         nf_ct_expect_put(exp);
1617
1618         return ret;
1619 }
1620
1621 /****************************************************************************/
1622 static int process_lrq(struct sk_buff *skb, struct nf_conn *ct,
1623                        enum ip_conntrack_info ctinfo,
1624                        unsigned int protoff,
1625                        unsigned char **data, LocationRequest *lrq)
1626 {
1627         typeof(set_ras_addr_hook) set_ras_addr;
1628
1629         pr_debug("nf_ct_ras: LRQ\n");
1630
1631         set_ras_addr = rcu_dereference(set_ras_addr_hook);
1632         if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1633             ct->status & IPS_NAT_MASK)
1634                 return set_ras_addr(skb, ct, ctinfo, protoff, data,
1635                                     &lrq->replyAddress, 1);
1636         return 0;
1637 }
1638
1639 /****************************************************************************/
1640 static int process_lcf(struct sk_buff *skb, struct nf_conn *ct,
1641                        enum ip_conntrack_info ctinfo,
1642                        unsigned int protoff,
1643                        unsigned char **data, LocationConfirm *lcf)
1644 {
1645         int dir = CTINFO2DIR(ctinfo);
1646         int ret = 0;
1647         __be16 port;
1648         union nf_inet_addr addr;
1649         struct nf_conntrack_expect *exp;
1650
1651         pr_debug("nf_ct_ras: LCF\n");
1652
1653         if (!get_h225_addr(ct, *data, &lcf->callSignalAddress,
1654                            &addr, &port))
1655                 return 0;
1656
1657         /* Need new expect for call signal */
1658         if ((exp = nf_ct_expect_alloc(ct)) == NULL)
1659                 return -1;
1660         nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1661                           &ct->tuplehash[!dir].tuple.src.u3, &addr,
1662                           IPPROTO_TCP, NULL, &port);
1663         exp->flags = NF_CT_EXPECT_PERMANENT;
1664         exp->helper = nf_conntrack_helper_q931;
1665
1666         if (nf_ct_expect_related(exp) == 0) {
1667                 pr_debug("nf_ct_ras: expect Q.931 ");
1668                 nf_ct_dump_tuple(&exp->tuple);
1669         } else
1670                 ret = -1;
1671
1672         nf_ct_expect_put(exp);
1673
1674         /* Ignore rasAddress */
1675
1676         return ret;
1677 }
1678
1679 /****************************************************************************/
1680 static int process_irr(struct sk_buff *skb, struct nf_conn *ct,
1681                        enum ip_conntrack_info ctinfo,
1682                        unsigned int protoff,
1683                        unsigned char **data, InfoRequestResponse *irr)
1684 {
1685         int ret;
1686         typeof(set_ras_addr_hook) set_ras_addr;
1687         typeof(set_sig_addr_hook) set_sig_addr;
1688
1689         pr_debug("nf_ct_ras: IRR\n");
1690
1691         set_ras_addr = rcu_dereference(set_ras_addr_hook);
1692         if (set_ras_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1693             ct->status & IPS_NAT_MASK) {
1694                 ret = set_ras_addr(skb, ct, ctinfo, protoff, data,
1695                                    &irr->rasAddress, 1);
1696                 if (ret < 0)
1697                         return -1;
1698         }
1699
1700         set_sig_addr = rcu_dereference(set_sig_addr_hook);
1701         if (set_sig_addr && nf_ct_l3num(ct) == NFPROTO_IPV4 &&
1702             ct->status & IPS_NAT_MASK) {
1703                 ret = set_sig_addr(skb, ct, ctinfo, protoff, data,
1704                                         irr->callSignalAddress.item,
1705                                         irr->callSignalAddress.count);
1706                 if (ret < 0)
1707                         return -1;
1708         }
1709
1710         return 0;
1711 }
1712
1713 /****************************************************************************/
1714 static int process_ras(struct sk_buff *skb, struct nf_conn *ct,
1715                        enum ip_conntrack_info ctinfo,
1716                        unsigned int protoff,
1717                        unsigned char **data, RasMessage *ras)
1718 {
1719         switch (ras->choice) {
1720         case eRasMessage_gatekeeperRequest:
1721                 return process_grq(skb, ct, ctinfo, protoff, data,
1722                                    &ras->gatekeeperRequest);
1723         case eRasMessage_gatekeeperConfirm:
1724                 return process_gcf(skb, ct, ctinfo, protoff, data,
1725                                    &ras->gatekeeperConfirm);
1726         case eRasMessage_registrationRequest:
1727                 return process_rrq(skb, ct, ctinfo, protoff, data,
1728                                    &ras->registrationRequest);
1729         case eRasMessage_registrationConfirm:
1730                 return process_rcf(skb, ct, ctinfo, protoff, data,
1731                                    &ras->registrationConfirm);
1732         case eRasMessage_unregistrationRequest:
1733                 return process_urq(skb, ct, ctinfo, protoff, data,
1734                                    &ras->unregistrationRequest);
1735         case eRasMessage_admissionRequest:
1736                 return process_arq(skb, ct, ctinfo, protoff, data,
1737                                    &ras->admissionRequest);
1738         case eRasMessage_admissionConfirm:
1739                 return process_acf(skb, ct, ctinfo, protoff, data,
1740                                    &ras->admissionConfirm);
1741         case eRasMessage_locationRequest:
1742                 return process_lrq(skb, ct, ctinfo, protoff, data,
1743                                    &ras->locationRequest);
1744         case eRasMessage_locationConfirm:
1745                 return process_lcf(skb, ct, ctinfo, protoff, data,
1746                                    &ras->locationConfirm);
1747         case eRasMessage_infoRequestResponse:
1748                 return process_irr(skb, ct, ctinfo, protoff, data,
1749                                    &ras->infoRequestResponse);
1750         default:
1751                 pr_debug("nf_ct_ras: RAS message %d\n", ras->choice);
1752                 break;
1753         }
1754
1755         return 0;
1756 }
1757
1758 /****************************************************************************/
1759 static int ras_help(struct sk_buff *skb, unsigned int protoff,
1760                     struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1761 {
1762         static RasMessage ras;
1763         unsigned char *data;
1764         int datalen = 0;
1765         int ret;
1766
1767         pr_debug("nf_ct_ras: skblen = %u\n", skb->len);
1768
1769         spin_lock_bh(&nf_h323_lock);
1770
1771         /* Get UDP data */
1772         data = get_udp_data(skb, protoff, &datalen);
1773         if (data == NULL)
1774                 goto accept;
1775         pr_debug("nf_ct_ras: RAS message len=%d ", datalen);
1776         nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple);
1777
1778         /* Decode RAS message */
1779         ret = DecodeRasMessage(data, datalen, &ras);
1780         if (ret < 0) {
1781                 pr_debug("nf_ct_ras: decoding error: %s\n",
1782                          ret == H323_ERROR_BOUND ?
1783                          "out of bound" : "out of range");
1784                 goto accept;
1785         }
1786
1787         /* Process RAS message */
1788         if (process_ras(skb, ct, ctinfo, protoff, &data, &ras) < 0)
1789                 goto drop;
1790
1791       accept:
1792         spin_unlock_bh(&nf_h323_lock);
1793         return NF_ACCEPT;
1794
1795       drop:
1796         spin_unlock_bh(&nf_h323_lock);
1797         net_info_ratelimited("nf_ct_ras: packet dropped\n");
1798         return NF_DROP;
1799 }
1800
1801 /****************************************************************************/
1802 static const struct nf_conntrack_expect_policy ras_exp_policy = {
1803         .max_expected           = 32,
1804         .timeout                = 240,
1805 };
1806
1807 static struct nf_conntrack_helper nf_conntrack_helper_ras[] __read_mostly = {
1808         {
1809                 .name                   = "RAS",
1810                 .me                     = THIS_MODULE,
1811                 .data_len               = sizeof(struct nf_ct_h323_master),
1812                 .tuple.src.l3num        = AF_INET,
1813                 .tuple.src.u.udp.port   = cpu_to_be16(RAS_PORT),
1814                 .tuple.dst.protonum     = IPPROTO_UDP,
1815                 .help                   = ras_help,
1816                 .expect_policy          = &ras_exp_policy,
1817         },
1818         {
1819                 .name                   = "RAS",
1820                 .me                     = THIS_MODULE,
1821                 .data_len               = sizeof(struct nf_ct_h323_master),
1822                 .tuple.src.l3num        = AF_INET6,
1823                 .tuple.src.u.udp.port   = cpu_to_be16(RAS_PORT),
1824                 .tuple.dst.protonum     = IPPROTO_UDP,
1825                 .help                   = ras_help,
1826                 .expect_policy          = &ras_exp_policy,
1827         },
1828 };
1829
1830 /****************************************************************************/
1831 static void __exit nf_conntrack_h323_fini(void)
1832 {
1833         nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[1]);
1834         nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1835         nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1836         nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1837         nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1838         kfree(h323_buffer);
1839         pr_debug("nf_ct_h323: fini\n");
1840 }
1841
1842 /****************************************************************************/
1843 static int __init nf_conntrack_h323_init(void)
1844 {
1845         int ret;
1846
1847         h323_buffer = kmalloc(65536, GFP_KERNEL);
1848         if (!h323_buffer)
1849                 return -ENOMEM;
1850         ret = nf_conntrack_helper_register(&nf_conntrack_helper_h245);
1851         if (ret < 0)
1852                 goto err1;
1853         ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[0]);
1854         if (ret < 0)
1855                 goto err2;
1856         ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[1]);
1857         if (ret < 0)
1858                 goto err3;
1859         ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[0]);
1860         if (ret < 0)
1861                 goto err4;
1862         ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[1]);
1863         if (ret < 0)
1864                 goto err5;
1865         pr_debug("nf_ct_h323: init success\n");
1866         return 0;
1867
1868 err5:
1869         nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]);
1870 err4:
1871         nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]);
1872 err3:
1873         nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]);
1874 err2:
1875         nf_conntrack_helper_unregister(&nf_conntrack_helper_h245);
1876 err1:
1877         kfree(h323_buffer);
1878         return ret;
1879 }
1880
1881 /****************************************************************************/
1882 module_init(nf_conntrack_h323_init);
1883 module_exit(nf_conntrack_h323_fini);
1884
1885 EXPORT_SYMBOL_GPL(get_h225_addr);
1886 EXPORT_SYMBOL_GPL(set_h245_addr_hook);
1887 EXPORT_SYMBOL_GPL(set_h225_addr_hook);
1888 EXPORT_SYMBOL_GPL(set_sig_addr_hook);
1889 EXPORT_SYMBOL_GPL(set_ras_addr_hook);
1890 EXPORT_SYMBOL_GPL(nat_rtp_rtcp_hook);
1891 EXPORT_SYMBOL_GPL(nat_t120_hook);
1892 EXPORT_SYMBOL_GPL(nat_h245_hook);
1893 EXPORT_SYMBOL_GPL(nat_callforwarding_hook);
1894 EXPORT_SYMBOL_GPL(nat_q931_hook);
1895
1896 MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>");
1897 MODULE_DESCRIPTION("H.323 connection tracking helper");
1898 MODULE_LICENSE("GPL");
1899 MODULE_ALIAS("ip_conntrack_h323");
1900 MODULE_ALIAS_NFCT_HELPER("RAS");
1901 MODULE_ALIAS_NFCT_HELPER("Q.931");
1902 MODULE_ALIAS_NFCT_HELPER("H.245");