lapbether.c 12.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2 3 4 5 6 7 8
/*
 *	"LAPB via ethernet" driver release 001
 *
 *	This code REQUIRES 2.1.15 or higher/ NET3.038
 *
 *	This is a "pseudo" network driver to allow LAPB over Ethernet.
 *
9
 *	This driver can use any ethernet destination address, and can be
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17
 *	limited to accept frames from one dedicated ethernet card only.
 *
 *	History
 *	LAPBETH 001	Jonathan Naylor		Cloned from bpqether.c
 *	2000-10-29	Henner Eisen	lapb_data_indication() return status.
 *	2000-11-14	Henner Eisen	dev_hold/put, NETDEV_GOING_DOWN support
 */

18 19
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
20 21 22 23
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/in.h>
24
#include <linux/slab.h>
L
Linus Torvalds 已提交
25 26 27 28 29 30 31 32
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/net.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/skbuff.h>
#include <net/sock.h>
33
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/notifier.h>
#include <linux/stat.h>
#include <linux/module.h>
#include <linux/lapb.h>
#include <linux/init.h>

#include <net/x25device.h>

44
static const u8 bcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52 53

/* If this number is made larger, check that the temporary string buffer
 * in lapbeth_new_device is large enough to store the probe device name.*/
#define MAXLAPBDEV 100

struct lapbethdev {
	struct list_head	node;
	struct net_device	*ethdev;	/* link to ethernet device */
	struct net_device	*axdev;		/* lapbeth device (lapb#) */
54 55
	bool			up;
	spinlock_t		up_lock;	/* Protects "up" */
56 57
	struct sk_buff_head	rx_queue;
	struct napi_struct	napi;
L
Linus Torvalds 已提交
58 59
};

60
static LIST_HEAD(lapbeth_devices);
L
Linus Torvalds 已提交
61

62 63 64
static void lapbeth_connected(struct net_device *dev, int reason);
static void lapbeth_disconnected(struct net_device *dev, int reason);

L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72 73
/* ------------------------------------------------------------------------ */

/*
 *	Get the LAPB device for the ethernet device
 */
static struct lapbethdev *lapbeth_get_x25_dev(struct net_device *dev)
{
	struct lapbethdev *lapbeth;

74
	list_for_each_entry_rcu(lapbeth, &lapbeth_devices, node, lockdep_rtnl_is_held()) {
75
		if (lapbeth->ethdev == dev)
L
Linus Torvalds 已提交
76 77 78 79 80 81 82 83 84 85 86 87
			return lapbeth;
	}
	return NULL;
}

static __inline__ int dev_is_ethdev(struct net_device *dev)
{
	return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
}

/* ------------------------------------------------------------------------ */

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
static int lapbeth_napi_poll(struct napi_struct *napi, int budget)
{
	struct lapbethdev *lapbeth = container_of(napi, struct lapbethdev,
						  napi);
	struct sk_buff *skb;
	int processed = 0;

	for (; processed < budget; ++processed) {
		skb = skb_dequeue(&lapbeth->rx_queue);
		if (!skb)
			break;
		netif_receive_skb_core(skb);
	}

	if (processed < budget)
		napi_complete(napi);

	return processed;
}

L
Linus Torvalds 已提交
108 109 110
/*
 *	Receive a LAPB frame via an ethernet interface.
 */
D
David S. Miller 已提交
111
static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
L
Linus Torvalds 已提交
112 113 114 115
{
	int len, err;
	struct lapbethdev *lapbeth;

116
	if (dev_net(dev) != &init_net)
117 118
		goto drop;

119 120
	skb = skb_share_check(skb, GFP_ATOMIC);
	if (!skb)
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128
		return NET_RX_DROP;

	if (!pskb_may_pull(skb, 2))
		goto drop;

	rcu_read_lock();
	lapbeth = lapbeth_get_x25_dev(dev);
	if (!lapbeth)
129 130 131
		goto drop_unlock_rcu;
	spin_lock_bh(&lapbeth->up_lock);
	if (!lapbeth->up)
L
Linus Torvalds 已提交
132 133 134
		goto drop_unlock;

	len = skb->data[0] + skb->data[1] * 256;
135 136
	dev->stats.rx_packets++;
	dev->stats.rx_bytes += len;
L
Linus Torvalds 已提交
137 138 139 140

	skb_pull(skb, 2);	/* Remove the length bytes */
	skb_trim(skb, len);	/* Set the length of the data */

141 142
	err = lapb_data_received(lapbeth->axdev, skb);
	if (err != LAPB_OK) {
L
Linus Torvalds 已提交
143 144 145 146
		printk(KERN_DEBUG "lapbether: lapb_data_received err - %d\n", err);
		goto drop_unlock;
	}
out:
147
	spin_unlock_bh(&lapbeth->up_lock);
L
Linus Torvalds 已提交
148 149 150 151 152
	rcu_read_unlock();
	return 0;
drop_unlock:
	kfree_skb(skb);
	goto out;
153 154
drop_unlock_rcu:
	rcu_read_unlock();
L
Linus Torvalds 已提交
155 156 157 158 159 160 161
drop:
	kfree_skb(skb);
	return 0;
}

