blob: 5be68869064de037f7e4b324cbe37ca291fcd2ab [file] [log] [blame]
Thomas Gleixneree5d8f42019-05-20 19:08:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * LAPB release 002
4 *
5 * This code REQUIRES 2.1.15 or higher/ NET3.038
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * History
8 * LAPB 001 Jonathan Naylor Started Coding
9 * LAPB 002 Jonathan Naylor New timer architecture.
10 */
11
Joe Perchesa508da62012-05-17 10:25:49 +000012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/errno.h>
15#include <linux/types.h>
16#include <linux/socket.h>
17#include <linux/in.h>
18#include <linux/kernel.h>
19#include <linux/jiffies.h>
20#include <linux/timer.h>
21#include <linux/string.h>
22#include <linux/sockios.h>
23#include <linux/net.h>
24#include <linux/inet.h>
25#include <linux/skbuff.h>
26#include <net/sock.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080027#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/fcntl.h>
29#include <linux/mm.h>
30#include <linux/interrupt.h>
31#include <net/lapb.h>
32
Kees Cook83a37b32017-10-16 17:28:46 -070033static void lapb_t1timer_expiry(struct timer_list *);
34static void lapb_t2timer_expiry(struct timer_list *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36void lapb_start_t1timer(struct lapb_cb *lapb)
37{
38 del_timer(&lapb->t1timer);
39
Kees Cook841b86f2017-10-23 09:40:42 +020040 lapb->t1timer.function = lapb_t1timer_expiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 lapb->t1timer.expires = jiffies + lapb->t1;
42
Xie He65d2dbb2021-03-21 02:39:35 -070043 lapb->t1timer_running = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 add_timer(&lapb->t1timer);
45}
46
47void lapb_start_t2timer(struct lapb_cb *lapb)
48{
49 del_timer(&lapb->t2timer);
50
Kees Cook841b86f2017-10-23 09:40:42 +020051 lapb->t2timer.function = lapb_t2timer_expiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 lapb->t2timer.expires = jiffies + lapb->t2;
53
Xie He65d2dbb2021-03-21 02:39:35 -070054 lapb->t2timer_running = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 add_timer(&lapb->t2timer);
56}
57
58void lapb_stop_t1timer(struct lapb_cb *lapb)
59{
Xie He65d2dbb2021-03-21 02:39:35 -070060 lapb->t1timer_running = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 del_timer(&lapb->t1timer);
62}
63
64void lapb_stop_t2timer(struct lapb_cb *lapb)
65{
Xie He65d2dbb2021-03-21 02:39:35 -070066 lapb->t2timer_running = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 del_timer(&lapb->t2timer);
68}
69
70int lapb_t1timer_running(struct lapb_cb *lapb)
71{
Xie He65d2dbb2021-03-21 02:39:35 -070072 return lapb->t1timer_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Kees Cook83a37b32017-10-16 17:28:46 -070075static void lapb_t2timer_expiry(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Kees Cook83a37b32017-10-16 17:28:46 -070077 struct lapb_cb *lapb = from_timer(lapb, t, t2timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Xie Heb491e6a2021-01-25 20:09:39 -080079 spin_lock_bh(&lapb->lock);
80 if (timer_pending(&lapb->t2timer)) /* A new timer has been set up */
81 goto out;
Xie He65d2dbb2021-03-21 02:39:35 -070082 if (!lapb->t2timer_running) /* The timer has been stopped */
Xie Heb491e6a2021-01-25 20:09:39 -080083 goto out;
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (lapb->condition & LAPB_ACK_PENDING_CONDITION) {
86 lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
87 lapb_timeout_response(lapb);
88 }
Xie He65d2dbb2021-03-21 02:39:35 -070089 lapb->t2timer_running = false;
Xie Heb491e6a2021-01-25 20:09:39 -080090
91out:
92 spin_unlock_bh(&lapb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Kees Cook83a37b32017-10-16 17:28:46 -070095static void lapb_t1timer_expiry(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Kees Cook83a37b32017-10-16 17:28:46 -070097 struct lapb_cb *lapb = from_timer(lapb, t, t1timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Xie Heb491e6a2021-01-25 20:09:39 -080099 spin_lock_bh(&lapb->lock);
100 if (timer_pending(&lapb->t1timer)) /* A new timer has been set up */
101 goto out;
Xie He65d2dbb2021-03-21 02:39:35 -0700102 if (!lapb->t1timer_running) /* The timer has been stopped */
Xie Heb491e6a2021-01-25 20:09:39 -0800103 goto out;
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 switch (lapb->state) {
106
107 /*
Martin Schiller62480b92020-11-26 07:35:55 +0100108 * If we are a DCE, send DM up to N2 times, then switch to
109 * STATE_1 and send SABM(E).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 */
111 case LAPB_STATE_0:
Martin Schiller62480b92020-11-26 07:35:55 +0100112 if (lapb->mode & LAPB_DCE &&
113 lapb->n2count != lapb->n2) {
114 lapb->n2count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 lapb_send_control(lapb, LAPB_DM, LAPB_POLLOFF, LAPB_RESPONSE);
Martin Schiller62480b92020-11-26 07:35:55 +0100116 } else {
117 lapb->state = LAPB_STATE_1;
118 lapb_establish_data_link(lapb);
119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 break;
121
122 /*
123 * Awaiting connection state, send SABM(E), up to N2 times.
124 */
YOSHIFUJI Hideaki56d6c3d2007-02-09 23:24:59 +0900125 case LAPB_STATE_1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (lapb->n2count == lapb->n2) {
127 lapb_clear_queues(lapb);
128 lapb->state = LAPB_STATE_0;
129 lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
Joe Perchesa508da62012-05-17 10:25:49 +0000130 lapb_dbg(0, "(%p) S1 -> S0\n", lapb->dev);
Xie He65d2dbb2021-03-21 02:39:35 -0700131 lapb->t1timer_running = false;
Xie Heb491e6a2021-01-25 20:09:39 -0800132 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 } else {
134 lapb->n2count++;
135 if (lapb->mode & LAPB_EXTENDED) {
Joe Perchesa508da62012-05-17 10:25:49 +0000136 lapb_dbg(1, "(%p) S1 TX SABME(1)\n",
137 lapb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 lapb_send_control(lapb, LAPB_SABME, LAPB_POLLON, LAPB_COMMAND);
139 } else {
Joe Perchesa508da62012-05-17 10:25:49 +0000140 lapb_dbg(1, "(%p) S1 TX SABM(1)\n",
141 lapb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 lapb_send_control(lapb, LAPB_SABM, LAPB_POLLON, LAPB_COMMAND);
143 }
144 }
145 break;
146
147 /*
148 * Awaiting disconnection state, send DISC, up to N2 times.
149 */
150 case LAPB_STATE_2:
151 if (lapb->n2count == lapb->n2) {
152 lapb_clear_queues(lapb);
153 lapb->state = LAPB_STATE_0;
154 lapb_disconnect_confirmation(lapb, LAPB_TIMEDOUT);
Joe Perchesa508da62012-05-17 10:25:49 +0000155 lapb_dbg(0, "(%p) S2 -> S0\n", lapb->dev);
Xie He65d2dbb2021-03-21 02:39:35 -0700156 lapb->t1timer_running = false;
Xie Heb491e6a2021-01-25 20:09:39 -0800157 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 } else {
159 lapb->n2count++;
Joe Perchesa508da62012-05-17 10:25:49 +0000160 lapb_dbg(1, "(%p) S2 TX DISC(1)\n", lapb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
162 }
163 break;
164
165 /*
166 * Data transfer state, restransmit I frames, up to N2 times.
167 */
168 case LAPB_STATE_3:
169 if (lapb->n2count == lapb->n2) {
170 lapb_clear_queues(lapb);
171 lapb->state = LAPB_STATE_0;
172 lapb_stop_t2timer(lapb);
173 lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
Joe Perchesa508da62012-05-17 10:25:49 +0000174 lapb_dbg(0, "(%p) S3 -> S0\n", lapb->dev);
Xie He65d2dbb2021-03-21 02:39:35 -0700175 lapb->t1timer_running = false;
Xie Heb491e6a2021-01-25 20:09:39 -0800176 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 } else {
178 lapb->n2count++;
179 lapb_requeue_frames(lapb);
josselin.costanzi@mobile-devices.fra224bd32013-09-18 12:00:35 +0200180 lapb_kick(lapb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182 break;
183
184 /*
185 * Frame reject state, restransmit FRMR frames, up to N2 times.
186 */
187 case LAPB_STATE_4:
188 if (lapb->n2count == lapb->n2) {
189 lapb_clear_queues(lapb);
190 lapb->state = LAPB_STATE_0;
191 lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
Joe Perchesa508da62012-05-17 10:25:49 +0000192 lapb_dbg(0, "(%p) S4 -> S0\n", lapb->dev);
Xie He65d2dbb2021-03-21 02:39:35 -0700193 lapb->t1timer_running = false;
Xie Heb491e6a2021-01-25 20:09:39 -0800194 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 } else {
196 lapb->n2count++;
197 lapb_transmit_frmr(lapb);
198 }
199 break;
200 }
201
202 lapb_start_t1timer(lapb);
Xie Heb491e6a2021-01-25 20:09:39 -0800203
204out:
205 spin_unlock_bh(&lapb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}