Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1 | /* GPRS LLC protocol implementation as per 3GPP TS 04.64 */ |
| 2 | |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 3 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 16 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <errno.h> |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 23 | #include <stdint.h> |
Max | 4bc2821 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 24 | #include <stdbool.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 25 | |
Max | 6d99958 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 26 | #include <openssl/rand.h> |
| 27 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 28 | #include <osmocom/core/msgb.h> |
| 29 | #include <osmocom/core/linuxlist.h> |
| 30 | #include <osmocom/core/timer.h> |
| 31 | #include <osmocom/core/talloc.h> |
Alexander Couzens | b7d11e3 | 2016-07-05 11:04:27 +0200 | [diff] [blame] | 32 | #include <osmocom/core/rate_ctr.h> |
Harald Welte | cfb6b28 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 33 | #include <osmocom/gprs/gprs_bssgp.h> |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 34 | |
| 35 | #include <openbsc/gsm_data.h> |
| 36 | #include <openbsc/debug.h> |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 37 | #include <openbsc/gprs_sgsn.h> |
| 38 | #include <openbsc/gprs_gmm.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 39 | #include <openbsc/gprs_llc.h> |
| 40 | #include <openbsc/crc24.h> |
Holger Hans Peter Freyther | f9839f6 | 2011-10-14 23:42:13 +0200 | [diff] [blame] | 41 | #include <openbsc/sgsn.h> |
Philipp | 9985f47 | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 42 | #include <openbsc/gprs_llc_xid.h> |
Philipp | 91df16c | 2016-08-26 17:00:21 +0200 | [diff] [blame^] | 43 | #include <openbsc/gprs_sndcp_comp.h> |
Philipp | 9985f47 | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 44 | #include <openbsc/gprs_sndcp.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 45 | |
Holger Hans Peter Freyther | 1a3fff2 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 46 | static struct gprs_llc_llme *llme_alloc(uint32_t tlli); |
Philipp | 9985f47 | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 47 | static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg, |
| 48 | int command); |
| 49 | static int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, |
| 50 | int command, enum gprs_llc_u_cmd u_cmd, int pf_bit); |
| 51 | |
| 52 | /* BEGIN XID RELATED */ |
| 53 | |
| 54 | /* Generate XID message */ |
| 55 | static int gprs_llc_generate_xid(uint8_t *bytes, int bytes_len, |
| 56 | struct gprs_llc_xid_field *l3_xid_field, |
| 57 | struct gprs_llc_llme *llme) |
| 58 | { |
| 59 | /* Note: Called by gprs_ll_xid_req() */ |
| 60 | |
| 61 | LLIST_HEAD(xid_fields); |
| 62 | |
| 63 | struct gprs_llc_xid_field xid_version; |
| 64 | struct gprs_llc_xid_field xid_n201u; |
| 65 | struct gprs_llc_xid_field xid_n201i; |
| 66 | |
| 67 | xid_version.type = GPRS_LLC_XID_T_VERSION; |
| 68 | xid_version.data = (uint8_t *) "\x00"; |
| 69 | xid_version.data_len = 1; |
| 70 | |
| 71 | xid_n201u.type = GPRS_LLC_XID_T_N201_U; |
| 72 | xid_n201u.data = (uint8_t *) "\x05\xf0"; |
| 73 | xid_n201u.data_len = 2; |
| 74 | |
| 75 | xid_n201i.type = GPRS_LLC_XID_T_N201_I; |
| 76 | xid_n201i.data = (uint8_t *) "\x05\xf0"; |
| 77 | xid_n201i.data_len = 2; |
| 78 | |
| 79 | /* Add locally managed XID Fields */ |
| 80 | llist_add(&xid_n201i.list, &xid_fields); |
| 81 | llist_add(&xid_n201u.list, &xid_fields); |
| 82 | llist_add(&xid_version.list, &xid_fields); |
| 83 | |
| 84 | /* Append layer 3 XID field (if present) */ |
| 85 | if (l3_xid_field) { |
| 86 | /* Enforce layer 3 XID type (just to be sure) */ |
| 87 | l3_xid_field->type = GPRS_LLC_XID_T_L3_PAR; |
| 88 | |
| 89 | /* Add Layer 3 XID field to the list */ |
| 90 | llist_add(&l3_xid_field->list, &xid_fields); |
| 91 | } |
| 92 | |
| 93 | /* Store generated XID for later reference */ |
| 94 | talloc_free(llme->xid); |
| 95 | llme->xid = gprs_llc_copy_xid(llme, &xid_fields); |
| 96 | |
| 97 | return gprs_llc_compile_xid(bytes, bytes_len, &xid_fields); |
| 98 | } |
| 99 | |
| 100 | /* Generate XID message that will cause the GMM to reset */ |
| 101 | static int gprs_llc_generate_xid_for_gmm_reset(uint8_t *bytes, |
| 102 | int bytes_len, uint32_t iov_ui, |
| 103 | struct gprs_llc_llme *llme) |
| 104 | { |
| 105 | /* Called by gprs_llgmm_reset() and |
| 106 | * gprs_llgmm_reset_oldmsg() */ |
| 107 | |
| 108 | LLIST_HEAD(xid_fields); |
| 109 | |
| 110 | struct gprs_llc_xid_field xid_reset; |
| 111 | struct gprs_llc_xid_field xid_iovui; |
| 112 | |
| 113 | /* First XID component must be RESET */ |
| 114 | xid_reset.type = GPRS_LLC_XID_T_RESET; |
| 115 | xid_reset.data = NULL; |
| 116 | xid_reset.data_len = 0; |
| 117 | |
| 118 | /* Add new IOV-UI */ |
| 119 | xid_iovui.type = GPRS_LLC_XID_T_IOV_UI; |
| 120 | xid_iovui.data = (uint8_t *) & iov_ui; |
| 121 | xid_iovui.data_len = 4; |
| 122 | |
| 123 | /* Add locally managed XID Fields */ |
| 124 | llist_add(&xid_iovui.list, &xid_fields); |
| 125 | llist_add(&xid_reset.list, &xid_fields); |
| 126 | |
| 127 | /* Store generated XID for later reference */ |
| 128 | talloc_free(llme->xid); |
| 129 | llme->xid = gprs_llc_copy_xid(llme, &xid_fields); |
| 130 | |
| 131 | return gprs_llc_compile_xid(bytes, bytes_len, &xid_fields); |
| 132 | } |
| 133 | |
| 134 | /* Process an incoming XID confirmation */ |
| 135 | static int gprs_llc_process_xid_conf(uint8_t *bytes, int bytes_len, |
| 136 | struct gprs_llc_lle *lle) |
| 137 | { |
| 138 | /* Note: This function handles the response of a network originated |
| 139 | * XID-Request. There XID messages reflected by the phone are analyzed |
| 140 | * and processed here. The caller is called by rx_llc_xid(). */ |
| 141 | |
| 142 | struct llist_head *xid_fields; |
| 143 | struct gprs_llc_xid_field *xid_field; |
Philipp | 91df16c | 2016-08-26 17:00:21 +0200 | [diff] [blame^] | 144 | struct gprs_llc_xid_field *xid_field_request; |
| 145 | struct gprs_llc_xid_field *xid_field_request_l3 = NULL; |
| 146 | |
| 147 | /* Pick layer3 XID from the XID request we have sent last */ |
| 148 | if (lle->llme->xid) { |
| 149 | llist_for_each_entry(xid_field_request, lle->llme->xid, list) { |
| 150 | if (xid_field_request->type == GPRS_LLC_XID_T_L3_PAR) |
| 151 | xid_field_request_l3 = xid_field_request; |
| 152 | } |
| 153 | } |
Philipp | 9985f47 | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 154 | |
| 155 | /* Parse and analyze XID-Response */ |
| 156 | xid_fields = gprs_llc_parse_xid(NULL, bytes, bytes_len); |
| 157 | |
| 158 | if (xid_fields) { |
| 159 | |
| 160 | gprs_llc_dump_xid_fields(xid_fields, LOGL_DEBUG); |
| 161 | llist_for_each_entry(xid_field, xid_fields, list) { |
| 162 | |
| 163 | /* Forward SNDCP-XID fields to Layer 3 (SNDCP) */ |
Philipp | 91df16c | 2016-08-26 17:00:21 +0200 | [diff] [blame^] | 164 | if (xid_field->type == GPRS_LLC_XID_T_L3_PAR && |
| 165 | xid_field_request_l3) { |
| 166 | sndcp_sn_xid_conf(xid_field, |
| 167 | xid_field_request_l3, lle); |
Philipp | 9985f47 | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* Process LLC-XID fields: */ |
| 171 | else { |
| 172 | |
| 173 | /* FIXME: Do something more useful with the |
| 174 | * echoed XID-Information. Currently we |
| 175 | * just ignore the response completely and |
| 176 | * by doing so we blindly accept any changes |
| 177 | * the MS might have done to the our XID |
| 178 | * inquiry. There is a remainig risk of |
| 179 | * malfunction! */ |
| 180 | LOGP(DLLC, LOGL_NOTICE, |
| 181 | "Ignoring XID-Field: XID: type=%d, data_len=%d, data=%s\n", |
| 182 | xid_field->type, xid_field->data_len, |
| 183 | osmo_hexdump_nospc(xid_field->data, |
| 184 | xid_field->data_len)); |
| 185 | } |
| 186 | } |
| 187 | talloc_free(xid_fields); |
| 188 | } |
| 189 | |
| 190 | /* Flush pending XID fields */ |
| 191 | talloc_free(lle->llme->xid); |
| 192 | lle->llme->xid = NULL; |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /* Process an incoming XID indication and generate an appropiate response */ |
| 198 | static int gprs_llc_process_xid_ind(uint8_t *bytes_request, |
| 199 | int bytes_request_len, |
| 200 | uint8_t *bytes_response, |
| 201 | int bytes_response_maxlen, |
| 202 | struct gprs_llc_lle *lle) |
| 203 | { |
| 204 | /* Note: This function computes the response that is sent back to the |
| 205 | * phone when a phone originated XID is received. The function is |
| 206 | * called by rx_llc_xid() */ |
| 207 | |
| 208 | int rc = -EINVAL; |
| 209 | |
| 210 | struct llist_head *xid_fields; |
| 211 | struct llist_head *xid_fields_response; |
| 212 | |
| 213 | struct gprs_llc_xid_field *xid_field; |
| 214 | struct gprs_llc_xid_field *xid_field_response; |
| 215 | |
Philipp | 9985f47 | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 216 | /* Parse and analyze XID-Request */ |
| 217 | xid_fields = |
| 218 | gprs_llc_parse_xid(lle->llme, bytes_request, bytes_request_len); |
| 219 | if (xid_fields) { |
| 220 | xid_fields_response = talloc_zero(lle->llme, struct llist_head); |
| 221 | INIT_LLIST_HEAD(xid_fields_response); |
| 222 | gprs_llc_dump_xid_fields(xid_fields, LOGL_DEBUG); |
| 223 | |
| 224 | /* Process LLC-XID fields: */ |
| 225 | llist_for_each_entry(xid_field, xid_fields, list) { |
| 226 | |
| 227 | if (xid_field->type != GPRS_LLC_XID_T_L3_PAR) { |
| 228 | /* FIXME: Check the incoming XID parameters for |
| 229 | * for validity. Currently we just blindly |
| 230 | * accept all XID fields by just echoing them. |
| 231 | * There is a remaining risk of malfunction |
| 232 | * when a phone submits values which defer from |
| 233 | * the default! */ |
| 234 | LOGP(DLLC, LOGL_NOTICE, |
| 235 | "Echoing XID-Field: XID: type=%d, data_len=%d, data=%s\n", |
| 236 | xid_field->type, xid_field->data_len, |
| 237 | osmo_hexdump_nospc(xid_field->data, |
| 238 | xid_field->data_len)); |
| 239 | xid_field_response = |
| 240 | gprs_llc_dup_xid_field |
| 241 | (lle->llme, xid_field); |
| 242 | llist_add(&xid_field_response->list, |
| 243 | xid_fields_response); |
| 244 | } |
| 245 | } |
| 246 | |
Philipp | 91df16c | 2016-08-26 17:00:21 +0200 | [diff] [blame^] | 247 | /* Forward SNDCP-XID fields to Layer 3 (SNDCP) */ |
| 248 | llist_for_each_entry(xid_field, xid_fields, list) { |
| 249 | if (xid_field->type == GPRS_LLC_XID_T_L3_PAR) { |
| 250 | |
| 251 | xid_field_response = |
| 252 | talloc_zero(lle->llme, |
| 253 | struct gprs_llc_xid_field); |
| 254 | rc = sndcp_sn_xid_ind(xid_field, |
| 255 | xid_field_response, lle); |
| 256 | if (rc == 0) |
| 257 | llist_add(&xid_field_response->list, |
| 258 | xid_fields_response); |
| 259 | else |
| 260 | talloc_free(xid_field_response); |
| 261 | } |
| 262 | } |
| 263 | |
Philipp | 9985f47 | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 264 | rc = gprs_llc_compile_xid(bytes_response, |
| 265 | bytes_response_maxlen, |
| 266 | xid_fields_response); |
| 267 | talloc_free(xid_fields_response); |
| 268 | talloc_free(xid_fields); |
| 269 | } |
| 270 | |
| 271 | return rc; |
| 272 | } |
| 273 | |
| 274 | /* Dispatch XID indications and responses comming from the Phone */ |
| 275 | static void rx_llc_xid(struct gprs_llc_lle *lle, |
| 276 | struct gprs_llc_hdr_parsed *gph) |
| 277 | { |
| 278 | uint8_t response[1024]; |
| 279 | int response_len; |
| 280 | |
| 281 | /* FIXME: 8.5.3.3: check if XID is invalid */ |
| 282 | if (gph->is_cmd) { |
| 283 | LOGP(DLLC, LOGL_NOTICE, |
| 284 | "Received XID indication from phone.\n"); |
| 285 | |
| 286 | struct msgb *resp; |
| 287 | uint8_t *xid; |
| 288 | resp = msgb_alloc_headroom(4096, 1024, "LLC_XID"); |
| 289 | |
| 290 | response_len = |
| 291 | gprs_llc_process_xid_ind(gph->data, gph->data_len, |
| 292 | response, sizeof(response), |
| 293 | lle); |
Philipp | 91df16c | 2016-08-26 17:00:21 +0200 | [diff] [blame^] | 294 | if (response_len < 0) { |
| 295 | LOGP(DLLC, LOGL_ERROR, |
| 296 | "invalid XID indication received!\n"); |
| 297 | } else { |
| 298 | xid = msgb_put(resp, response_len); |
| 299 | memcpy(xid, response, response_len); |
| 300 | } |
Philipp | 9985f47 | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 301 | gprs_llc_tx_xid(lle, resp, 0); |
| 302 | } else { |
| 303 | LOGP(DLLC, LOGL_NOTICE, |
| 304 | "Received XID confirmation from phone.\n"); |
| 305 | gprs_llc_process_xid_conf(gph->data, gph->data_len, lle); |
| 306 | /* FIXME: if we had sent a XID reset, send |
| 307 | * LLGMM-RESET.conf to GMM */ |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | |
| 312 | /* Set of LL-XID negotiation (See also: TS 101 351, Section 7.2.2.4) */ |
| 313 | int gprs_ll_xid_req(struct gprs_llc_lle *lle, |
| 314 | struct gprs_llc_xid_field *l3_xid_field) |
| 315 | { |
| 316 | /* Note: This functions is calle from gprs_sndcp.c */ |
| 317 | |
| 318 | uint8_t xid_bytes[1024];; |
| 319 | int xid_bytes_len; |
| 320 | uint8_t *xid; |
| 321 | struct msgb *msg; |
| 322 | |
| 323 | /* Generate XID */ |
| 324 | xid_bytes_len = |
| 325 | gprs_llc_generate_xid(xid_bytes, sizeof(xid_bytes), |
| 326 | l3_xid_field, lle->llme); |
| 327 | |
| 328 | /* Only perform XID sending if the XID message contains something */ |
| 329 | if (xid_bytes_len > 0) { |
| 330 | /* Transmit XID bytes */ |
| 331 | msg = msgb_alloc_headroom(4096, 1024, "LLC_XID"); |
| 332 | xid = msgb_put(msg, xid_bytes_len); |
| 333 | memcpy(xid, xid_bytes, xid_bytes_len); |
| 334 | LOGP(DLLC, LOGL_NOTICE, "Sending XID request to phone...\n"); |
| 335 | gprs_llc_tx_xid(lle, msg, 1); |
| 336 | } else { |
| 337 | LOGP(DLLC, LOGL_ERROR, |
| 338 | "XID-Message generation failed, XID not sent!\n"); |
| 339 | return -EINVAL; |
| 340 | } |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | /* END XID RELATED */ |
| 345 | |
| 346 | |
| 347 | |
Holger Hans Peter Freyther | 1a3fff2 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 348 | |
Harald Welte | 1e8b946 | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 349 | /* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU |
| 350 | * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */ |
| 351 | static int _bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx) |
| 352 | { |
| 353 | struct bssgp_dl_ud_par dup; |
| 354 | const uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 }; |
| 355 | |
Harald Welte | 2609601 | 2012-07-04 21:53:12 +0200 | [diff] [blame] | 356 | memset(&dup, 0, sizeof(dup)); |
| 357 | /* before we have received some identity from the MS, we might |
| 358 | * not yet have a MMC context (e.g. XID negotiation of primarly |
Philipp | 9985f47 | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 359 | * LLC connection from GMM sapi). */ |
Harald Welte | 2609601 | 2012-07-04 21:53:12 +0200 | [diff] [blame] | 360 | if (mmctx) { |
| 361 | dup.imsi = mmctx->imsi; |
| 362 | dup.drx_parms = mmctx->drx_parms; |
| 363 | dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len; |
| 364 | 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] | 365 | |
| 366 | /* make sure we only send it to the right llme */ |
Harald Welte | c28dc2f | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 367 | OSMO_ASSERT(msgb_tlli(msg) == mmctx->gb.llme->tlli |
| 368 | || msgb_tlli(msg) == mmctx->gb.llme->old_tlli); |
Harald Welte | 2609601 | 2012-07-04 21:53:12 +0200 | [diff] [blame] | 369 | } |
Harald Welte | 1e8b946 | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 370 | memcpy(&dup.qos_profile, qos_profile_default, |
| 371 | sizeof(qos_profile_default)); |
| 372 | |
Harald Welte | a084f12 | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 373 | return bssgp_tx_dl_ud(msg, 1000, &dup); |
Harald Welte | 1e8b946 | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | |
Harald Welte | df270ad | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 377 | /* Section 8.9.9 LLC layer parameter default values */ |
Daniel Willmann | 922303c | 2014-06-27 17:05:48 +0200 | [diff] [blame] | 378 | static const struct gprs_llc_params llc_default_params[NUM_SAPIS] = { |
Harald Welte | df270ad | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 379 | [1] = { |
| 380 | .t200_201 = 5, |
| 381 | .n200 = 3, |
| 382 | .n201_u = 400, |
| 383 | }, |
| 384 | [2] = { |
| 385 | .t200_201 = 5, |
| 386 | .n200 = 3, |
| 387 | .n201_u = 270, |
| 388 | }, |
| 389 | [3] = { |
| 390 | .iov_i_exp = 27, |
| 391 | .t200_201 = 5, |
| 392 | .n200 = 3, |
| 393 | .n201_u = 500, |
| 394 | .n201_i = 1503, |
| 395 | .mD = 1520, |
| 396 | .mU = 1520, |
| 397 | .kD = 16, |
| 398 | .kU = 16, |
| 399 | }, |
| 400 | [5] = { |
| 401 | .iov_i_exp = 27, |
| 402 | .t200_201 = 10, |
| 403 | .n200 = 3, |
| 404 | .n201_u = 500, |
| 405 | .n201_i = 1503, |
| 406 | .mD = 760, |
| 407 | .mU = 760, |
| 408 | .kD = 8, |
| 409 | .kU = 8, |
| 410 | }, |
| 411 | [7] = { |
| 412 | .t200_201 = 20, |
| 413 | .n200 = 3, |
| 414 | .n201_u = 270, |
| 415 | }, |
| 416 | [8] = { |
| 417 | .t200_201 = 20, |
| 418 | .n200 = 3, |
| 419 | .n201_u = 270, |
| 420 | }, |
| 421 | [9] = { |
| 422 | .iov_i_exp = 27, |
| 423 | .t200_201 = 20, |
| 424 | .n200 = 3, |
| 425 | .n201_u = 500, |
| 426 | .n201_i = 1503, |
| 427 | .mD = 380, |
| 428 | .mU = 380, |
| 429 | .kD = 4, |
| 430 | .kU = 4, |
| 431 | }, |
| 432 | [11] = { |
| 433 | .iov_i_exp = 27, |
| 434 | .t200_201 = 40, |
| 435 | .n200 = 3, |
| 436 | .n201_u = 500, |
| 437 | .n201_i = 1503, |
| 438 | .mD = 190, |
| 439 | .mU = 190, |
| 440 | .kD = 2, |
| 441 | .kU = 2, |
| 442 | }, |
| 443 | }; |
| 444 | |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 445 | LLIST_HEAD(gprs_llc_llmes); |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 446 | void *llc_tall_ctx; |
| 447 | |
| 448 | /* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */ |
Holger Hans Peter Freyther | fe5a6e8 | 2013-07-29 09:06:46 +0200 | [diff] [blame] | 449 | 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] | 450 | { |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 451 | struct gprs_llc_llme *llme; |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 452 | |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 453 | llist_for_each_entry(llme, &gprs_llc_llmes, list) { |
| 454 | if (llme->tlli == tlli || llme->old_tlli == tlli) |
| 455 | return &llme->lle[sapi]; |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 456 | } |
| 457 | return NULL; |
| 458 | } |
| 459 | |
Holger Hans Peter Freyther | 49dbcd9 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 460 | struct gprs_llc_lle *gprs_lle_get_or_create(const uint32_t tlli, uint8_t sapi) |
| 461 | { |
| 462 | struct gprs_llc_llme *llme; |
| 463 | struct gprs_llc_lle *lle; |
| 464 | |
| 465 | lle = lle_by_tlli_sapi(tlli, sapi); |
| 466 | if (lle) |
| 467 | return lle; |
| 468 | |
Holger Hans Peter Freyther | 49dbcd9 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 469 | LOGP(DLLC, LOGL_NOTICE, "LLC: unknown TLLI 0x%08x, " |
| 470 | "creating LLME on the fly\n", tlli); |
| 471 | llme = llme_alloc(tlli); |
| 472 | lle = &llme->lle[sapi]; |
| 473 | return lle; |
| 474 | } |
| 475 | |
| 476 | struct llist_head *gprs_llme_list(void) |
| 477 | { |
| 478 | return &gprs_llc_llmes; |
| 479 | } |
| 480 | |
Holger Hans Peter Freyther | 1a3fff2 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 481 | /* lookup LLC Entity for RX based on DLCI (TLLI+SAPI tuple) */ |
| 482 | static struct gprs_llc_lle *lle_for_rx_by_tlli_sapi(const uint32_t tlli, |
| 483 | uint8_t sapi, enum gprs_llc_cmd cmd) |
| 484 | { |
| 485 | struct gprs_llc_lle *lle; |
| 486 | |
| 487 | /* We already know about this TLLI */ |
| 488 | lle = lle_by_tlli_sapi(tlli, sapi); |
| 489 | if (lle) |
| 490 | return lle; |
| 491 | |
| 492 | /* Maybe it is a routing area update but we already know this sapi? */ |
| 493 | if (gprs_tlli_type(tlli) == TLLI_FOREIGN) { |
Jacob Erlbeck | b0fbb84 | 2016-01-04 18:43:32 +0100 | [diff] [blame] | 494 | lle = lle_by_tlli_sapi(tlli, sapi); |
Holger Hans Peter Freyther | 1a3fff2 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 495 | if (lle) { |
| 496 | LOGP(DLLC, LOGL_NOTICE, |
| 497 | "LLC RX: Found a local entry for TLLI 0x%08x\n", |
| 498 | tlli); |
| 499 | return lle; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded, |
| 504 | * except UID and XID frames with SAPI=1 */ |
| 505 | if (sapi == GPRS_SAPI_GMM && |
| 506 | (cmd == GPRS_LLC_XID || cmd == GPRS_LLC_UI)) { |
| 507 | struct gprs_llc_llme *llme; |
| 508 | /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */ |
| 509 | llme = llme_alloc(tlli); |
Daniel Willmann | c9ac973 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 510 | LOGP(DLLC, LOGL_NOTICE, "LLC RX: unknown TLLI 0x%08x, " |
Holger Hans Peter Freyther | 1a3fff2 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 511 | "creating LLME on the fly\n", tlli); |
| 512 | lle = &llme->lle[sapi]; |
| 513 | return lle; |
| 514 | } |
| 515 | |
| 516 | LOGP(DLLC, LOGL_NOTICE, |
| 517 | "unknown TLLI(0x%08x)/SAPI(%d): Silently dropping\n", |
| 518 | tlli, sapi); |
| 519 | return NULL; |
| 520 | } |
| 521 | |
Harald Welte | df270ad | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 522 | static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi) |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 523 | { |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 524 | struct gprs_llc_lle *lle = &llme->lle[sapi]; |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 525 | |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 526 | lle->llme = llme; |
| 527 | lle->sapi = sapi; |
| 528 | lle->state = GPRS_LLES_UNASSIGNED; |
| 529 | |
Harald Welte | df270ad | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 530 | /* Initialize according to parameters */ |
| 531 | memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params)); |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | static struct gprs_llc_llme *llme_alloc(uint32_t tlli) |
| 535 | { |
| 536 | struct gprs_llc_llme *llme; |
| 537 | uint32_t i; |
| 538 | |
| 539 | llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme); |
| 540 | if (!llme) |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 541 | return NULL; |
| 542 | |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 543 | llme->tlli = tlli; |
Harald Welte | b3d84a4 | 2010-07-01 11:54:31 +0200 | [diff] [blame] | 544 | llme->old_tlli = 0xffffffff; |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 545 | llme->state = GPRS_LLMS_UNASSIGNED; |
Jacob Erlbeck | e6140d0 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 546 | llme->age_timestamp = GPRS_LLME_RESET_AGE; |
Max | eb64e3f | 2016-07-06 11:33:04 +0200 | [diff] [blame] | 547 | llme->cksn = GSM_KEY_SEQ_INVAL; |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 548 | |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 549 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) |
| 550 | lle_init(llme, i); |
| 551 | |
| 552 | llist_add(&llme->list, &gprs_llc_llmes); |
| 553 | |
Philipp | 91df16c | 2016-08-26 17:00:21 +0200 | [diff] [blame^] | 554 | llme->comp.proto = gprs_sndcp_comp_alloc(llme); |
| 555 | llme->comp.data = gprs_sndcp_comp_alloc(llme); |
| 556 | |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 557 | return llme; |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 558 | } |
| 559 | |
Harald Welte | 7a62a3a | 2010-06-28 22:18:26 +0200 | [diff] [blame] | 560 | static void llme_free(struct gprs_llc_llme *llme) |
| 561 | { |
Philipp | 91df16c | 2016-08-26 17:00:21 +0200 | [diff] [blame^] | 562 | gprs_sndcp_comp_free(llme->comp.proto); |
| 563 | gprs_sndcp_comp_free(llme->comp.data); |
Philipp | 9985f47 | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 564 | talloc_free(llme->xid); |
Harald Welte | 7a62a3a | 2010-06-28 22:18:26 +0200 | [diff] [blame] | 565 | llist_del(&llme->list); |
| 566 | talloc_free(llme); |
| 567 | } |
| 568 | |
Holger Hans Peter Freyther | 19bb474 | 2014-04-04 12:47:32 +0200 | [diff] [blame] | 569 | #if 0 |
| 570 | /* FIXME: Unused code... */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 571 | static void t200_expired(void *data) |
| 572 | { |
| 573 | struct gprs_llc_lle *lle = data; |
| 574 | |
| 575 | /* 8.5.1.3: Expiry of T200 */ |
| 576 | |
Harald Welte | df270ad | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 577 | if (lle->retrans_ctr >= lle->params.n200) { |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 578 | /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */ |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 579 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | switch (lle->state) { |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 583 | case GPRS_LLES_LOCAL_EST: |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 584 | /* FIXME: retransmit SABM */ |
| 585 | /* FIXME: re-start T200 */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 586 | lle->retrans_ctr++; |
| 587 | break; |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 588 | case GPRS_LLES_LOCAL_REL: |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 589 | /* FIXME: retransmit DISC */ |
| 590 | /* FIXME: re-start T200 */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 591 | lle->retrans_ctr++; |
| 592 | break; |
Holger Hans Peter Freyther | 19bb474 | 2014-04-04 12:47:32 +0200 | [diff] [blame] | 593 | default: |
| 594 | LOGP(DLLC, LOGL_ERROR, "LLC unhandled state: %d\n", lle->state); |
| 595 | break; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | } |
| 599 | |
| 600 | static void t201_expired(void *data) |
| 601 | { |
| 602 | struct gprs_llc_lle *lle = data; |
| 603 | |
Harald Welte | df270ad | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 604 | if (lle->retrans_ctr < lle->params.n200) { |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 605 | /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */ |
| 606 | /* FIXME: set timer T201 */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 607 | lle->retrans_ctr++; |
| 608 | } |
| 609 | } |
Holger Hans Peter Freyther | 19bb474 | 2014-04-04 12:47:32 +0200 | [diff] [blame] | 610 | #endif |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 611 | |
Harald Welte | 439fa72 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 612 | int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command, |
| 613 | enum gprs_llc_u_cmd u_cmd, int pf_bit) |
| 614 | { |
| 615 | uint8_t *fcs, *llch; |
| 616 | uint8_t addr, ctrl; |
| 617 | uint32_t fcs_calc; |
| 618 | |
| 619 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 620 | |
| 621 | /* Address Field */ |
| 622 | addr = sapi & 0xf; |
| 623 | if (command) |
| 624 | addr |= 0x40; |
| 625 | |
| 626 | /* 6.3 Figure 8 */ |
| 627 | ctrl = 0xe0 | u_cmd; |
| 628 | if (pf_bit) |
| 629 | ctrl |= 0x10; |
| 630 | |
| 631 | /* prepend LLC UI header */ |
| 632 | llch = msgb_push(msg, 2); |
| 633 | llch[0] = addr; |
| 634 | llch[1] = ctrl; |
| 635 | |
| 636 | /* append FCS to end of frame */ |
| 637 | fcs = msgb_put(msg, 3); |
| 638 | fcs_calc = gprs_llc_fcs(llch, fcs - llch); |
| 639 | fcs[0] = fcs_calc & 0xff; |
| 640 | fcs[1] = (fcs_calc >> 8) & 0xff; |
| 641 | fcs[2] = (fcs_calc >> 16) & 0xff; |
| 642 | |
| 643 | /* Identifiers passed down: (BVCI, NSEI) */ |
| 644 | |
Alexander Couzens | b7d11e3 | 2016-07-05 11:04:27 +0200 | [diff] [blame] | 645 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_PACKETS]); |
| 646 | rate_ctr_add(&sgsn->rate_ctrs->ctr[CTR_LLC_DL_BYTES], msg->len); |
| 647 | |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 648 | /* Send BSSGP-DL-UNITDATA.req */ |
Harald Welte | a62e9fe | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 649 | return _bssgp_tx_dl_ud(msg, NULL); |
Harald Welte | 439fa72 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* Send XID response to LLE */ |
Harald Welte | ac687be | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 653 | static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg, |
| 654 | int command) |
Harald Welte | 439fa72 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 655 | { |
| 656 | /* copy identifiers from LLE to ensure lower layers can route */ |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 657 | msgb_tlli(msg) = lle->llme->tlli; |
| 658 | msgb_bvci(msg) = lle->llme->bvci; |
| 659 | msgb_nsei(msg) = lle->llme->nsei; |
Harald Welte | 439fa72 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 660 | |
Harald Welte | ac687be | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 661 | 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] | 662 | } |
| 663 | |
Max | 8f1c636 | 2016-07-11 12:42:12 +0200 | [diff] [blame] | 664 | /* encrypt information field + FCS, if needed! */ |
| 665 | static int apply_gea(struct gprs_llc_lle *lle, uint16_t crypt_len, uint16_t nu, |
| 666 | uint32_t oc, uint8_t sapi, uint8_t *fcs, uint8_t *data) |
| 667 | { |
| 668 | uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK]; |
| 669 | |
| 670 | if (lle->llme->algo == GPRS_ALGO_GEA0) |
| 671 | return -EINVAL; |
| 672 | |
| 673 | /* Compute the 'Input' Paraemeter */ |
Dieter Spaar | 5236a9e | 2016-07-11 12:48:07 +0200 | [diff] [blame] | 674 | uint32_t fcs_calc, iv = gprs_cipher_gen_input_ui(lle->llme->iov_ui, sapi, |
Max | 8f1c636 | 2016-07-11 12:42:12 +0200 | [diff] [blame] | 675 | nu, oc); |
| 676 | /* Compute gamma that we need to XOR with the data */ |
| 677 | int r = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo, |
| 678 | lle->llme->kc, iv, |
| 679 | fcs ? GPRS_CIPH_SGSN2MS : GPRS_CIPH_MS2SGSN); |
| 680 | if (r < 0) { |
| 681 | LOGP(DLLC, LOGL_ERROR, "Error producing %s gamma for UI " |
| 682 | "frame: %d\n", get_value_string(gprs_cipher_names, |
| 683 | lle->llme->algo), r); |
| 684 | return -ENOMSG; |
| 685 | } |
| 686 | |
| 687 | if (fcs) { |
Dieter Spaar | 5236a9e | 2016-07-11 12:48:07 +0200 | [diff] [blame] | 688 | /* Mark frame as encrypted and update FCS */ |
| 689 | data[2] |= 0x02; |
| 690 | fcs_calc = gprs_llc_fcs(data, fcs - data); |
| 691 | fcs[0] = fcs_calc & 0xff; |
| 692 | fcs[1] = (fcs_calc >> 8) & 0xff; |
| 693 | fcs[2] = (fcs_calc >> 16) & 0xff; |
Max | 8f1c636 | 2016-07-11 12:42:12 +0200 | [diff] [blame] | 694 | data += 3; |
| 695 | } |
| 696 | |
| 697 | /* XOR the cipher output with the data */ |
| 698 | for (r = 0; r < crypt_len; r++) |
| 699 | *(data + r) ^= cipher_out[r]; |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
Max | 4bc2821 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 704 | /* Transmit a UI frame over the given SAPI: |
| 705 | 'encryptable' indicates whether particular message can be encrypted according |
| 706 | to 3GPP TS 24.008 § 4.7.1.2 |
| 707 | */ |
Harald Welte | cca80c9 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 708 | int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command, |
Max | 4bc2821 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 709 | struct sgsn_mm_ctx *mmctx, bool encryptable) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 710 | { |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 711 | struct gprs_llc_lle *lle; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 712 | uint8_t *fcs, *llch; |
| 713 | uint8_t addr, ctrl[2]; |
| 714 | uint32_t fcs_calc; |
| 715 | uint16_t nu = 0; |
Harald Welte | 222b068 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 716 | uint32_t oc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 717 | |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 718 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 719 | |
| 720 | /* 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] | 721 | lle = gprs_lle_get_or_create(msgb_tlli(msg), sapi); |
Harald Welte | df270ad | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 722 | |
| 723 | if (msg->len > lle->params.n201_u) { |
| 724 | LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n", |
| 725 | msg->len, lle->params.n201_u); |
Holger Hans Peter Freyther | 13293f3 | 2014-10-10 17:35:54 +0200 | [diff] [blame] | 726 | msgb_free(msg); |
Harald Welte | df270ad | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 727 | return -EFBIG; |
| 728 | } |
| 729 | |
Max | eb64e3f | 2016-07-06 11:33:04 +0200 | [diff] [blame] | 730 | gprs_llme_copy_key(mmctx, lle->llme); |
| 731 | |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 732 | /* Update LLE's (BVCI, NSEI) tuple */ |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 733 | lle->llme->bvci = msgb_bvci(msg); |
| 734 | lle->llme->nsei = msgb_nsei(msg); |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 735 | |
Harald Welte | 222b068 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 736 | /* Obtain current values for N(u) and OC */ |
Harald Welte | 02343f9 | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 737 | nu = lle->vu_send; |
Harald Welte | 222b068 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 738 | oc = lle->oc_ui_send; |
| 739 | /* Increment V(U) */ |
Harald Welte | 02343f9 | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 740 | lle->vu_send = (lle->vu_send + 1) % 512; |
Harald Welte | 222b068 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 741 | /* Increment Overflow Counter, if needed */ |
| 742 | if ((lle->vu_send + 1) / 512) |
| 743 | lle->oc_ui_send += 512; |
Harald Welte | 02343f9 | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 744 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 745 | /* Address Field */ |
| 746 | addr = sapi & 0xf; |
| 747 | if (command) |
| 748 | addr |= 0x40; |
| 749 | |
| 750 | /* Control Field */ |
| 751 | ctrl[0] = 0xc0; |
| 752 | ctrl[0] |= nu >> 6; |
| 753 | ctrl[1] = (nu << 2) & 0xfc; |
| 754 | ctrl[1] |= 0x01; /* Protected Mode */ |
| 755 | |
| 756 | /* prepend LLC UI header */ |
| 757 | llch = msgb_push(msg, 3); |
| 758 | llch[0] = addr; |
| 759 | llch[1] = ctrl[0]; |
| 760 | llch[2] = ctrl[1]; |
| 761 | |
| 762 | /* append FCS to end of frame */ |
| 763 | fcs = msgb_put(msg, 3); |
| 764 | fcs_calc = gprs_llc_fcs(llch, fcs - llch); |
| 765 | fcs[0] = fcs_calc & 0xff; |
| 766 | fcs[1] = (fcs_calc >> 8) & 0xff; |
| 767 | fcs[2] = (fcs_calc >> 16) & 0xff; |
| 768 | |
Max | 4bc2821 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 769 | if (lle->llme->algo != GPRS_ALGO_GEA0 && encryptable) { |
Max | 8f1c636 | 2016-07-11 12:42:12 +0200 | [diff] [blame] | 770 | int rc = apply_gea(lle, fcs - llch, nu, oc, sapi, fcs, llch); |
Harald Welte | 222b068 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 771 | if (rc < 0) { |
Holger Hans Peter Freyther | 13293f3 | 2014-10-10 17:35:54 +0200 | [diff] [blame] | 772 | msgb_free(msg); |
Harald Welte | 222b068 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 773 | return rc; |
| 774 | } |
Harald Welte | 222b068 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 775 | } |
| 776 | |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 777 | /* Identifiers passed down: (BVCI, NSEI) */ |
| 778 | |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 779 | /* Send BSSGP-DL-UNITDATA.req */ |
Harald Welte | 1e8b946 | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 780 | return _bssgp_tx_dl_ud(msg, mmctx); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 781 | } |
| 782 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 783 | static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph, |
| 784 | struct gprs_llc_lle *lle) |
| 785 | { |
| 786 | switch (gph->cmd) { |
| 787 | case GPRS_LLC_SABM: /* Section 6.4.1.1 */ |
| 788 | lle->v_sent = lle->v_ack = lle->v_recv = 0; |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 789 | if (lle->state == GPRS_LLES_ASSIGNED_ADM) { |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 790 | /* start re-establishment (8.7.1) */ |
| 791 | } |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 792 | lle->state = GPRS_LLES_REMOTE_EST; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 793 | /* FIXME: Send UA */ |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 794 | lle->state = GPRS_LLES_ABM; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 795 | /* FIXME: process data */ |
| 796 | break; |
| 797 | case GPRS_LLC_DISC: /* Section 6.4.1.2 */ |
| 798 | /* FIXME: Send UA */ |
| 799 | /* terminate ABM */ |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 800 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 801 | break; |
| 802 | case GPRS_LLC_UA: /* Section 6.4.1.3 */ |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 803 | if (lle->state == GPRS_LLES_LOCAL_EST) |
| 804 | lle->state = GPRS_LLES_ABM; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 805 | break; |
| 806 | 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] | 807 | if (lle->state == GPRS_LLES_LOCAL_EST) |
| 808 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 809 | break; |
| 810 | case GPRS_LLC_FRMR: /* Section 6.4.1.5 */ |
| 811 | break; |
| 812 | case GPRS_LLC_XID: /* Section 6.4.1.6 */ |
Harald Welte | ac687be | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 813 | rx_llc_xid(lle, gph); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 814 | break; |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 815 | case GPRS_LLC_UI: |
Holger Hans Peter Freyther | 6721a08 | 2011-06-23 17:53:27 -0400 | [diff] [blame] | 816 | if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) { |
| 817 | LOGP(DLLC, LOGL_NOTICE, |
| 818 | "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] | 819 | lle->llme ? lle->llme->tlli : -1, |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 820 | gph->seq_tx, lle->vu_recv); |
Harald Welte | 0273fab | 2013-06-21 14:06:18 +0200 | [diff] [blame] | 821 | |
| 822 | /* HACK: non-standard recovery handling. If remote LLE |
| 823 | * is re-transmitting the same sequence number for |
Harald Welte | d4b9b02 | 2013-07-21 17:41:46 +0800 | [diff] [blame] | 824 | * three times, don't discard the frame but pass it on |
Harald Welte | 0273fab | 2013-06-21 14:06:18 +0200 | [diff] [blame] | 825 | * and 'learn' the new sequence number */ |
| 826 | if (gph->seq_tx != lle->vu_recv_last) { |
| 827 | lle->vu_recv_last = gph->seq_tx; |
| 828 | lle->vu_recv_duplicates = 0; |
| 829 | } else { |
| 830 | lle->vu_recv_duplicates++; |
| 831 | if (lle->vu_recv_duplicates < 3) |
| 832 | return -EIO; |
| 833 | LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering " |
| 834 | "N(U=%d) after receiving %u duplicates\n", |
| 835 | lle->llme ? lle->llme->tlli : -1, |
| 836 | gph->seq_tx, lle->vu_recv_duplicates); |
| 837 | } |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 838 | } |
| 839 | /* Increment the sequence number that we expect in the next frame */ |
| 840 | lle->vu_recv = (gph->seq_tx + 1) % 512; |
Harald Welte | 222b068 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 841 | /* Increment Overflow Counter */ |
| 842 | if ((gph->seq_tx + 1) / 512) |
| 843 | lle->oc_ui_recv += 512; |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 844 | break; |
Holger Hans Peter Freyther | 19bb474 | 2014-04-04 12:47:32 +0200 | [diff] [blame] | 845 | default: |
| 846 | LOGP(DLLC, LOGL_NOTICE, "Unhandled command: %d\n", gph->cmd); |
| 847 | break; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | return 0; |
| 851 | } |
| 852 | |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 853 | /* 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] | 854 | int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv) |
| 855 | { |
Holger Hans Peter Freyther | f9839f6 | 2011-10-14 23:42:13 +0200 | [diff] [blame] | 856 | struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 857 | struct gprs_llc_hdr_parsed llhp; |
Harald Welte | 439fa72 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 858 | struct gprs_llc_lle *lle; |
Max | 4bc2821 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 859 | bool drop_cipherable = false; |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 860 | int rc = 0; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 861 | |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 862 | /* Identifiers from DOWN: NSEI, BVCI, TLLI */ |
| 863 | |
Holger Hans Peter Freyther | 510ae72 | 2010-05-23 21:33:57 +0800 | [diff] [blame] | 864 | memset(&llhp, 0, sizeof(llhp)); |
Holger Hans Peter Freyther | 6f26151 | 2010-05-23 21:43:57 +0800 | [diff] [blame] | 865 | 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] | 866 | gprs_llc_hdr_dump(&llhp); |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 867 | if (rc < 0) { |
Harald Welte | e349736 | 2010-05-13 19:49:06 +0200 | [diff] [blame] | 868 | LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n"); |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 869 | return rc; |
| 870 | } |
| 871 | |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 872 | switch (gprs_tlli_type(msgb_tlli(msg))) { |
| 873 | case TLLI_LOCAL: |
| 874 | case TLLI_FOREIGN: |
| 875 | case TLLI_RANDOM: |
| 876 | case TLLI_AUXILIARY: |
| 877 | break; |
| 878 | default: |
| 879 | LOGP(DLLC, LOGL_ERROR, |
| 880 | "Discarding frame with strange TLLI type\n"); |
| 881 | break; |
| 882 | } |
| 883 | |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 884 | /* find the LLC Entity for this TLLI+SAPI tuple */ |
Holger Hans Peter Freyther | 1a3fff2 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 885 | 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] | 886 | if (!lle) { |
| 887 | switch (llhp.sapi) { |
| 888 | case GPRS_SAPI_SNDCP3: |
| 889 | case GPRS_SAPI_SNDCP5: |
| 890 | case GPRS_SAPI_SNDCP9: |
| 891 | case GPRS_SAPI_SNDCP11: |
| 892 | /* Ask an upper layer for help. */ |
Alexander Couzens | 81b0513 | 2016-08-30 18:51:50 +0200 | [diff] [blame] | 893 | return gsm0408_gprs_force_reattach_oldmsg(msg, NULL); |
Jacob Erlbeck | ccaa22a | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 894 | default: |
| 895 | break; |
| 896 | } |
Holger Hans Peter Freyther | 1a3fff2 | 2013-07-30 09:29:27 +0200 | [diff] [blame] | 897 | return 0; |
Jacob Erlbeck | ccaa22a | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 898 | } |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 899 | |
Jacob Erlbeck | e6140d0 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 900 | /* reset age computation */ |
| 901 | lle->llme->age_timestamp = GPRS_LLME_RESET_AGE; |
| 902 | |
Harald Welte | 222b068 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 903 | /* decrypt information field + FCS, if needed! */ |
| 904 | if (llhp.is_encrypted) { |
Max | 8f1c636 | 2016-07-11 12:42:12 +0200 | [diff] [blame] | 905 | if (lle->llme->algo != GPRS_ALGO_GEA0) { |
| 906 | rc = apply_gea(lle, llhp.data_len + 3, llhp.seq_tx, |
| 907 | lle->oc_ui_recv, lle->sapi, NULL, |
| 908 | llhp.data); |
| 909 | if (rc < 0) |
| 910 | return rc; |
Dieter Spaar | 5236a9e | 2016-07-11 12:48:07 +0200 | [diff] [blame] | 911 | llhp.fcs = *(llhp.data + llhp.data_len); |
| 912 | llhp.fcs |= *(llhp.data + llhp.data_len + 1) << 8; |
| 913 | llhp.fcs |= *(llhp.data + llhp.data_len + 2) << 16; |
Max | 8f1c636 | 2016-07-11 12:42:12 +0200 | [diff] [blame] | 914 | } else { |
Harald Welte | 222b068 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 915 | LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that " |
| 916 | "has no KC/Algo! Dropping.\n"); |
| 917 | return 0; |
| 918 | } |
Harald Welte | 222b068 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 919 | } else { |
Max | 4bc2821 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 920 | if (lle->llme->algo != GPRS_ALGO_GEA0 && |
| 921 | lle->llme->cksn != GSM_KEY_SEQ_INVAL) |
| 922 | drop_cipherable = true; |
Harald Welte | 222b068 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | /* We have to do the FCS check _after_ decryption */ |
Harald Welte | fa32507 | 2010-06-30 23:15:57 +0200 | [diff] [blame] | 926 | llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length); |
Harald Welte | 222b068 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 927 | if (llhp.fcs != llhp.fcs_calc) { |
| 928 | LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n"); |
| 929 | return -EIO; |
| 930 | } |
| 931 | |
Harald Welte | 439fa72 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 932 | /* Update LLE's (BVCI, NSEI) tuple */ |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 933 | lle->llme->bvci = msgb_bvci(msg); |
| 934 | lle->llme->nsei = msgb_nsei(msg); |
Harald Welte | 439fa72 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 935 | |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 936 | /* Receive and Process the actual LLC frame */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 937 | rc = gprs_llc_hdr_rx(&llhp, lle); |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 938 | if (rc < 0) |
| 939 | return rc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 940 | |
Alexander Couzens | b7d11e3 | 2016-07-05 11:04:27 +0200 | [diff] [blame] | 941 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_LLC_UL_PACKETS]); |
| 942 | rate_ctr_add(&sgsn->rate_ctrs->ctr[CTR_LLC_UL_BYTES], msg->len); |
| 943 | |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 944 | /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */ |
Harald Welte | e423596 | 2015-08-16 15:23:32 +0200 | [diff] [blame] | 945 | if (llhp.cmd == GPRS_LLC_UI && llhp.data && llhp.data_len) { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 946 | msgb_gmmh(msg) = llhp.data; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 947 | switch (llhp.sapi) { |
| 948 | case GPRS_SAPI_GMM: |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 949 | /* send LL_UNITDATA_IND to GMM */ |
Max | 4bc2821 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 950 | rc = gsm0408_gprs_rcvmsg_gb(msg, lle->llme, |
| 951 | drop_cipherable); |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 952 | break; |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 953 | case GPRS_SAPI_SNDCP3: |
| 954 | case GPRS_SAPI_SNDCP5: |
| 955 | case GPRS_SAPI_SNDCP9: |
| 956 | case GPRS_SAPI_SNDCP11: |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 957 | /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */ |
| 958 | rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len); |
| 959 | break; |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 960 | case GPRS_SAPI_SMS: |
| 961 | /* FIXME */ |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 962 | case GPRS_SAPI_TOM2: |
| 963 | case GPRS_SAPI_TOM8: |
| 964 | /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */ |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 965 | default: |
Harald Welte | 24b7887 | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 966 | LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi); |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 967 | rc = -EINVAL; |
| 968 | break; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 972 | return rc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 973 | } |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 974 | |
Max | eb64e3f | 2016-07-06 11:33:04 +0200 | [diff] [blame] | 975 | /* Propagate crypto parameters MM -> LLME */ |
| 976 | void gprs_llme_copy_key(struct sgsn_mm_ctx *mm, struct gprs_llc_llme *llme) |
| 977 | { |
| 978 | if (!mm) |
| 979 | return; |
| 980 | if (mm->ciph_algo != GPRS_ALGO_GEA0) { |
| 981 | llme->algo = mm->ciph_algo; |
| 982 | if (llme->cksn != mm->auth_triplet.key_seq && |
| 983 | mm->auth_triplet.key_seq != GSM_KEY_SEQ_INVAL) { |
| 984 | memcpy(llme->kc, mm->auth_triplet.vec.kc, |
| 985 | gprs_cipher_key_length(mm->ciph_algo)); |
| 986 | llme->cksn = mm->auth_triplet.key_seq; |
| 987 | } |
| 988 | } else |
| 989 | llme->cksn = GSM_KEY_SEQ_INVAL; |
| 990 | } |
| 991 | |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 992 | /* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */ |
| 993 | int gprs_llgmm_assign(struct gprs_llc_llme *llme, |
Max | eb64e3f | 2016-07-06 11:33:04 +0200 | [diff] [blame] | 994 | uint32_t old_tlli, uint32_t new_tlli) |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 995 | { |
| 996 | unsigned int i; |
| 997 | |
| 998 | if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) { |
| 999 | /* TLLI Assignment 8.3.1 */ |
| 1000 | /* New TLLI shall be assigned and used when (re)transmitting LLC frames */ |
| 1001 | /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI |
| 1002 | * old is unassigned. Only TLLI new shall be accepted when |
| 1003 | * received from peer. */ |
Harald Welte | b3d84a4 | 2010-07-01 11:54:31 +0200 | [diff] [blame] | 1004 | if (llme->old_tlli != 0xffffffff) { |
| 1005 | llme->old_tlli = 0xffffffff; |
| 1006 | llme->tlli = new_tlli; |
| 1007 | } else { |
| 1008 | /* If TLLI old == 0xffffffff was assigned to LLME, then this is |
| 1009 | * TLLI assignmemt according to 8.3.1 */ |
| 1010 | llme->old_tlli = 0xffffffff; |
| 1011 | llme->tlli = new_tlli; |
| 1012 | llme->state = GPRS_LLMS_ASSIGNED; |
| 1013 | /* 8.5.3.1 For all LLE's */ |
| 1014 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) { |
| 1015 | struct gprs_llc_lle *l = &llme->lle[i]; |
| 1016 | l->vu_send = l->vu_recv = 0; |
| 1017 | l->retrans_ctr = 0; |
| 1018 | l->state = GPRS_LLES_ASSIGNED_ADM; |
| 1019 | /* FIXME Set parameters according to table 9 */ |
| 1020 | } |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1021 | } |
| 1022 | } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) { |
| 1023 | /* TLLI Change 8.3.2 */ |
| 1024 | /* 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] | 1025 | * (re)transmitting. Accept both Old and New on Rx */ |
Holger Hans Peter Freyther | 1cb1ce2 | 2013-07-31 11:20:37 +0200 | [diff] [blame] | 1026 | llme->old_tlli = old_tlli; |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1027 | llme->tlli = new_tlli; |
| 1028 | llme->state = GPRS_LLMS_ASSIGNED; |
| 1029 | } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) { |
| 1030 | /* TLLI Unassignment 8.3.3) */ |
| 1031 | llme->tlli = llme->old_tlli = 0; |
| 1032 | llme->state = GPRS_LLMS_UNASSIGNED; |
| 1033 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) { |
| 1034 | struct gprs_llc_lle *l = &llme->lle[i]; |
| 1035 | l->state = GPRS_LLES_UNASSIGNED; |
| 1036 | } |
Harald Welte | 7a62a3a | 2010-06-28 22:18:26 +0200 | [diff] [blame] | 1037 | llme_free(llme); |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1038 | } else |
| 1039 | return -EINVAL; |
| 1040 | |
| 1041 | return 0; |
| 1042 | } |
Harald Welte | 81fb358 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 1043 | |
Max | a86f5c0 | 2016-06-28 17:39:20 +0200 | [diff] [blame] | 1044 | /* TLLI unassignment */ |
| 1045 | int gprs_llgmm_unassign(struct gprs_llc_llme *llme) |
| 1046 | { |
Max | eb64e3f | 2016-07-06 11:33:04 +0200 | [diff] [blame] | 1047 | return gprs_llgmm_assign(llme, llme->tlli, 0xffffffff); |
Max | a86f5c0 | 2016-06-28 17:39:20 +0200 | [diff] [blame] | 1048 | } |
| 1049 | |
Harald Welte | ac687be | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 1050 | /* Chapter 7.2.1.2 LLGMM-RESET.req */ |
| 1051 | int gprs_llgmm_reset(struct gprs_llc_llme *llme) |
| 1052 | { |
| 1053 | struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID"); |
Jacob Erlbeck | 956f5c7 | 2014-09-11 14:20:53 +0200 | [diff] [blame] | 1054 | struct gprs_llc_lle *lle = &llme->lle[1]; |
Philipp | 9985f47 | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 1055 | uint8_t xid_bytes[1024]; |
| 1056 | int xid_bytes_len; |
| 1057 | uint8_t *xid; |
Harald Welte | ac687be | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 1058 | |
Philipp | 9985f47 | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 1059 | LOGP(DLLC, LOGL_NOTICE, "LLGM Reset\n"); |
Max | 6d99958 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 1060 | if (RAND_bytes((uint8_t *) &llme->iov_ui, 4) != 1) { |
| 1061 | LOGP(DLLC, LOGL_NOTICE, "RAND_bytes failed for LLC XID reset, " |
| 1062 | "falling back to rand()\n"); |
| 1063 | llme->iov_ui = rand(); |
| 1064 | } |
| 1065 | |
Philipp | 9985f47 | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 1066 | /* Generate XID message */ |
| 1067 | xid_bytes_len = gprs_llc_generate_xid_for_gmm_reset(xid_bytes, |
| 1068 | sizeof(xid_bytes),llme->iov_ui,llme); |
| 1069 | if(xid_bytes_len < 0) |
| 1070 | return -EINVAL; |
| 1071 | xid = msgb_put(msg, xid_bytes_len); |
| 1072 | memcpy(xid, xid_bytes, xid_bytes_len); |
Harald Welte | ac687be | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 1073 | |
Jacob Erlbeck | 956f5c7 | 2014-09-11 14:20:53 +0200 | [diff] [blame] | 1074 | /* Reset some of the LLC parameters. See GSM 04.64, 8.5.3.1 */ |
| 1075 | lle->vu_recv = 0; |
| 1076 | lle->vu_send = 0; |
| 1077 | lle->oc_ui_send = 0; |
| 1078 | lle->oc_ui_recv = 0; |
| 1079 | |
Harald Welte | ac687be | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 1080 | /* FIXME: Start T200, wait for XID response */ |
Jacob Erlbeck | 956f5c7 | 2014-09-11 14:20:53 +0200 | [diff] [blame] | 1081 | return gprs_llc_tx_xid(lle, msg, 1); |
Harald Welte | ac687be | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 1082 | } |
| 1083 | |
Max | 6d99958 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 1084 | int gprs_llgmm_reset_oldmsg(struct msgb* oldmsg, uint8_t sapi, |
| 1085 | struct gprs_llc_llme *llme) |
Jacob Erlbeck | ccaa22a | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 1086 | { |
| 1087 | struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID"); |
Philipp | 9985f47 | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 1088 | uint8_t xid_bytes[1024]; |
| 1089 | int xid_bytes_len; |
| 1090 | uint8_t *xid; |
Max | 6d99958 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 1091 | |
Philipp | 9985f47 | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 1092 | LOGP(DLLC, LOGL_NOTICE, "LLGM Reset\n"); |
Max | 6d99958 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 1093 | if (RAND_bytes((uint8_t *) &llme->iov_ui, 4) != 1) { |
| 1094 | LOGP(DLLC, LOGL_NOTICE, "RAND_bytes failed for LLC XID reset, " |
| 1095 | "falling back to rand()\n"); |
| 1096 | llme->iov_ui = rand(); |
| 1097 | } |
Jacob Erlbeck | ccaa22a | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 1098 | |
Philipp | 9985f47 | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 1099 | /* Generate XID message */ |
| 1100 | xid_bytes_len = gprs_llc_generate_xid_for_gmm_reset(xid_bytes, |
| 1101 | sizeof(xid_bytes),llme->iov_ui,llme); |
| 1102 | if(xid_bytes_len < 0) |
| 1103 | return -EINVAL; |
| 1104 | xid = msgb_put(msg, xid_bytes_len); |
| 1105 | memcpy(xid, xid_bytes, xid_bytes_len); |
Jacob Erlbeck | ccaa22a | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 1106 | |
| 1107 | /* FIXME: Start T200, wait for XID response */ |
| 1108 | |
| 1109 | msgb_tlli(msg) = msgb_tlli(oldmsg); |
| 1110 | msgb_bvci(msg) = msgb_bvci(oldmsg); |
| 1111 | msgb_nsei(msg) = msgb_nsei(oldmsg); |
| 1112 | |
| 1113 | return gprs_llc_tx_u(msg, sapi, 1, GPRS_LLC_U_XID, 1); |
| 1114 | } |
| 1115 | |
Harald Welte | 81fb358 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 1116 | int gprs_llc_init(const char *cipher_plugin_path) |
| 1117 | { |
| 1118 | return gprs_cipher_load(cipher_plugin_path); |
| 1119 | } |