static int lapbeth_data_indication(struct net_device *dev, struct sk_buff *skb)
{
162
	struct lapbethdev *lapbeth = netdev_priv(dev);
L
Linus Torvalds 已提交
163 164
	unsigned char *ptr;

165 166
	if (skb_cow(skb, 1)) {
		kfree_skb(skb);
L
Linus Torvalds 已提交
167
		return NET_RX_DROP;
168 169 170
	}

	skb_push(skb, 1);
L
Linus Torvalds 已提交
171 172

	ptr  = skb->data;
173
	*ptr = X25_IFACE_DATA;
L
Linus Torvalds 已提交
174 175

	skb->protocol = x25_type_trans(skb, dev);
176 177 178 179

	skb_queue_tail(&lapbeth->rx_queue, skb);
	napi_schedule(&lapbeth->napi);
	return NET_RX_SUCCESS;
L
Linus Torvalds 已提交
180 181 182 183 184
}

/*
 *	Send a LAPB frame via an ethernet interface
 */
185 186
static netdev_tx_t lapbeth_xmit(struct sk_buff *skb,
				      struct net_device *dev)
L
Linus Torvalds 已提交
187
{
188
	struct lapbethdev *lapbeth = netdev_priv(dev);
189
	int err;
L
Linus Torvalds 已提交
190

191 192
	spin_lock_bh(&lapbeth->up_lock);
	if (!lapbeth->up)
L
Linus Torvalds 已提交
193 194
		goto drop;

195 196 197 198 199 200
	/* There should be a pseudo header of 1 byte added by upper layers.
	 * Check to make sure it is there before reading it.
	 */
	if (skb->len < 1)
		goto drop;

L
Linus Torvalds 已提交
201
	switch (skb->data[0]) {
202
	case X25_IFACE_DATA:
L
Linus Torvalds 已提交
203
		break;
204
	case X25_IFACE_CONNECT:
205 206 207 208
		err = lapb_connect_request(dev);
		if (err == LAPB_CONNECTED)
			lapbeth_connected(dev, LAPB_OK);
		else if (err != LAPB_OK)
209
			pr_err("lapb_connect_request error: %d\n", err);
210
		goto drop;
211
	case X25_IFACE_DISCONNECT:
212 213 214 215
		err = lapb_disconnect_request(dev);
		if (err == LAPB_NOTCONNECTED)
			lapbeth_disconnected(dev, LAPB_OK);
		else if (err != LAPB_OK)
216
			pr_err("lapb_disconnect_request err: %d\n", err);
217
		fallthrough;
L
Linus Torvalds 已提交
218
	default:
219
		goto drop;
L
Linus Torvalds 已提交
220 221 222 223
	}

	skb_pull(skb, 1);

224 225
	err = lapb_data_request(dev, skb);
	if (err != LAPB_OK) {
226
		pr_err("lapb_data_request error - %d\n", err);
L
Linus Torvalds 已提交
227 228 229
		goto drop;
	}
out:
230
	spin_unlock_bh(&lapbeth->up_lock);
231
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
drop:
	kfree_skb(skb);
	goto out;
}

static void lapbeth_data_transmit(struct net_device *ndev, struct sk_buff *skb)
{
	struct lapbethdev *lapbeth = netdev_priv(ndev);
	unsigned char *ptr;
	struct net_device *dev;
	int size = skb->len;

	ptr = skb_push(skb, 2);

	*ptr++ = size % 256;
	*ptr++ = size / 256;

249 250
	ndev->stats.tx_packets++;
	ndev->stats.tx_bytes += size;
L
Linus Torvalds 已提交
251 252 253

	skb->dev = dev = lapbeth->ethdev;

254 255
	skb->protocol = htons(ETH_P_DEC);

256 257
	skb_reset_network_header(skb);

258
	dev_hard_header(skb, dev, ETH_P_DEC, bcast_addr, NULL, 0);
L
Linus Torvalds 已提交
259 260 261 262 263 264

	dev_queue_xmit(skb);
}

