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