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