blob: a7b34489693615fe2f2024741e1ca311969738f1 [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 <linux/fcntl.h>
27#include <linux/mm.h>
28#include <linux/interrupt.h>
29#include <net/rose.h>
30
31static void rose_heartbeat_expiry(unsigned long);
32static void rose_timer_expiry(unsigned long);
33static void rose_idletimer_expiry(unsigned long);
34
35void rose_start_heartbeat(struct sock *sk)
36{
Duoming Zhou3ab68a92022-06-29 08:26:40 +080037 sk_stop_timer(sk, &sk->sk_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
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
Duoming Zhou3ab68a92022-06-29 08:26:40 +080043 sk_reset_timer(sk, &sk->sk_timer, sk->sk_timer.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
46void rose_start_t1timer(struct sock *sk)
47{
48 struct rose_sock *rose = rose_sk(sk);
49
Duoming Zhou3ab68a92022-06-29 08:26:40 +080050 sk_stop_timer(sk, &rose->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 rose->timer.data = (unsigned long)sk;
53 rose->timer.function = &rose_timer_expiry;
54 rose->timer.expires = jiffies + rose->t1;
55
Duoming Zhou3ab68a92022-06-29 08:26:40 +080056 sk_reset_timer(sk, &rose->timer, rose->timer.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59void rose_start_t2timer(struct sock *sk)
60{
61 struct rose_sock *rose = rose_sk(sk);
62
Duoming Zhou3ab68a92022-06-29 08:26:40 +080063 sk_stop_timer(sk, &rose->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 rose->timer.data = (unsigned long)sk;
66 rose->timer.function = &rose_timer_expiry;
67 rose->timer.expires = jiffies + rose->t2;
68
Duoming Zhou3ab68a92022-06-29 08:26:40 +080069 sk_reset_timer(sk, &rose->timer, rose->timer.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72void rose_start_t3timer(struct sock *sk)
73{
74 struct rose_sock *rose = rose_sk(sk);
75
Duoming Zhou3ab68a92022-06-29 08:26:40 +080076 sk_stop_timer(sk, &rose->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 rose->timer.data = (unsigned long)sk;
79 rose->timer.function = &rose_timer_expiry;
80 rose->timer.expires = jiffies + rose->t3;
81
Duoming Zhou3ab68a92022-06-29 08:26:40 +080082 sk_reset_timer(sk, &rose->timer, rose->timer.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85void rose_start_hbtimer(struct sock *sk)
86{
87 struct rose_sock *rose = rose_sk(sk);
88
Duoming Zhou3ab68a92022-06-29 08:26:40 +080089 sk_stop_timer(sk, &rose->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 rose->timer.data = (unsigned long)sk;
92 rose->timer.function = &rose_timer_expiry;
93 rose->timer.expires = jiffies + rose->hb;
94
Duoming Zhou3ab68a92022-06-29 08:26:40 +080095 sk_reset_timer(sk, &rose->timer, rose->timer.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98void rose_start_idletimer(struct sock *sk)
99{
100 struct rose_sock *rose = rose_sk(sk);
101
Duoming Zhou3ab68a92022-06-29 08:26:40 +0800102 sk_stop_timer(sk, &rose->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 if (rose->idle > 0) {
105 rose->idletimer.data = (unsigned long)sk;
106 rose->idletimer.function = &rose_idletimer_expiry;
107 rose->idletimer.expires = jiffies + rose->idle;
108
Duoming Zhou3ab68a92022-06-29 08:26:40 +0800109 sk_reset_timer(sk, &rose->idletimer, rose->idletimer.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111}
112
113void rose_stop_heartbeat(struct sock *sk)
114{
Duoming Zhou3ab68a92022-06-29 08:26:40 +0800115 sk_stop_timer(sk, &sk->sk_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118void rose_stop_timer(struct sock *sk)
119{
Duoming Zhou3ab68a92022-06-29 08:26:40 +0800120 sk_stop_timer(sk, &rose_sk(sk)->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123void rose_stop_idletimer(struct sock *sk)
124{
Duoming Zhou3ab68a92022-06-29 08:26:40 +0800125 sk_stop_timer(sk, &rose_sk(sk)->idletimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128static void rose_heartbeat_expiry(unsigned long param)
129{
130 struct sock *sk = (struct sock *)param;
131 struct rose_sock *rose = rose_sk(sk);
132
133 bh_lock_sock(sk);
134 switch (rose->state) {
135 case ROSE_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))) {
Andrew Mortona3d7a9d2005-10-28 15:12:02 -0700140 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 rose_destroy_socket(sk);
Duoming Zhou3ab68a92022-06-29 08:26:40 +0800142 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return;
144 }
145 break;
146
147 case ROSE_STATE_3:
148 /*
149 * Check for the state of the receive buffer.
150 */
151 if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
152 (rose->condition & ROSE_COND_OWN_RX_BUSY)) {
153 rose->condition &= ~ROSE_COND_OWN_RX_BUSY;
154 rose->condition &= ~ROSE_COND_ACK_PENDING;
155 rose->vl = rose->vr;
156 rose_write_internal(sk, ROSE_RR);
157 rose_stop_timer(sk); /* HB */
158 break;
159 }
160 break;
161 }
162
163 rose_start_heartbeat(sk);
164 bh_unlock_sock(sk);
Duoming Zhou3ab68a92022-06-29 08:26:40 +0800165 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168static void rose_timer_expiry(unsigned long param)
169{
170 struct sock *sk = (struct sock *)param;
171 struct rose_sock *rose = rose_sk(sk);
172
173 bh_lock_sock(sk);
174 switch (rose->state) {
175 case ROSE_STATE_1: /* T1 */
176 case ROSE_STATE_4: /* T2 */
177 rose_write_internal(sk, ROSE_CLEAR_REQUEST);
178 rose->state = ROSE_STATE_2;
179 rose_start_t3timer(sk);
180 break;
181
182 case ROSE_STATE_2: /* T3 */
183 rose->neighbour->use--;
184 rose_disconnect(sk, ETIMEDOUT, -1, -1);
185 break;
186
187 case ROSE_STATE_3: /* HB */
188 if (rose->condition & ROSE_COND_ACK_PENDING) {
189 rose->condition &= ~ROSE_COND_ACK_PENDING;
190 rose_enquiry_response(sk);
191 }
192 break;
193 }
194 bh_unlock_sock(sk);
Duoming Zhou3ab68a92022-06-29 08:26:40 +0800195 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198static void rose_idletimer_expiry(unsigned long param)
199{
200 struct sock *sk = (struct sock *)param;
201
202 bh_lock_sock(sk);
203 rose_clear_queues(sk);
204
205 rose_write_internal(sk, ROSE_CLEAR_REQUEST);
206 rose_sk(sk)->state = ROSE_STATE_2;
207
208 rose_start_t3timer(sk);
209
210 sk->sk_state = TCP_CLOSE;
211 sk->sk_err = 0;
212 sk->sk_shutdown |= SEND_SHUTDOWN;
213
214 if (!sock_flag(sk, SOCK_DEAD)) {
215 sk->sk_state_change(sk);
216 sock_set_flag(sk, SOCK_DEAD);
217 }
218 bh_unlock_sock(sk);
Duoming Zhou3ab68a92022-06-29 08:26:40 +0800219 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}