Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * minimal standalone network-side lap-d implementation |
| 4 | * oystein@homelien.no, 2009 |
| 5 | */ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | #include <assert.h> |
| 10 | |
| 11 | #include "lapd.h" |
| 12 | #include "openbsc/debug.h" |
| 13 | |
| 14 | #define DEBUG_LAPD(f, args...) { printf("lapd "); printf(f, ## args); }; |
| 15 | |
| 16 | typedef enum { |
| 17 | LAPD_TEI_NONE = 0, |
| 18 | |
| 19 | LAPD_TEI_ASSIGNED, |
| 20 | |
| 21 | LAPD_TEI_ACTIVE, |
| 22 | } lapd_tei_state; |
| 23 | |
| 24 | const char *lapd_tei_states[] = { |
| 25 | "NONE", |
| 26 | "ASSIGNED", |
| 27 | "ACTIVE", |
| 28 | }; |
| 29 | |
| 30 | typedef enum { |
| 31 | LAPD_TYPE_NONE = 0, |
| 32 | |
| 33 | LAPD_TYPE_I, |
| 34 | LAPD_TYPE_S, |
| 35 | LAPD_TYPE_U, |
| 36 | } lapd_msg_type; |
| 37 | |
| 38 | typedef enum { |
| 39 | // commands/responses |
| 40 | LAPD_CMD_NONE = 0, |
| 41 | |
| 42 | LAPD_CMD_I, |
| 43 | LAPD_CMD_RR, |
| 44 | LAPD_CMD_RNR, |
| 45 | LAPD_CMD_REJ, |
| 46 | |
| 47 | LAPD_CMD_SABME, |
| 48 | LAPD_CMD_DM, |
| 49 | LAPD_CMD_UI, |
| 50 | LAPD_CMD_DISC, |
| 51 | LAPD_CMD_UA, |
| 52 | LAPD_CMD_FRMR, |
| 53 | LAPD_CMD_XID, |
| 54 | } lapd_cmd_type; |
| 55 | |
| 56 | const char *lapd_cmd_types[] = { |
| 57 | "NONE", |
| 58 | |
| 59 | "I", |
| 60 | "RR", |
| 61 | "RNR", |
| 62 | "REJ", |
| 63 | |
| 64 | "SABME", |
| 65 | "DM", |
| 66 | "UI", |
| 67 | "DISC", |
| 68 | "UA", |
| 69 | "FRMR", |
| 70 | "XID", |
| 71 | |
| 72 | |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | |
| 77 | const char *lapd_msg_types = "?ISU"; |
| 78 | const int network_side = 1; // 0 for user side |
| 79 | |
| 80 | typedef struct { |
| 81 | int tei; |
| 82 | int sapi; |
| 83 | //A valid N(R) value is one that is in the range V(A) ≤ N(R) ≤ V(S). |
| 84 | int vs; // next to be transmitted |
| 85 | int va; // last acked by peer |
| 86 | int vr; // next expected to be received |
| 87 | lapd_tei_state state; |
| 88 | } lapd_tei_t; |
| 89 | |
| 90 | // 3.5.2.2 Send state variable V(S) |
| 91 | // Each point-to-point data link connection endpoint shall have an associated V(S) when using I frame |
| 92 | // commands. V(S) denotes the sequence number of the next I frame to be transmitted. The V(S) can |
| 93 | // take on the value 0 through n minus 1. The value of V(S) shall be incremented by 1 with each |
| 94 | // successive I frame transmission, and shall not exceed V(A) by more than the maximum number of |
| 95 | // outstanding I frames k. The value of k may be in the range of 1 ≤ k ≤ 127. |
| 96 | // |
| 97 | // 3.5.2.3 Acknowledge state variable V(A) |
| 98 | // Each point-to-point data link connection endpoint shall have an associated V(A) when using I frame |
| 99 | // commands and supervisory frame commands/responses. V(A) identifies the last I frame that has been |
| 100 | // acknowledged by its peer [V(A) − 1 equals the N(S) of the last acknowledged I frame]. V(A) can |
| 101 | // take on the value 0 through n minus 1. The value of V(A) shall be updated by the valid N(R) values |
| 102 | // received from its peer (see 3.5.2.6). A valid N(R) value is one that is in the range V(A) ≤ N(R) ≤ |
| 103 | // V(S). |
| 104 | // |
| 105 | // 3.5.2.5 Receive state variable V(R) |
| 106 | // Each point-to-point data link connection endpoint shall have an associated V(R) when using I frame |
| 107 | // commands and supervisory frame commands/responses. V(R) denotes the sequence number of the |
| 108 | // next in-sequence I frame expected to be received. V(R) can take on the value 0 through n minus 1. |
| 109 | // The value of V(R) shall be incremented by one with the receipt of an error-free, in-sequence I frame |
| 110 | // whose N(S) equals V(R). |
| 111 | // |
| 112 | #define LAPD_NS(teip) (teip->vs) |
| 113 | #define LAPD_NR(teip) (teip->vr) |
| 114 | |
| 115 | // 3.5.2.4 Send sequence number N(S) |
| 116 | // Only I frames contain N(S), the send sequence number of transmitted I frames. At the time that an in- |
| 117 | // sequence I frame is designated for transmission, the value of N(S) is set equal to V(S). |
| 118 | // |
| 119 | // 3.5.2.6 Receive sequence number N(R) |
| 120 | // All I frames and supervisory frames contain N(R), the expected send sequence number of the next |
| 121 | // received I frame. At the time that a frame of the above types is designated for transmission, the value |
| 122 | // of N(R) is set equal to V(R). N(R) indicates that the data link layer entity transmitting the N(R) has |
| 123 | // correctly received all I frames numbered up to and including N(R) − 1. |
| 124 | |
| 125 | void (*lapd_transmit_cb)(uint8_t *data, int len, void *cbdata); |
| 126 | |
| 127 | static lapd_tei_t tei_list[] = { |
| 128 | { 25, 62, }, |
| 129 | { 1, 0, }, |
| 130 | { -1 }, |
| 131 | }; |
| 132 | |
| 133 | lapd_tei_t *teip_from_tei(int tei) { |
| 134 | lapd_tei_t *p; |
| 135 | for (p = tei_list; p->tei != -1; p++) { |
| 136 | if (p->tei == tei) return p; |
| 137 | }; |
| 138 | return NULL; |
| 139 | }; |
| 140 | |
| 141 | void lapd_tei_set_state(lapd_tei_t *teip, int newstate) { |
| 142 | DEBUG_LAPD("state change on tei %d: %s -> %s\n", teip->tei, lapd_tei_states[teip->state], lapd_tei_states[newstate]); |
| 143 | teip->state = newstate; |
| 144 | }; |
| 145 | |
| 146 | void lapd_tei_receive(uint8_t *data, int len, void *cbdata) { |
| 147 | //DEBUG_LAPD("tei receive %p, %d\n", data, len); |
| 148 | int entity = data[0]; |
| 149 | int ref = data[1]; |
| 150 | int mt = data[3]; |
| 151 | int action = data[4] >> 1; |
| 152 | int e = data[4] & 1; |
| 153 | //DEBUG_LAPD("tei mgmt: entity %x, ref %x, mt %x, action %x, e %x\n", entity, ref, mt, action, e); |
| 154 | |
| 155 | switch (mt) { |
| 156 | case 0x01: {// identity request |
| 157 | int tei = action; |
| 158 | DEBUG_LAPD("tei mgmt: identity request, accepting tei %d\n", tei); |
| 159 | //printf("tei: %d\n", tei); |
| 160 | uint8_t resp[8]; |
| 161 | memmove(resp, "\xfe\xff\x03\x0f\x00\x00\x02\x00", 8); |
| 162 | resp[7] = (tei << 1) | 1; |
| 163 | lapd_transmit_cb(resp, 8, cbdata); |
| 164 | lapd_tei_t *teip = teip_from_tei (tei); |
| 165 | if (teip->state == LAPD_TEI_NONE) |
| 166 | lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED); |
| 167 | break; } |
| 168 | default: |
| 169 | DEBUG_LAPD("tei mgmt: unknown mt %x action %x\n", mt, action); |
| 170 | assert(0); |
| 171 | }; |
| 172 | }; |
| 173 | |
| 174 | uint8_t *lapd_receive(uint8_t *data, int len, int *ilen, lapd_mph_type *prim, void *cbdata) { |
| 175 | #if 0 |
| 176 | DEBUG_LAPD("receive %p, %d\n", data, len); |
| 177 | hexdump(data, len); |
| 178 | #endif |
| 179 | |
| 180 | *ilen = 0; |
| 181 | *prim = 0; |
| 182 | |
| 183 | if (len < 2) { |
| 184 | DEBUG_LAPD("len %d < 2\n", len); |
| 185 | return NULL; |
| 186 | }; |
| 187 | |
| 188 | if ((data[0] & 1) != 0 || (data[1] & 1) != 1) { |
| 189 | DEBUG_LAPD("address field %x/%x not well formed\n", data[0], data[1]); |
| 190 | return NULL; |
| 191 | }; |
| 192 | |
| 193 | int sapi = data[0] >> 2; |
| 194 | int cr = (data[0] >> 1) & 1; |
| 195 | int tei = data[1] >> 1; |
| 196 | int command = network_side ^ cr; |
| 197 | //DEBUG_LAPD(" address sapi %x tei %d cmd %d cr %d\n", sapi, tei, command, cr); |
| 198 | |
| 199 | if (len < 3) { |
| 200 | DEBUG_LAPD("len %d < 4\n", len); |
| 201 | return NULL; |
| 202 | }; |
| 203 | |
| 204 | lapd_msg_type typ = 0; |
| 205 | lapd_cmd_type cmd = 0; |
| 206 | int pf = -1; |
| 207 | int ns = -1; |
| 208 | int nr = -1; |
| 209 | if ((data[2] & 1) == 0) { |
| 210 | typ = LAPD_TYPE_I; |
| 211 | assert(len >= 4); |
| 212 | ns = data[2] >> 1; |
| 213 | nr = data[3] >> 1; |
| 214 | pf = data[3] & 1; |
| 215 | cmd = LAPD_CMD_I; |
| 216 | } else if ((data[2] & 3) == 1) { |
| 217 | typ = LAPD_TYPE_S; |
| 218 | assert(len >= 4); |
| 219 | nr = data[3] >> 1; |
| 220 | pf = data[3] & 1; |
| 221 | switch (data[2]) { |
| 222 | case 0x1: cmd = LAPD_CMD_RR; break; |
| 223 | case 0x5: cmd = LAPD_CMD_RNR; break; |
| 224 | case 0x9: cmd = LAPD_CMD_REJ; break; |
| 225 | default: |
| 226 | DEBUG_LAPD("unknown S cmd %x\n", data[2]); |
| 227 | assert(0); |
| 228 | }; |
| 229 | } else if ((data[2] & 3) == 3) { |
| 230 | typ = LAPD_TYPE_U; |
| 231 | pf = (data[2] >> 4) & 1; |
| 232 | int val = data[2] & ~(1<<4); |
| 233 | switch (val) { |
| 234 | case 0x6f: cmd = LAPD_CMD_SABME; break; |
| 235 | case 0x0f: cmd = LAPD_CMD_DM; break; |
| 236 | case 0x03: cmd = LAPD_CMD_UI; break; |
| 237 | case 0x43: cmd = LAPD_CMD_DISC; break; |
| 238 | case 0x63: cmd = LAPD_CMD_UA; break; |
| 239 | case 0x87: cmd = LAPD_CMD_FRMR; break; |
| 240 | case 0xaf: cmd = LAPD_CMD_XID; break; |
| 241 | |
| 242 | default: |
| 243 | DEBUG_LAPD("unknown U cmd %x (pf %x data %x)\n", val, pf, data[2]); |
| 244 | assert(0); |
| 245 | }; |
| 246 | }; |
| 247 | |
| 248 | uint8_t *contents = &data[4]; |
| 249 | if (typ == LAPD_TYPE_U) contents--; |
| 250 | *ilen = len - (contents - data); |
| 251 | |
| 252 | lapd_tei_t *teip = teip_from_tei(tei); |
| 253 | if (tei == 127) |
| 254 | lapd_tei_receive(contents, *ilen, cbdata); |
| 255 | |
| 256 | DEBUG_LAPD("<- %c %s sapi %x tei %3d cmd %x pf %x ns %3d nr %3d ilen %d teip %p vs %d va %d vr %d len %d\n", lapd_msg_types[typ], lapd_cmd_types[cmd], sapi, tei, command, pf, ns, nr, *ilen, teip, teip ? teip->vs : -1, teip ? teip->va : -1, teip ? teip->vr : -1, len); |
| 257 | |
| 258 | if (teip) { |
| 259 | switch (cmd) { |
| 260 | case LAPD_CMD_I: { |
| 261 | if (ns != teip->vr) { |
| 262 | DEBUG_LAPD("ns %d != vr %d\n", ns, teip->vr); |
| 263 | if (ns == teip->vr-1) { |
| 264 | DEBUG_LAPD("DOUBLE FRAME, ignoring\n"); |
| 265 | cmd = 0; // ignore |
| 266 | } else { |
| 267 | assert(0); |
| 268 | }; |
| 269 | } else { |
| 270 | //printf("IN SEQUENCE\n"); |
| 271 | }; |
| 272 | teip->vr = (ns + 1) & 0x7f; // FIXME: hack! |
| 273 | |
| 274 | |
| 275 | break; } |
| 276 | case LAPD_CMD_SABME: { |
| 277 | teip->vs = 0; |
| 278 | teip->vr = 0; |
| 279 | teip->va = 0; |
| 280 | |
| 281 | // ua |
| 282 | uint8_t resp[8]; |
| 283 | int l = 0; |
| 284 | resp[l++] = data[0]; |
| 285 | resp[l++] = (tei << 1) | 1; |
| 286 | resp[l++] = 0x73; |
| 287 | lapd_transmit_cb(resp, l, cbdata); |
| 288 | |
| 289 | break; } |
| 290 | case LAPD_CMD_RR: { |
| 291 | teip->va = (nr & 0x7f); |
| 292 | if (teip->state != LAPD_TEI_ACTIVE) { |
| 293 | if (teip->state == LAPD_TEI_ASSIGNED) { |
| 294 | lapd_tei_set_state(teip, LAPD_TEI_ACTIVE); |
| 295 | *prim = LAPD_MPH_ACTIVATE_IND; |
| 296 | //printf("ASSIGNED and ACTIVE\n"); |
| 297 | } else { |
| 298 | #if 0 |
| 299 | DEBUG_LAPD("rr in strange state, send rej\n"); |
| 300 | |
| 301 | // rej |
| 302 | uint8_t resp[8]; |
| 303 | int l = 0; |
| 304 | resp[l++] = (teip->sapi << 2) | (network_side ? 0 : 2); |
| 305 | resp[l++] = (tei << 1) | 1; |
| 306 | resp[l++] = 0x09; //rej |
| 307 | resp[l++] = ((teip->vr+1) << 1) | 0; |
| 308 | lapd_transmit_cb(resp, l, cbdata); |
| 309 | pf = 0; // dont reply |
| 310 | #endif |
| 311 | }; |
| 312 | }; |
| 313 | if (pf) { |
| 314 | // interrogating us, send rr |
| 315 | uint8_t resp[8]; |
| 316 | int l = 0; |
| 317 | resp[l++] = data[0]; |
| 318 | resp[l++] = (tei << 1) | 1; |
| 319 | resp[l++] = 0x01; // rr |
| 320 | resp[l++] = (LAPD_NR(teip) << 1) | (data[3] & 1); // pf bit from req |
| 321 | |
| 322 | lapd_transmit_cb(resp, l, cbdata); |
| 323 | |
| 324 | }; |
| 325 | |
| 326 | break; } |
| 327 | case LAPD_CMD_FRMR: { // frame reject |
| 328 | #if 0 |
| 329 | if (teip->state == LAPD_TEI_ACTIVE) |
| 330 | *prim = LAPD_MPH_DEACTIVATE_IND; |
| 331 | lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED); |
| 332 | #endif |
| 333 | DEBUG_LAPD("frame reject, ignoring\n"); |
| 334 | assert(0); |
| 335 | break; } |
| 336 | case LAPD_CMD_DISC: { // disconnect |
| 337 | uint8_t resp[8]; |
| 338 | int l = 0; |
| 339 | resp[l++] = data[0]; |
| 340 | resp[l++] = (tei << 1) | 1; |
| 341 | resp[l++] = 0x73; |
| 342 | lapd_transmit_cb(resp, l, cbdata); |
| 343 | lapd_tei_set_state(teip, LAPD_TEI_NONE); |
| 344 | break; } |
| 345 | default: |
| 346 | DEBUG_LAPD("unknown cmd for tei %d (cmd %x)\n", tei, cmd); |
| 347 | assert(0); |
| 348 | }; |
| 349 | }; |
| 350 | |
| 351 | //if ((*prim == 0) && (ilen > 0) && (typ != LAPD_TYPE_S)) { |
| 352 | //if (cmd == LAPD_CMD_I) { |
| 353 | if (typ == LAPD_TYPE_I) { |
| 354 | // send rr |
| 355 | // Thu Jan 22 19:17:13 2009 <4000> sangoma.c:340 read (62/25) 4: fa 33 01 0a |
| 356 | // lapd <- S RR sapi 3e tei 25 cmd 0 pf 0 ns -1 nr 5 ilen 0 teip 0x613800 vs 7 va 5 vr 2 len 4 |
| 357 | |
| 358 | // interrogating us, send rr |
| 359 | uint8_t resp[8]; |
| 360 | int l = 0; |
| 361 | resp[l++] = data[0]; |
| 362 | resp[l++] = (tei << 1) | 1; |
| 363 | resp[l++] = 0x01; // rr |
| 364 | resp[l++] = (LAPD_NR(teip) << 1) | (data[3] & 1); // pf bit from req |
| 365 | |
| 366 | lapd_transmit_cb(resp, l, cbdata); |
| 367 | |
| 368 | *prim = LAPD_DL_DATA_IND; |
| 369 | return contents; |
| 370 | }; |
| 371 | |
| 372 | return NULL; |
| 373 | }; |
| 374 | |
| 375 | void lapd_transmit(int tei, uint8_t *data, int len, void *cbdata) { |
| 376 | //printf("lapd_transmit %d, %d\n", tei, len); |
| 377 | //hexdump(data, len); |
| 378 | lapd_tei_t *teip = teip_from_tei(tei); |
| 379 | //printf("teip %p\n", teip); |
| 380 | |
| 381 | // prepend stuff |
| 382 | uint8_t buf[10000]; |
| 383 | memset(buf, 0, sizeof(buf)); |
| 384 | memmove(buf+4, data, len); |
| 385 | len += 4; |
| 386 | |
| 387 | buf[0] = (teip->sapi << 2) | (network_side ? 2 : 0); |
| 388 | buf[1] = (teip->tei << 1) | 1; |
| 389 | buf[2] = (LAPD_NS(teip) << 1); |
| 390 | buf[3] = (LAPD_NR(teip) << 1) | 0; |
| 391 | |
| 392 | teip->vs = (teip->vs + 1) & 0x7f; |
| 393 | |
| 394 | lapd_transmit_cb(buf, len, cbdata); |
| 395 | }; |
| 396 | |