blob: 9f6532532e632cd90f2f95ba9633301d90e84a7c [file] [log] [blame]
Harald Welte2720e732010-05-17 00:44:57 +02001/* GPRS SGSN integration with libgtp of OpenGGSN */
Harald Welte993d0602010-06-04 17:43:19 +02002/* libgtp implements the GPRS Tunelling Protocol GTP per TS 09.60 / 29.060 */
Harald Welte2720e732010-05-17 00:44:57 +02003
4/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther85531cc2010-10-06 20:37:09 +08005 * (C) 2010 by On-Waves
Holger Hans Peter Freyther9ba273d2015-04-23 09:53:53 -04006 * (C) 2015 by Holger Hans Peter Freyther
Harald Welte2720e732010-05-17 00:44:57 +02007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte2720e732010-05-17 00:44:57 +020012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte2720e732010-05-17 00:44:57 +020018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte2720e732010-05-17 00:44:57 +020021 *
22 */
23
24#include <unistd.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <getopt.h>
29#include <errno.h>
30#include <signal.h>
31#include <sys/fcntl.h>
32#include <sys/stat.h>
Harald Welte2720e732010-05-17 00:44:57 +020033#include <sys/socket.h>
34#include <netinet/in.h>
35#include <arpa/inet.h>
36
Daniel Willmann6292c8d2016-05-21 17:35:57 +020037#include "bscconfig.h"
38
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010039#include <osmocom/core/talloc.h>
40#include <osmocom/core/select.h>
41#include <osmocom/core/rate_ctr.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080042#include <osmocom/gprs/gprs_bssgp.h>
Harald Welte53373bc2016-04-20 17:11:43 +020043#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Harald Welte2720e732010-05-17 00:44:57 +020044
45#include <openbsc/signal.h>
46#include <openbsc/debug.h>
47#include <openbsc/sgsn.h>
Harald Welteebabdea2010-06-01 18:28:10 +020048#include <openbsc/gprs_llc.h>
Harald Welte2720e732010-05-17 00:44:57 +020049#include <openbsc/gprs_sgsn.h>
Harald Welte6abf94e2010-05-18 10:35:06 +020050#include <openbsc/gprs_gmm.h>
Holger Hans Peter Freyther9ba273d2015-04-23 09:53:53 -040051#include <openbsc/gsm_subscriber.h>
Philippf1f34362016-08-26 17:00:21 +020052#include <openbsc/gprs_sndcp.h>
Harald Welte2720e732010-05-17 00:44:57 +020053
Daniel Willmann6292c8d2016-05-21 17:35:57 +020054#ifdef BUILD_IU
55#include <openbsc/iu.h>
56#include <osmocom/ranap/ranap_ies_defs.h>
57#endif
58
Harald Welte2720e732010-05-17 00:44:57 +020059#include <gtp.h>
60#include <pdp.h>
61
Harald Welte2720e732010-05-17 00:44:57 +020062const struct value_string gtp_cause_strs[] = {
63 { GTPCAUSE_REQ_IMSI, "Request IMSI" },
64 { GTPCAUSE_REQ_IMEI, "Request IMEI" },
65 { GTPCAUSE_REQ_IMSI_IMEI, "Request IMSI and IMEI" },
66 { GTPCAUSE_NO_ID_NEEDED, "No identity needed" },
67 { GTPCAUSE_MS_REFUSES_X, "MS refuses" },
68 { GTPCAUSE_MS_NOT_RESP_X, "MS is not GPRS responding" },
69 { GTPCAUSE_ACC_REQ, "Request accepted" },
70 { GTPCAUSE_NON_EXIST, "Non-existent" },
71 { GTPCAUSE_INVALID_MESSAGE, "Invalid message format" },
72 { GTPCAUSE_IMSI_NOT_KNOWN, "IMSI not known" },
73 { GTPCAUSE_MS_DETACHED, "MS is GPRS detached" },
74 { GTPCAUSE_MS_NOT_RESP, "MS is not GPRS responding" },
75 { GTPCAUSE_MS_REFUSES, "MS refuses" },
76 { GTPCAUSE_NO_RESOURCES, "No resources available" },
77 { GTPCAUSE_NOT_SUPPORTED, "Service not supported" },
78 { GTPCAUSE_MAN_IE_INCORRECT, "Mandatory IE incorrect" },
79 { GTPCAUSE_MAN_IE_MISSING, "Mandatory IE missing" },
80 { GTPCAUSE_OPT_IE_INCORRECT, "Optional IE incorrect" },
81 { GTPCAUSE_SYS_FAIL, "System failure" },
82 { GTPCAUSE_ROAMING_REST, "Roaming restrictions" },
83 { GTPCAUSE_PTIMSI_MISMATCH, "P-TMSI Signature mismatch" },
84 { GTPCAUSE_CONN_SUSP, "GPRS connection suspended" },
85 { GTPCAUSE_AUTH_FAIL, "Authentication failure" },
86 { GTPCAUSE_USER_AUTH_FAIL, "User authentication failed" },
87 { GTPCAUSE_CONTEXT_NOT_FOUND, "Context not found" },
88 { GTPCAUSE_ADDR_OCCUPIED, "All dynamic PDP addresses occupied" },
89 { GTPCAUSE_NO_MEMORY, "No memory is available" },
90 { GTPCAUSE_RELOC_FAIL, "Relocation failure" },
91 { GTPCAUSE_UNKNOWN_MAN_EXTHEADER, "Unknown mandatory ext. header" },
92 { GTPCAUSE_SEM_ERR_TFT, "Semantic error in TFT operation" },
93 { GTPCAUSE_SYN_ERR_TFT, "Syntactic error in TFT operation" },
94 { GTPCAUSE_SEM_ERR_FILTER, "Semantic errors in packet filter" },
95 { GTPCAUSE_SYN_ERR_FILTER, "Syntactic errors in packet filter" },
96 { GTPCAUSE_MISSING_APN, "Missing or unknown APN" },
97 { GTPCAUSE_UNKNOWN_PDP, "Unknown PDP address or PDP type" },
98 { 0, NULL }
99};
100
Harald Welte6add7c22010-07-02 22:47:39 +0200101/* Generate the GTP IMSI IE according to 09.60 Section 7.9.2 */
102static uint64_t imsi_str2gtp(char *str)
103{
104 uint64_t imsi64 = 0;
105 unsigned int n;
106 unsigned int imsi_len = strlen(str);
107
108 if (imsi_len > 16) {
109 LOGP(DGPRS, LOGL_NOTICE, "IMSI length > 16 not supported!\n");
110 return 0;
111 }
112
113 for (n = 0; n < 16; n++) {
114 uint64_t val;
115 if (n < imsi_len)
116 val = (str[n]-'0') & 0xf;
117 else
118 val = 0xf;
119 imsi64 |= (val << (n*4));
120 }
121 return imsi64;
122}
123
Harald Welte2720e732010-05-17 00:44:57 +0200124/* generate a PDP context based on the IE's from the 04.08 message,
125 * and send the GTP create pdp context request to the GGSN */
Harald Welte77289c22010-05-18 14:32:29 +0200126struct sgsn_pdp_ctx *sgsn_create_pdp_ctx(struct sgsn_ggsn_ctx *ggsn,
Harald Welted193cb32010-05-17 22:58:03 +0200127 struct sgsn_mm_ctx *mmctx,
128 uint16_t nsapi,
129 struct tlv_parsed *tp)
Harald Welte2720e732010-05-17 00:44:57 +0200130{
Holger Hans Peter Freyther072bee52015-05-17 17:36:23 +0200131 struct gprs_ra_id raid;
Harald Welted193cb32010-05-17 22:58:03 +0200132 struct sgsn_pdp_ctx *pctx;
Harald Welte2720e732010-05-17 00:44:57 +0200133 struct pdp_t *pdp;
Harald Welte6add7c22010-07-02 22:47:39 +0200134 uint64_t imsi_ui64;
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400135 size_t qos_len;
136 const uint8_t *qos;
Harald Welte2720e732010-05-17 00:44:57 +0200137 int rc;
138
Harald Welte77289c22010-05-18 14:32:29 +0200139 LOGP(DGPRS, LOGL_ERROR, "Create PDP Context\n");
Harald Welted193cb32010-05-17 22:58:03 +0200140 pctx = sgsn_pdp_ctx_alloc(mmctx, nsapi);
141 if (!pctx) {
142 LOGP(DGPRS, LOGL_ERROR, "Couldn't allocate PDP Ctx\n");
143 return NULL;
144 }
145
Harald Welte6add7c22010-07-02 22:47:39 +0200146 imsi_ui64 = imsi_str2gtp(mmctx->imsi);
147
Harald Welte2720e732010-05-17 00:44:57 +0200148 rc = pdp_newpdp(&pdp, imsi_ui64, nsapi, NULL);
149 if (rc) {
Harald Welted193cb32010-05-17 22:58:03 +0200150 LOGP(DGPRS, LOGL_ERROR, "Out of libgtp PDP Contexts\n");
151 return NULL;
Harald Welte2720e732010-05-17 00:44:57 +0200152 }
Harald Weltebb1c8052010-06-03 06:38:38 +0200153 pdp->priv = pctx;
Harald Welted193cb32010-05-17 22:58:03 +0200154 pctx->lib = pdp;
155 pctx->ggsn = ggsn;
156
Harald Welte2720e732010-05-17 00:44:57 +0200157 //pdp->peer = /* sockaddr_in of GGSN (receive) */
158 //pdp->ipif = /* not used by library */
159 pdp->version = ggsn->gtp_version;
160 pdp->hisaddr0 = ggsn->remote_addr;
161 pdp->hisaddr1 = ggsn->remote_addr;
162 //pdp->cch_pdp = 512; /* Charging Flat Rate */
163
Holger Hans Peter Freyther0f7a2792015-05-22 10:03:21 +0800164 /* MS provided APN, subscription was verified by the caller */
165 pdp->selmode = 0xFC | 0x00;
Harald Welte2720e732010-05-17 00:44:57 +0200166
167 /* IMSI, TEID/TEIC, FLLU/FLLC, TID, NSAPI set in pdp_newpdp */
168
Holger Hans Peter Freyther9ba273d2015-04-23 09:53:53 -0400169 /* Put the MSISDN in case we have it */
170 if (mmctx->subscr) {
171 pdp->msisdn.l = mmctx->subscr->sgsn_data->msisdn_len;
172 if (pdp->msisdn.l > sizeof(pdp->msisdn.v))
Holger Hans Peter Freyther9861c122015-10-12 10:43:17 +0200173 pdp->msisdn.l = sizeof(pdp->msisdn.v);
Holger Hans Peter Freyther9ba273d2015-04-23 09:53:53 -0400174 memcpy(pdp->msisdn.v, mmctx->subscr->sgsn_data->msisdn,
175 pdp->msisdn.l);
176 }
Harald Welte2720e732010-05-17 00:44:57 +0200177
178 /* End User Address from GMM requested PDP address */
179 pdp->eua.l = TLVP_LEN(tp, OSMO_IE_GSM_REQ_PDP_ADDR);
180 if (pdp->eua.l > sizeof(pdp->eua.v))
181 pdp->eua.l = sizeof(pdp->eua.v);
182 memcpy(pdp->eua.v, TLVP_VAL(tp, OSMO_IE_GSM_REQ_PDP_ADDR),
183 pdp->eua.l);
184 /* Highest 4 bits of first byte need to be set to 1, otherwise
185 * the IE is identical with the 04.08 PDP Address IE */
186 pdp->eua.v[0] |= 0xf0;
187
188 /* APN name from GMM */
189 pdp->apn_use.l = TLVP_LEN(tp, GSM48_IE_GSM_APN);
190 if (pdp->apn_use.l > sizeof(pdp->apn_use.v))
191 pdp->apn_use.l = sizeof(pdp->apn_use.v);
192 memcpy(pdp->apn_use.v, TLVP_VAL(tp, GSM48_IE_GSM_APN),
193 pdp->apn_use.l);
194
195 /* Protocol Configuration Options from GMM */
196 pdp->pco_req.l = TLVP_LEN(tp, GSM48_IE_GSM_PROTO_CONF_OPT);
197 if (pdp->pco_req.l > sizeof(pdp->pco_req.v))
198 pdp->pco_req.l = sizeof(pdp->pco_req.v);
199 memcpy(pdp->pco_req.v, TLVP_VAL(tp, GSM48_IE_GSM_PROTO_CONF_OPT),
200 pdp->pco_req.l);
201
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400202 /* QoS options from GMM or remote */
203 if (TLVP_LEN(tp, OSMO_IE_GSM_SUB_QOS) > 0) {
204 qos_len = TLVP_LEN(tp, OSMO_IE_GSM_SUB_QOS);
205 qos = TLVP_VAL(tp, OSMO_IE_GSM_SUB_QOS);
206 } else {
207 qos_len = TLVP_LEN(tp, OSMO_IE_GSM_REQ_QOS);
208 qos = TLVP_VAL(tp, OSMO_IE_GSM_REQ_QOS);
209 }
210
Holger Hans Peter Freyther4bd931f2015-04-23 11:50:41 -0400211 if (qos_len <= 3) {
212 pdp->qos_req.l = qos_len + 1;
213 if (pdp->qos_req.l > sizeof(pdp->qos_req.v))
214 pdp->qos_req.l = sizeof(pdp->qos_req.v);
215 pdp->qos_req.v[0] = 0; /* Allocation/Retention policy */
216 memcpy(&pdp->qos_req.v[1], qos, pdp->qos_req.l - 1);
217 } else {
218 pdp->qos_req.l = qos_len;
219 if (pdp->qos_req.l > sizeof(pdp->qos_req.v))
220 pdp->qos_req.l = sizeof(pdp->qos_req.v);
221 memcpy(pdp->qos_req.v, qos, pdp->qos_req.l);
222 }
Harald Welte2720e732010-05-17 00:44:57 +0200223
224 /* SGSN address for control plane */
Harald Welte61ca7ce2010-06-02 23:17:33 +0200225 pdp->gsnlc.l = sizeof(sgsn->cfg.gtp_listenaddr.sin_addr);
226 memcpy(pdp->gsnlc.v, &sgsn->cfg.gtp_listenaddr.sin_addr,
227 sizeof(sgsn->cfg.gtp_listenaddr.sin_addr));
Harald Welte2720e732010-05-17 00:44:57 +0200228
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200229 /* SGSN address for user plane
230 * Default to the control plane addr for now. If we are connected to a
231 * hnbgw via IuPS we'll need to send a PDP context update with the
232 * correct IP address after the RAB Assignment is complete */
Harald Welte61ca7ce2010-06-02 23:17:33 +0200233 pdp->gsnlu.l = sizeof(sgsn->cfg.gtp_listenaddr.sin_addr);
234 memcpy(pdp->gsnlu.v, &sgsn->cfg.gtp_listenaddr.sin_addr,
235 sizeof(sgsn->cfg.gtp_listenaddr.sin_addr));
Harald Welte2720e732010-05-17 00:44:57 +0200236
Holger Hans Peter Freytherd4b03182015-04-22 21:40:39 -0400237 /* Assume we are a GERAN system */
238 pdp->rattype.l = 1;
239 pdp->rattype.v[0] = 2;
240 pdp->rattype_given = 1;
241
Holger Hans Peter Freyther6ddb6ac2015-05-12 22:00:22 +0200242 /* Include RAI and ULI all the time */
Holger Hans Peter Freyther22093ef2015-05-12 21:46:58 +0200243 pdp->rai_given = 1;
244 pdp->rai.l = 6;
Holger Hans Peter Freyther072bee52015-05-17 17:36:23 +0200245 raid = mmctx->ra;
246 raid.lac = 0xFFFE;
247 raid.rac = 0xFF;
248 gsm48_construct_ra(pdp->rai.v, &raid);
Holger Hans Peter Freyther22093ef2015-05-12 21:46:58 +0200249
Holger Hans Peter Freyther6ddb6ac2015-05-12 22:00:22 +0200250 pdp->userloc_given = 1;
251 pdp->userloc.l = 8;
252 pdp->userloc.v[0] = 0; /* CGI for GERAN */
Harald Weltef97ee042015-12-25 19:12:21 +0100253 bssgp_create_cell_id(&pdp->userloc.v[1], &mmctx->ra, mmctx->gb.cell_id);
Holger Hans Peter Freyther6ddb6ac2015-05-12 22:00:22 +0200254
Holger Hans Peter Freytherd6900df2015-05-12 22:20:05 +0200255 /* include the IMEI(SV) */
256 pdp->imeisv_given = 1;
257 gsm48_encode_bcd_number(&pdp->imeisv.v[0], 8, 0, mmctx->imei);
258 pdp->imeisv.l = pdp->imeisv.v[0];
259 memmove(&pdp->imeisv.v[0], &pdp->imeisv.v[1], 8);
260
Harald Welte6abf94e2010-05-18 10:35:06 +0200261 /* change pdp state to 'requested' */
262 pctx->state = PDP_STATE_CR_REQ;
Harald Welte2720e732010-05-17 00:44:57 +0200263
Harald Welted193cb32010-05-17 22:58:03 +0200264 rc = gtp_create_context_req(ggsn->gsn, pdp, pctx);
265 /* FIXME */
266
267 return pctx;
Harald Welte2720e732010-05-17 00:44:57 +0200268}
269
Harald Welte3357add2010-12-24 19:21:13 +0100270/* SGSN wants to delete a PDP context */
Harald Welte77289c22010-05-18 14:32:29 +0200271int sgsn_delete_pdp_ctx(struct sgsn_pdp_ctx *pctx)
272{
Daniel Willmann46553142014-09-03 17:46:44 +0200273 LOGPDPCTXP(LOGL_ERROR, pctx, "Delete PDP Context\n");
Harald Welte77289c22010-05-18 14:32:29 +0200274
275 /* FIXME: decide if we need teardown or not ! */
276 return gtp_delete_context_req(pctx->ggsn->gsn, pctx->lib, pctx, 1);
277}
Harald Welte6abf94e2010-05-18 10:35:06 +0200278
279struct cause_map {
280 uint8_t cause_in;
281 uint8_t cause_out;
282};
283
284static uint8_t cause_map(const struct cause_map *map, uint8_t in, uint8_t deflt)
285{
286 const struct cause_map *m;
287
288 for (m = map; m->cause_in && m->cause_out; m++) {
289 if (m->cause_in == in)
290 return m->cause_out;
291 }
292 return deflt;
293}
294
295/* how do we map from gtp cause to SM cause */
296static const struct cause_map gtp2sm_cause_map[] = {
297 { GTPCAUSE_NO_RESOURCES, GSM_CAUSE_INSUFF_RSRC },
298 { GTPCAUSE_NOT_SUPPORTED, GSM_CAUSE_SERV_OPT_NOTSUPP },
299 { GTPCAUSE_MAN_IE_INCORRECT, GSM_CAUSE_INV_MAND_INFO },
300 { GTPCAUSE_MAN_IE_MISSING, GSM_CAUSE_INV_MAND_INFO },
301 { GTPCAUSE_OPT_IE_INCORRECT, GSM_CAUSE_PROTO_ERR_UNSPEC },
302 { GTPCAUSE_SYS_FAIL, GSM_CAUSE_NET_FAIL },
303 { GTPCAUSE_ROAMING_REST, GSM_CAUSE_REQ_SERV_OPT_NOTSUB },
304 { GTPCAUSE_PTIMSI_MISMATCH, GSM_CAUSE_PROTO_ERR_UNSPEC },
305 { GTPCAUSE_CONN_SUSP, GSM_CAUSE_PROTO_ERR_UNSPEC },
306 { GTPCAUSE_AUTH_FAIL, GSM_CAUSE_AUTH_FAILED },
307 { GTPCAUSE_USER_AUTH_FAIL, GSM_CAUSE_ACT_REJ_GGSN },
308 { GTPCAUSE_CONTEXT_NOT_FOUND, GSM_CAUSE_PROTO_ERR_UNSPEC },
309 { GTPCAUSE_ADDR_OCCUPIED, GSM_CAUSE_INSUFF_RSRC },
310 { GTPCAUSE_NO_MEMORY, GSM_CAUSE_INSUFF_RSRC },
311 { GTPCAUSE_RELOC_FAIL, GSM_CAUSE_PROTO_ERR_UNSPEC },
312 { GTPCAUSE_UNKNOWN_MAN_EXTHEADER, GSM_CAUSE_PROTO_ERR_UNSPEC },
313 { GTPCAUSE_MISSING_APN, GSM_CAUSE_MISSING_APN },
314 { GTPCAUSE_UNKNOWN_PDP, GSM_CAUSE_UNKNOWN_PDP },
315 { 0, 0 }
316};
317
Daniel Willmann62ff3842016-05-21 01:40:19 +0200318static int send_act_pdp_cont_acc(struct sgsn_pdp_ctx *pctx)
319{
320 struct sgsn_signal_data sig_data;
Philippf1f34362016-08-26 17:00:21 +0200321 int rc;
322 struct gprs_llc_lle *lle;
Daniel Willmann62ff3842016-05-21 01:40:19 +0200323
324 /* Inform others about it */
325 memset(&sig_data, 0, sizeof(sig_data));
326 sig_data.pdp = pctx;
327 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_ACT, &sig_data);
328
329 /* Send PDP CTX ACT to MS */
Philippf1f34362016-08-26 17:00:21 +0200330 rc = gsm48_tx_gsm_act_pdp_acc(pctx);
331 if(rc < 0)
332 return rc;
333
334 /* Send SNDCP XID to MS */
335 lle = &pctx->mm->gb.llme->lle[pctx->sapi];
336 rc = sndcp_sn_xid_req(lle,pctx->nsapi);
337 if(rc < 0)
338 return rc;
339
340 return 0;
Daniel Willmann62ff3842016-05-21 01:40:19 +0200341}
342
Harald Welte2720e732010-05-17 00:44:57 +0200343/* The GGSN has confirmed the creation of a PDP Context */
344static int create_pdp_conf(struct pdp_t *pdp, void *cbp, int cause)
345{
Harald Welted193cb32010-05-17 22:58:03 +0200346 struct sgsn_pdp_ctx *pctx = cbp;
Harald Welte6abf94e2010-05-18 10:35:06 +0200347 uint8_t reject_cause;
Harald Welte2720e732010-05-17 00:44:57 +0200348
Daniel Willmann46553142014-09-03 17:46:44 +0200349 LOGPDPCTXP(LOGL_INFO, pctx, "Received CREATE PDP CTX CONF, cause=%d(%s)\n",
Harald Welte2720e732010-05-17 00:44:57 +0200350 cause, get_value_string(gtp_cause_strs, cause));
351
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200352 if (!pctx->mm) {
353 LOGP(DGPRS, LOGL_INFO,
354 "No MM context, aborting CREATE PDP CTX CONF\n");
355 return -EIO;
356 }
357
Harald Welte2720e732010-05-17 00:44:57 +0200358 /* Check for cause value if it was really successful */
359 if (cause < 0) {
360 LOGP(DGPRS, LOGL_NOTICE, "Create PDP ctx req timed out\n");
Harald Welte1371f7d2010-06-01 11:52:41 +0200361 if (pdp && pdp->version == 1) {
Harald Welte2720e732010-05-17 00:44:57 +0200362 pdp->version = 0;
Harald Welte8fc1a462010-05-17 00:53:10 +0200363 gtp_create_context_req(sgsn->gsn, pdp, cbp);
Harald Welte2720e732010-05-17 00:44:57 +0200364 return 0;
365 } else {
Harald Welte6abf94e2010-05-18 10:35:06 +0200366 reject_cause = GSM_CAUSE_NET_FAIL;
367 goto reject;
Harald Welte2720e732010-05-17 00:44:57 +0200368 }
369 }
370
371 /* Check for cause value if it was really successful */
372 if (cause != GTPCAUSE_ACC_REQ) {
Harald Welte6abf94e2010-05-18 10:35:06 +0200373 reject_cause = cause_map(gtp2sm_cause_map, cause,
374 GSM_CAUSE_ACT_REJ_GGSN);
375 goto reject;
Harald Welte2720e732010-05-17 00:44:57 +0200376 }
377
Daniel Willmannf9f43872016-05-20 22:36:23 +0200378 if (pctx->mm->ran_type == MM_CTX_T_GERAN_Gb) {
379 /* Activate the SNDCP layer */
380 sndcp_sm_activate_ind(&pctx->mm->gb.llme->lle[pctx->sapi], pctx->nsapi);
381 return send_act_pdp_cont_acc(pctx);
Daniel Willmann6b7b3192016-05-21 00:53:42 +0200382 } else if (pctx->mm->ran_type == MM_CTX_T_UTRAN_Iu) {
383#ifdef BUILD_IU
384 /* Activate a radio bearer */
385 iu_rab_act_ps(pdp->nsapi, pctx, 1);
386 return 0;
387#else
388 return -ENOTSUP;
389#endif
Daniel Willmannf9f43872016-05-20 22:36:23 +0200390 }
Harald Welte6abf94e2010-05-18 10:35:06 +0200391
Neels Hofmeyr67933a12016-07-23 22:23:17 +0200392 LOGP(DGPRS, LOGL_ERROR, "Unknown ran_type %d\n",
393 pctx->mm->ran_type);
394 reject_cause = GSM_CAUSE_PROTO_ERR_UNSPEC;
395
Harald Welte6abf94e2010-05-18 10:35:06 +0200396reject:
Holger Hans Peter Freyther598e7b32013-07-30 08:45:29 +0200397 /*
398 * In case of a timeout pdp will be NULL but we have a valid pointer
399 * in pctx->lib. For other rejects pctx->lib and pdp might be the
400 * same.
401 */
Harald Welte6abf94e2010-05-18 10:35:06 +0200402 pctx->state = PDP_STATE_NONE;
Holger Hans Peter Freyther598e7b32013-07-30 08:45:29 +0200403 if (pctx->lib && pctx->lib != pdp)
404 pdp_freepdp(pctx->lib);
405 pctx->lib = NULL;
406
Harald Welte1371f7d2010-06-01 11:52:41 +0200407 if (pdp)
408 pdp_freepdp(pdp);
Harald Welte6abf94e2010-05-18 10:35:06 +0200409 /* Send PDP CTX ACT REJ to MS */
Holger Hans Peter Freyther65762e02014-04-04 12:49:00 +0200410 gsm48_tx_gsm_act_pdp_rej(pctx->mm, pctx->ti, reject_cause,
Harald Welte6abf94e2010-05-18 10:35:06 +0200411 0, NULL);
Harald Weltef11b6d22010-06-02 10:09:50 +0200412 sgsn_pdp_ctx_free(pctx);
Harald Welte6abf94e2010-05-18 10:35:06 +0200413
414 return EOF;
Harald Welte2720e732010-05-17 00:44:57 +0200415}
416
Daniel Willmann21b269f2016-05-11 12:43:47 +0200417void sgsn_pdp_upd_gtp_u(struct sgsn_pdp_ctx *pdp, void *addr, size_t alen)
418{
419 pdp->lib->gsnlu.l = alen;
420 memcpy(pdp->lib->gsnlu.v, addr, alen);
421 gtp_update_context(pdp->ggsn->gsn, pdp->lib, pdp, &pdp->lib->hisaddr0);
422}
423
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200424#ifdef BUILD_IU
425/* Callback for RAB assignment response */
426int sgsn_ranap_rab_ass_resp(struct sgsn_mm_ctx *ctx, RANAP_RAB_SetupOrModifiedItemIEs_t *setup_ies)
427{
428 uint8_t rab_id;
429 bool require_pdp_update = false;
430 struct sgsn_pdp_ctx *pdp = NULL;
431 RANAP_RAB_SetupOrModifiedItem_t *item = &setup_ies->raB_SetupOrModifiedItem;
432
433 rab_id = item->rAB_ID.buf[0];
434
435 pdp = sgsn_pdp_ctx_by_nsapi(ctx, rab_id);
436 if (!pdp) {
437 LOGP(DRANAP, LOGL_ERROR, "RAB Assignment Response for unknown RAB/NSAPI=%u\n", rab_id);
438 return -1;
439 }
440
441 if (item->transportLayerAddress) {
442 LOGPC(DRANAP, LOGL_INFO, " Setup: (%u/%s)", rab_id, osmo_hexdump(item->transportLayerAddress->buf,
443 item->transportLayerAddress->size));
444 switch (item->transportLayerAddress->size) {
445 case 7:
446 /* It must be IPv4 inside a X213 NSAP */
447 memcpy(pdp->lib->gsnlu.v, &item->transportLayerAddress->buf[3], 4);
448 break;
449 case 4:
450 /* It must be a raw IPv4 address */
451 memcpy(pdp->lib->gsnlu.v, item->transportLayerAddress->buf, 4);
452 break;
453 case 16:
454 /* TODO: It must be a raw IPv6 address */
455 case 19:
456 /* TODO: It must be IPv6 inside a X213 NSAP */
457 default:
458 LOGP(DRANAP, LOGL_ERROR, "RAB Assignment Resp: Unknown "
459 "transport layer address size %u\n",
460 item->transportLayerAddress->size);
461 return -1;
462 }
463 require_pdp_update = true;
464 }
465
466 /* The TEI on the RNC side might have changed, too */
467 if (item->iuTransportAssociation &&
468 item->iuTransportAssociation->present == RANAP_IuTransportAssociation_PR_gTP_TEI &&
469 item->iuTransportAssociation->choice.gTP_TEI.buf &&
470 item->iuTransportAssociation->choice.gTP_TEI.size >= 4) {
471 uint32_t tei = osmo_load32be(item->iuTransportAssociation->choice.gTP_TEI.buf);
472 LOGP(DRANAP, LOGL_DEBUG, "Updating TEID on RNC side from 0x%08x to 0x%08x\n",
473 pdp->lib->teid_own, tei);
474 pdp->lib->teid_own = tei;
475 require_pdp_update = true;
476 }
477
478 if (require_pdp_update)
479 gtp_update_context(pdp->ggsn->gsn, pdp->lib, pdp, &pdp->lib->hisaddr0);
480
481 if (pdp->state != PDP_STATE_CR_CONF) {
482 send_act_pdp_cont_acc(pdp);
483 pdp->state = PDP_STATE_CR_CONF;
484 }
485 return 0;
486
487}
488#endif
489
Harald Welte2720e732010-05-17 00:44:57 +0200490/* Confirmation of a PDP Context Delete */
Harald Welte77289c22010-05-18 14:32:29 +0200491static int delete_pdp_conf(struct pdp_t *pdp, void *cbp, int cause)
Harald Welte2720e732010-05-17 00:44:57 +0200492{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200493 struct sgsn_signal_data sig_data;
Harald Welte77289c22010-05-18 14:32:29 +0200494 struct sgsn_pdp_ctx *pctx = cbp;
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200495 int rc = 0;
Harald Welte77289c22010-05-18 14:32:29 +0200496
Daniel Willmann46553142014-09-03 17:46:44 +0200497 LOGPDPCTXP(LOGL_INFO, pctx, "Received DELETE PDP CTX CONF, cause=%d(%s)\n",
Harald Welte2720e732010-05-17 00:44:57 +0200498 cause, get_value_string(gtp_cause_strs, cause));
Harald Welte77289c22010-05-18 14:32:29 +0200499
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200500 memset(&sig_data, 0, sizeof(sig_data));
501 sig_data.pdp = pctx;
502 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_DEACT, &sig_data);
503
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200504 if (pctx->mm) {
Daniel Willmannf9f43872016-05-20 22:36:23 +0200505 if (pctx->mm->ran_type == MM_CTX_T_GERAN_Gb) {
506 /* Deactivate the SNDCP layer */
507 sndcp_sm_deactivate_ind(&pctx->mm->gb.llme->lle[pctx->sapi], pctx->nsapi);
Daniel Willmann7bc69862016-05-21 00:55:25 +0200508 } else {
509#ifdef BUILD_IU
510 /* Deactivate radio bearer */
511 iu_rab_deact(pctx->mm->iu.ue_ctx, 1);
512#else
513 return -ENOTSUP;
514#endif
Daniel Willmannf9f43872016-05-20 22:36:23 +0200515 }
Harald Welte8fc60f02010-06-28 22:21:57 +0200516
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200517 /* Confirm deactivation of PDP context to MS */
518 rc = gsm48_tx_gsm_deact_pdp_acc(pctx);
519 } else {
520 LOGPDPCTXP(LOGL_NOTICE, pctx,
521 "Not deactivating SNDCP layer since the MM context "
522 "is not available\n");
523 }
Harald Welte77289c22010-05-18 14:32:29 +0200524
Harald Weltefdf453c2012-07-14 12:15:19 +0200525 /* unlink the now non-existing library handle from the pdp
526 * context */
527 pctx->lib = NULL;
528
Harald Welte77289c22010-05-18 14:32:29 +0200529 sgsn_pdp_ctx_free(pctx);
530
531 return rc;
Harald Welte2720e732010-05-17 00:44:57 +0200532}
533
534/* Confirmation of an GTP ECHO request */
Harald Weltea9b473a2010-12-24 21:13:26 +0100535static int echo_conf(struct pdp_t *pdp, void *cbp, int recovery)
Harald Welte2720e732010-05-17 00:44:57 +0200536{
537 if (recovery < 0) {
Daniel Willmann46553142014-09-03 17:46:44 +0200538 LOGP(DGPRS, LOGL_NOTICE, "GTP Echo Request timed out\n");
Harald Welte2720e732010-05-17 00:44:57 +0200539 /* FIXME: if version == 1, retry with version 0 */
540 } else {
541 DEBUGP(DGPRS, "GTP Rx Echo Response\n");
Harald Weltea9b473a2010-12-24 21:13:26 +0100542 }
543 return 0;
544}
545
546/* Any message received by GGSN contains a recovery IE */
547static int cb_recovery(struct sockaddr_in *peer, uint8_t recovery)
548{
549 struct sgsn_ggsn_ctx *ggsn;
550
551 ggsn = sgsn_ggsn_ctx_by_addr(&peer->sin_addr);
552 if (!ggsn) {
Daniel Willmann46553142014-09-03 17:46:44 +0200553 LOGP(DGPRS, LOGL_NOTICE, "Received Recovery IE for unknown GGSN\n");
Harald Weltea9b473a2010-12-24 21:13:26 +0100554 return -EINVAL;
555 }
556
557 if (ggsn->remote_restart_ctr == -1) {
558 /* First received ECHO RESPONSE, note the restart ctr */
559 ggsn->remote_restart_ctr = recovery;
560 } else if (ggsn->remote_restart_ctr != recovery) {
561 /* counter has changed (GGSN restart): release all PDP */
562 LOGP(DGPRS, LOGL_NOTICE, "GGSN recovery (%u->%u), "
563 "releasing all PDP contexts\n",
564 ggsn->remote_restart_ctr, recovery);
565 ggsn->remote_restart_ctr = recovery;
566 drop_all_pdp_for_ggsn(ggsn);
Harald Welte2720e732010-05-17 00:44:57 +0200567 }
568 return 0;
569}
570
571/* libgtp callback for confirmations */
572static int cb_conf(int type, int cause, struct pdp_t *pdp, void *cbp)
573{
574 DEBUGP(DGPRS, "libgtp cb_conf(type=%d, cause=%d, pdp=%p, cbp=%p)\n",
575 type, cause, pdp, cbp);
576
577 if (cause == EOF)
578 LOGP(DGPRS, LOGL_ERROR, "libgtp EOF (type=%u, pdp=%p, cbp=%p)\n",
579 type, pdp, cbp);
580
581 switch (type) {
582 case GTP_ECHO_REQ:
Harald Welte3357add2010-12-24 19:21:13 +0100583 /* libgtp hands us the RECOVERY number instead of a cause */
Harald Weltea9b473a2010-12-24 21:13:26 +0100584 return echo_conf(pdp, cbp, cause);
Harald Welte2720e732010-05-17 00:44:57 +0200585 case GTP_CREATE_PDP_REQ:
586 return create_pdp_conf(pdp, cbp, cause);
587 case GTP_DELETE_PDP_REQ:
Harald Welte77289c22010-05-18 14:32:29 +0200588 return delete_pdp_conf(pdp, cbp, cause);
Harald Welte2720e732010-05-17 00:44:57 +0200589 default:
590 break;
591 }
592 return 0;
593}
594
595/* Called whenever a PDP context is deleted for any reason */
596static int cb_delete_context(struct pdp_t *pdp)
597{
598 LOGP(DGPRS, LOGL_INFO, "PDP Context was deleted\n");
599 return 0;
600}
601
602/* Called when we receive a Version Not Supported message */
603static int cb_unsup_ind(struct sockaddr_in *peer)
604{
605 LOGP(DGPRS, LOGL_INFO, "GTP Version not supported Indication "
606 "from %s:%u\n", inet_ntoa(peer->sin_addr),
607 ntohs(peer->sin_port));
608 return 0;
609}
610
611/* Called when we receive a Supported Ext Headers Notification */
612static int cb_extheader_ind(struct sockaddr_in *peer)
613{
614 LOGP(DGPRS, LOGL_INFO, "GTP Supported Ext Headers Noficiation "
615 "from %s:%u\n", inet_ntoa(peer->sin_addr),
616 ntohs(peer->sin_port));
617 return 0;
618}
619
620/* Called whenever we recive a DATA packet */
Harald Weltebb1c8052010-06-03 06:38:38 +0200621static int cb_data_ind(struct pdp_t *lib, void *packet, unsigned int len)
Harald Welte2720e732010-05-17 00:44:57 +0200622{
Harald Weltebb35c452010-06-09 16:22:28 +0200623 struct bssgp_paging_info pinfo;
Harald Weltebb1c8052010-06-03 06:38:38 +0200624 struct sgsn_pdp_ctx *pdp;
Harald Weltebb35c452010-06-09 16:22:28 +0200625 struct sgsn_mm_ctx *mm;
626 struct msgb *msg;
Harald Weltebb1c8052010-06-03 06:38:38 +0200627 uint8_t *ud;
628
Harald Welte2720e732010-05-17 00:44:57 +0200629 DEBUGP(DGPRS, "GTP DATA IND from GGSN, length=%u\n", len);
Harald Welte2720e732010-05-17 00:44:57 +0200630
Harald Weltebb1c8052010-06-03 06:38:38 +0200631 pdp = lib->priv;
632 if (!pdp) {
Harald Weltefdf453c2012-07-14 12:15:19 +0200633 LOGP(DGPRS, LOGL_NOTICE,
634 "GTP DATA IND from GGSN for unknown PDP\n");
Harald Weltebb1c8052010-06-03 06:38:38 +0200635 return -EIO;
636 }
Harald Weltebb35c452010-06-09 16:22:28 +0200637 mm = pdp->mm;
Harald Weltefdf453c2012-07-14 12:15:19 +0200638 if (!mm) {
639 LOGP(DGPRS, LOGL_ERROR,
640 "PDP context (imsi=%s) without MM context!\n", mm->imsi);
641 return -EIO;
642 }
Harald Weltebb1c8052010-06-03 06:38:38 +0200643
Daniel Willmann21b269f2016-05-11 12:43:47 +0200644 if (mm->ran_type == MM_CTX_T_UTRAN_Iu) {
645#ifdef BUILD_IU
646 /* Ignore the packet for now and page the UE to get the RAB
647 * reestablished */
648 iu_page_ps(mm->imsi, &mm->p_tmsi, mm->ra.lac, mm->ra.rac);
649
650 return 0;
651#else
652 return -ENOTSUP;
653#endif
654 }
655
Sylvain Munaut5914bad2010-06-09 20:49:24 +0200656 msg = msgb_alloc_headroom(len+256, 128, "GTP->SNDCP");
Harald Weltebb1c8052010-06-03 06:38:38 +0200657 ud = msgb_put(msg, len);
658 memcpy(ud, packet, len);
659
Harald Weltef97ee042015-12-25 19:12:21 +0100660 msgb_tlli(msg) = mm->gb.tlli;
661 msgb_bvci(msg) = mm->gb.bvci;
662 msgb_nsei(msg) = mm->gb.nsei;
Harald Weltebb1c8052010-06-03 06:38:38 +0200663
Harald Weltebb35c452010-06-09 16:22:28 +0200664 switch (mm->mm_state) {
665 case GMM_REGISTERED_SUSPENDED:
666 /* initiate PS PAGING procedure */
667 memset(&pinfo, 0, sizeof(pinfo));
668 pinfo.mode = BSSGP_PAGING_PS;
669 pinfo.scope = BSSGP_PAGING_BVCI;
Harald Weltef97ee042015-12-25 19:12:21 +0100670 pinfo.bvci = mm->gb.bvci;
Harald Weltebb35c452010-06-09 16:22:28 +0200671 pinfo.imsi = mm->imsi;
Harald Welte937a9ec2010-07-01 11:53:25 +0200672 pinfo.ptmsi = &mm->p_tmsi;
Harald Weltebb35c452010-06-09 16:22:28 +0200673 pinfo.drx_params = mm->drx_parms;
674 pinfo.qos[0] = 0; // FIXME
Harald Weltef97ee042015-12-25 19:12:21 +0100675 bssgp_tx_paging(mm->gb.nsei, 0, &pinfo);
Harald Welteefbdee92010-06-10 00:20:12 +0200676 rate_ctr_inc(&mm->ctrg->ctr[GMM_CTR_PAGING_PS]);
Harald Weltebb35c452010-06-09 16:22:28 +0200677 /* FIXME: queue the packet we received from GTP */
678 break;
679 case GMM_REGISTERED_NORMAL:
680 break;
681 default:
682 LOGP(DGPRS, LOGL_ERROR, "GTP DATA IND for TLLI %08X in state "
Harald Weltef97ee042015-12-25 19:12:21 +0100683 "%u\n", mm->gb.tlli, mm->mm_state);
Harald Weltebb35c452010-06-09 16:22:28 +0200684 msgb_free(msg);
685 return -1;
686 }
687
Harald Welteefbdee92010-06-10 00:20:12 +0200688 rate_ctr_inc(&pdp->ctrg->ctr[PDP_CTR_PKTS_UDATA_OUT]);
689 rate_ctr_add(&pdp->ctrg->ctr[PDP_CTR_BYTES_UDATA_OUT], len);
690 rate_ctr_inc(&mm->ctrg->ctr[GMM_CTR_PKTS_UDATA_OUT]);
691 rate_ctr_add(&mm->ctrg->ctr[GMM_CTR_BYTES_UDATA_OUT], len);
692
Holger Hans Peter Freyther4f5b8232015-05-05 22:25:48 +0200693 /* It is easier to have a global count */
694 pdp->cdr_bytes_out += len;
695
Harald Weltef97ee042015-12-25 19:12:21 +0100696 return sndcp_unitdata_req(msg, &mm->gb.llme->lle[pdp->sapi],
Harald Weltebb35c452010-06-09 16:22:28 +0200697 pdp->nsapi, mm);
Harald Welte2720e732010-05-17 00:44:57 +0200698}
699
Harald Welteebabdea2010-06-01 18:28:10 +0200700/* Called by SNDCP when it has received/re-assembled a N-PDU */
Harald Welte8911cef2010-07-01 19:56:19 +0200701int sgsn_rx_sndcp_ud_ind(struct gprs_ra_id *ra_id, int32_t tlli, uint8_t nsapi,
702 struct msgb *msg, uint32_t npdu_len, uint8_t *npdu)
Harald Welteebabdea2010-06-01 18:28:10 +0200703{
704 struct sgsn_mm_ctx *mmctx;
705 struct sgsn_pdp_ctx *pdp;
Harald Welteebabdea2010-06-01 18:28:10 +0200706
707 /* look-up the MM context for this message */
Harald Welte8911cef2010-07-01 19:56:19 +0200708 mmctx = sgsn_mm_ctx_by_tlli(tlli, ra_id);
Harald Welteebabdea2010-06-01 18:28:10 +0200709 if (!mmctx) {
710 LOGP(DGPRS, LOGL_ERROR,
711 "Cannot find MM CTX for TLLI %08x\n", tlli);
712 return -EIO;
713 }
714 /* look-up the PDP context for this message */
715 pdp = sgsn_pdp_ctx_by_nsapi(mmctx, nsapi);
716 if (!pdp) {
717 LOGP(DGPRS, LOGL_ERROR, "Cannot find PDP CTX for "
718 "TLLI=%08x, NSAPI=%u\n", tlli, nsapi);
719 return -EIO;
720 }
721 if (!pdp->lib) {
722 LOGP(DGPRS, LOGL_ERROR, "PDP CTX without libgtp\n");
723 return -EIO;
724 }
Harald Welteefbdee92010-06-10 00:20:12 +0200725
726 rate_ctr_inc(&pdp->ctrg->ctr[PDP_CTR_PKTS_UDATA_IN]);
727 rate_ctr_add(&pdp->ctrg->ctr[PDP_CTR_BYTES_UDATA_IN], npdu_len);
728 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_UDATA_IN]);
729 rate_ctr_add(&mmctx->ctrg->ctr[GMM_CTR_BYTES_UDATA_IN], npdu_len);
730
Holger Hans Peter Freyther4f5b8232015-05-05 22:25:48 +0200731 /* It is easier to have a global count */
732 pdp->cdr_bytes_in += npdu_len;
733
Harald Welteefbdee92010-06-10 00:20:12 +0200734 return gtp_data_req(pdp->ggsn->gsn, pdp->lib, npdu, npdu_len);
Harald Welteebabdea2010-06-01 18:28:10 +0200735}
736
Harald Welte2720e732010-05-17 00:44:57 +0200737/* libgtp select loop integration */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200738static int sgsn_gtp_fd_cb(struct osmo_fd *fd, unsigned int what)
Harald Welte2720e732010-05-17 00:44:57 +0200739{
740 struct sgsn_instance *sgi = fd->data;
741 int rc;
742
743 if (!(what & BSC_FD_READ))
744 return 0;
745
746 switch (fd->priv_nr) {
747 case 0:
748 rc = gtp_decaps0(sgi->gsn);
749 break;
750 case 1:
751 rc = gtp_decaps1c(sgi->gsn);
752 break;
753 case 2:
754 rc = gtp_decaps1u(sgi->gsn);
755 break;
Harald Weltecd4dd4d2010-05-18 17:20:49 +0200756 default:
757 rc = -EINVAL;
758 break;
Harald Welte2720e732010-05-17 00:44:57 +0200759 }
760 return rc;
761}
762
Harald Welte2720e732010-05-17 00:44:57 +0200763static void sgsn_gtp_tmr_start(struct sgsn_instance *sgi)
764{
Harald Welteb4a31292010-05-18 18:24:53 +0200765 struct timeval next;
Harald Welte2720e732010-05-17 00:44:57 +0200766
767 /* Retrieve next retransmission as struct timeval */
768 gtp_retranstimeout(sgi->gsn, &next);
769
Harald Welte2720e732010-05-17 00:44:57 +0200770 /* re-schedule the timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200771 osmo_timer_schedule(&sgi->gtp_timer, next.tv_sec, next.tv_usec/1000);
Harald Welte2720e732010-05-17 00:44:57 +0200772}
773
774/* timer callback for libgtp retransmissions and ping */
775static void sgsn_gtp_tmr_cb(void *data)
776{
777 struct sgsn_instance *sgi = data;
778
779 /* Do all the retransmissions as needed */
780 gtp_retrans(sgi->gsn);
781
782 sgsn_gtp_tmr_start(sgi);
783}
784
785int sgsn_gtp_init(struct sgsn_instance *sgi)
786{
787 int rc;
788 struct gsn_t *gsn;
789
790 rc = gtp_new(&sgi->gsn, sgi->cfg.gtp_statedir,
791 &sgi->cfg.gtp_listenaddr.sin_addr, GTP_MODE_SGSN);
792 if (rc) {
793 LOGP(DGPRS, LOGL_ERROR, "Failed to create GTP: %d\n", rc);
794 return rc;
795 }
796 gsn = sgi->gsn;
797
798 sgi->gtp_fd0.fd = gsn->fd0;
799 sgi->gtp_fd0.priv_nr = 0;
800 sgi->gtp_fd0.data = sgi;
Harald Welte322a5ee2010-05-18 13:13:11 +0200801 sgi->gtp_fd0.when = BSC_FD_READ;
Harald Welte2720e732010-05-17 00:44:57 +0200802 sgi->gtp_fd0.cb = sgsn_gtp_fd_cb;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200803 rc = osmo_fd_register(&sgi->gtp_fd0);
Harald Welte2720e732010-05-17 00:44:57 +0200804 if (rc < 0)
805 return rc;
806
807 sgi->gtp_fd1c.fd = gsn->fd1c;
808 sgi->gtp_fd1c.priv_nr = 1;
809 sgi->gtp_fd1c.data = sgi;
Harald Welte322a5ee2010-05-18 13:13:11 +0200810 sgi->gtp_fd1c.when = BSC_FD_READ;
Harald Welte2720e732010-05-17 00:44:57 +0200811 sgi->gtp_fd1c.cb = sgsn_gtp_fd_cb;
Holger Hans Peter Freyther3a708af2013-07-04 18:39:02 +0200812 rc = osmo_fd_register(&sgi->gtp_fd1c);
813 if (rc < 0) {
814 osmo_fd_unregister(&sgi->gtp_fd0);
Harald Welte2720e732010-05-17 00:44:57 +0200815 return rc;
Holger Hans Peter Freyther3a708af2013-07-04 18:39:02 +0200816 }
Harald Welte2720e732010-05-17 00:44:57 +0200817
818 sgi->gtp_fd1u.fd = gsn->fd1u;
819 sgi->gtp_fd1u.priv_nr = 2;
820 sgi->gtp_fd1u.data = sgi;
Harald Welte322a5ee2010-05-18 13:13:11 +0200821 sgi->gtp_fd1u.when = BSC_FD_READ;
Harald Welte2720e732010-05-17 00:44:57 +0200822 sgi->gtp_fd1u.cb = sgsn_gtp_fd_cb;
Holger Hans Peter Freyther3a708af2013-07-04 18:39:02 +0200823 rc = osmo_fd_register(&sgi->gtp_fd1u);
824 if (rc < 0) {
825 osmo_fd_unregister(&sgi->gtp_fd0);
826 osmo_fd_unregister(&sgi->gtp_fd1c);
Harald Welte2720e732010-05-17 00:44:57 +0200827 return rc;
Holger Hans Peter Freyther3a708af2013-07-04 18:39:02 +0200828 }
Harald Welte2720e732010-05-17 00:44:57 +0200829
830 /* Start GTP re-transmission timer */
831 sgi->gtp_timer.cb = sgsn_gtp_tmr_cb;
Harald Welte51537ee2010-05-18 18:28:13 +0200832 sgi->gtp_timer.data = sgi;
Harald Welte2720e732010-05-17 00:44:57 +0200833 sgsn_gtp_tmr_start(sgi);
834
835 /* Register callbackcs with libgtp */
836 gtp_set_cb_delete_context(gsn, cb_delete_context);
837 gtp_set_cb_conf(gsn, cb_conf);
Harald Weltea9b473a2010-12-24 21:13:26 +0100838 gtp_set_cb_recovery(gsn, cb_recovery);
Harald Welte2720e732010-05-17 00:44:57 +0200839 gtp_set_cb_data_ind(gsn, cb_data_ind);
840 gtp_set_cb_unsup_ind(gsn, cb_unsup_ind);
841 gtp_set_cb_extheader_ind(gsn, cb_extheader_ind);
842
843 return 0;
844}