Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 | 2 | /* |
1da177e4 LT |
3 | * |
4 | * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk) | |
5 | */ | |
1da177e4 LT |
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> | |
70868eac | 11 | #include <linux/module.h> |
1da177e4 LT |
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> | |
5a0e3ad6 | 17 | #include <linux/slab.h> |
1da177e4 LT |
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> | |
7c0f6ba6 | 23 | #include <linux/uaccess.h> |
1da177e4 LT |
24 | #include <linux/fcntl.h> |
25 | #include <linux/mm.h> | |
26 | #include <linux/interrupt.h> | |
27 | ||
8d5cf596 | 28 | static struct ax25_protocol *protocol_list; |
1da177e4 LT |
29 | static DEFINE_RWLOCK(protocol_list_lock); |
30 | ||
a4282717 | 31 | static HLIST_HEAD(ax25_linkfail_list); |
1da177e4 LT |
32 | static DEFINE_SPINLOCK(linkfail_lock); |
33 | ||
34 | static struct listen_struct { | |
35 | struct listen_struct *next; | |
36 | ax25_address callsign; | |
37 | struct net_device *dev; | |
38 | } *listen_list = NULL; | |
39 | static DEFINE_SPINLOCK(listen_lock); | |
40 | ||
8d5cf596 RB |
41 | /* |
42 | * Do not register the internal protocols AX25_P_TEXT, AX25_P_SEGMENT, | |
43 | * AX25_P_IP or AX25_P_ARP ... | |
44 | */ | |
45 | void ax25_register_pid(struct ax25_protocol *ap) | |
1da177e4 | 46 | { |
95ff9f4d | 47 | write_lock_bh(&protocol_list_lock); |
8d5cf596 RB |
48 | ap->next = protocol_list; |
49 | protocol_list = ap; | |
95ff9f4d | 50 | write_unlock_bh(&protocol_list_lock); |
1da177e4 LT |
51 | } |
52 | ||
8d5cf596 | 53 | EXPORT_SYMBOL_GPL(ax25_register_pid); |
70868eac | 54 | |
1da177e4 LT |
55 | void ax25_protocol_release(unsigned int pid) |
56 | { | |
d8f969e6 | 57 | struct ax25_protocol *protocol; |
1da177e4 | 58 | |
95ff9f4d | 59 | write_lock_bh(&protocol_list_lock); |
1da177e4 | 60 | protocol = protocol_list; |
910d30b7 IJ |
61 | if (protocol == NULL) |
62 | goto out; | |
1da177e4 LT |
63 | |
64 | if (protocol->pid == pid) { | |
65 | protocol_list = protocol->next; | |
910d30b7 | 66 | goto out; |
1da177e4 LT |
67 | } |
68 | ||
69 | while (protocol != NULL && protocol->next != NULL) { | |
70 | if (protocol->next->pid == pid) { | |
1da177e4 | 71 | protocol->next = protocol->next->next; |
910d30b7 | 72 | goto out; |
1da177e4 LT |
73 | } |
74 | ||
75 | protocol = protocol->next; | |
76 | } | |
910d30b7 | 77 | out: |
95ff9f4d | 78 | write_unlock_bh(&protocol_list_lock); |
1da177e4 LT |
79 | } |
80 | ||
70868eac RB |
81 | EXPORT_SYMBOL(ax25_protocol_release); |
82 | ||
a4282717 | 83 | void ax25_linkfail_register(struct ax25_linkfail *lf) |
1da177e4 | 84 | { |
1da177e4 | 85 | spin_lock_bh(&linkfail_lock); |
a4282717 | 86 | hlist_add_head(&lf->lf_node, &ax25_linkfail_list); |
1da177e4 | 87 | spin_unlock_bh(&linkfail_lock); |
1da177e4 LT |
88 | } |
89 | ||
70868eac RB |
90 | EXPORT_SYMBOL(ax25_linkfail_register); |
91 | ||
a4282717 | 92 | void ax25_linkfail_release(struct ax25_linkfail *lf) |
1da177e4 | 93 | { |
1da177e4 | 94 | spin_lock_bh(&linkfail_lock); |
a4282717 | 95 | hlist_del_init(&lf->lf_node); |
1da177e4 LT |
96 | spin_unlock_bh(&linkfail_lock); |
97 | } | |
98 | ||
70868eac RB |
99 | EXPORT_SYMBOL(ax25_linkfail_release); |
100 | ||
c045ad2c | 101 | int ax25_listen_register(const ax25_address *callsign, struct net_device *dev) |
1da177e4 LT |
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) | |
81dcd169 | 109 | return -ENOMEM; |
1da177e4 LT |
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 | ||
81dcd169 | 119 | return 0; |
1da177e4 LT |
120 | } |
121 | ||
70868eac RB |
122 | EXPORT_SYMBOL(ax25_listen_register); |
123 | ||
c045ad2c | 124 | void ax25_listen_release(const ax25_address *callsign, struct net_device *dev) |
1da177e4 LT |
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 | ||
70868eac RB |
156 | EXPORT_SYMBOL(ax25_listen_release); |
157 | ||
1da177e4 LT |
158 | int (*ax25_protocol_function(unsigned int pid))(struct sk_buff *, ax25_cb *) |
159 | { | |
160 | int (*res)(struct sk_buff *, ax25_cb *) = NULL; | |
8d5cf596 | 161 | struct ax25_protocol *protocol; |
1da177e4 LT |
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 | ||
c045ad2c | 174 | int ax25_listen_mine(const ax25_address *callsign, struct net_device *dev) |
1da177e4 LT |
175 | { |
176 | struct listen_struct *listen; | |
177 | ||
178 | spin_lock_bh(&listen_lock); | |
179 | for (listen = listen_list; listen != NULL; listen = listen->next) | |
81dcd169 RB |
180 | if (ax25cmp(&listen->callsign, callsign) == 0 && |
181 | (listen->dev == dev || listen->dev == NULL)) { | |
1da177e4 LT |
182 | spin_unlock_bh(&listen_lock); |
183 | return 1; | |
184 | } | |
185 | spin_unlock_bh(&listen_lock); | |
186 | ||
187 | return 0; | |
188 | } | |
189 | ||
190 | void ax25_link_failed(ax25_cb *ax25, int reason) | |
191 | { | |
a4282717 | 192 | struct ax25_linkfail *lf; |
1da177e4 LT |
193 | |
194 | spin_lock_bh(&linkfail_lock); | |
b67bfe0d | 195 | hlist_for_each_entry(lf, &ax25_linkfail_list, lf_node) |
a4282717 | 196 | lf->func(ax25, reason); |
1da177e4 LT |
197 | spin_unlock_bh(&linkfail_lock); |
198 | } | |
199 | ||
200 | int ax25_protocol_is_registered(unsigned int pid) | |
201 | { | |
8d5cf596 | 202 | struct ax25_protocol *protocol; |
1da177e4 LT |
203 | int res = 0; |
204 | ||
95ff9f4d | 205 | read_lock_bh(&protocol_list_lock); |
1da177e4 LT |
206 | for (protocol = protocol_list; protocol != NULL; protocol = protocol->next) |
207 | if (protocol->pid == pid) { | |
208 | res = 1; | |
209 | break; | |
210 | } | |
95ff9f4d | 211 | read_unlock_bh(&protocol_list_lock); |
1da177e4 LT |
212 | |
213 | return res; | |
214 | } |