blob: 623bec56ac80568789080d50e020c72c2d01b4f9 [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 Freytherdcf8a7d2010-06-15 18:48:01 +080055 return nat;
56}
57
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080058void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
59{
60 if (nat->msc_ip)
61 talloc_free(nat->msc_ip);
62 nat->msc_ip = talloc_strdup(nat, ip);
63}
64
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080065struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
66{
67 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
68 if (!con)
69 return NULL;
70
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +020071 con->nat = nat;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080072 return con;
73}
74
75struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
76{
77 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
78 if (!conf)
79 return NULL;
80
81 conf->token = talloc_strdup(conf, token);
82 conf->lac = lac;
83 conf->nr = nat->num_bsc;
84 conf->nat = nat;
85
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +020086 llist_add_tail(&conf->entry, &nat->bsc_configs);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080087 ++nat->num_bsc;
88
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020089 conf->stats.sccp.conn = counter_alloc("nat.bsc.sccp.conn");
90 conf->stats.sccp.calls = counter_alloc("nat.bsc.sccp.calls");
91 conf->stats.net.reconn = counter_alloc("nat.bsc.net.reconnects");
92
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080093 return conf;
94}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020095
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020096void sccp_connection_destroy(struct sccp_connections *conn)
97{
98 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
99 sccp_src_ref_to_int(&conn->real_ref),
100 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800101 bsc_mgcp_clear(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200102 llist_del(&conn->list_entry);
103 talloc_free(conn);
104}
105
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200106struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg)
107{
108 struct bsc_connection *bsc;
109 int data_length;
110 const u_int8_t *data;
111 struct tlv_parsed tp;
112 int i = 0;
113
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200114 if (!msg->l3h || msgb_l3len(msg) < 3) {
115 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
116 return NULL;
117 }
118
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200119 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
120 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
121 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
122 return NULL;
123 }
124
125 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
126 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
127 if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200128 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200129 return NULL;
130 }
131
132 /* Currently we only handle one BSC */
133 for (i = 1; i < data_length - 1; i += 2) {
134 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
135 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200136 if (!bsc->cfg)
137 continue;
138 if (!bsc->authenticated || _lac != bsc->cfg->lac)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200139 continue;
140
141 return bsc;
142 }
143 }
144
145 return NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200146}
147
148int bsc_write_mgcp(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length)
149{
150 struct msgb *msg;
151
152 if (length > 4096 - 128) {
153 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
154 return -1;
155 }
156
157 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
158 if (!msg) {
159 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
160 return -1;
161 }
162
163 /* copy the data */
164 msg->l3h = msgb_put(msg, length);
165 memcpy(msg->l3h, data, length);
166
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200167 return bsc_write(bsc, msg, NAT_IPAC_PROTO_MGCP);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200168}
169
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200170int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200171{
172 /* prepend the header */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200173 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200174
175 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
176 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
177 msgb_free(msg);
178 return -1;
179 }
180
181 return 0;
182}