2 * sctp_probe - Observe the SCTP flow with kprobes.
4 * The idea for this came from Werner Almesberger's umlsim
5 * Copyright (C) 2004, Stephen Hemminger <shemminger@osdl.org>
7 * Modified for SCTP from Stephen Hemminger's code
8 * Copyright (C) 2010, Wei Yongjun <yjwei@cn.fujitsu.com>
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.
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.
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.
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
32 #include <linux/vmalloc.h>
33 #include <linux/module.h>
34 #include <linux/kfifo.h>
35 #include <linux/time.h>
36 #include <net/net_namespace.h>
38 #include <net/sctp/sctp.h>
39 #include <net/sctp/sm.h>
41 MODULE_AUTHOR("Wei Yongjun <yjwei@cn.fujitsu.com>");
42 MODULE_DESCRIPTION("SCTP snooper");
43 MODULE_LICENSE("GPL");
45 static int port __read_mostly = 0;
46 MODULE_PARM_DESC(port, "Port to match (0=all)");
47 module_param(port, int, 0);
49 static int bufsize __read_mostly = 64 * 1024;
50 MODULE_PARM_DESC(bufsize, "Log buffer size (default 64k)");
51 module_param(bufsize, int, 0);
53 static int full __read_mostly = 1;
54 MODULE_PARM_DESC(full, "Full log (1=every ack packet received, 0=only cwnd changes)");
55 module_param(full, int, 0);
57 static const char procname[] = "sctpprobe";
62 wait_queue_head_t wait;
63 struct timespec tstart;
66 static void printl(const char *fmt, ...)
73 len = vscnprintf(tbuf, sizeof(tbuf), fmt, args);
76 kfifo_in_locked(&sctpw.fifo, tbuf, len, &sctpw.lock);
80 static int sctpprobe_open(struct inode *inode, struct file *file)
82 kfifo_reset(&sctpw.fifo);
83 getnstimeofday(&sctpw.tstart);
88 static ssize_t sctpprobe_read(struct file *file, char __user *buf,
89 size_t len, loff_t *ppos)
91 int error = 0, cnt = 0;
104 error = wait_event_interruptible(sctpw.wait,
105 kfifo_len(&sctpw.fifo) != 0);
109 cnt = kfifo_out_locked(&sctpw.fifo, tbuf, len, &sctpw.lock);
110 error = copy_to_user(buf, tbuf, cnt) ? -EFAULT : 0;
115 return error ? error : cnt;
118 static const struct file_operations sctpprobe_fops = {
119 .owner = THIS_MODULE,
120 .open = sctpprobe_open,
121 .read = sctpprobe_read,
122 .llseek = noop_llseek,
125 sctp_disposition_t jsctp_sf_eat_sack(const struct sctp_endpoint *ep,
126 const struct sctp_association *asoc,
127 const sctp_subtype_t type,
129 sctp_cmd_seq_t *commands)
131 struct sctp_transport *sp;
132 static __u32 lcwnd = 0;
135 sp = asoc->peer.primary_path;
137 if ((full || sp->cwnd != lcwnd) &&
138 (!port || asoc->peer.port == port ||
139 ep->base.bind_addr.port == port)) {
142 getnstimeofday(&now);
143 now = timespec_sub(now, sctpw.tstart);
145 printl("%lu.%06lu ", (unsigned long) now.tv_sec,
146 (unsigned long) now.tv_nsec / NSEC_PER_USEC);
148 printl("%p %5d %5d %5d %8d %5d ", asoc,
149 ep->base.bind_addr.port, asoc->peer.port,
150 asoc->pathmtu, asoc->peer.rwnd, asoc->unack_data);
152 list_for_each_entry(sp, &asoc->peer.transport_addr_list,
154 if (sp == asoc->peer.primary_path)
157 if (sp->ipaddr.sa.sa_family == AF_INET)
158 printl("%pI4 ", &sp->ipaddr.v4.sin_addr);
160 printl("%pI6 ", &sp->ipaddr.v6.sin6_addr);
162 printl("%2u %8u %8u %8u %8u %8u ",
163 sp->state, sp->cwnd, sp->ssthresh,
164 sp->flight_size, sp->partial_bytes_acked,
174 static struct jprobe sctp_recv_probe = {
176 .symbol_name = "sctp_sf_eat_sack_6_2",
178 .entry = jsctp_sf_eat_sack,
181 static __init int sctpprobe_init(void)
185 init_waitqueue_head(&sctpw.wait);
186 spin_lock_init(&sctpw.lock);
187 if (kfifo_alloc(&sctpw.fifo, bufsize, GFP_KERNEL))
190 if (!proc_net_fops_create(&init_net, procname, S_IRUSR,
194 ret = register_jprobe(&sctp_recv_probe);
198 pr_info("probe registered (port=%d)\n", port);
203 proc_net_remove(&init_net, procname);
205 kfifo_free(&sctpw.fifo);
209 static __exit void sctpprobe_exit(void)
211 kfifo_free(&sctpw.fifo);
212 proc_net_remove(&init_net, procname);
213 unregister_jprobe(&sctp_recv_probe);
216 module_init(sctpprobe_init);
217 module_exit(sctpprobe_exit);