blob: 91b0239f0f0da9c94e44cdd9a704776c549ac6ec [file] [log] [blame]
Harald Welte9b455bf2010-03-14 15:45:01 +08001/* GPRS LLC protocol implementation as per 3GPP TS 04.64 */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
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>
24
25#include <openbsc/gsm_data.h>
26#include <osmocore/msgb.h>
27#include <openbsc/debug.h>
28#include <osmocore/linuxlist.h>
29#include <osmocore/timer.h>
30#include <openbsc/gprs_bssgp.h>
31#include <openbsc/gprs_llc.h>
32#include <openbsc/crc24.h>
33
34/* Section 4.5.2 Logical Link States + Annex C.2 */
35enum gprs_llc_ll_state {
36 GPRS_LLS_UNASSIGNED = 1, /* No TLLI yet */
37 GPRS_LLS_ASSIGNED_ADM = 2, /* TLLI assigned */
38 GPRS_LLS_LOCAL_EST = 3, /* Local Establishment */
39 GPRS_LLS_REMOTE_EST = 4, /* Remote Establishment */
40 GPRS_LLS_ABM = 5,
41 GPRS_LLS_LOCAL_REL = 6, /* Local Release */
42 GPRS_LLS_TIMER_REC = 7, /* Timer Recovery */
43};
44
45/* Section 4.7.1: Logical Link Entity: One per DLCI (TLLI + SAPI) */
46struct gprs_llc_lle {
47 struct llist_head list;
48 struct timer_list t200;
49 struct timer_list t201; /* wait for acknowledgement */
50
51 enum gprs_llc_ll_state state;
52
53 u_int32_t tlli;
54 u_int32_t sapi;
55
56 u_int8_t v_sent;
57 u_int8_t v_ack;
58 u_int8_t v_recv;
59
60 unsigned int n200;
61 unsigned int retrans_ctr;
62};
63
64enum gprs_llc_cmd {
65 GPRS_LLC_NULL,
66 GPRS_LLC_RR,
67 GPRS_LLC_ACK,
68 GPRS_LLC_RNR,
69 GPRS_LLC_SACK,
70 GPRS_LLC_DM,
71 GPRS_LLC_DISC,
72 GPRS_LLC_UA,
73 GPRS_LLC_SABM,
74 GPRS_LLC_FRMR,
75 GPRS_LLC_XID,
76};
77
78struct gprs_llc_hdr_parsed {
79 u_int8_t sapi;
80 u_int8_t is_cmd:1,
81 ack_req:1,
82 is_encrypted:1;
83 u_int32_t seq_rx;
84 u_int32_t seq_tx;
85 u_int32_t fcs;
86 u_int32_t fcs_calc;
87 u_int8_t *data;
88 enum gprs_llc_cmd cmd;
89};
90
91#define LLC_ALLOC_SIZE 16384
92#define UI_HDR_LEN 3
93#define N202 4
94#define CRC24_LENGTH 3
95
96static int gprs_llc_fcs(u_int8_t *data, unsigned int len)
97{
98 u_int32_t fcs_calc;
99
100 fcs_calc = crc24_calc(INIT_CRC24, data, len);
101 fcs_calc = ~fcs_calc;
102 fcs_calc &= 0xffffff;
103
104 return fcs_calc;
105}
106
107/* transmit a simple U frame */
108static int gprs_llc_tx_u()
109{
110 struct msgb *msg = msgb_alloc(LLC_ALLOC_SIZE, "GPRS/LLC");
111
112 if (!msg)
113 return -ENOMEM;
114
115
116
117 /* transmit the frame via BSSGP->NS->... */
118}
119
120static void t200_expired(void *data)
121{
122 struct gprs_llc_lle *lle = data;
123
124 /* 8.5.1.3: Expiry of T200 */
125
126 if (lle->retrans_ctr >= lle->n200) {
127 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
128 lle->state = GPRS_LLS_ASSIGNED_ADM;
129 }
130
131 switch (lle->state) {
132 case GPRS_LLS_LOCAL_EST:
133 /* retransmit SABM */
134 /* re-start T200 */
135 lle->retrans_ctr++;
136 break;
137 case GPRS_LLS_LOCAL_REL:
138 /* retransmit DISC */
139 /* re-start T200 */
140 lle->retrans_ctr++;
141 break;
142 }
143
144}
145
146static void t201_expired(void *data)
147{
148 struct gprs_llc_lle *lle = data;
149
150 if (lle->retrans_ctr < lle->n200) {
151 /* transmit apropriate supervisory frame (8.6.4.1) */
152 /* set timer T201 */
153 lle->retrans_ctr++;
154 }
155}
156
157/* Transmit a UI frame over the given SAPI */
158int gprs_llc_tx_ui(struct msgb *msg, u_int8_t sapi, int command)
159{
160 u_int8_t *fcs, *llch;
161 u_int8_t addr, ctrl[2];
162 u_int32_t fcs_calc;
163 u_int16_t nu = 0;
164
165 /* Address Field */
166 addr = sapi & 0xf;
167 if (command)
168 addr |= 0x40;
169
170 /* Control Field */
171 ctrl[0] = 0xc0;
172 ctrl[0] |= nu >> 6;
173 ctrl[1] = (nu << 2) & 0xfc;
174 ctrl[1] |= 0x01; /* Protected Mode */
175
176 /* prepend LLC UI header */
177 llch = msgb_push(msg, 3);
178 llch[0] = addr;
179 llch[1] = ctrl[0];
180 llch[2] = ctrl[1];
181
182 /* append FCS to end of frame */
183 fcs = msgb_put(msg, 3);
184 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
185 fcs[0] = fcs_calc & 0xff;
186 fcs[1] = (fcs_calc >> 8) & 0xff;
187 fcs[2] = (fcs_calc >> 16) & 0xff;
188
189 return gprs_bssgp_tx_dl_ud(msg);
190}
191
192static int gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph)
193{
194 DEBUGP(DGPRS, "LLC SAPI=%u %c %c FCS=0x%06x(%s) ",
195 gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ',
196 gph->fcs, gph->fcs_calc == gph->fcs ? "correct" : "WRONG");
197
198 if (gph->cmd)
199 DEBUGPC(DGPRS, "CMD=%u ", gph->cmd);
200
201 if (gph->data)
202 DEBUGPC(DGPRS, "DATA ");
203
204 DEBUGPC(DGPRS, "\n");
205}
206static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
207 struct gprs_llc_lle *lle)
208{
209 switch (gph->cmd) {
210 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
211 lle->v_sent = lle->v_ack = lle->v_recv = 0;
212 if (lle->state == GPRS_LLS_ASSIGNED_ADM) {
213 /* start re-establishment (8.7.1) */
214 }
215 lle->state = GPRS_LLS_REMOTE_EST;
216 /* FIXME: Send UA */
217 lle->state = GPRS_LLS_ABM;
218 /* FIXME: process data */
219 break;
220 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
221 /* FIXME: Send UA */
222 /* terminate ABM */
223 lle->state = GPRS_LLS_ASSIGNED_ADM;
224 break;
225 case GPRS_LLC_UA: /* Section 6.4.1.3 */
226 if (lle->state == GPRS_LLS_LOCAL_EST)
227 lle->state = GPRS_LLS_ABM;
228 break;
229 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
230 if (lle->state == GPRS_LLS_LOCAL_EST)
231 lle->state = GPRS_LLS_ASSIGNED_ADM;
232 break;
233 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
234 break;
235 case GPRS_LLC_XID: /* Section 6.4.1.6 */
236 break;
237 }
238
239 return 0;
240}
241
242/* parse a GPRS LLC header, also check for invalid frames */
243static int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
244 const u_int8_t *llc_hdr, int len)
245{
246 u_int8_t *ctrl = llc_hdr+1;
247 int is_sack = 0;
248 unsigned int crc_length;
249 u_int32_t fcs_calc;
250
251 if (len <= CRC24_LENGTH)
252 return -EIO;
253
254 crc_length = len - CRC24_LENGTH;
255
256 ghp->ack_req = 0;
257
258 /* Section 5.5: FCS */
259 ghp->fcs = *(llc_hdr + len - 3);
260 ghp->fcs |= *(llc_hdr + len - 2) << 8;
261 ghp->fcs |= *(llc_hdr + len - 1) << 16;
262
263 /* Section 6.2.1: invalid PD field */
264 if (llc_hdr[0] & 0x80)
265 return -EIO;
266
267 /* This only works for the MS->SGSN direction */
268 if (llc_hdr[0] & 0x40)
269 ghp->is_cmd = 0;
270 else
271 ghp->is_cmd = 1;
272
273 ghp->sapi = llc_hdr[0] & 0xf;
274
275 /* Section 6.2.3: check for reserved SAPI */
276 switch (ghp->sapi) {
277 case 0:
278 case 4:
279 case 6:
280 case 0xa:
281 case 0xc:
282 case 0xd:
283 case 0xf:
284 return -EINVAL;
285 }
286
287 if ((ctrl[0] & 0x80) == 0) {
288 /* I (Information transfer + Supervisory) format */
289 u_int8_t k;
290
291 ghp->data = ctrl + 3;
292
293 if (ctrl[0] & 0x40)
294 ghp->ack_req = 1;
295
296 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
297 ghp->seq_tx |= (ctrl[1] >> 4);
298
299 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
300 ghp->seq_rx |= (ctrl[2] >> 2);
301
302 switch (ctrl[2] & 0x03) {
303 case 0:
304 ghp->cmd = GPRS_LLC_RR;
305 break;
306 case 1:
307 ghp->cmd = GPRS_LLC_ACK;
308 break;
309 case 2:
310 ghp->cmd = GPRS_LLC_RNR;
311 break;
312 case 3:
313 ghp->cmd = GPRS_LLC_SACK;
314 k = ctrl[3] & 0x1f;
315 ghp->data += 1 + k;
316 break;
317 }
318 } else if ((ctrl[0] & 0xc0) == 0x80) {
319 /* S (Supervisory) format */
320 ghp->data = NULL;
321
322 if (ctrl[0] & 0x20)
323 ghp->ack_req = 1;
324 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
325 ghp->seq_rx |= (ctrl[1] >> 2);
326
327 switch (ctrl[1] & 0x03) {
328 case 0:
329 ghp->cmd = GPRS_LLC_RR;
330 break;
331 case 1:
332 ghp->cmd = GPRS_LLC_ACK;
333 break;
334 case 2:
335 ghp->cmd = GPRS_LLC_RNR;
336 break;
337 case 3:
338 ghp->cmd = GPRS_LLC_SACK;
339 break;
340 }
341 } else if ((ctrl[0] & 0xe0) == 0xc0) {
342 /* UI (Unconfirmed Inforamtion) format */
343 ghp->data = ctrl + 2;
344
345 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
346 ghp->seq_tx |= (ctrl[1] >> 2);
347 if (ctrl[1] & 0x02) {
348 ghp->is_encrypted = 1;
349 /* FIXME: encryption */
350 }
351 if (ctrl[1] & 0x01) {
352 /* FCS over hdr + all inf fields */
353 } else {
354 /* FCS over hdr + N202 octets (4) */
355 if (crc_length > UI_HDR_LEN + N202)
356 crc_length = UI_HDR_LEN + N202;
357 }
358 } else {
359 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
360 ghp->data = NULL;
361
362 switch (ctrl[0] & 0xf) {
363 case 0:
364 ghp->cmd = GPRS_LLC_NULL;
365 break;
366 case 0x1:
367 ghp->cmd = GPRS_LLC_DM;
368 break;
369 case 0x4:
370 ghp->cmd = GPRS_LLC_DISC;
371 break;
372 case 0x6:
373 ghp->cmd = GPRS_LLC_UA;
374 break;
375 case 0x7:
376 ghp->cmd = GPRS_LLC_SABM;
377 break;
378 case 0x8:
379 ghp->cmd = GPRS_LLC_FRMR;
380 break;
381 case 0xb:
382 ghp->cmd = GPRS_LLC_XID;
383 break;
384 default:
385 return -EIO;
386 }
387 }
388
389 /* calculate what FCS we expect */
390 ghp->fcs_calc = gprs_llc_fcs(llc_hdr, crc_length);
391
392 /* FIXME: parse sack frame */
393}
394
395/* receive an incoming LLC PDU */
396int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
397{
398 struct bssgp_ud_hdr *udh = (struct bssgp_ud_hdr *) msg->l3h;
Harald Welte943c5bc2010-04-30 16:33:12 +0200399 struct gprs_llc_hdr *lh = msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800400 struct gprs_llc_hdr_parsed llhp;
401 struct gprs_llc_entity *lle;
402 int rc;
403
404 rc = gprs_llc_hdr_parse(&llhp, lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
405
406 /* FIXME: find LLC Entity */
407
408 gprs_llc_hdr_dump(&llhp);
409 rc = gprs_llc_hdr_rx(&llhp, lle);
410
411 if (llhp.data) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200412 msgb_gmmh(msg) = llhp.data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800413 switch (llhp.sapi) {
414 case GPRS_SAPI_GMM:
415 rc = gsm0408_gprs_rcvmsg(msg);
416 }
417 }
418
419 return 0;
420}