ax25_iface.c 4.9 KB

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