blob: 60b545e2822acee4e3a30a0cfd53ccb564dadd53 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
6 *
7 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/errno.h>
10#include <linux/types.h>
11#include <linux/socket.h>
12#include <linux/in.h>
13#include <linux/kernel.h>
Ralf Baechle70868ea2006-05-03 23:25:17 -070014#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/spinlock.h>
16#include <linux/timer.h>
17#include <linux/string.h>
18#include <linux/sockios.h>
19#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
31
Ralf Baechle8d5cf592006-12-14 15:50:01 -080032static struct ax25_protocol *protocol_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static DEFINE_RWLOCK(protocol_list_lock);
34
Ralf Baechlea4282712006-12-14 15:51:23 -080035static HLIST_HEAD(ax25_linkfail_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static DEFINE_SPINLOCK(linkfail_lock);
37
38static struct listen_struct {
39 struct listen_struct *next;
40 ax25_address callsign;
41 struct net_device *dev;
42} *listen_list = NULL;
43static DEFINE_SPINLOCK(listen_lock);
44
Ralf Baechle8d5cf592006-12-14 15:50:01 -080045/*
46 * Do not register the internal protocols AX25_P_TEXT, AX25_P_SEGMENT,
47 * AX25_P_IP or AX25_P_ARP ...
48 */
49void ax25_register_pid(struct ax25_protocol *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Ralf Baechle95ff9f42006-07-10 16:21:29 -070051 write_lock_bh(&protocol_list_lock);
Ralf Baechle8d5cf592006-12-14 15:50:01 -080052 ap->next = protocol_list;
53 protocol_list = ap;
Ralf Baechle95ff9f42006-07-10 16:21:29 -070054 write_unlock_bh(&protocol_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Ralf Baechle8d5cf592006-12-14 15:50:01 -080057EXPORT_SYMBOL_GPL(ax25_register_pid);
Ralf Baechle70868ea2006-05-03 23:25:17 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059void ax25_protocol_release(unsigned int pid)
60{
David S. Millerd8f969e2011-04-17 00:46:45 -070061 struct ax25_protocol *protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Ralf Baechle95ff9f42006-07-10 16:21:29 -070063 write_lock_bh(&protocol_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 protocol = protocol_list;
Ilpo Järvinen910d30b2009-02-06 23:47:14 -080065 if (protocol == NULL)
66 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 if (protocol->pid == pid) {
69 protocol_list = protocol->next;
Ilpo Järvinen910d30b2009-02-06 23:47:14 -080070 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
72
73 while (protocol != NULL && protocol->next != NULL) {
74 if (protocol->next->pid == pid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 protocol->next = protocol->next->next;
Ilpo Järvinen910d30b2009-02-06 23:47:14 -080076 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
78
79 protocol = protocol->next;
80 }
Ilpo Järvinen910d30b2009-02-06 23:47:14 -080081out:
Ralf Baechle95ff9f42006-07-10 16:21:29 -070082 write_unlock_bh(&protocol_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Ralf Baechle70868ea2006-05-03 23:25:17 -070085EXPORT_SYMBOL(ax25_protocol_release);
86
Ralf Baechlea4282712006-12-14 15:51:23 -080087void ax25_linkfail_register(struct ax25_linkfail *lf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 spin_lock_bh(&linkfail_lock);
Ralf Baechlea4282712006-12-14 15:51:23 -080090 hlist_add_head(&lf->lf_node, &ax25_linkfail_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 spin_unlock_bh(&linkfail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Ralf Baechle70868ea2006-05-03 23:25:17 -070094EXPORT_SYMBOL(ax25_linkfail_register);
95
Ralf Baechlea4282712006-12-14 15:51:23 -080096void ax25_linkfail_release(struct ax25_linkfail *lf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 spin_lock_bh(&linkfail_lock);
Ralf Baechlea4282712006-12-14 15:51:23 -080099 hlist_del_init(&lf->lf_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 spin_unlock_bh(&linkfail_lock);
101}
102
Ralf Baechle70868ea2006-05-03 23:25:17 -0700103EXPORT_SYMBOL(ax25_linkfail_release);
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105int ax25_listen_register(ax25_address *callsign, struct net_device *dev)
106{
107 struct listen_struct *listen;
108
109 if (ax25_listen_mine(callsign, dev))
110 return 0;
111
112 if ((listen = kmalloc(sizeof(*listen), GFP_ATOMIC)) == NULL)
Ralf Baechle81dcd162006-12-14 15:50:34 -0800113 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 listen->callsign = *callsign;
116 listen->dev = dev;
117
118 spin_lock_bh(&listen_lock);
119 listen->next = listen_list;
120 listen_list = listen;
121 spin_unlock_bh(&listen_lock);
122
Ralf Baechle81dcd162006-12-14 15:50:34 -0800123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Ralf Baechle70868ea2006-05-03 23:25:17 -0700126EXPORT_SYMBOL(ax25_listen_register);
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128void ax25_listen_release(ax25_address *callsign, struct net_device *dev)
129{
130 struct listen_struct *s, *listen;
131
132 spin_lock_bh(&listen_lock);
133 listen = listen_list;
134 if (listen == NULL) {
135 spin_unlock_bh(&listen_lock);
136 return;
137 }
138
139 if (ax25cmp(&listen->callsign, callsign) == 0 && listen->dev == dev) {
140 listen_list = listen->next;
141 spin_unlock_bh(&listen_lock);
142 kfree(listen);
143 return;
144 }
145
146 while (listen != NULL && listen->next != NULL) {
147 if (ax25cmp(&listen->next->callsign, callsign) == 0 && listen->next->dev == dev) {
148 s = listen->next;
149 listen->next = listen->next->next;
150 spin_unlock_bh(&listen_lock);
151 kfree(s);
152 return;
153 }
154
155 listen = listen->next;
156 }
157 spin_unlock_bh(&listen_lock);
158}
159
Ralf Baechle70868ea2006-05-03 23:25:17 -0700160EXPORT_SYMBOL(ax25_listen_release);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162int (*ax25_protocol_function(unsigned int pid))(struct sk_buff *, ax25_cb *)
163{
164 int (*res)(struct sk_buff *, ax25_cb *) = NULL;
Ralf Baechle8d5cf592006-12-14 15:50:01 -0800165 struct ax25_protocol *protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 read_lock(&protocol_list_lock);
168 for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
169 if (protocol->pid == pid) {
170 res = protocol->func;
171 break;
172 }
173 read_unlock(&protocol_list_lock);
174
175 return res;
176}
177
178int ax25_listen_mine(ax25_address *callsign, struct net_device *dev)
179{
180 struct listen_struct *listen;
181
182 spin_lock_bh(&listen_lock);
183 for (listen = listen_list; listen != NULL; listen = listen->next)
Ralf Baechle81dcd162006-12-14 15:50:34 -0800184 if (ax25cmp(&listen->callsign, callsign) == 0 &&
185 (listen->dev == dev || listen->dev == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 spin_unlock_bh(&listen_lock);
187 return 1;
188 }
189 spin_unlock_bh(&listen_lock);
190
191 return 0;
192}
193
194void ax25_link_failed(ax25_cb *ax25, int reason)
195{
Ralf Baechlea4282712006-12-14 15:51:23 -0800196 struct ax25_linkfail *lf;
197 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 spin_lock_bh(&linkfail_lock);
Ralf Baechlea4282712006-12-14 15:51:23 -0800200 hlist_for_each_entry(lf, node, &ax25_linkfail_list, lf_node)
201 lf->func(ax25, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 spin_unlock_bh(&linkfail_lock);
203}
204
205int ax25_protocol_is_registered(unsigned int pid)
206{
Ralf Baechle8d5cf592006-12-14 15:50:01 -0800207 struct ax25_protocol *protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 int res = 0;
209
Ralf Baechle95ff9f42006-07-10 16:21:29 -0700210 read_lock_bh(&protocol_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
212 if (protocol->pid == pid) {
213 res = 1;
214 break;
215 }
Ralf Baechle95ff9f42006-07-10 16:21:29 -0700216 read_unlock_bh(&protocol_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 return res;
219}