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 | |
Harald Welte | faa70ff | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 39 | /* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU |
| 40 | * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */ |
| 41 | static int _bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx) |
| 42 | { |
| 43 | struct bssgp_dl_ud_par dup; |
| 44 | const uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 }; |
| 45 | |
Harald Welte | 8c00496 | 2012-07-04 21:53:12 +0200 | [diff] [blame] | 46 | memset(&dup, 0, sizeof(dup)); |
| 47 | /* before we have received some identity from the MS, we might |
| 48 | * not yet have a MMC context (e.g. XID negotiation of primarly |
| 49 | * LLC connection fro GMM sapi). */ |
| 50 | if (mmctx) { |
| 51 | dup.imsi = mmctx->imsi; |
| 52 | dup.drx_parms = mmctx->drx_parms; |
| 53 | dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len; |
| 54 | dup.ms_ra_cap.v = mmctx->ms_radio_access_capa.buf; |
| 55 | } |
Harald Welte | faa70ff | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 56 | memcpy(&dup.qos_profile, qos_profile_default, |
| 57 | sizeof(qos_profile_default)); |
| 58 | |
Harald Welte | ce95b27 | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 59 | return bssgp_tx_dl_ud(msg, 1000, &dup); |
Harald Welte | faa70ff | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 63 | /* Section 8.9.9 LLC layer parameter default values */ |
| 64 | static const struct gprs_llc_params llc_default_params[] = { |
| 65 | [1] = { |
| 66 | .t200_201 = 5, |
| 67 | .n200 = 3, |
| 68 | .n201_u = 400, |
| 69 | }, |
| 70 | [2] = { |
| 71 | .t200_201 = 5, |
| 72 | .n200 = 3, |
| 73 | .n201_u = 270, |
| 74 | }, |
| 75 | [3] = { |
| 76 | .iov_i_exp = 27, |
| 77 | .t200_201 = 5, |
| 78 | .n200 = 3, |
| 79 | .n201_u = 500, |
| 80 | .n201_i = 1503, |
| 81 | .mD = 1520, |
| 82 | .mU = 1520, |
| 83 | .kD = 16, |
| 84 | .kU = 16, |
| 85 | }, |
| 86 | [5] = { |
| 87 | .iov_i_exp = 27, |
| 88 | .t200_201 = 10, |
| 89 | .n200 = 3, |
| 90 | .n201_u = 500, |
| 91 | .n201_i = 1503, |
| 92 | .mD = 760, |
| 93 | .mU = 760, |
| 94 | .kD = 8, |
| 95 | .kU = 8, |
| 96 | }, |
| 97 | [7] = { |
| 98 | .t200_201 = 20, |
| 99 | .n200 = 3, |
| 100 | .n201_u = 270, |
| 101 | }, |
| 102 | [8] = { |
| 103 | .t200_201 = 20, |
| 104 | .n200 = 3, |
| 105 | .n201_u = 270, |
| 106 | }, |
| 107 | [9] = { |
| 108 | .iov_i_exp = 27, |
| 109 | .t200_201 = 20, |
| 110 | .n200 = 3, |
| 111 | .n201_u = 500, |
| 112 | .n201_i = 1503, |
| 113 | .mD = 380, |
| 114 | .mU = 380, |
| 115 | .kD = 4, |
| 116 | .kU = 4, |
| 117 | }, |
| 118 | [11] = { |
| 119 | .iov_i_exp = 27, |
| 120 | .t200_201 = 40, |
| 121 | .n200 = 3, |
| 122 | .n201_u = 500, |
| 123 | .n201_i = 1503, |
| 124 | .mD = 190, |
| 125 | .mU = 190, |
| 126 | .kD = 2, |
| 127 | .kU = 2, |
| 128 | }, |
| 129 | }; |
| 130 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 131 | LLIST_HEAD(gprs_llc_llmes); |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 132 | void *llc_tall_ctx; |
| 133 | |
Harald Welte | f0901f0 | 2010-12-26 10:39:26 +0100 | [diff] [blame] | 134 | /* If the TLLI is foreign, return its local version */ |
| 135 | static inline uint32_t tlli_foreign2local(uint32_t tlli) |
| 136 | { |
| 137 | uint32_t new_tlli; |
| 138 | |
| 139 | if (gprs_tlli_type(tlli) == TLLI_FOREIGN) { |
| 140 | new_tlli = tlli | 0x40000000; |
| 141 | DEBUGP(DLLC, "TLLI 0x%08x is foreign, converting to " |
| 142 | "local TLLI 0x%08x\n", tlli, new_tlli); |
| 143 | } else |
| 144 | new_tlli = tlli; |
| 145 | |
| 146 | return new_tlli; |
| 147 | } |
| 148 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 149 | /* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */ |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 150 | static struct gprs_llc_lle *lle_by_tlli_sapi(uint32_t tlli, uint8_t sapi) |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 151 | { |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 152 | struct gprs_llc_llme *llme; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 153 | |
Harald Welte | f0901f0 | 2010-12-26 10:39:26 +0100 | [diff] [blame] | 154 | tlli = tlli_foreign2local(tlli); |
| 155 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 156 | llist_for_each_entry(llme, &gprs_llc_llmes, list) { |
| 157 | if (llme->tlli == tlli || llme->old_tlli == tlli) |
| 158 | return &llme->lle[sapi]; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 159 | } |
| 160 | return NULL; |
| 161 | } |
| 162 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 163 | static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi) |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 164 | { |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 165 | struct gprs_llc_lle *lle = &llme->lle[sapi]; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 166 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 167 | lle->llme = llme; |
| 168 | lle->sapi = sapi; |
| 169 | lle->state = GPRS_LLES_UNASSIGNED; |
| 170 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 171 | /* Initialize according to parameters */ |
| 172 | memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params)); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static struct gprs_llc_llme *llme_alloc(uint32_t tlli) |
| 176 | { |
| 177 | struct gprs_llc_llme *llme; |
| 178 | uint32_t i; |
| 179 | |
| 180 | llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme); |
| 181 | if (!llme) |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 182 | return NULL; |
| 183 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 184 | llme->tlli = tlli; |
Harald Welte | 875840c | 2010-07-01 11:54:31 +0200 | [diff] [blame] | 185 | llme->old_tlli = 0xffffffff; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 186 | llme->state = GPRS_LLMS_UNASSIGNED; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 187 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 188 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) |
| 189 | lle_init(llme, i); |
| 190 | |
| 191 | llist_add(&llme->list, &gprs_llc_llmes); |
| 192 | |
| 193 | return llme; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 194 | } |
| 195 | |
Harald Welte | f7fef48 | 2010-06-28 22:18:26 +0200 | [diff] [blame] | 196 | static void llme_free(struct gprs_llc_llme *llme) |
| 197 | { |
| 198 | llist_del(&llme->list); |
| 199 | talloc_free(llme); |
| 200 | } |
| 201 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 202 | enum gprs_llc_cmd { |
| 203 | GPRS_LLC_NULL, |
| 204 | GPRS_LLC_RR, |
| 205 | GPRS_LLC_ACK, |
| 206 | GPRS_LLC_RNR, |
| 207 | GPRS_LLC_SACK, |
| 208 | GPRS_LLC_DM, |
| 209 | GPRS_LLC_DISC, |
| 210 | GPRS_LLC_UA, |
| 211 | GPRS_LLC_SABM, |
| 212 | GPRS_LLC_FRMR, |
| 213 | GPRS_LLC_XID, |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 214 | GPRS_LLC_UI, |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 215 | }; |
| 216 | |
Harald Welte | b61f403 | 2010-05-18 12:31:50 +0200 | [diff] [blame] | 217 | static const struct value_string llc_cmd_strs[] = { |
| 218 | { GPRS_LLC_NULL, "NULL" }, |
| 219 | { GPRS_LLC_RR, "RR" }, |
| 220 | { GPRS_LLC_ACK, "ACK" }, |
| 221 | { GPRS_LLC_RNR, "RNR" }, |
| 222 | { GPRS_LLC_SACK, "SACK" }, |
| 223 | { GPRS_LLC_DM, "DM" }, |
| 224 | { GPRS_LLC_DISC, "DISC" }, |
| 225 | { GPRS_LLC_UA, "UA" }, |
| 226 | { GPRS_LLC_SABM, "SABM" }, |
| 227 | { GPRS_LLC_FRMR, "FRMR" }, |
| 228 | { GPRS_LLC_XID, "XID" }, |
| 229 | { GPRS_LLC_UI, "UI" }, |
| 230 | { 0, NULL } |
| 231 | }; |
| 232 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 233 | struct gprs_llc_hdr_parsed { |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 234 | uint8_t sapi; |
| 235 | uint8_t is_cmd:1, |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 236 | ack_req:1, |
| 237 | is_encrypted:1; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 238 | uint32_t seq_rx; |
| 239 | uint32_t seq_tx; |
| 240 | uint32_t fcs; |
| 241 | uint32_t fcs_calc; |
| 242 | uint8_t *data; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 243 | uint16_t data_len; |
Harald Welte | 1b8827a | 2010-06-30 23:15:57 +0200 | [diff] [blame] | 244 | uint16_t crc_length; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 245 | enum gprs_llc_cmd cmd; |
| 246 | }; |
| 247 | |
| 248 | #define LLC_ALLOC_SIZE 16384 |
| 249 | #define UI_HDR_LEN 3 |
| 250 | #define N202 4 |
| 251 | #define CRC24_LENGTH 3 |
| 252 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 253 | static int gprs_llc_fcs(uint8_t *data, unsigned int len) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 254 | { |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 255 | uint32_t fcs_calc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 256 | |
| 257 | fcs_calc = crc24_calc(INIT_CRC24, data, len); |
| 258 | fcs_calc = ~fcs_calc; |
| 259 | fcs_calc &= 0xffffff; |
| 260 | |
| 261 | return fcs_calc; |
| 262 | } |
| 263 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 264 | static void t200_expired(void *data) |
| 265 | { |
| 266 | struct gprs_llc_lle *lle = data; |
| 267 | |
| 268 | /* 8.5.1.3: Expiry of T200 */ |
| 269 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 270 | if (lle->retrans_ctr >= lle->params.n200) { |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 271 | /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 272 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | switch (lle->state) { |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 276 | case GPRS_LLES_LOCAL_EST: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 277 | /* FIXME: retransmit SABM */ |
| 278 | /* FIXME: re-start T200 */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 279 | lle->retrans_ctr++; |
| 280 | break; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 281 | case GPRS_LLES_LOCAL_REL: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 282 | /* FIXME: retransmit DISC */ |
| 283 | /* FIXME: re-start T200 */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 284 | lle->retrans_ctr++; |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | } |
| 289 | |
| 290 | static void t201_expired(void *data) |
| 291 | { |
| 292 | struct gprs_llc_lle *lle = data; |
| 293 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 294 | if (lle->retrans_ctr < lle->params.n200) { |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 295 | /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */ |
| 296 | /* FIXME: set timer T201 */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 297 | lle->retrans_ctr++; |
| 298 | } |
| 299 | } |
| 300 | |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 301 | int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command, |
| 302 | enum gprs_llc_u_cmd u_cmd, int pf_bit) |
| 303 | { |
| 304 | uint8_t *fcs, *llch; |
| 305 | uint8_t addr, ctrl; |
| 306 | uint32_t fcs_calc; |
| 307 | |
| 308 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 309 | |
| 310 | /* Address Field */ |
| 311 | addr = sapi & 0xf; |
| 312 | if (command) |
| 313 | addr |= 0x40; |
| 314 | |
| 315 | /* 6.3 Figure 8 */ |
| 316 | ctrl = 0xe0 | u_cmd; |
| 317 | if (pf_bit) |
| 318 | ctrl |= 0x10; |
| 319 | |
| 320 | /* prepend LLC UI header */ |
| 321 | llch = msgb_push(msg, 2); |
| 322 | llch[0] = addr; |
| 323 | llch[1] = ctrl; |
| 324 | |
| 325 | /* append FCS to end of frame */ |
| 326 | fcs = msgb_put(msg, 3); |
| 327 | fcs_calc = gprs_llc_fcs(llch, fcs - llch); |
| 328 | fcs[0] = fcs_calc & 0xff; |
| 329 | fcs[1] = (fcs_calc >> 8) & 0xff; |
| 330 | fcs[2] = (fcs_calc >> 16) & 0xff; |
| 331 | |
| 332 | /* Identifiers passed down: (BVCI, NSEI) */ |
| 333 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 334 | /* Send BSSGP-DL-UNITDATA.req */ |
Harald Welte | b1fd902 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 335 | return _bssgp_tx_dl_ud(msg, NULL); |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* Send XID response to LLE */ |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 339 | static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg, |
| 340 | int command) |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 341 | { |
| 342 | /* copy identifiers from LLE to ensure lower layers can route */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 343 | msgb_tlli(msg) = lle->llme->tlli; |
| 344 | msgb_bvci(msg) = lle->llme->bvci; |
| 345 | msgb_nsei(msg) = lle->llme->nsei; |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 346 | |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 347 | return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1); |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 350 | /* Transmit a UI frame over the given SAPI */ |
Harald Welte | 56a0145 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 351 | int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command, |
| 352 | void *mmctx) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 353 | { |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 354 | struct gprs_llc_lle *lle; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 355 | uint8_t *fcs, *llch; |
| 356 | uint8_t addr, ctrl[2]; |
| 357 | uint32_t fcs_calc; |
| 358 | uint16_t nu = 0; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 359 | uint32_t oc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 360 | |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 361 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 362 | |
| 363 | /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */ |
| 364 | lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 365 | if (!lle) { |
| 366 | struct gprs_llc_llme *llme; |
Harald Welte | f0901f0 | 2010-12-26 10:39:26 +0100 | [diff] [blame] | 367 | LOGP(DLLC, LOGL_ERROR, "LLC TX: unknown TLLI 0x%08x, " |
| 368 | "creating LLME on the fly\n", msgb_tlli(msg)); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 369 | llme = llme_alloc(msgb_tlli(msg)); |
| 370 | lle = &llme->lle[sapi]; |
| 371 | } |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 372 | |
| 373 | if (msg->len > lle->params.n201_u) { |
| 374 | LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n", |
| 375 | msg->len, lle->params.n201_u); |
| 376 | return -EFBIG; |
| 377 | } |
| 378 | |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 379 | /* Update LLE's (BVCI, NSEI) tuple */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 380 | lle->llme->bvci = msgb_bvci(msg); |
| 381 | lle->llme->nsei = msgb_nsei(msg); |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 382 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 383 | /* Obtain current values for N(u) and OC */ |
Harald Welte | 6bdee6a | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 384 | nu = lle->vu_send; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 385 | oc = lle->oc_ui_send; |
| 386 | /* Increment V(U) */ |
Harald Welte | 6bdee6a | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 387 | lle->vu_send = (lle->vu_send + 1) % 512; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 388 | /* Increment Overflow Counter, if needed */ |
| 389 | if ((lle->vu_send + 1) / 512) |
| 390 | lle->oc_ui_send += 512; |
Harald Welte | 6bdee6a | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 391 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 392 | /* Address Field */ |
| 393 | addr = sapi & 0xf; |
| 394 | if (command) |
| 395 | addr |= 0x40; |
| 396 | |
| 397 | /* Control Field */ |
| 398 | ctrl[0] = 0xc0; |
| 399 | ctrl[0] |= nu >> 6; |
| 400 | ctrl[1] = (nu << 2) & 0xfc; |
| 401 | ctrl[1] |= 0x01; /* Protected Mode */ |
| 402 | |
| 403 | /* prepend LLC UI header */ |
| 404 | llch = msgb_push(msg, 3); |
| 405 | llch[0] = addr; |
| 406 | llch[1] = ctrl[0]; |
| 407 | llch[2] = ctrl[1]; |
| 408 | |
| 409 | /* append FCS to end of frame */ |
| 410 | fcs = msgb_put(msg, 3); |
| 411 | fcs_calc = gprs_llc_fcs(llch, fcs - llch); |
| 412 | fcs[0] = fcs_calc & 0xff; |
| 413 | fcs[1] = (fcs_calc >> 8) & 0xff; |
| 414 | fcs[2] = (fcs_calc >> 16) & 0xff; |
| 415 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 416 | /* encrypt information field + FCS, if needed! */ |
| 417 | if (lle->llme->algo != GPRS_ALGO_GEA0) { |
| 418 | uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */ |
| 419 | uint16_t crypt_len = (fcs + 3) - (llch + 3); |
| 420 | uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK]; |
| 421 | uint32_t iv; |
| 422 | int rc, i; |
| 423 | uint64_t kc = *(uint64_t *)&lle->llme->kc; |
| 424 | |
| 425 | /* Compute the 'Input' Paraemeter */ |
| 426 | iv = gprs_cipher_gen_input_ui(iov_ui, sapi, nu, oc); |
| 427 | |
| 428 | /* Compute the keystream that we need to XOR with the data */ |
| 429 | rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo, |
| 430 | kc, iv, GPRS_CIPH_SGSN2MS); |
| 431 | if (rc < 0) { |
| 432 | LOGP(DLLC, LOGL_ERROR, "Error crypting UI frame: %d\n", rc); |
| 433 | return rc; |
| 434 | } |
| 435 | |
| 436 | /* XOR the cipher output with the information field + FCS */ |
| 437 | for (i = 0; i < crypt_len; i++) |
| 438 | *(llch + 3 + i) ^= cipher_out[i]; |
| 439 | |
| 440 | /* Mark frame as encrypted */ |
| 441 | ctrl[1] |= 0x02; |
| 442 | } |
| 443 | |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 444 | /* Identifiers passed down: (BVCI, NSEI) */ |
| 445 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 446 | /* Send BSSGP-DL-UNITDATA.req */ |
Harald Welte | faa70ff | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 447 | return _bssgp_tx_dl_ud(msg, mmctx); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 448 | } |
| 449 | |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 450 | /* According to 6.4.1.6 / Figure 11 */ |
| 451 | static int msgb_put_xid_par(struct msgb *msg, uint8_t type, uint8_t length, uint8_t *data) |
| 452 | { |
| 453 | uint8_t header_len = 1; |
| 454 | uint8_t *cur; |
| 455 | |
| 456 | /* type is a 5-bit field... */ |
| 457 | if (type > 0x1f) |
| 458 | return -EINVAL; |
| 459 | |
| 460 | if (length > 3) |
| 461 | header_len = 2; |
| 462 | |
| 463 | cur = msgb_put(msg, length + header_len); |
| 464 | |
| 465 | /* build the header without or with XL bit */ |
| 466 | if (length <= 3) { |
| 467 | *cur++ = (type << 2) | (length & 3); |
| 468 | } else { |
| 469 | *cur++ = 0x80 | (type << 2) | (length >> 6); |
| 470 | *cur++ = (length << 2); |
| 471 | } |
| 472 | |
| 473 | /* copy over the payload of the parameter*/ |
| 474 | memcpy(cur, data, length); |
| 475 | |
| 476 | return length + header_len; |
| 477 | } |
| 478 | |
| 479 | static void rx_llc_xid(struct gprs_llc_lle *lle, |
| 480 | struct gprs_llc_hdr_parsed *gph) |
| 481 | { |
| 482 | /* FIXME: 8.5.3.3: check if XID is invalid */ |
| 483 | if (gph->is_cmd) { |
| 484 | /* FIXME: implement XID negotiation using SNDCP */ |
| 485 | struct msgb *resp; |
| 486 | uint8_t *xid; |
| 487 | resp = msgb_alloc_headroom(4096, 1024, "LLC_XID"); |
| 488 | xid = msgb_put(resp, gph->data_len); |
| 489 | memcpy(xid, gph->data, gph->data_len); |
| 490 | gprs_llc_tx_xid(lle, resp, 0); |
| 491 | } else { |
| 492 | /* FIXME: if we had sent a XID reset, send |
| 493 | * LLGMM-RESET.conf to GMM */ |
| 494 | /* FIXME: implement XID negotiation using SNDCP */ |
| 495 | } |
| 496 | } |
| 497 | |
Holger Hans Peter Freyther | 3a6fdcd | 2010-05-23 21:35:25 +0800 | [diff] [blame] | 498 | static void gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 499 | { |
Sylvain Munaut | 6f3850f | 2010-07-03 22:03:02 +0200 | [diff] [blame] | 500 | DEBUGP(DLLC, "LLC SAPI=%u %c %c FCS=0x%06x", |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 501 | gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ', |
Sylvain Munaut | 6f3850f | 2010-07-03 22:03:02 +0200 | [diff] [blame] | 502 | gph->fcs); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 503 | |
| 504 | if (gph->cmd) |
Harald Welte | b61f403 | 2010-05-18 12:31:50 +0200 | [diff] [blame] | 505 | DEBUGPC(DLLC, "CMD=%s ", get_value_string(llc_cmd_strs, gph->cmd)); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 506 | |
| 507 | if (gph->data) |
Harald Welte | c6ecafe | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 508 | DEBUGPC(DLLC, "DATA "); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 509 | |
Harald Welte | c6ecafe | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 510 | DEBUGPC(DLLC, "\n"); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 511 | } |
| 512 | static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph, |
| 513 | struct gprs_llc_lle *lle) |
| 514 | { |
| 515 | switch (gph->cmd) { |
| 516 | case GPRS_LLC_SABM: /* Section 6.4.1.1 */ |
| 517 | lle->v_sent = lle->v_ack = lle->v_recv = 0; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 518 | if (lle->state == GPRS_LLES_ASSIGNED_ADM) { |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 519 | /* start re-establishment (8.7.1) */ |
| 520 | } |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 521 | lle->state = GPRS_LLES_REMOTE_EST; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 522 | /* FIXME: Send UA */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 523 | lle->state = GPRS_LLES_ABM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 524 | /* FIXME: process data */ |
| 525 | break; |
| 526 | case GPRS_LLC_DISC: /* Section 6.4.1.2 */ |
| 527 | /* FIXME: Send UA */ |
| 528 | /* terminate ABM */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 529 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 530 | break; |
| 531 | case GPRS_LLC_UA: /* Section 6.4.1.3 */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 532 | if (lle->state == GPRS_LLES_LOCAL_EST) |
| 533 | lle->state = GPRS_LLES_ABM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 534 | break; |
| 535 | 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] | 536 | if (lle->state == GPRS_LLES_LOCAL_EST) |
| 537 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 538 | break; |
| 539 | case GPRS_LLC_FRMR: /* Section 6.4.1.5 */ |
| 540 | break; |
| 541 | case GPRS_LLC_XID: /* Section 6.4.1.6 */ |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 542 | rx_llc_xid(lle, gph); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 543 | break; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 544 | case GPRS_LLC_UI: |
Holger Hans Peter Freyther | faf1f64 | 2011-06-23 17:53:27 -0400 | [diff] [blame] | 545 | if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) { |
| 546 | LOGP(DLLC, LOGL_NOTICE, |
| 547 | "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] | 548 | lle->llme ? lle->llme->tlli : -1, |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 549 | gph->seq_tx, lle->vu_recv); |
| 550 | return -EIO; |
| 551 | } |
| 552 | /* Increment the sequence number that we expect in the next frame */ |
| 553 | lle->vu_recv = (gph->seq_tx + 1) % 512; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 554 | /* Increment Overflow Counter */ |
| 555 | if ((gph->seq_tx + 1) / 512) |
| 556 | lle->oc_ui_recv += 512; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 557 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | /* parse a GPRS LLC header, also check for invalid frames */ |
| 564 | static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp, |
Holger Hans Peter Freyther | fa848d4 | 2010-05-23 21:43:57 +0800 | [diff] [blame] | 565 | uint8_t *llc_hdr, int len) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 566 | { |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 567 | uint8_t *ctrl = llc_hdr+1; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 568 | |
| 569 | if (len <= CRC24_LENGTH) |
| 570 | return -EIO; |
| 571 | |
Harald Welte | 1b8827a | 2010-06-30 23:15:57 +0200 | [diff] [blame] | 572 | ghp->crc_length = len - CRC24_LENGTH; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 573 | |
| 574 | ghp->ack_req = 0; |
| 575 | |
| 576 | /* Section 5.5: FCS */ |
| 577 | ghp->fcs = *(llc_hdr + len - 3); |
| 578 | ghp->fcs |= *(llc_hdr + len - 2) << 8; |
| 579 | ghp->fcs |= *(llc_hdr + len - 1) << 16; |
| 580 | |
| 581 | /* Section 6.2.1: invalid PD field */ |
| 582 | if (llc_hdr[0] & 0x80) |
| 583 | return -EIO; |
| 584 | |
| 585 | /* This only works for the MS->SGSN direction */ |
| 586 | if (llc_hdr[0] & 0x40) |
| 587 | ghp->is_cmd = 0; |
| 588 | else |
| 589 | ghp->is_cmd = 1; |
| 590 | |
| 591 | ghp->sapi = llc_hdr[0] & 0xf; |
| 592 | |
| 593 | /* Section 6.2.3: check for reserved SAPI */ |
| 594 | switch (ghp->sapi) { |
| 595 | case 0: |
| 596 | case 4: |
| 597 | case 6: |
| 598 | case 0xa: |
| 599 | case 0xc: |
| 600 | case 0xd: |
| 601 | case 0xf: |
| 602 | return -EINVAL; |
| 603 | } |
| 604 | |
| 605 | if ((ctrl[0] & 0x80) == 0) { |
| 606 | /* I (Information transfer + Supervisory) format */ |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 607 | uint8_t k; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 608 | |
| 609 | ghp->data = ctrl + 3; |
| 610 | |
| 611 | if (ctrl[0] & 0x40) |
| 612 | ghp->ack_req = 1; |
| 613 | |
| 614 | ghp->seq_tx = (ctrl[0] & 0x1f) << 4; |
| 615 | ghp->seq_tx |= (ctrl[1] >> 4); |
| 616 | |
| 617 | ghp->seq_rx = (ctrl[1] & 0x7) << 6; |
| 618 | ghp->seq_rx |= (ctrl[2] >> 2); |
| 619 | |
| 620 | switch (ctrl[2] & 0x03) { |
| 621 | case 0: |
| 622 | ghp->cmd = GPRS_LLC_RR; |
| 623 | break; |
| 624 | case 1: |
| 625 | ghp->cmd = GPRS_LLC_ACK; |
| 626 | break; |
| 627 | case 2: |
| 628 | ghp->cmd = GPRS_LLC_RNR; |
| 629 | break; |
| 630 | case 3: |
| 631 | ghp->cmd = GPRS_LLC_SACK; |
| 632 | k = ctrl[3] & 0x1f; |
| 633 | ghp->data += 1 + k; |
| 634 | break; |
| 635 | } |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 636 | ghp->data_len = (llc_hdr + len - 3) - ghp->data; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 637 | } else if ((ctrl[0] & 0xc0) == 0x80) { |
| 638 | /* S (Supervisory) format */ |
| 639 | ghp->data = NULL; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 640 | ghp->data_len = 0; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 641 | |
| 642 | if (ctrl[0] & 0x20) |
| 643 | ghp->ack_req = 1; |
| 644 | ghp->seq_rx = (ctrl[0] & 0x7) << 6; |
| 645 | ghp->seq_rx |= (ctrl[1] >> 2); |
| 646 | |
| 647 | switch (ctrl[1] & 0x03) { |
| 648 | case 0: |
| 649 | ghp->cmd = GPRS_LLC_RR; |
| 650 | break; |
| 651 | case 1: |
| 652 | ghp->cmd = GPRS_LLC_ACK; |
| 653 | break; |
| 654 | case 2: |
| 655 | ghp->cmd = GPRS_LLC_RNR; |
| 656 | break; |
| 657 | case 3: |
| 658 | ghp->cmd = GPRS_LLC_SACK; |
| 659 | break; |
| 660 | } |
| 661 | } else if ((ctrl[0] & 0xe0) == 0xc0) { |
| 662 | /* UI (Unconfirmed Inforamtion) format */ |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 663 | ghp->cmd = GPRS_LLC_UI; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 664 | ghp->data = ctrl + 2; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 665 | ghp->data_len = (llc_hdr + len - 3) - ghp->data; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 666 | |
| 667 | ghp->seq_tx = (ctrl[0] & 0x7) << 6; |
| 668 | ghp->seq_tx |= (ctrl[1] >> 2); |
| 669 | if (ctrl[1] & 0x02) { |
| 670 | ghp->is_encrypted = 1; |
| 671 | /* FIXME: encryption */ |
| 672 | } |
| 673 | if (ctrl[1] & 0x01) { |
| 674 | /* FCS over hdr + all inf fields */ |
| 675 | } else { |
| 676 | /* FCS over hdr + N202 octets (4) */ |
Harald Welte | 1b8827a | 2010-06-30 23:15:57 +0200 | [diff] [blame] | 677 | if (ghp->crc_length > UI_HDR_LEN + N202) |
| 678 | ghp->crc_length = UI_HDR_LEN + N202; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 679 | } |
| 680 | } else { |
| 681 | /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */ |
| 682 | ghp->data = NULL; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 683 | ghp->data_len = 0; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 684 | |
| 685 | switch (ctrl[0] & 0xf) { |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 686 | case GPRS_LLC_U_NULL_CMD: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 687 | ghp->cmd = GPRS_LLC_NULL; |
| 688 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 689 | case GPRS_LLC_U_DM_RESP: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 690 | ghp->cmd = GPRS_LLC_DM; |
| 691 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 692 | case GPRS_LLC_U_DISC_CMD: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 693 | ghp->cmd = GPRS_LLC_DISC; |
| 694 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 695 | case GPRS_LLC_U_UA_RESP: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 696 | ghp->cmd = GPRS_LLC_UA; |
| 697 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 698 | case GPRS_LLC_U_SABM_CMD: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 699 | ghp->cmd = GPRS_LLC_SABM; |
| 700 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 701 | case GPRS_LLC_U_FRMR_RESP: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 702 | ghp->cmd = GPRS_LLC_FRMR; |
| 703 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 704 | case GPRS_LLC_U_XID: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 705 | ghp->cmd = GPRS_LLC_XID; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 706 | ghp->data = ctrl + 1; |
| 707 | ghp->data_len = (llc_hdr + len - 3) - ghp->data; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 708 | break; |
| 709 | default: |
| 710 | return -EIO; |
| 711 | } |
| 712 | } |
| 713 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 714 | /* FIXME: parse sack frame */ |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 715 | if (ghp->cmd == GPRS_LLC_SACK) { |
Harald Welte | 1b170d1 | 2010-05-13 19:49:06 +0200 | [diff] [blame] | 716 | LOGP(DLLC, LOGL_NOTICE, "Unsupported SACK frame\n"); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 717 | return -EIO; |
| 718 | } |
| 719 | |
| 720 | return 0; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 721 | } |
| 722 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 723 | /* 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] | 724 | int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv) |
| 725 | { |
Harald Welte | fd3fa1d | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 726 | struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); |
Holger Hans Peter Freyther | 3dccda5 | 2011-10-14 23:42:13 +0200 | [diff] [blame] | 727 | struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 728 | struct gprs_llc_hdr_parsed llhp; |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 729 | struct gprs_llc_lle *lle; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 730 | int rc = 0; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 731 | |
Harald Welte | 11d7c10 | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 732 | /* Identifiers from DOWN: NSEI, BVCI, TLLI */ |
| 733 | |
Holger Hans Peter Freyther | 4752e0c | 2010-05-23 21:33:57 +0800 | [diff] [blame] | 734 | memset(&llhp, 0, sizeof(llhp)); |
Holger Hans Peter Freyther | fa848d4 | 2010-05-23 21:43:57 +0800 | [diff] [blame] | 735 | 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] | 736 | gprs_llc_hdr_dump(&llhp); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 737 | if (rc < 0) { |
Harald Welte | 1b170d1 | 2010-05-13 19:49:06 +0200 | [diff] [blame] | 738 | LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n"); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 739 | return rc; |
| 740 | } |
| 741 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 742 | switch (gprs_tlli_type(msgb_tlli(msg))) { |
| 743 | case TLLI_LOCAL: |
| 744 | case TLLI_FOREIGN: |
| 745 | case TLLI_RANDOM: |
| 746 | case TLLI_AUXILIARY: |
| 747 | break; |
| 748 | default: |
| 749 | LOGP(DLLC, LOGL_ERROR, |
| 750 | "Discarding frame with strange TLLI type\n"); |
| 751 | break; |
| 752 | } |
| 753 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 754 | /* find the LLC Entity for this TLLI+SAPI tuple */ |
| 755 | lle = lle_by_tlli_sapi(msgb_tlli(msg), llhp.sapi); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 756 | |
| 757 | /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded, |
| 758 | * except UID and XID frames with SAPI=1 */ |
Harald Welte | d764c06 | 2010-05-18 12:45:08 +0200 | [diff] [blame] | 759 | if (!lle) { |
| 760 | if (llhp.sapi == GPRS_SAPI_GMM && |
| 761 | (llhp.cmd == GPRS_LLC_XID || llhp.cmd == GPRS_LLC_UI)) { |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 762 | struct gprs_llc_llme *llme; |
Harald Welte | d764c06 | 2010-05-18 12:45:08 +0200 | [diff] [blame] | 763 | /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 764 | llme = llme_alloc(msgb_tlli(msg)); |
Dieter Spaar | 1f447fb | 2011-07-27 23:38:46 +0200 | [diff] [blame] | 765 | LOGP(DLLC, LOGL_DEBUG, "LLC RX: unknown TLLI 0x%08x, " |
Harald Welte | f0901f0 | 2010-12-26 10:39:26 +0100 | [diff] [blame] | 766 | "creating LLME on the fly\n", msgb_tlli(msg)); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 767 | lle = &llme->lle[llhp.sapi]; |
Harald Welte | d764c06 | 2010-05-18 12:45:08 +0200 | [diff] [blame] | 768 | } else { |
| 769 | LOGP(DLLC, LOGL_NOTICE, |
| 770 | "unknown TLLI/SAPI: Silently dropping\n"); |
| 771 | return 0; |
| 772 | } |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 773 | } |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 774 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 775 | /* decrypt information field + FCS, if needed! */ |
| 776 | if (llhp.is_encrypted) { |
| 777 | uint32_t iov_ui = 0; /* FIXME: randomly select for TLLI */ |
| 778 | uint16_t crypt_len = llhp.data_len + 3; |
| 779 | uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK]; |
| 780 | uint32_t iv; |
| 781 | uint64_t kc = *(uint64_t *)&lle->llme->kc; |
| 782 | int rc, i; |
| 783 | |
| 784 | if (lle->llme->algo == GPRS_ALGO_GEA0) { |
| 785 | LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that " |
| 786 | "has no KC/Algo! Dropping.\n"); |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | iv = gprs_cipher_gen_input_ui(iov_ui, lle->sapi, llhp.seq_tx, |
| 791 | lle->oc_ui_recv); |
| 792 | rc = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo, |
| 793 | kc, iv, GPRS_CIPH_MS2SGSN); |
| 794 | if (rc < 0) { |
| 795 | LOGP(DLLC, LOGL_ERROR, "Error decrypting frame: %d\n", |
| 796 | rc); |
| 797 | return rc; |
| 798 | } |
| 799 | |
| 800 | /* XOR the cipher output with the information field + FCS */ |
| 801 | for (i = 0; i < crypt_len; i++) |
| 802 | *(llhp.data + i) ^= cipher_out[i]; |
| 803 | } else { |
| 804 | if (lle->llme->algo != GPRS_ALGO_GEA0) { |
| 805 | LOGP(DLLC, LOGL_NOTICE, "unencrypted frame for LLC " |
| 806 | "that is supposed to be encrypted. Dropping.\n"); |
| 807 | return 0; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | /* We have to do the FCS check _after_ decryption */ |
Harald Welte | 1b8827a | 2010-06-30 23:15:57 +0200 | [diff] [blame] | 812 | llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length); |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 813 | if (llhp.fcs != llhp.fcs_calc) { |
| 814 | LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n"); |
| 815 | return -EIO; |
| 816 | } |
| 817 | |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 818 | /* Update LLE's (BVCI, NSEI) tuple */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 819 | lle->llme->bvci = msgb_bvci(msg); |
| 820 | lle->llme->nsei = msgb_nsei(msg); |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 821 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 822 | /* Receive and Process the actual LLC frame */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 823 | rc = gprs_llc_hdr_rx(&llhp, lle); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 824 | if (rc < 0) |
| 825 | return rc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 826 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 827 | /* 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] | 828 | if (llhp.data) { |
Harald Welte | 943c5bc | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 829 | msgb_gmmh(msg) = llhp.data; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 830 | switch (llhp.sapi) { |
| 831 | case GPRS_SAPI_GMM: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 832 | /* send LL_UNITDATA_IND to GMM */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 833 | rc = gsm0408_gprs_rcvmsg(msg, lle->llme); |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 834 | break; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 835 | case GPRS_SAPI_SNDCP3: |
| 836 | case GPRS_SAPI_SNDCP5: |
| 837 | case GPRS_SAPI_SNDCP9: |
| 838 | case GPRS_SAPI_SNDCP11: |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 839 | /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */ |
| 840 | rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len); |
| 841 | break; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 842 | case GPRS_SAPI_SMS: |
| 843 | /* FIXME */ |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 844 | case GPRS_SAPI_TOM2: |
| 845 | case GPRS_SAPI_TOM8: |
| 846 | /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */ |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 847 | default: |
Harald Welte | c6ecafe | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 848 | LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi); |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 849 | rc = -EINVAL; |
| 850 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 854 | return rc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 855 | } |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 856 | |
| 857 | /* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */ |
| 858 | int gprs_llgmm_assign(struct gprs_llc_llme *llme, |
| 859 | uint32_t old_tlli, uint32_t new_tlli, |
| 860 | enum gprs_ciph_algo alg, const uint8_t *kc) |
| 861 | { |
| 862 | unsigned int i; |
| 863 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 864 | /* Update the crypto parameters */ |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 865 | llme->algo = alg; |
Harald Welte | 3e2e159 | 2010-06-30 23:19:23 +0200 | [diff] [blame] | 866 | if (alg != GPRS_ALGO_GEA0) |
| 867 | memcpy(llme->kc, kc, sizeof(llme->kc)); |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 868 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 869 | if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) { |
| 870 | /* TLLI Assignment 8.3.1 */ |
| 871 | /* New TLLI shall be assigned and used when (re)transmitting LLC frames */ |
| 872 | /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI |
| 873 | * old is unassigned. Only TLLI new shall be accepted when |
| 874 | * received from peer. */ |
Harald Welte | 875840c | 2010-07-01 11:54:31 +0200 | [diff] [blame] | 875 | if (llme->old_tlli != 0xffffffff) { |
| 876 | llme->old_tlli = 0xffffffff; |
| 877 | llme->tlli = new_tlli; |
| 878 | } else { |
| 879 | /* If TLLI old == 0xffffffff was assigned to LLME, then this is |
| 880 | * TLLI assignmemt according to 8.3.1 */ |
| 881 | llme->old_tlli = 0xffffffff; |
| 882 | llme->tlli = new_tlli; |
| 883 | llme->state = GPRS_LLMS_ASSIGNED; |
| 884 | /* 8.5.3.1 For all LLE's */ |
| 885 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) { |
| 886 | struct gprs_llc_lle *l = &llme->lle[i]; |
| 887 | l->vu_send = l->vu_recv = 0; |
| 888 | l->retrans_ctr = 0; |
| 889 | l->state = GPRS_LLES_ASSIGNED_ADM; |
| 890 | /* FIXME Set parameters according to table 9 */ |
| 891 | } |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 892 | } |
| 893 | } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) { |
| 894 | /* TLLI Change 8.3.2 */ |
| 895 | /* Both TLLI Old and TLLI New are assigned; use New when |
| 896 | * (re)transmitting. Accept toth Old and New on Rx */ |
| 897 | llme->old_tlli = llme->tlli; |
| 898 | llme->tlli = new_tlli; |
| 899 | llme->state = GPRS_LLMS_ASSIGNED; |
| 900 | } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) { |
| 901 | /* TLLI Unassignment 8.3.3) */ |
| 902 | llme->tlli = llme->old_tlli = 0; |
| 903 | llme->state = GPRS_LLMS_UNASSIGNED; |
| 904 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) { |
| 905 | struct gprs_llc_lle *l = &llme->lle[i]; |
| 906 | l->state = GPRS_LLES_UNASSIGNED; |
| 907 | } |
Harald Welte | f7fef48 | 2010-06-28 22:18:26 +0200 | [diff] [blame] | 908 | llme_free(llme); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 909 | } else |
| 910 | return -EINVAL; |
| 911 | |
| 912 | return 0; |
| 913 | } |
Harald Welte | 496aee4 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 914 | |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 915 | /* Chapter 7.2.1.2 LLGMM-RESET.req */ |
| 916 | int gprs_llgmm_reset(struct gprs_llc_llme *llme) |
| 917 | { |
| 918 | struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID"); |
| 919 | int random = rand(); |
| 920 | |
| 921 | /* First XID component must be RESET */ |
| 922 | msgb_put_xid_par(msg, GPRS_LLC_XID_T_RESET, 0, NULL); |
| 923 | /* randomly select new IOV-UI */ |
Harald Welte | 066a0f5 | 2011-10-16 18:59:20 +0200 | [diff] [blame] | 924 | 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] | 925 | |
| 926 | /* FIXME: Start T200, wait for XID response */ |
| 927 | return gprs_llc_tx_xid(&llme->lle[1], msg, 1); |
| 928 | } |
| 929 | |
Harald Welte | 496aee4 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 930 | int gprs_llc_init(const char *cipher_plugin_path) |
| 931 | { |
| 932 | return gprs_cipher_load(cipher_plugin_path); |
| 933 | } |