[AX.25]: Fix unchecked ax25_listen_register uses
[linux-2.6/verdex.git] / net / ax25 / ax25_iface.c
blob51e293420b7f90fc8c9dbf5df0cee13ee64383be
1 /*
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)
8 */
9 #include <linux/errno.h>
10 #include <linux/types.h>
11 #include <linux/socket.h>
12 #include <linux/in.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>
21 #include <net/ax25.h>
22 #include <linux/inet.h>
23 #include <linux/netdevice.h>
24 #include <linux/skbuff.h>
25 #include <net/sock.h>
26 #include <asm/uaccess.h>
27 #include <asm/system.h>
28 #include <linux/fcntl.h>
29 #include <linux/mm.h>
30 #include <linux/interrupt.h>
32 static struct ax25_protocol *protocol_list;
33 static DEFINE_RWLOCK(protocol_list_lock);
35 static struct linkfail_struct {
36 struct linkfail_struct *next;
37 void (*func)(ax25_cb *, int);
38 } *linkfail_list = NULL;
39 static DEFINE_SPINLOCK(linkfail_lock);
41 static struct listen_struct {
42 struct listen_struct *next;
43 ax25_address callsign;
44 struct net_device *dev;
45 } *listen_list = NULL;
46 static DEFINE_SPINLOCK(listen_lock);
49 * Do not register the internal protocols AX25_P_TEXT, AX25_P_SEGMENT,
50 * AX25_P_IP or AX25_P_ARP ...
52 void ax25_register_pid(struct ax25_protocol *ap)
54 write_lock_bh(&protocol_list_lock);
55 ap->next = protocol_list;
56 protocol_list = ap;
57 write_unlock_bh(&protocol_list_lock);
60 EXPORT_SYMBOL_GPL(ax25_register_pid);
62 void ax25_protocol_release(unsigned int pid)
64 struct ax25_protocol *s, *protocol;
66 write_lock_bh(&protocol_list_lock);
67 protocol = protocol_list;
68 if (protocol == NULL) {
69 write_unlock_bh(&protocol_list_lock);
70 return;
73 if (protocol->pid == pid) {
74 protocol_list = protocol->next;
75 write_unlock_bh(&protocol_list_lock);
76 kfree(protocol);
77 return;
80 while (protocol != NULL && protocol->next != NULL) {
81 if (protocol->next->pid == pid) {
82 s = protocol->next;
83 protocol->next = protocol->next->next;
84 write_unlock_bh(&protocol_list_lock);
85 kfree(s);
86 return;
89 protocol = protocol->next;
91 write_unlock_bh(&protocol_list_lock);
94 EXPORT_SYMBOL(ax25_protocol_release);
96 int ax25_linkfail_register(void (*func)(ax25_cb *, int))
98 struct linkfail_struct *linkfail;
100 if ((linkfail = kmalloc(sizeof(*linkfail), GFP_ATOMIC)) == NULL)
101 return 0;
103 linkfail->func = func;
105 spin_lock_bh(&linkfail_lock);
106 linkfail->next = linkfail_list;
107 linkfail_list = linkfail;
108 spin_unlock_bh(&linkfail_lock);
110 return 1;
113 EXPORT_SYMBOL(ax25_linkfail_register);
115 void ax25_linkfail_release(void (*func)(ax25_cb *, int))
117 struct linkfail_struct *s, *linkfail;
119 spin_lock_bh(&linkfail_lock);
120 linkfail = linkfail_list;
121 if (linkfail == NULL) {
122 spin_unlock_bh(&linkfail_lock);
123 return;
126 if (linkfail->func == func) {
127 linkfail_list = linkfail->next;
128 spin_unlock_bh(&linkfail_lock);
129 kfree(linkfail);
130 return;
133 while (linkfail != NULL && linkfail->next != NULL) {
134 if (linkfail->next->func == func) {
135 s = linkfail->next;
136 linkfail->next = linkfail->next->next;
137 spin_unlock_bh(&linkfail_lock);
138 kfree(s);
139 return;
142 linkfail = linkfail->next;
144 spin_unlock_bh(&linkfail_lock);
147 EXPORT_SYMBOL(ax25_linkfail_release);
149 int ax25_listen_register(ax25_address *callsign, struct net_device *dev)
151 struct listen_struct *listen;
153 if (ax25_listen_mine(callsign, dev))
154 return 0;
156 if ((listen = kmalloc(sizeof(*listen), GFP_ATOMIC)) == NULL)
157 return -ENOMEM;
159 listen->callsign = *callsign;
160 listen->dev = dev;
162 spin_lock_bh(&listen_lock);
163 listen->next = listen_list;
164 listen_list = listen;
165 spin_unlock_bh(&listen_lock);
167 return 0;
170 EXPORT_SYMBOL(ax25_listen_register);
172 void ax25_listen_release(ax25_address *callsign, struct net_device *dev)
174 struct listen_struct *s, *listen;
176 spin_lock_bh(&listen_lock);
177 listen = listen_list;
178 if (listen == NULL) {
179 spin_unlock_bh(&listen_lock);
180 return;
183 if (ax25cmp(&listen->callsign, callsign) == 0 && listen->dev == dev) {
184 listen_list = listen->next;
185 spin_unlock_bh(&listen_lock);
186 kfree(listen);
187 return;
190 while (listen != NULL && listen->next != NULL) {
191 if (ax25cmp(&listen->next->callsign, callsign) == 0 && listen->next->dev == dev) {
192 s = listen->next;
193 listen->next = listen->next->next;
194 spin_unlock_bh(&listen_lock);
195 kfree(s);
196 return;
199 listen = listen->next;
201 spin_unlock_bh(&listen_lock);
204 EXPORT_SYMBOL(ax25_listen_release);
206 int (*ax25_protocol_function(unsigned int pid))(struct sk_buff *, ax25_cb *)
208 int (*res)(struct sk_buff *, ax25_cb *) = NULL;
209 struct ax25_protocol *protocol;
211 read_lock(&protocol_list_lock);
212 for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
213 if (protocol->pid == pid) {
214 res = protocol->func;
215 break;
217 read_unlock(&protocol_list_lock);
219 return res;
222 int ax25_listen_mine(ax25_address *callsign, struct net_device *dev)
224 struct listen_struct *listen;
226 spin_lock_bh(&listen_lock);
227 for (listen = listen_list; listen != NULL; listen = listen->next)
228 if (ax25cmp(&listen->callsign, callsign) == 0 &&
229 (listen->dev == dev || listen->dev == NULL)) {
230 spin_unlock_bh(&listen_lock);
231 return 1;
233 spin_unlock_bh(&listen_lock);
235 return 0;
238 void ax25_link_failed(ax25_cb *ax25, int reason)
240 struct linkfail_struct *linkfail;
242 spin_lock_bh(&linkfail_lock);
243 for (linkfail = linkfail_list; linkfail != NULL; linkfail = linkfail->next)
244 (linkfail->func)(ax25, reason);
245 spin_unlock_bh(&linkfail_lock);
248 int ax25_protocol_is_registered(unsigned int pid)
250 struct ax25_protocol *protocol;
251 int res = 0;
253 read_lock_bh(&protocol_list_lock);
254 for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
255 if (protocol->pid == pid) {
256 res = 1;
257 break;
259 read_unlock_bh(&protocol_list_lock);
261 return res;