1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * "LAPB via ethernet" driver release 001
4 *
5 * This code REQUIRES 2.1.15 or higher/ NET3.038
6 *
7 * This is a "pseudo" network driver to allow LAPB over Ethernet.
8 *
9 * This driver can use any ethernet destination address, and can be
10 * limited to accept frames from one dedicated ethernet card only.
11 *
12 * History
13 * LAPBETH 001 Jonathan Naylor Cloned from bpqether.c
14 * 2000-10-29 Henner Eisen lapb_data_indication() return status.
15 * 2000-11-14 Henner Eisen dev_hold/put, NETDEV_GOING_DOWN support
16 */
17
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20 #include <linux/errno.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/in.h>
24 #include <linux/slab.h>
25 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <linux/net.h>
28 #include <linux/inet.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/skbuff.h>
32 #include <net/sock.h>
33 #include <linux/uaccess.h>
34 #include <linux/mm.h>
35 #include <linux/interrupt.h>
36 #include <linux/notifier.h>
37 #include <linux/stat.h>
38 #include <linux/module.h>
39 #include <linux/lapb.h>
40 #include <linux/init.h>
41
42 #include <net/x25device.h>
43
44 static const u8 bcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
45
46 /* If this number is made larger, check that the temporary string buffer
47 * in lapbeth_new_device is large enough to store the probe device name.*/
48 #define MAXLAPBDEV 100
49
50 struct lapbethdev {
51 struct list_head node;
52 struct net_device *ethdev; /* link to ethernet device */
53 struct net_device *axdev; /* lapbeth device (lapb#) */
54 bool up;
55 spinlock_t up_lock; /* Protects "up" */
56 };
57
58 static LIST_HEAD(lapbeth_devices);
59
60 /* ------------------------------------------------------------------------ */
61
62 /*
63 * Get the LAPB device for the ethernet device
64 */
lapbeth_get_x25_dev(struct net_device * dev)65 static struct lapbethdev *lapbeth_get_x25_dev(struct net_device *dev)
66 {
67 struct lapbethdev *lapbeth;
68
69 list_for_each_entry_rcu(lapbeth, &lapbeth_devices, node, lockdep_rtnl_is_held()) {
70 if (lapbeth->ethdev == dev)
71 return lapbeth;
72 }
73 return NULL;
74 }
75
dev_is_ethdev(struct net_device * dev)76 static __inline__ int dev_is_ethdev(struct net_device *dev)
77 {
78 return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
79 }
80
81 /* ------------------------------------------------------------------------ */
82
83 /*
84 * Receive a LAPB frame via an ethernet interface.
85 */
lapbeth_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * ptype,struct net_device * orig_dev)86 static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
87 {
88 int len, err;
89 struct lapbethdev *lapbeth;
90
91 if (dev_net(dev) != &init_net)
92 goto drop;
93
94 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
95 return NET_RX_DROP;
96
97 if (!pskb_may_pull(skb, 2))
98 goto drop;
99
100 rcu_read_lock();
101 lapbeth = lapbeth_get_x25_dev(dev);
102 if (!lapbeth)
103 goto drop_unlock_rcu;
104 spin_lock_bh(&lapbeth->up_lock);
105 if (!lapbeth->up)
106 goto drop_unlock;
107
108 len = skb->data[0] + skb->data[1] * 256;
109 dev->stats.rx_packets++;
110 dev->stats.rx_bytes += len;
111
112 skb_pull(skb, 2); /* Remove the length bytes */
113 skb_trim(skb, len); /* Set the length of the data */
114
115 if ((err = lapb_data_received(lapbeth->axdev, skb)) != LAPB_OK) {
116 printk(KERN_DEBUG "lapbether: lapb_data_received err - %d\n", err);
117 goto drop_unlock;
118 }
119 out:
120 spin_unlock_bh(&lapbeth->up_lock);
121 rcu_read_unlock();
122 return 0;
123 drop_unlock:
124 kfree_skb(skb);
125 goto out;
126 drop_unlock_rcu:
127 rcu_read_unlock();
128 drop:
129 kfree_skb(skb);
130 return 0;
131 }
132
lapbeth_data_indication(struct net_device * dev,struct sk_buff * skb)133 static int lapbeth_data_indication(struct net_device *dev, struct sk_buff *skb)
134 {
135 unsigned char *ptr;
136
137 if (skb_cow(skb, 1)) {
138 kfree_skb(skb);
139 return NET_RX_DROP;
140 }
141
142 skb_push(skb, 1);
143
144 ptr = skb->data;
145 *ptr = X25_IFACE_DATA;
146
147 skb->protocol = x25_type_trans(skb, dev);
148 return netif_rx(skb);
149 }
150
151 /*
152 * Send a LAPB frame via an ethernet interface
153 */
lapbeth_xmit(struct sk_buff * skb,struct net_device * dev)154 static netdev_tx_t lapbeth_xmit(struct sk_buff *skb,
155 struct net_device *dev)
156 {
157 struct lapbethdev *lapbeth = netdev_priv(dev);
158 int err;
159
160 spin_lock_bh(&lapbeth->up_lock);
161 if (!lapbeth->up)
162 goto drop;
163
164 /* There should be a pseudo header of 1 byte added by upper layers.
165 * Check to make sure it is there before reading it.
166 */
167 if (skb->len < 1)
168 goto drop;
169
170 switch (skb->data[0]) {
171 case X25_IFACE_DATA:
172 break;
173 case X25_IFACE_CONNECT:
174 if ((err = lapb_connect_request(dev)) != LAPB_OK)
175 pr_err("lapb_connect_request error: %d\n", err);
176 goto drop;
177 case X25_IFACE_DISCONNECT:
178 if ((err = lapb_disconnect_request(dev)) != LAPB_OK)
179 pr_err("lapb_disconnect_request err: %d\n", err);
180 fallthrough;
181 default:
182 goto drop;
183 }
184
185 skb_pull(skb, 1);
186
187 if ((err = lapb_data_request(dev, skb)) != LAPB_OK) {
188 pr_err("lapb_data_request error - %d\n", err);
189 goto drop;
190 }
191 out:
192 spin_unlock_bh(&lapbeth->up_lock);
193 return NETDEV_TX_OK;
194 drop:
195 kfree_skb(skb);
196 goto out;
197 }
198
lapbeth_data_transmit(struct net_device * ndev,struct sk_buff * skb)199 static void lapbeth_data_transmit(struct net_device *ndev, struct sk_buff *skb)
200 {
201 struct lapbethdev *lapbeth = netdev_priv(ndev);
202 unsigned char *ptr;
203 struct net_device *dev;
204 int size = skb->len;
205
206 ptr = skb_push(skb, 2);
207
208 *ptr++ = size % 256;
209 *ptr++ = size / 256;
210
211 ndev->stats.tx_packets++;
212 ndev->stats.tx_bytes += size;
213
214 skb->dev = dev = lapbeth->ethdev;
215
216 skb->protocol = htons(ETH_P_DEC);
217
218 skb_reset_network_header(skb);
219
220 dev_hard_header(skb, dev, ETH_P_DEC, bcast_addr, NULL, 0);
221
222 dev_queue_xmit(skb);
223 }
224
lapbeth_connected(struct net_device * dev,int reason)225 static void lapbeth_connected(struct net_device *dev, int reason)
226 {
227 unsigned char *ptr;
228 struct sk_buff *skb = dev_alloc_skb(1);
229
230 if (!skb) {
231 pr_err("out of memory\n");
232 return;
233 }
234
235 ptr = skb_put(skb, 1);
236 *ptr = X25_IFACE_CONNECT;
237
238 skb->protocol = x25_type_trans(skb, dev);
239 netif_rx(skb);
240 }
241
lapbeth_disconnected(struct net_device * dev,int reason)242 static void lapbeth_disconnected(struct net_device *dev, int reason)
243 {
244 unsigned char *ptr;
245 struct sk_buff *skb = dev_alloc_skb(1);
246
247 if (!skb) {
248 pr_err("out of memory\n");
249 return;
250 }
251
252 ptr = skb_put(skb, 1);
253 *ptr = X25_IFACE_DISCONNECT;
254
255 skb->protocol = x25_type_trans(skb, dev);
256 netif_rx(skb);
257 }
258
259 /*
260 * Set AX.25 callsign
261 */
lapbeth_set_mac_address(struct net_device * dev,void * addr)262 static int lapbeth_set_mac_address(struct net_device *dev, void *addr)
263 {
264 struct sockaddr *sa = addr;
265 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
266 return 0;
267 }
268
269
270 static const struct lapb_register_struct lapbeth_callbacks = {
271 .connect_confirmation = lapbeth_connected,
272 .connect_indication = lapbeth_connected,
273 .disconnect_confirmation = lapbeth_disconnected,
274 .disconnect_indication = lapbeth_disconnected,
275 .data_indication = lapbeth_data_indication,
276 .data_transmit = lapbeth_data_transmit,
277 };
278
279 /*
280 * open/close a device
281 */
lapbeth_open(struct net_device * dev)282 static int lapbeth_open(struct net_device *dev)
283 {
284 struct lapbethdev *lapbeth = netdev_priv(dev);
285 int err;
286
287 if ((err = lapb_register(dev, &lapbeth_callbacks)) != LAPB_OK) {
288 pr_err("lapb_register error: %d\n", err);
289 return -ENODEV;
290 }
291
292 spin_lock_bh(&lapbeth->up_lock);
293 lapbeth->up = true;
294 spin_unlock_bh(&lapbeth->up_lock);
295
296 return 0;
297 }
298
lapbeth_close(struct net_device * dev)299 static int lapbeth_close(struct net_device *dev)
300 {
301 struct lapbethdev *lapbeth = netdev_priv(dev);
302 int err;
303
304 spin_lock_bh(&lapbeth->up_lock);
305 lapbeth->up = false;
306 spin_unlock_bh(&lapbeth->up_lock);
307
308 if ((err = lapb_unregister(dev)) != LAPB_OK)
309 pr_err("lapb_unregister error: %d\n", err);
310
311 return 0;
312 }
313
314 /* ------------------------------------------------------------------------ */
315
316 static const struct net_device_ops lapbeth_netdev_ops = {
317 .ndo_open = lapbeth_open,
318 .ndo_stop = lapbeth_close,
319 .ndo_start_xmit = lapbeth_xmit,
320 .ndo_set_mac_address = lapbeth_set_mac_address,
321 };
322
lapbeth_setup(struct net_device * dev)323 static void lapbeth_setup(struct net_device *dev)
324 {
325 dev->netdev_ops = &lapbeth_netdev_ops;
326 dev->needs_free_netdev = true;
327 dev->type = ARPHRD_X25;
328 dev->hard_header_len = 0;
329 dev->mtu = 1000;
330 dev->addr_len = 0;
331 }
332
333 /*
334 * Setup a new device.
335 */
lapbeth_new_device(struct net_device * dev)336 static int lapbeth_new_device(struct net_device *dev)
337 {
338 struct net_device *ndev;
339 struct lapbethdev *lapbeth;
340 int rc = -ENOMEM;
341
342 ASSERT_RTNL();
343
344 ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d", NET_NAME_UNKNOWN,
345 lapbeth_setup);
346 if (!ndev)
347 goto out;
348
349 /* When transmitting data:
350 * first this driver removes a pseudo header of 1 byte,
351 * then the lapb module prepends an LAPB header of at most 3 bytes,
352 * then this driver prepends a length field of 2 bytes,
353 * then the underlying Ethernet device prepends its own header.
354 */
355 ndev->needed_headroom = -1 + 3 + 2 + dev->hard_header_len
356 + dev->needed_headroom;
357 ndev->needed_tailroom = dev->needed_tailroom;
358
359 lapbeth = netdev_priv(ndev);
360 lapbeth->axdev = ndev;
361
362 dev_hold(dev);
363 lapbeth->ethdev = dev;
364
365 lapbeth->up = false;
366 spin_lock_init(&lapbeth->up_lock);
367
368 rc = -EIO;
369 if (register_netdevice(ndev))
370 goto fail;
371
372 list_add_rcu(&lapbeth->node, &lapbeth_devices);
373 rc = 0;
374 out:
375 return rc;
376 fail:
377 dev_put(dev);
378 free_netdev(ndev);
379 goto out;
380 }
381
382 /*
383 * Free a lapb network device.
384 */
lapbeth_free_device(struct lapbethdev * lapbeth)385 static void lapbeth_free_device(struct lapbethdev *lapbeth)
386 {
387 dev_put(lapbeth->ethdev);
388 list_del_rcu(&lapbeth->node);
389 unregister_netdevice(lapbeth->axdev);
390 }
391
392 /*
393 * Handle device status changes.
394 *
395 * Called from notifier with RTNL held.
396 */
lapbeth_device_event(struct notifier_block * this,unsigned long event,void * ptr)397 static int lapbeth_device_event(struct notifier_block *this,
398 unsigned long event, void *ptr)
399 {
400 struct lapbethdev *lapbeth;
401 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
402
403 if (dev_net(dev) != &init_net)
404 return NOTIFY_DONE;
405
406 if (!dev_is_ethdev(dev))
407 return NOTIFY_DONE;
408
409 switch (event) {
410 case NETDEV_UP:
411 /* New ethernet device -> new LAPB interface */
412 if (lapbeth_get_x25_dev(dev) == NULL)
413 lapbeth_new_device(dev);
414 break;
415 case NETDEV_DOWN:
416 /* ethernet device closed -> close LAPB interface */
417 lapbeth = lapbeth_get_x25_dev(dev);
418 if (lapbeth)
419 dev_close(lapbeth->axdev);
420 break;
421 case NETDEV_UNREGISTER:
422 /* ethernet device disappears -> remove LAPB interface */
423 lapbeth = lapbeth_get_x25_dev(dev);
424 if (lapbeth)
425 lapbeth_free_device(lapbeth);
426 break;
427 }
428
429 return NOTIFY_DONE;
430 }
431
432 /* ------------------------------------------------------------------------ */
433
434 static struct packet_type lapbeth_packet_type __read_mostly = {
435 .type = cpu_to_be16(ETH_P_DEC),
436 .func = lapbeth_rcv,
437 };
438
439 static struct notifier_block lapbeth_dev_notifier = {
440 .notifier_call = lapbeth_device_event,
441 };
442
443 static const char banner[] __initconst =
444 KERN_INFO "LAPB Ethernet driver version 0.02\n";
445
lapbeth_init_driver(void)446 static int __init lapbeth_init_driver(void)
447 {
448 dev_add_pack(&lapbeth_packet_type);
449
450 register_netdevice_notifier(&lapbeth_dev_notifier);
451
452 printk(banner);
453
454 return 0;
455 }
456 module_init(lapbeth_init_driver);
457
lapbeth_cleanup_driver(void)458 static void __exit lapbeth_cleanup_driver(void)
459 {
460 struct lapbethdev *lapbeth;
461 struct list_head *entry, *tmp;
462
463 dev_remove_pack(&lapbeth_packet_type);
464 unregister_netdevice_notifier(&lapbeth_dev_notifier);
465
466 rtnl_lock();
467 list_for_each_safe(entry, tmp, &lapbeth_devices) {
468 lapbeth = list_entry(entry, struct lapbethdev, node);
469
470 dev_put(lapbeth->ethdev);
471 unregister_netdevice(lapbeth->axdev);
472 }
473 rtnl_unlock();
474 }
475 module_exit(lapbeth_cleanup_driver);
476
477 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
478 MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver");
479 MODULE_LICENSE("GPL");
480