blob: bb2d5df95c789faac0332359e1fbf5a01923804e [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 Freytherb2c38eb2010-06-17 18:16:00 +080034#include <osmocore/rate_ctr.h>
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020035#include <osmocore/statistics.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 Freytherd131b792010-03-31 07:30:58 +020042#define NAT_IPAC_PROTO_MGCP 0xfc
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +080043
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +080044struct bsc_nat;
45
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +080046enum {
47 NAT_CON_TYPE_NONE,
48 NAT_CON_TYPE_LU,
49 NAT_CON_TYPE_CM_SERV_REQ,
50 NAT_CON_TYPE_PAG_RESP,
Holger Hans Peter Freytherb71c23b2010-05-16 20:43:52 +080051 NAT_CON_TYPE_LOCAL_REJECT,
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +080052 NAT_CON_TYPE_OTHER,
53};
54
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080055/*
56 * For the NAT we will need to analyze and later patch
57 * the received message. This would require us to parse
58 * the IPA and SCCP header twice. Instead of doing this
59 * we will have one analyze structure and have the patching
60 * and filter operate on the same structure.
61 */
62struct bsc_nat_parsed {
63 /* ip access prototype */
64 int ipa_proto;
65
66 /* source local reference */
67 struct sccp_source_reference *src_local_ref;
68
69 /* destination local reference */
70 struct sccp_source_reference *dest_local_ref;
71
72 /* called ssn number */
73 int called_ssn;
74
75 /* calling ssn number */
76 int calling_ssn;
77
78 /* sccp message type */
79 int sccp_type;
80
81 /* bssap type, e.g. 0 for BSS Management */
82 int bssap;
83
84 /* the gsm0808 message type */
85 int gsm_type;
86};
87
Holger Hans Peter Freyther9f8f3d02010-02-07 13:08:09 +010088/*
89 * Per BSC data structure
90 */
91struct bsc_connection {
92 struct llist_head list_entry;
93
94 /* do we know anything about this BSC? */
95 int authenticated;
96
97 /* the fd we use to communicate */
Holger Hans Peter Freythered07a3f2010-06-15 18:47:10 +080098 struct write_queue write_queue;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080099
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200100 /* the BSS associated */
101 struct bsc_config *cfg;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800102
103 /* a timeout node */
104 struct timer_list id_timeout;
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800105
Holger Hans Peter Freyther906c15e2010-05-02 19:28:59 +0800106 /* pong timeout */
107 struct timer_list ping_timeout;
108 struct timer_list pong_timeout;
109
Holger Hans Peter Freytheraa698242010-06-15 18:46:19 +0800110 /* a back pointer */
111 struct bsc_nat *nat;
Holger Hans Peter Freyther9f8f3d02010-02-07 13:08:09 +0100112};
113
114/*
115 * Per SCCP source local reference patch table. It needs to
116 * be updated on new SCCP connections, connection confirm and reject,
117 * and on the loss of the BSC connection.
118 */
119struct sccp_connections {
120 struct llist_head list_entry;
121
122 struct bsc_connection *bsc;
123
124 struct sccp_source_reference real_ref;
125 struct sccp_source_reference patched_ref;
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200126 struct sccp_source_reference remote_ref;
Holger Hans Peter Freyther4c683d12010-04-23 14:13:27 +0800127 int has_remote_ref;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800128
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800129 /* status */
130 int con_type;
Holger Hans Peter Freytherc58da4b2010-05-16 16:36:36 +0800131 int con_local;
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800132
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800133 /* GSM audio handling. That is 32 * multiplex + ts */
Holger Hans Peter Freyther959bbcf2010-04-22 20:12:13 +0800134 int crcx;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800135 int msc_timeslot;
136 int bsc_timeslot;
Holger Hans Peter Freytherc14e09b2010-06-15 18:51:49 +0800137
138 /* timeout handling */
139 struct timespec creation_time;
Holger Hans Peter Freyther9f8f3d02010-02-07 13:08:09 +0100140};
141
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800142/**
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200143 * Stats per BSC
144 */
145struct bsc_config_stats {
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800146 struct rate_ctr_group *ctrg;
147};
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200148
Holger Hans Peter Freytherb2c38eb2010-06-17 18:16:00 +0800149enum bsc_cfg_ctr {
150 BCFG_CTR_SCCP_CONN,
151 BCFG_CTR_SCCP_CALLS,
152 BCFG_CTR_NET_RECONN,
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200153};
154
155/**
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800156 * One BSC entry in the config
157 */
158struct bsc_config {
159 struct llist_head entry;
160
161 char *token;
162 unsigned int lac;
163 int nr;
164
Holger Hans Peter Freytherb6061012010-05-14 22:06:28 +0800165 char *description;
166
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200167 /* imsi white and blacklist */
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800168 char *acc_lst_name;
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200169
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800170 int forbid_paging;
171
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200172 /* backpointer */
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800173 struct bsc_nat *nat;
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200174
175 struct bsc_config_stats stats;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800176};
177
178/**
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200179 * BSCs point of view of endpoints
180 */
181struct bsc_endpoint {
182 /* the pending transaction id */
183 char *transaction_id;
184 /* the bsc we are talking to */
185 struct bsc_connection *bsc;
186};
187
188/**
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200189 * Statistic for the nat.
190 */
191struct bsc_nat_statistics {
192 struct {
193 struct counter *conn;
194 struct counter *calls;
195 } sccp;
196
197 struct {
198 struct counter *reconn;
199 struct counter *auth_fail;
200 } bsc;
201
202 struct {
203 struct counter *reconn;
204 } msc;
205};
206
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800207struct bsc_nat_acc_lst {
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800208 struct llist_head list;
209
210 /* the name of the list */
211 const char *name;
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800212 struct llist_head fltr_list;
213};
214
215struct bsc_nat_acc_lst_entry {
216 struct llist_head list;
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800217
218 /* the filter */
219 char *imsi_allow;
220 regex_t imsi_allow_re;
221 char *imsi_deny;
222 regex_t imsi_deny_re;
223};
224
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200225/**
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800226 * the structure of the "nat" network
227 */
228struct bsc_nat {
229 /* active SCCP connections that need patching */
230 struct llist_head sccp_connections;
231
232 /* active BSC connections that need patching */
233 struct llist_head bsc_connections;
234
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800235 /* access lists */
236 struct llist_head access_lists;
237
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800238 /* known BSC's */
239 struct llist_head bsc_configs;
240 int num_bsc;
Holger Hans Peter Freyther078321a2010-05-31 10:36:35 +0800241 int bsc_ip_tos;
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800242
243 /* MGCP config */
244 struct mgcp_config *mgcp_cfg;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200245 struct write_queue mgcp_queue;
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200246 u_int8_t mgcp_msg[4096];
247 int mgcp_length;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200248
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200249 /* msc things */
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800250 char *msc_ip;
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +0200251 int msc_port;
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200252 int first_contact;
Holger Hans Peter Freytheraad82ce2010-05-11 19:07:39 +0800253 struct bsc_msc_connection *msc_con;
Holger Hans Peter Freythere635dab2010-05-15 00:14:58 +0800254 char *token;
Holger Hans Peter Freytherb7527612010-04-07 11:20:36 +0200255
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800256 /* timeouts */
257 int auth_timeout;
258 int ping_timeout;
259 int pong_timeout;
260
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200261 struct bsc_endpoint *bsc_endpoints;
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200262
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200263 /* filter */
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800264 char *acc_lst_name;
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200265
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200266 /* statistics */
267 struct bsc_nat_statistics stats;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800268};
269
270/* create and init the structures */
271struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac);
272struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +0800273struct bsc_nat *bsc_nat_alloc(void);
274struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800275void bsc_nat_set_msc_ip(struct bsc_nat *bsc, const char *ip);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800276
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200277void sccp_connection_destroy(struct sccp_connections *);
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800278void bsc_close_connection(struct bsc_connection *);
Holger Hans Peter Freyther9f8f3d02010-02-07 13:08:09 +0100279
Holger Hans Peter Freyther234d3122010-05-16 02:06:11 +0800280const char *bsc_con_type_to_string(int type);
281
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800282/**
283 * parse the given message into the above structure
284 */
285struct bsc_nat_parsed *bsc_nat_parse(struct msgb *msg);
286
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800287/**
288 * filter based on IP Access header in both directions
289 */
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100290int bsc_nat_filter_ipa(int direction, struct msgb *msg, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800291int bsc_nat_vty_init(struct bsc_nat *nat);
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200292struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg, int *_lac);
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800293
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800294/**
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800295 * Content filtering.
296 */
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800297int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg,
298 struct bsc_nat_parsed *, int *con_type);
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800299
300/**
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800301 * SCCP patching and handling
302 */
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800303struct sccp_connections *create_sccp_src_ref(struct bsc_connection *bsc, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800304int update_sccp_src_ref(struct sccp_connections *sccp, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800305void 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 +0800306struct 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 +0800307struct 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 +0800308
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800309/**
310 * MGCP/Audio handling
311 */
Holger Hans Peter Freytherd131b792010-03-31 07:30:58 +0200312int bsc_write_mgcp(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800313int bsc_mgcp_assign(struct sccp_connections *, struct msgb *msg);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800314void bsc_mgcp_init(struct sccp_connections *);
315void bsc_mgcp_dlcx(struct sccp_connections *);
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200316void bsc_mgcp_free_endpoints(struct bsc_nat *nat);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800317int bsc_mgcp_nat_init(struct bsc_nat *nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800318
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800319struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *, int endpoint_number);
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200320struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200321void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg);
322
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200323void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200324int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60]);
325int bsc_mgcp_extract_ci(const char *resp);
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200326
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200327
328int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int id);
329
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800330/* IMSI allow/deny handling */
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800331void bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv);
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800332struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct bsc_nat *nat, const char *name);
333struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *name);
334void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst);
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800335
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800336struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *);
337
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800338#endif