1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * This code REQUIRES 2.1.15 or higher/ NET3.038
8 * LAPB 001 Jonathan Naylor Started Coding
9 * LAPB 002 Jonathan Naylor New timer architecture.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/socket.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>
27 #include <linux/uaccess.h>
28 #include <linux/fcntl.h>
30 #include <linux/interrupt.h>
33 static void lapb_t1timer_expiry(struct timer_list *);
34 static void lapb_t2timer_expiry(struct timer_list *);
36 void lapb_start_t1timer(struct lapb_cb *lapb)
38 del_timer(&lapb->t1timer);
40 lapb->t1timer.function = lapb_t1timer_expiry;
41 lapb->t1timer.expires = jiffies + lapb->t1;
43 lapb->t1timer_running = true;
44 add_timer(&lapb->t1timer);
47 void lapb_start_t2timer(struct lapb_cb *lapb)
49 del_timer(&lapb->t2timer);
51 lapb->t2timer.function = lapb_t2timer_expiry;
52 lapb->t2timer.expires = jiffies + lapb->t2;
54 lapb->t2timer_running = true;
55 add_timer(&lapb->t2timer);
58 void lapb_stop_t1timer(struct lapb_cb *lapb)
60 lapb->t1timer_running = false;
61 del_timer(&lapb->t1timer);
64 void lapb_stop_t2timer(struct lapb_cb *lapb)
66 lapb->t2timer_running = false;
67 del_timer(&lapb->t2timer);
70 int lapb_t1timer_running(struct lapb_cb *lapb)
72 return lapb->t1timer_running;
75 static void lapb_t2timer_expiry(struct timer_list *t)
77 struct lapb_cb *lapb = from_timer(lapb, t, t2timer);
79 spin_lock_bh(&lapb->lock);
80 if (timer_pending(&lapb->t2timer)) /* A new timer has been set up */
82 if (!lapb->t2timer_running) /* The timer has been stopped */
85 if (lapb->condition & LAPB_ACK_PENDING_CONDITION) {
86 lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
87 lapb_timeout_response(lapb);
89 lapb->t2timer_running = false;
92 spin_unlock_bh(&lapb->lock);
95 static void lapb_t1timer_expiry(struct timer_list *t)
97 struct lapb_cb *lapb = from_timer(lapb, t, t1timer);
99 spin_lock_bh(&lapb->lock);
100 if (timer_pending(&lapb->t1timer)) /* A new timer has been set up */
102 if (!lapb->t1timer_running) /* The timer has been stopped */
105 switch (lapb->state) {
108 * If we are a DCE, send DM up to N2 times, then switch to
109 * STATE_1 and send SABM(E).
112 if (lapb->mode & LAPB_DCE &&
113 lapb->n2count != lapb->n2) {
115 lapb_send_control(lapb, LAPB_DM, LAPB_POLLOFF, LAPB_RESPONSE);
117 lapb->state = LAPB_STATE_1;
118 lapb_establish_data_link(lapb);
123 * Awaiting connection state, send SABM(E), up to N2 times.
126 if (lapb->n2count == lapb->n2) {
127 lapb_clear_queues(lapb);
128 lapb->state = LAPB_STATE_0;
129 lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
130 lapb_dbg(0, "(%p) S1 -> S0\n", lapb->dev);
131 lapb->t1timer_running = false;
135 if (lapb->mode & LAPB_EXTENDED) {
136 lapb_dbg(1, "(%p) S1 TX SABME(1)\n",
138 lapb_send_control(lapb, LAPB_SABME, LAPB_POLLON, LAPB_COMMAND);
140 lapb_dbg(1, "(%p) S1 TX SABM(1)\n",
142 lapb_send_control(lapb, LAPB_SABM, LAPB_POLLON, LAPB_COMMAND);
148 * Awaiting disconnection state, send DISC, up to N2 times.
151 if (lapb->n2count == lapb->n2) {
152 lapb_clear_queues(lapb);
153 lapb->state = LAPB_STATE_0;
154 lapb_disconnect_confirmation(lapb, LAPB_TIMEDOUT);
155 lapb_dbg(0, "(%p) S2 -> S0\n", lapb->dev);
156 lapb->t1timer_running = false;
160 lapb_dbg(1, "(%p) S2 TX DISC(1)\n", lapb->dev);
161 lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
166 * Data transfer state, restransmit I frames, up to N2 times.
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);
174 lapb_dbg(0, "(%p) S3 -> S0\n", lapb->dev);
175 lapb->t1timer_running = false;
179 lapb_requeue_frames(lapb);
185 * Frame reject state, restransmit FRMR frames, up to N2 times.
188 if (lapb->n2count == lapb->n2) {
189 lapb_clear_queues(lapb);
190 lapb->state = LAPB_STATE_0;
191 lapb_disconnect_indication(lapb, LAPB_TIMEDOUT);
192 lapb_dbg(0, "(%p) S4 -> S0\n", lapb->dev);
193 lapb->t1timer_running = false;
197 lapb_transmit_frmr(lapb);
202 lapb_start_t1timer(lapb);
205 spin_unlock_bh(&lapb->lock);