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