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