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