blob: cb76a5927fce1a70e1a3a94a7a22dbcd0988b194 [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
84/* Find the gbprox_peer by its BVCI */
85struct gbproxy_peer *gbproxy_peer_by_bvci(struct gbproxy_config *cfg, uint16_t bvci)
86{
87 struct gbproxy_peer *peer;
88 llist_for_each_entry(peer, &cfg->bts_peers, list) {
89 if (peer->bvci == bvci)
90 return peer;
91 }
92 return NULL;
93}
94
95/* Find the gbprox_peer by its NSEI */
96struct gbproxy_peer *gbproxy_peer_by_nsei(struct gbproxy_config *cfg,
97 uint16_t nsei)
98{
99 struct gbproxy_peer *peer;
100 llist_for_each_entry(peer, &cfg->bts_peers, list) {
101 if (peer->nsei == nsei)
102 return peer;
103 }
104 return NULL;
105}
106
107/* look-up a peer by its Routeing Area Identification (RAI) */
Harald Welte09ea4902020-11-24 09:27:38 +0100108/* FIXME: this doesn't make sense, as RA can span multiple peers! */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200109struct gbproxy_peer *gbproxy_peer_by_rai(struct gbproxy_config *cfg,
110 const uint8_t *ra)
111{
112 struct gbproxy_peer *peer;
113 llist_for_each_entry(peer, &cfg->bts_peers, list) {
114 if (!memcmp(peer->ra, ra, 6))
115 return peer;
116 }
117 return NULL;
118}
119
120/* look-up a peer by its Location Area Identification (LAI) */
Harald Welte09ea4902020-11-24 09:27:38 +0100121/* FIXME: this doesn't make sense, as LA can span multiple peers! */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200122struct gbproxy_peer *gbproxy_peer_by_lai(struct gbproxy_config *cfg,
123 const uint8_t *la)
124{
125 struct gbproxy_peer *peer;
126 llist_for_each_entry(peer, &cfg->bts_peers, list) {
127 if (!memcmp(peer->ra, la, 5))
128 return peer;
129 }
130 return NULL;
131}
132
133/* look-up a peer by its Location Area Code (LAC) */
Harald Welte09ea4902020-11-24 09:27:38 +0100134/* FIXME: this doesn't make sense, as LAC can span multiple peers! */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200135struct gbproxy_peer *gbproxy_peer_by_lac(struct gbproxy_config *cfg,
136 const uint8_t *la)
137{
138 struct gbproxy_peer *peer;
139 llist_for_each_entry(peer, &cfg->bts_peers, list) {
140 if (!memcmp(peer->ra + 3, la + 3, 2))
141 return peer;
142 }
143 return NULL;
144}
145
146struct gbproxy_peer *gbproxy_peer_by_bssgp_tlv(struct gbproxy_config *cfg,
147 struct tlv_parsed *tp)
148{
149 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
150 uint16_t bvci;
151
152 bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
153 if (bvci >= 2)
154 return gbproxy_peer_by_bvci(cfg, bvci);
155 }
156
Harald Welte09ea4902020-11-24 09:27:38 +0100157 /* FIXME: this doesn't make sense, as RA can span multiple peers! */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200158 if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
159 uint8_t *rai = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA);
160 /* Only compare LAC part, since MCC/MNC are possibly patched.
161 * Since the LAC of different BSS must be different when
162 * MCC/MNC are patched, collisions shouldn't happen. */
163 return gbproxy_peer_by_lac(cfg, rai);
164 }
165
Harald Welte09ea4902020-11-24 09:27:38 +0100166 /* FIXME: this doesn't make sense, as LA can span multiple peers! */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200167 if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
168 uint8_t *lai = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA);
169 return gbproxy_peer_by_lac(cfg, lai);
170 }
171
172 return NULL;
173}
174
175static void clean_stale_timer_cb(void *data)
176{
177 time_t now;
178 struct timespec ts = {0,};
179 struct gbproxy_peer *peer = (struct gbproxy_peer *) data;
180
181 osmo_clock_gettime(CLOCK_MONOTONIC, &ts);
182 now = ts.tv_sec;
183 gbproxy_remove_stale_link_infos(peer, now);
184 if (peer->cfg->clean_stale_timer_freq != 0)
185 osmo_timer_schedule(&peer->clean_stale_timer,
186 peer->cfg->clean_stale_timer_freq, 0);
187}
188
189struct gbproxy_peer *gbproxy_peer_alloc(struct gbproxy_config *cfg, uint16_t bvci)
190{
191 struct gbproxy_peer *peer;
192
193 peer = talloc_zero(tall_sgsn_ctx, struct gbproxy_peer);
194 if (!peer)
195 return NULL;
196
197 peer->bvci = bvci;
198 peer->ctrg = rate_ctr_group_alloc(peer, &peer_ctrg_desc, bvci);
199 if (!peer->ctrg) {
200 talloc_free(peer);
201 return NULL;
202 }
203 peer->cfg = cfg;
204
205 llist_add(&peer->list, &cfg->bts_peers);
206
207 INIT_LLIST_HEAD(&peer->patch_state.logical_links);
208
209 osmo_timer_setup(&peer->clean_stale_timer, clean_stale_timer_cb, peer);
210 if (peer->cfg->clean_stale_timer_freq != 0)
211 osmo_timer_schedule(&peer->clean_stale_timer,
212 peer->cfg->clean_stale_timer_freq, 0);
213
214 return peer;
215}
216
217void gbproxy_peer_free(struct gbproxy_peer *peer)
218{
219 llist_del(&peer->list);
220 osmo_timer_del(&peer->clean_stale_timer);
221 gbproxy_delete_link_infos(peer);
222
223 rate_ctr_group_free(peer->ctrg);
224 peer->ctrg = NULL;
225
226 talloc_free(peer);
227}
228
229int gbproxy_cleanup_peers(struct gbproxy_config *cfg, uint16_t nsei, uint16_t bvci)
230{
231 int counter = 0;
232 struct gbproxy_peer *peer, *tmp;
233
234 llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list) {
235 if (peer->nsei != nsei)
236 continue;
237 if (bvci && peer->bvci != bvci)
238 continue;
239
240 gbproxy_peer_free(peer);
241 counter += 1;
242 }
243
244 return counter;
245}