blob: 1cd67233742a0974f563af7acc7228175ac26ddc [file] [log] [blame]
Philipp Maierfbf66102017-04-09 12:32:51 +02001/* (C) 2017 by Sysmocom s.f.m.c. GmbH
2 * All Rights Reserved
3 *
4 * Author: Philipp Maier
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * 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
9 * (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
14 * GNU Affero General Public License for more details.
15 *
16 * 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/>.
18 *
19 */
20
21#include <osmocom/core/utils.h>
22#include <osmocom/core/msgb.h>
23#include <osmocom/core/logging.h>
24#include <osmocom/sigtran/sccp_helpers.h>
25#include <osmocom/sccp/sccp_types.h>
26#include <osmocom/gsm/gsm0808.h>
27#include <osmocom/gsm/gsm0808_utils.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020028#include <osmocom/msc/debug.h>
29#include <osmocom/msc/gsm_data.h>
30#include <osmocom/msc/a_iface_bssap.h>
31#include <osmocom/msc/a_iface.h>
32#include <osmocom/msc/osmo_msc.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020033#include <osmocom/core/byteswap.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020034#include <osmocom/msc/a_reset.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020035
36#define IP_V4_ADDR_LEN 4
37
38/*
39 * Helper functions to lookup and allocate subscribers
40 */
41
42/* Allocate a new subscriber connection */
43static struct gsm_subscriber_connection *subscr_conn_allocate_a(const struct a_conn_info *a_conn_info,
44 struct gsm_network *network,
45 uint16_t lac, struct osmo_sccp_user *scu, int conn_id)
46{
47 struct gsm_subscriber_connection *conn;
48
49 LOGP(DMSC, LOGL_NOTICE, "Allocating A-Interface subscriber conn: lac %i, conn_id %i\n", lac, conn_id);
50
51 conn = talloc_zero(network, struct gsm_subscriber_connection);
52 if (!conn)
53 return NULL;
54
55 conn->network = network;
56 conn->via_ran = RAN_GERAN_A;
57 conn->lac = lac;
58
59 conn->a.conn_id = conn_id;
60 conn->a.scu = scu;
61
62 /* Also backup the calling address of the BSC, this allows us to
63 * identify later which BSC is responsible for this subscriber connection */
64 memcpy(&conn->a.bsc_addr, a_conn_info->bsc_addr, sizeof(conn->a.bsc_addr));
65
66 llist_add_tail(&conn->entry, &network->subscr_conns);
67 LOGP(DMSC, LOGL_NOTICE, "A-Interface subscriber connection successfully allocated!\n");
68 return conn;
69}
70
71/* Return an existing A subscriber connection record for the given
72 * connection IDs, or return NULL if not found. */
73static struct gsm_subscriber_connection *subscr_conn_lookup_a(const struct gsm_network *network, int conn_id)
74{
75 struct gsm_subscriber_connection *conn;
76
77 OSMO_ASSERT(network);
78
79 DEBUGP(DMSC, "Looking for A subscriber: conn_id %i\n", conn_id);
80
81 /* FIXME: log_subscribers() is defined in iucs.c as static inline, if
82 * maybe this function should be public to reach it from here? */
83 /* log_subscribers(network); */
84
85 llist_for_each_entry(conn, &network->subscr_conns, entry) {
86 if (conn->via_ran == RAN_GERAN_A && conn->a.conn_id == conn_id) {
87 DEBUGP(DIUCS, "Found A subscriber for conn_id %i\n", conn_id);
88 return conn;
89 }
90 }
91 DEBUGP(DMSC, "No A subscriber found for conn_id %i\n", conn_id);
92 return NULL;
93}
94
95/*
96 * BSSMAP handling for UNITDATA
97 */
98
99/* Endpoint to handle BSSMAP reset */
100static void bssmap_rx_reset(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
101{
102 struct gsm_network *network = a_conn_info->network;
103 struct osmo_ss7_instance *ss7;
104
105 ss7 = osmo_ss7_instance_find(network->a.cs7_instance);
106 OSMO_ASSERT(ss7);
107
108 LOGP(DMSC, LOGL_NOTICE, "Rx RESET from BSC %s, sending RESET ACK\n",
109 osmo_sccp_addr_name(ss7, a_conn_info->bsc_addr));
110 osmo_sccp_tx_unitdata_msg(scu, a_conn_info->msc_addr, a_conn_info->bsc_addr, gsm0808_create_reset_ack());
111
112 /* Make sure all orphand subscriber connections will be cleard */
113 a_clear_all(scu, a_conn_info->bsc_addr);
114
115 msgb_free(msg);
116}
117
118/* Endpoint to handle BSSMAP reset acknowlegement */
119static void bssmap_rx_reset_ack(const struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info,
120 struct msgb *msg)
121{
122
123 struct gsm_network *network = a_conn_info->network;
124 struct osmo_ss7_instance *ss7;
125
126 ss7 = osmo_ss7_instance_find(network->a.cs7_instance);
127 OSMO_ASSERT(ss7);
128
129 if (a_conn_info->reset == NULL) {
130 LOGP(DMSC, LOGL_ERROR, "Received RESET ACK from an unknown BSC %s, ignoring...\n",
131 osmo_sccp_addr_name(ss7, a_conn_info->bsc_addr));
132 goto fail;
133 }
134
135 LOGP(DMSC, LOGL_NOTICE, "Received RESET ACK from BSC %s\n", osmo_sccp_addr_name(ss7, a_conn_info->bsc_addr));
136
137 /* Confirm that we managed to get the reset ack message
138 * towards the connection reset logic */
139 a_reset_ack_confirm(a_conn_info->reset);
140
141fail:
142 msgb_free(msg);
143}
144
145/* Handle UNITDATA BSSMAP messages */
146static void bssmap_rcvmsg_udt(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
147{
148 /* Note: When in the MSC role, RESET ACK is the only valid message that
149 * can be received via UNITDATA */
150
151 if (msgb_l3len(msg) < 1) {
152 LOGP(DMSC, LOGL_NOTICE, "Error: No data received -- discarding message!\n");
153 return;
154 }
155
156 LOGP(DMSC, LOGL_NOTICE, "Rx BSC UDT BSSMAP %s\n", gsm0808_bssmap_name(msg->l3h[0]));
157
158 switch (msg->l3h[0]) {
159 case BSS_MAP_MSG_RESET:
160 bssmap_rx_reset(scu, a_conn_info, msg);
161 break;
162 case BSS_MAP_MSG_RESET_ACKNOWLEDGE:
163 bssmap_rx_reset_ack(scu, a_conn_info, msg);
164 break;
165 default:
166 LOGP(DMSC, LOGL_NOTICE, "Unimplemented message format: %s -- message discarded!\n",
167 gsm0808_bssmap_name(msg->l3h[0]));
168 msgb_free(msg);
169 }
170}
171
172/* Receive incoming connection less data messages via sccp */
173void sccp_rx_udt(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
174{
175 /* Note: The only valid message type that can be received
176 * via UNITDATA are BSS Management messages */
177 struct bssmap_header *bs;
178
179 OSMO_ASSERT(scu);
180 OSMO_ASSERT(a_conn_info);
181 OSMO_ASSERT(msg);
182
183 LOGP(DMSC, LOGL_NOTICE, "Rx BSC UDT: %s\n", osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
184
185 if (msgb_l2len(msg) < sizeof(*bs)) {
186 LOGP(DMSC, LOGL_ERROR, "Error: Header is too short -- discarding message!\n");
187 msgb_free(msg);
188 return;
189 }
190
191 bs = (struct bssmap_header *)msgb_l2(msg);
192 if (bs->length < msgb_l2len(msg) - sizeof(*bs)) {
193 LOGP(DMSC, LOGL_ERROR, "Error: Message is too short -- discarding message!\n");
194 msgb_free(msg);
195 return;
196 }
197
198 switch (bs->type) {
199 case BSSAP_MSG_BSS_MANAGEMENT:
200 msg->l3h = &msg->l2h[sizeof(struct bssmap_header)];
201 bssmap_rcvmsg_udt(scu, a_conn_info, msg);
202 break;
203 default:
204 LOGP(DMSC, LOGL_ERROR,
205 "Error: Unimplemented message type: %s -- message discarded!\n", gsm0808_bssmap_name(bs->type));
206 msgb_free(msg);
207 }
208}
209
210/*
211 * BSSMAP handling for connection oriented data
212 */
213
214/* Endpoint to handle BSSMAP clear request */
215static int bssmap_rx_clear_rqst(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
216{
217 struct gsm_network *network = a_conn_info->network;
218 struct tlv_parsed tp;
219 int rc;
220 struct msgb *msg_resp;
221 uint8_t cause;
222 struct gsm_subscriber_connection *conn;
223
224 LOGP(DMSC, LOGL_NOTICE, "BSC requested to clear connection (conn_id=%i)\n", a_conn_info->conn_id);
225
226 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
227 if (!TLVP_PRESENT(&tp, GSM0808_IE_CAUSE)) {
228 LOGP(DMSC, LOGL_ERROR, "Cause code is missing -- discarding message!\n");
229 goto fail;
230 }
231 cause = TLVP_VAL(&tp, GSM0808_IE_CAUSE)[0];
232
233 /* Respond with clear command */
234 msg_resp = gsm0808_create_clear_command(GSM0808_CAUSE_CALL_CONTROL);
235 rc = osmo_sccp_tx_data_msg(scu, a_conn_info->conn_id, msg_resp);
236
237 /* If possible, inform the MSC about the clear request */
238 conn = subscr_conn_lookup_a(network, a_conn_info->conn_id);
239 if (!conn)
240 goto fail;
241 msc_clear_request(conn, cause);
242
243 msgb_free(msg);
244 return rc;
245
246fail:
247 msgb_free(msg);
248 return -EINVAL;
249}
250
251/* Endpoint to handle BSSMAP clear complete */
252static int bssmap_rx_clear_complete(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
253{
254 int rc;
255
256 LOGP(DMSC, LOGL_NOTICE, "Releasing connection (conn_id=%i)\n", a_conn_info->conn_id);
257 rc = osmo_sccp_tx_disconn(scu, a_conn_info->conn_id,
258 a_conn_info->msc_addr, SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
259
260 /* Remove the record from the list with active connections. */
261 a_delete_bsc_con(a_conn_info->conn_id);
262
263 msgb_free(msg);
264 return rc;
265}
266
267/* Endpoint to handle layer 3 complete messages */
268static int bssmap_rx_l3_compl(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
269{
270 struct tlv_parsed tp;
271 struct {
272 uint8_t ident;
273 struct gsm48_loc_area_id lai;
274 uint16_t ci;
275 } __attribute__ ((packed)) lai_ci;
276 uint16_t mcc;
277 uint16_t mnc;
278 uint16_t lac;
279 uint8_t data_length;
280 const uint8_t *data;
281 int rc;
282
283 struct gsm_network *network = a_conn_info->network;
284 struct gsm_subscriber_connection *conn;
285
286 LOGP(DMSC, LOGL_NOTICE, "BSC has completed layer 3 connection (conn_id=%i)\n", a_conn_info->conn_id);
287
288 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
289 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER)) {
290 LOGP(DMSC, LOGL_ERROR, "Mandatory CELL IDENTIFIER not present -- discarding message!\n");
291 goto fail;
292 }
293 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
294 LOGP(DMSC, LOGL_ERROR, "Mandatory LAYER 3 INFORMATION not present -- discarding message!\n");
295 goto fail;
296 }
297
298 /* Parse Cell ID element */
299 /* FIXME: Encapsulate this in a parser/generator function inside
300 * libosmocore, add support for all specified cell identification
301 * discriminators (see 3GPP ts 3.2.2.17 Cell Identifier) */
302 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER);
303 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER);
304 if (sizeof(lai_ci) != data_length) {
305 LOGP(DMSC, LOGL_ERROR,
306 "Unable to parse element CELL IDENTIFIER (wrong field length) -- discarding message!\n");
307 goto fail;
308 }
309 memcpy(&lai_ci, data, sizeof(lai_ci));
310 if (lai_ci.ident != CELL_IDENT_WHOLE_GLOBAL) {
311 LOGP(DMSC, LOGL_ERROR,
312 "Unable to parse element CELL IDENTIFIER (wrong cell identification discriminator) -- discarding message!\n");
313 goto fail;
314 }
315 if (gsm48_decode_lai(&lai_ci.lai, &mcc, &mnc, &lac) != 0) {
316 LOGP(DMSC, LOGL_ERROR,
317 "Unable to parse element CELL IDENTIFIER (lai decoding failed) -- discarding message!\n");
318 goto fail;
319 }
320
321 /* Parse Layer 3 Information element */
322 /* FIXME: This is probably to hackish, compiler also complains "assignment discards ‘const’ qualifier..." */
323 msg->l3h = TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
324 msg->tail = msg->l3h + TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
325
326 /* Create new subscriber context */
327 conn = subscr_conn_allocate_a(a_conn_info, network, lac, scu, a_conn_info->conn_id);
328
329 /* Handover location update to the MSC code */
330 /* msc_compl_l3() takes ownership of dtap_msg
331 * message buffer */
332 rc = msc_compl_l3(conn, msg, 0);
333 if (rc == MSC_CONN_ACCEPT) {
334 LOGP(DMSC, LOGL_NOTICE, "User has been accepted by MSC.\n");
335 return 0;
336 } else if (rc == MSC_CONN_REJECT)
337 LOGP(DMSC, LOGL_NOTICE, "User has been rejected by MSC.\n");
338 else
339 LOGP(DMSC, LOGL_NOTICE, "User has been rejected by MSC (unknown error)\n");
340
341 return -EINVAL;
342
343fail:
344 msgb_free(msg);
345 return -EINVAL;
346}
347
348/* Endpoint to handle BSSMAP classmark update */
349static int bssmap_rx_classmark_upd(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
350{
351 struct gsm_network *network = a_conn_info->network;
352 struct gsm_subscriber_connection *conn;
353 struct tlv_parsed tp;
354 const uint8_t *cm2 = NULL;
355 const uint8_t *cm3 = NULL;
356 uint8_t cm2_len = 0;
357 uint8_t cm3_len = 0;
358
359 conn = subscr_conn_lookup_a(network, a_conn_info->conn_id);
360 if (!conn)
361 goto fail;
362
363 LOGP(DMSC, LOGL_NOTICE, "BSC sends clasmark update (conn_id=%i)\n", conn->a.conn_id);
364
365 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
366 if (!TLVP_PRESENT(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T2)) {
367 LOGP(DMSC, LOGL_ERROR, "Mandatory Classmark Information Type 2 not present -- discarding message!\n");
368 goto fail;
369 }
370
371 cm2 = TLVP_VAL(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T2);
372 cm2_len = TLVP_LEN(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T2);
373
374 if (TLVP_PRESENT(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T3)) {
375 cm3 = TLVP_VAL(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T3);
376 cm3_len = TLVP_LEN(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T3);
377 }
378
379 /* Inform MSC about the classmark change */
380 msc_classmark_chg(conn, cm2, cm2_len, cm3, cm3_len);
381
382 msgb_free(msg);
383 return 0;
384
385fail:
386 msgb_free(msg);
387 return -EINVAL;
388}
389
390/* Endpoint to handle BSSMAP cipher mode complete */
391static int bssmap_rx_ciph_compl(const struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info,
392 struct msgb *msg)
393{
394 /* FIXME: The field GSM0808_IE_LAYER_3_MESSAGE_CONTENTS is optional by
395 * means of the specification. So there can be messages without L3 info.
396 * In this case, the code will crash becrause msc_cipher_mode_compl()
397 * is not able to deal with msg = NULL and apperently
398 * msc_cipher_mode_compl() was never meant to be used without L3 data.
399 * This needs to be discussed further! */
400
401 struct gsm_network *network = a_conn_info->network;
402 struct gsm_subscriber_connection *conn;
403 struct tlv_parsed tp;
404 uint8_t alg_id = 1;
405
406 conn = subscr_conn_lookup_a(network, a_conn_info->conn_id);
407 if (!conn)
408 goto fail;
409
410 LOGP(DMSC, LOGL_NOTICE, "BSC sends cipher mode complete (conn_id=%i)\n", conn->a.conn_id);
411
412 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
413
414 if (TLVP_PRESENT(&tp, GSM0808_IE_CHOSEN_ENCR_ALG)) {
415 alg_id = TLVP_VAL(&tp, GSM0808_IE_CHOSEN_ENCR_ALG)[0] - 1;
416 }
417
418 if (TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_MESSAGE_CONTENTS)) {
419 msg->l3h = TLVP_VAL(&tp, GSM0808_IE_LAYER_3_MESSAGE_CONTENTS);
420 msg->tail = msg->l3h + TLVP_LEN(&tp, GSM0808_IE_LAYER_3_MESSAGE_CONTENTS);
421 } else {
422 msgb_free(msg);
423 msg = NULL;
424 }
425
426 /* Hand over cipher mode complete message to the MSC,
427 * msc_cipher_mode_compl() takes ownership for msg */
428 msc_cipher_mode_compl(conn, msg, alg_id);
429
430 return 0;
431fail:
432 msgb_free(msg);
433 return -EINVAL;
434}
435
436/* Endpoint to handle BSSMAP cipher mode reject */
437static int bssmap_rx_ciph_rej(const struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
438{
439 struct gsm_network *network = a_conn_info->network;
440 struct gsm_subscriber_connection *conn;
441 struct tlv_parsed tp;
442 uint8_t cause;
443
444 conn = subscr_conn_lookup_a(network, a_conn_info->conn_id);
445 if (!conn)
446 goto fail;
447
448 LOGP(DMSC, LOGL_NOTICE, "BSC sends cipher mode reject (conn_id=%i)\n", conn->a.conn_id);
449
450 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
451 if (!TLVP_PRESENT(&tp, BSS_MAP_MSG_CIPHER_MODE_REJECT)) {
452 LOGP(DMSC, LOGL_ERROR, "Cause code is missing -- discarding message!\n");
453 goto fail;
454 }
455
456 cause = TLVP_VAL(&tp, BSS_MAP_MSG_CIPHER_MODE_REJECT)[0];
457 LOGP(DMSC, LOGL_NOTICE, "Cipher mode rejection cause: %i\n", cause);
458
459 /* FIXME: Can we do something meaningful here? e.g. report to the
460 * msc code somehow that the cipher mode command has failed. */
461
462 msgb_free(msg);
463 return 0;
464fail:
465 msgb_free(msg);
466 return -EINVAL;
467}
468
469/* Endpoint to handle BSSMAP assignment failure */
470static int bssmap_rx_ass_fail(const struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
471{
472 struct gsm_network *network = a_conn_info->network;
473 struct gsm_subscriber_connection *conn;
474 struct tlv_parsed tp;
475 uint8_t cause;
476 uint8_t *rr_cause_ptr = NULL;
477 uint8_t rr_cause;
478
479 conn = subscr_conn_lookup_a(network, a_conn_info->conn_id);
480 if (!conn)
481 goto fail;
482
483 LOGP(DMSC, LOGL_NOTICE, "BSC sends assignment failure message (conn_id=%i)\n", conn->a.conn_id);
484
485 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
486 if (!TLVP_PRESENT(&tp, GSM0808_IE_CAUSE)) {
487 LOGP(DMSC, LOGL_ERROR, "Cause code is missing -- discarding message!\n");
488 goto fail;
489 }
490 cause = TLVP_VAL(&tp, GSM0808_IE_CAUSE)[0];
491
492 if (TLVP_PRESENT(&tp, GSM0808_IE_RR_CAUSE)) {
493 rr_cause = TLVP_VAL(&tp, GSM0808_IE_RR_CAUSE)[0];
494 rr_cause_ptr = &rr_cause;
495 }
496
497 /* FIXME: In AoIP, the Assignment failure will carry also an optional
498 * Codec List (BSS Supported) element. It has to be discussed if we
499 * can ignore this element. If not, The msc_assign_fail() function
500 * call has to change. However msc_assign_fail() does nothing in the
501 * end. So probably we can just leave it as it is. Even for AoIP */
502
503 /* Inform the MSC about the assignment failure event */
504 msc_assign_fail(conn, cause, rr_cause_ptr);
505
506 msgb_free(msg);
507 return 0;
508fail:
509 msgb_free(msg);
510 return -EINVAL;
511}
512
513/* Endpoint to handle sapi "n" reject */
514static int bssmap_rx_sapi_n_rej(const struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info,
515 struct msgb *msg)
516{
517 struct gsm_network *network = a_conn_info->network;
518 struct gsm_subscriber_connection *conn;
519 struct tlv_parsed tp;
520 uint8_t dlci;
521
522 conn = subscr_conn_lookup_a(network, a_conn_info->conn_id);
523 if (!conn)
524 goto fail;
525
526 LOGP(DMSC, LOGL_NOTICE, "BSC sends sapi \"n\" reject message (conn_id=%i)\n", conn->a.conn_id);
527
528 /* Note: The MSC code seems not to care about the cause code, but by
529 * the specification it is mandatory, so we check its presence. See
530 * also 3GPP TS 48.008 3.2.1.34 SAPI "n" REJECT */
531 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
532 if (!TLVP_PRESENT(&tp, GSM0808_IE_CAUSE)) {
533 LOGP(DMSC, LOGL_ERROR, "Cause code is missing -- discarding message!\n");
534 goto fail;
535 }
536
537 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
538 if (!TLVP_PRESENT(&tp, GSM0808_IE_DLCI)) {
539 LOGP(DMSC, LOGL_ERROR, "DLCI is missing -- discarding message!\n");
540 goto fail;
541 }
542 dlci = TLVP_VAL(&tp, GSM0808_IE_DLCI)[0];
543
544 /* Inform the MSC about the sapi "n" reject event */
545 msc_sapi_n_reject(conn, dlci);
546
547 msgb_free(msg);
548 return 0;
549fail:
550 msgb_free(msg);
551 return -EINVAL;
552}
553
554/* Endpoint to handle assignment complete */
555static int bssmap_rx_ass_compl(const struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info,
556 struct msgb *msg)
557{
558 struct gsm_network *network = a_conn_info->network;
559 struct gsm_subscriber_connection *conn;
560 struct mgcpgw_client *mgcp;
561 struct tlv_parsed tp;
562 struct sockaddr_storage rtp_addr;
563 struct sockaddr_in *rtp_addr_in;
564 int rc;
565
566 conn = subscr_conn_lookup_a(network, a_conn_info->conn_id);
567 if (!conn)
568 goto fail;
569
570 mgcp = conn->network->mgcpgw.client;
571 OSMO_ASSERT(mgcp);
572
573 LOGP(DMSC, LOGL_NOTICE, "BSC sends assignment complete message (conn_id=%i)\n", conn->a.conn_id);
574
575 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
576
577 if (!TLVP_PRESENT(&tp, GSM0808_IE_AOIP_TRASP_ADDR)) {
578 LOGP(DMSC, LOGL_ERROR, "AoIP transport identifier missing -- discarding message!\n");
579 goto fail;
580 }
581
582 /* Decode AoIP transport address element */
583 rc = gsm0808_dec_aoip_trasp_addr(&rtp_addr, TLVP_VAL(&tp, GSM0808_IE_AOIP_TRASP_ADDR),
584 TLVP_LEN(&tp, GSM0808_IE_AOIP_TRASP_ADDR));
585 if (rc < 0) {
586 LOGP(DMSC, LOGL_ERROR, "Unable to decode aoip transport address.\n");
587 goto fail;
588 }
589
590 /* use address / port supplied with the AoIP
591 * transport address element */
592 if (rtp_addr.ss_family == AF_INET) {
593 rtp_addr_in = (struct sockaddr_in *)&rtp_addr;
594 conn->rtp.port_subscr = osmo_ntohs(rtp_addr_in->sin_port);
595 /* FIXME: We also get the IP-Address of the remote (e.g. BTS)
596 * end with the response. Currently we just ignore that address.
597 * Instead we expect that our local MGCP gateway and the code
598 * controlling it, magically knows the IP of the remote end. */
599 } else {
600 LOGP(DMSC, LOGL_ERROR, "Unsopported addressing scheme. (supports only IPV4)\n");
601 goto fail;
602 }
603
604 /* FIXME: Seems to be related to authentication or,
605 encryption. Is this really in the right place? */
606 msc_rx_sec_mode_compl(conn);
607
608 msgb_free(msg);
609 return 0;
610fail:
611 msgb_free(msg);
612 return -EINVAL;
613}
614
615/* Handle incoming connection oriented BSSMAP messages */
616static int rx_bssmap(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
617{
618 if (msgb_l3len(msg) < 1) {
619 LOGP(DMSC, LOGL_NOTICE, "Error: No data received -- discarding message!\n");
620 msgb_free(msg);
621 return -1;
622 }
623
624 LOGP(DMSC, LOGL_NOTICE, "Rx MSC DT1 BSSMAP %s\n", gsm0808_bssmap_name(msg->l3h[0]));
625
626 switch (msg->l3h[0]) {
627 case BSS_MAP_MSG_CLEAR_RQST:
628 return bssmap_rx_clear_rqst(scu, a_conn_info, msg);
629 break;
630 case BSS_MAP_MSG_CLEAR_COMPLETE:
631 return bssmap_rx_clear_complete(scu, a_conn_info, msg);
632 break;
633 case BSS_MAP_MSG_COMPLETE_LAYER_3:
634 return bssmap_rx_l3_compl(scu, a_conn_info, msg);
635 break;
636 case BSS_MAP_MSG_CLASSMARK_UPDATE:
637 return bssmap_rx_classmark_upd(scu, a_conn_info, msg);
638 break;
639 case BSS_MAP_MSG_CIPHER_MODE_COMPLETE:
640 return bssmap_rx_ciph_compl(scu, a_conn_info, msg);
641 break;
642 case BSS_MAP_MSG_CIPHER_MODE_REJECT:
643 return bssmap_rx_ciph_rej(scu, a_conn_info, msg);
644 break;
645 case BSS_MAP_MSG_ASSIGMENT_FAILURE:
646 return bssmap_rx_ass_fail(scu, a_conn_info, msg);
647 break;
648 case BSS_MAP_MSG_SAPI_N_REJECT:
649 return bssmap_rx_sapi_n_rej(scu, a_conn_info, msg);
650 break;
651 case BSS_MAP_MSG_ASSIGMENT_COMPLETE:
652 return bssmap_rx_ass_compl(scu, a_conn_info, msg);
653 break;
654 default:
655 LOGP(DMSC, LOGL_ERROR, "Unimplemented msg type: %s\n", gsm0808_bssmap_name(msg->l3h[0]));
656 msgb_free(msg);
657 return -EINVAL;
658 }
659
660 return -EINVAL;
661}
662
663/* Endpoint to handle regular BSSAP DTAP messages */
664static int rx_dtap(const struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
665{
666 struct gsm_network *network = a_conn_info->network;
667 struct gsm_subscriber_connection *conn;
668
669 conn = subscr_conn_lookup_a(network, a_conn_info->conn_id);
670 if (!conn) {
671 msgb_free(msg);
672 return -EINVAL;
673 }
674
675 LOGP(DMSC, LOGL_NOTICE, "BSC sends layer 3 dtap (conn_id=%i)\n", conn->a.conn_id);
676
677 /* msc_dtap expects the dtap payload in l3h */
678 msg->l3h = msg->l2h + 3;
679
680 /* Forward dtap payload into the msc,
681 * msc_dtap() takes ownership for msg */
682 msc_dtap(conn, conn->a.conn_id, msg);
683
684 return 0;
685}
686
687/* Handle incoming connection oriented messages */
688int sccp_rx_dt(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
689{
690 OSMO_ASSERT(scu);
691 OSMO_ASSERT(a_conn_info);
692 OSMO_ASSERT(msg);
693
694 LOGP(DMSC, LOGL_NOTICE, "Rx BSC DT: %s\n", osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
695
696 if (msgb_l2len(msg) < sizeof(struct bssmap_header)) {
697 LOGP(DMSC, LOGL_NOTICE, "The header is too short -- discarding message!\n");
698 msgb_free(msg);
699 }
700
701 switch (msg->l2h[0]) {
702 case BSSAP_MSG_BSS_MANAGEMENT:
703 msg->l3h = &msg->l2h[sizeof(struct bssmap_header)];
704 return rx_bssmap(scu, a_conn_info, msg);
705 break;
706 case BSSAP_MSG_DTAP:
707 return rx_dtap(scu, a_conn_info, msg);
708 break;
709 default:
710 LOGP(DMSC, LOGL_ERROR, "Unimplemented BSSAP msg type: %s\n", gsm0808_bssap_name(msg->l2h[0]));
711 msgb_free(msg);
712 return -EINVAL;
713 }
714
715 return -EINVAL;
716}