blob: 9115f8a7dd45b5ee7f8b3efc9d771f183b747013 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
5 * Copyright (C) 2002 Ralf Baechle DO1GRB (ralf@gnu.org)
6 */
7#include <linux/errno.h>
8#include <linux/types.h>
9#include <linux/socket.h>
10#include <linux/in.h>
11#include <linux/kernel.h>
12#include <linux/jiffies.h>
13#include <linux/timer.h>
14#include <linux/string.h>
15#include <linux/sockios.h>
16#include <linux/net.h>
17#include <net/ax25.h>
18#include <linux/inet.h>
19#include <linux/netdevice.h>
20#include <linux/skbuff.h>
21#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070022#include <net/tcp_states.h>
Fabian Frederickdc8e5412014-10-17 22:00:22 +020023#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/fcntl.h>
25#include <linux/mm.h>
26#include <linux/interrupt.h>
27#include <net/netrom.h>
28
Kees Cook99767f22017-10-16 17:29:36 -070029static void nr_heartbeat_expiry(struct timer_list *);
30static void nr_t1timer_expiry(struct timer_list *);
31static void nr_t2timer_expiry(struct timer_list *);
32static void nr_t4timer_expiry(struct timer_list *);
33static void nr_idletimer_expiry(struct timer_list *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35void nr_init_timers(struct sock *sk)
36{
37 struct nr_sock *nr = nr_sk(sk);
38
Kees Cook99767f22017-10-16 17:29:36 -070039 timer_setup(&nr->t1timer, nr_t1timer_expiry, 0);
40 timer_setup(&nr->t2timer, nr_t2timer_expiry, 0);
41 timer_setup(&nr->t4timer, nr_t4timer_expiry, 0);
42 timer_setup(&nr->idletimer, nr_idletimer_expiry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 /* initialized by sock_init_data */
Kees Cook841b86f2017-10-23 09:40:42 +020045 sk->sk_timer.function = nr_heartbeat_expiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
48void nr_start_t1timer(struct sock *sk)
49{
50 struct nr_sock *nr = nr_sk(sk);
51
Cong Wang63346652019-01-24 14:18:18 -080052 sk_reset_timer(sk, &nr->t1timer, jiffies + nr->t1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
55void nr_start_t2timer(struct sock *sk)
56{
57 struct nr_sock *nr = nr_sk(sk);
58
Cong Wang63346652019-01-24 14:18:18 -080059 sk_reset_timer(sk, &nr->t2timer, jiffies + nr->t2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
62void nr_start_t4timer(struct sock *sk)
63{
64 struct nr_sock *nr = nr_sk(sk);
65
Cong Wang63346652019-01-24 14:18:18 -080066 sk_reset_timer(sk, &nr->t4timer, jiffies + nr->t4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
69void nr_start_idletimer(struct sock *sk)
70{
71 struct nr_sock *nr = nr_sk(sk);
72
73 if (nr->idle > 0)
Cong Wang63346652019-01-24 14:18:18 -080074 sk_reset_timer(sk, &nr->idletimer, jiffies + nr->idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77void nr_start_heartbeat(struct sock *sk)
78{
Cong Wang63346652019-01-24 14:18:18 -080079 sk_reset_timer(sk, &sk->sk_timer, jiffies + 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82void nr_stop_t1timer(struct sock *sk)
83{
Cong Wang63346652019-01-24 14:18:18 -080084 sk_stop_timer(sk, &nr_sk(sk)->t1timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87void nr_stop_t2timer(struct sock *sk)
88{
Cong Wang63346652019-01-24 14:18:18 -080089 sk_stop_timer(sk, &nr_sk(sk)->t2timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92void nr_stop_t4timer(struct sock *sk)
93{
Cong Wang63346652019-01-24 14:18:18 -080094 sk_stop_timer(sk, &nr_sk(sk)->t4timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97void nr_stop_idletimer(struct sock *sk)
98{
Cong Wang63346652019-01-24 14:18:18 -080099 sk_stop_timer(sk, &nr_sk(sk)->idletimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102void nr_stop_heartbeat(struct sock *sk)
103{
Cong Wang63346652019-01-24 14:18:18 -0800104 sk_stop_timer(sk, &sk->sk_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107int nr_t1timer_running(struct sock *sk)
108{
109 return timer_pending(&nr_sk(sk)->t1timer);
110}
111
Kees Cook99767f22017-10-16 17:29:36 -0700112static void nr_heartbeat_expiry(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Kees Cook99767f22017-10-16 17:29:36 -0700114 struct sock *sk = from_timer(sk, t, sk_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 struct nr_sock *nr = nr_sk(sk);
116
117 bh_lock_sock(sk);
118 switch (nr->state) {
119 case NR_STATE_0:
120 /* Magic here: If we listen() and a new link dies before it
121 is accepted() it isn't 'dead' so doesn't get removed. */
122 if (sock_flag(sk, SOCK_DESTROY) ||
123 (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
124 sock_hold(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 bh_unlock_sock(sk);
Ralf Baechle8bf2b7b2006-07-10 20:21:05 -0700126 nr_destroy_socket(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 sock_put(sk);
128 return;
129 }
130 break;
131
132 case NR_STATE_3:
133 /*
134 * Check for the state of the receive buffer.
135 */
136 if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
137 (nr->condition & NR_COND_OWN_RX_BUSY)) {
138 nr->condition &= ~NR_COND_OWN_RX_BUSY;
139 nr->condition &= ~NR_COND_ACK_PENDING;
140 nr->vl = nr->vr;
141 nr_write_internal(sk, NR_INFOACK);
142 break;
143 }
144 break;
145 }
146
147 nr_start_heartbeat(sk);
148 bh_unlock_sock(sk);
149}
150
Kees Cook99767f22017-10-16 17:29:36 -0700151static void nr_t2timer_expiry(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Kees Cook99767f22017-10-16 17:29:36 -0700153 struct nr_sock *nr = from_timer(nr, t, t2timer);
154 struct sock *sk = &nr->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 bh_lock_sock(sk);
157 if (nr->condition & NR_COND_ACK_PENDING) {
158 nr->condition &= ~NR_COND_ACK_PENDING;
159 nr_enquiry_response(sk);
160 }
161 bh_unlock_sock(sk);
162}
163
Kees Cook99767f22017-10-16 17:29:36 -0700164static void nr_t4timer_expiry(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Kees Cook99767f22017-10-16 17:29:36 -0700166 struct nr_sock *nr = from_timer(nr, t, t4timer);
167 struct sock *sk = &nr->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 bh_lock_sock(sk);
170 nr_sk(sk)->condition &= ~NR_COND_PEER_RX_BUSY;
171 bh_unlock_sock(sk);
172}
173
Kees Cook99767f22017-10-16 17:29:36 -0700174static void nr_idletimer_expiry(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Kees Cook99767f22017-10-16 17:29:36 -0700176 struct nr_sock *nr = from_timer(nr, t, idletimer);
177 struct sock *sk = &nr->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 bh_lock_sock(sk);
180
181 nr_clear_queues(sk);
182
183 nr->n2count = 0;
184 nr_write_internal(sk, NR_DISCREQ);
185 nr->state = NR_STATE_2;
186
187 nr_start_t1timer(sk);
188 nr_stop_t2timer(sk);
189 nr_stop_t4timer(sk);
190
191 sk->sk_state = TCP_CLOSE;
192 sk->sk_err = 0;
193 sk->sk_shutdown |= SEND_SHUTDOWN;
194
195 if (!sock_flag(sk, SOCK_DEAD)) {
196 sk->sk_state_change(sk);
197 sock_set_flag(sk, SOCK_DEAD);
198 }
199 bh_unlock_sock(sk);
200}
201
Kees Cook99767f22017-10-16 17:29:36 -0700202static void nr_t1timer_expiry(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Kees Cook99767f22017-10-16 17:29:36 -0700204 struct nr_sock *nr = from_timer(nr, t, t1timer);
205 struct sock *sk = &nr->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 bh_lock_sock(sk);
208 switch (nr->state) {
209 case NR_STATE_1:
210 if (nr->n2count == nr->n2) {
211 nr_disconnect(sk, ETIMEDOUT);
212 bh_unlock_sock(sk);
213 return;
214 } else {
215 nr->n2count++;
216 nr_write_internal(sk, NR_CONNREQ);
217 }
218 break;
219
220 case NR_STATE_2:
221 if (nr->n2count == nr->n2) {
222 nr_disconnect(sk, ETIMEDOUT);
223 bh_unlock_sock(sk);
224 return;
225 } else {
226 nr->n2count++;
227 nr_write_internal(sk, NR_DISCREQ);
228 }
229 break;
230
231 case NR_STATE_3:
232 if (nr->n2count == nr->n2) {
233 nr_disconnect(sk, ETIMEDOUT);
234 bh_unlock_sock(sk);
235 return;
236 } else {
237 nr->n2count++;
238 nr_requeue_frames(sk);
239 }
240 break;
241 }
242
243 nr_start_t1timer(sk);
244 bh_unlock_sock(sk);
245}