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