Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1 | /* GPRS LLC protocol implementation as per 3GPP TS 04.64 */ |
| 2 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 3 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 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 | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 24 | #include <stdint.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 25 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 26 | #include <osmocore/msgb.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 27 | #include <osmocore/linuxlist.h> |
| 28 | #include <osmocore/timer.h> |
Harald Welte | a266554 | 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 | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 33 | #include <openbsc/gprs_bssgp.h> |
| 34 | #include <openbsc/gprs_llc.h> |
| 35 | #include <openbsc/crc24.h> |
| 36 | |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 37 | LLIST_HEAD(gprs_llc_lles); |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 38 | void *llc_tall_ctx; |
| 39 | |
| 40 | /* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */ |
| 41 | static struct gprs_llc_lle *lle_by_tlli_sapi(uint32_t tlli, uint32_t sapi) |
| 42 | { |
| 43 | struct gprs_llc_lle *lle; |
| 44 | |
| 45 | llist_for_each_entry(lle, &gprs_llc_lles, list) { |
| 46 | if (lle->tlli == tlli && lle->sapi == sapi) |
| 47 | return lle; |
| 48 | } |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | static struct gprs_llc_lle *lle_alloc(uint32_t tlli, uint32_t sapi) |
| 53 | { |
| 54 | struct gprs_llc_lle *lle; |
| 55 | |
| 56 | lle = talloc_zero(llc_tall_ctx, struct gprs_llc_lle); |
| 57 | if (!lle) |
| 58 | return NULL; |
| 59 | |
| 60 | lle->tlli = tlli; |
| 61 | lle->sapi = sapi; |
| 62 | lle->state = GPRS_LLS_UNASSIGNED; |
| 63 | llist_add(&lle->list, &gprs_llc_lles); |
| 64 | |
| 65 | return lle; |
| 66 | } |
| 67 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 68 | enum gprs_llc_cmd { |
| 69 | GPRS_LLC_NULL, |
| 70 | GPRS_LLC_RR, |
| 71 | GPRS_LLC_ACK, |
| 72 | GPRS_LLC_RNR, |
| 73 | GPRS_LLC_SACK, |
| 74 | GPRS_LLC_DM, |
| 75 | GPRS_LLC_DISC, |
| 76 | GPRS_LLC_UA, |
| 77 | GPRS_LLC_SABM, |
| 78 | GPRS_LLC_FRMR, |
| 79 | GPRS_LLC_XID, |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 80 | GPRS_LLC_UI, |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 81 | }; |
| 82 | |
Harald Welte | b61f403 | 2010-05-18 12:31:50 +0200 | [diff] [blame] | 83 | static const struct value_string llc_cmd_strs[] = { |
| 84 | { GPRS_LLC_NULL, "NULL" }, |
| 85 | { GPRS_LLC_RR, "RR" }, |
| 86 | { GPRS_LLC_ACK, "ACK" }, |
| 87 | { GPRS_LLC_RNR, "RNR" }, |
| 88 | { GPRS_LLC_SACK, "SACK" }, |
| 89 | { GPRS_LLC_DM, "DM" }, |
| 90 | { GPRS_LLC_DISC, "DISC" }, |
| 91 | { GPRS_LLC_UA, "UA" }, |
| 92 | { GPRS_LLC_SABM, "SABM" }, |
| 93 | { GPRS_LLC_FRMR, "FRMR" }, |
| 94 | { GPRS_LLC_XID, "XID" }, |
| 95 | { GPRS_LLC_UI, "UI" }, |
| 96 | { 0, NULL } |
| 97 | }; |
| 98 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 99 | struct gprs_llc_hdr_parsed { |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 100 | uint8_t sapi; |
| 101 | uint8_t is_cmd:1, |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 102 | ack_req:1, |
| 103 | is_encrypted:1; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 104 | uint32_t seq_rx; |
| 105 | uint32_t seq_tx; |
| 106 | uint32_t fcs; |
| 107 | uint32_t fcs_calc; |
| 108 | uint8_t *data; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 109 | uint16_t data_len; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 110 | enum gprs_llc_cmd cmd; |
| 111 | }; |
| 112 | |
| 113 | #define LLC_ALLOC_SIZE 16384 |
| 114 | #define UI_HDR_LEN 3 |
| 115 | #define N202 4 |
| 116 | #define CRC24_LENGTH 3 |
| 117 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 118 | static int gprs_llc_fcs(uint8_t *data, unsigned int len) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 119 | { |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 120 | uint32_t fcs_calc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 121 | |
| 122 | fcs_calc = crc24_calc(INIT_CRC24, data, len); |
| 123 | fcs_calc = ~fcs_calc; |
| 124 | fcs_calc &= 0xffffff; |
| 125 | |
| 126 | return fcs_calc; |
| 127 | } |
| 128 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 129 | static void t200_expired(void *data) |
| 130 | { |
| 131 | struct gprs_llc_lle *lle = data; |
| 132 | |
| 133 | /* 8.5.1.3: Expiry of T200 */ |
| 134 | |
| 135 | if (lle->retrans_ctr >= lle->n200) { |
| 136 | /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */ |
| 137 | lle->state = GPRS_LLS_ASSIGNED_ADM; |
| 138 | } |
| 139 | |
| 140 | switch (lle->state) { |
| 141 | case GPRS_LLS_LOCAL_EST: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 142 | /* FIXME: retransmit SABM */ |
| 143 | /* FIXME: re-start T200 */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 144 | lle->retrans_ctr++; |
| 145 | break; |
| 146 | case GPRS_LLS_LOCAL_REL: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 147 | /* FIXME: retransmit DISC */ |
| 148 | /* FIXME: re-start T200 */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 149 | lle->retrans_ctr++; |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | } |
| 154 | |
| 155 | static void t201_expired(void *data) |
| 156 | { |
| 157 | struct gprs_llc_lle *lle = data; |
| 158 | |
| 159 | if (lle->retrans_ctr < lle->n200) { |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 160 | /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */ |
| 161 | /* FIXME: set timer T201 */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 162 | lle->retrans_ctr++; |
| 163 | } |
| 164 | } |
| 165 | |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 166 | int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command, |
| 167 | enum gprs_llc_u_cmd u_cmd, int pf_bit) |
| 168 | { |
| 169 | uint8_t *fcs, *llch; |
| 170 | uint8_t addr, ctrl; |
| 171 | uint32_t fcs_calc; |
| 172 | |
| 173 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 174 | |
| 175 | /* Address Field */ |
| 176 | addr = sapi & 0xf; |
| 177 | if (command) |
| 178 | addr |= 0x40; |
| 179 | |
| 180 | /* 6.3 Figure 8 */ |
| 181 | ctrl = 0xe0 | u_cmd; |
| 182 | if (pf_bit) |
| 183 | ctrl |= 0x10; |
| 184 | |
| 185 | /* prepend LLC UI header */ |
| 186 | llch = msgb_push(msg, 2); |
| 187 | llch[0] = addr; |
| 188 | llch[1] = ctrl; |
| 189 | |
| 190 | /* append FCS to end of frame */ |
| 191 | fcs = msgb_put(msg, 3); |
| 192 | fcs_calc = gprs_llc_fcs(llch, fcs - llch); |
| 193 | fcs[0] = fcs_calc & 0xff; |
| 194 | fcs[1] = (fcs_calc >> 8) & 0xff; |
| 195 | fcs[2] = (fcs_calc >> 16) & 0xff; |
| 196 | |
| 197 | /* Identifiers passed down: (BVCI, NSEI) */ |
| 198 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 199 | /* Send BSSGP-DL-UNITDATA.req */ |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 200 | return gprs_bssgp_tx_dl_ud(msg); |
| 201 | } |
| 202 | |
| 203 | /* Send XID response to LLE */ |
| 204 | static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg) |
| 205 | { |
| 206 | /* copy identifiers from LLE to ensure lower layers can route */ |
| 207 | msgb_tlli(msg) = lle->tlli; |
| 208 | msgb_bvci(msg) = lle->bvci; |
| 209 | msgb_nsei(msg) = lle->nsei; |
| 210 | |
| 211 | return gprs_llc_tx_u(msg, lle->sapi, 0, GPRS_LLC_U_XID, 1); |
| 212 | } |
| 213 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 214 | /* Transmit a UI frame over the given SAPI */ |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 215 | int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 216 | { |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 217 | struct gprs_llc_lle *lle; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 218 | uint8_t *fcs, *llch; |
| 219 | uint8_t addr, ctrl[2]; |
| 220 | uint32_t fcs_calc; |
| 221 | uint16_t nu = 0; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 222 | |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 223 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 224 | |
| 225 | /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */ |
| 226 | lle = lle_by_tlli_sapi(msgb_tlli(msg), sapi); |
| 227 | if (!lle) |
| 228 | lle = lle_alloc(msgb_tlli(msg), sapi); |
| 229 | /* Update LLE's (BVCI, NSEI) tuple */ |
| 230 | lle->bvci = msgb_bvci(msg); |
| 231 | lle->nsei = msgb_nsei(msg); |
| 232 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 233 | /* Address Field */ |
| 234 | addr = sapi & 0xf; |
| 235 | if (command) |
| 236 | addr |= 0x40; |
| 237 | |
| 238 | /* Control Field */ |
| 239 | ctrl[0] = 0xc0; |
| 240 | ctrl[0] |= nu >> 6; |
| 241 | ctrl[1] = (nu << 2) & 0xfc; |
| 242 | ctrl[1] |= 0x01; /* Protected Mode */ |
| 243 | |
| 244 | /* prepend LLC UI header */ |
| 245 | llch = msgb_push(msg, 3); |
| 246 | llch[0] = addr; |
| 247 | llch[1] = ctrl[0]; |
| 248 | llch[2] = ctrl[1]; |
| 249 | |
| 250 | /* append FCS to end of frame */ |
| 251 | fcs = msgb_put(msg, 3); |
| 252 | fcs_calc = gprs_llc_fcs(llch, fcs - llch); |
| 253 | fcs[0] = fcs_calc & 0xff; |
| 254 | fcs[1] = (fcs_calc >> 8) & 0xff; |
| 255 | fcs[2] = (fcs_calc >> 16) & 0xff; |
| 256 | |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 257 | /* Identifiers passed down: (BVCI, NSEI) */ |
| 258 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 259 | /* Send BSSGP-DL-UNITDATA.req */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 260 | return gprs_bssgp_tx_dl_ud(msg); |
| 261 | } |
| 262 | |
| 263 | static int gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph) |
| 264 | { |
Harald Welte | c6ecafe | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 265 | DEBUGP(DLLC, "LLC SAPI=%u %c %c FCS=0x%06x(%s) ", |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 266 | gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ', |
| 267 | gph->fcs, gph->fcs_calc == gph->fcs ? "correct" : "WRONG"); |
| 268 | |
| 269 | if (gph->cmd) |
Harald Welte | b61f403 | 2010-05-18 12:31:50 +0200 | [diff] [blame] | 270 | DEBUGPC(DLLC, "CMD=%s ", get_value_string(llc_cmd_strs, gph->cmd)); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 271 | |
| 272 | if (gph->data) |
Harald Welte | c6ecafe | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 273 | DEBUGPC(DLLC, "DATA "); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 274 | |
Harald Welte | c6ecafe | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 275 | DEBUGPC(DLLC, "\n"); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 276 | } |
| 277 | static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph, |
| 278 | struct gprs_llc_lle *lle) |
| 279 | { |
| 280 | switch (gph->cmd) { |
| 281 | case GPRS_LLC_SABM: /* Section 6.4.1.1 */ |
| 282 | lle->v_sent = lle->v_ack = lle->v_recv = 0; |
| 283 | if (lle->state == GPRS_LLS_ASSIGNED_ADM) { |
| 284 | /* start re-establishment (8.7.1) */ |
| 285 | } |
| 286 | lle->state = GPRS_LLS_REMOTE_EST; |
| 287 | /* FIXME: Send UA */ |
| 288 | lle->state = GPRS_LLS_ABM; |
| 289 | /* FIXME: process data */ |
| 290 | break; |
| 291 | case GPRS_LLC_DISC: /* Section 6.4.1.2 */ |
| 292 | /* FIXME: Send UA */ |
| 293 | /* terminate ABM */ |
| 294 | lle->state = GPRS_LLS_ASSIGNED_ADM; |
| 295 | break; |
| 296 | case GPRS_LLC_UA: /* Section 6.4.1.3 */ |
| 297 | if (lle->state == GPRS_LLS_LOCAL_EST) |
| 298 | lle->state = GPRS_LLS_ABM; |
| 299 | break; |
| 300 | case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */ |
| 301 | if (lle->state == GPRS_LLS_LOCAL_EST) |
| 302 | lle->state = GPRS_LLS_ASSIGNED_ADM; |
| 303 | break; |
| 304 | case GPRS_LLC_FRMR: /* Section 6.4.1.5 */ |
| 305 | break; |
| 306 | case GPRS_LLC_XID: /* Section 6.4.1.6 */ |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 307 | /* FIXME: implement XID negotiation using SNDCP */ |
| 308 | { |
| 309 | struct msgb *resp; |
| 310 | uint8_t *xid; |
| 311 | resp = msgb_alloc_headroom(4096, 1024, "LLC_XID"); |
| 312 | xid = msgb_put(resp, gph->data_len); |
| 313 | memcpy(xid, gph->data, gph->data_len); |
| 314 | gprs_llc_tx_xid(lle, resp); |
| 315 | } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 316 | break; |
| 317 | } |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | /* parse a GPRS LLC header, also check for invalid frames */ |
| 323 | static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp, |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 324 | const uint8_t *llc_hdr, int len) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 325 | { |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 326 | uint8_t *ctrl = llc_hdr+1; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 327 | int is_sack = 0; |
| 328 | unsigned int crc_length; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 329 | uint32_t fcs_calc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 330 | |
| 331 | if (len <= CRC24_LENGTH) |
| 332 | return -EIO; |
| 333 | |
| 334 | crc_length = len - CRC24_LENGTH; |
| 335 | |
| 336 | ghp->ack_req = 0; |
| 337 | |
| 338 | /* Section 5.5: FCS */ |
| 339 | ghp->fcs = *(llc_hdr + len - 3); |
| 340 | ghp->fcs |= *(llc_hdr + len - 2) << 8; |
| 341 | ghp->fcs |= *(llc_hdr + len - 1) << 16; |
| 342 | |
| 343 | /* Section 6.2.1: invalid PD field */ |
| 344 | if (llc_hdr[0] & 0x80) |
| 345 | return -EIO; |
| 346 | |
| 347 | /* This only works for the MS->SGSN direction */ |
| 348 | if (llc_hdr[0] & 0x40) |
| 349 | ghp->is_cmd = 0; |
| 350 | else |
| 351 | ghp->is_cmd = 1; |
| 352 | |
| 353 | ghp->sapi = llc_hdr[0] & 0xf; |
| 354 | |
| 355 | /* Section 6.2.3: check for reserved SAPI */ |
| 356 | switch (ghp->sapi) { |
| 357 | case 0: |
| 358 | case 4: |
| 359 | case 6: |
| 360 | case 0xa: |
| 361 | case 0xc: |
| 362 | case 0xd: |
| 363 | case 0xf: |
| 364 | return -EINVAL; |
| 365 | } |
| 366 | |
| 367 | if ((ctrl[0] & 0x80) == 0) { |
| 368 | /* I (Information transfer + Supervisory) format */ |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 369 | uint8_t k; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 370 | |
| 371 | ghp->data = ctrl + 3; |
| 372 | |
| 373 | if (ctrl[0] & 0x40) |
| 374 | ghp->ack_req = 1; |
| 375 | |
| 376 | ghp->seq_tx = (ctrl[0] & 0x1f) << 4; |
| 377 | ghp->seq_tx |= (ctrl[1] >> 4); |
| 378 | |
| 379 | ghp->seq_rx = (ctrl[1] & 0x7) << 6; |
| 380 | ghp->seq_rx |= (ctrl[2] >> 2); |
| 381 | |
| 382 | switch (ctrl[2] & 0x03) { |
| 383 | case 0: |
| 384 | ghp->cmd = GPRS_LLC_RR; |
| 385 | break; |
| 386 | case 1: |
| 387 | ghp->cmd = GPRS_LLC_ACK; |
| 388 | break; |
| 389 | case 2: |
| 390 | ghp->cmd = GPRS_LLC_RNR; |
| 391 | break; |
| 392 | case 3: |
| 393 | ghp->cmd = GPRS_LLC_SACK; |
| 394 | k = ctrl[3] & 0x1f; |
| 395 | ghp->data += 1 + k; |
| 396 | break; |
| 397 | } |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 398 | ghp->data_len = (llc_hdr + len - 3) - ghp->data; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 399 | } else if ((ctrl[0] & 0xc0) == 0x80) { |
| 400 | /* S (Supervisory) format */ |
| 401 | ghp->data = NULL; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 402 | ghp->data_len = 0; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 403 | |
| 404 | if (ctrl[0] & 0x20) |
| 405 | ghp->ack_req = 1; |
| 406 | ghp->seq_rx = (ctrl[0] & 0x7) << 6; |
| 407 | ghp->seq_rx |= (ctrl[1] >> 2); |
| 408 | |
| 409 | switch (ctrl[1] & 0x03) { |
| 410 | case 0: |
| 411 | ghp->cmd = GPRS_LLC_RR; |
| 412 | break; |
| 413 | case 1: |
| 414 | ghp->cmd = GPRS_LLC_ACK; |
| 415 | break; |
| 416 | case 2: |
| 417 | ghp->cmd = GPRS_LLC_RNR; |
| 418 | break; |
| 419 | case 3: |
| 420 | ghp->cmd = GPRS_LLC_SACK; |
| 421 | break; |
| 422 | } |
| 423 | } else if ((ctrl[0] & 0xe0) == 0xc0) { |
| 424 | /* UI (Unconfirmed Inforamtion) format */ |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 425 | ghp->cmd = GPRS_LLC_UI; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 426 | ghp->data = ctrl + 2; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 427 | ghp->data_len = (llc_hdr + len - 3) - ghp->data; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 428 | |
| 429 | ghp->seq_tx = (ctrl[0] & 0x7) << 6; |
| 430 | ghp->seq_tx |= (ctrl[1] >> 2); |
| 431 | if (ctrl[1] & 0x02) { |
| 432 | ghp->is_encrypted = 1; |
| 433 | /* FIXME: encryption */ |
| 434 | } |
| 435 | if (ctrl[1] & 0x01) { |
| 436 | /* FCS over hdr + all inf fields */ |
| 437 | } else { |
| 438 | /* FCS over hdr + N202 octets (4) */ |
| 439 | if (crc_length > UI_HDR_LEN + N202) |
| 440 | crc_length = UI_HDR_LEN + N202; |
| 441 | } |
| 442 | } else { |
| 443 | /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */ |
| 444 | ghp->data = NULL; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 445 | ghp->data_len = 0; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 446 | |
| 447 | switch (ctrl[0] & 0xf) { |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 448 | case GPRS_LLC_U_NULL_CMD: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 449 | ghp->cmd = GPRS_LLC_NULL; |
| 450 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 451 | case GPRS_LLC_U_DM_RESP: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 452 | ghp->cmd = GPRS_LLC_DM; |
| 453 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 454 | case GPRS_LLC_U_DISC_CMD: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 455 | ghp->cmd = GPRS_LLC_DISC; |
| 456 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 457 | case GPRS_LLC_U_UA_RESP: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 458 | ghp->cmd = GPRS_LLC_UA; |
| 459 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 460 | case GPRS_LLC_U_SABM_CMD: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 461 | ghp->cmd = GPRS_LLC_SABM; |
| 462 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 463 | case GPRS_LLC_U_FRMR_RESP: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 464 | ghp->cmd = GPRS_LLC_FRMR; |
| 465 | break; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 466 | case GPRS_LLC_U_XID: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 467 | ghp->cmd = GPRS_LLC_XID; |
Harald Welte | 5658a1a | 2010-05-03 13:25:07 +0200 | [diff] [blame] | 468 | ghp->data = ctrl + 1; |
| 469 | ghp->data_len = (llc_hdr + len - 3) - ghp->data; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 470 | break; |
| 471 | default: |
| 472 | return -EIO; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /* calculate what FCS we expect */ |
| 477 | ghp->fcs_calc = gprs_llc_fcs(llc_hdr, crc_length); |
| 478 | |
| 479 | /* FIXME: parse sack frame */ |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 480 | if (ghp->cmd == GPRS_LLC_SACK) { |
Harald Welte | 1b170d1 | 2010-05-13 19:49:06 +0200 | [diff] [blame] | 481 | LOGP(DLLC, LOGL_NOTICE, "Unsupported SACK frame\n"); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 482 | return -EIO; |
| 483 | } |
| 484 | |
| 485 | return 0; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 486 | } |
| 487 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 488 | /* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 489 | int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv) |
| 490 | { |
Harald Welte | fd3fa1d | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 491 | struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); |
Harald Welte | 943c5bc | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 492 | struct gprs_llc_hdr *lh = msgb_llch(msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 493 | struct gprs_llc_hdr_parsed llhp; |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 494 | struct gprs_llc_lle *lle; |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 495 | int rc = 0; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 496 | |
Harald Welte | 11d7c10 | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 497 | /* Identifiers from DOWN: NSEI, BVCI, TLLI */ |
| 498 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 499 | rc = gprs_llc_hdr_parse(&llhp, lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU)); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 500 | gprs_llc_hdr_dump(&llhp); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 501 | if (rc < 0) { |
Harald Welte | 1b170d1 | 2010-05-13 19:49:06 +0200 | [diff] [blame] | 502 | LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n"); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 503 | return rc; |
| 504 | } |
| 505 | |
| 506 | if (llhp.fcs != llhp.fcs_calc) { |
Harald Welte | 1b170d1 | 2010-05-13 19:49:06 +0200 | [diff] [blame] | 507 | LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n"); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 508 | return -EIO; |
| 509 | } |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 510 | |
| 511 | /* find the LLC Entity for this TLLI+SAPI tuple */ |
| 512 | lle = lle_by_tlli_sapi(msgb_tlli(msg), llhp.sapi); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 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 (!lle && llhp.sapi == GPRS_SAPI_GMM && |
| 517 | (llhp.cmd == GPRS_LLC_XID || llhp.cmd == GPRS_LLC_UI)) { |
| 518 | /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */ |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 519 | lle = lle_alloc(msgb_tlli(msg), llhp.sapi); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 520 | } else { |
Harald Welte | 1b170d1 | 2010-05-13 19:49:06 +0200 | [diff] [blame] | 521 | LOGP(DLLC, LOGL_NOTICE, |
| 522 | "unknown TLLI/SAPI: Silently dropping\n"); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 523 | return 0; |
| 524 | } |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 525 | |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 526 | /* Update LLE's (BVCI, NSEI) tuple */ |
| 527 | lle->bvci = msgb_bvci(msg); |
| 528 | lle->nsei = msgb_nsei(msg); |
| 529 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 530 | /* Receive and Process the actual LLC frame */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 531 | rc = gprs_llc_hdr_rx(&llhp, lle); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 532 | if (rc < 0) |
| 533 | return rc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 534 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 535 | /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 536 | if (llhp.data) { |
Harald Welte | 943c5bc | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 537 | msgb_gmmh(msg) = llhp.data; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 538 | switch (llhp.sapi) { |
| 539 | case GPRS_SAPI_GMM: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 540 | /* send LL_UNITDATA_IND to GMM */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 541 | rc = gsm0408_gprs_rcvmsg(msg); |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 542 | break; |
| 543 | case GPRS_SAPI_TOM2: |
| 544 | case GPRS_SAPI_TOM8: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 545 | /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */ |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 546 | case GPRS_SAPI_SNDCP3: |
| 547 | case GPRS_SAPI_SNDCP5: |
| 548 | case GPRS_SAPI_SNDCP9: |
| 549 | case GPRS_SAPI_SNDCP11: |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 550 | /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */ |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 551 | case GPRS_SAPI_SMS: |
| 552 | /* FIXME */ |
| 553 | default: |
Harald Welte | c6ecafe | 2010-05-13 19:47:50 +0200 | [diff] [blame] | 554 | LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi); |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 555 | rc = -EINVAL; |
| 556 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
Harald Welte | a266554 | 2010-05-02 09:28:11 +0200 | [diff] [blame] | 560 | return rc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 561 | } |