blob: 103593780cd90d9952a59925c24bad47343c91b1 [file] [log] [blame]
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +08001/*
Holger Hans Peter Freytherd2361d92012-12-03 15:32:54 +01002 * (C) 2010-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2010-2012 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
Harald Welte9af6ddf2011-01-01 15:25:50 +01007 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +08009 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010014 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080015 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080018 *
19 */
20
21#ifndef BSC_NAT_H
22#define BSC_NAT_H
23
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +080024#include "mgcp.h"
Holger Hans Peter Freyther4ba947b2015-04-05 18:07:45 +020025#include "bsc_msg_filter.h"
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +080026
Holger Hans Peter Freyther9f8f3d02010-02-07 13:08:09 +010027
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010028#include <osmocom/core/select.h>
29#include <osmocom/core/msgb.h>
30#include <osmocom/core/msgfile.h>
31#include <osmocom/core/timer.h>
32#include <osmocom/core/write_queue.h>
33#include <osmocom/core/rate_ctr.h>
34#include <osmocom/core/statistics.h>
35#include <osmocom/gsm/protocol/gsm_04_08.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080036
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +020037#include <regex.h>
38
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +010039#define DIR_BSC 1
40#define DIR_MSC 2
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080041
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +020042#define PAGIN_GROUP_UNASSIGNED -1
43
Holger Hans Peter Freyther3d387422010-10-16 16:35:00 +020044struct sccp_source_reference;
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +020045struct nat_sccp_connection;
Holger Hans Peter Freytherc2b31ed2010-07-31 05:17:17 +080046struct bsc_nat_parsed;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +080047struct bsc_nat;
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020048struct bsc_nat_ussd_con;
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +020049struct nat_rewrite_rule;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +080050
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080051/*
Holger Hans Peter Freythere5d32492011-04-16 16:02:59 +020052 * Is this terminated to the MSC, to the local machine (release
53 * handling for IMSI filtering) or to a USSD provider?
54 */
55enum {
56 NAT_CON_END_MSC,
57 NAT_CON_END_LOCAL,
58 NAT_CON_END_USSD,
59};
60
61/*
Daniel Willmanna86bc392011-02-18 14:32:56 +010062 * Pending command entry
63 */
64struct bsc_cmd_list {
65 struct llist_head list_entry;
66
67 struct osmo_timer_list timeout;
68
69 /* The NATed ID used on the bsc_con*/
70 int nat_id;
71
72 /* The control connection from which the command originated */
73 struct ctrl_connection *ccon;
74
75 /* The command from the control connection */
76 struct ctrl_cmd *cmd;
77};
78
79/*
Holger Hans Peter Freyther9f8f3d02010-02-07 13:08:09 +010080 * Per BSC data structure
81 */
82struct bsc_connection {
83 struct llist_head list_entry;
84
85 /* do we know anything about this BSC? */
86 int authenticated;
87
88 /* the fd we use to communicate */
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +020089 struct osmo_wqueue write_queue;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080090
Jacob Erlbecke8278122014-03-31 13:42:11 +020091 /* incoming message buffer */
92 struct msgb *pending_msg;
93
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +020094 /* the BSS associated */
95 struct bsc_config *cfg;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080096
97 /* a timeout node */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +020098 struct osmo_timer_list id_timeout;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +080099
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800100 /* pong timeout */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200101 struct osmo_timer_list ping_timeout;
102 struct osmo_timer_list pong_timeout;
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800103
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800104 /* mgcp related code */
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +0100105 char *_endpoint_status;
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100106 int number_multiplexes;
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100107 int max_endpoints;
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800108 int last_endpoint;
Holger Hans Peter Freyther462b7d72012-10-24 21:53:40 +0200109 int next_transaction;
110 uint32_t pending_dlcx_count;
111 struct llist_head pending_dlcx;
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800112
Daniel Willmanna86bc392011-02-18 14:32:56 +0100113 /* track the pending commands for this BSC */
114 struct llist_head cmd_pending;
115 int last_id;
116
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800117 /* a back pointer */
118 struct bsc_nat *nat;
Holger Hans Peter Freyther9f8f3d02010-02-07 13:08:09 +0100119};
120
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800121/**
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200122 * Stats per BSC
123 */
124struct bsc_config_stats {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800125 struct rate_ctr_group *ctrg;
126};
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200127
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800128enum bsc_cfg_ctr {
129 BCFG_CTR_SCCP_CONN,
130 BCFG_CTR_SCCP_CALLS,
131 BCFG_CTR_NET_RECONN,
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800132 BCFG_CTR_DROPPED_SCCP,
133 BCFG_CTR_DROPPED_CALLS,
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800134 BCFG_CTR_REJECTED_CR,
135 BCFG_CTR_REJECTED_MSG,
136 BCFG_CTR_ILL_PACKET,
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800137 BCFG_CTR_CON_TYPE_LU,
138 BCFG_CTR_CON_CMSERV_RQ,
139 BCFG_CTR_CON_PAG_RESP,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800140 BCFG_CTR_CON_SSA,
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800141 BCFG_CTR_CON_OTHER,
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200142};
143
144/**
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800145 * One BSC entry in the config
146 */
147struct bsc_config {
148 struct llist_head entry;
149
150 char *token;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800151 int nr;
152
Holger Hans Peter Freytherb6061012010-05-14 22:06:28 +0800153 char *description;
154
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200155 /* imsi white and blacklist */
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800156 char *acc_lst_name;
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200157
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800158 int forbid_paging;
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200159 int paging_group;
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800160
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +0100161 /* audio handling */
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100162 int max_endpoints;
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +0100163
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200164 /* backpointer */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800165 struct bsc_nat *nat;
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200166
167 struct bsc_config_stats stats;
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800168
169 struct llist_head lac_list;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100170
171 /* Osmux is enabled/disabled per BSC */
172 int osmux;
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800173};
174
175struct bsc_lac_entry {
176 struct llist_head entry;
177 uint16_t lac;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800178};
179
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200180struct bsc_nat_paging_group {
181 struct llist_head entry;
182
183 /* list of lac entries */
184 struct llist_head lists;
185 int nr;
186};
187
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800188/**
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200189 * BSCs point of view of endpoints
190 */
191struct bsc_endpoint {
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800192 /* the operation that is carried out */
193 int transaction_state;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200194 /* the pending transaction id */
195 char *transaction_id;
196 /* the bsc we are talking to */
197 struct bsc_connection *bsc;
198};
199
200/**
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200201 * Statistic for the nat.
202 */
203struct bsc_nat_statistics {
204 struct {
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200205 struct osmo_counter *conn;
206 struct osmo_counter *calls;
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200207 } sccp;
208
209 struct {
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200210 struct osmo_counter *reconn;
211 struct osmo_counter *auth_fail;
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200212 } bsc;
213
214 struct {
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200215 struct osmo_counter *reconn;
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200216 } msc;
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200217
218 struct {
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200219 struct osmo_counter *reconn;
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200220 } ussd;
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200221};
222
223/**
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800224 * the structure of the "nat" network
225 */
226struct bsc_nat {
227 /* active SCCP connections that need patching */
228 struct llist_head sccp_connections;
229
230 /* active BSC connections that need patching */
231 struct llist_head bsc_connections;
232
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800233 /* access lists */
234 struct llist_head access_lists;
235
Holger Hans Peter Freyther474698a2011-05-02 16:50:36 +0200236 /* paging groups */
237 struct llist_head paging_groups;
238
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800239 /* known BSC's */
240 struct llist_head bsc_configs;
241 int num_bsc;
Holger Hans Peter Freyther6b771072010-07-27 19:21:53 +0800242 int bsc_ip_dscp;
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800243
244 /* MGCP config */
245 struct mgcp_config *mgcp_cfg;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800246 uint8_t mgcp_msg[4096];
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200247 int mgcp_length;
Holger Hans Peter Freytherc3271872012-11-05 14:54:56 +0100248 int mgcp_ipa;
Holger Hans Peter Freyther7f100c92015-04-23 20:25:17 -0400249 int sdp_ensure_amr_mode_set;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200250
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200251 /* msc things */
Holger Hans Peter Freythere18801052011-04-23 23:31:31 +0200252 struct llist_head dests;
253 struct bsc_msc_dest *main_dest;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +0800254 struct bsc_msc_connection *msc_con;
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800255 char *token;
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200256
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800257 /* timeouts */
258 int auth_timeout;
259 int ping_timeout;
260 int pong_timeout;
261
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200262 struct bsc_endpoint *bsc_endpoints;
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200263
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200264 /* filter */
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800265 char *acc_lst_name;
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200266
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +0100267 /* Barring of subscribers with a rb tree */
268 struct rb_root imsi_black_list;
269 char *imsi_black_list_fn;
270
Holger Hans Peter Freyther76424392010-10-21 10:59:54 +0200271 /* number rewriting */
272 char *num_rewr_name;
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200273 struct llist_head num_rewr;
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +0200274 char *num_rewr_post_name;
275 struct llist_head num_rewr_post;
Holger Hans Peter Freyther76424392010-10-21 10:59:54 +0200276
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +0200277 char *smsc_rewr_name;
278 struct llist_head smsc_rewr;
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +0200279 char *tpdest_match_name;
280 struct llist_head tpdest_match;
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +0100281 char *sms_clear_tp_srr_name;
282 struct llist_head sms_clear_tp_srr;
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +0100283 char *sms_num_rewr_name;
284 struct llist_head sms_num_rewr;
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +0200285
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200286 /* more rewriting */
287 char *num_rewr_trie_name;
288 struct nat_rewrite *num_rewr_trie;
289
Holger Hans Peter Freytherd1effd82010-10-10 17:29:20 +0200290 /* USSD messages we want to match */
291 char *ussd_lst_name;
Holger Hans Peter Freytherc1578bc2010-10-11 10:06:39 +0200292 char *ussd_query;
Holger Hans Peter Freythera18b1162011-04-01 17:32:21 +0200293 regex_t ussd_query_re;
Holger Hans Peter Freythere393f272010-10-12 18:25:52 +0200294 char *ussd_token;
295 char *ussd_local;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200296 struct osmo_fd ussd_listen;
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200297 struct bsc_nat_ussd_con *ussd_con;
Holger Hans Peter Freytherd1effd82010-10-10 17:29:20 +0200298
Holger Hans Peter Freytherde2b8602011-04-23 23:55:14 +0200299 /* for maintainenance */
300 int blocked;
301
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200302 /* statistics */
303 struct bsc_nat_statistics stats;
Daniel Willmann6fc4a982011-07-22 17:55:42 +0200304
305 /* control interface */
306 struct ctrl_handle *ctrl;
Holger Hans Peter Freytherfce69712015-06-08 11:56:59 +0200307
308 /* for random values */
309 int random_fd;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800310};
311
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200312struct bsc_nat_ussd_con {
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200313 struct osmo_wqueue queue;
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200314 struct bsc_nat *nat;
315 int authorized;
316
Jacob Erlbecke8278122014-03-31 13:42:11 +0200317 struct msgb *pending_msg;
318
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200319 struct osmo_timer_list auth_timeout;
Holger Hans Peter Freyther91b9a452011-04-16 17:01:47 +0200320};
321
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800322/* create and init the structures */
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800323struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800324struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num);
Holger Hans Peter Freytheraa0cecd2015-06-08 09:54:45 +0200325struct bsc_config *bsc_config_by_token(struct bsc_nat *nat, const char *token, int len);
Holger Hans Peter Freyther9212d9d2011-02-27 11:18:41 +0100326void bsc_config_free(struct bsc_config *);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800327void bsc_config_add_lac(struct bsc_config *cfg, int lac);
328void bsc_config_del_lac(struct bsc_config *cfg, int lac);
329int bsc_config_handles_lac(struct bsc_config *cfg, int lac);
330
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800331struct bsc_nat *bsc_nat_alloc(void);
332struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800333void bsc_nat_set_msc_ip(struct bsc_nat *bsc, const char *ip);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800334
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200335void sccp_connection_destroy(struct nat_sccp_connection *);
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800336void bsc_close_connection(struct bsc_connection *);
Holger Hans Peter Freyther9f8f3d02010-02-07 13:08:09 +0100337
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800338const char *bsc_con_type_to_string(int type);
339
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800340/**
341 * parse the given message into the above structure
342 */
343struct bsc_nat_parsed *bsc_nat_parse(struct msgb *msg);
344
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800345/**
346 * filter based on IP Access header in both directions
347 */
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100348int bsc_nat_filter_ipa(int direction, struct msgb *msg, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800349int bsc_nat_vty_init(struct bsc_nat *nat);
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200350int bsc_nat_find_paging(struct msgb *msg, const uint8_t **,int *len);
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800351
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800352/**
353 * SCCP patching and handling
354 */
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200355struct nat_sccp_connection *create_sccp_src_ref(struct bsc_connection *bsc, struct bsc_nat_parsed *parsed);
356int update_sccp_src_ref(struct nat_sccp_connection *sccp, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800357void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200358struct nat_sccp_connection *patch_sccp_src_ref_to_bsc(struct msgb *, struct bsc_nat_parsed *, struct bsc_nat *);
359struct nat_sccp_connection *patch_sccp_src_ref_to_msc(struct msgb *, struct bsc_nat_parsed *, struct bsc_connection *);
360struct nat_sccp_connection *bsc_nat_find_con_by_bsc(struct bsc_nat *, struct sccp_source_reference *);
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800361
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800362/**
363 * MGCP/Audio handling
364 */
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100365int bsc_mgcp_nr_multiplexes(int max_endpoints);
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800366int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length);
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200367int bsc_mgcp_assign_patch(struct nat_sccp_connection *, struct msgb *msg);
368void bsc_mgcp_init(struct nat_sccp_connection *);
369void bsc_mgcp_dlcx(struct nat_sccp_connection *);
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200370void bsc_mgcp_free_endpoints(struct bsc_nat *nat);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800371int bsc_mgcp_nat_init(struct bsc_nat *nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800372
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200373struct nat_sccp_connection *bsc_mgcp_find_con(struct bsc_nat *, int endpoint_number);
Jacob Erlbeck3dff27d2013-11-29 13:43:48 +0100374struct msgb *bsc_mgcp_rewrite(char *input, int length, int endp, const char *ip,
Holger Hans Peter Freyther7f100c92015-04-23 20:25:17 -0400375 int port, int osmux, int *payload_type, int mode_set);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200376void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg);
377
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200378void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200379int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60]);
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800380uint32_t bsc_mgcp_extract_ci(const char *resp);
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200381
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200382
383int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int id);
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200384int bsc_do_write(struct osmo_wqueue *queue, struct msgb *msg, int id);
385int bsc_write_msg(struct osmo_wqueue *queue, struct msgb *msg);
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200386int bsc_write_cb(struct osmo_fd *bfd, struct msgb *msg);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200387
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +0800388int bsc_nat_msc_is_connected(struct bsc_nat *nat);
389
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200390int bsc_conn_type_to_ctr(struct nat_sccp_connection *conn);
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800391
Holger Hans Peter Freythera3967572010-09-29 02:30:50 +0800392struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed, struct msgb *msg, uint32_t *len);
393
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800394/** USSD filtering */
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200395int bsc_ussd_init(struct bsc_nat *nat);
Holger Hans Peter Freyther6fcc3a92013-08-26 14:04:43 +0200396int bsc_ussd_check(struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed, struct msgb *msg);
397int bsc_ussd_close_connections(struct bsc_nat *nat);
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800398
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +0200399struct msgb *bsc_nat_rewrite_msg(struct bsc_nat *nat, struct msgb *msg, struct bsc_nat_parsed *, const char *imsi);
Holger Hans Peter Freythera914daf2010-10-21 12:12:57 +0200400
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200401/** paging group handling */
402struct bsc_nat_paging_group *bsc_nat_paging_group_num(struct bsc_nat *nat, int group);
403struct bsc_nat_paging_group *bsc_nat_paging_group_create(struct bsc_nat *nat, int group);
404void bsc_nat_paging_group_delete(struct bsc_nat_paging_group *);
405void bsc_nat_paging_group_add_lac(struct bsc_nat_paging_group *grp, int lac);
406void bsc_nat_paging_group_del_lac(struct bsc_nat_paging_group *grp, int lac);
407
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200408/**
409 * Number rewriting support below
410 */
411struct bsc_nat_num_rewr_entry {
412 struct llist_head list;
413
414 regex_t msisdn_reg;
415 regex_t num_reg;
416
417 char *replace;
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200418 uint8_t is_prefix_lookup;
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200419};
420
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +0200421void bsc_nat_num_rewr_entry_adapt(void *ctx, struct llist_head *head, const struct osmo_config_list *);
Holger Hans Peter Freytherad75eab2011-05-27 12:38:58 +0200422
Holger Hans Peter Freytherc3271872012-11-05 14:54:56 +0100423void bsc_nat_send_mgcp_to_msc(struct bsc_nat *bsc_nat, struct msgb *msg);
424void bsc_nat_handle_mgcp(struct bsc_nat *bsc, struct msgb *msg);
425
Holger Hans Peter Freyther98da5442012-11-05 16:04:10 +0100426struct ctrl_handle *bsc_nat_controlif_setup(struct bsc_nat *nat, int port);
427void bsc_nat_ctrl_del_pending(struct bsc_cmd_list *pending);
428int bsc_nat_handle_ctrlif_msg(struct bsc_connection *bsc, struct msgb *msg);
429
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +0200430int bsc_nat_extract_lac(struct bsc_connection *bsc, struct nat_sccp_connection *con,
431 struct bsc_nat_parsed *parsed, struct msgb *msg);
432
Holger Hans Peter Freyther4ba947b2015-04-05 18:07:45 +0200433int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg,
434 struct bsc_nat_parsed *, int *con_type, char **imsi,
435 struct bsc_filter_reject_cause *cause);
436int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
437 struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed,
438 struct bsc_filter_reject_cause *cause);
439
Holger Hans Peter Freyther7c009832015-04-05 13:45:53 +0200440/**
441 * CTRL interface helper
442 */
443void bsc_nat_inform_reject(struct bsc_connection *bsc, const char *imsi);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +0200444
445/*
446 * Use for testing
447 */
448void bsc_nat_free(struct bsc_nat *nat);
449
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800450#endif