Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 1 | /* 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 | |
Neels Hofmeyr | 396f2e6 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 23 | #include <osmocom/sgsn/gb_proxy.h> |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 24 | |
Neels Hofmeyr | 396f2e6 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 25 | #include <osmocom/sgsn/debug.h> |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 26 | |
| 27 | #include <osmocom/gprs/protocol/gsm_08_18.h> |
Daniel Willmann | e8c8ec9 | 2020-12-02 19:33:50 +0100 | [diff] [blame^] | 28 | #include <osmocom/core/logging.h> |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 29 | #include <osmocom/core/rate_ctr.h> |
Jacob Erlbeck | 46caed8 | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 30 | #include <osmocom/core/stats.h> |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 31 | #include <osmocom/core/talloc.h> |
Neels Hofmeyr | ee6cfdc | 2017-07-13 02:03:50 +0200 | [diff] [blame] | 32 | #include <osmocom/gsm/tlv.h> |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 33 | |
| 34 | #include <string.h> |
| 35 | |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 36 | extern void *tall_sgsn_ctx; |
Neels Hofmeyr | ee6cfdc | 2017-07-13 02:03:50 +0200 | [diff] [blame] | 37 | |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 38 | static const struct rate_ctr_desc peer_ctr_description[] = { |
| 39 | { "blocked", "BVC Block " }, |
| 40 | { "unblocked", "BVC Unblock " }, |
| 41 | { "dropped", "BVC blocked, dropped packet " }, |
| 42 | { "inv-nsei", "NSEI mismatch " }, |
| 43 | { "tx-err", "NS Transmission error " }, |
Pau Espin Pedrol | c19817b | 2018-08-20 19:57:42 +0200 | [diff] [blame] | 44 | { "raid-mod:bss", "RAID patched (BSS )" }, |
| 45 | { "raid-mod:sgsn", "RAID patched (SGSN)" }, |
| 46 | { "apn-mod:sgsn", "APN patched " }, |
| 47 | { "tlli-mod:bss", "TLLI patched (BSS )" }, |
| 48 | { "tlli-mod:sgsn", "TLLI patched (SGSN)" }, |
| 49 | { "ptmsi-mod:bss", "P-TMSI patched (BSS )" }, |
| 50 | { "ptmsi-mod:sgsn","P-TMSI patched (SGSN)" }, |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 51 | { "mod-crypt-err", "Patch error: encrypted " }, |
| 52 | { "mod-err", "Patch error: other " }, |
| 53 | { "attach-reqs", "Attach Request count " }, |
| 54 | { "attach-rejs", "Attach Reject count " }, |
Holger Hans Peter Freyther | 98fa3dc | 2015-11-09 14:30:22 +0100 | [diff] [blame] | 55 | { "attach-acks", "Attach Accept count " }, |
| 56 | { "attach-cpls", "Attach Completed count " }, |
| 57 | { "ra-upd-reqs", "RoutingArea Update Request count" }, |
| 58 | { "ra-upd-rejs", "RoutingArea Update Reject count " }, |
| 59 | { "ra-upd-acks", "RoutingArea Update Accept count " }, |
| 60 | { "ra-upd-cpls", "RoutingArea Update Compltd count" }, |
| 61 | { "gmm-status", "GMM Status count (BSS)" }, |
| 62 | { "gmm-status", "GMM Status count (SGSN)" }, |
| 63 | { "detach-reqs", "Detach Request count " }, |
| 64 | { "detach-acks", "Detach Accept count " }, |
| 65 | { "pdp-act-reqs", "PDP Activation Request count " }, |
| 66 | { "pdp-act-rejs", "PDP Activation Reject count " }, |
| 67 | { "pdp-act-acks", "PDP Activation Accept count " }, |
| 68 | { "pdp-deact-reqs","PDP Deactivation Request count " }, |
| 69 | { "pdp-deact-acks","PDP Deactivation Accept count " }, |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 70 | { "tlli-unknown", "TLLI from SGSN unknown " }, |
| 71 | { "tlli-cache", "TLLI cache size " }, |
| 72 | }; |
| 73 | |
Holger Hans Peter Freyther | 98fa3dc | 2015-11-09 14:30:22 +0100 | [diff] [blame] | 74 | osmo_static_assert(ARRAY_SIZE(peer_ctr_description) == GBPROX_PEER_CTR_LAST, everything_described); |
| 75 | |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 76 | static const struct rate_ctr_group_desc peer_ctrg_desc = { |
Max | 8a01a80 | 2017-12-20 13:10:11 +0100 | [diff] [blame] | 77 | .group_name_prefix = "gbproxy:peer", |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 78 | .group_description = "GBProxy Peer Statistics", |
| 79 | .num_ctr = ARRAY_SIZE(peer_ctr_description), |
| 80 | .ctr_desc = peer_ctr_description, |
Jacob Erlbeck | 46caed8 | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 81 | .class_id = OSMO_STATS_CLASS_PEER, |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 85 | /* Find the gbproxy_peer by its BVCI. There can only be one match */ |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 86 | struct gbproxy_peer *gbproxy_peer_by_bvci(struct gbproxy_config *cfg, uint16_t bvci) |
| 87 | { |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 88 | struct gbproxy_nse *nse; |
| 89 | |
| 90 | llist_for_each_entry(nse, &cfg->nse_peers, list) { |
| 91 | struct gbproxy_peer *peer; |
| 92 | llist_for_each_entry(peer, &nse->bts_peers, list) { |
| 93 | if (peer->bvci == bvci) |
| 94 | return peer; |
| 95 | } |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 96 | } |
| 97 | return NULL; |
| 98 | } |
| 99 | |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 100 | /* Find the gbproxy_peer by its NSEI */ |
| 101 | /* FIXME: Only returns the first peer, but we could have multiple on this nsei */ |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 102 | struct gbproxy_peer *gbproxy_peer_by_nsei(struct gbproxy_config *cfg, |
| 103 | uint16_t nsei) |
| 104 | { |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 105 | struct gbproxy_nse *nse; |
| 106 | llist_for_each_entry(nse, &cfg->nse_peers, list) { |
| 107 | if (nse->nsei == nsei && !llist_empty(&nse->bts_peers)) |
| 108 | return llist_first_entry(&nse->bts_peers, struct gbproxy_peer, list); |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 109 | } |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | /* look-up a peer by its Routeing Area Identification (RAI) */ |
Harald Welte | eb4233e | 2020-11-24 09:27:38 +0100 | [diff] [blame] | 114 | /* FIXME: this doesn't make sense, as RA can span multiple peers! */ |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 115 | struct gbproxy_peer *gbproxy_peer_by_rai(struct gbproxy_config *cfg, |
| 116 | const uint8_t *ra) |
| 117 | { |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 118 | struct gbproxy_nse *nse; |
| 119 | |
| 120 | llist_for_each_entry(nse, &cfg->nse_peers, list) { |
| 121 | struct gbproxy_peer *peer; |
| 122 | llist_for_each_entry(peer, &nse->bts_peers, list) { |
| 123 | if (!memcmp(peer->ra, ra, 6)) |
| 124 | return peer; |
| 125 | } |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 126 | } |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 127 | |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 128 | return NULL; |
| 129 | } |
| 130 | |
| 131 | /* look-up a peer by its Location Area Identification (LAI) */ |
Harald Welte | eb4233e | 2020-11-24 09:27:38 +0100 | [diff] [blame] | 132 | /* FIXME: this doesn't make sense, as LA can span multiple peers! */ |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 133 | struct gbproxy_peer *gbproxy_peer_by_lai(struct gbproxy_config *cfg, |
| 134 | const uint8_t *la) |
| 135 | { |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 136 | struct gbproxy_nse *nse; |
| 137 | |
| 138 | llist_for_each_entry(nse, &cfg->nse_peers, list) { |
| 139 | struct gbproxy_peer *peer; |
| 140 | llist_for_each_entry(peer, &nse->bts_peers, list) { |
| 141 | if (!memcmp(peer->ra, la, 5)) |
| 142 | return peer; |
| 143 | } |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 144 | } |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | /* look-up a peer by its Location Area Code (LAC) */ |
Harald Welte | eb4233e | 2020-11-24 09:27:38 +0100 | [diff] [blame] | 149 | /* FIXME: this doesn't make sense, as LAC can span multiple peers! */ |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 150 | struct gbproxy_peer *gbproxy_peer_by_lac(struct gbproxy_config *cfg, |
| 151 | const uint8_t *la) |
| 152 | { |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 153 | struct gbproxy_nse *nse; |
| 154 | |
| 155 | llist_for_each_entry(nse, &cfg->nse_peers, list) { |
| 156 | struct gbproxy_peer *peer; |
| 157 | llist_for_each_entry(peer, &nse->bts_peers, list) { |
| 158 | if (!memcmp(peer->ra + 3, la + 3, 2)) |
| 159 | return peer; |
| 160 | } |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 161 | } |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | struct gbproxy_peer *gbproxy_peer_by_bssgp_tlv(struct gbproxy_config *cfg, |
| 166 | struct tlv_parsed *tp) |
| 167 | { |
| 168 | if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
| 169 | uint16_t bvci; |
| 170 | |
| 171 | bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
| 172 | if (bvci >= 2) |
| 173 | return gbproxy_peer_by_bvci(cfg, bvci); |
| 174 | } |
| 175 | |
Harald Welte | eb4233e | 2020-11-24 09:27:38 +0100 | [diff] [blame] | 176 | /* FIXME: this doesn't make sense, as RA can span multiple peers! */ |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 177 | if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) { |
| 178 | uint8_t *rai = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA); |
| 179 | /* Only compare LAC part, since MCC/MNC are possibly patched. |
| 180 | * Since the LAC of different BSS must be different when |
| 181 | * MCC/MNC are patched, collisions shouldn't happen. */ |
| 182 | return gbproxy_peer_by_lac(cfg, rai); |
| 183 | } |
| 184 | |
Harald Welte | eb4233e | 2020-11-24 09:27:38 +0100 | [diff] [blame] | 185 | /* FIXME: this doesn't make sense, as LA can span multiple peers! */ |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 186 | if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) { |
| 187 | uint8_t *lai = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA); |
| 188 | return gbproxy_peer_by_lac(cfg, lai); |
| 189 | } |
| 190 | |
| 191 | return NULL; |
| 192 | } |
| 193 | |
Pau Espin Pedrol | 82f1361 | 2018-08-17 13:13:27 +0200 | [diff] [blame] | 194 | static void clean_stale_timer_cb(void *data) |
| 195 | { |
| 196 | time_t now; |
| 197 | struct timespec ts = {0,}; |
| 198 | struct gbproxy_peer *peer = (struct gbproxy_peer *) data; |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 199 | OSMO_ASSERT(peer); |
| 200 | OSMO_ASSERT(peer->nse); |
| 201 | struct gbproxy_config *cfg = peer->nse->cfg; |
| 202 | OSMO_ASSERT(cfg); |
Pau Espin Pedrol | 82f1361 | 2018-08-17 13:13:27 +0200 | [diff] [blame] | 203 | |
| 204 | osmo_clock_gettime(CLOCK_MONOTONIC, &ts); |
| 205 | now = ts.tv_sec; |
| 206 | gbproxy_remove_stale_link_infos(peer, now); |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 207 | if (cfg->clean_stale_timer_freq != 0) |
Pau Espin Pedrol | 82f1361 | 2018-08-17 13:13:27 +0200 | [diff] [blame] | 208 | osmo_timer_schedule(&peer->clean_stale_timer, |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 209 | cfg->clean_stale_timer_freq, 0); |
Pau Espin Pedrol | 82f1361 | 2018-08-17 13:13:27 +0200 | [diff] [blame] | 210 | } |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 211 | |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 212 | struct gbproxy_peer *gbproxy_peer_alloc(struct gbproxy_nse *nse, uint16_t bvci) |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 213 | { |
| 214 | struct gbproxy_peer *peer; |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 215 | OSMO_ASSERT(nse); |
| 216 | struct gbproxy_config *cfg = nse->cfg; |
| 217 | OSMO_ASSERT(cfg); |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 218 | |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 219 | peer = talloc_zero(tall_sgsn_ctx, struct gbproxy_peer); |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 220 | if (!peer) |
| 221 | return NULL; |
| 222 | |
| 223 | peer->bvci = bvci; |
| 224 | peer->ctrg = rate_ctr_group_alloc(peer, &peer_ctrg_desc, bvci); |
Harald Welte | 26c1465 | 2017-07-12 00:25:51 +0200 | [diff] [blame] | 225 | if (!peer->ctrg) { |
| 226 | talloc_free(peer); |
| 227 | return NULL; |
| 228 | } |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 229 | peer->nse = nse; |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 230 | |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 231 | llist_add(&peer->list, &nse->bts_peers); |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 232 | |
Jacob Erlbeck | f8562e3 | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 233 | INIT_LLIST_HEAD(&peer->patch_state.logical_links); |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 234 | |
Pau Espin Pedrol | 82f1361 | 2018-08-17 13:13:27 +0200 | [diff] [blame] | 235 | osmo_timer_setup(&peer->clean_stale_timer, clean_stale_timer_cb, peer); |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 236 | if (cfg->clean_stale_timer_freq != 0) |
Pau Espin Pedrol | 82f1361 | 2018-08-17 13:13:27 +0200 | [diff] [blame] | 237 | osmo_timer_schedule(&peer->clean_stale_timer, |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 238 | cfg->clean_stale_timer_freq, 0); |
Pau Espin Pedrol | 82f1361 | 2018-08-17 13:13:27 +0200 | [diff] [blame] | 239 | |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 240 | return peer; |
| 241 | } |
| 242 | |
| 243 | void gbproxy_peer_free(struct gbproxy_peer *peer) |
| 244 | { |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 245 | OSMO_ASSERT(peer); |
| 246 | |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 247 | llist_del(&peer->list); |
Pau Espin Pedrol | 82f1361 | 2018-08-17 13:13:27 +0200 | [diff] [blame] | 248 | osmo_timer_del(&peer->clean_stale_timer); |
Jacob Erlbeck | 91d2f8a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 249 | gbproxy_delete_link_infos(peer); |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 250 | |
| 251 | rate_ctr_group_free(peer->ctrg); |
| 252 | peer->ctrg = NULL; |
| 253 | |
| 254 | talloc_free(peer); |
| 255 | } |
| 256 | |
Daniel Willmann | 9e583c8 | 2020-11-30 17:14:24 +0100 | [diff] [blame] | 257 | void gbproxy_peer_move(struct gbproxy_peer *peer, struct gbproxy_nse *nse) |
| 258 | { |
| 259 | llist_del(&peer->list); |
| 260 | llist_add(&peer->list, &nse->bts_peers); |
| 261 | peer->nse = nse; |
| 262 | } |
| 263 | |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 264 | int gbproxy_cleanup_peers(struct gbproxy_config *cfg, uint16_t nsei, uint16_t bvci) |
| 265 | { |
| 266 | int counter = 0; |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 267 | struct gbproxy_nse *nse, *ntmp; |
| 268 | OSMO_ASSERT(cfg); |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 269 | |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 270 | llist_for_each_entry_safe(nse, ntmp, &cfg->nse_peers, list) { |
| 271 | struct gbproxy_peer *peer, *tmp; |
| 272 | if (nse->nsei != nsei) |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 273 | continue; |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 274 | llist_for_each_entry_safe(peer, tmp, &nse->bts_peers, list) { |
| 275 | if (bvci && peer->bvci != bvci) |
| 276 | continue; |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 277 | |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 278 | gbproxy_peer_free(peer); |
| 279 | counter += 1; |
| 280 | } |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | return counter; |
| 284 | } |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 285 | |
| 286 | struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei) |
| 287 | { |
| 288 | struct gbproxy_nse *nse; |
| 289 | OSMO_ASSERT(cfg); |
| 290 | |
| 291 | nse = talloc_zero(tall_sgsn_ctx, struct gbproxy_nse); |
| 292 | if (!nse) |
| 293 | return NULL; |
| 294 | |
| 295 | nse->nsei = nsei; |
| 296 | nse->cfg = cfg; |
| 297 | |
| 298 | llist_add(&nse->list, &cfg->nse_peers); |
| 299 | |
| 300 | INIT_LLIST_HEAD(&nse->bts_peers); |
| 301 | |
| 302 | return nse; |
| 303 | } |
| 304 | |
| 305 | void gbproxy_nse_free(struct gbproxy_nse *nse) |
| 306 | { |
| 307 | struct gbproxy_peer *peer, *tmp; |
| 308 | OSMO_ASSERT(nse); |
| 309 | |
| 310 | llist_del(&nse->list); |
| 311 | |
| 312 | llist_for_each_entry_safe(peer, tmp, &nse->bts_peers, list) |
| 313 | gbproxy_peer_free(peer); |
| 314 | |
| 315 | talloc_free(nse); |
| 316 | } |
| 317 | |
| 318 | struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei) |
| 319 | { |
| 320 | struct gbproxy_nse *nse; |
| 321 | OSMO_ASSERT(cfg); |
| 322 | |
| 323 | llist_for_each_entry(nse, &cfg->nse_peers, list) { |
| 324 | if (nse->nsei == nsei) |
| 325 | return nse; |
| 326 | } |
| 327 | |
| 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | struct gbproxy_nse *gbproxy_nse_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei) |
| 332 | { |
| 333 | struct gbproxy_nse *nse; |
| 334 | OSMO_ASSERT(cfg); |
| 335 | |
| 336 | nse = gbproxy_nse_by_nsei(cfg, nsei); |
| 337 | if (!nse) |
| 338 | nse = gbproxy_nse_alloc(cfg, nsei); |
| 339 | |
| 340 | return nse; |
| 341 | } |