ax25_iface.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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/sched.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/timer.h>
  18. #include <linux/string.h>
  19. #include <linux/sockios.h>
  20. #include <linux/net.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. static struct protocol_struct {
  32. struct protocol_struct *next;
  33. unsigned int pid;
  34. int (*func)(struct sk_buff *, ax25_cb *);
  35. } *protocol_list = NULL;
  36. static DEFINE_RWLOCK(protocol_list_lock);
  37. static struct linkfail_struct {
  38. struct linkfail_struct *next;
  39. void (*func)(ax25_cb *, int);
  40. } *linkfail_list = NULL;
  41. static DEFINE_SPINLOCK(linkfail_lock);
  42. static struct listen_struct {
  43. struct listen_struct *next;
  44. ax25_address callsign;
  45. struct net_device *dev;
  46. } *listen_list = NULL;
  47. static DEFINE_SPINLOCK(listen_lock);
  48. int ax25_protocol_register(unsigned int pid,
  49. int (*func)(struct sk_buff *, ax25_cb *))
  50. {
  51. struct protocol_struct *protocol;
  52. if (pid == AX25_P_TEXT || pid == AX25_P_SEGMENT)
  53. return 0;
  54. #ifdef CONFIG_INET
  55. if (pid == AX25_P_IP || pid == AX25_P_ARP)
  56. return 0;
  57. #endif
  58. if ((protocol = kmalloc(sizeof(*protocol), GFP_ATOMIC)) == NULL)
  59. return 0;
  60. protocol->pid = pid;
  61. protocol->func = func;
  62. write_lock_bh(&protocol_list_lock);
  63. protocol->next = protocol_list;
  64. protocol_list = protocol;
  65. write_unlock_bh(&protocol_list_lock);
  66. return 1;
  67. }
  68. EXPORT_SYMBOL(ax25_protocol_register);
  69. void ax25_protocol_release(unsigned int pid)
  70. {
  71. struct protocol_struct *s, *protocol;
  72. write_lock_bh(&protocol_list_lock);
  73. protocol = protocol_list;
  74. if (protocol == NULL) {
  75. write_unlock_bh(&protocol_list_lock);
  76. return;
  77. }
  78. if (protocol->pid == pid) {
  79. protocol_list = protocol->next;
  80. write_unlock_bh(&protocol_list_lock);
  81. kfree(protocol);
  82. return;
  83. }
  84. while (protocol != NULL && protocol->next != NULL) {
  85. if (protocol->next->pid == pid) {
  86. s = protocol->next;
  87. protocol->next = protocol->next->next;
  88. write_unlock_bh(&protocol_list_lock);
  89. kfree(s);
  90. return;
  91. }
  92. protocol = protocol->next;
  93. }
  94. write_unlock_bh(&protocol_list_lock);
  95. }
  96. EXPORT_SYMBOL(ax25_protocol_release);
  97. int ax25_linkfail_register(void (*func)(ax25_cb *, int))
  98. {
  99. struct linkfail_struct *linkfail;
  100. if ((linkfail = kmalloc(sizeof(*linkfail), GFP_ATOMIC)) == NULL)
  101. return 0;
  102. linkfail->func = func;
  103. spin_lock_bh(&linkfail_lock);
  104. linkfail->next = linkfail_list;
  105. linkfail_list = linkfail;
  106. spin_unlock_bh(&linkfail_lock);
  107. return 1;
  108. }
  109. EXPORT_SYMBOL(ax25_linkfail_register);
  110. void ax25_linkfail_release(void (*func)(ax25_cb *, int))
  111. {
  112. struct linkfail_struct *s, *linkfail;
  113. spin_lock_bh(&linkfail_lock);
  114. linkfail = linkfail_list;
  115. if (linkfail == NULL) {
  116. spin_unlock_bh(&linkfail_lock);
  117. return;
  118. }
  119. if (linkfail->func == func) {
  120. linkfail_list = linkfail->next;
  121. spin_unlock_bh(&linkfail_lock);
  122. kfree(linkfail);
  123. return;
  124. }
  125. while (linkfail != NULL && linkfail->next != NULL) {
  126. if (linkfail->next->func == func) {
  127. s = linkfail->next;
  128. linkfail->next = linkfail->next->next;
  129. spin_unlock_bh(&linkfail_lock);
  130. kfree(s);
  131. return;
  132. }
  133. linkfail = linkfail->next;
  134. }
  135. spin_unlock_bh(&linkfail_lock);
  136. }
  137. EXPORT_SYMBOL(ax25_linkfail_release);
  138. int ax25_listen_register(ax25_address *callsign, struct net_device *dev)
  139. {
  140. struct listen_struct *listen;
  141. if (ax25_listen_mine(callsign, dev))
  142. return 0;
  143. if ((listen = kmalloc(sizeof(*listen), GFP_ATOMIC)) == NULL)
  144. return 0;
  145. listen->callsign = *callsign;
  146. listen->dev = dev;
  147. spin_lock_bh(&listen_lock);
  148. listen->next = listen_list;
  149. listen_list = listen;
  150. spin_unlock_bh(&listen_lock);
  151. return 1;
  152. }
  153. EXPORT_SYMBOL(ax25_listen_register);
  154. void ax25_listen_release(ax25_address *callsign, struct net_device *dev)
  155. {
  156. struct listen_struct *s, *listen;
  157. spin_lock_bh(&listen_lock);
  158. listen = listen_list;
  159. if (listen == NULL) {
  160. spin_unlock_bh(&listen_lock);
  161. return;
  162. }
  163. if (ax25cmp(&listen->callsign, callsign) == 0 && listen->dev == dev) {
  164. listen_list = listen->next;
  165. spin_unlock_bh(&listen_lock);
  166. kfree(listen);
  167. return;
  168. }
  169. while (listen != NULL && listen->next != NULL) {
  170. if (ax25cmp(&listen->next->callsign, callsign) == 0 && listen->next->dev == dev) {
  171. s = listen->next;
  172. listen->next = listen->next->next;
  173. spin_unlock_bh(&listen_lock);
  174. kfree(s);
  175. return;
  176. }
  177. listen = listen->next;
  178. }
  179. spin_unlock_bh(&listen_lock);
  180. }
  181. EXPORT_SYMBOL(ax25_listen_release);
  182. int (*ax25_protocol_function(unsigned int pid))(struct sk_buff *, ax25_cb *)
  183. {
  184. int (*res)(struct sk_buff *, ax25_cb *) = NULL;
  185. struct protocol_struct *protocol;
  186. read_lock(&protocol_list_lock);
  187. for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
  188. if (protocol->pid == pid) {
  189. res = protocol->func;
  190. break;
  191. }
  192. read_unlock(&protocol_list_lock);
  193. return res;
  194. }
  195. int ax25_listen_mine(ax25_address *callsign, struct net_device *dev)
  196. {
  197. struct listen_struct *listen;
  198. spin_lock_bh(&listen_lock);
  199. for (listen = listen_list; listen != NULL; listen = listen->next)
  200. if (ax25cmp(&listen->callsign, callsign) == 0 && (listen->dev == dev || listen->dev == NULL)) {
  201. spin_unlock_bh(&listen_lock);
  202. return 1;
  203. }
  204. spin_unlock_bh(&listen_lock);
  205. return 0;
  206. }
  207. void ax25_link_failed(ax25_cb *ax25, int reason)
  208. {
  209. struct linkfail_struct *linkfail;
  210. spin_lock_bh(&linkfail_lock);
  211. for (linkfail = linkfail_list; linkfail != NULL; linkfail = linkfail->next)
  212. (linkfail->func)(ax25, reason);
  213. spin_unlock_bh(&linkfail_lock);
  214. }
  215. int ax25_protocol_is_registered(unsigned int pid)
  216. {
  217. struct protocol_struct *protocol;
  218. int res = 0;
  219. read_lock_bh(&protocol_list_lock);
  220. for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
  221. if (protocol->pid == pid) {
  222. res = 1;
  223. break;
  224. }
  225. read_unlock_bh(&protocol_list_lock);
  226. return res;
  227. }