aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmsc/neighbor_ident.c
blob: 4a0cd47ad379148b887e7f1ea6b3e4ac24168283 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* Manage identity of neighboring BSS cells for inter-BSC handover.
 *
 * Measurement reports tell us about neighbor ARFCN and BSIC. If that ARFCN and BSIC is not managed by
 * this local BSS, we need to tell the MSC a cell identity, like CGI, LAC+CI, etc. -- hence we need a
 * mapping from ARFCN+BSIC to Cell Identifier List, which needs to be configured by the user.
 */
/* (C) 2018 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
 *
 * All Rights Reserved
 *
 * Author: Neels Hofmeyr <nhofmeyr@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/bsc/neighbor_ident.h>

struct neighbor_ident_list {
	struct llist_head list;
};

struct neighbor_ident {
	struct llist_head entry;

	struct neighbor_ident_key key;
	struct gsm0808_cell_id_list2 val;
};

#define APPEND_THING(func, args...) do { \
		int remain = buflen - (pos - buf); \
		int l = func(pos, remain, ##args); \
		if (l < 0 || l > remain) \
			pos = buf + buflen; \
		else \
			pos += l; \
	} while(0)
#define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args)

const char *_neighbor_ident_key_name(char *buf, size_t buflen, const struct neighbor_ident_key *ni_key)
{
	char *pos = buf;

	APPEND_STR("BTS ");
	if (ni_key->from_bts == NEIGHBOR_IDENT_KEY_ANY_BTS)
		APPEND_STR("*");
	else if (ni_key->from_bts >= 0 && ni_key->from_bts <= 255)
		APPEND_STR("%d", ni_key->from_bts);
	else
		APPEND_STR("invalid(%d)", ni_key->from_bts);

	APPEND_STR(" to ");
	if (ni_key->bsic == BSIC_ANY)
		APPEND_STR("ARFCN %u (any BSIC)", ni_key->arfcn);
	else
		APPEND_STR("ARFCN %u BSIC %u", ni_key->arfcn, ni_key->bsic & 0x3f);
	return buf;
}

const char *neighbor_ident_key_name(const struct neighbor_ident_key *ni_key)
{
	static char buf[64];
	return _neighbor_ident_key_name(buf, sizeof(buf), ni_key);
}

struct neighbor_ident_list *neighbor_ident_init(void *talloc_ctx)
{
	struct neighbor_ident_list *nil = talloc_zero(talloc_ctx, struct neighbor_ident_list);
	OSMO_ASSERT(nil);
	INIT_LLIST_HEAD(&nil->list);
	return nil;
}

void neighbor_ident_free(struct neighbor_ident_list *nil)
{
	if (!nil)
		return;
	talloc_free(nil);
}

/* Return true when the entry matches the search_for requirements.
 * If exact_match is false, a BSIC_ANY entry acts as wildcard to match any search_for on that ARFCN,
 * and a BSIC_ANY in search_for likewise returns any one entry that matches the ARFCN;
 * also a from_bts == NEIGHBOR_IDENT_KEY_ANY_BTS in either entry or search_for will match.
 * If exact_match is true, only identical bsic values and identical from_bts values return a match.
 * Note, typically wildcard BSICs are only in entry, e.g. the user configured list, and search_for
 * contains a specific BSIC, e.g. as received from a Measurement Report. */
bool neighbor_ident_key_match(const struct neighbor_ident_key *entry,
			      const struct neighbor_ident_key *search_for,
			      bool exact_match)
{
	if (exact_match
	    && entry->from_bts != search_for->from_bts)
		return false;

	if (search_for->from_bts != NEIGHBOR_IDENT_KEY_ANY_BTS
	    && entry->from_bts != NEIGHBOR_IDENT_KEY_ANY_BTS
	    && entry->from_bts != search_for->from_bts)
		return false;

	if (entry->arfcn != search_for->arfcn)
		return false;

	if (exact_match && entry->bsic != search_for->bsic)
		return false;

	if (entry->bsic == BSIC_ANY || search_for->bsic == BSIC_ANY)
		return true;

	return entry->bsic == search_for->bsic;
}