static void lapbeth_connected(struct net_device *dev, int reason)
{
265
	struct lapbethdev *lapbeth = netdev_priv(dev);
L
Linus Torvalds 已提交
266
	unsigned char *ptr;
267
	struct sk_buff *skb = __dev_alloc_skb(1, GFP_ATOMIC | __GFP_NOMEMALLOC);
L
Linus Torvalds 已提交
268

269
	if (!skb)
L
Linus Torvalds 已提交
270 271 272
		return;

	ptr  = skb_put(skb, 1);
273
	*ptr = X25_IFACE_CONNECT;
L
Linus Torvalds 已提交
274 275

	skb->protocol = x25_type_trans(skb, dev);
276 277 278

	skb_queue_tail(&lapbeth->rx_queue, skb);
	napi_schedule(&lapbeth->napi);
L
Linus Torvalds 已提交
279 280 281 282
}

static void lapbeth_disconnected(struct net_device *dev, int reason)
{
283
	struct lapbethdev *lapbeth = netdev_priv(dev);
L
Linus Torvalds 已提交
284
	unsigned char *ptr;
285
	struct sk_buff *skb = __dev_alloc_skb(1, GFP_ATOMIC | __GFP_NOMEMALLOC);
L
Linus Torvalds 已提交
286

287
	if (!skb)
L
Linus Torvalds 已提交
288 289 290
		return;

	ptr  = skb_put(skb, 1);
291
	*ptr = X25_IFACE_DISCONNECT;
L
Linus Torvalds 已提交
292 293

	skb->protocol = x25_type_trans(skb, dev);
294 295 296

	skb_queue_tail(&lapbeth->rx_queue, skb);
	napi_schedule(&lapbeth->napi);
L
Linus Torvalds 已提交
297 298 299 300 301 302 303 304
}

/*
 *	Set AX.25 callsign
 */
static int lapbeth_set_mac_address(struct net_device *dev, void *addr)
{
	struct sockaddr *sa = addr;
305

L
Linus Torvalds 已提交
306 307 308 309
	memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
	return 0;
}

310
static const struct lapb_register_struct lapbeth_callbacks = {
L
Linus Torvalds 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323
	.connect_confirmation    = lapbeth_connected,
	.connect_indication      = lapbeth_connected,
	.disconnect_confirmation = lapbeth_disconnected,
	.disconnect_indication   = lapbeth_disconnected,
	.data_indication         = lapbeth_data_indication,
	.data_transmit           = lapbeth_data_transmit,
};

/*
 * open/close a device
 */
static int lapbeth_open(struct net_device *dev)
{
324
	struct lapbethdev *lapbeth = netdev_priv(dev);
L
Linus Torvalds 已提交
325 326
	int err;

327 328
	napi_enable(&lapbeth->napi);

329 330
	err = lapb_register(dev, &lapbeth_callbacks);
	if (err != LAPB_OK) {
331
		pr_err("lapb_register error: %d\n", err);
L
Linus Torvalds 已提交
332 333 334
		return -ENODEV;
	}

335 336 337 338
	spin_lock_bh(&lapbeth->up_lock);
	lapbeth->up = true;
	spin_unlock_bh(&lapbeth->up_lock);

L
Linus Torvalds 已提交
339 340 341 342 343
	return 0;
}

static int lapbeth_close(struct net_device *dev)
{
344
	struct lapbethdev *lapbeth = netdev_priv(dev);
L
Linus Torvalds 已提交
345 346
	int err;

347 348 349 350
	spin_lock_bh(&lapbeth->up_lock);
	lapbeth->up = false;
	spin_unlock_bh(&lapbeth->up_lock);

351 352
	err = lapb_unregister(dev);
	if (err != LAPB_OK)
353
		pr_err("lapb_unregister error: %d\n", err);
L
Linus Torvalds 已提交
354

355 356
	napi_disable(&lapbeth->napi);

L
Linus Torvalds 已提交
357 358 359 360 361
	return 0;
}

/* ------------------------------------------------------------------------ */

362 363 364 365 366 367 368
static const struct net_device_ops lapbeth_netdev_ops = {
	.ndo_open	     = lapbeth_open,
	.ndo_stop	     = lapbeth_close,
	.ndo_start_xmit	     = lapbeth_xmit,
	.ndo_set_mac_address = lapbeth_set_mac_address,
};

L
Linus Torvalds 已提交
369 370
static void lapbeth_setup(struct net_device *dev)
{
371
	dev->netdev_ops	     = &lapbeth_netdev_ops;
372
	dev->needs_free_netdev = true;
L
Linus Torvalds 已提交
373
	dev->type            = ARPHRD_X25;
374
	dev->hard_header_len = 0;
L
Linus Torvalds 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	dev->mtu             = 1000;
	dev->addr_len        = 0;
}

/*
 *	Setup a new device.
 */
static int lapbeth_new_device(struct net_device *dev)
{
	struct net_device *ndev;
	struct lapbethdev *lapbeth;
	int rc = -ENOMEM;

	ASSERT_RTNL();

390 391
	ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d", NET_NAME_UNKNOWN,
			    lapbeth_setup);
