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