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