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> | |
5a0e3ad6 | 20 | #include <linux/slab.h> |
f587de0e PM |
21 | #include <linux/udp.h> |
22 | #include <linux/tcp.h> | |
23 | #include <linux/skbuff.h> | |
24 | #include <net/route.h> | |
25 | #include <net/ip6_route.h> | |
26 | ||
27 | #include <net/netfilter/nf_conntrack.h> | |
28 | #include <net/netfilter/nf_conntrack_core.h> | |
29 | #include <net/netfilter/nf_conntrack_tuple.h> | |
30 | #include <net/netfilter/nf_conntrack_expect.h> | |
31 | #include <net/netfilter/nf_conntrack_ecache.h> | |
32 | #include <net/netfilter/nf_conntrack_helper.h> | |
5d0aa2cc | 33 | #include <net/netfilter/nf_conntrack_zones.h> |
f587de0e PM |
34 | #include <linux/netfilter/nf_conntrack_h323.h> |
35 | ||
f587de0e PM |
36 | /* Parameters */ |
37 | static unsigned int default_rrq_ttl __read_mostly = 300; | |
38 | module_param(default_rrq_ttl, uint, 0600); | |
39 | MODULE_PARM_DESC(default_rrq_ttl, "use this TTL if it's missing in RRQ"); | |
40 | ||
41 | static int gkrouted_only __read_mostly = 1; | |
42 | module_param(gkrouted_only, int, 0600); | |
43 | MODULE_PARM_DESC(gkrouted_only, "only accept calls from gatekeeper"); | |
44 | ||
45 | static int callforward_filter __read_mostly = 1; | |
46 | module_param(callforward_filter, bool, 0600); | |
47 | MODULE_PARM_DESC(callforward_filter, "only create call forwarding expectations " | |
601e68e1 | 48 | "if both endpoints are on different sides " |
f587de0e PM |
49 | "(determined by routing information)"); |
50 | ||
51 | /* Hooks for NAT */ | |
3db05fea | 52 | int (*set_h245_addr_hook) (struct sk_buff *skb, |
f587de0e PM |
53 | unsigned char **data, int dataoff, |
54 | H245_TransportAddress *taddr, | |
643a2c15 | 55 | union nf_inet_addr *addr, __be16 port) |
f587de0e | 56 | __read_mostly; |
3db05fea | 57 | int (*set_h225_addr_hook) (struct sk_buff *skb, |
f587de0e PM |
58 | unsigned char **data, int dataoff, |
59 | TransportAddress *taddr, | |
643a2c15 | 60 | union nf_inet_addr *addr, __be16 port) |
f587de0e | 61 | __read_mostly; |
3db05fea | 62 | int (*set_sig_addr_hook) (struct sk_buff *skb, |
f587de0e PM |
63 | struct nf_conn *ct, |
64 | enum ip_conntrack_info ctinfo, | |
65 | unsigned char **data, | |
66 | TransportAddress *taddr, int count) __read_mostly; | |
3db05fea | 67 | int (*set_ras_addr_hook) (struct sk_buff *skb, |
f587de0e PM |
68 | struct nf_conn *ct, |
69 | enum ip_conntrack_info ctinfo, | |
70 | unsigned char **data, | |
71 | TransportAddress *taddr, int count) __read_mostly; | |
3db05fea | 72 | int (*nat_rtp_rtcp_hook) (struct sk_buff *skb, |
f587de0e PM |
73 | struct nf_conn *ct, |
74 | enum ip_conntrack_info ctinfo, | |
75 | unsigned char **data, int dataoff, | |
76 | H245_TransportAddress *taddr, | |
77 | __be16 port, __be16 rtp_port, | |
78 | struct nf_conntrack_expect *rtp_exp, | |
79 | struct nf_conntrack_expect *rtcp_exp) __read_mostly; | |
3db05fea | 80 | int (*nat_t120_hook) (struct sk_buff *skb, |
f587de0e PM |
81 | struct nf_conn *ct, |
82 | enum ip_conntrack_info ctinfo, | |
83 | unsigned char **data, int dataoff, | |
84 | H245_TransportAddress *taddr, __be16 port, | |
85 | struct nf_conntrack_expect *exp) __read_mostly; | |
3db05fea | 86 | int (*nat_h245_hook) (struct sk_buff *skb, |
f587de0e PM |
87 | struct nf_conn *ct, |
88 | enum ip_conntrack_info ctinfo, | |
89 | unsigned char **data, int dataoff, | |
90 | TransportAddress *taddr, __be16 port, | |
91 | struct nf_conntrack_expect *exp) __read_mostly; | |
3db05fea | 92 | int (*nat_callforwarding_hook) (struct sk_buff *skb, |
f587de0e PM |
93 | struct nf_conn *ct, |
94 | enum ip_conntrack_info ctinfo, | |
95 | unsigned char **data, int dataoff, | |
96 | TransportAddress *taddr, __be16 port, | |
97 | struct nf_conntrack_expect *exp) __read_mostly; | |
3db05fea | 98 | int (*nat_q931_hook) (struct sk_buff *skb, |
f587de0e PM |
99 | struct nf_conn *ct, |
100 | enum ip_conntrack_info ctinfo, | |
101 | unsigned char **data, TransportAddress *taddr, int idx, | |
102 | __be16 port, struct nf_conntrack_expect *exp) | |
103 | __read_mostly; | |
104 | ||
105 | static DEFINE_SPINLOCK(nf_h323_lock); | |
106 | static char *h323_buffer; | |
107 | ||
108 | static struct nf_conntrack_helper nf_conntrack_helper_h245; | |
109 | static struct nf_conntrack_helper nf_conntrack_helper_q931[]; | |
110 | static struct nf_conntrack_helper nf_conntrack_helper_ras[]; | |
111 | ||
112 | /****************************************************************************/ | |
3db05fea | 113 | static int get_tpkt_data(struct sk_buff *skb, unsigned int protoff, |
f587de0e PM |
114 | struct nf_conn *ct, enum ip_conntrack_info ctinfo, |
115 | unsigned char **data, int *datalen, int *dataoff) | |
116 | { | |
117 | struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info; | |
118 | int dir = CTINFO2DIR(ctinfo); | |
905e3e8e JE |
119 | const struct tcphdr *th; |
120 | struct tcphdr _tcph; | |
f587de0e PM |
121 | int tcpdatalen; |
122 | int tcpdataoff; | |
123 | unsigned char *tpkt; | |
124 | int tpktlen; | |
125 | int tpktoff; | |
126 | ||
127 | /* Get TCP header */ | |
3db05fea | 128 | th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph); |
f587de0e PM |
129 | if (th == NULL) |
130 | return 0; | |
131 | ||
132 | /* Get TCP data offset */ | |
133 | tcpdataoff = protoff + th->doff * 4; | |
134 | ||
135 | /* Get TCP data length */ | |
3db05fea | 136 | tcpdatalen = skb->len - tcpdataoff; |
f587de0e PM |
137 | if (tcpdatalen <= 0) /* No TCP data */ |
138 | goto clear_out; | |
139 | ||
140 | if (*data == NULL) { /* first TPKT */ | |
141 | /* Get first TPKT pointer */ | |
3db05fea | 142 | tpkt = skb_header_pointer(skb, tcpdataoff, tcpdatalen, |
f587de0e PM |
143 | h323_buffer); |
144 | BUG_ON(tpkt == NULL); | |
145 | ||
146 | /* Validate TPKT identifier */ | |
147 | if (tcpdatalen < 4 || tpkt[0] != 0x03 || tpkt[1] != 0) { | |
148 | /* Netmeeting sends TPKT header and data separately */ | |
149 | if (info->tpkt_len[dir] > 0) { | |
0d53778e PM |
150 | pr_debug("nf_ct_h323: previous packet " |
151 | "indicated separate TPKT data of %hu " | |
152 | "bytes\n", info->tpkt_len[dir]); | |
f587de0e PM |
153 | if (info->tpkt_len[dir] <= tcpdatalen) { |
154 | /* Yes, there was a TPKT header | |
155 | * received */ | |
156 | *data = tpkt; | |
157 | *datalen = info->tpkt_len[dir]; | |
158 | *dataoff = 0; | |
159 | goto out; | |
160 | } | |
161 | ||
162 | /* Fragmented TPKT */ | |
0d53778e | 163 | pr_debug("nf_ct_h323: fragmented TPKT\n"); |
f587de0e PM |
164 | goto clear_out; |
165 | } | |
166 | ||
167 | /* It is not even a TPKT */ | |
168 | return 0; | |
169 | } | |
170 | tpktoff = 0; | |
171 | } else { /* Next TPKT */ | |
172 | tpktoff = *dataoff + *datalen; | |
173 | tcpdatalen -= tpktoff; | |
174 | if (tcpdatalen <= 4) /* No more TPKT */ | |
175 | goto clear_out; | |
176 | tpkt = *data + *datalen; | |
177 | ||
178 | /* Validate TPKT identifier */ | |
179 | if (tpkt[0] != 0x03 || tpkt[1] != 0) | |
180 | goto clear_out; | |
181 | } | |
182 | ||
183 | /* Validate TPKT length */ | |
184 | tpktlen = tpkt[2] * 256 + tpkt[3]; | |
185 | if (tpktlen < 4) | |
186 | goto clear_out; | |
187 | if (tpktlen > tcpdatalen) { | |
188 | if (tcpdatalen == 4) { /* Separate TPKT header */ | |
189 | /* Netmeeting sends TPKT header and data separately */ | |
0d53778e PM |
190 | pr_debug("nf_ct_h323: separate TPKT header indicates " |
191 | "there will be TPKT data of %hu bytes\n", | |
192 | tpktlen - 4); | |
f587de0e PM |
193 | info->tpkt_len[dir] = tpktlen - 4; |
194 | return 0; | |
195 | } | |
196 | ||
197 | if (net_ratelimit()) | |
198 | printk("nf_ct_h323: incomplete TPKT (fragmented?)\n"); | |
199 | goto clear_out; | |
200 | } | |
201 | ||
202 | /* This is the encapsulated data */ | |
203 | *data = tpkt + 4; | |
204 | *datalen = tpktlen - 4; | |
205 | *dataoff = tpktoff + 4; | |
206 | ||
207 | out: | |
208 | /* Clear TPKT length */ | |
209 | info->tpkt_len[dir] = 0; | |
210 | return 1; | |
211 | ||
212 | clear_out: | |
213 | info->tpkt_len[dir] = 0; | |
214 | return 0; | |
215 | } | |
216 | ||
217 | /****************************************************************************/ | |
905e3e8e | 218 | static int get_h245_addr(struct nf_conn *ct, const unsigned char *data, |
f587de0e | 219 | H245_TransportAddress *taddr, |
643a2c15 | 220 | union nf_inet_addr *addr, __be16 *port) |
f587de0e | 221 | { |
905e3e8e | 222 | const unsigned char *p; |
f587de0e PM |
223 | int len; |
224 | ||
225 | if (taddr->choice != eH245_TransportAddress_unicastAddress) | |
226 | return 0; | |
227 | ||
228 | switch (taddr->unicastAddress.choice) { | |
229 | case eUnicastAddress_iPAddress: | |
5e8fbe2a | 230 | if (nf_ct_l3num(ct) != AF_INET) |
f587de0e PM |
231 | return 0; |
232 | p = data + taddr->unicastAddress.iPAddress.network; | |
233 | len = 4; | |
234 | break; | |
235 | case eUnicastAddress_iP6Address: | |
5e8fbe2a | 236 | if (nf_ct_l3num(ct) != AF_INET6) |
f587de0e PM |
237 | return 0; |
238 | p = data + taddr->unicastAddress.iP6Address.network; | |
239 | len = 16; | |
240 | break; | |
241 | default: | |
242 | return 0; | |
243 | } | |
244 | ||
245 | memcpy(addr, p, len); | |
246 | memset((void *)addr + len, 0, sizeof(*addr) - len); | |
247 | memcpy(port, p + len, sizeof(__be16)); | |
248 | ||
249 | return 1; | |
250 | } | |
251 | ||
252 | /****************************************************************************/ | |
3db05fea | 253 | static int expect_rtp_rtcp(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
254 | enum ip_conntrack_info ctinfo, |
255 | unsigned char **data, int dataoff, | |
256 | H245_TransportAddress *taddr) | |
257 | { | |
258 | int dir = CTINFO2DIR(ctinfo); | |
259 | int ret = 0; | |
260 | __be16 port; | |
261 | __be16 rtp_port, rtcp_port; | |
643a2c15 | 262 | union nf_inet_addr addr; |
f587de0e PM |
263 | struct nf_conntrack_expect *rtp_exp; |
264 | struct nf_conntrack_expect *rtcp_exp; | |
265 | typeof(nat_rtp_rtcp_hook) nat_rtp_rtcp; | |
266 | ||
267 | /* Read RTP or RTCP address */ | |
268 | if (!get_h245_addr(ct, *data, taddr, &addr, &port) || | |
269 | memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) || | |
270 | port == 0) | |
271 | return 0; | |
272 | ||
273 | /* RTP port is even */ | |
274 | port &= htons(~1); | |
275 | rtp_port = port; | |
276 | rtcp_port = htons(ntohs(port) + 1); | |
277 | ||
278 | /* Create expect for RTP */ | |
6823645d | 279 | if ((rtp_exp = nf_ct_expect_alloc(ct)) == NULL) |
f587de0e | 280 | return -1; |
5e8fbe2a | 281 | nf_ct_expect_init(rtp_exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct), |
6823645d PM |
282 | &ct->tuplehash[!dir].tuple.src.u3, |
283 | &ct->tuplehash[!dir].tuple.dst.u3, | |
284 | IPPROTO_UDP, NULL, &rtp_port); | |
f587de0e PM |
285 | |
286 | /* Create expect for RTCP */ | |
6823645d PM |
287 | if ((rtcp_exp = nf_ct_expect_alloc(ct)) == NULL) { |
288 | nf_ct_expect_put(rtp_exp); | |
f587de0e PM |
289 | return -1; |
290 | } | |
5e8fbe2a | 291 | nf_ct_expect_init(rtcp_exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct), |
6823645d PM |
292 | &ct->tuplehash[!dir].tuple.src.u3, |
293 | &ct->tuplehash[!dir].tuple.dst.u3, | |
294 | IPPROTO_UDP, NULL, &rtcp_port); | |
f587de0e PM |
295 | |
296 | if (memcmp(&ct->tuplehash[dir].tuple.src.u3, | |
601e68e1 | 297 | &ct->tuplehash[!dir].tuple.dst.u3, |
f587de0e PM |
298 | sizeof(ct->tuplehash[dir].tuple.src.u3)) && |
299 | (nat_rtp_rtcp = rcu_dereference(nat_rtp_rtcp_hook)) && | |
300 | ct->status & IPS_NAT_MASK) { | |
301 | /* NAT needed */ | |
3db05fea | 302 | ret = nat_rtp_rtcp(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
303 | taddr, port, rtp_port, rtp_exp, rtcp_exp); |
304 | } else { /* Conntrack only */ | |
6823645d PM |
305 | if (nf_ct_expect_related(rtp_exp) == 0) { |
306 | if (nf_ct_expect_related(rtcp_exp) == 0) { | |
0d53778e | 307 | pr_debug("nf_ct_h323: expect RTP "); |
3c9fba65 | 308 | nf_ct_dump_tuple(&rtp_exp->tuple); |
0d53778e | 309 | pr_debug("nf_ct_h323: expect RTCP "); |
3c9fba65 | 310 | nf_ct_dump_tuple(&rtcp_exp->tuple); |
f587de0e | 311 | } else { |
6823645d | 312 | nf_ct_unexpect_related(rtp_exp); |
f587de0e PM |
313 | ret = -1; |
314 | } | |
315 | } else | |
316 | ret = -1; | |
317 | } | |
318 | ||
6823645d PM |
319 | nf_ct_expect_put(rtp_exp); |
320 | nf_ct_expect_put(rtcp_exp); | |
f587de0e PM |
321 | |
322 | return ret; | |
323 | } | |
324 | ||
325 | /****************************************************************************/ | |
3db05fea | 326 | static int expect_t120(struct sk_buff *skb, |
f587de0e PM |
327 | struct nf_conn *ct, |
328 | enum ip_conntrack_info ctinfo, | |
329 | unsigned char **data, int dataoff, | |
330 | H245_TransportAddress *taddr) | |
331 | { | |
332 | int dir = CTINFO2DIR(ctinfo); | |
333 | int ret = 0; | |
334 | __be16 port; | |
643a2c15 | 335 | union nf_inet_addr addr; |
f587de0e PM |
336 | struct nf_conntrack_expect *exp; |
337 | typeof(nat_t120_hook) nat_t120; | |
338 | ||
339 | /* Read T.120 address */ | |
340 | if (!get_h245_addr(ct, *data, taddr, &addr, &port) || | |
341 | memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) || | |
342 | port == 0) | |
343 | return 0; | |
344 | ||
345 | /* Create expect for T.120 connections */ | |
6823645d | 346 | if ((exp = nf_ct_expect_alloc(ct)) == NULL) |
f587de0e | 347 | return -1; |
5e8fbe2a | 348 | nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct), |
6823645d PM |
349 | &ct->tuplehash[!dir].tuple.src.u3, |
350 | &ct->tuplehash[!dir].tuple.dst.u3, | |
351 | IPPROTO_TCP, NULL, &port); | |
f587de0e PM |
352 | exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple channels */ |
353 | ||
354 | if (memcmp(&ct->tuplehash[dir].tuple.src.u3, | |
355 | &ct->tuplehash[!dir].tuple.dst.u3, | |
356 | sizeof(ct->tuplehash[dir].tuple.src.u3)) && | |
357 | (nat_t120 = rcu_dereference(nat_t120_hook)) && | |
358 | ct->status & IPS_NAT_MASK) { | |
359 | /* NAT needed */ | |
3db05fea | 360 | ret = nat_t120(skb, ct, ctinfo, data, dataoff, taddr, |
f587de0e PM |
361 | port, exp); |
362 | } else { /* Conntrack only */ | |
6823645d | 363 | if (nf_ct_expect_related(exp) == 0) { |
0d53778e | 364 | pr_debug("nf_ct_h323: expect T.120 "); |
3c9fba65 | 365 | nf_ct_dump_tuple(&exp->tuple); |
f587de0e PM |
366 | } else |
367 | ret = -1; | |
368 | } | |
369 | ||
6823645d | 370 | nf_ct_expect_put(exp); |
f587de0e PM |
371 | |
372 | return ret; | |
373 | } | |
374 | ||
375 | /****************************************************************************/ | |
3db05fea | 376 | static int process_h245_channel(struct sk_buff *skb, |
f587de0e PM |
377 | struct nf_conn *ct, |
378 | enum ip_conntrack_info ctinfo, | |
379 | unsigned char **data, int dataoff, | |
380 | H2250LogicalChannelParameters *channel) | |
381 | { | |
382 | int ret; | |
383 | ||
384 | if (channel->options & eH2250LogicalChannelParameters_mediaChannel) { | |
385 | /* RTP */ | |
3db05fea | 386 | ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
387 | &channel->mediaChannel); |
388 | if (ret < 0) | |
389 | return -1; | |
390 | } | |
391 | ||
392 | if (channel-> | |
393 | options & eH2250LogicalChannelParameters_mediaControlChannel) { | |
394 | /* RTCP */ | |
3db05fea | 395 | ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
396 | &channel->mediaControlChannel); |
397 | if (ret < 0) | |
398 | return -1; | |
399 | } | |
400 | ||
401 | return 0; | |
402 | } | |
403 | ||
404 | /****************************************************************************/ | |
3db05fea | 405 | static int process_olc(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
406 | enum ip_conntrack_info ctinfo, |
407 | unsigned char **data, int dataoff, | |
408 | OpenLogicalChannel *olc) | |
409 | { | |
410 | int ret; | |
411 | ||
0d53778e | 412 | pr_debug("nf_ct_h323: OpenLogicalChannel\n"); |
f587de0e PM |
413 | |
414 | if (olc->forwardLogicalChannelParameters.multiplexParameters.choice == | |
415 | eOpenLogicalChannel_forwardLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters) | |
416 | { | |
3db05fea | 417 | ret = process_h245_channel(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
418 | &olc-> |
419 | forwardLogicalChannelParameters. | |
420 | multiplexParameters. | |
421 | h2250LogicalChannelParameters); | |
422 | if (ret < 0) | |
423 | return -1; | |
424 | } | |
425 | ||
426 | if ((olc->options & | |
427 | eOpenLogicalChannel_reverseLogicalChannelParameters) && | |
428 | (olc->reverseLogicalChannelParameters.options & | |
429 | eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters) | |
430 | && (olc->reverseLogicalChannelParameters.multiplexParameters. | |
431 | choice == | |
432 | eOpenLogicalChannel_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters)) | |
433 | { | |
434 | ret = | |
3db05fea | 435 | process_h245_channel(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
436 | &olc-> |
437 | reverseLogicalChannelParameters. | |
438 | multiplexParameters. | |
439 | h2250LogicalChannelParameters); | |
440 | if (ret < 0) | |
441 | return -1; | |
442 | } | |
443 | ||
444 | if ((olc->options & eOpenLogicalChannel_separateStack) && | |
445 | olc->forwardLogicalChannelParameters.dataType.choice == | |
446 | eDataType_data && | |
447 | olc->forwardLogicalChannelParameters.dataType.data.application. | |
448 | choice == eDataApplicationCapability_application_t120 && | |
449 | olc->forwardLogicalChannelParameters.dataType.data.application. | |
450 | t120.choice == eDataProtocolCapability_separateLANStack && | |
451 | olc->separateStack.networkAddress.choice == | |
452 | eNetworkAccessParameters_networkAddress_localAreaAddress) { | |
3db05fea | 453 | ret = expect_t120(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
454 | &olc->separateStack.networkAddress. |
455 | localAreaAddress); | |
456 | if (ret < 0) | |
457 | return -1; | |
458 | } | |
459 | ||
460 | return 0; | |
461 | } | |
462 | ||
463 | /****************************************************************************/ | |
3db05fea | 464 | static int process_olca(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
465 | enum ip_conntrack_info ctinfo, |
466 | unsigned char **data, int dataoff, | |
467 | OpenLogicalChannelAck *olca) | |
468 | { | |
469 | H2250LogicalChannelAckParameters *ack; | |
470 | int ret; | |
471 | ||
0d53778e | 472 | pr_debug("nf_ct_h323: OpenLogicalChannelAck\n"); |
f587de0e PM |
473 | |
474 | if ((olca->options & | |
475 | eOpenLogicalChannelAck_reverseLogicalChannelParameters) && | |
476 | (olca->reverseLogicalChannelParameters.options & | |
477 | eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters) | |
478 | && (olca->reverseLogicalChannelParameters.multiplexParameters. | |
479 | choice == | |
480 | eOpenLogicalChannelAck_reverseLogicalChannelParameters_multiplexParameters_h2250LogicalChannelParameters)) | |
481 | { | |
3db05fea | 482 | ret = process_h245_channel(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
483 | &olca-> |
484 | reverseLogicalChannelParameters. | |
485 | multiplexParameters. | |
486 | h2250LogicalChannelParameters); | |
487 | if (ret < 0) | |
488 | return -1; | |
489 | } | |
490 | ||
491 | if ((olca->options & | |
492 | eOpenLogicalChannelAck_forwardMultiplexAckParameters) && | |
493 | (olca->forwardMultiplexAckParameters.choice == | |
494 | eOpenLogicalChannelAck_forwardMultiplexAckParameters_h2250LogicalChannelAckParameters)) | |
495 | { | |
496 | ack = &olca->forwardMultiplexAckParameters. | |
497 | h2250LogicalChannelAckParameters; | |
498 | if (ack->options & | |
499 | eH2250LogicalChannelAckParameters_mediaChannel) { | |
500 | /* RTP */ | |
3db05fea | 501 | ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
502 | &ack->mediaChannel); |
503 | if (ret < 0) | |
504 | return -1; | |
505 | } | |
506 | ||
507 | if (ack->options & | |
508 | eH2250LogicalChannelAckParameters_mediaControlChannel) { | |
509 | /* RTCP */ | |
3db05fea | 510 | ret = expect_rtp_rtcp(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
511 | &ack->mediaControlChannel); |
512 | if (ret < 0) | |
513 | return -1; | |
514 | } | |
515 | } | |
516 | ||
f8f1c08e JMZ |
517 | if ((olca->options & eOpenLogicalChannelAck_separateStack) && |
518 | olca->separateStack.networkAddress.choice == | |
519 | eNetworkAccessParameters_networkAddress_localAreaAddress) { | |
3db05fea | 520 | ret = expect_t120(skb, ct, ctinfo, data, dataoff, |
f8f1c08e JMZ |
521 | &olca->separateStack.networkAddress. |
522 | localAreaAddress); | |
523 | if (ret < 0) | |
524 | return -1; | |
525 | } | |
526 | ||
f587de0e PM |
527 | return 0; |
528 | } | |
529 | ||
530 | /****************************************************************************/ | |
3db05fea | 531 | static int process_h245(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
532 | enum ip_conntrack_info ctinfo, |
533 | unsigned char **data, int dataoff, | |
534 | MultimediaSystemControlMessage *mscm) | |
535 | { | |
536 | switch (mscm->choice) { | |
537 | case eMultimediaSystemControlMessage_request: | |
538 | if (mscm->request.choice == | |
539 | eRequestMessage_openLogicalChannel) { | |
3db05fea | 540 | return process_olc(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
541 | &mscm->request.openLogicalChannel); |
542 | } | |
0d53778e PM |
543 | pr_debug("nf_ct_h323: H.245 Request %d\n", |
544 | mscm->request.choice); | |
f587de0e PM |
545 | break; |
546 | case eMultimediaSystemControlMessage_response: | |
547 | if (mscm->response.choice == | |
548 | eResponseMessage_openLogicalChannelAck) { | |
3db05fea | 549 | return process_olca(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
550 | &mscm->response. |
551 | openLogicalChannelAck); | |
552 | } | |
0d53778e PM |
553 | pr_debug("nf_ct_h323: H.245 Response %d\n", |
554 | mscm->response.choice); | |
f587de0e PM |
555 | break; |
556 | default: | |
0d53778e | 557 | pr_debug("nf_ct_h323: H.245 signal %d\n", mscm->choice); |
f587de0e PM |
558 | break; |
559 | } | |
560 | ||
561 | return 0; | |
562 | } | |
563 | ||
564 | /****************************************************************************/ | |
3db05fea | 565 | static int h245_help(struct sk_buff *skb, unsigned int protoff, |
f587de0e PM |
566 | struct nf_conn *ct, enum ip_conntrack_info ctinfo) |
567 | { | |
568 | static MultimediaSystemControlMessage mscm; | |
569 | unsigned char *data = NULL; | |
570 | int datalen; | |
571 | int dataoff; | |
572 | int ret; | |
573 | ||
574 | /* Until there's been traffic both ways, don't look in packets. */ | |
575 | if (ctinfo != IP_CT_ESTABLISHED && | |
576 | ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) { | |
577 | return NF_ACCEPT; | |
578 | } | |
3db05fea | 579 | pr_debug("nf_ct_h245: skblen = %u\n", skb->len); |
f587de0e PM |
580 | |
581 | spin_lock_bh(&nf_h323_lock); | |
582 | ||
583 | /* Process each TPKT */ | |
3db05fea | 584 | while (get_tpkt_data(skb, protoff, ct, ctinfo, |
f587de0e | 585 | &data, &datalen, &dataoff)) { |
0d53778e | 586 | pr_debug("nf_ct_h245: TPKT len=%d ", datalen); |
3c9fba65 | 587 | nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple); |
f587de0e PM |
588 | |
589 | /* Decode H.245 signal */ | |
590 | ret = DecodeMultimediaSystemControlMessage(data, datalen, | |
591 | &mscm); | |
592 | if (ret < 0) { | |
0d53778e PM |
593 | pr_debug("nf_ct_h245: decoding error: %s\n", |
594 | ret == H323_ERROR_BOUND ? | |
595 | "out of bound" : "out of range"); | |
f587de0e PM |
596 | /* We don't drop when decoding error */ |
597 | break; | |
598 | } | |
599 | ||
600 | /* Process H.245 signal */ | |
3db05fea | 601 | if (process_h245(skb, ct, ctinfo, &data, dataoff, &mscm) < 0) |
f587de0e PM |
602 | goto drop; |
603 | } | |
604 | ||
605 | spin_unlock_bh(&nf_h323_lock); | |
606 | return NF_ACCEPT; | |
607 | ||
608 | drop: | |
609 | spin_unlock_bh(&nf_h323_lock); | |
610 | if (net_ratelimit()) | |
611 | printk("nf_ct_h245: packet dropped\n"); | |
612 | return NF_DROP; | |
613 | } | |
614 | ||
615 | /****************************************************************************/ | |
6002f266 PM |
616 | static const struct nf_conntrack_expect_policy h245_exp_policy = { |
617 | .max_expected = H323_RTP_CHANNEL_MAX * 4 + 2 /* T.120 */, | |
618 | .timeout = 240, | |
619 | }; | |
620 | ||
f587de0e PM |
621 | static struct nf_conntrack_helper nf_conntrack_helper_h245 __read_mostly = { |
622 | .name = "H.245", | |
623 | .me = THIS_MODULE, | |
a56b8f81 | 624 | .tuple.src.l3num = AF_UNSPEC, |
f587de0e | 625 | .tuple.dst.protonum = IPPROTO_UDP, |
6002f266 PM |
626 | .help = h245_help, |
627 | .expect_policy = &h245_exp_policy, | |
f587de0e PM |
628 | }; |
629 | ||
630 | /****************************************************************************/ | |
631 | int get_h225_addr(struct nf_conn *ct, unsigned char *data, | |
632 | TransportAddress *taddr, | |
643a2c15 | 633 | union nf_inet_addr *addr, __be16 *port) |
f587de0e | 634 | { |
905e3e8e | 635 | const unsigned char *p; |
f587de0e PM |
636 | int len; |
637 | ||
638 | switch (taddr->choice) { | |
639 | case eTransportAddress_ipAddress: | |
5e8fbe2a | 640 | if (nf_ct_l3num(ct) != AF_INET) |
f587de0e PM |
641 | return 0; |
642 | p = data + taddr->ipAddress.ip; | |
643 | len = 4; | |
644 | break; | |
645 | case eTransportAddress_ip6Address: | |
5e8fbe2a | 646 | if (nf_ct_l3num(ct) != AF_INET6) |
f587de0e | 647 | return 0; |
bb807245 | 648 | p = data + taddr->ip6Address.ip; |
f587de0e PM |
649 | len = 16; |
650 | break; | |
651 | default: | |
652 | return 0; | |
653 | } | |
654 | ||
655 | memcpy(addr, p, len); | |
656 | memset((void *)addr + len, 0, sizeof(*addr) - len); | |
657 | memcpy(port, p + len, sizeof(__be16)); | |
658 | ||
659 | return 1; | |
660 | } | |
661 | ||
662 | /****************************************************************************/ | |
3db05fea | 663 | static int expect_h245(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
664 | enum ip_conntrack_info ctinfo, |
665 | unsigned char **data, int dataoff, | |
666 | TransportAddress *taddr) | |
667 | { | |
668 | int dir = CTINFO2DIR(ctinfo); | |
669 | int ret = 0; | |
670 | __be16 port; | |
643a2c15 | 671 | union nf_inet_addr addr; |
f587de0e PM |
672 | struct nf_conntrack_expect *exp; |
673 | typeof(nat_h245_hook) nat_h245; | |
674 | ||
675 | /* Read h245Address */ | |
676 | if (!get_h225_addr(ct, *data, taddr, &addr, &port) || | |
677 | memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) || | |
678 | port == 0) | |
679 | return 0; | |
680 | ||
681 | /* Create expect for h245 connection */ | |
6823645d | 682 | if ((exp = nf_ct_expect_alloc(ct)) == NULL) |
f587de0e | 683 | return -1; |
5e8fbe2a | 684 | nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct), |
6823645d PM |
685 | &ct->tuplehash[!dir].tuple.src.u3, |
686 | &ct->tuplehash[!dir].tuple.dst.u3, | |
687 | IPPROTO_TCP, NULL, &port); | |
f587de0e PM |
688 | exp->helper = &nf_conntrack_helper_h245; |
689 | ||
690 | if (memcmp(&ct->tuplehash[dir].tuple.src.u3, | |
691 | &ct->tuplehash[!dir].tuple.dst.u3, | |
692 | sizeof(ct->tuplehash[dir].tuple.src.u3)) && | |
693 | (nat_h245 = rcu_dereference(nat_h245_hook)) && | |
694 | ct->status & IPS_NAT_MASK) { | |
695 | /* NAT needed */ | |
3db05fea | 696 | ret = nat_h245(skb, ct, ctinfo, data, dataoff, taddr, |
f587de0e PM |
697 | port, exp); |
698 | } else { /* Conntrack only */ | |
6823645d | 699 | if (nf_ct_expect_related(exp) == 0) { |
0d53778e | 700 | pr_debug("nf_ct_q931: expect H.245 "); |
3c9fba65 | 701 | nf_ct_dump_tuple(&exp->tuple); |
f587de0e PM |
702 | } else |
703 | ret = -1; | |
704 | } | |
705 | ||
6823645d | 706 | nf_ct_expect_put(exp); |
f587de0e PM |
707 | |
708 | return ret; | |
709 | } | |
710 | ||
711 | /* If the calling party is on the same side of the forward-to party, | |
712 | * we don't need to track the second call */ | |
905e3e8e | 713 | static int callforward_do_filter(const union nf_inet_addr *src, |
76108cea JE |
714 | const union nf_inet_addr *dst, |
715 | u_int8_t family) | |
f587de0e | 716 | { |
1e796fda | 717 | const struct nf_afinfo *afinfo; |
f587de0e PM |
718 | struct flowi fl1, fl2; |
719 | int ret = 0; | |
720 | ||
1841a4c7 PM |
721 | /* rcu_read_lock()ed by nf_hook_slow() */ |
722 | afinfo = nf_get_afinfo(family); | |
723 | if (!afinfo) | |
724 | return 0; | |
725 | ||
f587de0e PM |
726 | memset(&fl1, 0, sizeof(fl1)); |
727 | memset(&fl2, 0, sizeof(fl2)); | |
728 | ||
729 | switch (family) { | |
730 | case AF_INET: { | |
731 | struct rtable *rt1, *rt2; | |
732 | ||
733 | fl1.fl4_dst = src->ip; | |
734 | fl2.fl4_dst = dst->ip; | |
1841a4c7 PM |
735 | if (!afinfo->route((struct dst_entry **)&rt1, &fl1)) { |
736 | if (!afinfo->route((struct dst_entry **)&rt2, &fl2)) { | |
f587de0e PM |
737 | if (rt1->rt_gateway == rt2->rt_gateway && |
738 | rt1->u.dst.dev == rt2->u.dst.dev) | |
739 | ret = 1; | |
740 | dst_release(&rt2->u.dst); | |
741 | } | |
742 | dst_release(&rt1->u.dst); | |
743 | } | |
744 | break; | |
745 | } | |
1841a4c7 PM |
746 | #if defined(CONFIG_NF_CONNTRACK_IPV6) || \ |
747 | defined(CONFIG_NF_CONNTRACK_IPV6_MODULE) | |
f587de0e PM |
748 | case AF_INET6: { |
749 | struct rt6_info *rt1, *rt2; | |
750 | ||
751 | memcpy(&fl1.fl6_dst, src, sizeof(fl1.fl6_dst)); | |
752 | memcpy(&fl2.fl6_dst, dst, sizeof(fl2.fl6_dst)); | |
1841a4c7 PM |
753 | if (!afinfo->route((struct dst_entry **)&rt1, &fl1)) { |
754 | if (!afinfo->route((struct dst_entry **)&rt2, &fl2)) { | |
f587de0e | 755 | if (!memcmp(&rt1->rt6i_gateway, &rt2->rt6i_gateway, |
601e68e1 | 756 | sizeof(rt1->rt6i_gateway)) && |
f587de0e PM |
757 | rt1->u.dst.dev == rt2->u.dst.dev) |
758 | ret = 1; | |
759 | dst_release(&rt2->u.dst); | |
760 | } | |
761 | dst_release(&rt1->u.dst); | |
762 | } | |
763 | break; | |
764 | } | |
765 | #endif | |
766 | } | |
767 | return ret; | |
768 | ||
769 | } | |
770 | ||
771 | /****************************************************************************/ | |
3db05fea | 772 | static int expect_callforwarding(struct sk_buff *skb, |
f587de0e PM |
773 | struct nf_conn *ct, |
774 | enum ip_conntrack_info ctinfo, | |
775 | unsigned char **data, int dataoff, | |
776 | TransportAddress *taddr) | |
777 | { | |
778 | int dir = CTINFO2DIR(ctinfo); | |
779 | int ret = 0; | |
780 | __be16 port; | |
643a2c15 | 781 | union nf_inet_addr addr; |
f587de0e PM |
782 | struct nf_conntrack_expect *exp; |
783 | typeof(nat_callforwarding_hook) nat_callforwarding; | |
784 | ||
785 | /* Read alternativeAddress */ | |
786 | if (!get_h225_addr(ct, *data, taddr, &addr, &port) || port == 0) | |
787 | return 0; | |
788 | ||
789 | /* If the calling party is on the same side of the forward-to party, | |
790 | * we don't need to track the second call */ | |
791 | if (callforward_filter && | |
792 | callforward_do_filter(&addr, &ct->tuplehash[!dir].tuple.src.u3, | |
5e8fbe2a | 793 | nf_ct_l3num(ct))) { |
0d53778e | 794 | pr_debug("nf_ct_q931: Call Forwarding not tracked\n"); |
f587de0e PM |
795 | return 0; |
796 | } | |
797 | ||
798 | /* Create expect for the second call leg */ | |
6823645d | 799 | if ((exp = nf_ct_expect_alloc(ct)) == NULL) |
f587de0e | 800 | return -1; |
5e8fbe2a | 801 | nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct), |
6823645d PM |
802 | &ct->tuplehash[!dir].tuple.src.u3, &addr, |
803 | IPPROTO_TCP, NULL, &port); | |
f587de0e PM |
804 | exp->helper = nf_conntrack_helper_q931; |
805 | ||
806 | if (memcmp(&ct->tuplehash[dir].tuple.src.u3, | |
807 | &ct->tuplehash[!dir].tuple.dst.u3, | |
808 | sizeof(ct->tuplehash[dir].tuple.src.u3)) && | |
809 | (nat_callforwarding = rcu_dereference(nat_callforwarding_hook)) && | |
810 | ct->status & IPS_NAT_MASK) { | |
811 | /* Need NAT */ | |
3db05fea | 812 | ret = nat_callforwarding(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
813 | taddr, port, exp); |
814 | } else { /* Conntrack only */ | |
6823645d | 815 | if (nf_ct_expect_related(exp) == 0) { |
0d53778e | 816 | pr_debug("nf_ct_q931: expect Call Forwarding "); |
3c9fba65 | 817 | nf_ct_dump_tuple(&exp->tuple); |
f587de0e PM |
818 | } else |
819 | ret = -1; | |
820 | } | |
821 | ||
6823645d | 822 | nf_ct_expect_put(exp); |
f587de0e PM |
823 | |
824 | return ret; | |
825 | } | |
826 | ||
827 | /****************************************************************************/ | |
3db05fea | 828 | static int process_setup(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
829 | enum ip_conntrack_info ctinfo, |
830 | unsigned char **data, int dataoff, | |
831 | Setup_UUIE *setup) | |
832 | { | |
833 | int dir = CTINFO2DIR(ctinfo); | |
834 | int ret; | |
835 | int i; | |
836 | __be16 port; | |
643a2c15 | 837 | union nf_inet_addr addr; |
f587de0e PM |
838 | typeof(set_h225_addr_hook) set_h225_addr; |
839 | ||
0d53778e | 840 | pr_debug("nf_ct_q931: Setup\n"); |
f587de0e PM |
841 | |
842 | if (setup->options & eSetup_UUIE_h245Address) { | |
3db05fea | 843 | ret = expect_h245(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
844 | &setup->h245Address); |
845 | if (ret < 0) | |
846 | return -1; | |
847 | } | |
848 | ||
849 | set_h225_addr = rcu_dereference(set_h225_addr_hook); | |
850 | if ((setup->options & eSetup_UUIE_destCallSignalAddress) && | |
6aebb9b2 | 851 | (set_h225_addr) && ct->status & IPS_NAT_MASK && |
f587de0e | 852 | get_h225_addr(ct, *data, &setup->destCallSignalAddress, |
601e68e1 | 853 | &addr, &port) && |
f587de0e | 854 | memcmp(&addr, &ct->tuplehash[!dir].tuple.src.u3, sizeof(addr))) { |
5b095d98 | 855 | pr_debug("nf_ct_q931: set destCallSignalAddress %pI6:%hu->%pI6:%hu\n", |
38ff4fa4 | 856 | &addr, ntohs(port), &ct->tuplehash[!dir].tuple.src.u3, |
0d53778e | 857 | ntohs(ct->tuplehash[!dir].tuple.src.u.tcp.port)); |
3db05fea | 858 | ret = set_h225_addr(skb, data, dataoff, |
f587de0e PM |
859 | &setup->destCallSignalAddress, |
860 | &ct->tuplehash[!dir].tuple.src.u3, | |
861 | ct->tuplehash[!dir].tuple.src.u.tcp.port); | |
862 | if (ret < 0) | |
863 | return -1; | |
864 | } | |
865 | ||
866 | if ((setup->options & eSetup_UUIE_sourceCallSignalAddress) && | |
867 | (set_h225_addr) && ct->status & IPS_NAT_MASK && | |
868 | get_h225_addr(ct, *data, &setup->sourceCallSignalAddress, | |
601e68e1 | 869 | &addr, &port) && |
f587de0e | 870 | memcmp(&addr, &ct->tuplehash[!dir].tuple.dst.u3, sizeof(addr))) { |
5b095d98 | 871 | pr_debug("nf_ct_q931: set sourceCallSignalAddress %pI6:%hu->%pI6:%hu\n", |
38ff4fa4 | 872 | &addr, ntohs(port), &ct->tuplehash[!dir].tuple.dst.u3, |
0d53778e | 873 | ntohs(ct->tuplehash[!dir].tuple.dst.u.tcp.port)); |
3db05fea | 874 | ret = set_h225_addr(skb, data, dataoff, |
f587de0e PM |
875 | &setup->sourceCallSignalAddress, |
876 | &ct->tuplehash[!dir].tuple.dst.u3, | |
877 | ct->tuplehash[!dir].tuple.dst.u.tcp.port); | |
878 | if (ret < 0) | |
879 | return -1; | |
880 | } | |
881 | ||
882 | if (setup->options & eSetup_UUIE_fastStart) { | |
883 | for (i = 0; i < setup->fastStart.count; i++) { | |
3db05fea | 884 | ret = process_olc(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
885 | &setup->fastStart.item[i]); |
886 | if (ret < 0) | |
887 | return -1; | |
888 | } | |
889 | } | |
890 | ||
891 | return 0; | |
892 | } | |
893 | ||
894 | /****************************************************************************/ | |
3db05fea | 895 | static int process_callproceeding(struct sk_buff *skb, |
f587de0e PM |
896 | struct nf_conn *ct, |
897 | enum ip_conntrack_info ctinfo, | |
898 | unsigned char **data, int dataoff, | |
899 | CallProceeding_UUIE *callproc) | |
900 | { | |
901 | int ret; | |
902 | int i; | |
903 | ||
0d53778e | 904 | pr_debug("nf_ct_q931: CallProceeding\n"); |
f587de0e PM |
905 | |
906 | if (callproc->options & eCallProceeding_UUIE_h245Address) { | |
3db05fea | 907 | ret = expect_h245(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
908 | &callproc->h245Address); |
909 | if (ret < 0) | |
910 | return -1; | |
911 | } | |
912 | ||
913 | if (callproc->options & eCallProceeding_UUIE_fastStart) { | |
914 | for (i = 0; i < callproc->fastStart.count; i++) { | |
3db05fea | 915 | ret = process_olc(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
916 | &callproc->fastStart.item[i]); |
917 | if (ret < 0) | |
918 | return -1; | |
919 | } | |
920 | } | |
921 | ||
922 | return 0; | |
923 | } | |
924 | ||
925 | /****************************************************************************/ | |
3db05fea | 926 | static int process_connect(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
927 | enum ip_conntrack_info ctinfo, |
928 | unsigned char **data, int dataoff, | |
929 | Connect_UUIE *connect) | |
930 | { | |
931 | int ret; | |
932 | int i; | |
933 | ||
0d53778e | 934 | pr_debug("nf_ct_q931: Connect\n"); |
f587de0e PM |
935 | |
936 | if (connect->options & eConnect_UUIE_h245Address) { | |
3db05fea | 937 | ret = expect_h245(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
938 | &connect->h245Address); |
939 | if (ret < 0) | |
940 | return -1; | |
941 | } | |
942 | ||
943 | if (connect->options & eConnect_UUIE_fastStart) { | |
944 | for (i = 0; i < connect->fastStart.count; i++) { | |
3db05fea | 945 | ret = process_olc(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
946 | &connect->fastStart.item[i]); |
947 | if (ret < 0) | |
948 | return -1; | |
949 | } | |
950 | } | |
951 | ||
952 | return 0; | |
953 | } | |
954 | ||
955 | /****************************************************************************/ | |
3db05fea | 956 | static int process_alerting(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
957 | enum ip_conntrack_info ctinfo, |
958 | unsigned char **data, int dataoff, | |
959 | Alerting_UUIE *alert) | |
960 | { | |
961 | int ret; | |
962 | int i; | |
963 | ||
0d53778e | 964 | pr_debug("nf_ct_q931: Alerting\n"); |
f587de0e PM |
965 | |
966 | if (alert->options & eAlerting_UUIE_h245Address) { | |
3db05fea | 967 | ret = expect_h245(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
968 | &alert->h245Address); |
969 | if (ret < 0) | |
970 | return -1; | |
971 | } | |
972 | ||
973 | if (alert->options & eAlerting_UUIE_fastStart) { | |
974 | for (i = 0; i < alert->fastStart.count; i++) { | |
3db05fea | 975 | ret = process_olc(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
976 | &alert->fastStart.item[i]); |
977 | if (ret < 0) | |
978 | return -1; | |
979 | } | |
980 | } | |
981 | ||
982 | return 0; | |
983 | } | |
984 | ||
f587de0e | 985 | /****************************************************************************/ |
3db05fea | 986 | static int process_facility(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
987 | enum ip_conntrack_info ctinfo, |
988 | unsigned char **data, int dataoff, | |
989 | Facility_UUIE *facility) | |
990 | { | |
991 | int ret; | |
992 | int i; | |
993 | ||
0d53778e | 994 | pr_debug("nf_ct_q931: Facility\n"); |
f587de0e PM |
995 | |
996 | if (facility->reason.choice == eFacilityReason_callForwarded) { | |
997 | if (facility->options & eFacility_UUIE_alternativeAddress) | |
3db05fea | 998 | return expect_callforwarding(skb, ct, ctinfo, data, |
f587de0e PM |
999 | dataoff, |
1000 | &facility-> | |
1001 | alternativeAddress); | |
1002 | return 0; | |
1003 | } | |
1004 | ||
1005 | if (facility->options & eFacility_UUIE_h245Address) { | |
3db05fea | 1006 | ret = expect_h245(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1007 | &facility->h245Address); |
1008 | if (ret < 0) | |
1009 | return -1; | |
1010 | } | |
1011 | ||
1012 | if (facility->options & eFacility_UUIE_fastStart) { | |
1013 | for (i = 0; i < facility->fastStart.count; i++) { | |
3db05fea | 1014 | ret = process_olc(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1015 | &facility->fastStart.item[i]); |
1016 | if (ret < 0) | |
1017 | return -1; | |
1018 | } | |
1019 | } | |
1020 | ||
1021 | return 0; | |
1022 | } | |
1023 | ||
1024 | /****************************************************************************/ | |
3db05fea | 1025 | static int process_progress(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
1026 | enum ip_conntrack_info ctinfo, |
1027 | unsigned char **data, int dataoff, | |
1028 | Progress_UUIE *progress) | |
1029 | { | |
1030 | int ret; | |
1031 | int i; | |
1032 | ||
0d53778e | 1033 | pr_debug("nf_ct_q931: Progress\n"); |
f587de0e PM |
1034 | |
1035 | if (progress->options & eProgress_UUIE_h245Address) { | |
3db05fea | 1036 | ret = expect_h245(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1037 | &progress->h245Address); |
1038 | if (ret < 0) | |
1039 | return -1; | |
1040 | } | |
1041 | ||
1042 | if (progress->options & eProgress_UUIE_fastStart) { | |
1043 | for (i = 0; i < progress->fastStart.count; i++) { | |
3db05fea | 1044 | ret = process_olc(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1045 | &progress->fastStart.item[i]); |
1046 | if (ret < 0) | |
1047 | return -1; | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | return 0; | |
1052 | } | |
1053 | ||
1054 | /****************************************************************************/ | |
3db05fea | 1055 | static int process_q931(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
1056 | enum ip_conntrack_info ctinfo, |
1057 | unsigned char **data, int dataoff, Q931 *q931) | |
1058 | { | |
1059 | H323_UU_PDU *pdu = &q931->UUIE.h323_uu_pdu; | |
1060 | int i; | |
1061 | int ret = 0; | |
1062 | ||
1063 | switch (pdu->h323_message_body.choice) { | |
1064 | case eH323_UU_PDU_h323_message_body_setup: | |
3db05fea | 1065 | ret = process_setup(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1066 | &pdu->h323_message_body.setup); |
1067 | break; | |
1068 | case eH323_UU_PDU_h323_message_body_callProceeding: | |
3db05fea | 1069 | ret = process_callproceeding(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1070 | &pdu->h323_message_body. |
1071 | callProceeding); | |
1072 | break; | |
1073 | case eH323_UU_PDU_h323_message_body_connect: | |
3db05fea | 1074 | ret = process_connect(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1075 | &pdu->h323_message_body.connect); |
1076 | break; | |
1077 | case eH323_UU_PDU_h323_message_body_alerting: | |
3db05fea | 1078 | ret = process_alerting(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1079 | &pdu->h323_message_body.alerting); |
1080 | break; | |
f587de0e | 1081 | case eH323_UU_PDU_h323_message_body_facility: |
3db05fea | 1082 | ret = process_facility(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1083 | &pdu->h323_message_body.facility); |
1084 | break; | |
1085 | case eH323_UU_PDU_h323_message_body_progress: | |
3db05fea | 1086 | ret = process_progress(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1087 | &pdu->h323_message_body.progress); |
1088 | break; | |
1089 | default: | |
0d53778e PM |
1090 | pr_debug("nf_ct_q931: Q.931 signal %d\n", |
1091 | pdu->h323_message_body.choice); | |
f587de0e PM |
1092 | break; |
1093 | } | |
1094 | ||
1095 | if (ret < 0) | |
1096 | return -1; | |
1097 | ||
1098 | if (pdu->options & eH323_UU_PDU_h245Control) { | |
1099 | for (i = 0; i < pdu->h245Control.count; i++) { | |
3db05fea | 1100 | ret = process_h245(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1101 | &pdu->h245Control.item[i]); |
1102 | if (ret < 0) | |
1103 | return -1; | |
1104 | } | |
1105 | } | |
1106 | ||
1107 | return 0; | |
1108 | } | |
1109 | ||
1110 | /****************************************************************************/ | |
3db05fea | 1111 | static int q931_help(struct sk_buff *skb, unsigned int protoff, |
f587de0e PM |
1112 | struct nf_conn *ct, enum ip_conntrack_info ctinfo) |
1113 | { | |
1114 | static Q931 q931; | |
1115 | unsigned char *data = NULL; | |
1116 | int datalen; | |
1117 | int dataoff; | |
1118 | int ret; | |
1119 | ||
1120 | /* Until there's been traffic both ways, don't look in packets. */ | |
1121 | if (ctinfo != IP_CT_ESTABLISHED && | |
1122 | ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) { | |
1123 | return NF_ACCEPT; | |
1124 | } | |
3db05fea | 1125 | pr_debug("nf_ct_q931: skblen = %u\n", skb->len); |
f587de0e PM |
1126 | |
1127 | spin_lock_bh(&nf_h323_lock); | |
1128 | ||
1129 | /* Process each TPKT */ | |
3db05fea | 1130 | while (get_tpkt_data(skb, protoff, ct, ctinfo, |
f587de0e | 1131 | &data, &datalen, &dataoff)) { |
0d53778e | 1132 | pr_debug("nf_ct_q931: TPKT len=%d ", datalen); |
3c9fba65 | 1133 | nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple); |
f587de0e PM |
1134 | |
1135 | /* Decode Q.931 signal */ | |
1136 | ret = DecodeQ931(data, datalen, &q931); | |
1137 | if (ret < 0) { | |
0d53778e PM |
1138 | pr_debug("nf_ct_q931: decoding error: %s\n", |
1139 | ret == H323_ERROR_BOUND ? | |
1140 | "out of bound" : "out of range"); | |
f587de0e PM |
1141 | /* We don't drop when decoding error */ |
1142 | break; | |
1143 | } | |
1144 | ||
1145 | /* Process Q.931 signal */ | |
3db05fea | 1146 | if (process_q931(skb, ct, ctinfo, &data, dataoff, &q931) < 0) |
f587de0e PM |
1147 | goto drop; |
1148 | } | |
1149 | ||
1150 | spin_unlock_bh(&nf_h323_lock); | |
1151 | return NF_ACCEPT; | |
1152 | ||
1153 | drop: | |
1154 | spin_unlock_bh(&nf_h323_lock); | |
1155 | if (net_ratelimit()) | |
1156 | printk("nf_ct_q931: packet dropped\n"); | |
1157 | return NF_DROP; | |
1158 | } | |
1159 | ||
1160 | /****************************************************************************/ | |
6002f266 PM |
1161 | static const struct nf_conntrack_expect_policy q931_exp_policy = { |
1162 | /* T.120 and H.245 */ | |
1163 | .max_expected = H323_RTP_CHANNEL_MAX * 4 + 4, | |
1164 | .timeout = 240, | |
1165 | }; | |
1166 | ||
f587de0e PM |
1167 | static struct nf_conntrack_helper nf_conntrack_helper_q931[] __read_mostly = { |
1168 | { | |
1169 | .name = "Q.931", | |
1170 | .me = THIS_MODULE, | |
f587de0e | 1171 | .tuple.src.l3num = AF_INET, |
09640e63 | 1172 | .tuple.src.u.tcp.port = cpu_to_be16(Q931_PORT), |
f587de0e | 1173 | .tuple.dst.protonum = IPPROTO_TCP, |
6002f266 PM |
1174 | .help = q931_help, |
1175 | .expect_policy = &q931_exp_policy, | |
f587de0e PM |
1176 | }, |
1177 | { | |
1178 | .name = "Q.931", | |
1179 | .me = THIS_MODULE, | |
f587de0e | 1180 | .tuple.src.l3num = AF_INET6, |
09640e63 | 1181 | .tuple.src.u.tcp.port = cpu_to_be16(Q931_PORT), |
f587de0e | 1182 | .tuple.dst.protonum = IPPROTO_TCP, |
6002f266 PM |
1183 | .help = q931_help, |
1184 | .expect_policy = &q931_exp_policy, | |
f587de0e PM |
1185 | }, |
1186 | }; | |
1187 | ||
1188 | /****************************************************************************/ | |
3db05fea | 1189 | static unsigned char *get_udp_data(struct sk_buff *skb, unsigned int protoff, |
f587de0e PM |
1190 | int *datalen) |
1191 | { | |
905e3e8e JE |
1192 | const struct udphdr *uh; |
1193 | struct udphdr _uh; | |
f587de0e PM |
1194 | int dataoff; |
1195 | ||
3db05fea | 1196 | uh = skb_header_pointer(skb, protoff, sizeof(_uh), &_uh); |
f587de0e PM |
1197 | if (uh == NULL) |
1198 | return NULL; | |
1199 | dataoff = protoff + sizeof(_uh); | |
3db05fea | 1200 | if (dataoff >= skb->len) |
f587de0e | 1201 | return NULL; |
3db05fea HX |
1202 | *datalen = skb->len - dataoff; |
1203 | return skb_header_pointer(skb, dataoff, *datalen, h323_buffer); | |
f587de0e PM |
1204 | } |
1205 | ||
1206 | /****************************************************************************/ | |
1207 | static struct nf_conntrack_expect *find_expect(struct nf_conn *ct, | |
643a2c15 | 1208 | union nf_inet_addr *addr, |
f587de0e PM |
1209 | __be16 port) |
1210 | { | |
84541cc1 | 1211 | struct net *net = nf_ct_net(ct); |
f587de0e PM |
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 | ||
5d0aa2cc | 1221 | exp = __nf_ct_expect_find(net, nf_ct_zone(ct), &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 | 1745 | .tuple.src.l3num = AF_INET, |
09640e63 | 1746 | .tuple.src.u.udp.port = cpu_to_be16(RAS_PORT), |
f587de0e | 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 | 1754 | .tuple.src.l3num = AF_INET6, |
09640e63 | 1755 | .tuple.src.u.udp.port = cpu_to_be16(RAS_PORT), |
f587de0e | 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"); | |
4dc06f96 | 1832 | MODULE_ALIAS_NFCT_HELPER("h323"); |