Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1 | /* NS-over-IP proxy */ |
| 2 | |
| 3 | /* (C) 2010 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 4 | * (C) 2010-2013 by On-Waves |
| 5 | * (C) 2013 by Holger Hans Peter Freyther |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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 |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 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 |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 17 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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/>. |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <unistd.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <getopt.h> |
| 28 | #include <errno.h> |
| 29 | #include <sys/fcntl.h> |
| 30 | #include <sys/stat.h> |
Harald Welte | 7fc9822 | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 31 | #include <arpa/inet.h> |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 32 | #include <time.h> |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 33 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 34 | #include <osmocom/core/talloc.h> |
| 35 | #include <osmocom/core/select.h> |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 36 | #include <osmocom/core/rate_ctr.h> |
| 37 | |
| 38 | #include <osmocom/vty/misc.h> |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 39 | |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 40 | #include <osmocom/gprs/gprs_ns.h> |
| 41 | #include <osmocom/gprs/gprs_bssgp.h> |
| 42 | |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 43 | #include <osmocom/gsm/gsm_utils.h> |
| 44 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 45 | #include <openbsc/signal.h> |
| 46 | #include <openbsc/debug.h> |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 47 | #include <openbsc/gb_proxy.h> |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 48 | |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 49 | #include <openbsc/gprs_llc.h> |
| 50 | #include <openbsc/gsm_04_08.h> |
| 51 | #include <openbsc/gsm_04_08_gprs.h> |
| 52 | |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 53 | enum gbprox_global_ctr { |
| 54 | GBPROX_GLOB_CTR_INV_BVCI, |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 55 | GBPROX_GLOB_CTR_INV_LAI, |
| 56 | GBPROX_GLOB_CTR_INV_RAI, |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 57 | GBPROX_GLOB_CTR_INV_NSEI, |
| 58 | GBPROX_GLOB_CTR_PROTO_ERR_BSS, |
| 59 | GBPROX_GLOB_CTR_PROTO_ERR_SGSN, |
| 60 | GBPROX_GLOB_CTR_NOT_SUPPORTED_BSS, |
| 61 | GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN, |
| 62 | GBPROX_GLOB_CTR_RESTART_RESET_SGSN, |
| 63 | GBPROX_GLOB_CTR_TX_ERR_SGSN, |
| 64 | GBPROX_GLOB_CTR_OTHER_ERR, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 65 | GBPROX_GLOB_CTR_PATCH_PEER_ERR, |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static const struct rate_ctr_desc global_ctr_description[] = { |
| 69 | { "inv-bvci", "Invalid BVC Identifier " }, |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 70 | { "inv-lai", "Invalid Location Area Identifier" }, |
| 71 | { "inv-rai", "Invalid Routing Area Identifier " }, |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 72 | { "inv-nsei", "No BVC established for NSEI " }, |
| 73 | { "proto-err.bss", "BSSGP protocol error (BSS )" }, |
| 74 | { "proto-err.sgsn", "BSSGP protocol error (SGSN)" }, |
| 75 | { "not-supp.bss", "Feature not supported (BSS )" }, |
| 76 | { "not-supp.sgsn", "Feature not supported (SGSN)" }, |
| 77 | { "restart.sgsn", "Restarted RESET procedure (SGSN)" }, |
| 78 | { "tx-err.sgsn", "NS Transmission error (SGSN)" }, |
| 79 | { "error", "Other error " }, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 80 | { "mod-peer-err", "Patch error: no peer " }, |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | static const struct rate_ctr_group_desc global_ctrg_desc = { |
| 84 | .group_name_prefix = "gbproxy.global", |
| 85 | .group_description = "GBProxy Global Statistics", |
| 86 | .num_ctr = ARRAY_SIZE(global_ctr_description), |
| 87 | .ctr_desc = global_ctr_description, |
| 88 | }; |
| 89 | |
| 90 | static struct rate_ctr_group *global_ctrg = NULL; |
| 91 | |
| 92 | static struct rate_ctr_group *get_global_ctrg() |
| 93 | { |
| 94 | if (global_ctrg) |
| 95 | return global_ctrg; |
| 96 | |
| 97 | global_ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0); |
| 98 | return global_ctrg; |
| 99 | } |
| 100 | |
| 101 | enum gbprox_peer_ctr { |
| 102 | GBPROX_PEER_CTR_BLOCKED, |
| 103 | GBPROX_PEER_CTR_UNBLOCKED, |
| 104 | GBPROX_PEER_CTR_DROPPED, |
| 105 | GBPROX_PEER_CTR_INV_NSEI, |
| 106 | GBPROX_PEER_CTR_TX_ERR, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 107 | GBPROX_PEER_CTR_RAID_PATCHED_BSS, |
| 108 | GBPROX_PEER_CTR_RAID_PATCHED_SGSN, |
| 109 | GBPROX_PEER_CTR_APN_PATCHED, |
| 110 | GBPROX_PEER_CTR_PATCH_CRYPT_ERR, |
| 111 | GBPROX_PEER_CTR_PATCH_ERR, |
Jacob Erlbeck | 25049b9 | 2014-06-27 10:44:46 +0200 | [diff] [blame] | 112 | GBPROX_PEER_CTR_ATTACH_REQS, |
| 113 | GBPROX_PEER_CTR_ATTACH_REJS, |
| 114 | GBPROX_PEER_CTR_TLLI_CACHE_SIZE, |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | static const struct rate_ctr_desc peer_ctr_description[] = { |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 118 | { "blocked", "BVC Block " }, |
| 119 | { "unblocked", "BVC Unblock " }, |
| 120 | { "dropped", "BVC blocked, dropped packet " }, |
| 121 | { "inv-nsei", "NSEI mismatch " }, |
| 122 | { "tx-err", "NS Transmission error " }, |
| 123 | { "raid-mod.bss", "RAID patched (BSS )" }, |
| 124 | { "raid-mod.sgsn", "RAID patched (SGSN)" }, |
| 125 | { "apn-mod.sgsn", "APN patched " }, |
| 126 | { "mod-crypt-err", "Patch error: encrypted " }, |
| 127 | { "mod-err", "Patch error: other " }, |
Jacob Erlbeck | 25049b9 | 2014-06-27 10:44:46 +0200 | [diff] [blame] | 128 | { "attach-reqs", "Attach Request count " }, |
| 129 | { "attach-rejs", "Attach Reject count " }, |
| 130 | { "tlli-cache", "TLLI cache size " }, |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | static const struct rate_ctr_group_desc peer_ctrg_desc = { |
| 134 | .group_name_prefix = "gbproxy.peer", |
| 135 | .group_description = "GBProxy Peer Statistics", |
| 136 | .num_ctr = ARRAY_SIZE(peer_ctr_description), |
| 137 | .ctr_desc = peer_ctr_description, |
| 138 | }; |
| 139 | |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 140 | struct { |
| 141 | int check_imsi; |
| 142 | regex_t imsi_re_comp; |
| 143 | } gbprox_global_patch_state = {0,}; |
| 144 | |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 145 | struct gbprox_patch_state { |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 146 | int local_mnc; |
| 147 | int local_mcc; |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 148 | |
| 149 | /* List of TLLIs for which patching is enabled */ |
| 150 | struct llist_head enabled_tllis; |
| 151 | int enabled_tllis_count; |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 152 | }; |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 153 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 154 | struct gbprox_peer { |
| 155 | struct llist_head list; |
| 156 | |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 157 | /* NSEI of the peer entity */ |
| 158 | uint16_t nsei; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 159 | |
| 160 | /* BVCI used for Point-to-Point to this peer */ |
| 161 | uint16_t bvci; |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 162 | int blocked; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 163 | |
| 164 | /* Routeing Area that this peer is part of (raw 04.08 encoding) */ |
| 165 | uint8_t ra[6]; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 166 | |
| 167 | /* Counter */ |
| 168 | struct rate_ctr_group *ctrg; |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 169 | |
| 170 | struct gbprox_patch_state patch_state; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | /* Linked list of all Gb peers (except SGSN) */ |
| 174 | static LLIST_HEAD(gbprox_bts_peers); |
| 175 | |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 176 | static void gbprox_delete_tllis(struct gbprox_peer *peer); |
| 177 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 178 | /* Find the gbprox_peer by its BVCI */ |
| 179 | static struct gbprox_peer *peer_by_bvci(uint16_t bvci) |
| 180 | { |
| 181 | struct gbprox_peer *peer; |
| 182 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
| 183 | if (peer->bvci == bvci) |
| 184 | return peer; |
| 185 | } |
| 186 | return NULL; |
| 187 | } |
| 188 | |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 189 | /* Find the gbprox_peer by its NSEI */ |
| 190 | static struct gbprox_peer *peer_by_nsei(uint16_t nsei) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 191 | { |
| 192 | struct gbprox_peer *peer; |
| 193 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 194 | if (peer->nsei == nsei) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 195 | return peer; |
| 196 | } |
| 197 | return NULL; |
| 198 | } |
| 199 | |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 200 | /* look-up a peer by its Routeing Area Identification (RAI) */ |
| 201 | static struct gbprox_peer *peer_by_rai(const uint8_t *ra) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 202 | { |
| 203 | struct gbprox_peer *peer; |
| 204 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 205 | if (!memcmp(peer->ra, ra, 6)) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 206 | return peer; |
| 207 | } |
| 208 | return NULL; |
| 209 | } |
| 210 | |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 211 | /* look-up a peer by its Location Area Identification (LAI) */ |
| 212 | static struct gbprox_peer *peer_by_lai(const uint8_t *la) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 213 | { |
| 214 | struct gbprox_peer *peer; |
| 215 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 216 | if (!memcmp(peer->ra, la, 5)) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 217 | return peer; |
| 218 | } |
| 219 | return NULL; |
| 220 | } |
| 221 | |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 222 | /* look-up a peer by its Location Area Code (LAC) */ |
| 223 | static struct gbprox_peer *peer_by_lac(const uint8_t *la) |
| 224 | { |
| 225 | struct gbprox_peer *peer; |
| 226 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
| 227 | if (!memcmp(peer->ra + 3, la + 3, 2)) |
| 228 | return peer; |
| 229 | } |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 234 | static int check_peer_nsei(struct gbprox_peer *peer, uint16_t nsei) |
| 235 | { |
| 236 | if (peer->nsei != nsei) { |
| 237 | LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI " |
| 238 | "BVCI=%u via NSEI=%u (expected NSEI=%u)\n", |
| 239 | peer->bvci, nsei, peer->nsei); |
| 240 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]); |
Jacob Erlbeck | 64cb924 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 241 | return 0; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Jacob Erlbeck | 64cb924 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 244 | return 1; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 247 | static struct gbprox_peer *peer_alloc(uint16_t bvci) |
| 248 | { |
| 249 | struct gbprox_peer *peer; |
| 250 | |
| 251 | peer = talloc_zero(tall_bsc_ctx, struct gbprox_peer); |
| 252 | if (!peer) |
| 253 | return NULL; |
| 254 | |
| 255 | peer->bvci = bvci; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 256 | peer->ctrg = rate_ctr_group_alloc(peer, &peer_ctrg_desc, bvci); |
| 257 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 258 | llist_add(&peer->list, &gbprox_bts_peers); |
| 259 | |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 260 | INIT_LLIST_HEAD(&peer->patch_state.enabled_tllis); |
| 261 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 262 | return peer; |
| 263 | } |
| 264 | |
| 265 | static void peer_free(struct gbprox_peer *peer) |
| 266 | { |
Jacob Erlbeck | 02ca778 | 2013-10-23 11:24:16 +0200 | [diff] [blame] | 267 | rate_ctr_group_free(peer->ctrg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 268 | llist_del(&peer->list); |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 269 | |
| 270 | gbprox_delete_tllis(peer); |
| 271 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 272 | talloc_free(peer); |
| 273 | } |
| 274 | |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 275 | /* FIXME: this needs to go to libosmocore/msgb.c */ |
| 276 | static struct msgb *msgb_copy(const struct msgb *msg, const char *name) |
| 277 | { |
Harald Welte | 8645e10 | 2012-06-16 16:09:52 +0800 | [diff] [blame] | 278 | struct libgb_msgb_cb *old_cb, *new_cb; |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 279 | struct msgb *new_msg; |
| 280 | |
Holger Hans Peter Freyther | 62d9798 | 2010-06-08 18:14:37 +0800 | [diff] [blame] | 281 | new_msg = msgb_alloc(msg->data_len, name); |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 282 | if (!new_msg) |
| 283 | return NULL; |
| 284 | |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 285 | /* copy data */ |
Holger Hans Peter Freyther | 62d9798 | 2010-06-08 18:14:37 +0800 | [diff] [blame] | 286 | memcpy(new_msg->_data, msg->_data, new_msg->data_len); |
| 287 | |
| 288 | /* copy header */ |
| 289 | new_msg->len = msg->len; |
| 290 | new_msg->data += msg->data - msg->_data; |
| 291 | new_msg->head += msg->head - msg->_data; |
| 292 | new_msg->tail += msg->tail - msg->_data; |
| 293 | |
| 294 | new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data); |
| 295 | new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data); |
| 296 | new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data); |
| 297 | new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data); |
| 298 | |
| 299 | /* copy GB specific data */ |
Harald Welte | 8645e10 | 2012-06-16 16:09:52 +0800 | [diff] [blame] | 300 | old_cb = LIBGB_MSGB_CB(msg); |
| 301 | new_cb = LIBGB_MSGB_CB(new_msg); |
Holger Hans Peter Freyther | 62d9798 | 2010-06-08 18:14:37 +0800 | [diff] [blame] | 302 | |
| 303 | new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data); |
| 304 | new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data); |
| 305 | |
Harald Welte | fb9e06f | 2011-02-06 17:17:05 +0100 | [diff] [blame] | 306 | /* bssgp_cell_id is a pointer into the old msgb, so we need to make |
| 307 | * it a pointer into the new msgb */ |
| 308 | new_cb->bssgp_cell_id = new_msg->_data + (old_cb->bssgp_cell_id - msg->_data); |
Holger Hans Peter Freyther | 62d9798 | 2010-06-08 18:14:37 +0800 | [diff] [blame] | 309 | new_cb->nsei = old_cb->nsei; |
| 310 | new_cb->bvci = old_cb->bvci; |
| 311 | new_cb->tlli = old_cb->tlli; |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 312 | |
| 313 | return new_msg; |
| 314 | } |
| 315 | |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 316 | /* strip off the NS header */ |
| 317 | static void strip_ns_hdr(struct msgb *msg) |
| 318 | { |
| 319 | int strip_len = msgb_bssgph(msg) - msg->data; |
| 320 | msgb_pull(msg, strip_len); |
| 321 | } |
| 322 | |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 323 | /* TODO: Move this to libosmocore/msgb.c */ |
| 324 | static int msgb_resize_area(struct msgb *msg, uint8_t *area, |
| 325 | size_t old_size, size_t new_size) |
| 326 | { |
| 327 | int rc; |
| 328 | uint8_t *rest = area + old_size; |
| 329 | int rest_len = msg->len - old_size - (area - msg->data); |
| 330 | int delta_size = (int)new_size - (int)old_size; |
| 331 | |
| 332 | if (delta_size == 0) |
| 333 | return 0; |
| 334 | |
| 335 | if (delta_size > 0) { |
| 336 | rc = msgb_trim(msg, msg->len + delta_size); |
| 337 | if (rc < 0) |
| 338 | return rc; |
| 339 | } |
| 340 | |
| 341 | memmove(area + new_size, area + old_size, rest_len); |
| 342 | |
| 343 | if (msg->l1h >= rest) |
| 344 | msg->l1h += delta_size; |
| 345 | if (msg->l2h >= rest) |
| 346 | msg->l2h += delta_size; |
| 347 | if (msg->l3h >= rest) |
| 348 | msg->l3h += delta_size; |
| 349 | if (msg->l4h >= rest) |
| 350 | msg->l4h += delta_size; |
| 351 | |
| 352 | if (delta_size < 0) |
| 353 | msgb_trim(msg, msg->len + delta_size); |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | /* TODO: Move these conversion functions to a utils file. */ |
| 359 | char * gbprox_apn_to_str(char *out_str, const uint8_t *apn_enc, size_t rest_chars) |
| 360 | { |
| 361 | char *str = out_str; |
| 362 | |
| 363 | while (rest_chars > 0 && apn_enc[0]) { |
| 364 | size_t label_size = apn_enc[0]; |
| 365 | if (label_size + 1 > rest_chars) |
| 366 | return NULL; |
| 367 | |
| 368 | memmove(str, apn_enc + 1, label_size); |
| 369 | str += label_size; |
| 370 | rest_chars -= label_size + 1; |
| 371 | apn_enc += label_size + 1; |
| 372 | |
| 373 | if (rest_chars) |
| 374 | *(str++) = '.'; |
| 375 | } |
| 376 | str[0] = '\0'; |
| 377 | |
| 378 | return out_str; |
| 379 | } |
| 380 | |
| 381 | int gbprox_str_to_apn(uint8_t *apn_enc, const char *str, size_t max_chars) |
| 382 | { |
| 383 | uint8_t *last_len_field = apn_enc; |
| 384 | int len = 1; |
| 385 | apn_enc += 1; |
| 386 | |
| 387 | while (str[0]) { |
| 388 | if (str[0] == '.') { |
| 389 | *last_len_field = (apn_enc - last_len_field) - 1; |
| 390 | last_len_field = apn_enc; |
| 391 | } else { |
| 392 | *apn_enc = str[0]; |
| 393 | } |
| 394 | apn_enc += 1; |
| 395 | str += 1; |
| 396 | len += 1; |
| 397 | if (len > max_chars) |
| 398 | return -1; |
| 399 | } |
| 400 | |
| 401 | *last_len_field = (apn_enc - last_len_field) - 1; |
| 402 | |
| 403 | return len; |
| 404 | } |
| 405 | |
Jacob Erlbeck | b138106 | 2014-07-01 12:41:13 +0200 | [diff] [blame] | 406 | /* TODO: Move shift functions to libosmocore */ |
| 407 | |
| 408 | int v_fixed_shift(uint8_t **data, size_t *data_len, |
| 409 | size_t len, uint8_t **value) |
| 410 | { |
| 411 | if (len > *data_len) |
| 412 | goto fail; |
| 413 | |
| 414 | if (value) |
| 415 | *value = *data; |
| 416 | |
| 417 | *data += len; |
| 418 | *data_len -= len; |
| 419 | |
| 420 | return len; |
| 421 | |
| 422 | fail: |
| 423 | *data += *data_len; |
| 424 | *data_len = 0; |
| 425 | return -1; |
| 426 | } |
| 427 | |
| 428 | int tv_fixed_match(uint8_t **data, size_t *data_len, |
| 429 | uint8_t tag, size_t len, |
| 430 | uint8_t **value) |
| 431 | { |
| 432 | size_t ie_len; |
| 433 | |
| 434 | if (*data_len == 0) |
| 435 | goto fail; |
| 436 | |
| 437 | if ((*data)[0] != tag) |
| 438 | return 0; |
| 439 | |
| 440 | if (len > *data_len - 1) |
| 441 | goto fail; |
| 442 | |
| 443 | if (value) |
| 444 | *value = *data + 1; |
| 445 | |
| 446 | ie_len = len + 1; |
| 447 | *data += ie_len; |
| 448 | *data_len -= ie_len; |
| 449 | |
| 450 | return ie_len; |
| 451 | |
| 452 | fail: |
| 453 | *data += *data_len; |
| 454 | *data_len = 0; |
| 455 | return -1; |
| 456 | } |
| 457 | |
| 458 | int tlv_match(uint8_t **data, size_t *data_len, |
| 459 | uint8_t tag, uint8_t **value, size_t *value_len) |
| 460 | { |
| 461 | size_t len; |
| 462 | size_t ie_len; |
| 463 | |
| 464 | if (*data_len < 2) |
| 465 | goto fail; |
| 466 | |
| 467 | if ((*data)[0] != tag) |
| 468 | return 0; |
| 469 | |
| 470 | len = (*data)[1]; |
| 471 | if (len > *data_len - 2) |
| 472 | goto fail; |
| 473 | |
| 474 | if (value) |
| 475 | *value = *data + 2; |
| 476 | if (value_len) |
| 477 | *value_len = len; |
| 478 | |
| 479 | ie_len = len + 2; |
| 480 | |
| 481 | *data += ie_len; |
| 482 | *data_len -= ie_len; |
| 483 | |
| 484 | return ie_len; |
| 485 | |
| 486 | fail: |
| 487 | *data += *data_len; |
| 488 | *data_len = 0; |
| 489 | return -1; |
| 490 | } |
| 491 | |
| 492 | int lv_shift(uint8_t **data, size_t *data_len, |
| 493 | uint8_t **value, size_t *value_len) |
| 494 | { |
| 495 | size_t len; |
| 496 | size_t ie_len; |
| 497 | |
| 498 | if (*data_len < 1) |
| 499 | goto fail; |
| 500 | |
| 501 | len = (*data)[0]; |
| 502 | if (len > *data_len - 1) |
| 503 | goto fail; |
| 504 | |
| 505 | if (value) |
| 506 | *value = *data + 1; |
| 507 | if (value_len) |
| 508 | *value_len = len; |
| 509 | |
| 510 | ie_len = len + 1; |
| 511 | *data += ie_len; |
| 512 | *data_len -= ie_len; |
| 513 | |
| 514 | return ie_len; |
| 515 | |
| 516 | fail: |
| 517 | *data += *data_len; |
| 518 | *data_len = 0; |
| 519 | return -1; |
| 520 | } |
| 521 | |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 522 | struct gbprox_tlli_info { |
| 523 | struct llist_head list; |
| 524 | |
| 525 | uint32_t tlli; |
| 526 | time_t timestamp; |
| 527 | uint8_t *mi_data; |
| 528 | size_t mi_data_len; |
| 529 | }; |
| 530 | |
| 531 | static struct gbprox_tlli_info *gbprox_find_tlli(struct gbprox_peer *peer, |
| 532 | uint32_t tlli) |
| 533 | { |
| 534 | struct gbprox_tlli_info *tlli_info; |
| 535 | struct gbprox_patch_state *state = &peer->patch_state; |
| 536 | |
| 537 | llist_for_each_entry(tlli_info, &state->enabled_tllis, list) |
| 538 | if (tlli_info->tlli == tlli) |
| 539 | return tlli_info; |
| 540 | |
| 541 | return NULL; |
| 542 | } |
| 543 | |
| 544 | static struct gbprox_tlli_info *gbprox_find_tlli_by_mi( |
| 545 | struct gbprox_peer *peer, |
| 546 | const uint8_t *mi_data, |
| 547 | size_t mi_data_len) |
| 548 | { |
| 549 | struct gbprox_tlli_info *tlli_info; |
| 550 | struct gbprox_patch_state *state = &peer->patch_state; |
| 551 | |
| 552 | llist_for_each_entry(tlli_info, &state->enabled_tllis, list) { |
| 553 | if (tlli_info->mi_data_len != mi_data_len) |
| 554 | continue; |
| 555 | if (memcmp(tlli_info->mi_data, mi_data, mi_data_len) != 0) |
| 556 | continue; |
| 557 | |
| 558 | return tlli_info; |
| 559 | } |
| 560 | |
| 561 | return NULL; |
| 562 | } |
| 563 | |
| 564 | static void gbprox_delete_tlli(struct gbprox_peer *peer, |
| 565 | struct gbprox_tlli_info *tlli_info) |
| 566 | { |
| 567 | struct gbprox_patch_state *state = &peer->patch_state; |
| 568 | |
| 569 | llist_del(&tlli_info->list); |
| 570 | talloc_free(tlli_info); |
| 571 | state->enabled_tllis_count -= 1; |
| 572 | } |
| 573 | |
| 574 | static void gbprox_delete_tllis(struct gbprox_peer *peer) |
| 575 | { |
| 576 | struct gbprox_tlli_info *tlli_info, *nxt; |
| 577 | struct gbprox_patch_state *state = &peer->patch_state; |
| 578 | |
| 579 | llist_for_each_entry_safe(tlli_info, nxt, &state->enabled_tllis, list) { |
| 580 | llist_del(&tlli_info->list); |
| 581 | talloc_free(tlli_info); |
| 582 | } |
Jacob Erlbeck | 040b401 | 2014-06-27 12:20:44 +0200 | [diff] [blame] | 583 | state->enabled_tllis_count = 0; |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 584 | |
| 585 | OSMO_ASSERT(llist_empty(&state->enabled_tllis)); |
| 586 | } |
| 587 | |
| 588 | int gbprox_set_patch_filter(const char *filter, const char **err_msg) |
| 589 | { |
| 590 | static char err_buf[300]; |
| 591 | int rc; |
| 592 | |
| 593 | if (gbprox_global_patch_state.check_imsi) { |
| 594 | regfree(&gbprox_global_patch_state.imsi_re_comp); |
| 595 | gbprox_global_patch_state.check_imsi = 0; |
| 596 | } |
| 597 | |
| 598 | if (!filter) |
| 599 | return 0; |
| 600 | |
| 601 | rc = regcomp(&gbprox_global_patch_state.imsi_re_comp, filter, |
| 602 | REG_EXTENDED | REG_NOSUB | REG_ICASE); |
| 603 | |
| 604 | if (rc == 0) { |
| 605 | gbprox_global_patch_state.check_imsi = 1; |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | if (err_msg) { |
| 610 | regerror(rc, &gbprox_global_patch_state.imsi_re_comp, |
| 611 | err_buf, sizeof(err_buf)); |
| 612 | *err_msg = err_buf; |
| 613 | } |
| 614 | |
| 615 | return -1; |
| 616 | } |
| 617 | |
| 618 | static int gbprox_check_imsi(struct gbprox_peer *peer, |
| 619 | const uint8_t *imsi, size_t imsi_len) |
| 620 | { |
| 621 | char mi_buf[200]; |
| 622 | int rc; |
| 623 | |
| 624 | if (!gbprox_global_patch_state.check_imsi) |
| 625 | return 1; |
| 626 | |
| 627 | rc = gsm48_mi_to_string(mi_buf, sizeof(mi_buf), imsi, imsi_len); |
| 628 | if (rc < 1) { |
| 629 | LOGP(DGPRS, LOGL_NOTICE, "Invalid IMSI %s\n", |
| 630 | osmo_hexdump(imsi, imsi_len)); |
| 631 | return -1; |
| 632 | } |
| 633 | |
| 634 | LOGP(DGPRS, LOGL_DEBUG, "Checking IMSI '%s' (%d)\n", mi_buf, rc); |
| 635 | |
| 636 | rc = regexec(&gbprox_global_patch_state.imsi_re_comp, mi_buf, 0, NULL, 0); |
| 637 | if (rc == REG_NOMATCH) { |
| 638 | LOGP(DGPRS, LOGL_INFO, |
| 639 | "IMSI '%s' doesn't match pattern '%s'\n", |
| 640 | mi_buf, gbcfg.match_re); |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | return 1; |
| 645 | } |
| 646 | |
| 647 | static int gbprox_remove_stale_ttlis(struct gbprox_peer *peer, time_t now) |
| 648 | { |
| 649 | struct gbprox_patch_state *state = &peer->patch_state; |
| 650 | struct gbprox_tlli_info *tlli_info = NULL, *nxt; |
| 651 | int count = 0; |
| 652 | int deleted_count = 0; |
| 653 | |
| 654 | llist_for_each_entry_safe(tlli_info, nxt, &state->enabled_tllis, list) { |
| 655 | int is_stale = 0; |
| 656 | time_t age = now - tlli_info->timestamp; |
| 657 | |
| 658 | count += 1; |
| 659 | |
| 660 | if (gbcfg.tlli_max_len > 0) |
| 661 | is_stale = is_stale || count > gbcfg.tlli_max_len; |
| 662 | |
| 663 | if (gbcfg.tlli_max_age > 0) |
| 664 | is_stale = is_stale || age > gbcfg.tlli_max_age; |
| 665 | |
| 666 | if (!is_stale) |
| 667 | continue; |
| 668 | |
| 669 | LOGP(DGPRS, LOGL_INFO, |
| 670 | "Removing TLLI %08x from list (stale)\n", |
| 671 | tlli_info->tlli); |
| 672 | |
| 673 | gbprox_delete_tlli(peer, tlli_info); |
| 674 | tlli_info = NULL; |
| 675 | |
| 676 | deleted_count += 1; |
| 677 | } |
| 678 | |
| 679 | return deleted_count; |
| 680 | } |
| 681 | |
| 682 | static void gbprox_register_tlli(struct gbprox_peer *peer, uint32_t tlli, |
| 683 | const uint8_t *imsi, size_t imsi_len) |
| 684 | { |
| 685 | struct gbprox_patch_state *state = &peer->patch_state; |
| 686 | struct gbprox_tlli_info *tlli_info; |
| 687 | int enable_patching; |
| 688 | time_t now = 0; |
| 689 | |
| 690 | if (gprs_tlli_type(tlli) != TLLI_LOCAL) |
| 691 | return; |
| 692 | |
| 693 | if (!imsi || (imsi[0] & GSM_MI_TYPE_MASK) != GSM_MI_TYPE_IMSI) |
| 694 | return; |
| 695 | |
| 696 | if (!gbprox_global_patch_state.check_imsi) |
| 697 | return; |
| 698 | |
| 699 | tlli_info = gbprox_find_tlli(peer, tlli); |
| 700 | |
| 701 | /* Check, whether the IMSI matches */ |
| 702 | enable_patching = gbprox_check_imsi(peer, imsi, imsi_len); |
| 703 | |
| 704 | if (enable_patching < 0) |
| 705 | return; |
| 706 | |
| 707 | if (!tlli_info) { |
| 708 | tlli_info = gbprox_find_tlli_by_mi(peer, imsi, imsi_len); |
| 709 | |
| 710 | if (tlli_info) { |
| 711 | /* TLLI has changed somehow, adjust it */ |
| 712 | LOGP(DGPRS, LOGL_INFO, |
| 713 | "The TLLI has changed from %08x to %08x\n", |
| 714 | tlli_info->tlli, tlli); |
| 715 | tlli_info->tlli = tlli; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | if (!tlli_info) { |
| 720 | if (!enable_patching) |
| 721 | return; |
| 722 | |
| 723 | LOGP(DGPRS, LOGL_INFO, "Adding TLLI %08x to list\n", tlli); |
| 724 | tlli_info = talloc_zero(peer, struct gbprox_tlli_info); |
| 725 | tlli_info->tlli = tlli; |
| 726 | } else { |
| 727 | llist_del(&tlli_info->list); |
| 728 | OSMO_ASSERT(state->enabled_tllis_count > 0); |
| 729 | state->enabled_tllis_count -= 1; |
| 730 | } |
| 731 | |
| 732 | OSMO_ASSERT(tlli_info != NULL); |
| 733 | |
| 734 | if (enable_patching) { |
| 735 | now = time(NULL); |
| 736 | |
| 737 | tlli_info->timestamp = now; |
| 738 | llist_add(&tlli_info->list, &state->enabled_tllis); |
| 739 | state->enabled_tllis_count += 1; |
| 740 | |
| 741 | gbprox_remove_stale_ttlis(peer, now); |
| 742 | |
| 743 | if (tlli_info != llist_entry(state->enabled_tllis.next, |
| 744 | struct gbprox_tlli_info, list)) { |
| 745 | LOGP(DGPRS, LOGL_ERROR, |
| 746 | "Unexpectedly removed new TLLI entry as stale, " |
| 747 | "TLLI %08x\n", tlli); |
| 748 | tlli_info = NULL; |
| 749 | } |
| 750 | } else { |
| 751 | LOGP(DGPRS, LOGL_INFO, |
| 752 | "Removing TLLI %08x from list (patching no longer enabled)\n", |
| 753 | tlli); |
| 754 | talloc_free(tlli_info); |
| 755 | tlli_info = NULL; |
| 756 | } |
| 757 | |
| 758 | if (tlli_info) { |
| 759 | tlli_info->mi_data_len = imsi_len; |
| 760 | tlli_info->mi_data = |
| 761 | talloc_realloc_size(tlli_info, tlli_info->mi_data, imsi_len); |
| 762 | OSMO_ASSERT(tlli_info->mi_data != NULL); |
| 763 | memcpy(tlli_info->mi_data, imsi, imsi_len); |
| 764 | } |
Jacob Erlbeck | 25049b9 | 2014-06-27 10:44:46 +0200 | [diff] [blame] | 765 | |
Jacob Erlbeck | 25049b9 | 2014-06-27 10:44:46 +0200 | [diff] [blame] | 766 | peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current = |
| 767 | state->enabled_tllis_count; |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | static void gbprox_unregister_tlli(struct gbprox_peer *peer, uint32_t tlli) |
| 771 | { |
| 772 | struct gbprox_tlli_info *tlli_info; |
Jacob Erlbeck | 040b401 | 2014-06-27 12:20:44 +0200 | [diff] [blame] | 773 | struct gbprox_patch_state *state = &peer->patch_state; |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 774 | |
| 775 | tlli_info = gbprox_find_tlli(peer, tlli); |
| 776 | if (tlli_info) { |
| 777 | LOGP(DGPRS, LOGL_INFO, |
| 778 | "Removing TLLI %08x from list\n", |
| 779 | tlli); |
| 780 | llist_del(&tlli_info->list); |
| 781 | talloc_free(tlli_info); |
Jacob Erlbeck | 040b401 | 2014-06-27 12:20:44 +0200 | [diff] [blame] | 782 | state->enabled_tllis_count -= 1; |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 783 | } |
Jacob Erlbeck | 040b401 | 2014-06-27 12:20:44 +0200 | [diff] [blame] | 784 | |
| 785 | peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current = |
| 786 | state->enabled_tllis_count; |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | static int gbprox_check_tlli(struct gbprox_peer *peer, uint32_t tlli) |
| 790 | { |
| 791 | LOGP(DGPRS, LOGL_INFO, "Checking TLLI %08x, class: %d\n", |
| 792 | tlli, gprs_tlli_type(tlli)); |
| 793 | if (gprs_tlli_type(tlli) != TLLI_LOCAL) |
| 794 | return 0; |
| 795 | |
| 796 | return !gbprox_global_patch_state.check_imsi || |
| 797 | gbprox_find_tlli(peer, tlli) != NULL; |
| 798 | } |
| 799 | |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 800 | /* check whether patching is enabled at this level */ |
| 801 | static int patching_is_enabled(enum gbproxy_patch_mode need_at_least) |
| 802 | { |
| 803 | enum gbproxy_patch_mode patch_mode = gbcfg.patch_mode; |
| 804 | if (patch_mode == GBPROX_PATCH_DEFAULT) |
| 805 | patch_mode = GBPROX_PATCH_LLC; |
| 806 | |
| 807 | return need_at_least <= patch_mode; |
| 808 | } |
| 809 | |
| 810 | /* check whether patching is enabled at this level */ |
| 811 | static int patching_is_required(enum gbproxy_patch_mode need_at_least) |
| 812 | { |
| 813 | return need_at_least <= gbcfg.patch_mode; |
| 814 | } |
| 815 | |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 816 | /* patch RA identifier in place, update peer accordingly */ |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 817 | static void gbprox_patch_raid(uint8_t *raid_enc, struct gbprox_peer *peer, |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 818 | int to_bss, const char *log_text) |
| 819 | { |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 820 | struct gbprox_patch_state *state = &peer->patch_state; |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 821 | const int old_local_mcc = state->local_mcc; |
| 822 | const int old_local_mnc = state->local_mnc; |
| 823 | int old_mcc; |
| 824 | int old_mnc; |
| 825 | struct gprs_ra_id raid; |
| 826 | |
| 827 | gsm48_parse_ra(&raid, raid_enc); |
| 828 | |
| 829 | old_mcc = raid.mcc; |
| 830 | old_mnc = raid.mnc; |
| 831 | |
| 832 | if (!to_bss) { |
| 833 | /* BSS -> SGSN */ |
| 834 | /* save BSS side MCC/MNC */ |
| 835 | if (!gbcfg.core_mcc || raid.mcc == gbcfg.core_mcc) { |
| 836 | state->local_mcc = 0; |
| 837 | } else { |
| 838 | state->local_mcc = raid.mcc; |
| 839 | raid.mcc = gbcfg.core_mcc; |
| 840 | } |
| 841 | |
| 842 | if (!gbcfg.core_mnc || raid.mnc == gbcfg.core_mnc) { |
| 843 | state->local_mnc = 0; |
| 844 | } else { |
| 845 | state->local_mnc = raid.mnc; |
| 846 | raid.mnc = gbcfg.core_mnc; |
| 847 | } |
| 848 | } else { |
| 849 | /* SGSN -> BSS */ |
| 850 | if (state->local_mcc) |
| 851 | raid.mcc = state->local_mcc; |
| 852 | |
| 853 | if (state->local_mnc) |
| 854 | raid.mnc = state->local_mnc; |
| 855 | } |
| 856 | |
| 857 | if (old_local_mcc != state->local_mcc || |
| 858 | old_local_mnc != state->local_mnc) |
| 859 | LOGP(DGPRS, LOGL_NOTICE, |
| 860 | "Patching RAID %sactivated, msg: %s, " |
| 861 | "local: %d-%d, core: %d-%d, to %s\n", |
| 862 | state->local_mcc || state->local_mnc ? |
| 863 | "" : "de", |
| 864 | log_text, |
| 865 | state->local_mcc, state->local_mnc, |
| 866 | gbcfg.core_mcc, gbcfg.core_mnc, |
| 867 | to_bss ? "BSS" : "SGSN"); |
| 868 | |
| 869 | if (state->local_mcc || state->local_mnc) { |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 870 | enum gbprox_peer_ctr counter = |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 871 | to_bss ? |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 872 | GBPROX_PEER_CTR_RAID_PATCHED_SGSN : |
| 873 | GBPROX_PEER_CTR_RAID_PATCHED_BSS; |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 874 | |
| 875 | LOGP(DGPRS, LOGL_DEBUG, |
| 876 | "Patching %s to %s: " |
| 877 | "%d-%d-%d-%d -> %d-%d-%d-%d\n", |
| 878 | log_text, |
| 879 | to_bss ? "BSS" : "SGSN", |
| 880 | old_mcc, old_mnc, raid.lac, raid.rac, |
| 881 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 882 | |
| 883 | gsm48_construct_ra(raid_enc, &raid); |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 884 | rate_ctr_inc(&peer->ctrg->ctr[counter]); |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 885 | } |
| 886 | } |
| 887 | |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 888 | static void gbprox_patch_apn_ie(struct msgb *msg, |
| 889 | uint8_t *apn_ie, size_t apn_ie_len, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 890 | struct gbprox_peer *peer, |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 891 | size_t *new_apn_ie_len, const char *log_text) |
| 892 | { |
| 893 | struct apn_ie_hdr { |
| 894 | uint8_t iei; |
| 895 | uint8_t apn_len; |
| 896 | uint8_t apn[0]; |
| 897 | } *hdr = (void *)apn_ie; |
| 898 | |
| 899 | size_t apn_len = hdr->apn_len; |
| 900 | uint8_t *apn = hdr->apn; |
| 901 | |
| 902 | OSMO_ASSERT(apn_ie_len == apn_len + sizeof(struct apn_ie_hdr)); |
| 903 | OSMO_ASSERT(apn_ie_len > 2 && apn_ie_len <= 102); |
| 904 | |
| 905 | if (gbcfg.core_apn_size == 0) { |
| 906 | char str1[110]; |
| 907 | /* Remove the IE */ |
| 908 | LOGP(DGPRS, LOGL_DEBUG, |
| 909 | "Patching %s to SGSN: Removing APN '%s'\n", |
| 910 | log_text, |
| 911 | gbprox_apn_to_str(str1, apn, apn_len)); |
| 912 | |
| 913 | *new_apn_ie_len = 0; |
| 914 | msgb_resize_area(msg, apn_ie, apn_ie_len, 0); |
| 915 | } else { |
| 916 | /* Resize the IE */ |
| 917 | char str1[110]; |
| 918 | char str2[110]; |
| 919 | |
| 920 | OSMO_ASSERT(gbcfg.core_apn_size <= 100); |
| 921 | |
| 922 | LOGP(DGPRS, LOGL_DEBUG, |
| 923 | "Patching %s to SGSN: " |
| 924 | "Replacing APN '%s' -> '%s'\n", |
| 925 | log_text, |
| 926 | gbprox_apn_to_str(str1, apn, apn_len), |
| 927 | gbprox_apn_to_str(str2, gbcfg.core_apn, |
| 928 | gbcfg.core_apn_size)); |
| 929 | |
| 930 | *new_apn_ie_len = gbcfg.core_apn_size + 2; |
| 931 | msgb_resize_area(msg, apn, apn_len, gbcfg.core_apn_size); |
| 932 | memcpy(apn, gbcfg.core_apn, gbcfg.core_apn_size); |
| 933 | hdr->apn_len = gbcfg.core_apn_size; |
| 934 | } |
| 935 | |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 936 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_APN_PATCHED]); |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 937 | } |
| 938 | |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 939 | static int gbprox_patch_gmm_attach_req(struct msgb *msg, |
| 940 | uint8_t *data, size_t data_len, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 941 | struct gbprox_peer *peer, |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 942 | int to_bss, int *len_change) |
| 943 | { |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 944 | uint8_t *value; |
| 945 | size_t value_len; |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 946 | |
| 947 | /* Skip MS network capability */ |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 948 | if (lv_shift(&data, &data_len, NULL, &value_len) <= 0 || |
| 949 | value_len < 1 || value_len > 2) |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 950 | /* invalid */ |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 951 | return 0;; |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 952 | |
| 953 | /* Skip Attach type */ |
| 954 | /* Skip Ciphering key sequence number */ |
| 955 | /* Skip DRX parameter */ |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 956 | v_fixed_shift(&data, &data_len, 3, NULL); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 957 | |
| 958 | /* Skip Mobile identity */ |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 959 | if (lv_shift(&data, &data_len, NULL, &value_len) <= 0 || |
| 960 | value_len < 5 || value_len > 8) |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 961 | /* invalid */ |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 962 | return 0;; |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 963 | |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 964 | if (v_fixed_shift(&data, &data_len, 6, &value) <= 0) |
| 965 | return 0; |
| 966 | |
| 967 | gbprox_patch_raid(value, peer, to_bss, "LLC/ATTACH_REQ"); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 968 | |
| 969 | return 1; |
| 970 | } |
| 971 | |
| 972 | static int gbprox_patch_gmm_attach_ack(struct msgb *msg, |
| 973 | uint8_t *data, size_t data_len, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 974 | struct gbprox_peer *peer, |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 975 | int to_bss, int *len_change) |
| 976 | { |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 977 | uint8_t *value; |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 978 | |
| 979 | /* Skip Attach result */ |
| 980 | /* Skip Force to standby */ |
| 981 | /* Skip Periodic RA update timer */ |
| 982 | /* Skip Radio priority for SMS */ |
| 983 | /* Skip Spare half octet */ |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 984 | v_fixed_shift(&data, &data_len, 3, NULL); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 985 | |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 986 | if (v_fixed_shift(&data, &data_len, 6, &value) <= 0) |
| 987 | return 0; |
| 988 | |
| 989 | gbprox_patch_raid(value, peer, to_bss, "LLC/ATTACH_ACK"); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 990 | |
| 991 | return 1; |
| 992 | } |
| 993 | |
| 994 | static int gbprox_patch_gmm_ra_upd_req(struct msgb *msg, |
| 995 | uint8_t *data, size_t data_len, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 996 | struct gbprox_peer *peer, |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 997 | int to_bss, int *len_change) |
| 998 | { |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 999 | uint8_t *value; |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1000 | |
| 1001 | /* Skip Update type */ |
| 1002 | /* Skip GPRS ciphering key sequence number */ |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1003 | v_fixed_shift(&data, &data_len, 1, NULL); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1004 | |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1005 | if (v_fixed_shift(&data, &data_len, 6, &value) <= 0) |
| 1006 | return 0; |
| 1007 | |
| 1008 | gbprox_patch_raid(value, peer, to_bss, "LLC/RA_UPD_REQ"); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1009 | |
| 1010 | return 1; |
| 1011 | } |
| 1012 | |
| 1013 | static int gbprox_patch_gmm_ra_upd_ack(struct msgb *msg, |
| 1014 | uint8_t *data, size_t data_len, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1015 | struct gbprox_peer *peer, |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1016 | int to_bss, int *len_change) |
| 1017 | { |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1018 | uint8_t *value; |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1019 | |
| 1020 | /* Skip Force to standby */ |
| 1021 | /* Skip Update result */ |
| 1022 | /* Skip Periodic RA update timer */ |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1023 | v_fixed_shift(&data, &data_len, 2, NULL); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1024 | |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1025 | if (v_fixed_shift(&data, &data_len, 6, &value) <= 0) |
| 1026 | return 0; |
| 1027 | |
| 1028 | gbprox_patch_raid(value, peer, to_bss, "LLC/RA_UPD_ACK"); |
| 1029 | |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1030 | |
| 1031 | return 1; |
| 1032 | } |
| 1033 | |
| 1034 | static int gbprox_patch_gmm_ptmsi_reall_cmd(struct msgb *msg, |
| 1035 | uint8_t *data, size_t data_len, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1036 | struct gbprox_peer *peer, |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1037 | int to_bss, int *len_change) |
| 1038 | { |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1039 | uint8_t *value; |
| 1040 | size_t value_len; |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1041 | |
| 1042 | /* Skip Allocated P-TMSI */ |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1043 | if (lv_shift(&data, &data_len, NULL, &value_len) <= 0 || value_len != 5) |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1044 | /* invalid */ |
| 1045 | return 0; |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1046 | |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1047 | if (v_fixed_shift(&data, &data_len, 6, &value) <= 0) |
| 1048 | return 0; |
| 1049 | |
| 1050 | gbprox_patch_raid(value, peer, to_bss, "LLC/PTMSI_REALL_CMD"); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1051 | |
| 1052 | return 1; |
| 1053 | } |
| 1054 | |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1055 | static int gbprox_patch_gsm_act_pdp_req(struct msgb *msg, |
| 1056 | uint8_t *data, size_t data_len, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1057 | struct gbprox_peer *peer, |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1058 | int to_bss, int *len_change) |
| 1059 | { |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1060 | size_t new_len; |
| 1061 | ssize_t old_len; |
| 1062 | uint8_t *value; |
| 1063 | size_t value_len; |
| 1064 | int have_patched = 0; |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1065 | |
| 1066 | /* Skip Requested NSAPI */ |
| 1067 | /* Skip Requested LLC SAPI */ |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1068 | v_fixed_shift(&data, &data_len, 2, NULL); |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1069 | |
| 1070 | /* Skip Requested QoS (support 04.08 and 24.008) */ |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1071 | if (lv_shift(&data, &data_len, NULL, &value_len) <= 0 || |
| 1072 | value_len < 4 || value_len > 14) |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1073 | /* invalid */ |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1074 | return 0;; |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1075 | |
| 1076 | /* Skip Requested PDP address */ |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1077 | if (lv_shift(&data, &data_len, NULL, &value_len) <= 0 || |
| 1078 | value_len < 2 || value_len > 18) |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1079 | /* invalid */ |
| 1080 | return 0; |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1081 | |
| 1082 | /* Access point name */ |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1083 | old_len = tlv_match(&data, &data_len, |
| 1084 | GSM48_IE_GSM_APN, &value, &value_len); |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1085 | |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1086 | if (old_len > 0 && value_len >=1 && value_len <= 100) { |
| 1087 | gbprox_patch_apn_ie(msg, data - old_len, old_len, peer, |
| 1088 | &new_len, "LLC/ACT_PDP_REQ"); |
| 1089 | *len_change += (int)new_len - (int)old_len; |
| 1090 | data += *len_change; |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1091 | |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1092 | have_patched = 1; |
| 1093 | } |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1094 | |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1095 | return have_patched; |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1096 | } |
| 1097 | |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1098 | struct gbprox_peer *peer_by_bssgp_tlv(struct tlv_parsed *tp) |
| 1099 | { |
| 1100 | if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
| 1101 | uint16_t bvci; |
| 1102 | |
| 1103 | bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
| 1104 | if (bvci >= 2) |
| 1105 | return peer_by_bvci(bvci); |
| 1106 | } |
| 1107 | |
| 1108 | if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) { |
| 1109 | uint8_t *rai = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA); |
| 1110 | /* Only compare LAC part, since MCC/MNC are possibly patched. |
| 1111 | * Since the LAC of different BSS must be different when |
| 1112 | * MCC/MNC are patched, collisions shouldn't happen. */ |
| 1113 | return peer_by_lac(rai); |
| 1114 | } |
| 1115 | |
| 1116 | if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) { |
| 1117 | uint8_t *lai = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA); |
| 1118 | return peer_by_lac(lai); |
| 1119 | } |
| 1120 | |
| 1121 | return NULL; |
| 1122 | } |
| 1123 | |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1124 | static int gbprox_patch_dtap(struct msgb *msg, uint8_t *data, size_t data_len, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1125 | struct gbprox_peer *peer, int to_bss, |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 1126 | uint32_t tlli, int *len_change) |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1127 | { |
| 1128 | struct gsm48_hdr *g48h; |
| 1129 | |
| 1130 | *len_change = 0; |
| 1131 | |
Jacob Erlbeck | aefaf92 | 2014-06-27 18:25:53 +0200 | [diff] [blame^] | 1132 | if (v_fixed_shift(&data, &data_len, sizeof(*g48h), (uint8_t **)&g48h) <= 0) |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1133 | return 0; |
| 1134 | |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1135 | if ((g48h->proto_discr & 0x0f) != GSM48_PDISC_MM_GPRS && |
| 1136 | (g48h->proto_discr & 0x0f) != GSM48_PDISC_SM_GPRS) |
| 1137 | return 0; |
| 1138 | |
| 1139 | switch (g48h->msg_type) { |
| 1140 | case GSM48_MT_GMM_ATTACH_REQ: |
Jacob Erlbeck | 25049b9 | 2014-06-27 10:44:46 +0200 | [diff] [blame] | 1141 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1142 | return gbprox_patch_gmm_attach_req(msg, data, data_len, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1143 | peer, to_bss, len_change); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1144 | |
| 1145 | case GSM48_MT_GMM_ATTACH_ACK: |
| 1146 | if (!patching_is_enabled(GBPROX_PATCH_LLC_ATTACH)) |
| 1147 | break; |
| 1148 | return gbprox_patch_gmm_attach_ack(msg, data, data_len, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1149 | peer, to_bss, len_change); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1150 | |
Jacob Erlbeck | 25049b9 | 2014-06-27 10:44:46 +0200 | [diff] [blame] | 1151 | case GSM48_MT_GMM_ATTACH_REJ: |
| 1152 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]); |
| 1153 | break; |
| 1154 | |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1155 | case GSM48_MT_GMM_RA_UPD_REQ: |
| 1156 | if (!patching_is_enabled(GBPROX_PATCH_LLC_GMM)) |
| 1157 | break; |
| 1158 | return gbprox_patch_gmm_ra_upd_req(msg, data, data_len, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1159 | peer, to_bss, len_change); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1160 | |
| 1161 | case GSM48_MT_GMM_RA_UPD_ACK: |
| 1162 | if (!patching_is_enabled(GBPROX_PATCH_LLC_GMM)) |
| 1163 | break; |
| 1164 | return gbprox_patch_gmm_ra_upd_ack(msg, data, data_len, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1165 | peer, to_bss, len_change); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1166 | |
| 1167 | case GSM48_MT_GMM_PTMSI_REALL_CMD: |
| 1168 | if (!patching_is_enabled(GBPROX_PATCH_LLC_GMM)) |
| 1169 | break; |
| 1170 | return gbprox_patch_gmm_ptmsi_reall_cmd(msg, data, data_len, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1171 | peer, to_bss, len_change); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1172 | |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1173 | case GSM48_MT_GSM_ACT_PDP_REQ: |
| 1174 | if (!patching_is_enabled(GBPROX_PATCH_LLC_GSM)) |
| 1175 | break; |
| 1176 | if (gbcfg.core_apn == NULL) |
| 1177 | break; |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 1178 | if (!gbprox_check_tlli(peer, tlli)) |
| 1179 | break; |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1180 | return gbprox_patch_gsm_act_pdp_req(msg, data, data_len, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1181 | peer, to_bss, len_change); |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 1182 | |
| 1183 | case GSM48_MT_GMM_DETACH_ACK: |
| 1184 | case GSM48_MT_GMM_DETACH_REQ: |
| 1185 | gbprox_unregister_tlli(peer, tlli); |
| 1186 | break; |
| 1187 | |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1188 | default: |
| 1189 | break; |
| 1190 | }; |
| 1191 | |
| 1192 | return 0; |
| 1193 | } |
| 1194 | |
| 1195 | static void gbprox_patch_llc(struct msgb *msg, uint8_t *llc, size_t llc_len, |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 1196 | struct gbprox_peer *peer, int to_bss, |
| 1197 | struct bssgp_ud_hdr *budh, |
| 1198 | struct tlv_parsed *bssgp_tp) |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1199 | { |
| 1200 | struct gprs_llc_hdr_parsed ghp = {0}; |
| 1201 | int rc; |
| 1202 | uint8_t *data; |
| 1203 | size_t data_len; |
| 1204 | int fcs; |
| 1205 | int len_change = 0; |
| 1206 | const char *err_info = NULL; |
| 1207 | int err_ctr = -1; |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 1208 | uint32_t tlli = budh ? ntohl(budh->tlli) : 0; |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1209 | |
| 1210 | /* parse LLC */ |
| 1211 | rc = gprs_llc_hdr_parse(&ghp, llc, llc_len); |
| 1212 | gprs_llc_hdr_dump(&ghp); |
| 1213 | if (rc != 0) { |
| 1214 | LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n"); |
| 1215 | return; |
| 1216 | } |
| 1217 | |
| 1218 | fcs = gprs_llc_fcs(llc, ghp.crc_length); |
| 1219 | LOGP(DLLC, LOGL_DEBUG, "Got LLC message, CRC: %06x (computed %06x)\n", |
| 1220 | ghp.fcs, fcs); |
| 1221 | |
| 1222 | if (!ghp.data) |
| 1223 | return; |
| 1224 | |
| 1225 | if (ghp.sapi != GPRS_SAPI_GMM) |
| 1226 | return; |
| 1227 | |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 1228 | if (gbcfg.core_apn && to_bss && tlli && |
| 1229 | TLVP_PRESENT(bssgp_tp, BSSGP_IE_IMSI)) |
| 1230 | gbprox_register_tlli(peer, tlli, |
| 1231 | TLVP_VAL(bssgp_tp, BSSGP_IE_IMSI), |
| 1232 | TLVP_LEN(bssgp_tp, BSSGP_IE_IMSI)); |
| 1233 | |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1234 | if (ghp.cmd != GPRS_LLC_UI) |
| 1235 | return; |
| 1236 | |
| 1237 | if (ghp.is_encrypted) { |
| 1238 | if (patching_is_required(GBPROX_PATCH_LLC_ATTACH)) { |
| 1239 | /* Patching LLC messages has been requested explicitly, |
| 1240 | * but the message (including the type) is encrypted, |
| 1241 | * so we possibly fail to patch the LLC part of the |
| 1242 | * message. */ |
| 1243 | |
| 1244 | err_info = "GMM message is encrypted"; |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1245 | err_ctr = GBPROX_PEER_CTR_PATCH_CRYPT_ERR; |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1246 | goto patch_error; |
| 1247 | } |
| 1248 | |
| 1249 | return; |
| 1250 | } |
| 1251 | |
| 1252 | /* fix DTAP GMM/GSM */ |
| 1253 | data = ghp.data; |
| 1254 | data_len = ghp.data_len; |
| 1255 | |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1256 | rc = gbprox_patch_dtap(msg, data, data_len, peer, to_bss, |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 1257 | tlli, &len_change); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1258 | |
| 1259 | if (rc > 0) { |
| 1260 | llc_len += len_change; |
| 1261 | ghp.crc_length += len_change; |
| 1262 | |
| 1263 | /* Fix LLC IE len */ |
| 1264 | if (llc[-2] == BSSGP_IE_LLC_PDU && llc[-1] & 0x80) { |
| 1265 | /* most probably a one byte length */ |
| 1266 | if (llc_len > 127) { |
| 1267 | err_info = "Cannot increase size"; |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1268 | err_ctr = GBPROX_PEER_CTR_PATCH_ERR; |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1269 | goto patch_error; |
| 1270 | } |
| 1271 | llc[-1] = llc_len | 0x80; |
| 1272 | } else { |
| 1273 | llc[-2] = (llc_len >> 8) & 0x7f; |
| 1274 | llc[-1] = llc_len & 0xff; |
| 1275 | } |
| 1276 | |
| 1277 | /* Fix FCS */ |
| 1278 | fcs = gprs_llc_fcs(llc, ghp.crc_length); |
| 1279 | LOGP(DLLC, LOGL_DEBUG, "Updated LLC message, CRC: %06x -> %06x\n", |
| 1280 | ghp.fcs, fcs); |
| 1281 | |
| 1282 | llc[llc_len - 3] = fcs & 0xff; |
| 1283 | llc[llc_len - 2] = (fcs >> 8) & 0xff; |
| 1284 | llc[llc_len - 1] = (fcs >> 16) & 0xff; |
| 1285 | } |
| 1286 | |
| 1287 | return; |
| 1288 | |
| 1289 | patch_error: |
| 1290 | OSMO_ASSERT(err_ctr >= 0); |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1291 | rate_ctr_inc(&peer->ctrg->ctr[err_ctr]); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1292 | LOGP(DGPRS, LOGL_ERROR, |
| 1293 | "Failed to patch BSSGP/GMM message as requested: %s.\n", err_info); |
| 1294 | |
| 1295 | return; |
| 1296 | } |
| 1297 | |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1298 | /* patch BSSGP message to use core_mcc/mnc on the SGSN side */ |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1299 | static void gbprox_patch_bssgp_message(struct msgb *msg, |
| 1300 | struct gbprox_peer *peer, int to_bss) |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1301 | { |
| 1302 | struct bssgp_normal_hdr *bgph; |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 1303 | struct bssgp_ud_hdr *budh = NULL; |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1304 | struct tlv_parsed tp; |
| 1305 | uint8_t pdu_type; |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1306 | uint8_t *data; |
| 1307 | size_t data_len; |
| 1308 | |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1309 | if (!gbcfg.core_mcc && !gbcfg.core_mnc && !gbcfg.core_apn) |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1310 | return; |
| 1311 | |
| 1312 | bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1313 | pdu_type = bgph->pdu_type; |
| 1314 | |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1315 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 1316 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 1317 | budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); |
| 1318 | bgph = NULL; |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1319 | data = budh->data; |
| 1320 | data_len = msgb_bssgp_len(msg) - sizeof(*budh); |
| 1321 | } else { |
| 1322 | data = bgph->data; |
| 1323 | data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
| 1324 | } |
| 1325 | |
| 1326 | bssgp_tlv_parse(&tp, data, data_len); |
| 1327 | |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1328 | if (!peer && msgb_bvci(msg) >= 2) |
| 1329 | peer = peer_by_bvci(msgb_bvci(msg)); |
| 1330 | |
| 1331 | if (!peer && !to_bss) |
| 1332 | peer = peer_by_nsei(msgb_nsei(msg)); |
| 1333 | |
| 1334 | if (!peer) |
| 1335 | peer = peer_by_bssgp_tlv(&tp); |
| 1336 | |
| 1337 | if (!peer) { |
| 1338 | LOGP(DLLC, LOGL_INFO, |
| 1339 | "NSEI=%d(%s) patching: didn't find peer for message, " |
| 1340 | "PDU %d\n", |
| 1341 | msgb_nsei(msg), to_bss ? "SGSN" : "BSS", pdu_type); |
| 1342 | /* Increment counter */ |
| 1343 | rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]); |
| 1344 | return; |
| 1345 | } |
| 1346 | |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1347 | if (TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) { |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1348 | gbprox_patch_raid((uint8_t *)TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA), |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1349 | peer, to_bss, "ROUTING_AREA"); |
| 1350 | } |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1351 | |
| 1352 | if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) |
| 1353 | gbprox_patch_raid((uint8_t *)TLVP_VAL(&tp, BSSGP_IE_CELL_ID), |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1354 | peer, to_bss, "CELL_ID"); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1355 | |
| 1356 | if (TLVP_PRESENT(&tp, BSSGP_IE_LLC_PDU) && |
| 1357 | patching_is_enabled(GBPROX_PATCH_LLC_ATTACH_REQ)) { |
| 1358 | uint8_t *llc = (uint8_t *)TLVP_VAL(&tp, BSSGP_IE_LLC_PDU); |
| 1359 | size_t llc_len = TLVP_LEN(&tp, BSSGP_IE_LLC_PDU); |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 1360 | gbprox_patch_llc(msg, llc, llc_len, peer, to_bss, budh, &tp); |
Jacob Erlbeck | 91fb680 | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1361 | /* Note that the tp struct might contain invalid pointers here |
| 1362 | * if the LLC field has changed its size */ |
| 1363 | } |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1364 | } |
| 1365 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1366 | /* feed a message down the NS-VC associated with the specified peer */ |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1367 | static int gbprox_relay2sgsn(struct msgb *old_msg, |
| 1368 | struct gbprox_peer *peer, uint16_t ns_bvci) |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 1369 | { |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 1370 | /* create a copy of the message so the old one can |
| 1371 | * be free()d safely when we return from gbprox_rcvmsg() */ |
| 1372 | struct msgb *msg = msgb_copy(old_msg, "msgb_relay2sgsn"); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1373 | int rc; |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 1374 | |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1375 | gbprox_patch_bssgp_message(msg, peer, 0); |
| 1376 | |
Harald Welte | e9ea269 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 1377 | DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n", |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 1378 | msgb_nsei(msg), ns_bvci, gbcfg.nsip_sgsn_nsei); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 1379 | |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 1380 | msgb_bvci(msg) = ns_bvci; |
| 1381 | msgb_nsei(msg) = gbcfg.nsip_sgsn_nsei; |
| 1382 | |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 1383 | strip_ns_hdr(msg); |
| 1384 | |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1385 | rc = gprs_ns_sendmsg(bssgp_nsi, msg); |
| 1386 | if (rc < 0) |
| 1387 | rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]); |
| 1388 | |
| 1389 | return rc; |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 1390 | } |
| 1391 | |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 1392 | /* feed a message down the NS-VC associated with the specified peer */ |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 1393 | static int gbprox_relay2peer(struct msgb *old_msg, struct gbprox_peer *peer, |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1394 | uint16_t ns_bvci) |
| 1395 | { |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 1396 | /* create a copy of the message so the old one can |
| 1397 | * be free()d safely when we return from gbprox_rcvmsg() */ |
| 1398 | struct msgb *msg = msgb_copy(old_msg, "msgb_relay2peer"); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1399 | int rc; |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 1400 | |
Harald Welte | 0ab535b | 2010-05-13 10:34:56 +0200 | [diff] [blame] | 1401 | DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1402 | msgb_nsei(msg), ns_bvci, peer->nsei); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 1403 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1404 | msgb_bvci(msg) = ns_bvci; |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1405 | msgb_nsei(msg) = peer->nsei; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1406 | |
Harald Welte | 0ab535b | 2010-05-13 10:34:56 +0200 | [diff] [blame] | 1407 | /* Strip the old NS header, it will be replaced with a new one */ |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 1408 | strip_ns_hdr(msg); |
| 1409 | |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1410 | rc = gprs_ns_sendmsg(bssgp_nsi, msg); |
| 1411 | if (rc < 0) |
| 1412 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]); |
| 1413 | |
| 1414 | return rc; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1415 | } |
| 1416 | |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1417 | static int block_unblock_peer(uint16_t ptp_bvci, uint8_t pdu_type) |
| 1418 | { |
| 1419 | struct gbprox_peer *peer; |
| 1420 | |
| 1421 | peer = peer_by_bvci(ptp_bvci); |
| 1422 | if (!peer) { |
| 1423 | LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n", |
| 1424 | ptp_bvci); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1425 | rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1426 | return -ENOENT; |
| 1427 | } |
| 1428 | |
| 1429 | switch (pdu_type) { |
| 1430 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 1431 | peer->blocked = 1; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1432 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1433 | break; |
| 1434 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 1435 | peer->blocked = 0; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1436 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1437 | break; |
| 1438 | default: |
| 1439 | break; |
| 1440 | } |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1444 | /* Send a message to a peer identified by ptp_bvci but using ns_bvci |
| 1445 | * in the NS hdr */ |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 1446 | static int gbprox_relay2bvci(struct msgb *msg, uint16_t ptp_bvci, |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1447 | uint16_t ns_bvci) |
| 1448 | { |
| 1449 | struct gbprox_peer *peer; |
| 1450 | |
| 1451 | peer = peer_by_bvci(ptp_bvci); |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 1452 | if (!peer) { |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 1453 | LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n", |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 1454 | ptp_bvci); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1455 | rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1456 | return -ENOENT; |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 1457 | } |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1458 | |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 1459 | return gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1460 | } |
| 1461 | |
Harald Welte | b1fd902 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 1462 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 1463 | { |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1467 | /* Receive an incoming signalling message from a BSS-side NS-VC */ |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1468 | static int gbprox_rx_sig_from_bss(struct msgb *msg, uint16_t nsei, |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1469 | uint16_t ns_bvci) |
| 1470 | { |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 1471 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1472 | struct tlv_parsed tp; |
| 1473 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 1474 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1475 | struct gbprox_peer *from_peer = NULL; |
Harald Welte | 70f38d2 | 2010-05-01 12:10:57 +0200 | [diff] [blame] | 1476 | struct gprs_ra_id raid; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1477 | |
Harald Welte | c471d3d | 2011-02-06 17:13:12 +0100 | [diff] [blame] | 1478 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 1479 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1480 | nsei, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1481 | return -EINVAL; |
| 1482 | } |
| 1483 | |
| 1484 | /* we actually should never see those two for BVCI == 0, but double-check |
| 1485 | * just to make sure */ |
| 1486 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 1487 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 1488 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1489 | "signalling\n", nsei); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1490 | return -EINVAL; |
| 1491 | } |
| 1492 | |
| 1493 | bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 1494 | |
| 1495 | switch (pdu_type) { |
| 1496 | case BSSGP_PDUT_SUSPEND: |
| 1497 | case BSSGP_PDUT_RESUME: |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1498 | /* We implement RAI snooping during SUSPEND/RESUME, since it |
| 1499 | * establishes a relationsip between BVCI/peer and the routeing |
| 1500 | * area identification. The snooped information is then used |
| 1501 | * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct |
| 1502 | * BSSGP */ |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1503 | if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 1504 | goto err_mand_ie; |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1505 | from_peer = peer_by_nsei(nsei); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1506 | if (!from_peer) |
| 1507 | goto err_no_peer; |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 1508 | memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA), |
| 1509 | sizeof(from_peer->ra)); |
Harald Welte | 7fc9822 | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 1510 | gsm48_parse_ra(&raid, from_peer->ra); |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 1511 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME " |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1512 | "RAI snooping: RAI %u-%u-%u-%u behind BVCI=%u\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1513 | nsei, raid.mcc, raid.mnc, raid.lac, |
| 1514 | raid.rac , from_peer->bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1515 | /* FIXME: This only supports one BSS per RA */ |
| 1516 | break; |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 1517 | case BSSGP_PDUT_BVC_RESET: |
| 1518 | /* If we receive a BVC reset on the signalling endpoint, we |
| 1519 | * don't want the SGSN to reset, as the signalling endpoint |
| 1520 | * is common for all point-to-point BVCs (and thus all BTS) */ |
| 1521 | if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 1522 | uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 72953b8 | 2010-05-12 00:20:41 +0200 | [diff] [blame] | 1523 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1524 | nsei, bvci); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 1525 | if (bvci == 0) { |
| 1526 | /* FIXME: only do this if SGSN is alive! */ |
Harald Welte | e9ea269 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 1527 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1528 | "BVC RESET ACK of BVCI=0\n", nsei); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 1529 | return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1530 | nsei, 0, ns_bvci); |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 1531 | } |
| 1532 | from_peer = peer_by_bvci(bvci); |
| 1533 | if (!from_peer) { |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 1534 | /* if a PTP-BVC is reset, and we don't know that |
| 1535 | * PTP-BVCI yet, we should allocate a new peer */ |
| 1536 | LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1537 | "BVCI=%u via NSEI=%u\n", bvci, nsei); |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 1538 | from_peer = peer_alloc(bvci); |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1539 | from_peer->nsei = nsei; |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 1540 | } |
Jacob Erlbeck | 64cb924 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 1541 | |
| 1542 | if (!check_peer_nsei(from_peer, nsei)) |
| 1543 | from_peer->nsei = nsei; |
| 1544 | |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 1545 | if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) { |
| 1546 | struct gprs_ra_id raid; |
| 1547 | /* We have a Cell Identifier present in this |
| 1548 | * PDU, this means we can extend our local |
| 1549 | * state information about this particular cell |
| 1550 | * */ |
| 1551 | memcpy(from_peer->ra, |
| 1552 | TLVP_VAL(&tp, BSSGP_IE_CELL_ID), |
| 1553 | sizeof(from_peer->ra)); |
| 1554 | gsm48_parse_ra(&raid, from_peer->ra); |
| 1555 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1556 | "Cell ID %u-%u-%u-%u\n", nsei, |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 1557 | bvci, raid.mcc, raid.mnc, raid.lac, |
| 1558 | raid.rac); |
| 1559 | } |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 1560 | } |
| 1561 | break; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1562 | } |
| 1563 | |
Harald Welte | e9ea269 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 1564 | /* Normally, we can simply pass on all signalling messages from BSS to |
| 1565 | * SGSN */ |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1566 | return gbprox_relay2sgsn(msg, from_peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1567 | err_no_peer: |
Jacob Erlbeck | c5085f9 | 2013-10-18 13:04:48 +0200 | [diff] [blame] | 1568 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1569 | nsei); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1570 | rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_INV_NSEI]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1571 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1572 | err_mand_ie: |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1573 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1574 | nsei); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1575 | rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1576 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /* Receive paging request from SGSN, we need to relay to proper BSS */ |
| 1580 | static int gbprox_rx_paging(struct msgb *msg, struct tlv_parsed *tp, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1581 | uint32_t nsei, uint16_t ns_bvci) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1582 | { |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 1583 | struct gbprox_peer *peer = NULL; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1584 | int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1585 | |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 1586 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1587 | nsei); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1588 | if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 1589 | uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 1590 | LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n", |
| 1591 | bvci); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1592 | errctr = GBPROX_GLOB_CTR_OTHER_ERR; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1593 | } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) { |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1594 | peer = peer_by_rai(TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA)); |
| 1595 | LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n", |
Holger Hans Peter Freyther | 62eaf54 | 2010-06-08 16:30:24 +0800 | [diff] [blame] | 1596 | peer ? peer->bvci : -1); |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1597 | errctr = GBPROX_GLOB_CTR_INV_RAI; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1598 | } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) { |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1599 | peer = peer_by_lai(TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA)); |
| 1600 | LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n", |
Holger Hans Peter Freyther | 62eaf54 | 2010-06-08 16:30:24 +0800 | [diff] [blame] | 1601 | peer ? peer->bvci : -1); |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1602 | errctr = GBPROX_GLOB_CTR_INV_LAI; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1603 | } else |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 1604 | LOGPC(DGPRS, LOGL_INFO, "\n"); |
| 1605 | |
| 1606 | if (!peer) { |
| 1607 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1608 | "unable to route, missing IE\n", nsei); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1609 | rate_ctr_inc(&get_global_ctrg()->ctr[errctr]); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1610 | return -EINVAL; |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 1611 | } |
| 1612 | return gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1613 | } |
| 1614 | |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1615 | /* Receive an incoming BVC-RESET message from the SGSN */ |
| 1616 | static int rx_reset_from_sgsn(struct msgb *msg, struct tlv_parsed *tp, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1617 | uint32_t nsei, uint16_t ns_bvci) |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1618 | { |
| 1619 | struct gbprox_peer *peer; |
| 1620 | uint16_t ptp_bvci; |
| 1621 | |
| 1622 | if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1623 | rate_ctr_inc(&get_global_ctrg()-> |
| 1624 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1625 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, |
| 1626 | NULL, msg); |
| 1627 | } |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 1628 | ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1629 | |
| 1630 | if (ptp_bvci >= 2) { |
| 1631 | /* A reset for a PTP BVC was received, forward it to its |
| 1632 | * respective peer */ |
| 1633 | peer = peer_by_bvci(ptp_bvci); |
| 1634 | if (!peer) { |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 1635 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1636 | nsei, ptp_bvci); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1637 | rate_ctr_inc(&get_global_ctrg()-> |
| 1638 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1639 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 1640 | NULL, msg); |
| 1641 | } |
| 1642 | return gbprox_relay2peer(msg, peer, ns_bvci); |
| 1643 | } |
| 1644 | |
| 1645 | /* A reset for the Signalling entity has been received |
| 1646 | * from the SGSN. As the signalling BVCI is shared |
| 1647 | * among all the BSS's that we multiplex, it needs to |
| 1648 | * be relayed */ |
| 1649 | llist_for_each_entry(peer, &gbprox_bts_peers, list) |
| 1650 | gbprox_relay2peer(msg, peer, ns_bvci); |
| 1651 | |
| 1652 | return 0; |
| 1653 | } |
| 1654 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1655 | /* Receive an incoming signalling message from the SGSN-side NS-VC */ |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1656 | static int gbprox_rx_sig_from_sgsn(struct msgb *msg, uint32_t nsei, |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1657 | uint16_t ns_bvci) |
| 1658 | { |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 1659 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1660 | struct tlv_parsed tp; |
| 1661 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 1662 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1663 | struct gbprox_peer *peer; |
| 1664 | uint16_t bvci; |
| 1665 | int rc = 0; |
| 1666 | |
Harald Welte | c471d3d | 2011-02-06 17:13:12 +0100 | [diff] [blame] | 1667 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 1668 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1669 | "signalling\n", nsei, ns_bvci); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1670 | /* FIXME: Send proper error message */ |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1671 | return -EINVAL; |
| 1672 | } |
| 1673 | |
| 1674 | /* we actually should never see those two for BVCI == 0, but double-check |
| 1675 | * just to make sure */ |
| 1676 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 1677 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 1678 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1679 | "signalling\n", nsei); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1680 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 1684 | |
| 1685 | switch (pdu_type) { |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1686 | case BSSGP_PDUT_BVC_RESET: |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1687 | rc = rx_reset_from_sgsn(msg, &tp, nsei, ns_bvci); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1688 | break; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1689 | case BSSGP_PDUT_FLUSH_LL: |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1690 | case BSSGP_PDUT_BVC_RESET_ACK: |
| 1691 | /* simple case: BVCI IE is mandatory */ |
| 1692 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 1693 | goto err_mand_ie; |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 1694 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 1695 | rc = gbprox_relay2bvci(msg, bvci, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1696 | break; |
| 1697 | case BSSGP_PDUT_PAGING_PS: |
| 1698 | case BSSGP_PDUT_PAGING_CS: |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1699 | /* process the paging request (LAI/RAI lookup) */ |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1700 | rc = gbprox_rx_paging(msg, &tp, nsei, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1701 | break; |
| 1702 | case BSSGP_PDUT_STATUS: |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1703 | /* Some exception has occurred */ |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 1704 | LOGP(DGPRS, LOGL_NOTICE, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1705 | "NSEI=%u(SGSN) BSSGP STATUS ", nsei); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1706 | if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) { |
| 1707 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
| 1708 | goto err_mand_ie; |
| 1709 | } |
| 1710 | LOGPC(DGPRS, LOGL_NOTICE, |
| 1711 | "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE), |
| 1712 | bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE))); |
| 1713 | if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 1714 | uint16_t bvci = tlvp_val16_unal(&tp, BSSGP_IE_BVCI); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1715 | LOGPC(DGPRS, LOGL_NOTICE, |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 1716 | "BVCI=%u\n", ntohs(bvci)); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1717 | } else |
| 1718 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1719 | break; |
| 1720 | /* those only exist in the SGSN -> BSS direction */ |
| 1721 | case BSSGP_PDUT_SUSPEND_ACK: |
| 1722 | case BSSGP_PDUT_SUSPEND_NACK: |
| 1723 | case BSSGP_PDUT_RESUME_ACK: |
| 1724 | case BSSGP_PDUT_RESUME_NACK: |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1725 | /* RAI IE is mandatory */ |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1726 | if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 1727 | goto err_mand_ie; |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1728 | peer = peer_by_rai(TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA)); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1729 | if (!peer) |
| 1730 | goto err_no_peer; |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 1731 | rc = gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1732 | break; |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1733 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 1734 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 1735 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 1736 | goto err_mand_ie; |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 1737 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1738 | if (bvci == 0) { |
| 1739 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1740 | "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei, |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1741 | pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":""); |
| 1742 | /* should we send STATUS ? */ |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1743 | rate_ctr_inc(&get_global_ctrg()-> |
| 1744 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1745 | } else { |
| 1746 | /* Mark BVC as (un)blocked */ |
| 1747 | block_unblock_peer(bvci, pdu_type); |
| 1748 | } |
| 1749 | rc = gbprox_relay2bvci(msg, bvci, ns_bvci); |
| 1750 | break; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1751 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 1752 | LOGP(DGPRS, LOGL_ERROR, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1753 | "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1754 | rate_ctr_inc(&get_global_ctrg()-> |
| 1755 | ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1756 | rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1757 | break; |
| 1758 | default: |
Harald Welte | 72953b8 | 2010-05-12 00:20:41 +0200 | [diff] [blame] | 1759 | LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n", |
| 1760 | pdu_type); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1761 | rate_ctr_inc(&get_global_ctrg()-> |
| 1762 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1763 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1764 | break; |
| 1765 | } |
| 1766 | |
| 1767 | return rc; |
| 1768 | err_mand_ie: |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 1769 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1770 | nsei); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1771 | rate_ctr_inc(&get_global_ctrg()-> |
| 1772 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1773 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1774 | err_no_peer: |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1775 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1776 | nsei); |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1777 | rate_ctr_inc(&get_global_ctrg()-> ctr[GBPROX_GLOB_CTR_INV_RAI]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1778 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | /* Main input function for Gb proxy */ |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1782 | int gbprox_rcvmsg(struct msgb *msg, uint16_t nsei, uint16_t ns_bvci, uint16_t nsvci) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1783 | { |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 1784 | int rc; |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1785 | struct gbprox_peer *peer; |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1786 | int remote_end_is_sgsn = nsei == gbcfg.nsip_sgsn_nsei; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1787 | |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1788 | if (remote_end_is_sgsn) |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1789 | gbprox_patch_bssgp_message(msg, NULL, 1); |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1790 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1791 | /* Only BVCI=0 messages need special treatment */ |
| 1792 | if (ns_bvci == 0 || ns_bvci == 1) { |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1793 | if (remote_end_is_sgsn) |
| 1794 | rc = gbprox_rx_sig_from_sgsn(msg, nsei, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1795 | else |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1796 | rc = gbprox_rx_sig_from_bss(msg, nsei, ns_bvci); |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 1797 | } else { |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1798 | peer = peer_by_bvci(ns_bvci); |
| 1799 | |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 1800 | /* All other BVCI are PTP and thus can be simply forwarded */ |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1801 | if (!remote_end_is_sgsn) { |
| 1802 | if (peer) |
| 1803 | check_peer_nsei(peer, nsei); |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1804 | return gbprox_relay2sgsn(msg, peer, ns_bvci); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1805 | } |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1806 | |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1807 | /* else: SGSN -> BSS direction */ |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1808 | if (!peer) { |
Jacob Erlbeck | f0f63a4 | 2013-10-18 22:12:17 +0200 | [diff] [blame] | 1809 | LOGP(DGPRS, LOGL_INFO, "Didn't find peer for " |
| 1810 | "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n", |
| 1811 | ns_bvci, nsvci, nsei); |
| 1812 | rate_ctr_inc(&get_global_ctrg()-> |
| 1813 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
| 1814 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 1815 | &ns_bvci, msg); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1816 | } |
| 1817 | if (peer->blocked) { |
| 1818 | LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for " |
| 1819 | "blocked BVCI=%u via NSVC=%u/NSEI=%u\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1820 | ns_bvci, nsvci, nsei); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1821 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1822 | return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, NULL, msg); |
| 1823 | } |
| 1824 | rc = gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1825 | } |
| 1826 | |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 1827 | return rc; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1828 | } |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 1829 | |
Harald Welte | 1ccbf44 | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 1830 | int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi) |
| 1831 | { |
| 1832 | struct gprs_nsvc *nsvc; |
| 1833 | |
| 1834 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 1835 | if (!nsvc->persistent) |
| 1836 | continue; |
| 1837 | gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
| 1838 | } |
| 1839 | return 0; |
| 1840 | } |
| 1841 | |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1842 | /* Signal handler for signals from NS layer */ |
| 1843 | int gbprox_signal(unsigned int subsys, unsigned int signal, |
| 1844 | void *handler_data, void *signal_data) |
| 1845 | { |
| 1846 | struct ns_signal_data *nssd = signal_data; |
| 1847 | struct gprs_nsvc *nsvc = nssd->nsvc; |
| 1848 | struct gbprox_peer *peer; |
| 1849 | |
Harald Welte | a6a20b4 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 1850 | if (subsys != SS_L_NS) |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1851 | return 0; |
| 1852 | |
Harald Welte | f69c059 | 2010-05-11 18:29:44 +0200 | [diff] [blame] | 1853 | if (signal == S_NS_RESET && nsvc->nsei == gbcfg.nsip_sgsn_nsei) { |
| 1854 | /* We have received a NS-RESET from the NSEI and NSVC |
| 1855 | * of the SGSN. This might happen with SGSN that start |
| 1856 | * their own NS-RESET procedure without waiting for our |
| 1857 | * NS-RESET */ |
| 1858 | nsvc->remote_end_is_sgsn = 1; |
| 1859 | } |
| 1860 | |
Harald Welte | b778d2c | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 1861 | if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) { |
| 1862 | LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, " |
| 1863 | "re-starting RESET procedure\n"); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1864 | rate_ctr_inc(&get_global_ctrg()-> |
| 1865 | ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]); |
Harald Welte | e6599ee | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 1866 | gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr, |
| 1867 | nsvc->nsei, nsvc->nsvci); |
Harald Welte | b778d2c | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1870 | if (!nsvc->remote_end_is_sgsn) { |
| 1871 | /* from BSS to SGSN */ |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1872 | peer = peer_by_nsei(nsvc->nsei); |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1873 | if (!peer) { |
| 1874 | LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer " |
| 1875 | "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei, |
| 1876 | nsvc->nsvci); |
| 1877 | return 0; |
| 1878 | } |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1879 | switch (signal) { |
| 1880 | case S_NS_RESET: |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1881 | case S_NS_BLOCK: |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1882 | if (!peer->blocked) |
| 1883 | break; |
| 1884 | LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from " |
| 1885 | "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n", |
| 1886 | nsvc->nsei, nsvc->nsvci); |
| 1887 | bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei, |
| 1888 | peer->bvci, 0); |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1889 | break; |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1890 | } |
| 1891 | } else { |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1892 | /* Forward this message to all NS-VC to BSS */ |
| 1893 | struct gprs_ns_inst *nsi = gbcfg.nsi; |
| 1894 | struct gprs_nsvc *next_nsvc; |
| 1895 | |
| 1896 | llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) { |
| 1897 | if (next_nsvc->remote_end_is_sgsn) |
| 1898 | continue; |
| 1899 | |
| 1900 | /* Note that the following does not start the full |
| 1901 | * procedures including timer based retransmissions. */ |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1902 | switch (signal) { |
| 1903 | case S_NS_RESET: |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1904 | gprs_ns_tx_reset(next_nsvc, nssd->cause); |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1905 | break; |
| 1906 | case S_NS_BLOCK: |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1907 | gprs_ns_tx_block(next_nsvc, nssd->cause); |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1908 | break; |
| 1909 | case S_NS_UNBLOCK: |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1910 | gprs_ns_tx_unblock(next_nsvc); |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1911 | break; |
| 1912 | } |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | return 0; |
| 1916 | } |
| 1917 | |
Jacob Erlbeck | b32d3c0 | 2014-07-07 10:45:59 +0200 | [diff] [blame] | 1918 | int gbprox_dump_global(FILE *stream, int indent) |
Jacob Erlbeck | da890c7 | 2013-10-18 22:12:16 +0200 | [diff] [blame] | 1919 | { |
| 1920 | unsigned int i; |
| 1921 | const struct rate_ctr_group_desc *desc; |
| 1922 | int rc; |
| 1923 | |
| 1924 | rc = fprintf(stream, "%*sGbproxy global:\n", indent, ""); |
| 1925 | if (rc < 0) |
| 1926 | return rc; |
| 1927 | |
Jacob Erlbeck | da890c7 | 2013-10-18 22:12:16 +0200 | [diff] [blame] | 1928 | desc = get_global_ctrg()->desc; |
| 1929 | |
| 1930 | for (i = 0; i < desc->num_ctr; i++) { |
| 1931 | struct rate_ctr *ctr = &get_global_ctrg()->ctr[i]; |
| 1932 | if (ctr->current) { |
| 1933 | rc = fprintf(stream, "%*s %s: %llu\n", |
| 1934 | indent, "", |
| 1935 | desc->ctr_desc[i].description, |
| 1936 | (long long)ctr->current); |
| 1937 | |
| 1938 | if (rc < 0) |
| 1939 | return rc; |
| 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | return 0; |
| 1944 | } |
| 1945 | |
Jacob Erlbeck | b32d3c0 | 2014-07-07 10:45:59 +0200 | [diff] [blame] | 1946 | int gbprox_dump_peers(FILE *stream, int indent) |
Jacob Erlbeck | 51a869c | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 1947 | { |
| 1948 | struct gbprox_peer *peer; |
| 1949 | struct gprs_ra_id raid; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1950 | unsigned int i; |
| 1951 | const struct rate_ctr_group_desc *desc; |
Jacob Erlbeck | 51a869c | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 1952 | int rc; |
Jacob Erlbeck | aa3e334 | 2014-06-27 11:55:04 +0200 | [diff] [blame] | 1953 | time_t now = time(NULL); |
Jacob Erlbeck | 51a869c | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 1954 | |
Jacob Erlbeck | da890c7 | 2013-10-18 22:12:16 +0200 | [diff] [blame] | 1955 | rc = fprintf(stream, "%*sPeers:\n", indent, ""); |
| 1956 | if (rc < 0) |
| 1957 | return rc; |
| 1958 | |
Jacob Erlbeck | 51a869c | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 1959 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Jacob Erlbeck | aa3e334 | 2014-06-27 11:55:04 +0200 | [diff] [blame] | 1960 | struct gbprox_tlli_info *tlli_info; |
| 1961 | struct gbprox_patch_state *state = &peer->patch_state; |
Jacob Erlbeck | 51a869c | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 1962 | gsm48_parse_ra(&raid, peer->ra); |
| 1963 | |
| 1964 | rc = fprintf(stream, "%*s NSEI %u, BVCI %u, %sblocked, " |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1965 | "RAI %u-%u-%u-%u\n", |
Jacob Erlbeck | 51a869c | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 1966 | indent, "", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1967 | peer->nsei, peer->bvci, |
Jacob Erlbeck | 51a869c | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 1968 | peer->blocked ? "" : "not ", |
| 1969 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 1970 | |
| 1971 | if (rc < 0) |
| 1972 | return rc; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1973 | |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1974 | desc = peer->ctrg->desc; |
| 1975 | |
| 1976 | for (i = 0; i < desc->num_ctr; i++) { |
| 1977 | struct rate_ctr *ctr = &peer->ctrg->ctr[i]; |
| 1978 | if (ctr->current) { |
| 1979 | rc = fprintf(stream, "%*s %s: %llu\n", |
| 1980 | indent, "", |
| 1981 | desc->ctr_desc[i].description, |
| 1982 | (long long)ctr->current); |
| 1983 | |
| 1984 | if (rc < 0) |
| 1985 | return rc; |
| 1986 | } |
| 1987 | } |
Jacob Erlbeck | aa3e334 | 2014-06-27 11:55:04 +0200 | [diff] [blame] | 1988 | |
| 1989 | fprintf(stream, "%*s TLLI-Cache: %d\n", |
| 1990 | indent, "", state->enabled_tllis_count); |
| 1991 | llist_for_each_entry(tlli_info, &state->enabled_tllis, list) { |
| 1992 | char mi_buf[200]; |
| 1993 | time_t age = now - tlli_info->timestamp; |
| 1994 | snprintf(mi_buf, sizeof(mi_buf), "(invalid)"); |
| 1995 | gsm48_mi_to_string(mi_buf, sizeof(mi_buf), |
| 1996 | tlli_info->mi_data, |
| 1997 | tlli_info->mi_data_len); |
| 1998 | rc = fprintf(stream, |
| 1999 | "%*s TLLI %08x, IMSI %s, AGE %d\n", |
| 2000 | indent, "", |
| 2001 | tlli_info->tlli, mi_buf, (int)age); |
| 2002 | if (rc < 0) |
| 2003 | return rc; |
| 2004 | } |
Jacob Erlbeck | 51a869c | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | return 0; |
| 2008 | } |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 2009 | |
Jacob Erlbeck | 72b401f | 2013-10-24 12:48:55 +0200 | [diff] [blame] | 2010 | void gbprox_reset() |
| 2011 | { |
| 2012 | struct gbprox_peer *peer, *tmp; |
| 2013 | |
| 2014 | llist_for_each_entry_safe(peer, tmp, &gbprox_bts_peers, list) |
| 2015 | peer_free(peer); |
| 2016 | |
| 2017 | rate_ctr_group_free(global_ctrg); |
| 2018 | global_ctrg = NULL; |
| 2019 | } |
| 2020 | |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 2021 | static int gbprox_cleanup_peers(uint16_t nsei, uint16_t bvci) |
| 2022 | { |
| 2023 | int counter = 0; |
| 2024 | struct gbprox_peer *peer, *tmp; |
| 2025 | |
| 2026 | llist_for_each_entry_safe(peer, tmp, &gbprox_bts_peers, list) { |
| 2027 | if (peer->nsei != nsei) |
| 2028 | continue; |
| 2029 | if (bvci && peer->bvci != bvci) |
| 2030 | continue; |
| 2031 | |
| 2032 | peer_free(peer); |
| 2033 | counter += 1; |
| 2034 | } |
| 2035 | |
| 2036 | return counter; |
| 2037 | } |
| 2038 | |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 2039 | #include <osmocom/vty/command.h> |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 2040 | |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 2041 | static void gbprox_vty_print_peer(struct vty *vty, struct gbprox_peer *peer) |
| 2042 | { |
| 2043 | struct gprs_ra_id raid; |
| 2044 | gsm48_parse_ra(&raid, peer->ra); |
| 2045 | |
| 2046 | vty_out(vty, "NSEI %5u, PTP-BVCI %5u, " |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 2047 | "RAI %u-%u-%u-%u", |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 2048 | peer->nsei, peer->bvci, |
| 2049 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 2050 | if (peer->blocked) |
| 2051 | vty_out(vty, " [BVC-BLOCKED]"); |
| 2052 | |
| 2053 | vty_out(vty, "%s", VTY_NEWLINE); |
| 2054 | } |
| 2055 | |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2056 | gDEFUN(show_gbproxy, show_gbproxy_cmd, "show gbproxy [stats]", |
Holger Hans Peter Freyther | 82e644b | 2013-11-03 17:34:17 +0100 | [diff] [blame] | 2057 | SHOW_STR "Display information about the Gb proxy\n" "Show statistics\n") |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 2058 | { |
| 2059 | struct gbprox_peer *peer; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2060 | int show_stats = argc >= 1; |
| 2061 | |
| 2062 | if (show_stats) |
| 2063 | vty_out_rate_ctr_group(vty, "", get_global_ctrg()); |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 2064 | |
| 2065 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 2066 | gbprox_vty_print_peer(vty, peer); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2067 | |
| 2068 | if (show_stats) |
| 2069 | vty_out_rate_ctr_group(vty, " ", peer->ctrg); |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 2070 | } |
| 2071 | return CMD_SUCCESS; |
| 2072 | } |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 2073 | |
Jacob Erlbeck | 7dd498d | 2014-06-19 10:15:27 +0200 | [diff] [blame] | 2074 | gDEFUN(show_gbproxy_tllis, show_gbproxy_tllis_cmd, "show gbproxy tllis", |
| 2075 | SHOW_STR "Display information about the Gb proxy\n" "Show TLLIs\n") |
| 2076 | { |
| 2077 | struct gbprox_peer *peer; |
| 2078 | char mi_buf[200]; |
| 2079 | time_t now = time(NULL); |
| 2080 | |
| 2081 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
| 2082 | struct gbprox_tlli_info *tlli_info; |
| 2083 | struct gbprox_patch_state *state = &peer->patch_state; |
| 2084 | |
| 2085 | gbprox_vty_print_peer(vty, peer); |
| 2086 | |
| 2087 | llist_for_each_entry(tlli_info, &state->enabled_tllis, list) { |
| 2088 | time_t age = now - tlli_info->timestamp; |
| 2089 | snprintf(mi_buf, sizeof(mi_buf), "(invalid)"); |
| 2090 | gsm48_mi_to_string(mi_buf, sizeof(mi_buf), |
| 2091 | tlli_info->mi_data, |
| 2092 | tlli_info->mi_data_len); |
| 2093 | vty_out(vty, " TLLI %08x, IMSI %s, AGE %d%s", |
| 2094 | tlli_info->tlli, mi_buf, (int)age, |
| 2095 | VTY_NEWLINE); |
| 2096 | } |
| 2097 | } |
| 2098 | return CMD_SUCCESS; |
| 2099 | } |
| 2100 | |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 2101 | gDEFUN(delete_gb_bvci, delete_gb_bvci_cmd, |
| 2102 | "delete-gbproxy-peer <0-65534> bvci <2-65534>", |
| 2103 | "Delete a GBProxy peer by NSEI and optionally BVCI\n" |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 2104 | "NSEI number\n" |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 2105 | "Only delete peer with a matching BVCI\n" |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 2106 | "BVCI number\n") |
| 2107 | { |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 2108 | const uint16_t nsei = atoi(argv[0]); |
| 2109 | const uint16_t bvci = atoi(argv[1]); |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 2110 | int counter; |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 2111 | |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 2112 | counter = gbprox_cleanup_peers(nsei, bvci); |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 2113 | |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 2114 | if (counter == 0) { |
| 2115 | vty_out(vty, "BVC not found%s", VTY_NEWLINE); |
| 2116 | return CMD_WARNING; |
| 2117 | } |
| 2118 | |
| 2119 | return CMD_SUCCESS; |
| 2120 | } |
| 2121 | |
| 2122 | gDEFUN(delete_gb_nsei, delete_gb_nsei_cmd, |
| 2123 | "delete-gbproxy-peer <0-65534> (only-bvc|only-nsvc|all) [dry-run]", |
| 2124 | "Delete a GBProxy peer by NSEI and optionally BVCI\n" |
| 2125 | "NSEI number\n" |
| 2126 | "Only delete BSSGP connections (BVC)\n" |
| 2127 | "Only delete dynamic NS connections (NS-VC)\n" |
| 2128 | "Delete BVC and dynamic NS connections\n" |
| 2129 | "Show what would be deleted instead of actually deleting\n" |
| 2130 | ) |
| 2131 | { |
| 2132 | const uint16_t nsei = atoi(argv[0]); |
| 2133 | const char *mode = argv[1]; |
| 2134 | int dry_run = argc > 2; |
| 2135 | int delete_bvc = 0; |
| 2136 | int delete_nsvc = 0; |
| 2137 | int counter; |
| 2138 | |
| 2139 | if (strcmp(mode, "only-bvc") == 0) |
| 2140 | delete_bvc = 1; |
| 2141 | else if (strcmp(mode, "only-nsvc") == 0) |
| 2142 | delete_nsvc = 1; |
| 2143 | else |
| 2144 | delete_bvc = delete_nsvc = 1; |
| 2145 | |
| 2146 | if (delete_bvc) { |
| 2147 | if (!dry_run) |
| 2148 | counter = gbprox_cleanup_peers(nsei, 0); |
| 2149 | else { |
| 2150 | struct gbprox_peer *peer; |
| 2151 | counter = 0; |
| 2152 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
| 2153 | if (peer->nsei != nsei) |
| 2154 | continue; |
| 2155 | |
| 2156 | vty_out(vty, "BVC: "); |
| 2157 | gbprox_vty_print_peer(vty, peer); |
| 2158 | counter += 1; |
| 2159 | } |
| 2160 | } |
| 2161 | vty_out(vty, "%sDeleted %d BVC%s", |
| 2162 | dry_run ? "Not " : "", counter, VTY_NEWLINE); |
| 2163 | } |
| 2164 | |
| 2165 | if (delete_nsvc) { |
| 2166 | struct gprs_ns_inst *nsi = gbcfg.nsi; |
| 2167 | struct gprs_nsvc *nsvc, *nsvc2; |
| 2168 | |
| 2169 | counter = 0; |
| 2170 | llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list) { |
| 2171 | if (nsvc->nsei != nsei) |
| 2172 | continue; |
| 2173 | if (nsvc->persistent) |
| 2174 | continue; |
| 2175 | |
| 2176 | if (!dry_run) |
| 2177 | gprs_nsvc_delete(nsvc); |
| 2178 | else |
| 2179 | vty_out(vty, "NS-VC: NSEI %5u, NS-VCI %5u, " |
| 2180 | "remote %s%s", |
| 2181 | nsvc->nsei, nsvc->nsvci, |
| 2182 | gprs_ns_ll_str(nsvc), VTY_NEWLINE); |
| 2183 | counter += 1; |
| 2184 | } |
| 2185 | vty_out(vty, "%sDeleted %d NS-VC%s", |
| 2186 | dry_run ? "Not " : "", counter, VTY_NEWLINE); |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | return CMD_SUCCESS; |
| 2190 | } |
Jacob Erlbeck | 7dd498d | 2014-06-19 10:15:27 +0200 | [diff] [blame] | 2191 | |
| 2192 | gDEFUN(delete_gb_tlli, delete_gb_tlli_cmd, |
| 2193 | "delete-gbproxy-tlli <0-65534> (tlli|imsi|stale) [IDENT]", |
| 2194 | "Delete a GBProxy TLLI entry by NSEI and identification\n" |
| 2195 | "NSEI number\n" |
| 2196 | "Delete entries with a matching TLLI (hex)\n" |
| 2197 | "Delete entries with a matching IMSI\n" |
| 2198 | "Identification to match\n") |
| 2199 | { |
| 2200 | const uint16_t nsei = atoi(argv[0]); |
| 2201 | enum {MATCH_TLLI = 't', MATCH_IMSI = 'i', MATCH_STALE = 's'} match; |
| 2202 | uint32_t tlli = 0; |
| 2203 | const char *imsi = NULL; |
| 2204 | struct gbprox_peer *peer = 0; |
| 2205 | struct gbprox_tlli_info *tlli_info, *nxt; |
| 2206 | struct gbprox_patch_state *state; |
| 2207 | char mi_buf[200]; |
| 2208 | int found = 0; |
| 2209 | |
| 2210 | match = argv[1][0]; |
| 2211 | |
| 2212 | switch (match) { |
| 2213 | case MATCH_TLLI: |
| 2214 | if (argc < 2 || !argv[2][0]) { |
| 2215 | vty_out(vty, "%% Missing TLLI%s", VTY_NEWLINE); |
| 2216 | return CMD_WARNING; |
| 2217 | } |
| 2218 | tlli = strtoll(argv[2], NULL, 16); |
| 2219 | break; |
| 2220 | case MATCH_IMSI: |
| 2221 | if (argc < 2 || !argv[2][0]) { |
| 2222 | vty_out(vty, "%% Missing IMSI%s", VTY_NEWLINE); |
| 2223 | return CMD_WARNING; |
| 2224 | } |
| 2225 | imsi = argv[2]; |
| 2226 | break; |
| 2227 | default: |
| 2228 | break; |
| 2229 | } |
| 2230 | |
| 2231 | peer = peer_by_nsei(nsei); |
| 2232 | if (!peer) { |
| 2233 | vty_out(vty, "Didn't find peer with NSEI %d%s", |
| 2234 | nsei, VTY_NEWLINE); |
| 2235 | return CMD_WARNING; |
| 2236 | } |
| 2237 | |
| 2238 | state = &peer->patch_state; |
| 2239 | |
| 2240 | if (match == MATCH_STALE) { |
| 2241 | found = gbprox_remove_stale_ttlis(peer, time(NULL)); |
| 2242 | if (found) |
| 2243 | vty_out(vty, "Deleted %d stale TLLI%s%s", |
| 2244 | found, found == 1 ? "" : "s", VTY_NEWLINE); |
| 2245 | return CMD_SUCCESS; |
| 2246 | } |
| 2247 | |
| 2248 | llist_for_each_entry_safe(tlli_info, nxt, &state->enabled_tllis, list) { |
| 2249 | if (match == MATCH_TLLI && tlli_info->tlli != tlli) |
| 2250 | continue; |
| 2251 | |
| 2252 | if (match == MATCH_IMSI) { |
| 2253 | mi_buf[0] = '\0'; |
| 2254 | gsm48_mi_to_string(mi_buf, sizeof(mi_buf), |
| 2255 | tlli_info->mi_data, |
| 2256 | tlli_info->mi_data_len); |
| 2257 | |
| 2258 | if (strcmp(mi_buf, imsi) != 0) |
| 2259 | continue; |
| 2260 | } |
| 2261 | vty_out(vty, "Deleting TLLI %08x%s", tlli_info->tlli, VTY_NEWLINE); |
| 2262 | gbprox_delete_tlli(peer, tlli_info); |
| 2263 | found += 1; |
| 2264 | } |
| 2265 | |
| 2266 | if (!found && argc >= 2) { |
| 2267 | vty_out(vty, "Didn't find TLLI entry with %s %s%s", |
| 2268 | argv[1], argv[2], VTY_NEWLINE); |
| 2269 | } |
| 2270 | |
| 2271 | return CMD_SUCCESS; |
| 2272 | } |
| 2273 | |