Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
| 15 | |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/socket.h> |
| 19 | #include <linux/in.h> |
| 20 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/timer.h> |
| 22 | #include <linux/string.h> |
| 23 | #include <linux/sockios.h> |
| 24 | #include <linux/net.h> |
| 25 | #include <linux/inet.h> |
| 26 | #include <linux/skbuff.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <net/sock.h> |
| 29 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/fcntl.h> |
| 31 | #include <linux/mm.h> |
| 32 | #include <linux/interrupt.h> |
| 33 | #include <net/lapb.h> |
| 34 | |
| 35 | /* |
| 36 | * This routine purges all the queues of frames. |
| 37 | */ |
| 38 | void lapb_clear_queues(struct lapb_cb *lapb) |
| 39 | { |
| 40 | skb_queue_purge(&lapb->write_queue); |
| 41 | skb_queue_purge(&lapb->ack_queue); |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * This routine purges the input queue of those frames that have been |
| 46 | * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the |
| 47 | * SDL diagram. |
| 48 | */ |
| 49 | void lapb_frames_acked(struct lapb_cb *lapb, unsigned short nr) |
| 50 | { |
| 51 | struct sk_buff *skb; |
| 52 | int modulus; |
| 53 | |
| 54 | modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS; |
| 55 | |
| 56 | /* |
| 57 | * Remove all the ack-ed frames from the ack queue. |
| 58 | */ |
| 59 | if (lapb->va != nr) |
| 60 | while (skb_peek(&lapb->ack_queue) && lapb->va != nr) { |
YOSHIFUJI Hideaki | 56d6c3d | 2007-02-09 23:24:59 +0900 | [diff] [blame] | 61 | skb = skb_dequeue(&lapb->ack_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | kfree_skb(skb); |
| 63 | lapb->va = (lapb->va + 1) % modulus; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void lapb_requeue_frames(struct lapb_cb *lapb) |
| 68 | { |
YOSHIFUJI Hideaki | 56d6c3d | 2007-02-09 23:24:59 +0900 | [diff] [blame] | 69 | struct sk_buff *skb, *skb_prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * Requeue all the un-ack-ed frames on the output queue to be picked |
| 73 | * up by lapb_kick called from the timer. This arrangement handles the |
| 74 | * possibility of an empty output queue. |
| 75 | */ |
| 76 | while ((skb = skb_dequeue(&lapb->ack_queue)) != NULL) { |
| 77 | if (!skb_prev) |
| 78 | skb_queue_head(&lapb->write_queue, skb); |
| 79 | else |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 80 | skb_append(skb_prev, skb, &lapb->write_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | skb_prev = skb; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Validate that the value of nr is between va and vs. Return true or |
| 87 | * false for testing. |
| 88 | */ |
| 89 | int lapb_validate_nr(struct lapb_cb *lapb, unsigned short nr) |
| 90 | { |
| 91 | unsigned short vc = lapb->va; |
| 92 | int modulus; |
YOSHIFUJI Hideaki | 56d6c3d | 2007-02-09 23:24:59 +0900 | [diff] [blame] | 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS; |
| 95 | |
| 96 | while (vc != lapb->vs) { |
| 97 | if (nr == vc) |
| 98 | return 1; |
| 99 | vc = (vc + 1) % modulus; |
| 100 | } |
YOSHIFUJI Hideaki | 56d6c3d | 2007-02-09 23:24:59 +0900 | [diff] [blame] | 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | return nr == lapb->vs; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * This routine is the centralised routine for parsing the control |
| 107 | * information for the different frame formats. |
| 108 | */ |
| 109 | int lapb_decode(struct lapb_cb *lapb, struct sk_buff *skb, |
| 110 | struct lapb_frame *frame) |
| 111 | { |
| 112 | frame->type = LAPB_ILLEGAL; |
| 113 | |
| 114 | #if LAPB_DEBUG > 2 |
| 115 | printk(KERN_DEBUG "lapb: (%p) S%d RX %02X %02X %02X\n", |
| 116 | lapb->dev, lapb->state, |
| 117 | skb->data[0], skb->data[1], skb->data[2]); |
| 118 | #endif |
| 119 | |
| 120 | /* We always need to look at 2 bytes, sometimes we need |
| 121 | * to look at 3 and those cases are handled below. |
| 122 | */ |
| 123 | if (!pskb_may_pull(skb, 2)) |
| 124 | return -1; |
| 125 | |
| 126 | if (lapb->mode & LAPB_MLP) { |
| 127 | if (lapb->mode & LAPB_DCE) { |
| 128 | if (skb->data[0] == LAPB_ADDR_D) |
| 129 | frame->cr = LAPB_COMMAND; |
| 130 | if (skb->data[0] == LAPB_ADDR_C) |
| 131 | frame->cr = LAPB_RESPONSE; |
| 132 | } else { |
| 133 | if (skb->data[0] == LAPB_ADDR_C) |
| 134 | frame->cr = LAPB_COMMAND; |
| 135 | if (skb->data[0] == LAPB_ADDR_D) |
| 136 | frame->cr = LAPB_RESPONSE; |
| 137 | } |
| 138 | } else { |
| 139 | if (lapb->mode & LAPB_DCE) { |
| 140 | if (skb->data[0] == LAPB_ADDR_B) |
| 141 | frame->cr = LAPB_COMMAND; |
| 142 | if (skb->data[0] == LAPB_ADDR_A) |
| 143 | frame->cr = LAPB_RESPONSE; |
| 144 | } else { |
| 145 | if (skb->data[0] == LAPB_ADDR_A) |
| 146 | frame->cr = LAPB_COMMAND; |
| 147 | if (skb->data[0] == LAPB_ADDR_B) |
| 148 | frame->cr = LAPB_RESPONSE; |
| 149 | } |
| 150 | } |
YOSHIFUJI Hideaki | 56d6c3d | 2007-02-09 23:24:59 +0900 | [diff] [blame] | 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | skb_pull(skb, 1); |
| 153 | |
| 154 | if (lapb->mode & LAPB_EXTENDED) { |
| 155 | if (!(skb->data[0] & LAPB_S)) { |
| 156 | if (!pskb_may_pull(skb, 2)) |
| 157 | return -1; |
| 158 | /* |
| 159 | * I frame - carries NR/NS/PF |
| 160 | */ |
| 161 | frame->type = LAPB_I; |
| 162 | frame->ns = (skb->data[0] >> 1) & 0x7F; |
| 163 | frame->nr = (skb->data[1] >> 1) & 0x7F; |
| 164 | frame->pf = skb->data[1] & LAPB_EPF; |
| 165 | frame->control[0] = skb->data[0]; |
| 166 | frame->control[1] = skb->data[1]; |
| 167 | skb_pull(skb, 2); |
| 168 | } else if ((skb->data[0] & LAPB_U) == 1) { |
| 169 | if (!pskb_may_pull(skb, 2)) |
| 170 | return -1; |
| 171 | /* |
| 172 | * S frame - take out PF/NR |
| 173 | */ |
| 174 | frame->type = skb->data[0] & 0x0F; |
| 175 | frame->nr = (skb->data[1] >> 1) & 0x7F; |
| 176 | frame->pf = skb->data[1] & LAPB_EPF; |
| 177 | frame->control[0] = skb->data[0]; |
| 178 | frame->control[1] = skb->data[1]; |
| 179 | skb_pull(skb, 2); |
| 180 | } else if ((skb->data[0] & LAPB_U) == 3) { |
| 181 | /* |
| 182 | * U frame - take out PF |
| 183 | */ |
| 184 | frame->type = skb->data[0] & ~LAPB_SPF; |
| 185 | frame->pf = skb->data[0] & LAPB_SPF; |
| 186 | frame->control[0] = skb->data[0]; |
| 187 | frame->control[1] = 0x00; |
| 188 | skb_pull(skb, 1); |
| 189 | } |
| 190 | } else { |
| 191 | if (!(skb->data[0] & LAPB_S)) { |
| 192 | /* |
| 193 | * I frame - carries NR/NS/PF |
| 194 | */ |
| 195 | frame->type = LAPB_I; |
| 196 | frame->ns = (skb->data[0] >> 1) & 0x07; |
| 197 | frame->nr = (skb->data[0] >> 5) & 0x07; |
| 198 | frame->pf = skb->data[0] & LAPB_SPF; |
| 199 | } else if ((skb->data[0] & LAPB_U) == 1) { |
| 200 | /* |
| 201 | * S frame - take out PF/NR |
| 202 | */ |
| 203 | frame->type = skb->data[0] & 0x0F; |
| 204 | frame->nr = (skb->data[0] >> 5) & 0x07; |
| 205 | frame->pf = skb->data[0] & LAPB_SPF; |
| 206 | } else if ((skb->data[0] & LAPB_U) == 3) { |
| 207 | /* |
| 208 | * U frame - take out PF |
| 209 | */ |
| 210 | frame->type = skb->data[0] & ~LAPB_SPF; |
| 211 | frame->pf = skb->data[0] & LAPB_SPF; |
| 212 | } |
| 213 | |
| 214 | frame->control[0] = skb->data[0]; |
| 215 | |
| 216 | skb_pull(skb, 1); |
| 217 | } |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
YOSHIFUJI Hideaki | 56d6c3d | 2007-02-09 23:24:59 +0900 | [diff] [blame] | 222 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | * This routine is called when the HDLC layer internally generates a |
YOSHIFUJI Hideaki | 56d6c3d | 2007-02-09 23:24:59 +0900 | [diff] [blame] | 224 | * command or response for the remote machine ( eg. RR, UA etc. ). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | * Only supervisory or unnumbered frames are processed, FRMRs are handled |
| 226 | * by lapb_transmit_frmr below. |
| 227 | */ |
| 228 | void lapb_send_control(struct lapb_cb *lapb, int frametype, |
| 229 | int poll_bit, int type) |
| 230 | { |
| 231 | struct sk_buff *skb; |
| 232 | unsigned char *dptr; |
| 233 | |
| 234 | if ((skb = alloc_skb(LAPB_HEADER_LEN + 3, GFP_ATOMIC)) == NULL) |
| 235 | return; |
| 236 | |
| 237 | skb_reserve(skb, LAPB_HEADER_LEN + 1); |
| 238 | |
| 239 | if (lapb->mode & LAPB_EXTENDED) { |
| 240 | if ((frametype & LAPB_U) == LAPB_U) { |
| 241 | dptr = skb_put(skb, 1); |
| 242 | *dptr = frametype; |
| 243 | *dptr |= poll_bit ? LAPB_SPF : 0; |
| 244 | } else { |
| 245 | dptr = skb_put(skb, 2); |
| 246 | dptr[0] = frametype; |
| 247 | dptr[1] = (lapb->vr << 1); |
| 248 | dptr[1] |= poll_bit ? LAPB_EPF : 0; |
| 249 | } |
| 250 | } else { |
| 251 | dptr = skb_put(skb, 1); |
| 252 | *dptr = frametype; |
| 253 | *dptr |= poll_bit ? LAPB_SPF : 0; |
| 254 | if ((frametype & LAPB_U) == LAPB_S) /* S frames carry NR */ |
| 255 | *dptr |= (lapb->vr << 5); |
| 256 | } |
| 257 | |
| 258 | lapb_transmit_buffer(lapb, skb, type); |
| 259 | } |
| 260 | |
YOSHIFUJI Hideaki | 56d6c3d | 2007-02-09 23:24:59 +0900 | [diff] [blame] | 261 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | * This routine generates FRMRs based on information previously stored in |
| 263 | * the LAPB control block. |
| 264 | */ |
| 265 | void lapb_transmit_frmr(struct lapb_cb *lapb) |
| 266 | { |
| 267 | struct sk_buff *skb; |
| 268 | unsigned char *dptr; |
| 269 | |
| 270 | if ((skb = alloc_skb(LAPB_HEADER_LEN + 7, GFP_ATOMIC)) == NULL) |
| 271 | return; |
| 272 | |
| 273 | skb_reserve(skb, LAPB_HEADER_LEN + 1); |
| 274 | |
| 275 | if (lapb->mode & LAPB_EXTENDED) { |
| 276 | dptr = skb_put(skb, 6); |
| 277 | *dptr++ = LAPB_FRMR; |
| 278 | *dptr++ = lapb->frmr_data.control[0]; |
| 279 | *dptr++ = lapb->frmr_data.control[1]; |
| 280 | *dptr++ = (lapb->vs << 1) & 0xFE; |
| 281 | *dptr = (lapb->vr << 1) & 0xFE; |
| 282 | if (lapb->frmr_data.cr == LAPB_RESPONSE) |
| 283 | *dptr |= 0x01; |
| 284 | dptr++; |
| 285 | *dptr++ = lapb->frmr_type; |
| 286 | |
| 287 | #if LAPB_DEBUG > 1 |
| 288 | printk(KERN_DEBUG "lapb: (%p) S%d TX FRMR %02X %02X %02X %02X %02X\n", |
| 289 | lapb->dev, lapb->state, |
| 290 | skb->data[1], skb->data[2], skb->data[3], |
| 291 | skb->data[4], skb->data[5]); |
| 292 | #endif |
| 293 | } else { |
| 294 | dptr = skb_put(skb, 4); |
| 295 | *dptr++ = LAPB_FRMR; |
| 296 | *dptr++ = lapb->frmr_data.control[0]; |
| 297 | *dptr = (lapb->vs << 1) & 0x0E; |
| 298 | *dptr |= (lapb->vr << 5) & 0xE0; |
| 299 | if (lapb->frmr_data.cr == LAPB_RESPONSE) |
| 300 | *dptr |= 0x10; |
| 301 | dptr++; |
| 302 | *dptr++ = lapb->frmr_type; |
| 303 | |
| 304 | #if LAPB_DEBUG > 1 |
| 305 | printk(KERN_DEBUG "lapb: (%p) S%d TX FRMR %02X %02X %02X\n", |
| 306 | lapb->dev, lapb->state, skb->data[1], |
| 307 | skb->data[2], skb->data[3]); |
| 308 | #endif |
| 309 | } |
| 310 | |
| 311 | lapb_transmit_buffer(lapb, skb, LAPB_RESPONSE); |
| 312 | } |