blob: f17db32259d4d387584f7ac4677ea382c9756c74 [file] [log] [blame]
Holger Hans Peter Freyther3485feb2010-11-09 23:28:33 +01001/* GSM 08.08 BSSMAP handling */
Holger Hans Peter Freyther8ec49522011-08-15 15:53:00 +02002/* (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2009-2011 by On-Waves
Holger Hans Peter Freyther3485feb2010-11-09 23:28:33 +01004 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01007 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freyther3485feb2010-11-09 23:28:33 +01009 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010014 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther3485feb2010-11-09 23:28:33 +010015 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther3485feb2010-11-09 23:28:33 +010018 *
19 */
20
21#include <openbsc/osmo_bsc.h>
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +010022#include <openbsc/osmo_bsc_grace.h>
Holger Hans Peter Freyther50c579b2010-11-10 10:07:30 +010023#include <openbsc/osmo_msc_data.h>
Holger Hans Peter Freyther3485feb2010-11-09 23:28:33 +010024#include <openbsc/debug.h>
Holger Hans Peter Freyther9c838ae2010-11-15 09:16:09 +010025#include <openbsc/gsm_subscriber.h>
Holger Hans Peter Freyther50c579b2010-11-10 10:07:30 +010026#include <openbsc/mgcp.h>
Holger Hans Peter Freyther9c838ae2010-11-15 09:16:09 +010027#include <openbsc/paging.h>
Holger Hans Peter Freyther3485feb2010-11-09 23:28:33 +010028
Harald Welted36ff762011-03-23 18:26:56 +010029#include <osmocom/gsm/protocol/gsm_08_08.h>
30#include <osmocom/gsm/gsm0808.h>
Holger Hans Peter Freyther3485feb2010-11-09 23:28:33 +010031
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +010032#include <arpa/inet.h>
33
34static uint16_t read_data16(const uint8_t *data)
35{
36 uint16_t res;
37
38 memcpy(&res, data, sizeof(res));
39 return res;
40}
41
Holger Hans Peter Freyther50c579b2010-11-10 10:07:30 +010042/*
43 * helpers for the assignment command
44 */
45enum gsm0808_permitted_speech audio_support_to_gsm88(struct gsm_audio_support *audio)
46{
47 if (audio->hr) {
48 switch (audio->ver) {
49 case 1:
50 return GSM0808_PERM_HR1;
51 break;
52 case 2:
53 return GSM0808_PERM_HR2;
54 break;
55 case 3:
56 return GSM0808_PERM_HR3;
57 break;
58 default:
59 LOGP(DMSC, LOGL_ERROR, "Wrong speech mode: %d\n", audio->ver);
60 return GSM0808_PERM_FR1;
61 }
62 } else {
63 switch (audio->ver) {
64 case 1:
65 return GSM0808_PERM_FR1;
66 break;
67 case 2:
68 return GSM0808_PERM_FR2;
69 break;
70 case 3:
71 return GSM0808_PERM_FR3;
72 break;
73 default:
74 LOGP(DMSC, LOGL_ERROR, "Wrong speech mode: %d\n", audio->ver);
75 return GSM0808_PERM_HR1;
76 }
77 }
78}
79
80enum gsm48_chan_mode gsm88_to_chan_mode(enum gsm0808_permitted_speech speech)
81{
82 switch (speech) {
83 case GSM0808_PERM_HR1:
84 case GSM0808_PERM_FR1:
85 return GSM48_CMODE_SPEECH_V1;
86 break;
87 case GSM0808_PERM_HR2:
88 case GSM0808_PERM_FR2:
89 return GSM48_CMODE_SPEECH_EFR;
90 break;
91 case GSM0808_PERM_HR3:
92 case GSM0808_PERM_FR3:
93 return GSM48_CMODE_SPEECH_AMR;
94 break;
95 }
96
97 LOGP(DMSC, LOGL_FATAL, "Should not be reached.\n");
98 return GSM48_CMODE_SPEECH_AMR;
99}
100
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +0100101static int bssmap_handle_reset_ack(struct gsm_network *net,
102 struct msgb *msg, unsigned int length)
103{
104 LOGP(DMSC, LOGL_NOTICE, "Reset ACK from MSC\n");
105 return 0;
106}
107
108/* GSM 08.08 ยง 3.2.1.19 */
109static int bssmap_handle_paging(struct gsm_network *net,
110 struct msgb *msg, unsigned int payload_length)
111{
Holger Hans Peter Freyther9c838ae2010-11-15 09:16:09 +0100112 struct gsm_subscriber *subscr;
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +0100113 struct tlv_parsed tp;
114 char mi_string[GSM48_MI_SIZE];
115 uint32_t tmsi = GSM_RESERVED_TMSI;
116 unsigned int lac = GSM_LAC_RESERVED_ALL_BTS;
117 uint8_t data_length;
118 const uint8_t *data;
119 uint8_t chan_needed = RSL_CHANNEED_ANY;
120
121 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h + 1, payload_length - 1, 0, 0);
122
123 if (!TLVP_PRESENT(&tp, GSM0808_IE_IMSI)) {
Holger Hans Peter Freyther59043d82012-08-03 11:05:29 +0200124 LOGP(DMSC, LOGL_ERROR, "Mandatory IMSI not present.\n");
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +0100125 return -1;
126 } else if ((TLVP_VAL(&tp, GSM0808_IE_IMSI)[0] & GSM_MI_TYPE_MASK) != GSM_MI_TYPE_IMSI) {
127 LOGP(DMSC, LOGL_ERROR, "Wrong content in the IMSI\n");
128 return -1;
129 }
130
131 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
Holger Hans Peter Freyther59043d82012-08-03 11:05:29 +0200132 LOGP(DMSC, LOGL_ERROR, "Mandatory CELL IDENTIFIER LIST not present.\n");
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +0100133 return -1;
134 }
135
Harald Welted55cef92012-01-20 17:20:51 +0100136 if (TLVP_PRESENT(&tp, GSM0808_IE_TMSI) &&
137 TLVP_LEN(&tp, GSM0808_IE_TMSI) == 4) {
138 tmsi = ntohl(*(uint32_t *) TLVP_VAL(&tp, GSM0808_IE_TMSI));
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +0100139 }
140
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +0100141 /*
142 * parse the IMSI
143 */
144 gsm48_mi_to_string(mi_string, sizeof(mi_string),
145 TLVP_VAL(&tp, GSM0808_IE_IMSI), TLVP_LEN(&tp, GSM0808_IE_IMSI));
146
147 /*
148 * parse the cell identifier list
149 */
150 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
151 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
152
153 /*
154 * Support paging to all network or one BTS at one LAC
155 */
156 if (data_length == 3 && data[0] == CELL_IDENT_LAC) {
157 lac = ntohs(read_data16(&data[1]));
158 } else if (data_length > 1 || (data[0] & 0x0f) != CELL_IDENT_BSS) {
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200159 LOGP(DMSC, LOGL_ERROR, "Unsupported Cell Identifier List: %s\n", osmo_hexdump(data, data_length));
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +0100160 return -1;
161 }
162
163 if (TLVP_PRESENT(&tp, GSM0808_IE_CHANNEL_NEEDED) && TLVP_LEN(&tp, GSM0808_IE_CHANNEL_NEEDED) == 1)
164 chan_needed = TLVP_VAL(&tp, GSM0808_IE_CHANNEL_NEEDED)[0] & 0x03;
165
166 if (TLVP_PRESENT(&tp, GSM0808_IE_EMLPP_PRIORITY)) {
167 LOGP(DMSC, LOGL_ERROR, "eMLPP is not handled\n");
168 }
169
Holger Hans Peter Freyther9c838ae2010-11-15 09:16:09 +0100170 subscr = subscr_get_or_create(net, mi_string);
171 if (!subscr) {
172 LOGP(DMSC, LOGL_ERROR, "Failed to allocate a subscriber for %s\n", mi_string);
173 return -1;
174 }
175
176 subscr->lac = lac;
177 subscr->tmsi = tmsi;
178
Harald Weltecccd3012011-07-11 18:18:35 +0200179 LOGP(DMSC, LOGL_INFO, "Paging request from MSC IMSI: '%s' TMSI: '0x%x/%u' LAC: 0x%x\n", mi_string, tmsi, tmsi, lac);
Holger Hans Peter Freyther9c838ae2010-11-15 09:16:09 +0100180 paging_request(net, subscr, chan_needed, NULL, NULL);
181 return 0;
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +0100182}
183
Holger Hans Peter Freytherf1f57a82010-11-10 09:34:47 +0100184/*
185 * GSM 08.08 ยง 3.1.9.1 and 3.2.1.21...
186 * release our gsm_subscriber_connection and send message
187 */
188static int bssmap_handle_clear_command(struct osmo_bsc_sccp_con *conn,
189 struct msgb *msg, unsigned int payload_length)
190{
191 struct msgb *resp;
192
193 /* TODO: handle the cause of this package */
194
195 if (conn->conn) {
Harald Weltecccd3012011-07-11 18:18:35 +0200196 LOGP(DMSC, LOGL_INFO, "Releasing all transactions on %p\n", conn);
Holger Hans Peter Freytherf1f57a82010-11-10 09:34:47 +0100197 gsm0808_clear(conn->conn);
198 subscr_con_free(conn->conn);
199 conn->conn = NULL;
200 }
201
202 /* send the clear complete message */
203 resp = gsm0808_create_clear_complete();
204 if (!resp) {
205 LOGP(DMSC, LOGL_ERROR, "Sending clear complete failed.\n");
206 return -1;
207 }
208
209 bsc_queue_for_msc(conn, resp);
210 return 0;
211}
212
Holger Hans Peter Freytherfae3c652010-11-10 09:44:34 +0100213/*
214 * GSM 08.08 ยง 3.4.7 cipher mode handling. We will have to pick
215 * the cipher to be used for this. In case we are already using
216 * a cipher we will have to send cipher mode reject to the MSC,
217 * otherwise we will have to pick something that we and the MS
218 * is supporting. Currently we are doing it in a rather static
219 * way by picking one ecnryption or no encrytpion.
220 */
221static int bssmap_handle_cipher_mode(struct osmo_bsc_sccp_con *conn,
222 struct msgb *msg, unsigned int payload_length)
223{
224 uint16_t len;
225 struct gsm_network *network = NULL;
226 const uint8_t *data;
227 struct tlv_parsed tp;
228 struct msgb *resp;
229 int reject_cause = -1;
230 int include_imeisv = 1;
231
232 if (!conn->conn) {
233 LOGP(DMSC, LOGL_ERROR, "No lchan/msc_data in cipher mode command.\n");
234 goto reject;
235 }
236
237 if (conn->ciphering_handled) {
238 LOGP(DMSC, LOGL_ERROR, "Already seen ciphering command. Protocol Error.\n");
239 goto reject;
240 }
241
242 conn->ciphering_handled = 1;
243
244 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h + 1, payload_length - 1, 0, 0);
245 if (!TLVP_PRESENT(&tp, GSM0808_IE_ENCRYPTION_INFORMATION)) {
246 LOGP(DMSC, LOGL_ERROR, "IE Encryption Information missing.\n");
247 goto reject;
248 }
249
250 /*
251 * check if our global setting is allowed
252 * - Currently we check for A5/0 and A5/1
253 * - Copy the key if that is necessary
254 * - Otherwise reject
255 */
256 len = TLVP_LEN(&tp, GSM0808_IE_ENCRYPTION_INFORMATION);
257 if (len < 1) {
258 LOGP(DMSC, LOGL_ERROR, "IE Encryption Information is too short.\n");
259 goto reject;
260 }
261
262 network = conn->conn->bts->network;
263 data = TLVP_VAL(&tp, GSM0808_IE_ENCRYPTION_INFORMATION);
264
265 if (TLVP_PRESENT(&tp, GSM0808_IE_CIPHER_RESPONSE_MODE))
266 include_imeisv = TLVP_VAL(&tp, GSM0808_IE_CIPHER_RESPONSE_MODE)[0] & 0x1;
267
268 if (network->a5_encryption == 0 && (data[0] & 0x1) == 0x1) {
269 gsm0808_cipher_mode(conn->conn, 0, NULL, 0, include_imeisv);
270 } else if (network->a5_encryption != 0 && (data[0] & 0x2) == 0x2) {
271 gsm0808_cipher_mode(conn->conn, 1, &data[1], len - 1, include_imeisv);
272 } else {
273 LOGP(DMSC, LOGL_ERROR, "Can not select encryption...\n");
274 goto reject;
275 }
276
Harald Welte75413c42011-07-12 00:03:43 +0200277 return 0;
278
Holger Hans Peter Freytherfae3c652010-11-10 09:44:34 +0100279reject:
280 resp = gsm0808_create_cipher_reject(reject_cause);
281 if (!resp) {
282 LOGP(DMSC, LOGL_ERROR, "Sending the cipher reject failed.\n");
283 return -1;
284 }
285
286 bsc_queue_for_msc(conn, resp);
287 return -1;
288}
289
Holger Hans Peter Freyther50c579b2010-11-10 10:07:30 +0100290/*
291 * Handle the assignment request message.
292 *
293 * See ยง3.2.1.1 for the message type
294 */
295static int bssmap_handle_assignm_req(struct osmo_bsc_sccp_con *conn,
296 struct msgb *msg, unsigned int length)
297{
298 struct msgb *resp;
Holger Hans Peter Freyther8ec49522011-08-15 15:53:00 +0200299 struct osmo_msc_data *msc;
Holger Hans Peter Freyther50c579b2010-11-10 10:07:30 +0100300 struct tlv_parsed tp;
301 uint8_t *data;
302 uint16_t cic;
303 uint8_t timeslot;
304 uint8_t multiplex;
305 enum gsm48_chan_mode chan_mode = GSM48_CMODE_SIGN;
306 int i, supported, port, full_rate = -1;
307
308 if (!conn->conn) {
309 LOGP(DMSC, LOGL_ERROR, "No lchan/msc_data in cipher mode command.\n");
310 return -1;
311 }
312
Holger Hans Peter Freyther50c579b2010-11-10 10:07:30 +0100313 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h + 1, length - 1, 0, 0);
314
315 if (!TLVP_PRESENT(&tp, GSM0808_IE_CHANNEL_TYPE)) {
Holger Hans Peter Freyther59043d82012-08-03 11:05:29 +0200316 LOGP(DMSC, LOGL_ERROR, "Mandatory channel type not present.\n");
Holger Hans Peter Freyther50c579b2010-11-10 10:07:30 +0100317 goto reject;
318 }
319
320 if (!TLVP_PRESENT(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)) {
321 LOGP(DMSC, LOGL_ERROR, "Identity code missing. Audio routing will not work.\n");
322 goto reject;
323 }
324
325 cic = ntohs(read_data16(TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)));
326 timeslot = cic & 0x1f;
327 multiplex = (cic & ~0x1f) >> 5;
328
329 /*
330 * Currently we only support a limited subset of all
331 * possible channel types. The limitation ends by not using
332 * multi-slot, limiting the channel coding, speech...
333 */
334 if (TLVP_LEN(&tp, GSM0808_IE_CHANNEL_TYPE) < 3) {
335 LOGP(DMSC, LOGL_ERROR, "ChannelType len !=3 not supported: %d\n",
336 TLVP_LEN(&tp, GSM0808_IE_CHANNEL_TYPE));
337 goto reject;
338 }
339
340 /*
341 * Try to figure out if we support the proposed speech codecs. For
342 * now we will always pick the full rate codecs.
343 */
344
345 data = (uint8_t *) TLVP_VAL(&tp, GSM0808_IE_CHANNEL_TYPE);
346 if ((data[0] & 0xf) != 0x1) {
347 LOGP(DMSC, LOGL_ERROR, "ChannelType != speech: %d\n", data[0]);
348 goto reject;
349 }
350
Holger Hans Peter Freyther50c579b2010-11-10 10:07:30 +0100351 /*
352 * go through the list of preferred codecs of our gsm network
353 * and try to find it among the permitted codecs. If we found
354 * it we will send chan_mode to the right mode and break the
355 * inner loop. The outer loop will exit due chan_mode having
356 * the correct value.
357 */
358 full_rate = 0;
Holger Hans Peter Freytherf936fb42011-06-04 15:12:57 +0200359 msc = conn->msc;
Holger Hans Peter Freyther50c579b2010-11-10 10:07:30 +0100360 for (supported = 0;
Holger Hans Peter Freyther8ec49522011-08-15 15:53:00 +0200361 chan_mode == GSM48_CMODE_SIGN && supported < msc->audio_length;
Holger Hans Peter Freyther50c579b2010-11-10 10:07:30 +0100362 ++supported) {
363
Holger Hans Peter Freyther8ec49522011-08-15 15:53:00 +0200364 int perm_val = audio_support_to_gsm88(msc->audio_support[supported]);
Holger Hans Peter Freyther50c579b2010-11-10 10:07:30 +0100365 for (i = 2; i < TLVP_LEN(&tp, GSM0808_IE_CHANNEL_TYPE); ++i) {
366 if ((data[i] & 0x7f) == perm_val) {
367 chan_mode = gsm88_to_chan_mode(perm_val);
368 full_rate = (data[i] & 0x4) == 0;
369 break;
370 } else if ((data[i] & 0x80) == 0x00) {
371 break;
372 }
373 }
374 }
375
376 if (chan_mode == GSM48_CMODE_SIGN) {
377 LOGP(DMSC, LOGL_ERROR, "No supported audio type found.\n");
378 goto reject;
379 }
380
381 /* map it to a MGCP Endpoint and a RTP port */
382 port = mgcp_timeslot_to_endpoint(multiplex, timeslot);
Holger Hans Peter Freyther8ec49522011-08-15 15:53:00 +0200383 conn->rtp_port = rtp_calculate_port(port, msc->rtp_base);
Holger Hans Peter Freyther50c579b2010-11-10 10:07:30 +0100384
385 return gsm0808_assign_req(conn->conn, chan_mode, full_rate);
386
387reject:
388 resp = gsm0808_create_assignment_failure(GSM0808_CAUSE_NO_RADIO_RESOURCE_AVAILABLE, NULL);
389 if (!resp) {
390 LOGP(DMSC, LOGL_ERROR, "Channel allocation failure.\n");
391 return -1;
392 }
393
394 bsc_queue_for_msc(conn, resp);
395 return -1;
396}
397
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +0100398static int bssmap_rcvmsg_udt(struct gsm_network *net,
399 struct msgb *msg, unsigned int length)
400{
401 int ret = 0;
402
403 if (length < 1) {
404 LOGP(DMSC, LOGL_ERROR, "Not enough room: %d\n", length);
405 return -1;
406 }
407
Harald Weltecccd3012011-07-11 18:18:35 +0200408 LOGP(DMSC, LOGL_INFO, "Rx MSC UDT BSSMAP %s\n",
409 gsm0808_bssmap_name(msg->l4h[0]));
410
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +0100411 switch (msg->l4h[0]) {
412 case BSS_MAP_MSG_RESET_ACKNOWLEDGE:
413 ret = bssmap_handle_reset_ack(net, msg, length);
414 break;
415 case BSS_MAP_MSG_PAGING:
416 if (bsc_grace_allow_new_connection(net))
417 ret = bssmap_handle_paging(net, msg, length);
418 break;
419 }
420
421 return ret;
422}
423
424static int bssmap_rcvmsg_dt1(struct osmo_bsc_sccp_con *conn,
425 struct msgb *msg, unsigned int length)
426{
Holger Hans Peter Freytherf1f57a82010-11-10 09:34:47 +0100427 int ret = 0;
428
429 if (length < 1) {
430 LOGP(DMSC, LOGL_ERROR, "Not enough room: %d\n", length);
431 return -1;
432 }
433
Harald Weltecccd3012011-07-11 18:18:35 +0200434 LOGP(DMSC, LOGL_INFO, "Rx MSC DT1 BSSMAP %s\n",
435 gsm0808_bssmap_name(msg->l4h[0]));
436
Holger Hans Peter Freytherf1f57a82010-11-10 09:34:47 +0100437 switch (msg->l4h[0]) {
438 case BSS_MAP_MSG_CLEAR_CMD:
439 ret = bssmap_handle_clear_command(conn, msg, length);
440 break;
Holger Hans Peter Freytherfae3c652010-11-10 09:44:34 +0100441 case BSS_MAP_MSG_CIPHER_MODE_CMD:
442 ret = bssmap_handle_cipher_mode(conn, msg, length);
443 break;
Holger Hans Peter Freyther50c579b2010-11-10 10:07:30 +0100444 case BSS_MAP_MSG_ASSIGMENT_RQST:
445 ret = bssmap_handle_assignm_req(conn, msg, length);
446 break;
Holger Hans Peter Freytherf1f57a82010-11-10 09:34:47 +0100447 default:
Harald Welte9f32a8a2011-07-11 17:56:34 +0200448 LOGP(DMSC, LOGL_NOTICE, "Unimplemented msg type: %s\n",
449 gsm0808_bssmap_name(msg->l4h[0]));
Holger Hans Peter Freytherf1f57a82010-11-10 09:34:47 +0100450 break;
451 }
452
453 return ret;
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +0100454}
455
456static int dtap_rcvmsg(struct osmo_bsc_sccp_con *conn,
457 struct msgb *msg, unsigned int length)
458{
Holger Hans Peter Freytherdbc698a2010-11-10 10:17:05 +0100459 struct dtap_header *header;
460 struct msgb *gsm48;
461 uint8_t *data;
462
Harald Weltecccd3012011-07-11 18:18:35 +0200463 LOGP(DMSC, LOGL_DEBUG, "Rx MSC DTAP: %s\n",
464 osmo_hexdump(msg->l3h, length));
465
Holger Hans Peter Freytherdbc698a2010-11-10 10:17:05 +0100466 if (!conn->conn) {
467 LOGP(DMSC, LOGL_ERROR, "No subscriber connection available\n");
468 return -1;
469 }
470
471 header = (struct dtap_header *) msg->l3h;
472 if (sizeof(*header) >= length) {
Holger Hans Peter Freyther2f2be572012-03-16 12:18:39 +0100473 LOGP(DMSC, LOGL_ERROR, "The DTAP header does not fit. Wanted: %zu got: %u\n", sizeof(*header), length);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200474 LOGP(DMSC, LOGL_ERROR, "hex: %s\n", osmo_hexdump(msg->l3h, length));
Holger Hans Peter Freytherdbc698a2010-11-10 10:17:05 +0100475 return -1;
476 }
477
478 if (header->length > length - sizeof(*header)) {
479 LOGP(DMSC, LOGL_ERROR, "The DTAP l4 information does not fit: header: %u length: %u\n", header->length, length);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200480 LOGP(DMSC, LOGL_ERROR, "hex: %s\n", osmo_hexdump(msg->l3h, length));
Holger Hans Peter Freytherdbc698a2010-11-10 10:17:05 +0100481 return -1;
482 }
483
Harald Weltecccd3012011-07-11 18:18:35 +0200484 LOGP(DMSC, LOGL_INFO, "Rx MSC DTAP, SAPI: %u CHAN: %u\n", header->link_id & 0x07, header->link_id & 0xC0);
Holger Hans Peter Freytherdbc698a2010-11-10 10:17:05 +0100485
486 /* forward the data */
487 gsm48 = gsm48_msgb_alloc();
488 if (!gsm48) {
489 LOGP(DMSC, LOGL_ERROR, "Allocation of the message failed.\n");
490 return -1;
491 }
492
493 gsm48->l3h = gsm48->data;
494 data = msgb_put(gsm48, length - sizeof(*header));
495 memcpy(data, msg->l3h + sizeof(*header), length - sizeof(*header));
496
497 /* pass it to the filter for extra actions */
498 bsc_scan_msc_msg(conn->conn, gsm48);
499 return gsm0808_submit_dtap(conn->conn, gsm48, header->link_id, 1);
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +0100500}
501
Holger Hans Peter Freyther3485feb2010-11-09 23:28:33 +0100502int bsc_handle_udt(struct gsm_network *network,
503 struct bsc_msc_connection *conn,
504 struct msgb *msgb, unsigned int length)
505{
506 struct bssmap_header *bs;
507
Harald Weltecccd3012011-07-11 18:18:35 +0200508 LOGP(DMSC, LOGL_DEBUG, "Rx MSC UDT: %s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200509 osmo_hexdump(msgb->l3h, length));
Holger Hans Peter Freyther3485feb2010-11-09 23:28:33 +0100510
511 if (length < sizeof(*bs)) {
512 LOGP(DMSC, LOGL_ERROR, "The header is too short.\n");
513 return -1;
514 }
515
516 bs = (struct bssmap_header *) msgb->l3h;
517 if (bs->length < length - sizeof(*bs))
518 return -1;
519
520 switch (bs->type) {
521 case BSSAP_MSG_BSS_MANAGEMENT:
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +0100522 msgb->l4h = &msgb->l3h[sizeof(*bs)];
523 bssmap_rcvmsg_udt(network, msgb, length - sizeof(*bs));
Holger Hans Peter Freyther3485feb2010-11-09 23:28:33 +0100524 break;
525 default:
Harald Welte9f32a8a2011-07-11 17:56:34 +0200526 LOGP(DMSC, LOGL_NOTICE, "Unimplemented msg type: %s\n",
527 gsm0808_bssmap_name(bs->type));
Holger Hans Peter Freyther3485feb2010-11-09 23:28:33 +0100528 }
529
530 return 0;
531}
532
533int bsc_handle_dt1(struct osmo_bsc_sccp_con *conn,
534 struct msgb *msg, unsigned int len)
535{
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +0100536 if (len < sizeof(struct bssmap_header)) {
537 LOGP(DMSC, LOGL_ERROR, "The header is too short.\n");
538 }
539
540 switch (msg->l3h[0]) {
541 case BSSAP_MSG_BSS_MANAGEMENT:
542 msg->l4h = &msg->l3h[sizeof(struct bssmap_header)];
543 bssmap_rcvmsg_dt1(conn, msg, len - sizeof(struct bssmap_header));
544 break;
545 case BSSAP_MSG_DTAP:
546 dtap_rcvmsg(conn, msg, len);
547 break;
548 default:
Harald Weltecccd3012011-07-11 18:18:35 +0200549 LOGP(DMSC, LOGL_NOTICE, "Unimplemented BSSAP msg type: %s\n",
550 gsm0808_bssap_name(msg->l3h[0]));
Holger Hans Peter Freyther890dfc52010-11-10 09:24:37 +0100551 }
552
Holger Hans Peter Freyther3485feb2010-11-09 23:28:33 +0100553 return -1;
554}