aboutsummaryrefslogtreecommitdiffstats
path: root/src/isup.c
blob: 404dbb10d17e07c24a656822bf988ecc45eea4ec (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
/*
 * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
 * (C) 2010-2011 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 Affero General Public License as published by
 * the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <isup_types.h>
#include <cellmgr_debug.h>
#include <mtp_data.h>

#include <osmocom/core/msgb.h>
#include <osmocom/gsm/tlv.h>

static struct msgb *isup_status_alloc(int cic, int msg_type, uint8_t *extra, int range, int val)
{
	struct isup_msg_hdr *hdr;
	struct msgb *msg;
	int bits, len;
	uint8_t *data;

	msg = msgb_alloc_headroom(4096, 128, "ISUP Simple MSG");
	if (!msg) {
		LOGP(DISUP, LOGL_ERROR, "Allocation of status message failed.\n");
		return NULL;
	}

	msg->l2h = msgb_put(msg, sizeof(*hdr));

	/* write the ISUP header */
	hdr = (struct isup_msg_hdr *) msg->l2h;
	hdr->cic = cic;
	hdr->msg_type = msg_type;

	if (extra)
		msgb_v_put(msg, *extra);

	/*
	 * place the pointers here.
	 * 1.) place the variable start after us
	 * 2.) place the length
	 */
	msgb_v_put(msg, 1);

	bits = range + 1;
	len = (bits / 8) + 1;
	msgb_v_put(msg, len + 1);
	msgb_v_put(msg, range);

	data = msgb_put(msg, len);

	/* set the status bits to val... FIXME this sets the extra bits too */
	memset(data, val, len);

	return msg;
}

static struct msgb *isup_simple_alloc(int cic, int msg_type)
{
	struct isup_msg_hdr *hdr;
	struct msgb *msg;

	msg = msgb_alloc_headroom(4096, 128, "ISUP Simple MSG");
	if (!msg) {
		LOGP(DISUP, LOGL_ERROR, "Allocation of Simple message failed.\n");
		return NULL;
	}

	msg->l2h = msgb_put(msg, sizeof(*hdr));

	/* write the ISUP header */
	hdr = (struct isup_msg_hdr *) msg->l2h;
	hdr->cic = cic;
	hdr->msg_type = msg_type;

	msgb_v_put(msg, 0);
	return msg;
}

/* this message contains the range */
int isup_parse_status(const uint8_t *data, uint8_t in_length)
{
	uint8_t ptr;
	uint8_t length;

	if (in_length < 3) {
		LOGP(DISUP, LOGL_ERROR, "This needs three bytes.\n");
		return -1;	
	}

	ptr = data[0];
	if (1 + ptr > in_length) {
		LOGP(DISUP, LOGL_ERROR, "Pointing outside the packet.\n");
		return -1;
	}

	length = data[0 + ptr];

	if (1 + ptr + 1 > in_length) {
		LOGP(DISUP, LOGL_ERROR, "No space for the data.\n");
		return -1;
	}

	return data[0 + ptr + 1];
}


/* Handle incoming ISUP data */
static int handle_circuit_reset_grs(struct mtp_link_set *set, int sls, int cic,
				    const uint8_t *data, int size)
{
	struct msgb *resp;
	int range;

	range = isup_parse_status(data, size);
	if (range < 0)
		return -1;

	resp = isup_status_alloc(cic, ISUP_MSG_GRA, NULL, range, 0);
	if (!resp)
		return -1;

	mtp_link_set_submit_isup_data(set, sls, resp->l2h, msgb_l2len(resp));
	msgb_free(resp);
	return 0;
}

static int handle_circuit_reset_cgb(struct mtp_link_set *set, int sls, int cic,
				    const uint8_t *data, int size)
{
	struct msgb *resp;
	int range;
	uint8_t val;

	if (size < 1)
		return -1;

	range = isup_parse_status(data + 1, size - 1);
	if (range < 0)
		return -1;

	val = 0;
	resp = isup_status_alloc(cic, ISUP_MSG_CGBA, &val, range, 0xff);
	if (!resp)
		return -1;

	mtp_link_set_submit_isup_data(set, sls, resp->l2h, msgb_l2len(resp));
	msgb_free(resp);
	return 0;
}

static int send_cgu(struct mtp_link_set *set, int sls, int cic, int range)
{
	struct msgb *resp;
	uint8_t val;

	val = 0;
	resp = isup_status_alloc(cic, ISUP_MSG_CGU, &val, range, 0);
	if (!resp)
		return -1;

	mtp_link_set_submit_isup_data(set, sls, resp->l2h, msgb_l2len(resp));
	msgb_free(resp);
	return 0;
}

static int handle_cgu(struct mtp_link_set *set, int sls, int cic,
		      uint8_t *data, uint16_t size)
{
	uint8_t *out;
	struct isup_msg_hdr *hdr;
	struct msgb *resp;

	resp = msgb_alloc_headroom(4096, 128, "ISUP CGUA MSG");
	if (!resp) {
		LOGP(DISUP, LOGL_ERROR, "Allocation of CGUA message failed.\n");
		return -1;
	}

	resp->l2h = msgb_put(resp, sizeof(*hdr));

	/* write the ISUP header */
	hdr = (struct isup_msg_hdr *) resp->l2h;
	hdr->cic = cic;
	hdr->msg_type = ISUP_MSG_CGUA;

	out = msgb_put(resp, size);
	memcpy(out, data, size);

	mtp_link_set_submit_isup_data(set, sls, resp->l2h, msgb_l2len(resp));
	msgb_free(resp);
	return 0;
}

static int handle_simple_resp(struct mtp_link_set *set, int sls, int cic, int msg_type)
{
	struct msgb *resp;

	resp = isup_simple_alloc(cic, msg_type);
	if (!resp)
		return -1;
	mtp_link_set_submit_isup_data(set, sls, resp->l2h, msgb_l2len(resp));
	msgb_free(resp);
	return 0;
}

int mtp_link_set_isup(struct mtp_link_set *set, struct msgb *msg, int sls)
{
	int rc = -1;
	int payload_size;
	struct isup_msg_hdr *hdr;

	if (msgb_l3len(msg) < sizeof(*hdr)) {
		LOGP(DISUP, LOGL_ERROR, "ISUP header is too short.\n");
		return -1;
	}

	if (set->pass_all_isup) {
		mtp_link_set_forward_isup(set, msg, sls);
		return 0;
	}

	hdr = (struct isup_msg_hdr *) msg->l3h;
	payload_size = msgb_l3len(msg) - sizeof(*hdr);

	switch (hdr->msg_type) {
	case ISUP_MSG_GRS:
		rc = handle_circuit_reset_grs(set, sls, hdr->cic, hdr->data, payload_size);
		break;
	case ISUP_MSG_CGB:
		rc = handle_circuit_reset_cgb(set, sls, hdr->cic, hdr->data, payload_size);
		if (rc == 0)
			rc = send_cgu(set, sls, hdr->cic, 28);
		break;
	case ISUP_MSG_CGU:
		rc = handle_cgu(set, sls, hdr->cic, hdr->data, payload_size);
		break;
	case ISUP_MSG_CGUA:
		LOGP(DISUP, LOGL_NOTICE, "CIC %d is now unblocked on linkset %d/%s.\n",
		     hdr->cic, set->nr, set->name);
		break;
	case ISUP_MSG_RSC:
		rc = handle_simple_resp(set, sls, hdr->cic, ISUP_MSG_RLC);
		break;
	default:
		mtp_link_set_forward_isup(set, msg, sls);
		rc = 0;
		break;
	}

	return rc;
}