2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
9 #include <linux/config.h>
10 #include <linux/errno.h>
11 #include <linux/types.h>
12 #include <linux/socket.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/spinlock.h>
18 #include <linux/timer.h>
19 #include <linux/string.h>
20 #include <linux/sockios.h>
21 #include <linux/net.h>
23 #include <linux/inet.h>
24 #include <linux/netdevice.h>
25 #include <linux/skbuff.h>
27 #include <asm/uaccess.h>
28 #include <asm/system.h>
29 #include <linux/fcntl.h>
31 #include <linux/interrupt.h>
33 static struct protocol_struct
{
34 struct protocol_struct
*next
;
36 int (*func
)(struct sk_buff
*, ax25_cb
*);
37 } *protocol_list
= NULL
;
38 static DEFINE_RWLOCK(protocol_list_lock
);
40 static struct linkfail_struct
{
41 struct linkfail_struct
*next
;
42 void (*func
)(ax25_cb
*, int);
43 } *linkfail_list
= NULL
;
44 static DEFINE_SPINLOCK(linkfail_lock
);
46 static struct listen_struct
{
47 struct listen_struct
*next
;
48 ax25_address callsign
;
49 struct net_device
*dev
;
50 } *listen_list
= NULL
;
51 static DEFINE_SPINLOCK(listen_lock
);
53 int ax25_protocol_register(unsigned int pid
,
54 int (*func
)(struct sk_buff
*, ax25_cb
*))
56 struct protocol_struct
*protocol
;
58 if (pid
== AX25_P_TEXT
|| pid
== AX25_P_SEGMENT
)
61 if (pid
== AX25_P_IP
|| pid
== AX25_P_ARP
)
64 if ((protocol
= kmalloc(sizeof(*protocol
), GFP_ATOMIC
)) == NULL
)
68 protocol
->func
= func
;
70 write_lock(&protocol_list_lock
);
71 protocol
->next
= protocol_list
;
72 protocol_list
= protocol
;
73 write_unlock(&protocol_list_lock
);
78 EXPORT_SYMBOL(ax25_protocol_register
);
80 void ax25_protocol_release(unsigned int pid
)
82 struct protocol_struct
*s
, *protocol
;
84 write_lock(&protocol_list_lock
);
85 protocol
= protocol_list
;
86 if (protocol
== NULL
) {
87 write_unlock(&protocol_list_lock
);
91 if (protocol
->pid
== pid
) {
92 protocol_list
= protocol
->next
;
93 write_unlock(&protocol_list_lock
);
98 while (protocol
!= NULL
&& protocol
->next
!= NULL
) {
99 if (protocol
->next
->pid
== pid
) {
101 protocol
->next
= protocol
->next
->next
;
102 write_unlock(&protocol_list_lock
);
107 protocol
= protocol
->next
;
109 write_unlock(&protocol_list_lock
);
112 EXPORT_SYMBOL(ax25_protocol_release
);
114 int ax25_linkfail_register(void (*func
)(ax25_cb
*, int))
116 struct linkfail_struct
*linkfail
;
118 if ((linkfail
= kmalloc(sizeof(*linkfail
), GFP_ATOMIC
)) == NULL
)
121 linkfail
->func
= func
;
123 spin_lock_bh(&linkfail_lock
);
124 linkfail
->next
= linkfail_list
;
125 linkfail_list
= linkfail
;
126 spin_unlock_bh(&linkfail_lock
);
131 EXPORT_SYMBOL(ax25_linkfail_register
);
133 void ax25_linkfail_release(void (*func
)(ax25_cb
*, int))
135 struct linkfail_struct
*s
, *linkfail
;
137 spin_lock_bh(&linkfail_lock
);
138 linkfail
= linkfail_list
;
139 if (linkfail
== NULL
) {
140 spin_unlock_bh(&linkfail_lock
);
144 if (linkfail
->func
== func
) {
145 linkfail_list
= linkfail
->next
;
146 spin_unlock_bh(&linkfail_lock
);
151 while (linkfail
!= NULL
&& linkfail
->next
!= NULL
) {
152 if (linkfail
->next
->func
== func
) {
154 linkfail
->next
= linkfail
->next
->next
;
155 spin_unlock_bh(&linkfail_lock
);
160 linkfail
= linkfail
->next
;
162 spin_unlock_bh(&linkfail_lock
);
165 EXPORT_SYMBOL(ax25_linkfail_release
);
167 int ax25_listen_register(ax25_address
*callsign
, struct net_device
*dev
)
169 struct listen_struct
*listen
;
171 if (ax25_listen_mine(callsign
, dev
))
174 if ((listen
= kmalloc(sizeof(*listen
), GFP_ATOMIC
)) == NULL
)
177 listen
->callsign
= *callsign
;
180 spin_lock_bh(&listen_lock
);
181 listen
->next
= listen_list
;
182 listen_list
= listen
;
183 spin_unlock_bh(&listen_lock
);
188 EXPORT_SYMBOL(ax25_listen_register
);
190 void ax25_listen_release(ax25_address
*callsign
, struct net_device
*dev
)
192 struct listen_struct
*s
, *listen
;
194 spin_lock_bh(&listen_lock
);
195 listen
= listen_list
;
196 if (listen
== NULL
) {
197 spin_unlock_bh(&listen_lock
);
201 if (ax25cmp(&listen
->callsign
, callsign
) == 0 && listen
->dev
== dev
) {
202 listen_list
= listen
->next
;
203 spin_unlock_bh(&listen_lock
);
208 while (listen
!= NULL
&& listen
->next
!= NULL
) {
209 if (ax25cmp(&listen
->next
->callsign
, callsign
) == 0 && listen
->next
->dev
== dev
) {
211 listen
->next
= listen
->next
->next
;
212 spin_unlock_bh(&listen_lock
);
217 listen
= listen
->next
;
219 spin_unlock_bh(&listen_lock
);
222 EXPORT_SYMBOL(ax25_listen_release
);
224 int (*ax25_protocol_function(unsigned int pid
))(struct sk_buff
*, ax25_cb
*)
226 int (*res
)(struct sk_buff
*, ax25_cb
*) = NULL
;
227 struct protocol_struct
*protocol
;
229 read_lock(&protocol_list_lock
);
230 for (protocol
= protocol_list
; protocol
!= NULL
; protocol
= protocol
->next
)
231 if (protocol
->pid
== pid
) {
232 res
= protocol
->func
;
235 read_unlock(&protocol_list_lock
);
240 int ax25_listen_mine(ax25_address
*callsign
, struct net_device
*dev
)
242 struct listen_struct
*listen
;
244 spin_lock_bh(&listen_lock
);
245 for (listen
= listen_list
; listen
!= NULL
; listen
= listen
->next
)
246 if (ax25cmp(&listen
->callsign
, callsign
) == 0 && (listen
->dev
== dev
|| listen
->dev
== NULL
)) {
247 spin_unlock_bh(&listen_lock
);
250 spin_unlock_bh(&listen_lock
);
255 void ax25_link_failed(ax25_cb
*ax25
, int reason
)
257 struct linkfail_struct
*linkfail
;
259 spin_lock_bh(&linkfail_lock
);
260 for (linkfail
= linkfail_list
; linkfail
!= NULL
; linkfail
= linkfail
->next
)
261 (linkfail
->func
)(ax25
, reason
);
262 spin_unlock_bh(&linkfail_lock
);
265 int ax25_protocol_is_registered(unsigned int pid
)
267 struct protocol_struct
*protocol
;
270 read_lock(&protocol_list_lock
);
271 for (protocol
= protocol_list
; protocol
!= NULL
; protocol
= protocol
->next
)
272 if (protocol
->pid
== pid
) {
276 read_unlock(&protocol_list_lock
);