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