ax25_iface.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/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(&protocol_list_lock);
  63. protocol->next = protocol_list;
  64. protocol_list = protocol;
  65. write_unlock(&protocol_list_lock);
  66. return 1;
  67. }
  68. void ax25_protocol_release(unsigned int pid)
  69. {
  70. struct protocol_struct *s, *protocol;
  71. write_lock(&protocol_list_lock);
  72. protocol = protocol_list;
  73. if (protocol == NULL) {
  74. write_unlock(&protocol_list_lock);
  75. return;
  76. }
  77. if (protocol->pid == pid) {
  78. protocol_list = protocol->next;
  79. write_unlock(&protocol_list_lock);
  80. kfree(protocol);
  81. return;
  82. }
  83. while (protocol != NULL && protocol->next != NULL) {
  84. if (protocol->next->pid == pid) {
  85. s = protocol->next;
  86. protocol->next = protocol->next->next;
  87. write_unlock(&protocol_list_lock);
  88. kfree(s);
  89. return;
  90. }
  91. protocol = protocol->next;
  92. }
  93. write_unlock(&protocol_list_lock);
  94. }
  95. int ax25_linkfail_register(void (*func)(ax25_cb *, int))
  96. {
  97. struct linkfail_struct *linkfail;
  98. if ((linkfail = kmalloc(sizeof(*linkfail), GFP_ATOMIC)) == NULL)
  99. return 0;
  100. linkfail->func = func;
  101. spin_lock_bh(&linkfail_lock);
  102. linkfail->next = linkfail_list;
  103. linkfail_list = linkfail;
  104. spin_unlock_bh(&linkfail_lock);
  105. return 1;
  106. }
  107. void ax25_linkfail_release(void (*func)(ax25_cb *, int))
  108. {
  109. struct linkfail_struct *s, *linkfail;
  110. spin_lock_bh(&linkfail_lock);
  111. linkfail = linkfail_list;
  112. if (linkfail == NULL) {
  113. spin_unlock_bh(&linkfail_lock);
  114. return;
  115. }
  116. if (linkfail->func == func) {
  117. linkfail_list = linkfail->next;
  118. spin_unlock_bh(&linkfail_lock);
  119. kfree(linkfail);
  120. return;
  121. }
  122. while (linkfail != NULL && linkfail->next != NULL) {
  123. if (linkfail->next->func == func) {
  124. s = linkfail->next;
  125. linkfail->next = linkfail->next->next;
  126. spin_unlock_bh(&linkfail_lock);
  127. kfree(s);
  128. return;
  129. }
  130. linkfail = linkfail->next;
  131. }
  132. spin_unlock_bh(&linkfail_lock);
  133. }
  134. int ax25_listen_register(ax25_address *callsign, struct net_device *dev)
  135. {
  136. struct listen_struct *listen;
  137. if (ax25_listen_mine(callsign, dev))
  138. return 0;
  139. if ((listen = kmalloc(sizeof(*listen), GFP_ATOMIC)) == NULL)
  140. return 0;
  141. listen->callsign = *callsign;
  142. listen->dev = dev;
  143. spin_lock_bh(&listen_lock);
  144. listen->next = listen_list;
  145. listen_list = listen;
  146. spin_unlock_bh(&listen_lock);
  147. return 1;
  148. }
  149. void ax25_listen_release(ax25_address *callsign, struct net_device *dev)
  150. {
  151. struct listen_struct *s, *listen;
  152. spin_lock_bh(&listen_lock);
  153. listen = listen_list;
  154. if (listen == NULL) {
  155. spin_unlock_bh(&listen_lock);
  156. return;
  157. }
  158. if (ax25cmp(&listen->callsign, callsign) == 0 && listen->dev == dev) {
  159. listen_list = listen->next;
  160. spin_unlock_bh(&listen_lock);
  161. kfree(listen);
  162. return;
  163. }
  164. while (listen != NULL && listen->next != NULL) {
  165. if (ax25cmp(&listen->next->callsign, callsign) == 0 && listen->next->dev == dev) {
  166. s = listen->next;
  167. listen->next = listen->next->next;
  168. spin_unlock_bh(&listen_lock);
  169. kfree(s);
  170. return;
  171. }
  172. listen = listen->next;
  173. }
  174. spin_unlock_bh(&listen_lock);
  175. }
  176. int (*ax25_protocol_function(unsigned int pid))(struct sk_buff *, ax25_cb *)
  177. {
  178. int (*res)(struct sk_buff *, ax25_cb *) = NULL;
  179. struct protocol_struct *protocol;
  180. read_lock(&protocol_list_lock);
  181. for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
  182. if (protocol->pid == pid) {
  183. res = protocol->func;
  184. break;
  185. }
  186. read_unlock(&protocol_list_lock);
  187. return res;
  188. }
  189. int ax25_listen_mine(ax25_address *callsign, struct net_device *dev)
  190. {
  191. struct listen_struct *listen;
  192. spin_lock_bh(&listen_lock);
  193. for (listen = listen_list; listen != NULL; listen = listen->next)
  194. if (ax25cmp(&listen->callsign, callsign) == 0 && (listen->dev == dev || listen->dev == NULL)) {
  195. spin_unlock_bh(&listen_lock);
  196. return 1;
  197. }
  198. spin_unlock_bh(&listen_lock);
  199. return 0;
  200. }
  201. void ax25_link_failed(ax25_cb *ax25, int reason)
  202. {
  203. struct linkfail_struct *linkfail;
  204. spin_lock_bh(&linkfail_lock);
  205. for (linkfail = linkfail_list; linkfail != NULL; linkfail = linkfail->next)
  206. (linkfail->func)(ax25, reason);
  207. spin_unlock_bh(&linkfail_lock);
  208. }
  209. int ax25_protocol_is_registered(unsigned int pid)
  210. {
  211. struct protocol_struct *protocol;
  212. int res = 0;
  213. read_lock(&protocol_list_lock);
  214. for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
  215. if (protocol->pid == pid) {
  216. res = 1;
  217. break;
  218. }
  219. read_unlock(&protocol_list_lock);
  220. return res;
  221. }