xref: /openbmc/linux/net/ax25/ax25_iface.c (revision 81dcd1690697efbdf8126e78fbbf7c76d359377f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
31da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
41da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
51da177e4SLinus Torvalds  * (at your option) any later version.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds #include <linux/errno.h>
101da177e4SLinus Torvalds #include <linux/types.h>
111da177e4SLinus Torvalds #include <linux/socket.h>
121da177e4SLinus Torvalds #include <linux/in.h>
131da177e4SLinus Torvalds #include <linux/kernel.h>
1470868eacSRalf Baechle #include <linux/module.h>
151da177e4SLinus Torvalds #include <linux/sched.h>
161da177e4SLinus Torvalds #include <linux/spinlock.h>
171da177e4SLinus Torvalds #include <linux/timer.h>
181da177e4SLinus Torvalds #include <linux/string.h>
191da177e4SLinus Torvalds #include <linux/sockios.h>
201da177e4SLinus Torvalds #include <linux/net.h>
211da177e4SLinus Torvalds #include <net/ax25.h>
221da177e4SLinus Torvalds #include <linux/inet.h>
231da177e4SLinus Torvalds #include <linux/netdevice.h>
241da177e4SLinus Torvalds #include <linux/skbuff.h>
251da177e4SLinus Torvalds #include <net/sock.h>
261da177e4SLinus Torvalds #include <asm/uaccess.h>
271da177e4SLinus Torvalds #include <asm/system.h>
281da177e4SLinus Torvalds #include <linux/fcntl.h>
291da177e4SLinus Torvalds #include <linux/mm.h>
301da177e4SLinus Torvalds #include <linux/interrupt.h>
311da177e4SLinus Torvalds 
328d5cf596SRalf Baechle static struct ax25_protocol *protocol_list;
331da177e4SLinus Torvalds static DEFINE_RWLOCK(protocol_list_lock);
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds static struct linkfail_struct {
361da177e4SLinus Torvalds 	struct linkfail_struct *next;
371da177e4SLinus Torvalds 	void (*func)(ax25_cb *, int);
381da177e4SLinus Torvalds } *linkfail_list = NULL;
391da177e4SLinus Torvalds static DEFINE_SPINLOCK(linkfail_lock);
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds static struct listen_struct {
421da177e4SLinus Torvalds 	struct listen_struct *next;
431da177e4SLinus Torvalds 	ax25_address  callsign;
441da177e4SLinus Torvalds 	struct net_device *dev;
451da177e4SLinus Torvalds } *listen_list = NULL;
461da177e4SLinus Torvalds static DEFINE_SPINLOCK(listen_lock);
471da177e4SLinus Torvalds 
488d5cf596SRalf Baechle /*
498d5cf596SRalf Baechle  * Do not register the internal protocols AX25_P_TEXT, AX25_P_SEGMENT,
508d5cf596SRalf Baechle  * AX25_P_IP or AX25_P_ARP ...
518d5cf596SRalf Baechle  */
528d5cf596SRalf Baechle void ax25_register_pid(struct ax25_protocol *ap)
531da177e4SLinus Torvalds {
5495ff9f4dSRalf Baechle 	write_lock_bh(&protocol_list_lock);
558d5cf596SRalf Baechle 	ap->next = protocol_list;
568d5cf596SRalf Baechle 	protocol_list = ap;
5795ff9f4dSRalf Baechle 	write_unlock_bh(&protocol_list_lock);
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds 
608d5cf596SRalf Baechle EXPORT_SYMBOL_GPL(ax25_register_pid);
6170868eacSRalf Baechle 
621da177e4SLinus Torvalds void ax25_protocol_release(unsigned int pid)
631da177e4SLinus Torvalds {
648d5cf596SRalf Baechle 	struct ax25_protocol *s, *protocol;
651da177e4SLinus Torvalds 
6695ff9f4dSRalf Baechle 	write_lock_bh(&protocol_list_lock);
671da177e4SLinus Torvalds 	protocol = protocol_list;
681da177e4SLinus Torvalds 	if (protocol == NULL) {
6995ff9f4dSRalf Baechle 		write_unlock_bh(&protocol_list_lock);
701da177e4SLinus Torvalds 		return;
711da177e4SLinus Torvalds 	}
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	if (protocol->pid == pid) {
741da177e4SLinus Torvalds 		protocol_list = protocol->next;
7595ff9f4dSRalf Baechle 		write_unlock_bh(&protocol_list_lock);
761da177e4SLinus Torvalds 		kfree(protocol);
771da177e4SLinus Torvalds 		return;
781da177e4SLinus Torvalds 	}
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	while (protocol != NULL && protocol->next != NULL) {
811da177e4SLinus Torvalds 		if (protocol->next->pid == pid) {
821da177e4SLinus Torvalds 			s = protocol->next;
831da177e4SLinus Torvalds 			protocol->next = protocol->next->next;
8495ff9f4dSRalf Baechle 			write_unlock_bh(&protocol_list_lock);
851da177e4SLinus Torvalds 			kfree(s);
861da177e4SLinus Torvalds 			return;
871da177e4SLinus Torvalds 		}
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 		protocol = protocol->next;
901da177e4SLinus Torvalds 	}
9195ff9f4dSRalf Baechle 	write_unlock_bh(&protocol_list_lock);
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
9470868eacSRalf Baechle EXPORT_SYMBOL(ax25_protocol_release);
9570868eacSRalf Baechle 
961da177e4SLinus Torvalds int ax25_linkfail_register(void (*func)(ax25_cb *, int))
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds 	struct linkfail_struct *linkfail;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	if ((linkfail = kmalloc(sizeof(*linkfail), GFP_ATOMIC)) == NULL)
1011da177e4SLinus Torvalds 		return 0;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	linkfail->func = func;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	spin_lock_bh(&linkfail_lock);
1061da177e4SLinus Torvalds 	linkfail->next = linkfail_list;
1071da177e4SLinus Torvalds 	linkfail_list  = linkfail;
1081da177e4SLinus Torvalds 	spin_unlock_bh(&linkfail_lock);
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	return 1;
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
11370868eacSRalf Baechle EXPORT_SYMBOL(ax25_linkfail_register);
11470868eacSRalf Baechle 
1151da177e4SLinus Torvalds void ax25_linkfail_release(void (*func)(ax25_cb *, int))
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	struct linkfail_struct *s, *linkfail;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	spin_lock_bh(&linkfail_lock);
1201da177e4SLinus Torvalds 	linkfail = linkfail_list;
1211da177e4SLinus Torvalds 	if (linkfail == NULL) {
1221da177e4SLinus Torvalds 		spin_unlock_bh(&linkfail_lock);
1231da177e4SLinus Torvalds 		return;
1241da177e4SLinus Torvalds 	}
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	if (linkfail->func == func) {
1271da177e4SLinus Torvalds 		linkfail_list = linkfail->next;
1281da177e4SLinus Torvalds 		spin_unlock_bh(&linkfail_lock);
1291da177e4SLinus Torvalds 		kfree(linkfail);
1301da177e4SLinus Torvalds 		return;
1311da177e4SLinus Torvalds 	}
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	while (linkfail != NULL && linkfail->next != NULL) {
1341da177e4SLinus Torvalds 		if (linkfail->next->func == func) {
1351da177e4SLinus Torvalds 			s = linkfail->next;
1361da177e4SLinus Torvalds 			linkfail->next = linkfail->next->next;
1371da177e4SLinus Torvalds 			spin_unlock_bh(&linkfail_lock);
1381da177e4SLinus Torvalds 			kfree(s);
1391da177e4SLinus Torvalds 			return;
1401da177e4SLinus Torvalds 		}
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 		linkfail = linkfail->next;
1431da177e4SLinus Torvalds 	}
1441da177e4SLinus Torvalds 	spin_unlock_bh(&linkfail_lock);
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
14770868eacSRalf Baechle EXPORT_SYMBOL(ax25_linkfail_release);
14870868eacSRalf Baechle 
1491da177e4SLinus Torvalds int ax25_listen_register(ax25_address *callsign, struct net_device *dev)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	struct listen_struct *listen;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	if (ax25_listen_mine(callsign, dev))
1541da177e4SLinus Torvalds 		return 0;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	if ((listen = kmalloc(sizeof(*listen), GFP_ATOMIC)) == NULL)
157*81dcd169SRalf Baechle 		return -ENOMEM;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	listen->callsign = *callsign;
1601da177e4SLinus Torvalds 	listen->dev      = dev;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	spin_lock_bh(&listen_lock);
1631da177e4SLinus Torvalds 	listen->next = listen_list;
1641da177e4SLinus Torvalds 	listen_list  = listen;
1651da177e4SLinus Torvalds 	spin_unlock_bh(&listen_lock);
1661da177e4SLinus Torvalds 
167*81dcd169SRalf Baechle 	return 0;
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds 
17070868eacSRalf Baechle EXPORT_SYMBOL(ax25_listen_register);
17170868eacSRalf Baechle 
1721da177e4SLinus Torvalds void ax25_listen_release(ax25_address *callsign, struct net_device *dev)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	struct listen_struct *s, *listen;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	spin_lock_bh(&listen_lock);
1771da177e4SLinus Torvalds 	listen = listen_list;
1781da177e4SLinus Torvalds 	if (listen == NULL) {
1791da177e4SLinus Torvalds 		spin_unlock_bh(&listen_lock);
1801da177e4SLinus Torvalds 		return;
1811da177e4SLinus Torvalds 	}
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	if (ax25cmp(&listen->callsign, callsign) == 0 && listen->dev == dev) {
1841da177e4SLinus Torvalds 		listen_list = listen->next;
1851da177e4SLinus Torvalds 		spin_unlock_bh(&listen_lock);
1861da177e4SLinus Torvalds 		kfree(listen);
1871da177e4SLinus Torvalds 		return;
1881da177e4SLinus Torvalds 	}
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	while (listen != NULL && listen->next != NULL) {
1911da177e4SLinus Torvalds 		if (ax25cmp(&listen->next->callsign, callsign) == 0 && listen->next->dev == dev) {
1921da177e4SLinus Torvalds 			s = listen->next;
1931da177e4SLinus Torvalds 			listen->next = listen->next->next;
1941da177e4SLinus Torvalds 			spin_unlock_bh(&listen_lock);
1951da177e4SLinus Torvalds 			kfree(s);
1961da177e4SLinus Torvalds 			return;
1971da177e4SLinus Torvalds 		}
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 		listen = listen->next;
2001da177e4SLinus Torvalds 	}
2011da177e4SLinus Torvalds 	spin_unlock_bh(&listen_lock);
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
20470868eacSRalf Baechle EXPORT_SYMBOL(ax25_listen_release);
20570868eacSRalf Baechle 
2061da177e4SLinus Torvalds int (*ax25_protocol_function(unsigned int pid))(struct sk_buff *, ax25_cb *)
2071da177e4SLinus Torvalds {
2081da177e4SLinus Torvalds 	int (*res)(struct sk_buff *, ax25_cb *) = NULL;
2098d5cf596SRalf Baechle 	struct ax25_protocol *protocol;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	read_lock(&protocol_list_lock);
2121da177e4SLinus Torvalds 	for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
2131da177e4SLinus Torvalds 		if (protocol->pid == pid) {
2141da177e4SLinus Torvalds 			res = protocol->func;
2151da177e4SLinus Torvalds 			break;
2161da177e4SLinus Torvalds 		}
2171da177e4SLinus Torvalds 	read_unlock(&protocol_list_lock);
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	return res;
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds int ax25_listen_mine(ax25_address *callsign, struct net_device *dev)
2231da177e4SLinus Torvalds {
2241da177e4SLinus Torvalds 	struct listen_struct *listen;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	spin_lock_bh(&listen_lock);
2271da177e4SLinus Torvalds 	for (listen = listen_list; listen != NULL; listen = listen->next)
228*81dcd169SRalf Baechle 		if (ax25cmp(&listen->callsign, callsign) == 0 &&
229*81dcd169SRalf Baechle 		    (listen->dev == dev || listen->dev == NULL)) {
2301da177e4SLinus Torvalds 			spin_unlock_bh(&listen_lock);
2311da177e4SLinus Torvalds 			return 1;
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds 	spin_unlock_bh(&listen_lock);
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	return 0;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds void ax25_link_failed(ax25_cb *ax25, int reason)
2391da177e4SLinus Torvalds {
2401da177e4SLinus Torvalds 	struct linkfail_struct *linkfail;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	spin_lock_bh(&linkfail_lock);
2431da177e4SLinus Torvalds 	for (linkfail = linkfail_list; linkfail != NULL; linkfail = linkfail->next)
2441da177e4SLinus Torvalds 		(linkfail->func)(ax25, reason);
2451da177e4SLinus Torvalds 	spin_unlock_bh(&linkfail_lock);
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds int ax25_protocol_is_registered(unsigned int pid)
2491da177e4SLinus Torvalds {
2508d5cf596SRalf Baechle 	struct ax25_protocol *protocol;
2511da177e4SLinus Torvalds 	int res = 0;
2521da177e4SLinus Torvalds 
25395ff9f4dSRalf Baechle 	read_lock_bh(&protocol_list_lock);
2541da177e4SLinus Torvalds 	for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
2551da177e4SLinus Torvalds 		if (protocol->pid == pid) {
2561da177e4SLinus Torvalds 			res = 1;
2571da177e4SLinus Torvalds 			break;
2581da177e4SLinus Torvalds 		}
25995ff9f4dSRalf Baechle 	read_unlock_bh(&protocol_list_lock);
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	return res;
2621da177e4SLinus Torvalds }
263