blob: 48482b6a1583bee7637597a0f73bd06f28183bd2 [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) */
108struct gbproxy_peer *gbproxy_peer_by_rai(struct gbproxy_config *cfg,
109 const uint8_t *ra)
110{
111 struct gbproxy_peer *peer;
112 llist_for_each_entry(peer, &cfg->bts_peers, list) {
113 if (!memcmp(peer->ra, ra, 6))
114 return peer;
115 }
116 return NULL;
117}
118
119/* look-up a peer by its Location Area Identification (LAI) */
120struct gbproxy_peer *gbproxy_peer_by_lai(struct gbproxy_config *cfg,
121 const uint8_t *la)
122{
123 struct gbproxy_peer *peer;
124 llist_for_each_entry(peer, &cfg->bts_peers, list) {
125 if (!memcmp(peer->ra, la, 5))
126 return peer;
127 }
128 return NULL;
129}
130
131/* look-up a peer by its Location Area Code (LAC) */
132struct gbproxy_peer *gbproxy_peer_by_lac(struct gbproxy_config *cfg,
133 const uint8_t *la)
134{
135 struct gbproxy_peer *peer;
136 llist_for_each_entry(peer, &cfg->bts_peers, list) {
137 if (!memcmp(peer->ra + 3, la + 3, 2))
138 return peer;
139 }
140 return NULL;
141}
142
143struct gbproxy_peer *gbproxy_peer_by_bssgp_tlv(struct gbproxy_config *cfg,
144 struct tlv_parsed *tp)
145{
146 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
147 uint16_t bvci;
148
149 bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
150 if (bvci >= 2)
151 return gbproxy_peer_by_bvci(cfg, bvci);
152 }
153
154 if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
155 uint8_t *rai = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA);
156 /* Only compare LAC part, since MCC/MNC are possibly patched.
157 * Since the LAC of different BSS must be different when
158 * MCC/MNC are patched, collisions shouldn't happen. */
159 return gbproxy_peer_by_lac(cfg, rai);
160 }
161
162 if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
163 uint8_t *lai = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA);
164 return gbproxy_peer_by_lac(cfg, lai);
165 }
166
167 return NULL;
168}
169
170static void clean_stale_timer_cb(void *data)
171{
172 time_t now;
173 struct timespec ts = {0,};
174 struct gbproxy_peer *peer = (struct gbproxy_peer *) data;
175
176 osmo_clock_gettime(CLOCK_MONOTONIC, &ts);
177 now = ts.tv_sec;
178 gbproxy_remove_stale_link_infos(peer, now);
179 if (peer->cfg->clean_stale_timer_freq != 0)
180 osmo_timer_schedule(&peer->clean_stale_timer,
181 peer->cfg->clean_stale_timer_freq, 0);
182}
183
184struct gbproxy_peer *gbproxy_peer_alloc(struct gbproxy_config *cfg, uint16_t bvci)
185{
186 struct gbproxy_peer *peer;
187
188 peer = talloc_zero(tall_sgsn_ctx, struct gbproxy_peer);
189 if (!peer)
190 return NULL;
191
192 peer->bvci = bvci;
193 peer->ctrg = rate_ctr_group_alloc(peer, &peer_ctrg_desc, bvci);
194 if (!peer->ctrg) {
195 talloc_free(peer);
196 return NULL;
197 }
198 peer->cfg = cfg;
199
200 llist_add(&peer->list, &cfg->bts_peers);
201
202 INIT_LLIST_HEAD(&peer->patch_state.logical_links);
203
204 osmo_timer_setup(&peer->clean_stale_timer, clean_stale_timer_cb, peer);
205 if (peer->cfg->clean_stale_timer_freq != 0)
206 osmo_timer_schedule(&peer->clean_stale_timer,
207 peer->cfg->clean_stale_timer_freq, 0);
208
209 return peer;
210}
211
212void gbproxy_peer_free(struct gbproxy_peer *peer)
213{
214 llist_del(&peer->list);
215 osmo_timer_del(&peer->clean_stale_timer);
216 gbproxy_delete_link_infos(peer);
217
218 rate_ctr_group_free(peer->ctrg);
219 peer->ctrg = NULL;
220
221 talloc_free(peer);
222}
223
224int gbproxy_cleanup_peers(struct gbproxy_config *cfg, uint16_t nsei, uint16_t bvci)
225{
226 int counter = 0;
227 struct gbproxy_peer *peer, *tmp;
228
229 llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list) {
230 if (peer->nsei != nsei)
231 continue;
232 if (bvci && peer->bvci != bvci)
233 continue;
234
235 gbproxy_peer_free(peer);
236 counter += 1;
237 }
238
239 return counter;
240}