blob: a034e7ae202fd9aa373a46d4a2b9bad9aecd24d2 [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>
Harald Welte6abf94e2010-05-18 10:35:06 +020040#include <openbsc/gsm_04_08_gprs.h>
Harald Welte2720e732010-05-17 00:44:57 +020041
42#include <openbsc/signal.h>
43#include <openbsc/debug.h>
44#include <openbsc/sgsn.h>
Harald Welte2720e732010-05-17 00:44:57 +020045#include <openbsc/gprs_sgsn.h>
Harald Welte6abf94e2010-05-18 10:35:06 +020046#include <openbsc/gprs_gmm.h>
Harald Welte2720e732010-05-17 00:44:57 +020047
48#include <gtp.h>
49#include <pdp.h>
50
Harald Welte2720e732010-05-17 00:44:57 +020051const struct value_string gtp_cause_strs[] = {
52 { GTPCAUSE_REQ_IMSI, "Request IMSI" },
53 { GTPCAUSE_REQ_IMEI, "Request IMEI" },
54 { GTPCAUSE_REQ_IMSI_IMEI, "Request IMSI and IMEI" },
55 { GTPCAUSE_NO_ID_NEEDED, "No identity needed" },
56 { GTPCAUSE_MS_REFUSES_X, "MS refuses" },
57 { GTPCAUSE_MS_NOT_RESP_X, "MS is not GPRS responding" },
58 { GTPCAUSE_ACC_REQ, "Request accepted" },
59 { GTPCAUSE_NON_EXIST, "Non-existent" },
60 { GTPCAUSE_INVALID_MESSAGE, "Invalid message format" },
61 { GTPCAUSE_IMSI_NOT_KNOWN, "IMSI not known" },
62 { GTPCAUSE_MS_DETACHED, "MS is GPRS detached" },
63 { GTPCAUSE_MS_NOT_RESP, "MS is not GPRS responding" },
64 { GTPCAUSE_MS_REFUSES, "MS refuses" },
65 { GTPCAUSE_NO_RESOURCES, "No resources available" },
66 { GTPCAUSE_NOT_SUPPORTED, "Service not supported" },
67 { GTPCAUSE_MAN_IE_INCORRECT, "Mandatory IE incorrect" },
68 { GTPCAUSE_MAN_IE_MISSING, "Mandatory IE missing" },
69 { GTPCAUSE_OPT_IE_INCORRECT, "Optional IE incorrect" },
70 { GTPCAUSE_SYS_FAIL, "System failure" },
71 { GTPCAUSE_ROAMING_REST, "Roaming restrictions" },
72 { GTPCAUSE_PTIMSI_MISMATCH, "P-TMSI Signature mismatch" },
73 { GTPCAUSE_CONN_SUSP, "GPRS connection suspended" },
74 { GTPCAUSE_AUTH_FAIL, "Authentication failure" },
75 { GTPCAUSE_USER_AUTH_FAIL, "User authentication failed" },
76 { GTPCAUSE_CONTEXT_NOT_FOUND, "Context not found" },
77 { GTPCAUSE_ADDR_OCCUPIED, "All dynamic PDP addresses occupied" },
78 { GTPCAUSE_NO_MEMORY, "No memory is available" },
79 { GTPCAUSE_RELOC_FAIL, "Relocation failure" },
80 { GTPCAUSE_UNKNOWN_MAN_EXTHEADER, "Unknown mandatory ext. header" },
81 { GTPCAUSE_SEM_ERR_TFT, "Semantic error in TFT operation" },
82 { GTPCAUSE_SYN_ERR_TFT, "Syntactic error in TFT operation" },
83 { GTPCAUSE_SEM_ERR_FILTER, "Semantic errors in packet filter" },
84 { GTPCAUSE_SYN_ERR_FILTER, "Syntactic errors in packet filter" },
85 { GTPCAUSE_MISSING_APN, "Missing or unknown APN" },
86 { GTPCAUSE_UNKNOWN_PDP, "Unknown PDP address or PDP type" },
87 { 0, NULL }
88};
89
90/* generate a PDP context based on the IE's from the 04.08 message,
91 * and send the GTP create pdp context request to the GGSN */
Harald Welted193cb32010-05-17 22:58:03 +020092struct sgsn_pdp_ctx *sgsn_create_pdp_ctx(struct ggsn_ctx *ggsn,
93 struct sgsn_mm_ctx *mmctx,
94 uint16_t nsapi,
95 struct tlv_parsed *tp)
Harald Welte2720e732010-05-17 00:44:57 +020096{
Harald Welted193cb32010-05-17 22:58:03 +020097 struct sgsn_pdp_ctx *pctx;
Harald Welte2720e732010-05-17 00:44:57 +020098 struct pdp_t *pdp;
99 uint64_t imsi_ui64;
100 int rc;
101
Harald Welted193cb32010-05-17 22:58:03 +0200102 pctx = sgsn_pdp_ctx_alloc(mmctx, nsapi);
103 if (!pctx) {
104 LOGP(DGPRS, LOGL_ERROR, "Couldn't allocate PDP Ctx\n");
105 return NULL;
106 }
107
Harald Welte2720e732010-05-17 00:44:57 +0200108 rc = pdp_newpdp(&pdp, imsi_ui64, nsapi, NULL);
109 if (rc) {
Harald Welted193cb32010-05-17 22:58:03 +0200110 LOGP(DGPRS, LOGL_ERROR, "Out of libgtp PDP Contexts\n");
111 return NULL;
Harald Welte2720e732010-05-17 00:44:57 +0200112 }
Harald Welted193cb32010-05-17 22:58:03 +0200113 pctx->lib = pdp;
114 pctx->ggsn = ggsn;
115
Harald Welte2720e732010-05-17 00:44:57 +0200116 //pdp->peer = /* sockaddr_in of GGSN (receive) */
117 //pdp->ipif = /* not used by library */
118 pdp->version = ggsn->gtp_version;
119 pdp->hisaddr0 = ggsn->remote_addr;
120 pdp->hisaddr1 = ggsn->remote_addr;
121 //pdp->cch_pdp = 512; /* Charging Flat Rate */
122
123 /* MS provided APN, subscription not verified */
124 pdp->selmode = 0x01;
125
126 /* IMSI, TEID/TEIC, FLLU/FLLC, TID, NSAPI set in pdp_newpdp */
127
128 /* FIXME: MSISDN in BCD format from mmctx */
129 //pdp->msisdn.l/.v
130
131 /* End User Address from GMM requested PDP address */
132 pdp->eua.l = TLVP_LEN(tp, OSMO_IE_GSM_REQ_PDP_ADDR);
133 if (pdp->eua.l > sizeof(pdp->eua.v))
134 pdp->eua.l = sizeof(pdp->eua.v);
135 memcpy(pdp->eua.v, TLVP_VAL(tp, OSMO_IE_GSM_REQ_PDP_ADDR),
136 pdp->eua.l);
137 /* Highest 4 bits of first byte need to be set to 1, otherwise
138 * the IE is identical with the 04.08 PDP Address IE */
139 pdp->eua.v[0] |= 0xf0;
140
141 /* APN name from GMM */
142 pdp->apn_use.l = TLVP_LEN(tp, GSM48_IE_GSM_APN);
143 if (pdp->apn_use.l > sizeof(pdp->apn_use.v))
144 pdp->apn_use.l = sizeof(pdp->apn_use.v);
145 memcpy(pdp->apn_use.v, TLVP_VAL(tp, GSM48_IE_GSM_APN),
146 pdp->apn_use.l);
147
148 /* Protocol Configuration Options from GMM */
149 pdp->pco_req.l = TLVP_LEN(tp, GSM48_IE_GSM_PROTO_CONF_OPT);
150 if (pdp->pco_req.l > sizeof(pdp->pco_req.v))
151 pdp->pco_req.l = sizeof(pdp->pco_req.v);
152 memcpy(pdp->pco_req.v, TLVP_VAL(tp, GSM48_IE_GSM_PROTO_CONF_OPT),
153 pdp->pco_req.l);
154
155 /* QoS options from GMM */
156 pdp->qos_req.l = TLVP_LEN(tp, OSMO_IE_GSM_REQ_QOS);
157 if (pdp->qos_req.l > sizeof(pdp->qos_req.v))
158 pdp->qos_req.l = sizeof(pdp->qos_req.v);
159 memcpy(pdp->qos_req.v, TLVP_VAL(tp, OSMO_IE_GSM_REQ_QOS),
160 pdp->qos_req.l);
161
162 /* SGSN address for control plane */
163 pdp->gsnlc.l = sizeof(sgsn->cfg.gtp_listenaddr);
164 memcpy(pdp->gsnlc.v, &sgsn->cfg.gtp_listenaddr,
165 sizeof(sgsn->cfg.gtp_listenaddr));
166
167 /* SGSN address for user plane */
168 pdp->gsnlu.l = sizeof(sgsn->cfg.gtp_listenaddr);
169 memcpy(pdp->gsnlu.v, &sgsn->cfg.gtp_listenaddr,
170 sizeof(sgsn->cfg.gtp_listenaddr));
171
Harald Welte6abf94e2010-05-18 10:35:06 +0200172 /* change pdp state to 'requested' */
173 pctx->state = PDP_STATE_CR_REQ;
Harald Welte2720e732010-05-17 00:44:57 +0200174
Harald Welted193cb32010-05-17 22:58:03 +0200175 rc = gtp_create_context_req(ggsn->gsn, pdp, pctx);
176 /* FIXME */
177
178 return pctx;
Harald Welte2720e732010-05-17 00:44:57 +0200179}
180
Harald Welte6abf94e2010-05-18 10:35:06 +0200181
182struct cause_map {
183 uint8_t cause_in;
184 uint8_t cause_out;
185};
186
187static uint8_t cause_map(const struct cause_map *map, uint8_t in, uint8_t deflt)
188{
189 const struct cause_map *m;
190
191 for (m = map; m->cause_in && m->cause_out; m++) {
192 if (m->cause_in == in)
193 return m->cause_out;
194 }
195 return deflt;
196}
197
198/* how do we map from gtp cause to SM cause */
199static const struct cause_map gtp2sm_cause_map[] = {
200 { GTPCAUSE_NO_RESOURCES, GSM_CAUSE_INSUFF_RSRC },
201 { GTPCAUSE_NOT_SUPPORTED, GSM_CAUSE_SERV_OPT_NOTSUPP },
202 { GTPCAUSE_MAN_IE_INCORRECT, GSM_CAUSE_INV_MAND_INFO },
203 { GTPCAUSE_MAN_IE_MISSING, GSM_CAUSE_INV_MAND_INFO },
204 { GTPCAUSE_OPT_IE_INCORRECT, GSM_CAUSE_PROTO_ERR_UNSPEC },
205 { GTPCAUSE_SYS_FAIL, GSM_CAUSE_NET_FAIL },
206 { GTPCAUSE_ROAMING_REST, GSM_CAUSE_REQ_SERV_OPT_NOTSUB },
207 { GTPCAUSE_PTIMSI_MISMATCH, GSM_CAUSE_PROTO_ERR_UNSPEC },
208 { GTPCAUSE_CONN_SUSP, GSM_CAUSE_PROTO_ERR_UNSPEC },
209 { GTPCAUSE_AUTH_FAIL, GSM_CAUSE_AUTH_FAILED },
210 { GTPCAUSE_USER_AUTH_FAIL, GSM_CAUSE_ACT_REJ_GGSN },
211 { GTPCAUSE_CONTEXT_NOT_FOUND, GSM_CAUSE_PROTO_ERR_UNSPEC },
212 { GTPCAUSE_ADDR_OCCUPIED, GSM_CAUSE_INSUFF_RSRC },
213 { GTPCAUSE_NO_MEMORY, GSM_CAUSE_INSUFF_RSRC },
214 { GTPCAUSE_RELOC_FAIL, GSM_CAUSE_PROTO_ERR_UNSPEC },
215 { GTPCAUSE_UNKNOWN_MAN_EXTHEADER, GSM_CAUSE_PROTO_ERR_UNSPEC },
216 { GTPCAUSE_MISSING_APN, GSM_CAUSE_MISSING_APN },
217 { GTPCAUSE_UNKNOWN_PDP, GSM_CAUSE_UNKNOWN_PDP },
218 { 0, 0 }
219};
220
Harald Welte2720e732010-05-17 00:44:57 +0200221/* The GGSN has confirmed the creation of a PDP Context */
222static int create_pdp_conf(struct pdp_t *pdp, void *cbp, int cause)
223{
Harald Welted193cb32010-05-17 22:58:03 +0200224 struct sgsn_pdp_ctx *pctx = cbp;
Harald Welte6abf94e2010-05-18 10:35:06 +0200225 uint8_t reject_cause;
Harald Welte2720e732010-05-17 00:44:57 +0200226
227 DEBUGP(DGPRS, "Received CREATE PDP CTX CONF, cause=%d(%s)\n",
228 cause, get_value_string(gtp_cause_strs, cause));
229
230 /* Check for cause value if it was really successful */
231 if (cause < 0) {
232 LOGP(DGPRS, LOGL_NOTICE, "Create PDP ctx req timed out\n");
233 if (pdp->version == 1) {
234 pdp->version = 0;
Harald Welte8fc1a462010-05-17 00:53:10 +0200235 gtp_create_context_req(sgsn->gsn, pdp, cbp);
Harald Welte2720e732010-05-17 00:44:57 +0200236 return 0;
237 } else {
Harald Welte6abf94e2010-05-18 10:35:06 +0200238 reject_cause = GSM_CAUSE_NET_FAIL;
239 goto reject;
Harald Welte2720e732010-05-17 00:44:57 +0200240 }
241 }
242
243 /* Check for cause value if it was really successful */
244 if (cause != GTPCAUSE_ACC_REQ) {
Harald Welte6abf94e2010-05-18 10:35:06 +0200245 reject_cause = cause_map(gtp2sm_cause_map, cause,
246 GSM_CAUSE_ACT_REJ_GGSN);
247 goto reject;
Harald Welte2720e732010-05-17 00:44:57 +0200248 }
249
Harald Welte6abf94e2010-05-18 10:35:06 +0200250 /* Send PDP CTX ACT to MS */
251 return gsm48_tx_gsm_act_pdp_acc(pctx);
252
253reject:
254 pctx->state = PDP_STATE_NONE;
255 pdp_freepdp(pdp);
256 sgsn_pdp_ctx_free(pctx);
257 /* Send PDP CTX ACT REJ to MS */
258 return gsm48_tx_gsm_act_pdp_rej(pctx->mm, pdp->ti, reject_cause,
259 0, NULL);
260
261 return EOF;
Harald Welte2720e732010-05-17 00:44:57 +0200262}
263
264/* If we receive a 04.08 DEACT PDP CTX REQ or GPRS DETACH, we need to
265 * look-up the PDP context and request its deletion from the SGSN */
266int sgsn_delete_pdp_ctx(struct ggsn_ctx *ggsn, struct sgsn_mm_ctx *mmctx,
267 struct tlv_parsed *tp)
268{
269 //return gtp_delete_context_req(gsn, pdp, cbp, teardown);
270}
271
272/* Confirmation of a PDP Context Delete */
273static int delete_pdp_conf(struct pdp_t *pdp, int cause)
274{
275 DEBUGP(DGPRS, "Received DELETE PDP CTX CONF, cause=%d(%s)\n",
276 cause, get_value_string(gtp_cause_strs, cause));
277 return 0;
278}
279
280/* Confirmation of an GTP ECHO request */
281static int echo_conf(int recovery)
282{
283 if (recovery < 0) {
284 DEBUGP(DGPRS, "GTP Echo Request timed out\n");
285 /* FIXME: if version == 1, retry with version 0 */
286 } else {
287 DEBUGP(DGPRS, "GTP Rx Echo Response\n");
288 }
289 return 0;
290}
291
292/* libgtp callback for confirmations */
293static int cb_conf(int type, int cause, struct pdp_t *pdp, void *cbp)
294{
295 DEBUGP(DGPRS, "libgtp cb_conf(type=%d, cause=%d, pdp=%p, cbp=%p)\n",
296 type, cause, pdp, cbp);
297
298 if (cause == EOF)
299 LOGP(DGPRS, LOGL_ERROR, "libgtp EOF (type=%u, pdp=%p, cbp=%p)\n",
300 type, pdp, cbp);
301
302 switch (type) {
303 case GTP_ECHO_REQ:
304 return echo_conf(cause);
305 case GTP_CREATE_PDP_REQ:
306 return create_pdp_conf(pdp, cbp, cause);
307 case GTP_DELETE_PDP_REQ:
308 return delete_pdp_conf(pdp, cause);
309 default:
310 break;
311 }
312 return 0;
313}
314
315/* Called whenever a PDP context is deleted for any reason */
316static int cb_delete_context(struct pdp_t *pdp)
317{
318 LOGP(DGPRS, LOGL_INFO, "PDP Context was deleted\n");
319 return 0;
320}
321
322/* Called when we receive a Version Not Supported message */
323static int cb_unsup_ind(struct sockaddr_in *peer)
324{
325 LOGP(DGPRS, LOGL_INFO, "GTP Version not supported Indication "
326 "from %s:%u\n", inet_ntoa(peer->sin_addr),
327 ntohs(peer->sin_port));
328 return 0;
329}
330
331/* Called when we receive a Supported Ext Headers Notification */
332static int cb_extheader_ind(struct sockaddr_in *peer)
333{
334 LOGP(DGPRS, LOGL_INFO, "GTP Supported Ext Headers Noficiation "
335 "from %s:%u\n", inet_ntoa(peer->sin_addr),
336 ntohs(peer->sin_port));
337 return 0;
338}
339
340/* Called whenever we recive a DATA packet */
341static int cb_data_ind(struct pdp_t *pdp, void *packet, unsigned int len)
342{
343 DEBUGP(DGPRS, "GTP DATA IND from GGSN, length=%u\n", len);
Harald Welte6abf94e2010-05-18 10:35:06 +0200344 /* FIXME: resolve PDP/MM context, forward to SNDCP layer */
Harald Welte2720e732010-05-17 00:44:57 +0200345
346 return 0;
347}
348
349/* libgtp select loop integration */
350static int sgsn_gtp_fd_cb(struct bsc_fd *fd, unsigned int what)
351{
352 struct sgsn_instance *sgi = fd->data;
353 int rc;
354
355 if (!(what & BSC_FD_READ))
356 return 0;
357
358 switch (fd->priv_nr) {
359 case 0:
360 rc = gtp_decaps0(sgi->gsn);
361 break;
362 case 1:
363 rc = gtp_decaps1c(sgi->gsn);
364 break;
365 case 2:
366 rc = gtp_decaps1u(sgi->gsn);
367 break;
368 }
369 return rc;
370}
371
372static void timeval_normalize(struct timeval *tv)
373{
374 unsigned int sec = tv->tv_usec / 1000000;
375 tv->tv_sec += sec;
376 tv->tv_usec -= sec * 1000000;
377}
378
379/* diff = a - b */
380static int timeval_diff(struct timeval *diff,
381 struct timeval *a,
382 struct timeval *b)
383{
384 /* Step 1: normalize input values */
385 timeval_normalize(a);
386 timeval_normalize(b);
387
388 if (b->tv_sec > a->tv_sec ||
389 b->tv_sec == a->tv_sec && b->tv_usec > a->tv_usec) {
390 b->tv_sec = b->tv_usec = 0;
391 return -ERANGE;
392 }
393
394 if (b->tv_usec > a->tv_usec) {
395 a->tv_sec -= 1;
396 a->tv_usec += 1000000;
397 }
398
399 diff->tv_usec = a->tv_usec - b->tv_usec;
400 diff->tv_sec = a->tv_sec - b->tv_sec;
401
402 return 0;
403}
404
405static void sgsn_gtp_tmr_start(struct sgsn_instance *sgi)
406{
407 struct timeval now, next, diff;
408
409 /* Retrieve next retransmission as struct timeval */
410 gtp_retranstimeout(sgi->gsn, &next);
411
412 /* Calculate the difference to now */
413 gettimeofday(&now, NULL);
414 timeval_diff(&diff, &next, &now);
415
416 /* re-schedule the timer */
417 bsc_schedule_timer(&sgi->gtp_timer, diff.tv_sec, diff.tv_usec/1000);
418}
419
420/* timer callback for libgtp retransmissions and ping */
421static void sgsn_gtp_tmr_cb(void *data)
422{
423 struct sgsn_instance *sgi = data;
424
425 /* Do all the retransmissions as needed */
426 gtp_retrans(sgi->gsn);
427
428 sgsn_gtp_tmr_start(sgi);
429}
430
431int sgsn_gtp_init(struct sgsn_instance *sgi)
432{
433 int rc;
434 struct gsn_t *gsn;
435
436 rc = gtp_new(&sgi->gsn, sgi->cfg.gtp_statedir,
437 &sgi->cfg.gtp_listenaddr.sin_addr, GTP_MODE_SGSN);
438 if (rc) {
439 LOGP(DGPRS, LOGL_ERROR, "Failed to create GTP: %d\n", rc);
440 return rc;
441 }
442 gsn = sgi->gsn;
443
444 sgi->gtp_fd0.fd = gsn->fd0;
445 sgi->gtp_fd0.priv_nr = 0;
446 sgi->gtp_fd0.data = sgi;
Harald Welte322a5ee2010-05-18 13:13:11 +0200447 sgi->gtp_fd0.when = BSC_FD_READ;
Harald Welte2720e732010-05-17 00:44:57 +0200448 sgi->gtp_fd0.cb = sgsn_gtp_fd_cb;
449 rc = bsc_register_fd(&sgi->gtp_fd0);
450 if (rc < 0)
451 return rc;
452
453 sgi->gtp_fd1c.fd = gsn->fd1c;
454 sgi->gtp_fd1c.priv_nr = 1;
455 sgi->gtp_fd1c.data = sgi;
Harald Welte322a5ee2010-05-18 13:13:11 +0200456 sgi->gtp_fd1c.when = BSC_FD_READ;
Harald Welte2720e732010-05-17 00:44:57 +0200457 sgi->gtp_fd1c.cb = sgsn_gtp_fd_cb;
458 bsc_register_fd(&sgi->gtp_fd1c);
459 if (rc < 0)
460 return rc;
461
462 sgi->gtp_fd1u.fd = gsn->fd1u;
463 sgi->gtp_fd1u.priv_nr = 2;
464 sgi->gtp_fd1u.data = sgi;
Harald Welte322a5ee2010-05-18 13:13:11 +0200465 sgi->gtp_fd1u.when = BSC_FD_READ;
Harald Welte2720e732010-05-17 00:44:57 +0200466 sgi->gtp_fd1u.cb = sgsn_gtp_fd_cb;
467 bsc_register_fd(&sgi->gtp_fd1u);
468 if (rc < 0)
469 return rc;
470
471 /* Start GTP re-transmission timer */
472 sgi->gtp_timer.cb = sgsn_gtp_tmr_cb;
473 sgsn_gtp_tmr_start(sgi);
474
475 /* Register callbackcs with libgtp */
476 gtp_set_cb_delete_context(gsn, cb_delete_context);
477 gtp_set_cb_conf(gsn, cb_conf);
478 gtp_set_cb_data_ind(gsn, cb_data_ind);
479 gtp_set_cb_unsup_ind(gsn, cb_unsup_ind);
480 gtp_set_cb_extheader_ind(gsn, cb_extheader_ind);
481
482 return 0;
483}