blob: 2d5e2287e644cb5dcb75650618edffb4e7224242 [file] [log] [blame]
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +08001/*
2 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freytherdf6143a2010-06-15 18:46:56 +08003 * (C) 2010 by On-Waves
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +08004 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22#ifndef BSC_NAT_H
23#define BSC_NAT_H
24
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +080025#include "mgcp.h"
26
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080027#include <sys/types.h>
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080028#include <sccp/sccp_types.h>
Holger Hans Peter Freyther9f8f3d02010-02-07 13:08:09 +010029
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080030#include <osmocore/select.h>
31#include <osmocore/msgb.h>
32#include <osmocore/timer.h>
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +080033#include <osmocore/write_queue.h>
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020034#include <osmocore/statistics.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080035
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +020036#include <regex.h>
37
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +010038#define DIR_BSC 1
39#define DIR_MSC 2
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080040
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +020041#define NAT_IPAC_PROTO_MGCP 0xfc
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +080042
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +080043struct bsc_nat;
44
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080045/*
46 * For the NAT we will need to analyze and later patch
47 * the received message. This would require us to parse
48 * the IPA and SCCP header twice. Instead of doing this
49 * we will have one analyze structure and have the patching
50 * and filter operate on the same structure.
51 */
52struct bsc_nat_parsed {
53 /* ip access prototype */
54 int ipa_proto;
55
56 /* source local reference */
57 struct sccp_source_reference *src_local_ref;
58
59 /* destination local reference */
60 struct sccp_source_reference *dest_local_ref;
61
62 /* called ssn number */
63 int called_ssn;
64
65 /* calling ssn number */
66 int calling_ssn;
67
68 /* sccp message type */
69 int sccp_type;
70
71 /* bssap type, e.g. 0 for BSS Management */
72 int bssap;
73
74 /* the gsm0808 message type */
75 int gsm_type;
76};
77
Holger Hans Peter Freyther9f8f3d02010-02-07 13:08:09 +010078/*
79 * Per BSC data structure
80 */
81struct bsc_connection {
82 struct llist_head list_entry;
83
84 /* do we know anything about this BSC? */
85 int authenticated;
86
87 /* the fd we use to communicate */
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +080088 struct write_queue write_queue;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080089
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +020090 /* the BSS associated */
91 struct bsc_config *cfg;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080092
93 /* a timeout node */
94 struct timer_list id_timeout;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +080095
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +080096 /* pong timeout */
97 struct timer_list ping_timeout;
98 struct timer_list pong_timeout;
99
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800100 /* a back pointer */
101 struct bsc_nat *nat;
Holger Hans Peter Freyther9f8f3d02010-02-07 13:08:09 +0100102};
103
104/*
105 * Per SCCP source local reference patch table. It needs to
106 * be updated on new SCCP connections, connection confirm and reject,
107 * and on the loss of the BSC connection.
108 */
109struct sccp_connections {
110 struct llist_head list_entry;
111
112 struct bsc_connection *bsc;
113
114 struct sccp_source_reference real_ref;
115 struct sccp_source_reference patched_ref;
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200116 struct sccp_source_reference remote_ref;
Holger Hans Peter Freyther4c683d12010-04-23 14:13:27 +0800117 int has_remote_ref;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800118
119 /* GSM audio handling. That is 32 * multiplex + ts */
Holger Hans Peter Freyther959bbcf2010-04-22 20:12:13 +0800120 int crcx;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800121 int msc_timeslot;
122 int bsc_timeslot;
Holger Hans Peter Freyther9f8f3d02010-02-07 13:08:09 +0100123};
124
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800125/**
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200126 * Stats per BSC
127 */
128struct bsc_config_stats {
129 struct {
130 struct counter *conn;
131 struct counter *calls;
132 } sccp;
133
134 struct {
135 struct counter *reconn;
136 } net;
137};
138
139/**
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800140 * One BSC entry in the config
141 */
142struct bsc_config {
143 struct llist_head entry;
144
145 char *token;
146 unsigned int lac;
147 int nr;
148
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200149 /* imsi white and blacklist */
150 char *imsi_allow;
151 regex_t imsi_allow_re;
152 char *imsi_deny;
153 regex_t imsi_deny_re;
154
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800155 int forbid_paging;
156
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200157 /* backpointer */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800158 struct bsc_nat *nat;
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200159
160 struct bsc_config_stats stats;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800161};
162
163/**
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200164 * BSCs point of view of endpoints
165 */
166struct bsc_endpoint {
167 /* the pending transaction id */
168 char *transaction_id;
169 /* the bsc we are talking to */
170 struct bsc_connection *bsc;
171};
172
173/**
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200174 * Statistic for the nat.
175 */
176struct bsc_nat_statistics {
177 struct {
178 struct counter *conn;
179 struct counter *calls;
180 } sccp;
181
182 struct {
183 struct counter *reconn;
184 struct counter *auth_fail;
185 } bsc;
186
187 struct {
188 struct counter *reconn;
189 } msc;
190};
191
192/**
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800193 * the structure of the "nat" network
194 */
195struct bsc_nat {
196 /* active SCCP connections that need patching */
197 struct llist_head sccp_connections;
198
199 /* active BSC connections that need patching */
200 struct llist_head bsc_connections;
201
202 /* known BSC's */
203 struct llist_head bsc_configs;
204 int num_bsc;
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800205
206 /* MGCP config */
207 struct mgcp_config *mgcp_cfg;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200208 struct write_queue mgcp_queue;
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200209 u_int8_t mgcp_msg[4096];
210 int mgcp_length;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200211
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200212 /* msc things */
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800213 char *msc_ip;
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +0200214 int msc_port;
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200215 int first_contact;
216
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800217 /* timeouts */
218 int auth_timeout;
219 int ping_timeout;
220 int pong_timeout;
221
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200222 struct bsc_endpoint *bsc_endpoints;
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200223
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200224 /* filter */
225 char *imsi_allow;
226 regex_t imsi_allow_re;
227 char *imsi_deny;
228 regex_t imsi_deny_re;
229
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200230 /* statistics */
231 struct bsc_nat_statistics stats;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800232};
233
234/* create and init the structures */
235struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac);
236struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800237struct bsc_nat *bsc_nat_alloc(void);
238struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800239void bsc_nat_set_msc_ip(struct bsc_nat *bsc, const char *ip);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800240
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200241void sccp_connection_destroy(struct sccp_connections *);
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800242void bsc_close_connection(struct bsc_connection *);
Holger Hans Peter Freyther9f8f3d02010-02-07 13:08:09 +0100243
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800244/**
245 * parse the given message into the above structure
246 */
247struct bsc_nat_parsed *bsc_nat_parse(struct msgb *msg);
248
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800249/**
250 * filter based on IP Access header in both directions
251 */
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100252int bsc_nat_filter_ipa(int direction, struct msgb *msg, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800253int bsc_nat_vty_init(struct bsc_nat *nat);
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200254struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg, int *_lac);
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800255
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800256/**
257 * SCCP patching and handling
258 */
259int create_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800260int update_sccp_src_ref(struct sccp_connections *sccp, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800261void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800262struct sccp_connections *patch_sccp_src_ref_to_bsc(struct msgb *, struct bsc_nat_parsed *, struct bsc_nat *);
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800263struct sccp_connections *patch_sccp_src_ref_to_msc(struct msgb *, struct bsc_nat_parsed *, struct bsc_connection *);
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800264
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800265/**
266 * MGCP/Audio handling
267 */
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200268int bsc_write_mgcp(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800269int bsc_mgcp_assign(struct sccp_connections *, struct msgb *msg);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800270void bsc_mgcp_init(struct sccp_connections *);
271void bsc_mgcp_dlcx(struct sccp_connections *);
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200272void bsc_mgcp_free_endpoints(struct bsc_nat *nat);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800273int bsc_mgcp_nat_init(struct bsc_nat *nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800274
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800275struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *, int endpoint_number);
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200276struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200277void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg);
278
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200279void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200280int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60]);
281int bsc_mgcp_extract_ci(const char *resp);
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200282
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200283
284int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int id);
285
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800286#endif