blob: baa247fe4ed057a13b3a1fb257bf5685570f35f4 [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
43 add_timer(&lapb->t1timer);
44}
45
46void lapb_start_t2timer(struct lapb_cb *lapb)
47{
48 del_timer(&lapb->t2timer);
49
Kees Cook841b86f2017-10-23 09:40:42 +020050 lapb->t2timer.function = lapb_t2timer_expiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 lapb->t2timer.expires = jiffies + lapb->t2;
52
53 add_timer(&lapb->t2timer);
54}
55
56void lapb_stop_t1timer(struct lapb_cb *lapb)
57{
58 del_timer(&lapb->t1timer);
59}
60
61void lapb_stop_t2timer(struct lapb_cb *lapb)
62{
63 del_timer(&lapb->t2timer);
64}
65
66int lapb_t1timer_running(struct lapb_cb *lapb)
67{
68 return timer_pending(&lapb->t1timer);
69}
70
Kees Cook83a37b32017-10-16 17:28:46 -070071static void lapb_t2timer_expiry(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Kees Cook83a37b32017-10-16 17:28:46 -070073 struct lapb_cb *lapb = from_timer(lapb, t, t2timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 if (lapb->condition & LAPB_ACK_PENDING_CONDITION) {
76 lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
77 lapb_timeout_response(lapb);
78 }
79}
80
Kees Cook83a37b32017-10-16 17:28:46 -070081static void lapb_t1timer_expiry(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Kees Cook83a37b32017-10-16 17:28:46 -070083 struct lapb_cb *lapb = from_timer(lapb, t, t1timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 switch (lapb->state) {
86
87 /*
Martin Schiller62480b92020-11-26 07:35:55 +010088 * If we are a DCE, send DM up to N2 times, then switch to
89 * STATE_1 and send SABM(E).
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
91 case LAPB_STATE_0:
Martin Schiller62480b92020-11-26 07:35:55 +010092 if (lapb->mode & LAPB_DCE &&
93 lapb->n2count != lapb->n2) {
94 lapb->n2count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 lapb_send_control(lapb, LAPB_DM, LAPB_POLLOFF, LAPB_RESPONSE);
Martin Schiller62480b92020-11-26 07:35:55 +010096 } else {
97 lapb->state = LAPB_STATE_1;
98 lapb_establish_data_link(lapb);
99 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 break;
101
102 /*
103 * Awaiting connection state, send SABM(E), up to N2 times.
104 */
YOSHIFUJI Hideaki56d6c3d2007-02-09 23:24:59 +0900105 case LAPB_STATE_1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (lapb->n2count == lapb->n2) {
107 lapb_clear_queues(lapb);
108 lapb->state = LAPB_STATE_0;
109 lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
Joe Perchesa508da62012-05-17 10:25:49 +0000110 lapb_dbg(0, "(%p) S1 -> S0\n", lapb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return;
112 } else {
113 lapb->n2count++;
114 if (lapb->mode & LAPB_EXTENDED) {
Joe Perchesa508da62012-05-17 10:25:49 +0000115 lapb_dbg(1, "(%p) S1 TX SABME(1)\n",
116 lapb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 lapb_send_control(lapb, LAPB_SABME, LAPB_POLLON, LAPB_COMMAND);
118 } else {
Joe Perchesa508da62012-05-17 10:25:49 +0000119 lapb_dbg(1, "(%p) S1 TX SABM(1)\n",
120 lapb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 lapb_send_control(lapb, LAPB_SABM, LAPB_POLLON, LAPB_COMMAND);
122 }
123 }
124 break;
125
126 /*
127 * Awaiting disconnection state, send DISC, up to N2 times.
128 */
129 case LAPB_STATE_2:
130 if (lapb->n2count == lapb->n2) {
131 lapb_clear_queues(lapb);
132 lapb->state = LAPB_STATE_0;
133 lapb_disconnect_confirmation(lapb, LAPB_TIMEDOUT);
Joe Perchesa508da62012-05-17 10:25:49 +0000134 lapb_dbg(0, "(%p) S2 -> S0\n", lapb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return;
136 } else {
137 lapb->n2count++;
Joe Perchesa508da62012-05-17 10:25:49 +0000138 lapb_dbg(1, "(%p) S2 TX DISC(1)\n", lapb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
140 }
141 break;
142
143 /*
144 * Data transfer state, restransmit I frames, up to N2 times.
145 */
146 case LAPB_STATE_3:
147 if (lapb->n2count == lapb->n2) {
148 lapb_clear_queues(lapb);
149 lapb->state = LAPB_STATE_0;
150 lapb_stop_t2timer(lapb);
151 lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
Joe Perchesa508da62012-05-17 10:25:49 +0000152 lapb_dbg(0, "(%p) S3 -> S0\n", lapb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return;
154 } else {
155 lapb->n2count++;
156 lapb_requeue_frames(lapb);
josselin.costanzi@mobile-devices.fra224bd32013-09-18 12:00:35 +0200157 lapb_kick(lapb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159 break;
160
161 /*
162 * Frame reject state, restransmit FRMR frames, up to N2 times.
163 */
164 case LAPB_STATE_4:
165 if (lapb->n2count == lapb->n2) {
166 lapb_clear_queues(lapb);
167 lapb->state = LAPB_STATE_0;
168 lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
Joe Perchesa508da62012-05-17 10:25:49 +0000169 lapb_dbg(0, "(%p) S4 -> S0\n", lapb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return;
171 } else {
172 lapb->n2count++;
173 lapb_transmit_frmr(lapb);
174 }
175 break;
176 }
177
178 lapb_start_t1timer(lapb);
179}