Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 1 | /* Code to manage MSC RAN connections over IuCS interface */ |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * (C) 2016,2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 5 | * |
| 6 | * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de> |
| 7 | * |
| 8 | * All Rights Reserved |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Affero General Public License as published by |
| 12 | * the Free Software Foundation; either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Affero General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Affero General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <inttypes.h> |
| 26 | |
| 27 | #include <osmocom/core/logging.h> |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 28 | #include <osmocom/ranap/iu_client.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 29 | #include <osmocom/msc/debug.h> |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 30 | |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 31 | #include <osmocom/msc/gsm_data.h> |
| 32 | #include <osmocom/msc/gsm_subscriber.h> |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 33 | #include <osmocom/msc/transaction.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 34 | #include <osmocom/msc/vlr.h> |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 35 | #include <osmocom/core/byteswap.h> |
| 36 | |
| 37 | #include "../../bscconfig.h" |
| 38 | |
| 39 | #ifdef BUILD_IU |
| 40 | #include <osmocom/ranap/iu_client.h> |
| 41 | extern struct msgb *ranap_new_msg_rab_assign_voice(uint8_t rab_id, |
| 42 | uint32_t rtp_ip, |
| 43 | uint16_t rtp_port, |
| 44 | bool use_x213_nsap); |
| 45 | #else |
| 46 | #include <osmocom/msc/iu_dummy.h> |
| 47 | #endif /* BUILD_IU */ |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 48 | |
| 49 | /* For A-interface see libbsc/bsc_api.c subscr_con_allocate() */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 50 | static struct ran_conn *ran_conn_allocate_iu(struct gsm_network *network, |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 51 | struct ranap_ue_conn_ctx *ue, |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 52 | uint16_t lac) |
| 53 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 54 | struct ran_conn *conn; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 55 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 56 | DEBUGP(DIUCS, "Allocating IuCS RAN conn: lac %d, conn_id %" PRIx32 "\n", |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 57 | lac, ue->conn_id); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 58 | |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 59 | conn = ran_conn_alloc(network, OSMO_RAT_UTRAN_IU, lac); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 60 | if (!conn) |
| 61 | return NULL; |
| 62 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 63 | conn->iu.ue_ctx = ue; |
| 64 | conn->iu.ue_ctx->rab_assign_addr_enc = network->iu.rab_assign_addr_enc; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 65 | return conn; |
| 66 | } |
| 67 | |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 68 | static int same_ue_conn(struct ranap_ue_conn_ctx *a, struct ranap_ue_conn_ctx *b) |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 69 | { |
| 70 | if (a == b) |
| 71 | return 1; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 72 | return (a->conn_id == b->conn_id); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static inline void log_subscribers(struct gsm_network *network) |
| 76 | { |
| 77 | if (!log_check_level(DIUCS, LOGL_DEBUG)) |
| 78 | return; |
| 79 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 80 | struct ran_conn *conn; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 81 | int i = 0; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 82 | llist_for_each_entry(conn, &network->ran_conns, entry) { |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 83 | DEBUGP(DIUCS, "%3d: %s", i, vlr_subscr_name(conn->vsub)); |
| 84 | switch (conn->via_ran) { |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 85 | case OSMO_RAT_UTRAN_IU: |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 86 | DEBUGPC(DIUCS, " Iu"); |
| 87 | if (conn->iu.ue_ctx) { |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 88 | DEBUGPC(DIUCS, " conn_id %d", |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 89 | conn->iu.ue_ctx->conn_id |
| 90 | ); |
| 91 | } |
| 92 | break; |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 93 | case OSMO_RAT_GERAN_A: |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 94 | DEBUGPC(DIUCS, " A"); |
| 95 | /* TODO log A-interface connection details */ |
| 96 | break; |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 97 | case OSMO_RAT_UNKNOWN: |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 98 | DEBUGPC(DIUCS, " ?"); |
| 99 | break; |
| 100 | default: |
| 101 | DEBUGPC(DIUCS, " invalid"); |
| 102 | break; |
| 103 | } |
| 104 | DEBUGPC(DIUCS, "\n"); |
| 105 | i++; |
| 106 | } |
| 107 | DEBUGP(DIUCS, "subscribers registered: %d\n", i); |
| 108 | } |
| 109 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 110 | /* Return an existing IuCS RAN connection record for the given |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 111 | * connection IDs, or return NULL if not found. */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 112 | struct ran_conn *ran_conn_lookup_iu( |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 113 | struct gsm_network *network, |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 114 | struct ranap_ue_conn_ctx *ue) |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 115 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 116 | struct ran_conn *conn; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 117 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 118 | DEBUGP(DIUCS, "Looking for IuCS subscriber: conn_id %" PRIx32 "\n", |
| 119 | ue->conn_id); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 120 | log_subscribers(network); |
| 121 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 122 | llist_for_each_entry(conn, &network->ran_conns, entry) { |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 123 | if (conn->via_ran != OSMO_RAT_UTRAN_IU) |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 124 | continue; |
| 125 | if (!same_ue_conn(conn->iu.ue_ctx, ue)) |
| 126 | continue; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 127 | DEBUGP(DIUCS, "Found IuCS subscriber for conn_id %" PRIx32 "\n", |
| 128 | ue->conn_id); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 129 | return conn; |
| 130 | } |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 131 | DEBUGP(DIUCS, "No IuCS subscriber found for conn_id %" PRIx32 "\n", |
| 132 | ue->conn_id); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | /* Receive MM/CC/... message from IuCS (SCCP user SAP). |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 137 | * msg->dst must reference a struct ranap_ue_conn_ctx, which identifies the peer that |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 138 | * sent the msg. |
| 139 | * |
| 140 | * For A-interface see libbsc/bsc_api.c gsm0408_rcvmsg(). */ |
| 141 | int gsm0408_rcvmsg_iucs(struct gsm_network *network, struct msgb *msg, |
| 142 | uint16_t *lac) |
| 143 | { |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 144 | struct ranap_ue_conn_ctx *ue_ctx; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 145 | struct ran_conn *conn; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 146 | |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 147 | ue_ctx = (struct ranap_ue_conn_ctx*)msg->dst; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 148 | |
| 149 | /* TODO: are there message types that could allow us to skip this |
| 150 | * search? */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 151 | conn = ran_conn_lookup_iu(network, ue_ctx); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 152 | |
| 153 | if (conn && lac && (conn->lac != *lac)) { |
| 154 | LOGP(DIUCS, LOGL_ERROR, "IuCS subscriber has changed LAC" |
| 155 | " within the same connection, discarding connection:" |
| 156 | " %s from LAC %d to %d\n", |
| 157 | vlr_subscr_name(conn->vsub), conn->lac, *lac); |
| 158 | /* Deallocate conn with previous LAC */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 159 | ran_conn_close(conn, GSM_CAUSE_INV_MAND_INFO); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 160 | /* At this point we could be tolerant and allocate a new |
| 161 | * connection, but changing the LAC within the same connection |
| 162 | * is shifty. Rather cancel everything. */ |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | if (conn) { |
| 167 | /* Make sure we don't receive RR over IuCS; otherwise all |
| 168 | * messages handled by gsm0408_dispatch() are of interest (CC, |
| 169 | * MM, SMS, NS_SS, maybe even MM_GPRS and SM_GPRS). */ |
| 170 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 171 | uint8_t pdisc = gh->proto_discr & 0x0f; |
| 172 | OSMO_ASSERT(pdisc != GSM48_PDISC_RR); |
| 173 | |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 174 | ran_conn_dtap(conn, msg); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 175 | } else { |
| 176 | /* allocate a new connection */ |
| 177 | |
| 178 | if (!lac) { |
| 179 | LOGP(DIUCS, LOGL_ERROR, "New IuCS subscriber" |
| 180 | " but no LAC available. Expecting an InitialUE" |
| 181 | " message containing a LAI IE." |
| 182 | " Dropping connection.\n"); |
| 183 | return -1; |
| 184 | } |
| 185 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 186 | conn = ran_conn_allocate_iu(network, ue_ctx, *lac); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 187 | if (!conn) |
| 188 | abort(); |
| 189 | |
| 190 | /* ownership of conn hereby goes to the MSC: */ |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 191 | ran_conn_compl_l3(conn, msg, 0); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Neels Hofmeyr | d03e728 | 2018-11-30 01:20:32 +0100 | [diff] [blame] | 194 | return 0; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 195 | } |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 196 | |
| 197 | int iu_rab_act_cs(struct gsm_trans *trans) |
| 198 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 199 | struct ran_conn *conn; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 200 | struct msgb *msg; |
| 201 | bool use_x213_nsap; |
| 202 | uint32_t conn_id; |
| 203 | struct ranap_ue_conn_ctx *uectx; |
| 204 | uint8_t rab_id; |
| 205 | uint32_t rtp_ip; |
| 206 | uint16_t rtp_port; |
| 207 | |
| 208 | conn = trans->conn; |
| 209 | uectx = conn->iu.ue_ctx; |
| 210 | rab_id = conn->iu.rab_id; |
| 211 | rtp_ip = osmo_htonl(inet_addr(conn->rtp.local_addr_ran)); |
| 212 | rtp_port = conn->rtp.local_port_ran; |
| 213 | conn_id = uectx->conn_id; |
| 214 | |
| 215 | if (rtp_ip == INADDR_NONE) { |
| 216 | LOGP(DIUCS, LOGL_DEBUG, |
| 217 | "Assigning RAB: conn_id=%u, rab_id=%d, invalid RTP IP-Address\n", |
| 218 | conn_id, rab_id); |
| 219 | return -EINVAL; |
| 220 | } |
| 221 | if (rtp_port == 0) { |
| 222 | LOGP(DIUCS, LOGL_DEBUG, |
| 223 | "Assigning RAB: conn_id=%u, rab_id=%d, invalid RTP Port\n", |
| 224 | conn_id, rab_id); |
| 225 | return -EINVAL; |
| 226 | } |
| 227 | |
| 228 | use_x213_nsap = |
| 229 | (uectx->rab_assign_addr_enc == RANAP_NSAP_ADDR_ENC_X213); |
| 230 | |
| 231 | LOGP(DIUCS, LOGL_DEBUG, |
| 232 | "Assigning RAB: conn_id=%u, rab_id=%d, rtp=%x:%u, use_x213_nsap=%d\n", |
| 233 | conn_id, rab_id, rtp_ip, rtp_port, use_x213_nsap); |
| 234 | |
| 235 | msg = ranap_new_msg_rab_assign_voice(rab_id, rtp_ip, rtp_port, |
| 236 | use_x213_nsap); |
| 237 | msg->l2h = msg->data; |
| 238 | |
| 239 | if (ranap_iu_rab_act(uectx, msg)) |
| 240 | LOGP(DIUCS, LOGL_ERROR, |
| 241 | "Failed to send RAB Assignment: conn_id=%d rab_id=%d rtp=%x:%u\n", |
| 242 | conn_id, rab_id, rtp_ip, rtp_port); |
| 243 | return 0; |
| 244 | } |
| 245 | |
Daniel Willmann | 6fbd3bf | 2018-02-15 10:33:21 +0100 | [diff] [blame] | 246 | uint32_t iu_get_conn_id(const struct ranap_ue_conn_ctx *ue) |
| 247 | { |
| 248 | return ue->conn_id; |
| 249 | } |