-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnf_conntrack_skinny.c
813 lines (739 loc) · 28.1 KB
/
nf_conntrack_skinny.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
/* nf_conntrack_skinny -- Connection tracking for Skinny.
*
* $Id$
*
*
* PRINCIPLES OF OPERATION
*
* Skinny is a TCP connection from a Call Manager to each IP Phone. Messages
* across Skinny alter the display, ring the bell, and so on. Some messages
* request the establishment of a RTP connection, these messages are the ones
* which concern us for connection tracking.
*
* The TCP connection transfers Skinny protocol data units. These have
* a header followed by a few commands in a type-length-value format.
*
* Some commands contain IP addresses and RTP/UDP port numbers, these are:
* - RegisterMessage (phone IP address)
* - IpPortMessage (phone UDP port)
* - StartMediaTransmission (remote IP address, remote UDP port,
* IP precedence)
* - OpenReceiveChannelAck (IP address, port number)
*
* There are also matching commands which can mark an end to tracking some
* connections:
* - CloseReceiveChannel (paired with OpenRecieveChannelAck)
* - StopMediaTransmission (paired with StartMediaTransmission)
* these do not contain IP addresses but Conference IDs.
*
* KeepAlive messages are tracked so that running but idle connections
* do not time out.
*
* Cisco Systems' document "SCCP Messaging Guide for Cisco Unified
* Communications Manager 5.0(1)" suggests that these commands also
* establish RTP/UDP sessions, but I have no packet capture of these:
* - StationSubscribeDtmfPayloadReqMessage
* - StationUnSubscribeDtmfPayloadReqMessage
* If DTMF does not work via this module these messages are the likely
* cause and a packet capture in PCAP (tcpdump or wireshark) format
* would be appreciated by the maintainer.
*
*
* COPYRIGHT AND OTHER HASSLES
*
* Copyright © Glen David Turner of Semaphore, South Australia, 2008.
*
* This file is part of nf_skinny.
*
* nf_skinny is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License,
* or (at your option) any later version.
*
* nf_skinny is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with nf_skinny. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/init.h> /* For module_init/fini */
#include <linux/module.h>
#include <linux/stat.h> /* for S_* */
#include <linux/kernel.h> /* for printk() */
#include <asm/byteorder.h>
#include <linux/ctype.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <linux/tcp.h>
#include <linux/netfilter.h>
#include <net/netfilter/nf_conntrack.h>
#include <net/netfilter/nf_conntrack_tuple.h>
#include <net/netfilter/nf_conntrack_expect.h>
#include <net/netfilter/nf_conntrack_ecache.h>
#include <net/netfilter/nf_conntrack_helper.h>
#include "nf_conntrack_skinny.h"
#include <linux/moduleparam.h>
#define PFX "nf_conntrack_skinny: "
static unsigned int __read_mostly skinny_port = SKINNY_CONTROL_TCP_PORT;
module_param(skinny_port, uint, S_IRUGO);
MODULE_PARM_DESC(skinny_port,
"Well-known TCP port for Skinny control connection, "
"default " __MODULE_STRING(SKINNY_CONTROL_TCP_PORT));
static unsigned int __read_mostly skinny_timeout = SKINNY_TIMEOUT;
module_param(skinny_timeout, uint, S_IRUGO);
MODULE_PARM_DESC(skinny_timeout,
"Timeout for Skinny connection tracking, in seconds, "
"default "__MODULE_STRING(SKINNY_TIMEOUT));
static unsigned int __read_mostly skinny_max_expected = SKINNY_MAX_EXPECTED;
module_param(skinny_max_expected, uint, S_IRUGO);
MODULE_PARM_DESC(skinny_max_expected,
"Maximum Skinny control connections, one used per IP phone, "
"default " __MODULE_STRING(SKINNY_MAX_EXPECTED));
/* Access to this symbol is locked using RCU. */
unsigned int
(*nf_nat_skinny_hook)(struct sk_buff**pskb,
enum ip_conntrack_info ctinfo,
struct nf_conntrack_expect exp,
const char *dptr) __read_mostly;
EXPORT_SYMBOL_GPL(nf_nat_skinny_hook);
char *skinny_buffer = NULL;
static DEFINE_SPINLOCK(skinny_buffer_lock);
/* This error message is a hack. We don't want to blow lots of memory
* for strings literals that are 90% this same for this bizaare error
* so the string literals are made to be identical at compile time.
*
* Usage:
* printk_no_skb_header_pointer(__func__);
*/
static void
printk_no_skb_header_pointer(const char *func)
{
if (likely(net_ratelimit())) {
printk(KERN_ERR PFX
"%s(): Packet contents missing.\n",
func);
}
}
/* Take string "str" of allocated size "length", possibly not
* 0-terminated, possibly filled to the right with trailing spaces,
* NULs and other non-printable trash. Turn this into a printable
* 0-terminated string with no trailing spaces. This may truncate the
* last character, regardless if it is a space or not.
*
* This allows us to print strings in the protocol without opening
* a covert channel attack on the Linux console (eg: someone setting
* their phone name to the ANSI codes to set the Return key to
* "rm -rf /").
*/
static void
str_tidy(char *str, size_t length)
{
char *p;
/* Ensure 0-termination. */
str[length] = '\0';
/* Zap non-printable characters */
for (p = str; *p != '\0'; p++) {
if (!isprint(*p)) {
*p = ' ';
}
}
/* Remove trailing spaces. */
while (p != str) {
if (isspace(*p)) {
*p = '\0';
} else {
break;
}
p--;
}
}
/* Parse a Station Register message.
*
*
* Returns: !0: do not parse further PDUs in this packet.
*/
static int
parse_station_register(struct sk_buff *matching_skb,
unsigned int offset,
unsigned int end_offset,
struct nf_conn *ct,
enum ip_conntrack_info ct_info)
{
struct skinny_station_register *station_register;
struct skinny_station_register station_register_buffer;
char device_name[SKINNY_STATION_REGISTER_DEVICE_NAME_LENGTH];
printk("parse_station_register\n");
printk("parse_station_register() offset = %u\n", offset);
printk("parse_station_register() end_offset = %u\n", end_offset);
if (offset + sizeof(struct skinny_station_register) > end_offset) {
if (net_ratelimit()) {
printk(KERN_INFO PFX
"Station Register message is too short "
"(it is %u-%u bytes, but should be at least %zu).\n",
end_offset,
offset,
sizeof(struct skinny_station_register));
}
return 0;
}
station_register = skb_header_pointer(matching_skb,
offset,
sizeof(struct skinny_station_register),
&station_register_buffer);
if (!station_register) {
printk_no_skb_header_pointer(__func__);
return !0;
}
/* It's not clear what we can usefully do with this packet for
* connection tracking (as opposed to NAT). Archiving the
* device_name, instance and user would be very useful for user
* debugging and archiving the device_type is useful information
* should this module act oddly with a particular model of phone.
*/
memcpy(device_name,
station_register->device_name,
SKINNY_STATION_REGISTER_DEVICE_NAME_LENGTH);
str_tidy(device_name, SKINNY_STATION_REGISTER_DEVICE_NAME_LENGTH);
printk(KERN_INFO PFX
"User %u on station %s, instance %u, model %u at " NIPQUAD_FMT
" is registering to " NIPQUAD_FMT ".\n",
le32_to_cpu(station_register->station_user_id),
device_name,
le32_to_cpu(station_register->station_instance),
le32_to_cpu(station_register->device_type),
NIPQUAD(ct->tuplehash[CTINFO2DIR(ct_info)].tuple.src.u3),
NIPQUAD(ct->tuplehash[CTINFO2DIR(ct_info)].tuple.dst.u3));
return 0;
}
/* Parse a Station IP Port message.
*
* This message informs the Call Manager that the IP Phone has
* selected UDP port "udp_port" for incoming call-progress sounds.
*
* Might need to pass IP address to this to set up connection tracking.
*
* Returns: !0: do not parse further PDUs in this packet.
*/
static int
parse_station_ip_port(struct sk_buff *matching_skb,
unsigned int offset,
unsigned int end_offset,
struct nf_conn *ct,
enum ip_conntrack_info ct_info)
{
struct skinny_station_ip_port *station_ip_port;
struct skinny_station_ip_port station_ip_port_buffer;
struct nf_conntrack_expect *expect;
int problems;
enum ip_conntrack_dir direction = CTINFO2DIR(ct_info);
station_ip_port = skb_header_pointer(
matching_skb,
offset,
sizeof(struct skinny_station_ip_port),
&station_ip_port_buffer);
if (unlikely(!station_ip_port)) {
printk_no_skb_header_pointer(__func__);
return !0;
}
/* Establish an expectation that UDP traffic to the port in the
* StationIpPort protocol data unit will appear in the reverse
* direction to the Skinny control connection.
*/
expect = nf_ct_expect_alloc(ct);
if (!expect) {
return 0;
}
nf_ct_expect_init(expect,
ct->tuplehash[direction].tuple.src.l3num,
&ct->tuplehash[direction].tuple.dst.u3,
&ct->tuplehash[direction].tuple.src.u3,
IPPROTO_UDP,
NULL, /* Any UDP source port. */
&station_ip_port->udp_port);
expect->dir = !direction;
problems = nf_ct_expect_related(expect);
if (problems) {
if (net_ratelimit()) {
printk(KERN_INFO PFX "parse_station_ip_port(): "
"Request for connection tracking failed with error code %d "
"for ipv4:" NIPQUAD_FMT ",udp:* "
"--> ipv4:" NIPQUAD_FMT ",udp:%u.\n",
problems,
NIPQUAD(ct->tuplehash[direction].tuple.src.u3),
NIPQUAD(ct->tuplehash[direction].tuple.dst.u3),
ntohs(station_ip_port->udp_port));
}
}
/* The counterpart to nf_ct_expect_alloc(). */
nf_ct_expect_put(expect);
return 0;
}
/* Parse a Open Recieve Channel Acknowledge message.
*
* Returns: !0: do not parse further PDUs in this packet.
*/
static int
parse_open_receive_channel_ack(struct sk_buff *matching_skb,
unsigned int offset,
unsigned int length,
struct nf_conn *ct,
enum ip_conntrack_info ct_info)
{
struct skinny_open_receive_channel_ack *open_receive_channel_ack;
struct skinny_open_receive_channel_ack open_receive_channel_ack_buffer;
open_receive_channel_ack = skb_header_pointer(
matching_skb,
offset,
sizeof(struct skinny_open_receive_channel_ack),
&open_receive_channel_ack_buffer);
if (!open_receive_channel_ack) {
printk_no_skb_header_pointer(__func__);
return !0;
}
printk(KERN_INFO PFX
"open_receive_channel_ack\n");
/* Extract Port */
/* Set up conntrack */
return 0;
}
/* Parse a Start Media Transmission message.
*
* Returns: !0: do not parse further PDUs in this packet.
*/
static int
parse_start_media_transmission(struct sk_buff *matching_skb,
unsigned int offset,
unsigned int length,
struct nf_conn *ct,
enum ip_conntrack_info ct_info)
{
struct skinny_start_media_transmission *start_media_transmission;
struct skinny_start_media_transmission start_media_transmission_buffer;
struct nf_conntrack_expect *expect;
int problems;
enum ip_conntrack_dir direction = CTINFO2DIR(ct_info);
printk(KERN_INFO "start_media_transmission\n");
start_media_transmission = skb_header_pointer(
matching_skb,
offset,
sizeof(struct skinny_start_media_transmission),
&start_media_transmission_buffer);
if (!start_media_transmission) {
printk_no_skb_header_pointer(__func__);
return !0;
}
start_media_transmission = skb_header_pointer(
matching_skb,
offset,
sizeof(struct skinny_start_media_transmission),
&start_media_transmission_buffer);
if (unlikely(!start_media_transmission)) {
printk_no_skb_header_pointer(__func__);
return !0;
}
/* Establish an expectation that UDP traffic from (ip_address, *)
* to (remote_ip_address, remote_udp_port) will appear in the
* reverse direction to this message (ie, outgoing).
*/
expect = nf_ct_expect_alloc(ct);
if (!expect) {
return 0;
}
nf_ct_expect_init(expect,
ct->tuplehash[direction].tuple.src.l3num,
&ct->tuplehash[direction].tuple.src.u3,
(union nf_inet_addr *)&start_media_transmission->remote_ip_address,
IPPROTO_UDP,
NULL,
&start_media_transmission->remote_udp_port);
expect->dir = !direction;
problems = nf_ct_expect_related(expect);
if (problems) {
if (net_ratelimit()) {
printk(KERN_INFO PFX "parse_start_media_transmission(): "
"Request for connection tracking failed with error code %d "
"for ipv4:" NIPQUAD_FMT ",udp:* "
"--> ipv4:" NIPQUAD_FMT ",udp:%u.\n",
problems,
NIPQUAD(ct->tuplehash[direction].tuple.src.u3),
NIPQUAD(ct->tuplehash[direction].tuple.dst.u3),
ntohs(start_media_transmission->remote_udp_port));
}
}
/* The counterpart to nf_ct_expect_alloc(). */
nf_ct_expect_put(expect);
return 0;
}
/* Parse a Skinny protocol data unit.
* Input:
* matching_skb
* skinny_offset
* skinny_length
* direction
* Output:
* updated skinny_offset
* Returns: !0: do not parse further PDUs in this packet.
*/
static int
parse_skinny_pdu(struct sk_buff *matching_skb,
unsigned int *skinny_offset,
unsigned int skinny_length,
struct nf_conn *ct,
enum ip_conntrack_info ct_info)
{
struct skinny_tcp_msg_header *tcp_msg_header;
struct skinny_tcp_msg_header tcp_msg_header_buffer;
struct skinny_msg_id *msg_id_header;
struct skinny_msg_id msg_id_buffer;
unsigned int offset;
unsigned int end_offset;
unsigned int msg_len;
unsigned int msg_id;
int problems = 0;
offset = *skinny_offset;
end_offset = skinny_length;
/* Each Skinny PDU begins with a header named TcpMsgHeader. It
* contains a MsgLen (the number of bytes following this header)
* and a LinkMsgType (indicating compression, encryption and so
* on).
*
* Since this is reverse-engineered, we don't know if MsgLen can
* be validly 0 (from a conceptual point of view a zero-length
* Skinny PDU could be useful for keep alives). So we had better
* allow for that.
*
* LinkMsgType has only been reported with value 0. We punt on
* other values as trying to parse a compressed or encrypted
* packet isn't going to work.
*/
if (offset + sizeof(struct skinny_tcp_msg_header) > end_offset) {
printk(KERN_INFO PFX
"Too short for a Skinny packet "
"(it is %u-%u, but should be at least %zu). "
"Perhaps not Skinny traffic?\n",
end_offset,
offset,
sizeof(struct skinny_tcp_msg_header));
return !0;
}
tcp_msg_header = skb_header_pointer(matching_skb,
offset,
sizeof(struct skinny_tcp_msg_header),
&tcp_msg_header_buffer);
if (unlikely(!tcp_msg_header)) {
printk_no_skb_header_pointer(__func__);
return !0;
}
offset += sizeof(struct skinny_tcp_msg_header);
msg_len = le32_to_cpu(tcp_msg_header->msg_len);
/* Validate reasonableness of msg_len. */
if (offset + msg_len > end_offset) {
if (likely(net_ratelimit())) {
printk(KERN_ERR PFX
"Message length from Skinny header (%u bytes) is longer "
"than data in packet (%u-%u bytes). Perhaps not Skinny "
"traffic?\n",
msg_len,
end_offset,
offset);
}
/* If the message length is outrageous we don't know where
* this message ends and thus can't parse any following
* messages in the remainder of this packet.
*/
return !0;
}
/* End of PDU is now set by msg_len rather than packet length */
end_offset = offset + msg_len;
if (unlikely(le32_to_cpu(tcp_msg_header->link_msg_type)) !=
SKINNY_LINK_MSG_TYPE_PLAINTEXT) {
if (likely(net_ratelimit())) {
printk(KERN_INFO PFX
"Cannot analyse this message. It is not plaintext but has "
"Link Message Type %u. Perhaps not Skinny traffic? "
"Perhaps Skinny is using compression or encryption?\n",
le32_to_cpu(tcp_msg_header->link_msg_type));
}
/* Try to parse the next message in the packet. Perhaps not
* all messages are encrypted or compressed.
*/
*skinny_offset = end_offset;
return 0;
}
/* The msg_id identifies the purpose and structure of the
* message. We only need to parse some msg_id types to be
* able to track a connection.
*/
if (offset + sizeof(struct skinny_msg_id) > end_offset) {
if (likely(net_ratelimit())) {
printk(KERN_INFO PFX
"Message is too short to contain a MsgID "
"(it is %u-%u, but should be at least %zu).\n",
end_offset,
offset,
sizeof(struct skinny_msg_id));
}
/* Try to parse the rest of the packet. The protocol design
* seems to allow for message headers followed by no actual
* message.
*/
*skinny_offset = end_offset;
return 0;
}
msg_id_header = skb_header_pointer(matching_skb,
offset,
sizeof(struct skinny_msg_id),
&msg_id_buffer);
if (unlikely(!msg_id_header)) {
printk_no_skb_header_pointer(__func__);
return !0;
}
msg_id = le16_to_cpu(msg_id_header->msg_id);
offset += sizeof(struct skinny_msg_id);
switch (msg_id) {
case SKINNY_MSG_ID_STATION_REGISTER:
problems = parse_station_register(matching_skb,
offset,
end_offset,
ct,
ct_info);
break;
case SKINNY_MSG_ID_STATION_IP_PORT:
problems = parse_station_ip_port(matching_skb,
offset,
end_offset,
ct,
ct_info);
break;
case SKINNY_MSG_ID_OPEN_RECEIVE_CHANNEL_ACK:
problems = parse_open_receive_channel_ack(matching_skb,
offset,
end_offset,
ct,
ct_info);
break;
case SKINNY_MSG_ID_START_MEDIA_TRANSMISSION:
problems = parse_start_media_transmission(matching_skb,
offset,
end_offset,
ct,
ct_info);
break;
#if 0
case SKINNY_MSG_ID_KEEPALIVE:
/* Keep alive packets call nf_ct_refresh() so that connections
* don't time out. This allows the NAT timeout to be dropped
* to ~100s from 1 hour.
*/
break;
#endif
}
*skinny_offset = end_offset;
return problems;
}
/* Parse a packet containing Skinny PDUs.
* Input:
* matching_skb
* skinny_offset
* skinny_length
* direction
* Returns: accept or reject packet and all its PDUs.
*/
static int
parse_skinny_packet(struct sk_buff *matching_skb,
unsigned int skinny_offset,
unsigned int skinny_length,
struct nf_conn *ct,
enum ip_conntrack_info ct_info)
{
unsigned int pdu_offset;
int problems = 0;
/* The packet can contain one or more Skinny protocol data units.
* Accept the packet unless a subordinate parser says to toss it.
*/
pdu_offset = skinny_offset;
while (pdu_offset < skinny_length && !problems) {
problems = parse_skinny_pdu(matching_skb,
&pdu_offset,
skinny_length,
ct,
ct_info);
}
return NF_ACCEPT;
}
/* The Skinny TCP control connecction exchanges information about the
* establishment of RTP flows. This helper module tracks the TCP
* connection adding expected RTP flows to the connection tracking
* system.
*/
static int
skinny_conntrack_helper(struct sk_buff *matching_skb,
unsigned int matching_offset,
struct nf_conn *ct,
enum ip_conntrack_info conntrack_info)
{
struct tcphdr tcp_buffer,
*tcp_header;
int ret = NF_ACCEPT;
unsigned int skinny_offset;
/* Don't track connections until the TCP connection is fully
* established. TCP handshakes carry no application data.
*/
if (unlikely(conntrack_info != IP_CT_ESTABLISHED &&
conntrack_info != (IP_CT_ESTABLISHED + IP_CT_IS_REPLY))) {
return NF_ACCEPT;
}
/* Do we need to see if the TCP checksum is valid? If so, it goes
* here.
*/
/* Like most other conntrack modules we are too slack to
* handle non-linear SKBs.
*/
if (unlikely(skb_is_nonlinear(matching_skb))) {
return NF_ACCEPT;
}
/* Replace with finer RCU lock when data structure usage fully sorted. */
spin_lock_bh(&skinny_buffer_lock);
tcp_header = skb_header_pointer(matching_skb,
matching_offset,
sizeof(tcp_buffer),
&tcp_buffer);
if (unlikely(!tcp_header)) {
printk_no_skb_header_pointer(__func__);
ret = NF_ACCEPT;
goto unlock_end;
}
/* Skinny protocol data unit is after TCP header and its options.
* TCP stores the length of its header in .doff in units of 4-byte
* words.
*/
/* Is doff in network byte order? */
skinny_offset = matching_offset + tcp_header->doff * 4;
/* No data to parse? */
if (unlikely(skinny_offset == matching_skb->len)) {
ret = NF_ACCEPT;
goto unlock_end;
}
if (unlikely(skinny_offset > matching_skb->len)) {
if (likely(net_ratelimit())) {
printk(KERN_ERR PFX
"Unexpectedly short packet, perhaps not Skinny traffic? "
"Expected Skinny data to begin at byte %u, but packet "
"too short with only %u bytes.\n",
skinny_offset,
matching_skb->len);
}
ret = NF_ACCEPT;
goto unlock_end;
}
/* Do the heavy-duty Skinny parsing, setting up connection
* tracking if relevant protocol data units are encountered.
*/
ret = parse_skinny_packet(matching_skb,
skinny_offset,
matching_skb->len,
ct,
conntrack_info);
unlock_end:
spin_unlock_bh(&skinny_buffer_lock);
return ret;
}
static struct nf_conntrack_helper helper __read_mostly;
static int __init
nf_test_init(void)
{
int problems;
#if 1
/* "Hello world, this is John Laws".
* During development it's useful to know if the module binary in
* the kernel matches the module source in the editor. You'd be
* surprised how many head-scratching bugs are because they don't
* match.
*/
printk(KERN_INFO PFX
"Version $Id$ compiled on " __DATE__ " at " __TIME__ ".\n");
#endif
/* Validate parameters. */
if (skinny_max_expected < 1) {
printk(KERN_ERR PFX
"Module parameter skinny_max_expected must be 1 or more. "
"Abandoning module installation.\n");
return -EINVAL;
}
if (skinny_timeout < SKINNY_TIMEOUT_REASONABLE_MIN) {
printk(KERN_INFO PFX
"Module parameter skinny_timeout is perhaps too low at less "
"than %d seconds. Proceeeding regardless.\n",
SKINNY_TIMEOUT_REASONABLE_MIN);
}
/* Allocate resources.
* A non-NULL value for a resource is a flag that it should be
* freed upon module removal.
*/
skinny_buffer = kmalloc(0x10000, GFP_KERNEL);
if (!skinny_buffer) {
/* If they even see this message then the author of printk() has
* done fine work.
*/
printk(KERN_ERR PFX
"Insufficient kernel memory for 64KB packet parsing buffer. "
"Abandoning module installation.\n");
return -ENOMEM;
}
memset(&helper, 0, sizeof(struct nf_conntrack_helper));
/* Information about this conntrack helper. */
/* .name also indicates that this helper has been registered. */
helper.name = "skinny";
helper.me = THIS_MODULE;
helper.help = skinny_conntrack_helper;
helper.max_expected = skinny_max_expected;
helper.timeout = skinny_timeout;
/* Send all Skinny packets to this conntrack module. */
helper.tuple.src.l3num = AF_INET;
helper.tuple.dst.protonum = IPPROTO_TCP;
helper.tuple.src.u.tcp.port = htons(skinny_port);
/* Register this connection tracking helper. */
problems = nf_conntrack_helper_register(&helper);
if (problems) {
printk(KERN_WARNING PFX
"Failed to register connection tracking for TCP/IPv4 port %u. "
"Abandoning module installation.\n",
skinny_port);
/* Registration of this helper failed, unset .name to indicate that */
helper.name = NULL;
kfree(skinny_buffer);
skinny_buffer = NULL;
} else {
printk(KERN_INFO PFX
"Module installed with parameters skinny_port %u, "
"skinny_max_expected %u, "
"skinny_timeout %u.\n",
skinny_port,
skinny_max_expected,
skinny_timeout);
}
return problems;
}
static void __exit
nf_test_exit(void)
{
/* Free resources which are in use. */
if (helper.name) {
nf_conntrack_helper_unregister(&helper);
helper.name = NULL;
}
if (skinny_buffer) {
kfree(skinny_buffer);
skinny_buffer = NULL;
}
printk(KERN_INFO PFX "Module removed.\n");
}
module_init(nf_test_init);
module_exit(nf_test_exit);
MODULE_AUTHOR("Glen Turner <http://www.gdt.id.au/~gdt/>");
MODULE_DESCRIPTION("Cisco Skinny Station Protocol (formerly Selsius "
"Connection Control Protocol, SCCP) connection tracking");
MODULE_LICENSE("GPL");