rtnet_chrdev.c 5.86 KB
Newer Older
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
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
/***
 *
 *  stack/rtnet_chrdev.c - implements char device for management interface
 *
 *  Copyright (C) 1999       Lineo, Inc
 *                1999, 2002 David A. Schleef <ds@schleef.org>
 *                2002       Ulrich Marx <marx@fet.uni-hannover.de>
 *                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 version 2 of the GNU General Public License as
 *  published by the Free Software Foundation.
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/if_arp.h>
#include <linux/kmod.h>
#include <linux/miscdevice.h>
#include <linux/netdevice.h>
#include <linux/spinlock.h>

#include <rtnet_chrdev.h>
#include <rtnet_internal.h>
#include <ipv4/route.h>


static DEFINE_SPINLOCK(ioctl_handler_lock);
static LIST_HEAD(ioctl_handlers);

static long rtnet_ioctl(struct file *file,
			unsigned int request, unsigned long arg)
{
    struct rtnet_ioctl_head head;
    struct rtnet_device     *rtdev = NULL;
    struct rtnet_ioctls     *ioctls;
    struct list_head        *entry;
    int                     ret;


    if (!capable(CAP_SYS_ADMIN))
52
	return -EPERM;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
53
54
55

    ret = copy_from_user(&head, (void *)arg, sizeof(head));
    if (ret != 0)
56
	return -EFAULT;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
57
58
59
60

    spin_lock(&ioctl_handler_lock);

    list_for_each(entry, &ioctl_handlers) {
61
	ioctls = list_entry(entry, struct rtnet_ioctls, entry);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
62

63
64
	if (ioctls->ioctl_type == _IOC_TYPE(request)) {
	    atomic_inc(&ioctls->ref_count);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
65

66
	    spin_unlock(&ioctl_handler_lock);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
67

68
69
70
71
72
73
74
	    if ((_IOC_NR(request) & RTNET_IOC_NODEV_PARAM) == 0) {
		rtdev = rtdev_get_by_name(head.if_name);
		if (!rtdev) {
		    atomic_dec(&ioctls->ref_count);
		    return -ENODEV;
		}
	    }
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
75

76
	    ret = ioctls->handler(rtdev, request, arg);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
77

78
79
80
	    if (rtdev)
		rtdev_dereference(rtdev);
	    atomic_dec(&ioctls->ref_count);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
81

82
83
	    return ret;
	}
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
84
85
86
87
88
89
90
91
    }

    spin_unlock(&ioctl_handler_lock);

    return -ENOTTY;
}

static int rtnet_core_ioctl(struct rtnet_device *rtdev, unsigned int request,
92
			    unsigned long arg)
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
93
94
95
96
97
98
99
{
    struct rtnet_core_cmd   cmd;
    int                     ret;


    ret = copy_from_user(&cmd, (void *)arg, sizeof(cmd));
    if (ret != 0)
100
	return -EFAULT;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
101
102

    switch (request) {
103
	case IOC_RT_IFUP:
104
	    ret = rtdev_up(rtdev, &cmd);
105
106
107
	    break;

	case IOC_RT_IFDOWN:
108
	    ret = rtdev_down(rtdev);
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
	    break;

	case IOC_RT_IFINFO:
	    if (cmd.args.info.ifindex > 0)
		rtdev = rtdev_get_by_index(cmd.args.info.ifindex);
	    else
		rtdev = rtdev_get_by_name(cmd.head.if_name);
	    if (rtdev == NULL)
		return -ENODEV;

	    if (mutex_lock_interruptible(&rtdev->nrt_lock)) {
		rtdev_dereference(rtdev);
		return -ERESTARTSYS;
	    }

	    memcpy(cmd.head.if_name, rtdev->name, IFNAMSIZ);
	    cmd.args.info.ifindex      = rtdev->ifindex;
	    cmd.args.info.type         = rtdev->type;
	    cmd.args.info.ip_addr      = rtdev->local_ip;
	    cmd.args.info.broadcast_ip = rtdev->broadcast_ip;
	    cmd.args.info.mtu          = rtdev->mtu;
	    cmd.args.info.flags        = rtdev->flags;
131
132
133
134
135
136
137
            if ((cmd.args.info.flags & IFF_UP)
		    && (rtdev->link_state
			    & (RTNET_LINK_STATE_PRESENT
				    | RTNET_LINK_STATE_NOCARRIER))
                    == RTNET_LINK_STATE_PRESENT)
                    cmd.args.info.flags |= IFF_RUNNING;

138
139
140
141
142
	    memcpy(cmd.args.info.dev_addr, rtdev->dev_addr, MAX_ADDR_LEN);

	    mutex_unlock(&rtdev->nrt_lock);

	    rtdev_dereference(rtdev);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
143

144
145
146
	    if (copy_to_user((void *)arg, &cmd, sizeof(cmd)) != 0)
		return -EFAULT;
	    break;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
147

148
149
	default:
	    ret = -ENOTTY;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    }

    return ret;
}



int rtnet_register_ioctls(struct rtnet_ioctls *ioctls)
{
    struct list_head    *entry;
    struct rtnet_ioctls *registered_ioctls;


    RTNET_ASSERT(ioctls->handler != NULL, return -EINVAL;);

    spin_lock(&ioctl_handler_lock);

    list_for_each(entry, &ioctl_handlers) {
168
169
170
171
172
	registered_ioctls = list_entry(entry, struct rtnet_ioctls, entry);
	if (registered_ioctls->ioctl_type == ioctls->ioctl_type) {
	    spin_unlock(&ioctl_handler_lock);
	    return -EEXIST;
	}
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
    }

    list_add_tail(&ioctls->entry, &ioctl_handlers);
    atomic_set(&ioctls->ref_count, 0);

    spin_unlock(&ioctl_handler_lock);

    return 0;
}



void rtnet_unregister_ioctls(struct rtnet_ioctls *ioctls)
{
    spin_lock(&ioctl_handler_lock);

    while (atomic_read(&ioctls->ref_count) != 0) {
190
	spin_unlock(&ioctl_handler_lock);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
191

192
193
	set_current_state(TASK_UNINTERRUPTIBLE);
	schedule_timeout(1*HZ); /* wait a second */
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
194

