2 * dccp_probe - Observe the DCCP 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 DCCP from Stephen Hemminger's code
8 * Copyright (C) 2006, Ian McDonald <ian.mcdonald@jandi.co.nz>
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 #include <linux/kernel.h>
26 #include <linux/kprobes.h>
27 #include <linux/socket.h>
28 #include <linux/dccp.h>
29 #include <linux/proc_fs.h>
30 #include <linux/module.h>
31 #include <linux/kfifo.h>
32 #include <linux/vmalloc.h>
33 #include <net/net_namespace.h>
37 #include "ccids/ccid3.h"
41 static int bufsize = 64 * 1024;
43 static const char procname[] = "dccpprobe";
48 wait_queue_head_t wait;
52 static void jdccp_write_xmit(struct sock *sk)
54 const struct inet_sock *inet = inet_sk(sk);
55 struct ccid3_hc_tx_sock *hctx = NULL;
58 int len, ccid = ccid_get_current_tx_ccid(dccp_sk(sk));
60 if (ccid == DCCPC_CCID3)
61 hctx = ccid3_hc_tx_sk(sk);
63 if (!port || ntohs(inet->dport) == port || ntohs(inet->sport) == port) {
65 tv = ktime_to_timespec(ktime_sub(ktime_get(), dccpw.start));
66 len = sprintf(buf, "%lu.%09lu %d.%d.%d.%d:%u %d.%d.%d.%d:%u %d",
67 (unsigned long)tv.tv_sec,
68 (unsigned long)tv.tv_nsec,
69 NIPQUAD(inet->saddr), ntohs(inet->sport),
70 NIPQUAD(inet->daddr), ntohs(inet->dport), ccid);
73 len += sprintf(buf + len, " %d %d %d %u %u %u %d",
74 hctx->s, hctx->rtt, hctx->p, hctx->x_calc,
75 (unsigned)(hctx->x_recv >> 6),
76 (unsigned)(hctx->x >> 6), hctx->t_ipi);
78 len += sprintf(buf + len, "\n");
79 kfifo_put(dccpw.fifo, buf, len);
86 static struct jprobe dccp_send_probe = {
88 .symbol_name = "dccp_write_xmit",
90 .entry = jdccp_write_xmit,
93 static int dccpprobe_open(struct inode *inode, struct file *file)
95 kfifo_reset(dccpw.fifo);
96 dccpw.start = ktime_get();
100 static ssize_t dccpprobe_read(struct file *file, char __user *buf,
101 size_t len, loff_t *ppos)
103 int error = 0, cnt = 0;
116 error = wait_event_interruptible(dccpw.wait,
117 __kfifo_len(dccpw.fifo) != 0);
121 cnt = kfifo_get(dccpw.fifo, tbuf, len);
122 error = copy_to_user(buf, tbuf, cnt) ? -EFAULT : 0;
127 return error ? error : cnt;
130 static const struct file_operations dccpprobe_fops = {
131 .owner = THIS_MODULE,
132 .open = dccpprobe_open,
133 .read = dccpprobe_read,
136 static __init int dccpprobe_init(void)
140 init_waitqueue_head(&dccpw.wait);
141 spin_lock_init(&dccpw.lock);
142 dccpw.fifo = kfifo_alloc(bufsize, GFP_KERNEL, &dccpw.lock);
143 if (IS_ERR(dccpw.fifo))
144 return PTR_ERR(dccpw.fifo);
146 if (!proc_net_fops_create(&init_net, procname, S_IRUSR, &dccpprobe_fops))
149 ret = register_jprobe(&dccp_send_probe);
153 pr_info("DCCP watch registered (port=%d)\n", port);
156 proc_net_remove(&init_net, procname);
158 kfifo_free(dccpw.fifo);
161 module_init(dccpprobe_init);
163 static __exit void dccpprobe_exit(void)
165 kfifo_free(dccpw.fifo);
166 proc_net_remove(&init_net, procname);
167 unregister_jprobe(&dccp_send_probe);
170 module_exit(dccpprobe_exit);
172 MODULE_PARM_DESC(port, "Port to match (0=all)");
173 module_param(port, int, 0);
175 MODULE_PARM_DESC(bufsize, "Log buffer size (default 64k)");
176 module_param(bufsize, int, 0);
178 MODULE_AUTHOR("Ian McDonald <ian.mcdonald@jandi.co.nz>");
179 MODULE_DESCRIPTION("DCCP snooper");
180 MODULE_LICENSE("GPL");