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