blob: ea1b926bd5d3a123bce53e9aad6c49882fdb9db4 [file] [log] [blame]
Philipp Maierfbf66102017-04-09 12:32:51 +02001/* (C) 2017 by Sysmocom s.f.m.c. GmbH
Harald Weltec9e78592018-02-09 01:42:50 +01002 * (C) 2018 by Harald Welte <laforge@gnumonks.org>
Philipp Maierfbf66102017-04-09 12:32:51 +02003 * All Rights Reserved
4 *
5 * Author: Philipp Maier
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 <osmocom/core/utils.h>
23#include <osmocom/core/msgb.h>
24#include <osmocom/core/logging.h>
25#include <osmocom/sigtran/sccp_helpers.h>
26#include <osmocom/sccp/sccp_types.h>
27#include <osmocom/gsm/gsm0808.h>
Max43b01b02017-09-15 11:22:30 +020028#include <osmocom/gsm/gsm48.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020029#include <osmocom/gsm/gsm0808_utils.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020030#include <osmocom/msc/debug.h>
31#include <osmocom/msc/gsm_data.h>
32#include <osmocom/msc/a_iface_bssap.h>
33#include <osmocom/msc/a_iface.h>
34#include <osmocom/msc/osmo_msc.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020035#include <osmocom/core/byteswap.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020036#include <osmocom/msc/a_reset.h>
Max43b01b02017-09-15 11:22:30 +020037#include <osmocom/msc/transaction.h>
Philipp Maier621ba032017-11-07 17:19:25 +010038#include <osmocom/msc/msc_mgcp.h>
Max43b01b02017-09-15 11:22:30 +020039
40#include <errno.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020041
42#define IP_V4_ADDR_LEN 4
43
44/*
45 * Helper functions to lookup and allocate subscribers
46 */
47
48/* Allocate a new subscriber connection */
49static struct gsm_subscriber_connection *subscr_conn_allocate_a(const struct a_conn_info *a_conn_info,
50 struct gsm_network *network,
51 uint16_t lac, struct osmo_sccp_user *scu, int conn_id)
52{
53 struct gsm_subscriber_connection *conn;
54
Harald Weltef0dc1be2018-02-09 00:37:56 +010055 LOGP(DMSC, LOGL_DEBUG, "Allocating A-Interface subscriber conn: lac %i, conn_id %i\n", lac, conn_id);
Philipp Maierfbf66102017-04-09 12:32:51 +020056
57 conn = talloc_zero(network, struct gsm_subscriber_connection);
58 if (!conn)
59 return NULL;
60
61 conn->network = network;
62 conn->via_ran = RAN_GERAN_A;
63 conn->lac = lac;
64
65 conn->a.conn_id = conn_id;
66 conn->a.scu = scu;
67
68 /* Also backup the calling address of the BSC, this allows us to
69 * identify later which BSC is responsible for this subscriber connection */
Harald Welte54a10ef2018-02-09 00:09:16 +010070 memcpy(&conn->a.bsc_addr, &a_conn_info->bsc->bsc_addr, sizeof(conn->a.bsc_addr));
Philipp Maierfbf66102017-04-09 12:32:51 +020071
72 llist_add_tail(&conn->entry, &network->subscr_conns);
Harald Welte6de46592018-02-09 00:53:17 +010073 LOGPCONN(conn, LOGL_DEBUG, "A-Interface subscriber connection successfully allocated!\n");
Philipp Maierfbf66102017-04-09 12:32:51 +020074 return conn;
75}
76
77/* Return an existing A subscriber connection record for the given
78 * connection IDs, or return NULL if not found. */
79static struct gsm_subscriber_connection *subscr_conn_lookup_a(const struct gsm_network *network, int conn_id)
80{
81 struct gsm_subscriber_connection *conn;
82
83 OSMO_ASSERT(network);
84
85 DEBUGP(DMSC, "Looking for A subscriber: conn_id %i\n", conn_id);
86
87 /* FIXME: log_subscribers() is defined in iucs.c as static inline, if
88 * maybe this function should be public to reach it from here? */
89 /* log_subscribers(network); */
90
91 llist_for_each_entry(conn, &network->subscr_conns, entry) {
92 if (conn->via_ran == RAN_GERAN_A && conn->a.conn_id == conn_id) {
Harald Welte6de46592018-02-09 00:53:17 +010093 LOGPCONN(conn, LOGL_DEBUG, "Found A subscriber for conn_id %i\n", conn_id);
Philipp Maierfbf66102017-04-09 12:32:51 +020094 return conn;
95 }
96 }
97 DEBUGP(DMSC, "No A subscriber found for conn_id %i\n", conn_id);
98 return NULL;
99}
100
101/*
102 * BSSMAP handling for UNITDATA
103 */
104
105/* Endpoint to handle BSSMAP reset */
106static void bssmap_rx_reset(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
107{
108 struct gsm_network *network = a_conn_info->network;
109 struct osmo_ss7_instance *ss7;
110
111 ss7 = osmo_ss7_instance_find(network->a.cs7_instance);
112 OSMO_ASSERT(ss7);
113
Harald Welte1f477442018-02-09 01:49:01 +0100114 LOGP(DBSSAP, LOGL_NOTICE, "Rx BSSMAP RESET from BSC %s, sending RESET ACK\n",
Harald Welte54a10ef2018-02-09 00:09:16 +0100115 osmo_sccp_addr_name(ss7, &a_conn_info->bsc->bsc_addr));
116 osmo_sccp_tx_unitdata_msg(scu, &a_conn_info->bsc->msc_addr, &a_conn_info->bsc->bsc_addr,
117 gsm0808_create_reset_ack());
Philipp Maierfbf66102017-04-09 12:32:51 +0200118
119 /* Make sure all orphand subscriber connections will be cleard */
Harald Welte54a10ef2018-02-09 00:09:16 +0100120 a_clear_all(scu, &a_conn_info->bsc->bsc_addr);
121
122 if (!a_conn_info->bsc->reset)
123 a_start_reset(a_conn_info->bsc, true);
Philipp Maierfbf66102017-04-09 12:32:51 +0200124}
125
126/* Endpoint to handle BSSMAP reset acknowlegement */
127static void bssmap_rx_reset_ack(const struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info,
128 struct msgb *msg)
129{
130
131 struct gsm_network *network = a_conn_info->network;
132 struct osmo_ss7_instance *ss7;
133
134 ss7 = osmo_ss7_instance_find(network->a.cs7_instance);
135 OSMO_ASSERT(ss7);
136
Harald Welte54a10ef2018-02-09 00:09:16 +0100137 if (a_conn_info->bsc->reset == NULL) {
Harald Welte1f477442018-02-09 01:49:01 +0100138 LOGP(DBSSAP, LOGL_ERROR, "Received RESET ACK from an unknown BSC %s, ignoring...\n",
Harald Welte54a10ef2018-02-09 00:09:16 +0100139 osmo_sccp_addr_name(ss7, &a_conn_info->bsc->bsc_addr));
Harald Weltea172e9e2018-02-09 21:33:24 +0100140 return;
Philipp Maierfbf66102017-04-09 12:32:51 +0200141 }
142
Harald Welte1f477442018-02-09 01:49:01 +0100143 LOGP(DBSSAP, LOGL_NOTICE, "Received RESET ACK from BSC %s\n",
Harald Welte54a10ef2018-02-09 00:09:16 +0100144 osmo_sccp_addr_name(ss7, &a_conn_info->bsc->bsc_addr));
Philipp Maierfbf66102017-04-09 12:32:51 +0200145
146 /* Confirm that we managed to get the reset ack message
147 * towards the connection reset logic */
Harald Welte54a10ef2018-02-09 00:09:16 +0100148 a_reset_ack_confirm(a_conn_info->bsc->reset);
Philipp Maierfbf66102017-04-09 12:32:51 +0200149}
150
151/* Handle UNITDATA BSSMAP messages */
152static void bssmap_rcvmsg_udt(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
153{
154 /* Note: When in the MSC role, RESET ACK is the only valid message that
155 * can be received via UNITDATA */
156
157 if (msgb_l3len(msg) < 1) {
Harald Welte1f477442018-02-09 01:49:01 +0100158 LOGP(DBSSAP, LOGL_NOTICE, "Error: No data received -- discarding message!\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200159 return;
160 }
161
Harald Welte1f477442018-02-09 01:49:01 +0100162 LOGP(DBSSAP, LOGL_DEBUG, "Rx BSSMAP UDT %s\n", gsm0808_bssmap_name(msg->l3h[0]));
Philipp Maierfbf66102017-04-09 12:32:51 +0200163
164 switch (msg->l3h[0]) {
165 case BSS_MAP_MSG_RESET:
166 bssmap_rx_reset(scu, a_conn_info, msg);
167 break;
168 case BSS_MAP_MSG_RESET_ACKNOWLEDGE:
169 bssmap_rx_reset_ack(scu, a_conn_info, msg);
170 break;
171 default:
Harald Welte1f477442018-02-09 01:49:01 +0100172 LOGP(DBSSAP, LOGL_NOTICE, "Unimplemented message format: %s -- message discarded!\n",
Philipp Maierfbf66102017-04-09 12:32:51 +0200173 gsm0808_bssmap_name(msg->l3h[0]));
Philipp Maierfbf66102017-04-09 12:32:51 +0200174 }
175}
176
177/* Receive incoming connection less data messages via sccp */
Neels Hofmeyrc1d69252017-12-18 04:06:04 +0100178void a_sccp_rx_udt(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
Philipp Maierfbf66102017-04-09 12:32:51 +0200179{
180 /* Note: The only valid message type that can be received
181 * via UNITDATA are BSS Management messages */
182 struct bssmap_header *bs;
183
184 OSMO_ASSERT(scu);
185 OSMO_ASSERT(a_conn_info);
186 OSMO_ASSERT(msg);
187
Harald Welte1f477442018-02-09 01:49:01 +0100188 LOGP(DBSSAP, LOGL_DEBUG, "Rx BSSMAP UDT: %s\n", msgb_hexdump_l2(msg));
Philipp Maierfbf66102017-04-09 12:32:51 +0200189
190 if (msgb_l2len(msg) < sizeof(*bs)) {
Harald Welte1f477442018-02-09 01:49:01 +0100191 LOGP(DBSSAP, LOGL_ERROR, "Error: Header is too short -- discarding message!\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200192 return;
193 }
194
195 bs = (struct bssmap_header *)msgb_l2(msg);
196 if (bs->length < msgb_l2len(msg) - sizeof(*bs)) {
Harald Welte1f477442018-02-09 01:49:01 +0100197 LOGP(DBSSAP, LOGL_ERROR, "Error: Message is too short -- discarding message!\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200198 return;
199 }
200
201 switch (bs->type) {
202 case BSSAP_MSG_BSS_MANAGEMENT:
203 msg->l3h = &msg->l2h[sizeof(struct bssmap_header)];
204 bssmap_rcvmsg_udt(scu, a_conn_info, msg);
205 break;
206 default:
Harald Welte1f477442018-02-09 01:49:01 +0100207 LOGP(DBSSAP, LOGL_ERROR,
Philipp Maierfbf66102017-04-09 12:32:51 +0200208 "Error: Unimplemented message type: %s -- message discarded!\n", gsm0808_bssmap_name(bs->type));
Philipp Maierfbf66102017-04-09 12:32:51 +0200209 }
210}
211
212/*
213 * BSSMAP handling for connection oriented data
214 */
215
216/* Endpoint to handle BSSMAP clear request */
Harald Weltec27ef652018-02-09 01:23:25 +0100217static int bssmap_rx_clear_rqst(struct gsm_subscriber_connection *conn, struct msgb *msg)
Philipp Maierfbf66102017-04-09 12:32:51 +0200218{
Philipp Maierfbf66102017-04-09 12:32:51 +0200219 struct tlv_parsed tp;
220 int rc;
221 struct msgb *msg_resp;
222 uint8_t cause;
Philipp Maierfbf66102017-04-09 12:32:51 +0200223
Harald Welte35284462018-02-09 01:31:29 +0100224 LOGPCONN(conn, LOGL_INFO, "Rx BSSMAP CLEAR REQUEST\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200225
226 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
227 if (!TLVP_PRESENT(&tp, GSM0808_IE_CAUSE)) {
Harald Welte1f477442018-02-09 01:49:01 +0100228 LOGP(DBSSAP, LOGL_ERROR, "Cause code is missing -- discarding message!\n");
Harald Weltea172e9e2018-02-09 21:33:24 +0100229 return -EINVAL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200230 }
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);
Harald Weltec27ef652018-02-09 01:23:25 +0100235 rc = osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg_resp);
Philipp Maierfbf66102017-04-09 12:32:51 +0200236
Philipp Maierfbf66102017-04-09 12:32:51 +0200237 msc_clear_request(conn, cause);
238
Philipp Maierfbf66102017-04-09 12:32:51 +0200239 return rc;
Philipp Maierfbf66102017-04-09 12:32:51 +0200240}
241
242/* Endpoint to handle BSSMAP clear complete */
Harald Weltec27ef652018-02-09 01:23:25 +0100243static int bssmap_rx_clear_complete(struct osmo_sccp_user *scu,
244 const struct a_conn_info *a_conn_info, struct msgb *msg)
Philipp Maierfbf66102017-04-09 12:32:51 +0200245{
246 int rc;
247
Pau Espin Pedrol9f055f52018-02-14 14:11:58 +0100248 LOGP(DMSC, LOGL_INFO, "Rx BSSMAP CLEAR COMPLETE, releasing SCCP connection\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200249 rc = osmo_sccp_tx_disconn(scu, a_conn_info->conn_id,
Harald Welte54a10ef2018-02-09 00:09:16 +0100250 NULL, SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
Philipp Maierfbf66102017-04-09 12:32:51 +0200251
252 /* Remove the record from the list with active connections. */
253 a_delete_bsc_con(a_conn_info->conn_id);
254
Philipp Maierfbf66102017-04-09 12:32:51 +0200255 return rc;
256}
257
258/* Endpoint to handle layer 3 complete messages */
259static int bssmap_rx_l3_compl(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
260{
261 struct tlv_parsed tp;
262 struct {
263 uint8_t ident;
264 struct gsm48_loc_area_id lai;
265 uint16_t ci;
266 } __attribute__ ((packed)) lai_ci;
267 uint16_t mcc;
268 uint16_t mnc;
269 uint16_t lac;
270 uint8_t data_length;
271 const uint8_t *data;
272 int rc;
273
274 struct gsm_network *network = a_conn_info->network;
275 struct gsm_subscriber_connection *conn;
276
Harald Welte1f477442018-02-09 01:49:01 +0100277 LOGP(DBSSAP, LOGL_INFO, "Rx BSSMAP COMPLETE L3 INFO (conn_id=%i)\n", a_conn_info->conn_id);
Philipp Maierfbf66102017-04-09 12:32:51 +0200278
279 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
280 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER)) {
Harald Welte1f477442018-02-09 01:49:01 +0100281 LOGP(DBSSAP, LOGL_ERROR, "Mandatory CELL IDENTIFIER not present -- discarding message!\n");
Harald Weltea172e9e2018-02-09 21:33:24 +0100282 return -EINVAL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200283 }
284 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
Harald Welte1f477442018-02-09 01:49:01 +0100285 LOGP(DBSSAP, LOGL_ERROR, "Mandatory LAYER 3 INFORMATION not present -- discarding message!\n");
Harald Weltea172e9e2018-02-09 21:33:24 +0100286 return -EINVAL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200287 }
288
289 /* Parse Cell ID element */
290 /* FIXME: Encapsulate this in a parser/generator function inside
291 * libosmocore, add support for all specified cell identification
292 * discriminators (see 3GPP ts 3.2.2.17 Cell Identifier) */
293 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER);
294 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER);
295 if (sizeof(lai_ci) != data_length) {
Harald Welte1f477442018-02-09 01:49:01 +0100296 LOGP(DBSSAP, LOGL_ERROR,
Philipp Maierfbf66102017-04-09 12:32:51 +0200297 "Unable to parse element CELL IDENTIFIER (wrong field length) -- discarding message!\n");
Harald Weltea172e9e2018-02-09 21:33:24 +0100298 return -EINVAL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200299 }
300 memcpy(&lai_ci, data, sizeof(lai_ci));
301 if (lai_ci.ident != CELL_IDENT_WHOLE_GLOBAL) {
Harald Welte1f477442018-02-09 01:49:01 +0100302 LOGP(DBSSAP, LOGL_ERROR,
Philipp Maierfbf66102017-04-09 12:32:51 +0200303 "Unable to parse element CELL IDENTIFIER (wrong cell identification discriminator) -- discarding message!\n");
Harald Weltea172e9e2018-02-09 21:33:24 +0100304 return -EINVAL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200305 }
306 if (gsm48_decode_lai(&lai_ci.lai, &mcc, &mnc, &lac) != 0) {
Harald Welte1f477442018-02-09 01:49:01 +0100307 LOGP(DBSSAP, LOGL_ERROR,
Philipp Maierfbf66102017-04-09 12:32:51 +0200308 "Unable to parse element CELL IDENTIFIER (lai decoding failed) -- discarding message!\n");
Harald Weltea172e9e2018-02-09 21:33:24 +0100309 return -EINVAL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200310 }
311
312 /* Parse Layer 3 Information element */
313 /* FIXME: This is probably to hackish, compiler also complains "assignment discards ‘const’ qualifier..." */
Neels Hofmeyr9baedaf2017-12-18 04:07:01 +0100314 msg->l3h = (uint8_t*)TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
Philipp Maierfbf66102017-04-09 12:32:51 +0200315 msg->tail = msg->l3h + TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
316
317 /* Create new subscriber context */
318 conn = subscr_conn_allocate_a(a_conn_info, network, lac, scu, a_conn_info->conn_id);
319
320 /* Handover location update to the MSC code */
Philipp Maierfbf66102017-04-09 12:32:51 +0200321 rc = msc_compl_l3(conn, msg, 0);
Philipp Maier4502f5f2017-09-07 11:39:58 +0200322
Philipp Maierfbf66102017-04-09 12:32:51 +0200323 if (rc == MSC_CONN_ACCEPT) {
Harald Weltef0dc1be2018-02-09 00:37:56 +0100324 LOGP(DMSC, LOGL_INFO, "User has been accepted by MSC.\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200325 return 0;
326 } else if (rc == MSC_CONN_REJECT)
Harald Weltef0dc1be2018-02-09 00:37:56 +0100327 LOGP(DMSC, LOGL_INFO, "User has been rejected by MSC.\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200328 else
Harald Weltef0dc1be2018-02-09 00:37:56 +0100329 LOGP(DMSC, LOGL_INFO, "User has been rejected by MSC (unknown error)\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200330
331 return -EINVAL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200332}
333
334/* Endpoint to handle BSSMAP classmark update */
Harald Weltec27ef652018-02-09 01:23:25 +0100335static int bssmap_rx_classmark_upd(struct gsm_subscriber_connection *conn, struct msgb *msg)
Philipp Maierfbf66102017-04-09 12:32:51 +0200336{
Philipp Maierfbf66102017-04-09 12:32:51 +0200337 struct tlv_parsed tp;
338 const uint8_t *cm2 = NULL;
339 const uint8_t *cm3 = NULL;
340 uint8_t cm2_len = 0;
341 uint8_t cm3_len = 0;
342
Harald Weltefb7ba912018-02-09 01:05:27 +0100343 LOGPCONN(conn, LOGL_DEBUG, "Rx BSSMAP CLASSMARK UPDATE\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200344
345 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
346 if (!TLVP_PRESENT(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T2)) {
Harald Welte6de46592018-02-09 00:53:17 +0100347 LOGPCONN(conn, LOGL_ERROR, "Mandatory Classmark Information Type 2 not present -- discarding message!\n");
Harald Weltea172e9e2018-02-09 21:33:24 +0100348 return -EINVAL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200349 }
350
351 cm2 = TLVP_VAL(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T2);
352 cm2_len = TLVP_LEN(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T2);
353
354 if (TLVP_PRESENT(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T3)) {
355 cm3 = TLVP_VAL(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T3);
356 cm3_len = TLVP_LEN(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T3);
357 }
358
359 /* Inform MSC about the classmark change */
360 msc_classmark_chg(conn, cm2, cm2_len, cm3, cm3_len);
361
Philipp Maierfbf66102017-04-09 12:32:51 +0200362 return 0;
Philipp Maierfbf66102017-04-09 12:32:51 +0200363}
364
365/* Endpoint to handle BSSMAP cipher mode complete */
Harald Weltec27ef652018-02-09 01:23:25 +0100366static int bssmap_rx_ciph_compl(struct gsm_subscriber_connection *conn, struct msgb *msg)
Philipp Maierfbf66102017-04-09 12:32:51 +0200367{
368 /* FIXME: The field GSM0808_IE_LAYER_3_MESSAGE_CONTENTS is optional by
369 * means of the specification. So there can be messages without L3 info.
370 * In this case, the code will crash becrause msc_cipher_mode_compl()
371 * is not able to deal with msg = NULL and apperently
372 * msc_cipher_mode_compl() was never meant to be used without L3 data.
373 * This needs to be discussed further! */
374
Philipp Maierfbf66102017-04-09 12:32:51 +0200375 struct tlv_parsed tp;
376 uint8_t alg_id = 1;
377
Harald Weltefb7ba912018-02-09 01:05:27 +0100378 LOGPCONN(conn, LOGL_DEBUG, "Rx BSSMAP CIPHER MODE COMPLETE\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200379
380 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
381
382 if (TLVP_PRESENT(&tp, GSM0808_IE_CHOSEN_ENCR_ALG)) {
383 alg_id = TLVP_VAL(&tp, GSM0808_IE_CHOSEN_ENCR_ALG)[0] - 1;
384 }
385
386 if (TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_MESSAGE_CONTENTS)) {
Neels Hofmeyr9baedaf2017-12-18 04:07:01 +0100387 msg->l3h = (uint8_t*)TLVP_VAL(&tp, GSM0808_IE_LAYER_3_MESSAGE_CONTENTS);
Philipp Maierfbf66102017-04-09 12:32:51 +0200388 msg->tail = msg->l3h + TLVP_LEN(&tp, GSM0808_IE_LAYER_3_MESSAGE_CONTENTS);
389 } else {
Philipp Maierfbf66102017-04-09 12:32:51 +0200390 msg = NULL;
391 }
392
Philipp Maier4502f5f2017-09-07 11:39:58 +0200393 /* Hand over cipher mode complete message to the MSC */
Philipp Maierfbf66102017-04-09 12:32:51 +0200394 msc_cipher_mode_compl(conn, msg, alg_id);
395
396 return 0;
Philipp Maierfbf66102017-04-09 12:32:51 +0200397}
398
399/* Endpoint to handle BSSMAP cipher mode reject */
Harald Weltec27ef652018-02-09 01:23:25 +0100400static int bssmap_rx_ciph_rej(struct gsm_subscriber_connection *conn, struct msgb *msg)
Philipp Maierfbf66102017-04-09 12:32:51 +0200401{
Philipp Maierfbf66102017-04-09 12:32:51 +0200402 struct tlv_parsed tp;
403 uint8_t cause;
404
Harald Weltefb7ba912018-02-09 01:05:27 +0100405 LOGPCONN(conn, LOGL_NOTICE, "RX BSSMAP CIPHER MODE REJECT\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200406
407 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
408 if (!TLVP_PRESENT(&tp, BSS_MAP_MSG_CIPHER_MODE_REJECT)) {
Harald Welte6de46592018-02-09 00:53:17 +0100409 LOGPCONN(conn, LOGL_ERROR, "Cause code is missing -- discarding message!\n");
Harald Weltea172e9e2018-02-09 21:33:24 +0100410 return -EINVAL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200411 }
412
413 cause = TLVP_VAL(&tp, BSS_MAP_MSG_CIPHER_MODE_REJECT)[0];
Harald Welte6de46592018-02-09 00:53:17 +0100414 LOGPCONN(conn, LOGL_NOTICE, "Cipher mode rejection cause: %i\n", cause);
Philipp Maierfbf66102017-04-09 12:32:51 +0200415
416 /* FIXME: Can we do something meaningful here? e.g. report to the
417 * msc code somehow that the cipher mode command has failed. */
418
Philipp Maierfbf66102017-04-09 12:32:51 +0200419 return 0;
Philipp Maierfbf66102017-04-09 12:32:51 +0200420}
421
422/* Endpoint to handle BSSMAP assignment failure */
Harald Weltec27ef652018-02-09 01:23:25 +0100423static int bssmap_rx_ass_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
Philipp Maierfbf66102017-04-09 12:32:51 +0200424{
Philipp Maierfbf66102017-04-09 12:32:51 +0200425 struct tlv_parsed tp;
426 uint8_t cause;
427 uint8_t *rr_cause_ptr = NULL;
428 uint8_t rr_cause;
429
Harald Weltefb7ba912018-02-09 01:05:27 +0100430 LOGPCONN(conn, LOGL_NOTICE, "Rx BSSMAP ASSIGNMENT FAILURE message\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200431
432 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
433 if (!TLVP_PRESENT(&tp, GSM0808_IE_CAUSE)) {
Harald Welte6de46592018-02-09 00:53:17 +0100434 LOGPCONN(conn, LOGL_ERROR, "Cause code is missing -- discarding message!\n");
Harald Weltea172e9e2018-02-09 21:33:24 +0100435 return -EINVAL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200436 }
437 cause = TLVP_VAL(&tp, GSM0808_IE_CAUSE)[0];
438
439 if (TLVP_PRESENT(&tp, GSM0808_IE_RR_CAUSE)) {
440 rr_cause = TLVP_VAL(&tp, GSM0808_IE_RR_CAUSE)[0];
441 rr_cause_ptr = &rr_cause;
442 }
443
444 /* FIXME: In AoIP, the Assignment failure will carry also an optional
445 * Codec List (BSS Supported) element. It has to be discussed if we
446 * can ignore this element. If not, The msc_assign_fail() function
447 * call has to change. However msc_assign_fail() does nothing in the
448 * end. So probably we can just leave it as it is. Even for AoIP */
449
450 /* Inform the MSC about the assignment failure event */
451 msc_assign_fail(conn, cause, rr_cause_ptr);
452
Philipp Maierfbf66102017-04-09 12:32:51 +0200453 return 0;
Philipp Maierfbf66102017-04-09 12:32:51 +0200454}
455
456/* Endpoint to handle sapi "n" reject */
Harald Weltec27ef652018-02-09 01:23:25 +0100457static int bssmap_rx_sapi_n_rej(struct gsm_subscriber_connection *conn, struct msgb *msg)
Philipp Maierfbf66102017-04-09 12:32:51 +0200458{
Philipp Maierfbf66102017-04-09 12:32:51 +0200459 struct tlv_parsed tp;
460 uint8_t dlci;
461
Harald Welte35284462018-02-09 01:31:29 +0100462 LOGPCONN(conn, LOGL_NOTICE, "Rx BSSMAP SAPI-N-REJECT message\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200463
464 /* Note: The MSC code seems not to care about the cause code, but by
465 * the specification it is mandatory, so we check its presence. See
466 * also 3GPP TS 48.008 3.2.1.34 SAPI "n" REJECT */
467 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
468 if (!TLVP_PRESENT(&tp, GSM0808_IE_CAUSE)) {
Harald Welte6de46592018-02-09 00:53:17 +0100469 LOGPCONN(conn, LOGL_ERROR, "Cause code is missing -- discarding message!\n");
Harald Weltea172e9e2018-02-09 21:33:24 +0100470 return -EINVAL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200471 }
472
473 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
474 if (!TLVP_PRESENT(&tp, GSM0808_IE_DLCI)) {
Harald Welte6de46592018-02-09 00:53:17 +0100475 LOGPCONN(conn, LOGL_ERROR, "DLCI is missing -- discarding message!\n");
Harald Weltea172e9e2018-02-09 21:33:24 +0100476 return -EINVAL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200477 }
478 dlci = TLVP_VAL(&tp, GSM0808_IE_DLCI)[0];
479
480 /* Inform the MSC about the sapi "n" reject event */
481 msc_sapi_n_reject(conn, dlci);
482
Philipp Maierfbf66102017-04-09 12:32:51 +0200483 return 0;
Philipp Maierfbf66102017-04-09 12:32:51 +0200484}
485
486/* Endpoint to handle assignment complete */
Harald Weltec27ef652018-02-09 01:23:25 +0100487static int bssmap_rx_ass_compl(struct gsm_subscriber_connection *conn, struct msgb *msg)
Philipp Maierfbf66102017-04-09 12:32:51 +0200488{
Neels Hofmeyr6c8afe12017-09-04 01:03:58 +0200489 struct mgcp_client *mgcp;
Philipp Maierfbf66102017-04-09 12:32:51 +0200490 struct tlv_parsed tp;
491 struct sockaddr_storage rtp_addr;
492 struct sockaddr_in *rtp_addr_in;
493 int rc;
494
Neels Hofmeyr6c8afe12017-09-04 01:03:58 +0200495 mgcp = conn->network->mgw.client;
Philipp Maierfbf66102017-04-09 12:32:51 +0200496 OSMO_ASSERT(mgcp);
497
Harald Welte35284462018-02-09 01:31:29 +0100498 LOGPCONN(conn, LOGL_INFO, "Rx BSSMAP ASSIGNMENT COMPLETE message\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200499
500 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0);
501
502 if (!TLVP_PRESENT(&tp, GSM0808_IE_AOIP_TRASP_ADDR)) {
Harald Welte6de46592018-02-09 00:53:17 +0100503 LOGPCONN(conn, LOGL_ERROR, "AoIP transport identifier missing -- discarding message!\n");
Harald Weltea172e9e2018-02-09 21:33:24 +0100504 return -EINVAL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200505 }
506
507 /* Decode AoIP transport address element */
508 rc = gsm0808_dec_aoip_trasp_addr(&rtp_addr, TLVP_VAL(&tp, GSM0808_IE_AOIP_TRASP_ADDR),
509 TLVP_LEN(&tp, GSM0808_IE_AOIP_TRASP_ADDR));
510 if (rc < 0) {
Harald Welte6de46592018-02-09 00:53:17 +0100511 LOGPCONN(conn, LOGL_ERROR, "Unable to decode aoip transport address.\n");
Harald Weltea172e9e2018-02-09 21:33:24 +0100512 return -EINVAL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200513 }
514
515 /* use address / port supplied with the AoIP
516 * transport address element */
517 if (rtp_addr.ss_family == AF_INET) {
518 rtp_addr_in = (struct sockaddr_in *)&rtp_addr;
Philipp Maier621ba032017-11-07 17:19:25 +0100519 msc_mgcp_ass_complete(conn, osmo_ntohs(rtp_addr_in->sin_port), inet_ntoa(rtp_addr_in->sin_addr));
Philipp Maierfbf66102017-04-09 12:32:51 +0200520 } else {
Harald Welte6de46592018-02-09 00:53:17 +0100521 LOGPCONN(conn, LOGL_ERROR, "Unsopported addressing scheme. (supports only IPV4)\n");
Harald Weltea172e9e2018-02-09 21:33:24 +0100522 return -EINVAL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200523 }
524
525 /* FIXME: Seems to be related to authentication or,
526 encryption. Is this really in the right place? */
527 msc_rx_sec_mode_compl(conn);
528
Philipp Maierfbf66102017-04-09 12:32:51 +0200529 return 0;
Philipp Maierfbf66102017-04-09 12:32:51 +0200530}
531
532/* Handle incoming connection oriented BSSMAP messages */
533static int rx_bssmap(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
534{
Harald Weltec27ef652018-02-09 01:23:25 +0100535 struct gsm_subscriber_connection *conn;
536
Philipp Maierfbf66102017-04-09 12:32:51 +0200537 if (msgb_l3len(msg) < 1) {
Harald Welte1f477442018-02-09 01:49:01 +0100538 LOGP(DBSSAP, LOGL_NOTICE, "Error: No data received -- discarding message!\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200539 return -1;
540 }
541
Harald Weltec27ef652018-02-09 01:23:25 +0100542 /* Only message types allowed without a 'conn' */
543 switch (msg->l3h[0]) {
544 case BSS_MAP_MSG_COMPLETE_LAYER_3:
545 return bssmap_rx_l3_compl(scu, a_conn_info, msg);
546 case BSS_MAP_MSG_CLEAR_COMPLETE:
547 return bssmap_rx_clear_complete(scu, a_conn_info, msg);
548 default:
549 break;
550 }
551
552 conn = subscr_conn_lookup_a(a_conn_info->network, a_conn_info->conn_id);
553 if (!conn) {
Harald Welte1f477442018-02-09 01:49:01 +0100554 LOGP(DBSSAP, LOGL_ERROR, "Couldn't find subscr_conn for conn_id=%d\n", a_conn_info->conn_id);
Harald Weltec27ef652018-02-09 01:23:25 +0100555 return -EINVAL;
556 }
557
558 LOGPCONN(conn, LOGL_DEBUG, "Rx BSSMAP DT1 %s\n", gsm0808_bssmap_name(msg->l3h[0]));
Philipp Maierfbf66102017-04-09 12:32:51 +0200559
560 switch (msg->l3h[0]) {
561 case BSS_MAP_MSG_CLEAR_RQST:
Harald Weltec27ef652018-02-09 01:23:25 +0100562 return bssmap_rx_clear_rqst(conn, msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200563 case BSS_MAP_MSG_CLASSMARK_UPDATE:
Harald Weltec27ef652018-02-09 01:23:25 +0100564 return bssmap_rx_classmark_upd(conn, msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200565 case BSS_MAP_MSG_CIPHER_MODE_COMPLETE:
Harald Weltec27ef652018-02-09 01:23:25 +0100566 return bssmap_rx_ciph_compl(conn, msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200567 case BSS_MAP_MSG_CIPHER_MODE_REJECT:
Harald Weltec27ef652018-02-09 01:23:25 +0100568 return bssmap_rx_ciph_rej(conn, msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200569 case BSS_MAP_MSG_ASSIGMENT_FAILURE:
Harald Weltec27ef652018-02-09 01:23:25 +0100570 return bssmap_rx_ass_fail(conn, msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200571 case BSS_MAP_MSG_SAPI_N_REJECT:
Harald Weltec27ef652018-02-09 01:23:25 +0100572 return bssmap_rx_sapi_n_rej(conn, msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200573 case BSS_MAP_MSG_ASSIGMENT_COMPLETE:
Harald Weltec27ef652018-02-09 01:23:25 +0100574 return bssmap_rx_ass_compl(conn, msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200575 default:
Harald Weltec27ef652018-02-09 01:23:25 +0100576 LOGPCONN(conn, LOGL_ERROR, "Unimplemented msg type: %s\n", gsm0808_bssmap_name(msg->l3h[0]));
Philipp Maierfbf66102017-04-09 12:32:51 +0200577 return -EINVAL;
578 }
579
580 return -EINVAL;
581}
582
Harald Weltea172e9e2018-02-09 21:33:24 +0100583/* Endpoint to handle regular BSSAP DTAP messages. No ownership of 'msg' is passed on! */
Philipp Maierfbf66102017-04-09 12:32:51 +0200584static int rx_dtap(const struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
585{
586 struct gsm_network *network = a_conn_info->network;
587 struct gsm_subscriber_connection *conn;
588
589 conn = subscr_conn_lookup_a(network, a_conn_info->conn_id);
590 if (!conn) {
Philipp Maierfbf66102017-04-09 12:32:51 +0200591 return -EINVAL;
592 }
593
Harald Weltefb7ba912018-02-09 01:05:27 +0100594 LOGPCONN(conn, LOGL_DEBUG, "Rx DTAP %s\n", msgb_hexdump_l2(msg));
Philipp Maierfbf66102017-04-09 12:32:51 +0200595
596 /* msc_dtap expects the dtap payload in l3h */
597 msg->l3h = msg->l2h + 3;
598
Philipp Maier4502f5f2017-09-07 11:39:58 +0200599 /* Forward dtap payload into the msc */
Philipp Maierfbf66102017-04-09 12:32:51 +0200600 msc_dtap(conn, conn->a.conn_id, msg);
601
602 return 0;
603}
604
Harald Weltea172e9e2018-02-09 21:33:24 +0100605/* Handle incoming connection oriented messages. No ownership of 'msg' is passed on! */
Neels Hofmeyrc1d69252017-12-18 04:06:04 +0100606int a_sccp_rx_dt(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg)
Philipp Maierfbf66102017-04-09 12:32:51 +0200607{
608 OSMO_ASSERT(scu);
609 OSMO_ASSERT(a_conn_info);
610 OSMO_ASSERT(msg);
611
Philipp Maierfbf66102017-04-09 12:32:51 +0200612 if (msgb_l2len(msg) < sizeof(struct bssmap_header)) {
Harald Welte1f477442018-02-09 01:49:01 +0100613 LOGP(DBSSAP, LOGL_NOTICE, "The header is too short -- discarding message!\n");
Philipp Maier4502f5f2017-09-07 11:39:58 +0200614 return -EINVAL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200615 }
616
617 switch (msg->l2h[0]) {
618 case BSSAP_MSG_BSS_MANAGEMENT:
619 msg->l3h = &msg->l2h[sizeof(struct bssmap_header)];
620 return rx_bssmap(scu, a_conn_info, msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200621 case BSSAP_MSG_DTAP:
622 return rx_dtap(scu, a_conn_info, msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200623 default:
Harald Welte1f477442018-02-09 01:49:01 +0100624 LOGP(DBSSAP, LOGL_ERROR, "Unimplemented BSSAP msg type: %s\n", gsm0808_bssap_name(msg->l2h[0]));
Philipp Maierfbf66102017-04-09 12:32:51 +0200625 return -EINVAL;
626 }
627
628 return -EINVAL;
629}