Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1 | /* GPRS LLC protocol implementation as per 3GPP TS 04.64 */ |
| 2 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 3 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 16 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <errno.h> |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 23 | #include <stdint.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 24 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 25 | #include <osmocom/core/msgb.h> |
| 26 | #include <osmocom/core/linuxlist.h> |
| 27 | #include <osmocom/core/timer.h> |
| 28 | #include <osmocom/core/talloc.h> |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 29 | #include <osmocom/gprs/gprs_bssgp.h> |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 30 | |
| 31 | #include <openbsc/gsm_data.h> |
| 32 | #include <openbsc/debug.h> |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 33 | #include <openbsc/gprs_sgsn.h> |
| 34 | #include <openbsc/gprs_gmm.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 35 | #include <openbsc/gprs_llc.h> |
| 36 | #include <openbsc/crc24.h> |
Holger Hans Peter Freyther | 3dccda5 | 2011-10-14 23:42:13 +0200 | [diff] [blame] | 37 | #include <openbsc/sgsn.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 38 | |
Holger Hans Peter Freyther | 964a9b3 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 39 | static struct gprs_llc_llme *llme_alloc(uint32_t tlli); |
| 40 | |
Holger Hans Peter Freyther | 7e0fec1 | 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; |
| 48 | DEBUGP(DLLC, "TLLI 0x%08x is foreign, converting to " |
| 49 | "local TLLI 0x%08x\n", tlli, new_tlli); |
| 50 | } else |
| 51 | new_tlli = tlli; |
| 52 | |
| 53 | return new_tlli; |
| 54 | } |
| 55 | |
Harald Welte | faa70ff | 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 | 8c00496 | 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 | 7e0fec1 | 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 | 8c00496 | 2012-07-04 21:53:12 +0200 | [diff] [blame] | 78 | } |
Harald Welte | faa70ff | 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 | ce95b27 | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 82 | return bssgp_tx_dl_ud(msg, 1000, &dup); |
Harald Welte | faa70ff | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 86 | /* Section 8.9.9 LLC layer parameter default values */ |
Daniel Willmann | 46d1326 | 2014-06-27 17:05:48 +0200 | [diff] [blame] | 87 | static const struct gprs_llc_params llc_default_params[NUM_SAPIS] = { |
Harald Welte | 1d9d944 | 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 | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 154 | LLIST_HEAD(gprs_llc_llmes); |
Harald Welte | a266554 | 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 | 012a7ee | 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 | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 159 | { |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 160 | struct gprs_llc_llme *llme; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 161 | |
Harald Welte | 807a5d8 | 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 | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 165 | } |
| 166 | return NULL; |
| 167 | } |
| 168 | |
Holger Hans Peter Freyther | 964a9b3 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 169 | /* lookup LLC Entity for RX based on DLCI (TLLI+SAPI tuple) */ |
| 170 | static struct gprs_llc_lle *lle_for_rx_by_tlli_sapi(const uint32_t tlli, |
| 171 | uint8_t sapi, enum gprs_llc_cmd cmd) |
| 172 | { |
| 173 | struct gprs_llc_lle *lle; |
| 174 | |
| 175 | /* We already know about this TLLI */ |
| 176 | lle = lle_by_tlli_sapi(tlli, sapi); |
| 177 | if (lle) |
| 178 | return lle; |
| 179 | |
| 180 | /* Maybe it is a routing area update but we already know this sapi? */ |
| 181 | if (gprs_tlli_type(tlli) == TLLI_FOREIGN) { |
| 182 | lle = lle_by_tlli_sapi(tlli_foreign2local(tlli), sapi); |
| 183 | if (lle) { |
| 184 | LOGP(DLLC, LOGL_NOTICE, |
| 185 | "LLC RX: Found a local entry for TLLI 0x%08x\n", |
| 186 | tlli); |
| 187 | return lle; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded, |
| 192 | * except UID and XID frames with SAPI=1 */ |
| 193 | if (sapi == GPRS_SAPI_GMM && |
| 194 | (cmd == GPRS_LLC_XID || cmd == GPRS_LLC_UI)) { |
| 195 | struct gprs_llc_llme *llme; |
| 196 | /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */ |
| 197 | llme = llme_alloc(tlli); |
| 198 | LOGP(DLLC, LOGL_DEBUG, "LLC RX: unknown TLLI 0x%08x, " |
| 199 | "creating LLME on the fly\n", tlli); |
| 200 | lle = &llme->lle[sapi]; |
| 201 | return lle; |
| 202 | } |
| 203 | |
| 204 | LOGP(DLLC, LOGL_NOTICE, |
| 205 | "unknown TLLI(0x%08x)/SAPI(%d): Silently dropping\n", |
| 206 | tlli, sapi); |
| 207 | return NULL; |
| 208 | } |
| 209 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 210 | static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi) |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 211 | { |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 212 | struct gprs_llc_lle *lle = &llme->lle[sapi]; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 213 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 214 | lle->llme = llme; |
| 215 | lle->sapi = sapi; |
| 216 | lle->state = GPRS_LLES_UNASSIGNED; |
| 217 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 218 | /* Initialize according to parameters */ |
| 219 | memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params)); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static struct gprs_llc_llme *llme_alloc(uint32_t tlli) |
| 223 | { |
| 224 | struct gprs_llc_llme *llme; |
| 225 | uint32_t i; |
| 226 | |
| 227 | llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme); |
| 228 | if (!llme) |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 229 | return NULL; |
| 230 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 231 | llme->tlli = tlli; |
Harald Welte | 875840c | 2010-07-01 11:54:31 +0200 | [diff] [blame] | 232 | llme->old_tlli = 0xffffffff; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 233 | llme->state = GPRS_LLMS_UNASSIGNED; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 234 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 235 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) |
| 236 | lle_init(llme, i); |
| 237 | |
| 238 | llist_add(&llme->list, &gprs_llc_llmes); |
| 239 | |
| 240 | return llme; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Harald Welte | f7fef48 | 2010-06-28 22:18:26 +0200 | [diff] [blame] | 243 | static void llme_free(struct gprs_llc_llme *llme) |
| 244 | { |
| 245 | llist_del(&llme->list); |
| 246 | talloc_free(llme); |
| 247 | } |
| 248 | |
Holger Hans Peter Freyther | 744568b | 2014-04-04 12:47:32 +0200 | [diff] [blame] | 249 | #if 0 |
| 250 | /* FIXME: Unused code... */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 251 | static void t200_expired(void *data) |
| 252 | { |
| 253 | struct gprs_llc_lle *lle = data; |
| 254 | |
| 255 | /* 8.5.1.3: Expiry of T200 */ |
| 256 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 257 | if (lle->retrans_ctr >= lle->params.n200) { |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 258 | /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 259 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | switch (lle->state) { |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 263 | case GPRS_LLES_LOCAL_EST: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 264 | /* FIXME: retransmit SABM */ |
| 265 | /* FIXME: re-start T200 */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 266 | lle->retrans_ctr++; |
| 267 | break; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 268 | case GPRS_LLES_LOCAL_REL: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 269 | /* FIXME: retransmit DISC */ |
| 270 | /* FIXME: re-start T200 */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 271 | lle->retrans_ctr++; |
| 272 | break; |
Holger Hans Peter Freyther | 744568b | 2014-04-04 12:47:32 +0200 | [diff] [blame] | 273 | default: |
| 274 | LOGP(DLLC, LOGL_ERROR, "LLC unhandled state: %d\n", lle->state); |
| 275 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | } |
| 279 | |
| 280 | static void t201_expired(void *data) |
| 281 | { |
| 282 | struct gprs_llc_lle *lle = data; |
| 283 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 284 | if (lle->retrans_ctr < lle->params.n200) { |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 285 | /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */ |
| 286 | /* FIXME: set timer T201 */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 287 | lle->retrans_ctr++; |
| 288 | } |
| 289 | } |
Holger Hans Peter Freyther | 744568b | 2014-04-04 12:47:32 +0200 | [diff] [blame] | 290 | #endif |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 291 | |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 292 | int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command, |
| 293 | enum gprs_llc_u_cmd u_cmd, int pf_bit) |
| 294 | { |
| 295 | uint8_t *fcs, *llch; |
| 296 | uint8_t addr, ctrl; |
| 297 | uint32_t fcs_calc; |
| 298 | |
| 299 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 300 | |
| 301 | /* Address Field */ |
| 302 | addr = sapi & 0xf; |
| 303 | if (command) |
| 304 | addr |= 0x40; |
| 305 | |
| 306 | /* 6.3 Figure 8 */ |
| 307 | ctrl = 0xe0 | u_cmd; |
| 308 | if (pf_bit) |
| 309 | ctrl |= 0x10; |
| 310 | |
| 311 | /* prepend LLC UI header */ |
| 312 | llch = msgb_push(msg, 2); |
| 313 | llch[0] = addr; |
| 314 | llch[1] = ctrl; |
| 315 | |
| 316 | /* append FCS to end of frame */ |
| 317 | fcs = msgb_put(msg, 3); |
| 318 | fcs_calc = gprs_llc_fcs(llch, fcs - llch); |
| 319 | fcs[0] = fcs_calc & 0xff; |
| 320 | fcs[1] = (fcs_calc >> 8) & 0xff; |
| 321 | fcs[2] = (fcs_calc >> 16) & 0xff; |
| 322 | |
| 323 | /* Identifiers passed down: (BVCI, NSEI) */ |
| 324 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 325 | /* Send BSSGP-DL-UNITDATA.req */ |
Harald Welte | b1fd902 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 326 | return _bssgp_tx_dl_ud(msg, NULL); |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* Send XID response to LLE */ |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 330 | static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg, |
| 331 | int command) |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 332 | { |
| 333 | /* copy identifiers from LLE to ensure lower layers can route */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 334 | msgb_tlli(msg) = lle->llme->tlli; |
| 335 | msgb_bvci(msg) = lle->llme->bvci; |
| 336 | msgb_nsei(msg) = lle->llme->nsei; |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 337 | |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 338 | return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1); |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 339 | } |
| 340 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 341 | /* Transmit a UI frame over the given SAPI */ |
Harald Welte | 56a0145 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 342 | int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command, |
| 343 | void *mmctx) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 344 | { |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 345 | struct gprs_llc_lle *lle; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 346 | uint8_t *fcs, *llch; |
| 347 | uint8_t addr, ctrl[2]; |
| 348 | uint32_t fcs_calc; |
| 349 | uint16_t nu = 0; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 350 | uint32_t oc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 351 | |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 352 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 353 | |
| 354 | /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */ |
| 355 | lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi); |
Holger Hans Peter Freyther | 964a9b3 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 356 | if (!lle) |
| 357 | lle = lle_by_tlli_sapi(tlli_foreign2local(msgb_tlli(msg)), sapi); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 358 | if (!lle) { |
| 359 | struct gprs_llc_llme *llme; |
Harald Welte | f0901f0 | 2010-12-26 10:39:26 +0100 | [diff] [blame] | 360 | LOGP(DLLC, LOGL_ERROR, "LLC TX: unknown TLLI 0x%08x, " |
| 361 | "creating LLME on the fly\n", msgb_tlli(msg)); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 362 | llme = llme_alloc(msgb_tlli(msg)); |
| 363 | lle = &llme->lle[sapi]; |
| 364 | } |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 365 | |
| 366 | if (msg->len > lle->params.n201_u) { |
| 367 | LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n", |
| 368 | msg->len, lle->params.n201_u); |
| 369 | return -EFBIG; |
| 370 | } |
| 371 | |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 372 | /* Update LLE's (BVCI, NSEI) tuple */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 373 | lle->llme->bvci = msgb_bvci(msg); |
| 374 | lle->llme->nsei = msgb_nsei(msg); |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 375 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 376 | /* Obtain current values for N(u) and OC */ |
Harald Welte | 6bdee6a | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 377 | nu = lle->vu_send; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 378 | oc = lle->oc_ui_send; |
| 379 | /* Increment V(U) */ |
Harald Welte | 6bdee6a | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 380 | lle->vu_send = (lle->vu_send + 1) % 512; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 381 | /* Increment Overflow Counter, if needed */ |
| 382 | if ((lle->vu_send + 1) / 512) |
| 383 | lle->oc_ui_send += 512; |
Harald Welte | 6bdee6a | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 384 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 385 | /* Address Field */ |
| 386 | addr = sapi & 0xf; |
| 387 | if (command) |
| 388 | addr |= 0x40; |
| 389 | |
| 390 | /* Control Field */ |
| 391 | ctrl[0] = 0xc0; |
| 392 | ctrl[0] |= nu >> 6; |
| 393 | ctrl[1] = (nu << 2) & 0xfc; |
| 394 | ctrl[1] |= 0x01; /* Protected Mode */ |
| 395 | |
| 396 | /* prepend LLC UI header */ |
| 397 | llch = msgb_push(msg, 3); |
| 398 | llch[0] = addr; |
| 399 | llch[1] = ctrl[0]; |
| 400 | llch[2] = ctrl[1]; |
| 401 | |
| 402 | /* append FCS to end of frame */ |
| 403 | fcs = msgb_put(msg, 3); |
| 404 | fcs_calc = gprs_llc_fcs(llch, fcs - llch); |
| 405 | fcs[0] = fcs_calc & 0xff; |
| 406 | fcs[1] = (fcs_calc >> 8) & 0xff; |
| 407 | fcs[2] = (fcs_calc >> 16) & 0xff; |
| 408 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 409 | /* encrypt information field + FCS, if needed! */ |
| 410 | if (lle->llme->algo != GPRS_ALGO_GEA0) { |
| 411 | uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */ |
| 412 | uint16_t crypt_len = (fcs + 3) - (llch + 3); |
| 413 | uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK]; |
| 414 | uint32_t iv; |
| 415 | int rc, i; |
| 416 | uint64_t kc = *(uint64_t *)&lle->llme->kc; |
| 417 | |
| 418 | /* Compute the 'Input' Paraemeter */ |
| 419 | iv = gprs_cipher_gen_input_ui(iov_ui, sapi, nu, oc); |
| 420 | |
| 421 | /* Compute the keystream that we need to XOR with the data */ |
| 422 | rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo, |
| 423 | kc, iv, GPRS_CIPH_SGSN2MS); |
| 424 | if (rc < 0) { |
| 425 | LOGP(DLLC, LOGL_ERROR, "Error crypting UI frame: %d\n", rc); |
| 426 | return rc; |
| 427 | } |
| 428 | |
| 429 | /* XOR the cipher output with the information field + FCS */ |
| 430 | for (i = 0; i < crypt_len; i++) |
| 431 | *(llch + 3 + i) ^= cipher_out[i]; |
| 432 | |
| 433 | /* Mark frame as encrypted */ |
| 434 | ctrl[1] |= 0x02; |
| 435 | } |
| 436 | |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 437 | /* Identifiers passed down: (BVCI, NSEI) */ |
| 438 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 439 | /* Send BSSGP-DL-UNITDATA.req */ |
Harald Welte | faa70ff | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 440 | return _bssgp_tx_dl_ud(msg, mmctx); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 441 | } |
| 442 | |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 443 | /* According to 6.4.1.6 / Figure 11 */ |
| 444 | static int msgb_put_xid_par(struct msgb *msg, uint8_t type, uint8_t length, uint8_t *data) |
| 445 | { |
| 446 | uint8_t header_len = 1; |
| 447 | uint8_t *cur; |
| 448 | |
| 449 | /* type is a 5-bit field... */ |
| 450 | if (type > 0x1f) |
| 451 | return -EINVAL; |
| 452 | |
| 453 | if (length > 3) |
| 454 | header_len = 2; |
| 455 | |
| 456 | cur = msgb_put(msg, length + header_len); |
| 457 | |
| 458 | /* build the header without or with XL bit */ |
| 459 | if (length <= 3) { |
| 460 | *cur++ = (type << 2) | (length & 3); |
| 461 | } else { |
| 462 | *cur++ = 0x80 | (type << 2) | (length >> 6); |
| 463 | *cur++ = (length << 2); |
| 464 | } |
| 465 | |
| 466 | /* copy over the payload of the parameter*/ |
| 467 | memcpy(cur, data, length); |
| 468 | |
| 469 | return length + header_len; |
| 470 | } |
| 471 | |
| 472 | static void rx_llc_xid(struct gprs_llc_lle *lle, |
| 473 | struct gprs_llc_hdr_parsed *gph) |
| 474 | { |
| 475 | /* FIXME: 8.5.3.3: check if XID is invalid */ |
| 476 | if (gph->is_cmd) { |
| 477 | /* FIXME: implement XID negotiation using SNDCP */ |
| 478 | struct msgb *resp; |
| 479 | uint8_t *xid; |
| 480 | resp = msgb_alloc_headroom(4096, 1024, "LLC_XID"); |
| 481 | xid = msgb_put(resp, gph->data_len); |
| 482 | memcpy(xid, gph->data, gph->data_len); |
| 483 | gprs_llc_tx_xid(lle, resp, 0); |
| 484 | } else { |
| 485 | /* FIXME: if we had sent a XID reset, send |
| 486 | * LLGMM-RESET.conf to GMM */ |
| 487 | /* FIXME: implement XID negotiation using SNDCP */ |
| 488 | } |
| 489 | } |
| 490 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 491 | static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph, |
| 492 | struct gprs_llc_lle *lle) |
| 493 | { |
| 494 | switch (gph->cmd) { |
| 495 | case GPRS_LLC_SABM: /* Section 6.4.1.1 */ |
| 496 | lle->v_sent = lle->v_ack = lle->v_recv = 0; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 497 | if (lle->state == GPRS_LLES_ASSIGNED_ADM) { |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 498 | /* start re-establishment (8.7.1) */ |
| 499 | } |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 500 | lle->state = GPRS_LLES_REMOTE_EST; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 501 | /* FIXME: Send UA */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 502 | lle->state = GPRS_LLES_ABM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 503 | /* FIXME: process data */ |
| 504 | break; |
| 505 | case GPRS_LLC_DISC: /* Section 6.4.1.2 */ |
| 506 | /* FIXME: Send UA */ |
| 507 | /* terminate ABM */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 508 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 509 | break; |
| 510 | case GPRS_LLC_UA: /* Section 6.4.1.3 */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 511 | if (lle->state == GPRS_LLES_LOCAL_EST) |
| 512 | lle->state = GPRS_LLES_ABM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 513 | break; |
| 514 | case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 515 | if (lle->state == GPRS_LLES_LOCAL_EST) |
| 516 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 517 | break; |
| 518 | case GPRS_LLC_FRMR: /* Section 6.4.1.5 */ |
| 519 | break; |
| 520 | case GPRS_LLC_XID: /* Section 6.4.1.6 */ |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 521 | rx_llc_xid(lle, gph); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 522 | break; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 523 | case GPRS_LLC_UI: |
Holger Hans Peter Freyther | faf1f64 | 2011-06-23 17:53:27 -0400 | [diff] [blame] | 524 | if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) { |
| 525 | LOGP(DLLC, LOGL_NOTICE, |
| 526 | "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n", |
Holger Hans Peter Freyther | 2788b96 | 2010-06-23 09:48:25 +0800 | [diff] [blame] | 527 | lle->llme ? lle->llme->tlli : -1, |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 528 | gph->seq_tx, lle->vu_recv); |
Harald Welte | abadd54 | 2013-06-21 14:06:18 +0200 | [diff] [blame] | 529 | |
| 530 | /* HACK: non-standard recovery handling. If remote LLE |
| 531 | * is re-transmitting the same sequence number for |
Harald Welte | 649e1ff | 2013-07-21 17:41:46 +0800 | [diff] [blame] | 532 | * three times, don't discard the frame but pass it on |
Harald Welte | abadd54 | 2013-06-21 14:06:18 +0200 | [diff] [blame] | 533 | * and 'learn' the new sequence number */ |
| 534 | if (gph->seq_tx != lle->vu_recv_last) { |
| 535 | lle->vu_recv_last = gph->seq_tx; |
| 536 | lle->vu_recv_duplicates = 0; |
| 537 | } else { |
| 538 | lle->vu_recv_duplicates++; |
| 539 | if (lle->vu_recv_duplicates < 3) |
| 540 | return -EIO; |
| 541 | LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering " |
| 542 | "N(U=%d) after receiving %u duplicates\n", |
| 543 | lle->llme ? lle->llme->tlli : -1, |
| 544 | gph->seq_tx, lle->vu_recv_duplicates); |
| 545 | } |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 546 | } |
| 547 | /* Increment the sequence number that we expect in the next frame */ |
| 548 | lle->vu_recv = (gph->seq_tx + 1) % 512; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 549 | /* Increment Overflow Counter */ |
| 550 | if ((gph->seq_tx + 1) / 512) |
| 551 | lle->oc_ui_recv += 512; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 552 | break; |
Holger Hans Peter Freyther | 744568b | 2014-04-04 12:47:32 +0200 | [diff] [blame] | 553 | default: |
| 554 | LOGP(DLLC, LOGL_NOTICE, "Unhandled command: %d\n", gph->cmd); |
| 555 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 561 | /* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 562 | int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv) |
| 563 | { |
Holger Hans Peter Freyther | 3dccda5 | 2011-10-14 23:42:13 +0200 | [diff] [blame] | 564 | struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 565 | struct gprs_llc_hdr_parsed llhp; |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 566 | struct gprs_llc_lle *lle; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 567 | int rc = 0; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 568 | |
Harald Welte | 11d7c10 | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 569 | /* Identifiers from DOWN: NSEI, BVCI, TLLI */ |
| 570 | |
Holger Hans Peter Freyther | 4752e0c | 2010-05-23 21:33:57 +0800 | [diff] [blame] | 571 | memset(&llhp, 0, sizeof(llhp)); |
Holger Hans Peter Freyther | fa848d4 | 2010-05-23 21:43:57 +0800 | [diff] [blame] | 572 | rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU)); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 573 | gprs_llc_hdr_dump(&llhp); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 574 | if (rc < 0) { |
Harald Welte | 1b170d1 | 2010-05-13 19:49:06 +0200 | [diff] [blame] | 575 | LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n"); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 576 | return rc; |
| 577 | } |
| 578 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 579 | switch (gprs_tlli_type(msgb_tlli(msg))) { |
| 580 | case TLLI_LOCAL: |
| 581 | case TLLI_FOREIGN: |
| 582 | case TLLI_RANDOM: |
| 583 | case TLLI_AUXILIARY: |
| 584 | break; |
| 585 | default: |
| 586 | LOGP(DLLC, LOGL_ERROR, |
| 587 | "Discarding frame with strange TLLI type\n"); |
| 588 | break; |
| 589 | } |
| 590 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 591 | /* find the LLC Entity for this TLLI+SAPI tuple */ |
Holger Hans Peter Freyther | 964a9b3 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 592 | lle = lle_for_rx_by_tlli_sapi(msgb_tlli(msg), llhp.sapi, llhp.cmd); |
| 593 | if (!lle) |
| 594 | return 0; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 595 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 596 | /* decrypt information field + FCS, if needed! */ |
| 597 | if (llhp.is_encrypted) { |
| 598 | uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */ |
| 599 | uint16_t crypt_len = llhp.data_len + 3; |
| 600 | uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK]; |
| 601 | uint32_t iv; |
| 602 | uint64_t kc = *(uint64_t *)&lle->llme->kc; |
| 603 | int rc, i; |
| 604 | |
| 605 | if (lle->llme->algo == GPRS_ALGO_GEA0) { |
| 606 | LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that " |
| 607 | "has no KC/Algo! Dropping.\n"); |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | iv = gprs_cipher_gen_input_ui(iov_ui, lle->sapi, llhp.seq_tx, |
| 612 | lle->oc_ui_recv); |
| 613 | rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo, |
| 614 | kc, iv, GPRS_CIPH_MS2SGSN); |
| 615 | if (rc < 0) { |
| 616 | LOGP(DLLC, LOGL_ERROR, "Error decrypting frame: %d\n", |
| 617 | rc); |
| 618 | return rc; |
| 619 | } |
| 620 | |
| 621 | /* XOR the cipher output with the information field + FCS */ |
| 622 | for (i = 0; i < crypt_len; i++) |
| 623 | *(llhp.data + i) ^= cipher_out[i]; |
| 624 | } else { |
| 625 | if (lle->llme->algo != GPRS_ALGO_GEA0) { |
| 626 | LOGP(DLLC, LOGL_NOTICE, "unencrypted frame for LLC " |
| 627 | "that is supposed to be encrypted. Dropping.\n"); |
| 628 | return 0; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | /* We have to do the FCS check _after_ decryption */ |
Harald Welte | 1b8827a | 2010-06-30 23:15:57 +0200 | [diff] [blame] | 633 | llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length); |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 634 | if (llhp.fcs != llhp.fcs_calc) { |
| 635 | LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n"); |
| 636 | return -EIO; |
| 637 | } |
| 638 | |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 639 | /* Update LLE's (BVCI, NSEI) tuple */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 640 | lle->llme->bvci = msgb_bvci(msg); |
| 641 | lle->llme->nsei = msgb_nsei(msg); |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 642 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 643 | /* Receive and Process the actual LLC frame */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 644 | rc = gprs_llc_hdr_rx(&llhp, lle); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 645 | if (rc < 0) |
| 646 | return rc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 647 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 648 | /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 649 | if (llhp.data) { |
Harald Welte | 943c5bc | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 650 | msgb_gmmh(msg) = llhp.data; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 651 | switch (llhp.sapi) { |
| 652 | case GPRS_SAPI_GMM: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 653 | /* send LL_UNITDATA_IND to GMM */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 654 | rc = gsm0408_gprs_rcvmsg(msg, lle->llme); |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 655 | break; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 656 | case GPRS_SAPI_SNDCP3: |
| 657 | case GPRS_SAPI_SNDCP5: |
| 658 | case GPRS_SAPI_SNDCP9: |
| 659 | case GPRS_SAPI_SNDCP11: |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 660 | /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */ |
| 661 | rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len); |
| 662 | break; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 663 | case GPRS_SAPI_SMS: |
| 664 | /* FIXME */ |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 665 | case GPRS_SAPI_TOM2: |
| 666 | case GPRS_SAPI_TOM8: |
| 667 | /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */ |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 668 | default: |
Harald Welte | c6ecafe | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 669 | LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi); |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 670 | rc = -EINVAL; |
| 671 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 675 | return rc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 676 | } |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 677 | |
| 678 | /* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */ |
| 679 | int gprs_llgmm_assign(struct gprs_llc_llme *llme, |
| 680 | uint32_t old_tlli, uint32_t new_tlli, |
| 681 | enum gprs_ciph_algo alg, const uint8_t *kc) |
| 682 | { |
| 683 | unsigned int i; |
| 684 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 685 | /* Update the crypto parameters */ |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 686 | llme->algo = alg; |
Harald Welte | 3e2e159 | 2010-06-30 23:19:23 +0200 | [diff] [blame] | 687 | if (alg != GPRS_ALGO_GEA0) |
| 688 | memcpy(llme->kc, kc, sizeof(llme->kc)); |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 689 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 690 | if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) { |
| 691 | /* TLLI Assignment 8.3.1 */ |
| 692 | /* New TLLI shall be assigned and used when (re)transmitting LLC frames */ |
| 693 | /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI |
| 694 | * old is unassigned. Only TLLI new shall be accepted when |
| 695 | * received from peer. */ |
Harald Welte | 875840c | 2010-07-01 11:54:31 +0200 | [diff] [blame] | 696 | if (llme->old_tlli != 0xffffffff) { |
| 697 | llme->old_tlli = 0xffffffff; |
| 698 | llme->tlli = new_tlli; |
| 699 | } else { |
| 700 | /* If TLLI old == 0xffffffff was assigned to LLME, then this is |
| 701 | * TLLI assignmemt according to 8.3.1 */ |
| 702 | llme->old_tlli = 0xffffffff; |
| 703 | llme->tlli = new_tlli; |
| 704 | llme->state = GPRS_LLMS_ASSIGNED; |
| 705 | /* 8.5.3.1 For all LLE's */ |
| 706 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) { |
| 707 | struct gprs_llc_lle *l = &llme->lle[i]; |
| 708 | l->vu_send = l->vu_recv = 0; |
| 709 | l->retrans_ctr = 0; |
| 710 | l->state = GPRS_LLES_ASSIGNED_ADM; |
| 711 | /* FIXME Set parameters according to table 9 */ |
| 712 | } |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 713 | } |
| 714 | } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) { |
| 715 | /* TLLI Change 8.3.2 */ |
| 716 | /* Both TLLI Old and TLLI New are assigned; use New when |
Holger Hans Peter Freyther | 92aa6bb | 2013-07-28 20:13:01 +0200 | [diff] [blame] | 717 | * (re)transmitting. Accept both Old and New on Rx */ |
Holger Hans Peter Freyther | aa93bac | 2013-07-31 11:20:37 +0200 | [diff] [blame] | 718 | llme->old_tlli = old_tlli; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 719 | llme->tlli = new_tlli; |
| 720 | llme->state = GPRS_LLMS_ASSIGNED; |
| 721 | } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) { |
| 722 | /* TLLI Unassignment 8.3.3) */ |
| 723 | llme->tlli = llme->old_tlli = 0; |
| 724 | llme->state = GPRS_LLMS_UNASSIGNED; |
| 725 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) { |
| 726 | struct gprs_llc_lle *l = &llme->lle[i]; |
| 727 | l->state = GPRS_LLES_UNASSIGNED; |
| 728 | } |
Harald Welte | f7fef48 | 2010-06-28 22:18:26 +0200 | [diff] [blame] | 729 | llme_free(llme); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 730 | } else |
| 731 | return -EINVAL; |
| 732 | |
| 733 | return 0; |
| 734 | } |
Harald Welte | 496aee4 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 735 | |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 736 | /* Chapter 7.2.1.2 LLGMM-RESET.req */ |
| 737 | int gprs_llgmm_reset(struct gprs_llc_llme *llme) |
| 738 | { |
| 739 | struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID"); |
| 740 | int random = rand(); |
| 741 | |
| 742 | /* First XID component must be RESET */ |
| 743 | msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL); |
| 744 | /* randomly select new IOV-UI */ |
Harald Welte | 066a0f5 | 2011-10-16 18:59:20 +0200 | [diff] [blame] | 745 | msgb_put_xid_par(msg, GPRS_LLC_XID_T_IOV_UI, 4, (uint8_t *) &random); |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 746 | |
| 747 | /* FIXME: Start T200, wait for XID response */ |
| 748 | return gprs_llc_tx_xid(&llme->lle[1], msg, 1); |
| 749 | } |
| 750 | |
Harald Welte | 496aee4 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 751 | int gprs_llc_init(const char *cipher_plugin_path) |
| 752 | { |
| 753 | return gprs_cipher_load(cipher_plugin_path); |
| 754 | } |