blob: 8c07c28569e47566f5c3cb21abcef4f0d47eae9c [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>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080026#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/fcntl.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
30
Ralf Baechle8d5cf592006-12-14 15:50:01 -080031static struct ax25_protocol *protocol_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static DEFINE_RWLOCK(protocol_list_lock);
33
Ralf Baechlea4282712006-12-14 15:51:23 -080034static HLIST_HEAD(ax25_linkfail_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static DEFINE_SPINLOCK(linkfail_lock);
36
37static struct listen_struct {
38 struct listen_struct *next;
39 ax25_address callsign;
40 struct net_device *dev;
41} *listen_list = NULL;
42static DEFINE_SPINLOCK(listen_lock);
43
Ralf Baechle8d5cf592006-12-14 15:50:01 -080044/*
45 * Do not register the internal protocols AX25_P_TEXT, AX25_P_SEGMENT,
46 * AX25_P_IP or AX25_P_ARP ...
47 */
48void ax25_register_pid(struct ax25_protocol *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Ralf Baechle95ff9f42006-07-10 16:21:29 -070050 write_lock_bh(&protocol_list_lock);
Ralf Baechle8d5cf592006-12-14 15:50:01 -080051 ap->next = protocol_list;
52 protocol_list = ap;
Ralf Baechle95ff9f42006-07-10 16:21:29 -070053 write_unlock_bh(&protocol_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Ralf Baechle8d5cf592006-12-14 15:50:01 -080056EXPORT_SYMBOL_GPL(ax25_register_pid);
Ralf Baechle70868ea2006-05-03 23:25:17 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058void ax25_protocol_release(unsigned int pid)
59{
David S. Millerd8f969e2011-04-17 00:46:45 -070060 struct ax25_protocol *protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Ralf Baechle95ff9f42006-07-10 16:21:29 -070062 write_lock_bh(&protocol_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 protocol = protocol_list;
Ilpo Järvinen910d30b2009-02-06 23:47:14 -080064 if (protocol == NULL)
65 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 if (protocol->pid == pid) {
68 protocol_list = protocol->next;
Ilpo Järvinen910d30b2009-02-06 23:47:14 -080069 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71
72 while (protocol != NULL && protocol->next != NULL) {
73 if (protocol->next->pid == pid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 protocol->next = protocol->next->next;
Ilpo Järvinen910d30b2009-02-06 23:47:14 -080075 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77
78 protocol = protocol->next;
79 }
Ilpo Järvinen910d30b2009-02-06 23:47:14 -080080out:
Ralf Baechle95ff9f42006-07-10 16:21:29 -070081 write_unlock_bh(&protocol_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Ralf Baechle70868ea2006-05-03 23:25:17 -070084EXPORT_SYMBOL(ax25_protocol_release);
85
Ralf Baechlea4282712006-12-14 15:51:23 -080086void ax25_linkfail_register(struct ax25_linkfail *lf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 spin_lock_bh(&linkfail_lock);
Ralf Baechlea4282712006-12-14 15:51:23 -080089 hlist_add_head(&lf->lf_node, &ax25_linkfail_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 spin_unlock_bh(&linkfail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Ralf Baechle70868ea2006-05-03 23:25:17 -070093EXPORT_SYMBOL(ax25_linkfail_register);
94
Ralf Baechlea4282712006-12-14 15:51:23 -080095void ax25_linkfail_release(struct ax25_linkfail *lf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 spin_lock_bh(&linkfail_lock);
Ralf Baechlea4282712006-12-14 15:51:23 -080098 hlist_del_init(&lf->lf_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 spin_unlock_bh(&linkfail_lock);
100}
101
Ralf Baechle70868ea2006-05-03 23:25:17 -0700102EXPORT_SYMBOL(ax25_linkfail_release);
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104int ax25_listen_register(ax25_address *callsign, struct net_device *dev)
105{
106 struct listen_struct *listen;
107
108 if (ax25_listen_mine(callsign, dev))
109 return 0;
110
111 if ((listen = kmalloc(sizeof(*listen), GFP_ATOMIC)) == NULL)
Ralf Baechle81dcd162006-12-14 15:50:34 -0800112 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 listen->callsign = *callsign;
115 listen->dev = dev;
116
117 spin_lock_bh(&listen_lock);
118 listen->next = listen_list;
119 listen_list = listen;
120 spin_unlock_bh(&listen_lock);
121
Ralf Baechle81dcd162006-12-14 15:50:34 -0800122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Ralf Baechle70868ea2006-05-03 23:25:17 -0700125EXPORT_SYMBOL(ax25_listen_register);
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127void ax25_listen_release(ax25_address *callsign, struct net_device *dev)
128{
129 struct listen_struct *s, *listen;
130
131 spin_lock_bh(&listen_lock);
132 listen = listen_list;
133 if (listen == NULL) {
134 spin_unlock_bh(&listen_lock);
135 return;
136 }
137
138 if (ax25cmp(&listen->callsign, callsign) == 0 && listen->dev == dev) {
139 listen_list = listen->next;
140 spin_unlock_bh(&listen_lock);
141 kfree(listen);
142 return;
143 }
144
145 while (listen != NULL && listen->next != NULL) {
146 if (ax25cmp(&listen->next->callsign, callsign) == 0 && listen->next->dev == dev) {
147 s = listen->next;
148 listen->next = listen->next->next;
149 spin_unlock_bh(&listen_lock);
150 kfree(s);
151 return;
152 }
153
154 listen = listen->next;
155 }
156 spin_unlock_bh(&listen_lock);
157}
158
Ralf Baechle70868ea2006-05-03 23:25:17 -0700159EXPORT_SYMBOL(ax25_listen_release);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161int (*ax25_protocol_function(unsigned int pid))(struct sk_buff *, ax25_cb *)
162{
163 int (*res)(struct sk_buff *, ax25_cb *) = NULL;
Ralf Baechle8d5cf592006-12-14 15:50:01 -0800164 struct ax25_protocol *protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 read_lock(&protocol_list_lock);
167 for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
168 if (protocol->pid == pid) {
169 res = protocol->func;
170 break;
171 }
172 read_unlock(&protocol_list_lock);
173
174 return res;
175}
176
177int ax25_listen_mine(ax25_address *callsign, struct net_device *dev)
178{
179 struct listen_struct *listen;
180
181 spin_lock_bh(&listen_lock);
182 for (listen = listen_list; listen != NULL; listen = listen->next)
Ralf Baechle81dcd162006-12-14 15:50:34 -0800183 if (ax25cmp(&listen->callsign, callsign) == 0 &&
184 (listen->dev == dev || listen->dev == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 spin_unlock_bh(&listen_lock);
186 return 1;
187 }
188 spin_unlock_bh(&listen_lock);
189
190 return 0;
191}
192
193void ax25_link_failed(ax25_cb *ax25, int reason)
194{
Ralf Baechlea4282712006-12-14 15:51:23 -0800195 struct ax25_linkfail *lf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 spin_lock_bh(&linkfail_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800198 hlist_for_each_entry(lf, &ax25_linkfail_list, lf_node)
Ralf Baechlea4282712006-12-14 15:51:23 -0800199 lf->func(ax25, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 spin_unlock_bh(&linkfail_lock);
201}
202
203int ax25_protocol_is_registered(unsigned int pid)
204{
Ralf Baechle8d5cf592006-12-14 15:50:01 -0800205 struct ax25_protocol *protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 int res = 0;
207
Ralf Baechle95ff9f42006-07-10 16:21:29 -0700208 read_lock_bh(&protocol_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
210 if (protocol->pid == pid) {
211 res = 1;
212 break;
213 }
Ralf Baechle95ff9f42006-07-10 16:21:29 -0700214 read_unlock_bh(&protocol_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 return res;
217}