blob: a83630bc1409fe1a3d91df536e82c76159b7bc18 [file] [log] [blame]
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +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 <openbsc/gb_proxy.h>
24
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +020025#include <openbsc/debug.h>
26
27#include <osmocom/gprs/protocol/gsm_08_18.h>
28#include <osmocom/core/rate_ctr.h>
Jacob Erlbeck46caed82015-11-02 15:15:38 +010029#include <osmocom/core/stats.h>
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +020030#include <osmocom/core/talloc.h>
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020031#include <osmocom/gsm/tlv.h>
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +020032
33#include <string.h>
34
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020035extern void *tall_bsc_ctx;
36
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +020037static 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 " },
Holger Hans Peter Freyther98fa3dc2015-11-09 14:30:22 +010054 { "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 " },
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +020069 { "tlli-unknown", "TLLI from SGSN unknown " },
70 { "tlli-cache", "TLLI cache size " },
71};
72
Holger Hans Peter Freyther98fa3dc2015-11-09 14:30:22 +010073osmo_static_assert(ARRAY_SIZE(peer_ctr_description) == GBPROX_PEER_CTR_LAST, everything_described);
74
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +020075static 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,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010080 .class_id = OSMO_STATS_CLASS_PEER,
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +020081};
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
170
171struct gbproxy_peer *gbproxy_peer_alloc(struct gbproxy_config *cfg, uint16_t bvci)
172{
173 struct gbproxy_peer *peer;
174
175 peer = talloc_zero(tall_bsc_ctx, struct gbproxy_peer);
176 if (!peer)
177 return NULL;
178
179 peer->bvci = bvci;
180 peer->ctrg = rate_ctr_group_alloc(peer, &peer_ctrg_desc, bvci);
Harald Welte26c14652017-07-12 00:25:51 +0200181 if (!peer->ctrg) {
182 talloc_free(peer);
183 return NULL;
184 }
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200185 peer->cfg = cfg;
186
187 llist_add(&peer->list, &cfg->bts_peers);
188
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200189 INIT_LLIST_HEAD(&peer->patch_state.logical_links);
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200190
191 return peer;
192}
193
194void gbproxy_peer_free(struct gbproxy_peer *peer)
195{
196 llist_del(&peer->list);
197
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200198 gbproxy_delete_link_infos(peer);
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200199
200 rate_ctr_group_free(peer->ctrg);
201 peer->ctrg = NULL;
202
203 talloc_free(peer);
204}
205
206int gbproxy_cleanup_peers(struct gbproxy_config *cfg, uint16_t nsei, uint16_t bvci)
207{
208 int counter = 0;
209 struct gbproxy_peer *peer, *tmp;
210
211 llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list) {
212 if (peer->nsei != nsei)
213 continue;
214 if (bvci && peer->bvci != bvci)
215 continue;
216
217 gbproxy_peer_free(peer);
218 counter += 1;
219 }
220
221 return counter;
222}
223