ax25_iface.c 5.8 KB

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