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/errno.h>
10 #include <linux/types.h>
11 #include <linux/socket.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/spinlock.h>
17 #include <linux/timer.h>
18 #include <linux/string.h>
19 #include <linux/sockios.h>
20 #include <linux/net.h>
22 #include <linux/inet.h>
23 #include <linux/netdevice.h>
24 #include <linux/skbuff.h>
26 #include <asm/uaccess.h>
27 #include <asm/system.h>
28 #include <linux/fcntl.h>
30 #include <linux/interrupt.h>
32 static struct protocol_struct {
33 struct protocol_struct *next;
35 int (*func)(struct sk_buff *, ax25_cb *);
36 } *protocol_list = NULL;
37 static DEFINE_RWLOCK(protocol_list_lock);
39 static struct linkfail_struct {
40 struct linkfail_struct *next;
41 void (*func)(ax25_cb *, int);
42 } *linkfail_list = NULL;
43 static DEFINE_SPINLOCK(linkfail_lock);
45 static struct listen_struct {
46 struct listen_struct *next;
47 ax25_address callsign;
48 struct net_device *dev;
49 } *listen_list = NULL;
50 static DEFINE_SPINLOCK(listen_lock);
52 int ax25_protocol_register(unsigned int pid,
53 int (*func)(struct sk_buff *, ax25_cb *))
55 struct protocol_struct *protocol;
57 if (pid == AX25_P_TEXT || pid == AX25_P_SEGMENT)
60 if (pid == AX25_P_IP || pid == AX25_P_ARP)
63 if ((protocol = kmalloc(sizeof(*protocol), GFP_ATOMIC)) == NULL)
67 protocol->func = func;
69 write_lock_bh(&protocol_list_lock);
70 protocol->next = protocol_list;
71 protocol_list = protocol;
72 write_unlock_bh(&protocol_list_lock);
77 EXPORT_SYMBOL(ax25_protocol_register);
79 void ax25_protocol_release(unsigned int pid)
81 struct protocol_struct *s, *protocol;
83 write_lock_bh(&protocol_list_lock);
84 protocol = protocol_list;
85 if (protocol == NULL) {
86 write_unlock_bh(&protocol_list_lock);
90 if (protocol->pid == pid) {
91 protocol_list = protocol->next;
92 write_unlock_bh(&protocol_list_lock);
97 while (protocol != NULL && protocol->next != NULL) {
98 if (protocol->next->pid == pid) {
100 protocol->next = protocol->next->next;
101 write_unlock_bh(&protocol_list_lock);
106 protocol = protocol->next;
108 write_unlock_bh(&protocol_list_lock);
111 EXPORT_SYMBOL(ax25_protocol_release);
113 int ax25_linkfail_register(void (*func)(ax25_cb *, int))
115 struct linkfail_struct *linkfail;
117 if ((linkfail = kmalloc(sizeof(*linkfail), GFP_ATOMIC)) == NULL)
120 linkfail->func = func;
122 spin_lock_bh(&linkfail_lock);
123 linkfail->next = linkfail_list;
124 linkfail_list = linkfail;
125 spin_unlock_bh(&linkfail_lock);
130 EXPORT_SYMBOL(ax25_linkfail_register);
132 void ax25_linkfail_release(void (*func)(ax25_cb *, int))
134 struct linkfail_struct *s, *linkfail;
136 spin_lock_bh(&linkfail_lock);
137 linkfail = linkfail_list;
138 if (linkfail == NULL) {
139 spin_unlock_bh(&linkfail_lock);
143 if (linkfail->func == func) {
144 linkfail_list = linkfail->next;
145 spin_unlock_bh(&linkfail_lock);
150 while (linkfail != NULL && linkfail->next != NULL) {
151 if (linkfail->next->func == func) {
153 linkfail->next = linkfail->next->next;
154 spin_unlock_bh(&linkfail_lock);
159 linkfail = linkfail->next;
161 spin_unlock_bh(&linkfail_lock);
164 EXPORT_SYMBOL(ax25_linkfail_release);
166 int ax25_listen_register(ax25_address *callsign, struct net_device *dev)
168 struct listen_struct *listen;
170 if (ax25_listen_mine(callsign, dev))
173 if ((listen = kmalloc(sizeof(*listen), GFP_ATOMIC)) == NULL)
176 listen->callsign = *callsign;
179 spin_lock_bh(&listen_lock);
180 listen->next = listen_list;
181 listen_list = listen;
182 spin_unlock_bh(&listen_lock);
187 EXPORT_SYMBOL(ax25_listen_register);
189 void ax25_listen_release(ax25_address *callsign, struct net_device *dev)
191 struct listen_struct *s, *listen;
193 spin_lock_bh(&listen_lock);
194 listen = listen_list;
195 if (listen == NULL) {
196 spin_unlock_bh(&listen_lock);
200 if (ax25cmp(&listen->callsign, callsign) == 0 && listen->dev == dev) {
201 listen_list = listen->next;
202 spin_unlock_bh(&listen_lock);
207 while (listen != NULL && listen->next != NULL) {
208 if (ax25cmp(&listen->next->callsign, callsign) == 0 && listen->next->dev == dev) {
210 listen->next = listen->next->next;
211 spin_unlock_bh(&listen_lock);
216 listen = listen->next;
218 spin_unlock_bh(&listen_lock);
221 EXPORT_SYMBOL(ax25_listen_release);
223 int (*ax25_protocol_function(unsigned int pid))(struct sk_buff *, ax25_cb *)
225 int (*res)(struct sk_buff *, ax25_cb *) = NULL;
226 struct protocol_struct *protocol;
228 read_lock(&protocol_list_lock);
229 for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
230 if (protocol->pid == pid) {
231 res = protocol->func;
234 read_unlock(&protocol_list_lock);
239 int ax25_listen_mine(ax25_address *callsign, struct net_device *dev)
241 struct listen_struct *listen;
243 spin_lock_bh(&listen_lock);
244 for (listen = listen_list; listen != NULL; listen = listen->next)
245 if (ax25cmp(&listen->callsign, callsign) == 0 && (listen->dev == dev || listen->dev == NULL)) {
246 spin_unlock_bh(&listen_lock);
249 spin_unlock_bh(&listen_lock);
254 void ax25_link_failed(ax25_cb *ax25, int reason)
256 struct linkfail_struct *linkfail;
258 spin_lock_bh(&linkfail_lock);
259 for (linkfail = linkfail_list; linkfail != NULL; linkfail = linkfail->next)
260 (linkfail->func)(ax25, reason);
261 spin_unlock_bh(&linkfail_lock);
264 int ax25_protocol_is_registered(unsigned int pid)
266 struct protocol_struct *protocol;
269 read_lock_bh(&protocol_list_lock);
270 for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
271 if (protocol->pid == pid) {
275 read_unlock_bh(&protocol_list_lock);