Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1 | /* NS-over-IP proxy */ |
| 2 | |
| 3 | /* (C) 2010 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 85531cc | 2010-10-06 20:37:09 +0800 | [diff] [blame] | 4 | * (C) 2010 by On-Waves |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 16 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <unistd.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <getopt.h> |
| 27 | #include <errno.h> |
| 28 | #include <sys/fcntl.h> |
| 29 | #include <sys/stat.h> |
Harald Welte | 7fc9822 | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 30 | #include <arpa/inet.h> |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 31 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 32 | #include <osmocom/core/talloc.h> |
| 33 | #include <osmocom/core/select.h> |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 34 | |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 35 | #include <osmocom/gprs/gprs_ns.h> |
| 36 | #include <osmocom/gprs/gprs_bssgp.h> |
| 37 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 38 | #include <openbsc/signal.h> |
| 39 | #include <openbsc/debug.h> |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 40 | #include <openbsc/gb_proxy.h> |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 41 | |
| 42 | struct gbprox_peer { |
| 43 | struct llist_head list; |
| 44 | |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 45 | /* NSEI of the peer entity */ |
| 46 | uint16_t nsei; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 47 | |
| 48 | /* BVCI used for Point-to-Point to this peer */ |
| 49 | uint16_t bvci; |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 50 | int blocked; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 51 | |
| 52 | /* Routeing Area that this peer is part of (raw 04.08 encoding) */ |
| 53 | uint8_t ra[6]; |
| 54 | }; |
| 55 | |
| 56 | /* Linked list of all Gb peers (except SGSN) */ |
| 57 | static LLIST_HEAD(gbprox_bts_peers); |
| 58 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 59 | /* Find the gbprox_peer by its BVCI */ |
| 60 | static struct gbprox_peer *peer_by_bvci(uint16_t bvci) |
| 61 | { |
| 62 | struct gbprox_peer *peer; |
| 63 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
| 64 | if (peer->bvci == bvci) |
| 65 | return peer; |
| 66 | } |
| 67 | return NULL; |
| 68 | } |
| 69 | |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 70 | /* Find the gbprox_peer by its NSEI */ |
| 71 | static struct gbprox_peer *peer_by_nsei(uint16_t nsei) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 72 | { |
| 73 | struct gbprox_peer *peer; |
| 74 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 75 | if (peer->nsei == nsei) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 76 | return peer; |
| 77 | } |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | /* look-up a peer by its Routeing Area Code (RAC) */ |
Harald Welte | 70f38d2 | 2010-05-01 12:10:57 +0200 | [diff] [blame] | 82 | static struct gbprox_peer *peer_by_rac(const uint8_t *ra) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 83 | { |
| 84 | struct gbprox_peer *peer; |
| 85 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 86 | if (!memcmp(peer->ra, ra, 6)) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 87 | return peer; |
| 88 | } |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | /* look-up a peer by its Location Area Code (LAC) */ |
Harald Welte | 70f38d2 | 2010-05-01 12:10:57 +0200 | [diff] [blame] | 93 | static struct gbprox_peer *peer_by_lac(const uint8_t *la) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 94 | { |
| 95 | struct gbprox_peer *peer; |
| 96 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 97 | if (!memcmp(peer->ra, la, 5)) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 98 | return peer; |
| 99 | } |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | static struct gbprox_peer *peer_alloc(uint16_t bvci) |
| 104 | { |
| 105 | struct gbprox_peer *peer; |
| 106 | |
| 107 | peer = talloc_zero(tall_bsc_ctx, struct gbprox_peer); |
| 108 | if (!peer) |
| 109 | return NULL; |
| 110 | |
| 111 | peer->bvci = bvci; |
| 112 | llist_add(&peer->list, &gbprox_bts_peers); |
| 113 | |
| 114 | return peer; |
| 115 | } |
| 116 | |
Jacob Erlbeck | c5085f9 | 2013-10-18 13:04:48 +0200 | [diff] [blame^] | 117 | static void peer_free(struct gbprox_peer *peer) __attribute__((__unused__)); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 118 | static void peer_free(struct gbprox_peer *peer) |
| 119 | { |
| 120 | llist_del(&peer->list); |
| 121 | talloc_free(peer); |
| 122 | } |
| 123 | |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 124 | /* FIXME: this needs to go to libosmocore/msgb.c */ |
| 125 | static struct msgb *msgb_copy(const struct msgb *msg, const char *name) |
| 126 | { |
Harald Welte | 8645e10 | 2012-06-16 16:09:52 +0800 | [diff] [blame] | 127 | struct libgb_msgb_cb *old_cb, *new_cb; |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 128 | struct msgb *new_msg; |
| 129 | |
Holger Hans Peter Freyther | 62d9798 | 2010-06-08 18:14:37 +0800 | [diff] [blame] | 130 | new_msg = msgb_alloc(msg->data_len, name); |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 131 | if (!new_msg) |
| 132 | return NULL; |
| 133 | |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 134 | /* copy data */ |
Holger Hans Peter Freyther | 62d9798 | 2010-06-08 18:14:37 +0800 | [diff] [blame] | 135 | memcpy(new_msg->_data, msg->_data, new_msg->data_len); |
| 136 | |
| 137 | /* copy header */ |
| 138 | new_msg->len = msg->len; |
| 139 | new_msg->data += msg->data - msg->_data; |
| 140 | new_msg->head += msg->head - msg->_data; |
| 141 | new_msg->tail += msg->tail - msg->_data; |
| 142 | |
| 143 | new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data); |
| 144 | new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data); |
| 145 | new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data); |
| 146 | new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data); |
| 147 | |
| 148 | /* copy GB specific data */ |
Harald Welte | 8645e10 | 2012-06-16 16:09:52 +0800 | [diff] [blame] | 149 | old_cb = LIBGB_MSGB_CB(msg); |
| 150 | new_cb = LIBGB_MSGB_CB(new_msg); |
Holger Hans Peter Freyther | 62d9798 | 2010-06-08 18:14:37 +0800 | [diff] [blame] | 151 | |
| 152 | new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data); |
| 153 | new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data); |
| 154 | |
Harald Welte | fb9e06f | 2011-02-06 17:17:05 +0100 | [diff] [blame] | 155 | /* bssgp_cell_id is a pointer into the old msgb, so we need to make |
| 156 | * it a pointer into the new msgb */ |
| 157 | new_cb->bssgp_cell_id = new_msg->_data + (old_cb->bssgp_cell_id - msg->_data); |
Holger Hans Peter Freyther | 62d9798 | 2010-06-08 18:14:37 +0800 | [diff] [blame] | 158 | new_cb->nsei = old_cb->nsei; |
| 159 | new_cb->bvci = old_cb->bvci; |
| 160 | new_cb->tlli = old_cb->tlli; |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 161 | |
| 162 | return new_msg; |
| 163 | } |
| 164 | |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 165 | /* strip off the NS header */ |
| 166 | static void strip_ns_hdr(struct msgb *msg) |
| 167 | { |
| 168 | int strip_len = msgb_bssgph(msg) - msg->data; |
| 169 | msgb_pull(msg, strip_len); |
| 170 | } |
| 171 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 172 | /* feed a message down the NS-VC associated with the specified peer */ |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 173 | static int gbprox_relay2sgsn(struct msgb *old_msg, uint16_t ns_bvci) |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 174 | { |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 175 | /* create a copy of the message so the old one can |
| 176 | * be free()d safely when we return from gbprox_rcvmsg() */ |
| 177 | struct msgb *msg = msgb_copy(old_msg, "msgb_relay2sgsn"); |
| 178 | |
Harald Welte | e9ea269 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 179 | DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n", |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 180 | msgb_nsei(msg), ns_bvci, gbcfg.nsip_sgsn_nsei); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 181 | |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 182 | msgb_bvci(msg) = ns_bvci; |
| 183 | msgb_nsei(msg) = gbcfg.nsip_sgsn_nsei; |
| 184 | |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 185 | strip_ns_hdr(msg); |
| 186 | |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 187 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 190 | /* feed a message down the NS-VC associated with the specified peer */ |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 191 | static int gbprox_relay2peer(struct msgb *old_msg, struct gbprox_peer *peer, |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 192 | uint16_t ns_bvci) |
| 193 | { |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 194 | /* create a copy of the message so the old one can |
| 195 | * be free()d safely when we return from gbprox_rcvmsg() */ |
| 196 | struct msgb *msg = msgb_copy(old_msg, "msgb_relay2peer"); |
| 197 | |
Harald Welte | 0ab535b | 2010-05-13 10:34:56 +0200 | [diff] [blame] | 198 | DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 199 | msgb_nsei(msg), ns_bvci, peer->nsei); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 200 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 201 | msgb_bvci(msg) = ns_bvci; |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 202 | msgb_nsei(msg) = peer->nsei; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 203 | |
Harald Welte | 0ab535b | 2010-05-13 10:34:56 +0200 | [diff] [blame] | 204 | /* Strip the old NS header, it will be replaced with a new one */ |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 205 | strip_ns_hdr(msg); |
| 206 | |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 207 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 210 | static int block_unblock_peer(uint16_t ptp_bvci, uint8_t pdu_type) |
| 211 | { |
| 212 | struct gbprox_peer *peer; |
| 213 | |
| 214 | peer = peer_by_bvci(ptp_bvci); |
| 215 | if (!peer) { |
| 216 | LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n", |
| 217 | ptp_bvci); |
| 218 | return -ENOENT; |
| 219 | } |
| 220 | |
| 221 | switch (pdu_type) { |
| 222 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 223 | peer->blocked = 1; |
| 224 | break; |
| 225 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 226 | peer->blocked = 0; |
| 227 | break; |
| 228 | default: |
| 229 | break; |
| 230 | } |
| 231 | return 0; |
| 232 | } |
| 233 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 234 | /* Send a message to a peer identified by ptp_bvci but using ns_bvci |
| 235 | * in the NS hdr */ |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 236 | static int gbprox_relay2bvci(struct msgb *msg, uint16_t ptp_bvci, |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 237 | uint16_t ns_bvci) |
| 238 | { |
| 239 | struct gbprox_peer *peer; |
| 240 | |
| 241 | peer = peer_by_bvci(ptp_bvci); |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 242 | if (!peer) { |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 243 | LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n", |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 244 | ptp_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 245 | return -ENOENT; |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 246 | } |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 247 | |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 248 | return gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Harald Welte | b1fd902 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 251 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 252 | { |
| 253 | return 0; |
| 254 | } |
| 255 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 256 | /* Receive an incoming signalling message from a BSS-side NS-VC */ |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 257 | static int gbprox_rx_sig_from_bss(struct msgb *msg, uint16_t nsei, |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 258 | uint16_t ns_bvci) |
| 259 | { |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 260 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 261 | struct tlv_parsed tp; |
| 262 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 263 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 264 | struct gbprox_peer *from_peer; |
Harald Welte | 70f38d2 | 2010-05-01 12:10:57 +0200 | [diff] [blame] | 265 | struct gprs_ra_id raid; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 266 | |
Harald Welte | c471d3d | 2011-02-06 17:13:12 +0100 | [diff] [blame] | 267 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 268 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 269 | nsei, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 270 | return -EINVAL; |
| 271 | } |
| 272 | |
| 273 | /* we actually should never see those two for BVCI == 0, but double-check |
| 274 | * just to make sure */ |
| 275 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 276 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 277 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 278 | "signalling\n", nsei); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 279 | return -EINVAL; |
| 280 | } |
| 281 | |
| 282 | bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 283 | |
| 284 | switch (pdu_type) { |
| 285 | case BSSGP_PDUT_SUSPEND: |
| 286 | case BSSGP_PDUT_RESUME: |
Harald Welte | 70f38d2 | 2010-05-01 12:10:57 +0200 | [diff] [blame] | 287 | /* We implement RAC snooping during SUSPEND/RESUME, since |
| 288 | * it establishes a relationsip between BVCI/peer and the |
| 289 | * routeing area code. The snooped information is then |
| 290 | * used for routing the {SUSPEND,RESUME}_[N]ACK back to |
| 291 | * the correct BSSGP */ |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 292 | if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 293 | goto err_mand_ie; |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 294 | from_peer = peer_by_nsei(nsei); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 295 | if (!from_peer) |
| 296 | goto err_no_peer; |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 297 | memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA), |
| 298 | sizeof(from_peer->ra)); |
Harald Welte | 7fc9822 | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 299 | gsm48_parse_ra(&raid, from_peer->ra); |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 300 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 301 | "RAC snooping: RAC %u-%u-%u-%u behind BVCI=%u\n", |
| 302 | nsei, raid.mcc, raid.mnc, raid.lac, |
| 303 | raid.rac , from_peer->bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 304 | /* FIXME: This only supports one BSS per RA */ |
| 305 | break; |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 306 | case BSSGP_PDUT_BVC_RESET: |
| 307 | /* If we receive a BVC reset on the signalling endpoint, we |
| 308 | * don't want the SGSN to reset, as the signalling endpoint |
| 309 | * is common for all point-to-point BVCs (and thus all BTS) */ |
| 310 | if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 311 | uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 72953b8 | 2010-05-12 00:20:41 +0200 | [diff] [blame] | 312 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 313 | nsei, bvci); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 314 | if (bvci == 0) { |
| 315 | /* FIXME: only do this if SGSN is alive! */ |
Harald Welte | e9ea269 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 316 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 317 | "BVC RESET ACK of BVCI=0\n", nsei); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 318 | return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 319 | nsei, 0, ns_bvci); |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 320 | } |
| 321 | from_peer = peer_by_bvci(bvci); |
| 322 | if (!from_peer) { |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 323 | /* if a PTP-BVC is reset, and we don't know that |
| 324 | * PTP-BVCI yet, we should allocate a new peer */ |
| 325 | LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 326 | "BVCI=%u via NSEI=%u\n", bvci, nsei); |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 327 | from_peer = peer_alloc(bvci); |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 328 | from_peer->nsei = nsei; |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 329 | } |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 330 | if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) { |
| 331 | struct gprs_ra_id raid; |
| 332 | /* We have a Cell Identifier present in this |
| 333 | * PDU, this means we can extend our local |
| 334 | * state information about this particular cell |
| 335 | * */ |
| 336 | memcpy(from_peer->ra, |
| 337 | TLVP_VAL(&tp, BSSGP_IE_CELL_ID), |
| 338 | sizeof(from_peer->ra)); |
| 339 | gsm48_parse_ra(&raid, from_peer->ra); |
| 340 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 341 | "Cell ID %u-%u-%u-%u\n", nsei, |
Harald Welte | 1174c08 | 2010-05-12 00:07:29 +0200 | [diff] [blame] | 342 | bvci, raid.mcc, raid.mnc, raid.lac, |
| 343 | raid.rac); |
| 344 | } |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 345 | } |
| 346 | break; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 347 | } |
| 348 | |
Harald Welte | e9ea269 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 349 | /* Normally, we can simply pass on all signalling messages from BSS to |
| 350 | * SGSN */ |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 351 | return gbprox_relay2sgsn(msg, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 352 | err_no_peer: |
Jacob Erlbeck | c5085f9 | 2013-10-18 13:04:48 +0200 | [diff] [blame^] | 353 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 354 | nsei); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 355 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 356 | err_mand_ie: |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 357 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 358 | nsei); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 359 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* Receive paging request from SGSN, we need to relay to proper BSS */ |
| 363 | static int gbprox_rx_paging(struct msgb *msg, struct tlv_parsed *tp, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 364 | uint32_t nsei, uint16_t ns_bvci) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 365 | { |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 366 | struct gbprox_peer *peer = NULL; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 367 | |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 368 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 369 | nsei); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 370 | if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 371 | uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 372 | LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n", |
| 373 | bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 374 | } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) { |
| 375 | peer = peer_by_rac(TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA)); |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 376 | LOGPC(DGPRS, LOGL_INFO, "routing by RAC to peer BVCI=%u\n", |
Holger Hans Peter Freyther | 62eaf54 | 2010-06-08 16:30:24 +0800 | [diff] [blame] | 377 | peer ? peer->bvci : -1); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 378 | } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) { |
| 379 | peer = peer_by_lac(TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA)); |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 380 | LOGPC(DGPRS, LOGL_INFO, "routing by LAC to peer BVCI=%u\n", |
Holger Hans Peter Freyther | 62eaf54 | 2010-06-08 16:30:24 +0800 | [diff] [blame] | 381 | peer ? peer->bvci : -1); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 382 | } else |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 383 | LOGPC(DGPRS, LOGL_INFO, "\n"); |
| 384 | |
| 385 | if (!peer) { |
| 386 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 387 | "unable to route, missing IE\n", nsei); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 388 | return -EINVAL; |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 389 | } |
| 390 | return gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 391 | } |
| 392 | |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 393 | /* Receive an incoming BVC-RESET message from the SGSN */ |
| 394 | static int rx_reset_from_sgsn(struct msgb *msg, struct tlv_parsed *tp, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 395 | uint32_t nsei, uint16_t ns_bvci) |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 396 | { |
| 397 | struct gbprox_peer *peer; |
| 398 | uint16_t ptp_bvci; |
| 399 | |
| 400 | if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
| 401 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, |
| 402 | NULL, msg); |
| 403 | } |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 404 | ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 405 | |
| 406 | if (ptp_bvci >= 2) { |
| 407 | /* A reset for a PTP BVC was received, forward it to its |
| 408 | * respective peer */ |
| 409 | peer = peer_by_bvci(ptp_bvci); |
| 410 | if (!peer) { |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 411 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 412 | nsei, ptp_bvci); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 413 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 414 | NULL, msg); |
| 415 | } |
| 416 | return gbprox_relay2peer(msg, peer, ns_bvci); |
| 417 | } |
| 418 | |
| 419 | /* A reset for the Signalling entity has been received |
| 420 | * from the SGSN. As the signalling BVCI is shared |
| 421 | * among all the BSS's that we multiplex, it needs to |
| 422 | * be relayed */ |
| 423 | llist_for_each_entry(peer, &gbprox_bts_peers, list) |
| 424 | gbprox_relay2peer(msg, peer, ns_bvci); |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 429 | /* Receive an incoming signalling message from the SGSN-side NS-VC */ |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 430 | static int gbprox_rx_sig_from_sgsn(struct msgb *msg, uint32_t nsei, |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 431 | uint16_t ns_bvci) |
| 432 | { |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 433 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 434 | struct tlv_parsed tp; |
| 435 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 436 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 437 | struct gbprox_peer *peer; |
| 438 | uint16_t bvci; |
| 439 | int rc = 0; |
| 440 | |
Harald Welte | c471d3d | 2011-02-06 17:13:12 +0100 | [diff] [blame] | 441 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 442 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 443 | "signalling\n", nsei, ns_bvci); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 444 | /* FIXME: Send proper error message */ |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 445 | return -EINVAL; |
| 446 | } |
| 447 | |
| 448 | /* we actually should never see those two for BVCI == 0, but double-check |
| 449 | * just to make sure */ |
| 450 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 451 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 452 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 453 | "signalling\n", nsei); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 454 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 458 | |
| 459 | switch (pdu_type) { |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 460 | case BSSGP_PDUT_BVC_RESET: |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 461 | rc = rx_reset_from_sgsn(msg, &tp, nsei, ns_bvci); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 462 | break; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 463 | case BSSGP_PDUT_FLUSH_LL: |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 464 | case BSSGP_PDUT_BVC_RESET_ACK: |
| 465 | /* simple case: BVCI IE is mandatory */ |
| 466 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 467 | goto err_mand_ie; |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 468 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 469 | rc = gbprox_relay2bvci(msg, bvci, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 470 | break; |
| 471 | case BSSGP_PDUT_PAGING_PS: |
| 472 | case BSSGP_PDUT_PAGING_CS: |
| 473 | /* process the paging request (LAC/RAC lookup) */ |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 474 | rc = gbprox_rx_paging(msg, &tp, nsei, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 475 | break; |
| 476 | case BSSGP_PDUT_STATUS: |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 477 | /* Some exception has occurred */ |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 478 | LOGP(DGPRS, LOGL_NOTICE, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 479 | "NSEI=%u(SGSN) BSSGP STATUS ", nsei); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 480 | if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) { |
| 481 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
| 482 | goto err_mand_ie; |
| 483 | } |
| 484 | LOGPC(DGPRS, LOGL_NOTICE, |
| 485 | "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE), |
| 486 | bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE))); |
| 487 | if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 488 | uint16_t bvci = tlvp_val16_unal(&tp, BSSGP_IE_BVCI); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 489 | LOGPC(DGPRS, LOGL_NOTICE, |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 490 | "BVCI=%u\n", ntohs(bvci)); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 491 | } else |
| 492 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 493 | break; |
| 494 | /* those only exist in the SGSN -> BSS direction */ |
| 495 | case BSSGP_PDUT_SUSPEND_ACK: |
| 496 | case BSSGP_PDUT_SUSPEND_NACK: |
| 497 | case BSSGP_PDUT_RESUME_ACK: |
| 498 | case BSSGP_PDUT_RESUME_NACK: |
| 499 | /* RAC IE is mandatory */ |
| 500 | if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 501 | goto err_mand_ie; |
| 502 | peer = peer_by_rac(TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA)); |
| 503 | if (!peer) |
| 504 | goto err_no_peer; |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 505 | rc = gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 506 | break; |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 507 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 508 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 509 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 510 | goto err_mand_ie; |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 511 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 512 | if (bvci == 0) { |
| 513 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 514 | "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei, |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 515 | pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":""); |
| 516 | /* should we send STATUS ? */ |
| 517 | } else { |
| 518 | /* Mark BVC as (un)blocked */ |
| 519 | block_unblock_peer(bvci, pdu_type); |
| 520 | } |
| 521 | rc = gbprox_relay2bvci(msg, bvci, ns_bvci); |
| 522 | break; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 523 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 524 | LOGP(DGPRS, LOGL_ERROR, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 525 | "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 526 | rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 527 | break; |
| 528 | default: |
Harald Welte | 72953b8 | 2010-05-12 00:20:41 +0200 | [diff] [blame] | 529 | LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n", |
| 530 | pdu_type); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 531 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 532 | break; |
| 533 | } |
| 534 | |
| 535 | return rc; |
| 536 | err_mand_ie: |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 537 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 538 | nsei); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 539 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 540 | err_no_peer: |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 541 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAC\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 542 | nsei); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 543 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /* Main input function for Gb proxy */ |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 547 | int gbprox_rcvmsg(struct msgb *msg, uint16_t nsei, uint16_t ns_bvci, uint16_t nsvci) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 548 | { |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 549 | int rc; |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 550 | struct gbprox_peer *peer; |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 551 | int remote_end_is_sgsn = nsei == gbcfg.nsip_sgsn_nsei; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 552 | |
| 553 | /* Only BVCI=0 messages need special treatment */ |
| 554 | if (ns_bvci == 0 || ns_bvci == 1) { |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 555 | if (remote_end_is_sgsn) |
| 556 | rc = gbprox_rx_sig_from_sgsn(msg, nsei, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 557 | else |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 558 | rc = gbprox_rx_sig_from_bss(msg, nsei, ns_bvci); |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 559 | } else { |
| 560 | /* All other BVCI are PTP and thus can be simply forwarded */ |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 561 | if (!remote_end_is_sgsn) |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 562 | return gbprox_relay2sgsn(msg, ns_bvci); |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 563 | |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 564 | /* else: SGSN -> BSS direction */ |
| 565 | peer = peer_by_bvci(ns_bvci); |
| 566 | if (!peer) { |
| 567 | LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for " |
| 568 | "BVCI=%u via NSVC=%u/NSEI=%u\n", ns_bvci, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 569 | nsvci, nsei); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 570 | peer = peer_alloc(ns_bvci); |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 571 | peer->nsei = nsei; |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 572 | } |
| 573 | if (peer->blocked) { |
| 574 | LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for " |
| 575 | "blocked BVCI=%u via NSVC=%u/NSEI=%u\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 576 | ns_bvci, nsvci, nsei); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 577 | return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, NULL, msg); |
| 578 | } |
| 579 | rc = gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 580 | } |
| 581 | |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 582 | return rc; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 583 | } |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 584 | |
Harald Welte | 1ccbf44 | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 585 | int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi) |
| 586 | { |
| 587 | struct gprs_nsvc *nsvc; |
| 588 | |
| 589 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 590 | if (!nsvc->persistent) |
| 591 | continue; |
| 592 | gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
| 593 | } |
| 594 | return 0; |
| 595 | } |
| 596 | |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 597 | /* Signal handler for signals from NS layer */ |
| 598 | int gbprox_signal(unsigned int subsys, unsigned int signal, |
| 599 | void *handler_data, void *signal_data) |
| 600 | { |
| 601 | struct ns_signal_data *nssd = signal_data; |
| 602 | struct gprs_nsvc *nsvc = nssd->nsvc; |
| 603 | struct gbprox_peer *peer; |
| 604 | |
Harald Welte | a6a20b4 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 605 | if (subsys != SS_L_NS) |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 606 | return 0; |
| 607 | |
Harald Welte | f69c059 | 2010-05-11 18:29:44 +0200 | [diff] [blame] | 608 | if (signal == S_NS_RESET && nsvc->nsei == gbcfg.nsip_sgsn_nsei) { |
| 609 | /* We have received a NS-RESET from the NSEI and NSVC |
| 610 | * of the SGSN. This might happen with SGSN that start |
| 611 | * their own NS-RESET procedure without waiting for our |
| 612 | * NS-RESET */ |
| 613 | nsvc->remote_end_is_sgsn = 1; |
| 614 | } |
| 615 | |
Harald Welte | b778d2c | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 616 | if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) { |
| 617 | LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, " |
| 618 | "re-starting RESET procedure\n"); |
Harald Welte | e6599ee | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 619 | gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr, |
| 620 | nsvc->nsei, nsvc->nsvci); |
Harald Welte | b778d2c | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 623 | if (!nsvc->remote_end_is_sgsn) { |
| 624 | /* from BSS to SGSN */ |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 625 | peer = peer_by_nsei(nsvc->nsei); |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 626 | if (!peer) { |
| 627 | LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer " |
| 628 | "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei, |
| 629 | nsvc->nsvci); |
| 630 | return 0; |
| 631 | } |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 632 | switch (signal) { |
| 633 | case S_NS_RESET: |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 634 | case S_NS_BLOCK: |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 635 | if (!peer->blocked) |
| 636 | break; |
| 637 | LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from " |
| 638 | "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n", |
| 639 | nsvc->nsei, nsvc->nsvci); |
| 640 | bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei, |
| 641 | peer->bvci, 0); |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 642 | break; |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 643 | } |
| 644 | } else { |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 645 | /* Forward this message to all NS-VC to BSS */ |
| 646 | struct gprs_ns_inst *nsi = gbcfg.nsi; |
| 647 | struct gprs_nsvc *next_nsvc; |
| 648 | |
| 649 | llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) { |
| 650 | if (next_nsvc->remote_end_is_sgsn) |
| 651 | continue; |
| 652 | |
| 653 | /* Note that the following does not start the full |
| 654 | * procedures including timer based retransmissions. */ |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 655 | switch (signal) { |
| 656 | case S_NS_RESET: |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 657 | gprs_ns_tx_reset(next_nsvc, nssd->cause); |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 658 | break; |
| 659 | case S_NS_BLOCK: |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 660 | gprs_ns_tx_block(next_nsvc, nssd->cause); |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 661 | break; |
| 662 | case S_NS_UNBLOCK: |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 663 | gprs_ns_tx_unblock(next_nsvc); |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 664 | break; |
| 665 | } |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | return 0; |
| 669 | } |
| 670 | |
Jacob Erlbeck | 51a869c | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 671 | int gbprox_dump_peers(FILE *stream, int indent) |
| 672 | { |
| 673 | struct gbprox_peer *peer; |
| 674 | struct gprs_ra_id raid; |
| 675 | int rc; |
| 676 | |
| 677 | fprintf(stream, "%*sPeers:\n", indent, ""); |
| 678 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
| 679 | gsm48_parse_ra(&raid, peer->ra); |
| 680 | |
| 681 | rc = fprintf(stream, "%*s NSEI %u, BVCI %u, %sblocked, " |
| 682 | "RAC %u-%u-%u-%u\n", |
| 683 | indent, "", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 684 | peer->nsei, peer->bvci, |
Jacob Erlbeck | 51a869c | 2013-10-15 12:00:26 +0200 | [diff] [blame] | 685 | peer->blocked ? "" : "not ", |
| 686 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 687 | |
| 688 | if (rc < 0) |
| 689 | return rc; |
| 690 | } |
| 691 | |
| 692 | return 0; |
| 693 | } |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 694 | |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 695 | #include <osmocom/vty/command.h> |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 696 | |
| 697 | gDEFUN(show_gbproxy, show_gbproxy_cmd, "show gbproxy", |
| 698 | SHOW_STR "Display information about the Gb proxy") |
| 699 | { |
| 700 | struct gbprox_peer *peer; |
| 701 | |
| 702 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 703 | struct gprs_ra_id raid; |
Harald Welte | 7fc9822 | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 704 | gsm48_parse_ra(&raid, peer->ra); |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 705 | |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 706 | vty_out(vty, "NSEI %5u, PTP-BVCI %5u, " |
Harald Welte | 7570c21 | 2010-05-19 17:06:16 +0200 | [diff] [blame] | 707 | "RAC %u-%u-%u-%u", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 708 | peer->nsei, peer->bvci, |
Harald Welte | 7570c21 | 2010-05-19 17:06:16 +0200 | [diff] [blame] | 709 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
Harald Welte | f3956cb | 2011-02-06 17:44:14 +0100 | [diff] [blame] | 710 | if (peer->blocked) |
| 711 | vty_out(vty, " [BVC-BLOCKED]"); |
| 712 | |
| 713 | vty_out(vty, "%s", VTY_NEWLINE); |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 714 | } |
| 715 | return CMD_SUCCESS; |
| 716 | } |