xref: /linux/drivers/net/hamradio/bpqether.c (revision 40286d6379aacfcc053253ef78dc78b09addffda)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	G8BPQ compatible "AX.25 via ethernet" driver release 004
4  *
5  *	This code REQUIRES 2.0.0 or higher/ NET3.029
6  *
7  *	This is a "pseudo" network driver to allow AX.25 over Ethernet
8  *	using G8BPQ encapsulation. It has been extracted from the protocol
9  *	implementation because
10  *
11  *		- things got unreadable within the protocol stack
12  *		- to cure the protocol stack from "feature-ism"
13  *		- a protocol implementation shouldn't need to know on
14  *		  which hardware it is running
15  *		- user-level programs like the AX.25 utilities shouldn't
16  *		  need to know about the hardware.
17  *		- IP over ethernet encapsulated AX.25 was impossible
18  *		- rxecho.c did not work
19  *		- to have room for extensions
20  *		- it just deserves to "live" as an own driver
21  *
22  *	This driver can use any ethernet destination address, and can be
23  *	limited to accept frames from one dedicated ethernet card only.
24  *
25  *	Note that the driver sets up the BPQ devices automagically on
26  *	startup or (if started before the "insmod" of an ethernet device)
27  *	on "ifconfig up". It hopefully will remove the BPQ on "rmmod"ing
28  *	the ethernet device (in fact: as soon as another ethernet or bpq
29  *	device gets "ifconfig"ured).
30  *
31  *	I have heard that several people are thinking of experiments
32  *	with highspeed packet radio using existing ethernet cards.
33  *	Well, this driver is prepared for this purpose, just add
34  *	your tx key control and a txdelay / tailtime algorithm,
35  *	probably some buffering, and /voila/...
36  *
37  *	History
38  *	BPQ   001	Joerg(DL1BKE)		Extracted BPQ code from AX.25
39  *						protocol stack and added my own
40  *						yet existing patches
41  *	BPQ   002	Joerg(DL1BKE)		Scan network device list on
42  *						startup.
43  *	BPQ   003	Joerg(DL1BKE)		Ethernet destination address
44  *						and accepted source address
45  *						can be configured by an ioctl()
46  *						call.
47  *						Fixed to match Linux networking
48  *						changes - 2.1.15.
49  *	BPQ   004	Joerg(DL1BKE)		Fixed to not lock up on ifconfig.
50  */
51 
52 #include <linux/errno.h>
53 #include <linux/types.h>
54 #include <linux/socket.h>
55 #include <linux/in.h>
56 #include <linux/kernel.h>
57 #include <linux/string.h>
58 #include <linux/net.h>
59 #include <linux/slab.h>
60 #include <net/ax25.h>
61 #include <linux/inet.h>
62 #include <linux/netdevice.h>
63 #include <linux/etherdevice.h>
64 #include <linux/if_arp.h>
65 #include <linux/skbuff.h>
66 #include <net/sock.h>
67 #include <linux/uaccess.h>
68 #include <linux/mm.h>
69 #include <linux/interrupt.h>
70 #include <linux/notifier.h>
71 #include <linux/proc_fs.h>
72 #include <linux/seq_file.h>
73 #include <linux/stat.h>
74 #include <linux/module.h>
75 #include <linux/init.h>
76 #include <linux/rtnetlink.h>
77 
78 #include <net/ip.h>
79 #include <net/arp.h>
80 #include <net/netdev_lock.h>
81 #include <net/net_namespace.h>
82 
83 #include <linux/bpqether.h>
84 
85 static const char banner[] __initconst = KERN_INFO \
86 	"AX.25: bpqether driver version 004\n";
87 
88 static int bpq_rcv(struct sk_buff *, struct net_device *, struct packet_type *, struct net_device *);
89 static int bpq_device_event(struct notifier_block *, unsigned long, void *);
90 
91 static struct packet_type bpq_packet_type __read_mostly = {
92 	.type	= cpu_to_be16(ETH_P_BPQ),
93 	.func	= bpq_rcv,
94 };
95 
96 static struct notifier_block bpq_dev_notifier = {
97 	.notifier_call = bpq_device_event,
98 };
99 
100 
101 struct bpqdev {
102 	struct list_head bpq_list;	/* list of bpq devices chain */
103 	struct net_device *ethdev;	/* link to ethernet device */
104 	struct net_device *axdev;	/* bpq device (bpq#) */
105 	char   dest_addr[6];		/* ether destination address */
106 	char   acpt_addr[6];		/* accept ether frames from this address only */
107 };
108 
109 static LIST_HEAD(bpq_devices);
110 
111 /* ------------------------------------------------------------------------ */
112 
113 
114 /*
115  *	Get the ethernet device for a BPQ device
116  */
117 static inline struct net_device *bpq_get_ether_dev(struct net_device *dev)
118 {
119 	struct bpqdev *bpq = netdev_priv(dev);
120 
121 	return bpq ? bpq->ethdev : NULL;
122 }
123 
124 /*
125  *	Get the BPQ device for the ethernet device
126  */
127 static inline struct net_device *bpq_get_ax25_dev(struct net_device *dev)
128 {
129 	struct bpqdev *bpq;
130 
131 	list_for_each_entry_rcu(bpq, &bpq_devices, bpq_list,
132 				lockdep_rtnl_is_held()) {
133 		if (bpq->ethdev == dev)
134 			return bpq->axdev;
135 	}
136 	return NULL;
137 }
138 
139 static inline int dev_is_ethdev(struct net_device *dev)
140 {
141 	return dev->type == ARPHRD_ETHER && !netdev_need_ops_lock(dev);
142 }
143 
144 /* ------------------------------------------------------------------------ */
145 
146 
147 /*
148  *	Receive an AX.25 frame via an ethernet interface.
149  */
150 static int bpq_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
151 {
152 	int len;
153 	char * ptr;
154 	struct ethhdr *eth;
155 	struct bpqdev *bpq;
156 
157 	if (!net_eq(dev_net(dev), &init_net))
158 		goto drop;
159 
160 	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
161 		return NET_RX_DROP;
162 
163 	if (!pskb_may_pull(skb, sizeof(struct ethhdr)))
164 		goto drop;
165 
166 	rcu_read_lock();
167 	dev = bpq_get_ax25_dev(dev);
168 
169 	if (dev == NULL || !netif_running(dev))
170 		goto drop_unlock;
171 
172 	/*
173 	 * if we want to accept frames from just one ethernet device
174 	 * we check the source address of the sender.
175 	 */
176 
177 	bpq = netdev_priv(dev);
178 
179 	eth = eth_hdr(skb);
180 
181 	if (!(bpq->acpt_addr[0] & 0x01) &&
182 	    !ether_addr_equal(eth->h_source, bpq->acpt_addr))
183 		goto drop_unlock;
184 
185 	if (skb_cow(skb, sizeof(struct ethhdr)))
186 		goto drop_unlock;
187 
188 	len = skb->data[0] + skb->data[1] * 256 - 5;
189 
190 	if (len < 0 || len > skb->len - 2)
191 		goto drop_unlock;
192 
193 	skb_pull(skb, 2);	/* Remove the length bytes */
194 	skb_trim(skb, len);	/* Set the length of the data */
195 
196 	dev->stats.rx_packets++;
197 	dev->stats.rx_bytes += len;
198 
199 	ptr = skb_push(skb, 1);
200 	*ptr = 0;
201 
202 	skb->protocol = ax25_type_trans(skb, dev);
203 	netif_rx(skb);
204 unlock:
205 
206 	rcu_read_unlock();
207 
208 	return 0;
209 drop_unlock:
210 	kfree_skb(skb);
211 	goto unlock;
212 
213 drop:
214 	kfree_skb(skb);
215 	return 0;
216 }
217 
218 /*
219  * 	Send an AX.25 frame via an ethernet interface
220  */
221 static netdev_tx_t bpq_xmit(struct sk_buff *skb, struct net_device *dev)
222 {
223 	unsigned char *ptr;
224 	struct bpqdev *bpq;
225 	struct net_device *orig_dev;
226 	int size;
227 
228 	if (skb->protocol == htons(ETH_P_IP))
229 		return ax25_ip_xmit(skb);
230 
231 	/*
232 	 * Just to be *really* sure not to send anything if the interface
233 	 * is down, the ethernet device may have gone.
234 	 */
235 	if (!netif_running(dev)) {
236 		kfree_skb(skb);
237 		return NETDEV_TX_OK;
238 	}
239 
240 	skb_pull(skb, 1);			/* Drop KISS byte */
241 	size = skb->len;
242 
243 	/*
244 	 * We're about to mess with the skb which may still shared with the
245 	 * generic networking code so unshare and ensure it's got enough
246 	 * space for the BPQ headers.
247 	 */
248 	if (skb_cow(skb, AX25_BPQ_HEADER_LEN)) {
249 		if (net_ratelimit())
250 			pr_err("bpqether: out of memory\n");
251 		kfree_skb(skb);
252 
253 		return NETDEV_TX_OK;
254 	}
255 
256 	ptr = skb_push(skb, 2);			/* Make space for length */
257 
258 	*ptr++ = (size + 5) % 256;
259 	*ptr++ = (size + 5) / 256;
260 
261 	bpq = netdev_priv(dev);
262 
263 	orig_dev = dev;
264 	if ((dev = bpq_get_ether_dev(dev)) == NULL) {
265 		orig_dev->stats.tx_dropped++;
266 		kfree_skb(skb);
267 		return NETDEV_TX_OK;
268 	}
269 
270 	skb->protocol = ax25_type_trans(skb, dev);
271 	skb_reset_network_header(skb);
272 	dev_hard_header(skb, dev, ETH_P_BPQ, bpq->dest_addr, NULL, 0);
273 	dev->stats.tx_packets++;
274 	dev->stats.tx_bytes+=skb->len;
275 
276 	dev_queue_xmit(skb);
277 	netif_wake_queue(dev);
278 	return NETDEV_TX_OK;
279 }
280 
281 /*
282  *	Set AX.25 callsign
283  */
284 static int bpq_set_mac_address(struct net_device *dev, void *addr)
285 {
286     struct sockaddr *sa = (struct sockaddr *)addr;
287 
288     dev_addr_set(dev, sa->sa_data);
289 
290     return 0;
291 }
292 
293 /*	Ioctl commands
294  *
295  *		SIOCSBPQETHOPT		reserved for enhancements
296  *		SIOCSBPQETHADDR		set the destination and accepted
297  *					source ethernet address (broadcast
298  *					or multicast: accept all)
299  */
300 static int bpq_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
301 			      void __user *data, int cmd)
302 {
303 	struct bpq_ethaddr __user *ethaddr = data;
304 	struct bpqdev *bpq = netdev_priv(dev);
305 	struct bpq_req req;
306 
307 	if (!capable(CAP_NET_ADMIN))
308 		return -EPERM;
309 
310 	switch (cmd) {
311 		case SIOCSBPQETHOPT:
312 			if (copy_from_user(&req, data, sizeof(struct bpq_req)))
313 				return -EFAULT;
314 			switch (req.cmd) {
315 				case SIOCGBPQETHPARAM:
316 				case SIOCSBPQETHPARAM:
317 				default:
318 					return -EINVAL;
319 			}
320 
321 			break;
322 
323 		case SIOCSBPQETHADDR:
324 			if (copy_from_user(bpq->dest_addr, ethaddr->destination, ETH_ALEN))
325 				return -EFAULT;
326 			if (copy_from_user(bpq->acpt_addr, ethaddr->accept, ETH_ALEN))
327 				return -EFAULT;
328 			break;
329 
330 		default:
331 			return -EINVAL;
332 	}
333 
334 	return 0;
335 }
336 
337 /*
338  * open/close a device
339  */
340 static int bpq_open(struct net_device *dev)
341 {
342 	netif_start_queue(dev);
343 	return 0;
344 }
345 
346 static int bpq_close(struct net_device *dev)
347 {
348 	netif_stop_queue(dev);
349 	return 0;
350 }
351 
352 
353 /* ------------------------------------------------------------------------ */
354 
355 #ifdef CONFIG_PROC_FS
356 /*
357  *	Proc filesystem
358  */
359 static void *bpq_seq_start(struct seq_file *seq, loff_t *pos)
360 	__acquires(RCU)
361 {
362 	int i = 1;
363 	struct bpqdev *bpqdev;
364 
365 	rcu_read_lock();
366 
367 	if (*pos == 0)
368 		return SEQ_START_TOKEN;
369 
370 	list_for_each_entry_rcu(bpqdev, &bpq_devices, bpq_list) {
371 		if (i == *pos)
372 			return bpqdev;
373 	}
374 	return NULL;
375 }
376 
377 static void *bpq_seq_next(struct seq_file *seq, void *v, loff_t *pos)
378 {
379 	struct list_head *p;
380 	struct bpqdev *bpqdev = v;
381 
382 	++*pos;
383 
384 	if (v == SEQ_START_TOKEN)
385 		p = rcu_dereference(list_next_rcu(&bpq_devices));
386 	else
387 		p = rcu_dereference(list_next_rcu(&bpqdev->bpq_list));
388 
389 	return (p == &bpq_devices) ? NULL
390 		: list_entry(p, struct bpqdev, bpq_list);
391 }
392 
393 static void bpq_seq_stop(struct seq_file *seq, void *v)
394 	__releases(RCU)
395 {
396 	rcu_read_unlock();
397 }
398 
399 
400 static int bpq_seq_show(struct seq_file *seq, void *v)
401 {
402 	if (v == SEQ_START_TOKEN)
403 		seq_puts(seq,
404 			 "dev   ether      destination        accept from\n");
405 	else {
406 		const struct bpqdev *bpqdev = v;
407 
408 		seq_printf(seq, "%-5s %-10s %pM  ",
409 			bpqdev->axdev->name, bpqdev->ethdev->name,
410 			bpqdev->dest_addr);
411 
412 		if (is_multicast_ether_addr(bpqdev->acpt_addr))
413 			seq_printf(seq, "*\n");
414 		else
415 			seq_printf(seq, "%pM\n", bpqdev->acpt_addr);
416 
417 	}
418 	return 0;
419 }
420 
421 static const struct seq_operations bpq_seqops = {
422 	.start = bpq_seq_start,
423 	.next = bpq_seq_next,
424 	.stop = bpq_seq_stop,
425 	.show = bpq_seq_show,
426 };
427 #endif
428 /* ------------------------------------------------------------------------ */
429 
430 static const struct net_device_ops bpq_netdev_ops = {
431 	.ndo_open	     = bpq_open,
432 	.ndo_stop	     = bpq_close,
433 	.ndo_start_xmit	     = bpq_xmit,
434 	.ndo_set_mac_address = bpq_set_mac_address,
435 	.ndo_siocdevprivate  = bpq_siocdevprivate,
436 };
437 
438 static void bpq_setup(struct net_device *dev)
439 {
440 	netdev_lockdep_set_classes(dev);
441 
442 	dev->netdev_ops	     = &bpq_netdev_ops;
443 	dev->needs_free_netdev = true;
444 
445 	dev->flags      = 0;
446 	dev->lltx = true;	/* Allow recursion */
447 
448 #if IS_ENABLED(CONFIG_AX25)
449 	dev->header_ops      = &ax25_header_ops;
450 #endif
451 
452 	dev->type            = ARPHRD_AX25;
453 	dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
454 	dev->mtu             = AX25_DEF_PACLEN;
455 	dev->addr_len        = AX25_ADDR_LEN;
456 
457 	memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
458 	dev_addr_set(dev, (u8 *)&ax25_defaddr);
459 }
460 
461 /*
462  *	Setup a new device.
463  */
464 static int bpq_new_device(struct net_device *edev)
465 {
466 	int err;
467 	struct net_device *ndev;
468 	struct bpqdev *bpq;
469 
470 	ndev = alloc_netdev(sizeof(struct bpqdev), "bpq%d", NET_NAME_UNKNOWN,
471 			    bpq_setup);
472 	if (!ndev)
473 		return -ENOMEM;
474 
475 
476 	bpq = netdev_priv(ndev);
477 	dev_hold(edev);
478 	bpq->ethdev = edev;
479 	bpq->axdev = ndev;
480 
481 	eth_broadcast_addr(bpq->dest_addr);
482 	eth_broadcast_addr(bpq->acpt_addr);
483 
484 	err = register_netdevice(ndev);
485 	if (err)
486 		goto error;
487 
488 	/* List protected by RTNL */
489 	list_add_rcu(&bpq->bpq_list, &bpq_devices);
490 	return 0;
491 
492  error:
493 	dev_put(edev);
494 	free_netdev(ndev);
495 	return err;
496 
497 }
498 
499 static void bpq_free_device(struct net_device *ndev)
500 {
501 	struct bpqdev *bpq = netdev_priv(ndev);
502 
503 	dev_put(bpq->ethdev);
504 	list_del_rcu(&bpq->bpq_list);
505 
506 	unregister_netdevice(ndev);
507 }
508 
509 /*
510  *	Handle device status changes.
511  */
512 static int bpq_device_event(struct notifier_block *this,
513 			    unsigned long event, void *ptr)
514 {
515 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
516 
517 	if (!net_eq(dev_net(dev), &init_net))
518 		return NOTIFY_DONE;
519 
520 	if (!dev_is_ethdev(dev) && !bpq_get_ax25_dev(dev))
521 		return NOTIFY_DONE;
522 
523 	switch (event) {
524 	case NETDEV_UP:		/* new ethernet device -> new BPQ interface */
525 		if (bpq_get_ax25_dev(dev) == NULL)
526 			bpq_new_device(dev);
527 		break;
528 
529 	case NETDEV_DOWN:	/* ethernet device closed -> close BPQ interface */
530 		if ((dev = bpq_get_ax25_dev(dev)) != NULL)
531 			dev_close(dev);
532 		break;
533 
534 	case NETDEV_UNREGISTER:	/* ethernet device removed -> free BPQ interface */
535 		if ((dev = bpq_get_ax25_dev(dev)) != NULL)
536 			bpq_free_device(dev);
537 		break;
538 	default:
539 		break;
540 	}
541 
542 	return NOTIFY_DONE;
543 }
544 
545 
546 /* ------------------------------------------------------------------------ */
547 
548 /*
549  * Initialize driver. To be called from af_ax25 if not compiled as a
550  * module
551  */
552 static int __init bpq_init_driver(void)
553 {
554 #ifdef CONFIG_PROC_FS
555 	if (!proc_create_seq("bpqether", 0444, init_net.proc_net, &bpq_seqops)) {
556 		printk(KERN_ERR
557 			"bpq: cannot create /proc/net/bpqether entry.\n");
558 		return -ENOENT;
559 	}
560 #endif  /* CONFIG_PROC_FS */
561 
562 	dev_add_pack(&bpq_packet_type);
563 
564 	register_netdevice_notifier(&bpq_dev_notifier);
565 
566 	printk(banner);
567 
568 	return 0;
569 }
570 
571 static void __exit bpq_cleanup_driver(void)
572 {
573 	struct bpqdev *bpq;
574 
575 	dev_remove_pack(&bpq_packet_type);
576 
577 	unregister_netdevice_notifier(&bpq_dev_notifier);
578 
579 	remove_proc_entry("bpqether", init_net.proc_net);
580 
581 	rtnl_lock();
582 	while (!list_empty(&bpq_devices)) {
583 		bpq = list_entry(bpq_devices.next, struct bpqdev, bpq_list);
584 		bpq_free_device(bpq->axdev);
585 	}
586 	rtnl_unlock();
587 }
588 
589 MODULE_AUTHOR("Joerg Reuter DL1BKE <jreuter@yaina.de>");
590 MODULE_DESCRIPTION("Transmit and receive AX.25 packets over Ethernet");
591 MODULE_LICENSE("GPL");
592 module_init(bpq_init_driver);
593 module_exit(bpq_cleanup_driver);
594