aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmsc/ran_conn.c
blob: 8418c9eb59bc682b72db08662030aef9475feb52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* MSC RAN connection implementation */

/*
 * (C) 2016-2018 by sysmocom s.m.f.c. <info@sysmocom.de>
 * All Rights Reserved
 *
 * Author: Neels Hofmeyr
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <osmocom/core/logging.h>
#include <osmocom/core/fsm.h>
#include <osmocom/core/signal.h>

#include <osmocom/msc/ran_conn.h>
#include <osmocom/msc/vlr.h>
#include <osmocom/msc/debug.h>
#include <osmocom/msc/transaction.h>
#include <osmocom/msc/signal.h>
#include <osmocom/msc/sgs_iface.h>
#include <osmocom/msc/ran_peer.h>
#include <osmocom/msc/sccp_ran.h>
#include <osmocom/msc/ran_infra.h>
#include <osmocom/msc/msub.h>

struct ran_conn *ran_conn_create_incoming(struct ran_peer *ran_peer, uint32_t sccp_conn_id)
{
	struct ran_conn *conn;

	conn = talloc(ran_peer, struct ran_conn);
	OSMO_ASSERT(conn);

	*conn = (struct ran_conn){
		.ran_peer = ran_peer,
		.sccp_conn_id = sccp_conn_id,
	};

	llist_add(&conn->entry, &ran_peer->sri->ran_conns);
	return conn;
}

struct ran_conn *ran_conn_create_outgoing(struct ran_peer *ran_peer)
{
	/* FIXME use method being developed in gerrit id Ifd55c6b7ed2558ff072042079cf45f5068a971de */
	static uint32_t next_outgoing_conn_id = 2342;
	uint32_t conn_id = 0;
	int attempts = 1000;
	bool already_used = true;
	while (attempts--) {
		struct ran_conn *conn;

		conn_id = next_outgoing_conn_id;
		next_outgoing_conn_id++;

		already_used = false;
		llist_for_each_entry(conn, &ran_peer->sri->ran_conns, entry) {
			if (conn->sccp_conn_id == conn_id) {
				already_used = true;
				break;
			}
		}

		if (!already_used)
			break;
	}
	if (already_used)
		return NULL;
	LOG_RAN_PEER(ran_peer, LOGL_DEBUG, "Outgoing conn id: %u\n", conn_id);
	return ran_conn_create_incoming(ran_peer, conn_id);
}

/* Return statically allocated string of the ran_conn RAT type and id. */
const char *ran_conn_name(struct ran_conn *conn)
{
	static char id[42];
	int rc;
	const char *ran_peer_name;

	if (!conn)
		return "ran_conn==NULL";

	if (!conn->ran_peer || !conn->ran_peer->sri || !conn->ran_peer->sri->ran)
		ran_peer_name = "no-RAN-peer";
	else
		ran_peer_name = osmo_rat_type_name(conn->ran_peer->sri->ran->type);

	rc = snprintf(id, sizeof(id), "%s-%u", ran_peer_name, conn->sccp_conn_id);
	/* < 0 is error, == 0 is empty, >= size means truncation. Not really expecting this to catch on in any practical
	 * situation. */
	if (rc <= 0 || rc >= sizeof(id))
		return "conn-name-error";
	return id;
}

int ran_conn_down_l2_co(struct ran_conn *conn, struct msgb *l3, bool initial)
{
	struct ran_peer_ev_ctx co = {
		.conn_id = conn->sccp_conn_id,
		.conn = conn,
		.msg = l3,
	};
	if (!conn->ran_peer)
		return -EIO;
	return osmo_fsm_inst_dispatch(conn->ran_peer->fi,
				      initial ? RAN_PEER_EV_MSG_DOWN_CO_INITIAL : RAN_PEER_EV_MSG_DOWN_CO,
				      &co);
}

void ran_conn_msc_role_gone(struct ran_conn *conn, struct osmo_fsm_inst *msc_role)
{
	if (!conn)
		return;

	if (conn->msc_role != msc_role)
		return;

	conn->msc_role = NULL;
	ran_conn_close(conn);
}

/* Regularly close the conn */
void ran_conn_close(struct ran_conn *conn)
{
	if (!conn)
		return;
	if (conn->closing)
		return;
	conn->closing = true;
	LOG_RAN_PEER(conn->ran_peer, LOGL_DEBUG, "Closing %s\n", ran_conn_name(conn));

	if (conn->msc_role) {
		osmo_fsm_inst_dispatch(conn->msc_role, MSC_EV_FROM_RAN_CONN_RELEASED, NULL);
		conn->msc_role = NULL;
	}

	if (conn->ran_peer) {
		/* Todo: pass a useful SCCP cause? */
		sccp_ran_disconnect(conn->ran_peer->sri, conn->sccp_conn_id, 0);
		conn->ran_peer = NULL;
	}

	LOG_RAN_PEER(conn->ran_peer, LOGL_DEBUG, "Deallocating %s\n", ran_conn_name(conn));
	llist_del(&conn->entry);
	talloc_free(conn);
}

/* Same as ran_conn_close() but without sending any SCCP messages (e.g. after RESET) */
void ran_conn_discard(struct ran_conn *conn)
{
	if (!conn)
		return;
	/* Make sure to drop dead and don't dispatch things like DISCONNECT requests on SCCP. */
	conn->ran_peer = NULL;
	ran_conn_close(conn);
}