blob: 575f4e15510421180ae9954d5d41467396af7315 [file] [log] [blame]
Holger Hans Peter Freytherbc1e52c2013-08-22 08:44:38 +02001/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
2 *
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19#include "openbsc_clone.h"
20
21#include <gprs_debug.h>
22
23#include <osmocom/core/utils.h>
Holger Hans Peter Freyther9d938382013-07-31 21:59:29 +020024#include <osmocom/gsm/tlv.h>
Holger Hans Peter Freytherbc1e52c2013-08-22 08:44:38 +020025
26#include <errno.h>
27
28/* Section 6.4 Commands and Responses */
29enum gprs_llc_u_cmd {
30 GPRS_LLC_U_DM_RESP = 0x01,
31 GPRS_LLC_U_DISC_CMD = 0x04,
32 GPRS_LLC_U_UA_RESP = 0x06,
33 GPRS_LLC_U_SABM_CMD = 0x07,
34 GPRS_LLC_U_FRMR_RESP = 0x08,
35 GPRS_LLC_U_XID = 0x0b,
36 GPRS_LLC_U_NULL_CMD = 0x00,
37};
38
39#define LLC_ALLOC_SIZE 16384
40#define UI_HDR_LEN 3
41#define N202 4
42#define CRC24_LENGTH 3
43
44static const struct value_string llc_cmd_strs[] = {
45 { GPRS_LLC_NULL, "NULL" },
46 { GPRS_LLC_RR, "RR" },
47 { GPRS_LLC_ACK, "ACK" },
48 { GPRS_LLC_RNR, "RNR" },
49 { GPRS_LLC_SACK, "SACK" },
50 { GPRS_LLC_DM, "DM" },
51 { GPRS_LLC_DISC, "DISC" },
52 { GPRS_LLC_UA, "UA" },
53 { GPRS_LLC_SABM, "SABM" },
54 { GPRS_LLC_FRMR, "FRMR" },
55 { GPRS_LLC_XID, "XID" },
56 { GPRS_LLC_UI, "UI" },
57 { 0, NULL }
58};
59
60int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
61 const uint8_t *llc_hdr, int len)
62{
63 const uint8_t *ctrl = llc_hdr+1;
64
65 if (len <= CRC24_LENGTH)
66 return -EIO;
67
68 ghp->crc_length = len - CRC24_LENGTH;
69
70 ghp->ack_req = 0;
71
72 /* Section 5.5: FCS */
73 ghp->fcs = *(llc_hdr + len - 3);
74 ghp->fcs |= *(llc_hdr + len - 2) << 8;
75 ghp->fcs |= *(llc_hdr + len - 1) << 16;
76
77 /* Section 6.2.1: invalid PD field */
78 if (llc_hdr[0] & 0x80)
79 return -EIO;
80
81 /* This only works for the MS->SGSN direction */
82 if (llc_hdr[0] & 0x40)
83 ghp->is_cmd = 0;
84 else
85 ghp->is_cmd = 1;
86
87 ghp->sapi = llc_hdr[0] & 0xf;
88
89 /* Section 6.2.3: check for reserved SAPI */
90 switch (ghp->sapi) {
91 case 0:
92 case 4:
93 case 6:
94 case 0xa:
95 case 0xc:
96 case 0xd:
97 case 0xf:
98 return -EINVAL;
99 }
100
101 if ((ctrl[0] & 0x80) == 0) {
102 /* I (Information transfer + Supervisory) format */
103 uint8_t k;
104
105 ghp->data = ctrl + 3;
106
107 if (ctrl[0] & 0x40)
108 ghp->ack_req = 1;
109
110 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
111 ghp->seq_tx |= (ctrl[1] >> 4);
112
113 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
114 ghp->seq_rx |= (ctrl[2] >> 2);
115
116 switch (ctrl[2] & 0x03) {
117 case 0:
118 ghp->cmd = GPRS_LLC_RR;
119 break;
120 case 1:
121 ghp->cmd = GPRS_LLC_ACK;
122 break;
123 case 2:
124 ghp->cmd = GPRS_LLC_RNR;
125 break;
126 case 3:
127 ghp->cmd = GPRS_LLC_SACK;
128 k = ctrl[3] & 0x1f;
129 ghp->data += 1 + k;
130 break;
131 }
132 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
133 } else if ((ctrl[0] & 0xc0) == 0x80) {
134 /* S (Supervisory) format */
135 ghp->data = NULL;
136 ghp->data_len = 0;
137
138 if (ctrl[0] & 0x20)
139 ghp->ack_req = 1;
140 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
141 ghp->seq_rx |= (ctrl[1] >> 2);
142
143 switch (ctrl[1] & 0x03) {
144 case 0:
145 ghp->cmd = GPRS_LLC_RR;
146 break;
147 case 1:
148 ghp->cmd = GPRS_LLC_ACK;
149 break;
150 case 2:
151 ghp->cmd = GPRS_LLC_RNR;
152 break;
153 case 3:
154 ghp->cmd = GPRS_LLC_SACK;
155 break;
156 }
157 } else if ((ctrl[0] & 0xe0) == 0xc0) {
158 /* UI (Unconfirmed Inforamtion) format */
159 ghp->cmd = GPRS_LLC_UI;
160 ghp->data = ctrl + 2;
161 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
162
163 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
164 ghp->seq_tx |= (ctrl[1] >> 2);
165 if (ctrl[1] & 0x02) {
166 ghp->is_encrypted = 1;
167 /* FIXME: encryption */
168 }
169 if (ctrl[1] & 0x01) {
170 /* FCS over hdr + all inf fields */
171 } else {
172 /* FCS over hdr + N202 octets (4) */
173 if (ghp->crc_length > UI_HDR_LEN + N202)
174 ghp->crc_length = UI_HDR_LEN + N202;
175 }
176 } else {
177 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
178 ghp->data = NULL;
179 ghp->data_len = 0;
180
181 switch (ctrl[0] & 0xf) {
182 case GPRS_LLC_U_NULL_CMD:
183 ghp->cmd = GPRS_LLC_NULL;
184 break;
185 case GPRS_LLC_U_DM_RESP:
186 ghp->cmd = GPRS_LLC_DM;
187 break;
188 case GPRS_LLC_U_DISC_CMD:
189 ghp->cmd = GPRS_LLC_DISC;
190 break;
191 case GPRS_LLC_U_UA_RESP:
192 ghp->cmd = GPRS_LLC_UA;
193 break;
194 case GPRS_LLC_U_SABM_CMD:
195 ghp->cmd = GPRS_LLC_SABM;
196 break;
197 case GPRS_LLC_U_FRMR_RESP:
198 ghp->cmd = GPRS_LLC_FRMR;
199 break;
200 case GPRS_LLC_U_XID:
201 ghp->cmd = GPRS_LLC_XID;
202 ghp->data = ctrl + 1;
203 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
204 break;
205 default:
206 return -EIO;
207 }
208 }
209
210 /* FIXME: parse sack frame */
211 if (ghp->cmd == GPRS_LLC_SACK) {
212 LOGP(DPCU, LOGL_NOTICE, "Unsupported SACK frame\n");
213 return -EIO;
214 }
215
216 return 0;
217}
Holger Hans Peter Freyther9d938382013-07-31 21:59:29 +0200218
219const struct tlv_definition gsm48_gmm_att_tlvdef = {
220 .def = {
221 [GSM48_IE_GMM_CIPH_CKSN] = { TLV_TYPE_FIXED, 1 },
222 [GSM48_IE_GMM_TIMER_READY] = { TLV_TYPE_TV, 1 },
223 [GSM48_IE_GMM_ALLOC_PTMSI] = { TLV_TYPE_TLV, 0 },
224 [GSM48_IE_GMM_PTMSI_SIG] = { TLV_TYPE_FIXED, 3 },
225 [GSM48_IE_GMM_AUTH_RAND] = { TLV_TYPE_FIXED, 16 },
226 [GSM48_IE_GMM_AUTH_SRES] = { TLV_TYPE_FIXED, 4 },
227 [GSM48_IE_GMM_IMEISV] = { TLV_TYPE_TLV, 0 },
228 [GSM48_IE_GMM_DRX_PARAM] = { TLV_TYPE_FIXED, 2 },
229 [GSM48_IE_GMM_MS_NET_CAPA] = { TLV_TYPE_TLV, 0 },
230 [GSM48_IE_GMM_PDP_CTX_STATUS] = { TLV_TYPE_TLV, 0 },
231 [GSM48_IE_GMM_PS_LCS_CAPA] = { TLV_TYPE_TLV, 0 },
232 [GSM48_IE_GMM_GMM_MBMS_CTX_ST] = { TLV_TYPE_TLV, 0 },
233 },
234};