ax25_iface.c 4.9 KB

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