x25_timer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * X.25 Packet Layer release 002
  3. *
  4. * This is ALPHA test software. This code may break your machine,
  5. * randomly fail to work with new releases, misbehave and/or generally
  6. * screw up. It might even work.
  7. *
  8. * This code REQUIRES 2.1.15 or higher
  9. *
  10. * This module:
  11. * This module is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. * History
  17. * X.25 001 Jonathan Naylor Started coding.
  18. * X.25 002 Jonathan Naylor New timer architecture.
  19. * Centralised disconnection processing.
  20. */
  21. #include <linux/errno.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/timer.h>
  24. #include <net/sock.h>
  25. #include <net/tcp_states.h>
  26. #include <net/x25.h>
  27. static void x25_heartbeat_expiry(unsigned long);
  28. static void x25_timer_expiry(unsigned long);
  29. void x25_init_timers(struct sock *sk)
  30. {
  31. struct x25_sock *x25 = x25_sk(sk);
  32. init_timer(&x25->timer);
  33. x25->timer.data = (unsigned long)sk;
  34. x25->timer.function = &x25_timer_expiry;
  35. /* initialized by sock_init_data */
  36. sk->sk_timer.data = (unsigned long)sk;
  37. sk->sk_timer.function = &x25_heartbeat_expiry;
  38. }
  39. void x25_start_heartbeat(struct sock *sk)
  40. {
  41. mod_timer(&sk->sk_timer, jiffies + 5 * HZ);
  42. }
  43. void x25_stop_heartbeat(struct sock *sk)
  44. {
  45. del_timer(&sk->sk_timer);
  46. }
  47. void x25_start_t2timer(struct sock *sk)
  48. {
  49. struct x25_sock *x25 = x25_sk(sk);
  50. mod_timer(&x25->timer, jiffies + x25->t2);
  51. }
  52. void x25_start_t21timer(struct sock *sk)
  53. {
  54. struct x25_sock *x25 = x25_sk(sk);
  55. mod_timer(&x25->timer, jiffies + x25->t21);
  56. }
  57. void x25_start_t22timer(struct sock *sk)
  58. {
  59. struct x25_sock *x25 = x25_sk(sk);
  60. mod_timer(&x25->timer, jiffies + x25->t22);
  61. }
  62. void x25_start_t23timer(struct sock *sk)
  63. {
  64. struct x25_sock *x25 = x25_sk(sk);
  65. mod_timer(&x25->timer, jiffies + x25->t23);
  66. }
  67. void x25_stop_timer(struct sock *sk)
  68. {
  69. del_timer(&x25_sk(sk)->timer);
  70. }
  71. unsigned long x25_display_timer(struct sock *sk)
  72. {
  73. struct x25_sock *x25 = x25_sk(sk);
  74. if (!timer_pending(&x25->timer))
  75. return 0;
  76. return x25->timer.expires - jiffies;
  77. }
  78. static void x25_heartbeat_expiry(unsigned long param)
  79. {
  80. struct sock *sk = (struct sock *)param;
  81. bh_lock_sock(sk);
  82. if (sock_owned_by_user(sk)) /* can currently only occur in state 3 */
  83. goto restart_heartbeat;
  84. switch (x25_sk(sk)->state) {
  85. case X25_STATE_0:
  86. /*
  87. * Magic here: If we listen() and a new link dies
  88. * before it is accepted() it isn't 'dead' so doesn't
  89. * get removed.
  90. */
  91. if (sock_flag(sk, SOCK_DESTROY) ||
  92. (sk->sk_state == TCP_LISTEN &&
  93. sock_flag(sk, SOCK_DEAD))) {
  94. bh_unlock_sock(sk);
  95. x25_destroy_socket(sk);
  96. return;
  97. }
  98. break;
  99. case X25_STATE_3:
  100. /*
  101. * Check for the state of the receive buffer.
  102. */
  103. x25_check_rbuf(sk);
  104. break;
  105. }
  106. restart_heartbeat:
  107. x25_start_heartbeat(sk);
  108. bh_unlock_sock(sk);
  109. }
  110. /*
  111. * Timer has expired, it may have been T2, T21, T22, or T23. We can tell
  112. * by the state machine state.
  113. */
  114. static inline void x25_do_timer_expiry(struct sock * sk)
  115. {
  116. struct x25_sock *x25 = x25_sk(sk);
  117. switch (x25->state) {
  118. case X25_STATE_3: /* T2 */
  119. if (x25->condition & X25_COND_ACK_PENDING) {
  120. x25->condition &= ~X25_COND_ACK_PENDING;
  121. x25_enquiry_response(sk);
  122. }
  123. break;
  124. case X25_STATE_1: /* T21 */
  125. case X25_STATE_4: /* T22 */
  126. x25_write_internal(sk, X25_CLEAR_REQUEST);
  127. x25->state = X25_STATE_2;
  128. x25_start_t23timer(sk);
  129. break;
  130. case X25_STATE_2: /* T23 */
  131. x25_disconnect(sk, ETIMEDOUT, 0, 0);
  132. break;
  133. }
  134. }
  135. static void x25_timer_expiry(unsigned long param)
  136. {
  137. struct sock *sk = (struct sock *)param;
  138. bh_lock_sock(sk);
  139. if (sock_owned_by_user(sk)) { /* can currently only occur in state 3 */
  140. if (x25_sk(sk)->state == X25_STATE_3)
  141. x25_start_t2timer(sk);
  142. } else
  143. x25_do_timer_expiry(sk);
  144. bh_unlock_sock(sk);
  145. }