blob: 5f730fe6b9622221a948346806bd8345b1153030 [file] [log] [blame]
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +08001/*
2 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freyther98e49d42010-06-15 18:46:56 +08003 * (C) 2010 by On-Waves
Holger Hans Peter Freyther57adba52010-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
25#include <sys/types.h>
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +080026#include <sccp/sccp_types.h>
Holger Hans Peter Freyther2ef30aa2010-02-07 13:08:09 +010027
Holger Hans Peter Freyther3b960892010-06-15 19:06:18 +080028#include <osmocore/select.h>
29#include <osmocore/msgb.h>
30#include <osmocore/timer.h>
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +080031#include <osmocore/write_queue.h>
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +080032
Holger Hans Peter Freytherbbf6b652010-01-30 11:53:30 +010033#define DIR_BSC 1
34#define DIR_MSC 2
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +080035
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +080036struct bsc_nat;
37
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +080038/*
39 * For the NAT we will need to analyze and later patch
40 * the received message. This would require us to parse
41 * the IPA and SCCP header twice. Instead of doing this
42 * we will have one analyze structure and have the patching
43 * and filter operate on the same structure.
44 */
45struct bsc_nat_parsed {
46 /* ip access prototype */
47 int ipa_proto;
48
49 /* source local reference */
50 struct sccp_source_reference *src_local_ref;
51
52 /* destination local reference */
53 struct sccp_source_reference *dest_local_ref;
54
55 /* called ssn number */
56 int called_ssn;
57
58 /* calling ssn number */
59 int calling_ssn;
60
61 /* sccp message type */
62 int sccp_type;
63
64 /* bssap type, e.g. 0 for BSS Management */
65 int bssap;
66
67 /* the gsm0808 message type */
68 int gsm_type;
69};
70
Holger Hans Peter Freyther2ef30aa2010-02-07 13:08:09 +010071/*
72 * Per BSC data structure
73 */
74struct bsc_connection {
75 struct llist_head list_entry;
76
77 /* do we know anything about this BSC? */
78 int authenticated;
79
80 /* the fd we use to communicate */
Holger Hans Peter Freyther84010542010-06-15 18:47:10 +080081 struct write_queue write_queue;
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +080082
83 /* the LAC assigned to this connection */
84 unsigned int lac;
85
86 /* a timeout node */
87 struct timer_list id_timeout;
Holger Hans Peter Freytherde557662010-06-15 18:46:19 +080088
89 /* a back pointer */
90 struct bsc_nat *nat;
Holger Hans Peter Freyther2ef30aa2010-02-07 13:08:09 +010091};
92
93/*
94 * Per SCCP source local reference patch table. It needs to
95 * be updated on new SCCP connections, connection confirm and reject,
96 * and on the loss of the BSC connection.
97 */
98struct sccp_connections {
99 struct llist_head list_entry;
100
101 struct bsc_connection *bsc;
102
103 struct sccp_source_reference real_ref;
104 struct sccp_source_reference patched_ref;
Holger Hans Peter Freythera25e3ec2010-03-29 17:18:42 +0200105 struct sccp_source_reference remote_ref;
Holger Hans Peter Freyther2ef30aa2010-02-07 13:08:09 +0100106};
107
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800108/**
109 * One BSC entry in the config
110 */
111struct bsc_config {
112 struct llist_head entry;
113
114 char *token;
115 unsigned int lac;
116 int nr;
117
118 struct bsc_nat *nat;
119};
120
121/**
122 * the structure of the "nat" network
123 */
124struct bsc_nat {
125 /* active SCCP connections that need patching */
126 struct llist_head sccp_connections;
127
128 /* active BSC connections that need patching */
129 struct llist_head bsc_connections;
130
131 /* known BSC's */
132 struct llist_head bsc_configs;
133 int num_bsc;
134};
135
136/* create and init the structures */
137struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac);
138struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num);
Holger Hans Peter Freyther090a4d82010-06-15 18:48:01 +0800139struct bsc_nat *bsc_nat_alloc(void);
140struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat);
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800141
Holger Hans Peter Freyther2ef30aa2010-02-07 13:08:09 +0100142
Holger Hans Peter Freytherf75a6802010-06-15 18:45:38 +0800143/**
144 * parse the given message into the above structure
145 */
146struct bsc_nat_parsed *bsc_nat_parse(struct msgb *msg);
147
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +0800148/**
149 * filter based on IP Access header in both directions
150 */
Holger Hans Peter Freytherbbf6b652010-01-30 11:53:30 +0100151int bsc_nat_filter_ipa(int direction, struct msgb *msg, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freyther5e547882010-06-15 18:46:11 +0800152int bsc_nat_vty_init(struct bsc_nat *nat);
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +0800153
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800154/**
155 * SCCP patching and handling
156 */
157int create_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freythera25e3ec2010-03-29 17:18:42 +0200158int update_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed);
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800159void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed);
160struct bsc_connection *patch_sccp_src_ref_to_bsc(struct msgb *, struct bsc_nat_parsed *, struct bsc_nat *);
161struct bsc_connection *patch_sccp_src_ref_to_msc(struct msgb *, struct bsc_nat_parsed *, struct bsc_nat *);
162
Holger Hans Peter Freyther57adba52010-06-15 18:45:26 +0800163#endif