static struct neighbor_ident *_neighbor_ident_get(const struct neighbor_ident_list *nil,
						  const struct neighbor_ident_key *key,
						  bool exact_match)
{
	struct neighbor_ident *ni;
	struct neighbor_ident *wildcard_match = NULL;

	/* Do both exact-bsic and wildcard matching in the same iteration:
	 * Any exact match returns immediately, while for a wildcard match we still go through all
	 * remaining items in case an exact match exists. */
	llist_for_each_entry(ni, &nil->list, entry) {
		if (neighbor_ident_key_match(&ni->key, key, true))
			return ni;
		if (!exact_match) {
			if (neighbor_ident_key_match(&ni->key, key, false))
				wildcard_match = ni;
		}
	}
	return wildcard_match;
}

static void _neighbor_ident_free(struct neighbor_ident *ni)
{
	llist_del(&ni->entry);
	talloc_free(ni);
}

bool neighbor_ident_key_valid(const struct neighbor_ident_key *key)
{
	if (key->from_bts != NEIGHBOR_IDENT_KEY_ANY_BTS
	    && (key->from_bts < 0 || key->from_bts > 255))
		return false;

	if (key->bsic != BSIC_ANY && key->bsic > 0x3f)
		return false;
	return true;
}

/*! Add Cell Identifiers to an ARFCN+BSIC entry.
 * Exactly one kind of identifier is allowed per ARFCN+BSIC entry, and any number of entries of that kind
 * may be added up to the capacity of gsm0808_cell_id_list2, by one or more calls to this function. To
 * replace an existing entry, first call neighbor_ident_del(nil, key).
 * \returns number of entries in the resulting identifier list, or negative on error:
 *   see gsm0808_cell_id_list_add() for the meaning of returned error codes;
 *   return -ENOMEM when the list is not initialized, -ERANGE when the BSIC value is too large. */
int neighbor_ident_add(struct neighbor_ident_list *nil, const struct neighbor_ident_key *key,
		       const struct gsm0808_cell_id_list2 *val)
{
	struct neighbor_ident *ni;
	int rc;

	if (!nil)
		return -ENOMEM;

	if (!neighbor_ident_key_valid(key))
		return -ERANGE;

	ni = _neighbor_ident_get(nil, key, true);
	if (!ni) {
		ni = talloc_zero(nil, struct neighbor_ident);
		OSMO_ASSERT(ni);
		*ni = (struct neighbor_ident){
			.key = *key,
			.val = *val,
		};
		llist_add_tail(&ni->entry, &nil->list);
		return ni->val.id_list_len;
	}

	rc = gsm0808_cell_id_list_add(&ni->val, val);

	if (rc < 0)
		return rc;

	return ni->val.id_list_len;
}

/*! Find cell identity for given BTS, ARFCN and BSIC, as previously added by neighbor_ident_add().
 */
const struct gsm0808_cell_id_list2 *neighbor_ident_get(const struct neighbor_ident_list *nil,
						       const struct neighbor_ident_key *key)
{
	struct neighbor_ident *ni;
	if (!nil)
		return NULL;
	ni = _neighbor_ident_get(nil, key, false);
	if (!ni)
		return NULL;
	return &ni->val;
}

bool neighbor_ident_del(struct neighbor_ident_list *nil, const struct neighbor_ident_key *key)
{
	struct neighbor_ident *ni;
	if (!nil)
		return false;
	ni = _neighbor_ident_get(nil, key, true);
	if (!ni)
		return false;
	_neighbor_ident_free(ni);
	return true;
}

void neighbor_ident_clear(struct neighbor_ident_list *nil)
{
	struct neighbor_ident *ni;
	while ((ni = llist_first_entry_or_null(&nil->list, struct neighbor_ident, entry)))
		_neighbor_ident_free(ni);
}

/*! Iterate all neighbor_ident_list entries and call iter_cb for each.
 * If iter_cb returns false, the iteration is stopped. */
void neighbor_ident_iter(const struct neighbor_ident_list *nil,
			 bool (* iter_cb )(const struct neighbor_ident_key *key,
					   const struct gsm0808_cell_id_list2 *val,
					   void *cb_data),
			 void *cb_data)
{
	struct neighbor_ident *ni, *ni_next;
	if (!nil)
		return;
	llist_for_each_entry_safe(ni, ni_next, &nil->list, entry) {
		if (!iter_cb(&ni->key, &ni->val, cb_data))
			return;
	}
}