Loading...
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.
6 *
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/spinlock.h>
16#include <linux/timer.h>
17#include <linux/string.h>
18#include <linux/sockios.h>
19#include <linux/net.h>
20#include <linux/slab.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>
31
32static struct ax25_protocol *protocol_list;
33static DEFINE_RWLOCK(protocol_list_lock);
34
35static HLIST_HEAD(ax25_linkfail_list);
36static 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
45/*
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)
50{
51 write_lock_bh(&protocol_list_lock);
52 ap->next = protocol_list;
53 protocol_list = ap;
54 write_unlock_bh(&protocol_list_lock);
55}
56
57EXPORT_SYMBOL_GPL(ax25_register_pid);
58
59void ax25_protocol_release(unsigned int pid)
60{
61 struct ax25_protocol *protocol;
62
63 write_lock_bh(&protocol_list_lock);
64 protocol = protocol_list;
65 if (protocol == NULL)
66 goto out;
67
68 if (protocol->pid == pid) {
69 protocol_list = protocol->next;
70 goto out;
71 }
72
73 while (protocol != NULL && protocol->next != NULL) {
74 if (protocol->next->pid == pid) {
75 protocol->next = protocol->next->next;
76 goto out;
77 }
78
79 protocol = protocol->next;
80 }
81out:
82 write_unlock_bh(&protocol_list_lock);
83}
84
85EXPORT_SYMBOL(ax25_protocol_release);
86
87void ax25_linkfail_register(struct ax25_linkfail *lf)
88{
89 spin_lock_bh(&linkfail_lock);
90 hlist_add_head(&lf->lf_node, &ax25_linkfail_list);
91 spin_unlock_bh(&linkfail_lock);
92}
93
94EXPORT_SYMBOL(ax25_linkfail_register);
95
96void ax25_linkfail_release(struct ax25_linkfail *lf)
97{
98 spin_lock_bh(&linkfail_lock);
99 hlist_del_init(&lf->lf_node);
100 spin_unlock_bh(&linkfail_lock);
101}
102
103EXPORT_SYMBOL(ax25_linkfail_release);
104
105int 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)
113 return -ENOMEM;
114
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
123 return 0;
124}
125
126EXPORT_SYMBOL(ax25_listen_register);
127
128void 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
160EXPORT_SYMBOL(ax25_listen_release);
161
162int (*ax25_protocol_function(unsigned int pid))(struct sk_buff *, ax25_cb *)
163{
164 int (*res)(struct sk_buff *, ax25_cb *) = NULL;
165 struct ax25_protocol *protocol;
166
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)
184 if (ax25cmp(&listen->callsign, callsign) == 0 &&
185 (listen->dev == dev || listen->dev == NULL)) {
186 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{
196 struct ax25_linkfail *lf;
197 struct hlist_node *node;
198
199 spin_lock_bh(&linkfail_lock);
200 hlist_for_each_entry(lf, node, &ax25_linkfail_list, lf_node)
201 lf->func(ax25, reason);
202 spin_unlock_bh(&linkfail_lock);
203}
204
205int ax25_protocol_is_registered(unsigned int pid)
206{
207 struct ax25_protocol *protocol;
208 int res = 0;
209
210 read_lock_bh(&protocol_list_lock);
211 for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
212 if (protocol->pid == pid) {
213 res = 1;
214 break;
215 }
216 read_unlock_bh(&protocol_list_lock);
217
218 return res;
219}
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 *
4 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
5 */
6#include <linux/errno.h>
7#include <linux/types.h>
8#include <linux/socket.h>
9#include <linux/in.h>
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/spinlock.h>
13#include <linux/timer.h>
14#include <linux/string.h>
15#include <linux/sockios.h>
16#include <linux/net.h>
17#include <linux/slab.h>
18#include <net/ax25.h>
19#include <linux/inet.h>
20#include <linux/netdevice.h>
21#include <linux/skbuff.h>
22#include <net/sock.h>
23#include <linux/uaccess.h>
24#include <linux/fcntl.h>
25#include <linux/mm.h>
26#include <linux/interrupt.h>
27
28static struct ax25_protocol *protocol_list;
29static DEFINE_RWLOCK(protocol_list_lock);
30
31static HLIST_HEAD(ax25_linkfail_list);
32static DEFINE_SPINLOCK(linkfail_lock);
33
34static struct listen_struct {
35 struct listen_struct *next;
36 ax25_address callsign;
37 struct net_device *dev;
38} *listen_list = NULL;
39static DEFINE_SPINLOCK(listen_lock);
40
41/*
42 * Do not register the internal protocols AX25_P_TEXT, AX25_P_SEGMENT,
43 * AX25_P_IP or AX25_P_ARP ...
44 */
45void ax25_register_pid(struct ax25_protocol *ap)
46{
47 write_lock_bh(&protocol_list_lock);
48 ap->next = protocol_list;
49 protocol_list = ap;
50 write_unlock_bh(&protocol_list_lock);
51}
52
53EXPORT_SYMBOL_GPL(ax25_register_pid);
54
55void ax25_protocol_release(unsigned int pid)
56{
57 struct ax25_protocol *protocol;
58
59 write_lock_bh(&protocol_list_lock);
60 protocol = protocol_list;
61 if (protocol == NULL)
62 goto out;
63
64 if (protocol->pid == pid) {
65 protocol_list = protocol->next;
66 goto out;
67 }
68
69 while (protocol != NULL && protocol->next != NULL) {
70 if (protocol->next->pid == pid) {
71 protocol->next = protocol->next->next;
72 goto out;
73 }
74
75 protocol = protocol->next;
76 }
77out:
78 write_unlock_bh(&protocol_list_lock);
79}
80
81EXPORT_SYMBOL(ax25_protocol_release);
82
83void ax25_linkfail_register(struct ax25_linkfail *lf)
84{
85 spin_lock_bh(&linkfail_lock);
86 hlist_add_head(&lf->lf_node, &ax25_linkfail_list);
87 spin_unlock_bh(&linkfail_lock);
88}
89
90EXPORT_SYMBOL(ax25_linkfail_register);
91
92void ax25_linkfail_release(struct ax25_linkfail *lf)
93{
94 spin_lock_bh(&linkfail_lock);
95 hlist_del_init(&lf->lf_node);
96 spin_unlock_bh(&linkfail_lock);
97}
98
99EXPORT_SYMBOL(ax25_linkfail_release);
100
101int ax25_listen_register(const ax25_address *callsign, struct net_device *dev)
102{
103 struct listen_struct *listen;
104
105 if (ax25_listen_mine(callsign, dev))
106 return 0;
107
108 if ((listen = kmalloc(sizeof(*listen), GFP_ATOMIC)) == NULL)
109 return -ENOMEM;
110
111 listen->callsign = *callsign;
112 listen->dev = dev;
113
114 spin_lock_bh(&listen_lock);
115 listen->next = listen_list;
116 listen_list = listen;
117 spin_unlock_bh(&listen_lock);
118
119 return 0;
120}
121
122EXPORT_SYMBOL(ax25_listen_register);
123
124void ax25_listen_release(const ax25_address *callsign, struct net_device *dev)
125{
126 struct listen_struct *s, *listen;
127
128 spin_lock_bh(&listen_lock);
129 listen = listen_list;
130 if (listen == NULL) {
131 spin_unlock_bh(&listen_lock);
132 return;
133 }
134
135 if (ax25cmp(&listen->callsign, callsign) == 0 && listen->dev == dev) {
136 listen_list = listen->next;
137 spin_unlock_bh(&listen_lock);
138 kfree(listen);
139 return;
140 }
141
142 while (listen != NULL && listen->next != NULL) {
143 if (ax25cmp(&listen->next->callsign, callsign) == 0 && listen->next->dev == dev) {
144 s = listen->next;
145 listen->next = listen->next->next;
146 spin_unlock_bh(&listen_lock);
147 kfree(s);
148 return;
149 }
150
151 listen = listen->next;
152 }
153 spin_unlock_bh(&listen_lock);
154}
155
156EXPORT_SYMBOL(ax25_listen_release);
157
158int (*ax25_protocol_function(unsigned int pid))(struct sk_buff *, ax25_cb *)
159{
160 int (*res)(struct sk_buff *, ax25_cb *) = NULL;
161 struct ax25_protocol *protocol;
162
163 read_lock(&protocol_list_lock);
164 for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
165 if (protocol->pid == pid) {
166 res = protocol->func;
167 break;
168 }
169 read_unlock(&protocol_list_lock);
170
171 return res;
172}
173
174int ax25_listen_mine(const ax25_address *callsign, struct net_device *dev)
175{
176 struct listen_struct *listen;
177
178 spin_lock_bh(&listen_lock);
179 for (listen = listen_list; listen != NULL; listen = listen->next)
180 if (ax25cmp(&listen->callsign, callsign) == 0 &&
181 (listen->dev == dev || listen->dev == NULL)) {
182 spin_unlock_bh(&listen_lock);
183 return 1;
184 }
185 spin_unlock_bh(&listen_lock);
186
187 return 0;
188}
189
190void ax25_link_failed(ax25_cb *ax25, int reason)
191{
192 struct ax25_linkfail *lf;
193
194 spin_lock_bh(&linkfail_lock);
195 hlist_for_each_entry(lf, &ax25_linkfail_list, lf_node)
196 lf->func(ax25, reason);
197 spin_unlock_bh(&linkfail_lock);
198}
199
200int ax25_protocol_is_registered(unsigned int pid)
201{
202 struct ax25_protocol *protocol;
203 int res = 0;
204
205 read_lock_bh(&protocol_list_lock);
206 for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
207 if (protocol->pid == pid) {
208 res = 1;
209 break;
210 }
211 read_unlock_bh(&protocol_list_lock);
212
213 return res;
214}