blob: 424dff519d71063810533082b2c612ec8ae95844 [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
Holger Hans Peter Freytherbc1e52c2013-08-22 08:44:38 +020044int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
45 const uint8_t *llc_hdr, int len)
46{
47 const uint8_t *ctrl = llc_hdr+1;
48
49 if (len <= CRC24_LENGTH)
50 return -EIO;
51
52 ghp->crc_length = len - CRC24_LENGTH;
53
54 ghp->ack_req = 0;
55
56 /* Section 5.5: FCS */
57 ghp->fcs = *(llc_hdr + len - 3);
58 ghp->fcs |= *(llc_hdr + len - 2) << 8;
59 ghp->fcs |= *(llc_hdr + len - 1) << 16;
60
61 /* Section 6.2.1: invalid PD field */
62 if (llc_hdr[0] & 0x80)
63 return -EIO;
64
65 /* This only works for the MS->SGSN direction */
66 if (llc_hdr[0] & 0x40)
67 ghp->is_cmd = 0;
68 else
69 ghp->is_cmd = 1;
70
71 ghp->sapi = llc_hdr[0] & 0xf;
72
73 /* Section 6.2.3: check for reserved SAPI */
74 switch (ghp->sapi) {
75 case 0:
76 case 4:
77 case 6:
78 case 0xa:
79 case 0xc:
80 case 0xd:
81 case 0xf:
82 return -EINVAL;
83 }
84
85 if ((ctrl[0] & 0x80) == 0) {
86 /* I (Information transfer + Supervisory) format */
87 uint8_t k;
88
89 ghp->data = ctrl + 3;
90
91 if (ctrl[0] & 0x40)
92 ghp->ack_req = 1;
93
94 ghp->seq_tx = (ctrl[0] & 0x1f) << 4;
95 ghp->seq_tx |= (ctrl[1] >> 4);
96
97 ghp->seq_rx = (ctrl[1] & 0x7) << 6;
98 ghp->seq_rx |= (ctrl[2] >> 2);
99
100 switch (ctrl[2] & 0x03) {
101 case 0:
102 ghp->cmd = GPRS_LLC_RR;
103 break;
104 case 1:
105 ghp->cmd = GPRS_LLC_ACK;
106 break;
107 case 2:
108 ghp->cmd = GPRS_LLC_RNR;
109 break;
110 case 3:
111 ghp->cmd = GPRS_LLC_SACK;
112 k = ctrl[3] & 0x1f;
113 ghp->data += 1 + k;
114 break;
115 }
116 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
117 } else if ((ctrl[0] & 0xc0) == 0x80) {
118 /* S (Supervisory) format */
119 ghp->data = NULL;
120 ghp->data_len = 0;
121
122 if (ctrl[0] & 0x20)
123 ghp->ack_req = 1;
124 ghp->seq_rx = (ctrl[0] & 0x7) << 6;
125 ghp->seq_rx |= (ctrl[1] >> 2);
126
127 switch (ctrl[1] & 0x03) {
128 case 0:
129 ghp->cmd = GPRS_LLC_RR;
130 break;
131 case 1:
132 ghp->cmd = GPRS_LLC_ACK;
133 break;
134 case 2:
135 ghp->cmd = GPRS_LLC_RNR;
136 break;
137 case 3:
138 ghp->cmd = GPRS_LLC_SACK;
139 break;
140 }
141 } else if ((ctrl[0] & 0xe0) == 0xc0) {
142 /* UI (Unconfirmed Inforamtion) format */
143 ghp->cmd = GPRS_LLC_UI;
144 ghp->data = ctrl + 2;
145 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
146
147 ghp->seq_tx = (ctrl[0] & 0x7) << 6;
148 ghp->seq_tx |= (ctrl[1] >> 2);
149 if (ctrl[1] & 0x02) {
150 ghp->is_encrypted = 1;
151 /* FIXME: encryption */
152 }
153 if (ctrl[1] & 0x01) {
154 /* FCS over hdr + all inf fields */
155 } else {
156 /* FCS over hdr + N202 octets (4) */
157 if (ghp->crc_length > UI_HDR_LEN + N202)
158 ghp->crc_length = UI_HDR_LEN + N202;
159 }
160 } else {
161 /* U (Unnumbered) format: 1 1 1 P/F M4 M3 M2 M1 */
162 ghp->data = NULL;
163 ghp->data_len = 0;
164
165 switch (ctrl[0] & 0xf) {
166 case GPRS_LLC_U_NULL_CMD:
167 ghp->cmd = GPRS_LLC_NULL;
168 break;
169 case GPRS_LLC_U_DM_RESP:
170 ghp->cmd = GPRS_LLC_DM;
171 break;
172 case GPRS_LLC_U_DISC_CMD:
173 ghp->cmd = GPRS_LLC_DISC;
174 break;
175 case GPRS_LLC_U_UA_RESP:
176 ghp->cmd = GPRS_LLC_UA;
177 break;
178 case GPRS_LLC_U_SABM_CMD:
179 ghp->cmd = GPRS_LLC_SABM;
180 break;
181 case GPRS_LLC_U_FRMR_RESP:
182 ghp->cmd = GPRS_LLC_FRMR;
183 break;
184 case GPRS_LLC_U_XID:
185 ghp->cmd = GPRS_LLC_XID;
186 ghp->data = ctrl + 1;
187 ghp->data_len = (llc_hdr + len - 3) - ghp->data;
188 break;
189 default:
190 return -EIO;
191 }
192 }
193
194 /* FIXME: parse sack frame */
195 if (ghp->cmd == GPRS_LLC_SACK) {
196 LOGP(DPCU, LOGL_NOTICE, "Unsupported SACK frame\n");
197 return -EIO;
198 }
199
200 return 0;
201}
Holger Hans Peter Freyther9d938382013-07-31 21:59:29 +0200202
203const struct tlv_definition gsm48_gmm_att_tlvdef = {
204 .def = {
205 [GSM48_IE_GMM_CIPH_CKSN] = { TLV_TYPE_FIXED, 1 },
206 [GSM48_IE_GMM_TIMER_READY] = { TLV_TYPE_TV, 1 },
207 [GSM48_IE_GMM_ALLOC_PTMSI] = { TLV_TYPE_TLV, 0 },
208 [GSM48_IE_GMM_PTMSI_SIG] = { TLV_TYPE_FIXED, 3 },
209 [GSM48_IE_GMM_AUTH_RAND] = { TLV_TYPE_FIXED, 16 },
210 [GSM48_IE_GMM_AUTH_SRES] = { TLV_TYPE_FIXED, 4 },
211 [GSM48_IE_GMM_IMEISV] = { TLV_TYPE_TLV, 0 },
212 [GSM48_IE_GMM_DRX_PARAM] = { TLV_TYPE_FIXED, 2 },
213 [GSM48_IE_GMM_MS_NET_CAPA] = { TLV_TYPE_TLV, 0 },
214 [GSM48_IE_GMM_PDP_CTX_STATUS] = { TLV_TYPE_TLV, 0 },
215 [GSM48_IE_GMM_PS_LCS_CAPA] = { TLV_TYPE_TLV, 0 },
216 [GSM48_IE_GMM_GMM_MBMS_CTX_ST] = { TLV_TYPE_TLV, 0 },
217 },
218};