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 |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 4 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2010 by Digium and Matthew Fredrickson <creslin@digium.com> |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 6 | * (C) 2011 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 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 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 26 | /* TODO: |
| 27 | * detect RR timeout and set SAP state back to SABM_RETRANSMIT |
| 28 | * use of value_string |
| 29 | * further code cleanup (spaghetti) |
| 30 | */ |
| 31 | |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <string.h> |
| 34 | #include <assert.h> |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 35 | #include <errno.h> |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 36 | |
| 37 | #include "lapd.h" |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 38 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 39 | #include <osmocom/core/linuxlist.h> |
| 40 | #include <osmocom/core/talloc.h> |
| 41 | #include <osmocom/core/msgb.h> |
| 42 | #include <osmocom/core/timer.h> |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 43 | #include <openbsc/debug.h> |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 44 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 45 | #define SABM_INTERVAL 0, 300000 |
| 46 | |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 47 | typedef enum { |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 48 | LAPD_TEI_NONE = 0, |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 49 | LAPD_TEI_ASSIGNED, |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 50 | LAPD_TEI_ACTIVE, |
| 51 | } lapd_tei_state; |
| 52 | |
| 53 | const char *lapd_tei_states[] = { |
| 54 | "NONE", |
| 55 | "ASSIGNED", |
| 56 | "ACTIVE", |
| 57 | }; |
| 58 | |
| 59 | typedef enum { |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 60 | LAPD_TYPE_NONE = 0, |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 61 | |
| 62 | LAPD_TYPE_I, |
| 63 | LAPD_TYPE_S, |
| 64 | LAPD_TYPE_U, |
| 65 | } lapd_msg_type; |
| 66 | |
| 67 | typedef enum { |
Harald Welte | 7e859bc | 2011-02-04 20:36:50 +0100 | [diff] [blame] | 68 | /* commands/responses */ |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 69 | LAPD_CMD_NONE = 0, |
| 70 | |
| 71 | LAPD_CMD_I, |
| 72 | LAPD_CMD_RR, |
| 73 | LAPD_CMD_RNR, |
| 74 | LAPD_CMD_REJ, |
| 75 | |
| 76 | LAPD_CMD_SABME, |
| 77 | LAPD_CMD_DM, |
| 78 | LAPD_CMD_UI, |
| 79 | LAPD_CMD_DISC, |
| 80 | LAPD_CMD_UA, |
| 81 | LAPD_CMD_FRMR, |
| 82 | LAPD_CMD_XID, |
| 83 | } lapd_cmd_type; |
| 84 | |
| 85 | const char *lapd_cmd_types[] = { |
| 86 | "NONE", |
| 87 | |
| 88 | "I", |
| 89 | "RR", |
| 90 | "RNR", |
| 91 | "REJ", |
| 92 | |
| 93 | "SABME", |
| 94 | "DM", |
| 95 | "UI", |
| 96 | "DISC", |
| 97 | "UA", |
| 98 | "FRMR", |
| 99 | "XID", |
| 100 | |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 101 | }; |
| 102 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 103 | enum lapd_sap_state { |
| 104 | SAP_STATE_INACTIVE, |
| 105 | SAP_STATE_SABM_RETRANS, |
| 106 | SAP_STATE_ACTIVE, |
| 107 | }; |
| 108 | |
| 109 | const char *lapd_sap_states[] = { |
| 110 | "INACTIVE", |
| 111 | "SABM_RETRANS", |
| 112 | "ACTIVE", |
| 113 | }; |
| 114 | |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 115 | const char *lapd_msg_types = "?ISU"; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 116 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 117 | /* structure representing an allocated TEI within a LAPD instance */ |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 118 | struct lapd_tei { |
| 119 | struct llist_head list; |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 120 | struct lapd_instance *li; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 121 | uint8_t tei; |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 122 | lapd_tei_state state; |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 123 | |
| 124 | struct llist_head sap_list; |
| 125 | }; |
| 126 | |
| 127 | /* Structure representing a SAP within a TEI. We use this for TE-mode to |
| 128 | * re-transmit SABM */ |
| 129 | struct lapd_sap { |
| 130 | struct llist_head list; |
| 131 | struct lapd_tei *tei; |
| 132 | uint8_t sapi; |
| 133 | enum lapd_sap_state state; |
| 134 | |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 135 | /* A valid N(R) value is one that is in the range V(A) ≤ N(R) ≤ V(S). */ |
| 136 | int vs; /* next to be transmitted */ |
| 137 | int va; /* last acked by peer */ |
| 138 | int vr; /* next expected to be received */ |
| 139 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 140 | struct timer_list sabme_timer; /* timer to re-transmit SABM message */ |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 141 | }; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 142 | |
Harald Welte | 7e859bc | 2011-02-04 20:36:50 +0100 | [diff] [blame] | 143 | /* 3.5.2.2 Send state variable V(S) |
| 144 | * Each point-to-point data link connection endpoint shall have an associated V(S) when using I frame |
| 145 | * commands. V(S) denotes the sequence number of the next I frame to be transmitted. The V(S) can |
| 146 | * take on the value 0 through n minus 1. The value of V(S) shall be incremented by 1 with each |
| 147 | * successive I frame transmission, and shall not exceed V(A) by more than the maximum number of |
| 148 | * outstanding I frames k. The value of k may be in the range of 1 ≤ k ≤ 127. |
| 149 | * |
| 150 | * 3.5.2.3 Acknowledge state variable V(A) |
| 151 | * Each point-to-point data link connection endpoint shall have an associated V(A) when using I frame |
| 152 | * commands and supervisory frame commands/responses. V(A) identifies the last I frame that has been |
| 153 | * acknowledged by its peer [V(A) − 1 equals the N(S) of the last acknowledged I frame]. V(A) can |
| 154 | * take on the value 0 through n minus 1. The value of V(A) shall be updated by the valid N(R) values |
| 155 | * 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) ≤ |
| 156 | * V(S). |
| 157 | * |
| 158 | * 3.5.2.5 Receive state variable V(R) |
| 159 | * Each point-to-point data link connection endpoint shall have an associated V(R) when using I frame |
| 160 | * commands and supervisory frame commands/responses. V(R) denotes the sequence number of the |
| 161 | * next in-sequence I frame expected to be received. V(R) can take on the value 0 through n minus 1. |
| 162 | * The value of V(R) shall be incremented by one with the receipt of an error-free, in-sequence I frame |
| 163 | * whose N(S) equals V(R). |
| 164 | */ |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 165 | #define LAPD_NS(sap) (sap->vs) |
| 166 | #define LAPD_NR(sap) (sap->vr) |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 167 | |
Harald Welte | 7e859bc | 2011-02-04 20:36:50 +0100 | [diff] [blame] | 168 | /* 3.5.2.4 Send sequence number N(S) |
| 169 | * Only I frames contain N(S), the send sequence number of transmitted I frames. At the time that an in- |
| 170 | * sequence I frame is designated for transmission, the value of N(S) is set equal to V(S). |
| 171 | * |
| 172 | * 3.5.2.6 Receive sequence number N(R) |
| 173 | * All I frames and supervisory frames contain N(R), the expected send sequence number of the next |
| 174 | * received I frame. At the time that a frame of the above types is designated for transmission, the value |
| 175 | * of N(R) is set equal to V(R). N(R) indicates that the data link layer entity transmitting the N(R) has |
| 176 | * correctly received all I frames numbered up to and including N(R) − 1. |
| 177 | */ |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 178 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 179 | /* Resolve TEI structure from given numeric TEI */ |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 180 | 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] | 181 | { |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 182 | struct lapd_tei *lt; |
| 183 | |
| 184 | llist_for_each_entry(lt, &li->tei_list, list) { |
| 185 | if (lt->tei == tei) |
| 186 | return lt; |
| 187 | } |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 188 | return NULL; |
| 189 | }; |
| 190 | |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 191 | static void lapd_tei_set_state(struct lapd_tei *teip, int newstate) |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 192 | { |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 193 | DEBUGP(DMI, "state change on TEI %d: %s -> %s\n", teip->tei, |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 194 | lapd_tei_states[teip->state], lapd_tei_states[newstate]); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 195 | teip->state = newstate; |
| 196 | }; |
| 197 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 198 | /* Allocate a new TEI */ |
| 199 | struct lapd_tei *lapd_tei_alloc(struct lapd_instance *li, uint8_t tei) |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 200 | { |
| 201 | struct lapd_tei *teip; |
| 202 | |
| 203 | teip = talloc_zero(li, struct lapd_tei); |
| 204 | if (!teip) |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 205 | return NULL; |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 206 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 207 | teip->li = li; |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 208 | teip->tei = tei; |
| 209 | llist_add(&teip->list, &li->tei_list); |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 210 | INIT_LLIST_HEAD(&teip->sap_list); |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 211 | |
| 212 | lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED); |
| 213 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 214 | return teip; |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 217 | /* Find a SAP within a given TEI */ |
| 218 | static struct lapd_sap *lapd_sap_find(struct lapd_tei *teip, uint8_t sapi) |
| 219 | { |
| 220 | struct lapd_sap *sap; |
| 221 | |
| 222 | llist_for_each_entry(sap, &teip->sap_list, list) { |
| 223 | if (sap->sapi == sapi) |
| 224 | return sap; |
| 225 | } |
| 226 | |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | static void sabme_timer_cb(void *_sap); |
| 231 | |
| 232 | /* Allocate a new SAP within a given TEI */ |
| 233 | static struct lapd_sap *lapd_sap_alloc(struct lapd_tei *teip, uint8_t sapi) |
| 234 | { |
| 235 | struct lapd_sap *sap = talloc_zero(teip, struct lapd_sap); |
| 236 | |
| 237 | LOGP(DMI, LOGL_INFO, "Allocating SAP for SAPI=%u / TEI=%u\n", |
| 238 | sapi, teip->tei); |
| 239 | |
| 240 | sap->sapi = sapi; |
| 241 | sap->tei = teip; |
| 242 | sap->sabme_timer.cb = &sabme_timer_cb; |
| 243 | sap->sabme_timer.data = sap; |
| 244 | |
| 245 | llist_add(&sap->list, &teip->sap_list); |
| 246 | |
| 247 | return sap; |
| 248 | } |
| 249 | |
| 250 | static void lapd_sap_set_state(struct lapd_tei *teip, uint8_t sapi, |
| 251 | enum lapd_sap_state newstate) |
| 252 | { |
| 253 | struct lapd_sap *sap = lapd_sap_find(teip, sapi); |
| 254 | if (!sap) |
| 255 | return; |
| 256 | |
| 257 | DEBUGP(DMI, "state change on TEI %u / SAPI %u: %s -> %s\n", teip->tei, |
| 258 | sapi, lapd_sap_states[sap->state], lapd_sap_states[newstate]); |
| 259 | switch (sap->state) { |
| 260 | case SAP_STATE_SABM_RETRANS: |
| 261 | if (newstate != SAP_STATE_SABM_RETRANS) |
| 262 | bsc_del_timer(&sap->sabme_timer); |
| 263 | break; |
| 264 | default: |
| 265 | if (newstate == SAP_STATE_SABM_RETRANS) |
| 266 | bsc_schedule_timer(&sap->sabme_timer, SABM_INTERVAL); |
| 267 | break; |
| 268 | } |
| 269 | |
| 270 | sap->state = newstate; |
| 271 | }; |
| 272 | |
| 273 | /* Input function into TEI manager */ |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 274 | 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] | 275 | { |
Harald Welte | 8fc66a0 | 2011-02-05 19:51:05 +0100 | [diff] [blame] | 276 | uint8_t entity = data[0]; |
| 277 | uint8_t ref = data[1]; |
| 278 | uint8_t mt = data[3]; |
| 279 | uint8_t action = data[4] >> 1; |
| 280 | uint8_t e = data[4] & 1; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 281 | uint8_t resp[8]; |
| 282 | struct lapd_tei *teip; |
| 283 | |
Harald Welte | 8fc66a0 | 2011-02-05 19:51:05 +0100 | [diff] [blame] | 284 | 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] | 285 | |
| 286 | switch (mt) { |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 287 | case 0x01: /* IDENTITY REQUEST */ |
Harald Welte | 8fc66a0 | 2011-02-05 19:51:05 +0100 | [diff] [blame] | 288 | DEBUGP(DMI, "TEIMGR: identity request for TEI %u\n", action); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 289 | |
Harald Welte | 8fc66a0 | 2011-02-05 19:51:05 +0100 | [diff] [blame] | 290 | teip = teip_from_tei(li, action); |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 291 | if (!teip) { |
Harald Welte | 8fc66a0 | 2011-02-05 19:51:05 +0100 | [diff] [blame] | 292 | LOGP(DMI, LOGL_INFO, "TEI MGR: New TEI %u\n", action); |
Holger Hans Peter Freyther | 1367374 | 2011-04-04 19:17:58 +0200 | [diff] [blame^] | 293 | teip = lapd_tei_alloc(li, action); |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 294 | } |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 295 | |
| 296 | /* Send ACCEPT */ |
| 297 | memmove(resp, "\xfe\xff\x03\x0f\x00\x00\x02\x00", 8); |
Harald Welte | 8fc66a0 | 2011-02-05 19:51:05 +0100 | [diff] [blame] | 298 | resp[7] = (action << 1) | 1; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 299 | li->transmit_cb(resp, 8, li->cbdata); |
| 300 | |
| 301 | if (teip->state == LAPD_TEI_NONE) |
| 302 | lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED); |
| 303 | break; |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 304 | default: |
Harald Welte | 8fc66a0 | 2011-02-05 19:51:05 +0100 | [diff] [blame] | 305 | LOGP(DMI, LOGL_NOTICE, "TEIMGR: unknown mt %x action %x\n", |
Harald Welte | 0ae5755 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 306 | mt, action); |
| 307 | break; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 308 | }; |
| 309 | }; |
| 310 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 311 | /* General input function for any data received for this LAPD instance */ |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 312 | uint8_t *lapd_receive(struct lapd_instance *li, uint8_t * data, unsigned int len, |
| 313 | int *ilen, lapd_mph_type *prim) |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 314 | { |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 315 | uint8_t sapi, cr, tei, command; |
| 316 | int pf, ns, nr; |
| 317 | uint8_t *contents; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 318 | struct lapd_tei *teip; |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 319 | struct lapd_sap *sap; |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 320 | |
| 321 | uint8_t resp[8]; |
| 322 | int l = 0; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 323 | |
| 324 | *ilen = 0; |
| 325 | *prim = 0; |
| 326 | |
| 327 | if (len < 2) { |
Harald Welte | 0abc11a | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 328 | DEBUGP(DMI, "len %d < 2\n", len); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 329 | return NULL; |
| 330 | }; |
| 331 | |
| 332 | if ((data[0] & 1) != 0 || (data[1] & 1) != 1) { |
Harald Welte | 0abc11a | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 333 | DEBUGP(DMI, "address field %x/%x not well formed\n", data[0], |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 334 | data[1]); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 335 | return NULL; |
| 336 | }; |
| 337 | |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 338 | sapi = data[0] >> 2; |
| 339 | cr = (data[0] >> 1) & 1; |
| 340 | tei = data[1] >> 1; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 341 | command = li->network_side ^ cr; |
Harald Welte | 0abc11a | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 342 | //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] | 343 | |
| 344 | if (len < 3) { |
Harald Welte | 0abc11a | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 345 | DEBUGP(DMI, "len %d < 3\n", len); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 346 | return NULL; |
| 347 | }; |
| 348 | |
| 349 | lapd_msg_type typ = 0; |
| 350 | lapd_cmd_type cmd = 0; |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 351 | pf = -1; |
| 352 | ns = -1; |
| 353 | nr = -1; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 354 | if ((data[2] & 1) == 0) { |
| 355 | typ = LAPD_TYPE_I; |
| 356 | assert(len >= 4); |
| 357 | ns = data[2] >> 1; |
| 358 | nr = data[3] >> 1; |
| 359 | pf = data[3] & 1; |
| 360 | cmd = LAPD_CMD_I; |
| 361 | } else if ((data[2] & 3) == 1) { |
| 362 | typ = LAPD_TYPE_S; |
| 363 | assert(len >= 4); |
| 364 | nr = data[3] >> 1; |
| 365 | pf = data[3] & 1; |
| 366 | switch (data[2]) { |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 367 | case 0x1: |
| 368 | cmd = LAPD_CMD_RR; |
| 369 | break; |
| 370 | case 0x5: |
| 371 | cmd = LAPD_CMD_RNR; |
| 372 | break; |
| 373 | case 0x9: |
| 374 | cmd = LAPD_CMD_REJ; |
| 375 | break; |
| 376 | default: |
Harald Welte | 0ae5755 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 377 | LOGP(DMI, LOGL_ERROR, "unknown LAPD S cmd %x\n", data[2]); |
| 378 | return NULL; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 379 | }; |
| 380 | } else if ((data[2] & 3) == 3) { |
| 381 | typ = LAPD_TYPE_U; |
| 382 | pf = (data[2] >> 4) & 1; |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 383 | int val = data[2] & ~(1 << 4); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 384 | switch (val) { |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 385 | case 0x6f: |
| 386 | cmd = LAPD_CMD_SABME; |
| 387 | break; |
| 388 | case 0x0f: |
| 389 | cmd = LAPD_CMD_DM; |
| 390 | break; |
| 391 | case 0x03: |
| 392 | cmd = LAPD_CMD_UI; |
| 393 | break; |
| 394 | case 0x43: |
| 395 | cmd = LAPD_CMD_DISC; |
| 396 | break; |
| 397 | case 0x63: |
| 398 | cmd = LAPD_CMD_UA; |
| 399 | break; |
| 400 | case 0x87: |
| 401 | cmd = LAPD_CMD_FRMR; |
| 402 | break; |
| 403 | case 0xaf: |
| 404 | cmd = LAPD_CMD_XID; |
| 405 | break; |
| 406 | |
| 407 | default: |
Harald Welte | 0ae5755 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 408 | LOGP(DMI, LOGL_ERROR, "unknown U cmd %x " |
| 409 | "(pf %x data %x)\n", val, pf, data[2]); |
| 410 | return NULL; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 411 | }; |
| 412 | }; |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 413 | |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 414 | contents = &data[4]; |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 415 | if (typ == LAPD_TYPE_U) |
| 416 | contents--; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 417 | *ilen = len - (contents - data); |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 418 | |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 419 | if (tei == 127) |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 420 | lapd_tei_receive(li, contents, *ilen); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 421 | |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 422 | teip = teip_from_tei(li, tei); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 423 | if (!teip) { |
| 424 | LOGP(DMI, LOGL_NOTICE, "Unknown TEI %u\n", tei); |
| 425 | return NULL; |
| 426 | } |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 427 | |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 428 | sap = lapd_sap_find(teip, sapi); |
| 429 | if (!sap) { |
| 430 | LOGP(DMI, LOGL_INFO, "No SAP for TEI=%u / SAPI=%u, " |
| 431 | "allocating\n", tei, sapi); |
| 432 | sap = lapd_sap_alloc(teip, sapi); |
| 433 | } |
| 434 | |
| 435 | DEBUGP(DMI, "<- %c %s sapi %x tei %3d cmd %x pf %x ns %3d nr %3d " |
| 436 | "ilen %d teip %p vs %d va %d vr %d len %d\n", |
| 437 | lapd_msg_types[typ], lapd_cmd_types[cmd], sapi, tei, command, pf, |
| 438 | ns, nr, *ilen, teip, sap->vs, sap->va, sap->vr, len); |
| 439 | |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 440 | switch (cmd) { |
| 441 | case LAPD_CMD_I: |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 442 | if (ns != sap->vr) { |
| 443 | DEBUGP(DMI, "ns %d != vr %d\n", ns, sap->vr); |
| 444 | if (ns == ((sap->vr - 1) & 0x7f)) { |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 445 | DEBUGP(DMI, "DOUBLE FRAME, ignoring\n"); |
| 446 | cmd = 0; // ignore |
| 447 | } else { |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 448 | assert(0); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 449 | }; |
| 450 | } else { |
| 451 | //printf("IN SEQUENCE\n"); |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 452 | sap->vr = (ns + 1) & 0x7f; // FIXME: hack! |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 453 | }; |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 454 | |
| 455 | break; |
| 456 | case LAPD_CMD_UI: |
| 457 | break; |
| 458 | case LAPD_CMD_SABME: |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 459 | sap->vs = 0; |
| 460 | sap->vr = 0; |
| 461 | sap->va = 0; |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 462 | |
| 463 | // ua |
| 464 | resp[l++] = data[0]; |
| 465 | resp[l++] = (tei << 1) | 1; |
| 466 | resp[l++] = 0x73; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 467 | li->transmit_cb(resp, l, li->cbdata); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 468 | if (teip->state != LAPD_TEI_ACTIVE) { |
| 469 | if (teip->state == LAPD_TEI_ASSIGNED) { |
| 470 | lapd_tei_set_state(teip, |
| 471 | LAPD_TEI_ACTIVE); |
| 472 | //printf("ASSIGNED and ACTIVE\n"); |
| 473 | } else { |
| 474 | #if 0 |
| 475 | DEBUGP(DMI, "rr in strange state, send rej\n"); |
| 476 | |
| 477 | // rej |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 478 | resp[l++] = (sap-> sapi << 2) | (li->network_side ? 0 : 2); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 479 | resp[l++] = (tei << 1) | 1; |
| 480 | resp[l++] = 0x09; //rej |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 481 | resp[l++] = ((sap->vr + 1) << 1) | 0; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 482 | li->transmit_cb(resp, l, li->cbdata); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 483 | pf = 0; // dont reply |
| 484 | #endif |
| 485 | }; |
| 486 | }; |
| 487 | |
| 488 | *prim = LAPD_MPH_ACTIVATE_IND; |
| 489 | break; |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 490 | case LAPD_CMD_UA: |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 491 | sap->vs = 0; |
| 492 | sap->vr = 0; |
| 493 | sap->va = 0; |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 494 | lapd_tei_set_state(teip, LAPD_TEI_ACTIVE); |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 495 | lapd_sap_set_state(teip, sapi, SAP_STATE_ACTIVE); |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 496 | *prim = LAPD_MPH_ACTIVATE_IND; |
| 497 | break; |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 498 | case LAPD_CMD_RR: |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 499 | sap->va = (nr & 0x7f); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 500 | #if 0 |
| 501 | if (teip->state != LAPD_TEI_ACTIVE) { |
| 502 | if (teip->state == LAPD_TEI_ASSIGNED) { |
| 503 | lapd_tei_set_state(teip, LAPD_TEI_ACTIVE); |
| 504 | *prim = LAPD_MPH_ACTIVATE_IND; |
| 505 | //printf("ASSIGNED and ACTIVE\n"); |
| 506 | } else { |
| 507 | #if 0 |
| 508 | DEBUGP(DMI, "rr in strange " "state, send rej\n"); |
| 509 | |
| 510 | // rej |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 511 | resp[l++] = (sap-> sapi << 2) | (li->network_side ? 0 : 2); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 512 | resp[l++] = (tei << 1) | 1; |
| 513 | resp[l++] = 0x09; //rej |
| 514 | resp[l++] = |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 515 | ((sap->vr + 1) << 1) | 0; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 516 | li->transmit_cb(resp, l, li->cbdata); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 517 | pf = 0; // dont reply |
| 518 | #endif |
| 519 | }; |
| 520 | }; |
| 521 | #endif |
| 522 | if (pf) { |
| 523 | // interrogating us, send rr |
| 524 | resp[l++] = data[0]; |
| 525 | resp[l++] = (tei << 1) | 1; |
| 526 | resp[l++] = 0x01; // rr |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 527 | resp[l++] = (LAPD_NR(sap) << 1) | (data[3] & 1); // pf bit from req |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 528 | |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 529 | li->transmit_cb(resp, l, li->cbdata); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 530 | |
| 531 | }; |
| 532 | break; |
| 533 | case LAPD_CMD_FRMR: |
| 534 | // frame reject |
| 535 | #if 0 |
| 536 | if (teip->state == LAPD_TEI_ACTIVE) |
| 537 | *prim = LAPD_MPH_DEACTIVATE_IND; |
| 538 | lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED); |
| 539 | #endif |
Harald Welte | 0ae5755 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 540 | LOGP(DMI, LOGL_NOTICE, "frame reject, ignoring\n"); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 541 | break; |
| 542 | case LAPD_CMD_DISC: |
| 543 | // disconnect |
| 544 | resp[l++] = data[0]; |
| 545 | resp[l++] = (tei << 1) | 1; |
| 546 | resp[l++] = 0x73; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 547 | li->transmit_cb(resp, l, li->cbdata); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 548 | lapd_tei_set_state(teip, LAPD_TEI_NONE); |
| 549 | break; |
| 550 | default: |
Harald Welte | 0ae5755 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 551 | LOGP(DMI, LOGL_NOTICE, "unknown cmd for tei %d (cmd %x)\n", |
| 552 | tei, cmd); |
| 553 | break; |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 554 | } |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 555 | |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 556 | if (typ == LAPD_TYPE_I) { |
Harald Welte | 0ae5755 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 557 | /* send rr |
| 558 | * Thu Jan 22 19:17:13 2009 <4000> sangoma.c:340 read (62/25) 4: fa 33 01 0a |
| 559 | * 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 |
| 560 | */ |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 561 | |
Harald Welte | 0ae5755 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 562 | /* interrogating us, send rr */ |
Harald Welte | 0abc11a | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 563 | DEBUGP(DMI, "Sending RR response\n"); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 564 | resp[l++] = data[0]; |
| 565 | resp[l++] = (tei << 1) | 1; |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 566 | resp[l++] = 0x01; // rr |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 567 | resp[l++] = (LAPD_NR(sap) << 1) | (data[3] & 1); // pf bit from req |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 568 | |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 569 | li->transmit_cb(resp, l, li->cbdata); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 570 | |
Matthew Fredrickson | 69245a0 | 2010-03-15 12:24:39 -0500 | [diff] [blame] | 571 | if (cmd != 0) { |
| 572 | *prim = LAPD_DL_DATA_IND; |
| 573 | return contents; |
| 574 | } |
| 575 | } else if (tei != 127 && typ == LAPD_TYPE_U && cmd == LAPD_CMD_UI) { |
| 576 | *prim = LAPD_DL_UNITDATA_IND; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 577 | return contents; |
Matthew Fredrickson | 69245a0 | 2010-03-15 12:24:39 -0500 | [diff] [blame] | 578 | } |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 579 | |
| 580 | return NULL; |
| 581 | }; |
| 582 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 583 | /* low-level function to send a single SABM message */ |
| 584 | static int lapd_send_sabm(struct lapd_instance *li, uint8_t tei, uint8_t sapi) |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 585 | { |
| 586 | struct msgb *msg = msgb_alloc_headroom(1024, 128, "LAPD SABM"); |
| 587 | if (!msg) |
| 588 | return -ENOMEM; |
| 589 | |
| 590 | DEBUGP(DMI, "Sending SABM for TEI=%u, SAPI=%u\n", tei, sapi); |
| 591 | |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 592 | msgb_put_u8(msg, (sapi << 2) | (li->network_side ? 2 : 0)); |
| 593 | msgb_put_u8(msg, (tei << 1) | 1); |
| 594 | msgb_put_u8(msg, 0x7F); |
| 595 | |
| 596 | li->transmit_cb(msg->data, msg->len, li->cbdata); |
| 597 | |
| 598 | msgb_free(msg); |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 603 | /* timer call-back function for SABM re-transmission */ |
| 604 | static void sabme_timer_cb(void *_sap) |
| 605 | { |
| 606 | struct lapd_sap *sap = _sap; |
| 607 | |
| 608 | lapd_send_sabm(sap->tei->li, sap->tei->tei, sap->sapi); |
| 609 | |
Harald Welte | 909212f | 2011-02-13 15:42:07 +0100 | [diff] [blame] | 610 | if (sap->state == SAP_STATE_SABM_RETRANS) |
| 611 | bsc_schedule_timer(&sap->sabme_timer, SABM_INTERVAL); |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /* Start a (user-side) SAP for the specified TEI/SAPI on the LAPD instance */ |
| 615 | int lapd_sap_start(struct lapd_instance *li, uint8_t tei, uint8_t sapi) |
| 616 | { |
| 617 | struct lapd_sap *sap; |
| 618 | struct lapd_tei *teip; |
| 619 | |
| 620 | teip = teip_from_tei(li, tei); |
| 621 | if (!teip) |
| 622 | teip = lapd_tei_alloc(li, tei); |
| 623 | |
| 624 | sap = lapd_sap_find(teip, sapi); |
| 625 | if (sap) |
| 626 | return -EEXIST; |
| 627 | |
| 628 | sap = lapd_sap_alloc(teip, sapi); |
| 629 | |
| 630 | lapd_sap_set_state(teip, sapi, SAP_STATE_SABM_RETRANS); |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | /* Stop a (user-side) SAP for the specified TEI/SAPI on the LAPD instance */ |
| 636 | int lapd_sap_stop(struct lapd_instance *li, uint8_t tei, uint8_t sapi) |
| 637 | { |
| 638 | struct lapd_tei *teip; |
| 639 | struct lapd_sap *sap; |
| 640 | |
| 641 | teip = teip_from_tei(li, tei); |
| 642 | if (!teip) |
| 643 | return -ENODEV; |
| 644 | |
| 645 | sap = lapd_sap_find(teip, sapi); |
| 646 | if (!sap) |
| 647 | return -ENODEV; |
| 648 | |
Harald Welte | 909212f | 2011-02-13 15:42:07 +0100 | [diff] [blame] | 649 | lapd_sap_set_state(teip, sapi, SAP_STATE_INACTIVE); |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 650 | |
| 651 | llist_del(&sap->list); |
| 652 | talloc_free(sap); |
| 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | /* Transmit Data (I-Frame) on the given LAPD Instance / TEI / SAPI */ |
Harald Welte | 4ee2eaf | 2011-02-05 20:20:50 +0100 | [diff] [blame] | 658 | 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] | 659 | uint8_t *data, unsigned int len) |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 660 | { |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 661 | struct lapd_tei *teip = teip_from_tei(li, tei); |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 662 | struct lapd_sap *sap; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 663 | |
| 664 | if (!teip) { |
| 665 | LOGP(DMI, LOGL_ERROR, "Cannot transmit on non-existing " |
| 666 | "TEI %u\n", tei); |
| 667 | return; |
| 668 | } |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 669 | |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 670 | sap = lapd_sap_find(teip, sapi); |
| 671 | if (!sap) { |
| 672 | LOGP(DMI, LOGL_INFO, "Tx on unknown SAPI=%u in TEI=%u, " |
| 673 | "allocating\n", sapi, tei); |
| 674 | sap = lapd_sap_alloc(teip, sapi); |
| 675 | } |
| 676 | |
Harald Welte | 0ae5755 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 677 | /* prepend stuff */ |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 678 | uint8_t buf[10000]; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 679 | memset(buf, 0, sizeof(buf)); |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 680 | memmove(buf + 4, data, len); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 681 | len += 4; |
| 682 | |
Harald Welte | 4ee2eaf | 2011-02-05 20:20:50 +0100 | [diff] [blame] | 683 | buf[0] = (sapi << 2) | (li->network_side ? 2 : 0); |
| 684 | buf[1] = (tei << 1) | 1; |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 685 | buf[2] = (LAPD_NS(sap) << 1); |
| 686 | buf[3] = (LAPD_NR(sap) << 1) | 0; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 687 | |
Harald Welte | a0fe72d | 2011-02-14 15:51:57 +0100 | [diff] [blame] | 688 | sap->vs = (sap->vs + 1) & 0x7f; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 689 | |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 690 | li->transmit_cb(buf, len, li->cbdata); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 691 | }; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 692 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 693 | /* Allocate a new LAPD instance */ |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 694 | struct lapd_instance *lapd_instance_alloc(int network_side, |
| 695 | void (*tx_cb)(uint8_t *data, int len, |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 696 | void *cbdata), void *cbdata) |
| 697 | { |
| 698 | struct lapd_instance *li; |
| 699 | |
| 700 | li = talloc_zero(NULL, struct lapd_instance); |
| 701 | if (!li) |
| 702 | return NULL; |
| 703 | |
| 704 | li->transmit_cb = tx_cb; |
| 705 | li->cbdata = cbdata; |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 706 | li->network_side = network_side; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 707 | INIT_LLIST_HEAD(&li->tei_list); |
| 708 | |
| 709 | return li; |
| 710 | } |