ax25_iface.c 5.1 KB

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