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/timer.h>
20 #include <linux/string.h>
21 #include <linux/sockios.h>
22 #include <linux/net.h>
23 #include <linux/inet.h>
24 #include <linux/skbuff.h>
25 #include <linux/slab.h>
27 #include <linux/uaccess.h>
28 #include <linux/fcntl.h>
30 #include <linux/interrupt.h>
34 * This procedure is passed a buffer descriptor for an iframe. It builds
35 * the rest of the control part of the frame and then writes it out.
37 static void lapb_send_iframe(struct lapb_cb *lapb, struct sk_buff *skb, int poll_bit)
44 if (lapb->mode & LAPB_EXTENDED) {
45 frame = skb_push(skb, 2);
48 frame[0] |= lapb->vs << 1;
49 frame[1] = poll_bit ? LAPB_EPF : 0;
50 frame[1] |= lapb->vr << 1;
52 frame = skb_push(skb, 1);
55 *frame |= poll_bit ? LAPB_SPF : 0;
56 *frame |= lapb->vr << 5;
57 *frame |= lapb->vs << 1;
60 lapb_dbg(1, "(%p) S%d TX I(%d) S%d R%d\n",
61 lapb->dev, lapb->state, poll_bit, lapb->vs, lapb->vr);
63 lapb_transmit_buffer(lapb, skb, LAPB_COMMAND);
66 void lapb_kick(struct lapb_cb *lapb)
68 struct sk_buff *skb, *skbn;
69 unsigned short modulus, start, end;
71 modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS;
72 start = !skb_peek(&lapb->ack_queue) ? lapb->va : lapb->vs;
73 end = (lapb->va + lapb->window) % modulus;
75 if (!(lapb->condition & LAPB_PEER_RX_BUSY_CONDITION) &&
76 start != end && skb_peek(&lapb->write_queue)) {
80 * Dequeue the frame and copy it.
82 skb = skb_dequeue(&lapb->write_queue);
85 skbn = skb_copy(skb, GFP_ATOMIC);
87 skb_queue_head(&lapb->write_queue, skb);
92 skb_set_owner_w(skbn, skb->sk);
95 * Transmit the frame copy.
97 lapb_send_iframe(lapb, skbn, LAPB_POLLOFF);
99 lapb->vs = (lapb->vs + 1) % modulus;
102 * Requeue the original data frame.
104 skb_queue_tail(&lapb->ack_queue, skb);
106 } while (lapb->vs != end && (skb = skb_dequeue(&lapb->write_queue)) != NULL);
108 lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
110 if (!lapb_t1timer_running(lapb))
111 lapb_start_t1timer(lapb);
115 void lapb_transmit_buffer(struct lapb_cb *lapb, struct sk_buff *skb, int type)
119 ptr = skb_push(skb, 1);
121 if (lapb->mode & LAPB_MLP) {
122 if (lapb->mode & LAPB_DCE) {
123 if (type == LAPB_COMMAND)
125 if (type == LAPB_RESPONSE)
128 if (type == LAPB_COMMAND)
130 if (type == LAPB_RESPONSE)
134 if (lapb->mode & LAPB_DCE) {
135 if (type == LAPB_COMMAND)
137 if (type == LAPB_RESPONSE)
140 if (type == LAPB_COMMAND)
142 if (type == LAPB_RESPONSE)
147 lapb_dbg(2, "(%p) S%d TX %3ph\n", lapb->dev, lapb->state, skb->data);
149 if (!lapb_data_transmit(lapb, skb))
153 void lapb_establish_data_link(struct lapb_cb *lapb)
155 lapb->condition = 0x00;
158 if (lapb->mode & LAPB_EXTENDED) {
159 lapb_dbg(1, "(%p) S%d TX SABME(1)\n", lapb->dev, lapb->state);
160 lapb_send_control(lapb, LAPB_SABME, LAPB_POLLON, LAPB_COMMAND);
162 lapb_dbg(1, "(%p) S%d TX SABM(1)\n", lapb->dev, lapb->state);
163 lapb_send_control(lapb, LAPB_SABM, LAPB_POLLON, LAPB_COMMAND);
166 lapb_start_t1timer(lapb);
167 lapb_stop_t2timer(lapb);
170 void lapb_enquiry_response(struct lapb_cb *lapb)
172 lapb_dbg(1, "(%p) S%d TX RR(1) R%d\n",
173 lapb->dev, lapb->state, lapb->vr);
175 lapb_send_control(lapb, LAPB_RR, LAPB_POLLON, LAPB_RESPONSE);
177 lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
180 void lapb_timeout_response(struct lapb_cb *lapb)
182 lapb_dbg(1, "(%p) S%d TX RR(0) R%d\n",
183 lapb->dev, lapb->state, lapb->vr);
184 lapb_send_control(lapb, LAPB_RR, LAPB_POLLOFF, LAPB_RESPONSE);
186 lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
189 void lapb_check_iframes_acked(struct lapb_cb *lapb, unsigned short nr)
191 if (lapb->vs == nr) {
192 lapb_frames_acked(lapb, nr);
193 lapb_stop_t1timer(lapb);
195 } else if (lapb->va != nr) {
196 lapb_frames_acked(lapb, nr);
197 lapb_start_t1timer(lapb);
201 void lapb_check_need_response(struct lapb_cb *lapb, int type, int pf)
203 if (type == LAPB_COMMAND && pf)
204 lapb_enquiry_response(lapb);