blob: 920547c9e0824726dd33908561eba19b0d29ef35 [file] [log] [blame]
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001/* Gb proxy peer handling */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010-2013 by On-Waves
5 * (C) 2013 by Holger Hans Peter Freyther
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <osmocom/sgsn/gb_proxy.h>
24
25#include <osmocom/sgsn/debug.h>
26
27#include <osmocom/gprs/protocol/gsm_08_18.h>
28#include <osmocom/core/rate_ctr.h>
29#include <osmocom/core/stats.h>
30#include <osmocom/core/talloc.h>
31#include <osmocom/gsm/tlv.h>
32
33#include <string.h>
34
35extern void *tall_sgsn_ctx;
36
37static const struct rate_ctr_desc peer_ctr_description[] = {
38 { "blocked", "BVC Block " },
39 { "unblocked", "BVC Unblock " },
40 { "dropped", "BVC blocked, dropped packet " },
41 { "inv-nsei", "NSEI mismatch " },
42 { "tx-err", "NS Transmission error " },
43 { "raid-mod:bss", "RAID patched (BSS )" },
44 { "raid-mod:sgsn", "RAID patched (SGSN)" },
45 { "apn-mod:sgsn", "APN patched " },
46 { "tlli-mod:bss", "TLLI patched (BSS )" },
47 { "tlli-mod:sgsn", "TLLI patched (SGSN)" },
48 { "ptmsi-mod:bss", "P-TMSI patched (BSS )" },
49 { "ptmsi-mod:sgsn","P-TMSI patched (SGSN)" },
50 { "mod-crypt-err", "Patch error: encrypted " },
51 { "mod-err", "Patch error: other " },
52 { "attach-reqs", "Attach Request count " },
53 { "attach-rejs", "Attach Reject count " },
54 { "attach-acks", "Attach Accept count " },
55 { "attach-cpls", "Attach Completed count " },
56 { "ra-upd-reqs", "RoutingArea Update Request count" },
57 { "ra-upd-rejs", "RoutingArea Update Reject count " },
58 { "ra-upd-acks", "RoutingArea Update Accept count " },
59 { "ra-upd-cpls", "RoutingArea Update Compltd count" },
60 { "gmm-status", "GMM Status count (BSS)" },
61 { "gmm-status", "GMM Status count (SGSN)" },
62 { "detach-reqs", "Detach Request count " },
63 { "detach-acks", "Detach Accept count " },
64 { "pdp-act-reqs", "PDP Activation Request count " },
65 { "pdp-act-rejs", "PDP Activation Reject count " },
66 { "pdp-act-acks", "PDP Activation Accept count " },
67 { "pdp-deact-reqs","PDP Deactivation Request count " },
68 { "pdp-deact-acks","PDP Deactivation Accept count " },
69 { "tlli-unknown", "TLLI from SGSN unknown " },
70 { "tlli-cache", "TLLI cache size " },
71};
72
73osmo_static_assert(ARRAY_SIZE(peer_ctr_description) == GBPROX_PEER_CTR_LAST, everything_described);
74
75static const struct rate_ctr_group_desc peer_ctrg_desc = {
76 .group_name_prefix = "gbproxy:peer",
77 .group_description = "GBProxy Peer Statistics",
78 .num_ctr = ARRAY_SIZE(peer_ctr_description),
79 .ctr_desc = peer_ctr_description,
80 .class_id = OSMO_STATS_CLASS_PEER,
81};
82
83
Daniel Willmanne50550e2020-11-26 18:19:21 +010084/* Find the gbproxy_peer by its BVCI. There can only be one match */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020085struct gbproxy_peer *gbproxy_peer_by_bvci(struct gbproxy_config *cfg, uint16_t bvci)
86{
Daniel Willmanne50550e2020-11-26 18:19:21 +010087 struct gbproxy_nse *nse;
88
89 llist_for_each_entry(nse, &cfg->nse_peers, list) {
90 struct gbproxy_peer *peer;
91 llist_for_each_entry(peer, &nse->bts_peers, list) {
92 if (peer->bvci == bvci)
93 return peer;
94 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020095 }
96 return NULL;
97}
98
Daniel Willmanne50550e2020-11-26 18:19:21 +010099/* Find the gbproxy_peer by its NSEI */
100/* FIXME: Only returns the first peer, but we could have multiple on this nsei */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200101struct gbproxy_peer *gbproxy_peer_by_nsei(struct gbproxy_config *cfg,
102 uint16_t nsei)
103{
Daniel Willmanne50550e2020-11-26 18:19:21 +0100104 struct gbproxy_nse *nse;
105 llist_for_each_entry(nse, &cfg->nse_peers, list) {
106 if (nse->nsei == nsei && !llist_empty(&nse->bts_peers))
107 return llist_first_entry(&nse->bts_peers, struct gbproxy_peer, list);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200108 }
109 return NULL;
110}
111
112/* look-up a peer by its Routeing Area Identification (RAI) */
Harald Welte09ea4902020-11-24 09:27:38 +0100113/* FIXME: this doesn't make sense, as RA can span multiple peers! */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200114struct gbproxy_peer *gbproxy_peer_by_rai(struct gbproxy_config *cfg,
115 const uint8_t *ra)
116{
Daniel Willmanne50550e2020-11-26 18:19:21 +0100117 struct gbproxy_nse *nse;
118
119 llist_for_each_entry(nse, &cfg->nse_peers, list) {
120 struct gbproxy_peer *peer;
121 llist_for_each_entry(peer, &nse->bts_peers, list) {
122 if (!memcmp(peer->ra, ra, 6))
123 return peer;
124 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200125 }
Daniel Willmanne50550e2020-11-26 18:19:21 +0100126
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200127 return NULL;
128}
129
130/* look-up a peer by its Location Area Identification (LAI) */
Harald Welte09ea4902020-11-24 09:27:38 +0100131/* FIXME: this doesn't make sense, as LA can span multiple peers! */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200132struct gbproxy_peer *gbproxy_peer_by_lai(struct gbproxy_config *cfg,
133 const uint8_t *la)
134{
Daniel Willmanne50550e2020-11-26 18:19:21 +0100135 struct gbproxy_nse *nse;
136
137 llist_for_each_entry(nse, &cfg->nse_peers, list) {
138 struct gbproxy_peer *peer;
139 llist_for_each_entry(peer, &nse->bts_peers, list) {
140 if (!memcmp(peer->ra, la, 5))
141 return peer;
142 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200143 }
144 return NULL;
145}
146
147/* look-up a peer by its Location Area Code (LAC) */
Harald Welte09ea4902020-11-24 09:27:38 +0100148/* FIXME: this doesn't make sense, as LAC can span multiple peers! */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200149struct gbproxy_peer *gbproxy_peer_by_lac(struct gbproxy_config *cfg,
150 const uint8_t *la)
151{
Daniel Willmanne50550e2020-11-26 18:19:21 +0100152 struct gbproxy_nse *nse;
153
154 llist_for_each_entry(nse, &cfg->nse_peers, list) {
155 struct gbproxy_peer *peer;
156 llist_for_each_entry(peer, &nse->bts_peers, list) {
157 if (!memcmp(peer->ra + 3, la + 3, 2))
158 return peer;
159 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200160 }
161 return NULL;
162}
163
164struct gbproxy_peer *gbproxy_peer_by_bssgp_tlv(struct gbproxy_config *cfg,
165 struct tlv_parsed *tp)
166{
167 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
168 uint16_t bvci;
169
170 bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
171 if (bvci >= 2)
172 return gbproxy_peer_by_bvci(cfg, bvci);
173 }
174
Harald Welte09ea4902020-11-24 09:27:38 +0100175 /* FIXME: this doesn't make sense, as RA can span multiple peers! */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200176 if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
177 uint8_t *rai = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA);
178 /* Only compare LAC part, since MCC/MNC are possibly patched.
179 * Since the LAC of different BSS must be different when
180 * MCC/MNC are patched, collisions shouldn't happen. */
181 return gbproxy_peer_by_lac(cfg, rai);
182 }
183
Harald Welte09ea4902020-11-24 09:27:38 +0100184 /* FIXME: this doesn't make sense, as LA can span multiple peers! */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200185 if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
186 uint8_t *lai = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA);
187 return gbproxy_peer_by_lac(cfg, lai);
188 }
189
190 return NULL;
191}
192
193static void clean_stale_timer_cb(void *data)
194{
195 time_t now;
196 struct timespec ts = {0,};
197 struct gbproxy_peer *peer = (struct gbproxy_peer *) data;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100198 OSMO_ASSERT(peer);
199 OSMO_ASSERT(peer->nse);
200 struct gbproxy_config *cfg = peer->nse->cfg;
201 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200202
203 osmo_clock_gettime(CLOCK_MONOTONIC, &ts);
204 now = ts.tv_sec;
205 gbproxy_remove_stale_link_infos(peer, now);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100206 if (cfg->clean_stale_timer_freq != 0)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200207 osmo_timer_schedule(&peer->clean_stale_timer,
Daniel Willmanne50550e2020-11-26 18:19:21 +0100208 cfg->clean_stale_timer_freq, 0);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200209}
210
Daniel Willmanne50550e2020-11-26 18:19:21 +0100211struct gbproxy_peer *gbproxy_peer_alloc(struct gbproxy_nse *nse, uint16_t bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200212{
213 struct gbproxy_peer *peer;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100214 OSMO_ASSERT(nse);
215 struct gbproxy_config *cfg = nse->cfg;
216 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200217
218 peer = talloc_zero(tall_sgsn_ctx, struct gbproxy_peer);
219 if (!peer)
220 return NULL;
221
222 peer->bvci = bvci;
223 peer->ctrg = rate_ctr_group_alloc(peer, &peer_ctrg_desc, bvci);
224 if (!peer->ctrg) {
225 talloc_free(peer);
226 return NULL;
227 }
Daniel Willmanne50550e2020-11-26 18:19:21 +0100228 peer->nse = nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200229
Daniel Willmanne50550e2020-11-26 18:19:21 +0100230 llist_add(&peer->list, &nse->bts_peers);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200231
232 INIT_LLIST_HEAD(&peer->patch_state.logical_links);
233
234 osmo_timer_setup(&peer->clean_stale_timer, clean_stale_timer_cb, peer);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100235 if (cfg->clean_stale_timer_freq != 0)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200236 osmo_timer_schedule(&peer->clean_stale_timer,
Daniel Willmanne50550e2020-11-26 18:19:21 +0100237 cfg->clean_stale_timer_freq, 0);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200238
239 return peer;
240}
241
242void gbproxy_peer_free(struct gbproxy_peer *peer)
243{
Daniel Willmanne50550e2020-11-26 18:19:21 +0100244 OSMO_ASSERT(peer);
245
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200246 llist_del(&peer->list);
247 osmo_timer_del(&peer->clean_stale_timer);
248 gbproxy_delete_link_infos(peer);
249
250 rate_ctr_group_free(peer->ctrg);
251 peer->ctrg = NULL;
252
253 talloc_free(peer);
254}
255
256int gbproxy_cleanup_peers(struct gbproxy_config *cfg, uint16_t nsei, uint16_t bvci)
257{
258 int counter = 0;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100259 struct gbproxy_nse *nse, *ntmp;
260 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200261
Daniel Willmanne50550e2020-11-26 18:19:21 +0100262 llist_for_each_entry_safe(nse, ntmp, &cfg->nse_peers, list) {
263 struct gbproxy_peer *peer, *tmp;
264 if (nse->nsei != nsei)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200265 continue;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100266 llist_for_each_entry_safe(peer, tmp, &nse->bts_peers, list) {
267 if (bvci && peer->bvci != bvci)
268 continue;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200269
Daniel Willmanne50550e2020-11-26 18:19:21 +0100270 gbproxy_peer_free(peer);
271 counter += 1;
272 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200273 }
274
275 return counter;
276}
Daniel Willmanne50550e2020-11-26 18:19:21 +0100277
278struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei)
279{
280 struct gbproxy_nse *nse;
281 OSMO_ASSERT(cfg);
282
283 nse = talloc_zero(tall_sgsn_ctx, struct gbproxy_nse);
284 if (!nse)
285 return NULL;
286
287 nse->nsei = nsei;
288 nse->cfg = cfg;
289
290 llist_add(&nse->list, &cfg->nse_peers);
291
292 INIT_LLIST_HEAD(&nse->bts_peers);
293
294 return nse;
295}
296
297void gbproxy_nse_free(struct gbproxy_nse *nse)
298{
299 struct gbproxy_peer *peer, *tmp;
300 OSMO_ASSERT(nse);
301
302 llist_del(&nse->list);
303
304 llist_for_each_entry_safe(peer, tmp, &nse->bts_peers, list)
305 gbproxy_peer_free(peer);
306
307 talloc_free(nse);
308}
309
310struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei)
311{
312 struct gbproxy_nse *nse;
313 OSMO_ASSERT(cfg);
314
315 llist_for_each_entry(nse, &cfg->nse_peers, list) {
316 if (nse->nsei == nsei)
317 return nse;
318 }
319
320 return NULL;
321}
322
323struct gbproxy_nse *gbproxy_nse_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei)
324{
325 struct gbproxy_nse *nse;
326 OSMO_ASSERT(cfg);
327
328 nse = gbproxy_nse_by_nsei(cfg, nsei);
329 if (!nse)
330 nse = gbproxy_nse_alloc(cfg, nsei);
331
332 return nse;
333}