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