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 && strncmp(dev->name, "dummy", 5);
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 	skb_pull(skb, 2);	/* Remove the length bytes */
191 	skb_trim(skb, len);	/* Set the length of the data */
192 
193 	dev->stats.rx_packets++;
194 	dev->stats.rx_bytes += len;
195 
196 	ptr = skb_push(skb, 1);
197 	*ptr = 0;
198 
199 	skb->protocol = ax25_type_trans(skb, dev);
200 	netif_rx(skb);
201 unlock:
202 
203 	rcu_read_unlock();
204 
205 	return 0;
206 drop_unlock:
207 	kfree_skb(skb);
208 	goto unlock;
209 
210 drop:
211 	kfree_skb(skb);
212 	return 0;
213 }
214 
215 /*
216  * 	Send an AX.25 frame via an ethernet interface
217  */
218 static netdev_tx_t bpq_xmit(struct sk_buff *skb, struct net_device *dev)
219 {
220 	unsigned char *ptr;
221 	struct bpqdev *bpq;
222 	struct net_device *orig_dev;
223 	int size;
224 
225 	if (skb->protocol == htons(ETH_P_IP))
226 		return ax25_ip_xmit(skb);
227 
228 	/*
229 	 * Just to be *really* sure not to send anything if the interface
230 	 * is down, the ethernet device may have gone.
231 	 */
232 	if (!netif_running(dev)) {
233 		kfree_skb(skb);
234 		return NETDEV_TX_OK;
235 	}
236 
237 	skb_pull(skb, 1);			/* Drop KISS byte */
238 	size = skb->len;
239 
240 	/*
241 	 * We're about to mess with the skb which may still shared with the
242 	 * generic networking code so unshare and ensure it's got enough
243 	 * space for the BPQ headers.
244 	 */
245 	if (skb_cow(skb, AX25_BPQ_HEADER_LEN)) {
246 		if (net_ratelimit())
247 			pr_err("bpqether: out of memory\n");
248 		kfree_skb(skb);
249 
250 		return NETDEV_TX_OK;
251 	}
252 
253 	ptr = skb_push(skb, 2);			/* Make space for length */
254 
255 	*ptr++ = (size + 5) % 256;
256 	*ptr++ = (size + 5) / 256;
257 
258 	bpq = netdev_priv(dev);
259 
260 	orig_dev = dev;
261 	if ((dev = bpq_get_ether_dev(dev)) == NULL) {
262 		orig_dev->stats.tx_dropped++;
263 		kfree_skb(skb);
264 		return NETDEV_TX_OK;
265 	}
266 
267 	skb->protocol = ax25_type_trans(skb, dev);
268 	skb_reset_network_header(skb);
269 	dev_hard_header(skb, dev, ETH_P_BPQ, bpq->dest_addr, NULL, 0);
270 	dev->stats.tx_packets++;
271 	dev->stats.tx_bytes+=skb->len;
272 
273 	dev_queue_xmit(skb);
274 	netif_wake_queue(dev);
275 	return NETDEV_TX_OK;
276 }
277 
278 /*
279  *	Set AX.25 callsign
280  */
281 static int bpq_set_mac_address(struct net_device *dev, void *addr)
282 {
283     struct sockaddr *sa = (struct sockaddr *)addr;
284 
285     dev_addr_set(dev, sa->sa_data);
286 
287     return 0;
288 }
289 
290 /*	Ioctl commands
291  *
292  *		SIOCSBPQETHOPT		reserved for enhancements
293  *		SIOCSBPQETHADDR		set the destination and accepted
294  *					source ethernet address (broadcast
295  *					or multicast: accept all)
296  */
297 static int bpq_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
298 			      void __user *data, int cmd)
299 {
300 	struct bpq_ethaddr __user *ethaddr = data;
301 	struct bpqdev *bpq = netdev_priv(dev);
302 	struct bpq_req req;
303 
304 	if (!capable(CAP_NET_ADMIN))
305 		return -EPERM;
306 
307 	switch (cmd) {
308 		case SIOCSBPQETHOPT:
309 			if (copy_from_user(&req, data, sizeof(struct bpq_req)))
310 				return -EFAULT;
311 			switch (req.cmd) {
312 				case SIOCGBPQETHPARAM:
313 				case SIOCSBPQETHPARAM:
314 				default:
315 					return -EINVAL;
316 			}
317 
318 			break;
319 
320 		case SIOCSBPQETHADDR:
321 			if (copy_from_user(bpq->dest_addr, ethaddr->destination, ETH_ALEN))
322 				return -EFAULT;
323 			if (copy_from_user(bpq->acpt_addr, ethaddr->accept, ETH_ALEN))
324 				return -EFAULT;
325 			break;
326 
327 		default:
328 			return -EINVAL;
329 	}
330 
331 	return 0;
332 }
333 
334 /*
335  * open/close a device
336  */
337 static int bpq_open(struct net_device *dev)
338 {
339 	netif_start_queue(dev);
340 	return 0;
341 }
342 
343 static int bpq_close(struct net_device *dev)
344 {
345 	netif_stop_queue(dev);
346 	return 0;
347 }
348 
349 
350 /* ------------------------------------------------------------------------ */
351 
352 #ifdef CONFIG_PROC_FS
353 /*
354  *	Proc filesystem
355  */
356 static void *bpq_seq_start(struct seq_file *seq, loff_t *pos)
357 	__acquires(RCU)
358 {
359 	int i = 1;
360 	struct bpqdev *bpqdev;
361 
362 	rcu_read_lock();
363 
364 	if (*pos == 0)
365 		return SEQ_START_TOKEN;
366 
367 	list_for_each_entry_rcu(bpqdev, &bpq_devices, bpq_list) {
368 		if (i == *pos)
369 			return bpqdev;
370 	}
371 	return NULL;
372 }
373 
374 static void *bpq_seq_next(struct seq_file *seq, void *v, loff_t *pos)
375 {
376 	struct list_head *p;
377 	struct bpqdev *bpqdev = v;
378 
379 	++*pos;
380 
381 	if (v == SEQ_START_TOKEN)
382 		p = rcu_dereference(list_next_rcu(&bpq_devices));
383 	else
384 		p = rcu_dereference(list_next_rcu(&bpqdev->bpq_list));
385 
386 	return (p == &bpq_devices) ? NULL
387 		: list_entry(p, struct bpqdev, bpq_list);
388 }
389 
390 static void bpq_seq_stop(struct seq_file *seq, void *v)
391 	__releases(RCU)
392 {
393 	rcu_read_unlock();
394 }
395 
396 
397 static int bpq_seq_show(struct seq_file *seq, void *v)
398 {
399 	if (v == SEQ_START_TOKEN)
400 		seq_puts(seq,
401 			 "dev   ether      destination        accept from\n");
402 	else {
403 		const struct bpqdev *bpqdev = v;
404 
405 		seq_printf(seq, "%-5s %-10s %pM  ",
406 			bpqdev->axdev->name, bpqdev->ethdev->name,
407 			bpqdev->dest_addr);
408 
409 		if (is_multicast_ether_addr(bpqdev->acpt_addr))
410 			seq_printf(seq, "*\n");
411 		else
412 			seq_printf(seq, "%pM\n", bpqdev->acpt_addr);
413 
414 	}
415 	return 0;
416 }
417 
418 static const struct seq_operations bpq_seqops = {
419 	.start = bpq_seq_start,
420 	.next = bpq_seq_next,
421 	.stop = bpq_seq_stop,
422 	.show = bpq_seq_show,
423 };
424 #endif
425 /* ------------------------------------------------------------------------ */
426 
427 static const struct net_device_ops bpq_netdev_ops = {
428 	.ndo_open	     = bpq_open,
429 	.ndo_stop	     = bpq_close,
430 	.ndo_start_xmit	     = bpq_xmit,
431 	.ndo_set_mac_address = bpq_set_mac_address,
432 	.ndo_siocdevprivate  = bpq_siocdevprivate,
433 };
434 
435 static void bpq_setup(struct net_device *dev)
436 {
437 	netdev_lockdep_set_classes(dev);
438 
439 	dev->netdev_ops	     = &bpq_netdev_ops;
440 	dev->needs_free_netdev = true;
441 
442 	dev->flags      = 0;
443 	dev->lltx = true;	/* Allow recursion */
444 
445 #if IS_ENABLED(CONFIG_AX25)
446 	dev->header_ops      = &ax25_header_ops;
447 #endif
448 
449 	dev->type            = ARPHRD_AX25;
450 	dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
451 	dev->mtu             = AX25_DEF_PACLEN;
452 	dev->addr_len        = AX25_ADDR_LEN;
453 
454 	memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
455 	dev_addr_set(dev, (u8 *)&ax25_defaddr);
456 }
457 
458 /*
459  *	Setup a new device.
460  */
461 static int bpq_new_device(struct net_device *edev)
462 {
463 	int err;
464 	struct net_device *ndev;
465 	struct bpqdev *bpq;
466 
467 	ndev = alloc_netdev(sizeof(struct bpqdev), "bpq%d", NET_NAME_UNKNOWN,
468 			    bpq_setup);
469 	if (!ndev)
470 		return -ENOMEM;
471 
472 
473 	bpq = netdev_priv(ndev);
474 	dev_hold(edev);
475 	bpq->ethdev = edev;
476 	bpq->axdev = ndev;
477 
478 	eth_broadcast_addr(bpq->dest_addr);
479 	eth_broadcast_addr(bpq->acpt_addr);
480 
481 	err = register_netdevice(ndev);
482 	if (err)
483 		goto error;
484 
485 	/* List protected by RTNL */
486 	list_add_rcu(&bpq->bpq_list, &bpq_devices);
487 	return 0;
488 
489  error:
490 	dev_put(edev);
491 	free_netdev(ndev);
492 	return err;
493 
494 }
495 
496 static void bpq_free_device(struct net_device *ndev)
497 {
498 	struct bpqdev *bpq = netdev_priv(ndev);
499 
500 	dev_put(bpq->ethdev);
501 	list_del_rcu(&bpq->bpq_list);
502 
503 	unregister_netdevice(ndev);
504 }
505 
506 /*
507  *	Handle device status changes.
508  */
509 static int bpq_device_event(struct notifier_block *this,
510 			    unsigned long event, void *ptr)
511 {
512 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
513 
514 	if (!net_eq(dev_net(dev), &init_net))
515 		return NOTIFY_DONE;
516 
517 	if (!dev_is_ethdev(dev) && !bpq_get_ax25_dev(dev))
518 		return NOTIFY_DONE;
519 
520 	switch (event) {
521 	case NETDEV_UP:		/* new ethernet device -> new BPQ interface */
522 		if (bpq_get_ax25_dev(dev) == NULL)
523 			bpq_new_device(dev);
524 		break;
525 
526 	case NETDEV_DOWN:	/* ethernet device closed -> close BPQ interface */
527 		if ((dev = bpq_get_ax25_dev(dev)) != NULL)
528 			dev_close(dev);
529 		break;
530 
531 	case NETDEV_UNREGISTER:	/* ethernet device removed -> free BPQ interface */
532 		if ((dev = bpq_get_ax25_dev(dev)) != NULL)
533 			bpq_free_device(dev);
534 		break;
535 	default:
536 		break;
537 	}
538 
539 	return NOTIFY_DONE;
540 }
541 
542 
543 /* ------------------------------------------------------------------------ */
544 
545 /*
546  * Initialize driver. To be called from af_ax25 if not compiled as a
547  * module
548  */
549 static int __init bpq_init_driver(void)
550 {
551 #ifdef CONFIG_PROC_FS
552 	if (!proc_create_seq("bpqether", 0444, init_net.proc_net, &bpq_seqops)) {
553 		printk(KERN_ERR
554 			"bpq: cannot create /proc/net/bpqether entry.\n");
555 		return -ENOENT;
556 	}
557 #endif  /* CONFIG_PROC_FS */
558 
559 	dev_add_pack(&bpq_packet_type);
560 
561 	register_netdevice_notifier(&bpq_dev_notifier);
562 
563 	printk(banner);
564 
565 	return 0;
566 }
567 
568 static void __exit bpq_cleanup_driver(void)
569 {
570 	struct bpqdev *bpq;
571 
572 	dev_remove_pack(&bpq_packet_type);
573 
574 	unregister_netdevice_notifier(&bpq_dev_notifier);
575 
576 	remove_proc_entry("bpqether", init_net.proc_net);
577 
578 	rtnl_lock();
579 	while (!list_empty(&bpq_devices)) {
580 		bpq = list_entry(bpq_devices.next, struct bpqdev, bpq_list);
581 		bpq_free_device(bpq->axdev);
582 	}
583 	rtnl_unlock();
584 }
585 
586 MODULE_AUTHOR("Joerg Reuter DL1BKE <jreuter@yaina.de>");
587 MODULE_DESCRIPTION("Transmit and receive AX.25 packets over Ethernet");
588 MODULE_LICENSE("GPL");
589 module_init(bpq_init_driver);
590 module_exit(bpq_cleanup_driver);
591