blob: baa950ccba55a06d5cebdb64ae08689a699eeb7c [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 Freyther9f8f3d02010-02-07 13:08:09 +010028
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080029#include <osmocore/select.h>
30#include <osmocore/msgb.h>
31#include <osmocore/timer.h>
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +080032#include <osmocore/write_queue.h>
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080033#include <osmocore/rate_ctr.h>
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020034#include <osmocore/statistics.h>
Holger Hans Peter Freythera3967572010-09-29 02:30:50 +080035#include <osmocore/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 Freyther3d387422010-10-16 16:35:00 +020042struct sccp_source_reference;
Holger Hans Peter Freytherc2b31ed2010-07-31 05:17:17 +080043struct sccp_connections;
44struct bsc_nat_parsed;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +080045struct bsc_nat;
46
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +080047enum {
48 NAT_CON_TYPE_NONE,
49 NAT_CON_TYPE_LU,
50 NAT_CON_TYPE_CM_SERV_REQ,
51 NAT_CON_TYPE_PAG_RESP,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +080052 NAT_CON_TYPE_SSA,
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +080053 NAT_CON_TYPE_LOCAL_REJECT,
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +080054 NAT_CON_TYPE_OTHER,
55};
56
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080057/*
Holger Hans Peter Freyther9f8f3d02010-02-07 13:08:09 +010058 * Per BSC data structure
59 */
60struct bsc_connection {
61 struct llist_head list_entry;
62
63 /* do we know anything about this BSC? */
64 int authenticated;
65
66 /* the fd we use to communicate */
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +080067 struct write_queue write_queue;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080068
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +020069 /* the BSS associated */
70 struct bsc_config *cfg;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080071
72 /* a timeout node */
73 struct timer_list id_timeout;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +080074
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +080075 /* pong timeout */
76 struct timer_list ping_timeout;
77 struct timer_list pong_timeout;
78
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +080079 /* mgcp related code */
Holger Hans Peter Freyther85902a42010-09-01 09:31:46 +080080 int endpoint_status[32];
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +080081 int last_endpoint;
82
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +080083 /* a back pointer */
84 struct bsc_nat *nat;
Holger Hans Peter Freyther9f8f3d02010-02-07 13:08:09 +010085};
86
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080087/**
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020088 * Stats per BSC
89 */
90struct bsc_config_stats {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080091 struct rate_ctr_group *ctrg;
92};
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020093
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +080094enum bsc_cfg_ctr {
95 BCFG_CTR_SCCP_CONN,
96 BCFG_CTR_SCCP_CALLS,
97 BCFG_CTR_NET_RECONN,
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +080098 BCFG_CTR_DROPPED_SCCP,
99 BCFG_CTR_DROPPED_CALLS,
Holger Hans Peter Freytheree884962010-09-25 17:58:22 +0800100 BCFG_CTR_REJECTED_CR,
101 BCFG_CTR_REJECTED_MSG,
102 BCFG_CTR_ILL_PACKET,
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800103 BCFG_CTR_CON_TYPE_LU,
104 BCFG_CTR_CON_CMSERV_RQ,
105 BCFG_CTR_CON_PAG_RESP,
Holger Hans Peter Freyther74dc3032010-09-29 02:47:29 +0800106 BCFG_CTR_CON_SSA,
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800107 BCFG_CTR_CON_OTHER,
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200108};
109
110/**
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800111 * One BSC entry in the config
112 */
113struct bsc_config {
114 struct llist_head entry;
115
116 char *token;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800117 int nr;
118
Holger Hans Peter Freytherb6061012010-05-14 22:06:28 +0800119 char *description;
120
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200121 /* imsi white and blacklist */
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800122 char *acc_lst_name;
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200123
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800124 int forbid_paging;
125
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200126 /* backpointer */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800127 struct bsc_nat *nat;
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200128
129 struct bsc_config_stats stats;
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800130
131 struct llist_head lac_list;
132};
133
134struct bsc_lac_entry {
135 struct llist_head entry;
136 uint16_t lac;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800137};
138
139/**
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200140 * BSCs point of view of endpoints
141 */
142struct bsc_endpoint {
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800143 /* the operation that is carried out */
144 int transaction_state;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200145 /* the pending transaction id */
146 char *transaction_id;
147 /* the bsc we are talking to */
148 struct bsc_connection *bsc;
149};
150
151/**
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200152 * Statistic for the nat.
153 */
154struct bsc_nat_statistics {
155 struct {
156 struct counter *conn;
157 struct counter *calls;
158 } sccp;
159
160 struct {
161 struct counter *reconn;
162 struct counter *auth_fail;
163 } bsc;
164
165 struct {
166 struct counter *reconn;
167 } msc;
168};
169
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800170enum bsc_nat_acc_ctr {
171 ACC_LIST_BSC_FILTER,
172 ACC_LIST_NAT_FILTER,
173};
174
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800175struct bsc_nat_acc_lst {
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800176 struct llist_head list;
177
Holger Hans Peter Freyther2f1a9842010-09-25 06:14:52 +0800178 /* counter */
179 struct rate_ctr_group *stats;
180
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800181 /* the name of the list */
182 const char *name;
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800183 struct llist_head fltr_list;
184};
185
186struct bsc_nat_acc_lst_entry {
187 struct llist_head list;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800188
189 /* the filter */
190 char *imsi_allow;
191 regex_t imsi_allow_re;
192 char *imsi_deny;
193 regex_t imsi_deny_re;
194};
195
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200196/**
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800197 * the structure of the "nat" network
198 */
199struct bsc_nat {
200 /* active SCCP connections that need patching */
201 struct llist_head sccp_connections;
202
203 /* active BSC connections that need patching */
204 struct llist_head bsc_connections;
205
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800206 /* access lists */
207 struct llist_head access_lists;
208
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800209 /* known BSC's */
210 struct llist_head bsc_configs;
211 int num_bsc;
Holger Hans Peter Freyther6b771072010-07-27 19:21:53 +0800212 int bsc_ip_dscp;
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800213
214 /* MGCP config */
215 struct mgcp_config *mgcp_cfg;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800216 uint8_t mgcp_msg[4096];
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200217 int mgcp_length;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200218
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200219 /* msc things */
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800220 char *msc_ip;
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +0200221 int msc_port;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +0800222 struct bsc_msc_connection *msc_con;
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800223 char *token;
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200224
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800225 /* timeouts */
226 int auth_timeout;
227 int ping_timeout;
228 int pong_timeout;
229
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200230 struct bsc_endpoint *bsc_endpoints;
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200231
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200232 /* filter */
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800233 char *acc_lst_name;
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200234
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200235 /* statistics */
236 struct bsc_nat_statistics stats;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800237};
238
239/* create and init the structures */
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800240struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800241struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800242void bsc_config_add_lac(struct bsc_config *cfg, int lac);
243void bsc_config_del_lac(struct bsc_config *cfg, int lac);
244int bsc_config_handles_lac(struct bsc_config *cfg, int lac);
245
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800246struct bsc_nat *bsc_nat_alloc(void);
247struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800248void bsc_nat_set_msc_ip(struct bsc_nat *bsc, const char *ip);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800249
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200250void sccp_connection_destroy(struct sccp_connections *);
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800251void bsc_close_connection(struct bsc_connection *);
Holger Hans Peter Freyther9f8f3d02010-02-07 13:08:09 +0100252
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800253const char *bsc_con_type_to_string(int type);
254
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800255/**
256 * parse the given message into the above structure
257 */
258struct bsc_nat_parsed *bsc_nat_parse(struct msgb *msg);
259
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800260/**
261 * filter based on IP Access header in both directions
262 */
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100263int bsc_nat_filter_ipa(int direction, struct msgb *msg, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800264int bsc_nat_vty_init(struct bsc_nat *nat);
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200265struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg, int *_lac);
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800266
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800267/**
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800268 * Content filtering.
269 */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800270int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg,
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800271 struct bsc_nat_parsed *, int *con_type, char **imsi);
Holger Hans Peter Freyther74e0a1b2010-09-15 01:11:08 +0800272int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg,
273 struct sccp_connections *con, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800274
275/**
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800276 * SCCP patching and handling
277 */
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800278struct sccp_connections *create_sccp_src_ref(struct bsc_connection *bsc, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800279int update_sccp_src_ref(struct sccp_connections *sccp, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800280void 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 +0800281struct 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 +0800282struct sccp_connections *patch_sccp_src_ref_to_msc(struct msgb *, struct bsc_nat_parsed *, struct bsc_connection *);
Holger Hans Peter Freyther3d387422010-10-16 16:35:00 +0200283struct sccp_connections *bsc_nat_find_con_by_bsc(struct bsc_nat *, struct sccp_source_reference *);
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800284
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800285/**
286 * MGCP/Audio handling
287 */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800288int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length);
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800289int bsc_mgcp_assign_patch(struct sccp_connections *, struct msgb *msg);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800290void bsc_mgcp_init(struct sccp_connections *);
291void bsc_mgcp_dlcx(struct sccp_connections *);
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200292void bsc_mgcp_free_endpoints(struct bsc_nat *nat);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800293int bsc_mgcp_nat_init(struct bsc_nat *nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800294
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800295struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *, int endpoint_number);
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800296struct msgb *bsc_mgcp_rewrite(char *input, int length, int endp, const char *ip, int port);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200297void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg);
298
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200299void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200300int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60]);
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800301uint32_t bsc_mgcp_extract_ci(const char *resp);
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200302
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200303
304int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int id);
Holger Hans Peter Freyther4d44fc52010-10-13 17:56:22 +0200305int bsc_do_write(struct write_queue *queue, struct msgb *msg, int id);
Holger Hans Peter Freyther7e8da132010-10-16 17:33:46 +0200306int bsc_write_msg(struct write_queue *queue, struct msgb *msg);
Holger Hans Peter Freyther69cfa172010-10-13 20:37:13 +0200307int bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg);
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200308
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800309/* IMSI allow/deny handling */
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800310void bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv);
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800311struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct bsc_nat *nat, const char *name);
312struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *name);
313void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst);
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800314
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800315struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *);
316
Holger Hans Peter Freyther20ee3122010-07-05 14:39:44 +0800317int bsc_nat_msc_is_connected(struct bsc_nat *nat);
318
Holger Hans Peter Freyther463dc622010-10-03 19:41:42 +0800319int bsc_conn_type_to_ctr(struct sccp_connections *conn);
320
Holger Hans Peter Freythera3967572010-09-29 02:30:50 +0800321struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed, struct msgb *msg, uint32_t *len);
322
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800323/** USSD filtering */
324int bsc_check_ussd(struct sccp_connections *con, struct bsc_nat_parsed *parsed, struct msgb *msg);
325
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800326#endif