blob: a5a7a7122a6b5872ad5d386a291267acf3120d7d [file] [log] [blame]
Jacob Erlbeckb492d392014-06-02 10:49:01 +02001/* GPRS LLC protocol implementation as per 3GPP TS 04.64 */
2
3/* (C) 2009-2010 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 Affero General Public License as published by
9 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <errno.h>
23#include <stdint.h>
24
25#include <osmocom/core/msgb.h>
26#include <osmocom/core/linuxlist.h>
27#include <osmocom/core/timer.h>
28#include <osmocom/core/talloc.h>
29#include <osmocom/gprs/gprs_bssgp.h>
30
31#include <openbsc/gsm_data.h>
32#include <openbsc/debug.h>
33#include <openbsc/gprs_sgsn.h>
34#include <openbsc/gprs_gmm.h>
35#include <openbsc/gprs_llc.h>
36#include <openbsc/crc24.h>
37
38static const struct value_string llc_cmd_strs[] = {
39 { GPRS_LLC_NULL, "NULL" },
40 { GPRS_LLC_RR, "RR" },
41 { GPRS_LLC_ACK, "ACK" },
42 { GPRS_LLC_RNR, "RNR" },
43 { GPRS_LLC_SACK, "SACK" },
44 { GPRS_LLC_DM, "DM" },
45 { GPRS_LLC_DISC, "DISC" },
46 { GPRS_LLC_UA, "UA" },
47 { GPRS_LLC_SABM, "SABM" },
48 { GPRS_LLC_FRMR, "FRMR" },
49 { GPRS_LLC_XID, "XID" },
50 { GPRS_LLC_UI, "UI" },
51 { 0, NULL }
52};
53
54#define LLC_ALLOC_SIZE 16384
55#define UI_HDR_LEN 3
56#define N202 4
57#define CRC24_LENGTH 3
58
59int gprs_llc_fcs(uint8_t *data, unsigned int len)
60{
61 uint32_t fcs_calc;
62
63 fcs_calc = crc24_calc(INIT_CRC24, data, len);
64 fcs_calc = ~fcs_calc;
65 fcs_calc &= 0xffffff;
66
67 return fcs_calc;
68}
69
Max549ebc72016-11-18 14:07:04 +010070void gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph, struct gprs_llc_lle *lle)
Jacob Erlbeckb492d392014-06-02 10:49:01 +020071{
Max549ebc72016-11-18 14:07:04 +010072 const char *gea;
73 uint32_t iov_ui = 0;
74 if (lle) {
75 gea = get_value_string(gprs_cipher_names, lle->llme->algo);
76 iov_ui = lle->llme->iov_ui;
77 } else
78 gea = "GEA?";
79 DEBUGP(DLLC, "LLC SAPI=%u %c %c %c %s IOV-UI=0x%06x FCS=0x%06x ",
80 gph->sapi, gph->is_cmd ? 'C' : 'R', gph->ack_req ? 'A' : ' ',
81 gph->is_encrypted ? 'E' : 'U',
82 gea, iov_ui, gph->fcs);
Jacob Erlbeckb492d392014-06-02 10:49:01 +020083
84 if (gph->cmd)
85 DEBUGPC(DLLC, "CMD=%s ", get_value_string(llc_cmd_strs, gph->cmd));
86
87 if (gph->data)
88 DEBUGPC(DLLC, "DATA ");
89
90 DEBUGPC(DLLC, "\n");
91}
92
93/* parse a GPRS LLC header, also check for invalid frames */
94int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
95 uint8_t *llc_hdr, int len)
96{
97 uint8_t *ctrl = llc_hdr+1;
98
99 if (len <= CRC24_LENGTH)
100 return -EIO;
101
102 ghp->crc_length = len - CRC24_LENGTH;
103
104 ghp->ack_req = 0;
105
106 /* Section 5.5: FCS */
107 ghp->fcs = *(llc_hdr + len - 3);
108 ghp->fcs |= *(llc_hdr + len - 2) << 8;
109 ghp->fcs |= *(llc_hdr + len - 1) << 16;
110
111 /* Section 6.2.1: invalid PD field */
112 if (llc_hdr[0] & 0x80)
113 return -EIO;
114
115 /* This only works for the MS->SGSN direction */
116 if (llc_hdr[0] & 0x40)
117 ghp->is_cmd = 0;
118 else
119 ghp->is_cmd = 1;
120
121 ghp->sapi = llc_hdr[0] & 0xf;
122
123 /* Section 6.2.3: check for reserved SAPI */
124 switch (ghp->sapi) {
125 case 0:
126 case 4:
127 case 6:
128 case 0xa:
129 case 0xc:
130 case 0xd:
131 case 0xf:
132 return -EINVAL;
133 }
134
135 if ((ctrl[0] & 0x80) == 0) {
136 /* I (Information transfer + Supervisory) format */
137 uint8_t k;
138
139 ghp->data = ctrl + 3;
140
141 if (ctrl[0] & 0x40)
142 ghp->ack_req = 1;
143
144 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
145 ghp->seq_tx |= (ctrl[1] >> 4);
146
147 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
148 ghp->seq_rx |= (ctrl[2] >> 2);
149
150 switch (ctrl[2] & 0x03) {
151 case 0:
152 ghp->cmd = GPRS_LLC_RR;
153 break;
154 case 1:
155 ghp->cmd = GPRS_LLC_ACK;
156 break;
157 case 2:
158 ghp->cmd = GPRS_LLC_RNR;
159 break;
160 case 3:
161 ghp->cmd = GPRS_LLC_SACK;
162 k = ctrl[3] & 0x1f;
163 ghp->data += 1 + k;
164 break;
165 }
166 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
167 } else if ((ctrl[0] & 0xc0) == 0x80) {
168 /* S (Supervisory) format */
169 ghp->data = NULL;
170 ghp->data_len = 0;
171
172 if (ctrl[0] & 0x20)
173 ghp->ack_req = 1;
174 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
175 ghp->seq_rx |= (ctrl[1] >> 2);
176
177 switch (ctrl[1] & 0x03) {
178 case 0:
179 ghp->cmd = GPRS_LLC_RR;
180 break;
181 case 1:
182 ghp->cmd = GPRS_LLC_ACK;
183 break;
184 case 2:
185 ghp->cmd = GPRS_LLC_RNR;
186 break;
187 case 3:
188 ghp->cmd = GPRS_LLC_SACK;
189 break;
190 }
191 } else if ((ctrl[0] & 0xe0) == 0xc0) {
192 /* UI (Unconfirmed Inforamtion) format */
193 ghp->cmd = GPRS_LLC_UI;
194 ghp->data = ctrl + 2;
195 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
196
197 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
198 ghp->seq_tx |= (ctrl[1] >> 2);
199 if (ctrl[1] & 0x02) {
200 ghp->is_encrypted = 1;
201 /* FIXME: encryption */
202 }
203 if (ctrl[1] & 0x01) {
204 /* FCS over hdr + all inf fields */
205 } else {
206 /* FCS over hdr + N202 octets (4) */
207 if (ghp->crc_length > UI_HDR_LEN + N202)
208 ghp->crc_length = UI_HDR_LEN + N202;
209 }
210 } else {
211 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
212 ghp->data = NULL;
213 ghp->data_len = 0;
214
215 switch (ctrl[0] & 0xf) {
216 case GPRS_LLC_U_NULL_CMD:
217 ghp->cmd = GPRS_LLC_NULL;
218 break;
219 case GPRS_LLC_U_DM_RESP:
220 ghp->cmd = GPRS_LLC_DM;
221 break;
222 case GPRS_LLC_U_DISC_CMD:
223 ghp->cmd = GPRS_LLC_DISC;
224 break;
225 case GPRS_LLC_U_UA_RESP:
226 ghp->cmd = GPRS_LLC_UA;
227 break;
228 case GPRS_LLC_U_SABM_CMD:
229 ghp->cmd = GPRS_LLC_SABM;
230 break;
231 case GPRS_LLC_U_FRMR_RESP:
232 ghp->cmd = GPRS_LLC_FRMR;
233 break;
234 case GPRS_LLC_U_XID:
235 ghp->cmd = GPRS_LLC_XID;
236 ghp->data = ctrl + 1;
237 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
238 break;
239 default:
240 return -EIO;
241 }
242 }
243
244 /* FIXME: parse sack frame */
245 if (ghp->cmd == GPRS_LLC_SACK) {
246 LOGP(DLLC, LOGL_NOTICE, "Unsupported SACK frame\n");
247 return -EIO;
248 }
249
250 return 0;
251}