blob: 97fa4fe2e5781cb95ce32817c26c45802faba238 [file] [log] [blame]
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001/*
Jonathan Santos5a45b152011-08-17 15:33:57 -04002 * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2010-2011 by On-Waves
Jonathan Santos03fd8d02011-05-25 13:54:02 -04004 * 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 Affero General Public License as published by
8 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
15 *
16 * 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/>.
18 *
19 */
20
21#ifndef BSC_NAT_H
22#define BSC_NAT_H
23
24#include "mgcp.h"
25
Jonathan Santos03fd8d02011-05-25 13:54:02 -040026
Jonathan Santos5a45b152011-08-17 15:33:57 -040027#include <osmocom/core/select.h>
28#include <osmocom/core/msgb.h>
29#include <osmocom/core/msgfile.h>
30#include <osmocom/core/timer.h>
31#include <osmocom/core/write_queue.h>
32#include <osmocom/core/rate_ctr.h>
33#include <osmocom/core/statistics.h>
34#include <osmocom/gsm/protocol/gsm_04_08.h>
Jonathan Santos03fd8d02011-05-25 13:54:02 -040035
36#include <regex.h>
37
38#define DIR_BSC 1
39#define DIR_MSC 2
40
Jonathan Santos5a45b152011-08-17 15:33:57 -040041#define PAGIN_GROUP_UNASSIGNED -1
42
Jonathan Santos03fd8d02011-05-25 13:54:02 -040043struct sccp_source_reference;
44struct sccp_connections;
45struct bsc_nat_parsed;
46struct bsc_nat;
47struct bsc_nat_ussd_con;
48
49enum {
50 NAT_CON_TYPE_NONE,
51 NAT_CON_TYPE_LU,
52 NAT_CON_TYPE_CM_SERV_REQ,
53 NAT_CON_TYPE_PAG_RESP,
54 NAT_CON_TYPE_SSA,
55 NAT_CON_TYPE_LOCAL_REJECT,
56 NAT_CON_TYPE_OTHER,
57};
58
59/*
Jonathan Santos5a45b152011-08-17 15:33:57 -040060 * Is this terminated to the MSC, to the local machine (release
61 * handling for IMSI filtering) or to a USSD provider?
62 */
63enum {
64 NAT_CON_END_MSC,
65 NAT_CON_END_LOCAL,
66 NAT_CON_END_USSD,
67};
68
69/*
Jonathan Santos03fd8d02011-05-25 13:54:02 -040070 * Per BSC data structure
71 */
72struct bsc_connection {
73 struct llist_head list_entry;
74
75 /* do we know anything about this BSC? */
76 int authenticated;
77
78 /* the fd we use to communicate */
Jonathan Santos5a45b152011-08-17 15:33:57 -040079 struct osmo_wqueue write_queue;
Jonathan Santos03fd8d02011-05-25 13:54:02 -040080
81 /* the BSS associated */
82 struct bsc_config *cfg;
83
84 /* a timeout node */
Jonathan Santos5a45b152011-08-17 15:33:57 -040085 struct osmo_timer_list id_timeout;
Jonathan Santos03fd8d02011-05-25 13:54:02 -040086
87 /* pong timeout */
Jonathan Santos5a45b152011-08-17 15:33:57 -040088 struct osmo_timer_list ping_timeout;
89 struct osmo_timer_list pong_timeout;
Jonathan Santos03fd8d02011-05-25 13:54:02 -040090
91 /* mgcp related code */
92 char *_endpoint_status;
93 int number_multiplexes;
94 int max_endpoints;
95 int last_endpoint;
96
97 /* a back pointer */
98 struct bsc_nat *nat;
99};
100
101/**
102 * Stats per BSC
103 */
104struct bsc_config_stats {
105 struct rate_ctr_group *ctrg;
106};
107
108enum bsc_cfg_ctr {
109 BCFG_CTR_SCCP_CONN,
110 BCFG_CTR_SCCP_CALLS,
111 BCFG_CTR_NET_RECONN,
112 BCFG_CTR_DROPPED_SCCP,
113 BCFG_CTR_DROPPED_CALLS,
114 BCFG_CTR_REJECTED_CR,
115 BCFG_CTR_REJECTED_MSG,
116 BCFG_CTR_ILL_PACKET,
117 BCFG_CTR_CON_TYPE_LU,
118 BCFG_CTR_CON_CMSERV_RQ,
119 BCFG_CTR_CON_PAG_RESP,
120 BCFG_CTR_CON_SSA,
121 BCFG_CTR_CON_OTHER,
122};
123
124/**
125 * One BSC entry in the config
126 */
127struct bsc_config {
128 struct llist_head entry;
129
130 char *token;
131 int nr;
132
133 char *description;
134
135 /* imsi white and blacklist */
136 char *acc_lst_name;
137
138 int forbid_paging;
Jonathan Santos5a45b152011-08-17 15:33:57 -0400139 int paging_group;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400140
141 /* audio handling */
142 int max_endpoints;
143
144 /* backpointer */
145 struct bsc_nat *nat;
146
147 struct bsc_config_stats stats;
148
149 struct llist_head lac_list;
150};
151
152struct bsc_lac_entry {
153 struct llist_head entry;
154 uint16_t lac;
155};
156
Jonathan Santos5a45b152011-08-17 15:33:57 -0400157struct bsc_nat_paging_group {
158 struct llist_head entry;
159
160 /* list of lac entries */
161 struct llist_head lists;
162 int nr;
163};
164
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400165/**
166 * BSCs point of view of endpoints
167 */
168struct bsc_endpoint {
169 /* the operation that is carried out */
170 int transaction_state;
171 /* the pending transaction id */
172 char *transaction_id;
173 /* the bsc we are talking to */
174 struct bsc_connection *bsc;
175};
176
177/**
178 * Statistic for the nat.
179 */
180struct bsc_nat_statistics {
181 struct {
Jonathan Santos5a45b152011-08-17 15:33:57 -0400182 struct osmo_counter *conn;
183 struct osmo_counter *calls;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400184 } sccp;
185
186 struct {
Jonathan Santos5a45b152011-08-17 15:33:57 -0400187 struct osmo_counter *reconn;
188 struct osmo_counter *auth_fail;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400189 } bsc;
190
191 struct {
Jonathan Santos5a45b152011-08-17 15:33:57 -0400192 struct osmo_counter *reconn;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400193 } msc;
194
195 struct {
Jonathan Santos5a45b152011-08-17 15:33:57 -0400196 struct osmo_counter *reconn;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400197 } ussd;
198};
199
200enum bsc_nat_acc_ctr {
201 ACC_LIST_BSC_FILTER,
202 ACC_LIST_NAT_FILTER,
203};
204
205struct bsc_nat_acc_lst {
206 struct llist_head list;
207
208 /* counter */
209 struct rate_ctr_group *stats;
210
211 /* the name of the list */
212 const char *name;
213 struct llist_head fltr_list;
214};
215
216struct bsc_nat_acc_lst_entry {
217 struct llist_head list;
218
219 /* the filter */
220 char *imsi_allow;
221 regex_t imsi_allow_re;
222 char *imsi_deny;
223 regex_t imsi_deny_re;
224};
225
226/**
227 * the structure of the "nat" network
228 */
229struct bsc_nat {
230 /* active SCCP connections that need patching */
231 struct llist_head sccp_connections;
232
233 /* active BSC connections that need patching */
234 struct llist_head bsc_connections;
235
236 /* access lists */
237 struct llist_head access_lists;
238
Jonathan Santos5a45b152011-08-17 15:33:57 -0400239 /* paging groups */
240 struct llist_head paging_groups;
241
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400242 /* known BSC's */
243 struct llist_head bsc_configs;
244 int num_bsc;
245 int bsc_ip_dscp;
246
247 /* MGCP config */
248 struct mgcp_config *mgcp_cfg;
249 uint8_t mgcp_msg[4096];
250 int mgcp_length;
251
252 /* msc things */
Jonathan Santos5a45b152011-08-17 15:33:57 -0400253 struct llist_head dests;
254 struct bsc_msc_dest *main_dest;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400255 struct bsc_msc_connection *msc_con;
256 char *token;
257
258 /* timeouts */
259 int auth_timeout;
260 int ping_timeout;
261 int pong_timeout;
262
263 struct bsc_endpoint *bsc_endpoints;
264
265 /* filter */
266 char *acc_lst_name;
267
268 /* number rewriting */
269 char *num_rewr_name;
Jonathan Santos5a45b152011-08-17 15:33:57 -0400270 struct llist_head num_rewr;
271
272 char *smsc_rewr_name;
273 struct llist_head smsc_rewr;
274 char *tpdest_match_name;
275 struct llist_head tpdest_match;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400276
277 /* USSD messages we want to match */
278 char *ussd_lst_name;
279 char *ussd_query;
Jonathan Santos5a45b152011-08-17 15:33:57 -0400280 regex_t ussd_query_re;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400281 char *ussd_token;
282 char *ussd_local;
Jonathan Santos5a45b152011-08-17 15:33:57 -0400283 struct osmo_fd ussd_listen;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400284 struct bsc_nat_ussd_con *ussd_con;
285
Jonathan Santos5a45b152011-08-17 15:33:57 -0400286 /* for maintainenance */
287 int blocked;
288
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400289 /* statistics */
290 struct bsc_nat_statistics stats;
291};
292
Jonathan Santos5a45b152011-08-17 15:33:57 -0400293struct bsc_nat_ussd_con {
294 struct osmo_wqueue queue;
295 struct bsc_nat *nat;
296 int authorized;
297
298 struct osmo_timer_list auth_timeout;
299};
300
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400301/* create and init the structures */
302struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token);
303struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num);
304void bsc_config_free(struct bsc_config *);
305void bsc_config_add_lac(struct bsc_config *cfg, int lac);
306void bsc_config_del_lac(struct bsc_config *cfg, int lac);
307int bsc_config_handles_lac(struct bsc_config *cfg, int lac);
308
309struct bsc_nat *bsc_nat_alloc(void);
310struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat);
311void bsc_nat_set_msc_ip(struct bsc_nat *bsc, const char *ip);
312
313void sccp_connection_destroy(struct sccp_connections *);
314void bsc_close_connection(struct bsc_connection *);
315
316const char *bsc_con_type_to_string(int type);
317
318/**
319 * parse the given message into the above structure
320 */
321struct bsc_nat_parsed *bsc_nat_parse(struct msgb *msg);
322
323/**
324 * filter based on IP Access header in both directions
325 */
326int bsc_nat_filter_ipa(int direction, struct msgb *msg, struct bsc_nat_parsed *parsed);
327int bsc_nat_vty_init(struct bsc_nat *nat);
Jonathan Santos5a45b152011-08-17 15:33:57 -0400328int bsc_nat_find_paging(struct msgb *msg, const uint8_t **,int *len);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400329
330/**
331 * Content filtering.
332 */
333int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg,
334 struct bsc_nat_parsed *, int *con_type, char **imsi);
335int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
336 struct sccp_connections *con, struct bsc_nat_parsed *parsed);
337
338/**
339 * SCCP patching and handling
340 */
341struct sccp_connections *create_sccp_src_ref(struct bsc_connection *bsc, struct bsc_nat_parsed *parsed);
342int update_sccp_src_ref(struct sccp_connections *sccp, struct bsc_nat_parsed *parsed);
343void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed);
344struct sccp_connections *patch_sccp_src_ref_to_bsc(struct msgb *, struct bsc_nat_parsed *, struct bsc_nat *);
345struct sccp_connections *patch_sccp_src_ref_to_msc(struct msgb *, struct bsc_nat_parsed *, struct bsc_connection *);
346struct sccp_connections *bsc_nat_find_con_by_bsc(struct bsc_nat *, struct sccp_source_reference *);
347
348/**
349 * MGCP/Audio handling
350 */
351int bsc_mgcp_nr_multiplexes(int max_endpoints);
352int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length);
353int bsc_mgcp_assign_patch(struct sccp_connections *, struct msgb *msg);
354void bsc_mgcp_init(struct sccp_connections *);
355void bsc_mgcp_dlcx(struct sccp_connections *);
356void bsc_mgcp_free_endpoints(struct bsc_nat *nat);
357int bsc_mgcp_nat_init(struct bsc_nat *nat);
358
359struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *, int endpoint_number);
360struct msgb *bsc_mgcp_rewrite(char *input, int length, int endp, const char *ip, int port);
361void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg);
362
363void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc);
364int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60]);
365uint32_t bsc_mgcp_extract_ci(const char *resp);
366
367
368int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int id);
Jonathan Santos5a45b152011-08-17 15:33:57 -0400369int bsc_do_write(struct osmo_wqueue *queue, struct msgb *msg, int id);
370int bsc_write_msg(struct osmo_wqueue *queue, struct msgb *msg);
371int bsc_write_cb(struct osmo_fd *bfd, struct msgb *msg);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400372
373/* IMSI allow/deny handling */
Jonathan Santos5a45b152011-08-17 15:33:57 -0400374int bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv) __attribute__ ((warn_unused_result));
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400375struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct bsc_nat *nat, const char *name);
376struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *name);
377void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst);
378
379struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *);
380int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *imsi);
381
382int bsc_nat_msc_is_connected(struct bsc_nat *nat);
383
384int bsc_conn_type_to_ctr(struct sccp_connections *conn);
385
386struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed, struct msgb *msg, uint32_t *len);
387
388/** USSD filtering */
389int bsc_ussd_init(struct bsc_nat *nat);
390int bsc_check_ussd(struct sccp_connections *con, struct bsc_nat_parsed *parsed, struct msgb *msg);
391int bsc_close_ussd_connections(struct bsc_nat *nat);
392
Jonathan Santos5a45b152011-08-17 15:33:57 -0400393struct msgb *bsc_nat_rewrite_msg(struct bsc_nat *nat, struct msgb *msg, struct bsc_nat_parsed *, const char *imsi);
394
395/** paging group handling */
396struct bsc_nat_paging_group *bsc_nat_paging_group_num(struct bsc_nat *nat, int group);
397struct bsc_nat_paging_group *bsc_nat_paging_group_create(struct bsc_nat *nat, int group);
398void bsc_nat_paging_group_delete(struct bsc_nat_paging_group *);
399void bsc_nat_paging_group_add_lac(struct bsc_nat_paging_group *grp, int lac);
400void bsc_nat_paging_group_del_lac(struct bsc_nat_paging_group *grp, int lac);
401
402/**
403 * Number rewriting support below
404 */
405struct bsc_nat_num_rewr_entry {
406 struct llist_head list;
407
408 regex_t msisdn_reg;
409 regex_t num_reg;
410
411 char *replace;
412};
413
414void bsc_nat_num_rewr_entry_adapt(void *ctx, struct llist_head *head, const struct osmo_config_list *);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400415
416#endif