Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1 | /* GPRS LLC protocol implementation as per 3GPP TS 04.64 */ |
| 2 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 3 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 16 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <errno.h> |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 23 | #include <stdint.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 24 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 25 | #include <osmocom/core/msgb.h> |
| 26 | #include <osmocom/core/linuxlist.h> |
| 27 | #include <osmocom/core/timer.h> |
| 28 | #include <osmocom/core/talloc.h> |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 29 | #include <osmocom/gprs/gprs_bssgp.h> |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 30 | |
| 31 | #include <openbsc/gsm_data.h> |
| 32 | #include <openbsc/debug.h> |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 33 | #include <openbsc/gprs_sgsn.h> |
| 34 | #include <openbsc/gprs_gmm.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 35 | #include <openbsc/gprs_llc.h> |
| 36 | #include <openbsc/crc24.h> |
Holger Hans Peter Freyther | 3dccda5 | 2011-10-14 23:42:13 +0200 | [diff] [blame] | 37 | #include <openbsc/sgsn.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 38 | |
Holger Hans Peter Freyther | 964a9b3 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 39 | enum gprs_llc_cmd { |
| 40 | GPRS_LLC_NULL, |
| 41 | GPRS_LLC_RR, |
| 42 | GPRS_LLC_ACK, |
| 43 | GPRS_LLC_RNR, |
| 44 | GPRS_LLC_SACK, |
| 45 | GPRS_LLC_DM, |
| 46 | GPRS_LLC_DISC, |
| 47 | GPRS_LLC_UA, |
| 48 | GPRS_LLC_SABM, |
| 49 | GPRS_LLC_FRMR, |
| 50 | GPRS_LLC_XID, |
| 51 | GPRS_LLC_UI, |
| 52 | }; |
| 53 | |
| 54 | static const struct value_string llc_cmd_strs[] = { |
| 55 | { GPRS_LLC_NULL, "NULL" }, |
| 56 | { GPRS_LLC_RR, "RR" }, |
| 57 | { GPRS_LLC_ACK, "ACK" }, |
| 58 | { GPRS_LLC_RNR, "RNR" }, |
| 59 | { GPRS_LLC_SACK, "SACK" }, |
| 60 | { GPRS_LLC_DM, "DM" }, |
| 61 | { GPRS_LLC_DISC, "DISC" }, |
| 62 | { GPRS_LLC_UA, "UA" }, |
| 63 | { GPRS_LLC_SABM, "SABM" }, |
| 64 | { GPRS_LLC_FRMR, "FRMR" }, |
| 65 | { GPRS_LLC_XID, "XID" }, |
| 66 | { GPRS_LLC_UI, "UI" }, |
| 67 | { 0, NULL } |
| 68 | }; |
| 69 | |
| 70 | struct gprs_llc_hdr_parsed { |
| 71 | uint8_t sapi; |
| 72 | uint8_t is_cmd:1, |
| 73 | ack_req:1, |
| 74 | is_encrypted:1; |
| 75 | uint32_t seq_rx; |
| 76 | uint32_t seq_tx; |
| 77 | uint32_t fcs; |
| 78 | uint32_t fcs_calc; |
| 79 | uint8_t *data; |
| 80 | uint16_t data_len; |
| 81 | uint16_t crc_length; |
| 82 | enum gprs_llc_cmd cmd; |
| 83 | }; |
| 84 | |
| 85 | static struct gprs_llc_llme *llme_alloc(uint32_t tlli); |
| 86 | |
Holger Hans Peter Freyther | 7e0fec1 | 2013-07-29 10:09:12 +0200 | [diff] [blame] | 87 | /* If the TLLI is foreign, return its local version */ |
| 88 | static inline uint32_t tlli_foreign2local(uint32_t tlli) |
| 89 | { |
| 90 | uint32_t new_tlli; |
| 91 | |
| 92 | if (gprs_tlli_type(tlli) == TLLI_FOREIGN) { |
| 93 | new_tlli = tlli | 0x40000000; |
| 94 | DEBUGP(DLLC, "TLLI 0x%08x is foreign, converting to " |
| 95 | "local TLLI 0x%08x\n", tlli, new_tlli); |
| 96 | } else |
| 97 | new_tlli = tlli; |
| 98 | |
| 99 | return new_tlli; |
| 100 | } |
| 101 | |
Harald Welte | faa70ff | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 102 | /* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU |
| 103 | * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */ |
| 104 | static int _bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx) |
| 105 | { |
| 106 | struct bssgp_dl_ud_par dup; |
| 107 | const uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 }; |
| 108 | |
Harald Welte | 8c00496 | 2012-07-04 21:53:12 +0200 | [diff] [blame] | 109 | memset(&dup, 0, sizeof(dup)); |
| 110 | /* before we have received some identity from the MS, we might |
| 111 | * not yet have a MMC context (e.g. XID negotiation of primarly |
| 112 | * LLC connection fro GMM sapi). */ |
| 113 | if (mmctx) { |
| 114 | dup.imsi = mmctx->imsi; |
| 115 | dup.drx_parms = mmctx->drx_parms; |
| 116 | dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len; |
| 117 | dup.ms_ra_cap.v = mmctx->ms_radio_access_capa.buf; |
Holger Hans Peter Freyther | 7e0fec1 | 2013-07-29 10:09:12 +0200 | [diff] [blame] | 118 | |
| 119 | /* make sure we only send it to the right llme */ |
| 120 | OSMO_ASSERT(msgb_tlli(msg) == mmctx->llme->tlli |
| 121 | || msgb_tlli(msg) == mmctx->llme->old_tlli |
| 122 | || tlli_foreign2local(msgb_tlli(msg)) == mmctx->llme->tlli |
| 123 | || tlli_foreign2local(msgb_tlli(msg)) == mmctx->llme->old_tlli); |
Harald Welte | 8c00496 | 2012-07-04 21:53:12 +0200 | [diff] [blame] | 124 | } |
Harald Welte | faa70ff | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 125 | memcpy(&dup.qos_profile, qos_profile_default, |
| 126 | sizeof(qos_profile_default)); |
| 127 | |
Harald Welte | ce95b27 | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 128 | return bssgp_tx_dl_ud(msg, 1000, &dup); |
Harald Welte | faa70ff | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 132 | /* Section 8.9.9 LLC layer parameter default values */ |
| 133 | static const struct gprs_llc_params llc_default_params[] = { |
| 134 | [1] = { |
| 135 | .t200_201 = 5, |
| 136 | .n200 = 3, |
| 137 | .n201_u = 400, |
| 138 | }, |
| 139 | [2] = { |
| 140 | .t200_201 = 5, |
| 141 | .n200 = 3, |
| 142 | .n201_u = 270, |
| 143 | }, |
| 144 | [3] = { |
| 145 | .iov_i_exp = 27, |
| 146 | .t200_201 = 5, |
| 147 | .n200 = 3, |
| 148 | .n201_u = 500, |
| 149 | .n201_i = 1503, |
| 150 | .mD = 1520, |
| 151 | .mU = 1520, |
| 152 | .kD = 16, |
| 153 | .kU = 16, |
| 154 | }, |
| 155 | [5] = { |
| 156 | .iov_i_exp = 27, |
| 157 | .t200_201 = 10, |
| 158 | .n200 = 3, |
| 159 | .n201_u = 500, |
| 160 | .n201_i = 1503, |
| 161 | .mD = 760, |
| 162 | .mU = 760, |
| 163 | .kD = 8, |
| 164 | .kU = 8, |
| 165 | }, |
| 166 | [7] = { |
| 167 | .t200_201 = 20, |
| 168 | .n200 = 3, |
| 169 | .n201_u = 270, |
| 170 | }, |
| 171 | [8] = { |
| 172 | .t200_201 = 20, |
| 173 | .n200 = 3, |
| 174 | .n201_u = 270, |
| 175 | }, |
| 176 | [9] = { |
| 177 | .iov_i_exp = 27, |
| 178 | .t200_201 = 20, |
| 179 | .n200 = 3, |
| 180 | .n201_u = 500, |
| 181 | .n201_i = 1503, |
| 182 | .mD = 380, |
| 183 | .mU = 380, |
| 184 | .kD = 4, |
| 185 | .kU = 4, |
| 186 | }, |
| 187 | [11] = { |
| 188 | .iov_i_exp = 27, |
| 189 | .t200_201 = 40, |
| 190 | .n200 = 3, |
| 191 | .n201_u = 500, |
| 192 | .n201_i = 1503, |
| 193 | .mD = 190, |
| 194 | .mU = 190, |
| 195 | .kD = 2, |
| 196 | .kU = 2, |
| 197 | }, |
| 198 | }; |
| 199 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 200 | LLIST_HEAD(gprs_llc_llmes); |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 201 | void *llc_tall_ctx; |
| 202 | |
| 203 | /* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */ |
Holger Hans Peter Freyther | 012a7ee | 2013-07-29 09:06:46 +0200 | [diff] [blame] | 204 | static struct gprs_llc_lle *lle_by_tlli_sapi(const uint32_t tlli, uint8_t sapi) |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 205 | { |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 206 | struct gprs_llc_llme *llme; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 207 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 208 | llist_for_each_entry(llme, &gprs_llc_llmes, list) { |
| 209 | if (llme->tlli == tlli || llme->old_tlli == tlli) |
| 210 | return &llme->lle[sapi]; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 211 | } |
| 212 | return NULL; |
| 213 | } |
| 214 | |
Holger Hans Peter Freyther | 964a9b3 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 215 | /* lookup LLC Entity for RX based on DLCI (TLLI+SAPI tuple) */ |
| 216 | static struct gprs_llc_lle *lle_for_rx_by_tlli_sapi(const uint32_t tlli, |
| 217 | uint8_t sapi, enum gprs_llc_cmd cmd) |
| 218 | { |
| 219 | struct gprs_llc_lle *lle; |
| 220 | |
| 221 | /* We already know about this TLLI */ |
| 222 | lle = lle_by_tlli_sapi(tlli, sapi); |
| 223 | if (lle) |
| 224 | return lle; |
| 225 | |
| 226 | /* Maybe it is a routing area update but we already know this sapi? */ |
| 227 | if (gprs_tlli_type(tlli) == TLLI_FOREIGN) { |
| 228 | lle = lle_by_tlli_sapi(tlli_foreign2local(tlli), sapi); |
| 229 | if (lle) { |
| 230 | LOGP(DLLC, LOGL_NOTICE, |
| 231 | "LLC RX: Found a local entry for TLLI 0x%08x\n", |
| 232 | tlli); |
| 233 | return lle; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded, |
| 238 | * except UID and XID frames with SAPI=1 */ |
| 239 | if (sapi == GPRS_SAPI_GMM && |
| 240 | (cmd == GPRS_LLC_XID || cmd == GPRS_LLC_UI)) { |
| 241 | struct gprs_llc_llme *llme; |
| 242 | /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */ |
| 243 | llme = llme_alloc(tlli); |
| 244 | LOGP(DLLC, LOGL_DEBUG, "LLC RX: unknown TLLI 0x%08x, " |
| 245 | "creating LLME on the fly\n", tlli); |
| 246 | lle = &llme->lle[sapi]; |
| 247 | return lle; |
| 248 | } |
| 249 | |
| 250 | LOGP(DLLC, LOGL_NOTICE, |
| 251 | "unknown TLLI(0x%08x)/SAPI(%d): Silently dropping\n", |
| 252 | tlli, sapi); |
| 253 | return NULL; |
| 254 | } |
| 255 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 256 | static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi) |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 257 | { |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 258 | struct gprs_llc_lle *lle = &llme->lle[sapi]; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 259 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 260 | lle->llme = llme; |
| 261 | lle->sapi = sapi; |
| 262 | lle->state = GPRS_LLES_UNASSIGNED; |
| 263 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 264 | /* Initialize according to parameters */ |
| 265 | memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params)); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static struct gprs_llc_llme *llme_alloc(uint32_t tlli) |
| 269 | { |
| 270 | struct gprs_llc_llme *llme; |
| 271 | uint32_t i; |
| 272 | |
| 273 | llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme); |
| 274 | if (!llme) |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 275 | return NULL; |
| 276 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 277 | llme->tlli = tlli; |
Harald Welte | 875840c | 2010-07-01 11:54:31 +0200 | [diff] [blame] | 278 | llme->old_tlli = 0xffffffff; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 279 | llme->state = GPRS_LLMS_UNASSIGNED; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 280 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 281 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) |
| 282 | lle_init(llme, i); |
| 283 | |
| 284 | llist_add(&llme->list, &gprs_llc_llmes); |
| 285 | |
| 286 | return llme; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 287 | } |
| 288 | |
Harald Welte | f7fef48 | 2010-06-28 22:18:26 +0200 | [diff] [blame] | 289 | static void llme_free(struct gprs_llc_llme *llme) |
| 290 | { |
| 291 | llist_del(&llme->list); |
| 292 | talloc_free(llme); |
| 293 | } |
| 294 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 295 | #define LLC_ALLOC_SIZE 16384 |
| 296 | #define UI_HDR_LEN 3 |
| 297 | #define N202 4 |
| 298 | #define CRC24_LENGTH 3 |
| 299 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 300 | static int gprs_llc_fcs(uint8_t *data, unsigned int len) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 301 | { |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 302 | uint32_t fcs_calc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 303 | |
| 304 | fcs_calc = crc24_calc(INIT_CRC24, data, len); |
| 305 | fcs_calc = ~fcs_calc; |
| 306 | fcs_calc &= 0xffffff; |
| 307 | |
| 308 | return fcs_calc; |
| 309 | } |
| 310 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 311 | static void t200_expired(void *data) |
| 312 | { |
| 313 | struct gprs_llc_lle *lle = data; |
| 314 | |
| 315 | /* 8.5.1.3: Expiry of T200 */ |
| 316 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 317 | if (lle->retrans_ctr >= lle->params.n200) { |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 318 | /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 319 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | switch (lle->state) { |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 323 | case GPRS_LLES_LOCAL_EST: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 324 | /* FIXME: retransmit SABM */ |
| 325 | /* FIXME: re-start T200 */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 326 | lle->retrans_ctr++; |
| 327 | break; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 328 | case GPRS_LLES_LOCAL_REL: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 329 | /* FIXME: retransmit DISC */ |
| 330 | /* FIXME: re-start T200 */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 331 | lle->retrans_ctr++; |
| 332 | break; |
| 333 | } |
| 334 | |
| 335 | } |
| 336 | |
| 337 | static void t201_expired(void *data) |
| 338 | { |
| 339 | struct gprs_llc_lle *lle = data; |
| 340 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 341 | if (lle->retrans_ctr < lle->params.n200) { |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 342 | /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */ |
| 343 | /* FIXME: set timer T201 */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 344 | lle->retrans_ctr++; |
| 345 | } |
| 346 | } |
| 347 | |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 348 | int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command, |
| 349 | enum gprs_llc_u_cmd u_cmd, int pf_bit) |
| 350 | { |
| 351 | uint8_t *fcs, *llch; |
| 352 | uint8_t addr, ctrl; |
| 353 | uint32_t fcs_calc; |
| 354 | |
| 355 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 356 | |
| 357 | /* Address Field */ |
| 358 | addr = sapi & 0xf; |
| 359 | if (command) |
| 360 | addr |= 0x40; |
| 361 | |
| 362 | /* 6.3 Figure 8 */ |
| 363 | ctrl = 0xe0 | u_cmd; |
| 364 | if (pf_bit) |
| 365 | ctrl |= 0x10; |
| 366 | |
| 367 | /* prepend LLC UI header */ |
| 368 | llch = msgb_push(msg, 2); |
| 369 | llch[0] = addr; |
| 370 | llch[1] = ctrl; |
| 371 | |
| 372 | /* append FCS to end of frame */ |
| 373 | fcs = msgb_put(msg, 3); |
| 374 | fcs_calc = gprs_llc_fcs(llch, fcs - llch); |
| 375 | fcs[0] = fcs_calc & 0xff; |
| 376 | fcs[1] = (fcs_calc >> 8) & 0xff; |
| 377 | fcs[2] = (fcs_calc >> 16) & 0xff; |
| 378 | |
| 379 | /* Identifiers passed down: (BVCI, NSEI) */ |
| 380 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 381 | /* Send BSSGP-DL-UNITDATA.req */ |
Harald Welte | b1fd902 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 382 | return _bssgp_tx_dl_ud(msg, NULL); |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* Send XID response to LLE */ |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 386 | static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg, |
| 387 | int command) |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 388 | { |
| 389 | /* copy identifiers from LLE to ensure lower layers can route */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 390 | msgb_tlli(msg) = lle->llme->tlli; |
| 391 | msgb_bvci(msg) = lle->llme->bvci; |
| 392 | msgb_nsei(msg) = lle->llme->nsei; |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 393 | |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 394 | return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1); |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 397 | /* Transmit a UI frame over the given SAPI */ |
Harald Welte | 56a0145 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 398 | int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command, |
| 399 | void *mmctx) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 400 | { |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 401 | struct gprs_llc_lle *lle; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 402 | uint8_t *fcs, *llch; |
| 403 | uint8_t addr, ctrl[2]; |
| 404 | uint32_t fcs_calc; |
| 405 | uint16_t nu = 0; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 406 | uint32_t oc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 407 | |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 408 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 409 | |
| 410 | /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */ |
| 411 | lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi); |
Holger Hans Peter Freyther | 964a9b3 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 412 | if (!lle) |
| 413 | lle = lle_by_tlli_sapi(tlli_foreign2local(msgb_tlli(msg)), sapi); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 414 | if (!lle) { |
| 415 | struct gprs_llc_llme *llme; |
Harald Welte | f0901f0 | 2010-12-26 10:39:26 +0100 | [diff] [blame] | 416 | LOGP(DLLC, LOGL_ERROR, "LLC TX: unknown TLLI 0x%08x, " |
| 417 | "creating LLME on the fly\n", msgb_tlli(msg)); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 418 | llme = llme_alloc(msgb_tlli(msg)); |
| 419 | lle = &llme->lle[sapi]; |
| 420 | } |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 421 | |
| 422 | if (msg->len > lle->params.n201_u) { |
| 423 | LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n", |
| 424 | msg->len, lle->params.n201_u); |
| 425 | return -EFBIG; |
| 426 | } |
| 427 | |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 428 | /* Update LLE's (BVCI, NSEI) tuple */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 429 | lle->llme->bvci = msgb_bvci(msg); |
| 430 | lle->llme->nsei = msgb_nsei(msg); |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 431 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 432 | /* Obtain current values for N(u) and OC */ |
Harald Welte | 6bdee6a | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 433 | nu = lle->vu_send; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 434 | oc = lle->oc_ui_send; |
| 435 | /* Increment V(U) */ |
Harald Welte | 6bdee6a | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 436 | lle->vu_send = (lle->vu_send + 1) % 512; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 437 | /* Increment Overflow Counter, if needed */ |
| 438 | if ((lle->vu_send + 1) / 512) |
| 439 | lle->oc_ui_send += 512; |
Harald Welte | 6bdee6a | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 440 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 441 | /* Address Field */ |
| 442 | addr = sapi & 0xf; |
| 443 | if (command) |
| 444 | addr |= 0x40; |
| 445 | |
| 446 | /* Control Field */ |
| 447 | ctrl[0] = 0xc0; |
| 448 | ctrl[0] |= nu >> 6; |
| 449 | ctrl[1] = (nu << 2) & 0xfc; |
| 450 | ctrl[1] |= 0x01; /* Protected Mode */ |
| 451 | |
| 452 | /* prepend LLC UI header */ |
| 453 | llch = msgb_push(msg, 3); |
| 454 | llch[0] = addr; |
| 455 | llch[1] = ctrl[0]; |
| 456 | llch[2] = ctrl[1]; |
| 457 | |
| 458 | /* append FCS to end of frame */ |
| 459 | fcs = msgb_put(msg, 3); |
| 460 | fcs_calc = gprs_llc_fcs(llch, fcs - llch); |
| 461 | fcs[0] = fcs_calc & 0xff; |
| 462 | fcs[1] = (fcs_calc >> 8) & 0xff; |
| 463 | fcs[2] = (fcs_calc >> 16) & 0xff; |
| 464 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 465 | /* encrypt information field + FCS, if needed! */ |
| 466 | if (lle->llme->algo != GPRS_ALGO_GEA0) { |
| 467 | uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */ |
| 468 | uint16_t crypt_len = (fcs + 3) - (llch + 3); |
| 469 | uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK]; |
| 470 | uint32_t iv; |
| 471 | int rc, i; |
| 472 | uint64_t kc = *(uint64_t *)&lle->llme->kc; |
| 473 | |
| 474 | /* Compute the 'Input' Paraemeter */ |
| 475 | iv = gprs_cipher_gen_input_ui(iov_ui, sapi, nu, oc); |
| 476 | |
| 477 | /* Compute the keystream that we need to XOR with the data */ |
| 478 | rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo, |
| 479 | kc, iv, GPRS_CIPH_SGSN2MS); |
| 480 | if (rc < 0) { |
| 481 | LOGP(DLLC, LOGL_ERROR, "Error crypting UI frame: %d\n", rc); |
| 482 | return rc; |
| 483 | } |
| 484 | |
| 485 | /* XOR the cipher output with the information field + FCS */ |
| 486 | for (i = 0; i < crypt_len; i++) |
| 487 | *(llch + 3 + i) ^= cipher_out[i]; |
| 488 | |
| 489 | /* Mark frame as encrypted */ |
| 490 | ctrl[1] |= 0x02; |
| 491 | } |
| 492 | |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 493 | /* Identifiers passed down: (BVCI, NSEI) */ |
| 494 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 495 | /* Send BSSGP-DL-UNITDATA.req */ |
Harald Welte | faa70ff | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 496 | return _bssgp_tx_dl_ud(msg, mmctx); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 497 | } |
| 498 | |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 499 | /* According to 6.4.1.6 / Figure 11 */ |
| 500 | static int msgb_put_xid_par(struct msgb *msg, uint8_t type, uint8_t length, uint8_t *data) |
| 501 | { |
| 502 | uint8_t header_len = 1; |
| 503 | uint8_t *cur; |
| 504 | |
| 505 | /* type is a 5-bit field... */ |
| 506 | if (type > 0x1f) |
| 507 | return -EINVAL; |
| 508 | |
| 509 | if (length > 3) |
| 510 | header_len = 2; |
| 511 | |
| 512 | cur = msgb_put(msg, length + header_len); |
| 513 | |
| 514 | /* build the header without or with XL bit */ |
| 515 | if (length <= 3) { |
| 516 | *cur++ = (type << 2) | (length & 3); |
| 517 | } else { |
| 518 | *cur++ = 0x80 | (type << 2) | (length >> 6); |
| 519 | *cur++ = (length << 2); |
| 520 | } |
| 521 | |
| 522 | /* copy over the payload of the parameter*/ |
| 523 | memcpy(cur, data, length); |
| 524 | |
| 525 | return length + header_len; |
| 526 | } |
| 527 | |
| 528 | static void rx_llc_xid(struct gprs_llc_lle *lle, |
| 529 | struct gprs_llc_hdr_parsed *gph) |
| 530 | { |
| 531 | /* FIXME: 8.5.3.3: check if XID is invalid */ |
| 532 | if (gph->is_cmd) { |
| 533 | /* FIXME: implement XID negotiation using SNDCP */ |
| 534 | struct msgb *resp; |
| 535 | uint8_t *xid; |
| 536 | resp = msgb_alloc_headroom(4096, 1024, "LLC_XID"); |
| 537 | xid = msgb_put(resp, gph->data_len); |
| 538 | memcpy(xid, gph->data, gph->data_len); |
| 539 | gprs_llc_tx_xid(lle, resp, 0); |
| 540 | } else { |
| 541 | /* FIXME: if we had sent a XID reset, send |
| 542 | * LLGMM-RESET.conf to GMM */ |
| 543 | /* FIXME: implement XID negotiation using SNDCP */ |
| 544 | } |
| 545 | } |
| 546 | |
Holger Hans Peter Freyther | 3a6fdcd | 2010-05-23 21:35:25 +0800 | [diff] [blame] | 547 | static void gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 548 | { |
Sylvain Munaut | 6f3850f | 2010-07-03 22:03:02 +0200 | [diff] [blame] | 549 | DEBUGP(DLLC, "LLC SAPI=%u %c %c FCS=0x%06x", |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 550 | gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ', |
Sylvain Munaut | 6f3850f | 2010-07-03 22:03:02 +0200 | [diff] [blame] | 551 | gph->fcs); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 552 | |
| 553 | if (gph->cmd) |
Harald Welte | b61f403 | 2010-05-18 12:31:50 +0200 | [diff] [blame] | 554 | DEBUGPC(DLLC, "CMD=%s ", get_value_string(llc_cmd_strs, gph->cmd)); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 555 | |
| 556 | if (gph->data) |
Harald Welte | c6ecafe | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 557 | DEBUGPC(DLLC, "DATA "); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 558 | |
Harald Welte | c6ecafe | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 559 | DEBUGPC(DLLC, "\n"); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 560 | } |
| 561 | static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph, |
| 562 | struct gprs_llc_lle *lle) |
| 563 | { |
| 564 | switch (gph->cmd) { |
| 565 | case GPRS_LLC_SABM: /* Section 6.4.1.1 */ |
| 566 | lle->v_sent = lle->v_ack = lle->v_recv = 0; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 567 | if (lle->state == GPRS_LLES_ASSIGNED_ADM) { |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 568 | /* start re-establishment (8.7.1) */ |
| 569 | } |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 570 | lle->state = GPRS_LLES_REMOTE_EST; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 571 | /* FIXME: Send UA */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 572 | lle->state = GPRS_LLES_ABM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 573 | /* FIXME: process data */ |
| 574 | break; |
| 575 | case GPRS_LLC_DISC: /* Section 6.4.1.2 */ |
| 576 | /* FIXME: Send UA */ |
| 577 | /* terminate ABM */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 578 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 579 | break; |
| 580 | case GPRS_LLC_UA: /* Section 6.4.1.3 */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 581 | if (lle->state == GPRS_LLES_LOCAL_EST) |
| 582 | lle->state = GPRS_LLES_ABM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 583 | break; |
| 584 | case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 585 | if (lle->state == GPRS_LLES_LOCAL_EST) |
| 586 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 587 | break; |
| 588 | case GPRS_LLC_FRMR: /* Section 6.4.1.5 */ |
| 589 | break; |
| 590 | case GPRS_LLC_XID: /* Section 6.4.1.6 */ |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 591 | rx_llc_xid(lle, gph); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 592 | break; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 593 | case GPRS_LLC_UI: |
Holger Hans Peter Freyther | faf1f64 | 2011-06-23 17:53:27 -0400 | [diff] [blame] | 594 | if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) { |
| 595 | LOGP(DLLC, LOGL_NOTICE, |
| 596 | "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n", |
Holger Hans Peter Freyther | 2788b96 | 2010-06-23 09:48:25 +0800 | [diff] [blame] | 597 | lle->llme ? lle->llme->tlli : -1, |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 598 | gph->seq_tx, lle->vu_recv); |
Harald Welte | abadd54 | 2013-06-21 14:06:18 +0200 | [diff] [blame] | 599 | |
| 600 | /* HACK: non-standard recovery handling. If remote LLE |
| 601 | * is re-transmitting the same sequence number for |
Harald Welte | 649e1ff | 2013-07-21 17:41:46 +0800 | [diff] [blame] | 602 | * three times, don't discard the frame but pass it on |
Harald Welte | abadd54 | 2013-06-21 14:06:18 +0200 | [diff] [blame] | 603 | * and 'learn' the new sequence number */ |
| 604 | if (gph->seq_tx != lle->vu_recv_last) { |
| 605 | lle->vu_recv_last = gph->seq_tx; |
| 606 | lle->vu_recv_duplicates = 0; |
| 607 | } else { |
| 608 | lle->vu_recv_duplicates++; |
| 609 | if (lle->vu_recv_duplicates < 3) |
| 610 | return -EIO; |
| 611 | LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering " |
| 612 | "N(U=%d) after receiving %u duplicates\n", |
| 613 | lle->llme ? lle->llme->tlli : -1, |
| 614 | gph->seq_tx, lle->vu_recv_duplicates); |
| 615 | } |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 616 | } |
| 617 | /* Increment the sequence number that we expect in the next frame */ |
| 618 | lle->vu_recv = (gph->seq_tx + 1) % 512; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 619 | /* Increment Overflow Counter */ |
| 620 | if ((gph->seq_tx + 1) / 512) |
| 621 | lle->oc_ui_recv += 512; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 622 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | /* parse a GPRS LLC header, also check for invalid frames */ |
| 629 | static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp, |
Holger Hans Peter Freyther | fa848d4 | 2010-05-23 21:43:57 +0800 | [diff] [blame] | 630 | uint8_t *llc_hdr, int len) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 631 | { |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 632 | uint8_t *ctrl = llc_hdr+1; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 633 | |
| 634 | if (len <= CRC24_LENGTH) |
| 635 | return -EIO; |
| 636 | |
Harald Welte | 1b8827a | 2010-06-30 23:15:57 +0200 | [diff] [blame] | 637 | ghp->crc_length = len - CRC24_LENGTH; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 638 | |
| 639 | ghp->ack_req = 0; |
| 640 | |
| 641 | /* Section 5.5: FCS */ |
| 642 | ghp->fcs = *(llc_hdr + len - 3); |
| 643 | ghp->fcs |= *(llc_hdr + len - 2) << 8; |
| 644 | ghp->fcs |= *(llc_hdr + len - 1) << 16; |
| 645 | |
| 646 | /* Section 6.2.1: invalid PD field */ |
| 647 | if (llc_hdr[0] & 0x80) |
| 648 | return -EIO; |
| 649 | |
| 650 | /* This only works for the MS->SGSN direction */ |
| 651 | if (llc_hdr[0] & 0x40) |
| 652 | ghp->is_cmd = 0; |
| 653 | else |
| 654 | ghp->is_cmd = 1; |
| 655 | |
| 656 | ghp->sapi = llc_hdr[0] & 0xf; |
| 657 | |
| 658 | /* Section 6.2.3: check for reserved SAPI */ |
| 659 | switch (ghp->sapi) { |
| 660 | case 0: |
| 661 | case 4: |
| 662 | case 6: |
| 663 | case 0xa: |
| 664 | case 0xc: |
| 665 | case 0xd: |
| 666 | case 0xf: |
| 667 | return -EINVAL; |
| 668 | } |
| 669 | |
| 670 | if ((ctrl[0] & 0x80) == 0) { |
| 671 | /* I (Information transfer + Supervisory) format */ |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 672 | uint8_t k; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 673 | |
| 674 | ghp->data = ctrl + 3; |
| 675 | |
| 676 | if (ctrl[0] & 0x40) |
| 677 | ghp->ack_req = 1; |
| 678 | |
| 679 | ghp->seq_tx = (ctrl[0] & 0x1f) << 4; |
| 680 | ghp->seq_tx |= (ctrl[1] >> 4); |
| 681 | |
| 682 | ghp->seq_rx = (ctrl[1] & 0x7) << 6; |
| 683 | ghp->seq_rx |= (ctrl[2] >> 2); |
| 684 | |
| 685 | switch (ctrl[2] & 0x03) { |
| 686 | case 0: |
| 687 | ghp->cmd = GPRS_LLC_RR; |
| 688 | break; |
| 689 | case 1: |
| 690 | ghp->cmd = GPRS_LLC_ACK; |
| 691 | break; |
| 692 | case 2: |
| 693 | ghp->cmd = GPRS_LLC_RNR; |
| 694 | break; |
| 695 | case 3: |
| 696 | ghp->cmd = GPRS_LLC_SACK; |
| 697 | k = ctrl[3] & 0x1f; |
| 698 | ghp->data += 1 + k; |
| 699 | break; |
| 700 | } |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 701 | ghp->data_len = (llc_hdr + len - 3) - ghp->data; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 702 | } else if ((ctrl[0] & 0xc0) == 0x80) { |
| 703 | /* S (Supervisory) format */ |
| 704 | ghp->data = NULL; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 705 | ghp->data_len = 0; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 706 | |
| 707 | if (ctrl[0] & 0x20) |
| 708 | ghp->ack_req = 1; |
| 709 | ghp->seq_rx = (ctrl[0] & 0x7) << 6; |
| 710 | ghp->seq_rx |= (ctrl[1] >> 2); |
| 711 | |
| 712 | switch (ctrl[1] & 0x03) { |
| 713 | case 0: |
| 714 | ghp->cmd = GPRS_LLC_RR; |
| 715 | break; |
| 716 | case 1: |
| 717 | ghp->cmd = GPRS_LLC_ACK; |
| 718 | break; |
| 719 | case 2: |
| 720 | ghp->cmd = GPRS_LLC_RNR; |
| 721 | break; |
| 722 | case 3: |
| 723 | ghp->cmd = GPRS_LLC_SACK; |
| 724 | break; |
| 725 | } |
| 726 | } else if ((ctrl[0] & 0xe0) == 0xc0) { |
| 727 | /* UI (Unconfirmed Inforamtion) format */ |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 728 | ghp->cmd = GPRS_LLC_UI; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 729 | ghp->data = ctrl + 2; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 730 | ghp->data_len = (llc_hdr + len - 3) - ghp->data; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 731 | |
| 732 | ghp->seq_tx = (ctrl[0] & 0x7) << 6; |
| 733 | ghp->seq_tx |= (ctrl[1] >> 2); |
| 734 | if (ctrl[1] & 0x02) { |
| 735 | ghp->is_encrypted = 1; |
| 736 | /* FIXME: encryption */ |
| 737 | } |
| 738 | if (ctrl[1] & 0x01) { |
| 739 | /* FCS over hdr + all inf fields */ |
| 740 | } else { |
| 741 | /* FCS over hdr + N202 octets (4) */ |
Harald Welte | 1b8827a | 2010-06-30 23:15:57 +0200 | [diff] [blame] | 742 | if (ghp->crc_length > UI_HDR_LEN + N202) |
| 743 | ghp->crc_length = UI_HDR_LEN + N202; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 744 | } |
| 745 | } else { |
| 746 | /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */ |
| 747 | ghp->data = NULL; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 748 | ghp->data_len = 0; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 749 | |
| 750 | switch (ctrl[0] & 0xf) { |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 751 | case GPRS_LLC_U_NULL_CMD: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 752 | ghp->cmd = GPRS_LLC_NULL; |
| 753 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 754 | case GPRS_LLC_U_DM_RESP: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 755 | ghp->cmd = GPRS_LLC_DM; |
| 756 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 757 | case GPRS_LLC_U_DISC_CMD: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 758 | ghp->cmd = GPRS_LLC_DISC; |
| 759 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 760 | case GPRS_LLC_U_UA_RESP: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 761 | ghp->cmd = GPRS_LLC_UA; |
| 762 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 763 | case GPRS_LLC_U_SABM_CMD: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 764 | ghp->cmd = GPRS_LLC_SABM; |
| 765 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 766 | case GPRS_LLC_U_FRMR_RESP: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 767 | ghp->cmd = GPRS_LLC_FRMR; |
| 768 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 769 | case GPRS_LLC_U_XID: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 770 | ghp->cmd = GPRS_LLC_XID; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 771 | ghp->data = ctrl + 1; |
| 772 | ghp->data_len = (llc_hdr + len - 3) - ghp->data; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 773 | break; |
| 774 | default: |
| 775 | return -EIO; |
| 776 | } |
| 777 | } |
| 778 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 779 | /* FIXME: parse sack frame */ |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 780 | if (ghp->cmd == GPRS_LLC_SACK) { |
Harald Welte | 1b170d1 | 2010-05-13 19:49:06 +0200 | [diff] [blame] | 781 | LOGP(DLLC, LOGL_NOTICE, "Unsupported SACK frame\n"); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 782 | return -EIO; |
| 783 | } |
| 784 | |
| 785 | return 0; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 786 | } |
| 787 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 788 | /* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 789 | int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv) |
| 790 | { |
Harald Welte | fd3fa1d | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 791 | struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); |
Holger Hans Peter Freyther | 3dccda5 | 2011-10-14 23:42:13 +0200 | [diff] [blame] | 792 | struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 793 | struct gprs_llc_hdr_parsed llhp; |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 794 | struct gprs_llc_lle *lle; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 795 | int rc = 0; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 796 | |
Harald Welte | 11d7c10 | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 797 | /* Identifiers from DOWN: NSEI, BVCI, TLLI */ |
| 798 | |
Holger Hans Peter Freyther | 4752e0c | 2010-05-23 21:33:57 +0800 | [diff] [blame] | 799 | memset(&llhp, 0, sizeof(llhp)); |
Holger Hans Peter Freyther | fa848d4 | 2010-05-23 21:43:57 +0800 | [diff] [blame] | 800 | rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU)); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 801 | gprs_llc_hdr_dump(&llhp); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 802 | if (rc < 0) { |
Harald Welte | 1b170d1 | 2010-05-13 19:49:06 +0200 | [diff] [blame] | 803 | LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n"); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 804 | return rc; |
| 805 | } |
| 806 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 807 | switch (gprs_tlli_type(msgb_tlli(msg))) { |
| 808 | case TLLI_LOCAL: |
| 809 | case TLLI_FOREIGN: |
| 810 | case TLLI_RANDOM: |
| 811 | case TLLI_AUXILIARY: |
| 812 | break; |
| 813 | default: |
| 814 | LOGP(DLLC, LOGL_ERROR, |
| 815 | "Discarding frame with strange TLLI type\n"); |
| 816 | break; |
| 817 | } |
| 818 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 819 | /* find the LLC Entity for this TLLI+SAPI tuple */ |
Holger Hans Peter Freyther | 964a9b3 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 820 | lle = lle_for_rx_by_tlli_sapi(msgb_tlli(msg), llhp.sapi, llhp.cmd); |
| 821 | if (!lle) |
| 822 | return 0; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 823 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 824 | /* decrypt information field + FCS, if needed! */ |
| 825 | if (llhp.is_encrypted) { |
| 826 | uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */ |
| 827 | uint16_t crypt_len = llhp.data_len + 3; |
| 828 | uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK]; |
| 829 | uint32_t iv; |
| 830 | uint64_t kc = *(uint64_t *)&lle->llme->kc; |
| 831 | int rc, i; |
| 832 | |
| 833 | if (lle->llme->algo == GPRS_ALGO_GEA0) { |
| 834 | LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that " |
| 835 | "has no KC/Algo! Dropping.\n"); |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | iv = gprs_cipher_gen_input_ui(iov_ui, lle->sapi, llhp.seq_tx, |
| 840 | lle->oc_ui_recv); |
| 841 | rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo, |
| 842 | kc, iv, GPRS_CIPH_MS2SGSN); |
| 843 | if (rc < 0) { |
| 844 | LOGP(DLLC, LOGL_ERROR, "Error decrypting frame: %d\n", |
| 845 | rc); |
| 846 | return rc; |
| 847 | } |
| 848 | |
| 849 | /* XOR the cipher output with the information field + FCS */ |
| 850 | for (i = 0; i < crypt_len; i++) |
| 851 | *(llhp.data + i) ^= cipher_out[i]; |
| 852 | } else { |
| 853 | if (lle->llme->algo != GPRS_ALGO_GEA0) { |
| 854 | LOGP(DLLC, LOGL_NOTICE, "unencrypted frame for LLC " |
| 855 | "that is supposed to be encrypted. Dropping.\n"); |
| 856 | return 0; |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | /* We have to do the FCS check _after_ decryption */ |
Harald Welte | 1b8827a | 2010-06-30 23:15:57 +0200 | [diff] [blame] | 861 | llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length); |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 862 | if (llhp.fcs != llhp.fcs_calc) { |
| 863 | LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n"); |
| 864 | return -EIO; |
| 865 | } |
| 866 | |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 867 | /* Update LLE's (BVCI, NSEI) tuple */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 868 | lle->llme->bvci = msgb_bvci(msg); |
| 869 | lle->llme->nsei = msgb_nsei(msg); |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 870 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 871 | /* Receive and Process the actual LLC frame */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 872 | rc = gprs_llc_hdr_rx(&llhp, lle); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 873 | if (rc < 0) |
| 874 | return rc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 875 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 876 | /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 877 | if (llhp.data) { |
Harald Welte | 943c5bc | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 878 | msgb_gmmh(msg) = llhp.data; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 879 | switch (llhp.sapi) { |
| 880 | case GPRS_SAPI_GMM: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 881 | /* send LL_UNITDATA_IND to GMM */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 882 | rc = gsm0408_gprs_rcvmsg(msg, lle->llme); |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 883 | break; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 884 | case GPRS_SAPI_SNDCP3: |
| 885 | case GPRS_SAPI_SNDCP5: |
| 886 | case GPRS_SAPI_SNDCP9: |
| 887 | case GPRS_SAPI_SNDCP11: |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 888 | /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */ |
| 889 | rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len); |
| 890 | break; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 891 | case GPRS_SAPI_SMS: |
| 892 | /* FIXME */ |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 893 | case GPRS_SAPI_TOM2: |
| 894 | case GPRS_SAPI_TOM8: |
| 895 | /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */ |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 896 | default: |
Harald Welte | c6ecafe | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 897 | LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi); |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 898 | rc = -EINVAL; |
| 899 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 903 | return rc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 904 | } |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 905 | |
| 906 | /* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */ |
| 907 | int gprs_llgmm_assign(struct gprs_llc_llme *llme, |
| 908 | uint32_t old_tlli, uint32_t new_tlli, |
| 909 | enum gprs_ciph_algo alg, const uint8_t *kc) |
| 910 | { |
| 911 | unsigned int i; |
| 912 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 913 | /* Update the crypto parameters */ |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 914 | llme->algo = alg; |
Harald Welte | 3e2e159 | 2010-06-30 23:19:23 +0200 | [diff] [blame] | 915 | if (alg != GPRS_ALGO_GEA0) |
| 916 | memcpy(llme->kc, kc, sizeof(llme->kc)); |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 917 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 918 | if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) { |
| 919 | /* TLLI Assignment 8.3.1 */ |
| 920 | /* New TLLI shall be assigned and used when (re)transmitting LLC frames */ |
| 921 | /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI |
| 922 | * old is unassigned. Only TLLI new shall be accepted when |
| 923 | * received from peer. */ |
Harald Welte | 875840c | 2010-07-01 11:54:31 +0200 | [diff] [blame] | 924 | if (llme->old_tlli != 0xffffffff) { |
| 925 | llme->old_tlli = 0xffffffff; |
| 926 | llme->tlli = new_tlli; |
| 927 | } else { |
| 928 | /* If TLLI old == 0xffffffff was assigned to LLME, then this is |
| 929 | * TLLI assignmemt according to 8.3.1 */ |
| 930 | llme->old_tlli = 0xffffffff; |
| 931 | llme->tlli = new_tlli; |
| 932 | llme->state = GPRS_LLMS_ASSIGNED; |
| 933 | /* 8.5.3.1 For all LLE's */ |
| 934 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) { |
| 935 | struct gprs_llc_lle *l = &llme->lle[i]; |
| 936 | l->vu_send = l->vu_recv = 0; |
| 937 | l->retrans_ctr = 0; |
| 938 | l->state = GPRS_LLES_ASSIGNED_ADM; |
| 939 | /* FIXME Set parameters according to table 9 */ |
| 940 | } |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 941 | } |
| 942 | } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) { |
| 943 | /* TLLI Change 8.3.2 */ |
| 944 | /* Both TLLI Old and TLLI New are assigned; use New when |
Holger Hans Peter Freyther | 92aa6bb | 2013-07-28 20:13:01 +0200 | [diff] [blame] | 945 | * (re)transmitting. Accept both Old and New on Rx */ |
Holger Hans Peter Freyther | aa93bac | 2013-07-31 11:20:37 +0200 | [diff] [blame] | 946 | llme->old_tlli = old_tlli; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 947 | llme->tlli = new_tlli; |
| 948 | llme->state = GPRS_LLMS_ASSIGNED; |
| 949 | } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) { |
| 950 | /* TLLI Unassignment 8.3.3) */ |
| 951 | llme->tlli = llme->old_tlli = 0; |
| 952 | llme->state = GPRS_LLMS_UNASSIGNED; |
| 953 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) { |
| 954 | struct gprs_llc_lle *l = &llme->lle[i]; |
| 955 | l->state = GPRS_LLES_UNASSIGNED; |
| 956 | } |
Harald Welte | f7fef48 | 2010-06-28 22:18:26 +0200 | [diff] [blame] | 957 | llme_free(llme); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 958 | } else |
| 959 | return -EINVAL; |
| 960 | |
| 961 | return 0; |
| 962 | } |
Harald Welte | 496aee4 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 963 | |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 964 | /* Chapter 7.2.1.2 LLGMM-RESET.req */ |
| 965 | int gprs_llgmm_reset(struct gprs_llc_llme *llme) |
| 966 | { |
| 967 | struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID"); |
| 968 | int random = rand(); |
| 969 | |
| 970 | /* First XID component must be RESET */ |
| 971 | msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL); |
| 972 | /* randomly select new IOV-UI */ |
Harald Welte | 066a0f5 | 2011-10-16 18:59:20 +0200 | [diff] [blame] | 973 | msgb_put_xid_par(msg, GPRS_LLC_XID_T_IOV_UI, 4, (uint8_t *) &random); |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 974 | |
| 975 | /* FIXME: Start T200, wait for XID response */ |
| 976 | return gprs_llc_tx_xid(&llme->lle[1], msg, 1); |
| 977 | } |
| 978 | |
Harald Welte | 496aee4 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 979 | int gprs_llc_init(const char *cipher_plugin_path) |
| 980 | { |
| 981 | return gprs_cipher_load(cipher_plugin_path); |
| 982 | } |