blob: 36a64b70cb8a5e75832e38cd6dd552cb3f2031c8 [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>
33
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020034#include <sccp/sccp.h>
35
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020036#include <netinet/in.h>
37#include <arpa/inet.h>
38
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080039struct bsc_nat *bsc_nat_alloc(void)
40{
41 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
42 if (!nat)
43 return NULL;
44
45 INIT_LLIST_HEAD(&nat->sccp_connections);
46 INIT_LLIST_HEAD(&nat->bsc_connections);
47 INIT_LLIST_HEAD(&nat->bsc_configs);
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020048 nat->stats.sccp.conn = counter_alloc("nat.sccp.conn");
49 nat->stats.sccp.calls = counter_alloc("nat.sccp.calls");
50 nat->stats.bsc.reconn = counter_alloc("nat.bsc.conn");
51 nat->stats.bsc.auth_fail = counter_alloc("nat.bsc.auth_fail");
52 nat->stats.msc.reconn = counter_alloc("nat.msc.conn");
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080053 return nat;
54}
55
56struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
57{
58 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
59 if (!con)
60 return NULL;
61
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +020062 con->nat = nat;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080063 return con;
64}
65
66struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
67{
68 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
69 if (!conf)
70 return NULL;
71
72 conf->token = talloc_strdup(conf, token);
73 conf->lac = lac;
74 conf->nr = nat->num_bsc;
75 conf->nat = nat;
76
77 llist_add(&conf->entry, &nat->bsc_configs);
78 ++nat->num_bsc;
79
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020080 conf->stats.sccp.conn = counter_alloc("nat.bsc.sccp.conn");
81 conf->stats.sccp.calls = counter_alloc("nat.bsc.sccp.calls");
82 conf->stats.net.reconn = counter_alloc("nat.bsc.net.reconnects");
83
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080084 return conf;
85}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020086
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020087void sccp_connection_destroy(struct sccp_connections *conn)
88{
89 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
90 sccp_src_ref_to_int(&conn->real_ref),
91 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080092 bsc_mgcp_clear(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020093 llist_del(&conn->list_entry);
94 talloc_free(conn);
95}
96
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020097struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg)
98{
99 struct bsc_connection *bsc;
100 int data_length;
101 const u_int8_t *data;
102 struct tlv_parsed tp;
103 int i = 0;
104
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200105 if (!msg->l3h || msgb_l3len(msg) < 3) {
106 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
107 return NULL;
108 }
109
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200110 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
111 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
112 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
113 return NULL;
114 }
115
116 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
117 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
118 if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200119 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200120 return NULL;
121 }
122
123 /* Currently we only handle one BSC */
124 for (i = 1; i < data_length - 1; i += 2) {
125 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
126 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200127 if (!bsc->cfg)
128 continue;
129 if (!bsc->authenticated || _lac != bsc->cfg->lac)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200130 continue;
131
132 return bsc;
133 }
134 }
135
136 return NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200137}
138
139int bsc_write_mgcp(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length)
140{
141 struct msgb *msg;
142
143 if (length > 4096 - 128) {
144 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
145 return -1;
146 }
147
148 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
149 if (!msg) {
150 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
151 return -1;
152 }
153
154 /* copy the data */
155 msg->l3h = msgb_put(msg, length);
156 memcpy(msg->l3h, data, length);
157
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200158 return bsc_write(bsc, msg, NAT_IPAC_PROTO_MGCP);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200159}
160
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200161int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200162{
163 /* prepend the header */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200164 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200165
166 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
167 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
168 msgb_free(msg);
169 return -1;
170 }
171
172 return 0;
173}