195
	spin_lock(&ioctl_handler_lock);
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
196
197
198
199
200
201
202
203
204
205
    }

    list_del(&ioctls->entry);

    spin_unlock(&ioctl_handler_lock);
}



static struct file_operations rtnet_fops = {
206
207
    .owner = THIS_MODULE,
    .unlocked_ioctl = rtnet_ioctl,
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
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
};

static struct miscdevice rtnet_chr_misc_dev = {
    .minor= RTNET_MINOR,
    .name = "rtnet",
    .fops = &rtnet_fops,
};

static struct rtnet_ioctls core_ioctls = {
    .service_name = "RTnet Core",
    .ioctl_type =   RTNET_IOC_TYPE_CORE,
    .handler =      rtnet_core_ioctl
};



/**
 * rtnet_chrdev_init -
 *
 */
int __init rtnet_chrdev_init(void)
{
    int err;

    err = misc_register(&rtnet_chr_misc_dev);
    if (err) {
234
235
236
	printk("RTnet: unable to register rtnet management device/class "
	       "(error %d)\n", err);
	return err;
Gilles Chanteperdrix's avatar
Gilles Chanteperdrix committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
    }

    rtnet_register_ioctls(&core_ioctls);
    return 0;
}



/**
 * rtnet_chrdev_release -
 *
 */
void rtnet_chrdev_release(void)
{
    misc_deregister(&rtnet_chr_misc_dev);
}


255
256
EXPORT_SYMBOL_GPL(rtnet_register_ioctls);
EXPORT_SYMBOL_GPL(rtnet_unregister_ioctls);