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