Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1 | /* NS-over-IP proxy */ |
| 2 | |
| 3 | /* (C) 2010 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 4 | * (C) 2010-2013 by On-Waves |
| 5 | * (C) 2013 by Holger Hans Peter Freyther |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 17 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <unistd.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <getopt.h> |
| 28 | #include <errno.h> |
| 29 | #include <sys/fcntl.h> |
| 30 | #include <sys/stat.h> |
Harald Welte | 7fc9822 | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 31 | #include <arpa/inet.h> |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 32 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 33 | #include <osmocom/core/talloc.h> |
| 34 | #include <osmocom/core/select.h> |
Jacob Erlbeck | bc55574 | 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 | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 38 | |
Harald Welte | ea34a4e | 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 | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 42 | #include <openbsc/signal.h> |
| 43 | #include <openbsc/debug.h> |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 44 | #include <openbsc/gb_proxy.h> |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 45 | |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 46 | enum gbprox_global_ctr { |
| 47 | GBPROX_GLOB_CTR_INV_BVCI, |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 48 | GBPROX_GLOB_CTR_INV_LAI, |
| 49 | GBPROX_GLOB_CTR_INV_RAI, |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 50 | GBPROX_GLOB_CTR_INV_NSEI, |
| 51 | GBPROX_GLOB_CTR_PROTO_ERR_BSS, |
| 52 | GBPROX_GLOB_CTR_PROTO_ERR_SGSN, |
| 53 | GBPROX_GLOB_CTR_NOT_SUPPORTED_BSS, |
| 54 | GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN, |
| 55 | GBPROX_GLOB_CTR_RESTART_RESET_SGSN, |
| 56 | GBPROX_GLOB_CTR_TX_ERR_SGSN, |
| 57 | GBPROX_GLOB_CTR_OTHER_ERR, |
| 58 | }; |
| 59 | |
| 60 | static const struct rate_ctr_desc global_ctr_description[] = { |
| 61 | { "inv-bvci", "Invalid BVC Identifier " }, |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 62 | { "inv-lai", "Invalid Location Area Identifier" }, |
| 63 | { "inv-rai", "Invalid Routing Area Identifier " }, |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 64 | { "inv-nsei", "No BVC established for NSEI " }, |
| 65 | { "proto-err.bss", "BSSGP protocol error (BSS )" }, |
| 66 | { "proto-err.sgsn", "BSSGP protocol error (SGSN)" }, |
| 67 | { "not-supp.bss", "Feature not supported (BSS )" }, |
| 68 | { "not-supp.sgsn", "Feature not supported (SGSN)" }, |
| 69 | { "restart.sgsn", "Restarted RESET procedure (SGSN)" }, |
| 70 | { "tx-err.sgsn", "NS Transmission error (SGSN)" }, |
| 71 | { "error", "Other error " }, |
| 72 | }; |
| 73 | |
| 74 | static const struct rate_ctr_group_desc global_ctrg_desc = { |
| 75 | .group_name_prefix = "gbproxy.global", |
| 76 | .group_description = "GBProxy Global Statistics", |
| 77 | .num_ctr = ARRAY_SIZE(global_ctr_description), |
| 78 | .ctr_desc = global_ctr_description, |
| 79 | }; |
| 80 | |
| 81 | static struct rate_ctr_group *global_ctrg = NULL; |
| 82 | |
| 83 | static struct rate_ctr_group *get_global_ctrg() |
| 84 | { |
| 85 | if (global_ctrg) |
| 86 | return global_ctrg; |
| 87 | |
| 88 | global_ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0); |
| 89 | return global_ctrg; |
| 90 | } |
| 91 | |
| 92 | enum gbprox_peer_ctr { |
| 93 | GBPROX_PEER_CTR_BLOCKED, |
| 94 | GBPROX_PEER_CTR_UNBLOCKED, |
| 95 | GBPROX_PEER_CTR_DROPPED, |
| 96 | GBPROX_PEER_CTR_INV_NSEI, |
| 97 | GBPROX_PEER_CTR_TX_ERR, |
| 98 | }; |
| 99 | |
| 100 | static const struct rate_ctr_desc peer_ctr_description[] = { |
| 101 | { "blocked", "BVC Block " }, |
| 102 | { "unblocked", "BVC Unblock " }, |
| 103 | { "dropped", "BVC blocked, dropped packet " }, |
| 104 | { "inv-nsei", "NSEI mismatch " }, |
| 105 | { "tx-err", "NS Transmission error " }, |
| 106 | }; |
| 107 | |
| 108 | static const struct rate_ctr_group_desc peer_ctrg_desc = { |
| 109 | .group_name_prefix = "gbproxy.peer", |
| 110 | .group_description = "GBProxy Peer Statistics", |
| 111 | .num_ctr = ARRAY_SIZE(peer_ctr_description), |
| 112 | .ctr_desc = peer_ctr_description, |
| 113 | }; |
| 114 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 115 | struct gbprox_peer { |
| 116 | struct llist_head list; |
| 117 | |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 118 | /* NSEI of the peer entity */ |
| 119 | uint16_t nsei; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 120 | |
| 121 | /* BVCI used for Point-to-Point to this peer */ |
| 122 | uint16_t bvci; |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 123 | int blocked; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 124 | |
| 125 | /* Routeing Area that this peer is part of (raw 04.08 encoding) */ |
| 126 | uint8_t ra[6]; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 127 | |
| 128 | /* Counter */ |
| 129 | struct rate_ctr_group *ctrg; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | /* Linked list of all Gb peers (except SGSN) */ |
| 133 | static LLIST_HEAD(gbprox_bts_peers); |
| 134 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 135 | /* Find the gbprox_peer by its BVCI */ |
| 136 | static struct gbprox_peer *peer_by_bvci(uint16_t bvci) |
| 137 | { |
| 138 | struct gbprox_peer *peer; |
| 139 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
| 140 | if (peer->bvci == bvci) |
| 141 | return peer; |
| 142 | } |
| 143 | return NULL; |
| 144 | } |
| 145 | |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 146 | /* Find the gbprox_peer by its NSEI */ |
| 147 | static struct gbprox_peer *peer_by_nsei(uint16_t nsei) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 148 | { |
| 149 | struct gbprox_peer *peer; |
| 150 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 151 | if (peer->nsei == nsei) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 152 | return peer; |
| 153 | } |
| 154 | return NULL; |
| 155 | } |
| 156 | |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 157 | /* look-up a peer by its Routeing Area Identification (RAI) */ |
| 158 | static struct gbprox_peer *peer_by_rai(const uint8_t *ra) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 159 | { |
| 160 | struct gbprox_peer *peer; |
| 161 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 162 | if (!memcmp(peer->ra, ra, 6)) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 163 | return peer; |
| 164 | } |
| 165 | return NULL; |
| 166 | } |
| 167 | |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 168 | /* look-up a peer by its Location Area Identification (LAI) */ |
| 169 | static struct gbprox_peer *peer_by_lai(const uint8_t *la) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 170 | { |
| 171 | struct gbprox_peer *peer; |
| 172 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 173 | if (!memcmp(peer->ra, la, 5)) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 174 | return peer; |
| 175 | } |
| 176 | return NULL; |
| 177 | } |
| 178 | |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 179 | static int check_peer_nsei(struct gbprox_peer *peer, uint16_t nsei) |
| 180 | { |
| 181 | if (peer->nsei != nsei) { |
| 182 | LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI " |
| 183 | "BVCI=%u via NSEI=%u (expected NSEI=%u)\n", |
| 184 | peer->bvci, nsei, peer->nsei); |
| 185 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]); |
Jacob Erlbeck | 64cb924 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 186 | return 0; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Jacob Erlbeck | 64cb924 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 189 | return 1; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 192 | static struct gbprox_peer *peer_alloc(uint16_t bvci) |
| 193 | { |
| 194 | struct gbprox_peer *peer; |
| 195 | |
| 196 | peer = talloc_zero(tall_bsc_ctx, struct gbprox_peer); |
| 197 | if (!peer) |
| 198 | return NULL; |
| 199 | |
| 200 | peer->bvci = bvci; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 201 | peer->ctrg = rate_ctr_group_alloc(peer, &peer_ctrg_desc, bvci); |
| 202 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 203 | llist_add(&peer->list, &gbprox_bts_peers); |
| 204 | |
| 205 | return peer; |
| 206 | } |
| 207 | |
| 208 | static void peer_free(struct gbprox_peer *peer) |
| 209 | { |
Jacob Erlbeck | 02ca778 | 2013-10-23 11:24:16 +0200 | [diff] [blame] | 210 | rate_ctr_group_free(peer->ctrg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 211 | llist_del(&peer->list); |
| 212 | talloc_free(peer); |
| 213 | } |
| 214 | |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 215 | /* FIXME: this needs to go to libosmocore/msgb.c */ |
| 216 | static struct msgb *msgb_copy(const struct msgb *msg, const char *name) |
| 217 | { |
Harald Welte | 8645e10 | 2012-06-16 16:09:52 +0800 | [diff] [blame] | 218 | struct libgb_msgb_cb *old_cb, *new_cb; |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 219 | struct msgb *new_msg; |
| 220 | |
Holger Hans Peter Freyther | 62d9798 | 2010-06-08 18:14:37 +0800 | [diff] [blame] | 221 | new_msg = msgb_alloc(msg->data_len, name); |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 222 | if (!new_msg) |
| 223 | return NULL; |
| 224 | |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 225 | /* copy data */ |
Holger Hans Peter Freyther | 62d9798 | 2010-06-08 18:14:37 +0800 | [diff] [blame] | 226 | memcpy(new_msg->_data, msg->_data, new_msg->data_len); |
| 227 | |
| 228 | /* copy header */ |
| 229 | new_msg->len = msg->len; |
| 230 | new_msg->data += msg->data - msg->_data; |
| 231 | new_msg->head += msg->head - msg->_data; |
| 232 | new_msg->tail += msg->tail - msg->_data; |
| 233 | |
| 234 | new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data); |
| 235 | new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data); |
| 236 | new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data); |
| 237 | new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data); |
| 238 | |
| 239 | /* copy GB specific data */ |
Harald Welte | 8645e10 | 2012-06-16 16:09:52 +0800 | [diff] [blame] | 240 | old_cb = LIBGB_MSGB_CB(msg); |
| 241 | new_cb = LIBGB_MSGB_CB(new_msg); |
Holger Hans Peter Freyther | 62d9798 | 2010-06-08 18:14:37 +0800 | [diff] [blame] | 242 | |
| 243 | new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data); |
| 244 | new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data); |
| 245 | |
Harald Welte | fb9e06f | 2011-02-06 17:17:05 +0100 | [diff] [blame] | 246 | /* bssgp_cell_id is a pointer into the old msgb, so we need to make |
| 247 | * it a pointer into the new msgb */ |
| 248 | new_cb->bssgp_cell_id = new_msg->_data + (old_cb->bssgp_cell_id - msg->_data); |
Holger Hans Peter Freyther | 62d9798 | 2010-06-08 18:14:37 +0800 | [diff] [blame] | 249 | new_cb->nsei = old_cb->nsei; |
| 250 | new_cb->bvci = old_cb->bvci; |
| 251 | new_cb->tlli = old_cb->tlli; |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 252 | |
| 253 | return new_msg; |
| 254 | } |
| 255 | |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 256 | /* strip off the NS header */ |
| 257 | static void strip_ns_hdr(struct msgb *msg) |
| 258 | { |
| 259 | int strip_len = msgb_bssgph(msg) - msg->data; |
| 260 | msgb_pull(msg, strip_len); |
| 261 | } |
| 262 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 263 | /* feed a message down the NS-VC associated with the specified peer */ |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 264 | static int gbprox_relay2sgsn(struct msgb *old_msg, uint16_t ns_bvci) |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 265 | { |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 266 | /* create a copy of the message so the old one can |
| 267 | * be free()d safely when we return from gbprox_rcvmsg() */ |
| 268 | struct msgb *msg = msgb_copy(old_msg, "msgb_relay2sgsn"); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 269 | int rc; |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 270 | |
Harald Welte | e9ea269 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 271 | DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n", |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 272 | msgb_nsei(msg), ns_bvci, gbcfg.nsip_sgsn_nsei); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 273 | |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 274 | msgb_bvci(msg) = ns_bvci; |
| 275 | msgb_nsei(msg) = gbcfg.nsip_sgsn_nsei; |
| 276 | |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 277 | strip_ns_hdr(msg); |
| 278 | |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 279 | rc = gprs_ns_sendmsg(bssgp_nsi, msg); |
| 280 | if (rc < 0) |
| 281 | rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]); |
| 282 | |
| 283 | return rc; |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 286 | /* feed a message down the NS-VC associated with the specified peer */ |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 287 | static int gbprox_relay2peer(struct msgb *old_msg, struct gbprox_peer *peer, |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 288 | uint16_t ns_bvci) |
| 289 | { |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 290 | /* create a copy of the message so the old one can |
| 291 | * be free()d safely when we return from gbprox_rcvmsg() */ |
| 292 | struct msgb *msg = msgb_copy(old_msg, "msgb_relay2peer"); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 293 | int rc; |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 294 | |
Harald Welte | 0ab535b | 2010-05-13 10:34:56 +0200 | [diff] [blame] | 295 | DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 296 | msgb_nsei(msg), ns_bvci, peer->nsei); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 297 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 298 | msgb_bvci(msg) = ns_bvci; |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 299 | msgb_nsei(msg) = peer->nsei; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 300 | |
Harald Welte | 0ab535b | 2010-05-13 10:34:56 +0200 | [diff] [blame] | 301 | /* Strip the old NS header, it will be replaced with a new one */ |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 302 | strip_ns_hdr(msg); |
| 303 | |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 304 | rc = gprs_ns_sendmsg(bssgp_nsi, msg); |
| 305 | if (rc < 0) |
| 306 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]); |
| 307 | |
| 308 | return rc; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 311 | static int block_unblock_peer(uint16_t ptp_bvci, uint8_t pdu_type) |
| 312 | { |
| 313 | struct gbprox_peer *peer; |
| 314 | |
| 315 | peer = peer_by_bvci(ptp_bvci); |
| 316 | if (!peer) { |
| 317 | LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n", |
| 318 | ptp_bvci); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 319 | rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 320 | return -ENOENT; |
| 321 | } |
| 322 | |
| 323 | switch (pdu_type) { |
| 324 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 325 | peer->blocked = 1; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 326 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 327 | break; |
| 328 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 329 | peer->blocked = 0; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 330 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 331 | break; |
| 332 | default: |
| 333 | break; |
| 334 | } |
| 335 | return 0; |
| 336 | } |
| 337 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 338 | /* Send a message to a peer identified by ptp_bvci but using ns_bvci |
| 339 | * in the NS hdr */ |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 340 | static int gbprox_relay2bvci(struct msgb *msg, uint16_t ptp_bvci, |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 341 | uint16_t ns_bvci) |
| 342 | { |
| 343 | struct gbprox_peer *peer; |
| 344 | |
| 345 | peer = peer_by_bvci(ptp_bvci); |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 346 | if (!peer) { |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 347 | LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n", |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 348 | ptp_bvci); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 349 | rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 350 | return -ENOENT; |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 351 | } |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 352 | |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 353 | return gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Harald Welte | b1fd902 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 356 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 357 | { |
| 358 | return 0; |
| 359 | } |
| 360 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 361 | /* Receive an incoming signalling message from a BSS-side NS-VC */ |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 362 | static int gbprox_rx_sig_from_bss(struct msgb *msg, uint16_t nsei, |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 363 | uint16_t ns_bvci) |
| 364 | { |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 365 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 366 | struct tlv_parsed tp; |
| 367 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 368 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 369 | struct gbprox_peer *from_peer; |
Harald Welte | 70f38d2 | 2010-05-01 12:10:57 +0200 | [diff] [blame] | 370 | struct gprs_ra_id raid; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 371 | |
Harald Welte | c471d3d | 2011-02-06 17:13:12 +0100 | [diff] [blame] | 372 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 373 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 374 | nsei, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 375 | return -EINVAL; |
| 376 | } |
| 377 | |
| 378 | /* we actually should never see those two for BVCI == 0, but double-check |
| 379 | * just to make sure */ |
| 380 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 381 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 382 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 383 | "signalling\n", nsei); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 384 | return -EINVAL; |
| 385 | } |
| 386 | |
| 387 | bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 388 | |
| 389 | switch (pdu_type) { |
| 390 | case BSSGP_PDUT_SUSPEND: |
| 391 | case BSSGP_PDUT_RESUME: |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 392 | /* We implement RAI snooping during SUSPEND/RESUME, since it |
| 393 | * establishes a relationsip between BVCI/peer and the routeing |
| 394 | * area identification. The snooped information is then used |
| 395 | * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct |
| 396 | * BSSGP */ |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 397 | if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 398 | goto err_mand_ie; |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 399 | from_peer = peer_by_nsei(nsei); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 400 | if (!from_peer) |
| 401 | goto err_no_peer; |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 402 | memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA), |
| 403 | sizeof(from_peer->ra)); |
Harald Welte | 7fc9822 | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 404 | gsm48_parse_ra(&raid, from_peer->ra); |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 405 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME " |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 406 | "RAI snooping: RAI %u-%u-%u-%u behind BVCI=%u\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 407 | nsei, raid.mcc, raid.mnc, raid.lac, |
| 408 | raid.rac , from_peer->bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 409 | /* FIXME: This only supports one BSS per RA */ |
| 410 | break; |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 411 | case BSSGP_PDUT_BVC_RESET: |
| 412 | /* If we receive a BVC reset on the signalling endpoint, we |
| 413 | * don't want the SGSN to reset, as the signalling endpoint |
| 414 | * is common for all point-to-point BVCs (and thus all BTS) */ |
| 415 | if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 416 | uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 72953b8 | 2010-05-12 00:20:41 +0200 | [diff] [blame] | 417 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 418 | nsei, bvci); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 419 | if (bvci == 0) { |
| 420 | /* FIXME: only do this if SGSN is alive! */ |
Harald Welte | e9ea269 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 421 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 422 | "BVC RESET ACK of BVCI=0\n", nsei); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 423 | return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 424 | nsei, 0, ns_bvci); |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 425 | } |
| 426 | from_peer = peer_by_bvci(bvci); |
| 427 | if (!from_peer) { |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 428 | /* if a PTP-BVC is reset, and we don't know that |
| 429 | * PTP-BVCI yet, we should allocate a new peer */ |
| 430 | LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 431 | "BVCI=%u via NSEI=%u\n", bvci, nsei); |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 432 | from_peer = peer_alloc(bvci); |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 433 | from_peer->nsei = nsei; |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 434 | } |
Jacob Erlbeck | 64cb924 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 435 | |
| 436 | if (!check_peer_nsei(from_peer, nsei)) |
| 437 | from_peer->nsei = nsei; |
| 438 | |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 439 | if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) { |
| 440 | struct gprs_ra_id raid; |
| 441 | /* We have a Cell Identifier present in this |
| 442 | * PDU, this means we can extend our local |
| 443 | * state information about this particular cell |
| 444 | * */ |
| 445 | memcpy(from_peer->ra, |
| 446 | TLVP_VAL(&tp, BSSGP_IE_CELL_ID), |
| 447 | sizeof(from_peer->ra)); |
| 448 | gsm48_parse_ra(&raid, from_peer->ra); |
| 449 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 450 | "Cell ID %u-%u-%u-%u\n", nsei, |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 451 | bvci, raid.mcc, raid.mnc, raid.lac, |
| 452 | raid.rac); |
| 453 | } |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 454 | } |
| 455 | break; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Harald Welte | e9ea269 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 458 | /* Normally, we can simply pass on all signalling messages from BSS to |
| 459 | * SGSN */ |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 460 | return gbprox_relay2sgsn(msg, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 461 | err_no_peer: |
Jacob Erlbeck | c5085f9 | 2013-10-18 13:04:48 +0200 | [diff] [blame] | 462 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 463 | nsei); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 464 | rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_INV_NSEI]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 465 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 466 | err_mand_ie: |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 467 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 468 | nsei); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 469 | rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 470 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /* Receive paging request from SGSN, we need to relay to proper BSS */ |
| 474 | static int gbprox_rx_paging(struct msgb *msg, struct tlv_parsed *tp, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 475 | uint32_t nsei, uint16_t ns_bvci) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 476 | { |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 477 | struct gbprox_peer *peer = NULL; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 478 | int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 479 | |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 480 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 481 | nsei); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 482 | if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 483 | uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 484 | LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n", |
| 485 | bvci); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 486 | errctr = GBPROX_GLOB_CTR_OTHER_ERR; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 487 | } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) { |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 488 | peer = peer_by_rai(TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA)); |
| 489 | LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n", |
Holger Hans Peter Freyther | 62eaf54 | 2010-06-08 16:30:24 +0800 | [diff] [blame] | 490 | peer ? peer->bvci : -1); |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 491 | errctr = GBPROX_GLOB_CTR_INV_RAI; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 492 | } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) { |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 493 | peer = peer_by_lai(TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA)); |
| 494 | LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n", |
Holger Hans Peter Freyther | 62eaf54 | 2010-06-08 16:30:24 +0800 | [diff] [blame] | 495 | peer ? peer->bvci : -1); |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 496 | errctr = GBPROX_GLOB_CTR_INV_LAI; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 497 | } else |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 498 | LOGPC(DGPRS, LOGL_INFO, "\n"); |
| 499 | |
| 500 | if (!peer) { |
| 501 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 502 | "unable to route, missing IE\n", nsei); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 503 | rate_ctr_inc(&get_global_ctrg()->ctr[errctr]); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 504 | return -EINVAL; |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 505 | } |
| 506 | return gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 507 | } |
| 508 | |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 509 | /* Receive an incoming BVC-RESET message from the SGSN */ |
| 510 | static int rx_reset_from_sgsn(struct msgb *msg, struct tlv_parsed *tp, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 511 | uint32_t nsei, uint16_t ns_bvci) |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 512 | { |
| 513 | struct gbprox_peer *peer; |
| 514 | uint16_t ptp_bvci; |
| 515 | |
| 516 | if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 517 | rate_ctr_inc(&get_global_ctrg()-> |
| 518 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 519 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, |
| 520 | NULL, msg); |
| 521 | } |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 522 | ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 523 | |
| 524 | if (ptp_bvci >= 2) { |
| 525 | /* A reset for a PTP BVC was received, forward it to its |
| 526 | * respective peer */ |
| 527 | peer = peer_by_bvci(ptp_bvci); |
| 528 | if (!peer) { |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 529 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 530 | nsei, ptp_bvci); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 531 | rate_ctr_inc(&get_global_ctrg()-> |
| 532 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 533 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 534 | NULL, msg); |
| 535 | } |
| 536 | return gbprox_relay2peer(msg, peer, ns_bvci); |
| 537 | } |
| 538 | |
| 539 | /* A reset for the Signalling entity has been received |
| 540 | * from the SGSN. As the signalling BVCI is shared |
| 541 | * among all the BSS's that we multiplex, it needs to |
| 542 | * be relayed */ |
| 543 | llist_for_each_entry(peer, &gbprox_bts_peers, list) |
| 544 | gbprox_relay2peer(msg, peer, ns_bvci); |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 549 | /* Receive an incoming signalling message from the SGSN-side NS-VC */ |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 550 | static int gbprox_rx_sig_from_sgsn(struct msgb *msg, uint32_t nsei, |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 551 | uint16_t ns_bvci) |
| 552 | { |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 553 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 554 | struct tlv_parsed tp; |
| 555 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 556 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 557 | struct gbprox_peer *peer; |
| 558 | uint16_t bvci; |
| 559 | int rc = 0; |
| 560 | |
Harald Welte | c471d3d | 2011-02-06 17:13:12 +0100 | [diff] [blame] | 561 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 562 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 563 | "signalling\n", nsei, ns_bvci); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 564 | /* FIXME: Send proper error message */ |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 565 | return -EINVAL; |
| 566 | } |
| 567 | |
| 568 | /* we actually should never see those two for BVCI == 0, but double-check |
| 569 | * just to make sure */ |
| 570 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 571 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 572 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 573 | "signalling\n", nsei); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 574 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 578 | |
| 579 | switch (pdu_type) { |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 580 | case BSSGP_PDUT_BVC_RESET: |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 581 | rc = rx_reset_from_sgsn(msg, &tp, nsei, ns_bvci); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 582 | break; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 583 | case BSSGP_PDUT_FLUSH_LL: |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 584 | case BSSGP_PDUT_BVC_RESET_ACK: |
| 585 | /* simple case: BVCI IE is mandatory */ |
| 586 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 587 | goto err_mand_ie; |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 588 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 589 | rc = gbprox_relay2bvci(msg, bvci, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 590 | break; |
| 591 | case BSSGP_PDUT_PAGING_PS: |
| 592 | case BSSGP_PDUT_PAGING_CS: |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 593 | /* process the paging request (LAI/RAI lookup) */ |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 594 | rc = gbprox_rx_paging(msg, &tp, nsei, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 595 | break; |
| 596 | case BSSGP_PDUT_STATUS: |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 597 | /* Some exception has occurred */ |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 598 | LOGP(DGPRS, LOGL_NOTICE, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 599 | "NSEI=%u(SGSN) BSSGP STATUS ", nsei); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 600 | if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) { |
| 601 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
| 602 | goto err_mand_ie; |
| 603 | } |
| 604 | LOGPC(DGPRS, LOGL_NOTICE, |
| 605 | "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE), |
| 606 | bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE))); |
| 607 | if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 608 | uint16_t bvci = tlvp_val16_unal(&tp, BSSGP_IE_BVCI); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 609 | LOGPC(DGPRS, LOGL_NOTICE, |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 610 | "BVCI=%u\n", ntohs(bvci)); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 611 | } else |
| 612 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 613 | break; |
| 614 | /* those only exist in the SGSN -> BSS direction */ |
| 615 | case BSSGP_PDUT_SUSPEND_ACK: |
| 616 | case BSSGP_PDUT_SUSPEND_NACK: |
| 617 | case BSSGP_PDUT_RESUME_ACK: |
| 618 | case BSSGP_PDUT_RESUME_NACK: |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 619 | /* RAI IE is mandatory */ |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 620 | if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 621 | goto err_mand_ie; |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 622 | peer = peer_by_rai(TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA)); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 623 | if (!peer) |
| 624 | goto err_no_peer; |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 625 | rc = gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 626 | break; |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 627 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 628 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 629 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 630 | goto err_mand_ie; |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 631 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 632 | if (bvci == 0) { |
| 633 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 634 | "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei, |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 635 | pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":""); |
| 636 | /* should we send STATUS ? */ |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 637 | rate_ctr_inc(&get_global_ctrg()-> |
| 638 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 639 | } else { |
| 640 | /* Mark BVC as (un)blocked */ |
| 641 | block_unblock_peer(bvci, pdu_type); |
| 642 | } |
| 643 | rc = gbprox_relay2bvci(msg, bvci, ns_bvci); |
| 644 | break; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 645 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 646 | LOGP(DGPRS, LOGL_ERROR, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 647 | "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 648 | rate_ctr_inc(&get_global_ctrg()-> |
| 649 | ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 650 | rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 651 | break; |
| 652 | default: |
Harald Welte | 72953b8 | 2010-05-12 00:20:41 +0200 | [diff] [blame] | 653 | LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n", |
| 654 | pdu_type); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 655 | rate_ctr_inc(&get_global_ctrg()-> |
| 656 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 657 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 658 | break; |
| 659 | } |
| 660 | |
| 661 | return rc; |
| 662 | err_mand_ie: |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 663 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 664 | nsei); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 665 | rate_ctr_inc(&get_global_ctrg()-> |
| 666 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 667 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 668 | err_no_peer: |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 669 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 670 | nsei); |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 671 | rate_ctr_inc(&get_global_ctrg()-> ctr[GBPROX_GLOB_CTR_INV_RAI]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 672 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* Main input function for Gb proxy */ |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 676 | int gbprox_rcvmsg(struct msgb *msg, uint16_t nsei, uint16_t ns_bvci, uint16_t nsvci) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 677 | { |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 678 | int rc; |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 679 | struct gbprox_peer *peer; |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 680 | int remote_end_is_sgsn = nsei == gbcfg.nsip_sgsn_nsei; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 681 | |
| 682 | /* Only BVCI=0 messages need special treatment */ |
| 683 | if (ns_bvci == 0 || ns_bvci == 1) { |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 684 | if (remote_end_is_sgsn) |
| 685 | rc = gbprox_rx_sig_from_sgsn(msg, nsei, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 686 | else |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 687 | rc = gbprox_rx_sig_from_bss(msg, nsei, ns_bvci); |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 688 | } else { |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 689 | peer = peer_by_bvci(ns_bvci); |
| 690 | |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 691 | /* All other BVCI are PTP and thus can be simply forwarded */ |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 692 | if (!remote_end_is_sgsn) { |
| 693 | if (peer) |
| 694 | check_peer_nsei(peer, nsei); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 695 | return gbprox_relay2sgsn(msg, ns_bvci); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 696 | } |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 697 | |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 698 | /* else: SGSN -> BSS direction */ |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 699 | if (!peer) { |
Jacob Erlbeck | f0f63a4 | 2013-10-18 22:12:17 +0200 | [diff] [blame] | 700 | LOGP(DGPRS, LOGL_INFO, "Didn't find peer for " |
| 701 | "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n", |
| 702 | ns_bvci, nsvci, nsei); |
| 703 | rate_ctr_inc(&get_global_ctrg()-> |
| 704 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
| 705 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 706 | &ns_bvci, msg); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 707 | } |
| 708 | if (peer->blocked) { |
| 709 | LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for " |
| 710 | "blocked BVCI=%u via NSVC=%u/NSEI=%u\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 711 | ns_bvci, nsvci, nsei); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 712 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 713 | return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, NULL, msg); |
| 714 | } |
| 715 | rc = gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 716 | } |
| 717 | |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 718 | return rc; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 719 | } |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 720 | |
Harald Welte | 1ccbf44 | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 721 | int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi) |
| 722 | { |
| 723 | struct gprs_nsvc *nsvc; |
| 724 | |
| 725 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 726 | if (!nsvc->persistent) |
| 727 | continue; |
| 728 | gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
| 729 | } |
| 730 | return 0; |
| 731 | } |
| 732 | |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 733 | /* Signal handler for signals from NS layer */ |
| 734 | int gbprox_signal(unsigned int subsys, unsigned int signal, |
| 735 | void *handler_data, void *signal_data) |
| 736 | { |
| 737 | struct ns_signal_data *nssd = signal_data; |
| 738 | struct gprs_nsvc *nsvc = nssd->nsvc; |
| 739 | struct gbprox_peer *peer; |
| 740 | |
Harald Welte | a6a20b4 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 741 | if (subsys != SS_L_NS) |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 742 | return 0; |
| 743 | |
Harald Welte | f69c059 | 2010-05-11 18:29:44 +0200 | [diff] [blame] | 744 | if (signal == S_NS_RESET && nsvc->nsei == gbcfg.nsip_sgsn_nsei) { |
| 745 | /* We have received a NS-RESET from the NSEI and NSVC |
| 746 | * of the SGSN. This might happen with SGSN that start |
| 747 | * their own NS-RESET procedure without waiting for our |
| 748 | * NS-RESET */ |
| 749 | nsvc->remote_end_is_sgsn = 1; |
| 750 | } |
| 751 | |
Harald Welte | b778d2c | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 752 | if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) { |
| 753 | LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, " |
| 754 | "re-starting RESET procedure\n"); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 755 | rate_ctr_inc(&get_global_ctrg()-> |
| 756 | ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]); |
Harald Welte | e6599ee | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 757 | gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr, |
| 758 | nsvc->nsei, nsvc->nsvci); |
Harald Welte | b778d2c | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 761 | if (!nsvc->remote_end_is_sgsn) { |
| 762 | /* from BSS to SGSN */ |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 763 | peer = peer_by_nsei(nsvc->nsei); |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 764 | if (!peer) { |
| 765 | LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer " |
| 766 | "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei, |
| 767 | nsvc->nsvci); |
| 768 | return 0; |
| 769 | } |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 770 | switch (signal) { |
| 771 | case S_NS_RESET: |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 772 | case S_NS_BLOCK: |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 773 | if (!peer->blocked) |
| 774 | break; |
| 775 | LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from " |
| 776 | "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n", |
| 777 | nsvc->nsei, nsvc->nsvci); |
| 778 | bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei, |
| 779 | peer->bvci, 0); |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 780 | break; |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 781 | } |
| 782 | } else { |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 783 | /* Forward this message to all NS-VC to BSS */ |
| 784 | struct gprs_ns_inst *nsi = gbcfg.nsi; |
| 785 | struct gprs_nsvc *next_nsvc; |
| 786 | |
| 787 | llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) { |
| 788 | if (next_nsvc->remote_end_is_sgsn) |
| 789 | continue; |
| 790 | |
| 791 | /* Note that the following does not start the full |
| 792 | * procedures including timer based retransmissions. */ |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 793 | switch (signal) { |
| 794 | case S_NS_RESET: |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 795 | gprs_ns_tx_reset(next_nsvc, nssd->cause); |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 796 | break; |
| 797 | case S_NS_BLOCK: |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 798 | gprs_ns_tx_block(next_nsvc, nssd->cause); |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 799 | break; |
| 800 | case S_NS_UNBLOCK: |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 801 | gprs_ns_tx_unblock(next_nsvc); |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 802 | break; |
| 803 | } |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | return 0; |
| 807 | } |
| 808 | |
Jacob Erlbeck | da890c7 | 2013-10-18 22:12:16 +0200 | [diff] [blame] | 809 | int gbprox_dump_global(FILE *stream, int indent, int verbose) |
| 810 | { |
| 811 | unsigned int i; |
| 812 | const struct rate_ctr_group_desc *desc; |
| 813 | int rc; |
| 814 | |
| 815 | rc = fprintf(stream, "%*sGbproxy global:\n", indent, ""); |
| 816 | if (rc < 0) |
| 817 | return rc; |
| 818 | |
| 819 | if (!verbose) |
| 820 | return 0; |
| 821 | |
| 822 | desc = get_global_ctrg()->desc; |
| 823 | |
| 824 | for (i = 0; i < desc->num_ctr; i++) { |
| 825 | struct rate_ctr *ctr = &get_global_ctrg()->ctr[i]; |
| 826 | if (ctr->current) { |
| 827 | rc = fprintf(stream, "%*s %s: %llu\n", |
| 828 | indent, "", |
| 829 | desc->ctr_desc[i].description, |
| 830 | (long long)ctr->current); |
| 831 | |
| 832 | if (rc < 0) |
| 833 | return rc; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | return 0; |
| 838 | } |
| 839 | |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 840 | int gbprox_dump_peers(FILE *stream, int indent, int verbose) |
Jacob Erlbeck | 51a869c | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 841 | { |
| 842 | struct gbprox_peer *peer; |
| 843 | struct gprs_ra_id raid; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 844 | unsigned int i; |
| 845 | const struct rate_ctr_group_desc *desc; |
Jacob Erlbeck | 51a869c | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 846 | int rc; |
| 847 | |
Jacob Erlbeck | da890c7 | 2013-10-18 22:12:16 +0200 | [diff] [blame] | 848 | rc = fprintf(stream, "%*sPeers:\n", indent, ""); |
| 849 | if (rc < 0) |
| 850 | return rc; |
| 851 | |
Jacob Erlbeck | 51a869c | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 852 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
| 853 | gsm48_parse_ra(&raid, peer->ra); |
| 854 | |
| 855 | rc = fprintf(stream, "%*s NSEI %u, BVCI %u, %sblocked, " |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 856 | "RAI %u-%u-%u-%u\n", |
Jacob Erlbeck | 51a869c | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 857 | indent, "", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 858 | peer->nsei, peer->bvci, |
Jacob Erlbeck | 51a869c | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 859 | peer->blocked ? "" : "not ", |
| 860 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 861 | |
| 862 | if (rc < 0) |
| 863 | return rc; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 864 | |
| 865 | if (!verbose) |
| 866 | continue; |
| 867 | |
| 868 | desc = peer->ctrg->desc; |
| 869 | |
| 870 | for (i = 0; i < desc->num_ctr; i++) { |
| 871 | struct rate_ctr *ctr = &peer->ctrg->ctr[i]; |
| 872 | if (ctr->current) { |
| 873 | rc = fprintf(stream, "%*s %s: %llu\n", |
| 874 | indent, "", |
| 875 | desc->ctr_desc[i].description, |
| 876 | (long long)ctr->current); |
| 877 | |
| 878 | if (rc < 0) |
| 879 | return rc; |
| 880 | } |
| 881 | } |
Jacob Erlbeck | 51a869c | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | return 0; |
| 885 | } |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 886 | |
Jacob Erlbeck | 72b401f | 2013-10-24 12:48:55 +0200 | [diff] [blame] | 887 | void gbprox_reset() |
| 888 | { |
| 889 | struct gbprox_peer *peer, *tmp; |
| 890 | |
| 891 | llist_for_each_entry_safe(peer, tmp, &gbprox_bts_peers, list) |
| 892 | peer_free(peer); |
| 893 | |
| 894 | rate_ctr_group_free(global_ctrg); |
| 895 | global_ctrg = NULL; |
| 896 | } |
| 897 | |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 898 | static int gbprox_cleanup_peers(uint16_t nsei, uint16_t bvci) |
| 899 | { |
| 900 | int counter = 0; |
| 901 | struct gbprox_peer *peer, *tmp; |
| 902 | |
| 903 | llist_for_each_entry_safe(peer, tmp, &gbprox_bts_peers, list) { |
| 904 | if (peer->nsei != nsei) |
| 905 | continue; |
| 906 | if (bvci && peer->bvci != bvci) |
| 907 | continue; |
| 908 | |
| 909 | peer_free(peer); |
| 910 | counter += 1; |
| 911 | } |
| 912 | |
| 913 | return counter; |
| 914 | } |
| 915 | |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 916 | #include <osmocom/vty/command.h> |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 917 | |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 918 | static void gbprox_vty_print_peer(struct vty *vty, struct gbprox_peer *peer) |
| 919 | { |
| 920 | struct gprs_ra_id raid; |
| 921 | gsm48_parse_ra(&raid, peer->ra); |
| 922 | |
| 923 | vty_out(vty, "NSEI %5u, PTP-BVCI %5u, " |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 924 | "RAI %u-%u-%u-%u", |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 925 | peer->nsei, peer->bvci, |
| 926 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 927 | if (peer->blocked) |
| 928 | vty_out(vty, " [BVC-BLOCKED]"); |
| 929 | |
| 930 | vty_out(vty, "%s", VTY_NEWLINE); |
| 931 | } |
| 932 | |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 933 | gDEFUN(show_gbproxy, show_gbproxy_cmd, "show gbproxy [stats]", |
Holger Hans Peter Freyther | 82e644b | 2013-11-03 17:34:17 +0100 | [diff] [blame] | 934 | SHOW_STR "Display information about the Gb proxy\n" "Show statistics\n") |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 935 | { |
| 936 | struct gbprox_peer *peer; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 937 | int show_stats = argc >= 1; |
| 938 | |
| 939 | if (show_stats) |
| 940 | vty_out_rate_ctr_group(vty, "", get_global_ctrg()); |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 941 | |
| 942 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 943 | gbprox_vty_print_peer(vty, peer); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 944 | |
| 945 | if (show_stats) |
| 946 | vty_out_rate_ctr_group(vty, " ", peer->ctrg); |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 947 | } |
| 948 | return CMD_SUCCESS; |
| 949 | } |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 950 | |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 951 | gDEFUN(delete_gb_bvci, delete_gb_bvci_cmd, |
| 952 | "delete-gbproxy-peer <0-65534> bvci <2-65534>", |
| 953 | "Delete a GBProxy peer by NSEI and optionally BVCI\n" |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 954 | "NSEI number\n" |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 955 | "Only delete peer with a matching BVCI\n" |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 956 | "BVCI number\n") |
| 957 | { |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 958 | const uint16_t nsei = atoi(argv[0]); |
| 959 | const uint16_t bvci = atoi(argv[1]); |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 960 | int counter; |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 961 | |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 962 | counter = gbprox_cleanup_peers(nsei, bvci); |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 963 | |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 964 | if (counter == 0) { |
| 965 | vty_out(vty, "BVC not found%s", VTY_NEWLINE); |
| 966 | return CMD_WARNING; |
| 967 | } |
| 968 | |
| 969 | return CMD_SUCCESS; |
| 970 | } |
| 971 | |
| 972 | gDEFUN(delete_gb_nsei, delete_gb_nsei_cmd, |
| 973 | "delete-gbproxy-peer <0-65534> (only-bvc|only-nsvc|all) [dry-run]", |
| 974 | "Delete a GBProxy peer by NSEI and optionally BVCI\n" |
| 975 | "NSEI number\n" |
| 976 | "Only delete BSSGP connections (BVC)\n" |
| 977 | "Only delete dynamic NS connections (NS-VC)\n" |
| 978 | "Delete BVC and dynamic NS connections\n" |
| 979 | "Show what would be deleted instead of actually deleting\n" |
| 980 | ) |
| 981 | { |
| 982 | const uint16_t nsei = atoi(argv[0]); |
| 983 | const char *mode = argv[1]; |
| 984 | int dry_run = argc > 2; |
| 985 | int delete_bvc = 0; |
| 986 | int delete_nsvc = 0; |
| 987 | int counter; |
| 988 | |
| 989 | if (strcmp(mode, "only-bvc") == 0) |
| 990 | delete_bvc = 1; |
| 991 | else if (strcmp(mode, "only-nsvc") == 0) |
| 992 | delete_nsvc = 1; |
| 993 | else |
| 994 | delete_bvc = delete_nsvc = 1; |
| 995 | |
| 996 | if (delete_bvc) { |
| 997 | if (!dry_run) |
| 998 | counter = gbprox_cleanup_peers(nsei, 0); |
| 999 | else { |
| 1000 | struct gbprox_peer *peer; |
| 1001 | counter = 0; |
| 1002 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
| 1003 | if (peer->nsei != nsei) |
| 1004 | continue; |
| 1005 | |
| 1006 | vty_out(vty, "BVC: "); |
| 1007 | gbprox_vty_print_peer(vty, peer); |
| 1008 | counter += 1; |
| 1009 | } |
| 1010 | } |
| 1011 | vty_out(vty, "%sDeleted %d BVC%s", |
| 1012 | dry_run ? "Not " : "", counter, VTY_NEWLINE); |
| 1013 | } |
| 1014 | |
| 1015 | if (delete_nsvc) { |
| 1016 | struct gprs_ns_inst *nsi = gbcfg.nsi; |
| 1017 | struct gprs_nsvc *nsvc, *nsvc2; |
| 1018 | |
| 1019 | counter = 0; |
| 1020 | llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list) { |
| 1021 | if (nsvc->nsei != nsei) |
| 1022 | continue; |
| 1023 | if (nsvc->persistent) |
| 1024 | continue; |
| 1025 | |
| 1026 | if (!dry_run) |
| 1027 | gprs_nsvc_delete(nsvc); |
| 1028 | else |
| 1029 | vty_out(vty, "NS-VC: NSEI %5u, NS-VCI %5u, " |
| 1030 | "remote %s%s", |
| 1031 | nsvc->nsei, nsvc->nsvci, |
| 1032 | gprs_ns_ll_str(nsvc), VTY_NEWLINE); |
| 1033 | counter += 1; |
| 1034 | } |
| 1035 | vty_out(vty, "%sDeleted %d NS-VC%s", |
| 1036 | dry_run ? "Not " : "", counter, VTY_NEWLINE); |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | return CMD_SUCCESS; |
| 1040 | } |