Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1 | /* NS-over-IP proxy */ |
| 2 | |
| 3 | /* (C) 2010 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 2ea8812 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 4 | * (C) 2010-2013 by On-Waves |
| 5 | * (C) 2013 by Holger Hans Peter Freyther |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 17 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <unistd.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <getopt.h> |
| 28 | #include <errno.h> |
| 29 | #include <sys/fcntl.h> |
| 30 | #include <sys/stat.h> |
Harald Welte | f2fdefe | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 31 | #include <arpa/inet.h> |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 32 | #include <time.h> |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 33 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 34 | #include <osmocom/core/talloc.h> |
| 35 | #include <osmocom/core/select.h> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 36 | #include <osmocom/core/rate_ctr.h> |
| 37 | |
Harald Welte | cfb6b28 | 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 | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 41 | #include <osmocom/gsm/gsm_utils.h> |
| 42 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 43 | #include <openbsc/signal.h> |
| 44 | #include <openbsc/debug.h> |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 45 | #include <openbsc/gb_proxy.h> |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 46 | |
Jacob Erlbeck | 2504bc4 | 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 | 8ac3a72 | 2014-08-04 11:52:52 +0200 | [diff] [blame] | 50 | #include <openbsc/gprs_utils.h> |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 51 | |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 52 | enum gbprox_global_ctr { |
| 53 | GBPROX_GLOB_CTR_INV_BVCI, |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 54 | GBPROX_GLOB_CTR_INV_LAI, |
| 55 | GBPROX_GLOB_CTR_INV_RAI, |
Jacob Erlbeck | 174bad4 | 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 | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 64 | GBPROX_GLOB_CTR_PATCH_PEER_ERR, |
Jacob Erlbeck | 174bad4 | 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 | 9d7268a | 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 | 174bad4 | 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 | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 79 | { "mod-peer-err", "Patch error: no peer " }, |
Jacob Erlbeck | 174bad4 | 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 | 174bad4 | 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 | 90a1fd1 | 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, |
Jacob Erlbeck | d65d92d | 2014-08-18 17:23:01 +0200 | [diff] [blame] | 98 | GBPROX_PEER_CTR_TLLI_PATCHED_BSS, |
| 99 | GBPROX_PEER_CTR_TLLI_PATCHED_SGSN, |
| 100 | GBPROX_PEER_CTR_PTMSI_PATCHED_BSS, |
| 101 | GBPROX_PEER_CTR_PTMSI_PATCHED_SGSN, |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 102 | GBPROX_PEER_CTR_PATCH_CRYPT_ERR, |
| 103 | GBPROX_PEER_CTR_PATCH_ERR, |
Jacob Erlbeck | 481da7c | 2014-06-27 10:44:46 +0200 | [diff] [blame] | 104 | GBPROX_PEER_CTR_ATTACH_REQS, |
| 105 | GBPROX_PEER_CTR_ATTACH_REJS, |
Jacob Erlbeck | d65d92d | 2014-08-18 17:23:01 +0200 | [diff] [blame] | 106 | GBPROX_PEER_CTR_TLLI_UNKNOWN, |
Jacob Erlbeck | 481da7c | 2014-06-27 10:44:46 +0200 | [diff] [blame] | 107 | GBPROX_PEER_CTR_TLLI_CACHE_SIZE, |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | static const struct rate_ctr_desc peer_ctr_description[] = { |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 111 | { "blocked", "BVC Block " }, |
| 112 | { "unblocked", "BVC Unblock " }, |
| 113 | { "dropped", "BVC blocked, dropped packet " }, |
| 114 | { "inv-nsei", "NSEI mismatch " }, |
| 115 | { "tx-err", "NS Transmission error " }, |
| 116 | { "raid-mod.bss", "RAID patched (BSS )" }, |
| 117 | { "raid-mod.sgsn", "RAID patched (SGSN)" }, |
| 118 | { "apn-mod.sgsn", "APN patched " }, |
Jacob Erlbeck | d65d92d | 2014-08-18 17:23:01 +0200 | [diff] [blame] | 119 | { "tlli-mod.bss", "TLLI patched (BSS )" }, |
| 120 | { "tlli-mod.sgsn", "TLLI patched (SGSN)" }, |
| 121 | { "ptmsi-mod.bss", "P-TMSI patched (BSS )" }, |
| 122 | { "ptmsi-mod.sgsn","P-TMSI patched (SGSN)" }, |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 123 | { "mod-crypt-err", "Patch error: encrypted " }, |
| 124 | { "mod-err", "Patch error: other " }, |
Jacob Erlbeck | 481da7c | 2014-06-27 10:44:46 +0200 | [diff] [blame] | 125 | { "attach-reqs", "Attach Request count " }, |
| 126 | { "attach-rejs", "Attach Reject count " }, |
Jacob Erlbeck | d65d92d | 2014-08-18 17:23:01 +0200 | [diff] [blame] | 127 | { "tlli-unknown", "TLLI from SGSN unknown " }, |
Jacob Erlbeck | 481da7c | 2014-06-27 10:44:46 +0200 | [diff] [blame] | 128 | { "tlli-cache", "TLLI cache size " }, |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | static const struct rate_ctr_group_desc peer_ctrg_desc = { |
| 132 | .group_name_prefix = "gbproxy.peer", |
| 133 | .group_description = "GBProxy Peer Statistics", |
| 134 | .num_ctr = ARRAY_SIZE(peer_ctr_description), |
| 135 | .ctr_desc = peer_ctr_description, |
| 136 | }; |
| 137 | |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 138 | static void gbprox_delete_tllis(struct gbproxy_peer *peer); |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 139 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 140 | /* Find the gbprox_peer by its BVCI */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 141 | static struct gbproxy_peer *peer_by_bvci(struct gbproxy_config *cfg, uint16_t bvci) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 142 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 143 | struct gbproxy_peer *peer; |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 144 | llist_for_each_entry(peer, &cfg->bts_peers, list) { |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 145 | if (peer->bvci == bvci) |
| 146 | return peer; |
| 147 | } |
| 148 | return NULL; |
| 149 | } |
| 150 | |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 151 | /* Find the gbprox_peer by its NSEI */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 152 | struct gbproxy_peer *gbprox_peer_by_nsei(struct gbproxy_config *cfg, uint16_t nsei) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 153 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 154 | struct gbproxy_peer *peer; |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 155 | llist_for_each_entry(peer, &cfg->bts_peers, list) { |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 156 | if (peer->nsei == nsei) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 157 | return peer; |
| 158 | } |
| 159 | return NULL; |
| 160 | } |
| 161 | |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 162 | /* look-up a peer by its Routeing Area Identification (RAI) */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 163 | static struct gbproxy_peer *peer_by_rai(struct gbproxy_config *cfg, const uint8_t *ra) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 164 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 165 | struct gbproxy_peer *peer; |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 166 | llist_for_each_entry(peer, &cfg->bts_peers, list) { |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 167 | if (!memcmp(peer->ra, ra, 6)) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 168 | return peer; |
| 169 | } |
| 170 | return NULL; |
| 171 | } |
| 172 | |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 173 | /* look-up a peer by its Location Area Identification (LAI) */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 174 | static struct gbproxy_peer *peer_by_lai(struct gbproxy_config *cfg, const uint8_t *la) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 175 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 176 | struct gbproxy_peer *peer; |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 177 | llist_for_each_entry(peer, &cfg->bts_peers, list) { |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 178 | if (!memcmp(peer->ra, la, 5)) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 179 | return peer; |
| 180 | } |
| 181 | return NULL; |
| 182 | } |
| 183 | |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 184 | /* look-up a peer by its Location Area Code (LAC) */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 185 | static struct gbproxy_peer *peer_by_lac(struct gbproxy_config *cfg, const uint8_t *la) |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 186 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 187 | struct gbproxy_peer *peer; |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 188 | llist_for_each_entry(peer, &cfg->bts_peers, list) { |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 189 | if (!memcmp(peer->ra + 3, la + 3, 2)) |
| 190 | return peer; |
| 191 | } |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 196 | static int check_peer_nsei(struct gbproxy_peer *peer, uint16_t nsei) |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 197 | { |
| 198 | if (peer->nsei != nsei) { |
| 199 | LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI " |
| 200 | "BVCI=%u via NSEI=%u (expected NSEI=%u)\n", |
| 201 | peer->bvci, nsei, peer->nsei); |
| 202 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]); |
Jacob Erlbeck | d0a9b50 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 203 | return 0; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Jacob Erlbeck | d0a9b50 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 206 | return 1; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Holger Hans Peter Freyther | 60fa5b9 | 2014-08-04 17:10:08 +0200 | [diff] [blame] | 209 | struct gbproxy_peer *gbproxy_peer_alloc(struct gbproxy_config *cfg, uint16_t bvci) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 210 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 211 | struct gbproxy_peer *peer; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 212 | |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 213 | peer = talloc_zero(tall_bsc_ctx, struct gbproxy_peer); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 214 | if (!peer) |
| 215 | return NULL; |
| 216 | |
| 217 | peer->bvci = bvci; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 218 | peer->ctrg = rate_ctr_group_alloc(peer, &peer_ctrg_desc, bvci); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 219 | peer->cfg = cfg; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 220 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 221 | llist_add(&peer->list, &cfg->bts_peers); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 222 | |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 223 | INIT_LLIST_HEAD(&peer->patch_state.enabled_tllis); |
| 224 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 225 | return peer; |
| 226 | } |
| 227 | |
Holger Hans Peter Freyther | 60fa5b9 | 2014-08-04 17:10:08 +0200 | [diff] [blame] | 228 | void gbproxy_peer_free(struct gbproxy_peer *peer) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 229 | { |
| 230 | llist_del(&peer->list); |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 231 | |
| 232 | gbprox_delete_tllis(peer); |
| 233 | |
Jacob Erlbeck | 985b46e | 2014-08-07 17:23:00 +0200 | [diff] [blame] | 234 | rate_ctr_group_free(peer->ctrg); |
| 235 | peer->ctrg = NULL; |
| 236 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 237 | talloc_free(peer); |
| 238 | } |
| 239 | |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 240 | /* strip off the NS header */ |
| 241 | static void strip_ns_hdr(struct msgb *msg) |
| 242 | { |
| 243 | int strip_len = msgb_bssgph(msg) - msg->data; |
| 244 | msgb_pull(msg, strip_len); |
| 245 | } |
| 246 | |
Jacob Erlbeck | 077abce | 2014-07-01 12:41:13 +0200 | [diff] [blame] | 247 | /* TODO: Move shift functions to libosmocore */ |
| 248 | |
| 249 | int v_fixed_shift(uint8_t **data, size_t *data_len, |
| 250 | size_t len, uint8_t **value) |
| 251 | { |
| 252 | if (len > *data_len) |
| 253 | goto fail; |
| 254 | |
| 255 | if (value) |
| 256 | *value = *data; |
| 257 | |
| 258 | *data += len; |
| 259 | *data_len -= len; |
| 260 | |
| 261 | return len; |
| 262 | |
| 263 | fail: |
| 264 | *data += *data_len; |
| 265 | *data_len = 0; |
| 266 | return -1; |
| 267 | } |
| 268 | |
| 269 | int tv_fixed_match(uint8_t **data, size_t *data_len, |
| 270 | uint8_t tag, size_t len, |
| 271 | uint8_t **value) |
| 272 | { |
| 273 | size_t ie_len; |
| 274 | |
| 275 | if (*data_len == 0) |
| 276 | goto fail; |
| 277 | |
| 278 | if ((*data)[0] != tag) |
| 279 | return 0; |
| 280 | |
| 281 | if (len > *data_len - 1) |
| 282 | goto fail; |
| 283 | |
| 284 | if (value) |
| 285 | *value = *data + 1; |
| 286 | |
| 287 | ie_len = len + 1; |
| 288 | *data += ie_len; |
| 289 | *data_len -= ie_len; |
| 290 | |
| 291 | return ie_len; |
| 292 | |
| 293 | fail: |
| 294 | *data += *data_len; |
| 295 | *data_len = 0; |
| 296 | return -1; |
| 297 | } |
| 298 | |
| 299 | int tlv_match(uint8_t **data, size_t *data_len, |
| 300 | uint8_t tag, uint8_t **value, size_t *value_len) |
| 301 | { |
| 302 | size_t len; |
| 303 | size_t ie_len; |
| 304 | |
| 305 | if (*data_len < 2) |
| 306 | goto fail; |
| 307 | |
| 308 | if ((*data)[0] != tag) |
| 309 | return 0; |
| 310 | |
| 311 | len = (*data)[1]; |
| 312 | if (len > *data_len - 2) |
| 313 | goto fail; |
| 314 | |
| 315 | if (value) |
| 316 | *value = *data + 2; |
| 317 | if (value_len) |
| 318 | *value_len = len; |
| 319 | |
| 320 | ie_len = len + 2; |
| 321 | |
| 322 | *data += ie_len; |
| 323 | *data_len -= ie_len; |
| 324 | |
| 325 | return ie_len; |
| 326 | |
| 327 | fail: |
| 328 | *data += *data_len; |
| 329 | *data_len = 0; |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | int lv_shift(uint8_t **data, size_t *data_len, |
| 334 | uint8_t **value, size_t *value_len) |
| 335 | { |
| 336 | size_t len; |
| 337 | size_t ie_len; |
| 338 | |
| 339 | if (*data_len < 1) |
| 340 | goto fail; |
| 341 | |
| 342 | len = (*data)[0]; |
| 343 | if (len > *data_len - 1) |
| 344 | goto fail; |
| 345 | |
| 346 | if (value) |
| 347 | *value = *data + 1; |
| 348 | if (value_len) |
| 349 | *value_len = len; |
| 350 | |
| 351 | ie_len = len + 1; |
| 352 | *data += ie_len; |
| 353 | *data_len -= ie_len; |
| 354 | |
| 355 | return ie_len; |
| 356 | |
| 357 | fail: |
| 358 | *data += *data_len; |
| 359 | *data_len = 0; |
| 360 | return -1; |
| 361 | } |
| 362 | |
Jacob Erlbeck | e39a1c9 | 2014-08-07 13:11:34 +0200 | [diff] [blame] | 363 | /* GSM 04.08, 10.5.1.4 */ |
| 364 | static int is_mi_tmsi(const uint8_t *value, size_t value_len) |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 365 | { |
| 366 | if (value_len != GSM48_TMSI_LEN) |
| 367 | return 0; |
| 368 | |
Jacob Erlbeck | b679977 | 2014-08-07 10:46:29 +0200 | [diff] [blame] | 369 | if (!value || (value[0] & GSM_MI_TYPE_MASK) != GSM_MI_TYPE_TMSI) |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 370 | return 0; |
| 371 | |
| 372 | return 1; |
| 373 | } |
| 374 | |
Jacob Erlbeck | e39a1c9 | 2014-08-07 13:11:34 +0200 | [diff] [blame] | 375 | /* GSM 04.08, 10.5.1.4 */ |
| 376 | static int is_mi_imsi(const uint8_t *value, size_t value_len) |
Jacob Erlbeck | dc6dcdf | 2014-08-06 15:16:45 +0200 | [diff] [blame] | 377 | { |
| 378 | if (value_len == 0) |
| 379 | return 0; |
| 380 | |
Jacob Erlbeck | b679977 | 2014-08-07 10:46:29 +0200 | [diff] [blame] | 381 | if (!value || (value[0] & GSM_MI_TYPE_MASK) != GSM_MI_TYPE_IMSI) |
Jacob Erlbeck | dc6dcdf | 2014-08-06 15:16:45 +0200 | [diff] [blame] | 382 | return 0; |
| 383 | |
| 384 | return 1; |
| 385 | } |
| 386 | |
Jacob Erlbeck | e39a1c9 | 2014-08-07 13:11:34 +0200 | [diff] [blame] | 387 | static int parse_mi_tmsi(const uint8_t *value, size_t value_len, uint32_t *tmsi) |
Jacob Erlbeck | f5b2085 | 2014-06-27 18:10:53 +0200 | [diff] [blame] | 388 | { |
| 389 | uint32_t tmsi_be; |
| 390 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 391 | if (!is_mi_tmsi(value, value_len)) |
Jacob Erlbeck | f5b2085 | 2014-06-27 18:10:53 +0200 | [diff] [blame] | 392 | return 0; |
| 393 | |
| 394 | memcpy(&tmsi_be, value + 1, sizeof(tmsi_be)); |
| 395 | |
| 396 | *tmsi = ntohl(tmsi_be); |
Jacob Erlbeck | f5b2085 | 2014-06-27 18:10:53 +0200 | [diff] [blame] | 397 | return 1; |
| 398 | } |
| 399 | |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 400 | struct gbproxy_parse_context { |
Jacob Erlbeck | f40a6c9 | 2014-06-27 19:00:45 +0200 | [diff] [blame] | 401 | /* Pointer to protocol specific parts */ |
| 402 | struct gsm48_hdr *g48_hdr; |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 403 | struct bssgp_normal_hdr *bgp_hdr; |
Jacob Erlbeck | f40a6c9 | 2014-06-27 19:00:45 +0200 | [diff] [blame] | 404 | struct bssgp_ud_hdr *bud_hdr; |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 405 | uint8_t *bssgp_data; |
| 406 | size_t bssgp_data_len; |
| 407 | uint8_t *llc; |
| 408 | size_t llc_len; |
Jacob Erlbeck | f40a6c9 | 2014-06-27 19:00:45 +0200 | [diff] [blame] | 409 | |
| 410 | /* Extracted information */ |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 411 | struct gprs_llc_hdr_parsed llc_hdr_parsed; |
| 412 | struct tlv_parsed bssgp_tp; |
Jacob Erlbeck | f40a6c9 | 2014-06-27 19:00:45 +0200 | [diff] [blame] | 413 | int to_bss; |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 414 | uint8_t *tlli_enc; |
| 415 | uint8_t *imsi; |
Jacob Erlbeck | f40a6c9 | 2014-06-27 19:00:45 +0200 | [diff] [blame] | 416 | size_t imsi_len; |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 417 | uint8_t *apn_ie; |
| 418 | size_t apn_ie_len; |
| 419 | uint8_t *ptmsi_enc; |
Jacob Erlbeck | dc6dcdf | 2014-08-06 15:16:45 +0200 | [diff] [blame] | 420 | uint8_t *new_ptmsi_enc; |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 421 | uint8_t *raid_enc; |
Jacob Erlbeck | 2cb9e3f | 2014-08-18 15:41:41 +0200 | [diff] [blame] | 422 | uint8_t *old_raid_enc; |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 423 | uint8_t *bssgp_raid_enc; |
| 424 | uint8_t *bssgp_ptimsi; |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 425 | |
| 426 | /* General info */ |
| 427 | const char *llc_msg_name; |
| 428 | int invalidate_tlli; |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 429 | int need_decryption; |
| 430 | uint32_t tlli; |
| 431 | int pdu_type; |
Jacob Erlbeck | 2cb9e3f | 2014-08-18 15:41:41 +0200 | [diff] [blame] | 432 | int old_raid_matches; |
Jacob Erlbeck | f40a6c9 | 2014-06-27 19:00:45 +0200 | [diff] [blame] | 433 | }; |
| 434 | |
Jacob Erlbeck | 3113287 | 2014-08-11 17:26:21 +0200 | [diff] [blame] | 435 | struct gbproxy_tlli_info *gbprox_find_tlli(struct gbproxy_peer *peer, |
| 436 | uint32_t tlli) |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 437 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 438 | struct gbproxy_tlli_info *tlli_info; |
| 439 | struct gbproxy_patch_state *state = &peer->patch_state; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 440 | |
| 441 | llist_for_each_entry(tlli_info, &state->enabled_tllis, list) |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 442 | if (tlli_info->tlli.current == tlli || |
| 443 | tlli_info->tlli.assigned == tlli) |
| 444 | return tlli_info; |
| 445 | |
| 446 | return NULL; |
| 447 | } |
| 448 | |
Jacob Erlbeck | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 449 | static struct gbproxy_tlli_info *gbprox_find_tlli_by_ptmsi( |
| 450 | struct gbproxy_peer *peer, |
| 451 | uint32_t ptmsi) |
| 452 | { |
| 453 | struct gbproxy_tlli_info *tlli_info; |
| 454 | struct gbproxy_patch_state *state = &peer->patch_state; |
| 455 | |
| 456 | llist_for_each_entry(tlli_info, &state->enabled_tllis, list) |
| 457 | if (tlli_info->tlli.ptmsi == ptmsi) |
| 458 | return tlli_info; |
| 459 | |
| 460 | return NULL; |
| 461 | } |
| 462 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 463 | struct gbproxy_tlli_info *gbprox_find_tlli_by_sgsn_tlli( |
| 464 | struct gbproxy_peer *peer, |
| 465 | uint32_t tlli) |
| 466 | { |
| 467 | struct gbproxy_tlli_info *tlli_info; |
| 468 | struct gbproxy_patch_state *state = &peer->patch_state; |
| 469 | |
| 470 | llist_for_each_entry(tlli_info, &state->enabled_tllis, list) |
| 471 | if (tlli_info->sgsn_tlli.current == tlli || |
| 472 | tlli_info->sgsn_tlli.assigned == tlli) |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 473 | return tlli_info; |
| 474 | |
| 475 | return NULL; |
| 476 | } |
| 477 | |
Holger Hans Peter Freyther | 60fa5b9 | 2014-08-04 17:10:08 +0200 | [diff] [blame] | 478 | struct gbproxy_tlli_info *gbprox_find_tlli_by_mi( |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 479 | struct gbproxy_peer *peer, |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 480 | const uint8_t *mi_data, |
| 481 | size_t mi_data_len) |
| 482 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 483 | struct gbproxy_tlli_info *tlli_info; |
| 484 | struct gbproxy_patch_state *state = &peer->patch_state; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 485 | |
Jacob Erlbeck | 9ac42ba | 2014-08-06 18:55:15 +0200 | [diff] [blame] | 486 | if (!is_mi_imsi(mi_data, mi_data_len)) |
| 487 | return NULL; |
| 488 | |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 489 | llist_for_each_entry(tlli_info, &state->enabled_tllis, list) { |
| 490 | if (tlli_info->mi_data_len != mi_data_len) |
| 491 | continue; |
| 492 | if (memcmp(tlli_info->mi_data, mi_data, mi_data_len) != 0) |
| 493 | continue; |
| 494 | |
| 495 | return tlli_info; |
| 496 | } |
| 497 | |
| 498 | return NULL; |
| 499 | } |
| 500 | |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 501 | void gbprox_delete_tlli(struct gbproxy_peer *peer, |
| 502 | struct gbproxy_tlli_info *tlli_info) |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 503 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 504 | struct gbproxy_patch_state *state = &peer->patch_state; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 505 | |
| 506 | llist_del(&tlli_info->list); |
| 507 | talloc_free(tlli_info); |
| 508 | state->enabled_tllis_count -= 1; |
Jacob Erlbeck | 985b46e | 2014-08-07 17:23:00 +0200 | [diff] [blame] | 509 | |
| 510 | peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current = |
| 511 | state->enabled_tllis_count; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 512 | } |
| 513 | |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 514 | static void gbprox_delete_tllis(struct gbproxy_peer *peer) |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 515 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 516 | struct gbproxy_tlli_info *tlli_info, *nxt; |
| 517 | struct gbproxy_patch_state *state = &peer->patch_state; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 518 | |
Holger Hans Peter Freyther | c120d11 | 2014-08-04 12:18:07 +0200 | [diff] [blame] | 519 | llist_for_each_entry_safe(tlli_info, nxt, &state->enabled_tllis, list) |
| 520 | gbprox_delete_tlli(peer, tlli_info); |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 521 | |
Holger Hans Peter Freyther | c120d11 | 2014-08-04 12:18:07 +0200 | [diff] [blame] | 522 | OSMO_ASSERT(state->enabled_tllis_count == 0); |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 523 | OSMO_ASSERT(llist_empty(&state->enabled_tllis)); |
| 524 | } |
| 525 | |
Jacob Erlbeck | 581728f | 2014-08-14 08:57:04 +0200 | [diff] [blame] | 526 | void gbprox_clear_patch_filter(struct gbproxy_config *cfg) |
| 527 | { |
| 528 | if (cfg->check_imsi) { |
| 529 | regfree(&cfg->imsi_re_comp); |
| 530 | cfg->check_imsi = 0; |
| 531 | } |
| 532 | } |
| 533 | |
Holger Hans Peter Freyther | 5e94f7f | 2014-08-04 16:27:11 +0200 | [diff] [blame] | 534 | int gbprox_set_patch_filter(struct gbproxy_config *cfg, const char *filter, |
| 535 | const char **err_msg) |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 536 | { |
| 537 | static char err_buf[300]; |
| 538 | int rc; |
| 539 | |
Jacob Erlbeck | 581728f | 2014-08-14 08:57:04 +0200 | [diff] [blame] | 540 | gbprox_clear_patch_filter(cfg); |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 541 | |
| 542 | if (!filter) |
| 543 | return 0; |
| 544 | |
Holger Hans Peter Freyther | 5e94f7f | 2014-08-04 16:27:11 +0200 | [diff] [blame] | 545 | rc = regcomp(&cfg->imsi_re_comp, filter, |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 546 | REG_EXTENDED | REG_NOSUB | REG_ICASE); |
| 547 | |
| 548 | if (rc == 0) { |
Holger Hans Peter Freyther | 5e94f7f | 2014-08-04 16:27:11 +0200 | [diff] [blame] | 549 | cfg->check_imsi = 1; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | if (err_msg) { |
Holger Hans Peter Freyther | 5e94f7f | 2014-08-04 16:27:11 +0200 | [diff] [blame] | 554 | regerror(rc, &cfg->imsi_re_comp, |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 555 | err_buf, sizeof(err_buf)); |
| 556 | *err_msg = err_buf; |
| 557 | } |
| 558 | |
| 559 | return -1; |
| 560 | } |
| 561 | |
Jacob Erlbeck | b679977 | 2014-08-07 10:46:29 +0200 | [diff] [blame] | 562 | int gbprox_check_imsi(struct gbproxy_peer *peer, |
| 563 | const uint8_t *imsi, size_t imsi_len) |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 564 | { |
| 565 | char mi_buf[200]; |
| 566 | int rc; |
| 567 | |
Holger Hans Peter Freyther | 5e94f7f | 2014-08-04 16:27:11 +0200 | [diff] [blame] | 568 | if (!peer->cfg->check_imsi) |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 569 | return 1; |
| 570 | |
Jacob Erlbeck | b679977 | 2014-08-07 10:46:29 +0200 | [diff] [blame] | 571 | rc = is_mi_imsi(imsi, imsi_len); |
| 572 | if (rc > 0) |
| 573 | rc = gsm48_mi_to_string(mi_buf, sizeof(mi_buf), imsi, imsi_len); |
| 574 | if (rc <= 0) { |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 575 | LOGP(DGPRS, LOGL_NOTICE, "Invalid IMSI %s\n", |
| 576 | osmo_hexdump(imsi, imsi_len)); |
| 577 | return -1; |
| 578 | } |
| 579 | |
| 580 | LOGP(DGPRS, LOGL_DEBUG, "Checking IMSI '%s' (%d)\n", mi_buf, rc); |
| 581 | |
Holger Hans Peter Freyther | 5e94f7f | 2014-08-04 16:27:11 +0200 | [diff] [blame] | 582 | rc = regexec(&peer->cfg->imsi_re_comp, mi_buf, 0, NULL, 0); |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 583 | if (rc == REG_NOMATCH) { |
| 584 | LOGP(DGPRS, LOGL_INFO, |
| 585 | "IMSI '%s' doesn't match pattern '%s'\n", |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 586 | mi_buf, peer->cfg->match_re); |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | return 1; |
| 591 | } |
| 592 | |
Jacob Erlbeck | 6d9118f | 2014-07-03 15:52:49 +0200 | [diff] [blame] | 593 | static void gbprox_attach_tlli_info(struct gbproxy_peer *peer, time_t now, |
| 594 | struct gbproxy_tlli_info *tlli_info) |
| 595 | { |
| 596 | struct gbproxy_patch_state *state = &peer->patch_state; |
| 597 | |
| 598 | tlli_info->timestamp = now; |
| 599 | llist_add(&tlli_info->list, &state->enabled_tllis); |
| 600 | state->enabled_tllis_count += 1; |
Jacob Erlbeck | 985b46e | 2014-08-07 17:23:00 +0200 | [diff] [blame] | 601 | |
| 602 | peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current = |
| 603 | state->enabled_tllis_count; |
Jacob Erlbeck | 6d9118f | 2014-07-03 15:52:49 +0200 | [diff] [blame] | 604 | } |
| 605 | |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 606 | int gbprox_remove_stale_tllis(struct gbproxy_peer *peer, time_t now) |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 607 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 608 | struct gbproxy_patch_state *state = &peer->patch_state; |
Jacob Erlbeck | cefe85c | 2014-08-07 18:35:30 +0200 | [diff] [blame] | 609 | int exceeded_max_len = 0; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 610 | int deleted_count = 0; |
Jacob Erlbeck | cefe85c | 2014-08-07 18:35:30 +0200 | [diff] [blame] | 611 | int check_for_age; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 612 | |
Jacob Erlbeck | cefe85c | 2014-08-07 18:35:30 +0200 | [diff] [blame] | 613 | if (peer->cfg->tlli_max_len > 0) |
| 614 | exceeded_max_len = |
| 615 | state->enabled_tllis_count - peer->cfg->tlli_max_len; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 616 | |
Jacob Erlbeck | cefe85c | 2014-08-07 18:35:30 +0200 | [diff] [blame] | 617 | check_for_age = peer->cfg->tlli_max_age > 0; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 618 | |
Jacob Erlbeck | cefe85c | 2014-08-07 18:35:30 +0200 | [diff] [blame] | 619 | for (; exceeded_max_len > 0; exceeded_max_len--) { |
| 620 | struct gbproxy_tlli_info *tlli_info; |
| 621 | OSMO_ASSERT(!llist_empty(&state->enabled_tllis)); |
| 622 | tlli_info = llist_entry(state->enabled_tllis.prev, |
| 623 | struct gbproxy_tlli_info, |
| 624 | list); |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 625 | LOGP(DGPRS, LOGL_INFO, |
Jacob Erlbeck | cefe85c | 2014-08-07 18:35:30 +0200 | [diff] [blame] | 626 | "Removing TLLI %08x from list " |
| 627 | "(stale, length %d, max_len exceeded)\n", |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 628 | tlli_info->tlli.current, state->enabled_tllis_count); |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 629 | |
| 630 | gbprox_delete_tlli(peer, tlli_info); |
Jacob Erlbeck | cefe85c | 2014-08-07 18:35:30 +0200 | [diff] [blame] | 631 | deleted_count += 1; |
| 632 | } |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 633 | |
Jacob Erlbeck | cefe85c | 2014-08-07 18:35:30 +0200 | [diff] [blame] | 634 | while (check_for_age && !llist_empty(&state->enabled_tllis)) { |
| 635 | time_t age; |
| 636 | struct gbproxy_tlli_info *tlli_info; |
| 637 | tlli_info = llist_entry(state->enabled_tllis.prev, |
| 638 | struct gbproxy_tlli_info, |
| 639 | list); |
| 640 | age = now - tlli_info->timestamp; |
| 641 | /* age < 0 only happens after system time jumps, discard entry */ |
| 642 | if (age <= peer->cfg->tlli_max_age && age >= 0) { |
| 643 | check_for_age = 0; |
| 644 | continue; |
| 645 | } |
| 646 | |
| 647 | LOGP(DGPRS, LOGL_INFO, |
| 648 | "Removing TLLI %08x from list " |
| 649 | "(stale, age %d, max_age exceeded)\n", |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 650 | tlli_info->tlli.current, (int)age); |
Jacob Erlbeck | cefe85c | 2014-08-07 18:35:30 +0200 | [diff] [blame] | 651 | |
| 652 | gbprox_delete_tlli(peer, tlli_info); |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 653 | deleted_count += 1; |
| 654 | } |
| 655 | |
| 656 | return deleted_count; |
| 657 | } |
| 658 | |
Jacob Erlbeck | d893588 | 2014-08-18 15:02:17 +0200 | [diff] [blame] | 659 | static struct gbproxy_tlli_info *gbprox_tlli_info_alloc( |
| 660 | struct gbproxy_peer *peer) |
| 661 | { |
| 662 | struct gbproxy_tlli_info *tlli_info; |
| 663 | |
| 664 | tlli_info = talloc_zero(peer, struct gbproxy_tlli_info); |
| 665 | tlli_info->tlli.ptmsi = GSM_RESERVED_TMSI; |
| 666 | tlli_info->sgsn_tlli.ptmsi = GSM_RESERVED_TMSI; |
| 667 | |
| 668 | return tlli_info; |
| 669 | } |
| 670 | |
| 671 | static void gbprox_detach_tlli_info( |
Jacob Erlbeck | 6d9118f | 2014-07-03 15:52:49 +0200 | [diff] [blame] | 672 | struct gbproxy_peer *peer, |
Jacob Erlbeck | d893588 | 2014-08-18 15:02:17 +0200 | [diff] [blame] | 673 | struct gbproxy_tlli_info *tlli_info) |
Jacob Erlbeck | 6d9118f | 2014-07-03 15:52:49 +0200 | [diff] [blame] | 674 | { |
| 675 | struct gbproxy_patch_state *state = &peer->patch_state; |
| 676 | |
Jacob Erlbeck | d893588 | 2014-08-18 15:02:17 +0200 | [diff] [blame] | 677 | llist_del(&tlli_info->list); |
| 678 | OSMO_ASSERT(state->enabled_tllis_count > 0); |
| 679 | state->enabled_tllis_count -= 1; |
Jacob Erlbeck | 985b46e | 2014-08-07 17:23:00 +0200 | [diff] [blame] | 680 | |
Jacob Erlbeck | d893588 | 2014-08-18 15:02:17 +0200 | [diff] [blame] | 681 | peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current = |
| 682 | state->enabled_tllis_count; |
Jacob Erlbeck | 6d9118f | 2014-07-03 15:52:49 +0200 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | static void gbprox_update_tlli_info(struct gbproxy_tlli_info *tlli_info, |
| 686 | const uint8_t *imsi, size_t imsi_len) |
| 687 | { |
Jacob Erlbeck | 9ac42ba | 2014-08-06 18:55:15 +0200 | [diff] [blame] | 688 | if (!is_mi_imsi(imsi, imsi_len)) |
| 689 | return; |
| 690 | |
Jacob Erlbeck | 6d9118f | 2014-07-03 15:52:49 +0200 | [diff] [blame] | 691 | tlli_info->mi_data_len = imsi_len; |
| 692 | tlli_info->mi_data = |
| 693 | talloc_realloc_size(tlli_info, tlli_info->mi_data, imsi_len); |
| 694 | OSMO_ASSERT(tlli_info->mi_data != NULL); |
| 695 | memcpy(tlli_info->mi_data, imsi, imsi_len); |
| 696 | } |
| 697 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 698 | void gbprox_reassign_tlli(struct gbproxy_tlli_state *tlli_state, |
Jacob Erlbeck | 9ac42ba | 2014-08-06 18:55:15 +0200 | [diff] [blame] | 699 | struct gbproxy_peer *peer, uint32_t new_tlli) |
| 700 | { |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 701 | if (new_tlli == tlli_state->current) |
Jacob Erlbeck | 9ac42ba | 2014-08-06 18:55:15 +0200 | [diff] [blame] | 702 | return; |
| 703 | |
| 704 | LOGP(DGPRS, LOGL_INFO, |
| 705 | "The TLLI has been reassigned from %08x to %08x\n", |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 706 | tlli_state->current, new_tlli); |
Jacob Erlbeck | 9ac42ba | 2014-08-06 18:55:15 +0200 | [diff] [blame] | 707 | |
Jacob Erlbeck | 3113287 | 2014-08-11 17:26:21 +0200 | [diff] [blame] | 708 | /* Remember assigned TLLI */ |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 709 | tlli_state->assigned = new_tlli; |
| 710 | tlli_state->bss_validated = 0; |
| 711 | tlli_state->net_validated = 0; |
Jacob Erlbeck | 3113287 | 2014-08-11 17:26:21 +0200 | [diff] [blame] | 712 | } |
| 713 | |
Jacob Erlbeck | d52f2d1 | 2014-08-18 17:01:32 +0200 | [diff] [blame] | 714 | static uint32_t gbprox_map_tlli(uint32_t other_tlli, |
| 715 | struct gbproxy_tlli_info *tlli_info, int to_bss) |
| 716 | { |
| 717 | uint32_t tlli = 0; |
| 718 | struct gbproxy_tlli_state *src, *dst; |
| 719 | if (to_bss) { |
| 720 | src = &tlli_info->sgsn_tlli; |
| 721 | dst = &tlli_info->tlli; |
| 722 | } else { |
| 723 | src = &tlli_info->tlli; |
| 724 | dst = &tlli_info->sgsn_tlli; |
| 725 | } |
| 726 | if (src->current == other_tlli) |
| 727 | tlli = dst->current; |
| 728 | else if (src->assigned == other_tlli) |
| 729 | tlli = dst->assigned; |
| 730 | |
| 731 | return tlli; |
| 732 | } |
| 733 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 734 | static void gbprox_validate_tlli(struct gbproxy_tlli_state *tlli_state, |
Jacob Erlbeck | 3113287 | 2014-08-11 17:26:21 +0200 | [diff] [blame] | 735 | uint32_t tlli, int to_bss) |
| 736 | { |
| 737 | LOGP(DGPRS, LOGL_DEBUG, |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 738 | "%s({current = %08x, assigned = %08x, net_vld = %d, bss_vld = %d}, %08x)\n", |
| 739 | __func__, tlli_state->current, tlli_state->assigned, |
| 740 | tlli_state->net_validated, tlli_state->bss_validated, tlli); |
Jacob Erlbeck | 3113287 | 2014-08-11 17:26:21 +0200 | [diff] [blame] | 741 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 742 | if (!tlli_state->assigned || tlli_state->assigned != tlli) |
Jacob Erlbeck | 3113287 | 2014-08-11 17:26:21 +0200 | [diff] [blame] | 743 | return; |
| 744 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 745 | /* TODO: Is this ok? Check spec */ |
Jacob Erlbeck | 3113287 | 2014-08-11 17:26:21 +0200 | [diff] [blame] | 746 | if (gprs_tlli_type(tlli) != TLLI_LOCAL) |
| 747 | return; |
| 748 | |
| 749 | /* See GSM 04.08, 4.7.1.5 */ |
| 750 | if (to_bss) |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 751 | tlli_state->net_validated = 1; |
Jacob Erlbeck | 3113287 | 2014-08-11 17:26:21 +0200 | [diff] [blame] | 752 | else |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 753 | tlli_state->bss_validated = 1; |
Jacob Erlbeck | 3113287 | 2014-08-11 17:26:21 +0200 | [diff] [blame] | 754 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 755 | if (!tlli_state->bss_validated || !tlli_state->net_validated) |
Jacob Erlbeck | 3113287 | 2014-08-11 17:26:21 +0200 | [diff] [blame] | 756 | return; |
| 757 | |
| 758 | LOGP(DGPRS, LOGL_INFO, |
| 759 | "The TLLI %08x has been validated (was %08x)\n", |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 760 | tlli_state->assigned, tlli_state->current); |
Jacob Erlbeck | 3113287 | 2014-08-11 17:26:21 +0200 | [diff] [blame] | 761 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 762 | tlli_state->current = tlli; |
| 763 | tlli_state->assigned = 0; |
Jacob Erlbeck | 9ac42ba | 2014-08-06 18:55:15 +0200 | [diff] [blame] | 764 | } |
| 765 | |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 766 | void gbprox_touch_tlli(struct gbproxy_peer *peer, |
| 767 | struct gbproxy_tlli_info *tlli_info, time_t now) |
| 768 | { |
Jacob Erlbeck | d893588 | 2014-08-18 15:02:17 +0200 | [diff] [blame] | 769 | gbprox_detach_tlli_info(peer, tlli_info); |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 770 | gbprox_attach_tlli_info(peer, now, tlli_info); |
| 771 | } |
| 772 | |
| 773 | struct gbproxy_tlli_info *gbprox_register_tlli( |
| 774 | struct gbproxy_peer *peer, uint32_t tlli, |
| 775 | const uint8_t *imsi, size_t imsi_len, time_t now) |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 776 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 777 | struct gbproxy_tlli_info *tlli_info; |
Jacob Erlbeck | 9ac42ba | 2014-08-06 18:55:15 +0200 | [diff] [blame] | 778 | int enable_patching = -1; |
Jacob Erlbeck | 6d9118f | 2014-07-03 15:52:49 +0200 | [diff] [blame] | 779 | int tlli_already_known; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 780 | |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 781 | /* Check, whether the IMSI matches */ |
Jacob Erlbeck | 9ac42ba | 2014-08-06 18:55:15 +0200 | [diff] [blame] | 782 | if (is_mi_imsi(imsi, imsi_len)) { |
| 783 | enable_patching = gbprox_check_imsi(peer, imsi, imsi_len); |
| 784 | if (enable_patching < 0) |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 785 | return NULL; |
Jacob Erlbeck | 9ac42ba | 2014-08-06 18:55:15 +0200 | [diff] [blame] | 786 | } |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 787 | |
Jacob Erlbeck | 6d9118f | 2014-07-03 15:52:49 +0200 | [diff] [blame] | 788 | tlli_info = gbprox_find_tlli(peer, tlli); |
| 789 | |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 790 | if (!tlli_info) { |
| 791 | tlli_info = gbprox_find_tlli_by_mi(peer, imsi, imsi_len); |
| 792 | |
| 793 | if (tlli_info) { |
| 794 | /* TLLI has changed somehow, adjust it */ |
| 795 | LOGP(DGPRS, LOGL_INFO, |
| 796 | "The TLLI has changed from %08x to %08x\n", |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 797 | tlli_info->tlli.current, tlli); |
| 798 | tlli_info->tlli.current = tlli; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
Jacob Erlbeck | d893588 | 2014-08-18 15:02:17 +0200 | [diff] [blame] | 802 | if (!tlli_info) { |
| 803 | tlli_info = gbprox_tlli_info_alloc(peer); |
| 804 | tlli_info->tlli.current = tlli; |
| 805 | } else { |
| 806 | gbprox_detach_tlli_info(peer, tlli_info); |
| 807 | tlli_already_known = 1; |
| 808 | } |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 809 | |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 810 | OSMO_ASSERT(tlli_info != NULL); |
| 811 | |
Jacob Erlbeck | 9ac42ba | 2014-08-06 18:55:15 +0200 | [diff] [blame] | 812 | if (!tlli_already_known) |
| 813 | LOGP(DGPRS, LOGL_INFO, "Adding TLLI %08x to list\n", tlli); |
Jacob Erlbeck | 6d9118f | 2014-07-03 15:52:49 +0200 | [diff] [blame] | 814 | |
Jacob Erlbeck | 9ac42ba | 2014-08-06 18:55:15 +0200 | [diff] [blame] | 815 | gbprox_attach_tlli_info(peer, now, tlli_info); |
| 816 | gbprox_update_tlli_info(tlli_info, imsi, imsi_len); |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 817 | |
Jacob Erlbeck | 9ac42ba | 2014-08-06 18:55:15 +0200 | [diff] [blame] | 818 | if (enable_patching >= 0) |
| 819 | tlli_info->enable_patching = enable_patching; |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 820 | |
| 821 | return tlli_info; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 824 | static void gbprox_unregister_tlli(struct gbproxy_peer *peer, uint32_t tlli) |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 825 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 826 | struct gbproxy_tlli_info *tlli_info; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 827 | |
| 828 | tlli_info = gbprox_find_tlli(peer, tlli); |
| 829 | if (tlli_info) { |
| 830 | LOGP(DGPRS, LOGL_INFO, |
| 831 | "Removing TLLI %08x from list\n", |
| 832 | tlli); |
Holger Hans Peter Freyther | c120d11 | 2014-08-04 12:18:07 +0200 | [diff] [blame] | 833 | gbprox_delete_tlli(peer, tlli_info); |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 837 | static int gbprox_check_tlli(struct gbproxy_peer *peer, uint32_t tlli) |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 838 | { |
Jacob Erlbeck | 9ac42ba | 2014-08-06 18:55:15 +0200 | [diff] [blame] | 839 | struct gbproxy_tlli_info *tlli_info; |
| 840 | |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 841 | LOGP(DGPRS, LOGL_INFO, "Checking TLLI %08x, class: %d\n", |
| 842 | tlli, gprs_tlli_type(tlli)); |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 843 | |
Jacob Erlbeck | 9ac42ba | 2014-08-06 18:55:15 +0200 | [diff] [blame] | 844 | if (!peer->cfg->check_imsi) |
| 845 | return 1; |
| 846 | |
| 847 | tlli_info = gbprox_find_tlli(peer, tlli); |
| 848 | |
| 849 | return tlli_info != NULL && tlli_info->enable_patching; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 850 | } |
| 851 | |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 852 | /* check whether patching is enabled at this level */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 853 | static int patching_is_enabled(struct gbproxy_peer *peer, |
| 854 | enum gbproxy_patch_mode need_at_least) |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 855 | { |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 856 | enum gbproxy_patch_mode patch_mode = peer->cfg->patch_mode; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 857 | if (patch_mode == GBPROX_PATCH_DEFAULT) |
| 858 | patch_mode = GBPROX_PATCH_LLC; |
| 859 | |
| 860 | return need_at_least <= patch_mode; |
| 861 | } |
| 862 | |
| 863 | /* check whether patching is enabled at this level */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 864 | static int patching_is_required(struct gbproxy_peer *peer, |
| 865 | enum gbproxy_patch_mode need_at_least) |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 866 | { |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 867 | return need_at_least <= peer->cfg->patch_mode; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 868 | } |
| 869 | |
Jacob Erlbeck | 2cb9e3f | 2014-08-18 15:41:41 +0200 | [diff] [blame] | 870 | /* update peer according to the BSS message */ |
| 871 | static void gbprox_update_current_raid(uint8_t *raid_enc, |
| 872 | struct gbproxy_peer *peer, |
| 873 | const char *log_text) |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 874 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 875 | struct gbproxy_patch_state *state = &peer->patch_state; |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 876 | const int old_local_mcc = state->local_mcc; |
| 877 | const int old_local_mnc = state->local_mnc; |
Jacob Erlbeck | 2cb9e3f | 2014-08-18 15:41:41 +0200 | [diff] [blame] | 878 | struct gprs_ra_id raid; |
| 879 | |
| 880 | if (!raid_enc) |
| 881 | return; |
| 882 | |
| 883 | gsm48_parse_ra(&raid, raid_enc); |
| 884 | |
| 885 | /* save source side MCC/MNC */ |
| 886 | if (!peer->cfg->core_mcc || raid.mcc == peer->cfg->core_mcc) { |
| 887 | state->local_mcc = 0; |
| 888 | } else { |
| 889 | state->local_mcc = raid.mcc; |
| 890 | } |
| 891 | |
| 892 | if (!peer->cfg->core_mnc || raid.mnc == peer->cfg->core_mnc) { |
| 893 | state->local_mnc = 0; |
| 894 | } else { |
| 895 | state->local_mnc = raid.mnc; |
| 896 | } |
| 897 | |
| 898 | if (old_local_mcc != state->local_mcc || |
| 899 | old_local_mnc != state->local_mnc) |
| 900 | LOGP(DGPRS, LOGL_NOTICE, |
| 901 | "Patching RAID %sactivated, msg: %s, " |
| 902 | "local: %d-%d, core: %d-%d\n", |
| 903 | state->local_mcc || state->local_mnc ? |
| 904 | "" : "de", |
| 905 | log_text, |
| 906 | state->local_mcc, state->local_mnc, |
| 907 | peer->cfg->core_mcc, peer->cfg->core_mnc); |
| 908 | } |
| 909 | |
| 910 | /* patch RA identifier in place */ |
| 911 | static void gbprox_patch_raid(uint8_t *raid_enc, struct gbproxy_peer *peer, |
| 912 | int to_bss, const char *log_text) |
| 913 | { |
| 914 | struct gbproxy_patch_state *state = &peer->patch_state; |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 915 | int old_mcc; |
| 916 | int old_mnc; |
| 917 | struct gprs_ra_id raid; |
| 918 | |
| 919 | gsm48_parse_ra(&raid, raid_enc); |
| 920 | |
| 921 | old_mcc = raid.mcc; |
| 922 | old_mnc = raid.mnc; |
| 923 | |
| 924 | if (!to_bss) { |
| 925 | /* BSS -> SGSN */ |
Jacob Erlbeck | 2cb9e3f | 2014-08-18 15:41:41 +0200 | [diff] [blame] | 926 | if (state->local_mcc) |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 927 | raid.mcc = peer->cfg->core_mcc; |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 928 | |
Jacob Erlbeck | 2cb9e3f | 2014-08-18 15:41:41 +0200 | [diff] [blame] | 929 | if (state->local_mnc) |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 930 | raid.mnc = peer->cfg->core_mnc; |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 931 | } else { |
| 932 | /* SGSN -> BSS */ |
| 933 | if (state->local_mcc) |
| 934 | raid.mcc = state->local_mcc; |
| 935 | |
| 936 | if (state->local_mnc) |
| 937 | raid.mnc = state->local_mnc; |
| 938 | } |
| 939 | |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 940 | if (state->local_mcc || state->local_mnc) { |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 941 | enum gbprox_peer_ctr counter = |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 942 | to_bss ? |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 943 | GBPROX_PEER_CTR_RAID_PATCHED_SGSN : |
| 944 | GBPROX_PEER_CTR_RAID_PATCHED_BSS; |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 945 | |
| 946 | LOGP(DGPRS, LOGL_DEBUG, |
| 947 | "Patching %s to %s: " |
| 948 | "%d-%d-%d-%d -> %d-%d-%d-%d\n", |
| 949 | log_text, |
| 950 | to_bss ? "BSS" : "SGSN", |
| 951 | old_mcc, old_mnc, raid.lac, raid.rac, |
| 952 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 953 | |
| 954 | gsm48_construct_ra(raid_enc, &raid); |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 955 | rate_ctr_inc(&peer->ctrg->ctr[counter]); |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 959 | static void gbprox_patch_apn_ie(struct msgb *msg, |
| 960 | uint8_t *apn_ie, size_t apn_ie_len, |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 961 | struct gbproxy_peer *peer, |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 962 | size_t *new_apn_ie_len, const char *log_text) |
| 963 | { |
| 964 | struct apn_ie_hdr { |
| 965 | uint8_t iei; |
| 966 | uint8_t apn_len; |
| 967 | uint8_t apn[0]; |
| 968 | } *hdr = (void *)apn_ie; |
| 969 | |
| 970 | size_t apn_len = hdr->apn_len; |
| 971 | uint8_t *apn = hdr->apn; |
| 972 | |
| 973 | OSMO_ASSERT(apn_ie_len == apn_len + sizeof(struct apn_ie_hdr)); |
| 974 | OSMO_ASSERT(apn_ie_len > 2 && apn_ie_len <= 102); |
| 975 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 976 | if (peer->cfg->core_apn_size == 0) { |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 977 | char str1[110]; |
| 978 | /* Remove the IE */ |
| 979 | LOGP(DGPRS, LOGL_DEBUG, |
| 980 | "Patching %s to SGSN: Removing APN '%s'\n", |
| 981 | log_text, |
Holger Hans Peter Freyther | 8ac3a72 | 2014-08-04 11:52:52 +0200 | [diff] [blame] | 982 | gprs_apn_to_str(str1, apn, apn_len)); |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 983 | |
| 984 | *new_apn_ie_len = 0; |
Holger Hans Peter Freyther | 8ac3a72 | 2014-08-04 11:52:52 +0200 | [diff] [blame] | 985 | gprs_msgb_resize_area(msg, apn_ie, apn_ie_len, 0); |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 986 | } else { |
| 987 | /* Resize the IE */ |
| 988 | char str1[110]; |
| 989 | char str2[110]; |
| 990 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 991 | OSMO_ASSERT(peer->cfg->core_apn_size <= 100); |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 992 | |
| 993 | LOGP(DGPRS, LOGL_DEBUG, |
| 994 | "Patching %s to SGSN: " |
| 995 | "Replacing APN '%s' -> '%s'\n", |
| 996 | log_text, |
Holger Hans Peter Freyther | 8ac3a72 | 2014-08-04 11:52:52 +0200 | [diff] [blame] | 997 | gprs_apn_to_str(str1, apn, apn_len), |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 998 | gprs_apn_to_str(str2, peer->cfg->core_apn, |
| 999 | peer->cfg->core_apn_size)); |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1000 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1001 | *new_apn_ie_len = peer->cfg->core_apn_size + 2; |
| 1002 | gprs_msgb_resize_area(msg, apn, apn_len, peer->cfg->core_apn_size); |
| 1003 | memcpy(apn, peer->cfg->core_apn, peer->cfg->core_apn_size); |
| 1004 | hdr->apn_len = peer->cfg->core_apn_size; |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1007 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_APN_PATCHED]); |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1008 | } |
| 1009 | |
Jacob Erlbeck | d65d92d | 2014-08-18 17:23:01 +0200 | [diff] [blame] | 1010 | static int gbprox_patch_tlli(uint8_t *tlli_enc, |
| 1011 | struct gbproxy_peer *peer, |
| 1012 | uint32_t new_tlli, |
| 1013 | int to_bss, const char *log_text) |
| 1014 | { |
| 1015 | uint32_t tlli_be; |
| 1016 | uint32_t tlli; |
| 1017 | enum gbprox_peer_ctr counter = |
| 1018 | to_bss ? |
| 1019 | GBPROX_PEER_CTR_TLLI_PATCHED_SGSN : |
| 1020 | GBPROX_PEER_CTR_TLLI_PATCHED_BSS; |
| 1021 | |
| 1022 | memcpy(&tlli_be, tlli_enc, sizeof(tlli_be)); |
| 1023 | tlli = ntohl(tlli_be); |
| 1024 | |
| 1025 | if (tlli == new_tlli) |
| 1026 | return 0; |
| 1027 | |
| 1028 | LOGP(DGPRS, LOGL_DEBUG, |
| 1029 | "Patching %ss: " |
| 1030 | "Replacing %08x -> %08x\n", |
| 1031 | log_text, tlli, new_tlli); |
| 1032 | |
| 1033 | tlli_be = htonl(new_tlli); |
| 1034 | memcpy(tlli_enc, &tlli_be, sizeof(tlli_be)); |
| 1035 | |
| 1036 | rate_ctr_inc(&peer->ctrg->ctr[counter]); |
| 1037 | |
| 1038 | return 1; |
| 1039 | } |
| 1040 | |
| 1041 | static int gbprox_patch_ptmsi(uint8_t *ptmsi_enc, |
| 1042 | struct gbproxy_peer *peer, |
| 1043 | uint32_t new_ptmsi, |
| 1044 | int to_bss, const char *log_text) |
| 1045 | { |
| 1046 | uint32_t ptmsi_be; |
| 1047 | uint32_t ptmsi; |
| 1048 | enum gbprox_peer_ctr counter = |
| 1049 | to_bss ? |
| 1050 | GBPROX_PEER_CTR_PTMSI_PATCHED_SGSN : |
| 1051 | GBPROX_PEER_CTR_PTMSI_PATCHED_BSS; |
| 1052 | memcpy(&ptmsi_be, ptmsi_enc + 1, sizeof(ptmsi_be)); |
| 1053 | ptmsi = ntohl(ptmsi_be); |
| 1054 | |
| 1055 | if (ptmsi == new_ptmsi) |
| 1056 | return 0; |
| 1057 | |
| 1058 | LOGP(DGPRS, LOGL_DEBUG, |
| 1059 | "Patching %ss: " |
| 1060 | "Replacing %08x -> %08x\n", |
| 1061 | log_text, ptmsi, new_ptmsi); |
| 1062 | |
| 1063 | ptmsi_be = htonl(new_ptmsi); |
| 1064 | memcpy(ptmsi_enc + 1, &ptmsi_be, sizeof(ptmsi_be)); |
| 1065 | |
| 1066 | rate_ctr_inc(&peer->ctrg->ctr[counter]); |
| 1067 | |
| 1068 | return 1; |
| 1069 | } |
| 1070 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1071 | static int gbprox_parse_gmm_attach_req(uint8_t *data, size_t data_len, |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 1072 | struct gbproxy_parse_context *parse_ctx) |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1073 | { |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1074 | uint8_t *value; |
| 1075 | size_t value_len; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1076 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1077 | parse_ctx->llc_msg_name = "ATTACH_REQ"; |
| 1078 | |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1079 | /* Skip MS network capability */ |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1080 | if (lv_shift(&data, &data_len, NULL, &value_len) <= 0 || |
| 1081 | value_len < 1 || value_len > 2) |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1082 | /* invalid */ |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1083 | return 0;; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1084 | |
| 1085 | /* Skip Attach type */ |
| 1086 | /* Skip Ciphering key sequence number */ |
| 1087 | /* Skip DRX parameter */ |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1088 | v_fixed_shift(&data, &data_len, 3, NULL); |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1089 | |
Jacob Erlbeck | dc6dcdf | 2014-08-06 15:16:45 +0200 | [diff] [blame] | 1090 | /* Get Mobile identity */ |
| 1091 | if (lv_shift(&data, &data_len, &value, &value_len) <= 0 || |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1092 | value_len < 5 || value_len > 8) |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1093 | /* invalid */ |
Jacob Erlbeck | dc6dcdf | 2014-08-06 15:16:45 +0200 | [diff] [blame] | 1094 | return 0; |
| 1095 | |
| 1096 | if (is_mi_tmsi(value, value_len)) { |
| 1097 | parse_ctx->ptmsi_enc = value; |
| 1098 | } else if (is_mi_imsi(value, value_len)) { |
| 1099 | parse_ctx->imsi = value; |
| 1100 | parse_ctx->imsi_len = value_len; |
| 1101 | } |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1102 | |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1103 | if (v_fixed_shift(&data, &data_len, 6, &value) <= 0) |
| 1104 | return 0; |
| 1105 | |
Jacob Erlbeck | 2cb9e3f | 2014-08-18 15:41:41 +0200 | [diff] [blame] | 1106 | parse_ctx->old_raid_enc = value; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1107 | |
| 1108 | return 1; |
| 1109 | } |
| 1110 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1111 | static int gbprox_parse_gmm_attach_ack(uint8_t *data, size_t data_len, |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 1112 | struct gbproxy_parse_context *parse_ctx) |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1113 | { |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1114 | uint8_t *value; |
Jacob Erlbeck | f5b2085 | 2014-06-27 18:10:53 +0200 | [diff] [blame] | 1115 | size_t value_len; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1116 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1117 | parse_ctx->llc_msg_name = "ATTACH_ACK"; |
| 1118 | |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1119 | /* Skip Attach result */ |
| 1120 | /* Skip Force to standby */ |
| 1121 | /* Skip Periodic RA update timer */ |
| 1122 | /* Skip Radio priority for SMS */ |
| 1123 | /* Skip Spare half octet */ |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1124 | v_fixed_shift(&data, &data_len, 3, NULL); |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1125 | |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1126 | if (v_fixed_shift(&data, &data_len, 6, &value) <= 0) |
| 1127 | return 0; |
| 1128 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1129 | parse_ctx->raid_enc = value; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1130 | |
Jacob Erlbeck | f5b2085 | 2014-06-27 18:10:53 +0200 | [diff] [blame] | 1131 | /* Skip P-TMSI signature (P-TMSI signature, opt, TV, length 4) */ |
| 1132 | tv_fixed_match(&data, &data_len, GSM48_IE_GMM_PTMSI_SIG, 3, NULL); |
| 1133 | |
| 1134 | /* Skip Negotiated READY timer value (GPRS timer, opt, TV, length 2) */ |
| 1135 | tv_fixed_match(&data, &data_len, GSM48_IE_GMM_TIMER_READY, 1, NULL); |
| 1136 | |
| 1137 | /* Allocated P-TMSI (Mobile identity, opt, TLV, length 7) */ |
| 1138 | if (tlv_match(&data, &data_len, GSM48_IE_GMM_ALLOC_PTMSI, |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1139 | &value, &value_len) > 0 && |
| 1140 | is_mi_tmsi(value, value_len)) |
Jacob Erlbeck | dc6dcdf | 2014-08-06 15:16:45 +0200 | [diff] [blame] | 1141 | parse_ctx->new_ptmsi_enc = value; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1142 | return 1; |
| 1143 | } |
| 1144 | |
Jacob Erlbeck | f95340d | 2014-08-11 15:07:37 +0200 | [diff] [blame] | 1145 | static int gbprox_parse_gmm_detach_req(uint8_t *data, size_t data_len, |
| 1146 | struct gbproxy_parse_context *parse_ctx) |
| 1147 | { |
| 1148 | uint8_t *value; |
| 1149 | size_t value_len; |
| 1150 | int detach_type; |
| 1151 | int power_off; |
| 1152 | |
| 1153 | parse_ctx->llc_msg_name = "DETACH_REQ"; |
| 1154 | |
| 1155 | /* Skip spare half octet */ |
| 1156 | /* Get Detach type */ |
| 1157 | if (v_fixed_shift(&data, &data_len, 1, &value) <= 0) |
| 1158 | /* invalid */ |
| 1159 | return 0; |
| 1160 | |
| 1161 | detach_type = *value & 0x07; |
| 1162 | power_off = *value & 0x08 ? 1 : 0; |
| 1163 | |
| 1164 | if (!parse_ctx->to_bss) { |
| 1165 | /* Mobile originated */ |
| 1166 | |
| 1167 | if (power_off) |
| 1168 | parse_ctx->invalidate_tlli = 1; |
| 1169 | |
| 1170 | /* Get P-TMSI (Mobile identity), see GSM 24.008, 9.4.5.2 */ |
| 1171 | if (tlv_match(&data, &data_len, |
| 1172 | GSM48_IE_GMM_ALLOC_PTMSI, &value, &value_len) > 0) |
| 1173 | { |
| 1174 | if (is_mi_tmsi(value, value_len)) |
| 1175 | parse_ctx->ptmsi_enc = value; |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | return 1; |
| 1180 | } |
| 1181 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1182 | static int gbprox_parse_gmm_ra_upd_req(uint8_t *data, size_t data_len, |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 1183 | struct gbproxy_parse_context *parse_ctx) |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1184 | { |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1185 | uint8_t *value; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1186 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1187 | parse_ctx->llc_msg_name = "RA_UPD_REQ"; |
| 1188 | |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1189 | /* Skip Update type */ |
| 1190 | /* Skip GPRS ciphering key sequence number */ |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1191 | v_fixed_shift(&data, &data_len, 1, NULL); |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1192 | |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1193 | if (v_fixed_shift(&data, &data_len, 6, &value) <= 0) |
| 1194 | return 0; |
| 1195 | |
Jacob Erlbeck | 2cb9e3f | 2014-08-18 15:41:41 +0200 | [diff] [blame] | 1196 | parse_ctx->old_raid_enc = value; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1197 | |
| 1198 | return 1; |
| 1199 | } |
| 1200 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1201 | static int gbprox_parse_gmm_ra_upd_ack(uint8_t *data, size_t data_len, |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 1202 | struct gbproxy_parse_context *parse_ctx) |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1203 | { |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1204 | uint8_t *value; |
Jacob Erlbeck | f5b2085 | 2014-06-27 18:10:53 +0200 | [diff] [blame] | 1205 | size_t value_len; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1206 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1207 | parse_ctx->llc_msg_name = "RA_UPD_ACK"; |
| 1208 | |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1209 | /* Skip Force to standby */ |
| 1210 | /* Skip Update result */ |
| 1211 | /* Skip Periodic RA update timer */ |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1212 | v_fixed_shift(&data, &data_len, 2, NULL); |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1213 | |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1214 | if (v_fixed_shift(&data, &data_len, 6, &value) <= 0) |
| 1215 | return 0; |
| 1216 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1217 | parse_ctx->raid_enc = value; |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1218 | |
Jacob Erlbeck | f5b2085 | 2014-06-27 18:10:53 +0200 | [diff] [blame] | 1219 | /* Skip P-TMSI signature (P-TMSI signature, opt, TV, length 4) */ |
| 1220 | tv_fixed_match(&data, &data_len, GSM48_IE_GMM_PTMSI_SIG, 3, NULL); |
| 1221 | |
| 1222 | /* Allocated P-TMSI (Mobile identity, opt, TLV, length 7) */ |
| 1223 | if (tlv_match(&data, &data_len, GSM48_IE_GMM_ALLOC_PTMSI, |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1224 | &value, &value_len) > 0 && |
| 1225 | is_mi_tmsi(value, value_len)) |
Jacob Erlbeck | dc6dcdf | 2014-08-06 15:16:45 +0200 | [diff] [blame] | 1226 | parse_ctx->new_ptmsi_enc = value; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1227 | |
| 1228 | return 1; |
| 1229 | } |
| 1230 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1231 | static int gbprox_parse_gmm_ptmsi_reall_cmd(uint8_t *data, size_t data_len, |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 1232 | struct gbproxy_parse_context *parse_ctx) |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1233 | { |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1234 | uint8_t *value; |
| 1235 | size_t value_len; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1236 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1237 | parse_ctx->llc_msg_name = "PTMSI_REALL_CMD"; |
| 1238 | |
Jacob Erlbeck | f5b2085 | 2014-06-27 18:10:53 +0200 | [diff] [blame] | 1239 | LOGP(DLLC, LOGL_NOTICE, |
| 1240 | "Got P-TMSI Reallocation Command which is not covered by unit tests yet.\n"); |
| 1241 | |
| 1242 | /* Allocated P-TMSI */ |
| 1243 | if (lv_shift(&data, &data_len, &value, &value_len) > 0 && |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1244 | is_mi_tmsi(value, value_len)) |
Jacob Erlbeck | dc6dcdf | 2014-08-06 15:16:45 +0200 | [diff] [blame] | 1245 | parse_ctx->new_ptmsi_enc = value; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1246 | |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1247 | if (v_fixed_shift(&data, &data_len, 6, &value) <= 0) |
| 1248 | return 0; |
| 1249 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1250 | parse_ctx->raid_enc = value; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1251 | |
| 1252 | return 1; |
| 1253 | } |
| 1254 | |
Jacob Erlbeck | dc6dcdf | 2014-08-06 15:16:45 +0200 | [diff] [blame] | 1255 | static int gbprox_parse_gmm_id_resp(uint8_t *data, size_t data_len, |
| 1256 | struct gbproxy_parse_context *parse_ctx) |
| 1257 | { |
| 1258 | uint8_t *value; |
| 1259 | size_t value_len; |
| 1260 | |
| 1261 | parse_ctx->llc_msg_name = "ID_RESP"; |
| 1262 | |
| 1263 | /* Mobile identity, Mobile identity 10.5.1.4, M LV 2-10 */ |
| 1264 | if (lv_shift(&data, &data_len, &value, &value_len) <= 0 || |
| 1265 | value_len < 1 || value_len > 9) |
| 1266 | /* invalid */ |
| 1267 | return 0; |
| 1268 | |
| 1269 | if (is_mi_tmsi(value, value_len)) { |
| 1270 | parse_ctx->ptmsi_enc = value; |
| 1271 | } else if (is_mi_imsi(value, value_len)) { |
| 1272 | parse_ctx->imsi = value; |
| 1273 | parse_ctx->imsi_len = value_len; |
| 1274 | } |
| 1275 | |
| 1276 | return 1; |
| 1277 | } |
| 1278 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1279 | static int gbprox_parse_gsm_act_pdp_req(uint8_t *data, size_t data_len, |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 1280 | struct gbproxy_parse_context *parse_ctx) |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1281 | { |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1282 | ssize_t old_len; |
| 1283 | uint8_t *value; |
| 1284 | size_t value_len; |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1285 | |
| 1286 | parse_ctx->llc_msg_name = "ACT_PDP_REQ"; |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1287 | |
| 1288 | /* Skip Requested NSAPI */ |
| 1289 | /* Skip Requested LLC SAPI */ |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1290 | v_fixed_shift(&data, &data_len, 2, NULL); |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1291 | |
| 1292 | /* Skip Requested QoS (support 04.08 and 24.008) */ |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1293 | if (lv_shift(&data, &data_len, NULL, &value_len) <= 0 || |
| 1294 | value_len < 4 || value_len > 14) |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1295 | /* invalid */ |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1296 | return 0;; |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1297 | |
| 1298 | /* Skip Requested PDP address */ |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1299 | if (lv_shift(&data, &data_len, NULL, &value_len) <= 0 || |
| 1300 | value_len < 2 || value_len > 18) |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1301 | /* invalid */ |
| 1302 | return 0; |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1303 | |
| 1304 | /* Access point name */ |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1305 | old_len = tlv_match(&data, &data_len, |
| 1306 | GSM48_IE_GSM_APN, &value, &value_len); |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1307 | |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1308 | if (old_len > 0 && value_len >=1 && value_len <= 100) { |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1309 | parse_ctx->apn_ie = data - old_len; |
| 1310 | parse_ctx->apn_ie_len = old_len; |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1311 | } |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1312 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1313 | return 1; |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1314 | } |
| 1315 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1316 | struct gbproxy_peer *peer_by_bssgp_tlv(struct gbproxy_config *cfg, struct tlv_parsed *tp) |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1317 | { |
| 1318 | if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
| 1319 | uint16_t bvci; |
| 1320 | |
| 1321 | bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
| 1322 | if (bvci >= 2) |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1323 | return peer_by_bvci(cfg, bvci); |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) { |
| 1327 | uint8_t *rai = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA); |
| 1328 | /* Only compare LAC part, since MCC/MNC are possibly patched. |
| 1329 | * Since the LAC of different BSS must be different when |
| 1330 | * MCC/MNC are patched, collisions shouldn't happen. */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1331 | return peer_by_lac(cfg, rai); |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) { |
| 1335 | uint8_t *lai = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 1336 | return peer_by_lac(cfg, lai); |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | return NULL; |
| 1340 | } |
| 1341 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1342 | static int gbprox_parse_dtap(uint8_t *data, size_t data_len, |
Holger Hans Peter Freyther | 9f9bd13 | 2014-08-04 16:06:20 +0200 | [diff] [blame] | 1343 | struct gbproxy_parse_context *parse_ctx) __attribute__((nonnull)); |
| 1344 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1345 | static int gbprox_parse_dtap(uint8_t *data, size_t data_len, |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 1346 | struct gbproxy_parse_context *parse_ctx) |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1347 | { |
| 1348 | struct gsm48_hdr *g48h; |
| 1349 | |
Jacob Erlbeck | 4b3d038 | 2014-06-27 18:25:53 +0200 | [diff] [blame] | 1350 | if (v_fixed_shift(&data, &data_len, sizeof(*g48h), (uint8_t **)&g48h) <= 0) |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1351 | return 0; |
| 1352 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1353 | parse_ctx->g48_hdr = g48h; |
| 1354 | |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1355 | if ((g48h->proto_discr & 0x0f) != GSM48_PDISC_MM_GPRS && |
| 1356 | (g48h->proto_discr & 0x0f) != GSM48_PDISC_SM_GPRS) |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1357 | return 1; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1358 | |
| 1359 | switch (g48h->msg_type) { |
| 1360 | case GSM48_MT_GMM_ATTACH_REQ: |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1361 | return gbprox_parse_gmm_attach_req(data, data_len, parse_ctx); |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1362 | |
| 1363 | case GSM48_MT_GMM_ATTACH_ACK: |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1364 | return gbprox_parse_gmm_attach_ack(data, data_len, parse_ctx); |
Jacob Erlbeck | 481da7c | 2014-06-27 10:44:46 +0200 | [diff] [blame] | 1365 | |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1366 | case GSM48_MT_GMM_RA_UPD_REQ: |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1367 | return gbprox_parse_gmm_ra_upd_req(data, data_len, parse_ctx); |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1368 | |
| 1369 | case GSM48_MT_GMM_RA_UPD_ACK: |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1370 | return gbprox_parse_gmm_ra_upd_ack(data, data_len, parse_ctx); |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1371 | |
| 1372 | case GSM48_MT_GMM_PTMSI_REALL_CMD: |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1373 | return gbprox_parse_gmm_ptmsi_reall_cmd(data, data_len, parse_ctx); |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1374 | |
Jacob Erlbeck | 5620c6d | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 1375 | case GSM48_MT_GSM_ACT_PDP_REQ: |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1376 | return gbprox_parse_gsm_act_pdp_req(data, data_len, parse_ctx); |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 1377 | |
Jacob Erlbeck | dc6dcdf | 2014-08-06 15:16:45 +0200 | [diff] [blame] | 1378 | case GSM48_MT_GMM_ID_RESP: |
| 1379 | return gbprox_parse_gmm_id_resp(data, data_len, parse_ctx); |
Jacob Erlbeck | dc6dcdf | 2014-08-06 15:16:45 +0200 | [diff] [blame] | 1380 | |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 1381 | case GSM48_MT_GMM_DETACH_REQ: |
Jacob Erlbeck | f95340d | 2014-08-11 15:07:37 +0200 | [diff] [blame] | 1382 | return gbprox_parse_gmm_detach_req(data, data_len, parse_ctx); |
Jacob Erlbeck | 2f2aa2e | 2014-08-05 18:28:51 +0200 | [diff] [blame] | 1383 | |
| 1384 | case GSM48_MT_GMM_DETACH_ACK: |
| 1385 | parse_ctx->llc_msg_name = "DETACH_ACK"; |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1386 | parse_ctx->invalidate_tlli = 1; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 1387 | break; |
| 1388 | |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1389 | default: |
| 1390 | break; |
| 1391 | }; |
| 1392 | |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1393 | return 1; |
| 1394 | } |
| 1395 | |
| 1396 | static int allow_message_patching(struct gbproxy_peer *peer, int msg_type) |
| 1397 | { |
| 1398 | if (msg_type >= GSM48_MT_GSM_ACT_PDP_REQ) { |
| 1399 | return patching_is_enabled(peer, GBPROX_PATCH_LLC_GSM); |
| 1400 | } else if (msg_type > GSM48_MT_GMM_ATTACH_REJ) { |
| 1401 | return patching_is_enabled(peer, GBPROX_PATCH_LLC); |
| 1402 | } else if (msg_type > GSM48_MT_GMM_ATTACH_REQ) { |
| 1403 | return patching_is_enabled(peer, GBPROX_PATCH_LLC_ATTACH); |
| 1404 | } else { |
| 1405 | return patching_is_enabled(peer, GBPROX_PATCH_LLC_ATTACH_REQ); |
| 1406 | } |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1407 | } |
| 1408 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1409 | static int gbprox_parse_llc(uint8_t *llc, size_t llc_len, |
| 1410 | struct gbproxy_parse_context *parse_ctx) __attribute__((nonnull)); |
Jacob Erlbeck | 2f2aa2e | 2014-08-05 18:28:51 +0200 | [diff] [blame] | 1411 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1412 | static int gbprox_parse_llc(uint8_t *llc, size_t llc_len, |
| 1413 | struct gbproxy_parse_context *parse_ctx) |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1414 | { |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1415 | struct gprs_llc_hdr_parsed *ghp = &parse_ctx->llc_hdr_parsed; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1416 | int rc; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1417 | int fcs; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1418 | |
| 1419 | /* parse LLC */ |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1420 | rc = gprs_llc_hdr_parse(ghp, llc, llc_len); |
| 1421 | gprs_llc_hdr_dump(ghp); |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1422 | if (rc != 0) { |
| 1423 | LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n"); |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1424 | return 0; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1425 | } |
| 1426 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1427 | fcs = gprs_llc_fcs(llc, ghp->crc_length); |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1428 | LOGP(DLLC, LOGL_DEBUG, "Got LLC message, CRC: %06x (computed %06x)\n", |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1429 | ghp->fcs, fcs); |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1430 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1431 | if (!ghp->data) |
| 1432 | return 0; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1433 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1434 | if (ghp->sapi != GPRS_SAPI_GMM) |
Jacob Erlbeck | 33ba981 | 2014-08-18 16:50:11 +0200 | [diff] [blame] | 1435 | return 1; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1436 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1437 | if (ghp->cmd != GPRS_LLC_UI) |
Jacob Erlbeck | 33ba981 | 2014-08-18 16:50:11 +0200 | [diff] [blame] | 1438 | return 1; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1439 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1440 | if (ghp->is_encrypted) { |
| 1441 | parse_ctx->need_decryption = 1; |
| 1442 | return 0; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1443 | } |
| 1444 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1445 | return gbprox_parse_dtap(ghp->data, ghp->data_len, parse_ctx); |
| 1446 | } |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1447 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1448 | static int gbprox_patch_llc(struct msgb *msg, uint8_t *llc, size_t llc_len, |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 1449 | struct gbproxy_peer *peer, |
| 1450 | struct gbproxy_tlli_info *tlli_info, int *len_change, |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1451 | struct gbproxy_parse_context *parse_ctx) __attribute__((nonnull)); |
Jacob Erlbeck | f40a6c9 | 2014-06-27 19:00:45 +0200 | [diff] [blame] | 1452 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1453 | static int gbprox_patch_llc(struct msgb *msg, uint8_t *llc, size_t llc_len, |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 1454 | struct gbproxy_peer *peer, |
| 1455 | struct gbproxy_tlli_info *tlli_info, int *len_change, |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1456 | struct gbproxy_parse_context *parse_ctx) |
| 1457 | { |
| 1458 | struct gprs_llc_hdr_parsed *ghp = &parse_ctx->llc_hdr_parsed; |
| 1459 | int have_patched = 0; |
| 1460 | int fcs; |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1461 | |
Jacob Erlbeck | 33ba981 | 2014-08-18 16:50:11 +0200 | [diff] [blame] | 1462 | if (parse_ctx->g48_hdr && !allow_message_patching(peer, parse_ctx->g48_hdr->msg_type)) |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1463 | return have_patched; |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1464 | |
Jacob Erlbeck | d65d92d | 2014-08-18 17:23:01 +0200 | [diff] [blame] | 1465 | if (parse_ctx->ptmsi_enc && tlli_info) { |
| 1466 | uint32_t ptmsi; |
| 1467 | if (parse_ctx->to_bss) |
| 1468 | ptmsi = tlli_info->tlli.ptmsi; |
| 1469 | else |
| 1470 | ptmsi = tlli_info->sgsn_tlli.ptmsi; |
| 1471 | |
| 1472 | if (ptmsi != GSM_RESERVED_TMSI) { |
| 1473 | if (gbprox_patch_ptmsi(parse_ctx->ptmsi_enc, peer, |
| 1474 | ptmsi, parse_ctx->to_bss, "P-TMSI")) |
| 1475 | have_patched = 1; |
| 1476 | } else { |
| 1477 | /* TODO: invalidate old RAI if present (see below) */ |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | if (parse_ctx->new_ptmsi_enc && tlli_info) { |
| 1482 | uint32_t ptmsi; |
| 1483 | if (parse_ctx->to_bss) |
| 1484 | ptmsi = tlli_info->tlli.ptmsi; |
| 1485 | else |
| 1486 | ptmsi = tlli_info->sgsn_tlli.ptmsi; |
| 1487 | |
| 1488 | OSMO_ASSERT(ptmsi); |
| 1489 | if (gbprox_patch_ptmsi(parse_ctx->new_ptmsi_enc, peer, |
| 1490 | ptmsi, parse_ctx->to_bss, "new P-TMSI")) |
| 1491 | have_patched = 1; |
| 1492 | } |
| 1493 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1494 | if (parse_ctx->raid_enc) { |
| 1495 | gbprox_patch_raid(parse_ctx->raid_enc, peer, parse_ctx->to_bss, |
| 1496 | parse_ctx->llc_msg_name); |
| 1497 | have_patched = 1; |
Jacob Erlbeck | f5b2085 | 2014-06-27 18:10:53 +0200 | [diff] [blame] | 1498 | } |
| 1499 | |
Jacob Erlbeck | 2cb9e3f | 2014-08-18 15:41:41 +0200 | [diff] [blame] | 1500 | if (parse_ctx->old_raid_enc && parse_ctx->old_raid_matches) { |
| 1501 | /* TODO: Patch to invalid if P-TMSI unknown. */ |
| 1502 | gbprox_patch_raid(parse_ctx->old_raid_enc, peer, parse_ctx->to_bss, |
| 1503 | parse_ctx->llc_msg_name); |
| 1504 | have_patched = 1; |
| 1505 | } |
| 1506 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1507 | if (parse_ctx->apn_ie && |
| 1508 | peer->cfg->core_apn && |
| 1509 | !parse_ctx->to_bss && |
| 1510 | gbprox_check_tlli(peer, parse_ctx->tlli)) { |
| 1511 | size_t new_len; |
| 1512 | gbprox_patch_apn_ie(msg, |
| 1513 | parse_ctx->apn_ie, parse_ctx->apn_ie_len, |
| 1514 | peer, &new_len, parse_ctx->llc_msg_name); |
| 1515 | *len_change += (int)new_len - (int)parse_ctx->apn_ie_len; |
Jacob Erlbeck | 2f2aa2e | 2014-08-05 18:28:51 +0200 | [diff] [blame] | 1516 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1517 | have_patched = 1; |
| 1518 | } |
| 1519 | |
| 1520 | if (have_patched) { |
| 1521 | llc_len += *len_change; |
| 1522 | ghp->crc_length += *len_change; |
| 1523 | |
| 1524 | /* Fix FCS */ |
| 1525 | fcs = gprs_llc_fcs(llc, ghp->crc_length); |
| 1526 | LOGP(DLLC, LOGL_DEBUG, "Updated LLC message, CRC: %06x -> %06x\n", |
| 1527 | ghp->fcs, fcs); |
| 1528 | |
| 1529 | llc[llc_len - 3] = fcs & 0xff; |
| 1530 | llc[llc_len - 2] = (fcs >> 8) & 0xff; |
| 1531 | llc[llc_len - 1] = (fcs >> 16) & 0xff; |
| 1532 | } |
| 1533 | |
| 1534 | return have_patched; |
Jacob Erlbeck | 2f2aa2e | 2014-08-05 18:28:51 +0200 | [diff] [blame] | 1535 | } |
| 1536 | |
Jacob Erlbeck | cae8b0c | 2014-08-07 16:42:51 +0200 | [diff] [blame] | 1537 | static void gbprox_log_parse_context(struct gbproxy_parse_context *parse_ctx, |
| 1538 | const char *default_msg_name) |
| 1539 | { |
| 1540 | const char *msg_name = default_msg_name; |
| 1541 | const char *sep = ""; |
| 1542 | |
| 1543 | if (!parse_ctx->tlli_enc && |
| 1544 | !parse_ctx->ptmsi_enc && |
| 1545 | !parse_ctx->new_ptmsi_enc && |
| 1546 | !parse_ctx->imsi) |
| 1547 | return; |
| 1548 | |
| 1549 | if (parse_ctx->llc_msg_name) |
| 1550 | msg_name = parse_ctx->llc_msg_name; |
| 1551 | |
| 1552 | LOGP(DGPRS, LOGL_DEBUG, "%s: Got", msg_name); |
| 1553 | |
| 1554 | if (parse_ctx->tlli_enc) { |
| 1555 | LOGP(DGPRS, LOGL_DEBUG, "%s TLLI %08x", sep, parse_ctx->tlli); |
| 1556 | sep = ","; |
| 1557 | } |
| 1558 | |
Jacob Erlbeck | 2cb9e3f | 2014-08-18 15:41:41 +0200 | [diff] [blame] | 1559 | if (parse_ctx->bssgp_raid_enc) { |
| 1560 | struct gprs_ra_id raid; |
| 1561 | gsm48_parse_ra(&raid, parse_ctx->bssgp_raid_enc); |
| 1562 | LOGP(DGPRS, LOGL_DEBUG, "%s BSSGP RAID %u-%u-%u-%u", sep, |
| 1563 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 1564 | sep = ","; |
| 1565 | } |
| 1566 | |
| 1567 | if (parse_ctx->raid_enc) { |
| 1568 | struct gprs_ra_id raid; |
| 1569 | gsm48_parse_ra(&raid, parse_ctx->raid_enc); |
| 1570 | LOGP(DGPRS, LOGL_DEBUG, "%s RAID %u-%u-%u-%u", sep, |
| 1571 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 1572 | sep = ","; |
| 1573 | } |
| 1574 | |
| 1575 | if (parse_ctx->old_raid_enc) { |
| 1576 | struct gprs_ra_id raid; |
| 1577 | gsm48_parse_ra(&raid, parse_ctx->old_raid_enc); |
| 1578 | LOGP(DGPRS, LOGL_DEBUG, "%s old RAID %u-%u-%u-%u", sep, |
| 1579 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 1580 | sep = ","; |
| 1581 | } |
| 1582 | |
Jacob Erlbeck | cae8b0c | 2014-08-07 16:42:51 +0200 | [diff] [blame] | 1583 | if (parse_ctx->ptmsi_enc) { |
| 1584 | uint32_t ptmsi = GSM_RESERVED_TMSI; |
| 1585 | int ok; |
| 1586 | ok = parse_mi_tmsi(parse_ctx->ptmsi_enc, GSM48_TMSI_LEN, &ptmsi); |
| 1587 | LOGP(DGPRS, LOGL_DEBUG, "%s PTMSI %08x%s", |
| 1588 | sep, ptmsi, ok ? "" : " (parse error)"); |
| 1589 | sep = ","; |
| 1590 | } |
| 1591 | |
| 1592 | if (parse_ctx->new_ptmsi_enc) { |
| 1593 | uint32_t new_ptmsi = GSM_RESERVED_TMSI; |
| 1594 | int ok; |
| 1595 | ok = parse_mi_tmsi(parse_ctx->new_ptmsi_enc, GSM48_TMSI_LEN, |
| 1596 | &new_ptmsi); |
| 1597 | LOGP(DGPRS, LOGL_DEBUG, "%s new PTMSI %08x%s", |
| 1598 | sep, new_ptmsi, ok ? "" : " (parse error)"); |
| 1599 | sep = ","; |
| 1600 | } |
| 1601 | |
| 1602 | if (parse_ctx->imsi) { |
| 1603 | char mi_buf[200]; |
| 1604 | mi_buf[0] = '\0'; |
| 1605 | gsm48_mi_to_string(mi_buf, sizeof(mi_buf), |
| 1606 | parse_ctx->imsi, parse_ctx->imsi_len); |
| 1607 | LOGP(DGPRS, LOGL_DEBUG, "%s IMSI %s", |
| 1608 | sep, mi_buf); |
| 1609 | sep = ","; |
| 1610 | } |
Jacob Erlbeck | f95340d | 2014-08-11 15:07:37 +0200 | [diff] [blame] | 1611 | if (parse_ctx->invalidate_tlli) { |
| 1612 | LOGP(DGPRS, LOGL_DEBUG, "%s invalidate", sep); |
| 1613 | sep = ","; |
| 1614 | } |
| 1615 | |
Jacob Erlbeck | cae8b0c | 2014-08-07 16:42:51 +0200 | [diff] [blame] | 1616 | LOGP(DGPRS, LOGL_DEBUG, "\n"); |
| 1617 | } |
| 1618 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1619 | static uint32_t gbprox_make_bss_ptmsi(struct gbproxy_peer *peer, |
| 1620 | uint32_t sgsn_ptmsi) |
| 1621 | { |
Jacob Erlbeck | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 1622 | uint32_t bss_ptmsi; |
| 1623 | if (!peer->cfg->patch_ptmsi) { |
| 1624 | bss_ptmsi = sgsn_ptmsi; |
| 1625 | } else { |
| 1626 | do { |
| 1627 | bss_ptmsi = rand_r(&peer->cfg->bss_ptmsi_state); |
| 1628 | bss_ptmsi = bss_ptmsi | 0xC0000000; |
| 1629 | |
| 1630 | if (gbprox_find_tlli_by_ptmsi(peer, bss_ptmsi)) |
| 1631 | bss_ptmsi = GSM_RESERVED_TMSI; |
| 1632 | } while (bss_ptmsi == GSM_RESERVED_TMSI); |
| 1633 | } |
| 1634 | |
| 1635 | return bss_ptmsi; |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | static uint32_t gbprox_make_sgsn_tlli(struct gbproxy_peer *peer, |
Jacob Erlbeck | d52f2d1 | 2014-08-18 17:01:32 +0200 | [diff] [blame] | 1639 | struct gbproxy_tlli_info *tlli_info, |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1640 | uint32_t bss_tlli) |
| 1641 | { |
Jacob Erlbeck | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 1642 | uint32_t sgsn_tlli; |
| 1643 | if (!peer->cfg->patch_ptmsi) { |
| 1644 | sgsn_tlli = bss_tlli; |
| 1645 | } else if (tlli_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI) { |
| 1646 | sgsn_tlli = gprs_tmsi2tlli(tlli_info->sgsn_tlli.ptmsi, |
| 1647 | TLLI_FOREIGN); |
| 1648 | } else { |
| 1649 | do { |
| 1650 | /* create random TLLI, 0b01111xxx... */ |
| 1651 | sgsn_tlli = rand_r(&peer->cfg->sgsn_tlli_state); |
| 1652 | sgsn_tlli = (sgsn_tlli & 0x7fffffff) | 0x78000000; |
| 1653 | |
| 1654 | if (gbprox_find_tlli_by_sgsn_tlli(peer, sgsn_tlli)) |
| 1655 | sgsn_tlli = 0; |
| 1656 | } while (!sgsn_tlli); |
| 1657 | } |
| 1658 | return sgsn_tlli; |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | static struct gbproxy_tlli_info *gbprox_update_state_ul( |
| 1662 | struct gbproxy_peer *peer, |
| 1663 | time_t now, |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 1664 | struct gbproxy_parse_context *parse_ctx) |
Jacob Erlbeck | 2f2aa2e | 2014-08-05 18:28:51 +0200 | [diff] [blame] | 1665 | { |
Jacob Erlbeck | 9ac42ba | 2014-08-06 18:55:15 +0200 | [diff] [blame] | 1666 | struct gbproxy_tlli_info *tlli_info = NULL; |
| 1667 | |
Jacob Erlbeck | 9ac42ba | 2014-08-06 18:55:15 +0200 | [diff] [blame] | 1668 | if (parse_ctx->tlli_enc) |
| 1669 | tlli_info = gbprox_find_tlli(peer, parse_ctx->tlli); |
Jacob Erlbeck | 2f2aa2e | 2014-08-05 18:28:51 +0200 | [diff] [blame] | 1670 | |
Jacob Erlbeck | 2f2aa2e | 2014-08-05 18:28:51 +0200 | [diff] [blame] | 1671 | if (parse_ctx->g48_hdr) { |
| 1672 | switch (parse_ctx->g48_hdr->msg_type) { |
| 1673 | case GSM48_MT_GMM_ATTACH_REQ: |
| 1674 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]); |
| 1675 | break; |
| 1676 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1677 | default: |
| 1678 | break; |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | gbprox_log_parse_context(parse_ctx, "BSSGP"); |
| 1683 | |
| 1684 | if (parse_ctx->tlli_enc && parse_ctx->llc) { |
| 1685 | uint32_t sgsn_tlli; |
| 1686 | if (!tlli_info) { |
| 1687 | tlli_info = |
| 1688 | gbprox_register_tlli(peer, parse_ctx->tlli, |
| 1689 | parse_ctx->imsi, |
| 1690 | parse_ctx->imsi_len, now); |
| 1691 | /* Setup TLLIs */ |
Jacob Erlbeck | d52f2d1 | 2014-08-18 17:01:32 +0200 | [diff] [blame] | 1692 | sgsn_tlli = gbprox_make_sgsn_tlli(peer, tlli_info, |
| 1693 | parse_ctx->tlli); |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1694 | tlli_info->sgsn_tlli.current = sgsn_tlli; |
| 1695 | } else { |
Jacob Erlbeck | d52f2d1 | 2014-08-18 17:01:32 +0200 | [diff] [blame] | 1696 | sgsn_tlli = gbprox_map_tlli(parse_ctx->tlli, tlli_info, 0); |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1697 | if (!sgsn_tlli) |
Jacob Erlbeck | d52f2d1 | 2014-08-18 17:01:32 +0200 | [diff] [blame] | 1698 | sgsn_tlli = gbprox_make_sgsn_tlli(peer, tlli_info, |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1699 | parse_ctx->tlli); |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1700 | |
| 1701 | gbprox_validate_tlli(&tlli_info->tlli, |
| 1702 | parse_ctx->tlli, 0); |
| 1703 | gbprox_validate_tlli(&tlli_info->sgsn_tlli, |
| 1704 | sgsn_tlli, 0); |
| 1705 | gbprox_touch_tlli(peer, tlli_info, now); |
| 1706 | } |
| 1707 | } else if (tlli_info) { |
| 1708 | gbprox_touch_tlli(peer, tlli_info, now); |
| 1709 | } |
| 1710 | |
| 1711 | if (parse_ctx->imsi && tlli_info && tlli_info->mi_data_len == 0) { |
| 1712 | int enable_patching; |
| 1713 | gbprox_update_tlli_info(tlli_info, |
| 1714 | parse_ctx->imsi, parse_ctx->imsi_len); |
| 1715 | |
| 1716 | /* Check, whether the IMSI matches */ |
| 1717 | enable_patching = gbprox_check_imsi(peer, parse_ctx->imsi, |
| 1718 | parse_ctx->imsi_len); |
| 1719 | if (enable_patching >= 0) |
| 1720 | tlli_info->enable_patching = enable_patching; |
| 1721 | } |
| 1722 | |
| 1723 | return tlli_info; |
| 1724 | } |
| 1725 | |
| 1726 | static struct gbproxy_tlli_info *gbprox_update_state_dl( |
| 1727 | struct gbproxy_peer *peer, |
| 1728 | time_t now, |
| 1729 | struct gbproxy_parse_context *parse_ctx) |
| 1730 | { |
| 1731 | struct gbproxy_tlli_info *tlli_info = NULL; |
| 1732 | |
| 1733 | if (parse_ctx->tlli_enc) |
| 1734 | tlli_info = gbprox_find_tlli_by_sgsn_tlli(peer, parse_ctx->tlli); |
| 1735 | |
| 1736 | if (parse_ctx->g48_hdr) { |
| 1737 | switch (parse_ctx->g48_hdr->msg_type) { |
Jacob Erlbeck | 2f2aa2e | 2014-08-05 18:28:51 +0200 | [diff] [blame] | 1738 | case GSM48_MT_GMM_ATTACH_REJ: |
| 1739 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]); |
| 1740 | break; |
| 1741 | |
| 1742 | default: |
| 1743 | break; |
| 1744 | } |
| 1745 | } |
| 1746 | |
Jacob Erlbeck | cae8b0c | 2014-08-07 16:42:51 +0200 | [diff] [blame] | 1747 | gbprox_log_parse_context(parse_ctx, "BSSGP"); |
Jacob Erlbeck | 2f2aa2e | 2014-08-05 18:28:51 +0200 | [diff] [blame] | 1748 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1749 | if (parse_ctx->tlli_enc && parse_ctx->new_ptmsi_enc) { |
Jacob Erlbeck | fe7e670 | 2014-08-06 18:50:05 +0200 | [diff] [blame] | 1750 | /* A new PTMSI has been signaled in the message, |
| 1751 | * register new TLLI */ |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1752 | uint32_t new_sgsn_ptmsi; |
| 1753 | uint32_t new_sgsn_tlli; |
| 1754 | uint32_t new_bss_ptmsi; |
| 1755 | uint32_t new_bss_tlli = 0; |
Jacob Erlbeck | dc6dcdf | 2014-08-06 15:16:45 +0200 | [diff] [blame] | 1756 | if (!parse_mi_tmsi(parse_ctx->new_ptmsi_enc, GSM48_TMSI_LEN, |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1757 | &new_sgsn_ptmsi)) { |
Jacob Erlbeck | 2f2aa2e | 2014-08-05 18:28:51 +0200 | [diff] [blame] | 1758 | LOGP(DGPRS, LOGL_ERROR, |
| 1759 | "Failed to parse new TLLI/PTMSI (current is %08x)\n", |
| 1760 | parse_ctx->tlli); |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 1761 | return tlli_info; |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1762 | } |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1763 | new_sgsn_tlli = gprs_tmsi2tlli(new_sgsn_ptmsi, TLLI_LOCAL); |
| 1764 | new_bss_ptmsi = gbprox_make_bss_ptmsi(peer, new_sgsn_ptmsi); |
| 1765 | if (new_bss_ptmsi != GSM_RESERVED_TMSI) |
| 1766 | new_bss_tlli = gprs_tmsi2tlli(new_bss_ptmsi, TLLI_LOCAL); |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1767 | LOGP(DGPRS, LOGL_INFO, |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1768 | "Got new TLLI(PTMSI) %08x(%08x) from SGSN, using %08x(%08x)\n", |
| 1769 | new_sgsn_tlli, new_sgsn_ptmsi, new_bss_tlli, new_bss_ptmsi); |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 1770 | if (tlli_info) { |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1771 | gbprox_reassign_tlli(&tlli_info->sgsn_tlli, |
| 1772 | peer, new_sgsn_tlli); |
| 1773 | gbprox_reassign_tlli(&tlli_info->tlli, |
| 1774 | peer, new_bss_tlli); |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 1775 | gbprox_touch_tlli(peer, tlli_info, now); |
| 1776 | } else { |
Jacob Erlbeck | d52f2d1 | 2014-08-18 17:01:32 +0200 | [diff] [blame] | 1777 | tlli_info = gbprox_tlli_info_alloc(peer); |
| 1778 | LOGP(DGPRS, LOGL_INFO, |
| 1779 | "Adding TLLI %08x to list (SGSN, new P-TMSI)\n", |
| 1780 | new_sgsn_tlli); |
| 1781 | |
| 1782 | gbprox_attach_tlli_info(peer, now, tlli_info); |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1783 | /* Setup TLLIs */ |
| 1784 | tlli_info->sgsn_tlli.current = new_sgsn_tlli; |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 1785 | } |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1786 | /* Setup PTMSIs */ |
| 1787 | tlli_info->sgsn_tlli.ptmsi = new_sgsn_ptmsi; |
| 1788 | tlli_info->tlli.ptmsi = new_bss_ptmsi; |
| 1789 | } else if (parse_ctx->tlli_enc && parse_ctx->llc && !tlli_info) { |
| 1790 | /* Unknown SGSN TLLI */ |
Jacob Erlbeck | d52f2d1 | 2014-08-18 17:01:32 +0200 | [diff] [blame] | 1791 | tlli_info = gbprox_tlli_info_alloc(peer); |
| 1792 | LOGP(DGPRS, LOGL_INFO, "Adding TLLI %08x to list (SGSN)\n", |
| 1793 | parse_ctx->tlli); |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1794 | |
Jacob Erlbeck | d52f2d1 | 2014-08-18 17:01:32 +0200 | [diff] [blame] | 1795 | gbprox_attach_tlli_info(peer, now, tlli_info); |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1796 | /* Setup TLLIs */ |
Jacob Erlbeck | d52f2d1 | 2014-08-18 17:01:32 +0200 | [diff] [blame] | 1797 | tlli_info->sgsn_tlli.current = parse_ctx->tlli; |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1798 | if (peer->cfg->patch_ptmsi) { |
| 1799 | /* TODO: We don't know the local TLLI here, perhaps add |
| 1800 | * a workaround that derives a PTMSI from the SGSN TLLI |
| 1801 | * and use that to get the missing values. This may |
| 1802 | * only happen when the gbproxy has been restarted or a |
| 1803 | * tlli_info has been discarded due to age or queue |
| 1804 | * length. |
| 1805 | */ |
| 1806 | tlli_info->tlli.current = 0; |
Jacob Erlbeck | d52f2d1 | 2014-08-18 17:01:32 +0200 | [diff] [blame] | 1807 | } else { |
| 1808 | tlli_info->tlli.current = tlli_info->sgsn_tlli.current; |
Jacob Erlbeck | 3113287 | 2014-08-11 17:26:21 +0200 | [diff] [blame] | 1809 | } |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1810 | } else if (parse_ctx->tlli_enc && parse_ctx->llc && tlli_info) { |
Jacob Erlbeck | d52f2d1 | 2014-08-18 17:01:32 +0200 | [diff] [blame] | 1811 | uint32_t bss_tlli = gbprox_map_tlli(parse_ctx->tlli, |
| 1812 | tlli_info, 1); |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1813 | gbprox_validate_tlli(&tlli_info->sgsn_tlli, parse_ctx->tlli, 1); |
Jacob Erlbeck | d52f2d1 | 2014-08-18 17:01:32 +0200 | [diff] [blame] | 1814 | gbprox_validate_tlli(&tlli_info->tlli, bss_tlli, 1); |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 1815 | gbprox_touch_tlli(peer, tlli_info, now); |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 1816 | } else if (tlli_info) { |
| 1817 | gbprox_touch_tlli(peer, tlli_info, now); |
Jacob Erlbeck | 2f2aa2e | 2014-08-05 18:28:51 +0200 | [diff] [blame] | 1818 | } |
| 1819 | |
Jacob Erlbeck | 2e071fc | 2014-08-13 14:17:23 +0200 | [diff] [blame] | 1820 | if (parse_ctx->imsi && tlli_info && tlli_info->mi_data_len == 0) { |
| 1821 | int enable_patching; |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 1822 | gbprox_update_tlli_info(tlli_info, |
| 1823 | parse_ctx->imsi, parse_ctx->imsi_len); |
| 1824 | |
Jacob Erlbeck | 2e071fc | 2014-08-13 14:17:23 +0200 | [diff] [blame] | 1825 | /* Check, whether the IMSI matches */ |
| 1826 | enable_patching = gbprox_check_imsi(peer, parse_ctx->imsi, |
| 1827 | parse_ctx->imsi_len); |
| 1828 | if (enable_patching >= 0) |
| 1829 | tlli_info->enable_patching = enable_patching; |
| 1830 | } |
| 1831 | |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 1832 | return tlli_info; |
Jacob Erlbeck | 2f2aa2e | 2014-08-05 18:28:51 +0200 | [diff] [blame] | 1833 | } |
| 1834 | |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 1835 | static void gbprox_update_state_after(struct gbproxy_peer *peer, |
| 1836 | struct gbproxy_tlli_info *tlli_info, |
| 1837 | time_t now, |
Jacob Erlbeck | 2f2aa2e | 2014-08-05 18:28:51 +0200 | [diff] [blame] | 1838 | struct gbproxy_parse_context *parse_ctx) |
| 1839 | { |
| 1840 | if (parse_ctx->invalidate_tlli) |
Jacob Erlbeck | 3b66d85 | 2014-08-05 15:53:43 +0200 | [diff] [blame] | 1841 | gbprox_unregister_tlli(peer, parse_ctx->tlli); |
Jacob Erlbeck | 985b46e | 2014-08-07 17:23:00 +0200 | [diff] [blame] | 1842 | |
| 1843 | gbprox_remove_stale_tllis(peer, now); |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 1844 | } |
| 1845 | |
Jacob Erlbeck | 0f9273b | 2014-08-07 16:07:24 +0200 | [diff] [blame] | 1846 | static int gbprox_parse_bssgp(uint8_t *bssgp, size_t bssgp_len, |
| 1847 | struct gbproxy_parse_context *parse_ctx) |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1848 | { |
| 1849 | struct bssgp_normal_hdr *bgph; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 1850 | struct bssgp_ud_hdr *budh = NULL; |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1851 | struct tlv_parsed *tp = &parse_ctx->bssgp_tp; |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1852 | uint8_t pdu_type; |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1853 | uint8_t *data; |
| 1854 | size_t data_len; |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1855 | int rc; |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1856 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1857 | if (bssgp_len < sizeof(struct bssgp_normal_hdr)) |
| 1858 | return 0; |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1859 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1860 | bgph = (struct bssgp_normal_hdr *)bssgp; |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1861 | pdu_type = bgph->pdu_type; |
| 1862 | |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1863 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 1864 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1865 | if (bssgp_len < sizeof(struct bssgp_ud_hdr)) |
| 1866 | return 0; |
| 1867 | budh = (struct bssgp_ud_hdr *)bssgp; |
Jacob Erlbeck | 4303763 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 1868 | bgph = NULL; |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1869 | data = budh->data; |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1870 | data_len = bssgp_len - sizeof(*budh); |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1871 | } else { |
| 1872 | data = bgph->data; |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1873 | data_len = bssgp_len - sizeof(*bgph); |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 1874 | } |
| 1875 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1876 | if (bssgp_tlv_parse(tp, data, data_len) < 0) |
| 1877 | return 0; |
| 1878 | |
| 1879 | parse_ctx->pdu_type = pdu_type; |
| 1880 | parse_ctx->bud_hdr = budh; |
| 1881 | parse_ctx->bgp_hdr = bgph; |
| 1882 | parse_ctx->bssgp_data = data; |
| 1883 | parse_ctx->bssgp_data_len = data_len; |
| 1884 | |
| 1885 | if (budh) |
| 1886 | parse_ctx->tlli_enc = (uint8_t *)&budh->tlli; |
| 1887 | |
| 1888 | if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) |
| 1889 | parse_ctx->bssgp_raid_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA); |
| 1890 | |
| 1891 | if (TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) |
| 1892 | parse_ctx->bssgp_raid_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_CELL_ID); |
| 1893 | |
| 1894 | if (TLVP_PRESENT(tp, BSSGP_IE_IMSI)) { |
| 1895 | parse_ctx->imsi = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_IMSI); |
| 1896 | parse_ctx->imsi_len = TLVP_LEN(tp, BSSGP_IE_IMSI); |
| 1897 | } |
| 1898 | |
Jacob Erlbeck | d52f2d1 | 2014-08-18 17:01:32 +0200 | [diff] [blame] | 1899 | /* TODO: This is TLLI old, don't confuse with TLLI current, add |
| 1900 | * and use tlli_old_enc instead */ |
| 1901 | if (0 && TLVP_PRESENT(tp, BSSGP_IE_TLLI)) |
Jacob Erlbeck | dc6dcdf | 2014-08-06 15:16:45 +0200 | [diff] [blame] | 1902 | parse_ctx->tlli_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_TLLI); |
| 1903 | |
| 1904 | if (TLVP_PRESENT(tp, BSSGP_IE_TMSI) && pdu_type == BSSGP_PDUT_PAGING_PS) |
| 1905 | parse_ctx->ptmsi_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_TMSI); |
| 1906 | |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1907 | if (TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) { |
| 1908 | uint8_t *llc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_LLC_PDU); |
| 1909 | size_t llc_len = TLVP_LEN(tp, BSSGP_IE_LLC_PDU); |
| 1910 | |
| 1911 | rc = gbprox_parse_llc(llc, llc_len, parse_ctx); |
| 1912 | if (!rc) |
| 1913 | return 0; |
| 1914 | |
| 1915 | parse_ctx->llc = llc; |
| 1916 | parse_ctx->llc_len = llc_len; |
| 1917 | } |
| 1918 | |
| 1919 | if (parse_ctx->tlli_enc) { |
| 1920 | uint32_t tmp_tlli; |
| 1921 | memcpy(&tmp_tlli, parse_ctx->tlli_enc, sizeof(tmp_tlli)); |
| 1922 | parse_ctx->tlli = ntohl(tmp_tlli); |
| 1923 | } |
| 1924 | |
| 1925 | return 1; |
| 1926 | } |
| 1927 | |
| 1928 | /* patch BSSGP message to use core_mcc/mnc on the SGSN side */ |
Jacob Erlbeck | 0f9273b | 2014-08-07 16:07:24 +0200 | [diff] [blame] | 1929 | static void gbprox_patch_bssgp(struct msgb *msg, uint8_t *bssgp, size_t bssgp_len, |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 1930 | struct gbproxy_peer *peer, |
| 1931 | struct gbproxy_tlli_info *tlli_info, int *len_change, |
Jacob Erlbeck | 0f9273b | 2014-08-07 16:07:24 +0200 | [diff] [blame] | 1932 | struct gbproxy_parse_context *parse_ctx) |
| 1933 | __attribute__((nonnull)); |
| 1934 | static void gbprox_patch_bssgp(struct msgb *msg, uint8_t *bssgp, size_t bssgp_len, |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 1935 | struct gbproxy_peer *peer, |
| 1936 | struct gbproxy_tlli_info *tlli_info, int *len_change, |
Jacob Erlbeck | 0f9273b | 2014-08-07 16:07:24 +0200 | [diff] [blame] | 1937 | struct gbproxy_parse_context *parse_ctx) |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1938 | { |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 1939 | const char *err_info = NULL; |
| 1940 | int err_ctr = -1; |
Jacob Erlbeck | 0f9273b | 2014-08-07 16:07:24 +0200 | [diff] [blame] | 1941 | |
| 1942 | if (!patching_is_enabled(peer, GBPROX_PATCH_BSSGP)) |
| 1943 | return; |
| 1944 | |
| 1945 | if (parse_ctx->bssgp_raid_enc) |
| 1946 | gbprox_patch_raid(parse_ctx->bssgp_raid_enc, peer, |
| 1947 | parse_ctx->to_bss, "BSSGP"); |
| 1948 | |
| 1949 | if (!patching_is_enabled(peer, GBPROX_PATCH_LLC_ATTACH_REQ)) |
| 1950 | return; |
| 1951 | |
| 1952 | if (parse_ctx->need_decryption && |
| 1953 | patching_is_required(peer, GBPROX_PATCH_LLC_ATTACH)) { |
| 1954 | /* Patching LLC messages has been requested |
| 1955 | * explicitly, but the message (including the |
| 1956 | * type) is encrypted, so we possibly fail to |
| 1957 | * patch the LLC part of the message. */ |
| 1958 | err_ctr = GBPROX_PEER_CTR_PATCH_CRYPT_ERR; |
| 1959 | err_info = "GMM message is encrypted"; |
| 1960 | goto patch_error; |
| 1961 | } |
| 1962 | |
Jacob Erlbeck | d65d92d | 2014-08-18 17:23:01 +0200 | [diff] [blame] | 1963 | if (parse_ctx->tlli_enc && tlli_info) { |
| 1964 | uint32_t tlli = gbprox_map_tlli(parse_ctx->tlli, |
| 1965 | tlli_info, parse_ctx->to_bss); |
| 1966 | |
| 1967 | if (tlli) { |
| 1968 | gbprox_patch_tlli(parse_ctx->tlli_enc, peer, tlli, |
| 1969 | parse_ctx->to_bss, "TLLI"); |
| 1970 | parse_ctx->tlli = tlli; |
| 1971 | } else if (parse_ctx->to_bss) { |
| 1972 | /* Happens with unknown (not cached) TLLI coming from |
| 1973 | * the SGSN */ |
| 1974 | /* TODO: What shall be done with the message in this case? */ |
| 1975 | err_ctr = GBPROX_PEER_CTR_TLLI_UNKNOWN; |
| 1976 | err_info = "TLLI sent by the SGSN is unknown"; |
| 1977 | goto patch_error; |
| 1978 | } else { |
| 1979 | /* Internal error */ |
| 1980 | err_ctr = GBPROX_PEER_CTR_PATCH_ERR; |
| 1981 | err_info = "Replacement TLLI is 0"; |
| 1982 | goto patch_error; |
| 1983 | } |
| 1984 | } |
| 1985 | |
Jacob Erlbeck | 0f9273b | 2014-08-07 16:07:24 +0200 | [diff] [blame] | 1986 | if (parse_ctx->llc) { |
| 1987 | uint8_t *llc = parse_ctx->llc; |
| 1988 | size_t llc_len = parse_ctx->llc_len; |
| 1989 | int llc_len_change = 0; |
| 1990 | |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 1991 | gbprox_patch_llc(msg, llc, llc_len, peer, tlli_info, |
| 1992 | &llc_len_change, parse_ctx); |
Jacob Erlbeck | 0f9273b | 2014-08-07 16:07:24 +0200 | [diff] [blame] | 1993 | /* Note that the APN might have been resized here, but no |
| 1994 | * pointer int the parse_ctx will refer to an adress after the |
| 1995 | * APN. So it's possible to patch first and do the TLLI |
| 1996 | * handling afterwards. */ |
| 1997 | |
| 1998 | if (llc_len_change) { |
| 1999 | llc_len += llc_len_change; |
| 2000 | |
| 2001 | /* Fix LLC IE len */ |
| 2002 | /* TODO: This is a kludge, but the a pointer to the |
| 2003 | * start of the IE is not available here */ |
| 2004 | if (llc[-2] == BSSGP_IE_LLC_PDU && llc[-1] & 0x80) { |
| 2005 | /* most probably a one byte length */ |
| 2006 | if (llc_len > 127) { |
| 2007 | err_info = "Cannot increase size"; |
| 2008 | err_ctr = GBPROX_PEER_CTR_PATCH_ERR; |
| 2009 | goto patch_error; |
| 2010 | } |
| 2011 | llc[-1] = llc_len | 0x80; |
| 2012 | } else { |
| 2013 | llc[-2] = (llc_len >> 8) & 0x7f; |
| 2014 | llc[-1] = llc_len & 0xff; |
| 2015 | } |
| 2016 | *len_change += llc_len_change; |
| 2017 | } |
| 2018 | /* Note that the tp struct might contain invalid pointers here |
| 2019 | * if the LLC field has changed its size */ |
| 2020 | parse_ctx->llc_len = llc_len; |
| 2021 | } |
| 2022 | return; |
| 2023 | |
| 2024 | patch_error: |
| 2025 | OSMO_ASSERT(err_ctr >= 0); |
| 2026 | rate_ctr_inc(&peer->ctrg->ctr[err_ctr]); |
| 2027 | LOGP(DGPRS, LOGL_ERROR, |
Jacob Erlbeck | 4b5070e | 2014-08-18 17:26:04 +0200 | [diff] [blame] | 2028 | "NSEI=%u(%s) failed to patch BSSGP message as requested: %s.\n", |
| 2029 | msgb_nsei(msg), parse_ctx->to_bss ? "SGSN" : "BSS", |
| 2030 | err_info); |
Jacob Erlbeck | 0f9273b | 2014-08-07 16:07:24 +0200 | [diff] [blame] | 2031 | } |
| 2032 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 2033 | /* patch BSSGP message */ |
| 2034 | static void gbprox_process_bssgp_ul(struct gbproxy_config *cfg, |
| 2035 | struct msgb *msg, |
| 2036 | struct gbproxy_peer *peer) |
Jacob Erlbeck | 0f9273b | 2014-08-07 16:07:24 +0200 | [diff] [blame] | 2037 | { |
| 2038 | struct gbproxy_parse_context parse_ctx = {0}; |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 2039 | int rc; |
Jacob Erlbeck | 0f9273b | 2014-08-07 16:07:24 +0200 | [diff] [blame] | 2040 | int len_change = 0; |
Jacob Erlbeck | c404c08 | 2014-08-08 08:37:37 +0200 | [diff] [blame] | 2041 | time_t now; |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 2042 | struct gbproxy_tlli_info *tlli_info = NULL; |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 2043 | |
| 2044 | if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn) |
| 2045 | return; |
| 2046 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 2047 | parse_ctx.to_bss = 0; |
| 2048 | |
| 2049 | /* Parse BSSGP/LLC */ |
| 2050 | rc = gbprox_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg), |
| 2051 | &parse_ctx); |
| 2052 | |
| 2053 | if (!rc) { |
| 2054 | if (!parse_ctx.need_decryption) { |
| 2055 | LOGP(DGPRS, LOGL_ERROR, |
Jacob Erlbeck | 4b5070e | 2014-08-18 17:26:04 +0200 | [diff] [blame] | 2056 | "NSEI=%u(BSS) patching: " |
| 2057 | "failed to parse BSSGP/GMM message\n", |
| 2058 | msgb_nsei(msg)); |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 2059 | return; |
| 2060 | } |
| 2061 | } |
| 2062 | |
| 2063 | /* Get peer */ |
| 2064 | if (!peer && msgb_bvci(msg) >= 2) |
| 2065 | peer = peer_by_bvci(cfg, msgb_bvci(msg)); |
| 2066 | |
| 2067 | if (!peer) |
| 2068 | peer = gbprox_peer_by_nsei(cfg, msgb_nsei(msg)); |
| 2069 | |
| 2070 | if (!peer) |
| 2071 | peer = peer_by_bssgp_tlv(cfg, &parse_ctx.bssgp_tp); |
| 2072 | |
| 2073 | if (!peer) { |
| 2074 | LOGP(DLLC, LOGL_INFO, |
| 2075 | "NSEI=%d(BSS) patching: didn't find peer for message, " |
| 2076 | "PDU %d\n", |
| 2077 | msgb_nsei(msg), parse_ctx.pdu_type); |
| 2078 | /* Increment counter */ |
| 2079 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]); |
| 2080 | return; |
| 2081 | } |
| 2082 | |
| 2083 | now = time(NULL); |
| 2084 | |
Jacob Erlbeck | 2cb9e3f | 2014-08-18 15:41:41 +0200 | [diff] [blame] | 2085 | if (parse_ctx.bssgp_raid_enc && parse_ctx.old_raid_enc && |
| 2086 | memcmp(parse_ctx.bssgp_raid_enc, parse_ctx.old_raid_enc, 6) == 0) |
| 2087 | parse_ctx.old_raid_matches = 1; |
| 2088 | |
| 2089 | gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer, |
| 2090 | parse_ctx.llc_msg_name); |
| 2091 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 2092 | tlli_info = gbprox_update_state_ul(peer, now, &parse_ctx); |
| 2093 | |
| 2094 | gbprox_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg), |
| 2095 | peer, tlli_info, &len_change, &parse_ctx); |
| 2096 | |
| 2097 | gbprox_update_state_after(peer, tlli_info, now, &parse_ctx); |
| 2098 | |
| 2099 | return; |
| 2100 | } |
| 2101 | |
| 2102 | /* patch BSSGP message to use core_mcc/mnc on the SGSN side */ |
| 2103 | static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg, |
| 2104 | struct msgb *msg, |
| 2105 | struct gbproxy_peer *peer) |
| 2106 | { |
| 2107 | struct gbproxy_parse_context parse_ctx = {0}; |
| 2108 | int rc; |
| 2109 | int len_change = 0; |
| 2110 | time_t now; |
| 2111 | struct gbproxy_tlli_info *tlli_info = NULL; |
| 2112 | |
| 2113 | parse_ctx.to_bss = 1; |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 2114 | |
Jacob Erlbeck | 0f9273b | 2014-08-07 16:07:24 +0200 | [diff] [blame] | 2115 | rc = gbprox_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg), |
| 2116 | &parse_ctx); |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 2117 | |
| 2118 | if (!rc) { |
| 2119 | if (!parse_ctx.need_decryption) { |
| 2120 | LOGP(DGPRS, LOGL_ERROR, |
Jacob Erlbeck | 4b5070e | 2014-08-18 17:26:04 +0200 | [diff] [blame] | 2121 | "NSEI=%u(SGSN) patching: " |
| 2122 | "failed to parse BSSGP/GMM message\n", |
| 2123 | msgb_nsei(msg)); |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 2124 | return; |
| 2125 | } |
| 2126 | } |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 2127 | |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 2128 | if (!peer && msgb_bvci(msg) >= 2) |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2129 | peer = peer_by_bvci(cfg, msgb_bvci(msg)); |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 2130 | |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 2131 | if (!peer) |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 2132 | peer = peer_by_bssgp_tlv(cfg, &parse_ctx.bssgp_tp); |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 2133 | |
| 2134 | if (!peer) { |
| 2135 | LOGP(DLLC, LOGL_INFO, |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 2136 | "NSEI=%d(SGSN) patching: didn't find peer for message, " |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 2137 | "PDU %d\n", |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 2138 | msgb_nsei(msg), parse_ctx.pdu_type); |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 2139 | /* Increment counter */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2140 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]); |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 2141 | return; |
| 2142 | } |
| 2143 | |
Jacob Erlbeck | c404c08 | 2014-08-08 08:37:37 +0200 | [diff] [blame] | 2144 | now = time(NULL); |
| 2145 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 2146 | tlli_info = gbprox_update_state_dl(peer, now, &parse_ctx); |
Jacob Erlbeck | 0727f9a | 2014-05-28 10:59:10 +0200 | [diff] [blame] | 2147 | |
Jacob Erlbeck | 0f9273b | 2014-08-07 16:07:24 +0200 | [diff] [blame] | 2148 | gbprox_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg), |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 2149 | peer, tlli_info, &len_change, &parse_ctx); |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 2150 | |
Jacob Erlbeck | ccc5970 | 2014-08-07 20:18:47 +0200 | [diff] [blame] | 2151 | gbprox_update_state_after(peer, tlli_info, now, &parse_ctx); |
Jacob Erlbeck | 4dc663f | 2014-08-06 12:41:31 +0200 | [diff] [blame] | 2152 | |
Jacob Erlbeck | f14bb91 | 2014-06-27 19:56:05 +0200 | [diff] [blame] | 2153 | return; |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 2154 | } |
| 2155 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2156 | /* feed a message down the NS-VC associated with the specified peer */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2157 | static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg, |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 2158 | struct gbproxy_peer *peer, uint16_t ns_bvci) |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 2159 | { |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 2160 | /* create a copy of the message so the old one can |
| 2161 | * be free()d safely when we return from gbprox_rcvmsg() */ |
Holger Hans Peter Freyther | 8ac3a72 | 2014-08-04 11:52:52 +0200 | [diff] [blame] | 2162 | struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2sgsn"); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2163 | int rc; |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 2164 | |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 2165 | gbprox_process_bssgp_ul(cfg, msg, peer); |
Jacob Erlbeck | 90a1fd1 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 2166 | |
Harald Welte | b8da061 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 2167 | DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n", |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2168 | msgb_nsei(msg), ns_bvci, cfg->nsip_sgsn_nsei); |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 2169 | |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 2170 | msgb_bvci(msg) = ns_bvci; |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2171 | msgb_nsei(msg) = cfg->nsip_sgsn_nsei; |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 2172 | |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 2173 | strip_ns_hdr(msg); |
| 2174 | |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2175 | rc = gprs_ns_sendmsg(bssgp_nsi, msg); |
| 2176 | if (rc < 0) |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2177 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2178 | |
| 2179 | return rc; |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 2180 | } |
| 2181 | |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 2182 | /* feed a message down the NS-VC associated with the specified peer */ |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 2183 | static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer, |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2184 | uint16_t ns_bvci) |
| 2185 | { |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 2186 | /* create a copy of the message so the old one can |
| 2187 | * be free()d safely when we return from gbprox_rcvmsg() */ |
Holger Hans Peter Freyther | 8ac3a72 | 2014-08-04 11:52:52 +0200 | [diff] [blame] | 2188 | struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2peer"); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2189 | int rc; |
Harald Welte | 3fb22d6 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 2190 | |
Harald Welte | 0cc900d | 2010-05-13 10:34:56 +0200 | [diff] [blame] | 2191 | DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2192 | msgb_nsei(msg), ns_bvci, peer->nsei); |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 2193 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2194 | msgb_bvci(msg) = ns_bvci; |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2195 | msgb_nsei(msg) = peer->nsei; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2196 | |
Harald Welte | 0cc900d | 2010-05-13 10:34:56 +0200 | [diff] [blame] | 2197 | /* Strip the old NS header, it will be replaced with a new one */ |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 2198 | strip_ns_hdr(msg); |
| 2199 | |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2200 | rc = gprs_ns_sendmsg(bssgp_nsi, msg); |
| 2201 | if (rc < 0) |
| 2202 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]); |
| 2203 | |
| 2204 | return rc; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2205 | } |
| 2206 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2207 | static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type) |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 2208 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 2209 | struct gbproxy_peer *peer; |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 2210 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2211 | peer = peer_by_bvci(cfg, ptp_bvci); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 2212 | if (!peer) { |
| 2213 | LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n", |
| 2214 | ptp_bvci); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2215 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 2216 | return -ENOENT; |
| 2217 | } |
| 2218 | |
| 2219 | switch (pdu_type) { |
| 2220 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 2221 | peer->blocked = 1; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2222 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 2223 | break; |
| 2224 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 2225 | peer->blocked = 0; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2226 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 2227 | break; |
| 2228 | default: |
| 2229 | break; |
| 2230 | } |
| 2231 | return 0; |
| 2232 | } |
| 2233 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2234 | /* Send a message to a peer identified by ptp_bvci but using ns_bvci |
| 2235 | * in the NS hdr */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2236 | static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci, |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2237 | uint16_t ns_bvci) |
| 2238 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 2239 | struct gbproxy_peer *peer; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2240 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2241 | peer = peer_by_bvci(cfg, ptp_bvci); |
Harald Welte | 0358aae | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 2242 | if (!peer) { |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 2243 | LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n", |
Harald Welte | 0358aae | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 2244 | ptp_bvci); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2245 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2246 | return -ENOENT; |
Harald Welte | 0358aae | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 2247 | } |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2248 | |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 2249 | return gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2250 | } |
| 2251 | |
Harald Welte | a62e9fe | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 2252 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 2253 | { |
| 2254 | return 0; |
| 2255 | } |
| 2256 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2257 | /* Receive an incoming signalling message from a BSS-side NS-VC */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2258 | static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg, |
| 2259 | struct msgb *msg, uint16_t nsei, |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2260 | uint16_t ns_bvci) |
| 2261 | { |
Harald Welte | 2ac2e91 | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 2262 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2263 | struct tlv_parsed tp; |
| 2264 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | 2ac2e91 | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 2265 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 2266 | struct gbproxy_peer *from_peer = NULL; |
Harald Welte | e49c829 | 2010-05-01 12:10:57 +0200 | [diff] [blame] | 2267 | struct gprs_ra_id raid; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2268 | |
Harald Welte | 4127072 | 2011-02-06 17:13:12 +0100 | [diff] [blame] | 2269 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 2270 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2271 | nsei, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2272 | return -EINVAL; |
| 2273 | } |
| 2274 | |
| 2275 | /* we actually should never see those two for BVCI == 0, but double-check |
| 2276 | * just to make sure */ |
| 2277 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 2278 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 2279 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2280 | "signalling\n", nsei); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2281 | return -EINVAL; |
| 2282 | } |
| 2283 | |
| 2284 | bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 2285 | |
| 2286 | switch (pdu_type) { |
| 2287 | case BSSGP_PDUT_SUSPEND: |
| 2288 | case BSSGP_PDUT_RESUME: |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 2289 | /* We implement RAI snooping during SUSPEND/RESUME, since it |
| 2290 | * establishes a relationsip between BVCI/peer and the routeing |
| 2291 | * area identification. The snooped information is then used |
| 2292 | * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct |
| 2293 | * BSSGP */ |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2294 | if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 2295 | goto err_mand_ie; |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2296 | from_peer = gbprox_peer_by_nsei(cfg, nsei); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2297 | if (!from_peer) |
| 2298 | goto err_no_peer; |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 2299 | memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA), |
| 2300 | sizeof(from_peer->ra)); |
Harald Welte | f2fdefe | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 2301 | gsm48_parse_ra(&raid, from_peer->ra); |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 2302 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME " |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 2303 | "RAI snooping: RAI %u-%u-%u-%u behind BVCI=%u\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2304 | nsei, raid.mcc, raid.mnc, raid.lac, |
| 2305 | raid.rac , from_peer->bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2306 | /* FIXME: This only supports one BSS per RA */ |
| 2307 | break; |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 2308 | case BSSGP_PDUT_BVC_RESET: |
| 2309 | /* If we receive a BVC reset on the signalling endpoint, we |
| 2310 | * don't want the SGSN to reset, as the signalling endpoint |
| 2311 | * is common for all point-to-point BVCs (and thus all BTS) */ |
| 2312 | if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 2313 | uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | be6e25a | 2010-05-12 00:20:41 +0200 | [diff] [blame] | 2314 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2315 | nsei, bvci); |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 2316 | if (bvci == 0) { |
| 2317 | /* FIXME: only do this if SGSN is alive! */ |
Harald Welte | b8da061 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 2318 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2319 | "BVC RESET ACK of BVCI=0\n", nsei); |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 2320 | return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK, |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2321 | nsei, 0, ns_bvci); |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 2322 | } |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2323 | from_peer = peer_by_bvci(cfg, bvci); |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 2324 | if (!from_peer) { |
Harald Welte | 0358aae | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 2325 | /* if a PTP-BVC is reset, and we don't know that |
| 2326 | * PTP-BVCI yet, we should allocate a new peer */ |
| 2327 | LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2328 | "BVCI=%u via NSEI=%u\n", bvci, nsei); |
Holger Hans Peter Freyther | 60fa5b9 | 2014-08-04 17:10:08 +0200 | [diff] [blame] | 2329 | from_peer = gbproxy_peer_alloc(cfg, bvci); |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2330 | from_peer->nsei = nsei; |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 2331 | } |
Jacob Erlbeck | d0a9b50 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 2332 | |
| 2333 | if (!check_peer_nsei(from_peer, nsei)) |
| 2334 | from_peer->nsei = nsei; |
| 2335 | |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 2336 | if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) { |
| 2337 | struct gprs_ra_id raid; |
| 2338 | /* We have a Cell Identifier present in this |
| 2339 | * PDU, this means we can extend our local |
| 2340 | * state information about this particular cell |
| 2341 | * */ |
| 2342 | memcpy(from_peer->ra, |
| 2343 | TLVP_VAL(&tp, BSSGP_IE_CELL_ID), |
| 2344 | sizeof(from_peer->ra)); |
| 2345 | gsm48_parse_ra(&raid, from_peer->ra); |
| 2346 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2347 | "Cell ID %u-%u-%u-%u\n", nsei, |
Harald Welte | 8ba9d2d | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 2348 | bvci, raid.mcc, raid.mnc, raid.lac, |
| 2349 | raid.rac); |
| 2350 | } |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 2351 | } |
| 2352 | break; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2353 | } |
| 2354 | |
Harald Welte | b8da061 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 2355 | /* Normally, we can simply pass on all signalling messages from BSS to |
| 2356 | * SGSN */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2357 | return gbprox_relay2sgsn(cfg, msg, from_peer, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2358 | err_no_peer: |
Jacob Erlbeck | 700b873 | 2013-10-18 13:04:48 +0200 | [diff] [blame] | 2359 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2360 | nsei); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2361 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2362 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2363 | err_mand_ie: |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2364 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2365 | nsei); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2366 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2367 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2368 | } |
| 2369 | |
| 2370 | /* Receive paging request from SGSN, we need to relay to proper BSS */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2371 | static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp, |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2372 | uint32_t nsei, uint16_t ns_bvci) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2373 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 2374 | struct gbproxy_peer *peer = NULL; |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2375 | int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2376 | |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 2377 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2378 | nsei); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2379 | if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 2380 | uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 2381 | LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n", |
| 2382 | bvci); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2383 | errctr = GBPROX_GLOB_CTR_OTHER_ERR; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2384 | } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) { |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2385 | peer = peer_by_rai(cfg, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA)); |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 2386 | LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n", |
Holger Hans Peter Freyther | 98b82c1 | 2010-06-08 16:30:24 +0800 | [diff] [blame] | 2387 | peer ? peer->bvci : -1); |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 2388 | errctr = GBPROX_GLOB_CTR_INV_RAI; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2389 | } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) { |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2390 | peer = peer_by_lai(cfg, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA)); |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 2391 | LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n", |
Holger Hans Peter Freyther | 98b82c1 | 2010-06-08 16:30:24 +0800 | [diff] [blame] | 2392 | peer ? peer->bvci : -1); |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 2393 | errctr = GBPROX_GLOB_CTR_INV_LAI; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2394 | } else |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 2395 | LOGPC(DGPRS, LOGL_INFO, "\n"); |
| 2396 | |
| 2397 | if (!peer) { |
| 2398 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2399 | "unable to route, missing IE\n", nsei); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2400 | rate_ctr_inc(&cfg->ctrg->ctr[errctr]); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2401 | return -EINVAL; |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 2402 | } |
| 2403 | return gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2404 | } |
| 2405 | |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2406 | /* Receive an incoming BVC-RESET message from the SGSN */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2407 | static int rx_reset_from_sgsn(struct gbproxy_config *cfg, |
| 2408 | struct msgb *msg, struct tlv_parsed *tp, |
| 2409 | uint32_t nsei, uint16_t ns_bvci) |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2410 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 2411 | struct gbproxy_peer *peer; |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2412 | uint16_t ptp_bvci; |
| 2413 | |
| 2414 | if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2415 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2416 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2417 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, |
| 2418 | NULL, msg); |
| 2419 | } |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 2420 | ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2421 | |
| 2422 | if (ptp_bvci >= 2) { |
| 2423 | /* A reset for a PTP BVC was received, forward it to its |
| 2424 | * respective peer */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2425 | peer = peer_by_bvci(cfg, ptp_bvci); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2426 | if (!peer) { |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 2427 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2428 | nsei, ptp_bvci); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2429 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2430 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2431 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 2432 | NULL, msg); |
| 2433 | } |
| 2434 | return gbprox_relay2peer(msg, peer, ns_bvci); |
| 2435 | } |
| 2436 | |
| 2437 | /* A reset for the Signalling entity has been received |
| 2438 | * from the SGSN. As the signalling BVCI is shared |
| 2439 | * among all the BSS's that we multiplex, it needs to |
| 2440 | * be relayed */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2441 | llist_for_each_entry(peer, &cfg->bts_peers, list) |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2442 | gbprox_relay2peer(msg, peer, ns_bvci); |
| 2443 | |
| 2444 | return 0; |
| 2445 | } |
| 2446 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2447 | /* Receive an incoming signalling message from the SGSN-side NS-VC */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2448 | static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg, |
| 2449 | struct msgb *msg, uint32_t nsei, |
| 2450 | uint16_t ns_bvci) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2451 | { |
Harald Welte | 2ac2e91 | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 2452 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2453 | struct tlv_parsed tp; |
| 2454 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | 2ac2e91 | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 2455 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 2456 | struct gbproxy_peer *peer; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2457 | uint16_t bvci; |
| 2458 | int rc = 0; |
| 2459 | |
Harald Welte | 4127072 | 2011-02-06 17:13:12 +0100 | [diff] [blame] | 2460 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | 93048cf | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 2461 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2462 | "signalling\n", nsei, ns_bvci); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2463 | /* FIXME: Send proper error message */ |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2464 | return -EINVAL; |
| 2465 | } |
| 2466 | |
| 2467 | /* we actually should never see those two for BVCI == 0, but double-check |
| 2468 | * just to make sure */ |
| 2469 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 2470 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 2471 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2472 | "signalling\n", nsei); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2473 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 2477 | |
| 2478 | switch (pdu_type) { |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2479 | case BSSGP_PDUT_BVC_RESET: |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2480 | rc = rx_reset_from_sgsn(cfg, msg, &tp, nsei, ns_bvci); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2481 | break; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2482 | case BSSGP_PDUT_FLUSH_LL: |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2483 | case BSSGP_PDUT_BVC_RESET_ACK: |
| 2484 | /* simple case: BVCI IE is mandatory */ |
| 2485 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 2486 | goto err_mand_ie; |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 2487 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2488 | rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2489 | break; |
| 2490 | case BSSGP_PDUT_PAGING_PS: |
| 2491 | case BSSGP_PDUT_PAGING_CS: |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 2492 | /* process the paging request (LAI/RAI lookup) */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2493 | rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2494 | break; |
| 2495 | case BSSGP_PDUT_STATUS: |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2496 | /* Some exception has occurred */ |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 2497 | LOGP(DGPRS, LOGL_NOTICE, |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2498 | "NSEI=%u(SGSN) BSSGP STATUS ", nsei); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2499 | if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) { |
| 2500 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
| 2501 | goto err_mand_ie; |
| 2502 | } |
| 2503 | LOGPC(DGPRS, LOGL_NOTICE, |
| 2504 | "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE), |
| 2505 | bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE))); |
| 2506 | if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 2507 | uint16_t bvci = tlvp_val16_unal(&tp, BSSGP_IE_BVCI); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2508 | LOGPC(DGPRS, LOGL_NOTICE, |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 2509 | "BVCI=%u\n", ntohs(bvci)); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2510 | } else |
| 2511 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2512 | break; |
| 2513 | /* those only exist in the SGSN -> BSS direction */ |
| 2514 | case BSSGP_PDUT_SUSPEND_ACK: |
| 2515 | case BSSGP_PDUT_SUSPEND_NACK: |
| 2516 | case BSSGP_PDUT_RESUME_ACK: |
| 2517 | case BSSGP_PDUT_RESUME_NACK: |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 2518 | /* RAI IE is mandatory */ |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2519 | if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 2520 | goto err_mand_ie; |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2521 | peer = peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA)); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2522 | if (!peer) |
| 2523 | goto err_no_peer; |
Harald Welte | 3ad3243 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 2524 | rc = gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2525 | break; |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 2526 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 2527 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 2528 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 2529 | goto err_mand_ie; |
Holger Hans Peter Freyther | 25eeaab | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 2530 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 2531 | if (bvci == 0) { |
| 2532 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP " |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2533 | "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei, |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 2534 | pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":""); |
| 2535 | /* should we send STATUS ? */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2536 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2537 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 2538 | } else { |
| 2539 | /* Mark BVC as (un)blocked */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2540 | block_unblock_peer(cfg, bvci, pdu_type); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 2541 | } |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2542 | rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 2543 | break; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2544 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
Harald Welte | 6aa24d1 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 2545 | LOGP(DGPRS, LOGL_ERROR, |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2546 | "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2547 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2548 | ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2549 | rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2550 | break; |
| 2551 | default: |
Harald Welte | be6e25a | 2010-05-12 00:20:41 +0200 | [diff] [blame] | 2552 | LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n", |
| 2553 | pdu_type); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2554 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2555 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2556 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2557 | break; |
| 2558 | } |
| 2559 | |
| 2560 | return rc; |
| 2561 | err_mand_ie: |
Harald Welte | 0358aae | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 2562 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2563 | nsei); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2564 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2565 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2566 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2567 | err_no_peer: |
Jacob Erlbeck | 9d7268a | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 2568 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2569 | nsei); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2570 | rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]); |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 2571 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2572 | } |
| 2573 | |
| 2574 | /* Main input function for Gb proxy */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2575 | int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei, |
| 2576 | uint16_t ns_bvci, uint16_t nsvci) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2577 | { |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 2578 | int rc; |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 2579 | struct gbproxy_peer *peer; |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2580 | int remote_end_is_sgsn = nsei == cfg->nsip_sgsn_nsei; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2581 | |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 2582 | if (remote_end_is_sgsn) |
Jacob Erlbeck | 383c841 | 2014-08-12 16:30:30 +0200 | [diff] [blame] | 2583 | gbprox_process_bssgp_dl(cfg, msg, NULL); |
Jacob Erlbeck | 2504bc4 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 2584 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2585 | /* Only BVCI=0 messages need special treatment */ |
| 2586 | if (ns_bvci == 0 || ns_bvci == 1) { |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2587 | if (remote_end_is_sgsn) |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2588 | rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2589 | else |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2590 | rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci); |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 2591 | } else { |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2592 | peer = peer_by_bvci(cfg, ns_bvci); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2593 | |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 2594 | /* All other BVCI are PTP and thus can be simply forwarded */ |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2595 | if (!remote_end_is_sgsn) { |
| 2596 | if (peer) |
| 2597 | check_peer_nsei(peer, nsei); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2598 | return gbprox_relay2sgsn(cfg, msg, peer, ns_bvci); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2599 | } |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2600 | |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 2601 | /* else: SGSN -> BSS direction */ |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 2602 | if (!peer) { |
Jacob Erlbeck | 901b9a4 | 2013-10-18 22:12:17 +0200 | [diff] [blame] | 2603 | LOGP(DGPRS, LOGL_INFO, "Didn't find peer for " |
| 2604 | "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n", |
| 2605 | ns_bvci, nsvci, nsei); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2606 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | 901b9a4 | 2013-10-18 22:12:17 +0200 | [diff] [blame] | 2607 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
| 2608 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 2609 | &ns_bvci, msg); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 2610 | } |
| 2611 | if (peer->blocked) { |
| 2612 | LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for " |
| 2613 | "blocked BVCI=%u via NSVC=%u/NSEI=%u\n", |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2614 | ns_bvci, nsvci, nsei); |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2615 | rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]); |
Harald Welte | 51b7d40 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 2616 | return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, NULL, msg); |
| 2617 | } |
| 2618 | rc = gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2619 | } |
| 2620 | |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 2621 | return rc; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2622 | } |
Harald Welte | b34e2c1 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 2623 | |
Harald Welte | 9ee404a | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 2624 | int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi) |
| 2625 | { |
| 2626 | struct gprs_nsvc *nsvc; |
| 2627 | |
| 2628 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 2629 | if (!nsvc->persistent) |
| 2630 | continue; |
| 2631 | gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
| 2632 | } |
| 2633 | return 0; |
| 2634 | } |
| 2635 | |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 2636 | /* Signal handler for signals from NS layer */ |
| 2637 | int gbprox_signal(unsigned int subsys, unsigned int signal, |
| 2638 | void *handler_data, void *signal_data) |
| 2639 | { |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2640 | struct gbproxy_config *cfg = handler_data; |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 2641 | struct ns_signal_data *nssd = signal_data; |
| 2642 | struct gprs_nsvc *nsvc = nssd->nsvc; |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 2643 | struct gbproxy_peer *peer; |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 2644 | |
Harald Welte | 7ed98e3 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 2645 | if (subsys != SS_L_NS) |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 2646 | return 0; |
| 2647 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2648 | if (signal == S_NS_RESET && nsvc->nsei == cfg->nsip_sgsn_nsei) { |
Harald Welte | 15cbb0e | 2010-05-11 18:29:44 +0200 | [diff] [blame] | 2649 | /* We have received a NS-RESET from the NSEI and NSVC |
| 2650 | * of the SGSN. This might happen with SGSN that start |
| 2651 | * their own NS-RESET procedure without waiting for our |
| 2652 | * NS-RESET */ |
| 2653 | nsvc->remote_end_is_sgsn = 1; |
| 2654 | } |
| 2655 | |
Harald Welte | fa41cb7 | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 2656 | if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) { |
| 2657 | LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, " |
| 2658 | "re-starting RESET procedure\n"); |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2659 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | 174bad4 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 2660 | ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]); |
Harald Welte | a529c07 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 2661 | gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr, |
| 2662 | nsvc->nsei, nsvc->nsvci); |
Harald Welte | fa41cb7 | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 2665 | if (!nsvc->remote_end_is_sgsn) { |
| 2666 | /* from BSS to SGSN */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2667 | peer = gbprox_peer_by_nsei(cfg, nsvc->nsei); |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 2668 | if (!peer) { |
| 2669 | LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer " |
| 2670 | "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei, |
| 2671 | nsvc->nsvci); |
| 2672 | return 0; |
| 2673 | } |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 2674 | switch (signal) { |
| 2675 | case S_NS_RESET: |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 2676 | case S_NS_BLOCK: |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 2677 | if (!peer->blocked) |
| 2678 | break; |
| 2679 | LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from " |
| 2680 | "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n", |
| 2681 | nsvc->nsei, nsvc->nsvci); |
| 2682 | bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei, |
| 2683 | peer->bvci, 0); |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 2684 | break; |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 2685 | } |
| 2686 | } else { |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2687 | /* Forward this message to all NS-VC to BSS */ |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2688 | struct gprs_ns_inst *nsi = cfg->nsi; |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2689 | struct gprs_nsvc *next_nsvc; |
| 2690 | |
| 2691 | llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) { |
| 2692 | if (next_nsvc->remote_end_is_sgsn) |
| 2693 | continue; |
| 2694 | |
| 2695 | /* Note that the following does not start the full |
| 2696 | * procedures including timer based retransmissions. */ |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 2697 | switch (signal) { |
| 2698 | case S_NS_RESET: |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2699 | gprs_ns_tx_reset(next_nsvc, nssd->cause); |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 2700 | break; |
| 2701 | case S_NS_BLOCK: |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2702 | gprs_ns_tx_block(next_nsvc, nssd->cause); |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 2703 | break; |
| 2704 | case S_NS_UNBLOCK: |
Jacob Erlbeck | e48c347 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 2705 | gprs_ns_tx_unblock(next_nsvc); |
Harald Welte | db2a2d5 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 2706 | break; |
| 2707 | } |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 2708 | } |
| 2709 | } |
| 2710 | return 0; |
| 2711 | } |
| 2712 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2713 | void gbprox_reset(struct gbproxy_config *cfg) |
Jacob Erlbeck | 67f03bd | 2013-10-24 12:48:55 +0200 | [diff] [blame] | 2714 | { |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 2715 | struct gbproxy_peer *peer, *tmp; |
Jacob Erlbeck | 67f03bd | 2013-10-24 12:48:55 +0200 | [diff] [blame] | 2716 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2717 | llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list) |
Holger Hans Peter Freyther | 60fa5b9 | 2014-08-04 17:10:08 +0200 | [diff] [blame] | 2718 | gbproxy_peer_free(peer); |
Jacob Erlbeck | 67f03bd | 2013-10-24 12:48:55 +0200 | [diff] [blame] | 2719 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2720 | rate_ctr_group_free(cfg->ctrg); |
| 2721 | gbproxy_init_config(cfg); |
Jacob Erlbeck | 67f03bd | 2013-10-24 12:48:55 +0200 | [diff] [blame] | 2722 | } |
| 2723 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2724 | int gbprox_cleanup_peers(struct gbproxy_config *cfg, uint16_t nsei, uint16_t bvci) |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 2725 | { |
| 2726 | int counter = 0; |
Holger Hans Peter Freyther | d64bf22 | 2014-08-04 11:35:32 +0200 | [diff] [blame] | 2727 | struct gbproxy_peer *peer, *tmp; |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 2728 | |
Holger Hans Peter Freyther | 7d9c1df | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 2729 | llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list) { |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 2730 | if (peer->nsei != nsei) |
| 2731 | continue; |
| 2732 | if (bvci && peer->bvci != bvci) |
| 2733 | continue; |
| 2734 | |
Holger Hans Peter Freyther | 60fa5b9 | 2014-08-04 17:10:08 +0200 | [diff] [blame] | 2735 | gbproxy_peer_free(peer); |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 2736 | counter += 1; |
| 2737 | } |
| 2738 | |
| 2739 | return counter; |
| 2740 | } |
| 2741 | |
Holger Hans Peter Freyther | 5eaf1a2 | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 2742 | int gbproxy_init_config(struct gbproxy_config *cfg) |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 2743 | { |
Jacob Erlbeck | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 2744 | struct timespec tp; |
Holger Hans Peter Freyther | 5eaf1a2 | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 2745 | INIT_LLIST_HEAD(&cfg->bts_peers); |
Holger Hans Peter Freyther | 98e753f | 2014-08-04 11:19:56 +0200 | [diff] [blame] | 2746 | cfg->ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0); |
Jacob Erlbeck | 4c47f2f | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 2747 | clock_gettime(CLOCK_REALTIME, &tp); |
| 2748 | cfg->bss_ptmsi_state = tp.tv_sec + tp.tv_nsec; |
| 2749 | cfg->sgsn_tlli_state = tp.tv_sec - tp.tv_nsec; |
Holger Hans Peter Freyther | 5eaf1a2 | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 2750 | return 0; |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 2751 | } |