1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
6 #include <linux/errno.h>
7 #include <linux/types.h>
8 #include <linux/socket.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/spinlock.h>
13 #include <linux/timer.h>
14 #include <linux/string.h>
15 #include <linux/sockios.h>
16 #include <linux/net.h>
17 #include <linux/slab.h>
19 #include <linux/inet.h>
20 #include <linux/netdevice.h>
21 #include <linux/skbuff.h>
23 #include <linux/uaccess.h>
24 #include <linux/fcntl.h>
26 #include <linux/interrupt.h>
28 static struct ax25_protocol
*protocol_list
;
29 static DEFINE_RWLOCK(protocol_list_lock
);
31 static HLIST_HEAD(ax25_linkfail_list
);
32 static DEFINE_SPINLOCK(linkfail_lock
);
34 static struct listen_struct
{
35 struct listen_struct
*next
;
36 ax25_address callsign
;
37 struct net_device
*dev
;
38 } *listen_list
= NULL
;
39 static DEFINE_SPINLOCK(listen_lock
);
42 * Do not register the internal protocols AX25_P_TEXT, AX25_P_SEGMENT,
43 * AX25_P_IP or AX25_P_ARP ...
45 void ax25_register_pid(struct ax25_protocol
*ap
)
47 write_lock_bh(&protocol_list_lock
);
48 ap
->next
= protocol_list
;
50 write_unlock_bh(&protocol_list_lock
);
53 EXPORT_SYMBOL_GPL(ax25_register_pid
);
55 void ax25_protocol_release(unsigned int pid
)
57 struct ax25_protocol
*protocol
;
59 write_lock_bh(&protocol_list_lock
);
60 protocol
= protocol_list
;
64 if (protocol
->pid
== pid
) {
65 protocol_list
= protocol
->next
;
69 while (protocol
!= NULL
&& protocol
->next
!= NULL
) {
70 if (protocol
->next
->pid
== pid
) {
71 protocol
->next
= protocol
->next
->next
;
75 protocol
= protocol
->next
;
78 write_unlock_bh(&protocol_list_lock
);
81 EXPORT_SYMBOL(ax25_protocol_release
);
83 void ax25_linkfail_register(struct ax25_linkfail
*lf
)
85 spin_lock_bh(&linkfail_lock
);
86 hlist_add_head(&lf
->lf_node
, &ax25_linkfail_list
);
87 spin_unlock_bh(&linkfail_lock
);
90 EXPORT_SYMBOL(ax25_linkfail_register
);
92 void ax25_linkfail_release(struct ax25_linkfail
*lf
)
94 spin_lock_bh(&linkfail_lock
);
95 hlist_del_init(&lf
->lf_node
);
96 spin_unlock_bh(&linkfail_lock
);
99 EXPORT_SYMBOL(ax25_linkfail_release
);
101 int ax25_listen_register(const ax25_address
*callsign
, struct net_device
*dev
)
103 struct listen_struct
*listen
;
105 if (ax25_listen_mine(callsign
, dev
))
108 if ((listen
= kmalloc(sizeof(*listen
), GFP_ATOMIC
)) == NULL
)
111 listen
->callsign
= *callsign
;
114 spin_lock_bh(&listen_lock
);
115 listen
->next
= listen_list
;
116 listen_list
= listen
;
117 spin_unlock_bh(&listen_lock
);
122 EXPORT_SYMBOL(ax25_listen_register
);
124 void ax25_listen_release(const ax25_address
*callsign
, struct net_device
*dev
)
126 struct listen_struct
*s
, *listen
;
128 spin_lock_bh(&listen_lock
);
129 listen
= listen_list
;
130 if (listen
== NULL
) {
131 spin_unlock_bh(&listen_lock
);
135 if (ax25cmp(&listen
->callsign
, callsign
) == 0 && listen
->dev
== dev
) {
136 listen_list
= listen
->next
;
137 spin_unlock_bh(&listen_lock
);
142 while (listen
!= NULL
&& listen
->next
!= NULL
) {
143 if (ax25cmp(&listen
->next
->callsign
, callsign
) == 0 && listen
->next
->dev
== dev
) {
145 listen
->next
= listen
->next
->next
;
146 spin_unlock_bh(&listen_lock
);
151 listen
= listen
->next
;
153 spin_unlock_bh(&listen_lock
);
156 EXPORT_SYMBOL(ax25_listen_release
);
158 int (*ax25_protocol_function(unsigned int pid
))(struct sk_buff
*, ax25_cb
*)
160 int (*res
)(struct sk_buff
*, ax25_cb
*) = NULL
;
161 struct ax25_protocol
*protocol
;
163 read_lock(&protocol_list_lock
);
164 for (protocol
= protocol_list
; protocol
!= NULL
; protocol
= protocol
->next
)
165 if (protocol
->pid
== pid
) {
166 res
= protocol
->func
;
169 read_unlock(&protocol_list_lock
);
174 int ax25_listen_mine(const ax25_address
*callsign
, struct net_device
*dev
)
176 struct listen_struct
*listen
;
178 spin_lock_bh(&listen_lock
);
179 for (listen
= listen_list
; listen
!= NULL
; listen
= listen
->next
)
180 if (ax25cmp(&listen
->callsign
, callsign
) == 0 &&
181 (listen
->dev
== dev
|| listen
->dev
== NULL
)) {
182 spin_unlock_bh(&listen_lock
);
185 spin_unlock_bh(&listen_lock
);
190 void ax25_link_failed(ax25_cb
*ax25
, int reason
)
192 struct ax25_linkfail
*lf
;
194 spin_lock_bh(&linkfail_lock
);
195 hlist_for_each_entry(lf
, &ax25_linkfail_list
, lf_node
)
196 lf
->func(ax25
, reason
);
197 spin_unlock_bh(&linkfail_lock
);
200 int ax25_protocol_is_registered(unsigned int pid
)
202 struct ax25_protocol
*protocol
;
205 read_lock_bh(&protocol_list_lock
);
206 for (protocol
= protocol_list
; protocol
!= NULL
; protocol
= protocol
->next
)
207 if (protocol
->pid
== pid
) {
211 read_unlock_bh(&protocol_list_lock
);