aboutsummaryrefslogtreecommitdiffstats
path: root/src/links.c
blob: 24a1738dd1515708c4f4bc53daa68deb092a1992 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
 * (C) 2010 by On-Waves
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <mtp_data.h>
#include <bsc_data.h>
#include <cellmgr_debug.h>
#include <snmp_mtp.h>

struct link_data *linkset_find_link(struct bsc_data *bsc, int link_index)
{
	struct link_data *link;

	llist_for_each_entry(link, &bsc->links, entry)
		if (link->link_index == link_index)
			return link;

	return NULL;
}

void link_stop_all(struct bsc_data *bsc)
{
	struct link_data *link;

	llist_for_each_entry(link, &bsc->links, entry)
		mtp_link_stop(link->the_link);
}

void link_reset_all(struct bsc_data *bsc)
{
	struct link_data *link;

	llist_for_each_entry(link, &bsc->links, entry)
		mtp_link_reset(link->the_link);
}

void link_start_all(struct bsc_data *bsc)
{
	struct link_data *link;

	llist_for_each_entry(link, &bsc->links, entry)
		link->start(link);
}

void link_shutdown_all(struct bsc_data *bsc)
{
	struct link_data *link;

	llist_for_each_entry(link, &bsc->links, entry)
		link->shutdown(link);
}

void link_set_pcap_fd(struct bsc_data *bsc)
{
	struct link_data *link;

	llist_for_each_entry(link, &bsc->links, entry)
		link->pcap_fd = bsc->pcap_fd;
}

void link_set_reset_timeout(struct bsc_data *bsc)
{
	struct link_data *link;

	llist_for_each_entry(link, &bsc->links, entry)
		link->udp.reset_timeout = bsc->udp_reset_timeout;
}

static void start_rest(void *start)
{
	struct bsc_data *bsc = start;
	bsc->setup = 1;

	if (msc_init(bsc, 1) != 0) {
		fprintf(stderr, "Failed to init MSC part.\n");
		exit(3);
	}

	link_start_all(bsc);
}

int link_setup_start(struct bsc_data *bsc)
{
	struct link_data *link;

	if (llist_empty(&bsc->links)) {
		LOGP(DINP, LOGL_ERROR, "No links defined.\n");
		return -1;
	}

	if (link_udp_network_init(bsc) != 0)
		return -1;

	llist_for_each_entry(link, &bsc->links, entry) {
		link->the_link = mtp_link_alloc();
		link->the_link->data = link;
		link->the_link->dpc = bsc->dpc;
		link->the_link->opc = bsc->opc;
		link->the_link->sccp_opc = bsc->sccp_opc > -1 ? bsc->sccp_opc : bsc->opc;
		link->the_link->link = 0;
		link->the_link->sltm_once = bsc->once;
		link->the_link->ni = bsc->ni_ni;
		link->the_link->spare = bsc->ni_spare;
		link->bsc = bsc;
		link->pcap_fd = bsc->pcap_fd;
		link->udp.reset_timeout = bsc->udp_reset_timeout;

		if (!link->udp.udp_ip) {
			LOGP(DINP, LOGL_ERROR, "Need to set a UDP IP on %d.\n",
			     link->link_index);
			return -1;
		}

		if (!link->udp.udp_port) {
			LOGP(DINP, LOGL_ERROR, "Need to set a UDP Port on %d.\n",
			     link->link_index);
			return -1;
		}
	}

	LOGP(DINP, LOGL_NOTICE, "Using UDP MTP mode.\n");

	llist_for_each_entry(link, &bsc->links, entry) {
		/* setup SNMP first, it is blocking */
		link->udp.session = snmp_mtp_session_create(link->udp.udp_ip);
		if (!link->udp.session) {
			LOGP(DINP, LOGL_ERROR,
			     "Failed to initialize SNMP on %d\n", link->link_index);
			return -1;
		}

		/* now connect to the transport */
		if (link_udp_init(link, link->udp.udp_ip, link->udp.udp_port) != 0) {
			LOGP(DINP, LOGL_ERROR,
			     "Failed to init the link on %d\n", link->link_index);
			return -1;
		}

		/*
		 * We will ask the MTP link to be taken down for two
		 * timeouts of the BSC to make sure we are missing the
		 * SLTM and it begins a reset. Then we will take it up
		 * again and do the usual business.
		 */
		snmp_mtp_deactivate(link->udp.session, link->link_index);
	}

	bsc->start_timer.cb = start_rest;
	bsc->start_timer.data = bsc;
	bsc_schedule_timer(&bsc->start_timer, bsc->udp_reset_timeout, 0);
	LOGP(DMSC, LOGL_NOTICE, "Making sure SLTM will timeout.\n");

	return 0;
}

/*
 * methods called from the MTP Level3 part
 */
void mtp_link_submit(struct mtp_link *_link, struct msgb *msg)
{
	struct link_data *link = _link->data;
	link->write(link, msg);
}

void mtp_link_restart(struct mtp_link *_link)
{
	struct link_data *link = _link->data;

	LOGP(DINP, LOGL_ERROR, "Need to restart the SS7 link.\n");
	link->reset(link);
}

void mtp_link_sccp_down(struct mtp_link *_link)
{
}

static struct mtp_link *find_for_sls(struct bsc_data *bsc, int sls)
{
	struct link_data *link;

	llist_for_each_entry(link, &bsc->links, entry)
		return link->the_link;

	return NULL;
}

int linkset_send_bsc_msg(struct bsc_data *bsc, int sls, struct msgb *msg)
{
	return linkset_send_bsc_data(bsc, sls, msg->l2h, msgb_l2len(msg));
}

int linkset_send_bsc_data(struct bsc_data *bsc, int sls, const uint8_t *data, int len)
{
	struct mtp_link *link;

	link = find_for_sls(bsc, sls);
	if (!link) {
		LOGP(DINP, LOGL_ERROR, "No MTPLink for SLS: %d\n", sls);
		return 0;
	}

	if (!link->sccp_up) {
		LOGP(DINP, LOGL_ERROR, "SCCP is not up on the linkset.\n");
		return 0;
	}

	if (mtp_link_submit_sccp_data(link, sls, data, len) != 0)
		LOGP(DMSC, LOGL_ERROR, "Could not forward SCCP message.\n");
	return 0;
}

/* One of the links of the linkset failed */
void bsc_link_down(struct link_data *data)
{
	struct link_data *link;
	struct bsc_data *bsc;
	int one_up = 0;

	bsc = data->bsc;
	data->the_link->available = 0;

	llist_for_each_entry(link, &bsc->links, entry)
		one_up |= link->the_link->available;


	mtp_link_stop(data->the_link);

	if (!one_up)
		bsc_linkset_down(bsc);

	data->clear_queue(data);
}

/* One of the links of the linkset is back */
void bsc_link_up(struct link_data *data)
{
	struct link_data *link;
	struct bsc_data *bsc;
	int one_up = 0;

	bsc = data->bsc;
	llist_for_each_entry(link, &bsc->links, entry)
		one_up |= link->the_link->available;

	data->the_link->available = 1;

	/* if at least one link is back... report it as up */
	if (!one_up)
		bsc_linkset_up(bsc);

	mtp_link_reset(data->the_link);
}

void mtp_link_forward_sccp(struct mtp_link *_link, struct msgb *_msg, int sls)
{
	struct link_data *link = _link->data;

	linkset_forward_sccp(link->bsc, _msg, sls);
}