1 /*
2 * LAPB release 002
3 *
4 * This code REQUIRES 2.1.15 or higher/ NET3.038
5 *
6 * This module:
7 * This module is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * History
13 * LAPB 001 Jonathan Naylor Started Coding
14 * LAPB 002 Jonathan Naylor New timer architecture.
15 */
16
17 #include <linux/errno.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/in.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/timer.h>
24 #include <linux/string.h>
25 #include <linux/sockios.h>
26 #include <linux/net.h>
27 #include <linux/inet.h>
28 #include <linux/skbuff.h>
29 #include <net/sock.h>
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
32 #include <linux/fcntl.h>
33 #include <linux/mm.h>
34 #include <linux/interrupt.h>
35 #include <net/lapb.h>
36
37 /*
38 * This procedure is passed a buffer descriptor for an iframe. It builds
39 * the rest of the control part of the frame and then writes it out.
40 */
lapb_send_iframe(lapb_cb * lapb,struct sk_buff * skb,int poll_bit)41 static void lapb_send_iframe(lapb_cb *lapb, struct sk_buff *skb, int poll_bit)
42 {
43 unsigned char *frame;
44
45 if (skb == NULL)
46 return;
47
48 if (lapb->mode & LAPB_EXTENDED) {
49 frame = skb_push(skb, 2);
50
51 frame[0] = LAPB_I;
52 frame[0] |= (lapb->vs << 1);
53 frame[1] = (poll_bit) ? LAPB_EPF : 0;
54 frame[1] |= (lapb->vr << 1);
55 } else {
56 frame = skb_push(skb, 1);
57
58 *frame = LAPB_I;
59 *frame |= (poll_bit) ? LAPB_SPF : 0;
60 *frame |= (lapb->vr << 5);
61 *frame |= (lapb->vs << 1);
62 }
63
64 #if LAPB_DEBUG > 1
65 printk(KERN_DEBUG "lapb: (%p) S%d TX I(%d) S%d R%d\n", lapb->token, lapb->state, poll_bit, lapb->vs, lapb->vr);
66 #endif
67
68 lapb_transmit_buffer(lapb, skb, LAPB_COMMAND);
69 }
70
lapb_kick(lapb_cb * lapb)71 void lapb_kick(lapb_cb *lapb)
72 {
73 struct sk_buff *skb, *skbn;
74 unsigned short modulus, start, end;
75
76 modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS;
77
78 start = (skb_peek(&lapb->ack_queue) == NULL) ? lapb->va : lapb->vs;
79 end = (lapb->va + lapb->window) % modulus;
80
81 if (!(lapb->condition & LAPB_PEER_RX_BUSY_CONDITION) &&
82 start != end &&
83 skb_peek(&lapb->write_queue) != NULL) {
84
85 lapb->vs = start;
86
87 /*
88 * Dequeue the frame and copy it.
89 */
90 skb = skb_dequeue(&lapb->write_queue);
91
92 do {
93 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
94 skb_queue_head(&lapb->write_queue, skb);
95 break;
96 }
97
98 if (skb->sk != NULL)
99 skb_set_owner_w(skbn, skb->sk);
100
101 /*
102 * Transmit the frame copy.
103 */
104 lapb_send_iframe(lapb, skbn, LAPB_POLLOFF);
105
106 lapb->vs = (lapb->vs + 1) % modulus;
107
108 /*
109 * Requeue the original data frame.
110 */
111 skb_queue_tail(&lapb->ack_queue, skb);
112
113 } while (lapb->vs != end && (skb = skb_dequeue(&lapb->write_queue)) != NULL);
114
115 lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
116
117 if (!lapb_t1timer_running(lapb))
118 lapb_start_t1timer(lapb);
119 }
120 }
121
lapb_transmit_buffer(lapb_cb * lapb,struct sk_buff * skb,int type)122 void lapb_transmit_buffer(lapb_cb *lapb, struct sk_buff *skb, int type)
123 {
124 unsigned char *ptr;
125
126 ptr = skb_push(skb, 1);
127
128 if (lapb->mode & LAPB_MLP) {
129 if (lapb->mode & LAPB_DCE) {
130 if (type == LAPB_COMMAND)
131 *ptr = LAPB_ADDR_C;
132 if (type == LAPB_RESPONSE)
133 *ptr = LAPB_ADDR_D;
134 } else {
135 if (type == LAPB_COMMAND)
136 *ptr = LAPB_ADDR_D;
137 if (type == LAPB_RESPONSE)
138 *ptr = LAPB_ADDR_C;
139 }
140 } else {
141 if (lapb->mode & LAPB_DCE) {
142 if (type == LAPB_COMMAND)
143 *ptr = LAPB_ADDR_A;
144 if (type == LAPB_RESPONSE)
145 *ptr = LAPB_ADDR_B;
146 } else {
147 if (type == LAPB_COMMAND)
148 *ptr = LAPB_ADDR_B;
149 if (type == LAPB_RESPONSE)
150 *ptr = LAPB_ADDR_A;
151 }
152 }
153
154 #if LAPB_DEBUG > 2
155 printk(KERN_DEBUG "lapb: (%p) S%d TX %02X %02X %02X\n", lapb->token, lapb->state, skb->data[0], skb->data[1], skb->data[2]);
156 #endif
157
158 if (!lapb_data_transmit(lapb, skb))
159 kfree_skb(skb);
160 }
161
lapb_establish_data_link(lapb_cb * lapb)162 void lapb_establish_data_link(lapb_cb *lapb)
163 {
164 lapb->condition = 0x00;
165 lapb->n2count = 0;
166
167 if (lapb->mode & LAPB_EXTENDED) {
168 #if LAPB_DEBUG > 1
169 printk(KERN_DEBUG "lapb: (%p) S%d TX SABME(1)\n", lapb->token, lapb->state);
170 #endif
171 lapb_send_control(lapb, LAPB_SABME, LAPB_POLLON, LAPB_COMMAND);
172 } else {
173 #if LAPB_DEBUG > 1
174 printk(KERN_DEBUG "lapb: (%p) S%d TX SABM(1)\n", lapb->token, lapb->state);
175 #endif
176 lapb_send_control(lapb, LAPB_SABM, LAPB_POLLON, LAPB_COMMAND);
177 }
178
179 lapb_start_t1timer(lapb);
180 lapb_stop_t2timer(lapb);
181 }
182
lapb_enquiry_response(lapb_cb * lapb)183 void lapb_enquiry_response(lapb_cb *lapb)
184 {
185 #if LAPB_DEBUG > 1
186 printk(KERN_DEBUG "lapb: (%p) S%d TX RR(1) R%d\n", lapb->token, lapb->state, lapb->vr);
187 #endif
188
189 lapb_send_control(lapb, LAPB_RR, LAPB_POLLON, LAPB_RESPONSE);
190
191 lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
192 }
193
lapb_timeout_response(lapb_cb * lapb)194 void lapb_timeout_response(lapb_cb *lapb)
195 {
196 #if LAPB_DEBUG > 1
197 printk(KERN_DEBUG "lapb: (%p) S%d TX RR(0) R%d\n", lapb->token, lapb->state, lapb->vr);
198 #endif
199
200 lapb_send_control(lapb, LAPB_RR, LAPB_POLLOFF, LAPB_RESPONSE);
201
202 lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
203 }
204
lapb_check_iframes_acked(lapb_cb * lapb,unsigned short nr)205 void lapb_check_iframes_acked(lapb_cb *lapb, unsigned short nr)
206 {
207 if (lapb->vs == nr) {
208 lapb_frames_acked(lapb, nr);
209 lapb_stop_t1timer(lapb);
210 lapb->n2count = 0;
211 } else {
212 if (lapb->va != nr) {
213 lapb_frames_acked(lapb, nr);
214 lapb_start_t1timer(lapb);
215 }
216 }
217 }
218
lapb_check_need_response(lapb_cb * lapb,int type,int pf)219 void lapb_check_need_response(lapb_cb *lapb, int type, int pf)
220 {
221 if (type == LAPB_COMMAND && pf)
222 lapb_enquiry_response(lapb);
223 }
224