Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * OsmoGGSN - Gateway GPRS Support Node |
| 3 | * Copyright (C) 2002, 2003, 2004 Mondru AB. |
| 4 | * Copyright (C) 2010-2011, 2016-2017 Harald Welte <laforge@gnumonks.org> |
| 5 | * Copyright (C) 2015-2017 sysmocom - s.f.m.c. GmbH |
| 6 | * |
| 7 | * The contents of this file may be used under the terms of the GNU |
| 8 | * General Public License Version 2, provided that the above copyright |
| 9 | * notice and this permission notice is included in all copies or |
| 10 | * substantial portions of the software. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * gtp.c: Contains all GTP functionality. Should be able to handle multiple |
| 16 | * tunnels in the same program. |
| 17 | * |
| 18 | * TODO: |
| 19 | * - Do we need to handle fragmentation? |
| 20 | */ |
| 21 | |
| 22 | #ifdef __linux__ |
| 23 | #define _GNU_SOURCE 1 |
| 24 | #endif |
| 25 | |
| 26 | #include <osmocom/core/logging.h> |
| 27 | #include <osmocom/core/utils.h> |
Pau Espin Pedrol | b9036af | 2022-11-02 18:17:56 +0100 | [diff] [blame] | 28 | #include <osmocom/core/stats.h> |
| 29 | #include <osmocom/core/rate_ctr.h> |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 30 | |
| 31 | #if defined(__FreeBSD__) |
| 32 | #include <sys/endian.h> |
| 33 | #endif |
| 34 | |
| 35 | #include "../config.h" |
| 36 | #ifdef HAVE_STDINT_H |
| 37 | #include <stdint.h> |
| 38 | #endif |
| 39 | |
| 40 | #include <stdio.h> |
| 41 | #include <stdarg.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <sys/time.h> |
| 44 | #include <sys/types.h> |
| 45 | #include <sys/socket.h> |
| 46 | #include <netinet/in.h> |
| 47 | #include <arpa/inet.h> |
| 48 | #include <sys/stat.h> |
| 49 | #include <time.h> |
| 50 | #include <unistd.h> |
| 51 | #include <string.h> |
| 52 | #include <errno.h> |
| 53 | #include <fcntl.h> |
| 54 | #include <inttypes.h> |
| 55 | |
| 56 | #include <arpa/inet.h> |
| 57 | |
| 58 | /* #include <stdint.h> ISO C99 types */ |
| 59 | |
| 60 | #include "pdp.h" |
| 61 | #include "gtp.h" |
| 62 | #include "gtpie.h" |
| 63 | #include "queue.h" |
| 64 | |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 65 | /* Error reporting functions */ |
| 66 | |
| 67 | #define LOGP_WITH_ADDR(ss, level, addr, fmt, args...) \ |
| 68 | LOGP(ss, level, "addr(%s:%d) " fmt, \ |
| 69 | inet_ntoa((addr).sin_addr), htons((addr).sin_port), \ |
| 70 | ##args); |
| 71 | |
Pau Espin Pedrol | b9036af | 2022-11-02 18:17:56 +0100 | [diff] [blame] | 72 | static const struct rate_ctr_desc gsn_ctr_description[] = { |
| 73 | [GSN_CTR_ERR_SOCKET] = { "err:socket", "Socket error" }, |
| 74 | [GSN_CTR_ERR_READFROM] = { "err:readfrom", "readfrom() errors" }, |
| 75 | [GSN_CTR_ERR_SENDTO] = { "err:sendto", "sendto() errors" }, |
| 76 | [GSN_CTR_ERR_QUEUEFULL] = { "err:queuefull", "Failed to queue message because queue is full" }, |
| 77 | [GSN_CTR_ERR_SEQ] = { "err:seq", "Sequence number out of range" }, |
| 78 | [GSN_CTR_ERR_ADDRESS] = { "err:address", "GSN address conversion failed" }, |
| 79 | [GSN_CTR_ERR_UNKNOWN_PDP] = { "err:unknown_pdp", "Failed looking up PDP context" }, |
| 80 | [GSN_CTR_ERR_UNEXPECTED_CAUSE] = { "err:unexpected_cause", "Unexpected cause value received" }, |
| 81 | [GSN_CTR_ERR_OUT_OF_PDP] = { "err:out_of_pdp", "Out of storage for PDP contexts" }, |
| 82 | [GSN_CTR_PKT_EMPTY] = { "pkt:empty", "Empty packet received" }, |
| 83 | [GSN_CTR_PKT_UNSUP] = { "pkt:unsupported", "Unsupported GTP version received" }, |
| 84 | [GSN_CTR_PKT_TOOSHORT] = { "pkt:too_short", "Packet too short received" }, |
| 85 | [GSN_CTR_PKT_UNKNOWN] = { "pkt:unknown", "Unknown packet type received" }, |
| 86 | [GSN_CTR_PKT_UNEXPECT] = { "pkt:unexpected", "Unexpected packet type received" }, |
| 87 | [GSN_CTR_PKT_DUPLICATE] = { "pkt:duplicate", "Duplicate or unsolicited packet received" }, |
| 88 | [GSN_CTR_PKT_MISSING] = { "pkt:missing", "Missing IE in packet received" }, |
| 89 | [GSN_CTR_PKT_INCORRECT] = { "pkt:incorrect", "Incorrect IE in packet received" }, |
| 90 | [GSN_CTR_PKT_INVALID] = { "pkt:invalid", "Invalid format in packet received" }, |
| 91 | }; |
| 92 | |
| 93 | static const struct rate_ctr_group_desc gsn_ctrg_desc = { |
| 94 | "gsn", |
| 95 | "GSN Statistics", |
| 96 | OSMO_STATS_CLASS_PEER, |
| 97 | ARRAY_SIZE(gsn_ctr_description), |
| 98 | gsn_ctr_description, |
| 99 | }; |
| 100 | static unsigned int gsn_ctr_next_idx = 0; |
| 101 | |
Pau Espin Pedrol | 9f1f747 | 2022-11-02 19:52:19 +0100 | [diff] [blame] | 102 | /* Global timer definitions for GTP operation, provided for convenience. To make these user configurable, it is convenient to add |
| 103 | * gtp_gsn_tdefs as one of your program's osmo_tdef_group entries and call osmo_tdef_vty_init(). */ |
| 104 | struct osmo_tdef gtp_T_defs[] = { |
| 105 | { .T = GTP_GSN_TIMER_T3_RESPONSE, .default_val = 5, .unit = OSMO_TDEF_S, |
| 106 | .desc = "Timer T3-RESPONSE holds the maximum wait time for a response of a request message" |
| 107 | }, |
| 108 | { .T = GTP_GSN_TIMER_N3_REQUESTS, .default_val = 3, .unit = OSMO_TDEF_CUSTOM, |
| 109 | .desc = "Counter N3-REQUESTS holds the maximum number of attempts made by GTP to send a request message" |
| 110 | }, |
Pau Espin Pedrol | 3a55b89 | 2022-11-04 11:21:23 +0100 | [diff] [blame] | 111 | { .T = GTP_GSN_TIMER_T3_HOLD_RESPONSE, .default_val = 5 * 3 /* (GTP_GSN_TIMER_T3_RESPONSE * GTP_GSN_TIMER_N3_REQUESTS) */, .unit = OSMO_TDEF_S, |
| 112 | .desc = "Time a GTP respoonse message is kept cached to re-transmit it when a duplicate request is received. Value is generally equal to (T3-RESPONSE * N3-REQUESTS) set at the peer" |
| 113 | }, |
Pau Espin Pedrol | 9f1f747 | 2022-11-02 19:52:19 +0100 | [diff] [blame] | 114 | {} |
| 115 | }; |
| 116 | |
Pau Espin Pedrol | b9036af | 2022-11-02 18:17:56 +0100 | [diff] [blame] | 117 | |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 118 | /* API Functions */ |
| 119 | |
| 120 | /* Deprecated, use gtp_pdp_newpdp() instead */ |
| 121 | int gtp_newpdp(struct gsn_t *gsn, struct pdp_t **pdp, |
| 122 | uint64_t imsi, uint8_t nsapi) |
| 123 | { |
| 124 | int rc; |
| 125 | rc = gtp_pdp_newpdp(gsn, pdp, imsi, nsapi, NULL); |
| 126 | return rc; |
| 127 | } |
| 128 | |
| 129 | int gtp_freepdp(struct gsn_t *gsn, struct pdp_t *pdp) |
| 130 | { |
| 131 | if (gsn->cb_delete_context) |
| 132 | gsn->cb_delete_context(pdp); |
| 133 | return pdp_freepdp(pdp); |
| 134 | } |
| 135 | |
| 136 | /* Free pdp and all its secondary PDP contexts. Must be called on the primary PDP context. */ |
| 137 | int gtp_freepdp_teardown(struct gsn_t *gsn, struct pdp_t *pdp) |
| 138 | { |
| 139 | int n; |
| 140 | struct pdp_t *secondary_pdp; |
| 141 | OSMO_ASSERT(!pdp->secondary); |
| 142 | |
| 143 | for (n = 0; n < PDP_MAXNSAPI; n++) { |
| 144 | if (pdp->secondary_tei[n]) { |
| 145 | if (gtp_pdp_getgtp1(gsn, &secondary_pdp, |
| 146 | pdp->secondary_tei[n])) { |
| 147 | LOGP(DLGTP, LOGL_ERROR, |
| 148 | "Unknown secondary PDP context\n"); |
| 149 | continue; |
| 150 | } |
| 151 | if (pdp != secondary_pdp) { |
| 152 | gtp_freepdp(gsn, secondary_pdp); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return gtp_freepdp(gsn, pdp); |
| 158 | } |
| 159 | |
| 160 | /* gtp_gpdu */ |
| 161 | |
| 162 | extern int gtp_fd(struct gsn_t *gsn) |
| 163 | { |
| 164 | return gsn->fd0; |
| 165 | } |
| 166 | |
| 167 | int gtp_set_cb_unsup_ind(struct gsn_t *gsn, |
| 168 | int (*cb) (struct sockaddr_in * peer)) |
| 169 | { |
| 170 | gsn->cb_unsup_ind = cb; |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | int gtp_set_cb_extheader_ind(struct gsn_t *gsn, |
| 175 | int (*cb) (struct sockaddr_in * peer)) |
| 176 | { |
| 177 | gsn->cb_extheader_ind = cb; |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | int gtp_set_cb_ran_info_relay_ind(struct gsn_t *gsn, |
| 182 | int (*cb) (struct sockaddr_in * peer, union gtpie_member **ie)) |
| 183 | { |
| 184 | gsn->cb_ran_info_relay_ind = cb; |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | /* API: Initialise delete context callback */ |
| 189 | /* Called whenever a pdp context is deleted for any reason */ |
| 190 | int gtp_set_cb_delete_context(struct gsn_t *gsn, int (*cb) (struct pdp_t * pdp)) |
| 191 | { |
| 192 | gsn->cb_delete_context = cb; |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | int gtp_set_cb_conf(struct gsn_t *gsn, |
| 197 | int (*cb) (int type, int cause, |
| 198 | struct pdp_t * pdp, void *cbp)) |
| 199 | { |
| 200 | gsn->cb_conf = cb; |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | int gtp_set_cb_recovery(struct gsn_t *gsn, |
| 205 | int (*cb) (struct sockaddr_in * peer, uint8_t recovery)) |
| 206 | { |
| 207 | gsn->cb_recovery = cb; |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | /* cb_recovery() |
| 212 | * pdp may be NULL if Recovery IE was received from a message independent |
| 213 | * of any PDP ctx (such as Echo Response), or because pdp ctx is unknown to the |
| 214 | * local setup. In case pdp is known, caller may want to keep that pdp alive to |
| 215 | * handle subsequent msg cb as this specific pdp ctx is still valid according to |
| 216 | * specs. |
| 217 | */ |
| 218 | int gtp_set_cb_recovery2(struct gsn_t *gsn, |
| 219 | int (*cb_recovery2) (struct sockaddr_in * peer, struct pdp_t * pdp, uint8_t recovery)) |
| 220 | { |
| 221 | gsn->cb_recovery2 = cb_recovery2; |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | /* cb_recovery() |
| 226 | * pdp may be NULL if Recovery IE was received from a message independent |
| 227 | * of any PDP ctx (such as Echo Response), or because pdp ctx is unknown to the |
| 228 | * local setup. In case pdp is known, caller may want to keep that pdp alive to |
| 229 | * handle subsequent msg cb as this specific pdp ctx is still valid according to |
| 230 | * specs. |
| 231 | */ |
| 232 | int gtp_set_cb_recovery3(struct gsn_t *gsn, |
| 233 | int (*cb_recovery3) (struct gsn_t *gsn, struct sockaddr_in *peer, |
| 234 | struct pdp_t *pdp, uint8_t recovery)) |
| 235 | { |
| 236 | gsn->cb_recovery3 = cb_recovery3; |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | int gtp_set_cb_data_ind(struct gsn_t *gsn, |
| 241 | int (*cb_data_ind) (struct pdp_t * pdp, |
| 242 | void *pack, unsigned len)) |
| 243 | { |
| 244 | gsn->cb_data_ind = cb_data_ind; |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static int queue_timer_retrans(struct gsn_t *gsn) |
| 249 | { |
| 250 | /* Retransmit any outstanding packets */ |
| 251 | /* Remove from queue if maxretrans exceeded */ |
| 252 | time_t now; |
| 253 | struct qmsg_t *qmsg; |
Pau Espin Pedrol | 9f1f747 | 2022-11-02 19:52:19 +0100 | [diff] [blame] | 254 | unsigned int t3_response, n3_requests; |
| 255 | |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 256 | now = time(NULL); |
Pau Espin Pedrol | 9f1f747 | 2022-11-02 19:52:19 +0100 | [diff] [blame] | 257 | t3_response = osmo_tdef_get(gsn->tdef, GTP_GSN_TIMER_T3_RESPONSE, OSMO_TDEF_S, -1); |
| 258 | n3_requests = osmo_tdef_get(gsn->tdef, GTP_GSN_TIMER_N3_REQUESTS, OSMO_TDEF_CUSTOM, -1); |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 259 | |
| 260 | /* get first element in queue, as long as the timeout of that |
| 261 | * element has expired */ |
| 262 | while ((!queue_getfirst(gsn->queue_req, &qmsg)) && |
| 263 | (qmsg->timeout <= now)) { |
Pau Espin Pedrol | 9f1f747 | 2022-11-02 19:52:19 +0100 | [diff] [blame] | 264 | if (qmsg->retrans > n3_requests) { /* Too many retrans */ |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 265 | LOGP(DLGTP, LOGL_NOTICE, "Retransmit req queue timeout of seq %" PRIu16 "\n", |
| 266 | qmsg->seq); |
| 267 | if (gsn->cb_conf) |
| 268 | gsn->cb_conf(qmsg->type, EOF, NULL, qmsg->cbp); |
| 269 | queue_freemsg(gsn->queue_req, qmsg); |
| 270 | } else { |
| 271 | LOGP(DLGTP, LOGL_INFO, "Retransmit (%d) of seq %" PRIu16 "\n", |
| 272 | qmsg->retrans, qmsg->seq); |
| 273 | if (sendto(qmsg->fd, &qmsg->p, qmsg->l, 0, |
| 274 | (struct sockaddr *)&qmsg->peer, |
| 275 | sizeof(struct sockaddr_in)) < 0) { |
Pau Espin Pedrol | b9036af | 2022-11-02 18:17:56 +0100 | [diff] [blame] | 276 | rate_ctr_inc2(gsn->ctrg, GSN_CTR_ERR_SENDTO); |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 277 | LOGP(DLGTP, LOGL_ERROR, |
| 278 | "Sendto(fd0=%d, msg=%lx, len=%d) failed: Error = %s\n", |
| 279 | gsn->fd0, (unsigned long)&qmsg->p, |
| 280 | qmsg->l, strerror(errno)); |
| 281 | } |
| 282 | queue_back(gsn->queue_req, qmsg); |
Pau Espin Pedrol | 9f1f747 | 2022-11-02 19:52:19 +0100 | [diff] [blame] | 283 | qmsg->timeout = now + t3_response; |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 284 | qmsg->retrans++; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /* Also clean up reply timeouts */ |
| 289 | while ((!queue_getfirst(gsn->queue_resp, &qmsg)) && |
| 290 | (qmsg->timeout < now)) { |
| 291 | LOGP(DLGTP, LOGL_DEBUG, "Retransmit resp queue seq %" |
| 292 | PRIu16 " expired, removing from queue\n", qmsg->seq); |
| 293 | queue_freemsg(gsn->queue_resp, qmsg); |
| 294 | } |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static int queue_timer_retranstimeout(struct gsn_t *gsn, struct timeval *timeout) |
| 300 | { |
| 301 | time_t now, later, diff; |
| 302 | struct qmsg_t *qmsg; |
| 303 | timeout->tv_usec = 0; |
| 304 | |
| 305 | if (queue_getfirst(gsn->queue_req, &qmsg)) { |
| 306 | timeout->tv_sec = 10; |
| 307 | } else { |
| 308 | now = time(NULL); |
| 309 | later = qmsg->timeout; |
| 310 | timeout->tv_sec = later - now; |
| 311 | if (timeout->tv_sec < 0) |
| 312 | timeout->tv_sec = 0; /* No negative allowed */ |
| 313 | if (timeout->tv_sec > 10) |
| 314 | timeout->tv_sec = 10; /* Max sleep for 10 sec */ |
| 315 | } |
| 316 | |
| 317 | if (queue_getfirst(gsn->queue_resp, &qmsg)) { |
| 318 | /* already set by queue_req, do nothing */ |
| 319 | } else { /* trigger faster if earlier timeout exists in queue_resp */ |
| 320 | now = time(NULL); |
| 321 | later = qmsg->timeout; |
| 322 | diff = later - now; |
| 323 | if (diff < 0) |
| 324 | diff = 0; |
| 325 | if (diff < timeout->tv_sec) |
| 326 | timeout->tv_sec = diff; |
| 327 | } |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | void gtp_queue_timer_start(struct gsn_t *gsn) |
| 333 | { |
| 334 | struct timeval next; |
| 335 | |
| 336 | /* Retrieve next retransmission as timeval */ |
| 337 | queue_timer_retranstimeout(gsn, &next); |
| 338 | |
| 339 | /* re-schedule the timer */ |
| 340 | osmo_timer_schedule(&gsn->queue_timer, next.tv_sec, next.tv_usec/1000); |
| 341 | } |
| 342 | |
| 343 | /* timer callback for libgtp retransmission and ping */ |
| 344 | static void queue_timer_cb(void *data) |
| 345 | { |
| 346 | struct gsn_t *gsn = data; |
| 347 | |
| 348 | /* do all the retransmissions as needed */ |
| 349 | queue_timer_retrans(gsn); |
| 350 | |
| 351 | gtp_queue_timer_start(gsn); |
| 352 | } |
| 353 | |
| 354 | |
| 355 | /** |
| 356 | * @brief clear the request and response queue. Useful for debugging to reset "some" state. |
| 357 | * @param gsn The GGSN instance |
| 358 | */ |
| 359 | void gtp_clear_queues(struct gsn_t *gsn) |
| 360 | { |
| 361 | struct qmsg_t *qmsg; |
| 362 | |
| 363 | LOGP(DLGTP, LOGL_INFO, "Clearing req & resp retransmit queues\n"); |
| 364 | while (!queue_getfirst(gsn->queue_req, &qmsg)) { |
| 365 | queue_freemsg(gsn->queue_req, qmsg); |
| 366 | } |
| 367 | |
| 368 | while (!queue_getfirst(gsn->queue_resp, &qmsg)) { |
| 369 | queue_freemsg(gsn->queue_resp, qmsg); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /* Perform restoration and recovery error handling as described in 29.060 */ |
| 374 | static void log_restart(struct gsn_t *gsn) |
| 375 | { |
| 376 | FILE *f; |
| 377 | int i, rc; |
| 378 | int counter = 0; |
| 379 | char *filename; |
| 380 | |
| 381 | filename = talloc_asprintf(NULL, "%s/%s", gsn->statedir, RESTART_FILE); |
| 382 | OSMO_ASSERT(filename); |
| 383 | |
| 384 | /* We try to open file. On failure we will later try to create file */ |
| 385 | if (!(f = fopen(filename, "r"))) { |
| 386 | LOGP(DLGTP, LOGL_NOTICE, |
| 387 | "State information file (%s) not found. Creating new file.\n", |
| 388 | filename); |
| 389 | } else { |
| 390 | rc = fscanf(f, "%d", &counter); |
| 391 | if (rc != 1) { |
| 392 | LOGP(DLGTP, LOGL_ERROR, |
| 393 | "fscanf failed to read counter value\n"); |
| 394 | goto close_file; |
| 395 | } |
| 396 | if (fclose(f)) { |
| 397 | LOGP(DLGTP, LOGL_ERROR, |
| 398 | "fclose failed: Error = %s\n", strerror(errno)); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | gsn->restart_counter = (unsigned char)counter; |
| 403 | gsn->restart_counter++; |
| 404 | |
| 405 | /* Keep the umask closely wrapped around our fopen() call in case the |
| 406 | * log outputs cause file creation. */ |
| 407 | i = umask(022); |
| 408 | f = fopen(filename, "w"); |
| 409 | umask(i); |
| 410 | if (!f) { |
| 411 | LOGP(DLGTP, LOGL_ERROR, |
| 412 | "fopen(path=%s, mode=%s) failed: Error = %s\n", filename, |
| 413 | "w", strerror(errno)); |
| 414 | goto free_filename; |
| 415 | } |
| 416 | |
| 417 | fprintf(f, "%d\n", gsn->restart_counter); |
| 418 | close_file: |
| 419 | if (fclose(f)) |
| 420 | LOGP(DLGTP, LOGL_ERROR, |
| 421 | "fclose failed: Error = %s\n", strerror(errno)); |
| 422 | free_filename: |
| 423 | talloc_free(filename); |
| 424 | } |
| 425 | |
| 426 | int gtp_new(struct gsn_t **gsn, char *statedir, struct in_addr *listen, |
| 427 | int mode) |
| 428 | { |
| 429 | struct sockaddr_in addr; |
| 430 | |
| 431 | LOGP(DLGTP, LOGL_NOTICE, "GTP: gtp_newgsn() started at %s\n", inet_ntoa(*listen)); |
| 432 | |
| 433 | *gsn = calloc(sizeof(struct gsn_t), 1); /* TODO */ |
| 434 | |
| 435 | (*gsn)->statedir = statedir; |
| 436 | log_restart(*gsn); |
| 437 | |
| 438 | /* Initialise sequence number */ |
| 439 | (*gsn)->seq_next = (*gsn)->restart_counter * 1024; |
| 440 | |
Pau Espin Pedrol | 9f1f747 | 2022-11-02 19:52:19 +0100 | [diff] [blame] | 441 | /* Initialize timers: */ |
| 442 | (*gsn)->tdef = gtp_T_defs; |
| 443 | /* Small hack to properly reset tdef for old clients not using the tdef_group: */ |
Vadim Yanitskiy | bf69ddb | 2023-02-25 18:04:45 +0700 | [diff] [blame] | 444 | OSMO_ASSERT(gtp_T_defs[0].default_val != 0); |
Pau Espin Pedrol | 9f1f747 | 2022-11-02 19:52:19 +0100 | [diff] [blame] | 445 | if (gtp_T_defs[0].val == 0) |
| 446 | osmo_tdefs_reset((*gsn)->tdef); |
| 447 | |
| 448 | |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 449 | /* Initialise request retransmit queue */ |
| 450 | queue_new(&(*gsn)->queue_req); |
| 451 | queue_new(&(*gsn)->queue_resp); |
| 452 | |
| 453 | /* Initialise pdp table */ |
| 454 | pdp_init(*gsn); |
| 455 | |
| 456 | /* Initialize internal queue timer */ |
| 457 | osmo_timer_setup(&(*gsn)->queue_timer, queue_timer_cb, *gsn); |
| 458 | |
Pau Espin Pedrol | b9036af | 2022-11-02 18:17:56 +0100 | [diff] [blame] | 459 | /* Initialize counter group: */ |
| 460 | (*gsn)->ctrg = rate_ctr_group_alloc(NULL, &gsn_ctrg_desc, gsn_ctr_next_idx++); |
| 461 | |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 462 | /* Initialise call back functions */ |
| 463 | (*gsn)->cb_create_context_ind = 0; |
| 464 | (*gsn)->cb_delete_context = 0; |
| 465 | (*gsn)->cb_unsup_ind = 0; |
| 466 | (*gsn)->cb_conf = 0; |
| 467 | (*gsn)->cb_data_ind = 0; |
| 468 | |
| 469 | /* Store function parameters */ |
| 470 | (*gsn)->gsnc = *listen; |
| 471 | (*gsn)->gsnu = *listen; |
| 472 | (*gsn)->mode = mode; |
| 473 | |
| 474 | /* Create GTP version 0 socket */ |
| 475 | if (((*gsn)->fd0 = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
Pau Espin Pedrol | b9036af | 2022-11-02 18:17:56 +0100 | [diff] [blame] | 476 | rate_ctr_inc2((*gsn)->ctrg, GSN_CTR_ERR_SOCKET); |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 477 | LOGP(DLGTP, LOGL_ERROR, |
| 478 | "GTPv0 socket(domain=%d, type=%d, protocol=%d) failed: Error = %s\n", |
| 479 | AF_INET, SOCK_DGRAM, 0, strerror(errno)); |
| 480 | return -errno; |
| 481 | } |
| 482 | |
| 483 | memset(&addr, 0, sizeof(addr)); |
| 484 | addr.sin_family = AF_INET; |
| 485 | addr.sin_addr = *listen; /* Same IP for user traffic and signalling */ |
| 486 | addr.sin_port = htons(GTP0_PORT); |
| 487 | #if defined(__FreeBSD__) || defined(__APPLE__) |
| 488 | addr.sin_len = sizeof(addr); |
| 489 | #endif |
| 490 | |
| 491 | if (bind((*gsn)->fd0, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
Pau Espin Pedrol | b9036af | 2022-11-02 18:17:56 +0100 | [diff] [blame] | 492 | rate_ctr_inc2((*gsn)->ctrg, GSN_CTR_ERR_SOCKET); |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 493 | LOGP_WITH_ADDR(DLGTP, LOGL_ERROR, addr, |
| 494 | "bind(fd0=%d) failed: Error = %s\n", |
| 495 | (*gsn)->fd0, strerror(errno)); |
| 496 | return -errno; |
| 497 | } |
| 498 | |
| 499 | /* Create GTP version 1 control plane socket */ |
| 500 | if (((*gsn)->fd1c = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
Pau Espin Pedrol | b9036af | 2022-11-02 18:17:56 +0100 | [diff] [blame] | 501 | rate_ctr_inc2((*gsn)->ctrg, GSN_CTR_ERR_SOCKET); |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 502 | LOGP(DLGTP, LOGL_ERROR, |
| 503 | "GTPv1 control plane socket(domain=%d, type=%d, protocol=%d) failed: Error = %s\n", |
| 504 | AF_INET, SOCK_DGRAM, 0, strerror(errno)); |
| 505 | return -errno; |
| 506 | } |
| 507 | |
| 508 | memset(&addr, 0, sizeof(addr)); |
| 509 | addr.sin_family = AF_INET; |
| 510 | addr.sin_addr = *listen; /* Same IP for user traffic and signalling */ |
| 511 | addr.sin_port = htons(GTP1C_PORT); |
| 512 | #if defined(__FreeBSD__) || defined(__APPLE__) |
| 513 | addr.sin_len = sizeof(addr); |
| 514 | #endif |
| 515 | |
| 516 | if (bind((*gsn)->fd1c, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
Pau Espin Pedrol | b9036af | 2022-11-02 18:17:56 +0100 | [diff] [blame] | 517 | rate_ctr_inc2((*gsn)->ctrg, GSN_CTR_ERR_SOCKET); |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 518 | LOGP_WITH_ADDR(DLGTP, LOGL_ERROR, addr, |
| 519 | "bind(fd1c=%d) failed: Error = %s\n", |
| 520 | (*gsn)->fd1c, strerror(errno)); |
| 521 | return -errno; |
| 522 | } |
| 523 | |
| 524 | /* Create GTP version 1 user plane socket */ |
| 525 | if (((*gsn)->fd1u = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
Pau Espin Pedrol | b9036af | 2022-11-02 18:17:56 +0100 | [diff] [blame] | 526 | rate_ctr_inc2((*gsn)->ctrg, GSN_CTR_ERR_SOCKET); |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 527 | LOGP(DLGTP, LOGL_ERROR, |
| 528 | "GTPv1 user plane socket(domain=%d, type=%d, protocol=%d) failed: Error = %s\n", |
| 529 | AF_INET, SOCK_DGRAM, 0, strerror(errno)); |
| 530 | return -errno; |
| 531 | } |
| 532 | |
| 533 | memset(&addr, 0, sizeof(addr)); |
| 534 | addr.sin_family = AF_INET; |
| 535 | addr.sin_addr = *listen; /* Same IP for user traffic and signalling */ |
| 536 | addr.sin_port = htons(GTP1U_PORT); |
| 537 | #if defined(__FreeBSD__) || defined(__APPLE__) |
| 538 | addr.sin_len = sizeof(addr); |
| 539 | #endif |
| 540 | |
| 541 | if (bind((*gsn)->fd1u, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
Pau Espin Pedrol | b9036af | 2022-11-02 18:17:56 +0100 | [diff] [blame] | 542 | rate_ctr_inc2((*gsn)->ctrg, GSN_CTR_ERR_SOCKET); |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 543 | LOGP_WITH_ADDR(DLGTP, LOGL_ERROR, addr, |
| 544 | "bind(fd1u=%d) failed: Error = %s\n", |
| 545 | (*gsn)->fd1u, strerror(errno)); |
| 546 | return -errno; |
| 547 | } |
| 548 | |
| 549 | /* Start internal queue timer */ |
| 550 | gtp_queue_timer_start(*gsn); |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | int gtp_free(struct gsn_t *gsn) |
| 556 | { |
| 557 | |
| 558 | /* Cleanup internal queue timer */ |
| 559 | osmo_timer_del(&gsn->queue_timer); |
| 560 | |
| 561 | /* Clean up retransmit queues */ |
| 562 | queue_free(gsn->queue_req); |
| 563 | queue_free(gsn->queue_resp); |
| 564 | |
| 565 | close(gsn->fd0); |
| 566 | close(gsn->fd1c); |
| 567 | close(gsn->fd1u); |
| 568 | |
Pau Espin Pedrol | b9036af | 2022-11-02 18:17:56 +0100 | [diff] [blame] | 569 | rate_ctr_group_free(gsn->ctrg); |
| 570 | |
Pau Espin Pedrol | 724ecc6 | 2022-11-02 14:57:24 +0100 | [diff] [blame] | 571 | free(gsn); |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | /* API: Register create context indication callback */ |
| 576 | int gtp_set_cb_create_context_ind(struct gsn_t *gsn, |
| 577 | int (*cb_create_context_ind) (struct pdp_t * |
| 578 | pdp)) |
| 579 | { |
| 580 | gsn->cb_create_context_ind = cb_create_context_ind; |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | int gtp_retrans(struct gsn_t *gsn) |
| 585 | { |
| 586 | /* dummy API, deprecated. */ |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | int gtp_retranstimeout(struct gsn_t *gsn, struct timeval *timeout) |
| 591 | { |
| 592 | timeout->tv_sec = 24*60*60; |
| 593 | timeout->tv_usec = 0; |
| 594 | /* dummy API, deprecated. Return a huge timer to do nothing */ |
| 595 | return 0; |
Vadim Yanitskiy | 70a4e2e | 2023-02-25 18:02:01 +0700 | [diff] [blame] | 596 | } |