hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/***
 *
 *  rtcfg/rtcfg_ioctl.c
 *
 *  Real-Time Configuration Distribution Protocol
 *
 *  Copyright (C) 2003-2005 Jan Kiszka <jan.kiszka@web.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */
 
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/vmalloc.h>
 
#include <rtcfg_chrdev.h>
#include <rtnet_rtpc.h>
#include <rtcfg/rtcfg_conn_event.h>
#include <rtcfg/rtcfg_event.h>
#include <rtcfg/rtcfg_frame.h>
#include <rtcfg/rtcfg_proc.h>
 
int rtcfg_event_handler(struct rt_proc_call *call)
{
   struct rtcfg_cmd *cmd_event;
 
   cmd_event = rtpc_get_priv(call, struct rtcfg_cmd);
   return rtcfg_do_main_event(cmd_event->internal.data.ifindex,
                  cmd_event->internal.data.event_id, call);
}
 
void keep_cmd_add(struct rt_proc_call *call, void *priv_data)
{
   /* do nothing on error (<0), or if file already present (=0) */
   if (rtpc_get_result(call) <= 0)
       return;
 
   /* Don't cleanup any buffers, we are going to recycle them! */
   rtpc_set_cleanup_handler(call, NULL);
}
 
void cleanup_cmd_add(void *priv_data)
{
   struct rtcfg_cmd *cmd = (struct rtcfg_cmd *)priv_data;
   void *buf;
 
   /* unlock proc and update directory structure */
   rtcfg_unlockwr_proc(cmd->internal.data.ifindex);
 
   buf = cmd->args.add.conn_buf;
   if (buf != NULL)
       kfree(buf);
 
   buf = cmd->args.add.stage1_data;
   if (buf != NULL)
       kfree(buf);
 
   if (cmd->args.add.stage2_file != NULL) {
       buf = cmd->args.add.stage2_file->buffer;
       if (buf != NULL)
           vfree(buf);
       kfree(cmd->args.add.stage2_file);
   }
}
 
void cleanup_cmd_del(void *priv_data)
{
   struct rtcfg_cmd *cmd = (struct rtcfg_cmd *)priv_data;
   void *buf;
 
   /* unlock proc and update directory structure */
   rtcfg_unlockwr_proc(cmd->internal.data.ifindex);
 
   if (cmd->args.del.conn_buf != NULL) {
       buf = cmd->args.del.conn_buf->stage1_data;
       if (buf != NULL)
           kfree(buf);
       kfree(cmd->args.del.conn_buf);
   }
 
   if (cmd->args.del.stage2_file != NULL) {
       buf = cmd->args.del.stage2_file->buffer;
       if (buf != NULL)
           vfree(buf);
       kfree(cmd->args.del.stage2_file);
   }
}
 
void copy_stage_1_data(struct rt_proc_call *call, void *priv_data)
{
   struct rtcfg_cmd *cmd;
   int result = rtpc_get_result(call);
 
   if (result <= 0)
       return;
 
   cmd = rtpc_get_priv(call, struct rtcfg_cmd);
 
   if (cmd->args.client.buffer_size < (size_t)result)
       rtpc_set_result(call, -ENOSPC);
   else if (copy_to_user(cmd->args.client.buffer,
                 cmd->args.client.rtskb->data, result) != 0)
       rtpc_set_result(call, -EFAULT);
}
 
void cleanup_cmd_client(void *priv_data)
{
   struct rtcfg_cmd *cmd = (struct rtcfg_cmd *)priv_data;
   void *station_buf;
   struct rtskb *rtskb;
 
   station_buf = cmd->args.client.station_buf;
   if (station_buf != NULL)
       kfree(station_buf);
 
   rtskb = cmd->args.client.rtskb;
   if (rtskb != NULL)
       kfree_rtskb(rtskb);
}
 
void copy_stage_2_data(struct rt_proc_call *call, void *priv_data)
{
   struct rtcfg_cmd *cmd;
   int result = rtpc_get_result(call);
   struct rtskb *rtskb;
 
   if (result <= 0)
       return;
 
   cmd = rtpc_get_priv(call, struct rtcfg_cmd);
 
   if (cmd->args.announce.buffer_size < (size_t)result)
       rtpc_set_result(call, -ENOSPC);
   else {
       rtskb = cmd->args.announce.rtskb;
       do {
           if (copy_to_user(cmd->args.announce.buffer, rtskb->data,
                    rtskb->len) != 0) {
               rtpc_set_result(call, -EFAULT);
               break;
           }
           cmd->args.announce.buffer += rtskb->len;
           rtskb = rtskb->next;
       } while (rtskb != NULL);
   }
}
 
