Commit | Line | Data |
---|---|---|
787a51a0 WY |
1 | /* |
2 | * sctp_probe - Observe the SCTP flow with kprobes. | |
3 | * | |
4 | * The idea for this came from Werner Almesberger's umlsim | |
5 | * Copyright (C) 2004, Stephen Hemminger <shemminger@osdl.org> | |
6 | * | |
7 | * Modified for SCTP from Stephen Hemminger's code | |
8 | * Copyright (C) 2010, Wei Yongjun <yjwei@cn.fujitsu.com> | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | */ | |
24 | ||
25 | #include <linux/kernel.h> | |
26 | #include <linux/kprobes.h> | |
27 | #include <linux/socket.h> | |
28 | #include <linux/sctp.h> | |
29 | #include <linux/proc_fs.h> | |
f5460618 | 30 | #include <linux/vmalloc.h> |
787a51a0 WY |
31 | #include <linux/module.h> |
32 | #include <linux/kfifo.h> | |
33 | #include <linux/time.h> | |
34 | #include <net/net_namespace.h> | |
35 | ||
36 | #include <net/sctp/sctp.h> | |
37 | #include <net/sctp/sm.h> | |
38 | ||
39 | MODULE_AUTHOR("Wei Yongjun <yjwei@cn.fujitsu.com>"); | |
40 | MODULE_DESCRIPTION("SCTP snooper"); | |
41 | MODULE_LICENSE("GPL"); | |
42 | ||
43 | static int port __read_mostly = 0; | |
44 | MODULE_PARM_DESC(port, "Port to match (0=all)"); | |
45 | module_param(port, int, 0); | |
46 | ||
47 | static int bufsize __read_mostly = 64 * 1024; | |
48 | MODULE_PARM_DESC(bufsize, "Log buffer size (default 64k)"); | |
49 | module_param(bufsize, int, 0); | |
50 | ||
51 | static int full __read_mostly = 1; | |
52 | MODULE_PARM_DESC(full, "Full log (1=every ack packet received, 0=only cwnd changes)"); | |
53 | module_param(full, int, 0); | |
54 | ||
55 | static const char procname[] = "sctpprobe"; | |
56 | ||
57 | static struct { | |
58 | struct kfifo fifo; | |
59 | spinlock_t lock; | |
60 | wait_queue_head_t wait; | |
61 | struct timespec tstart; | |
62 | } sctpw; | |
63 | ||
64 | static void printl(const char *fmt, ...) | |
65 | { | |
66 | va_list args; | |
67 | int len; | |
68 | char tbuf[256]; | |
69 | ||
70 | va_start(args, fmt); | |
71 | len = vscnprintf(tbuf, sizeof(tbuf), fmt, args); | |
72 | va_end(args); | |
73 | ||
74 | kfifo_in_locked(&sctpw.fifo, tbuf, len, &sctpw.lock); | |
75 | wake_up(&sctpw.wait); | |
76 | } | |
77 | ||
78 | static int sctpprobe_open(struct inode *inode, struct file *file) | |
79 | { | |
80 | kfifo_reset(&sctpw.fifo); | |
81 | getnstimeofday(&sctpw.tstart); | |
82 | ||
83 | return 0; | |
84 | } | |
85 | ||
86 | static ssize_t sctpprobe_read(struct file *file, char __user *buf, | |
87 | size_t len, loff_t *ppos) | |
88 | { | |
89 | int error = 0, cnt = 0; | |
90 | unsigned char *tbuf; | |
91 | ||
92 | if (!buf) | |
93 | return -EINVAL; | |
94 | ||
95 | if (len == 0) | |
96 | return 0; | |
97 | ||
98 | tbuf = vmalloc(len); | |
99 | if (!tbuf) | |
100 | return -ENOMEM; | |
101 | ||
102 | error = wait_event_interruptible(sctpw.wait, | |
103 | kfifo_len(&sctpw.fifo) != 0); | |
104 | if (error) | |
105 | goto out_free; | |
106 | ||
107 | cnt = kfifo_out_locked(&sctpw.fifo, tbuf, len, &sctpw.lock); | |
108 | error = copy_to_user(buf, tbuf, cnt) ? -EFAULT : 0; | |
109 | ||
110 | out_free: | |
111 | vfree(tbuf); | |
112 | ||
113 | return error ? error : cnt; | |
114 | } | |
115 | ||
116 | static const struct file_operations sctpprobe_fops = { | |
117 | .owner = THIS_MODULE, | |
118 | .open = sctpprobe_open, | |
119 | .read = sctpprobe_read, | |
120 | }; | |
121 | ||
122 | sctp_disposition_t jsctp_sf_eat_sack(const struct sctp_endpoint *ep, | |
123 | const struct sctp_association *asoc, | |
124 | const sctp_subtype_t type, | |
125 | void *arg, | |
126 | sctp_cmd_seq_t *commands) | |
127 | { | |
128 | struct sctp_transport *sp; | |
129 | static __u32 lcwnd = 0; | |
130 | struct timespec now; | |
131 | ||
132 | sp = asoc->peer.primary_path; | |
133 | ||
134 | if ((full || sp->cwnd != lcwnd) && | |
135 | (!port || asoc->peer.port == port || | |
136 | ep->base.bind_addr.port == port)) { | |
137 | lcwnd = sp->cwnd; | |
138 | ||
139 | getnstimeofday(&now); | |
140 | now = timespec_sub(now, sctpw.tstart); | |
141 | ||
142 | printl("%lu.%06lu ", (unsigned long) now.tv_sec, | |
143 | (unsigned long) now.tv_nsec / NSEC_PER_USEC); | |
144 | ||
145 | printl("%p %5d %5d %5d %8d %5d ", asoc, | |
146 | ep->base.bind_addr.port, asoc->peer.port, | |
147 | asoc->pathmtu, asoc->peer.rwnd, asoc->unack_data); | |
148 | ||
149 | list_for_each_entry(sp, &asoc->peer.transport_addr_list, | |
150 | transports) { | |
151 | if (sp == asoc->peer.primary_path) | |
152 | printl("*"); | |
153 | ||
154 | if (sp->ipaddr.sa.sa_family == AF_INET) | |
155 | printl("%pI4 ", &sp->ipaddr.v4.sin_addr); | |
156 | else | |
157 | printl("%pI6 ", &sp->ipaddr.v6.sin6_addr); | |
158 | ||
159 | printl("%2u %8u %8u %8u %8u %8u ", | |
160 | sp->state, sp->cwnd, sp->ssthresh, | |
161 | sp->flight_size, sp->partial_bytes_acked, | |
162 | sp->pathmtu); | |
163 | } | |
164 | printl("\n"); | |
165 | } | |
166 | ||
167 | jprobe_return(); | |
168 | return 0; | |
169 | } | |
170 | ||
171 | static struct jprobe sctp_recv_probe = { | |
172 | .kp = { | |
173 | .symbol_name = "sctp_sf_eat_sack_6_2", | |
174 | }, | |
175 | .entry = jsctp_sf_eat_sack, | |
176 | }; | |
177 | ||
178 | static __init int sctpprobe_init(void) | |
179 | { | |
180 | int ret = -ENOMEM; | |
181 | ||
182 | init_waitqueue_head(&sctpw.wait); | |
183 | spin_lock_init(&sctpw.lock); | |
184 | if (kfifo_alloc(&sctpw.fifo, bufsize, GFP_KERNEL)) | |
185 | return ret; | |
186 | ||
187 | if (!proc_net_fops_create(&init_net, procname, S_IRUSR, | |
188 | &sctpprobe_fops)) | |
189 | goto free_kfifo; | |
190 | ||
191 | ret = register_jprobe(&sctp_recv_probe); | |
192 | if (ret) | |
193 | goto remove_proc; | |
194 | ||
195 | pr_info("SCTP probe registered (port=%d)\n", port); | |
196 | ||
197 | return 0; | |
198 | ||
199 | remove_proc: | |
200 | proc_net_remove(&init_net, procname); | |
201 | free_kfifo: | |
202 | kfifo_free(&sctpw.fifo); | |
203 | return ret; | |
204 | } | |
205 | ||
206 | static __exit void sctpprobe_exit(void) | |
207 | { | |
208 | kfifo_free(&sctpw.fifo); | |
209 | proc_net_remove(&init_net, procname); | |
210 | unregister_jprobe(&sctp_recv_probe); | |
211 | } | |
212 | ||
213 | module_init(sctpprobe_init); | |
214 | module_exit(sctpprobe_exit); |