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 | static struct gprs_llc_llme *llme_alloc(uint32_t tlli); |
| 40 | |
Harald Welte | faa70ff | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 41 | /* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU |
| 42 | * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */ |
| 43 | static int _bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx) |
| 44 | { |
| 45 | struct bssgp_dl_ud_par dup; |
| 46 | const uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 }; |
| 47 | |
Harald Welte | 8c00496 | 2012-07-04 21:53:12 +0200 | [diff] [blame] | 48 | memset(&dup, 0, sizeof(dup)); |
| 49 | /* before we have received some identity from the MS, we might |
| 50 | * not yet have a MMC context (e.g. XID negotiation of primarly |
| 51 | * LLC connection fro GMM sapi). */ |
| 52 | if (mmctx) { |
| 53 | dup.imsi = mmctx->imsi; |
| 54 | dup.drx_parms = mmctx->drx_parms; |
| 55 | dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len; |
| 56 | 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] | 57 | |
| 58 | /* make sure we only send it to the right llme */ |
Harald Welte | f97ee04 | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 59 | OSMO_ASSERT(msgb_tlli(msg) == mmctx->gb.llme->tlli |
| 60 | || msgb_tlli(msg) == mmctx->gb.llme->old_tlli); |
Harald Welte | 8c00496 | 2012-07-04 21:53:12 +0200 | [diff] [blame] | 61 | } |
Harald Welte | faa70ff | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 62 | memcpy(&dup.qos_profile, qos_profile_default, |
| 63 | sizeof(qos_profile_default)); |
| 64 | |
Harald Welte | ce95b27 | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 65 | return bssgp_tx_dl_ud(msg, 1000, &dup); |
Harald Welte | faa70ff | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 69 | /* Section 8.9.9 LLC layer parameter default values */ |
Daniel Willmann | 46d1326 | 2014-06-27 17:05:48 +0200 | [diff] [blame] | 70 | static const struct gprs_llc_params llc_default_params[NUM_SAPIS] = { |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 71 | [1] = { |
| 72 | .t200_201 = 5, |
| 73 | .n200 = 3, |
| 74 | .n201_u = 400, |
| 75 | }, |
| 76 | [2] = { |
| 77 | .t200_201 = 5, |
| 78 | .n200 = 3, |
| 79 | .n201_u = 270, |
| 80 | }, |
| 81 | [3] = { |
| 82 | .iov_i_exp = 27, |
| 83 | .t200_201 = 5, |
| 84 | .n200 = 3, |
| 85 | .n201_u = 500, |
| 86 | .n201_i = 1503, |
| 87 | .mD = 1520, |
| 88 | .mU = 1520, |
| 89 | .kD = 16, |
| 90 | .kU = 16, |
| 91 | }, |
| 92 | [5] = { |
| 93 | .iov_i_exp = 27, |
| 94 | .t200_201 = 10, |
| 95 | .n200 = 3, |
| 96 | .n201_u = 500, |
| 97 | .n201_i = 1503, |
| 98 | .mD = 760, |
| 99 | .mU = 760, |
| 100 | .kD = 8, |
| 101 | .kU = 8, |
| 102 | }, |
| 103 | [7] = { |
| 104 | .t200_201 = 20, |
| 105 | .n200 = 3, |
| 106 | .n201_u = 270, |
| 107 | }, |
| 108 | [8] = { |
| 109 | .t200_201 = 20, |
| 110 | .n200 = 3, |
| 111 | .n201_u = 270, |
| 112 | }, |
| 113 | [9] = { |
| 114 | .iov_i_exp = 27, |
| 115 | .t200_201 = 20, |
| 116 | .n200 = 3, |
| 117 | .n201_u = 500, |
| 118 | .n201_i = 1503, |
| 119 | .mD = 380, |
| 120 | .mU = 380, |
| 121 | .kD = 4, |
| 122 | .kU = 4, |
| 123 | }, |
| 124 | [11] = { |
| 125 | .iov_i_exp = 27, |
| 126 | .t200_201 = 40, |
| 127 | .n200 = 3, |
| 128 | .n201_u = 500, |
| 129 | .n201_i = 1503, |
| 130 | .mD = 190, |
| 131 | .mU = 190, |
| 132 | .kD = 2, |
| 133 | .kU = 2, |
| 134 | }, |
| 135 | }; |
| 136 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 137 | LLIST_HEAD(gprs_llc_llmes); |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 138 | void *llc_tall_ctx; |
| 139 | |
| 140 | /* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */ |
Holger Hans Peter Freyther | 012a7ee | 2013-07-29 09:06:46 +0200 | [diff] [blame] | 141 | 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] | 142 | { |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 143 | struct gprs_llc_llme *llme; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 144 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 145 | llist_for_each_entry(llme, &gprs_llc_llmes, list) { |
| 146 | if (llme->tlli == tlli || llme->old_tlli == tlli) |
| 147 | return &llme->lle[sapi]; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 148 | } |
| 149 | return NULL; |
| 150 | } |
| 151 | |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 152 | struct gprs_llc_lle *gprs_lle_get_or_create(const uint32_t tlli, uint8_t sapi) |
| 153 | { |
| 154 | struct gprs_llc_llme *llme; |
| 155 | struct gprs_llc_lle *lle; |
| 156 | |
| 157 | lle = lle_by_tlli_sapi(tlli, sapi); |
| 158 | if (lle) |
| 159 | return lle; |
| 160 | |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 161 | LOGP(DLLC, LOGL_NOTICE, "LLC: unknown TLLI 0x%08x, " |
| 162 | "creating LLME on the fly\n", tlli); |
| 163 | llme = llme_alloc(tlli); |
| 164 | lle = &llme->lle[sapi]; |
| 165 | return lle; |
| 166 | } |
| 167 | |
| 168 | struct llist_head *gprs_llme_list(void) |
| 169 | { |
| 170 | return &gprs_llc_llmes; |
| 171 | } |
| 172 | |
Holger Hans Peter Freyther | 964a9b3 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 173 | /* lookup LLC Entity for RX based on DLCI (TLLI+SAPI tuple) */ |
| 174 | static struct gprs_llc_lle *lle_for_rx_by_tlli_sapi(const uint32_t tlli, |
| 175 | uint8_t sapi, enum gprs_llc_cmd cmd) |
| 176 | { |
| 177 | struct gprs_llc_lle *lle; |
| 178 | |
| 179 | /* We already know about this TLLI */ |
| 180 | lle = lle_by_tlli_sapi(tlli, sapi); |
| 181 | if (lle) |
| 182 | return lle; |
| 183 | |
| 184 | /* Maybe it is a routing area update but we already know this sapi? */ |
| 185 | if (gprs_tlli_type(tlli) == TLLI_FOREIGN) { |
Jacob Erlbeck | 3fbf0a3 | 2016-01-04 18:43:32 +0100 | [diff] [blame] | 186 | lle = lle_by_tlli_sapi(tlli, sapi); |
Holger Hans Peter Freyther | 964a9b3 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 187 | if (lle) { |
| 188 | LOGP(DLLC, LOGL_NOTICE, |
| 189 | "LLC RX: Found a local entry for TLLI 0x%08x\n", |
| 190 | tlli); |
| 191 | return lle; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded, |
| 196 | * except UID and XID frames with SAPI=1 */ |
| 197 | if (sapi == GPRS_SAPI_GMM && |
| 198 | (cmd == GPRS_LLC_XID || cmd == GPRS_LLC_UI)) { |
| 199 | struct gprs_llc_llme *llme; |
| 200 | /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */ |
| 201 | llme = llme_alloc(tlli); |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 202 | LOGP(DLLC, LOGL_NOTICE, "LLC RX: unknown TLLI 0x%08x, " |
Holger Hans Peter Freyther | 964a9b3 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 203 | "creating LLME on the fly\n", tlli); |
| 204 | lle = &llme->lle[sapi]; |
| 205 | return lle; |
| 206 | } |
| 207 | |
| 208 | LOGP(DLLC, LOGL_NOTICE, |
| 209 | "unknown TLLI(0x%08x)/SAPI(%d): Silently dropping\n", |
| 210 | tlli, sapi); |
| 211 | return NULL; |
| 212 | } |
| 213 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 214 | static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi) |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 215 | { |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 216 | struct gprs_llc_lle *lle = &llme->lle[sapi]; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 217 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 218 | lle->llme = llme; |
| 219 | lle->sapi = sapi; |
| 220 | lle->state = GPRS_LLES_UNASSIGNED; |
| 221 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 222 | /* Initialize according to parameters */ |
| 223 | memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params)); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static struct gprs_llc_llme *llme_alloc(uint32_t tlli) |
| 227 | { |
| 228 | struct gprs_llc_llme *llme; |
| 229 | uint32_t i; |
| 230 | |
| 231 | llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme); |
| 232 | if (!llme) |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 233 | return NULL; |
| 234 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 235 | llme->tlli = tlli; |
Harald Welte | 875840c | 2010-07-01 11:54:31 +0200 | [diff] [blame] | 236 | llme->old_tlli = 0xffffffff; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 237 | llme->state = GPRS_LLMS_UNASSIGNED; |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 238 | llme->age_timestamp = GPRS_LLME_RESET_AGE; |
Max | 5aa5196 | 2016-07-06 11:33:04 +0200 | [diff] [blame^] | 239 | llme->cksn = GSM_KEY_SEQ_INVAL; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 240 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 241 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) |
| 242 | lle_init(llme, i); |
| 243 | |
| 244 | llist_add(&llme->list, &gprs_llc_llmes); |
| 245 | |
| 246 | return llme; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Harald Welte | f7fef48 | 2010-06-28 22:18:26 +0200 | [diff] [blame] | 249 | static void llme_free(struct gprs_llc_llme *llme) |
| 250 | { |
| 251 | llist_del(&llme->list); |
| 252 | talloc_free(llme); |
| 253 | } |
| 254 | |
Holger Hans Peter Freyther | 744568b | 2014-04-04 12:47:32 +0200 | [diff] [blame] | 255 | #if 0 |
| 256 | /* FIXME: Unused code... */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 257 | static void t200_expired(void *data) |
| 258 | { |
| 259 | struct gprs_llc_lle *lle = data; |
| 260 | |
| 261 | /* 8.5.1.3: Expiry of T200 */ |
| 262 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 263 | if (lle->retrans_ctr >= lle->params.n200) { |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 264 | /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 265 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | switch (lle->state) { |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 269 | case GPRS_LLES_LOCAL_EST: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 270 | /* FIXME: retransmit SABM */ |
| 271 | /* FIXME: re-start T200 */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 272 | lle->retrans_ctr++; |
| 273 | break; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 274 | case GPRS_LLES_LOCAL_REL: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 275 | /* FIXME: retransmit DISC */ |
| 276 | /* FIXME: re-start T200 */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 277 | lle->retrans_ctr++; |
| 278 | break; |
Holger Hans Peter Freyther | 744568b | 2014-04-04 12:47:32 +0200 | [diff] [blame] | 279 | default: |
| 280 | LOGP(DLLC, LOGL_ERROR, "LLC unhandled state: %d\n", lle->state); |
| 281 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | } |
| 285 | |
| 286 | static void t201_expired(void *data) |
| 287 | { |
| 288 | struct gprs_llc_lle *lle = data; |
| 289 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 290 | if (lle->retrans_ctr < lle->params.n200) { |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 291 | /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */ |
| 292 | /* FIXME: set timer T201 */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 293 | lle->retrans_ctr++; |
| 294 | } |
| 295 | } |
Holger Hans Peter Freyther | 744568b | 2014-04-04 12:47:32 +0200 | [diff] [blame] | 296 | #endif |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 297 | |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 298 | int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command, |
| 299 | enum gprs_llc_u_cmd u_cmd, int pf_bit) |
| 300 | { |
| 301 | uint8_t *fcs, *llch; |
| 302 | uint8_t addr, ctrl; |
| 303 | uint32_t fcs_calc; |
| 304 | |
| 305 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 306 | |
| 307 | /* Address Field */ |
| 308 | addr = sapi & 0xf; |
| 309 | if (command) |
| 310 | addr |= 0x40; |
| 311 | |
| 312 | /* 6.3 Figure 8 */ |
| 313 | ctrl = 0xe0 | u_cmd; |
| 314 | if (pf_bit) |
| 315 | ctrl |= 0x10; |
| 316 | |
| 317 | /* prepend LLC UI header */ |
| 318 | llch = msgb_push(msg, 2); |
| 319 | llch[0] = addr; |
| 320 | llch[1] = ctrl; |
| 321 | |
| 322 | /* append FCS to end of frame */ |
| 323 | fcs = msgb_put(msg, 3); |
| 324 | fcs_calc = gprs_llc_fcs(llch, fcs - llch); |
| 325 | fcs[0] = fcs_calc & 0xff; |
| 326 | fcs[1] = (fcs_calc >> 8) & 0xff; |
| 327 | fcs[2] = (fcs_calc >> 16) & 0xff; |
| 328 | |
| 329 | /* Identifiers passed down: (BVCI, NSEI) */ |
| 330 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 331 | /* Send BSSGP-DL-UNITDATA.req */ |
Harald Welte | b1fd902 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 332 | return _bssgp_tx_dl_ud(msg, NULL); |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /* Send XID response to LLE */ |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 336 | static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg, |
| 337 | int command) |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 338 | { |
| 339 | /* copy identifiers from LLE to ensure lower layers can route */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 340 | msgb_tlli(msg) = lle->llme->tlli; |
| 341 | msgb_bvci(msg) = lle->llme->bvci; |
| 342 | msgb_nsei(msg) = lle->llme->nsei; |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 343 | |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 344 | 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] | 345 | } |
| 346 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 347 | /* Transmit a UI frame over the given SAPI */ |
Harald Welte | 56a0145 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 348 | int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command, |
| 349 | void *mmctx) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 350 | { |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 351 | struct gprs_llc_lle *lle; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 352 | uint8_t *fcs, *llch; |
| 353 | uint8_t addr, ctrl[2]; |
| 354 | uint32_t fcs_calc; |
| 355 | uint16_t nu = 0; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 356 | uint32_t oc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 357 | |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 358 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 359 | |
| 360 | /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */ |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 361 | lle = gprs_lle_get_or_create(msgb_tlli(msg), sapi); |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 362 | |
| 363 | if (msg->len > lle->params.n201_u) { |
| 364 | LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n", |
| 365 | msg->len, lle->params.n201_u); |
Holger Hans Peter Freyther | f9ffd1f | 2014-10-10 17:35:54 +0200 | [diff] [blame] | 366 | msgb_free(msg); |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 367 | return -EFBIG; |
| 368 | } |
| 369 | |
Max | 5aa5196 | 2016-07-06 11:33:04 +0200 | [diff] [blame^] | 370 | gprs_llme_copy_key(mmctx, lle->llme); |
| 371 | |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 372 | /* Update LLE's (BVCI, NSEI) tuple */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 373 | lle->llme->bvci = msgb_bvci(msg); |
| 374 | lle->llme->nsei = msgb_nsei(msg); |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 375 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 376 | /* Obtain current values for N(u) and OC */ |
Harald Welte | 6bdee6a | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 377 | nu = lle->vu_send; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 378 | oc = lle->oc_ui_send; |
| 379 | /* Increment V(U) */ |
Harald Welte | 6bdee6a | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 380 | lle->vu_send = (lle->vu_send + 1) % 512; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 381 | /* Increment Overflow Counter, if needed */ |
| 382 | if ((lle->vu_send + 1) / 512) |
| 383 | lle->oc_ui_send += 512; |
Harald Welte | 6bdee6a | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 384 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 385 | /* Address Field */ |
| 386 | addr = sapi & 0xf; |
| 387 | if (command) |
| 388 | addr |= 0x40; |
| 389 | |
| 390 | /* Control Field */ |
| 391 | ctrl[0] = 0xc0; |
| 392 | ctrl[0] |= nu >> 6; |
| 393 | ctrl[1] = (nu << 2) & 0xfc; |
| 394 | ctrl[1] |= 0x01; /* Protected Mode */ |
| 395 | |
| 396 | /* prepend LLC UI header */ |
| 397 | llch = msgb_push(msg, 3); |
| 398 | llch[0] = addr; |
| 399 | llch[1] = ctrl[0]; |
| 400 | llch[2] = ctrl[1]; |
| 401 | |
| 402 | /* append FCS to end of frame */ |
| 403 | fcs = msgb_put(msg, 3); |
| 404 | fcs_calc = gprs_llc_fcs(llch, fcs - llch); |
| 405 | fcs[0] = fcs_calc & 0xff; |
| 406 | fcs[1] = (fcs_calc >> 8) & 0xff; |
| 407 | fcs[2] = (fcs_calc >> 16) & 0xff; |
| 408 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 409 | /* encrypt information field + FCS, if needed! */ |
| 410 | if (lle->llme->algo != GPRS_ALGO_GEA0) { |
| 411 | uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */ |
| 412 | uint16_t crypt_len = (fcs + 3) - (llch + 3); |
| 413 | uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK]; |
| 414 | uint32_t iv; |
| 415 | int rc, i; |
Alexander Couzens | 85f8fda | 2016-05-31 17:37:54 +0200 | [diff] [blame] | 416 | uint8_t *kc = lle->llme->kc; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 417 | |
| 418 | /* Compute the 'Input' Paraemeter */ |
| 419 | iv = gprs_cipher_gen_input_ui(iov_ui, sapi, nu, oc); |
| 420 | |
| 421 | /* Compute the keystream that we need to XOR with the data */ |
| 422 | rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo, |
| 423 | kc, iv, GPRS_CIPH_SGSN2MS); |
| 424 | if (rc < 0) { |
| 425 | LOGP(DLLC, LOGL_ERROR, "Error crypting UI frame: %d\n", rc); |
Holger Hans Peter Freyther | f9ffd1f | 2014-10-10 17:35:54 +0200 | [diff] [blame] | 426 | msgb_free(msg); |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 427 | return rc; |
| 428 | } |
| 429 | |
| 430 | /* XOR the cipher output with the information field + FCS */ |
| 431 | for (i = 0; i < crypt_len; i++) |
| 432 | *(llch + 3 + i) ^= cipher_out[i]; |
| 433 | |
| 434 | /* Mark frame as encrypted */ |
| 435 | ctrl[1] |= 0x02; |
| 436 | } |
| 437 | |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 438 | /* Identifiers passed down: (BVCI, NSEI) */ |
| 439 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 440 | /* Send BSSGP-DL-UNITDATA.req */ |
Harald Welte | faa70ff | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 441 | return _bssgp_tx_dl_ud(msg, mmctx); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 442 | } |
| 443 | |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 444 | /* According to 6.4.1.6 / Figure 11 */ |
| 445 | static int msgb_put_xid_par(struct msgb *msg, uint8_t type, uint8_t length, uint8_t *data) |
| 446 | { |
| 447 | uint8_t header_len = 1; |
| 448 | uint8_t *cur; |
| 449 | |
| 450 | /* type is a 5-bit field... */ |
| 451 | if (type > 0x1f) |
| 452 | return -EINVAL; |
| 453 | |
| 454 | if (length > 3) |
| 455 | header_len = 2; |
| 456 | |
| 457 | cur = msgb_put(msg, length + header_len); |
| 458 | |
| 459 | /* build the header without or with XL bit */ |
| 460 | if (length <= 3) { |
| 461 | *cur++ = (type << 2) | (length & 3); |
| 462 | } else { |
| 463 | *cur++ = 0x80 | (type << 2) | (length >> 6); |
| 464 | *cur++ = (length << 2); |
| 465 | } |
| 466 | |
| 467 | /* copy over the payload of the parameter*/ |
| 468 | memcpy(cur, data, length); |
| 469 | |
| 470 | return length + header_len; |
| 471 | } |
| 472 | |
| 473 | static void rx_llc_xid(struct gprs_llc_lle *lle, |
| 474 | struct gprs_llc_hdr_parsed *gph) |
| 475 | { |
| 476 | /* FIXME: 8.5.3.3: check if XID is invalid */ |
| 477 | if (gph->is_cmd) { |
| 478 | /* FIXME: implement XID negotiation using SNDCP */ |
| 479 | struct msgb *resp; |
| 480 | uint8_t *xid; |
| 481 | resp = msgb_alloc_headroom(4096, 1024, "LLC_XID"); |
| 482 | xid = msgb_put(resp, gph->data_len); |
| 483 | memcpy(xid, gph->data, gph->data_len); |
| 484 | gprs_llc_tx_xid(lle, resp, 0); |
| 485 | } else { |
| 486 | /* FIXME: if we had sent a XID reset, send |
| 487 | * LLGMM-RESET.conf to GMM */ |
| 488 | /* FIXME: implement XID negotiation using SNDCP */ |
| 489 | } |
| 490 | } |
| 491 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 492 | static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph, |
| 493 | struct gprs_llc_lle *lle) |
| 494 | { |
| 495 | switch (gph->cmd) { |
| 496 | case GPRS_LLC_SABM: /* Section 6.4.1.1 */ |
| 497 | lle->v_sent = lle->v_ack = lle->v_recv = 0; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 498 | if (lle->state == GPRS_LLES_ASSIGNED_ADM) { |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 499 | /* start re-establishment (8.7.1) */ |
| 500 | } |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 501 | lle->state = GPRS_LLES_REMOTE_EST; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 502 | /* FIXME: Send UA */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 503 | lle->state = GPRS_LLES_ABM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 504 | /* FIXME: process data */ |
| 505 | break; |
| 506 | case GPRS_LLC_DISC: /* Section 6.4.1.2 */ |
| 507 | /* FIXME: Send UA */ |
| 508 | /* terminate ABM */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 509 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 510 | break; |
| 511 | case GPRS_LLC_UA: /* Section 6.4.1.3 */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 512 | if (lle->state == GPRS_LLES_LOCAL_EST) |
| 513 | lle->state = GPRS_LLES_ABM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 514 | break; |
| 515 | 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] | 516 | if (lle->state == GPRS_LLES_LOCAL_EST) |
| 517 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 518 | break; |
| 519 | case GPRS_LLC_FRMR: /* Section 6.4.1.5 */ |
| 520 | break; |
| 521 | case GPRS_LLC_XID: /* Section 6.4.1.6 */ |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 522 | rx_llc_xid(lle, gph); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 523 | break; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 524 | case GPRS_LLC_UI: |
Holger Hans Peter Freyther | faf1f64 | 2011-06-23 17:53:27 -0400 | [diff] [blame] | 525 | if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) { |
| 526 | LOGP(DLLC, LOGL_NOTICE, |
| 527 | "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] | 528 | lle->llme ? lle->llme->tlli : -1, |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 529 | gph->seq_tx, lle->vu_recv); |
Harald Welte | abadd54 | 2013-06-21 14:06:18 +0200 | [diff] [blame] | 530 | |
| 531 | /* HACK: non-standard recovery handling. If remote LLE |
| 532 | * is re-transmitting the same sequence number for |
Harald Welte | 649e1ff | 2013-07-21 17:41:46 +0800 | [diff] [blame] | 533 | * three times, don't discard the frame but pass it on |
Harald Welte | abadd54 | 2013-06-21 14:06:18 +0200 | [diff] [blame] | 534 | * and 'learn' the new sequence number */ |
| 535 | if (gph->seq_tx != lle->vu_recv_last) { |
| 536 | lle->vu_recv_last = gph->seq_tx; |
| 537 | lle->vu_recv_duplicates = 0; |
| 538 | } else { |
| 539 | lle->vu_recv_duplicates++; |
| 540 | if (lle->vu_recv_duplicates < 3) |
| 541 | return -EIO; |
| 542 | LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering " |
| 543 | "N(U=%d) after receiving %u duplicates\n", |
| 544 | lle->llme ? lle->llme->tlli : -1, |
| 545 | gph->seq_tx, lle->vu_recv_duplicates); |
| 546 | } |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 547 | } |
| 548 | /* Increment the sequence number that we expect in the next frame */ |
| 549 | lle->vu_recv = (gph->seq_tx + 1) % 512; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 550 | /* Increment Overflow Counter */ |
| 551 | if ((gph->seq_tx + 1) / 512) |
| 552 | lle->oc_ui_recv += 512; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 553 | break; |
Holger Hans Peter Freyther | 744568b | 2014-04-04 12:47:32 +0200 | [diff] [blame] | 554 | default: |
| 555 | LOGP(DLLC, LOGL_NOTICE, "Unhandled command: %d\n", gph->cmd); |
| 556 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 562 | /* 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] | 563 | int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv) |
| 564 | { |
Holger Hans Peter Freyther | 3dccda5 | 2011-10-14 23:42:13 +0200 | [diff] [blame] | 565 | struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 566 | struct gprs_llc_hdr_parsed llhp; |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 567 | struct gprs_llc_lle *lle; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 568 | int rc = 0; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 569 | |
Harald Welte | 11d7c10 | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 570 | /* Identifiers from DOWN: NSEI, BVCI, TLLI */ |
| 571 | |
Holger Hans Peter Freyther | 4752e0c | 2010-05-23 21:33:57 +0800 | [diff] [blame] | 572 | memset(&llhp, 0, sizeof(llhp)); |
Holger Hans Peter Freyther | fa848d4 | 2010-05-23 21:43:57 +0800 | [diff] [blame] | 573 | 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] | 574 | gprs_llc_hdr_dump(&llhp); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 575 | if (rc < 0) { |
Harald Welte | 1b170d1 | 2010-05-13 19:49:06 +0200 | [diff] [blame] | 576 | LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n"); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 577 | return rc; |
| 578 | } |
| 579 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 580 | switch (gprs_tlli_type(msgb_tlli(msg))) { |
| 581 | case TLLI_LOCAL: |
| 582 | case TLLI_FOREIGN: |
| 583 | case TLLI_RANDOM: |
| 584 | case TLLI_AUXILIARY: |
| 585 | break; |
| 586 | default: |
| 587 | LOGP(DLLC, LOGL_ERROR, |
| 588 | "Discarding frame with strange TLLI type\n"); |
| 589 | break; |
| 590 | } |
| 591 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 592 | /* find the LLC Entity for this TLLI+SAPI tuple */ |
Holger Hans Peter Freyther | 964a9b3 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 593 | lle = lle_for_rx_by_tlli_sapi(msgb_tlli(msg), llhp.sapi, llhp.cmd); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 594 | if (!lle) { |
| 595 | switch (llhp.sapi) { |
| 596 | case GPRS_SAPI_SNDCP3: |
| 597 | case GPRS_SAPI_SNDCP5: |
| 598 | case GPRS_SAPI_SNDCP9: |
| 599 | case GPRS_SAPI_SNDCP11: |
| 600 | /* Ask an upper layer for help. */ |
| 601 | return sgsn_force_reattach_oldmsg(msg); |
| 602 | default: |
| 603 | break; |
| 604 | } |
Holger Hans Peter Freyther | 964a9b3 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 605 | return 0; |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 606 | } |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 607 | |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 608 | /* reset age computation */ |
| 609 | lle->llme->age_timestamp = GPRS_LLME_RESET_AGE; |
| 610 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 611 | /* decrypt information field + FCS, if needed! */ |
| 612 | if (llhp.is_encrypted) { |
| 613 | uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */ |
| 614 | uint16_t crypt_len = llhp.data_len + 3; |
| 615 | uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK]; |
| 616 | uint32_t iv; |
Alexander Couzens | 85f8fda | 2016-05-31 17:37:54 +0200 | [diff] [blame] | 617 | uint8_t *kc = lle->llme->kc; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 618 | int rc, i; |
| 619 | |
| 620 | if (lle->llme->algo == GPRS_ALGO_GEA0) { |
| 621 | LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that " |
| 622 | "has no KC/Algo! Dropping.\n"); |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | iv = gprs_cipher_gen_input_ui(iov_ui, lle->sapi, llhp.seq_tx, |
| 627 | lle->oc_ui_recv); |
| 628 | rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo, |
| 629 | kc, iv, GPRS_CIPH_MS2SGSN); |
| 630 | if (rc < 0) { |
| 631 | LOGP(DLLC, LOGL_ERROR, "Error decrypting frame: %d\n", |
| 632 | rc); |
| 633 | return rc; |
| 634 | } |
| 635 | |
| 636 | /* XOR the cipher output with the information field + FCS */ |
| 637 | for (i = 0; i < crypt_len; i++) |
| 638 | *(llhp.data + i) ^= cipher_out[i]; |
| 639 | } else { |
| 640 | if (lle->llme->algo != GPRS_ALGO_GEA0) { |
| 641 | LOGP(DLLC, LOGL_NOTICE, "unencrypted frame for LLC " |
| 642 | "that is supposed to be encrypted. Dropping.\n"); |
| 643 | return 0; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | /* We have to do the FCS check _after_ decryption */ |
Harald Welte | 1b8827a | 2010-06-30 23:15:57 +0200 | [diff] [blame] | 648 | llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length); |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 649 | if (llhp.fcs != llhp.fcs_calc) { |
| 650 | LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n"); |
| 651 | return -EIO; |
| 652 | } |
| 653 | |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 654 | /* Update LLE's (BVCI, NSEI) tuple */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 655 | lle->llme->bvci = msgb_bvci(msg); |
| 656 | lle->llme->nsei = msgb_nsei(msg); |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 657 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 658 | /* Receive and Process the actual LLC frame */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 659 | rc = gprs_llc_hdr_rx(&llhp, lle); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 660 | if (rc < 0) |
| 661 | return rc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 662 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 663 | /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */ |
Harald Welte | 22df4ac | 2015-08-16 15:23:32 +0200 | [diff] [blame] | 664 | if (llhp.cmd == GPRS_LLC_UI && llhp.data && llhp.data_len) { |
Harald Welte | 943c5bc | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 665 | msgb_gmmh(msg) = llhp.data; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 666 | switch (llhp.sapi) { |
| 667 | case GPRS_SAPI_GMM: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 668 | /* send LL_UNITDATA_IND to GMM */ |
Harald Welte | 49393e1 | 2015-12-25 19:14:57 +0100 | [diff] [blame] | 669 | rc = gsm0408_gprs_rcvmsg_gb(msg, lle->llme); |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 670 | break; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 671 | case GPRS_SAPI_SNDCP3: |
| 672 | case GPRS_SAPI_SNDCP5: |
| 673 | case GPRS_SAPI_SNDCP9: |
| 674 | case GPRS_SAPI_SNDCP11: |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 675 | /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */ |
| 676 | rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len); |
| 677 | break; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 678 | case GPRS_SAPI_SMS: |
| 679 | /* FIXME */ |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 680 | case GPRS_SAPI_TOM2: |
| 681 | case GPRS_SAPI_TOM8: |
| 682 | /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */ |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 683 | default: |
Harald Welte | c6ecafe | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 684 | LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi); |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 685 | rc = -EINVAL; |
| 686 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 690 | return rc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 691 | } |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 692 | |
Max | 5aa5196 | 2016-07-06 11:33:04 +0200 | [diff] [blame^] | 693 | /* Propagate crypto parameters MM -> LLME */ |
| 694 | void gprs_llme_copy_key(struct sgsn_mm_ctx *mm, struct gprs_llc_llme *llme) |
| 695 | { |
| 696 | if (!mm) |
| 697 | return; |
| 698 | if (mm->ciph_algo != GPRS_ALGO_GEA0) { |
| 699 | llme->algo = mm->ciph_algo; |
| 700 | if (llme->cksn != mm->auth_triplet.key_seq && |
| 701 | mm->auth_triplet.key_seq != GSM_KEY_SEQ_INVAL) { |
| 702 | memcpy(llme->kc, mm->auth_triplet.vec.kc, |
| 703 | gprs_cipher_key_length(mm->ciph_algo)); |
| 704 | llme->cksn = mm->auth_triplet.key_seq; |
| 705 | } |
| 706 | } else |
| 707 | llme->cksn = GSM_KEY_SEQ_INVAL; |
| 708 | } |
| 709 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 710 | /* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */ |
| 711 | int gprs_llgmm_assign(struct gprs_llc_llme *llme, |
Max | 5aa5196 | 2016-07-06 11:33:04 +0200 | [diff] [blame^] | 712 | uint32_t old_tlli, uint32_t new_tlli) |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 713 | { |
| 714 | unsigned int i; |
| 715 | |
| 716 | if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) { |
| 717 | /* TLLI Assignment 8.3.1 */ |
| 718 | /* New TLLI shall be assigned and used when (re)transmitting LLC frames */ |
| 719 | /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI |
| 720 | * old is unassigned. Only TLLI new shall be accepted when |
| 721 | * received from peer. */ |
Harald Welte | 875840c | 2010-07-01 11:54:31 +0200 | [diff] [blame] | 722 | if (llme->old_tlli != 0xffffffff) { |
| 723 | llme->old_tlli = 0xffffffff; |
| 724 | llme->tlli = new_tlli; |
| 725 | } else { |
| 726 | /* If TLLI old == 0xffffffff was assigned to LLME, then this is |
| 727 | * TLLI assignmemt according to 8.3.1 */ |
| 728 | llme->old_tlli = 0xffffffff; |
| 729 | llme->tlli = new_tlli; |
| 730 | llme->state = GPRS_LLMS_ASSIGNED; |
| 731 | /* 8.5.3.1 For all LLE's */ |
| 732 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) { |
| 733 | struct gprs_llc_lle *l = &llme->lle[i]; |
| 734 | l->vu_send = l->vu_recv = 0; |
| 735 | l->retrans_ctr = 0; |
| 736 | l->state = GPRS_LLES_ASSIGNED_ADM; |
| 737 | /* FIXME Set parameters according to table 9 */ |
| 738 | } |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 739 | } |
| 740 | } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) { |
| 741 | /* TLLI Change 8.3.2 */ |
| 742 | /* 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] | 743 | * (re)transmitting. Accept both Old and New on Rx */ |
Holger Hans Peter Freyther | aa93bac | 2013-07-31 11:20:37 +0200 | [diff] [blame] | 744 | llme->old_tlli = old_tlli; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 745 | llme->tlli = new_tlli; |
| 746 | llme->state = GPRS_LLMS_ASSIGNED; |
| 747 | } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) { |
| 748 | /* TLLI Unassignment 8.3.3) */ |
| 749 | llme->tlli = llme->old_tlli = 0; |
| 750 | llme->state = GPRS_LLMS_UNASSIGNED; |
| 751 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) { |
| 752 | struct gprs_llc_lle *l = &llme->lle[i]; |
| 753 | l->state = GPRS_LLES_UNASSIGNED; |
| 754 | } |
Harald Welte | f7fef48 | 2010-06-28 22:18:26 +0200 | [diff] [blame] | 755 | llme_free(llme); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 756 | } else |
| 757 | return -EINVAL; |
| 758 | |
| 759 | return 0; |
| 760 | } |
Harald Welte | 496aee4 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 761 | |
Max | 3955025 | 2016-06-28 17:39:20 +0200 | [diff] [blame] | 762 | /* TLLI unassignment */ |
| 763 | int gprs_llgmm_unassign(struct gprs_llc_llme *llme) |
| 764 | { |
Max | 5aa5196 | 2016-07-06 11:33:04 +0200 | [diff] [blame^] | 765 | return gprs_llgmm_assign(llme, llme->tlli, 0xffffffff); |
Max | 3955025 | 2016-06-28 17:39:20 +0200 | [diff] [blame] | 766 | } |
| 767 | |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 768 | /* Chapter 7.2.1.2 LLGMM-RESET.req */ |
| 769 | int gprs_llgmm_reset(struct gprs_llc_llme *llme) |
| 770 | { |
| 771 | struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID"); |
| 772 | int random = rand(); |
Jacob Erlbeck | 25ad52c | 2014-09-11 14:20:53 +0200 | [diff] [blame] | 773 | struct gprs_llc_lle *lle = &llme->lle[1]; |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 774 | |
| 775 | /* First XID component must be RESET */ |
| 776 | msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL); |
| 777 | /* randomly select new IOV-UI */ |
Harald Welte | 066a0f5 | 2011-10-16 18:59:20 +0200 | [diff] [blame] | 778 | 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] | 779 | |
Jacob Erlbeck | 25ad52c | 2014-09-11 14:20:53 +0200 | [diff] [blame] | 780 | /* Reset some of the LLC parameters. See GSM 04.64, 8.5.3.1 */ |
| 781 | lle->vu_recv = 0; |
| 782 | lle->vu_send = 0; |
| 783 | lle->oc_ui_send = 0; |
| 784 | lle->oc_ui_recv = 0; |
| 785 | |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 786 | /* FIXME: Start T200, wait for XID response */ |
Jacob Erlbeck | 25ad52c | 2014-09-11 14:20:53 +0200 | [diff] [blame] | 787 | return gprs_llc_tx_xid(lle, msg, 1); |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 788 | } |
| 789 | |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 790 | int gprs_llgmm_reset_oldmsg(struct msgb* oldmsg, uint8_t sapi) |
| 791 | { |
| 792 | struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID"); |
| 793 | int random = rand(); |
| 794 | |
| 795 | /* First XID component must be RESET */ |
| 796 | msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL); |
| 797 | /* randomly select new IOV-UI */ |
| 798 | msgb_put_xid_par(msg, GPRS_LLC_XID_T_IOV_UI, 4, (uint8_t *) &random); |
| 799 | |
| 800 | /* FIXME: Start T200, wait for XID response */ |
| 801 | |
| 802 | msgb_tlli(msg) = msgb_tlli(oldmsg); |
| 803 | msgb_bvci(msg) = msgb_bvci(oldmsg); |
| 804 | msgb_nsei(msg) = msgb_nsei(oldmsg); |
| 805 | |
| 806 | return gprs_llc_tx_u(msg, sapi, 1, GPRS_LLC_U_XID, 1); |
| 807 | } |
| 808 | |
Harald Welte | 496aee4 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 809 | int gprs_llc_init(const char *cipher_plugin_path) |
| 810 | { |
| 811 | return gprs_cipher_load(cipher_plugin_path); |
| 812 | } |