Linux 4.8.3
[linux/fpc-iii.git] / net / ax25 / ax25_iface.c
blob7f16e8a931b2fa149e45dbb2b07d7f47a988af53
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/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 <linux/fcntl.h>
28 #include <linux/mm.h>
29 #include <linux/interrupt.h>
31 static struct ax25_protocol *protocol_list;
32 static DEFINE_RWLOCK(protocol_list_lock);
34 static HLIST_HEAD(ax25_linkfail_list);
35 static DEFINE_SPINLOCK(linkfail_lock);
37 static struct listen_struct {
38 struct listen_struct *next;
39 ax25_address callsign;
40 struct net_device *dev;
41 } *listen_list = NULL;
42 static DEFINE_SPINLOCK(listen_lock);
45 * Do not register the internal protocols AX25_P_TEXT, AX25_P_SEGMENT,
46 * AX25_P_IP or AX25_P_ARP ...
48 void ax25_register_pid(struct ax25_protocol *ap)
50 write_lock_bh(&protocol_list_lock);
51 ap->next = protocol_list;
52 protocol_list = ap;
53 write_unlock_bh(&protocol_list_lock);
56 EXPORT_SYMBOL_GPL(ax25_register_pid);
58 void ax25_protocol_release(unsigned int pid)
60 struct ax25_protocol *protocol;
62 write_lock_bh(&protocol_list_lock);
63 protocol = protocol_list;
64 if (protocol == NULL)
65 goto out;
67 if (protocol->pid == pid) {
68 protocol_list = protocol->next;
69 goto out;
72 while (protocol != NULL && protocol->next != NULL) {
73 if (protocol->next->pid == pid) {
74 protocol->next = protocol->next->next;
75 goto out;
78 protocol = protocol->next;
80 out:
81 write_unlock_bh(&protocol_list_lock);
84 EXPORT_SYMBOL(ax25_protocol_release);
86 void ax25_linkfail_register(struct ax25_linkfail *lf)
88 spin_lock_bh(&linkfail_lock);
89 hlist_add_head(&lf->lf_node, &ax25_linkfail_list);
90 spin_unlock_bh(&linkfail_lock);
93 EXPORT_SYMBOL(ax25_linkfail_register);
95 void ax25_linkfail_release(struct ax25_linkfail *lf)
97 spin_lock_bh(&linkfail_lock);
98 hlist_del_init(&lf->lf_node);
99 spin_unlock_bh(&linkfail_lock);
102 EXPORT_SYMBOL(ax25_linkfail_release);
104 int ax25_listen_register(ax25_address *callsign, struct net_device *dev)
106 struct listen_struct *listen;
108 if (ax25_listen_mine(callsign, dev))
109 return 0;
111 if ((listen = kmalloc(sizeof(*listen), GFP_ATOMIC)) == NULL)
112 return -ENOMEM;
114 listen->callsign = *callsign;
115 listen->dev = dev;
117 spin_lock_bh(&listen_lock);
118 listen->next = listen_list;
119 listen_list = listen;
120 spin_unlock_bh(&listen_lock);
122 return 0;
125 EXPORT_SYMBOL(ax25_listen_register);
127 void ax25_listen_release(ax25_address *callsign, struct net_device *dev)
129 struct listen_struct *s, *listen;
131 spin_lock_bh(&listen_lock);
132 listen = listen_list;
133 if (listen == NULL) {
134 spin_unlock_bh(&listen_lock);
135 return;
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;
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;
154 listen = listen->next;
156 spin_unlock_bh(&listen_lock);
159 EXPORT_SYMBOL(ax25_listen_release);
161 int (*ax25_protocol_function(unsigned int pid))(struct sk_buff *, ax25_cb *)
163 int (*res)(struct sk_buff *, ax25_cb *) = NULL;
164 struct ax25_protocol *protocol;
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;
172 read_unlock(&protocol_list_lock);
174 return res;
177 int ax25_listen_mine(ax25_address *callsign, struct net_device *dev)
179 struct listen_struct *listen;
181 spin_lock_bh(&listen_lock);
182 for (listen = listen_list; listen != NULL; listen = listen->next)
183 if (ax25cmp(&listen->callsign, callsign) == 0 &&
184 (listen->dev == dev || listen->dev == NULL)) {
185 spin_unlock_bh(&listen_lock);
186 return 1;
188 spin_unlock_bh(&listen_lock);
190 return 0;
193 void ax25_link_failed(ax25_cb *ax25, int reason)
195 struct ax25_linkfail *lf;
197 spin_lock_bh(&linkfail_lock);
198 hlist_for_each_entry(lf, &ax25_linkfail_list, lf_node)
199 lf->func(ax25, reason);
200 spin_unlock_bh(&linkfail_lock);
203 int ax25_protocol_is_registered(unsigned int pid)
205 struct ax25_protocol *protocol;
206 int res = 0;
208 read_lock_bh(&protocol_list_lock);
209 for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
210 if (protocol->pid == pid) {
211 res = 1;
212 break;
214 read_unlock_bh(&protocol_list_lock);
216 return res;