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