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 |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 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 |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <errno.h> |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 24 | #include <stdint.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 25 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 26 | #include <osmocore/msgb.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 27 | #include <osmocore/linuxlist.h> |
| 28 | #include <osmocore/timer.h> |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 29 | #include <osmocore/talloc.h> |
| 30 | |
| 31 | #include <openbsc/gsm_data.h> |
| 32 | #include <openbsc/debug.h> |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 33 | #include <openbsc/gprs_sgsn.h> |
| 34 | #include <openbsc/gprs_gmm.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 35 | #include <openbsc/gprs_bssgp.h> |
| 36 | #include <openbsc/gprs_llc.h> |
| 37 | #include <openbsc/crc24.h> |
| 38 | |
Harald Welte | df270ad | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 39 | /* Section 8.9.9 LLC layer parameter default values */ |
| 40 | static const struct gprs_llc_params llc_default_params[] = { |
| 41 | [1] = { |
| 42 | .t200_201 = 5, |
| 43 | .n200 = 3, |
| 44 | .n201_u = 400, |
| 45 | }, |
| 46 | [2] = { |
| 47 | .t200_201 = 5, |
| 48 | .n200 = 3, |
| 49 | .n201_u = 270, |
| 50 | }, |
| 51 | [3] = { |
| 52 | .iov_i_exp = 27, |
| 53 | .t200_201 = 5, |
| 54 | .n200 = 3, |
| 55 | .n201_u = 500, |
| 56 | .n201_i = 1503, |
| 57 | .mD = 1520, |
| 58 | .mU = 1520, |
| 59 | .kD = 16, |
| 60 | .kU = 16, |
| 61 | }, |
| 62 | [5] = { |
| 63 | .iov_i_exp = 27, |
| 64 | .t200_201 = 10, |
| 65 | .n200 = 3, |
| 66 | .n201_u = 500, |
| 67 | .n201_i = 1503, |
| 68 | .mD = 760, |
| 69 | .mU = 760, |
| 70 | .kD = 8, |
| 71 | .kU = 8, |
| 72 | }, |
| 73 | [7] = { |
| 74 | .t200_201 = 20, |
| 75 | .n200 = 3, |
| 76 | .n201_u = 270, |
| 77 | }, |
| 78 | [8] = { |
| 79 | .t200_201 = 20, |
| 80 | .n200 = 3, |
| 81 | .n201_u = 270, |
| 82 | }, |
| 83 | [9] = { |
| 84 | .iov_i_exp = 27, |
| 85 | .t200_201 = 20, |
| 86 | .n200 = 3, |
| 87 | .n201_u = 500, |
| 88 | .n201_i = 1503, |
| 89 | .mD = 380, |
| 90 | .mU = 380, |
| 91 | .kD = 4, |
| 92 | .kU = 4, |
| 93 | }, |
| 94 | [11] = { |
| 95 | .iov_i_exp = 27, |
| 96 | .t200_201 = 40, |
| 97 | .n200 = 3, |
| 98 | .n201_u = 500, |
| 99 | .n201_i = 1503, |
| 100 | .mD = 190, |
| 101 | .mU = 190, |
| 102 | .kD = 2, |
| 103 | .kU = 2, |
| 104 | }, |
| 105 | }; |
| 106 | |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 107 | LLIST_HEAD(gprs_llc_llmes); |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 108 | void *llc_tall_ctx; |
| 109 | |
| 110 | /* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */ |
Harald Welte | df270ad | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 111 | static struct gprs_llc_lle *lle_by_tlli_sapi(uint32_t tlli, uint8_t sapi) |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 112 | { |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 113 | struct gprs_llc_llme *llme; |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 114 | |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 115 | llist_for_each_entry(llme, &gprs_llc_llmes, list) { |
| 116 | if (llme->tlli == tlli || llme->old_tlli == tlli) |
| 117 | return &llme->lle[sapi]; |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 118 | } |
| 119 | return NULL; |
| 120 | } |
| 121 | |
Harald Welte | df270ad | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 122 | static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi) |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 123 | { |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 124 | struct gprs_llc_lle *lle = &llme->lle[sapi]; |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 125 | |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 126 | lle->llme = llme; |
| 127 | lle->sapi = sapi; |
| 128 | lle->state = GPRS_LLES_UNASSIGNED; |
| 129 | |
Harald Welte | df270ad | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 130 | /* Initialize according to parameters */ |
| 131 | memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params)); |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static struct gprs_llc_llme *llme_alloc(uint32_t tlli) |
| 135 | { |
| 136 | struct gprs_llc_llme *llme; |
| 137 | uint32_t i; |
| 138 | |
| 139 | llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme); |
| 140 | if (!llme) |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 141 | return NULL; |
| 142 | |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 143 | llme->tlli = tlli; |
| 144 | llme->state = GPRS_LLMS_UNASSIGNED; |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 145 | |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 146 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) |
| 147 | lle_init(llme, i); |
| 148 | |
| 149 | llist_add(&llme->list, &gprs_llc_llmes); |
| 150 | |
| 151 | return llme; |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 154 | enum gprs_llc_cmd { |
| 155 | GPRS_LLC_NULL, |
| 156 | GPRS_LLC_RR, |
| 157 | GPRS_LLC_ACK, |
| 158 | GPRS_LLC_RNR, |
| 159 | GPRS_LLC_SACK, |
| 160 | GPRS_LLC_DM, |
| 161 | GPRS_LLC_DISC, |
| 162 | GPRS_LLC_UA, |
| 163 | GPRS_LLC_SABM, |
| 164 | GPRS_LLC_FRMR, |
| 165 | GPRS_LLC_XID, |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 166 | GPRS_LLC_UI, |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 167 | }; |
| 168 | |
Harald Welte | 366c204 | 2010-05-18 12:31:50 +0200 | [diff] [blame] | 169 | static const struct value_string llc_cmd_strs[] = { |
| 170 | { GPRS_LLC_NULL, "NULL" }, |
| 171 | { GPRS_LLC_RR, "RR" }, |
| 172 | { GPRS_LLC_ACK, "ACK" }, |
| 173 | { GPRS_LLC_RNR, "RNR" }, |
| 174 | { GPRS_LLC_SACK, "SACK" }, |
| 175 | { GPRS_LLC_DM, "DM" }, |
| 176 | { GPRS_LLC_DISC, "DISC" }, |
| 177 | { GPRS_LLC_UA, "UA" }, |
| 178 | { GPRS_LLC_SABM, "SABM" }, |
| 179 | { GPRS_LLC_FRMR, "FRMR" }, |
| 180 | { GPRS_LLC_XID, "XID" }, |
| 181 | { GPRS_LLC_UI, "UI" }, |
| 182 | { 0, NULL } |
| 183 | }; |
| 184 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 185 | struct gprs_llc_hdr_parsed { |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 186 | uint8_t sapi; |
| 187 | uint8_t is_cmd:1, |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 188 | ack_req:1, |
| 189 | is_encrypted:1; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 190 | uint32_t seq_rx; |
| 191 | uint32_t seq_tx; |
| 192 | uint32_t fcs; |
| 193 | uint32_t fcs_calc; |
| 194 | uint8_t *data; |
Harald Welte | 9f14f98 | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 195 | uint16_t data_len; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 196 | enum gprs_llc_cmd cmd; |
| 197 | }; |
| 198 | |
| 199 | #define LLC_ALLOC_SIZE 16384 |
| 200 | #define UI_HDR_LEN 3 |
| 201 | #define N202 4 |
| 202 | #define CRC24_LENGTH 3 |
| 203 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 204 | static int gprs_llc_fcs(uint8_t *data, unsigned int len) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 205 | { |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 206 | uint32_t fcs_calc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 207 | |
| 208 | fcs_calc = crc24_calc(INIT_CRC24, data, len); |
| 209 | fcs_calc = ~fcs_calc; |
| 210 | fcs_calc &= 0xffffff; |
| 211 | |
| 212 | return fcs_calc; |
| 213 | } |
| 214 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 215 | static void t200_expired(void *data) |
| 216 | { |
| 217 | struct gprs_llc_lle *lle = data; |
| 218 | |
| 219 | /* 8.5.1.3: Expiry of T200 */ |
| 220 | |
Harald Welte | df270ad | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 221 | if (lle->retrans_ctr >= lle->params.n200) { |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 222 | /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */ |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 223 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | switch (lle->state) { |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 227 | case GPRS_LLES_LOCAL_EST: |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 228 | /* FIXME: retransmit SABM */ |
| 229 | /* FIXME: re-start T200 */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 230 | lle->retrans_ctr++; |
| 231 | break; |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 232 | case GPRS_LLES_LOCAL_REL: |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 233 | /* FIXME: retransmit DISC */ |
| 234 | /* FIXME: re-start T200 */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 235 | lle->retrans_ctr++; |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | } |
| 240 | |
| 241 | static void t201_expired(void *data) |
| 242 | { |
| 243 | struct gprs_llc_lle *lle = data; |
| 244 | |
Harald Welte | df270ad | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 245 | if (lle->retrans_ctr < lle->params.n200) { |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 246 | /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */ |
| 247 | /* FIXME: set timer T201 */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 248 | lle->retrans_ctr++; |
| 249 | } |
| 250 | } |
| 251 | |
Harald Welte | 439fa72 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 252 | int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command, |
| 253 | enum gprs_llc_u_cmd u_cmd, int pf_bit) |
| 254 | { |
| 255 | uint8_t *fcs, *llch; |
| 256 | uint8_t addr, ctrl; |
| 257 | uint32_t fcs_calc; |
| 258 | |
| 259 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 260 | |
| 261 | /* Address Field */ |
| 262 | addr = sapi & 0xf; |
| 263 | if (command) |
| 264 | addr |= 0x40; |
| 265 | |
| 266 | /* 6.3 Figure 8 */ |
| 267 | ctrl = 0xe0 | u_cmd; |
| 268 | if (pf_bit) |
| 269 | ctrl |= 0x10; |
| 270 | |
| 271 | /* prepend LLC UI header */ |
| 272 | llch = msgb_push(msg, 2); |
| 273 | llch[0] = addr; |
| 274 | llch[1] = ctrl; |
| 275 | |
| 276 | /* append FCS to end of frame */ |
| 277 | fcs = msgb_put(msg, 3); |
| 278 | fcs_calc = gprs_llc_fcs(llch, fcs - llch); |
| 279 | fcs[0] = fcs_calc & 0xff; |
| 280 | fcs[1] = (fcs_calc >> 8) & 0xff; |
| 281 | fcs[2] = (fcs_calc >> 16) & 0xff; |
| 282 | |
| 283 | /* Identifiers passed down: (BVCI, NSEI) */ |
| 284 | |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 285 | /* Send BSSGP-DL-UNITDATA.req */ |
Harald Welte | cca80c9 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 286 | return gprs_bssgp_tx_dl_ud(msg, NULL); |
Harald Welte | 439fa72 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /* Send XID response to LLE */ |
| 290 | static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg) |
| 291 | { |
| 292 | /* copy identifiers from LLE to ensure lower layers can route */ |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 293 | msgb_tlli(msg) = lle->llme->tlli; |
| 294 | msgb_bvci(msg) = lle->llme->bvci; |
| 295 | msgb_nsei(msg) = lle->llme->nsei; |
Harald Welte | 439fa72 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 296 | |
| 297 | return gprs_llc_tx_u(msg, lle->sapi, 0, GPRS_LLC_U_XID, 1); |
| 298 | } |
| 299 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 300 | /* Transmit a UI frame over the given SAPI */ |
Harald Welte | cca80c9 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 301 | int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command, |
| 302 | void *mmctx) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 303 | { |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 304 | struct gprs_llc_lle *lle; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 305 | uint8_t *fcs, *llch; |
| 306 | uint8_t addr, ctrl[2]; |
| 307 | uint32_t fcs_calc; |
| 308 | uint16_t nu = 0; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 309 | |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 310 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 311 | |
| 312 | /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */ |
| 313 | lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi); |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 314 | if (!lle) { |
| 315 | struct gprs_llc_llme *llme; |
| 316 | llme = llme_alloc(msgb_tlli(msg)); |
| 317 | lle = &llme->lle[sapi]; |
| 318 | } |
Harald Welte | df270ad | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 319 | |
| 320 | if (msg->len > lle->params.n201_u) { |
| 321 | LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n", |
| 322 | msg->len, lle->params.n201_u); |
| 323 | return -EFBIG; |
| 324 | } |
| 325 | |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 326 | /* Update LLE's (BVCI, NSEI) tuple */ |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 327 | lle->llme->bvci = msgb_bvci(msg); |
| 328 | lle->llme->nsei = msgb_nsei(msg); |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 329 | |
Harald Welte | 02343f9 | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 330 | /* Increment V(U) */ |
| 331 | nu = lle->vu_send; |
| 332 | lle->vu_send = (lle->vu_send + 1) % 512; |
| 333 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 334 | /* Address Field */ |
| 335 | addr = sapi & 0xf; |
| 336 | if (command) |
| 337 | addr |= 0x40; |
| 338 | |
| 339 | /* Control Field */ |
| 340 | ctrl[0] = 0xc0; |
| 341 | ctrl[0] |= nu >> 6; |
| 342 | ctrl[1] = (nu << 2) & 0xfc; |
| 343 | ctrl[1] |= 0x01; /* Protected Mode */ |
| 344 | |
| 345 | /* prepend LLC UI header */ |
| 346 | llch = msgb_push(msg, 3); |
| 347 | llch[0] = addr; |
| 348 | llch[1] = ctrl[0]; |
| 349 | llch[2] = ctrl[1]; |
| 350 | |
| 351 | /* append FCS to end of frame */ |
| 352 | fcs = msgb_put(msg, 3); |
| 353 | fcs_calc = gprs_llc_fcs(llch, fcs - llch); |
| 354 | fcs[0] = fcs_calc & 0xff; |
| 355 | fcs[1] = (fcs_calc >> 8) & 0xff; |
| 356 | fcs[2] = (fcs_calc >> 16) & 0xff; |
| 357 | |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 358 | /* Identifiers passed down: (BVCI, NSEI) */ |
| 359 | |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 360 | /* Send BSSGP-DL-UNITDATA.req */ |
Harald Welte | cca80c9 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 361 | return gprs_bssgp_tx_dl_ud(msg, mmctx); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 362 | } |
| 363 | |
Holger Hans Peter Freyther | 6f8867b | 2010-05-23 21:35:25 +0800 | [diff] [blame] | 364 | static void gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 365 | { |
Harald Welte | 24b7887 | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 366 | DEBUGP(DLLC, "LLC SAPI=%u %c %c FCS=0x%06x(%s) ", |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 367 | gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ', |
| 368 | gph->fcs, gph->fcs_calc == gph->fcs ? "correct" : "WRONG"); |
| 369 | |
| 370 | if (gph->cmd) |
Harald Welte | 366c204 | 2010-05-18 12:31:50 +0200 | [diff] [blame] | 371 | DEBUGPC(DLLC, "CMD=%s ", get_value_string(llc_cmd_strs, gph->cmd)); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 372 | |
| 373 | if (gph->data) |
Harald Welte | 24b7887 | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 374 | DEBUGPC(DLLC, "DATA "); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 375 | |
Harald Welte | 24b7887 | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 376 | DEBUGPC(DLLC, "\n"); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 377 | } |
| 378 | static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph, |
| 379 | struct gprs_llc_lle *lle) |
| 380 | { |
| 381 | switch (gph->cmd) { |
| 382 | case GPRS_LLC_SABM: /* Section 6.4.1.1 */ |
| 383 | lle->v_sent = lle->v_ack = lle->v_recv = 0; |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 384 | if (lle->state == GPRS_LLES_ASSIGNED_ADM) { |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 385 | /* start re-establishment (8.7.1) */ |
| 386 | } |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 387 | lle->state = GPRS_LLES_REMOTE_EST; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 388 | /* FIXME: Send UA */ |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 389 | lle->state = GPRS_LLES_ABM; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 390 | /* FIXME: process data */ |
| 391 | break; |
| 392 | case GPRS_LLC_DISC: /* Section 6.4.1.2 */ |
| 393 | /* FIXME: Send UA */ |
| 394 | /* terminate ABM */ |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 395 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 396 | break; |
| 397 | case GPRS_LLC_UA: /* Section 6.4.1.3 */ |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 398 | if (lle->state == GPRS_LLES_LOCAL_EST) |
| 399 | lle->state = GPRS_LLES_ABM; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 400 | break; |
| 401 | 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] | 402 | if (lle->state == GPRS_LLES_LOCAL_EST) |
| 403 | lle->state = GPRS_LLES_ASSIGNED_ADM; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 404 | break; |
| 405 | case GPRS_LLC_FRMR: /* Section 6.4.1.5 */ |
| 406 | break; |
| 407 | case GPRS_LLC_XID: /* Section 6.4.1.6 */ |
Harald Welte | 9f14f98 | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 408 | /* FIXME: implement XID negotiation using SNDCP */ |
| 409 | { |
| 410 | struct msgb *resp; |
| 411 | uint8_t *xid; |
| 412 | resp = msgb_alloc_headroom(4096, 1024, "LLC_XID"); |
| 413 | xid = msgb_put(resp, gph->data_len); |
| 414 | memcpy(xid, gph->data, gph->data_len); |
| 415 | gprs_llc_tx_xid(lle, resp); |
| 416 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 417 | break; |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 418 | case GPRS_LLC_UI: |
| 419 | if (gph->seq_tx < lle->vu_recv) { |
| 420 | LOGP(DLLC, "TLLI=%08x dropping UI, vurecv %u <= %u\n", |
| 421 | gph->seq_tx, lle->vu_recv); |
| 422 | return -EIO; |
| 423 | } |
| 424 | /* Increment the sequence number that we expect in the next frame */ |
| 425 | lle->vu_recv = (gph->seq_tx + 1) % 512; |
| 426 | break; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | /* parse a GPRS LLC header, also check for invalid frames */ |
| 433 | static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp, |
Holger Hans Peter Freyther | 6f26151 | 2010-05-23 21:43:57 +0800 | [diff] [blame] | 434 | uint8_t *llc_hdr, int len) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 435 | { |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 436 | uint8_t *ctrl = llc_hdr+1; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 437 | int is_sack = 0; |
| 438 | unsigned int crc_length; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 439 | uint32_t fcs_calc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 440 | |
| 441 | if (len <= CRC24_LENGTH) |
| 442 | return -EIO; |
| 443 | |
| 444 | crc_length = len - CRC24_LENGTH; |
| 445 | |
| 446 | ghp->ack_req = 0; |
| 447 | |
| 448 | /* Section 5.5: FCS */ |
| 449 | ghp->fcs = *(llc_hdr + len - 3); |
| 450 | ghp->fcs |= *(llc_hdr + len - 2) << 8; |
| 451 | ghp->fcs |= *(llc_hdr + len - 1) << 16; |
| 452 | |
| 453 | /* Section 6.2.1: invalid PD field */ |
| 454 | if (llc_hdr[0] & 0x80) |
| 455 | return -EIO; |
| 456 | |
| 457 | /* This only works for the MS->SGSN direction */ |
| 458 | if (llc_hdr[0] & 0x40) |
| 459 | ghp->is_cmd = 0; |
| 460 | else |
| 461 | ghp->is_cmd = 1; |
| 462 | |
| 463 | ghp->sapi = llc_hdr[0] & 0xf; |
| 464 | |
| 465 | /* Section 6.2.3: check for reserved SAPI */ |
| 466 | switch (ghp->sapi) { |
| 467 | case 0: |
| 468 | case 4: |
| 469 | case 6: |
| 470 | case 0xa: |
| 471 | case 0xc: |
| 472 | case 0xd: |
| 473 | case 0xf: |
| 474 | return -EINVAL; |
| 475 | } |
| 476 | |
| 477 | if ((ctrl[0] & 0x80) == 0) { |
| 478 | /* I (Information transfer + Supervisory) format */ |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 479 | uint8_t k; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 480 | |
| 481 | ghp->data = ctrl + 3; |
| 482 | |
| 483 | if (ctrl[0] & 0x40) |
| 484 | ghp->ack_req = 1; |
| 485 | |
| 486 | ghp->seq_tx = (ctrl[0] & 0x1f) << 4; |
| 487 | ghp->seq_tx |= (ctrl[1] >> 4); |
| 488 | |
| 489 | ghp->seq_rx = (ctrl[1] & 0x7) << 6; |
| 490 | ghp->seq_rx |= (ctrl[2] >> 2); |
| 491 | |
| 492 | switch (ctrl[2] & 0x03) { |
| 493 | case 0: |
| 494 | ghp->cmd = GPRS_LLC_RR; |
| 495 | break; |
| 496 | case 1: |
| 497 | ghp->cmd = GPRS_LLC_ACK; |
| 498 | break; |
| 499 | case 2: |
| 500 | ghp->cmd = GPRS_LLC_RNR; |
| 501 | break; |
| 502 | case 3: |
| 503 | ghp->cmd = GPRS_LLC_SACK; |
| 504 | k = ctrl[3] & 0x1f; |
| 505 | ghp->data += 1 + k; |
| 506 | break; |
| 507 | } |
Harald Welte | 9f14f98 | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 508 | ghp->data_len = (llc_hdr + len - 3) - ghp->data; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 509 | } else if ((ctrl[0] & 0xc0) == 0x80) { |
| 510 | /* S (Supervisory) format */ |
| 511 | ghp->data = NULL; |
Harald Welte | 9f14f98 | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 512 | ghp->data_len = 0; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 513 | |
| 514 | if (ctrl[0] & 0x20) |
| 515 | ghp->ack_req = 1; |
| 516 | ghp->seq_rx = (ctrl[0] & 0x7) << 6; |
| 517 | ghp->seq_rx |= (ctrl[1] >> 2); |
| 518 | |
| 519 | switch (ctrl[1] & 0x03) { |
| 520 | case 0: |
| 521 | ghp->cmd = GPRS_LLC_RR; |
| 522 | break; |
| 523 | case 1: |
| 524 | ghp->cmd = GPRS_LLC_ACK; |
| 525 | break; |
| 526 | case 2: |
| 527 | ghp->cmd = GPRS_LLC_RNR; |
| 528 | break; |
| 529 | case 3: |
| 530 | ghp->cmd = GPRS_LLC_SACK; |
| 531 | break; |
| 532 | } |
| 533 | } else if ((ctrl[0] & 0xe0) == 0xc0) { |
| 534 | /* UI (Unconfirmed Inforamtion) format */ |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 535 | ghp->cmd = GPRS_LLC_UI; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 536 | ghp->data = ctrl + 2; |
Harald Welte | 9f14f98 | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 537 | ghp->data_len = (llc_hdr + len - 3) - ghp->data; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 538 | |
| 539 | ghp->seq_tx = (ctrl[0] & 0x7) << 6; |
| 540 | ghp->seq_tx |= (ctrl[1] >> 2); |
| 541 | if (ctrl[1] & 0x02) { |
| 542 | ghp->is_encrypted = 1; |
| 543 | /* FIXME: encryption */ |
| 544 | } |
| 545 | if (ctrl[1] & 0x01) { |
| 546 | /* FCS over hdr + all inf fields */ |
| 547 | } else { |
| 548 | /* FCS over hdr + N202 octets (4) */ |
| 549 | if (crc_length > UI_HDR_LEN + N202) |
| 550 | crc_length = UI_HDR_LEN + N202; |
| 551 | } |
| 552 | } else { |
| 553 | /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */ |
| 554 | ghp->data = NULL; |
Harald Welte | 9f14f98 | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 555 | ghp->data_len = 0; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 556 | |
| 557 | switch (ctrl[0] & 0xf) { |
Harald Welte | 9f14f98 | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 558 | case GPRS_LLC_U_NULL_CMD: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 559 | ghp->cmd = GPRS_LLC_NULL; |
| 560 | break; |
Harald Welte | 9f14f98 | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 561 | case GPRS_LLC_U_DM_RESP: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 562 | ghp->cmd = GPRS_LLC_DM; |
| 563 | break; |
Harald Welte | 9f14f98 | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 564 | case GPRS_LLC_U_DISC_CMD: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 565 | ghp->cmd = GPRS_LLC_DISC; |
| 566 | break; |
Harald Welte | 9f14f98 | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 567 | case GPRS_LLC_U_UA_RESP: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 568 | ghp->cmd = GPRS_LLC_UA; |
| 569 | break; |
Harald Welte | 9f14f98 | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 570 | case GPRS_LLC_U_SABM_CMD: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 571 | ghp->cmd = GPRS_LLC_SABM; |
| 572 | break; |
Harald Welte | 9f14f98 | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 573 | case GPRS_LLC_U_FRMR_RESP: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 574 | ghp->cmd = GPRS_LLC_FRMR; |
| 575 | break; |
Harald Welte | 9f14f98 | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 576 | case GPRS_LLC_U_XID: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 577 | ghp->cmd = GPRS_LLC_XID; |
Harald Welte | 9f14f98 | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 578 | ghp->data = ctrl + 1; |
| 579 | ghp->data_len = (llc_hdr + len - 3) - ghp->data; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 580 | break; |
| 581 | default: |
| 582 | return -EIO; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | /* calculate what FCS we expect */ |
| 587 | ghp->fcs_calc = gprs_llc_fcs(llc_hdr, crc_length); |
| 588 | |
| 589 | /* FIXME: parse sack frame */ |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 590 | if (ghp->cmd == GPRS_LLC_SACK) { |
Harald Welte | e349736 | 2010-05-13 19:49:06 +0200 | [diff] [blame] | 591 | LOGP(DLLC, LOGL_NOTICE, "Unsupported SACK frame\n"); |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 592 | return -EIO; |
| 593 | } |
| 594 | |
| 595 | return 0; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 596 | } |
| 597 | |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 598 | /* 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] | 599 | int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv) |
| 600 | { |
Harald Welte | 7316440 | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 601 | struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 602 | struct gprs_llc_hdr *lh = msgb_llch(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 603 | struct gprs_llc_hdr_parsed llhp; |
Harald Welte | 439fa72 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 604 | struct gprs_llc_lle *lle; |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 605 | int rc = 0; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 606 | |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 607 | /* Identifiers from DOWN: NSEI, BVCI, TLLI */ |
| 608 | |
Holger Hans Peter Freyther | 510ae72 | 2010-05-23 21:33:57 +0800 | [diff] [blame] | 609 | memset(&llhp, 0, sizeof(llhp)); |
Holger Hans Peter Freyther | 6f26151 | 2010-05-23 21:43:57 +0800 | [diff] [blame] | 610 | 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] | 611 | gprs_llc_hdr_dump(&llhp); |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 612 | if (rc < 0) { |
Harald Welte | e349736 | 2010-05-13 19:49:06 +0200 | [diff] [blame] | 613 | LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n"); |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 614 | return rc; |
| 615 | } |
| 616 | |
| 617 | if (llhp.fcs != llhp.fcs_calc) { |
Harald Welte | e349736 | 2010-05-13 19:49:06 +0200 | [diff] [blame] | 618 | LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n"); |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 619 | return -EIO; |
| 620 | } |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 621 | |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 622 | switch (gprs_tlli_type(msgb_tlli(msg))) { |
| 623 | case TLLI_LOCAL: |
| 624 | case TLLI_FOREIGN: |
| 625 | case TLLI_RANDOM: |
| 626 | case TLLI_AUXILIARY: |
| 627 | break; |
| 628 | default: |
| 629 | LOGP(DLLC, LOGL_ERROR, |
| 630 | "Discarding frame with strange TLLI type\n"); |
| 631 | break; |
| 632 | } |
| 633 | |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 634 | /* find the LLC Entity for this TLLI+SAPI tuple */ |
| 635 | lle = lle_by_tlli_sapi(msgb_tlli(msg), llhp.sapi); |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 636 | |
| 637 | /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded, |
| 638 | * except UID and XID frames with SAPI=1 */ |
Harald Welte | 4b7d1de | 2010-05-18 12:45:08 +0200 | [diff] [blame] | 639 | if (!lle) { |
| 640 | if (llhp.sapi == GPRS_SAPI_GMM && |
| 641 | (llhp.cmd == GPRS_LLC_XID || llhp.cmd == GPRS_LLC_UI)) { |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 642 | struct gprs_llc_llme *llme; |
Harald Welte | 4b7d1de | 2010-05-18 12:45:08 +0200 | [diff] [blame] | 643 | /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */ |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 644 | llme = llme_alloc(msgb_tlli(msg)); |
| 645 | lle = &llme->lle[llhp.sapi]; |
Harald Welte | 4b7d1de | 2010-05-18 12:45:08 +0200 | [diff] [blame] | 646 | } else { |
| 647 | LOGP(DLLC, LOGL_NOTICE, |
| 648 | "unknown TLLI/SAPI: Silently dropping\n"); |
| 649 | return 0; |
| 650 | } |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 651 | } |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 652 | |
Harald Welte | 439fa72 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 653 | /* Update LLE's (BVCI, NSEI) tuple */ |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 654 | lle->llme->bvci = msgb_bvci(msg); |
| 655 | lle->llme->nsei = msgb_nsei(msg); |
Harald Welte | 439fa72 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 656 | |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 657 | /* Receive and Process the actual LLC frame */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 658 | rc = gprs_llc_hdr_rx(&llhp, lle); |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 659 | if (rc < 0) |
| 660 | return rc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 661 | |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 662 | /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 663 | if (llhp.data) { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 664 | msgb_gmmh(msg) = llhp.data; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 665 | switch (llhp.sapi) { |
| 666 | case GPRS_SAPI_GMM: |
Harald Welte | febc426 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 667 | /* send LL_UNITDATA_IND to GMM */ |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 668 | rc = gsm0408_gprs_rcvmsg(msg, lle->llme); |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 669 | break; |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 670 | case GPRS_SAPI_SNDCP3: |
| 671 | case GPRS_SAPI_SNDCP5: |
| 672 | case GPRS_SAPI_SNDCP9: |
| 673 | case GPRS_SAPI_SNDCP11: |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 674 | /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */ |
| 675 | rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len); |
| 676 | break; |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 677 | case GPRS_SAPI_SMS: |
| 678 | /* FIXME */ |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 679 | case GPRS_SAPI_TOM2: |
| 680 | case GPRS_SAPI_TOM8: |
| 681 | /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */ |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 682 | default: |
Harald Welte | 24b7887 | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 683 | LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi); |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 684 | rc = -EINVAL; |
| 685 | break; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Harald Welte | c14e582 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 689 | return rc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 690 | } |
Harald Welte | 75871c7 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 691 | |
| 692 | /* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */ |
| 693 | int gprs_llgmm_assign(struct gprs_llc_llme *llme, |
| 694 | uint32_t old_tlli, uint32_t new_tlli, |
| 695 | enum gprs_ciph_algo alg, const uint8_t *kc) |
| 696 | { |
| 697 | unsigned int i; |
| 698 | |
| 699 | if (old_tlli == 0xffffffff && new_tlli != 0xffffffff) { |
| 700 | /* TLLI Assignment 8.3.1 */ |
| 701 | /* New TLLI shall be assigned and used when (re)transmitting LLC frames */ |
| 702 | /* If old TLLI != 0xffffffff was assigned to LLME, then TLLI |
| 703 | * old is unassigned. Only TLLI new shall be accepted when |
| 704 | * received from peer. */ |
| 705 | |
| 706 | /* If TLLI old == 0xffffffff was assigned to LLME, then this is |
| 707 | * TLLI assignmemt according to 8.3.1 */ |
| 708 | llme->old_tlli = 0; |
| 709 | llme->tlli = new_tlli; |
| 710 | llme->state = GPRS_LLMS_ASSIGNED; |
| 711 | /* 8.5.3.1 For all LLE's */ |
| 712 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) { |
| 713 | struct gprs_llc_lle *l = &llme->lle[i]; |
| 714 | l->vu_send = l->vu_recv = 0; |
| 715 | l->retrans_ctr = 0; |
| 716 | l->state = GPRS_LLES_ASSIGNED_ADM; |
| 717 | /* FIXME Set parameters according to table 9 */ |
| 718 | } |
| 719 | } else if (old_tlli != 0xffffffff && new_tlli != 0xffffffff) { |
| 720 | /* TLLI Change 8.3.2 */ |
| 721 | /* Both TLLI Old and TLLI New are assigned; use New when |
| 722 | * (re)transmitting. Accept toth Old and New on Rx */ |
| 723 | llme->old_tlli = llme->tlli; |
| 724 | llme->tlli = new_tlli; |
| 725 | llme->state = GPRS_LLMS_ASSIGNED; |
| 726 | } else if (old_tlli != 0xffffffff && new_tlli == 0xffffffff) { |
| 727 | /* TLLI Unassignment 8.3.3) */ |
| 728 | llme->tlli = llme->old_tlli = 0; |
| 729 | llme->state = GPRS_LLMS_UNASSIGNED; |
| 730 | for (i = 0; i < ARRAY_SIZE(llme->lle); i++) { |
| 731 | struct gprs_llc_lle *l = &llme->lle[i]; |
| 732 | l->state = GPRS_LLES_UNASSIGNED; |
| 733 | } |
| 734 | } else |
| 735 | return -EINVAL; |
| 736 | |
| 737 | return 0; |
| 738 | } |