void cleanup_cmd_announce(void *priv_data)
{
   struct rtcfg_cmd *cmd = (struct rtcfg_cmd *)priv_data;
   struct rtskb *rtskb;
 
   rtskb = cmd->args.announce.rtskb;
   if (rtskb != NULL)
       kfree_rtskb(rtskb);
}
 
void cleanup_cmd_detach(void *priv_data)
{
   struct rtcfg_cmd *cmd = (struct rtcfg_cmd *)priv_data;
   void *buf;
 
   /* unlock proc and update directory structure */
   rtcfg_unlockwr_proc(cmd->internal.data.ifindex);
 
   if (cmd->args.detach.conn_buf) {
       buf = cmd->args.detach.conn_buf->stage1_data;
       if (buf != NULL)
           kfree(buf);
       kfree(cmd->args.detach.conn_buf);
   }
 
   if (cmd->args.detach.stage2_file != NULL) {
       buf = cmd->args.detach.stage2_file->buffer;
       if (buf)
           vfree(buf);
       kfree(cmd->args.detach.stage2_file);
   }
 
   if (cmd->args.detach.station_addr_list)
       kfree(cmd->args.detach.station_addr_list);
 
   if (cmd->args.detach.stage2_chain)
       kfree_rtskb(cmd->args.detach.stage2_chain);
}
 
static int load_cfg_file(struct rtcfg_file *cfgfile, struct rtcfg_cmd *cmd)
{
   size_t file_size = 0;
   struct file *filp;
   loff_t i_size;
   int ret;
 
   filp = filp_open(cfgfile->name, O_RDONLY, 0);
   if (IS_ERR(filp))
       return PTR_ERR(filp);
 
   i_size = i_size_read(file_inode(filp));
   if (i_size <= 0) {
       /* allocate buffer even for empty files */
       cfgfile->buffer = vmalloc(1);
   } else {
       cfgfile->buffer = NULL; /* Leave allocation to the kernel. */
       ret = read_file_from_kernel(filp, &cfgfile->buffer,
                   i_size_read(file_inode(filp)),
                   &file_size, READING_UNKNOWN);
       if (ret < 0) {
           fput(filp);
           return ret;
       }
   }
 
   fput(filp);
   cfgfile->size = file_size;
 
   /* dispatch again, this time with new file attached */
   return rtpc_dispatch_call(rtcfg_event_handler, 0, cmd,
               sizeof(*cmd), NULL, cleanup_cmd_add);
}
 
int rtcfg_ioctl_add(struct rtnet_device *rtdev, struct rtcfg_cmd *cmd)
{
   struct rtcfg_connection *conn_buf;
   struct rtcfg_file *file = NULL;
   void *data_buf;
   size_t size;
   int ret;
 
   conn_buf = kmalloc(sizeof(struct rtcfg_connection), GFP_KERNEL);
   if (conn_buf == NULL)
       return -ENOMEM;
   cmd->args.add.conn_buf = conn_buf;
 
   data_buf = NULL;
   size = cmd->args.add.stage1_size;
   if (size > 0) {
       /* check stage 1 data size */
       if (sizeof(struct rtcfg_frm_stage_1_cfg) +
               2 * RTCFG_ADDRSIZE_IP + size >
           rtdev->get_mtu(rtdev, RTCFG_SKB_PRIO)) {
           ret = -ESTAGE1SIZE;
           goto err;
       }
 
       data_buf = kmalloc(size, GFP_KERNEL);
       if (data_buf == NULL) {
           ret = -ENOMEM;
           goto err;
       }
 
       ret = copy_from_user(data_buf, cmd->args.add.stage1_data, size);
       if (ret != 0) {
           ret = -EFAULT;
           goto err;
       }
   }
   cmd->args.add.stage1_data = data_buf;
 
   if (cmd->args.add.stage2_filename != NULL) {
       size = strnlen_user(cmd->args.add.stage2_filename, PATH_MAX);
 
       file = kmalloc(sizeof(struct rtcfg_file) + size, GFP_KERNEL);
       if (file == NULL) {
           ret = -ENOMEM;
           goto err;
       }
 
       file->name = (char *)file + sizeof(struct rtcfg_file);
       file->buffer = NULL;
 
       ret = copy_from_user(
           (void *)file + sizeof(struct rtcfg_file),
           (const void *)cmd->args.add.stage2_filename, size);
       if (ret != 0) {
           ret = -EFAULT;
           goto err;
       }
   }
   cmd->args.add.stage2_file = file;
 
   /* lock proc structure for modification */
   rtcfg_lockwr_proc(cmd->internal.data.ifindex);
 
   ret = rtpc_dispatch_call(rtcfg_event_handler, 0, cmd, sizeof(*cmd),
                keep_cmd_add, cleanup_cmd_add);
 
   /* load file if missing */
   if (ret > 0) {
       ret = load_cfg_file(file, cmd);
       if (ret) {
           rtcfg_unlockwr_proc(cmd->internal.data.ifindex);
           goto err;
       }
   }
 
   return ret;
 
err:
   kfree(conn_buf);
   if (data_buf != NULL)
       kfree(data_buf);
   if (file != NULL) {
       if (file->buffer != NULL)
           vfree(file->buffer);
       kfree(file);
   }
   return ret;
}
 
