]>
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 | ||
145ce502 JP |
25 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
26 | ||
787a51a0 WY |
27 | #include <linux/kernel.h> |
28 | #include <linux/kprobes.h> | |
29 | #include <linux/socket.h> | |
30 | #include <linux/sctp.h> | |
31 | #include <linux/proc_fs.h> | |
f5460618 | 32 | #include <linux/vmalloc.h> |
787a51a0 WY |
33 | #include <linux/module.h> |
34 | #include <linux/kfifo.h> | |
35 | #include <linux/time.h> | |
36 | #include <net/net_namespace.h> | |
37 | ||
38 | #include <net/sctp/sctp.h> | |
39 | #include <net/sctp/sm.h> | |
40 | ||
41 | MODULE_AUTHOR("Wei Yongjun <yjwei@cn.fujitsu.com>"); | |
42 | MODULE_DESCRIPTION("SCTP snooper"); | |
43 | MODULE_LICENSE("GPL"); | |
44 | ||
45 | static int port __read_mostly = 0; | |
46 | MODULE_PARM_DESC(port, "Port to match (0=all)"); | |
47 | module_param(port, int, 0); | |
48 | ||
b1b72076 DB |
49 | static unsigned int fwmark __read_mostly = 0; |
50 | MODULE_PARM_DESC(fwmark, "skb mark to match (0=no mark)"); | |
51 | module_param(fwmark, uint, 0); | |
52 | ||
787a51a0 WY |
53 | static int bufsize __read_mostly = 64 * 1024; |
54 | MODULE_PARM_DESC(bufsize, "Log buffer size (default 64k)"); | |
55 | module_param(bufsize, int, 0); | |
56 | ||
57 | static int full __read_mostly = 1; | |
58 | MODULE_PARM_DESC(full, "Full log (1=every ack packet received, 0=only cwnd changes)"); | |
59 | module_param(full, int, 0); | |
60 | ||
61 | static const char procname[] = "sctpprobe"; | |
62 | ||
63 | static struct { | |
64 | struct kfifo fifo; | |
65 | spinlock_t lock; | |
66 | wait_queue_head_t wait; | |
67 | struct timespec tstart; | |
68 | } sctpw; | |
69 | ||
be3e4581 | 70 | static __printf(1, 2) void printl(const char *fmt, ...) |
787a51a0 WY |
71 | { |
72 | va_list args; | |
73 | int len; | |
74 | char tbuf[256]; | |
75 | ||
76 | va_start(args, fmt); | |
77 | len = vscnprintf(tbuf, sizeof(tbuf), fmt, args); | |
78 | va_end(args); | |
79 | ||
80 | kfifo_in_locked(&sctpw.fifo, tbuf, len, &sctpw.lock); | |
81 | wake_up(&sctpw.wait); | |
82 | } | |
83 | ||
84 | static int sctpprobe_open(struct inode *inode, struct file *file) | |
85 | { | |
86 | kfifo_reset(&sctpw.fifo); | |
87 | getnstimeofday(&sctpw.tstart); | |
88 | ||
89 | return 0; | |
90 | } | |
91 | ||
92 | static ssize_t sctpprobe_read(struct file *file, char __user *buf, | |
93 | size_t len, loff_t *ppos) | |
94 | { | |
95 | int error = 0, cnt = 0; | |
96 | unsigned char *tbuf; | |
97 | ||
98 | if (!buf) | |
99 | return -EINVAL; | |
100 | ||
101 | if (len == 0) | |
102 | return 0; | |
103 | ||
104 | tbuf = vmalloc(len); | |
105 | if (!tbuf) | |
106 | return -ENOMEM; | |
107 | ||
108 | error = wait_event_interruptible(sctpw.wait, | |
109 | kfifo_len(&sctpw.fifo) != 0); | |
110 | if (error) | |
111 | goto out_free; | |
112 | ||
113 | cnt = kfifo_out_locked(&sctpw.fifo, tbuf, len, &sctpw.lock); | |
114 | error = copy_to_user(buf, tbuf, cnt) ? -EFAULT : 0; | |
115 | ||
116 | out_free: | |
117 | vfree(tbuf); | |
118 | ||
119 | return error ? error : cnt; | |
120 | } | |
121 | ||
122 | static const struct file_operations sctpprobe_fops = { | |
123 | .owner = THIS_MODULE, | |
124 | .open = sctpprobe_open, | |
125 | .read = sctpprobe_read, | |
6038f373 | 126 | .llseek = noop_llseek, |
787a51a0 WY |
127 | }; |
128 | ||
1e558174 DB |
129 | static sctp_disposition_t jsctp_sf_eat_sack(struct net *net, |
130 | const struct sctp_endpoint *ep, | |
131 | const struct sctp_association *asoc, | |
132 | const sctp_subtype_t type, | |
133 | void *arg, | |
134 | sctp_cmd_seq_t *commands) | |
787a51a0 | 135 | { |
b1b72076 DB |
136 | struct sctp_chunk *chunk = arg; |
137 | struct sk_buff *skb = chunk->skb; | |
787a51a0 WY |
138 | struct sctp_transport *sp; |
139 | static __u32 lcwnd = 0; | |
140 | struct timespec now; | |
141 | ||
142 | sp = asoc->peer.primary_path; | |
143 | ||
b1b72076 DB |
144 | if (((port == 0 && fwmark == 0) || |
145 | asoc->peer.port == port || | |
146 | ep->base.bind_addr.port == port || | |
147 | (fwmark > 0 && skb->mark == fwmark)) && | |
148 | (full || sp->cwnd != lcwnd)) { | |
787a51a0 WY |
149 | lcwnd = sp->cwnd; |
150 | ||
151 | getnstimeofday(&now); | |
152 | now = timespec_sub(now, sctpw.tstart); | |
153 | ||
154 | printl("%lu.%06lu ", (unsigned long) now.tv_sec, | |
155 | (unsigned long) now.tv_nsec / NSEC_PER_USEC); | |
156 | ||
157 | printl("%p %5d %5d %5d %8d %5d ", asoc, | |
158 | ep->base.bind_addr.port, asoc->peer.port, | |
159 | asoc->pathmtu, asoc->peer.rwnd, asoc->unack_data); | |
160 | ||
161 | list_for_each_entry(sp, &asoc->peer.transport_addr_list, | |
162 | transports) { | |
163 | if (sp == asoc->peer.primary_path) | |
164 | printl("*"); | |
165 | ||
05f147ef DB |
166 | printl("%pISc %2u %8u %8u %8u %8u %8u ", |
167 | &sp->ipaddr, sp->state, sp->cwnd, sp->ssthresh, | |
787a51a0 WY |
168 | sp->flight_size, sp->partial_bytes_acked, |
169 | sp->pathmtu); | |
170 | } | |
171 | printl("\n"); | |
172 | } | |
173 | ||
174 | jprobe_return(); | |
175 | return 0; | |
176 | } | |
177 | ||
178 | static struct jprobe sctp_recv_probe = { | |
179 | .kp = { | |
180 | .symbol_name = "sctp_sf_eat_sack_6_2", | |
181 | }, | |
182 | .entry = jsctp_sf_eat_sack, | |
183 | }; | |
184 | ||
185 | static __init int sctpprobe_init(void) | |
186 | { | |
187 | int ret = -ENOMEM; | |
188 | ||
22222997 DB |
189 | /* Warning: if the function signature of sctp_sf_eat_sack_6_2, |
190 | * has been changed, you also have to change the signature of | |
191 | * jsctp_sf_eat_sack, otherwise you end up right here! | |
192 | */ | |
193 | BUILD_BUG_ON(__same_type(sctp_sf_eat_sack_6_2, | |
194 | jsctp_sf_eat_sack) == 0); | |
195 | ||
787a51a0 WY |
196 | init_waitqueue_head(&sctpw.wait); |
197 | spin_lock_init(&sctpw.lock); | |
198 | if (kfifo_alloc(&sctpw.fifo, bufsize, GFP_KERNEL)) | |
199 | return ret; | |
200 | ||
d4beaa66 G |
201 | if (!proc_create(procname, S_IRUSR, init_net.proc_net, |
202 | &sctpprobe_fops)) | |
787a51a0 WY |
203 | goto free_kfifo; |
204 | ||
205 | ret = register_jprobe(&sctp_recv_probe); | |
206 | if (ret) | |
207 | goto remove_proc; | |
208 | ||
b1b72076 DB |
209 | pr_info("probe registered (port=%d/fwmark=%u) bufsize=%u\n", |
210 | port, fwmark, bufsize); | |
787a51a0 WY |
211 | return 0; |
212 | ||
213 | remove_proc: | |
ece31ffd | 214 | remove_proc_entry(procname, init_net.proc_net); |
787a51a0 WY |
215 | free_kfifo: |
216 | kfifo_free(&sctpw.fifo); | |
217 | return ret; | |
218 | } | |
219 | ||
220 | static __exit void sctpprobe_exit(void) | |
221 | { | |
222 | kfifo_free(&sctpw.fifo); | |
ece31ffd | 223 | remove_proc_entry(procname, init_net.proc_net); |
787a51a0 WY |
224 | unregister_jprobe(&sctp_recv_probe); |
225 | } | |
226 | ||
227 | module_init(sctpprobe_init); | |
228 | module_exit(sctpprobe_exit); |