L
Linus Torvalds 已提交
392 393 394
	if (!ndev)
		goto out;

395 396 397 398 399 400
	/* When transmitting data:
	 * first this driver removes a pseudo header of 1 byte,
	 * then the lapb module prepends an LAPB header of at most 3 bytes,
	 * then this driver prepends a length field of 2 bytes,
	 * then the underlying Ethernet device prepends its own header.
	 */
401 402
	ndev->needed_headroom = -1 + 3 + 2 + dev->hard_header_len
					   + dev->needed_headroom;
403
	ndev->needed_tailroom = dev->needed_tailroom;
404

L
Linus Torvalds 已提交
405 406 407 408 409 410
	lapbeth = netdev_priv(ndev);
	lapbeth->axdev = ndev;

	dev_hold(dev);
	lapbeth->ethdev = dev;

411 412 413
	lapbeth->up = false;
	spin_lock_init(&lapbeth->up_lock);

414 415 416
	skb_queue_head_init(&lapbeth->rx_queue);
	netif_napi_add(ndev, &lapbeth->napi, lapbeth_napi_poll, 16);

L
Linus Torvalds 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
	rc = -EIO;
	if (register_netdevice(ndev))
		goto fail;

	list_add_rcu(&lapbeth->node, &lapbeth_devices);
	rc = 0;
out:
	return rc;
fail:
	dev_put(dev);
	free_netdev(ndev);
	goto out;
}

/*
 *	Free a lapb network device.
 */
static void lapbeth_free_device(struct lapbethdev *lapbeth)
{
	dev_put(lapbeth->ethdev);
	list_del_rcu(&lapbeth->node);
	unregister_netdevice(lapbeth->axdev);
}

/*
 *	Handle device status changes.
 *
 * Called from notifier with RTNL held.
 */
static int lapbeth_device_event(struct notifier_block *this,
				unsigned long event, void *ptr)
{
	struct lapbethdev *lapbeth;
450
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
L
Linus Torvalds 已提交
451

452
	if (dev_net(dev) != &init_net)
453 454
		return NOTIFY_DONE;

L
Linus Torvalds 已提交
455 456 457 458 459 460 461 462 463
	if (!dev_is_ethdev(dev))
		return NOTIFY_DONE;

	switch (event) {
	case NETDEV_UP:
		/* New ethernet device -> new LAPB interface	 */
		if (lapbeth_get_x25_dev(dev) == NULL)
			lapbeth_new_device(dev);
		break;
464 465
	case NETDEV_GOING_DOWN:
		/* ethernet device closes -> close LAPB interface */
L
Linus Torvalds 已提交
466
		lapbeth = lapbeth_get_x25_dev(dev);
467
		if (lapbeth)
L
Linus Torvalds 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
			dev_close(lapbeth->axdev);
		break;
	case NETDEV_UNREGISTER:
		/* ethernet device disappears -> remove LAPB interface */
		lapbeth = lapbeth_get_x25_dev(dev);
		if (lapbeth)
			lapbeth_free_device(lapbeth);
		break;
	}

	return NOTIFY_DONE;
}

/* ------------------------------------------------------------------------ */

483
static struct packet_type lapbeth_packet_type __read_mostly = {
484
	.type = cpu_to_be16(ETH_P_DEC),
L
Linus Torvalds 已提交
485 486 487 488 489 490 491
	.func = lapbeth_rcv,
};

static struct notifier_block lapbeth_dev_notifier = {
	.notifier_call = lapbeth_device_event,
};

492
static const char banner[] __initconst =
493
	KERN_INFO "LAPB Ethernet driver version 0.02\n";
L
Linus Torvalds 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518

static int __init lapbeth_init_driver(void)
{
	dev_add_pack(&lapbeth_packet_type);

	register_netdevice_notifier(&lapbeth_dev_notifier);

	printk(banner);

	return 0;
}
module_init(lapbeth_init_driver);

static void __exit lapbeth_cleanup_driver(void)
{
	struct lapbethdev *lapbeth;
	struct list_head *entry, *tmp;

	dev_remove_pack(&lapbeth_packet_type);
	unregister_netdevice_notifier(&lapbeth_dev_notifier);

	rtnl_lock();
	list_for_each_safe(entry, tmp, &lapbeth_devices) {
		lapbeth = list_entry(entry, struct lapbethdev, node);

519
		dev_put(lapbeth->ethdev);
L
Linus Torvalds 已提交
520 521 522 523 524 525 526 527 528
		unregister_netdevice(lapbeth->axdev);
	}
	rtnl_unlock();
}
module_exit(lapbeth_cleanup_driver);

MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver");
MODULE_LICENSE("GPL");