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.
7 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 * Copyright (C) 2002 Ralf Baechle DO1GRB (ralf@gnu.org)
10 #include <linux/errno.h>
11 #include <linux/types.h>
12 #include <linux/socket.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>
21 #include <linux/inet.h>
22 #include <linux/netdevice.h>
23 #include <linux/skbuff.h>
25 #include <net/tcp_states.h>
26 #include <linux/uaccess.h>
27 #include <linux/fcntl.h>
29 #include <linux/interrupt.h>
30 #include <net/netrom.h>
32 static void nr_heartbeat_expiry(unsigned long);
33 static void nr_t1timer_expiry(unsigned long);
34 static void nr_t2timer_expiry(unsigned long);
35 static void nr_t4timer_expiry(unsigned long);
36 static void nr_idletimer_expiry(unsigned long);
38 void nr_init_timers(struct sock
*sk
)
40 struct nr_sock
*nr
= nr_sk(sk
);
42 setup_timer(&nr
->t1timer
, nr_t1timer_expiry
, (unsigned long)sk
);
43 setup_timer(&nr
->t2timer
, nr_t2timer_expiry
, (unsigned long)sk
);
44 setup_timer(&nr
->t4timer
, nr_t4timer_expiry
, (unsigned long)sk
);
45 setup_timer(&nr
->idletimer
, nr_idletimer_expiry
, (unsigned long)sk
);
47 /* initialized by sock_init_data */
48 sk
->sk_timer
.data
= (unsigned long)sk
;
49 sk
->sk_timer
.function
= &nr_heartbeat_expiry
;
52 void nr_start_t1timer(struct sock
*sk
)
54 struct nr_sock
*nr
= nr_sk(sk
);
56 sk_reset_timer(sk
, &nr
->t1timer
, jiffies
+ nr
->t1
);
59 void nr_start_t2timer(struct sock
*sk
)
61 struct nr_sock
*nr
= nr_sk(sk
);
63 sk_reset_timer(sk
, &nr
->t2timer
, jiffies
+ nr
->t2
);
66 void nr_start_t4timer(struct sock
*sk
)
68 struct nr_sock
*nr
= nr_sk(sk
);
70 sk_reset_timer(sk
, &nr
->t4timer
, jiffies
+ nr
->t4
);
73 void nr_start_idletimer(struct sock
*sk
)
75 struct nr_sock
*nr
= nr_sk(sk
);
78 sk_reset_timer(sk
, &nr
->idletimer
, jiffies
+ nr
->idle
);
81 void nr_start_heartbeat(struct sock
*sk
)
83 sk_reset_timer(sk
, &sk
->sk_timer
, jiffies
+ 5 * HZ
);
86 void nr_stop_t1timer(struct sock
*sk
)
88 sk_stop_timer(sk
, &nr_sk(sk
)->t1timer
);
91 void nr_stop_t2timer(struct sock
*sk
)
93 sk_stop_timer(sk
, &nr_sk(sk
)->t2timer
);
96 void nr_stop_t4timer(struct sock
*sk
)
98 sk_stop_timer(sk
, &nr_sk(sk
)->t4timer
);
101 void nr_stop_idletimer(struct sock
*sk
)
103 sk_stop_timer(sk
, &nr_sk(sk
)->idletimer
);
106 void nr_stop_heartbeat(struct sock
*sk
)
108 sk_stop_timer(sk
, &sk
->sk_timer
);
111 int nr_t1timer_running(struct sock
*sk
)
113 return timer_pending(&nr_sk(sk
)->t1timer
);
116 static void nr_heartbeat_expiry(unsigned long param
)
118 struct sock
*sk
= (struct sock
*)param
;
119 struct nr_sock
*nr
= nr_sk(sk
);
124 /* Magic here: If we listen() and a new link dies before it
125 is accepted() it isn't 'dead' so doesn't get removed. */
126 if (sock_flag(sk
, SOCK_DESTROY
) ||
127 (sk
->sk_state
== TCP_LISTEN
&& sock_flag(sk
, SOCK_DEAD
))) {
130 nr_destroy_socket(sk
);
138 * Check for the state of the receive buffer.
140 if (atomic_read(&sk
->sk_rmem_alloc
) < (sk
->sk_rcvbuf
/ 2) &&
141 (nr
->condition
& NR_COND_OWN_RX_BUSY
)) {
142 nr
->condition
&= ~NR_COND_OWN_RX_BUSY
;
143 nr
->condition
&= ~NR_COND_ACK_PENDING
;
145 nr_write_internal(sk
, NR_INFOACK
);
151 nr_start_heartbeat(sk
);
155 static void nr_t2timer_expiry(unsigned long param
)
157 struct sock
*sk
= (struct sock
*)param
;
158 struct nr_sock
*nr
= nr_sk(sk
);
161 if (nr
->condition
& NR_COND_ACK_PENDING
) {
162 nr
->condition
&= ~NR_COND_ACK_PENDING
;
163 nr_enquiry_response(sk
);
168 static void nr_t4timer_expiry(unsigned long param
)
170 struct sock
*sk
= (struct sock
*)param
;
173 nr_sk(sk
)->condition
&= ~NR_COND_PEER_RX_BUSY
;
177 static void nr_idletimer_expiry(unsigned long param
)
179 struct sock
*sk
= (struct sock
*)param
;
180 struct nr_sock
*nr
= nr_sk(sk
);
187 nr_write_internal(sk
, NR_DISCREQ
);
188 nr
->state
= NR_STATE_2
;
190 nr_start_t1timer(sk
);
194 sk
->sk_state
= TCP_CLOSE
;
196 sk
->sk_shutdown
|= SEND_SHUTDOWN
;
198 if (!sock_flag(sk
, SOCK_DEAD
)) {
199 sk
->sk_state_change(sk
);
200 sock_set_flag(sk
, SOCK_DEAD
);
205 static void nr_t1timer_expiry(unsigned long param
)
207 struct sock
*sk
= (struct sock
*)param
;
208 struct nr_sock
*nr
= nr_sk(sk
);
213 if (nr
->n2count
== nr
->n2
) {
214 nr_disconnect(sk
, ETIMEDOUT
);
219 nr_write_internal(sk
, NR_CONNREQ
);
224 if (nr
->n2count
== nr
->n2
) {
225 nr_disconnect(sk
, ETIMEDOUT
);
230 nr_write_internal(sk
, NR_DISCREQ
);
235 if (nr
->n2count
== nr
->n2
) {
236 nr_disconnect(sk
, ETIMEDOUT
);
241 nr_requeue_frames(sk
);
246 nr_start_t1timer(sk
);