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> |
| 4 | * (C) 2010 by On Waves |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 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 |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 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> |
| 31 | #include <sys/types.h> |
Harald Welte | 7fc9822 | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 32 | #include <arpa/inet.h> |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 33 | |
| 34 | #include <osmocore/talloc.h> |
| 35 | #include <osmocore/select.h> |
| 36 | |
| 37 | #include <openbsc/signal.h> |
| 38 | #include <openbsc/debug.h> |
| 39 | #include <openbsc/gprs_ns.h> |
| 40 | #include <openbsc/gprs_bssgp.h> |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 41 | #include <openbsc/gb_proxy.h> |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 42 | |
| 43 | struct gbprox_peer { |
| 44 | struct llist_head list; |
| 45 | |
| 46 | /* NS-VC over which we send/receive data to this BVC */ |
| 47 | struct gprs_nsvc *nsvc; |
| 48 | |
| 49 | /* BVCI used for Point-to-Point to this peer */ |
| 50 | uint16_t bvci; |
| 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 | |
| 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 | 70f38d2 | 2010-05-01 12:10:57 +0200 | [diff] [blame] | 81 | static struct gbprox_peer *peer_by_rac(const uint8_t *ra) |
Harald Welte | 9f75c35 | 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) { |
| 85 | if (!memcmp(&peer->ra, ra, 6)) |
| 86 | return peer; |
| 87 | } |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | /* look-up a peer by its Location Area Code (LAC) */ |
Harald Welte | 70f38d2 | 2010-05-01 12:10:57 +0200 | [diff] [blame] | 92 | static struct gbprox_peer *peer_by_lac(const uint8_t *la) |
Harald Welte | 9f75c35 | 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) { |
| 96 | if (!memcmp(&peer->ra, la, 5)) |
| 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 | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 122 | /* strip off the NS header */ |
| 123 | static void strip_ns_hdr(struct msgb *msg) |
| 124 | { |
| 125 | int strip_len = msgb_bssgph(msg) - msg->data; |
| 126 | msgb_pull(msg, strip_len); |
| 127 | } |
| 128 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 129 | /* feed a message down the NS-VC associated with the specified peer */ |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 130 | static int gbprox_relay2sgsn(struct msgb *msg, uint16_t ns_bvci) |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 131 | { |
Harald Welte | e9ea269 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 132 | 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] | 133 | msgb_nsei(msg), ns_bvci, gbcfg.nsip_sgsn_nsei); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 134 | |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 135 | msgb_bvci(msg) = ns_bvci; |
| 136 | msgb_nsei(msg) = gbcfg.nsip_sgsn_nsei; |
| 137 | |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 138 | strip_ns_hdr(msg); |
| 139 | |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 140 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 143 | /* feed a message down the NS-VC associated with the specified peer */ |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 144 | static int gbprox_relay2peer(struct msgb *msg, struct gbprox_peer *peer, |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 145 | uint16_t ns_bvci) |
| 146 | { |
Harald Welte | e9ea269 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 147 | DEBUGP(DGPRS, "NSEI=%u proxying to SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n", |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 148 | msgb_nsei(msg), ns_bvci, peer->nsvc->nsei); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 149 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 150 | msgb_bvci(msg) = ns_bvci; |
| 151 | msgb_nsei(msg) = peer->nsvc->nsei; |
| 152 | |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 153 | strip_ns_hdr(msg); |
| 154 | |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 155 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* Send a message to a peer identified by ptp_bvci but using ns_bvci |
| 159 | * in the NS hdr */ |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 160 | static int gbprox_relay2bvci(struct msgb *msg, uint16_t ptp_bvci, |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 161 | uint16_t ns_bvci) |
| 162 | { |
| 163 | struct gbprox_peer *peer; |
| 164 | |
| 165 | peer = peer_by_bvci(ptp_bvci); |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 166 | if (!peer) { |
| 167 | LOGP(DGPRS, LOGL_ERROR, "Cannot find BSS for BVCI %u\n", |
| 168 | ptp_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 169 | return -ENOENT; |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 170 | } |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 171 | |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 172 | return gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* Receive an incoming signalling message from a BSS-side NS-VC */ |
| 176 | static int gbprox_rx_sig_from_bss(struct msgb *msg, struct gprs_nsvc *nsvc, |
| 177 | uint16_t ns_bvci) |
| 178 | { |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 179 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 180 | struct tlv_parsed tp; |
| 181 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 182 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 183 | struct gbprox_peer *from_peer; |
Harald Welte | 70f38d2 | 2010-05-01 12:10:57 +0200 | [diff] [blame] | 184 | struct gprs_ra_id raid; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 185 | |
| 186 | if (ns_bvci != 0) { |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 187 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI %u is not signalling\n", |
| 188 | nsvc->nsei, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 189 | return -EINVAL; |
| 190 | } |
| 191 | |
| 192 | /* we actually should never see those two for BVCI == 0, but double-check |
| 193 | * just to make sure */ |
| 194 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 195 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 196 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in " |
| 197 | "signalling\n", nsvc->nsei); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 198 | return -EINVAL; |
| 199 | } |
| 200 | |
| 201 | bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 202 | |
| 203 | switch (pdu_type) { |
| 204 | case BSSGP_PDUT_SUSPEND: |
| 205 | case BSSGP_PDUT_RESUME: |
Harald Welte | 70f38d2 | 2010-05-01 12:10:57 +0200 | [diff] [blame] | 206 | /* We implement RAC snooping during SUSPEND/RESUME, since |
| 207 | * it establishes a relationsip between BVCI/peer and the |
| 208 | * routeing area code. The snooped information is then |
| 209 | * used for routing the {SUSPEND,RESUME}_[N]ACK back to |
| 210 | * the correct BSSGP */ |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 211 | if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 212 | goto err_mand_ie; |
| 213 | from_peer = peer_by_nsvc(nsvc); |
| 214 | if (!from_peer) |
| 215 | goto err_no_peer; |
| 216 | memcpy(&from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA), |
| 217 | sizeof(&from_peer->ra)); |
Harald Welte | 7fc9822 | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 218 | gsm48_parse_ra(&raid, from_peer->ra); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 219 | DEBUGP(DGPRS, "NSEI=%u RAC snooping: RAC %u/%u/%u/%u behind BVCI=%u, " |
| 220 | "NSVCI=%u\n", nsvc->nsei, raid.mcc, raid.mnc, raid.lac, |
| 221 | raid.rac , from_peer->bvci, nsvc->nsvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 222 | /* FIXME: This only supports one BSS per RA */ |
| 223 | break; |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 224 | case BSSGP_PDUT_BVC_RESET: |
| 225 | /* If we receive a BVC reset on the signalling endpoint, we |
| 226 | * don't want the SGSN to reset, as the signalling endpoint |
| 227 | * is common for all point-to-point BVCs (and thus all BTS) */ |
| 228 | if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
| 229 | uint16_t bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI)); |
Harald Welte | e9ea269 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 230 | LOGP(DGPRS, LOGL_DEBUG, "NSEI=%u Rx BVC RESET (BVCI=%u)\n", |
| 231 | nsvc->nsei, bvci); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 232 | if (bvci == 0) { |
| 233 | /* FIXME: only do this if SGSN is alive! */ |
Harald Welte | e9ea269 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 234 | LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake " |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 235 | "BVC RESET ACK of BVCI=0\n", nsvc->nsei); |
| 236 | return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK, |
| 237 | nsvc->nsei, 0, ns_bvci); |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 238 | } else if (!peer_by_bvci(bvci)) { |
| 239 | /* if a PTP-BVC is reset, and we don't know that |
| 240 | * PTP-BVCI yet, we should allocate a new peer */ |
| 241 | LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for " |
| 242 | "BVCI=%u via NSVCI=%u/NSEI=%u\n", bvci, |
| 243 | nsvc->nsvci, nsvc->nsei); |
| 244 | from_peer = peer_alloc(bvci); |
| 245 | from_peer->nsvc = nsvc; |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | break; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Harald Welte | e9ea269 | 2010-05-11 20:20:13 +0200 | [diff] [blame] | 251 | /* Normally, we can simply pass on all signalling messages from BSS to |
| 252 | * SGSN */ |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 253 | return gbprox_relay2sgsn(msg, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 254 | err_no_peer: |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 255 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on RAC\n", |
| 256 | nsvc->nsei); |
| 257 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 258 | err_mand_ie: |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 259 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n", |
| 260 | nsvc->nsei); |
| 261 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /* Receive paging request from SGSN, we need to relay to proper BSS */ |
| 265 | static int gbprox_rx_paging(struct msgb *msg, struct tlv_parsed *tp, |
| 266 | struct gprs_nsvc *nsvc, uint16_t ns_bvci) |
| 267 | { |
| 268 | struct gbprox_peer *peer; |
| 269 | |
| 270 | if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
| 271 | uint16_t bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI)); |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 272 | return gbprox_relay2bvci(msg, bvci, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 273 | } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) { |
| 274 | peer = peer_by_rac(TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA)); |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 275 | return gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 276 | } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) { |
| 277 | peer = peer_by_lac(TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA)); |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 278 | return gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 279 | } else |
| 280 | return -EINVAL; |
| 281 | } |
| 282 | |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 283 | /* Receive an incoming BVC-RESET message from the SGSN */ |
| 284 | static int rx_reset_from_sgsn(struct msgb *msg, struct tlv_parsed *tp, |
| 285 | struct gprs_nsvc *nsvc, uint16_t ns_bvci) |
| 286 | { |
| 287 | struct gbprox_peer *peer; |
| 288 | uint16_t ptp_bvci; |
| 289 | |
| 290 | if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
| 291 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, |
| 292 | NULL, msg); |
| 293 | } |
| 294 | ptp_bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI)); |
| 295 | |
| 296 | if (ptp_bvci >= 2) { |
| 297 | /* A reset for a PTP BVC was received, forward it to its |
| 298 | * respective peer */ |
| 299 | peer = peer_by_bvci(ptp_bvci); |
| 300 | if (!peer) { |
| 301 | LOGP(DGPRS, LOGL_ERROR, "Cannot find BSS for BVCI %u\n", |
| 302 | ptp_bvci); |
| 303 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 304 | NULL, msg); |
| 305 | } |
| 306 | return gbprox_relay2peer(msg, peer, ns_bvci); |
| 307 | } |
| 308 | |
| 309 | /* A reset for the Signalling entity has been received |
| 310 | * from the SGSN. As the signalling BVCI is shared |
| 311 | * among all the BSS's that we multiplex, it needs to |
| 312 | * be relayed */ |
| 313 | llist_for_each_entry(peer, &gbprox_bts_peers, list) |
| 314 | gbprox_relay2peer(msg, peer, ns_bvci); |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 319 | /* Receive an incoming signalling message from the SGSN-side NS-VC */ |
| 320 | static int gbprox_rx_sig_from_sgsn(struct msgb *msg, struct gprs_nsvc *nsvc, |
| 321 | uint16_t ns_bvci) |
| 322 | { |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 323 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 324 | struct tlv_parsed tp; |
| 325 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 326 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 327 | struct gbprox_peer *peer; |
| 328 | uint16_t bvci; |
| 329 | int rc = 0; |
| 330 | |
| 331 | if (ns_bvci != 0) { |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 332 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI %u is not " |
| 333 | "signalling\n", nsvc->nsei, ns_bvci); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 334 | /* FIXME: Send proper error message */ |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 335 | return -EINVAL; |
| 336 | } |
| 337 | |
| 338 | /* we actually should never see those two for BVCI == 0, but double-check |
| 339 | * just to make sure */ |
| 340 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 341 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 342 | LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in " |
| 343 | "signalling\n", nsvc->nsei); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 344 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 348 | |
| 349 | switch (pdu_type) { |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 350 | case BSSGP_PDUT_BVC_RESET: |
| 351 | rc = rx_reset_from_sgsn(msg, &tp, nsvc, ns_bvci); |
| 352 | break; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 353 | case BSSGP_PDUT_FLUSH_LL: |
| 354 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 355 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 356 | case BSSGP_PDUT_BVC_RESET_ACK: |
| 357 | /* simple case: BVCI IE is mandatory */ |
| 358 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 359 | goto err_mand_ie; |
| 360 | bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 361 | rc = gbprox_relay2bvci(msg, bvci, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 362 | break; |
| 363 | case BSSGP_PDUT_PAGING_PS: |
| 364 | case BSSGP_PDUT_PAGING_CS: |
| 365 | /* process the paging request (LAC/RAC lookup) */ |
| 366 | rc = gbprox_rx_paging(msg, &tp, nsvc, ns_bvci); |
| 367 | break; |
| 368 | case BSSGP_PDUT_STATUS: |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 369 | /* Some exception has occurred */ |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 370 | LOGP(DGPRS, LOGL_NOTICE, |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 371 | "NSEI=%u(SGSN) STATUS ", nsvc->nsei); |
| 372 | if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) { |
| 373 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
| 374 | goto err_mand_ie; |
| 375 | } |
| 376 | LOGPC(DGPRS, LOGL_NOTICE, |
| 377 | "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE), |
| 378 | bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE))); |
| 379 | if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
Harald Welte | 7fc9822 | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 380 | uint16_t *bvci = (uint16_t *) |
| 381 | TLVP_VAL(&tp, BSSGP_IE_BVCI); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 382 | LOGPC(DGPRS, LOGL_NOTICE, |
| 383 | "BVCI=%u\n", ntohs(*bvci)); |
| 384 | } else |
| 385 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 386 | break; |
| 387 | /* those only exist in the SGSN -> BSS direction */ |
| 388 | case BSSGP_PDUT_SUSPEND_ACK: |
| 389 | case BSSGP_PDUT_SUSPEND_NACK: |
| 390 | case BSSGP_PDUT_RESUME_ACK: |
| 391 | case BSSGP_PDUT_RESUME_NACK: |
| 392 | /* RAC IE is mandatory */ |
| 393 | if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 394 | goto err_mand_ie; |
| 395 | peer = peer_by_rac(TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA)); |
| 396 | if (!peer) |
| 397 | goto err_no_peer; |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 398 | rc = gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 399 | break; |
| 400 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 401 | LOGP(DGPRS, LOGL_ERROR, |
| 402 | "NSEI=%u(SGSN) INVOKE TRACE not supported\n", nsvc->nsei); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 403 | rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 404 | break; |
| 405 | default: |
| 406 | DEBUGP(DGPRS, "BSSGP PDU type 0x%02x unknown\n", pdu_type); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 407 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 408 | break; |
| 409 | } |
| 410 | |
| 411 | return rc; |
| 412 | err_mand_ie: |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 413 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n", |
| 414 | nsvc->nsei); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 415 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 416 | err_no_peer: |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 417 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAC\n", |
| 418 | nsvc->nsei); |
| 419 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /* Main input function for Gb proxy */ |
| 423 | int gbprox_rcvmsg(struct msgb *msg, struct gprs_nsvc *nsvc, uint16_t ns_bvci) |
| 424 | { |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 425 | int rc; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 426 | |
| 427 | /* Only BVCI=0 messages need special treatment */ |
| 428 | if (ns_bvci == 0 || ns_bvci == 1) { |
| 429 | if (nsvc->remote_end_is_sgsn) |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 430 | rc = gbprox_rx_sig_from_sgsn(msg, nsvc, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 431 | else |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 432 | rc = gbprox_rx_sig_from_bss(msg, nsvc, ns_bvci); |
| 433 | } else { |
| 434 | /* All other BVCI are PTP and thus can be simply forwarded */ |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 435 | if (!nsvc->remote_end_is_sgsn) { |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 436 | rc = gbprox_relay2sgsn(msg, ns_bvci); |
Harald Welte | 70f38d2 | 2010-05-01 12:10:57 +0200 | [diff] [blame] | 437 | } else { |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 438 | struct gbprox_peer *peer = peer_by_bvci(ns_bvci); |
| 439 | if (!peer) { |
| 440 | LOGP(DGPRS, LOGL_NOTICE, "Allocationg new peer for " |
| 441 | "BVCI=%u via NSVC=%u/NSEI=%u\n", ns_bvci, |
| 442 | nsvc->nsvci, nsvc->nsei); |
| 443 | peer = peer_alloc(ns_bvci); |
| 444 | peer->nsvc = nsvc; |
| 445 | } |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 446 | rc = gbprox_relay2peer(msg, peer, ns_bvci); |
Harald Welte | 70f38d2 | 2010-05-01 12:10:57 +0200 | [diff] [blame] | 447 | } |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 450 | return rc; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 451 | } |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 452 | |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 453 | /* Signal handler for signals from NS layer */ |
| 454 | int gbprox_signal(unsigned int subsys, unsigned int signal, |
| 455 | void *handler_data, void *signal_data) |
| 456 | { |
| 457 | struct ns_signal_data *nssd = signal_data; |
| 458 | struct gprs_nsvc *nsvc = nssd->nsvc; |
| 459 | struct gbprox_peer *peer; |
| 460 | |
| 461 | if (subsys != SS_NS) |
| 462 | return 0; |
| 463 | |
Harald Welte | f69c059 | 2010-05-11 18:29:44 +0200 | [diff] [blame] | 464 | if (signal == S_NS_RESET && nsvc->nsei == gbcfg.nsip_sgsn_nsei) { |
| 465 | /* We have received a NS-RESET from the NSEI and NSVC |
| 466 | * of the SGSN. This might happen with SGSN that start |
| 467 | * their own NS-RESET procedure without waiting for our |
| 468 | * NS-RESET */ |
| 469 | nsvc->remote_end_is_sgsn = 1; |
| 470 | } |
| 471 | |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 472 | /* We currently only care about signals from the SGSN */ |
| 473 | if (!nsvc->remote_end_is_sgsn) |
| 474 | return 0; |
| 475 | |
| 476 | /* iterate over all BTS peers and send the respective PDU */ |
| 477 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
| 478 | switch (signal) { |
| 479 | case S_NS_RESET: |
| 480 | gprs_ns_tx_reset(peer->nsvc, nssd->cause); |
| 481 | break; |
| 482 | case S_NS_BLOCK: |
| 483 | gprs_ns_tx_block(peer->nsvc, nssd->cause); |
| 484 | break; |
| 485 | case S_NS_UNBLOCK: |
| 486 | gprs_ns_tx_unblock(peer->nsvc); |
| 487 | break; |
| 488 | } |
| 489 | } |
| 490 | return 0; |
| 491 | } |
| 492 | |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 493 | |
| 494 | #include <vty/command.h> |
| 495 | |
| 496 | gDEFUN(show_gbproxy, show_gbproxy_cmd, "show gbproxy", |
| 497 | SHOW_STR "Display information about the Gb proxy") |
| 498 | { |
| 499 | struct gbprox_peer *peer; |
| 500 | |
| 501 | llist_for_each_entry(peer, &gbprox_bts_peers, list) { |
| 502 | struct gprs_nsvc *nsvc = peer->nsvc; |
| 503 | struct gprs_ra_id raid; |
Harald Welte | 7fc9822 | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 504 | gsm48_parse_ra(&raid, peer->ra); |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 505 | |
| 506 | vty_out(vty, "NSEI %5u, NS-VC %5u, PTP-BVCI %u, " |
| 507 | "RAC %u-%u-%u-%u%s", |
| 508 | nsvc->nsei, nsvc->nsvci, peer->bvci, |
| 509 | raid.mcc, raid.mnc, raid.lac, raid.rac, VTY_NEWLINE); |
| 510 | if (nsvc->nsi->ll == GPRS_NS_LL_UDP) |
| 511 | vty_out(vty, " remote address %s:%u%s", |
| 512 | inet_ntoa(nsvc->ip.bts_addr.sin_addr), |
| 513 | ntohs(nsvc->ip.bts_addr.sin_port), VTY_NEWLINE); |
| 514 | } |
| 515 | return CMD_SUCCESS; |
| 516 | } |