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