GNU Linux-libre 4.14.266-gnu1
[releases.git] / net / rose / rose_timer.c
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  * Copyright (C) 2002 Ralf Baechle DO1GRB (ralf@gnu.org)
9  */
10 #include <linux/errno.h>
11 #include <linux/types.h>
12 #include <linux/socket.h>
13 #include <linux/in.h>
14 #include <linux/kernel.h>
15 #include <linux/jiffies.h>
16 #include <linux/timer.h>
17 #include <linux/string.h>
18 #include <linux/sockios.h>
19 #include <linux/net.h>
20 #include <net/ax25.h>
21 #include <linux/inet.h>
22 #include <linux/netdevice.h>
23 #include <linux/skbuff.h>
24 #include <net/sock.h>
25 #include <net/tcp_states.h>
26 #include <linux/fcntl.h>
27 #include <linux/mm.h>
28 #include <linux/interrupt.h>
29 #include <net/rose.h>
30
31 static void rose_heartbeat_expiry(unsigned long);
32 static void rose_timer_expiry(struct timer_list *);
33 static void rose_idletimer_expiry(struct timer_list *);
34
35 void rose_start_heartbeat(struct sock *sk)
36 {
37         del_timer(&sk->sk_timer);
38
39         sk->sk_timer.data     = (unsigned long)sk;
40         sk->sk_timer.function = &rose_heartbeat_expiry;
41         sk->sk_timer.expires  = jiffies + 5 * HZ;
42
43         add_timer(&sk->sk_timer);
44 }
45
46 void rose_start_t1timer(struct sock *sk)
47 {
48         struct rose_sock *rose = rose_sk(sk);
49
50         del_timer(&rose->timer);
51
52         rose->timer.function = (TIMER_FUNC_TYPE)rose_timer_expiry;
53         rose->timer.expires  = jiffies + rose->t1;
54
55         add_timer(&rose->timer);
56 }
57
58 void rose_start_t2timer(struct sock *sk)
59 {
60         struct rose_sock *rose = rose_sk(sk);
61
62         del_timer(&rose->timer);
63
64         rose->timer.function = (TIMER_FUNC_TYPE)rose_timer_expiry;
65         rose->timer.expires  = jiffies + rose->t2;
66
67         add_timer(&rose->timer);
68 }
69
70 void rose_start_t3timer(struct sock *sk)
71 {
72         struct rose_sock *rose = rose_sk(sk);
73
74         del_timer(&rose->timer);
75
76         rose->timer.function = (TIMER_FUNC_TYPE)rose_timer_expiry;
77         rose->timer.expires  = jiffies + rose->t3;
78
79         add_timer(&rose->timer);
80 }
81
82 void rose_start_hbtimer(struct sock *sk)
83 {
84         struct rose_sock *rose = rose_sk(sk);
85
86         del_timer(&rose->timer);
87
88         rose->timer.function = (TIMER_FUNC_TYPE)rose_timer_expiry;
89         rose->timer.expires  = jiffies + rose->hb;
90
91         add_timer(&rose->timer);
92 }
93
94 void rose_start_idletimer(struct sock *sk)
95 {
96         struct rose_sock *rose = rose_sk(sk);
97
98         del_timer(&rose->idletimer);
99
100         if (rose->idle > 0) {
101                 rose->idletimer.function = (TIMER_FUNC_TYPE)rose_idletimer_expiry;
102                 rose->idletimer.expires  = jiffies + rose->idle;
103
104                 add_timer(&rose->idletimer);
105         }
106 }
107
108 void rose_stop_heartbeat(struct sock *sk)
109 {
110         del_timer(&sk->sk_timer);
111 }
112
113 void rose_stop_timer(struct sock *sk)
114 {
115         del_timer(&rose_sk(sk)->timer);
116 }
117
118 void rose_stop_idletimer(struct sock *sk)
119 {
120         del_timer(&rose_sk(sk)->idletimer);
121 }
122
123 static void rose_heartbeat_expiry(unsigned long param)
124 {
125         struct sock *sk = (struct sock *)param;
126         struct rose_sock *rose = rose_sk(sk);
127
128         bh_lock_sock(sk);
129         switch (rose->state) {
130         case ROSE_STATE_0:
131                 /* Magic here: If we listen() and a new link dies before it
132                    is accepted() it isn't 'dead' so doesn't get removed. */
133                 if (sock_flag(sk, SOCK_DESTROY) ||
134                     (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
135                         bh_unlock_sock(sk);
136                         rose_destroy_socket(sk);
137                         return;
138                 }
139                 break;
140
141         case ROSE_STATE_3:
142                 /*
143                  * Check for the state of the receive buffer.
144                  */
145                 if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
146                     (rose->condition & ROSE_COND_OWN_RX_BUSY)) {
147                         rose->condition &= ~ROSE_COND_OWN_RX_BUSY;
148                         rose->condition &= ~ROSE_COND_ACK_PENDING;
149                         rose->vl         = rose->vr;
150                         rose_write_internal(sk, ROSE_RR);
151                         rose_stop_timer(sk);    /* HB */
152                         break;
153                 }
154                 break;
155         }
156
157         rose_start_heartbeat(sk);
158         bh_unlock_sock(sk);
159 }
160
161 static void rose_timer_expiry(struct timer_list *t)
162 {
163         struct rose_sock *rose = from_timer(rose, t, timer);
164         struct sock *sk = &rose->sock;
165
166         bh_lock_sock(sk);
167         switch (rose->state) {
168         case ROSE_STATE_1:      /* T1 */
169         case ROSE_STATE_4:      /* T2 */
170                 rose_write_internal(sk, ROSE_CLEAR_REQUEST);
171                 rose->state = ROSE_STATE_2;
172                 rose_start_t3timer(sk);
173                 break;
174
175         case ROSE_STATE_2:      /* T3 */
176                 rose->neighbour->use--;
177                 rose_disconnect(sk, ETIMEDOUT, -1, -1);
178                 break;
179
180         case ROSE_STATE_3:      /* HB */
181                 if (rose->condition & ROSE_COND_ACK_PENDING) {
182                         rose->condition &= ~ROSE_COND_ACK_PENDING;
183                         rose_enquiry_response(sk);
184                 }
185                 break;
186         }
187         bh_unlock_sock(sk);
188 }
189
190 static void rose_idletimer_expiry(struct timer_list *t)
191 {
192         struct rose_sock *rose = from_timer(rose, t, idletimer);
193         struct sock *sk = &rose->sock;
194
195         bh_lock_sock(sk);
196         rose_clear_queues(sk);
197
198         rose_write_internal(sk, ROSE_CLEAR_REQUEST);
199         rose_sk(sk)->state = ROSE_STATE_2;
200
201         rose_start_t3timer(sk);
202
203         sk->sk_state     = TCP_CLOSE;
204         sk->sk_err       = 0;
205         sk->sk_shutdown |= SEND_SHUTDOWN;
206
207         if (!sock_flag(sk, SOCK_DEAD)) {
208                 sk->sk_state_change(sk);
209                 sock_set_flag(sk, SOCK_DEAD);
210         }
211         bh_unlock_sock(sk);
212 }