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