ax25_dev.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/slab.h>
  13. #include <linux/in.h>
  14. #include <linux/kernel.h>
  15. #include <linux/timer.h>
  16. #include <linux/string.h>
  17. #include <linux/sockios.h>
  18. #include <linux/net.h>
  19. #include <linux/spinlock.h>
  20. #include <net/ax25.h>
  21. #include <linux/inet.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/if_arp.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. #include <linux/init.h>
  31. ax25_dev *ax25_dev_list;
  32. DEFINE_SPINLOCK(ax25_dev_lock);
  33. ax25_dev *ax25_addr_ax25dev(ax25_address *addr)
  34. {
  35. ax25_dev *ax25_dev, *res = NULL;
  36. spin_lock_bh(&ax25_dev_lock);
  37. for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next)
  38. if (ax25cmp(addr, (ax25_address *)ax25_dev->dev->dev_addr) == 0) {
  39. res = ax25_dev;
  40. }
  41. spin_unlock_bh(&ax25_dev_lock);
  42. return res;
  43. }
  44. /*
  45. * This is called when an interface is brought up. These are
  46. * reasonable defaults.
  47. */
  48. void ax25_dev_device_up(struct net_device *dev)
  49. {
  50. ax25_dev *ax25_dev;
  51. if ((ax25_dev = kzalloc(sizeof(*ax25_dev), GFP_ATOMIC)) == NULL) {
  52. printk(KERN_ERR "AX.25: ax25_dev_device_up - out of memory\n");
  53. return;
  54. }
  55. dev->ax25_ptr = ax25_dev;
  56. ax25_dev->dev = dev;
  57. dev_hold(dev);
  58. ax25_dev->forward = NULL;
  59. ax25_dev->values[AX25_VALUES_IPDEFMODE] = AX25_DEF_IPDEFMODE;
  60. ax25_dev->values[AX25_VALUES_AXDEFMODE] = AX25_DEF_AXDEFMODE;
  61. ax25_dev->values[AX25_VALUES_BACKOFF] = AX25_DEF_BACKOFF;
  62. ax25_dev->values[AX25_VALUES_CONMODE] = AX25_DEF_CONMODE;
  63. ax25_dev->values[AX25_VALUES_WINDOW] = AX25_DEF_WINDOW;
  64. ax25_dev->values[AX25_VALUES_EWINDOW] = AX25_DEF_EWINDOW;
  65. ax25_dev->values[AX25_VALUES_T1] = AX25_DEF_T1;
  66. ax25_dev->values[AX25_VALUES_T2] = AX25_DEF_T2;
  67. ax25_dev->values[AX25_VALUES_T3] = AX25_DEF_T3;
  68. ax25_dev->values[AX25_VALUES_IDLE] = AX25_DEF_IDLE;
  69. ax25_dev->values[AX25_VALUES_N2] = AX25_DEF_N2;
  70. ax25_dev->values[AX25_VALUES_PACLEN] = AX25_DEF_PACLEN;
  71. ax25_dev->values[AX25_VALUES_PROTOCOL] = AX25_DEF_PROTOCOL;
  72. ax25_dev->values[AX25_VALUES_DS_TIMEOUT]= AX25_DEF_DS_TIMEOUT;
  73. #if defined(CONFIG_AX25_DAMA_SLAVE) || defined(CONFIG_AX25_DAMA_MASTER)
  74. ax25_ds_setup_timer(ax25_dev);
  75. #endif
  76. spin_lock_bh(&ax25_dev_lock);
  77. ax25_dev->next = ax25_dev_list;
  78. ax25_dev_list = ax25_dev;
  79. spin_unlock_bh(&ax25_dev_lock);
  80. ax25_register_dev_sysctl(ax25_dev);
  81. }
  82. void ax25_dev_device_down(struct net_device *dev)
  83. {
  84. ax25_dev *s, *ax25_dev;
  85. if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
  86. return;
  87. ax25_unregister_dev_sysctl(ax25_dev);
  88. spin_lock_bh(&ax25_dev_lock);
  89. #ifdef CONFIG_AX25_DAMA_SLAVE
  90. ax25_ds_del_timer(ax25_dev);
  91. #endif
  92. /*
  93. * Remove any packet forwarding that points to this device.
  94. */
  95. for (s = ax25_dev_list; s != NULL; s = s->next)
  96. if (s->forward == dev)
  97. s->forward = NULL;
  98. if ((s = ax25_dev_list) == ax25_dev) {
  99. ax25_dev_list = s->next;
  100. spin_unlock_bh(&ax25_dev_lock);
  101. dev_put(dev);
  102. kfree(ax25_dev);
  103. return;
  104. }
  105. while (s != NULL && s->next != NULL) {
  106. if (s->next == ax25_dev) {
  107. s->next = ax25_dev->next;
  108. spin_unlock_bh(&ax25_dev_lock);
  109. dev_put(dev);
  110. kfree(ax25_dev);
  111. return;
  112. }
  113. s = s->next;
  114. }
  115. spin_unlock_bh(&ax25_dev_lock);
  116. dev->ax25_ptr = NULL;
  117. }
  118. int ax25_fwd_ioctl(unsigned int cmd, struct ax25_fwd_struct *fwd)
  119. {
  120. ax25_dev *ax25_dev, *fwd_dev;
  121. if ((ax25_dev = ax25_addr_ax25dev(&fwd->port_from)) == NULL)
  122. return -EINVAL;
  123. switch (cmd) {
  124. case SIOCAX25ADDFWD:
  125. if ((fwd_dev = ax25_addr_ax25dev(&fwd->port_to)) == NULL)
  126. return -EINVAL;
  127. if (ax25_dev->forward != NULL)
  128. return -EINVAL;
  129. ax25_dev->forward = fwd_dev->dev;
  130. break;
  131. case SIOCAX25DELFWD:
  132. if (ax25_dev->forward == NULL)
  133. return -EINVAL;
  134. ax25_dev->forward = NULL;
  135. break;
  136. default:
  137. return -EINVAL;
  138. }
  139. return 0;
  140. }
  141. struct net_device *ax25_fwd_dev(struct net_device *dev)
  142. {
  143. ax25_dev *ax25_dev;
  144. if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
  145. return dev;
  146. if (ax25_dev->forward == NULL)
  147. return dev;
  148. return ax25_dev->forward;
  149. }
  150. /*
  151. * Free all memory associated with device structures.
  152. */
  153. void __exit ax25_dev_free(void)
  154. {
  155. ax25_dev *s, *ax25_dev;
  156. spin_lock_bh(&ax25_dev_lock);
  157. ax25_dev = ax25_dev_list;
  158. while (ax25_dev != NULL) {
  159. s = ax25_dev;
  160. dev_put(ax25_dev->dev);
  161. ax25_dev = ax25_dev->next;
  162. kfree(s);
  163. }
  164. ax25_dev_list = NULL;
  165. spin_unlock_bh(&ax25_dev_lock);
  166. }