Jacob Erlbeck | 040fabc | 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 | |
| 23 | #include <openbsc/gb_proxy.h> |
| 24 | |
| 25 | #include <openbsc/gsm_data.h> |
| 26 | #include <openbsc/gsm_data_shared.h> |
Jacob Erlbeck | 040fabc | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 27 | #include <openbsc/debug.h> |
| 28 | |
| 29 | #include <osmocom/gprs/protocol/gsm_08_18.h> |
| 30 | #include <osmocom/core/rate_ctr.h> |
Jacob Erlbeck | d31596b | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 31 | #include <osmocom/core/stats.h> |
Jacob Erlbeck | 040fabc | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 32 | #include <osmocom/core/talloc.h> |
| 33 | |
| 34 | #include <string.h> |
| 35 | |
| 36 | static const struct rate_ctr_desc peer_ctr_description[] = { |
| 37 | { "blocked", "BVC Block " }, |
| 38 | { "unblocked", "BVC Unblock " }, |
| 39 | { "dropped", "BVC blocked, dropped packet " }, |
| 40 | { "inv-nsei", "NSEI mismatch " }, |
| 41 | { "tx-err", "NS Transmission error " }, |
| 42 | { "raid-mod.bss", "RAID patched (BSS )" }, |
| 43 | { "raid-mod.sgsn", "RAID patched (SGSN)" }, |
| 44 | { "apn-mod.sgsn", "APN patched " }, |
| 45 | { "tlli-mod.bss", "TLLI patched (BSS )" }, |
| 46 | { "tlli-mod.sgsn", "TLLI patched (SGSN)" }, |
| 47 | { "ptmsi-mod.bss", "P-TMSI patched (BSS )" }, |
| 48 | { "ptmsi-mod.sgsn","P-TMSI patched (SGSN)" }, |
| 49 | { "mod-crypt-err", "Patch error: encrypted " }, |
| 50 | { "mod-err", "Patch error: other " }, |
| 51 | { "attach-reqs", "Attach Request count " }, |
| 52 | { "attach-rejs", "Attach Reject count " }, |
Holger Hans Peter Freyther | 055a994 | 2015-11-09 14:30:22 +0100 | [diff] [blame] | 53 | { "attach-acks", "Attach Accept count " }, |
| 54 | { "attach-cpls", "Attach Completed count " }, |
| 55 | { "ra-upd-reqs", "RoutingArea Update Request count" }, |
| 56 | { "ra-upd-rejs", "RoutingArea Update Reject count " }, |
| 57 | { "ra-upd-acks", "RoutingArea Update Accept count " }, |
| 58 | { "ra-upd-cpls", "RoutingArea Update Compltd count" }, |
| 59 | { "gmm-status", "GMM Status count (BSS)" }, |
| 60 | { "gmm-status", "GMM Status count (SGSN)" }, |
| 61 | { "detach-reqs", "Detach Request count " }, |
| 62 | { "detach-acks", "Detach Accept count " }, |
| 63 | { "pdp-act-reqs", "PDP Activation Request count " }, |
| 64 | { "pdp-act-rejs", "PDP Activation Reject count " }, |
| 65 | { "pdp-act-acks", "PDP Activation Accept count " }, |
| 66 | { "pdp-deact-reqs","PDP Deactivation Request count " }, |
| 67 | { "pdp-deact-acks","PDP Deactivation Accept count " }, |
Jacob Erlbeck | 040fabc | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 68 | { "tlli-unknown", "TLLI from SGSN unknown " }, |
| 69 | { "tlli-cache", "TLLI cache size " }, |
| 70 | }; |
| 71 | |
Holger Hans Peter Freyther | 055a994 | 2015-11-09 14:30:22 +0100 | [diff] [blame] | 72 | osmo_static_assert(ARRAY_SIZE(peer_ctr_description) == GBPROX_PEER_CTR_LAST, everything_described); |
| 73 | |
Jacob Erlbeck | 040fabc | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 74 | static const struct rate_ctr_group_desc peer_ctrg_desc = { |
| 75 | .group_name_prefix = "gbproxy.peer", |
| 76 | .group_description = "GBProxy Peer Statistics", |
| 77 | .num_ctr = ARRAY_SIZE(peer_ctr_description), |
| 78 | .ctr_desc = peer_ctr_description, |
Jacob Erlbeck | d31596b | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 79 | .class_id = OSMO_STATS_CLASS_PEER, |
Jacob Erlbeck | 040fabc | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | |
| 83 | /* Find the gbprox_peer by its BVCI */ |
| 84 | struct gbproxy_peer *gbproxy_peer_by_bvci(struct gbproxy_config *cfg, uint16_t bvci) |
| 85 | { |
| 86 | struct gbproxy_peer *peer; |
| 87 | llist_for_each_entry(peer, &cfg->bts_peers, list) { |
| 88 | if (peer->bvci == bvci) |
| 89 | return peer; |
| 90 | } |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | /* Find the gbprox_peer by its NSEI */ |
| 95 | struct gbproxy_peer *gbproxy_peer_by_nsei(struct gbproxy_config *cfg, |
| 96 | uint16_t nsei) |
| 97 | { |
| 98 | struct gbproxy_peer *peer; |
| 99 | llist_for_each_entry(peer, &cfg->bts_peers, list) { |
| 100 | if (peer->nsei == nsei) |
| 101 | return peer; |
| 102 | } |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | /* look-up a peer by its Routeing Area Identification (RAI) */ |
| 107 | struct gbproxy_peer *gbproxy_peer_by_rai(struct gbproxy_config *cfg, |
| 108 | const uint8_t *ra) |
| 109 | { |
| 110 | struct gbproxy_peer *peer; |
| 111 | llist_for_each_entry(peer, &cfg->bts_peers, list) { |
| 112 | if (!memcmp(peer->ra, ra, 6)) |
| 113 | return peer; |
| 114 | } |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | /* look-up a peer by its Location Area Identification (LAI) */ |
| 119 | struct gbproxy_peer *gbproxy_peer_by_lai(struct gbproxy_config *cfg, |
| 120 | const uint8_t *la) |
| 121 | { |
| 122 | struct gbproxy_peer *peer; |
| 123 | llist_for_each_entry(peer, &cfg->bts_peers, list) { |
| 124 | if (!memcmp(peer->ra, la, 5)) |
| 125 | return peer; |
| 126 | } |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | /* look-up a peer by its Location Area Code (LAC) */ |
| 131 | struct gbproxy_peer *gbproxy_peer_by_lac(struct gbproxy_config *cfg, |
| 132 | const uint8_t *la) |
| 133 | { |
| 134 | struct gbproxy_peer *peer; |
| 135 | llist_for_each_entry(peer, &cfg->bts_peers, list) { |
| 136 | if (!memcmp(peer->ra + 3, la + 3, 2)) |
| 137 | return peer; |
| 138 | } |
| 139 | return NULL; |
| 140 | } |
| 141 | |
| 142 | struct gbproxy_peer *gbproxy_peer_by_bssgp_tlv(struct gbproxy_config *cfg, |
| 143 | struct tlv_parsed *tp) |
| 144 | { |
| 145 | if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
| 146 | uint16_t bvci; |
| 147 | |
| 148 | bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
| 149 | if (bvci >= 2) |
| 150 | return gbproxy_peer_by_bvci(cfg, bvci); |
| 151 | } |
| 152 | |
| 153 | if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) { |
| 154 | uint8_t *rai = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA); |
| 155 | /* Only compare LAC part, since MCC/MNC are possibly patched. |
| 156 | * Since the LAC of different BSS must be different when |
| 157 | * MCC/MNC are patched, collisions shouldn't happen. */ |
| 158 | return gbproxy_peer_by_lac(cfg, rai); |
| 159 | } |
| 160 | |
| 161 | if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) { |
| 162 | uint8_t *lai = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA); |
| 163 | return gbproxy_peer_by_lac(cfg, lai); |
| 164 | } |
| 165 | |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | struct gbproxy_peer *gbproxy_peer_alloc(struct gbproxy_config *cfg, uint16_t bvci) |
| 171 | { |
| 172 | struct gbproxy_peer *peer; |
| 173 | |
| 174 | peer = talloc_zero(tall_bsc_ctx, struct gbproxy_peer); |
| 175 | if (!peer) |
| 176 | return NULL; |
| 177 | |
| 178 | peer->bvci = bvci; |
| 179 | peer->ctrg = rate_ctr_group_alloc(peer, &peer_ctrg_desc, bvci); |
| 180 | peer->cfg = cfg; |
| 181 | |
| 182 | llist_add(&peer->list, &cfg->bts_peers); |
| 183 | |
Jacob Erlbeck | 485e28c | 2014-09-19 16:03:07 +0200 | [diff] [blame] | 184 | INIT_LLIST_HEAD(&peer->patch_state.logical_links); |
Jacob Erlbeck | 040fabc | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 185 | |
| 186 | return peer; |
| 187 | } |
| 188 | |
| 189 | void gbproxy_peer_free(struct gbproxy_peer *peer) |
| 190 | { |
| 191 | llist_del(&peer->list); |
| 192 | |
Jacob Erlbeck | 2b2406a | 2014-09-19 15:07:27 +0200 | [diff] [blame] | 193 | gbproxy_delete_link_infos(peer); |
Jacob Erlbeck | 040fabc | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 194 | |
| 195 | rate_ctr_group_free(peer->ctrg); |
| 196 | peer->ctrg = NULL; |
| 197 | |
| 198 | talloc_free(peer); |
| 199 | } |
| 200 | |
| 201 | int gbproxy_cleanup_peers(struct gbproxy_config *cfg, uint16_t nsei, uint16_t bvci) |
| 202 | { |
| 203 | int counter = 0; |
| 204 | struct gbproxy_peer *peer, *tmp; |
| 205 | |
| 206 | llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list) { |
| 207 | if (peer->nsei != nsei) |
| 208 | continue; |
| 209 | if (bvci && peer->bvci != bvci) |
| 210 | continue; |
| 211 | |
| 212 | gbproxy_peer_free(peer); |
| 213 | counter += 1; |
| 214 | } |
| 215 | |
| 216 | return counter; |
| 217 | } |
| 218 | |