blob: 8cf5c5d88f44b3330612ce7906b9e87aed9291e7 [file] [log] [blame]
Pau Espin Pedrol35f0e662019-09-02 18:27:27 +02001/* Section "9.5 GPRS Session Management Messages"
2 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
4/* (C) 2009-2015 by Harald Welte <laforge@gnumonks.org>
5 * (C) 2010 by On-Waves
6 * (C) 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
7 *
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#include <netinet/in.h>
26#include <arpa/inet.h>
27#include <netdb.h>
28
29#include "bscconfig.h"
30
31#include <osmocom/core/rate_ctr.h>
32#include <osmocom/core/tdef.h>
33#include <osmocom/gsm/apn.h>
34#include <osmocom/gprs/gprs_bssgp.h>
35
36#include <osmocom/sgsn/gprs_sm.h>
37#include <osmocom/sgsn/gprs_gmm.h>
38#include <osmocom/sgsn/gprs_utils.h>
39#include <osmocom/sgsn/sgsn.h>
40#include <osmocom/sgsn/debug.h>
41#include <osmocom/sgsn/gprs_llc.h>
42#include <osmocom/sgsn/gprs_sndcp.h>
43#include <osmocom/sgsn/gprs_ranap.h>
44
45extern void *tall_sgsn_ctx;
46
47/* 3GPP TS 04.08 sec 6.1.3.4.3(.a) "Abnormal cases" */
48#define T339X_MAX_RETRANS 4
49
50static const struct tlv_definition gsm48_sm_att_tlvdef = {
51 .def = {
52 [GSM48_IE_GSM_APN] = { TLV_TYPE_TLV, 0 },
53 [GSM48_IE_GSM_PROTO_CONF_OPT] = { TLV_TYPE_TLV, 0 },
54 [GSM48_IE_GSM_PDP_ADDR] = { TLV_TYPE_TLV, 0 },
55 [GSM48_IE_GSM_AA_TMR] = { TLV_TYPE_TV, 1 },
56 [GSM48_IE_GSM_NAME_FULL] = { TLV_TYPE_TLV, 0 },
57 [GSM48_IE_GSM_NAME_SHORT] = { TLV_TYPE_TLV, 0 },
58 [GSM48_IE_GSM_TIMEZONE] = { TLV_TYPE_FIXED, 1 },
59 [GSM48_IE_GSM_UTC_AND_TZ] = { TLV_TYPE_FIXED, 7 },
60 [GSM48_IE_GSM_LSA_ID] = { TLV_TYPE_TLV, 0 },
61 },
62};
63
64static struct gsm48_qos default_qos = {
65 .delay_class = 4, /* best effort */
66 .reliab_class = GSM48_QOS_RC_LLC_UN_RLC_ACK_DATA_PROT,
67 .peak_tput = GSM48_QOS_PEAK_TPUT_32000bps,
68 .preced_class = GSM48_QOS_PC_NORMAL,
69 .mean_tput = GSM48_QOS_MEAN_TPUT_BEST_EFFORT,
70 .traf_class = GSM48_QOS_TC_INTERACTIVE,
71 .deliv_order = GSM48_QOS_DO_UNORDERED,
72 .deliv_err_sdu = GSM48_QOS_ERRSDU_YES,
73 .max_sdu_size = GSM48_QOS_MAXSDU_1520,
74 .max_bitrate_up = GSM48_QOS_MBRATE_63k,
75 .max_bitrate_down = GSM48_QOS_MBRATE_63k,
76 .resid_ber = GSM48_QOS_RBER_5e_2,
77 .sdu_err_ratio = GSM48_QOS_SERR_1e_2,
78 .handling_prio = 3,
79 .xfer_delay = 0x10, /* 200ms */
80 .guar_bitrate_up = GSM48_QOS_MBRATE_0k,
81 .guar_bitrate_down = GSM48_QOS_MBRATE_0k,
82 .sig_ind = 0, /* not optimised for signalling */
83 .max_bitrate_down_ext = 0, /* use octet 9 */
84 .guar_bitrate_down_ext = 0, /* use octet 13 */
85};
86
87/* GPRS SESSION MANAGEMENT */
88
89static void pdpctx_timer_cb(void *_mm);
90
91static void pdpctx_timer_rearm(struct sgsn_pdp_ctx *pdp, unsigned int T)
92{
93 unsigned long seconds;
94 if (osmo_timer_pending(&pdp->timer))
95 LOGPDPCTXP(LOGL_ERROR, pdp, "Scheduling PDP timer %u while old "
96 "timer %u pending\n", T, pdp->T);
97 seconds = osmo_tdef_get(sgsn->cfg.T_defs, T, OSMO_TDEF_S, -1);
98 osmo_timer_schedule(&pdp->timer, seconds, 0);
99}
100
101static void pdpctx_timer_start(struct sgsn_pdp_ctx *pdp, unsigned int T)
102{
103 if (osmo_timer_pending(&pdp->timer))
104 LOGPDPCTXP(LOGL_ERROR, pdp, "Starting PDP timer %u while old "
105 "timer %u pending\n", T, pdp->T);
106 pdp->T = T;
107 pdp->num_T_exp = 0;
108
109 osmo_timer_setup(&pdp->timer, pdpctx_timer_cb, pdp);
110 pdpctx_timer_rearm(pdp, pdp->T);
111}
112
113static void pdpctx_timer_stop(struct sgsn_pdp_ctx *pdp, unsigned int T)
114{
115 if (pdp->T != T)
116 LOGPDPCTXP(LOGL_ERROR, pdp, "Stopping PDP timer %u but "
117 "%u is running\n", T, pdp->T);
118 osmo_timer_del(&pdp->timer);
119}
120
121void pdp_ctx_detach_mm_ctx(struct sgsn_pdp_ctx *pdp)
122{
123 /* Detach from MM context */
124 llist_del(&pdp->list);
125 pdp->mm = NULL;
126
127 /* stop timer 3395 */
128 pdpctx_timer_stop(pdp, 3395);
129}
130
131#if 0
132static void msgb_put_pdp_addr_ipv4(struct msgb *msg, uint32_t ipaddr)
133{
134 uint8_t v[6];
135
136 v[0] = PDP_TYPE_ORG_IETF;
137 v[1] = PDP_TYPE_N_IETF_IPv4;
138 *(uint32_t *)(v+2) = htonl(ipaddr);
139
140 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
141}
142
143static void msgb_put_pdp_addr_ppp(struct msgb *msg)
144{
145 uint8_t v[2];
146
147 v[0] = PDP_TYPE_ORG_ETSI;
148 v[1] = PDP_TYPE_N_ETSI_PPP;
149
150 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
151}
152#endif
153
154/* Chapter 9.4.18 */
155static int _tx_status(struct msgb *msg, uint8_t cause,
156 struct sgsn_mm_ctx *mmctx)
157{
158 struct gsm48_hdr *gh;
159
160 /* MMCTX might be NULL! */
161
162 DEBUGP(DMM, "<- GPRS MM STATUS (cause: %s)\n",
163 get_value_string(gsm48_gmm_cause_names, cause));
164
165 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
166 gh->proto_discr = GSM48_PDISC_SM_GPRS;
167 gh->msg_type = GSM48_MT_GSM_STATUS;
168 gh->data[0] = cause;
169
170 return gsm48_gmm_sendmsg(msg, 0, mmctx, true);
171}
172
173static int gsm48_tx_sm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause)
174{
175 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 SM STATUS");
176
177 mmctx2msgid(msg, mmctx);
178 return _tx_status(msg, cause, mmctx);
179}
180
181/* 3GPP TS 24.008 § 9.5.2: Activate PDP Context Accept */
182int gsm48_tx_gsm_act_pdp_acc(struct sgsn_pdp_ctx *pdp)
183{
184 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 PDP ACC");
185 struct gsm48_hdr *gh;
186 uint8_t transaction_id = pdp->ti ^ 0x8; /* flip */
187
188 LOGPDPCTXP(LOGL_INFO, pdp, "<- ACTIVATE PDP CONTEXT ACK\n");
189 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_PDP_ACTIVATE_ACCEPT]);
190
191 mmctx2msgid(msg, pdp->mm);
192
193 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
194 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
195 gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK;
196
197 /* Negotiated LLC SAPI */
198 msgb_v_put(msg, pdp->sapi);
199
200 /* FIXME: copy QoS parameters from original request */
201 //msgb_lv_put(msg, pdp->lib->qos_neg.l, pdp->lib->qos_neg.v);
202 msgb_lv_put(msg, sizeof(default_qos), (uint8_t *)&default_qos);
203
204 /* Radio priority 10.5.7.2 */
205 msgb_v_put(msg, pdp->lib->radio_pri);
206
207 /* PDP address */
208 /* Highest 4 bits of first byte need to be set to 1, otherwise
209 * the IE is identical with the 04.08 PDP Address IE */
210 pdp->lib->eua.v[0] &= ~0xf0;
211 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR,
212 pdp->lib->eua.l, pdp->lib->eua.v);
213 pdp->lib->eua.v[0] |= 0xf0;
214
215 /* Optional: Protocol configuration options (FIXME: why 'req') */
216 if (pdp->lib->pco_req.l)
217 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT,
218 pdp->lib->pco_req.l, pdp->lib->pco_req.v);
219
220 /* Optional: Packet Flow Identifier */
221
222 return gsm48_gmm_sendmsg(msg, 0, pdp->mm, true);
223}
224
225/* 3GPP TS 24.008 § 9.5.3: Activate PDP Context reject */
226int gsm48_tx_gsm_act_pdp_rej(struct sgsn_mm_ctx *mm, uint8_t tid,
227 uint8_t cause, uint8_t pco_len, uint8_t *pco_v)
228{
229 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 PDP REJ");
230 struct gsm48_hdr *gh;
231 uint8_t transaction_id = tid ^ 0x8; /* flip */
232
233 LOGMMCTXP(LOGL_NOTICE, mm, "<- ACTIVATE PDP CONTEXT REJ: %s\n",
234 get_value_string(gsm48_gsm_cause_names, cause));
235 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_PDP_ACTIVATE_REJECT]);
236
237 mmctx2msgid(msg, mm);
238
239 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
240 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
241 gh->msg_type = GSM48_MT_GSM_ACT_PDP_REJ;
242
243 msgb_v_put(msg, cause);
244 if (pco_len && pco_v)
245 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT, pco_len, pco_v);
246
247 return gsm48_gmm_sendmsg(msg, 0, mm, true);
248}
249
250/* 3GPP TS 24.008 § 9.5.8: Deactivate PDP Context Request */
251static int _gsm48_tx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, uint8_t tid,
252 uint8_t sm_cause, bool teardown)
253{
254 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 PDP DET REQ");
255 struct gsm48_hdr *gh;
256 uint8_t transaction_id = tid ^ 0x8; /* flip */
257 uint8_t tear_down_ind = (0x9 << 4) | (!!teardown);
258
259 LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT REQ\n");
260 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_PDP_DL_DEACTIVATE_REQUEST]);
261
262 mmctx2msgid(msg, mm);
263
264 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
265 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
266 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_REQ;
267
268 msgb_v_put(msg, sm_cause);
269 msgb_v_put(msg, tear_down_ind);
270
271 return gsm48_gmm_sendmsg(msg, 0, mm, true);
272}
273int gsm48_tx_gsm_deact_pdp_req(struct sgsn_pdp_ctx *pdp, uint8_t sm_cause, bool teardown)
274{
275 pdpctx_timer_start(pdp, 3395);
276
277 return _gsm48_tx_gsm_deact_pdp_req(pdp->mm, pdp->ti, sm_cause, teardown);
278}
279
280/* 3GPP TS 24.008 § 9.5.9: Deactivate PDP Context Accept */
281static int _gsm48_tx_gsm_deact_pdp_acc(struct sgsn_mm_ctx *mm, uint8_t tid)
282{
283 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 PDP DET ACC");
284 struct gsm48_hdr *gh;
285 uint8_t transaction_id = tid ^ 0x8; /* flip */
286
287 LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT ACK\n");
288 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_PDP_DL_DEACTIVATE_ACCEPT]);
289
290 mmctx2msgid(msg, mm);
291
292 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
293 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
294 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK;
295
296 return gsm48_gmm_sendmsg(msg, 0, mm, true);
297}
298int gsm48_tx_gsm_deact_pdp_acc(struct sgsn_pdp_ctx *pdp)
299{
300 return _gsm48_tx_gsm_deact_pdp_acc(pdp->mm, pdp->ti);
301}
302
303static int activate_ggsn(struct sgsn_mm_ctx *mmctx,
304 struct sgsn_ggsn_ctx *ggsn, const uint8_t transaction_id,
305 const uint8_t req_nsapi, const uint8_t req_llc_sapi,
306 struct tlv_parsed *tp, int destroy_ggsn)
307{
308 struct sgsn_pdp_ctx *pdp;
309
310 LOGMMCTXP(LOGL_DEBUG, mmctx, "Using GGSN %u\n", ggsn->id);
311 ggsn->gsn = sgsn->gsn;
312 pdp = sgsn_create_pdp_ctx(ggsn, mmctx, req_nsapi, tp);
313 if (!pdp)
314 return -1;
315
316 /* Store SAPI and Transaction Identifier */
317 pdp->sapi = req_llc_sapi;
318 pdp->ti = transaction_id;
319 pdp->destroy_ggsn = destroy_ggsn;
320
321 return 0;
322}
323
324static void ggsn_lookup_cb(void *arg, int status, int timeouts, struct hostent *hostent)
325{
326 struct sgsn_ggsn_ctx *ggsn;
327 struct sgsn_ggsn_lookup *lookup = arg;
328 struct in_addr *addr = NULL;
329 char buf[INET_ADDRSTRLEN];
330
331 /* The context is gone while we made a request */
332 if (!lookup->mmctx) {
333 talloc_free(lookup->orig_msg);
334 talloc_free(lookup);
335 return;
336 }
337
338 if (status != ARES_SUCCESS) {
339 struct sgsn_mm_ctx *mmctx = lookup->mmctx;
340
341 LOGMMCTXP(LOGL_ERROR, mmctx, "DNS query failed.\n");
342
343 /* Need to try with three digits now */
344 if (lookup->state == SGSN_GGSN_2DIGIT) {
345 char *hostname;
346 int rc;
347
348 lookup->state = SGSN_GGSN_3DIGIT;
349 hostname = osmo_apn_qualify_from_imsi(mmctx->imsi,
350 lookup->apn_str, 1);
351 LOGMMCTXP(LOGL_DEBUG, mmctx,
352 "Going to query %s\n", hostname);
353 rc = sgsn_ares_query(sgsn, hostname,
354 ggsn_lookup_cb, lookup);
355 if (rc != 0) {
356 LOGMMCTXP(LOGL_ERROR, mmctx, "Couldn't start GGSN\n");
357 goto reject_due_failure;
358 }
359 return;
360 }
361
362 LOGMMCTXP(LOGL_ERROR, mmctx, "Couldn't resolve GGSN\n");
363 goto reject_due_failure;
364 }
365
366 if (hostent->h_length != sizeof(struct in_addr)) {
367 LOGMMCTXP(LOGL_ERROR, lookup->mmctx,
368 "Wrong addr size(%zu)\n", sizeof(struct in_addr));
369 goto reject_due_failure;
370 }
371
372 /* Get the first addr from the list */
373 addr = (struct in_addr *) hostent->h_addr_list[0];
374 if (!addr) {
375 LOGMMCTXP(LOGL_ERROR, lookup->mmctx, "No host address.\n");
376 goto reject_due_failure;
377 }
378
379 ggsn = sgsn_ggsn_ctx_alloc(UINT32_MAX);
380 if (!ggsn) {
381 LOGMMCTXP(LOGL_ERROR, lookup->mmctx, "Failed to create ggsn.\n");
382 goto reject_due_failure;
383 }
384 ggsn->remote_addr = *addr;
385 LOGMMCTXP(LOGL_NOTICE, lookup->mmctx,
386 "Selected %s as GGSN.\n",
387 inet_ntop(AF_INET, addr, buf, sizeof(buf)));
388
389 /* forget about the ggsn look-up */
390 lookup->mmctx->ggsn_lookup = NULL;
391
392 activate_ggsn(lookup->mmctx, ggsn, lookup->ti, lookup->nsapi,
393 lookup->sapi, &lookup->tp, 1);
394
395 /* Now free it */
396 talloc_free(lookup->orig_msg);
397 talloc_free(lookup);
398 return;
399
400reject_due_failure:
401 gsm48_tx_gsm_act_pdp_rej(lookup->mmctx, lookup->ti,
402 GMM_CAUSE_NET_FAIL, 0, NULL);
403 lookup->mmctx->ggsn_lookup = NULL;
404 talloc_free(lookup->orig_msg);
405 talloc_free(lookup);
406}
407
408static int do_act_pdp_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg, bool *delete)
409{
410 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
411 struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data;
412 uint8_t req_qos_len, req_pdpa_len;
413 uint8_t *req_qos, *req_pdpa;
414 struct tlv_parsed tp;
415 uint8_t transaction_id = gsm48_hdr_trans_id(gh);
416 struct sgsn_ggsn_ctx *ggsn;
417 struct sgsn_pdp_ctx *pdp;
418 enum gsm48_gsm_cause gsm_cause;
419 char apn_str[GSM_APN_LENGTH] = { 0, };
420 char *hostname;
421 int rc;
422 struct gprs_llc_lle *lle;
423 char buf[INET_ADDRSTRLEN];
424
425 LOGMMCTXP(LOGL_INFO, mmctx, "-> ACTIVATE PDP CONTEXT REQ: SAPI=%u NSAPI=%u ",
426 act_req->req_llc_sapi, act_req->req_nsapi);
427
428 /* FIXME: length checks! */
429 req_qos_len = act_req->data[0];
430 req_qos = act_req->data + 1; /* 10.5.6.5 */
431 req_pdpa_len = act_req->data[1 + req_qos_len];
432 req_pdpa = act_req->data + 1 + req_qos_len + 1; /* 10.5.6.4 */
433
434 switch (req_pdpa[0] & 0xf) {
435 case 0x0:
436 DEBUGPC(DMM, "ETSI ");
437 break;
438 case 0x1:
439 DEBUGPC(DMM, "IETF ");
440 break;
441 case 0xf:
442 DEBUGPC(DMM, "Empty ");
443 break;
444 }
445
446 switch (req_pdpa[1]) {
447 case 0x21:
448 DEBUGPC(DMM, "IPv4 ");
449 if (req_pdpa_len >= 6) {
450 struct in_addr ia;
451 ia.s_addr = ntohl(*((uint32_t *) (req_pdpa+2)));
452 DEBUGPC(DMM, "%s ", inet_ntop(AF_INET, &ia, buf, sizeof(buf)));
453 }
454 break;
455 case 0x57:
456 DEBUGPC(DMM, "IPv6 ");
457 if (req_pdpa_len >= 18) {
458 /* FIXME: print IPv6 address */
459 }
460 break;
461 default:
462 DEBUGPC(DMM, "0x%02x ", req_pdpa[1]);
463 break;
464 }
465
466 LOGPC(DMM, LOGL_INFO, "\n");
467
468 /* Check if NSAPI is out of range (TS 04.65 / 7.2) */
469 if (act_req->req_nsapi < 5 || act_req->req_nsapi > 15) {
470 /* Send reject with GSM_CAUSE_INV_MAND_INFO */
471 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
472 GSM_CAUSE_INV_MAND_INFO,
473 0, NULL);
474 }
475
476 /* Optional: Access Point Name, Protocol Config Options */
477 if (req_pdpa + req_pdpa_len < msg->data + msg->len)
478 tlv_parse(&tp, &gsm48_sm_att_tlvdef, req_pdpa + req_pdpa_len,
479 (msg->data + msg->len) - (req_pdpa + req_pdpa_len), 0, 0);
480 else
481 memset(&tp, 0, sizeof(tp));
482
483
484 /* put the non-TLV elements in the TLV parser structure to
485 * pass them on to the SGSN / GTP code */
486 tp.lv[OSMO_IE_GSM_REQ_QOS].len = req_qos_len;
487 tp.lv[OSMO_IE_GSM_REQ_QOS].val = req_qos;
488 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].len = req_pdpa_len;
489 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].val = req_pdpa;
490
491 /* Check if NSAPI is already in use */
492 pdp = sgsn_pdp_ctx_by_nsapi(mmctx, act_req->req_nsapi);
493 if (pdp) {
494 /* We already have a PDP context for this TLLI + NSAPI tuple */
495 if (pdp->sapi == act_req->req_llc_sapi &&
496 pdp->ti == transaction_id) {
497 /* This apparently is a re-transmission of a PDP CTX
498 * ACT REQ (our ACT ACK must have got dropped) */
499 rc = gsm48_tx_gsm_act_pdp_acc(pdp);
500 if (rc < 0)
501 return rc;
502
503 if (pdp->mm->ran_type == MM_CTX_T_GERAN_Gb) {
504 /* Also re-transmit the SNDCP XID message */
505 lle = &pdp->mm->gb.llme->lle[pdp->sapi];
506 rc = sndcp_sn_xid_req(lle,pdp->nsapi);
507 if (rc < 0)
508 return rc;
509 }
510
511 return 0;
512 }
513
514 /* Send reject with GSM_CAUSE_NSAPI_IN_USE */
515 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
516 GSM_CAUSE_NSAPI_IN_USE,
517 0, NULL);
518 }
519
520 if (mmctx->ggsn_lookup) {
521 if (mmctx->ggsn_lookup->sapi == act_req->req_llc_sapi &&
522 mmctx->ggsn_lookup->ti == transaction_id) {
523 LOGMMCTXP(LOGL_NOTICE, mmctx,
524 "Re-transmission while doing look-up. Ignoring.\n");
525 return 0;
526 }
527 }
528
529 /* Only increment counter for a real activation, after we checked
530 * for re-transmissions */
531 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PDP_CTX_ACT]);
532
533 /* Determine GGSN based on APN and subscription options */
534 ggsn = sgsn_mm_ctx_find_ggsn_ctx(mmctx, &tp, &gsm_cause, apn_str);
535 if (ggsn)
536 return activate_ggsn(mmctx, ggsn, transaction_id,
537 act_req->req_nsapi, act_req->req_llc_sapi,
538 &tp, 0);
539
540 if (strlen(apn_str) == 0)
541 goto no_context;
542 if (!sgsn->cfg.dynamic_lookup)
543 goto no_context;
544
545 /* schedule a dynamic look-up */
546 mmctx->ggsn_lookup = talloc_zero(tall_sgsn_ctx, struct sgsn_ggsn_lookup);
547 if (!mmctx->ggsn_lookup)
548 goto no_context;
549
550 mmctx->ggsn_lookup->state = SGSN_GGSN_2DIGIT;
551 mmctx->ggsn_lookup->mmctx = mmctx;
552 strcpy(mmctx->ggsn_lookup->apn_str, apn_str);
553
554 mmctx->ggsn_lookup->orig_msg = msg;
555 mmctx->ggsn_lookup->tp = tp;
556
557 mmctx->ggsn_lookup->ti = transaction_id;
558 mmctx->ggsn_lookup->nsapi = act_req->req_nsapi;
559 mmctx->ggsn_lookup->sapi = act_req->req_llc_sapi;
560
561 hostname = osmo_apn_qualify_from_imsi(mmctx->imsi,
562 mmctx->ggsn_lookup->apn_str, 0);
563
564 LOGMMCTXP(LOGL_DEBUG, mmctx, "Going to query %s\n", hostname);
565 rc = sgsn_ares_query(sgsn, hostname,
566 ggsn_lookup_cb, mmctx->ggsn_lookup);
567 if (rc != 0) {
568 LOGMMCTXP(LOGL_ERROR, mmctx, "Failed to start ares query.\n");
569 goto no_context;
570 }
571 *delete = 0;
572
573 return 0;
574
575no_context:
576 LOGMMCTXP(LOGL_ERROR, mmctx, "No GGSN context found!\n");
577 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
578 gsm_cause, 0, NULL);
579}
580
581/* 3GPP TS 24.008 § 9.5.1: Activate PDP Context Request */
582static int gsm48_rx_gsm_act_pdp_req(struct sgsn_mm_ctx *mmctx,
583 struct msgb *_msg)
584{
585 bool delete = 1;
586 struct msgb *msg;
587 int rc;
588
589 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_PDP_ACTIVATE_REQUEST]);
590
591 /*
592 * This is painful. We might not have a static GGSN
593 * configuration and then would need to copy the msg
594 * and re-do most of this routine (or call it again
595 * and make sure it only goes through the dynamic
596 * resolving. The question is what to optimize for
597 * and the dynamic resolution will be the right thing
598 * in the long run.
599 */
600 msg = bssgp_msgb_copy(_msg, __func__);
601 if (!msg) {
602 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(_msg);
603 uint8_t transaction_id = gsm48_hdr_trans_id(gh);
604
605 LOGMMCTXP(LOGL_ERROR, mmctx, "-> ACTIVATE PDP CONTEXT REQ failed copy.\n");
606 /* Send reject with GSM_CAUSE_INV_MAND_INFO */
607 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
608 GSM_CAUSE_NET_FAIL,
609 0, NULL);
610 }
611
612 rc = do_act_pdp_req(mmctx, msg, &delete);
613 if (delete)
614 msgb_free(msg);
615 return rc;
616}
617
618/* 3GPP TS 24.008 § 9.5.8: Deactivate PDP Context Request */
619static int gsm48_rx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, struct msgb *msg)
620{
621 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
622 uint8_t transaction_id = gsm48_hdr_trans_id(gh);
623 struct sgsn_pdp_ctx *pdp;
624
625 LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT REQ (cause: %s)\n",
626 get_value_string(gsm48_gsm_cause_names, gh->data[0]));
627 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_PDP_UL_DEACTIVATE_REQUEST]);
628
629 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
630 if (!pdp) {
631 LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Request for "
632 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
633 mm->imsi, transaction_id);
634 return _gsm48_tx_gsm_deact_pdp_acc(mm, transaction_id);
635 }
636
637 return sgsn_delete_pdp_ctx(pdp);
638}
639
640/* 3GPP TS 24.008 § 9.5.9: Deactivate PDP Context Accept */
641static int gsm48_rx_gsm_deact_pdp_ack(struct sgsn_mm_ctx *mm, struct msgb *msg)
642{
643 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
644 uint8_t transaction_id = gsm48_hdr_trans_id(gh);
645 struct sgsn_pdp_ctx *pdp;
646
647 LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT ACK\n");
648 rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_PDP_UL_DEACTIVATE_ACCEPT]);
649
650 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
651 if (!pdp) {
652 LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Accept for "
653 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
654 mm->imsi, transaction_id);
655 return 0;
656 }
657 /* stop timer 3395 */
658 pdpctx_timer_stop(pdp, 3395);
659 if (pdp->ggsn)
660 return sgsn_delete_pdp_ctx(pdp);
661 /* GTP side already detached, freeing */
662 sgsn_pdp_ctx_free(pdp);
663 return 0;
664}
665
666static int gsm48_rx_gsm_status(struct sgsn_mm_ctx *ctx, struct msgb *msg)
667{
668 struct gsm48_hdr *gh = msgb_l3(msg);
669
670 LOGMMCTXP(LOGL_INFO, ctx, "-> GPRS SM STATUS (cause: %s)\n",
671 get_value_string(gsm48_gsm_cause_names, gh->data[0]));
672
673 return 0;
674}
675
676static void pdpctx_timer_cb(void *_pdp)
677{
678 struct sgsn_pdp_ctx *pdp = _pdp;
679
680 pdp->num_T_exp++;
681
682 switch (pdp->T) {
683 case 3395: /* waiting for PDP CTX DEACT ACK */
684 if (pdp->num_T_exp > T339X_MAX_RETRANS) {
685 LOGPDPCTXP(LOGL_NOTICE, pdp, "T3395 expired > %d times\n", T339X_MAX_RETRANS);
686 pdp->state = PDP_STATE_INACTIVE;
687 if (pdp->ggsn)
688 sgsn_delete_pdp_ctx(pdp);
689 else
690 sgsn_pdp_ctx_free(pdp);
691 break;
692 }
693 _gsm48_tx_gsm_deact_pdp_req(pdp->mm, pdp->ti, GSM_CAUSE_NET_FAIL, true);
694 pdpctx_timer_rearm(pdp, 3395);
695 break;
696 default:
697 LOGPDPCTXP(LOGL_ERROR, pdp, "timer expired in unknown mode %u\n",
698 pdp->T);
699 }
700}
701
702/* GPRS Session Management */
703int gsm0408_rcv_gsm(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
704 struct gprs_llc_llme *llme)
705{
706 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
707 int rc;
708
709 /* MMCTX can be NULL when called */
710
711 if (!mmctx) {
712 LOGGBIUP(llme, msg, LOGL_NOTICE, "Cannot handle SM for unknown MM CTX\n");
713 /* 6.1.3.6 */
714 if (gh->msg_type == GSM48_MT_GSM_STATUS)
715 return 0;
716
717 return gsm0408_gprs_force_reattach_oldmsg(msg, llme);
718 }
719
720 switch (gh->msg_type) {
721 case GSM48_MT_GSM_ACT_PDP_REQ:
722 rc = gsm48_rx_gsm_act_pdp_req(mmctx, msg);
723 break;
724 case GSM48_MT_GSM_DEACT_PDP_REQ:
725 rc = gsm48_rx_gsm_deact_pdp_req(mmctx, msg);
726 break;
727 case GSM48_MT_GSM_DEACT_PDP_ACK:
728 rc = gsm48_rx_gsm_deact_pdp_ack(mmctx, msg);
729 break;
730 case GSM48_MT_GSM_STATUS:
731 rc = gsm48_rx_gsm_status(mmctx, msg);
732 break;
733 case GSM48_MT_GSM_REQ_PDP_ACT_REJ:
734 case GSM48_MT_GSM_ACT_AA_PDP_REQ:
735 case GSM48_MT_GSM_DEACT_AA_PDP_REQ:
736 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unimplemented GSM 04.08 GSM msg type 0x%02x: %s\n",
737 gh->msg_type, osmo_hexdump((uint8_t *)gh, msgb_l3len(msg)));
738 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
739 break;
740 default:
741 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unknown GSM 04.08 GSM msg type 0x%02x: %s\n",
742 gh->msg_type, osmo_hexdump((uint8_t *)gh, msgb_l3len(msg)));
743 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
744 break;
745
746 }
747
748 return rc;
749}