blob: b65fc178d3c7afd5613e36b0af76e890c54ee760 [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
25#include <openbsc/gsm_data.h>
26#include <openbsc/gsm_data_shared.h>
27#include <openbsc/gsm_04_08_gprs.h>
28#include <openbsc/debug.h>
29
30#include <osmocom/gprs/protocol/gsm_08_18.h>
31#include <osmocom/core/rate_ctr.h>
Jacob Erlbeck46caed82015-11-02 15:15:38 +010032#include <osmocom/core/stats.h>
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +020033#include <osmocom/core/talloc.h>
34
35#include <string.h>
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 { "tlli-unknown", "TLLI from SGSN unknown " },
55 { "tlli-cache", "TLLI cache size " },
56};
57
58static const struct rate_ctr_group_desc peer_ctrg_desc = {
59 .group_name_prefix = "gbproxy.peer",
60 .group_description = "GBProxy Peer Statistics",
61 .num_ctr = ARRAY_SIZE(peer_ctr_description),
62 .ctr_desc = peer_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010063 .class_id = OSMO_STATS_CLASS_PEER,
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +020064};
65
66
67/* Find the gbprox_peer by its BVCI */
68struct gbproxy_peer *gbproxy_peer_by_bvci(struct gbproxy_config *cfg, uint16_t bvci)
69{
70 struct gbproxy_peer *peer;
71 llist_for_each_entry(peer, &cfg->bts_peers, list) {
72 if (peer->bvci == bvci)
73 return peer;
74 }
75 return NULL;
76}
77
78/* Find the gbprox_peer by its NSEI */
79struct gbproxy_peer *gbproxy_peer_by_nsei(struct gbproxy_config *cfg,
80 uint16_t nsei)
81{
82 struct gbproxy_peer *peer;
83 llist_for_each_entry(peer, &cfg->bts_peers, list) {
84 if (peer->nsei == nsei)
85 return peer;
86 }
87 return NULL;
88}
89
90/* look-up a peer by its Routeing Area Identification (RAI) */
91struct gbproxy_peer *gbproxy_peer_by_rai(struct gbproxy_config *cfg,
92 const uint8_t *ra)
93{
94 struct gbproxy_peer *peer;
95 llist_for_each_entry(peer, &cfg->bts_peers, list) {
96 if (!memcmp(peer->ra, ra, 6))
97 return peer;
98 }
99 return NULL;
100}
101
102/* look-up a peer by its Location Area Identification (LAI) */
103struct gbproxy_peer *gbproxy_peer_by_lai(struct gbproxy_config *cfg,
104 const uint8_t *la)
105{
106 struct gbproxy_peer *peer;
107 llist_for_each_entry(peer, &cfg->bts_peers, list) {
108 if (!memcmp(peer->ra, la, 5))
109 return peer;
110 }
111 return NULL;
112}
113
114/* look-up a peer by its Location Area Code (LAC) */
115struct gbproxy_peer *gbproxy_peer_by_lac(struct gbproxy_config *cfg,
116 const uint8_t *la)
117{
118 struct gbproxy_peer *peer;
119 llist_for_each_entry(peer, &cfg->bts_peers, list) {
120 if (!memcmp(peer->ra + 3, la + 3, 2))
121 return peer;
122 }
123 return NULL;
124}
125
126struct gbproxy_peer *gbproxy_peer_by_bssgp_tlv(struct gbproxy_config *cfg,
127 struct tlv_parsed *tp)
128{
129 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
130 uint16_t bvci;
131
132 bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
133 if (bvci >= 2)
134 return gbproxy_peer_by_bvci(cfg, bvci);
135 }
136
137 if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
138 uint8_t *rai = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA);
139 /* Only compare LAC part, since MCC/MNC are possibly patched.
140 * Since the LAC of different BSS must be different when
141 * MCC/MNC are patched, collisions shouldn't happen. */
142 return gbproxy_peer_by_lac(cfg, rai);
143 }
144
145 if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
146 uint8_t *lai = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA);
147 return gbproxy_peer_by_lac(cfg, lai);
148 }
149
150 return NULL;
151}
152
153
154struct gbproxy_peer *gbproxy_peer_alloc(struct gbproxy_config *cfg, uint16_t bvci)
155{
156 struct gbproxy_peer *peer;
157
158 peer = talloc_zero(tall_bsc_ctx, struct gbproxy_peer);
159 if (!peer)
160 return NULL;
161
162 peer->bvci = bvci;
163 peer->ctrg = rate_ctr_group_alloc(peer, &peer_ctrg_desc, bvci);
164 peer->cfg = cfg;
165
166 llist_add(&peer->list, &cfg->bts_peers);
167
Jacob Erlbeckf8562e32014-09-19 16:03:07 +0200168 INIT_LLIST_HEAD(&peer->patch_state.logical_links);
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200169
170 return peer;
171}
172
173void gbproxy_peer_free(struct gbproxy_peer *peer)
174{
175 llist_del(&peer->list);
176
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200177 gbproxy_delete_link_infos(peer);
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200178
179 rate_ctr_group_free(peer->ctrg);
180 peer->ctrg = NULL;
181
182 talloc_free(peer);
183}
184
185int gbproxy_cleanup_peers(struct gbproxy_config *cfg, uint16_t nsei, uint16_t bvci)
186{
187 int counter = 0;
188 struct gbproxy_peer *peer, *tmp;
189
190 llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list) {
191 if (peer->nsei != nsei)
192 continue;
193 if (bvci && peer->bvci != bvci)
194 continue;
195
196 gbproxy_peer_free(peer);
197 counter += 1;
198 }
199
200 return counter;
201}
202