~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/nf_conntrack_h323_main.c

Version: ~ [ linux-5.16 ] ~ [ linux-5.15.13 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.90 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.170 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.224 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.261 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.296 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.298 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp