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