Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 1 | /* |
Harald Welte | 632e843 | 2017-09-05 18:12:14 +0200 | [diff] [blame] | 2 | * OsmoGGSN - Gateway GPRS Support Node |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 3 | * Copyright (C) 2002, 2003, 2004 Mondru AB. |
Harald Welte | 632e843 | 2017-09-05 18:12:14 +0200 | [diff] [blame] | 4 | * Copyright (C) 2010-2011, 2016-2017 Harald Welte <laforge@gnumonks.org> |
| 5 | * Copyright (C) 2015-2017 sysmocom - s.f.m.c. GmbH |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 6 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 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. |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 11 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * gtp.c: Contains all GTP functionality. Should be able to handle multiple |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 16 | * tunnels in the same program. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 17 | * |
| 18 | * TODO: |
| 19 | * - Do we need to handle fragmentation? |
| 20 | */ |
| 21 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 22 | #ifdef __linux__ |
| 23 | #define _GNU_SOURCE 1 |
| 24 | #endif |
| 25 | |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 26 | #include <osmocom/core/logging.h> |
| 27 | #include <osmocom/core/utils.h> |
| 28 | |
Holger Hans Peter Freyther | 5816bcf | 2014-04-04 11:43:09 +0200 | [diff] [blame] | 29 | #if defined(__FreeBSD__) |
| 30 | #include <sys/endian.h> |
| 31 | #endif |
| 32 | |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 33 | #include "../config.h" |
| 34 | #ifdef HAVE_STDINT_H |
| 35 | #include <stdint.h> |
| 36 | #endif |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 37 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 38 | #include <stdio.h> |
| 39 | #include <stdarg.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <sys/time.h> |
| 42 | #include <sys/types.h> |
| 43 | #include <sys/socket.h> |
| 44 | #include <netinet/in.h> |
| 45 | #include <arpa/inet.h> |
| 46 | #include <sys/stat.h> |
| 47 | #include <time.h> |
| 48 | #include <unistd.h> |
| 49 | #include <string.h> |
| 50 | #include <errno.h> |
| 51 | #include <fcntl.h> |
Alexander Huemer | db852a1 | 2015-11-06 20:59:01 +0100 | [diff] [blame] | 52 | #include <inttypes.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 53 | |
| 54 | #include <arpa/inet.h> |
| 55 | |
jjako | bae2cd4 | 2004-01-09 12:04:39 +0000 | [diff] [blame] | 56 | /* #include <stdint.h> ISO C99 types */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 57 | |
| 58 | #include "pdp.h" |
| 59 | #include "gtp.h" |
| 60 | #include "gtpie.h" |
| 61 | #include "queue.h" |
| 62 | |
Harald Welte | 95848ba | 2011-11-02 18:17:50 +0100 | [diff] [blame] | 63 | /* According to section 14.2 of 3GPP TS 29.006 version 6.9.0 */ |
| 64 | #define N3_REQUESTS 5 |
| 65 | |
| 66 | #define T3_REQUEST 3 |
| 67 | |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 68 | /* Error reporting functions */ |
| 69 | |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 70 | #define GTP_LOGPKG(pri, peer, pack, len, fmt, args...) \ |
| 71 | logp2(DLGTP, pri, __FILE__, __LINE__, 0, \ |
| 72 | "Packet from %s:%u, length: %d content: %s: " fmt, \ |
| 73 | inet_ntoa((peer)->sin_addr), htons((peer)->sin_port), \ |
| 74 | len, osmo_hexdump((const uint8_t *) pack, len), \ |
| 75 | ##args); |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 76 | |
Neels Hofmeyr | 9b09738 | 2015-10-12 14:00:19 +0200 | [diff] [blame] | 77 | #define LOGP_WITH_ADDR(ss, level, addr, fmt, args...) \ |
| 78 | LOGP(ss, level, "addr(%s:%d) " fmt, \ |
| 79 | inet_ntoa((addr).sin_addr), htons((addr).sin_port), \ |
| 80 | ##args); |
| 81 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 82 | /* API Functions */ |
| 83 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 84 | const char *gtp_version() |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 85 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 86 | return VERSION; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Max | e661277 | 2018-01-11 18:25:37 +0100 | [diff] [blame] | 89 | const struct value_string gtp_type_names[] = { |
| 90 | { GTP_ECHO_REQ, "Echo Request" }, |
| 91 | { GTP_ECHO_RSP, "Echo Response" }, |
| 92 | { GTP_NOT_SUPPORTED, "Version Not Supported" }, |
| 93 | { GTP_ALIVE_REQ, "Node Alive Request" }, |
| 94 | { GTP_ALIVE_RSP, "Node Alive Response" }, |
| 95 | { GTP_REDIR_REQ, "Redirection Request" }, |
| 96 | { GTP_REDIR_RSP, "Redirection Response" }, |
| 97 | { GTP_CREATE_PDP_REQ, "Create PDP Context Request" }, |
| 98 | { GTP_CREATE_PDP_RSP, "Create PDP Context Response" }, |
| 99 | { GTP_UPDATE_PDP_REQ, "Update PDP Context Request" }, |
| 100 | { GTP_UPDATE_PDP_RSP, "Update PDP Context Response" }, |
| 101 | { GTP_DELETE_PDP_REQ, "Delete PDP Context Request" }, |
| 102 | { GTP_DELETE_PDP_RSP, "Delete PDP Context Response" }, |
| 103 | { GTP_ERROR, "Error Indication" }, |
| 104 | { GTP_PDU_NOT_REQ, "PDU Notification Request" }, |
| 105 | { GTP_PDU_NOT_RSP, "PDU Notification Response" }, |
| 106 | { GTP_PDU_NOT_REJ_REQ, "PDU Notification Reject Request" }, |
| 107 | { GTP_PDU_NOT_REJ_RSP, "PDU Notification Reject Response" }, |
| 108 | { GTP_SUPP_EXT_HEADER, "Supported Extension Headers Notification" }, |
| 109 | { GTP_SND_ROUTE_REQ, "Send Routeing Information for GPRS Request" }, |
| 110 | { GTP_SND_ROUTE_RSP, "Send Routeing Information for GPRS Response" }, |
| 111 | { GTP_FAILURE_REQ, "Failure Report Request" }, |
| 112 | { GTP_FAILURE_RSP, "Failure Report Response" }, |
| 113 | { GTP_MS_PRESENT_REQ, "Note MS GPRS Present Request" }, |
| 114 | { GTP_MS_PRESENT_RSP, "Note MS GPRS Present Response" }, |
| 115 | { GTP_IDEN_REQ, "Identification Request" }, |
| 116 | { GTP_IDEN_RSP, "Identification Response" }, |
| 117 | { GTP_SGSN_CONTEXT_REQ,"SGSN Context Request" }, |
| 118 | { GTP_SGSN_CONTEXT_RSP,"SGSN Context Response" }, |
| 119 | { GTP_SGSN_CONTEXT_ACK,"SGSN Context Acknowledge" }, |
| 120 | { GTP_FWD_RELOC_REQ, "Forward Relocation Request" }, |
| 121 | { GTP_FWD_RELOC_RSP, "Forward Relocation Response" }, |
| 122 | { GTP_FWD_RELOC_COMPL, "Forward Relocation Complete" }, |
| 123 | { GTP_RELOC_CANCEL_REQ,"Relocation Cancel Request" }, |
| 124 | { GTP_RELOC_CANCEL_RSP,"Relocation Cancel Response" }, |
| 125 | { GTP_FWD_SRNS, "Forward SRNS Context" }, |
| 126 | { GTP_FWD_RELOC_ACK, "Forward Relocation Complete Acknowledge" }, |
| 127 | { GTP_FWD_SRNS_ACK, "Forward SRNS Context Acknowledge" }, |
| 128 | { GTP_DATA_TRAN_REQ, "Data Record Transfer Request" }, |
| 129 | { GTP_DATA_TRAN_RSP, "Data Record Transfer Response" }, |
| 130 | { GTP_GPDU, "G-PDU" }, |
| 131 | { 0, NULL } |
| 132 | }; |
| 133 | |
Pau Espin Pedrol | eefa30d | 2019-05-31 15:42:49 +0200 | [diff] [blame] | 134 | /* Deprecated, use gtp_pdp_newpdp() instead */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 135 | int gtp_newpdp(struct gsn_t *gsn, struct pdp_t **pdp, |
| 136 | uint64_t imsi, uint8_t nsapi) |
| 137 | { |
Harald Welte | e257be1 | 2017-08-12 14:55:09 +0200 | [diff] [blame] | 138 | int rc; |
Pau Espin Pedrol | eefa30d | 2019-05-31 15:42:49 +0200 | [diff] [blame] | 139 | rc = gtp_pdp_newpdp(gsn, pdp, imsi, nsapi, NULL); |
Harald Welte | e257be1 | 2017-08-12 14:55:09 +0200 | [diff] [blame] | 140 | return rc; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 143 | int gtp_freepdp(struct gsn_t *gsn, struct pdp_t *pdp) |
| 144 | { |
Pau Espin Pedrol | 0d0b059 | 2019-05-29 20:42:09 +0200 | [diff] [blame] | 145 | if (gsn->cb_delete_context) |
| 146 | gsn->cb_delete_context(pdp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 147 | return pdp_freepdp(pdp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Pau Espin Pedrol | 0d0b059 | 2019-05-29 20:42:09 +0200 | [diff] [blame] | 150 | /* Free pdp and all its secondary PDP contexts. Must be called on the primary PDP context. */ |
| 151 | int gtp_freepdp_teardown(struct gsn_t *gsn, struct pdp_t *pdp) |
| 152 | { |
| 153 | int n; |
| 154 | struct pdp_t *secondary_pdp; |
| 155 | OSMO_ASSERT(!pdp->secondary); |
| 156 | |
| 157 | for (n = 0; n < PDP_MAXNSAPI; n++) { |
| 158 | if (pdp->secondary_tei[n]) { |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 159 | if (gtp_pdp_getgtp1(gsn, &secondary_pdp, |
| 160 | pdp->secondary_tei[n])) { |
Pau Espin Pedrol | 0d0b059 | 2019-05-29 20:42:09 +0200 | [diff] [blame] | 161 | LOGP(DLGTP, LOGL_ERROR, |
| 162 | "Unknown secondary PDP context\n"); |
| 163 | continue; |
| 164 | } |
| 165 | if (pdp != secondary_pdp) { |
| 166 | gtp_freepdp(gsn, secondary_pdp); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return gtp_freepdp(gsn, pdp); |
| 172 | } |
| 173 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 174 | /* gtp_gpdu */ |
| 175 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 176 | extern int gtp_fd(struct gsn_t *gsn) |
| 177 | { |
| 178 | return gsn->fd0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* gtp_decaps */ |
| 182 | /* gtp_retrans */ |
| 183 | /* gtp_retranstimeout */ |
| 184 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 185 | int gtp_set_cb_unsup_ind(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 186 | int (*cb) (struct sockaddr_in * peer)) |
| 187 | { |
| 188 | gsn->cb_unsup_ind = cb; |
| 189 | return 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 190 | } |
| 191 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 192 | int gtp_set_cb_extheader_ind(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 193 | int (*cb) (struct sockaddr_in * peer)) |
| 194 | { |
| 195 | gsn->cb_extheader_ind = cb; |
| 196 | return 0; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Pau Espin Pedrol | f32c6a9 | 2021-05-03 17:58:55 +0200 | [diff] [blame] | 199 | int gtp_set_cb_ran_info_relay_ind(struct gsn_t *gsn, |
| 200 | int (*cb) (struct sockaddr_in * peer, union gtpie_member **ie)) |
| 201 | { |
| 202 | gsn->cb_ran_info_relay_ind = cb; |
| 203 | return 0; |
| 204 | } |
| 205 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 206 | /* API: Initialise delete context callback */ |
| 207 | /* Called whenever a pdp context is deleted for any reason */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 208 | int gtp_set_cb_delete_context(struct gsn_t *gsn, int (*cb) (struct pdp_t * pdp)) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 209 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 210 | gsn->cb_delete_context = cb; |
| 211 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 212 | } |
| 213 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 214 | int gtp_set_cb_conf(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 215 | int (*cb) (int type, int cause, |
| 216 | struct pdp_t * pdp, void *cbp)) |
| 217 | { |
| 218 | gsn->cb_conf = cb; |
| 219 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 222 | static void emit_cb_recovery(struct gsn_t *gsn, struct sockaddr_in * peer, |
| 223 | struct pdp_t * pdp, uint8_t recovery) |
| 224 | { |
| 225 | if (gsn->cb_recovery) |
| 226 | gsn->cb_recovery(peer, recovery); |
| 227 | if (gsn->cb_recovery2) |
| 228 | gsn->cb_recovery2(peer, pdp, recovery); |
Pau Espin Pedrol | 5d8b226 | 2019-08-22 15:00:00 +0200 | [diff] [blame] | 229 | if (gsn->cb_recovery3) |
| 230 | gsn->cb_recovery3(gsn, peer, pdp, recovery); |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Harald Welte | 629e986 | 2010-12-24 20:58:09 +0100 | [diff] [blame] | 233 | int gtp_set_cb_recovery(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 234 | int (*cb) (struct sockaddr_in * peer, uint8_t recovery)) |
| 235 | { |
| 236 | gsn->cb_recovery = cb; |
| 237 | return 0; |
Harald Welte | 629e986 | 2010-12-24 20:58:09 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 240 | /* cb_recovery() |
| 241 | * pdp may be NULL if Recovery IE was received from a message independent |
| 242 | * of any PDP ctx (such as Echo Response), or because pdp ctx is unknown to the |
| 243 | * local setup. In case pdp is known, caller may want to keep that pdp alive to |
| 244 | * handle subsequent msg cb as this specific pdp ctx is still valid according to |
| 245 | * specs. |
| 246 | */ |
| 247 | int gtp_set_cb_recovery2(struct gsn_t *gsn, |
| 248 | int (*cb_recovery2) (struct sockaddr_in * peer, struct pdp_t * pdp, uint8_t recovery)) |
| 249 | { |
| 250 | gsn->cb_recovery2 = cb_recovery2; |
| 251 | return 0; |
| 252 | } |
| 253 | |
Pau Espin Pedrol | 5d8b226 | 2019-08-22 15:00:00 +0200 | [diff] [blame] | 254 | /* cb_recovery() |
| 255 | * pdp may be NULL if Recovery IE was received from a message independent |
| 256 | * of any PDP ctx (such as Echo Response), or because pdp ctx is unknown to the |
| 257 | * local setup. In case pdp is known, caller may want to keep that pdp alive to |
| 258 | * handle subsequent msg cb as this specific pdp ctx is still valid according to |
| 259 | * specs. |
| 260 | */ |
| 261 | int gtp_set_cb_recovery3(struct gsn_t *gsn, |
| 262 | int (*cb_recovery3) (struct gsn_t *gsn, struct sockaddr_in *peer, |
| 263 | struct pdp_t *pdp, uint8_t recovery)) |
| 264 | { |
| 265 | gsn->cb_recovery3 = cb_recovery3; |
| 266 | return 0; |
| 267 | } |
| 268 | |
Pau Espin Pedrol | 7b38af5 | 2018-01-25 18:35:33 +0100 | [diff] [blame] | 269 | int gtp_set_cb_data_ind(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 270 | int (*cb_data_ind) (struct pdp_t * pdp, |
| 271 | void *pack, unsigned len)) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 272 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 273 | gsn->cb_data_ind = cb_data_ind; |
| 274 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 275 | } |
| 276 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 277 | /** |
| 278 | * get_default_gtp() |
| 279 | * Generate a GPRS Tunneling Protocol signalling packet header, depending |
| 280 | * on GTP version and message type. pdp is used for teid/flow label. |
| 281 | * *packet must be allocated by the calling function, and be large enough |
| 282 | * to hold the packet header. |
| 283 | * returns the length of the header. 0 on error. |
| 284 | **/ |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 285 | static unsigned int get_default_gtp(uint8_t version, uint8_t type, void *packet) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 286 | { |
| 287 | struct gtp0_header *gtp0_default = (struct gtp0_header *)packet; |
| 288 | struct gtp1_header_long *gtp1_default = |
| 289 | (struct gtp1_header_long *)packet; |
| 290 | switch (version) { |
| 291 | case 0: |
| 292 | /* Initialise "standard" GTP0 header */ |
| 293 | memset(gtp0_default, 0, sizeof(struct gtp0_header)); |
| 294 | gtp0_default->flags = 0x1e; |
| 295 | gtp0_default->type = hton8(type); |
| 296 | gtp0_default->spare1 = 0xff; |
| 297 | gtp0_default->spare2 = 0xff; |
| 298 | gtp0_default->spare3 = 0xff; |
| 299 | gtp0_default->number = 0xff; |
| 300 | return GTP0_HEADER_SIZE; |
| 301 | case 1: |
| 302 | /* Initialise "standard" GTP1 header */ |
| 303 | /* 29.060: 8.2: S=1 and PN=0 */ |
| 304 | /* 29.060 9.3.1: For GTP-U messages Echo Request, Echo Response */ |
| 305 | /* and Supported Extension Headers Notification, the S field shall be */ |
| 306 | /* set to 1 */ |
| 307 | /* Currently extension headers are not supported */ |
| 308 | memset(gtp1_default, 0, sizeof(struct gtp1_header_long)); |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 309 | /* No extension, enable sequence, no N-PDU */ |
| 310 | gtp1_default->flags = GTPHDR_F_VER(1) | GTP1HDR_F_GTP1 | GTP1HDR_F_SEQ; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 311 | gtp1_default->type = hton8(type); |
| 312 | return GTP1_HEADER_SIZE_LONG; |
| 313 | default: |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 314 | LOGP(DLGTP, LOGL_ERROR, |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 315 | "Unknown GTP packet version: %d\n", version); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 316 | return 0; |
| 317 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 318 | } |
| 319 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 320 | /** |
| 321 | * get_seq() |
| 322 | * Get sequence number of a packet. |
| 323 | * Returns 0 on error |
| 324 | **/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 325 | static uint16_t get_seq(void *pack) |
| 326 | { |
| 327 | union gtp_packet *packet = (union gtp_packet *)pack; |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 328 | uint8_t ver = GTPHDR_F_GET_VER(packet->flags); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 329 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 330 | if (ver == 0) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 331 | return ntoh16(packet->gtp0.h.seq); |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 332 | } else if (ver == 1 && (packet->flags & GTP1HDR_F_SEQ)) { /* Version 1 with seq */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 333 | return ntoh16(packet->gtp1l.h.seq); |
| 334 | } else { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 335 | return 0; |
| 336 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /** |
| 340 | * get_tid() |
| 341 | * Get tunnel identifier of a packet. |
| 342 | * Returns 0 on error |
| 343 | **/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 344 | static uint64_t get_tid(void *pack) |
| 345 | { |
| 346 | union gtp_packet *packet = (union gtp_packet *)pack; |
| 347 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 348 | if (GTPHDR_F_GET_VER(packet->flags) == 0) { /* Version 0 */ |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 349 | return be64toh(packet->gtp0.h.tid); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 350 | } |
| 351 | return 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
| 355 | * get_hlen() |
| 356 | * Get the header length of a packet. |
| 357 | * Returns 0 on error |
| 358 | **/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 359 | static uint16_t get_hlen(void *pack) |
| 360 | { |
| 361 | union gtp_packet *packet = (union gtp_packet *)pack; |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 362 | uint8_t ver = GTPHDR_F_GET_VER(packet->flags); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 363 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 364 | if (ver == 0) { /* Version 0 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 365 | return GTP0_HEADER_SIZE; |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 366 | } else if (ver == 1 && (packet->flags & 0x07) == 0) { /* Short version 1 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 367 | return GTP1_HEADER_SIZE_SHORT; |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 368 | } else if (ver == 1) { /* Version 1 with seq/n-pdu/ext */ |
| 369 | return GTP1_HEADER_SIZE_LONG; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 370 | } else { |
Harald Welte | d37b80a | 2016-12-15 18:33:15 +0100 | [diff] [blame] | 371 | LOGP(DLGTP, LOGL_ERROR, "Unknown packet flags: 0x%02x\n", packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 372 | return 0; |
| 373 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /** |
| 377 | * get_tei() |
| 378 | * Get the tunnel endpoint identifier (flow label) of a packet. |
| 379 | * Returns 0xffffffff on error. |
| 380 | **/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 381 | static uint32_t get_tei(void *pack) |
| 382 | { |
| 383 | union gtp_packet *packet = (union gtp_packet *)pack; |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 384 | uint8_t ver = GTPHDR_F_GET_VER(packet->flags); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 385 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 386 | if (ver == 0) { /* Version 0 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 387 | return ntoh16(packet->gtp0.h.flow); |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 388 | } else if (ver == 1) { /* Version 1 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 389 | return ntoh32(packet->gtp1l.h.tei); |
| 390 | } else { |
Harald Welte | d37b80a | 2016-12-15 18:33:15 +0100 | [diff] [blame] | 391 | LOGP(DLGTP, LOGL_ERROR, "Unknown packet flags: 0x%02x\n", packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 392 | return 0xffffffff; |
| 393 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 394 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 395 | |
Pau Espin Pedrol | c94837c | 2019-08-28 19:44:20 +0200 | [diff] [blame] | 396 | static int queue_timer_retrans(struct gsn_t *gsn) |
| 397 | { |
| 398 | /* Retransmit any outstanding packets */ |
| 399 | /* Remove from queue if maxretrans exceeded */ |
| 400 | time_t now; |
| 401 | struct qmsg_t *qmsg; |
| 402 | now = time(NULL); |
| 403 | /*printf("Retrans: New beginning %d\n", (int) now); */ |
| 404 | |
| 405 | /* get first element in queue, as long as the timeout of that |
| 406 | * element has expired */ |
| 407 | while ((!queue_getfirst(gsn->queue_req, &qmsg)) && |
| 408 | (qmsg->timeout <= now)) { |
| 409 | /*printf("Retrans timeout found: %d\n", (int) time(NULL)); */ |
| 410 | if (qmsg->retrans > N3_REQUESTS) { /* To many retrans */ |
| 411 | LOGP(DLGTP, LOGL_NOTICE, "Timeout of seq %" PRIu16 "\n", |
| 412 | qmsg->seq); |
| 413 | if (gsn->cb_conf) |
| 414 | gsn->cb_conf(qmsg->type, EOF, NULL, qmsg->cbp); |
| 415 | queue_freemsg(gsn->queue_req, qmsg); |
| 416 | } else { |
| 417 | LOGP(DLGTP, LOGL_INFO, "Retransmit (%d) of seq %" PRIu16 "\n", |
| 418 | qmsg->retrans, qmsg->seq); |
| 419 | if (sendto(qmsg->fd, &qmsg->p, qmsg->l, 0, |
| 420 | (struct sockaddr *)&qmsg->peer, |
| 421 | sizeof(struct sockaddr_in)) < 0) { |
| 422 | gsn->err_sendto++; |
| 423 | LOGP(DLGTP, LOGL_ERROR, |
| 424 | "Sendto(fd0=%d, msg=%lx, len=%d) failed: Error = %s\n", |
| 425 | gsn->fd0, (unsigned long)&qmsg->p, |
| 426 | qmsg->l, strerror(errno)); |
| 427 | } |
| 428 | queue_back(gsn->queue_req, qmsg); |
| 429 | qmsg->timeout = now + T3_REQUEST; |
| 430 | qmsg->retrans++; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* Also clean up reply timeouts */ |
| 435 | while ((!queue_getfirst(gsn->queue_resp, &qmsg)) && |
| 436 | (qmsg->timeout < now)) { |
| 437 | /*printf("Retrans (reply) timeout found: %d\n", (int) time(NULL)); */ |
| 438 | queue_freemsg(gsn->queue_resp, qmsg); |
| 439 | } |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static int queue_timer_retranstimeout(struct gsn_t *gsn, struct timeval *timeout) |
| 445 | { |
| 446 | time_t now, later, diff; |
| 447 | struct qmsg_t *qmsg; |
| 448 | timeout->tv_usec = 0; |
| 449 | |
| 450 | if (queue_getfirst(gsn->queue_req, &qmsg)) { |
| 451 | timeout->tv_sec = 10; |
| 452 | } else { |
| 453 | now = time(NULL); |
| 454 | later = qmsg->timeout; |
| 455 | timeout->tv_sec = later - now; |
| 456 | if (timeout->tv_sec < 0) |
| 457 | timeout->tv_sec = 0; /* No negative allowed */ |
| 458 | if (timeout->tv_sec > 10) |
| 459 | timeout->tv_sec = 10; /* Max sleep for 10 sec */ |
| 460 | } |
| 461 | |
| 462 | if (queue_getfirst(gsn->queue_resp, &qmsg)) { |
| 463 | /* already set by queue_req, do nothing */ |
| 464 | } else { /* trigger faster if earlier timeout exists in queue_resp */ |
| 465 | now = time(NULL); |
| 466 | later = qmsg->timeout; |
| 467 | diff = later - now; |
| 468 | if (diff < 0) |
| 469 | diff = 0; |
| 470 | if (diff < timeout->tv_sec) |
| 471 | timeout->tv_sec = diff; |
| 472 | } |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | static void queue_timer_start(struct gsn_t *gsn) |
| 478 | { |
| 479 | struct timeval next; |
| 480 | |
| 481 | /* Retrieve next retransmission as timeval */ |
| 482 | queue_timer_retranstimeout(gsn, &next); |
| 483 | |
| 484 | /* re-schedule the timer */ |
| 485 | osmo_timer_schedule(&gsn->queue_timer, next.tv_sec, next.tv_usec/1000); |
| 486 | } |
| 487 | |
| 488 | /* timer callback for libgtp retransmission and ping */ |
| 489 | static void queue_timer_cb(void *data) |
| 490 | { |
| 491 | struct gsn_t *gsn = data; |
| 492 | |
| 493 | /* do all the retransmissions as needed */ |
| 494 | queue_timer_retrans(gsn); |
| 495 | |
| 496 | queue_timer_start(gsn); |
| 497 | } |
| 498 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 499 | /* *********************************************************** |
| 500 | * Reliable delivery of signalling messages |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 501 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 502 | * Sequence numbers are used for both signalling messages and |
| 503 | * data messages. |
| 504 | * |
| 505 | * For data messages each tunnel maintains a sequence counter, |
| 506 | * which is incremented by one each time a new data message |
| 507 | * is sent. The sequence number starts at (0) zero at tunnel |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 508 | * establishment, and wraps around at 65535 (29.060 9.3.1.1 |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 509 | * and 09.60 8.1.1.1). The sequence numbers are either ignored, |
| 510 | * or can be used to check the validity of the message in the |
| 511 | * receiver, or for reordering af packets. |
| 512 | * |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 513 | * For signalling messages the sequence number is used by |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 514 | * signalling messages for which a response is defined. A response |
| 515 | * message should copy the sequence from the corresponding request |
| 516 | * message. The sequence number "unambiguously" identifies a request |
| 517 | * message within a given path, with a path being defined as a set of |
| 518 | * two endpoints (29.060 8.2, 29.060 7.6, 09.60 7.8). "All request |
| 519 | * messages shall be responded to, and all response messages associated |
| 520 | * with a certain request shall always include the same information" |
| 521 | * |
| 522 | * We take this to mean that the GSN transmitting a request is free to |
| 523 | * choose the sequence number, as long as it is unique within a given path. |
| 524 | * It means that we are allowed to count backwards, or roll over at 17 |
| 525 | * if we prefer that. It also means that we can use the same counter for |
| 526 | * all paths. This has the advantage that the transmitted request sequence |
| 527 | * numbers are unique within each GSN, and also we dont have to mess around |
| 528 | * with path setup and teardown. |
| 529 | * |
| 530 | * If a response message is lost, the request will be retransmitted, and |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 531 | * the receiving GSN will receive a "duplicated" request. The standard |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 532 | * requires the receiving GSN to send a response, with the same information |
| 533 | * as in the original response. For most messages this happens automatically: |
| 534 | * |
Pau Espin Pedrol | de72d26 | 2019-05-29 18:17:05 +0200 | [diff] [blame] | 535 | * Echo: Automatically duplicates the original response |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 536 | * Create pdp context: The SGSN may send create context request even if |
| 537 | * a context allready exist (imsi+nsapi?). This means that the reply will |
Pau Espin Pedrol | de72d26 | 2019-05-29 18:17:05 +0200 | [diff] [blame] | 538 | automatically duplicate the original response. It might however have |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 539 | * side effects in the application which is asked twice to validate |
| 540 | * the login. |
Pau Espin Pedrol | de72d26 | 2019-05-29 18:17:05 +0200 | [diff] [blame] | 541 | * Update pdp context: Automatically duplicates the original response??? |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 542 | * Delete pdp context. Automatically in gtp0, but in gtp1 will generate |
| 543 | * a nonexist reply message. |
| 544 | * |
| 545 | * The correct solution will be to make a queue containing response messages. |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 546 | * This queue should be checked whenever a request is received. If the |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 547 | * response is allready in the queue that response should be transmitted. |
| 548 | * It should be possible to find messages in this queue on the basis of |
| 549 | * the sequence number and peer GSN IP address (The sequense number is unique |
| 550 | * within each path). This need to be implemented by a hash table. Furthermore |
| 551 | * it should be possibly to delete messages based on a timeout. This can be |
| 552 | * achieved by means of a linked list. The timeout value need to be larger |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 553 | * than T3-RESPONSE * N3-REQUESTS (recommended value 5). These timers are |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 554 | * set in the peer GSN, so there is no way to know these parameters. On the |
| 555 | * other hand the timeout value need to be so small that we do not receive |
| 556 | * wraparound sequence numbere before the message is deleted. 60 seconds is |
| 557 | * probably not a bad choise. |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 558 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 559 | * This queue however is first really needed from gtp1. |
| 560 | * |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 561 | * gtp_req: |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 562 | * Send off a signalling message with appropiate sequence |
| 563 | * number. Store packet in queue. |
| 564 | * gtp_conf: |
| 565 | * Remove an incoming confirmation from the queue |
| 566 | * gtp_resp: |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 567 | * Send off a response to a request. Use the same sequence |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 568 | * number in the response as in the request. |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 569 | * gtp_notification: |
| 570 | * Send off a notification message. This is neither a request nor |
| 571 | * a response. Both TEI and SEQ are zero. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 572 | * gtp_retrans: |
| 573 | * Retransmit any outstanding packets which have exceeded |
| 574 | * a predefined timeout. |
| 575 | *************************************************************/ |
| 576 | |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 577 | static int gtp_req(struct gsn_t *gsn, uint8_t version, struct pdp_t *pdp, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 578 | union gtp_packet *packet, int len, |
| 579 | struct in_addr *inetaddr, void *cbp) |
| 580 | { |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 581 | uint8_t ver = GTPHDR_F_GET_VER(packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 582 | struct sockaddr_in addr; |
| 583 | struct qmsg_t *qmsg; |
| 584 | int fd; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 585 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 586 | memset(&addr, 0, sizeof(addr)); |
| 587 | addr.sin_family = AF_INET; |
| 588 | addr.sin_addr = *inetaddr; |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 589 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 590 | addr.sin_len = sizeof(addr); |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 591 | #endif |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 592 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 593 | if (ver == 0) { /* Version 0 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 594 | addr.sin_port = htons(GTP0_PORT); |
| 595 | packet->gtp0.h.length = hton16(len - GTP0_HEADER_SIZE); |
| 596 | packet->gtp0.h.seq = hton16(gsn->seq_next); |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 597 | if (pdp) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 598 | packet->gtp0.h.tid = |
Pablo Neira Ayuso | 746b944 | 2014-03-24 17:58:27 +0100 | [diff] [blame] | 599 | htobe64(pdp_gettid(pdp->imsi, pdp->nsapi)); |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 600 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 601 | if (pdp && ((packet->gtp0.h.type == GTP_GPDU) |
| 602 | || (packet->gtp0.h.type == GTP_ERROR))) |
| 603 | packet->gtp0.h.flow = hton16(pdp->flru); |
| 604 | else if (pdp) |
| 605 | packet->gtp0.h.flow = hton16(pdp->flrc); |
| 606 | fd = gsn->fd0; |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 607 | } else if (ver == 1 && (packet->flags & GTP1HDR_F_SEQ)) { /* Version 1 with seq */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 608 | addr.sin_port = htons(GTP1C_PORT); |
| 609 | packet->gtp1l.h.length = hton16(len - GTP1_HEADER_SIZE_SHORT); |
| 610 | packet->gtp1l.h.seq = hton16(gsn->seq_next); |
| 611 | if (pdp && ((packet->gtp1l.h.type == GTP_GPDU) || |
| 612 | (packet->gtp1l.h.type == GTP_ERROR))) |
| 613 | packet->gtp1l.h.tei = hton32(pdp->teid_gn); |
| 614 | else if (pdp) |
| 615 | packet->gtp1l.h.tei = hton32(pdp->teic_gn); |
| 616 | fd = gsn->fd1c; |
| 617 | } else { |
Harald Welte | d37b80a | 2016-12-15 18:33:15 +0100 | [diff] [blame] | 618 | LOGP(DLGTP, LOGL_ERROR, "Unknown packet flags: 0x%02x\n", packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 619 | return -1; |
| 620 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 621 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 622 | if (sendto(fd, packet, len, 0, |
| 623 | (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 624 | gsn->err_sendto++; |
Max | 14b1b63 | 2017-08-21 20:14:59 +0200 | [diff] [blame] | 625 | LOGP(DLGTP, LOGL_ERROR, "Sendto(fd=%d, msg=%lx, len=%d, dst=%s) failed: Error = %s\n", fd, |
| 626 | (unsigned long)&packet, len, inet_ntoa(addr.sin_addr), strerror(errno)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 627 | return -1; |
| 628 | } |
| 629 | |
| 630 | /* Use new queue structure */ |
| 631 | if (queue_newmsg(gsn->queue_req, &qmsg, &addr, gsn->seq_next)) { |
| 632 | gsn->err_queuefull++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 633 | LOGP(DLGTP, LOGL_ERROR, |
| 634 | "Retransmit queue is full\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 635 | } else { |
| 636 | memcpy(&qmsg->p, packet, sizeof(union gtp_packet)); |
| 637 | qmsg->l = len; |
Harald Welte | 95848ba | 2011-11-02 18:17:50 +0100 | [diff] [blame] | 638 | qmsg->timeout = time(NULL) + T3_REQUEST; /* When to timeout */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 639 | qmsg->retrans = 0; /* No retransmissions so far */ |
| 640 | qmsg->cbp = cbp; |
| 641 | qmsg->type = ntoh8(packet->gtp0.h.type); |
| 642 | qmsg->fd = fd; |
Pau Espin Pedrol | 623c5b3 | 2019-08-16 13:20:09 +0200 | [diff] [blame] | 643 | if (pdp) /* echo requests are not pdp-bound */ |
| 644 | llist_add(&qmsg->entry, &pdp->qmsg_list_req); |
Pau Espin Pedrol | c94837c | 2019-08-28 19:44:20 +0200 | [diff] [blame] | 645 | |
| 646 | /* Rearm timer: Retrans time for qmsg just queued may be required |
| 647 | before an existing one (for instance a gtp echo req) */ |
| 648 | queue_timer_start(gsn); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 649 | } |
| 650 | gsn->seq_next++; /* Count up this time */ |
| 651 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Alexander Couzens | e1412d9 | 2018-09-16 05:10:03 +0200 | [diff] [blame] | 654 | |
| 655 | /** |
| 656 | * @brief clear the request and response queue. Useful for debugging to reset "some" state. |
| 657 | * @param gsn The GGSN instance |
| 658 | */ |
| 659 | void gtp_clear_queues(struct gsn_t *gsn) |
| 660 | { |
| 661 | struct qmsg_t *qmsg; |
| 662 | |
| 663 | while (!queue_getfirst(gsn->queue_req, &qmsg)) { |
| 664 | queue_freemsg(gsn->queue_req, qmsg); |
| 665 | } |
| 666 | |
| 667 | while (!queue_getfirst(gsn->queue_resp, &qmsg)) { |
| 668 | queue_freemsg(gsn->queue_resp, qmsg); |
| 669 | } |
| 670 | } |
| 671 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 672 | /* gtp_conf |
| 673 | * Remove signalling packet from retransmission queue. |
| 674 | * return 0 on success, EOF if packet was not found */ |
| 675 | |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 676 | static int gtp_conf(struct gsn_t *gsn, uint8_t version, struct sockaddr_in *peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 677 | union gtp_packet *packet, int len, uint8_t * type, void **cbp) |
| 678 | { |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 679 | uint8_t ver = GTPHDR_F_GET_VER(packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 680 | uint16_t seq; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 681 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 682 | if (ver == 0) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 683 | seq = ntoh16(packet->gtp0.h.seq); |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 684 | else if (ver == 1 && (packet->gtp1l.h.flags & GTP1HDR_F_SEQ)) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 685 | seq = ntoh16(packet->gtp1l.h.seq); |
| 686 | else { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 687 | GTP_LOGPKG(LOGL_ERROR, peer, packet, len, |
| 688 | "Unknown GTP packet version\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 689 | return EOF; |
| 690 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 691 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 692 | if (queue_freemsg_seq(gsn->queue_req, peer, seq, type, cbp)) { |
| 693 | gsn->err_seq++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 694 | GTP_LOGPKG(LOGL_ERROR, peer, packet, len, |
| 695 | "Confirmation packet not found in queue\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 696 | return EOF; |
| 697 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 698 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 699 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 702 | int gtp_retrans(struct gsn_t *gsn) |
| 703 | { |
Pau Espin Pedrol | c94837c | 2019-08-28 19:44:20 +0200 | [diff] [blame] | 704 | /* dummy API, deprecated. */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 705 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 708 | int gtp_retranstimeout(struct gsn_t *gsn, struct timeval *timeout) |
| 709 | { |
Pau Espin Pedrol | c94837c | 2019-08-28 19:44:20 +0200 | [diff] [blame] | 710 | timeout->tv_sec = 24*60*60; |
Pau Espin Pedrol | ceac078 | 2019-05-27 17:39:45 +0200 | [diff] [blame] | 711 | timeout->tv_usec = 0; |
Pau Espin Pedrol | c94837c | 2019-08-28 19:44:20 +0200 | [diff] [blame] | 712 | /* dummy API, deprecated. Return a huge timer to do nothing */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 713 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 716 | static int gtp_resp(uint8_t version, struct gsn_t *gsn, struct pdp_t *pdp, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 717 | union gtp_packet *packet, int len, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 718 | struct sockaddr_in *peer, int fd, uint16_t seq, uint64_t tid) |
| 719 | { |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 720 | uint8_t ver = GTPHDR_F_GET_VER(packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 721 | struct qmsg_t *qmsg; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 722 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 723 | if (ver == 0) { /* Version 0 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 724 | packet->gtp0.h.length = hton16(len - GTP0_HEADER_SIZE); |
| 725 | packet->gtp0.h.seq = hton16(seq); |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 726 | packet->gtp0.h.tid = htobe64(tid); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 727 | if (pdp && ((packet->gtp0.h.type == GTP_GPDU) || |
| 728 | (packet->gtp0.h.type == GTP_ERROR))) |
| 729 | packet->gtp0.h.flow = hton16(pdp->flru); |
| 730 | else if (pdp) |
| 731 | packet->gtp0.h.flow = hton16(pdp->flrc); |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 732 | } else if (ver == 1 && (packet->flags & GTP1HDR_F_SEQ)) { /* Version 1 with seq */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 733 | packet->gtp1l.h.length = hton16(len - GTP1_HEADER_SIZE_SHORT); |
| 734 | packet->gtp1l.h.seq = hton16(seq); |
| 735 | if (pdp && (fd == gsn->fd1u)) |
| 736 | packet->gtp1l.h.tei = hton32(pdp->teid_gn); |
| 737 | else if (pdp) |
| 738 | packet->gtp1l.h.tei = hton32(pdp->teic_gn); |
| 739 | } else { |
Harald Welte | d37b80a | 2016-12-15 18:33:15 +0100 | [diff] [blame] | 740 | LOGP(DLGTP, LOGL_ERROR, "Unknown packet flags: 0x%02x\n", packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 741 | return -1; |
| 742 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 743 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 744 | if (fcntl(fd, F_SETFL, 0)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 745 | LOGP(DLGTP, LOGL_ERROR, "fnctl()\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 746 | return -1; |
| 747 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 748 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 749 | if (sendto(fd, packet, len, 0, |
| 750 | (struct sockaddr *)peer, sizeof(struct sockaddr_in)) < 0) { |
| 751 | gsn->err_sendto++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 752 | LOGP(DLGTP, LOGL_ERROR, |
| 753 | "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s\n", fd, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 754 | (unsigned long)&packet, len, strerror(errno)); |
| 755 | return -1; |
| 756 | } |
| 757 | |
| 758 | /* Use new queue structure */ |
| 759 | if (queue_newmsg(gsn->queue_resp, &qmsg, peer, seq)) { |
| 760 | gsn->err_queuefull++; |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 761 | LOGP(DLGTP, LOGL_ERROR, "Retransmit queue is full\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 762 | } else { |
| 763 | memcpy(&qmsg->p, packet, sizeof(union gtp_packet)); |
| 764 | qmsg->l = len; |
| 765 | qmsg->timeout = time(NULL) + 60; /* When to timeout */ |
| 766 | qmsg->retrans = 0; /* No retransmissions so far */ |
| 767 | qmsg->cbp = NULL; |
| 768 | qmsg->type = 0; |
| 769 | qmsg->fd = fd; |
Pau Espin Pedrol | 623c5b3 | 2019-08-16 13:20:09 +0200 | [diff] [blame] | 770 | /* No need to add to pdp list here, because even on pdp ctx free |
| 771 | we want to leave messages in queue_resp until timeout to |
| 772 | detect duplicates */ |
Pau Espin Pedrol | c94837c | 2019-08-28 19:44:20 +0200 | [diff] [blame] | 773 | |
| 774 | /* Rearm timer: Retrans time for qmsg just queued may be required |
| 775 | before an existing one (for instance a gtp echo req) */ |
| 776 | queue_timer_start(gsn); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 777 | } |
| 778 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 781 | static int gtp_notification(struct gsn_t *gsn, uint8_t version, |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 782 | union gtp_packet *packet, int len, |
Pau Espin Pedrol | 2eed6ec | 2021-05-05 17:51:19 +0200 | [diff] [blame] | 783 | const struct sockaddr_in *peer, int fd, uint16_t seq) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 784 | { |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 785 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 786 | uint8_t ver = GTPHDR_F_GET_VER(packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 787 | struct sockaddr_in addr; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 788 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 789 | memcpy(&addr, peer, sizeof(addr)); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 790 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 791 | /* In GTP0 notifications are treated as replies. In GTP1 they |
| 792 | are requests for which there is no reply */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 793 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 794 | if (fd == gsn->fd1c) |
| 795 | addr.sin_port = htons(GTP1C_PORT); |
| 796 | else if (fd == gsn->fd1u) |
| 797 | addr.sin_port = htons(GTP1C_PORT); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 798 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 799 | if (ver == 0) { /* Version 0 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 800 | packet->gtp0.h.length = hton16(len - GTP0_HEADER_SIZE); |
| 801 | packet->gtp0.h.seq = hton16(seq); |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 802 | } else if (ver == 1 && (packet->flags & GTP1HDR_F_SEQ)) { /* Version 1 with seq */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 803 | packet->gtp1l.h.length = hton16(len - GTP1_HEADER_SIZE_SHORT); |
| 804 | packet->gtp1l.h.seq = hton16(seq); |
| 805 | } else { |
Harald Welte | d37b80a | 2016-12-15 18:33:15 +0100 | [diff] [blame] | 806 | LOGP(DLGTP, LOGL_ERROR, "Unknown packet flags: 0x%02x\n", packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 807 | return -1; |
| 808 | } |
| 809 | |
| 810 | if (fcntl(fd, F_SETFL, 0)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 811 | LOGP(DLGTP, LOGL_ERROR, "fnctl()\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 812 | return -1; |
| 813 | } |
| 814 | |
| 815 | if (sendto(fd, packet, len, 0, |
| 816 | (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { |
| 817 | gsn->err_sendto++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 818 | LOGP(DLGTP, LOGL_ERROR, |
| 819 | "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s\n", fd, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 820 | (unsigned long)&packet, len, strerror(errno)); |
| 821 | return -1; |
| 822 | } |
| 823 | return 0; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Pau Espin Pedrol | de72d26 | 2019-05-29 18:17:05 +0200 | [diff] [blame] | 826 | static int gtp_duplicate(struct gsn_t *gsn, uint8_t version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 827 | struct sockaddr_in *peer, uint16_t seq) |
| 828 | { |
| 829 | struct qmsg_t *qmsg; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 830 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 831 | if (queue_seqget(gsn->queue_resp, &qmsg, peer, seq)) { |
| 832 | return EOF; /* Notfound */ |
| 833 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 834 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 835 | if (fcntl(qmsg->fd, F_SETFL, 0)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 836 | LOGP(DLGTP, LOGL_ERROR, "fnctl()\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 837 | return -1; |
| 838 | } |
| 839 | |
| 840 | if (sendto(qmsg->fd, &qmsg->p, qmsg->l, 0, |
| 841 | (struct sockaddr *)peer, sizeof(struct sockaddr_in)) < 0) { |
| 842 | gsn->err_sendto++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 843 | LOGP(DLGTP, LOGL_ERROR, |
| 844 | "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 845 | qmsg->fd, (unsigned long)&qmsg->p, qmsg->l, |
| 846 | strerror(errno)); |
| 847 | } |
| 848 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 849 | } |
| 850 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 851 | /* Perform restoration and recovery error handling as described in 29.060 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 852 | static void log_restart(struct gsn_t *gsn) |
| 853 | { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 854 | FILE *f; |
Emmanuel Bretelle | 111e054 | 2010-09-06 19:49:16 +0200 | [diff] [blame] | 855 | int i, rc; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 856 | int counter = 0; |
Neels Hofmeyr | f7611c3 | 2016-08-18 03:53:09 +0200 | [diff] [blame] | 857 | char *filename; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 858 | |
Neels Hofmeyr | f7611c3 | 2016-08-18 03:53:09 +0200 | [diff] [blame] | 859 | filename = talloc_asprintf(NULL, "%s/%s", gsn->statedir, RESTART_FILE); |
| 860 | OSMO_ASSERT(filename); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 861 | |
| 862 | /* We try to open file. On failure we will later try to create file */ |
| 863 | if (!(f = fopen(filename, "r"))) { |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 864 | LOGP(DLGTP, LOGL_NOTICE, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 865 | "State information file (%s) not found. Creating new file.\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 866 | filename); |
| 867 | } else { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 868 | rc = fscanf(f, "%d", &counter); |
| 869 | if (rc != 1) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 870 | LOGP(DLGTP, LOGL_ERROR, |
| 871 | "fscanf failed to read counter value\n"); |
Holger Hans Peter Freyther | 8ddb680 | 2016-01-23 10:40:52 +0100 | [diff] [blame] | 872 | goto close_file; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 873 | } |
| 874 | if (fclose(f)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 875 | LOGP(DLGTP, LOGL_ERROR, |
| 876 | "fclose failed: Error = %s\n", strerror(errno)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 877 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 878 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 879 | |
| 880 | gsn->restart_counter = (unsigned char)counter; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 881 | gsn->restart_counter++; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 882 | |
Neels Hofmeyr | f41f586 | 2016-09-19 03:35:53 +0200 | [diff] [blame] | 883 | /* Keep the umask closely wrapped around our fopen() call in case the |
| 884 | * log outputs cause file creation. */ |
| 885 | i = umask(022); |
| 886 | f = fopen(filename, "w"); |
| 887 | umask(i); |
| 888 | if (!f) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 889 | LOGP(DLGTP, LOGL_ERROR, |
| 890 | "fopen(path=%s, mode=%s) failed: Error = %s\n", filename, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 891 | "w", strerror(errno)); |
Neels Hofmeyr | f7611c3 | 2016-08-18 03:53:09 +0200 | [diff] [blame] | 892 | goto free_filename; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 893 | } |
| 894 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 895 | fprintf(f, "%d\n", gsn->restart_counter); |
Holger Hans Peter Freyther | 8ddb680 | 2016-01-23 10:40:52 +0100 | [diff] [blame] | 896 | close_file: |
Neels Hofmeyr | f7611c3 | 2016-08-18 03:53:09 +0200 | [diff] [blame] | 897 | if (fclose(f)) |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 898 | LOGP(DLGTP, LOGL_ERROR, |
| 899 | "fclose failed: Error = %s\n", strerror(errno)); |
Neels Hofmeyr | f7611c3 | 2016-08-18 03:53:09 +0200 | [diff] [blame] | 900 | free_filename: |
| 901 | talloc_free(filename); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 902 | } |
| 903 | |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 904 | int gtp_new(struct gsn_t **gsn, char *statedir, struct in_addr *listen, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 905 | int mode) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 906 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 907 | struct sockaddr_in addr; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 908 | |
Max | 14b1b63 | 2017-08-21 20:14:59 +0200 | [diff] [blame] | 909 | LOGP(DLGTP, LOGL_NOTICE, "GTP: gtp_newgsn() started at %s\n", inet_ntoa(*listen)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 910 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 911 | *gsn = calloc(sizeof(struct gsn_t), 1); /* TODO */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 912 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 913 | (*gsn)->statedir = statedir; |
| 914 | log_restart(*gsn); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 915 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 916 | /* Initialise sequence number */ |
| 917 | (*gsn)->seq_next = (*gsn)->restart_counter * 1024; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 918 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 919 | /* Initialise request retransmit queue */ |
| 920 | queue_new(&(*gsn)->queue_req); |
| 921 | queue_new(&(*gsn)->queue_resp); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 922 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 923 | /* Initialise pdp table */ |
Pau Espin Pedrol | eefa30d | 2019-05-31 15:42:49 +0200 | [diff] [blame] | 924 | pdp_init(*gsn); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 925 | |
Pau Espin Pedrol | c94837c | 2019-08-28 19:44:20 +0200 | [diff] [blame] | 926 | /* Initialize internal queue timer */ |
| 927 | osmo_timer_setup(&(*gsn)->queue_timer, queue_timer_cb, *gsn); |
| 928 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 929 | /* Initialise call back functions */ |
| 930 | (*gsn)->cb_create_context_ind = 0; |
| 931 | (*gsn)->cb_delete_context = 0; |
| 932 | (*gsn)->cb_unsup_ind = 0; |
| 933 | (*gsn)->cb_conf = 0; |
| 934 | (*gsn)->cb_data_ind = 0; |
| 935 | |
| 936 | /* Store function parameters */ |
| 937 | (*gsn)->gsnc = *listen; |
| 938 | (*gsn)->gsnu = *listen; |
| 939 | (*gsn)->mode = mode; |
| 940 | |
| 941 | /* Create GTP version 0 socket */ |
| 942 | if (((*gsn)->fd0 = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 943 | (*gsn)->err_socket++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 944 | LOGP(DLGTP, LOGL_ERROR, |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 945 | "GTPv0 socket(domain=%d, type=%d, protocol=%d) failed: Error = %s\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 946 | AF_INET, SOCK_DGRAM, 0, strerror(errno)); |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 947 | return -errno; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | memset(&addr, 0, sizeof(addr)); |
| 951 | addr.sin_family = AF_INET; |
| 952 | addr.sin_addr = *listen; /* Same IP for user traffic and signalling */ |
| 953 | addr.sin_port = htons(GTP0_PORT); |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 954 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 955 | addr.sin_len = sizeof(addr); |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 956 | #endif |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 957 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 958 | if (bind((*gsn)->fd0, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 959 | (*gsn)->err_socket++; |
Neels Hofmeyr | 9b09738 | 2015-10-12 14:00:19 +0200 | [diff] [blame] | 960 | LOGP_WITH_ADDR(DLGTP, LOGL_ERROR, addr, |
| 961 | "bind(fd0=%d) failed: Error = %s\n", |
| 962 | (*gsn)->fd0, strerror(errno)); |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 963 | return -errno; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 964 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 965 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 966 | /* Create GTP version 1 control plane socket */ |
| 967 | if (((*gsn)->fd1c = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 968 | (*gsn)->err_socket++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 969 | LOGP(DLGTP, LOGL_ERROR, |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 970 | "GTPv1 control plane socket(domain=%d, type=%d, protocol=%d) failed: Error = %s\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 971 | AF_INET, SOCK_DGRAM, 0, strerror(errno)); |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 972 | return -errno; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | memset(&addr, 0, sizeof(addr)); |
| 976 | addr.sin_family = AF_INET; |
| 977 | addr.sin_addr = *listen; /* Same IP for user traffic and signalling */ |
| 978 | addr.sin_port = htons(GTP1C_PORT); |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 979 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 980 | addr.sin_len = sizeof(addr); |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 981 | #endif |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 982 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 983 | if (bind((*gsn)->fd1c, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 984 | (*gsn)->err_socket++; |
Neels Hofmeyr | 9b09738 | 2015-10-12 14:00:19 +0200 | [diff] [blame] | 985 | LOGP_WITH_ADDR(DLGTP, LOGL_ERROR, addr, |
| 986 | "bind(fd1c=%d) failed: Error = %s\n", |
| 987 | (*gsn)->fd1c, strerror(errno)); |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 988 | return -errno; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 989 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 990 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 991 | /* Create GTP version 1 user plane socket */ |
| 992 | if (((*gsn)->fd1u = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 993 | (*gsn)->err_socket++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 994 | LOGP(DLGTP, LOGL_ERROR, |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 995 | "GTPv1 user plane socket(domain=%d, type=%d, protocol=%d) failed: Error = %s\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 996 | AF_INET, SOCK_DGRAM, 0, strerror(errno)); |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 997 | return -errno; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | memset(&addr, 0, sizeof(addr)); |
| 1001 | addr.sin_family = AF_INET; |
| 1002 | addr.sin_addr = *listen; /* Same IP for user traffic and signalling */ |
| 1003 | addr.sin_port = htons(GTP1U_PORT); |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 1004 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1005 | addr.sin_len = sizeof(addr); |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 1006 | #endif |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1007 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1008 | if (bind((*gsn)->fd1u, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 1009 | (*gsn)->err_socket++; |
Neels Hofmeyr | 9b09738 | 2015-10-12 14:00:19 +0200 | [diff] [blame] | 1010 | LOGP_WITH_ADDR(DLGTP, LOGL_ERROR, addr, |
Neels Hofmeyr | e845cb9 | 2015-10-12 14:00:22 +0200 | [diff] [blame] | 1011 | "bind(fd1u=%d) failed: Error = %s\n", |
| 1012 | (*gsn)->fd1u, strerror(errno)); |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 1013 | return -errno; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1014 | } |
| 1015 | |
Pau Espin Pedrol | c94837c | 2019-08-28 19:44:20 +0200 | [diff] [blame] | 1016 | /* Start internal queue timer */ |
| 1017 | queue_timer_start(*gsn); |
| 1018 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1019 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1022 | int gtp_free(struct gsn_t *gsn) |
| 1023 | { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1024 | |
Pau Espin Pedrol | c94837c | 2019-08-28 19:44:20 +0200 | [diff] [blame] | 1025 | /* Cleanup internal queue timer */ |
| 1026 | osmo_timer_del(&gsn->queue_timer); |
| 1027 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1028 | /* Clean up retransmit queues */ |
| 1029 | queue_free(gsn->queue_req); |
| 1030 | queue_free(gsn->queue_resp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1031 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1032 | close(gsn->fd0); |
| 1033 | close(gsn->fd1c); |
| 1034 | close(gsn->fd1u); |
| 1035 | |
| 1036 | free(gsn); |
| 1037 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | /* *********************************************************** |
| 1041 | * Path management messages |
| 1042 | * Messages: echo and version not supported. |
| 1043 | * A path is connection between two UDP/IP endpoints |
| 1044 | * |
| 1045 | * A path is either using GTP0 or GTP1. A path can be |
| 1046 | * established by any kind of GTP message?? |
| 1047 | |
| 1048 | * Which source port to use? |
| 1049 | * GTP-C request destination port is 2123/3386 |
| 1050 | * GTP-U request destination port is 2152/3386 |
| 1051 | * T-PDU destination port is 2152/3386. |
| 1052 | * For the above messages the source port is locally allocated. |
| 1053 | * For response messages src=rx-dst and dst=rx-src. |
| 1054 | * For simplicity we should probably use 2123+2152/3386 as |
| 1055 | * src port even for the cases where src can be locally |
| 1056 | * allocated. This also means that we have to listen only to |
| 1057 | * the same ports. |
| 1058 | * For response messages we need to be able to respond to |
| 1059 | * the relevant src port even if it is locally allocated by |
| 1060 | * the peer. |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 1061 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1062 | * The need for path management! |
| 1063 | * We might need to keep a list of active paths. This might |
| 1064 | * be in the form of remote IP address + UDP port numbers. |
| 1065 | * (We will consider a path astablished if we have a context |
| 1066 | * with the node in question) |
| 1067 | *************************************************************/ |
| 1068 | |
| 1069 | /* Send off an echo request */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1070 | int gtp_echo_req(struct gsn_t *gsn, int version, void *cbp, |
| 1071 | struct in_addr *inetaddr) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1072 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1073 | union gtp_packet packet; |
| 1074 | unsigned int length = get_default_gtp(version, GTP_ECHO_REQ, &packet); |
| 1075 | return gtp_req(gsn, version, NULL, &packet, length, inetaddr, cbp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1078 | /* Send off an echo reply */ |
| 1079 | int gtp_echo_resp(struct gsn_t *gsn, int version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1080 | struct sockaddr_in *peer, int fd, void *pack, unsigned len) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1081 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1082 | union gtp_packet packet; |
| 1083 | unsigned int length = get_default_gtp(version, GTP_ECHO_RSP, &packet); |
| 1084 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
| 1085 | gsn->restart_counter); |
| 1086 | return gtp_resp(version, gsn, NULL, &packet, length, peer, fd, |
| 1087 | get_seq(pack), get_tid(pack)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1090 | /* Handle a received echo request */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1091 | int gtp_echo_ind(struct gsn_t *gsn, int version, struct sockaddr_in *peer, |
| 1092 | int fd, void *pack, unsigned len) |
| 1093 | { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1094 | |
Pau Espin Pedrol | de72d26 | 2019-05-29 18:17:05 +0200 | [diff] [blame] | 1095 | /* Check if it was a duplicate request */ |
| 1096 | if (!gtp_duplicate(gsn, 0, peer, get_seq(pack))) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1097 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1098 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1099 | /* Send off reply to request */ |
| 1100 | return gtp_echo_resp(gsn, version, peer, fd, pack, len); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | /* Handle a received echo reply */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1104 | int gtp_echo_conf(struct gsn_t *gsn, int version, struct sockaddr_in *peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1105 | void *pack, unsigned len) |
| 1106 | { |
| 1107 | union gtpie_member *ie[GTPIE_SIZE]; |
| 1108 | unsigned char recovery; |
| 1109 | void *cbp = NULL; |
| 1110 | uint8_t type = 0; |
| 1111 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1112 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1113 | /* Remove packet from queue */ |
| 1114 | if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) |
| 1115 | return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1116 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1117 | /* Extract information elements into a pointer array */ |
| 1118 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 1119 | gsn->invalid++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1120 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1121 | "Invalid message format\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1122 | if (gsn->cb_conf) |
| 1123 | gsn->cb_conf(type, EOF, NULL, cbp); |
| 1124 | return EOF; |
| 1125 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1126 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1127 | if (gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) { |
| 1128 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1129 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1130 | "Missing mandatory field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1131 | if (gsn->cb_conf) |
| 1132 | gsn->cb_conf(type, EOF, NULL, cbp); |
| 1133 | return EOF; |
| 1134 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1135 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1136 | /* Echo reply packages does not have a cause information element */ |
| 1137 | /* Instead we return the recovery number in the callback function */ |
| 1138 | if (gsn->cb_conf) |
| 1139 | gsn->cb_conf(type, recovery, NULL, cbp); |
Harald Welte | 629e986 | 2010-12-24 20:58:09 +0100 | [diff] [blame] | 1140 | |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 1141 | emit_cb_recovery(gsn, peer, NULL, recovery); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1142 | |
| 1143 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | /* Send off a Version Not Supported message */ |
| 1147 | /* This message is somewhat special in that it actually is a |
| 1148 | * response to some other message with unsupported GTP version |
| 1149 | * For this reason it has parameters like a response, and does |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 1150 | * its own message transmission. No signalling queue is used |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1151 | * The reply is sent to the peer IP and peer UDP. This means that |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 1152 | * the peer will be receiving a GTP0 message on a GTP1 port! |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1153 | * In practice however this will never happen as a GTP0 GSN will |
| 1154 | * only listen to the GTP0 port, and therefore will never receive |
| 1155 | * anything else than GTP0 */ |
| 1156 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1157 | int gtp_unsup_req(struct gsn_t *gsn, int version, struct sockaddr_in *peer, |
| 1158 | int fd, void *pack, unsigned len) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1159 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1160 | union gtp_packet packet; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1161 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1162 | /* GTP 1 is the highest supported protocol */ |
| 1163 | unsigned int length = get_default_gtp(1, GTP_NOT_SUPPORTED, &packet); |
| 1164 | return gtp_notification(gsn, version, &packet, length, peer, fd, 0); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | /* Handle a Version Not Supported message */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1168 | int gtp_unsup_ind(struct gsn_t *gsn, struct sockaddr_in *peer, |
| 1169 | void *pack, unsigned len) |
| 1170 | { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1171 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1172 | if (gsn->cb_unsup_ind) |
| 1173 | gsn->cb_unsup_ind(peer); |
| 1174 | |
| 1175 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1178 | /* Send off an Supported Extension Headers Notification */ |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 1179 | static int gtp_extheader_req(struct gsn_t *gsn, uint8_t version, struct sockaddr_in *peer, |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1180 | int fd, void *pack, unsigned len) |
| 1181 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1182 | union gtp_packet packet; |
| 1183 | unsigned int length = |
| 1184 | get_default_gtp(version, GTP_SUPP_EXT_HEADER, &packet); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1185 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1186 | uint8_t pdcp_pdu = GTP_EXT_PDCP_PDU; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1187 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1188 | if (version < 1) |
| 1189 | return 0; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1190 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1191 | /* We report back that we support only PDCP PDU headers */ |
| 1192 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EXT_HEADER_T, |
| 1193 | sizeof(pdcp_pdu), &pdcp_pdu); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1194 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1195 | return gtp_notification(gsn, version, &packet, length, |
| 1196 | peer, fd, get_seq(pack)); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /* Handle a Supported Extension Headers Notification */ |
Pau Espin Pedrol | 7b38af5 | 2018-01-25 18:35:33 +0100 | [diff] [blame] | 1200 | static int gtp_extheader_ind(struct gsn_t *gsn, struct sockaddr_in *peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1201 | void *pack, unsigned len) |
| 1202 | { |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1203 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1204 | if (gsn->cb_extheader_ind) |
| 1205 | gsn->cb_extheader_ind(peer); |
| 1206 | |
| 1207 | return 0; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Pau Espin Pedrol | f32c6a9 | 2021-05-03 17:58:55 +0200 | [diff] [blame] | 1210 | /* Handle a RAN Information Relay message */ |
| 1211 | static int gtp_ran_info_relay_ind(struct gsn_t *gsn, int version, struct sockaddr_in *peer, |
| 1212 | void *pack, unsigned len) |
| 1213 | { |
| 1214 | union gtpie_member *ie[GTPIE_SIZE]; |
| 1215 | |
| 1216 | if (version != 1) { |
| 1217 | LOGP(DLGTP, LOGL_NOTICE, |
| 1218 | "RAN Information Relay expected only on GTPCv1: %u\n", version); |
| 1219 | return -EINVAL; |
| 1220 | } |
| 1221 | |
| 1222 | int hlen = get_hlen(pack); |
| 1223 | |
| 1224 | /* Decode information elements */ |
| 1225 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 1226 | gsn->invalid++; |
| 1227 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1228 | "Invalid message format (AN Information Relay)\n"); |
| 1229 | return -EINVAL; |
| 1230 | } |
| 1231 | |
| 1232 | if (gsn->cb_ran_info_relay_ind) |
| 1233 | gsn->cb_ran_info_relay_ind(peer, ie); |
| 1234 | |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | /* Send off a RAN Information Relay message */ |
| 1239 | int gtp_ran_info_relay_req(struct gsn_t *gsn, const struct sockaddr_in *peer, |
| 1240 | const uint8_t *ran_container, size_t ran_container_len, |
| 1241 | const uint8_t *rim_route_addr, size_t rim_route_addr_len, |
| 1242 | uint8_t rim_route_addr_discr) |
| 1243 | { |
| 1244 | union gtp_packet packet; |
| 1245 | |
| 1246 | /* GTP 1 is the highest supported protocol */ |
| 1247 | unsigned int length = get_default_gtp(1, GTP_RAN_INFO_RELAY, &packet); |
| 1248 | |
| 1249 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_RAN_T_CONTAIN, ran_container_len, |
| 1250 | ran_container); |
| 1251 | if (rim_route_addr) { |
| 1252 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_RIM_ROUT_ADDR, |
| 1253 | rim_route_addr_len, rim_route_addr); |
| 1254 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_RIM_RA_DISCR, 1, |
| 1255 | &rim_route_addr_discr); |
| 1256 | } |
| 1257 | |
| 1258 | return gtp_notification(gsn, 1, &packet, length, peer, gsn->fd1c, 0); |
| 1259 | } |
| 1260 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1261 | /* *********************************************************** |
| 1262 | * Session management messages |
| 1263 | * Messages: create, update and delete PDP context |
| 1264 | * |
| 1265 | * Information storage |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 1266 | * Information storage for each PDP context is defined in |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1267 | * 23.060 section 13.3. Includes IMSI, MSISDN, APN, PDP-type, |
| 1268 | * PDP-address (IP address), sequence numbers, charging ID. |
| 1269 | * For the SGSN it also includes radio related mobility |
| 1270 | * information. |
| 1271 | *************************************************************/ |
| 1272 | |
Harald Welte | 7b3347b | 2010-05-15 12:18:46 +0200 | [diff] [blame] | 1273 | /* API: Send Create PDP Context Request (7.3.1) */ |
Pau Espin Pedrol | 7b38af5 | 2018-01-25 18:35:33 +0100 | [diff] [blame] | 1274 | int gtp_create_context_req(struct gsn_t *gsn, struct pdp_t *pdp, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1275 | void *cbp) |
| 1276 | { |
| 1277 | union gtp_packet packet; |
| 1278 | unsigned int length = |
| 1279 | get_default_gtp(pdp->version, GTP_CREATE_PDP_REQ, &packet); |
| 1280 | struct pdp_t *linked_pdp = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1281 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1282 | /* TODO: Secondary PDP Context Activation Procedure */ |
| 1283 | /* In secondary activation procedure the PDP context is identified |
| 1284 | by tei in the header. The following fields are omitted: Selection |
| 1285 | mode, IMSI, MSISDN, End User Address, Access Point Name and |
| 1286 | Protocol Configuration Options */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1287 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1288 | if (pdp->secondary) { |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 1289 | if (gtp_pdp_getgtp1(gsn, &linked_pdp, pdp->teic_own)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1290 | LOGP(DLGTP, LOGL_ERROR, |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 1291 | "Unknown linked PDP context: %u\n", pdp->teic_own); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1292 | return EOF; |
| 1293 | } |
| 1294 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1295 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1296 | if (pdp->version == 0) { |
| 1297 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0, |
| 1298 | sizeof(pdp->qos_req0), pdp->qos_req0); |
| 1299 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1300 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1301 | /* Section 7.7.2 */ |
| 1302 | if (pdp->version == 1) { |
| 1303 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1304 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_IMSI, |
| 1305 | sizeof(pdp->imsi), (uint8_t *) & pdp->imsi); |
| 1306 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1307 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1308 | /* Section 7.7.3 Routing Area Information */ |
| 1309 | if (pdp->rai_given == 1) |
| 1310 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_RAI, |
| 1311 | pdp->rai.l, (uint8_t *) & pdp->rai.v); |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 1312 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1313 | /* Section 7.7.11 */ |
| 1314 | if (pdp->norecovery_given == 0) |
| 1315 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
| 1316 | gsn->restart_counter); |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 1317 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1318 | /* Section 7.7.12 */ |
| 1319 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1320 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_SELECTION_MODE, |
| 1321 | pdp->selmode); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1322 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1323 | if (pdp->version == 0) { |
| 1324 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI, pdp->fllu); |
| 1325 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C, pdp->fllc); |
| 1326 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1327 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1328 | /* Section 7.7.13 */ |
| 1329 | if (pdp->version == 1) { |
| 1330 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI, |
| 1331 | pdp->teid_own); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1332 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1333 | /* Section 7.7.14 */ |
| 1334 | if (!pdp->teic_confirmed) |
| 1335 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C, |
| 1336 | pdp->teic_own); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1337 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1338 | /* Section 7.7.17 */ |
| 1339 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI, pdp->nsapi); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1340 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1341 | /* Section 7.7.17 */ |
| 1342 | if (pdp->secondary) /* Secondary PDP Context Activation Procedure */ |
| 1343 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI, |
| 1344 | linked_pdp->nsapi); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1345 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1346 | /* Section 7.7.23 */ |
| 1347 | if (pdp->cch_pdp) /* Only include charging if flags are set */ |
| 1348 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_CHARGING_C, |
| 1349 | pdp->cch_pdp); |
| 1350 | } |
jjako | 9b4971d | 2004-05-27 20:30:19 +0000 | [diff] [blame] | 1351 | |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 1352 | /* TODO |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1353 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_REF, |
| 1354 | pdp->traceref); |
| 1355 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_TYPE, |
| 1356 | pdp->tracetype); */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1357 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1358 | /* Section 7.7.27 */ |
| 1359 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1360 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA, |
| 1361 | pdp->eua.l, pdp->eua.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1362 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1363 | /* Section 7.7.30 */ |
| 1364 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1365 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_APN, |
| 1366 | pdp->apn_use.l, pdp->apn_use.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1367 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1368 | /* Section 7.7.31 */ |
| 1369 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1370 | if (pdp->pco_req.l) |
| 1371 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_PCO, |
| 1372 | pdp->pco_req.l, pdp->pco_req.v); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1373 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1374 | /* Section 7.7.32 */ |
| 1375 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1376 | pdp->gsnlc.l, pdp->gsnlc.v); |
| 1377 | /* Section 7.7.32 */ |
| 1378 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1379 | pdp->gsnlu.l, pdp->gsnlu.v); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1380 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1381 | /* Section 7.7.33 */ |
| 1382 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1383 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_MSISDN, |
| 1384 | pdp->msisdn.l, pdp->msisdn.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1385 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1386 | /* Section 7.7.34 */ |
| 1387 | if (pdp->version == 1) |
| 1388 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE, |
| 1389 | pdp->qos_req.l, pdp->qos_req.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1390 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1391 | /* Section 7.7.36 */ |
| 1392 | if ((pdp->version == 1) && pdp->tft.l) |
| 1393 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TFT, |
| 1394 | pdp->tft.l, pdp->tft.v); |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 1395 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1396 | /* Section 7.7.41 */ |
| 1397 | if ((pdp->version == 1) && pdp->triggerid.l) |
| 1398 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TRIGGER_ID, |
| 1399 | pdp->triggerid.l, pdp->triggerid.v); |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 1400 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1401 | /* Section 7.7.42 */ |
| 1402 | if ((pdp->version == 1) && pdp->omcid.l) |
| 1403 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_OMC_ID, |
| 1404 | pdp->omcid.l, pdp->omcid.v); |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 1405 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1406 | /* new R7 fields */ |
| 1407 | if (pdp->rattype_given == 1) |
| 1408 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_RAT_TYPE, |
| 1409 | pdp->rattype.l, pdp->rattype.v); |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 1410 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1411 | if (pdp->userloc_given == 1) |
| 1412 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_USER_LOC, |
| 1413 | pdp->userloc.l, pdp->userloc.v); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1414 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1415 | if (pdp->mstz_given == 1) |
| 1416 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_MS_TZ, |
| 1417 | pdp->mstz.l, pdp->mstz.v); |
| 1418 | |
| 1419 | if (pdp->imeisv_given == 1) |
| 1420 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_IMEI_SV, |
| 1421 | pdp->imeisv.l, pdp->imeisv.v); |
| 1422 | |
| 1423 | /* TODO hisaddr0 */ |
| 1424 | gtp_req(gsn, pdp->version, pdp, &packet, length, &pdp->hisaddr0, cbp); |
| 1425 | |
| 1426 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1429 | /* API: Application response to context indication */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1430 | int gtp_create_context_resp(struct gsn_t *gsn, struct pdp_t *pdp, int cause) |
| 1431 | { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1432 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1433 | /* Now send off a reply to the peer */ |
| 1434 | gtp_create_pdp_resp(gsn, pdp->version, pdp, cause); |
| 1435 | |
Pau Espin Pedrol | d950134 | 2019-08-21 15:24:29 +0200 | [diff] [blame] | 1436 | if (cause != GTPCAUSE_ACC_REQ) |
| 1437 | gtp_freepdp(gsn, pdp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1438 | |
| 1439 | return 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | /* API: Register create context indication callback */ |
| 1443 | int gtp_set_cb_create_context_ind(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1444 | int (*cb_create_context_ind) (struct pdp_t * |
| 1445 | pdp)) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1446 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1447 | gsn->cb_create_context_ind = cb_create_context_ind; |
| 1448 | return 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1451 | /* Send Create PDP Context Response */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1452 | int gtp_create_pdp_resp(struct gsn_t *gsn, int version, struct pdp_t *pdp, |
| 1453 | uint8_t cause) |
| 1454 | { |
| 1455 | union gtp_packet packet; |
| 1456 | unsigned int length = |
| 1457 | get_default_gtp(version, GTP_CREATE_PDP_RSP, &packet); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1458 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1459 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1460 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1461 | if (cause == GTPCAUSE_ACC_REQ) { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1462 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1463 | if (version == 0) |
| 1464 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0, |
| 1465 | sizeof(pdp->qos_neg0), pdp->qos_neg0); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1466 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1467 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_REORDER, |
| 1468 | pdp->reorder); |
| 1469 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
| 1470 | gsn->restart_counter); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1471 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1472 | if (version == 0) { |
| 1473 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI, |
| 1474 | pdp->fllu); |
| 1475 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C, |
| 1476 | pdp->fllc); |
| 1477 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1478 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1479 | if (version == 1) { |
| 1480 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI, |
| 1481 | pdp->teid_own); |
| 1482 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C, |
| 1483 | pdp->teic_own); |
| 1484 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1485 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1486 | /* TODO: We use teic_own as charging ID */ |
| 1487 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_CHARGING_ID, |
| 1488 | pdp->teic_own); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1489 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1490 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA, |
| 1491 | pdp->eua.l, pdp->eua.v); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1492 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1493 | if (pdp->pco_neg.l) { /* Optional PCO */ |
| 1494 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_PCO, |
| 1495 | pdp->pco_neg.l, pdp->pco_neg.v); |
| 1496 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1497 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1498 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1499 | pdp->gsnlc.l, pdp->gsnlc.v); |
| 1500 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1501 | pdp->gsnlu.l, pdp->gsnlu.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1502 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1503 | if (version == 1) |
| 1504 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE, |
| 1505 | pdp->qos_neg.l, pdp->qos_neg.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1506 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1507 | /* TODO: Charging gateway address */ |
| 1508 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1509 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1510 | return gtp_resp(version, gsn, pdp, &packet, length, &pdp->sa_peer, |
| 1511 | pdp->fd, pdp->seq, pdp->tid); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | /* Handle Create PDP Context Request */ |
| 1515 | int gtp_create_pdp_ind(struct gsn_t *gsn, int version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1516 | struct sockaddr_in *peer, int fd, |
| 1517 | void *pack, unsigned len) |
| 1518 | { |
| 1519 | struct pdp_t *pdp, *pdp_old; |
| 1520 | struct pdp_t pdp_buf; |
| 1521 | union gtpie_member *ie[GTPIE_SIZE]; |
| 1522 | uint8_t recovery; |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 1523 | bool recovery_recvd = false; |
| 1524 | int rc; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1525 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1526 | uint16_t seq = get_seq(pack); |
| 1527 | int hlen = get_hlen(pack); |
| 1528 | uint8_t linked_nsapi = 0; |
| 1529 | struct pdp_t *linked_pdp = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1530 | |
Pau Espin Pedrol | de72d26 | 2019-05-29 18:17:05 +0200 | [diff] [blame] | 1531 | if (!gtp_duplicate(gsn, version, peer, seq)) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1532 | return 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1533 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1534 | pdp = &pdp_buf; |
| 1535 | memset(pdp, 0, sizeof(struct pdp_t)); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1536 | |
Vadim Yanitskiy | 68c5a74 | 2019-08-30 21:04:19 +0200 | [diff] [blame] | 1537 | if (version == 0) |
| 1538 | pdp_set_imsi_nsapi(pdp, get_tid(pack)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1539 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1540 | pdp->seq = seq; |
| 1541 | pdp->sa_peer = *peer; |
| 1542 | pdp->fd = fd; |
| 1543 | pdp->version = version; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1544 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1545 | /* Decode information elements */ |
| 1546 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 1547 | gsn->invalid++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1548 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1549 | "Invalid message format\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1550 | if (0 == version) |
| 1551 | return EOF; |
| 1552 | else |
| 1553 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1554 | GTPCAUSE_INVALID_MESSAGE); |
| 1555 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1556 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1557 | if (version == 1) { |
| 1558 | /* Linked NSAPI (conditional) */ |
| 1559 | /* If included this is the Secondary PDP Context Activation Procedure */ |
| 1560 | /* In secondary activation IMSI is not included, so the context must be */ |
| 1561 | /* identified by the tei */ |
| 1562 | if (!gtpie_gettv1(ie, GTPIE_NSAPI, 1, &linked_nsapi)) { |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1563 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1564 | /* Find the primary PDP context */ |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 1565 | if (gtp_pdp_getgtp1(gsn, &linked_pdp, get_tei(pack))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1566 | gsn->incorrect++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1567 | GTP_LOGPKG(LOGL_ERROR, peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1568 | pack, len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1569 | "Incorrect optional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1570 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1571 | GTPCAUSE_OPT_IE_INCORRECT); |
| 1572 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1573 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1574 | /* Check that the primary PDP context matches linked nsapi */ |
| 1575 | if (linked_pdp->nsapi != linked_nsapi) { |
| 1576 | gsn->incorrect++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1577 | GTP_LOGPKG(LOGL_ERROR, peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1578 | pack, len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1579 | "Incorrect optional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1580 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1581 | GTPCAUSE_OPT_IE_INCORRECT); |
| 1582 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1583 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1584 | /* Copy parameters from primary context */ |
| 1585 | pdp->selmode = linked_pdp->selmode; |
| 1586 | pdp->imsi = linked_pdp->imsi; |
| 1587 | pdp->msisdn = linked_pdp->msisdn; |
| 1588 | pdp->eua = linked_pdp->eua; |
| 1589 | pdp->pco_req = linked_pdp->pco_req; |
| 1590 | pdp->apn_req = linked_pdp->apn_req; |
| 1591 | pdp->teic_gn = linked_pdp->teic_gn; |
| 1592 | pdp->secondary = 1; |
Pau Espin Pedrol | aad77a0 | 2019-05-30 12:44:20 +0200 | [diff] [blame] | 1593 | } else { |
| 1594 | /* Not Secondary PDP Context Activation Procedure */ |
| 1595 | /* IMSI (conditional): If the MS is emergency attached |
| 1596 | and the MS is UICCless, the IMSI cannot be included |
| 1597 | in the message and therefore IMSI shall not be |
| 1598 | included in the message. */ |
| 1599 | if (gtpie_gettv0 |
| 1600 | (ie, GTPIE_IMSI, 0, &pdp->imsi, sizeof(pdp->imsi))) { |
| 1601 | gsn->missing++; |
| 1602 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1603 | len, "Missing IMSI not supported\n"); |
| 1604 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1605 | GTPCAUSE_MAN_IE_MISSING); |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | /* TEID (mandatory) */ |
| 1610 | if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) { |
| 1611 | gsn->missing++; |
| 1612 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1613 | len, "Missing mandatory information field\n"); |
| 1614 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1615 | GTPCAUSE_MAN_IE_MISSING); |
| 1616 | } |
| 1617 | /* TEIC (conditional) */ |
| 1618 | if (!linked_pdp) { /* Not Secondary PDP Context Activation Procedure */ |
| 1619 | if (gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn)) { |
| 1620 | gsn->missing++; |
| 1621 | GTP_LOGPKG(LOGL_ERROR, peer, |
| 1622 | pack, len, |
| 1623 | "Missing mandatory information field\n"); |
| 1624 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1625 | GTPCAUSE_MAN_IE_MISSING); |
| 1626 | } |
| 1627 | } |
| 1628 | /* NSAPI (mandatory) */ |
| 1629 | if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &pdp->nsapi)) { |
| 1630 | gsn->missing++; |
| 1631 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1632 | len, "Missing mandatory information field\n"); |
| 1633 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1634 | GTPCAUSE_MAN_IE_MISSING); |
| 1635 | } |
| 1636 | /* QoS (mandatory) */ |
| 1637 | if (gtpie_gettlv(ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_req.l, |
| 1638 | &pdp->qos_req.v, sizeof(pdp->qos_req.v))) { |
| 1639 | gsn->missing++; |
| 1640 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1641 | len, "Missing mandatory information field\n"); |
| 1642 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1643 | GTPCAUSE_MAN_IE_MISSING); |
| 1644 | } |
| 1645 | /* TFT (conditional) */ |
| 1646 | if (gtpie_gettlv(ie, GTPIE_TFT, 0, &pdp->tft.l, |
| 1647 | &pdp->tft.v, sizeof(pdp->tft.v))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1648 | } |
| 1649 | } |
| 1650 | /* if (version == 1) */ |
| 1651 | if (version == 0) { |
| 1652 | if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0, |
| 1653 | pdp->qos_req0, sizeof(pdp->qos_req0))) { |
| 1654 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1655 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1656 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1657 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1658 | GTPCAUSE_MAN_IE_MISSING); |
| 1659 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1660 | if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) { |
| 1661 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1662 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1663 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1664 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1665 | GTPCAUSE_MAN_IE_MISSING); |
| 1666 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1667 | if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) { |
| 1668 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1669 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1670 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1671 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1672 | GTPCAUSE_MAN_IE_MISSING); |
| 1673 | } |
| 1674 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1675 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1676 | /* SGSN address for signalling (mandatory) */ |
| 1677 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l, |
| 1678 | &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) { |
| 1679 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1680 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1681 | "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1682 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1683 | GTPCAUSE_MAN_IE_MISSING); |
| 1684 | } |
jjako | 2e840a3 | 2003-01-28 16:05:18 +0000 | [diff] [blame] | 1685 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1686 | /* SGSN address for user traffic (mandatory) */ |
| 1687 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l, |
| 1688 | &pdp->gsnru.v, sizeof(pdp->gsnru.v))) { |
| 1689 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1690 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1691 | "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1692 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1693 | GTPCAUSE_MAN_IE_MISSING); |
| 1694 | } |
Pau Espin Pedrol | aad77a0 | 2019-05-30 12:44:20 +0200 | [diff] [blame] | 1695 | /* Recovery (optional) */ |
| 1696 | if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) { |
| 1697 | /* we use recovery futher down after announcing new pdp ctx to user */ |
| 1698 | recovery_recvd = true; |
| 1699 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1700 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1701 | if (!linked_pdp) { /* Not Secondary PDP Context Activation Procedure */ |
Pau Espin Pedrol | aad77a0 | 2019-05-30 12:44:20 +0200 | [diff] [blame] | 1702 | /* Selection mode (conditional) */ |
| 1703 | if (gtpie_gettv0(ie, GTPIE_SELECTION_MODE, 0, |
| 1704 | &pdp->selmode, sizeof(pdp->selmode))) { |
| 1705 | gsn->missing++; |
| 1706 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1707 | len, "Missing mandatory information field\n"); |
| 1708 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1709 | GTPCAUSE_MAN_IE_MISSING); |
| 1710 | } |
| 1711 | /* End User Address (conditional) */ |
| 1712 | if (gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l, |
| 1713 | &pdp->eua.v, sizeof(pdp->eua.v))) { |
| 1714 | gsn->missing++; |
| 1715 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1716 | len, "Missing mandatory information field\n"); |
| 1717 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1718 | GTPCAUSE_MAN_IE_MISSING); |
| 1719 | } |
| 1720 | /* APN */ |
| 1721 | if (gtpie_gettlv(ie, GTPIE_APN, 0, &pdp->apn_req.l, |
| 1722 | &pdp->apn_req.v, sizeof(pdp->apn_req.v))) { |
| 1723 | gsn->missing++; |
| 1724 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1725 | len, "Missing mandatory information field\n"); |
| 1726 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1727 | GTPCAUSE_MAN_IE_MISSING); |
| 1728 | } |
| 1729 | /* Extract protocol configuration options (optional) */ |
| 1730 | if (!gtpie_gettlv(ie, GTPIE_PCO, 0, &pdp->pco_req.l, |
| 1731 | &pdp->pco_req.v, sizeof(pdp->pco_req.v))) { |
| 1732 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1733 | /* MSISDN (conditional) */ |
| 1734 | if (gtpie_gettlv(ie, GTPIE_MSISDN, 0, &pdp->msisdn.l, |
| 1735 | &pdp->msisdn.v, sizeof(pdp->msisdn.v))) { |
| 1736 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1737 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1738 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1739 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1740 | GTPCAUSE_MAN_IE_MISSING); |
| 1741 | } |
| 1742 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1743 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1744 | /* Initialize our own IP addresses */ |
| 1745 | in_addr2gsna(&pdp->gsnlc, &gsn->gsnc); |
| 1746 | in_addr2gsna(&pdp->gsnlu, &gsn->gsnu); |
| 1747 | |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 1748 | DEBUGP(DLGTP, "gtp_create_pdp_ind: Before gtp_pdp_tidget\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1749 | |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 1750 | if (!gtp_pdp_getimsi(gsn, &pdp_old, pdp->imsi, pdp->nsapi)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1751 | /* Found old pdp with same tid. Now the voodoo begins! */ |
| 1752 | /* 09.60 / 29.060 allows create on existing context to "steal" */ |
| 1753 | /* the context which was allready established */ |
| 1754 | /* We check that the APN, selection mode and MSISDN is the same */ |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 1755 | DEBUGP(DLGTP, "gtp_create_pdp_ind: Old context found\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1756 | if ((pdp->apn_req.l == pdp_old->apn_req.l) |
| 1757 | && |
| 1758 | (!memcmp |
| 1759 | (pdp->apn_req.v, pdp_old->apn_req.v, pdp->apn_req.l)) |
| 1760 | && (pdp->selmode == pdp_old->selmode) |
| 1761 | && (pdp->msisdn.l == pdp_old->msisdn.l) |
| 1762 | && |
| 1763 | (!memcmp(pdp->msisdn.v, pdp_old->msisdn.v, pdp->msisdn.l))) |
| 1764 | { |
| 1765 | /* OK! We are dealing with the same APN. We will copy new |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 1766 | * parameters to the old pdp and send off confirmation |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1767 | * We ignore the following information elements: |
| 1768 | * QoS: MS will get originally negotiated QoS. |
| 1769 | * End user address (EUA). MS will get old EUA anyway. |
| 1770 | * Protocol configuration option (PCO): Only application can verify */ |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 1771 | DEBUGP(DLGTP, "gtp_create_pdp_ind: Old context found\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1772 | |
| 1773 | /* Copy remote flow label */ |
| 1774 | pdp_old->flru = pdp->flru; |
| 1775 | pdp_old->flrc = pdp->flrc; |
| 1776 | |
| 1777 | /* Copy remote tei */ |
| 1778 | pdp_old->teid_gn = pdp->teid_gn; |
| 1779 | pdp_old->teic_gn = pdp->teic_gn; |
| 1780 | |
| 1781 | /* Copy peer GSN address */ |
| 1782 | pdp_old->gsnrc.l = pdp->gsnrc.l; |
| 1783 | memcpy(&pdp_old->gsnrc.v, &pdp->gsnrc.v, pdp->gsnrc.l); |
| 1784 | pdp_old->gsnru.l = pdp->gsnru.l; |
| 1785 | memcpy(&pdp_old->gsnru.v, &pdp->gsnru.v, pdp->gsnru.l); |
| 1786 | |
| 1787 | /* Copy request parameters */ |
| 1788 | pdp_old->seq = pdp->seq; |
| 1789 | pdp_old->sa_peer = pdp->sa_peer; |
| 1790 | pdp_old->fd = pdp->fd = fd; |
| 1791 | pdp_old->version = pdp->version = version; |
| 1792 | |
| 1793 | /* Switch to using the old pdp context */ |
| 1794 | pdp = pdp_old; |
| 1795 | |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 1796 | if (recovery_recvd) |
| 1797 | emit_cb_recovery(gsn, peer, pdp, recovery); |
| 1798 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1799 | /* Confirm to peer that things were "successful" */ |
| 1800 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1801 | GTPCAUSE_ACC_REQ); |
| 1802 | } else { /* This is not the same PDP context. Delete the old one. */ |
| 1803 | |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 1804 | DEBUGP(DLGTP, "gtp_create_pdp_ind: Deleting old context\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1805 | |
Pau Espin Pedrol | 0d0b059 | 2019-05-29 20:42:09 +0200 | [diff] [blame] | 1806 | gtp_freepdp(gsn, pdp_old); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1807 | |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 1808 | DEBUGP(DLGTP, "gtp_create_pdp_ind: Deleted...\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1809 | } |
| 1810 | } |
| 1811 | |
Pau Espin Pedrol | eefa30d | 2019-05-31 15:42:49 +0200 | [diff] [blame] | 1812 | gtp_pdp_newpdp(gsn, &pdp, pdp->imsi, pdp->nsapi, pdp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1813 | |
Pau Espin Pedrol | aad77a0 | 2019-05-30 12:44:20 +0200 | [diff] [blame] | 1814 | /* Callback function to validate login */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1815 | if (gsn->cb_create_context_ind != 0) |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 1816 | rc = gsn->cb_create_context_ind(pdp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1817 | else { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1818 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1819 | "No create_context_ind callback defined\n"); |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 1820 | rc = gtp_create_pdp_resp(gsn, version, pdp, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1821 | GTPCAUSE_NOT_SUPPORTED); |
| 1822 | } |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 1823 | if (recovery_recvd) |
| 1824 | emit_cb_recovery(gsn, peer, pdp, recovery); |
| 1825 | return rc; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1828 | /* Handle Create PDP Context Response */ |
| 1829 | int gtp_create_pdp_conf(struct gsn_t *gsn, int version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1830 | struct sockaddr_in *peer, void *pack, unsigned len) |
| 1831 | { |
| 1832 | struct pdp_t *pdp; |
| 1833 | union gtpie_member *ie[GTPIE_SIZE]; |
| 1834 | uint8_t cause, recovery; |
| 1835 | void *cbp = NULL; |
| 1836 | uint8_t type = 0; |
| 1837 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1838 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1839 | /* Remove packet from queue */ |
| 1840 | if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) |
| 1841 | return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1842 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1843 | /* Find the context in question */ |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 1844 | if (gtp_pdp_getgtp1(gsn, &pdp, get_tei(pack))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1845 | gsn->err_unknownpdp++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1846 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 1847 | "Unknown PDP context: %u\n", get_tei(pack)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1848 | if (gsn->cb_conf) |
| 1849 | gsn->cb_conf(type, EOF, NULL, cbp); |
| 1850 | return EOF; |
| 1851 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1852 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1853 | /* Decode information elements */ |
| 1854 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 1855 | gsn->invalid++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1856 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1857 | "Invalid message format\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1858 | if (gsn->cb_conf) |
| 1859 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1860 | return EOF; |
| 1861 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1862 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1863 | /* Extract cause value (mandatory) */ |
| 1864 | if (gtpie_gettv1(ie, GTPIE_CAUSE, 0, &cause)) { |
| 1865 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1866 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1867 | "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1868 | if (gsn->cb_conf) |
| 1869 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1870 | return EOF; |
| 1871 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1872 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1873 | /* Extract recovery (optional) */ |
| 1874 | if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) { |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 1875 | emit_cb_recovery(gsn, peer, pdp, recovery); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1876 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1877 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1878 | /* Extract protocol configuration options (optional) */ |
| 1879 | if (!gtpie_gettlv(ie, GTPIE_PCO, 0, &pdp->pco_req.l, |
| 1880 | &pdp->pco_req.v, sizeof(pdp->pco_req.v))) { |
| 1881 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1882 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1883 | /* Check all conditional information elements */ |
| 1884 | if (GTPCAUSE_ACC_REQ == cause) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1885 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1886 | if (version == 0) { |
| 1887 | if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0, |
| 1888 | &pdp->qos_neg0, |
| 1889 | sizeof(pdp->qos_neg0))) { |
| 1890 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1891 | GTP_LOGPKG(LOGL_ERROR, peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1892 | pack, len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1893 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1894 | if (gsn->cb_conf) |
| 1895 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1896 | return EOF; |
| 1897 | } |
| 1898 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1899 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1900 | if (gtpie_gettv1(ie, GTPIE_REORDER, 0, &pdp->reorder)) { |
| 1901 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1902 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1903 | len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1904 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1905 | if (gsn->cb_conf) |
| 1906 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1907 | return EOF; |
| 1908 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1909 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1910 | if (version == 0) { |
| 1911 | if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) { |
| 1912 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1913 | GTP_LOGPKG(LOGL_ERROR, peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1914 | pack, len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1915 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1916 | if (gsn->cb_conf) |
| 1917 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1918 | return EOF; |
| 1919 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1920 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1921 | if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) { |
| 1922 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1923 | GTP_LOGPKG(LOGL_ERROR, peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1924 | pack, len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1925 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1926 | if (gsn->cb_conf) |
| 1927 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1928 | return EOF; |
| 1929 | } |
| 1930 | } |
| 1931 | |
| 1932 | if (version == 1) { |
| 1933 | if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) { |
| 1934 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1935 | GTP_LOGPKG(LOGL_ERROR, peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1936 | pack, len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1937 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1938 | if (gsn->cb_conf) |
| 1939 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1940 | return EOF; |
| 1941 | } |
| 1942 | |
| 1943 | if (gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn)) { |
| 1944 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1945 | GTP_LOGPKG(LOGL_ERROR, peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1946 | pack, len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1947 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1948 | if (gsn->cb_conf) |
| 1949 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1950 | return EOF; |
| 1951 | } |
Pau Espin Pedrol | 0b1d9db | 2021-04-21 19:45:23 +0200 | [diff] [blame] | 1952 | /* Register that we have received a valid teic from GGSN */ |
| 1953 | pdp->teic_confirmed = 1; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | if (gtpie_gettv4(ie, GTPIE_CHARGING_ID, 0, &pdp->cid)) { |
| 1957 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1958 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1959 | len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1960 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1961 | if (gsn->cb_conf) |
| 1962 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | if (gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l, |
| 1966 | &pdp->eua.v, sizeof(pdp->eua.v))) { |
| 1967 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1968 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1969 | len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1970 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1971 | if (gsn->cb_conf) |
| 1972 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1973 | return EOF; |
| 1974 | } |
| 1975 | |
| 1976 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l, |
| 1977 | &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) { |
| 1978 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1979 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1980 | len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1981 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1982 | if (gsn->cb_conf) |
| 1983 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1984 | return EOF; |
| 1985 | } |
| 1986 | |
| 1987 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l, |
| 1988 | &pdp->gsnru.v, sizeof(pdp->gsnru.v))) { |
| 1989 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1990 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1991 | len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1992 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1993 | if (gsn->cb_conf) |
| 1994 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1995 | return EOF; |
| 1996 | } |
| 1997 | |
| 1998 | if (version == 1) { |
| 1999 | if (gtpie_gettlv |
| 2000 | (ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_neg.l, |
| 2001 | &pdp->qos_neg.v, sizeof(pdp->qos_neg.v))) { |
| 2002 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2003 | GTP_LOGPKG(LOGL_ERROR, peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2004 | pack, len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2005 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2006 | if (gsn->cb_conf) |
| 2007 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2008 | return EOF; |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | } |
| 2013 | |
| 2014 | if (gsn->cb_conf) |
| 2015 | gsn->cb_conf(type, cause, pdp, cbp); |
| 2016 | |
| 2017 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2020 | /* API: Send Update PDP Context Request */ |
| 2021 | int gtp_update_context(struct gsn_t *gsn, struct pdp_t *pdp, void *cbp, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2022 | struct in_addr *inetaddr) |
| 2023 | { |
| 2024 | union gtp_packet packet; |
| 2025 | unsigned int length = |
| 2026 | get_default_gtp(pdp->version, GTP_UPDATE_PDP_REQ, &packet); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2027 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2028 | if (pdp->version == 0) |
| 2029 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0, |
| 2030 | sizeof(pdp->qos_req0), pdp->qos_req0); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2031 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2032 | /* Include IMSI if updating with unknown teic_gn */ |
| 2033 | if ((pdp->version == 1) && (!pdp->teic_gn)) |
| 2034 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_IMSI, |
| 2035 | sizeof(pdp->imsi), (uint8_t *) & pdp->imsi); |
| 2036 | |
| 2037 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
| 2038 | gsn->restart_counter); |
| 2039 | |
| 2040 | if (pdp->version == 0) { |
| 2041 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI, pdp->fllu); |
| 2042 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C, pdp->fllc); |
| 2043 | } |
| 2044 | |
| 2045 | if (pdp->version == 1) { |
| 2046 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI, |
| 2047 | pdp->teid_own); |
| 2048 | |
| 2049 | if (!pdp->teic_confirmed) |
| 2050 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C, |
| 2051 | pdp->teic_own); |
| 2052 | } |
| 2053 | |
| 2054 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI, pdp->nsapi); |
| 2055 | |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 2056 | /* TODO |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2057 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_REF, |
| 2058 | pdp->traceref); |
| 2059 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_TYPE, |
| 2060 | pdp->tracetype); */ |
| 2061 | |
| 2062 | /* TODO if ggsn update message |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 2063 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2064 | pdp->eua.l, pdp->eua.v); |
| 2065 | */ |
| 2066 | |
| 2067 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 2068 | pdp->gsnlc.l, pdp->gsnlc.v); |
| 2069 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 2070 | pdp->gsnlu.l, pdp->gsnlu.v); |
| 2071 | |
| 2072 | if (pdp->version == 1) |
| 2073 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE, |
| 2074 | pdp->qos_req.l, pdp->qos_req.v); |
| 2075 | |
| 2076 | if ((pdp->version == 1) && pdp->tft.l) |
| 2077 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TFT, |
| 2078 | pdp->tft.l, pdp->tft.v); |
| 2079 | |
| 2080 | if ((pdp->version == 1) && pdp->triggerid.l) |
| 2081 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TRIGGER_ID, |
| 2082 | pdp->triggerid.l, pdp->triggerid.v); |
| 2083 | |
| 2084 | if ((pdp->version == 1) && pdp->omcid.l) |
| 2085 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_OMC_ID, |
| 2086 | pdp->omcid.l, pdp->omcid.v); |
| 2087 | |
Daniel Willmann | 134a775 | 2016-02-03 18:53:29 +0100 | [diff] [blame] | 2088 | gtp_req(gsn, pdp->version, pdp, &packet, length, inetaddr, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2089 | |
| 2090 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2093 | /* Send Update PDP Context Response */ |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 2094 | static int gtp_update_pdp_resp(struct gsn_t *gsn, uint8_t version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2095 | struct sockaddr_in *peer, int fd, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2096 | void *pack, unsigned len, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2097 | struct pdp_t *pdp, uint8_t cause) |
| 2098 | { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2099 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2100 | union gtp_packet packet; |
| 2101 | unsigned int length = |
| 2102 | get_default_gtp(version, GTP_UPDATE_PDP_RSP, &packet); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2103 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2104 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2105 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2106 | if (cause == GTPCAUSE_ACC_REQ) { |
| 2107 | |
| 2108 | if (version == 0) |
| 2109 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0, |
| 2110 | sizeof(pdp->qos_neg0), pdp->qos_neg0); |
| 2111 | |
| 2112 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
| 2113 | gsn->restart_counter); |
| 2114 | |
| 2115 | if (version == 0) { |
| 2116 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI, |
| 2117 | pdp->fllu); |
| 2118 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C, |
| 2119 | pdp->fllc); |
| 2120 | } |
| 2121 | |
| 2122 | if (version == 1) { |
| 2123 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI, |
| 2124 | pdp->teid_own); |
| 2125 | |
| 2126 | if (!pdp->teic_confirmed) |
| 2127 | gtpie_tv4(&packet, &length, GTP_MAX, |
| 2128 | GTPIE_TEI_C, pdp->teic_own); |
| 2129 | } |
| 2130 | |
| 2131 | /* TODO we use teid_own as charging ID address */ |
| 2132 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_CHARGING_ID, |
| 2133 | pdp->teid_own); |
| 2134 | |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 2135 | /* If ggsn |
| 2136 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2137 | pdp->eua.l, pdp->eua.v); */ |
| 2138 | |
| 2139 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 2140 | pdp->gsnlc.l, pdp->gsnlc.v); |
| 2141 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 2142 | pdp->gsnlu.l, pdp->gsnlu.v); |
| 2143 | |
| 2144 | if (version == 1) |
| 2145 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE, |
| 2146 | pdp->qos_neg.l, pdp->qos_neg.v); |
| 2147 | |
| 2148 | /* TODO: Charging gateway address */ |
| 2149 | } |
| 2150 | |
| 2151 | return gtp_resp(version, gsn, pdp, &packet, length, peer, |
| 2152 | fd, get_seq(pack), get_tid(pack)); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2155 | /* Handle Update PDP Context Request */ |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 2156 | static int gtp_update_pdp_ind(struct gsn_t *gsn, uint8_t version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2157 | struct sockaddr_in *peer, int fd, |
| 2158 | void *pack, unsigned len) |
| 2159 | { |
| 2160 | struct pdp_t *pdp; |
| 2161 | struct pdp_t pdp_backup; |
| 2162 | union gtpie_member *ie[GTPIE_SIZE]; |
| 2163 | uint8_t recovery; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2164 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2165 | uint16_t seq = get_seq(pack); |
| 2166 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2167 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2168 | uint64_t imsi; |
| 2169 | uint8_t nsapi; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2170 | |
Pau Espin Pedrol | de72d26 | 2019-05-29 18:17:05 +0200 | [diff] [blame] | 2171 | /* Is this a duplicate ? */ |
| 2172 | if (!gtp_duplicate(gsn, version, peer, seq)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2173 | return 0; /* We allready send of response once */ |
| 2174 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2175 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2176 | /* Decode information elements */ |
| 2177 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 2178 | gsn->invalid++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2179 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2180 | "Invalid message format\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2181 | if (0 == version) |
| 2182 | return EOF; |
| 2183 | else |
| 2184 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2185 | len, NULL, |
| 2186 | GTPCAUSE_INVALID_MESSAGE); |
| 2187 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2188 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2189 | /* Finding PDP: */ |
| 2190 | /* For GTP0 we use the tunnel identifier to provide imsi and nsapi. */ |
| 2191 | /* For GTP1 we must use imsi and nsapi if imsi is present. Otherwise */ |
| 2192 | /* we have to use the tunnel endpoint identifier */ |
| 2193 | if (version == 0) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2194 | /* Find the context in question */ |
Vadim Yanitskiy | 00a6171 | 2019-08-30 21:00:22 +0200 | [diff] [blame] | 2195 | if (gtp_pdp_tidget(gsn, &pdp, get_tid(pack))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2196 | gsn->err_unknownpdp++; |
Pau Espin Pedrol | bfd3119 | 2021-04-22 15:01:23 +0200 | [diff] [blame] | 2197 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2198 | "Unknown PDP context: TID=0x%" PRIx64 "\n", |
| 2199 | get_tid(pack)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2200 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2201 | len, NULL, |
| 2202 | GTPCAUSE_NON_EXIST); |
| 2203 | } |
Vadim Yanitskiy | 00a6171 | 2019-08-30 21:00:22 +0200 | [diff] [blame] | 2204 | |
| 2205 | /* Update IMSI and NSAPI */ |
| 2206 | pdp_set_imsi_nsapi(pdp, get_tid(pack)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2207 | } else if (version == 1) { |
| 2208 | /* NSAPI (mandatory) */ |
| 2209 | if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &nsapi)) { |
| 2210 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2211 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2212 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2213 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2214 | len, NULL, |
| 2215 | GTPCAUSE_MAN_IE_MISSING); |
| 2216 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2217 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2218 | /* IMSI (conditional) */ |
| 2219 | if (gtpie_gettv0(ie, GTPIE_IMSI, 0, &imsi, sizeof(imsi))) { |
| 2220 | /* Find the context in question */ |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2221 | if (gtp_pdp_getgtp1(gsn, &pdp, get_tei(pack))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2222 | gsn->err_unknownpdp++; |
Pau Espin Pedrol | bfd3119 | 2021-04-22 15:01:23 +0200 | [diff] [blame] | 2223 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2224 | "Unknown PDP context: TEI=0x%" PRIx32 "\n", |
| 2225 | get_tei(pack)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2226 | return gtp_update_pdp_resp(gsn, version, peer, |
| 2227 | fd, pack, len, NULL, |
| 2228 | GTPCAUSE_NON_EXIST); |
| 2229 | } |
| 2230 | } else { |
| 2231 | /* Find the context in question */ |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2232 | if (gtp_pdp_getimsi(gsn, &pdp, imsi, nsapi)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2233 | gsn->err_unknownpdp++; |
Pau Espin Pedrol | bfd3119 | 2021-04-22 15:01:23 +0200 | [diff] [blame] | 2234 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2235 | "Unknown PDP context: IMSI=0x%" PRIx64 |
| 2236 | " NSAPI=%" PRIu8 "\n", imsi, nsapi); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2237 | return gtp_update_pdp_resp(gsn, version, peer, |
| 2238 | fd, pack, len, NULL, |
| 2239 | GTPCAUSE_NON_EXIST); |
| 2240 | } |
| 2241 | } |
| 2242 | } else { |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 2243 | LOGP(DLGTP, LOGL_ERROR, "Unknown version: %d\n", version); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2244 | return EOF; |
| 2245 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2246 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2247 | /* Make a backup copy in case anything is wrong */ |
| 2248 | memcpy(&pdp_backup, pdp, sizeof(pdp_backup)); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2249 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2250 | if (version == 0) { |
| 2251 | if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0, |
| 2252 | pdp->qos_req0, sizeof(pdp->qos_req0))) { |
| 2253 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2254 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2255 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2256 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2257 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2258 | len, pdp, |
| 2259 | GTPCAUSE_MAN_IE_MISSING); |
| 2260 | } |
| 2261 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2262 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2263 | /* Recovery (optional) */ |
| 2264 | if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) { |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 2265 | emit_cb_recovery(gsn, peer, pdp, recovery); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2266 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2267 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2268 | if (version == 0) { |
| 2269 | if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) { |
| 2270 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2271 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2272 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2273 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2274 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2275 | len, pdp, |
| 2276 | GTPCAUSE_MAN_IE_MISSING); |
| 2277 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2278 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2279 | if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) { |
| 2280 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2281 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2282 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2283 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2284 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2285 | len, pdp, |
| 2286 | GTPCAUSE_MAN_IE_MISSING); |
| 2287 | } |
| 2288 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2289 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2290 | if (version == 1) { |
| 2291 | /* TEID (mandatory) */ |
| 2292 | if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) { |
| 2293 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2294 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2295 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2296 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2297 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2298 | len, pdp, |
| 2299 | GTPCAUSE_MAN_IE_MISSING); |
| 2300 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2301 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2302 | /* TEIC (conditional) */ |
| 2303 | /* If TEIC is not included it means that we have allready received it */ |
| 2304 | /* TODO: From 29.060 it is not clear if TEI_C MUST be included for */ |
| 2305 | /* all updated contexts, or only for one of the linked contexts */ |
| 2306 | gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn); |
| 2307 | |
| 2308 | /* NSAPI (mandatory) */ |
| 2309 | if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &pdp->nsapi)) { |
| 2310 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2311 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2312 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2313 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2314 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2315 | len, pdp, |
| 2316 | GTPCAUSE_MAN_IE_MISSING); |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | /* Trace reference (optional) */ |
| 2321 | /* Trace type (optional) */ |
| 2322 | |
| 2323 | /* End User Address (conditional) TODO: GGSN Initiated |
| 2324 | if (gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l, |
| 2325 | &pdp->eua.v, sizeof(pdp->eua.v))) { |
| 2326 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2327 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2328 | "Missing mandatory information field"); |
| 2329 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 2330 | return gtp_update_pdp_resp(gsn, version, pdp, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2331 | GTPCAUSE_MAN_IE_MISSING); |
| 2332 | } */ |
| 2333 | |
| 2334 | /* SGSN address for signalling (mandatory) */ |
| 2335 | /* It is weird that this is mandatory when TEIC is conditional */ |
| 2336 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l, |
| 2337 | &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) { |
| 2338 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2339 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2340 | "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2341 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2342 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, |
| 2343 | pdp, GTPCAUSE_MAN_IE_MISSING); |
| 2344 | } |
| 2345 | |
| 2346 | /* SGSN address for user traffic (mandatory) */ |
| 2347 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l, |
| 2348 | &pdp->gsnru.v, sizeof(pdp->gsnru.v))) { |
| 2349 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2350 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2351 | "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2352 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2353 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, |
| 2354 | pdp, GTPCAUSE_MAN_IE_MISSING); |
| 2355 | } |
| 2356 | |
| 2357 | if (version == 1) { |
| 2358 | /* QoS (mandatory) */ |
| 2359 | if (gtpie_gettlv(ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_req.l, |
| 2360 | &pdp->qos_req.v, sizeof(pdp->qos_req.v))) { |
| 2361 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2362 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2363 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2364 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2365 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2366 | len, pdp, |
| 2367 | GTPCAUSE_MAN_IE_MISSING); |
| 2368 | } |
| 2369 | |
| 2370 | /* TFT (conditional) */ |
| 2371 | if (gtpie_gettlv(ie, GTPIE_TFT, 0, &pdp->tft.l, |
| 2372 | &pdp->tft.v, sizeof(pdp->tft.v))) { |
| 2373 | } |
| 2374 | |
| 2375 | /* OMC identity */ |
| 2376 | } |
| 2377 | |
| 2378 | /* Confirm to peer that things were "successful" */ |
| 2379 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp, |
| 2380 | GTPCAUSE_ACC_REQ); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2383 | /* Handle Update PDP Context Response */ |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 2384 | static int gtp_update_pdp_conf(struct gsn_t *gsn, uint8_t version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2385 | struct sockaddr_in *peer, void *pack, unsigned len) |
| 2386 | { |
Pau Espin Pedrol | 00e0559 | 2021-04-22 13:47:57 +0200 | [diff] [blame] | 2387 | struct pdp_t *pdp = NULL; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2388 | union gtpie_member *ie[GTPIE_SIZE]; |
Pau Espin Pedrol | 00e0559 | 2021-04-22 13:47:57 +0200 | [diff] [blame] | 2389 | uint8_t cause = EOF; |
| 2390 | uint8_t recovery; |
| 2391 | int rc = 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2392 | void *cbp = NULL; |
| 2393 | uint8_t type = 0; |
Pau Espin Pedrol | 00e0559 | 2021-04-22 13:47:57 +0200 | [diff] [blame] | 2394 | bool trigger_recovery = false; |
Daniel Willmann | 05f3ef3 | 2016-02-03 18:53:30 +0100 | [diff] [blame] | 2395 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2396 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2397 | /* Remove packet from queue */ |
| 2398 | if (gtp_conf(gsn, 0, peer, pack, len, &type, &cbp)) |
| 2399 | return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2400 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2401 | /* Decode information elements */ |
Daniel Willmann | 05f3ef3 | 2016-02-03 18:53:30 +0100 | [diff] [blame] | 2402 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2403 | gsn->invalid++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2404 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2405 | "Invalid message format\n"); |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2406 | goto err_out; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2407 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2408 | |
Pau Espin Pedrol | 00e0559 | 2021-04-22 13:47:57 +0200 | [diff] [blame] | 2409 | /* Extract recovery (optional) */ |
| 2410 | if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) |
| 2411 | trigger_recovery = true; |
| 2412 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2413 | /* Extract cause value (mandatory) */ |
| 2414 | if (gtpie_gettv1(ie, GTPIE_CAUSE, 0, &cause)) { |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2415 | goto err_missing; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2416 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2417 | |
Pau Espin Pedrol | 00e0559 | 2021-04-22 13:47:57 +0200 | [diff] [blame] | 2418 | /* 3GPP TS 29.060 sec 8.2: "Receiving node shall send back to the source |
| 2419 | * of the message, a response with the appropriate cause value (either |
| 2420 | * "Non-existent" or "Context not found"). The Tunnel Endpoint |
| 2421 | * Identifier used in the response message shall be set to all zeroes." |
| 2422 | * Hence, TEID=0 in this scenario, it makes no sense to infer PDP ctx |
| 2423 | * from it. User is responsible to infer it from cbp */ |
| 2424 | if (cause != GTPCAUSE_NON_EXIST && cause != GTPCAUSE_CONTEXT_NOT_FOUND) { |
| 2425 | /* Find the context in question */ |
| 2426 | if (gtp_pdp_getgtp1(gsn, &pdp, get_tei(pack))) { |
| 2427 | gsn->err_unknownpdp++; |
| 2428 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
Pau Espin Pedrol | bfd3119 | 2021-04-22 15:01:23 +0200 | [diff] [blame] | 2429 | "Unknown PDP context: TEI=0x%" PRIx32 "\n", get_tei(pack)); |
Pau Espin Pedrol | 00e0559 | 2021-04-22 13:47:57 +0200 | [diff] [blame] | 2430 | goto err_out; |
| 2431 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | /* Check all conditional information elements */ |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2435 | /* TODO: This does not handle GGSN-initiated update responses */ |
Pau Espin Pedrol | 00e0559 | 2021-04-22 13:47:57 +0200 | [diff] [blame] | 2436 | if (cause == GTPCAUSE_ACC_REQ) { |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2437 | if (version == 0) { |
| 2438 | if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0, |
| 2439 | &pdp->qos_neg0, |
| 2440 | sizeof(pdp->qos_neg0))) { |
| 2441 | goto err_missing; |
| 2442 | } |
| 2443 | |
| 2444 | if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) { |
| 2445 | goto err_missing; |
| 2446 | } |
| 2447 | |
| 2448 | if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) { |
| 2449 | goto err_missing; |
| 2450 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2451 | } |
| 2452 | |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2453 | if (version == 1) { |
| 2454 | if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) { |
| 2455 | goto err_missing; |
| 2456 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2457 | |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2458 | if (gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn)) { |
| 2459 | goto err_missing; |
| 2460 | } |
Pau Espin Pedrol | 0b1d9db | 2021-04-21 19:45:23 +0200 | [diff] [blame] | 2461 | /* Register that we have received a valid teic from GGSN */ |
| 2462 | pdp->teic_confirmed = 1; |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | if (gtpie_gettv4(ie, GTPIE_CHARGING_ID, 0, &pdp->cid)) { |
| 2466 | goto err_missing; |
| 2467 | } |
| 2468 | |
| 2469 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l, |
| 2470 | &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) { |
| 2471 | goto err_missing; |
| 2472 | } |
| 2473 | |
| 2474 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l, |
| 2475 | &pdp->gsnru.v, sizeof(pdp->gsnru.v))) { |
| 2476 | goto err_missing; |
| 2477 | } |
| 2478 | |
| 2479 | if (version == 1) { |
| 2480 | if (gtpie_gettlv |
| 2481 | (ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_neg.l, |
| 2482 | &pdp->qos_neg.v, sizeof(pdp->qos_neg.v))) { |
| 2483 | goto err_missing; |
| 2484 | } |
| 2485 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2486 | } |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2487 | |
Pau Espin Pedrol | 00e0559 | 2021-04-22 13:47:57 +0200 | [diff] [blame] | 2488 | generic_ret: |
| 2489 | if (trigger_recovery) |
| 2490 | emit_cb_recovery(gsn, peer, pdp, recovery); |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2491 | if (gsn->cb_conf) |
| 2492 | gsn->cb_conf(type, cause, pdp, cbp); |
Pau Espin Pedrol | 00e0559 | 2021-04-22 13:47:57 +0200 | [diff] [blame] | 2493 | return rc; /* Succes */ |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2494 | |
| 2495 | err_missing: |
| 2496 | gsn->missing++; |
| 2497 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2498 | "Missing information field\n"); |
| 2499 | err_out: |
Pau Espin Pedrol | 00e0559 | 2021-04-22 13:47:57 +0200 | [diff] [blame] | 2500 | rc = EOF; |
| 2501 | goto generic_ret; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2504 | /* API: Deprecated. Send Delete PDP Context Request And free pdp ctx. */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2505 | int gtp_delete_context_req(struct gsn_t *gsn, struct pdp_t *pdp, void *cbp, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2506 | int teardown) |
| 2507 | { |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2508 | struct pdp_t *linked_pdp; |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2509 | |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2510 | if (gtp_pdp_getgtp1(gsn, &linked_pdp, pdp->teic_own)) { |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2511 | LOGP(DLGTP, LOGL_ERROR, |
| 2512 | "Unknown linked PDP context: %u\n", pdp->teic_own); |
| 2513 | return EOF; |
| 2514 | } |
| 2515 | |
| 2516 | if (gtp_delete_context_req2(gsn, pdp, cbp, teardown) == EOF) |
| 2517 | return EOF; |
| 2518 | |
| 2519 | if (teardown) { /* Remove all contexts */ |
Pau Espin Pedrol | 0d0b059 | 2019-05-29 20:42:09 +0200 | [diff] [blame] | 2520 | gtp_freepdp_teardown(gsn, linked_pdp); |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2521 | } else { |
Pau Espin Pedrol | 8651573 | 2019-05-31 16:40:37 +0200 | [diff] [blame] | 2522 | /* If we end up here (no teardown) it means we still |
| 2523 | have at least another pdp context active for this |
| 2524 | PDN connection (since last DeleteReq should come |
| 2525 | with teardown enabled). If the ctx to delete is a |
| 2526 | secondary ctx, simply free it. If it's the primary |
| 2527 | ctx, mark it as nodata but don't free it since we |
| 2528 | need it to hold data linked together and we'll |
| 2529 | require it later to tear down the entire tree. Still, |
| 2530 | we announce its deletion through cb_delete_context |
| 2531 | because we don't want user to release its related |
| 2532 | data and not use it anymore. |
| 2533 | */ |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2534 | if (pdp == linked_pdp) { |
Pau Espin Pedrol | 93dd798 | 2019-05-31 16:42:05 +0200 | [diff] [blame] | 2535 | if (gsn->cb_delete_context) |
| 2536 | gsn->cb_delete_context(pdp); |
| 2537 | pdp->secondary_tei[pdp->nsapi & 0xf0] = 0; |
| 2538 | pdp->nodata = 1; |
| 2539 | } else { |
| 2540 | gtp_freepdp(gsn, pdp); |
| 2541 | } |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | return 0; |
| 2545 | } |
| 2546 | |
Oliver Smith | 1cde2c1 | 2019-05-13 11:35:03 +0200 | [diff] [blame] | 2547 | /* API: Send Delete PDP Context Request. PDP CTX shall be free'd by user at any |
Pau Espin Pedrol | 4e605b3 | 2019-08-29 13:54:28 +0200 | [diff] [blame] | 2548 | point in time later than this function through a call to pdp_freepdp(pdp) (or |
| 2549 | through gtp_freepdp() if willing to receive cb_delete_context() callback), |
| 2550 | but it must be freed no later than during cb_conf(GTP_DELETE_PDP_REQ, pdp) */ |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2551 | int gtp_delete_context_req2(struct gsn_t *gsn, struct pdp_t *pdp, void *cbp, |
| 2552 | int teardown) |
| 2553 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2554 | union gtp_packet packet; |
| 2555 | unsigned int length = |
| 2556 | get_default_gtp(pdp->version, GTP_DELETE_PDP_REQ, &packet); |
| 2557 | struct in_addr addr; |
| 2558 | struct pdp_t *linked_pdp; |
Pau Espin Pedrol | 9ee8d32 | 2019-05-30 14:11:22 +0200 | [diff] [blame] | 2559 | int count; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2560 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2561 | if (gsna2in_addr(&addr, &pdp->gsnrc)) { |
| 2562 | gsn->err_address++; |
Max | 14b1b63 | 2017-08-21 20:14:59 +0200 | [diff] [blame] | 2563 | LOGP(DLGTP, LOGL_ERROR, "GSN address (len=%u) conversion failed\n", pdp->gsnrc.l); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2564 | return EOF; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2565 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2566 | |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2567 | if (gtp_pdp_getgtp1(gsn, &linked_pdp, pdp->teic_own)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2568 | LOGP(DLGTP, LOGL_ERROR, |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 2569 | "Unknown linked PDP context: %u\n", pdp->teic_own); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2570 | return EOF; |
| 2571 | } |
| 2572 | |
| 2573 | if (!teardown) { |
Pau Espin Pedrol | 9ee8d32 | 2019-05-30 14:11:22 +0200 | [diff] [blame] | 2574 | count = pdp_count_secondary(linked_pdp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2575 | if (count <= 1) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2576 | LOGP(DLGTP, LOGL_ERROR, |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 2577 | "Must use teardown for last context: %d\n", count); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2578 | return EOF; |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | if (pdp->version == 1) { |
| 2583 | if (teardown) |
| 2584 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_TEARDOWN, |
| 2585 | 0xff); |
| 2586 | |
| 2587 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI, pdp->nsapi); |
| 2588 | } |
| 2589 | |
| 2590 | gtp_req(gsn, pdp->version, pdp, &packet, length, &addr, cbp); |
| 2591 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2592 | return 0; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2593 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2594 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2595 | /* Send Delete PDP Context Response */ |
| 2596 | int gtp_delete_pdp_resp(struct gsn_t *gsn, int version, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2597 | struct sockaddr_in *peer, int fd, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2598 | void *pack, unsigned len, |
| 2599 | struct pdp_t *pdp, struct pdp_t *linked_pdp, |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2600 | uint8_t cause, int teardown) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2601 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2602 | union gtp_packet packet; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2603 | unsigned int length = |
| 2604 | get_default_gtp(version, GTP_DELETE_PDP_RSP, &packet); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2605 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2606 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2607 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2608 | gtp_resp(version, gsn, pdp, &packet, length, peer, fd, |
| 2609 | get_seq(pack), get_tid(pack)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2610 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2611 | if (cause == GTPCAUSE_ACC_REQ) { |
| 2612 | if ((teardown) || (version == 0)) { /* Remove all contexts */ |
Pau Espin Pedrol | 0d0b059 | 2019-05-29 20:42:09 +0200 | [diff] [blame] | 2613 | gtp_freepdp_teardown(gsn, linked_pdp); |
Pau Espin Pedrol | 8651573 | 2019-05-31 16:40:37 +0200 | [diff] [blame] | 2614 | } else { |
| 2615 | /* If we end up here (no teardown) it means we still |
| 2616 | have at least another pdp context active for this |
| 2617 | PDN connection (since last DeleteReq should come |
| 2618 | with teardown enabled). If the ctx to delete is a |
| 2619 | secondary ctx, simply free it. If it's the primary |
| 2620 | ctx, mark it as nodata but don't free it since we |
| 2621 | need it to hold data linked together and we'll |
| 2622 | require it later to tear down the entire tree. Still, |
| 2623 | we announce its deletion through cb_delete_context |
| 2624 | because we don't want user to release its related |
| 2625 | data and not use it anymore. |
| 2626 | */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2627 | if (pdp == linked_pdp) { |
Pau Espin Pedrol | 93dd798 | 2019-05-31 16:42:05 +0200 | [diff] [blame] | 2628 | if (gsn->cb_delete_context) |
| 2629 | gsn->cb_delete_context(pdp); |
| 2630 | pdp->secondary_tei[pdp->nsapi & 0xf0] = 0; |
| 2631 | pdp->nodata = 1; |
| 2632 | } else { |
| 2633 | gtp_freepdp(gsn, pdp); |
| 2634 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2635 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2636 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2637 | /* if (cause == GTPCAUSE_ACC_REQ) */ |
| 2638 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2639 | } |
| 2640 | |
| 2641 | /* Handle Delete PDP Context Request */ |
| 2642 | int gtp_delete_pdp_ind(struct gsn_t *gsn, int version, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2643 | struct sockaddr_in *peer, int fd, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2644 | void *pack, unsigned len) |
| 2645 | { |
| 2646 | struct pdp_t *pdp = NULL; |
| 2647 | struct pdp_t *linked_pdp = NULL; |
| 2648 | union gtpie_member *ie[GTPIE_SIZE]; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2649 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2650 | uint16_t seq = get_seq(pack); |
| 2651 | int hlen = get_hlen(pack); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2652 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2653 | uint8_t nsapi; |
| 2654 | uint8_t teardown = 0; |
Pau Espin Pedrol | 9ee8d32 | 2019-05-30 14:11:22 +0200 | [diff] [blame] | 2655 | int count; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2656 | |
Pau Espin Pedrol | de72d26 | 2019-05-29 18:17:05 +0200 | [diff] [blame] | 2657 | /* Is this a duplicate ? */ |
| 2658 | if (!gtp_duplicate(gsn, version, peer, seq)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2659 | return 0; /* We allready send off response once */ |
| 2660 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2661 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2662 | /* Find the linked context in question */ |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2663 | if (gtp_pdp_getgtp1(gsn, &linked_pdp, get_tei(pack))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2664 | gsn->err_unknownpdp++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2665 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
Pau Espin Pedrol | bfd3119 | 2021-04-22 15:01:23 +0200 | [diff] [blame] | 2666 | "Unknown PDP context: TEI=0x%" PRIx32 "\n", get_tei(pack)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2667 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, |
| 2668 | NULL, NULL, GTPCAUSE_NON_EXIST, |
| 2669 | teardown); |
| 2670 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2671 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2672 | /* If version 0 this is also the secondary context */ |
| 2673 | if (version == 0) |
| 2674 | pdp = linked_pdp; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2675 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2676 | /* Decode information elements */ |
| 2677 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 2678 | gsn->invalid++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2679 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2680 | "Invalid message format\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2681 | if (0 == version) |
| 2682 | return EOF; |
| 2683 | else |
| 2684 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, |
| 2685 | len, NULL, NULL, |
| 2686 | GTPCAUSE_INVALID_MESSAGE, |
| 2687 | teardown); |
| 2688 | } |
| 2689 | |
| 2690 | if (version == 1) { |
| 2691 | /* NSAPI (mandatory) */ |
| 2692 | if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &nsapi)) { |
| 2693 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2694 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
Pau Espin Pedrol | bfd3119 | 2021-04-22 15:01:23 +0200 | [diff] [blame] | 2695 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2696 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, |
| 2697 | len, NULL, NULL, |
| 2698 | GTPCAUSE_MAN_IE_MISSING, |
| 2699 | teardown); |
| 2700 | } |
| 2701 | |
| 2702 | /* Find the context in question */ |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2703 | if (gtp_pdp_getgtp1(gsn, &pdp, linked_pdp->secondary_tei[nsapi & 0x0f])) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2704 | gsn->err_unknownpdp++; |
Pau Espin Pedrol | bfd3119 | 2021-04-22 15:01:23 +0200 | [diff] [blame] | 2705 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2706 | "Unknown PDP context: Secondary TEI=0x%" PRIx32 "\n", |
| 2707 | linked_pdp->secondary_tei[nsapi & 0x0f]); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2708 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, |
| 2709 | len, NULL, NULL, |
| 2710 | GTPCAUSE_NON_EXIST, |
| 2711 | teardown); |
| 2712 | } |
| 2713 | |
| 2714 | /* Teardown (conditional) */ |
| 2715 | gtpie_gettv1(ie, GTPIE_TEARDOWN, 0, &teardown); |
| 2716 | |
| 2717 | if (!teardown) { |
Pau Espin Pedrol | 742a6b5 | 2019-05-30 13:36:10 +0200 | [diff] [blame] | 2718 | /* TS 29.060 section 7.3.5: If a GSN receives a Delete PDP context |
| 2719 | * without a Teardown Indicator or with a Teardown Indicator with |
| 2720 | * value set to "0" and only that PDP context is active for a PDN |
| 2721 | * connection, then the GSN shall ignore the message. (Note: |
| 2722 | * This is symptom of a race condition. The reliable delivery of |
| 2723 | * signalling messages will eventually lead to a consistent |
| 2724 | * situation, allowing the teardown of the PDP context.) |
| 2725 | */ |
Pau Espin Pedrol | 9ee8d32 | 2019-05-30 14:11:22 +0200 | [diff] [blame] | 2726 | count = pdp_count_secondary(linked_pdp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2727 | if (count <= 1) { |
Pau Espin Pedrol | d1bd6fc | 2018-07-13 19:11:45 +0200 | [diff] [blame] | 2728 | GTP_LOGPKG(LOGL_NOTICE, peer, pack, len, |
| 2729 | "Ignoring CTX DEL without teardown and count=%d\n", |
| 2730 | count); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2731 | return 0; /* 29.060 7.3.5 Ignore message */ |
| 2732 | } |
| 2733 | } |
| 2734 | } |
| 2735 | |
| 2736 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, |
| 2737 | pdp, linked_pdp, GTPCAUSE_ACC_REQ, teardown); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2738 | } |
| 2739 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2740 | /* Handle Delete PDP Context Response */ |
| 2741 | int gtp_delete_pdp_conf(struct gsn_t *gsn, int version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2742 | struct sockaddr_in *peer, void *pack, unsigned len) |
| 2743 | { |
| 2744 | union gtpie_member *ie[GTPIE_SIZE]; |
| 2745 | uint8_t cause; |
| 2746 | void *cbp = NULL; |
| 2747 | uint8_t type = 0; |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2748 | struct pdp_t *pdp = NULL; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2749 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2750 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2751 | /* Remove packet from queue */ |
| 2752 | if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) |
| 2753 | return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2754 | |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2755 | /* Find the context in question. It may not be available if gtp_delete_context_req |
| 2756 | * was used and as a result the PDP ctx was already freed */ |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2757 | if (gtp_pdp_getgtp1(gsn, &pdp, get_tei(pack))) { |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2758 | gsn->err_unknownpdp++; |
| 2759 | GTP_LOGPKG(LOGL_NOTICE, peer, pack, len, |
Pau Espin Pedrol | bfd3119 | 2021-04-22 15:01:23 +0200 | [diff] [blame] | 2760 | "Unknown PDP context: TEI=0x%" PRIx32 " (expected if " |
| 2761 | "gtp_delete_context_req is used or pdp ctx was freed " |
| 2762 | "manually before response)\n", get_tei(pack)); |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2763 | if (gsn->cb_conf) |
| 2764 | gsn->cb_conf(type, EOF, NULL, cbp); |
| 2765 | return EOF; |
| 2766 | } |
| 2767 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2768 | /* Decode information elements */ |
| 2769 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 2770 | gsn->invalid++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2771 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2772 | "Invalid message format\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2773 | if (gsn->cb_conf) |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2774 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2775 | return EOF; |
| 2776 | } |
| 2777 | |
| 2778 | /* Extract cause value (mandatory) */ |
| 2779 | if (gtpie_gettv1(ie, GTPIE_CAUSE, 0, &cause)) { |
| 2780 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2781 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2782 | "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2783 | if (gsn->cb_conf) |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2784 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2785 | return EOF; |
| 2786 | } |
| 2787 | |
| 2788 | /* Check the cause value (again) */ |
| 2789 | if ((GTPCAUSE_ACC_REQ != cause) && (GTPCAUSE_NON_EXIST != cause)) { |
| 2790 | gsn->err_cause++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2791 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2792 | "Unexpected cause value received: %d\n", cause); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2793 | if (gsn->cb_conf) |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2794 | gsn->cb_conf(type, cause, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2795 | return EOF; |
| 2796 | } |
| 2797 | |
| 2798 | /* Callback function to notify application */ |
| 2799 | if (gsn->cb_conf) |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2800 | gsn->cb_conf(type, cause, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2801 | |
| 2802 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
Harald Welte | 54d082e | 2017-08-12 22:43:21 +0200 | [diff] [blame] | 2805 | /* Send Error Indication (response to a GPDU message) - 3GPP TS 29.060 7.3.7 */ |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 2806 | static int gtp_error_ind_resp(struct gsn_t *gsn, uint8_t version, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2807 | struct sockaddr_in *peer, int fd, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2808 | void *pack, unsigned len) |
| 2809 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2810 | union gtp_packet packet; |
| 2811 | unsigned int length = get_default_gtp(version, GTP_ERROR, &packet); |
| 2812 | |
Harald Welte | 54d082e | 2017-08-12 22:43:21 +0200 | [diff] [blame] | 2813 | if (version == 1) { |
| 2814 | /* Mandatory 7.7.13 TEI Data I */ |
| 2815 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI, |
| 2816 | ntoh32(((union gtp_packet *)pack)->gtp1l.h.tei)); |
| 2817 | |
| 2818 | /* Mandatory 7.7.32 GSN Address */ |
| 2819 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 2820 | sizeof(gsn->gsnu), &gsn->gsnu); |
| 2821 | } |
| 2822 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2823 | return gtp_resp(version, gsn, NULL, &packet, length, peer, fd, |
| 2824 | get_seq(pack), get_tid(pack)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | /* Handle Error Indication */ |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 2828 | static int gtp_error_ind_conf(struct gsn_t *gsn, uint8_t version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2829 | struct sockaddr_in *peer, void *pack, unsigned len) |
| 2830 | { |
Harald Welte | 37d5b15 | 2017-08-12 23:58:29 +0200 | [diff] [blame] | 2831 | union gtpie_member *ie[GTPIE_SIZE]; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2832 | struct pdp_t *pdp; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2833 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2834 | /* Find the context in question */ |
Harald Welte | 37d5b15 | 2017-08-12 23:58:29 +0200 | [diff] [blame] | 2835 | if (version == 0) { |
Vadim Yanitskiy | 68c5a74 | 2019-08-30 21:04:19 +0200 | [diff] [blame] | 2836 | if (gtp_pdp_tidget(gsn, &pdp, get_tid(pack))) { |
Harald Welte | 37d5b15 | 2017-08-12 23:58:29 +0200 | [diff] [blame] | 2837 | gsn->err_unknownpdp++; |
| 2838 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
Pau Espin Pedrol | bfd3119 | 2021-04-22 15:01:23 +0200 | [diff] [blame] | 2839 | "Unknown PDP context: TID=0x%" PRIx64 "\n", |
| 2840 | get_tid(pack)); |
Harald Welte | 37d5b15 | 2017-08-12 23:58:29 +0200 | [diff] [blame] | 2841 | return EOF; |
| 2842 | } |
| 2843 | } else if (version == 1) { |
| 2844 | /* we have to look-up based on the *peer* TEID */ |
| 2845 | int hlen = get_hlen(pack); |
| 2846 | uint32_t teid_gn; |
| 2847 | |
| 2848 | /* Decode information elements */ |
| 2849 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 2850 | gsn->invalid++; |
| 2851 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2852 | "Invalid message format\n"); |
| 2853 | return EOF; |
| 2854 | } |
| 2855 | |
| 2856 | if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &teid_gn)) { |
| 2857 | gsn->missing++; |
| 2858 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2859 | "Missing mandatory information field\n"); |
| 2860 | return EOF; |
| 2861 | } |
| 2862 | |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2863 | if (gtp_pdp_getgtp1_peer_d(gsn, &pdp, peer, teid_gn)) { |
Harald Welte | 37d5b15 | 2017-08-12 23:58:29 +0200 | [diff] [blame] | 2864 | gsn->err_unknownpdp++; |
Pau Espin Pedrol | bfd3119 | 2021-04-22 15:01:23 +0200 | [diff] [blame] | 2865 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2866 | "Unknown PDP context: Peer TEID=0x%" PRIx32 "\n", |
| 2867 | teid_gn); |
Harald Welte | 37d5b15 | 2017-08-12 23:58:29 +0200 | [diff] [blame] | 2868 | return EOF; |
| 2869 | } |
Vadim Yanitskiy | bdf2cf9 | 2019-08-30 21:23:11 +0200 | [diff] [blame] | 2870 | } else { |
| 2871 | LOGP(DLGTP, LOGL_ERROR, "Unknown version: %d\n", version); |
| 2872 | return EOF; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2873 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2874 | |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2875 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2876 | "Received Error Indication\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2877 | |
Harald Welte | bd22824 | 2017-11-06 03:16:49 +0900 | [diff] [blame] | 2878 | /* This is obvious from above code, given the semantics of the |
| 2879 | * functions above, but Coverity doesn't figure this out, so |
| 2880 | * let's make it clear. It's good style anyway in case above |
| 2881 | * code should ever change. */ |
| 2882 | OSMO_ASSERT(pdp); |
| 2883 | |
Pau Espin Pedrol | 0d0b059 | 2019-05-29 20:42:09 +0200 | [diff] [blame] | 2884 | gtp_freepdp(gsn, pdp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2885 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 2888 | static int gtp_gpdu_ind(struct gsn_t *gsn, uint8_t version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2889 | struct sockaddr_in *peer, int fd, void *pack, unsigned len) |
| 2890 | { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2891 | |
Pau Espin Pedrol | 282d4e3 | 2018-01-25 18:20:51 +0100 | [diff] [blame] | 2892 | int hlen; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2893 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2894 | struct pdp_t *pdp; |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 2895 | |
Pau Espin Pedrol | 42d3250 | 2018-01-25 18:17:17 +0100 | [diff] [blame] | 2896 | switch (version) { |
| 2897 | case 0: |
Pau Espin Pedrol | bfd3119 | 2021-04-22 15:01:23 +0200 | [diff] [blame] | 2898 | if (gtp_pdp_getgtp0(gsn, &pdp, get_tei(pack))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2899 | gsn->err_unknownpdp++; |
Pau Espin Pedrol | bfd3119 | 2021-04-22 15:01:23 +0200 | [diff] [blame] | 2900 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2901 | "Unknown PDP context: TEI=0x%" PRIx32 "\n", |
| 2902 | get_tei(pack)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2903 | return gtp_error_ind_resp(gsn, version, peer, fd, pack, |
| 2904 | len); |
| 2905 | } |
| 2906 | hlen = GTP0_HEADER_SIZE; |
Pau Espin Pedrol | 42d3250 | 2018-01-25 18:17:17 +0100 | [diff] [blame] | 2907 | break; |
| 2908 | case 1: |
Pau Espin Pedrol | bfd3119 | 2021-04-22 15:01:23 +0200 | [diff] [blame] | 2909 | if (gtp_pdp_getgtp1(gsn, &pdp, get_tei(pack))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2910 | gsn->err_unknownpdp++; |
Pau Espin Pedrol | bfd3119 | 2021-04-22 15:01:23 +0200 | [diff] [blame] | 2911 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2912 | "Unknown PDP context: TEI=0x%" PRIx32 "\n", |
| 2913 | get_tei(pack)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2914 | return gtp_error_ind_resp(gsn, version, peer, fd, pack, |
| 2915 | len); |
| 2916 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2917 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2918 | /* Is this a long or a short header ? */ |
| 2919 | if (((union gtp_packet *)pack)->gtp1l.h.flags & 0x07) |
| 2920 | hlen = GTP1_HEADER_SIZE_LONG; |
| 2921 | else |
| 2922 | hlen = GTP1_HEADER_SIZE_SHORT; |
Pau Espin Pedrol | 42d3250 | 2018-01-25 18:17:17 +0100 | [diff] [blame] | 2923 | break; |
| 2924 | default: |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2925 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 2926 | "Unknown version: %d\n", version); |
Pau Espin Pedrol | 282d4e3 | 2018-01-25 18:20:51 +0100 | [diff] [blame] | 2927 | return EOF; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2928 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2929 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2930 | /* If the GPDU was not from the peer GSN tell him to delete context */ |
| 2931 | if (memcmp(&peer->sin_addr, pdp->gsnru.v, pdp->gsnru.l)) { /* TODO Range? */ |
| 2932 | gsn->err_unknownpdp++; |
Max | 14b1b63 | 2017-08-21 20:14:59 +0200 | [diff] [blame] | 2933 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, "Unknown GSN peer %s\n", inet_ntoa(peer->sin_addr)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2934 | return gtp_error_ind_resp(gsn, version, peer, fd, pack, len); |
| 2935 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2936 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2937 | /* Callback function */ |
| 2938 | if (gsn->cb_data_ind != 0) |
| 2939 | return gsn->cb_data_ind(pdp, pack + hlen, len - hlen); |
| 2940 | |
| 2941 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 2944 | /* Receives GTP packet and sends off for further processing |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2945 | * Function will check the validity of the header. If the header |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 2946 | * is not valid the packet is either dropped or a version not |
| 2947 | * supported is returned to the peer. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2948 | * TODO: Need to decide on return values! */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2949 | int gtp_decaps0(struct gsn_t *gsn) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2950 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2951 | unsigned char buffer[PACKET_MAX]; |
| 2952 | struct sockaddr_in peer; |
Harald Welte | d88e11d | 2011-11-02 13:09:43 +0100 | [diff] [blame] | 2953 | socklen_t peerlen; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2954 | int status; |
| 2955 | struct gtp0_header *pheader; |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 2956 | uint8_t version; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2957 | int fd = gsn->fd0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2958 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2959 | /* TODO: Need strategy of userspace buffering and blocking */ |
| 2960 | /* Currently read is non-blocking and send is blocking. */ |
| 2961 | /* This means that the program have to wait for busy send calls... */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2962 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2963 | while (1) { /* Loop until no more to read */ |
| 2964 | if (fcntl(gsn->fd0, F_SETFL, O_NONBLOCK)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2965 | LOGP(DLGTP, LOGL_ERROR, "fnctl()\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2966 | return -1; |
| 2967 | } |
| 2968 | peerlen = sizeof(peer); |
| 2969 | if ((status = |
| 2970 | recvfrom(gsn->fd0, buffer, sizeof(buffer), 0, |
| 2971 | (struct sockaddr *)&peer, &peerlen)) < 0) { |
| 2972 | if (errno == EAGAIN) |
| 2973 | return 0; |
| 2974 | gsn->err_readfrom++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2975 | LOGP(DLGTP, LOGL_ERROR, |
Alexander Huemer | db852a1 | 2015-11-06 20:59:01 +0100 | [diff] [blame] | 2976 | "recvfrom(fd0=%d, buffer=%lx, len=%zu) failed: status = %d error = %s\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2977 | gsn->fd0, (unsigned long)buffer, sizeof(buffer), |
| 2978 | status, status ? strerror(errno) : "No error"); |
| 2979 | return -1; |
| 2980 | } |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 2981 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2982 | /* Need at least 1 byte in order to check version */ |
| 2983 | if (status < (1)) { |
| 2984 | gsn->empty++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2985 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
| 2986 | status, "Discarding packet - too small\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2987 | continue; |
| 2988 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2989 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2990 | pheader = (struct gtp0_header *)(buffer); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2991 | |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 2992 | version = GTPHDR_F_GET_VER(pheader->flags); |
| 2993 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2994 | /* Version should be gtp0 (or earlier) */ |
| 2995 | /* 09.60 is somewhat unclear on this issue. On gsn->fd0 we expect only */ |
| 2996 | /* GTP 0 messages. If other version message is received we reply that we */ |
| 2997 | /* only support version 0, implying that this is the only version */ |
| 2998 | /* supported on this port */ |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 2999 | if (version > 0) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3000 | gsn->unsup++; |
Pau Espin Pedrol | a884a95 | 2018-01-25 17:28:11 +0100 | [diff] [blame] | 3001 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, status, |
| 3002 | "Unsupported GTP version %"PRIu8"\n", version); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3003 | gtp_unsup_req(gsn, 0, &peer, gsn->fd0, buffer, status); /* 29.60: 11.1.1 */ |
| 3004 | continue; |
| 3005 | } |
| 3006 | |
| 3007 | /* Check length of gtp0 packet */ |
| 3008 | if (status < GTP0_HEADER_SIZE) { |
| 3009 | gsn->tooshort++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3010 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
| 3011 | status, "GTP0 packet too short\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3012 | continue; /* Silently discard 29.60: 11.1.2 */ |
| 3013 | } |
| 3014 | |
| 3015 | /* Check packet length field versus length of packet */ |
| 3016 | if (status != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) { |
| 3017 | gsn->tooshort++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3018 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3019 | status, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3020 | "GTP packet length field does not match actual length\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3021 | continue; /* Silently discard */ |
| 3022 | } |
| 3023 | |
| 3024 | if ((gsn->mode == GTP_MODE_GGSN) && |
| 3025 | ((pheader->type == GTP_CREATE_PDP_RSP) || |
Pau Espin Pedrol | a32e4c4 | 2018-07-13 19:05:00 +0200 | [diff] [blame] | 3026 | (pheader->type == GTP_UPDATE_PDP_RSP))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3027 | gsn->unexpect++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3028 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3029 | status, |
Pau Espin Pedrol | 3b84e92 | 2018-07-13 18:32:35 +0200 | [diff] [blame] | 3030 | "Unexpected GTPv0 Signalling Message '%s'\n", |
| 3031 | get_value_string(gtp_type_names, pheader->type)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3032 | continue; /* Silently discard 29.60: 11.1.4 */ |
| 3033 | } |
| 3034 | |
| 3035 | if ((gsn->mode == GTP_MODE_SGSN) && |
| 3036 | ((pheader->type == GTP_CREATE_PDP_REQ) || |
Pau Espin Pedrol | a32e4c4 | 2018-07-13 19:05:00 +0200 | [diff] [blame] | 3037 | (pheader->type == GTP_UPDATE_PDP_REQ))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3038 | gsn->unexpect++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3039 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3040 | status, |
Pau Espin Pedrol | 3b84e92 | 2018-07-13 18:32:35 +0200 | [diff] [blame] | 3041 | "Unexpected GTPv0 Signalling Message '%s'\n", |
| 3042 | get_value_string(gtp_type_names, pheader->type)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3043 | continue; /* Silently discard 29.60: 11.1.4 */ |
| 3044 | } |
| 3045 | |
| 3046 | switch (pheader->type) { |
| 3047 | case GTP_ECHO_REQ: |
| 3048 | gtp_echo_ind(gsn, version, &peer, fd, buffer, status); |
| 3049 | break; |
| 3050 | case GTP_ECHO_RSP: |
| 3051 | gtp_echo_conf(gsn, version, &peer, buffer, status); |
| 3052 | break; |
| 3053 | case GTP_NOT_SUPPORTED: |
| 3054 | gtp_unsup_ind(gsn, &peer, buffer, status); |
| 3055 | break; |
| 3056 | case GTP_CREATE_PDP_REQ: |
| 3057 | gtp_create_pdp_ind(gsn, version, &peer, fd, buffer, |
| 3058 | status); |
| 3059 | break; |
| 3060 | case GTP_CREATE_PDP_RSP: |
| 3061 | gtp_create_pdp_conf(gsn, version, &peer, buffer, |
| 3062 | status); |
| 3063 | break; |
| 3064 | case GTP_UPDATE_PDP_REQ: |
| 3065 | gtp_update_pdp_ind(gsn, version, &peer, fd, buffer, |
| 3066 | status); |
| 3067 | break; |
| 3068 | case GTP_UPDATE_PDP_RSP: |
| 3069 | gtp_update_pdp_conf(gsn, version, &peer, buffer, |
| 3070 | status); |
| 3071 | break; |
| 3072 | case GTP_DELETE_PDP_REQ: |
| 3073 | gtp_delete_pdp_ind(gsn, version, &peer, fd, buffer, |
| 3074 | status); |
| 3075 | break; |
| 3076 | case GTP_DELETE_PDP_RSP: |
| 3077 | gtp_delete_pdp_conf(gsn, version, &peer, buffer, |
| 3078 | status); |
| 3079 | break; |
| 3080 | case GTP_ERROR: |
| 3081 | gtp_error_ind_conf(gsn, version, &peer, buffer, status); |
| 3082 | break; |
| 3083 | case GTP_GPDU: |
| 3084 | gtp_gpdu_ind(gsn, version, &peer, fd, buffer, status); |
| 3085 | break; |
| 3086 | default: |
| 3087 | gsn->unknown++; |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 3088 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, status, |
| 3089 | "Unknown GTP message type received: %d\n", |
| 3090 | pheader->type); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3091 | break; |
| 3092 | } |
| 3093 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3096 | int gtp_decaps1c(struct gsn_t *gsn) |
| 3097 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3098 | unsigned char buffer[PACKET_MAX]; |
| 3099 | struct sockaddr_in peer; |
Harald Welte | d88e11d | 2011-11-02 13:09:43 +0100 | [diff] [blame] | 3100 | socklen_t peerlen; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3101 | int status; |
| 3102 | struct gtp1_header_short *pheader; |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 3103 | uint8_t version; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3104 | int fd = gsn->fd1c; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3105 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3106 | /* TODO: Need strategy of userspace buffering and blocking */ |
| 3107 | /* Currently read is non-blocking and send is blocking. */ |
| 3108 | /* This means that the program have to wait for busy send calls... */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3109 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3110 | while (1) { /* Loop until no more to read */ |
| 3111 | if (fcntl(fd, F_SETFL, O_NONBLOCK)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3112 | LOGP(DLGTP, LOGL_ERROR, "fnctl()\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3113 | return -1; |
| 3114 | } |
| 3115 | peerlen = sizeof(peer); |
| 3116 | if ((status = |
| 3117 | recvfrom(fd, buffer, sizeof(buffer), 0, |
| 3118 | (struct sockaddr *)&peer, &peerlen)) < 0) { |
| 3119 | if (errno == EAGAIN) |
| 3120 | return 0; |
| 3121 | gsn->err_readfrom++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3122 | LOGP(DLGTP, LOGL_ERROR, |
Alexander Huemer | db852a1 | 2015-11-06 20:59:01 +0100 | [diff] [blame] | 3123 | "recvfrom(fd=%d, buffer=%lx, len=%zu) failed: status = %d error = %s\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3124 | fd, (unsigned long)buffer, sizeof(buffer), |
| 3125 | status, status ? strerror(errno) : "No error"); |
| 3126 | return -1; |
| 3127 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3128 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3129 | /* Need at least 1 byte in order to check version */ |
| 3130 | if (status < (1)) { |
| 3131 | gsn->empty++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3132 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
| 3133 | status, "Discarding packet - too small\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3134 | continue; |
| 3135 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3136 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3137 | pheader = (struct gtp1_header_short *)(buffer); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3138 | |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 3139 | version = GTPHDR_F_GET_VER(pheader->flags); |
| 3140 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3141 | /* Version must be no larger than GTP 1 */ |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 3142 | if (version > 1) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3143 | gsn->unsup++; |
Pau Espin Pedrol | a884a95 | 2018-01-25 17:28:11 +0100 | [diff] [blame] | 3144 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, status, |
| 3145 | "Unsupported GTP version %"PRIu8"\n", version); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3146 | gtp_unsup_req(gsn, version, &peer, fd, buffer, status); |
| 3147 | /*29.60: 11.1.1 */ |
| 3148 | continue; |
| 3149 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3150 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3151 | /* Version must be at least GTP 1 */ |
| 3152 | /* 29.060 is somewhat unclear on this issue. On gsn->fd1c we expect only */ |
| 3153 | /* GTP 1 messages. If GTP 0 message is received we silently discard */ |
| 3154 | /* the message */ |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 3155 | if (version < 1) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3156 | gsn->unsup++; |
Pau Espin Pedrol | a884a95 | 2018-01-25 17:28:11 +0100 | [diff] [blame] | 3157 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, status, |
| 3158 | "Unsupported GTP version %"PRIu8"\n", version); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3159 | continue; |
| 3160 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3161 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3162 | /* Check packet flag field */ |
| 3163 | if (((pheader->flags & 0xf7) != 0x32)) { |
| 3164 | gsn->unsup++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3165 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | d37b80a | 2016-12-15 18:33:15 +0100 | [diff] [blame] | 3166 | status, "Unsupported packet flags: 0x%02x\n", pheader->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3167 | continue; |
| 3168 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 3169 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3170 | /* Check length of packet */ |
| 3171 | if (status < GTP1_HEADER_SIZE_LONG) { |
| 3172 | gsn->tooshort++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3173 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
| 3174 | status, "GTP packet too short\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3175 | continue; /* Silently discard 29.60: 11.1.2 */ |
| 3176 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 3177 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3178 | /* Check packet length field versus length of packet */ |
| 3179 | if (status != |
| 3180 | (ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT)) { |
| 3181 | gsn->tooshort++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3182 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3183 | status, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3184 | "GTP packet length field does not match actual length\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3185 | continue; /* Silently discard */ |
| 3186 | } |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 3187 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3188 | /* Check for extension headers */ |
| 3189 | /* TODO: We really should cycle through the headers and determine */ |
| 3190 | /* if any have the comprehension required flag set */ |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 3191 | if (((pheader->flags & GTP1HDR_F_EXT) != 0x00)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3192 | gsn->unsup++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3193 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
| 3194 | status, "Unsupported extension header\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3195 | gtp_extheader_req(gsn, version, &peer, fd, buffer, |
| 3196 | status); |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 3197 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3198 | continue; |
| 3199 | } |
| 3200 | |
| 3201 | if ((gsn->mode == GTP_MODE_GGSN) && |
| 3202 | ((pheader->type == GTP_CREATE_PDP_RSP) || |
Pau Espin Pedrol | a32e4c4 | 2018-07-13 19:05:00 +0200 | [diff] [blame] | 3203 | (pheader->type == GTP_UPDATE_PDP_RSP))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3204 | gsn->unexpect++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3205 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3206 | status, |
Pau Espin Pedrol | 3b84e92 | 2018-07-13 18:32:35 +0200 | [diff] [blame] | 3207 | "Unexpected GTPv1 Signalling Message '%s'\n", |
| 3208 | get_value_string(gtp_type_names, pheader->type)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3209 | continue; /* Silently discard 29.60: 11.1.4 */ |
| 3210 | } |
| 3211 | |
| 3212 | if ((gsn->mode == GTP_MODE_SGSN) && |
| 3213 | ((pheader->type == GTP_CREATE_PDP_REQ) || |
Pau Espin Pedrol | a32e4c4 | 2018-07-13 19:05:00 +0200 | [diff] [blame] | 3214 | (pheader->type == GTP_UPDATE_PDP_REQ))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3215 | gsn->unexpect++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3216 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3217 | status, |
Pau Espin Pedrol | 3b84e92 | 2018-07-13 18:32:35 +0200 | [diff] [blame] | 3218 | "Unexpected GTPv1 Signalling Message '%s'\n", |
| 3219 | get_value_string(gtp_type_names, pheader->type)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3220 | continue; /* Silently discard 29.60: 11.1.4 */ |
| 3221 | } |
| 3222 | |
| 3223 | switch (pheader->type) { |
| 3224 | case GTP_ECHO_REQ: |
| 3225 | gtp_echo_ind(gsn, version, &peer, fd, buffer, status); |
| 3226 | break; |
| 3227 | case GTP_ECHO_RSP: |
| 3228 | gtp_echo_conf(gsn, version, &peer, buffer, status); |
| 3229 | break; |
| 3230 | case GTP_NOT_SUPPORTED: |
| 3231 | gtp_unsup_ind(gsn, &peer, buffer, status); |
| 3232 | break; |
| 3233 | case GTP_SUPP_EXT_HEADER: |
| 3234 | gtp_extheader_ind(gsn, &peer, buffer, status); |
| 3235 | break; |
| 3236 | case GTP_CREATE_PDP_REQ: |
| 3237 | gtp_create_pdp_ind(gsn, version, &peer, fd, buffer, |
| 3238 | status); |
| 3239 | break; |
| 3240 | case GTP_CREATE_PDP_RSP: |
| 3241 | gtp_create_pdp_conf(gsn, version, &peer, buffer, |
| 3242 | status); |
| 3243 | break; |
| 3244 | case GTP_UPDATE_PDP_REQ: |
| 3245 | gtp_update_pdp_ind(gsn, version, &peer, fd, buffer, |
| 3246 | status); |
| 3247 | break; |
| 3248 | case GTP_UPDATE_PDP_RSP: |
| 3249 | gtp_update_pdp_conf(gsn, version, &peer, buffer, |
| 3250 | status); |
| 3251 | break; |
| 3252 | case GTP_DELETE_PDP_REQ: |
| 3253 | gtp_delete_pdp_ind(gsn, version, &peer, fd, buffer, |
| 3254 | status); |
| 3255 | break; |
| 3256 | case GTP_DELETE_PDP_RSP: |
| 3257 | gtp_delete_pdp_conf(gsn, version, &peer, buffer, |
| 3258 | status); |
| 3259 | break; |
| 3260 | case GTP_ERROR: |
| 3261 | gtp_error_ind_conf(gsn, version, &peer, buffer, status); |
| 3262 | break; |
Pau Espin Pedrol | f32c6a9 | 2021-05-03 17:58:55 +0200 | [diff] [blame] | 3263 | case GTP_RAN_INFO_RELAY: |
| 3264 | gtp_ran_info_relay_ind(gsn, version, &peer, buffer, status); |
| 3265 | break; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3266 | default: |
| 3267 | gsn->unknown++; |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 3268 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, status, |
| 3269 | "Unknown GTP message type received: %u\n", |
| 3270 | pheader->type); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3271 | break; |
| 3272 | } |
| 3273 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3274 | } |
| 3275 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3276 | int gtp_decaps1u(struct gsn_t *gsn) |
| 3277 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3278 | unsigned char buffer[PACKET_MAX]; |
| 3279 | struct sockaddr_in peer; |
Harald Welte | d88e11d | 2011-11-02 13:09:43 +0100 | [diff] [blame] | 3280 | socklen_t peerlen; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3281 | int status; |
| 3282 | struct gtp1_header_short *pheader; |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 3283 | uint8_t version; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3284 | int fd = gsn->fd1u; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3285 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3286 | /* TODO: Need strategy of userspace buffering and blocking */ |
| 3287 | /* Currently read is non-blocking and send is blocking. */ |
| 3288 | /* This means that the program have to wait for busy send calls... */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3289 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3290 | while (1) { /* Loop until no more to read */ |
| 3291 | if (fcntl(gsn->fd1u, F_SETFL, O_NONBLOCK)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3292 | LOGP(DLGTP, LOGL_ERROR, "fnctl()\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3293 | return -1; |
| 3294 | } |
| 3295 | peerlen = sizeof(peer); |
| 3296 | if ((status = |
| 3297 | recvfrom(gsn->fd1u, buffer, sizeof(buffer), 0, |
| 3298 | (struct sockaddr *)&peer, &peerlen)) < 0) { |
| 3299 | if (errno == EAGAIN) |
| 3300 | return 0; |
| 3301 | gsn->err_readfrom++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3302 | LOGP(DLGTP, LOGL_ERROR, |
Alexander Huemer | db852a1 | 2015-11-06 20:59:01 +0100 | [diff] [blame] | 3303 | "recvfrom(fd1u=%d, buffer=%lx, len=%zu) failed: status = %d error = %s\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3304 | gsn->fd1u, (unsigned long)buffer, |
| 3305 | sizeof(buffer), status, |
| 3306 | status ? strerror(errno) : "No error"); |
| 3307 | return -1; |
| 3308 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3309 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3310 | /* Need at least 1 byte in order to check version */ |
| 3311 | if (status < (1)) { |
| 3312 | gsn->empty++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3313 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
| 3314 | status, "Discarding packet - too small\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3315 | continue; |
| 3316 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3317 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3318 | pheader = (struct gtp1_header_short *)(buffer); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3319 | |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 3320 | version = GTPHDR_F_GET_VER(pheader->flags); |
| 3321 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3322 | /* Version must be no larger than GTP 1 */ |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 3323 | if (version > 1) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3324 | gsn->unsup++; |
Pau Espin Pedrol | a884a95 | 2018-01-25 17:28:11 +0100 | [diff] [blame] | 3325 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, status, |
| 3326 | "Unsupported GTP version %"PRIu8"\n", version); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3327 | gtp_unsup_req(gsn, 1, &peer, gsn->fd1c, buffer, status); /*29.60: 11.1.1 */ |
| 3328 | continue; |
| 3329 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3330 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3331 | /* Version must be at least GTP 1 */ |
| 3332 | /* 29.060 is somewhat unclear on this issue. On gsn->fd1c we expect only */ |
| 3333 | /* GTP 1 messages. If GTP 0 message is received we silently discard */ |
| 3334 | /* the message */ |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 3335 | if (version < 1) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3336 | gsn->unsup++; |
Pau Espin Pedrol | a884a95 | 2018-01-25 17:28:11 +0100 | [diff] [blame] | 3337 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, status, |
| 3338 | "Unsupported GTP version %"PRIu8"\n", version); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3339 | continue; |
| 3340 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 3341 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3342 | /* Check packet flag field (allow both with and without sequence number) */ |
| 3343 | if (((pheader->flags & 0xf5) != 0x30)) { |
| 3344 | gsn->unsup++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3345 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | d37b80a | 2016-12-15 18:33:15 +0100 | [diff] [blame] | 3346 | status, "Unsupported packet flags 0x%02x\n", pheader->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3347 | continue; |
| 3348 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 3349 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3350 | /* Check length of packet */ |
| 3351 | if (status < GTP1_HEADER_SIZE_SHORT) { |
| 3352 | gsn->tooshort++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3353 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
| 3354 | status, "GTP packet too short\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3355 | continue; /* Silently discard 29.60: 11.1.2 */ |
| 3356 | } |
| 3357 | |
| 3358 | /* Check packet length field versus length of packet */ |
| 3359 | if (status != |
| 3360 | (ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT)) { |
| 3361 | gsn->tooshort++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3362 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3363 | status, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3364 | "GTP packet length field does not match actual length\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3365 | continue; /* Silently discard */ |
| 3366 | } |
| 3367 | |
| 3368 | /* Check for extension headers */ |
| 3369 | /* TODO: We really should cycle through the headers and determine */ |
| 3370 | /* if any have the comprehension required flag set */ |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 3371 | if (((pheader->flags & GTP1HDR_F_EXT) != 0x00)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3372 | gsn->unsup++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3373 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
| 3374 | status, "Unsupported extension header\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3375 | gtp_extheader_req(gsn, version, &peer, fd, buffer, |
| 3376 | status); |
| 3377 | |
| 3378 | continue; |
| 3379 | } |
| 3380 | |
| 3381 | switch (pheader->type) { |
| 3382 | case GTP_ECHO_REQ: |
| 3383 | gtp_echo_ind(gsn, version, &peer, fd, buffer, status); |
| 3384 | break; |
| 3385 | case GTP_ECHO_RSP: |
| 3386 | gtp_echo_conf(gsn, version, &peer, buffer, status); |
| 3387 | break; |
| 3388 | case GTP_SUPP_EXT_HEADER: |
| 3389 | gtp_extheader_ind(gsn, &peer, buffer, status); |
| 3390 | break; |
| 3391 | case GTP_ERROR: |
| 3392 | gtp_error_ind_conf(gsn, version, &peer, buffer, status); |
| 3393 | break; |
| 3394 | /* Supported header extensions */ |
| 3395 | case GTP_GPDU: |
| 3396 | gtp_gpdu_ind(gsn, version, &peer, fd, buffer, status); |
| 3397 | break; |
| 3398 | default: |
| 3399 | gsn->unknown++; |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 3400 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, status, |
| 3401 | "Unknown GTP message type received: %u\n", |
| 3402 | pheader->type); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3403 | break; |
| 3404 | } |
| 3405 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3408 | int gtp_data_req(struct gsn_t *gsn, struct pdp_t *pdp, void *pack, unsigned len) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3409 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3410 | union gtp_packet packet; |
| 3411 | struct sockaddr_in addr; |
Harald Welte | 471e349 | 2017-09-24 16:12:39 +0800 | [diff] [blame] | 3412 | struct msghdr msgh; |
| 3413 | struct iovec iov[2]; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3414 | int fd; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3415 | |
Harald Welte | 471e349 | 2017-09-24 16:12:39 +0800 | [diff] [blame] | 3416 | /* prepare destination address */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3417 | memset(&addr, 0, sizeof(addr)); |
| 3418 | addr.sin_family = AF_INET; |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 3419 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3420 | addr.sin_len = sizeof(addr); |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 3421 | #endif |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3422 | memcpy(&addr.sin_addr, pdp->gsnru.v, pdp->gsnru.l); /* TODO range check */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3423 | |
Harald Welte | 471e349 | 2017-09-24 16:12:39 +0800 | [diff] [blame] | 3424 | /* prepare msghdr */ |
| 3425 | memset(&msgh, 0, sizeof(msgh)); |
| 3426 | msgh.msg_name = &addr; |
| 3427 | msgh.msg_namelen = sizeof(addr); |
| 3428 | msgh.msg_iov = iov; |
| 3429 | msgh.msg_iovlen = ARRAY_SIZE(iov); |
| 3430 | |
| 3431 | /* prepare iovectors */ |
| 3432 | iov[0].iov_base = &packet; |
| 3433 | /* iov[0].iov_len is not known here yet */ |
| 3434 | iov[1].iov_base = pack; |
| 3435 | iov[1].iov_len = len; |
| 3436 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3437 | if (pdp->version == 0) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3438 | |
Harald Welte | 471e349 | 2017-09-24 16:12:39 +0800 | [diff] [blame] | 3439 | iov[0].iov_len = GTP0_HEADER_SIZE; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3440 | addr.sin_port = htons(GTP0_PORT); |
| 3441 | fd = gsn->fd0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3442 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3443 | get_default_gtp(0, GTP_GPDU, &packet); |
| 3444 | packet.gtp0.h.length = hton16(len); |
Harald Welte | 3c1cce2 | 2017-09-24 16:40:12 +0800 | [diff] [blame] | 3445 | if (pdp->tx_gpdu_seq) |
| 3446 | packet.gtp0.h.seq = hton16(pdp->gtpsntx++); |
| 3447 | else |
| 3448 | packet.gtp0.h.seq = 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3449 | packet.gtp0.h.flow = hton16(pdp->flru); |
Pablo Neira Ayuso | 746b944 | 2014-03-24 17:58:27 +0100 | [diff] [blame] | 3450 | packet.gtp0.h.tid = htobe64(pdp_gettid(pdp->imsi, pdp->nsapi)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3451 | } else if (pdp->version == 1) { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3452 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3453 | addr.sin_port = htons(GTP1U_PORT); |
| 3454 | fd = gsn->fd1u; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 3455 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3456 | get_default_gtp(1, GTP_GPDU, &packet); |
Harald Welte | 3c1cce2 | 2017-09-24 16:40:12 +0800 | [diff] [blame] | 3457 | if (pdp->tx_gpdu_seq) { |
| 3458 | packet.gtp1l.h.seq = hton16(pdp->gtpsntx++); |
| 3459 | packet.gtp1l.h.length = hton16(len - GTP1_HEADER_SIZE_SHORT + |
| 3460 | GTP1_HEADER_SIZE_LONG); |
| 3461 | packet.gtp1l.h.tei = hton32(pdp->teid_gn); |
| 3462 | iov[0].iov_len = GTP1_HEADER_SIZE_LONG; |
| 3463 | } else { |
| 3464 | packet.gtp1s.h.flags &= ~GTP1HDR_F_SEQ; |
| 3465 | packet.gtp1s.h.length = hton16(len); |
| 3466 | packet.gtp1s.h.tei = hton32(pdp->teid_gn); |
| 3467 | iov[0].iov_len = GTP1_HEADER_SIZE_SHORT; |
| 3468 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3469 | } else { |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 3470 | LOGP(DLGTP, LOGL_ERROR, "Unknown version: %d\n", pdp->version); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3471 | return EOF; |
| 3472 | } |
| 3473 | |
| 3474 | if (fcntl(fd, F_SETFL, 0)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3475 | LOGP(DLGTP, LOGL_ERROR, "fnctl()\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3476 | return -1; |
| 3477 | } |
| 3478 | |
Harald Welte | 471e349 | 2017-09-24 16:12:39 +0800 | [diff] [blame] | 3479 | if (sendmsg(fd, &msgh, 0) < 0) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3480 | gsn->err_sendto++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3481 | LOGP(DLGTP, LOGL_ERROR, |
Harald Welte | 471e349 | 2017-09-24 16:12:39 +0800 | [diff] [blame] | 3482 | "sendmsg(fd=%d, msg=%lx, len=%d) failed: Error = %s\n", fd, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3483 | (unsigned long)&packet, GTP0_HEADER_SIZE + len, |
| 3484 | strerror(errno)); |
| 3485 | return EOF; |
| 3486 | } |
| 3487 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3490 | /* *********************************************************** |
| 3491 | * Conversion functions |
| 3492 | *************************************************************/ |
| 3493 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3494 | /* *********************************************************** |
| 3495 | * IP address conversion functions |
| 3496 | * There exist several types of address representations: |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 3497 | * - eua: End User Address. (29.060, 7.7.27, message type 128) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3498 | * Used for signalling address to mobile station. Supports IPv4 |
| 3499 | * IPv6 x.25 etc. etc. |
| 3500 | * - gsna: GSN Address. (29.060, 7.7.32, message type 133): IP address |
| 3501 | * of GSN. If length is 4 it is IPv4. If length is 16 it is IPv6. |
| 3502 | * - in_addr: IPv4 address struct. |
| 3503 | * - sockaddr_in: Socket API representation of IP address and |
| 3504 | * port number. |
| 3505 | *************************************************************/ |
| 3506 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3507 | int ipv42eua(struct ul66_t *eua, struct in_addr *src) |
| 3508 | { |
Harald Welte | cee7546 | 2017-09-24 17:45:05 +0800 | [diff] [blame] | 3509 | eua->v[0] = PDP_EUA_ORG_IETF; |
| 3510 | eua->v[1] = PDP_EUA_TYPE_v4; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3511 | if (src) { |
| 3512 | eua->l = 6; |
| 3513 | memcpy(&eua->v[2], src, 4); |
| 3514 | } else { |
| 3515 | eua->l = 2; |
| 3516 | } |
| 3517 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3520 | int eua2ipv4(struct in_addr *dst, struct ul66_t *eua) |
| 3521 | { |
Harald Welte | cee7546 | 2017-09-24 17:45:05 +0800 | [diff] [blame] | 3522 | if ((eua->l != 6) || (eua->v[0] != PDP_EUA_ORG_IETF) || (eua->v[1] != PDP_EUA_TYPE_v4)) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3523 | return -1; /* Not IPv4 address */ |
| 3524 | memcpy(dst, &eua->v[2], 4); |
| 3525 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3528 | int gsna2in_addr(struct in_addr *dst, struct ul16_t *gsna) |
| 3529 | { |
| 3530 | memset(dst, 0, sizeof(struct in_addr)); |
| 3531 | if (gsna->l != 4) |
| 3532 | return EOF; /* Return if not IPv4 */ |
| 3533 | memcpy(dst, gsna->v, gsna->l); |
| 3534 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3535 | } |
| 3536 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3537 | int in_addr2gsna(struct ul16_t *gsna, struct in_addr *src) |
| 3538 | { |
| 3539 | memset(gsna, 0, sizeof(struct ul16_t)); |
| 3540 | gsna->l = 4; |
| 3541 | memcpy(gsna->v, src, gsna->l); |
| 3542 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3543 | } |
Harald Welte | b10ee08 | 2017-08-12 19:29:16 +0200 | [diff] [blame] | 3544 | |
| 3545 | /* TS 29.060 has yet again a different encoding for IMSIs than |
| 3546 | * what we have in other places, so we cannot use the gsm48 |
| 3547 | * decoding functions. Also, libgtp uses an uint64_t in |
| 3548 | * _network byte order_ to contain BCD digits ?!? */ |
| 3549 | const char *imsi_gtp2str(const uint64_t *imsi) |
| 3550 | { |
Harald Welte | a06120d | 2017-11-06 03:12:54 +0900 | [diff] [blame] | 3551 | static char buf[sizeof(*imsi)*2+1]; |
Harald Welte | b10ee08 | 2017-08-12 19:29:16 +0200 | [diff] [blame] | 3552 | const uint8_t *imsi8 = (const uint8_t *) imsi; |
| 3553 | unsigned int i, j = 0; |
| 3554 | |
| 3555 | for (i = 0; i < sizeof(*imsi); i++) { |
| 3556 | uint8_t nibble; |
| 3557 | |
| 3558 | nibble = imsi8[i] & 0xf; |
| 3559 | if (nibble == 0xf) |
| 3560 | break; |
| 3561 | buf[j++] = osmo_bcd2char(nibble); |
| 3562 | |
| 3563 | nibble = imsi8[i] >> 4; |
| 3564 | if (nibble == 0xf) |
| 3565 | break; |
| 3566 | buf[j++] = osmo_bcd2char(nibble); |
| 3567 | } |
| 3568 | |
| 3569 | buf[j++] = '\0'; |
| 3570 | return buf; |
| 3571 | } |
Keith | cbc07bd | 2020-10-10 12:17:26 +0200 | [diff] [blame] | 3572 | |
Keith | fb2a729 | 2020-10-12 15:32:07 +0200 | [diff] [blame] | 3573 | /* Generate the GTP IMSI IE according to 09.60 Section 7.9.2 */ |
| 3574 | uint64_t gtp_imsi_str2gtp(const char *str) |
Keith | cbc07bd | 2020-10-10 12:17:26 +0200 | [diff] [blame] | 3575 | { |
Keith | fb2a729 | 2020-10-12 15:32:07 +0200 | [diff] [blame] | 3576 | uint64_t imsi64 = 0; |
| 3577 | unsigned int n; |
| 3578 | unsigned int imsi_len = strlen(str); |
Keith | cbc07bd | 2020-10-10 12:17:26 +0200 | [diff] [blame] | 3579 | |
Keith | fb2a729 | 2020-10-12 15:32:07 +0200 | [diff] [blame] | 3580 | if (imsi_len > 16) { |
| 3581 | LOGP(DLGTP, LOGL_NOTICE, "IMSI length > 16 not supported!\n"); |
| 3582 | return 0; |
| 3583 | } |
| 3584 | |
| 3585 | for (n = 0; n < 16; n++) { |
| 3586 | uint64_t val; |
| 3587 | if (n < imsi_len) |
| 3588 | val = (str[n]-'0') & 0xf; |
| 3589 | else |
| 3590 | val = 0xf; |
| 3591 | imsi64 |= (val << (n*4)); |
| 3592 | } |
| 3593 | return imsi64; |
| 3594 | } |