Harald Welte | d1991e7 | 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 | 2ea8812 | 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 | d1991e7 | 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 | 0e3e88e | 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 | d1991e7 | 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 | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 17 | * |
Harald Welte | 0e3e88e | 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 | d1991e7 | 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 | f2fdefe | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 31 | #include <arpa/inet.h> |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 32 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 33 | #include <osmocom/core/talloc.h> |
| 34 | #include <osmocom/core/select.h> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 35 | #include <osmocom/core/rate_ctr.h> |
| 36 | |
| 37 | #include <osmocom/vty/misc.h> |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 38 | |
Harald Welte | cfb6b28 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 39 | #include <osmocom/gprs/gprs_ns.h> |
| 40 | #include <osmocom/gprs/gprs_bssgp.h> |
| 41 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 42 | #include <openbsc/signal.h> |
| 43 | #include <openbsc/debug.h> |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 44 | #include <openbsc/gb_proxy.h> |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 45 | |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 46 | #include <openbsc/gprs_llc.h> |
| 47 | #include <openbsc/gsm_04_08.h> |
| 48 | #include <openbsc/gsm_04_08_gprs.h> |
| 49 | |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 50 | enum gbprox_global_ctr { |
| 51 | GBPROX_GLOB_CTR_INV_BVCI, |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 52 | GBPROX_GLOB_CTR_INV_LAI, |
| 53 | GBPROX_GLOB_CTR_INV_RAI, |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 54 | GBPROX_GLOB_CTR_INV_NSEI, |
| 55 | GBPROX_GLOB_CTR_PROTO_ERR_BSS, |
| 56 | GBPROX_GLOB_CTR_PROTO_ERR_SGSN, |
| 57 | GBPROX_GLOB_CTR_NOT_SUPPORTED_BSS, |
| 58 | GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN, |
| 59 | GBPROX_GLOB_CTR_RESTART_RESET_SGSN, |
| 60 | GBPROX_GLOB_CTR_TX_ERR_SGSN, |
| 61 | GBPROX_GLOB_CTR_OTHER_ERR, |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 62 | GBPROX_GLOB_CTR_RAID_PATCHED_BSS, |
| 63 | GBPROX_GLOB_CTR_RAID_PATCHED_SGSN, |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame^] | 64 | GBPROX_GLOB_CTR_PATCH_CRYPT_ERR, |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 65 | GBPROX_GLOB_CTR_PATCH_ERR, |
Jacob Erlbeck | 174bad4 | 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 | 9d7268a | 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 | 174bad4 | 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 | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 80 | { "raid-mod.bss", "RAID patched (BSS )" }, |
| 81 | { "raid-mod.sgsn", "RAID patched (SGSN)" }, |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame^] | 82 | { "mod-crypt-err", "Patch error: encrypted " }, |
| 83 | { "mod-err", "Patch error: other " }, |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static const struct rate_ctr_group_desc global_ctrg_desc = { |
| 87 | .group_name_prefix = "gbproxy.global", |
| 88 | .group_description = "GBProxy Global Statistics", |
| 89 | .num_ctr = ARRAY_SIZE(global_ctr_description), |
| 90 | .ctr_desc = global_ctr_description, |
| 91 | }; |
| 92 | |
| 93 | static struct rate_ctr_group *global_ctrg = NULL; |
| 94 | |
| 95 | static struct rate_ctr_group *get_global_ctrg() |
| 96 | { |
| 97 | if (global_ctrg) |
| 98 | return global_ctrg; |
| 99 | |
| 100 | global_ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0); |
| 101 | return global_ctrg; |
| 102 | } |
| 103 | |
| 104 | enum gbprox_peer_ctr { |
| 105 | GBPROX_PEER_CTR_BLOCKED, |
| 106 | GBPROX_PEER_CTR_UNBLOCKED, |
| 107 | GBPROX_PEER_CTR_DROPPED, |
| 108 | GBPROX_PEER_CTR_INV_NSEI, |
| 109 | GBPROX_PEER_CTR_TX_ERR, |
| 110 | }; |
| 111 | |
| 112 | static const struct rate_ctr_desc peer_ctr_description[] = { |
| 113 | { "blocked", "BVC Block " }, |
| 114 | { "unblocked", "BVC Unblock " }, |
| 115 | { "dropped", "BVC blocked, dropped packet " }, |
| 116 | { "inv-nsei", "NSEI mismatch " }, |
| 117 | { "tx-err", "NS Transmission error " }, |
| 118 | }; |
| 119 | |
| 120 | static const struct rate_ctr_group_desc peer_ctrg_desc = { |
| 121 | .group_name_prefix = "gbproxy.peer", |
| 122 | .group_description = "GBProxy Peer Statistics", |
| 123 | .num_ctr = ARRAY_SIZE(peer_ctr_description), |
| 124 | .ctr_desc = peer_ctr_description, |
| 125 | }; |
| 126 | |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 127 | static struct gbprox_patch_state { |
| 128 | int local_mnc; |
| 129 | int local_mcc; |
| 130 | } gbprox_patch_state = {0}; |
| 131 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 132 | struct gbprox_peer { |
| 133 | struct llist_head list; |
| 134 | |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 135 | /* NSEI of the peer entity */ |
| 136 | uint16_t nsei; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 137 | |
| 138 | /* BVCI used for Point-to-Point to this peer */ |
| 139 | uint16_t bvci; |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 140 | int blocked; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 141 | |
| 142 | /* Routeing Area that this peer is part of (raw 04.08 encoding) */ |
| 143 | uint8_t ra[6]; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 144 | |
| 145 | /* Counter */ |
| 146 | struct rate_ctr_group *ctrg; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | /* Linked list of all Gb peers (except SGSN) */ |
| 150 | static LLIST_HEAD(gbprox_bts_peers); |
| 151 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 152 | /* Find the gbprox_peer by its BVCI */ |
| 153 | static struct gbprox_peer *peer_by_bvci(uint16_t bvci) |
| 154 | { |
| 155 | struct gbprox_peer *peer; |
| 156 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
| 157 | if (peer->bvci == bvci) |
| 158 | return peer; |
| 159 | } |
| 160 | return NULL; |
| 161 | } |
| 162 | |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 163 | /* Find the gbprox_peer by its NSEI */ |
| 164 | static struct gbprox_peer *peer_by_nsei(uint16_t nsei) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 165 | { |
| 166 | struct gbprox_peer *peer; |
| 167 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 168 | if (peer->nsei == nsei) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 169 | return peer; |
| 170 | } |
| 171 | return NULL; |
| 172 | } |
| 173 | |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 174 | /* look-up a peer by its Routeing Area Identification (RAI) */ |
| 175 | static struct gbprox_peer *peer_by_rai(const uint8_t *ra) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 176 | { |
| 177 | struct gbprox_peer *peer; |
| 178 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 179 | if (!memcmp(peer->ra, ra, 6)) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 180 | return peer; |
| 181 | } |
| 182 | return NULL; |
| 183 | } |
| 184 | |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 185 | /* look-up a peer by its Location Area Identification (LAI) */ |
| 186 | static struct gbprox_peer *peer_by_lai(const uint8_t *la) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 187 | { |
| 188 | struct gbprox_peer *peer; |
| 189 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 190 | if (!memcmp(peer->ra, la, 5)) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 191 | return peer; |
| 192 | } |
| 193 | return NULL; |
| 194 | } |
| 195 | |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 196 | static int check_peer_nsei(struct gbprox_peer *peer, uint16_t nsei) |
| 197 | { |
| 198 | if (peer->nsei != nsei) { |
| 199 | LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI " |
| 200 | "BVCI=%u via NSEI=%u (expected NSEI=%u)\n", |
| 201 | peer->bvci, nsei, peer->nsei); |
| 202 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]); |
Jacob Erlbeck | d0a9b50 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 203 | return 0; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Jacob Erlbeck | d0a9b50 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 206 | return 1; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 209 | static struct gbprox_peer *peer_alloc(uint16_t bvci) |
| 210 | { |
| 211 | struct gbprox_peer *peer; |
| 212 | |
| 213 | peer = talloc_zero(tall_bsc_ctx, struct gbprox_peer); |
| 214 | if (!peer) |
| 215 | return NULL; |
| 216 | |
| 217 | peer->bvci = bvci; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 218 | peer->ctrg = rate_ctr_group_alloc(peer, &peer_ctrg_desc, bvci); |
| 219 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 220 | llist_add(&peer->list, &gbprox_bts_peers); |
| 221 | |
| 222 | return peer; |
| 223 | } |
| 224 | |
| 225 | static void peer_free(struct gbprox_peer *peer) |
| 226 | { |
Jacob Erlbeck | b44b873 | 2013-10-23 11:24:16 +0200 | [diff] [blame] | 227 | rate_ctr_group_free(peer->ctrg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 228 | llist_del(&peer->list); |
| 229 | talloc_free(peer); |
| 230 | } |
| 231 | |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 232 | /* FIXME: this needs to go to libosmocore/msgb.c */ |
| 233 | static struct msgb *msgb_copy(const struct msgb *msg, const char *name) |
| 234 | { |
Harald Welte | 14f112a | 2012-06-16 16:09:52 +0800 | [diff] [blame] | 235 | struct libgb_msgb_cb *old_cb, *new_cb; |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 236 | struct msgb *new_msg; |
| 237 | |
Holger Hans Peter Freyther | 337f4d5 | 2010-06-08 18:14:37 +0800 | [diff] [blame] | 238 | new_msg = msgb_alloc(msg->data_len, name); |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 239 | if (!new_msg) |
| 240 | return NULL; |
| 241 | |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 242 | /* copy data */ |
Holger Hans Peter Freyther | 337f4d5 | 2010-06-08 18:14:37 +0800 | [diff] [blame] | 243 | memcpy(new_msg->_data, msg->_data, new_msg->data_len); |
| 244 | |
| 245 | /* copy header */ |
| 246 | new_msg->len = msg->len; |
| 247 | new_msg->data += msg->data - msg->_data; |
| 248 | new_msg->head += msg->head - msg->_data; |
| 249 | new_msg->tail += msg->tail - msg->_data; |
| 250 | |
| 251 | new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data); |
| 252 | new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data); |
| 253 | new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data); |
| 254 | new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data); |
| 255 | |
| 256 | /* copy GB specific data */ |
Harald Welte | 14f112a | 2012-06-16 16:09:52 +0800 | [diff] [blame] | 257 | old_cb = LIBGB_MSGB_CB(msg); |
| 258 | new_cb = LIBGB_MSGB_CB(new_msg); |
Holger Hans Peter Freyther | 337f4d5 | 2010-06-08 18:14:37 +0800 | [diff] [blame] | 259 | |
| 260 | new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data); |
| 261 | new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data); |
| 262 | |
Harald Welte | 7b7eaa9 | 2011-02-06 17:17:05 +0100 | [diff] [blame] | 263 | /* bssgp_cell_id is a pointer into the old msgb, so we need to make |
| 264 | * it a pointer into the new msgb */ |
| 265 | new_cb->bssgp_cell_id = new_msg->_data + (old_cb->bssgp_cell_id - msg->_data); |
Holger Hans Peter Freyther | 337f4d5 | 2010-06-08 18:14:37 +0800 | [diff] [blame] | 266 | new_cb->nsei = old_cb->nsei; |
| 267 | new_cb->bvci = old_cb->bvci; |
| 268 | new_cb->tlli = old_cb->tlli; |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 269 | |
| 270 | return new_msg; |
| 271 | } |
| 272 | |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 273 | /* strip off the NS header */ |
| 274 | static void strip_ns_hdr(struct msgb *msg) |
| 275 | { |
| 276 | int strip_len = msgb_bssgph(msg) - msg->data; |
| 277 | msgb_pull(msg, strip_len); |
| 278 | } |
| 279 | |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame^] | 280 | /* check whether patching is enabled at this level */ |
| 281 | static int patching_is_enabled(enum gbproxy_patch_mode need_at_least) |
| 282 | { |
| 283 | enum gbproxy_patch_mode patch_mode = gbcfg.patch_mode; |
| 284 | if (patch_mode == GBPROX_PATCH_DEFAULT) |
| 285 | patch_mode = GBPROX_PATCH_LLC; |
| 286 | |
| 287 | return need_at_least <= patch_mode; |
| 288 | } |
| 289 | |
| 290 | /* check whether patching is enabled at this level */ |
| 291 | static int patching_is_required(enum gbproxy_patch_mode need_at_least) |
| 292 | { |
| 293 | return need_at_least <= gbcfg.patch_mode; |
| 294 | } |
| 295 | |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 296 | /* patch RA identifier in place, update peer accordingly */ |
| 297 | static void gbprox_patch_raid(uint8_t *raid_enc, struct gbprox_patch_state *state, |
| 298 | int to_bss, const char *log_text) |
| 299 | { |
| 300 | const int old_local_mcc = state->local_mcc; |
| 301 | const int old_local_mnc = state->local_mnc; |
| 302 | int old_mcc; |
| 303 | int old_mnc; |
| 304 | struct gprs_ra_id raid; |
| 305 | |
| 306 | gsm48_parse_ra(&raid, raid_enc); |
| 307 | |
| 308 | old_mcc = raid.mcc; |
| 309 | old_mnc = raid.mnc; |
| 310 | |
| 311 | if (!to_bss) { |
| 312 | /* BSS -> SGSN */ |
| 313 | /* save BSS side MCC/MNC */ |
| 314 | if (!gbcfg.core_mcc || raid.mcc == gbcfg.core_mcc) { |
| 315 | state->local_mcc = 0; |
| 316 | } else { |
| 317 | state->local_mcc = raid.mcc; |
| 318 | raid.mcc = gbcfg.core_mcc; |
| 319 | } |
| 320 | |
| 321 | if (!gbcfg.core_mnc || raid.mnc == gbcfg.core_mnc) { |
| 322 | state->local_mnc = 0; |
| 323 | } else { |
| 324 | state->local_mnc = raid.mnc; |
| 325 | raid.mnc = gbcfg.core_mnc; |
| 326 | } |
| 327 | } else { |
| 328 | /* SGSN -> BSS */ |
| 329 | if (state->local_mcc) |
| 330 | raid.mcc = state->local_mcc; |
| 331 | |
| 332 | if (state->local_mnc) |
| 333 | raid.mnc = state->local_mnc; |
| 334 | } |
| 335 | |
| 336 | if (old_local_mcc != state->local_mcc || |
| 337 | old_local_mnc != state->local_mnc) |
| 338 | LOGP(DGPRS, LOGL_NOTICE, |
| 339 | "Patching RAID %sactivated, msg: %s, " |
| 340 | "local: %d-%d, core: %d-%d, to %s\n", |
| 341 | state->local_mcc || state->local_mnc ? |
| 342 | "" : "de", |
| 343 | log_text, |
| 344 | state->local_mcc, state->local_mnc, |
| 345 | gbcfg.core_mcc, gbcfg.core_mnc, |
| 346 | to_bss ? "BSS" : "SGSN"); |
| 347 | |
| 348 | if (state->local_mcc || state->local_mnc) { |
| 349 | enum gbprox_global_ctr counter = |
| 350 | to_bss ? |
| 351 | GBPROX_GLOB_CTR_RAID_PATCHED_SGSN : |
| 352 | GBPROX_GLOB_CTR_RAID_PATCHED_BSS; |
| 353 | |
| 354 | LOGP(DGPRS, LOGL_DEBUG, |
| 355 | "Patching %s to %s: " |
| 356 | "%d-%d-%d-%d -> %d-%d-%d-%d\n", |
| 357 | log_text, |
| 358 | to_bss ? "BSS" : "SGSN", |
| 359 | old_mcc, old_mnc, raid.lac, raid.rac, |
| 360 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 361 | |
| 362 | gsm48_construct_ra(raid_enc, &raid); |
| 363 | rate_ctr_inc(&get_global_ctrg()->ctr[counter]); |
| 364 | } |
| 365 | } |
| 366 | |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame^] | 367 | static int gbprox_patch_gmm_attach_req(struct msgb *msg, |
| 368 | uint8_t *data, size_t data_len, |
| 369 | struct gbprox_patch_state *state, |
| 370 | int to_bss, int *len_change) |
| 371 | { |
| 372 | /* Check minimum length, always includes the RAI */ |
| 373 | if (data_len < 23) |
| 374 | return 0; |
| 375 | |
| 376 | /* Skip MS network capability */ |
| 377 | if (data[0] < 1 || data[0] > 2) |
| 378 | /* invalid */ |
| 379 | return 0; |
| 380 | data_len -= data[0] + 1; |
| 381 | data += data[0] + 1; |
| 382 | |
| 383 | /* Skip Attach type */ |
| 384 | /* Skip Ciphering key sequence number */ |
| 385 | /* Skip DRX parameter */ |
| 386 | data_len -= 3; |
| 387 | data += 3; |
| 388 | |
| 389 | /* Skip Mobile identity */ |
| 390 | if (data[0] < 5 || data[0] > 8) |
| 391 | /* invalid */ |
| 392 | return 0; |
| 393 | data_len -= data[0] + 1; |
| 394 | data += data[0] + 1; |
| 395 | |
| 396 | gbprox_patch_raid(data, state, to_bss, "LLC/ATTACH_REQ"); |
| 397 | |
| 398 | return 1; |
| 399 | } |
| 400 | |
| 401 | static int gbprox_patch_gmm_attach_ack(struct msgb *msg, |
| 402 | uint8_t *data, size_t data_len, |
| 403 | struct gbprox_patch_state *state, |
| 404 | int to_bss, int *len_change) |
| 405 | { |
| 406 | /* Check minimum length, always includes the RAI */ |
| 407 | if (data_len < 9) |
| 408 | return 0; |
| 409 | |
| 410 | /* Skip Attach result */ |
| 411 | /* Skip Force to standby */ |
| 412 | /* Skip Periodic RA update timer */ |
| 413 | /* Skip Radio priority for SMS */ |
| 414 | /* Skip Spare half octet */ |
| 415 | data_len -= 3; |
| 416 | data += 3; |
| 417 | |
| 418 | gbprox_patch_raid(data, state, to_bss, "LLC/ATTACH_ACK"); |
| 419 | |
| 420 | return 1; |
| 421 | } |
| 422 | |
| 423 | static int gbprox_patch_gmm_ra_upd_req(struct msgb *msg, |
| 424 | uint8_t *data, size_t data_len, |
| 425 | struct gbprox_patch_state *state, |
| 426 | int to_bss, int *len_change) |
| 427 | { |
| 428 | /* Check minimum length, always includes the RAI */ |
| 429 | if (data_len < 13) |
| 430 | return 0; |
| 431 | |
| 432 | /* Skip Update type */ |
| 433 | /* Skip GPRS ciphering key sequence number */ |
| 434 | data_len -= 1; |
| 435 | data += 1; |
| 436 | |
| 437 | gbprox_patch_raid(data, state, to_bss, "LLC/RA_UPD_REQ"); |
| 438 | |
| 439 | return 1; |
| 440 | } |
| 441 | |
| 442 | static int gbprox_patch_gmm_ra_upd_ack(struct msgb *msg, |
| 443 | uint8_t *data, size_t data_len, |
| 444 | struct gbprox_patch_state *state, |
| 445 | int to_bss, int *len_change) |
| 446 | { |
| 447 | /* Check minimum length, always includes the RAI */ |
| 448 | if (data_len < 8) |
| 449 | return 0; |
| 450 | |
| 451 | /* Skip Force to standby */ |
| 452 | /* Skip Update result */ |
| 453 | /* Skip Periodic RA update timer */ |
| 454 | data_len -= 2; |
| 455 | data += 2; |
| 456 | |
| 457 | gbprox_patch_raid(data, state, to_bss, "LLC/RA_UPD_ACK"); |
| 458 | |
| 459 | return 1; |
| 460 | } |
| 461 | |
| 462 | static int gbprox_patch_gmm_ptmsi_reall_cmd(struct msgb *msg, |
| 463 | uint8_t *data, size_t data_len, |
| 464 | struct gbprox_patch_state *state, |
| 465 | int to_bss, int *len_change) |
| 466 | { |
| 467 | /* Check minimum length, always includes the RAI */ |
| 468 | if (data_len < 12) |
| 469 | return 0; |
| 470 | |
| 471 | /* Skip Allocated P-TMSI */ |
| 472 | if (data[0] != 5) |
| 473 | /* invalid */ |
| 474 | return 0; |
| 475 | data_len -= 6; |
| 476 | data += 6; |
| 477 | |
| 478 | gbprox_patch_raid(data, state, to_bss, "LLC/PTMSI_REALL_CMD"); |
| 479 | |
| 480 | return 1; |
| 481 | } |
| 482 | |
| 483 | static int gbprox_patch_dtap(struct msgb *msg, uint8_t *data, size_t data_len, |
| 484 | struct gbprox_patch_state *state, int to_bss, |
| 485 | int *len_change) |
| 486 | { |
| 487 | struct gsm48_hdr *g48h; |
| 488 | |
| 489 | *len_change = 0; |
| 490 | |
| 491 | if (data_len < 2) |
| 492 | return 0; |
| 493 | |
| 494 | g48h = (struct gsm48_hdr *)data; |
| 495 | |
| 496 | data += sizeof(struct gsm48_hdr); |
| 497 | data_len -= sizeof(struct gsm48_hdr); |
| 498 | |
| 499 | if ((g48h->proto_discr & 0x0f) != GSM48_PDISC_MM_GPRS && |
| 500 | (g48h->proto_discr & 0x0f) != GSM48_PDISC_SM_GPRS) |
| 501 | return 0; |
| 502 | |
| 503 | switch (g48h->msg_type) { |
| 504 | case GSM48_MT_GMM_ATTACH_REQ: |
| 505 | return gbprox_patch_gmm_attach_req(msg, data, data_len, |
| 506 | state, to_bss, len_change); |
| 507 | |
| 508 | case GSM48_MT_GMM_ATTACH_ACK: |
| 509 | if (!patching_is_enabled(GBPROX_PATCH_LLC_ATTACH)) |
| 510 | break; |
| 511 | return gbprox_patch_gmm_attach_ack(msg, data, data_len, |
| 512 | state, to_bss, len_change); |
| 513 | |
| 514 | case GSM48_MT_GMM_RA_UPD_REQ: |
| 515 | if (!patching_is_enabled(GBPROX_PATCH_LLC_GMM)) |
| 516 | break; |
| 517 | return gbprox_patch_gmm_ra_upd_req(msg, data, data_len, |
| 518 | state, to_bss, len_change); |
| 519 | |
| 520 | case GSM48_MT_GMM_RA_UPD_ACK: |
| 521 | if (!patching_is_enabled(GBPROX_PATCH_LLC_GMM)) |
| 522 | break; |
| 523 | return gbprox_patch_gmm_ra_upd_ack(msg, data, data_len, |
| 524 | state, to_bss, len_change); |
| 525 | |
| 526 | case GSM48_MT_GMM_PTMSI_REALL_CMD: |
| 527 | if (!patching_is_enabled(GBPROX_PATCH_LLC_GMM)) |
| 528 | break; |
| 529 | return gbprox_patch_gmm_ptmsi_reall_cmd(msg, data, data_len, |
| 530 | state, to_bss, len_change); |
| 531 | |
| 532 | default: |
| 533 | break; |
| 534 | }; |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | static void gbprox_patch_llc(struct msgb *msg, uint8_t *llc, size_t llc_len, |
| 540 | struct gbprox_patch_state *state, int to_bss) |
| 541 | { |
| 542 | struct gprs_llc_hdr_parsed ghp = {0}; |
| 543 | int rc; |
| 544 | uint8_t *data; |
| 545 | size_t data_len; |
| 546 | int fcs; |
| 547 | int len_change = 0; |
| 548 | const char *err_info = NULL; |
| 549 | int err_ctr = -1; |
| 550 | |
| 551 | /* parse LLC */ |
| 552 | rc = gprs_llc_hdr_parse(&ghp, llc, llc_len); |
| 553 | gprs_llc_hdr_dump(&ghp); |
| 554 | if (rc != 0) { |
| 555 | LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n"); |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | fcs = gprs_llc_fcs(llc, ghp.crc_length); |
| 560 | LOGP(DLLC, LOGL_DEBUG, "Got LLC message, CRC: %06x (computed %06x)\n", |
| 561 | ghp.fcs, fcs); |
| 562 | |
| 563 | if (!ghp.data) |
| 564 | return; |
| 565 | |
| 566 | if (ghp.sapi != GPRS_SAPI_GMM) |
| 567 | return; |
| 568 | |
| 569 | if (ghp.cmd != GPRS_LLC_UI) |
| 570 | return; |
| 571 | |
| 572 | if (ghp.is_encrypted) { |
| 573 | if (patching_is_required(GBPROX_PATCH_LLC_ATTACH)) { |
| 574 | /* Patching LLC messages has been requested explicitly, |
| 575 | * but the message (including the type) is encrypted, |
| 576 | * so we possibly fail to patch the LLC part of the |
| 577 | * message. */ |
| 578 | |
| 579 | err_info = "GMM message is encrypted"; |
| 580 | err_ctr = GBPROX_GLOB_CTR_PATCH_CRYPT_ERR; |
| 581 | goto patch_error; |
| 582 | } |
| 583 | |
| 584 | return; |
| 585 | } |
| 586 | |
| 587 | /* fix DTAP GMM/GSM */ |
| 588 | data = ghp.data; |
| 589 | data_len = ghp.data_len; |
| 590 | |
| 591 | rc = gbprox_patch_dtap(msg, data, data_len, state, to_bss, &len_change); |
| 592 | |
| 593 | if (rc > 0) { |
| 594 | llc_len += len_change; |
| 595 | ghp.crc_length += len_change; |
| 596 | |
| 597 | /* Fix LLC IE len */ |
| 598 | if (llc[-2] == BSSGP_IE_LLC_PDU && llc[-1] & 0x80) { |
| 599 | /* most probably a one byte length */ |
| 600 | if (llc_len > 127) { |
| 601 | err_info = "Cannot increase size"; |
| 602 | err_ctr = GBPROX_GLOB_CTR_PATCH_ERR; |
| 603 | goto patch_error; |
| 604 | } |
| 605 | llc[-1] = llc_len | 0x80; |
| 606 | } else { |
| 607 | llc[-2] = (llc_len >> 8) & 0x7f; |
| 608 | llc[-1] = llc_len & 0xff; |
| 609 | } |
| 610 | |
| 611 | /* Fix FCS */ |
| 612 | fcs = gprs_llc_fcs(llc, ghp.crc_length); |
| 613 | LOGP(DLLC, LOGL_DEBUG, "Updated LLC message, CRC: %06x -> %06x\n", |
| 614 | ghp.fcs, fcs); |
| 615 | |
| 616 | llc[llc_len - 3] = fcs & 0xff; |
| 617 | llc[llc_len - 2] = (fcs >> 8) & 0xff; |
| 618 | llc[llc_len - 1] = (fcs >> 16) & 0xff; |
| 619 | } |
| 620 | |
| 621 | return; |
| 622 | |
| 623 | patch_error: |
| 624 | OSMO_ASSERT(err_ctr >= 0); |
| 625 | rate_ctr_inc(&get_global_ctrg()->ctr[err_ctr]); |
| 626 | LOGP(DGPRS, LOGL_ERROR, |
| 627 | "Failed to patch BSSGP/GMM message as requested: %s.\n", err_info); |
| 628 | |
| 629 | return; |
| 630 | } |
| 631 | |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 632 | /* patch BSSGP message to use core_mcc/mnc on the SGSN side */ |
| 633 | static void gbprox_patch_bssgp_message(struct msgb *msg, int to_bss) |
| 634 | { |
| 635 | struct bssgp_normal_hdr *bgph; |
| 636 | struct bssgp_ud_hdr *budh; |
| 637 | struct tlv_parsed tp; |
| 638 | uint8_t pdu_type; |
| 639 | struct gbprox_patch_state *state = &gbprox_patch_state; |
| 640 | uint8_t *data; |
| 641 | size_t data_len; |
| 642 | |
| 643 | if (!gbcfg.core_mcc && !gbcfg.core_mnc) |
| 644 | return; |
| 645 | |
| 646 | bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 647 | budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); |
| 648 | pdu_type = bgph->pdu_type; |
| 649 | |
| 650 | if (to_bss && !state->local_mcc && !state->local_mnc) |
| 651 | return; |
| 652 | |
| 653 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 654 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
| 655 | data = budh->data; |
| 656 | data_len = msgb_bssgp_len(msg) - sizeof(*budh); |
| 657 | } else { |
| 658 | data = bgph->data; |
| 659 | data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
| 660 | } |
| 661 | |
| 662 | bssgp_tlv_parse(&tp, data, data_len); |
| 663 | |
| 664 | if (TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 665 | gbprox_patch_raid((uint8_t *)TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA), |
| 666 | state, to_bss, "ROUTING_AREA"); |
| 667 | |
| 668 | if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) |
| 669 | gbprox_patch_raid((uint8_t *)TLVP_VAL(&tp, BSSGP_IE_CELL_ID), |
| 670 | state, to_bss, "CELL_ID"); |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame^] | 671 | |
| 672 | if (TLVP_PRESENT(&tp, BSSGP_IE_LLC_PDU) && |
| 673 | patching_is_enabled(GBPROX_PATCH_LLC_ATTACH_REQ)) { |
| 674 | uint8_t *llc = (uint8_t *)TLVP_VAL(&tp, BSSGP_IE_LLC_PDU); |
| 675 | size_t llc_len = TLVP_LEN(&tp, BSSGP_IE_LLC_PDU); |
| 676 | gbprox_patch_llc(msg, llc, llc_len, state, to_bss); |
| 677 | /* Note that the tp struct might contain invalid pointers here |
| 678 | * if the LLC field has changed its size */ |
| 679 | } |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 680 | } |
| 681 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 682 | /* feed a message down the NS-VC associated with the specified peer */ |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 683 | static int gbprox_relay2sgsn(struct msgb *old_msg, uint16_t ns_bvci) |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 684 | { |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 685 | /* create a copy of the message so the old one can |
| 686 | * be free()d safely when we return from gbprox_rcvmsg() */ |
| 687 | struct msgb *msg = msgb_copy(old_msg, "msgb_relay2sgsn"); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 688 | int rc; |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 689 | |
Harald Welte | b8da061 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 690 | DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n", |
Harald Welte | e661b27 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 691 | msgb_nsei(msg), ns_bvci, gbcfg.nsip_sgsn_nsei); |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 692 | |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 693 | msgb_bvci(msg) = ns_bvci; |
| 694 | msgb_nsei(msg) = gbcfg.nsip_sgsn_nsei; |
| 695 | |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 696 | strip_ns_hdr(msg); |
| 697 | |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 698 | gbprox_patch_bssgp_message(msg, 0); |
| 699 | |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 700 | rc = gprs_ns_sendmsg(bssgp_nsi, msg); |
| 701 | if (rc < 0) |
| 702 | rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]); |
| 703 | |
| 704 | return rc; |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 705 | } |
| 706 | |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 707 | /* feed a message down the NS-VC associated with the specified peer */ |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 708 | static int gbprox_relay2peer(struct msgb *old_msg, struct gbprox_peer *peer, |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 709 | uint16_t ns_bvci) |
| 710 | { |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 711 | /* create a copy of the message so the old one can |
| 712 | * be free()d safely when we return from gbprox_rcvmsg() */ |
| 713 | struct msgb *msg = msgb_copy(old_msg, "msgb_relay2peer"); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 714 | int rc; |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 715 | |
Harald Welte | 0cc900d | 2010-05-13 10:34:56 +0200 | [diff] [blame] | 716 | DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 717 | msgb_nsei(msg), ns_bvci, peer->nsei); |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 718 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 719 | msgb_bvci(msg) = ns_bvci; |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 720 | msgb_nsei(msg) = peer->nsei; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 721 | |
Harald Welte | 0cc900d | 2010-05-13 10:34:56 +0200 | [diff] [blame] | 722 | /* Strip the old NS header, it will be replaced with a new one */ |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 723 | strip_ns_hdr(msg); |
| 724 | |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 725 | rc = gprs_ns_sendmsg(bssgp_nsi, msg); |
| 726 | if (rc < 0) |
| 727 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]); |
| 728 | |
| 729 | return rc; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 730 | } |
| 731 | |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 732 | static int block_unblock_peer(uint16_t ptp_bvci, uint8_t pdu_type) |
| 733 | { |
| 734 | struct gbprox_peer *peer; |
| 735 | |
| 736 | peer = peer_by_bvci(ptp_bvci); |
| 737 | if (!peer) { |
| 738 | LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n", |
| 739 | ptp_bvci); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 740 | rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 741 | return -ENOENT; |
| 742 | } |
| 743 | |
| 744 | switch (pdu_type) { |
| 745 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 746 | peer->blocked = 1; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 747 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 748 | break; |
| 749 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 750 | peer->blocked = 0; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 751 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 752 | break; |
| 753 | default: |
| 754 | break; |
| 755 | } |
| 756 | return 0; |
| 757 | } |
| 758 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 759 | /* Send a message to a peer identified by ptp_bvci but using ns_bvci |
| 760 | * in the NS hdr */ |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 761 | static int gbprox_relay2bvci(struct msgb *msg, uint16_t ptp_bvci, |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 762 | uint16_t ns_bvci) |
| 763 | { |
| 764 | struct gbprox_peer *peer; |
| 765 | |
| 766 | peer = peer_by_bvci(ptp_bvci); |
Harald Welte | 0358aae | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 767 | if (!peer) { |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 768 | LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n", |
Harald Welte | 0358aae | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 769 | ptp_bvci); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 770 | rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 771 | return -ENOENT; |
Harald Welte | 0358aae | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 772 | } |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 773 | |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 774 | return gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 775 | } |
| 776 | |
Harald Welte | a62e9fe | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 777 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 778 | { |
| 779 | return 0; |
| 780 | } |
| 781 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 782 | /* Receive an incoming signalling message from a BSS-side NS-VC */ |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 783 | static int gbprox_rx_sig_from_bss(struct msgb *msg, uint16_t nsei, |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 784 | uint16_t ns_bvci) |
| 785 | { |
Harald Welte | 2ac2e91 | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 786 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 787 | struct tlv_parsed tp; |
| 788 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | 2ac2e91 | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 789 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 790 | struct gbprox_peer *from_peer; |
Harald Welte | e49c829 | 2010-05-01 12:10:57 +0200 | [diff] [blame] | 791 | struct gprs_ra_id raid; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 792 | |
Harald Welte | 4127072 | 2011-02-06 17:13:12 +0100 | [diff] [blame] | 793 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 794 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 795 | nsei, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 796 | return -EINVAL; |
| 797 | } |
| 798 | |
| 799 | /* we actually should never see those two for BVCI == 0, but double-check |
| 800 | * just to make sure */ |
| 801 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 802 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 803 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 804 | "signalling\n", nsei); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 805 | return -EINVAL; |
| 806 | } |
| 807 | |
| 808 | bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 809 | |
| 810 | switch (pdu_type) { |
| 811 | case BSSGP_PDUT_SUSPEND: |
| 812 | case BSSGP_PDUT_RESUME: |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 813 | /* We implement RAI snooping during SUSPEND/RESUME, since it |
| 814 | * establishes a relationsip between BVCI/peer and the routeing |
| 815 | * area identification. The snooped information is then used |
| 816 | * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct |
| 817 | * BSSGP */ |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 818 | if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 819 | goto err_mand_ie; |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 820 | from_peer = peer_by_nsei(nsei); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 821 | if (!from_peer) |
| 822 | goto err_no_peer; |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 823 | memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA), |
| 824 | sizeof(from_peer->ra)); |
Harald Welte | f2fdefe | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 825 | gsm48_parse_ra(&raid, from_peer->ra); |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 826 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME " |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 827 | "RAI snooping: RAI %u-%u-%u-%u behind BVCI=%u\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 828 | nsei, raid.mcc, raid.mnc, raid.lac, |
| 829 | raid.rac , from_peer->bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 830 | /* FIXME: This only supports one BSS per RA */ |
| 831 | break; |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 832 | case BSSGP_PDUT_BVC_RESET: |
| 833 | /* If we receive a BVC reset on the signalling endpoint, we |
| 834 | * don't want the SGSN to reset, as the signalling endpoint |
| 835 | * is common for all point-to-point BVCs (and thus all BTS) */ |
| 836 | if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 837 | uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | be6e25a | 2010-05-12 00:20:41 +0200 | [diff] [blame] | 838 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 839 | nsei, bvci); |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 840 | if (bvci == 0) { |
| 841 | /* FIXME: only do this if SGSN is alive! */ |
Harald Welte | b8da061 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 842 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 843 | "BVC RESET ACK of BVCI=0\n", nsei); |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 844 | return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK, |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 845 | nsei, 0, ns_bvci); |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 846 | } |
| 847 | from_peer = peer_by_bvci(bvci); |
| 848 | if (!from_peer) { |
Harald Welte | 0358aae | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 849 | /* if a PTP-BVC is reset, and we don't know that |
| 850 | * PTP-BVCI yet, we should allocate a new peer */ |
| 851 | LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 852 | "BVCI=%u via NSEI=%u\n", bvci, nsei); |
Harald Welte | 0358aae | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 853 | from_peer = peer_alloc(bvci); |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 854 | from_peer->nsei = nsei; |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 855 | } |
Jacob Erlbeck | d0a9b50 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 856 | |
| 857 | if (!check_peer_nsei(from_peer, nsei)) |
| 858 | from_peer->nsei = nsei; |
| 859 | |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 860 | if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) { |
| 861 | struct gprs_ra_id raid; |
| 862 | /* We have a Cell Identifier present in this |
| 863 | * PDU, this means we can extend our local |
| 864 | * state information about this particular cell |
| 865 | * */ |
| 866 | memcpy(from_peer->ra, |
| 867 | TLVP_VAL(&tp, BSSGP_IE_CELL_ID), |
| 868 | sizeof(from_peer->ra)); |
| 869 | gsm48_parse_ra(&raid, from_peer->ra); |
| 870 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 871 | "Cell ID %u-%u-%u-%u\n", nsei, |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 872 | bvci, raid.mcc, raid.mnc, raid.lac, |
| 873 | raid.rac); |
| 874 | } |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 875 | } |
| 876 | break; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 877 | } |
| 878 | |
Harald Welte | b8da061 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 879 | /* Normally, we can simply pass on all signalling messages from BSS to |
| 880 | * SGSN */ |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 881 | return gbprox_relay2sgsn(msg, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 882 | err_no_peer: |
Jacob Erlbeck | 700b873 | 2013-10-18 13:04:48 +0200 | [diff] [blame] | 883 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 884 | nsei); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 885 | rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_INV_NSEI]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 886 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 887 | err_mand_ie: |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 888 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 889 | nsei); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 890 | rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 891 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | /* Receive paging request from SGSN, we need to relay to proper BSS */ |
| 895 | static int gbprox_rx_paging(struct msgb *msg, struct tlv_parsed *tp, |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 896 | uint32_t nsei, uint16_t ns_bvci) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 897 | { |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 898 | struct gbprox_peer *peer = NULL; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 899 | int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 900 | |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 901 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 902 | nsei); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 903 | if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 904 | uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 905 | LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n", |
| 906 | bvci); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 907 | errctr = GBPROX_GLOB_CTR_OTHER_ERR; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 908 | } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) { |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 909 | peer = peer_by_rai(TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA)); |
| 910 | LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n", |
Holger Hans Peter Freyther | 98b82c1 | 2010-06-08 16:30:24 +0800 | [diff] [blame] | 911 | peer ? peer->bvci : -1); |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 912 | errctr = GBPROX_GLOB_CTR_INV_RAI; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 913 | } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) { |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 914 | peer = peer_by_lai(TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA)); |
| 915 | LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n", |
Holger Hans Peter Freyther | 98b82c1 | 2010-06-08 16:30:24 +0800 | [diff] [blame] | 916 | peer ? peer->bvci : -1); |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 917 | errctr = GBPROX_GLOB_CTR_INV_LAI; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 918 | } else |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 919 | LOGPC(DGPRS, LOGL_INFO, "\n"); |
| 920 | |
| 921 | if (!peer) { |
| 922 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 923 | "unable to route, missing IE\n", nsei); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 924 | rate_ctr_inc(&get_global_ctrg()->ctr[errctr]); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 925 | return -EINVAL; |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 926 | } |
| 927 | return gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 928 | } |
| 929 | |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 930 | /* Receive an incoming BVC-RESET message from the SGSN */ |
| 931 | static int rx_reset_from_sgsn(struct msgb *msg, struct tlv_parsed *tp, |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 932 | uint32_t nsei, uint16_t ns_bvci) |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 933 | { |
| 934 | struct gbprox_peer *peer; |
| 935 | uint16_t ptp_bvci; |
| 936 | |
| 937 | if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 938 | rate_ctr_inc(&get_global_ctrg()-> |
| 939 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 940 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, |
| 941 | NULL, msg); |
| 942 | } |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 943 | ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 944 | |
| 945 | if (ptp_bvci >= 2) { |
| 946 | /* A reset for a PTP BVC was received, forward it to its |
| 947 | * respective peer */ |
| 948 | peer = peer_by_bvci(ptp_bvci); |
| 949 | if (!peer) { |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 950 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 951 | nsei, ptp_bvci); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 952 | rate_ctr_inc(&get_global_ctrg()-> |
| 953 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 954 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 955 | NULL, msg); |
| 956 | } |
| 957 | return gbprox_relay2peer(msg, peer, ns_bvci); |
| 958 | } |
| 959 | |
| 960 | /* A reset for the Signalling entity has been received |
| 961 | * from the SGSN. As the signalling BVCI is shared |
| 962 | * among all the BSS's that we multiplex, it needs to |
| 963 | * be relayed */ |
| 964 | llist_for_each_entry(peer, &gbprox_bts_peers, list) |
| 965 | gbprox_relay2peer(msg, peer, ns_bvci); |
| 966 | |
| 967 | return 0; |
| 968 | } |
| 969 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 970 | /* Receive an incoming signalling message from the SGSN-side NS-VC */ |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 971 | static int gbprox_rx_sig_from_sgsn(struct msgb *msg, uint32_t nsei, |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 972 | uint16_t ns_bvci) |
| 973 | { |
Harald Welte | 2ac2e91 | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 974 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 975 | struct tlv_parsed tp; |
| 976 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | 2ac2e91 | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 977 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 978 | struct gbprox_peer *peer; |
| 979 | uint16_t bvci; |
| 980 | int rc = 0; |
| 981 | |
Harald Welte | 4127072 | 2011-02-06 17:13:12 +0100 | [diff] [blame] | 982 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 983 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 984 | "signalling\n", nsei, ns_bvci); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 985 | /* FIXME: Send proper error message */ |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 986 | return -EINVAL; |
| 987 | } |
| 988 | |
| 989 | /* we actually should never see those two for BVCI == 0, but double-check |
| 990 | * just to make sure */ |
| 991 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 992 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 993 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 994 | "signalling\n", nsei); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 995 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 999 | |
| 1000 | switch (pdu_type) { |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1001 | case BSSGP_PDUT_BVC_RESET: |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1002 | rc = rx_reset_from_sgsn(msg, &tp, nsei, ns_bvci); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1003 | break; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1004 | case BSSGP_PDUT_FLUSH_LL: |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1005 | case BSSGP_PDUT_BVC_RESET_ACK: |
| 1006 | /* simple case: BVCI IE is mandatory */ |
| 1007 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 1008 | goto err_mand_ie; |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 1009 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 1010 | rc = gbprox_relay2bvci(msg, bvci, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1011 | break; |
| 1012 | case BSSGP_PDUT_PAGING_PS: |
| 1013 | case BSSGP_PDUT_PAGING_CS: |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1014 | /* process the paging request (LAI/RAI lookup) */ |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1015 | rc = gbprox_rx_paging(msg, &tp, nsei, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1016 | break; |
| 1017 | case BSSGP_PDUT_STATUS: |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1018 | /* Some exception has occurred */ |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 1019 | LOGP(DGPRS, LOGL_NOTICE, |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1020 | "NSEI=%u(SGSN) BSSGP STATUS ", nsei); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1021 | if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) { |
| 1022 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
| 1023 | goto err_mand_ie; |
| 1024 | } |
| 1025 | LOGPC(DGPRS, LOGL_NOTICE, |
| 1026 | "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE), |
| 1027 | bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE))); |
| 1028 | if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 1029 | uint16_t bvci = tlvp_val16_unal(&tp, BSSGP_IE_BVCI); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1030 | LOGPC(DGPRS, LOGL_NOTICE, |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 1031 | "BVCI=%u\n", ntohs(bvci)); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1032 | } else |
| 1033 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1034 | break; |
| 1035 | /* those only exist in the SGSN -> BSS direction */ |
| 1036 | case BSSGP_PDUT_SUSPEND_ACK: |
| 1037 | case BSSGP_PDUT_SUSPEND_NACK: |
| 1038 | case BSSGP_PDUT_RESUME_ACK: |
| 1039 | case BSSGP_PDUT_RESUME_NACK: |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1040 | /* RAI IE is mandatory */ |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1041 | if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 1042 | goto err_mand_ie; |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1043 | peer = peer_by_rai(TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA)); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1044 | if (!peer) |
| 1045 | goto err_no_peer; |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 1046 | rc = gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1047 | break; |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1048 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 1049 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 1050 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 1051 | goto err_mand_ie; |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 1052 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1053 | if (bvci == 0) { |
| 1054 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1055 | "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei, |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1056 | pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":""); |
| 1057 | /* should we send STATUS ? */ |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1058 | rate_ctr_inc(&get_global_ctrg()-> |
| 1059 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1060 | } else { |
| 1061 | /* Mark BVC as (un)blocked */ |
| 1062 | block_unblock_peer(bvci, pdu_type); |
| 1063 | } |
| 1064 | rc = gbprox_relay2bvci(msg, bvci, ns_bvci); |
| 1065 | break; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1066 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 1067 | LOGP(DGPRS, LOGL_ERROR, |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1068 | "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1069 | rate_ctr_inc(&get_global_ctrg()-> |
| 1070 | ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1071 | rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1072 | break; |
| 1073 | default: |
Harald Welte | be6e25a | 2010-05-12 00:20:41 +0200 | [diff] [blame] | 1074 | LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n", |
| 1075 | pdu_type); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1076 | rate_ctr_inc(&get_global_ctrg()-> |
| 1077 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1078 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1079 | break; |
| 1080 | } |
| 1081 | |
| 1082 | return rc; |
| 1083 | err_mand_ie: |
Harald Welte | 0358aae | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 1084 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1085 | nsei); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1086 | rate_ctr_inc(&get_global_ctrg()-> |
| 1087 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1088 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1089 | err_no_peer: |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1090 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1091 | nsei); |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1092 | rate_ctr_inc(&get_global_ctrg()-> ctr[GBPROX_GLOB_CTR_INV_RAI]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1093 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | /* Main input function for Gb proxy */ |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1097 | int gbprox_rcvmsg(struct msgb *msg, uint16_t nsei, uint16_t ns_bvci, uint16_t nsvci) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1098 | { |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 1099 | int rc; |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1100 | struct gbprox_peer *peer; |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1101 | int remote_end_is_sgsn = nsei == gbcfg.nsip_sgsn_nsei; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1102 | |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1103 | if (remote_end_is_sgsn) |
| 1104 | gbprox_patch_bssgp_message(msg, 1); |
| 1105 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1106 | /* Only BVCI=0 messages need special treatment */ |
| 1107 | if (ns_bvci == 0 || ns_bvci == 1) { |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1108 | if (remote_end_is_sgsn) |
| 1109 | rc = gbprox_rx_sig_from_sgsn(msg, nsei, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1110 | else |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1111 | rc = gbprox_rx_sig_from_bss(msg, nsei, ns_bvci); |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 1112 | } else { |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1113 | peer = peer_by_bvci(ns_bvci); |
| 1114 | |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 1115 | /* All other BVCI are PTP and thus can be simply forwarded */ |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1116 | if (!remote_end_is_sgsn) { |
| 1117 | if (peer) |
| 1118 | check_peer_nsei(peer, nsei); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1119 | return gbprox_relay2sgsn(msg, ns_bvci); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1120 | } |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1121 | |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1122 | /* else: SGSN -> BSS direction */ |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1123 | if (!peer) { |
Jacob Erlbeck | 901b9a4 | 2013-10-18 22:12:17 +0200 | [diff] [blame] | 1124 | LOGP(DGPRS, LOGL_INFO, "Didn't find peer for " |
| 1125 | "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n", |
| 1126 | ns_bvci, nsvci, nsei); |
| 1127 | rate_ctr_inc(&get_global_ctrg()-> |
| 1128 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
| 1129 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 1130 | &ns_bvci, msg); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1131 | } |
| 1132 | if (peer->blocked) { |
| 1133 | LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for " |
| 1134 | "blocked BVCI=%u via NSVC=%u/NSEI=%u\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1135 | ns_bvci, nsvci, nsei); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1136 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 1137 | return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, NULL, msg); |
| 1138 | } |
| 1139 | rc = gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1140 | } |
| 1141 | |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 1142 | return rc; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1143 | } |
Harald Welte | b34e2c1 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 1144 | |
Harald Welte | 9ee404a | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 1145 | int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi) |
| 1146 | { |
| 1147 | struct gprs_nsvc *nsvc; |
| 1148 | |
| 1149 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 1150 | if (!nsvc->persistent) |
| 1151 | continue; |
| 1152 | gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
| 1153 | } |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1157 | /* Signal handler for signals from NS layer */ |
| 1158 | int gbprox_signal(unsigned int subsys, unsigned int signal, |
| 1159 | void *handler_data, void *signal_data) |
| 1160 | { |
| 1161 | struct ns_signal_data *nssd = signal_data; |
| 1162 | struct gprs_nsvc *nsvc = nssd->nsvc; |
| 1163 | struct gbprox_peer *peer; |
| 1164 | |
Harald Welte | 7ed98e3 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 1165 | if (subsys != SS_L_NS) |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1166 | return 0; |
| 1167 | |
Harald Welte | 15cbb0e | 2010-05-11 18:29:44 +0200 | [diff] [blame] | 1168 | if (signal == S_NS_RESET && nsvc->nsei == gbcfg.nsip_sgsn_nsei) { |
| 1169 | /* We have received a NS-RESET from the NSEI and NSVC |
| 1170 | * of the SGSN. This might happen with SGSN that start |
| 1171 | * their own NS-RESET procedure without waiting for our |
| 1172 | * NS-RESET */ |
| 1173 | nsvc->remote_end_is_sgsn = 1; |
| 1174 | } |
| 1175 | |
Harald Welte | fa41cb7 | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 1176 | if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) { |
| 1177 | LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, " |
| 1178 | "re-starting RESET procedure\n"); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1179 | rate_ctr_inc(&get_global_ctrg()-> |
| 1180 | ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]); |
Harald Welte | a529c07 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 1181 | gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr, |
| 1182 | nsvc->nsei, nsvc->nsvci); |
Harald Welte | fa41cb7 | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1185 | if (!nsvc->remote_end_is_sgsn) { |
| 1186 | /* from BSS to SGSN */ |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1187 | peer = peer_by_nsei(nsvc->nsei); |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1188 | if (!peer) { |
| 1189 | LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer " |
| 1190 | "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei, |
| 1191 | nsvc->nsvci); |
| 1192 | return 0; |
| 1193 | } |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1194 | switch (signal) { |
| 1195 | case S_NS_RESET: |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1196 | case S_NS_BLOCK: |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1197 | if (!peer->blocked) |
| 1198 | break; |
| 1199 | LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from " |
| 1200 | "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n", |
| 1201 | nsvc->nsei, nsvc->nsvci); |
| 1202 | bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei, |
| 1203 | peer->bvci, 0); |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1204 | break; |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1205 | } |
| 1206 | } else { |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1207 | /* Forward this message to all NS-VC to BSS */ |
| 1208 | struct gprs_ns_inst *nsi = gbcfg.nsi; |
| 1209 | struct gprs_nsvc *next_nsvc; |
| 1210 | |
| 1211 | llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) { |
| 1212 | if (next_nsvc->remote_end_is_sgsn) |
| 1213 | continue; |
| 1214 | |
| 1215 | /* Note that the following does not start the full |
| 1216 | * procedures including timer based retransmissions. */ |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1217 | switch (signal) { |
| 1218 | case S_NS_RESET: |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1219 | gprs_ns_tx_reset(next_nsvc, nssd->cause); |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1220 | break; |
| 1221 | case S_NS_BLOCK: |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1222 | gprs_ns_tx_block(next_nsvc, nssd->cause); |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1223 | break; |
| 1224 | case S_NS_UNBLOCK: |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1225 | gprs_ns_tx_unblock(next_nsvc); |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 1226 | break; |
| 1227 | } |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
Jacob Erlbeck | eed33ef | 2014-07-07 10:45:59 +0200 | [diff] [blame] | 1233 | int gbprox_dump_global(FILE *stream, int indent) |
Jacob Erlbeck | cc4a2aa | 2013-10-18 22:12:16 +0200 | [diff] [blame] | 1234 | { |
| 1235 | unsigned int i; |
| 1236 | const struct rate_ctr_group_desc *desc; |
| 1237 | int rc; |
| 1238 | |
| 1239 | rc = fprintf(stream, "%*sGbproxy global:\n", indent, ""); |
| 1240 | if (rc < 0) |
| 1241 | return rc; |
| 1242 | |
Jacob Erlbeck | cc4a2aa | 2013-10-18 22:12:16 +0200 | [diff] [blame] | 1243 | desc = get_global_ctrg()->desc; |
| 1244 | |
| 1245 | for (i = 0; i < desc->num_ctr; i++) { |
| 1246 | struct rate_ctr *ctr = &get_global_ctrg()->ctr[i]; |
| 1247 | if (ctr->current) { |
| 1248 | rc = fprintf(stream, "%*s %s: %llu\n", |
| 1249 | indent, "", |
| 1250 | desc->ctr_desc[i].description, |
| 1251 | (long long)ctr->current); |
| 1252 | |
| 1253 | if (rc < 0) |
| 1254 | return rc; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
Jacob Erlbeck | eed33ef | 2014-07-07 10:45:59 +0200 | [diff] [blame] | 1261 | int gbprox_dump_peers(FILE *stream, int indent) |
Jacob Erlbeck | 76fa57a | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 1262 | { |
| 1263 | struct gbprox_peer *peer; |
| 1264 | struct gprs_ra_id raid; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1265 | unsigned int i; |
| 1266 | const struct rate_ctr_group_desc *desc; |
Jacob Erlbeck | 76fa57a | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 1267 | int rc; |
| 1268 | |
Jacob Erlbeck | cc4a2aa | 2013-10-18 22:12:16 +0200 | [diff] [blame] | 1269 | rc = fprintf(stream, "%*sPeers:\n", indent, ""); |
| 1270 | if (rc < 0) |
| 1271 | return rc; |
| 1272 | |
Jacob Erlbeck | 76fa57a | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 1273 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
| 1274 | gsm48_parse_ra(&raid, peer->ra); |
| 1275 | |
| 1276 | rc = fprintf(stream, "%*s NSEI %u, BVCI %u, %sblocked, " |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1277 | "RAI %u-%u-%u-%u\n", |
Jacob Erlbeck | 76fa57a | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 1278 | indent, "", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 1279 | peer->nsei, peer->bvci, |
Jacob Erlbeck | 76fa57a | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 1280 | peer->blocked ? "" : "not ", |
| 1281 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 1282 | |
| 1283 | if (rc < 0) |
| 1284 | return rc; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1285 | |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1286 | desc = peer->ctrg->desc; |
| 1287 | |
| 1288 | for (i = 0; i < desc->num_ctr; i++) { |
| 1289 | struct rate_ctr *ctr = &peer->ctrg->ctr[i]; |
| 1290 | if (ctr->current) { |
| 1291 | rc = fprintf(stream, "%*s %s: %llu\n", |
| 1292 | indent, "", |
| 1293 | desc->ctr_desc[i].description, |
| 1294 | (long long)ctr->current); |
| 1295 | |
| 1296 | if (rc < 0) |
| 1297 | return rc; |
| 1298 | } |
| 1299 | } |
Jacob Erlbeck | 76fa57a | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | return 0; |
| 1303 | } |
Harald Welte | b34e2c1 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 1304 | |
Jacob Erlbeck | 67f03bd | 2013-10-24 12:48:55 +0200 | [diff] [blame] | 1305 | void gbprox_reset() |
| 1306 | { |
| 1307 | struct gbprox_peer *peer, *tmp; |
| 1308 | |
| 1309 | llist_for_each_entry_safe(peer, tmp, &gbprox_bts_peers, list) |
| 1310 | peer_free(peer); |
| 1311 | |
| 1312 | rate_ctr_group_free(global_ctrg); |
| 1313 | global_ctrg = NULL; |
| 1314 | } |
| 1315 | |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 1316 | static int gbprox_cleanup_peers(uint16_t nsei, uint16_t bvci) |
| 1317 | { |
| 1318 | int counter = 0; |
| 1319 | struct gbprox_peer *peer, *tmp; |
| 1320 | |
| 1321 | llist_for_each_entry_safe(peer, tmp, &gbprox_bts_peers, list) { |
| 1322 | if (peer->nsei != nsei) |
| 1323 | continue; |
| 1324 | if (bvci && peer->bvci != bvci) |
| 1325 | continue; |
| 1326 | |
| 1327 | peer_free(peer); |
| 1328 | counter += 1; |
| 1329 | } |
| 1330 | |
| 1331 | return counter; |
| 1332 | } |
| 1333 | |
Harald Welte | bd9591f | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 1334 | #include <osmocom/vty/command.h> |
Harald Welte | b34e2c1 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 1335 | |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 1336 | static void gbprox_vty_print_peer(struct vty *vty, struct gbprox_peer *peer) |
| 1337 | { |
| 1338 | struct gprs_ra_id raid; |
| 1339 | gsm48_parse_ra(&raid, peer->ra); |
| 1340 | |
| 1341 | vty_out(vty, "NSEI %5u, PTP-BVCI %5u, " |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 1342 | "RAI %u-%u-%u-%u", |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 1343 | peer->nsei, peer->bvci, |
| 1344 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 1345 | if (peer->blocked) |
| 1346 | vty_out(vty, " [BVC-BLOCKED]"); |
| 1347 | |
| 1348 | vty_out(vty, "%s", VTY_NEWLINE); |
| 1349 | } |
| 1350 | |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1351 | gDEFUN(show_gbproxy, show_gbproxy_cmd, "show gbproxy [stats]", |
Holger Hans Peter Freyther | f340960 | 2013-11-03 17:34:17 +0100 | [diff] [blame] | 1352 | SHOW_STR "Display information about the Gb proxy\n" "Show statistics\n") |
Harald Welte | b34e2c1 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 1353 | { |
| 1354 | struct gbprox_peer *peer; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1355 | int show_stats = argc >= 1; |
| 1356 | |
| 1357 | if (show_stats) |
| 1358 | vty_out_rate_ctr_group(vty, "", get_global_ctrg()); |
Harald Welte | b34e2c1 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 1359 | |
| 1360 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 1361 | gbprox_vty_print_peer(vty, peer); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 1362 | |
| 1363 | if (show_stats) |
| 1364 | vty_out_rate_ctr_group(vty, " ", peer->ctrg); |
Harald Welte | b34e2c1 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 1365 | } |
| 1366 | return CMD_SUCCESS; |
| 1367 | } |
Holger Hans Peter Freyther | 2ea8812 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 1368 | |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 1369 | gDEFUN(delete_gb_bvci, delete_gb_bvci_cmd, |
| 1370 | "delete-gbproxy-peer <0-65534> bvci <2-65534>", |
| 1371 | "Delete a GBProxy peer by NSEI and optionally BVCI\n" |
Holger Hans Peter Freyther | 2ea8812 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 1372 | "NSEI number\n" |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 1373 | "Only delete peer with a matching BVCI\n" |
Holger Hans Peter Freyther | 2ea8812 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 1374 | "BVCI number\n") |
| 1375 | { |
Holger Hans Peter Freyther | 2ea8812 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 1376 | const uint16_t nsei = atoi(argv[0]); |
| 1377 | const uint16_t bvci = atoi(argv[1]); |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 1378 | int counter; |
Holger Hans Peter Freyther | 2ea8812 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 1379 | |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 1380 | counter = gbprox_cleanup_peers(nsei, bvci); |
Holger Hans Peter Freyther | 2ea8812 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 1381 | |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 1382 | if (counter == 0) { |
| 1383 | vty_out(vty, "BVC not found%s", VTY_NEWLINE); |
| 1384 | return CMD_WARNING; |
| 1385 | } |
| 1386 | |
| 1387 | return CMD_SUCCESS; |
| 1388 | } |
| 1389 | |
| 1390 | gDEFUN(delete_gb_nsei, delete_gb_nsei_cmd, |
| 1391 | "delete-gbproxy-peer <0-65534> (only-bvc|only-nsvc|all) [dry-run]", |
| 1392 | "Delete a GBProxy peer by NSEI and optionally BVCI\n" |
| 1393 | "NSEI number\n" |
| 1394 | "Only delete BSSGP connections (BVC)\n" |
| 1395 | "Only delete dynamic NS connections (NS-VC)\n" |
| 1396 | "Delete BVC and dynamic NS connections\n" |
| 1397 | "Show what would be deleted instead of actually deleting\n" |
| 1398 | ) |
| 1399 | { |
| 1400 | const uint16_t nsei = atoi(argv[0]); |
| 1401 | const char *mode = argv[1]; |
| 1402 | int dry_run = argc > 2; |
| 1403 | int delete_bvc = 0; |
| 1404 | int delete_nsvc = 0; |
| 1405 | int counter; |
| 1406 | |
| 1407 | if (strcmp(mode, "only-bvc") == 0) |
| 1408 | delete_bvc = 1; |
| 1409 | else if (strcmp(mode, "only-nsvc") == 0) |
| 1410 | delete_nsvc = 1; |
| 1411 | else |
| 1412 | delete_bvc = delete_nsvc = 1; |
| 1413 | |
| 1414 | if (delete_bvc) { |
| 1415 | if (!dry_run) |
| 1416 | counter = gbprox_cleanup_peers(nsei, 0); |
| 1417 | else { |
| 1418 | struct gbprox_peer *peer; |
| 1419 | counter = 0; |
| 1420 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
| 1421 | if (peer->nsei != nsei) |
| 1422 | continue; |
| 1423 | |
| 1424 | vty_out(vty, "BVC: "); |
| 1425 | gbprox_vty_print_peer(vty, peer); |
| 1426 | counter += 1; |
| 1427 | } |
| 1428 | } |
| 1429 | vty_out(vty, "%sDeleted %d BVC%s", |
| 1430 | dry_run ? "Not " : "", counter, VTY_NEWLINE); |
| 1431 | } |
| 1432 | |
| 1433 | if (delete_nsvc) { |
| 1434 | struct gprs_ns_inst *nsi = gbcfg.nsi; |
| 1435 | struct gprs_nsvc *nsvc, *nsvc2; |
| 1436 | |
| 1437 | counter = 0; |
| 1438 | llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list) { |
| 1439 | if (nsvc->nsei != nsei) |
| 1440 | continue; |
| 1441 | if (nsvc->persistent) |
| 1442 | continue; |
| 1443 | |
| 1444 | if (!dry_run) |
| 1445 | gprs_nsvc_delete(nsvc); |
| 1446 | else |
| 1447 | vty_out(vty, "NS-VC: NSEI %5u, NS-VCI %5u, " |
| 1448 | "remote %s%s", |
| 1449 | nsvc->nsei, nsvc->nsvci, |
| 1450 | gprs_ns_ll_str(nsvc), VTY_NEWLINE); |
| 1451 | counter += 1; |
| 1452 | } |
| 1453 | vty_out(vty, "%sDeleted %d NS-VC%s", |
| 1454 | dry_run ? "Not " : "", counter, VTY_NEWLINE); |
Holger Hans Peter Freyther | 2ea8812 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | return CMD_SUCCESS; |
| 1458 | } |