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