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 | |
| 39 | #include <osmocore/linuxlist.h> |
| 40 | #include <osmocore/talloc.h> |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 41 | #include <osmocore/msgb.h> |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 42 | #include <osmocore/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 | |
| 122 | uint8_t tei; |
Harald Welte | 7e859bc | 2011-02-04 20:36:50 +0100 | [diff] [blame] | 123 | /* A valid N(R) value is one that is in the range V(A) ≤ N(R) ≤ V(S). */ |
| 124 | int vs; /* next to be transmitted */ |
| 125 | int va; /* last acked by peer */ |
| 126 | int vr; /* next expected to be received */ |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 127 | lapd_tei_state state; |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 128 | |
| 129 | struct llist_head sap_list; |
| 130 | }; |
| 131 | |
| 132 | /* Structure representing a SAP within a TEI. We use this for TE-mode to |
| 133 | * re-transmit SABM */ |
| 134 | struct lapd_sap { |
| 135 | struct llist_head list; |
| 136 | struct lapd_tei *tei; |
| 137 | uint8_t sapi; |
| 138 | enum lapd_sap_state state; |
| 139 | |
| 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 | */ |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 165 | #define LAPD_NS(teip) (teip->vs) |
| 166 | #define LAPD_NR(teip) (teip->vr) |
| 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); |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 293 | 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 | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 319 | |
| 320 | uint8_t resp[8]; |
| 321 | int l = 0; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 322 | |
| 323 | *ilen = 0; |
| 324 | *prim = 0; |
| 325 | |
| 326 | if (len < 2) { |
Harald Welte | 0abc11a | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 327 | DEBUGP(DMI, "len %d < 2\n", len); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 328 | return NULL; |
| 329 | }; |
| 330 | |
| 331 | if ((data[0] & 1) != 0 || (data[1] & 1) != 1) { |
Harald Welte | 0abc11a | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 332 | DEBUGP(DMI, "address field %x/%x not well formed\n", data[0], |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 333 | data[1]); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 334 | return NULL; |
| 335 | }; |
| 336 | |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 337 | sapi = data[0] >> 2; |
| 338 | cr = (data[0] >> 1) & 1; |
| 339 | tei = data[1] >> 1; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 340 | command = li->network_side ^ cr; |
Harald Welte | 0abc11a | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 341 | //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] | 342 | |
| 343 | if (len < 3) { |
Harald Welte | 0abc11a | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 344 | DEBUGP(DMI, "len %d < 3\n", len); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 345 | return NULL; |
| 346 | }; |
| 347 | |
| 348 | lapd_msg_type typ = 0; |
| 349 | lapd_cmd_type cmd = 0; |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 350 | pf = -1; |
| 351 | ns = -1; |
| 352 | nr = -1; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 353 | if ((data[2] & 1) == 0) { |
| 354 | typ = LAPD_TYPE_I; |
| 355 | assert(len >= 4); |
| 356 | ns = data[2] >> 1; |
| 357 | nr = data[3] >> 1; |
| 358 | pf = data[3] & 1; |
| 359 | cmd = LAPD_CMD_I; |
| 360 | } else if ((data[2] & 3) == 1) { |
| 361 | typ = LAPD_TYPE_S; |
| 362 | assert(len >= 4); |
| 363 | nr = data[3] >> 1; |
| 364 | pf = data[3] & 1; |
| 365 | switch (data[2]) { |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 366 | case 0x1: |
| 367 | cmd = LAPD_CMD_RR; |
| 368 | break; |
| 369 | case 0x5: |
| 370 | cmd = LAPD_CMD_RNR; |
| 371 | break; |
| 372 | case 0x9: |
| 373 | cmd = LAPD_CMD_REJ; |
| 374 | break; |
| 375 | default: |
Harald Welte | 0ae5755 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 376 | LOGP(DMI, LOGL_ERROR, "unknown LAPD S cmd %x\n", data[2]); |
| 377 | return NULL; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 378 | }; |
| 379 | } else if ((data[2] & 3) == 3) { |
| 380 | typ = LAPD_TYPE_U; |
| 381 | pf = (data[2] >> 4) & 1; |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 382 | int val = data[2] & ~(1 << 4); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 383 | switch (val) { |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 384 | case 0x6f: |
| 385 | cmd = LAPD_CMD_SABME; |
| 386 | break; |
| 387 | case 0x0f: |
| 388 | cmd = LAPD_CMD_DM; |
| 389 | break; |
| 390 | case 0x03: |
| 391 | cmd = LAPD_CMD_UI; |
| 392 | break; |
| 393 | case 0x43: |
| 394 | cmd = LAPD_CMD_DISC; |
| 395 | break; |
| 396 | case 0x63: |
| 397 | cmd = LAPD_CMD_UA; |
| 398 | break; |
| 399 | case 0x87: |
| 400 | cmd = LAPD_CMD_FRMR; |
| 401 | break; |
| 402 | case 0xaf: |
| 403 | cmd = LAPD_CMD_XID; |
| 404 | break; |
| 405 | |
| 406 | default: |
Harald Welte | 0ae5755 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 407 | LOGP(DMI, LOGL_ERROR, "unknown U cmd %x " |
| 408 | "(pf %x data %x)\n", val, pf, data[2]); |
| 409 | return NULL; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 410 | }; |
| 411 | }; |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 412 | |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 413 | contents = &data[4]; |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 414 | if (typ == LAPD_TYPE_U) |
| 415 | contents--; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 416 | *ilen = len - (contents - data); |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 417 | |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 418 | if (tei == 127) |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 419 | lapd_tei_receive(li, contents, *ilen); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 420 | |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 421 | teip = teip_from_tei(li, tei); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 422 | |
Harald Welte | 0abc11a | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 423 | DEBUGP(DMI, "<- %c %s sapi %x tei %3d cmd %x pf %x ns %3d nr %3d " |
| 424 | "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] | 425 | lapd_msg_types[typ], lapd_cmd_types[cmd], sapi, tei, command, pf, |
| 426 | ns, nr, *ilen, teip, teip ? teip->vs : -1, teip ? teip->va : -1, |
| 427 | teip ? teip->vr : -1, len); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 428 | |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 429 | if (!teip) { |
| 430 | LOGP(DMI, LOGL_NOTICE, "Unknown TEI %u\n", tei); |
| 431 | return NULL; |
| 432 | } |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 433 | |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 434 | switch (cmd) { |
| 435 | case LAPD_CMD_I: |
| 436 | if (ns != teip->vr) { |
| 437 | DEBUGP(DMI, "ns %d != vr %d\n", ns, teip->vr); |
| 438 | if (ns == ((teip->vr - 1) & 0x7f)) { |
| 439 | DEBUGP(DMI, "DOUBLE FRAME, ignoring\n"); |
| 440 | cmd = 0; // ignore |
| 441 | } else { |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 442 | assert(0); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 443 | }; |
| 444 | } else { |
| 445 | //printf("IN SEQUENCE\n"); |
| 446 | teip->vr = (ns + 1) & 0x7f; // FIXME: hack! |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 447 | }; |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 448 | |
| 449 | break; |
| 450 | case LAPD_CMD_UI: |
| 451 | break; |
| 452 | case LAPD_CMD_SABME: |
| 453 | teip->vs = 0; |
| 454 | teip->vr = 0; |
| 455 | teip->va = 0; |
| 456 | |
| 457 | // ua |
| 458 | resp[l++] = data[0]; |
| 459 | resp[l++] = (tei << 1) | 1; |
| 460 | resp[l++] = 0x73; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 461 | li->transmit_cb(resp, l, li->cbdata); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 462 | if (teip->state != LAPD_TEI_ACTIVE) { |
| 463 | if (teip->state == LAPD_TEI_ASSIGNED) { |
| 464 | lapd_tei_set_state(teip, |
| 465 | LAPD_TEI_ACTIVE); |
| 466 | //printf("ASSIGNED and ACTIVE\n"); |
| 467 | } else { |
| 468 | #if 0 |
| 469 | DEBUGP(DMI, "rr in strange state, send rej\n"); |
| 470 | |
| 471 | // rej |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 472 | resp[l++] = (teip-> sapi << 2) | (li->network_side ? 0 : 2); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 473 | resp[l++] = (tei << 1) | 1; |
| 474 | resp[l++] = 0x09; //rej |
| 475 | resp[l++] = ((teip->vr + 1) << 1) | 0; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 476 | li->transmit_cb(resp, l, li->cbdata); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 477 | pf = 0; // dont reply |
| 478 | #endif |
| 479 | }; |
| 480 | }; |
| 481 | |
| 482 | *prim = LAPD_MPH_ACTIVATE_IND; |
| 483 | break; |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 484 | case LAPD_CMD_UA: |
| 485 | teip->vs = 0; |
| 486 | teip->vr = 0; |
| 487 | teip->va = 0; |
| 488 | lapd_tei_set_state(teip, LAPD_TEI_ACTIVE); |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 489 | lapd_sap_set_state(teip, sapi, SAP_STATE_ACTIVE); |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 490 | *prim = LAPD_MPH_ACTIVATE_IND; |
| 491 | break; |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 492 | case LAPD_CMD_RR: |
| 493 | teip->va = (nr & 0x7f); |
| 494 | #if 0 |
| 495 | if (teip->state != LAPD_TEI_ACTIVE) { |
| 496 | if (teip->state == LAPD_TEI_ASSIGNED) { |
| 497 | lapd_tei_set_state(teip, LAPD_TEI_ACTIVE); |
| 498 | *prim = LAPD_MPH_ACTIVATE_IND; |
| 499 | //printf("ASSIGNED and ACTIVE\n"); |
| 500 | } else { |
| 501 | #if 0 |
| 502 | DEBUGP(DMI, "rr in strange " "state, send rej\n"); |
| 503 | |
| 504 | // rej |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 505 | resp[l++] = (teip-> sapi << 2) | (li->network_side ? 0 : 2); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 506 | resp[l++] = (tei << 1) | 1; |
| 507 | resp[l++] = 0x09; //rej |
| 508 | resp[l++] = |
| 509 | ((teip->vr + 1) << 1) | 0; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 510 | li->transmit_cb(resp, l, li->cbdata); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 511 | pf = 0; // dont reply |
| 512 | #endif |
| 513 | }; |
| 514 | }; |
| 515 | #endif |
| 516 | if (pf) { |
| 517 | // interrogating us, send rr |
| 518 | resp[l++] = data[0]; |
| 519 | resp[l++] = (tei << 1) | 1; |
| 520 | resp[l++] = 0x01; // rr |
| 521 | resp[l++] = (LAPD_NR(teip) << 1) | (data[3] & 1); // pf bit from req |
| 522 | |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 523 | li->transmit_cb(resp, l, li->cbdata); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 524 | |
| 525 | }; |
| 526 | break; |
| 527 | case LAPD_CMD_FRMR: |
| 528 | // frame reject |
| 529 | #if 0 |
| 530 | if (teip->state == LAPD_TEI_ACTIVE) |
| 531 | *prim = LAPD_MPH_DEACTIVATE_IND; |
| 532 | lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED); |
| 533 | #endif |
Harald Welte | 0ae5755 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 534 | LOGP(DMI, LOGL_NOTICE, "frame reject, ignoring\n"); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 535 | break; |
| 536 | case LAPD_CMD_DISC: |
| 537 | // disconnect |
| 538 | resp[l++] = data[0]; |
| 539 | resp[l++] = (tei << 1) | 1; |
| 540 | resp[l++] = 0x73; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 541 | li->transmit_cb(resp, l, li->cbdata); |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 542 | lapd_tei_set_state(teip, LAPD_TEI_NONE); |
| 543 | break; |
| 544 | default: |
Harald Welte | 0ae5755 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 545 | LOGP(DMI, LOGL_NOTICE, "unknown cmd for tei %d (cmd %x)\n", |
| 546 | tei, cmd); |
| 547 | break; |
Harald Welte | 716d2a4 | 2011-02-05 17:29:05 +0100 | [diff] [blame] | 548 | } |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 549 | |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 550 | if (typ == LAPD_TYPE_I) { |
Harald Welte | 0ae5755 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 551 | /* send rr |
| 552 | * Thu Jan 22 19:17:13 2009 <4000> sangoma.c:340 read (62/25) 4: fa 33 01 0a |
| 553 | * 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 |
| 554 | */ |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 555 | |
Harald Welte | 0ae5755 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 556 | /* interrogating us, send rr */ |
Harald Welte | 0abc11a | 2011-02-05 17:16:26 +0100 | [diff] [blame] | 557 | DEBUGP(DMI, "Sending RR response\n"); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 558 | resp[l++] = data[0]; |
| 559 | resp[l++] = (tei << 1) | 1; |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 560 | resp[l++] = 0x01; // rr |
| 561 | 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] | 562 | |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 563 | li->transmit_cb(resp, l, li->cbdata); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 564 | |
Matthew Fredrickson | 69245a0 | 2010-03-15 12:24:39 -0500 | [diff] [blame] | 565 | if (cmd != 0) { |
| 566 | *prim = LAPD_DL_DATA_IND; |
| 567 | return contents; |
| 568 | } |
| 569 | } else if (tei != 127 && typ == LAPD_TYPE_U && cmd == LAPD_CMD_UI) { |
| 570 | *prim = LAPD_DL_UNITDATA_IND; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 571 | return contents; |
Matthew Fredrickson | 69245a0 | 2010-03-15 12:24:39 -0500 | [diff] [blame] | 572 | } |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 573 | |
| 574 | return NULL; |
| 575 | }; |
| 576 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 577 | /* low-level function to send a single SABM message */ |
| 578 | 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] | 579 | { |
| 580 | struct msgb *msg = msgb_alloc_headroom(1024, 128, "LAPD SABM"); |
| 581 | if (!msg) |
| 582 | return -ENOMEM; |
| 583 | |
| 584 | DEBUGP(DMI, "Sending SABM for TEI=%u, SAPI=%u\n", tei, sapi); |
| 585 | |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 586 | msgb_put_u8(msg, (sapi << 2) | (li->network_side ? 2 : 0)); |
| 587 | msgb_put_u8(msg, (tei << 1) | 1); |
| 588 | msgb_put_u8(msg, 0x7F); |
| 589 | |
| 590 | li->transmit_cb(msg->data, msg->len, li->cbdata); |
| 591 | |
| 592 | msgb_free(msg); |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 597 | /* timer call-back function for SABM re-transmission */ |
| 598 | static void sabme_timer_cb(void *_sap) |
| 599 | { |
| 600 | struct lapd_sap *sap = _sap; |
| 601 | |
| 602 | lapd_send_sabm(sap->tei->li, sap->tei->tei, sap->sapi); |
| 603 | |
| 604 | bsc_schedule_timer(&sap->sabme_timer, SABM_INTERVAL); |
| 605 | } |
| 606 | |
| 607 | /* Start a (user-side) SAP for the specified TEI/SAPI on the LAPD instance */ |
| 608 | int lapd_sap_start(struct lapd_instance *li, uint8_t tei, uint8_t sapi) |
| 609 | { |
| 610 | struct lapd_sap *sap; |
| 611 | struct lapd_tei *teip; |
| 612 | |
| 613 | teip = teip_from_tei(li, tei); |
| 614 | if (!teip) |
| 615 | teip = lapd_tei_alloc(li, tei); |
| 616 | |
| 617 | sap = lapd_sap_find(teip, sapi); |
| 618 | if (sap) |
| 619 | return -EEXIST; |
| 620 | |
| 621 | sap = lapd_sap_alloc(teip, sapi); |
| 622 | |
| 623 | lapd_sap_set_state(teip, sapi, SAP_STATE_SABM_RETRANS); |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | /* Stop a (user-side) SAP for the specified TEI/SAPI on the LAPD instance */ |
| 629 | int lapd_sap_stop(struct lapd_instance *li, uint8_t tei, uint8_t sapi) |
| 630 | { |
| 631 | struct lapd_tei *teip; |
| 632 | struct lapd_sap *sap; |
| 633 | |
| 634 | teip = teip_from_tei(li, tei); |
| 635 | if (!teip) |
| 636 | return -ENODEV; |
| 637 | |
| 638 | sap = lapd_sap_find(teip, sapi); |
| 639 | if (!sap) |
| 640 | return -ENODEV; |
| 641 | |
| 642 | bsc_del_timer(&sap->sabme_timer); |
| 643 | |
| 644 | llist_del(&sap->list); |
| 645 | talloc_free(sap); |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | /* Transmit Data (I-Frame) on the given LAPD Instance / TEI / SAPI */ |
Harald Welte | 4ee2eaf | 2011-02-05 20:20:50 +0100 | [diff] [blame] | 651 | 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] | 652 | uint8_t *data, unsigned int len) |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 653 | { |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 654 | //printf("lapd_transmit %d, %d\n", tei, len); |
| 655 | //hexdump(data, len); |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 656 | struct lapd_tei *teip = teip_from_tei(li, tei); |
| 657 | |
| 658 | if (!teip) { |
| 659 | LOGP(DMI, LOGL_ERROR, "Cannot transmit on non-existing " |
| 660 | "TEI %u\n", tei); |
| 661 | return; |
| 662 | } |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 663 | |
Harald Welte | 0ae5755 | 2011-02-05 18:33:12 +0100 | [diff] [blame] | 664 | /* prepend stuff */ |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 665 | uint8_t buf[10000]; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 666 | memset(buf, 0, sizeof(buf)); |
Harald Welte | 30fe641 | 2011-02-04 20:34:08 +0100 | [diff] [blame] | 667 | memmove(buf + 4, data, len); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 668 | len += 4; |
| 669 | |
Harald Welte | 4ee2eaf | 2011-02-05 20:20:50 +0100 | [diff] [blame] | 670 | buf[0] = (sapi << 2) | (li->network_side ? 2 : 0); |
| 671 | buf[1] = (tei << 1) | 1; |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 672 | buf[2] = (LAPD_NS(teip) << 1); |
| 673 | buf[3] = (LAPD_NR(teip) << 1) | 0; |
| 674 | |
| 675 | teip->vs = (teip->vs + 1) & 0x7f; |
| 676 | |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 677 | li->transmit_cb(buf, len, li->cbdata); |
Matthew Fredrickson | bc6649e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 678 | }; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 679 | |
Harald Welte | dcf42e6 | 2011-02-13 11:58:21 +0100 | [diff] [blame] | 680 | /* Allocate a new LAPD instance */ |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 681 | struct lapd_instance *lapd_instance_alloc(int network_side, |
| 682 | void (*tx_cb)(uint8_t *data, int len, |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 683 | void *cbdata), void *cbdata) |
| 684 | { |
| 685 | struct lapd_instance *li; |
| 686 | |
| 687 | li = talloc_zero(NULL, struct lapd_instance); |
| 688 | if (!li) |
| 689 | return NULL; |
| 690 | |
| 691 | li->transmit_cb = tx_cb; |
| 692 | li->cbdata = cbdata; |
Harald Welte | 1a00d82 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 693 | li->network_side = network_side; |
Harald Welte | d38f105 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 694 | INIT_LLIST_HEAD(&li->tei_list); |
| 695 | |
| 696 | return li; |
| 697 | } |