nf_conntrack_h323_main.c 55 KB

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