]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/dccp/probe.c
Merge branch 'dccp' of git://eden-feed.erg.abdn.ac.uk/dccp_exp
[karo-tx-linux.git] / net / dccp / probe.c
1 /*
2  * dccp_probe - Observe the DCCP 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 DCCP from Stephen Hemminger's code
8  * Copyright (C) 2006, Ian McDonald <ian.mcdonald@jandi.co.nz>
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/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>
34
35 #include "dccp.h"
36 #include "ccid.h"
37 #include "ccids/ccid3.h"
38
39 static int port;
40
41 static int bufsize = 64 * 1024;
42
43 static const char procname[] = "dccpprobe";
44
45 static struct {
46         struct kfifo      *fifo;
47         spinlock_t        lock;
48         wait_queue_head_t wait;
49         ktime_t           start;
50 } dccpw;
51
52 static void jdccp_write_xmit(struct sock *sk)
53 {
54         const struct inet_sock *inet = inet_sk(sk);
55         struct ccid3_hc_tx_sock *hctx = NULL;
56         struct timespec tv;
57         char buf[256];
58         int len, ccid = ccid_get_current_tx_ccid(dccp_sk(sk));
59
60         if (ccid == DCCPC_CCID3)
61                 hctx = ccid3_hc_tx_sk(sk);
62
63         if (!port || ntohs(inet->dport) == port || ntohs(inet->sport) == port) {
64
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);
71
72                 if (hctx)
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);
77
78                 len += sprintf(buf + len, "\n");
79                 kfifo_put(dccpw.fifo, buf, len);
80                 wake_up(&dccpw.wait);
81         }
82
83         jprobe_return();
84 }
85
86 static struct jprobe dccp_send_probe = {
87         .kp     = {
88                 .symbol_name = "dccp_write_xmit",
89         },
90         .entry  = jdccp_write_xmit,
91 };
92
93 static int dccpprobe_open(struct inode *inode, struct file *file)
94 {
95         kfifo_reset(dccpw.fifo);
96         dccpw.start = ktime_get();
97         return 0;
98 }
99
100 static ssize_t dccpprobe_read(struct file *file, char __user *buf,
101                               size_t len, loff_t *ppos)
102 {
103         int error = 0, cnt = 0;
104         unsigned char *tbuf;
105
106         if (!buf)
107                 return -EINVAL;
108
109         if (len == 0)
110                 return 0;
111
112         tbuf = vmalloc(len);
113         if (!tbuf)
114                 return -ENOMEM;
115
116         error = wait_event_interruptible(dccpw.wait,
117                                          __kfifo_len(dccpw.fifo) != 0);
118         if (error)
119                 goto out_free;
120
121         cnt = kfifo_get(dccpw.fifo, tbuf, len);
122         error = copy_to_user(buf, tbuf, cnt) ? -EFAULT : 0;
123
124 out_free:
125         vfree(tbuf);
126
127         return error ? error : cnt;
128 }
129
130 static const struct file_operations dccpprobe_fops = {
131         .owner   = THIS_MODULE,
132         .open    = dccpprobe_open,
133         .read    = dccpprobe_read,
134 };
135
136 static __init int dccpprobe_init(void)
137 {
138         int ret = -ENOMEM;
139
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);
145
146         if (!proc_net_fops_create(&init_net, procname, S_IRUSR, &dccpprobe_fops))
147                 goto err0;
148
149         ret = register_jprobe(&dccp_send_probe);
150         if (ret)
151                 goto err1;
152
153         pr_info("DCCP watch registered (port=%d)\n", port);
154         return 0;
155 err1:
156         proc_net_remove(&init_net, procname);
157 err0:
158         kfifo_free(dccpw.fifo);
159         return ret;
160 }
161 module_init(dccpprobe_init);
162
163 static __exit void dccpprobe_exit(void)
164 {
165         kfifo_free(dccpw.fifo);
166         proc_net_remove(&init_net, procname);
167         unregister_jprobe(&dccp_send_probe);
168
169 }
170 module_exit(dccpprobe_exit);
171
172 MODULE_PARM_DESC(port, "Port to match (0=all)");
173 module_param(port, int, 0);
174
175 MODULE_PARM_DESC(bufsize, "Log buffer size (default 64k)");
176 module_param(bufsize, int, 0);
177
178 MODULE_AUTHOR("Ian McDonald <ian.mcdonald@jandi.co.nz>");
179 MODULE_DESCRIPTION("DCCP snooper");
180 MODULE_LICENSE("GPL");