blob: d617f2cfd9cc8e4777c5bdd3f6c57216114ea424 [file] [log] [blame]
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08001
2/* BSC Multiplexer/NAT Utilities */
3
4/*
5 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2010 by On-Waves
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
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
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <openbsc/bsc_nat.h>
26#include <openbsc/gsm_data.h>
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020027#include <openbsc/bssap.h>
28#include <openbsc/debug.h>
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020029#include <openbsc/ipaccess.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080030
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020031#include <osmocore/linuxlist.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080032#include <osmocore/talloc.h>
Holger Hans Peter Freyther13959482010-06-15 18:50:57 +080033#include <osmocore/gsm0808.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080034
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020035#include <sccp/sccp.h>
36
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020037#include <netinet/in.h>
38#include <arpa/inet.h>
39
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080040struct bsc_nat *bsc_nat_alloc(void)
41{
42 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
43 if (!nat)
44 return NULL;
45
46 INIT_LLIST_HEAD(&nat->sccp_connections);
47 INIT_LLIST_HEAD(&nat->bsc_connections);
48 INIT_LLIST_HEAD(&nat->bsc_configs);
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020049 nat->stats.sccp.conn = counter_alloc("nat.sccp.conn");
50 nat->stats.sccp.calls = counter_alloc("nat.sccp.calls");
51 nat->stats.bsc.reconn = counter_alloc("nat.bsc.conn");
52 nat->stats.bsc.auth_fail = counter_alloc("nat.bsc.auth_fail");
53 nat->stats.msc.reconn = counter_alloc("nat.msc.conn");
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080054 nat->msc_ip = talloc_strdup(nat, "127.0.0.1");
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +020055 nat->msc_port = 5000;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +080056 nat->auth_timeout = 2;
57 nat->ping_timeout = 20;
58 nat->pong_timeout = 5;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080059 return nat;
60}
61
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080062void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
63{
64 if (nat->msc_ip)
65 talloc_free(nat->msc_ip);
66 nat->msc_ip = talloc_strdup(nat, ip);
67}
68
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080069struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
70{
71 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
72 if (!con)
73 return NULL;
74
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +020075 con->nat = nat;
Holger Hans Peter Freyther81519732010-04-22 12:05:23 +080076 write_queue_init(&con->write_queue, 100);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080077 return con;
78}
79
80struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
81{
82 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
83 if (!conf)
84 return NULL;
85
86 conf->token = talloc_strdup(conf, token);
87 conf->lac = lac;
88 conf->nr = nat->num_bsc;
89 conf->nat = nat;
90
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +020091 llist_add_tail(&conf->entry, &nat->bsc_configs);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080092 ++nat->num_bsc;
93
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020094 conf->stats.sccp.conn = counter_alloc("nat.bsc.sccp.conn");
95 conf->stats.sccp.calls = counter_alloc("nat.bsc.sccp.calls");
96 conf->stats.net.reconn = counter_alloc("nat.bsc.net.reconnects");
97
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080098 return conf;
99}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200100
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200101void sccp_connection_destroy(struct sccp_connections *conn)
102{
103 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
104 sccp_src_ref_to_int(&conn->real_ref),
105 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800106 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200107 llist_del(&conn->list_entry);
108 talloc_free(conn);
109}
110
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200111struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg, int *lac_out)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200112{
113 struct bsc_connection *bsc;
114 int data_length;
115 const u_int8_t *data;
116 struct tlv_parsed tp;
117 int i = 0;
118
Holger Hans Peter Freyther7a773692010-04-18 02:41:20 +0800119 *lac_out = -1;
120
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200121 if (!msg->l3h || msgb_l3len(msg) < 3) {
122 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
123 return NULL;
124 }
125
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200126 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
127 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
128 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
129 return NULL;
130 }
131
132 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
133 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800134
135 /* No need to try a different BSS */
136 if (data[0] == CELL_IDENT_BSS) {
137 return NULL;
138 } else if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200139 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200140 return NULL;
141 }
142
143 /* Currently we only handle one BSC */
144 for (i = 1; i < data_length - 1; i += 2) {
145 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200146 *lac_out = _lac;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200147 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200148 if (!bsc->cfg)
149 continue;
150 if (!bsc->authenticated || _lac != bsc->cfg->lac)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200151 continue;
152
153 return bsc;
154 }
155 }
156
157 return NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200158}
159
160int bsc_write_mgcp(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length)
161{
162 struct msgb *msg;
163
164 if (length > 4096 - 128) {
165 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
166 return -1;
167 }
168
169 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
170 if (!msg) {
171 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
172 return -1;
173 }
174
175 /* copy the data */
176 msg->l3h = msgb_put(msg, length);
177 memcpy(msg->l3h, data, length);
178
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200179 return bsc_write(bsc, msg, NAT_IPAC_PROTO_MGCP);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200180}
181
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200182int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200183{
184 /* prepend the header */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200185 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200186
187 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
188 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
189 msgb_free(msg);
190 return -1;
191 }
192
193 return 0;
194}