1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#include <linux/module.h>
37#include <linux/tty.h>
38#include <linux/sched.h>
39#include <asm/unaligned.h>
40#include <linux/slab.h>
41#include <linux/proc_fs.h>
42#include <linux/uaccess.h>
43
44#include "dgrp_common.h"
45
46
47static int dgrp_mon_open(struct inode *, struct file *);
48static int dgrp_mon_release(struct inode *, struct file *);
49static ssize_t dgrp_mon_read(struct file *, char __user *, size_t, loff_t *);
50static long dgrp_mon_ioctl(struct file *file, unsigned int cmd,
51 unsigned long arg);
52
53const struct file_operations dgrp_mon_ops = {
54 .owner = THIS_MODULE,
55 .read = dgrp_mon_read,
56 .unlocked_ioctl = dgrp_mon_ioctl,
57 .open = dgrp_mon_open,
58 .release = dgrp_mon_release,
59};
60
61
62
63
64
65
66
67
68static int dgrp_mon_open(struct inode *inode, struct file *file)
69{
70 struct nd_struct *nd;
71 struct timeval tv;
72 uint32_t time;
73 u8 *buf;
74 int rtn;
75
76 rtn = try_module_get(THIS_MODULE);
77 if (!rtn)
78 return -ENXIO;
79
80 rtn = 0;
81
82 if (!capable(CAP_SYS_ADMIN)) {
83 rtn = -EPERM;
84 goto done;
85 }
86
87
88
89
90 if (file->private_data) {
91 rtn = -EINVAL;
92 goto done;
93 }
94
95
96
97
98 nd = PDE_DATA(inode);
99 if (!nd) {
100 rtn = -ENXIO;
101 goto done;
102 }
103
104 file->private_data = (void *) nd;
105
106
107
108
109
110
111
112
113 down(&nd->nd_mon_semaphore);
114
115 if (nd->nd_mon_buf) {
116 rtn = -EBUSY;
117 goto done_up;
118 }
119
120 nd->nd_mon_buf = kmalloc(MON_MAX, GFP_KERNEL);
121
122 if (!nd->nd_mon_buf) {
123 rtn = -ENOMEM;
124 goto done_up;
125 }
126
127
128
129
130
131 buf = nd->nd_mon_buf;
132
133 strcpy(buf, RPDUMP_MAGIC);
134 buf += strlen(buf) + 1;
135
136 do_gettimeofday(&tv);
137
138
139
140
141
142
143 time = (uint32_t) (tv.tv_sec & 0xffffffff);
144
145 put_unaligned_be32(time, buf);
146 put_unaligned_be16(0, buf + 4);
147 buf += 6;
148
149 if (nd->nd_tx_module) {
150 buf[0] = RPDUMP_CLIENT;
151 put_unaligned_be32(0, buf + 1);
152 put_unaligned_be16(1, buf + 5);
153 buf[7] = 0xf0 + nd->nd_tx_module;
154 buf += 8;
155 }
156
157 if (nd->nd_rx_module) {
158 buf[0] = RPDUMP_SERVER;
159 put_unaligned_be32(0, buf + 1);
160 put_unaligned_be16(1, buf + 5);
161 buf[7] = 0xf0 + nd->nd_rx_module;
162 buf += 8;
163 }
164
165 nd->nd_mon_out = 0;
166 nd->nd_mon_in = buf - nd->nd_mon_buf;
167 nd->nd_mon_lbolt = jiffies;
168
169done_up:
170 up(&nd->nd_mon_semaphore);
171
172done:
173 if (rtn)
174 module_put(THIS_MODULE);
175 return rtn;
176}
177
178
179
180
181
182
183
184static int dgrp_mon_release(struct inode *inode, struct file *file)
185{
186 struct nd_struct *nd;
187
188
189
190
191 nd = (struct nd_struct *)(file->private_data);
192 if (!nd)
193 goto done;
194
195
196
197
198
199 down(&nd->nd_mon_semaphore);
200
201 kfree(nd->nd_mon_buf);
202 nd->nd_mon_buf = NULL;
203 nd->nd_mon_out = nd->nd_mon_in;
204
205
206
207
208
209 if (nd->nd_mon_flag & MON_WAIT_SPACE) {
210 nd->nd_mon_flag &= ~MON_WAIT_SPACE;
211 wake_up_interruptible(&nd->nd_mon_wqueue);
212 }
213
214 up(&nd->nd_mon_semaphore);
215
216
217
218
219 down(&nd->nd_net_semaphore);
220 up(&nd->nd_net_semaphore);
221
222done:
223 module_put(THIS_MODULE);
224 file->private_data = NULL;
225 return 0;
226}
227
228
229
230
231static ssize_t dgrp_mon_read(struct file *file, char __user *buf, size_t count,
232 loff_t *ppos)
233{
234 struct nd_struct *nd;
235 int r;
236 int offset = 0;
237 int res = 0;
238 ssize_t rtn;
239
240
241
242
243 nd = (struct nd_struct *)(file->private_data);
244 if (!nd)
245 return -ENXIO;
246
247
248
249
250
251 down(&nd->nd_mon_semaphore);
252
253 for (;;) {
254 res = (nd->nd_mon_in - nd->nd_mon_out) & MON_MASK;
255
256 if (res)
257 break;
258
259 nd->nd_mon_flag |= MON_WAIT_DATA;
260
261 up(&nd->nd_mon_semaphore);
262
263
264
265
266 rtn = wait_event_interruptible(nd->nd_mon_wqueue,
267 ((nd->nd_mon_flag & MON_WAIT_DATA) == 0));
268
269 if (rtn)
270 return rtn;
271
272 down(&nd->nd_mon_semaphore);
273 }
274
275
276
277
278
279 if (res > count)
280 res = count;
281
282 r = MON_MAX - nd->nd_mon_out;
283
284 if (r <= res) {
285 rtn = copy_to_user((void __user *)buf,
286 nd->nd_mon_buf + nd->nd_mon_out, r);
287 if (rtn) {
288 up(&nd->nd_mon_semaphore);
289 return -EFAULT;
290 }
291
292 nd->nd_mon_out = 0;
293 res -= r;
294 offset = r;
295 }
296
297 rtn = copy_to_user((void __user *) buf + offset,
298 nd->nd_mon_buf + nd->nd_mon_out, res);
299 if (rtn) {
300 up(&nd->nd_mon_semaphore);
301 return -EFAULT;
302 }
303
304 nd->nd_mon_out += res;
305
306 *ppos += res;
307
308 up(&nd->nd_mon_semaphore);
309
310
311
312
313
314 if (nd->nd_mon_flag & MON_WAIT_SPACE) {
315 nd->nd_mon_flag &= ~MON_WAIT_SPACE;
316 wake_up_interruptible(&nd->nd_mon_wqueue);
317 }
318
319 return res;
320}
321
322
323static long dgrp_mon_ioctl(struct file *file, unsigned int cmd,
324 unsigned long arg)
325{
326 return -EINVAL;
327}
328