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>
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 init_timer(&nr
->t1timer
);
44 nr
->t1timer
.data
= (unsigned long)sk
;
45 nr
->t1timer
.function
= &nr_t1timer_expiry
;
47 init_timer(&nr
->t2timer
);
48 nr
->t2timer
.data
= (unsigned long)sk
;
49 nr
->t2timer
.function
= &nr_t2timer_expiry
;
51 init_timer(&nr
->t4timer
);
52 nr
->t4timer
.data
= (unsigned long)sk
;
53 nr
->t4timer
.function
= &nr_t4timer_expiry
;
55 init_timer(&nr
->idletimer
);
56 nr
->idletimer
.data
= (unsigned long)sk
;
57 nr
->idletimer
.function
= &nr_idletimer_expiry
;
59 /* initialized by sock_init_data */
60 sk
->sk_timer
.data
= (unsigned long)sk
;
61 sk
->sk_timer
.function
= &nr_heartbeat_expiry
;
64 void nr_start_t1timer(struct sock
*sk
)
66 struct nr_sock
*nr
= nr_sk(sk
);
68 mod_timer(&nr
->t1timer
, jiffies
+ nr
->t1
);
71 void nr_start_t2timer(struct sock
*sk
)
73 struct nr_sock
*nr
= nr_sk(sk
);
75 mod_timer(&nr
->t2timer
, jiffies
+ nr
->t2
);
78 void nr_start_t4timer(struct sock
*sk
)
80 struct nr_sock
*nr
= nr_sk(sk
);
82 mod_timer(&nr
->t4timer
, jiffies
+ nr
->t4
);
85 void nr_start_idletimer(struct sock
*sk
)
87 struct nr_sock
*nr
= nr_sk(sk
);
90 mod_timer(&nr
->idletimer
, jiffies
+ nr
->idle
);
93 void nr_start_heartbeat(struct sock
*sk
)
95 mod_timer(&sk
->sk_timer
, jiffies
+ 5 * HZ
);
98 void nr_stop_t1timer(struct sock
*sk
)
100 del_timer(&nr_sk(sk
)->t1timer
);
103 void nr_stop_t2timer(struct sock
*sk
)
105 del_timer(&nr_sk(sk
)->t2timer
);
108 void nr_stop_t4timer(struct sock
*sk
)
110 del_timer(&nr_sk(sk
)->t4timer
);
113 void nr_stop_idletimer(struct sock
*sk
)
115 del_timer(&nr_sk(sk
)->idletimer
);
118 void nr_stop_heartbeat(struct sock
*sk
)
120 del_timer(&sk
->sk_timer
);
123 int nr_t1timer_running(struct sock
*sk
)
125 return timer_pending(&nr_sk(sk
)->t1timer
);
128 static void nr_heartbeat_expiry(unsigned long param
)
130 struct sock
*sk
= (struct sock
*)param
;
131 struct nr_sock
*nr
= nr_sk(sk
);
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
))) {
141 nr_destroy_socket(sk
);
150 * Check for the state of the receive buffer.
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
;
157 nr_write_internal(sk
, NR_INFOACK
);
163 nr_start_heartbeat(sk
);
167 static void nr_t2timer_expiry(unsigned long param
)
169 struct sock
*sk
= (struct sock
*)param
;
170 struct nr_sock
*nr
= nr_sk(sk
);
173 if (nr
->condition
& NR_COND_ACK_PENDING
) {
174 nr
->condition
&= ~NR_COND_ACK_PENDING
;
175 nr_enquiry_response(sk
);
180 static void nr_t4timer_expiry(unsigned long param
)
182 struct sock
*sk
= (struct sock
*)param
;
185 nr_sk(sk
)->condition
&= ~NR_COND_PEER_RX_BUSY
;
189 static void nr_idletimer_expiry(unsigned long param
)
191 struct sock
*sk
= (struct sock
*)param
;
192 struct nr_sock
*nr
= nr_sk(sk
);
199 nr_write_internal(sk
, NR_DISCREQ
);
200 nr
->state
= NR_STATE_2
;
202 nr_start_t1timer(sk
);
206 sk
->sk_state
= TCP_CLOSE
;
208 sk
->sk_shutdown
|= SEND_SHUTDOWN
;
210 if (!sock_flag(sk
, SOCK_DEAD
)) {
211 sk
->sk_state_change(sk
);
212 sock_set_flag(sk
, SOCK_DEAD
);
217 static void nr_t1timer_expiry(unsigned long param
)
219 struct sock
*sk
= (struct sock
*)param
;
220 struct nr_sock
*nr
= nr_sk(sk
);
225 if (nr
->n2count
== nr
->n2
) {
226 nr_disconnect(sk
, ETIMEDOUT
);
231 nr_write_internal(sk
, NR_CONNREQ
);
236 if (nr
->n2count
== nr
->n2
) {
237 nr_disconnect(sk
, ETIMEDOUT
);
242 nr_write_internal(sk
, NR_DISCREQ
);
247 if (nr
->n2count
== nr
->n2
) {
248 nr_disconnect(sk
, ETIMEDOUT
);
253 nr_requeue_frames(sk
);
258 nr_start_t1timer(sk
);