ax25_iface.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. kfree(protocol);
  65. return;
  66. }
  67. while (protocol != NULL && protocol->next != NULL) {
  68. if (protocol->next->pid == pid) {
  69. s = protocol->next;
  70. protocol->next = protocol->next->next;
  71. write_unlock_bh(&protocol_list_lock);
  72. kfree(s);
  73. return;
  74. }
  75. protocol = protocol->next;
  76. }
  77. write_unlock_bh(&protocol_list_lock);
  78. }
  79. EXPORT_SYMBOL(ax25_protocol_release);
  80. void ax25_linkfail_register(struct ax25_linkfail *lf)
  81. {
  82. spin_lock_bh(&linkfail_lock);
  83. hlist_add_head(&lf->lf_node, &ax25_linkfail_list);
  84. spin_unlock_bh(&linkfail_lock);
  85. }
  86. EXPORT_SYMBOL(ax25_linkfail_register);
  87. void ax25_linkfail_release(struct ax25_linkfail *lf)
  88. {
  89. spin_lock_bh(&linkfail_lock);
  90. hlist_del_init(&lf->lf_node);
  91. spin_unlock_bh(&linkfail_lock);
  92. }
  93. EXPORT_SYMBOL(ax25_linkfail_release);
  94. int ax25_listen_register(ax25_address *callsign, struct net_device *dev)
  95. {
  96. struct listen_struct *listen;
  97. if (ax25_listen_mine(callsign, dev))
  98. return 0;
  99. if ((listen = kmalloc(sizeof(*listen), GFP_ATOMIC)) == NULL)
  100. return -ENOMEM;
  101. listen->callsign = *callsign;
  102. listen->dev = dev;
  103. spin_lock_bh(&listen_lock);
  104. listen->next = listen_list;
  105. listen_list = listen;
  106. spin_unlock_bh(&listen_lock);
  107. return 0;
  108. }
  109. EXPORT_SYMBOL(ax25_listen_register);
  110. void ax25_listen_release(ax25_address *callsign, struct net_device *dev)
  111. {
  112. struct listen_struct *s, *listen;
  113. spin_lock_bh(&listen_lock);
  114. listen = listen_list;
  115. if (listen == NULL) {
  116. spin_unlock_bh(&listen_lock);
  117. return;
  118. }
  119. if (ax25cmp(&listen->callsign, callsign) == 0 && listen->dev == dev) {
  120. listen_list = listen->next;
  121. spin_unlock_bh(&listen_lock);
  122. kfree(listen);
  123. return;
  124. }
  125. while (listen != NULL && listen->next != NULL) {
  126. if (ax25cmp(&listen->next->callsign, callsign) == 0 && listen->next->dev == dev) {
  127. s = listen->next;
  128. listen->next = listen->next->next;
  129. spin_unlock_bh(&listen_lock);
  130. kfree(s);
  131. return;
  132. }
  133. listen = listen->next;
  134. }
  135. spin_unlock_bh(&listen_lock);
  136. }
  137. EXPORT_SYMBOL(ax25_listen_release);
  138. int (*ax25_protocol_function(unsigned int pid))(struct sk_buff *, ax25_cb *)
  139. {
  140. int (*res)(struct sk_buff *, ax25_cb *) = NULL;
  141. struct ax25_protocol *protocol;
  142. read_lock(&protocol_list_lock);
  143. for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
  144. if (protocol->pid == pid) {
  145. res = protocol->func;
  146. break;
  147. }
  148. read_unlock(&protocol_list_lock);
  149. return res;
  150. }
  151. int ax25_listen_mine(ax25_address *callsign, struct net_device *dev)
  152. {
  153. struct listen_struct *listen;
  154. spin_lock_bh(&listen_lock);
  155. for (listen = listen_list; listen != NULL; listen = listen->next)
  156. if (ax25cmp(&listen->callsign, callsign) == 0 &&
  157. (listen->dev == dev || listen->dev == NULL)) {
  158. spin_unlock_bh(&listen_lock);
  159. return 1;
  160. }
  161. spin_unlock_bh(&listen_lock);
  162. return 0;
  163. }
  164. void ax25_link_failed(ax25_cb *ax25, int reason)
  165. {
  166. struct ax25_linkfail *lf;
  167. struct hlist_node *node;
  168. spin_lock_bh(&linkfail_lock);
  169. hlist_for_each_entry(lf, node, &ax25_linkfail_list, lf_node)
  170. lf->func(ax25, reason);
  171. spin_unlock_bh(&linkfail_lock);
  172. }
  173. int ax25_protocol_is_registered(unsigned int pid)
  174. {
  175. struct ax25_protocol *protocol;
  176. int res = 0;
  177. read_lock_bh(&protocol_list_lock);
  178. for (protocol = protocol_list; protocol != NULL; protocol = protocol->next)
  179. if (protocol->pid == pid) {
  180. res = 1;
  181. break;
  182. }
  183. read_unlock_bh(&protocol_list_lock);
  184. return res;
  185. }