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