Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1 | /* OpenBSC Abis/IP proxy ip.access nanoBTS */ |
| 2 | |
| 3 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 85531cc | 2010-10-06 20:37:09 +0800 | [diff] [blame] | 4 | * (C) 2010 by On-Waves |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 5 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 18 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <unistd.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <errno.h> |
| 28 | #include <string.h> |
| 29 | #include <signal.h> |
| 30 | #include <time.h> |
| 31 | #include <sys/fcntl.h> |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 32 | #include <sys/socket.h> |
| 33 | #include <sys/ioctl.h> |
| 34 | #include <arpa/inet.h> |
| 35 | #include <netinet/in.h> |
| 36 | |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 37 | #define _GNU_SOURCE |
| 38 | #include <getopt.h> |
| 39 | |
Neels Hofmeyr | c016479 | 2017-09-04 15:15:32 +0200 | [diff] [blame] | 40 | #include <osmocom/bsc/gsm_data.h> |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 41 | #include <osmocom/core/application.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 42 | #include <osmocom/core/select.h> |
| 43 | #include <osmocom/gsm/tlv.h> |
| 44 | #include <osmocom/core/msgb.h> |
Harald Welte | 4a88a49 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 45 | #include <osmocom/gsm/ipa.h> |
Harald Welte | eb62301 | 2014-08-18 22:33:12 +0200 | [diff] [blame] | 46 | #include <osmocom/abis/ipa.h> |
Harald Welte | 4a88a49 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 47 | #include <osmocom/abis/ipaccess.h> |
Neels Hofmeyr | c016479 | 2017-09-04 15:15:32 +0200 | [diff] [blame] | 48 | #include <osmocom/bsc/debug.h> |
| 49 | #include <osmocom/bsc/ipaccess.h> |
Neels Hofmeyr | 978f58c | 2018-02-13 17:37:39 +0100 | [diff] [blame] | 50 | #include <osmocom/core/socket.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 51 | #include <osmocom/core/talloc.h> |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 52 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 53 | /* one instance of an ip.access protocol proxy */ |
| 54 | struct ipa_proxy { |
| 55 | /* socket where we listen for incoming OML from BTS */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 56 | struct osmo_fd oml_listen_fd; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 57 | /* socket where we listen for incoming RSL from BTS */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 58 | struct osmo_fd rsl_listen_fd; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 59 | /* list of BTS's (struct ipa_bts_conn */ |
| 60 | struct llist_head bts_list; |
| 61 | /* the BSC reconnect timer */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 62 | struct osmo_timer_list reconn_timer; |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 63 | /* global GPRS NS data */ |
| 64 | struct in_addr gprs_addr; |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 65 | struct in_addr listen_addr; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | /* global pointer to the proxy structure */ |
| 69 | static struct ipa_proxy *ipp; |
| 70 | |
| 71 | struct ipa_proxy_conn { |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 72 | struct osmo_fd fd; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 73 | struct llist_head tx_queue; |
| 74 | struct ipa_bts_conn *bts_conn; |
| 75 | }; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 76 | #define MAX_TRX 4 |
| 77 | |
| 78 | /* represents a particular BTS in our proxy */ |
| 79 | struct ipa_bts_conn { |
| 80 | /* list of BTS's (ipa_proxy->bts_list) */ |
| 81 | struct llist_head list; |
| 82 | /* back pointer to the proxy which we belong to */ |
| 83 | struct ipa_proxy *ipp; |
| 84 | /* the unit ID as determined by CCM */ |
| 85 | struct { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 86 | uint16_t site_id; |
| 87 | uint16_t bts_id; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 88 | } unit_id; |
| 89 | |
| 90 | /* incoming connections from BTS */ |
| 91 | struct ipa_proxy_conn *oml_conn; |
| 92 | struct ipa_proxy_conn *rsl_conn[MAX_TRX]; |
| 93 | |
| 94 | /* outgoing connections to BSC */ |
| 95 | struct ipa_proxy_conn *bsc_oml_conn; |
| 96 | struct ipa_proxy_conn *bsc_rsl_conn[MAX_TRX]; |
| 97 | |
| 98 | /* UDP sockets for BTS and BSC injection */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 99 | struct osmo_fd udp_bts_fd; |
| 100 | struct osmo_fd udp_bsc_fd; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 101 | |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 102 | /* NS data */ |
| 103 | struct in_addr bts_addr; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 104 | struct osmo_fd gprs_ns_fd; |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 105 | int gprs_local_port; |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 106 | uint16_t gprs_orig_port; |
| 107 | uint32_t gprs_orig_ip; |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 108 | |
Harald Welte | 36ac775 | 2011-07-16 13:38:48 +0200 | [diff] [blame] | 109 | char *id_tags[256]; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 110 | uint8_t *id_resp; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 111 | unsigned int id_resp_len; |
| 112 | }; |
| 113 | |
| 114 | enum ipp_fd_type { |
| 115 | OML_FROM_BTS = 1, |
| 116 | RSL_FROM_BTS = 2, |
| 117 | OML_TO_BSC = 3, |
| 118 | RSL_TO_BSC = 4, |
| 119 | UDP_TO_BTS = 5, |
| 120 | UDP_TO_BSC = 6, |
| 121 | }; |
| 122 | |
Neels Hofmeyr | 87592b8 | 2018-02-14 01:44:06 +0100 | [diff] [blame] | 123 | extern void *tall_bsc_ctx; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 124 | |
| 125 | static char *listen_ipaddr; |
| 126 | static char *bsc_ipaddr; |
Holger Hans Peter Freyther | 6147c5d | 2010-06-09 16:20:39 +0800 | [diff] [blame] | 127 | static char *gprs_ns_ipaddr; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 128 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 129 | static int gprs_ns_cb(struct osmo_fd *bfd, unsigned int what); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 130 | |
Holger Hans Peter Freyther | 3dac881 | 2010-06-09 14:39:22 +0800 | [diff] [blame] | 131 | #define PROXY_ALLOC_SIZE 1200 |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 132 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 133 | static struct ipa_bts_conn *find_bts_by_unitid(struct ipa_proxy *ipp, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 134 | uint16_t site_id, |
| 135 | uint16_t bts_id) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 136 | { |
| 137 | struct ipa_bts_conn *ipbc; |
| 138 | |
| 139 | llist_for_each_entry(ipbc, &ipp->bts_list, list) { |
| 140 | if (ipbc->unit_id.site_id == site_id && |
| 141 | ipbc->unit_id.bts_id == bts_id) |
| 142 | return ipbc; |
| 143 | } |
| 144 | |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | struct ipa_proxy_conn *alloc_conn(void) |
| 149 | { |
| 150 | struct ipa_proxy_conn *ipc; |
| 151 | |
| 152 | ipc = talloc_zero(tall_bsc_ctx, struct ipa_proxy_conn); |
| 153 | if (!ipc) |
| 154 | return NULL; |
| 155 | |
| 156 | INIT_LLIST_HEAD(&ipc->tx_queue); |
| 157 | |
| 158 | return ipc; |
| 159 | } |
| 160 | |
| 161 | static int store_idtags(struct ipa_bts_conn *ipbc, struct tlv_parsed *tlvp) |
| 162 | { |
| 163 | unsigned int i, len; |
| 164 | |
| 165 | for (i = 0; i <= 0xff; i++) { |
| 166 | if (!TLVP_PRESENT(tlvp, i)) |
| 167 | continue; |
| 168 | |
| 169 | len = TLVP_LEN(tlvp, i); |
| 170 | #if 0 |
| 171 | if (!ipbc->id_tags[i]) |
| 172 | ipbc->id_tags[i] = talloc_size(tall_bsc_ctx, len); |
| 173 | else |
| 174 | #endif |
Harald Welte | a16ef3d | 2009-12-23 23:35:51 +0100 | [diff] [blame] | 175 | ipbc->id_tags[i] = talloc_realloc_size(ipbc, |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 176 | ipbc->id_tags[i], len); |
| 177 | if (!ipbc->id_tags[i]) |
| 178 | return -ENOMEM; |
| 179 | |
| 180 | memset(ipbc->id_tags[i], 0, len); |
| 181 | //memcpy(ipbc->id_tags[i], TLVP_VAL(tlvp, i), len); |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | static struct ipa_proxy_conn *connect_bsc(struct sockaddr_in *sa, int priv_nr, void *data); |
| 188 | |
| 189 | #define logp_ipbc_uid(ss, lvl, ipbc, trx_id) _logp_ipbc_uid(ss, lvl, __FILE__, __LINE__, ipbc, trx_id) |
| 190 | |
| 191 | static void _logp_ipbc_uid(unsigned int ss, unsigned int lvl, char *file, int line, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 192 | struct ipa_bts_conn *ipbc, uint8_t trx_id) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 193 | { |
| 194 | if (ipbc) |
Harald Welte | dc5062b | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 195 | logp2(ss, lvl, file, line, 0, "(%u/%u/%u) ", ipbc->unit_id.site_id, |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 196 | ipbc->unit_id.bts_id, trx_id); |
| 197 | else |
Harald Welte | dc5062b | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 198 | logp2(ss, lvl, file, line, 0, "unknown "); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 201 | static int handle_udp_read(struct osmo_fd *bfd) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 202 | { |
| 203 | struct ipa_bts_conn *ipbc = bfd->data; |
| 204 | struct ipa_proxy_conn *other_conn = NULL; |
| 205 | struct msgb *msg = msgb_alloc(PROXY_ALLOC_SIZE, "Abis/IP UDP"); |
| 206 | struct ipaccess_head *hh; |
| 207 | int ret; |
| 208 | |
| 209 | /* with UDP sockets, we cannot read partial packets but have to read |
| 210 | * all of it in one go */ |
| 211 | hh = (struct ipaccess_head *) msg->data; |
| 212 | ret = recv(bfd->fd, msg->data, msg->data_len, 0); |
| 213 | if (ret < 0) { |
Harald Welte | fb33957 | 2009-12-24 13:35:18 +0100 | [diff] [blame] | 214 | if (errno != EAGAIN) |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 215 | LOGP(DLINP, LOGL_ERROR, "recv error %s\n", strerror(errno)); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 216 | msgb_free(msg); |
| 217 | return ret; |
| 218 | } |
| 219 | if (ret == 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 220 | DEBUGP(DLINP, "UDP peer disappeared, dead socket\n"); |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 221 | osmo_fd_unregister(bfd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 222 | close(bfd->fd); |
| 223 | bfd->fd = -1; |
| 224 | msgb_free(msg); |
| 225 | return -EIO; |
| 226 | } |
| 227 | if (ret < sizeof(*hh)) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 228 | DEBUGP(DLINP, "could not even read header!?!\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 229 | msgb_free(msg); |
| 230 | return -EIO; |
| 231 | } |
| 232 | msgb_put(msg, ret); |
| 233 | msg->l2h = msg->data + sizeof(*hh); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 234 | DEBUGP(DLMI, "UDP RX: %s\n", osmo_hexdump(msg->data, msg->len)); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 235 | |
| 236 | if (hh->len != msg->len - sizeof(*hh)) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 237 | DEBUGP(DLINP, "length (%u/%u) disagrees with header(%u)\n", |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 238 | msg->len, msg->len - 3, hh->len); |
| 239 | msgb_free(msg); |
| 240 | return -EIO; |
| 241 | } |
| 242 | |
| 243 | switch (bfd->priv_nr & 0xff) { |
| 244 | case UDP_TO_BTS: |
| 245 | /* injection towards BTS */ |
| 246 | switch (hh->proto) { |
| 247 | case IPAC_PROTO_RSL: |
| 248 | /* FIXME: what to do about TRX > 0 */ |
| 249 | other_conn = ipbc->rsl_conn[0]; |
| 250 | break; |
| 251 | default: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 252 | DEBUGP(DLINP, "Unknown protocol 0x%02x, sending to " |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 253 | "OML FD\n", hh->proto); |
| 254 | /* fall through */ |
| 255 | case IPAC_PROTO_IPACCESS: |
| 256 | case IPAC_PROTO_OML: |
| 257 | other_conn = ipbc->oml_conn; |
| 258 | break; |
| 259 | } |
| 260 | break; |
| 261 | case UDP_TO_BSC: |
| 262 | /* injection towards BSC */ |
| 263 | switch (hh->proto) { |
| 264 | case IPAC_PROTO_RSL: |
| 265 | /* FIXME: what to do about TRX > 0 */ |
| 266 | other_conn = ipbc->bsc_rsl_conn[0]; |
| 267 | break; |
| 268 | default: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 269 | DEBUGP(DLINP, "Unknown protocol 0x%02x, sending to " |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 270 | "OML FD\n", hh->proto); |
Holger Hans Peter Freyther | 4e5f93c | 2014-06-12 11:32:25 +0200 | [diff] [blame] | 271 | /* fall through */ |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 272 | case IPAC_PROTO_IPACCESS: |
| 273 | case IPAC_PROTO_OML: |
| 274 | other_conn = ipbc->bsc_oml_conn; |
| 275 | break; |
| 276 | } |
| 277 | break; |
| 278 | default: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 279 | DEBUGP(DLINP, "Unknown filedescriptor priv_nr=%04x\n", bfd->priv_nr); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 280 | break; |
| 281 | } |
| 282 | |
| 283 | if (other_conn) { |
| 284 | /* enqueue the message for TX on the respective FD */ |
| 285 | msgb_enqueue(&other_conn->tx_queue, msg); |
| 286 | other_conn->fd.when |= BSC_FD_WRITE; |
| 287 | } else |
| 288 | msgb_free(msg); |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 293 | static int handle_udp_write(struct osmo_fd *bfd) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 294 | { |
| 295 | /* not implemented yet */ |
| 296 | bfd->when &= ~BSC_FD_WRITE; |
| 297 | |
| 298 | return -EIO; |
| 299 | } |
| 300 | |
| 301 | /* callback from select.c in case one of the fd's can be read/written */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 302 | static int udp_fd_cb(struct osmo_fd *bfd, unsigned int what) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 303 | { |
| 304 | int rc = 0; |
| 305 | |
| 306 | if (what & BSC_FD_READ) |
| 307 | rc = handle_udp_read(bfd); |
| 308 | if (what & BSC_FD_WRITE) |
| 309 | rc = handle_udp_write(bfd); |
| 310 | |
| 311 | return rc; |
| 312 | } |
| 313 | |
| 314 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 315 | static int ipbc_alloc_connect(struct ipa_proxy_conn *ipc, struct osmo_fd *bfd, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 316 | uint16_t site_id, uint16_t bts_id, |
| 317 | uint16_t trx_id, struct tlv_parsed *tlvp, |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 318 | struct msgb *msg) |
| 319 | { |
| 320 | struct ipa_bts_conn *ipbc; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 321 | uint16_t udp_port; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 322 | int ret = 0; |
| 323 | struct sockaddr_in sin; |
| 324 | |
| 325 | memset(&sin, 0, sizeof(sin)); |
| 326 | sin.sin_family = AF_INET; |
| 327 | inet_aton(bsc_ipaddr, &sin.sin_addr); |
| 328 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 329 | DEBUGP(DLINP, "(%u/%u/%u) New BTS connection: ", |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 330 | site_id, bts_id, trx_id); |
| 331 | |
| 332 | /* OML needs to be established before RSL */ |
| 333 | if ((bfd->priv_nr & 0xff) != OML_FROM_BTS) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 334 | DEBUGPC(DLINP, "Not a OML connection ?!?\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 335 | return -EIO; |
| 336 | } |
| 337 | |
| 338 | /* allocate new BTS connection data structure */ |
| 339 | ipbc = talloc_zero(tall_bsc_ctx, struct ipa_bts_conn); |
| 340 | if (!ipbc) { |
| 341 | ret = -ENOMEM; |
| 342 | goto err_out; |
| 343 | } |
| 344 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 345 | DEBUGPC(DLINP, "Created BTS Conn data structure\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 346 | ipbc->ipp = ipp; |
| 347 | ipbc->unit_id.site_id = site_id; |
| 348 | ipbc->unit_id.bts_id = bts_id; |
| 349 | ipbc->oml_conn = ipc; |
| 350 | ipc->bts_conn = ipbc; |
| 351 | |
| 352 | /* store the content of the ID TAGS for later reference */ |
| 353 | store_idtags(ipbc, tlvp); |
| 354 | ipbc->id_resp_len = msg->len; |
| 355 | ipbc->id_resp = talloc_size(tall_bsc_ctx, ipbc->id_resp_len); |
| 356 | memcpy(ipbc->id_resp, msg->data, ipbc->id_resp_len); |
| 357 | |
| 358 | /* Create OML TCP connection towards BSC */ |
Harald Welte | 87ed5cd | 2009-12-23 22:47:53 +0100 | [diff] [blame] | 359 | sin.sin_port = htons(IPA_TCP_PORT_OML); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 360 | ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc); |
| 361 | if (!ipbc->bsc_oml_conn) { |
| 362 | ret = -EIO; |
| 363 | goto err_bsc_conn; |
| 364 | } |
| 365 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 366 | DEBUGP(DLINP, "(%u/%u/%u) OML Connected to BSC\n", |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 367 | site_id, bts_id, trx_id); |
| 368 | |
| 369 | /* Create UDP socket for BTS packet injection */ |
| 370 | udp_port = 10000 + (site_id % 1000)*100 + (bts_id % 100); |
Neels Hofmeyr | 978f58c | 2018-02-13 17:37:39 +0100 | [diff] [blame] | 371 | ipbc->udp_bts_fd.priv_nr = UDP_TO_BTS; |
| 372 | ipbc->udp_bts_fd.cb = udp_fd_cb; |
| 373 | ipbc->udp_bts_fd.data = ipbc; |
| 374 | ret = osmo_sock_init_ofd(&ipbc->udp_bts_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 375 | NULL, udp_port, OSMO_SOCK_F_BIND); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 376 | if (ret < 0) |
| 377 | goto err_udp_bts; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 378 | DEBUGP(DLINP, "(%u/%u/%u) Created UDP socket for injection " |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 379 | "towards BTS at port %u\n", site_id, bts_id, trx_id, udp_port); |
| 380 | |
| 381 | /* Create UDP socket for BSC packet injection */ |
| 382 | udp_port = 20000 + (site_id % 1000)*100 + (bts_id % 100); |
Neels Hofmeyr | 978f58c | 2018-02-13 17:37:39 +0100 | [diff] [blame] | 383 | ipbc->udp_bsc_fd.priv_nr = UDP_TO_BSC; |
| 384 | ipbc->udp_bsc_fd.cb = udp_fd_cb; |
| 385 | ipbc->udp_bsc_fd.data = ipbc; |
| 386 | ret = osmo_sock_init_ofd(&ipbc->udp_bsc_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 387 | NULL, udp_port, OSMO_SOCK_F_BIND); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 388 | if (ret < 0) |
| 389 | goto err_udp_bsc; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 390 | DEBUGP(DLINP, "(%u/%u/%u) Created UDP socket for injection " |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 391 | "towards BSC at port %u\n", site_id, bts_id, trx_id, udp_port); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 392 | |
| 393 | |
| 394 | /* GPRS NS related code */ |
| 395 | if (gprs_ns_ipaddr) { |
| 396 | struct sockaddr_in sock; |
| 397 | socklen_t len = sizeof(sock); |
Pablo Neira Ayuso | 7e73700 | 2011-04-11 16:33:17 +0200 | [diff] [blame] | 398 | |
Neels Hofmeyr | 978f58c | 2018-02-13 17:37:39 +0100 | [diff] [blame] | 399 | ipbc->gprs_ns_fd.priv_nr = 0; |
| 400 | ipbc->gprs_ns_fd.cb = gprs_ns_cb; |
| 401 | ipbc->gprs_ns_fd.data = ipbc; |
| 402 | ret = osmo_sock_init_ofd(&ipbc->gprs_ns_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 403 | listen_ipaddr, 0, OSMO_SOCK_F_BIND); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 404 | if (ret < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 405 | LOGP(DLINP, LOGL_ERROR, "Creating the GPRS socket failed.\n"); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 406 | goto err_udp_bsc; |
| 407 | } |
| 408 | |
| 409 | ret = getsockname(ipbc->gprs_ns_fd.fd, (struct sockaddr* ) &sock, &len); |
| 410 | ipbc->gprs_local_port = ntohs(sock.sin_port); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 411 | LOGP(DLINP, LOGL_NOTICE, |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 412 | "Created GPRS NS Socket. Listening on: %s:%d\n", |
| 413 | inet_ntoa(sock.sin_addr), ipbc->gprs_local_port); |
| 414 | |
| 415 | ret = getpeername(bfd->fd, (struct sockaddr* ) &sock, &len); |
| 416 | ipbc->bts_addr = sock.sin_addr; |
| 417 | } |
| 418 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 419 | llist_add(&ipbc->list, &ipp->bts_list); |
| 420 | |
| 421 | return 0; |
| 422 | |
| 423 | err_udp_bsc: |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 424 | osmo_fd_unregister(&ipbc->udp_bts_fd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 425 | err_udp_bts: |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 426 | osmo_fd_unregister(&ipbc->bsc_oml_conn->fd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 427 | close(ipbc->bsc_oml_conn->fd.fd); |
| 428 | talloc_free(ipbc->bsc_oml_conn); |
| 429 | ipbc->bsc_oml_conn = NULL; |
| 430 | err_bsc_conn: |
| 431 | talloc_free(ipbc->id_resp); |
| 432 | talloc_free(ipbc); |
| 433 | #if 0 |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 434 | osmo_fd_unregister(bfd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 435 | close(bfd->fd); |
| 436 | talloc_free(bfd); |
| 437 | #endif |
| 438 | err_out: |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | static int ipaccess_rcvmsg(struct ipa_proxy_conn *ipc, struct msgb *msg, |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 443 | struct osmo_fd *bfd) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 444 | { |
| 445 | struct tlv_parsed tlvp; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 446 | uint8_t msg_type = *(msg->l2h); |
Harald Welte | eb62301 | 2014-08-18 22:33:12 +0200 | [diff] [blame] | 447 | struct ipaccess_unit unit_data; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 448 | struct ipa_bts_conn *ipbc; |
| 449 | int ret = 0; |
| 450 | |
| 451 | switch (msg_type) { |
| 452 | case IPAC_MSGT_PING: |
Harald Welte | 4a88a49 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 453 | ret = ipa_ccm_send_pong(bfd->fd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 454 | break; |
| 455 | case IPAC_MSGT_PONG: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 456 | DEBUGP(DLMI, "PONG!\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 457 | break; |
| 458 | case IPAC_MSGT_ID_RESP: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 459 | DEBUGP(DLMI, "ID_RESP "); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 460 | /* parse tags, search for Unit ID */ |
Harald Welte | b0177b2 | 2018-10-21 12:18:14 +0200 | [diff] [blame] | 461 | ret = ipa_ccm_id_resp_parse(&tlvp, (uint8_t *)msg->l2h+1, msgb_l2len(msg)-1); |
| 462 | if (ret < 0) { |
| 463 | LOGP(DLINP, LOGL_ERROR, "Error parsing CCM ID RESP !?!\n"); |
| 464 | return -EIO; |
| 465 | } |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 466 | DEBUGP(DLMI, "\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 467 | |
| 468 | if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 469 | LOGP(DLINP, LOGL_ERROR, "No Unit ID in ID RESPONSE !?!\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 470 | return -EIO; |
| 471 | } |
| 472 | |
| 473 | /* lookup BTS, create sign_link, ... */ |
Harald Welte | eb62301 | 2014-08-18 22:33:12 +0200 | [diff] [blame] | 474 | memset(&unit_data, 0, sizeof(unit_data)); |
Harald Welte | 4a88a49 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 475 | ipa_parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT), |
Harald Welte | eb62301 | 2014-08-18 22:33:12 +0200 | [diff] [blame] | 476 | &unit_data); |
| 477 | ipbc = find_bts_by_unitid(ipp, unit_data.site_id, unit_data.bts_id); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 478 | if (!ipbc) { |
| 479 | /* We have not found an ipbc (per-bts proxy instance) |
| 480 | * for this BTS yet. The first connection of a new BTS must |
| 481 | * be a OML connection. We allocate the associated data structures, |
| 482 | * and try to connect to the remote end */ |
| 483 | |
Harald Welte | eb62301 | 2014-08-18 22:33:12 +0200 | [diff] [blame] | 484 | return ipbc_alloc_connect(ipc, bfd, unit_data.site_id, |
| 485 | unit_data.bts_id, |
| 486 | unit_data.trx_id, &tlvp, msg); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 487 | /* if this fails, the caller will clean up bfd */ |
| 488 | } else { |
| 489 | struct sockaddr_in sin; |
| 490 | memset(&sin, 0, sizeof(sin)); |
| 491 | sin.sin_family = AF_INET; |
| 492 | inet_aton(bsc_ipaddr, &sin.sin_addr); |
| 493 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 494 | DEBUGP(DLINP, "Identified BTS %u/%u/%u\n", |
Harald Welte | eb62301 | 2014-08-18 22:33:12 +0200 | [diff] [blame] | 495 | unit_data.site_id, unit_data.bts_id, unit_data.trx_id); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 496 | |
| 497 | if ((bfd->priv_nr & 0xff) != RSL_FROM_BTS) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 498 | LOGP(DLINP, LOGL_ERROR, "Second OML connection from " |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 499 | "same BTS ?!?\n"); |
| 500 | return 0; |
| 501 | } |
| 502 | |
Harald Welte | eb62301 | 2014-08-18 22:33:12 +0200 | [diff] [blame] | 503 | if (unit_data.trx_id >= MAX_TRX) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 504 | LOGP(DLINP, LOGL_ERROR, "We don't support more " |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 505 | "than %u TRX\n", MAX_TRX); |
| 506 | return -EINVAL; |
| 507 | } |
| 508 | |
| 509 | ipc->bts_conn = ipbc; |
| 510 | /* store TRX number in higher 8 bit of the bfd private number */ |
Harald Welte | eb62301 | 2014-08-18 22:33:12 +0200 | [diff] [blame] | 511 | bfd->priv_nr |= unit_data.trx_id << 8; |
| 512 | ipbc->rsl_conn[unit_data.trx_id] = ipc; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 513 | |
| 514 | /* Create RSL TCP connection towards BSC */ |
Harald Welte | 87ed5cd | 2009-12-23 22:47:53 +0100 | [diff] [blame] | 515 | sin.sin_port = htons(IPA_TCP_PORT_RSL); |
Harald Welte | eb62301 | 2014-08-18 22:33:12 +0200 | [diff] [blame] | 516 | ipbc->bsc_rsl_conn[unit_data.trx_id] = |
| 517 | connect_bsc(&sin, RSL_TO_BSC | (unit_data.trx_id << 8), ipbc); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 518 | if (!ipbc->bsc_oml_conn) |
| 519 | return -EIO; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 520 | DEBUGP(DLINP, "(%u/%u/%u) Connected RSL to BSC\n", |
Harald Welte | eb62301 | 2014-08-18 22:33:12 +0200 | [diff] [blame] | 521 | unit_data.site_id, unit_data.bts_id, unit_data.trx_id); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 522 | } |
| 523 | break; |
| 524 | case IPAC_MSGT_ID_GET: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 525 | DEBUGP(DLMI, "ID_GET\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 526 | if ((bfd->priv_nr & 0xff) != OML_TO_BSC && |
| 527 | (bfd->priv_nr & 0xff) != RSL_TO_BSC) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 528 | DEBUGP(DLINP, "IDentity REQuest from BTS ?!?\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 529 | return -EIO; |
| 530 | } |
| 531 | ipbc = ipc->bts_conn; |
| 532 | if (!ipbc) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 533 | DEBUGP(DLINP, "ID_GET from BSC before we have ID_RESP from BTS\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 534 | return -EIO; |
| 535 | } |
| 536 | ret = write(bfd->fd, ipbc->id_resp, ipbc->id_resp_len); |
Neels Hofmeyr | 127fc93 | 2016-02-24 19:29:59 +0100 | [diff] [blame] | 537 | if (ret != ipbc->id_resp_len) { |
| 538 | LOGP(DLINP, LOGL_ERROR, "Partial write: %d of %d\n", |
| 539 | ret, ipbc->id_resp_len); |
| 540 | return -EIO; |
| 541 | } |
| 542 | ret = 0; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 543 | break; |
| 544 | case IPAC_MSGT_ID_ACK: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 545 | DEBUGP(DLMI, "ID_ACK? -> ACK!\n"); |
Harald Welte | 4a88a49 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 546 | ret = ipa_ccm_send_id_ack(bfd->fd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 547 | break; |
Holger Hans Peter Freyther | 0897dad | 2010-06-09 17:38:59 +0800 | [diff] [blame] | 548 | default: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 549 | LOGP(DLMI, LOGL_ERROR, "Unhandled IPA type; %d\n", msg_type); |
Holger Hans Peter Freyther | 0897dad | 2010-06-09 17:38:59 +0800 | [diff] [blame] | 550 | return 1; |
| 551 | break; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 552 | } |
Neels Hofmeyr | 127fc93 | 2016-02-24 19:29:59 +0100 | [diff] [blame] | 553 | return ret; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 554 | } |
| 555 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 556 | struct msgb *ipaccess_proxy_read_msg(struct osmo_fd *bfd, int *error) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 557 | { |
| 558 | struct msgb *msg = msgb_alloc(PROXY_ALLOC_SIZE, "Abis/IP"); |
| 559 | struct ipaccess_head *hh; |
| 560 | int len, ret = 0; |
| 561 | |
| 562 | if (!msg) { |
| 563 | *error = -ENOMEM; |
| 564 | return NULL; |
| 565 | } |
| 566 | |
| 567 | /* first read our 3-byte header */ |
| 568 | hh = (struct ipaccess_head *) msg->data; |
| 569 | ret = recv(bfd->fd, msg->data, 3, 0); |
| 570 | if (ret < 0) { |
Harald Welte | fb33957 | 2009-12-24 13:35:18 +0100 | [diff] [blame] | 571 | if (errno != EAGAIN) |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 572 | LOGP(DLINP, LOGL_ERROR, "recv error: %s\n", strerror(errno)); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 573 | msgb_free(msg); |
| 574 | *error = ret; |
| 575 | return NULL; |
| 576 | } else if (ret == 0) { |
| 577 | msgb_free(msg); |
| 578 | *error = ret; |
| 579 | return NULL; |
| 580 | } |
| 581 | |
| 582 | msgb_put(msg, ret); |
| 583 | |
Martin Hauke | a29affd | 2019-11-13 22:10:41 +0100 | [diff] [blame] | 584 | /* then read the length as specified in header */ |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 585 | msg->l2h = msg->data + sizeof(*hh); |
| 586 | len = ntohs(hh->len); |
| 587 | ret = recv(bfd->fd, msg->l2h, len, 0); |
| 588 | if (ret < len) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 589 | LOGP(DLINP, LOGL_ERROR, "short read!\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 590 | msgb_free(msg); |
| 591 | *error = -EIO; |
| 592 | return NULL; |
| 593 | } |
| 594 | msgb_put(msg, ret); |
| 595 | |
| 596 | return msg; |
| 597 | } |
| 598 | |
| 599 | static struct ipa_proxy_conn *ipc_by_priv_nr(struct ipa_bts_conn *ipbc, |
| 600 | unsigned int priv_nr) |
| 601 | { |
| 602 | struct ipa_proxy_conn *bsc_conn; |
| 603 | unsigned int trx_id = priv_nr >> 8; |
| 604 | |
| 605 | switch (priv_nr & 0xff) { |
| 606 | case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */ |
| 607 | bsc_conn = ipbc->bsc_oml_conn; |
| 608 | break; |
| 609 | case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */ |
| 610 | bsc_conn = ipbc->bsc_rsl_conn[trx_id]; |
| 611 | break; |
| 612 | case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */ |
| 613 | bsc_conn = ipbc->oml_conn; |
| 614 | break; |
| 615 | case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */ |
| 616 | bsc_conn = ipbc->rsl_conn[trx_id]; |
| 617 | break; |
| 618 | default: |
| 619 | bsc_conn = NULL; |
| 620 | break; |
| 621 | } |
| 622 | return bsc_conn; |
| 623 | } |
| 624 | |
| 625 | static void reconn_tmr_cb(void *data) |
| 626 | { |
| 627 | struct ipa_proxy *ipp = data; |
| 628 | struct ipa_bts_conn *ipbc; |
| 629 | struct sockaddr_in sin; |
| 630 | int i; |
| 631 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 632 | DEBUGP(DLINP, "Running reconnect timer\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 633 | |
| 634 | memset(&sin, 0, sizeof(sin)); |
| 635 | sin.sin_family = AF_INET; |
| 636 | inet_aton(bsc_ipaddr, &sin.sin_addr); |
| 637 | |
| 638 | llist_for_each_entry(ipbc, &ipp->bts_list, list) { |
| 639 | /* if OML to BSC is dead, try to restore it */ |
| 640 | if (ipbc->oml_conn && !ipbc->bsc_oml_conn) { |
Harald Welte | 87ed5cd | 2009-12-23 22:47:53 +0100 | [diff] [blame] | 641 | sin.sin_port = htons(IPA_TCP_PORT_OML); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 642 | logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, 0); |
| 643 | LOGPC(DLINP, LOGL_NOTICE, "OML Trying to reconnect\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 644 | ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc); |
| 645 | if (!ipbc->bsc_oml_conn) |
| 646 | goto reschedule; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 647 | logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, 0); |
| 648 | LOGPC(DLINP, LOGL_NOTICE, "OML Reconnected\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 649 | } |
| 650 | /* if we (still) don't have a OML connection, skip RSL */ |
| 651 | if (!ipbc->oml_conn || !ipbc->bsc_oml_conn) |
| 652 | continue; |
| 653 | |
| 654 | for (i = 0; i < ARRAY_SIZE(ipbc->rsl_conn); i++) { |
| 655 | unsigned int priv_nr; |
| 656 | /* don't establish RSL links which we don't have */ |
| 657 | if (!ipbc->rsl_conn[i]) |
| 658 | continue; |
| 659 | if (ipbc->bsc_rsl_conn[i]) |
| 660 | continue; |
| 661 | priv_nr = ipbc->rsl_conn[i]->fd.priv_nr; |
| 662 | priv_nr &= ~0xff; |
| 663 | priv_nr |= RSL_TO_BSC; |
Harald Welte | 87ed5cd | 2009-12-23 22:47:53 +0100 | [diff] [blame] | 664 | sin.sin_port = htons(IPA_TCP_PORT_RSL); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 665 | logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, priv_nr >> 8); |
| 666 | LOGPC(DLINP, LOGL_NOTICE, "RSL Trying to reconnect\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 667 | ipbc->bsc_rsl_conn[i] = connect_bsc(&sin, priv_nr, ipbc); |
Holger Hans Peter Freyther | c9251fa | 2013-07-14 08:47:06 +0200 | [diff] [blame] | 668 | if (!ipbc->bsc_rsl_conn[i]) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 669 | goto reschedule; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 670 | logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, priv_nr >> 8); |
| 671 | LOGPC(DLINP, LOGL_NOTICE, "RSL Reconnected\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | return; |
| 675 | |
| 676 | reschedule: |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 677 | osmo_timer_schedule(&ipp->reconn_timer, 5, 0); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 678 | } |
| 679 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 680 | static void handle_dead_socket(struct osmo_fd *bfd) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 681 | { |
| 682 | struct ipa_proxy_conn *ipc = bfd->data; /* local conn */ |
| 683 | struct ipa_proxy_conn *bsc_conn; /* remote conn */ |
| 684 | struct ipa_bts_conn *ipbc = ipc->bts_conn; |
| 685 | unsigned int trx_id = bfd->priv_nr >> 8; |
| 686 | struct msgb *msg, *msg2; |
| 687 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 688 | osmo_fd_unregister(bfd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 689 | close(bfd->fd); |
| 690 | bfd->fd = -1; |
| 691 | |
| 692 | /* FIXME: clear tx_queue, remove all references, etc. */ |
| 693 | llist_for_each_entry_safe(msg, msg2, &ipc->tx_queue, list) |
| 694 | msgb_free(msg); |
| 695 | |
| 696 | switch (bfd->priv_nr & 0xff) { |
| 697 | case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */ |
Pablo Neira Ayuso | 3c409c2 | 2011-03-27 21:31:48 +0200 | [diff] [blame] | 698 | /* The BTS started a connection with us but we got no |
| 699 | * IPAC_MSGT_ID_RESP message yet, in that scenario we did not |
| 700 | * allocate the ipa_bts_conn structure. */ |
| 701 | if (ipbc == NULL) |
| 702 | break; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 703 | ipbc->oml_conn = NULL; |
| 704 | bsc_conn = ipbc->bsc_oml_conn; |
| 705 | /* close the connection to the BSC */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 706 | osmo_fd_unregister(&bsc_conn->fd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 707 | close(bsc_conn->fd.fd); |
| 708 | llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list) |
| 709 | msgb_free(msg); |
| 710 | talloc_free(bsc_conn); |
| 711 | ipbc->bsc_oml_conn = NULL; |
| 712 | /* FIXME: do we need to delete the entire ipbc ? */ |
| 713 | break; |
| 714 | case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */ |
| 715 | ipbc->rsl_conn[trx_id] = NULL; |
| 716 | bsc_conn = ipbc->bsc_rsl_conn[trx_id]; |
| 717 | /* close the connection to the BSC */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 718 | osmo_fd_unregister(&bsc_conn->fd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 719 | close(bsc_conn->fd.fd); |
| 720 | llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list) |
| 721 | msgb_free(msg); |
| 722 | talloc_free(bsc_conn); |
| 723 | ipbc->bsc_rsl_conn[trx_id] = NULL; |
| 724 | break; |
| 725 | case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */ |
| 726 | ipbc->bsc_oml_conn = NULL; |
| 727 | bsc_conn = ipbc->oml_conn; |
| 728 | /* start reconnect timer */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 729 | osmo_timer_schedule(&ipp->reconn_timer, 5, 0); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 730 | break; |
| 731 | case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */ |
| 732 | ipbc->bsc_rsl_conn[trx_id] = NULL; |
| 733 | bsc_conn = ipbc->rsl_conn[trx_id]; |
| 734 | /* start reconnect timer */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 735 | osmo_timer_schedule(&ipp->reconn_timer, 5, 0); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 736 | break; |
| 737 | default: |
| 738 | bsc_conn = NULL; |
| 739 | break; |
| 740 | } |
| 741 | |
| 742 | talloc_free(ipc); |
| 743 | } |
| 744 | |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 745 | static void patch_gprs_msg(struct ipa_bts_conn *ipbc, int priv_nr, struct msgb *msg) |
| 746 | { |
| 747 | uint8_t *nsvci; |
| 748 | |
| 749 | if ((priv_nr & 0xff) != OML_FROM_BTS && (priv_nr & 0xff) != OML_TO_BSC) |
| 750 | return; |
| 751 | |
| 752 | if (msgb_l2len(msg) != 39) |
| 753 | return; |
| 754 | |
| 755 | /* |
| 756 | * Check if this is a IPA Set Attribute or IPA Set Attribute ACK |
| 757 | * and if the FOM Class is GPRS NSVC0 and then we will patch it. |
| 758 | * |
| 759 | * The patch assumes the message looks like the one from the trace |
| 760 | * but we only match messages with a specific size anyway... So |
| 761 | * this hack should work just fine. |
| 762 | */ |
| 763 | |
| 764 | if (msg->l2h[0] == 0x10 && msg->l2h[1] == 0x80 && |
| 765 | msg->l2h[2] == 0x00 && msg->l2h[3] == 0x15 && |
| 766 | msg->l2h[18] == 0xf5 && msg->l2h[19] == 0xf2) { |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 767 | nsvci = &msg->l2h[23]; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 768 | ipbc->gprs_orig_port = *(uint16_t *)(nsvci+8); |
| 769 | ipbc->gprs_orig_ip = *(uint32_t *)(nsvci+10); |
| 770 | *(uint16_t *)(nsvci+8) = htons(ipbc->gprs_local_port); |
| 771 | *(uint32_t *)(nsvci+10) = ipbc->ipp->listen_addr.s_addr; |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 772 | } else if (msg->l2h[0] == 0x10 && msg->l2h[1] == 0x80 && |
| 773 | msg->l2h[2] == 0x00 && msg->l2h[3] == 0x15 && |
| 774 | msg->l2h[18] == 0xf6 && msg->l2h[19] == 0xf2) { |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 775 | nsvci = &msg->l2h[23]; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 776 | *(uint16_t *)(nsvci+8) = ipbc->gprs_orig_port; |
| 777 | *(uint32_t *)(nsvci+10) = ipbc->gprs_orig_ip; |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 781 | static int handle_tcp_read(struct osmo_fd *bfd) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 782 | { |
| 783 | struct ipa_proxy_conn *ipc = bfd->data; |
| 784 | struct ipa_bts_conn *ipbc = ipc->bts_conn; |
| 785 | struct ipa_proxy_conn *bsc_conn; |
Harald Welte | a16ef3d | 2009-12-23 23:35:51 +0100 | [diff] [blame] | 786 | struct msgb *msg; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 787 | struct ipaccess_head *hh; |
| 788 | int ret = 0; |
| 789 | char *btsbsc; |
| 790 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 791 | if ((bfd->priv_nr & 0xff) <= 2) |
| 792 | btsbsc = "BTS"; |
| 793 | else |
| 794 | btsbsc = "BSC"; |
| 795 | |
Pablo Neira Ayuso | 22f58a9 | 2011-04-07 14:15:06 +0200 | [diff] [blame] | 796 | msg = ipaccess_proxy_read_msg(bfd, &ret); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 797 | if (!msg) { |
| 798 | if (ret == 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 799 | logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8); |
| 800 | LOGPC(DLINP, LOGL_NOTICE, "%s disappeared, " |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 801 | "dead socket\n", btsbsc); |
| 802 | handle_dead_socket(bfd); |
| 803 | } |
| 804 | return ret; |
| 805 | } |
| 806 | |
| 807 | msgb_put(msg, ret); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 808 | logp_ipbc_uid(DLMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8); |
| 809 | DEBUGPC(DLMI, "RX<-%s: %s\n", btsbsc, osmo_hexdump(msg->data, msg->len)); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 810 | |
| 811 | hh = (struct ipaccess_head *) msg->data; |
| 812 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 813 | ret = ipaccess_rcvmsg(ipc, msg, bfd); |
| 814 | if (ret < 0) { |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 815 | osmo_fd_unregister(bfd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 816 | close(bfd->fd); |
| 817 | bfd->fd = -1; |
| 818 | talloc_free(bfd); |
Holger Hans Peter Freyther | 0897dad | 2010-06-09 17:38:59 +0800 | [diff] [blame] | 819 | msgb_free(msg); |
| 820 | return ret; |
| 821 | } else if (ret == 0) { |
| 822 | /* we do not forward parts of the CCM protocol |
| 823 | * through the proxy but rather terminate it ourselves. */ |
| 824 | msgb_free(msg); |
| 825 | return ret; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 826 | } |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | if (!ipbc) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 830 | LOGP(DLINP, LOGL_ERROR, |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 831 | "received %s packet but no ipc->bts_conn?!?\n", btsbsc); |
| 832 | msgb_free(msg); |
| 833 | return -EIO; |
| 834 | } |
| 835 | |
| 836 | bsc_conn = ipc_by_priv_nr(ipbc, bfd->priv_nr); |
| 837 | if (bsc_conn) { |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 838 | if (gprs_ns_ipaddr) |
| 839 | patch_gprs_msg(ipbc, bfd->priv_nr, msg); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 840 | /* enqueue packet towards BSC */ |
| 841 | msgb_enqueue(&bsc_conn->tx_queue, msg); |
| 842 | /* mark respective filedescriptor as 'we want to write' */ |
| 843 | bsc_conn->fd.when |= BSC_FD_WRITE; |
| 844 | } else { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 845 | logp_ipbc_uid(DLINP, LOGL_INFO, ipbc, bfd->priv_nr >> 8); |
| 846 | LOGPC(DLINP, LOGL_INFO, "Dropping packet from %s, " |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 847 | "since remote connection is dead\n", btsbsc); |
| 848 | msgb_free(msg); |
| 849 | } |
| 850 | |
| 851 | return ret; |
| 852 | } |
| 853 | |
| 854 | /* a TCP socket is ready to be written to */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 855 | static int handle_tcp_write(struct osmo_fd *bfd) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 856 | { |
| 857 | struct ipa_proxy_conn *ipc = bfd->data; |
| 858 | struct ipa_bts_conn *ipbc = ipc->bts_conn; |
| 859 | struct llist_head *lh; |
| 860 | struct msgb *msg; |
| 861 | char *btsbsc; |
| 862 | int ret; |
| 863 | |
| 864 | if ((bfd->priv_nr & 0xff) <= 2) |
| 865 | btsbsc = "BTS"; |
| 866 | else |
| 867 | btsbsc = "BSC"; |
| 868 | |
| 869 | |
| 870 | /* get the next msg for this timeslot */ |
| 871 | if (llist_empty(&ipc->tx_queue)) { |
| 872 | bfd->when &= ~BSC_FD_WRITE; |
| 873 | return 0; |
| 874 | } |
| 875 | lh = ipc->tx_queue.next; |
| 876 | llist_del(lh); |
| 877 | msg = llist_entry(lh, struct msgb, list); |
| 878 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 879 | logp_ipbc_uid(DLMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8); |
| 880 | DEBUGPC(DLMI, "TX %04x: %s\n", bfd->priv_nr, |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 881 | osmo_hexdump(msg->data, msg->len)); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 882 | |
| 883 | ret = send(bfd->fd, msg->data, msg->len, 0); |
| 884 | msgb_free(msg); |
| 885 | |
| 886 | if (ret == 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 887 | logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8); |
| 888 | LOGP(DLINP, LOGL_NOTICE, "%s disappeared, dead socket\n", btsbsc); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 889 | handle_dead_socket(bfd); |
| 890 | } |
| 891 | |
| 892 | return ret; |
| 893 | } |
| 894 | |
| 895 | /* callback from select.c in case one of the fd's can be read/written */ |
Harald Welte | eb62301 | 2014-08-18 22:33:12 +0200 | [diff] [blame] | 896 | static int proxy_ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 897 | { |
| 898 | int rc = 0; |
| 899 | |
| 900 | if (what & BSC_FD_READ) { |
| 901 | rc = handle_tcp_read(bfd); |
| 902 | if (rc < 0) |
| 903 | return rc; |
| 904 | } |
| 905 | if (what & BSC_FD_WRITE) |
| 906 | rc = handle_tcp_write(bfd); |
| 907 | |
| 908 | return rc; |
| 909 | } |
| 910 | |
| 911 | /* callback of the listening filedescriptor */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 912 | static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 913 | { |
| 914 | int ret; |
| 915 | struct ipa_proxy_conn *ipc; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 916 | struct osmo_fd *bfd; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 917 | struct sockaddr_in sa; |
| 918 | socklen_t sa_len = sizeof(sa); |
| 919 | |
| 920 | if (!(what & BSC_FD_READ)) |
| 921 | return 0; |
| 922 | |
| 923 | ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 924 | if (ret < 0) { |
| 925 | perror("accept"); |
| 926 | return ret; |
| 927 | } |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 928 | DEBUGP(DLINP, "accept()ed new %s link from %s\n", |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 929 | (listen_bfd->priv_nr & 0xff) == OML_FROM_BTS ? "OML" : "RSL", |
| 930 | inet_ntoa(sa.sin_addr)); |
| 931 | |
| 932 | ipc = alloc_conn(); |
| 933 | if (!ipc) { |
| 934 | close(ret); |
| 935 | return -ENOMEM; |
| 936 | } |
| 937 | |
| 938 | bfd = &ipc->fd; |
| 939 | bfd->fd = ret; |
| 940 | bfd->data = ipc; |
| 941 | bfd->priv_nr = listen_bfd->priv_nr; |
Harald Welte | eb62301 | 2014-08-18 22:33:12 +0200 | [diff] [blame] | 942 | bfd->cb = proxy_ipaccess_fd_cb; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 943 | bfd->when = BSC_FD_READ; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 944 | ret = osmo_fd_register(bfd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 945 | if (ret < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 946 | LOGP(DLINP, LOGL_ERROR, "could not register FD\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 947 | close(bfd->fd); |
| 948 | talloc_free(ipc); |
| 949 | return ret; |
| 950 | } |
| 951 | |
| 952 | /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */ |
Harald Welte | 4a88a49 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 953 | ret = ipa_ccm_send_id_req(bfd->fd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 954 | |
| 955 | return 0; |
| 956 | } |
| 957 | |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 958 | static void send_ns(int fd, const char *buf, int size, struct in_addr ip, int port) |
| 959 | { |
| 960 | int ret; |
| 961 | struct sockaddr_in addr; |
| 962 | socklen_t len = sizeof(addr); |
| 963 | memset(&addr, 0, sizeof(addr)); |
| 964 | |
| 965 | addr.sin_family = AF_INET; |
Holger Hans Peter Freyther | 21e1c0d | 2010-06-10 15:56:26 +0800 | [diff] [blame] | 966 | addr.sin_port = htons(port); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 967 | addr.sin_addr = ip; |
| 968 | |
| 969 | ret = sendto(fd, buf, size, 0, (struct sockaddr *) &addr, len); |
| 970 | if (ret < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 971 | LOGP(DLINP, LOGL_ERROR, "Failed to forward GPRS message.\n"); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 975 | static int gprs_ns_cb(struct osmo_fd *bfd, unsigned int what) |
Holger Hans Peter Freyther | 6147c5d | 2010-06-09 16:20:39 +0800 | [diff] [blame] | 976 | { |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 977 | struct ipa_bts_conn *bts; |
| 978 | char buf[4096]; |
| 979 | int ret; |
| 980 | struct sockaddr_in sock; |
| 981 | socklen_t len = sizeof(sock); |
| 982 | |
| 983 | /* 1. get the data... */ |
| 984 | ret = recvfrom(bfd->fd, buf, sizeof(buf), 0, (struct sockaddr *) &sock, &len); |
| 985 | if (ret < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 986 | LOGP(DLINP, LOGL_ERROR, "Failed to recv GPRS NS msg: %s.\n", strerror(errno)); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 987 | return -1; |
| 988 | } |
| 989 | |
| 990 | bts = bfd->data; |
| 991 | |
| 992 | /* 2. figure out where to send it to */ |
| 993 | if (memcmp(&sock.sin_addr, &ipp->gprs_addr, sizeof(sock.sin_addr)) == 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 994 | LOGP(DLINP, LOGL_DEBUG, "GPRS NS msg from network.\n"); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 995 | send_ns(bfd->fd, buf, ret, bts->bts_addr, 23000); |
| 996 | } else if (memcmp(&sock.sin_addr, &bts->bts_addr, sizeof(sock.sin_addr)) == 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 997 | LOGP(DLINP, LOGL_DEBUG, "GPRS NS msg from BTS.\n"); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 998 | send_ns(bfd->fd, buf, ret, ipp->gprs_addr, 23000); |
Holger Hans Peter Freyther | 21e1c0d | 2010-06-10 15:56:26 +0800 | [diff] [blame] | 999 | } else { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 1000 | LOGP(DLINP, LOGL_ERROR, "Unknown GPRS source: %s\n", inet_ntoa(sock.sin_addr)); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 1001 | } |
| 1002 | |
Holger Hans Peter Freyther | 6147c5d | 2010-06-09 16:20:39 +0800 | [diff] [blame] | 1003 | return 0; |
| 1004 | } |
| 1005 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1006 | /* Actively connect to a BSC. */ |
| 1007 | static struct ipa_proxy_conn *connect_bsc(struct sockaddr_in *sa, int priv_nr, void *data) |
| 1008 | { |
| 1009 | struct ipa_proxy_conn *ipc; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1010 | struct osmo_fd *bfd; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1011 | int ret, on = 1; |
| 1012 | |
| 1013 | ipc = alloc_conn(); |
| 1014 | if (!ipc) |
| 1015 | return NULL; |
| 1016 | |
| 1017 | ipc->bts_conn = data; |
| 1018 | |
| 1019 | bfd = &ipc->fd; |
| 1020 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 1021 | bfd->cb = ipaccess_fd_cb; |
| 1022 | bfd->when = BSC_FD_READ | BSC_FD_WRITE; |
| 1023 | bfd->data = ipc; |
| 1024 | bfd->priv_nr = priv_nr; |
| 1025 | |
Holger Hans Peter Freyther | e18209c | 2013-12-12 16:16:35 +0100 | [diff] [blame] | 1026 | if (bfd->fd < 0) { |
| 1027 | LOGP(DLINP, LOGL_ERROR, "Could not create socket: %s\n", |
| 1028 | strerror(errno)); |
| 1029 | talloc_free(ipc); |
| 1030 | return NULL; |
| 1031 | } |
| 1032 | |
Harald Welte | 3c165d0 | 2016-11-26 14:09:34 +0100 | [diff] [blame] | 1033 | ret = setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 1034 | if (ret < 0) { |
| 1035 | LOGP(DLINP, LOGL_ERROR, "Could not set socket option\n"); |
| 1036 | close(bfd->fd); |
| 1037 | talloc_free(ipc); |
| 1038 | return NULL; |
| 1039 | } |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1040 | |
| 1041 | ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa)); |
| 1042 | if (ret < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 1043 | LOGP(DLINP, LOGL_ERROR, "Could not connect socket: %s\n", |
Holger Hans Peter Freyther | 6cb9b23 | 2010-06-09 15:15:11 +0800 | [diff] [blame] | 1044 | inet_ntoa(sa->sin_addr)); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1045 | close(bfd->fd); |
| 1046 | talloc_free(ipc); |
| 1047 | return NULL; |
| 1048 | } |
| 1049 | |
| 1050 | /* pre-fill tx_queue with identity request */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1051 | ret = osmo_fd_register(bfd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1052 | if (ret < 0) { |
| 1053 | close(bfd->fd); |
| 1054 | talloc_free(ipc); |
| 1055 | return NULL; |
| 1056 | } |
| 1057 | |
| 1058 | return ipc; |
| 1059 | } |
| 1060 | |
| 1061 | static int ipaccess_proxy_setup(void) |
| 1062 | { |
| 1063 | int ret; |
| 1064 | |
| 1065 | ipp = talloc_zero(tall_bsc_ctx, struct ipa_proxy); |
| 1066 | if (!ipp) |
| 1067 | return -ENOMEM; |
| 1068 | INIT_LLIST_HEAD(&ipp->bts_list); |
Pablo Neira Ayuso | 5121576 | 2017-05-08 20:57:52 +0200 | [diff] [blame] | 1069 | osmo_timer_setup(&ipp->reconn_timer, reconn_tmr_cb, ipp); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1070 | |
| 1071 | /* Listen for OML connections */ |
Neels Hofmeyr | 978f58c | 2018-02-13 17:37:39 +0100 | [diff] [blame] | 1072 | ipp->oml_listen_fd.priv_nr = OML_FROM_BTS; |
| 1073 | ipp->oml_listen_fd.cb = listen_fd_cb; |
| 1074 | ret = osmo_sock_init_ofd(&ipp->oml_listen_fd, AF_INET, SOCK_STREAM, IPPROTO_TCP, |
| 1075 | NULL, IPA_TCP_PORT_OML, OSMO_SOCK_F_BIND); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1076 | if (ret < 0) |
| 1077 | return ret; |
| 1078 | |
| 1079 | /* Listen for RSL connections */ |
Neels Hofmeyr | 978f58c | 2018-02-13 17:37:39 +0100 | [diff] [blame] | 1080 | ipp->rsl_listen_fd.priv_nr = RSL_FROM_BTS; |
| 1081 | ipp->rsl_listen_fd.cb = listen_fd_cb; |
| 1082 | ret = osmo_sock_init_ofd(&ipp->rsl_listen_fd, AF_INET, SOCK_STREAM, IPPROTO_TCP, |
| 1083 | NULL, IPA_TCP_PORT_RSL, OSMO_SOCK_F_BIND); |
Holger Hans Peter Freyther | 6147c5d | 2010-06-09 16:20:39 +0800 | [diff] [blame] | 1084 | if (ret < 0) |
| 1085 | return ret; |
| 1086 | |
| 1087 | /* Connect the GPRS NS Socket */ |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 1088 | if (gprs_ns_ipaddr) { |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 1089 | inet_aton(gprs_ns_ipaddr, &ipp->gprs_addr); |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 1090 | inet_aton(listen_ipaddr, &ipp->listen_addr); |
| 1091 | } |
Holger Hans Peter Freyther | 6147c5d | 2010-06-09 16:20:39 +0800 | [diff] [blame] | 1092 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1093 | return ret; |
| 1094 | } |
| 1095 | |
| 1096 | static void signal_handler(int signal) |
| 1097 | { |
| 1098 | fprintf(stdout, "signal %u received\n", signal); |
| 1099 | |
| 1100 | switch (signal) { |
| 1101 | case SIGABRT: |
| 1102 | /* in case of abort, we want to obtain a talloc report |
| 1103 | * and then return to the caller, who will abort the process */ |
| 1104 | case SIGUSR1: |
| 1105 | talloc_report_full(tall_bsc_ctx, stderr); |
| 1106 | break; |
| 1107 | default: |
| 1108 | break; |
| 1109 | } |
| 1110 | } |
| 1111 | |
Harald Welte | d4ab13b | 2011-07-16 13:39:44 +0200 | [diff] [blame] | 1112 | static void print_help(void) |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1113 | { |
| 1114 | printf(" ipaccess-proxy is a proxy BTS.\n"); |
| 1115 | printf(" -h --help. This help text.\n"); |
| 1116 | printf(" -l --listen IP. The ip to listen to.\n"); |
Holger Hans Peter Freyther | 6147c5d | 2010-06-09 16:20:39 +0800 | [diff] [blame] | 1117 | printf(" -b --bsc IP. The BSC IP address.\n"); |
| 1118 | printf(" -g --gprs IP. Take GPRS NS from that IP.\n"); |
| 1119 | printf("\n"); |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1120 | printf(" -s --disable-color. Disable the color inside the logging message.\n"); |
| 1121 | printf(" -e --log-level number. Set the global loglevel.\n"); |
| 1122 | printf(" -T --timestamp. Prefix every log message with a timestamp.\n"); |
| 1123 | printf(" -V --version. Print the version of OpenBSC.\n"); |
| 1124 | } |
| 1125 | |
Harald Welte | d4ab13b | 2011-07-16 13:39:44 +0200 | [diff] [blame] | 1126 | static void print_usage(void) |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1127 | { |
Pablo Neira Ayuso | 9f1294d | 2011-04-07 18:47:39 +0200 | [diff] [blame] | 1128 | printf("Usage: ipaccess-proxy [options]\n"); |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1129 | } |
| 1130 | |
Pablo Neira Ayuso | 9f1294d | 2011-04-07 18:47:39 +0200 | [diff] [blame] | 1131 | enum { |
| 1132 | IPA_PROXY_OPT_LISTEN_NONE = 0, |
| 1133 | IPA_PROXY_OPT_LISTEN_IP = (1 << 0), |
| 1134 | IPA_PROXY_OPT_BSC_IP = (1 << 1), |
| 1135 | }; |
| 1136 | |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1137 | static void handle_options(int argc, char** argv) |
| 1138 | { |
Pablo Neira Ayuso | 9f1294d | 2011-04-07 18:47:39 +0200 | [diff] [blame] | 1139 | int options_mask = 0; |
| 1140 | |
Pablo Neira Ayuso | 25ffe54 | 2011-04-11 16:33:03 +0200 | [diff] [blame] | 1141 | /* disable explicit missing arguments error output from getopt_long */ |
| 1142 | opterr = 0; |
| 1143 | |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1144 | while (1) { |
| 1145 | int option_index = 0, c; |
| 1146 | static struct option long_options[] = { |
| 1147 | {"help", 0, 0, 'h'}, |
| 1148 | {"disable-color", 0, 0, 's'}, |
| 1149 | {"timestamp", 0, 0, 'T'}, |
| 1150 | {"log-level", 1, 0, 'e'}, |
| 1151 | {"listen", 1, 0, 'l'}, |
| 1152 | {"bsc", 1, 0, 'b'}, |
| 1153 | {0, 0, 0, 0} |
| 1154 | }; |
| 1155 | |
Holger Hans Peter Freyther | 6147c5d | 2010-06-09 16:20:39 +0800 | [diff] [blame] | 1156 | c = getopt_long(argc, argv, "hsTe:l:b:g:", |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1157 | long_options, &option_index); |
| 1158 | if (c == -1) |
| 1159 | break; |
| 1160 | |
| 1161 | switch (c) { |
| 1162 | case 'h': |
| 1163 | print_usage(); |
| 1164 | print_help(); |
| 1165 | exit(0); |
| 1166 | case 'l': |
| 1167 | listen_ipaddr = optarg; |
Pablo Neira Ayuso | 9f1294d | 2011-04-07 18:47:39 +0200 | [diff] [blame] | 1168 | options_mask |= IPA_PROXY_OPT_LISTEN_IP; |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1169 | break; |
| 1170 | case 'b': |
| 1171 | bsc_ipaddr = optarg; |
Pablo Neira Ayuso | 9f1294d | 2011-04-07 18:47:39 +0200 | [diff] [blame] | 1172 | options_mask |= IPA_PROXY_OPT_BSC_IP; |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1173 | break; |
Holger Hans Peter Freyther | 6147c5d | 2010-06-09 16:20:39 +0800 | [diff] [blame] | 1174 | case 'g': |
| 1175 | gprs_ns_ipaddr = optarg; |
| 1176 | break; |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1177 | case 's': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1178 | log_set_use_color(osmo_stderr_target, 0); |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1179 | break; |
| 1180 | case 'T': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1181 | log_set_print_timestamp(osmo_stderr_target, 1); |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1182 | break; |
| 1183 | case 'e': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1184 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1185 | break; |
Pablo Neira Ayuso | 25ffe54 | 2011-04-11 16:33:03 +0200 | [diff] [blame] | 1186 | case '?': |
| 1187 | if (optopt) { |
| 1188 | printf("ERROR: missing mandatory argument " |
| 1189 | "for `%s' option\n", argv[optind-1]); |
| 1190 | } else { |
| 1191 | printf("ERROR: unknown option `%s'\n", |
| 1192 | argv[optind-1]); |
| 1193 | } |
| 1194 | print_usage(); |
| 1195 | print_help(); |
| 1196 | exit(EXIT_FAILURE); |
| 1197 | break; |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1198 | default: |
| 1199 | /* ignore */ |
| 1200 | break; |
| 1201 | } |
| 1202 | } |
Harald Welte | 2ca002f | 2019-12-03 21:49:06 +0100 | [diff] [blame] | 1203 | if (argc > optind) { |
| 1204 | fprintf(stderr, "Unsupported positional arguments on command line\n"); |
| 1205 | exit(2); |
| 1206 | } |
Pablo Neira Ayuso | 9f1294d | 2011-04-07 18:47:39 +0200 | [diff] [blame] | 1207 | if ((options_mask & (IPA_PROXY_OPT_LISTEN_IP | IPA_PROXY_OPT_BSC_IP)) |
| 1208 | != (IPA_PROXY_OPT_LISTEN_IP | IPA_PROXY_OPT_BSC_IP)) { |
| 1209 | printf("ERROR: You have to specify `--listen' and `--bsc' " |
| 1210 | "options at least.\n"); |
| 1211 | print_usage(); |
| 1212 | print_help(); |
| 1213 | exit(EXIT_FAILURE); |
| 1214 | } |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1215 | } |
| 1216 | |
Neels Hofmeyr | 7997bf4 | 2018-02-13 17:16:44 +0100 | [diff] [blame] | 1217 | static const struct log_info_cat log_categories[] = { |
| 1218 | [DMM] = { |
| 1219 | .name = "DMM", |
| 1220 | .description = "Layer3 Mobility Management (MM)", |
| 1221 | .color = "\033[1;33m", |
| 1222 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 1223 | }, |
| 1224 | }; |
| 1225 | |
| 1226 | const struct log_info log_info = { |
| 1227 | .cat = log_categories, |
| 1228 | .num_cat = ARRAY_SIZE(log_categories), |
| 1229 | }; |
| 1230 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1231 | int main(int argc, char **argv) |
| 1232 | { |
| 1233 | int rc; |
| 1234 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1235 | tall_bsc_ctx = talloc_named_const(NULL, 1, "ipaccess-proxy"); |
Neels Hofmeyr | 4c2d4ab | 2016-09-16 02:31:17 +0200 | [diff] [blame] | 1236 | msgb_talloc_ctx_init(tall_bsc_ctx, 0); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1237 | |
Neels Hofmeyr | e341618 | 2018-03-05 05:31:14 +0100 | [diff] [blame] | 1238 | osmo_init_logging2(tall_bsc_ctx, &log_info); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 1239 | log_parse_category_mask(osmo_stderr_target, "DLINP:DLMI"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1240 | |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1241 | handle_options(argc, argv); |
| 1242 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1243 | rc = ipaccess_proxy_setup(); |
| 1244 | if (rc < 0) |
| 1245 | exit(1); |
| 1246 | |
| 1247 | signal(SIGUSR1, &signal_handler); |
| 1248 | signal(SIGABRT, &signal_handler); |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1249 | osmo_init_ignore_signals(); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1250 | |
| 1251 | while (1) { |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1252 | osmo_select_main(0); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1253 | } |
| 1254 | } |
Pau Espin Pedrol | f8d0389 | 2019-11-12 16:30:30 +0100 | [diff] [blame] | 1255 | |
| 1256 | /* Stub */ |
| 1257 | int rsl_chan_ms_power_ctrl(struct gsm_lchan *lchan) |
| 1258 | { |
| 1259 | return 0; |
| 1260 | } |