blob: f74cae2a82f7d1ce61917b3d7c00bb126a7f45ce [file] [log] [blame]
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001/*
2 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2010 by On-Waves
4 * 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
26#include <sys/types.h>
27
28#include <osmocore/select.h>
29#include <osmocore/msgb.h>
30#include <osmocore/msgfile.h>
31#include <osmocore/timer.h>
32#include <osmocore/write_queue.h>
33#include <osmocore/rate_ctr.h>
34#include <osmocore/statistics.h>
35#include <osmocore/protocol/gsm_04_08.h>
36
37#include <regex.h>
38
39#define DIR_BSC 1
40#define DIR_MSC 2
41
42struct sccp_source_reference;
43struct sccp_connections;
44struct bsc_nat_parsed;
45struct bsc_nat;
46struct bsc_nat_ussd_con;
47
48enum {
49 NAT_CON_TYPE_NONE,
50 NAT_CON_TYPE_LU,
51 NAT_CON_TYPE_CM_SERV_REQ,
52 NAT_CON_TYPE_PAG_RESP,
53 NAT_CON_TYPE_SSA,
54 NAT_CON_TYPE_LOCAL_REJECT,
55 NAT_CON_TYPE_OTHER,
56};
57
58/*
59 * Per BSC data structure
60 */
61struct bsc_connection {
62 struct llist_head list_entry;
63
64 /* do we know anything about this BSC? */
65 int authenticated;
66
67 /* the fd we use to communicate */
68 struct write_queue write_queue;
69
70 /* the BSS associated */
71 struct bsc_config *cfg;
72
73 /* a timeout node */
74 struct timer_list id_timeout;
75
76 /* pong timeout */
77 struct timer_list ping_timeout;
78 struct timer_list pong_timeout;
79
80 /* mgcp related code */
81 char *_endpoint_status;
82 int number_multiplexes;
83 int max_endpoints;
84 int last_endpoint;
85
86 /* a back pointer */
87 struct bsc_nat *nat;
88};
89
90/**
91 * Stats per BSC
92 */
93struct bsc_config_stats {
94 struct rate_ctr_group *ctrg;
95};
96
97enum bsc_cfg_ctr {
98 BCFG_CTR_SCCP_CONN,
99 BCFG_CTR_SCCP_CALLS,
100 BCFG_CTR_NET_RECONN,
101 BCFG_CTR_DROPPED_SCCP,
102 BCFG_CTR_DROPPED_CALLS,
103 BCFG_CTR_REJECTED_CR,
104 BCFG_CTR_REJECTED_MSG,
105 BCFG_CTR_ILL_PACKET,
106 BCFG_CTR_CON_TYPE_LU,
107 BCFG_CTR_CON_CMSERV_RQ,
108 BCFG_CTR_CON_PAG_RESP,
109 BCFG_CTR_CON_SSA,
110 BCFG_CTR_CON_OTHER,
111};
112
113/**
114 * One BSC entry in the config
115 */
116struct bsc_config {
117 struct llist_head entry;
118
119 char *token;
120 int nr;
121
122 char *description;
123
124 /* imsi white and blacklist */
125 char *acc_lst_name;
126
127 int forbid_paging;
128
129 /* audio handling */
130 int max_endpoints;
131
132 /* backpointer */
133 struct bsc_nat *nat;
134
135 struct bsc_config_stats stats;
136
137 struct llist_head lac_list;
138};
139
140struct bsc_lac_entry {
141 struct llist_head entry;
142 uint16_t lac;
143};
144
145/**
146 * BSCs point of view of endpoints
147 */
148struct bsc_endpoint {
149 /* the operation that is carried out */
150 int transaction_state;
151 /* the pending transaction id */
152 char *transaction_id;
153 /* the bsc we are talking to */
154 struct bsc_connection *bsc;
155};
156
157/**
158 * Statistic for the nat.
159 */
160struct bsc_nat_statistics {
161 struct {
162 struct counter *conn;
163 struct counter *calls;
164 } sccp;
165
166 struct {
167 struct counter *reconn;
168 struct counter *auth_fail;
169 } bsc;
170
171 struct {
172 struct counter *reconn;
173 } msc;
174
175 struct {
176 struct counter *reconn;
177 } ussd;
178};
179
180enum bsc_nat_acc_ctr {
181 ACC_LIST_BSC_FILTER,
182 ACC_LIST_NAT_FILTER,
183};
184
185struct bsc_nat_acc_lst {
186 struct llist_head list;
187
188 /* counter */
189 struct rate_ctr_group *stats;
190
191 /* the name of the list */
192 const char *name;
193 struct llist_head fltr_list;
194};
195
196struct bsc_nat_acc_lst_entry {
197 struct llist_head list;
198
199 /* the filter */
200 char *imsi_allow;
201 regex_t imsi_allow_re;
202 char *imsi_deny;
203 regex_t imsi_deny_re;
204};
205
206/**
207 * the structure of the "nat" network
208 */
209struct bsc_nat {
210 /* active SCCP connections that need patching */
211 struct llist_head sccp_connections;
212
213 /* active BSC connections that need patching */
214 struct llist_head bsc_connections;
215
216 /* access lists */
217 struct llist_head access_lists;
218
219 /* known BSC's */
220 struct llist_head bsc_configs;
221 int num_bsc;
222 int bsc_ip_dscp;
223
224 /* MGCP config */
225 struct mgcp_config *mgcp_cfg;
226 uint8_t mgcp_msg[4096];
227 int mgcp_length;
228
229 /* msc things */
230 char *msc_ip;
231 int msc_port;
232 struct bsc_msc_connection *msc_con;
233 char *token;
234
235 /* timeouts */
236 int auth_timeout;
237 int ping_timeout;
238 int pong_timeout;
239
240 struct bsc_endpoint *bsc_endpoints;
241
242 /* filter */
243 char *acc_lst_name;
244
245 /* number rewriting */
246 char *num_rewr_name;
247 struct msg_entries *num_rewr;
248
249 /* USSD messages we want to match */
250 char *ussd_lst_name;
251 char *ussd_query;
252 char *ussd_token;
253 char *ussd_local;
254 struct bsc_fd ussd_listen;
255 struct bsc_nat_ussd_con *ussd_con;
256
257 /* statistics */
258 struct bsc_nat_statistics stats;
259};
260
261/* create and init the structures */
262struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token);
263struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num);
264void bsc_config_free(struct bsc_config *);
265void bsc_config_add_lac(struct bsc_config *cfg, int lac);
266void bsc_config_del_lac(struct bsc_config *cfg, int lac);
267int bsc_config_handles_lac(struct bsc_config *cfg, int lac);
268
269struct bsc_nat *bsc_nat_alloc(void);
270struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat);
271void bsc_nat_set_msc_ip(struct bsc_nat *bsc, const char *ip);
272
273void sccp_connection_destroy(struct sccp_connections *);
274void bsc_close_connection(struct bsc_connection *);
275
276const char *bsc_con_type_to_string(int type);
277
278/**
279 * parse the given message into the above structure
280 */
281struct bsc_nat_parsed *bsc_nat_parse(struct msgb *msg);
282
283/**
284 * filter based on IP Access header in both directions
285 */
286int bsc_nat_filter_ipa(int direction, struct msgb *msg, struct bsc_nat_parsed *parsed);
287int bsc_nat_vty_init(struct bsc_nat *nat);
288struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg, int *_lac);
289
290/**
291 * Content filtering.
292 */
293int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg,
294 struct bsc_nat_parsed *, int *con_type, char **imsi);
295int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
296 struct sccp_connections *con, struct bsc_nat_parsed *parsed);
297
298/**
299 * SCCP patching and handling
300 */
301struct sccp_connections *create_sccp_src_ref(struct bsc_connection *bsc, struct bsc_nat_parsed *parsed);
302int update_sccp_src_ref(struct sccp_connections *sccp, struct bsc_nat_parsed *parsed);
303void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed);
304struct sccp_connections *patch_sccp_src_ref_to_bsc(struct msgb *, struct bsc_nat_parsed *, struct bsc_nat *);
305struct sccp_connections *patch_sccp_src_ref_to_msc(struct msgb *, struct bsc_nat_parsed *, struct bsc_connection *);
306struct sccp_connections *bsc_nat_find_con_by_bsc(struct bsc_nat *, struct sccp_source_reference *);
307
308/**
309 * MGCP/Audio handling
310 */
311int bsc_mgcp_nr_multiplexes(int max_endpoints);
312int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length);
313int bsc_mgcp_assign_patch(struct sccp_connections *, struct msgb *msg);
314void bsc_mgcp_init(struct sccp_connections *);
315void bsc_mgcp_dlcx(struct sccp_connections *);
316void bsc_mgcp_free_endpoints(struct bsc_nat *nat);
317int bsc_mgcp_nat_init(struct bsc_nat *nat);
318
319struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *, int endpoint_number);
320struct msgb *bsc_mgcp_rewrite(char *input, int length, int endp, const char *ip, int port);
321void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg);
322
323void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc);
324int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60]);
325uint32_t bsc_mgcp_extract_ci(const char *resp);
326
327
328int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int id);
329int bsc_do_write(struct write_queue *queue, struct msgb *msg, int id);
330int bsc_write_msg(struct write_queue *queue, struct msgb *msg);
331int bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg);
332
333/* IMSI allow/deny handling */
334void bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv);
335struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct bsc_nat *nat, const char *name);
336struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *name);
337void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst);
338
339struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *);
340int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *imsi);
341
342int bsc_nat_msc_is_connected(struct bsc_nat *nat);
343
344int bsc_conn_type_to_ctr(struct sccp_connections *conn);
345
346struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed, struct msgb *msg, uint32_t *len);
347
348/** USSD filtering */
349int bsc_ussd_init(struct bsc_nat *nat);
350int bsc_check_ussd(struct sccp_connections *con, struct bsc_nat_parsed *parsed, struct msgb *msg);
351int bsc_close_ussd_connections(struct bsc_nat *nat);
352
353struct msgb *bsc_nat_rewrite_setup(struct bsc_nat *nat, struct msgb *msg, struct bsc_nat_parsed *, const char *imsi);
354
355#endif