blob: ddba1c144260d4b6adfe1b288c9a68739d551b31 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070025#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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#include <net/netrom.h>
32
33static void nr_heartbeat_expiry(unsigned long);
34static void nr_t1timer_expiry(unsigned long);
35static void nr_t2timer_expiry(unsigned long);
36static void nr_t4timer_expiry(unsigned long);
37static void nr_idletimer_expiry(unsigned long);
38
39void nr_init_timers(struct sock *sk)
40{
41 struct nr_sock *nr = nr_sk(sk);
42
43 init_timer(&nr->t1timer);
44 nr->t1timer.data = (unsigned long)sk;
45 nr->t1timer.function = &nr_t1timer_expiry;
46
47 init_timer(&nr->t2timer);
48 nr->t2timer.data = (unsigned long)sk;
49 nr->t2timer.function = &nr_t2timer_expiry;
50
51 init_timer(&nr->t4timer);
52 nr->t4timer.data = (unsigned long)sk;
53 nr->t4timer.function = &nr_t4timer_expiry;
54
55 init_timer(&nr->idletimer);
56 nr->idletimer.data = (unsigned long)sk;
57 nr->idletimer.function = &nr_idletimer_expiry;
58
59 /* initialized by sock_init_data */
60 sk->sk_timer.data = (unsigned long)sk;
61 sk->sk_timer.function = &nr_heartbeat_expiry;
62}
63
64void nr_start_t1timer(struct sock *sk)
65{
66 struct nr_sock *nr = nr_sk(sk);
67
68 mod_timer(&nr->t1timer, jiffies + nr->t1);
69}
70
71void nr_start_t2timer(struct sock *sk)
72{
73 struct nr_sock *nr = nr_sk(sk);
74
75 mod_timer(&nr->t2timer, jiffies + nr->t2);
76}
77
78void nr_start_t4timer(struct sock *sk)
79{
80 struct nr_sock *nr = nr_sk(sk);
81
82 mod_timer(&nr->t4timer, jiffies + nr->t4);
83}
84
85void nr_start_idletimer(struct sock *sk)
86{
87 struct nr_sock *nr = nr_sk(sk);
88
89 if (nr->idle > 0)
90 mod_timer(&nr->idletimer, jiffies + nr->idle);
91}
92
93void nr_start_heartbeat(struct sock *sk)
94{
95 mod_timer(&sk->sk_timer, jiffies + 5 * HZ);
96}
97
98void nr_stop_t1timer(struct sock *sk)
99{
100 del_timer(&nr_sk(sk)->t1timer);
101}
102
103void nr_stop_t2timer(struct sock *sk)
104{
105 del_timer(&nr_sk(sk)->t2timer);
106}
107
108void nr_stop_t4timer(struct sock *sk)
109{
110 del_timer(&nr_sk(sk)->t4timer);
111}
112
113void nr_stop_idletimer(struct sock *sk)
114{
115 del_timer(&nr_sk(sk)->idletimer);
116}
117
118void nr_stop_heartbeat(struct sock *sk)
119{
120 del_timer(&sk->sk_timer);
121}
122
123int nr_t1timer_running(struct sock *sk)
124{
125 return timer_pending(&nr_sk(sk)->t1timer);
126}
127
128static void nr_heartbeat_expiry(unsigned long param)
129{
130 struct sock *sk = (struct sock *)param;
131 struct nr_sock *nr = nr_sk(sk);
132
133 bh_lock_sock(sk);
134 switch (nr->state) {
135 case NR_STATE_0:
136 /* Magic here: If we listen() and a new link dies before it
137 is accepted() it isn't 'dead' so doesn't get removed. */
138 if (sock_flag(sk, SOCK_DESTROY) ||
139 (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
140 sock_hold(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 bh_unlock_sock(sk);
Ralf Baechle8bf2b7b2006-07-10 20:21:05 -0700142 nr_destroy_socket(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 sock_put(sk);
144 return;
145 }
146 break;
147
148 case NR_STATE_3:
149 /*
150 * Check for the state of the receive buffer.
151 */
152 if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
153 (nr->condition & NR_COND_OWN_RX_BUSY)) {
154 nr->condition &= ~NR_COND_OWN_RX_BUSY;
155 nr->condition &= ~NR_COND_ACK_PENDING;
156 nr->vl = nr->vr;
157 nr_write_internal(sk, NR_INFOACK);
158 break;
159 }
160 break;
161 }
162
163 nr_start_heartbeat(sk);
164 bh_unlock_sock(sk);
165}
166
167static void nr_t2timer_expiry(unsigned long param)
168{
169 struct sock *sk = (struct sock *)param;
170 struct nr_sock *nr = nr_sk(sk);
171
172 bh_lock_sock(sk);
173 if (nr->condition & NR_COND_ACK_PENDING) {
174 nr->condition &= ~NR_COND_ACK_PENDING;
175 nr_enquiry_response(sk);
176 }
177 bh_unlock_sock(sk);
178}
179
180static void nr_t4timer_expiry(unsigned long param)
181{
182 struct sock *sk = (struct sock *)param;
183
184 bh_lock_sock(sk);
185 nr_sk(sk)->condition &= ~NR_COND_PEER_RX_BUSY;
186 bh_unlock_sock(sk);
187}
188
189static void nr_idletimer_expiry(unsigned long param)
190{
191 struct sock *sk = (struct sock *)param;
192 struct nr_sock *nr = nr_sk(sk);
193
194 bh_lock_sock(sk);
195
196 nr_clear_queues(sk);
197
198 nr->n2count = 0;
199 nr_write_internal(sk, NR_DISCREQ);
200 nr->state = NR_STATE_2;
201
202 nr_start_t1timer(sk);
203 nr_stop_t2timer(sk);
204 nr_stop_t4timer(sk);
205
206 sk->sk_state = TCP_CLOSE;
207 sk->sk_err = 0;
208 sk->sk_shutdown |= SEND_SHUTDOWN;
209
210 if (!sock_flag(sk, SOCK_DEAD)) {
211 sk->sk_state_change(sk);
212 sock_set_flag(sk, SOCK_DEAD);
213 }
214 bh_unlock_sock(sk);
215}
216
217static void nr_t1timer_expiry(unsigned long param)
218{
219 struct sock *sk = (struct sock *)param;
220 struct nr_sock *nr = nr_sk(sk);
221
222 bh_lock_sock(sk);
223 switch (nr->state) {
224 case NR_STATE_1:
225 if (nr->n2count == nr->n2) {
226 nr_disconnect(sk, ETIMEDOUT);
227 bh_unlock_sock(sk);
228 return;
229 } else {
230 nr->n2count++;
231 nr_write_internal(sk, NR_CONNREQ);
232 }
233 break;
234
235 case NR_STATE_2:
236 if (nr->n2count == nr->n2) {
237 nr_disconnect(sk, ETIMEDOUT);
238 bh_unlock_sock(sk);
239 return;
240 } else {
241 nr->n2count++;
242 nr_write_internal(sk, NR_DISCREQ);
243 }
244 break;
245
246 case NR_STATE_3:
247 if (nr->n2count == nr->n2) {
248 nr_disconnect(sk, ETIMEDOUT);
249 bh_unlock_sock(sk);
250 return;
251 } else {
252 nr->n2count++;
253 nr_requeue_frames(sk);
254 }
255 break;
256 }
257
258 nr_start_t1timer(sk);
259 bh_unlock_sock(sk);
260}