12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds *
41da177e4SLinus Torvalds * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
51da177e4SLinus Torvalds */
61da177e4SLinus Torvalds #include <linux/errno.h>
71da177e4SLinus Torvalds #include <linux/types.h>
81da177e4SLinus Torvalds #include <linux/socket.h>
91da177e4SLinus Torvalds #include <linux/in.h>
101da177e4SLinus Torvalds #include <linux/kernel.h>
1170868eacSRalf Baechle #include <linux/module.h>
121da177e4SLinus Torvalds #include <linux/spinlock.h>
131da177e4SLinus Torvalds #include <linux/timer.h>
141da177e4SLinus Torvalds #include <linux/string.h>
151da177e4SLinus Torvalds #include <linux/sockios.h>
161da177e4SLinus Torvalds #include <linux/net.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
181da177e4SLinus Torvalds #include <net/ax25.h>
191da177e4SLinus Torvalds #include <linux/inet.h>
201da177e4SLinus Torvalds #include <linux/netdevice.h>
211da177e4SLinus Torvalds #include <linux/skbuff.h>
221da177e4SLinus Torvalds #include <net/sock.h>
237c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
241da177e4SLinus Torvalds #include <linux/fcntl.h>
251da177e4SLinus Torvalds #include <linux/mm.h>
261da177e4SLinus Torvalds #include <linux/interrupt.h>
271da177e4SLinus Torvalds
288d5cf596SRalf Baechle static struct ax25_protocol *protocol_list;
291da177e4SLinus Torvalds static DEFINE_RWLOCK(protocol_list_lock);
301da177e4SLinus Torvalds
31a4282717SRalf Baechle static HLIST_HEAD(ax25_linkfail_list);
321da177e4SLinus Torvalds static DEFINE_SPINLOCK(linkfail_lock);
331da177e4SLinus Torvalds
341da177e4SLinus Torvalds static struct listen_struct {
351da177e4SLinus Torvalds struct listen_struct *next;
361da177e4SLinus Torvalds ax25_address callsign;
371da177e4SLinus Torvalds struct net_device *dev;
381da177e4SLinus Torvalds } *listen_list = NULL;
391da177e4SLinus Torvalds static DEFINE_SPINLOCK(listen_lock);
401da177e4SLinus Torvalds
418d5cf596SRalf Baechle /*
428d5cf596SRalf Baechle * Do not register the internal protocols AX25_P_TEXT, AX25_P_SEGMENT,
438d5cf596SRalf Baechle * AX25_P_IP or AX25_P_ARP ...
448d5cf596SRalf Baechle */
ax25_register_pid(struct ax25_protocol * ap)458d5cf596SRalf Baechle void ax25_register_pid(struct ax25_protocol *ap)
461da177e4SLinus Torvalds {
4795ff9f4dSRalf Baechle write_lock_bh(&protocol_list_lock);
488d5cf596SRalf Baechle ap->next = protocol_list;
498d5cf596SRalf Baechle protocol_list = ap;
5095ff9f4dSRalf Baechle write_unlock_bh(&protocol_list_lock);
511da177e4SLinus Torvalds }
521da177e4SLinus Torvalds
538d5cf596SRalf Baechle EXPORT_SYMBOL_GPL(ax25_register_pid);
5470868eacSRalf Baechle
ax25_protocol_release(unsigned int pid)551da177e4SLinus Torvalds void ax25_protocol_release(unsigned int pid)
561da177e4SLinus Torvalds {
57d8f969e6SDavid S. Miller struct ax25_protocol *protocol;
581da177e4SLinus Torvalds
5995ff9f4dSRalf Baechle write_lock_bh(&protocol_list_lock);
601da177e4SLinus Torvalds protocol = protocol_list;
61910d30b7SIlpo Järvinen if (protocol == NULL)
62910d30b7SIlpo Järvinen goto out;
631da177e4SLinus Torvalds
641da177e4SLinus Torvalds if (protocol->pid == pid) {
651da177e4SLinus Torvalds protocol_list = protocol->next;
66910d30b7SIlpo Järvinen goto out;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds
691da177e4SLinus Torvalds while (protocol != NULL && protocol->next != NULL) {
701da177e4SLinus Torvalds if (protocol->next->pid == pid) {
711da177e4SLinus Torvalds protocol->next = protocol->next->next;
72910d30b7SIlpo Järvinen goto out;
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds
751da177e4SLinus Torvalds protocol = protocol->next;
761da177e4SLinus Torvalds }
77910d30b7SIlpo Järvinen out:
7895ff9f4dSRalf Baechle write_unlock_bh(&protocol_list_lock);
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds
8170868eacSRalf Baechle EXPORT_SYMBOL(ax25_protocol_release);
8270868eacSRalf Baechle
ax25_linkfail_register(struct ax25_linkfail * lf)83a4282717SRalf Baechle void ax25_linkfail_register(struct ax25_linkfail *lf)
841da177e4SLinus Torvalds {
851da177e4SLinus Torvalds spin_lock_bh(&linkfail_lock);
86a4282717SRalf Baechle hlist_add_head(&lf->lf_node, &ax25_linkfail_list);
871da177e4SLinus Torvalds spin_unlock_bh(&linkfail_lock);
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds
9070868eacSRalf Baechle EXPORT_SYMBOL(ax25_linkfail_register);
9170868eacSRalf Baechle
ax25_linkfail_release(struct ax25_linkfail * lf)92a4282717SRalf Baechle void ax25_linkfail_release(struct ax25_linkfail *lf)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds spin_lock_bh(&linkfail_lock);
95a4282717SRalf Baechle hlist_del_init(&lf->lf_node);
961da177e4SLinus Torvalds spin_unlock_bh(&linkfail_lock);
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds
9970868eacSRalf Baechle EXPORT_SYMBOL(ax25_linkfail_release);
10070868eacSRalf Baechle
ax25_listen_register(const ax25_address * callsign,struct net_device * dev)101*c045ad2cSJakub Kicinski int ax25_listen_register(const ax25_address *callsign, struct net_device *dev)
1021da177e4SLinus Torvalds {
1031da177e4SLinus Torvalds struct listen_struct *listen;
1041da177e4SLinus Torvalds
1051da177e4SLinus Torvalds if (ax25_listen_mine(callsign, dev))
1061da177e4SLinus Torvalds return 0;
1071da177e4SLinus Torvalds
1081da177e4SLinus Torvalds if ((listen = kmalloc(sizeof(*listen), GFP_ATOMIC)) == NULL)
10981dcd169SRalf Baechle return -ENOMEM;
1101da177e4SLinus Torvalds
1111da177e4SLinus Torvalds listen->callsign = *callsign;
1121da177e4SLinus Torvalds listen->dev = dev;
1131da177e4SLinus Torvalds
1141da177e4SLinus Torvalds spin_lock_bh(&listen_lock);
1151da177e4SLinus Torvalds listen->next = listen_list;
1161da177e4SLinus Torvalds listen_list = listen;
1171da177e4SLinus Torvalds spin_unlock_bh(&listen_lock);
1181da177e4SLinus Torvalds
11981dcd169SRalf Baechle return 0;
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds
12270868eacSRalf Baechle EXPORT_SYMBOL(ax25_listen_register);
12370868eacSRalf Baechle
ax25_listen_release(const ax25_address * callsign,struct net_device * dev)124*c045ad2cSJakub Kicinski void ax25_listen_release(const ax25_address *callsign, struct net_device *dev)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds struct listen_struct *s, *listen;
1271da177e4SLinus Torvalds
1281da177e4SLinus Torvalds spin_lock_bh(&listen_lock);
1291da177e4SLinus Torvalds listen = listen_list;
1301da177e4SLinus Torvalds if (listen == NULL) {
1311da177e4SLinus Torvalds spin_unlock_bh(&listen_lock);
1321da177e4SLinus Torvalds return;
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds
1351da177e4SLinus Torvalds if (ax25cmp(&listen->callsign, callsign) == 0 && listen->dev == dev) {
1361da177e4SLinus Torvalds listen_list = listen->next;
1371da177e4SLinus Torvalds spin_unlock_bh(&listen_lock);
1381da177e4SLinus Torvalds kfree(listen);
1391da177e4SLinus Torvalds return;
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds
1421da177e4SLinus Torvalds while (listen != NULL && listen->next != NULL) {
1431da177e4SLinus Torvalds if (ax25cmp(&listen->next->callsign, callsign) == 0 && listen->next->dev == dev) {
1441da177e4SLinus Torvalds s = listen->next;
1451da177e4SLinus Torvalds listen->next = listen->next->next;
1461da177e4SLinus Torvalds spin_unlock_bh(&listen_lock);
1471da177e4SLinus Torvalds kfree(s);
1481da177e4SLinus Torvalds return;
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds
1511da177e4SLinus Torvalds listen = listen->next;
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds spin_unlock_bh(&listen_lock);
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds
15670868eacSRalf Baechle EXPORT_SYMBOL(ax25_listen_release);
15770868eacSRalf Baechle
ax25_protocol_function(unsigned int pid)1581da177e4SLinus Torvalds int (*ax25_protocol_function(unsigned int pid))(struct sk_buff *, ax25_cb *)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds int (*res)(struct sk_buff *, ax25_cb *) = NULL;
1618d5cf596SRalf Baechle struct ax25_protocol *protocol;
1621da177e4SLinus Torvalds
1631da177e4SLinus Torvalds read_lock(&protocol_list_lock);
1641da177e4SLinus Torvalds for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
1651da177e4SLinus Torvalds if (protocol->pid == pid) {
1661da177e4SLinus Torvalds res = protocol->func;
1671da177e4SLinus Torvalds break;
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds read_unlock(&protocol_list_lock);
1701da177e4SLinus Torvalds
1711da177e4SLinus Torvalds return res;
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds
ax25_listen_mine(const ax25_address * callsign,struct net_device * dev)174*c045ad2cSJakub Kicinski int ax25_listen_mine(const ax25_address *callsign, struct net_device *dev)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds struct listen_struct *listen;
1771da177e4SLinus Torvalds
1781da177e4SLinus Torvalds spin_lock_bh(&listen_lock);
1791da177e4SLinus Torvalds for (listen = listen_list; listen != NULL; listen = listen->next)
18081dcd169SRalf Baechle if (ax25cmp(&listen->callsign, callsign) == 0 &&
18181dcd169SRalf Baechle (listen->dev == dev || listen->dev == NULL)) {
1821da177e4SLinus Torvalds spin_unlock_bh(&listen_lock);
1831da177e4SLinus Torvalds return 1;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds spin_unlock_bh(&listen_lock);
1861da177e4SLinus Torvalds
1871da177e4SLinus Torvalds return 0;
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds
ax25_link_failed(ax25_cb * ax25,int reason)1901da177e4SLinus Torvalds void ax25_link_failed(ax25_cb *ax25, int reason)
1911da177e4SLinus Torvalds {
192a4282717SRalf Baechle struct ax25_linkfail *lf;
1931da177e4SLinus Torvalds
1941da177e4SLinus Torvalds spin_lock_bh(&linkfail_lock);
195b67bfe0dSSasha Levin hlist_for_each_entry(lf, &ax25_linkfail_list, lf_node)
196a4282717SRalf Baechle lf->func(ax25, reason);
1971da177e4SLinus Torvalds spin_unlock_bh(&linkfail_lock);
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds
ax25_protocol_is_registered(unsigned int pid)2001da177e4SLinus Torvalds int ax25_protocol_is_registered(unsigned int pid)
2011da177e4SLinus Torvalds {
2028d5cf596SRalf Baechle struct ax25_protocol *protocol;
2031da177e4SLinus Torvalds int res = 0;
2041da177e4SLinus Torvalds
20595ff9f4dSRalf Baechle read_lock_bh(&protocol_list_lock);
2061da177e4SLinus Torvalds for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
2071da177e4SLinus Torvalds if (protocol->pid == pid) {
2081da177e4SLinus Torvalds res = 1;
2091da177e4SLinus Torvalds break;
2101da177e4SLinus Torvalds }
21195ff9f4dSRalf Baechle read_unlock_bh(&protocol_list_lock);
2121da177e4SLinus Torvalds
2131da177e4SLinus Torvalds return res;
2141da177e4SLinus Torvalds }
215