ax25_iface.c 6.2 KB

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