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 <asm/uaccess.h>
27 #include <asm/system.h>
28 #include <linux/fcntl.h>
30 #include <linux/interrupt.h>
31 #include <net/netrom.h>
33 static void nr_heartbeat_expiry(unsigned long);
34 static void nr_t1timer_expiry(unsigned long);
35 static void nr_t2timer_expiry(unsigned long);
36 static void nr_t4timer_expiry(unsigned long);
37 static void nr_idletimer_expiry(unsigned long);
39 void nr_init_timers(struct sock
*sk
)
41 struct nr_sock
*nr
= nr_sk(sk
);
43 setup_timer(&nr
->t1timer
, nr_t1timer_expiry
, (unsigned long)sk
);
44 setup_timer(&nr
->t2timer
, nr_t2timer_expiry
, (unsigned long)sk
);
45 setup_timer(&nr
->t4timer
, nr_t4timer_expiry
, (unsigned long)sk
);
46 setup_timer(&nr
->idletimer
, nr_idletimer_expiry
, (unsigned long)sk
);
48 /* initialized by sock_init_data */
49 sk
->sk_timer
.data
= (unsigned long)sk
;
50 sk
->sk_timer
.function
= &nr_heartbeat_expiry
;
53 void nr_start_t1timer(struct sock
*sk
)
55 struct nr_sock
*nr
= nr_sk(sk
);
57 mod_timer(&nr
->t1timer
, jiffies
+ nr
->t1
);
60 void nr_start_t2timer(struct sock
*sk
)
62 struct nr_sock
*nr
= nr_sk(sk
);
64 mod_timer(&nr
->t2timer
, jiffies
+ nr
->t2
);
67 void nr_start_t4timer(struct sock
*sk
)
69 struct nr_sock
*nr
= nr_sk(sk
);
71 mod_timer(&nr
->t4timer
, jiffies
+ nr
->t4
);
74 void nr_start_idletimer(struct sock
*sk
)
76 struct nr_sock
*nr
= nr_sk(sk
);
79 mod_timer(&nr
->idletimer
, jiffies
+ nr
->idle
);
82 void nr_start_heartbeat(struct sock
*sk
)
84 mod_timer(&sk
->sk_timer
, jiffies
+ 5 * HZ
);
87 void nr_stop_t1timer(struct sock
*sk
)
89 del_timer(&nr_sk(sk
)->t1timer
);
92 void nr_stop_t2timer(struct sock
*sk
)
94 del_timer(&nr_sk(sk
)->t2timer
);
97 void nr_stop_t4timer(struct sock
*sk
)
99 del_timer(&nr_sk(sk
)->t4timer
);
102 void nr_stop_idletimer(struct sock
*sk
)
104 del_timer(&nr_sk(sk
)->idletimer
);
107 void nr_stop_heartbeat(struct sock
*sk
)
109 del_timer(&sk
->sk_timer
);
112 int nr_t1timer_running(struct sock
*sk
)
114 return timer_pending(&nr_sk(sk
)->t1timer
);
117 static void nr_heartbeat_expiry(unsigned long param
)
119 struct sock
*sk
= (struct sock
*)param
;
120 struct nr_sock
*nr
= nr_sk(sk
);
125 /* Magic here: If we listen() and a new link dies before it
126 is accepted() it isn't 'dead' so doesn't get removed. */
127 if (sock_flag(sk
, SOCK_DESTROY
) ||
128 (sk
->sk_state
== TCP_LISTEN
&& sock_flag(sk
, SOCK_DEAD
))) {
131 nr_destroy_socket(sk
);
139 * Check for the state of the receive buffer.
141 if (atomic_read(&sk
->sk_rmem_alloc
) < (sk
->sk_rcvbuf
/ 2) &&
142 (nr
->condition
& NR_COND_OWN_RX_BUSY
)) {
143 nr
->condition
&= ~NR_COND_OWN_RX_BUSY
;
144 nr
->condition
&= ~NR_COND_ACK_PENDING
;
146 nr_write_internal(sk
, NR_INFOACK
);
152 nr_start_heartbeat(sk
);
156 static void nr_t2timer_expiry(unsigned long param
)
158 struct sock
*sk
= (struct sock
*)param
;
159 struct nr_sock
*nr
= nr_sk(sk
);
162 if (nr
->condition
& NR_COND_ACK_PENDING
) {
163 nr
->condition
&= ~NR_COND_ACK_PENDING
;
164 nr_enquiry_response(sk
);
169 static void nr_t4timer_expiry(unsigned long param
)
171 struct sock
*sk
= (struct sock
*)param
;
174 nr_sk(sk
)->condition
&= ~NR_COND_PEER_RX_BUSY
;
178 static void nr_idletimer_expiry(unsigned long param
)
180 struct sock
*sk
= (struct sock
*)param
;
181 struct nr_sock
*nr
= nr_sk(sk
);
188 nr_write_internal(sk
, NR_DISCREQ
);
189 nr
->state
= NR_STATE_2
;
191 nr_start_t1timer(sk
);
195 sk
->sk_state
= TCP_CLOSE
;
197 sk
->sk_shutdown
|= SEND_SHUTDOWN
;
199 if (!sock_flag(sk
, SOCK_DEAD
)) {
200 sk
->sk_state_change(sk
);
201 sock_set_flag(sk
, SOCK_DEAD
);
206 static void nr_t1timer_expiry(unsigned long param
)
208 struct sock
*sk
= (struct sock
*)param
;
209 struct nr_sock
*nr
= nr_sk(sk
);
214 if (nr
->n2count
== nr
->n2
) {
215 nr_disconnect(sk
, ETIMEDOUT
);
220 nr_write_internal(sk
, NR_CONNREQ
);
225 if (nr
->n2count
== nr
->n2
) {
226 nr_disconnect(sk
, ETIMEDOUT
);
231 nr_write_internal(sk
, NR_DISCREQ
);
236 if (nr
->n2count
== nr
->n2
) {
237 nr_disconnect(sk
, ETIMEDOUT
);
242 nr_requeue_frames(sk
);
247 nr_start_t1timer(sk
);