aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmsc/neighbor_ident.c
blob: 5120e168ec59e09706da388924df4f674ea72575 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* Manage identity of neighboring BSS cells for inter-MSC handover. */
/*
 * (C) 2018-2019 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
 * All Rights Reserved
 *
 * SPDX-License-Identifier: AGPL-3.0+
 *
 * Author: Neels Hofmeyr
 * Author: Stefan Sperling <ssperling@sysmocom.de>
 *
 * 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 <errno.h>

#include <osmocom/core/linuxlist.h>
#include <osmocom/core/utils.h>
#include <osmocom/gsm/gsm0808.h>
#include <osmocom/sigtran/osmo_ss7.h>
#include <osmocom/sigtran/sccp_helpers.h>

#include <osmocom/msc/neighbor_ident.h>
#include <osmocom/msc/gsm_data.h>
#include <osmocom/msc/sccp_ran.h>
#include <osmocom/msc/cell_id_list.h>

/* XXX greater than or equal to IPA_STRING_MAX (libosmocore) and MAX_PC_STR_LEN (libosmo-sccp). */
#define NEIGHBOR_IDENT_ADDR_STRING_MAX 64

static struct gsm_network *gsmnet;

void neighbor_ident_init(struct gsm_network *net)
{
	gsmnet = net;
	INIT_LLIST_HEAD(&gsmnet->neighbor_ident_list);
}

int msc_ipa_name_from_str(struct msc_ipa_name *min, const char *name)
{
	int rc = osmo_strlcpy(min->buf, name, sizeof(min->buf));
	if (rc >= sizeof(min->buf)) {
		min->len = 0;
		return -1;
	}
	min->len = rc;
	return 0;
}

int msc_ipa_name_cmp(const struct msc_ipa_name *a, const struct msc_ipa_name *b)
{
	size_t cmp_len;
	int rc;
	if (a == b)
		return 0;
	if (!a || !b)
		return a ? 1 : -1;
	cmp_len = OSMO_MIN(sizeof(a->buf), OSMO_MIN(a->len, b->len));
	if (!cmp_len)
		rc = 0;
	else
		rc = memcmp(a->buf, b->buf, cmp_len);
	if (rc)
		return rc;
	if (a->len < b->len)
		return -1;
	if (a->len > b->len)
		return 1;
	return 0;
}

const char *neighbor_ident_addr_name(const struct neighbor_ident_addr *nia)
{
	static char buf[128];
	struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) };

	OSMO_STRBUF_PRINTF(sb, "%s-", osmo_rat_type_name(nia->ran_type));

	switch (nia->type) {
	case MSC_NEIGHBOR_TYPE_LOCAL_RAN_PEER:
		OSMO_STRBUF_PRINTF(sb, "localRAN-%s", nia->local_ran_peer_pc_str);
		break;
	case MSC_NEIGHBOR_TYPE_REMOTE_MSC:
		OSMO_STRBUF_PRINTF(sb, "remoteMSC-");
		OSMO_STRBUF_APPEND_NOLEN(sb, osmo_escape_str_buf2, nia->remote_msc_ipa_name.buf,
					 nia->remote_msc_ipa_name.len);
		break;
	default:
		return NULL;
	}

	return buf;
}

const struct neighbor_ident_entry *neighbor_ident_add(struct llist_head *ni_list,
						      const struct neighbor_ident_addr *nia,
						      const struct gsm0808_cell_id *cid)
{
	struct neighbor_ident_entry *nie;

	if (!ni_list)
		return NULL;

	nie = (struct neighbor_ident_entry*)neighbor_ident_find_by_addr(ni_list, nia);
	if (!nie) {
		nie = talloc_zero(gsmnet, struct neighbor_ident_entry);
		OSMO_ASSERT(nie);
		*nie = (struct neighbor_ident_entry){
			.addr = *nia,
		};
		INIT_LLIST_HEAD(&nie->cell_ids);
		llist_add_tail(&nie->entry, ni_list);
	}

	cell_id_list_add_cell(nie, &nie->cell_ids, cid);
	return nie;
}

const struct neighbor_ident_entry *neighbor_ident_find_by_cell(const struct llist_head *ni_list,
							       enum osmo_rat_type ran_type,
							       const struct gsm0808_cell_id *cell_id)
{
	struct neighbor_ident_entry *e;
	llist_for_each_entry(e, ni_list, entry) {
		if (ran_type != OSMO_RAT_UNKNOWN) {
			if (e->addr.ran_type != ran_type)
				continue;
		}

		if (!cell_id_list_find(&e->cell_ids, cell_id, 0, false))
			continue;
		return e;
	}
	return NULL;
}

const struct neighbor_ident_entry *neighbor_ident_find_by_addr(const struct llist_head *ni_list,
							       const struct neighbor_ident_addr *nia)
{
	struct neighbor_ident_entry *e;

	llist_for_each_entry(e, ni_list, entry) {
		if (nia->ran_type != OSMO_RAT_UNKNOWN
		    && e->addr.ran_type != nia->ran_type)
			continue;

		if (e->addr.type != nia->type)
			continue;

		switch (e->addr.type) {
		case MSC_NEIGHBOR_TYPE_LOCAL_RAN_PEER:
			if (strcmp(e->addr.local_ran_peer_pc_str, nia->local_ran_peer_pc_str))
				continue;
			break;
		case MSC_NEIGHBOR_TYPE_REMOTE_MSC:
			if (msc_ipa_name_cmp(&e->addr.remote_msc_ipa_name, &nia->remote_msc_ipa_name))
				continue;
			break;
		default:
			continue;
		}

		return e;
	}

	return NULL;
}

void neighbor_ident_del(const struct neighbor_ident_entry *nie)
{
	struct neighbor_ident_entry *e = (struct neighbor_ident_entry*)nie;
	llist_del(&e->entry);
	talloc_free(e);
}

void neighbor_ident_clear(struct llist_head *ni_list)
{
	struct neighbor_ident_entry *nie;
	while ((nie = llist_first_entry_or_null(ni_list, struct neighbor_ident_entry, entry)))
		neighbor_ident_del(nie);
}