Pau Espin Pedrol | 7c4de07 | 2017-12-14 13:59:02 +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 | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 4 | * Copyright (C) 2017 Harald Welte <laforge@gnumonks.org> |
Pau Espin Pedrol | 7c4de07 | 2017-12-14 13:59:02 +0100 | [diff] [blame] | 5 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 6 | * The contents of this file may be used under the terms of the GNU |
| 7 | * General Public License Version 2, provided that the above copyright |
| 8 | * notice and this permission notice is included in all copies or |
| 9 | * substantial portions of the software. |
Pau Espin Pedrol | 7c4de07 | 2017-12-14 13:59:02 +0100 | [diff] [blame] | 10 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * sgsnemu.c |
| 15 | * |
| 16 | */ |
| 17 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 18 | #ifdef __linux__ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 19 | #define _GNU_SOURCE 1 /* strdup() prototype, broken arpa/inet.h */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 20 | #endif |
| 21 | |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 22 | #include <osmocom/core/application.h> |
Pau Espin Pedrol | 042a445 | 2018-04-17 14:31:42 +0200 | [diff] [blame] | 23 | #include <osmocom/core/msgb.h> |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 24 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 25 | #include <ctype.h> |
| 26 | #include <netdb.h> |
| 27 | #include <signal.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <netinet/in.h> |
Harald Welte | 63ebccd | 2017-08-02 21:10:09 +0200 | [diff] [blame] | 34 | #include <netinet/ip.h> |
Harald Welte | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 35 | #include <netinet/ip6.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 36 | #include <arpa/inet.h> |
| 37 | #include <sys/wait.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <unistd.h> |
| 40 | #include <sys/socket.h> |
| 41 | #include <sys/ioctl.h> |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 42 | #include <sys/time.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 43 | #include <net/if.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 44 | #include <errno.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 45 | #include <sys/socket.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 46 | #include <resolv.h> |
| 47 | #include <time.h> |
| 48 | |
jjako | ff9985c | 2004-01-16 11:05:22 +0000 | [diff] [blame] | 49 | #include "config.h" |
Emmanuel Bretelle | 2a10368 | 2010-09-07 17:01:20 +0200 | [diff] [blame] | 50 | #include "../lib/tun.h" |
| 51 | #include "../lib/ippool.h" |
| 52 | #include "../lib/syserr.h" |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 53 | #include "../gtp/pdp.h" |
| 54 | #include "../gtp/gtp.h" |
| 55 | #include "cmdline.h" |
| 56 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 57 | #define IPADDRLEN 256 /* Character length of addresses */ |
| 58 | #define MAXCONTEXTS 1024 /* Max number of allowed contexts */ |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 59 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 60 | /* HASH tables for IP address allocation */ |
| 61 | struct iphash_t { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 62 | uint8_t inuse; /* 0=free. 1=used by somebody */ |
| 63 | struct iphash_t *ipnext; |
| 64 | struct pdp_t *pdp; |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 65 | struct in46_addr addr; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 66 | }; |
| 67 | struct iphash_t iparr[MAXCONTEXTS]; |
| 68 | struct iphash_t *iphash[MAXCONTEXTS]; |
| 69 | |
| 70 | /* State variable used for ping */ |
| 71 | /* 0: Idle */ |
| 72 | /* 1: Wait_connect */ |
| 73 | /* 2: Connected */ |
jjako | 7b8fad4 | 2003-07-07 14:37:42 +0000 | [diff] [blame] | 74 | /* 3: Done */ |
| 75 | /* 4: Wait_disconnect */ |
| 76 | /* 5: Disconnected */ |
bjovan | a8f71eb | 2017-02-24 17:39:20 +0100 | [diff] [blame] | 77 | volatile sig_atomic_t state = 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 78 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 79 | struct gsn_t *gsn = NULL; /* GSN instance */ |
| 80 | struct tun_t *tun = NULL; /* TUN instance */ |
| 81 | int maxfd = 0; /* For select() */ |
| 82 | int echoversion = 1; /* First try this version */ |
Pau Espin Pedrol | 042a445 | 2018-04-17 14:31:42 +0200 | [diff] [blame] | 83 | void *tall_sgsnemu_ctx; /* root talloc ctx */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 84 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 85 | /* Struct with local versions of gengetopt options */ |
| 86 | struct { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 87 | int debug; /* Print debug messages */ |
| 88 | int createif; /* Create local network interface */ |
Harald Welte | 73abc38 | 2017-10-10 08:50:11 +0800 | [diff] [blame] | 89 | char *tun_dev_name; |
Pau Espin Pedrol | f5e40b7 | 2017-12-14 14:01:23 +0100 | [diff] [blame] | 90 | struct in46_addr netaddr, destaddr, net; /* Network interface */ |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 91 | size_t prefixlen; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 92 | char *ipup, *ipdown; /* Filename of scripts */ |
| 93 | int defaultroute; /* Set up default route */ |
| 94 | struct in_addr pinghost; /* Remote ping host */ |
| 95 | int pingrate; |
| 96 | int pingsize; |
| 97 | int pingcount; |
| 98 | int pingquiet; |
| 99 | struct in_addr listen; |
| 100 | struct in_addr remote; |
| 101 | struct in_addr dns; |
| 102 | int contexts; /* Number of contexts to create */ |
| 103 | int timelimit; /* Number of seconds to be connected */ |
| 104 | char *statedir; |
| 105 | uint64_t imsi; |
| 106 | uint8_t nsapi; |
| 107 | int gtpversion; |
| 108 | struct ul255_t pco; |
| 109 | struct ul255_t qos; |
| 110 | uint16_t cch; |
| 111 | struct ul255_t apn; |
| 112 | uint8_t selmode; |
| 113 | struct ul255_t rattype; |
| 114 | int rattype_given; |
| 115 | struct ul255_t userloc; |
| 116 | int userloc_given; |
| 117 | struct ul255_t rai; |
| 118 | int rai_given; |
| 119 | struct ul255_t mstz; |
| 120 | int mstz_given; |
| 121 | struct ul255_t imeisv; |
| 122 | int imeisv_given; |
| 123 | struct ul16_t msisdn; |
| 124 | int norecovery_given; |
Harald Welte | fbb9c7f | 2017-09-24 11:50:20 +0800 | [diff] [blame] | 125 | int tx_gpdu_seq; |
Harald Welte | 840a8e9 | 2017-09-24 18:12:40 +0800 | [diff] [blame] | 126 | uint8_t pdp_type; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 127 | } options; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 128 | |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 129 | /* Definitions to use for PING. Most of the ping code was derived from */ |
| 130 | /* the original ping program by Mike Muuss */ |
| 131 | |
| 132 | /* IP header and ICMP echo header */ |
| 133 | #define CREATEPING_MAX 2048 |
| 134 | #define CREATEPING_IP 20 |
| 135 | #define CREATEPING_ICMP 8 |
| 136 | |
| 137 | struct ip_ping { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 138 | uint8_t ipver; /* Type and header length */ |
| 139 | uint8_t tos; /* Type of Service */ |
| 140 | uint16_t length; /* Total length */ |
| 141 | uint16_t fragid; /* Identifier */ |
| 142 | uint16_t offset; /* Flags and fragment offset */ |
| 143 | uint8_t ttl; /* Time to live */ |
| 144 | uint8_t protocol; /* Protocol */ |
| 145 | uint16_t ipcheck; /* Header checksum */ |
| 146 | uint32_t src; /* Source address */ |
| 147 | uint32_t dst; /* Destination */ |
| 148 | uint8_t type; /* Type and header length */ |
| 149 | uint8_t code; /* Code */ |
| 150 | uint16_t checksum; /* Header checksum */ |
| 151 | uint16_t ident; /* Identifier */ |
| 152 | uint16_t seq; /* Sequence number */ |
| 153 | uint8_t data[CREATEPING_MAX]; /* Data */ |
| 154 | } __attribute__ ((packed)); |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 155 | |
| 156 | /* Statistical values for ping */ |
| 157 | int nreceived = 0; |
| 158 | int ntreceived = 0; |
| 159 | int ntransmitted = 0; |
| 160 | int tmin = 999999999; |
| 161 | int tmax = 0; |
| 162 | int tsum = 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 163 | int pingseq = 0; /* Ping sequence counter */ |
jjako | afb2a97 | 2003-01-29 21:04:13 +0000 | [diff] [blame] | 164 | struct timeval firstping; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 165 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 166 | static void signal_handler(int signo) |
bjovan | a8f71eb | 2017-02-24 17:39:20 +0100 | [diff] [blame] | 167 | { |
| 168 | if (state == 2) |
| 169 | state = 3; /* Tell main loop to finish. */ |
| 170 | } |
| 171 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 172 | static int ipset(struct iphash_t *ipaddr, struct in46_addr *addr) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 173 | { |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 174 | int hash = ippool_hash(addr) % MAXCONTEXTS; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 175 | struct iphash_t *h; |
| 176 | struct iphash_t *prev = NULL; |
| 177 | ipaddr->ipnext = NULL; |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 178 | ipaddr->addr = *addr; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 179 | for (h = iphash[hash]; h; h = h->ipnext) |
| 180 | prev = h; |
| 181 | if (!prev) |
| 182 | iphash[hash] = ipaddr; |
| 183 | else |
| 184 | prev->ipnext = ipaddr; |
| 185 | return 0; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 188 | static int ipdel(struct iphash_t *ipaddr) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 189 | { |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 190 | int hash = ippool_hash(&ipaddr->addr) % MAXCONTEXTS; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 191 | struct iphash_t *h; |
| 192 | struct iphash_t *prev = NULL; |
| 193 | for (h = iphash[hash]; h; h = h->ipnext) { |
| 194 | if (h == ipaddr) { |
| 195 | if (!prev) |
| 196 | iphash[hash] = h->ipnext; |
| 197 | else |
| 198 | prev->ipnext = h->ipnext; |
| 199 | return 0; |
| 200 | } |
| 201 | prev = h; |
| 202 | } |
| 203 | return EOF; /* End of linked list and not found */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 206 | static int ipget(struct iphash_t **ipaddr, struct in46_addr *addr) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 207 | { |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 208 | int hash = ippool_hash(addr) % MAXCONTEXTS; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 209 | struct iphash_t *h; |
| 210 | for (h = iphash[hash]; h; h = h->ipnext) { |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 211 | if (in46a_equal(&h->addr, addr)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 212 | *ipaddr = h; |
| 213 | return 0; |
| 214 | } |
| 215 | } |
| 216 | return EOF; /* End of linked list and not found */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 217 | } |
| 218 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 219 | /* Used to write process ID to file. Assume someone else will delete */ |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 220 | static void log_pid(char *pidfile) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 221 | { |
| 222 | FILE *file; |
| 223 | mode_t oldmask; |
| 224 | |
| 225 | oldmask = umask(022); |
| 226 | file = fopen(pidfile, "w"); |
| 227 | umask(oldmask); |
| 228 | if (!file) |
| 229 | return; |
| 230 | fprintf(file, "%d\n", (int)getpid()); |
| 231 | fclose(file); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 234 | static int process_options(int argc, char **argv) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 235 | { |
| 236 | /* gengeopt declarations */ |
| 237 | struct gengetopt_args_info args_info; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 238 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 239 | struct hostent *host; |
| 240 | unsigned int n; |
| 241 | uint16_t i; |
| 242 | uint8_t a; |
| 243 | uint8_t b; |
| 244 | char *tmp; |
| 245 | char *pch; |
| 246 | char *type; |
| 247 | char *mcc; |
| 248 | char *mnc; |
Andreas Schultz | 10abfba | 2015-11-13 15:57:37 +0100 | [diff] [blame] | 249 | char *tok, *apn; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 250 | char *lac; |
| 251 | int lac_d; |
| 252 | char *rest; |
| 253 | char *userloc_el[] = { "TYPE", "MCC", "MNC", "LAC", "REST" }; |
| 254 | char *rai_el[] = { "MCC", "MNC", "LAC", "RAC" }; |
| 255 | char *mstz_el[] = { "SIGN", "QUARTERS", "DST" }; |
| 256 | int sign; |
| 257 | int nbquarters; |
| 258 | int DST; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 259 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 260 | if (cmdline_parser(argc, argv, &args_info) != 0) |
| 261 | return -1; |
| 262 | if (args_info.debug_flag) { |
| 263 | if (args_info.remote_arg) |
| 264 | printf("remote: %s\n", args_info.remote_arg); |
| 265 | if (args_info.listen_arg) |
| 266 | printf("listen: %s\n", args_info.listen_arg); |
| 267 | if (args_info.conf_arg) |
| 268 | printf("conf: %s\n", args_info.conf_arg); |
| 269 | printf("debug: %d\n", args_info.debug_flag); |
| 270 | if (args_info.imsi_arg) |
| 271 | printf("imsi: %s\n", args_info.imsi_arg); |
| 272 | printf("qos: %#08x\n", args_info.qos_arg); |
| 273 | printf("qose1: %#0.16llx\n", args_info.qose1_arg); |
| 274 | printf("qose2: %#04x\n", args_info.qose2_arg); |
| 275 | printf("qose3: %#06x\n", args_info.qose3_arg); |
| 276 | printf("qose4: %#06x\n", args_info.qose4_arg); |
| 277 | printf("charging: %#04x\n", args_info.charging_arg); |
| 278 | if (args_info.apn_arg) |
| 279 | printf("apn: %s\n", args_info.apn_arg); |
| 280 | if (args_info.msisdn_arg) |
| 281 | printf("msisdn: %s\n", args_info.msisdn_arg); |
| 282 | if (args_info.uid_arg) |
| 283 | printf("uid: %s\n", args_info.uid_arg); |
| 284 | if (args_info.pwd_arg) |
| 285 | printf("pwd: %s\n", args_info.pwd_arg); |
| 286 | if (args_info.pidfile_arg) |
| 287 | printf("pidfile: %s\n", args_info.pidfile_arg); |
| 288 | if (args_info.statedir_arg) |
| 289 | printf("statedir: %s\n", args_info.statedir_arg); |
| 290 | if (args_info.dns_arg) |
| 291 | printf("dns: %s\n", args_info.dns_arg); |
| 292 | printf("contexts: %d\n", args_info.contexts_arg); |
| 293 | printf("timelimit: %d\n", args_info.timelimit_arg); |
| 294 | printf("createif: %d\n", args_info.createif_flag); |
Harald Welte | 73abc38 | 2017-10-10 08:50:11 +0800 | [diff] [blame] | 295 | if (args_info.tun_device_arg) |
| 296 | printf("tun-device: %d\n", args_info.tun_device_arg); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 297 | if (args_info.ipup_arg) |
| 298 | printf("ipup: %s\n", args_info.ipup_arg); |
| 299 | if (args_info.ipdown_arg) |
| 300 | printf("ipdown: %s\n", args_info.ipdown_arg); |
| 301 | printf("defaultroute: %d\n", args_info.defaultroute_flag); |
| 302 | if (args_info.pinghost_arg) |
| 303 | printf("pinghost: %s\n", args_info.pinghost_arg); |
| 304 | printf("pingrate: %d\n", args_info.pingrate_arg); |
| 305 | printf("pingsize: %d\n", args_info.pingsize_arg); |
| 306 | printf("pingcount: %d\n", args_info.pingcount_arg); |
| 307 | printf("pingquiet: %d\n", args_info.pingquiet_flag); |
| 308 | printf("norecovery: %d\n", args_info.norecovery_flag); |
Harald Welte | fbb9c7f | 2017-09-24 11:50:20 +0800 | [diff] [blame] | 309 | printf("no-tx-gpdu-seq: %d\n", args_info.no_tx_gpdu_seq_flag); |
Yann BONNAMY | 11a398f | 2010-11-18 10:01:21 +0100 | [diff] [blame] | 310 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 311 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 312 | /* Try out our new parser */ |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 313 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 314 | if (args_info.conf_arg) { |
| 315 | if (cmdline_parser_configfile |
| 316 | (args_info.conf_arg, &args_info, 0, 0, 0) != 0) |
| 317 | return -1; |
| 318 | if (args_info.debug_flag) { |
| 319 | printf("cmdline_parser_configfile\n"); |
| 320 | if (args_info.remote_arg) |
| 321 | printf("remote: %s\n", args_info.remote_arg); |
| 322 | if (args_info.listen_arg) |
| 323 | printf("listen: %s\n", args_info.listen_arg); |
| 324 | if (args_info.conf_arg) |
| 325 | printf("conf: %s\n", args_info.conf_arg); |
| 326 | printf("debug: %d\n", args_info.debug_flag); |
| 327 | if (args_info.imsi_arg) |
| 328 | printf("imsi: %s\n", args_info.imsi_arg); |
| 329 | printf("qos: %#08x\n", args_info.qos_arg); |
| 330 | printf("qose1: %#0.16llx\n", args_info.qose1_arg); |
| 331 | printf("qose2: %#04x\n", args_info.qose2_arg); |
| 332 | printf("qose3: %#06x\n", args_info.qose3_arg); |
| 333 | printf("qose4: %#06x\n", args_info.qose4_arg); |
| 334 | printf("charging: %#04x\n", args_info.charging_arg); |
| 335 | if (args_info.apn_arg) |
| 336 | printf("apn: %s\n", args_info.apn_arg); |
| 337 | if (args_info.msisdn_arg) |
| 338 | printf("msisdn: %s\n", args_info.msisdn_arg); |
| 339 | if (args_info.uid_arg) |
| 340 | printf("uid: %s\n", args_info.uid_arg); |
| 341 | if (args_info.pwd_arg) |
| 342 | printf("pwd: %s\n", args_info.pwd_arg); |
| 343 | if (args_info.pidfile_arg) |
| 344 | printf("pidfile: %s\n", args_info.pidfile_arg); |
| 345 | if (args_info.statedir_arg) |
| 346 | printf("statedir: %s\n", |
| 347 | args_info.statedir_arg); |
| 348 | if (args_info.dns_arg) |
| 349 | printf("dns: %s\n", args_info.dns_arg); |
| 350 | printf("contexts: %d\n", args_info.contexts_arg); |
| 351 | printf("timelimit: %d\n", args_info.timelimit_arg); |
| 352 | printf("createif: %d\n", args_info.createif_flag); |
Harald Welte | 73abc38 | 2017-10-10 08:50:11 +0800 | [diff] [blame] | 353 | if (args_info.tun_device_arg) |
Harald Welte | 9c33210 | 2017-11-06 02:44:42 +0900 | [diff] [blame] | 354 | printf("tun-device: %s\n", args_info.tun_device_arg); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 355 | if (args_info.ipup_arg) |
| 356 | printf("ipup: %s\n", args_info.ipup_arg); |
| 357 | if (args_info.ipdown_arg) |
| 358 | printf("ipdown: %s\n", args_info.ipdown_arg); |
| 359 | printf("defaultroute: %d\n", |
| 360 | args_info.defaultroute_flag); |
| 361 | if (args_info.pinghost_arg) |
| 362 | printf("pinghost: %s\n", |
| 363 | args_info.pinghost_arg); |
| 364 | printf("pingrate: %d\n", args_info.pingrate_arg); |
| 365 | printf("pingsize: %d\n", args_info.pingsize_arg); |
| 366 | printf("pingcount: %d\n", args_info.pingcount_arg); |
| 367 | printf("pingquiet: %d\n", args_info.pingquiet_flag); |
| 368 | printf("norecovery: %d\n", args_info.norecovery_flag); |
Harald Welte | fbb9c7f | 2017-09-24 11:50:20 +0800 | [diff] [blame] | 369 | printf("no-tx-gpdu-seq: %d\n", args_info.no_tx_gpdu_seq_flag); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 370 | } |
| 371 | } |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 372 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 373 | /* Handle each option */ |
jjako | 1a51df7 | 2004-07-20 08:30:21 +0000 | [diff] [blame] | 374 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 375 | /* foreground */ |
| 376 | /* If fg flag not given run as a daemon */ |
Pau Espin Pedrol | 7c4de07 | 2017-12-14 13:59:02 +0100 | [diff] [blame] | 377 | /* Do not allow sgsnemu to run as deamon |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 378 | if (!args_info.fg_flag) |
| 379 | { |
Pau Espin Pedrol | 7c4de07 | 2017-12-14 13:59:02 +0100 | [diff] [blame] | 380 | closelog(); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 381 | freopen("/dev/null", "w", stdout); |
| 382 | freopen("/dev/null", "w", stderr); |
| 383 | freopen("/dev/null", "r", stdin); |
| 384 | daemon(0, 0); |
| 385 | openlog(PACKAGE, LOG_PID, LOG_DAEMON); |
| 386 | } */ |
jjako | 1a51df7 | 2004-07-20 08:30:21 +0000 | [diff] [blame] | 387 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 388 | /* debug */ |
| 389 | options.debug = args_info.debug_flag; |
jjako | 1a51df7 | 2004-07-20 08:30:21 +0000 | [diff] [blame] | 390 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 391 | /* pidfile */ |
| 392 | /* This has to be done after we have our final pid */ |
| 393 | if (args_info.pidfile_arg) { |
| 394 | log_pid(args_info.pidfile_arg); |
| 395 | } |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 396 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 397 | /* dns */ |
| 398 | /* If no dns option is given use system default */ |
| 399 | /* Do hostname lookup to translate hostname to IP address */ |
| 400 | printf("\n"); |
| 401 | if (args_info.dns_arg) { |
| 402 | if (!(host = gethostbyname(args_info.dns_arg))) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 403 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 404 | "Invalid DNS address: %s!", args_info.dns_arg); |
| 405 | return -1; |
| 406 | } else { |
| 407 | memcpy(&options.dns.s_addr, host->h_addr, |
| 408 | host->h_length); |
| 409 | _res.nscount = 1; |
| 410 | _res.nsaddr_list[0].sin_addr = options.dns; |
| 411 | printf("Using DNS server: %s (%s)\n", |
| 412 | args_info.dns_arg, inet_ntoa(options.dns)); |
| 413 | } |
| 414 | } else { |
| 415 | options.dns.s_addr = 0; |
| 416 | printf("Using default DNS server\n"); |
| 417 | } |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 418 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 419 | /* listen */ |
| 420 | /* If no listen option is specified listen to any local port */ |
| 421 | /* Do hostname lookup to translate hostname to IP address */ |
| 422 | if (args_info.listen_arg) { |
| 423 | if (!(host = gethostbyname(args_info.listen_arg))) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 424 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 425 | "Invalid listening address: %s!", |
| 426 | args_info.listen_arg); |
| 427 | return -1; |
| 428 | } else { |
| 429 | memcpy(&options.listen.s_addr, host->h_addr, |
| 430 | host->h_length); |
| 431 | printf("Local IP address is: %s (%s)\n", |
| 432 | args_info.listen_arg, inet_ntoa(options.listen)); |
| 433 | } |
| 434 | } else { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 435 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 436 | "Listening address must be specified: %s!", |
| 437 | args_info.listen_arg); |
| 438 | return -1; |
| 439 | } |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 440 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 441 | /* remote */ |
| 442 | /* If no remote option is specified terminate */ |
| 443 | /* Do hostname lookup to translate hostname to IP address */ |
| 444 | if (args_info.remote_arg) { |
| 445 | if (!(host = gethostbyname(args_info.remote_arg))) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 446 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 447 | "Invalid remote address: %s!", |
| 448 | args_info.remote_arg); |
| 449 | return -1; |
| 450 | } else { |
| 451 | memcpy(&options.remote.s_addr, host->h_addr, |
| 452 | host->h_length); |
| 453 | printf("Remote IP address is: %s (%s)\n", |
| 454 | args_info.remote_arg, inet_ntoa(options.remote)); |
| 455 | } |
| 456 | } else { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 457 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 458 | "No remote address given!"); |
| 459 | return -1; |
| 460 | } |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 461 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 462 | /* imsi */ |
| 463 | if (strlen(args_info.imsi_arg) != 15) { |
| 464 | printf("Invalid IMSI\n"); |
| 465 | return -1; |
| 466 | } |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 467 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 468 | options.imsi = 0xf000000000000000ull; |
| 469 | options.imsi |= ((uint64_t) (args_info.imsi_arg[0] - 48)); |
| 470 | options.imsi |= ((uint64_t) (args_info.imsi_arg[1] - 48)) << 4; |
| 471 | options.imsi |= ((uint64_t) (args_info.imsi_arg[2] - 48)) << 8; |
| 472 | options.imsi |= ((uint64_t) (args_info.imsi_arg[3] - 48)) << 12; |
| 473 | options.imsi |= ((uint64_t) (args_info.imsi_arg[4] - 48)) << 16; |
| 474 | options.imsi |= ((uint64_t) (args_info.imsi_arg[5] - 48)) << 20; |
| 475 | options.imsi |= ((uint64_t) (args_info.imsi_arg[6] - 48)) << 24; |
| 476 | options.imsi |= ((uint64_t) (args_info.imsi_arg[7] - 48)) << 28; |
| 477 | options.imsi |= ((uint64_t) (args_info.imsi_arg[8] - 48)) << 32; |
| 478 | options.imsi |= ((uint64_t) (args_info.imsi_arg[9] - 48)) << 36; |
| 479 | options.imsi |= ((uint64_t) (args_info.imsi_arg[10] - 48)) << 40; |
| 480 | options.imsi |= ((uint64_t) (args_info.imsi_arg[11] - 48)) << 44; |
| 481 | options.imsi |= ((uint64_t) (args_info.imsi_arg[12] - 48)) << 48; |
| 482 | options.imsi |= ((uint64_t) (args_info.imsi_arg[13] - 48)) << 52; |
| 483 | options.imsi |= ((uint64_t) (args_info.imsi_arg[14] - 48)) << 56; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 484 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 485 | printf("IMSI is: %s (%#08llx)\n", |
| 486 | args_info.imsi_arg, options.imsi); |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 487 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 488 | /* nsapi */ |
| 489 | if ((args_info.nsapi_arg > 15) || (args_info.nsapi_arg < 0)) { |
| 490 | printf("Invalid NSAPI\n"); |
| 491 | return -1; |
| 492 | } |
| 493 | options.nsapi = args_info.nsapi_arg; |
| 494 | printf("Using NSAPI: %d\n", args_info.nsapi_arg); |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 495 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 496 | /* qos */ |
| 497 | options.qos.l = 4; |
| 498 | options.qos.v[3] = (args_info.qos_arg) & 0xff; |
| 499 | options.qos.v[2] = ((args_info.qos_arg) >> 8) & 0xff; |
| 500 | options.qos.v[1] = ((args_info.qos_arg) >> 16) & 0xff; |
| 501 | options.qos.v[0] = ((args_info.qos_arg) >> 24) & 0xff; |
| 502 | /* Extensions according to 3GPP TS 24.008 */ |
| 503 | if (args_info.qose1_given == 1) { |
| 504 | options.qos.l = 12; |
| 505 | options.qos.v[11] = (args_info.qose1_arg) & 0xff; |
| 506 | options.qos.v[10] = ((args_info.qose1_arg) >> 8) & 0xff; |
| 507 | options.qos.v[9] = ((args_info.qose1_arg) >> 16) & 0xff; |
| 508 | options.qos.v[8] = ((args_info.qose1_arg) >> 24) & 0xff; |
| 509 | options.qos.v[7] = ((args_info.qose1_arg) >> 32) & 0xff; |
| 510 | options.qos.v[6] = ((args_info.qose1_arg) >> 40) & 0xff; |
| 511 | options.qos.v[5] = ((args_info.qose1_arg) >> 48) & 0xff; |
| 512 | options.qos.v[4] = ((args_info.qose1_arg) >> 56) & 0xff; |
| 513 | if (args_info.qose2_given == 1) { |
| 514 | options.qos.l = 13; |
| 515 | options.qos.v[12] = (args_info.qose2_arg) & 0xff; |
| 516 | if (args_info.qose3_given == 1) { |
| 517 | options.qos.l = 15; |
| 518 | options.qos.v[14] = |
| 519 | (args_info.qose3_arg) & 0xff; |
| 520 | options.qos.v[13] = |
| 521 | ((args_info.qose3_arg) >> 8) & 0xff; |
| 522 | if (args_info.qose4_given == 1) { |
| 523 | options.qos.l = 17; |
| 524 | options.qos.v[16] = |
| 525 | (args_info.qose4_arg) & 0xff; |
| 526 | options.qos.v[15] = |
| 527 | ((args_info.qose4_arg) >> 8) & 0xff; |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 532 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 533 | /* charging */ |
| 534 | options.cch = args_info.charging_arg; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 535 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 536 | /* contexts */ |
| 537 | if (args_info.contexts_arg > MAXCONTEXTS) { |
| 538 | printf("Contexts has to be less than %d\n", MAXCONTEXTS); |
| 539 | return -1; |
| 540 | } |
| 541 | options.contexts = args_info.contexts_arg; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 542 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 543 | /* Timelimit */ |
| 544 | options.timelimit = args_info.timelimit_arg; |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 545 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 546 | /* gtpversion */ |
| 547 | if ((args_info.gtpversion_arg > 1) || (args_info.gtpversion_arg < 0)) { |
| 548 | printf("Invalid GTP version\n"); |
| 549 | return -1; |
| 550 | } |
| 551 | options.gtpversion = args_info.gtpversion_arg; |
| 552 | printf("Using GTP version: %d\n", args_info.gtpversion_arg); |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 553 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 554 | /* apn */ |
| 555 | if (strlen(args_info.apn_arg) > (sizeof(options.apn.v) - 1)) { |
| 556 | printf("Invalid APN\n"); |
| 557 | return -1; |
| 558 | } |
Andreas Schultz | 10abfba | 2015-11-13 15:57:37 +0100 | [diff] [blame] | 559 | options.apn.l = strlen(args_info.apn_arg) + 1; |
| 560 | |
| 561 | apn = (char *)options.apn.v; |
| 562 | for (tok = strtok(args_info.apn_arg, "."); |
| 563 | tok != NULL; |
| 564 | tok = strtok(NULL, ".")) { |
| 565 | size_t len = strlen(tok); |
| 566 | |
| 567 | *apn++ = (char)len; |
| 568 | strncpy(apn, tok, len); |
| 569 | apn += len; |
| 570 | } |
| 571 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 572 | printf("Using APN: %s\n", args_info.apn_arg); |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 573 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 574 | /* selmode */ |
| 575 | options.selmode = args_info.selmode_arg; |
| 576 | printf("Using selection mode: %d\n", args_info.selmode_arg); |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 577 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 578 | /* rattype */ |
| 579 | if (args_info.rattype_given == 1) { |
| 580 | options.rattype_given = 1; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame] | 581 | options.rattype.l = 1; |
| 582 | options.rattype.v[0] = args_info.rattype_arg; |
| 583 | printf("Using RAT Type: %d\n", args_info.rattype_arg); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 584 | } |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 585 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 586 | /* userloc */ |
| 587 | if (args_info.userloc_given == 1) { |
| 588 | printf("Using User Location Information: %s\n", |
| 589 | args_info.userloc_arg); |
| 590 | tmp = args_info.userloc_arg; |
| 591 | n = 0; |
| 592 | pch = strtok(tmp, "."); |
| 593 | while (pch != NULL) { |
| 594 | userloc_el[n] = pch; |
| 595 | pch = strtok(NULL, "."); |
| 596 | n++; |
| 597 | } |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 598 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 599 | options.userloc_given = 1; |
| 600 | options.userloc.l = 8; |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 601 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 602 | /* 3GPP Geographic Location Type t0 / t1 / t2 */ |
| 603 | type = userloc_el[0]; |
| 604 | printf("->type : %c\n", type[0]); |
| 605 | if ((strlen(type) != 1) || (!isdigit(type[0]))) { |
| 606 | printf("Invalid type \n"); |
| 607 | return -1; |
| 608 | } |
| 609 | /* options.userloc.v[0] = 0x00 */ |
| 610 | options.userloc.v[0] = type[0] - 48; |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 611 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 612 | /* MCC */ |
| 613 | mcc = userloc_el[1]; |
| 614 | printf("->mcc : %s\n", mcc); |
| 615 | if (strlen(mcc) != 3) { |
Ruben Undheim | cc077ae | 2016-05-24 23:31:47 +0200 | [diff] [blame] | 616 | printf("Invalid MCC length\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 617 | return -1; |
| 618 | } |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 619 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 620 | /* MNC */ |
| 621 | mnc = userloc_el[2]; |
| 622 | printf("->mnc : %s\n", mnc); |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 623 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 624 | /* octet 5 - MCC Digit 2 - MCC Digit 1 */ |
| 625 | /* options.userloc.v[1] = 0x52 */ |
| 626 | a = (uint8_t) (mcc[0] - 48); |
| 627 | b = (uint8_t) (mcc[1] - 48); |
| 628 | options.userloc.v[1] = 16 * b + a; |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 629 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 630 | /* octet 6 - MNC Digit 3 - MCC Digit 3 */ |
| 631 | /* options.userloc.v[2] = 0xf0 */ |
| 632 | a = (uint8_t) (mcc[2] - 48); |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 633 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 634 | if ((strlen(mnc) > 3) || (strlen(mnc) < 2)) { |
Ruben Undheim | cc077ae | 2016-05-24 23:31:47 +0200 | [diff] [blame] | 635 | printf("Invalid MNC length\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 636 | return -1; |
| 637 | } |
| 638 | if (strlen(mnc) == 2) { |
| 639 | b = 15; |
| 640 | } |
| 641 | if (strlen(mnc) == 3) { |
| 642 | b = (uint8_t) (mnc[2] - 48); |
| 643 | } |
| 644 | options.userloc.v[2] = 16 * b + a; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 645 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 646 | /* octet 7 - MNC Digit 2 - MNC Digit 1 */ |
| 647 | /* options.userloc.v[3] = 0x99 */ |
| 648 | a = (uint8_t) (mnc[0] - 48); |
| 649 | b = (uint8_t) (mnc[1] - 48); |
| 650 | options.userloc.v[3] = 16 * b + a; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 651 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 652 | /* LAC */ |
| 653 | lac = userloc_el[3]; |
| 654 | /*options.userloc.v[4] = 0x12 ; */ |
| 655 | /*options.userloc.v[5] = 0x10 ; */ |
| 656 | printf("->LAC: %s\n", lac); |
| 657 | lac_d = atoi(lac); |
| 658 | if (lac_d > 65535 || lac_d < 1) { |
| 659 | printf("Invalid LAC\n"); |
| 660 | return -1; |
| 661 | } |
| 662 | i = lac_d >> 8; |
| 663 | options.userloc.v[4] = i; /* octet 8 - LAC */ |
| 664 | options.userloc.v[5] = lac_d; /* octet 9 - LAC */ |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 665 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 666 | /* CI/SAC/RAC */ |
| 667 | rest = userloc_el[4]; |
| 668 | printf("->CI/SAC/RAC : %s\n", rest); |
| 669 | lac_d = atoi(rest); |
| 670 | if (lac_d > 65535 || lac_d < 1) { |
| 671 | printf("Invalid CI/SAC/RAC\n"); |
| 672 | return -1; |
| 673 | } |
| 674 | /*options.userloc.v[6] = 0x04 ; */ |
| 675 | /*options.userloc.v[7] = 0xb7 ; */ |
| 676 | i = lac_d >> 8; |
| 677 | options.userloc.v[6] = i; /* octet 10 - t0,CI / t1,SAC / t2,RAC */ |
| 678 | options.userloc.v[7] = lac_d; /* octet 11 - t0,CI / t1,SAC / t2,RAC */ |
| 679 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 680 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 681 | /* RAI */ |
| 682 | if (args_info.rai_given == 1) { |
| 683 | printf("Using RAI: %s\n", args_info.rai_arg); |
| 684 | tmp = args_info.rai_arg; |
| 685 | n = 0; |
| 686 | pch = strtok(tmp, "."); |
| 687 | while (pch != NULL) { |
| 688 | rai_el[n] = pch; |
| 689 | pch = strtok(NULL, "."); |
| 690 | n++; |
| 691 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 692 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 693 | options.rai_given = 1; |
| 694 | options.rai.l = 6; |
jjako | c6762cf | 2004-04-28 14:52:58 +0000 | [diff] [blame] | 695 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 696 | /* MCC */ |
| 697 | mcc = rai_el[0]; |
| 698 | printf("->mcc : %s\n", mcc); |
| 699 | if (strlen(mcc) != 3) { |
Ruben Undheim | cc077ae | 2016-05-24 23:31:47 +0200 | [diff] [blame] | 700 | printf("Invalid MCC length\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 701 | return -1; |
| 702 | } |
| 703 | |
| 704 | /* MNC */ |
| 705 | mnc = rai_el[1]; |
| 706 | printf("->mnc : %s\n", mnc); |
| 707 | |
| 708 | a = (uint8_t) (mcc[0] - 48); |
| 709 | b = (uint8_t) (mcc[1] - 48); |
| 710 | options.rai.v[0] = 16 * b + a; |
| 711 | |
| 712 | /* octet 3 - MNC Digit 3 - MCC Digit 3 */ |
| 713 | a = (uint8_t) (mcc[2] - 48); |
| 714 | |
| 715 | if ((strlen(mnc) > 3) || (strlen(mnc) < 2)) { |
Ruben Undheim | cc077ae | 2016-05-24 23:31:47 +0200 | [diff] [blame] | 716 | printf("Invalid MNC length\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 717 | return -1; |
| 718 | } |
| 719 | if (strlen(mnc) == 2) { |
| 720 | b = 15; |
| 721 | } |
| 722 | if (strlen(mnc) == 3) { |
| 723 | b = (uint8_t) (mnc[2] - 48); |
| 724 | } |
| 725 | options.rai.v[1] = 16 * b + a; |
| 726 | |
| 727 | /* octet 4 - MNC Digit 2 - MNC Digit 1 */ |
| 728 | a = (uint8_t) (mnc[0] - 48); |
| 729 | b = (uint8_t) (mnc[1] - 48); |
| 730 | options.rai.v[2] = 16 * b + a; |
| 731 | |
| 732 | /* LAC */ |
| 733 | lac = rai_el[2]; |
| 734 | printf("->LAC: %s\n", lac); |
| 735 | lac_d = atoi(lac); |
| 736 | if (lac_d > 65535 || lac_d < 1) { |
| 737 | printf("Invalid LAC\n"); |
| 738 | return -1; |
| 739 | } |
| 740 | i = lac_d >> 8; |
| 741 | options.rai.v[3] = i; /* octet 5 - LAC */ |
| 742 | options.rai.v[4] = lac_d; /* octet 6 - LAC */ |
| 743 | |
| 744 | /* RAC */ |
| 745 | rest = rai_el[3]; |
| 746 | printf("->RAC : %s\n", rest); |
| 747 | lac_d = atoi(rest); |
| 748 | if (lac_d > 255 || lac_d < 1) { |
| 749 | printf("Invalid RAC\n"); |
| 750 | return -1; |
| 751 | } |
| 752 | options.rai.v[5] = lac_d; /* octet 7 - RAC */ |
| 753 | } |
| 754 | |
| 755 | /* mstz */ |
| 756 | if (args_info.mstz_given == 1) { |
| 757 | options.mstz_given = 1; |
| 758 | options.mstz.l = 2; |
| 759 | |
| 760 | printf("Using MS Time Zone: %s\n", args_info.mstz_arg); |
| 761 | tmp = args_info.mstz_arg; |
| 762 | n = 0; |
| 763 | pch = strtok(tmp, "."); |
| 764 | while (pch != NULL) { |
| 765 | mstz_el[n] = pch; |
| 766 | pch = strtok(NULL, "."); |
| 767 | n++; |
| 768 | } |
| 769 | |
| 770 | /* sign */ |
| 771 | sign = atoi(mstz_el[0]); |
| 772 | printf("->Sign (0=+ / 1=-): %d\n", sign); |
| 773 | if (sign != 0 && sign != 1) { |
| 774 | printf("Invalid Sign \n"); |
| 775 | return -1; |
| 776 | } |
| 777 | /* nbquarters */ |
| 778 | nbquarters = atoi(mstz_el[1]); |
| 779 | printf("->Number of Quarters of an Hour : %d\n", nbquarters); |
| 780 | if (nbquarters < 0 || nbquarters > 79) { |
| 781 | printf("Invalid Number of Quarters \n"); |
| 782 | return -1; |
| 783 | } |
| 784 | /* DST */ |
| 785 | DST = atoi(mstz_el[2]); |
| 786 | printf("->Daylight Saving Time Adjustment : %d\n", DST); |
| 787 | if (DST < 0 || DST > 3) { |
| 788 | printf("Invalid DST Adjustment \n"); |
| 789 | return -1; |
| 790 | } |
| 791 | /* 12345678 |
| 792 | bits 123 = unit of # of quarters of an hour |
| 793 | bits 678 = # of quarters of an hour / 10 |
| 794 | bit 5 = sign |
| 795 | */ |
| 796 | i = nbquarters % 10; |
| 797 | i = i << 4; |
| 798 | i = i + nbquarters / 10 + 8 * sign; |
| 799 | /* options.mstz.v[0] = 0x69 ; */ |
| 800 | /* options.mstz.v[1] = 0x01 ; */ |
| 801 | options.mstz.v[0] = i; |
| 802 | options.mstz.v[1] = DST; |
| 803 | n = (i & 0x08) ? '-' : '+'; |
| 804 | printf |
| 805 | ("->Human Readable MS Time Zone : GMT %c %d hours %d minutes\n", |
| 806 | n, nbquarters / 4, nbquarters % 4 * 15); |
| 807 | } |
| 808 | |
| 809 | /* imeisv */ |
| 810 | if (args_info.imeisv_given == 1) { |
| 811 | options.imeisv_given = 1; |
| 812 | if (strlen(args_info.imeisv_arg) != 16) { |
| 813 | printf("Invalid IMEI(SV)\n"); |
| 814 | return -1; |
| 815 | } |
| 816 | options.imeisv.l = 8; |
| 817 | for (n = 0; n < 8; n++) { |
| 818 | a = (uint8_t) (args_info.imeisv_arg[2 * n] - 48); |
| 819 | b = (uint8_t) (args_info.imeisv_arg[2 * n + 1] - 48); |
| 820 | options.imeisv.v[n] = 16 * b + a; |
| 821 | } |
| 822 | printf("Using IMEI(SV): %s\n", args_info.imeisv_arg); |
| 823 | } |
| 824 | |
| 825 | /* msisdn */ |
| 826 | if (strlen(args_info.msisdn_arg) > (sizeof(options.msisdn.v) - 1)) { |
| 827 | printf("Invalid MSISDN\n"); |
| 828 | return -1; |
| 829 | } |
| 830 | options.msisdn.l = 1; |
| 831 | options.msisdn.v[0] = 0x91; /* International format */ |
| 832 | for (n = 0; n < strlen(args_info.msisdn_arg); n++) { |
| 833 | if ((n % 2) == 0) { |
| 834 | options.msisdn.v[((int)n / 2) + 1] = |
| 835 | args_info.msisdn_arg[n] - 48 + 0xf0; |
| 836 | options.msisdn.l += 1; |
| 837 | } else { |
| 838 | options.msisdn.v[((int)n / 2) + 1] = |
| 839 | (options.msisdn.v[((int)n / 2) + 1] & 0x0f) + |
| 840 | (args_info.msisdn_arg[n] - 48) * 16; |
| 841 | } |
| 842 | } |
| 843 | printf("Using MSISDN: %s\n", args_info.msisdn_arg); |
| 844 | |
| 845 | /* UID and PWD */ |
| 846 | /* Might need to also insert stuff like DNS etc. */ |
| 847 | if ((strlen(args_info.uid_arg) + strlen(args_info.pwd_arg) + 10) > |
| 848 | (sizeof(options.pco.v) - 1)) { |
| 849 | printf("invalid UID and PWD\n"); |
| 850 | return -1; |
| 851 | } |
| 852 | options.pco.l = |
| 853 | strlen(args_info.uid_arg) + strlen(args_info.pwd_arg) + 10; |
| 854 | options.pco.v[0] = 0x80; /* PPP */ |
| 855 | options.pco.v[1] = 0xc0; /* PAP */ |
| 856 | options.pco.v[2] = 0x23; |
| 857 | options.pco.v[3] = |
| 858 | strlen(args_info.uid_arg) + strlen(args_info.pwd_arg) + 6; |
| 859 | options.pco.v[4] = 0x01; /* Authenticate request */ |
| 860 | options.pco.v[5] = 0x01; |
| 861 | options.pco.v[6] = 0x00; /* MSB of length */ |
| 862 | options.pco.v[7] = |
| 863 | strlen(args_info.uid_arg) + strlen(args_info.pwd_arg) + 6; |
| 864 | options.pco.v[8] = strlen(args_info.uid_arg); |
| 865 | memcpy(&options.pco.v[9], args_info.uid_arg, strlen(args_info.uid_arg)); |
| 866 | options.pco.v[9 + strlen(args_info.uid_arg)] = |
| 867 | strlen(args_info.pwd_arg); |
| 868 | memcpy(&options.pco.v[10 + strlen(args_info.uid_arg)], |
| 869 | args_info.pwd_arg, strlen(args_info.pwd_arg)); |
| 870 | |
| 871 | /* createif */ |
| 872 | options.createif = args_info.createif_flag; |
Harald Welte | 73abc38 | 2017-10-10 08:50:11 +0800 | [diff] [blame] | 873 | options.tun_dev_name = args_info.tun_device_arg; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 874 | |
| 875 | /* net */ |
| 876 | /* Store net as in_addr net and mask */ |
| 877 | if (args_info.net_arg) { |
| 878 | if (ippool_aton |
Pau Espin Pedrol | f5e40b7 | 2017-12-14 14:01:23 +0100 | [diff] [blame] | 879 | (&options.net, &options.prefixlen, args_info.net_arg, 0)) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 880 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 881 | "Invalid network address: %s!", |
| 882 | args_info.net_arg); |
| 883 | exit(1); |
| 884 | } |
Pau Espin Pedrol | f5e40b7 | 2017-12-14 14:01:23 +0100 | [diff] [blame] | 885 | options.netaddr = options.net; |
| 886 | options.destaddr = options.net; |
jjako | c6762cf | 2004-04-28 14:52:58 +0000 | [diff] [blame] | 887 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 888 | } else { |
Pau Espin Pedrol | f5e40b7 | 2017-12-14 14:01:23 +0100 | [diff] [blame] | 889 | memset(&options.net, 0, sizeof(options.net)); |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 890 | options.prefixlen = 0; |
Pau Espin Pedrol | f5e40b7 | 2017-12-14 14:01:23 +0100 | [diff] [blame] | 891 | memset(&options.netaddr, 0, sizeof(options.netaddr)); |
| 892 | memset(&options.destaddr, 0, sizeof(options.destaddr)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 893 | } |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 894 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 895 | /* ipup */ |
| 896 | options.ipup = args_info.ipup_arg; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 897 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 898 | /* ipdown */ |
| 899 | options.ipdown = args_info.ipdown_arg; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 900 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 901 | /* statedir */ |
| 902 | options.statedir = args_info.statedir_arg; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 903 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 904 | /* defaultroute */ |
| 905 | options.defaultroute = args_info.defaultroute_flag; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 906 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 907 | /* pinghost */ |
| 908 | /* Store ping host as in_addr */ |
| 909 | if (args_info.pinghost_arg) { |
| 910 | if (!(host = gethostbyname(args_info.pinghost_arg))) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 911 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 912 | "Invalid ping host: %s!", |
| 913 | args_info.pinghost_arg); |
| 914 | return -1; |
| 915 | } else { |
| 916 | memcpy(&options.pinghost.s_addr, host->h_addr, |
| 917 | host->h_length); |
| 918 | printf("Using ping host: %s (%s)\n", |
| 919 | args_info.pinghost_arg, |
| 920 | inet_ntoa(options.pinghost)); |
| 921 | } |
| 922 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 923 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 924 | /* Other ping parameters */ |
| 925 | options.pingrate = args_info.pingrate_arg; |
| 926 | options.pingsize = args_info.pingsize_arg; |
| 927 | options.pingcount = args_info.pingcount_arg; |
| 928 | options.pingquiet = args_info.pingquiet_flag; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 929 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 930 | /* norecovery */ |
| 931 | options.norecovery_given = args_info.norecovery_flag; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 932 | |
Harald Welte | fbb9c7f | 2017-09-24 11:50:20 +0800 | [diff] [blame] | 933 | if (args_info.no_tx_gpdu_seq_flag) |
| 934 | options.tx_gpdu_seq = 0; |
| 935 | else |
| 936 | options.tx_gpdu_seq = 1; |
| 937 | |
Harald Welte | 840a8e9 | 2017-09-24 18:12:40 +0800 | [diff] [blame] | 938 | /* PDP Type */ |
| 939 | if (!strcmp(args_info.pdp_type_arg, "v6")) |
| 940 | options.pdp_type = PDP_EUA_TYPE_v6; |
Harald Welte | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 941 | else if (!strcmp(args_info.pdp_type_arg, "v4")) |
Harald Welte | 840a8e9 | 2017-09-24 18:12:40 +0800 | [diff] [blame] | 942 | options.pdp_type = PDP_EUA_TYPE_v4; |
Harald Welte | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 943 | else { |
| 944 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "Unsupported/unknown PDP Type '%s'\n", |
| 945 | args_info.pdp_type_arg); |
| 946 | return -1; |
| 947 | } |
| 948 | |
| 949 | if (options.pingcount && options.pdp_type != PDP_EUA_TYPE_v4) { |
| 950 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "built-in ping only works with IPv4, use tun-device"); |
| 951 | return -1; |
| 952 | } |
Harald Welte | 840a8e9 | 2017-09-24 18:12:40 +0800 | [diff] [blame] | 953 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 954 | return 0; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 955 | |
| 956 | } |
| 957 | |
Harald Welte | 081f30c | 2017-10-10 09:36:35 +0800 | [diff] [blame] | 958 | /* read a single value from a /procc file, up to 255 bytes, callee-allocated */ |
| 959 | static char *proc_read(const char *path) |
| 960 | { |
| 961 | char *ret = NULL; |
| 962 | FILE *f; |
| 963 | |
| 964 | f = fopen(path, "r"); |
| 965 | if (!f) |
| 966 | return NULL; |
| 967 | |
| 968 | ret = malloc(256); |
| 969 | if (!ret) |
| 970 | goto out; |
| 971 | |
| 972 | if (!fgets(ret, 256, f)) { |
| 973 | free(ret); |
| 974 | ret = NULL; |
| 975 | goto out; |
| 976 | } |
Harald Welte | 081f30c | 2017-10-10 09:36:35 +0800 | [diff] [blame] | 977 | |
| 978 | out: |
| 979 | fclose(f); |
| 980 | return ret; |
| 981 | } |
| 982 | |
| 983 | /* Read value of a /proc/sys/net/ipv6/conf file for given device. |
| 984 | * Memory is dynamically allocated, caller must free it later. */ |
| 985 | static char *proc_ipv6_conf_read(const char *dev, const char *file) |
| 986 | { |
| 987 | const char *fmt = "/proc/sys/net/ipv6/conf/%s/%s"; |
Harald Welte | b11ed0f | 2017-11-06 03:07:26 +0900 | [diff] [blame] | 988 | char path[strlen(fmt) + strlen(dev) + strlen(file)+1]; |
Harald Welte | 081f30c | 2017-10-10 09:36:35 +0800 | [diff] [blame] | 989 | snprintf(path, sizeof(path), fmt, dev, file); |
| 990 | return proc_read(path); |
| 991 | } |
| 992 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 993 | static char *print_ipprot(int t) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 994 | { |
Harald Welte | e37f48e | 2017-10-10 09:05:50 +0800 | [diff] [blame] | 995 | struct protoent *pe = getprotobynumber(t); |
| 996 | |
| 997 | if (!pe) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 998 | return "Unknown"; |
Harald Welte | e37f48e | 2017-10-10 09:05:50 +0800 | [diff] [blame] | 999 | else |
| 1000 | return pe->p_name; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1003 | static char *print_icmptype(int t) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1004 | { |
| 1005 | static char *ttab[] = { |
| 1006 | "Echo Reply", |
| 1007 | "ICMP 1", |
| 1008 | "ICMP 2", |
| 1009 | "Dest Unreachable", |
| 1010 | "Source Quench", |
| 1011 | "Redirect", |
| 1012 | "ICMP 6", |
| 1013 | "ICMP 7", |
| 1014 | "Echo", |
| 1015 | "ICMP 9", |
| 1016 | "ICMP 10", |
| 1017 | "Time Exceeded", |
| 1018 | "Parameter Problem", |
| 1019 | "Timestamp", |
| 1020 | "Timestamp Reply", |
| 1021 | "Info Request", |
| 1022 | "Info Reply" |
| 1023 | }; |
| 1024 | if (t < 0 || t > 16) |
| 1025 | return ("OUT-OF-RANGE"); |
| 1026 | return (ttab[t]); |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1029 | static int msisdn_add(struct ul16_t *src, struct ul16_t *dst, int add) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1030 | { |
| 1031 | unsigned int n; |
| 1032 | uint64_t i64 = 0; |
| 1033 | uint8_t msa[sizeof(i64) * 3]; /* Allocate 3 digits per octet (0..255) */ |
| 1034 | unsigned int msalen = 0; |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1035 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1036 | /* Convert to uint64_t from ul16_t format (most significant digit first) */ |
| 1037 | /* ul16_t format always starts with 0x91 to indicate international format */ |
| 1038 | /* In ul16_t format 0x0f/0xf0 indicates that digit is not used */ |
| 1039 | for (n = 0; n < src->l; n++) { |
| 1040 | if ((src->v[n] & 0x0f) != 0x0f) { |
| 1041 | i64 *= 10; |
| 1042 | i64 += src->v[n] & 0x0f; |
| 1043 | } |
| 1044 | if ((src->v[n] & 0xf0) != 0xf0) { |
| 1045 | i64 *= 10; |
| 1046 | i64 += (src->v[n] & 0xf0) >> 4; |
| 1047 | } |
| 1048 | } |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1049 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1050 | i64 += add; |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1051 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1052 | /* Generate array with least significant digit in first octet */ |
| 1053 | while (i64) { |
| 1054 | msa[msalen++] = i64 % 10; |
| 1055 | i64 = i64 / 10; |
| 1056 | } |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1057 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1058 | /* Convert back to ul16_t format */ |
| 1059 | for (n = 0; n < msalen; n++) { |
| 1060 | if ((n % 2) == 0) { |
| 1061 | dst->v[((int)n / 2)] = msa[msalen - n - 1] + 0xf0; |
| 1062 | dst->l += 1; |
| 1063 | } else { |
| 1064 | dst->v[((int)n / 2)] = (dst->v[((int)n / 2)] & 0x0f) + |
| 1065 | msa[msalen - n - 1] * 16; |
| 1066 | } |
| 1067 | } |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1068 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1069 | return 0; |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1070 | |
| 1071 | } |
| 1072 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1073 | static int imsi_add(uint64_t src, uint64_t * dst, int add) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1074 | { |
| 1075 | /* TODO: big endian / small endian ??? */ |
| 1076 | uint64_t i64 = 0; |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1077 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1078 | /* Convert from uint64_t bcd to uint64_t integer format */ |
| 1079 | /* The resulting integer format is multiplied by 10 */ |
| 1080 | while (src) { |
| 1081 | if ((src & 0x0f) != 0x0f) { |
| 1082 | i64 *= 10; |
| 1083 | i64 += (src & 0x0f); |
| 1084 | } |
| 1085 | if ((src & 0xf0) != 0xf0) { |
| 1086 | i64 *= 10; |
| 1087 | i64 += (src & 0xf0) >> 4; |
| 1088 | } |
| 1089 | src = src >> 8; |
| 1090 | } |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1091 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1092 | i64 += add * 10; |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1093 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1094 | *dst = 0; |
| 1095 | while (i64) { |
| 1096 | *dst = *dst << 4; |
| 1097 | *dst += (i64 % 10); |
| 1098 | i64 = i64 / 10; |
| 1099 | } |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1100 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1101 | *dst |= 0xf000000000000000ull; |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 1102 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1103 | return 0; |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1104 | |
| 1105 | } |
| 1106 | |
jjako | afb2a97 | 2003-01-29 21:04:13 +0000 | [diff] [blame] | 1107 | /* Calculate time left until we have to send off next ping packet */ |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1108 | static int ping_timeout(struct timeval *tp) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1109 | { |
| 1110 | struct timezone tz; |
| 1111 | struct timeval tv; |
| 1112 | int diff; |
| 1113 | if ((options.pinghost.s_addr) && (2 == state) && |
| 1114 | ((pingseq < options.pingcount) || (options.pingcount == 0))) { |
| 1115 | gettimeofday(&tv, &tz); |
| 1116 | diff = 1000000 / options.pingrate * pingseq - 1000000 * (tv.tv_sec - firstping.tv_sec) - (tv.tv_usec - firstping.tv_usec); /* Microseconds safe up to 500 sec */ |
| 1117 | tp->tv_sec = 0; |
| 1118 | if (diff > 0) |
| 1119 | tp->tv_usec = diff; |
| 1120 | else { |
| 1121 | /* For some reason we get packet loss if set to zero */ |
| 1122 | tp->tv_usec = 100000 / options.pingrate; /* 10 times pingrate */ |
| 1123 | tp->tv_usec = 0; |
| 1124 | } |
| 1125 | } |
| 1126 | return 0; |
jjako | afb2a97 | 2003-01-29 21:04:13 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1129 | /* Print out statistics when at the end of ping sequence */ |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1130 | static int ping_finish() |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1131 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1132 | struct timezone tz; |
| 1133 | struct timeval tv; |
| 1134 | int elapsed; |
| 1135 | gettimeofday(&tv, &tz); |
| 1136 | elapsed = 1000000 * (tv.tv_sec - firstping.tv_sec) + (tv.tv_usec - firstping.tv_usec); /* Microseconds */ |
| 1137 | printf("\n"); |
| 1138 | printf("\n----%s PING Statistics----\n", inet_ntoa(options.pinghost)); |
| 1139 | printf("%d packets transmitted in %.3f seconds, ", ntransmitted, |
| 1140 | elapsed / 1000000.0); |
| 1141 | printf("%d packets received, ", nreceived); |
| 1142 | if (ntransmitted) { |
| 1143 | if (nreceived > ntransmitted) |
| 1144 | printf("-- somebody's printing up packets!"); |
| 1145 | else |
| 1146 | printf("%d%% packet loss", |
| 1147 | (int)(((ntransmitted - nreceived) * 100) / |
| 1148 | ntransmitted)); |
| 1149 | } |
| 1150 | printf("\n"); |
| 1151 | if (options.debug) |
| 1152 | printf("%d packets received in total\n", ntreceived); |
| 1153 | if (nreceived && tsum) |
| 1154 | printf("round-trip (ms) min/avg/max = %.3f/%.3f/%.3f\n\n", |
| 1155 | tmin / 1000.0, tsum / 1000.0 / nreceived, tmax / 1000.0); |
| 1156 | printf("%d packets transmitted \n", ntreceived); |
jjako | afb2a97 | 2003-01-29 21:04:13 +0000 | [diff] [blame] | 1157 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1158 | ntransmitted = 0; |
| 1159 | return 0; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | /* Handle a received ping packet. Print out line and update statistics. */ |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1163 | static int encaps_ping(struct pdp_t *pdp, void *pack, unsigned len) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1164 | { |
| 1165 | struct timezone tz; |
| 1166 | struct timeval tv; |
| 1167 | struct timeval *tp; |
| 1168 | struct ip_ping *pingpack = pack; |
| 1169 | struct in_addr src; |
| 1170 | int triptime; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1171 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1172 | src.s_addr = pingpack->src; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1173 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1174 | gettimeofday(&tv, &tz); |
| 1175 | if (options.debug) |
| 1176 | printf("%d.%6d ", (int)tv.tv_sec, (int)tv.tv_usec); |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1177 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1178 | if (len < CREATEPING_IP + CREATEPING_ICMP) { |
| 1179 | printf("packet too short (%d bytes) from %s\n", len, |
| 1180 | inet_ntoa(src)); |
| 1181 | return 0; |
| 1182 | } |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1183 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1184 | ntreceived++; |
| 1185 | if (pingpack->protocol != 1) { |
| 1186 | if (!options.pingquiet) |
| 1187 | printf("%d bytes from %s: ip_protocol=%d (%s)\n", |
| 1188 | len, inet_ntoa(src), pingpack->protocol, |
| 1189 | print_ipprot(pingpack->protocol)); |
| 1190 | return 0; |
| 1191 | } |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1192 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1193 | if (pingpack->type != 0) { |
| 1194 | if (!options.pingquiet) |
| 1195 | printf |
| 1196 | ("%d bytes from %s: icmp_type=%d (%s) icmp_code=%d\n", |
| 1197 | len, inet_ntoa(src), pingpack->type, |
| 1198 | print_icmptype(pingpack->type), pingpack->code); |
| 1199 | return 0; |
| 1200 | } |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1201 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1202 | nreceived++; |
| 1203 | if (!options.pingquiet) |
| 1204 | printf("%d bytes from %s: icmp_seq=%d", len, |
| 1205 | inet_ntoa(src), ntohs(pingpack->seq)); |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1206 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1207 | if (len >= sizeof(struct timeval) + CREATEPING_IP + CREATEPING_ICMP) { |
| 1208 | gettimeofday(&tv, &tz); |
| 1209 | tp = (struct timeval *)pingpack->data; |
| 1210 | if ((tv.tv_usec -= tp->tv_usec) < 0) { |
| 1211 | tv.tv_sec--; |
| 1212 | tv.tv_usec += 1000000; |
| 1213 | } |
| 1214 | tv.tv_sec -= tp->tv_sec; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1215 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1216 | triptime = tv.tv_sec * 1000000 + (tv.tv_usec); |
| 1217 | tsum += triptime; |
| 1218 | if (triptime < tmin) |
| 1219 | tmin = triptime; |
| 1220 | if (triptime > tmax) |
| 1221 | tmax = triptime; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1222 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1223 | if (!options.pingquiet) |
| 1224 | printf(" time=%.3f ms\n", triptime / 1000.0); |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1225 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1226 | } else if (!options.pingquiet) |
| 1227 | printf("\n"); |
| 1228 | return 0; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | /* Create a new ping packet and send it off to peer. */ |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1232 | static int create_ping(void *gsn, struct pdp_t *pdp, |
| 1233 | struct in_addr *dst, int seq, unsigned int datasize) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1234 | { |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1235 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1236 | struct ip_ping pack; |
| 1237 | uint16_t *p = (uint16_t *) & pack; |
| 1238 | uint8_t *p8 = (uint8_t *) & pack; |
| 1239 | struct in_addr src; |
| 1240 | unsigned int n; |
| 1241 | long int sum = 0; |
| 1242 | int count = 0; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1243 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1244 | struct timezone tz; |
| 1245 | struct timeval *tp = |
| 1246 | (struct timeval *)&p8[CREATEPING_IP + CREATEPING_ICMP]; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1247 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1248 | if (datasize > CREATEPING_MAX) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 1249 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1250 | "Ping size to large: %d!", datasize); |
| 1251 | return -1; |
| 1252 | } |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1253 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1254 | memcpy(&src, &(pdp->eua.v[2]), 4); /* Copy a 4 byte address */ |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1255 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1256 | pack.ipver = 0x45; |
| 1257 | pack.tos = 0x00; |
| 1258 | pack.length = htons(CREATEPING_IP + CREATEPING_ICMP + datasize); |
| 1259 | pack.fragid = 0x0000; |
| 1260 | pack.offset = 0x0040; |
| 1261 | pack.ttl = 0x40; |
| 1262 | pack.protocol = 0x01; |
| 1263 | pack.ipcheck = 0x0000; |
| 1264 | pack.src = src.s_addr; |
| 1265 | pack.dst = dst->s_addr; |
| 1266 | pack.type = 0x08; |
| 1267 | pack.code = 0x00; |
| 1268 | pack.checksum = 0x0000; |
| 1269 | pack.ident = 0x0000; |
| 1270 | pack.seq = htons(seq); |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1271 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1272 | /* Generate ICMP payload */ |
| 1273 | p8 = (uint8_t *) & pack + CREATEPING_IP + CREATEPING_ICMP; |
| 1274 | for (n = 0; n < (datasize); n++) |
| 1275 | p8[n] = n; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1276 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1277 | if (datasize >= sizeof(struct timeval)) |
| 1278 | gettimeofday(tp, &tz); |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1279 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1280 | /* Calculate IP header checksum */ |
| 1281 | p = (uint16_t *) & pack; |
| 1282 | count = CREATEPING_IP; |
| 1283 | sum = 0; |
| 1284 | while (count > 1) { |
| 1285 | sum += *p++; |
| 1286 | count -= 2; |
| 1287 | } |
| 1288 | while (sum >> 16) |
| 1289 | sum = (sum & 0xffff) + (sum >> 16); |
| 1290 | pack.ipcheck = ~sum; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1291 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1292 | /* Calculate ICMP checksum */ |
| 1293 | count = CREATEPING_ICMP + datasize; /* Length of ICMP message */ |
| 1294 | sum = 0; |
| 1295 | p = (uint16_t *) & pack; |
| 1296 | p += CREATEPING_IP / 2; |
| 1297 | while (count > 1) { |
| 1298 | sum += *p++; |
| 1299 | count -= 2; |
| 1300 | } |
| 1301 | if (count > 0) |
| 1302 | sum += *(unsigned char *)p; |
| 1303 | while (sum >> 16) |
| 1304 | sum = (sum & 0xffff) + (sum >> 16); |
| 1305 | pack.checksum = ~sum; |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1306 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1307 | ntransmitted++; |
| 1308 | return gtp_data_req(gsn, pdp, &pack, 28 + datasize); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1311 | static int delete_context(struct pdp_t *pdp) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1312 | { |
| 1313 | |
| 1314 | if (tun && options.ipdown) |
| 1315 | tun_runscript(tun, options.ipdown); |
| 1316 | |
Pau Espin Pedrol | dbeaa04 | 2018-02-12 19:11:28 +0100 | [diff] [blame] | 1317 | ipdel((struct iphash_t *)pdp->peer[0]); |
| 1318 | memset(pdp->peer[0], 0, sizeof(struct iphash_t)); /* To be sure */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1319 | |
| 1320 | if (1 == options.contexts) |
| 1321 | state = 5; /* Disconnected */ |
| 1322 | |
| 1323 | return 0; |
| 1324 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1325 | |
Harald Welte | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 1326 | /* Link-Local address prefix fe80::/64 */ |
| 1327 | static const uint8_t ll_prefix[] = { 0xfe,0x80, 0,0, 0,0, 0,0 }; |
| 1328 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1329 | /* Callback for receiving messages from tun */ |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1330 | static int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1331 | { |
| 1332 | struct iphash_t *ipm; |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 1333 | struct in46_addr src; |
Harald Welte | 63ebccd | 2017-08-02 21:10:09 +0200 | [diff] [blame] | 1334 | struct iphdr *iph = (struct iphdr *)pack; |
Harald Welte | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 1335 | struct ip6_hdr *ip6h = (struct ip6_hdr *)pack; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1336 | |
Harald Welte | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 1337 | if (iph->version == 4) { |
| 1338 | if (len < sizeof(*iph) || len < 4*iph->ihl) { |
| 1339 | printf("Dropping packet with too short IP header\n"); |
| 1340 | return 0; |
| 1341 | } |
| 1342 | src.len = 4; |
| 1343 | src.v4.s_addr = iph->saddr; |
| 1344 | } else if (iph->version == 6) { |
| 1345 | /* We only have a single entry in the hash table, and it consists of the link-local |
| 1346 | * address "fe80::prefix". So we need to make sure to convert non-link-local source |
| 1347 | * addresses to that format before looking up the hash table via ippool_getip() */ |
| 1348 | src.len = 16; |
| 1349 | if (!memcmp(ip6h->ip6_src.s6_addr, ll_prefix, sizeof(ll_prefix))) { |
| 1350 | /* is a link-local address, we can do the hash lookup 1:1 */ |
| 1351 | src.v6 = ip6h->ip6_src; |
| 1352 | } else { |
| 1353 | /* it is not a link-local address, so we must convert from the /64 prefix |
| 1354 | * to the link-local format that's used in the hash table */ |
| 1355 | memcpy(&src.v6.s6_addr[0], ll_prefix, sizeof(ll_prefix)); |
| 1356 | memcpy(&src.v6.s6_addr[sizeof(ll_prefix)], ip6h->ip6_src.s6_addr, 16-sizeof(ll_prefix)); |
| 1357 | } |
| 1358 | } else { |
| 1359 | printf("Dropping packet with invalid IP version %u\n", iph->version); |
| 1360 | return 0; |
| 1361 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1362 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1363 | if (ipget(&ipm, &src)) { |
Neels Hofmeyr | 041824d | 2015-10-19 13:26:39 +0200 | [diff] [blame] | 1364 | printf("Dropping packet from invalid source address: %s\n", |
Harald Welte | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 1365 | in46a_ntoa(&src)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1366 | return 0; |
| 1367 | } |
| 1368 | |
| 1369 | if (ipm->pdp) /* Check if a peer protocol is defined */ |
| 1370 | gtp_data_req(gsn, ipm->pdp, pack, len); |
| 1371 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1374 | static int create_pdp_conf(struct pdp_t *pdp, void *cbp, int cause) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1375 | { |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 1376 | struct in46_addr addr; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1377 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1378 | struct iphash_t *iph = (struct iphash_t *)cbp; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1379 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1380 | if (cause < 0) { |
| 1381 | printf("Create PDP Context Request timed out\n"); |
| 1382 | if (iph->pdp->version == 1) { |
| 1383 | printf("Retrying with version 0\n"); |
| 1384 | iph->pdp->version = 0; |
| 1385 | gtp_create_context_req(gsn, iph->pdp, iph); |
| 1386 | return 0; |
| 1387 | } else { |
| 1388 | state = 0; |
| 1389 | pdp_freepdp(iph->pdp); |
| 1390 | iph->pdp = NULL; |
| 1391 | return EOF; |
| 1392 | } |
| 1393 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1394 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1395 | if (cause != 128) { |
| 1396 | printf |
| 1397 | ("Received create PDP context response. Cause value: %d\n", |
| 1398 | cause); |
| 1399 | state = 0; |
| 1400 | pdp_freepdp(iph->pdp); |
| 1401 | iph->pdp = NULL; |
| 1402 | return EOF; /* Not what we expected */ |
| 1403 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1404 | |
Viktor Tsymbalyuk | a2a08f7 | 2018-01-26 12:47:55 +0200 | [diff] [blame] | 1405 | if (in46a_from_eua(&pdp->eua, &addr) < 1) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1406 | printf |
| 1407 | ("Received create PDP context response. Cause value: %d\n", |
| 1408 | cause); |
| 1409 | pdp_freepdp(iph->pdp); |
| 1410 | iph->pdp = NULL; |
| 1411 | state = 0; |
| 1412 | return EOF; /* Not a valid IP address */ |
| 1413 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1414 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1415 | printf("Received create PDP context response. IP address: %s\n", |
Harald Welte | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 1416 | in46a_ntoa(&addr)); |
| 1417 | |
| 1418 | switch (addr.len) { |
| 1419 | case 16: /* IPv6 */ |
| 1420 | /* we have to enable the kernel to perform stateless autoconfiguration, |
| 1421 | * i.e. send a router solicitation using the lover 64bits of the allocated |
| 1422 | * EUA as interface identifier, as per 3GPP TS 29.061 Section 11.2.1.3.2 */ |
| 1423 | memcpy(addr.v6.s6_addr, ll_prefix, sizeof(ll_prefix)); |
| 1424 | printf("Derived IPv6 link-local address: %s\n", in46a_ntoa(&addr)); |
| 1425 | break; |
| 1426 | case 4: /* IPv4 */ |
| 1427 | break; |
| 1428 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1429 | |
Pau Espin Pedrol | f5e40b7 | 2017-12-14 14:01:23 +0100 | [diff] [blame] | 1430 | if ((options.createif) && (!options.net.len)) { |
Harald Welte | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 1431 | size_t prefixlen = 32; |
| 1432 | if (addr.len == 16) |
| 1433 | prefixlen = 64; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1434 | /* printf("Setting up interface and routing\n"); */ |
Harald Welte | 6748dc9 | 2017-09-24 21:54:59 +0800 | [diff] [blame] | 1435 | /* FIXME: use tun_addattr() not tun_setaddr() */ |
| 1436 | tun_setaddr(tun, &addr, &addr, prefixlen); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1437 | if (options.defaultroute) { |
| 1438 | struct in_addr rm; |
| 1439 | rm.s_addr = 0; |
Harald Welte | b4c0828 | 2018-04-25 16:55:39 +0200 | [diff] [blame^] | 1440 | netdev_addroute(&rm, &addr.v4, &rm); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1441 | } |
| 1442 | if (options.ipup) |
| 1443 | tun_runscript(tun, options.ipup); |
| 1444 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1445 | |
Harald Welte | 081f30c | 2017-10-10 09:36:35 +0800 | [diff] [blame] | 1446 | /* now that ip-up has been executed, check if we are configured to |
| 1447 | * accept router advertisements */ |
| 1448 | if (options.createif && options.pdp_type == PDP_EUA_TYPE_v6) { |
| 1449 | char *accept_ra, *forwarding; |
| 1450 | |
| 1451 | accept_ra = proc_ipv6_conf_read(tun->devname, "accept_ra"); |
| 1452 | forwarding = proc_ipv6_conf_read(tun->devname, "forwarding"); |
| 1453 | if (!accept_ra || !forwarding) |
| 1454 | printf("Could not open proc file for %s ?!?\n", tun->devname); |
| 1455 | else { |
| 1456 | if (!strcmp(accept_ra, "0") || |
| 1457 | (!strcmp(forwarding, "1") && !strcmp(accept_ra, "1"))) { |
| 1458 | printf("%s is %s, i.e. your tun device is not configured to accept " |
| 1459 | "router advertisements; SLAAC will not suceed, please " |
| 1460 | "fix your setup!\n"); |
| 1461 | } |
Harald Welte | 081f30c | 2017-10-10 09:36:35 +0800 | [diff] [blame] | 1462 | } |
Harald Welte | b589e78 | 2017-11-06 03:09:35 +0900 | [diff] [blame] | 1463 | free(accept_ra); |
| 1464 | free(forwarding); |
Harald Welte | 081f30c | 2017-10-10 09:36:35 +0800 | [diff] [blame] | 1465 | } |
| 1466 | |
Viktor Tsymbalyuk | 7ad4d5e | 2018-02-12 14:00:45 +0200 | [diff] [blame] | 1467 | ipset(iph, &addr); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1468 | |
| 1469 | state = 2; /* Connected */ |
| 1470 | |
| 1471 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1474 | static int delete_pdp_conf(struct pdp_t *pdp, int cause) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1475 | { |
| 1476 | printf("Received delete PDP context response. Cause value: %d\n", |
| 1477 | cause); |
| 1478 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1481 | static int echo_conf(int recovery) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1482 | { |
jjako | 91aaf22 | 2003-10-22 10:09:32 +0000 | [diff] [blame] | 1483 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1484 | if (recovery < 0) { |
| 1485 | printf("Echo Request timed out\n"); |
| 1486 | if (echoversion == 1) { |
| 1487 | printf("Retrying with version 0\n"); |
| 1488 | echoversion = 0; |
| 1489 | gtp_echo_req(gsn, echoversion, NULL, &options.remote); |
| 1490 | return 0; |
| 1491 | } else { |
| 1492 | state = 0; |
| 1493 | return EOF; |
| 1494 | } |
| 1495 | } else { |
| 1496 | printf("Received echo response\n"); |
| 1497 | if (!options.contexts) |
| 1498 | state = 5; |
| 1499 | } |
| 1500 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1503 | static int conf(int type, int cause, struct pdp_t *pdp, void *cbp) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1504 | { |
| 1505 | /* if (cause < 0) return 0; Some error occurred. We don't care */ |
| 1506 | switch (type) { |
| 1507 | case GTP_ECHO_REQ: |
| 1508 | return echo_conf(cause); |
| 1509 | case GTP_CREATE_PDP_REQ: |
| 1510 | return create_pdp_conf(pdp, cbp, cause); |
| 1511 | case GTP_DELETE_PDP_REQ: |
| 1512 | if (cause != 128) |
| 1513 | return 0; /* Request not accepted. We don't care */ |
| 1514 | return delete_pdp_conf(pdp, cause); |
| 1515 | default: |
| 1516 | return 0; |
| 1517 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Harald Welte | fed3389 | 2017-10-10 09:02:45 +0800 | [diff] [blame] | 1520 | static int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1521 | { |
| 1522 | /* printf("encaps_tun. Packet received: forwarding to tun\n"); */ |
| 1523 | return tun_encaps((struct tun_t *)pdp->ipif, pack, len); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | int main(int argc, char **argv) |
| 1527 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1528 | fd_set fds; /* For select() */ |
| 1529 | struct timeval idleTime; /* How long to select() */ |
| 1530 | struct pdp_t *pdp; |
| 1531 | int n; |
| 1532 | int starttime = time(NULL); /* Time program was started */ |
| 1533 | int stoptime = 0; /* Time to exit */ |
| 1534 | int pingtimeout = 0; /* Time to print ping statistics */ |
bjovan | a8f71eb | 2017-02-24 17:39:20 +0100 | [diff] [blame] | 1535 | int signal_received; /* If select() on fd_set is interrupted by signal. */ |
jjako | afb2a97 | 2003-01-29 21:04:13 +0000 | [diff] [blame] | 1536 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1537 | struct timezone tz; /* Used for calculating ping times */ |
| 1538 | struct timeval tv; |
| 1539 | int diff; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1540 | |
bjovan | a8f71eb | 2017-02-24 17:39:20 +0100 | [diff] [blame] | 1541 | signal(SIGTERM, signal_handler); |
| 1542 | signal(SIGHUP, signal_handler); |
| 1543 | signal(SIGINT, signal_handler); |
| 1544 | |
Pau Espin Pedrol | 042a445 | 2018-04-17 14:31:42 +0200 | [diff] [blame] | 1545 | tall_sgsnemu_ctx = talloc_named_const(NULL, 0, "sgsnemu"); |
| 1546 | msgb_talloc_ctx_init(tall_sgsnemu_ctx, 0); |
| 1547 | osmo_init_logging2(tall_sgsnemu_ctx, &log_info); |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 1548 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1549 | /* Process options given in configuration file and command line */ |
| 1550 | if (process_options(argc, argv)) |
| 1551 | exit(1); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1552 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1553 | printf("\nInitialising GTP library\n"); |
| 1554 | if (gtp_new(&gsn, options.statedir, &options.listen, GTP_MODE_SGSN)) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 1555 | SYS_ERR(DSGSN, LOGL_ERROR, 0, "Failed to create gtp"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1556 | exit(1); |
| 1557 | } |
| 1558 | if (gsn->fd0 > maxfd) |
| 1559 | maxfd = gsn->fd0; |
| 1560 | if (gsn->fd1c > maxfd) |
| 1561 | maxfd = gsn->fd1c; |
| 1562 | if (gsn->fd1u > maxfd) |
| 1563 | maxfd = gsn->fd1u; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1564 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1565 | gtp_set_cb_delete_context(gsn, delete_context); |
| 1566 | gtp_set_cb_conf(gsn, conf); |
| 1567 | if (options.createif) |
| 1568 | gtp_set_cb_data_ind(gsn, encaps_tun); |
| 1569 | else |
| 1570 | gtp_set_cb_data_ind(gsn, encaps_ping); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1571 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1572 | if (options.createif) { |
| 1573 | printf("Setting up interface\n"); |
| 1574 | /* Create a tunnel interface */ |
Harald Welte | 73abc38 | 2017-10-10 08:50:11 +0800 | [diff] [blame] | 1575 | if (tun_new((struct tun_t **)&tun, options.tun_dev_name)) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 1576 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1577 | "Failed to create tun"); |
| 1578 | exit(1); |
| 1579 | } |
| 1580 | tun_set_cb_ind(tun, cb_tun_ind); |
| 1581 | if (tun->fd > maxfd) |
| 1582 | maxfd = tun->fd; |
| 1583 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1584 | |
Pau Espin Pedrol | f5e40b7 | 2017-12-14 14:01:23 +0100 | [diff] [blame] | 1585 | if ((options.createif) && (options.net.len)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1586 | /* printf("Setting up interface and routing\n"); */ |
Pau Espin Pedrol | f5e40b7 | 2017-12-14 14:01:23 +0100 | [diff] [blame] | 1587 | tun_addaddr(tun, &options.netaddr, &options.destaddr, options.prefixlen); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1588 | if (options.defaultroute) { |
| 1589 | struct in_addr rm; |
| 1590 | rm.s_addr = 0; |
Harald Welte | b4c0828 | 2018-04-25 16:55:39 +0200 | [diff] [blame^] | 1591 | netdev_addroute(&rm, &options.destaddr.v4, &rm); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1592 | } |
| 1593 | if (options.ipup) |
| 1594 | tun_runscript(tun, options.ipup); |
| 1595 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1596 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1597 | /* Initialise hash tables */ |
| 1598 | memset(&iphash, 0, sizeof(iphash)); |
| 1599 | memset(&iparr, 0, sizeof(iparr)); |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1600 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1601 | printf("Done initialising GTP library\n\n"); |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1602 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1603 | /* See if anybody is there */ |
| 1604 | printf("Sending off echo request\n"); |
| 1605 | echoversion = options.gtpversion; |
| 1606 | gtp_echo_req(gsn, echoversion, NULL, &options.remote); /* Is remote alive? */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1607 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1608 | for (n = 0; n < options.contexts; n++) { |
| 1609 | uint64_t myimsi; |
| 1610 | printf("Setting up PDP context #%d\n", n); |
| 1611 | iparr[n].inuse = 1; /* TODO */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1612 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1613 | imsi_add(options.imsi, &myimsi, n); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1614 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1615 | /* Allocated here. */ |
| 1616 | /* If create context failes we have to deallocate ourselves. */ |
| 1617 | /* Otherwise it is deallocated by gtplib */ |
| 1618 | pdp_newpdp(&pdp, myimsi, options.nsapi, NULL); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1619 | |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 1620 | pdp->peer[0] = &iparr[n]; /* FIXME: support v4v6, have 2 peers */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1621 | pdp->ipif = tun; /* TODO */ |
| 1622 | iparr[n].pdp = pdp; |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1623 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1624 | if (options.gtpversion == 0) { |
| 1625 | if (options.qos.l - 1 > sizeof(pdp->qos_req0)) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 1626 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1627 | "QoS length too big"); |
| 1628 | exit(1); |
| 1629 | } else { |
| 1630 | memcpy(pdp->qos_req0, options.qos.v, |
| 1631 | options.qos.l); |
| 1632 | } |
| 1633 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1634 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1635 | pdp->qos_req.l = options.qos.l; |
| 1636 | memcpy(pdp->qos_req.v, options.qos.v, options.qos.l); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1637 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1638 | pdp->selmode = options.selmode; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1639 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1640 | pdp->rattype.l = options.rattype.l; |
| 1641 | memcpy(pdp->rattype.v, options.rattype.v, options.rattype.l); |
| 1642 | pdp->rattype_given = options.rattype_given; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 1643 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1644 | pdp->userloc.l = options.userloc.l; |
| 1645 | memcpy(pdp->userloc.v, options.userloc.v, options.userloc.l); |
| 1646 | pdp->userloc_given = options.userloc_given; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 1647 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1648 | pdp->rai.l = options.rai.l; |
| 1649 | memcpy(pdp->rai.v, options.rai.v, options.rai.l); |
| 1650 | pdp->rai_given = options.rai_given; |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 1651 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1652 | pdp->mstz.l = options.mstz.l; |
| 1653 | memcpy(pdp->mstz.v, options.mstz.v, options.mstz.l); |
| 1654 | pdp->mstz_given = options.mstz_given; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 1655 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1656 | pdp->imeisv.l = options.imeisv.l; |
| 1657 | memcpy(pdp->imeisv.v, options.imeisv.v, options.imeisv.l); |
| 1658 | pdp->imeisv_given = options.imeisv_given; |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 1659 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1660 | pdp->norecovery_given = options.norecovery_given; |
Harald Welte | 3a4c67b | 2011-10-07 18:45:54 +0200 | [diff] [blame] | 1661 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1662 | if (options.apn.l > sizeof(pdp->apn_use.v)) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 1663 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1664 | "APN length too big"); |
| 1665 | exit(1); |
| 1666 | } else { |
| 1667 | pdp->apn_use.l = options.apn.l; |
| 1668 | memcpy(pdp->apn_use.v, options.apn.v, options.apn.l); |
| 1669 | } |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 1670 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1671 | pdp->gsnlc.l = sizeof(options.listen); |
| 1672 | memcpy(pdp->gsnlc.v, &options.listen, sizeof(options.listen)); |
| 1673 | pdp->gsnlu.l = sizeof(options.listen); |
| 1674 | memcpy(pdp->gsnlu.v, &options.listen, sizeof(options.listen)); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1675 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1676 | if (options.msisdn.l > sizeof(pdp->msisdn.v)) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 1677 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1678 | "MSISDN length too big"); |
| 1679 | exit(1); |
| 1680 | } else { |
| 1681 | msisdn_add(&options.msisdn, &pdp->msisdn, n); |
| 1682 | } |
jjako | b62c3dd | 2004-05-27 18:51:55 +0000 | [diff] [blame] | 1683 | |
Harald Welte | 840a8e9 | 2017-09-24 18:12:40 +0800 | [diff] [blame] | 1684 | /* Request dynamic IP address */ |
| 1685 | pdp->eua.v[0] = PDP_EUA_ORG_IETF; |
| 1686 | pdp->eua.v[1] = options.pdp_type; |
| 1687 | pdp->eua.l = 2; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1688 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1689 | if (options.pco.l > sizeof(pdp->pco_req.v)) { |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 1690 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1691 | "PCO length too big"); |
| 1692 | exit(1); |
| 1693 | } else { |
| 1694 | pdp->pco_req.l = options.pco.l; |
| 1695 | memcpy(pdp->pco_req.v, options.pco.v, options.pco.l); |
| 1696 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1697 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1698 | pdp->version = options.gtpversion; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1699 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1700 | pdp->hisaddr0 = options.remote; |
| 1701 | pdp->hisaddr1 = options.remote; |
| 1702 | |
Pau Espin Pedrol | 7c4de07 | 2017-12-14 13:59:02 +0100 | [diff] [blame] | 1703 | pdp->cch_pdp = options.cch; /* 2048 = Normal, 1024 = Prepaid, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1704 | 512 = Flat rate, 256 = Hot billing */ |
| 1705 | |
Harald Welte | fbb9c7f | 2017-09-24 11:50:20 +0800 | [diff] [blame] | 1706 | pdp->tx_gpdu_seq = options.tx_gpdu_seq; |
| 1707 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1708 | /* Create context */ |
| 1709 | /* We send this of once. Retransmissions are handled by gtplib */ |
| 1710 | gtp_create_context_req(gsn, pdp, &iparr[n]); |
| 1711 | } |
| 1712 | |
| 1713 | state = 1; /* Enter wait_connection state */ |
| 1714 | |
| 1715 | printf("Waiting for response from ggsn........\n\n"); |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1716 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1717 | /******************************************************************/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1718 | /* Main select loop */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1719 | /******************************************************************/ |
| 1720 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1721 | while ((0 != state) && (5 != state)) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1722 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1723 | /* Take down client after timeout after disconnect */ |
| 1724 | if ((4 == state) && ((stoptime) <= time(NULL))) { |
| 1725 | state = 5; |
| 1726 | } |
jjako | 7b8fad4 | 2003-07-07 14:37:42 +0000 | [diff] [blame] | 1727 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1728 | /* Take down client after timelimit timeout */ |
| 1729 | if ((2 == state) && (options.timelimit) && |
| 1730 | ((starttime + options.timelimit) <= time(NULL))) { |
| 1731 | state = 3; |
| 1732 | } |
jjako | 7b8fad4 | 2003-07-07 14:37:42 +0000 | [diff] [blame] | 1733 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1734 | /* Take down client after ping timeout */ |
| 1735 | if ((2 == state) && (pingtimeout) |
| 1736 | && (pingtimeout <= time(NULL))) { |
| 1737 | state = 3; |
| 1738 | } |
jjako | 7b8fad4 | 2003-07-07 14:37:42 +0000 | [diff] [blame] | 1739 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1740 | /* Set pingtimeout for later disconnection */ |
| 1741 | if (options.pingcount && ntransmitted >= options.pingcount) { |
| 1742 | pingtimeout = time(NULL) + 5; /* Extra seconds */ |
| 1743 | } |
jjako | 7b8fad4 | 2003-07-07 14:37:42 +0000 | [diff] [blame] | 1744 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1745 | /* Print statistics if no more ping packets are missing */ |
| 1746 | if (ntransmitted && options.pingcount |
| 1747 | && nreceived >= options.pingcount) { |
| 1748 | ping_finish(); |
| 1749 | if (!options.createif) |
| 1750 | state = 3; |
| 1751 | } |
jjako | 7b8fad4 | 2003-07-07 14:37:42 +0000 | [diff] [blame] | 1752 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1753 | /* Send off disconnect */ |
| 1754 | if (3 == state) { |
| 1755 | state = 4; |
| 1756 | stoptime = time(NULL) + 5; /* Extra seconds to allow disconnect */ |
| 1757 | for (n = 0; n < options.contexts; n++) { |
| 1758 | /* Delete context */ |
| 1759 | printf("Disconnecting PDP context #%d\n", n); |
| 1760 | gtp_delete_context_req(gsn, iparr[n].pdp, NULL, |
| 1761 | 1); |
| 1762 | if ((options.pinghost.s_addr != 0) |
| 1763 | && ntransmitted) |
| 1764 | ping_finish(); |
| 1765 | } |
| 1766 | } |
jjako | 7b8fad4 | 2003-07-07 14:37:42 +0000 | [diff] [blame] | 1767 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1768 | /* Send of ping packets */ |
| 1769 | diff = 0; |
| 1770 | while ((diff <= 0) && |
| 1771 | /* Send off an ICMP ping packet */ |
| 1772 | /*if ( */ (options.pinghost.s_addr) && (2 == state) && |
| 1773 | ((pingseq < options.pingcount) |
| 1774 | || (options.pingcount == 0))) { |
| 1775 | if (!pingseq) |
| 1776 | gettimeofday(&firstping, &tz); /* Set time of first ping */ |
| 1777 | gettimeofday(&tv, &tz); |
| 1778 | diff = 1000000 / options.pingrate * pingseq - 1000000 * (tv.tv_sec - firstping.tv_sec) - (tv.tv_usec - firstping.tv_usec); /* Microseconds safe up to 500 sec */ |
| 1779 | if (diff <= 0) { |
| 1780 | if (options.debug) |
| 1781 | printf("Create_ping %d\n", diff); |
| 1782 | create_ping(gsn, |
| 1783 | iparr[pingseq % |
| 1784 | options.contexts].pdp, |
| 1785 | &options.pinghost, pingseq, |
| 1786 | options.pingsize); |
| 1787 | pingseq++; |
| 1788 | } |
| 1789 | } |
jjako | 5da6845 | 2003-01-28 16:08:47 +0000 | [diff] [blame] | 1790 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1791 | FD_ZERO(&fds); |
| 1792 | if (tun) |
| 1793 | FD_SET(tun->fd, &fds); |
| 1794 | FD_SET(gsn->fd0, &fds); |
| 1795 | FD_SET(gsn->fd1c, &fds); |
| 1796 | FD_SET(gsn->fd1u, &fds); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1797 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1798 | gtp_retranstimeout(gsn, &idleTime); |
| 1799 | ping_timeout(&idleTime); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1800 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1801 | if (options.debug) |
| 1802 | printf("idletime.tv_sec %d, idleTime.tv_usec %d\n", |
| 1803 | (int)idleTime.tv_sec, (int)idleTime.tv_usec); |
jjako | 7b8fad4 | 2003-07-07 14:37:42 +0000 | [diff] [blame] | 1804 | |
bjovan | a8f71eb | 2017-02-24 17:39:20 +0100 | [diff] [blame] | 1805 | signal_received = 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1806 | switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) { |
| 1807 | case -1: |
bjovan | a8f71eb | 2017-02-24 17:39:20 +0100 | [diff] [blame] | 1808 | if (errno == EINTR) |
| 1809 | signal_received = 1; |
| 1810 | else |
| 1811 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
| 1812 | "Select returned -1"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1813 | break; |
| 1814 | case 0: |
| 1815 | gtp_retrans(gsn); /* Only retransmit if nothing else */ |
| 1816 | break; |
| 1817 | default: |
| 1818 | break; |
| 1819 | } |
| 1820 | |
bjovan | a8f71eb | 2017-02-24 17:39:20 +0100 | [diff] [blame] | 1821 | if (!signal_received) { |
| 1822 | |
| 1823 | if ((tun) && FD_ISSET(tun->fd, &fds) && tun_decaps(tun) < 0) { |
| 1824 | SYS_ERR(DSGSN, LOGL_ERROR, 0, |
| 1825 | "TUN decaps failed"); |
| 1826 | } |
| 1827 | |
| 1828 | if (FD_ISSET(gsn->fd0, &fds)) |
| 1829 | gtp_decaps0(gsn); |
| 1830 | |
| 1831 | if (FD_ISSET(gsn->fd1c, &fds)) |
| 1832 | gtp_decaps1c(gsn); |
| 1833 | |
| 1834 | if (FD_ISSET(gsn->fd1u, &fds)) |
| 1835 | gtp_decaps1u(gsn); |
| 1836 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1837 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | gtp_free(gsn); /* Clean up the gsn instance */ |
| 1841 | |
| 1842 | if (options.createif) |
| 1843 | tun_free(tun); |
| 1844 | |
| 1845 | if (0 == state) |
| 1846 | exit(1); /* Indicate error */ |
| 1847 | |
| 1848 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1849 | } |