blob: c9a90f96cc59145ae2d5e8e3f52e23c57262c6a8 [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 Freytherdcf8a7d2010-06-15 18:48:01 +080056 return nat;
57}
58
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080059void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
60{
61 if (nat->msc_ip)
62 talloc_free(nat->msc_ip);
63 nat->msc_ip = talloc_strdup(nat, ip);
64}
65
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080066struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
67{
68 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
69 if (!con)
70 return NULL;
71
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +020072 con->nat = nat;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080073 return con;
74}
75
76struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
77{
78 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
79 if (!conf)
80 return NULL;
81
82 conf->token = talloc_strdup(conf, token);
83 conf->lac = lac;
84 conf->nr = nat->num_bsc;
85 conf->nat = nat;
86
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +020087 llist_add_tail(&conf->entry, &nat->bsc_configs);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080088 ++nat->num_bsc;
89
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020090 conf->stats.sccp.conn = counter_alloc("nat.bsc.sccp.conn");
91 conf->stats.sccp.calls = counter_alloc("nat.bsc.sccp.calls");
92 conf->stats.net.reconn = counter_alloc("nat.bsc.net.reconnects");
93
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080094 return conf;
95}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020096
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020097void sccp_connection_destroy(struct sccp_connections *conn)
98{
99 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
100 sccp_src_ref_to_int(&conn->real_ref),
101 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800102 bsc_mgcp_clear(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200103 llist_del(&conn->list_entry);
104 talloc_free(conn);
105}
106
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200107struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg)
108{
109 struct bsc_connection *bsc;
110 int data_length;
111 const u_int8_t *data;
112 struct tlv_parsed tp;
113 int i = 0;
114
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200115 if (!msg->l3h || msgb_l3len(msg) < 3) {
116 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
117 return NULL;
118 }
119
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200120 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
121 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
122 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
123 return NULL;
124 }
125
126 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
127 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
128 if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200129 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200130 return NULL;
131 }
132
133 /* Currently we only handle one BSC */
134 for (i = 1; i < data_length - 1; i += 2) {
135 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
136 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200137 if (!bsc->cfg)
138 continue;
139 if (!bsc->authenticated || _lac != bsc->cfg->lac)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200140 continue;
141
142 return bsc;
143 }
144 }
145
146 return NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200147}
148
149int bsc_write_mgcp(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length)
150{
151 struct msgb *msg;
152
153 if (length > 4096 - 128) {
154 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
155 return -1;
156 }
157
158 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
159 if (!msg) {
160 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
161 return -1;
162 }
163
164 /* copy the data */
165 msg->l3h = msgb_put(msg, length);
166 memcpy(msg->l3h, data, length);
167
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200168 return bsc_write(bsc, msg, NAT_IPAC_PROTO_MGCP);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200169}
170
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200171int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200172{
173 /* prepend the header */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200174 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200175
176 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
177 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
178 msgb_free(msg);
179 return -1;
180 }
181
182 return 0;
183}