ax25_iface.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/spinlock.h>
  16. #include <linux/timer.h>
  17. #include <linux/string.h>
  18. #include <linux/sockios.h>
  19. #include <linux/net.h>
  20. #include <net/ax25.h>
  21. #include <linux/inet.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/skbuff.h>
  24. #include <net/sock.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/system.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/mm.h>
  29. #include <linux/interrupt.h>
  30. static struct ax25_protocol *protocol_list;
  31. static DEFINE_RWLOCK(protocol_list_lock);
  32. static HLIST_HEAD(ax25_linkfail_list);
  33. static DEFINE_SPINLOCK(linkfail_lock);
  34. static struct listen_struct {
  35. struct listen_struct *next;
  36. ax25_address callsign;
  37. struct net_device *dev;
  38. } *listen_list = NULL;
  39. static DEFINE_SPINLOCK(listen_lock);
  40. /*
  41. * Do not register the internal protocols AX25_P_TEXT, AX25_P_SEGMENT,
  42. * AX25_P_IP or AX25_P_ARP ...
  43. */
  44. void ax25_register_pid(struct ax25_protocol *ap)
  45. {
  46. write_lock_bh(&protocol_list_lock);
  47. ap->next = protocol_list;
  48. protocol_list = ap;
  49. write_unlock_bh(&protocol_list_lock);
  50. }
  51. EXPORT_SYMBOL_GPL(ax25_register_pid);
  52. void ax25_protocol_release(unsigned int pid)
  53. {
  54. struct ax25_protocol *s, *protocol;
  55. write_lock_bh(&protocol_list_lock);
  56. protocol = protocol_list;
  57. if (protocol == NULL) {
  58. write_unlock_bh(&protocol_list_lock);
  59. return;
  60. }
  61. if (protocol->pid == pid) {
  62. protocol_list = protocol->next;
  63. write_unlock_bh(&protocol_list_lock);
  64. return;
  65. }
  66. while (protocol != NULL && protocol->next != NULL) {
  67. if (protocol->next->pid == pid) {
  68. s = protocol->next;
  69. protocol->next = protocol->next->next;
  70. write_unlock_bh(&protocol_list_lock);
  71. return;
  72. }
  73. protocol = protocol->next;
  74. }
  75. write_unlock_bh(&protocol_list_lock);
  76. }
  77. EXPORT_SYMBOL(ax25_protocol_release);
  78. void ax25_linkfail_register(struct ax25_linkfail *lf)
  79. {
  80. spin_lock_bh(&linkfail_lock);
  81. hlist_add_head(&lf->lf_node, &ax25_linkfail_list);
  82. spin_unlock_bh(&linkfail_lock);
  83. }
  84. EXPORT_SYMBOL(ax25_linkfail_register);
  85. void ax25_linkfail_release(struct ax25_linkfail *lf)
  86. {
  87. spin_lock_bh(&linkfail_lock);
  88. hlist_del_init(&lf->lf_node);
  89. spin_unlock_bh(&linkfail_lock);
  90. }
  91. EXPORT_SYMBOL(ax25_linkfail_release);
  92. int ax25_listen_register(ax25_address *callsign, struct net_device *dev)
  93. {
  94. struct listen_struct *listen;
  95. if (ax25_listen_mine(callsign, dev))
  96. return 0;
  97. if ((listen = kmalloc(sizeof(*listen), GFP_ATOMIC)) == NULL)
  98. return -ENOMEM;
  99. listen->callsign = *callsign;
  100. listen->dev = dev;
  101. spin_lock_bh(&listen_lock);
  102. listen->next = listen_list;
  103. listen_list = listen;
  104. spin_unlock_bh(&listen_lock);
  105. return 0;
  106. }
  107. EXPORT_SYMBOL(ax25_listen_register);
  108. void ax25_listen_release(ax25_address *callsign, struct net_device *dev)
  109. {
  110. struct listen_struct *s, *listen;
  111. spin_lock_bh(&listen_lock);
  112. listen = listen_list;
  113. if (listen == NULL) {
  114. spin_unlock_bh(&listen_lock);
  115. return;
  116. }
  117. if (ax25cmp(&listen->callsign, callsign) == 0 && listen->dev == dev) {
  118. listen_list = listen->next;
  119. spin_unlock_bh(&listen_lock);
  120. kfree(listen);
  121. return;
  122. }
  123. while (listen != NULL && listen->next != NULL) {
  124. if (ax25cmp(&listen->next->callsign, callsign) == 0 && listen->next->dev == dev) {
  125. s = listen->next;
  126. listen->next = listen->next->next;
  127. spin_unlock_bh(&listen_lock);
  128. kfree(s);
  129. return;
  130. }
  131. listen = listen->next;
  132. }
  133. spin_unlock_bh(&listen_lock);
  134. }
  135. EXPORT_SYMBOL(ax25_listen_release);
  136. int (*ax25_protocol_function(unsigned int pid))(struct sk_buff *, ax25_cb *)
  137. {
  138. int (*res)(struct sk_buff *, ax25_cb *) = NULL;
  139. struct ax25_protocol *protocol;
  140. read_lock(&protocol_list_lock);
  141. for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
  142. if (protocol->pid == pid) {
  143. res = protocol->func;
  144. break;
  145. }
  146. read_unlock(&protocol_list_lock);
  147. return res;
  148. }
  149. int ax25_listen_mine(ax25_address *callsign, struct net_device *dev)
  150. {
  151. struct listen_struct *listen;
  152. spin_lock_bh(&listen_lock);
  153. for (listen = listen_list; listen != NULL; listen = listen->next)
  154. if (ax25cmp(&listen->callsign, callsign) == 0 &&
  155. (listen->dev == dev || listen->dev == NULL)) {
  156. spin_unlock_bh(&listen_lock);
  157. return 1;
  158. }
  159. spin_unlock_bh(&listen_lock);
  160. return 0;
  161. }
  162. void ax25_link_failed(ax25_cb *ax25, int reason)
  163. {
  164. struct ax25_linkfail *lf;
  165. struct hlist_node *node;
  166. spin_lock_bh(&linkfail_lock);
  167. hlist_for_each_entry(lf, node, &ax25_linkfail_list, lf_node)
  168. lf->func(ax25, reason);
  169. spin_unlock_bh(&linkfail_lock);
  170. }
  171. int ax25_protocol_is_registered(unsigned int pid)
  172. {
  173. struct ax25_protocol *protocol;
  174. int res = 0;
  175. read_lock_bh(&protocol_list_lock);
  176. for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
  177. if (protocol->pid == pid) {
  178. res = 1;
  179. break;
  180. }
  181. read_unlock_bh(&protocol_list_lock);
  182. return res;
  183. }