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