blob: fcaee8bbb41e2560c84c389d9366a9e5a31e03e5 [file] [log] [blame]
Harald Welte2720e732010-05-17 00:44:57 +02001/* GPRS SGSN integration with libgtp of OpenGGSN */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010 by On Waves
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <unistd.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <getopt.h>
28#include <errno.h>
29#include <signal.h>
30#include <sys/fcntl.h>
31#include <sys/stat.h>
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <netinet/in.h>
35#include <arpa/inet.h>
36
37#include <osmocore/talloc.h>
38#include <osmocore/select.h>
39#include <osmocore/rate_ctr.h>
40
41#include <openbsc/signal.h>
42#include <openbsc/debug.h>
43#include <openbsc/sgsn.h>
44//#include <openbsc/gprs_ns.h>
45//#include <openbsc/gprs_bssgp.h>
46#include <openbsc/gprs_sgsn.h>
47#include <openbsc/gsm_04_08_gprs.h>
48
49#include <gtp.h>
50#include <pdp.h>
51
52struct ggsn_ctx {
53 unsigned int gtp_version;
54 struct in_addr remote_addr;
55 struct gsn_t *gsn;
56};
57
58const struct value_string gtp_cause_strs[] = {
59 { GTPCAUSE_REQ_IMSI, "Request IMSI" },
60 { GTPCAUSE_REQ_IMEI, "Request IMEI" },
61 { GTPCAUSE_REQ_IMSI_IMEI, "Request IMSI and IMEI" },
62 { GTPCAUSE_NO_ID_NEEDED, "No identity needed" },
63 { GTPCAUSE_MS_REFUSES_X, "MS refuses" },
64 { GTPCAUSE_MS_NOT_RESP_X, "MS is not GPRS responding" },
65 { GTPCAUSE_ACC_REQ, "Request accepted" },
66 { GTPCAUSE_NON_EXIST, "Non-existent" },
67 { GTPCAUSE_INVALID_MESSAGE, "Invalid message format" },
68 { GTPCAUSE_IMSI_NOT_KNOWN, "IMSI not known" },
69 { GTPCAUSE_MS_DETACHED, "MS is GPRS detached" },
70 { GTPCAUSE_MS_NOT_RESP, "MS is not GPRS responding" },
71 { GTPCAUSE_MS_REFUSES, "MS refuses" },
72 { GTPCAUSE_NO_RESOURCES, "No resources available" },
73 { GTPCAUSE_NOT_SUPPORTED, "Service not supported" },
74 { GTPCAUSE_MAN_IE_INCORRECT, "Mandatory IE incorrect" },
75 { GTPCAUSE_MAN_IE_MISSING, "Mandatory IE missing" },
76 { GTPCAUSE_OPT_IE_INCORRECT, "Optional IE incorrect" },
77 { GTPCAUSE_SYS_FAIL, "System failure" },
78 { GTPCAUSE_ROAMING_REST, "Roaming restrictions" },
79 { GTPCAUSE_PTIMSI_MISMATCH, "P-TMSI Signature mismatch" },
80 { GTPCAUSE_CONN_SUSP, "GPRS connection suspended" },
81 { GTPCAUSE_AUTH_FAIL, "Authentication failure" },
82 { GTPCAUSE_USER_AUTH_FAIL, "User authentication failed" },
83 { GTPCAUSE_CONTEXT_NOT_FOUND, "Context not found" },
84 { GTPCAUSE_ADDR_OCCUPIED, "All dynamic PDP addresses occupied" },
85 { GTPCAUSE_NO_MEMORY, "No memory is available" },
86 { GTPCAUSE_RELOC_FAIL, "Relocation failure" },
87 { GTPCAUSE_UNKNOWN_MAN_EXTHEADER, "Unknown mandatory ext. header" },
88 { GTPCAUSE_SEM_ERR_TFT, "Semantic error in TFT operation" },
89 { GTPCAUSE_SYN_ERR_TFT, "Syntactic error in TFT operation" },
90 { GTPCAUSE_SEM_ERR_FILTER, "Semantic errors in packet filter" },
91 { GTPCAUSE_SYN_ERR_FILTER, "Syntactic errors in packet filter" },
92 { GTPCAUSE_MISSING_APN, "Missing or unknown APN" },
93 { GTPCAUSE_UNKNOWN_PDP, "Unknown PDP address or PDP type" },
94 { 0, NULL }
95};
96
97/* generate a PDP context based on the IE's from the 04.08 message,
98 * and send the GTP create pdp context request to the GGSN */
99int sgsn_create_pdp_ctx(struct ggsn_ctx *ggsn, struct sgsn_mm_ctx *mmctx,
100 uint16_t nsapi, struct tlv_parsed *tp)
101{
102 struct sgsn_instance *sgsn = mmctx->sgsn;
103 struct pdp_t *pdp;
104 uint64_t imsi_ui64;
105 int rc;
106
107 rc = pdp_newpdp(&pdp, imsi_ui64, nsapi, NULL);
108 if (rc) {
109 LOGP(DGPRS, LOGL_ERROR, "Out of PDP Contexts\n");
110 return -ENOMEM;
111 }
112 //pdp->peer = /* sockaddr_in of GGSN (receive) */
113 //pdp->ipif = /* not used by library */
114 pdp->version = ggsn->gtp_version;
115 pdp->hisaddr0 = ggsn->remote_addr;
116 pdp->hisaddr1 = ggsn->remote_addr;
117 //pdp->cch_pdp = 512; /* Charging Flat Rate */
118
119 /* MS provided APN, subscription not verified */
120 pdp->selmode = 0x01;
121
122 /* IMSI, TEID/TEIC, FLLU/FLLC, TID, NSAPI set in pdp_newpdp */
123
124 /* FIXME: MSISDN in BCD format from mmctx */
125 //pdp->msisdn.l/.v
126
127 /* End User Address from GMM requested PDP address */
128 pdp->eua.l = TLVP_LEN(tp, OSMO_IE_GSM_REQ_PDP_ADDR);
129 if (pdp->eua.l > sizeof(pdp->eua.v))
130 pdp->eua.l = sizeof(pdp->eua.v);
131 memcpy(pdp->eua.v, TLVP_VAL(tp, OSMO_IE_GSM_REQ_PDP_ADDR),
132 pdp->eua.l);
133 /* Highest 4 bits of first byte need to be set to 1, otherwise
134 * the IE is identical with the 04.08 PDP Address IE */
135 pdp->eua.v[0] |= 0xf0;
136
137 /* APN name from GMM */
138 pdp->apn_use.l = TLVP_LEN(tp, GSM48_IE_GSM_APN);
139 if (pdp->apn_use.l > sizeof(pdp->apn_use.v))
140 pdp->apn_use.l = sizeof(pdp->apn_use.v);
141 memcpy(pdp->apn_use.v, TLVP_VAL(tp, GSM48_IE_GSM_APN),
142 pdp->apn_use.l);
143
144 /* Protocol Configuration Options from GMM */
145 pdp->pco_req.l = TLVP_LEN(tp, GSM48_IE_GSM_PROTO_CONF_OPT);
146 if (pdp->pco_req.l > sizeof(pdp->pco_req.v))
147 pdp->pco_req.l = sizeof(pdp->pco_req.v);
148 memcpy(pdp->pco_req.v, TLVP_VAL(tp, GSM48_IE_GSM_PROTO_CONF_OPT),
149 pdp->pco_req.l);
150
151 /* QoS options from GMM */
152 pdp->qos_req.l = TLVP_LEN(tp, OSMO_IE_GSM_REQ_QOS);
153 if (pdp->qos_req.l > sizeof(pdp->qos_req.v))
154 pdp->qos_req.l = sizeof(pdp->qos_req.v);
155 memcpy(pdp->qos_req.v, TLVP_VAL(tp, OSMO_IE_GSM_REQ_QOS),
156 pdp->qos_req.l);
157
158 /* SGSN address for control plane */
159 pdp->gsnlc.l = sizeof(sgsn->cfg.gtp_listenaddr);
160 memcpy(pdp->gsnlc.v, &sgsn->cfg.gtp_listenaddr,
161 sizeof(sgsn->cfg.gtp_listenaddr));
162
163 /* SGSN address for user plane */
164 pdp->gsnlu.l = sizeof(sgsn->cfg.gtp_listenaddr);
165 memcpy(pdp->gsnlu.v, &sgsn->cfg.gtp_listenaddr,
166 sizeof(sgsn->cfg.gtp_listenaddr));
167
168 /* FIXME: change pdp state to 'requested' */
169
170 /* FIXME: pass along a pointer to the MM CTX */
171 return gtp_create_context_req(ggsn->gsn, pdp, mmctx);
172}
173
174/* The GGSN has confirmed the creation of a PDP Context */
175static int create_pdp_conf(struct pdp_t *pdp, void *cbp, int cause)
176{
177 struct sgsn_mm_ctx *mmctx = cbp;
178
179 DEBUGP(DGPRS, "Received CREATE PDP CTX CONF, cause=%d(%s)\n",
180 cause, get_value_string(gtp_cause_strs, cause));
181
182 /* Check for cause value if it was really successful */
183 if (cause < 0) {
184 LOGP(DGPRS, LOGL_NOTICE, "Create PDP ctx req timed out\n");
185 if (pdp->version == 1) {
186 pdp->version = 0;
187 gtp_create_context_req(mmctx->sgsn->gsn, pdp, cbp);
188 return 0;
189 } else {
190 pdp_freepdp(pdp);
191 return EOF;
192 }
193 }
194
195 /* Check for cause value if it was really successful */
196 if (cause != GTPCAUSE_ACC_REQ) {
197 pdp_freepdp(pdp);
198 return EOF;
199 }
200
201 /* FIXME: Determine MM ctx for the PDP ctx */
202 /* FIXME: Send PDP CTX ACT ACK/REJ to MS */
203 return 0;
204}
205
206/* If we receive a 04.08 DEACT PDP CTX REQ or GPRS DETACH, we need to
207 * look-up the PDP context and request its deletion from the SGSN */
208int sgsn_delete_pdp_ctx(struct ggsn_ctx *ggsn, struct sgsn_mm_ctx *mmctx,
209 struct tlv_parsed *tp)
210{
211 //return gtp_delete_context_req(gsn, pdp, cbp, teardown);
212}
213
214/* Confirmation of a PDP Context Delete */
215static int delete_pdp_conf(struct pdp_t *pdp, int cause)
216{
217 DEBUGP(DGPRS, "Received DELETE PDP CTX CONF, cause=%d(%s)\n",
218 cause, get_value_string(gtp_cause_strs, cause));
219 return 0;
220}
221
222/* Confirmation of an GTP ECHO request */
223static int echo_conf(int recovery)
224{
225 if (recovery < 0) {
226 DEBUGP(DGPRS, "GTP Echo Request timed out\n");
227 /* FIXME: if version == 1, retry with version 0 */
228 } else {
229 DEBUGP(DGPRS, "GTP Rx Echo Response\n");
230 }
231 return 0;
232}
233
234/* libgtp callback for confirmations */
235static int cb_conf(int type, int cause, struct pdp_t *pdp, void *cbp)
236{
237 DEBUGP(DGPRS, "libgtp cb_conf(type=%d, cause=%d, pdp=%p, cbp=%p)\n",
238 type, cause, pdp, cbp);
239
240 if (cause == EOF)
241 LOGP(DGPRS, LOGL_ERROR, "libgtp EOF (type=%u, pdp=%p, cbp=%p)\n",
242 type, pdp, cbp);
243
244 switch (type) {
245 case GTP_ECHO_REQ:
246 return echo_conf(cause);
247 case GTP_CREATE_PDP_REQ:
248 return create_pdp_conf(pdp, cbp, cause);
249 case GTP_DELETE_PDP_REQ:
250 return delete_pdp_conf(pdp, cause);
251 default:
252 break;
253 }
254 return 0;
255}
256
257/* Called whenever a PDP context is deleted for any reason */
258static int cb_delete_context(struct pdp_t *pdp)
259{
260 LOGP(DGPRS, LOGL_INFO, "PDP Context was deleted\n");
261 return 0;
262}
263
264/* Called when we receive a Version Not Supported message */
265static int cb_unsup_ind(struct sockaddr_in *peer)
266{
267 LOGP(DGPRS, LOGL_INFO, "GTP Version not supported Indication "
268 "from %s:%u\n", inet_ntoa(peer->sin_addr),
269 ntohs(peer->sin_port));
270 return 0;
271}
272
273/* Called when we receive a Supported Ext Headers Notification */
274static int cb_extheader_ind(struct sockaddr_in *peer)
275{
276 LOGP(DGPRS, LOGL_INFO, "GTP Supported Ext Headers Noficiation "
277 "from %s:%u\n", inet_ntoa(peer->sin_addr),
278 ntohs(peer->sin_port));
279 return 0;
280}
281
282/* Called whenever we recive a DATA packet */
283static int cb_data_ind(struct pdp_t *pdp, void *packet, unsigned int len)
284{
285 DEBUGP(DGPRS, "GTP DATA IND from GGSN, length=%u\n", len);
286
287 return 0;
288}
289
290/* libgtp select loop integration */
291static int sgsn_gtp_fd_cb(struct bsc_fd *fd, unsigned int what)
292{
293 struct sgsn_instance *sgi = fd->data;
294 int rc;
295
296 if (!(what & BSC_FD_READ))
297 return 0;
298
299 switch (fd->priv_nr) {
300 case 0:
301 rc = gtp_decaps0(sgi->gsn);
302 break;
303 case 1:
304 rc = gtp_decaps1c(sgi->gsn);
305 break;
306 case 2:
307 rc = gtp_decaps1u(sgi->gsn);
308 break;
309 }
310 return rc;
311}
312
313static void timeval_normalize(struct timeval *tv)
314{
315 unsigned int sec = tv->tv_usec / 1000000;
316 tv->tv_sec += sec;
317 tv->tv_usec -= sec * 1000000;
318}
319
320/* diff = a - b */
321static int timeval_diff(struct timeval *diff,
322 struct timeval *a,
323 struct timeval *b)
324{
325 /* Step 1: normalize input values */
326 timeval_normalize(a);
327 timeval_normalize(b);
328
329 if (b->tv_sec > a->tv_sec ||
330 b->tv_sec == a->tv_sec && b->tv_usec > a->tv_usec) {
331 b->tv_sec = b->tv_usec = 0;
332 return -ERANGE;
333 }
334
335 if (b->tv_usec > a->tv_usec) {
336 a->tv_sec -= 1;
337 a->tv_usec += 1000000;
338 }
339
340 diff->tv_usec = a->tv_usec - b->tv_usec;
341 diff->tv_sec = a->tv_sec - b->tv_sec;
342
343 return 0;
344}
345
346static void sgsn_gtp_tmr_start(struct sgsn_instance *sgi)
347{
348 struct timeval now, next, diff;
349
350 /* Retrieve next retransmission as struct timeval */
351 gtp_retranstimeout(sgi->gsn, &next);
352
353 /* Calculate the difference to now */
354 gettimeofday(&now, NULL);
355 timeval_diff(&diff, &next, &now);
356
357 /* re-schedule the timer */
358 bsc_schedule_timer(&sgi->gtp_timer, diff.tv_sec, diff.tv_usec/1000);
359}
360
361/* timer callback for libgtp retransmissions and ping */
362static void sgsn_gtp_tmr_cb(void *data)
363{
364 struct sgsn_instance *sgi = data;
365
366 /* Do all the retransmissions as needed */
367 gtp_retrans(sgi->gsn);
368
369 sgsn_gtp_tmr_start(sgi);
370}
371
372int sgsn_gtp_init(struct sgsn_instance *sgi)
373{
374 int rc;
375 struct gsn_t *gsn;
376
377 rc = gtp_new(&sgi->gsn, sgi->cfg.gtp_statedir,
378 &sgi->cfg.gtp_listenaddr.sin_addr, GTP_MODE_SGSN);
379 if (rc) {
380 LOGP(DGPRS, LOGL_ERROR, "Failed to create GTP: %d\n", rc);
381 return rc;
382 }
383 gsn = sgi->gsn;
384
385 sgi->gtp_fd0.fd = gsn->fd0;
386 sgi->gtp_fd0.priv_nr = 0;
387 sgi->gtp_fd0.data = sgi;
388 sgi->gtp_fd0.cb = sgsn_gtp_fd_cb;
389 rc = bsc_register_fd(&sgi->gtp_fd0);
390 if (rc < 0)
391 return rc;
392
393 sgi->gtp_fd1c.fd = gsn->fd1c;
394 sgi->gtp_fd1c.priv_nr = 1;
395 sgi->gtp_fd1c.data = sgi;
396 sgi->gtp_fd1c.cb = sgsn_gtp_fd_cb;
397 bsc_register_fd(&sgi->gtp_fd1c);
398 if (rc < 0)
399 return rc;
400
401 sgi->gtp_fd1u.fd = gsn->fd1u;
402 sgi->gtp_fd1u.priv_nr = 2;
403 sgi->gtp_fd1u.data = sgi;
404 sgi->gtp_fd1u.cb = sgsn_gtp_fd_cb;
405 bsc_register_fd(&sgi->gtp_fd1u);
406 if (rc < 0)
407 return rc;
408
409 /* Start GTP re-transmission timer */
410 sgi->gtp_timer.cb = sgsn_gtp_tmr_cb;
411 sgsn_gtp_tmr_start(sgi);
412
413 /* Register callbackcs with libgtp */
414 gtp_set_cb_delete_context(gsn, cb_delete_context);
415 gtp_set_cb_conf(gsn, cb_conf);
416 gtp_set_cb_data_ind(gsn, cb_data_ind);
417 gtp_set_cb_unsup_ind(gsn, cb_unsup_ind);
418 gtp_set_cb_extheader_ind(gsn, cb_extheader_ind);
419
420 return 0;
421}