Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 1 | /* OpenBSC minimal LAPD implementation */ |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 2 | |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 3 | /* (C) 2009 by oystein@homelien.no |
| 4 | * (C) 2011 by Harald Welte <laforge@gnumonks.org> |
| 5 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2010 by Digium and Matthew Fredrickson <creslin@digium.com> |
| 7 | * |
| 8 | * All Rights Reserved |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | * |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <assert.h> |
| 29 | |
| 30 | #include "lapd.h" |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 31 | |
| 32 | #include <osmocore/linuxlist.h> |
| 33 | #include <osmocore/talloc.h> |
| 34 | #include <openbsc/debug.h> |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 35 | |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 36 | typedef enum { |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 37 | LAPD_TEI_NONE = 0, |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 38 | |
| 39 | LAPD_TEI_ASSIGNED, |
| 40 | |
| 41 | LAPD_TEI_ACTIVE, |
| 42 | } lapd_tei_state; |
| 43 | |
| 44 | const char *lapd_tei_states[] = { |
| 45 | "NONE", |
| 46 | "ASSIGNED", |
| 47 | "ACTIVE", |
| 48 | }; |
| 49 | |
| 50 | typedef enum { |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 51 | LAPD_TYPE_NONE = 0, |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 52 | |
| 53 | LAPD_TYPE_I, |
| 54 | LAPD_TYPE_S, |
| 55 | LAPD_TYPE_U, |
| 56 | } lapd_msg_type; |
| 57 | |
| 58 | typedef enum { |
Harald Welte | 98d0103 | 2011-02-04 20:36:50 +0100 | [diff] [blame] | 59 | /* commands/responses */ |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 60 | LAPD_CMD_NONE = 0, |
| 61 | |
| 62 | LAPD_CMD_I, |
| 63 | LAPD_CMD_RR, |
| 64 | LAPD_CMD_RNR, |
| 65 | LAPD_CMD_REJ, |
| 66 | |
| 67 | LAPD_CMD_SABME, |
| 68 | LAPD_CMD_DM, |
| 69 | LAPD_CMD_UI, |
| 70 | LAPD_CMD_DISC, |
| 71 | LAPD_CMD_UA, |
| 72 | LAPD_CMD_FRMR, |
| 73 | LAPD_CMD_XID, |
| 74 | } lapd_cmd_type; |
| 75 | |
| 76 | const char *lapd_cmd_types[] = { |
| 77 | "NONE", |
| 78 | |
| 79 | "I", |
| 80 | "RR", |
| 81 | "RNR", |
| 82 | "REJ", |
| 83 | |
| 84 | "SABME", |
| 85 | "DM", |
| 86 | "UI", |
| 87 | "DISC", |
| 88 | "UA", |
| 89 | "FRMR", |
| 90 | "XID", |
| 91 | |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 92 | }; |
| 93 | |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 94 | const char *lapd_msg_types = "?ISU"; |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 95 | |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 96 | struct lapd_tei { |
| 97 | struct llist_head list; |
| 98 | |
| 99 | uint8_t tei; |
| 100 | uint8_t sapi; |
Harald Welte | 98d0103 | 2011-02-04 20:36:50 +0100 | [diff] [blame] | 101 | /* A valid N(R) value is one that is in the range V(A) ≤ N(R) ≤ V(S). */ |
| 102 | int vs; /* next to be transmitted */ |
| 103 | int va; /* last acked by peer */ |
| 104 | int vr; /* next expected to be received */ |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 105 | lapd_tei_state state; |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 106 | }; |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 107 | |
Harald Welte | 98d0103 | 2011-02-04 20:36:50 +0100 | [diff] [blame] | 108 | /* 3.5.2.2 Send state variable V(S) |
| 109 | * Each point-to-point data link connection endpoint shall have an associated V(S) when using I frame |
| 110 | * commands. V(S) denotes the sequence number of the next I frame to be transmitted. The V(S) can |
| 111 | * take on the value 0 through n minus 1. The value of V(S) shall be incremented by 1 with each |
| 112 | * successive I frame transmission, and shall not exceed V(A) by more than the maximum number of |
| 113 | * outstanding I frames k. The value of k may be in the range of 1 ≤ k ≤ 127. |
| 114 | * |
| 115 | * 3.5.2.3 Acknowledge state variable V(A) |
| 116 | * Each point-to-point data link connection endpoint shall have an associated V(A) when using I frame |
| 117 | * commands and supervisory frame commands/responses. V(A) identifies the last I frame that has been |
| 118 | * acknowledged by its peer [V(A) − 1 equals the N(S) of the last acknowledged I frame]. V(A) can |
| 119 | * take on the value 0 through n minus 1. The value of V(A) shall be updated by the valid N(R) values |
| 120 | * 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) ≤ |
| 121 | * V(S). |
| 122 | * |
| 123 | * 3.5.2.5 Receive state variable V(R) |
| 124 | * Each point-to-point data link connection endpoint shall have an associated V(R) when using I frame |
| 125 | * commands and supervisory frame commands/responses. V(R) denotes the sequence number of the |
| 126 | * next in-sequence I frame expected to be received. V(R) can take on the value 0 through n minus 1. |
| 127 | * The value of V(R) shall be incremented by one with the receipt of an error-free, in-sequence I frame |
| 128 | * whose N(S) equals V(R). |
| 129 | */ |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 130 | #define LAPD_NS(teip) (teip->vs) |
| 131 | #define LAPD_NR(teip) (teip->vr) |
| 132 | |
Harald Welte | 98d0103 | 2011-02-04 20:36:50 +0100 | [diff] [blame] | 133 | /* 3.5.2.4 Send sequence number N(S) |
| 134 | * Only I frames contain N(S), the send sequence number of transmitted I frames. At the time that an in- |
| 135 | * sequence I frame is designated for transmission, the value of N(S) is set equal to V(S). |
| 136 | * |
| 137 | * 3.5.2.6 Receive sequence number N(R) |
| 138 | * All I frames and supervisory frames contain N(R), the expected send sequence number of the next |
| 139 | * received I frame. At the time that a frame of the above types is designated for transmission, the value |
| 140 | * of N(R) is set equal to V(R). N(R) indicates that the data link layer entity transmitting the N(R) has |
| 141 | * correctly received all I frames numbered up to and including N(R) − 1. |
| 142 | */ |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 143 | |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 144 | static struct lapd_tei *teip_from_tei(struct lapd_instance *li, uint8_t tei) |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 145 | { |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 146 | struct lapd_tei *lt; |
| 147 | |
| 148 | llist_for_each_entry(lt, &li->tei_list, list) { |
| 149 | if (lt->tei == tei) |
| 150 | return lt; |
| 151 | } |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 152 | return NULL; |
| 153 | }; |
| 154 | |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 155 | static void lapd_tei_set_state(struct lapd_tei *teip, int newstate) |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 156 | { |
Harald Welte | 1b2494d | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 157 | DEBUGP(DMI, "state change on tei %d: %s -> %s\n", teip->tei, |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 158 | lapd_tei_states[teip->state], lapd_tei_states[newstate]); |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 159 | teip->state = newstate; |
| 160 | }; |
| 161 | |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 162 | static void lapd_tei_receive(struct lapd_instance *li, uint8_t *data, int len) |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 163 | { |
Harald Welte | 32a7434 | 2011-02-05 19:51:05 +0100 | [diff] [blame^] | 164 | uint8_t entity = data[0]; |
| 165 | uint8_t ref = data[1]; |
| 166 | uint8_t mt = data[3]; |
| 167 | uint8_t action = data[4] >> 1; |
| 168 | uint8_t e = data[4] & 1; |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 169 | uint8_t resp[8]; |
| 170 | struct lapd_tei *teip; |
| 171 | |
Harald Welte | 32a7434 | 2011-02-05 19:51:05 +0100 | [diff] [blame^] | 172 | DEBUGP(DMI, "TEIMGR: entity %x, ref %x, mt %x, action %x, e %x\n", entity, ref, mt, action, e); |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 173 | |
| 174 | switch (mt) { |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 175 | case 0x01: /* IDENTITY REQUEST */ |
Harald Welte | 32a7434 | 2011-02-05 19:51:05 +0100 | [diff] [blame^] | 176 | DEBUGP(DMI, "TEIMGR: identity request for TEI %u\n", action); |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 177 | |
Harald Welte | 32a7434 | 2011-02-05 19:51:05 +0100 | [diff] [blame^] | 178 | teip = teip_from_tei(li, action); |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 179 | if (!teip) { |
Harald Welte | 32a7434 | 2011-02-05 19:51:05 +0100 | [diff] [blame^] | 180 | LOGP(DMI, LOGL_INFO, "TEI MGR: New TEI %u\n", action); |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 181 | teip = talloc_zero(li, struct lapd_tei); |
Harald Welte | 32a7434 | 2011-02-05 19:51:05 +0100 | [diff] [blame^] | 182 | teip->tei = action; |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 183 | llist_add(&teip->list, &li->tei_list); |
| 184 | lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED); |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 185 | } |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 186 | |
| 187 | /* Send ACCEPT */ |
| 188 | memmove(resp, "\xfe\xff\x03\x0f\x00\x00\x02\x00", 8); |
Harald Welte | 32a7434 | 2011-02-05 19:51:05 +0100 | [diff] [blame^] | 189 | resp[7] = (action << 1) | 1; |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 190 | li->transmit_cb(resp, 8, li->cbdata); |
| 191 | |
| 192 | if (teip->state == LAPD_TEI_NONE) |
| 193 | lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED); |
| 194 | break; |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 195 | default: |
Harald Welte | 32a7434 | 2011-02-05 19:51:05 +0100 | [diff] [blame^] | 196 | LOGP(DMI, LOGL_NOTICE, "TEIMGR: unknown mt %x action %x\n", |
Harald Welte | a91e1c8 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 197 | mt, action); |
| 198 | break; |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 199 | }; |
| 200 | }; |
| 201 | |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 202 | uint8_t *lapd_receive(struct lapd_instance *li, uint8_t * data, unsigned int len, |
| 203 | int *ilen, lapd_mph_type *prim) |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 204 | { |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 205 | uint8_t sapi, cr, tei, command; |
| 206 | int pf, ns, nr; |
| 207 | uint8_t *contents; |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 208 | struct lapd_tei *teip; |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 209 | |
| 210 | uint8_t resp[8]; |
| 211 | int l = 0; |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 212 | |
| 213 | *ilen = 0; |
| 214 | *prim = 0; |
| 215 | |
| 216 | if (len < 2) { |
Harald Welte | 1b2494d | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 217 | DEBUGP(DMI, "len %d < 2\n", len); |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 218 | return NULL; |
| 219 | }; |
| 220 | |
| 221 | if ((data[0] & 1) != 0 || (data[1] & 1) != 1) { |
Harald Welte | 1b2494d | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 222 | DEBUGP(DMI, "address field %x/%x not well formed\n", data[0], |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 223 | data[1]); |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 224 | return NULL; |
| 225 | }; |
| 226 | |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 227 | sapi = data[0] >> 2; |
| 228 | cr = (data[0] >> 1) & 1; |
| 229 | tei = data[1] >> 1; |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 230 | command = li->network_side ^ cr; |
Harald Welte | 1b2494d | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 231 | //DEBUGP(DMI, " address sapi %x tei %d cmd %d cr %d\n", sapi, tei, command, cr); |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 232 | |
| 233 | if (len < 3) { |
Harald Welte | 1b2494d | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 234 | DEBUGP(DMI, "len %d < 3\n", len); |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 235 | return NULL; |
| 236 | }; |
| 237 | |
| 238 | lapd_msg_type typ = 0; |
| 239 | lapd_cmd_type cmd = 0; |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 240 | pf = -1; |
| 241 | ns = -1; |
| 242 | nr = -1; |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 243 | if ((data[2] & 1) == 0) { |
| 244 | typ = LAPD_TYPE_I; |
| 245 | assert(len >= 4); |
| 246 | ns = data[2] >> 1; |
| 247 | nr = data[3] >> 1; |
| 248 | pf = data[3] & 1; |
| 249 | cmd = LAPD_CMD_I; |
| 250 | } else if ((data[2] & 3) == 1) { |
| 251 | typ = LAPD_TYPE_S; |
| 252 | assert(len >= 4); |
| 253 | nr = data[3] >> 1; |
| 254 | pf = data[3] & 1; |
| 255 | switch (data[2]) { |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 256 | case 0x1: |
| 257 | cmd = LAPD_CMD_RR; |
| 258 | break; |
| 259 | case 0x5: |
| 260 | cmd = LAPD_CMD_RNR; |
| 261 | break; |
| 262 | case 0x9: |
| 263 | cmd = LAPD_CMD_REJ; |
| 264 | break; |
| 265 | default: |
Harald Welte | a91e1c8 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 266 | LOGP(DMI, LOGL_ERROR, "unknown LAPD S cmd %x\n", data[2]); |
| 267 | return NULL; |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 268 | }; |
| 269 | } else if ((data[2] & 3) == 3) { |
| 270 | typ = LAPD_TYPE_U; |
| 271 | pf = (data[2] >> 4) & 1; |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 272 | int val = data[2] & ~(1 << 4); |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 273 | switch (val) { |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 274 | case 0x6f: |
| 275 | cmd = LAPD_CMD_SABME; |
| 276 | break; |
| 277 | case 0x0f: |
| 278 | cmd = LAPD_CMD_DM; |
| 279 | break; |
| 280 | case 0x03: |
| 281 | cmd = LAPD_CMD_UI; |
| 282 | break; |
| 283 | case 0x43: |
| 284 | cmd = LAPD_CMD_DISC; |
| 285 | break; |
| 286 | case 0x63: |
| 287 | cmd = LAPD_CMD_UA; |
| 288 | break; |
| 289 | case 0x87: |
| 290 | cmd = LAPD_CMD_FRMR; |
| 291 | break; |
| 292 | case 0xaf: |
| 293 | cmd = LAPD_CMD_XID; |
| 294 | break; |
| 295 | |
| 296 | default: |
Harald Welte | a91e1c8 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 297 | LOGP(DMI, LOGL_ERROR, "unknown U cmd %x " |
| 298 | "(pf %x data %x)\n", val, pf, data[2]); |
| 299 | return NULL; |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 300 | }; |
| 301 | }; |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 302 | |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 303 | contents = &data[4]; |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 304 | if (typ == LAPD_TYPE_U) |
| 305 | contents--; |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 306 | *ilen = len - (contents - data); |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 307 | |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 308 | if (tei == 127) |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 309 | lapd_tei_receive(li, contents, *ilen); |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 310 | |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 311 | teip = teip_from_tei(li, tei); |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 312 | |
Harald Welte | 1b2494d | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 313 | DEBUGP(DMI, "<- %c %s sapi %x tei %3d cmd %x pf %x ns %3d nr %3d " |
| 314 | "ilen %d teip %p vs %d va %d vr %d len %d\n", |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 315 | lapd_msg_types[typ], lapd_cmd_types[cmd], sapi, tei, command, pf, |
| 316 | ns, nr, *ilen, teip, teip ? teip->vs : -1, teip ? teip->va : -1, |
| 317 | teip ? teip->vr : -1, len); |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 318 | |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 319 | if (!teip) { |
| 320 | LOGP(DMI, LOGL_NOTICE, "Unknown TEI %u\n", tei); |
| 321 | return NULL; |
| 322 | } |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 323 | |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 324 | switch (cmd) { |
| 325 | case LAPD_CMD_I: |
| 326 | if (ns != teip->vr) { |
| 327 | DEBUGP(DMI, "ns %d != vr %d\n", ns, teip->vr); |
| 328 | if (ns == ((teip->vr - 1) & 0x7f)) { |
| 329 | DEBUGP(DMI, "DOUBLE FRAME, ignoring\n"); |
| 330 | cmd = 0; // ignore |
| 331 | } else { |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 332 | assert(0); |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 333 | }; |
| 334 | } else { |
| 335 | //printf("IN SEQUENCE\n"); |
| 336 | teip->vr = (ns + 1) & 0x7f; // FIXME: hack! |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 337 | }; |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 338 | |
| 339 | break; |
| 340 | case LAPD_CMD_UI: |
| 341 | break; |
| 342 | case LAPD_CMD_SABME: |
| 343 | teip->vs = 0; |
| 344 | teip->vr = 0; |
| 345 | teip->va = 0; |
| 346 | |
| 347 | // ua |
| 348 | resp[l++] = data[0]; |
| 349 | resp[l++] = (tei << 1) | 1; |
| 350 | resp[l++] = 0x73; |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 351 | li->transmit_cb(resp, l, li->cbdata); |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 352 | if (teip->state != LAPD_TEI_ACTIVE) { |
| 353 | if (teip->state == LAPD_TEI_ASSIGNED) { |
| 354 | lapd_tei_set_state(teip, |
| 355 | LAPD_TEI_ACTIVE); |
| 356 | //printf("ASSIGNED and ACTIVE\n"); |
| 357 | } else { |
| 358 | #if 0 |
| 359 | DEBUGP(DMI, "rr in strange state, send rej\n"); |
| 360 | |
| 361 | // rej |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 362 | resp[l++] = (teip-> sapi << 2) | (li->network_side ? 0 : 2); |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 363 | resp[l++] = (tei << 1) | 1; |
| 364 | resp[l++] = 0x09; //rej |
| 365 | resp[l++] = ((teip->vr + 1) << 1) | 0; |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 366 | li->transmit_cb(resp, l, li->cbdata); |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 367 | pf = 0; // dont reply |
| 368 | #endif |
| 369 | }; |
| 370 | }; |
| 371 | |
| 372 | *prim = LAPD_MPH_ACTIVATE_IND; |
| 373 | break; |
| 374 | case LAPD_CMD_RR: |
| 375 | teip->va = (nr & 0x7f); |
| 376 | #if 0 |
| 377 | if (teip->state != LAPD_TEI_ACTIVE) { |
| 378 | if (teip->state == LAPD_TEI_ASSIGNED) { |
| 379 | lapd_tei_set_state(teip, LAPD_TEI_ACTIVE); |
| 380 | *prim = LAPD_MPH_ACTIVATE_IND; |
| 381 | //printf("ASSIGNED and ACTIVE\n"); |
| 382 | } else { |
| 383 | #if 0 |
| 384 | DEBUGP(DMI, "rr in strange " "state, send rej\n"); |
| 385 | |
| 386 | // rej |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 387 | resp[l++] = (teip-> sapi << 2) | (li->network_side ? 0 : 2); |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 388 | resp[l++] = (tei << 1) | 1; |
| 389 | resp[l++] = 0x09; //rej |
| 390 | resp[l++] = |
| 391 | ((teip->vr + 1) << 1) | 0; |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 392 | li->transmit_cb(resp, l, li->cbdata); |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 393 | pf = 0; // dont reply |
| 394 | #endif |
| 395 | }; |
| 396 | }; |
| 397 | #endif |
| 398 | if (pf) { |
| 399 | // interrogating us, send rr |
| 400 | resp[l++] = data[0]; |
| 401 | resp[l++] = (tei << 1) | 1; |
| 402 | resp[l++] = 0x01; // rr |
| 403 | resp[l++] = (LAPD_NR(teip) << 1) | (data[3] & 1); // pf bit from req |
| 404 | |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 405 | li->transmit_cb(resp, l, li->cbdata); |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 406 | |
| 407 | }; |
| 408 | break; |
| 409 | case LAPD_CMD_FRMR: |
| 410 | // frame reject |
| 411 | #if 0 |
| 412 | if (teip->state == LAPD_TEI_ACTIVE) |
| 413 | *prim = LAPD_MPH_DEACTIVATE_IND; |
| 414 | lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED); |
| 415 | #endif |
Harald Welte | a91e1c8 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 416 | LOGP(DMI, LOGL_NOTICE, "frame reject, ignoring\n"); |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 417 | break; |
| 418 | case LAPD_CMD_DISC: |
| 419 | // disconnect |
| 420 | resp[l++] = data[0]; |
| 421 | resp[l++] = (tei << 1) | 1; |
| 422 | resp[l++] = 0x73; |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 423 | li->transmit_cb(resp, l, li->cbdata); |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 424 | lapd_tei_set_state(teip, LAPD_TEI_NONE); |
| 425 | break; |
| 426 | default: |
Harald Welte | a91e1c8 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 427 | LOGP(DMI, LOGL_NOTICE, "unknown cmd for tei %d (cmd %x)\n", |
| 428 | tei, cmd); |
| 429 | break; |
Harald Welte | 9be5403 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 430 | } |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 431 | |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 432 | if (typ == LAPD_TYPE_I) { |
Harald Welte | a91e1c8 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 433 | /* send rr |
| 434 | * Thu Jan 22 19:17:13 2009 <4000> sangoma.c:340 read (62/25) 4: fa 33 01 0a |
| 435 | * 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 |
| 436 | */ |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 437 | |
Harald Welte | a91e1c8 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 438 | /* interrogating us, send rr */ |
Harald Welte | 1b2494d | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 439 | DEBUGP(DMI, "Sending RR response\n"); |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 440 | resp[l++] = data[0]; |
| 441 | resp[l++] = (tei << 1) | 1; |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 442 | resp[l++] = 0x01; // rr |
| 443 | resp[l++] = (LAPD_NR(teip) << 1) | (data[3] & 1); // pf bit from req |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 444 | |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 445 | li->transmit_cb(resp, l, li->cbdata); |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 446 | |
Matthew Fredrickson | eb3459e | 2010-03-15 12:24:39 -0500 | [diff] [blame] | 447 | if (cmd != 0) { |
| 448 | *prim = LAPD_DL_DATA_IND; |
| 449 | return contents; |
| 450 | } |
| 451 | } else if (tei != 127 && typ == LAPD_TYPE_U && cmd == LAPD_CMD_UI) { |
| 452 | *prim = LAPD_DL_UNITDATA_IND; |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 453 | return contents; |
Matthew Fredrickson | eb3459e | 2010-03-15 12:24:39 -0500 | [diff] [blame] | 454 | } |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 455 | |
| 456 | return NULL; |
| 457 | }; |
| 458 | |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 459 | void lapd_transmit(struct lapd_instance *li, uint8_t tei, |
| 460 | uint8_t *data, unsigned int len) |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 461 | { |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 462 | //printf("lapd_transmit %d, %d\n", tei, len); |
| 463 | //hexdump(data, len); |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 464 | struct lapd_tei *teip = teip_from_tei(li, tei); |
| 465 | |
| 466 | if (!teip) { |
| 467 | LOGP(DMI, LOGL_ERROR, "Cannot transmit on non-existing " |
| 468 | "TEI %u\n", tei); |
| 469 | return; |
| 470 | } |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 471 | |
Harald Welte | a91e1c8 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 472 | /* prepend stuff */ |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 473 | uint8_t buf[10000]; |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 474 | memset(buf, 0, sizeof(buf)); |
Harald Welte | 8c911f8 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 475 | memmove(buf + 4, data, len); |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 476 | len += 4; |
| 477 | |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 478 | buf[0] = (teip->sapi << 2) | (li->network_side ? 2 : 0); |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 479 | buf[1] = (teip->tei << 1) | 1; |
| 480 | buf[2] = (LAPD_NS(teip) << 1); |
| 481 | buf[3] = (LAPD_NR(teip) << 1) | 0; |
| 482 | |
| 483 | teip->vs = (teip->vs + 1) & 0x7f; |
| 484 | |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 485 | li->transmit_cb(buf, len, li->cbdata); |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 486 | }; |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 487 | |
| 488 | struct lapd_instance *lapd_instance_alloc(void (*tx_cb)(uint8_t *data, int len, |
| 489 | void *cbdata), void *cbdata) |
| 490 | { |
| 491 | struct lapd_instance *li; |
| 492 | |
| 493 | li = talloc_zero(NULL, struct lapd_instance); |
| 494 | if (!li) |
| 495 | return NULL; |
| 496 | |
| 497 | li->transmit_cb = tx_cb; |
| 498 | li->cbdata = cbdata; |
| 499 | li->network_side = 1; |
| 500 | INIT_LLIST_HEAD(&li->tei_list); |
| 501 | |
| 502 | return li; |
| 503 | } |