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
|
/*
* trelay.c: Trivial Ethernet Relay
*
* Copyright (C) 2012 Felix Fietkau <nbd@nbd.name>
*
* 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.
*/
#include <linux/module.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/netdevice.h>
#include <linux/rtnetlink.h>
#include <linux/debugfs.h>
static LIST_HEAD(trelay_devs);
static struct dentry *debugfs_dir;
struct trelay {
struct list_head list;
struct net_device *dev1, *dev2;
struct dentry *debugfs;
int to_remove;
char name[];
};
rx_handler_result_t trelay_handle_frame(struct sk_buff **pskb)
{
struct net_device *dev;
struct sk_buff *skb = *pskb;
dev = rcu_dereference(skb->dev->rx_handler_data);
if (!dev)
return RX_HANDLER_PASS;
if (skb->protocol == htons(ETH_P_PAE))
return RX_HANDLER_PASS;
skb_push(skb, ETH_HLEN);
skb->dev = dev;
skb_forward_csum(skb);
dev_queue_xmit(skb);
return RX_HANDLER_CONSUMED;
}
static int trelay_open(struct inode *inode, struct file *file)
{
file->private_data = inode->i_private;
return 0;
}
static int trelay_do_remove(struct trelay *tr)
{
list_del(&tr->list);
/* First and before all, ensure that the debugfs file is removed
* to prevent dangling pointer in file->private_data */
debugfs_remove_recursive(tr->debugfs);
dev_put(tr->dev1);
dev_put(tr->dev2);
netdev_rx_handler_unregister(tr->dev1);
netdev_rx_handler_unregister(tr->dev2);
kfree(tr);
return 0;
}
static struct trelay *trelay_find(struct net_device *dev)
{
struct trelay *tr;
list_for_each_entry(tr, &trelay_devs, list) {
if (tr->dev1 == dev || tr->dev2 == dev)
return tr;
}
return NULL;
}
static int tr_device_event(struct notifier_block *unused, unsigned long event,
void *ptr)
{
struct net_device *dev = netdev_notifier_info_to_dev(ptr);
struct trelay *tr;
if (event != NETDEV_UNREGISTER)
goto out;
tr = trelay_find(dev);
if (!tr)
goto out;
trelay_do_remove(tr);
out:
return NOTIFY_DONE;
}
static ssize_t trelay_remove_write(struct file *file, const char __user *ubuf,
size_t count, loff_t *ppos)
{
struct trelay *tr = file->private_data;
tr->to_remove = 1;
return count;
}
static int trelay_remove_release(struct inode *inode, struct file *file)
{
struct trelay *tr, *tmp;
/* This is the only file op that is called outside debugfs_use_file_*()
* context which means that: (1) this file can be removed and
* (2) file->private_data may no longer be valid */
rtnl_lock();
list_for_each_entry_safe(tr, tmp, &trelay_devs, list)
if (tr->to_remove)
trelay_do_remove(tr);
rtnl_unlock();
return 0;
}
static const struct file_operations fops_remove = {
.owner = THIS_MODULE,
.open = trelay_open,
.write = trelay_remove_write,
.llseek = default_llseek,
.release = trelay_remove_release,
};
static int trelay_do_add(char *name, char *devn1, char *devn2)
{
struct net_device *dev1, *dev2;
struct trelay *tr, *tr1;
int ret;
tr = kzalloc(sizeof(*tr) + strlen(name) + 1, GFP_KERNEL);
if (!tr)
return -ENOMEM;
rtnl_lock();
rcu_read_lock();
ret = -EEXIST;
list_for_each_entry(tr1, &trelay_devs, list) {
if (!strcmp(tr1->name, name))
goto out;
}
ret = -ENOENT;
dev1 = dev_get_by_name_rcu(&init_net, devn1);
dev2 = dev_get_by_name_rcu(&init_net, devn2);
if (!dev1 || !dev2)
goto out;
ret = netdev_rx_handler_register(dev1, trelay_handle_frame, dev2);
if (ret < 0)
goto out;
ret = netdev_rx_handler_register(dev2, trelay_handle_frame, dev1);
if (ret < 0) {
netdev_rx_handler_unregister(dev1);
goto out;
}
dev_hold(dev1);
dev_hold(dev2);
strcpy(tr->name, name);
tr->dev1 = dev1;
tr->dev2 = dev2;
list_add_tail(&tr->list, &trelay_devs);
tr->debugfs = debugfs_create_dir(name, debugfs_dir);
debugfs_create_file("remove", S_IWUSR, tr->debugfs, tr, &fops_remove);
ret = 0;
out:
rcu_read_unlock();
rtnl_unlock();
if (ret < 0)
kfree(tr);
return ret;
}
static ssize_t trelay_add_write(struct file *file, const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[256];
char *dev1, *dev2, *tmp;
ssize_t len, ret;
len = min(count, sizeof(buf) - 1);
if (copy_from_user(buf, ubuf, len))
return -EFAULT;
buf[len] = 0;
if ((tmp = strchr(buf, '\n')))
*tmp = 0;
dev1 = strchr(buf, ',');
if (!dev1)
return -EINVAL;
*(dev1++) = 0;
dev2 = strchr(dev1, ',');
if (!dev2)
return -EINVAL;
*(dev2++) = 0;
if (strchr(dev2, ','))
return -EINVAL;
if (!strlen(buf) || !strlen(dev1) || !strlen(dev2))
return -EINVAL;
ret = trelay_do_add(buf, dev1, dev2);
if (ret < 0)
return ret;
return count;
}
static const struct file_operations fops_add = {
.owner = THIS_MODULE,
.write = trelay_add_write,
.llseek = default_llseek,
};
static struct notifier_block tr_dev_notifier = {
.notifier_call = tr_device_event
};
static int __init trelay_init(void)
{
int ret;
debugfs_dir = debugfs_create_dir("trelay", NULL);
if (!debugfs_dir)
return -ENOMEM;
debugfs_create_file("add", S_IWUSR, debugfs_dir, NULL, &fops_add);
ret = register_netdevice_notifier(&tr_dev_notifier);
if (ret < 0)
goto error;
return 0;
error:
debugfs_remove_recursive(debugfs_dir);
return ret;
}
static void __exit trelay_exit(void)
{
struct trelay *tr, *tmp;
unregister_netdevice_notifier(&tr_dev_notifier);
rtnl_lock();
list_for_each_entry_safe(tr, tmp, &trelay_devs, list)
trelay_do_remove(tr);
rtnl_unlock();
debugfs_remove_recursive(debugfs_dir);
}
module_init(trelay_init);
module_exit(trelay_exit);
MODULE_LICENSE("GPL");
|