int rtcfg_ioctl(struct rtnet_device *rtdev, unsigned int request,
       unsigned long arg)
{
   struct rtcfg_cmd cmd;
   struct rtcfg_station *station_buf;
   int ret;
 
   ret = copy_from_user(&cmd, (void *)arg, sizeof(cmd));
   if (ret != 0)
       return -EFAULT;
 
   cmd.internal.data.ifindex = rtdev->ifindex;
   cmd.internal.data.event_id = _IOC_NR(request);
 
   switch (request) {
   case RTCFG_IOC_SERVER:
       ret = rtpc_dispatch_call(rtcfg_event_handler, 0, &cmd,
                    sizeof(cmd), NULL, NULL);
       break;
 
   case RTCFG_IOC_ADD:
       ret = rtcfg_ioctl_add(rtdev, &cmd);
       break;
 
   case RTCFG_IOC_DEL:
       cmd.args.del.conn_buf = NULL;
       cmd.args.del.stage2_file = NULL;
 
       /* lock proc structure for modification
               (unlock in cleanup_cmd_del) */
       rtcfg_lockwr_proc(cmd.internal.data.ifindex);
 
       ret = rtpc_dispatch_call(rtcfg_event_handler, 0, &cmd,
                    sizeof(cmd), NULL, cleanup_cmd_del);
       break;
 
   case RTCFG_IOC_WAIT:
       ret = rtpc_dispatch_call(rtcfg_event_handler,
                    cmd.args.wait.timeout, &cmd,
                    sizeof(cmd), NULL, NULL);
       break;
 
   case RTCFG_IOC_CLIENT:
       station_buf = kmalloc(sizeof(struct rtcfg_station) *
                         cmd.args.client.max_stations,
                     GFP_KERNEL);
       if (station_buf == NULL)
           return -ENOMEM;
       cmd.args.client.station_buf = station_buf;
       cmd.args.client.rtskb = NULL;
 
       ret = rtpc_dispatch_call(rtcfg_event_handler,
                    cmd.args.client.timeout, &cmd,
                    sizeof(cmd), copy_stage_1_data,
                    cleanup_cmd_client);
       break;
 
   case RTCFG_IOC_ANNOUNCE:
       cmd.args.announce.rtskb = NULL;
 
       ret = rtpc_dispatch_call(rtcfg_event_handler,
                    cmd.args.announce.timeout, &cmd,
                    sizeof(cmd), copy_stage_2_data,
                    cleanup_cmd_announce);
       break;
 
   case RTCFG_IOC_READY:
       ret = rtpc_dispatch_call(rtcfg_event_handler,
                    cmd.args.ready.timeout, &cmd,
                    sizeof(cmd), NULL, NULL);
       break;
 
   case RTCFG_IOC_DETACH:
       do {
           cmd.args.detach.conn_buf = NULL;
           cmd.args.detach.stage2_file = NULL;
           cmd.args.detach.station_addr_list = NULL;
           cmd.args.detach.stage2_chain = NULL;
 
           /* lock proc structure for modification
                   (unlock in cleanup_cmd_detach) */
           rtcfg_lockwr_proc(cmd.internal.data.ifindex);
 
           ret = rtpc_dispatch_call(rtcfg_event_handler, 0, &cmd,
                        sizeof(cmd), NULL,
                        cleanup_cmd_detach);
       } while (ret == -EAGAIN);
       break;
 
   default:
       ret = -ENOTTY;
   }
 
   return ret;
}
 
struct rtnet_ioctls rtcfg_ioctls = { .service_name = "RTcfg",
                    .ioctl_type = RTNET_IOC_TYPE_RTCFG,
                    .handler = rtcfg_ioctl };