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 | 711 | static int callforward_do_filter(const union nf_inet_addr *src, |
76108cea JE |
712 | const union nf_inet_addr *dst, |
713 | u_int8_t family) | |
f587de0e | 714 | { |
1e796fda | 715 | const struct nf_afinfo *afinfo; |
f587de0e PM |
716 | struct flowi fl1, fl2; |
717 | int ret = 0; | |
718 | ||
1841a4c7 PM |
719 | /* rcu_read_lock()ed by nf_hook_slow() */ |
720 | afinfo = nf_get_afinfo(family); | |
721 | if (!afinfo) | |
722 | return 0; | |
723 | ||
f587de0e PM |
724 | memset(&fl1, 0, sizeof(fl1)); |
725 | memset(&fl2, 0, sizeof(fl2)); | |
726 | ||
727 | switch (family) { | |
728 | case AF_INET: { | |
729 | struct rtable *rt1, *rt2; | |
730 | ||
731 | fl1.fl4_dst = src->ip; | |
732 | fl2.fl4_dst = dst->ip; | |
1841a4c7 PM |
733 | if (!afinfo->route((struct dst_entry **)&rt1, &fl1)) { |
734 | if (!afinfo->route((struct dst_entry **)&rt2, &fl2)) { | |
f587de0e PM |
735 | if (rt1->rt_gateway == rt2->rt_gateway && |
736 | rt1->u.dst.dev == rt2->u.dst.dev) | |
737 | ret = 1; | |
738 | dst_release(&rt2->u.dst); | |
739 | } | |
740 | dst_release(&rt1->u.dst); | |
741 | } | |
742 | break; | |
743 | } | |
1841a4c7 PM |
744 | #if defined(CONFIG_NF_CONNTRACK_IPV6) || \ |
745 | defined(CONFIG_NF_CONNTRACK_IPV6_MODULE) | |
f587de0e PM |
746 | case AF_INET6: { |
747 | struct rt6_info *rt1, *rt2; | |
748 | ||
749 | memcpy(&fl1.fl6_dst, src, sizeof(fl1.fl6_dst)); | |
750 | memcpy(&fl2.fl6_dst, dst, sizeof(fl2.fl6_dst)); | |
1841a4c7 PM |
751 | if (!afinfo->route((struct dst_entry **)&rt1, &fl1)) { |
752 | if (!afinfo->route((struct dst_entry **)&rt2, &fl2)) { | |
f587de0e | 753 | if (!memcmp(&rt1->rt6i_gateway, &rt2->rt6i_gateway, |
601e68e1 | 754 | sizeof(rt1->rt6i_gateway)) && |
f587de0e PM |
755 | rt1->u.dst.dev == rt2->u.dst.dev) |
756 | ret = 1; | |
757 | dst_release(&rt2->u.dst); | |
758 | } | |
759 | dst_release(&rt1->u.dst); | |
760 | } | |
761 | break; | |
762 | } | |
763 | #endif | |
764 | } | |
765 | return ret; | |
766 | ||
767 | } | |
768 | ||
769 | /****************************************************************************/ | |
3db05fea | 770 | static int expect_callforwarding(struct sk_buff *skb, |
f587de0e PM |
771 | struct nf_conn *ct, |
772 | enum ip_conntrack_info ctinfo, | |
773 | unsigned char **data, int dataoff, | |
774 | TransportAddress *taddr) | |
775 | { | |
776 | int dir = CTINFO2DIR(ctinfo); | |
777 | int ret = 0; | |
778 | __be16 port; | |
643a2c15 | 779 | union nf_inet_addr addr; |
f587de0e PM |
780 | struct nf_conntrack_expect *exp; |
781 | typeof(nat_callforwarding_hook) nat_callforwarding; | |
782 | ||
783 | /* Read alternativeAddress */ | |
784 | if (!get_h225_addr(ct, *data, taddr, &addr, &port) || port == 0) | |
785 | return 0; | |
786 | ||
787 | /* If the calling party is on the same side of the forward-to party, | |
788 | * we don't need to track the second call */ | |
789 | if (callforward_filter && | |
790 | callforward_do_filter(&addr, &ct->tuplehash[!dir].tuple.src.u3, | |
5e8fbe2a | 791 | nf_ct_l3num(ct))) { |
0d53778e | 792 | pr_debug("nf_ct_q931: Call Forwarding not tracked\n"); |
f587de0e PM |
793 | return 0; |
794 | } | |
795 | ||
796 | /* Create expect for the second call leg */ | |
6823645d | 797 | if ((exp = nf_ct_expect_alloc(ct)) == NULL) |
f587de0e | 798 | return -1; |
5e8fbe2a | 799 | nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct), |
6823645d PM |
800 | &ct->tuplehash[!dir].tuple.src.u3, &addr, |
801 | IPPROTO_TCP, NULL, &port); | |
f587de0e PM |
802 | exp->helper = nf_conntrack_helper_q931; |
803 | ||
804 | if (memcmp(&ct->tuplehash[dir].tuple.src.u3, | |
805 | &ct->tuplehash[!dir].tuple.dst.u3, | |
806 | sizeof(ct->tuplehash[dir].tuple.src.u3)) && | |
807 | (nat_callforwarding = rcu_dereference(nat_callforwarding_hook)) && | |
808 | ct->status & IPS_NAT_MASK) { | |
809 | /* Need NAT */ | |
3db05fea | 810 | ret = nat_callforwarding(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
811 | taddr, port, exp); |
812 | } else { /* Conntrack only */ | |
6823645d | 813 | if (nf_ct_expect_related(exp) == 0) { |
0d53778e | 814 | pr_debug("nf_ct_q931: expect Call Forwarding "); |
3c9fba65 | 815 | nf_ct_dump_tuple(&exp->tuple); |
f587de0e PM |
816 | } else |
817 | ret = -1; | |
818 | } | |
819 | ||
6823645d | 820 | nf_ct_expect_put(exp); |
f587de0e PM |
821 | |
822 | return ret; | |
823 | } | |
824 | ||
825 | /****************************************************************************/ | |
3db05fea | 826 | static int process_setup(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
827 | enum ip_conntrack_info ctinfo, |
828 | unsigned char **data, int dataoff, | |
829 | Setup_UUIE *setup) | |
830 | { | |
831 | int dir = CTINFO2DIR(ctinfo); | |
832 | int ret; | |
833 | int i; | |
834 | __be16 port; | |
643a2c15 | 835 | union nf_inet_addr addr; |
f587de0e PM |
836 | typeof(set_h225_addr_hook) set_h225_addr; |
837 | ||
0d53778e | 838 | pr_debug("nf_ct_q931: Setup\n"); |
f587de0e PM |
839 | |
840 | if (setup->options & eSetup_UUIE_h245Address) { | |
3db05fea | 841 | ret = expect_h245(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
842 | &setup->h245Address); |
843 | if (ret < 0) | |
844 | return -1; | |
845 | } | |
846 | ||
847 | set_h225_addr = rcu_dereference(set_h225_addr_hook); | |
848 | if ((setup->options & eSetup_UUIE_destCallSignalAddress) && | |
6aebb9b2 | 849 | (set_h225_addr) && ct->status & IPS_NAT_MASK && |
f587de0e | 850 | get_h225_addr(ct, *data, &setup->destCallSignalAddress, |
601e68e1 | 851 | &addr, &port) && |
f587de0e | 852 | memcmp(&addr, &ct->tuplehash[!dir].tuple.src.u3, sizeof(addr))) { |
5b095d98 | 853 | pr_debug("nf_ct_q931: set destCallSignalAddress %pI6:%hu->%pI6:%hu\n", |
38ff4fa4 | 854 | &addr, ntohs(port), &ct->tuplehash[!dir].tuple.src.u3, |
0d53778e | 855 | ntohs(ct->tuplehash[!dir].tuple.src.u.tcp.port)); |
3db05fea | 856 | ret = set_h225_addr(skb, data, dataoff, |
f587de0e PM |
857 | &setup->destCallSignalAddress, |
858 | &ct->tuplehash[!dir].tuple.src.u3, | |
859 | ct->tuplehash[!dir].tuple.src.u.tcp.port); | |
860 | if (ret < 0) | |
861 | return -1; | |
862 | } | |
863 | ||
864 | if ((setup->options & eSetup_UUIE_sourceCallSignalAddress) && | |
865 | (set_h225_addr) && ct->status & IPS_NAT_MASK && | |
866 | get_h225_addr(ct, *data, &setup->sourceCallSignalAddress, | |
601e68e1 | 867 | &addr, &port) && |
f587de0e | 868 | memcmp(&addr, &ct->tuplehash[!dir].tuple.dst.u3, sizeof(addr))) { |
5b095d98 | 869 | pr_debug("nf_ct_q931: set sourceCallSignalAddress %pI6:%hu->%pI6:%hu\n", |
38ff4fa4 | 870 | &addr, ntohs(port), &ct->tuplehash[!dir].tuple.dst.u3, |
0d53778e | 871 | ntohs(ct->tuplehash[!dir].tuple.dst.u.tcp.port)); |
3db05fea | 872 | ret = set_h225_addr(skb, data, dataoff, |
f587de0e PM |
873 | &setup->sourceCallSignalAddress, |
874 | &ct->tuplehash[!dir].tuple.dst.u3, | |
875 | ct->tuplehash[!dir].tuple.dst.u.tcp.port); | |
876 | if (ret < 0) | |
877 | return -1; | |
878 | } | |
879 | ||
880 | if (setup->options & eSetup_UUIE_fastStart) { | |
881 | for (i = 0; i < setup->fastStart.count; i++) { | |
3db05fea | 882 | ret = process_olc(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
883 | &setup->fastStart.item[i]); |
884 | if (ret < 0) | |
885 | return -1; | |
886 | } | |
887 | } | |
888 | ||
889 | return 0; | |
890 | } | |
891 | ||
892 | /****************************************************************************/ | |
3db05fea | 893 | static int process_callproceeding(struct sk_buff *skb, |
f587de0e PM |
894 | struct nf_conn *ct, |
895 | enum ip_conntrack_info ctinfo, | |
896 | unsigned char **data, int dataoff, | |
897 | CallProceeding_UUIE *callproc) | |
898 | { | |
899 | int ret; | |
900 | int i; | |
901 | ||
0d53778e | 902 | pr_debug("nf_ct_q931: CallProceeding\n"); |
f587de0e PM |
903 | |
904 | if (callproc->options & eCallProceeding_UUIE_h245Address) { | |
3db05fea | 905 | ret = expect_h245(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
906 | &callproc->h245Address); |
907 | if (ret < 0) | |
908 | return -1; | |
909 | } | |
910 | ||
911 | if (callproc->options & eCallProceeding_UUIE_fastStart) { | |
912 | for (i = 0; i < callproc->fastStart.count; i++) { | |
3db05fea | 913 | ret = process_olc(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
914 | &callproc->fastStart.item[i]); |
915 | if (ret < 0) | |
916 | return -1; | |
917 | } | |
918 | } | |
919 | ||
920 | return 0; | |
921 | } | |
922 | ||
923 | /****************************************************************************/ | |
3db05fea | 924 | static int process_connect(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
925 | enum ip_conntrack_info ctinfo, |
926 | unsigned char **data, int dataoff, | |
927 | Connect_UUIE *connect) | |
928 | { | |
929 | int ret; | |
930 | int i; | |
931 | ||
0d53778e | 932 | pr_debug("nf_ct_q931: Connect\n"); |
f587de0e PM |
933 | |
934 | if (connect->options & eConnect_UUIE_h245Address) { | |
3db05fea | 935 | ret = expect_h245(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
936 | &connect->h245Address); |
937 | if (ret < 0) | |
938 | return -1; | |
939 | } | |
940 | ||
941 | if (connect->options & eConnect_UUIE_fastStart) { | |
942 | for (i = 0; i < connect->fastStart.count; i++) { | |
3db05fea | 943 | ret = process_olc(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
944 | &connect->fastStart.item[i]); |
945 | if (ret < 0) | |
946 | return -1; | |
947 | } | |
948 | } | |
949 | ||
950 | return 0; | |
951 | } | |
952 | ||
953 | /****************************************************************************/ | |
3db05fea | 954 | static int process_alerting(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
955 | enum ip_conntrack_info ctinfo, |
956 | unsigned char **data, int dataoff, | |
957 | Alerting_UUIE *alert) | |
958 | { | |
959 | int ret; | |
960 | int i; | |
961 | ||
0d53778e | 962 | pr_debug("nf_ct_q931: Alerting\n"); |
f587de0e PM |
963 | |
964 | if (alert->options & eAlerting_UUIE_h245Address) { | |
3db05fea | 965 | ret = expect_h245(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
966 | &alert->h245Address); |
967 | if (ret < 0) | |
968 | return -1; | |
969 | } | |
970 | ||
971 | if (alert->options & eAlerting_UUIE_fastStart) { | |
972 | for (i = 0; i < alert->fastStart.count; i++) { | |
3db05fea | 973 | ret = process_olc(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
974 | &alert->fastStart.item[i]); |
975 | if (ret < 0) | |
976 | return -1; | |
977 | } | |
978 | } | |
979 | ||
980 | return 0; | |
981 | } | |
982 | ||
f587de0e | 983 | /****************************************************************************/ |
3db05fea | 984 | static int process_facility(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
985 | enum ip_conntrack_info ctinfo, |
986 | unsigned char **data, int dataoff, | |
987 | Facility_UUIE *facility) | |
988 | { | |
989 | int ret; | |
990 | int i; | |
991 | ||
0d53778e | 992 | pr_debug("nf_ct_q931: Facility\n"); |
f587de0e PM |
993 | |
994 | if (facility->reason.choice == eFacilityReason_callForwarded) { | |
995 | if (facility->options & eFacility_UUIE_alternativeAddress) | |
3db05fea | 996 | return expect_callforwarding(skb, ct, ctinfo, data, |
f587de0e PM |
997 | dataoff, |
998 | &facility-> | |
999 | alternativeAddress); | |
1000 | return 0; | |
1001 | } | |
1002 | ||
1003 | if (facility->options & eFacility_UUIE_h245Address) { | |
3db05fea | 1004 | ret = expect_h245(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1005 | &facility->h245Address); |
1006 | if (ret < 0) | |
1007 | return -1; | |
1008 | } | |
1009 | ||
1010 | if (facility->options & eFacility_UUIE_fastStart) { | |
1011 | for (i = 0; i < facility->fastStart.count; i++) { | |
3db05fea | 1012 | ret = process_olc(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1013 | &facility->fastStart.item[i]); |
1014 | if (ret < 0) | |
1015 | return -1; | |
1016 | } | |
1017 | } | |
1018 | ||
1019 | return 0; | |
1020 | } | |
1021 | ||
1022 | /****************************************************************************/ | |
3db05fea | 1023 | static int process_progress(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
1024 | enum ip_conntrack_info ctinfo, |
1025 | unsigned char **data, int dataoff, | |
1026 | Progress_UUIE *progress) | |
1027 | { | |
1028 | int ret; | |
1029 | int i; | |
1030 | ||
0d53778e | 1031 | pr_debug("nf_ct_q931: Progress\n"); |
f587de0e PM |
1032 | |
1033 | if (progress->options & eProgress_UUIE_h245Address) { | |
3db05fea | 1034 | ret = expect_h245(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1035 | &progress->h245Address); |
1036 | if (ret < 0) | |
1037 | return -1; | |
1038 | } | |
1039 | ||
1040 | if (progress->options & eProgress_UUIE_fastStart) { | |
1041 | for (i = 0; i < progress->fastStart.count; i++) { | |
3db05fea | 1042 | ret = process_olc(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1043 | &progress->fastStart.item[i]); |
1044 | if (ret < 0) | |
1045 | return -1; | |
1046 | } | |
1047 | } | |
1048 | ||
1049 | return 0; | |
1050 | } | |
1051 | ||
1052 | /****************************************************************************/ | |
3db05fea | 1053 | static int process_q931(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
1054 | enum ip_conntrack_info ctinfo, |
1055 | unsigned char **data, int dataoff, Q931 *q931) | |
1056 | { | |
1057 | H323_UU_PDU *pdu = &q931->UUIE.h323_uu_pdu; | |
1058 | int i; | |
1059 | int ret = 0; | |
1060 | ||
1061 | switch (pdu->h323_message_body.choice) { | |
1062 | case eH323_UU_PDU_h323_message_body_setup: | |
3db05fea | 1063 | ret = process_setup(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1064 | &pdu->h323_message_body.setup); |
1065 | break; | |
1066 | case eH323_UU_PDU_h323_message_body_callProceeding: | |
3db05fea | 1067 | ret = process_callproceeding(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1068 | &pdu->h323_message_body. |
1069 | callProceeding); | |
1070 | break; | |
1071 | case eH323_UU_PDU_h323_message_body_connect: | |
3db05fea | 1072 | ret = process_connect(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1073 | &pdu->h323_message_body.connect); |
1074 | break; | |
1075 | case eH323_UU_PDU_h323_message_body_alerting: | |
3db05fea | 1076 | ret = process_alerting(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1077 | &pdu->h323_message_body.alerting); |
1078 | break; | |
f587de0e | 1079 | case eH323_UU_PDU_h323_message_body_facility: |
3db05fea | 1080 | ret = process_facility(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1081 | &pdu->h323_message_body.facility); |
1082 | break; | |
1083 | case eH323_UU_PDU_h323_message_body_progress: | |
3db05fea | 1084 | ret = process_progress(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1085 | &pdu->h323_message_body.progress); |
1086 | break; | |
1087 | default: | |
0d53778e PM |
1088 | pr_debug("nf_ct_q931: Q.931 signal %d\n", |
1089 | pdu->h323_message_body.choice); | |
f587de0e PM |
1090 | break; |
1091 | } | |
1092 | ||
1093 | if (ret < 0) | |
1094 | return -1; | |
1095 | ||
1096 | if (pdu->options & eH323_UU_PDU_h245Control) { | |
1097 | for (i = 0; i < pdu->h245Control.count; i++) { | |
3db05fea | 1098 | ret = process_h245(skb, ct, ctinfo, data, dataoff, |
f587de0e PM |
1099 | &pdu->h245Control.item[i]); |
1100 | if (ret < 0) | |
1101 | return -1; | |
1102 | } | |
1103 | } | |
1104 | ||
1105 | return 0; | |
1106 | } | |
1107 | ||
1108 | /****************************************************************************/ | |
3db05fea | 1109 | static int q931_help(struct sk_buff *skb, unsigned int protoff, |
f587de0e PM |
1110 | struct nf_conn *ct, enum ip_conntrack_info ctinfo) |
1111 | { | |
1112 | static Q931 q931; | |
1113 | unsigned char *data = NULL; | |
1114 | int datalen; | |
1115 | int dataoff; | |
1116 | int ret; | |
1117 | ||
1118 | /* Until there's been traffic both ways, don't look in packets. */ | |
1119 | if (ctinfo != IP_CT_ESTABLISHED && | |
1120 | ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) { | |
1121 | return NF_ACCEPT; | |
1122 | } | |
3db05fea | 1123 | pr_debug("nf_ct_q931: skblen = %u\n", skb->len); |
f587de0e PM |
1124 | |
1125 | spin_lock_bh(&nf_h323_lock); | |
1126 | ||
1127 | /* Process each TPKT */ | |
3db05fea | 1128 | while (get_tpkt_data(skb, protoff, ct, ctinfo, |
f587de0e | 1129 | &data, &datalen, &dataoff)) { |
0d53778e | 1130 | pr_debug("nf_ct_q931: TPKT len=%d ", datalen); |
3c9fba65 | 1131 | nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple); |
f587de0e PM |
1132 | |
1133 | /* Decode Q.931 signal */ | |
1134 | ret = DecodeQ931(data, datalen, &q931); | |
1135 | if (ret < 0) { | |
0d53778e PM |
1136 | pr_debug("nf_ct_q931: decoding error: %s\n", |
1137 | ret == H323_ERROR_BOUND ? | |
1138 | "out of bound" : "out of range"); | |
f587de0e PM |
1139 | /* We don't drop when decoding error */ |
1140 | break; | |
1141 | } | |
1142 | ||
1143 | /* Process Q.931 signal */ | |
3db05fea | 1144 | if (process_q931(skb, ct, ctinfo, &data, dataoff, &q931) < 0) |
f587de0e PM |
1145 | goto drop; |
1146 | } | |
1147 | ||
1148 | spin_unlock_bh(&nf_h323_lock); | |
1149 | return NF_ACCEPT; | |
1150 | ||
1151 | drop: | |
1152 | spin_unlock_bh(&nf_h323_lock); | |
1153 | if (net_ratelimit()) | |
1154 | printk("nf_ct_q931: packet dropped\n"); | |
1155 | return NF_DROP; | |
1156 | } | |
1157 | ||
1158 | /****************************************************************************/ | |
6002f266 PM |
1159 | static const struct nf_conntrack_expect_policy q931_exp_policy = { |
1160 | /* T.120 and H.245 */ | |
1161 | .max_expected = H323_RTP_CHANNEL_MAX * 4 + 4, | |
1162 | .timeout = 240, | |
1163 | }; | |
1164 | ||
f587de0e PM |
1165 | static struct nf_conntrack_helper nf_conntrack_helper_q931[] __read_mostly = { |
1166 | { | |
1167 | .name = "Q.931", | |
1168 | .me = THIS_MODULE, | |
f587de0e | 1169 | .tuple.src.l3num = AF_INET, |
09640e63 | 1170 | .tuple.src.u.tcp.port = cpu_to_be16(Q931_PORT), |
f587de0e | 1171 | .tuple.dst.protonum = IPPROTO_TCP, |
6002f266 PM |
1172 | .help = q931_help, |
1173 | .expect_policy = &q931_exp_policy, | |
f587de0e PM |
1174 | }, |
1175 | { | |
1176 | .name = "Q.931", | |
1177 | .me = THIS_MODULE, | |
f587de0e | 1178 | .tuple.src.l3num = AF_INET6, |
09640e63 | 1179 | .tuple.src.u.tcp.port = cpu_to_be16(Q931_PORT), |
f587de0e | 1180 | .tuple.dst.protonum = IPPROTO_TCP, |
6002f266 PM |
1181 | .help = q931_help, |
1182 | .expect_policy = &q931_exp_policy, | |
f587de0e PM |
1183 | }, |
1184 | }; | |
1185 | ||
1186 | /****************************************************************************/ | |
3db05fea | 1187 | static unsigned char *get_udp_data(struct sk_buff *skb, unsigned int protoff, |
f587de0e PM |
1188 | int *datalen) |
1189 | { | |
905e3e8e JE |
1190 | const struct udphdr *uh; |
1191 | struct udphdr _uh; | |
f587de0e PM |
1192 | int dataoff; |
1193 | ||
3db05fea | 1194 | uh = skb_header_pointer(skb, protoff, sizeof(_uh), &_uh); |
f587de0e PM |
1195 | if (uh == NULL) |
1196 | return NULL; | |
1197 | dataoff = protoff + sizeof(_uh); | |
3db05fea | 1198 | if (dataoff >= skb->len) |
f587de0e | 1199 | return NULL; |
3db05fea HX |
1200 | *datalen = skb->len - dataoff; |
1201 | return skb_header_pointer(skb, dataoff, *datalen, h323_buffer); | |
f587de0e PM |
1202 | } |
1203 | ||
1204 | /****************************************************************************/ | |
1205 | static struct nf_conntrack_expect *find_expect(struct nf_conn *ct, | |
643a2c15 | 1206 | union nf_inet_addr *addr, |
f587de0e PM |
1207 | __be16 port) |
1208 | { | |
84541cc1 | 1209 | struct net *net = nf_ct_net(ct); |
f587de0e PM |
1210 | struct nf_conntrack_expect *exp; |
1211 | struct nf_conntrack_tuple tuple; | |
1212 | ||
1213 | memset(&tuple.src.u3, 0, sizeof(tuple.src.u3)); | |
1214 | tuple.src.u.tcp.port = 0; | |
1215 | memcpy(&tuple.dst.u3, addr, sizeof(tuple.dst.u3)); | |
1216 | tuple.dst.u.tcp.port = port; | |
1217 | tuple.dst.protonum = IPPROTO_TCP; | |
1218 | ||
84541cc1 | 1219 | exp = __nf_ct_expect_find(net, &tuple); |
f587de0e PM |
1220 | if (exp && exp->master == ct) |
1221 | return exp; | |
1222 | return NULL; | |
1223 | } | |
1224 | ||
1225 | /****************************************************************************/ | |
1226 | static int set_expect_timeout(struct nf_conntrack_expect *exp, | |
1227 | unsigned timeout) | |
1228 | { | |
1229 | if (!exp || !del_timer(&exp->timeout)) | |
1230 | return 0; | |
1231 | ||
1232 | exp->timeout.expires = jiffies + timeout * HZ; | |
1233 | add_timer(&exp->timeout); | |
1234 | ||
1235 | return 1; | |
1236 | } | |
1237 | ||
1238 | /****************************************************************************/ | |
3db05fea | 1239 | static int expect_q931(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
1240 | enum ip_conntrack_info ctinfo, |
1241 | unsigned char **data, | |
1242 | TransportAddress *taddr, int count) | |
1243 | { | |
1244 | struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info; | |
1245 | int dir = CTINFO2DIR(ctinfo); | |
1246 | int ret = 0; | |
1247 | int i; | |
1248 | __be16 port; | |
643a2c15 | 1249 | union nf_inet_addr addr; |
f587de0e PM |
1250 | struct nf_conntrack_expect *exp; |
1251 | typeof(nat_q931_hook) nat_q931; | |
1252 | ||
1253 | /* Look for the first related address */ | |
1254 | for (i = 0; i < count; i++) { | |
1255 | if (get_h225_addr(ct, *data, &taddr[i], &addr, &port) && | |
1256 | memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, | |
601e68e1 | 1257 | sizeof(addr)) == 0 && port != 0) |
f587de0e PM |
1258 | break; |
1259 | } | |
1260 | ||
1261 | if (i >= count) /* Not found */ | |
1262 | return 0; | |
1263 | ||
1264 | /* Create expect for Q.931 */ | |
6823645d | 1265 | if ((exp = nf_ct_expect_alloc(ct)) == NULL) |
f587de0e | 1266 | return -1; |
5e8fbe2a | 1267 | nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct), |
6823645d PM |
1268 | gkrouted_only ? /* only accept calls from GK? */ |
1269 | &ct->tuplehash[!dir].tuple.src.u3 : NULL, | |
1270 | &ct->tuplehash[!dir].tuple.dst.u3, | |
1271 | IPPROTO_TCP, NULL, &port); | |
f587de0e PM |
1272 | exp->helper = nf_conntrack_helper_q931; |
1273 | exp->flags = NF_CT_EXPECT_PERMANENT; /* Accept multiple calls */ | |
1274 | ||
1275 | nat_q931 = rcu_dereference(nat_q931_hook); | |
1276 | if (nat_q931 && ct->status & IPS_NAT_MASK) { /* Need NAT */ | |
3db05fea | 1277 | ret = nat_q931(skb, ct, ctinfo, data, taddr, i, port, exp); |
f587de0e | 1278 | } else { /* Conntrack only */ |
6823645d | 1279 | if (nf_ct_expect_related(exp) == 0) { |
0d53778e | 1280 | pr_debug("nf_ct_ras: expect Q.931 "); |
3c9fba65 | 1281 | nf_ct_dump_tuple(&exp->tuple); |
f587de0e PM |
1282 | |
1283 | /* Save port for looking up expect in processing RCF */ | |
1284 | info->sig_port[dir] = port; | |
1285 | } else | |
1286 | ret = -1; | |
1287 | } | |
1288 | ||
6823645d | 1289 | nf_ct_expect_put(exp); |
f587de0e PM |
1290 | |
1291 | return ret; | |
1292 | } | |
1293 | ||
1294 | /****************************************************************************/ | |
3db05fea | 1295 | static int process_grq(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
1296 | enum ip_conntrack_info ctinfo, |
1297 | unsigned char **data, GatekeeperRequest *grq) | |
1298 | { | |
1299 | typeof(set_ras_addr_hook) set_ras_addr; | |
1300 | ||
0d53778e | 1301 | pr_debug("nf_ct_ras: GRQ\n"); |
f587de0e PM |
1302 | |
1303 | set_ras_addr = rcu_dereference(set_ras_addr_hook); | |
1304 | if (set_ras_addr && ct->status & IPS_NAT_MASK) /* NATed */ | |
3db05fea | 1305 | return set_ras_addr(skb, ct, ctinfo, data, |
f587de0e PM |
1306 | &grq->rasAddress, 1); |
1307 | return 0; | |
1308 | } | |
1309 | ||
1310 | /****************************************************************************/ | |
3db05fea | 1311 | static int process_gcf(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
1312 | enum ip_conntrack_info ctinfo, |
1313 | unsigned char **data, GatekeeperConfirm *gcf) | |
1314 | { | |
1315 | int dir = CTINFO2DIR(ctinfo); | |
1316 | int ret = 0; | |
1317 | __be16 port; | |
643a2c15 | 1318 | union nf_inet_addr addr; |
f587de0e PM |
1319 | struct nf_conntrack_expect *exp; |
1320 | ||
0d53778e | 1321 | pr_debug("nf_ct_ras: GCF\n"); |
f587de0e PM |
1322 | |
1323 | if (!get_h225_addr(ct, *data, &gcf->rasAddress, &addr, &port)) | |
1324 | return 0; | |
1325 | ||
1326 | /* Registration port is the same as discovery port */ | |
1327 | if (!memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) && | |
1328 | port == ct->tuplehash[dir].tuple.src.u.udp.port) | |
1329 | return 0; | |
1330 | ||
1331 | /* Avoid RAS expectation loops. A GCF is never expected. */ | |
1332 | if (test_bit(IPS_EXPECTED_BIT, &ct->status)) | |
1333 | return 0; | |
1334 | ||
1335 | /* Need new expect */ | |
6823645d | 1336 | if ((exp = nf_ct_expect_alloc(ct)) == NULL) |
f587de0e | 1337 | return -1; |
5e8fbe2a | 1338 | nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct), |
6823645d PM |
1339 | &ct->tuplehash[!dir].tuple.src.u3, &addr, |
1340 | IPPROTO_UDP, NULL, &port); | |
f587de0e PM |
1341 | exp->helper = nf_conntrack_helper_ras; |
1342 | ||
6823645d | 1343 | if (nf_ct_expect_related(exp) == 0) { |
0d53778e | 1344 | pr_debug("nf_ct_ras: expect RAS "); |
3c9fba65 | 1345 | nf_ct_dump_tuple(&exp->tuple); |
f587de0e PM |
1346 | } else |
1347 | ret = -1; | |
1348 | ||
6823645d | 1349 | nf_ct_expect_put(exp); |
f587de0e PM |
1350 | |
1351 | return ret; | |
1352 | } | |
1353 | ||
1354 | /****************************************************************************/ | |
3db05fea | 1355 | static int process_rrq(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
1356 | enum ip_conntrack_info ctinfo, |
1357 | unsigned char **data, RegistrationRequest *rrq) | |
1358 | { | |
1359 | struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info; | |
1360 | int ret; | |
1361 | typeof(set_ras_addr_hook) set_ras_addr; | |
1362 | ||
0d53778e | 1363 | pr_debug("nf_ct_ras: RRQ\n"); |
f587de0e | 1364 | |
3db05fea | 1365 | ret = expect_q931(skb, ct, ctinfo, data, |
f587de0e PM |
1366 | rrq->callSignalAddress.item, |
1367 | rrq->callSignalAddress.count); | |
1368 | if (ret < 0) | |
1369 | return -1; | |
1370 | ||
1371 | set_ras_addr = rcu_dereference(set_ras_addr_hook); | |
1372 | if (set_ras_addr && ct->status & IPS_NAT_MASK) { | |
3db05fea | 1373 | ret = set_ras_addr(skb, ct, ctinfo, data, |
f587de0e PM |
1374 | rrq->rasAddress.item, |
1375 | rrq->rasAddress.count); | |
1376 | if (ret < 0) | |
1377 | return -1; | |
1378 | } | |
1379 | ||
1380 | if (rrq->options & eRegistrationRequest_timeToLive) { | |
0d53778e | 1381 | pr_debug("nf_ct_ras: RRQ TTL = %u seconds\n", rrq->timeToLive); |
f587de0e PM |
1382 | info->timeout = rrq->timeToLive; |
1383 | } else | |
1384 | info->timeout = default_rrq_ttl; | |
1385 | ||
1386 | return 0; | |
1387 | } | |
1388 | ||
1389 | /****************************************************************************/ | |
3db05fea | 1390 | static int process_rcf(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
1391 | enum ip_conntrack_info ctinfo, |
1392 | unsigned char **data, RegistrationConfirm *rcf) | |
1393 | { | |
1394 | struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info; | |
1395 | int dir = CTINFO2DIR(ctinfo); | |
1396 | int ret; | |
1397 | struct nf_conntrack_expect *exp; | |
1398 | typeof(set_sig_addr_hook) set_sig_addr; | |
1399 | ||
0d53778e | 1400 | pr_debug("nf_ct_ras: RCF\n"); |
f587de0e PM |
1401 | |
1402 | set_sig_addr = rcu_dereference(set_sig_addr_hook); | |
1403 | if (set_sig_addr && ct->status & IPS_NAT_MASK) { | |
3db05fea | 1404 | ret = set_sig_addr(skb, ct, ctinfo, data, |
f587de0e PM |
1405 | rcf->callSignalAddress.item, |
1406 | rcf->callSignalAddress.count); | |
1407 | if (ret < 0) | |
1408 | return -1; | |
1409 | } | |
1410 | ||
1411 | if (rcf->options & eRegistrationConfirm_timeToLive) { | |
0d53778e | 1412 | pr_debug("nf_ct_ras: RCF TTL = %u seconds\n", rcf->timeToLive); |
f587de0e PM |
1413 | info->timeout = rcf->timeToLive; |
1414 | } | |
1415 | ||
1416 | if (info->timeout > 0) { | |
0d53778e PM |
1417 | pr_debug("nf_ct_ras: set RAS connection timeout to " |
1418 | "%u seconds\n", info->timeout); | |
3db05fea | 1419 | nf_ct_refresh(ct, skb, info->timeout * HZ); |
f587de0e PM |
1420 | |
1421 | /* Set expect timeout */ | |
f8ba1aff | 1422 | spin_lock_bh(&nf_conntrack_lock); |
f587de0e PM |
1423 | exp = find_expect(ct, &ct->tuplehash[dir].tuple.dst.u3, |
1424 | info->sig_port[!dir]); | |
1425 | if (exp) { | |
0d53778e PM |
1426 | pr_debug("nf_ct_ras: set Q.931 expect " |
1427 | "timeout to %u seconds for", | |
1428 | info->timeout); | |
3c9fba65 | 1429 | nf_ct_dump_tuple(&exp->tuple); |
f587de0e PM |
1430 | set_expect_timeout(exp, info->timeout); |
1431 | } | |
f8ba1aff | 1432 | spin_unlock_bh(&nf_conntrack_lock); |
f587de0e PM |
1433 | } |
1434 | ||
1435 | return 0; | |
1436 | } | |
1437 | ||
1438 | /****************************************************************************/ | |
3db05fea | 1439 | static int process_urq(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
1440 | enum ip_conntrack_info ctinfo, |
1441 | unsigned char **data, UnregistrationRequest *urq) | |
1442 | { | |
1443 | struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info; | |
1444 | int dir = CTINFO2DIR(ctinfo); | |
1445 | int ret; | |
1446 | typeof(set_sig_addr_hook) set_sig_addr; | |
1447 | ||
0d53778e | 1448 | pr_debug("nf_ct_ras: URQ\n"); |
f587de0e PM |
1449 | |
1450 | set_sig_addr = rcu_dereference(set_sig_addr_hook); | |
1451 | if (set_sig_addr && ct->status & IPS_NAT_MASK) { | |
3db05fea | 1452 | ret = set_sig_addr(skb, ct, ctinfo, data, |
f587de0e PM |
1453 | urq->callSignalAddress.item, |
1454 | urq->callSignalAddress.count); | |
1455 | if (ret < 0) | |
1456 | return -1; | |
1457 | } | |
1458 | ||
1459 | /* Clear old expect */ | |
1460 | nf_ct_remove_expectations(ct); | |
1461 | info->sig_port[dir] = 0; | |
1462 | info->sig_port[!dir] = 0; | |
1463 | ||
1464 | /* Give it 30 seconds for UCF or URJ */ | |
3db05fea | 1465 | nf_ct_refresh(ct, skb, 30 * HZ); |
f587de0e PM |
1466 | |
1467 | return 0; | |
1468 | } | |
1469 | ||
1470 | /****************************************************************************/ | |
3db05fea | 1471 | static int process_arq(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
1472 | enum ip_conntrack_info ctinfo, |
1473 | unsigned char **data, AdmissionRequest *arq) | |
1474 | { | |
905e3e8e | 1475 | const struct nf_ct_h323_master *info = &nfct_help(ct)->help.ct_h323_info; |
f587de0e PM |
1476 | int dir = CTINFO2DIR(ctinfo); |
1477 | __be16 port; | |
643a2c15 | 1478 | union nf_inet_addr addr; |
f587de0e PM |
1479 | typeof(set_h225_addr_hook) set_h225_addr; |
1480 | ||
0d53778e | 1481 | pr_debug("nf_ct_ras: ARQ\n"); |
f587de0e PM |
1482 | |
1483 | set_h225_addr = rcu_dereference(set_h225_addr_hook); | |
1484 | if ((arq->options & eAdmissionRequest_destCallSignalAddress) && | |
1485 | get_h225_addr(ct, *data, &arq->destCallSignalAddress, | |
601e68e1 | 1486 | &addr, &port) && |
f587de0e PM |
1487 | !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) && |
1488 | port == info->sig_port[dir] && | |
1489 | set_h225_addr && ct->status & IPS_NAT_MASK) { | |
1490 | /* Answering ARQ */ | |
3db05fea | 1491 | return set_h225_addr(skb, data, 0, |
f587de0e PM |
1492 | &arq->destCallSignalAddress, |
1493 | &ct->tuplehash[!dir].tuple.dst.u3, | |
1494 | info->sig_port[!dir]); | |
1495 | } | |
1496 | ||
1497 | if ((arq->options & eAdmissionRequest_srcCallSignalAddress) && | |
1498 | get_h225_addr(ct, *data, &arq->srcCallSignalAddress, | |
601e68e1 | 1499 | &addr, &port) && |
f587de0e PM |
1500 | !memcmp(&addr, &ct->tuplehash[dir].tuple.src.u3, sizeof(addr)) && |
1501 | set_h225_addr && ct->status & IPS_NAT_MASK) { | |
1502 | /* Calling ARQ */ | |
3db05fea | 1503 | return set_h225_addr(skb, data, 0, |
f587de0e PM |
1504 | &arq->srcCallSignalAddress, |
1505 | &ct->tuplehash[!dir].tuple.dst.u3, | |
1506 | port); | |
1507 | } | |
1508 | ||
1509 | return 0; | |
1510 | } | |
1511 | ||
1512 | /****************************************************************************/ | |
3db05fea | 1513 | static int process_acf(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
1514 | enum ip_conntrack_info ctinfo, |
1515 | unsigned char **data, AdmissionConfirm *acf) | |
1516 | { | |
1517 | int dir = CTINFO2DIR(ctinfo); | |
1518 | int ret = 0; | |
1519 | __be16 port; | |
643a2c15 | 1520 | union nf_inet_addr addr; |
f587de0e PM |
1521 | struct nf_conntrack_expect *exp; |
1522 | typeof(set_sig_addr_hook) set_sig_addr; | |
1523 | ||
0d53778e | 1524 | pr_debug("nf_ct_ras: ACF\n"); |
f587de0e PM |
1525 | |
1526 | if (!get_h225_addr(ct, *data, &acf->destCallSignalAddress, | |
1527 | &addr, &port)) | |
1528 | return 0; | |
1529 | ||
1530 | if (!memcmp(&addr, &ct->tuplehash[dir].tuple.dst.u3, sizeof(addr))) { | |
1531 | /* Answering ACF */ | |
1532 | set_sig_addr = rcu_dereference(set_sig_addr_hook); | |
1533 | if (set_sig_addr && ct->status & IPS_NAT_MASK) | |
3db05fea | 1534 | return set_sig_addr(skb, ct, ctinfo, data, |
f587de0e PM |
1535 | &acf->destCallSignalAddress, 1); |
1536 | return 0; | |
1537 | } | |
1538 | ||
1539 | /* Need new expect */ | |
6823645d | 1540 | if ((exp = nf_ct_expect_alloc(ct)) == NULL) |
f587de0e | 1541 | return -1; |
5e8fbe2a | 1542 | nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct), |
6823645d PM |
1543 | &ct->tuplehash[!dir].tuple.src.u3, &addr, |
1544 | IPPROTO_TCP, NULL, &port); | |
f587de0e PM |
1545 | exp->flags = NF_CT_EXPECT_PERMANENT; |
1546 | exp->helper = nf_conntrack_helper_q931; | |
1547 | ||
6823645d | 1548 | if (nf_ct_expect_related(exp) == 0) { |
0d53778e | 1549 | pr_debug("nf_ct_ras: expect Q.931 "); |
3c9fba65 | 1550 | nf_ct_dump_tuple(&exp->tuple); |
f587de0e PM |
1551 | } else |
1552 | ret = -1; | |
1553 | ||
6823645d | 1554 | nf_ct_expect_put(exp); |
f587de0e PM |
1555 | |
1556 | return ret; | |
1557 | } | |
1558 | ||
1559 | /****************************************************************************/ | |
3db05fea | 1560 | static int process_lrq(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
1561 | enum ip_conntrack_info ctinfo, |
1562 | unsigned char **data, LocationRequest *lrq) | |
1563 | { | |
1564 | typeof(set_ras_addr_hook) set_ras_addr; | |
1565 | ||
0d53778e | 1566 | pr_debug("nf_ct_ras: LRQ\n"); |
f587de0e PM |
1567 | |
1568 | set_ras_addr = rcu_dereference(set_ras_addr_hook); | |
1569 | if (set_ras_addr && ct->status & IPS_NAT_MASK) | |
3db05fea | 1570 | return set_ras_addr(skb, ct, ctinfo, data, |
f587de0e PM |
1571 | &lrq->replyAddress, 1); |
1572 | return 0; | |
1573 | } | |
1574 | ||
1575 | /****************************************************************************/ | |
3db05fea | 1576 | static int process_lcf(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
1577 | enum ip_conntrack_info ctinfo, |
1578 | unsigned char **data, LocationConfirm *lcf) | |
1579 | { | |
1580 | int dir = CTINFO2DIR(ctinfo); | |
1581 | int ret = 0; | |
1582 | __be16 port; | |
643a2c15 | 1583 | union nf_inet_addr addr; |
f587de0e PM |
1584 | struct nf_conntrack_expect *exp; |
1585 | ||
0d53778e | 1586 | pr_debug("nf_ct_ras: LCF\n"); |
f587de0e PM |
1587 | |
1588 | if (!get_h225_addr(ct, *data, &lcf->callSignalAddress, | |
1589 | &addr, &port)) | |
1590 | return 0; | |
1591 | ||
1592 | /* Need new expect for call signal */ | |
6823645d | 1593 | if ((exp = nf_ct_expect_alloc(ct)) == NULL) |
f587de0e | 1594 | return -1; |
5e8fbe2a | 1595 | nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct), |
6823645d PM |
1596 | &ct->tuplehash[!dir].tuple.src.u3, &addr, |
1597 | IPPROTO_TCP, NULL, &port); | |
f587de0e PM |
1598 | exp->flags = NF_CT_EXPECT_PERMANENT; |
1599 | exp->helper = nf_conntrack_helper_q931; | |
1600 | ||
6823645d | 1601 | if (nf_ct_expect_related(exp) == 0) { |
0d53778e | 1602 | pr_debug("nf_ct_ras: expect Q.931 "); |
3c9fba65 | 1603 | nf_ct_dump_tuple(&exp->tuple); |
f587de0e PM |
1604 | } else |
1605 | ret = -1; | |
1606 | ||
6823645d | 1607 | nf_ct_expect_put(exp); |
f587de0e PM |
1608 | |
1609 | /* Ignore rasAddress */ | |
1610 | ||
1611 | return ret; | |
1612 | } | |
1613 | ||
1614 | /****************************************************************************/ | |
3db05fea | 1615 | static int process_irr(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
1616 | enum ip_conntrack_info ctinfo, |
1617 | unsigned char **data, InfoRequestResponse *irr) | |
1618 | { | |
1619 | int ret; | |
1620 | typeof(set_ras_addr_hook) set_ras_addr; | |
1621 | typeof(set_sig_addr_hook) set_sig_addr; | |
1622 | ||
0d53778e | 1623 | pr_debug("nf_ct_ras: IRR\n"); |
f587de0e PM |
1624 | |
1625 | set_ras_addr = rcu_dereference(set_ras_addr_hook); | |
1626 | if (set_ras_addr && ct->status & IPS_NAT_MASK) { | |
3db05fea | 1627 | ret = set_ras_addr(skb, ct, ctinfo, data, |
f587de0e PM |
1628 | &irr->rasAddress, 1); |
1629 | if (ret < 0) | |
1630 | return -1; | |
1631 | } | |
1632 | ||
1633 | set_sig_addr = rcu_dereference(set_sig_addr_hook); | |
1634 | if (set_sig_addr && ct->status & IPS_NAT_MASK) { | |
3db05fea | 1635 | ret = set_sig_addr(skb, ct, ctinfo, data, |
f587de0e PM |
1636 | irr->callSignalAddress.item, |
1637 | irr->callSignalAddress.count); | |
1638 | if (ret < 0) | |
1639 | return -1; | |
1640 | } | |
1641 | ||
1642 | return 0; | |
1643 | } | |
1644 | ||
1645 | /****************************************************************************/ | |
3db05fea | 1646 | static int process_ras(struct sk_buff *skb, struct nf_conn *ct, |
f587de0e PM |
1647 | enum ip_conntrack_info ctinfo, |
1648 | unsigned char **data, RasMessage *ras) | |
1649 | { | |
1650 | switch (ras->choice) { | |
1651 | case eRasMessage_gatekeeperRequest: | |
3db05fea | 1652 | return process_grq(skb, ct, ctinfo, data, |
f587de0e PM |
1653 | &ras->gatekeeperRequest); |
1654 | case eRasMessage_gatekeeperConfirm: | |
3db05fea | 1655 | return process_gcf(skb, ct, ctinfo, data, |
f587de0e PM |
1656 | &ras->gatekeeperConfirm); |
1657 | case eRasMessage_registrationRequest: | |
3db05fea | 1658 | return process_rrq(skb, ct, ctinfo, data, |
f587de0e PM |
1659 | &ras->registrationRequest); |
1660 | case eRasMessage_registrationConfirm: | |
3db05fea | 1661 | return process_rcf(skb, ct, ctinfo, data, |
f587de0e PM |
1662 | &ras->registrationConfirm); |
1663 | case eRasMessage_unregistrationRequest: | |
3db05fea | 1664 | return process_urq(skb, ct, ctinfo, data, |
f587de0e PM |
1665 | &ras->unregistrationRequest); |
1666 | case eRasMessage_admissionRequest: | |
3db05fea | 1667 | return process_arq(skb, ct, ctinfo, data, |
f587de0e PM |
1668 | &ras->admissionRequest); |
1669 | case eRasMessage_admissionConfirm: | |
3db05fea | 1670 | return process_acf(skb, ct, ctinfo, data, |
f587de0e PM |
1671 | &ras->admissionConfirm); |
1672 | case eRasMessage_locationRequest: | |
3db05fea | 1673 | return process_lrq(skb, ct, ctinfo, data, |
f587de0e PM |
1674 | &ras->locationRequest); |
1675 | case eRasMessage_locationConfirm: | |
3db05fea | 1676 | return process_lcf(skb, ct, ctinfo, data, |
f587de0e PM |
1677 | &ras->locationConfirm); |
1678 | case eRasMessage_infoRequestResponse: | |
3db05fea | 1679 | return process_irr(skb, ct, ctinfo, data, |
f587de0e PM |
1680 | &ras->infoRequestResponse); |
1681 | default: | |
0d53778e | 1682 | pr_debug("nf_ct_ras: RAS message %d\n", ras->choice); |
f587de0e PM |
1683 | break; |
1684 | } | |
1685 | ||
1686 | return 0; | |
1687 | } | |
1688 | ||
1689 | /****************************************************************************/ | |
3db05fea | 1690 | static int ras_help(struct sk_buff *skb, unsigned int protoff, |
f587de0e PM |
1691 | struct nf_conn *ct, enum ip_conntrack_info ctinfo) |
1692 | { | |
1693 | static RasMessage ras; | |
1694 | unsigned char *data; | |
1695 | int datalen = 0; | |
1696 | int ret; | |
1697 | ||
3db05fea | 1698 | pr_debug("nf_ct_ras: skblen = %u\n", skb->len); |
f587de0e PM |
1699 | |
1700 | spin_lock_bh(&nf_h323_lock); | |
1701 | ||
1702 | /* Get UDP data */ | |
3db05fea | 1703 | data = get_udp_data(skb, protoff, &datalen); |
f587de0e PM |
1704 | if (data == NULL) |
1705 | goto accept; | |
0d53778e | 1706 | pr_debug("nf_ct_ras: RAS message len=%d ", datalen); |
3c9fba65 | 1707 | nf_ct_dump_tuple(&ct->tuplehash[CTINFO2DIR(ctinfo)].tuple); |
f587de0e PM |
1708 | |
1709 | /* Decode RAS message */ | |
1710 | ret = DecodeRasMessage(data, datalen, &ras); | |
1711 | if (ret < 0) { | |
0d53778e PM |
1712 | pr_debug("nf_ct_ras: decoding error: %s\n", |
1713 | ret == H323_ERROR_BOUND ? | |
1714 | "out of bound" : "out of range"); | |
f587de0e PM |
1715 | goto accept; |
1716 | } | |
1717 | ||
1718 | /* Process RAS message */ | |
3db05fea | 1719 | if (process_ras(skb, ct, ctinfo, &data, &ras) < 0) |
f587de0e PM |
1720 | goto drop; |
1721 | ||
1722 | accept: | |
1723 | spin_unlock_bh(&nf_h323_lock); | |
1724 | return NF_ACCEPT; | |
1725 | ||
1726 | drop: | |
1727 | spin_unlock_bh(&nf_h323_lock); | |
1728 | if (net_ratelimit()) | |
1729 | printk("nf_ct_ras: packet dropped\n"); | |
1730 | return NF_DROP; | |
1731 | } | |
1732 | ||
1733 | /****************************************************************************/ | |
6002f266 PM |
1734 | static const struct nf_conntrack_expect_policy ras_exp_policy = { |
1735 | .max_expected = 32, | |
1736 | .timeout = 240, | |
1737 | }; | |
1738 | ||
f587de0e PM |
1739 | static struct nf_conntrack_helper nf_conntrack_helper_ras[] __read_mostly = { |
1740 | { | |
1741 | .name = "RAS", | |
1742 | .me = THIS_MODULE, | |
f587de0e | 1743 | .tuple.src.l3num = AF_INET, |
09640e63 | 1744 | .tuple.src.u.udp.port = cpu_to_be16(RAS_PORT), |
f587de0e | 1745 | .tuple.dst.protonum = IPPROTO_UDP, |
f587de0e | 1746 | .help = ras_help, |
6002f266 | 1747 | .expect_policy = &ras_exp_policy, |
f587de0e PM |
1748 | }, |
1749 | { | |
1750 | .name = "RAS", | |
1751 | .me = THIS_MODULE, | |
f587de0e | 1752 | .tuple.src.l3num = AF_INET6, |
09640e63 | 1753 | .tuple.src.u.udp.port = cpu_to_be16(RAS_PORT), |
f587de0e | 1754 | .tuple.dst.protonum = IPPROTO_UDP, |
f587de0e | 1755 | .help = ras_help, |
6002f266 | 1756 | .expect_policy = &ras_exp_policy, |
f587de0e PM |
1757 | }, |
1758 | }; | |
1759 | ||
1760 | /****************************************************************************/ | |
1761 | static void __exit nf_conntrack_h323_fini(void) | |
1762 | { | |
1763 | nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[1]); | |
1764 | nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]); | |
1765 | nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]); | |
1766 | nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]); | |
a56b8f81 | 1767 | nf_conntrack_helper_unregister(&nf_conntrack_helper_h245); |
f587de0e | 1768 | kfree(h323_buffer); |
0d53778e | 1769 | pr_debug("nf_ct_h323: fini\n"); |
f587de0e PM |
1770 | } |
1771 | ||
1772 | /****************************************************************************/ | |
1773 | static int __init nf_conntrack_h323_init(void) | |
1774 | { | |
1775 | int ret; | |
1776 | ||
1777 | h323_buffer = kmalloc(65536, GFP_KERNEL); | |
1778 | if (!h323_buffer) | |
1779 | return -ENOMEM; | |
a56b8f81 | 1780 | ret = nf_conntrack_helper_register(&nf_conntrack_helper_h245); |
f587de0e PM |
1781 | if (ret < 0) |
1782 | goto err1; | |
a56b8f81 | 1783 | ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[0]); |
f587de0e PM |
1784 | if (ret < 0) |
1785 | goto err2; | |
a56b8f81 | 1786 | ret = nf_conntrack_helper_register(&nf_conntrack_helper_q931[1]); |
f587de0e PM |
1787 | if (ret < 0) |
1788 | goto err3; | |
a56b8f81 | 1789 | ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[0]); |
f587de0e PM |
1790 | if (ret < 0) |
1791 | goto err4; | |
a56b8f81 PM |
1792 | ret = nf_conntrack_helper_register(&nf_conntrack_helper_ras[1]); |
1793 | if (ret < 0) | |
1794 | goto err5; | |
0d53778e | 1795 | pr_debug("nf_ct_h323: init success\n"); |
f587de0e PM |
1796 | return 0; |
1797 | ||
a56b8f81 | 1798 | err5: |
f587de0e | 1799 | nf_conntrack_helper_unregister(&nf_conntrack_helper_ras[0]); |
a56b8f81 | 1800 | err4: |
f587de0e | 1801 | nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[1]); |
a56b8f81 | 1802 | err3: |
f587de0e | 1803 | nf_conntrack_helper_unregister(&nf_conntrack_helper_q931[0]); |
a56b8f81 PM |
1804 | err2: |
1805 | nf_conntrack_helper_unregister(&nf_conntrack_helper_h245); | |
f587de0e | 1806 | err1: |
8a548868 | 1807 | kfree(h323_buffer); |
f587de0e PM |
1808 | return ret; |
1809 | } | |
1810 | ||
1811 | /****************************************************************************/ | |
1812 | module_init(nf_conntrack_h323_init); | |
1813 | module_exit(nf_conntrack_h323_fini); | |
1814 | ||
1815 | EXPORT_SYMBOL_GPL(get_h225_addr); | |
1816 | EXPORT_SYMBOL_GPL(set_h245_addr_hook); | |
1817 | EXPORT_SYMBOL_GPL(set_h225_addr_hook); | |
1818 | EXPORT_SYMBOL_GPL(set_sig_addr_hook); | |
1819 | EXPORT_SYMBOL_GPL(set_ras_addr_hook); | |
1820 | EXPORT_SYMBOL_GPL(nat_rtp_rtcp_hook); | |
1821 | EXPORT_SYMBOL_GPL(nat_t120_hook); | |
1822 | EXPORT_SYMBOL_GPL(nat_h245_hook); | |
1823 | EXPORT_SYMBOL_GPL(nat_callforwarding_hook); | |
1824 | EXPORT_SYMBOL_GPL(nat_q931_hook); | |
1825 | ||
1826 | MODULE_AUTHOR("Jing Min Zhao <zhaojingmin@users.sourceforge.net>"); | |
1827 | MODULE_DESCRIPTION("H.323 connection tracking helper"); | |
1828 | MODULE_LICENSE("GPL"); | |
1829 | MODULE_ALIAS("ip_conntrack_h323"); | |
4dc06f96 | 1830 | MODULE_ALIAS_NFCT_HELPER("h323"); |